Skip to content

Commit 6ffc57e

Browse files
Eric Dumazetkuba-moo
authored andcommitted
af_packet: do not use READ_ONCE() in packet_bind()
A recent patch added READ_ONCE() in packet_bind() and packet_bind_spkt() This is better handled by reading pkt_sk(sk)->num later in packet_do_bind() while appropriate lock is held. READ_ONCE() in writers are often an evidence of something being wrong. Fixes: 822b5a1 ("af_packet: Fix data-races of pkt_sk(sk)->num.") Signed-off-by: Eric Dumazet <edumazet@google.com> Reviewed-by: Willem de Bruijn <willemb@google.com> Reviewed-by: Jiri Pirko <jiri@nvidia.com> Reviewed-by: Kuniyuki Iwashima <kuniyu@amazon.com> Link: https://lore.kernel.org/r/20230526154342.2533026-1-edumazet@google.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 0684f29 commit 6ffc57e

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

net/packet/af_packet.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3201,6 +3201,9 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
32013201

32023202
lock_sock(sk);
32033203
spin_lock(&po->bind_lock);
3204+
if (!proto)
3205+
proto = po->num;
3206+
32043207
rcu_read_lock();
32053208

32063209
if (po->fanout) {
@@ -3299,7 +3302,7 @@ static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
32993302
memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data_min));
33003303
name[sizeof(uaddr->sa_data_min)] = 0;
33013304

3302-
return packet_do_bind(sk, name, 0, READ_ONCE(pkt_sk(sk)->num));
3305+
return packet_do_bind(sk, name, 0, 0);
33033306
}
33043307

33053308
static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
@@ -3316,8 +3319,7 @@ static int packet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len
33163319
if (sll->sll_family != AF_PACKET)
33173320
return -EINVAL;
33183321

3319-
return packet_do_bind(sk, NULL, sll->sll_ifindex,
3320-
sll->sll_protocol ? : READ_ONCE(pkt_sk(sk)->num));
3322+
return packet_do_bind(sk, NULL, sll->sll_ifindex, sll->sll_protocol);
33213323
}
33223324

33233325
static struct proto packet_proto = {

0 commit comments

Comments
 (0)