Skip to contentSkip to conversation

hid: fix missing hid_is_usb() checks in three drivers

PATCH Superseded Jann Horn 2 participants 6 messages Jul 3

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

cover hid: fix missing hid_is_usb() checks in three drivers

This fixes missing hid_is_usb() checks before to_usb_interface() in three HID drivers.
I've split it into three patches so that they can have separate "Fixes" tags, hopefully they are easier to stable-backport this way.

---
Jann Horn (3):
HID: asus: fix missing hid_is_usb() check
HID: huawei: fix missing hid_is_usb() check
HID: rapoo: fix missing hid_is_usb() check

 drivers/hid/hid-asus.c   | 2 +-
 drivers/hid/hid-huawei.c | 5 +++--
 drivers/hid/hid-rapoo.c  | 2 +-
 3 files changed, 5 insertions(+), 4 deletions(-)

---
base-commit: 51512e22efe813d8223de27f6fd02a8a48ea2323
change-id: 20260703-hid-usbcheck-9163e6cf6015

Best regards,

Signature · 1 line
Jann Horn <[email protected]>
author
#1 lore raw

PATCH 1/3 HID: asus: fix missing hid_is_usb() check

to_usb_interface() can only be used on a hid_device whose parent is really USB; uhid can create devices that identify as being on BUS_USB, but don't actually have a USB parent.
Fix the use of to_usb_interface() without a hid_is_usb() check.

I have verified that it is currently possible to trigger a kernel splat due to this bug in an ASAN build, and that this commit fixes the issue.

Fixes 00e005c952f7 ("hid-asus: check ROG Ally MCU version and warn")
---
 drivers/hid/hid-asus.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/hid-asus.c +1 -1
index 3f5e96900b67..befa990b3210 100644
@@ -753,7 +753,7 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
 			return ret;
 	}
 
