[PATCH] hw/net/rtl8139: Limit DMA transfers to plain memory

Peter Maydell posted 1 patch 1 week, 2 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20261113165015.1150646-1-peter.maydell@linaro.org
Maintainers: Jason Wang <jasowangio@gmail.com>
hw/net/rtl8139.c | 98 +++++++++++++++++++++++++++++-------------------
1 file changed, 60 insertions(+), 38 deletions(-)
[PATCH] hw/net/rtl8139: Limit DMA transfers to plain memory
Posted by Peter Maydell 1 week, 2 days ago
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>
---
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.

 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,
+                                           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)
-- 
2.43.0
Re: [PATCH] hw/net/rtl8139: Limit DMA transfers to plain memory
Posted by Philippe Mathieu-Daudé 6 days, 6 hours ago
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: 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>
> ---
> 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);[...]
Re: [PATCH] hw/net/rtl8139: Limit DMA transfers to plain memory
Posted by Peter Maydell 5 days, 18 hours ago
On Mon, 16 Nov 2026 at 22:19, Philippe Mathieu-Daudé
<philmd@oss.qualcomm.com> wrote:
>
> 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: 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>
> > ---
> > 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)

Agreed. This was a cut and paste from the pci_dma_read() prototype.

> > +                                           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?

I don't care enough about the rtl8139 to do that kind of spring
cleaning on it (and a memory transaction error will only happen
if the guest is doing something stupid, in which case we don't
care as long as it doesn't cause actual security problems).
My aim at the moment is only to try to get our backlog of
"confidential" bugs down...

thanks
-- PMM
Re: [PATCH] hw/net/rtl8139: Limit DMA transfers to plain memory
Posted by Thomas Huth 5 days, 17 hours ago
Am 17.11.26 um 10:49 schrieb Peter Maydell:
> On Mon, 16 Nov 2026 at 22:19, Philippe Mathieu-Daudé
> <philmd@oss.qualcomm.com> wrote:
>>
>> 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: 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>
>>> ---
>>> 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)
> 
> Agreed. This was a cut and paste from the pci_dma_read() prototype.
> 
>>> +                                           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?
> 
> I don't care enough about the rtl8139 to do that kind of spring
> cleaning on it (and a memory transaction error will only happen
> if the guest is doing something stupid, in which case we don't
> care as long as it doesn't cause actual security problems).
Maybe you could change the return type to "void" if it's ignored anyway, and 
simply do a qemu_log_mask(LOG_GUEST_ERROR ...) in the new function in case 
the pci_dma_rw failed?

  Thomas


Re: [PATCH] hw/net/rtl8139: Limit DMA transfers to plain memory
Posted by Peter Maydell 5 days, 17 hours ago
On Tue, 17 Nov 2026 at 11:02, Thomas Huth <thuth@redhat.com> wrote:
>
> Am 17.11.26 um 10:49 schrieb Peter Maydell:
> > On Mon, 16 Nov 2026 at 22:19, Philippe Mathieu-Daudé
> > <philmd@oss.qualcomm.com> wrote:
> >>
> >> 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: 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>
> >>> ---
> >>> 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)
> >
> > Agreed. This was a cut and paste from the pci_dma_read() prototype.
> >
> >>> +                                           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?
> >
> > I don't care enough about the rtl8139 to do that kind of spring
> > cleaning on it (and a memory transaction error will only happen
> > if the guest is doing something stupid, in which case we don't
> > care as long as it doesn't cause actual security problems).
> Maybe you could change the return type to "void" if it's ignored anyway, and
> simply do a qemu_log_mask(LOG_GUEST_ERROR ...) in the new function in case
> the pci_dma_rw failed?

That's doing more work than I want to do in a CVE fix.
The wrapper should continue to return the error status
as pci_dma_read and pci_dma_write did. Eventually somebody
might want to fix up the handling in the callsites.

thanks
-- PMM