Skip to content

Don't grow truncated headers in write() - #635

Merged
j-t-1 merged 2 commits into
erocarrera:masterfrom
HuzaifaChaudary:fix/write-truncated-headers
Oct 30, 2026
Merged

j-t-1 merged 2 commits into
erocarrera:masterfrom
HuzaifaChaudary:fix/write-truncated-headers

Conversation

@HuzaifaChaudary

@HuzaifaChaudary HuzaifaChaudary commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 of write() , so for those files it was off by the 27 extra bytes in the length term : tinyXP.exe gave 0x91e9 where the checksum of the file is 0x91ce .

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 need tests/test_files , same as on master .

Copilot AI balanced review requested due to automatic review settings October 22, 2026 20:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@j-t-1 j-t-1 left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/pefile_test.py Resolved
Comment thread tests/pefile_test.py Resolved
Comment thread tests/pefile_test.py Resolved
Comment thread pefile.py Resolved
@HuzaifaChaudary

Copy link
Copy Markdown
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
j-t-1 merged commit e46112c into erocarrera:master Oct 30, 2026
25 checks passed
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 .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants