[PATCH 2/2] packet: in packet_do_bind, test fanout with bind_lock held

Paolo Pisati paolo.pisati at canonical.com
Tue Jul 10 11:18:58 UTC 2018


From: Willem de Bruijn <willemb at google.com>

Once a socket has po->fanout set, it remains a member of the group
until it is destroyed. The prot_hook must be constant and identical
across sockets in the group.

If fanout_add races with packet_do_bind between the test of po->fanout
and taking the lock, the bind call may make type or dev inconsistent
with that of the fanout group.

Hold po->bind_lock when testing po->fanout to avoid this race.

I had to introduce artificial delay (local_bh_enable) to actually
observe the race.

Fixes: dc99f600698d ("packet: Add fanout support.")
Signed-off-by: Willem de Bruijn <willemb at google.com>
Reviewed-by: Eric Dumazet <edumazet at google.com>
Signed-off-by: David S. Miller <davem at davemloft.net>
(cherry picked from commit 4971613c1639d8e5f102c4e797c3bf8f83a5a69e)
Signed-off-by: Paolo Pisati <paolo.pisati at canonical.com>
---
 net/packet/af_packet.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/net/packet/af_packet.c b/net/packet/af_packet.c
index 2a07eb2..c0230c7 100644
--- a/net/packet/af_packet.c
+++ b/net/packet/af_packet.c
@@ -2514,16 +2514,18 @@ static int packet_do_bind(struct sock *sk, struct net_device *dev, __be16 protoc
 {
 	struct packet_sock *po = pkt_sk(sk);
 
+	lock_sock(sk);
+
+	spin_lock(&po->bind_lock);
+
 	if (po->fanout) {
+		spin_unlock(&po->bind_lock);
+		release_sock(sk);
 		if (dev)
 			dev_put(dev);
-
 		return -EINVAL;
 	}
 
-	lock_sock(sk);
-
-	spin_lock(&po->bind_lock);
 	unregister_prot_hook(sk, true);
 
 	po->num = protocol;
-- 
2.7.4





More information about the kernel-team mailing list