Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit df9316b9 authored by Eric Dumazet's avatar Eric Dumazet Committed by minz1
Browse files

UPSTREAM: packet: fix races in fanout_add()



commit d199fab63c11998a602205f7ee7ff7c05c97164b upstream.

Multiple threads can call fanout_add() at the same time.

We need to grab fanout_mutex earlier to avoid races that could
lead to one thread freeing po->rollover that was set by another thread.

Do the same in fanout_release(), for peace of mind, and to help us
finding lockdep issues earlier.

[js] no rollover in 3.12

Fixes: dc99f600 ("packet: Add fanout support.")
Fixes: 0648ab70afe6 ("packet: rollover prepare: per-socket state")
Signed-off-by: default avatarEric Dumazet <edumazet@google.com>
Cc: Willem de Bruijn <willemb@google.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
Signed-off-by: default avatarWilly Tarreau <w@1wt.eu>
(cherry picked from commit 2a272abc4e543f488b3a73292ee75a06f20d077a)
Bug: 37897645
Change-Id: I3b021869ee26b88d10f4d6408ce34d351543ce74
parent a13e0478
Loading
Loading
Loading
Loading
+14 −12
Original line number Diff line number Diff line
@@ -1317,13 +1317,16 @@ static int fanout_add(struct sock *sk, u16 id, u16 type_flags)
		return -EINVAL;
	}

	mutex_lock(&fanout_mutex);

	err = -EINVAL;
	if (!po->running)
		return -EINVAL;
		goto out;

	err = -EALREADY;
	if (po->fanout)
		return -EALREADY;
		goto out;

	mutex_lock(&fanout_mutex);
	match = NULL;
	list_for_each_entry(f, &fanout_list, list) {
		if (f->id == id &&
@@ -1379,11 +1382,9 @@ static void fanout_release(struct sock *sk)
	struct packet_sock *po = pkt_sk(sk);
	struct packet_fanout *f;

	f = po->fanout;
	if (!f)
		return;

	mutex_lock(&fanout_mutex);
	f = po->fanout;
	if (f) {
		po->fanout = NULL;

		if (atomic_dec_and_test(&f->sk_ref)) {
@@ -1391,6 +1392,7 @@ static void fanout_release(struct sock *sk)
			dev_remove_pack(&f->prot_hook);
			kfree(f);
		}
	}
	mutex_unlock(&fanout_mutex);
}