Blame SOURCES/kvm-usb-drop-unnecessary-usb_device_post_load-checks.patch

8b1478
From ed9c0b1c244cb299cb0aa52b0dc93979ca503abd Mon Sep 17 00:00:00 2001
8b1478
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
8b1478
Date: Tue, 1 Oct 2019 19:01:06 +0200
8b1478
Subject: [PATCH 1/4] usb: drop unnecessary usb_device_post_load checks
8b1478
8b1478
RH-Author: Dr. David Alan Gilbert <dgilbert@redhat.com>
8b1478
Message-id: <20191001190106.30455-2-dgilbert@redhat.com>
8b1478
Patchwork-id: 90937
8b1478
O-Subject: [RHEL-7.8 qemu-kvm-rhev PATCH 1/1] usb: drop unnecessary usb_device_post_load checks
8b1478
Bugzilla: 1724048
8b1478
RH-Acked-by: Gerd Hoffmann <kraxel@redhat.com>
8b1478
RH-Acked-by: Igor Mammedov <imammedo@redhat.com>
8b1478
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
8b1478
8b1478
From: Jonathan Davies <jonathan.davies@nutanix.com>
8b1478
8b1478
In usb_device_post_load, certain values of dev->setup_len or
8b1478
dev->setup_index can cause -EINVAL to be returned. One example is when
8b1478
setup_len exceeds 4096, the hard-coded value of sizeof(dev->data_buf).
8b1478
This can happen through legitimate guest activity and will cause all
8b1478
subsequent attempts to migrate the guest to fail in vmstate_load_state.
8b1478
8b1478
The values of these variables can be set by USB packets originating in
8b1478
the guest. There are two ways in which they can be set: in
8b1478
do_token_setup and in do_parameter in hw/usb/core.c.
8b1478
8b1478
It is easy to craft a USB packet in a guest that causes do_token_setup
8b1478
to set setup_len to a value larger than 4096. When this has been done
8b1478
once, all subsequent attempts to migrate the VM will fail in
8b1478
usb_device_post_load until the VM is next power-cycled or a
8b1478
smaller-sized USB packet is sent to the device.
8b1478
8b1478
Sample code for achieving this in a VM started with "-device usb-tablet"
8b1478
running Linux with CONFIG_HIDRAW=y and HID_MAX_BUFFER_SIZE > 4096:
8b1478
8b1478
  #include <sys/types.h>
8b1478
  #include <sys/stat.h>
8b1478
  #include <fcntl.h>
8b1478
  #include <unistd.h>
8b1478
8b1478
  int main() {
8b1478
           char buf[4097];
8b1478
           int fd = open("/dev/hidraw0", O_RDWR|O_NONBLOCK);
8b1478
8b1478
           buf[0] = 0x1;
8b1478
           write(fd, buf, 4097);
8b1478
8b1478
           return 0;
8b1478
  }
8b1478
8b1478
When this code is run in the VM, qemu will output:
8b1478
8b1478
  usb_generic_handle_packet: ctrl buffer too small (4097 > 4096)
8b1478
8b1478
A subsequent attempt to migrate the VM will fail and output the
8b1478
following on the destination host:
8b1478
8b1478
  qemu-kvm: error while loading state for instance 0x0 of device '0000:00:06.7/1/usb-ptr'
8b1478
  qemu-kvm: load of migration failed: Invalid argument
8b1478
8b1478
The idea behind checking the values of setup_len and setup_index before
8b1478
they are used is correct, but doing it in usb_device_post_load feels
8b1478
arbitrary, and will cause unnecessary migration failures. Indeed, none
8b1478
of the commit messages for c60174e8, 9f8e9895 and 719ffe1f justify why
8b1478
post_load is the right place to do these checks. They correctly point
8b1478
out that the important thing to protect is the usb_packet_copy.
8b1478
8b1478
Instead, the right place to do the checks is in do_token_setup and
8b1478
do_parameter. Indeed, there are already some checks here. We can examine
8b1478
each of the disjuncts currently tested in usb_device_post_load to see
8b1478
whether any need adding to do_token_setup or do_parameter to improve
8b1478
safety there:
8b1478
8b1478
  * dev->setup_index < 0
8b1478
     - This test is not needed because setup_index is explicitly set to
8b1478
0 in do_token_setup and do_parameter.
8b1478
8b1478
  * dev->setup_len < 0
8b1478
     - In both do_token_setup and do_parameter, the value of setup_len
8b1478
is computed by (s->setup_buf[7] << 8) | s->setup_buf[6]. Since
8b1478
s->setup_buf is a byte array and setup_len is an int32_t, it's
8b1478
impossible for this arithmetic to set setup_len's top bit, so it can
8b1478
never be negative.
8b1478
8b1478
  * dev->setup_index > dev->setup_len
8b1478
     - Since setup_index is 0, this is equivalent to the previous test,
8b1478
so is redundant.
8b1478
8b1478
  * dev->setup_len > sizeof(dev->data_buf)
8b1478
     - This condition is already explicitly checked in both
8b1478
do_token_setup and do_parameter.
8b1478
8b1478
Hence there is no need to bolster the existing checks in do_token_setup
8b1478
or do_parameter, and we can safely remove these checks from
8b1478
usb_device_post_load without reducing safety but allowing migrations to
8b1478
proceed regardless of what USB packets have been generated by the guest.
8b1478
8b1478
Signed-off-by: Jonathan Davies <jonathan.davies@nutanix.com>
8b1478
Message-Id: <20190107175117.23769-1-jonathan.davies@nutanix.com>
8b1478
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
8b1478
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
8b1478
(cherry picked from commit f30815390adb1ec153327c3832ab378e8bce9808)
8b1478
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
8b1478
---
8b1478
 hw/usb/bus.c | 6 ------
8b1478
 1 file changed, 6 deletions(-)
8b1478
8b1478
diff --git a/hw/usb/bus.c b/hw/usb/bus.c
8b1478
index 11f7720..5499810 100644
8b1478
--- a/hw/usb/bus.c
8b1478
+++ b/hw/usb/bus.c
8b1478
@@ -59,12 +59,6 @@ static int usb_device_post_load(void *opaque, int version_id)
8b1478
     } else {
8b1478
         dev->attached = true;
8b1478
     }
8b1478
-    if (dev->setup_index < 0 ||
8b1478
-        dev->setup_len < 0 ||
8b1478
-        dev->setup_index > dev->setup_len ||
8b1478
-        dev->setup_len > sizeof(dev->data_buf)) {
8b1478
-        return -EINVAL;
8b1478
-    }
8b1478
     return 0;
8b1478
 }
8b1478
 
8b1478
-- 
8b1478
1.8.3.1
8b1478