linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] netfilter: ebtables: compat: fix a memory leak bug
@ 2019-07-20 12:22 Wenwen Wang
  2019-07-21  0:26 ` Florian Westphal
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Wenwen Wang @ 2019-07-20 12:22 UTC (permalink / raw)
  To: Wenwen Wang
  Cc: Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	Roopa Prabhu, Nikolay Aleksandrov, David S. Miller,
	open list:NETFILTER, open list:NETFILTER,
	moderated list:ETHERNET BRIDGE, open list:ETHERNET BRIDGE,
	open list

From: Wenwen Wang <wenwen@cs.uga.edu>

In compat_do_replace(), a temporary buffer is allocated through vmalloc()
to hold entries copied from the user space. The buffer address is firstly
saved to 'newinfo->entries', and later on assigned to 'entries_tmp'. Then
the entries in this temporary buffer is copied to the internal kernel
structure through compat_copy_entries(). If this copy process fails,
compat_do_replace() should be terminated. However, the allocated temporary
buffer is not freed on this path, leading to a memory leak.

To fix the bug, free the buffer before returning from compat_do_replace().

Signed-off-by: Wenwen Wang <wenwen@cs.uga.edu>
---
 net/bridge/netfilter/ebtables.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
index 963dfdc..fd84b48e 100644
--- a/net/bridge/netfilter/ebtables.c
+++ b/net/bridge/netfilter/ebtables.c
@@ -2261,8 +2261,10 @@ static int compat_do_replace(struct net *net, void __user *user,
 	state.buf_kern_len = size64;
 
 	ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
-	if (WARN_ON(ret < 0))
+	if (WARN_ON(ret < 0)) {
+		vfree(entries_tmp);
 		goto out_unlock;
+	}
 
 	vfree(entries_tmp);
 	tmp.entries_size = size64;
-- 
2.7.4


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

* Re: [PATCH] netfilter: ebtables: compat: fix a memory leak bug
  2019-07-20 12:22 [PATCH] netfilter: ebtables: compat: fix a memory leak bug Wenwen Wang
@ 2019-07-21  0:26 ` Florian Westphal
  2019-07-21 18:59 ` Pablo Neira Ayuso
  2019-07-22 12:32 ` Liam R. Howlett
  2 siblings, 0 replies; 5+ messages in thread
From: Florian Westphal @ 2019-07-21  0:26 UTC (permalink / raw)
  To: Wenwen Wang
  Cc: Wenwen Wang, Pablo Neira Ayuso, Jozsef Kadlecsik,
	Florian Westphal, Roopa Prabhu, Nikolay Aleksandrov,
	David S. Miller, open list:NETFILTER, open list:NETFILTER,
	moderated list:ETHERNET BRIDGE, open list:ETHERNET BRIDGE,
	open list

Wenwen Wang <wang6495@umn.edu> wrote:
> From: Wenwen Wang <wenwen@cs.uga.edu>
> 
> In compat_do_replace(), a temporary buffer is allocated through vmalloc()
> to hold entries copied from the user space. The buffer address is firstly
> saved to 'newinfo->entries', and later on assigned to 'entries_tmp'. Then
> the entries in this temporary buffer is copied to the internal kernel
> structure through compat_copy_entries(). If this copy process fails,
> compat_do_replace() should be terminated. However, the allocated temporary
> buffer is not freed on this path, leading to a memory leak.
> 
> To fix the bug, free the buffer before returning from compat_do_replace().
> 
> Signed-off-by: Wenwen Wang <wenwen@cs.uga.edu>

Reviewed-by: Florian Westphal <fw@strlen.de>

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

* Re: [PATCH] netfilter: ebtables: compat: fix a memory leak bug
  2019-07-20 12:22 [PATCH] netfilter: ebtables: compat: fix a memory leak bug Wenwen Wang
  2019-07-21  0:26 ` Florian Westphal
@ 2019-07-21 18:59 ` Pablo Neira Ayuso
  2019-07-22 12:32 ` Liam R. Howlett
  2 siblings, 0 replies; 5+ messages in thread
From: Pablo Neira Ayuso @ 2019-07-21 18:59 UTC (permalink / raw)
  To: Wenwen Wang
  Cc: Wenwen Wang, Jozsef Kadlecsik, Florian Westphal, Roopa Prabhu,
	Nikolay Aleksandrov, David S. Miller, open list:NETFILTER,
	open list:NETFILTER, moderated list:ETHERNET BRIDGE,
	open list:ETHERNET BRIDGE, open list

On Sat, Jul 20, 2019 at 07:22:45AM -0500, Wenwen Wang wrote:
> From: Wenwen Wang <wenwen@cs.uga.edu>
> 
> In compat_do_replace(), a temporary buffer is allocated through vmalloc()
> to hold entries copied from the user space. The buffer address is firstly
> saved to 'newinfo->entries', and later on assigned to 'entries_tmp'. Then
> the entries in this temporary buffer is copied to the internal kernel
> structure through compat_copy_entries(). If this copy process fails,
> compat_do_replace() should be terminated. However, the allocated temporary
> buffer is not freed on this path, leading to a memory leak.
> 
> To fix the bug, free the buffer before returning from compat_do_replace().

Applied, thanks.

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

* Re: [PATCH] netfilter: ebtables: compat: fix a memory leak bug
  2019-07-20 12:22 [PATCH] netfilter: ebtables: compat: fix a memory leak bug Wenwen Wang
  2019-07-21  0:26 ` Florian Westphal
  2019-07-21 18:59 ` Pablo Neira Ayuso
@ 2019-07-22 12:32 ` Liam R. Howlett
  2019-07-22 12:53   ` Wenwen Wang
  2 siblings, 1 reply; 5+ messages in thread
From: Liam R. Howlett @ 2019-07-22 12:32 UTC (permalink / raw)
  To: Wenwen Wang
  Cc: Wenwen Wang, Pablo Neira Ayuso, Jozsef Kadlecsik,
	Florian Westphal, Roopa Prabhu, Nikolay Aleksandrov,
	David S. Miller, open list:NETFILTER, open list:NETFILTER,
	moderated list:ETHERNET BRIDGE, open list:ETHERNET BRIDGE,
	open list

Nice catch.  The code that exists is confusing due to newinfo->entries
being overwritten and then freed in the existing code path as you state
in your commit log.

* Wenwen Wang <wang6495@umn.edu> [190720 08:23]:
> From: Wenwen Wang <wenwen@cs.uga.edu>
> 
> In compat_do_replace(), a temporary buffer is allocated through vmalloc()
> to hold entries copied from the user space. The buffer address is firstly
> saved to 'newinfo->entries', and later on assigned to 'entries_tmp'. Then
> the entries in this temporary buffer is copied to the internal kernel
> structure through compat_copy_entries(). If this copy process fails,
> compat_do_replace() should be terminated. However, the allocated temporary
> buffer is not freed on this path, leading to a memory leak.
> 
> To fix the bug, free the buffer before returning from compat_do_replace().
> 
> Signed-off-by: Wenwen Wang <wenwen@cs.uga.edu>
> ---
>  net/bridge/netfilter/ebtables.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
> index 963dfdc..fd84b48e 100644
> --- a/net/bridge/netfilter/ebtables.c
> +++ b/net/bridge/netfilter/ebtables.c
> @@ -2261,8 +2261,10 @@ static int compat_do_replace(struct net *net, void __user *user,
>  	state.buf_kern_len = size64;
>  
>  	ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
> -	if (WARN_ON(ret < 0))
> +	if (WARN_ON(ret < 0)) {
> +		vfree(entries_tmp);
>  		goto out_unlock;
> +	}


Would it be worth adding a new goto label above out_unlock and free this
entries_tmp?  It could then be used in previous failure path as well.

Thanks,
Liam

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

* Re: [PATCH] netfilter: ebtables: compat: fix a memory leak bug
  2019-07-22 12:32 ` Liam R. Howlett
@ 2019-07-22 12:53   ` Wenwen Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Wenwen Wang @ 2019-07-22 12:53 UTC (permalink / raw)
  To: Liam R. Howlett
  Cc: Pablo Neira Ayuso, Jozsef Kadlecsik, Florian Westphal,
	Roopa Prabhu, Nikolay Aleksandrov, David S. Miller,
	open list:NETFILTER, open list:NETFILTER,
	moderated list:ETHERNET BRIDGE, open list:ETHERNET BRIDGE,
	open list, Wenwen Wang

On Mon, Jul 22, 2019 at 7:34 AM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
>
> Nice catch.  The code that exists is confusing due to newinfo->entries
> being overwritten and then freed in the existing code path as you state
> in your commit log.
>
> * Wenwen Wang <wang6495@umn.edu> [190720 08:23]:
> > From: Wenwen Wang <wenwen@cs.uga.edu>
> >
> > In compat_do_replace(), a temporary buffer is allocated through vmalloc()
> > to hold entries copied from the user space. The buffer address is firstly
> > saved to 'newinfo->entries', and later on assigned to 'entries_tmp'. Then
> > the entries in this temporary buffer is copied to the internal kernel
> > structure through compat_copy_entries(). If this copy process fails,
> > compat_do_replace() should be terminated. However, the allocated temporary
> > buffer is not freed on this path, leading to a memory leak.
> >
> > To fix the bug, free the buffer before returning from compat_do_replace().
> >
> > Signed-off-by: Wenwen Wang <wenwen@cs.uga.edu>
> > ---
> >  net/bridge/netfilter/ebtables.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/net/bridge/netfilter/ebtables.c b/net/bridge/netfilter/ebtables.c
> > index 963dfdc..fd84b48e 100644
> > --- a/net/bridge/netfilter/ebtables.c
> > +++ b/net/bridge/netfilter/ebtables.c
> > @@ -2261,8 +2261,10 @@ static int compat_do_replace(struct net *net, void __user *user,
> >       state.buf_kern_len = size64;
> >
> >       ret = compat_copy_entries(entries_tmp, tmp.entries_size, &state);
> > -     if (WARN_ON(ret < 0))
> > +     if (WARN_ON(ret < 0)) {
> > +             vfree(entries_tmp);
> >               goto out_unlock;
> > +     }
>
>
> Would it be worth adding a new goto label above out_unlock and free this
> entries_tmp?  It could then be used in previous failure path as well.

Yes, that would make the code much clearer and easier to understand.

Thanks!
Wenwen

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

end of thread, other threads:[~2019-07-22 12:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-20 12:22 [PATCH] netfilter: ebtables: compat: fix a memory leak bug Wenwen Wang
2019-07-21  0:26 ` Florian Westphal
2019-07-21 18:59 ` Pablo Neira Ayuso
2019-07-22 12:32 ` Liam R. Howlett
2019-07-22 12:53   ` Wenwen Wang

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