bpf: Reject TCP_NODELAY in TCP header option callbacks
Subsystems (6)
v2 cover bpf: Reject TCP_NODELAY in TCP header option callbacks
This small patchset is about avoid infinite recursion in TCP header option callbacks via TCP_NODELAY setsockopt.
v2:
- Reject TCP_NODELAY in bpf_sock_ops_setsockopt() (AI and Martin)
v1:
https://lore.kernel.org/bpf/[email protected]/
---
KaFai Wan (2):
bpf: Reject TCP_NODELAY in TCP header option callbacks selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks
net/core/filter.c | 5 ++ .../bpf/prog_tests/tcp_hdr_options.c | 54 +++++++++++++++++++ .../bpf/progs/test_misc_tcp_hdr_options.c | 40 ++++++++++++++ 3 files changed, 99 insertions(+)
Signature · 1 line
2.43.0
PATCH v2 1/2 bpf: Reject TCP_NODELAY in TCP header option callbacks
A BPF_SOCK_OPS program can enable BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG and then call bpf_setsockopt(TCP_NODELAY) from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
In these callbacks, bpf_setsockopt(TCP_NODELAY) can reach __tcp_sock_set_nodelay(), which can call tcp_push_pending_frames().
From BPF_SOCK_OPS_HDR_OPT_LEN_CB, tcp_push_pending_frames() can call tcp_current_mss(), which calls tcp_established_options() and re-enters bpf_skops_hdr_opt_len().
BPF_SOCK_OPS_HDR_OPT_LEN_CB
-> bpf_setsockopt(TCP_NODELAY)
-> tcp_push_pending_frames()
-> tcp_current_mss()
-> tcp_established_options()
-> bpf_skops_hdr_opt_len()
-> BPF_SOCK_OPS_HDR_OPT_LEN_CBFrom BPF_SOCK_OPS_WRITE_HDR_OPT_CB, tcp_push_pending_frames() can call tcp_write_xmit(), which calls tcp_transmit_skb(). That path recomputes header option length through tcp_established_options() and bpf_skops_hdr_opt_len() before re-entering bpf_skops_write_hdr_opt().
BPF_SOCK_OPS_WRITE_HDR_OPT_CB
-> bpf_setsockopt(TCP_NODELAY)
-> tcp_push_pending_frames()
-> tcp_write_xmit()
-> tcp_transmit_skb()
-> tcp_established_options()
-> bpf_skops_hdr_opt_len()
-> bpf_skops_write_hdr_opt()
-> BPF_SOCK_OPS_WRITE_HDR_OPT_CBThis leads to unbounded recursion and can overflow the kernel stack.
Reject TCP_NODELAY with -EOPNOTSUPP in bpf_sock_ops_setsockopt() when bpf_setsockopt() is called from BPF_SOCK_OPS_HDR_OPT_LEN_CB or BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
--- net/core/filter.c | 5 +++++ 1 file changed, 5 insertions(+)
net/core/filter.c +5 -0
@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,if (!is_locked_tcp_sock_ops (bpf_sock))return -EOPNOTSUPP ; +if ((bpf_sock->op ==BPF_SOCK_OPS_HDR_OPT_LEN_CB || + bpf_sock->op ==BPF_SOCK_OPS_WRITE_HDR_OPT_CB ) && +IS_ENABLED (CONFIG_INET ) && level ==SOL_TCP && optname ==TCP_NODELAY ) +return -EOPNOTSUPP ; +return _bpf_setsockopt (bpf_sock->sk, level, optname, optval, optlen); }
Signature · 1 line
2.43.0
PATCH v2 2/2 selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks
In reply to KaFai WanAdd a sockops selftest for the TCP_NODELAY restriction in BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB.
The test program calls bpf_setsockopt(TCP_NODELAY) from ACTIVE_ESTABLISHED_CB and PASSIVE_ESTABLISHED_CB to verify that it is still allowed outside the TCP header option callbacks.
It then enables BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG, sends data to exercise the TCP header option path, and checks that bpf_setsockopt(TCP_NODELAY) returns -EOPNOTSUPP from both BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB while the connection continues to make forward progress.
--- .../bpf/prog_tests/tcp_hdr_options.c | 54 +++++++++++++++++++ .../bpf/progs/test_misc_tcp_hdr_options.c | 40 ++++++++++++++ 2 files changed, 94 insertions(+)
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +54 -0
@@ -513,6 +513,59 @@ static void misc(void)bpf_link__destroy (link); } +static void hdr_sockopt (void ) +{ +const char send_msg[] ="MISC!!!" ; +char recv_msg[sizeof (send_msg)]; +const unsigned int nr_data =2 ; +struct bpf_link *link; +struct sk_fds sk_fds; +int i, ret, true_val =1 ; + + lport_linum_map_fd =bpf_map__fd (misc_skel->maps.lport_linum_map); + + link =bpf_program__attach_cgroup (misc_skel->progs.misc_hdr_sockopt, cg_fd); +if (!ASSERT_OK_PTR (link,"attach_cgroup(misc_hdr_sockopt)" )) +return ; + +if (sk_fds_connect (&sk_fds, false)) { +bpf_link__destroy (link); +return ; + } + + ret =setsockopt (sk_fds.active_fd,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (!ASSERT_OK (ret,"setsockopt(TCP_NODELAY) active" )) + goto check_linum; + + ret =setsockopt (sk_fds.passive_fd,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (!ASSERT_OK (ret,"setsockopt(TCP_NODELAY) passive" )) + goto check_linum; + +for (i =0 ; i < nr_data; i++) { + ret =send (sk_fds.active_fd, send_msg,sizeof (send_msg),0 ); +if (!ASSERT_EQ (ret,sizeof (send_msg),"send(msg)" )) + goto check_linum; + + ret =read (sk_fds.passive_fd, recv_msg,sizeof (recv_msg)); +if (!ASSERT_EQ (ret,sizeof (send_msg),"read(msg)" )) + goto check_linum; + } + +ASSERT_NEQ (misc_skel->bss->nr_hdr_sockopt_estab,0 ,"nr_hdr_sockopt_estab" ); +ASSERT_EQ (misc_skel->bss->nr_hdr_sockopt_estab_err,0 ,"nr_hdr_sockopt_estab_err" ); + +ASSERT_NEQ (misc_skel->bss->nr_hdr_sockopt_len,0 ,"nr_hdr_sockopt_len" ); +ASSERT_EQ (misc_skel->bss->nr_hdr_sockopt_len_err,0 ,"nr_hdr_sockopt_len_err" ); + +ASSERT_NEQ (misc_skel->bss->nr_hdr_sockopt_write,0 ,"nr_hdr_sockopt_write" ); +ASSERT_EQ (misc_skel->bss->nr_hdr_sockopt_write_err,0 ,"nr_hdr_sockopt_write_err" ); + +check_linum: +ASSERT_FALSE (check_error_linum (&sk_fds),"check_error_linum" ); +sk_fds_close (&sk_fds); +bpf_link__destroy (link); +} +struct test {const char *desc;void (*run)(void );
@@ -526,6 +579,7 @@ static struct test tests[] = {DEF_TEST (fastopen_estab),DEF_TEST (fin),DEF_TEST (misc), + DEF_TEST(hdr_sockopt), };void test_tcp_hdr_options (void )
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +40 -0
@@ -28,6 +28,12 @@ unsigned int nr_data = 0;unsigned int nr_syn =0 ;unsigned int nr_fin =0 ;unsigned int nr_hwtstamp =0 ; +unsigned int nr_hdr_sockopt_estab =0 ; +unsigned int nr_hdr_sockopt_estab_err =0 ; +unsigned int nr_hdr_sockopt_len =0 ; +unsigned int nr_hdr_sockopt_len_err =0 ; +unsigned int nr_hdr_sockopt_write =0 ; +unsigned int nr_hdr_sockopt_write_err =0 ;/* Check the header received from the active side */ static int __check_active_hdr_in (struct bpf_sock_ops *skops,bool check_syn)
@@ -326,4 +332,38 @@ int misc_estab(struct bpf_sock_ops *skops)return CG_OK ; } +SEC("sockops") +int misc_hdr_sockopt (struct bpf_sock_ops *skops) +{ +int true_val =1 ; +int ret; + +switch (skops->op) { +case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB : +case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB : + nr_hdr_sockopt_estab++; +set_hdr_cb_flags (skops,0 ); + ret =bpf_setsockopt (skops,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (ret) + nr_hdr_sockopt_estab_err++; +break ; +case BPF_SOCK_OPS_HDR_OPT_LEN_CB : + nr_hdr_sockopt_len++; + ret =bpf_setsockopt (skops,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (ret != -EOPNOTSUPP ) + nr_hdr_sockopt_len_err++; +/* just trigger BPF_SOCK_OPS_WRITE_HDR_OPT_CB */ +bpf_reserve_hdr_opt (skops,12 ,0 ); +break ; +case BPF_SOCK_OPS_WRITE_HDR_OPT_CB : + nr_hdr_sockopt_write++; + ret =bpf_setsockopt (skops,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (ret != -EOPNOTSUPP ) + nr_hdr_sockopt_write_err++; +break ; + } + +return CG_OK ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
Martin KaFai Lau
reviewernet/core/filter.c BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 5838
Quoted 11 lines · KaFai Wan
net/core/filter.c +3
-0@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,if (!is_locked_tcp_sock_ops (bpf_sock))return -EOPNOTSUPP ; +if ((bpf_sock->op ==BPF_SOCK_OPS_HDR_OPT_LEN_CB || + bpf_sock->op ==BPF_SOCK_OPS_WRITE_HDR_OPT_CB ) && +IS_ENABLED (CONFIG_INET ) && level ==SOL_TCP && optname ==TCP_NODELAY )
IS_ENABLED(CONFIG_INET) is unnecessary.
pw-bot: cr
Quoted 8 lines
net/core/filter.c +2
-0+return -EOPNOTSUPP ; +return _bpf_setsockopt (bpf_sock->sk, level, optname, optval, optlen); }Signature · 1 line
2.43.0
Martin KaFai Lau
reviewerRe: [PATCH bpf v2 2/2] selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks
In reply to KaFai WanQuoted 22 lines · KaFai Wan
index
56685fc03c7e..2d738c0c4259100644tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +15
-0@@ -513,6 +513,59 @@ static void misc(void)bpf_link__destroy (link); } +static void hdr_sockopt (void ) +{ +const char send_msg[] ="MISC!!!" ; +char recv_msg[sizeof (send_msg)]; +const unsigned int nr_data =2 ; +struct bpf_link *link; +struct sk_fds sk_fds; +int i, ret, true_val =1 ; + + lport_linum_map_fd =bpf_map__fd (misc_skel->maps.lport_linum_map); + + link =bpf_program__attach_cgroup (misc_skel->progs.misc_hdr_sockopt, cg_fd); +if (!ASSERT_OK_PTR (link,"attach_cgroup(misc_hdr_sockopt)" )) +return ; +
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c static void misc(void) · line 540
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +10
-0+if (sk_fds_connect (&sk_fds, false)) { +bpf_link__destroy (link); +return ; + } + + ret =setsockopt (sk_fds.active_fd,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (!ASSERT_OK (ret,"setsockopt(TCP_NODELAY) active" )) + goto check_linum; + + ret =setsockopt (sk_fds.passive_fd,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val));
Why are these two setsockopt(TCP_NODELAY) calls needed?
Instead of creating a new "void hdr_sockopt(void)", can the test be done in the existing "void misc(void)" by doing bpf_setsockopt(TCP_NODELAY) in the misc_estab() bpf prog?
The PASSIVE_ESTABLISHED_CB can do the bpf_setsockopt(TCP_NODELAY, 0) if it wants to keep the same expectation on Nagle. The BPF_SOCK_OPS_HDR_OPT_LEN_CB and BPF_SOCK_OPS_WRITE_HDR_OPT_CB can do bpf_setsockopt(TCP_NODELAY, 1) to test recursion and the error return value.
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +0
-0void test_tcp_hdr_options (void )tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +0
-0
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c unsigned int nr_data = 0; · line 36
Quoted 9 lines
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +6
-0@@ -28,6 +28,12 @@ unsigned int nr_data = 0;unsigned int nr_syn =0 ;unsigned int nr_fin =0 ;unsigned int nr_hwtstamp =0 ; +unsigned int nr_hdr_sockopt_estab =0 ; +unsigned int nr_hdr_sockopt_estab_err =0 ; +unsigned int nr_hdr_sockopt_len =0 ; +unsigned int nr_hdr_sockopt_len_err =0 ; +unsigned int nr_hdr_sockopt_write =0 ; +unsigned int nr_hdr_sockopt_write_err =0 ;
nr_hdr_sockopt_estab, nr_hdr_sockopt_len, and nr_hdr_sockopt_write are unnecessary. These tests have already been covered in some ways.
Mostly a nit. The new counters are used in both connections. Note the existing nr_xxx is exclusively used in either active or passive, so there is no parallel counting in practice.
Instead of counting, just use a bool nodelay_est_ok, nodelay_hdr_len_err, nodelay_write_err and assert them to be true in userspace.
Quoted 14 lines · Martin KaFai Lau
net/core/filter.c BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 5838
Quoted 11 lines · KaFai Wan
net/core/filter.c +0
-0@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, if (!is_locked_tcp_sock_ops(bpf_sock)) return -EOPNOTSUPP;net/core/filter.c +3
-0+if ((bpf_sock->op ==BPF_SOCK_OPS_HDR_OPT_LEN_CB || + bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) && + IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY)
IS_ENABLED(CONFIG_INET)is unnecessary.
ok, will remove in next version.
Quoted 11 lines
pw-bot: cr
Quoted 8 lines
net/core/filter.c +2
-0+return -EOPNOTSUPP ; +return
_bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); }Signature · 1 line
2.43.0
Signature · 2 lines
Thanks, KaFai
Quoted 57 lines · KaFai Wan
A
BPF_SOCK_OPSprogram can enableBPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAGand then callbpf_setsockopt(TCP_NODELAY)fromBPF_SOCK_OPS_HDR_OPT_LEN_CBorBPF_SOCK_OPS_WRITE_HDR_OPT_CB.In these callbacks,
bpf_setsockopt(TCP_NODELAY)can reach__tcp_sock_set_nodelay(), which can calltcp_push_pending_frames().From
BPF_SOCK_OPS_HDR_OPT_LEN_CB,tcp_push_pending_frames()can calltcp_current_mss(), which callstcp_established_options()and re-entersbpf_skops_hdr_opt_len().
BPF_SOCK_OPS_HDR_OPT_LEN_CB
->bpf_setsockopt(TCP_NODELAY)-> tcp_push_pending_frames() -> tcp_current_mss() -> tcp_established_options() -> bpf_skops_hdr_opt_len() -> BPF_SOCK_OPS_HDR_OPT_LEN_CBFrom
BPF_SOCK_OPS_WRITE_HDR_OPT_CB,tcp_push_pending_frames()can calltcp_write_xmit(), which callstcp_transmit_skb(). That path recomputes header option length throughtcp_established_options()andbpf_skops_hdr_opt_len()before re-enteringbpf_skops_write_hdr_opt().
BPF_SOCK_OPS_WRITE_HDR_OPT_CB
->bpf_setsockopt(TCP_NODELAY)-> tcp_push_pending_frames() -> tcp_write_xmit() -> tcp_transmit_skb() -> tcp_established_options() -> bpf_skops_hdr_opt_len() -> bpf_skops_write_hdr_opt() -> BPF_SOCK_OPS_WRITE_HDR_OPT_CBThis leads to unbounded recursion and can overflow the kernel stack.
Reject
TCP_NODELAYwith-EOPNOTSUPPinbpf_sock_ops_setsockopt()whenbpf_setsockopt()is called fromBPF_SOCK_OPS_HDR_OPT_LEN_CBorBPF_SOCK_OPS_WRITE_HDR_OPT_CB.Reported-by Quan Sun <[email protected]>Reported-by Yinhao Hu <[email protected]>Reported-by Kaiyan Mei <[email protected]>Fixes 7e41df5dbba2 ("bpf: Add a few optnames to bpf_setsockopt")Signed-off-by KaFai Wan <[email protected]>--- net/core/filter.c | 5 +++++ 1 file changed, 5 insertions(+)net/core/filter.c +0
-0@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock,if (!is_locked_tcp_sock_ops (bpf_sock))
net/core/filter.c BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, · line 5843
Quoted 10 lines
net/core/filter.c +5
-0return -EOPNOTSUPP ; +if ((bpf_sock->op ==BPF_SOCK_OPS_HDR_OPT_LEN_CB || + bpf_sock->op ==BPF_SOCK_OPS_WRITE_HDR_OPT_CB ) && +IS_ENABLED (CONFIG_INET ) && level ==SOL_TCP && optname ==TCP_NODELAY ) +return -EOPNOTSUPP ; +return _bpf_setsockopt (bpf_sock->sk, level, optname, optval, optlen); }
A simple comment is recommended:
/* TCP_NODELAY triggers tcp_push_pending_frames() and re-enters these callbacks. */
Also like Martin pointed before, BPF_SOCK_OPS_HDR_OPT_LEN_CB / BPF_SOCK_OPS_WRITE_HDR_OPT_CB
can only be produced under CONFIG_INET so IS_ENABLED(CONFIG_INET) is dead.
Re: [PATCH bpf v2 2/2] selftests/bpf: Test TCP_NODELAY in TCP hdr opt callbacks
In reply to Martin KaFai LauQuoted 36 lines · Martin KaFai Lau
Quoted 22 lines · KaFai Wan
index
56685fc03c7e..2d738c0c4259100644tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +0
-0@@ -513,6 +513,59 @@ static void misc(void) bpf_link__destroy(link); }tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +15
-0+static void hdr_sockopt (void ) +{ +const char send_msg[] ="MISC!!!" ; +char recv_msg[sizeof (send_msg)]; +const unsigned int nr_data =2 ; +struct bpf_link *link; +struct sk_fds sk_fds; +int i, ret, true_val =1 ; + + lport_linum_map_fd =bpf_map__fd (misc_skel->maps.lport_linum_map); + + link =bpf_program__attach_cgroup (misc_skel->progs.misc_hdr_sockopt, cg_fd); +if (!ASSERT_OK_PTR (link,"attach_cgroup(misc_hdr_sockopt)" )) +return ; +tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c static void misc(void) · line 540
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/tcp_hdr_options.c +10
-0+if (sk_fds_connect (&sk_fds, false)) { +bpf_link__destroy (link); +return ; + } + + ret =setsockopt (sk_fds.active_fd,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val)); +if (!ASSERT_OK (ret,"setsockopt(TCP_NODELAY) active" )) + goto check_linum; + + ret =setsockopt (sk_fds.passive_fd,SOL_TCP ,TCP_NODELAY , &true_val,sizeof (true_val));Why are these two
setsockopt(TCP_NODELAY)calls needed?
Quoted 3 lines
Instead of creating a new "void
hdr_sockopt(void)", can the test be done in the existing "void misc(void)" by doingbpf_setsockopt(TCP_NODELAY)in themisc_estab()bpf prog?
Oh, I see. I meant to test on both active and passive side. We can only test on active side in the existing "void misc(void)".
Quoted 18 lines
The
PASSIVE_ESTABLISHED_CBcan do thebpf_setsockopt(TCP_NODELAY, 0)if it wants to keep the same expectation on Nagle. TheBPF_SOCK_OPS_HDR_OPT_LEN_CBandBPF_SOCK_OPS_WRITE_HDR_OPT_CBcan dobpf_setsockopt(TCP_NODELAY, 1)to test recursion and the error return value.Quoted 11 lines
void
test_tcp_hdr_options(void)tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +1
-0@@ -28,6 +28,12 @@ unsigned int nr_data = 0; unsigned int nr_syn = 0; unsigned int nr_fin = 0; unsigned int nr_hwtstamp = 0; +unsigned int nr_hdr_sockopt_estab =0 ;
Quoted 8 lines
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c unsigned int nr_data = 0; · line 36
Quoted 5 lines
tools/testing/selftests/bpf/progs/test_misc_tcp_hdr_options.c +5
-0+unsigned int nr_hdr_sockopt_estab_err =0 ; +unsigned int nr_hdr_sockopt_len =0 ; +unsigned int nr_hdr_sockopt_len_err =0 ; +unsigned int nr_hdr_sockopt_write =0 ; +unsigned int nr_hdr_sockopt_write_err =0 ;
nr_hdr_sockopt_estab,nr_hdr_sockopt_len, andnr_hdr_sockopt_writeare unnecessary. These tests have already been covered in some ways.
yes, they are unnecessary in existing misc_estab()
Quoted 8 lines
Mostly a nit. The new counters are used in both connections. Note the existing
nr_xxxis exclusively used in either active or passive, so there is no parallel counting in practice.Instead of counting, just use a bool
nodelay_est_ok,nodelay_hdr_len_err,nodelay_write_errand assert them to be true in userspace.
indeed. will fix these in next version.
Signature · 2 lines
Thanks, KaFai
Quoted 72 lines · Jiayuan Chen
Quoted 64 lines · KaFai Wan
A
BPF_SOCK_OPSprogram can enableBPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAGand then callbpf_setsockopt(TCP_NODELAY)fromBPF_SOCK_OPS_HDR_OPT_LEN_CBorBPF_SOCK_OPS_WRITE_HDR_OPT_CB.In these callbacks,
bpf_setsockopt(TCP_NODELAY)can reach__tcp_sock_set_nodelay(), which can calltcp_push_pending_frames().From
BPF_SOCK_OPS_HDR_OPT_LEN_CB,tcp_push_pending_frames()can calltcp_current_mss(), which callstcp_established_options()and re-entersbpf_skops_hdr_opt_len().
BPF_SOCK_OPS_HDR_OPT_LEN_CB
->bpf_setsockopt(TCP_NODELAY)
->tcp_push_pending_frames()
->tcp_current_mss()
->tcp_established_options()
->bpf_skops_hdr_opt_len()
->BPF_SOCK_OPS_HDR_OPT_LEN_CBFrom
BPF_SOCK_OPS_WRITE_HDR_OPT_CB,tcp_push_pending_frames()can calltcp_write_xmit(), which calls tcp_transmit_skb(). That path recomputes header option length throughtcp_established_options()andbpf_skops_hdr_opt_len()before re-enteringbpf_skops_write_hdr_opt().
BPF_SOCK_OPS_WRITE_HDR_OPT_CB
->bpf_setsockopt(TCP_NODELAY)
->tcp_push_pending_frames()
->tcp_write_xmit()
->tcp_transmit_skb()
->tcp_established_options()
->bpf_skops_hdr_opt_len()
->bpf_skops_write_hdr_opt()
->BPF_SOCK_OPS_WRITE_HDR_OPT_CBThis leads to unbounded recursion and can overflow the kernel stack.
Reject
TCP_NODELAYwith-EOPNOTSUPPinbpf_sock_ops_setsockopt()whenbpf_setsockopt()is called fromBPF_SOCK_OPS_HDR_OPT_LEN_CBorBPF_SOCK_OPS_WRITE_HDR_OPT_CB.Reported-by Quan Sun <[email protected]>Reported-by Yinhao Hu <[email protected]>Reported-by Kaiyan Mei <[email protected]>Fixes 7e41df5dbba2 ("bpf: Add a few optnames to bpf_setsockopt")Signed-off-by KaFai Wan <[email protected]>---
net/core/filter.c| 5 +++++1 file changed, 5 insertions(+)net/core/filter.c +0
-0@@ -5833,6 +5833,11 @@ BPF_CALL_5(bpf_sock_ops_setsockopt, struct bpf_sock_ops_kern *, bpf_sock, if (!is_locked_tcp_sock_ops(bpf_sock)) return -EOPNOTSUPP;net/core/filter.c +5
-0+if ((bpf_sock->op ==BPF_SOCK_OPS_HDR_OPT_LEN_CB || + bpf_sock->op == BPF_SOCK_OPS_WRITE_HDR_OPT_CB) && + IS_ENABLED(CONFIG_INET) && level == SOL_TCP && optname == TCP_NODELAY) +return -EOPNOTSUPP ; +Quoted 3 lines
return
_bpf_setsockopt(bpf_sock->sk, level, optname, optval, optlen); }A simple comment is recommended:
Quoted 3 lines
/*
TCP_NODELAYtriggerstcp_push_pending_frames()and re-enters these callbacks. */
ok. Added in new version.
Quoted 6 lines
Also like Martin pointed before,
BPF_SOCK_OPS_HDR_OPT_LEN_CB/BPF_SOCK_OPS_WRITE_HDR_OPT_CBcan only be produced under
CONFIG_INETsoIS_ENABLED(CONFIG_INET)is dead.
Signature · 2 lines
Thanks, KaFai