mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] Fix memory leak in vc_resize/vc_allocate
@ 2006-08-10 14:22 Catalin Marinas
  2006-08-14 22:43 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Catalin Marinas @ 2006-08-10 14:22 UTC (permalink / raw)
  To: linux-kernel

From: Catalin Marinas <catalin.marinas@arm.com>

Memory leaks can happen in the vc_resize() function in drivers/char/vt.c
because of the vc->vc_screenbuf variable overriding in vc_allocate(). The
kmemleak reported trace is as follows:

  <__kmalloc>
  <vc_resize>
  <fbcon_init>
  <visual_init>
  <vc_allocate>
  <con_open>
  <tty_open>
  <chrdev_open>

This patch no longer allocates a screen buffer in vc_allocate() if it was
already allocated by vc_resize().

Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
---

 drivers/char/vt.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/drivers/char/vt.c b/drivers/char/vt.c
index da7e66a..31c8b32 100644
--- a/drivers/char/vt.c
+++ b/drivers/char/vt.c
@@ -730,7 +730,8 @@ int vc_allocate(unsigned int currcons)	/
 	    visual_init(vc, currcons, 1);
 	    if (!*vc->vc_uni_pagedir_loc)
 		con_set_default_unimap(vc);
-	    vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
+	    if (!vc->vc_kmalloced)
+		vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
 	    if (!vc->vc_screenbuf) {
 		kfree(vc);
 		vc_cons[currcons].d = NULL;

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

* Re: [PATCH] Fix memory leak in vc_resize/vc_allocate
  2006-08-10 14:22 [PATCH] Fix memory leak in vc_resize/vc_allocate Catalin Marinas
@ 2006-08-14 22:43 ` Andrew Morton
  2006-08-14 23:44   ` Antonino A. Daplas
  2006-08-15  8:16   ` Catalin Marinas
  0 siblings, 2 replies; 4+ messages in thread
From: Andrew Morton @ 2006-08-14 22:43 UTC (permalink / raw)
  To: catalin.marinas; +Cc: Catalin Marinas, linux-kernel, Antonino A. Daplas

On Thu, 10 Aug 2006 15:22:21 +0100
Catalin Marinas <catalin.marinas@arm.com> wrote:

> From: Catalin Marinas <catalin.marinas@arm.com>
> 
> Memory leaks can happen in the vc_resize() function in drivers/char/vt.c
> because of the vc->vc_screenbuf variable overriding in vc_allocate(). The
> kmemleak reported trace is as follows:
> 
>   <__kmalloc>
>   <vc_resize>
>   <fbcon_init>
>   <visual_init>
>   <vc_allocate>
>   <con_open>
>   <tty_open>
>   <chrdev_open>
> 
> This patch no longer allocates a screen buffer in vc_allocate() if it was
> already allocated by vc_resize().
> 
> Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
> ---
> 
>  drivers/char/vt.c |    3 ++-
>  1 files changed, 2 insertions(+), 1 deletions(-)
> 
> diff --git a/drivers/char/vt.c b/drivers/char/vt.c
> index da7e66a..31c8b32 100644
> --- a/drivers/char/vt.c
> +++ b/drivers/char/vt.c
> @@ -730,7 +730,8 @@ int vc_allocate(unsigned int currcons)	/
>  	    visual_init(vc, currcons, 1);
>  	    if (!*vc->vc_uni_pagedir_loc)
>  		con_set_default_unimap(vc);
> -	    vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
> +	    if (!vc->vc_kmalloced)
> +		vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
>  	    if (!vc->vc_screenbuf) {
>  		kfree(vc);
>  		vc_cons[currcons].d = NULL;

hm.  Maybe.  I'd worry that the memory at vc->vc_screenbuf isn't of the
correct size and this patch will convert a leak into a buffer overrun.

Also, what's up with this, in vc_resize()?

	if (vc->vc_kmalloced)
		kfree(vc->vc_screenbuf);
	vc->vc_screenbuf = newscreen;
	vc->vc_kmalloced = 1;

if vc->vc_kmalloced means "there is kmalloced memory at vc->vc_screenbuf"
then this is wrong.

This code is all pretty twisty and I fear touching it.

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

* Re: [PATCH] Fix memory leak in vc_resize/vc_allocate
  2006-08-14 22:43 ` Andrew Morton
@ 2006-08-14 23:44   ` Antonino A. Daplas
  2006-08-15  8:16   ` Catalin Marinas
  1 sibling, 0 replies; 4+ messages in thread
From: Antonino A. Daplas @ 2006-08-14 23:44 UTC (permalink / raw)
  To: Andrew Morton; +Cc: catalin.marinas, Catalin Marinas, linux-kernel

On Mon, 2006-08-14 at 15:43 -0700, Andrew Morton wrote:
> On Thu, 10 Aug 2006 15:22:21 +0100
> Catalin Marinas <catalin.marinas@arm.com> wrote:
> 
> > From: Catalin Marinas <catalin.marinas@arm.com>
> > 
> > Memory leaks can happen in the vc_resize() function in drivers/char/vt.c
> > because of the vc->vc_screenbuf variable overriding in vc_allocate(). The
> > kmemleak reported trace is as follows:
> > 
> >   <__kmalloc>
> >   <vc_resize>
> >   <fbcon_init>
> >   <visual_init>
> >   <vc_allocate>
> >   <con_open>
> >   <tty_open>
> >   <chrdev_open>
> > 
> > This patch no longer allocates a screen buffer in vc_allocate() if it was
> > already allocated by vc_resize().
> > 
> > Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
> > ---
> > 
> >  drivers/char/vt.c |    3 ++-
> >  1 files changed, 2 insertions(+), 1 deletions(-)
> > 
> > diff --git a/drivers/char/vt.c b/drivers/char/vt.c
> > index da7e66a..31c8b32 100644
> > --- a/drivers/char/vt.c
> > +++ b/drivers/char/vt.c
> > @@ -730,7 +730,8 @@ int vc_allocate(unsigned int currcons)	/
> >  	    visual_init(vc, currcons, 1);
> >  	    if (!*vc->vc_uni_pagedir_loc)
> >  		con_set_default_unimap(vc);
> > -	    vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
> > +	    if (!vc->vc_kmalloced)
> > +		vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
> >  	    if (!vc->vc_screenbuf) {
> >  		kfree(vc);
> >  		vc_cons[currcons].d = NULL;
> 
> hm.  Maybe.  I'd worry that the memory at vc->vc_screenbuf isn't of the
> correct size and this patch will convert a leak into a buffer overrun.

It's a safe test, visual_init() will assure that screenbuf_size will
always be correct. 

> Also, what's up with this, in vc_resize()?
> 
> 	if (vc->vc_kmalloced)
> 		kfree(vc->vc_screenbuf);
> 	vc->vc_screenbuf = newscreen;
> 	vc->vc_kmalloced = 1;
> 
> if vc->vc_kmalloced means "there is kmalloced memory at vc->vc_screenbuf"
> then this is wrong.

vc_screenbuf is either of the bootmem type or the kmalloced type as
indicated by vc->vc_kmalloced.

> 
> This code is all pretty twisty and I fear touching it.

Yes, but the patch as is should be okay.

Tony


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

* Re: [PATCH] Fix memory leak in vc_resize/vc_allocate
  2006-08-14 22:43 ` Andrew Morton
  2006-08-14 23:44   ` Antonino A. Daplas
@ 2006-08-15  8:16   ` Catalin Marinas
  1 sibling, 0 replies; 4+ messages in thread
From: Catalin Marinas @ 2006-08-15  8:16 UTC (permalink / raw)
  To: Andrew Morton; +Cc: linux-kernel, Antonino A. Daplas

On 14/08/06, Andrew Morton <akpm@osdl.org> wrote:
> On Thu, 10 Aug 2006 15:22:21 +0100
> Catalin Marinas <catalin.marinas@arm.com> wrote:
> > diff --git a/drivers/char/vt.c b/drivers/char/vt.c
> > index da7e66a..31c8b32 100644
> > --- a/drivers/char/vt.c
> > +++ b/drivers/char/vt.c
> > @@ -730,7 +730,8 @@ int vc_allocate(unsigned int currcons)    /
> >           visual_init(vc, currcons, 1);
> >           if (!*vc->vc_uni_pagedir_loc)
> >               con_set_default_unimap(vc);
> > -         vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
> > +         if (!vc->vc_kmalloced)
> > +             vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
> >           if (!vc->vc_screenbuf) {
> >               kfree(vc);
> >               vc_cons[currcons].d = NULL;
>
> hm.  Maybe.  I'd worry that the memory at vc->vc_screenbuf isn't of the
> correct size and this patch will convert a leak into a buffer overrun.

My initial attempt was to free the kmalloc'ed buffer and reallocate it
in vc_allocate (similar to the code in vc_resize) but I realised that
vc_screenbuf kmalloced block is always of the vs_screenbuf_size
length.

> Also, what's up with this, in vc_resize()?
>
>         if (vc->vc_kmalloced)
>                 kfree(vc->vc_screenbuf);
>         vc->vc_screenbuf = newscreen;
>         vc->vc_kmalloced = 1;
>
> if vc->vc_kmalloced means "there is kmalloced memory at vc->vc_screenbuf"
> then this is wrong.

I think the above should be fine because vc_resize can be called
either via vc_allocate->visual_init->fbcon_init when the buffer was
not yet allocated or via a tty ioctl etc. and it needs to free the old
buffer.

-- 
Catalin

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

end of thread, other threads:[~2006-08-15  8:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-10 14:22 [PATCH] Fix memory leak in vc_resize/vc_allocate Catalin Marinas
2006-08-14 22:43 ` Andrew Morton
2006-08-14 23:44   ` Antonino A. Daplas
2006-08-15  8:16   ` Catalin Marinas

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome