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
xproto::GetPropertyReply::value() is unsound #95
Comments
While the Rust standard library does not yet provide the necessary traits for marking values that can be safely created from arbitrary byte sequences, this functionality is provided by a crate: https://github.com/Lokathor/bytemuck Anything that implements |
In the new /// Trait for element in a property list
///
/// In events (e.g. `GetProperty::value`), it allows to assert that the format
/// correspond to the type cast and therefore to do the cast safely at runtime.
///
/// In request (e.g. `ChangeProperty::data`), it allows to infer the format value
/// from the type of passed data.
pub trait PropEl {
const FORMAT: u8;
}
impl PropEl for u8 {
const FORMAT: u8 = 8;
}
impl PropEl for u16 {
const FORMAT: u8 = 16;
}
impl PropEl for u32 {
const FORMAT: u8 = 32;
}
impl PropEl for Atom {
const FORMAT: u8 = 32;
}
impl GetPropertyReply {
pub fn value<P: PropEl>(&self) -> &[P] {
assert_eq!(
self.format(),
P::FORMAT,
"mismatched format of xproto::GetPropertyReply::value"
);
unsafe {
let offset = 32usize;
let len = ((self.value_len() as usize) * ((self.format() as usize) / 8usize));
let len = len / std::mem::size_of::<P>();
let ptr = self.wire_ptr().add(offset) as *const P;
std::slice::from_raw_parts(ptr, len)
}
}
} |
v1.0.0 is released. |
Thank you for all the fixes and the v1.0 release! 🎉 I've opened a PR to mark all the soundness issues tracked by RustSec as fixed: rustsec/advisory-db#1206 |
The below is pure unsoundness. The caller can specify any type they like. How about
bool
or an enum? Or something with a pointer likeVec
orString
or&[u8]
for extra memory-fun? Edit: To explain this some more: Abool
is either 0 or 1. If a variable of typebool
contains any other value, that's undefined behaviour aka "that must not happen".The text was updated successfully, but these errors were encountered: