Skip to contentSkip to conversation

usb: fix some memory allocation issues.

PATCH Superseded Gerd Hoffmann 2 participants 9 messages May 3, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover usb: fix some memory allocation issues.

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        | 4 ++--
 6 files changed, 14 insertions(+), 6 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/5 usb/hid: avoid dynamic stack allocation

Use autofree heap allocation instead.

---
 hw/usb/dev-hid.c   | 2 +-
 hw/usb/dev-wacom.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
hw/usb/dev-hid.c +1 -1
index fc39bab79f94..1c7ae97c3033 100644
@@ -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) {
hw/usb/dev-wacom.c +1 -1
index b59504863509..ed687bc9f1eb 100644
@@ -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) {
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/5 usb/redir: avoid dynamic stack allocation (CVE-2021-3527)

In reply to Gerd Hoffmann

Use autofree heap allocation instead.

---
 hw/usb/redirect.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/usb/redirect.c +2 -2
index 17f06f34179a..db1a89a7cc92 100644
@@ -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);
Signature · 1 line
2.30.2
Reviewed-by on v1 2/5
#3 lore raw

Re: [PATCH 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)

Quoted 3 lines · Gerd Hoffmann

Use autofree heap allocation instead.

Fixes 4f4321c11ff ("usb: use iovecs in USBPacket")
Quoted 27 lines
---
 hw/usb/redirect.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
hw/usb/redirect.c +2 -2
index 17f06f34179a..db1a89a7cc92 100644
@@ -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);
#4 lore raw

Re: [PATCH 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)

Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 3 lines · Gerd Hoffmann

Use autofree heap allocation instead.

Fixes 4f4321c11ff ("usb: use iovecs in USBPacket")

Hmm there is still another use:

618 if (dev->endpoint[EP2I(ep)].iso_started) {
619 struct usb_redir_iso_packet_header iso_packet = {
620 .endpoint = ep,
621 .length = p->iov.size
622 };
623 uint8_t buf[p->iov.size];

#5 lore raw

PATCH 3/5 usb/mtp: avoid dynamic stack allocation

In reply to Gerd Hoffmann

Use autofree heap allocation instead.

---
 hw/usb/dev-mtp.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/usb/dev-mtp.c +2 -1
index bbb827434482..2a895a73b083 100644
@@ -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);
Signature · 1 line
2.30.2
#6 lore raw

PATCH 4/5 usb/xhci: sanity check packet size

In reply to Gerd Hoffmann

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

---
 hw/usb/hcd-xhci.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/hcd-xhci.c +5 -0
index 46212b1e695a..7acfb8137bc9 100644
@@ -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;
Signature · 1 line
2.30.2
#7 lore raw

PATCH 5/5 usb: limit combined packets to 1 MiB

In reply to 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.

---
 hw/usb/combined-packet.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
hw/usb/combined-packet.c +3 -1
index 5d57e883dcb5..e56802f89a32 100644
@@ -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) {
Signature · 1 line
2.30.2
#8 lore raw
In reply to Gerd Hoffmann
Quoted 8 lines · Gerd Hoffmann

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

What about enabling -Wvla by default?

-Wvla
Warn if a variable-length array is used in the code.

Most of our variable-length stack alloc could use some LENGTH_MAX definition or use the heap:

[2/1072] Compiling C object libqemuutil.a.p/util_iov.c.o
FAILED: libqemuutil.a.p/util_iov.c.o
util/iov.c: In function ‘qemu_iovec_clone’:
util/iov.c:626:5: error: ISO C90 forbids variable length array ‘sortelems’ [-Werror=vla]

  626 |     IOVectorSortElem sortelems[src->niov];
      |     ^~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
[125/1072] Compiling C object libio.fa.p/io_channel-websock.c.o
FAILED: libio.fa.p/io_channel-websock.c.o io/channel-websock.c: In function ‘qio_channel_websock_handshake_send_res_ok’:
io/channel-websock.c:350:23: error: ISO C90 forbids array ‘combined_key’ whose size cannot be evaluated [-Werror=vla]

  350 |                       QIO_CHANNEL_WEBSOCK_GUID_LEN + 1];
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

cc1: all warnings being treated as errors
[177/1072] Compiling C object libblock.fa.p/block_vpc.c.o
FAILED: libblock.fa.p/block_vpc.c.o
block/vpc.c: In function ‘get_image_offset’:
block/vpc.c:512:9: error: ISO C90 forbids variable length array ‘bitmap’ [-Werror=vla]

  512 |         uint8_t bitmap[s->bitmap_size];
      |         ^~~~~~~
block/vpc.c: In function ‘alloc_block’:
block/vpc.c:559:5: error: ISO C90 forbids variable length array ‘bitmap’
[-Werror=vla]
  559 |     uint8_t bitmap[s->bitmap_size];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[278/1072] Compiling C object libcommon.fa.p/ui_vnc-enc-hextile.c.o
FAILED: libcommon.fa.p/ui_vnc-enc-hextile.c.o
In file included from ui/vnc-enc-hextile.c:37:
ui/vnc-enc-hextile-template.h: In function ‘send_hextile_tile_32’:
ui/vnc-enc-hextile-template.h:28:5: error: ISO C90 forbids variable length array ‘data’ [-Werror=vla]

   28 |     uint8_t data[(vs->client_pf.bytes_per_pixel + 2) * 16 * 16];
      |     ^~~~~~~

In file included from ui/vnc-enc-hextile.c:42:
ui/vnc-enc-hextile-template.h: In function ‘send_hextile_tile_generic_32’:
ui/vnc-enc-hextile-template.h:28:5: error: ISO C90 forbids variable length array ‘data’ [-Werror=vla]

   28 |     uint8_t data[(vs->client_pf.bytes_per_pixel + 2) * 16 * 16];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[283/1072] Compiling C object libcommon.fa.p/ui_vnc-enc-tight.c.o
FAILED: libcommon.fa.p/ui_vnc-enc-tight.c.o ui/vnc-enc-tight.c: In function ‘send_palette_rect’:
ui/vnc-enc-tight.c:1101:9: error: ISO C90 forbids variable length array ‘header’ [-Werror=vla]

 1101 |         uint32_t header[palette_size(palette)];
      |         ^~~~~~~~
ui/vnc-enc-tight.c:1118:9: error: ISO C90 forbids variable length array
‘header’ [-Werror=vla]
 1118 |         uint16_t header[palette_size(palette)];
      |         ^~~~~~~~

cc1: all warnings being treated as errors
[353/1072] Compiling C object libcommon.fa.p/net_dump.c.o
FAILED: libcommon.fa.p/net_dump.c.o
net/dump.c: In function ‘dump_receive_iov’:
net/dump.c:71:12: error: ISO C90 forbids variable length array ‘dumpiov’ [-Werror=vla]

   71 |     struct iovec dumpiov[cnt + 1];
      |            ^~~~~

cc1: all warnings being treated as errors
[375/1072] Compiling C object libcommon.fa.p/net_tap.c.o
FAILED: libcommon.fa.p/net_tap.c.o
net/tap.c: In function ‘tap_receive_iov’:
net/tap.c:123:12: error: ISO C90 forbids variable length array ‘iov_copy’ [-Werror=vla]

  123 |     struct iovec iov_copy[iovcnt + 1];
      |            ^~~~~

cc1: all warnings being treated as errors
[451/1072] Compiling C object libcommon.fa.p/hw_block_nvme.c.o
FAILED: libcommon.fa.p/hw_block_nvme.c.o
hw/block/nvme.c: In function ‘nvme_map_prp’:
hw/block/nvme.c:655:13: error: ISO C90 forbids variable length array ‘prp_list’ [-Werror=vla]

  655 |             uint64_t prp_list[n->max_prp_ents];
      |             ^~~~~~~~
