-
Notifications
You must be signed in to change notification settings - Fork 1.6k
RFC: Never allow reads from uninitialized memory in safe Rust #837
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
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d7f8272
RFC: Never allow reads from uninitialized memory in safe Rust
aturon b2c7cb1
Clarifications
aturon 22c5996
Clarifications
aturon adf97bd
Clarifications
aturon d419cee
Clarifications
aturon 229bca6
Clarify practical consequences
aturon a066ed5
Clarify applicability
aturon 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
- Feature Name: (fill me in with a unique ident, my_awesome_feature) | ||
- Start Date: 2015-02-13 | ||
- RFC PR: (leave this empty) | ||
- Rust Issue: (leave this empty) | ||
|
||
# Summary | ||
|
||
Set an explicit policy that uninitialized memory can never be exposed | ||
in safe Rust, even when it would not lead to undefined behavior. | ||
|
||
# Motivation | ||
|
||
Exactly what is guaranteed by safe Rust code is not entirely | ||
clear. There are some clear baseline guarantees: data-race freedom, | ||
memory safety, type safety. But what about cases like reading from an | ||
uninitialized, but allocated slice of scalars? These cases can be made | ||
memory and typesafe, but they carry security risks. | ||
|
||
In particular, it may be possible to exploit a bug in safe Rust code | ||
that causes that code to reveal the contents of memory. | ||
|
||
Consider the `std::io::Read` trait: | ||
|
||
```rust | ||
pub trait Read { | ||
fn read(&mut self, buf: &mut [u8]) -> Result<usize>; | ||
|
||
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<()> { ... } | ||
} | ||
``` | ||
|
||
The `read_to_end` convenience function will extend the given vector's capacity, | ||
then pass the resulting (allocated but uninitialized) memory to the | ||
underlying `read` method. | ||
|
||
While the `read` method may be implemented in pure safe code, it is | ||
nonetheless given read access to uninitialized memory. The | ||
implementation of `read_to_end` guarantees that no UB will arise as a | ||
result. But nevertheless, an incorrect implementation of `read` -- for | ||
example, one that returned an incorrect number of bytes read -- could | ||
result in that memory being exposed (and then potentially sent over | ||
the wire). | ||
|
||
# Detailed design | ||
|
||
While we do not have a formal spec/contract for unsafe code, this RFC | ||
will serve to set an explicit policy that: | ||
|
||
**Uninitialized memory can never be exposed in safe Rust, even when it | ||
would not lead to undefined behavior**. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume that you mean "can never be exposed". 😀 |
||
|
||
Like other aspects of the definition of "safe Rust", this is part of | ||
the contract that *all* unsafe code must abide by, whether part of | ||
`std` or external libraries or applications. | ||
|
||
In practical terms, this will require methods like `read_to_end` to | ||
internally zero out (or otherwise ensure initialization of) memory | ||
before they pass it to unknown safe code like the `read` method. | ||
|
||
# Drawbacks | ||
|
||
In some cases, this policy may incur a performance overhead due to | ||
having to initialize memory that will just be overwritten | ||
later. However, these situations would be better served by improved | ||
implementation techniques and/or introducing something like a `&out` | ||
pointer expressing this idiom. | ||
|
||
In addition, in most cases `unsafe` variants of APIs can always be | ||
provided for maximal performance. | ||
|
||
# Alternatives | ||
|
||
The main alternative is to limit safety in Rust to e.g. having defined | ||
behavior (which generally entails memory and type safety and data-race | ||
freedom). While this is a good baseline, it seems worthwhile to aspire | ||
to greater guarantees where they come at relatively low cost. | ||
|
||
# Unresolved questions | ||
|
||
Are there APIs in `std` besides the convenience functions in IO that | ||
this policy would affect? |
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 noticed the one below first, but this also should say "can never".