HID: core: fix __hid_request when no report ID is used
Subsystems (4)
Benjamin Tissoires
maintainerv2 cover HID: core: fix __hid_request when no report ID is used
New version (unchanged for patches 1-3), with a test added so we can detect this.
Followup of https://lore.kernel.org/linux-input/[email protected]/
This initial series attempt at fixing the various bugs discovered by Alan regarding __hid_request().
Syzbot managed to create a report descriptor which presents a feature request of size 0 (still trying to extract it) and this exposed the fact that __hid_request() was incorrectly handling the case when the report ID is not used.
Send a first batch of fixes now so we get the feedback from syzbot ASAP.
Note: in the series, I also mentioned that the report of size 0 should be stripped out of the HID device, but I'm not entirely sure this would be a good idea in the end.
---
Changes in v2:
- added Tested-by from syzbot (https://lore.kernel.org/r/[email protected])
- added a python test for it
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Benjamin Tissoires (4):
HID: core: ensure the allocated report buffer can contain the reserved report ID
HID: core: ensure __hid_request reserves the report ID as the first byte
HID: core: do not bypass hid_hw_raw_request
selftests/hid: add a test case for the recent syzbot underflow
drivers/hid/hid-core.c | 19 +++++-- tools/testing/selftests/hid/tests/test_mouse.py | 70 +++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 5 deletions(-)
---
base-commit: 1f988d0788f50d8464f957e793fab356e2937369
change-id: 20250709-report-size-null-37619ea20288
Best regards,
Signature · 1 line
Benjamin Tissoires <[email protected]>
Benjamin Tissoires
maintainerPATCH v2 1/4 HID: core: ensure the allocated report buffer can contain the reserved report ID
When the report ID is not used, the low level transport drivers expect the first byte to be 0. However, currently the allocated buffer not account for that extra byte, meaning that instead of having 8 guaranteed bytes for implement to be working, we only have 7.
--- drivers/hid/hid-core.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/hid/hid-core.c +4 -1
@@ -1883,9 +1883,12 @@ u8 *hid_alloc_report_buf(struct hid_report *report, gfp_t flags)/* * 7 extra bytes are necessary to achieve proper functionality * of implement() working on 8 byte chunks +* 1 extra byte for the report ID if it is null (not used) so +* we can reserve that extra byte in the first position of the buffer +* when sending it to .raw_request() */ -u32 len =hid_report_len (report) +7 ; +u32 len =hid_report_len (report) +7 + (report->id ==0 );return kzalloc (len, flags); }
Signature · 1 line
2.49.0
Benjamin Tissoires
maintainerPATCH v2 2/4 HID: core: ensure __hid_request reserves the report ID as the first byte
In reply to Benjamin TissoiresThe low level transport driver expects the first byte to be the report ID, even when the report ID is not use (in which case they just shift the buffer).
However, __hid_request() whas not offsetting the buffer it used by one in this case, meaning that the raw_request() callback emitted by the transport driver would be stripped of the first byte.
--- drivers/hid/hid-core.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/hid/hid-core.c +9 -2
@@ -1976,7 +1976,7 @@ static struct hid_report *hid_get_report(struct hid_report_enum *report_enum,int __hid_request (struct hid_device *hid,struct hid_report *report,enum hid_class_request reqtype) { -char *buf; +char *buf, *data_buf;int ret;u32 len;
@@ -1984,10 +1984,17 @@ int __hid_request(struct hid_device *hid, struct hid_report *report,if (!buf)return -ENOMEM ; + data_buf = buf; len =hid_report_len (report); +if (report->id ==0 ) { +/* reserve the first byte for the report ID */ + data_buf++; + len++; + } +if (reqtype ==HID_REQ_SET_REPORT ) -hid_output_report (report, buf); +hid_output_report (report, data_buf); ret = hid->ll_driver->raw_request (hid, report->id, buf, len, report->type, reqtype);
Signature · 1 line
2.49.0
Benjamin Tissoires
maintainerPATCH v2 3/4 HID: core: do not bypass hid_hw_raw_request
In reply to Benjamin Tissoireshid_hw_raw_request() is actually useful to ensure the provided buffer and length are valid. Directly calling in the low level transport driver function bypassed those checks and allowed invalid paramto be used.
--- drivers/hid/hid-core.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hid/hid-core.c +1 -2
@@ -1996,8 +1996,7 @@ int __hid_request(struct hid_device *hid, struct hid_report *report,if (reqtype ==HID_REQ_SET_REPORT )hid_output_report (report, data_buf); - ret = hid->ll_driver->raw_request (hid, report->id, buf, len, - report->type, reqtype); + ret =hid_hw_raw_request (hid, report->id, buf, len, report->type, reqtype);if (ret <0 ) {dbg_hid ("unable to complete request: %d\n" , ret); goto out;
Signature · 1 line
2.49.0
Benjamin Tissoires
maintainerPATCH v2 4/4 selftests/hid: add a test case for the recent syzbot underflow
In reply to Benjamin TissoiresSyzbot found a buffer underflow in __hid_request(). Add a related test case for it.
It's not perfect, but it allows to catch a corner case when a report descriptor is crafted so that it has a size of 0.
--- tools/testing/selftests/hid/tests/test_mouse.py | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+)
tools/testing/selftests/hid/tests/test_mouse.py +70 -0
@@ -439,6 +439,68 @@ class BadResolutionMultiplierMouse(ResolutionMultiplierMouse): return 32 # EPIPE +class BadReportDescriptorMouse(BaseMouse): + """ + This "device" was one autogenerated by syzbot. There are a lot of issues in + it, and the most problematic is that it declares features that have no + size. + + This leads to report->size being set to 0 and can mess up with usbhid + internals. Fortunately, uhid merely passes the incoming buffer, without + touching it so a buffer of size 0 will be translated to [] without + triggering a kernel oops. + + Because the report descriptor is wrong, no input are created, and we need + to tweak a little bit the parameters to make it look correct. + """ + + # fmt: off + report_descriptor = [ + 0x96, 0x01, 0x00, # Report Count (1) 0 + 0x06, 0x01, 0x00, # Usage Page (Generic Desktop) 3 + # 0x03, 0x00, 0x00, 0x00, 0x00, # Ignored by the kernel somehow + 0x2a, 0x90, 0xa0, # Usage Maximum (41104) 6 + 0x27, 0x00, 0x00, 0x00, 0x00, # Logical Maximum (0) 9 + 0xb3, 0x81, 0x3e, 0x25, 0x03, # Feature (Cnst,Arr,Abs,Vol) 14 + 0x1b, 0xdd, 0xe8, 0x40, 0x50, # Usage Minimum (1346431197) 19 + 0x3b, 0x5d, 0x8c, 0x3d, 0xda, # Designator Index 24 + ] + # fmt: on + + def __init__( + self, rdesc=report_descriptor, name=None, input_info=(3, 0x045E, 0x07DA) + ): + super().__init__(rdesc, name, input_info) + self.high_resolution_report_called = False + + def get_evdev(self, application=None): + assert self._input_nodes is None + return ( + "Ok" # should be a list or None, but both would fail, so abusing the system + ) + + def next_sync_events(self, application=None): + # there are no evdev nodes, so no events + return [] + + def is_ready(self): + # we wait for the SET_REPORT command to come + return self.high_resolution_report_called + + def set_report(self, req, rnum, rtype, data): + if rtype != self.UHID_FEATURE_REPORT: + raise InvalidHIDCommunication(f"Unexpected report type: {rtype}") + if rnum != 0x0: + raise InvalidHIDCommunication(f"Unexpected report number: {rnum}") + + if len(data) != 1: + raise InvalidHIDCommunication(f"Unexpected data: {data}, expected '[0]'") + + self.high_resolution_report_called = True + + return 0 + + class ResolutionMultiplierHWheelMouse(TwoWheelMouse): # fmt: off report_descriptor = [
@@ -975,3 +1037,11 @@ class TestMiMouse(TestWheelMouse): # assert below print out the real error pass assert remaining == [] + + +class TestBadReportDescriptorMouse(base.BaseTestCase.TestUhid): + def create_device(self): + return BadReportDescriptorMouse() + + def assertName(self, uhdev): + pass
Signature · 1 line
2.49.0
Benjamin Tissoires
maintainerRe: [PATCH v2 4/4] selftests/hid: add a test case for the recent syzbot underflow
Quoted 65 lines · Benjamin Tissoires
Syzbot found a buffer underflow in
__hid_request(). Add a related test case for it.It's not perfect, but it allows to catch a corner case when a report descriptor is crafted so that it has a size of 0.
Signed-off-by Benjamin Tissoires <[email protected]>--- tools/testing/selftests/hid/tests/test_mouse.py | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+)tools/testing/selftests/hid/tests/test_mouse.py +46
-0@@ -439,6 +439,68 @@ class BadResolutionMultiplierMouse(ResolutionMultiplierMouse): return 32 # EPIPE +class BadReportDescriptorMouse(BaseMouse): + """ + This "device" was one autogenerated by syzbot. There are a lot of issues in + it, and the most problematic is that it declares features that have no + size. + + This leads to report->size being set to 0 and can mess up with usbhid + internals. Fortunately, uhid merely passes the incoming buffer, without + touching it so a buffer of size 0 will be translated to [] without + triggering a kernel oops. + + Because the report descriptor is wrong, no input are created, and we need + to tweak a little bit the parameters to make it look correct. + """ + + # fmt: off + report_descriptor = [ + 0x96, 0x01, 0x00, # Report Count (1) 0 + 0x06, 0x01, 0x00, # Usage Page (Generic Desktop) 3 + # 0x03, 0x00, 0x00, 0x00, 0x00, # Ignored by the kernel somehow + 0x2a, 0x90, 0xa0, # Usage Maximum (41104) 6 + 0x27, 0x00, 0x00, 0x00, 0x00, # Logical Maximum (0) 9 + 0xb3, 0x81, 0x3e, 0x25, 0x03, # Feature (Cnst,Arr,Abs,Vol) 14 + 0x1b, 0xdd, 0xe8, 0x40, 0x50, # Usage Minimum (1346431197) 19 + 0x3b, 0x5d, 0x8c, 0x3d, 0xda, # Designator Index 24 + ] + # fmt: on + + def __init__( + self, rdesc=report_descriptor, name=None, input_info=(3, 0x045E, 0x07DA) + ): + super().__init__(rdesc, name, input_info) + self.high_resolution_report_called = False + + def get_evdev(self, application=None): + assert self._input_nodes is None + return ( + "Ok" # should be a list or None, but both would fail, so abusing the system + ) + + def next_sync_events(self, application=None): + # there are no evdev nodes, so no events + return [] + + def is_ready(self): + # we wait for the SET_REPORT command to come
tools/testing/selftests/hid/tests/test_mouse.py class BadResolutionMultiplierMouse(ResolutionMultiplierMouse): · line 497
Quoted 10 lines
tools/testing/selftests/hid/tests/test_mouse.py +10
-0+ return self.high_resolution_report_called + + def set_report(self, req, rnum, rtype, data): + if rtype != self.UHID_FEATURE_REPORT: + raise InvalidHIDCommunication(f"Unexpected report type: {rtype}") + if rnum != 0x0: + raise InvalidHIDCommunication(f"Unexpected report number: {rnum}") + + if len(data) != 1: + raise InvalidHIDCommunication(f"Unexpected data: {data}, expected '[0]'")
For the record, while thinking more about this, I realized that I changed the API for uhid with the previous patches.
*But* after second thoughts, every request to a HID device made through hid_hw_request() would see that change, but every request made through hid_hw_raw_request() already has the new behaviour. So that means that the users are already facing situations where they might have or not the first byte being the null report ID when it is 0, so, maybe we are making things more straightforward in the end.
Cheers,
Benjamin
Quoted 25 lines
tools/testing/selftests/hid/tests/test_mouse.py +14
-0+ + self.high_resolution_report_called = True + + return 0 + + class ResolutionMultiplierHWheelMouse(TwoWheelMouse): # fmt: off report_descriptor = [@@ -975,3 +1037,11 @@ class TestMiMouse(TestWheelMouse): # assert below print out the real error pass assert remaining == [] + + +class TestBadReportDescriptorMouse(base.BaseTestCase.TestUhid): + def create_device(self): + return BadReportDescriptorMouse() + + def assertName(self, uhdev): + passSignature · 2 lines
-- 2.49.0
Benjamin Tissoires
maintainerRe: [PATCH v2 4/4] selftests/hid: add a test case for the recent syzbot underflow
Quoted 80 lines · Benjamin Tissoires
Quoted 65 lines · Benjamin Tissoires
Syzbot found a buffer underflow in
__hid_request(). Add a related test case for it.It's not perfect, but it allows to catch a corner case when a report descriptor is crafted so that it has a size of 0.
Signed-off-by Benjamin Tissoires <[email protected]>--- tools/testing/selftests/hid/tests/test_mouse.py | 70 +++++++++++++++++++++++++ 1 file changed, 70 insertions(+)tools/testing/selftests/hid/tests/test_mouse.py +46
-0@@ -439,6 +439,68 @@ class BadResolutionMultiplierMouse(ResolutionMultiplierMouse): return 32 # EPIPE +class BadReportDescriptorMouse(BaseMouse): + """ + This "device" was one autogenerated by syzbot. There are a lot of issues in + it, and the most problematic is that it declares features that have no + size. + + This leads to report->size being set to 0 and can mess up with usbhid + internals. Fortunately, uhid merely passes the incoming buffer, without + touching it so a buffer of size 0 will be translated to [] without + triggering a kernel oops. + + Because the report descriptor is wrong, no input are created, and we need + to tweak a little bit the parameters to make it look correct. + """ + + # fmt: off + report_descriptor = [ + 0x96, 0x01, 0x00, # Report Count (1) 0 + 0x06, 0x01, 0x00, # Usage Page (Generic Desktop) 3 + # 0x03, 0x00, 0x00, 0x00, 0x00, # Ignored by the kernel somehow + 0x2a, 0x90, 0xa0, # Usage Maximum (41104) 6 + 0x27, 0x00, 0x00, 0x00, 0x00, # Logical Maximum (0) 9 + 0xb3, 0x81, 0x3e, 0x25, 0x03, # Feature (Cnst,Arr,Abs,Vol) 14 + 0x1b, 0xdd, 0xe8, 0x40, 0x50, # Usage Minimum (1346431197) 19 + 0x3b, 0x5d, 0x8c, 0x3d, 0xda, # Designator Index 24 + ] + # fmt: on + + def __init__( + self, rdesc=report_descriptor, name=None, input_info=(3, 0x045E, 0x07DA) + ): + super().__init__(rdesc, name, input_info) + self.high_resolution_report_called = False + + def get_evdev(self, application=None): + assert self._input_nodes is None + return ( + "Ok" # should be a list or None, but both would fail, so abusing the system + ) + + def next_sync_events(self, application=None): + # there are no evdev nodes, so no events + return [] + + def is_ready(self): + # we wait for the SET_REPORT command to cometools/testing/selftests/hid/tests/test_mouse.py class BadResolutionMultiplierMouse(ResolutionMultiplierMouse): · line 497
Quoted 10 lines
tools/testing/selftests/hid/tests/test_mouse.py +10
-0+ return self.high_resolution_report_called + + def set_report(self, req, rnum, rtype, data): + if rtype != self.UHID_FEATURE_REPORT: + raise InvalidHIDCommunication(f"Unexpected report type: {rtype}") + if rnum != 0x0: + raise InvalidHIDCommunication(f"Unexpected report number: {rnum}") + + if len(data) != 1: + raise InvalidHIDCommunication(f"Unexpected data: {data}, expected '[0]'")For the record, while thinking more about this, I realized that I changed the API for uhid with the previous patches.
Quoted 7 lines
*But* after second thoughts, every request to a HID device made through
hid_hw_request()would see that change, but every request made throughhid_hw_raw_request()already has the new behaviour. So that means that the users are already facing situations where they might have or not the first byte being the null report ID when it is 0, so, maybe we are making things more straightforward in the end.
Looking into this more:
- uhid is mainly used for BLE devices
- uhid is also used for testing, but I don't see that change a big issue
- for BLE devices, we can check which kernel module is calling hid_hw_request()
- and in those modules, we can check which are using a Bluetooth device
- and then we can check if the command is used with a report ID or not.
Doing all the checks above gives me confidence that the only time the report ID 0 is used is when using the resolution multiplier. I don't expect a lot of BLE device without report ID to expose a feature with a high resolution wheel.
But for a more extensive checking:
https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/profiles/input/hog-lib.c#n879
in function set_report(), bluez does the same shift if the report ID is 0 and the given buffer has a size > 0.
So I think this patch will also fix a hypothetical BLE device without report ID with high resolution wheel.
Therefore, I'm going to merge this series as it is (and include those blobs in the commit description).
Cheers,
Benjamin
Quoted 25 lines
Quoted 25 lines
tools/testing/selftests/hid/tests/test_mouse.py +14
-0+ + self.high_resolution_report_called = True + + return 0 + + class ResolutionMultiplierHWheelMouse(TwoWheelMouse): # fmt: off report_descriptor = [@@ -975,3 +1037,11 @@ class TestMiMouse(TestWheelMouse): # assert below print out the real error pass assert remaining == [] + + +class TestBadReportDescriptorMouse(base.BaseTestCase.TestUhid): + def create_device(self): + return BadReportDescriptorMouse() + + def assertName(self, uhdev): + passSignature · 2 lines
-- 2.49.0
Quoted 9 lines · Benjamin Tissoires
New version (unchanged for patches 1-3), with a test added so we can detect this.
Followup of https://lore.kernel.org/linux-input/[email protected]/
This initial series attempt at fixing the various bugs discovered by Alan regarding
__hid_request().[...]
Applied to hid/hid.git (for-6.16/upstream-fixes), thanks!
[1/4] HID: core: ensure the allocated report buffer can contain the reserved report ID
https://git.kernel.org/hid/hid/c/4f15ee98304b [2/4] HID: core: ensure __hid_request reserves the report ID as the first byte https://git.kernel.org/hid/hid/c/0d0777ccaa2d [3/4] HID: core: do not bypass hid_hw_raw_request https://git.kernel.org/hid/hid/c/c2ca42f190b6 [4/4] selftests/hid: add a test case for the recent syzbot underflow https://git.kernel.org/hid/hid/c/3a1d22bd8538
Cheers,
Signature · 1 line
Benjamin Tissoires <[email protected]>
PATCH 1/1 HID: core: Improve the kerneldoc for hid_report_len()
The kerneldoc for hid_report_len() needs to be improved. The description of the @report argument is ungrammatical, and the documentation does not explain under what circumstances the report length will include the byte reserved for the report ID.
Let's fix up the kerneldoc.
---
include/linux/hid.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
include/linux/hid.h +5 -1
@@ -1216,7 +1216,11 @@ static inline void hid_hw_wait(struct hi/** * hid_report_len - calculate the report length * -* @report: the report we want to know the length +* @report: the report whose length we want to know +* +* The length counts the report ID byte, but only if the ID is nonzero +* and therefore is included in the report. Reports whose ID is zero +* never include an ID byte. */ static inline u32 hid_report_len (struct hid_report *report) {
Re: (subset) [PATCH] HID: core: Improve the kerneldoc for hid_report_len()
Quoted 8 lines · Alan Stern
The kerneldoc for
hid_report_len()needs to be improved. The description of the @report argument is ungrammatical, and the documentation does not explain under what circumstances the report length will include the byte reserved for the report ID.Let's fix up the kerneldoc.
[...]
Applied to hid/hid.git (for-6.17/core), thanks!
[1/1] HID: core: Improve the kerneldoc for hid_report_len()
https://git.kernel.org/hid/hid/c/12f33ef6c2aa
Cheers,
Signature · 1 line
Benjamin Tissoires <[email protected]>