Skip to content

Commit 37309f4

Browse files
PinglinuxJiri Kosina
authored and
Jiri Kosina
committed
HID: wacom: Fix memory leakage caused by kfifo_alloc
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: [email protected] Signed-off-by: Ping Cheng <[email protected]> Reviewed-by: Benjamin Tissoires <[email protected]> Signed-off-by: Jiri Kosina <[email protected]>
1 parent de925e2 commit 37309f4

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

drivers/hid/wacom_sys.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,37 @@ static int wacom_devm_sysfs_create_group(struct wacom *wacom,
12701270
group);
12711271
}
12721272

1273+
static void wacom_devm_kfifo_release(struct device *dev, void *res)
1274+
{
1275+
struct kfifo_rec_ptr_2 *devres = res;
1276+
1277+
kfifo_free(devres);
1278+
}
1279+
1280+
static int wacom_devm_kfifo_alloc(struct wacom *wacom)
1281+
{
1282+
struct wacom_wac *wacom_wac = &wacom->wacom_wac;
1283+
struct kfifo_rec_ptr_2 *pen_fifo = &wacom_wac->pen_fifo;
1284+
int error;
1285+
1286+
pen_fifo = devres_alloc(wacom_devm_kfifo_release,
1287+
sizeof(struct kfifo_rec_ptr_2),
1288+
GFP_KERNEL);
1289+
1290+
if (!pen_fifo)
1291+
return -ENOMEM;
1292+
1293+
error = kfifo_alloc(pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
1294+
if (error) {
1295+
devres_free(pen_fifo);
1296+
return error;
1297+
}
1298+
1299+
devres_add(&wacom->hdev->dev, pen_fifo);
1300+
1301+
return 0;
1302+
}
1303+
12731304
enum led_brightness wacom_leds_brightness_get(struct wacom_led *led)
12741305
{
12751306
struct wacom *wacom = led->wacom;
@@ -2724,7 +2755,7 @@ static int wacom_probe(struct hid_device *hdev,
27242755
if (features->check_for_hid_type && features->hid_type != hdev->type)
27252756
return -ENODEV;
27262757

2727-
error = kfifo_alloc(&wacom_wac->pen_fifo, WACOM_PKGLEN_MAX, GFP_KERNEL);
2758+
error = wacom_devm_kfifo_alloc(wacom);
27282759
if (error)
27292760
return error;
27302761

@@ -2786,8 +2817,6 @@ static void wacom_remove(struct hid_device *hdev)
27862817

27872818
if (wacom->wacom_wac.features.type != REMOTE)
27882819
wacom_release_resources(wacom);
2789-
2790-
kfifo_free(&wacom_wac->pen_fifo);
27912820
}
27922821

27932822
#ifdef CONFIG_PM

0 commit comments

Comments
 (0)