[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 15/25] usb/msd: add usb_msd_fatal_error() and fix guest-triggerabl
From: |
Gerd Hoffmann |
Subject: |
[PULL 15/25] usb/msd: add usb_msd_fatal_error() and fix guest-triggerable assert |
Date: |
Mon, 26 Sep 2022 11:54:59 +0200 |
Add handler for fatal errors. Moves device into error state where it
stops responding until the guest resets it.
Guest can send illegal requests where scsi command and usb packet
transfer directions are inconsistent. Use the new usb_msd_fatal_error()
function instead of assert() in that case.
Reported-by: Qiang Liu <cyruscyliu@gmail.com>
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Tested-by: Qiang Liu <cyruscyliu@gmail.com>
Message-Id: <20220830063827.813053-3-kraxel@redhat.com>
---
include/hw/usb/msd.h | 1 +
hw/usb/dev-storage.c | 30 +++++++++++++++++++++++++++++-
hw/usb/trace-events | 1 +
3 files changed, 31 insertions(+), 1 deletion(-)
diff --git a/include/hw/usb/msd.h b/include/hw/usb/msd.h
index 54e9f38bda46..f9fd862b529a 100644
--- a/include/hw/usb/msd.h
+++ b/include/hw/usb/msd.h
@@ -40,6 +40,7 @@ struct MSDState {
bool removable;
bool commandlog;
SCSIDevice *scsi_dev;
+ bool needs_reset;
};
typedef struct MSDState MSDState;
diff --git a/hw/usb/dev-storage.c b/hw/usb/dev-storage.c
index 140ef2aeaa80..e3bcffb3e0d7 100644
--- a/hw/usb/dev-storage.c
+++ b/hw/usb/dev-storage.c
@@ -191,6 +191,23 @@ static void usb_msd_packet_complete(MSDState *s)
usb_packet_complete(&s->dev, p);
}
+static void usb_msd_fatal_error(MSDState *s)
+{
+ trace_usb_msd_fatal_error();
+
+ if (s->packet) {
+ s->packet->status = USB_RET_STALL;
+ usb_msd_packet_complete(s);
+ }
+
+ /*
+ * Guest messed up up device state with illegal requests. Go
+ * ignore any requests until the guests resets the device (and
+ * brings it into a known state that way).
+ */
+ s->needs_reset = true;
+}
+
static void usb_msd_copy_data(MSDState *s, USBPacket *p)
{
uint32_t len;
@@ -227,7 +244,11 @@ void usb_msd_transfer_data(SCSIRequest *req, uint32_t len)
MSDState *s = DO_UPCAST(MSDState, dev.qdev, req->bus->qbus.parent);
USBPacket *p = s->packet;
- assert((s->mode == USB_MSDM_DATAOUT) == (req->cmd.mode ==
SCSI_XFER_TO_DEV));
+ if ((s->mode == USB_MSDM_DATAOUT) != (req->cmd.mode == SCSI_XFER_TO_DEV)) {
+ usb_msd_fatal_error(s);
+ return;
+ }
+
s->scsi_len = len;
s->scsi_off = 0;
if (p) {
@@ -317,6 +338,8 @@ void usb_msd_handle_reset(USBDevice *dev)
memset(&s->csw, 0, sizeof(s->csw));
s->mode = USB_MSDM_CBW;
+
+ s->needs_reset = false;
}
static void usb_msd_handle_control(USBDevice *dev, USBPacket *p,
@@ -382,6 +405,11 @@ static void usb_msd_handle_data(USBDevice *dev, USBPacket
*p)
SCSIDevice *scsi_dev;
uint32_t len;
+ if (s->needs_reset) {
+ p->status = USB_RET_STALL;
+ return;
+ }
+
switch (p->pid) {
case USB_TOKEN_OUT:
if (devep != 2)
diff --git a/hw/usb/trace-events b/hw/usb/trace-events
index 914ca7166829..b65269892c5e 100644
--- a/hw/usb/trace-events
+++ b/hw/usb/trace-events
@@ -263,6 +263,7 @@ usb_msd_packet_complete(void) ""
usb_msd_cmd_submit(unsigned lun, unsigned tag, unsigned flags, unsigned len,
unsigned data_len) "lun %u, tag 0x%x, flags 0x%08x, len %d, data-len %d"
usb_msd_cmd_complete(unsigned status, unsigned tag) "status %d, tag 0x%x"
usb_msd_cmd_cancel(unsigned tag) "tag 0x%x"
+usb_msd_fatal_error(void) ""
# dev-uas.c
usb_uas_reset(int addr) "dev %d"
--
2.37.3
- [PULL 06/25] ui/clipboard: fix serial priority, (continued)
- [PULL 06/25] ui/clipboard: fix serial priority, Gerd Hoffmann, 2022/09/26
- [PULL 04/25] meson: Allow to enable gtk and sdl while cocoa is enabled, Gerd Hoffmann, 2022/09/26
- [PULL 02/25] ui/cocoa: Run qemu_init in the main thread, Gerd Hoffmann, 2022/09/26
- [PULL 05/25] ui: add some vdagent related traces, Gerd Hoffmann, 2022/09/26
- [PULL 07/25] ui/vdagent: always reset the clipboard serial on caps, Gerd Hoffmann, 2022/09/26
- [PULL 08/25] ui/clipboard: reset the serial state on reset, Gerd Hoffmann, 2022/09/26
- [PULL 11/25] hw/usb/hcd-xhci: Check whether DMA accesses fail, Gerd Hoffmann, 2022/09/26
- [PULL 10/25] ui/console: fix three double frees in png_save(), Gerd Hoffmann, 2022/09/26
- [PULL 13/25] hcd-ohci: Fix inconsistency when resetting ohci root hubs, Gerd Hoffmann, 2022/09/26
- [PULL 12/25] hcd-ohci: Drop ohci_service_iso_td() if ed->head & OHCI_DPTR_MASK is zero, Gerd Hoffmann, 2022/09/26
- [PULL 15/25] usb/msd: add usb_msd_fatal_error() and fix guest-triggerable assert,
Gerd Hoffmann <=
- [PULL 09/25] ui/vdagent: fix serial reset of guest agent, Gerd Hoffmann, 2022/09/26
- [PULL 16/25] hcd-xhci: drop operation with secondary stream arrays enabled, Gerd Hoffmann, 2022/09/26
- [PULL 19/25] usbnet: Detect short packets as sent by the xHCI controller, Gerd Hoffmann, 2022/09/26
- [PULL 14/25] usb/msd: move usb_msd_packet_complete(), Gerd Hoffmann, 2022/09/26
- [PULL 17/25] usbnet: Add missing usb_wakeup() call in usbnet_receive(), Gerd Hoffmann, 2022/09/26
- [PULL 18/25] usbnet: Accept mandatory USB_CDC_SET_ETHERNET_PACKET_FILTER request, Gerd Hoffmann, 2022/09/26
- [PULL 25/25] virtio-gpu: update scanout if there is any area covered by the rect, Gerd Hoffmann, 2022/09/26
- [PULL 23/25] audio: remove abort() in audio_bug(), Gerd Hoffmann, 2022/09/26
- [PULL 20/25] usbnet: Report link-up via interrupt endpoint in CDC-ECM mode, Gerd Hoffmann, 2022/09/26
- [PULL 24/25] hw/display/ati_2d: Fix buffer overflow in ati_2d_blt (CVE-2021-3638), Gerd Hoffmann, 2022/09/26