Skip to content
Memory corruption Write-up Advanced

FreeBSD 11.0-13.0 LPE via aio_aqueue Kernel Refcount Bug

A reference-count bug in FreeBSD's aio_aqueue function meant that an error path took a reference on the calling process's credential but never released it, so repeatedly triggering the error could wrap the 32-bit counter and free a credential still in use. An unprivileged local attacker could then place a fake credential in the freed slot and gain root privileges.

Chris Follow

Published on accessvector.net

The FreeBSD 13.1 kernel patched a bug in the aioaqueue function, which is used to handle the internals of a few of the asynchronous I/O syscalls (aioread, aiowrite, aiofsync, etc.).

Chris (@accessvector), on accessvector.net

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 aqueue_fail error path in aio_aqueue frees the job without calling crfree on the credential reference taken earlier with crhold, so the refcount leaks and can eventually wrap.

Impact

An unprivileged local user could escalate to root; the text notes that in practice the naive fake-credential approach leads to NULL pointer dereferences, so the exploit needs care.

What the attacker needed

Local unprivileged access to the system; FreeBSD 11.0 through 13.0; ability to pin execution to one CPU

The fix

The missing crfree was added to the error path and the fix was backported to stable/12 and merged into supported releng branches, though FreeBSD 11.x was not patched.

How it was found

  1. Call an AIO syscall such as aio_fsync on a file type that is not a regular file or directory, so aio_queue_file returns EOPNOTSUPP and the error path is taken.
  2. Repeat this to increment the credential refcount until it wraps.
  3. Call setuid(getuid()) so the process credential has a known refcount of 2.
  4. Use the wrap to bring the count down and trigger crfree through aio_waitcomplete, freeing the credential.
  5. Allocate a fake credential in the freed slot and call setuid(getuid()) again so the kernel copies the forged identity into a new credential.

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.

Every process in the kernel has a credential record that says who it is (user ID, group ID and so on). The kernel keeps a count of how many things are using that record, and frees it only when the count reaches zero. A mistake in one error path meant the count was raised but never lowered, so by repeating the action many times the count wrapped around and the record was freed while the process still relied on it, letting an attacker swap in their own fake record and become root.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More from FreeBSD Security Team

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.