Skip to contentSkip to conversation

net: e1000e: fix an infinite loop issue

PATCH Superseded Li Qiang 2 participants 6 messages Feb 7, 2017 – Feb 8, 2017

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Li Qiang

author
#0 lore raw

PATCH 1/1 net: e1000e: fix an infinite loop issue

From: Li Qiang <[email protected]>

This issue is the same as e1000 network card in this commit:
e1000: eliminate infinite loops on out-of-bounds transfer start.

---
 hw/net/e1000e_core.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
hw/net/e1000e_core.c +16 -1
index 2b11499..53f2b1d 100644
@@ -914,7 +914,8 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
     struct e1000_tx_desc desc;
     bool ide = false;
     const E1000E_RingInfo *txi = txr->i;
-    uint32_t cause = E1000_ICS_TXQE;
+    uint32_t tdh_start = core->mac[txi->dh], cause = E1000_ICS_TXQE;
+
 
     if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
         trace_e1000e_tx_disabled();
@@ -933,6 +934,14 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
         cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx);
 
         e1000e_ring_advance(core, txi, 1);
+
+        /*
+         * The following avoid infinite loop, just as the e1000
+         */
+        if (core->mac[txi->dh] == tdh_start ||
+            tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
     }
 
     if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
@@ -1500,6 +1509,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
     size_t desc_size;
     size_t desc_offset = 0;
     size_t iov_ofs = 0;
+    uint32_t rdh_start;
 
     struct iovec *iov = net_rx_pkt_get_iovec(pkt);
     size_t size = net_rx_pkt_get_total_len(pkt);
@@ -1509,6 +1519,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
     bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
 
     rxi = rxr->i;
+    rdh_start = core->mac[rxi->dh];
 
     do {
         hwaddr ba[MAX_PS_BUFFERS];
@@ -1605,6 +1616,10 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
         e1000e_ring_advance(core, rxi,
                             core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
 
+        if (core->mac[rxi->dh] == rdh_start ||
+            rdh_start >= core->mac[rxi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
     } while (desc_offset < total_size);
 
     e1000e_update_rx_stats(core, size, total_size);
Signature · 1 line
1.8.3.1
#1 lore raw

Hello,

Thanks for the patch!

The problem of infinite loop indeed exists in e1000e, however it is different from the one fixed in e1000. Please find my comments inline.

~Dmitry

Quoted 28 lines · Li Qiang

From: Li Qiang <[email protected]>

This issue is the same as e1000 network card in this commit:
e1000: eliminate infinite loops on out-of-bounds transfer start.

---
hw/net/e1000e_core.c | 17 ++++++++++++++++-

1 file changed, 16 insertions(+), 1 deletion(-)
hw/net/e1000e_core.c +2 -1
index 2b11499..53f2b1d 100644
@@ -914,7 +914,8 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
    struct e1000_tx_desc desc;
    bool ide = false;
    const E1000E_RingInfo *txi = txr->i;
-    uint32_t cause = E1000_ICS_TXQE;
+    uint32_t tdh_start = core->mac[txi->dh], cause = E1000_ICS_TXQE;
+
 
    if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
        trace_e1000e_tx_disabled();
@@ -933,6 +934,14 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
        cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx);

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr) · line 944

Quoted 10 lines
hw/net/e1000e_core.c +8 -0
 
        e1000e_ring_advance(core, txi, 1);
+
+        /*
+         * The following avoid infinite loop, just as the e1000
+         */
+        if (core->mac[txi->dh] == tdh_start ||
+            tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }

Part of this validity check, namely
tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) already done by e1000e_ring_advance(), therefore not needed here.

The second part - full wraparound protection is relevant, but it fixes more symptoms than the cause. The only possible cause for full wraparound is r->dt (tail) value bigger or equal to r->dlen (length), so I would suggest to check for this in e1000e_ring_empty() and cover both TX and RX cases at once.

Quoted 33 lines
hw/net/e1000e_core.c +6 -0
    }
 
    if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
