-
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
Fix fail on malformed certificate table parsing #417
Fix fail on malformed certificate table parsing #417
Conversation
2642c9f
to
2c7f7b5
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.
need some clarification on default certificate directory; also formatting things are odd
src/pe/mod.rs
Outdated
use alloc::borrow::Cow; | ||
use alloc::string::String; | ||
use alloc::vec::Vec; | ||
use core::cmp::max; |
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'm surprised by these import changes, did the formatter do this?
src/pe/mod.rs
Outdated
@@ -142,7 +140,7 @@ impl<'a> PE<'a> { | |||
return Err(error::Error::Malformed(format!( | |||
"Unsupported header magic ({:#x})", | |||
magic | |||
))) | |||
))); |
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.
ditto here too, this is also surprising to me
src/pe/mod.rs
Outdated
) | ||
.unwrap_or_else(|err| { | ||
warn!("Cannot parse CertificateTable: {:?}", err); | ||
Default::default() |
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 can't remember, what is a default certificate directory in this case? is it going to cause other problems further down the line when parsing, or if a user accesses parts of it, will it panic? Does it have offsets into other parts of the PE file that are no longer valid, etc.?
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.
default is empty table (no certificates), so no wrong offsets there
So while this is an easy merge, I'm on the fence about whether we should; in general the malformed binary is kind of important to know, and in general, we choose to fail in those cases. However, sometimes we don't, and maybe this is one of those times, but it feels like it's just sort of skipping a malformed thing, and putting a default value in its place, which may be ok, but it also might not be. So I'd like to understand more about:
thanks for your patience! |
OK, that sounds reasonable. What if we add something like |
b94d9a3
to
ac97d4c
Compare
@@ -8,6 +8,16 @@ pub struct ParseOptions { | |||
/// memory](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#other-contents-of-the-file). | |||
/// For on-disk representations, leave as true. Default: true | |||
pub parse_attribute_certificates: bool, | |||
/// Whether or not to end with an error in case of incorrect data or continue parsing if able. Default: ParseMode::Strict |
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.
debating whether we should add #[non_exhaustive]
to this struct to make it future compatible if we need to add another field like this in the future (and not break people)
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.
this looks good to me, except as mentioned, but since we're here, i'd like to see the:
- non_exhaustive attrib added to ParseOptions
- it actually implement
Default
trait - add
fn te() -> Self
method for constructing the parse options that TE requires
@@ -16,6 +26,7 @@ impl ParseOptions { | |||
ParseOptions { |
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.
also i don't know who added this but this does not implement Default
, but has a method named that instead; this should be fixed to actually implement Default
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.
thanks for review, just pushed fixes
@@ -16,6 +26,7 @@ impl ParseOptions { | |||
ParseOptions { | |||
resolve_rva: true, | |||
parse_attribute_certificates: true, | |||
parse_mode: ParseMode::Strict, | |||
} |
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.
can you also add a method that returns what the TE parser uses; we can call it fn te()
or something like that, instead of manually constructing it in TE portion.
|
||
impl ParseOptions { | ||
pub fn te() -> Self { | ||
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.
more idiomatic would be something like:
Self {
resolve_rva: false,
parse_attribute_certificates: false,
.. Self::default()
}
this way if new methods are added don't (maybe) need to update this location in source, but it's fine. I'm also wondering if we should make this non pub
, just to reduce the api surface for now, i can fix this up though if you don't feel like pushing again.
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.
this is great, thank you!
fix private
NOTE: breaking change #434 |
Hello!
In some PE files Certificate Table can be malformed / contain invalid data. But if we use
ParseOptions::parse_attribute_certificates = true
, then whole parsing is failed.I suggest use default
CertificateDirectoryTable
in case of error.