Skip to content

Commit

Permalink
Merge pull request #42 from DataDog/rgs/safe-access-parse-program-hea…
Browse files Browse the repository at this point in the history
…ders

use safeaccess to avoid dereferencing unreadable pointers in program headers
  • Loading branch information
richardstartin authored Nov 8, 2023
2 parents 4fe47d3 + 33e2856 commit cea7b60
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions ddprof-lib/src/main/cpp/symbols_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "symbols.h"
#include "dwarf.h"
#include "log.h"
#include "safeAccess.h"


class SymbolDesc {
Expand Down Expand Up @@ -212,9 +213,16 @@ ElfProgramHeader* ElfParser::findProgramHeader(uint32_t type) {
const char* pheaders = (const char*)_header + _header->e_phoff;

for (int i = 0; i < _header->e_phnum; i++) {
ElfProgramHeader* pheader = (ElfProgramHeader*)(pheaders + i * _header->e_phentsize);
if (pheader->p_type == type) {
return pheader;
const char* unvalidated_pheader = pheaders + i * _header->e_phentsize;
// check we can load the pointer
void* checked = SafeAccess::load((void**) unvalidated_pheader);
if (checked == NULL) {
return NULL;
} else {
ElfProgramHeader* pheader = (ElfProgramHeader*) unvalidated_pheader;
if (pheader->p_type == type) {
return pheader;
}
}
}

Expand Down

0 comments on commit cea7b60

Please sign in to comment.