Blame SOURCES/kvm-usb-fix-setup_len-init-CVE-2020-14364.patch

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