Skip to contentSkip to conversation

vfio/pci: Fix ROM load failure handling in vfio_rom_read()

PATCH In review Cédric Le Goater 3 participants 5 messages Jul 24

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 vfio/pci: Fix ROM load failure handling in vfio_rom_read()

When vfio_pci_load_rom() fails, vdev->rom is NULL but the memcpy still computes a source pointer from it, which is undefined behavior. Guard the access and return a 0xff pattern instead, which is what hardware returns for an absent ROM.

---
 hw/vfio/pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/vfio/pci.c +7 -2
index a3147d28665abd29fd804bd08bdffc3c7440033c..16d4f70ea58c9e5b174dd57950df55dc2ab91f6f 100644
@@ -1176,8 +1176,13 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         }
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    /* If ROM loading failed, return 0xff pattern */
+    if (vdev->rom_read_failed) {
+        memset(&val, 0xff, sizeof(val));
+    } else {
+        memcpy(&val, vdev->rom + addr,
+               (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    }
 
     switch (size) {
     case 1:
Signature · 1 line
2.55.0
#1 lore raw
Quoted 20 lines · On Fri, 24 Jul 2026 11:24:13 +0200 Cédric Le Goater <[email protected]> wrote:

When vfio_pci_load_rom() fails, vdev->rom is NULL but the memcpy still computes a source pointer from it, which is undefined behavior. Guard the access and return a 0xff pattern instead, which is what hardware returns for an absent ROM.

---
 hw/vfio/pci.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
hw/vfio/pci.c +0 -2
index a3147d28665abd29fd804bd08bdffc3c7440033c..16d4f70ea58c9e5b174dd57950df55dc2ab91f6f 100644
@@ -1176,8 +1176,13 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
         }
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 1188

Quoted 10 lines
hw/vfio/pci.c +7 -0
+    /* If ROM loading failed, return 0xff pattern */
+    if (vdev->rom_read_failed) {
+        memset(&val, 0xff, sizeof(val));
+    } else {
+        memcpy(&val, vdev->rom + addr,
+               (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    }
 
     switch (size) {
     case 1:

I thought we guaranteed that when (vdev->rom == NULL) that (vdev->rom_size == 0), thus we end up with a harmless zero-sized memcpy here. Also val is pre-initialized to ~0 so the memset() is redundant.

Does this actually change any behavior or is it only meant to sanitize the undefined (NULL + addr) pointer that's never dereferenced? If the latter, I'd drop the memset and note it explicitly as a sanitization, not a fix. Thanks,

Alex

#2 lore raw
Quoted 38 lines · Alex Williamson
Quoted 20 lines · On Fri, 24 Jul 2026 11:24:13 +0200 Cédric Le Goater <[email protected]> wrote:

When vfio_pci_load_rom() fails, vdev->rom is NULL but the memcpy still computes a source pointer from it, which is undefined behavior. Guard the access and return a 0xff pattern instead, which is what hardware returns for an absent ROM.

---
  hw/vfio/pci.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
hw/vfio/pci.c +0 -2
index a3147d28665abd29fd804bd08bdffc3c7440033c..16d4f70ea58c9e5b174dd57950df55dc2ab91f6f 100644
@@ -1176,8 +1176,13 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
          }
      }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 1188

Quoted 10 lines
hw/vfio/pci.c +7 -0
+    /* If ROM loading failed, return 0xff pattern */
+    if (vdev->rom_read_failed) {
+        memset(&val, 0xff, sizeof(val));
+    } else {
+        memcpy(&val, vdev->rom + addr,
+               (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    }
 
      switch (size) {
      case 1:

I thought we guaranteed that when (vdev->rom == NULL) that (vdev->rom_size == 0), thus we end up with a harmless zero-sized memcpy here. Also val is pre-initialized to ~0 so the memset() is redundant.

Quoted 4 lines

Does this actually change any behavior or is it only meant to sanitize the undefined (NULL + addr) pointer that's never dereferenced? If the latter, I'd drop the memset and note it explicitly as a sanitization, not a fix. Thanks,

Right. the memcpy is a no-op because the size is 0 and val stays ~0. I had that patch in my tree for a while, but Mario's recent fixes made it useless. Let's drop it.

Thanks,

C.

#3 lore raw
In reply to Cédric Le Goater
Quoted 20 lines · Cédric Le Goater

When vfio_pci_load_rom() fails, vdev->rom is NULL but the memcpy still computes a source pointer from it, which is undefined behavior. Guard the access and return a 0xff pattern instead, which is what hardware returns for an absent ROM.

---
  hw/vfio/pci.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
hw/vfio/pci.c +0 -2
index a3147d28665abd29fd804bd08bdffc3c7440033c..16d4f70ea58c9e5b174dd57950df55dc2ab91f6f 100644
@@ -1176,8 +1176,13 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
          }
      }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 1188

Quoted 10 lines
hw/vfio/pci.c +7 -0
+    /* If ROM loading failed, return 0xff pattern */
+    if (vdev->rom_read_failed) {
+        memset(&val, 0xff, sizeof(val));
+    } else {
+        memcpy(&val, vdev->rom + addr,
+               (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    }
 
      switch (size) {
      case 1:

Maybe simpler:

-- >8 --

hw/vfio/pci.c +6 -2
index c204706e630..d06a2d76523 100644
@@ -1151,13 +1151,18 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
     } val = { .qword = ~0ULL };
     uint64_t data = 0;
 
+    if (unlikely(vdev->rom_read_failed)) {
+        return -1;
+    }
+
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
+    if (unlikely(!vdev->rom)) {
         Error *local_err = NULL;
 
         vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
         if (vdev->rom_read_failed) {
             error_report_err(local_err);
+            return -1;
         }
     }
---

But we can also rewrite into a more readable code using ld/st (also avoiding memcpy zero-sized):

-- >8 --

hw/vfio/pci.c +13 -24
index c204706e630..af51c898f50 100644
@@ -33,6 +33,7 @@
 #include "migration/vmstate.h"
 #include "migration/cpr.h"
 #include "qobject/qdict.h"
+#include "qemu/bswap.h"
 #include "qemu/error-report.h"
 #include "qemu/main-loop.h"
 #include "qemu/module.h"
@@ -1143,42 +1144,30 @@ static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
 static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
 {
     VFIOPCIDevice *vdev = opaque;
-    union {
-        uint8_t byte;
-        uint16_t word;
-        uint32_t dword;
-        uint64_t qword;
-    } val = { .qword = ~0ULL };
-    uint64_t data = 0;
+    uint64_t data = -1;
+
+    if (unlikely(vdev->rom_read_failed)) {
+        goto out;
+    }
 
     /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
+    if (unlikely(!vdev->rom)) {
         Error *local_err = NULL;
 
         vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
         if (vdev->rom_read_failed) {
             error_report_err(local_err);
+            goto out;
         }
     }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
-
-    switch (size) {
-    case 1:
-        data = val.byte;
-        break;
-    case 2:
-        data = le16_to_cpu(val.word);
-        break;
-    case 4:
-        data = le32_to_cpu(val.dword);
-        break;
-    default:
-        hw_error("vfio: unsupported read size, %d bytes\n", size);
-        break;
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
     }
 
+    data = ldn_le_p(&val, size);
+
+out:
     trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);
 
     return data;

---

(untested).

#4 lore raw
Quoted 50 lines · On Fri, 24 Jul 2026 17:41:45 +0200 Philippe Mathieu-Daudé…
Quoted 20 lines · Cédric Le Goater

When vfio_pci_load_rom() fails, vdev->rom is NULL but the memcpy still computes a source pointer from it, which is undefined behavior. Guard the access and return a 0xff pattern instead, which is what hardware returns for an absent ROM.

---
  hw/vfio/pci.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
hw/vfio/pci.c +0 -2
index a3147d28665abd29fd804bd08bdffc3c7440033c..16d4f70ea58c9e5b174dd57950df55dc2ab91f6f 100644
@@ -1176,8 +1176,13 @@ static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
          }
      }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size) · line 1188

Quoted 10 lines
hw/vfio/pci.c +7 -0
+    /* If ROM loading failed, return 0xff pattern */
+    if (vdev->rom_read_failed) {
+        memset(&val, 0xff, sizeof(val));
+    } else {
+        memcpy(&val, vdev->rom + addr,
+               (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
+    }
 
      switch (size) {
      case 1:  

Maybe simpler:

-- >8 --

hw/vfio/pci.c +4 -1
index c204706e630..d06a2d76523 100644
@@ -1151,13 +1151,18 @@ static uint64_t vfio_rom_read(void *opaque, 
 hwaddr addr, unsigned size)
      } val = { .qword = ~0ULL };
      uint64_t data = 0;
 
+    if (unlikely(vdev->rom_read_failed)) {
+        return -1;
+    }
+
      /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {

hw/vfio/pci.c static uint64_t vfio_rom_read(void *opaque, · line 1163

Quoted 10 lines
hw/vfio/pci.c +2 -1
+    if (unlikely(!vdev->rom)) {
          Error *local_err = NULL;
 
          vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
          if (vdev->rom_read_failed) {
              error_report_err(local_err);
+            return -1;
          }
      }
---

Loses tracing, introduces more return paths, uses implicit -1 handling to fill return value... not simpler, imo.

Quoted 58 lines

But we can also rewrite into a more readable code using ld/st (also avoiding memcpy zero-sized):

-- >8 --

hw/vfio/pci.c +8 -19
index c204706e630..af51c898f50 100644
@@ -33,6 +33,7 @@
  #include "migration/vmstate.h"
  #include "migration/cpr.h"
  #include "qobject/qdict.h"
+#include "qemu/bswap.h"
  #include "qemu/error-report.h"
  #include "qemu/main-loop.h"
  #include "qemu/module.h"
@@ -1143,42 +1144,30 @@ static int 
 vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset,
  static uint64_t vfio_rom_read(void *opaque, hwaddr addr, unsigned size)
  {
      VFIOPCIDevice *vdev = opaque;
-    union {
-        uint8_t byte;
-        uint16_t word;
-        uint32_t dword;
-        uint64_t qword;
-    } val = { .qword = ~0ULL };
-    uint64_t data = 0;
+    uint64_t data = -1;
+
+    if (unlikely(vdev->rom_read_failed)) {
+        goto out;
+    }
 
      /* Load the ROM lazily when the guest tries to read it */
-    if (unlikely(!vdev->rom && !vdev->rom_read_failed)) {
+    if (unlikely(!vdev->rom)) {
          Error *local_err = NULL;
 
          vdev->rom_read_failed = !vfio_pci_load_rom(vdev, &local_err);
          if (vdev->rom_read_failed) {
              error_report_err(local_err);
+            goto out;
          }
      }
 
-    memcpy(&val, vdev->rom + addr,
-           (addr < vdev->rom_size) ? MIN(size, vdev->rom_size - addr) : 0);
-
-    switch (size) {
-    case 1:
-        data = val.byte;
-        break;
-    case 2:
-        data = le16_to_cpu(val.word);
-        break;
-    case 4:

hw/vfio/pci.c static int · line 1169

Quoted 10 lines
hw/vfio/pci.c +3 -5
-        data = le32_to_cpu(val.dword);
-        break;
-    default:
-        hw_error("vfio: unsupported read size, %d bytes\n", size);
-        break;
+    if (addr < vdev->rom_size) {
+        memcpy(&val, vdev->rom + addr, MIN(size, vdev->rom_size - addr));
      }
 
+    data = ldn_le_p(&val, size);

val is undeclared.

Quoted 6 lines
hw/vfio/pci.c +2 -0
+
+out:
      trace_vfio_rom_read(vdev->vbasedev.name, addr, size, data);
 
      return data;

---

As described in my previous reply, there's not actually a bug to fix here. The code behaves as intended and, in this respect, always has. I don't really see a need for the churn. Thanks,

Alex