Repository navigation
Compute the PE checksum while writing the output file - #540
Open
francoisgirinon-netizen wants to merge 1 commit into
Open
francoisgirinon-netizen wants to merge 1 commit into
francoisgirinon-netizen wants to merge 1 commit into
Conversation
After writing a signed or unsigned PE file, pe_update_data_size() read the whole output file back to compute its checksum. On a multi-GB file this is a full extra read of the output. The checksum is a ones' complement sum of 16-bit words plus the file size, so it can be accumulated on the data as it is written: the input bytes copied by pe_modify_header() (each 64 KB chunk is summed right after it is written, while still in the CPU cache), the WIN_CERTIFICATE and signature, and the new Certificate Table entry. The zeroed CheckSum field and the zero padding add nothing. Each byte is summed according to its own file offset, so the result does not depend on the parity of the offsets. No value stored in the input file is trusted. The output files are byte-for-byte identical to the previous ones.
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.
Pull Request Type
Related Issue
Issue number: N/A (follow-up to #513)
Current Behavior
After writing a signed or unsigned PE file,
pe_update_data_size()seeks back tothe start of the output and reads the whole file again through the BIO, 64 KB at a
time, only to compute the
CheckSumfield (pe_calc_checksum()). On large filesthis read back is a large part of the time of
signandremove-signature.New Behavior
The checksum is a one's complement sum of 16-bit words plus the file size, so it is
now accumulated on the data as it is written, and the output file is no longer read
back.
signon a 1 GB PE file takes 1.9 s instead of 12.9 s,remove-signature2.6 s instead of 7.4 s. The output files are byte identical.
Scope of Changes
pe.c:pe_modify_header()sums the input bytes it copies, each 64 KB chunk rightafter it is written. The zeroed
CheckSumfield and Certificate Table entry,and the zero padding, add nothing.
pe_append_pkcs7()sums theWIN_CERTIFICATEheader and the signature.pe_update_data_size()sums the new Certificate Table entry, then folds the sumwith
pe_checksum_fold()and the output size.pe_checksum_add()wrapspe_checksum_sum()for data at any file offset: abyte at an odd offset is the high byte of its word, so the result does not
depend on where the data is split.
pe_calc_checksum()is removed.pe_calc_realchecksum()(used byverify) isunchanged.
Testing
Existing tests
New tests added
Manual testing
ctest: 424/424 passed on Windows x86-64 (MSVC 2019, OpenSSL 3.5.9), withoutLARGE_FILE_TESTS. This includes thechecksum_*tests from Speed up PE checksum calculation #513, which comparethe
CheckSumfield with an independent implementation.The output of this branch and of
masterwere compared byte for byte on 266cases: PE32 and PE32+ files, with and without an overlay, with 1 to 7 trailing
bytes, with an odd
e_lfanew, and with a signature table at an odd offset;commands
sign,sign -ph,sign -nest, re-signing,add,remove-signature,attach-signatureandattach-signature -nest. Allidentical.
Timings (median of 7 alternating runs, same build options, 1 GB PE file with a
random overlay, in the page cache):
signremove-signatureattach-signatureattach-signaturedoes not change:check_attached_data()still reads back andhashes the whole output file, and that dominates its time.
Additional Notes
pe_update_data_size()now relies onpe_modify_header(), and onpe_append_pkcs7()with the same signature, having run on the same context.Every caller in
main_execute()does; the function comment says so.License Declaration