Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Oct 2002 22:31:15 -0300 (ART)
From:      Fernando Gleiser <fgleiser@cactus.fi.uba.ar>
To:        Richard Tobin <richard@cogsci.ed.ac.uk>
Cc:        questions@FreeBSD.ORG
Subject:   Re: How i can force a stream socket to wait as limited  time inaccept() function?
Message-ID:  <20021008222316.E3949-100000@cactus.fi.uba.ar>
In-Reply-To: <200210090010.BAA23132@sorley.cogsci.ed.ac.uk>

next in thread | previous in thread | raw e-mail | index | archive | help
On Wed, 9 Oct 2002, Richard Tobin wrote:

>
> You don't need to put the socket in non-blocking mode to select for
> accept.  See example program below.

You're right, you don't *have to*, but it is better if you do it.
If you don't, you can have a DoS if the following happens:

1. The attacker connects to the server
2. The attacker resets the connection (calling shutdown(2))
3. If the rst arrives after the select but *before* the accept, the server
   blocks in the call to accept until another connection arrives.

This actual bug afected some versions of inetd a couple of years ago.

If the socket is set to nonblocking mode, accept fails (EWOULDBLOCK) and
the loop starts again. See Stevens (UNP, 2d E, Vol 1, page 422) for details.


			Fer

>
> -- Richard
>
> #include <stdio.h>
> #include <string.h>
> #include <unistd.h>
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <sys/time.h>
> #include <netinet/in.h>
>
>
> int main(int argc, char **argv)
> {
>     static struct sockaddr_in addr;
>     int s;
>     fd_set fds;
>     struct timeval t = {5, 0};
>
>     s = socket(PF_INET, SOCK_STREAM, 0);
>     addr.sin_family = AF_INET;
>     addr.sin_port = htons(atoi(argv[1]));
>     if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
>     {
>         perror("bind");
>         return 1;
>     }
>     listen(s, 5);
>     FD_ZERO(&fds);
>     FD_SET(s, &fds);
>     switch(select(s+1, &fds, 0, 0, &t))
>     {
>       case 0:
>         printf("timed out\n");
>         return 0;
>       case -1:
>         perror("select");
>         return 1;
>       default:
>         printf("select returned\n");
>         printf("accept returned %d\n", accept(s, 0, 0));
>         return 0;
>     }
> }
>


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20021008222316.E3949-100000>