kookie/atomptr
1
0
Fork
You've already forked atomptr
0

Potential Unsoundness: UAF #1

Open
opened 2026年01月21日 08:43:19 +01:00 by yxz · 2 comments

Thanks for your crates. There is a potential unsoundness, which can trigger UB in safe public API.
AtomPtr::compare_exchange returns a Ref<T> that owns the currently stored Box<Arc<T>> even on CAS failure; dropping that Ref and later dropping the AtomPtr frees the same pointer twice (double‐free).

use atomptr::AtomPtr;
fn main() {
 // Create an AtomPtr, take a Ref to the current value.
 let ptr = AtomPtr::new(String::from("a"));
 let prev = ptr.get_ref();
 // Change the stored pointer so `prev` becomes stale.
 let _old = ptr.swap(String::from("b"));
 // This compare_exchange must fail, and returns a Ref that owns
 // the *current* pointer still stored inside AtomPtr.
 let res = ptr.compare_exchange(prev, String::from("c"));
 let current = res.inner();
}

miri outputs:

error: Undefined Behavior: constructing invalid value: encountered a dangling box (use-after-free)
 --> /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1493:9
 |
1493 | Box(unsafe { Unique::new_unchecked(raw) }, alloc)
 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
 |
 = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
 = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
 = note: stack backtrace:
 0: std::boxed::Box::<std::sync::Arc<std::string::String>>::from_raw_in
 at /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1493:9: 1493:58
 1: std::boxed::Box::<std::sync::Arc<std::string::String>>::from_raw
 at /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1266:18: 1266:48
 2: <atomptr::AtomPtr<std::string::String> as std::ops::Drop>::drop
 at /Users/yxz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/atomptr-3.2.0/src/pointer.rs:30:31: 30:49
 3: std::ptr::drop_in_place::<atomptr::AtomPtr<std::string::String>> - shim(Some(atomptr::AtomPtr<std::string::String>))
 at /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:805:1: 807:25
 4: main
 at src/main.rs:15:1: 15:2
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
error: aborting due to 1 previous error; 1 warning emitted
Thanks for your crates. There is a potential unsoundness, which can trigger UB in safe public API. `AtomPtr::compare_exchange` returns a `Ref<T>` that owns the currently stored `Box<Arc<T>>` even on CAS failure; dropping that Ref and later dropping the AtomPtr frees the same pointer twice (double‐free). ``` use atomptr::AtomPtr; fn main() { // Create an AtomPtr, take a Ref to the current value. let ptr = AtomPtr::new(String::from("a")); let prev = ptr.get_ref(); // Change the stored pointer so `prev` becomes stale. let _old = ptr.swap(String::from("b")); // This compare_exchange must fail, and returns a Ref that owns // the *current* pointer still stored inside AtomPtr. let res = ptr.compare_exchange(prev, String::from("c")); let current = res.inner(); } ``` miri outputs: ``` error: Undefined Behavior: constructing invalid value: encountered a dangling box (use-after-free) --> /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1493:9 | 1493 | Box(unsafe { Unique::new_unchecked(raw) }, alloc) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: stack backtrace: 0: std::boxed::Box::<std::sync::Arc<std::string::String>>::from_raw_in at /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1493:9: 1493:58 1: std::boxed::Box::<std::sync::Arc<std::string::String>>::from_raw at /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1266:18: 1266:48 2: <atomptr::AtomPtr<std::string::String> as std::ops::Drop>::drop at /Users/yxz/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/atomptr-3.2.0/src/pointer.rs:30:31: 30:49 3: std::ptr::drop_in_place::<atomptr::AtomPtr<std::string::String>> - shim(Some(atomptr::AtomPtr<std::string::String>)) at /Users/yxz/.rustup/toolchains/nightly-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:805:1: 807:25 4: main at src/main.rs:15:1: 15:2 note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace error: aborting due to 1 previous error; 1 warning emitted ```

Hey there, thank you so much for opening this issue! I'm able to reproduce the problem and I'm gonna have a think about how to best fix this.

I'm kinda unhappy with the way the API has grown in the last 2 major version bumps (although this unsoundness existed in 1.0 as well—I thought I had fixed it 😭). There's a lot of trade-offs to make between convenience and safety.

I'm at fosdem right now and busy with some other projects but I'll try to figure out a fix in the next week or so. I hope this isn't blocking you from doing anything important 🙊

Hey there, thank you so much for opening this issue! I'm able to reproduce the problem and I'm gonna have a think about how to best fix this. I'm kinda unhappy with the way the API has grown in the last 2 major version bumps (although this unsoundness existed in 1.0 as well—I thought I had fixed it 😭). There's a lot of trade-offs to make between convenience and safety. I'm at fosdem right now and busy with some other projects but I'll try to figure out a fix in the next week or so. I hope this isn't blocking you from doing anything important 🙊
Author
Copy link

No worries. I've been conducting a security audit of the cratesio repository recently and detected this issue through my static analysis tool, which is why I'm raising this issue. Thank you very much for your maintenance efforts. :)

No worries. I've been conducting a security audit of the cratesio repository recently and detected this issue through my static analysis tool, which is why I'm raising this issue. Thank you very much for your maintenance efforts. :)
Sign in to join this conversation.
No Branch/Tag specified
main
release/2.0.0
Labels
Clear labels
Compat/Breaking
Breaking change that won't be backward compatible
Kind/Bug
Something is not working
Kind/Correctness
This is correctness/ undefined behaviour issue
Kind/Documentation
Documentation changes
Kind/Enhancement
Improve existing functionality
Kind/Feature
New functionality
Kind/Testing
Issue or pull request related to testing
Priority
Critical
The priority is critical
Priority
High
The priority is high
Priority
Low
The priority is low
Priority
Medium
The priority is medium
Reviewed
Confirmed
Issue has been confirmed
Reviewed
Duplicate
This issue or pull request already exists
Reviewed
Invalid
Invalid issue
Reviewed
Won't Fix
This issue won't be fixed
Status
Abandoned
Somebody has started to work on this but abandoned work
Status
Blocked
Something is blocking this issue or pull request
Status
Need More Info
Feedback is required to reproduce issue or to continue work
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
kookie/atomptr#1
Reference in a new issue
kookie/atomptr
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?