Skip to content
Memory corruption Write-up Advanced

Voucher_swap: Exploiting MIG reference counting in iOS 12

A reference counting flaw in XNU's task_swap_mach_voucher() allowed leaking or dropping references on Mach vouchers, leading to a use-after-free. An attacker could exploit this to build a fake kernel task port and gain arbitrary kernel read/write on iOS 12.1.2.

googleprojectzero.blogspot.com

Published on googleprojectzero.blogspot.com

In this post I'll describe how I discovered and exploited CVE-2019-6225, a MIG reference counting vulnerability in XNU's taskswapmachvoucher() function. We'll see how to exploit this bug on iOS 12.1.2 to build a fake kernel task port, giving us the ability to read and write arbitrary kernel memory.

The author, on googleprojectzero.blogspot.com

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 the placeholder implementation of task_swap_mach_voucher() failed to follow MIG ownership semantics for inout parameters, leaking a reference on the old voucher and consuming a reference on the new voucher without owning it.

Impact

An attacker could gain arbitrary kernel read/write, leading to full kernel compromise; the exploit is reliable (over 99% success) when run shortly after a fresh boot, but the limited range of allowed reference count values in iOS 12 reduces reliability otherwise.

What the attacker needed

Ability to send Mach messages; iOS 12.1.2 or earlier; run shortly after fresh boot for reliability

The fix

The vulnerability was fixed in iOS 12.1.3 beta build 16D5032a.

How it was found

  1. Allocate a page of Mach vouchers.
  2. Store a pointer to the target voucher in the thread's ith_voucher field and drop the added reference using the vulnerability.
  3. Deallocate the voucher ports, freeing all the vouchers.
  4. Force zone gc and reallocate the page of freed vouchers with an array of out-of-line ports.
  5. Call thread_get_mach_voucher() to retrieve a voucher port for the voucher overlapping the out-of-line ports.
  6. Use the vulnerability again to modify the overlapping voucher's iv_refs field, which changes the out-of-line base port pointer so that it points somewhere else instead.

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.

Mach vouchers are kernel objects that track references; a bug in how they were counted let an attacker free a voucher while still using it. By carefully reusing the freed memory, the attacker could trick the kernel into giving them a handle to kernel memory, which they then used to read and write anywhere in the kernel.

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.

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.