cryptospore / rpms / qemu-kvm

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