rust-keepass icon indicating copy to clipboard operation
rust-keepass copied to clipboard

Clear memory with volatile intrinsic

Open kornelski opened this issue 11 years ago • 4 comments

AFAIK the optimizer is free to optimize out memory writes if it finds that the memory isn't read afterwards.

This can potentially affect secure string deletion:

https://github.com/raymontag/rust-keepass/blob/2b7b701b6954160fa86d1e3dddf0154654ac510b/src/sec_str/mod.rs#L62,L63

There's an (unstable) intrinsic that guarantees the write won't be optimized out: https://doc.rust-lang.org/core/intrinsics/fn.volatile_set_memory.html

kornelski avatar Oct 05 '15 12:10 kornelski

Thanks for the hint, I will look at it.

raymontag avatar Oct 07 '15 08:10 raymontag

After researching a little bit it seems you're right, one has to use the volatile-functions for this. However it is unprobable that these become stable. But I'm verifying this ATM in the Rust-forum. libsodium would have a function for this, however linking to it only for this particular usecase sucks. It seems I have to write my own function for this via ffi...

raymontag avatar Oct 07 '15 11:10 raymontag

We could also look at the assembler that gets generated. It's a bit tricky with rust, but its doable.

jan-schreib avatar Oct 08 '15 09:10 jan-schreib

I've used volatile_set_memory in 3c20a8a6801ce56e2186dd122280f1c97c128e49, so it's fixed now. We will make a code review in the following days to see if there are more such caveats.

raymontag avatar Oct 08 '15 09:10 raymontag