Commit 4ffa12178c for qemu.org

commit 4ffa12178cfb6122f86a628ad0031afce8af2f68
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Tue Nov 24 10:23:08 2026 +0000

    hw/net/rtl8139: Limit DMA transfers to plain memory

    There's no reason for the RTL8139 to be doing a DMA transfer to
    something that isn't memory, and allowing it can cause weird
    re-entrancy problems, such as a DMA to the PCI hot-unplug register
    causing the device to be deleted while it's halfway through doing
    something.

    Replace the calls to pci_dma_read() and pci_dma_write() with
    local functions that are identical except that they pass
    a MemTxAttrs that forbids non-memory accesses.

    This is the same approach we took to a similar problem with
    the hcd-xhci USB controller in commit 89e653b7736 (but differs
    from what we did for lsi53c895a in commit 4862d2c95104d).

    The known trigger for this bug requires that you turned off
    ACPI hotplug on the QEMU commandline with
      -global ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off
    so most configurations won't run into it.

    Cc: qemu-stable@nongnu.org
    Fixes: 3ada003aee2004d24f2 ("rtl8139: Use PCI DMA stub functions")
    Fixes: CVE-2026-128868
    Reported-by: VictorCHEN <ahchen-yz@outlook.com>
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Message-id: 20261113165015.1150646-1-peter.maydell@linaro.org
    Reviewed-by: Thomas Huth <thuth@redhat.com>

diff --git a/hw/net/rtl8139.c b/hw/net/rtl8139.c
index 38bc16f312..f3d1718921 100644
--- a/hw/net/rtl8139.c
+++ b/hw/net/rtl8139.c
@@ -513,6 +513,28 @@ struct RTL8139State {
     int rtl8139_mmio_io_addr_dummy;
 };

+/*
+ * There's no reason for the RTL8139 to do DMA to something that
+ * isn't memory, and it can cause weird re-entrancy problems if e.g.
+ * a DMA write targets the PCI hot-unplug register for the device.
+ * Set memory=1 in the memory attributes to avoid that kind of thing.
+ */
+static MemTxResult rtl8139_dma_write(PCIDevice *dev, dma_addr_t addr,
+                                     const void *buf, dma_addr_t len)
+{
+    const MemTxAttrs attrs = { .memory = true };
+    return pci_dma_rw(dev, addr, (void *)buf, len,
+                      DMA_DIRECTION_FROM_DEVICE, attrs);
+}
+
+static inline MemTxResult rtl8139_dma_read(PCIDevice *dev, dma_addr_t addr,
+                                           void *buf, dma_addr_t len)
+{
+    const MemTxAttrs attrs = { .memory = true };
+    return pci_dma_rw(dev, addr, buf, len,
+                      DMA_DIRECTION_TO_DEVICE, attrs);
+}
+
 /* Writes tally counters to memory via DMA */
 static void RTL8139TallyCounters_dma_write(RTL8139State *s, dma_addr_t tc_addr);

@@ -756,15 +778,15 @@ static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size)

             if (size > wrapped)
             {
-                pci_dma_write(d, s->RxBuf + s->RxBufAddr,
-                              buf, size-wrapped);
+                rtl8139_dma_write(d, s->RxBuf + s->RxBufAddr,
+                                  buf, size - wrapped);
             }

             /* reset buffer pointer */
             s->RxBufAddr = 0;

-            pci_dma_write(d, s->RxBuf + s->RxBufAddr,
-                          buf + (size-wrapped), wrapped);
+            rtl8139_dma_write(d, s->RxBuf + s->RxBufAddr,
+                              buf + (size - wrapped), wrapped);

             s->RxBufAddr = wrapped;

@@ -773,7 +795,7 @@ static void rtl8139_write_buffer(RTL8139State *s, const void *buf, int size)
     }

     /* non-wrapping path or overwrapping enabled */
-    pci_dma_write(d, s->RxBuf + s->RxBufAddr, buf, size);
+    rtl8139_dma_write(d, s->RxBuf + s->RxBufAddr, buf, size);

     s->RxBufAddr += size;
 }
