-
Notifications
You must be signed in to change notification settings - Fork 85
zeroize: Remove scary language about undefined behavior #214
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
Conversation
The documentation previously included a lengthy essay about potential caveats in what it can guarantee as mixed volatile/non-volatile writes were previously assumed to be undefined behavior due to language to that effect in the official documentation for `write_volatile`. That language has subsequently been removed after discussion about the safety implications: rust-lang/rust#60972 Though generally it's not a good idea to mix volatile and non-volatile writes when using volatile accesses to communicate with e.g. memory mapped external devices, for the particular usage pattern in this crate, i.e. writing a zero-value, it can be considered well-defined.
//! leverages the [core::sync::atomic] memory fence functions including | ||
//! [compiler_fence] and [fence] (which uses the CPU's native fence | ||
//! instructions). These fences are leveraged with the strictest ordering | ||
//! guarantees, [Ordering::SeqCst], which ensures no accesses are reordered. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK atomic fences only really prevent reordering of atomic accesses. Non-atomic accesses can still be reordered. The interaction of this with volatile accesses is entirely unclear -- technically, they are non-atomic, but in practice it seems unlikely that the compiler would perform such reorderings.
But all this means is that zeroing is a write access, and as usual, if a write access happens concurrently with another read or write access, that is UB. If there is no such concurrent access, there should be no problem. But then also the fences should not be needed. A compiler fence seems like a reasonable precaution and also helps make sure the side-effects happens near where the programmer might expect it, but a CPU fence to me sounds more like cargo cult.
AFAIK, the main thing you are worried about here is the compiler removing the writes because "nobody sees them" before the memory gets deallocated? Volatile should entirely take care of that, because those are exactly writes that the compiler must assume "someone" can see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but a CPU fence to me sounds more like cargo cult.
Interesting. I can remove the CPU fences and update this section accordingly.
AFAIK, the main thing you are worried about here is the compiler removing the writes because "nobody sees them" before the memory gets deallocated?
Though this is the main intended usage pattern, it need not be the only one. Another would be reuse of a buffer which may contain secrets (e.g. decrypted plaintexts). Some examples of such usage going awry are Heartbleed (which is also general memory unsafety) and JetLeak (an example of what can go wrong when shared buffers are reused in a memory-safe way)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, but for a "normal reuse" one could use normal writes just as well, right? Compilers will preserve those just fine. Or are there cases where they did not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I'm just trying to nail down the details here. The scenario would look something like this
- non-volatile write
- non-volatile read
- zeroize (volatile write + compiler fence)
- non-volatile accesses (i.e. reads/writes)
Is it guaranteed that 4 will never be reordered before 3?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is access for any kind of memory access. Reordering would be observable in a single-threaded program!
AFAIK the "big enemy" is zero-ing is deallocation. Compilers are allowed to assume that after deallocation, the content of that memory is never observed again. So in *x = 0; free(x)
, the write may be removed. This is write you need to do volatile writes. Are you aware of any other issues?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To my knowledge volatile writes will prevent pre-drop zeroization from being elided, so really all my concerns stem from the previously "off label" mixture of volatile and non-volatile accesses. So long as the ordering I mentioned above is guaranteed, I don't foresee any other issues.
Per @RalfJung: #214 (comment) > AFAIK atomic fences only really prevent reordering of atomic accesses. > Non-atomic accesses can still be reordered. The interaction of this > with volatile accesses is entirely unclear -- technically, they are > non-atomic, but in practice it seems unlikely that the compiler would > perform such reorderings. > [...] > A compiler fence seems like a reasonable precaution and also helps > make sure the side-effects happens near where the programmer might > expect it, but a CPU fence to me sounds more like cargo cult.
Per @RalfJung: iqlusioninc/crates#214 (comment) > AFAIK atomic fences only really prevent reordering of atomic accesses. > Non-atomic accesses can still be reordered. The interaction of this > with volatile accesses is entirely unclear -- technically, they are > non-atomic, but in practice it seems unlikely that the compiler would > perform such reorderings. > [...] > A compiler fence seems like a reasonable precaution and also helps > make sure the side-effects happens near where the programmer might > expect it, but a CPU fence to me sounds more like cargo cult.
The documentation previously included a lengthy essay about potential caveats in what it can guarantee as mixed volatile/non-volatile writes were previously assumed to be undefined behavior due to language to that effect in the official documentation for
write_volatile
.That language has subsequently been removed after discussion about the safety implications:
rust-lang/rust#60972
Though generally it's not a good idea to mix volatile and non-volatile writes when using volatile accesses to communicate with e.g. memory mapped external devices, for the particular usage pattern in this crate, i.e. writing a zero-value, it can be considered well-defined.