HID: core: Add printk_ratelimited variants to hid_warn() etc
Subsystems (8)
PATCH 1/3 HID: core: Add printk_ratelimited variants to hid_warn() etc
From: Vicki Pfau <[email protected]>
hid_warn_ratelimited() is needed. Add the others as part of the block.
--- include/linux/hid.h | 11 +++++++++++ 1 file changed, 11 insertions(+)
include/linux/hid.h +11 -0
@@ -1248,4 +1248,15 @@ do { \#define hid_dbg_once (hid, fmt, ...) \ dev_dbg_once(&(hid)->dev, fmt, ##__VA_ARGS__) +#define hid_err_ratelimited (hid, fmt, ...) \ + dev_err_ratelimited(&(hid)->dev, fmt, ##__VA_ARGS__) +#define hid_notice_ratelimited (hid, fmt, ...) \ + dev_notice_ratelimited(&(hid)->dev, fmt, ##__VA_ARGS__) +#define hid_warn_ratelimited (hid, fmt, ...) \ + dev_warn_ratelimited(&(hid)->dev, fmt, ##__VA_ARGS__) +#define hid_info_ratelimited (hid, fmt, ...) \ + dev_info_ratelimited(&(hid)->dev, fmt, ##__VA_ARGS__) +#define hid_dbg_ratelimited (hid, fmt, ...) \ + dev_dbg_ratelimited(&(hid)->dev, fmt, ##__VA_ARGS__) +#endif
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
PATCH 2/3 HID: pass the buffer size to hid_report_raw_event
From: Benjamin Tissoires <[email protected]>
[ Upstream commit 2c85c61d1332e1e16f020d76951baf167dcb6f7a ]
commit 0a3fe972a7cb ("HID: core: Mitigate potential OOB by removing bogus memset()") enforced the provided data to be at least the size of the declared buffer in the report descriptor to prevent a buffer overflow. However, we can try to be smarter by providing both the buffer size and the data size, meaning that hid_report_raw_event() can make better decision whether we should plaining reject the buffer (buffer overflow attempt) or if we can safely memset it to 0 and pass it to the rest of the stack.
Stable-dep-of: 206342541fc8 ("HID: core: introduce hid_safe_input_report()")
--- drivers/hid/hid-core.c | 29 ++++++++++++++++++++++------- drivers/hid/hid-gfrm.c | 4 ++-- drivers/hid/hid-logitech-hidpp.c | 2 +- drivers/hid/hid-multitouch.c | 2 +- drivers/hid/hid-primax.c | 2 +- drivers/hid/hid-vivaldi.c | 2 +- drivers/hid/wacom_sys.c | 6 +++--- drivers/staging/greybus/hid.c | 2 +- include/linux/hid.h | 4 ++-- 9 files changed, 34 insertions(+), 19 deletions(-)
drivers/hid/hid-core.c +22 -7
@@ -1775,8 +1775,8 @@ int __hid_request(struct hid_device *hid, struct hid_report *report, }EXPORT_SYMBOL_GPL (__hid_request); -int hid_report_raw_event (struct hid_device *hid,int type,u8 *data,u32 size, -int interrupt) +int hid_report_raw_event (struct hid_device *hid,int type,u8 *data, +size_t bufsize,u32 size,int interrupt) {struct hid_report_enum *report_enum = hid->report_enum + type;struct hid_report *report;
@@ -1784,16 +1784,24 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,int max_buffer_size =HID_MAX_BUFFER_SIZE ;unsigned int a;u32 rsize, csize = size; +size_t bsize = bufsize;u8 *cdata = data;int ret =0 ; report =hid_get_report (report_enum, data);if (!report) - goto out; +return 0 ; + +if (unlikely (bsize < csize)) { +hid_warn_ratelimited (hid,"Event data for report %d is incorrect (%d vs %ld)\n" , + report->id, csize, bsize); +return -EINVAL ; + }if (report_enum->numbered) { cdata++; csize--; + bsize--; } rsize =hid_compute_report_size (report);
@@ -1806,9 +1814,15 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,else if (rsize > max_buffer_size) rsize = max_buffer_size; +if (bsize < rsize) { +hid_warn_ratelimited (hid,"Event data for report %d was too short (%d vs %ld)\n" , + report->id, rsize, bsize); +return -EINVAL ; + } +if (csize < rsize) {dbg_hid ("report %d is too short, (%d < %d)\n" , report->id, - csize, rsize); + csize, rsize);memset (cdata + csize,0 , rsize - csize); }
@@ -1817,7 +1831,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,if (hid->claimed &HID_CLAIMED_HIDRAW ) { ret =hidraw_report_event (hid, data, size);if (ret) - goto out; +return ret; }if (hid->claimed !=HID_CLAIMED_HIDRAW && report->maxfield) {
@@ -1830,7 +1844,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,if (hid->claimed &HID_CLAIMED_INPUT )hidinput_report_event (hid, report); -out: +return ret; }EXPORT_SYMBOL_GPL (hid_report_raw_event);
@@ -1851,6 +1865,7 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int istruct hid_report_enum *report_enum;struct hid_driver *hdrv;struct hid_report *report; +size_t bufsize = size;int ret =0 ;if (!hid)
@@ -1889,7 +1904,7 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i goto unlock; } - ret =hid_report_raw_event (hid, type, data, size, interrupt); + ret =hid_report_raw_event (hid, type, data, bufsize, size, interrupt); unlock:up (&hid->driver_input_lock);
drivers/hid/hid-gfrm.c +2 -2
@@ -66,7 +66,7 @@ static int gfrm_raw_event(struct hid_device *hdev, struct hid_report *report,switch (data[1 ]) {case GFRM100_SEARCH_KEY_DOWN : ret =hid_report_raw_event (hdev,HID_INPUT_REPORT , search_key_dn, -sizeof (search_key_dn),1 ); +sizeof (search_key_dn),sizeof (search_key_dn),1 );break ;case GFRM100_SEARCH_KEY_AUDIO_DATA :
@@ -74,7 +74,7 @@ static int gfrm_raw_event(struct hid_device *hdev, struct hid_report *report,case GFRM100_SEARCH_KEY_UP : ret =hid_report_raw_event (hdev,HID_INPUT_REPORT , search_key_up, -sizeof (search_key_up),1 ); +sizeof (search_key_up),sizeof (search_key_up),1 );break ;default :
drivers/hid/hid-logitech-hidpp.c +1 -1
@@ -3440,7 +3440,7 @@ static int hidpp10_consumer_keys_raw_event(struct hidpp_device *hidpp,memcpy (&consumer_report[1 ], &data[3 ],4 );/* We are called from atomic context */ hid_report_raw_event (hidpp->hid_dev,HID_INPUT_REPORT , - consumer_report,5 ,1 ); + consumer_report,sizeof (consumer_report),5 ,1 );return 1 ; }
drivers/hid/hid-multitouch.c +1 -1
@@ -479,7 +479,7 @@ static void mt_get_feature(struct hid_device *hdev, struct hid_report *report) } ret =hid_report_raw_event (hdev,HID_FEATURE_REPORT , buf, - size,0 ); + size, size,0 );if (ret)dev_warn (&hdev->dev,"failed to report feature\n" ); }
drivers/hid/hid-primax.c +1 -1
@@ -44,7 +44,7 @@ static int px_raw_event(struct hid_device *hid, struct hid_report *report, data[0 ] |= (1 << (data[idx] -0xE0 )); data[idx] =0 ; } -hid_report_raw_event (hid,HID_INPUT_REPORT , data, size,0 ); +hid_report_raw_event (hid,HID_INPUT_REPORT , data, size, size,0 );return 1 ;default :/* unknown report */
drivers/hid/hid-vivaldi.c +1 -1
@@ -126,7 +126,7 @@ static void vivaldi_feature_mapping(struct hid_device *hdev, } ret =hid_report_raw_event (hdev,HID_FEATURE_REPORT , report_data, - report_len,0 ); + report_len, report_len,0 );if (ret) {dev_warn (&hdev->dev,"failed to report feature %d\n" , field->report->id);
drivers/hid/wacom_sys.c +3 -3
@@ -79,7 +79,7 @@ static void wacom_wac_queue_flush(struct hid_device *hdev,int err; size =kfifo_out (fifo, buf,sizeof (buf)); - err =hid_report_raw_event (hdev,HID_INPUT_REPORT , buf, size, false); + err =hid_report_raw_event (hdev,HID_INPUT_REPORT , buf, size, size, false);if (err) {hid_warn (hdev,"%s: unable to flush event due to error %d\n" , __func__, err);
@@ -324,7 +324,7 @@ static void wacom_feature_mapping(struct hid_device *hdev, data, n, WAC_CMD_RETRIES);if (ret == n && features->type ==HID_GENERIC ) { ret =hid_report_raw_event (hdev, -HID_FEATURE_REPORT , data, n,0 ); +HID_FEATURE_REPORT , data, n, n,0 ); }else if (ret ==2 && features->type !=HID_GENERIC ) { features->touch_max = data[1 ]; }else {
@@ -385,7 +385,7 @@ static void wacom_feature_mapping(struct hid_device *hdev, data, n, WAC_CMD_RETRIES);if (ret == n) { ret =hid_report_raw_event (hdev,HID_FEATURE_REPORT , - data, n,0 ); + data, n, n,0 ); }else {hid_warn (hdev,"%s: could not retrieve sensor offsets\n" , __func__);
drivers/staging/greybus/hid.c +1 -1
@@ -201,7 +201,7 @@ static void gb_hid_init_report(struct gb_hid *ghid, struct hid_report *report)* we just need to setup the input fields, so using * hid_report_raw_event is safe. */ -hid_report_raw_event (ghid->hid, report->type, ghid->inbuf, size,1 ); +hid_report_raw_event (ghid->hid, report->type, ghid->inbuf, ghid->bufsize, size,1 ); }static void gb_hid_init_reports (struct gb_hid *ghid)
include/linux/hid.h +2 -2
@@ -1206,8 +1206,8 @@ static inline u32 hid_report_len(struct hid_report *report)return DIV_ROUND_UP (report->size,8 ) + (report->id >0 ); } -int hid_report_raw_event (struct hid_device *hid,int type,u8 *data,u32 size, -int interrupt); +int hid_report_raw_event (struct hid_device *hid,int type,u8 *data, +size_t bufsize,u32 size,int interrupt);/* HID quirks API */ unsigned long hid_lookup_quirk (const struct hid_device *hdev);
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
PATCH 3/3 HID: core: Fix size_t specifier in hid_report_raw_event()
In reply to Lee JonesFrom: Nathan Chancellor <[email protected]>
[ Upstream commit 4d3a2a466b8d68d852a1f3bbf11204b718428dc4 ]
When building for 32-bit platforms, for which 'size_t' is 'unsigned int', there are warnings around using the incorrect format specifier to print bsize in hid_report_raw_event():
drivers/hid/hid-core.c:2054:29: error: format specifies type 'long' but the argument has type 'size_t' (aka 'unsigned int') [-Werror,-Wformat]
2053 | hid_warn_ratelimited(hid, "Event data for report %d is incorrect (%d vs %ld)\n",
| ~~~
| %zu
2054 | report->id, csize, bsize);
| ^~~~~
drivers/hid/hid-core.c:2076:29: error: format specifies type 'long' but the argument has type 'size_t' (aka 'unsigned int') [-Werror,-Wformat]
2075 | hid_warn_ratelimited(hid, "Event data for report %d was too short (%d vs %ld)\n",
| ~~~
| %zu
2076 | report->id, rsize, bsize);
| ^~~~~Use the proper 'size_t' format specifier, '%zu', to clear up the warnings.
--- drivers/hid/hid-core.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/hid/hid-core.c +2 -2
@@ -1793,7 +1793,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data,return 0 ;if (unlikely (bsize < csize)) { -hid_warn_ratelimited (hid,"Event data for report %d is incorrect (%d vs %ld)\n" , +hid_warn_ratelimited (hid,"Event data for report %d is incorrect (%d vs %zu)\n" , report->id, csize, bsize);return -EINVAL ; }
@@ -1815,7 +1815,7 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, rsize = max_buffer_size;if (bsize < rsize) { -hid_warn_ratelimited (hid,"Event data for report %d was too short (%d vs %ld)\n" , +hid_warn_ratelimited (hid,"Event data for report %d was too short (%d vs %zu)\n" , report->id, rsize, bsize);return -EINVAL ; }
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
[email protected]
AI reviewRe: [linux-5.15.y 2/3] HID: pass the buffer size to hid_report_raw_event
In reply to Lee JonesThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] The patch fails to resolve the short report rejection regression for most HID devices because…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] The patch fails to resolve the short report rejection regression for most HID devices because hid_input_report() unconditionally sets bufsize = size.
- [High] Incorrect bufsize argument in wacom_wac_queue_flush defeats the mitigation for short reports.
- [Low] Format string mismatch for size_t variable bsize causes compiler warnings and potential build failures on 32-bit platforms. --
commit 410a0d731bb14f8c3685faa86ca65503898cc6f0
Author: Benjamin Tissoires <[email protected]>
HID: pass the buffer size to hid_report_raw_event
This commit passes the buffer size along with the data size to hid_report_raw_event() to make a better decision on whether to reject the buffer or safely memset it to 0 and pass it to the rest of the stack.
Quoted 21 lines
drivers/hid/hid-core.c +3
-2@@ -1775,8 +1775,8 @@ int __hid_request(struct hid_device *hid, struct hid_report *report, }EXPORT_SYMBOL_GPL (__hid_request); -int hid_report_raw_event (struct hid_device *hid,int type,u8 *data,u32 size, -int interrupt) +int hid_report_raw_event (struct hid_device *hid,int type,u8 *data, +size_t bufsize,u32 size,int interrupt) {struct hid_report_enum *report_enum = hid->report_enum + type;struct hid_report *report;@@ -1784,16 +1784,24 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,int max_buffer_size =HID_MAX_BUFFER_SIZE ;unsigned int a;u32 rsize, csize = size; +size_t bsize = bufsize;u8 *cdata = data;
drivers/hid/hid-core.c int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, · line 1797
Quoted 10 lines
drivers/hid/hid-core.c +5
-1int ret =0 ; report =hid_get_report (report_enum, data);if (!report) - goto out; +return 0 ; + +if (unlikely (bsize < csize)) { +hid_warn_ratelimited (hid,"Event data for report %d is incorrect (%d vs %ld)\n" , + report->id, csize, bsize);
[Severity: Low]
Does this cause a compiler warning on 32-bit platforms? bsize is a size_t, but is being printed with the %ld format specifier, which might trigger a -Wformat warning since size_t is usually an unsigned int on those architectures. Should this use %zu instead?
Quoted 11 lines
drivers/hid/hid-core.c +3
-0+return -EINVAL ; + }if (report_enum->numbered) { cdata++; csize--; + bsize--; } rsize =hid_compute_report_size (report);
drivers/hid/hid-core.c int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size, · line 1819
Quoted 6 lines
drivers/hid/hid-core.c +3
-0@@ -1806,9 +1814,15 @@ int hid_report_raw_event(struct hid_device *hid, int type, u8 *data, u32 size,else if (rsize > max_buffer_size) rsize = max_buffer_size; +if (bsize < rsize) { +hid_warn_ratelimited (hid,"Event data for report %d was too short (%d vs %ld)\n" , + report->id, rsize, bsize);
[Severity: Low]
This isn't a bug, but there's another instance here where bsize is printed with %ld instead of %zu.
Quoted 9 lines
drivers/hid/hid-core.c +4
-1+return -EINVAL ; + } +if (csize < rsize) {dbg_hid ("report %d is too short, (%d < %d)\n" , report->id, - csize, rsize); + csize, rsize);memset (cdata + csize,0 , rsize - csize); }
[ ... ]
Quoted 9 lines
drivers/hid/hid-core.c +1
-0@@ -1851,6 +1865,7 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int istruct hid_report_enum *report_enum;struct hid_driver *hdrv;struct hid_report *report; +size_t bufsize = size;int ret =0 ;if (!hid)
drivers/hid/hid-core.c int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i · line 1907
Quoted 5 lines
drivers/hid/hid-core.c +1
-1@@ -1889,7 +1904,7 @@ int hid_input_report(struct hid_device *hid, int type, u8 *data, u32 size, int i goto unlock; } - ret =hid_report_raw_event (hid, type, data, size, interrupt); + ret =hid_report_raw_event (hid, type, data, bufsize, size, interrupt);
[Severity: High]
Does this defeat the short report mitigation for most devices?
Since hid_input_report() is the primary entry point for HID transport drivers and lacks a bufsize argument, it sets bufsize equal to size (the payload length). When a short report is received, this guarantees that bsize < rsize evaluates to true in hid_report_raw_event().
Will this incorrectly drop valid short reports for most HID devices by triggering the -EINVAL rejection path?
Quoted 3 lines
drivers/hid/hid-core.c +0
-0unlock:up (&hid->driver_input_lock);
[ ... ]
drivers/hid/wacom_sys.c static void wacom_wac_queue_flush(struct hid_device *hdev, · line 82
Quoted 10 lines
drivers/hid/wacom_sys.c +1
-1@@ -79,7 +79,7 @@ static void wacom_wac_queue_flush(struct hid_device *hdev,int err; size =kfifo_out (fifo, buf,sizeof (buf)); - err =hid_report_raw_event (hdev,HID_INPUT_REPORT , buf, size, false); + err =hid_report_raw_event (hdev,HID_INPUT_REPORT , buf, size, size, false);
[Severity: High]
Should the bufsize argument here be sizeof(buf) instead of size?
Because bufsize is being passed as the short data length rather than the true allocated buffer size, hid_report_raw_event() will evaluate bsize < rsize as true and incorrectly reject valid short wacom reports with -EINVAL.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Sasha Levin
maintainerQuoted 1 line
[linux-5.15.y 1/3] HID: core: Add
printk_ratelimitedvariants tohid_warn()etc
Whole series queued for 5.15.y, thanks.
Signature · 2 lines
Thanks, Sasha