-
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
Merged
+35
−118
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Interesting. I can remove the CPU fences and update this section accordingly.
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
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.