11 year old unsoundness bugs found in Rust borrow checker (github.com)
630 points by formerly_proven 12 hours ago
GrantMoyer 9 hours ago
It's surprising this bug went unnoticed for so long, but at least it's easy to fix, since it's not a bug with the type system itself; it's just a bug in the compiler. The compiler should always have been rejecting that code according to lifetime rules of Rust's type system, so the fix is to just make it reject the code, rather than having to change the fundamental rules of lifetimes.
WiSaGaN 9 hours ago
It's fascinating that this kind of bug is exactly you can't really avoid even when rust is supposedly to guard against. Rust mitigated this problem a lot in practice, but you can never be certain because of issues like this in practice. This is similar that a math proof that's already been peer reviewed and accepted as truth, may still be possible to be wrong, although the chance is very small but it's not zero. Epseicially when the complexity gets higher.
lacker an hour ago
In theory it would be possible to formally verify the soundness of the borrow checker. Maybe with AI advances we'll be able to do that in the near future. I mean, how much more complicated can it really be than proving Navier-Stokes?
itishappy 8 minutes ago
mlindner an hour ago
orf 2 hours ago
How is this different from any other compiler bug?
There was one in the Linux kernel a while ago that mistakenly eliminated an important null check.
Is this different?
yencabulator 34 minutes ago
Garlef 7 hours ago
... and even if you formalize it, it could still be false due to an error in the theorem prover itself.
GrantMoyer 7 hours ago
gavinsyancey 4 hours ago
mav65 9 hours ago
Not knowing anything about rust, the problem in these type of case is making it rejecting that bad code without also rejecting valid programs. But maybe this is indeed easy to fix, I don't know.
foldr 8 hours ago
In this case the issue is that the compiler just isn't properly type checking the implicit call to the drop method that's inserted at the end of the nested block. AFAIK, no correct code should depend on the absence of that check.
bluGill 9 hours ago
There are no such valid programs. Proof by contradiction:
A valid program would be exploiting undefined behavior in rust, but rust has no undefined behavior. Thus such a program must not exist.
Now a program may appear to work before the fix, but it was never a valid Rust program.
afdbcreid 9 hours ago
wging 3 hours ago
weinzierl 10 hours ago
This is remarkable but not super unusual. There are more than 100 issues labeled unsound currently. Some are long standing but from what I understand the new trait solver will unblock fixes for some of them (but probably not the one in OP).
theemathas 9 hours ago
Finding unsoundness bugs isn't the most unusual, yes. But this one is unusual in how simple the code that causes it can be (see the versions of the reproducer code in the comments). For other unsoundness bugs, the code would need to contort itself a lot to avoid the compiler checks.
lambdaone 10 hours ago
Perhaps ironically, AI-based searching of the Rust compiler for bugs is likely to find more of these, pushing Rust closer and closer to the point of complete reliability.
The eventual formalization of Rust and a creation of a formally proven compiler is presumably going to be the end point of this process.
stingraycharles 10 hours ago
Why would that be ironic? AI is helping us find so many subtle bugs and isolate the root cause, it’s nearly perfect for this type of use case.
groundzeros2015 8 hours ago
> complete reliability.
I think that’s a myth! What tends to happen in mature software is there are unfixable tradeoffs that can be made that favor one use case or another. And of course people will keep adding features too
dymk 7 hours ago
That’s why they said “closer”
vips7L 5 hours ago
Just wait until someone relies on that bug too.
Ygg2 2 minutes ago
mi_lk 9 hours ago
What aspects of it are remotely ironic?
jere 9 hours ago
Obviously, many people have viewed AI’s contribution to programming as just producing unreliable garbage.
Yes, that’s an incomplete and tenuous belief, but that’s where the irony is. If you’re omniscient you don’t get to experience irony.
brzz 9 hours ago
Hey, if Alanis Morissette can make the same mistake…
antonvs 7 hours ago
“Slop generator makes all future programs more reliable”
sanderjd 6 hours ago
bbor 10 hours ago
Until they find bugs in the formal proof program ;)
https://www.lesswrong.com/posts/zHhGa3iQBQcdSifFL/a-critical...
samus 6 hours ago
It is well known that proof assistants can be wrong. Fortunately, the theories underlying formal methods are usually simple enough that it is feasible to write verifiers for these proofs.
pmarreck 9 hours ago
The UB goes all the way down!
malkia 9 hours ago
and what stops AI from "accidentally" writing code with such bugs?
hbn 9 hours ago
Code review and testing? As always?
bcjdjsndon 7 hours ago
antonvs 7 hours ago
The compiler. That’s the whole point here. Type systems can prove the absence of entire classes of bugs, at compile time. If AI identifies bugs in type systems that can be corrected, then all future programs benefit, including those written by AI.
lifeisloving 9 hours ago
I asked myself, How many comments to do I have to collapse before someone says something about AI. 3.
I used to compare this hype to crypto hype, but its so much worse. Can we just try not to shove llms into every single conversation.
Is there a hackernews type space where the word is banned? Even lobste.rs has this problem. I need some sort or way to just hide all content on certain sites where LLMs/AI are mentioned.
vor_ 2 hours ago
I browse Hacker News stories via this: https://unslop.news/
otherayden 14 minutes ago
lifeisloving an hour ago
purplesyringa 7 hours ago
lobste.rs has a `vibecoding` tag that you can hide.
alethic 2 hours ago
lifeisloving 5 hours ago
streetfighter64 9 hours ago
I'm not a huge AI hype man, but I think you'll just have to accept it. It's a new useful tool, of course people are excited to apply it to everything.
Like it or not, programming will become largely LLMs. And the comment you're replying to is actually making a pretty good case for where the use of LLMs is good: generating code to be formally verified.
lifeisloving 8 hours ago
1sh172645 7 hours ago
kimi 10 hours ago
Or we realize that it is not possible, and Rust accepts to be "just" a definitely safer C.
wongarsu 10 hours ago
There is a formally proven compiler for a large subset of C, so I don't see why formalizing (a large subset of) rust and making a formally proven compiler shouldn't be possible
afdbcreid 9 hours ago
afdbcreid 9 hours ago
UncleMeat 10 hours ago
Eridrus 8 hours ago
I feel like these things get waved around as gotchas without any sense for whether any real code is impacted.
It will be interesting to see if fixing this bug actually causes any crates to not compile.
Falell 8 hours ago
We should know shortly, the PR to fix this will presumably be paired a crater run, a build of the entire public crates universe.
mrkeen 11 hours ago
Type systems are hard! Check out https://counterexamples.org/
I gave it a quick scan for Rust examples; there are 11 samples among the following pages:
https://counterexamples.org/eventually-nothing.html
https://counterexamples.org/mutable-matching.html
https://counterexamples.org/selfishness.html
https://counterexamples.org/scope-escape.html
https://counterexamples.org/nearly-universal.html
The first thing I always look for is mutability (combined with some other tricky feature), e.g. "Now it's a Just(3):Maybe<Int>, now it's a Nothing:Maybe<Int>, now it's a Nothing:Maybe<String>."I wonder if TFA's example could be reproduced without mutability.
oconnor663 6 hours ago
https://github.com/JelleZijlstra/unsoundness is also a great collection of Python type system holes. In these cases, it's mostly deliberate design decisions, not bugs. The unfortunate fact is that a fully sound Python type system would be a pain to use.
gsnedders 3 hours ago
> The unfortunate fact is that a fully sound Python type system would be a pain to use.
Even beyond Python, most gradual type systems are unsound by design.
metalliqaz 5 hours ago
Perhaps I'm just an old dog that doesn't want to learn new tricks, but I still can't think of Python as typed and I never use type hints. I started with Python during the 2.3 era. I still view Python types as merely docs. So it's nice that someone tracks mypy quirks but I can't imagine relying on the results for anything serious.
Twirrim 4 hours ago
oconnor663 5 hours ago
tikhonj 4 hours ago
ChrisSD 5 hours ago
dang 3 hours ago
> Check out https://counterexamples.org/
A few past discussions if anyone is curious:
Counterexamples in type systems (2021) - https://news.ycombinator.com/item?id=48718972 - June 2026 (4 comments)
Counterexamples in Type Systems: programs that crash, segfault or explode (2021) - https://news.ycombinator.com/item?id=36213704 - June 2023 (97 comments)
Counterexamples in Type Systems - https://news.ycombinator.com/item?id=27252899 - May 2021 (62 comments)
immmmmm 6 hours ago
Aren’t some type systems (eg Lean) able to essentially verify/prove arbitrary mathematics?
kachnuv_ocasek 5 hours ago
Yes, until you introduce inconsistent axioms. Then you can prove anything.
codehero 9 hours ago
"Type systems are hard!" As hard as its safety is pushed on us, not even Rust is free from hand waving.
ahartmetz 9 hours ago
At least Rust has a chance to be largely correct because it has much greater effort per amount of code (in the compiler) invested into it than most any application code.
antonvs 7 hours ago
Rust’s type system enables the possibility of discovering and fixing a bug like this, and ruling out this class of bugs for all future programs.
The hand-waving comes from other languages which can’t do anything to prevent their users encounter such bugs and the ensuing CVEs.
doodpants 8 hours ago
I mean, Rust's whole shtick is safety, so that's really the only thing there is to "push" about the language.
oersted 8 hours ago
nimih 7 hours ago
eloisius 8 hours ago
airstrike 7 hours ago
hackingonempty 8 hours ago
caaqil 9 hours ago
> not even Rust is free from hand waving.
It is moral though, so who cares really.
j4k0bfr 10 hours ago
Took me a good couple minutes to wrap my head around. Man, what a nasty bug. I guess this specific use-case is rare. Time to see if any of the big libs are affected...
coliveira 9 hours ago
A nasty and rare bug can and will be exploited once it is found. The only thing necessary is for malicious actors to start adding this case in innocent looking code and there you have.
afdbcreid 9 hours ago
That's a misunderstanding. This is not a security vulnerability and Rust explicitly does not defend from malicious programs, only from mistakes. This bug however is quite dangerous, because unlike many soundness bugs it seems plausible to hit by mistake.
Liquid_Fire 7 hours ago
randyrand 9 hours ago
I just wish Rust handled out of memory more gracefully like a normal error.
yencabulator 9 minutes ago
On a typical Linux box, overcommit and eager or confused OOM killer will ruin your day anyway.
For no_std style work where running out of memory is actually a hard limit, you have two choices:
1. disable the global allocator and use APIs that take Allocator, to force yourself to only consume APIs that can return errors from allocation. (The ergonomics aren't always great, and fattening all types with a pointer to Allocator isn't great; might be able to use a zero-sized Allocator, with trickery. Say goodbye to many libraries.)
2. use static analysis and try to avoid ever hitting a code path like Vec::with_capacity that doesn't return a Result. Make sure the tooling also analyses your dependencies. (Tooling for the static analysis is painful. And still, say goodbye to many libraries.)
I do wish there were more conveniences for alloc-can-fail APIs, like the failable version of Vec::push needs both try_reserve and push_within_capacity (nightly only).
bluGill 9 hours ago
Why bother? Most systems these days have over commit and so you can never get an out of memory error. The OS will happily give you more and more RAM, and then when you try to use that memory kill a program (may or not be your program). The only out of memory error you can see is if you are trying to allocate more memory than the address space allows. This is so unlikely that nobody considers it worth bother to try to handle.
JoshTriplett 8 hours ago
It is important to try, for a few reasons. Among other things, increasing amounts of Rust written in the Linux kernel itself, and other environments where you need to take more care with memory allocation.
Rust as a language is capable of that. If you don't use the standard library then you can provide that functionality yourself. The standard library is trying to support it, without creating interfaces that are a massive pain to use.
For instance, it's easy for us to support `Vec::try_reserve`, and we do, but that's not trivial for users to use everywhere; it's error-prone, and it's easy to make a mistake and call a method that goes past your reservation. It's painful for us and for users to make `Vec::push` support this (and having a `try_` variant for every function), so we've been working on a better solution. One current possibility is to have a dedicated type for a Vec that has OOM handling, where `push` can return an error, so you know if you use that type you can never accidentally fail to handle OOM.
IshKebab 2 hours ago
> Most systems these days have over commit
Sure if you ignore the most popular desktop OS in the world.
Also overcommit (at least how Linux does it) is dumb. It leads to broken OOM behaviour (killing a totally random process, or more often locking up the system and hard rebooting). And some apps do need to handle OOM properly, e.g. formal verification systems which can use enormous amounts of memory. Jasper has to resort to monitoring page fault stats to know when it is using too much memory.
efficax 7 hours ago
the biggest design wart of rust (well one of them) is that support for custom allocators wasn't built into the type system from the start, with allocation osmething that returns Result
yencabulator 19 minutes ago
Sure, but it's worth acknowledging that that specific wart is being fixed.
gosen 9 hours ago
I'm sure we'll get more of those on other languages / frameworks too. I tend to believe that it's for good
wat10000 8 hours ago
The emotional reactions to this stuff are so weird. Yeah, software has bugs, what a surprise. That doesn't mean that static safety is useless or that we should all just continue YOLOing stuff in C.
FuzzywarmWalnut 11 hours ago
Worked on a variance-related lint once and the scary part is how rarely anyone writes down variance by hand, so bugs there just sit untested for a decade.
afdbcreid 9 hours ago
Nobody writes variance at hand in Rust because it's impossible (since before 1.0). You sometimes need to enforce stricter variance than the default (can cause unsoundness and is very subtle but actual unsoundness here is rare) or give more permissive variance (harder and also cannot cause unsoundness, just annoyance).
When implementing the type system, however, variance does have to be treated with care. Historically it wasn't much, now it is.
asdfman123 35 minutes ago
Beg, Borrow, or Steal: a guide to Rust object ownership
Surac 8 hours ago
The question is. If rust used rust would that have prevented this?
zamalek 7 hours ago
Rust does not prevent all bugs, only certain ones (that are common and dangerous).
romac 4 hours ago
The Rust compiler (rustc) is written in Rust, but there can always be bugs in the compiler, regardless of whether it's written in a memory-safe language or not.
loa_in_ 6 hours ago
Rust allows for type-proofing your logic, but it can't force you to.
efficax 8 hours ago
no
MrBuddyCasino 11 hours ago
The heart of the matter:
struct Q<T: 'static>(Option<T>);
impl<T: 'static> Drop for Q<T> {
fn drop(&mut self) {
// Code in here has access to a non-'static &String
// but assumes that it's a &'static String
}
}
fn main() {
let l = String::new();
let mut q = Q(None);
q.0 = Some(&l);
}
I wonder if the certified compiler for safety critical uses (edit: Ferrocene) has the same issue.afdbcreid 11 hours ago
> I wonder if the certified compiler for safety critical uses has the same issue.
It uses the same code, so yes.
(Although I don't know what are their guidelines, it might be that they require avoiding code relying on implied bounds, for example).
1718627440 10 hours ago
Can someone ELI5 for people who don't know Rust (but e.g. C)?
thinkharderdev 10 hours ago
Rust is supposed to guarantee statically that a reference cannot outlive its referent. The `T: 'static` bound means that the value that Q holds must have a 'static lifetime (e.g. either be an owned value or a reference to a static value). So this program should not compile since a reference to l is not 'static. This code is unsound because rust will drop the string l before q so when the Q destructor runs the underlying string has already been freed yet we have a valid reference to it.
1718627440 9 hours ago
biorach 8 hours ago
1718627440 10 hours ago
efficax 8 hours ago
'static lifetime bounds are kind of confusing in Rust, but here goes: A 'static lifetime constraint on T says: types that satisfy T must not contain any borrowed value/content whose lifetime will be shorter than the life of the program. So suppose you have a string s, and Option<&String> q. If q has the constraint 'static, and you do q = Some(&s) this should be rejected by the compiler since a reference to s gets a shorter lifetime than 'static by default (you could do q = Some(&'static s) if s is say a string literal or a leaked box but not for a normal string). In the examples it looks like you have to use a tuple to trick the borrow checker but the concept is the same: Rust allowed violation of the 'static lifetime bounds. The drop implemenation demonstrates it: drop happens in reverse declaration order, so the string s gets dropped first. Now q has a dangling reference, a pointer to dropped memory essentially, and we get a pretty classic use after free bug.
high_na_euv 11 hours ago
which compiler?
sshine 11 hours ago
Ferrocene
wg0 11 hours ago
DeepSeek v4.1 Flash thinks it should NOT compile. Whereas in reality it DOES compile which is why it is filed as a bug.
jug 11 hours ago
They're seriously using AI as a judge for what to file?
afdbcreid 11 hours ago
wg0 11 hours ago
badatnames 11 hours ago
There is a collection of these someplace. Rust is better but it'll never be perfect, news at 10
zer00eyz 11 hours ago
Rust is good, I generally do not dislike it - but it does have flaws.
I will happily continue to write things in Golang first. I don't enjoy refactors in Rust, and it's rare that I see things that genuinely need its raw performance.
Cloudflare's Oxy is a stellar example of build in Golang till your design is settled and then port over for performance when you need it.
vincent-uden 11 hours ago
What the hell. This is not the esoteric, crazy, impractical constellation I expected to find with a title like this. The minimal example is so simple and is definitely something you'd easily come across in normal code.
insanitybit 11 hours ago
For a long time `loop {}` was undefined behavior. Lots of l-unsounds are really nuts but a few are not.
The reality is that if this were actually code that people wrote, well, it wouldn't have sat around for a decade.
masklinn 10 hours ago
Afaik loop {} has never been UB, however because llvm started as a c++ compiled where it is, this has been causing miscompilations in rust and C for… nearly two decades (llvm issue 965).
insanitybit 5 hours ago
cowlevel 9 hours ago
fg137 10 hours ago
> is definitely something you'd easily come across in normal code.
The fact that nobody reported this before means it's not, by definition.
vincent-uden 9 hours ago
Not at all. There are way more valid lifetimes than the lifetime system of Rust can express. There are countless scenarios where you could accidentally use the unsound behaviour while not producing a segmentation fault because the lifetimes happens to be arranged such that no invalid memory access actually happened.
A program isn't invalid just because it violates the ownership and borrowing rules of Rust. There are tons of programs in the world with shared, mutable access, devoid of Mutexes, Arcs and Locks that nonetheless are completely valid.
fg137 9 hours ago
LoganDark 11 hours ago
I wonder if the reason it works is because you'd easily come across it in normal code, and someone didn't realize that saving the effort of implementing a check for this would result in unsoundness.
afdbcreid 9 hours ago
That's not how it works. rustc doesn't "check" for specific cases explicitly, the borrow checker (apparently at fault here by reports) has a set of rules that should reject invalid programs. So either one of the fundamental rules was broken (unlikely here from what I've heard), or some optimization caused the rules to not be implemented correctly.
LoganDark 39 minutes ago
jvanderbot 11 hours ago
Not really. I've worked with rust for 4 years, systems side, and never had to write a custom drop implementation. That's the gotcha here, not the rigamarole with the static+Send option type (which _would_ be something you'd encounter in prod).
bryanlarsen 11 hours ago
I've written dozens of custom Drop implementations. Well, many of them are named cleanup because of the async Drop mess in Rust, but...
dwattttt 10 hours ago
jvanderbot 10 hours ago
neysofu 11 hours ago
That's easily the exception rather than the rule. I've seen custom `Drop` implementations in almost every non-trivial Rust codebase I worked on.
gspr 11 hours ago
A custom `Drop` appears very quickly with FFI code. Suppose for example you interface with some C library that expects you to call a function to release some resources.
rvz 10 hours ago
Now we are at the phase where, Rust is “safe” but only if you use it “correctly” and without using X language feature or a custom Y feature.
This is the problem when a language advertises with “guarantees” and it turns out it never had any due to many false assumptions by their promoters.
So what other soundness bugs are there in the language that we need to know about?
jvanderbot 10 hours ago
cmrdporcupine 10 hours ago
Hugsbox 11 hours ago
I don't understand how something like this could take so long to be discovered. Pretty crazy stuff.
lelanthran 11 hours ago
> I don't understand how something like this could take so long to be discovered.
Maybe they wrote it in C :-)
On a serious note, old software is generally more trustworthy than new software, because bugs are discovered over time and with usage.
dabinat 5 hours ago
> On a serious note, old software is generally more trustworthy than new software, because bugs are discovered over time and with usage.
The age of software means bugs are more likely to be discovered but not necessarily fixed. In the case of Rust, the developers have documented a whole lot of unsoundness bugs that require a new borrow checker architecture to fix.
zaphar 10 hours ago
Old C software is generally more trustworthy than new C software. I don't think this rule of thumb transfers across languages very well. And the definition of new vs old has a fairly large effect. If a language has been under constant change it doesn't matter when it was started a large part of the codebase is "new".
fg137 10 hours ago
Because it's rarely used like this, just like weird bugs caused by edge cases found in all kinds of software in the world.
z3ratul163071 9 hours ago
so now what? back to c++?
medler 9 hours ago
It seems the bug here was caught by Asan instead of at compile time, so basically, for this one bug, rust is reduced to C++-like behavior. You’re still better off using rust because of all the bugs the borrow checker does catch.
The practical upshot to me is that you should be running your rust tests under asan.
JABVOLT 11 hours ago
very very good job
fnord77 10 hours ago
Here's the weakness of compiled programs. Does this compiled code have this vulnerability? You have no idea. At least with a VM or interpreter you can scan for vulnerable versions and replace the runtime or the specific library.
With this you have to recompile everything
cowlevel 9 hours ago
It's possible to scan compiled programs at the assembly or machine code level. Not enough people do, which is fair enough because it's a slog, but it makes all compiler UB transparent to the analysis.
baq 10 hours ago
That is less and less true by the day, for better or worse.
Rapzid 7 hours ago
JFC
I have never seen Rust code
Threw up in my mouth
Inspired by that Rust code.Rapzid 7 hours ago
That was a bit generic and easy to swap "Rust" for whatever...
Dense syntax pulsing
Writhing and undulating
What designer, who?rurban 9 hours ago
And a fork fixed it before upstream rust :)
Groxx 7 hours ago
...yes? that's usually the case in open source software.
sreekanth850 5 hours ago
Was this done by LLM? this is a doubt. because recently we are seeing many such bug reports.
beanjuiceII 10 hours ago
wonder what happened to this https://raphlinus.github.io/rust/2020/01/18/soundness-pledge...
jeremyjh 10 hours ago
I think it has no relevance here at all. Actix was unsound because of prolific use of unsafe. That was a poor design decision but a conscious one. TFA is just a bug.
Having ideals does not mean there are never bugs.
ModernMech 10 hours ago
What do you mean?
cheney_2004 8 hours ago
I think one social issue I’m seeing is that the Rust community bashes other languages for safety but then glosses over or even completely ignores their own safety risks (see supply chain). I would rather see Rust focus on its actual features without bashing others and maybe start moving away from claiming the gold metal on security. Being less toxic is always a good thing for long term health.
gchamonlive 8 hours ago
It's important to separate the community ethos and the rust core dev team policies, because I'd be surprised if the official stance of the rust team would be condescending to other technologies.
the__alchemist 5 hours ago
Rust is my favorite language in most domains I program in; I've learned to ignore the online rust OSS community due to a mix of preferring heavy abstractions (then shaming your code if you don't use them), and rude behavior.
This is fine, for the reasons you state: I can use the language and tools while ignoring the jerks.
SV_BubbleTime 8 hours ago
Have you met humans?
I’d be shocked if people working on and passionate about thing didn’t shit on other thing. That’s natural and even good in moderation. It’s up to a leader to keep it under control.
mcherm 8 hours ago
tehjoker 6 hours ago
ModernMech 8 hours ago
Can you give an example of this toxic bashing from rust contributors or are you talking about just random HN posters when you say “rust community”?
Moreover, I wouldn’t say they’re completely ignoring supply chain risks, as there have been discussions about this as early as 2021 [1] and there are ongoing efforts to address this [2]
[1] https://internals.rust-lang.org/t/about-supply-chain-attacks...
lifeisloving 8 hours ago
Yeah Ive actually never seen any Rust team members be toxic. Keep in mind there is a micro niche of tech influencers who try to politicize rust because they associate it with liberalism or something like that and paint Rust community as an opposing force to some status quo they think exists or should exist. Those notions have taken on a life of their own.
People who say this just dont know what they're talking about and are repeating youtubers and X takes.
bcjdjsndon 7 hours ago
There's a fanaticism amongst Rust devs that borders on a cult. They even described their thinking as such, unironically.
Even the basic logic is flawed.... That coders can't be trusted to handle pointers, but somehow can be trusted to write "safe" unsafe rust.
dabinat 5 hours ago
cmrdporcupine 5 hours ago
slopinthebag 4 hours ago
the only thing more exhausting than rust evangelism is people who drop in on any rust related thread and start whining about unrelated things just because it's rust.
jjgreen 8 hours ago
Dream on
zamalek 7 hours ago
> being less toxic
There's a difference between people you disagree with and toxic behavior. Can you give examples of toxic behavior?