Skip to contentSkip to conversation

hw/net/npcm_gmac: Catch accesses off the end of the register array

PATCH Applied Peter Maydell 2 participants 3 messages Mar 6 – Mar 9

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/net/npcm_gmac: Catch accesses off the end of the register array

In the npcm_gmac device, we create the iomem MemoryRegion with a size of 8KB, but NPCM_GMAC_NR_REGS is only 0x1060 / 4. This means there's a range of offsets that the guest can access that don't have gmac->regs[] entries. We weren't catching this, so the guest could get us to index off the end of the regs array.

Catch and log these invalid accesses.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3316

---
 hw/net/npcm_gmac.c         | 14 ++++++++++++++
 include/hw/net/npcm_gmac.h |  3 ++-
 2 files changed, 16 insertions(+), 1 deletion(-)
hw/net/npcm_gmac.c +14 -0
index 123fb92ca4..d9902d9ab5 100644
@@ -700,6 +700,13 @@ static uint64_t npcm_gmac_read(void *opaque, hwaddr offset, unsigned size)
     NPCMGMACState *gmac = opaque;
     uint32_t v = 0;
 
+    if (offset >= NPCM_GMAC_REG_SIZE) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: invalid register offset: 0x%04" HWADDR_PRIx"\n",
+                      DEVICE(gmac)->canonical_path, offset);
+        return v;
+    }
+
     switch (offset) {
     /* Write only registers */
     case A_NPCM_DMA_XMT_POLL_DEMAND:
@@ -724,6 +731,13 @@ static void npcm_gmac_write(void *opaque, hwaddr offset,
 
     trace_npcm_gmac_reg_write(DEVICE(gmac)->canonical_path, offset, v);
 
+    if (offset >= NPCM_GMAC_REG_SIZE) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: invalid register offset: 0x%04" HWADDR_PRIx"\n",
+                      DEVICE(gmac)->canonical_path, offset);
+        return;
+    }
+
     switch (offset) {
     /* Read only registers */
     case A_NPCM_GMAC_VERSION:
include/hw/net/npcm_gmac.h +2 -1
index d4fe49ada5..23b9841a80 100644
@@ -24,7 +24,8 @@
 #include "hw/core/sysbus.h"
 #include "net/net.h"
 
-#define NPCM_GMAC_NR_REGS (0x1060 / sizeof(uint32_t))
+#define NPCM_GMAC_REG_SIZE 0x1060
+#define NPCM_GMAC_NR_REGS (NPCM_GMAC_REG_SIZE / sizeof(uint32_t))
 
 #define NPCM_GMAC_MAX_PHYS 32
 #define NPCM_GMAC_MAX_PHY_REGS 32
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Peter Maydell

In the npcm_gmac device, we create the iomem MemoryRegion with a size of 8KB, but NPCM_GMAC_NR_REGS is only 0x1060 / 4. This means there's a range of offsets that the guest can access that don't have gmac->regs[] entries. We weren't catching this, so the guest could get us to index off the end of the regs array.

Catch and log these invalid accesses.

Quoted 7 lines

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3316

---
  hw/net/npcm_gmac.c         | 14 ++++++++++++++
  include/hw/net/npcm_gmac.h |  3 ++-
  2 files changed, 16 insertions(+), 1 deletion(-)

and queued.

#2 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

In the npcm_gmac device, we create the iomem MemoryRegion with a size of 8KB, but NPCM_GMAC_NR_REGS is only 0x1060 / 4. This means there's a range of offsets that the guest can access that don't have gmac->regs[] entries. We weren't catching this, so the guest could get us to index off the end of the regs array.

Catch and log these invalid accesses.

Quoted 7 lines

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/3316

---
  hw/net/npcm_gmac.c         | 14 ++++++++++++++
  include/hw/net/npcm_gmac.h |  3 ++-
  2 files changed, 16 insertions(+), 1 deletion(-)

Queued via hw-misc, thanks.