From 36ac103264634cafe2944c33a9677ce9a8b2ac2d Mon Sep 17 00:00:00 2001 From: Andrii Nakryiko Date: Mon, 27 Nov 2017 11:31:18 -0800 Subject: [PATCH] Fix ElfFile crashing when opening short (<64 bytes) files. Summary: ElfFile in init() assumes file is long enough to contain complete ElfHeader, which is 64 bytes long. This is not true for valid non-ELF files like short scripts. They shouldn't cause crash. Reviewed By: myreg Differential Revision: D6410210 fbshipit-source-id: 28fd017d8de17c431d7d006a1655ade8a95994bd --- folly/experimental/symbolizer/Elf.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/folly/experimental/symbolizer/Elf.cpp b/folly/experimental/symbolizer/Elf.cpp index 071f1b43..9cf3d137 100644 --- a/folly/experimental/symbolizer/Elf.cpp +++ b/folly/experimental/symbolizer/Elf.cpp @@ -193,19 +193,24 @@ void ElfFile::reset() { } bool ElfFile::init(const char** msg) { - auto& elfHeader = this->elfHeader(); + if (length_ < 4) { + if (msg) { + *msg = "not an ELF file (too short)"; + } + return false; + } // Validate ELF magic numbers - if (!(elfHeader.e_ident[EI_MAG0] == ELFMAG0 && - elfHeader.e_ident[EI_MAG1] == ELFMAG1 && - elfHeader.e_ident[EI_MAG2] == ELFMAG2 && - elfHeader.e_ident[EI_MAG3] == ELFMAG3)) { + if (file_[EI_MAG0] != ELFMAG0 || file_[EI_MAG1] != ELFMAG1 || + file_[EI_MAG2] != ELFMAG2 || file_[EI_MAG3] != ELFMAG3) { if (msg) { *msg = "invalid ELF magic"; } return false; } + auto& elfHeader = this->elfHeader(); + #define EXPECTED_CLASS P1(ELFCLASS, __ELF_NATIVE_CLASS) #define P1(a, b) P2(a, b) #define P2(a, b) a##b -- 2.34.1