Skip to contentSkip to conversation

Only run BPF cgroup unix sockaddr recvmsg() hooks on named sockets

PATCH In review Daan De Meyer 3 participants 4 messages Oct 12, 2023 – Oct 16, 2023

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

PATCH v2 1/1 Only run BPF cgroup unix sockaddr recvmsg() hooks on named sockets

Changes since v1:

* Added missing Signed-off-by tag

We should not run the recvmsg() hooks on unnamed sockets as we do not run them on unnamed sockets in the other hooks either. We may look into relaxing this later but for now let's make sure we are consistent and not run the hooks on unnamed sockets anywhere.

---
 net/unix/af_unix.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/unix/af_unix.c +8 -6
index e10d07c76044..81fb8bddaff9 100644
@@ -2416,9 +2416,10 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
 	if (msg->msg_name) {
 		unix_copy_addr(msg, skb->sk);
 
-		BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-						      msg->msg_name,
-						      &msg->msg_namelen);
+		if (msg->msg_namelen > 0)
+			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+							      msg->msg_name,
+							      &msg->msg_namelen);
 	}
 
 	if (size > skb->len - skip)
@@ -2773,9 +2774,10 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 					 state->msg->msg_name);
 			unix_copy_addr(state->msg, skb->sk);
 
-			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-							      state->msg->msg_name,
-							      &state->msg->msg_namelen);
+			if (state->msg->msg_namelen > 0)
+				BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+								      state->msg->msg_name,
+								      &state->msg->msg_namelen);
 
 			sunaddr = NULL;
 		}

--
2.41.0

#1 lore raw

From: Daan De Meyer <[email protected]>
Date: Thu, 12 Oct 2023 10:52:13 +0200

Quoted 3 lines

Changes since v1:

* Added missing Signed-off-by tag

You can put these after --- so that it will disappear when merged.

Quoted 16 lines

We should not run the recvmsg() hooks on unnamed sockets as we do not run them on unnamed sockets in the other hooks either. We may look into relaxing this later but for now let's make sure we are consistent and not run the hooks on unnamed sockets anywhere.

---
 net/unix/af_unix.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
net/unix/af_unix.c +0 -0
index e10d07c76044..81fb8bddaff9 100644

net/unix/af_unix.c int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, · line 2417

Quoted 2 lines
net/unix/af_unix.c +0 -0
@@ -2416,9 +2416,10 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
 	if (msg->msg_name) {
 		unix_copy_addr(msg, skb->sk);

How is an unnamed socket set to skb->sk ?

Quoted 28 lines
net/unix/af_unix.c +8 -7
-		BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-						      msg->msg_name,
-						      &msg->msg_namelen);
+		if (msg->msg_namelen > 0)
+			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+							      msg->msg_name,
+							      &msg->msg_namelen);
 	}
 
 	if (size > skb->len - skip)
@@ -2773,9 +2774,10 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
 					 state->msg->msg_name);
 			unix_copy_addr(state->msg, skb->sk);
 
-			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-							      state->msg->msg_name,
-							      &state->msg->msg_namelen);
+			if (state->msg->msg_namelen > 0)
+				BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+								      state->msg->msg_name,
+								      &state->msg->msg_namelen);
 
 			sunaddr = NULL;
 		}
--

2.41.0

#2 lore raw
Quoted 20 lines · Kuniyuki Iwashima

From: Daan De Meyer <[email protected]>
Date: Thu, 12 Oct 2023 10:52:13 +0200

Quoted 3 lines

Changes since v1:

* Added missing Signed-off-by tag

You can put these after --- so that it will disappear when merged.

Quoted 11 lines

We should not run the recvmsg() hooks on unnamed sockets as we do not run them on unnamed sockets in the other hooks either. We may look into relaxing this later but for now let's make sure we are consistent and not run the hooks on unnamed sockets anywhere.

---
  net/unix/af_unix.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
Quoted 9 lines

net/unix/af_unix.c int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, · line 2417

Quoted 7 lines
net/unix/af_unix.c +0 -0
index e10d07c76044..81fb8bddaff9 100644
@@ -2416,9 +2416,10 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
  	if (msg->msg_name) {
  		unix_copy_addr(msg, skb->sk);

How is an unnamed socket set to skb->sk ?

I had a similar question. Most likely socketpair? Please add an explanation in the commit message in v3. Please also help to add a selftest for this case.

Quoted 30 lines
Quoted 30 lines
net/unix/af_unix.c +8 -7
-		BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-						      msg->msg_name,
-						      &msg->msg_namelen);
+		if (msg->msg_namelen > 0)
+			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+							      msg->msg_name,
+							      &msg->msg_namelen);
  	}
 
  	if (size > skb->len - skip)
@@ -2773,9 +2774,10 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
  					 state->msg->msg_name);
  			unix_copy_addr(state->msg, skb->sk);
 
-			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-							      state->msg->msg_name,
-							      &state->msg->msg_namelen);
+			if (state->msg->msg_namelen > 0)
+				BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+								      state->msg->msg_name,
+								      &state->msg->msg_namelen);
 
  			sunaddr = NULL;
  		}
--

2.41.0

#3 lore raw

From: Martin KaFai Lau <[email protected]>
Date: Mon, 16 Oct 2023 11:33:36 -0700

Quoted 31 lines · Kuniyuki Iwashima
Quoted 20 lines

From: Daan De Meyer <[email protected]>
Date: Thu, 12 Oct 2023 10:52:13 +0200

Quoted 3 lines

Changes since v1:

* Added missing Signed-off-by tag

You can put these after --- so that it will disappear when merged.

Quoted 11 lines

We should not run the recvmsg() hooks on unnamed sockets as we do not run them on unnamed sockets in the other hooks either. We may look into relaxing this later but for now let's make sure we are consistent and not run the hooks on unnamed sockets anywhere.

---
  net/unix/af_unix.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)
Quoted 9 lines

net/unix/af_unix.c int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size, · line 2417

Quoted 7 lines
net/unix/af_unix.c +0 -0
index e10d07c76044..81fb8bddaff9 100644
@@ -2416,9 +2416,10 @@ int __unix_dgram_recvmsg(struct sock *sk, struct msghdr *msg, size_t size,
  	if (msg->msg_name) {
  		unix_copy_addr(msg, skb->sk);

How is an unnamed socket set to skb->sk ?

Quoted 2 lines

I had a similar question. Most likely socketpair? Please add an explanation in the commit message in v3. Please also help to add a selftest for this case.

Ah exactly, socketpair() for SOCK_STREAM does it.

Quoted 30 lines
Quoted 30 lines
Quoted 30 lines
net/unix/af_unix.c +8 -7
-		BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-						      msg->msg_name,
-						      &msg->msg_namelen);
+		if (msg->msg_namelen > 0)
+			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+							      msg->msg_name,
+							      &msg->msg_namelen);
  	}
 
  	if (size > skb->len - skip)
@@ -2773,9 +2774,10 @@ static int unix_stream_read_generic(struct unix_stream_read_state *state,
  					 state->msg->msg_name);
  			unix_copy_addr(state->msg, skb->sk);
 
-			BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
-							      state->msg->msg_name,
-							      &state->msg->msg_namelen);
+			if (state->msg->msg_namelen > 0)
+				BPF_CGROUP_RUN_PROG_UNIX_RECVMSG_LOCK(sk,
+								      state->msg->msg_name,
+								      &state->msg->msg_namelen);
 
  			sunaddr = NULL;
  		}
--

2.41.0