# GHSA-cq8v-f236-94qc: Rand is unsound with a custom logger using `rand::rng()`

> The rand library causes undefined behavior via aliased mutable references when a custom logger accesses the RNG during reseeding, potentially leading to crashes or code execution.

- **Severity:** Not scored
- **Exploited in the wild:** Not known
- **Login needed:** Not needed
- **Attackable:** Not stated
- **Victim action needed:** Not needed
- **Fix available:** Yes
- **Published:** 9 Apr 2026
- **Also known as:** RUSTSEC-2026-0097

## What is GHSA-cq8v-f236-94qc?

GHSA-cq8v-f236-94qc is a memory corruption vulnerability in rand, published on 9 Apr 2026. 

### Official description

It has been reported (by [@lopopolo](https://github.com/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:

- The `log` and `thread_rng` features are enabled
- A [custom logger](https://docs.rs/log/latest/log/#implementing-a-logger) is defined
- The custom logger accesses `rand::rng()` (previously `rand::thread_rng()`) and calls any `TryRng` (previously `RngCore`) methods on `ThreadRng`
- 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 `getrandom` crate) 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](https://doc.rust-lang.org/stable/nomicon/references.html), the behaviour of optimized builds is hard to predict.

## Which versions are affected by GHSA-cq8v-f236-94qc?

7 open source packages ship an affected version, including rand.


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `rand` | crates.io | From 0.10.0 | 0.10.1 |
| `rand` | crates.io | From 0.10.0 | 0.10.1 |
| `rand` | crates.io | From 0.7.0 | 0.8.6, 0.9.3, 0.10.1 |
| `rand` | crates.io | From 0.7.0 | 0.8.6 |
| `rand` | crates.io | From 0.7.0 | 0.8.6 |
| `rand` | crates.io | From 0.9.0 | 0.9.3 |
| `rand` | crates.io | From 0.9.0 | 0.9.3 |

## How do I fix GHSA-cq8v-f236-94qc?

Upgrade rand to a fixed version for your release line: 0.10.1, 0.8.6 or 0.9.3.

## Is GHSA-cq8v-f236-94qc being exploited?

There is no known exploitation of GHSA-cq8v-f236-94qc in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-cq8v-f236-94qc and fix it if it is affected.

What it is: GHSA-cq8v-f236-94qc is a memory corruption vulnerability in rand, published on 9 Apr 2026. 
Exploitation: There is no known exploitation of GHSA-cq8v-f236-94qc in the wild and no public exploit code.

Affected packages and fixed versions:
- rand (crates.io): affected from 0.10.0; fixed in 0.10.1
- rand (crates.io): affected from 0.10.0; fixed in 0.10.1
- rand (crates.io): affected from 0.7.0; fixed in 0.8.6, 0.9.3, 0.10.1
- rand (crates.io): affected from 0.7.0; fixed in 0.8.6
- rand (crates.io): affected from 0.7.0; fixed in 0.8.6
- rand (crates.io): affected from 0.9.0; fixed in 0.9.3
- rand (crates.io): affected from 0.9.0; fixed in 0.9.3

Task:
1. Search the dependency manifests and lockfiles (for example package.json, package-lock.json, requirements.txt, poetry.lock, uv.lock, pom.xml, build.gradle, go.mod, Cargo.lock, Gemfile.lock) and any Dockerfiles for the affected packages, including transitive dependencies.
2. If an affected version is in use, upgrade it to the fixed version for its release line with the project's package manager, so the lockfile is regenerated. Prefer the smallest upgrade that fixes it.
3. If there is no fix, or the upgrade would break the build, do not guess: explain the risk and the safest workaround instead.
4. Run the tests and summarise what changed and why.
5. If nothing in the project is affected, say so and change nothing.

Reference: https://insights.sbomify.com/GHSA-cq8v-f236-94qc/ (Markdown: https://insights.sbomify.com/GHSA-cq8v-f236-94qc/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-cq8v-f236-94qc

---
From sbomify Insights: https://insights.sbomify.com/GHSA-cq8v-f236-94qc/
Summaries are generated and may contain mistakes; the official description above is the record.
