Skip to contentSkip to conversation

HID: usbhid: fix out-of-bounds bug

PATCH Applied Alan Stern 4 participants 7 messages Sep 27, 2017 – Oct 11, 2017 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): CAAZ5spCffHnU_EtnhKpP5cDqsEKaGMatpaqAJqorvph+vZm1Sw@mail.gmail.com

Alan Stern

author
#1 lore raw
Quoted 8 lines · Michel Hermier

Review

Quoted 6 lines · Alan Stern
(quoted diff) +3 -1
-       for (n = 0; n < hdesc->bNumDescriptors; n++)
+       num_descriptors = min_t(int, hdesc->bNumDescriptors,
+                               (hdesc->bLength - 6) / 3);
+       for (n = 0; n < num_descriptors; n++)
                if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
                        rsize = le16_to_cpu(hdesc->desc[n].
Quoted 7 lines

wDescriptorLength);

Yes, this is a lot better.

Is it possible to explicit the magic number 6 and 3 in the code. Currently, it looks like it comes from no where.

Yes, it is possible. The 6 is equal to

offsetof(struct hid_descriptor, desc)

and the 3 is equal to

sizeof(struct hid_class_descriptor)

(at least, I think it is -- the structure is marked as packed so its size should be 3).

In this case I found the numbers to be more readable, but other people may have different opinions.

Quoted 2 lines

I'm also wondering if this change will not affect some devices in the wild, by rejecting hid descriptors with num descriptors == 0 ?

It's possible, but I doubt it. If such devices do exist, they should never have worked in the first place. Certainly they would generate warnings or errors during enumeration because of their invalid descriptors.

Alan Stern

#2 lore raw

2017-09-27 23:29 GMT+09:00 Alan Stern <[email protected]>:

Quoted 10 lines · Michel Hermier
Quoted 9 lines · Alan Stern
Quoted 7 lines
(quoted diff) +3 -1
-       for (n = 0; n < hdesc->bNumDescriptors; n++)
+       num_descriptors = min_t(int, hdesc->bNumDescriptors,
+                               (hdesc->bLength - 6) / 3);
+       for (n = 0; n < num_descriptors; n++)
                if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
                        rsize = le16_to_cpu(hdesc->desc[n].
Quoted 3 lines
Quoted 3 lines

wDescriptorLength);

Yes, this is a lot better.

OK.

Quoted 4 lines
Quoted 4 lines

Is it possible to explicit the magic number 6 and 3 in the code. Currently, it looks like it comes from no where.

I gree with you.

Quoted 7 lines

Yes, it is possible. The 6 is equal to

offsetof(struct hid_descriptor, desc)

and the 3 is equal to

Quoted 7 lines

sizeof(struct hid_class_descriptor)

(at least, I think it is -- the structure is marked as packed so its size should be 3).

In this case I found the numbers to be more readable, but other people may have different opinions.

I will post V2 shortly.

Quoted 11 lines
Quoted 3 lines

I'm also wondering if this change will not affect some devices in the wild, by rejecting hid descriptors with num descriptors == 0 ?

It's possible, but I doubt it. If such devices do exist, they should never have worked in the first place. Certainly they would generate warnings or errors during enumeration because of their invalid descriptors.

Alan Stern

Jaejoong

#3 lore raw

PATCH v2 1/1 HID: usbhid: fix out-of-bounds bug

The hid descriptor identifies the length and type of subordinate descriptors for a device. If the received hid descriptor is smaller than the size of the struct hid_descriptor, it is possible to cause out-of-bounds.

In addition, if bNumDescriptors of the hid descriptor have an incorrect value, this can also cause out-of-bounds while approaching hdesc->desc[n].

