Skip to content

Commit 89f3edc

Browse files
committed
HID: check for valid USB device for many HID drivers
commit 9302095 upstream. Many HID drivers assume that the HID device assigned to them is a USB device as that was the only way HID devices used to be able to be created in Linux. However, with the additional ways that HID devices can be created for many different bus types, that is no longer true, so properly check that we have a USB device associated with the HID device before allowing a driver that makes this assumption to claim it. Cc: Jiri Kosina <jikos@kernel.org> Cc: Benjamin Tissoires <benjamin.tissoires@redhat.com> Cc: Michael Zaidman <michael.zaidman@gmail.com> Cc: Stefan Achatz <erazor_de@users.sourceforge.net> Cc: Maxime Coquelin <mcoquelin.stm32@gmail.com> Cc: Alexandre Torgue <alexandre.torgue@foss.st.com> Cc: linux-input@vger.kernel.org Cc: stable@vger.kernel.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Tested-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> [bentiss: amended for thrustmater.c hunk to apply] Signed-off-by: Benjamin Tissoires <benjamin.tissoires@redhat.com> Link: https://lore.kernel.org/r/20211201183503.2373082-3-gregkh@linuxfoundation.org Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 889c391 commit 89f3edc

20 files changed

Lines changed: 84 additions & 10 deletions

‎drivers/hid/hid-chicony.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,12 @@ static int ch_input_mapping(struct hid_device *hdev, struct hid_input *hi,
5858
static __u8 *ch_switch12_report_fixup(struct hid_device *hdev, __u8 *rdesc,
5959
unsigned int *rsize)
6060
{
61-
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
62-
61+
struct usb_interface *intf;
62+
63+
if (!hid_is_usb(hdev))
64+
return rdesc;
65+
66+
intf = to_usb_interface(hdev->dev.parent);
6367
if (intf->cur_altsetting->desc.bInterfaceNumber == 1) {
6468
/* Change usage maximum and logical maximum from 0x7fff to
6569
* 0x2fff, so they don't exceed HID_MAX_USAGES */

‎drivers/hid/hid-corsair.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,12 @@ static int corsair_probe(struct hid_device *dev, const struct hid_device_id *id)
553553
int ret;
554554
unsigned long quirks = id->driver_data;
555555
struct corsair_drvdata *drvdata;
556-
struct usb_interface *usbif = to_usb_interface(dev->dev.parent);
556+
struct usb_interface *usbif;
557+
558+
if (!hid_is_usb(dev))
559+
return -EINVAL;
560+
561+
usbif = to_usb_interface(dev->dev.parent);
557562

558563
drvdata = devm_kzalloc(&dev->dev, sizeof(struct corsair_drvdata),
559564
GFP_KERNEL);

‎drivers/hid/hid-elan.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct elan_drvdata {
5050

5151
static int is_not_elan_touchpad(struct hid_device *hdev)
5252
{
53-
if (hdev->bus == BUS_USB) {
53+
if (hid_is_usb(hdev)) {
5454
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
5555

5656
return (intf->altsetting->desc.bInterfaceNumber !=

‎drivers/hid/hid-elo.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ static int elo_probe(struct hid_device *hdev, const struct hid_device_id *id)
229229
struct elo_priv *priv;
230230
int ret;
231231

232+
if (!hid_is_usb(hdev))
233+
return -EINVAL;
234+
232235
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
233236
if (!priv)
234237
return -ENOMEM;

‎drivers/hid/hid-holtek-kbd.c‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,17 @@ static int holtek_kbd_input_event(struct input_dev *dev, unsigned int type,
140140
static int holtek_kbd_probe(struct hid_device *hdev,
141141
const struct hid_device_id *id)
142142
{
143-
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
144-
int ret = hid_parse(hdev);
143+
struct usb_interface *intf;
144+
int ret;
145+
146+
if (!hid_is_usb(hdev))
147+
return -EINVAL;
145148

149+
ret = hid_parse(hdev);
146150
if (!ret)
147151
ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
148152

153+
intf = to_usb_interface(hdev->dev.parent);
149154
if (!ret && intf->cur_altsetting->desc.bInterfaceNumber == 1) {
150155
struct hid_input *hidinput;
151156
list_for_each_entry(hidinput, &hdev->inputs, list) {

‎drivers/hid/hid-holtek-mouse.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ static __u8 *holtek_mouse_report_fixup(struct hid_device *hdev, __u8 *rdesc,
6262
return rdesc;
6363
}
6464

65+
static int holtek_mouse_probe(struct hid_device *hdev,
66+
const struct hid_device_id *id)
67+
{
68+
if (!hid_is_usb(hdev))
69+
return -EINVAL;
70+
return 0;
71+
}
72+
6573
static const struct hid_device_id holtek_mouse_devices[] = {
6674
{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK_ALT,
6775
USB_DEVICE_ID_HOLTEK_ALT_MOUSE_A067) },
@@ -83,6 +91,7 @@ static struct hid_driver holtek_mouse_driver = {
8391
.name = "holtek_mouse",
8492
.id_table = holtek_mouse_devices,
8593
.report_fixup = holtek_mouse_report_fixup,
94+
.probe = holtek_mouse_probe,
8695
};
8796

8897
module_hid_driver(holtek_mouse_driver);

‎drivers/hid/hid-lg.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,12 +769,18 @@ static int lg_raw_event(struct hid_device *hdev, struct hid_report *report,
769769

770770
static int lg_probe(struct hid_device *hdev, const struct hid_device_id *id)
771771
{
772-
struct usb_interface *iface = to_usb_interface(hdev->dev.parent);
773-
__u8 iface_num = iface->cur_altsetting->desc.bInterfaceNumber;
772+
struct usb_interface *iface;
773+
__u8 iface_num;
774774
unsigned int connect_mask = HID_CONNECT_DEFAULT;
775775
struct lg_drv_data *drv_data;
776776
int ret;
777777

778+
if (!hid_is_usb(hdev))
779+
return -EINVAL;
780+
781+
iface = to_usb_interface(hdev->dev.parent);
782+
iface_num = iface->cur_altsetting->desc.bInterfaceNumber;
783+
778784
/* G29 only work with the 1st interface */
779785
if ((hdev->product == USB_DEVICE_ID_LOGITECH_G29_WHEEL) &&
780786
(iface_num != 0)) {

‎drivers/hid/hid-prodikeys.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,12 +798,18 @@ static int pk_raw_event(struct hid_device *hdev, struct hid_report *report,
798798
static int pk_probe(struct hid_device *hdev, const struct hid_device_id *id)
799799
{
800800
int ret;
801-
struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
802-
unsigned short ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
801+
struct usb_interface *intf;
802+
unsigned short ifnum;
803803
unsigned long quirks = id->driver_data;
804804
struct pk_device *pk;
805805
struct pcmidi_snd *pm = NULL;
806806

807+
if (!hid_is_usb(hdev))
808+
return -EINVAL;
809+
810+
intf = to_usb_interface(hdev->dev.parent);
811+
ifnum = intf->cur_altsetting->desc.bInterfaceNumber;
812+
807813
pk = kzalloc(sizeof(*pk), GFP_KERNEL);
808814
if (pk == NULL) {
809815
hid_err(hdev, "can't alloc descriptor\n");

‎drivers/hid/hid-roccat-arvo.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ static int arvo_probe(struct hid_device *hdev,
344344
{
345345
int retval;
346346

347+
if (!hid_is_usb(hdev))
348+
return -EINVAL;
349+
347350
retval = hid_parse(hdev);
348351
if (retval) {
349352
hid_err(hdev, "parse failed\n");

‎drivers/hid/hid-roccat-isku.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ static int isku_probe(struct hid_device *hdev,
324324
{
325325
int retval;
326326

327+
if (!hid_is_usb(hdev))
328+
return -EINVAL;
329+
327330
retval = hid_parse(hdev);
328331
if (retval) {
329332
hid_err(hdev, "parse failed\n");

0 commit comments

Comments
 (0)