-
Notifications
You must be signed in to change notification settings - Fork 84
zeroize: zeroize entire capacity of Vec
#341
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
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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.
I think there's a simpler option than casting to a
*mut u8
, which is usingmem::zeroed
to produce the all-zero byte pattern forZ
, andptr::write_volatile
to write it, e.g.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 considered that, but it would result in undefined behavior, when
Z
is not a type where an all-zero pattern is a valid value. However now that I think about it again, it wouldn't make much sense to implementZeroize
for such a type. I'll adjust that.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.
Actually on second thought, if it were implemented that way, it would be possible to invoke undefined behavior from safe code. For example this would then result in undefined behavior:
This may not be a very useful implementation of
Zeroize
, but it would be a way to invoke undefined behavior in safe code. The other version does not have this problem.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.
How would the uninitialized memory be exposed to safe code?
Regardless, the net effect is the same: you are writing zeroed bytes to the excess capacity.
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.
It doesn't need to. According to the doc of
mem::zeroed
:Which links to a section in the docs about undefined behavior:
The same page also states that
The example above would therefore be both unsound and incorrect, which is possible from safe code. The values do not need to be accessible by safe code to create the problem here.
With the method which zeroes the memory using
u8
this would not be the case, asu8
is guaranteed to be able to hold a value of0
.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.
The implementation, as currently written in this PR.
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.
It still seems like 6 of one, half dozen of another to me, except looping a byte-at-a-time and doing pointer arithmetic around
core::mem::zeroed::<NonNull<T>>()
is more complex and potentially slower.It seems the crux of this is...
...but we're talking about a buffer which is defined as being:
The contract of
Vec
is always to initialize this capacity in some way before reading from it.It seems the claim is it might be considered initialized by the Rust compiler when it is uninitialized, but before its API permits any reads, it will be initialized again.
Can either of you give a concrete example of a tractable problem which is possible given the contract of
Vec
to always (re)initialize this memory which would not occur with pointer-based zeroing?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.
What you are doing to the excess buffer of the vector is and never was the problem. You are indeed free to do with the extra capacity of bytes whatever you want. But not however you want. The code that writes invalid or uninitialized values into it must still be UB-free and executing
mem::zeroed::<T>
is 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.
The problem is not the
Vec
or its memory, the problem is simply that the code could callmem::zeroed<NonNull<()>>
, which is UB. The compiler might make optimizations that would crash the program if such a value ever exists. This would be the same problem in every program, even without aVec
being involved.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.
Okay, I think I get it now. Thank you for the explanation.