-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Conversation
There was a problem hiding this 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>);
71ba3ac
to
b6f79cd
Compare
There was a problem hiding this 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.
02b77bc
to
8e34ba7
Compare
8e34ba7
to
7b6ccb5
Compare
207eaec
to
aa303bc
Compare
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)
``` 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 ```
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.