From owner-freebsd-questions Tue Oct 8 18:33:37 2002 Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 6D9D537B401 for ; Tue, 8 Oct 2002 18:33:33 -0700 (PDT) Received: from cactus.fi.uba.ar (cactus.fi.uba.ar [157.92.49.108]) by mx1.FreeBSD.org (Postfix) with ESMTP id 5E62E43E3B for ; Tue, 8 Oct 2002 18:33:32 -0700 (PDT) (envelope-from fgleiser@cactus.fi.uba.ar) Received: from cactus.fi.uba.ar (cactus.fi.uba.ar [157.92.49.108]) by cactus.fi.uba.ar (8.12.3/8.12.3) with ESMTP id g991VFAO020535; Tue, 8 Oct 2002 22:31:16 -0300 (ART) (envelope-from fgleiser@cactus.fi.uba.ar) Date: Tue, 8 Oct 2002 22:31:15 -0300 (ART) From: Fernando Gleiser To: Richard Tobin Cc: questions@FreeBSD.ORG Subject: Re: How i can force a stream socket to wait as limited time inaccept() function? In-Reply-To: <200210090010.BAA23132@sorley.cogsci.ed.ac.uk> Message-ID: <20021008222316.E3949-100000@cactus.fi.uba.ar> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Status: No, hits=-3.5 required=5.0 tests=IN_REP_TO,SUBJ_ENDS_IN_Q_MARK version=2.31 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG 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 > #include > #include > #include > #include > #include > #include > > > 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