A tale of a simple Apple kernel bug
The ramdisk device driver in Apple's XNU kernel lacked size checks when copying data between user space and kernel space, allowing an attacker with root and unsandboxed code execution to read and write kernel memory at arbitrary offsets and sizes.
Published on pwning.systems
Earlier this year, I discovered a flaw in XNU, which is the kernel that Apple uses on both macOS and iOS. While it’s not a particularly complicated flaw, I wanted to explain how I discovered it and how it works, both so that I can motivate others and so that they can learn from my discovery.
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 mdevrw function in memdev.c did not validate the size or offset of the data transfer, allowing out-of-bounds read and write.
Impact
An attacker with root and unsandboxed code execution could read and write kernel memory at arbitrary offsets and sizes, potentially leading to kernel code execution or physical memory access, though the high privileges required limit severity.
What the attacker needed
Root privileges; unsandboxed code execution
The fix
The vulnerability was patched by Apple.
How it was found
- Search for calls to uiomove-like functions where the size argument includes a variable named resid.
- Identify the mdevrw function in memdev.c as a candidate.
- Inspect mdevrw and observe that it uses uio_resid as the size without checking it against the buffer size.
- Note that the destination address is calculated using a user-provided offset without bounds checking.
- Trigger the bug by opening the ramdisk device and reading from or writing to it.
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 is the core of an operating system, and it manages memory. A ramdisk is a storage device that lives in memory. The driver for ramdisks did not check how much data was being copied or where it was being placed, so someone with high privileges could read or write memory outside the intended area.
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.