mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Eduardo Valentin <eduval@amazon.com>
To: Guenter Roeck <linux@roeck-us.net>
Cc: Eduardo Valentin <eduval@amazon.com>,
	Jean Delvare <jdelvare@suse.com>, <linux-hwmon@vger.kernel.org>,
	<linux-kernel@vger.kernel.org>
Subject: [PATCHv2 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register*
Date: Wed, 29 May 2019 19:56:05 -0700	[thread overview]
Message-ID: <20190530025605.3698-3-eduval@amazon.com> (raw)
In-Reply-To: <20190530025605.3698-1-eduval@amazon.com>

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 <jdelvare@suse.com>
Cc: Guenter Roeck <linux@roeck-us.net>
Cc: linux-hwmon@vger.kernel.org
Cc: linux-kernel@vger.kernel.org
Signed-off-by: Eduardo Valentin <eduval@amazon.com>
---
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


  parent reply	other threads:[~2019-05-30  2:56 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-30  2:56 [PATCHv2 0/2] hwmon: couple of fixes on HWMON_C_REGISTER_TZ Eduardo Valentin
2019-05-30  2:56 ` [PATCHv2 1/2] hwmon: core: add thermal sensors only if dev->of_node is present Eduardo Valentin
2019-05-30  2:56 ` Eduardo Valentin [this message]
2019-06-05 20:29   ` [PATCHv2 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Guenter Roeck
2019-06-05 20:38   ` Guenter Roeck
2019-06-06 14:35     ` Eduardo Valentin
2019-06-06 16:55       ` Guenter Roeck

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=20190530025605.3698-3-eduval@amazon.com \
    --to=eduval@amazon.com \
    --cc=jdelvare@suse.com \
    --cc=linux-hwmon@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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

Powered by JetHome