All of lore.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 22:17:48 +0000	[thread overview]
Message-ID: <1408573066.9484.11.camel@intel.com> (raw)
In-Reply-To: <87ha16izpu.fsf@rustcorp.com.au>

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

On Thu, 2014-08-21 at 07:35 +0930, Rusty Russell wrote:
> 
> Above this:
>         if (!mk->mp) {
>                 num = 0;
>                 attrs = NULL;
>         } else {
>                 num = mk->mp->num;
>                 attrs = mk->mp->grp.attrs;
>         }
> 
> So, attrs is just a temporary: either NULL (doesn't need freeing), or
> is the old mk->mp->grp.attrs ptr.

Except that in the failure case we *free* the old mk->mp and never free
mk->mp->grp.attrs so it *is* indeed lost.

A simpler version of Arjun's patch might look like this:

diff --git a/kernel/params.c b/kernel/params.c
index 34f5270..f9459bc 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -613,7 +613,6 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
 		       sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
 		       GFP_KERNEL);
 	if (!new) {
-		kfree(attrs);
 		err = -ENOMEM;
 		goto fail;
 	}
@@ -653,7 +652,10 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
 fail_free_new:
 	kfree(new);
 fail:
-	mk->mp = NULL;
+	if (mk->mp) {
+		kfree(mk->mp->grp.attrs);
+		mk->mp = NULL;
+	}
 	return err;
 }
 


But as I suggested in my previous response, a *better* fix might look
like this:

diff --git a/kernel/params.c b/kernel/params.c
index 34f5270..cdab9d4 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -595,7 +595,7 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
 {
 	struct module_param_attrs *new;
 	struct attribute **attrs;
-	int err, num;
+	int num;
 
 	/* We don't bother calling this with invisible parameters. */
 	BUG_ON(!kp->perm);
@@ -612,18 +612,19 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
 	new = krealloc(mk->mp,
 		       sizeof(*mk->mp) + sizeof(mk->mp->attrs[0]) * (num+1),
 		       GFP_KERNEL);
-	if (!new) {
-		kfree(attrs);
-		err = -ENOMEM;
-		goto fail;
-	}
+	if (!new)
+		return -ENOMEM;
+
 	/* 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. */
+	 * In the failure case, the old 'attrs' is still in new->grp.attrs
+	 * and will live on there. */
 	attrs = krealloc(attrs, sizeof(new->grp.attrs[0])*(num+2), GFP_KERNEL);
 	if (!attrs) {
-		err = -ENOMEM;
-		goto fail_free_new;
+		/* This is in a larger kmalloc allocation than before but
+		 * otherwise entirely unchanged. We've failed to add the
+		 * new param but the existing ones are still there. */
+		mk->mp = new;
+		return -ENOMEM;
 	}
 
 	/* Sysfs wants everything zeroed. */
@@ -649,12 +650,6 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
 
 	mk->mp = new;
 	return 0;
-
-fail_free_new:
-	kfree(new);
-fail:
-	mk->mp = NULL;
-	return err;
 }
 
 #ifdef CONFIG_MODULES


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

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

  reply	other threads:[~2014-08-20 22:17 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 [this message]
2014-08-20 23:10         ` David Woodhouse
2014-08-21  6:50         ` Arjun Sreedharan
2014-08-20 21:36   ` Woodhouse, David
  -- 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=1408573066.9484.11.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.