Memory Safety Bugs in Rust Standard Library
forge-blog.github.io
forge-blog.github.io
The Rust project as a whole currently has 178 open issues with the tag I-unsound[1]. Many of these describe issues that only exist in the nightly compiler, of course, but it does happen that bugs are not caught before shipping. That's software.
[1]: https://github.com/rust-lang/rust/issues?q=is%3Aissue+state%...
If it were true, every bugfix would be a breaking change. It's clearly not.
What do you call a programming language which claims to be memory-safe that has memory safety issues in it's standard library?
Yes, that's the whole point. Rust is used to write software. Not memory-safe software. Let alone bug-free software.
But somehow that's the extraordinary cargo cult claim that Rust proponents make. Is that claim valid?
That's the point. Isn't it?
And no reasonable people are claiming that rust is used to write 100% bug free software. And yet people like you imagine such people daily apparently. It's certainly not a claim made anywhere upthread of your comment so if it's your claim feel free to take full ownership of it.
Rust unfortunately is renowned for it's toxic community (see actix-web). Within that community, some clueless but vocal members routinely attack people who have the audacity of not rewriting all their projects in Rust, and they parrot cargo cult mantras such as memory safety as their main attack points.
Consequently, discussions on memory issues plaguing Rust often draw these types, with the expected consequences.
Yes, Rust also suffers from memory safety issues. Yes, there are CVEs caused by Rust code. Yes, using Rust obviously does not make your code better or bug-free. But these discussions are not grounded on a technical ground.
The issue with actix-web, which played out in 2018-20, was that it played very liberally with memory safety, prioritizing speed over everything else, with the author closing PRs solving memory bugs.
Basing your definition on "techinical ground" you can dismiss anything that isn't perfect.
Rust's solution is the human response. Memory unsafety are treated as bugs, listed and fixed. Some fast, some more slowly. Depending on impact and how deep they go. C and C++'s solution is listing them in the specification, or having it be implementation defined, and telling you to not step on the land mine.
Given the research on this area introducing Rust in some of the most battle hardened codebases globally leads to far fewer memory issues and high impact vulnerabilities in the Rust parts. And the parts converted to Rust often are the ones which has had an history of bugs and vulnerabilities becuase they are non-trivial.
Thanks to that Rust community behaviour, C23 has several nice improvements, with several others to possibly land on C2y.
I lost track how many features have been on nightly, there is even a website to keep track of them,
If Jean Heyd went and implemented it, I wonder how far we'd be into actually having a decent alternative. Because his implementation did the hacky thing via trait abuse, while this actually changes compiler to allow const-only functions. The trait abuse would have exploded Rust's already long compile times.
Getting the PDF out is the equivalent of landing on stable.
We will never know now, will we?
It seems you are confused and emotionally involved, to the point you're trying to grasp at straws to have something to shout back.
C is specified by an international standard, which then is used as integration target by countless implementations.
Languages such as Rust are different in the way they have no formal specification. Whatever went into the reference implementation is what counts as "the language", bugs and all. At best a specification can be derived from the reference implementation.
The key difference is that a C compiler that doesn't implement a feature and/or deviates from the implementation is considered a buggy implementation, whereas Rust is whatever is currently in rust stable. If something is not on rust stable, it is not rust.
Not really. If you count when practically you can use a feature then it makes sense to wait till all major implementers use it. For example you can use `type_info` feature on nightly right now. Same way you can use some C23 features on some compilers but not others.
It's more of a light hearted jab at how I hear things work in C-land. "Wow, that feature is amazing!", "It's in C11, so it will be widely used by {current_year + 5}" (keep in mind this is an exaggeration for comedic effect).
Truth is the nightly features ship as fast as there is demand for them and depending on roadblocks encountered. E.g. specialization is highly sought after but effectively unsound until some nasty unsound cases are dealt with. Type_info doesn't seem to have these sort of roadblocks.
I mean. I'd sure be reluctant to depend on it until it had trickled out through the ecosystem
No, unfortunately Rust's toxic community tends to flock to these topics to flood them with their blend cargo cult claims. Topics which happen to question and even refute their claims is like their bat signal.
The actix-web episode is a good example of how far these types can take even the most harmless claim, at the expense of everyone.
Meanwhile, anyone who actually works in the field for a living is hardly surprised by this. When this sort of news pops up, we check if we need to start an unplanned update and that's it.
You sound like you're trying to gaslight everyone with this story. The problem with actix-web was not "memory bugs". Not the slightest.
The problem was that it was a high-profile project in Rust, and Rust's toxic community felt entitled to relentlessly harass it's maintainers with vacuous claims regarding the use of what they chose to frame as unidiomatic Rust to justify their campaign of personal attacks.
To the point where they forced the maintainer to outright quit.
We are talking about Rust's toxic community being so toxic it even leads high-profile members of the Rust community to burn out and give up.
This was the problem. Not "bugs". It's the community.
The claim that the problem was "not memory bugs. Not the slightest" is demonstrably false.
The issues were serious enough to receive a RustSec advisory and three separate CVEs covering memory corruption and potential use-after-free.
Their beloved language is most likely decades away from ever having a fully bootstraped reference implementation.
Which even if it ever happens, projects like Linux would most likely never adopt, as that new reference implementation has to provide the same hardware support guarantees as LLVM and GCC.
The mutable aliasing ecosystem is holding more advanced hardware back.
You say "vocal", yet they remain eerily silent in this and every other thread about Rust on HN, as well as /r/rust over on Reddit.
If you didn't know better, you could almost think they were imaginary.
You seem to have a mental picture of a raging Toxic Rust Evangelist consumed by shame and rage over Rust's Dark Secret: It's using the best codegen backend on the planet (with a compatible license), LLVM, which is written in - gasp - C++!! Dun dun dunnnn!
What a dramatic plot twist! I understand how that might feel very engaging.
The boring truth is that everybody is pretty fine with LLVM, because no reasonable (or real) person believes that this is a problem. As long as it generates pretty decent code in a reasonable amount of time for all the important architectures, it's a great choice.
We're trying to get work done.
Why must Rust have "a fully bootstraped reference implementation"?
This is weird, dude.
The main problem I have if I accept Rust as a "better C-like" with "safer memory" still ends up being the safety mechanism makes some code rather unergonomic.
What I would most like to see is a reduction of all `unsafe` throughout. The reduction implies two things: safer, and more ergonomic.
But the goal was to bring verification techniques (type systems, ownership and lifetime tracking, etc) to a practical language that also supported efficient interop with existing C libraries and APIs. Hence the 'unsafe' keyword. At this, Rust has succeeded wildly.
I've been writing Rust for years and almost never need to use 'unsafe'.
The `variable[i]` notation is intended to function like in other languages, where it returns an unwrapped value. It would be ugly, noisy, unergonomic, and annoying to have to properly handle `Some` (like in `variable[i].unwrap()`) every single time you ever used it. Imagine doing `c[i].unwrap()[j].unwrap() += a[i].unwrap()[k].unwrap() * b[k].unwrap()[j].unwrap();`.
If you really want the result to be wrapped in `Some`, you can use `variable.get(i)`.
The ? operator can be nicer to read then all those .unwraps() as an alternative and doesn't panic.
I guess the concern with panics is you're not forced to think about how you want to handle them like you do when you get a `Result` or `Option` returned.
As a result it could be used as a DDOS against your app if it's network accessible.
I would go so far as to say that an out-of-bounds index is always a logic error (to be handled by panicking), not an input error (to be handled with Result). And if the out-of-bounds access was caused by invalid input, then logic error consists of not checking the input before indexing.
The latter is a subjective matter. There are reasons to never want a panic (hello there, Linux kernel) and reasons to do more sophisticated stuff (core dump, cleanup or notifications) before crashing. And maybe one thread shouldn't lead to process death. I think most of these decisions should be kept to the user's discretion.
There are facilities for this.
option.ok_or(MyError::new())?That's what panic::set_hook is for. [1]
> And maybe one thread shouldn't lead to process death.
That's what panic::catch_unwind is for. [2]
[1] https://doc.rust-lang.org/std/panic/fn.set_hook.html
[2] https://doc.rust-lang.org/std/panic/fn.catch_unwind.html
If you are doing this then you've made a mistake in your code. Rust is consistent in catching these early.
Likewise regarding the storage for the length, if one is willing to go the way C went down a few years later, declare a pointer and make the array storage be pointer based, e.g. BASED(MY_ARRAY_PTR).
Alternatively when allocating the array storage define its access to be pointer based.
This means no length descriptor will be stored alongside the array, and the access to array elements will be done via the given pointer.
It is up to the programmer not to mess up then.
Unfortunately C authors never added a way to do the former, given their experience with PL/I and Multics.
I am aware we are finally going into that direction.
int n = ...;
int i = ...;
int array[n];
int (*p)[n] = &a;
(*p)[i] = 1; // safe (with checking activated)
int *q = a;
q[i] = 1; // unsafe (except if the compiler can derive the size somehow)I am quite sure it will happily corrupt memory in most C compilers I would reach for, that are a decade old.
Hardly helpful to write portable code safely.
If no one has done it so far, it kind of proves the point how this matters to C compiler vendors across the ecosystem.
Maybe optional fatigue is Rusty but it's not a good style in any language
For "fallible" variant indexing, you have `the_slice.get(index)`: https://doc.rust-lang.org/stable/std/primitive.slice.html#me...
Although its method signature is kind of cursed due to generics, it matches your requirement that allows graceful OOB handling.
If the default indexing behavior were like `.get()` it would be too cumbersome to express repeated use of indexing, or multi-dimensional array indexing, etc.
Also there is get_unchecked which is unsafe. Trivially you'll trigger OOB access when misused.
That's why I think having unwinding in Rust is a mistake. It gives a false sense of safety, but it's very hard to write unwinding-correct code. So, it would be better to handle panics as immediate process termination instead.
I don’t think that an unwind-less rust would have fit within the weirdness budget 10 years ago, today it would definitely be something to consider. Although it would have an unwind effect to contend with.
You can also get SIGBUS by mapping a file beyond the end of the file and then trying to read the extra bytes. If Rust's version of mmap checks this, you can still do it by truncating the file while it's mapped.
But that's the point see? You have to catch panics because it's easy, straightforward and "common" to trigger panics.
If it were not, you wouldn't, because the buggy user-provided code would not be triggering panics, in the same way you're not catching `std::process::abort` or segfaults or whatnot.
If you don't have any way to do non local control flow you get what web developers call prop drilling. Code gets more complex because you need to check and handle every possible error condition inline.
Maybe you could get away with it by supporting union types and changing the `?` operator to accumulate error types into a `Result<T, E1|E2|E3|...>` then short circuit on failure. That way your code can decide how to handle each one instead of choosing between immediately return error or do verbose manual handling.
Your plugin can still crash in ways other than a panic, like a segfault inside an unsafe block.
It's also likely possible to DoS the application from a plugin in ways other than a crash, e.g. add a billion entries to the right-click menu.
And if you're loading a plugin, how do you know the plugin was actually written in Rust, anyway? It could be written in C and scribble over all your stack memory as soon as you load it.
It's very hard to guarantee stability in the presence of arbitrary extensions, and you might not even want to because someone might have a real use case for an extremely large right-click menu (probably not a billion though). I don't think even language server protocol guarantees it.
Usually it's either safety-via-language (Lua, JS etc.) or just YOLO (dynamically linked libraries), at best lightweight isolation like WebAssembly but that's modern projects, <1%.
The process isolation approach is only widely used for Go (Kubernetes, Terraform), because Go doesn't support DLLs (nicely)... (and the systems themselves are very slow anyway)
Those are plugin systems too, unless your term 'plugin' requires dynamically loaded code libraries by definition.
Testing for panics is just a silly feature itself anyway.
reminds me of the tales from https://oxide.computer/blog/iddqd-unsafe
Effect systems usually fail because effects are very different from each other and can't be generalised.
I tend to agree with the general principle that panicking or throwing an exception should terminate the process in most cases. But those last microseconds of a process' lifetime matter quite a lot: Are you able to save the user's work? Are you able to log the error so the crash can be diagnosed?
With Undefined Behavior in the picture, you cannot do anything. The whole process is toast, and its entire memory state is invalid.
You can’t rely on it for correctness because for your software to be reliable it needs to deal with an abrupt power outage and recover.
Best effort diagnostics make sense. And the “Rust 2.0” or successor language will with certainty close this gap.
But I also understand why we are here. A decade ago when 1.0 came out Rust had already used up most of its complexity budget for traits, enums, borrow checker, generics, functional style programming and so on.
Diverging even further from what was expected by the majority of programmers would very likely have left Rust as an esoteric language with a tiny following. Instead of what it became.
And back then unsafe was thought to work more like “just do like in C”.
There has been years of research formalizing how it actually works that’s brought us to the current understanding where it’s not trivial to write it correctly.
It is just too difficult to write unwind safe code and it is not much of a benefit even if you have it.
Most Rust learning material teaches panic as if it's an abort. They don't lie about it unwinding, but 99% of uses of panic use it like a fatal error with no expectation of recovery. The fact you can _recover_ from panics is even worse because you can then continue running a program after one of these "fatal" errors failed to abort the process.
There's some powerful things panic/catch_unwind let you do. I'm yet to be convinced the complexity imposed on all code is worth the squeeze on the cases where panic = unwind is a genuine requirement.
I wish the default was panic = abort, but as a whole I still find Rust the best tool for where I use it.
You seem confused. The "exception safety" topic in C++, which clueless people often cite Google's style guide as an appeal to authority, is that it's undesirable to modify old legacy code that does not use exceptions to call libraries that do. The sole reason this is not desirable is that calling code that throws exceptions introduces code paths that the legacy code was not designed to consider and cleanups that leave it in a state it was not designed to be in (i.e., all stack unwinding and RAII cleanup in surprising points with surprising implications).
And project managers tasked with a project that involves legacy C++ code can't simply spare time and resources to redesigns legacy project to accommodate for these changes, or the risk of having stable production code one day crash just because someone linked to a library that now throws.
Therefore, the "exception safety" discussion in C++ is not a technical one. It is a project management one, and one which involves high reliable legacy code.
I don't know if it's ever been in Rust's threat model to handle malicious code.
Note that incorrect code can still cause
1. incorrect outputs, or
2. arbitrary performance degredations
safe rust doesn't have to work magic. All it must do is prevent UB.
I thought maybe I explain this well in the README for https://github.com/tialaramex/misfortunate/ but apparently not, so that's on my FIXME list now.
When I write such an explanation it will probably use Comte as an example. Comte implements ExactSizeIterator which is a safe trait for claiming your iterator gives accurate information about how many items are left. It's lying (but equally a safe implementer might just be mistaken), and Comte is not in fact giving accurate size information. However, if you need to rely on a promise that the iterator length is correct Rust has an unsafe trait for such promises named TrustedLen.
Unlike Comte, mis-implementing TrustedLen could cause Memory Unsafety, but you would need to write the `unsafe` keyword and be lying or mistaken, which is what Rust promised you will happen.
To give a concrete example, if I tell Rust I want to put the results from this iterator over Goose into an array [Goose; 6] in usual cases it will emit code to check for overflow and panic if the Iterator makes the wrong number of Goose, even if the iterator implements ExactSizeIterator and claims to have six Goose, that panic check happens. But if it implements TrustedLen and makes the same claim, the compiler will believe it and skip the check. If it was wrong about having six Goose, your program is nonsense and anything might happen.
* Non-fused iterators, implementing FusedIterator
* Iterators that, when cloned, produce an iterator with a different set of elements
* DoubleEndedIterator which is inconsistent when iterated from each end
* A trait such that cloning a Box<dyn Trait> produces a value with a different concrete type inside
* A Display type that always produces an error from fmt()
* A Display type that replaces the Formatter with a different one
* A type that always hashes to different values.
It is just the sketchiest feature in a very realiable language.
It doesn't make sense to me why anyone whould use rust with panic handling.
And the of course the language trusts the operating system, how else can you program?
This kind of thing is basically why all projects blocked AI issues, it is just slop that kind of makes sense if you don't know what you are doing.
How much code's inside the CAS loop? If it's being held for less than (sticks finger in the wind) a few thousand of instructions then backoff or limits are actively counterproductive. That's how you're supposed to use a CAS loop.
If it's more, you shouldn't try to invent a more clever CAS loop, you should replace it with a mutex.
Also, if your language isn't old and crap your mutex already knows about this trick and so you should default to using a mutex, because it will do that anyway, and only write your own CAS loop if you have measured that it matters.
Note that word measured. Since we're talking about Rust here and people doing this stuff should be used to writing SAFETY justifications in their Rust, your CAS loop should have an accompanying justification comment.
Agreed that mutexes are easy and a safe default.
The Benaphore wraps an OS mutual exclusion primitive (in BeOS the Semaphore) with a simple flag you can CAS, so your program makes an OS Semaphore, and when something wants mutual exclusion instead of using the OS to lock the semaphore they instead first CAS unlocked->locked and don't call the OS at all if they succeed. Only when it's contended you make the OS call and lock the OS primitive. This makes the uncontended case very fast while the contended case is little worse than before.
With a Futex (or similar) you don't need that OS Semaphore, instead the OS just remembers a list of waiters and threads can add themselves to the list if they discover their lock was contended. In all of C, C++ and Rust the out-of-the-box mutual exclusion now does this already on popular targets, of course I think Rust does this best but I would think that because I like Rust. Even in plain C you should never assume you can do better than what's provided in the box. Either measure or accept that you don't care and use the provided mutual exclusion.
Rust is unsafe by spec, not just by implementation. For memory and type safety. concurrency safety is impossible to get with their blocking IO approach.
I don't quite like AI, but I think from an objective point of view one can reason that AI led to the discovery of more bugs, if we deduct the non-bugs from those reports.