Skip to contentSkip to conversation

hw/usb/dev-wacom: Don't write off end of buffer

PATCH In review Peter Maydell 2 participants 3 messages Jul 6 – Jul 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/usb/dev-wacom: Don't write off end of buffer

In usb_wacom_handle_data() we allocate a buffer with a size determined by the transfer size requested by the guest. We then fill it in by calling either usb_mouse_poll() or usb_wacom_poll(), both of which functions take a length and return an actual length, which we pass to usb_packet_copy(). However, usb_mouse_poll() doesn't check the buffer size as it fills in the buffer, so if the guest passes an overly short transfer size then it will write off the end of the allocated buffer.

Check the length is at least big enough for the minimum 3 byte packet and return nothing if it is not, as usb_wacom_poll() does.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3672

---
 hw/usb/dev-wacom.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/usb/dev-wacom.c +4 -0
index c69e247f7b..895edcc17e 100644
@@ -285,6 +285,10 @@ static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len)
         b |= 0x04;
     }
 
+    if (len < 3) {
+        return 0;
+    }
+
     buf[0] = b;
     buf[1] = dx;
     buf[2] = dy;
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 24 lines · Peter Maydell

In usb_wacom_handle_data() we allocate a buffer with a size determined by the transfer size requested by the guest. We then fill it in by calling either usb_mouse_poll() or usb_wacom_poll(), both of which functions take a length and return an actual length, which we pass to usb_packet_copy(). However, usb_mouse_poll() doesn't check the buffer size as it fills in the buffer, so if the guest passes an overly short transfer size then it will write off the end of the allocated buffer.

Check the length is at least big enough for the minimum 3 byte packet and return nothing if it is not, as usb_wacom_poll() does.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3672

---
  hw/usb/dev-wacom.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/usb/dev-wacom.c +0 -0
index c69e247f7b..895edcc17e 100644

hw/usb/dev-wacom.c static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len) · line 288

Quoted 4 lines
hw/usb/dev-wacom.c +1 -0
@@ -285,6 +285,10 @@ static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len)
          b |= 0x04;
      }
 
+    if (len < 3) {

Maybe log GUEST_ERROR? Anyway:

Quoted 6 lines
hw/usb/dev-wacom.c +3 -0
+        return 0;
+    }
+
      buf[0] = b;
      buf[1] = dx;
      buf[2] = dy;
#2 lore raw
Quoted 29 lines · Philippe Mathieu-Daudé
Quoted 27 lines · Peter Maydell

In usb_wacom_handle_data() we allocate a buffer with a size determined by the transfer size requested by the guest. We then fill it in by calling either usb_mouse_poll() or usb_wacom_poll(), both of which functions take a length and return an actual length, which we pass to usb_packet_copy(). However, usb_mouse_poll() doesn't check the buffer size as it fills in the buffer, so if the guest passes an overly short transfer size then it will write off the end of the allocated buffer.

Check the length is at least big enough for the minimum 3 byte packet and return nothing if it is not, as usb_wacom_poll() does.

Cc: [email protected]
Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/3672

---
  hw/usb/dev-wacom.c | 4 ++++
  1 file changed, 4 insertions(+)
hw/usb/dev-wacom.c +0 -0
index c69e247f7b..895edcc17e 100644
@@ -285,6 +285,10 @@ static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len)
          b |= 0x04;
      }
 
Quoted 4 lines

hw/usb/dev-wacom.c static int usb_mouse_poll(USBWacomState *s, uint8_t *buf, int len) · line 288

Quoted 1 line
hw/usb/dev-wacom.c +1 -0
+    if (len < 3) {

Maybe log GUEST_ERROR? Anyway:

Thanks; I'll take this via target-arm.next as I'm putting together a pullreq anyway.

-- PMM