Linux af_packet.c race condition local root exploit
A race condition in the Linux kernel's packet socket code allowed an unprivileged local user to change the packet version after a timer was initialised, leading to a use-after-free of a timer object. This could be exploited to gain kernel code execution and root privileges.
seclists.org
Published on seclists.org
This is an announcement about CVE-2016-8655 which is a race-condition I found in Linux (net/packet/afpacket.c). It can be exploited to gain kernel code execution from unprivileged processes.
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
The packet version could be changed after a timer was initialised but before the ring buffer setup completed, so the timer was not deleted on close and its memory was freed, creating a use-after-free.
Impact
An unprivileged local attacker could gain kernel code execution and root privileges; the exploit defeats SMEP/SMAP and works on Ubuntu 16.04, and can also be triggered from within containers to compromise the host kernel.
What the attacker needed
Unprivileged user namespaces enabled; CAP_NET_RAW in a network namespace; local access
The fix
The bug was fixed by taking lock_sock(sk) in packet_setsockopt() when changing the packet version and also taking the lock at the start of packet_set_ring().
How it was found
- Create an AF_PACKET socket with TPACKET_V3 version.
- Call setsockopt with PACKET_RX_RING to initialise the ring buffer and timer.
- Race to change the packet version to TPACKET_V1 before packet_set_ring returns.
- Close the socket, causing the timer object to be freed without being deleted.
- Exploit the use-after-free by reallocating the freed memory with controlled data (e.g., using add_key).
- Trigger the timer to execute a manipulated function pointer and gain root.
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.
A race condition is when two actions happen at the same time and the program does not handle the order correctly. Here, an attacker could quickly change a setting on a network socket while the kernel was still setting it up, causing the kernel to later free memory that was still in use. That freed memory could then be reused to run attacker-controlled code with the highest privileges.
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.