Repository navigation
Don't grow truncated headers in write() - #635
Merged
j-t-1 merged 2 commits intoOct 30, 2026
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 <[email protected]>
j-t-1
reviewed
Oct 27, 2026
Collaborator
There was a problem hiding this comment.
Thank you for finding and fixing this. This is a good find.
found by computing the checksum of every PE in the .NET 10 SDK and the radare2 test binaries , 3419 files , with an independent implementation of the algorithm . pefile matched it on all but those three , and matches on all of them now . unmodified write() round trips on 3 more files than before .
These are excellent checks, other parts of PE file could do with this kind of third-party check.
Contributor
Author
|
thanks . applied all four suggestions in 12d1d4d , the condition reads the same way as before , and the test still passes . |
j-t-1
pushed a commit
that referenced
this pull request
Nov 8, 2026
the four commented out checks use files from the old checksum folder , which is not in pefile-tests . these use corkami files that are . multiss_drv.sys , multiss_gui.exe and multiss_con.exe only differ at offset 0x9c , the Subsystem field , set to 1 , 2 and 3 . all three carry the checksum of the driver , 0x255c , so the driver one is good and the other two are off by 1 and 2 . tinydrivXP.sys is the 97 byte file whose optional header runs past the end , it was the case #635 fixed , and that check fails on the pefile from before #635 . the values match radare2 , which prints the computed checksum next to the one in the header ( rabin2 -I , cmp.csum and hdr.csum ) , and a separate implementation of the checksum . pefile , radare2 and that implementation agree on every file in pefile-tests that radare2 loads , 354 of 356 .
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
when a header is cut short by the end of the file , pefile reads it zero padded , and
write()then put the whole structure back , so an unmodified file came out longer . the 97 byte tiny PE files (tinyXP.exe,tinydllXP.dll,tinydrivXP.sys, whose optional header runs past the end ) came back as 124 bytes .generate_checksum()checksums the output ofwrite(), so for those files it was off by the 27 extra bytes in the length term :tinyXP.exegave0x91e9where the checksum of the file is0x91ce.write()now leaves out the part of a structure past the end of the file when it is still the zero padding . if a value was written there it is kept as before , so the file still grows when it has to .found by computing the checksum of every PE in the .NET 10 SDK and the radare2 test binaries , 3419 files , with an independent implementation of the algorithm . pefile matched it on all but those three , and matches on all of them now . unmodified
write()round trips on 3 more files than before .the new test builds a PE32 header that ends 16 bytes into the optional header , checks
write()returns it unchanged , and that a value set past the end is still written . it fails before the change . the other 17 failing tests here needtests/test_files, same as on master .