-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Possible data race & use-after-free in std::env::var for unix implementation #114949
Comments
@rustbot claim |
😬 I think this happened because
The first one could be made more visible by linting to add an explicit |
Side note: even when fixed this assumes all other read/writes acquire Rust's lock and is potentially unsound if they do not. This has often been discussed with a view to making |
That's #90308. |
That PR did not add a test, did it? Would be good to add one in std/tests/env.rs (those are run in Miri regularly).
We probably don't want 100000 iterations though, that will take forever. 100 should easily be enough.
|
Something like #[test] // miri shouldn't detect any data race in this fn
#[cfg_attr(all(target_family = "wasm", not(target_os = "wasi")), ignore)] // monothreaded platforms
fn test_env_get_set_multithreaded() {
let t1 = std::thread::spawn(|| {
let mut n = 0;
for _ in 0..100 {
let var = std::env::var_os("foo");
n += var.map(|v| v.len()).unwrap_or_default();
}
n
});
let t2 = std::thread::spawn(|| {
for i in 0..100 {
let value = format!("bar{i:03}");
std::env::set_var("foo", &value);
}
});
let n = t1.join().unwrap();
assert!(n % 2 == 0, "the sum of even values should be even");
t2.join().unwrap();
} ? |
Yeah something like that. For Miri it's not really necessary to do this "sum of the lengths" computation, not sure why that would be particularly susceptible to a data race. The one existing concurrency test uses |
Investigating library sources for std::os::env::var/set_var, I have found that the copying of the env variable value is performed not under the lock:
So there is a potential data race with setenv.
I tried this code with miri
And got
Have tried to move
Some(OsStringExt::from_vec(unsafe { CStr::from_ptr(s) }.to_bytes().to_vec()))
under the lock, but still got error with miri.I was not able to trigger the real crash with it yet.
Meta
rustc --version --verbose
:Bug was introduced by this commit: 86974b8 (PR: #93668)
Before that changes, read was under the lock.
The text was updated successfully, but these errors were encountered: