Skip to content

Commit 4f65245

Browse files
GustavoARSilvaJiri Kosina
authored andcommitted
HID: hiddev: fix potential Spectre v1
uref->field_index, uref->usage_index, finfo.field_index and cinfo.index can be indirectly controlled by user-space, hence leading to a potential exploitation of the Spectre variant 1 vulnerability. This issue was detected with the help of Smatch: drivers/hid/usbhid/hiddev.c:473 hiddev_ioctl_usage() warn: potential spectre issue 'report->field' (local cap) drivers/hid/usbhid/hiddev.c:477 hiddev_ioctl_usage() warn: potential spectre issue 'field->usage' (local cap) drivers/hid/usbhid/hiddev.c:757 hiddev_ioctl() warn: potential spectre issue 'report->field' (local cap) drivers/hid/usbhid/hiddev.c:801 hiddev_ioctl() warn: potential spectre issue 'hid->collection' (local cap) Fix this by sanitizing such structure fields before using them to index report->field, field->usage and hid->collection Notice that given that speculation windows are large, the policy is to kill the speculation on the first load and not worry if it can be completed with a dependent load/store [1]. [1] https://marc.info/?l=linux-kernel&m=152449131114778&w=2 Cc: [email protected] Signed-off-by: Gustavo A. R. Silva <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent ef6eaf2 commit 4f65245

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

drivers/hid/usbhid/hiddev.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <linux/hiddev.h>
3737
#include <linux/compat.h>
3838
#include <linux/vmalloc.h>
39+
#include <linux/nospec.h>
3940
#include "usbhid.h"
4041

4142
#ifdef CONFIG_USB_DYNAMIC_MINORS
@@ -469,10 +470,14 @@ static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd,
469470

470471
if (uref->field_index >= report->maxfield)
471472
goto inval;
473+
uref->field_index = array_index_nospec(uref->field_index,
474+
report->maxfield);
472475

473476
field = report->field[uref->field_index];
474477
if (uref->usage_index >= field->maxusage)
475478
goto inval;
479+
uref->usage_index = array_index_nospec(uref->usage_index,
480+
field->maxusage);
476481

477482
uref->usage_code = field->usage[uref->usage_index].hid;
478483

@@ -499,6 +504,8 @@ static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd,
499504

500505
if (uref->field_index >= report->maxfield)
501506
goto inval;
507+
uref->field_index = array_index_nospec(uref->field_index,
508+
report->maxfield);
502509

503510
field = report->field[uref->field_index];
504511

@@ -753,6 +760,8 @@ static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
753760

754761
if (finfo.field_index >= report->maxfield)
755762
break;
763+
finfo.field_index = array_index_nospec(finfo.field_index,
764+
report->maxfield);
756765

757766
field = report->field[finfo.field_index];
758767
memset(&finfo, 0, sizeof(finfo));
@@ -797,6 +806,8 @@ static long hiddev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
797806

798807
if (cinfo.index >= hid->maxcollection)
799808
break;
809+
cinfo.index = array_index_nospec(cinfo.index,
810+
hid->maxcollection);
800811

801812
cinfo.type = hid->collection[cinfo.index].type;
802813
cinfo.usage = hid->collection[cinfo.index].usage;

0 commit comments

Comments
 (0)