Skip to content

Commit

Permalink
Fix to not implicitly dereference result of ElfFile::getSectionByInde…
Browse files Browse the repository at this point in the history
…x() (handle ELFs with bad section header offsets)

Summary: Fix several instances of callers of `getSectionByIndex()` implicitly dereferencing the result rather than checking for `nullptr`.

Reviewed By: jwiepert

Differential Revision: D48000246

fbshipit-source-id: af95986d8ee0ea1ce10438e9cebd9b70740e5c99
  • Loading branch information
nslingerland authored and facebook-github-bot committed Aug 2, 2023
1 parent bf3979f commit 4dcb3c6
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions folly/experimental/symbolizer/Elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,16 +350,23 @@ const char* ElfFile::getSectionName(const ElfShdr& section) const noexcept {
return nullptr; // no section name string table
}

const ElfShdr& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
return getString(sectionNames, section.sh_name);
auto stringSection = getSectionByIndex(elfHeader().e_shstrndx);
if (!stringSection) {
return nullptr;
}
return getString(*stringSection, section.sh_name);
}

const ElfShdr* ElfFile::getSectionByName(const char* name) const noexcept {
if (elfHeader().e_shstrndx == SHN_UNDEF) {
return nullptr; // no section name string table
}

const ElfShdr& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
auto stringSection = getSectionByIndex(elfHeader().e_shstrndx);
if (!stringSection) {
return nullptr;
}
const ElfShdr& sectionNames = *stringSection;
const char* start = file_ + sectionNames.sh_offset;

// Find section with the appropriate sh_name offset
Expand Down Expand Up @@ -419,8 +426,11 @@ ElfFile::Symbol ElfFile::getSymbolByName(
if (sym.st_name == 0) {
return false; // no name for this symbol
}
const char* sym_name =
getString(*getSectionByIndex(section.sh_link), sym.st_name);
auto linkSection = getSectionByIndex(section.sh_link);
if (!linkSection) {
return false;
}
const char* sym_name = getString(*linkSection, sym.st_name);
if (strcmp(sym_name, name) == 0) {
foundSymbol.first = &section;
foundSymbol.second = &sym;
Expand Down Expand Up @@ -460,8 +470,11 @@ const char* ElfFile::getSymbolName(const Symbol& symbol) const noexcept {
return nullptr; // symbol table has no strings
}

return getString(
*getSectionByIndex(symbol.first->sh_link), symbol.second->st_name);
auto linkSection = getSectionByIndex(symbol.first->sh_link);
if (!linkSection) {
return nullptr;
}
return getString(*linkSection, symbol.second->st_name);
}

std::pair<const int, char const*> ElfFile::posixFadvise(
Expand Down

0 comments on commit 4dcb3c6

Please sign in to comment.