# GHSA-4q83-7cq4-p6wg: `tokio::io::ReadHalf<T>::unsplit` is Unsound

> A soundness bug in Tokio's ReadHalf::unsplit can cause use-after-free if specific conditions are met, potentially allowing 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:** 11 Jan 2023
- **Also known as:** RUSTSEC-2023-0005

## What is GHSA-4q83-7cq4-p6wg?

GHSA-4q83-7cq4-p6wg is an use after free vulnerability in tokio, published on 11 Jan 2023. 

### Official description

`tokio::io::ReadHalf<T>::unsplit` can violate the `Pin` contract

The soundness issue is described in the [tokio/issues#5372](https://github.com/tokio-rs/tokio/issues/5372)

Specific set of conditions needed to trigger an issue (a !Unpin type in ReadHalf)
is unusual, combined with the difficulty of making any arbitrary use-after-free
exploitable in Rust without doing a lot of careful alignment of data types in
the surrounding code.

The `tokio` feature `io-util` is also required to be enabled to trigger this
soundness issue.

Thanks to zachs18 reporting the issue to Tokio team responsibly and taiki-e
and carllerche appropriately responding and fixing the soundness bug.

Tokio before 0.2.0 used `futures` 0.1 that did not have `Pin`, so it is not
affected by this issue.

## Which versions are affected by GHSA-4q83-7cq4-p6wg?

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


| Package | Ecosystem | Affected | Fixed in |
| --- | --- | --- | --- |
| `tokio` | crates.io | From 0.2.0 | 1.18.5, 1.20.4, 1.24.2 |
| `tokio` | crates.io | From 0.2.0 | 1.18.5 |
| `tokio` | crates.io | From 0.2.0 | 1.18.5 |
| `tokio` | crates.io | From 1.19.0 | 1.20.4 |
| `tokio` | crates.io | From 1.19.0 | 1.20.4 |
| `tokio` | crates.io | From 1.21.0 | 1.24.2 |
| `tokio` | crates.io | From 1.21.0 | 1.24.2 |

## How do I fix GHSA-4q83-7cq4-p6wg?

Upgrade tokio to a fixed version for your release line: 1.18.5, 1.20.4 or 1.24.2.

## Is GHSA-4q83-7cq4-p6wg being exploited?

There is no known exploitation of GHSA-4q83-7cq4-p6wg in the wild and no public exploit code.

## Fix prompt for AI coding agents

```text
Check this codebase for GHSA-4q83-7cq4-p6wg and fix it if it is affected.

What it is: GHSA-4q83-7cq4-p6wg is an use after free vulnerability in tokio, published on 11 Jan 2023. 
Exploitation: There is no known exploitation of GHSA-4q83-7cq4-p6wg in the wild and no public exploit code.

Affected packages and fixed versions:
- tokio (crates.io): affected from 0.2.0; fixed in 1.18.5, 1.20.4, 1.24.2
- tokio (crates.io): affected from 0.2.0; fixed in 1.18.5
- tokio (crates.io): affected from 0.2.0; fixed in 1.18.5
- tokio (crates.io): affected from 1.19.0; fixed in 1.20.4
- tokio (crates.io): affected from 1.19.0; fixed in 1.20.4
- tokio (crates.io): affected from 1.21.0; fixed in 1.24.2
- tokio (crates.io): affected from 1.21.0; fixed in 1.24.2

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-4q83-7cq4-p6wg/ (Markdown: https://insights.sbomify.com/GHSA-4q83-7cq4-p6wg/index.md)
```

## Sources

- OSV: https://osv.dev/vulnerability/GHSA-4q83-7cq4-p6wg

---
From sbomify Insights: https://insights.sbomify.com/GHSA-4q83-7cq4-p6wg/
Summaries are generated and may contain mistakes; the official description above is the record.
