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
Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that in the read_bytes_default_le impl for [T; $x]
Memory is created to hold the unpacked type T using std::mem::uninitialized(); and then <T>::read_bytes_default_le is called, which in the case of custom types could potentially panic. This means that the uninitialized memory would be dropped as if it were an instance of T, causing soundness problems. See this example:
#![forbid(unsafe_code)]use byte_struct::*;// Custom type that panics when reading bytes.structCustomByteStruct(u8);implByteStructLenforCustomByteStruct{constBYTE_LEN:usize = 1;}implByteStructforCustomByteStruct{fnwrite_bytes(&self,bytes:&mut[u8]){}fnread_bytes(bytes:&[u8]) -> Self{panic!("Panic when reading")}}implDropforCustomByteStruct{fndrop(&mutself){println!("Dropping {}", self.0)}}// Wrapper around the type above so we can use the// `ByteStructUnspecifiedByteOrder for [T; $x]` impl.#[derive(ByteStruct)]#[byte_struct_le]structArrayOfCustomByteStruct{custom_structs:[CustomByteStruct;2]}fnmain(){let bytes = [0x01,0x02];let deserialized = ArrayOfCustomByteStruct::read_bytes(&bytes[..]);}
It outputs:
thread 'main' panicked at 'Panic when reading', src/main.rs:31:43
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Dropping 166
Dropping 179
Return code 101
Notice that the drop is printing out uninitialized memory.
This code should probably use MaybeUninit to avoid this problem.
The text was updated successfully, but these errors were encountered:
Thanks for the report! There was an attempt of rewriting this part with MaybeUninit in the past, but it didn't quite work out as transmute couldn't prove the size equality that involves associated constant for types involving generics, or something like that. I will try that again today and see if I can resolve it
Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that in the
read_bytes_default_le
impl for[T; $x]
byte-struct-rs/byte_struct/src/lib.rs
Lines 410 to 422 in 9c41996
Memory is created to hold the unpacked type
T
usingstd::mem::uninitialized();
and then<T>::read_bytes_default_le
is called, which in the case of custom types could potentially panic. This means that the uninitialized memory would be dropped as if it were an instance ofT
, causing soundness problems. See this example:It outputs:
Notice that the drop is printing out uninitialized memory.
This code should probably use
MaybeUninit
to avoid this problem.The text was updated successfully, but these errors were encountered: