Skip to contentSkip to conversation

usb: don't trust report_size for buffer size

PATCH In review Kees Cook 2 participants 4 messages Oct 11, 2010 USB

lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 usb: don't trust report_size for buffer size

If the iowarrior devices in this case statement support more than 8 bytes per report, it is possible to write past the end of a kernel heap allocation. This will probably never be possible, but change the allocation to be more defensive anyway.

---
 drivers/usb/misc/iowarrior.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/usb/misc/iowarrior.c +1 -1
index bc88c79..8ed8d05 100644
@@ -374,7 +374,7 @@ static ssize_t iowarrior_write(struct file *file,
 	case USB_DEVICE_ID_CODEMERCS_IOWPV2:
 	case USB_DEVICE_ID_CODEMERCS_IOW40:
 		/* IOW24 and IOW40 use a synchronous call */
-		buf = kmalloc(8, GFP_KERNEL);	/* 8 bytes are enough for both products */
+		buf = kmalloc(count, GFP_KERNEL);
 		if (!buf) {
 			retval = -ENOMEM;
 			goto exit;
Signature · 5 lines
1.7.1

-- 
Kees Cook
Ubuntu Security Team
Acked-by on v1 1/1
#1 lore raw
Quoted 4 lines · Kees Cook

If the iowarrior devices in this case statement support more than 8 bytes per report, it is possible to write past the end of a kernel heap allocation. This will probably never be possible, but change the allocation to be more defensive anyway.

I think this might be triggered from user space, indeed. The iowarrior_class.fops->write points directly to the function iowarrior_write(). The iowarrior_class itself is passed to the function usb_register_dev(), which means that the write() system call to the character device will result in calling the iowarrior_write() function.

Quoted 1 line

There might be similar problem also in the case USB_DEVICE_ID_CODEMERCS_IOW56. There is buf is allocated with usb_alloc_coherent() to the size dev->report_size. However, some lines later the copy_from_user() function tries to copy "count" number of bytes to the dev->report_size allocated buffer. Unfortunately I don't have such devices to try the driver so these are just coming from "static analysis".

Quoted 17 lines
---
 drivers/usb/misc/iowarrior.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
drivers/usb/misc/iowarrior.c +1 -1
index bc88c79..8ed8d05 100644
@@ -374,7 +374,7 @@ static ssize_t iowarrior_write(struct file *file,
 	case USB_DEVICE_ID_CODEMERCS_IOWPV2:
 	case USB_DEVICE_ID_CODEMERCS_IOW40:
 		/* IOW24 and IOW40 use a synchronous call */
-		buf = kmalloc(8, GFP_KERNEL);	/* 8 bytes are enough for both products */
+		buf = kmalloc(count, GFP_KERNEL);
 		if (!buf) {
 			retval = -ENOMEM;
 			goto exit;
author
#2 lore raw

Hi Németh,

Quoted 5 lines · Németh Márton

There might be similar problem also in the case USB_DEVICE_ID_CODEMERCS_IOW56. There is buf is allocated with usb_alloc_coherent() to the size dev->report_size. However, some lines later the copy_from_user() function tries to copy "count" number of bytes to the dev->report_size allocated buffer. Unfortunately I don't have such devices to try the driver so these are just coming from "static analysis".

I don't think the USB_DEVICE_ID_CODEMERCS_IOW56 path is a problem:

        if (count != dev->report_size) {
                retval = -EINVAL;
                goto exit;
        }
        switch (dev->product_id) {
...
        case USB_DEVICE_ID_CODEMERCS_IOW56:
...
                buf = usb_alloc_coherent(dev->udev, dev->report_size,
                                         GFP_KERNEL, &int_out_urb->transfer_dma);
...
                if (copy_from_user(buf, user_buffer, count)) {

i.e. count must == dev->report_size, and the buf is allocated with size dev->report_size even though copy_from_user uses "count".

-Kees

Signature · 2 lines
Kees Cook
Ubuntu Security Team
#3 lore raw
Quoted 9 lines · Kees Cook

Hi Németh,

Quoted 5 lines · Németh Márton

There might be similar problem also in the case USB_DEVICE_ID_CODEMERCS_IOW56. There is buf is allocated with usb_alloc_coherent() to the size dev->report_size. However, some lines later the copy_from_user() function tries to copy "count" number of bytes to the dev->report_size allocated buffer. Unfortunately I don't have such devices to try the driver so these are just coming from "static analysis".

Quoted 6 lines

I don't think the USB_DEVICE_ID_CODEMERCS_IOW56 path is a problem:

        if (count != dev->report_size) {
                retval = -EINVAL;
                goto exit;
        }

You are right, I missed this condition.

Quoted 14 lines
        switch (dev->product_id) {
...
        case USB_DEVICE_ID_CODEMERCS_IOW56:
...
                buf = usb_alloc_coherent(dev->udev, dev->report_size,
                                         GFP_KERNEL, &int_out_urb->transfer_dma);
...
                if (copy_from_user(buf, user_buffer, count)) {

i.e. count must == dev->report_size, and the buf is allocated with size dev->report_size even though copy_from_user uses "count".

-Kees