* [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
@ 2026-11-25 7:03 Jiayuan Chen
2026-11-25 7:03 ` [PATCH bpf v2 2/2] selftests/bpf: test rejected bpf_setsockopt() in inet_csk_clone hook Jiayuan Chen
2026-11-25 17:04 ` [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Stanislav Fomichev
0 siblings, 2 replies; 5+ messages in thread
From: Jiayuan Chen @ 2026-11-25 7:03 UTC (permalink / raw)
To: bpf, kpsingh, matt
Cc: Jiayuan Chen, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Stanislav Fomichev,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Shuah Khan, Xu Kuohai, netdev, linux-kselftest
security_inet_csk_clone() is called at the end of inet_csk_clone_lock().
The child is locked, which is why the hook is in
bpf_lsm_locked_sockopt_hooks, but it is not set up yet:
icsk->icsk_ca_ops and the ipv6_pinfo still point to the ones of the
listener, and the timers are not initialized.
So bpf_setsockopt() from a lsm_cgroup prog is wrong there for:
- TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no
reference on.
- IPV6_TCLASS: writes to the ipv6_pinfo of the listener.
- SO_KEEPALIVE, TCP_KEEPIDLE: arm the keepalive timer before
tcp_init_xmit_timers() sets it up.
Options considered:
1. Remove the hook from bpf_lsm_locked_sockopt_hooks, as commit
5416c9aea832 ("bpf: Fix the kernel crash caused by bpf_setsockopt().")
did for socket_sock_rcv_skb. Not done, setting options like
SO_PRIORITY on the child is what the hook is used for and it works.
2. Make these options work. This needs the child set up before the
hook, too much for a fix.
So give the hook its own bpf_setsockopt() and return -EOPNOTSUPP for
these options there. bpf_getsockopt() is left as is.
For example, SO_KEEPALIVE ends up with:
Oops: general protection fault, probably for non-canonical address 0xfbd59c0000000025: 0000 [#1] SMP KASAN PTI
KASAN: maybe wild-memory-access in range [0xdead000000000128-0xdead00000000012f]
RIP: 0010:__run_timers+0x5d2/0xab0
Call Trace:
<IRQ>
run_timer_base+0x107/0x190
run_timer_softirq+0x1a/0x40
handle_softirqs+0x251/0xa70
__irq_exit_rcu+0x1a8/0x260
irq_exit_rcu+0xe/0x30
sysvec_apic_timer_interrupt+0xc2/0x110
</IRQ>
Fixes: 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
v1 -> v2 : fix some issues reported by AI.
---
include/linux/bpf.h | 1 +
kernel/bpf/bpf_lsm.c | 13 +++++++++++++
net/core/filter.c | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 56 insertions(+)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 1dd9db16f324..207a3be3138d 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -4159,6 +4159,7 @@ extern const struct bpf_func_proto bpf_task_storage_delete_proto;
extern const struct bpf_func_proto bpf_for_each_map_elem_proto;
extern const struct bpf_func_proto bpf_btf_find_by_name_kind_proto;
extern const struct bpf_func_proto bpf_sk_setsockopt_proto;
+extern const struct bpf_func_proto bpf_sk_clone_setsockopt_proto;
extern const struct bpf_func_proto bpf_sk_getsockopt_proto;
extern const struct bpf_func_proto bpf_sk_setsockopt_nodelay_proto;
extern const struct bpf_func_proto bpf_unlocked_sk_setsockopt_proto;
diff --git a/kernel/bpf/bpf_lsm.c b/kernel/bpf/bpf_lsm.c
index ae23511fd811..bec940efd354 100644
--- a/kernel/bpf/bpf_lsm.c
+++ b/kernel/bpf/bpf_lsm.c
@@ -79,6 +79,16 @@ BTF_ID(func, bpf_lsm_inet_conn_established)
#endif
BTF_SET_END(bpf_lsm_locked_sockopt_hooks)
+/*
+ * List of LSM hooks that trigger on a locked child socket which is not
+ * set up yet, bpf_setsockopt is restricted there.
+ */
+BTF_SET_START(bpf_lsm_clone_sockopt_hooks)
+#ifdef CONFIG_SECURITY_NETWORK
+BTF_ID(func, bpf_lsm_inet_csk_clone)
+#endif
+BTF_SET_END(bpf_lsm_clone_sockopt_hooks)
+
/* List of LSM hooks that trigger while the socket is _not_ locked,
* but it's ok to call bpf_{g,s}etsockopt because the socket is still
* in the early init phase.
@@ -282,6 +292,9 @@ bpf_lsm_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
case BPF_FUNC_setsockopt:
if (prog->expected_attach_type != BPF_LSM_CGROUP)
return NULL;
+ if (btf_id_set_contains(&bpf_lsm_clone_sockopt_hooks,
+ prog->aux->attach_btf_id))
+ return &bpf_sk_clone_setsockopt_proto;
if (btf_id_set_contains(&bpf_lsm_locked_sockopt_hooks,
prog->aux->attach_btf_id))
return &bpf_sk_setsockopt_proto;
diff --git a/net/core/filter.c b/net/core/filter.c
index 53c042b267ab..f17b3c5dd473 100644
--- a/net/core/filter.c
+++ b/net/core/filter.c
@@ -5938,6 +5938,48 @@ const struct bpf_func_proto bpf_sk_setsockopt_proto = {
.arg5_type = ARG_MEM_SIZE,
};
+/*
+ * security_inet_csk_clone() runs at the end of inet_csk_clone_lock(), the
+ * child is locked but not set up yet: its timers are not initialized, it
+ * holds no reference on icsk_ca_ops, an IPv6 child still points to the
+ * ipv6_pinfo of the listener and a MPTCP subflow has no msk.
+ */
+BPF_CALL_5(bpf_sk_clone_setsockopt, struct sock *, sk, int, level,
+ int, optname, char *, optval, int, optlen)
+{
+ bool mptcp = sk_fullsock(sk) && sk_is_tcp(sk) && sk_is_mptcp(sk);
+
+ switch (level) {
+ case SOL_SOCKET:
+ if (optname == SO_KEEPALIVE ||
+ (optname == SO_SNDBUF && mptcp))
+ return -EOPNOTSUPP;
+ break;
+ case SOL_TCP:
+ if (optname == TCP_KEEPIDLE || optname == TCP_CONGESTION ||
+ (optname == TCP_NOTSENT_LOWAT && mptcp))
+ return -EOPNOTSUPP;
+ break;
+ case SOL_IPV6:
+ if (optname == IPV6_TCLASS)
+ return -EOPNOTSUPP;
+ break;
+ }
+
+ return _bpf_setsockopt(sk, level, optname, optval, optlen);
+}
+
+const struct bpf_func_proto bpf_sk_clone_setsockopt_proto = {
+ .func = bpf_sk_clone_setsockopt,
+ .gpl_only = false,
+ .ret_type = RET_INTEGER,
+ .arg1_type = ARG_PTR_TO_BTF_ID_SOCK_COMMON,
+ .arg2_type = ARG_ANYTHING,
+ .arg3_type = ARG_ANYTHING,
+ .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY,
+ .arg5_type = ARG_MEM_SIZE,
+};
+
BPF_CALL_5(bpf_sk_getsockopt, struct sock *, sk, int, level,
int, optname, char *, optval, int, optlen)
{
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH bpf v2 2/2] selftests/bpf: test rejected bpf_setsockopt() in inet_csk_clone hook
2026-11-25 7:03 [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Jiayuan Chen
@ 2026-11-25 7:03 ` Jiayuan Chen
2026-11-25 17:04 ` [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Stanislav Fomichev
1 sibling, 0 replies; 5+ messages in thread
From: Jiayuan Chen @ 2026-11-25 7:03 UTC (permalink / raw)
To: bpf, kpsingh, matt
Cc: Jiayuan Chen, Alexei Starovoitov, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Stanislav Fomichev,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Shuah Khan, Xu Kuohai, netdev, linux-kselftest
Check in the lsm_cgroup/inet_csk_clone prog that SO_KEEPALIVE,
TCP_CONGESTION and IPV6_TCLASS are rejected with -EOPNOTSUPP.
Signed-off-by: Jiayuan Chen <jiayuan.chen@linux.dev>
---
tools/testing/selftests/bpf/progs/lsm_cgroup.c | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
diff --git a/tools/testing/selftests/bpf/progs/lsm_cgroup.c b/tools/testing/selftests/bpf/progs/lsm_cgroup.c
index 3bfa479104be..f4e3278d46d1 100644
--- a/tools/testing/selftests/bpf/progs/lsm_cgroup.c
+++ b/tools/testing/selftests/bpf/progs/lsm_cgroup.c
@@ -23,6 +23,10 @@ extern bool CONFIG_SECURITY_APPARMOR __kconfig __weak;
#define EPERM 1
#endif
+#ifndef EOPNOTSUPP
+#define EOPNOTSUPP 95
+#endif
+
struct {
__uint(type, BPF_MAP_TYPE_CGROUP_STORAGE);
__type(key, __u64);
@@ -168,7 +172,9 @@ int BPF_PROG(socket_alloc, struct sock *sk, int family, gfp_t priority)
SEC("lsm_cgroup/inet_csk_clone")
int BPF_PROG(socket_clone, struct sock *newsk, const struct request_sock *req)
{
+ char cc[] = "reno";
int prio = 234;
+ int one = 1;
if (!newsk)
return 1;
@@ -184,6 +190,17 @@ int BPF_PROG(socket_clone, struct sock *newsk, const struct request_sock *req)
if (prio != 234)
return 1;
+ /* The child is not set up yet, these are rejected. */
+ if (bpf_setsockopt(newsk, SOL_SOCKET, SO_KEEPALIVE, &one,
+ sizeof(one)) != -EOPNOTSUPP)
+ return 1;
+ if (bpf_setsockopt(newsk, SOL_TCP, TCP_CONGESTION, cc,
+ sizeof(cc)) != -EOPNOTSUPP)
+ return 1;
+ if (bpf_setsockopt(newsk, SOL_IPV6, IPV6_TCLASS, &one,
+ sizeof(one)) != -EOPNOTSUPP)
+ return 1;
+
/* Can access cgroup local storage. */
if (!test_local_storage())
return 1;
--
2.43.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
2026-11-25 7:03 [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Jiayuan Chen
2026-11-25 7:03 ` [PATCH bpf v2 2/2] selftests/bpf: test rejected bpf_setsockopt() in inet_csk_clone hook Jiayuan Chen
@ 2026-11-25 17:04 ` Stanislav Fomichev
2026-11-26 12:02 ` Jiayuan Chen
2026-11-28 0:31 ` Alexei Starovoitov
1 sibling, 2 replies; 5+ messages in thread
From: Stanislav Fomichev @ 2026-11-25 17:04 UTC (permalink / raw)
To: Jiayuan Chen
Cc: bpf, kpsingh, matt, Alexei Starovoitov, Daniel Borkmann,
John Fastabend, Andrii Nakryiko, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
Stanislav Fomichev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan, Xu Kuohai, netdev,
linux-kselftest
On 11/25, Jiayuan Chen wrote:
> security_inet_csk_clone() is called at the end of inet_csk_clone_lock().
> The child is locked, which is why the hook is in
> bpf_lsm_locked_sockopt_hooks, but it is not set up yet:
> icsk->icsk_ca_ops and the ipv6_pinfo still point to the ones of the
> listener, and the timers are not initialized.
>
> So bpf_setsockopt() from a lsm_cgroup prog is wrong there for:
>
> - TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no
> reference on.
> - IPV6_TCLASS: writes to the ipv6_pinfo of the listener.
> - SO_KEEPALIVE, TCP_KEEPIDLE: arm the keepalive timer before
> tcp_init_xmit_timers() sets it up.
>
> Options considered:
> 1. Remove the hook from bpf_lsm_locked_sockopt_hooks, as commit
> 5416c9aea832 ("bpf: Fix the kernel crash caused by bpf_setsockopt().")
> did for socket_sock_rcv_skb. Not done, setting options like
> SO_PRIORITY on the child is what the hook is used for and it works.
[..]
> 2. Make these options work. This needs the child set up before the
> hook, too much for a fix.
What is too much? Moving security_inet_csk_clone to happen at the end of
tcp_create_openreq_child? Or is it more involved?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
2026-11-25 17:04 ` [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Stanislav Fomichev
@ 2026-11-26 12:02 ` Jiayuan Chen
2026-11-28 0:31 ` Alexei Starovoitov
1 sibling, 0 replies; 5+ messages in thread
From: Jiayuan Chen @ 2026-11-26 12:02 UTC (permalink / raw)
To: Stanislav Fomichev
Cc: bpf, kpsingh, matt, Alexei Starovoitov, Daniel Borkmann,
John Fastabend, Andrii Nakryiko, Eduard Zingerman,
Kumar Kartikeya Dwivedi, Martin KaFai Lau, Song Liu,
Yonghong Song, Jiri Olsa, Emil Tsalapatis, Ihor Solodrai,
Stanislav Fomichev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan, Xu Kuohai, netdev,
linux-kselftest
On 11/26/26 1:04 AM, Stanislav Fomichev wrote:
> On 11/25, Jiayuan Chen wrote:
>> security_inet_csk_clone() is called at the end of inet_csk_clone_lock().
>> The child is locked, which is why the hook is in
>> bpf_lsm_locked_sockopt_hooks, but it is not set up yet:
>> icsk->icsk_ca_ops and the ipv6_pinfo still point to the ones of the
>> listener, and the timers are not initialized.
>>
>> So bpf_setsockopt() from a lsm_cgroup prog is wrong there for:
>>
>> - TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no
>> reference on.
>> - IPV6_TCLASS: writes to the ipv6_pinfo of the listener.
>> - SO_KEEPALIVE, TCP_KEEPIDLE: arm the keepalive timer before
>> tcp_init_xmit_timers() sets it up.
>>
>> Options considered:
>> 1. Remove the hook from bpf_lsm_locked_sockopt_hooks, as commit
>> 5416c9aea832 ("bpf: Fix the kernel crash caused by bpf_setsockopt().")
>> did for socket_sock_rcv_skb. Not done, setting options like
>> SO_PRIORITY on the child is what the hook is used for and it works.
>
> [..]
>
>> 2. Make these options work. This needs the child set up before the
>> hook, too much for a fix.
>
> What is too much? Moving security_inet_csk_clone to happen at the end of
> tcp_create_openreq_child? Or is it more involved?
It is more involved. The end of tcp_create_openreq_child() only fixes
the timers. icsk_ca_ops is set by tcp_ca_openreq_child() and pinet6 is
switched in tcp_v6_syn_recv_sock(), both after
tcp_create_openreq_child() returns. For MPTCP, subflow->conn is set
even later, in subflow_syn_recv_sock().
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
2026-11-25 17:04 ` [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Stanislav Fomichev
2026-11-26 12:02 ` Jiayuan Chen
@ 2026-11-28 0:31 ` Alexei Starovoitov
1 sibling, 0 replies; 5+ messages in thread
From: Alexei Starovoitov @ 2026-11-28 0:31 UTC (permalink / raw)
To: Stanislav Fomichev, Jiayuan Chen
Cc: bpf, kpsingh, matt, Daniel Borkmann, John Fastabend,
Andrii Nakryiko, Eduard Zingerman, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Ihor Solodrai, Stanislav Fomichev,
David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
Simon Horman, Shuah Khan, Xu Kuohai, netdev, linux-kselftest
On Wed, Nov 25, 2026 at 09:04 AM Stanislav Fomichev <sdf.kernel@gmail.com> wrote:
> On 11/25, Jiayuan Chen wrote:
>> 2. Make these options work. This needs the child set up before the
>> hook, too much for a fix.
>
> What is too much? Moving security_inet_csk_clone to happen at the end of
> tcp_create_openreq_child? Or is it more involved?
Here is what my bot thinks about moving the hook.
That spot fixes the timers only.
The callers set up the rest:
call chain option works after it
----------------------------------------- ----------------------------
subflow_syn_recv_sock()
tcp_v6_syn_recv_sock()
tcp_create_openreq_child()
inet_csk_clone_lock() <- the hook is here today
tcp_init_xmit_timers() SO_KEEPALIVE, TCP_KEEPIDLE
newinet->pinet6 = tcp_inet6_sk(newsk)
memcpy(newnp, np) IPV6_TCLASS
tcp_ca_openreq_child() TCP_CONGESTION
__inet_inherit_port()
inet_ehash_nolisten()
ctx->conn = mptcp_sk_clone_init() SO_SNDBUF, TCP_NOTSENT_LOWAT
There are two callers, v4 and v6. Let's call the hook right before
__inet_inherit_port() in both. Then there is no need for
bpf_sk_clone_setsockopt() and bpf_lsm_clone_sockopt_hooks.
SELinux and Smack shouldn't care. They copy labels from req.
The v4 route lookup in between takes secid from req too.
mptcp sets ctx->conn after inet_ehash_nolisten(). That's too late for
the hook. subflow_write_space() needs a NULL check instead.
bpf CC hits the same NULL and the patch doesn't cover it:
tcp_ca_openreq_child()
tcp_set_ca_state(sk, TCP_CA_Open)
set_state prog: bpf_setsockopt(sk, SOL_SOCKET, SO_SNDBUF)
subflow_write_space(sk)
mptcp_write_space(NULL)
With the move SO_MARK and IP_TOS drop the cached dst of the child.
tcp_init_transfer() looks it up again.
Backports need more. dccp and chtls need their own call.
IPV6=m needs an export.
This replaces patch 1 and goes on top of bpf/master 14f6b10422d6.
diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
index 9a3b05e..759f826 100644
--- a/net/ipv4/inet_connection_sock.c
+++ b/net/ipv4/inet_connection_sock.c
@@ -1276,8 +1276,6 @@ struct sock *inet_csk_clone_lock(const struct sock *sk,
inet_clone_ulp(req, newsk, priority);
- security_inet_csk_clone(newsk, req);
-
return newsk;
}
diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 2fc92a7..d03b50b 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -1761,6 +1761,9 @@ struct sock *tcp_v4_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
goto put_and_exit; /* OOM, release back memory */
#endif
+ /* The child is set up, but is not in bhash and ehash yet. */
+ security_inet_csk_clone(newsk, req);
+
if (__inet_inherit_port(sk, newsk) < 0)
goto put_and_exit;
*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash),
diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c
index 5c7ed38..d704d98 100644
--- a/net/ipv6/tcp_ipv6.c
+++ b/net/ipv6/tcp_ipv6.c
@@ -1516,6 +1516,9 @@ static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *
goto put_and_exit; /* OOM */
#endif
+ /* The child is set up, but is not in bhash and ehash yet. */
+ security_inet_csk_clone(newsk, req);
+
if (__inet_inherit_port(sk, newsk) < 0)
goto put_and_exit;
*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash),
diff --git a/net/mptcp/subflow.c b/net/mptcp/subflow.c
index 00677fb..a0fd3ce 100644
--- a/net/mptcp/subflow.c
+++ b/net/mptcp/subflow.c
@@ -1602,6 +1602,10 @@ static void subflow_write_space(struct sock *ssk)
{
struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
+ /* bpf_setsockopt() on a passive subflow that has no msk yet */
+ if (!sk)
+ return;
+
mptcp_propagate_sndbuf(sk, ssk);
mptcp_write_space(sk);
}
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-28 0:31 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-25 7:03 [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Jiayuan Chen
2026-11-25 7:03 ` [PATCH bpf v2 2/2] selftests/bpf: test rejected bpf_setsockopt() in inet_csk_clone hook Jiayuan Chen
2026-11-25 17:04 ` [PATCH bpf v2 1/2] bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone Stanislav Fomichev
2026-11-26 12:02 ` Jiayuan Chen
2026-11-28 0:31 ` Alexei Starovoitov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox