11 year old unsoundness bugs found in Rust borrow checker
Given that this issue has been shared online and is currently getting quite a few visitors, here’s some additional context.
Rust’s type system is intended to prevent memory unsafety. Upholding this promise is something we take very seriously, which is why we’ve been working on things like the next-generation trait solver, the region constraint rework, and more generally Project Zero. It is a large priority of the Types Team to fix all type system unsoundnesses and to work towards there being a solid foundation to minimize the risk of unknown ones.
This bug is a P-high issue and would have been P-critical regardless of whether people encountered it in the wild. The reason that it is “only P-high” is that it’s 11 years old. A bug like this which has not been found during the last 11 years cannot actually be that easy to hit.
Looking at the bug itself, it is also very likely that this unsoundness has not caused memory unsafety by accident. While it looks easy to trigger, to exploit this you need an intentionally malicious Drop implementation.
In the following code:
I expected to see this happen: program is not accepted
Instead, this happened: program compiles and prints XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX. With asan, the program will crash with a AddressSanitizer: stack-use-after-scope
Issue on current nightly, and stables at least down to 1.85.0
Godbolt with asan: https://rust.godbolt.org/z/83MvfbKde Godbolt without asan: https://rust.godbolt.org/z/GsGdqqMWe
The core of the unsoundness:
My version of the code has been compiling ever since 1.18.0 (2017)
How on earth was this not found before this???
Possibly related: #128225
without relying on a static
Another version that segfaults/prints freed memory since 1.4
Another version, cuz why not. I think this version is probably the easiest to understand.
I think unification just generally doesn’t check lifetime bounds of the type. Example with unifying coercion, without field assignment:
fix: refuse handles in aggregate storage on both front ends
Hello everyone, mod here.
We’re preventively locking this issue to prevent spam and other unhelpful comments