util/fifo8: vmstate_fifo8 restores unvalidated head and num fields, causing out-of-bounds read in fifo8_pop()
Host environment
-
Operating system: Debian GNU/Linux 12 (bookworm)
-
OS/kernel version:
Linux 6.1.0-51-cloud-amd64 x86_64 -
Architecture:
x86_64 -
QEMU flavor:
qemu-system-x86_64 -
QEMU version: Verified on QEMU
9.0.2,11.1.1, and current upstreammaster(73294d096cc591e122fde9e63db4739de6d84f2b, 2026-10-28) -
QEMU command line:
./qemu-system-x86_64 -machine pc,accel=kvm -m 512M -serial stdio -incoming tcp:127.0.0.1:4444
Emulated/Virtualized environment
- Operating system: Guest-agnostic (device state deserialization / post-migration access)
- OS/kernel version: N/A
- Architecture: x86_64 (
pc/q35with standard 16550Aisa-serialor anyVMSTATE_FIFO8consumer)
Description of problem
Tooling Disclosure (per QEMU bug reporting policy): This issue was identified with the assistance of a static analysis screener checking VMStateDescription deserialization invariants across QEMU device models, and was manually triaged, audited against upstream master (73294d096cc591e122fde9e63db4739de6d84f2b), and paired with a compiler-verified .post_load patch by the reporter prior to submission.
util/fifo8.c defines the shared vmstate_fifo8 migration descriptor used across numerous virtual devices (including hw/char/serial.c via VMSTATE_FIFO8(recv_fifo, SerialState) and VMSTATE_FIFO8(xmit_fifo, SerialState)).
While Fifo8 runtime operations maintain the ring invariant fifo->head < fifo->capacity and fifo->num <= fifo->capacity (anchored by fifo->capacity set at device realization), vmstate_fifo8 deserializes head and num directly from the migration stream as raw uint32_t values (util/fifo8.c:131-133) without a .post_load callback:
const VMStateDescription vmstate_fifo8 = {
.name = "Fifo8",
.version_id = 1,
.minimum_version_id = 1,
.fields = (const VMStateField[]) {
VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
VMSTATE_UINT32(head, Fifo8),
VMSTATE_UINT32(num, Fifo8),
VMSTATE_END_OF_LIST()
}
};Concretely, fifo8_pop() (util/fifo8.c:64-75) indexes fifo->data[fifo->head++] directly before wrapping only on fifo->head == fifo->capacity:
uint8_t fifo8_pop(Fifo8 *fifo)
{
uint8_t ret;
if (fifo->num == 0) {
abort();
}
ret = fifo->data[fifo->head++];
fifo->head %= fifo->capacity;
fifo->num--;
return ret;
}If an incoming migration stream or snapshot supplies fifo->head >= fifo->capacity (with fifo->num > 0), the first post-resume guest read (e.g., an inb(0x3f8) UART RBR read on isa-serial) indexes fifo->data[fifo->head] past the end of the fifo->capacity-byte heap allocation (heap-buffer-overflow read, returning adjacent host heap bytes to the guest). Similarly, if fifo->num > fifo->capacity, fifo8_is_full() (fifo->num == fifo->capacity) evaluates to false, allowing fifo8_push() to trip assert(fifo->num < fifo->capacity).
Steps to reproduce
- Start a QEMU instance with a device embedding
Fifo8(such asisa-serialon-machine pc). - Restore a migration stream or snapshot where
Fifo8.head >= Fifo8.capacityandFifo8.num > 0. - Perform a guest read from the device's receive register (e.g., reading UART RBR
0x3f8), which invokesfifo8_pop()and readsfifo->data[fifo->head]out of bounds.
Additional information
Proposed Fix Patch (01-util-fifo8-vmstate-postload.patch, verified against master 73294d096cc591e122fde9e63db4739de6d84f2b)
--- a/util/fifo8.c
+++ b/util/fifo8.c
@@ -15,6 +15,7 @@
#include "qemu/osdep.h"
#include "migration/vmstate.h"
#include "qemu/fifo8.h"
+#include "qemu/error-report.h"
void fifo8_reset(Fifo8 *fifo)
{
@@ -177,10 +178,31 @@
return fifo->num;
}
+static int fifo8_post_load(void *opaque, int version_id)
+{
+ Fifo8 *fifo = opaque;
+
+ /*
+ * head and num are plain uint32_t fields on the wire. A malicious or
+ * corrupted migration stream must not be able to break the FIFO
+ * invariant (head < capacity, num <= capacity): fifo8_pop() would
+ * otherwise index data[] out of bounds and fifo8_push() would trip
+ * its assert on the next guest access.
+ */
+ if (fifo->head >= fifo->capacity || fifo->num > fifo->capacity) {
+ error_report("fifo8: invalid state from migration stream "
+ "(head=%u num=%u capacity=%u)",
+ fifo->head, fifo->num, fifo->capacity);
+ return -EINVAL;
+ }
+ return 0;
+}
+
const VMStateDescription vmstate_fifo8 = {
.name = "Fifo8",
.version_id = 1,
.minimum_version_id = 1,
+ .post_load = fifo8_post_load,
.fields = (const VMStateField[]) {
VMSTATE_VBUFFER_UINT32(data, Fifo8, 1, NULL, capacity),
VMSTATE_UINT32(head, Fifo8),