Skip to content

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

Merged
merged 1 commit into from
Jun 4, 2019

Conversation

tony-iqlusion
Copy link
Member

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.

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.
@tony-iqlusion tony-iqlusion merged commit e943be1 into develop Jun 4, 2019
@tony-iqlusion tony-iqlusion deleted the zeroize/remove-scary-ubf-language branch June 4, 2019 20:33
//! 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.
Copy link

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.

Copy link
Collaborator

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)

Copy link

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?

Copy link
Collaborator

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

  1. non-volatile write
  2. non-volatile read
  3. zeroize (volatile write + compiler fence)
  4. non-volatile accesses (i.e. reads/writes)

Is it guaranteed that 4 will never be reordered before 3?

Copy link

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?

Copy link
Collaborator

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.

@tony-iqlusion tony-iqlusion mentioned this pull request Jun 4, 2019
tarcieri pushed a commit that referenced this pull request Jun 4, 2019
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.
snev68 added a commit to snev68/iqlusioninc-crates that referenced this pull request Aug 5, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants