-
Notifications
You must be signed in to change notification settings - Fork 163
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
pe: make sure authenticode is identical before/after signature #383
Conversation
When adding the signature, the last section of the file will be padded to 8-bytes align. We need to make sure the payload we feed to a signer is always padded to 8-bytes. This fixes signature breakage.
3f6b748
to
621bce2
Compare
I ended up re implementing the authenticode parser according to the specification. I think it's better that way, although a little bit more complex. |
if file_size > sum_of_bytes_hashed { | ||
let extra_data_start = sum_of_bytes_hashed; | ||
let len = | ||
file_size - sections.certificate_table_size - sum_of_bytes_hashed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
file_size - sections.certificate_table_size - sum_of_bytes_hashed; | |
file_size - sections.certificate_table_size + sum_of_bytes_hashed; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec calls for:
(File Size) - ((Size of AttributeCertificateTable) + SUM_OF_BYTES_HASHED)
^ ^
I can go with file_size - (sections.certificate_table_size + sum_of_bytes_hashed)
, but your suggestion is wrong I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's probably wrong :) I thought it was more important to point out the error.
0d8ac79
to
d70d582
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only question about allocation; otherwise, looks good to me, thankfully authenticode::ExcludedSections is not pub so not a breaking change
} | ||
} | ||
} | ||
|
||
pub struct ExcludedSectionsIter<'s> { | ||
pe: &'s PE<'s>, | ||
state: IterState, | ||
sections: VecDeque<SectionTable>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doesn't this mean the iterator allocates every time it's created? this is a little unusual for iterator implementations (most people expect the allocation to occur at their own choosing, e.g., from collect::<Vec<_>>()
or whatever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it does. But parsing PE already requires you to allocate (for the section table specifically).
I don't really have a better option here. The spec requires me to sort the section tables, and there is a non-fixed amount of them.
// image. The NumberOfSections field of COFF File Header indicates how big | ||
// the table should be. Do not include any section headers in the table whose | ||
// SizeOfRawData field is zero. | ||
let mut sections: VecDeque<SectionTable> = self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'd not expect allocations in the iterator itself; (same comment as above); is there anyway to avoid allocating? (if there's not, or it's too difficult, that's fine, just something to maybe document?)
after this change I believe we'll do a release, which will have the breaking changes we've accumulated, thanks for everyone's patience |
@baloo Just to be sure I understand the change. Are you aligning the file size itself or are you just making sure the buffer you are feeding the singer is aligned? The spec demands the file size itself to be aligned. The buffer you feed the signed is missing 12 bytes which is going to affect the padding you are applying. |
@Foxboron The file itself might be read-only (stored in usr/lib like
The 8 bytes alignment problem is already handled when we actually write to the file. (this was taken care of in #361) |
Sure, but you need to check the actual file size, not the buffer you are feeding the signer. I don't know Rust but
Tells me you are not passing the file size itself, just a slice of the bytes you are hashing. The actual file size is defined as |
I would probably not hide the padding between |
10b2f78
to
66ef2ae
Compare
@Foxboron I see your point. Padding was done by virtue of hash_size holding the data that was sent to the hasher / signer. I think the code was working because the I rewrote the logic in the last commit to be closer to the spec, and what you expect (? I think?). Just to confirm, you meant 8-bytes aligned right? I can't find any requirement to align stuff to 12-bytes in the spec, nor any other implementation (not even in yours). |
// NOTE (safety): pad size will be at most 7, and PADDING has a size of 7 | ||
// pad_size is computed ~10 lines above. | ||
debug_assert!(pad_size <= 7); | ||
debug_assert_eq!(PADDING.len(), 7); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@m4b are those debug_assert
fine? I thought I'd might as well back up my claims?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If they’re some internal check that should only be false if someone refactors this code and doesn’t maintain some internal invariant afterwards then yea. If they’re like things that probably should be true but maybe aren’t if the binary is malformed, then no, should be an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no, they are only meant to protect against a refactor.
Yes, when I mention the 12 bytes I'm thinking of the bytes we are omitting from the hash buffer (checksum + optional datadir 4). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's go!
released in 0.8.0, happy new years! |
Thanks! and happy new year to you too! |
When adding the signature, the last section of the file will be padded to 8-bytes align. We need to make sure the payload we feed to a signer is always padded to 8-bytes.
This fixes signature breakage.
cc @RaitoBezarius
Thanks to @Foxboron for the heads up!