Skip to content

Commit

Permalink
HID: core: Correctly handle ReportSize being zero
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Marc Zyngier authored and bentiss committed Sep 1, 2020
1 parent b7429ea commit bce1305
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions drivers/hid/hid-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,17 @@ static void hid_output_field(const struct hid_device *hid,
}
}

/*
* Compute the size of a report.
*/
static size_t hid_compute_report_size(struct hid_report *report)
{
if (report->size)
return ((report->size - 1) >> 3) + 1;

return 0;
}

/*
* Create a report. 'data' has to be allocated using
* hid_alloc_report_buf() so that it has proper size.
Expand All @@ -1609,7 +1620,7 @@ void hid_output_report(struct hid_report *report, __u8 *data)
if (report->id > 0)
*data++ = report->id;

memset(data, 0, ((report->size - 1) >> 3) + 1);
memset(data, 0, hid_compute_report_size(report));
for (n = 0; n < report->maxfield; n++)
hid_output_field(report->device, report->field[n], data);
}
Expand Down Expand Up @@ -1739,7 +1750,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,
csize--;
}

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

if (report_enum->numbered && rsize >= HID_MAX_BUFFER_SIZE)
rsize = HID_MAX_BUFFER_SIZE - 1;
Expand Down

0 comments on commit bce1305

Please sign in to comment.