Skip to contentSkip to conversation

net: reuseport: honour SO_LOCK_FILTER in SO_DETACH_REUSEPORT_BPF

PATCH In review Suyuan Wang 3 participants 5 messages Nov 22 – Nov 23

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

PATCH 1/1 net: reuseport: honour SO_LOCK_FILTER in SO_DETACH_REUSEPORT_BPF

From: Suyuan Wang <[email protected]>

SO_LOCK_FILTER lets a process lock the filters on a socket before handing it to a less privileged one. Once it is set, the existing attach and detach paths reject changes to the socket's BPF filters with -EPERM, but SO_DETACH_REUSEPORT_BPF does not check SOCK_FILTER_LOCKED. So an unprivileged process that receives a locked socket can still remove the reuseport program, which then changes how packets and connections are spread over the whole reuseport group.

Return -EPERM from reuseport_detach_prog() when the filter is locked, like sk_detach_filter() does.

Found with a static analysis tool and checked by hand.

Fixes 99f3a064bc2e ("bpf: net: Add SO_DETACH_REUSEPORT_BPF")

---
Tested in QEMU (x86_64): root binds a UDP socket with SO_REUSEPORT, attaches a reuseport cBPF program and sets SO_LOCK_FILTER, then a child running as nobody uses the inherited socket.

Before:
SO_ATTACH_REUSEPORT_CBPF: EPERM
SO_DETACH_FILTER: EPERM
SO_DETACH_REUSEPORT_BPF: succeeds, the program is gone

After:

SO_DETACH_REUSEPORT_BPF: EPERM as well

---
 net/core/sock_reuseport.c | 3 +++
 1 file changed, 3 insertions(+)
net/core/sock_reuseport.c +3 -0
index b9e5648fa4c5..cdff61161a70 100644
@@ -727,6 +727,9 @@ int reuseport_detach_prog(struct sock *sk)
 	struct sock_reuseport *reuse;
 	struct bpf_prog *old_prog;
 
+	if (sock_flag(sk, SOCK_FILTER_LOCKED))
+		return -EPERM;
+
 	old_prog = NULL;
 	spin_lock_bh(&reuseport_lock);
 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] net: null pointer dereference in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] net: null pointer dereference in reuseport_attach_prog()
[1]
--

commit da8d8e1657f8b3054e3eb3aca8885a9515a8a782
Author: Suyuan Wang <[email protected]>

net: reuseport: honour SO_LOCK_FILTER in SO_DETACH_REUSEPORT_BPF

This commit checks SOCK_FILTER_LOCKED in reuseport_detach_prog() and returns -EPERM so an unprivileged holder of a filter-locked socket cannot remove the reuseport BPF program via SO_DETACH_REUSEPORT_BPF.

Quoted 5 lines
net/core/sock_reuseport.c +0 -0
index b9e5648fa4c51..cdff61161a70a 100644

net/core/sock_reuseport.c int reuseport_detach_prog(struct sock *sk) · line 735

Quoted 9 lines
net/core/sock_reuseport.c +3 -0
@@ -727,6 +727,9 @@ int reuseport_detach_prog(struct sock *sk)
 	struct sock_reuseport *reuse;
 	struct bpf_prog *old_prog;
 
+	if (sock_flag(sk, SOCK_FILTER_LOCKED))
+		return -EPERM;
+
 	old_prog = NULL;
 	spin_lock_bh(&reuseport_lock);
 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,

[Severity: Critical]
This problem wasn't introduced by this patch, but can reuseport_attach_prog() right above hit a NULL pointer dereference on reuse->prog [1]?

Unlike reuseport_detach_prog(), which re-checks reuse after acquiring reuseport_lock, reuseport_attach_prog() checks sk->sk_reuseport_cb outside the lock on a hashed socket and then dereferences reuse->prog without re-checking reuse:

net/core/sock_reuseport.c:reuseport_attach_prog() {
	...
	} else if (!rcu_access_pointer(sk->sk_reuseport_cb)) {
		/* The socket wasn't bound with SO_REUSEPORT */
		return -EINVAL;
	}

	spin_lock_bh(&reuseport_lock);
	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
					  lockdep_is_held(&reuseport_lock));
	old_prog = rcu_dereference_protected(reuse->prog,
					     lockdep_is_held(&reuseport_lock));
	...
}

If the socket is closed but still hashed and a concurrent reuseport_grow() evicts it from the reuseport group before reuseport_lock is taken, sk->sk_reuseport_cb is cleared to NULL:

CPU0 CPU1

                                  reuseport_attach_prog()
                                    rcu_access_pointer(sk->sk_reuseport_cb)
reuseport_grow()
  rcu_assign_pointer(
    sk->sk_reuseport_cb, NULL)
                                    spin_lock_bh(&reuseport_lock)
                                    reuse = rcu_dereference_protected(...)
                                    reuse->prog  <-- NULL pointer dereference

[1] https://sashiko.dev/#/bug/linux-08c15fbe-8ad8-46d6-a3d8-bd898520f1ab

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Suyuan Wang
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The fix only covers the setsockopt(SO_DETACH_REUSEPORT_BPF) path.

--

net: reuseport: honour SO_LOCK_FILTER in SO_DETACH_REUSEPORT_BPF

This commit makes reuseport_detach_prog() return -EPERM when SOCK_FILTER_LOCKED is set. Without it, an unprivileged process that is handed a locked socket can remove the reuseport program with SO_DETACH_REUSEPORT_BPF. This matches what sk_detach_filter() already does.

net/core/sock_reuseport.c int reuseport_detach_prog(struct sock *sk) · line 732

Quoted 11 lines
net/core/sock_reuseport.c +3 -0
index b9e5648fa4c51..cdff61161a70a 100644
@@ -727,6 +727,9 @@ int reuseport_detach_prog(struct sock *sk)
 	struct sock_reuseport *reuse;
 	struct bpf_prog *old_prog;
 
+	if (sock_flag(sk, SOCK_FILTER_LOCKED))
+		return -EPERM;
+

[Severity: Medium]
This closes the setsockopt(SO_DETACH_REUSEPORT_BPF) path. Can the holder of a locked TCP SO_REUSEPORT listener still drop the program by disconnecting the listener and then calling listen() again?

connect(fd, AF_UNSPEC) or shutdown(fd, SHUT_RD) on a listener goes through this chain, and none of these functions check SOCK_FILTER_LOCKED:

tcp_disconnect()
tcp_set_state(sk, TCP_CLOSE)

    sk->sk_prot->unhash(sk)
      inet_unhash()
        reuseport_stop_listen_sock()
          reuseport_detach_sock()

With net.ipv4.tcp_migrate_req at its default of 0, and a program that is not BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, reuseport_stop_listen_sock() reaches the immediate detach:

net/core/sock_reuseport.c:reuseport_stop_listen_sock() {
    ...
	/* Not capable to do migration, detach immediately */
	reuseport_detach_sock(sk);
    ...
}

reuseport_detach_sock() clears sk->sk_reuseport_cb. If this socket was the only member, it also frees the group:

	if (reuse->num_socks + reuse->num_closed_socks == 0)
		call_rcu(&reuse->rcu, reuseport_free_rcu);

reuseport_free_rcu() then releases the locked program:

	sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));

