[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 36/36] hw/usb/canokey: Fix buffer overflow for OUT packet
From: |
Peter Maydell |
Subject: |
[PULL 36/36] hw/usb/canokey: Fix buffer overflow for OUT packet |
Date: |
Tue, 28 Jan 2025 20:13:14 +0000 |
From: Hongren Zheng <i@zenithal.me>
When USBPacket in OUT direction has larger payload
than the ep_out_buffer (of size 512), a buffer overflow
would occur.
It could be fixed by limiting the size of usb_packet_copy
to be at most buffer size. Further optimization gets rid
of the ep_out_buffer and directly uses ep_out as the target
buffer.
This is reported by a security researcher who artificially
constructed an OUT packet of size 2047. The report has gone
through the QEMU security process, and as this device is for
testing purpose and no deployment of it in virtualization
environment is observed, it is triaged not to be a security bug.
Cc: qemu-stable@nongnu.org
Fixes: d7d34918551dc48 ("hw/usb: Add CanoKey Implementation")
Reported-by: Juan Jose Lopez Jaimez <thatjiaozi@gmail.com>
Signed-off-by: Hongren Zheng <i@zenithal.me>
Message-id: Z4TfMOrZz6IQYl_h@Sun
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
hw/usb/canokey.h | 4 ----
hw/usb/canokey.c | 6 +++---
2 files changed, 3 insertions(+), 7 deletions(-)
diff --git a/hw/usb/canokey.h b/hw/usb/canokey.h
index e528889d332..1b60d734850 100644
--- a/hw/usb/canokey.h
+++ b/hw/usb/canokey.h
@@ -24,8 +24,6 @@
#define CANOKEY_EP_NUM 3
/* BULK/INTR IN can be up to 1352 bytes, e.g. get key info */
#define CANOKEY_EP_IN_BUFFER_SIZE 2048
-/* BULK OUT can be up to 270 bytes, e.g. PIV import cert */
-#define CANOKEY_EP_OUT_BUFFER_SIZE 512
typedef enum {
CANOKEY_EP_IN_WAIT,
@@ -59,8 +57,6 @@ typedef struct CanoKeyState {
/* OUT pointer to canokey recv buffer */
uint8_t *ep_out[CANOKEY_EP_NUM];
uint32_t ep_out_size[CANOKEY_EP_NUM];
- /* For large BULK OUT, multiple write to ep_out is needed */
- uint8_t ep_out_buffer[CANOKEY_EP_NUM][CANOKEY_EP_OUT_BUFFER_SIZE];
/* Properties */
char *file; /* canokey-file */
diff --git a/hw/usb/canokey.c b/hw/usb/canokey.c
index fae212f0530..e2d66179e0b 100644
--- a/hw/usb/canokey.c
+++ b/hw/usb/canokey.c
@@ -197,8 +197,8 @@ static void canokey_handle_data(USBDevice *dev, USBPacket
*p)
switch (p->pid) {
case USB_TOKEN_OUT:
trace_canokey_handle_data_out(ep_out, p->iov.size);
- usb_packet_copy(p, key->ep_out_buffer[ep_out], p->iov.size);
out_pos = 0;
+ /* segment packet into (possibly multiple) ep_out */
while (out_pos != p->iov.size) {
/*
* key->ep_out[ep_out] set by prepare_receive
@@ -207,8 +207,8 @@ static void canokey_handle_data(USBDevice *dev, USBPacket
*p)
* to be the buffer length
*/
out_len = MIN(p->iov.size - out_pos, key->ep_out_size[ep_out]);
- memcpy(key->ep_out[ep_out],
- key->ep_out_buffer[ep_out] + out_pos, out_len);
+ /* usb_packet_copy would update the pos offset internally */
+ usb_packet_copy(p, key->ep_out[ep_out], out_len);
out_pos += out_len;
/* update ep_out_size to actual len */
key->ep_out_size[ep_out] = out_len;
--
2.34.1
- [PULL 19/36] target/arm: Use fp_status_a64 or fp_status_a32 in is_ebf(), (continued)
- [PULL 19/36] target/arm: Use fp_status_a64 or fp_status_a32 in is_ebf(), Peter Maydell, 2025/01/28
- [PULL 18/36] target/arm: Use vfp.fp_status_a64 in A64-only helper functions, Peter Maydell, 2025/01/28
- [PULL 22/36] target/arm: Use FPST_A32 in A32 decoder, Peter Maydell, 2025/01/28
- [PULL 20/36] target/arm: Use fp_status_a32 in vjvct helper, Peter Maydell, 2025/01/28
- [PULL 21/36] target/arm: Use fp_status_a32 in vfp_cmp helpers, Peter Maydell, 2025/01/28
- [PULL 26/36] target/arm: Use fp_status_f16_a32 in AArch32-only helpers, Peter Maydell, 2025/01/28
- [PULL 23/36] target/arm: Use FPST_A64 in A64 decoder, Peter Maydell, 2025/01/28
- [PULL 28/36] target/arm: Use FPST_A32_F16 in A32 decoder, Peter Maydell, 2025/01/28
- [PULL 35/36] target/arm: Use FPST_A64_F16 for halfprec-to-other conversions, Peter Maydell, 2025/01/28
- [PULL 34/36] target/arm: Remove redundant advsimd float16 helpers, Peter Maydell, 2025/01/28
- [PULL 36/36] hw/usb/canokey: Fix buffer overflow for OUT packet,
Peter Maydell <=
- [PULL 25/36] target/arm: Define new fp_status_f16_a32 and fp_status_f16_a64, Peter Maydell, 2025/01/28
- [PULL 24/36] target/arm: Remove now-unused vfp.fp_status and FPST_FPCR, Peter Maydell, 2025/01/28
- [PULL 27/36] target/arm: Use fp_status_f16_a64 in AArch64-only helpers, Peter Maydell, 2025/01/28
- [PULL 31/36] fpu: Rename float_flag_input_denormal to float_flag_input_denormal_flushed, Peter Maydell, 2025/01/28
- [PULL 30/36] target/arm: Remove now-unused vfp.fp_status_f16 and FPST_FPCR_F16, Peter Maydell, 2025/01/28
- [PULL 32/36] fpu: Rename float_flag_output_denormal to float_flag_output_denormal_flushed, Peter Maydell, 2025/01/28
- [PULL 33/36] fpu: Fix a comment in softfloat-types.h, Peter Maydell, 2025/01/28
- [PULL 29/36] target/arm: Use FPST_A64_F16 in A64 decoder, Peter Maydell, 2025/01/28
- Re: [PULL 00/36] target-arm queue, Stefan Hajnoczi, 2025/01/29