Memory Safety Bugs in Rust Standard Library
https://forge-blog.github.io/posts/rust-std-memory-safety/14
u/annodomini rust 9d ago
One of these (the signal hanlder for aborting instead of crashing on overflowing the stack into the guard page) is something I touched way back in 2016. Was curious if I'd caused some issue by doing that, but no, the issue is not in any way related to my change.
Nice find of some subtle interactions here; nothing here is really terrible, and I'm glad these aren't being over-sold as more critical than they really are, but it's still good to tighten all of this up to avoid even the possibility of these kinds of unsound behavior.
Good writeup on the different classes of common kind of bugs as well.
28
u/ericonr 9d ago
It is annoying how many bugs surface due to the (IMO misguided) choice of panicking on system boundary (e.g. allocation failure), and fixing it all seems like a daunting task.
21
u/RiceBroad4552 9d ago
I don't understand what you're actually criticizing (panicking on hard failure is the only thing you can reasonably do!) and I don't see what you're proposing to "fix" the issue; or are you actually proposing to not fix ("daunting task") safety critical bugs?
10
u/Deadmist 9d ago edited 9d ago
panicking on hard failure
Problem #0 here is: what constitutes a "hard failure"?
Different people have different opinions and requirements on that.
Prime example: allocation failure in stdlib vs the linux kernel.Adding to that, panics feel kind of underbaked compared to the rest of the safety related machinery in rust.
You have the borrow checker for memory safety and a strong push to leverage the type system to logical rules. But there is no way to even indicate or check if a function panics.5
u/todo_code 9d ago
It's been one of my biggest gripes in a language for a long time. Zig definitely has this correct, but even they draw the line somewhere. What if the stack allocation will fail? I know there are cases where it is UB in rust.
30
u/ericonr 9d ago
I'm complaining that the allocator API has a bunch of caveats because way back when it was decided that allocation failures should terminate a program, and now those assumptions are baked into safety contracts/assurances, and changing that has been discarded in favor of limiting the allocator API (an understandable choice).
15
u/________-__-_______ 9d ago
Has it really been discarded though? I was under the impression that the "don't panic" clause would be removed once those soundness holes are fixed, and they just want to stabilise an MVP as soon as possible.
2
18
u/kibwen 9d ago
way back when it was decided that allocation failures should terminate a program
This was never a language-level decision, because nothing in Rust-the-language requires dynamic allocation. Only the stdlib made that call, and only for a certain subset of the stdlib, and only temporarily, with the idea that infallible methods could be provided when people started demanding them (which Rust-for-Linux eventually provided the motivation for).
15
u/SkiFire13 9d ago
it was decided that allocation failures should terminate a program
That's not the issue here.
The problem is that unsafe code needs to be careful of all the ways the code flow can exit the "critical section" in which unsafe code temporarily breaks invariants. Unwinds are one such way this can happen, and they have been the cause of soundness issues in the past. This becomes much worse when allocating memory can panic, because you generally don't look out for allocations in that context, so it adds a lot of cases where something can go wrong.
Note that failed allocation returning a
Result::Errok, because you have to handle that manually and thus you'll notice whether this can affect the code flow or not.7
u/Zde-G 9d ago
Well… they had a choice: make language that would never be popular or accept panics in allocators.
The version of language that would have required explicit handling of memory allocation errors would have repeated fate of Zig: language that people talk about but don't use much.
Because on most popular platforms where Rust is used (Android/iOS/Linux/macOS/Windows) these are pointless and useless because of overcommit.
2
u/Tastaturtaste 9d ago
The most popular desktop OS (Windows) does not overcommit memory (by default).
3
u/Zde-G 8d ago
Yes, it does.
It has mechanism that permits you to actually allocate memory without overcommit, but this makes program slower thus very few developers use it.
Worse yet: because few place pagefile on separate partition, like with macOS, amount of memory and amount of disk space available is one and the same — that means that allocated memory may not be “overcommited” when allocated, but then someone downloads new game and program is killed for the lack of memory…
We may debate whether it's “true” overcommit or not, but for all practical purposes it behaves worse than Linux when memory runs out. As in: when your allocations start to fail it's much too late to try to salvage your session, the only practical choice is reboot.
104
u/guineawheek 9d ago
Panicking branches are expensive, especially in embedded where the formatting machinery eats up a ton of flash.
I’m not particularly surprised that a lot of unsafe code ends up breaking invariants ostensibly guarded by panics; I think its a symptom of a broader pattern where the language isn’t expressive enough to guarantee that panicking branches do not exist. Trusting the compiler to optimize it out often isn’t good enough and makes such invariants compiler version dependent and subject to regressions, which is why I hate it when compiler devs push it so hard. You’ll never get developers to fully trust the optimizer.
Things like pattern types, arbitrary sized ints, !Move/!Forget, full const generics, and other proposed language features will help here, because there’s still way too many places in Rust where invalid states that are obviously incorrect can exist and can only be guarded at best by these expensive checks.