Skip to content
Memory corruption Write-up Advanced

Starting

A missing bounds check in Parallels Desktop's virtio common configuration handling allowed a guest VM to perform relative reads and writes in the hypervisor's data section. The researcher used this to achieve code execution in the hypervisor and then on the host.

Ben McBride Follow

Published on trenchant.io

A common challenge when approaching a new vulnerability research problem is getting started. This is especially true when there is little prior research and strict time constraints.

Benjamin McBride (@bdmcbri), on trenchant.io

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 13 min read.

Context

Parallels Desktop was announced as a new target in the virtualization category for the Zero Day Initiative's Pwn2Own Vancouver 2021. The researcher notes there had been little prior research on it, and suspected a wide range of issues, so many different approaches would likely succeed. The goal for the contest was to pivot from running code inside a guest virtual machine to executing code on the host by exploiting an unknown vulnerability in the hypervisor code, with the host being a fully updated macOS Big Sur machine.

The author is explicit that the vulnerability and exploit are not particularly interesting and that there is nothing novel there. The write-up focuses on the process and the decisions made along the way.

Setup

The researcher installed the latest version of Parallels Desktop and built an Ubuntu virtual machine. Logs for the VM were found in the parallels.log file on the host, and verbose logging could be turned on with the prlsrvctl program. Parallels also has a host user mode component called prl_vm_app.

Prior research by Reno Robert had found a vulnerability in prl_vm_app by fuzzing the VGA device's port I/O addresses, and previous Pwn2Own exploits in other hypervisors (VMware Workstation and VirtualBox) had also targeted virtual hardware components. That made virtual hardware a reasonable starting point.

A practical constraint shaped the whole effort: the researcher initially only had an old iMac that was incapable of running Big Sur, which would later prove problematic.

Finding it

Night 1: installation and background research

After installing Parallels Desktop and building the Ubuntu VM, the researcher examined the provided utility programs and learned that verbose logging could be enabled with prlsrvctl. The log showed the system loading hypervisor code, though at that point the location of that code on disk was unknown.

Night 2: a simple port I/O fuzzer

The first fuzzer simply wrote random data to random I/O port addresses in the guest, with the devices identified by reading /proc/ioports and running lspci. The hypervisor crashed after fuzzing only a couple of virtual hardware devices. The crashes were not in the prl_vm_app user mode process but in the highly privileged hypervisor code, and Parallels produced a crash report with detailed logs and data dumps. The cause was an ASSERT in the hypervisor. The researcher judged this unlikely to be useful for Pwn2Own, because asserts test for erroneous conditions and halt execution before memory corruption or other exploitable conditions occur. The logs did provide a backtrace with function names and register contents at the point of the ASSERT:

Text
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff00000197740> AbortRaw+0xf0
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000d305f> CmosOutPortFunc+0x81f
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000fb292> IoWriteHandleStatus+0xe2
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000fb2f4> IoWriteHandleStatus+0x144
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000fb2cf> IoWriteHandleStatus+0x11f
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000804b9> VtxLoopIteration+0x7909
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff0000005d1e9> _MonAppCmdHandlerDispatch+0x62c9
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff00000002268> _MonAppCmdHandler+0xd

Parallels also provided a memory dump of the hypervisor code in the form of a Mach-O binary, which was very helpful for the reverse engineering ahead.

Night 3: an MMIO fuzzer and a page fault

The second fuzzer targeted memory mapped I/O: random bytes, words and dwords written to random memory addresses identified by /proc/iomem. The VM crashed quickly again, with more ASSERTs. At this point there were enough ASSERTs from the two fuzzers to question whether the dumb fuzzer approach was viable at all, since the fuzzers kept hitting shallow ASSERT conditions and the program exited.

While wading through the ASSERTs, however, one crash stood out: a page fault in the hypervisor. The page fault handler printed debugging information and then asserted. The researcher reasoned this likely meant some sort of memory corruption vulnerability was resulting in a read or write of unmapped memory, similar to a segmentation fault in a userspace program. After triaging the rest of the crashes, the fuzzer was re-run on the MMIO addresses that crashed. It consistently page faulted, but in different spots with different register contents, which was very encouraging.

Night 4: documentation, reverse engineering and root cause

The crash occurred when fuzzing the virtio VGA device, specifically in the CommonCfg settings MMIO region:

Text
01:00.0 VGA compatible controller [0300]: Red Hat, Inc. Virtio GPU
[1af4:1050] (rev 01) (prog-if 00 [VGA controller])
Subsystem: Parallels, Inc. Virtio GPU [1ab8:0010]
Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoopParErr- Stepping- SERR+ FastB2B- DisINTx+
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort-
<TAbort- <MAbort- >SERR- <PERR- INTxInterrupt: pin A routed to IRQ 31
Region 0: Memory at b0000000 (32-bit, prefetchable) [size=256M]
Region 1: Memory at e2000000 (32-bit, non-prefetchable)
[size=4K]
Region 2: I/O ports at 6000 [size=32]
Region 4: Memory at e2040000 (32-bit, non-prefetchable)
[size=16K]
Expansion ROM at 000c0000 [disabled] [size=128K]
Capabilities: [40] MSI: Enable+ Count=1/1 Maskable+ 64bit+
Address: 00000000fee00000 Data: 402a
Masking: 00000000 Pending: 00000000
Capabilities: [b0] MSI-X: Enable- Count=2 MaskedVector table: BAR=1 offset=00000000
PBA: BAR=1 offset=00000800
Capabilities: [bc] Vendor Specific Information: VirtIO:
DeviceCfg
BAR=4 offset=00003000 size=00001000
Capabilities: [cc] Vendor Specific Information: VirtIO: ISR
BAR=4 offset=00002000 size=00001000
Capabilities: [dc] Vendor Specific Information: VirtIO: Notify
BAR=4 offset=00001000 size=00001000 multiplier=00000000
Capabilities: [f0] Vendor Specific Information: VirtIO: CommonCfg
BAR=4 offset=00000000 size=00001000
Kernel driver in use: prl_vid
Kernel modules: prl_vid

The crash appeared to be in PciVirtIOWriteMM, based on the backtrace in the log file:

Text
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000195435> PciVirtIOWriteMM+0x205
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff000001074f9> EmulWrite+0x309
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000124246> DcSetArg+0xb6
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff0000012a3db> Em_MOV_func+0x3b
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff000001207f1> CpuExecuteEmulation+0x1d1
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000122232> DcExecuteInstruction+0x1852
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000041c18> VtxExecuteOp+0xb8
01-31 19:47:56.774 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff0000007a470> VtxLoopIteration+0x18c0

The vulnerability became obvious after skimming the virtio specification, bisecting the MMIO range that was fuzzed, and reverse engineering PciVirtIOWriteMM. It is in the handling of virtio common configuration. The code stores guest supplied data (EBX) in the "Driver feature select" register, a global variable, without any bounds checking:

The value stored is then used as an index for writing a guest controlled value in the "Driver feature set" register:

After tracking all the uses of the guest controlled global variable, a corresponding relative read primitive was found in the PciVirtIOReadMM function:

From the original on trenchant.io

The value read into R8D is then returned to the guest.

The researcher describes this as a very strong exploitation primitive: a relative read and write in the data section should be winnable. He also notes that a vulnerability like this is normally not good enough to win Pwn2Own, because it is shallow and was found with a very simple and direct fuzzing approach. In his experience another competitor would likely find it, or it would be patched before the contest. Given his time constraints and life circumstances, he decided to roll the dice and see it through, at minimum learning more about Parallels Desktop during exploitation that could be useful for a different bug.

Nights 5 to 7

Personal events intervened: his wife went into labour, they had a baby boy, and one night nothing was done.

Exploitation

Step 1: shellcode in the hypervisor

The next goal was to use the relative read/write primitive to get control of the instruction pointer and then code execution in the hypervisor. The approach was to install a port I/O handler at an unused address range, since the handler could easily be triggered from the guest.

When port I/O (IN or OUT) is performed, an array of struct io_port_handler entries in the __bss is indexed by the address to find the appropriate handler. The structure can be reverse engineered by looking at AssignInPortFunc and AssignOutPortFunc:

Text
struct io_port_handler {
code *in_handler;
void *data_for_in;
code *out_handler;
void *data_for_out;
}

The handler is then executed and the appropriate data pointer is passed as a parameter. The researcher found an unused port I/O address range and wrote a pointer into the array that pointed to a fake struct io_port_handler installed in a data cave at the end of the __bss section. Issuing an IN on the appropriate address triggered execution of the installed handler and gained control of the instruction pointer.

Getting shellcode running was then easy, because there are no memory protections against executing code from the __data or __bss section. The shellcode was written in the same data cave as the fake struct io_port_handler, and the in_handler and out_handler were pointed at the shellcode. A subsequent IN or OUT on the appropriate address gets the shellcode executing.

Step 2: pivoting to the host

With shellcode running in the Parallels hypervisor, the remaining task for Pwn2Own was to pivot to the host and demonstrate full control of the system. The process was straightforward but somewhat painful and laborious:

  1. Map a physical memory page into the hypervisor as readable and writable. After much reverse engineering, a set of functions in the hypervisor that did this was found.
  2. Scan for a target page in the host. Ideally, this would be a page with code in the prl_vm_app that was easily triggered from the guest.
  3. If the target page is found, overwrite it with shellcode that pops calc. Otherwise, go back to step 1.

Step 3: adapting to Big Sur

Up to this point the work had been done on an old iMac. Big Sur, required for Pwn2Own, was not supported on the older hardware, so the researcher borrowed a friend's laptop and started testing. Predictably, the exploit did not work. On Big Sur, Apple introduced a hypervisor API that changed how hypervisors could interact with the host kernel, and almost everything changed.

Parallels no longer controlled the hypervisor in the new API, so the first question was whether the vulnerable code was still present. Most of the virtual device code had been abstracted into a host user mode library called libMonitor.dylib. After some nervous reverse engineering, the researcher was certain the vulnerability was still present, but the exploit had to be changed completely to account for all the exploit mitigations present in a user mode process. The plan was to defeat ASLR, turn the relative read/write primitive into an absolute read/write primitive, and then find a way to get arbitrary code execution.

Step 4: defeating ASLR

Reading at offset six (24 bytes) from the relative read base address gives an address to another object in the __common section. This allows locating the base address of libMonitor.dyld and defeating ASLR.

Step 5: absolute read and write

The port I/O address handler array in the __bss was manipulated again to upgrade to an arbitrary read and write primitive. When port I/O is performed, the pointers in the io_port_handler structure are passed as parameters to the handler functions. By controlling these pointers (passed in R8), an absolute read primitive is obtained using the existing AhciIdpIndexInPortFunc:

And an absolute write primitive using AhciIdpIndexOutPortFunc:

From the original on trenchant.io

Step 6: calling system

With an ASLR defeat, absolute read and write, and the ability to control the instruction pointer, the researcher chose to demonstrate execution control for Pwn2Own by calling:

Text
system("echo \"pwned!\" | open -f")

