Skip to content

Unsound problem in workflow_core::utils::buffer_as_slice #11

@safe4u

Description

@safe4u

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.

  1. The pointer calculation with byte_offset could lead to alignment problem, which is an undefined behavior.
  2. 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:

  1. Mark the function buffer_as_slice and buffer_as_slice_mut as unsafe and write Safety requirement.
  2. (recommended) Mark the function buffer_as_slice and buffer_as_slice_mut as pub(crate) since they are just util functions.
  3. (recommended) Add assert to guarantee the byte_offset is the multiple of size_of(T) and elements is in-bound.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions