-
Notifications
You must be signed in to change notification settings - Fork 7
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
RingBuffer allows sending Non-Send types across threads #1
Comments
Lines 108 to 113 in 966b949
Another data race issue has been found in #![forbid(unsafe_code)]
use std::cell::Cell;
use std::sync::Arc;
use std::thread;
use disrustor::internal::RingBuffer;
use disrustor::DisrustorBuilder;
use disrustor::EventProducer;
// A simple tagged union used to demonstrate problems with data races in Cell.
#[derive(Clone, Copy)]
enum RefOrInt {
Ref(&'static u64),
Int(u64),
}
static STATIC_INT: u64 = 123;
impl Default for RefOrInt {
fn default() -> Self {
RefOrInt::Ref(&STATIC_INT)
}
}
fn main() {
let provider = Arc::new(RingBuffer::<Cell<RefOrInt>>::new(1));
let provider_cloned = provider.clone();
thread::spawn(move || {
let (_executor, producer) = DisrustorBuilder::new(provider_cloned)
.with_spin_wait()
.with_single_producer()
.with_barrier(|_| {})
.build();
producer.write(std::iter::once(()), |slot, _seq, _item| loop {
// Repeatedly write Ref(&addr) and Int(0xdeadbeef) into the cell.
*slot.get_mut() = RefOrInt::Ref(&STATIC_INT);
*slot.get_mut() = RefOrInt::Int(0xdeadbeef);
});
});
let (_executor, producer) = DisrustorBuilder::new(provider.clone())
.with_spin_wait()
.with_single_producer()
.with_barrier(|_| {})
.build();
producer.write(std::iter::once(()), |slot, _seq, _item| {
loop {
if let RefOrInt::Ref(addr) = slot.get() {
// Hope that between the time we pattern match the object as a
// `Ref`, it gets written to by the other thread.
if addr as *const u64 == &STATIC_INT as *const u64 {
continue;
}
println!("Pointer is now: {:p}", addr);
println!("Dereferencing addr will now segfault: {}", *addr);
}
}
});
} Output:
Terminated with signal 11 (SIGSEGV) |
Heads up: this issue has been included in the RustSec advisory database. It will be surfaced by tools such as cargo-audit or cargo-deny from now on. Once a fix is released to crates.io, please open a pull request to update the advisory with the patched version, or file an issue on the advisory database repository. |
Hi everybody, apologies for me getting back to you so late. Really appreciate the effort of finding this issue. For the short term I will try to see if I can unpublish the package from crates.io and put a warning in the README file. It will probably take me a while before I find some time to finish the long overdue refactoring of the API to make it sound. -Sebastian |
This has finally been fixed in version 0.3. The examples have been added as |
This CVE has been fixed in version 0.3. Please see sklose/disrustor#1 for details.
This CVE has been fixed in version 0.3. Please see sklose/disrustor#1 for details.
Hello fellow Rustacean,
we (Rust group @sslab-gatech) are scanning Rust code on crates.io for potential memory safety and soundness bugs and found an issue in this crate which allows safe Rust code to exhibit an undefined behavior.
Issue Description
disrustor/src/ringbuffer.rs
Lines 48 to 49 in 966b949
RingBuffer
implements Send/Sync regardless of whether the internal type implements Send/Sync. This allows users to send a non-Send type across threads withRingBuffer
.RingBuffer
's Send/Sync should probably have a trait bound on the internal typeT
, or ifRingBuffer
needs to implement Send/Sync and it is not meant to be created by the end user,new()
should be hidden withpub(crate)
or so.Reproduction
Below is an example program that sends Non-Send type across threads using safe APIs of
disrustor
.Show Detail
This example just shows that non-Send type is sent across the thread, but it is possible to demonstrate a data race with a more complicated example.
Output:
Tested Environment
static_assertions = { version = "1.1.0" }
The text was updated successfully, but these errors were encountered: