Skip to content

Commit

Permalink
[AF_UNIX]: Fix stream recvmsg() race.
Browse files Browse the repository at this point in the history
A recv() on an AF_UNIX, SOCK_STREAM socket can race with a
send()+close() on the peer, causing recv() to return zero, even though
the sent data should be received.

This happens if the send() and the close() is performed between
skb_dequeue() and checking sk->sk_shutdown in unix_stream_recvmsg():

process A  skb_dequeue() returns NULL, there's no data in the socket queue
process B  new data is inserted onto the queue by unix_stream_sendmsg()
process B  sk->sk_shutdown is set to SHUTDOWN_MASK by unix_release_sock()
process A  sk->sk_shutdown is checked, unix_release_sock() returns zero

I'm surprised nobody noticed this, it's not hard to trigger.  Maybe
it's just (un)luck with the timing.

It's possible to work around this bug in userspace, by retrying the
recv() once in case of a zero return value.

Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
Signed-off-by: David S. Miller <davem@davemloft.net>
  • Loading branch information
Miklos Szeredi authored and David S. Miller committed Jun 7, 2007
1 parent c764c9a commit 3c0d2f3
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions net/unix/af_unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -1744,20 +1744,23 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
int chunk;
struct sk_buff *skb;

unix_state_lock(sk);
skb = skb_dequeue(&sk->sk_receive_queue);
if (skb==NULL)
{
if (copied >= target)
break;
goto unlock;

/*
* POSIX 1003.1g mandates this order.
*/

if ((err = sock_error(sk)) != 0)
break;
goto unlock;
if (sk->sk_shutdown & RCV_SHUTDOWN)
break;
goto unlock;

unix_state_unlock(sk);
err = -EAGAIN;
if (!timeo)
break;
Expand All @@ -1771,7 +1774,11 @@ static int unix_stream_recvmsg(struct kiocb *iocb, struct socket *sock,
}
mutex_lock(&u->readlock);
continue;
unlock:
unix_state_unlock(sk);
break;
}
unix_state_unlock(sk);

if (check_creds) {
/* Never glue messages from different writers */
Expand Down

0 comments on commit 3c0d2f3

Please sign in to comment.