hw/nvram/fw_cfg: vmstate_fw_cfg restores unvalidated cur_entry, allowing out-of-bounds read in fw_cfg_data_read() and fw_cfg_dma_transfer()

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 upstream master (73294d096cc591e122fde9e63db4739de6d84f2b, 2026-10-28)

  • QEMU command line:

    ./qemu-system-x86_64 -machine q35,accel=kvm -m 512M -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 / arm64 (q35, pc, virt)

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.

During normal guest I/O, fw_cfg_select() (hw/nvram/fw_cfg.c:270-292) validates the 16-bit selector key against fw_cfg_max_entry(s) and sets s->cur_entry = FW_CFG_INVALID whenever (key & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s).

Because fw_cfg_select() enforces this invariant at write time, the runtime read and DMA handlers fw_cfg_data_read() (hw/nvram/fw_cfg.c:294-318) and fw_cfg_dma_transfer() (hw/nvram/fw_cfg.c:363-366) only check s->cur_entry == FW_CFG_INVALID before indexing s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK]:

int arch = !!(s->cur_entry & FW_CFG_ARCH_LOCAL);
FWCfgEntry *e = (s->cur_entry == FW_CFG_INVALID) ? NULL :
                &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK];

However, vmstate_fw_cfg (hw/nvram/fw_cfg.c:680-695) restores s->cur_entry directly via VMSTATE_UINT16(cur_entry, FWCfgState) with no .post_load validator. If a migration stream supplies a cur_entry value where cur_entry != FW_CFG_INVALID and (cur_entry & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s) (up to 0x3fff, whereas s->entries[arch] holds fw_cfg_max_entry(s) entries, typically 0x20 + file_slots), the next guest read from the fw_cfg data port (0x511) or DMA transfer dereferences &s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK] out of bounds on the host heap.

Steps to reproduce

  1. Launch QEMU with fw_cfg (default on pc, q35, and virt machines).
  2. Load a migration state where FWCfgState.cur_entry is e.g. 0x3ffe (!= FW_CFG_INVALID, with (cur_entry & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)).
  3. Read a byte from the guest fw_cfg data port (0x511), triggering fw_cfg_data_read() to index s->entries[arch][s->cur_entry & FW_CFG_ENTRY_MASK] past the allocated FWCfgEntry array.

Additional information

Proposed Fix Patch (02-hw-nvram-fw_cfg-vmstate-postload.patch, verified against master 73294d096cc591e122fde9e63db4739de6d84f2b)

--- a/hw/nvram/fw_cfg.c
+++ b/hw/nvram/fw_cfg.c
@@ -36,6 +36,7 @@
 #include "migration/vmstate.h"
 #include "trace.h"
 #include "qemu/error-report.h"
+#include "qemu/log.h"
 #include "qemu/option.h"
 #include "qemu/config-file.h"
 #include "qemu/cutils.h"
@@ -676,10 +677,34 @@
     },
 };
 
+/*
+ * cur_entry and cur_offset are restored from the migration stream.  A
+ * malicious or buggy migration source can supply any cur_entry value, and
+ * fw_cfg_data_read()/fw_cfg_dma_transfer() index s->entries[] with
+ * (cur_entry & FW_CFG_ENTRY_MASK) without re-checking it.  Reject selectors
+ * that fw_cfg_select() would have refused.
+ */
+static int fw_cfg_post_load(void *opaque, int version_id)
+{
+    FWCfgState *s = opaque;
+
+    if (s->cur_entry != FW_CFG_INVALID &&
+        (s->cur_entry & FW_CFG_ENTRY_MASK) >= fw_cfg_max_entry(s)) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "fw_cfg: vmstate transfer received bad entry value 0x%x\n",
+                      s->cur_entry);
+        s->cur_entry = FW_CFG_INVALID;
+        s->cur_offset = 0;
+    }
+
+    return 0;
+}
+
 static const VMStateDescription vmstate_fw_cfg = {
     .name = "fw_cfg",
     .version_id = 2,
     .minimum_version_id = 1,
+    .post_load = fw_cfg_post_load,
     .fields = (const VMStateField[]) {
         VMSTATE_UINT16(cur_entry, FWCfgState),
         VMSTATE_UINT16_HACK(cur_offset, FWCfgState, is_version_1),

02-hw-nvram-fw_cfg-vmstate-postload.patch