diff mbox series

[v2] net: tulip: check frame size and r/w data length

Message ID 20200217113804.341836-1-ppandit@redhat.com (mailing list archive)
State New, archived
Headers show
Series [v2] net: tulip: check frame size and r/w data length | expand

Commit Message

Prasad Pandit Feb. 17, 2020, 11:38 a.m. UTC
From: Prasad J Pandit <pjp@fedoraproject.org>

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 <pangpei.lq@antfin.com>
Reported-by: Ziming Zhang <ezrakiez@gmail.com>
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
---
 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

Comments

Jason Wang Feb. 24, 2020, 5:58 a.m. UTC | #1
On 2020/2/17 下午7:38, P J P wrote:
> From: Prasad J Pandit <pjp@fedoraproject.org>
>
> 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 <pangpei.lq@antfin.com>
> Reported-by: Ziming Zhang <ezrakiez@gmail.com>
> Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
> ---
>   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
>
> diff --git a/hw/net/tulip.c b/hw/net/tulip.c
> index cfac2719d3..ea4fd371e3 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;
> +        }


What's the goal of this checking?


>           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;


It's better to move those checks in .can_receive().


>       }
>   
> @@ -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?


>               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;
> +    }


I think it's better to add a return value here to make sure caller 
tulip_xmit_list_update() can exit the loop early


>       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);


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
Prasad Pandit March 3, 2020, 10:57 a.m. UTC | #2
+-- 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 early

  Done.
 
| 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
diff mbox series

Patch

diff --git a/hw/net/tulip.c b/hw/net/tulip.c
index cfac2719d3..ea4fd371e3 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;
+        }
         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);