linux-hwmon.vger.kernel.org archive mirror
 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: Re: [PATCH 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register*
Date: Wed, 29 May 2019 16:14:45 -0700	[thread overview]
Message-ID: <20190529231445.GC18339@u40b0340c692b58f6553c.ant.amazon.com> (raw)
In-Reply-To: <20190528150640.GA5516@roeck-us.net>

On Tue, May 28, 2019 at 08:06:40AM -0700, Guenter Roeck wrote:
> Hi Eduardo,
> 
> On Fri, May 17, 2019 at 04:13:37PM -0700, Eduardo Valentin wrote:
> > 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 and also the hdev registered.
> > 
> > This patch fixes both issues by jumping to a new label that
> > will first unregister hdev and the 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>
> > ---
> >  drivers/hwmon/hwmon.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c
> > index 6b3559f58b67..6f1194952189 100644
> > --- a/drivers/hwmon/hwmon.c
> > +++ b/drivers/hwmon/hwmon.c
> > @@ -637,7 +637,7 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata,
> >  								hwdev, j);
> >  					if (err) {
> >  						device_unregister(hdev);
> > -						goto ida_remove;
> > +						goto device_unregister;
> 
> Good find, but device_unregister() is already called above.
> You need to either remove that, or replace the goto to point to free_hwmon.
> The new label would probably the cleaner solution since it follows the
> coding style.

Right, somehow I completely missed that unregister call. In any case, I will
take the route of adding a new label and remove the unregister call above.

> 
> Thanks
> Guenter
> 
> >  					}
> >  				}
> >  			}
> > @@ -646,6 +646,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
> > 

-- 
All the best,
Eduardo Valentin

      reply	other threads:[~2019-05-29 23:14 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20190517231337.27859-1-eduval@amazon.com>
2019-05-17 23:13 ` [PATCH 1/2] hwmon: core: add thermal sensors only if dev->of_node is present Eduardo Valentin
2019-05-28 15:08   ` Guenter Roeck
2019-05-29 23:12     ` Eduardo Valentin
2019-05-17 23:13 ` [PATCH 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Eduardo Valentin
2019-05-28 15:06   ` Guenter Roeck
2019-05-29 23:14     ` Eduardo Valentin [this message]

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=20190529231445.GC18339@u40b0340c692b58f6553c.ant.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;
as well as URLs for NNTP newsgroup(s).