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

serde_fressian::wasm::from_ptr should be marked unsafe #1

Open
ammaraskar opened this issue Feb 24, 2021 · 0 comments
Open

serde_fressian::wasm::from_ptr should be marked unsafe #1

ammaraskar opened this issue Feb 24, 2021 · 0 comments

Comments

@ammaraskar
Copy link

Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that the serde_fressian::wasm::from_ptr function dereferences a raw pointer provided by the user to deserialize:

let bytes: &[u8] = unsafe {
std::slice::from_raw_parts(ptr, len)
};
let mut deserializer = de::Deserializer::from_bytes(bytes);
T::deserialize(&mut deserializer)

This method should probably be marked unsafe so that the user upholds the documented invariant of passing in valid pointers with ownership. Otherwise, this allows a user to cause a memory safety bug using entirely safe Rust code such as the following:

#![forbid(unsafe_code)]

use serde_fressian::wasm::from_ptr;

fn return_raw_pointer() -> *mut u8 {
    let mut array: [u8; 4] = [0x41, 0x42, 0x43, 0x44];
    array.as_mut_ptr()
}

fn main() {
    let raw_ptr = return_raw_pointer();
    let deserialized : i32 = from_ptr(raw_ptr, 4).unwrap();

    println!("{:x}", deserialized);
    assert!(deserialized == 0x41424344)
}

This outputs:

fffff142
thread 'main' panicked at 'assertion failed: deserialized == 0x41424344', src/main.rs:32:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Return code 101
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