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

Message ID 1531221538-10134-3-git-send-email-paolo.pisati@canonical.com
State New
Headers show
  • Fix for CVE-2017-15649
Related show

Commit Message

Paolo Pisati July 10, 2018, 11:18 a.m.
From: Willem de Bruijn <willemb@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@google.com>
Reviewed-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
(cherry picked from commit 4971613c1639d8e5f102c4e797c3bf8f83a5a69e)
Signed-off-by: Paolo Pisati <paolo.pisati@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)
 		return -EINVAL;
-	lock_sock(sk);
-	spin_lock(&po->bind_lock);
 	unregister_prot_hook(sk, true);
 	po->num = protocol;