You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'd like to see this in next breaking change rollup if the callbacks field should be something like TLSCallbackIterator rather than a Vec<T> which requires alloc, so we can make TlsData derive Copy, Clone, Pread and Pwrite.
The size of entries can only known at parsing e.g., as following; so it still can raise errors for malformed callback entry when PE::parse*, not when the iterator is called.
Optionally, we can make callbacks virtual address validation (if utils::find_offset(callback.wrapping_sub...) configurable in opts like opts.validate_tls_callbacks_va so validation won't occur when consumers does not want it w.r.t. intentionally malformed binaries.
/// TLS information.#[derive(Debug,Clone,PartialEq,Default)]pubstructTlsData<'a>{/// TLS directory.pubimage_tls_directory:ImageTlsDirectory,/// Raw data of the TLS.pubraw_data:Option<&'a[u8]>,/// TLS index.pubslot:Option<u32>,/// Raw data of TLS callbacks elements without null-terminator element.pubcallbacks_data:&'a[u8],}pubstructTlsCallbacksIterator<'a>{pubis_x64:bool,pubdata:&'a[u8],}implIteratorforTlsCallbacksIterator<'_>{}impl<'a>TlsData<'a>{/// Returns iterator for [`ImageTlsDirectory::address_of_callbacks`]pubfncallbacks(&self) -> TlsCallbacksIterator<'a>{TlsCallbacksIterator{data:&self.callbacks_data}}}
// Parse the callbacks if anyif itd.address_of_callbacks != 0{if(itd.address_of_callbacksasusize) < image_base {returnErr(error::Error::Malformed(format!("tls address_of_callbacks ({:#x}) is less than image base ({:#x})",
itd.address_of_callbacks, image_base
)));}// VA to RVAlet rva = itd.address_of_callbacksasusize - image_base;let offset =
utils::find_offset(rva, sections, file_alignment, opts).ok_or_else(|| {
error::Error::Malformed(format!("cannot map tls address_of_callbacks rva ({:#x}) into offset",
rva
))})?;let num_callbacks = bytes[offset..].chunks(if is_64 {
core::mem::size_of::<u64>()}else{
core::mem::size_of::<u32>()})// Find null-terminator.take_while(|chunk| {if is_64 {
chunk.pread_with::<u64>(0, scroll::LE)}else{
chunk.pread_with::<u32>(0, scroll::LE).map(|v| v asu64)}.map(|x| x != 0).unwrap_or(false)})// Read callback entry from the byte slice.map(|chunk| {if is_64 {
chunk.pread_with::<u64>(0, scroll::LE)}else{
chunk.pread_with::<u32>(0, scroll::LE).map(|v| v asu64)}.map_err(|e| e.into())})// Maps malformed callback if any.map(|x| {
x.and_then(|callback| {if callback == 0{returnOk(callback);}if callback < image_base asu64{returnErr(error::Error::Malformed(format!("tls callback ({:#x}) is less than image base ({:#x})",
callback, image_base
)));}if utils::find_offset(
callback.wrapping_sub(image_base asu64)asusize,
sections,
file_alignment,
opts,).is_none(){returnErr(error::Error::Malformed(format!("cannot map tls callback ({:#x})",
callback
)));}Ok(callback)})}).collect::<Result<Vec<_>,_>>()?
.len();let callbacks_size = if is_64 {
core::mem::size_of::<u64>()}else{
core::mem::size_of::<u32>()};let callbacks_data = &bytes[offset..offset + num_callbacks * callbacks_size];}
The text was updated successfully, but these errors were encountered:
kkent030315
changed the title
Make TLS callbacks iterator instead of Vec<T>
PE: Make TLS callbacks iterator instead of Vec<T>Oct 29, 2024
goblin/src/pe/tls.rs
Line 44 in d096260
I'd like to see this in next breaking change rollup if the
callbacks
field should be something likeTLSCallbackIterator
rather than aVec<T>
which requires alloc, so we can makeTlsData
deriveCopy
,Clone
,Pread
andPwrite
.The size of entries can only known at parsing e.g., as following; so it still can raise errors for malformed callback entry when
PE::parse*
, not when the iterator is called.Optionally, we can make callbacks virtual address validation (
if utils::find_offset(callback.wrapping_sub...
) configurable inopts
likeopts.validate_tls_callbacks_va
so validation won't occur when consumers does not want it w.r.t. intentionally malformed binaries.The text was updated successfully, but these errors were encountered: