Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp22497pxb; Fri, 15 Jan 2021 04:57:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJx4Yrf1vdXl9GCBqYtl/+Q3bw9l15ZIAnpwpRNM6mX9EtNRN+h5VgAeG4FnBZgsxDlm5nYL X-Received: by 2002:a17:906:c087:: with SMTP id f7mr8794679ejz.492.1610715438815; Fri, 15 Jan 2021 04:57:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610715438; cv=none; d=google.com; s=arc-20160816; b=MPEcYLm15boB8ps7106khsOyGB2J0d6J/EiM2xSt4XThKfCJ8gPXLxVEbTzx8Emzbc whtE5TQpDOs2tMAww4rkPlE1wb2coY9A2hbkoU9nLw0jcdOgfd6WpAqBD9ekWCaRVjT5 WcV6YpienCi9lz8vU69KcDxK3RgZrSIIqKT/KzYHjQnb9LPHhk9sTJHjy9jtrdr/gro5 lGnGWwE4lydF0xJHIOJ28hOx7vTHWtk6mu1QfeuN6VWpsixxUkWs5fPOPo9XkQMbTer3 u/08LKBQRTVpYv3xeRlZqrdy9iRvh7PkdmRqVMXX7aWaQxWHQ5u0V5WW+Yp5bCcc94S/ ijbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=s2i7B/O8A9kvRC71FOaKqiGhDuH/qw8FK+yIQYuUdB4=; b=pYoY5rrFi7heUAxY5amPUCT46PVw4YIi9Suxu+zrvyF+fPTkEEJjF7VXnXrrs9Ar4t R38uXGJjSCb2xKfuqgJgnAq41FeI1Aw6W1CKhW77+VKyhE6LCXLMocaK8mn7azK3yn2v hNLPfdRNN8WgadLPi3sAz4DohS6GEDChvvt1zL+/xwVDQqL5Dj9Udelm+1EJZaGA9sVU 2WiV2NdTmH2yAQoURphJMWNHaJsuQwkQuS4TCuqV+J6e1QO7QuyYVqYVsBNFzuf05XFI rJEhURCgYxiNd/6k7t5e3NSPhGiePTIZZ9aTIAdGyLrrXfhonGho/iUEetC+IHH2UpRo /Drw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z7G3UPey; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t2si3693193ejd.302.2021.01.15.04.56.55; Fri, 15 Jan 2021 04:57:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Z7G3UPey; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387920AbhAOM4Y (ORCPT + 99 others); Fri, 15 Jan 2021 07:56:24 -0500 Received: from mail.kernel.org ([198.145.29.99]:41338 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387521AbhAOMep (ORCPT ); Fri, 15 Jan 2021 07:34:45 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7DE9C2256F; Fri, 15 Jan 2021 12:34:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1610714069; bh=Lr2E47ovMLNH5cjAfP0XE+8hZ6zMuDZnDmwq6ILvmos=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z7G3UPeyfpP4wK4hPa+/QBPzpRsIH/b5HJ4A7atmEjOF0f0mBgLuCzUiS4wru5xtI Z6lobYaEeAVuu8k9WN2VpXk2PtT7Q7gdpmka86cB9J2mDabmC/0x+yZryXkD3CMrry X2GCM/FaLCUQMEtqy/8wi7VFes6NGbQFym5lzy5o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+5b49c9695968d7250a26@syzkaller.appspotmail.com, Ping Cheng , Benjamin Tissoires , Jiri Kosina Subject: [PATCH 5.4 32/62] HID: wacom: Fix memory leakage caused by kfifo_alloc Date: Fri, 15 Jan 2021 13:27:54 +0100 Message-Id: <20210115121959.960347166@linuxfoundation.org> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210115121958.391610178@linuxfoundation.org> References: <20210115121958.391610178@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ping Cheng commit 37309f47e2f5674f3e86cb765312ace42cfcedf5 upstream. 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 Reviewed-by: Benjamin Tissoires Signed-off-by: Jiri Kosina Signed-off-by: Greg Kroah-Hartman --- drivers/hid/wacom_sys.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) --- a/drivers/hid/wacom_sys.c +++ b/drivers/hid/wacom_sys.c @@ -1270,6 +1270,37 @@ static int wacom_devm_sysfs_create_group 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 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_devi if (wacom->wacom_wac.features.type != REMOTE) wacom_release_resources(wacom); - - kfifo_free(&wacom_wac->pen_fifo); } #ifdef CONFIG_PM