linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* fs/exec.c: fix minor memory leak
@ 2016-04-21 21:15 Andrew Morton
  2016-05-16 14:57 ` Vlastimil Babka
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2016-04-21 21:15 UTC (permalink / raw)
  To: linux-mm


Could someone please double-check this?


From: Andrew Morton <akpm@linux-foundation.org>
Subject: fs/exec.c: fix minor memory leak

When the to-be-removed argument's trailing '\0' is the final byte in the
page, remove_arg_zero()'s logic will avoid freeing the page, will break
from the loop and will then advance bprm->p to point at the first byte in
the next page.  Net result: the final page for the zeroeth argument is
unfreed.

It isn't a very important leak - that page will be freed later by the
bprm-wide sweep in free_arg_pages().

Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=116841
Reported by: hujunjie <jj.net@163.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 fs/exec.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff -puN fs/exec.c~fs-execc-fix-minor-memory-leak fs/exec.c
--- a/fs/exec.c~fs-execc-fix-minor-memory-leak
+++ a/fs/exec.c
@@ -1482,8 +1482,15 @@ int remove_arg_zero(struct linux_binprm
 		kunmap_atomic(kaddr);
 		put_arg_page(page);
 
-		if (offset == PAGE_SIZE)
+		if (offset == PAGE_SIZE) {
 			free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
+		} else if (offset == PAGE_SIZE - 1) {
+			/*
+			 * The trailing '\0' is the last byte in a page - we're
+			 * about to advance past that byte so free its page now
+			 */
+			free_arg_page(bprm, (bprm->p >> PAGE_SHIFT));
+		}
 	} while (offset == PAGE_SIZE);
 
 	bprm->p++;
_

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: fs/exec.c: fix minor memory leak
  2016-04-21 21:15 fs/exec.c: fix minor memory leak Andrew Morton
@ 2016-05-16 14:57 ` Vlastimil Babka
  0 siblings, 0 replies; 5+ messages in thread
From: Vlastimil Babka @ 2016-05-16 14:57 UTC (permalink / raw)
  To: Andrew Morton, linux-mm

On 04/21/2016 11:15 PM, Andrew Morton wrote:
>
> Could someone please double-check this?

Looks OK to me.

> From: Andrew Morton <akpm@linux-foundation.org>
> Subject: fs/exec.c: fix minor memory leak
>
> When the to-be-removed argument's trailing '\0' is the final byte in the
> page, remove_arg_zero()'s logic will avoid freeing the page, will break
> from the loop and will then advance bprm->p to point at the first byte in
> the next page.  Net result: the final page for the zeroeth argument is
> unfreed.
>
> It isn't a very important leak - that page will be freed later by the
> bprm-wide sweep in free_arg_pages().
>
> Fixes: https://bugzilla.kernel.org/show_bug.cgi?id=116841
> Reported by: hujunjie <jj.net@163.com>
> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

Acked-by: Vlastimil Babka <vbabka@suse.cz>

> ---
>
>   fs/exec.c |    9 ++++++++-
>   1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff -puN fs/exec.c~fs-execc-fix-minor-memory-leak fs/exec.c
> --- a/fs/exec.c~fs-execc-fix-minor-memory-leak
> +++ a/fs/exec.c
> @@ -1482,8 +1482,15 @@ int remove_arg_zero(struct linux_binprm
>   		kunmap_atomic(kaddr);
>   		put_arg_page(page);
>
> -		if (offset == PAGE_SIZE)
> +		if (offset == PAGE_SIZE) {
>   			free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
> +		} else if (offset == PAGE_SIZE - 1) {
> +			/*
> +			 * The trailing '\0' is the last byte in a page - we're
> +			 * about to advance past that byte so free its page now
> +			 */
> +			free_arg_page(bprm, (bprm->p >> PAGE_SHIFT));
> +		}
>   	} while (offset == PAGE_SIZE);
>
>   	bprm->p++;
> _
>
> --
> To unsubscribe, send a message with 'unsubscribe linux-mm' in
> the body to majordomo@kvack.org.  For more info on Linux MM,
> see: http://www.linux-mm.org/ .
> Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: fs/exec.c: fix minor memory leak
@ 2016-05-16 20:43 Oleg Nesterov
  2016-05-16 20:55 ` Andrew Morton
  0 siblings, 1 reply; 5+ messages in thread
From: Oleg Nesterov @ 2016-05-16 20:43 UTC (permalink / raw)
  To: Andrew Morton, Vlastimil Babka, hujunjie; +Cc: linux-mm

Andrew, Vlastimil,

I found this patch by accident when I was looking at http://marc.info/?l=linux-mm
and I can't resist ;)

> On 04/21/2016 11:15 PM, Andrew Morton wrote:
> >
> > Could someone please double-check this?
>
> Looks OK to me.
>
> > From: Andrew Morton <akpm@linux-foundation.org>
> > Subject: fs/exec.c: fix minor memory leak
> >
> > When the to-be-removed argument's trailing '\0' is the final byte in the
> > page, remove_arg_zero()'s logic will avoid freeing the page, will break
> > from the loop and will then advance bprm->p to point at the first byte in
> > the next page.  Net result: the final page for the zeroeth argument is
> > unfreed.
> >
> > It isn't a very important leak - that page will be freed later by the
> > bprm-wide sweep in free_arg_pages().

And so I think we should just remove this free_arg_page(), it (and the patch)
only adds the unnecessary confusion.

Note that today free_arg_page() is nop if CONFIG_MMU. At the same time, the
only reason for this free_arg_page() was that (until the commit b6a2fea39)
CONFIG_MMU did install_arg_page() for every page != NULL in bprm->page[].

So we simply do not need it today. And note that the caller is going to do
copy_strings_kernel(), so if we do free_arg_page() with CONFIG_MMU=n we will
likely have to re-allocate this page right after free.

And note that this code is actually wrong! remove_arg_zero() assumes that
argv[0] is null-terminated but this is not necessarily true. copy_strings()
does:

	len = strnlen_user(...);
	...
	copy_from_user(..., len);

another thread or debugger can change the memory in between. Fortunately
nothing really bad can happen (afaics) even if CONFIG_MMU=n, bprm->filename
must be always zero-terminated and it was copied by the 1st copy_strings_kernel().
Still perhaps it makes sense to check "bprm->p < bprm->exec" in the main loop.

Oleg.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* Re: fs/exec.c: fix minor memory leak
  2016-05-16 20:43 fs/exec.c: fix minor memory leak Oleg Nesterov
@ 2016-05-16 20:55 ` Andrew Morton
  2016-05-17 15:53   ` [PATCH] exec: remove the no longer needed remove_arg_zero()->free_arg_page() Oleg Nesterov
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Morton @ 2016-05-16 20:55 UTC (permalink / raw)
  To: Oleg Nesterov; +Cc: Vlastimil Babka, hujunjie, linux-mm

On Mon, 16 May 2016 22:43:39 +0200 Oleg Nesterov <oleg@redhat.com> wrote:

> Andrew, Vlastimil,
> 
> I found this patch by accident when I was looking at http://marc.info/?l=linux-mm
> and I can't resist ;)
> 
> > On 04/21/2016 11:15 PM, Andrew Morton wrote:
> > >
> > > Could someone please double-check this?
> >
> > Looks OK to me.
> >
> > > From: Andrew Morton <akpm@linux-foundation.org>
> > > Subject: fs/exec.c: fix minor memory leak
> > >
> > > When the to-be-removed argument's trailing '\0' is the final byte in the
> > > page, remove_arg_zero()'s logic will avoid freeing the page, will break
> > > from the loop and will then advance bprm->p to point at the first byte in
> > > the next page.  Net result: the final page for the zeroeth argument is
> > > unfreed.
> > >
> > > It isn't a very important leak - that page will be freed later by the
> > > bprm-wide sweep in free_arg_pages().
> 
> And so I think we should just remove this free_arg_page(), it (and the patch)
> only adds the unnecessary confusion.
> 

Send patch :)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

* [PATCH] exec: remove the no longer needed remove_arg_zero()->free_arg_page()
  2016-05-16 20:55 ` Andrew Morton
@ 2016-05-17 15:53   ` Oleg Nesterov
  0 siblings, 0 replies; 5+ messages in thread
From: Oleg Nesterov @ 2016-05-17 15:53 UTC (permalink / raw)
  To: Andrew Morton; +Cc: Vlastimil Babka, hujunjie, linux-mm, linux-kernel

remove_arg_zero() does free_arg_page() for no reason. This was needed
before and only if CONFIG_MMU=y: see the commit 4fc75ff4 ("exec: fix
remove_arg_zero"), install_arg_page() was called for every page != NULL
in bprm->page[] array. Today install_arg_page() has already gone and
free_arg_page() is nop after another commit b6a2fea39 ("mm: variable
length argument support").

CONFIG_MMU=n does free_arg_pages() in free_bprm() and thus it doesn't
need remove_arg_zero()->free_arg_page() too; apart from get_arg_page()
it never checks if the page in bprm->page[] was allocated or not, so
the "extra" non-freed page is fine. OTOH, this free_arg_page() can add
the minor pessimization, the caller is going to do copy_strings_kernel()
right after remove_arg_zero() which will likely need to re-allocate the
same page again.

And as Hujunjie pointed out, the "offset == PAGE_SIZE" check is wrong
because we are going to increment bprm->p once again before return, so
CONFIG_MMU=n "leaks" the page anyway if '\0' is the final byte in this
page.

NOTE: remove_arg_zero() assumes that argv[0] is null-terminated but this
is not necessarily true. copy_strings() does "len = strnlen_user(...)",
then copy_from_user(len) but another thread or debuger can overwrite the
trailing '\0' in between. Afaics nothing really bad can happen because
we must always have the null-terminated bprm->filename copied by the 1st
copy_strings_kernel(), but perhaps we should change this code to check
"bprm->p < bprm->exec" anyway, and/or change copy_strings() to ensure
that the last byte in string is always zero.

Reported by: hujunjie <jj.net@163.com>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 fs/exec.c | 7 -------
 1 file changed, 7 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index c4010b8..9b85c4d 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -243,10 +243,6 @@ static void put_arg_page(struct page *page)
 	put_page(page);
 }
 
-static void free_arg_page(struct linux_binprm *bprm, int i)
-{
-}
-
 static void free_arg_pages(struct linux_binprm *bprm)
 {
 }
@@ -1481,9 +1477,6 @@ int remove_arg_zero(struct linux_binprm *bprm)
 
 		kunmap_atomic(kaddr);
 		put_arg_page(page);
-
-		if (offset == PAGE_SIZE)
-			free_arg_page(bprm, (bprm->p >> PAGE_SHIFT) - 1);
 	} while (offset == PAGE_SIZE);
 
 	bprm->p++;
-- 
2.5.0


--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org.  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>

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

end of thread, other threads:[~2016-05-17 15:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-05-16 20:43 fs/exec.c: fix minor memory leak Oleg Nesterov
2016-05-16 20:55 ` Andrew Morton
2016-05-17 15:53   ` [PATCH] exec: remove the no longer needed remove_arg_zero()->free_arg_page() Oleg Nesterov
  -- strict thread matches above, loose matches on Subject: below --
2016-04-21 21:15 fs/exec.c: fix minor memory leak Andrew Morton
2016-05-16 14:57 ` Vlastimil Babka

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