Skip to content

Commit f112743

Browse files
leitaoJiri Kosina
authored andcommitted
HID: hiddev: fix potential Spectre v1
uref->usage_index can be indirectly controlled by userspace, hence leading to a potential exploitation of the Spectre variant 1 vulnerability. This field is used as an array index by the hiddev_ioctl_usage() function, when 'cmd' is either HIDIOCGCOLLECTIONINDEX, HIDIOCGUSAGES or HIDIOCSUSAGES. For cmd == HIDIOCGCOLLECTIONINDEX case, uref->usage_index is compared to field->maxusage and then used as an index to dereference field->usage array. The same thing happens to the cmd == HIDIOC{G,S}USAGES cases, where uref->usage_index is checked against an array maximum value and then it is used as an index in an array. This is a summary of the HIDIOCGCOLLECTIONINDEX case, which matches the traditional Spectre V1 first load: copy_from_user(uref, user_arg, sizeof(*uref)) if (uref->usage_index >= field->maxusage) goto inval; i = field->usage[uref->usage_index].collection_index; return i; This patch fixes this by sanitizing field uref->usage_index before using it to index field->usage (HIDIOCGCOLLECTIONINDEX) or field->value in HIDIOC{G,S}USAGES arrays, thus, avoiding speculation in the first load. Cc: <stable@vger.kernel.org> Signed-off-by: Breno Leitao <leitao@debian.org> -- v2: Contemplate cmd == HIDIOC{G,S}USAGES case Signed-off-by: Jiri Kosina <jkosina@suse.cz>
1 parent 96f2f66 commit f112743

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

drivers/hid/usbhid/hiddev.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,14 +512,24 @@ static noinline int hiddev_ioctl_usage(struct hiddev *hiddev, unsigned int cmd,
512512
if (cmd == HIDIOCGCOLLECTIONINDEX) {
513513
if (uref->usage_index >= field->maxusage)
514514
goto inval;
515+
uref->usage_index =
516+
array_index_nospec(uref->usage_index,
517+
field->maxusage);
515518
} else if (uref->usage_index >= field->report_count)
516519
goto inval;
517520
}
518521

519-
if ((cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) &&
520-
(uref_multi->num_values > HID_MAX_MULTI_USAGES ||
521-
uref->usage_index + uref_multi->num_values > field->report_count))
522-
goto inval;
522+
if (cmd == HIDIOCGUSAGES || cmd == HIDIOCSUSAGES) {
523+
if (uref_multi->num_values > HID_MAX_MULTI_USAGES ||
524+
uref->usage_index + uref_multi->num_values >
525+
field->report_count)
526+
goto inval;
527+
528+
uref->usage_index =
529+
array_index_nospec(uref->usage_index,
530+
field->report_count -
531+
uref_multi->num_values);
532+
}
523533

524534
switch (cmd) {
525535
case HIDIOCGUSAGE:

0 commit comments

Comments
 (0)