tcp_set_state() keeps the port bound when SOCK_BINDPORT_LOCK is set, so listen() can be called again on the same port:

		sk->sk_prot->unhash(sk);
		if (inet_csk(sk)->icsk_bind_hash &&
		    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
			inet_put_port(sk);

On the second listen(), inet_reuseport_add_sock() finds no sibling and calls reuseport_alloc(). Because sk_reuseport_cb is now NULL, this creates a new group with prog == NULL. The socket keeps listening on the same port, but the program is gone.

Documentation/networking/filter.rst says that once SO_LOCK_FILTER is set, "a filter cannot be removed or changed" and that the filter "will be kept until the socket is closed".

This only happens when the locked socket is the only member of its group. If it has siblings, re-listen puts it back into the existing group through reuseport_add_sock(). With migration enabled, the socket moves to the closed section and keeps the program.

In a one-socket group, the program matters mostly when an SK_REUSEPORT eBPF program uses SK_DROP to decide which connections are admitted. The group-wide load-balancing change described in the commit message cannot happen through this path.

Should the disconnect and re-listen path also respect SOCK_FILTER_LOCKED, or should the commit message say that only the setsockopt detach path is covered?

Quoted 3 lines
net/core/sock_reuseport.c +0 -0
 	old_prog = NULL;
 	spin_lock_bh(&reuseport_lock);
 	reuse = rcu_dereference_protected(sk->sk_reuseport_cb,
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-reuseport-lock-filter-v1-1-a3e5324c8d55%40gmail.com
author
#3 lore raw
Quoted 2 lines

- [Medium] The fix only covers the setsockopt(SO_DETACH_REUSEPORT_BPF) path.

Thanks for the review. Right, the patch only covers the setsockopt path, and that is on purpose. The disconnect and re-listen case is
different: the socket leaves its group, and when it was the only member the group itself is freed, together with the program that belongs to it. The program is not removed from a group that keeps running. Making that path honour SO_LOCK_FILTER would mean keeping the group alive across a disconnect, which is a larger change than this one.

If you prefer, I can send a v2 that says in the commit message that only SO_DETACH_REUSEPORT_BPF is covered.

Thanks,
Suyuan

Quoted 110 lines · <[email protected]> 于2026年11月23日周一 16:47写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The fix only covers the setsockopt(SO_DETACH_REUSEPORT_BPF) path.

--

net: reuseport: honour SO_LOCK_FILTER in SO_DETACH_REUSEPORT_BPF

This commit makes reuseport_detach_prog() return -EPERM when SOCK_FILTER_LOCKED is set. Without it, an unprivileged process that is handed a locked socket can remove the reuseport program with SO_DETACH_REUSEPORT_BPF. This matches what sk_detach_filter() already does.

net/core/sock_reuseport.c int reuseport_detach_prog(struct sock *sk) · line 732

Quoted 11 lines
net/core/sock_reuseport.c +3 -0
index b9e5648fa4c51..cdff61161a70a 100644
@@ -727,6 +727,9 @@ int reuseport_detach_prog(struct sock *sk)
      struct sock_reuseport *reuse;
      struct bpf_prog *old_prog;
 
+     if (sock_flag(sk, SOCK_FILTER_LOCKED))
+             return -EPERM;
+

[Severity: Medium]
This closes the setsockopt(SO_DETACH_REUSEPORT_BPF) path. Can the holder of a locked TCP SO_REUSEPORT listener still drop the program by disconnecting the listener and then calling listen() again?

connect(fd, AF_UNSPEC) or shutdown(fd, SHUT_RD) on a listener goes through this chain, and none of these functions check SOCK_FILTER_LOCKED:

tcp_disconnect()
tcp_set_state(sk, TCP_CLOSE)

    sk->sk_prot->unhash(sk)
      inet_unhash()
        reuseport_stop_listen_sock()
          reuseport_detach_sock()

With net.ipv4.tcp_migrate_req at its default of 0, and a program that is not BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, reuseport_stop_listen_sock() reaches the immediate detach:

net/core/sock_reuseport.c:reuseport_stop_listen_sock() {
    ...
        /* Not capable to do migration, detach immediately */
        reuseport_detach_sock(sk);
    ...
}

reuseport_detach_sock() clears sk->sk_reuseport_cb. If this socket was the only member, it also frees the group:

        if (reuse->num_socks + reuse->num_closed_socks == 0)
                call_rcu(&reuse->rcu, reuseport_free_rcu);

reuseport_free_rcu() then releases the locked program:

        sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));

