-
Notifications
You must be signed in to change notification settings - Fork 14
Open
Description
Hi, thanks for your great contribution to this easy-to-use framework first.
We have found an unsound problem caused by the unsafe call std::slice::from_raw_parts
in core::utils::buffer_as_slice
and core::utils::buffer_as_slice_mut
These functions are very powerful and useful to convert the type of slice but are rather unsafe as well.
- The pointer calculation with
byte_offset
could lead to alignment problem, which is an undefined behavior. - The
from_raw_parts
could create an out-of-bound slice that allows the user to access or even change the memory illegally.
POC
Here follows a simple POC written in safe Rust code.
use workflow_core::utils::buffer_as_slice_mut;
fn main() {
let mut buffer: [u8; 5] = [1, 2, 3, 4, 5];
let slice: &mut [u32] = buffer_as_slice_mut(&mut buffer, 1, 10);
slice[3] = 1024;
println!("{:?}", slice);
}
Suggestion
There are two possible action choices could be taken:
- Mark the function
buffer_as_slice
andbuffer_as_slice_mut
as unsafe and write Safety requirement. - (recommended) Mark the function
buffer_as_slice
andbuffer_as_slice_mut
aspub(crate)
since they are just util functions. - (recommended) Add assert to guarantee the
byte_offset
is the multiple ofsize_of(T)
andelements
is in-bound.
Metadata
Metadata
Assignees
Labels
No labels