linux-input.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc
@ 2020-12-10  4:52 Ping Cheng
  2020-12-22  3:22 ` Ping Cheng
  2020-12-22  9:27 ` Jiri Kosina
  0 siblings, 2 replies; 3+ messages in thread
From: Ping Cheng @ 2020-12-10  4:52 UTC (permalink / raw)
  To: linux-input, syzbot+5b49c9695968d7250a26
  Cc: jkosina, benjamin.tissoires, Ping Cheng

As reported by syzbot below, kfifo_alloc'd memory would not be freed
if a non-zero return value is triggered in wacom_probe. This patch
creates and uses devm_kfifo_alloc to allocate and free itself.

BUG: memory leak
unreferenced object 0xffff88810dc44a00 (size 512):
  comm "kworker/1:2", pid 3674, jiffies 4294943617 (age 14.100s)
  hex dump (first 32 bytes):
   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
  backtrace:
   [<0000000023e1afac>] kmalloc_array include/linux/slab.h:592 [inline]
   [<0000000023e1afac>] __kfifo_alloc+0xad/0x100 lib/kfifo.c:43
   [<00000000c477f737>] wacom_probe+0x1a1/0x3b0 drivers/hid/wacom_sys.c:2727
   [<00000000b3109aca>] hid_device_probe+0x16b/0x210 drivers/hid/hid-core.c:2281
   [<00000000aff7c640>] really_probe+0x159/0x480 drivers/base/dd.c:554
   [<00000000778d0bc3>] driver_probe_device+0x84/0x100 drivers/base/dd.c:738
   [<000000005108dbb5>] __device_attach_driver+0xee/0x110 drivers/base/dd.c:844
   [<00000000efb7c59e>] bus_for_each_drv+0xb7/0x100 drivers/base/bus.c:431
   [<0000000024ab1590>] __device_attach+0x122/0x250 drivers/base/dd.c:912
   [<000000004c7ac048>] bus_probe_device+0xc6/0xe0 drivers/base/bus.c:491
   [<00000000b93050a3>] device_add+0x5ac/0xc30 drivers/base/core.c:2936
   [<00000000e5b46ea5>] hid_add_device+0x151/0x390 drivers/hid/hid-core.c:2437
   [<00000000c6add147>] usbhid_probe+0x412/0x560 drivers/hid/usbhid/hid-core.c:1407
   [<00000000c33acdb4>] usb_probe_interface+0x177/0x370 drivers/usb/core/driver.c:396
   [<00000000aff7c640>] really_probe+0x159/0x480 drivers/base/dd.c:554
   [<00000000778d0bc3>] driver_probe_device+0x84/0x100 drivers/base/dd.c:738
   [<000000005108dbb5>] __device_attach_driver+0xee/0x110 drivers/base/dd.c:844

https://syzkaller.appspot.com/bug?extid=5b49c9695968d7250a26

Reported-by: syzbot+5b49c9695968d7250a26@syzkaller.appspotmail.com
Signed-off-by: Ping Cheng <ping.cheng@wacom.com>
Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
---
 drivers/hid/wacom_sys.c | 35 ++++++++++++++++++++++++++++++++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c
index cd71e7133944..9e852b4bbf92 100644
--- a/drivers/hid/wacom_sys.c
+++ b/drivers/hid/wacom_sys.c
@@ -1270,6 +1270,37 @@ static int wacom_devm_sysfs_create_group(struct wacom *wacom,
 					       group);
 }
 
+static void wacom_devm_kfifo_release(struct device *dev, void *res)
+{
+	struct kfifo_rec_ptr_2 *devres = res;
+
+	kfifo_free(devres);
+}
+
+static int wacom_devm_kfifo_alloc(struct wacom *wacom)
+{
+	struct wacom_wac *wacom_wac = &wacom->wacom_wac;
+	struct kfifo_rec_ptr_2 *pen_fifo = &wacom_wac->pen_fifo;
+	int error;
+
+	pen_fifo = devres_alloc(wacom_devm_kfifo_release,
+			      sizeof(struct kfifo_rec_ptr_2),
+			      GFP_KERNEL);
+
+	if (!pen_fifo)
+		return -ENOMEM;
+
+	error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
+	if (error) {
+		devres_free(pen_fifo);
+		return error;
+	}
+
+	devres_add(&wacom->hdev->dev, pen_fifo);
+
+	return 0;
+}
+
 enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
 {
 	struct wacom *wacom = led->wacom;
@@ -2724,7 +2755,7 @@ static int wacom_probe(struct hid_device *hdev,
 	if (features->check_for_hid_type && features->hid_type != hdev->type)
 		return -ENODEV;
 
-	error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
+	error = wacom_devm_kfifo_alloc(wacom);
 	if (error)
 		return error;
 
@@ -2786,8 +2817,6 @@ static void wacom_remove(struct hid_device *hdev)
 
 	if (wacom->wacom_wac.features.type != REMOTE)
 		wacom_release_resources(wacom);
-
-	kfifo_free(&wacom_wac->pen_fifo);
 }
 
 #ifdef CONFIG_PM
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc
  2020-12-10  4:52 [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc Ping Cheng
@ 2020-12-22  3:22 ` Ping Cheng
  2020-12-22  9:27 ` Jiri Kosina
  1 sibling, 0 replies; 3+ messages in thread
From: Ping Cheng @ 2020-12-22  3:22 UTC (permalink / raw)
  To: Jiri Kosina; +Cc: Benjamin Tissoires, linux-input

Hi @Jiri Kosina,

Does this patch work for you? Is there anything else you'd like me to
change for the patch?

Thank you,
Ping

On Wed, Dec 9, 2020 at 8:52 PM Ping Cheng <pinglinux@gmail.com> wrote:
>
> As reported by syzbot below, kfifo_alloc'd memory would not be freed
> if a non-zero return value is triggered in wacom_probe. This patch
> creates and uses devm_kfifo_alloc to allocate and free itself.
>
> BUG: memory leak
> unreferenced object 0xffff88810dc44a00 (size 512):
>   comm "kworker/1:2", pid 3674, jiffies 4294943617 (age 14.100s)
>   hex dump (first 32 bytes):
>    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>   backtrace:
>    [<0000000023e1afac>] kmalloc_array include/linux/slab.h:592 [inline]
>    [<0000000023e1afac>] __kfifo_alloc+0xad/0x100 lib/kfifo.c:43
>    [<00000000c477f737>] wacom_probe+0x1a1/0x3b0 drivers/hid/wacom_sys.c:2727
>    [<00000000b3109aca>] hid_device_probe+0x16b/0x210 drivers/hid/hid-core.c:2281
>    [<00000000aff7c640>] really_probe+0x159/0x480 drivers/base/dd.c:554
>    [<00000000778d0bc3>] driver_probe_device+0x84/0x100 drivers/base/dd.c:738
>    [<000000005108dbb5>] __device_attach_driver+0xee/0x110 drivers/base/dd.c:844
>    [<00000000efb7c59e>] bus_for_each_drv+0xb7/0x100 drivers/base/bus.c:431
>    [<0000000024ab1590>] __device_attach+0x122/0x250 drivers/base/dd.c:912
>    [<000000004c7ac048>] bus_probe_device+0xc6/0xe0 drivers/base/bus.c:491
>    [<00000000b93050a3>] device_add+0x5ac/0xc30 drivers/base/core.c:2936
>    [<00000000e5b46ea5>] hid_add_device+0x151/0x390 drivers/hid/hid-core.c:2437
>    [<00000000c6add147>] usbhid_probe+0x412/0x560 drivers/hid/usbhid/hid-core.c:1407
>    [<00000000c33acdb4>] usb_probe_interface+0x177/0x370 drivers/usb/core/driver.c:396
>    [<00000000aff7c640>] really_probe+0x159/0x480 drivers/base/dd.c:554
>    [<00000000778d0bc3>] driver_probe_device+0x84/0x100 drivers/base/dd.c:738
>    [<000000005108dbb5>] __device_attach_driver+0xee/0x110 drivers/base/dd.c:844
>
> https://syzkaller.appspot.com/bug?extid=5b49c9695968d7250a26
>
> Reported-by: syzbot+5b49c9695968d7250a26@syzkaller.appspotmail.com
> Signed-off-by: Ping Cheng <ping.cheng@wacom.com>
> Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>
> ---
>  drivers/hid/wacom_sys.c | 35 ++++++++++++++++++++++++++++++++---
>  1 file changed, 32 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/hid/wacom_sys.c b/drivers/hid/wacom_sys.c
> index cd71e7133944..9e852b4bbf92 100644
> --- a/drivers/hid/wacom_sys.c
> +++ b/drivers/hid/wacom_sys.c
> @@ -1270,6 +1270,37 @@ static int wacom_devm_sysfs_create_group(struct wacom *wacom,
>                                                group);
>  }
>
> +static void wacom_devm_kfifo_release(struct device *dev, void *res)
> +{
> +       struct kfifo_rec_ptr_2 *devres = res;
> +
> +       kfifo_free(devres);
> +}
> +
> +static int wacom_devm_kfifo_alloc(struct wacom *wacom)
> +{
> +       struct wacom_wac *wacom_wac = &wacom->wacom_wac;
> +       struct kfifo_rec_ptr_2 *pen_fifo = &wacom_wac->pen_fifo;
> +       int error;
> +
> +       pen_fifo = devres_alloc(wacom_devm_kfifo_release,
> +                             sizeof(struct kfifo_rec_ptr_2),
> +                             GFP_KERNEL);
> +
> +       if (!pen_fifo)
> +               return -ENOMEM;
> +
> +       error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
> +       if (error) {
> +               devres_free(pen_fifo);
> +               return error;
> +       }
> +
> +       devres_add(&wacom->hdev->dev, pen_fifo);
> +
> +       return 0;
> +}
> +
>  enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
>  {
>         struct wacom *wacom = led->wacom;
> @@ -2724,7 +2755,7 @@ static int wacom_probe(struct hid_device *hdev,
>         if (features->check_for_hid_type && features->hid_type != hdev->type)
>                 return -ENODEV;
>
> -       error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
> +       error = wacom_devm_kfifo_alloc(wacom);
>         if (error)
>                 return error;
>
> @@ -2786,8 +2817,6 @@ static void wacom_remove(struct hid_device *hdev)
>
>         if (wacom->wacom_wac.features.type != REMOTE)
>                 wacom_release_resources(wacom);
> -
> -       kfifo_free(&wacom_wac->pen_fifo);
>  }
>
>  #ifdef CONFIG_PM
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc
  2020-12-10  4:52 [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc Ping Cheng
  2020-12-22  3:22 ` Ping Cheng
@ 2020-12-22  9:27 ` Jiri Kosina
  1 sibling, 0 replies; 3+ messages in thread
From: Jiri Kosina @ 2020-12-22  9:27 UTC (permalink / raw)
  To: Ping Cheng
  Cc: linux-input, syzbot+5b49c9695968d7250a26, benjamin.tissoires, Ping Cheng

On Wed, 9 Dec 2020, Ping Cheng wrote:

> As reported by syzbot below, kfifo_alloc'd memory would not be freed
> if a non-zero return value is triggered in wacom_probe. This patch
> creates and uses devm_kfifo_alloc to allocate and free itself.
> 
> BUG: memory leak
> unreferenced object 0xffff88810dc44a00 (size 512):
>   comm "kworker/1:2", pid 3674, jiffies 4294943617 (age 14.100s)
>   hex dump (first 32 bytes):
>    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
>   backtrace:
>    [<0000000023e1afac>] kmalloc_array include/linux/slab.h:592 [inline]
>    [<0000000023e1afac>] __kfifo_alloc+0xad/0x100 lib/kfifo.c:43
>    [<00000000c477f737>] wacom_probe+0x1a1/0x3b0 drivers/hid/wacom_sys.c:2727
>    [<00000000b3109aca>] hid_device_probe+0x16b/0x210 drivers/hid/hid-core.c:2281
>    [<00000000aff7c640>] really_probe+0x159/0x480 drivers/base/dd.c:554
>    [<00000000778d0bc3>] driver_probe_device+0x84/0x100 drivers/base/dd.c:738
>    [<000000005108dbb5>] __device_attach_driver+0xee/0x110 drivers/base/dd.c:844
>    [<00000000efb7c59e>] bus_for_each_drv+0xb7/0x100 drivers/base/bus.c:431
>    [<0000000024ab1590>] __device_attach+0x122/0x250 drivers/base/dd.c:912
>    [<000000004c7ac048>] bus_probe_device+0xc6/0xe0 drivers/base/bus.c:491
>    [<00000000b93050a3>] device_add+0x5ac/0xc30 drivers/base/core.c:2936
>    [<00000000e5b46ea5>] hid_add_device+0x151/0x390 drivers/hid/hid-core.c:2437
>    [<00000000c6add147>] usbhid_probe+0x412/0x560 drivers/hid/usbhid/hid-core.c:1407
>    [<00000000c33acdb4>] usb_probe_interface+0x177/0x370 drivers/usb/core/driver.c:396
>    [<00000000aff7c640>] really_probe+0x159/0x480 drivers/base/dd.c:554
>    [<00000000778d0bc3>] driver_probe_device+0x84/0x100 drivers/base/dd.c:738
>    [<000000005108dbb5>] __device_attach_driver+0xee/0x110 drivers/base/dd.c:844
> 
> https://syzkaller.appspot.com/bug?extid=5b49c9695968d7250a26
> 
> Reported-by: syzbot+5b49c9695968d7250a26@syzkaller.appspotmail.com
> Signed-off-by: Ping Cheng <ping.cheng@wacom.com>
> Reviewed-by: Benjamin Tissoires <benjamin.tissoires@redhat.com>

Now queued in for-5.11/upstream-fixes, sorry for the delay. Thanks,


-- 
Jiri Kosina
SUSE Labs


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-22  9:28 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-10  4:52 [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc Ping Cheng
2020-12-22  3:22 ` Ping Cheng
2020-12-22  9:27 ` Jiri Kosina

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).