
Fix the crossbeam-channel Double-Free Race in Drop
RUSTSEC-2025-0024 (CVE-2025-4574, GHSA-pg9f-39pc-qf8g) describes a race in crossbeam-channel’s internal Channel::drop cleanup. Two paths in discard_all_messages could observe a non-null head.block, but only one path swapped the pointer to null. Both cleanup paths could therefore act on the same allocation, leading to a double-free and possible memory corruption.
For applications, the production fix is to use crossbeam-channel 0.5.15 or later and verify the resolved dependency graph. The race is an internal unsafe-ownership bug: application code should not try to patch it by coordinating channel drops itself. To understand or test the defect, model the key invariant explicitly: only the operation that atomically takes ownership of a pointer may free it.
Environment and affected versions
This guide targets Rust 1.82+ (Edition 2021) and crossbeam-channel. The security-relevant package versions are:
- Unaffected:
crossbeam-channel <=0.5.11 - Affected:
0.5.12through0.5.14 - Fixed:
crossbeam-channel >=0.5.15
The advisory says the defect was introduced in 0.5.12 while fixing a memory leak, and fixed in upstream PR #1187. The affected code is internal to the crate; ordinary channel send/receive code does not expose the faulty pointer-management operation directly.
Check the dependency you actually build, including transitive dependencies:
cargo tree -i crossbeam-channel
cargo tree -i crossbeam-channel --locked
If the crate is a direct dependency, require a patched compatible release:
[dependencies]
crossbeam-channel = "0.5.15"
Cargo’s caret requirement permits compatible 0.5.x updates; Cargo.lock determines the resolved version for an application. For libraries, specify a compatible minimum in Cargo.toml, update the lockfile in the consuming application, and verify CI does not build an older vulnerable resolution.
The vulnerable mechanism: observing is not taking ownership
The root cause is not simply “two threads called drop.” The critical distinction is between reading a pointer and atomically removing the pointer from the shared owner.
The advisory describes two paths in discard_all_messages that could read head.block. Only one path swapped the value to null. A path that merely observed a non-null pointer could still attempt to free it, while Channel::drop later encountered the pointer and attempted to free it again.
The following pseudocode illustrates the ownership error. It is not a verbatim copy of the crate’s implementation:
// Illustrative anti-pattern: loading does not claim exclusive ownership.
let block = head.block.load(Ordering::Acquire);
if !block.is_null() {
// Another cleanup path may also have observed this pointer.
unsafe { free_block(block) };
}
// A separate path may swap the same pointer to null and free it;
// Channel::drop may also see it as non-null and free it.
An atomic load makes the read atomic; it does not make the pointed-to allocation exclusively owned by the reader. Nor does a later null store retroactively make an earlier read safe to free. If two cleanup paths both use a non-null observation as permission to deallocate, they can race to destroy one allocation.
The invariant for this kind of raw-pointer cleanup is: a pointer is freed only by the code path that successfully removes it from the shared ownership slot. A load is not that operation. An atomic swap or compare-and-swap can serve as a claim only if the surrounding algorithm’s ordering, lifetime, and reclamation rules are also correct.
Production fix: upgrade and verify resolution
For consumers, use the upstream fix rather than reproducing or locally modifying private crossbeam-channel internals. Update the dependency and lockfile, then check the resolved version:
cargo update -p crossbeam-channel
cargo tree -i crossbeam-channel
cargo test --locked
If cargo update cannot select 0.5.15 or later, inspect version constraints and lockfile changes. A transitive dependency may pin an older range; update that parent dependency or apply a reviewed temporary patch while planning a durable upgrade. Do not assume that changing a top-level declaration changes the version used in a deployed build.
The conceptual corrected ownership operation is a single atomic claim:
// Illustrative invariant, not a drop-in replacement for the crate's internals.
let block = head.block.swap(ptr::null_mut(), Ordering::AcqRel);
if !block.is_null() {
// This path took the pointer out of the shared slot.
unsafe { free_block(block) };
}
Only the path receiving the non-null pointer from the swap may free it. Other paths receive null and do nothing. This sketch explains the ownership principle; it does not establish that a standalone swap is sufficient for every lock-free reclamation design. Correct ordering and protection against concurrent readers still depend on the complete algorithm. The upstream 0.5.15 fix is the appropriate production implementation.
This is an internal memory-safety defect, not a reason to replace crossbeam channels with ad hoc Arc or mutex wrappers. If your application has independent unsafe pointer reclamation, audit that code separately: channel-level mitigation does not prove application-level ownership correct.
Reproducing the failure without relying on luck
A stress test that repeatedly drops channels may fail to trigger the race even when run many times. Scheduling is nondeterministic, allocator behavior can mask symptoms, and a double-free may manifest as a crash much later—or not visibly at all. Do not treat “the test passed” as evidence that an affected version is safe.
A useful reproduction strategy is to isolate the ownership transition in a small model:
- Represent the shared block slot as an atomic pointer or an equivalent abstract state.
- Let two cleanup actors interleave their read, claim, and free decisions.
- Assert that each allocation is freed at most once.
- Compare the vulnerable transition (a path can free based on a load) with the corrected transition (a path must successfully take the pointer).
A model of the vulnerable logic should permit this sequence:
cleanup A: load slot -> block
cleanup B: load slot -> block
cleanup A: free block
cleanup B or Channel::drop: free block again
The corrected ownership protocol should instead produce one successful non-null take and one or more null observations:
cleanup A: swap slot with null -> block; free block
cleanup B: swap slot with null -> null; do not free
This is a model of the advisory’s mechanism, not a claim that those exact instructions are the crate’s complete implementation. A test of a simplified model can validate the invariant, but it cannot substitute for reviewing or upgrading the actual crate.
Trade-offs and verification
| Approach | Version or operation | Deallocation ownership | Complexity | Security assessment |
|---|---|---|---|---|
| Keep affected dependency | 0.5.12–0.5.14 | A read path may free without taking the pointer | Cleanup is linear in discarded messages; race is schedule-dependent | Vulnerable; do not deploy |
| Upgrade to patched crate | >=0.5.15 | Uses upstream fix for the reported race | No application-level algorithm change | Recommended mitigation |
| Model the invariant | Load-based versus take-based ownership | Test asserts at most one successful owner | Small deterministic state space is preferable | Useful regression evidence; not a substitute for upgrade |
| Add a stress-only test | Repeated drop under concurrency | Depends on timing and allocator behavior | Potentially long and nondeterministic | Insufficient as a security fix |
The table’s complexity statement describes cleanup work in terms of discarded messages, not a performance benchmark. No timing or allocator measurements are implied: the security decision is based on the advisory’s affected and patched versions, not on a workload-specific speed comparison.
Use dependency auditing and locked CI builds to prevent regressions:
cargo install cargo-audit
cargo audit
cargo tree -i crossbeam-channel --locked
cargo test --locked
cargo clippy --all-targets --all-features -- -D warnings
cargo audit checks advisory data against the dependency graph; it does not prove a custom lock-free algorithm correct. cargo clippy is useful for general code quality but cannot establish that a raw pointer is freed exactly once. For independent unsafe code, add targeted concurrency models or deterministic synchronization tests, and consider cargo miri test for applicable undefined-behavior checks. Miri is not a general race detector and should not be presented as proof that this particular concurrent interleaving is impossible.
When reproducing from source, compare the affected and patched crate implementations rather than relying on a test that happens to crash. Review the change associated with upstream PR #1187, then confirm your build resolves to the fixed release. For broader unsafe-code context, see Reproduce Rust Borrow Checker Unsoundness in Covariant Fields. If your application also handles attacker-controlled binary structures, Validate Rust Arrow Run-End Arrays to Prevent Out-of-Bounds Reads covers a separate class of memory-safety failure.
Production pitfalls and edge cases
A semver declaration is not a deployed version. A library can declare a compatible minimum, while an application’s lockfile still selects an older version under its constraints. Inspect the lockfile used by release CI and deployment builds.
Multiple copies can appear in the graph. Different dependency branches may resolve to different crossbeam-channel versions. cargo tree -i crossbeam-channel helps identify each reverse dependency; ensure every resolved copy is patched.
A crash-free stress test is weak evidence. Double-free behavior is allocator- and schedule-dependent. A passing test cannot make a vulnerable crate safe, and a sanitizer run that does not reproduce the race cannot clear the affected version.
Do not copy an illustrative atomic snippet into production cleanup. Atomic pointer exchange addresses the specific “observe without claiming” mistake, but lock-free reclamation also has to account for concurrent readers and object lifetime. Use the maintained upstream fix unless you are responsible for a separately reviewed unsafe algorithm.
Keep verification reproducible. Commit the intended lockfile update for applications, use --locked in CI, and include dependency auditing in the release workflow. If the advisory database changes, rerun the audit rather than relying on a one-time result.
Frequently Asked Questions (FAQ)
Which crossbeam-channel versions are affected by RUSTSEC-2025-0024?
The advisory marks versions 0.5.12 through 0.5.14 as affected; versions <=0.5.11 are listed as unaffected, and the fix is published in 0.5.15. The issue is also identified as CVE-2025-4574 and GHSA-pg9f-39pc-qf8g. Upgrade to 0.5.15 or later and check the resolved dependency graph.
Why can an atomic pointer load still cause a double-free?
An atomic load makes observing the pointer atomic, but it does not remove the pointer from shared ownership or grant exclusive authority to free it. Two cleanup paths can both observe the same non-null value and act on the same allocation. A safe ownership protocol must ensure that only the path that successfully claims or removes the pointer may deallocate it.
Can cargo audit or Miri prove the race is fixed?
cargo audit can report whether the resolved crate version matches a known advisory, so it is useful for confirming the dependency upgrade. Miri can detect some classes of undefined behavior in supported executions, but a passing run does not prove that a nondeterministic concurrent race cannot occur. Verify the patched version and use targeted model-based testing for any separate unsafe reclamation code.