Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

panic safety issue in impls of ElementwiseMul trait on arrays #1

Open
JOE1994 opened this issue Jan 2, 2021 · 1 comment
Open

panic safety issue in impls of ElementwiseMul trait on arrays #1

JOE1994 opened this issue Jan 2, 2021 · 1 comment

Comments

@JOE1994
Copy link

JOE1994 commented Jan 2, 2021

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

impl<$it: Copy + $ot<Output = $it>> $nt<$it> for [$it; $length] {
    fn $nm(&self, other: &$it) -> [$it; $length] {
        let mut items: [$it; $length] = unsafe { mem::uninitialized() };

        for i in 0..$length {
            unsafe {
                ptr::write(&mut items[i], self[i].$om(*other));
            }
        }

        items
    }
}

In https://docs.rs/elementwise/0.3.2/src/elementwise/macros/array.rs.html ,
std::mem::uninitialized is used in the macros.
core::ops::Mul is a public trait that can be implmented on custom types,
and users can provide Mul implementations that can potentially panic.

If a panic happens, the partially uninitialized items will be dropped,
and dropping uninitialized memory will cause undefined behavior.

Thank you for checking out this issue 👍

@JOE1994
Copy link
Author

JOE1994 commented Jan 4, 2021

Following the docs I tried writing a fix using MaybeUninit<T>,
but I'm currently stuck with a known issue with mem::transmute.
The Rust team is currently working on project-safe-transmute to solve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant