HID: usbhid: free raw_report buffers in usbhid_stop
authorAnirudh Rayabharam <mail@anirudhrb.com>
Wed, 23 Jun 2021 18:40:29 +0000 (00:10 +0530)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 6 Oct 2021 08:23:43 +0000 (10:23 +0200)
commit f7744fa16b96da57187dc8e5634152d3b63d72de upstream.

Free the unsent raw_report buffers when the device is removed.

Fixes a memory leak reported by syzbot at:
https://syzkaller.appspot.com/bug?id=7b4fa7cb1a7c2d3342a2a8a6c53371c8c418ab47

Reported-by: syzbot+47b26cd837ececfc666d@syzkaller.appspotmail.com
Tested-by: syzbot+47b26cd837ececfc666d@syzkaller.appspotmail.com
Signed-off-by: Anirudh Rayabharam <mail@anirudhrb.com>
Signed-off-by: Jiri Kosina <jkosina@suse.cz>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/hid/usbhid/hid-core.c

index b6600329a272d50aa569704e84fe5fc2045649f2..368f849decd1ff9b53e66b2b8fbf9469a7f3e8ba 100644 (file)
@@ -500,7 +500,7 @@ static void hid_ctrl(struct urb *urb)
 
        if (unplug) {
                usbhid->ctrltail = usbhid->ctrlhead;
-       } else {
+       } else if (usbhid->ctrlhead != usbhid->ctrltail) {
                usbhid->ctrltail = (usbhid->ctrltail + 1) & (HID_CONTROL_FIFO_SIZE - 1);
 
                if (usbhid->ctrlhead != usbhid->ctrltail &&
@@ -1185,9 +1185,20 @@ static void usbhid_stop(struct hid_device *hid)
                usbhid->intf->needs_remote_wakeup = 0;
 
        clear_bit(HID_STARTED, &usbhid->iofl);
+
        spin_lock_irq(&usbhid->lock);   /* Sync with error and led handlers */
        set_bit(HID_DISCONNECTED, &usbhid->iofl);
+       while (usbhid->ctrltail != usbhid->ctrlhead) {
+               if (usbhid->ctrl[usbhid->ctrltail].dir == USB_DIR_OUT) {
+                       kfree(usbhid->ctrl[usbhid->ctrltail].raw_report);
+                       usbhid->ctrl[usbhid->ctrltail].raw_report = NULL;
+               }
+
+               usbhid->ctrltail = (usbhid->ctrltail + 1) &
+                       (HID_CONTROL_FIFO_SIZE - 1);
+       }
        spin_unlock_irq(&usbhid->lock);
+
        usb_kill_urb(usbhid->urbin);
        usb_kill_urb(usbhid->urbout);
        usb_kill_urb(usbhid->urbctrl);