I Found a $113,337 Af_alg Linux Local Privilege Escalation Before Copy Fail
A race condition in the Linux kernel's AF_ALG crypto socket code lets two concurrent writers leave the shared context in an inconsistent state where the last scatter-gather list has a zero entry count while the merge flag is still set. This causes an out-of-bounds read of a fake scatterlist entry, which the author turned into a usercopy oracle and arbitrary kernel write, overwriting core_pattern to gain root and escape a Docker container.
idnsec.com
Published on idnsec.com
In 2025, I found an AFALG vulnerability in the Linux kernel that allowed an ordinary user to escalate privileges to root. This is a retrospective on how I found CVE-2025-39964 and how we developed the exploit, before Copy Fail drew wider attention to AFALG in 2026.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely a race between two sendmsg() callers on the same AF_ALG socket: one sets ctx->merge while the other allocates a fresh tsgl with cur equal to zero and then fails on an invalid user address, leaving merge true with an empty final scatterlist.
Impact
An unprivileged local user could escalate to root and escape a Docker container to obtain root on the host, though the exploit relies on heap spraying and an oracle-based search rather than a single deterministic write.
What the attacker needed
Local unprivileged user; ability to open AF_ALG sockets; two threads on one opfd; heap spray of same-size objects
The fix
The upstream patch prevents concurrent writes to the same AF_ALG socket, which was fixed according to the text.
How it was found
- Set up an AF_ALG skcipher socket with a key and an accepted operation fd.
- Arrange the last tsgl to be nearly full, merge false, and the send buffer full.
- Have two threads call sendmsg() so both block waiting for writable memory.
- Let the first thread fill the last entry with a short input, setting merge true.
- Let the second thread allocate a new tsgl (cur zero) and fail its copy with an invalid user address.
- Call sendmsg() again so the merge branch computes sg[-1] and performs the out-of-bounds copy.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
The Linux kernel has a feature that lets ordinary programs ask it to encrypt or decrypt data. Two threads using the same connection at the same time could confuse the kernel's bookkeeping, so it read a piece of memory just before a list it expected to be non-empty. The researcher then used that mistake to write to a kernel setting and become the administrator (root) of the machine.
What is Race condition?
Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.