Skip to contentSkip to conversation

hw/usb/canokey: validate endpoint number in canokey_handle_data

PATCH In review Bin Guo 3 participants 4 messages Sep 18 – Nov 18

lore
Share & tools
lore

Bin Guo

author
#0 lore raw

PATCH 1/1 hw/usb/canokey: validate endpoint number in canokey_handle_data

canokey_handle_data() takes the endpoint number from the USB packet (p->ep->nr, guest-selectable 1..15) and uses it to index the fixed-size ep_out[] / ep_out_size[] arrays of CanoKeyState, which hold only CANOKEY_EP_NUM = 3 entries. A forged bulk-OUT transfer on any endpoint number >= 3 therefore reads a bogus length from past ep_out_size[] and writes guest data through a wild pointer read from past ep_out[].

Validate p->ep->nr against CANOKEY_EP_NUM and return USB_RET_STALL for invalid endpoints, before any array access.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4481

---
 hw/usb/canokey.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/usb/canokey.c +5 -0
index c3baedac2c..91dd886761 100644
@@ -189,6 +189,11 @@ static void canokey_handle_data(USBDevice *dev, USBPacket *p)
 {
     CanoKeyState *key = CANOKEY(dev);
 
+    if (p->ep->nr >= CANOKEY_EP_NUM) {
+        p->status = USB_RET_STALL;
+        return;
+    }
+
     uint8_t ep_in = CANOKEY_EP_IN(p->ep->nr);
     uint8_t ep_out = p->ep->nr;
     uint32_t in_len;
Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

(added the canokey maintainers)

Quoted 8 lines · Bin Guo

canokey_handle_data() takes the endpoint number from the USB packet (p->ep->nr, guest-selectable 1..15) and uses it to index the fixed-size ep_out[] / ep_out_size[] arrays of CanoKeyState, which hold only CANOKEY_EP_NUM = 3 entries. A forged bulk-OUT transfer on any endpoint number >= 3 therefore reads a bogus length from past ep_out_size[] and writes guest data through a wild pointer read from past ep_out[].

Quoted 5 lines

Validate p->ep->nr against CANOKEY_EP_NUM and return USB_RET_STALL for invalid endpoints, before any array access.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4481

Probably worth Cc: [email protected]

Quoted 1 line

--

Did you have trouble getting the canokey code to build? I've been playing around with it, and the canokey-qemu library appears to have a busted pkgconfig file, such that meson can't find the include path for it and the #include in QEMU fails. I guess if you installed it to the system include it would work.

NB: given that the canokey-qemu library is Apache licensed and QEMU is GPL-2, the whole canokey support in QEMU seems of very niche utility, because you can't actually distribute a QEMU binary that you build with it AIUI. And distros generally don't ship the canokey-qemu library anyway I think.

So is it actually useful to anybody, or should we just deprecate it?

thanks
-- PMM

Reviewed-by
#2 lore raw
Quoted 30 lines · Bin Guo
Quoted 7 lines

canokey_handle_data() takes the endpoint number from the USB packet (p->ep->nr, guest-selectable 1..15) and uses it to index the fixed-size ep_out[] / ep_out_size[] arrays of CanoKeyState, which hold only CANOKEY_EP_NUM = 3 entries. A forged bulk-OUT transfer on any endpoint number >= 3 therefore reads a bogus length from past ep_out_size[] and writes guest data through a wild pointer read from past ep_out[].

Quoted 5 lines

Validate p->ep->nr against CANOKEY_EP_NUM and return USB_RET_STALL for invalid endpoints, before any array access.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4481

Probably worth Cc: [email protected]

Quoted 1 line

--

Did you have trouble getting the canokey code to build? I've been playing around with it, and the canokey-qemu library appears to have a busted pkgconfig file, such that meson can't find the include path for it and the #include in QEMU fails. I guess if you installed it to the system include it would work.

NB: given that the canokey-qemu library is Apache licensed and QEMU is GPL-2, the whole canokey support in QEMU seems of very niche utility, because you can't actually distribute a QEMU binary that you build with it AIUI. And distros generally don't ship the canokey-qemu library anyway I think.

Quoted 5 lines

So is it actually useful to anybody, or should we just deprecate it?

thanks
-- PMM

Thanks for the email. The fix looks good to me.

Regarding the build, indeed it requires modifying PKG_CONFIG_PATH for meson to find it for custom build, and this behavior is documented in [1]. If it is a system package, it should be fine.

For downstream usage, NixOS used it for CI testing of their systemd-cryptoenroll for ensuring successful booting after upgrading [2]. There are efforts to push it for other distros [3]. I acknowledge that it is niche, but I think there are still users.

[1] https://www.qemu.org/docs/master/system/devices/canokey.html
[2] https://mynixos.com/nixpkgs/package/canokey-qemu
[3] https://github.com/canokeys/canokey-qemu/issues/6

Best,
Hongren

#3 lore raw
Quoted 33 lines · Hongren Zheng
Quoted 25 lines · Bin Guo
Quoted 7 lines

canokey_handle_data() takes the endpoint number from the USB packet (p->ep->nr, guest-selectable 1..15) and uses it to index the fixed-size ep_out[] / ep_out_size[] arrays of CanoKeyState, which hold only CANOKEY_EP_NUM = 3 entries. A forged bulk-OUT transfer on any endpoint number >= 3 therefore reads a bogus length from past ep_out_size[] and writes guest data through a wild pointer read from past ep_out[].

Quoted 5 lines

Validate p->ep->nr against CANOKEY_EP_NUM and return USB_RET_STALL for invalid endpoints, before any array access.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/4481

Probably worth Cc: [email protected]

Quoted 1 line

--

Did you have trouble getting the canokey code to build? I've been playing around with it, and the canokey-qemu library appears to have a busted pkgconfig file, such that meson can't find the include path for it and the #include in QEMU fails. I guess if you installed it to the system include it would work.

Quoted 7 lines

NB: given that the canokey-qemu library is Apache licensed and QEMU is GPL-2, the whole canokey support in QEMU seems of very niche utility, because you can't actually distribute a QEMU binary that you build with it AIUI. And distros generally don't ship the canokey-qemu library anyway I think.

So is it actually useful to anybody, or should we just deprecate it?

Quoted 7 lines

Thanks for the email. The fix looks good to me.

Regarding the build, indeed it requires modifying PKG_CONFIG_PATH for meson to find it for custom build, and this behavior is documented in [1]. If it is a system package, it should be fine.

Setting PKG_CONFIG_PATH is not sufficient. The problem is that the pkg-config is:

prefix=/home/pm215/src/canokey-install
libdir=lib
includedir=include

Name: libcanokey-qemu
Description: CanoKey lib for QEMU
Version: 1
Libs: -L${libdir} -lcanokey-qemu
Cflags: -I${includedir}

but it doesn't actually put ${prefix} into the libdir or includedir variables. So meson put "-Iinclude" into CFLAGS, which can't find the include file. I think it ought to say

libdir=${prefix}/lib
includedir=${prefix}/include

Quoted 8 lines

For downstream usage, NixOS used it for CI testing of their systemd-cryptoenroll for ensuring successful booting after upgrading [2]. There are efforts to push it for other distros [3]. I acknowledge that it is niche, but I think there are still users.

[1] https://www.qemu.org/docs/master/system/devices/canokey.html
[2] https://mynixos.com/nixpkgs/package/canokey-qemu
[3] https://github.com/canokeys/canokey-qemu/issues/6

But what about the license conflict? I don't see distros being likely to want to enable building something that isn't redistributable, and especially something that's documented as "testing purposes only, no warranty on the security".

thanks
-- PMM