@@ -971,13 +993,13 @@ static ssize_t rtl8139_receive(NetClientState *nc,

         uint32_t val, rxdw0,rxdw1,rxbufLO,rxbufHI;

-        pci_dma_read(d, cplus_rx_ring_desc, &val, 4);
+        rtl8139_dma_read(d, cplus_rx_ring_desc, &val, 4);
         rxdw0 = le32_to_cpu(val);
-        pci_dma_read(d, cplus_rx_ring_desc+4, &val, 4);
+        rtl8139_dma_read(d, cplus_rx_ring_desc + 4, &val, 4);
         rxdw1 = le32_to_cpu(val);
-        pci_dma_read(d, cplus_rx_ring_desc+8, &val, 4);
+        rtl8139_dma_read(d, cplus_rx_ring_desc + 8, &val, 4);
         rxbufLO = le32_to_cpu(val);
-        pci_dma_read(d, cplus_rx_ring_desc+12, &val, 4);
+        rtl8139_dma_read(d, cplus_rx_ring_desc + 12, &val, 4);
         rxbufHI = le32_to_cpu(val);

         DPRINTF("+++ C+ mode RX descriptor %d %08x %08x %08x %08x\n",
@@ -1040,12 +1062,12 @@ static ssize_t rtl8139_receive(NetClientState *nc,

         /* receive/copy to target memory */
         if (dot1q_buf) {
-            pci_dma_write(d, rx_addr, buf, 2 * ETH_ALEN);
-            pci_dma_write(d, rx_addr + 2 * ETH_ALEN,
-                          buf + 2 * ETH_ALEN + VLAN_HLEN,
-                          size - 2 * ETH_ALEN);
+            rtl8139_dma_write(d, rx_addr, buf, 2 * ETH_ALEN);
+            rtl8139_dma_write(d, rx_addr + 2 * ETH_ALEN,
+                              buf + 2 * ETH_ALEN + VLAN_HLEN,
+                              size - 2 * ETH_ALEN);
         } else {
-            pci_dma_write(d, rx_addr, buf, size);
+            rtl8139_dma_write(d, rx_addr, buf, size);
         }

         if (s->CpCmd & CPlusRxChkSum)
@@ -1055,7 +1077,7 @@ static ssize_t rtl8139_receive(NetClientState *nc,

         /* write checksum */
         val = cpu_to_le32(crc32(0, buf, size_));
-        pci_dma_write(d, rx_addr+size, (uint8_t *)&val, 4);
+        rtl8139_dma_write(d, rx_addr + size, (uint8_t *)&val, 4);

 /* first segment of received packet flag */
 #define CP_RX_STATUS_FS (1<<29)
@@ -1101,9 +1123,9 @@ static ssize_t rtl8139_receive(NetClientState *nc,

         /* update ring data */
         val = cpu_to_le32(rxdw0);
-        pci_dma_write(d, cplus_rx_ring_desc, (uint8_t *)&val, 4);
+        rtl8139_dma_write(d, cplus_rx_ring_desc, (uint8_t *)&val, 4);
         val = cpu_to_le32(rxdw1);
-        pci_dma_write(d, cplus_rx_ring_desc+4, (uint8_t *)&val, 4);
+        rtl8139_dma_write(d, cplus_rx_ring_desc + 4, (uint8_t *)&val, 4);

         /* update tally counter */
         ++s->tally_counters.RxOk;
@@ -1291,43 +1313,43 @@ static void RTL8139TallyCounters_dma_write(RTL8139State *s, dma_addr_t tc_addr)
     uint64_t val64;

     val64 = cpu_to_le64(tally_counters->TxOk);
-    pci_dma_write(d, tc_addr + 0,     (uint8_t *)&val64, 8);
+    rtl8139_dma_write(d, tc_addr + 0,     (uint8_t *)&val64, 8);

     val64 = cpu_to_le64(tally_counters->RxOk);
-    pci_dma_write(d, tc_addr + 8,     (uint8_t *)&val64, 8);
+    rtl8139_dma_write(d, tc_addr + 8,     (uint8_t *)&val64, 8);

     val64 = cpu_to_le64(tally_counters->TxERR);
-    pci_dma_write(d, tc_addr + 16,    (uint8_t *)&val64, 8);
+    rtl8139_dma_write(d, tc_addr + 16,    (uint8_t *)&val64, 8);

     val32 = cpu_to_le32(tally_counters->RxERR);
-    pci_dma_write(d, tc_addr + 24,    (uint8_t *)&val32, 4);
+    rtl8139_dma_write(d, tc_addr + 24,    (uint8_t *)&val32, 4);

     val16 = cpu_to_le16(tally_counters->MissPkt);
-    pci_dma_write(d, tc_addr + 28,    (uint8_t *)&val16, 2);
+    rtl8139_dma_write(d, tc_addr + 28,    (uint8_t *)&val16, 2);

     val16 = cpu_to_le16(tally_counters->FAE);
-    pci_dma_write(d, tc_addr + 30,    (uint8_t *)&val16, 2);
+    rtl8139_dma_write(d, tc_addr + 30,    (uint8_t *)&val16, 2);

     val32 = cpu_to_le32(tally_counters->Tx1Col);
-    pci_dma_write(d, tc_addr + 32,    (uint8_t *)&val32, 4);
+    rtl8139_dma_write(d, tc_addr + 32,    (uint8_t *)&val32, 4);

     val32 = cpu_to_le32(tally_counters->TxMCol);
-    pci_dma_write(d, tc_addr + 36,    (uint8_t *)&val32, 4);
+    rtl8139_dma_write(d, tc_addr + 36,    (uint8_t *)&val32, 4);

     val64 = cpu_to_le64(tally_counters->RxOkPhy);
-    pci_dma_write(d, tc_addr + 40,    (uint8_t *)&val64, 8);
+    rtl8139_dma_write(d, tc_addr + 40,    (uint8_t *)&val64, 8);

     val64 = cpu_to_le64(tally_counters->RxOkBrd);
-    pci_dma_write(d, tc_addr + 48,    (uint8_t *)&val64, 8);
+    rtl8139_dma_write(d, tc_addr + 48,    (uint8_t *)&val64, 8);

     val32 = cpu_to_le32(tally_counters->RxOkMul);
-    pci_dma_write(d, tc_addr + 56,    (uint8_t *)&val32, 4);
+    rtl8139_dma_write(d, tc_addr + 56,    (uint8_t *)&val32, 4);

     val16 = cpu_to_le16(tally_counters->TxAbt);
-    pci_dma_write(d, tc_addr + 60,    (uint8_t *)&val16, 2);
+    rtl8139_dma_write(d, tc_addr + 60,    (uint8_t *)&val16, 2);

     val16 = cpu_to_le16(tally_counters->TxUndrn);
-    pci_dma_write(d, tc_addr + 62,    (uint8_t *)&val16, 2);
+    rtl8139_dma_write(d, tc_addr + 62,    (uint8_t *)&val16, 2);
 }

 static void rtl8139_ChipCmd_write(RTL8139State *s, uint32_t val)
@@ -1810,7 +1832,7 @@ static int rtl8139_transmit_one(RTL8139State *s, int descriptor)
     DPRINTF("+++ transmit reading %d bytes from host memory at 0x%08x\n",
         txsize, s->TxAddr[descriptor]);

-    pci_dma_read(d, s->TxAddr[descriptor], txbuffer, txsize);
+    rtl8139_dma_read(d, s->TxAddr[descriptor], txbuffer, txsize);

     /* Mark descriptor as transferred */
     s->TxStatus[descriptor] |= TxHostOwns;
@@ -1886,13 +1908,13 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)

     uint32_t val, txdw0,txdw1,txbufLO,txbufHI;

-    pci_dma_read(d, cplus_tx_ring_desc,    (uint8_t *)&val, 4);
+    rtl8139_dma_read(d, cplus_tx_ring_desc, (uint8_t *)&val, 4);
     txdw0 = le32_to_cpu(val);
-    pci_dma_read(d, cplus_tx_ring_desc+4,  (uint8_t *)&val, 4);
+    rtl8139_dma_read(d, cplus_tx_ring_desc + 4, (uint8_t *)&val, 4);
     txdw1 = le32_to_cpu(val);
-    pci_dma_read(d, cplus_tx_ring_desc+8,  (uint8_t *)&val, 4);
+    rtl8139_dma_read(d, cplus_tx_ring_desc + 8, (uint8_t *)&val, 4);
     txbufLO = le32_to_cpu(val);
-    pci_dma_read(d, cplus_tx_ring_desc+12, (uint8_t *)&val, 4);
+    rtl8139_dma_read(d, cplus_tx_ring_desc + 12, (uint8_t *)&val, 4);
     txbufHI = le32_to_cpu(val);

     DPRINTF("+++ C+ mode TX descriptor %d %08x %08x %08x %08x\n", descriptor,
@@ -1986,8 +2008,8 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)
             DMA_ADDR_FMT" to offset %d\n", txsize, tx_addr,
             s->cplus_txbuffer_offset);

-    pci_dma_read(d, tx_addr,
-                 s->cplus_txbuffer + s->cplus_txbuffer_offset, txsize);
+    rtl8139_dma_read(d, tx_addr,
+                     s->cplus_txbuffer + s->cplus_txbuffer_offset, txsize);
     s->cplus_txbuffer_offset += txsize;

     /* seek to next Rx descriptor */
@@ -2017,7 +2039,7 @@ static int rtl8139_cplus_transmit_one(RTL8139State *s)

     /* update ring data */
     val = cpu_to_le32(tx_status);
-    pci_dma_write(d, cplus_tx_ring_desc, (uint8_t *)&val, 4);
+    rtl8139_dma_write(d, cplus_tx_ring_desc, (uint8_t *)&val, 4);

     /* Now decide if descriptor being processed is holding the last segment of packet */
     if (txdw0 & CP_TX_LS)