net: tulip: add checks to avoid OOB access
v6 cover net: tulip: add checks to avoid OOB access
From: Prasad J Pandit <[email protected]>
Hello,
* This series adds checks to avoid potential OOB access and infinite loop issues while processing rx/tx data.
* Tulip tx descriptors are capped at 128 to avoid infinite loop in
tulip_xmit_list_update(), wrt Tulip kernel driver
-> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/dec/tulip/tulip.h#n319
* Update v3: add .can_receive routine
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html
* Update v4: flush queued packets once they are received
-> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg05868.html
* Update v5: fixed a typo in patch commit message
-> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg06209.html
* Update v6: merge earlier patch 2 & 3 into one
-> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg06868.html
Thank you.
--
Prasad J Pandit (2):
net: tulip: check frame size and r/w data length
net: tulip: add .can_receive routine
hw/net/tulip.c | 51 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 9 deletions(-)
--
2.25.1
PATCH v6 1/2 net: tulip: check frame size and r/w data length
From: Prasad J Pandit <[email protected]>
Tulip network driver while copying tx/rx buffers does not check frame size against r/w data length. This may lead to OOB buffer access. Add check to avoid it.
Limit iterations over descriptors to avoid potential infinite loop issue in tulip_xmit_list_update.
--- hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-)
Update v3: return a value from tulip_copy_tx_buffers() and avoid infinite loop
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html
hw/net/tulip.c +27 -9
@@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) }else { len = s->rx_frame_len; } + +if (s->rx_frame_len + len >=sizeof (s->rx_frame)) { +return ; + }pci_dma_write (&s->dev, desc->buf_addr1, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len;
@@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) }else { len = s->rx_frame_len; } + +if (s->rx_frame_len + len >=sizeof (s->rx_frame)) { +return ; + }pci_dma_write (&s->dev, desc->buf_addr2, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len;
@@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)trace_tulip_receive (buf, size); -if (size <14 || size >2048 || s->rx_frame_len ||tulip_rx_stopped (s)) { +if (size <14 || size >sizeof (s->rx_frame) -4 + || s->rx_frame_len ||tulip_rx_stopped (s)) {return 0 ; }
@@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState *nc,return tulip_receive (qemu_get_nic_opaque (nc), buf, size); } -static NetClientInfo net_tulip_info = { .type =NET_CLIENT_DRIVER_NIC , .size =sizeof (NICState),
@@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)if ((s->csr[6 ] >>CSR6_OM_SHIFT ) &CSR6_OM_MASK ) {/* Internal or external Loopback */ tulip_receive (s, s->tx_frame, s->tx_frame_len); - }else { + }else if (s->tx_frame_len <=sizeof (s->tx_frame)) {qemu_send_packet (qemu_get_queue (s->nic), s->tx_frame, s->tx_frame_len); }
@@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) } } -static void tulip_copy_tx_buffers (TULIPState *s,struct tulip_descriptor *desc) +static int tulip_copy_tx_buffers (TULIPState *s,struct tulip_descriptor *desc) {int len1 = (desc->control >>TDES1_BUF1_SIZE_SHIFT ) &TDES1_BUF1_SIZE_MASK ;int len2 = (desc->control >>TDES1_BUF2_SIZE_SHIFT ) &TDES1_BUF2_SIZE_MASK ; +if (s->tx_frame_len + len1 >=sizeof (s->tx_frame)) { +return -1 ; + }if (len1) {pci_dma_read (&s->dev, desc->buf_addr1, s->tx_frame + s->tx_frame_len, len1); s->tx_frame_len += len1; } +if (s->tx_frame_len + len2 >=sizeof (s->tx_frame)) { +return -1 ; + }if (len2) {pci_dma_read (&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2); s->tx_frame_len += len2; } desc->status = (len1 + len2) ?0 :0x7fffffff ; + +return 0 ; }static void tulip_setup_filter_addr (TULIPState *s,uint8_t *buf,int n)
@@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s)static void tulip_xmit_list_update (TULIPState *s) { +#define TULIP_DESC_MAX 128 +uint8_t i =0 ;struct tulip_descriptor desc;if (tulip_ts (s) !=CSR5_TS_SUSPENDED ) {return ; } -for (;;) { +for (i =0 ; i <TULIP_DESC_MAX ; i++) {tulip_desc_read (s, s->current_tx_desc, &desc);tulip_dump_tx_descriptor (s, &desc);
@@ -675,10 +693,10 @@ static void tulip_xmit_list_update(TULIPState *s) s->tx_frame_len =0 ; } -tulip_copy_tx_buffers (s, &desc); - -if (desc.control &TDES1_LS ) { -tulip_tx (s, &desc); +if (!tulip_copy_tx_buffers (s, &desc)) { +if (desc.control &TDES1_LS ) { +tulip_tx (s, &desc); + } } }tulip_desc_write (s, s->current_tx_desc, &desc);
Signature · 1 line
2.25.1
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
Quoted 6 lines · P J P <[email protected]> 于2020年3月23日周一 下午8:24写道:
From: Prasad J Pandit <[email protected]>
Tulip network driver while copying tx/rx buffers does not check frame size against r/w data length. This may lead to OOB buffer access. Add check to avoid it.
Quoted 8 lines
Limit iterations over descriptors to avoid potential infinite loop issue in
tulip_xmit_list_update.Reported-by Li Qiang <[email protected]>Reported-by Ziming Zhang <[email protected]>Reported-by Jason Wang <[email protected]>Signed-off-by Prasad J Pandit <[email protected]>
But I have a minor question....
Quoted 13 lines
--- hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-)Update v3: return a value from
tulip_copy_tx_buffers()and avoid infinite loop
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.htmlhw/net/tulip.c +0
-0
hw/net/tulip.c static void tulip_copy_rx_bytes(TULIPState *s, struct · line 178
Quoted 9 lines
hw/net/tulip.c +4
-0@@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) }else { len = s->rx_frame_len; } + +if (s->rx_frame_len + len >=sizeof (s->rx_frame)) { +return ; + }
Why here is '>=' instead of '>'.
IIUC the total sending length can reach to sizeof(s->rx_frame). Same in the other place in this patch.
PS: I have planned to write a qtest case. But my personal qemu dev environment is broken.
I will try to write it tonight or tomorrow night.
Thanks,
Li Qiang
Quoted 120 lines
hw/net/tulip.c +0
-0pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame +(s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len;hw/net/tulip.c +4
-0@@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) }else { len = s->rx_frame_len; } + +if (s->rx_frame_len + len >=sizeof (s->rx_frame)) { +return ; + }pci_dma_write (&s->dev, desc->buf_addr2, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len);s->rx_frame_len -= len;hw/net/tulip.c +2
-1@@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)trace_tulip_receive (buf ,size ); -if (size <14 || size >2048 || s->rx_frame_len || tulip_rx_stopped(s)) { +if (size <14 || size >sizeof (s->rx_frame) -4 + || s->rx_frame_len ||tulip_rx_stopped (s)) {return 0 ;}hw/net/tulip.c +1
-2@@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState *nc,return tulip_receive (qemu_get_nic_opaque (nc), buf, size); } -static NetClientInfo net_tulip_info = { .type =NET_CLIENT_DRIVER_NIC , .size =sizeof (NICState),@@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc)if ((s->csr[6 ] >>CSR6_OM_SHIFT ) &CSR6_OM_MASK ) {/* Internal or external Loopback */ tulip_receive (s, s->tx_frame, s->tx_frame_len); - }else { + }else if (s->tx_frame_len <=sizeof (s->tx_frame)) {qemu_send_packet (qemu_get_queue (s->nic), s->tx_frame, s->tx_frame_len);}hw/net/tulip.c +16
-7@@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) } } -static void tulip_copy_tx_buffers (TULIPState *s,struct tulip_descriptor *desc) +static int tulip_copy_tx_buffers (TULIPState *s,struct tulip_descriptor *desc) {int len1 = (desc->control >>TDES1_BUF1_SIZE_SHIFT ) &TDES1_BUF1_SIZE_MASK ;int len2 = (desc->control >>TDES1_BUF2_SIZE_SHIFT ) &TDES1_BUF2_SIZE_MASK ; +if (s->tx_frame_len + len1 >=sizeof (s->tx_frame)) { +return -1 ; + }if (len1) {pci_dma_read (&s->dev, desc->buf_addr1, s->tx_frame + s->tx_frame_len, len1); s->tx_frame_len += len1; } +if (s->tx_frame_len + len2 >=sizeof (s->tx_frame)) { +return -1 ; + }if (len2) {pci_dma_read (&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2); s->tx_frame_len += len2; } desc->status = (len1 + len2) ?0 :0x7fffffff ; + +return 0 ; }static void tulip_setup_filter_addr (TULIPState *s,uint8_t *buf,int n)@@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s)static void tulip_xmit_list_update (TULIPState *s) { +#define TULIP_DESC_MAX 128 +uint8_t i =0 ;struct tulip_descriptor desc;if (tulip_ts (s) !=CSR5_TS_SUSPENDED ) {return ; } -for (;;) { +for (i =0 ; i <TULIP_DESC_MAX ; i++) {tulip_desc_read (s, s->current_tx_desc, &desc);tulip_dump_tx_descriptor (s, &desc);@@ -675,10 +693,10 @@ static void tulip_xmit_list_update(TULIPState *s) s->tx_frame_len =0 ; } -tulip_copy_tx_buffers (s, &desc); - -if (desc.control &TDES1_LS ) { -tulip_tx (s, &desc); +if (!tulip_copy_tx_buffers (s, &desc)) { +if (desc.control &TDES1_LS ) { +tulip_tx (s, &desc); + } } }tulip_desc_write (s, s->current_tx_desc, &desc); --2.25.1
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
Quoted 53 lines · Li Qiang
P J P <[email protected] <mailto:[email protected]>> 于2020年3月23日周一 下午8:24写道:
From: Prasad J Pandit <[email protected]
<mailto:[email protected]>>Tulip network driver while copying tx/rx buffers does not check frame size against r/w data length. This may lead to OOB buffer access. Add check to avoid it.
Limit iterations over descriptors to avoid potential infinite loop issue in
tulip_xmit_list_update.Reported-by Li Qiang <[email protected]<mailto:[email protected]>>
Reported-by Ziming Zhang <[email protected]<mailto:[email protected]>>
Reported-by Jason Wang <[email protected]<mailto:[email protected]>>
Signed-off-by Prasad J Pandit <[email protected]<mailto:[email protected]>>
Tested-by Li Qiang <[email protected] <mailto:[email protected]>>But I have a minor question....
--- hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) Update v3: return a value from tulip_copy_tx_buffers() and avoid infinite loop -> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html diff --git a/hw/net/tulip.c b/hw/net/tulip.c index cfac2719d3..fbe40095da 100644 --- a/hw/net/tulip.c +++ b/hw/net/tulip.c @@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) } else { len = s->rx_frame_len; } + + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { + return; + }
Quoted 3 lines
Why here is '>=' instead of '>'.
IIUC the total sending length can reach tosizeof(s->rx_frame). Same in the other place in this patch.
Yes, this need to be fixed.
Quoted 3 lines
PS: I have planned to write a qtest case. But my personal qemu dev environment is broken. I will try to write it tonight or tomorrow night.
Cool, good to know this.
Thanks
Quoted 129 lines
Thanks,
Li Qiangpci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len; @@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) } else { len = s->rx_frame_len; } + + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { + return; + } pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len; @@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) trace_tulip_receive(buf, size); - if (size < 14 || size > 2048 || s->rx_frame_len || tulip_rx_stopped(s)) { + if (size < 14 || size > sizeof(s->rx_frame) - 4 + || s->rx_frame_len || tulip_rx_stopped(s)) { return 0; } @@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState *nc, return tulip_receive(qemu_get_nic_opaque(nc), buf, size); } - static NetClientInfo net_tulip_info = { .type = NET_CLIENT_DRIVER_NIC, .size = sizeof(NICState), @@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) { /* Internal or external Loopback */ tulip_receive(s, s->tx_frame, s->tx_frame_len); - } else { + } else if (s->tx_frame_len <= sizeof(s->tx_frame)) { qemu_send_packet(qemu_get_queue(s->nic), s->tx_frame, s->tx_frame_len); } @@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) } } -static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) +static int tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) { int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK; int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK; + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) { + return -1; + } if (len1) { pci_dma_read(&s->dev, desc->buf_addr1, s->tx_frame + s->tx_frame_len, len1); s->tx_frame_len += len1; } + if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) { + return -1; + } if (len2) { pci_dma_read(&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2); s->tx_frame_len += len2; } desc->status = (len1 + len2) ? 0 : 0x7fffffff; + + return 0; } static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, int n) @@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s) static void tulip_xmit_list_update(TULIPState *s) { +#define TULIP_DESC_MAX 128 + uint8_t i = 0; struct tulip_descriptor desc; if (tulip_ts(s) != CSR5_TS_SUSPENDED) { return; } - for (;;) { + for (i = 0; i < TULIP_DESC_MAX; i++) { tulip_desc_read(s, s->current_tx_desc, &desc); tulip_dump_tx_descriptor(s, &desc); @@ -675,10 +693,10 @@ static void tulip_xmit_list_update(TULIPState *s) s->tx_frame_len = 0; } - tulip_copy_tx_buffers(s, &desc); - - if (desc.control & TDES1_LS) { - tulip_tx(s, &desc); + if (!tulip_copy_tx_buffers(s, &desc)) { + if (desc.control & TDES1_LS) { + tulip_tx(s, &desc); + } } } tulip_desc_write(s, s->current_tx_desc, &desc); -- 2.25.1
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
+-- On Tue, 24 Mar 2020, Jason Wang wrote --+
| > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) {
| > + return;
| > + }
| >
| > Why here is '>=' instead of '>'. IIUC the total sending length can reach
| > to sizeof(s->rx_frame). Same in the other place in this patch.
|
| Yes, this need to be fixed.But, wouldn't s->rx_frame[sizeof(s->rx_frame)] be off-by-one?
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
In reply to Jason WangQuoted 65 lines · Jason Wang <[email protected]> 于2020年3月24日周二 下午1:45写道:
Quoted 54 lines · Li Qiang
P J P <[email protected] <mailto:[email protected]>> 于2020年3月23日周一 下午8:24写道:
From: Prasad J Pandit <[email protected]
<mailto:[email protected]>>Tulip network driver while copying tx/rx buffers does not check frame size against r/w data length. This may lead to OOB buffer access. Add check to avoid it.
Limit iterations over descriptors to avoid potential infinite loop issue in
tulip_xmit_list_update.Reported-by Li Qiang <[email protected]<mailto:[email protected]>>
Reported-by Ziming Zhang <[email protected]<mailto:[email protected]>>
Reported-by Jason Wang <[email protected]<mailto:[email protected]>>
Signed-off-by Prasad J Pandit <[email protected]<mailto:[email protected]>>
Tested-by Li Qiang <[email protected] <mailto:[email protected]>>But I have a minor question....
--- hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) Update v3: return a value from tulip_copy_tx_buffers() and avoid infinite loop -> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html diff --git a/hw/net/tulip.c b/hw/net/tulip.c index cfac2719d3..fbe40095da 100644 --- a/hw/net/tulip.c +++ b/hw/net/tulip.c @@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) } else { len = s->rx_frame_len; } + + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { + return; + }Quoted 3 lines
Why here is '>=' instead of '>'.
IIUC the total sending length can reach tosizeof(s->rx_frame). Same in the other place in this patch.Yes, this need to be fixed.
Quoted 1 line
Quoted 8 lines
Quoted 3 lines
PS: I have planned to write a qtest case. But my personal qemu dev environment is broken.
I will try to write it tonight or tomorrow night.Cool, good to know this.
Hi all,
I have countered an interesting issue. Let's look at the definition of TULIPState.
21 typedef struct TULIPState {
22 PCIDevice dev;
23 MemoryRegion io;
24 MemoryRegion memory;
25 NICConf c;
26 qemu_irq irq;
27 NICState *nic;
28 eeprom_t *eeprom;
29 uint32_t csr[16];
30
31 /* state for MII */
32 uint32_t old_csr9;
33 uint32_t mii_word;
34 uint32_t mii_bitcnt;
35
36 hwaddr current_rx_desc;
37 hwaddr current_tx_desc;
38
39 uint8_t rx_frame[2048];
40 uint8_t tx_frame[2048];
41 uint16_t tx_frame_len;
42 uint16_t rx_frame_len;
43 uint16_t rx_frame_size;
44
45 uint32_t rx_status;
46 uint8_t filter[16][6];
47 } TULIPState;Here we can see the overflow is occured after 'tx_frame'.
In my quest, I have see the overflow(the s->tx_frame_len is big). However here doesn't cause SEGV in qtest.
In real case, the qemu process will access the data after TULIPState in heap and trigger segv.
However in qtest mode I don't know how to trigger this.
The core code like this:
qpci_device_enable(dev);
bar = qpci_iomap(dev, 0, NULL);
context_pa = guest_alloc(alloc, sizeof(context));
guest_pa = guest_alloc(alloc, 4096);
memset(guest_data, 'A', sizeof(guest_data));
context[0].status = 1 << 31;
context[0].control = 0x7ff << 11 | 0x7ff;
context[0].buf_addr2 = context_pa + sizeof(struct tulip_descriptor);
context[0].buf_addr1 = guest_pa;
for (i = 1; i < ARRAY_SIZE(context); ++i) {
context_pa += sizeof(struct tulip_descriptor);
context[i].status = 1 << 31;context[i].control = 0x7ff << 11 | 0x7ff; context[i].buf_addr2 = context_pa + sizeof(struct tulip_descriptor); context[i].buf_addr1 = guest_pa;
}
qtest_memwrite(dev->bus->qts, context_pa, context, sizeof(context)); qtest_memwrite(dev->bus->qts, guest_pa, guest_data, sizeof(guest_data)); qpci_io_writel(dev, bar, 0x20, context_pa); qpci_io_writel(dev, bar, 0x30, 1 << 13);
Paolo may give some hints?
Thanks,
Li Qiang
Quoted 134 lines
Thanks
Quoted 129 lines
Thanks,
Li Qiangpci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len; @@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) } else { len = s->rx_frame_len; } + + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { + return; + } pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame + (s->rx_frame_size - s->rx_frame_len), len); s->rx_frame_len -= len; @@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) trace_tulip_receive(buf, size); - if (size < 14 || size > 2048 || s->rx_frame_len || tulip_rx_stopped(s)) { + if (size < 14 || size > sizeof(s->rx_frame) - 4 + || s->rx_frame_len || tulip_rx_stopped(s)) { return 0; } @@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState *nc, return tulip_receive(qemu_get_nic_opaque(nc), buf, size); } - static NetClientInfo net_tulip_info = { .type = NET_CLIENT_DRIVER_NIC, .size = sizeof(NICState), @@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) { /* Internal or external Loopback */ tulip_receive(s, s->tx_frame, s->tx_frame_len); - } else { + } else if (s->tx_frame_len <= sizeof(s->tx_frame)) { qemu_send_packet(qemu_get_queue(s->nic), s->tx_frame, s->tx_frame_len); } @@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) } } -static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) +static int tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) { int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK; int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK; + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) { + return -1; + } if (len1) { pci_dma_read(&s->dev, desc->buf_addr1, s->tx_frame + s->tx_frame_len, len1); s->tx_frame_len += len1; } + if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) { + return -1; + } if (len2) { pci_dma_read(&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2); s->tx_frame_len += len2; } desc->status = (len1 + len2) ? 0 : 0x7fffffff; + + return 0; } static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, int n) @@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s) static void tulip_xmit_list_update(TULIPState *s) { +#define TULIP_DESC_MAX 128 + uint8_t i = 0; struct tulip_descriptor desc; if (tulip_ts(s) != CSR5_TS_SUSPENDED) { return; } - for (;;) { + for (i = 0; i < TULIP_DESC_MAX; i++) { tulip_desc_read(s, s->current_tx_desc, &desc); tulip_dump_tx_descriptor(s, &desc); @@ -675,10 +693,10 @@ static void tulip_xmit_list_update(TULIPState *s) s->tx_frame_len = 0; } - tulip_copy_tx_buffers(s, &desc); - - if (desc.control & TDES1_LS) { - tulip_tx(s, &desc); + if (!tulip_copy_tx_buffers(s, &desc)) { + if (desc.control & TDES1_LS) { + tulip_tx(s, &desc); + } } } tulip_desc_write(s, s->current_tx_desc, &desc); -- 2.25.1
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
Quoted 116 lines · Li Qiang
Jason Wang <[email protected] <mailto:[email protected]>> 于2020年3月24日周二 下午1:45写道:
On 2020/3/24 上午9:29, Li Qiang wrote: > > > P J P <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> 于2020年3月23日周一 > 下午8:24写道: > > From: Prasad J Pandit <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>>> > > Tulip network driver while copying tx/rx buffers does not check > frame size against r/w data length. This may lead to OOB buffer > access. Add check to avoid it. > > Limit iterations over descriptors to avoid potential infinite > loop issue in tulip_xmit_list_update. > > Reported-by: Li Qiang <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>>> > Reported-by: Ziming Zhang <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>>> > Reported-by: Jason Wang <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>>> > Signed-off-by: Prasad J Pandit <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>>> > > > > Tested-by: Li Qiang <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> > But I have a minor question.... > > --- > hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- > 1 file changed, 27 insertions(+), 9 deletions(-) > > Update v3: return a value from tulip_copy_tx_buffers() and avoid > infinite loop > -> > https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html > > diff --git a/hw/net/tulip.c b/hw/net/tulip.c > index cfac2719d3..fbe40095da 100644 > --- a/hw/net/tulip.c > +++ b/hw/net/tulip.c > @@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState > *s, struct tulip_descriptor *desc) > } else { > len = s->rx_frame_len; > } > + > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { > + return; > + } > > > > Why here is '>=' instead of '>'. > IIUC the total sending length can reach to sizeof(s->rx_frame). > Same in the other place in this patch. Yes, this need to be fixed. > > PS: I have planned to write a qtest case. But my personal qemu dev > environment is broken. > I will try to write it tonight or tomorrow night. Cool, good to know this.Hi all,
I have countered an interesting issue. Let's look at the definition of TULIPState.21 typedef struct TULIPState {
22 PCIDevice dev;
23 MemoryRegion io;
24 MemoryRegion memory;
25 NICConf c;
26qemu_irqirq;
27 NICState *nic;
28eeprom_t*eeprom;
29uint32_tcsr[16];
30
31 /* state for MII */
32uint32_told_csr9;
33uint32_tmii_word;
34uint32_tmii_bitcnt;
35
36 hwaddrcurrent_rx_desc;
37 hwaddrcurrent_tx_desc;
38
39uint8_trx_frame[2048];
40uint8_ttx_frame[2048];
41uint16_ttx_frame_len;
42uint16_trx_frame_len;
43uint16_trx_frame_size;
44
45uint32_trx_status;
46uint8_tfilter[16][6];
47 } TULIPState;
Quoted 5 lines
Here we can see the overflow is occured after '
tx_frame'.
In my quest, I have see theoverflow(the s->tx_frame_len is big).
However here doesn't cause SEGV in qtest.
In real case, the qemu process will access the data after TULIPState in heap and trigger segv. However in qtest mode I don't know how to trigger this.
If it's just the mangling of tx_frame_len, it won't hit SIGSEV.
I wonder maybe, somehow that large tx_frame_len is used for buffer copying or other stuffs that can lead the crash.
Thanks
Quoted 165 lines
The core code like this:
qpci_device_enable(dev);
bar =qpci_iomap(dev, 0, NULL);
context_pa=guest_alloc(alloc, sizeof(context));guest_pa=guest_alloc(alloc, 4096);memset(guest_data, 'A', sizeof(guest_data)); context[0].status = 1 << 31;
context[0].control =0x7ff<< 11 |0x7ff; context[0].buf_addr2 =context_pa+sizeof(struct tulip_descriptor); context[0].buf_addr1 =guest_pa;
for (i = 1; i <ARRAY_SIZE(context); ++i) {context_pa+=sizeof(struct tulip_descriptor); context[i].status = 1 << 31;
context[i].control =0x7ff<< 11 |0x7ff; context[i].buf_addr2 =context_pa+sizeof(struct tulip_descriptor); context[i].buf_addr1 =guest_pa;
}
qtest_memwrite(dev->bus->qts, context_pa, context, sizeof(context));qtest_memwrite(dev->bus->qts, guest_pa, guest_data, sizeof(guest_data));qpci_io_writel(dev, bar, 0x20, context_pa);qpci_io_writel(dev, bar, 0x30, 1 << 13);Paolo may give some hints?
Thanks,
Li QiangThanks > > Thanks, > Li Qiang > > > > > pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame + > (s->rx_frame_size - s->rx_frame_len), len); > s->rx_frame_len -= len; > @@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState > *s, struct tulip_descriptor *desc) > } else { > len = s->rx_frame_len; > } > + > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { > + return; > + } > pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame + > (s->rx_frame_size - s->rx_frame_len), len); > s->rx_frame_len -= len; > @@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, > const uint8_t *buf, size_t size) > > trace_tulip_receive(buf, size); > > - if (size < 14 || size > 2048 || s->rx_frame_len || > tulip_rx_stopped(s)) { > + if (size < 14 || size > sizeof(s->rx_frame) - 4 > + || s->rx_frame_len || tulip_rx_stopped(s)) { > return 0; > } > > @@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState > *nc, > return tulip_receive(qemu_get_nic_opaque(nc), buf, size); > } > > - > static NetClientInfo net_tulip_info = { > .type = NET_CLIENT_DRIVER_NIC, > .size = sizeof(NICState), > @@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct > tulip_descriptor *desc) > if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) { > /* Internal or external Loopback */ > tulip_receive(s, s->tx_frame, s->tx_frame_len); > - } else { > + } else if (s->tx_frame_len <= sizeof(s->tx_frame)) { > qemu_send_packet(qemu_get_queue(s->nic), > s->tx_frame, s->tx_frame_len); > } > @@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct > tulip_descriptor *desc) > } > } > > -static void tulip_copy_tx_buffers(TULIPState *s, struct > tulip_descriptor *desc) > +static int tulip_copy_tx_buffers(TULIPState *s, struct > tulip_descriptor *desc) > { > int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & > TDES1_BUF1_SIZE_MASK; > int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & > TDES1_BUF2_SIZE_MASK; > > + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) { > + return -1; > + } > if (len1) { > pci_dma_read(&s->dev, desc->buf_addr1, > s->tx_frame + s->tx_frame_len, len1); > s->tx_frame_len += len1; > } > > + if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) { > + return -1; > + } > if (len2) { > pci_dma_read(&s->dev, desc->buf_addr2, > s->tx_frame + s->tx_frame_len, len2); > s->tx_frame_len += len2; > } > desc->status = (len1 + len2) ? 0 : 0x7fffffff; > + > + return 0; > } > > static void tulip_setup_filter_addr(TULIPState *s, uint8_t *buf, > int n) > @@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s) > > static void tulip_xmit_list_update(TULIPState *s) > { > +#define TULIP_DESC_MAX 128 > + uint8_t i = 0; > struct tulip_descriptor desc; > > if (tulip_ts(s) != CSR5_TS_SUSPENDED) { > return; > } > > - for (;;) { > + for (i = 0; i < TULIP_DESC_MAX; i++) { > tulip_desc_read(s, s->current_tx_desc, &desc); > tulip_dump_tx_descriptor(s, &desc); > > @@ -675,10 +693,10 @@ static void > tulip_xmit_list_update(TULIPState *s) > s->tx_frame_len = 0; > } > > - tulip_copy_tx_buffers(s, &desc); > - > - if (desc.control & TDES1_LS) { > - tulip_tx(s, &desc); > + if (!tulip_copy_tx_buffers(s, &desc)) { > + if (desc.control & TDES1_LS) { > + tulip_tx(s, &desc); > + } > } > } > tulip_desc_write(s, s->current_tx_desc, &desc); > -- > 2.25.1 > >
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
Quoted 130 lines · Jason Wang <[email protected]> 于2020年3月27日周五 上午10:09写道:
Quoted 44 lines · Li Qiang
Jason Wang <[email protected] <mailto:[email protected]>> 于2020年3月24日周二 下午1:45写道:
On 2020/3/24 上午9:29, Li Qiang wrote:
>
>
> P J P <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> 于2020年3月23日周一
> 下午8:24写道:
>
> From: Prasad J Pandit <[email protected]
<mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>>
>
> Tulip network driver while copying tx/rx buffers does not check
> frame size against r/w data length. This may lead to OOB buffer
> access. Add check to avoid it.
>
> Limit iterations over descriptors to avoid potential infinite
> loop issue intulip_xmit_list_update.
>
> Reported-by: Li Qiang <[email protected]
<mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>>
> Reported-by: Ziming Zhang <[email protected]
<mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>>
> Reported-by: Jason Wang <[email protected]
<mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>>
> Signed-off-by: Prasad J Pandit <[email protected]
<mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>>>
>
>
>
> Tested-by: Li Qiang <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>
> But I have a minor question....
>Quoted 5 lines
> ---
>hw/net/tulip.c| 36 +++++++++++++++++++++++++++---------
> 1 file changed, 27 insertions(+), 9 deletions(-)
>
> Update v3: return a value fromtulip_copy_tx_buffers()andavoid
Quoted 3 lines
Quoted 3 lines
infinite loop ->https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html
Quoted 66 lines
Quoted 17 lines
diff --git a/hw/net/tulip.c b/hw/net/tulip.c index cfac2719d3..fbe40095da 100644 --- a/hw/net/tulip.c +++ b/hw/net/tulip.c @@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) } else { len = s->rx_frame_len; } + + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { + return; + }Quoted 3 lines
Why here is '>=' instead of '>'.
IIUC the total sending length can reach tosizeof(s->rx_frame). Same in the other place in this patch.Yes, this need to be fixed.
>
> PS: I have planned to write a qtest case. But my personal qemu dev > environment is broken.
> I will try to write it tonight or tomorrow night.Cool, good to know this.
Hi all,
I have countered an interesting issue. Let's look at the definition of TULIPState.21 typedef struct TULIPState { 22 PCIDevice dev; 23 MemoryRegion io; 24 MemoryRegion memory; 25 NICConf c; 26 qemu_irq irq; 27 NICState *nic; 28 eeprom_t *eeprom; 29 uint32_t csr[16]; 30 31 /* state for MII */ 32 uint32_t old_csr9; 33 uint32_t mii_word; 34 uint32_t mii_bitcnt; 35 36 hwaddr current_rx_desc; 37 hwaddr current_tx_desc; 38 39 uint8_t rx_frame[2048]; 40 uint8_t tx_frame[2048]; 41 uint16_t tx_frame_len; 42 uint16_t rx_frame_len; 43 uint16_t rx_frame_size; 44 45 uint32_t rx_status; 46 uint8_t filter[16][6]; 47 } TULIPState;Quoted 6 lines
Here we can see the overflow is occured after '
tx_frame'. In my quest, I have see theoverflow(the s->tx_frame_len is big). However here doesn't cause SEGV in qtest.
In real case, the qemu process will access the data after TULIPState in heap and trigger segv.
However in qtest mode I don't know how to trigger this.
Quoted 5 lines
If it's just the mangling of
tx_frame_len, it won't hit SIGSEV.I wonder maybe, somehow that large
tx_frame_lenis used for buffer copying or other stuffs that can lead the crash.
This is because in real qemu process, the OOB copy corrupts the head data after 'TULIPState' struct.
And maybe later(other thread) access the corrupted data thus leading crash. However in qtest mode, I don't remember the core code of qtest. But seems it's not a really VM? just a interface emulation.
In my case, it's backtrace is as this:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffbdb7fe700 (LWP 60813)] 0x0000000000000000 in ?? ()
...
(gdb) bt
#0 0x0000000000000000 in ()
#1 0x0000555555a7dfa3 in qemu_set_irq (irq=0x5555579e0710, level=1) at hw/core/irq.c:44
#2 0x0000555555b2b87a in tulip_update_int (s=0x5555579da7c0) at hw/net/tulip.c:121
#3 0x0000555555b2cdd9 in tulip_xmit_list_update (s=0x5555579da7c0) at hw/net/tulip.c:667
#4 0x0000555555b2d19d in tulip_write (opaque=0x5555579da7c0, addr=32, data=931909632, size=4) at hw/net/tulip.c:759
#5 0x000055555587eed1 in memory_region_write_accessor (mr=0x5555579db0b0, addr=32, value=0x7ffbdb7fd6a8, size=4, shift=0, mask=4294967295, attrs=...) at /xxx/qemu/memory.c:483
#6 0x000055555587f0da in access_with_adjusted_size (addr=32, value=0x7ffbdb7fd6a8, size=4, access_size_min=4, access_size_max=4, access_fn=0x55555587edf2 <memory_region_write_accessor>, mr=0x5555579db0b0, attrs=...)
at /xxx/qemu/memory.c:544
#7 0x000055555588213b in memory_region_dispatch_write (mr=0x5555579db0b0, addr=32, data=931909632, op=MO_32, attrs=...) at /xxx/qemu/memory.c:1476 #8 0x000055555581fe9c in flatview_write_continue (fv=0x7ffbb001eae0, addr=49184, attrs=..., ptr=0x7ffff7e13000, len=4, addr1=32, l=4, mr=0x5555579db0b0) at /xxx/qemu/exec.c:3125
#9 0x000055555581fff4 in flatview_write (fv=0x7ffbb001eae0, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4) at /xxx/qemu/exec.c:3165 #10 0x0000555555820368 in address_space_write (as=0x555556762560 <address_space_io>, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4) at /xxx/qemu/exec.c:3256
#11 0x00005555558203da in address_space_rw (as=0x555556762560 <address_space_io>, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4, is_write=true) at /xxx/qemu/exec.c:3266
#12 0x000055555589723b in kvm_handle_io (port=49184, attrs=..., data=0x7ffff7e13000, direction=1, size=4, count=1) at /xxx/qemu/accel/kvm/kvm-all.c:2140
#13 0x00005555558979d6 in kvm_cpu_exec (cpu=0x555556b1e220) at /xxx/qemu/accel/kvm/kvm-all.c:2386
#14 0x00005555558701c5 in qemu_kvm_cpu_thread_fn (arg=0x555556b1e220) at /xxx/qemu/cpus.c:1246
#15 0x0000555555de3ce4 in qemu_thread_start (args=0x555556b44040) at util/qemu-thread-posix.c:519
#16 0x00007ffff5bb0e25 in start_thread () at /lib64/libpthread.so.0 #17 0x00007ffff58d8f1d in clone () at /lib64/libc.so.6
I will try to dig into the qtest code and hope find a way to trigger a segv in qtest.
Thanks,
Li Qiang
Quoted 172 lines
Thanks
Quoted 80 lines
The core code like this:
qpci_device_enable(dev);bar = qpci_iomap(dev, 0, NULL); context_pa = guest_alloc(alloc, sizeof(context)); guest_pa = guest_alloc(alloc, 4096); memset(guest_data, 'A', sizeof(guest_data)); context[0].status = 1 << 31; context[0].control = 0x7ff << 11 | 0x7ff; context[0].buf_addr2 = context_pa + sizeof(struct tulip_descriptor); context[0].buf_addr1 = guest_pa; for (i = 1; i < ARRAY_SIZE(context); ++i) { context_pa += sizeof(struct tulip_descriptor); context[i].status = 1 << 31;context[i].control =
0x7ff<< 11 |0x7ff; context[i].buf_addr2 =context_pa+sizeof(struct tulip_descriptor); context[i].buf_addr1 =guest_pa;
}
qtest_memwrite(dev->bus->qts, context_pa, context, sizeof(context));qtest_memwrite(dev->bus->qts, guest_pa, guest_data, sizeof(guest_data));qpci_io_writel(dev, bar, 0x20, context_pa);qpci_io_writel(dev, bar, 0x30, 1 << 13);Paolo may give some hints?
Thanks,
Li QiangThanks > > Thanks, > Li Qiang > > > > > pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame + > (s->rx_frame_size - s->rx_frame_len), len); > s->rx_frame_len -= len; > @@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState > *s, struct tulip_descriptor *desc) > } else { > len = s->rx_frame_len; > } > + > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { > + return; > + } > pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame + > (s->rx_frame_size - s->rx_frame_len), len); > s->rx_frame_len -= len; > @@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, > const uint8_t *buf, size_t size) > > trace_tulip_receive(buf, size); > > - if (size < 14 || size > 2048 || s->rx_frame_len || > tulip_rx_stopped(s)) { > + if (size < 14 || size > sizeof(s->rx_frame) - 4 > + || s->rx_frame_len || tulip_rx_stopped(s)) { > return 0; > } > > @@ -275,7 +284,6 @@ static ssize_t tulip_receive_nc(NetClientState > *nc, > return tulip_receive(qemu_get_nic_opaque(nc), buf, size); > } > > - > static NetClientInfo net_tulip_info = { > .type = NET_CLIENT_DRIVER_NIC, > .size = sizeof(NICState), > @@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct > tulip_descriptor *desc) > if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) {Quoted 8 lines
> /* Internal or external Loopback */ > tulip_receive(s, s->tx_frame, s->tx_frame_len); > - } else { > + } else if (s->tx_frame_len <= sizeof(s->tx_frame)) { > qemu_send_packet(qemu_get_queue(s->nic), > s->tx_frame, s->tx_frame_len); > } > @@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s,struct
Quoted 77 lines
Quoted 29 lines
tulip_descriptor *desc) } } -static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) +static int tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) { int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & TDES1_BUF1_SIZE_MASK; int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & TDES1_BUF2_SIZE_MASK; + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) { + return -1; + } if (len1) { pci_dma_read(&s->dev, desc->buf_addr1, s->tx_frame + s->tx_frame_len, len1); s->tx_frame_len += len1; } + if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) { + return -1; + } if (len2) { pci_dma_read(&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2);Quoted 8 lines
s->tx_frame_len += len2; } desc->status = (len1 + len2) ? 0 : 0x7fffffff; + + return 0; } static void tulip_setup_filter_addr(TULIPState *s, uint8_t*buf, > int n) > @@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s) > > static void tulip_xmit_list_update(TULIPState *s) > { > +#define TULIP_DESC_MAX 128 > + uint8_t i = 0; > struct tulip_descriptor desc; > > if (tulip_ts(s) != CSR5_TS_SUSPENDED) { > return; > } > > - for (;;) { > + for (i = 0; i < TULIP_DESC_MAX; i++) { > tulip_desc_read(s, s->current_tx_desc, &desc); > tulip_dump_tx_descriptor(s, &desc); > > @@ -675,10 +693,10 @@ static void > tulip_xmit_list_update(TULIPState *s) > s->tx_frame_len = 0; > } > > - tulip_copy_tx_buffers(s, &desc); > - > - if (desc.control & TDES1_LS) { > - tulip_tx(s, &desc); > + if (!tulip_copy_tx_buffers(s, &desc)) { > + if (desc.control & TDES1_LS) { > + tulip_tx(s, &desc); > + } > } > } > tulip_desc_write(s, s->current_tx_desc, &desc); > -- > 2.25.1 > >
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
Quoted 163 lines · Li Qiang
Jason Wang <[email protected] <mailto:[email protected]>> 于2020年3月27日周五 上午10:09写道:
On 2020/3/24 下午10:54, Li Qiang wrote:
>
>
> Jason Wang <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> > 于2020年3月24日周二 下午1:45写道:
>
>
> On 2020/3/24 上午9:29, Li Qiang wrote:
> >
> >
> > P J P <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>
> <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > 于2020年3月23日周一
> > 下午8:24写道:
> >
> > From: Prasad J Pandit <[email protected] <mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>>
> >
> > Tulip network driver while copying tx/rx buffers does not check
> > frame size against r/w data length. This may lead to OOB buffer
> > access. Add check to avoid it.
> >
> > Limit iterations over descriptors to avoid potential infinite
> > loop issue intulip_xmit_list_update.
> >
> > Reported-by: Li Qiang <[email protected] <mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>>
> > Reported-by: Ziming Zhang <[email protected] <mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > Reported-by: Jason Wang <[email protected] <mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>>
> > Signed-off-by: Prasad J Pandit <[email protected] <mailto:[email protected]>
> <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>>
> >
> >
> >
> > Tested-by: Li Qiang <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>
> <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>>
> > But I have a minor question....
> >
> > ---
> > hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- > > 1 file changed, 27 insertions(+), 9 deletions(-) > >
> > Update v3: return a value fromtulip_copy_tx_buffers()and avoid
> > infinite loop
> > ->
> >
https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html
> >
> > diff --gita/hw/net/tulip.cb/hw/net/tulip.c> > indexcfac2719d3..fbe40095da100644
> > ---a/hw/net/tulip.c
> > +++b/hw/net/tulip.c
> > @@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState
> > *s, structtulip_descriptor*desc)
> > } else {
> > len =s->rx_frame_len;
> > }
> > +
> > + if (s->rx_frame_len+ len >=sizeof(s->rx_frame)) {
> > + return;
> > + }
> >
> >
> >
> > Why here is '>=' instead of '>'.
> > IIUC the total sending length can reach tosizeof(s->rx_frame).
> > Same in the other place in this patch.
>
>
> Yes, this need to be fixed.
>
>
> >
> > PS: I have planned to write a qtest case. But my personal qemu dev
> > environment is broken.
> > I will try to write it tonight or tomorrow night. >
>
> Cool, good to know this.
>
>
> Hi all,
> I have countered an interesting issue. Let's look at the definition of
> TULIPState.
>
> 21 typedef struct TULIPState {
> 22 PCIDevice dev;
> 23 MemoryRegion io;
> 24 MemoryRegion memory;
> 25 NICConf c;
> 26qemu_irqirq;
> 27 NICState *nic;
> 28eeprom_t*eeprom;
> 29uint32_tcsr[16];
> 30
> 31 /* state for MII */
> 32uint32_told_csr9;
> 33uint32_tmii_word;
> 34uint32_tmii_bitcnt;
> 35
> 36 hwaddrcurrent_rx_desc;
> 37 hwaddrcurrent_tx_desc;
> 38
> 39uint8_trx_frame[2048];
> 40uint8_ttx_frame[2048];
> 41uint16_ttx_frame_len;
> 42uint16_trx_frame_len;
> 43uint16_trx_frame_size;
> 44
> 45uint32_trx_status;
> 46uint8_tfilter[16][6];
> 47 } TULIPState;
>
> Here we can see the overflow is occured after 'tx_frame'. > In my quest, I have see theoverflow(the s->tx_frame_len is big). > However here doesn't cause SEGV in qtest.
> In real case, the qemu process will access the data after TULIPState
> in heap and trigger segv.
> However in qtest mode I don't know how to trigger this.
Quoted 8 lines
If it's just the mangling of
tx_frame_len, it won't hit SIGSEV.I wonder maybe, somehow that large
tx_frame_lenis used for buffer copying or other stuffs that can lead the crash.This is because in real qemu process, the OOB copy corrupts the head data after 'TULIPState' struct. And maybe later(other thread) access the corrupted data thus leading crash.
Ok, so I think ASAN can detect this crash. But I'm not sure whether or not it was enabled for qtest. If not, we probably need that.
I wrote a qtest for virtio-net that can lead OOB, so it should probably work for tulip but need to check.
Or if you don't want to depend on ASAN, we can check user visible status after tx_frame[], but it looks to me all other fields are not visible by guest.
Maybe we can reorder to place csr[] after tx_frame[] then check csr[] afterwards.
Quoted 1 line
However in qtest mode, I don't remember the core code of qtest. But seems it's not a really VM? just a interface emulation.
If my memory is correct, it's not a VM.
Thanks
Quoted 215 lines
In my case, it's backtrace is as this:
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread0x7ffbdb7fe700(LWP 60813)]0x0000000000000000in ?? ()
...
(gdb) bt
#0 0x0000000000000000 in ()
#1 0x0000555555a7dfa3 inqemu_set_irq(irq=0x5555579e0710, level=1) athw/core/irq.c:44
#2 0x0000555555b2b87a intulip_update_int(s=0x5555579da7c0) athw/net/tulip.c:121
#3 0x0000555555b2cdd9 intulip_xmit_list_update(s=0x5555579da7c0) athw/net/tulip.c:667
#4 0x0000555555b2d19d intulip_write(opaque=0x5555579da7c0, addr=32, data=931909632, size=4) athw/net/tulip.c:759
#5 0x000055555587eed1 inmemory_region_write_accessor(mr=0x5555579db0b0, addr=32, value=0x7ffbdb7fd6a8, size=4, shift=0, mask=4294967295, attrs=...) at /xxx/qemu/memory.c:483
#6 0x000055555587f0da inaccess_with_adjusted_size(addr=32, value=0x7ffbdb7fd6a8, size=4, access_size_min=4, access_size_max=4, access_fn=0x55555587edf2 <memory_region_write_accessor>, mr=0x5555579db0b0, attrs=...) at /xxx/qemu/memory.c:544
#7 0x000055555588213b inmemory_region_dispatch_write(mr=0x5555579db0b0, addr=32, data=931909632, op=MO_32, attrs=...) at /xxx/qemu/memory.c:1476
#8 0x000055555581fe9c inflatview_write_continue(fv=0x7ffbb001eae0, addr=49184, attrs=..., ptr=0x7ffff7e13000, len=4, addr1=32, l=4, mr=0x5555579db0b0) at /xxx/qemu/exec.c:3125 #9 0x000055555581fff4 inflatview_write(fv=0x7ffbb001eae0, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4) at /xxx/qemu/exec.c:3165
#100x0000555555820368inaddress_space_write(as=0x555556762560 <address_space_io>, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4) at /xxx/qemu/exec.c:3256
#110x00005555558203dainaddress_space_rw(as=0x555556762560 <address_space_io>, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4, is_write=true) at /xxx/qemu/exec.c:3266
#120x000055555589723binkvm_handle_io(port=49184, attrs=..., data=0x7ffff7e13000, direction=1, size=4, count=1) at /xxx/qemu/accel/kvm/kvm-all.c:2140
#130x00005555558979d6inkvm_cpu_exec(cpu=0x555556b1e220) at /xxx/qemu/accel/kvm/kvm-all.c:2386
#140x00005555558701c5inqemu_kvm_cpu_thread_fn(arg=0x555556b1e220) at /xxx/qemu/cpus.c:1246
#150x0000555555de3ce4inqemu_thread_start(args=0x555556b44040) atutil/qemu-thread-posix.c:519
#160x00007ffff5bb0e25instart_thread() at/lib64/libpthread.so.0
#170x00007ffff58d8f1din clone () at/lib64/libc.so.6I will try to dig into the qtest code and hope find a way to trigger a segv in qtest.
Thanks,
Li QiangThanks > > The core code like this: > > qpci_device_enable(dev); > bar = qpci_iomap(dev, 0, NULL); > context_pa = guest_alloc(alloc, sizeof(context)); > guest_pa = guest_alloc(alloc, 4096); > memset(guest_data, 'A', sizeof(guest_data)); > context[0].status = 1 << 31; > context[0].control = 0x7ff << 11 | 0x7ff; > context[0].buf_addr2 = context_pa + sizeof(struct tulip_descriptor); > context[0].buf_addr1 = guest_pa; > for (i = 1; i < ARRAY_SIZE(context); ++i) { > context_pa += sizeof(struct tulip_descriptor); > context[i].status = 1 << 31; > context[i].control = 0x7ff << 11 | 0x7ff; > context[i].buf_addr2 = context_pa + sizeof(struct tulip_descriptor); > context[i].buf_addr1 = guest_pa; > } > > qtest_memwrite(dev->bus->qts, context_pa, context, sizeof(context)); > qtest_memwrite(dev->bus->qts, guest_pa, guest_data, sizeof(guest_data)); > qpci_io_writel(dev, bar, 0x20, context_pa); > qpci_io_writel(dev, bar, 0x30, 1 << 13); > > Paolo may give some hints? > > Thanks, > Li Qiang > > Thanks > > > > > > Thanks, > > Li Qiang > > > > > > > > > > pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame + > > (s->rx_frame_size - s->rx_frame_len), len); > > s->rx_frame_len -= len; > > @@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState > > *s, struct tulip_descriptor *desc) > > } else { > > len = s->rx_frame_len; > > } > > + > > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { > > + return; > > + } > > pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame + > > (s->rx_frame_size - s->rx_frame_len), len); > > s->rx_frame_len -= len; > > @@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, > > const uint8_t *buf, size_t size) > > > > trace_tulip_receive(buf, size); > > > > - if (size < 14 || size > 2048 || s->rx_frame_len || > > tulip_rx_stopped(s)) { > > + if (size < 14 || size > sizeof(s->rx_frame) - 4 > > + || s->rx_frame_len || tulip_rx_stopped(s)) { > > return 0; > > } > > > > @@ -275,7 +284,6 @@ static ssize_t > tulip_receive_nc(NetClientState > > *nc, > > return tulip_receive(qemu_get_nic_opaque(nc), buf, size); > > } > > > > - > > static NetClientInfo net_tulip_info = { > > .type = NET_CLIENT_DRIVER_NIC, > > .size = sizeof(NICState), > > @@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct > > tulip_descriptor *desc) > > if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) { > > /* Internal or external Loopback */ > > tulip_receive(s, s->tx_frame, s->tx_frame_len); > > - } else { > > + } else if (s->tx_frame_len <= sizeof(s->tx_frame)) { > > qemu_send_packet(qemu_get_queue(s->nic), > > s->tx_frame, s->tx_frame_len); > > } > > @@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct > > tulip_descriptor *desc) > > } > > } > > > > -static void tulip_copy_tx_buffers(TULIPState *s, struct > > tulip_descriptor *desc) > > +static int tulip_copy_tx_buffers(TULIPState *s, struct > > tulip_descriptor *desc) > > { > > int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT) & > > TDES1_BUF1_SIZE_MASK; > > int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT) & > > TDES1_BUF2_SIZE_MASK; > > > > + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) { > > + return -1; > > + } > > if (len1) { > > pci_dma_read(&s->dev, desc->buf_addr1, > > s->tx_frame + s->tx_frame_len, len1); > > s->tx_frame_len += len1; > > } > > > > + if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) { > > + return -1; > > + } > > if (len2) { > > pci_dma_read(&s->dev, desc->buf_addr2, > > s->tx_frame + s->tx_frame_len, len2); > > s->tx_frame_len += len2; > > } > > desc->status = (len1 + len2) ? 0 : 0x7fffffff; > > + > > + return 0; > > } > > > > static void tulip_setup_filter_addr(TULIPState *s, uint8_t > *buf, > > int n) > > @@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s) > > > > static void tulip_xmit_list_update(TULIPState *s) > > { > > +#define TULIP_DESC_MAX 128 > > + uint8_t i = 0; > > struct tulip_descriptor desc; > > > > if (tulip_ts(s) != CSR5_TS_SUSPENDED) { > > return; > > } > > > > - for (;;) { > > + for (i = 0; i < TULIP_DESC_MAX; i++) { > > tulip_desc_read(s, s->current_tx_desc, &desc); > > tulip_dump_tx_descriptor(s, &desc); > > > > @@ -675,10 +693,10 @@ static void > > tulip_xmit_list_update(TULIPState *s) > > s->tx_frame_len = 0; > > } > > > > - tulip_copy_tx_buffers(s, &desc); > > - > > - if (desc.control & TDES1_LS) { > > - tulip_tx(s, &desc); > > + if (!tulip_copy_tx_buffers(s, &desc)) { > > + if (desc.control & TDES1_LS) { > > + tulip_tx(s, &desc); > > + } > > } > > } > > tulip_desc_write(s, s->current_tx_desc, &desc); > > -- > > 2.25.1 > > > > >
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
Quoted 172 lines · Jason Wang <[email protected]> 于2020年3月27日周五 上午10:53写道:
Quoted 170 lines · Li Qiang
Jason Wang <[email protected] <mailto:[email protected]>> 于2020年3月27日周五 上午10:09写道:
On 2020/3/24 下午10:54, Li Qiang wrote: > > > Jason Wang <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>> > 于2020年3月24日周二 下午1:45写道: > > > On 2020/3/24 上午9:29, Li Qiang wrote: > > > > > > P J P <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>> > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > 于2020年3月23日周一 > > 下午8:24写道: > > > > From: Prasad J Pandit <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > > > Tulip network driver while copying tx/rx buffers does not check > > frame size against r/w data length. This may lead to OOB buffer > > access. Add check to avoid it. > > > > Limit iterations over descriptors to avoid potential infinite > > loop issue in tulip_xmit_list_update. > > > > Reported-by: Li Qiang <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > Reported-by: Ziming Zhang <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > Reported-by: Jason Wang <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > Signed-off-by: Prasad J Pandit <[email protected] <mailto:[email protected]> > <mailto:[email protected] <mailto:[email protected]>> > > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > > > > > > > Tested-by: Li Qiang <[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>> > <mailto:[email protected] <mailto:[email protected]> <mailto:[email protected] <mailto:[email protected]>>>> > > But I have a minor question.... > > > > --- > > hw/net/tulip.c | 36 +++++++++++++++++++++++++++--------- > > 1 file changed, 27 insertions(+), 9 deletions(-) > > > > Update v3: return a value from tulip_copy_tx_buffers() and avoid > > infinite loop > > -> > > https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg06275.html > > > > diff --git a/hw/net/tulip.c b/hw/net/tulip.c > > index cfac2719d3..fbe40095da 100644 > > --- a/hw/net/tulip.c > > +++ b/hw/net/tulip.c > > @@ -170,6 +170,10 @@ static void tulip_copy_rx_bytes(TULIPState > > *s, struct tulip_descriptor *desc) > > } else { > > len = s->rx_frame_len; > > } > > + > > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { > > + return; > > + } > > > > > > > > Why here is '>=' instead of '>'. > > IIUC the total sending length can reach to sizeof(s->rx_frame). > > Same in the other place in this patch. > > > Yes, this need to be fixed. > > > > > > PS: I have planned to write a qtest case. But my personal qemu dev > > environment is broken. > > I will try to write it tonight or tomorrow night. > > > Cool, good to know this. > > > Hi all, > I have countered an interesting issue. Let's look at the definition of > TULIPState. > > 21 typedef struct TULIPState { > 22 PCIDevice dev; > 23 MemoryRegion io; > 24 MemoryRegion memory; > 25 NICConf c; > 26 qemu_irq irq; > 27 NICState *nic; > 28 eeprom_t *eeprom; > 29 uint32_t csr[16]; > 30 > 31 /* state for MII */ > 32 uint32_t old_csr9; > 33 uint32_t mii_word; > 34 uint32_t mii_bitcnt; > 35 > 36 hwaddr current_rx_desc; > 37 hwaddr current_tx_desc; > 38 > 39 uint8_t rx_frame[2048]; > 40 uint8_t tx_frame[2048]; > 41 uint16_t tx_frame_len; > 42 uint16_t rx_frame_len; > 43 uint16_t rx_frame_size; > 44 > 45 uint32_t rx_status; > 46 uint8_t filter[16][6]; > 47 } TULIPState; > > Here we can see the overflow is occured after 'tx_frame'. > In my quest, I have see the overflow(the s->tx_frame_len is big). > However here doesn't cause SEGV in qtest. > In real case, the qemu process will access the data after TULIPState > in heap and trigger segv. > However in qtest mode I don't know how to trigger this. If it's just the mangling of tx_frame_len, it won't hit SIGSEV. I wonder maybe, somehow that large tx_frame_len is used for buffer copying or other stuffs that can lead the crash.
Quoted 10 lines
Quoted 4 lines
This is because in real qemu process, the OOB copy corrupts the head data after 'TULIPState' struct.
And maybe later(other thread) access the corrupted data thus leading crash.Ok, so I think ASAN can detect this crash. But I'm not sure whether or not it was enabled for qtest. If not, we probably need that.
Yes, I think this is the solution.
Quoted 8 lines
I wrote a qtest for virtio-net that can lead OOB, so it should probably work for tulip but need to check.
Or if you don't want to depend on ASAN, we can check user visible status after tx_frame[], but it looks to me all other fields are not visible by guest.
Right, I have spent a lot of time to find a guest visible status but not find it.
Quoted 4 lines
Maybe we can reorder to place csr[] after tx_frame[] then check csr[] afterwards.
I think it's not worth to change this just for a test case.
I will test the ASAN solution asap.
Thanks,
Li Qiang
Quoted 257 lines
Quoted 3 lines
However in qtest mode, I don't remember the core code of qtest. But seems it's not a really VM? just a interface emulation.
If my memory is correct, it's not a VM.
Thanks
Quoted 23 lines
In my case, it's backtrace is as this:
Program received signal SIGSEGV, Segmentation fault. [Switching to Thread0x7ffbdb7fe700(LWP 60813)]0x0000000000000000in ?? ()
...
(gdb) bt
#00x0000000000000000in ()
#10x0000555555a7dfa3inqemu_set_irq(irq=0x5555579e0710, level=1) athw/core/irq.c:44
#20x0000555555b2b87aintulip_update_int(s=0x5555579da7c0) athw/net/tulip.c:121
#30x0000555555b2cdd9intulip_xmit_list_update(s=0x5555579da7c0) athw/net/tulip.c:667
#40x0000555555b2d19dintulip_write(opaque=0x5555579da7c0, addr=32, data=931909632, size=4) athw/net/tulip.c:759
#50x000055555587eed1inmemory_region_write_accessor(mr=0x5555579db0b0, addr=32, value=0x7ffbdb7fd6a8, size=4, shift=0, mask=4294967295, attrs=...) at /xxx/qemu/memory.c:483
#60x000055555587f0dainaccess_with_adjusted_size(addr=32, value=0x7ffbdb7fd6a8, size=4, access_size_min=4, access_size_max=4, access_fn=0x55555587edf2 <memory_region_write_accessor>, mr=0x5555579db0b0, attrs=...)at /xxx/qemu/memory.c:544Quoted 8 lines
#7
0x000055555588213binmemory_region_dispatch_write(mr=0x5555579db0b0, addr=32, data=931909632, op=MO_32, attrs=...) at /xxx/qemu/memory.c:1476
#80x000055555581fe9cinflatview_write_continue(fv=0x7ffbb001eae0, addr=49184, attrs=..., ptr=0x7ffff7e13000, len=4, addr1=32, l=4, mr=0x5555579db0b0) at /xxx/qemu/exec.c:3125
#90x000055555581fff4inflatview_write(fv=0x7ffbb001eae0, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4) at/xxx/qemu/exec.c:3165
Quoted 25 lines
#10
0x0000555555820368inaddress_space_write(as=0x555556762560 <address_space_io>, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4) at /xxx/qemu/exec.c:3256
#110x00005555558203dainaddress_space_rw(as=0x555556762560 <address_space_io>, addr=49184, attrs=..., buf=0x7ffff7e13000, len=4, is_write=true) at /xxx/qemu/exec.c:3266
#120x000055555589723binkvm_handle_io(port=49184, attrs=..., data=0x7ffff7e13000, direction=1, size=4, count=1) at /xxx/qemu/accel/kvm/kvm-all.c:2140
#130x00005555558979d6inkvm_cpu_exec(cpu=0x555556b1e220) at /xxx/qemu/accel/kvm/kvm-all.c:2386
#140x00005555558701c5inqemu_kvm_cpu_thread_fn(arg=0x555556b1e220) at /xxx/qemu/cpus.c:1246
#150x0000555555de3ce4inqemu_thread_start(args=0x555556b44040) atutil/qemu-thread-posix.c:519
#160x00007ffff5bb0e25instart_thread() at/lib64/libpthread.so.0#170x00007ffff58d8f1din clone () at/lib64/libc.so.6I will try to dig into the qtest code and hope find a way to trigger a segv in qtest.
Thanks,
Li QiangQuoted 14 lines
Thanks > > The core code like this: > > qpci_device_enable(dev); > bar = qpci_iomap(dev, 0, NULL); > context_pa = guest_alloc(alloc, sizeof(context)); > guest_pa = guest_alloc(alloc, 4096); > memset(guest_data, 'A', sizeof(guest_data)); > context[0].status = 1 << 31; > context[0].control = 0x7ff << 11 | 0x7ff; > context[0].buf_addr2 = context_pa + sizeof(struct
tulip_descriptor);Quoted 6 lines
Quoted 6 lines
context[0].buf_addr1 = guest_pa; for (i = 1; i < ARRAY_SIZE(context); ++i) { context_pa += sizeof(struct tulip_descriptor); context[i].status = 1 << 31;context[i].control =
0x7ff<< 11 |0x7ff;
context[i].buf_addr2 =context_pa+ sizeof(struct
tulip_descriptor);Quoted 4 lines
Quoted 4 lines
context[i].buf_addr1 =
guest_pa;
}qtest_memwrite(dev->bus->qts,
context_pa, context,sizeof(context));
Quoted 81 lines
Quoted 1 line
qtest_memwrite(dev->bus->qts,
guest_pa,guest_data,sizeof(guest_data)); > qpci_io_writel(dev, bar, 0x20, context_pa); > qpci_io_writel(dev, bar, 0x30, 1 << 13); > > Paolo may give some hints? > > Thanks, > Li Qiang > > Thanks > > > > > > Thanks, > > Li Qiang > > > > > > > > > > pci_dma_write(&s->dev, desc->buf_addr1, s->rx_frame + > > (s->rx_frame_size - s->rx_frame_len), len); > > s->rx_frame_len -= len; > > @@ -181,6 +185,10 @@ static void tulip_copy_rx_bytes(TULIPState > > *s, struct tulip_descriptor *desc) > > } else { > > len = s->rx_frame_len; > > } > > + > > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) { > > + return; > > + } > > pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame + > > (s->rx_frame_size - s->rx_frame_len), len); > > s->rx_frame_len -= len; > > @@ -227,7 +235,8 @@ static ssize_t tulip_receive(TULIPState *s, > > const uint8_t *buf, size_t size) > > > > trace_tulip_receive(buf, size); > > > > - if (size < 14 || size > 2048 || s->rx_frame_len || > > tulip_rx_stopped(s)) { > > + if (size < 14 || size > sizeof(s->rx_frame) - 4 > > + || s->rx_frame_len || tulip_rx_stopped(s)) { > > return 0; > > } > > > > @@ -275,7 +284,6 @@ static ssize_t > tulip_receive_nc(NetClientState > > *nc, > > return tulip_receive(qemu_get_nic_opaque(nc), buf, size); > > } > > > > - > > static NetClientInfo net_tulip_info = { > > .type = NET_CLIENT_DRIVER_NIC, > > .size = sizeof(NICState), > > @@ -558,7 +566,7 @@ static void tulip_tx(TULIPState *s, struct > > tulip_descriptor *desc) > > if ((s->csr[6] >> CSR6_OM_SHIFT) & CSR6_OM_MASK) { > > /* Internal or external Loopback */ > > tulip_receive(s, s->tx_frame, s->tx_frame_len); > > - } else { > > + } else if (s->tx_frame_len <= sizeof(s->tx_frame)) { > > qemu_send_packet(qemu_get_queue(s->nic), > > s->tx_frame, s->tx_frame_len); > > } > > @@ -570,23 +578,31 @@ static void tulip_tx(TULIPState *s, struct > > tulip_descriptor *desc) > > }Quoted 8 lines
> > } > > > > -static void tulip_copy_tx_buffers(TULIPState *s, struct > > tulip_descriptor *desc) > > +static int tulip_copy_tx_buffers(TULIPState *s, struct > > tulip_descriptor *desc) > > { > > int len1 = (desc->control >> TDES1_BUF1_SIZE_SHIFT)&
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
TDES1_BUF1_SIZE_MASK; int len2 = (desc->control >> TDES1_BUF2_SIZE_SHIFT)&
Quoted 68 lines
Quoted 24 lines
Quoted 24 lines
TDES1_BUF2_SIZE_MASK; + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) { + return -1; + } if (len1) { pci_dma_read(&s->dev, desc->buf_addr1, s->tx_frame + s->tx_frame_len, len1); s->tx_frame_len += len1; } + if (s->tx_frame_len + len2 >= sizeof(s->tx_frame)) { + return -1; + } if (len2) { pci_dma_read(&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2); s->tx_frame_len += len2; } desc->status = (len1 + len2) ? 0 : 0x7fffffff; + + return 0; }Quoted 1 line
Quoted 1 line
static void tulip_setup_filter_addr(TULIPState *s,
uint8_t > *buf, > > int n) > > @@ -651,13 +667,15 @@ static uint32_t tulip_ts(TULIPState *s) > > > > static void tulip_xmit_list_update(TULIPState *s) > > { > > +#define TULIP_DESC_MAX 128 > > + uint8_t i = 0; > > struct tulip_descriptor desc; > > > > if (tulip_ts(s) != CSR5_TS_SUSPENDED) { > > return; > > } > > > > - for (;;) { > > + for (i = 0; i < TULIP_DESC_MAX; i++) { > > tulip_desc_read(s, s->current_tx_desc, &desc); > > tulip_dump_tx_descriptor(s, &desc); > > > > @@ -675,10 +693,10 @@ static void > > tulip_xmit_list_update(TULIPState *s) > > s->tx_frame_len = 0; > > } > > > > - tulip_copy_tx_buffers(s, &desc); > > - > > - if (desc.control & TDES1_LS) { > > - tulip_tx(s, &desc); > > + if (!tulip_copy_tx_buffers(s, &desc)) { > > + if (desc.control & TDES1_LS) { > > + tulip_tx(s, &desc); > > + } > > } > > } > > tulip_desc_write(s, s->current_tx_desc, &desc); > > -- > > 2.25.1 > > > > >
Re: [PATCH v6 1/2] net: tulip: check frame size and r/w data length
In reply to Jason Wang+-- On Tue, 24 Mar 2020, Jason Wang wrote --+ | > Why here is '>=' instead of '>'. IIUC the total sending length can reach | > to sizeof(s->rx_frame). Same in the other place in this patch. | | Yes, this need to be fixed.
Sent patch v7. Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D
PATCH v6 2/2 net: tulip: add .can_receive routine
In reply to P J PFrom: Prasad J Pandit <[email protected]>
Define .can_receive routine to do sanity checks before receiving packet data. And call qemu_flush_queued_packets to flush queued packets once they are read in tulip_receive().
--- hw/net/tulip.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
Update v6: merge earlier patch 2 & 3 into one
-> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg06868.html
hw/net/tulip.c +16 -1
@@ -229,6 +229,18 @@ static bool tulip_filter_address(TULIPState *s, const uint8_t *addr)return ret; } +static int +tulip_can_receive (NetClientState *nc) +{ +TULIPState *s =qemu_get_nic_opaque (nc); + +if (s->rx_frame_len ||tulip_rx_stopped (s)) { +return false; + } + +return true; +} +static ssize_t tulip_receive (TULIPState *s,const uint8_t *buf,size_t size) {struct tulip_descriptor desc;
@@ -236,7 +248,7 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)trace_tulip_receive (buf, size);if (size <14 || size >sizeof (s->rx_frame) -4 - || s->rx_frame_len ||tulip_rx_stopped (s)) { + || !tulip_can_receive (s->nic->ncs)) {return 0 ; }
@@ -275,6 +287,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)tulip_desc_write (s, s->current_rx_desc, &desc);tulip_next_rx_descriptor (s, &desc); }while (s->rx_frame_len); + +qemu_flush_queued_packets (qemu_get_queue (s->nic));return size; }
@@ -288,6 +302,7 @@ static NetClientInfo net_tulip_info = { .type = NET_CLIENT_DRIVER_NIC, .size = sizeof(NICState), .receive = tulip_receive_nc, + .can_receive = tulip_can_receive, };static const char *tulip_reg_name (const hwaddr addr)
Signature · 1 line
2.25.1
Re: [PATCH v6 2/2] net: tulip: add .can_receive routine
Quoted 51 lines · P J P <[email protected]> 于2020年3月23日周一 下午8:25写道:
From: Prasad J Pandit <[email protected]>
Define .can_receive routine to do sanity checks before receiving packet data. And call
qemu_flush_queued_packetsto flush queued packets once they are read intulip_receive().Suggested-by Jason Wang <[email protected]>Signed-off-by Prasad J Pandit <[email protected]>--- hw/net/tulip.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)Update v6: merge earlier patch 2 & 3 into one
-> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg06868.htmlhw/net/tulip.c +12
-0@@ -229,6 +229,18 @@ static bool tulip_filter_address(TULIPState *s, const uint8_t *addr)return ret; } +static int +tulip_can_receive (NetClientState *nc) +{ +TULIPState *s =qemu_get_nic_opaque (nc); + +if (s->rx_frame_len ||tulip_rx_stopped (s)) { +return false; + } + +return true; +} +static ssize_t tulip_receive (TULIPState *s,const uint8_t *buf,size_t size){ struct tulip_descriptor desc;hw/net/tulip.c +1
-1@@ -236,7 +248,7 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size)trace_tulip_receive (buf ,size );if (size <14 || size >sizeof (s->rx_frame) -4 - || s->rx_frame_len ||tulip_rx_stopped (s)) { + || !tulip_can_receive (s->nic->ncs)) {return 0 ; }
hw/net/tulip.c static ssize_t tulip_receive(TULIPState *s, const · line 293
Quoted 7 lines
hw/net/tulip.c +2
-0@@ -275,6 +287,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) tulip_desc_write(s, s->current_rx_desc, &desc);tulip_next_rx_descriptor (s, &desc); }while (s->rx_frame_len); + +qemu_flush_queued_packets (qemu_get_queue (s->nic));
Hi Prasad ans Jason,
I want to know why here we need call 'qemu_flush_queued_packets'. AFAICS, the other networking card emulation need no this.
Thanks,
Li Qiang
Quoted 16 lines
hw/net/tulip.c +0
-0return size;}
hw/net/tulip.c +1
-1@@ -288,6 +302,7 @@ static NetClientInfo net_tulip_info = { .type = NET_CLIENT_DRIVER_NIC, .size = sizeof(NICState), .receive = tulip_receive_nc, + .can_receive = tulip_can_receive, };static const char *tulip_reg_name (const hwaddr addr) --2.25.1
Re: [PATCH v6 2/2] net: tulip: add .can_receive routine
Quoted 68 lines · Li Qiang
P J P <[email protected] <mailto:[email protected]>> 于2020年3月23日周一 下午8:25写道:
From: Prasad J Pandit <[email protected] <mailto:[email protected]>> Define .can_receive routine to do sanity checks before receiving packet data. And call qemu_flush_queued_packets to flush queued packets once they are read in tulip_receive(). Suggested-by: Jason Wang <[email protected] <mailto:[email protected]>> Signed-off-by: Prasad J Pandit <[email protected] <mailto:[email protected]>> --- hw/net/tulip.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) Update v6: merge earlier patch 2 & 3 into one -> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg06868.html diff --git a/hw/net/tulip.c b/hw/net/tulip.c index fbe40095da..8d8c9519e7 100644 --- a/hw/net/tulip.c +++ b/hw/net/tulip.c @@ -229,6 +229,18 @@ static bool tulip_filter_address(TULIPState *s, const uint8_t *addr) return ret; } +static int +tulip_can_receive(NetClientState *nc) +{ + TULIPState *s = qemu_get_nic_opaque(nc); + + if (s->rx_frame_len || tulip_rx_stopped(s)) { + return false; + } + + return true; +} + static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) { struct tulip_descriptor desc; @@ -236,7 +248,7 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) trace_tulip_receive(buf, size); if (size < 14 || size > sizeof(s->rx_frame) - 4 - || s->rx_frame_len || tulip_rx_stopped(s)) { + || !tulip_can_receive(s->nic->ncs)) { return 0; } @@ -275,6 +287,8 @@ static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) tulip_desc_write(s, s->current_rx_desc, &desc); tulip_next_rx_descriptor(s, &desc); } while (s->rx_frame_len); + + qemu_flush_queued_packets(qemu_get_queue(s->nic));
Quoted 3 lines
Hi Prasad ans Jason,
I want to know why here we need call 'qemu_flush_queued_packets'. AFAICS, the other networking card emulation need no this.
Right, I thought we need this because rx_frame_len is checked in tulip_can_receive().
But not I think it's unnecessary. According to kernel driver code, the rx is not necessarily stopped when rx buffer is overrun, e.g, tulip_rx_refill() only toggle the doorbell when chip is LC82C168. But own emulation did DC21143.
This means using can_receive() is wrong.
So I think we can drop this patch.
Thanks for the checking and sorry for the wrong suggestion.
Quoted 19 lines
Thanks,
Li Qiangreturn size; } @@ -288,6 +302,7 @@ static NetClientInfo net_tulip_info = { .type = NET_CLIENT_DRIVER_NIC, .size = sizeof(NICState), .receive = tulip_receive_nc, + .can_receive = tulip_can_receive, }; static const char *tulip_reg_name(const hwaddr addr) -- 2.25.1