-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Read
on uninitialized buffer may cause UB ('uu_od' crate)
#1729
Comments
Is this actually an issue when the container’s value type is Of course, if someone has a way to fix this without wastefully initializing the array, I’d be very happy to do so. |
I just took a solid look at the function, and I think it could actually be fixed pretty easily to avoid any allocations at all without using |
Thank you for your feedback :) It can be an issue even when the container's value type is Here is a link to a document that explains the issue well: |
So, IIUC, in the case of I don’t think these are actually concerns for the utilities here, but I’ll see if I can find a decent solution, as I’d prefer to use |
Data leakage is not the only consequence of using an uninitialized memory. The Rust compiler optimizes the code based on the assumption that the buffer is fully initialized. Thus, an uninitialized buffer can lead to a miscompilation and breaks the Rust's safety guarantee ("safe Rust code never causes memory safety error"). Such examples are provided in Ralf Jung's blog post "What The Hardware Does" is not What Your Program Does: Uninitialized Memory and "But how bad are undefined values really?" section of RFC 2930. |
I don’t see how that matters in this case as all |
In terms of the links you gave, it seems that as long as the reader doesn't do something bizarre such as read from the buffer it is supposed to write to several times or simply not write anything into the buffer (neither of which are issues in our code AFAIK, although I may need to verify the latter in cases where the input is empty), things are alright. So, basically, I suppose the functions/readers that use |
If |
I believe I've removed all the usages of BTW, if you happen to find any other usage of |
This should be resolved now that the linked PRs have been merged. Please let me know if I missed something. |
Hello 🦀,
we (Rust group @sslab-gatech) found a memory-safety/soundness issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.
Issue Description
coreutils/src/uu/od/src/partialreader.rs
Lines 35 to 47 in e2b5805
<partialreader::PartialReader<R> as std::io::Read>::read()
method creates an uninitialized buffer and passes it to user-providedRead
implementation. This is unsound, because it allows safe Rust code to exhibit an undefined behavior (read from uninitialized memory).This part from the
Read
trait documentation explains the issue:How to fix the issue?
The Naive & safe way to fix the issue is to always zero-initialize a buffer before lending it to a user-provided
Read
implementation. Note that this approach will add runtime performance overhead of zero-initializing the buffer.As of Feb 2021, there is not yet an ideal fix that works with no performance overhead. Below are links to relevant discussions & suggestions for the fix.
The text was updated successfully, but these errors were encountered: