linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ppdev: fix double-free of pp->pdev->name
@ 2016-10-30 15:44 Jann Horn
  2016-10-30 16:29 ` Andy Lutomirski
  2016-10-30 17:54 ` Sudip Mukherjee
  0 siblings, 2 replies; 6+ messages in thread
From: Jann Horn @ 2016-10-30 15:44 UTC (permalink / raw)
  To: Arnd Bergmann, Greg Kroah-Hartman, Sudip Mukherjee
  Cc: linux-kernel, linux-parport, Andy Lutomirski

free_pardevice() is called by parport_unregister_device() and already frees
pp->pdev->name, don't try to do it again.

This bug causes kernel crashes.

I found and verified this with KASAN and some added pr_emerg()s:

[   60.316568] pp_release: pp->pdev->name == ffff88039cb264c0
[   60.316692] free_pardevice: freeing par_dev->name at ffff88039cb264c0
[   60.316706] pp_release: kfree(ffff88039cb264c0)
[   60.316714] ==========================================================
[   60.316722] BUG: Double free or freeing an invalid pointer
[   60.316731] Unexpected shadow byte: 0xFB
[   60.316801] Object at ffff88039cb264c0, in cache kmalloc-32 size: 32
[   60.316813] Allocated:
[   60.316824] PID = 1695
[   60.316869] Freed:
[   60.316880] PID = 1695
[   60.316935] ==========================================================

CCing Andy Lutomirski because I think this is what broke vmapped stacks
for me - after applying this patch, vmapped stacks worked for me.
Previously, I got oopses (and lockups) caused by area->pages[0] being
0x400000000 in __vunmap(), with area->pages being allocated in the kmalloc
area.

Signed-off-by: Jann Horn <jann@thejh.net>
---
 drivers/char/ppdev.c | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/drivers/char/ppdev.c b/drivers/char/ppdev.c
index d23368874710..6af1ce04b3da 100644
--- a/drivers/char/ppdev.c
+++ b/drivers/char/ppdev.c
@@ -748,10 +748,7 @@ static int pp_release(struct inode *inode, struct file *file)
 	}
 
 	if (pp->pdev) {
-		const char *name = pp->pdev->name;
-
 		parport_unregister_device(pp->pdev);
-		kfree(name);
 		pp->pdev = NULL;
 		pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
 	}
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppdev: fix double-free of pp->pdev->name
  2016-10-30 15:44 [PATCH] ppdev: fix double-free of pp->pdev->name Jann Horn
@ 2016-10-30 16:29 ` Andy Lutomirski
  2016-10-30 16:47   ` Jann Horn
  2016-10-30 17:54 ` Sudip Mukherjee
  1 sibling, 1 reply; 6+ messages in thread
From: Andy Lutomirski @ 2016-10-30 16:29 UTC (permalink / raw)
  To: Jann Horn
  Cc: Arnd Bergmann, Greg Kroah-Hartman, Sudip Mukherjee, linux-kernel,
	linux-parport

On Sun, Oct 30, 2016 at 8:44 AM, Jann Horn <jann@thejh.net> wrote:
> free_pardevice() is called by parport_unregister_device() and already frees
> pp->pdev->name, don't try to do it again.
>
> This bug causes kernel crashes.
>
> I found and verified this with KASAN and some added pr_emerg()s:
>
> [   60.316568] pp_release: pp->pdev->name == ffff88039cb264c0
> [   60.316692] free_pardevice: freeing par_dev->name at ffff88039cb264c0
> [   60.316706] pp_release: kfree(ffff88039cb264c0)
> [   60.316714] ==========================================================
> [   60.316722] BUG: Double free or freeing an invalid pointer
> [   60.316731] Unexpected shadow byte: 0xFB
> [   60.316801] Object at ffff88039cb264c0, in cache kmalloc-32 size: 32
> [   60.316813] Allocated:
> [   60.316824] PID = 1695
> [   60.316869] Freed:
> [   60.316880] PID = 1695
> [   60.316935] ==========================================================
>
> CCing Andy Lutomirski because I think this is what broke vmapped stacks
> for me - after applying this patch, vmapped stacks worked for me.
> Previously, I got oopses (and lockups) caused by area->pages[0] being
> 0x400000000 in __vunmap(), with area->pages being allocated in the kmalloc
> area.

That's an odd symptom.  I assume that what's happening is that the
pages array is being freed early by the extra kfree in here and then
getting corrupted.

--Andy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppdev: fix double-free of pp->pdev->name
  2016-10-30 16:29 ` Andy Lutomirski
@ 2016-10-30 16:47   ` Jann Horn
  0 siblings, 0 replies; 6+ messages in thread
From: Jann Horn @ 2016-10-30 16:47 UTC (permalink / raw)
  To: Andy Lutomirski
  Cc: Arnd Bergmann, Greg Kroah-Hartman, Sudip Mukherjee, linux-kernel,
	linux-parport

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

On Sun, Oct 30, 2016 at 09:29:10AM -0700, Andy Lutomirski wrote:
> On Sun, Oct 30, 2016 at 8:44 AM, Jann Horn <jann@thejh.net> wrote:
> > free_pardevice() is called by parport_unregister_device() and already frees
> > pp->pdev->name, don't try to do it again.
> >
> > This bug causes kernel crashes.
> >
> > I found and verified this with KASAN and some added pr_emerg()s:
> >
> > [   60.316568] pp_release: pp->pdev->name == ffff88039cb264c0
> > [   60.316692] free_pardevice: freeing par_dev->name at ffff88039cb264c0
> > [   60.316706] pp_release: kfree(ffff88039cb264c0)
> > [   60.316714] ==========================================================
> > [   60.316722] BUG: Double free or freeing an invalid pointer
> > [   60.316731] Unexpected shadow byte: 0xFB
> > [   60.316801] Object at ffff88039cb264c0, in cache kmalloc-32 size: 32
> > [   60.316813] Allocated:
> > [   60.316824] PID = 1695
> > [   60.316869] Freed:
> > [   60.316880] PID = 1695
> > [   60.316935] ==========================================================
> >
> > CCing Andy Lutomirski because I think this is what broke vmapped stacks
> > for me - after applying this patch, vmapped stacks worked for me.
> > Previously, I got oopses (and lockups) caused by area->pages[0] being
> > 0x400000000 in __vunmap(), with area->pages being allocated in the kmalloc
> > area.
> 
> That's an odd symptom.  I assume that what's happening is that the
> pages array is being freed early by the extra kfree in here and then
> getting corrupted.

Well, as far as I can tell, there are two ways to reach that.

Obvious first way, but a pretty tight race:

Task A: free(name)
Task B: allocate area->pages in same place
Task A: second free(name), releasing area->pages

Second way (if the SLAB allocator, which I'm using, is used):

Task A: free(name), appends the object to ac->entry in ___cache_free()
Task A: second free(name), appends the object to ac->entry again
Task B: ____cache_alloc() returns object from array cache
Task C: ____cache_alloc() returns same object again

So then the same memory would be used by two separate objects?

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppdev: fix double-free of pp->pdev->name
  2016-10-30 15:44 [PATCH] ppdev: fix double-free of pp->pdev->name Jann Horn
  2016-10-30 16:29 ` Andy Lutomirski
@ 2016-10-30 17:54 ` Sudip Mukherjee
  2016-10-30 18:00   ` Jann Horn
  1 sibling, 1 reply; 6+ messages in thread
From: Sudip Mukherjee @ 2016-10-30 17:54 UTC (permalink / raw)
  To: Jann Horn, Arnd Bergmann, Greg Kroah-Hartman
  Cc: linux-kernel, linux-parport, Andy Lutomirski

On Sunday 30 October 2016 09:14 PM, Jann Horn wrote:
> free_pardevice() is called by parport_unregister_device() and already frees
> pp->pdev->name, don't try to do it again.
>
> This bug causes kernel crashes.
>
> I found and verified this with KASAN and some added pr_emerg()s:
>
> [   60.316568] pp_release: pp->pdev->name == ffff88039cb264c0
> [   60.316692] free_pardevice: freeing par_dev->name at ffff88039cb264c0
> [   60.316706] pp_release: kfree(ffff88039cb264c0)
> [   60.316714] ==========================================================
> [   60.316722] BUG: Double free or freeing an invalid pointer
> [   60.316731] Unexpected shadow byte: 0xFB
> [   60.316801] Object at ffff88039cb264c0, in cache kmalloc-32 size: 32
> [   60.316813] Allocated:
> [   60.316824] PID = 1695
> [   60.316869] Freed:
> [   60.316880] PID = 1695
> [   60.316935] ==========================================================
>
> CCing Andy Lutomirski because I think this is what broke vmapped stacks
> for me - after applying this patch, vmapped stacks worked for me.
> Previously, I got oopses (and lockups) caused by area->pages[0] being
> 0x400000000 in __vunmap(), with area->pages being allocated in the kmalloc
> area.

I think the above should not be a part of the commit message.
It looks like a valid bug. As we have now started using the device model 
from this release, so device_unregister() will finally call 
free_pardevice() and that will free the name.

>
> Signed-off-by: Jann Horn <jann@thejh.net>
> ---

Acked-by: Sudip Mukherjee <sudipm.mukherjee@gmail.com>

>   drivers/char/ppdev.c | 3 ---
>   1 file changed, 3 deletions(-)
>
> diff --git a/drivers/char/ppdev.c b/drivers/char/ppdev.c
> index d23368874710..6af1ce04b3da 100644
> --- a/drivers/char/ppdev.c
> +++ b/drivers/char/ppdev.c
> @@ -748,10 +748,7 @@ static int pp_release(struct inode *inode, struct file *file)
>   	}
>
>   	if (pp->pdev) {
> -		const char *name = pp->pdev->name;
> -
>   		parport_unregister_device(pp->pdev);
> -		kfree(name);
>   		pp->pdev = NULL;
>   		pr_debug(CHRDEV "%x: unregistered pardevice\n", minor);
>   	}
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppdev: fix double-free of pp->pdev->name
  2016-10-30 17:54 ` Sudip Mukherjee
@ 2016-10-30 18:00   ` Jann Horn
  2016-10-30 22:12     ` Greg Kroah-Hartman
  0 siblings, 1 reply; 6+ messages in thread
From: Jann Horn @ 2016-10-30 18:00 UTC (permalink / raw)
  To: Sudip Mukherjee
  Cc: Arnd Bergmann, Greg Kroah-Hartman, linux-kernel, linux-parport,
	Andy Lutomirski

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

On Sun, Oct 30, 2016 at 11:24:27PM +0530, Sudip Mukherjee wrote:
> On Sunday 30 October 2016 09:14 PM, Jann Horn wrote:
> >free_pardevice() is called by parport_unregister_device() and already frees
> >pp->pdev->name, don't try to do it again.
> >
> >This bug causes kernel crashes.
> >
> >I found and verified this with KASAN and some added pr_emerg()s:
> >
> >[   60.316568] pp_release: pp->pdev->name == ffff88039cb264c0
> >[   60.316692] free_pardevice: freeing par_dev->name at ffff88039cb264c0
> >[   60.316706] pp_release: kfree(ffff88039cb264c0)
> >[   60.316714] ==========================================================
> >[   60.316722] BUG: Double free or freeing an invalid pointer
> >[   60.316731] Unexpected shadow byte: 0xFB
> >[   60.316801] Object at ffff88039cb264c0, in cache kmalloc-32 size: 32
> >[   60.316813] Allocated:
> >[   60.316824] PID = 1695
> >[   60.316869] Freed:
> >[   60.316880] PID = 1695
> >[   60.316935] ==========================================================
> >
> >CCing Andy Lutomirski because I think this is what broke vmapped stacks
> >for me - after applying this patch, vmapped stacks worked for me.
> >Previously, I got oopses (and lockups) caused by area->pages[0] being
> >0x400000000 in __vunmap(), with area->pages being allocated in the kmalloc
> >area.
> 
> I think the above should not be a part of the commit message.

Ah, yes.

@maintainers: Feel free to remove that from the commit message. Or should I
resend?

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] ppdev: fix double-free of pp->pdev->name
  2016-10-30 18:00   ` Jann Horn
@ 2016-10-30 22:12     ` Greg Kroah-Hartman
  0 siblings, 0 replies; 6+ messages in thread
From: Greg Kroah-Hartman @ 2016-10-30 22:12 UTC (permalink / raw)
  To: Jann Horn
  Cc: Sudip Mukherjee, Arnd Bergmann, linux-kernel, linux-parport,
	Andy Lutomirski

On Sun, Oct 30, 2016 at 07:00:36PM +0100, Jann Horn wrote:
> On Sun, Oct 30, 2016 at 11:24:27PM +0530, Sudip Mukherjee wrote:
> > On Sunday 30 October 2016 09:14 PM, Jann Horn wrote:
> > >free_pardevice() is called by parport_unregister_device() and already frees
> > >pp->pdev->name, don't try to do it again.
> > >
> > >This bug causes kernel crashes.
> > >
> > >I found and verified this with KASAN and some added pr_emerg()s:
> > >
> > >[   60.316568] pp_release: pp->pdev->name == ffff88039cb264c0
> > >[   60.316692] free_pardevice: freeing par_dev->name at ffff88039cb264c0
> > >[   60.316706] pp_release: kfree(ffff88039cb264c0)
> > >[   60.316714] ==========================================================
> > >[   60.316722] BUG: Double free or freeing an invalid pointer
> > >[   60.316731] Unexpected shadow byte: 0xFB
> > >[   60.316801] Object at ffff88039cb264c0, in cache kmalloc-32 size: 32
> > >[   60.316813] Allocated:
> > >[   60.316824] PID = 1695
> > >[   60.316869] Freed:
> > >[   60.316880] PID = 1695
> > >[   60.316935] ==========================================================
> > >
> > >CCing Andy Lutomirski because I think this is what broke vmapped stacks
> > >for me - after applying this patch, vmapped stacks worked for me.
> > >Previously, I got oopses (and lockups) caused by area->pages[0] being
> > >0x400000000 in __vunmap(), with area->pages being allocated in the kmalloc
> > >area.
> > 
> > I think the above should not be a part of the commit message.
> 
> Ah, yes.
> 
> @maintainers: Feel free to remove that from the commit message. Or should I
> resend?

Please resend, never make a maintainer hand-edit a patch changelog :)

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2016-10-30 22:12 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-30 15:44 [PATCH] ppdev: fix double-free of pp->pdev->name Jann Horn
2016-10-30 16:29 ` Andy Lutomirski
2016-10-30 16:47   ` Jann Horn
2016-10-30 17:54 ` Sudip Mukherjee
2016-10-30 18:00   ` Jann Horn
2016-10-30 22:12     ` Greg Kroah-Hartman

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).