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

Unsoundness of gaussian_blur #256

Open
shinmao opened this issue Sep 4, 2024 · 0 comments
Open

Unsoundness of gaussian_blur #256

shinmao opened this issue Sep 4, 2024 · 0 comments

Comments

@shinmao
Copy link

shinmao commented Sep 4, 2024

I consider the gaussian_blur have the unsound implementation in line 32

silicon/src/blur.rs

Lines 23 to 33 in 7837ec8

pub fn gaussian_blur(image: RgbaImage, sigma: f32) -> RgbaImage {
let (width, height) = image.dimensions();
let mut raw = image.into_raw();
let len = raw.len();
// fastblur::gaussian_blur only accepts Vec<[u8; 4]>
unsafe {
raw.set_len(len / 4);
let ptr = &mut *(&mut raw as *mut Vec<u8> as *mut Vec<[u8; 4]>);
gaussian_blur_impl(ptr, width as usize, height as usize, sigma);

Since RgbaImage is a struct with repr(Rust), it means that it has an unstable layout and compiler preserves the right to insert any padding bytes around the fields.
When you cast the raw pointer of it to the pointer of Vec<u8>, it could lead to incorrect data interpretation and uninitialized memory exposure since Vec<u8> expects there are continuous bytes.

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