Skip to contentSkip to conversation

inet: race in wait for connect.

PATCH In review Stephen Hemminger 3 participants 5 messages Oct 29, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 inet: race in wait for connect.

Fix possible race while waiting for connections in accept. I don't know of a test case that could reproduce this directly.

The state of the socket should be checked before checking the queue. If the socket has left the TCP_LISTEN state, then the accept queue is no longer valid.

net/ipv4/inet_connection_sock.c +3 -3
@@ -203,12 +203,12 @@ static int inet_csk_wait_for_connect(str
 		if (reqsk_queue_empty(&icsk->icsk_accept_queue))
 			timeo = schedule_timeout(timeo);
 		lock_sock(sk);
-		err = 0;
-		if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
-			break;
 		err = -EINVAL;
 		if (sk->sk_state != TCP_LISTEN)
 			break;
+		err = 0;
+		if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
+			break;
 		err = sock_intr_errno(timeo);
 		if (signal_pending(current))
 			break;
#1 lore raw

Em Mon, Oct 29, 2007 at 01:52:22PM -0700, Stephen Hemminger escreveu:

Quoted 6 lines

Fix possible race while waiting for connections in accept. I don't know of a test case that could reproduce this directly.

The state of the socket should be checked before checking the queue. If the socket has left the TCP_LISTEN state, then the accept queue is no longer valid.

Well if it left from LISTEN to CLOSED inet_csk_listen_stop must have been called, and that calls reqsk_queue_yank_acceptq, that sets it to NULL, reqsk_queue_empty(&icsk->icsk_accept_queue) returns true and we get to if (sk->sk_state != TCP_LISTEN), returning -EINVAL as with your patch. So I can't see a race, just one branch less in one case :-)

- Arnaldo

Quoted 20 lines
net/ipv4/inet_connection_sock.c +3 -3
@@ -203,12 +203,12 @@ static int inet_csk_wait_for_connect(str
 		if (reqsk_queue_empty(&icsk->icsk_accept_queue))
 			timeo = schedule_timeout(timeo);
 		lock_sock(sk);
-		err = 0;
-		if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
-			break;
 		err = -EINVAL;
 		if (sk->sk_state != TCP_LISTEN)
 			break;
+		err = 0;
+		if (!reqsk_queue_empty(&icsk->icsk_accept_queue))
+			break;
 		err = sock_intr_errno(timeo);
 		if (signal_pending(current))
 			break;
#2 lore raw
Quoted 8 lines · On Mon, 29 Oct 2007 19:29:06 -0200 "Arnaldo Carvalho de Melo"…

Em Mon, Oct 29, 2007 at 01:52:22PM -0700, Stephen Hemminger escreveu:

Quoted 6 lines

Fix possible race while waiting for connections in accept. I don't know of a test case that could reproduce this directly.

The state of the socket should be checked before checking the queue. If the socket has left the TCP_LISTEN state, then the accept queue is no longer valid.

Quoted 7 lines

Well if it left from LISTEN to CLOSED inet_csk_listen_stop must have been called, and that calls reqsk_queue_yank_acceptq, that sets it to NULL, reqsk_queue_empty(&icsk->icsk_accept_queue) returns true and we get to if (sk->sk_state != TCP_LISTEN), returning -EINVAL as with your patch. So I can't see a race, just one branch less in one case :-)

- Arnaldo

Yeah, your right. The listen queue is garbage at this point but the accept queue is always empty.

Signature · 1 line
Stephen Hemminger <[email protected]>
maintainer
#3 lore raw

From: Stephen Hemminger <[email protected]>
Date: Mon, 29 Oct 2007 14:37:14 -0700

Quoted 17 lines
Quoted 14 lines · On Mon, 29 Oct 2007 19:29:06 -0200 "Arnaldo Carvalho de Melo"…

Em Mon, Oct 29, 2007 at 01:52:22PM -0700, Stephen Hemminger escreveu:

Quoted 6 lines

Fix possible race while waiting for connections in accept. I don't know of a test case that could reproduce this directly.

The state of the socket should be checked before checking the queue. If the socket has left the TCP_LISTEN state, then the accept queue is no longer valid.

Well if it left from LISTEN to CLOSED inet_csk_listen_stop must have been called, and that calls reqsk_queue_yank_acceptq, that sets it to NULL, reqsk_queue_empty(&icsk->icsk_accept_queue) returns true and we get to if (sk->sk_state != TCP_LISTEN), returning -EINVAL as with your patch. So I can't see a race, just one branch less in one case :-)

Quoted 4 lines
Quoted 1 line

- Arnaldo

Yeah, your right. The listen queue is garbage at this point but the accept queue is always empty.

Is there some failing test case you are aware of which led you to this piece of code or did you happen to notice it while scanning around?

#4 lore raw
Quoted 20 lines · On Mon, 29 Oct 2007 15:38:42 -0700 (PDT) David Miller <[email protected]>…

From: Stephen Hemminger <[email protected]>
Date: Mon, 29 Oct 2007 14:37:14 -0700

Quoted 17 lines
Quoted 14 lines · On Mon, 29 Oct 2007 19:29:06 -0200 "Arnaldo Carvalho de Melo"…

Em Mon, Oct 29, 2007 at 01:52:22PM -0700, Stephen Hemminger escreveu:

Quoted 6 lines

Fix possible race while waiting for connections in accept. I don't know of a test case that could reproduce this directly.

The state of the socket should be checked before checking the queue. If the socket has left the TCP_LISTEN state, then the accept queue is no longer valid.

Well if it left from LISTEN to CLOSED inet_csk_listen_stop must have been called, and that calls reqsk_queue_yank_acceptq, that sets it to NULL, reqsk_queue_empty(&icsk->icsk_accept_queue) returns true and we get to if (sk->sk_state != TCP_LISTEN), returning -EINVAL as with your patch. So I can't see a race, just one branch less in one case :-)

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

- Arnaldo

Yeah, your right. The listen queue is garbage at this point but the accept queue is always empty.

Is there some failing test case you are aware of which led you to this piece of code or did you happen to notice it while scanning around?

While instrumenting the snot out of accept logic for looking at alternatives to the MT-accept vs. close issue.

Signature · 1 line
Stephen Hemminger <[email protected]>