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
Hello 🦀 ,
we (Rust group @sslab-gatech) found a memory-safety/soundness issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.
// Writing all bytes through a pointer with less strict alignment when our type has no
// invalid bitpatterns is safe.
x.read_exact(slice::from_raw_parts_mut(
result.as_mut_ptr()as*mutu8,
words *4,
))?;
result.set_len(words);
}
gfx_auxil::read_spirv() method creates an uninitialized buffer (result) and passes it to user-provided Read implementation. This is unsound, because it allows safe Rust code to exhibit an undefined behavior (read from uninitialized memory).
This part from the Read trait documentation explains the issue:
It is your responsibility to make sure that buf is initialized before calling read. Calling read with an uninitialized buf (of the kind one obtains via MaybeUninit<T>) is not safe, and can lead to undefined behavior.
Suggested Fix
It is safe to zero-initialize the newly allocated buffer before read_exact(), in order to prevent user-provided code from accessing old contents of the newly allocated heap memory.
Also, there are two nightly features for handling such cases.
The text was updated successfully, but these errors were encountered:
JOE1994
changed the title
Reading into uninitialized buffer may cause UB ( gfx_auxil::read_spirv() )
Reading on uninitialized buffer may cause UB ( gfx_auxil::read_spirv() )
Jan 8, 2021
I initially tried a naive fix of changing result: Vec<i32> to a zero initialized vector (vec![0_u32; words]),
which turned out to deteriorate performance :(
read_spirv time: [2.0334 us 2.0352 us 2.0372 us]
change: [+214.74% +215.54% +216.27%] (p = 0.00 < 0.05)
Performance has regressed.
Found 8 outliers among 100 measurements (8.00%)
4 (4.00%) high mild
4 (4.00%) high severe
The above results are from running the simple benchmark below.
Hello 🦀 ,
we (Rust group @sslab-gatech) found a memory-safety/soundness issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.
Issue Description
gfx/src/auxil/auxil/src/lib.rs
Lines 72 to 82 in 83a437d
gfx_auxil::read_spirv()
method creates an uninitialized buffer (result
) and passes it to user-providedRead
implementation. This is unsound, because it allows safe Rust code to exhibit an undefined behavior (read from uninitialized memory).This part from the
Read
trait documentation explains the issue:Suggested Fix
It is safe to zero-initialize the newly allocated buffer before
read_exact()
, in order to prevent user-provided code from accessing old contents of the newly allocated heap memory.Also, there are two nightly features for handling such cases.
Thank you for checking out this issue 👍
The text was updated successfully, but these errors were encountered: