Skip to content
Race condition Write-up Advanced

How I Found a $113,337 AF_ALG Linux Local Privilege Escalation Before Copy Fail

A race between two threads calling sendmsg() on the same AF_ALG socket could leave the socket context in an inconsistent state where the merge flag was set but the last scatter-gather list had zero entries. This led to an out-of-bounds read of a fake scatterlist entry, which the researcher turned into a kernel write primitive and used to overwrite core_pattern for root privilege escalation.

starlabs.sg

Published on starlabs.sg

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 author, on starlabs.sg

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 where a thread sleeping in af_alg_wait_for_wmem() releases the socket lock, so a second thread can allocate a fresh af_alg_tsgl with cur set to zero while ctx->merge remains true from the first thread, making the merge branch compute sg[-1].

Impact

An ordinary user could escalate to root, and because AF_ALG is reachable from inside a container the same flaw could be used to escape a Docker container and obtain root on the host.

What the attacker needed

Unprivileged local user; ability to create AF_ALG sockets; two threads on one opfd; heap spray of same-size objects

How it was found

  1. Set up an AF_ALG skcipher socket and bind an algorithm such as AES-CBC, then set a key and accept an operation socket.
  2. Fill the send buffer so that two threads calling sendmsg() both block waiting for writable memory.
  3. Let the first thread wake and append a short input, leaving the last page partly empty and setting the merge flag.
  4. Have the second thread wake, allocate a new scatter-gather list with cur zero, and fail its copy using an invalid userspace address so the error path leaves the new list as the last entry.
  5. Call sendmsg() again so the merge branch computes sg[-1] and reads a fake scatterlist from the preceding heap object.
  6. Heap-spray same-size objects to control page_link, then use the EFAULT-versus-success oracle to find a mapped destination and shift the write toward core_pattern.

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.

AF_ALG is a way for ordinary programs to ask the Linux kernel to do encryption for them. The kernel keeps a small bookkeeping record of the data a program has sent so far, and two threads sending data at the same time could confuse that record. The confusion let the kernel read a fake memory descriptor and write to a place it should not, which the researcher used to run a program as the root user.

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.

Similar reports

View all

Same bug class and techniques, clearest first.

Race conditions

The lesson behind this report.

Parallel requests reach a state the code assumed could not happen: a coupon used twice, a limit overrun, a check skipped.