bpf: add is_locked_tcp_sock guard for sock_ops rtt_min access
Subsystems (7)
PATCH 1/1 bpf: add is_locked_tcp_sock guard for sock_ops rtt_min access
sock_ops_convert_ctx_access() generates BPF instructions to inline context field accesses for BPF_PROG_TYPE_SOCK_OPS programs. For tcp_sock-specific fields like snd_cwnd, srtt_us, etc., it uses the SOCK_OPS_GET_TCP_SOCK_FIELD() macro which checks is_locked_tcp_sock and returns 0 when the socket is not a locked full TCP socket.
However, the rtt_min field bypasses this guard entirely: it emits a raw two-instruction load sequence (load sk pointer, then load from tcp_sock->rtt_min offset) without checking is_locked_tcp_sock first.
This is a problem because bpf_skops_hdr_opt_len() and bpf_skops_write_hdr_opt() in tcp_output.c set sock_ops.sk to a tcp_request_sock (cast from request_sock) during SYN-ACK processing, with is_fullsock=0 and is_locked_tcp_sock=0. If a SOCK_OPS program with BPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAG reads ctx->rtt_min in this callback, the generated code treats the tcp_request_sock pointer as a tcp_sock and reads at offsetof(struct tcp_sock, rtt_min) -- which is well past the end of the tcp_request_sock allocation, causing an out-of-bounds slab read.
The rtt_min field was introduced in the same commit as the othertcp_sock fields but was given hand-rolled access code because it reads
a sub-field (rtt_min.s[0].v, a minmax_sample) rather than a direct
struct member, making it incompatible with the SOCK_OPS_GET_FIELD()
macro. This hand-rolled code omitted the is_fullsock guard that the
macro provides. The guard was later renamed to is_locked_tcp_sock in
commit fd93eaffb3f9 ("bpf: Prevent unsafe access to the sock fields in the BPF timestamping callback").
Add the is_locked_tcp_sock guard to the rtt_min case, replicating the exact instruction pattern used by SOCK_OPS_GET_FIELD() including proper handling of the dst_reg==src_reg case with temp register save/restore. Use offsetof(struct minmax_sample, v) for the sub-field offset to match the style in bpf_tcp_sock_convert_ctx_access().
Found via AST-based call-graph analysis using sqry.
--- net/core/filter.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-)
net/core/filter.c +44 -3
@@ -10830,13 +10830,54 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,BUILD_BUG_ON (sizeof (struct minmax ) <sizeof (struct minmax_sample )); +/* Unlike other tcp_sock fields that use +* SOCK_OPS_GET_TCP_SOCK_FIELD(), rtt_min requires a +* custom access pattern because it reads a sub-field +* (rtt_min.s[0].v) rather than a direct struct member. +* We must still guard the access with is_locked_tcp_sock +* to prevent an OOB read when sk points to a +* tcp_request_sock (e.g., during SYN-ACK processing via +* bpf_skops_hdr_opt_len/bpf_skops_write_hdr_opt). +*/ + off = offsetof(struct tcp_sock , rtt_min) + + offsetof(struct minmax_sample , v); + { +int fullsock_reg = si->dst_reg, reg =BPF_REG_9 , jmp =2 ; + +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_STX_MEM (BPF_DW , si->src_reg, reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + fullsock_reg = reg; + jmp +=2 ; + } + *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( + struct bpf_sock_ops_kern, + is_locked_tcp_sock), + fullsock_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + is_locked_tcp_sock)); + *insn++ =BPF_JMP_IMM (BPF_JEQ , fullsock_reg,0 , jmp); +if (si->dst_reg == si->src_reg) + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp)); *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( struct bpf_sock_ops_kern, sk), si->dst_reg, si->src_reg, offsetof(struct bpf_sock_ops_kern , sk)); - *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, - offsetof(struct tcp_sock , rtt_min) + - sizeof_field(struct minmax_sample, t)); + *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, off); +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_JMP_A (1 ); + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + } + }break ;case offsetof(struct bpf_sock_ops , bpf_sock_ops_cb_flags):
Signature · 1 line
2.43.0
Martin KaFai Lau
reviewerQuoted 10 lines · Werner Kasselman
sock_ops_convert_ctx_access()generates BPF instructions to inline context field accesses forBPF_PROG_TYPE_SOCK_OPSprograms. For tcp_sock-specific fields likesnd_cwnd,srtt_us, etc., it uses theSOCK_OPS_GET_TCP_SOCK_FIELD()macro which checksis_locked_tcp_sockand returns 0 when the socket is not a locked full TCP socket.However, the
rtt_minfield bypasses this guard entirely: it emits a raw two-instruction load sequence (load sk pointer, then load fromtcp_sock->rtt_minoffset) without checkingis_locked_tcp_sockfirst.
Quoted 9 lines
This is a problem because
bpf_skops_hdr_opt_len()andbpf_skops_write_hdr_opt()intcp_output.cset sock_ops.sk to atcp_request_sock(cast fromrequest_sock) during SYN-ACK processing, with is_fullsock=0 and is_locked_tcp_sock=0. If aSOCK_OPSprogram withBPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAGreadsctx->rtt_minin this callback, the generated code treats thetcp_request_sockpointer as atcp_sockand reads atoffsetof(struct tcp_sock, rtt_min)-- which is well past the end of thetcp_request_sockallocation, causing an out-of-bounds slab read.
This is not limited to hdr related CB flags.
It also happens to earlier CB flags that have a request_sock, such as BPF_SOCK_OPS_RWND_INIT.
Quoted 71 lines
The
rtt_minfield was introduced in the same commit as the othertcp_sockfields but was given hand-rolled access code because it reads
a sub-field (rtt_min.s[0].v, aminmax_sample) rather than a direct
struct member, making it incompatible with theSOCK_OPS_GET_FIELD()
macro. This hand-rolled code omitted theis_fullsockguard that the
macro provides. The guard was later renamed tois_locked_tcp_sockin
commitfd93eaffb3f9("bpf: Prevent unsafe access to the sock fields in the BPF timestamping callback").Add the
is_locked_tcp_sockguard to thertt_mincase, replicating the exact instruction pattern used bySOCK_OPS_GET_FIELD()including proper handling of the dst_reg==src_reg case with temp register save/restore. Useoffsetof(struct minmax_sample, v)for the sub-field offset to match the style inbpf_tcp_sock_convert_ctx_access().Found via AST-based call-graph analysis using sqry.
Fixes 44f0e43037d3 ("bpf: Add support for reading sk_state and more")Signed-off-by Werner Kasselman <[email protected]>--- net/core/filter.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-)net/core/filter.c +36
-0@@ -10830,13 +10830,54 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,BUILD_BUG_ON (sizeof (struct minmax ) <sizeof (struct minmax_sample )); +/* Unlike other tcp_sock fields that use +* SOCK_OPS_GET_TCP_SOCK_FIELD(), rtt_min requires a +* custom access pattern because it reads a sub-field +* (rtt_min.s[0].v) rather than a direct struct member. +* We must still guard the access with is_locked_tcp_sock +* to prevent an OOB read when sk points to a +* tcp_request_sock (e.g., during SYN-ACK processing via +* bpf_skops_hdr_opt_len/bpf_skops_write_hdr_opt). +*/ + off = offsetof(struct tcp_sock , rtt_min) + + offsetof(struct minmax_sample , v); + { +int fullsock_reg = si->dst_reg, reg =BPF_REG_9 , jmp =2 ; + +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_STX_MEM (BPF_DW , si->src_reg, reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + fullsock_reg = reg; + jmp +=2 ; + } + *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( + struct bpf_sock_ops_kern, + is_locked_tcp_sock), + fullsock_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + is_locked_tcp_sock)); + *insn++ =BPF_JMP_IMM (BPF_JEQ , fullsock_reg,0 , jmp); +if (si->dst_reg == si->src_reg) + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp)); *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( struct bpf_sock_ops_kern, sk), si->dst_reg, si->src_reg,
net/core/filter.c static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, · line 10878
Quoted 10 lines
net/core/filter.c +6
-3offsetof(struct bpf_sock_ops_kern, sk)); - *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, - offsetof(struct tcp_sock , rtt_min) + - sizeof_field(struct minmax_sample, t)); + *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, off); +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_JMP_A (1 ); + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp));
There is an existing bug in this copy-and-paste codes [1] and now is repeated here, so please find a way to refactor it to be reusable instead of duplicating it.
This also needs a test. It should be a subtest of [1], so [1] need to land first.
Thanks for the review.
You are right that this is not limited to the SYN-ACK header-option paths. The same unguarded ctx->rtt_min access is reachable from request_sock-backed sock_ops invocations through tcp_call_bpf(), including BPF_SOCK_OPS_RWND_INIT, BPF_SOCK_OPS_TIMEOUT_INIT, and BPF_SOCK_OPS_NEEDS_ECN. I'll update the changelog to describe the affected paths more accurately.
I also agree that this should not duplicate the existing guarded ctx-access sequence. I'll rework the change to reuse a common helper/macro instead of open-coding another copy, and I'll add a selftest as a subtest of [1] after
[1] lands.
Thanks,
Werner
-----Original Message-----
From: Martin KaFai Lau <[email protected]>
Sent: Tuesday, 7 April 2026 11:26 AM
To: Werner Kasselman <[email protected]>
Cc: Jiayuan Chen <[email protected]>; Alexei Starovoitov <[email protected]>; Daniel Borkmann <[email protected]>; Andrii Nakryiko <[email protected]>; John Fastabend <[email protected]>; David S. Miller <[email protected]>; Eric Dumazet <[email protected]>; Jakub Kicinski <[email protected]>; Paolo Abeni <[email protected]>; Lawrence Brakmo <[email protected]>; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] bpf: add is_locked_tcp_sock guard for sock_ops rtt_min access
Quoted 10 lines · Werner Kasselman
sock_ops_convert_ctx_access()generates BPF instructions to inline context field accesses forBPF_PROG_TYPE_SOCK_OPSprograms. For tcp_sock-specific fields likesnd_cwnd,srtt_us, etc., it uses theSOCK_OPS_GET_TCP_SOCK_FIELD()macro which checksis_locked_tcp_sockand returns 0 when the socket is not a locked full TCP socket.However, the
rtt_minfield bypasses this guard entirely: it emits a raw two-instruction load sequence (load sk pointer, then load fromtcp_sock->rtt_minoffset) without checkingis_locked_tcp_sockfirst.
Quoted 9 lines
This is a problem because
bpf_skops_hdr_opt_len()andbpf_skops_write_hdr_opt()intcp_output.cset sock_ops.sk to atcp_request_sock(cast fromrequest_sock) during SYN-ACK processing, with is_fullsock=0 and is_locked_tcp_sock=0. If aSOCK_OPSprogram withBPF_SOCK_OPS_WRITE_HDR_OPT_CB_FLAGreadsctx->rtt_minin this callback, the generated code treats thetcp_request_sockpointer as atcp_sockand reads atoffsetof(struct tcp_sock, rtt_min)-- which is well past the end of thetcp_request_sockallocation, causing an out-of-bounds slab read.
This is not limited to hdr related CB flags.
It also happens to earlier CB flags that have a request_sock, such as BPF_SOCK_OPS_RWND_INIT.
Quoted 72 lines
The
rtt_minfield was introduced in the same commit as the othertcp_sockfields but was given hand-rolled access code because it reads
a sub-field (rtt_min.s[0].v, aminmax_sample) rather than a direct
struct member, making it incompatible with theSOCK_OPS_GET_FIELD()
macro. This hand-rolled code omitted theis_fullsockguard that the
macro provides. The guard was later renamed tois_locked_tcp_sockin
commitfd93eaffb3f9("bpf: Prevent unsafe access to the sock fields in the BPF timestamping callback").Add the
is_locked_tcp_sockguard to thertt_mincase, replicating the exact instruction pattern used bySOCK_OPS_GET_FIELD()including proper handling of the dst_reg==src_reg case with temp register save/restore. Useoffsetof(struct minmax_sample, v)for the sub-field offset to match the style inbpf_tcp_sock_convert_ctx_access().Found via AST-based call-graph analysis using sqry.
Fixes 44f0e43037d3 ("bpf: Add support for reading sk_state and more")Signed-off-by Werner Kasselman <[email protected]>--- net/core/filter.c | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-)net/core/filter.c index renamed from net/core/filter.c +0
-0
78b548158fb0..58f0735b18d9100644net/core/filter.c +36
-0@@ -10830,13 +10830,54 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,BUILD_BUG_ON (sizeof (struct minmax ) <sizeof (struct minmax_sample )); +/* Unlike other tcp_sock fields that use +* SOCK_OPS_GET_TCP_SOCK_FIELD(), rtt_min requires a +* custom access pattern because it reads a sub-field +* (rtt_min.s[0].v) rather than a direct struct member. +* We must still guard the access with is_locked_tcp_sock +* to prevent an OOB read when sk points to a +* tcp_request_sock (e.g., during SYN-ACK processing via +* bpf_skops_hdr_opt_len/bpf_skops_write_hdr_opt). +*/ + off = offsetof(struct tcp_sock , rtt_min) + + offsetof(struct minmax_sample , v); + { +int fullsock_reg = si->dst_reg, reg =BPF_REG_9 , jmp =2 ; + +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_STX_MEM (BPF_DW , si->src_reg, reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + fullsock_reg = reg; + jmp +=2 ; + } + *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( + struct bpf_sock_ops_kern, + is_locked_tcp_sock), + fullsock_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + is_locked_tcp_sock)); + *insn++ =BPF_JMP_IMM (BPF_JEQ , fullsock_reg,0 , jmp); +if (si->dst_reg == si->src_reg) + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp)); *insn++ = BPF_LDX_MEM(BPF_FIELD_SIZEOF( struct bpf_sock_ops_kern, sk), si->dst_reg, si->src_reg,
net/core/filter.c static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, · line 10878
Quoted 10 lines
net/core/filter.c +6
-3offsetof(struct bpf_sock_ops_kern, sk)); - *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, - offsetof(struct tcp_sock , rtt_min) + - sizeof_field(struct minmax_sample, t)); + *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, off); +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_JMP_A (1 ); + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp));
There is an existing bug in this copy-and-paste codes [1] and now is repeated here, so please find a way to refactor it to be reusable instead of duplicating it.
This also needs a test. It should be a subtest of [1], so [1] need to land first.
PATCH v2 1/1 bpf: guard sock_ops rtt_min access with is_locked_tcp_sock
In reply to Werner Kasselmansock_ops_convert_ctx_access() emits guarded reads for tcp_sock-backed bpf_sock_ops fields such as snd_cwnd, srtt_us, snd_ssthresh, rcv_nxt, snd_nxt, snd_una, mss_cache, ecn_flags, rate_delivered, and rate_interval_us. Those accesses go through SOCK_OPS_GET_TCP_SOCK_FIELD(), which checks is_locked_tcp_sock before dereferencing sock_ops.sk.
The rtt_min case is different. Because it reads a subfield of struct minmax, it uses a custom open-coded load sequence instead of the usual helper macro, and that sequence currently dereferences sock_ops.sk without checking is_locked_tcp_sock first.
This is unsafe when sock_ops.sk points to a request_sock-backed object instead of a locked full tcp_sock. That is reachable not only from the SYNACK header option callbacks, but also from other request_sock-backed sock_ops callbacks such as BPF_SOCK_OPS_TIMEOUT_INIT, BPF_SOCK_OPS_RWND_INIT, and BPF_SOCK_OPS_NEEDS_ECN. In those cases, reading ctx->rtt_min makes the generated code treat a request_sock as a tcp_sock and read beyond the end of the request_sock allocation.
Fix the rtt_min conversion by adding the same is_locked_tcp_sock guard used for the other tcp_sock field reads. Also make the accessed subfield explicit by using offsetof(struct minmax_sample, v).
Add a selftest that verifies request_sock-backed sock_ops callbacks see ctx->rtt_min as zero after the fix.
Found via AST-based call-graph analysis using sqry.
--- net/core/filter.c | 53 +++++++++++++++---- .../selftests/bpf/prog_tests/tcpbpf_user.c | 9 ++++ .../selftests/bpf/progs/test_tcpbpf_kern.c | 21 ++++++++ tools/testing/selftests/bpf/test_tcpbpf.h | 6 +++ 4 files changed, 79 insertions(+), 10 deletions(-)
net/core/filter.c +43 -10
@@ -10827,16 +10827,49 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,case offsetof(struct bpf_sock_ops , rtt_min): BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != sizeof(struct minmax)); -BUILD_BUG_ON (sizeof (struct minmax ) < -sizeof (struct minmax_sample )); - - *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( - struct bpf_sock_ops_kern, sk), - si->dst_reg, si->src_reg, - offsetof(struct bpf_sock_ops_kern , sk)); - *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, - offsetof(struct tcp_sock , rtt_min) + - sizeof_field(struct minmax_sample, t)); + BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, rtt_min) != + sizeof_field(struct minmax_sample, v)); + off = offsetof(struct tcp_sock , rtt_min) + + offsetof(struct minmax_sample , v); + + { +int fullsock_reg = si->dst_reg, reg =BPF_REG_9 , jmp =2 ; + +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == reg || si->src_reg == reg) + reg--; +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_STX_MEM (BPF_DW , si->src_reg, reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + fullsock_reg = reg; + jmp +=2 ; + } + *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( + struct bpf_sock_ops_kern, + is_locked_tcp_sock), + fullsock_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + is_locked_tcp_sock)); + *insn++ =BPF_JMP_IMM (BPF_JEQ , fullsock_reg,0 , jmp); +if (si->dst_reg == si->src_reg) + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( + struct bpf_sock_ops_kern, sk), + si->dst_reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , sk)); + *insn++ =BPF_LDX_MEM (BPF_W , si->dst_reg, si->dst_reg, + off); +if (si->dst_reg == si->src_reg) { + *insn++ =BPF_JMP_A (1 ); + *insn++ =BPF_LDX_MEM (BPF_DW , reg, si->src_reg, + offsetof(struct bpf_sock_ops_kern , + temp)); + } + }break ;case offsetof(struct bpf_sock_ops , bpf_sock_ops_cb_flags):
tools/testing/selftests/bpf/prog_tests/tcpbpf_user.c +9 -0
@@ -42,6 +42,15 @@ static void verify_result(struct tcpbpf_globals *result)/* check getsockopt for window_clamp */ ASSERT_EQ (result->window_clamp_client,9216 ,"window_clamp_client" );ASSERT_EQ (result->window_clamp_server,9216 ,"window_clamp_server" ); + +ASSERT_EQ (result->timeout_init_req_seen,1 ,"timeout_init_req_seen" ); +ASSERT_EQ (result->timeout_init_req_rtt_min,0 ,"timeout_init_req_rtt_min" ); + +ASSERT_EQ (result->rwnd_init_req_seen,1 ,"rwnd_init_req_seen" ); +ASSERT_EQ (result->rwnd_init_req_rtt_min,0 ,"rwnd_init_req_rtt_min" ); + +ASSERT_EQ (result->needs_ecn_req_seen,1 ,"needs_ecn_req_seen" ); +ASSERT_EQ (result->needs_ecn_req_rtt_min,0 ,"needs_ecn_req_rtt_min" ); }static void run_test (struct tcpbpf_globals *result)
tools/testing/selftests/bpf/progs/test_tcpbpf_kern.c +21 -0
@@ -85,6 +85,27 @@ int bpf_testcb(struct bpf_sock_ops *skops) global.event_map |= (1 << op);switch (op) { +case BPF_SOCK_OPS_TIMEOUT_INIT : +if (!skops->is_fullsock) { + global.timeout_init_req_seen =1 ; + global.timeout_init_req_rtt_min = skops->rtt_min; + } + rv =-1 ; +break ; +case BPF_SOCK_OPS_RWND_INIT : +if (!skops->is_fullsock) { + global.rwnd_init_req_seen =1 ; + global.rwnd_init_req_rtt_min = skops->rtt_min; + } + rv =0 ; +break ; +case BPF_SOCK_OPS_NEEDS_ECN : +if (!skops->is_fullsock) { + global.needs_ecn_req_seen =1 ; + global.needs_ecn_req_rtt_min = skops->rtt_min; + } + rv =0 ; +break ;case BPF_SOCK_OPS_TCP_CONNECT_CB : rv =bpf_setsockopt (skops,SOL_TCP ,TCP_WINDOW_CLAMP , &window_clamp,sizeof (window_clamp));
tools/testing/selftests/bpf/test_tcpbpf.h +6 -0
@@ -18,5 +18,11 @@ struct tcpbpf_globals {__u32 tcp_saved_syn;__u32 window_clamp_client;__u32 window_clamp_server; +__u32 timeout_init_req_seen; +__u32 timeout_init_req_rtt_min; +__u32 rwnd_init_req_seen; +__u32 rwnd_init_req_rtt_min; +__u32 needs_ecn_req_seen; + __u32 needs_ecn_req_rtt_min; };#endif
Signature · 1 line
2.43.0
Re: [PATCH v2] bpf: guard sock_ops rtt_min access with is_locked_tcp_sock
Quoted 55 lines · Werner Kasselman
sock_ops_convert_ctx_access()emits guarded reads for tcp_sock-backedbpf_sock_opsfields such assnd_cwnd,srtt_us,snd_ssthresh,rcv_nxt,snd_nxt,snd_una,mss_cache,ecn_flags,rate_delivered, andrate_interval_us. Those accesses go throughSOCK_OPS_GET_TCP_SOCK_FIELD(), which checksis_locked_tcp_sockbefore dereferencing sock_ops.sk.The
rtt_mincase is different. Because it reads a subfield of struct minmax, it uses a custom open-coded load sequence instead of the usual helper macro, and that sequence currently dereferences sock_ops.sk without checkingis_locked_tcp_sockfirst.This is unsafe when sock_ops.sk points to a request_sock-backed object instead of a locked full
tcp_sock. That is reachable not only from the SYNACK header option callbacks, but also from other request_sock-backedsock_opscallbacks such asBPF_SOCK_OPS_TIMEOUT_INIT,BPF_SOCK_OPS_RWND_INIT, andBPF_SOCK_OPS_NEEDS_ECN. In those cases, readingctx->rtt_minmakes the generated code treat arequest_sockas atcp_sockand read beyond the end of therequest_sockallocation.Fix the
rtt_minconversion by adding the sameis_locked_tcp_sockguard used for the othertcp_sockfield reads. Also make the accessed subfield explicit by usingoffsetof(struct minmax_sample, v).Add a selftest that verifies request_sock-backed
sock_opscallbacks seectx->rtt_minas zero after the fix.Found via AST-based call-graph analysis using sqry.
Fixes 44f0e43037d3 ("bpf: Add support for reading sk_state and more")Signed-off-by Werner Kasselman <[email protected]>--- net/core/filter.c | 53 +++++++++++++++---- .../selftests/bpf/prog_tests/tcpbpf_user.c | 9 ++++ .../selftests/bpf/progs/test_tcpbpf_kern.c | 21 ++++++++ tools/testing/selftests/bpf/test_tcpbpf.h | 6 +++ 4 files changed, 79 insertions(+), 10 deletions(-)net/core/filter.c +0
-8@@ -10827,16 +10827,49 @@ static u32 sock_ops_convert_ctx_access(enum bpf_access_type type,case offsetof(struct bpf_sock_ops , rtt_min): BUILD_BUG_ON(sizeof_field(struct tcp_sock, rtt_min) != sizeof(struct minmax)); -BUILD_BUG_ON (sizeof (struct minmax ) < -sizeof (struct minmax_sample )); - - *insn++ =BPF_LDX_MEM (BPF_FIELD_SIZEOF ( - struct bpf_sock_ops_kern, sk), - si->dst_reg, si->src_reg, - offsetof(struct bpf_sock_ops_kern , sk)); - *insn++ = BPF_LDX_MEM(BPF_W, si->dst_reg, si->dst_reg,
net/core/filter.c static u32 sock_ops_convert_ctx_access(enum bpf_access_type type, · line 10837
Quoted 10 lines
net/core/filter.c +8
-2- offsetof(struct tcp_sock , rtt_min) + - sizeof_field(struct minmax_sample, t)); + BUILD_BUG_ON(sizeof_field(struct bpf_sock_ops, rtt_min) != + sizeof_field(struct minmax_sample, v)); + off = offsetof(struct tcp_sock , rtt_min) + + offsetof(struct minmax_sample , v); + + { +int fullsock_reg = si->dst_reg, reg =BPF_REG_9 , jmp =2 ; +
please de-claude your patches before posting.
pw-bot: cr