hw/block/nvme.c: In function ‘nvme_map_sgl’:
hw/block/nvme.c:817:5: error: ISO C90 forbids variable length array
‘segment’ [-Werror=vla]
  817 |     NvmeSglDescriptor segment[SEG_CHUNK_SIZE], *sgld, *last_sgld;
      |     ^~~~~~~~~~~~~~~~~
hw/block/nvme.c: In function ‘nvme_dsm’:
hw/block/nvme.c:2513:9: error: ISO C90 forbids variable length array
‘range’ [-Werror=vla]
 2513 |         NvmeDsmRange range[nr];
      |         ^~~~~~~~~~~~

cc1: all warnings being treated as errors
[653/1072] Compiling C object libcommon.fa.p/hw_net_e1000e_core.c.o
FAILED: libcommon.fa.p/hw_net_e1000e_core.c.o hw/net/e1000e_core.c: In function ‘e1000e_receive_iov’:
hw/net/e1000e_core.c:1632:5: error: ISO C90 forbids variable length array ‘min_buf’ [-Werror=vla]

 1632 |     uint8_t min_buf[min_buf_size];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[663/1072] Compiling C object libcommon.fa.p/hw_net_rocker_rocker_of_dpa.c.o
FAILED: libcommon.fa.p/hw_net_rocker_rocker_of_dpa.c.o hw/net/rocker/rocker_of_dpa.c: In function ‘of_dpa_ig’:
hw/net/rocker/rocker_of_dpa.c:1046:12: error: ISO C90 forbids variable length array ‘iov_copy’ [-Werror=vla]

 1046 |     struct iovec iov_copy[iovcnt + 2];
      |            ^~~~~

cc1: all warnings being treated as errors
[778/1072] Compiling C object libcommon.fa.p/hw_usb_hcd-ohci.c.o
FAILED: libcommon.fa.p/hw_usb_hcd-ohci.c.o hw/usb/hcd-ohci.c: In function ‘ohci_td_pkt’:
hw/usb/hcd-ohci.c:903:5: error: ISO C90 forbids variable length array ‘tmp’ [-Werror=vla]

  903 |     char tmp[3 * width + 1];
      |     ^~~~

cc1: all warnings being treated as errors
[787/1072] Compiling C object libcommon.fa.p/hw_usb_hcd-xhci.c.o
FAILED: libcommon.fa.p/hw_usb_hcd-xhci.c.o hw/usb/hcd-xhci.c: In function ‘xhci_get_port_bandwidth’:
hw/usb/hcd-xhci.c:2385:5: error: ISO C90 forbids variable length array ‘bw_ctx’ [-Werror=vla]

 2385 |     uint8_t bw_ctx[xhci->numports+1];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[791/1072] Compiling C object libcommon.fa.p/hw_usb_dev-hid.c.o
FAILED: libcommon.fa.p/hw_usb_dev-hid.c.o hw/usb/dev-hid.c: In function ‘usb_hid_handle_data’:
hw/usb/dev-hid.c:659:5: error: ISO C90 forbids variable length array ‘buf’ [-Werror=vla]

  659 |     uint8_t buf[p->iov.size];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[794/1072] Compiling C object libcommon.fa.p/hw_usb_dev-wacom.c.o
FAILED: libcommon.fa.p/hw_usb_dev-wacom.c.o hw/usb/dev-wacom.c: In function ‘usb_wacom_handle_data’:
hw/usb/dev-wacom.c:304:5: error: ISO C90 forbids variable length array ‘buf’ [-Werror=vla]

  304 |     uint8_t buf[p->iov.size];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[807/1072] Compiling C object libcommon.fa.p/hw_usb_dev-mtp.c.o
FAILED: libcommon.fa.p/hw_usb_dev-mtp.c.o hw/usb/dev-mtp.c: In function ‘usb_mtp_get_object_handles’:
hw/usb/dev-mtp.c:910:5: error: ISO C90 forbids variable length array ‘handles’ [-Werror=vla]

  910 |     uint32_t i = 0, handles[o->nchildren];
      |     ^~~~~~~~

