Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp436080ybl; Tue, 28 Jan 2020 06:00:27 -0800 (PST) X-Google-Smtp-Source: APXvYqzxwxjiayI9iWtoo9/FICbQSICwcpdpBLK65/LV4/kC14/eDZtkmVAmmusCRlZT3GdxwTyc X-Received: by 2002:a9d:7999:: with SMTP id h25mr16491989otm.347.1580220027666; Tue, 28 Jan 2020 06:00:27 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580220027; cv=none; d=google.com; s=arc-20160816; b=BWmp81aO9oZ0QN7YdwJDZPUTsDGxmtCtXbuMU+IADaFzOltpMPLjupx4qSQUtO4IsI c+jdK4Rt8a+2p1UcH6YymIZBnUFqKEV6/phb9ZjH8dQGJXIIz5AMuksY6SDzb7D5f3+A om0OB6Bzev4vfRZWUleFxpY16DE8xH0FdeqWRpWwATo205lsKogbjWLpvM28gVvjsOba 4JdHKS8ke1KIY2ipyR6HyIJxB7EPp+x3PgjQrTzjjFg9kKG0vGXSpWdaFINjOO8wou8e Y7gWcvYom1uwKqeSmHVyua8k0yDfyrjgrwwGgeK6f8zm6HYlG6sZAmQPmokjRnqSOdug GK8g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=7f4uxwedXba0ZxzoXUk4V9TUBB/aEQYCDdOaILIFXdc=; b=x1cW2Tt5Rvp4ca9gmsoZm5UR5esJhx2OKc6nJk7IzzquYYxUE43vJz3bJa0q2soCt3 a64UFJ/9t55QrGTCCjCjhYjBPu1ZhfwPu4ST6wXt61ySD8HwggGzqjWpKY+chkvHLOwF sPL4bof4MGdqBL1L5c/95g8XgjhA3kpsMvVaPfL3ki/Neyj3cgjF3iUTRl61hqBdXuDb KJxJCrygSktxyOq1AG7hHARfPZNV1wyLiVo+/6EYD6IST7/JV0Nlg6EuuHJak/LE6sOL pv1qVJygCXaxO599+avIAFFgwX9Q2RTLyYyDi5nfpj5eqx3E4jOx4SslpXqW7PkPGGOc heEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Z0jcrvJD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h125si5324885oia.253.2020.01.28.06.00.15; Tue, 28 Jan 2020 06:00:27 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Z0jcrvJD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726715AbgA1N67 (ORCPT + 99 others); Tue, 28 Jan 2020 08:58:59 -0500 Received: from mail.kernel.org ([198.145.29.99]:44032 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726677AbgA1N6y (ORCPT ); Tue, 28 Jan 2020 08:58:54 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1C79A24690; Tue, 28 Jan 2020 13:58:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1580219933; bh=YOgb+jnj2nI7pkh3Op4SXmqD4q8bK9Eanoae+mmYw7A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Z0jcrvJDR32Olo5+Yr8lQICkG4YFoow7emiM0IzBPu1p1a9e21r0fgqqYzMwdmz46 dRGLBM7nVN4GvRtgcI/uK+O3LUGWB/yH96E2DK8Yz/Xo+bzCuT8jvCPClE7NNhe3WV VLkorFmytvScISS0rbTnAMmekNw8ExkaczRcoGzk= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dmitry Osipenko , Guenter Roeck Subject: [PATCH 4.14 19/46] hwmon: (core) Fix double-free in __hwmon_device_register() Date: Tue, 28 Jan 2020 14:57:53 +0100 Message-Id: <20200128135752.399729134@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200128135749.822297911@linuxfoundation.org> References: <20200128135749.822297911@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Dmitry Osipenko commit 74e3512731bd5c9673176425a76a7cc5efa8ddb6 upstream. Fix double-free that happens when thermal zone setup fails, see KASAN log below. ================================================================== BUG: KASAN: double-free or invalid-free in __hwmon_device_register+0x5dc/0xa7c CPU: 0 PID: 132 Comm: kworker/0:2 Tainted: G B 4.19.0-rc8-next-20181016-00042-gb52cd80401e9-dirty #41 Hardware name: NVIDIA Tegra SoC (Flattened Device Tree) Workqueue: events deferred_probe_work_func Backtrace: [] (dump_backtrace) from [] (show_stack+0x20/0x24) [] (show_stack) from [] (dump_stack+0x9c/0xb0) [] (dump_stack) from [] (print_address_description+0x68/0x250) [] (print_address_description) from [] (kasan_report_invalid_free+0x68/0x88) [] (kasan_report_invalid_free) from [] (__kasan_slab_free+0x1f4/0x200) [] (__kasan_slab_free) from [] (kasan_slab_free+0x14/0x18) [] (kasan_slab_free) from [] (kfree+0x90/0x294) [] (kfree) from [] (__hwmon_device_register+0x5dc/0xa7c) [] (__hwmon_device_register) from [] (hwmon_device_register_with_info+0xa0/0xa8) [] (hwmon_device_register_with_info) from [] (devm_hwmon_device_register_with_info+0x74/0xb4) [] (devm_hwmon_device_register_with_info) from [] (lm90_probe+0x414/0x578) [] (lm90_probe) from [] (i2c_device_probe+0x35c/0x384) [] (i2c_device_probe) from [] (really_probe+0x290/0x3e4) [] (really_probe) from [] (driver_probe_device+0x80/0x1c4) [] (driver_probe_device) from [] (__device_attach_driver+0x104/0x11c) [] (__device_attach_driver) from [] (bus_for_each_drv+0xa4/0xc8) [] (bus_for_each_drv) from [] (__device_attach+0xf0/0x15c) [] (__device_attach) from [] (device_initial_probe+0x1c/0x20) [] (device_initial_probe) from [] (bus_probe_device+0xdc/0xec) [] (bus_probe_device) from [] (deferred_probe_work_func+0xa8/0xd4) [] (deferred_probe_work_func) from [] (process_one_work+0x3dc/0x96c) [] (process_one_work) from [] (worker_thread+0x4ec/0x8bc) [] (worker_thread) from [] (kthread+0x230/0x240) [] (kthread) from [] (ret_from_fork+0x14/0x38) Exception stack(0xcf743fb0 to 0xcf743ff8) 3fa0: 00000000 00000000 00000000 00000000 3fc0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 3fe0: 00000000 00000000 00000000 00000000 00000013 00000000 Allocated by task 132: kasan_kmalloc.part.1+0x58/0xf4 kasan_kmalloc+0x90/0xa4 kmem_cache_alloc_trace+0x90/0x2a0 __hwmon_device_register+0xbc/0xa7c hwmon_device_register_with_info+0xa0/0xa8 devm_hwmon_device_register_with_info+0x74/0xb4 lm90_probe+0x414/0x578 i2c_device_probe+0x35c/0x384 really_probe+0x290/0x3e4 driver_probe_device+0x80/0x1c4 __device_attach_driver+0x104/0x11c bus_for_each_drv+0xa4/0xc8 __device_attach+0xf0/0x15c device_initial_probe+0x1c/0x20 bus_probe_device+0xdc/0xec deferred_probe_work_func+0xa8/0xd4 process_one_work+0x3dc/0x96c worker_thread+0x4ec/0x8bc kthread+0x230/0x240 ret_from_fork+0x14/0x38 (null) Freed by task 132: __kasan_slab_free+0x12c/0x200 kasan_slab_free+0x14/0x18 kfree+0x90/0x294 hwmon_dev_release+0x1c/0x20 device_release+0x4c/0xe8 kobject_put+0xac/0x11c device_unregister+0x2c/0x30 __hwmon_device_register+0xa58/0xa7c hwmon_device_register_with_info+0xa0/0xa8 devm_hwmon_device_register_with_info+0x74/0xb4 lm90_probe+0x414/0x578 i2c_device_probe+0x35c/0x384 really_probe+0x290/0x3e4 driver_probe_device+0x80/0x1c4 __device_attach_driver+0x104/0x11c bus_for_each_drv+0xa4/0xc8 __device_attach+0xf0/0x15c device_initial_probe+0x1c/0x20 bus_probe_device+0xdc/0xec deferred_probe_work_func+0xa8/0xd4 process_one_work+0x3dc/0x96c worker_thread+0x4ec/0x8bc kthread+0x230/0x240 ret_from_fork+0x14/0x38 (null) Cc: # v4.15+ Fixes: 47c332deb8e8 ("hwmon: Deal with errors from the thermal subsystem") Signed-off-by: Dmitry Osipenko Signed-off-by: Guenter Roeck Signed-off-by: Greg Kroah-Hartman --- drivers/hwmon/hwmon.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -631,8 +631,10 @@ __hwmon_device_register(struct device *d if (info[i]->config[j] & HWMON_T_INPUT) { err = hwmon_thermal_add_sensor(dev, hwdev, j); - if (err) - goto free_device; + if (err) { + device_unregister(hdev); + goto ida_remove; + } } } } @@ -640,8 +642,6 @@ __hwmon_device_register(struct device *d return hdev; -free_device: - device_unregister(hdev); free_hwmon: kfree(hwdev); ida_remove: