Repository navigation
Closed
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: 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/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.
Description
... which can be used to trigger a UAF in safe code.
rustc 1.26.0 (a77568041 2018-05-07)
binary: rustc
commit-hash: a7756804103447ea4e68a71ccf071e7ad8f7a03e
commit-date: 2018-05-07
host: x86_64-pc-windows-msvc
release: 1.26.0
LLVM version: 6.0
fn main() {
let mut foo = Some("foo".to_string());
let bar = &mut foo;
match bar {
Some(baz) => {
bar.take();
println!("{:?}", baz); // UAF, segfaults or prints garbage
},
None => unreachable!(),
}
}Original repro:
enum CacheFuture {
Get(Option<String>)
}
fn main() {
let mut f = CacheFuture::Get(Some("foo".to_string()));
match &mut f {
CacheFuture::Get(get) => match get {
Some(mod_name) => {
get.take();
println!("{:?}", mod_name); // UAF, segfaults or prints garbage
},
None => unreachable!(),
},
}
}Inserting explicit annotations to disable match ergonomics leads to borrowck correctly complaining about two mutable borrows, which leads me to believe this is only because of match ergonomics.
enum CacheFuture {
Get(Option<String>)
}
fn main() {
let mut f = CacheFuture::Get(Some("foo".to_string()));
match *(&mut f) {
CacheFuture::Get(ref mut get) => match *get {
Some(ref mut mod_name) => {
get.take();
println!("{:?}", mod_name);
},
None => unreachable!(),
},
}
}9 | Some(ref mut mod_name) => {
| ---------------- first mutable borrow occurs here
10 | get.take();
| ^^^ second mutable borrow occurs here
...
14 | },
| - first borrow ends here
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerC-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: 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/SoundnessT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-stablePerformance or correctness regression from one stable version to another.Performance or correctness regression from one stable version to another.
Activity
est31 commentedon May 28, 2018
NLL fixes this. Adding
#![feature(nll)]gives me this error:Arnavion commentedon May 28, 2018
#rustwasn't sure if thisregression-from-stable-to-stable-worthy or not. On the one hand match ergonomics is new in 1.26, so it's not strictly a regression from 1.25 since the bad code would not have compiled there. On the other hand, 1.26 makes it easy to write code that hits this bug, so it would be nice to fix this in 1.26.2.Arnavion commentedon May 29, 2018
Now that 1.26.1 has been released, can someone confirm that this will be fixed in 1.26.2, or at least disable match ergonomics entirely? It isn't feasible to expect users to download a nightly and enable
feature(nll)just to be sure they don't accidentally write code with this bug.novacrazy commentedon May 29, 2018
I for one would love the option to disable match ergonomics for crates. It's been nothing but distracting so far.
emilyalbini commentedon May 30, 2018
cc @Mark-Simulacrum
Mark-Simulacrum commentedon May 30, 2018
I doubt that we will be disabling match ergonomics at this point due to their likely wide use and stabilization. It's possible we'd make another patch release but rather unlikely; we'll see what the results of compiler discussion are (I believe their meeting is Thursday). I do believe we'll backport the fix to beta so that it makes it for 1.27 if we have it by then.
Arnavion commentedon May 30, 2018
I figured compiling code to access garbage memory silently would be more serious than that. I guess users should stay with 1.25 then.
aturon commentedon May 30, 2018
@novacrazy This issue isn't the place to argue about the feature itself (which has already gone through several open comment periods prior to stabilization). I'd encourage you to instead open a thread on https://internals.rust-lang.org/ to talk about your experiences in detail, if you think there's something actionable for the lang team.
3 remaining items
Arnavion commentedon May 30, 2018
@nikomatsakis
let bar = &mut foo; match bar { Some(...)is using match ergonomics. Without match ergonomics you would have had to writematch *baror&mut Some(edit: ... both of which correctly flag the double mutable borrow.)nikomatsakis commentedon May 30, 2018
I see, I see. Yes, of course. That helps! thanks.
nikomatsakis commentedon May 30, 2018
Found the bug. "Implicit" derefs were accidentally being considered distinct from "explicit" derefs, leading borrowck to conclude that there are two distinct paths involved. Fix coming shortly.
Auto merge of #51235 - nikomatsakis:issue-51117-borrowck-implicit-der…
Mark-Simulacrum commentedon May 31, 2018
Core team just discussed this and we've decided to backport the fix and release 1.26.2.
Auto merge of #51258 - Mark-Simulacrum:stable-next, r=nikomatsakis
bstrie commentedon Jun 2, 2018
Can this be closed?
Mark-Simulacrum commentedon Jun 2, 2018
Yes, I think so.
Fixes for rust-lang/rust#51117.
Fixed build on nightly.
pnkfelix commentedon Jun 27, 2019
Since this was fixed on AST-borrowck by #51235, removing NLL-fixed-by-NLL.