Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp217613ybi; Wed, 29 May 2019 19:59:12 -0700 (PDT) X-Google-Smtp-Source: APXvYqyDS6FS40JvRkbxzq+AFosi1DoR0bDDc0WnaqKfXXIsKiNuL1jVv/yjKPhJJIjhCa2RXh6l X-Received: by 2002:a05:6a00:cc:: with SMTP id e12mr1293164pfj.207.1559185152693; Wed, 29 May 2019 19:59:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559185152; cv=none; d=google.com; s=arc-20160816; b=VSdWInC19vXr4axec69lzhehwN5owGjZTZatGq74IOZXin2XjaUK8q28udOTZ1FNVK SWZ86etgnGt98teyO6MzeTlEEJVxJrlbaOt8swShUvkiMGgIfLJdXiQlP6JwzeHpDwzK ga45nfv97ugY4FaeHSnt7XlAphWjz29FH+JdxPtRTd4BEM+t5PCi4FqJdY5yLBC0kZS1 WT04mb4qozWn+oi+1oGD1eeYaSf0OyUaMIolvIm2AYds+5IxMdajFrMUbdG+5xBNp7oc BTfXkPzU+a44dshXSi8QXpmQ6VW7aoVXw8Bo7RTLXQCPScF2L0prHCH6NUjzAlbOpEfE zj7Q== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=qDsL18aDwQTcILov+CgblUA1JhXa5nFHLDzuuC/mnGA=; b=mzNwkoTniaejCDeMzPFQ/j1PKM04j1Qv/5U/wwUdcWqIwLMYX6/TMWX6w8UKnfs/H5 MFe0DJcaoCk1fE03JDzMYyBYSBIOZpHuAkT2pygYSqrmQsZ/nb9dJy6SymRS9bUjoCwm RfqdcphwSXl0B3JOc7b+Vz5Tu7rc5hqNj1Nt6BiJWRkG9Xf72IUiB4qeEAMfQxWeclJl NhDbLRBHxamllt4EFaeS00SVxWtoRzOEdelQmipKbCnfQ1eE3gjA6lyAruOgvvlL2J6S yDC3rMpeUefQY2Ik/lWSr4JbzY7b5gHz1zhwCidMSWeDtZYHIeYkJ5cwrERwqy83ap60 cRBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=MjLytlaq; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 34si1797671plm.315.2019.05.29.19.58.57; Wed, 29 May 2019 19:59:12 -0700 (PDT) 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=@amazon.com header.s=amazon201209 header.b=MjLytlaq; 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; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727448AbfE3C4P (ORCPT + 99 others); Wed, 29 May 2019 22:56:15 -0400 Received: from smtp-fw-9101.amazon.com ([207.171.184.25]:23435 "EHLO smtp-fw-9101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726483AbfE3C4O (ORCPT ); Wed, 29 May 2019 22:56:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1559184973; x=1590720973; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=qDsL18aDwQTcILov+CgblUA1JhXa5nFHLDzuuC/mnGA=; b=MjLytlaqESDRNy/PcQzbkUWUbn1LeIPx37VN2HYgfvsUNA4kaTLVjS6k 3UYNZf/8DF9ygNvwE2RwZ8JJER9ilvdRD8nff3HsvQlRAvQ5bUzcpRasB wYye1qZduNZB+tks77k312dUt+xEiazQ52FJF1K8rzZWK1JOXcSDYO9kh 4=; X-IronPort-AV: E=Sophos;i="5.60,529,1549929600"; d="scan'208";a="807528124" Received: from sea3-co-svc-lb6-vlan3.sea.amazon.com (HELO email-inbound-relay-2b-3714e498.us-west-2.amazon.com) ([10.47.22.38]) by smtp-border-fw-out-9101.sea19.amazon.com with ESMTP; 30 May 2019 02:56:12 +0000 Received: from EX13MTAUWB001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2b-3714e498.us-west-2.amazon.com (Postfix) with ESMTPS id 2C6DDA25B0; Thu, 30 May 2019 02:56:11 +0000 (UTC) Received: from EX13D05UWB004.ant.amazon.com (10.43.161.208) by EX13MTAUWB001.ant.amazon.com (10.43.161.249) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 30 May 2019 02:56:11 +0000 Received: from EX13MTAUWB001.ant.amazon.com (10.43.161.207) by EX13D05UWB004.ant.amazon.com (10.43.161.208) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 30 May 2019 02:56:10 +0000 Received: from localhost (10.94.220.85) by mail-relay.amazon.com (10.43.161.249) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 30 May 2019 02:56:11 +0000 From: Eduardo Valentin To: Guenter Roeck CC: Eduardo Valentin , Jean Delvare , , Subject: [PATCHv2 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Date: Wed, 29 May 2019 19:56:05 -0700 Message-ID: <20190530025605.3698-3-eduval@amazon.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190530025605.3698-1-eduval@amazon.com> References: <20190530025605.3698-1-eduval@amazon.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When registering a hwmon device with HWMON_C_REGISTER_TZ flag in place, the hwmon subsystem will attempt to register the device also with the thermal subsystem. When the of-thermal registration fails, __hwmon_device_register jumps to ida_remove, leaving the locally allocated hwdev pointer. This patch fixes the leak by jumping to a new label that will first unregister hdev and then fall into the kfree of hwdev to finally remove the idas and propagate the error code. Cc: Jean Delvare Cc: Guenter Roeck Cc: linux-hwmon@vger.kernel.org Cc: linux-kernel@vger.kernel.org Signed-off-by: Eduardo Valentin --- V1->V2: removed the device_unregister() before jumping into the new label, as suggested in the first review round. drivers/hwmon/hwmon.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c index 429784edd5ff..620f05fc412a 100644 --- a/drivers/hwmon/hwmon.c +++ b/drivers/hwmon/hwmon.c @@ -652,10 +652,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, if (info[i]->config[j] & HWMON_T_INPUT) { err = hwmon_thermal_add_sensor(dev, hwdev, j); - if (err) { - device_unregister(hdev); - goto ida_remove; - } + if (err) + goto device_unregister; } } } @@ -663,6 +661,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, return hdev; +device_unregister: + device_unregister(hdev); free_hwmon: kfree(hwdev); ida_remove: -- 2.21.0