From 2d26ace5e5baaa29587187f760a4163f359a925d Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Wed, 12 Apr 2023 18:18:30 +1000 Subject: [PATCH] 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. --- components/espcoredump/corefile/elf.py | 7 +++++++ components/espcoredump/corefile/loader.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/components/espcoredump/corefile/elf.py b/components/espcoredump/corefile/elf.py index 8ffa12b6cd..76c2c60a63 100644 --- a/components/espcoredump/corefile/elf.py +++ b/components/espcoredump/corefile/elf.py @@ -284,6 +284,13 @@ class ElfNoteSegment(ElfSegment): 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 diff --git a/components/espcoredump/corefile/loader.py b/components/espcoredump/corefile/loader.py index 144a350826..8f9fce7442 100644 --- a/components/espcoredump/corefile/loader.py +++ b/components/espcoredump/corefile/loader.py @@ -261,7 +261,7 @@ class EspCoreDumpLoader(EspCoreDumpVersion): 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)