cc1: all warnings being treated as errors
[818/1072] Compiling C object libcommon.fa.p/chardev_baum.c.o
FAILED: libcommon.fa.p/chardev_baum.c.o
chardev/baum.c: In function ‘baum_write_packet’:
chardev/baum.c:299:5: error: ISO C90 forbids variable length array ‘io_buf’ [-Werror=vla]

  299 |     uint8_t io_buf[1 + 2 * len], *cur = io_buf;
      |     ^~~~~~~
chardev/baum.c: In function ‘baum_eat_packet’:
chardev/baum.c:383:9: error: ISO C90 forbids variable length array
‘cells’ [-Werror=vla]
  383 |         uint8_t cells[baum->x * baum->y], c;
      |         ^~~~~~~
chardev/baum.c:384:9: error: ISO C90 forbids variable length array
‘text’ [-Werror=vla]
  384 |         uint8_t text[baum->x * baum->y];
      |         ^~~~~~~
chardev/baum.c:385:9: error: ISO C90 forbids variable length array
‘zero’ [-Werror=vla]
  385 |         uint8_t zero[baum->x * baum->y];
      |         ^~~~~~~

cc1: all warnings being treated as errors
[825/1072] Compiling C object libcommon.fa.p/hw_usb_redirect.c.o
FAILED: libcommon.fa.p/hw_usb_redirect.c.o hw/usb/redirect.c: In function ‘usbredir_handle_iso_data’:
hw/usb/redirect.c:623:13: error: ISO C90 forbids variable length array ‘buf’ [-Werror=vla]

  623 |             uint8_t buf[p->iov.size];
      |             ^~~~~~~
hw/usb/redirect.c: In function ‘usbredir_handle_bulk_data’:
hw/usb/redirect.c:821:9: error: ISO C90 forbids variable length array
‘buf’ [-Werror=vla]
  821 |         uint8_t buf[size];
      |         ^~~~~~~
hw/usb/redirect.c: In function ‘usbredir_handle_interrupt_out_data’:
hw/usb/redirect.c:926:5: error: ISO C90 forbids variable length array
‘buf’ [-Werror=vla]
  926 |     uint8_t buf[p->iov.size];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[830/1072] Compiling C object libcommon.fa.p/ui_curses.c.o
FAILED: libcommon.fa.p/ui_curses.c.o
ui/curses.c: In function ‘curses_update’:
ui/curses.c:68:5: error: ISO C90 forbids variable length array ‘curses_line’ [-Werror=vla]

   68 |     cchar_t curses_line[width];
      |     ^~~~~~~

cc1: all warnings being treated as errors
[852/1072] Compiling C object libcommon.fa.p/ui_spice-display.c.o
FAILED: libcommon.fa.p/ui_spice-display.c.o ui/spice-display.c: In function ‘qemu_spice_create_update’:
ui/spice-display.c:191:5: error: ISO C90 forbids variable length array ‘dirty_top’ [-Werror=vla]

  191 |     int dirty_top[blocks];
      |     ^~~

cc1: all warnings being treated as errors
[987/1072] Compiling C object libqemu-arm-softmmu.fa.p/hw_block_dataplane_virtio-blk.c.o
FAILED: libqemu-arm-softmmu.fa.p/hw_block_dataplane_virtio-blk.c.o hw/block/dataplane/virtio-blk.c: In function ‘notify_guest_bh’:
hw/block/dataplane/virtio-blk.c:63:5: error: ISO C90 forbids variable length array ‘bitmap’ [-Werror=vla]

   63 |     unsigned long bitmap[BITS_TO_LONGS(nvqs)];
      |     ^~~~~~~~

cc1: all warnings being treated as errors
[1066/1072] Compiling C object libqemu-arm-softmmu.fa.p/accel_tcg_translate-all.c.o
ninja: build stopped: cannot make progress due to previous errors.