bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
Subsystems (8)
PATCH 1/2 bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
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, IPV6_AUTOFLOWLABEL: write 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>
--- include/linux/bpf.h | 1 + kernel/bpf/bpf_lsm.c | 12 ++++++++++++ net/core/filter.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+)
include/linux/bpf.h +1 -0
@@ -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;
kernel/bpf/bpf_lsm.c +12 -0
@@ -79,6 +79,15 @@ 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 +291,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;
net/core/filter.c +37 -0
@@ -5938,6 +5938,43 @@ 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 and an IPv6 child still points to the +* ipv6_pinfo of the listener. +*/ +BPF_CALL_5(bpf_sk_clone_setsockopt, struct sock *, sk, int, level, + int, optname, char *, optval, int, optlen) +{ +switch (level) { +case SOL_SOCKET : +if (optname ==SO_KEEPALIVE ) +return -EOPNOTSUPP ; +break ; +case SOL_TCP : +if (optname ==TCP_KEEPIDLE || optname ==TCP_CONGESTION ) +return -EOPNOTSUPP ; +break ; +case SOL_IPV6 : +if (optname ==IPV6_TCLASS || optname ==IPV6_AUTOFLOWLABEL ) +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) {
Signature · 1 line
2.43.0
PATCH 2/2 selftests/bpf: test rejected bpf_setsockopt() in inet_csk_clone hook
Check in the lsm_cgroup/inet_csk_clone prog that SO_KEEPALIVE, TCP_CONGESTION and IPV6_TCLASS are rejected with -EOPNOTSUPP.
--- tools/testing/selftests/bpf/progs/lsm_cgroup.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
tools/testing/selftests/bpf/progs/lsm_cgroup.c +17 -0
@@ -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 ;
Signature · 1 line
2.43.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [High] bpf: NULL deref in bpf_sk_clone_setsockopt() via MPTCP subflow_write_space() - [Medium] bpf…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] bpf: NULL deref in bpf_sk_clone_setsockopt() via MPTCP subflow_write_space()
- [Medium] bpf: unnecessary rejection of IPV6_AUTOFLOWLABEL in bpf_sk_clone_setsockopt()
- [Low] bpf: multi-line comment style violation in bpf_lsm.c and filter.c
--
commit 9470b8f4e13b2c8be2cf5cc10f852d869dbb6f7c
Author: Jiayuan Chen <[email protected]>
bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
This commit introduces a dedicated bpf_setsockopt() helper for the bpf_lsm_inet_csk_clone hook that rejects socket options unsafe to set on a partially initialized cloned child socket.
kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_inet_conn_established) · line 84
Quoted 11 lines
kernel/bpf/bpf_lsm.c +3
-0@@ -79,6 +79,15 @@ 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. +*/
[Severity: Low]
This isn't a bug, but BPF multi-line comments place the opening /* on its own line (here and above bpf_sk_clone_setsockopt() in net/core/filter.c).
[ ... ]
Quoted 15 lines
net/core/filter.c +7
-0@@ -5938,6 +5938,43 @@ 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 and an IPv6 child still points to the +* ipv6_pinfo of the listener. +*/ +BPF_CALL_5(bpf_sk_clone_setsockopt, struct sock *, sk, int, level, + int, optname, char *, optval, int, optlen)
net/core/filter.c const struct bpf_func_proto bpf_sk_setsockopt_proto = { · line 5957
Quoted 10 lines
net/core/filter.c +10
-0+{ +switch (level) { +case SOL_SOCKET : +if (optname ==SO_KEEPALIVE ) +return -EOPNOTSUPP ; +break ; +case SOL_TCP : +if (optname ==TCP_KEEPIDLE || optname ==TCP_CONGESTION ) +return -EOPNOTSUPP ; +break ;
[Severity: High]
Should bpf_sk_clone_setsockopt() also reject SO_SNDBUF and TCP_NOTSENT_LOWAT?
When an incoming MPTCP subflow is cloned in inet_csk_clone_lock(), inet_clone_ulp() -> subflow_ulp_clone() allocates a new mptcp_subflow_context with new_ctx->conn still NULL while newsk inherits sk_write_space = subflow_write_space (new_ctx->conn is only populated later in subflow_syn_recv_sock()). Both SO_SNDBUF in sk_setsockopt() and TCP_NOTSENT_LOWAT in do_tcp_setsockopt() invoke sk->sk_write_space(sk):
net/mptcp/subflow.c:subflow_write_space() {
struct sock *sk = mptcp_subflow_ctx(ssk)->conn;
mptcp_propagate_sndbuf(sk, ssk);
mptcp_write_space(sk);
}Passing sk == NULL into mptcp_write_space() -> mptcp_stream_memory_free() causes a NULL pointer dereference.
net/core/filter.c const struct bpf_func_proto bpf_sk_setsockopt_proto = { · line 5961
Quoted 4 lines
net/core/filter.c +4
-0+case SOL_IPV6 : +if (optname ==IPV6_TCLASS || optname ==IPV6_AUTOFLOWLABEL ) +return -EOPNOTSUPP ; +break ;
[Severity: Medium]
Does IPV6_AUTOFLOWLABEL need to be rejected here?
Since commit 5121516b0c47 ("ipv6: lockless IPV6_AUTOFLOWLABEL implementation"), do_ipv6_setsockopt() handles IPV6_AUTOFLOWLABEL via inet6_assign_bit(AUTOFLOWLABEL, sk, valbool) and inet6_set_bit(AUTOFLOWLABEL_SET, sk), which modify inet_sk(sk)->inet_flags on the child socket rather than the listener's ipv6_pinfo.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 83 lines · On 11/24/26 2:34 PM, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [High] bpf: NULL deref inbpf_sk_clone_setsockopt()via MPTCPsubflow_write_space()
- [Medium] bpf: unnecessary rejection ofIPV6_AUTOFLOWLABELinbpf_sk_clone_setsockopt()
- [Low] bpf: multi-line comment style violation inbpf_lsm.candfilter.c
--commit
9470b8f4e13b2c8be2cf5cc10f852d869dbb6f7c
Author: Jiayuan Chen <[email protected]>bpf, lsm: fix
bpf_setsockopt()on a half set up child ininet_csk_cloneThis commit introduces a dedicated
bpf_setsockopt()helper for thebpf_lsm_inet_csk_clonehook that rejects socket options unsafe to set on a partially initialized cloned child socket.kernel/bpf/bpf_lsm.c BTF_ID(func, bpf_lsm_inet_conn_established) · line 84
Quoted 11 lines
kernel/bpf/bpf_lsm.c +3
-0@@ -79,6 +79,15 @@ 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. +*/ [Severity: Low]
This isn't a bug, but BPF multi-line comments place the opening /* on its own line (here and abovebpf_sk_clone_setsockopt()innet/core/filter.c).[ ... ]
Quoted 15 lines
net/core/filter.c +7
-0@@ -5938,6 +5938,43 @@ 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 and an IPv6 child still points to the +* ipv6_pinfo of the listener. +*/ +BPF_CALL_5(bpf_sk_clone_setsockopt, struct sock *, sk, int, level, + int, optname, char *, optval, int, optlen)net/core/filter.c const struct bpf_func_proto bpf_sk_setsockopt_proto = { · line 5957
Quoted 10 lines
net/core/filter.c +10
-0+{ +switch (level) { +case SOL_SOCKET : +if (optname ==SO_KEEPALIVE ) +return -EOPNOTSUPP ; +break ; +case SOL_TCP : +if (optname ==TCP_KEEPIDLE || optname ==TCP_CONGESTION ) +return -EOPNOTSUPP ; +break ;[Severity: High]
Shouldbpf_sk_clone_setsockopt()also rejectSO_SNDBUFandTCP_NOTSENT_LOWAT?When an incoming MPTCP subflow is cloned in
inet_csk_clone_lock(),inet_clone_ulp()->subflow_ulp_clone()allocates a newmptcp_subflow_contextwithnew_ctx->connstill NULL while newsk inheritssk_write_space=subflow_write_space(new_ctx->connis only populated later insubflow_syn_recv_sock()). BothSO_SNDBUFinsk_setsockopt()andTCP_NOTSENT_LOWATindo_tcp_setsockopt()invokesk->sk_write_space(sk):net/mptcp/subflow.c:subflow_write_space() { struct sock *sk = mptcp_subflow_ctx(ssk)->conn; mptcp_propagate_sndbuf(sk, ssk); mptcp_write_space(sk); }
Quoted 2 lines
Passing sk == NULL into
mptcp_write_space()->mptcp_stream_memory_free()causes a NULL pointer dereference.
It's true.
I can add these two. But I am not sure the list will ever be complete, the child is only half set up at this point and each option needs a look. Or we just don't allow bpf_setsockopt() in this hook at all?
Quoted 16 lines
net/core/filter.c const struct bpf_func_proto bpf_sk_setsockopt_proto = { · line 5961
Quoted 5 lines
net/core/filter.c +4
-0+case SOL_IPV6 : +if (optname ==IPV6_TCLASS || optname ==IPV6_AUTOFLOWLABEL ) +return -EOPNOTSUPP ; +break ;[Severity: Medium]
DoesIPV6_AUTOFLOWLABELneed to be rejected here?Since commit
5121516b0c47("ipv6: locklessIPV6_AUTOFLOWLABELimplementation"),do_ipv6_setsockopt()handlesIPV6_AUTOFLOWLABELviainet6_assign_bit(AUTOFLOWLABEL, sk, valbool)andinet6_set_bit(AUTOFLOWLABEL_SET, sk), which modify inet_sk(sk)->inet_flags on the child socket rather than the listener'sipv6_pinfo.