tcp_set_state() keeps the port bound when SOCK_BINDPORT_LOCK is set, so listen() can be called again on the same port:

                sk->sk_prot->unhash(sk);
                if (inet_csk(sk)->icsk_bind_hash &&
                    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
                        inet_put_port(sk);

On the second listen(), inet_reuseport_add_sock() finds no sibling and calls reuseport_alloc(). Because sk_reuseport_cb is now NULL, this creates a new group with prog == NULL. The socket keeps listening on the same port, but the program is gone.

Documentation/networking/filter.rst says that once SO_LOCK_FILTER is set, "a filter cannot be removed or changed" and that the filter "will be kept until the socket is closed".

This only happens when the locked socket is the only member of its group. If it has siblings, re-listen puts it back into the existing group through reuseport_add_sock(). With migration enabled, the socket moves to the closed section and keeps the program.

In a one-socket group, the program matters mostly when an SK_REUSEPORT eBPF program uses SK_DROP to decide which connections are admitted. The group-wide load-balancing change described in the commit message cannot happen through this path.

Should the disconnect and re-listen path also respect SOCK_FILTER_LOCKED, or should the commit message say that only the setsockopt detach path is covered?

net/core/sock_reuseport.c int reuseport_detach_prog(struct sock *sk) · line 735

Quoted 3 lines
net/core/sock_reuseport.c +0 -0
      old_prog = NULL;
      spin_lock_bh(&reuseport_lock);
      reuse = rcu_dereference_protected(sk->sk_reuseport_cb,

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-reuseport-lock-filter-v1-1-a3e5324c8d55%40gmail.com

author
#4 lore raw
Quoted 3 lines

[Severity: Critical]
This problem wasn't introduced by this patch, but can reuseport_attach_prog() right above hit a NULL pointer dereference on reuse->prog [1]?

I don't think this can happen. setsockopt() holds the socket lock for the whole SO_ATTACH_REUSEPORT_* call, and a listener can only move to the closed section through shutdown(), connect(AF_UNSPEC) or close() on that same socket, which all take the socket lock first. So the socket cannot be shut down and evicted by reuseport_grow() between the check and the reuseport_lock. I also tried it with a delay added between the
two: shutdown() waits for the attach to finish.

Thanks,
Suyuan

Quoted 129 lines · Wong Steve <[email protected]> 于2026年11月23日周一 17:01写道:
Quoted 3 lines

- [Medium] The fix only covers the setsockopt(SO_DETACH_REUSEPORT_BPF) path.

Thanks for the review. Right, the patch only covers the setsockopt path, and that is on purpose. The disconnect and re-listen case is
different: the socket leaves its group, and when it was the only member the group itself is freed, together with the program that belongs to it. The program is not removed from a group that keeps running. Making that path honour SO_LOCK_FILTER would mean keeping the group alive across a disconnect, which is a larger change than this one.

If you prefer, I can send a v2 that says in the commit message that only SO_DETACH_REUSEPORT_BPF is covered.

Thanks,
Suyuan

Quoted 110 lines · <[email protected]> 于2026年11月23日周一 16:47写道:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] The fix only covers the setsockopt(SO_DETACH_REUSEPORT_BPF) path.

--

net: reuseport: honour SO_LOCK_FILTER in SO_DETACH_REUSEPORT_BPF

This commit makes reuseport_detach_prog() return -EPERM when SOCK_FILTER_LOCKED is set. Without it, an unprivileged process that is handed a locked socket can remove the reuseport program with SO_DETACH_REUSEPORT_BPF. This matches what sk_detach_filter() already does.

net/core/sock_reuseport.c int reuseport_detach_prog(struct sock *sk) · line 732

Quoted 11 lines
net/core/sock_reuseport.c +3 -0
index b9e5648fa4c51..cdff61161a70a 100644
@@ -727,6 +727,9 @@ int reuseport_detach_prog(struct sock *sk)
      struct sock_reuseport *reuse;
      struct bpf_prog *old_prog;
 
+     if (sock_flag(sk, SOCK_FILTER_LOCKED))
+             return -EPERM;
+

[Severity: Medium]
This closes the setsockopt(SO_DETACH_REUSEPORT_BPF) path. Can the holder of a locked TCP SO_REUSEPORT listener still drop the program by disconnecting the listener and then calling listen() again?

connect(fd, AF_UNSPEC) or shutdown(fd, SHUT_RD) on a listener goes through this chain, and none of these functions check SOCK_FILTER_LOCKED:

tcp_disconnect()
tcp_set_state(sk, TCP_CLOSE)

    sk->sk_prot->unhash(sk)
      inet_unhash()
        reuseport_stop_listen_sock()
          reuseport_detach_sock()

With net.ipv4.tcp_migrate_req at its default of 0, and a program that is not BPF_SK_REUSEPORT_SELECT_OR_MIGRATE, reuseport_stop_listen_sock() reaches the immediate detach:

net/core/sock_reuseport.c:reuseport_stop_listen_sock() {
    ...
        /* Not capable to do migration, detach immediately */
        reuseport_detach_sock(sk);
    ...
}

reuseport_detach_sock() clears sk->sk_reuseport_cb. If this socket was the only member, it also frees the group:

        if (reuse->num_socks + reuse->num_closed_socks == 0)
                call_rcu(&reuse->rcu, reuseport_free_rcu);

reuseport_free_rcu() then releases the locked program:

        sk_reuseport_prog_free(rcu_dereference_protected(reuse->prog, 1));

tcp_set_state() keeps the port bound when SOCK_BINDPORT_LOCK is set, so listen() can be called again on the same port:

                sk->sk_prot->unhash(sk);
                if (inet_csk(sk)->icsk_bind_hash &&
                    !(sk->sk_userlocks & SOCK_BINDPORT_LOCK))
                        inet_put_port(sk);

On the second listen(), inet_reuseport_add_sock() finds no sibling and calls reuseport_alloc(). Because sk_reuseport_cb is now NULL, this creates a new group with prog == NULL. The socket keeps listening on the same port, but the program is gone.

Documentation/networking/filter.rst says that once SO_LOCK_FILTER is set, "a filter cannot be removed or changed" and that the filter "will be kept until the socket is closed".

This only happens when the locked socket is the only member of its group. If it has siblings, re-listen puts it back into the existing group through reuseport_add_sock(). With migration enabled, the socket moves to the closed section and keeps the program.

In a one-socket group, the program matters mostly when an SK_REUSEPORT eBPF program uses SK_DROP to decide which connections are admitted. The group-wide load-balancing change described in the commit message cannot happen through this path.

Should the disconnect and re-listen path also respect SOCK_FILTER_LOCKED, or should the commit message say that only the setsockopt detach path is covered?

net/core/sock_reuseport.c int reuseport_detach_prog(struct sock *sk) · line 735

Quoted 3 lines
net/core/sock_reuseport.c +0 -0
      old_prog = NULL;
      spin_lock_bh(&reuseport_lock);
      reuse = rcu_dereference_protected(sk->sk_reuseport_cb,

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261122-reuseport-lock-filter-v1-1-a3e5324c8d55%40gmail.com