usb: fix some memory allocation issues.
v2 cover usb: fix some memory allocation issues.
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(-)
Signature · 1 line
2.30.2
PATCH v2 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
@@ -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
@@ -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
Re: [PATCH v2 1/5] usb/hid: avoid dynamic stack allocation
Quoted 7 lines · Gerd Hoffmann
Use autofree heap allocation instead.
Signed-off-by Gerd Hoffmann <[email protected]>--- hw/usb/dev-hid.c | 2 +- hw/usb/dev-wacom.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
PATCH v2 2/5 usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
In reply to Gerd HoffmannUse autofree heap allocation instead.
--- hw/usb/redirect.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
hw/usb/redirect.c +3 -3
@@ -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);
Signature · 1 line
2.30.2
Re: [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
Quoted 8 lines · Gerd Hoffmann
Use autofree heap allocation instead.
Fixes 4f4321c11ff ("usb: use iovecs in USBPacket")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Gerd Hoffmann <[email protected]>--- hw/usb/redirect.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Re: [PATCH v2 2/5] usb/redir: avoid dynamic stack allocation (CVE-2021-3527)
In reply to Gerd HoffmannQuoted 42 lines · Gerd Hoffmann
Use autofree heap allocation instead.
Fixes 4f4321c11ff ("usb: use iovecs in USBPacket")Reviewed-by Philippe Mathieu-Daudé <[email protected]>Signed-off-by Gerd Hoffmann <[email protected]>--- hw/usb/redirect.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)hw/usb/redirect.c +3
-4@@ -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); --
Quoted 2 lines
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
PATCH v2 3/5 usb/mtp: avoid dynamic stack allocation
In reply to Gerd HoffmannUse autofree heap allocation instead.
--- hw/usb/dev-mtp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/usb/dev-mtp.c +2 -1
@@ -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
Re: [PATCH v2 3/5] usb/mtp: avoid dynamic stack allocation
Quoted 6 lines · Gerd Hoffmann
Use autofree heap allocation instead.
Signed-off-by Gerd Hoffmann <[email protected]>--- hw/usb/dev-mtp.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
PATCH v2 4/5 usb/xhci: sanity check packet size
In reply to Gerd HoffmannMake 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
@@ -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
PATCH v2 5/5 usb: limit combined packets to 1 MiB
In reply to Gerd Hoffmannusb-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
@@ -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