3 ms·
Okay, fair point, sort of. Rust does not have a built-in feature to zero data. Rust does automatically drop references to data on the heap. Zeroing data is fair
by leoh 10mo ago
Okay, fair point, sort of. Rust does not have a built-in feature to zero data. Rust does automatically drop references to data on the heap. Zeroing data is fairly trivial, whereas in go, the issue is non-trivial (afaiu).
use std::ptr;
struct SecretData {
data: Vec<u8>,
}
impl Drop for SecretData {
fn drop(&mut self) {
// Zero out the data
unsafe {
ptr::write_bytes(self.data.as_mut_ptr(), 0, self.data.len());
}
}
}
- steveklabnik 10mo agoZeroing memory is trickier than that, if you want to do it in Rust you should use https://crates.io/crates/zeroize https://crates.io/crates/zeroize
- IshKebab 10mo agoHe was pretty close tbf - you just need to use `write_volatile` instead of `write_bytes`.
- rurban 10mo agoZeroing data does not protect from sidechannel exfiltration. You really need to mfence it also. The zeroize crate also doesn't help there, it only does protect from wrong compiler dead block elimination.