Skip to content

Commit

Permalink
packet: fix races in fanout_add()
Browse files Browse the repository at this point in the history
commit d199fab 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: dc99f60 ("packet: Add fanout support.")
Fixes: 0648ab7 ("packet: rollover prepare: per-socket state")
Signed-off-by: Eric Dumazet <[email protected]>
Cc: Willem de Bruijn <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
Signed-off-by: Jiri Slaby <[email protected]>
Signed-off-by: Willy Tarreau <[email protected]>
  • Loading branch information
Eric Dumazet authored and wtarreau committed Jun 7, 2017
1 parent 3d75337 commit 2a272ab
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions net/packet/af_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1304,13 +1304,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 &&
Expand Down Expand Up @@ -1366,17 +1369,16 @@ 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);
po->fanout = NULL;
f = po->fanout;
if (f) {
po->fanout = NULL;

if (atomic_dec_and_test(&f->sk_ref)) {
list_del(&f->list);
dev_remove_pack(&f->prot_hook);
kfree(f);
if (atomic_dec_and_test(&f->sk_ref)) {
list_del(&f->list);
dev_remove_pack(&f->prot_hook);
kfree(f);
}
}
mutex_unlock(&fanout_mutex);
}
Expand Down

0 comments on commit 2a272ab

Please sign in to comment.