GHSA-cq8v-f236-94qc: Rand is unsound with a custom logger using rand::rng()
It has been reported (by @lopopolo) that the rand library is unsound (i.e. that safe code using the public API can cause Undefined Behaviour) when all the following conditions are met: - The log and threadrng features are enabled - A custom logger is defined - The custom logger accesses rand::rng() (previously rand::threadrng()) and calls any TryRng (previously RngCore) methods on ThreadRng - The ThreadRng (attempts…
Published Apr 14, 2026 · Updated Apr 22, 2026
Affected packages
rand
- Ecosystem
- Rust (crates.io)
- Affected
- = 0.10.0
- Fixed in
- 0.10.1
- Check
cargo tree -i rand
rand
- Ecosystem
- Rust (crates.io)
- Affected
- >= 0.9.0, < 0.9.3
- Fixed in
- 0.9.3
- Check
cargo tree -i rand
rand
- Ecosystem
- Rust (crates.io)
- Affected
- >= 0.7.0, < 0.8.6
- Fixed in
- 0.8.6
- Check
cargo tree -i rand
Advisory details
From the GitHub Advisory Database (CC BY 4.0).
It has been reported (by @lopopolo) that the rand library is unsound (i.e. that safe code using the public API can cause Undefined Behaviour) when all the following conditions are met:
- The
logandthread_rngfeatures are enabled - A custom logger is defined
- The custom logger accesses
rand::rng()(previouslyrand::thread_rng()) and calls anyTryRng(previouslyRngCore) methods onThreadRng - The
ThreadRng(attempts to) reseed while called from the custom logger (this happens every 64 kB of generated data) - Trace-level logging is enabled or warn-level logging is enabled and the random source (the
getrandomcrate) is unable to provide a new seed
TryRng (previously RngCore) methods for ThreadRng use unsafe code to cast *mut BlockRng<ReseedingCore> to &mut BlockRng<ReseedingCore>. When all the above conditions are met this results in an aliased mutable reference, violating the Stacked Borrows rules. Miri is able to detect this violation in sample code. Since construction of aliased mutable references is Undefined Behaviour, the behaviour of optimized builds is hard to predict.
Credited to simonhollingshead, ShoyuVanilla, nbagnard.
References
How this page is made: the data panel comes from the GitHub Advisory Database, CISA’s KEV catalog, the NVD and FIRST EPSS, and updates twice a day. No analysis has been written for this entry yet. Methodology
