Skip to content

Commit

Permalink
elf: Fix for mismatched app ELF file not detected.
Browse files Browse the repository at this point in the history
The check that the app ELF file SHA256 matches the one stored in the core dump
would never fail, leading to gdb loading the wrong ELF file and either crashing
or producing misleading debug information.

Specifics:

The note_sec.name field was incorrectly read back as b'ESP_CORE_DUMP_INFO\x00E',
because the namesz length includes the terminating NUL byte and possible junk
padding bytes:
https://github.com/espressif/esp-idf/blob/master/components/espcoredump/src/core_dump_elf.c#L212

In addition, as 'note_sec.name' is a bytes object Python 3 would have never
successfully compared it with a string.
  • Loading branch information
projectgus authored and espressif-bot committed Jun 21, 2023
1 parent 0f977b6 commit 2d26ace
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
7 changes: 7 additions & 0 deletions components/espcoredump/corefile/elf.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ def __init__(self, addr, data, flags): # type: (int, bytes, int) -> None
super(ElfNoteSegment, self).__init__(addr, data, flags)
self.type = ElfFile.PT_NOTE
self.note_secs = NoteSections.parse(self.data)
for note in self.note_secs:
# note.name should include a terminating NUL byte, plus possible
# padding
#
# (note: construct.PaddingString can't parse this if there
# are non-zero padding bytes after the NUL, it also parses those.)
note.name = note.name.split(b'\x00')[0]

@staticmethod
def _type_str(): # type: () -> str
Expand Down
2 changes: 1 addition & 1 deletion components/espcoredump/corefile/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def _extract_elf_corefile(self, exe_name=None, e_machine=ESPCoreDumpElfFile.EM_X
for seg in core_elf.note_segments:
for note_sec in seg.note_secs:
# Check for version info note
if note_sec.name == 'ESP_CORE_DUMP_INFO' \
if note_sec.name == b'ESP_CORE_DUMP_INFO' \
and note_sec.type == ESPCoreDumpElfFile.PT_INFO \
and exe_name:
exe_elf = ElfFile(exe_name)
Expand Down

0 comments on commit 2d26ace

Please sign in to comment.