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 usages of unsafe implementation from PyObject to T #298

Open
llooFlashooll opened this issue Aug 20, 2024 · 1 comment
Open

Unsound usages of unsafe implementation from PyObject to T #298

llooFlashooll opened this issue Aug 20, 2024 · 1 comment

Comments

@llooFlashooll
Copy link

Hi, I am scanning the cpython in the latest version with my own static analyzer tool.

Unsafe conversion found at: src/py_class/mod.rs:96:15: 96:62.

#[inline]
#[doc(hidden)]
pub unsafe fn data_init<'a, T>(_py: Python<'a>, obj: &'a PyObject, offset: usize, value: T)
where
    T: Send + 'static,
{
    let ptr = (obj.as_ptr() as *mut u8).add(offset) as *mut T;
    ptr::write(ptr, value)
}

This unsound implementation would create a misalignment issues if the type size of PyObject is smaller than the type size of T.

This would potentially cause undefined behaviors in Rust. If we further manipulate the problematic converted types, it would potentially lead to different consequences such as access out-of-bound. I am reporting this issue for your attention.

@dgrunwald
Copy link
Owner

It's not unsound, it's just an unsafe function with an undocumented precondition:
The caller is supposed to pass an allocation that contains an ffi::PyObject followed by a T (with offset being sizeof(PyObject) plus maybe a bit more if required for alignment).

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

2 participants