linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH linux-next] lockd: nlmsvc_mark_resources(): avoid stack overflow
@ 2013-02-12 19:48 Tim Gardner
  2013-02-12 21:22 ` J. Bruce Fields
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Gardner @ 2013-02-12 19:48 UTC (permalink / raw)
  To: linux-kernel; +Cc: Tim Gardner, Trond Myklebust, J. Bruce Fields, linux-nfs

Dynamically allocate the NLM host structure in order to avoid stack overflow.
nlmsvc_mark_resources() is several call levels deep in a stack
that has a number of large variables. 512 bytes seems like a lot
on the stack at this point.

smatch analysis:

fs/lockd/svcsubs.c:366 nlmsvc_mark_resources() warn: 'hint' puts
 512 bytes on stack

Cc: Trond Myklebust <Trond.Myklebust@netapp.com>
Cc: "J. Bruce Fields" <bfields@fieldses.org>
Cc: linux-nfs@vger.kernel.org
Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
---
 fs/lockd/svcsubs.c |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/fs/lockd/svcsubs.c b/fs/lockd/svcsubs.c
index b904f41..f3abb7f 100644
--- a/fs/lockd/svcsubs.c
+++ b/fs/lockd/svcsubs.c
@@ -363,11 +363,15 @@ nlmsvc_is_client(void *data, struct nlm_host *dummy)
 void
 nlmsvc_mark_resources(struct net *net)
 {
-	struct nlm_host hint;
+	struct nlm_host *hint = kzalloc(sizeof(*hint), GFP_KERNEL);
 
-	dprintk("lockd: nlmsvc_mark_resources for net %p\n", net);
-	hint.net = net;
-	nlm_traverse_files(&hint, nlmsvc_mark_host, NULL);
+	if (hint) {
+		dprintk("lockd: nlmsvc_mark_resources for net %p\n", net);
+		hint->net = net;
+		nlm_traverse_files(hint, nlmsvc_mark_host, NULL);
+	}
+
+	kfree(hint);
 }
 
 /*
-- 
1.7.9.5


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

* Re: [PATCH linux-next] lockd: nlmsvc_mark_resources(): avoid stack overflow
  2013-02-12 19:48 [PATCH linux-next] lockd: nlmsvc_mark_resources(): avoid stack overflow Tim Gardner
@ 2013-02-12 21:22 ` J. Bruce Fields
  2013-02-13 14:40   ` Tim Gardner
  0 siblings, 1 reply; 3+ messages in thread
From: J. Bruce Fields @ 2013-02-12 21:22 UTC (permalink / raw)
  To: Tim Gardner; +Cc: linux-kernel, Trond Myklebust, linux-nfs

On Tue, Feb 12, 2013 at 12:48:58PM -0700, Tim Gardner wrote:
> Dynamically allocate the NLM host structure in order to avoid stack overflow.
> nlmsvc_mark_resources() is several call levels deep in a stack
> that has a number of large variables. 512 bytes seems like a lot
> on the stack at this point.
> 
> smatch analysis:
> 
> fs/lockd/svcsubs.c:366 nlmsvc_mark_resources() warn: 'hint' puts
>  512 bytes on stack
> 
> Cc: Trond Myklebust <Trond.Myklebust@netapp.com>
> Cc: "J. Bruce Fields" <bfields@fieldses.org>
> Cc: linux-nfs@vger.kernel.org
> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
> ---
>  fs/lockd/svcsubs.c |   12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/lockd/svcsubs.c b/fs/lockd/svcsubs.c
> index b904f41..f3abb7f 100644
> --- a/fs/lockd/svcsubs.c
> +++ b/fs/lockd/svcsubs.c
> @@ -363,11 +363,15 @@ nlmsvc_is_client(void *data, struct nlm_host *dummy)
>  void
>  nlmsvc_mark_resources(struct net *net)
>  {
> -	struct nlm_host hint;
> +	struct nlm_host *hint = kzalloc(sizeof(*hint), GFP_KERNEL);
>  
> -	dprintk("lockd: nlmsvc_mark_resources for net %p\n", net);
> -	hint.net = net;
> -	nlm_traverse_files(&hint, nlmsvc_mark_host, NULL);
> +	if (hint) {
> +		dprintk("lockd: nlmsvc_mark_resources for net %p\n", net);
> +		hint->net = net;
> +		nlm_traverse_files(hint, nlmsvc_mark_host, NULL);
> +	}

Silently neglecting to do this looks like a bad idea.

It's strange that we're passing in an nlm_host when all we actually use
is the struct net*.  Why not just change this to pass in the net
instead?

--b.

> +
> +	kfree(hint);
>  }
>  
>  /*
> -- 
> 1.7.9.5
> 

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

* Re: [PATCH linux-next] lockd: nlmsvc_mark_resources(): avoid stack overflow
  2013-02-12 21:22 ` J. Bruce Fields
@ 2013-02-13 14:40   ` Tim Gardner
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Gardner @ 2013-02-13 14:40 UTC (permalink / raw)
  To: J. Bruce Fields; +Cc: linux-kernel, Trond Myklebust, linux-nfs

On 02/12/2013 02:22 PM, J. Bruce Fields wrote:
> On Tue, Feb 12, 2013 at 12:48:58PM -0700, Tim Gardner wrote:
>> Dynamically allocate the NLM host structure in order to avoid stack overflow.
>> nlmsvc_mark_resources() is several call levels deep in a stack
>> that has a number of large variables. 512 bytes seems like a lot
>> on the stack at this point.
>>
>> smatch analysis:
>>
>> fs/lockd/svcsubs.c:366 nlmsvc_mark_resources() warn: 'hint' puts
>>  512 bytes on stack
>>
>> Cc: Trond Myklebust <Trond.Myklebust@netapp.com>
>> Cc: "J. Bruce Fields" <bfields@fieldses.org>
>> Cc: linux-nfs@vger.kernel.org
>> Signed-off-by: Tim Gardner <tim.gardner@canonical.com>
>> ---
>>  fs/lockd/svcsubs.c |   12 ++++++++----
>>  1 file changed, 8 insertions(+), 4 deletions(-)
>>
>> diff --git a/fs/lockd/svcsubs.c b/fs/lockd/svcsubs.c
>> index b904f41..f3abb7f 100644
>> --- a/fs/lockd/svcsubs.c
>> +++ b/fs/lockd/svcsubs.c
>> @@ -363,11 +363,15 @@ nlmsvc_is_client(void *data, struct nlm_host *dummy)
>>  void
>>  nlmsvc_mark_resources(struct net *net)
>>  {
>> -	struct nlm_host hint;
>> +	struct nlm_host *hint = kzalloc(sizeof(*hint), GFP_KERNEL);
>>  
>> -	dprintk("lockd: nlmsvc_mark_resources for net %p\n", net);
>> -	hint.net = net;
>> -	nlm_traverse_files(&hint, nlmsvc_mark_host, NULL);
>> +	if (hint) {
>> +		dprintk("lockd: nlmsvc_mark_resources for net %p\n", net);
>> +		hint->net = net;
>> +		nlm_traverse_files(hint, nlmsvc_mark_host, NULL);
>> +	}
> 
> Silently neglecting to do this looks like a bad idea.
> 
> It's strange that we're passing in an nlm_host when all we actually use
> is the struct net*.  Why not just change this to pass in the net
> instead?
> 
> --b.
> 

It won't really be silent. k[zm]alloc() dumps a stack trace on failure
to allocate unless GFP_NOWARN is set in the flags. I think this is a bit
better then possibly corrupting the stack.

Changing the prototype to just pass in 'net' has knock on effects that
make this patch a whole lot bigger. You'd have to change the code within
a bunch of functions which are difficult to verify at compile time
because of the use of 'void *data' as the first parameter. Is there
still a good reason for that parameter to be opaque ?

rtg
-- 
Tim Gardner tim.gardner@canonical.com

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

end of thread, other threads:[~2013-02-13 14:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-12 19:48 [PATCH linux-next] lockd: nlmsvc_mark_resources(): avoid stack overflow Tim Gardner
2013-02-12 21:22 ` J. Bruce Fields
2013-02-13 14:40   ` Tim Gardner

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