Skip to content

Commit

Permalink
[NET]: Fix race condition in sk_stream_wait_connect
Browse files Browse the repository at this point in the history
When sk_stream_wait_connect detects a state transition to ESTABLISHED
or CLOSE_WAIT prior to it going to sleep, it will return without
calling finish_wait and decrementing sk_write_pending.

This may result in crashes and other unintended behaviour.

The fix is to always call finish_wait and update sk_write_pending since
it is safe to do so even if the wait entry is no longer on the queue.

This bug was tracked down with the help of Alex Sidorenko and the
fix is also based on his suggestion.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Arnaldo Carvalho de Melo <acme@mandriva.com>
  • Loading branch information
Herbert Xu authored and Arnaldo Carvalho de Melo committed Nov 5, 2005
1 parent eb229c4 commit 6151b31
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions net/core/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ int sk_stream_wait_connect(struct sock *sk, long *timeo_p)
{
struct task_struct *tsk = current;
DEFINE_WAIT(wait);
int done;

while (1) {
do {
if (sk->sk_err)
return sock_error(sk);
if ((1 << sk->sk_state) & ~(TCPF_SYN_SENT | TCPF_SYN_RECV))
Expand All @@ -65,13 +66,12 @@ int sk_stream_wait_connect(struct sock *sk, long *timeo_p)

prepare_to_wait(sk->sk_sleep, &wait, TASK_INTERRUPTIBLE);
sk->sk_write_pending++;
if (sk_wait_event(sk, timeo_p,
!((1 << sk->sk_state) &
~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT))))
break;
done = sk_wait_event(sk, timeo_p,
!((1 << sk->sk_state) &
~(TCPF_ESTABLISHED | TCPF_CLOSE_WAIT)));
finish_wait(sk->sk_sleep, &wait);
sk->sk_write_pending--;
}
} while (!done);
return 0;
}

Expand Down

0 comments on commit 6151b31

Please sign in to comment.