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 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
- Build the vulnerable UEFI driver into OVMF firmware.
- Boot the firmware in QEMU with a Debian image.
- Create a new UEFI variable file under /sys/firmware/efi/efivars with the driver's GUID and name.
- Remove the immutable attribute from the variable file.
- Write the correct attribute flags and a large payload to the variable.
- 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.