So check the size of hid descriptor and bNumDescriptors.

	BUG: KASAN: slab-out-of-bounds in usbhid_parse+0x9b1/0xa20
	Read of size 1 at addr ffff88006c5f8edf by task kworker/1:2/1261

	CPU: 1 PID: 1261 Comm: kworker/1:2 Not tainted
	4.14.0-rc1-42251-gebb2c2437d80 #169
	Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
	Workqueue: usb_hub_wq hub_event
	Call Trace:
	__dump_stack lib/dump_stack.c:16
	dump_stack+0x292/0x395 lib/dump_stack.c:52
	print_address_description+0x78/0x280 mm/kasan/report.c:252
	kasan_report_error mm/kasan/report.c:351
	kasan_report+0x22f/0x340 mm/kasan/report.c:409
	__asan_report_load1_noabort+0x19/0x20 mm/kasan/report.c:427
	usbhid_parse+0x9b1/0xa20 drivers/hid/usbhid/hid-core.c:1004
	hid_add_device+0x16b/0xb30 drivers/hid/hid-core.c:2944
	usbhid_probe+0xc28/0x1100 drivers/hid/usbhid/hid-core.c:1369
	usb_probe_interface+0x35d/0x8e0 drivers/usb/core/driver.c:361
	really_probe drivers/base/dd.c:413
	driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
	__device_attach_driver+0x230/0x290 drivers/base/dd.c:653
	bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
	__device_attach+0x26e/0x3d0 drivers/base/dd.c:710
	device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
	bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
	device_add+0xd0b/0x1660 drivers/base/core.c:1835
	usb_set_configuration+0x104e/0x1870 drivers/usb/core/message.c:1932
	generic_probe+0x73/0xe0 drivers/usb/core/generic.c:174
	usb_probe_device+0xaf/0xe0 drivers/usb/core/driver.c:266
	really_probe drivers/base/dd.c:413
	driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
	__device_attach_driver+0x230/0x290 drivers/base/dd.c:653
	bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
	__device_attach+0x26e/0x3d0 drivers/base/dd.c:710
	device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
	bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
	device_add+0xd0b/0x1660 drivers/base/core.c:1835
	usb_new_device+0x7b8/0x1020 drivers/usb/core/hub.c:2457
	hub_port_connect drivers/usb/core/hub.c:4903
	hub_port_connect_change drivers/usb/core/hub.c:5009
	port_event drivers/usb/core/hub.c:5115
	hub_event+0x194d/0x3740 drivers/usb/core/hub.c:5195
	process_one_work+0xc7f/0x1db0 kernel/workqueue.c:2119
	worker_thread+0x221/0x1850 kernel/workqueue.c:2253
	kthread+0x3a1/0x470 kernel/kthread.c:231
	ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:431

---

Changes in v2:
- write a new commit message because orginal version is wrong approach
- add check hid descriptor size
- get proper value for bNumDescriptors as suggested by Alan Stern
- fix the Reported-by

 drivers/hid/usbhid/hid-core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/hid/usbhid/hid-core.c +11 -1
index 089bad8..045b5da 100644
@@ -975,6 +975,8 @@ static int usbhid_parse(struct hid_device *hid)
 	unsigned int rsize = 0;
 	char *rdesc;
 	int ret, n;
+	int num_descriptors;
+	size_t offset = offsetof(struct hid_descriptor, desc);
 
 	quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
 			le16_to_cpu(dev->descriptor.idProduct));
@@ -997,10 +999,18 @@ static int usbhid_parse(struct hid_device *hid)
 		return -ENODEV;
 	}
 
+	if (hdesc->bLength < sizeof(struct hid_descriptor)) {
+		dbg_hid("hid descriptor is too short\n");
+		return -EINVAL;
+	}
+
 	hid->version = le16_to_cpu(hdesc->bcdHID);
 	hid->country = hdesc->bCountryCode;
 
-	for (n = 0; n < hdesc->bNumDescriptors; n++)
+	num_descriptors = min_t(int, hdesc->bNumDescriptors,
+	       (hdesc->bLength - offset) / sizeof(struct hid_class_descriptor));
+
+	for (n = 0; n < num_descriptors; n++)
 		if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
 			rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
 
Signature · 1 line
2.7.4
#4 lore raw

Hi,

To. Jiri, Alan,

Could you please review this patch?

To. Andey,

