On 13/11/26 17:50, Peter Maydell wrote:
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: [email protected]
Fixes: 3ada003aee2004d24f2 ("rtl8139: Use PCI DMA stub functions")
Fixes: CVE-2026-128868
Reported-by: VictorCHEN <[email protected]>
Signed-off-by: Peter Maydell <[email protected]>
---
I'm also wondering whether there's an approach to this class of
bugs that would avoid us having to modify specific devices to
insist on DMA-to-memory. It seems a bit dubious that writing to
the PCI hot-unplug register can cause us to unrealize the plugged
device even if lots of other things are still holding a reference
to it. Or else we need to be more specific that you can still
validly (in the sense of "doesn't break things") do things on or
as part of an unrealized-but-not-yet-freed device, and move
things like pci_config_free() from unrealize to deinit.

While still thinking about that, ...


  hw/net/rtl8139.c | 98 +++++++++++++++++++++++++++++-------------------
  1 file changed, 60 insertions(+), 38 deletions(-)

diff --git a/hw/net/rtl8139.c b/hw/net/rtl8139.c
index 16479284eea..6644c1b62c5 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,

(drop inline)

+                                           void *buf, dma_addr_t len)
+{
+    const MemTxAttrs attrs = { .memory = true };
+    return pci_dma_rw(dev, addr, buf, len,
+                      DMA_DIRECTION_TO_DEVICE, attrs);

... these new helpers correctly return a MemTxResult but the
callers still ignore it. I appreciate this would be a larger
rework, unlikely worth it. What is your plan?

+}
+
  /* 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);[...]

Reply via email to