From: Li Qiang <liq3ea@163.com>
To: mst@redhat.com, kraxel@redhat.com, dmitry.fleytman@gmail.com,
jasowang@redhat.com, alxndr@bu.edu, peter.maydell@linaro.org,
pbonzini@redhat.com
Cc: Li Qiang <liq3ea@163.com>, liq3ea@gmail.com, qemu-devel@nongnu.org
Subject: [RFC 2/3] xhci: make the IO handler reentrant
Date: Wed, 2 Sep 2020 09:22:05 -0700 [thread overview]
Message-ID: <20200902162206.101872-3-liq3ea@163.com> (raw)
In-Reply-To: <20200902162206.101872-1-liq3ea@163.com>
The guest can program the xhci DMA address to its MMIO.
This will cause an UAF issue.
Following is the reproducer:
cat << EOF | ./i386-softmmu/qemu-system-i386 -device nec-usb-xhci \
-trace usb\* -device usb-audio -device usb-storage,drive=mydrive \
-drive id=mydrive,file=null-co://,size=2M,format=raw,if=none \
-nodefaults -nographic -qtest stdio
outl 0xcf8 0x80001010
outl 0xcfc 0xc0202
outl 0xcf8 0x80001004
outl 0xcfc 0x1c77695e
writel 0xc0040 0xffffd855
writeq 0xc2000 0xff05140100000000
write 0x1d 0x1 0x27
write 0x2d 0x1 0x2e
write 0x17232 0x1 0x03
write 0x17254 0x1 0x05
write 0x17276 0x1 0x72
write 0x17278 0x1 0x02
write 0x3d 0x1 0x27
write 0x40 0x1 0x2e
write 0x41 0x1 0x72
write 0x42 0x1 0x01
write 0x4d 0x1 0x2e
write 0x4f 0x1 0x01
write 0x2007c 0x1 0xc7
writeq 0xc2000 0x5c05140100000000
write 0x20070 0x1 0x80
write 0x20078 0x1 0x08
write 0x2007c 0x1 0xfe
write 0x2007d 0x1 0x08
write 0x20081 0x1 0xff
write 0x20082 0x1 0x0b
write 0x20089 0x1 0x8c
write 0x2008d 0x1 0x04
write 0x2009d 0x1 0x10
writeq 0xc2000 0x2505ef019e092f00
EOF
This patch avoid this by adding a 'in_io' in XHCIState to indicate it is in IO
processing.
Buglink: https://bugs.launchpad.net/qemu/+bug/1891354
Reported-by: Alexander Bulekov <alxndr@bu.edu>
Signed-off-by: Li Qiang <liq3ea@163.com>
---
hw/usb/hcd-xhci.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
hw/usb/hcd-xhci.h | 1 +
2 files changed, 61 insertions(+)
diff --git a/hw/usb/hcd-xhci.c b/hw/usb/hcd-xhci.c
index 46a2186d91..06cd235123 100644
--- a/hw/usb/hcd-xhci.c
+++ b/hw/usb/hcd-xhci.c
@@ -2738,6 +2738,11 @@ static uint64_t xhci_cap_read(void *ptr, hwaddr reg, unsigned size)
XHCIState *xhci = ptr;
uint32_t ret;
+ if (xhci->in_io) {
+ return 0;
+ }
+ xhci->in_io = true;
+
switch (reg) {
case 0x00: /* HCIVERSION, CAPLENGTH */
ret = 0x01000000 | LEN_CAP;
@@ -2805,6 +2810,9 @@ static uint64_t xhci_cap_read(void *ptr, hwaddr reg, unsigned size)
}
trace_usb_xhci_cap_read(reg, ret);
+
+ xhci->in_io = false;
+
return ret;
}
@@ -2813,6 +2821,11 @@ static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size)
XHCIPort *port = ptr;
uint32_t ret;
+ if (port->xhci->in_io) {
+ return 0;
+ }
+ port->xhci->in_io = true;
+
switch (reg) {
case 0x00: /* PORTSC */
ret = port->portsc;
@@ -2828,6 +2841,9 @@ static uint64_t xhci_port_read(void *ptr, hwaddr reg, unsigned size)
}
trace_usb_xhci_port_read(port->portnr, reg, ret);
+
+ port->xhci->in_io = false;
+
return ret;
}
@@ -2837,6 +2853,11 @@ static void xhci_port_write(void *ptr, hwaddr reg,
XHCIPort *port = ptr;
uint32_t portsc, notify;
+ if (port->xhci->in_io) {
+ return;
+ }
+ port->xhci->in_io = true;
+
trace_usb_xhci_port_write(port->portnr, reg, val);
switch (reg) {
@@ -2896,6 +2917,7 @@ static void xhci_port_write(void *ptr, hwaddr reg,
default:
trace_usb_xhci_unimplemented("port write", reg);
}
+ port->xhci->in_io = false;
}
static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size)
@@ -2903,6 +2925,11 @@ static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size)
XHCIState *xhci = ptr;
uint32_t ret;
+ if (xhci->in_io) {
+ return 0;
+ }
+ xhci->in_io = true;
+
switch (reg) {
case 0x00: /* USBCMD */
ret = xhci->usbcmd;
@@ -2937,6 +2964,9 @@ static uint64_t xhci_oper_read(void *ptr, hwaddr reg, unsigned size)
}
trace_usb_xhci_oper_read(reg, ret);
+
+ xhci->in_io = false;
+
return ret;
}
@@ -2946,6 +2976,11 @@ static void xhci_oper_write(void *ptr, hwaddr reg,
XHCIState *xhci = ptr;
DeviceState *d = DEVICE(ptr);
+ if (xhci->in_io) {
+ return;
+ }
+ xhci->in_io = true;
+
trace_usb_xhci_oper_write(reg, val);
switch (reg) {
@@ -3008,6 +3043,7 @@ static void xhci_oper_write(void *ptr, hwaddr reg,
default:
trace_usb_xhci_unimplemented("oper write", reg);
}
+ xhci->in_io = false;
}
static uint64_t xhci_runtime_read(void *ptr, hwaddr reg,
@@ -3016,6 +3052,11 @@ static uint64_t xhci_runtime_read(void *ptr, hwaddr reg,
XHCIState *xhci = ptr;
uint32_t ret = 0;
+ if (xhci->in_io) {
+ return 0;
+ }
+ xhci->in_io = true;
+
if (reg < 0x20) {
switch (reg) {
case 0x00: /* MFINDEX */
@@ -3054,6 +3095,9 @@ static uint64_t xhci_runtime_read(void *ptr, hwaddr reg,
}
trace_usb_xhci_runtime_read(reg, ret);
+
+ xhci->in_io = false;
+
return ret;
}
@@ -3063,10 +3107,17 @@ static void xhci_runtime_write(void *ptr, hwaddr reg,
XHCIState *xhci = ptr;
int v = (reg - 0x20) / 0x20;
XHCIInterrupter *intr = &xhci->intr[v];
+
+ if (xhci->in_io) {
+ return;
+ }
+ xhci->in_io = true;
+
trace_usb_xhci_runtime_write(reg, val);
if (reg < 0x20) {
trace_usb_xhci_unimplemented("runtime write", reg);
+ xhci->in_io = false;
return;
}
@@ -3121,6 +3172,7 @@ static void xhci_runtime_write(void *ptr, hwaddr reg,
default:
trace_usb_xhci_unimplemented("oper write", reg);
}
+ xhci->in_io = false;
}
static uint64_t xhci_doorbell_read(void *ptr, hwaddr reg,
@@ -3137,10 +3189,17 @@ static void xhci_doorbell_write(void *ptr, hwaddr reg,
XHCIState *xhci = ptr;
unsigned int epid, streamid;
+ if (xhci->in_io) {
+ return;
+ }
+
+ xhci->in_io = true;
+
trace_usb_xhci_doorbell_write(reg, val);
if (!xhci_running(xhci)) {
DPRINTF("xhci: wrote doorbell while xHC stopped or paused\n");
+ xhci->in_io = false;
return;
}
@@ -3165,6 +3224,7 @@ static void xhci_doorbell_write(void *ptr, hwaddr reg,
xhci_kick_ep(xhci, reg, epid, streamid);
}
}
+ xhci->in_io = false;
}
static void xhci_cap_write(void *opaque, hwaddr addr, uint64_t val,
diff --git a/hw/usb/hcd-xhci.h b/hw/usb/hcd-xhci.h
index 946af51fc2..ed16232c96 100644
--- a/hw/usb/hcd-xhci.h
+++ b/hw/usb/hcd-xhci.h
@@ -227,6 +227,7 @@ struct XHCIState {
XHCIRing cmd_ring;
bool nec_quirks;
+ bool in_io;
};
#endif
--
2.17.1
next prev parent reply other threads:[~2020-09-02 16:23 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-02 16:22 [RFC 0/3] try to solve the DMA to MMIO issue Li Qiang
2020-09-02 16:22 ` [RFC 1/3] e1000e: make the IO handler reentrant Li Qiang
2020-09-02 16:22 ` Li Qiang [this message]
2020-09-02 16:22 ` [RFC 3/3] virtio-gpu: " Li Qiang
2020-09-03 5:12 ` Michael Tokarev
2020-09-03 10:32 ` Li Qiang
2020-09-03 3:54 ` [RFC 0/3] try to solve the DMA to MMIO issue Jason Wang
2020-09-03 4:06 ` Alexander Bulekov
2020-09-03 4:24 ` Jason Wang
2020-09-03 4:50 ` Li Qiang
2020-09-03 6:16 ` Jason Wang
2020-09-03 6:28 ` Li Qiang
2020-09-03 10:53 ` Peter Maydell
2020-09-03 11:11 ` Li Qiang
2020-09-03 11:19 ` Peter Maydell
2020-09-03 11:23 ` Li Qiang
2020-09-03 11:28 ` Peter Maydell
2020-09-03 13:35 ` Philippe Mathieu-Daudé
2020-09-03 13:41 ` Peter Maydell
2020-09-04 2:45 ` Jason Wang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200902162206.101872-3-liq3ea@163.com \
--to=liq3ea@163.com \
--cc=alxndr@bu.edu \
--cc=dmitry.fleytman@gmail.com \
--cc=jasowang@redhat.com \
--cc=kraxel@redhat.com \
--cc=liq3ea@gmail.com \
--cc=mst@redhat.com \
--cc=pbonzini@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).