The address of system is found by reading __la_symbol_ptr::_gettimeofday (libMonitor.dylib + 0x109148), which gives an address in the dynamic linker cache; the system function is found at an offset from this. Finding a good way to turn the absolute write and execution control into the ability to call system with a controlled string took a significant amount of reverse engineering. A good gadget was found in the implementation of the SMBus virtual device to pivot from the memory write to calling a function of choice with a pointer to controlled data as the first argument. The gadget is in smb_write (libMonitor.dylib + 0x30c30):

After pointing pcVar6 to system and writing a command string at pbVar2, all that is needed is to trigger execution of the smb_write function. Successful triggering results in TextEdit displaying the string "pwned!":

The exploit then cleans up and execution continues normally.

Root cause

The mistake is in the handling of virtio common configuration. The "Driver feature select" register is a global variable, and the guest supplied value (EBX) is stored into it without any bounds checking. That value is then used as an index when writing a guest controlled value through the "Driver feature set" register, and the same guest controlled global is used in PciVirtIOReadMM to read a value into R8D that is returned to the guest. The result is a relative read and write primitive in the data section.

On Big Sur the vulnerable code survived the move into the host user mode library libMonitor.dylib, where the exploit had to contend with the exploit mitigations of a user mode process. The primitive itself was strong enough that ASLR could be defeated by reading a pointer at offset six (24 bytes) from the relative read base, and the port I/O handler array in the __bss could be abused to turn the relative primitive into an absolute one. In the original hypervisor context, execution from __data or __bss was possible because there are no memory protections against it.

Impact

A guest can reach the virtio common configuration through the virtual hardware, and the resulting relative read and write in the data section is described as a very strong exploitation primitive. In the contest context the researcher demonstrated host code execution: shellcode in the hypervisor, then a pivot to the host, and on Big Sur a call to system that displayed "pwned!" in TextEdit. The conditions are those of the Pwn2Own target: a fully updated macOS Big Sur host running Parallels Desktop, with the attacker able to run code in the guest. The author is clear that the bug is shallow and that he was surprised it was unknown and did not collide with other competitors, attributing that to the large number of vulnerabilities in Parallels Desktop.

Fix

The report does not describe a patch or a fix. The author spent the remaining weeks before Pwn2Own nervously expecting the vulnerability to be patched; it was unknown and did not collide with any other competitors. The obvious correction, implied by the root cause, is to bounds check the guest supplied value stored in the "Driver feature select" register before it is used as an index for the feature set write and the corresponding read.

Variants to test

  • Fuzz port I/O with random data to random addresses from /proc/ioports, and MMIO with random bytes, words and dwords to addresses from /proc/iomem. A positive result is a page fault in the hypervisor rather than a shallow ASSERT, especially one that reproduces consistently at the same addresses with different register contents.
  • Read the virtio specification alongside the device's PCI capabilities and check every register that stores a guest supplied index or selector. A positive result is a stored value used later as an index or offset without a bounds check.
  • Track every use of a guest controlled global variable after it is written. A positive result is a matching read path, as with PciVirtIOReadMM, that returns data derived from the out of range index to the guest.
  • Inspect the port I/O handler dispatch: the struct io_port_handler array in the __bss, AssignInPortFunc and AssignOutPortFunc. A positive result is an unused port range whose array entry can be redirected to a fake handler in a writable data cave.
  • Look for handler functions that pass structure pointers as parameters, such as AhciIdpIndexInPortFunc and AhciIdpIndexOutPortFunc. A positive result is a controlled pointer turning a relative primitive into an absolute read or write.
  • Look for gadgets that call a function pointer with a controlled first argument, such as smb_write. A positive result is a path from a memory write to calling an arbitrary function with a pointer to controlled data.
  • On newer macOS, check whether virtual device code has moved into a user mode library such as libMonitor.dylib, and re-test the same primitives there against user mode mitigations. A positive result is a stable pointer at a fixed offset from the relative read base that reveals the library base.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 27 min read.

What happened, in plain words

A security researcher took part in a hacking contest called Pwn2Own, run by the Zero Day Initiative, in Vancouver in 2021. In that contest, researchers try to break into software in front of an audience, and the organisers buy the bugs that are demonstrated. One of the categories was virtualisation, and a new target was announced: Parallels Desktop, a program for Mac computers that lets you run another operating system, such as Ubuntu Linux, in a window.

When you run another operating system inside a window like that, the system you are sitting in front of is called the host, and the one running in the window is called the guest. The guest is not really a separate computer: it is a pretend computer, simulated by software on the host. The part of Parallels that does the simulating is called the hypervisor. The contest task was to start with the ability to run ordinary code inside the guest, then find a bug in Parallels and use it to run code on the host instead. That jump from guest to host is called a VM escape, and it is serious because the guest is supposed to be a sealed box.

The researcher is upfront that the bug itself is not clever or new. What the write-up is really about is the process: how he got started, how he found the bug with very simple tools, and how he turned it into a working attack. He also had a very human constraint: his second child was about to be born, so he had only short evenings to work.

Getting set up

The first job was simply to install the latest Parallels Desktop and create an Ubuntu virtual machine. Two things came out of that evening.

First, Parallels writes a log file on the host called parallels.log. A log file is a diary that a program keeps of what it did, and it is gold for anyone trying to understand a program, because it shows internal names and events. The researcher also found that he could make the log more detailed by turning on verbose logging with a small command-line program called prlsrvctl.

Second, Parallels has a host program called prl_vm_app that runs in user mode. User mode is the ordinary, restricted way programs run: they cannot touch the deepest parts of the machine directly. The hypervisor, by contrast, runs with far more power. The distinction matters throughout the story, because a bug in the hypervisor is much more dangerous than a bug in an ordinary program.

Why start with the virtual hardware? Because of prior work. Another researcher, Reno Robert, had found a bug in prl_vm_app by fuzzing the VGA device's port I/O addresses. Fuzzing means feeding a program lots of random or semi-random input and watching for crashes. The VGA device is the pretend graphics card inside the guest. Previous Pwn2Own exploits against other virtualisation products, VMware Workstation and VirtualBox, had also gone after the pretend hardware. So pretend hardware looked like a sensible place to begin.

There was also a practical problem that shaped everything: the researcher only had an old iMac, and that machine could not run Big Sur, the version of macOS the contest required. That would come back later.

Night 1: installation and background research

After installing Parallels and building the Ubuntu VM, he looked at the utility programs that ship with Parallels and learned about prlsrvctl and verbose logging. Reading the log told him the system was loading hypervisor code, but not where that code lived on disk. Knowing that a piece of code exists but not where it is stored is a common starting point in this kind of work: the next step is to find it.

Night 2: a simple port I/O fuzzer

With only a couple of hours, he wrote the simplest possible fuzzer. It wrote random data to random I/O port addresses inside the guest.

An I/O port is an old but still used way for a computer's processor to talk to hardware. Each device listens on a small range of numbered ports, and the processor sends values to a port (an OUT instruction) or reads values from it (an IN instruction). In a virtual machine, the guest's IN and OUT instructions are intercepted by the hypervisor, which pretends to be the hardware. That interception point is exactly where a bug can hide.

To know which port numbers to poke, the fuzzer read /proc/ioports, a file in Linux that lists the port ranges in use, and ran the lspci command, which lists the devices attached to the PCI bus (the standard way a computer discovers its hardware).

The result was surprising: after fuzzing only a couple of virtual devices, the hypervisor crashed. Not prl_vm_app, the ordinary user mode program, but the highly privileged hypervisor code. Parallels helpfully produced a crash report with detailed logs and data dumps.

The cause was an ASSERT. An assert is a check a programmer puts in code that says "this should never happen"; if it does happen, the program stops immediately on purpose. The researcher judged this unlikely to be useful for the contest, because an assert stops execution before any memory corruption can occur. In other words, the program catches itself before anything dangerous happens.

The logs did give a backtrace, which is a list of the function calls that led to the crash, with function names and the contents of the processor's registers at that moment. Registers are the tiny, very fast storage slots inside the processor.

Text
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff00000197740> AbortRaw+0xf0
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000d305f> CmosOutPortFunc+0x81f
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000fb292> IoWriteHandleStatus+0xe2
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000fb2f4> IoWriteHandleStatus+0x144
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000fb2cf> IoWriteHandleStatus+0x11f
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff000000804b9> VtxLoopIteration+0x7909
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff0000005d1e9> _MonAppCmdHandlerDispatch+0x62c9
01-27 15:03:52.943 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff00000002268> _MonAppCmdHandler+0xd

