902636
From feb16ff29a13a4286389bb8b9d4f541aab9b84f1 Mon Sep 17 00:00:00 2001
42c570
From: Jon Maloy <jmaloy@redhat.com>
42c570
Date: Thu, 3 Sep 2020 15:27:13 -0400
42c570
Subject: [PATCH] usb: fix setup_len init (CVE-2020-14364)
42c570
42c570
RH-Author: Jon Maloy <jmaloy@redhat.com>
42c570
Message-id: <20200903152713.1420531-2-jmaloy@redhat.com>
42c570
Patchwork-id: 98271
42c570
O-Subject: [RHEL-8.3.0 qemu-kvm PATCH 1/1] usb: fix setup_len init (CVE-2020-14364)
902636
Bugzilla: 1869710
42c570
RH-Acked-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
42c570
RH-Acked-by: Thomas Huth <thuth@redhat.com>
42c570
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
42c570
42c570
From: Gerd Hoffmann <kraxel@redhat.com>
42c570
42c570
Store calculated setup_len in a local variable, verify it, and only
42c570
write it to the struct (USBDevice->setup_len) in case it passed the
42c570
sanity checks.
42c570
42c570
This prevents other code (do_token_{in,out} functions specifically)
42c570
from working with invalid USBDevice->setup_len values and overrunning
42c570
the USBDevice->setup_buf[] buffer.
42c570
42c570
Fixes: CVE-2020-14364
42c570
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
42c570
Tested-by: Gonglei <arei.gonglei@huawei.com>
42c570
Reviewed-by: Li Qiang <liq3ea@gmail.com>
42c570
Message-id: 20200825053636.29648-1-kraxel@redhat.com
42c570
(cherry picked from commit b946434f2659a182afc17e155be6791ebfb302eb)
42c570
Signed-off-by: Jon Maloy <jmaloy@redhat.com>
42c570
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
42c570
---
42c570
 hw/usb/core.c | 16 ++++++++++------
42c570
 1 file changed, 10 insertions(+), 6 deletions(-)
42c570
42c570
diff --git a/hw/usb/core.c b/hw/usb/core.c
902636
index 5abd128b6b..5234dcc73f 100644
42c570
--- a/hw/usb/core.c
42c570
+++ b/hw/usb/core.c
902636
@@ -129,6 +129,7 @@ void usb_wakeup(USBEndpoint *ep, unsigned int stream)
42c570
 static void do_token_setup(USBDevice *s, USBPacket *p)
42c570
 {
42c570
     int request, value, index;
42c570
+    unsigned int setup_len;
42c570
 
42c570
     if (p->iov.size != 8) {
42c570
         p->status = USB_RET_STALL;
902636
@@ -138,14 +139,15 @@ static void do_token_setup(USBDevice *s, USBPacket *p)
42c570
     usb_packet_copy(p, s->setup_buf, p->iov.size);
42c570
     s->setup_index = 0;
42c570
     p->actual_length = 0;
42c570
-    s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
42c570
-    if (s->setup_len > sizeof(s->data_buf)) {
42c570
+    setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
42c570
+    if (setup_len > sizeof(s->data_buf)) {
42c570
         fprintf(stderr,
42c570
                 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
42c570
-                s->setup_len, sizeof(s->data_buf));
42c570
+                setup_len, sizeof(s->data_buf));
42c570
         p->status = USB_RET_STALL;
42c570
         return;
42c570
     }
42c570
+    s->setup_len = setup_len;
42c570
 
42c570
     request = (s->setup_buf[0] << 8) | s->setup_buf[1];
42c570
     value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
902636
@@ -259,26 +261,28 @@ static void do_token_out(USBDevice *s, USBPacket *p)
42c570
 static void do_parameter(USBDevice *s, USBPacket *p)
42c570
 {
42c570
     int i, request, value, index;
42c570
+    unsigned int setup_len;
42c570
 
42c570
     for (i = 0; i < 8; i++) {
42c570
         s->setup_buf[i] = p->parameter >> (i*8);
42c570
     }
42c570
 
42c570
     s->setup_state = SETUP_STATE_PARAM;
42c570
-    s->setup_len   = (s->setup_buf[7] << 8) | s->setup_buf[6];
42c570
     s->setup_index = 0;
42c570
 
42c570
     request = (s->setup_buf[0] << 8) | s->setup_buf[1];
42c570
     value   = (s->setup_buf[3] << 8) | s->setup_buf[2];
42c570
     index   = (s->setup_buf[5] << 8) | s->setup_buf[4];
42c570
 
42c570
-    if (s->setup_len > sizeof(s->data_buf)) {
42c570
+    setup_len = (s->setup_buf[7] << 8) | s->setup_buf[6];
42c570
+    if (setup_len > sizeof(s->data_buf)) {
42c570
         fprintf(stderr,
42c570
                 "usb_generic_handle_packet: ctrl buffer too small (%d > %zu)\n",
42c570
-                s->setup_len, sizeof(s->data_buf));
42c570
+                setup_len, sizeof(s->data_buf));
42c570
         p->status = USB_RET_STALL;
42c570
         return;
42c570
     }
42c570
+    s->setup_len = setup_len;
42c570
 
42c570
     if (p->pid == USB_TOKEN_OUT) {
42c570
         usb_packet_copy(p, s->data_buf, s->setup_len);
42c570
-- 
42c570
2.27.0
42c570