Could you please test with this patch for KASAN OOB error?

Thanks, jaejoong

2017-09-28 19:16 GMT+09:00 Jaejoong Kim <[email protected]>:

Quoted 106 lines

The hid descriptor identifies the length and type of subordinate descriptors for a device. If the received hid descriptor is smaller than the size of the struct hid_descriptor, it is possible to cause out-of-bounds.

In addition, if bNumDescriptors of the hid descriptor have an incorrect value, this can also cause out-of-bounds while approaching hdesc->desc[n].

So check the size of hid descriptor and bNumDescriptors.

        BUG: KASAN: slab-out-of-bounds in usbhid_parse+0x9b1/0xa20
        Read of size 1 at addr ffff88006c5f8edf by task kworker/1:2/1261

        CPU: 1 PID: 1261 Comm: kworker/1:2 Not tainted
        4.14.0-rc1-42251-gebb2c2437d80 #169
        Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
        Workqueue: usb_hub_wq hub_event
        Call Trace:
        __dump_stack lib/dump_stack.c:16
        dump_stack+0x292/0x395 lib/dump_stack.c:52
        print_address_description+0x78/0x280 mm/kasan/report.c:252
        kasan_report_error mm/kasan/report.c:351
        kasan_report+0x22f/0x340 mm/kasan/report.c:409
        __asan_report_load1_noabort+0x19/0x20 mm/kasan/report.c:427
        usbhid_parse+0x9b1/0xa20 drivers/hid/usbhid/hid-core.c:1004
        hid_add_device+0x16b/0xb30 drivers/hid/hid-core.c:2944
        usbhid_probe+0xc28/0x1100 drivers/hid/usbhid/hid-core.c:1369
        usb_probe_interface+0x35d/0x8e0 drivers/usb/core/driver.c:361
        really_probe drivers/base/dd.c:413
        driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
        __device_attach_driver+0x230/0x290 drivers/base/dd.c:653
        bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
        __device_attach+0x26e/0x3d0 drivers/base/dd.c:710
        device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
        bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
        device_add+0xd0b/0x1660 drivers/base/core.c:1835
        usb_set_configuration+0x104e/0x1870 drivers/usb/core/message.c:1932
        generic_probe+0x73/0xe0 drivers/usb/core/generic.c:174
        usb_probe_device+0xaf/0xe0 drivers/usb/core/driver.c:266
        really_probe drivers/base/dd.c:413
        driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
        __device_attach_driver+0x230/0x290 drivers/base/dd.c:653
        bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
        __device_attach+0x26e/0x3d0 drivers/base/dd.c:710
        device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
        bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
        device_add+0xd0b/0x1660 drivers/base/core.c:1835
        usb_new_device+0x7b8/0x1020 drivers/usb/core/hub.c:2457
        hub_port_connect drivers/usb/core/hub.c:4903
        hub_port_connect_change drivers/usb/core/hub.c:5009
        port_event drivers/usb/core/hub.c:5115
        hub_event+0x194d/0x3740 drivers/usb/core/hub.c:5195
        process_one_work+0xc7f/0x1db0 kernel/workqueue.c:2119
        worker_thread+0x221/0x1850 kernel/workqueue.c:2253
        kthread+0x3a1/0x470 kernel/kthread.c:231
        ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:431

---

Changes in v2:
- write a new commit message because orginal version is wrong approach
- add check hid descriptor size
- get proper value for bNumDescriptors as suggested by Alan Stern
- fix the Reported-by

 drivers/hid/usbhid/hid-core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/hid/usbhid/hid-core.c +11 -2
index 089bad8..045b5da 100644
@@ -975,6 +975,8 @@ static int usbhid_parse(struct hid_device *hid)
        unsigned int rsize = 0;
        char *rdesc;
        int ret, n;
+       int num_descriptors;
+       size_t offset = offsetof(struct hid_descriptor, desc);
 
        quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
                        le16_to_cpu(dev->descriptor.idProduct));
@@ -997,10 +999,18 @@ static int usbhid_parse(struct hid_device *hid)
                return -ENODEV;
        }
 
+       if (hdesc->bLength < sizeof(struct hid_descriptor)) {
+               dbg_hid("hid descriptor is too short\n");
+               return -EINVAL;
+       }
+
        hid->version = le16_to_cpu(hdesc->bcdHID);
        hid->country = hdesc->bCountryCode;
 
-       for (n = 0; n < hdesc->bNumDescriptors; n++)
+       num_descriptors = min_t(int, hdesc->bNumDescriptors,
+              (hdesc->bLength - offset) / sizeof(struct hid_class_descriptor));
+
+       for (n = 0; n < num_descriptors; n++)
                if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
                        rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
 
--

2.7.4

#5 lore raw
Quoted 9 lines · Jaejoong Kim

Hi,

To. Jiri, Alan,

Could you please review this patch?

To. Andey,

Could you please test with this patch for KASAN OOB error?

Hi!

Yes, your patch fixes the issue.

Thanks!

Quoted 115 lines

Thanks, jaejoong

2017-09-28 19:16 GMT+09:00 Jaejoong Kim <[email protected]>:

Quoted 104 lines

The hid descriptor identifies the length and type of subordinate descriptors for a device. If the received hid descriptor is smaller than the size of the struct hid_descriptor, it is possible to cause out-of-bounds.

In addition, if bNumDescriptors of the hid descriptor have an incorrect value, this can also cause out-of-bounds while approaching hdesc->desc[n].

So check the size of hid descriptor and bNumDescriptors.

        BUG: KASAN: slab-out-of-bounds in usbhid_parse+0x9b1/0xa20
        Read of size 1 at addr ffff88006c5f8edf by task kworker/1:2/1261

        CPU: 1 PID: 1261 Comm: kworker/1:2 Not tainted
        4.14.0-rc1-42251-gebb2c2437d80 #169
        Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
        Workqueue: usb_hub_wq hub_event
        Call Trace:
        __dump_stack lib/dump_stack.c:16
        dump_stack+0x292/0x395 lib/dump_stack.c:52
        print_address_description+0x78/0x280 mm/kasan/report.c:252
        kasan_report_error mm/kasan/report.c:351
        kasan_report+0x22f/0x340 mm/kasan/report.c:409
        __asan_report_load1_noabort+0x19/0x20 mm/kasan/report.c:427
        usbhid_parse+0x9b1/0xa20 drivers/hid/usbhid/hid-core.c:1004
        hid_add_device+0x16b/0xb30 drivers/hid/hid-core.c:2944
        usbhid_probe+0xc28/0x1100 drivers/hid/usbhid/hid-core.c:1369
        usb_probe_interface+0x35d/0x8e0 drivers/usb/core/driver.c:361
        really_probe drivers/base/dd.c:413
        driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
        __device_attach_driver+0x230/0x290 drivers/base/dd.c:653
        bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
        __device_attach+0x26e/0x3d0 drivers/base/dd.c:710
        device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
        bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
        device_add+0xd0b/0x1660 drivers/base/core.c:1835
        usb_set_configuration+0x104e/0x1870 drivers/usb/core/message.c:1932
        generic_probe+0x73/0xe0 drivers/usb/core/generic.c:174
        usb_probe_device+0xaf/0xe0 drivers/usb/core/driver.c:266
        really_probe drivers/base/dd.c:413
        driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
        __device_attach_driver+0x230/0x290 drivers/base/dd.c:653
        bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
        __device_attach+0x26e/0x3d0 drivers/base/dd.c:710
        device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
        bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
        device_add+0xd0b/0x1660 drivers/base/core.c:1835
        usb_new_device+0x7b8/0x1020 drivers/usb/core/hub.c:2457
        hub_port_connect drivers/usb/core/hub.c:4903
        hub_port_connect_change drivers/usb/core/hub.c:5009
        port_event drivers/usb/core/hub.c:5115
        hub_event+0x194d/0x3740 drivers/usb/core/hub.c:5195
        process_one_work+0xc7f/0x1db0 kernel/workqueue.c:2119
        worker_thread+0x221/0x1850 kernel/workqueue.c:2253
        kthread+0x3a1/0x470 kernel/kthread.c:231
        ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:431

---

Changes in v2:
- write a new commit message because orginal version is wrong approach
- add check hid descriptor size
- get proper value for bNumDescriptors as suggested by Alan Stern
- fix the Reported-by

 drivers/hid/usbhid/hid-core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/hid/usbhid/hid-core.c +11 -2
index 089bad8..045b5da 100644
@@ -975,6 +975,8 @@ static int usbhid_parse(struct hid_device *hid)
        unsigned int rsize = 0;
        char *rdesc;
        int ret, n;
+       int num_descriptors;
+       size_t offset = offsetof(struct hid_descriptor, desc);
 
        quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
                        le16_to_cpu(dev->descriptor.idProduct));
@@ -997,10 +999,18 @@ static int usbhid_parse(struct hid_device *hid)
                return -ENODEV;
        }
 
+       if (hdesc->bLength < sizeof(struct hid_descriptor)) {
+               dbg_hid("hid descriptor is too short\n");
+               return -EINVAL;
+       }
+
        hid->version = le16_to_cpu(hdesc->bcdHID);
        hid->country = hdesc->bCountryCode;
 
-       for (n = 0; n < hdesc->bNumDescriptors; n++)
+       num_descriptors = min_t(int, hdesc->bNumDescriptors,
+              (hdesc->bLength - offset) / sizeof(struct hid_class_descriptor));
+
+       for (n = 0; n < num_descriptors; n++)
                if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
                        rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
 
--
Quoted 2 lines

2.7.4

--
You received this message because you are subscribed to the Google Groups "syzkaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

author Acked-by
#6 lore raw
In reply to Jaejoong Kim
Quoted 108 lines · Jaejoong Kim

Hi,

To. Jiri, Alan,

Could you please review this patch?

To. Andey,

Could you please test with this patch for KASAN OOB error?

Thanks, jaejoong

2017-09-28 19:16 GMT+09:00 Jaejoong Kim <[email protected]>:

Quoted 95 lines

The hid descriptor identifies the length and type of subordinate descriptors for a device. If the received hid descriptor is smaller than the size of the struct hid_descriptor, it is possible to cause out-of-bounds.

In addition, if bNumDescriptors of the hid descriptor have an incorrect value, this can also cause out-of-bounds while approaching hdesc->desc[n].

So check the size of hid descriptor and bNumDescriptors.

        BUG: KASAN: slab-out-of-bounds in usbhid_parse+0x9b1/0xa20
        Read of size 1 at addr ffff88006c5f8edf by task kworker/1:2/1261

        CPU: 1 PID: 1261 Comm: kworker/1:2 Not tainted
        4.14.0-rc1-42251-gebb2c2437d80 #169
        Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
        Workqueue: usb_hub_wq hub_event
        Call Trace:
        __dump_stack lib/dump_stack.c:16
        dump_stack+0x292/0x395 lib/dump_stack.c:52
        print_address_description+0x78/0x280 mm/kasan/report.c:252
        kasan_report_error mm/kasan/report.c:351
        kasan_report+0x22f/0x340 mm/kasan/report.c:409
        __asan_report_load1_noabort+0x19/0x20 mm/kasan/report.c:427
        usbhid_parse+0x9b1/0xa20 drivers/hid/usbhid/hid-core.c:1004
        hid_add_device+0x16b/0xb30 drivers/hid/hid-core.c:2944
        usbhid_probe+0xc28/0x1100 drivers/hid/usbhid/hid-core.c:1369
        usb_probe_interface+0x35d/0x8e0 drivers/usb/core/driver.c:361
        really_probe drivers/base/dd.c:413
        driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
        __device_attach_driver+0x230/0x290 drivers/base/dd.c:653
        bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
        __device_attach+0x26e/0x3d0 drivers/base/dd.c:710
        device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
        bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
        device_add+0xd0b/0x1660 drivers/base/core.c:1835
        usb_set_configuration+0x104e/0x1870 drivers/usb/core/message.c:1932
        generic_probe+0x73/0xe0 drivers/usb/core/generic.c:174
        usb_probe_device+0xaf/0xe0 drivers/usb/core/driver.c:266
        really_probe drivers/base/dd.c:413
        driver_probe_device+0x610/0xa00 drivers/base/dd.c:557
        __device_attach_driver+0x230/0x290 drivers/base/dd.c:653
        bus_for_each_drv+0x161/0x210 drivers/base/bus.c:463
        __device_attach+0x26e/0x3d0 drivers/base/dd.c:710
        device_initial_probe+0x1f/0x30 drivers/base/dd.c:757
        bus_probe_device+0x1eb/0x290 drivers/base/bus.c:523
        device_add+0xd0b/0x1660 drivers/base/core.c:1835
        usb_new_device+0x7b8/0x1020 drivers/usb/core/hub.c:2457
        hub_port_connect drivers/usb/core/hub.c:4903
        hub_port_connect_change drivers/usb/core/hub.c:5009
        port_event drivers/usb/core/hub.c:5115
        hub_event+0x194d/0x3740 drivers/usb/core/hub.c:5195
        process_one_work+0xc7f/0x1db0 kernel/workqueue.c:2119
        worker_thread+0x221/0x1850 kernel/workqueue.c:2253
        kthread+0x3a1/0x470 kernel/kthread.c:231
        ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:431