-	if (drvdata->quirks & QUIRK_ROG_ALLY_XPAD) {
+	if ((drvdata->quirks & QUIRK_ROG_ALLY_XPAD) && hid_is_usb(hdev)) {
 		intf = to_usb_interface(hdev->dev.parent);
 		udev = interface_to_usbdev(intf);
 		validate_mcu_fw_version(hdev,
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#2 lore raw

Re: [PATCH 1/3] HID: asus: fix missing hid_is_usb() check

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing pointer invalidation after devm_kfree causes a deterministic Use-After-Free on the failure path. --

commit d3bce769a13470fb7e8717746a98294d5eb006c9
Author: Jann Horn <[email protected]>

HID: asus: fix missing hid_is_usb() check

to_usb_interface() can only be used on a hid_device whose parent is really USB; uhid can create devices that identify as being on BUS_USB, but don't actually have a USB parent. Fix the use of to_usb_interface() without a hid_is_usb() check.

drivers/hid/hid-asus.c static int asus_kbd_register_leds(struct hid_device *hdev) · line 759

Quoted 13 lines
drivers/hid/hid-asus.c +1 -1
index 3f5e96900b67a..befa990b3210e 100644
@@ -753,7 +753,7 @@ static int asus_kbd_register_leds(struct hid_device *hdev)
 			return ret;
 	}
 
-	if (drvdata->quirks & QUIRK_ROG_ALLY_XPAD) {
+	if ((drvdata->quirks & QUIRK_ROG_ALLY_XPAD) && hid_is_usb(hdev)) {
 		intf = to_usb_interface(hdev->dev.parent);
 		udev = interface_to_usbdev(intf);
 		validate_mcu_fw_version(hdev,

[Severity: High]
This is a pre-existing issue, but does the failure path in this function leave a dangling pointer that can cause a use-after-free?

If asus_hid_register_listener() fails later in asus_kbd_register_leds():

	ret = asus_hid_register_listener(&drvdata->kbd_backlight->listener);
	if (ret < 0) {
		/* No need to have this still around */
		devm_kfree(&hdev->dev, drvdata->kbd_backlight);
	}

The memory allocated for drvdata->kbd_backlight is freed, but the pointer is not set to NULL.

Then asus_probe() catches the failure, emits a warning, but improperly allows probe to succeed:

	if (is_vendor && (drvdata->quirks & QUIRK_USE_KBD_BACKLIGHT) &&
	    (asus_has_report_id(hdev, FEATURE_KBD_REPORT_ID)) &&
		(asus_kbd_register_leds(hdev)))
		hid_warn(hdev, "Failed to initialize backlight.\n");

If this occurs, could a subsequent call to asus_remove() or asus_resume() dereference the freed memory?

	if (drvdata->kbd_backlight) {
		asus_hid_unregister_listener(&drvdata->kbd_backlight->listener);
		spin_lock_irqsave(&drvdata->kbd_backlight->lock, flags);
		drvdata->kbd_backlight->removed = true;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH 2/3 HID: huawei: fix missing hid_is_usb() check

In reply to Jann Horn

to_usb_interface() can only be used on a hid_device whose parent is really USB; uhid can create devices that identify as being on BUS_USB, but don't actually have a USB parent.
Fix the use of to_usb_interface() without a hid_is_usb() check.

I have verified that it is currently possible to trigger a kernel splat due to this bug in an ASAN build, and that this commit fixes the issue.

Fixes e93faaca84b7 ("HID: huawei: fix CD30 keyboard report descriptor issue")
---
 drivers/hid/hid-huawei.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/hid/hid-huawei.c +3 -2
index 6a616bf21b38..ee3fc6f68475 100644
@@ -44,11 +44,12 @@ static const __u8 huawei_cd30_kbd_rdesc_fixed[] = {
 static const __u8 *huawei_report_fixup(struct hid_device *hdev, __u8 *rdesc,
 				  unsigned int *rsize)
 {
-	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
+	struct usb_interface *intf = hid_is_usb(hdev) ?
+			to_usb_interface(hdev->dev.parent) : NULL;
 
 	switch (hdev->product) {
 	case USB_DEVICE_ID_HUAWEI_CD30KBD:
-		if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
+		if (!intf || intf->cur_altsetting->desc.bInterfaceNumber == 1) {
 			if (*rsize != sizeof(huawei_cd30_kbd_rdesc_fixed) ||
 				memcmp(huawei_cd30_kbd_rdesc_fixed, rdesc,
 					sizeof(huawei_cd30_kbd_rdesc_fixed)) != 0) {
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
author
#4 lore raw

PATCH 3/3 HID: rapoo: fix missing hid_is_usb() check

In reply to Jann Horn

to_usb_interface() can only be used on a hid_device whose parent is really USB; uhid can create devices that identify as being on BUS_USB, but don't actually have a USB parent.
Fix the use of to_usb_interface() without a hid_is_usb() check.

I have verified that it is currently possible to trigger a kernel splat due to this bug in an ASAN build, and that this commit fixes the issue.

Fixes 00e005c952f7 ("hid-asus: check ROG Ally MCU version and warn")Fixes: b3b1c68fb726 ("HID: rapoo: Add support for side buttons on RAPOO 0x2015 mouse")
---
 drivers/hid/hid-rapoo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/hid/hid-rapoo.c +1 -1
index 4c81f3086de4..5c9c396fabf7 100644
@@ -36,7 +36,7 @@ static int rapoo_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		return ret;
 	}
 
-	if (hdev->bus == BUS_USB) {
+	if (hid_is_usb(hdev)) {
 		struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
 
 		if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
Signature · 1 line
2.55.0.rc0.799.gd6f94ed593-goog
#5 lore raw

Re: [PATCH 3/3] HID: rapoo: fix missing hid_is_usb() check

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Low] Missing Kconfig dependency `depends on USB_HID` for `CONFIG_HID_RAPOO` causes a linker error.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Low] Missing Kconfig dependency depends on USB_HID for CONFIG_HID_RAPOO causes a linker error.
- [Low] Malformed Fixes: tags in the commit message.
--

commit ac5c5d7fc93db76e1ff4b587da9d47600a05ff83
Author: Jann Horn <[email protected]>
HID: rapoo: fix missing hid_is_usb() check

This commit fixes a potential splat by verifying a HID device is actually on a USB bus before casting its parent to a usb_interface.

Quoted 4 lines

I have verified that it is currently possible to trigger a kernel splat due to this bug in an ASAN build, and that this commit fixes the issue.

Fixes 00e005c952f7 ("hid-asus: check ROG Ally MCU version and warn")Fixes: b3b1c68fb726 ("HID: rapoo: Add support for side buttons on RAPOO 0x2015 mouse")

[Severity: Low]
This isn't a bug, but there are two Fixes tags concatenated on the same line.

Could this break automated patch parsing tools and stable backporting scripts that expect each tag to be on a separate line?

Quoted 7 lines
drivers/hid/hid-rapoo.c +0 -0
index 4c81f3086de43..5c9c396fabf77 100644

[ ... ]

drivers/hid/hid-rapoo.c static int rapoo_probe(struct hid_device *hdev, const struct hid_device_id *id) · line 39

Quoted 6 lines
drivers/hid/hid-rapoo.c +1 -1
@@ -36,7 +36,7 @@ static int rapoo_probe(struct hid_device *hdev, const struct hid_device_id *id)
 		return ret;
 	}
 
-	if (hdev->bus == BUS_USB) {
+	if (hid_is_usb(hdev)) {

[Severity: Low]
Does the introduction of hid_is_usb() in rapoo_probe() require an update to the CONFIG_HID_RAPOO entry in drivers/hid/Kconfig?

Because hid_is_usb() is defined and exported exclusively by the usbhid module, if a user configures the kernel with CONFIG_HID_RAPOO=y and CONFIG_USB_HID=n (or m), will this cause a build failure during the linking stage due to an undefined reference?

Should CONFIG_HID_RAPOO now explicitly depend on USB_HID?

Quoted 3 lines
drivers/hid/hid-rapoo.c +0 -0
 		struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
 
 		if (intf->cur_altsetting->desc.bInterfaceNumber != 1)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3