11 year old unsoundness bugs found in Rust borrow checker

formerly_proven · HackerNews · 5 min read · original

Skip to content

Navigation Menu

Sign in

All Features

Search/

Sign in

Sign up

Appearance settings

You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert

{{ message }}

  1. rust-lang
  2. rust

Repository navigation

More items

struct's lifetime requirements can be bypassed via covariant field assignment#164250

New issue

Copy link

New issue

Copy link

Open

#164277

Open

struct's lifetime requirements can be bypassed via covariant field assignment#164250

#164277

Copy link

Labels

A-borrow-checker Area: The borrow checkerArea: The borrow checkerA-destructors Area: Destructors (Drop, …)Area: Destructors (Drop, …)A-lifetimes Area: Lifetimes / regionsArea: Lifetimes / regionsA-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-bug Category: This is a bug.Category: This is a bug.I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-high High priorityHigh priorityT-types Relevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.

Description

Image 9: @jsnell

jsnell

opened on Oct 13, 2026

Issue body actions

Note

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.

~ @lcnr


In the following code:

undefinedrust
use std::sync::Mutex;
static KEPT: Mutex>> = Mutex::new(Vec::new());
struct Q(Option);
impl Drop for Q {
fn drop(&mut self) { if let Some(t) = self.0.take() { KEPT.lock().unwrap().push(Box::new(t)); } }
}
fn main() {
{
let l = String::from("hello world, this is long enough");
let mut q = Q(None);
q.0 = Some(&l);
}
let _z = String::from("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
let k = KEPT.lock().unwrap();
println!("{}", k[0].downcast_ref::<&'static String>().unwrap());
}
undefined

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

Meta

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

👍React with 👍52 Reacted by Eugene, Changgyoo Park, Sami Farin, Luke, Andrew Scott, Alex Gershberg, sigi, Afizullah, Artem Yarulin, Nikos and 42 more

😄React with 😄14 Reacted by Richard Patel, Frank Denis, Timur Tugushev, dzmitry-lahoda, Filip Dobrosavljević, Mikhail, Palm, teor, Abhinav Singh, fluoriteByte and 4 more

😕React with 😕3 Reacted by geo-bali, Michał J. Gajda and Abhinav Singh

🚀React with 🚀7 Reacted by Mateo Lafalce, prathamnagpure, Anish Shrestha, Richardn, Guilred, Abhinav Singh and Gabi Aioi

👀React with 👀74 Reacted by André Storfjord Kristiansen, Max, Serhiy, runiq, Dominik Wilkowski, James Lee, Stanislav Ursache, Edvin Bryntesson, Jeremy Lempereur, BioTomateDE and 64 more

Pinned bytheemathas

Pinned comment options

Image 10: @theemathastheemathason Oct 23, 2026

This github issue is not a forum for public discussion, but a means for maintainers to triage the issue for their impact, and track information that is directly relevant to fixing it.

We have locked this issue so that other comments do not clutter this issue, making it harder to find relevant information. Please see the note at the top of this page if you have concerns about the effect of this bug on Rust.

View full comment

Activity

Next

  1. Image 11jsnell added C-bug Category: This is a bug.Category: This is a bug. on Oct 13, 2026
  2. Image 12rustbot added needs-triage This issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging on Oct 13, 2026
  3. Image 13theemathas added I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness on Oct 13, 2026
  4. Image 14rustbot added I-prioritize Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical} on Oct 13, 2026
  5. Image 15theemathas added A-borrow-checker Area: The borrow checkerArea: The borrow checker T-types Relevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triagingThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging on Oct 13, 2026
  6. Image 16lcnr added this to T-types unsound issueson Oct 13, 2026
  7. theemathas commented on Oct 13, 2026

Image 17: @theemathas theemathas on Oct 13, 2026 Last edited by theemathas Contributor More actions The core of the unsoundness:

undefinedrust
struct Q(Option);

impl Drop for Q {
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);
}
undefined 👍React with 👍5 Reacted by Martin Habovštiak, Vilém Zouhar, whezard, Daniel Tschan and Matouš Volf 👀React with 👀19 Reacted by Daniel Vogelbacher, Serhiy, BioTomateDE, Timur Tugushev, palapapa, Ori Yadid, SeniorMars, dzmitry-lahoda, Til, Marco Quinten and 9 more
8. Image 18theemathas added A-destructors Area: Destructors (Drop, …)Area: Destructors (Drop, …) on Oct 13, 2026
9. Image 19rustbot removed I-prioritize Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}Issue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical} on Oct 13, 2026
10. ### rustbot commented on Oct 13, 2026

Image 20: @rustbot rustbot on Oct 13, 2026· Hidden as outdated show comment More actions
11. Image 21rustbot added P-critical Critical priorityCritical priority on Oct 13, 2026
12. ### theemathas commented on Oct 13, 2026

Image 22: @theemathas theemathas on Oct 13, 2026 Last edited by theemathas Contributor More actions My version of the code has been compiling ever since 1.18.0 (2017)

