elf: Fix for mismatched app ELF file not detected.

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.
This commit is contained in:
Angus Gratton
2023-04-12 18:18:30 +10:00
committed by BOT
parent 0f977b68bc
commit 2d26ace5e5
2 changed files with 8 additions and 1 deletions

View File

@@ -284,6 +284,13 @@ class ElfNoteSegment(ElfSegment):
super(ElfNoteSegment, self).__init__(addr, data, flags) super(ElfNoteSegment, self).__init__(addr, data, flags)
self.type = ElfFile.PT_NOTE self.type = ElfFile.PT_NOTE
self.note_secs = NoteSections.parse(self.data) 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 @staticmethod
def _type_str(): # type: () -> str def _type_str(): # type: () -> str

View File

@@ -261,7 +261,7 @@ class EspCoreDumpLoader(EspCoreDumpVersion):
for seg in core_elf.note_segments: for seg in core_elf.note_segments:
for note_sec in seg.note_secs: for note_sec in seg.note_secs:
# Check for version info note # 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 note_sec.type == ESPCoreDumpElfFile.PT_INFO \
and exe_name: and exe_name:
exe_elf = ElfFile(exe_name) exe_elf = ElfFile(exe_name)