Commit d9d4b1e4 authored by Alan Stern's avatar Alan Stern Committed by Benjamin Tissoires

HID: Fix assumption that devices have inputs

The syzbot fuzzer found a slab-out-of-bounds write bug in the hid-gaff
driver.  The problem is caused by the driver's assumption that the
device must have an input report.  While this will be true for all
normal HID input devices, a suitably malicious device can violate the
assumption.

The same assumption is present in over a dozen other HID drivers.
This patch fixes them by checking that the list of hid_inputs for the
hid_device is nonempty before allowing it to be used.

Reported-and-tested-by: syzbot+403741a091bf41d4ae79@syzkaller.appspotmail.com
Signed-off-by: default avatarAlan Stern <stern@rowland.harvard.edu>
CC: <stable@vger.kernel.org>
Signed-off-by: default avatarBenjamin Tissoires <benjamin.tissoires@redhat.com>
parent fe2199cf
...@@ -63,13 +63,20 @@ static int axff_init(struct hid_device *hid) ...@@ -63,13 +63,20 @@ static int axff_init(struct hid_device *hid)
{ {
struct axff_device *axff; struct axff_device *axff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_first_entry(&hid->inputs, struct hid_input, list); struct hid_input *hidinput;
struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list; struct list_head *report_list =&hid->report_enum[HID_OUTPUT_REPORT].report_list;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
int field_count = 0; int field_count = 0;
int i, j; int i, j;
int error; int error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
dev = hidinput->input;
if (list_empty(report_list)) { if (list_empty(report_list)) {
hid_err(hid, "no output reports found\n"); hid_err(hid, "no output reports found\n");
return -ENODEV; return -ENODEV;
......
...@@ -75,13 +75,19 @@ static int drff_init(struct hid_device *hid) ...@@ -75,13 +75,19 @@ static int drff_init(struct hid_device *hid)
{ {
struct drff_device *drff; struct drff_device *drff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_first_entry(&hid->inputs, struct hid_input *hidinput;
struct hid_input, list);
struct list_head *report_list = struct list_head *report_list =
&hid->report_enum[HID_OUTPUT_REPORT].report_list; &hid->report_enum[HID_OUTPUT_REPORT].report_list;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
int error; int error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
dev = hidinput->input;
if (list_empty(report_list)) { if (list_empty(report_list)) {
hid_err(hid, "no output reports found\n"); hid_err(hid, "no output reports found\n");
return -ENODEV; return -ENODEV;
......
...@@ -47,13 +47,19 @@ static int emsff_init(struct hid_device *hid) ...@@ -47,13 +47,19 @@ static int emsff_init(struct hid_device *hid)
{ {
struct emsff_device *emsff; struct emsff_device *emsff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_first_entry(&hid->inputs, struct hid_input *hidinput;
struct hid_input, list);
struct list_head *report_list = struct list_head *report_list =
&hid->report_enum[HID_OUTPUT_REPORT].report_list; &hid->report_enum[HID_OUTPUT_REPORT].report_list;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
int error; int error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_first_entry(&hid->inputs, struct hid_input, list);
dev = hidinput->input;
if (list_empty(report_list)) { if (list_empty(report_list)) {
hid_err(hid, "no output reports found\n"); hid_err(hid, "no output reports found\n");
return -ENODEV; return -ENODEV;
......
...@@ -64,14 +64,20 @@ static int gaff_init(struct hid_device *hid) ...@@ -64,14 +64,20 @@ static int gaff_init(struct hid_device *hid)
{ {
struct gaff_device *gaff; struct gaff_device *gaff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input *hidinput;
struct hid_input, list);
struct list_head *report_list = struct list_head *report_list =
&hid->report_enum[HID_OUTPUT_REPORT].report_list; &hid->report_enum[HID_OUTPUT_REPORT].report_list;
struct list_head *report_ptr = report_list; struct list_head *report_ptr = report_list;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
int error; int error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
if (list_empty(report_list)) { if (list_empty(report_list)) {
hid_err(hid, "no output reports found\n"); hid_err(hid, "no output reports found\n");
return -ENODEV; return -ENODEV;
......
...@@ -124,13 +124,19 @@ static int holtekff_init(struct hid_device *hid) ...@@ -124,13 +124,19 @@ static int holtekff_init(struct hid_device *hid)
{ {
struct holtekff_device *holtekff; struct holtekff_device *holtekff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input *hidinput;
struct hid_input, list);
struct list_head *report_list = struct list_head *report_list =
&hid->report_enum[HID_OUTPUT_REPORT].report_list; &hid->report_enum[HID_OUTPUT_REPORT].report_list;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
int error; int error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
if (list_empty(report_list)) { if (list_empty(report_list)) {
hid_err(hid, "no output report found\n"); hid_err(hid, "no output report found\n");
return -ENODEV; return -ENODEV;
......
...@@ -50,11 +50,17 @@ int lg2ff_init(struct hid_device *hid) ...@@ -50,11 +50,17 @@ int lg2ff_init(struct hid_device *hid)
{ {
struct lg2ff_device *lg2ff; struct lg2ff_device *lg2ff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input *hidinput;
struct hid_input, list); struct input_dev *dev;
struct input_dev *dev = hidinput->input;
int error; int error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
/* Check that the report looks ok */ /* Check that the report looks ok */
report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7); report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7);
if (!report) if (!report)
......
...@@ -117,12 +117,19 @@ static const signed short ff3_joystick_ac[] = { ...@@ -117,12 +117,19 @@ static const signed short ff3_joystick_ac[] = {
int lg3ff_init(struct hid_device *hid) int lg3ff_init(struct hid_device *hid)
{ {
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); struct hid_input *hidinput;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
const signed short *ff_bits = ff3_joystick_ac; const signed short *ff_bits = ff3_joystick_ac;
int error; int error;
int i; int i;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
/* Check that the report looks ok */ /* Check that the report looks ok */
if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 35)) if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 35))
return -ENODEV; return -ENODEV;
......
...@@ -1253,8 +1253,8 @@ static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_produc ...@@ -1253,8 +1253,8 @@ static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_produc
int lg4ff_init(struct hid_device *hid) int lg4ff_init(struct hid_device *hid)
{ {
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); struct hid_input *hidinput;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
struct hid_report *report = list_entry(report_list->next, struct hid_report, list); struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
...@@ -1266,6 +1266,13 @@ int lg4ff_init(struct hid_device *hid) ...@@ -1266,6 +1266,13 @@ int lg4ff_init(struct hid_device *hid)
int mmode_ret, mmode_idx = -1; int mmode_ret, mmode_idx = -1;
u16 real_product_id; u16 real_product_id;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
/* Check that the report looks ok */ /* Check that the report looks ok */
if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
return -1; return -1;
......
...@@ -115,12 +115,19 @@ static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude) ...@@ -115,12 +115,19 @@ static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
int lgff_init(struct hid_device* hid) int lgff_init(struct hid_device* hid)
{ {
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); struct hid_input *hidinput;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
const signed short *ff_bits = ff_joystick; const signed short *ff_bits = ff_joystick;
int error; int error;
int i; int i;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
/* Check that the report looks ok */ /* Check that the report looks ok */
if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
return -ENODEV; return -ENODEV;
......
...@@ -2084,8 +2084,8 @@ static void hidpp_ff_destroy(struct ff_device *ff) ...@@ -2084,8 +2084,8 @@ static void hidpp_ff_destroy(struct ff_device *ff)
static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index) static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
{ {
struct hid_device *hid = hidpp->hid_dev; struct hid_device *hid = hidpp->hid_dev;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); struct hid_input *hidinput;
struct input_dev *dev = hidinput->input; struct input_dev *dev;
const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
struct ff_device *ff; struct ff_device *ff;
...@@ -2094,6 +2094,13 @@ static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index) ...@@ -2094,6 +2094,13 @@ static int hidpp_ff_init(struct hidpp_device *hidpp, u8 feature_index)
int error, j, num_slots; int error, j, num_slots;
u8 version; u8 version;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
if (!dev) { if (!dev) {
hid_err(hid, "Struct input_dev not set!\n"); hid_err(hid, "Struct input_dev not set!\n");
return -EINVAL; return -EINVAL;
......
...@@ -328,11 +328,17 @@ static int ms_play_effect(struct input_dev *dev, void *data, ...@@ -328,11 +328,17 @@ static int ms_play_effect(struct input_dev *dev, void *data,
static int ms_init_ff(struct hid_device *hdev) static int ms_init_ff(struct hid_device *hdev)
{ {
struct hid_input *hidinput = list_entry(hdev->inputs.next, struct hid_input *hidinput;
struct hid_input, list); struct input_dev *input_dev;
struct input_dev *input_dev = hidinput->input;
struct ms_data *ms = hid_get_drvdata(hdev); struct ms_data *ms = hid_get_drvdata(hdev);
if (list_empty(&hdev->inputs)) {
hid_err(hdev, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hdev->inputs.next, struct hid_input, list);
input_dev = hidinput->input;
if (!(ms->quirks & MS_QUIRK_FF)) if (!(ms->quirks & MS_QUIRK_FF))
return 0; return 0;
......
...@@ -2254,9 +2254,15 @@ static int sony_play_effect(struct input_dev *dev, void *data, ...@@ -2254,9 +2254,15 @@ static int sony_play_effect(struct input_dev *dev, void *data,
static int sony_init_ff(struct sony_sc *sc) static int sony_init_ff(struct sony_sc *sc)
{ {
struct hid_input *hidinput = list_entry(sc->hdev->inputs.next, struct hid_input *hidinput;
struct hid_input, list); struct input_dev *input_dev;
struct input_dev *input_dev = hidinput->input;
if (list_empty(&sc->hdev->inputs)) {
hid_err(sc->hdev, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(sc->hdev->inputs.next, struct hid_input, list);
input_dev = hidinput->input;
input_set_capability(input_dev, EV_FF, FF_RUMBLE); input_set_capability(input_dev, EV_FF, FF_RUMBLE);
return input_ff_create_memless(input_dev, NULL, sony_play_effect); return input_ff_create_memless(input_dev, NULL, sony_play_effect);
......
...@@ -124,12 +124,18 @@ static int tmff_init(struct hid_device *hid, const signed short *ff_bits) ...@@ -124,12 +124,18 @@ static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
struct tmff_device *tmff; struct tmff_device *tmff;
struct hid_report *report; struct hid_report *report;
struct list_head *report_list; struct list_head *report_list;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input *hidinput;
struct hid_input, list); struct input_dev *input_dev;
struct input_dev *input_dev = hidinput->input;
int error; int error;
int i; int i;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
input_dev = hidinput->input;
tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL); tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
if (!tmff) if (!tmff)
return -ENOMEM; return -ENOMEM;
......
...@@ -54,11 +54,17 @@ static int zpff_init(struct hid_device *hid) ...@@ -54,11 +54,17 @@ static int zpff_init(struct hid_device *hid)
{ {
struct zpff_device *zpff; struct zpff_device *zpff;
struct hid_report *report; struct hid_report *report;
struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input *hidinput;
struct hid_input, list); struct input_dev *dev;
struct input_dev *dev = hidinput->input;
int i, error; int i, error;
if (list_empty(&hid->inputs)) {
hid_err(hid, "no inputs found\n");
return -ENODEV;
}
hidinput = list_entry(hid->inputs.next, struct hid_input, list);
dev = hidinput->input;
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1); report = hid_validate_values(hid, HID_OUTPUT_REPORT, 0, i, 1);
if (!report) if (!report)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment