Skip to content

sync: fix cloning value when receiving from broadcast channel #7232

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

Merged
merged 3 commits into from
Apr 3, 2025

Conversation

carllerche
Copy link
Member

@carllerche carllerche commented Mar 31, 2025

The broadcast channel does not require values to implement Sync, yet it calls the .clone() method without synchronizing. This is unsound logic. This patch adds per-value synchronization on receive to handle this case. It is unlikely any usage of the broadcast channel is currently at risk of the unsoundness issue as it requires accessing a !Sync type during .clone(), which would be very unusual when using the broadcast channel.

This is RUSTSEC-2025-0023. It has been fixed as of 1.44.2, with backports to 1.38.2, 1.42.1, and 1.43.1.

Thanks to @abonander for reporting this issue.

@carllerche carllerche added C-bug Category: This is a bug. A-tokio Area: The main tokio crate M-sync Module: tokio/sync I-unsound 💥 A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness R-loom-sync Run loom sync tests on this PR labels Mar 31, 2025
@carllerche carllerche requested a review from Copilot March 31, 2025 16:48
@carllerche carllerche marked this pull request as ready for review March 31, 2025 16:48
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes an unsound cloning behavior when receiving from the broadcast channel by introducing per-value synchronization. Key changes include:

  • Switching from RwLock to Mutex for protecting buffer slots and related operations.
  • Removing direct UnsafeCell usage for value storage in Slot.
  • Introducing a WaiterCell wrapper around UnsafeCell to encapsulate unsafe Send/Sync implementations.
Comments suppressed due to low confidence (1)

tokio/src/sync/broadcast.rs:401

  • [nitpick] Consider renaming 'WaiterCell' to a name that more clearly reflects its role in isolating unsafe implementations from 'Recv'.
struct WaiterCell(UnsafeCell<Waiter>);

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix itself LGTM. A few nits on CI changes.

The broadcast channel does not require values to implement `Sync` yet it calls
the `.clone()` method without synchronizing. This is unsound logic. This patch
adds per-value synchronization on receive to handle this case. It is unlikely
any usage of the broadcast channel is currently at risk of the unsoundeness
issue as it requires accessing a `!Sync` type during `.clone()`, which would be
very unusual when using the broadcast channel.
@carllerche carllerche force-pushed the fix-broadcast-recv branch from 02b77bc to 8e34ba7 Compare April 2, 2025 21:25
@carllerche carllerche force-pushed the fix-broadcast-recv branch from 8e34ba7 to 7b6ccb5 Compare April 2, 2025 21:34
@carllerche carllerche force-pushed the fix-broadcast-recv branch from 207eaec to aa303bc Compare April 3, 2025 04:58
@carllerche carllerche merged commit aa303bc into tokio-1.38.x Apr 3, 2025
76 checks passed
@carllerche carllerche deleted the fix-broadcast-recv branch April 3, 2025 17:39
kodiakhq bot pushed a commit to pdylanross/fatigue that referenced this pull request Apr 7, 2025
Bumps tokio from 1.44.1 to 1.44.2.

Release notes
Sourced from tokio's releases.

Tokio v1.44.2
This release fixes a soundness issue in the broadcast channel. The channel
accepts values that are Send but !Sync. Previously, the channel called
clone() on these values without synchronizing. This release fixes the channel
by synchronizing calls to .clone() (Thanks Austin Bonander for finding and
reporting the issue).
Fixed

sync: synchronize clone() call in broadcast channel (#7232)

#7232: tokio-rs/tokio#7232



Commits

ec4b1d7 chore: forward port 1.43.x
e3c3a56 Merge branch 'tokio-1.43.x' into forward-port-1.43.x
a7b658c chore: prepare Tokio v1.43.1 release
c1c8d10 Merge remote-tracking branch 'origin/tokio-1.38.x' into forward-port-1.38.x
aa303bc chore: prepare Tokio v1.38.2 release
7b6ccb5 chore: backport CI fixes
4b174ce sync: fix cloning value when receiving from broadcast channel
See full diff in compare view




Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting @dependabot rebase.


Dependabot commands and options

You can trigger Dependabot actions by commenting on this PR:

@dependabot rebase will rebase this PR
@dependabot recreate will recreate this PR, overwriting any edits that have been made to it
@dependabot merge will merge this PR after your CI passes on it
@dependabot squash and merge will squash and merge this PR after your CI passes on it
@dependabot cancel merge will cancel a previously requested merge and block automerging
@dependabot reopen will reopen this PR if it is closed
@dependabot close will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
@dependabot show <dependency name> ignore conditions will show all of the ignore conditions of the specified dependency
@dependabot ignore this major version will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this minor version will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
@dependabot ignore this dependency will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
taiki-e added a commit to taiki-e/assert-unmoved that referenced this pull request Apr 17, 2025
```
error[unsound]: Broadcast channel calls clone in parallel, but does not require `Sync`
   ┌─ /home/runner/work/assert-unmoved/assert-unmoved/Cargo.lock:19:1
   │
19 │ tokio 0.2.25 registry+https://github.com/rust-lang/crates.io-index
   │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ unsound advisory detected
   │
   ├ ID: RUSTSEC-2025-0023
   ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2025-0023
   ├ The broadcast channel internally calls `clone` on the stored value when
     receiving it, and only requires `T:Send`. This means that using the broadcast
     channel with values that are `Send` but not `Sync` can trigger unsoundness if
     the `clone` implementation makes use of the value being `!Sync`.

     Thank you to Austin Bonander for finding and reporting this issue.
   ├ Announcement: tokio-rs/tokio#7232
   ├ Solution: Upgrade to >=1.38.2, <1.39.0 OR >=1.42.1, <1.43.0 OR >=1.43.1, <1.44.0 OR >=1.44.2 (try `cargo update -p tokio`)
   ├ tokio v0.2.25
     └── assert-unmoved v0.1.6

error[unsound]: Broadcast channel calls clone in parallel, but does not require `Sync`
   ┌─ /home/runner/work/assert-unmoved/assert-unmoved/Cargo.lock:20:1
   │
20 │ tokio 0.3.7 registry+https://github.com/rust-lang/crates.io-index
   │ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ unsound advisory detected
   │
   ├ ID: RUSTSEC-2025-0023
   ├ Advisory: https://rustsec.org/advisories/RUSTSEC-2025-0023
   ├ The broadcast channel internally calls `clone` on the stored value when
     receiving it, and only requires `T:Send`. This means that using the broadcast
     channel with values that are `Send` but not `Sync` can trigger unsoundness if
     the `clone` implementation makes use of the value being `!Sync`.

     Thank you to Austin Bonander for finding and reporting this issue.
   ├ Announcement: tokio-rs/tokio#7232
   ├ Solution: Upgrade to >=1.38.2, <1.39.0 OR >=1.42.1, <1.43.0 OR >=1.43.1, <1.44.0 OR >=1.44.2 (try `cargo update -p tokio`)
   ├ tokio v0.3.7
     └── assert-unmoved v0.1.6
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. I-unsound 💥 A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundness M-sync Module: tokio/sync R-loom-sync Run loom sync tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy