netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] fix race in AF_UNIX
@ 2007-06-02 21:50 Miklos Szeredi
  2007-06-02 22:11 ` Arnaldo Carvalho de Melo
                   ` (2 more replies)
  0 siblings, 3 replies; 47+ messages in thread
From: Miklos Szeredi @ 2007-06-02 21:50 UTC (permalink / raw)
  To: akpm; +Cc: netdev, linux-kernel

From: Miklos Szeredi <mszeredi@suse.cz>

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>
---

Index: linux-2.6.22-rc2/net/unix/af_unix.c
===================================================================
--- linux-2.6.22-rc2.orig/net/unix/af_unix.c	2007-06-02 23:45:47.000000000 +0200
+++ linux-2.6.22-rc2/net/unix/af_unix.c	2007-06-02 23:45:49.000000000 +0200
@@ -1711,20 +1711,23 @@ static int unix_stream_recvmsg(struct ki
 		int chunk;
 		struct sk_buff *skb;
 
+		unix_state_rlock(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_runlock(sk);
 			err = -EAGAIN;
 			if (!timeo)
 				break;
@@ -1738,7 +1741,11 @@ static int unix_stream_recvmsg(struct ki
 			}
 			mutex_lock(&u->readlock);
 			continue;
+ unlock:
+			unix_state_runlock(sk);
+			break;
 		}
+		unix_state_runlock(sk);
 
 		if (check_creds) {
 			/* Never glue messages from different writers */

^ permalink raw reply	[flat|nested] 47+ messages in thread

end of thread, other threads:[~2007-06-26 15:24 UTC | newest]

Thread overview: 47+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-06-02 21:50 [PATCH] fix race in AF_UNIX Miklos Szeredi
2007-06-02 22:11 ` Arnaldo Carvalho de Melo
2007-06-04  9:45 ` Miklos Szeredi
2007-06-05  7:02   ` David Miller
2007-06-05  7:42     ` Miklos Szeredi
2007-06-05  7:55       ` David Miller
2007-06-05  8:11         ` Miklos Szeredi
2007-06-05  8:19           ` David Miller
2007-06-05 20:11       ` David Miller
2007-06-06  0:31     ` David Miller
2007-06-06  5:26       ` Miklos Szeredi
2007-06-06  5:41         ` David Miller
2007-06-06  8:08           ` Miklos Szeredi
2007-06-06  8:12             ` David Miller
2007-06-08  1:47             ` David Miller
2007-06-11  9:57               ` Miklos Szeredi
2007-06-18  7:49                 ` Miklos Szeredi
2007-06-18  7:57                   ` David Miller
2007-06-18  8:20                     ` Miklos Szeredi
2007-06-18  9:18                       ` David Miller
2007-06-18  9:29                         ` Miklos Szeredi
2007-06-18  9:35                           ` David Miller
2007-06-18  9:44                             ` Miklos Szeredi
2007-06-18  9:48                               ` David Miller
2007-06-18  9:55                                 ` Miklos Szeredi
2007-06-18  9:59                                   ` David Miller
2007-06-18 10:32                               ` Thomas Graf
2007-06-18 10:39                                 ` Miklos Szeredi
2007-06-18 10:43                                   ` Thomas Graf
2007-06-18 12:01                                     ` Alan Cox
2007-06-18 10:40                                 ` Thomas Graf
2007-06-18 10:47                                   ` Miklos Szeredi
2007-06-18 10:51                                     ` David Miller
2007-06-18 10:55                                       ` Miklos Szeredi
2007-06-18 11:02                                         ` David Miller
2007-06-18 11:06                                           ` Miklos Szeredi
2007-06-18 11:09                                           ` David Miller
2007-06-18 11:46                                             ` Miklos Szeredi
2007-06-18 11:47                           ` Alan Cox
2007-06-18 11:45                             ` Jan Engelhardt
2007-06-18 12:00                             ` Miklos Szeredi
2007-06-21 15:18                 ` Eric W. Biederman
2007-06-23  8:48                   ` Miklos Szeredi
2007-06-23 16:42                     ` Eric W. Biederman
2007-06-26  8:54                       ` Miklos Szeredi
2007-06-26 15:24                         ` Eric W. Biederman
2007-06-04  9:53 ` Miklos Szeredi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).