public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: "Woodhouse, David" <david.woodhouse@intel.com>
To: "rusty@rustcorp.com.au" <rusty@rustcorp.com.au>
Cc: "arjun024@gmail.com" <arjun024@gmail.com>,
	"torvalds@linux-foundation.org" <torvalds@linux-foundation.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"jg1.han@samsung.com" <jg1.han@samsung.com>,
	"akpm@linux-foundation.org" <akpm@linux-foundation.org>
Subject: Re: [PATCH] params: fix potential memory leak in add_sysfs_param()
Date: Wed, 20 Aug 2014 21:36:07 +0000	[thread overview]
Message-ID: <1408570565.9484.4.camel@intel.com> (raw)
In-Reply-To: <87mwayj39c.fsf@rustcorp.com.au>

[-- Attachment #1: Type: text/plain, Size: 1871 bytes --]

On Thu, 2014-08-21 at 06:19 +0930, Rusty Russell wrote:
> Arjun Sreedharan <arjun024@gmail.com> writes:
> > Do not leak memory when attrs is non NULL and
> > krealloc() fails. Without temporary variable,
> > reference to it is lost.
> >
> > Signed-off-by: Arjun Sreedharan <arjun024@gmail.com>
> 
> ...
> 
> >  	}
> > -	/* Despite looking like the typical realloc() bug, this is safe.
> > -	 * We *want* the old 'attrs' to be freed either way, and we'll store
> > -	 * the new one in the success case. */
> > -	attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
> > -	if (!attrs) {
> > +
> > +	new_attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
> > +	if (!new_attrs) {
> 
> I think that comment you deleted is pretty clear.  Is it wrong?

Yes, I think it's wrong.

I think I was under the impression that we shouldn't be freeing the old
'attrs' pointer on failure because it would live on in
{new,mk->mp}->grp.attrs and still be used. (Which I concede isn't
actually what the comment says, making it doubly wrong.)

I may have failed to notice that when this krealloc() fails, we actually
*free* the 'new' pointer and set mk->mp to NULL. Without freeing the
new->grp.attrs pointer. So it is indeed leaked. We *don't* need a
temporary variable for it though; we can find it.

In fact, I wonder if it's better to change the 'goto fail_free_new' in
the failure path here to instead do something like 'mk->mp = new; return
-ENOMEM;'. That way, the existing params would still be present and we'd
just have failed to add the *new* one. (And the leak wouldn't exist
because the 'attrs' pointer would live on as I originally thought.)

 
-- 
David Woodhouse                            Open Source Technology Centre
David.Woodhouse@intel.com                              Intel Corporation

[-- Attachment #2: smime.p7s --]
[-- Type: application/x-pkcs7-signature, Size: 3437 bytes --]

  parent reply	other threads:[~2014-08-20 21:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-20 20:00 [PATCH] params: fix potential memory leak in add_sysfs_param() Arjun Sreedharan
2014-08-20 20:49 ` Rusty Russell
2014-08-20 21:08   ` Arjun Sreedharan
2014-08-20 22:05     ` Rusty Russell
2014-08-20 22:17       ` Woodhouse, David
2014-08-20 23:10         ` David Woodhouse
2014-08-21  6:50         ` Arjun Sreedharan
2014-08-20 21:36   ` Woodhouse, David [this message]
  -- strict thread matches above, loose matches on Subject: below --
2013-03-14 13:36 [PATCH] params: Fix " David Woodhouse

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=1408570565.9484.4.camel@intel.com \
    --to=david.woodhouse@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=arjun024@gmail.com \
    --cc=jg1.han@samsung.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --cc=torvalds@linux-foundation.org \
    /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