netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Neil Brown <neilb@suse.de>
To: Stephen Hemminger <shemminger@vyatta.com>
Cc: netdev@vger.kernel.org
Subject: Re: Undefined behaviour of connect(fd, NULL, 0);
Date: Thu, 1 Apr 2010 07:24:12 +1100	[thread overview]
Message-ID: <20100401072412.032aa8e6@notabene.brown> (raw)
In-Reply-To: <20100331114936.3549ca90@s6510>

On Wed, 31 Mar 2010 11:49:36 -0700
Stephen Hemminger <shemminger@vyatta.com> wrote:

> On Wed, 31 Mar 2010 22:36:37 +1100
> Neil Brown <neilb@suse.de> wrote:
> 
> > 
> > Hi Netdev.
> > 
> > We have a customer who was reporting strangely unpredictable behaviour of an
> > in-house application that used networking.
> > 
> > It called connect on a non-blocking socket and subsequently called
> >    connect(fd, NULL, 0)
> > 
> > to check if the connection had succeeded.
> > This would sometime "work" and sometimes close the connection.
> > 
> > Looking at the code (sys_connect, move_addr_to_kernel, inet_stream_connect),
> > it seems that in this case an uninitialised on-stack address is passed
> > to inet_stream_connect and it makes a decision based on ->sa_family (which is
> > uninitialised).
> > 
> > It seems clear that connect(fd, NULL, 0) is the wrong thing to do in this
> > circumstance, but I think it would be good if it failed consistently rather
> > than unpredictably.
> > 
> > Would it be appropriate for move_addr_to_kernel to zero out the remainder of
> > the address?
> >    memset(kaddr+ulen, 0, MAX_SOCK_ADDR-ulen);
> > ??
> > 
> > Then connect(fd, NULL, 0) would always break the connection.
> 
> I think the problem is inet_stream_connect referencing past addr_len.
> 
> --- a/net/ipv4/af_inet.c	2010-03-31 11:47:01.952910248 -0700
> +++ b/net/ipv4/af_inet.c	2010-03-31 11:48:09.852938406 -0700
> @@ -575,7 +575,7 @@ int inet_stream_connect(struct socket *s
>  
>  	lock_sock(sk);
>  
> -	if (uaddr->sa_family == AF_UNSPEC) {
> +	if (addr_len < sizeof(sa_family_t) || uaddr->sa_family == AF_UNSPEC) {
>  		err = sk->sk_prot->disconnect(sk, flags);
>  		sock->state = err ? SS_DISCONNECTING : SS_UNCONNECTED;
>  		goto out;

Thanks for the reply.

The implication of this patch is that
   connect(fd, NULL, 0)
is actually a valid way to check if an in-progress connection has completed.

Is that the intention?

Does all other address manipulation code check the addr_len ?? (probably).

Thanks,
NeilBrown

  reply	other threads:[~2010-03-31 20:24 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-31 11:36 Undefined behaviour of connect(fd, NULL, 0); Neil Brown
2010-03-31 18:49 ` Stephen Hemminger
2010-03-31 20:24   ` Neil Brown [this message]
2010-03-31 21:14     ` Stephen Hemminger
2010-03-31 21:17     ` David Miller
2010-03-31 22:07       ` Neil Brown
2010-04-01  3:00         ` Changli Gao
2010-04-01  3:38           ` Neil Brown
2010-04-01  4:16             ` Changli Gao
2010-04-01  5:50               ` Changli Gao
2010-04-01  7:23               ` David Miller
2010-04-05  9:23                 ` Changli Gao
2010-04-05 15:56                   ` Eric Dumazet
2010-04-05 16:25                     ` Andreas Schwab
2010-04-05 19:25                   ` David Miller

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20100401072412.032aa8e6@notabene.brown \
    --to=neilb@suse.de \
    --cc=netdev@vger.kernel.org \
    --cc=shemminger@vyatta.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).