Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: Fix for mismatched app ELF file not detected. (IDFGH-9855) #3

Merged
merged 3 commits into from
Apr 14, 2023

Conversation

projectgus
Copy link
Contributor

@projectgus projectgus commented Apr 12, 2023

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.

@projectgus
Copy link
Contributor Author

If possible, I'd appreciate a backport to the ESP-IDF release/v4.4 branch as this is where I stumbled across it.

I hope you folks are all doing well. :)

@github-actions github-actions bot changed the title elf: Fix for mismatched app ELF file not detected. elf: Fix for mismatched app ELF file not detected. (IDFGH-9855) Apr 12, 2023
@projectgus
Copy link
Contributor Author

projectgus commented Apr 12, 2023

Hmm OK, this now fails for another reason. The core dump I have only contains the first 16 characters of the ELF SHA, for some reason:

00006f60  00 00 00 00 00 00 00 00  14 00 00 00 48 00 00 00  |............H...|
00006f70  4a 20 00 00 45 53 50 5f  43 4f 52 45 5f 44 55 4d  |J ..ESP_CORE_DUM|
00006f80  50 5f 49 4e 46 4f 00 45  00 01 09 00 63 36 36 34  |P_INFO.E....c664|
00006f90  66 66 38 61 61 66 39 31  37 32 31 39 00 00 00 00  |ff8aaf917219....|
00006fa0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

The remaining bytes are there, but all zero.

EDIT:

    config APP_RETRIEVE_LEN_ELF_SHA
        int "The length of APP ELF SHA is stored in RAM(chars)"
        default 16

ah yeah,that'll do it.

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.
@igrr
Copy link
Member

igrr commented Apr 12, 2023

Hey @projectgus, thanks for the fix!

> The remaining bytes are there, but all zero.

This could be related to CONFIG_APP_RETRIEVE_LEN_ELF_SHA setting, could you please check how the app is configured?

(I now see your updated comment)

@projectgus
Copy link
Contributor Author

@igrr yeah, I just remembered/saw this now as well!

So I guess the Python code needs to compare all the bytes in the core dump file, except for any trailing 00 bytes?

@igrr
Copy link
Member

igrr commented Apr 12, 2023

I think so, yes... Ideally we should have also recorded the length of the truncated SHA256 there, but alas.

Over here we use in to check for the SHA256 match:

https://github.com/espressif/esp-idf-monitor/blob/6a633628a380437bf3cb4a226ac1bfdec79eed5b/esp_idf_monitor/base/serial_handler.py#L165-L182

which is not very precise, but i guess the likelihood that the truncated SHA256 is a substring of a different SHA256 is pretty low in practice.

The comment says it returns the "SHA256 hash of the input ELF file", but this is
not true - it was the SHA256 hash of the output ELF file. As the parser may
change some bytes around in minor ways, these were often not the same.
With the default APP_RETRIEVE_LEN_ELF_SHA setting, core dump files only have a
truncated ELF SHA256 in them. Account for this when comparing the core dump SHA
with the app ELF SHA.
@projectgus
Copy link
Contributor Author

Thanks for the explanation, @igrr. Have pushed some fixes that seem to allow esp-coredump.py to detect correctly if the APP ELF matches or not.

This fix may potentially save a lot of hair-pulling about "nonsense" core dump debug output!

@igrr
Copy link
Member

igrr commented Apr 17, 2023

@projectgus I have done the backport of your fixes to IDF release/v4.4, but it turned out the ELF & coredump files used in our CI actually didn't have matching SHA256. (Which is understandable, since these were just "dummy" ELF files, but it never occurred to me that such combination is not supposed to work.)
We'll need a bit of time to fix this test issue and will merge the backport once that is done.

@projectgus
Copy link
Contributor Author

No worries @igrr, thanks for the update!

@dobairoland
Copy link
Collaborator

Thank you @projectgus for the contribution. It revealed a couple of issues in our tests which had to fixed first. That is why this took long to solve. Now it is merged to the v4.4 release branch and should be available soon. Probably it will be released in v4.4.6.

Here are the commits in v4.4 for future reference:

@projectgus
Copy link
Contributor Author

No worries at all @dobairoland, thanks for the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants