Skip to content
Memory corruption Write-up Advanced

Emulating and Exploiting UEFI Firmware

The author deliberately introduced a double GetVariable buffer overflow in a UEFI driver, where a malicious root user can set a large UEFI variable to overflow a stack buffer and gain code execution in firmware. The text demonstrates triggering the overflow from the OS and shows a crash with attacker-controlled registers.

margin.re

Published on margin.re

One major difficulty of doing low-level security research is the lack of a testing and debugging environments. When testing regular userspace programs written in C and other high-level programming languages, there are a plethora of debugging tools like gdb to run, inspect, and modify a running process.

The author, on margin.re

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

How it worked

Root cause

The driver calls GetVariable twice with the same DataSize variable; the first call updates DataSize to the attacker-controlled variable length, and the second call copies that much data into a fixed-size stack buffer without rechecking.

Impact

A malicious root user on the OS could overflow the stack buffer and achieve code execution in the firmware, with no binary protections like stack canaries, ASLR, or NX flags to stop it.

What the attacker needed

Root access on the OS; ability to write UEFI variables; vulnerable firmware driver present

How it was found

  1. Build the vulnerable UEFI driver into OVMF firmware.
  2. Boot the firmware in QEMU with a Debian image.
  3. Create a new UEFI variable file under /sys/firmware/efi/efivars with the driver's GUID and name.
  4. Remove the immutable attribute from the variable file.
  5. Write the correct attribute flags and a large payload to the variable.
  6. Reboot the VM to trigger the overflow.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.

Firmware is the first code a computer runs, before the operating system. UEFI is a standard for this firmware, and it stores settings called variables that the OS can read and write. The author wrote a small firmware driver that reads a variable into a fixed-size buffer twice without checking the size, so a large variable overflows the buffer and lets an attacker control the firmware's execution.

What is Memory corruption?

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

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

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