Skip to contentSkip to conversation

net: sock_reuseport: check program type on the migration path

PATCH Superseded Shihuang Liu 4 participants 6 messages Nov 15 – Nov 18

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: sock_reuseport: check program type on the migration path

From: Linux kernel audit <[email protected]>

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group; both steps are allowed without CAP_BPF. Closing a listener that still has queued children then runs that socket filter with a struct sk_reuseport_kern as its context, while the program was verified against struct sk_buff. It reads and writes past the 56-byte object on the stack, which KASAN reports as a stack-out-of-bounds in ___bpf_prog_run().

The cause is that reuseport_migrate_sock() decides from prog->expected_attach_type alone. That field is supplied by user space and does not identify the program type, and bpf_prog_load_check_attach() does not reject the value for SOCKET_FILTER programs.

Fix this by requiring BPF_PROG_TYPE_SK_REUSEPORT before running the program, and use the same check in reuseport_stop_listen_sock(). Socket filters can still be attached and used as selectors; the selection path is unchanged.

Fixes d5e4ddaeb6ab ("bpf: Support socket migration by eBPF.")
---
 net/core/sock_reuseport.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
net/core/sock_reuseport.c +14 -2
@@ -460,6 +460,18 @@
 }
 EXPORT_SYMBOL(reuseport_detach_sock);
 
+/*
+ * Only a BPF_PROG_TYPE_SK_REUSEPORT program is verified against
+ * struct sk_reuseport_kern, which is the context bpf_run_sk_reuseport()
+ * passes to it.  expected_attach_type is set by user space and does not
+ * identify the program type.
+ */
+static bool reuseport_is_migration_prog(const struct bpf_prog *prog)
+{
+	return prog && prog->type == BPF_PROG_TYPE_SK_REUSEPORT &&
+	       prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE;
+}
+
 void reuseport_stop_listen_sock(struct sock *sk)
 {
 	if (sk->sk_protocol == IPPROTO_TCP) {
@@ -474,7 +486,7 @@
 						 lockdep_is_held(&reuseport_lock));
 
 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req) ||
-		    (prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
+		    reuseport_is_migration_prog(prog)) {
 			/* Migration capable, move sk from the listening section
 			 * to the closed section.
 			 */
@@ -644,7 +656,7 @@
 
 	hash = migrating_sk->sk_hash;
 	prog = rcu_dereference(reuse->prog);
-	if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
+	if (!reuseport_is_migration_prog(prog)) {
 		if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req))
 			goto select_by_hash;
 		goto failure;
#1 lore raw
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

Hi!

This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:

- How the issue was discovered, e.g. hit in production, hit during development, syzbot report, manual code inspection, LLM or static analysis tool scan.

Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.

*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.

Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think
that is the case please reply and explain.
#2 lore raw
In reply to Shihuang Liu
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] net: sock_reuseport: placeholder Signed-off-by trailer in commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] net: sock_reuseport: placeholder Signed-off-by trailer in commit message
--

commit cfe7ad076364a4b63b18cb16611dd2ffb1e3f230
Author: Linux kernel audit <[email protected]>

Quoted 17 lines

net: sock_reuseport: check program type on the migration path

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group; both steps are allowed without CAP_BPF. Closing a listener that still has queued children then runs that socket filter with a struct sk_reuseport_kern as its context, while the program was verified against struct sk_buff. It reads and writes past the 56-byte object on the stack, which KASAN reports as a stack-out-of-bounds in ___bpf_prog_run().

The cause is that reuseport_migrate_sock() decides from prog->expected_attach_type alone. That field is supplied by user space and does not identify the program type, and bpf_prog_load_check_attach() does not reject the value for SOCKET_FILTER programs.

Quoted 7 lines

Fix this by requiring BPF_PROG_TYPE_SK_REUSEPORT before running the program, and use the same check in reuseport_stop_listen_sock(). Socket filters can still be attached and used as selectors; the selection path is unchanged.

Fixes d5e4ddaeb6ab ("bpf: Support socket migration by eBPF.")

[Severity: High]
The Signed-off-by trailer uses a placeholder identity and a non-routable .invalid email address instead of a real developer name and routable email address as required by the Developer's Certificate of Origin.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw
In reply to Shihuang Liu
Quoted 18 lines · Shihuang Liu

From: Linux kernel audit <[email protected]>

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group; both steps are allowed without CAP_BPF. Closing a listener that still has queued children then runs that socket filter with a struct sk_reuseport_kern as its context, while the program was verified against struct sk_buff. It reads and writes past the 56-byte object on the stack, which KASAN reports as a stack-out-of-bounds in ___bpf_prog_run().

The cause is that reuseport_migrate_sock() decides from prog->expected_attach_type alone. That field is supplied by user space and does not identify the program type, and bpf_prog_load_check_attach() does not reject the value for SOCKET_FILTER programs.

Quoted 8 lines

Fix this by requiring BPF_PROG_TYPE_SK_REUSEPORT before running the program, and use the same check in reuseport_stop_listen_sock(). Socket filters can still be attached and used as selectors; the selection path is unchanged.

Fixes d5e4ddaeb6ab ("bpf: Support socket migration by eBPF.")

---

My mistake — I got the From: and Signed-off-by: wrong in v1. Code and commit message are unchanged; v2 will fix that.

Thanks
Shihuang

Quoted 42 lines
 net/core/sock_reuseport.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
net/core/sock_reuseport.c +14 -2
@@ -460,6 +460,18 @@
 }
 EXPORT_SYMBOL(reuseport_detach_sock);
 
+/*
+ * Only a BPF_PROG_TYPE_SK_REUSEPORT program is verified against
+ * struct sk_reuseport_kern, which is the context bpf_run_sk_reuseport()
+ * passes to it.  expected_attach_type is set by user space and does not
+ * identify the program type.
+ */
+static bool reuseport_is_migration_prog(const struct bpf_prog *prog)
+{
+       return prog && prog->type == BPF_PROG_TYPE_SK_REUSEPORT &&
+              prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE;
+}
+
 void reuseport_stop_listen_sock(struct sock *sk)
 {
        if (sk->sk_protocol == IPPROTO_TCP) {
@@ -474,7 +486,7 @@
                                                 lockdep_is_held(&reuseport_lock));
 
                if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req) ||
-                   (prog && prog->expected_attach_type == BPF_SK_REUSEPORT_SELECT_OR_MIGRATE)) {
+                   reuseport_is_migration_prog(prog)) {
                        /* Migration capable, move sk from the listening section
                         * to the closed section.
                         */
@@ -644,7 +656,7 @@
 
        hash = migrating_sk->sk_hash;
        prog = rcu_dereference(reuse->prog);
-       if (!prog || prog->expected_attach_type != BPF_SK_REUSEPORT_SELECT_OR_MIGRATE) {
+       if (!reuseport_is_migration_prog(prog)) {
                if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_migrate_req))
                        goto select_by_hash;
                goto failure;
#4 lore raw
In reply to Shihuang Liu
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider.

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

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

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).

- [High] net: sock_reuseport: placeholder Signed-off-by trailer in commit message
- [Low] The commit message says loading a BPF_PROG_TYPE_SOCKET_FILTER program with expected_attach_type=BPF_SK_REUSEPORT_SELECT_OR_MIGRATE…

--

net: sock_reuseport: check program type on the migration path

This commit makes reuseport_migrate_sock() and reuseport_stop_listen_sock() treat a reuseport program as migration capable only when its type is BPF_PROG_TYPE_SK_REUSEPORT. Checking expected_attach_type alone allowed a SOCKET_FILTER program to run with a struct sk_reuseport_kern context.

Quoted 4 lines

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group; both steps are allowed without CAP_BPF.

[Severity: Low]
Is "both steps are allowed without CAP_BPF" accurate for a default configuration?

In bpf_prog_load() in kernel/bpf/syscall.c, this check runs before the SOCKET_FILTER/CGROUP_SKB exemption for unprivileged loads:

kernel/bpf/syscall.c:bpf_prog_load() {
    ...
	if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
		goto put_token;
    ...
}

The sysctl starts at 2 when CONFIG_BPF_UNPRIV_DEFAULT_OFF is enabled:

int sysctl_unprivileged_bpf_disabled __read_mostly =

	IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;

That Kconfig option is "default y" in kernel/bpf/Kconfig. So on a default kernel, a task without CAP_BPF (or a BPF token granting it) can't load the socket filter in the first place. Unprivileged users can only reach the out-of-bounds access when kernel.unprivileged_bpf_disabled has been set to 0.

Could the commit message mention that the unprivileged case depends on this sysctl? That would help backporters and security triage judge the exposure. The code change itself is not affected by this.

[Severity: High]
The author and Signed-off-by trailer both use the identity Linux kernel audit <[email protected]>. That isn't a person, and the .invalid TLD is reserved (RFC 2606), so the address can never receive mail.

Signed-off-by is the Developer's Certificate of Origin described in Documentation/process/submitting-patches.rst. It certifies that a specific person wrote the change or has the right to pass it on under the kernel's license. A placeholder identity can't make that certification. Nobody can be reached at this address if questions about the patch come up later, so this can't be applied as is.

Could you resend with the author and Signed-off-by set to your real name and a working email address? If you're forwarding a change written by someone else, it needs the original author's sign-off followed by your own. The code change itself is not affected by this.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261115152408.48205-1-shlomojune6%40gmail.com
author
#5 lore raw
Quoted 73 lines · On Tue, 17 Nov 2026 at 23:27, <[email protected]> wrote:

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

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

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).

- [High] net: sock_reuseport: placeholder Signed-off-by trailer in commit message
- [Low] The commit message says loading a BPF_PROG_TYPE_SOCKET_FILTER program with expected_attach_type=BPF_SK_REUSEPORT_SELECT_OR_MIGRATE…

--

net: sock_reuseport: check program type on the migration path

This commit makes reuseport_migrate_sock() and reuseport_stop_listen_sock() treat a reuseport program as migration capable only when its type is BPF_PROG_TYPE_SK_REUSEPORT. Checking expected_attach_type alone allowed a SOCKET_FILTER program to run with a struct sk_reuseport_kern context.

Quoted 4 lines

A BPF_PROG_TYPE_SOCKET_FILTER program can be loaded with expected_attach_type set to BPF_SK_REUSEPORT_SELECT_OR_MIGRATE and attached to a SO_REUSEPORT TCP group; both steps are allowed without CAP_BPF.

[Severity: Low]
Is "both steps are allowed without CAP_BPF" accurate for a default configuration?

In bpf_prog_load() in kernel/bpf/syscall.c, this check runs before the SOCKET_FILTER/CGROUP_SKB exemption for unprivileged loads:

kernel/bpf/syscall.c:bpf_prog_load() {
    ...
        if (sysctl_unprivileged_bpf_disabled && !bpf_cap)
                goto put_token;
    ...
}

The sysctl starts at 2 when CONFIG_BPF_UNPRIV_DEFAULT_OFF is enabled:

int sysctl_unprivileged_bpf_disabled __read_mostly =

        IS_BUILTIN(CONFIG_BPF_UNPRIV_DEFAULT_OFF) ? 2 : 0;

That Kconfig option is "default y" in kernel/bpf/Kconfig. So on a default kernel, a task without CAP_BPF (or a BPF token granting it) can't load the socket filter in the first place. Unprivileged users can only reach the out-of-bounds access when kernel.unprivileged_bpf_disabled has been set to 0.

Could the commit message mention that the unprivileged case depends on this sysctl? That would help backporters and security triage judge the exposure. The code change itself is not affected by this.

[Severity: High]
The author and Signed-off-by trailer both use the identity Linux kernel audit <[email protected]>. That isn't a person, and the .invalid TLD is reserved (RFC 2606), so the address can never receive mail.

Signed-off-by is the Developer's Certificate of Origin described in Documentation/process/submitting-patches.rst. It certifies that a specific person wrote the change or has the right to pass it on under the kernel's license. A placeholder identity can't make that certification. Nobody can be reached at this address if questions about the patch come up later, so this can't be applied as is.

Quoted 7 lines

Could you resend with the author and Signed-off-by set to your real name and a working email address? If you're forwarding a change written by someone else, it needs the original author's sign-off followed by your own. The code change itself is not affected by this.

--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261115152408.48205-1-shlomojune6%40gmail.com

Will fix it.

pw-bot: cr