net: tulip: check frame size and r/w data length
PATCH v2 1/1 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.
--- hw/net/tulip.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)
Update v2: retain earlier len[12] & s->rx_frame_len checks
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg04160.html
hw/net/tulip.c +17 -2
@@ -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 ; }
@@ -558,7 +567,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); }
@@ -575,12 +584,18 @@ static void 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 ; + }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 ; + }if (len2) {pci_dma_read (&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2);
Signature · 1 line
2.24.1
Quoted 21 lines · P J P
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.
Reported-by Li Qiang <[email protected]>Reported-by Ziming Zhang <[email protected]>Signed-off-by Prasad J Pandit <[email protected]>--- hw/net/tulip.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-)Update v2: retain earlier len[12] &
s->rx_frame_lenchecks
-> https://lists.gnu.org/archive/html/qemu-devel/2020-02/msg04160.htmlhw/net/tulip.c +0
-0
hw/net/tulip.c static void tulip_copy_rx_bytes(TULIPState *s, struct tulip_descriptor *desc) · line 176
Quoted 7 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 ; + }
What's the goal of this checking?
Quoted 15 lines
hw/net/tulip.c +4
-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;@@ -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 static ssize_t tulip_receive(TULIPState *s, const uint8_t *buf, size_t size) · line 240
Quoted 7 lines
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 ;
It's better to move those checks in .can_receive().
hw/net/tulip.c static void tulip_tx(TULIPState *s, struct tulip_descriptor *desc) · line 570
Quoted 8 lines
hw/net/tulip.c +1
-1}@@ -558,7 +567,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)) {
Should we use <= here?
hw/net/tulip.c static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) · line 589
Quoted 10 lines
hw/net/tulip.c +3
-0qemu_send_packet (qemu_get_queue (s->nic), s->tx_frame, s->tx_frame_len); }@@ -575,12 +584,18 @@ static void 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 ; + }
I think it's better to add a return value here to make sure caller tulip_xmit_list_update() can exit the loop early
hw/net/tulip.c static void tulip_copy_tx_buffers(TULIPState *s, struct tulip_descriptor *desc) · line 601
Quoted 12 lines
hw/net/tulip.c +3
-0if (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 ; + }if (len2) {pci_dma_read (&s->dev, desc->buf_addr2, s->tx_frame + s->tx_frame_len, len2);
One more thing.
It looks to me there could be a user trigger-able infinite loop in tun_list_update() through always set TDES0_OWN in its descriptors?
Thanks
+-- On Mon, 24 Feb 2020, Jason Wang wrote --+
| > + if (s->rx_frame_len + len >= sizeof(s->rx_frame)) {
| > + return;
| > + }
| What's the goal of this checking? To avoid potential OOB access while in pci_dma_write().
| > pci_dma_write(&s->dev, desc->buf_addr2, s->rx_frame +
| > (s->rx_frame_size - s->rx_frame_len), len);
|
| > + if (size < 14 || size > sizeof(s->rx_frame) - 4
| > + || s->rx_frame_len || tulip_rx_stopped(s)) {
| > return 0;
| It's better to move those checks in .can_receive(). I tried to move these checks to .can_recieve, but tulip_receive()
is called from tulip_receive_nc() and tulip_tx() and the 'size' check could not be moved to can_receive().
| > + } else if (s->tx_frame_len < sizeof(s->tx_frame)) {
| Should we use <= here?
Done.
| > + if (s->tx_frame_len + len1 >= sizeof(s->tx_frame)) {
| > + return;
| > + }
| I think it's better to add a return value here to make sure caller
| tulip_xmit_list_update() can exit the loop earlyDone.
| One more thing. | It looks to me there could be a user trigger-able infinite loop in | tun_list_update() through always set TDES0_OWN in its descriptors?
Yes, defined TULIP_DESC_MAX=128 to restrict loop iterations to 128
descriptors to avoid potential infinite loop.
-> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/net/ethernet/dec/tulip/tulip.h#n319
Sent patch v3 with above changes; Sorry about the delay.
Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
8685 545E B54C 486B C6EB 271E E285 8B5A F050 DE8D