How on earth was this not found before this??? 👀React with 👀26 Reacted by Max, Alisa Sireneva, Leon, maxbry-uh, Luke, slaiyer, Screeching Bagel, Slanterns, maliciousblahaj, Timur Tugushev and 16 more
13. ### theemathas commented on Oct 13, 2026

Image 23: @theemathas theemathas on Oct 13, 2026 Contributor More actions Possibly related: #128225
14. ### 15 remaining items Load more
15. ### apiraino commented on Oct 13, 2026

Image 24: @apiraino apiraino on Oct 13, 2026 Contributor More actions Hello everyone, mod here.

We're preventively locking this issue to prevent spam and other unhelpful comments 😄React with 😄1 Reacted by Arseniy Terekhin
16. Image 25rust-lang locked as too heated and limited conversation to collaborators on Oct 13, 2026
17. Image 26rust-lang unlocked this conversation on Oct 13, 2026
18. Image 27rust-lang locked and limited conversation to collaborators on Oct 13, 2026
19. Image 28lcnr removed regression-from-stable-to-stable Performance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another. on Oct 13, 2026
20. Image 29rust-lang unlocked this conversation on Oct 19, 2026
21. Image 30minzyfox added a commit that references this issue on Oct 19, 2026
blog: what the borrow checker let through (vx-lang#1756) ... 295ac52
22. ### sodiboo commented on Oct 22, 2026

Image 31: @sodiboo sodiboo on Oct 22, 2026· Hidden as resolved show comment More actions
23. ### ChayimFriedman2 commented on Oct 22, 2026

Image 32: @ChayimFriedman2 ChayimFriedman2 on Oct 22, 2026· Hidden as resolved Last edited by ChayimFriedman2 show comment More actions
24. ### bsodmike commented on Oct 22, 2026

Image 33: @bsodmike bsodmike on Oct 22, 2026· Hidden as low-quality Last edited by bsodmike show comment More actions
25. ### TilakMaddy commented on Oct 23, 2026

Image 34: @TilakMaddy TilakMaddy on Oct 23, 2026· Hidden as low-quality show comment More actions
26. Image 35rust-lang locked as too heated and limited conversation to collaborators on Oct 23, 2026
27. ### theemathas commented on Oct 23, 2026

Image 36: @theemathas theemathas on Oct 23, 2026· Pinned bytheemathas Last edited by theemathas Contributor More actions This github issue is not a forum for public discussion, but a means for maintainers to triage the issue for their impact, and track information that is directly relevant to fixing it.

We have locked this issue so that other comments do not clutter this issue, making it harder to find relevant information. Please see the note at the top of this page if you have concerns about the effect of this bug on Rust.
28. Image 37theemathas pinned a commenton Oct 23, 2026
29. ### theemathas commented on Oct 28, 2026

Image 38: @theemathas theemathas on Oct 28, 2026 Last edited by theemathas Contributor More actions I found a way to cause UB in safe code with this bug, without using a Drop impl:

undefinedrust
use std::any::Any;

struct Q(Option, Option<&'static String>);

trait Trait {
fn wrong(&mut self);
}

impl Trait for Q {
fn wrong(&mut self) {
let this: &mut Q<&'static String> = (self as &mut dyn Any).downcast_mut().unwrap();
this.1 = this.0;
}
}

fn main() {
let l = String::from("abc");
let mut q = Q(None, None);
q.0 = Some(&l);
(&mut q as &mut dyn Trait).wrong();
let weird: &'static String = q.1.unwrap();
drop(l);
println!("{}", weird);
}
undefined

Another variant:

undefinedrust
struct Q<'a: 'static>(Option<&'a String>, Option<&'static String>);

trait Trait {
fn wrong(&mut self);
}

impl<'a> Trait for Q<'a> {
fn wrong(&mut self) {
self.1 = self.0;
}
}

fn main() {
let l = String::from("abc");
let mut q = Q(None, None);
q.0 = Some(&l);
(&mut q as &mut dyn Trait).wrong();
let weird: &'static String = q.1.unwrap();
drop(l);
println!("{}", weird);
}
undefined

Edit: Simplified a bit

undefinedrust
struct Q<'a: 'static>(Option<&'a String>);

trait Trait {
fn wrong(&self) -> &'static String;
}

impl<'a> Trait for Q<'a> {
fn wrong(&self) -> &'static String {
self.0.unwrap()
}
}

fn main() {
let l = String::from("abc");
let mut q = Q(None);
q.0 = Some(&l);
let weird: &'static String = (&q as &dyn Trait).wrong();
drop(l);
println!("{}", weird);
}
undefined

Sign up for freeto join this conversation on GitHub.Already have an account?Sign in to comment

Metadata

Metadata

Assignees

No one assigned

Labels

A-borrow-checker Area: The borrow checkerArea: The borrow checkerA-destructors Area: Destructors (Drop, …)Area: Destructors (Drop, …)A-lifetimes Area: Lifetimes / regionsArea: Lifetimes / regionsA-variance Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)Area: Variance (https://doc.rust-lang.org/nomicon/subtyping.html)C-bug Category: This is a bug.Category: This is a bug.I-unsound Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-high High priorityHigh priorityT-types Relevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

Footer

© 2026 GitHub,Inc.

Footer navigation

You can’t perform that action at this time.