bpf, lsm: fix bpf_setsockopt() on a half set up child in inet_csk_clone
Subsystems (7)
PATCH v3 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. So bpf_setsockopt() from a lsm_cgroup prog is wrong there for:
- SO_KEEPALIVE, TCP_KEEPIDLE: arm the keepalive timer before tcp_init_xmit_timers() sets it up.
- IPV6_TCLASS: writes to the ipv6_pinfo of the listener.
- TCP_CONGESTION: puts icsk->icsk_ca_ops, which the child holds no reference on.
For example, SO_KEEPALIVE ends up with:
Oops: general protection fault, probably for non-canonical address ... KASAN: maybe wild-memory-access in range [0xdead000000000128-...] 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>
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. Reject these options in the hook. Not done, it is a deny list that has to follow every change of the child set up.
3. Call the hook after the child is set up.
Go with 3, as V1 suggested. Call the hook right before __inet_inherit_port() in tcp_v4_syn_recv_sock() and tcp_v6_syn_recv_sock(), the two callers of tcp_create_openreq_child(). The child is still locked and not in bhash and ehash yet. Options like SO_MARK and IP_TOS now drop the dst the child just got, tcp_init_transfer() looks it up again.
MPTCP is not covered by the move. A passive subflow inherits sk_write_space = subflow_write_space() from the listener, but its subflow->conn is only set in subflow_syn_recv_sock() after the child is in ehash, too late for the hook. SO_SNDBUF and TCP_NOTSENT_LOWAT call sk->sk_write_space(), and subflow_write_space() derefs the NULL conn. There is no msk to wake up yet, so return early there.
A bpf CC hits the same NULL without the lsm hook, set_state() is called from tcp_ca_openreq_child():
Oops: general protection fault, probably for non-canonical address ... KASAN: null-ptr-deref in range [0x00000000000008c0-0x00000000000008c7] RIP: 0010:subflow_write_space+0x142/0xb80 Call Trace: <IRQ> do_tcp_setsockopt+0x18e0/0x2500 sol_tcp_sockopt+0x415/0xaa0 __bpf_setsockopt+0xd8/0x180 _bpf_setsockopt+0x77/0x130 bpf_sk_setsockopt_nodelay+0x41/0x60 bpf_prog_47b652bba9e6db3d_repro_set_state+0x40/0x46 bpf__tcp_congestion_ops_set_state+0x4c/0xac tcp_set_ca_state+0xaf/0x250 tcp_ca_openreq_child+0x126/0x460 tcp_v4_syn_recv_sock+0x5b7/0x1240 subflow_syn_recv_sock+0x2ae/0x1570 tcp_check_req+0x531/0x2a30 tcp_v4_rcv+0x142d/0x3e00 ... </IRQ>
---
v1 -> v2: Both Stanislav and Alexei asked whether we can keep those options supported by moving
the hook rather than disabled them.
v1 : https://lore.kernel.org/bpf/[email protected]/
--- net/ipv4/inet_connection_sock.c | 2 -- net/ipv4/tcp_ipv4.c | 3 +++ net/ipv6/tcp_ipv6.c | 3 +++ net/mptcp/subflow.c | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-)
net/ipv4/inet_connection_sock.c +0 -2
@@ -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; }
net/ipv4/tcp_ipv4.c +3 -0
@@ -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),
net/ipv6/tcp_ipv6.c +3 -0
@@ -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),
net/mptcp/subflow.c +4 -0
@@ -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); }
Signature · 1 line
2.43.0
PATCH v3 2/2 selftests/bpf: test bpf_setsockopt() in inet_csk_clone hook
Set SO_KEEPALIVE, SO_SNDBUF, TCP_CONGESTION and IPV6_TCLASS in the lsm_cgroup/inet_csk_clone prog. Check them on the accepted socket and check that the listener keeps its tclass.
Also accept a MPTCP connection, SO_SNDBUF on the passive subflow calls subflow_write_space() before the msk is attached.
--- .../selftests/bpf/prog_tests/lsm_cgroup.c | 45 ++++++++++++++++++- .../testing/selftests/bpf/progs/lsm_cgroup.c | 18 ++++++++ 2 files changed, 62 insertions(+), 1 deletion(-)
tools/testing/selftests/bpf/prog_tests/lsm_cgroup.c +44 -1
@@ -11,6 +11,10 @@#include "cgroup_helpers.h" #include "network_helpers.h" +#ifndef IPPROTO_MPTCP +#define IPPROTO_MPTCP 262 +#endif +static struct btf *btf;static __u32 query_prog_cnt (int cgroup_fd,const char *attach_func)
@@ -64,6 +68,7 @@ static void test_lsm_cgroup_functional(void)DECLARE_LIBBPF_OPTS (bpf_prog_attach_opts, attach_opts);DECLARE_LIBBPF_OPTS (bpf_link_update_opts, update_opts);int cgroup_fd =-1 , cgroup_fd2 =-1 , cgroup_fd3 =-1 ; +struct network_helper_opts mptcp_opts = { .proto =IPPROTO_MPTCP };int listen_fd, client_fd, accepted_fd;struct lsm_cgroup *skel =NULL ;int post_create_prog_fd2 =-1 ;
@@ -74,8 +79,9 @@ static void test_lsm_cgroup_functional(void)int bind_prog_fd =-1 ;int bind_link_fd =-1 ;int clone_prog_fd =-1 ; -int err, fd, prio; +int err, fd, prio, val;socklen_t socklen; +char cc[16 ]; cgroup_fd3 =test__join_cgroup ("/sock_policy_empty" );if (!ASSERT_GE (cgroup_fd3,0 ,"create empty cgroup" ))
@@ -227,6 +233,30 @@ static void test_lsm_cgroup_functional(void) "getsockopt");ASSERT_EQ (prio,234 ,"sk_priority" ); +/* Set by the inet_csk_clone prog on the child, not on the listener. */ + val =0 ; + socklen =sizeof (val); +ASSERT_OK (getsockopt (accepted_fd,SOL_SOCKET ,SO_KEEPALIVE , &val, &socklen), +"getsockopt" ); +ASSERT_EQ (val,1 ,"keepalive" ); + + val =0 ; + socklen =sizeof (val); +ASSERT_OK (getsockopt (accepted_fd,SOL_IPV6 ,IPV6_TCLASS , &val, &socklen), +"getsockopt" ); +ASSERT_EQ (val,0x28 ,"child tclass" ); + + val =-1 ; + socklen =sizeof (val); +ASSERT_OK (getsockopt (listen_fd,SOL_IPV6 ,IPV6_TCLASS , &val, &socklen), +"getsockopt" ); +ASSERT_EQ (val,0 ,"listener tclass" ); + + socklen =sizeof (cc); +ASSERT_OK (getsockopt (accepted_fd,SOL_TCP ,TCP_CONGESTION , cc, &socklen), +"getsockopt" ); +ASSERT_STREQ (cc,"reno" ,"cc" ); +/* These are replaced and never called. */ ASSERT_EQ (skel->bss->called_socket_post_create,0 ,"called_create" );ASSERT_EQ (skel->bss->called_socket_bind,0 ,"called_bind" );
@@ -267,6 +297,19 @@ static void test_lsm_cgroup_functional(void)close (client_fd);close (accepted_fd); +/* The passive MPTCP subflow has no msk yet in inet_csk_clone. */ + listen_fd =start_server_str (AF_INET6 ,SOCK_STREAM ,"::1" ,0 , &mptcp_opts); +ASSERT_GE (listen_fd,0 ,"start_server mptcp" ); + client_fd =connect_to_fd (listen_fd,0 ); +ASSERT_GE (client_fd,0 ,"connect_to_fd mptcp" ); + accepted_fd =accept (listen_fd,NULL ,NULL ); +ASSERT_GE (accepted_fd,0 ,"accept mptcp" ); +ASSERT_EQ (skel->bss->called_socket_clone,2 ,"called_clone mptcp" ); + +close (listen_fd); +close (client_fd); +close (accepted_fd); +/* Make sure other cgroup doesn't trigger the programs. */ if (!ASSERT_OK (join_cgroup ("/sock_policy_empty" ),"join root cgroup" ))
tools/testing/selftests/bpf/progs/lsm_cgroup.c +18 -0
@@ -168,11 +168,19 @@ 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 sndbuf =65536 ; +int tclass =0x28 ;int prio =234 ; +int one =1 ;if (!newsk)return 1 ; +/* MPTCP calls the hook for the msk too. */ +if (newsk->sk_protocol !=IPPROTO_TCP ) +return 1 ; +/* Accepted request sockets get a different priority. */ if (bpf_setsockopt (newsk,SOL_SOCKET ,SO_PRIORITY , &prio,sizeof (prio)))return 1 ;
@@ -184,6 +192,16 @@ int BPF_PROG(socket_clone, struct sock *newsk, const struct request_sock *req)if (prio !=234 )return 1 ; +/* The child is set up. */ +if (bpf_setsockopt (newsk,SOL_SOCKET ,SO_KEEPALIVE , &one,sizeof (one))) +return 1 ; +if (bpf_setsockopt (newsk,SOL_SOCKET ,SO_SNDBUF , &sndbuf,sizeof (sndbuf))) +return 1 ; +if (bpf_setsockopt (newsk,SOL_TCP ,TCP_CONGESTION , cc,sizeof (cc))) +return 1 ; +if (bpf_setsockopt (newsk,SOL_IPV6 ,IPV6_TCLASS , &tclass,sizeof (tclass))) +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 1 potential issue to consider: - [Low] bpf, lsm: malformed Fixes trailer with trailing inline comment --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] bpf, lsm: malformed Fixes trailer with trailing inline comment --
commit f644a7db922e05a1990460caa80c5118da46a9eb
Author: Jiayuan Chen <[email protected]>
Quoted 75 lines
bpf, lsm: fix
bpf_setsockopt()on a half set up child ininet_csk_clone
security_inet_csk_clone()is called at the end ofinet_csk_clone_lock(). The child is locked, which is why the hook is inbpf_lsm_locked_sockopt_hooks, but it is not set up yet. Sobpf_setsockopt()from alsm_cgroupprog is wrong there for:-
SO_KEEPALIVE,TCP_KEEPIDLE: arm the keepalive timer beforetcp_init_xmit_timers()sets it up.
-IPV6_TCLASS: writes to theipv6_pinfoof the listener.
-TCP_CONGESTION: putsicsk->icsk_ca_ops, which the child holds no reference on.For example,
SO_KEEPALIVEends up with:Oops: general protection fault, probably for non-canonical address ... KASAN: maybe wild-memory-access in range [0xdead000000000128-...] 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>Options considered:
1. Remove the hook frombpf_lsm_locked_sockopt_hooks, as commit5416c9aea832("bpf: Fix the kernel crash caused bybpf_setsockopt().") did forsocket_sock_rcv_skb. Not done, setting options likeSO_PRIORITYon the child is what the hook is used for and it works.
2. Reject these options in the hook. Not done, it is a deny list that has to follow every change of the child set up.
3. Call the hook after the child is set up.Go with 3, as V1 suggested. Call the hook right before
__inet_inherit_port()intcp_v4_syn_recv_sock()andtcp_v6_syn_recv_sock(), the two callers oftcp_create_openreq_child(). The child is still locked and not in bhash and ehash yet. Options likeSO_MARKandIP_TOSnow drop the dst the child just got,tcp_init_transfer()looks it up again.MPTCP is not covered by the move. A passive subflow inherits
sk_write_space=subflow_write_space()from the listener, but itssubflow->connis only set insubflow_syn_recv_sock()after the child is in ehash, too late for the hook.SO_SNDBUFandTCP_NOTSENT_LOWATcallsk->sk_write_space(), andsubflow_write_space()derefs the NULL conn. There is no msk to wake up yet, so return early there.A bpf CC hits the same NULL without the lsm hook,
set_state()is called fromtcp_ca_openreq_child():Oops: general protection fault, probably for non-canonical address ... KASAN: null-ptr-deref in range [0x00000000000008c0-0x00000000000008c7] RIP: 0010:subflow_write_space+0x142/0xb80 Call Trace: <IRQ> do_tcp_setsockopt+0x18e0/0x2500 sol_tcp_sockopt+0x415/0xaa0 __bpf_setsockopt+0xd8/0x180 _bpf_setsockopt+0x77/0x130 bpf_sk_setsockopt_nodelay+0x41/0x60 bpf_prog_47b652bba9e6db3d_repro_set_state+0x40/0x46 bpf__tcp_congestion_ops_set_state+0x4c/0xac tcp_set_ca_state+0xaf/0x250 tcp_ca_openreq_child+0x126/0x460 tcp_v4_syn_recv_sock+0x5b7/0x1240 subflow_syn_recv_sock+0x2ae/0x1570 tcp_check_req+0x531/0x2a30 tcp_v4_rcv+0x142d/0x3e00 ... </IRQ>
Quoted 3 lines
Fixes 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")Fixes eb18b49ea758 ("bpf: tcp: Allow bpf-tcp-cc to call bpf_(get|set)sockopt") # MPTCP NULL derefSigned-off-by Jiayuan Chen <[email protected]>
[Severity: Low]
The trailing "# MPTCP NULL deref" comment on the second Fixes: trailer breaks canonical Fixes: tag parsing. Should this be formatted as:
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 119 lines · Jiayuan Chen
security_inet_csk_clone()is called at the end ofinet_csk_clone_lock(). The child is locked, which is why the hook is inbpf_lsm_locked_sockopt_hooks, but it is not set up yet. Sobpf_setsockopt()from alsm_cgroupprog is wrong there for:-
SO_KEEPALIVE,TCP_KEEPIDLE: arm the keepalive timer beforetcp_init_xmit_timers()sets it up.
-IPV6_TCLASS: writes to theipv6_pinfoof the listener.
-TCP_CONGESTION: putsicsk->icsk_ca_ops, which the child holds no reference on.For example,
SO_KEEPALIVEends up with:Oops: general protection fault, probably for non-canonical address ... KASAN: maybe wild-memory-access in range [0xdead000000000128-...] 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>Options considered:
1. Remove the hook frombpf_lsm_locked_sockopt_hooks, as commit5416c9aea832("bpf: Fix the kernel crash caused bybpf_setsockopt().") did forsocket_sock_rcv_skb. Not done, setting options likeSO_PRIORITYon the child is what the hook is used for and it works.
2. Reject these options in the hook. Not done, it is a deny list that has to follow every change of the child set up.
3. Call the hook after the child is set up.Go with 3, as V1 suggested. Call the hook right before
__inet_inherit_port()intcp_v4_syn_recv_sock()andtcp_v6_syn_recv_sock(), the two callers oftcp_create_openreq_child(). The child is still locked and not in bhash and ehash yet. Options likeSO_MARKandIP_TOSnow drop the dst the child just got,tcp_init_transfer()looks it up again.MPTCP is not covered by the move. A passive subflow inherits
sk_write_space=subflow_write_space()from the listener, but itssubflow->connis only set insubflow_syn_recv_sock()after the child is in ehash, too late for the hook.SO_SNDBUFandTCP_NOTSENT_LOWATcallsk->sk_write_space(), andsubflow_write_space()derefs the NULL conn. There is no msk to wake up yet, so return early there.A bpf CC hits the same NULL without the lsm hook,
set_state()is called fromtcp_ca_openreq_child():Oops: general protection fault, probably for non-canonical address ... KASAN: null-ptr-deref in range [0x00000000000008c0-0x00000000000008c7] RIP: 0010:subflow_write_space+0x142/0xb80 Call Trace: <IRQ> do_tcp_setsockopt+0x18e0/0x2500 sol_tcp_sockopt+0x415/0xaa0 __bpf_setsockopt+0xd8/0x180 _bpf_setsockopt+0x77/0x130 bpf_sk_setsockopt_nodelay+0x41/0x60 bpf_prog_47b652bba9e6db3d_repro_set_state+0x40/0x46 bpf__tcp_congestion_ops_set_state+0x4c/0xac tcp_set_ca_state+0xaf/0x250 tcp_ca_openreq_child+0x126/0x460 tcp_v4_syn_recv_sock+0x5b7/0x1240 subflow_syn_recv_sock+0x2ae/0x1570 tcp_check_req+0x531/0x2a30 tcp_v4_rcv+0x142d/0x3e00 ... </IRQ>Fixes 9113d7e48e91 ("bpf: expose bpf_{g,s}etsockopt to lsm cgroup")Fixes eb18b49ea758 ("bpf: tcp: Allow bpf-tcp-cc to call bpf_(get|set)sockopt") # MPTCP NULL derefSigned-off-by Jiayuan Chen <[email protected]>---
v1 -> v2: Both Stanislav and Alexei asked whether we can keep those options supported by moving
the hook rather than disabled them.
v1 : https://lore.kernel.org/bpf/[email protected]/--- net/ipv4/inet_connection_sock.c | 2 -- net/ipv4/tcp_ipv4.c | 3 +++ net/ipv6/tcp_ipv6.c | 3 +++ net/mptcp/subflow.c | 4 ++++ 4 files changed, 10 insertions(+), 2 deletions(-)net/ipv4/inet_connection_sock.c +0
-2@@ -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; }net/ipv4/tcp_ipv4.c +3
-0@@ -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),net/ipv6/tcp_ipv6.c +0
-0
net/ipv6/tcp_ipv6.c static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff * · line 1519
Quoted 4 lines
net/ipv6/tcp_ipv6.c +1
-0@@ -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. */
Don't think the comments above are providing anything useful.
Quoted 10 lines
net/ipv6/tcp_ipv6.c +2
-0+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),net/mptcp/subflow.c +0
-0
net/mptcp/subflow.c static void subflow_write_space(struct sock *ssk) · line 1605
Quoted 4 lines
net/mptcp/subflow.c +1
-0@@ -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 */
This one maybe reword to '/* bpf_setsockopt from security_inet_csk_clone */'? (unless MPTCP people have some ideas how to make it work properly)