-
Notifications
You must be signed in to change notification settings - Fork 47
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
Reliable Zeroing Requires Pinning #11
Comments
The zeroing part in general looks weird to me, justification-wise. I think the problem is more general than just zeroing structs on Take, for instance, the following made up example:FFI-wise, there was no need to zero rust's stack struct
|
If the struct that you wanna zero out implements either Putting your struct into a I remember years ago (my memory still not zeroed out completely ;), we had an Placement trait RFC. This would have solved this problem to some degree. The safest choice is to simply stick with a You can verify yourself by running this program: struct S(usize);
impl S {
fn new() -> Self {
Self(42)
}
}
impl Drop for S {
fn drop(&mut self) {
println!("drop");
unsafe{ ::std::ptr::write_volatile(&mut self.0, 0) };
}
}
struct Pointers {
a_ptr: *const S,
b_ptr: *const S,
}
fn test() -> Pointers {
let a = S::new();
let a_ptr: *const S = &a;
println!("&a = {:p}", a_ptr);
let b = a; // moved here
let b_ptr: *const S = &b;
println!("&b = {:p}", b_ptr);
println!("*a = {}", unsafe { (*a_ptr).0 });
println!("*b = {}", unsafe { (*b_ptr).0 });
Pointers { a_ptr, b_ptr }
}
fn main() {
let Pointers { a_ptr, b_ptr } = test();
let a = unsafe { (*a_ptr).0 };
let b = unsafe { (*b_ptr).0 };
println!("*a = {}", a);
println!("*b = {}", b);
} On my machine (FreeBSD), this outputs:
Look at the last two lines, |
It's not enough to zero memory with a
Drop
, it's also required that the type contains aPinned
marker, which would forbid the type from being moved. Types which move do not executeDrop
on the value that was moved.It would also be a good idea to contribute to Redox OS's ralloc, which supports a security flag that zeroes all frees.
The text was updated successfully, but these errors were encountered: