CVE-2020-27786 (Race Condition + Use-After-Free)
A race condition in the Linux kernel raw MIDI driver lets a local user free a buffer while another thread is still copying data into it, producing a use-after-free. The author then turns this into a full kernel exploit that leaks the kernel base address and gains control of execution to run a root shell.
ii4gsp.github.io
Published on ii4gsp.github.io
This blog post describes how to exploit a use-after-free vulnerability due to a race condition in MIDI devices in Linux Kernel 5.6.13. This vulnerability is identified as CVE-2020-27786.
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 driver drops its lock around the user-space copy in the write path, so a concurrent buffer resize can free the buffer that the copy is still using.
Impact
A local attacker could escalate to kernel code execution and a root shell, though the exploit relies on userfaultfd, which unprivileged users cannot use from kernel 5.11 onwards.
What the attacker needed
Local access to a raw MIDI device; kernel built with CONFIG_SND_RAWMIDI; userfaultfd or FUSE available; ability to win the race
The fix
The vulnerability was fixed by adding a reference count on the runtime buffer so a resize returns busy while the buffer is in use, and the write and read paths take and release that reference.
How it was found
- Open the raw MIDI device and set a small output buffer size via the params ioctl.
- Start a write whose user buffer ends on a page registered with userfaultfd, so the kernel blocks mid-copy.
- While the write is blocked, issue another params ioctl with a different size to free the old buffer.
- Spray msg_msg objects into the freed slot and then release the page fault so the stale copy overwrites them.
- Read the message back with a larger size to leak a timerfd function pointer and compute the kernel base.
- Repeat the same race to place a ROP chain and fake tty operations table, then trigger ioctl on a pty to pivot the stack.
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 kernel keeps a temporary memory buffer for each MIDI device. One part of the code frees that buffer when its size is changed, while another part is still writing into it, so the write lands in memory that has already been handed back and may now belong to something else. An attacker who can open the MIDI device can time these two actions to overlap and take over the freed memory, which in this write-up leads to running 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.