@@ -1500,6 +1509,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
    size_t desc_size;
    size_t desc_offset = 0;
    size_t iov_ofs = 0;
+    uint32_t rdh_start;
 
    struct iovec *iov = net_rx_pkt_get_iovec(pkt);
    size_t size = net_rx_pkt_get_total_len(pkt);
@@ -1509,6 +1519,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
    bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
 
    rxi = rxr->i;
+    rdh_start = core->mac[rxi->dh];
 
    do {
        hwaddr ba[MAX_PS_BUFFERS];
@@ -1605,6 +1616,10 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
        e1000e_ring_advance(core, rxi,
                            core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
 
+        if (core->mac[rxi->dh] == rdh_start ||
+            rdh_start >= core->mac[rxi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
    } while (desc_offset < total_size);
 
    e1000e_update_rx_stats(core, size, total_size);
Signature · 1 line
1.8.3.1
author
#2 lore raw

Hello,

2017-02-08 16:38 GMT+08:00 Dmitry Fleytman <[email protected]>:

Quoted 9 lines

Hello,

Thanks for the patch!

The problem of infinite loop indeed exists in e1000e, however it is different from the one fixed in e1000.
Please find my comments inline.

If I read the code correctly, I think this issue is the same. Could you please explain more? Thanks.

Quoted 44 lines

~Dmitry

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const

Quoted 17 lines · Li Qiang

From: Li Qiang <[email protected]>

This issue is the same as e1000 network card in this commit:
e1000: eliminate infinite loops on out-of-bounds transfer start.

---
hw/net/e1000e_core.c | 17 ++++++++++++++++-

1 file changed, 16 insertions(+), 1 deletion(-)
hw/net/e1000e_core.c +0 -0
index 2b11499..53f2b1d 100644
@@ -914,7 +914,8 @@ e1000e_start_xmit(E1000ECore *core, const

E1000E_TxRing *txr)

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const

Quoted 10 lines
hw/net/e1000e_core.c +2 -1
    struct e1000_tx_desc desc;
    bool ide = false;
    const E1000E_RingInfo *txi = txr->i;
-    uint32_t cause = E1000_ICS_TXQE;
+    uint32_t tdh_start = core->mac[txi->dh], cause = E1000_ICS_TXQE;
+
 
    if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
        trace_e1000e_tx_disabled();
@@ -933,6 +934,14 @@ e1000e_start_xmit(E1000ECore *core, const

E1000E_TxRing *txr)

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const · line 934

Quoted 1 line
hw/net/e1000e_core.c +0 -0
        cause |= e1000e_txdesc_writeback(core, base, &desc, &ide,

txi->idx);

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr) · line 944

Quoted 10 lines
hw/net/e1000e_core.c +8 -0
        e1000e_ring_advance(core, txi, 1);
+
+        /*
+         * The following avoid infinite loop, just as the e1000
+         */
+        if (core->mac[txi->dh] == tdh_start ||
+            tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
Quoted 5 lines

Part of this validity check, namely
tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) already done by e1000e_ring_advance(), therefore not needed here.

e1000e_ring_advance() check the added r->dh. Not the original one.

Quoted 8 lines

The second part - full wraparound protection is relevant, but it fixes more symptoms than the cause.
The only possible cause for full wraparound is r->dt (tail) value bigger or equal to r->dlen (length),
so I would suggest to check for this in e1000e_ring_empty() and cover both TX and RX cases at once.

Do you mean 'core->mac[r->dt] >= core->mac[r->dlen]' indicate an empty ring?

Quoted 38 lines

hw/net/e1000e_core.c e1000e_write_packet_to_guest(E1000ECore *core,

Quoted 4 lines
hw/net/e1000e_core.c +0 -0
    }
 
    if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
@@ -1500,6 +1509,7 @@ e1000e_write_packet_to_guest(E1000ECore *core,

struct NetRxPkt *pkt,

hw/net/e1000e_core.c e1000e_write_packet_to_guest(E1000ECore *core,

Quoted 8 lines
hw/net/e1000e_core.c +1 -0
    size_t desc_size;
    size_t desc_offset = 0;
    size_t iov_ofs = 0;
+    uint32_t rdh_start;
 
    struct iovec *iov = net_rx_pkt_get_iovec(pkt);
    size_t size = net_rx_pkt_get_total_len(pkt);
@@ -1509,6 +1519,7 @@ e1000e_write_packet_to_guest(E1000ECore *core,

struct NetRxPkt *pkt,

hw/net/e1000e_core.c e1000e_write_packet_to_guest(E1000ECore *core,

Quoted 8 lines
hw/net/e1000e_core.c +1 -0
    bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
 
    rxi = rxr->i;
+    rdh_start = core->mac[rxi->dh];
 
    do {
        hwaddr ba[MAX_PS_BUFFERS];
@@ -1605,6 +1616,10 @@ e1000e_write_packet_to_guest(E1000ECore *core,

struct NetRxPkt *pkt,

Quoted 13 lines
hw/net/e1000e_core.c +4 -1
        e1000e_ring_advance(core, rxi,
                            core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
 
+        if (core->mac[rxi->dh] == rdh_start ||
+            rdh_start >= core->mac[rxi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
    } while (desc_offset < total_size);
 
    e1000e_update_rx_stats(core, size, total_size);
--

1.8.3.1

#3 lore raw
Quoted 8 lines · Li Qiang

Hello,

2017-02-08 16:38 GMT+08:00 Dmitry Fleytman <[email protected] <mailto:[email protected]>>:
Hello,

Quoted 7 lines

Thanks for the patch!

The problem of infinite loop indeed exists in e1000e, however it is different from the one fixed in e1000. Please find my comments inline.

If I read the code correctly, I think this issue is the same. Could you please explain more? Thanks.

I meant that code changes needed to fix it are slightly different.

Quoted 47 lines

~Dmitry

Quoted 28 lines · On 7 Feb 2017, at 11:43 AM, Li Qiang <[email protected]…

From: Li Qiang <[email protected] <mailto:[email protected]>>

This issue is the same as e1000 network card in this commit:
e1000: eliminate infinite loops on out-of-bounds transfer start.

Signed-off-by Li Qiang <[email protected] <mailto:[email protected]>>

---
hw/net/e1000e_core.c | 17 ++++++++++++++++-

1 file changed, 16 insertions(+), 1 deletion(-)
hw/net/e1000e_core.c +2 -1
index 2b11499..53f2b1d 100644
@@ -914,7 +914,8 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
    struct e1000_tx_desc desc;
    bool ide = false;
    const E1000E_RingInfo *txi = txr->i;
-    uint32_t cause = E1000_ICS_TXQE;
+    uint32_t tdh_start = core->mac[txi->dh], cause = E1000_ICS_TXQE;
+
 
    if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
        trace_e1000e_tx_disabled();
@@ -933,6 +934,14 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
        cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx);

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr) · line 944

Quoted 10 lines
hw/net/e1000e_core.c +8 -0
 
        e1000e_ring_advance(core, txi, 1);
+
+        /*
+         * The following avoid infinite loop, just as the e1000
+         */
+        if (core->mac[txi->dh] == tdh_start ||
+            tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }

Part of this validity check, namely
tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) already done by e1000e_ring_advance(), therefore not needed here.

Quoted 1 line

e1000e_ring_advance() check the added r->dh. Not the original one.

Oh, I see now. Right, it does not make sense to check for wraparound in case original TDH was out of bounds.

Now I see there is one more problem - in case TDH is out of the ring, e1000e_ring_head_descr() called from e1000e_start_xmit() will do out of bounds read, so TDH should be validated prior to reading the descriptor as well.

Similar problem exists on RX also.

Besides that, as I see now, e1000e_ring_empty() is not called on RX so validation should be done separately for TX and RX rings.

A function like e1000e_ring_validate() is needed...

Quoted 41 lines

The second part - full wraparound protection is relevant, but it fixes more symptoms than the cause. The only possible cause for full wraparound is r->dt (tail) value bigger or equal to r->dlen (length), so I would suggest to check for this in e1000e_ring_empty() and cover both TX and RX cases at once.

Do you mean 'core->mac[r->dt] >= core->mac[r->dlen]' indicate an empty ring?

Quoted 33 lines
hw/net/e1000e_core.c +6 -1
    }
 
    if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
@@ -1500,6 +1509,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
    size_t desc_size;
    size_t desc_offset = 0;
    size_t iov_ofs = 0;
+    uint32_t rdh_start;
 
    struct iovec *iov = net_rx_pkt_get_iovec(pkt);
    size_t size = net_rx_pkt_get_total_len(pkt);
@@ -1509,6 +1519,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
    bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
 
    rxi = rxr->i;
+    rdh_start = core->mac[rxi->dh];
 
    do {
        hwaddr ba[MAX_PS_BUFFERS];
@@ -1605,6 +1616,10 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
        e1000e_ring_advance(core, rxi,
                            core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
 
+        if (core->mac[rxi->dh] == rdh_start ||
+            rdh_start >= core->mac[rxi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
    } while (desc_offset < total_size);
 
    e1000e_update_rx_stats(core, size, total_size);
--

1.8.3.1

author
#4 lore raw

Hello Dmitry,

2017-02-08 18:01 GMT+08:00 Dmitry Fleytman <[email protected]>:

Quoted 16 lines · Li Qiang

Hello,

2017-02-08 16:38 GMT+08:00 Dmitry Fleytman <[email protected]>:

Quoted 9 lines

Hello,

Thanks for the patch!

The problem of infinite loop indeed exists in e1000e, however it is different from the one fixed in e1000.
Please find my comments inline.

Quoted 6 lines

If I read the code correctly, I think this issue is the same. Could you please explain more? Thanks.

I meant that code changes needed to fix it are slightly different.

Got.

Quoted 55 lines
Quoted 47 lines

~Dmitry

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const

Quoted 17 lines · Li Qiang

From: Li Qiang <[email protected]>

This issue is the same as e1000 network card in this commit:
e1000: eliminate infinite loops on out-of-bounds transfer start.

---
hw/net/e1000e_core.c | 17 ++++++++++++++++-

1 file changed, 16 insertions(+), 1 deletion(-)
hw/net/e1000e_core.c +0 -0
index 2b11499..53f2b1d 100644
@@ -914,7 +914,8 @@ e1000e_start_xmit(E1000ECore *core, const

E1000E_TxRing *txr)

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const

Quoted 10 lines
hw/net/e1000e_core.c +2 -1
    struct e1000_tx_desc desc;
    bool ide = false;
    const E1000E_RingInfo *txi = txr->i;
-    uint32_t cause = E1000_ICS_TXQE;
+    uint32_t tdh_start = core->mac[txi->dh], cause = E1000_ICS_TXQE;
+
 
    if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
        trace_e1000e_tx_disabled();
@@ -933,6 +934,14 @@ e1000e_start_xmit(E1000ECore *core, const

E1000E_TxRing *txr)

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const · line 934

Quoted 1 line
hw/net/e1000e_core.c +0 -0
        cause |= e1000e_txdesc_writeback(core, base, &desc, &ide,

txi->idx);

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr) · line 944

Quoted 10 lines
hw/net/e1000e_core.c +8 -0
        e1000e_ring_advance(core, txi, 1);
+
+        /*
+         * The following avoid infinite loop, just as the e1000
+         */
+        if (core->mac[txi->dh] == tdh_start ||
+            tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
Quoted 5 lines

Part of this validity check, namely
tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) already done by e1000e_ring_advance(), therefore not needed here.

e1000e_ring_advance() check the added r->dh. Not the original one.

Quoted 10 lines

Oh, I see now. Right, it does not make sense to check for wraparound in case original TDH was out of bounds.

Now I see there is one more problem - in case TDH is out of the ring, e1000e_ring_head_descr()
called from e1000e_start_xmit() will do out of bounds read, so TDH should be validated prior to
reading the descriptor as well.

IIUC you mean pci_dma_read will do oob read, right? I think this is not a issue as this is reading data from guest
And guest can provide arbitrary data. For example, just provide 10 bytes buffer, but tell the qemu it has 1000.

Quoted 17 lines

Similar problem exists on RX also.

Besides that, as I see now, e1000e_ring_empty() is not called on RX so validation should be done separately for TX and RX rings.

A function like e1000e_ring_validate() is needed...

Quoted 8 lines

The second part - full wraparound protection is relevant, but it fixes more symptoms than the cause.
The only possible cause for full wraparound is r->dt (tail) value bigger or equal to r->dlen (length),
so I would suggest to check for this in e1000e_ring_empty() and cover both TX and RX cases at once.

Quoted 6 lines

Do you mean 'core->mac[r->dt] >= core->mac[r->dlen]' indicate an empty ring?

So I still confused by this 'full wrapparound', any more explain?

Quoted 39 lines
Quoted 37 lines
    }

hw/net/e1000e_core.c e1000e_write_packet_to_guest(E1000ECore *core,

Quoted 3 lines
hw/net/e1000e_core.c +0 -0
    if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
@@ -1500,6 +1509,7 @@ e1000e_write_packet_to_guest(E1000ECore *core,

struct NetRxPkt *pkt,

hw/net/e1000e_core.c e1000e_write_packet_to_guest(E1000ECore *core,

Quoted 8 lines
hw/net/e1000e_core.c +1 -0
    size_t desc_size;
    size_t desc_offset = 0;
    size_t iov_ofs = 0;
+    uint32_t rdh_start;
 
    struct iovec *iov = net_rx_pkt_get_iovec(pkt);
    size_t size = net_rx_pkt_get_total_len(pkt);
@@ -1509,6 +1519,7 @@ e1000e_write_packet_to_guest(E1000ECore *core,

struct NetRxPkt *pkt,

hw/net/e1000e_core.c e1000e_write_packet_to_guest(E1000ECore *core,

Quoted 8 lines
hw/net/e1000e_core.c +1 -0
    bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
 
    rxi = rxr->i;
+    rdh_start = core->mac[rxi->dh];
 
    do {
        hwaddr ba[MAX_PS_BUFFERS];
@@ -1605,6 +1616,10 @@ e1000e_write_packet_to_guest(E1000ECore *core,

struct NetRxPkt *pkt,

Quoted 13 lines
hw/net/e1000e_core.c +4 -1
        e1000e_ring_advance(core, rxi,
                            core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
 
+        if (core->mac[rxi->dh] == rdh_start ||
+            rdh_start >= core->mac[rxi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
    } while (desc_offset < total_size);
 
    e1000e_update_rx_stats(core, size, total_size);
--

1.8.3.1

#5 lore raw
Quoted 79 lines · Li Qiang

Hello Dmitry,

2017-02-08 18:01 GMT+08:00 Dmitry Fleytman <[email protected]>:

Quoted 7 lines · Li Qiang

Hello,

2017-02-08 16:38 GMT+08:00 Dmitry Fleytman <[email protected]>:
Hello,

Quoted 7 lines

Thanks for the patch!

The problem of infinite loop indeed exists in e1000e, however it is different from the one fixed in e1000. Please find my comments inline.

If I read the code correctly, I think this issue is the same. Could you please explain more? Thanks.

I meant that code changes needed to fix it are slightly different.

Got.

Quoted 47 lines

~Dmitry

Quoted 28 lines · Li Qiang

From: Li Qiang <[email protected]>

This issue is the same as e1000 network card in this commit:
e1000: eliminate infinite loops on out-of-bounds transfer start.

---
hw/net/e1000e_core.c | 17 ++++++++++++++++-

1 file changed, 16 insertions(+), 1 deletion(-)
hw/net/e1000e_core.c +2 -1
index 2b11499..53f2b1d 100644
@@ -914,7 +914,8 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
    struct e1000_tx_desc desc;
    bool ide = false;
    const E1000E_RingInfo *txi = txr->i;
-    uint32_t cause = E1000_ICS_TXQE;
+    uint32_t tdh_start = core->mac[txi->dh], cause = E1000_ICS_TXQE;
+
 
    if (!(core->mac[TCTL] & E1000_TCTL_EN)) {
        trace_e1000e_tx_disabled();
@@ -933,6 +934,14 @@ e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr)
        cause |= e1000e_txdesc_writeback(core, base, &desc, &ide, txi->idx);

hw/net/e1000e_core.c e1000e_start_xmit(E1000ECore *core, const E1000E_TxRing *txr) · line 944

Quoted 10 lines
hw/net/e1000e_core.c +8 -0
 
        e1000e_ring_advance(core, txi, 1);
+
+        /*
+         * The following avoid infinite loop, just as the e1000
+         */
+        if (core->mac[txi->dh] == tdh_start ||
+            tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }

Part of this validity check, namely
tdh_start >= core->mac[txi->dlen] / E1000_RING_DESC_LEN) already done by e1000e_ring_advance(), therefore not needed here.

Quoted 1 line

e1000e_ring_advance() check the added r->dh. Not the original one.

Oh, I see now. Right, it does not make sense to check for wraparound in case original TDH was out of bounds.

Quoted 7 lines

Now I see there is one more problem - in case TDH is out of the ring, e1000e_ring_head_descr() called from e1000e_start_xmit() will do out of bounds read, so TDH should be validated prior to reading the descriptor as well.

IIUC you mean pci_dma_read will do oob read, right? I think this is not a issue as this is reading data from guest And guest can provide arbitrary data. For example, just provide 10 bytes buffer, but tell the qemu it has 1000.

Agree.

Quoted 14 lines

Similar problem exists on RX also.

Besides that, as I see now, e1000e_ring_empty() is not called on RX so validation should be done separately for TX and RX rings.

A function like e1000e_ring_validate() is needed...

Quoted 6 lines

The second part - full wraparound protection is relevant, but it fixes more symptoms than the cause. The only possible cause for full wraparound is r->dt (tail) value bigger or equal to r->dlen (length), so I would suggest to check for this in e1000e_ring_empty() and cover both TX and RX cases at once.

Quoted 4 lines
Quoted 2 lines

Do you mean 'core->mac[r->dt] >= core->mac[r->dlen]' indicate an empty ring?

So I still confused by this 'full wrapparound', any more explain?

Yes, I would indicate an empty ring when r->dt in invalid. That would do the job.

Having e1000e_ring_empty() fixed, you might call it from e1000e_write_packet_to_guest() to fix RX issues as well.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
hw/net/e1000e_core.c +6 -1
    }
 
    if (!ide || !e1000e_intrmgr_delay_tx_causes(core, &cause)) {
@@ -1500,6 +1509,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
    size_t desc_size;
    size_t desc_offset = 0;
    size_t iov_ofs = 0;
+    uint32_t rdh_start;
 
    struct iovec *iov = net_rx_pkt_get_iovec(pkt);
    size_t size = net_rx_pkt_get_total_len(pkt);
@@ -1509,6 +1519,7 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
    bool do_ps = e1000e_do_ps(core, pkt, &ps_hdr_len);
 
    rxi = rxr->i;
+    rdh_start = core->mac[rxi->dh];
 
    do {
        hwaddr ba[MAX_PS_BUFFERS];
@@ -1605,6 +1616,10 @@ e1000e_write_packet_to_guest(E1000ECore *core, struct NetRxPkt *pkt,
        e1000e_ring_advance(core, rxi,
                            core->rx_desc_len / E1000_MIN_RX_DESC_LEN);
 
+        if (core->mac[rxi->dh] == rdh_start ||
+            rdh_start >= core->mac[rxi->dlen] / E1000_RING_DESC_LEN) {
+            break;
+            }
    } while (desc_offset < total_size);
 
    e1000e_update_rx_stats(core, size, total_size);
--

1.8.3.1