---

Changes in v2:
- write a new commit message because orginal version is wrong approach
- add check hid descriptor size
- get proper value for bNumDescriptors as suggested by Alan Stern
- fix the Reported-by

 drivers/hid/usbhid/hid-core.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/hid/usbhid/hid-core.c +7 -0
index 089bad8..045b5da 100644
@@ -975,6 +975,8 @@ static int usbhid_parse(struct hid_device *hid)
        unsigned int rsize = 0;
        char *rdesc;
        int ret, n;
+       int num_descriptors;
+       size_t offset = offsetof(struct hid_descriptor, desc);
 
        quirks = usbhid_lookup_quirk(le16_to_cpu(dev->descriptor.idVendor),
                        le16_to_cpu(dev->descriptor.idProduct));
@@ -997,10 +999,18 @@ static int usbhid_parse(struct hid_device *hid)
                return -ENODEV;
        }
 
+       if (hdesc->bLength < sizeof(struct hid_descriptor)) {
+               dbg_hid("hid descriptor is too short\n");
+               return -EINVAL;
+       }
+
        hid->version = le16_to_cpu(hdesc->bcdHID);
        hid->country = hdesc->bCountryCode;
 
Quoted 7 lines

drivers/hid/usbhid/hid-core.c static int usbhid_parse(struct hid_device *hid) · line 1015

Quoted 7 lines
drivers/hid/usbhid/hid-core.c +4 -1
-       for (n = 0; n < hdesc->bNumDescriptors; n++)
+       num_descriptors = min_t(int, hdesc->bNumDescriptors,
+              (hdesc->bLength - offset) / sizeof(struct hid_class_descriptor));
+
+       for (n = 0; n < num_descriptors; n++)
                if (hdesc->desc[n].bDescriptorType == HID_DT_REPORT)
                        rsize = le16_to_cpu(hdesc->desc[n].wDescriptorLength);
maintainer Applied
#7 lore raw
In reply to Jaejoong Kim
Quoted 9 lines · Jaejoong Kim

The hid descriptor identifies the length and type of subordinate descriptors for a device. If the received hid descriptor is smaller than the size of the struct hid_descriptor, it is possible to cause out-of-bounds.

In addition, if bNumDescriptors of the hid descriptor have an incorrect value, this can also cause out-of-bounds while approaching hdesc->desc[n].

So check the size of hid descriptor and bNumDescriptors.

Applied to for-4.14/upstream-fixes. Thanks,

Signature · 2 lines
Jiri Kosina
SUSE Labs