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

PE: TLS parser rework #435

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 9 additions & 19 deletions src/pe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,25 +221,15 @@ impl<'a> PE<'a> {
}

if let Some(tls_table) = optional_header.data_directories.get_tls_table() {
tls_data = if is_64 {
tls::TlsData::parse_with_opts::<u64>(
bytes,
image_base,
tls_table,
&sections,
file_alignment,
opts,
)?
} else {
tls::TlsData::parse_with_opts::<u32>(
bytes,
image_base,
&tls_table,
&sections,
file_alignment,
opts,
)?
};
tls_data = tls::TlsData::parse_with_opts(
bytes,
image_base,
tls_table,
&sections,
file_alignment,
opts,
is_64,
)?;
debug!("tls data: {:#?}", tls_data);
}

Expand Down
30 changes: 19 additions & 11 deletions src/pe/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,30 @@ pub struct TlsData<'a> {
}

impl ImageTlsDirectory {
pub fn parse<T: Sized>(
pub fn parse(
bytes: &[u8],
dd: data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32,
is_64: bool,
) -> error::Result<Self> {
Self::parse_with_opts::<T>(
Self::parse_with_opts(
bytes,
dd,
sections,
file_alignment,
&options::ParseOptions::default(),
is_64,
)
}

pub fn parse_with_opts<T: Sized>(
pub fn parse_with_opts(
bytes: &[u8],
dd: data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32,
opts: &options::ParseOptions,
is_64: bool,
) -> error::Result<Self> {
let rva = dd.virtual_address as usize;
let mut offset =
Expand All @@ -76,8 +79,6 @@ impl ImageTlsDirectory {
))
})?;

let is_64 = core::mem::size_of::<T>() == 8;

let start_address_of_raw_data = if is_64 {
bytes.gread_with::<u64>(&mut offset, scroll::LE)?
} else {
Expand Down Expand Up @@ -115,39 +116,40 @@ impl ImageTlsDirectory {
}

impl<'a> TlsData<'a> {
pub fn parse<T: Sized>(
pub fn parse(
bytes: &'a [u8],
image_base: usize,
dd: &data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32,
is_64: bool,
) -> error::Result<Option<Self>> {
Self::parse_with_opts::<T>(
Self::parse_with_opts(
bytes,
image_base,
dd,
sections,
file_alignment,
&options::ParseOptions::default(),
is_64,
)
}

pub fn parse_with_opts<T: Sized>(
pub fn parse_with_opts(
bytes: &'a [u8],
image_base: usize,
dd: &data_directories::DataDirectory,
sections: &[section_table::SectionTable],
file_alignment: u32,
opts: &options::ParseOptions,
is_64: bool,
) -> error::Result<Option<Self>> {
let mut raw_data = None;
let mut slot = None;
let mut callbacks = Vec::new();

let is_64 = core::mem::size_of::<T>() == 8;

let itd =
ImageTlsDirectory::parse_with_opts::<T>(bytes, *dd, sections, file_alignment, opts)?;
ImageTlsDirectory::parse_with_opts(bytes, *dd, sections, file_alignment, opts, is_64)?;

// Parse the raw data if any
if itd.end_address_of_raw_data != 0 && itd.start_address_of_raw_data != 0 {
Expand Down Expand Up @@ -176,6 +178,12 @@ impl<'a> TlsData<'a> {
rva
))
})?;
if offset + size as usize > bytes.len() {
return Err(error::Error::Malformed(format!(
"tls raw data offset ({:#x}) and size ({:#x}) greater than byte slice len ({:#x})",
offset, size, bytes.len()
)));
}
raw_data = Some(&bytes[offset..offset + size as usize]);
}

Expand Down
Loading