From: Ping Cheng <pinglinux@gmail.com>
To: Jiri Kosina <jkosina@suse.cz>
Cc: Benjamin Tissoires <benjamin.tissoires@gmail.com>,
linux-input <linux-input@vger.kernel.org>
Subject: Re: [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc
Date: Mon, 21 Dec 2020 19:22:04 -0800 [thread overview]
Message-ID: <CAF8JNh+yARDrw1ViUKcaK_zFrZJGWdTUmSJr09QQhiKY10z=xg@mail.gmail.com> (raw)
In-Reply-To: <20201210045230.26343-1-ping.cheng@wacom.com>
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
>
next prev parent reply other threads:[~2020-12-22 3:23 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-12-10 4:52 [PATCH] HID: wacom: Fix memory leakage caused by kfifo_alloc Ping Cheng
2020-12-22 3:22 ` Ping Cheng [this message]
2020-12-22 9:27 ` Jiri Kosina
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAF8JNh+yARDrw1ViUKcaK_zFrZJGWdTUmSJr09QQhiKY10z=xg@mail.gmail.com' \
--to=pinglinux@gmail.com \
--cc=benjamin.tissoires@gmail.com \
--cc=jkosina@suse.cz \
--cc=linux-input@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).