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
Hello 🦀 ,
we (Rust group @sslab-gatech) found an undefined-behavior issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.
Sync impl of OnceCell
By exploiting the fact that T has no Send bound,
it is possible to make OnceCell send a non-Send object across thread boundaries.
unsafeimpl<T,B>SyncforOnceCell<T,B>whereT:Sync{}
Proof of Concept
I prepared a small example that sends std::sync::MutexGuard across thread boundaries using OnceCell.
use conquer_once::OnceCell;use crossbeam_utils::thread;use std::sync::Mutex;fnmain(){let once_cell = OnceCell::uninit();
thread::scope(|s| {
s.spawn(|_| {
once_cell.try_init_once(move || {let mutex_static = Box::leak(Box::new(Mutex::new(0_i32)));// `MutexGuard`is `Sync`, but not `Send`.let mutex_guard = mutex_static.lock().unwrap();let tid = std::thread::current().id();(mutex_guard, tid)}).unwrap();});}).unwrap();ifletSome((smuggled_mutexguard, tid)) = once_cell.into_inner(){// `smuggled_mutexguard` is dropped at the end of its lexical scope.// The parent thread attempt to unlock the Mutex which it did not lock.// // If a thread attempts to unlock a Mutex that it has not locked, it can result in undefined behavior.// (https://github.com/rust-lang/rust/issues/23465#issuecomment-82730326)assert_eq!(tid, std::thread::current().id());}}
Suggested Fix
Adding a Send bound to T as following will allow the compiler to revoke the example program above.
Hello 🦀 ,
we (Rust group @sslab-gatech) found an undefined-behavior issue in this crate while scanning Rust code on crates.io for potential vulnerabilities.
Sync
impl ofOnceCell
By exploiting the fact that
T
has noSend
bound,it is possible to make
OnceCell
send a non-Send object across thread boundaries.Proof of Concept
I prepared a small example that sends
std::sync::MutexGuard
across thread boundaries usingOnceCell
.Suggested Fix
Adding a
Send
bound toT
as following will allow the compiler to revoke the example program above.Thank you for checking out this issue 👍
The text was updated successfully, but these errors were encountered: