qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/5] usb: fix some memory allocation issues.
@ 2021-05-03 13:29 Gerd Hoffmann
  2021-05-03 13:29 ` [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation Gerd Hoffmann
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Gerd Hoffmann @ 2021-05-03 13:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: mcascell, remy.noel, philmd, Gerd Hoffmann

v2: add one more fix (Philippe).

Gerd Hoffmann (5):
  usb/hid: avoid dynamic stack allocation
  usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
  usb/mtp: avoid dynamic stack allocation
  usb/xhci: sanity check packet size
  usb: limit combined packets to 1 MiB

 hw/usb/combined-packet.c | 4 +++-
 hw/usb/dev-hid.c         | 2 +-
 hw/usb/dev-mtp.c         | 3 ++-
 hw/usb/dev-wacom.c       | 2 +-
 hw/usb/hcd-xhci.c        | 5 +++++
 hw/usb/redirect.c        | 6 +++---
 6 files changed, 15 insertions(+), 7 deletions(-)

-- 
2.30.2




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

* [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation
  2021-05-03 13:29 [PATCH v2 0/5] usb: fix some memory allocation issues Gerd Hoffmann
@ 2021-05-03 13:29 ` Gerd Hoffmann
  2021-05-03 13:56   ` Philippe Mathieu-Daudé
  2021-05-03 13:29 ` [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527) Gerd Hoffmann
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Gerd Hoffmann @ 2021-05-03 13:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: mcascell, remy.noel, philmd, Gerd Hoffmann

Use autofree heap allocation instead.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb/dev-hid.c   | 2 +-
 hw/usb/dev-wacom.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/hw/usb/dev-hid.c b/hw/usb/dev-hid.c
index fc39bab79f94..1c7ae97c3033 100644
--- a/hw/usb/dev-hid.c
+++ b/hw/usb/dev-hid.c
@@ -656,7 +656,7 @@ static void usb_hid_handle_data(USBDevice *dev, USBPacket *p)
 {
     USBHIDState *us = USB_HID(dev);
     HIDState *hs = &us->hid;
-    uint8_t buf[p->iov.size];
+    g_autofree uint8_t *buf = g_malloc(p->iov.size);
     int len = 0;
 
     switch (p->pid) {
diff --git a/hw/usb/dev-wacom.c b/hw/usb/dev-wacom.c
index b59504863509..ed687bc9f1eb 100644
--- a/hw/usb/dev-wacom.c
+++ b/hw/usb/dev-wacom.c
@@ -301,7 +301,7 @@ static void usb_wacom_handle_control(USBDevice *dev, USBPacket *p,
 static void usb_wacom_handle_data(USBDevice *dev, USBPacket *p)
 {
     USBWacomState *s = (USBWacomState *) dev;
-    uint8_t buf[p->iov.size];
+    g_autofree uint8_t *buf = g_malloc(p->iov.size);
     int len = 0;
 
     switch (p->pid) {
-- 
2.30.2



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

* [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
  2021-05-03 13:29 [PATCH v2 0/5] usb: fix some memory allocation issues Gerd Hoffmann
  2021-05-03 13:29 ` [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation Gerd Hoffmann
@ 2021-05-03 13:29 ` Gerd Hoffmann
  2021-05-03 13:55   ` Philippe Mathieu-Daudé
  2021-05-03 14:25   ` Mauro Matteo Cascella
  2021-05-03 13:29 ` [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation Gerd Hoffmann
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 10+ messages in thread
From: Gerd Hoffmann @ 2021-05-03 13:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: mcascell, remy.noel, philmd, Gerd Hoffmann

Use autofree heap allocation instead.

Fixes: 4f4321c11ff ("usb: use iovecs in USBPacket")
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb/redirect.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/hw/usb/redirect.c b/hw/usb/redirect.c
index 17f06f34179a..6a75b0dc4ab2 100644
--- a/hw/usb/redirect.c
+++ b/hw/usb/redirect.c
@@ -620,7 +620,7 @@ static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
                 .endpoint = ep,
                 .length = p->iov.size
             };
-            uint8_t buf[p->iov.size];
+            g_autofree uint8_t *buf = g_malloc(p->iov.size);
             /* No id, we look at the ep when receiving a status back */
             usb_packet_copy(p, buf, p->iov.size);
             usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
@@ -818,7 +818,7 @@ static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
         usbredirparser_send_bulk_packet(dev->parser, p->id,
                                         &bulk_packet, NULL, 0);
     } else {
-        uint8_t buf[size];
+        g_autofree uint8_t *buf = g_malloc(size);
         usb_packet_copy(p, buf, size);
         usbredir_log_data(dev, "bulk data out:", buf, size);
         usbredirparser_send_bulk_packet(dev->parser, p->id,
@@ -923,7 +923,7 @@ static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev,
                                                USBPacket *p, uint8_t ep)
 {
     struct usb_redir_interrupt_packet_header interrupt_packet;
-    uint8_t buf[p->iov.size];
+    g_autofree uint8_t *buf = g_malloc(p->iov.size);
 
     DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
             p->iov.size, p->id);
-- 
2.30.2



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

* [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation
  2021-05-03 13:29 [PATCH v2 0/5] usb: fix some memory allocation issues Gerd Hoffmann
  2021-05-03 13:29 ` [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation Gerd Hoffmann
  2021-05-03 13:29 ` [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527) Gerd Hoffmann
@ 2021-05-03 13:29 ` Gerd Hoffmann
  2021-05-03 13:55   ` Philippe Mathieu-Daudé
  2021-05-03 13:29 ` [PATCH v2 4/5] usb/xhci: sanity check packet size Gerd Hoffmann
  2021-05-03 13:29 ` [PATCH v2 5/5] usb: limit combined packets to 1 MiB Gerd Hoffmann
  4 siblings, 1 reply; 10+ messages in thread
From: Gerd Hoffmann @ 2021-05-03 13:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: mcascell, remy.noel, philmd, Gerd Hoffmann

Use autofree heap allocation instead.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb/dev-mtp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/hw/usb/dev-mtp.c b/hw/usb/dev-mtp.c
index bbb827434482..2a895a73b083 100644
--- a/hw/usb/dev-mtp.c
+++ b/hw/usb/dev-mtp.c
@@ -907,7 +907,8 @@ static MTPData *usb_mtp_get_object_handles(MTPState *s, MTPControl *c,
                                            MTPObject *o)
 {
     MTPData *d = usb_mtp_data_alloc(c);
-    uint32_t i = 0, handles[o->nchildren];
+    uint32_t i = 0;
+    g_autofree uint32_t *handles = g_new(uint32_t, o->nchildren);
     MTPObject *iter;
 
     trace_usb_mtp_op_get_object_handles(s->dev.addr, o->handle, o->path);
-- 
2.30.2



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

* [PATCH v2 4/5] usb/xhci: sanity check packet size
  2021-05-03 13:29 [PATCH v2 0/5] usb: fix some memory allocation issues Gerd Hoffmann
                   ` (2 preceding siblings ...)
  2021-05-03 13:29 ` [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation Gerd Hoffmann
@ 2021-05-03 13:29 ` Gerd Hoffmann
  2021-05-03 13:29 ` [PATCH v2 5/5] usb: limit combined packets to 1 MiB Gerd Hoffmann
  4 siblings, 0 replies; 10+ messages in thread
From: Gerd Hoffmann @ 2021-05-03 13:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: mcascell, remy.noel, philmd, Gerd Hoffmann

Make sure the usb packet size is within the
bounds of the endpoint configuration.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb/hcd-xhci.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c
index 46212b1e695a..7acfb8137bc9 100644
--- a/hw/usb/hcd-xhci.c
+++ b/hw/usb/hcd-xhci.c
@@ -1568,6 +1568,11 @@ static int xhci_setup_packet(XHCITransfer *xfer)
         qemu_sglist_destroy(&xfer->sgl);
         return -1;
     }
+    if (xfer->packet.iov.size > ep->max_packet_size) {
+        usb_packet_unmap(&xfer->packet, &xfer->sgl);
+        qemu_sglist_destroy(&xfer->sgl);
+        return -1;
+    }
     DPRINTF("xhci: setup packet pid 0x%x addr %d ep %d\n",
             xfer->packet.pid, ep->dev->addr, ep->nr);
     return 0;
-- 
2.30.2



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

* [PATCH v2 5/5] usb: limit combined packets to 1 MiB
  2021-05-03 13:29 [PATCH v2 0/5] usb: fix some memory allocation issues Gerd Hoffmann
                   ` (3 preceding siblings ...)
  2021-05-03 13:29 ` [PATCH v2 4/5] usb/xhci: sanity check packet size Gerd Hoffmann
@ 2021-05-03 13:29 ` Gerd Hoffmann
  4 siblings, 0 replies; 10+ messages in thread
From: Gerd Hoffmann @ 2021-05-03 13:29 UTC (permalink / raw)
  To: qemu-devel; +Cc: mcascell, remy.noel, philmd, Gerd Hoffmann

usb-host and usb-redirect try to batch bulk transfers by combining many
small usb packets into a single, large transfer request, to reduce the
overhead and improve performance.

This patch adds a size limit of 1 MiB for those combined packets to
restrict the host resources the guest can bind that way.

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/usb/combined-packet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/hw/usb/combined-packet.c b/hw/usb/combined-packet.c
index 5d57e883dcb5..e56802f89a32 100644
--- a/hw/usb/combined-packet.c
+++ b/hw/usb/combined-packet.c
@@ -171,7 +171,9 @@ void usb_ep_combine_input_packets(USBEndpoint *ep)
         if ((p->iov.size % ep->max_packet_size) != 0 || !p->short_not_ok ||
                 next == NULL ||
                 /* Work around for Linux usbfs bulk splitting + migration */
-                (totalsize == (16 * KiB - 36) && p->int_req)) {
+                (totalsize == (16 * KiB - 36) && p->int_req) ||
+                /* Next package may grow combined package over 1MiB */
+                totalsize > 1 * MiB - ep->max_packet_size) {
             usb_device_handle_data(ep->dev, first);
             assert(first->status == USB_RET_ASYNC);
             if (first->combined) {
-- 
2.30.2



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

* Re: [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
  2021-05-03 13:29 ` [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527) Gerd Hoffmann
@ 2021-05-03 13:55   ` Philippe Mathieu-Daudé
  2021-05-03 14:25   ` Mauro Matteo Cascella
  1 sibling, 0 replies; 10+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-03 13:55 UTC (permalink / raw)
  To: Gerd Hoffmann, qemu-devel; +Cc: mcascell, remy.noel

On 5/3/21 3:29 PM, Gerd Hoffmann wrote:
> Use autofree heap allocation instead.
> 
> Fixes: 4f4321c11ff ("usb: use iovecs in USBPacket")
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/usb/redirect.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)

Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>



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

* Re: [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation
  2021-05-03 13:29 ` [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation Gerd Hoffmann
@ 2021-05-03 13:55   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 10+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-03 13:55 UTC (permalink / raw)
  To: Gerd Hoffmann, qemu-devel; +Cc: mcascell, remy.noel

On 5/3/21 3:29 PM, Gerd Hoffmann wrote:
> Use autofree heap allocation instead.
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/usb/dev-mtp.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>



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

* Re: [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation
  2021-05-03 13:29 ` [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation Gerd Hoffmann
@ 2021-05-03 13:56   ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 10+ messages in thread
From: Philippe Mathieu-Daudé @ 2021-05-03 13:56 UTC (permalink / raw)
  To: Gerd Hoffmann, qemu-devel; +Cc: mcascell, remy.noel

On 5/3/21 3:29 PM, Gerd Hoffmann wrote:
> Use autofree heap allocation instead.
> 
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/usb/dev-hid.c   | 2 +-
>  hw/usb/dev-wacom.c | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)

Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>



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

* Re: [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
  2021-05-03 13:29 ` [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527) Gerd Hoffmann
  2021-05-03 13:55   ` Philippe Mathieu-Daudé
@ 2021-05-03 14:25   ` Mauro Matteo Cascella
  1 sibling, 0 replies; 10+ messages in thread
From: Mauro Matteo Cascella @ 2021-05-03 14:25 UTC (permalink / raw)
  To: Gerd Hoffmann; +Cc: Remy Noel, Philippe Mathieu Daude, QEMU Developers

On Mon, May 3, 2021 at 3:29 PM Gerd Hoffmann <kraxel@redhat.com> wrote:
>
> Use autofree heap allocation instead.
>
> Fixes: 4f4321c11ff ("usb: use iovecs in USBPacket")
> Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/usb/redirect.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/hw/usb/redirect.c b/hw/usb/redirect.c
> index 17f06f34179a..6a75b0dc4ab2 100644
> --- a/hw/usb/redirect.c
> +++ b/hw/usb/redirect.c
> @@ -620,7 +620,7 @@ static void usbredir_handle_iso_data(USBRedirDevice *dev, USBPacket *p,
>                  .endpoint = ep,
>                  .length = p->iov.size
>              };
> -            uint8_t buf[p->iov.size];
> +            g_autofree uint8_t *buf = g_malloc(p->iov.size);
>              /* No id, we look at the ep when receiving a status back */
>              usb_packet_copy(p, buf, p->iov.size);
>              usbredirparser_send_iso_packet(dev->parser, 0, &iso_packet,
> @@ -818,7 +818,7 @@ static void usbredir_handle_bulk_data(USBRedirDevice *dev, USBPacket *p,
>          usbredirparser_send_bulk_packet(dev->parser, p->id,
>                                          &bulk_packet, NULL, 0);
>      } else {
> -        uint8_t buf[size];
> +        g_autofree uint8_t *buf = g_malloc(size);
>          usb_packet_copy(p, buf, size);
>          usbredir_log_data(dev, "bulk data out:", buf, size);
>          usbredirparser_send_bulk_packet(dev->parser, p->id,
> @@ -923,7 +923,7 @@ static void usbredir_handle_interrupt_out_data(USBRedirDevice *dev,
>                                                 USBPacket *p, uint8_t ep)
>  {
>      struct usb_redir_interrupt_packet_header interrupt_packet;
> -    uint8_t buf[p->iov.size];
> +    g_autofree uint8_t *buf = g_malloc(p->iov.size);
>
>      DPRINTF("interrupt-out ep %02X len %zd id %"PRIu64"\n", ep,
>              p->iov.size, p->id);
> --
> 2.30.2
>

Nitpick: I would probably reference CVE-2021-3527 in patch 4/5 and 5/5
as well. Just to avoid someone from cherry-picking this patch only,
not actually fixing the root cause of the CVE.

Regards.

--
Mauro Matteo Cascella
Red Hat Product Security
PGP-Key ID: BB3410B0



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

end of thread, other threads:[~2021-05-03 14:26 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-03 13:29 [PATCH v2 0/5] usb: fix some memory allocation issues Gerd Hoffmann
2021-05-03 13:29 ` [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation Gerd Hoffmann
2021-05-03 13:56   ` Philippe Mathieu-Daudé
2021-05-03 13:29 ` [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527) Gerd Hoffmann
2021-05-03 13:55   ` Philippe Mathieu-Daudé
2021-05-03 14:25   ` Mauro Matteo Cascella
2021-05-03 13:29 ` [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation Gerd Hoffmann
2021-05-03 13:55   ` Philippe Mathieu-Daudé
2021-05-03 13:29 ` [PATCH v2 4/5] usb/xhci: sanity check packet size Gerd Hoffmann
2021-05-03 13:29 ` [PATCH v2 5/5] usb: limit combined packets to 1 MiB Gerd Hoffmann

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