Skip to contentSkip to conversation

af_packet: do not use READ_ONCE() in packet_bind()

PATCH Applied Eric Dumazet 5 participants 5 messages May 26, 2023 – May 30, 2023

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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 822b5a1c17df ("af_packet: Fix data-races of pkt_sk(sk)->num.")
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>
---
 net/packet/af_packet.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/packet/af_packet.c +5 -3
index a1f9a0e9f3c8a72e5a95f96473b7b6c63f893935..a2dbeb264f260e5b8923ece9aac99fe19ddfeb62 100644
@@ -3201,6 +3201,9 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
 
 	lock_sock(sk);
 	spin_lock(&po->bind_lock);
+	if (!proto)
+		proto = po->num;
+
 	rcu_read_lock();
 
 	if (po->fanout) {
@@ -3299,7 +3302,7 @@ static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
 	memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data_min));
 	name[sizeof(uaddr->sa_data_min)] = 0;
 
-	return packet_do_bind(sk, name, 0, READ_ONCE(pkt_sk(sk)->num));
+	return packet_do_bind(sk, name, 0, 0);
 }
 
 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
 	if (sll->sll_family != AF_PACKET)
 		return -EINVAL;
 
-	return packet_do_bind(sk, NULL, sll->sll_ifindex,
-			      sll->sll_protocol ? : READ_ONCE(pkt_sk(sk)->num));
+	return packet_do_bind(sk, NULL, sll->sll_ifindex, sll->sll_protocol);
 }
 
 static struct proto packet_proto = {
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Eric Dumazet

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.

Quoted 6 lines

READ_ONCE() in writers are often an evidence of something being wrong.

Fixes 822b5a1c17df ("af_packet: Fix data-races of pkt_sk(sk)->num.")
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>

Thanks for catching this Eric.

Reviewed-by on v1 1/1
#2 lore raw
In reply to Eric Dumazet
Quoted 4 lines · Fri, May 26, 2023 at 05:43:42PM CEST, [email protected] wrote:

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.

Nit: easier to understand the desctiption if you use imperative mood, telling the codebase what to do.

Quoted 5 lines

READ_ONCE() in writers are often an evidence of something being wrong.

Fixes 822b5a1c17df ("af_packet: Fix data-races of pkt_sk(sk)->num.")
#3 lore raw

PATCH 1/1 af_packet: do not use READ_ONCE() in packet_bind()

In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Fri, 26 May 2023 15:43:42 +0000

Quoted 11 lines

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 822b5a1c17df ("af_packet: Fix data-races of pkt_sk(sk)->num.")
Cc Kuniyuki Iwashima <[email protected]>
Cc Willem de Bruijn <[email protected]>

Thanks!

Quoted 39 lines
---
 net/packet/af_packet.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
net/packet/af_packet.c +5 -3
index a1f9a0e9f3c8a72e5a95f96473b7b6c63f893935..a2dbeb264f260e5b8923ece9aac99fe19ddfeb62 100644
@@ -3201,6 +3201,9 @@ static int packet_do_bind(struct sock *sk, const char *name, int ifindex,
 
 	lock_sock(sk);
 	spin_lock(&po->bind_lock);
+	if (!proto)
+		proto = po->num;
+
 	rcu_read_lock();
 
 	if (po->fanout) {
@@ -3299,7 +3302,7 @@ static int packet_bind_spkt(struct socket *sock, struct sockaddr *uaddr,
 	memcpy(name, uaddr->sa_data, sizeof(uaddr->sa_data_min));
 	name[sizeof(uaddr->sa_data_min)] = 0;
 
-	return packet_do_bind(sk, name, 0, READ_ONCE(pkt_sk(sk)->num));
+	return packet_do_bind(sk, name, 0, 0);
 }
 
 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
 	if (sll->sll_family != AF_PACKET)
 		return -EINVAL;
 
-	return packet_do_bind(sk, NULL, sll->sll_ifindex,
-			      sll->sll_protocol ? : READ_ONCE(pkt_sk(sk)->num));
+	return packet_do_bind(sk, NULL, sll->sll_ifindex, sll->sll_protocol);
 }
 
 static struct proto packet_proto = {
Signature · 1 line
2.41.0.rc0.172.g3f132b7071-goog
#4 lore raw
In reply to Eric Dumazet
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Fri, 26 May 2023 15:43:42 +0000 you wrote:

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.

[...]

Here is the summary with links:
- [net] af_packet: do not use READ_ONCE() in packet_bind()

    https://git.kernel.org/netdev/net/c/6ffc57ea0042

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html