{"id":"RUSTSEC-2026-0097","summary":"Rand is unsound with a custom logger using `rand::rng()`","details":"It has been reported (by @lopopolo) that the `rand` library is [unsound](https://rust-lang.github.io/unsafe-code-guidelines/glossary.html#soundness-of-code--of-a-library) (i.e. that safe code using the public API can cause Undefined Behaviour) when all the following conditions are met:\n\n- The `log` and `thread_rng` features are enabled\n- A [custom logger](https://docs.rs/log/latest/log/#implementing-a-logger) is defined\n- The custom logger accesses `rand::rng()` (previously `rand::thread_rng()`) and calls any `TryRng` (previously `RngCore`) methods on `ThreadRng`\n- The `ThreadRng` (attempts to) reseed while called from the custom logger (this happens every 64 kB of generated data)\n- Trace-level logging is enabled or warn-level logging is enabled and the random source (the `getrandom` crate) is unable to provide a new seed\n\n`TryRng` (previously `RngCore`) methods for `ThreadRng` use `unsafe` code to cast `*mut BlockRng\u003cReseedingCore\u003e` to `&mut BlockRng\u003cReseedingCore\u003e`. 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](https://doc.rust-lang.org/stable/nomicon/references.html), the behaviour of optimized builds is hard to predict.\n\nAffected versions of `rand` are `\u003e= 0.7, \u003c 0.9.3` and `0.10.0`.","modified":"2026-04-11T11:45:06.174335Z","published":"2026-04-09T12:00:00Z","database_specific":{"license":"CC0-1.0"},"references":[{"type":"PACKAGE","url":"https://crates.io/crates/rand"},{"type":"ADVISORY","url":"https://rustsec.org/advisories/RUSTSEC-2026-0097.html"},{"type":"WEB","url":"https://github.com/rust-random/rand/pull/1763"}],"affected":[{"package":{"name":"rand","ecosystem":"crates.io","purl":"pkg:cargo/rand"},"ranges":[{"type":"SEMVER","events":[{"introduced":"0.7.0"},{"fixed":"0.9.3"},{"introduced":"0.10.0"},{"fixed":"0.10.1"}]}],"ecosystem_specific":{"affects":{"os":[],"functions":["rand::rng","rand::thread_rng"],"arch":[]},"affected_functions":null},"database_specific":{"cvss":null,"categories":[],"source":"https://github.com/rustsec/advisory-db/blob/osv/crates/RUSTSEC-2026-0097.json","informational":"unsound"}}],"schema_version":"1.7.5"}