diff mbox series

efivarfs: revert "fix memory leak in efivarfs_create()"

Message ID 20201125075303.3963-1-ardb@kernel.org
State Accepted
Commit ff04f3b6f2e27f8ae28a498416af2a8dd5072b43
Headers show
Series efivarfs: revert "fix memory leak in efivarfs_create()" | expand

Commit Message

Ard Biesheuvel Nov. 25, 2020, 7:53 a.m. UTC
The memory leak addressed by commit fe5186cf12e3 is a false positive:
all allocations are recorded in a linked list, and freed when the
filesystem is unmounted. This leads to double frees, and as reported
by David, leads to crashes if SLUB is configured to self destruct when
double frees occur.

So drop the redundant kfree() again, and instead, mark the offending
pointer variable so the allocation is ignored by kmemleak.

Cc: Vamshi K Sthambamkadi <vamshi.k.sthambamkadi@gmail.com>
Fixes: fe5186cf12e3 ("efivarfs: fix memory leak in efivarfs_create()")
Reported-by: David Laight <David.Laight@aculab.com>
Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 fs/efivarfs/inode.c | 1 +
 fs/efivarfs/super.c | 1 -
 2 files changed, 1 insertion(+), 1 deletion(-)

Comments

Ard Biesheuvel Nov. 25, 2020, 8:05 a.m. UTC | #1
On Wed, 25 Nov 2020 at 09:05, Oleksandr Natalenko
<oleksandr@natalenko.name> wrote:
>
> Hello.
>
> On 25.11.2020 08:53, Ard Biesheuvel wrote:
> > The memory leak addressed by commit fe5186cf12e3 is a false positive:
> > all allocations are recorded in a linked list, and freed when the
> > filesystem is unmounted. This leads to double frees, and as reported
> > by David, leads to crashes if SLUB is configured to self destruct when
> > double frees occur.
> >
> > So drop the redundant kfree() again, and instead, mark the offending
> > pointer variable so the allocation is ignored by kmemleak.
> >
> > Cc: Vamshi K Sthambamkadi <vamshi.k.sthambamkadi@gmail.com>
>
> Should also have:
>
> Cc: <stable@vger.kernel.org> # v5.9
>

No it should not. The fixes tag should be sufficient.

> > Fixes: fe5186cf12e3 ("efivarfs: fix memory leak in efivarfs_create()")
> > Reported-by: David Laight <David.Laight@aculab.com>
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > ---
> >  fs/efivarfs/inode.c | 1 +
> >  fs/efivarfs/super.c | 1 -
> >  2 files changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/fs/efivarfs/inode.c b/fs/efivarfs/inode.c
> > index 96c0c86f3fff..38324427a2b3 100644
> > --- a/fs/efivarfs/inode.c
> > +++ b/fs/efivarfs/inode.c
> > @@ -103,6 +103,7 @@ static int efivarfs_create(struct inode *dir,
> > struct dentry *dentry,
> >       var->var.VariableName[i] = '\0';
> >
> >       inode->i_private = var;
> > +     kmemleak_ignore(var);
> >
> >       err = efivar_entry_add(var, &efivarfs_list);
> >       if (err)
> > diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
> > index f943fd0b0699..15880a68faad 100644
> > --- a/fs/efivarfs/super.c
> > +++ b/fs/efivarfs/super.c
> > @@ -21,7 +21,6 @@ LIST_HEAD(efivarfs_list);
> >  static void efivarfs_evict_inode(struct inode *inode)
> >  {
> >       clear_inode(inode);
> > -     kfree(inode->i_private);
> >  }
> >
> >  static const struct super_operations efivarfs_ops = {
>
> --
>    Oleksandr Natalenko (post-factum)
Ard Biesheuvel Nov. 25, 2020, 10:28 a.m. UTC | #2
On Wed, 25 Nov 2020 at 11:27, Oleksandr Natalenko
<oleksandr@natalenko.name> wrote:
>
> On 25.11.2020 08:53, Ard Biesheuvel wrote:
> > The memory leak addressed by commit fe5186cf12e3 is a false positive:
> > all allocations are recorded in a linked list, and freed when the
> > filesystem is unmounted. This leads to double frees, and as reported
> > by David, leads to crashes if SLUB is configured to self destruct when
> > double frees occur.
> >
> > So drop the redundant kfree() again, and instead, mark the offending
> > pointer variable so the allocation is ignored by kmemleak.
> >
> > Cc: Vamshi K Sthambamkadi <vamshi.k.sthambamkadi@gmail.com>
> > Fixes: fe5186cf12e3 ("efivarfs: fix memory leak in efivarfs_create()")
> > Reported-by: David Laight <David.Laight@aculab.com>
> > Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
> > ---
> >  fs/efivarfs/inode.c | 1 +
> >  fs/efivarfs/super.c | 1 -
> >  2 files changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/fs/efivarfs/inode.c b/fs/efivarfs/inode.c
> > index 96c0c86f3fff..38324427a2b3 100644
> > --- a/fs/efivarfs/inode.c
> > +++ b/fs/efivarfs/inode.c
> > @@ -103,6 +103,7 @@ static int efivarfs_create(struct inode *dir,
> > struct dentry *dentry,
> >       var->var.VariableName[i] = '\0';
> >
> >       inode->i_private = var;
> > +     kmemleak_ignore(var);
>
> Do we need to do this as well:
>
> #include <linux/kmemleak.h>
>
> ?
>
> Because otherwise for 5.9 I get:
>
> [  148s] fs/efivarfs/inode.c: In function 'efivarfs_create':
> [  148s] fs/efivarfs/inode.c:106:2: error: implicit declaration of
> function 'kmemleak_ignore' [-Werror=implicit-function-declaration]
> [  148s]   106 |  kmemleak_ignore(var);
> [  148s]       |  ^~~~~~~~~~~~~~~
>

Ah yes, thanks for the report. I will add the include to the patch.


> >
> >       err = efivar_entry_add(var, &efivarfs_list);
> >       if (err)
> > diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
> > index f943fd0b0699..15880a68faad 100644
> > --- a/fs/efivarfs/super.c
> > +++ b/fs/efivarfs/super.c
> > @@ -21,7 +21,6 @@ LIST_HEAD(efivarfs_list);
> >  static void efivarfs_evict_inode(struct inode *inode)
> >  {
> >       clear_inode(inode);
> > -     kfree(inode->i_private);
> >  }
> >
> >  static const struct super_operations efivarfs_ops = {
>
> --
>    Oleksandr Natalenko (post-factum)
Jonathon Fernyhough Nov. 27, 2020, 4:50 p.m. UTC | #3
On 25/11/2020 10:28, Ard Biesheuvel wrote:
> On Wed, 25 Nov 2020 at 11:27, Oleksandr Natalenko

> <oleksandr@natalenko.name> wrote:

>>

>> On 25.11.2020 08:53, Ard Biesheuvel wrote:

--snip--
>>

>> Do we need to do this as well:

>>

>> #include <linux/kmemleak.h>

>>

>> ?

>>

>> Because otherwise for 5.9 I get:

>>

>> [  148s] fs/efivarfs/inode.c: In function 'efivarfs_create':

>> [  148s] fs/efivarfs/inode.c:106:2: error: implicit declaration of

>> function 'kmemleak_ignore' [-Werror=implicit-function-declaration]

>> [  148s]   106 |  kmemleak_ignore(var);

>> [  148s]       |  ^~~~~~~~~~~~~~~

>>

> 

> Ah yes, thanks for the report. I will add the include to the patch.

> 

> 


Is this necessary if CONFIG_DEBUG_KMEMLEAK is not enabled in the kernel
config? e.g. should there be an #ifdef CONFIG_DEBUG_KMEMLEAK somewhere
in there?
Ard Biesheuvel Nov. 27, 2020, 4:58 p.m. UTC | #4
On Fri, 27 Nov 2020 at 17:56, Jonathon Fernyhough <jonathon@m2x.dev> wrote:
>

> On 25/11/2020 10:28, Ard Biesheuvel wrote:

> > On Wed, 25 Nov 2020 at 11:27, Oleksandr Natalenko

> > <oleksandr@natalenko.name> wrote:

> >>

> >> On 25.11.2020 08:53, Ard Biesheuvel wrote:

> --snip--

> >>

> >> Do we need to do this as well:

> >>

> >> #include <linux/kmemleak.h>

> >>

> >> ?

> >>

> >> Because otherwise for 5.9 I get:

> >>

> >> [  148s] fs/efivarfs/inode.c: In function 'efivarfs_create':

> >> [  148s] fs/efivarfs/inode.c:106:2: error: implicit declaration of

> >> function 'kmemleak_ignore' [-Werror=implicit-function-declaration]

> >> [  148s]   106 |  kmemleak_ignore(var);

> >> [  148s]       |  ^~~~~~~~~~~~~~~

> >>

> >

> > Ah yes, thanks for the report. I will add the include to the patch.

> >

> >

>

> Is this necessary if CONFIG_DEBUG_KMEMLEAK is not enabled in the kernel

> config? e.g. should there be an #ifdef CONFIG_DEBUG_KMEMLEAK somewhere

> in there?

>


We typically define these helpers unconditionally, and sort out the
differences in the header file. In this case, we have

static inline void kmemleak_ignore(const void *ptr)
{
}

in include/linux/kmemleak.h if CONFIG_DEBUG_KMEMLEAK is not set.

This makes the calling code much cleaner.
Oleksandr Natalenko Nov. 27, 2020, 4:59 p.m. UTC | #5
Hi.

On Fri, Nov 27, 2020 at 04:50:34PM +0000, Jonathon Fernyhough wrote:
> On 25/11/2020 10:28, Ard Biesheuvel wrote:

> > On Wed, 25 Nov 2020 at 11:27, Oleksandr Natalenko

> > <oleksandr@natalenko.name> wrote:

> >>

> >> On 25.11.2020 08:53, Ard Biesheuvel wrote:

> --snip--

> >>

> >> Do we need to do this as well:

> >>

> >> #include <linux/kmemleak.h>

> >>

> >> ?

> >>

> >> Because otherwise for 5.9 I get:

> >>

> >> [  148s] fs/efivarfs/inode.c: In function 'efivarfs_create':

> >> [  148s] fs/efivarfs/inode.c:106:2: error: implicit declaration of

> >> function 'kmemleak_ignore' [-Werror=implicit-function-declaration]

> >> [  148s]   106 |  kmemleak_ignore(var);

> >> [  148s]       |  ^~~~~~~~~~~~~~~

> >>

> > 

> > Ah yes, thanks for the report. I will add the include to the patch.

> > 

> > 

> 

> Is this necessary if CONFIG_DEBUG_KMEMLEAK is not enabled in the kernel

> config? e.g. should there be an #ifdef CONFIG_DEBUG_KMEMLEAK somewhere

> in there?


kmemleak_ignore() is a noop if CONFIG_DEBUG_KMEMLEAK is not set. See
include/linux/kmemleak.h. Thus no extra condition is needed here.

-- 
  Oleksandr Natalenko (post-factum)
diff mbox series

Patch

diff --git a/fs/efivarfs/inode.c b/fs/efivarfs/inode.c
index 96c0c86f3fff..38324427a2b3 100644
--- a/fs/efivarfs/inode.c
+++ b/fs/efivarfs/inode.c
@@ -103,6 +103,7 @@  static int efivarfs_create(struct inode *dir, struct dentry *dentry,
 	var->var.VariableName[i] = '\0';
 
 	inode->i_private = var;
+	kmemleak_ignore(var);
 
 	err = efivar_entry_add(var, &efivarfs_list);
 	if (err)
diff --git a/fs/efivarfs/super.c b/fs/efivarfs/super.c
index f943fd0b0699..15880a68faad 100644
--- a/fs/efivarfs/super.c
+++ b/fs/efivarfs/super.c
@@ -21,7 +21,6 @@  LIST_HEAD(efivarfs_list);
 static void efivarfs_evict_inode(struct inode *inode)
 {
 	clear_inode(inode);
-	kfree(inode->i_private);
 }
 
 static const struct super_operations efivarfs_ops = {