You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
RegValue.bytes is a Rust Vec<u8>. This implementation assumes that the vec pointer is sufficiently aligned for u32/u64. This will usually be true (e.g. the Windows System allocator I believe always sufficiently aligns), but this is not guaranteed; an alternative #[global_allocator] could provide allocations at odd addresses. x86 bytecode doesn't care about alignment, but Rust/LLVM does, and an unaligned access is UB even if it works fine on the target.
Significantly more problematic, as RegValue's fields are public, it is possible to provide an empty vector to this function, which will happily try to read from a fully invalid pointer.
let val = RegValue{bytes:vec![],vtype:REG_QWORD,};
u64::from_reg_value(&val);// 💣💥‼️
A correct and maximally performant implementation can be written in pure safe code fairly easily:
"Fun" is any downstream which has copied the unsound pointer access code. Which I believe includes std 🙃
clippy::cast_ptr_alignment may be an aggressive lint, but it's often correct that your code is incorrect unless you are the one to have manually allocated that pointer.
winreg-rs/src/types.rs
Lines 110 to 128 in fc6521e
RegValue.bytes
is a RustVec<u8>
. This implementation assumes that the vec pointer is sufficiently aligned foru32
/u64
. This will usually be true (e.g. the WindowsSystem
allocator I believe always sufficiently aligns), but this is not guaranteed; an alternative#[global_allocator]
could provide allocations at odd addresses. x86 bytecode doesn't care about alignment, but Rust/LLVM does, and an unaligned access is UB even if it works fine on the target.Significantly more problematic, as
RegValue
's fields are public, it is possible to provide an empty vector to this function, which will happily try to read from a fully invalid pointer.A correct and maximally performant implementation can be written in pure safe code fairly easily:
Any cost imposed by the bounds check and unaligned access is trivially dominated by the cost of reading from the registry.
The text was updated successfully, but these errors were encountered: