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

Unsound implementation of video::AssetPtr::<T>::get #143

Open
shinmao opened this issue Oct 2, 2023 · 0 comments
Open

Unsound implementation of video::AssetPtr::<T>::get #143

shinmao opened this issue Oct 2, 2023 · 0 comments

Comments

@shinmao
Copy link

shinmao commented Oct 2, 2023

Hi, I am the security research from SunLab. We are running our tools on open-source repositories and found the following function could be unsound.

The source of unsoundness

rust-sciter/src/video.rs

Lines 476 to 479 in 789013a

fn get(&mut self) -> &mut iasset {
let ptr = self.ptr as *mut iasset;
unsafe { &mut *ptr }
}

Here, the raw pointer to T could be cast to the pointer of iasset.

pub struct AssetPtr<T> {
	ptr: *mut T,
}

Therefore, the attacker just needs to specify T aligned to lower bytes than iasset. Then, the call to adopt will trigger get with undefined behavior.

To reproduce the bug

use sciter::video::AssetPtr;

fn main() {
    let mut a: u8 = 1;
    let _ = AssetPtr::adopt(&mut a as *mut u8);
}

run with miri,

error: Undefined Behavior: dereferencing pointer failed: alloc820 has size 1, so pointer to 8 bytes starting at offset 0 is out-of-bounds
   --> /home/rafael/.cargo/registry/src/index.crates.io-6f17d22bba15001f/sciter-rs-0.5.58/src/video.rs:478:12
    |
478 |         unsafe { &mut *ptr }
    |                  ^^^^^^^^^ dereferencing pointer failed: alloc820 has size 1, so pointer to 8 bytes starting at offset 0 is out-of-bounds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant