netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] net: fix kernel_accept() error path
@ 2007-10-04 20:20 Tony Battersby
  2007-10-04 21:57 ` James Morris
  0 siblings, 1 reply; 5+ messages in thread
From: Tony Battersby @ 2007-10-04 20:20 UTC (permalink / raw)
  To: netdev, davem

If accept() returns an error, kernel_accept() releases the new socket
but passes a pointer to the released socket back to the caller.  Make it
pass back NULL instead.

Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
---
--- linux-2.6.23-rc9/net/socket.c.bak	2007-10-04 15:21:17.000000000 -0400
+++ linux-2.6.23-rc9/net/socket.c	2007-10-04 15:21:22.000000000 -0400
@@ -2230,6 +2230,7 @@ int kernel_accept(struct socket *sock, s
 	err = sock->ops->accept(sock, *newsock, flags);
 	if (err < 0) {
 		sock_release(*newsock);
+		*newsock = NULL;
 		goto done;
 	}
 



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

* Re: [PATCH] net: fix kernel_accept() error path
  2007-10-04 20:20 [PATCH] net: fix kernel_accept() error path Tony Battersby
@ 2007-10-04 21:57 ` James Morris
  2007-10-04 22:08   ` David Miller
  2007-10-04 22:12   ` Tony Battersby
  0 siblings, 2 replies; 5+ messages in thread
From: James Morris @ 2007-10-04 21:57 UTC (permalink / raw)
  To: Tony Battersby; +Cc: netdev, davem

On Thu, 4 Oct 2007, Tony Battersby wrote:

> If accept() returns an error, kernel_accept() releases the new socket
> but passes a pointer to the released socket back to the caller.  Make it
> pass back NULL instead.
> 
> Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
> ---
> --- linux-2.6.23-rc9/net/socket.c.bak	2007-10-04 15:21:17.000000000 -0400
> +++ linux-2.6.23-rc9/net/socket.c	2007-10-04 15:21:22.000000000 -0400
> @@ -2230,6 +2230,7 @@ int kernel_accept(struct socket *sock, s
>  	err = sock->ops->accept(sock, *newsock, flags);
>  	if (err < 0) {
>  		sock_release(*newsock);
> +		*newsock = NULL;
>  		goto done;
>  	}
>  

If you get an error back from kernel_accept, you should not be trying to 
use newsock.

-- 
James Morris
<jmorris@namei.org>

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

* Re: [PATCH] net: fix kernel_accept() error path
  2007-10-04 21:57 ` James Morris
@ 2007-10-04 22:08   ` David Miller
  2007-10-04 22:12   ` Tony Battersby
  1 sibling, 0 replies; 5+ messages in thread
From: David Miller @ 2007-10-04 22:08 UTC (permalink / raw)
  To: jmorris; +Cc: tonyb, netdev

From: James Morris <jmorris@namei.org>
Date: Thu, 4 Oct 2007 14:57:33 -0700 (PDT)

> On Thu, 4 Oct 2007, Tony Battersby wrote:
> 
> > If accept() returns an error, kernel_accept() releases the new socket
> > but passes a pointer to the released socket back to the caller.  Make it
> > pass back NULL instead.
> > 
> > Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
> > ---
> > --- linux-2.6.23-rc9/net/socket.c.bak	2007-10-04 15:21:17.000000000 -0400
> > +++ linux-2.6.23-rc9/net/socket.c	2007-10-04 15:21:22.000000000 -0400
> > @@ -2230,6 +2230,7 @@ int kernel_accept(struct socket *sock, s
> >  	err = sock->ops->accept(sock, *newsock, flags);
> >  	if (err < 0) {
> >  		sock_release(*newsock);
> > +		*newsock = NULL;
> >  		goto done;
> >  	}
> >  
> 
> If you get an error back from kernel_accept, you should not be trying to 
> use newsock.

Agreed, the caller should not try to deref the thing, it's
value is undefined.

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

* Re: [PATCH] net: fix kernel_accept() error path
  2007-10-04 21:57 ` James Morris
  2007-10-04 22:08   ` David Miller
@ 2007-10-04 22:12   ` Tony Battersby
  2007-10-04 23:55     ` David Miller
  1 sibling, 1 reply; 5+ messages in thread
From: Tony Battersby @ 2007-10-04 22:12 UTC (permalink / raw)
  To: James Morris; +Cc: netdev, davem

James Morris wrote:
> On Thu, 4 Oct 2007, Tony Battersby wrote:
>
>   
>> If accept() returns an error, kernel_accept() releases the new socket
>> but passes a pointer to the released socket back to the caller.  Make it
>> pass back NULL instead.
>>
>> Signed-off-by: Tony Battersby <tonyb@cybernetics.com>
>> ---
>> --- linux-2.6.23-rc9/net/socket.c.bak	2007-10-04 15:21:17.000000000 -0400
>> +++ linux-2.6.23-rc9/net/socket.c	2007-10-04 15:21:22.000000000 -0400
>> @@ -2230,6 +2230,7 @@ int kernel_accept(struct socket *sock, s
>>  	err = sock->ops->accept(sock, *newsock, flags);
>>  	if (err < 0) {
>>  		sock_release(*newsock);
>> +		*newsock = NULL;
>>  		goto done;
>>  	}
>>  
>>     
>
> If you get an error back from kernel_accept, you should not be trying to 
> use newsock.
>
>   

Here is an example of what I would consider "reasonable code" that would
fail:

int example()
{
    struct socket *conn_socket = NULL;
    int err;

    ...

    if ((err = kernel_accept(sock, &conn_socket, 0)) < 0)
        goto out_cleanup;

    [do whatever with conn_socket]

 out_cleanup:

    if (conn_socket != NULL)
        sock_release(&conn_socket);

    return err;
}

Without the patch, the double sock_release() will cause a BUG().

Also compare to sock_create_lite(), which sets *res to NULL on error.

Tony


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

* Re: [PATCH] net: fix kernel_accept() error path
  2007-10-04 22:12   ` Tony Battersby
@ 2007-10-04 23:55     ` David Miller
  0 siblings, 0 replies; 5+ messages in thread
From: David Miller @ 2007-10-04 23:55 UTC (permalink / raw)
  To: tonyb; +Cc: jmorris, netdev

From: Tony Battersby <tonyb@cybernetics.com>
Date: Thu, 04 Oct 2007 18:12:53 -0400

> Here is an example of what I would consider "reasonable code" that would
> fail:
> 
> int example()
> {
>     struct socket *conn_socket = NULL;
>     int err;
> 
>     ...
> 
>     if ((err = kernel_accept(sock, &conn_socket, 0)) < 0)
>         goto out_cleanup;
> 
>     [do whatever with conn_socket]
> 
>  out_cleanup:
> 
>     if (conn_socket != NULL)
>         sock_release(&conn_socket);
> 
>     return err;
> }

This is a grey area.

I'd say you shouldn't be trying to do cleanups on conn_socket unless
kernel_accept() gave you a success return.

However, kernel_accept() is guilty of leaving a stray pointer
in conn_socket, in fact a reference to freed memory.  So
from that perspective we should put your patch in.

Please resubmit, at least to me under seperate cover,thanks.

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

end of thread, other threads:[~2007-10-04 23:56 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-04 20:20 [PATCH] net: fix kernel_accept() error path Tony Battersby
2007-10-04 21:57 ` James Morris
2007-10-04 22:08   ` David Miller
2007-10-04 22:12   ` Tony Battersby
2007-10-04 23:55     ` David Miller

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