Skip to contentSkip to conversation

net: fix setsockopt() locking errors

PATCH In review Vegard Nossum 6 participants 9 messages Jan 24, 2009 – Jan 30, 2009

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

PATCH 1/1 net: fix setsockopt() locking errors

Hi,

This survives basic testing here, but I don't know what that counts for when I couldn't reproduce the lockdep report in the first place. Please review.

Vegard

From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Sat, 24 Jan 2009 22:44:16 +0100
Subject: [PATCH] net: fix setsockopt() locking errors
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Martin MOKREJŠ <[email protected]> reported:

Quoted 11 lines

=======================================================
[ INFO: possible circular locking dependency detected ] 2.6.29-rc2-git1 #1
-------------------------------------------------------
tcpdump/3734 is trying to acquire lock:
(&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b

but task is already holding lock:
(sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4

which lock already depends on the new lock.

It turns out that sock_setsockopt() is calling copy_from_user() while holding the lock on the socket. We fix it by splitting the ioctl code so that one switch handles the ioctls that have their own code for reading from userspace, and one switch handles the cases that require no additional reading.

---
 net/core/sock.c |  134 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 87 insertions(+), 47 deletions(-)
net/core/sock.c +87 -47
index f3a0d08..6bd618d 100644
@@ -424,6 +424,80 @@ out:
 	return ret;
 }
 
+static int sock_linger(struct sock *sk, char __user *optval, int optlen)
+{
+	struct linger ling;
+
+	if (optlen < sizeof(ling))
+		return -EINVAL; /* 1003.1g */
+	if (copy_from_user(&ling, optval, sizeof(ling)))
+		return -EFAULT;
+
+	lock_sock(sk);
+
+	if (!ling.l_onoff)
+		sock_reset_flag(sk, SOCK_LINGER);
+	else {
+#if (BITS_PER_LONG == 32)
+		if ((unsigned int) ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
+			sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
+		else
+#endif
+			sk->sk_lingertime = (unsigned int) ling.l_linger * HZ;
+		sock_set_flag(sk, SOCK_LINGER);
+	}
+
+	release_sock(sk);
+
+	return 0;
+}
+
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	long rcvtimeo;
+
+	ret = sock_set_timeout(&rcvtimeo, optval, optlen);
+
+	lock_sock(sk);
+	sk->sk_rcvtimeo = rcvtimeo;
+	release_sock(sk);
+
+	return ret;
+}
+
+static int sock_set_sndtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	long sndtimeo;
+
+	ret = sock_set_timeout(&sndtimeo, optval, optlen);
+
+	lock_sock(sk);
+	sk->sk_sndtimeo = sndtimeo;
+	release_sock(sk);
+
+	return ret;
+}
+
+static int sock_attach_filter(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	struct sock_fprog fprog;
+
+	if (optlen != sizeof(struct sock_fprog))
+		return -EINVAL;
+
+	if (copy_from_user(&fprog, optval, sizeof(fprog)))
+		return -EFAULT;
+
+	lock_sock(sk);
+	ret = sk_attach_filter(&fprog, sk);
+	release_sock(sk);
+
+	return ret;
+}
+
 static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
 {
 	if (valbool)
@@ -440,18 +514,27 @@ static inline void sock_valbool_flag(struct sock *sk, int bit, int valbool)
 int sock_setsockopt(struct socket *sock, int level, int optname,
 		    char __user *optval, int optlen)
 {
-	struct sock *sk=sock->sk;
+	struct sock *sk = sock->sk;
 	int val;
 	int valbool;
-	struct linger ling;
 	int ret = 0;
 
 	/*
-	 *	Options without arguments
+	 * Options with special locking requirements
 	 */
 
-	if (optname == SO_BINDTODEVICE)
+	switch (optname) {
+	case SO_BINDTODEVICE:
 		return sock_bindtodevice(sk, optval, optlen);
+	case SO_LINGER:
+		return sock_linger(sk, optval, optlen);
+	case SO_RCVTIMEO:
+		return sock_set_rcvtimeo(sk, optval, optlen);
+	case SO_SNDTIMEO:
+		return sock_set_sndtimeo(sk, optval, optlen);
+	case SO_ATTACH_FILTER:
+		return sock_attach_filter(sk, optval, optlen);
+	}
 
 	if (optlen < sizeof(int))
 		return -EINVAL;
@@ -573,28 +656,6 @@ set_rcvbuf:
 			ret = -EPERM;
 		break;
 
-	case SO_LINGER:
-		if (optlen < sizeof(ling)) {
-			ret = -EINVAL;	/* 1003.1g */
-			break;
-		}
-		if (copy_from_user(&ling,optval,sizeof(ling))) {
-			ret = -EFAULT;
-			break;
-		}
-		if (!ling.l_onoff)
-			sock_reset_flag(sk, SOCK_LINGER);
-		else {
-#if (BITS_PER_LONG == 32)
-			if ((unsigned int)ling.l_linger >= MAX_SCHEDULE_TIMEOUT/HZ)
-				sk->sk_lingertime = MAX_SCHEDULE_TIMEOUT;
-			else
-#endif
-				sk->sk_lingertime = (unsigned int)ling.l_linger * HZ;
-			sock_set_flag(sk, SOCK_LINGER);
-		}
-		break;
-
 	case SO_BSDCOMPAT:
 		sock_warn_obsolete_bsdism("setsockopt");
 		break;
@@ -627,27 +688,6 @@ set_rcvbuf:
 		sk->sk_rcvlowat = val ? : 1;
 		break;
 
-	case SO_RCVTIMEO:
-		ret = sock_set_timeout(&sk->sk_rcvtimeo, optval, optlen);
-		break;
-
-	case SO_SNDTIMEO:
-		ret = sock_set_timeout(&sk->sk_sndtimeo, optval, optlen);
-		break;
-
-	case SO_ATTACH_FILTER:
-		ret = -EINVAL;
-		if (optlen == sizeof(struct sock_fprog)) {
-			struct sock_fprog fprog;
-
-			ret = -EFAULT;
-			if (copy_from_user(&fprog, optval, sizeof(fprog)))
-				break;
-
-			ret = sk_attach_filter(&fprog, sk);
-		}
-		break;
-
 	case SO_DETACH_FILTER:
 		ret = sk_detach_filter(sk);
 		break;
Signature · 1 line
1.5.6.6
#1 lore raw
Quoted 26 lines · Vegard Nossum

Hi,

This survives basic testing here, but I don't know what that counts for when I couldn't reproduce the lockdep report in the first place. Please review.

Vegard

From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Sat, 24 Jan 2009 22:44:16 +0100
Subject: [PATCH] net: fix setsockopt() locking errors
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Martin MOKREJ. <[email protected]> reported:

Quoted 7 lines

=======================================================
[ INFO: possible circular locking dependency detected ] 2.6.29-rc2-git1 #1
-------------------------------------------------------
tcpdump/3734 is trying to acquire lock:
(&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b

Quoted 7 lines
Quoted 4 lines

but task is already holding lock:
(sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4

which lock already depends on the new lock.

It turns out that sock_setsockopt() is calling copy_from_user() while holding the lock on the socket.

I guess it has been like this for some time, so it would be nice to mention what scenario happens here, or IOW what exactly needs to get these locks in reverse order.

Quoted 20 lines

We fix it by splitting the ioctl code
so that one switch handles the ioctls that have their own code for reading from userspace, and one switch handles the cases that require no additional reading.

---
 net/core/sock.c |  134 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 87 insertions(+), 47 deletions(-)
net/core/sock.c +1 -0
index f3a0d08..6bd618d 100644
@@ -424,6 +424,80 @@ out:
 	return ret;
 }
 
+static int sock_linger(struct sock *sk, char __user *optval, int optlen)

...

net/core/sock.c out: · line 460

Quoted 6 lines
net/core/sock.c +6 -0
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	long rcvtimeo;
+
+	ret = sock_set_timeout(&rcvtimeo, optval, optlen);

A check for error is needed here and below.

Quoted 11 lines
net/core/sock.c +11 -0
+
+	lock_sock(sk);
+	sk->sk_rcvtimeo = rcvtimeo;
+	release_sock(sk);
+
+	return ret;
+}
+
+static int sock_set_sndtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;

net/core/sock.c out: · line 481

Quoted 10 lines
net/core/sock.c +10 -0
+	long sndtimeo;
+
+	ret = sock_set_timeout(&sndtimeo, optval, optlen);
+
+	lock_sock(sk);
+	sk->sk_sndtimeo = sndtimeo;
+	release_sock(sk);
+
+	return ret;
+}

...

Jarek P.

maintainer
#2 lore raw

From: Jarek Poplawski <[email protected]>
Date: Mon, 26 Jan 2009 11:50:12 +0000

Quoted 11 lines · Vegard Nossum
Quoted 1 line

+static int sock_linger(struct sock *sk, char __user *optval, int optlen)

...

net/core/sock.c out: · line 460

Quoted 6 lines
net/core/sock.c +6 -0
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	long rcvtimeo;
+
+	ret = sock_set_timeout(&rcvtimeo, optval, optlen);

A check for error is needed here and below.

Right, you cannot continue and update the socket state if this sock_set_timeout() call returns an error.

#4 lore raw
In reply to Jarek Poplawski

The patch really did not help:
http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
Martin

Quoted 68 lines · Jarek Poplawski
Quoted 26 lines · Vegard Nossum

Hi,

This survives basic testing here, but I don't know what that counts for when I couldn't reproduce the lockdep report in the first place. Please review.

Vegard

From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Sat, 24 Jan 2009 22:44:16 +0100
Subject: [PATCH] net: fix setsockopt() locking errors
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Martin MOKREJ. <[email protected]> reported:

Quoted 7 lines

=======================================================
[ INFO: possible circular locking dependency detected ] 2.6.29-rc2-git1 #1
-------------------------------------------------------
tcpdump/3734 is trying to acquire lock:
(&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b

Quoted 6 lines
Quoted 4 lines

but task is already holding lock:
(sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4

which lock already depends on the new lock.

It turns out that sock_setsockopt() is calling copy_from_user() while holding the lock on the socket.

I guess it has been like this for some time, so it would be nice to mention what scenario happens here, or IOW what exactly needs to get these locks in reverse order.

Quoted 20 lines

We fix it by splitting the ioctl code
so that one switch handles the ioctls that have their own code for reading from userspace, and one switch handles the cases that require no additional reading.

---
 net/core/sock.c |  134 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 87 insertions(+), 47 deletions(-)
net/core/sock.c +1 -0
index f3a0d08..6bd618d 100644
@@ -424,6 +424,80 @@ out:
 	return ret;
 }
 
+static int sock_linger(struct sock *sk, char __user *optval, int optlen)

...

net/core/sock.c out: · line 460

Quoted 6 lines
net/core/sock.c +6 -0
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	long rcvtimeo;
+
+	ret = sock_set_timeout(&rcvtimeo, optval, optlen);

A check for error is needed here and below.

[cut]

#5 lore raw
Quoted 3 lines · Martin MOKREJŠ

The patch really did not help:
http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
Martin

Actually, there is a little change: the warning triggerd in another place (sock_setsockopt() -> sk_attach_filter()). So we could go deeper with these changes, but I'm not sure this is the right way to fix.

It looks like the scenario is very old, but probably wasn't reported (maybe there is some lockdep improvement):

A) sys_mmap2() -> mm->mmap_sem -> packet_mmap() -> sk_lock
B) sock_setsockopt() -> sk_lock -> copy_from_user() -> mm->mmap_sem

packet_mmap() (net/packet/af_packet.c) seems to be the only place in net to implement mmap method, and using this lock order btw. On the other hand copy_from_user() could be more popular under sk_lock, and I'm not sure these changes are necessary.

Since I don't know enough neither sock/packet nor sys_mmap, I guess some advice would be precious. It looks like Peter Zijlstra solved similar problems in nfs, so I CC him.

Thanks,
Jarek P.

Quoted 71 lines
Quoted 68 lines · Jarek Poplawski
Quoted 26 lines · Vegard Nossum

Hi,

This survives basic testing here, but I don't know what that counts for when I couldn't reproduce the lockdep report in the first place. Please review.

Vegard

From cc8bcd1c4fd219a31d6d191aefa4b4b57dadb9b0 Mon Sep 17 00:00:00 2001
From: Vegard Nossum <[email protected]>
Date: Sat, 24 Jan 2009 22:44:16 +0100
Subject: [PATCH] net: fix setsockopt() locking errors
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit

Martin MOKREJ. <[email protected]> reported:

Quoted 7 lines

=======================================================
[ INFO: possible circular locking dependency detected ] 2.6.29-rc2-git1 #1
-------------------------------------------------------
tcpdump/3734 is trying to acquire lock:
(&mm->mmap_sem){----}, at: [<c1053294>] might_fault+0x30/0x6b

Quoted 6 lines
Quoted 4 lines

but task is already holding lock:
(sk_lock-AF_PACKET){--..}, at: [<c12798c8>] sock_setsockopt+0x12b/0x4a4

which lock already depends on the new lock.

It turns out that sock_setsockopt() is calling copy_from_user() while holding the lock on the socket.

I guess it has been like this for some time, so it would be nice to mention what scenario happens here, or IOW what exactly needs to get these locks in reverse order.

Quoted 20 lines

We fix it by splitting the ioctl code
so that one switch handles the ioctls that have their own code for reading from userspace, and one switch handles the cases that require no additional reading.

---
 net/core/sock.c |  134 +++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 87 insertions(+), 47 deletions(-)
net/core/sock.c +1 -0
index f3a0d08..6bd618d 100644
@@ -424,6 +424,80 @@ out:
 	return ret;
 }
 
+static int sock_linger(struct sock *sk, char __user *optval, int optlen)

...

net/core/sock.c out: · line 460

Quoted 6 lines
net/core/sock.c +6 -0
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+	int ret;
+	long rcvtimeo;
+
+	ret = sock_set_timeout(&rcvtimeo, optval, optlen);

A check for error is needed here and below.

[cut]

#6 lore raw
Quoted 11 lines · Jarek Poplawski
Quoted 3 lines · Martin MOKREJŠ

The patch really did not help:
http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
Martin

Actually, there is a little change: the warning triggerd in another place (sock_setsockopt() -> sk_attach_filter()). So we could go deeper with these changes, but I'm not sure this is the right way to fix.

It looks like the scenario is very old, but probably wasn't reported (maybe there is some lockdep improvement):

Yes, they likely are very old, and yes we added a lockdep annotation to copy_to/from_user() to catch these.

Quoted 11 lines

A) sys_mmap2() -> mm->mmap_sem -> packet_mmap() -> sk_lock
B) sock_setsockopt() -> sk_lock -> copy_from_user() -> mm->mmap_sem

packet_mmap() (net/packet/af_packet.c) seems to be the only place in net to implement mmap method, and using this lock order btw. On the other hand copy_from_user() could be more popular under sk_lock, and I'm not sure these changes are necessary.

Since I don't know enough neither sock/packet nor sys_mmap, I guess some advice would be precious. It looks like Peter Zijlstra solved similar problems in nfs, so I CC him.

The NFS/sunrpc case was special in that it did copy_to/from_kernel, that is, it never actually touched user memory -- we taught the might_fault() annotation about that.

Can't you simply do the copy_from_user() before you take the sk_lock?

#7 lore raw
Quoted 28 lines · Peter Zijlstra
Quoted 11 lines · Jarek Poplawski
Quoted 3 lines · Martin MOKREJŠ

The patch really did not help:
http://bugzilla.kernel.org/show_bug.cgi?id=12515#c5
Martin

Actually, there is a little change: the warning triggerd in another place (sock_setsockopt() -> sk_attach_filter()). So we could go deeper with these changes, but I'm not sure this is the right way to fix.

It looks like the scenario is very old, but probably wasn't reported (maybe there is some lockdep improvement):

Yes, they likely are very old, and yes we added a lockdep annotation to copy_to/from_user() to catch these.

Quoted 11 lines

A) sys_mmap2() -> mm->mmap_sem -> packet_mmap() -> sk_lock
B) sock_setsockopt() -> sk_lock -> copy_from_user() -> mm->mmap_sem

packet_mmap() (net/packet/af_packet.c) seems to be the only place in net to implement mmap method, and using this lock order btw. On the other hand copy_from_user() could be more popular under sk_lock, and I'm not sure these changes are necessary.

Since I don't know enough neither sock/packet nor sys_mmap, I guess some advice would be precious. It looks like Peter Zijlstra solved similar problems in nfs, so I CC him.

Quoted 6 lines

The NFS/sunrpc case was special in that it did copy_to/from_kernel, that is, it never actually touched user memory -- we taught the might_fault() annotation about that.

Can't you simply do the copy_from_user() before you take the sk_lock?

Since it's really needed, and Vegard started doing it like this, I guess he will try to add the missing pieces.

Thanks again,
Jarek P.

#3 lore raw
In reply to David Miller
Quoted 15 lines · David Miller

From: Jarek Poplawski <[email protected]>
Date: Mon, 26 Jan 2009 11:50:12 +0000

Quoted 11 lines · Vegard Nossum
Quoted 1 line

+static int sock_linger(struct sock *sk, char __user *optval, int optlen)

...

net/core/sock.c out: · line 460

Quoted 6 lines
net/core/sock.c +6 -0
+static int sock_set_rcvtimeo(struct sock *sk, char __user *optval, int optlen)
+{
+   int ret;
+   long rcvtimeo;
+
+   ret = sock_set_timeout(&rcvtimeo, optval, optlen);

A check for error is needed here and below.

Quoted 3 lines

Right, you cannot continue and update the socket state if this sock_set_timeout() call returns an error.

Argh, thanks both. What a stupid mistake. What is the point in fixing something if the fix introduces a different kind of error? :-(

I will fix the patch and also check for other places (like that attach_filter) that need to be fixed.

Vegard

Signature · 4 lines
"The animistic metaphor of the bug that maliciously sneaked in while
the programmer was not looking is intellectually dishonest as it
disguises that the error is the programmer's own creation."
	-- E. W. Dijkstra, EWD1036
#8 lore raw
In reply to Peter Zijlstra
Quoted 2 lines · Peter Zijlstra

Can't you simply do the copy_from_user() before you take the sk_lock?

Well, doing the copy under sk_lock is pretty common through all protocols. So I think it'd be safer to change the other path, which is doing the odd thing here, i.e., ->mmap() grabbing the socket lock while holding mmap_sem.

In fact, it would appear that we don't really need the socket lock in ->mmap() since it only needs to ensure that pg_vec* doesn't get yanked or changed. So this patch should work:

packet: Avoid lock_sock in mmap handler

As the mmap handler gets called under mmap_sem, and we may grab mmap_sem elsewhere under the socket lock to access user data, we should avoid grabbing the socket lock in the mmap handler.

Since the only thing we care about in the mmap handler is for pg_vec* to be invariant, i.e., to exclude packet_set_ring, we can achieve this by simply using sk_receive_queue.lock.

I resisted the temptation to create a new spin lock because the mmap path isn't exactly common.

net/packet/af_packet.c +6 -5
index 5f94db2..cac0a2b 100644
@@ -1874,13 +1874,14 @@ static int packet_set_ring(struct sock *sk, struct tpacket_req *req, int closing
 		po->frame_max = (req->tp_frame_nr - 1);
 		po->head = 0;
 		po->frame_size = req->tp_frame_size;
-		spin_unlock_bh(&sk->sk_receive_queue.lock);
 
-		order = XC(po->pg_vec_order, order);
 		req->tp_block_nr = XC(po->pg_vec_len, req->tp_block_nr);
-
 		po->pg_vec_pages = req->tp_block_size/PAGE_SIZE;
+		spin_unlock_bh(&sk->sk_receive_queue.lock);
+
+		order = XC(po->pg_vec_order, order);
 		po->prot_hook.func = po->pg_vec ? tpacket_rcv : packet_rcv;
+
 		skb_queue_purge(&sk->sk_receive_queue);
 #undef XC
 		if (atomic_read(&po->mapped))
@@ -1918,7 +1919,7 @@ static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_st
 
 	size = vma->vm_end - vma->vm_start;
 
-	lock_sock(sk);
+	spin_lock_bh(&sk->sk_receive_queue.lock);
 	if (po->pg_vec == NULL)
 		goto out;
 	if (size != po->pg_vec_len*po->pg_vec_pages*PAGE_SIZE)
@@ -1941,7 +1942,7 @@ static int packet_mmap(struct file *file, struct socket *sock, struct vm_area_st
 	err = 0;
 
 out:
-	release_sock(sk);
+	spin_unlock_bh(&sk->sk_receive_queue.lock);
 	return err;
 }
 #endif

Cheers,

Signature · 4 lines
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt