qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v7] net: tulip: check frame size and r/w data length
@ 2020-03-24 17:27 P J P
  2020-03-26 11:11 ` Li Qiang
  0 siblings, 1 reply; 3+ messages in thread
From: P J P @ 2020-03-24 17:27 UTC (permalink / raw)
  To: Jason Wang
  Cc: Prasad J Pandit, Stefan Hajnoczi, Sven Schnelle, Qemu Developers,
	Li Qiang, Philippe Mathieu-Daudé,
	Ziming Zhang

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.

Limit iterations over descriptors to avoid potential infinite
loop issue in tulip_xmit_list_update.

Reported-by: Li Qiang <pangpei.lq@antfin.com>
Reported-by: Ziming Zhang <ezrakiez@gmail.com>
Reported-by: Jason Wang <jasowang@redhat.com>
Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
---
 hw/net/tulip.c | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

Update v7: fix length check expression to replace '>=' with '>'
  -> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg07160.html

diff --git a/hw/net/tulip.c b/hw/net/tulip.c
index cfac2719d3..1295f51d07 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;
     }
 
@@ -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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v7] net: tulip: check frame size and r/w data length
  2020-03-24 17:27 [PATCH v7] net: tulip: check frame size and r/w data length P J P
@ 2020-03-26 11:11 ` Li Qiang
  2020-03-27  1:56   ` Jason Wang
  0 siblings, 1 reply; 3+ messages in thread
From: Li Qiang @ 2020-03-26 11:11 UTC (permalink / raw)
  To: P J P
  Cc: Prasad J Pandit, Stefan Hajnoczi, Jason Wang, Qemu Developers,
	Philippe Mathieu-Daudé,
	Li Qiang, Sven Schnelle, Ziming Zhang

[-- Attachment #1: Type: text/plain, Size: 5377 bytes --]

P J P <ppandit@redhat.com> 于2020年3月25日周三 上午1:31写道:

> 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.
>
> Limit iterations over descriptors to avoid potential infinite
> loop issue in tulip_xmit_list_update.
>
> Reported-by: Li Qiang <pangpei.lq@antfin.com>
> Reported-by: Ziming Zhang <ezrakiez@gmail.com>
> Reported-by: Jason Wang <jasowang@redhat.com>
> Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org>
>


Tested-by: Li Qiang <liq3ea@gmail.com>
Reviewed-by: Li Qiang <liq3ea@gmail.com>

Thanks,
Li Qiang



> ---
>  hw/net/tulip.c | 36 +++++++++++++++++++++++++++---------
>  1 file changed, 27 insertions(+), 9 deletions(-)
>
> Update v7: fix length check expression to replace '>=' with '>'
>   -> https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg07160.html
>
> diff --git a/hw/net/tulip.c b/hw/net/tulip.c
> index cfac2719d3..1295f51d07 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;
>      }
>
> @@ -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
>
>
>

[-- Attachment #2: Type: text/html, Size: 7372 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v7] net: tulip: check frame size and r/w data length
  2020-03-26 11:11 ` Li Qiang
@ 2020-03-27  1:56   ` Jason Wang
  0 siblings, 0 replies; 3+ messages in thread
From: Jason Wang @ 2020-03-27  1:56 UTC (permalink / raw)
  To: Li Qiang, P J P
  Cc: Prasad J Pandit, Stefan Hajnoczi, Qemu Developers,
	Philippe Mathieu-Daudé,
	Li Qiang, Sven Schnelle, Ziming Zhang


On 2020/3/26 下午7:11, Li Qiang wrote:
>
>
> P J P <ppandit@redhat.com <mailto:ppandit@redhat.com>> 于2020年3月25日周三 
> 上午1:31写道:
>
>     From: Prasad J Pandit <pjp@fedoraproject.org
>     <mailto: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.
>
>     Limit iterations over descriptors to avoid potential infinite
>     loop issue in tulip_xmit_list_update.
>
>     Reported-by: Li Qiang <pangpei.lq@antfin.com
>     <mailto:pangpei.lq@antfin.com>>
>     Reported-by: Ziming Zhang <ezrakiez@gmail.com
>     <mailto:ezrakiez@gmail.com>>
>     Reported-by: Jason Wang <jasowang@redhat.com
>     <mailto:jasowang@redhat.com>>
>     Signed-off-by: Prasad J Pandit <pjp@fedoraproject.org
>     <mailto:pjp@fedoraproject.org>>
>
>
>
> Tested-by: Li Qiang <liq3ea@gmail.com <mailto:liq3ea@gmail.com>>
> Reviewed-by: Li Qiang <liq3ea@gmail.com <mailto:liq3ea@gmail.com>>
>
> Thanks,
> Li Qiang


Applied.

Thanks


>
>     ---
>      hw/net/tulip.c | 36 +++++++++++++++++++++++++++---------
>      1 file changed, 27 insertions(+), 9 deletions(-)
>
>     Update v7: fix length check expression to replace '>=' with '>'
>       ->
>     https://lists.gnu.org/archive/html/qemu-devel/2020-03/msg07160.html
>
>     diff --git a/hw/net/tulip.c b/hw/net/tulip.c
>     index cfac2719d3..1295f51d07 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;
>          }
>
>     @@ -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
>
>



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-27  1:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-24 17:27 [PATCH v7] net: tulip: check frame size and r/w data length P J P
2020-03-26 11:11 ` Li Qiang
2020-03-27  1:56   ` Jason Wang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).