Line by line, in plain words:

  • Each line starts with a timestamp (01-27 15:03:52.943), a severity letter (F, for fatal), and the name of the logging component (/MonitorBacktraces), followed by some internal identifiers.
  • The part in angle brackets, such as <fffff00000197740>, is a memory address: where in the hypervisor's memory that function was running.
  • The name after it is the function, for example AbortRaw, and the +0xf0 means "240 bytes into that function". So the crash happened inside AbortRaw, which is the routine that stops the program when an assert fires.
  • Reading upwards from the bottom gives the chain of calls: _MonAppCmdHandler called _MonAppCmdHandlerDispatch, which called VtxLoopIteration, and so on, down to CmosOutPortFunc, which is the function that handles writes to the CMOS port (CMOS is the small chip that stores a computer's basic settings, including the clock).
  • The top line, AbortRaw, is where execution actually stopped.

Even better, Parallels produced a memory dump of the hypervisor code as a Mach-O binary. Mach-O is the file format macOS uses for programs. A memory dump is a copy of what was in memory, and having it as a normal program file made the reverse engineering ahead much easier. Reverse engineering means taking a finished program and working out how it works, usually by reading its instructions in a tool such as Ghidra or IDA Pro.

Night 3: an MMIO fuzzer and a page fault

The second fuzzer targeted memory mapped I/O, or MMIO. Instead of numbered ports, some devices appear to the processor as ordinary memory addresses: writing to that address is how you talk to the device. The fuzzer wrote random bytes, words and dwords (a byte is 8 bits, a word is 16 bits, a dword is 32 bits) to random memory addresses taken from /proc/iomem, a Linux file that lists memory ranges used by devices.

The VM crashed quickly again, with more asserts. By now there were enough asserts from both fuzzers to make him doubt whether the dumb fuzzer approach would ever work: the fuzzers kept hitting shallow assert conditions and the program simply exited.

But while wading through the asserts, one crash was different: a page fault in the hypervisor. A page fault happens when a program tries to read or write a memory address that is not mapped, meaning there is nothing there for it to touch. The page fault handler printed debugging information and then asserted. The researcher reasoned that this probably meant some kind of memory corruption was causing a read or write of unmapped memory, similar to a segmentation fault in an ordinary program. Memory corruption is when a program writes data where it should not, and it is the root of most serious security bugs.

He quickly triaged the rest of the crashes, meaning he sorted them by how interesting they looked, and then re-ran the fuzzer on the MMIO addresses that had crashed. It consistently page faulted, but in different places with different register contents. That consistency was very encouraging: a bug you can reproduce is a bug you can study.

Night 4: documentation, reverse engineering and root cause

The crash happened when fuzzing the virtio VGA device, specifically in the CommonCfg settings MMIO region. Virtio is a standard, shared set of rules for pretend hardware, so that one guest driver can work with many different hypervisors. The lspci output showed the device and its memory regions:

Text
01:00.0 VGA compatible controller [0300]: Red Hat, Inc. Virtio GPU
[1af4:1050] (rev 01) (prog-if 00 [VGA controller])
Subsystem: Parallels, Inc. Virtio GPU [1ab8:0010]
Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV- VGASnoopParErr- Stepping- SERR+ FastB2B- DisINTx+
Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort-
<TAbort- <MAbort- >SERR- <PERR- INTxInterrupt: pin A routed to IRQ 31
Region 0: Memory at b0000000 (32-bit, prefetchable) [size=256M]
Region 1: Memory at e2000000 (32-bit, non-prefetchable)
[size=4K]
Region 2: I/O ports at 6000 [size=32]
Region 4: Memory at e2040000 (32-bit, non-prefetchable)
[size=16K]
Expansion ROM at 000c0000 [disabled] [size=128K]
Capabilities: [40] MSI: Enable+ Count=1/1 Maskable+ 64bit+
Address: 00000000fee00000 Data: 402a
Masking: 00000000 Pending: 00000000
Capabilities: [b0] MSI-X: Enable- Count=2 MaskedVector table: BAR=1 offset=00000000
PBA: BAR=1 offset=00000800
Capabilities: [bc] Vendor Specific Information: VirtIO:
DeviceCfg
BAR=4 offset=00003000 size=00001000
Capabilities: [cc] Vendor Specific Information: VirtIO: ISR
BAR=4 offset=00002000 size=00001000
Capabilities: [dc] Vendor Specific Information: VirtIO: Notify
BAR=4 offset=00001000 size=00001000 multiplier=00000000
Capabilities: [f0] Vendor Specific Information: VirtIO: CommonCfg
BAR=4 offset=00000000 size=00001000
Kernel driver in use: prl_vid
Kernel modules: prl_vid

Line by line, in plain words:

  • The first line identifies the device: address 01:00.0 on the PCI bus, a VGA compatible controller, made by Red Hat, model Virtio GPU, with vendor and device identifiers [1af4:1050].
  • Subsystem: Parallels, Inc. Virtio GPU [1ab8:0010] says Parallels supplies this virtual device.
  • Control and Status lines are flags describing how the device behaves on the bus. The plus and minus signs mean a feature is on or off.
  • Region 0, Region 1, Region 2 and Region 4 are the device's BARs, or Base Address Registers: the memory or port ranges the device answers on. For example, Region 2: I/O ports at 6000 [size=32] means the device listens on 32 I/O ports starting at 6000, and Region 4 is a 16K memory range at e2040000.
  • The Capabilities entries describe optional features. The interesting ones are the Vendor Specific Information: VirtIO: entries, which say where each virtio structure lives. CommonCfg is at BAR=4 offset=00000000 size=00001000, meaning the common configuration block sits at the start of Region 4 and is 4096 bytes long. That is the region the fuzzer hit.
  • Kernel driver in use: prl_vid names the driver the guest uses for the device.

The crash appeared to be in a function called PciVirtIOWriteMM, according to the log:

Text
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000195435> PciVirtIOWriteMM+0x205
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff000001074f9> EmulWrite+0x309
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000124246> DcSetArg+0xb6
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff0000012a3db> Em_MOV_func+0x3b
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff000001207f1> CpuExecuteEmulation+0x1d1
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000122232> DcExecuteInstruction+0x1852
01-31 19:47:56.774 F /MonitorBacktraces:2021:18adf/ mon.stack:
<fffff00000041c18> VtxExecuteOp+0xb8
01-31 19:47:56.774 F /MonitorBacktraces:6739:358e1/ mon.stack:
<fffff0000007a470> VtxLoopIteration+0x18c0

Line by line, in plain words:

  • The format is the same as before: timestamp, severity, component, then an address and a function name with an offset.
  • The top line is where the crash happened: PciVirtIOWriteMM, the function that handles a write to the virtio device's memory mapped registers.
  • Below it, EmulWrite suggests the hypervisor was emulating a write instruction from the guest, and Em_MOV_func confirms it was emulating a MOV instruction, the processor instruction that moves data around.
  • CpuExecuteEmulation, DcExecuteInstruction, VtxExecuteOp and VtxLoopIteration are the layers of the hypervisor's main loop that decode and run the guest's instructions.
  • In short: the guest executed an instruction that wrote to the virtio device, the hypervisor intercepted it, and the handling code crashed.

With the crash located, the researcher grabbed the virtio specification, the official document describing how virtio devices must behave, and started reading. The bug became obvious after skimming the specification, bisecting the MMIO range (narrowing down which addresses triggered the crash by testing halves of the range), and reverse engineering PciVirtIOWriteMM.

The bug is in the handling of the virtio common configuration. The code stores a value supplied by the guest (held in the register EBX) into the "Driver feature select" register, which is a global variable, without any bounds checking. A global variable is a named storage slot that any part of the program can read and write. Bounds checking means checking that a number is within an allowed range before using it. Here, the guest can put any number it likes into that slot.

The image shows the code that stores the guest controlled value in the "Driver feature select" register without bounds checking.

That stored value is then used as an index when writing a guest controlled value through the "Driver feature set" register. An index is a position number, like the number of a drawer in a cabinet. If the program uses the guest's number to pick a drawer without checking that the drawer exists, the guest can make it write into drawers it should never touch.

After tracking every use of that guest controlled global variable, a matching relative read was found in the PciVirtIOReadMM function:

From the original on trenchant.io

The image shows the read path, where the value read into the register R8D is returned to the guest.

A relative read means the attacker can read memory at a position measured from some starting point, rather than at an address of their choosing. The value read into R8D is then handed back to the guest, so the guest can see whatever was at that position.

The researcher calls this a very strong exploitation primitive. A primitive is a small, reliable capability that an attacker builds on, like a single Lego brick. A relative read and write in the data section, the part of the program's memory that holds its variables, should be enough to win.

He is also honest that a bug like this is normally not good enough to win Pwn2Own. It is shallow, meaning it sits close to the surface and was found with a very simple, direct fuzzing approach. In his experience another competitor would likely find it, or it would be patched before the contest. Given his time constraints and his family situation, he decided to take the risk and see it through, reasoning that at worst he would learn more about Parallels that could help with a different bug.

Nights 5 to 7

Life intervened. His wife went into labour, they had a baby boy, and one night nothing was done at all.

Exploitation, step 1: shellcode in the hypervisor

The next goal was to use the relative read and write to take control of the instruction pointer, the processor register that says which instruction to run next, and from there run his own code in the hypervisor. His own code, written in machine instructions rather than a normal program, is called shellcode.

His plan was to install a port I/O handler at an unused address range, because a handler can be triggered easily from the guest: the guest just does an IN or OUT on that port.

When port I/O happens, an array of struct io_port_handler entries in the __bss is indexed by the address to find the right handler. The __bss is the part of a program's memory that holds variables which start out as zero. A struct is a bundle of related values stored together. He worked out the layout of the structure by looking at AssignInPortFunc and AssignOutPortFunc, the functions that fill in the array:

Text
struct io_port_handler {
code *in_handler;
void *data_for_in;
code *out_handler;
void *data_for_out;
}

Line by line, in plain words:

  • struct io_port_handler { starts the definition of a bundle named io_port_handler.
  • code *in_handler; is a pointer to the function to run when the guest reads from (IN) this port. A pointer is a value that holds a memory address, like a note saying where something lives.
  • void *data_for_in; is a pointer to some data that will be handed to that function.
  • code *out_handler; and void *data_for_out; are the same pair for writes (OUT).
  • } closes the bundle.

When the guest does an IN or OUT, the hypervisor looks up the entry for that port, runs the handler, and passes the matching data pointer as a parameter. The researcher found an unused port range and wrote a pointer into the array that pointed to a fake struct io_port_handler he had placed in a data cave at the end of the __bss. A data cave is a stretch of memory that is not used for anything important, so it is a safe place to stash your own data. Issuing an IN on the right address then ran the handler he had installed, and he had control of the instruction pointer.

Getting shellcode running was then easy, because there are no memory protections against executing code from the __data or __bss section. Normally modern systems mark data memory as non-executable, so that even if an attacker writes there, the processor refuses to run it. Here that protection was absent. He wrote the shellcode in the same data cave as the fake structure and pointed in_handler and out_handler at it. The next IN or OUT on that address ran the shellcode.

Exploitation, step 2: pivoting to the host

With shellcode running in the Parallels hypervisor, the remaining contest task was to pivot to the host and show full control of the system. Pivoting means moving from the foothold you have to the place you actually want to be. The process was straightforward but painful and laborious:

  1. Map a physical memory page into the hypervisor as readable and writable. A page is a fixed-size chunk of memory, and mapping it means making it visible at a chosen address. After much reverse engineering, he found a set of functions in the hypervisor that did this for him.
  2. Scan for a target page in the host. Ideally this would be a page containing code in prl_vm_app that is easily triggered from the guest, so that the guest can cause that code to run on demand.
  3. If the target page is found, overwrite it with shellcode that pops calc, meaning it opens the calculator app as a harmless visible proof. Otherwise, go back to step 1.

Exploitation, step 3: adapting to Big Sur

All of this had been done on an old iMac. Big Sur, required for the contest, was not supported on that hardware, so he borrowed a friend's laptop and started testing. Predictably, the exploit did not work.

On Big Sur, Apple introduced a hypervisor API that changed how hypervisors could interact with the host kernel. An API is a defined set of functions one piece of software offers to another, like a restaurant menu: you can order only what is listed. The kernel is the core of the operating system. Almost everything changed.

Parallels no longer controlled the hypervisor under the new API, so the first question was whether the vulnerable code was still there at all. Most of the virtual device code had been moved into a host user mode library called libMonitor.dylib. A library is a file of shared code that programs load and use, and .dylib is the macOS file extension for one. After some nervous reverse engineering, he was certain the bug was still present, but the exploit had to be rebuilt completely to deal with all the exploit mitigations present in a user mode process. Mitigations are defences built into the operating system and programs that make bugs harder to turn into working attacks.

His plan was: first defeat ASLR, then turn the relative read and write into an absolute read and write, then find a way to run arbitrary code.

Exploitation, step 4: defeating ASLR

ASLR, Address Space Layout Randomization, is a defence that shuffles the locations of programs and libraries in memory each time they start. If an attacker does not know where anything is, a bug that writes to a guessed address is much less useful.

Reading at offset six (24 bytes) from the relative read base address gives an address of another object in the __common section. An offset is a distance from a starting point, so "offset six" here means six units of 4 bytes, which is 24 bytes. Because that pointer sits at a fixed distance from the library, reading it reveals where libMonitor.dyld is loaded, and that defeats ASLR.

Exploitation, step 5: absolute read and write

He manipulated the port I/O handler array in the __bss again, this time to upgrade the relative primitive into an arbitrary read and write, meaning he could read and write any address he chose. When port I/O happens, the pointers inside the io_port_handler structure are passed as parameters to the handler functions. By controlling those pointers, which arrive in the register R8, he could get an absolute read using an existing function, AhciIdpIndexInPortFunc:

The image shows the read primitive built on AhciIdpIndexInPortFunc.

And an absolute write using AhciIdpIndexOutPortFunc:

From the original on trenchant.io

The image shows the write primitive built on AhciIdpIndexOutPortFunc.

Exploitation, step 6: calling system

With an ASLR defeat, absolute read and write, and control of the instruction pointer, he chose a simple demonstration of execution control for the contest: calling

Text
system("echo \"pwned!\" | open -f")

Line by line, in plain words:

  • system(...) is a standard C library function that asks the operating system to run a command line.
  • echo \"pwned!\" prints the text pwned!. The backslashes are there because the quotes are inside a quoted string and have to be escaped.
  • The | symbol is a pipe: it takes the output of the command on its left and feeds it to the command on its right.
  • open -f is a macOS command that opens the piped text in a new document in TextEdit.
  • So the whole line makes TextEdit appear with the word pwned! in it, a visible, harmless proof that code of his choosing ran on the host.

The address of system is found by reading __la_symbol_ptr::_gettimeofday (libMonitor.dylib + 0x109148). __la_symbol_ptr is a table of pointers to functions in other libraries, filled in when the program starts, and _gettimeofday is one of those functions. Reading that entry gives an address inside the dynamic linker cache, a shared store of system libraries, and system sits at a known offset from it.

Turning the absolute write and instruction pointer control into a call to system with a string he controlled took a significant amount of reverse engineering. He found a good gadget in the implementation of the SMBus virtual device. A gadget is a short existing sequence of instructions that, used cleverly, does something useful for the attacker. This one pivots from the memory write to calling a function of his choosing, with a pointer to data he controls as the first argument. It is in smb_write (libMonitor.dylib + 0x30c30):

The image shows the gadget inside smb_write.

After pointing pcVar6 to system and writing a command string at pbVar2, all that is needed is to trigger the smb_write function. If it works, TextEdit displays the string "pwned!":

The image shows the result: TextEdit displaying "pwned!".

The exploit then cleans up and execution continues normally, so the machine does not crash.

Root cause

The mistake is in the handling of virtio common configuration. The "Driver feature select" register is a global variable, and the guest supplied value (EBX) is stored into it without any bounds checking. That value is then used as an index when writing a guest controlled value through the "Driver feature set" register, and the same guest controlled global is used in PciVirtIOReadMM to read a value into R8D that is returned to the guest. The result is a relative read and write primitive in the data section.

On Big Sur the vulnerable code survived the move into the host user mode library libMonitor.dylib, where the exploit had to deal with the mitigations of a user mode process. The primitive was strong enough that ASLR could be defeated by reading a pointer at offset six (24 bytes) from the relative read base, and the port I/O handler array in the __bss could be abused to turn the relative primitive into an absolute one. In the original hypervisor context, execution from __data or __bss was possible because there are no memory protections against it.

Impact

A guest can reach the virtio common configuration through the virtual hardware, and the resulting relative read and write in the data section is described as a very strong exploitation primitive. In the contest the researcher demonstrated host code execution: shellcode in the hypervisor, then a pivot to the host, and on Big Sur a call to system that displayed "pwned!" in TextEdit. The conditions are those of the Pwn2Own target: a fully updated macOS Big Sur host running Parallels Desktop, with the attacker able to run code in the guest. The author is clear that the bug is shallow and that he was surprised it was unknown and did not collide with other competitors, attributing that to the large number of vulnerabilities in Parallels Desktop.

Fix

The report does not describe a patch or a fix. The author spent the remaining weeks before Pwn2Own nervously expecting the vulnerability to be patched; it was unknown and did not collide with any other competitors. The obvious correction, implied by the root cause, is to bounds check the guest supplied value stored in the "Driver feature select" register before it is used as an index for the feature set write and the corresponding read.

Variants to test

  • Fuzz port I/O with random data to random addresses from /proc/ioports, and MMIO with random bytes, words and dwords to addresses from /proc/iomem. A positive result is a page fault in the hypervisor rather than a shallow assert, especially one that reproduces consistently at the same addresses with different register contents.
  • Read the virtio specification alongside the device's PCI capabilities and check every register that stores a guest supplied index or selector. A positive result is a stored value used later as an index or offset without a bounds check.
  • Track every use of a guest controlled global variable after it is written. A positive result is a matching read path, as with PciVirtIOReadMM, that returns data derived from the out of range index to the guest.
  • Inspect the port I/O handler dispatch: the struct io_port_handler array in the __bss, AssignInPortFunc and AssignOutPortFunc. A positive result is an unused port range whose array entry can be redirected to a fake handler in a writable data cave.
  • Look for handler functions that pass structure pointers as parameters, such as AhciIdpIndexInPortFunc and AhciIdpIndexOutPortFunc. A positive result is a controlled pointer turning a relative primitive into an absolute read or write.
  • Look for gadgets that call a function pointer with a controlled first argument, such as smb_write. A positive result is a path from a memory write to calling an arbitrary function with a pointer to controlled data.
  • On newer macOS, check whether virtual device code has moved into a user mode library such as libMonitor.dylib, and re-test the same primitives there against user mode mitigations. A positive result is a stable pointer at a fixed offset from the relative read base that reveals the library base.

Conclusions

The researcher spent the remaining weeks before Pwn2Own nervously expecting the bug to be patched. Thankfully it was unknown and did not collide with any other competitor, meaning nobody else submitted the same bug. He was surprised, and thinks it was probably because Parallels Desktop has so many vulnerabilities that diving deep on any interface was likely a winning strategy for other competitors too. He does not expect such shallow bugs to be present in the future.

He enjoyed the whole process and was reminded of a few things:

  • Just try. Not every Pwn2Own target needs a novel technique. Sometimes simple techniques work. Many evenings it is easier to read other people's research, but you will not find vulnerabilities by reading blog posts. You find them by doing your own research.
  • Reading previous work and specifications is helpful, but each person has to balance that against hunting for new bugs.
  • When approaching a new target, start with simple but useful things: open the software in Ghidra or IDA Pro, work out how the target logs data, or write a simple fuzzer. The point is to get momentum and insight. Save complex approaches for later, when or if they are needed and you know more.
  • Make a plan for each chunk of focused time, however small. For example: for the next thirty minutes I will try to understand how port I/O dispatching works. Not everyone can spend days on a target without distraction, and small chunks of purposeful time make a difference.
  • You do not need to understand everything to make progress. Focus on what you know and grow your understanding methodically.
  • A good vulnerability simplifies exploitation even when strong mitigations are in place.
  • After looking at a target, do a post-mortem reflection on what worked and what did not. Did you miss vulnerabilities other researchers found? Why? What will you do differently next time? This only works if you really try.

Glossary

  • Host: the real computer you are sitting in front of, which runs the virtual machine software.
  • Guest: the operating system running inside a virtual machine, which believes it is a real computer.
  • Hypervisor: the software that creates and runs virtual machines by simulating a computer's hardware.
  • VM escape: an attack that breaks out of a guest virtual machine and runs code on the host.
  • Pwn2Own: a hacking contest run by the Zero Day Initiative where researchers demonstrate exploits against real products.
  • Zero Day Initiative: the organisation that runs Pwn2Own and buys security vulnerabilities.
  • User mode: the restricted way ordinary programs run, without direct access to the deepest parts of the machine.
  • Kernel: the core of an operating system, with the highest privileges.
  • Log file: a diary a program keeps of what it did, useful for understanding its behaviour.
  • Verbose logging: a setting that makes a program record much more detail than usual.
  • Fuzzing: feeding a program lots of random or semi-random input and watching for crashes.
  • I/O port: a numbered channel a processor uses to talk to hardware, written with OUT and read with IN.
  • MMIO (memory mapped I/O): a way for devices to appear as ordinary memory addresses, so writing to the address talks to the device.
  • PCI bus: the standard way a computer discovers and talks to attached hardware.
  • VGA device: a pretend or real graphics card.
  • Virtio: a shared standard describing how virtual hardware should behave, so one driver works with many hypervisors.
  • BAR (Base Address Register): a record of the memory or port range a device answers on.
  • ASSERT: a check in code that stops the program on purpose when something that should never happen does happen.
  • Backtrace: a list of the function calls that led to a crash, with names and addresses.
  • Register: a tiny, very fast storage slot inside the processor.
  • Mach-O binary: the file format macOS uses for programs.
  • Memory dump: a copy of what was in memory at a moment in time.
  • Reverse engineering: working out how a finished program works, usually by reading its instructions in a tool such as Ghidra or IDA Pro.
  • Page fault: an error raised when a program tries to read or write memory that is not mapped.
  • Segmentation fault: the equivalent error in an ordinary program, caused by touching memory it should not.
  • Memory corruption: a bug where a program writes data where it should not, often the root of serious security issues.
  • Global variable: a named storage slot any part of a program can read and write.
  • Bounds checking: checking that a number is within an allowed range before using it.
  • Index: a position number used to pick an item, such as a drawer number.
  • Relative read / relative write: reading or writing memory at a position measured from a starting point, rather than at an address of the attacker's choosing.
  • Absolute read / write (arbitrary read and write): reading or writing any memory address the attacker chooses.
  • Exploitation primitive: a small, reliable capability an attacker builds on.
  • Data section (__data): the part of a program's memory that holds its variables.
  • __bss: the part of a program's memory holding variables that start out as zero.
  • __common: a section of memory holding shared objects, used here to find a library's base address.
  • Instruction pointer: the processor register that says which instruction to run next.
  • Shellcode: small machine-code instructions an attacker runs to take control.
  • Struct: a bundle of related values stored together in memory.
  • Pointer: a value that holds a memory address.
  • Data cave: an unused stretch of memory used to stash an attacker's own data.
  • Page: a fixed-size chunk of memory.
  • Mapping: making a chunk of memory visible at a chosen address.
  • API: a defined set of functions one piece of software offers to another.
  • Library: a file of shared code that programs load and use.
  • Exploit mitigation: a defence built into an operating system or program that makes bugs harder to exploit.
  • ASLR (Address Space Layout Randomization): a defence that shuffles where programs and libraries sit in memory each time they start.
  • Offset: a distance from a starting point.
  • Dynamic linker cache: a shared store of system libraries on macOS.
  • Gadget: a short existing sequence of instructions that, used cleverly, does something useful for an attacker.
  • SMBus: a small hardware bus used for low-speed system management communication.
  • Pipe (|): a shell feature that feeds the output of one command into another.
  • TextEdit: the built-in macOS text editor.
  • Post-mortem: a review after the fact of what worked and what did not.

More from Parallels

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.