Skip to content

Commit bce1305

Browse files
Marc Zyngierbentiss
authored andcommitted
HID: core: Correctly handle ReportSize being zero
It appears that a ReportSize value of zero is legal, even if a bit non-sensical. Most of the HID code seems to handle that gracefully, except when computing the total size in bytes. When fed as input to memset, this leads to some funky outcomes. Detect the corner case and correctly compute the size. Cc: [email protected] Signed-off-by: Marc Zyngier <[email protected]> Signed-off-by: Benjamin Tissoires <[email protected]>
1 parent b7429ea commit bce1305

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

drivers/hid/hid-core.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,17 @@ static void hid_output_field(const struct hid_device *hid,
15971597
}
15981598
}
15991599

1600+
/*
1601+
* Compute the size of a report.
1602+
*/
1603+
static size_t hid_compute_report_size(struct hid_report *report)
1604+
{
1605+
if (report->size)
1606+
return ((report->size - 1) >> 3) + 1;
1607+
1608+
return 0;
1609+
}
1610+
16001611
/*
16011612
* Create a report. 'data' has to be allocated using
16021613
* hid_alloc_report_buf() so that it has proper size.
@@ -1609,7 +1620,7 @@ void hid_output_report(struct hid_report *report, __u8 *data)
16091620
if (report->id > 0)
16101621
*data++ = report->id;
16111622

1612-
memset(data, 0, ((report->size - 1) >> 3) + 1);
1623+
memset(data, 0, hid_compute_report_size(report));
16131624
for (n = 0; n < report->maxfield; n++)
16141625
hid_output_field(report->device, report->field[n], data);
16151626
}
@@ -1739,7 +1750,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
17391750
csize--;
17401751
}
17411752

1742-
rsize = ((report->size - 1) >> 3) + 1;
1753+
rsize = hid_compute_report_size(report);
17431754

17441755
if (report_enum->numbered && rsize >= HID_MAX_BUFFER_SIZE)
17451756
rsize = HID_MAX_BUFFER_SIZE - 1;

0 commit comments

Comments
 (0)