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