Skip to content

Unsound usages of unsafe implementation about c_void #28

Description

@llooFlashooll

Hi, I am scanning this crate in the latest version using my own static analyzer tool.

Unsafe pointer conversion is found at:

pub fn create_with_value(name: &str, value: &T) -> Result<Self, CreateError> {
        let name_c = CString::new(name)?;

        let existing = unsafe { XPLMFindDataRef(name_c.as_ptr()) };
        if !existing.is_null() {
            return Err(CreateError::Exists);
        }

        let value = value.to_storage();
        let mut value_box = Box::new(value);
        let value_ptr: *mut T::Storage = value_box.as_mut();

        let id = unsafe {
            XPLMRegisterDataAccessor(
                name_c.as_ptr(),
                T::sim_type(),
                Self::writeable(),
                Self::int_read(),
                Self::int_write(),
                Self::float_read(),
                Self::float_write(),
                Self::double_read(),
                Self::double_write(),
                Self::int_array_read(),
                Self::int_array_write(),
                Self::float_array_read(),
                Self::float_array_write(),
                Self::byte_array_read(),
                Self::byte_array_write(),
                value_ptr as *mut c_void,
                value_ptr as *mut c_void,
            )
        };

        assert!(!id.is_null());
        Ok(OwnedData {
            id,
            value: value_box,
            access_phantom: PhantomData,
        })
    }

This unsound implementation would create memory issues such as overflow, underflow, or misalignment. The attacker can manipulate the argument value associated with the c_void pointer with an unexpected type or layout, which can lead to an out-of-bounds memory access bug. The c_void pointer is passed through the FFI (XPLMRegisterDataAccessor) as both read and write references, which can further corrupt the C/C++ code.

This would cause undefined behaviors in Rust. Adversaries can manipulate the associated argument to cause memory safety bugs. I am reporting this issue for your attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions