Skip to contentSkip to conversation

bpf: Misc changes around AF_UNIX.

PATCH In review Kuniyuki Iwashima 3 participants 5 messages Feb 3 – Feb 4

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

cover bpf: Misc changes around AF_UNIX.

Patch 1 adapts sk_is_XXX() helpers in __cgroup_bpf_run_filter_sock_addr(). Patch 2 removes an unnecessary sk_fullsock() in bpf_skc_to_unix_sock().

Kuniyuki Iwashima (2):
bpf: Use sk_is_inet() and sk_is_unix() in

    __cgroup_bpf_run_filter_sock_addr().

bpf: Don't check sk_fullsock() in bpf_skc_to_unix_sock().

 kernel/bpf/cgroup.c | 6 +-----
 net/core/filter.c   | 2 +-
 2 files changed, 2 insertions(+), 6 deletions(-)
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#1 lore raw

PATCH 1/2 bpf: Use sk_is_inet() and sk_is_unix() in __cgroup_bpf_run_filter_sock_addr().

sk->sk_family should be read with READ_ONCE() in __cgroup_bpf_run_filter_sock_addr() due to IPV6_ADDRFORM.

Also, the comment there is a bit stale since commit 859051dd165e ("bpf: Implement cgroup sockaddr hooks for unix sockets"), and the kdoc has the same comment.

Let's use sk_is_inet() and sk_is_unix() and remove the comment.

---
 kernel/bpf/cgroup.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
kernel/bpf/cgroup.c +1 -5
index 69988af44b37..b029f0369ecf 100644
@@ -1680,11 +1680,7 @@ int __cgroup_bpf_run_filter_sock_addr(struct sock *sk,
 	struct cgroup *cgrp;
 	int ret;
 
-	/* Check socket family since not all sockets represent network
-	 * endpoint (e.g. AF_UNIX).
-	 */
-	if (sk->sk_family != AF_INET && sk->sk_family != AF_INET6 &&
-	    sk->sk_family != AF_UNIX)
+	if (!sk_is_inet(sk) && !sk_is_unix(sk))
 		return 0;
 
 	if (!ctx.uaddr) {
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
#2 lore raw

PATCH 2/2 bpf: Don't check sk_fullsock() in bpf_skc_to_unix_sock().

In reply to Kuniyuki Iwashima

AF_UNIX does not use TCP_NEW_SYN_RECV nor TCP_TIME_WAIT and checking sk->sk_family is sufficient.

Let's remove sk_fullsock() and use sk_is_unix() in bpf_skc_to_unix_sock().

---
 net/core/filter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/core/filter.c +1 -1
index f04982d79d72..90bfe5a6c1db 100644
@@ -12021,7 +12021,7 @@ BPF_CALL_1(bpf_skc_to_unix_sock, struct sock *, sk)
 	 * trigger an explicit type generation here.
 	 */
 	BTF_TYPE_EMIT(struct unix_sock);
-	if (sk && sk_fullsock(sk) && sk->sk_family == AF_UNIX)
+	if (sk && sk_is_unix(sk))
 		return (unsigned long)sk;
 
 	return (unsigned long)NULL;
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Acked-by on the v1 series
#3 lore raw
In reply to Kuniyuki Iwashima
Quoted 8 lines · Kuniyuki Iwashima

Patch 1 adapts sk_is_XXX() helpers in __cgroup_bpf_run_filter_sock_addr(). Patch 2 removes an unnecessary sk_fullsock() in bpf_skc_to_unix_sock().

Kuniyuki Iwashima (2):
bpf: Use sk_is_inet() and sk_is_unix() in

    __cgroup_bpf_run_filter_sock_addr().

bpf: Don't check sk_fullsock() in bpf_skc_to_unix_sock().

#4 lore raw
In reply to Kuniyuki Iwashima
Quoted 11 lines · Kuniyuki Iwashima

Patch 1 adapts sk_is_XXX() helpers in __cgroup_bpf_run_filter_sock_addr(). Patch 2 removes an unnecessary sk_fullsock() in bpf_skc_to_unix_sock().

Kuniyuki Iwashima (2):
bpf: Use sk_is_inet() and sk_is_unix() in

     __cgroup_bpf_run_filter_sock_addr().

bpf: Don't check sk_fullsock() in bpf_skc_to_unix_sock().

  kernel/bpf/cgroup.c | 6 +-----
  net/core/filter.c   | 2 +-

The bot is slow. The change in filter.c is small and the cycle is closing, so I have landed them to the bpf_next/master.

I just noticed the netdev list is not in cc, so added it here also.