
Audit Tokio Broadcast Clone Bounds and Synchronization Safely
Tokio’s broadcast channel had an unsoundness: it required values to implement Send, but cloned stored values while they were shared internally. A Send type is not necessarily Sync; its Clone implementation may access non-thread-safe interior state. Concurrent receives could therefore invoke Clone through shared references in a way that violates Rust’s safety guarantees. Upgrade Tokio to a release patched for your version line, audit the resolved dependency graph, and test custom Send + !Sync values if your code depends on broadcast.
The key distinction is that Send permits moving a value between threads, while Sync permits sharing references to it between threads. Requiring only Send is insufficient when a channel may clone one stored value concurrently through shared access. The advisory is RUSTSEC-2025-0023, also identified as GHSA-rr8g-9fpq-6wmg.
Environment and baseline
This guide targets Rust 1.82+ (Edition 2021) and Tokio 1.x. The affected component is Tokio’s broadcast channel, not all Tokio channels. The advisory identifies patched releases in these version lines:
1.38.2and later, but before1.39.01.42.1and later, but before1.43.01.43.1and later, but before1.44.01.44.2and later
The advisory lists Tokio versions below 0.2.5 as unaffected. Do not infer that every version omitted from the patched ranges is safe: check the advisory and your resolved version, especially if you use a less common release line. Prefer a currently maintained patched Tokio release compatible with your application.
A typical dependency declaration might look like:
[dependencies]
tokio = { version = "1.44.2", features = ["sync", "rt-multi-thread", "macros"] }
That version requirement does not alone prove that the build uses the expected version. Cargo resolves a dependency graph, and another package or lockfile can affect the result. Check Cargo.lock and the output of cargo tree.
The vulnerable mechanism: cloning shared values
A broadcast sender retains values so that multiple subscribers can receive them. Receiving a value involves cloning it; consequently, the type’s Clone implementation is part of the channel’s concurrency boundary. In affected versions, the channel required T: Send but did not require T: Sync, despite cloning a stored value through shared internal access.
Here is a deliberately hazardous type that illustrates why Send alone does not make concurrent shared access safe:
use std::cell::Cell;
struct CloneCounter(Cell<u32>);
impl Clone for CloneCounter {
fn clone(&self) -> Self {
let next = self.0.get() + 1;
self.0.set(next);
Self(Cell::new(next))
}
}
// Cell<u32> is Send but not Sync. CloneCounter is therefore
// Send but not Sync, provided it has no other non-Send fields.
Cell permits mutation through &self, but it is not safe for unsynchronized concurrent access. The Clone implementation is valid when called under ordinary single-threaded conditions. The problem is the context: if a channel allows its stored CloneCounter to be accessed concurrently through shared references and invokes clone, those interior mutations can race. Safe channel use must not make such an implementation capable of undefined behavior.
The relevant trait meanings are:
Send: ownership of a value can move to another thread.Sync: shared references to a value can safely be used across threads.
Neither trait promises that a custom Clone implementation is harmless under concurrent shared access. This is why treating Clone as inherently safe for concurrent use is a faulty assumption.
Production fix: upgrade and audit the resolved crate
The primary mitigation is to upgrade Tokio to a release patched for the affected line. Do not attempt to repair Tokio’s internal synchronization by wrapping application values in a mutex or by changing only the application’s type bounds: neither approach corrects an unsound implementation in the dependency.
Check the actual resolved version:
cargo tree -i tokio
cargo tree -p your_crate
Then scan the dependency graph for known advisories:
cargo install cargo-audit
cargo audit
If Tokio is transitive, update the package that brings it in, or use a compatible dependency update that causes Cargo to resolve a patched Tokio version. Review the lockfile diff and confirm the exact version rather than assuming a manifest constraint forced the intended upgrade:
cargo update -p tokio
cargo tree -i tokio
For a library, declare a compatible Tokio version range that excludes known affected releases where practical, and test against the versions you claim to support. A downstream application’s lockfile remains important: library consumers may resolve different versions within the declared range.
Application code can still enforce a stronger local contract when its design requires thread-safe values:
fn make_broadcast<T>(capacity: usize) -> tokio::sync::broadcast::Sender<T>
where
T: Clone + Send + Sync + 'static,
{
let (sender, _receiver) = tokio::sync::broadcast::channel(capacity);
sender
}
This helper communicates a conservative requirement, but it is not a substitute for upgrading Tokio. Also, Sync on T does not prove every possible Clone implementation is logically well-behaved; the dependency must uphold its own safety invariants. For background on using marker types to express API contracts, see Advanced Rust: Leveraging the Type System with PhantomData. For the broader boundary between safe callers and unsafe library invariants, see Advanced Rust: Mastering Unsafe Code for Performance and Control.
Why requiring T: Sync is not the whole audit
A T: Sync bound rules out the specific Send-but-not-Sync category described by this advisory. It is a meaningful API-level defense when shared references cross threads. But soundness review should begin with the implementation: determine where values are stored, which references are shared, and whether cloning can happen concurrently. A trait bound is only effective if it matches those operations.
Likewise, a wrapper can change the trait properties of a payload. Arc<Mutex<T>>, for example, can provide synchronized access to T when used correctly, but that does not make an affected Tokio release safe for arbitrary T. Fix the vulnerable dependency first; use synchronization wrappers only when they fit the application’s ownership and mutation requirements.
Verification workflow
First, establish which Tokio version is actually built and whether the advisory is reported:
cargo tree -i tokio
cargo audit
Then run the test suite with the project’s normal feature set and target configuration:
cargo test --all-targets --all-features
cargo clippy --all-targets --all-features -- -D warnings
These checks catch dependency and application regressions; they do not prove the old implementation sound or reproduce undefined behavior deterministically. A sanitizer or stress test is not a replacement for the patched crate.
If your project has a regression test around broadcast delivery, exercise multiple receivers and verify the intended message semantics. For example:
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn broadcast_delivers_to_each_receiver() {
let (sender, mut first) = tokio::sync::broadcast::channel(8);
let mut second = sender.subscribe();
sender.send(String::from("event")).unwrap();
assert_eq!(first.recv().await.unwrap(), "event");
assert_eq!(second.recv().await.unwrap(), "event");
}
This checks ordinary behavior, not the unsoundness itself. Keep any test payloads safe: do not use an affected Tokio version with a custom Send + !Sync type whose Clone mutates unsynchronized interior state merely to try to trigger a race.
For low-level unsafe code you own, cargo miri test can help detect some classes of invalid memory access and data races in supported executions. It cannot establish that a dependency is free of all undefined behavior, and it may not reproduce a concurrent race. Use it as one part of review, alongside advisory checks and source inspection. For API design around payload types, Rust Newtype Pattern: Building Stronger APIs with Type-Safe Wrappers can help make intended constraints explicit.
Quantitative trade-offs
The practical cost of the fix is usually dependency maintenance, not per-message application work. Adding a trait bound is generally a compile-time compatibility change; upgrading Tokio may also include unrelated fixes or behavior changes, so test the application’s supported feature set.
| Approach | Runtime cost | Build/API impact | Safety assessment |
|---|---|---|---|
Keep affected Tokio and use Send payloads | No immediate measured overhead | None | Unsafe for the advisory’s Send + !Sync scenario |
| Upgrade to an advisory-patched Tokio release | No advisory-specific per-message penalty established | Dependency update; run compatibility tests | Primary mitigation |
Add an application-level T: Sync bound | No runtime cost from the bound itself | Rejects !Sync payloads in that API | Defense in depth; does not patch Tokio |
| Wrap payloads with synchronization | Locking may add contention and latency | Changes ownership and access patterns | Not a substitute for upgrading |
Do not publish fabricated throughput or gas-style figures for this issue: the advisory describes a soundness failure, not a quantified performance regression. Benchmark only if your upgrade changes a measured hot path, using your workload and the exact old and new resolved dependency versions.
Production pitfalls and edge cases
- A manifest is not a resolution report. A broad
version = "1"requirement can select a vulnerable version from a lockfile or constrained dependency graph. Verifycargo tree -i tokio. - Transitive Tokio copies matter. A workspace can contain multiple Tokio versions. Inspect every occurrence and determine which one is used by the affected code.
- Features do not remove the underlying concern. Disabling an application feature is not proof that a compiled Tokio broadcast implementation is safe. Confirm the dependency version and the code path.
Cloneis user code. Audit whether cloned values useCell,UnsafeCell, raw pointers, or custom interior mutability. A type beingSenddoes not imply that cloning shared references is safe.- Do not equate a clean test run with soundness. Undefined behavior may be timing-dependent and may not fail visibly. The reliable first step is using a patched release.
- Retest public compatibility. A stricter trait bound in an API, whether from a dependency upgrade or your own wrapper, can make previously accepted payload types fail to compile. Treat that as an explicit compatibility change.
Frequently Asked Questions (FAQ)
What versions fix RUSTSEC-2025-0023?
The advisory lists patched releases including Tokio 1.38.2, 1.42.1, 1.43.1, and 1.44.2 for their respective version lines. Use the advisory’s exact ranges and verify the resolved version with cargo tree -i tokio; do not rely only on the version written in Cargo.toml.
Why is Send not enough for Tokio broadcast values?
Send allows a value to move between threads, but it does not allow shared references to be used across threads. Broadcast receivers may cause the channel to clone a stored value while it is internally shared, so a Clone implementation that mutates non-thread-safe interior state can be unsafe under concurrent access.
Can I fix RUSTSEC-2025-0023 by requiring T: Sync?
Requiring T: Sync in your own broadcast-related API can prevent Send + !Sync payloads from being accepted there. It does not repair an affected Tokio implementation or replace upgrading to a patched release; treat the bound as defense in depth.