Skip to content
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

Merged
merged 7 commits into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::container;
use crate::error;
use crate::pe::utils::pad;
use crate::strtab;
use options::ParseMode;

use scroll::{ctx, Pwrite};

Expand Down Expand Up @@ -264,11 +265,19 @@ impl<'a> PE<'a> {
if let Some(&certificate_table) =
optional_header.data_directories.get_certificate_table()
{
certificates = certificate_table::enumerate_certificates(
let certificates_result = certificate_table::enumerate_certificates(
bytes,
certificate_table.virtual_address,
certificate_table.size,
)?;
);

certificates = match opts.parse_mode {
ParseMode::Strict => certificates_result?,
ParseMode::Permissive => certificates_result.unwrap_or_else(|err| {
warn!("Cannot parse CertificateTable: {:?}", err);
Default::default()
}),
};

certificate_table.size as usize
} else {
Expand Down Expand Up @@ -522,10 +531,7 @@ pub struct TE<'a> {
impl<'a> TE<'a> {
/// Reads a TE binary from the underlying `bytes`
pub fn parse(bytes: &'a [u8]) -> error::Result<Self> {
let opts = &options::ParseOptions {
resolve_rva: false,
parse_attribute_certificates: false,
};
let opts = &options::ParseOptions::te();

let mut offset = 0;

Expand Down
26 changes: 24 additions & 2 deletions src/pe/options.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/// Parsing Options structure for the PE parser
#[non_exhaustive]
#[derive(Debug, Copy, Clone)]
pub struct ParseOptions {
/// Wether the parser should resolve rvas or not. Default: true
Expand All @@ -8,14 +9,35 @@ 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
Copy link
Owner

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)

pub parse_mode: ParseMode,
}

impl ParseOptions {
#[derive(Debug, Copy, Clone)]
pub enum ParseMode {
/// Always end with error on incorrect data
Strict,
/// Incorrect data will not cause to end with error if possible
Permissive,
}

impl Default for ParseOptions {
/// Returns a parse options structure with default values
pub fn default() -> Self {
fn default() -> Self {
ParseOptions {
Copy link
Owner

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

Copy link
Contributor Author

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

resolve_rva: true,
parse_attribute_certificates: true,
parse_mode: ParseMode::Strict,
}
Copy link
Owner

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(crate) fn te() -> Self {
Self {
Copy link
Owner

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.

resolve_rva: false,
parse_attribute_certificates: false,
parse_mode: ParseMode::Strict,
}
}
}
Loading