On Tue, Dec 1, 2026, at 11:17, Björn Töpel wrote:
> Each call to bpf_tcp_gen_syncookie(), bpf_tcp_raw_gen_syncookie_ipv4() > or bpf_tcp_raw_gen_syncookie_ipv6() reads th->doff twice. The helper > first checks that th->doff matches th_len, the header length that the > verifier allowed. tcp_parse_mss_option(), called directly or through > tcp_get_syncookie_mss(), then reads th->doff again to find the end of > the options. > > The header can change between the two reads when userspace can write > the memory it lives in. That is the case for packets in the UMEM of an > AF_XDP zero-copy socket,
Good catch, thanks for the fix! Acked-by: Alice Mikityanska <alic…@fastmail.im> Luckily, packets XDP_PASSed from UMEM are copied, otherwise such corruptions would be inevitable.
> and for values of a BPF_F_MMAPABLE array map. > If th->doff grows from 5 to 15 in between, the option walk reads 40 > bytes past the header that the verifier allowed, and faults when the > header ends a mapping. > > Pass the checked length to tcp_parse_mss_option(), so that each call > reads th->doff once. > > A header that changes during the call can still give another MSS or > cookie, as any rewritten packet would, but the walk stays within > th_len. The helpers that check cookies read no length from the header > and stay as they are. The functions that take the new argument have no > other callers. > > Overhead; This costs nothing measurable. A BPF_PROG_TEST_RUN loop that > calls bpf_tcp_raw_gen_syncookie_ipv4() on a 40-byte header takes 136 > ns per run with and without this patch. > > Found by an AI code review agent, and reproduced in QEMU without > kernel changes. An XDP program on an igb zero-copy queue calls the raw > IPv4 helper on a header in the last 20 bytes of the UMEM, while a > userspace thread flips th->doff between 5 and 15. The kernel oopses in > tcp_parse_mss_option() on the guard page after the UMEM within > seconds. A BPF_PROG_TEST_RUN program with the header at the end of an > mmap()ed array map value oopses the same way. With this patch, neither > oopses, and the test_progs tests xdp_synproxy, btf_skc_cls_ingress, > tcp_custom_syncookie and cls_redirect pass. > > Fixes: 70d66244317e ("bpf: add bpf_tcp_gen_syncookie helper") > Fixes: 33bf9885040c ("bpf: Add helpers to issue and check SYN cookies in XDP") > Signed-off-by: Björn Töpel <bjo…@kernel.org> > --- > include/net/tcp.h | 8 ++++---- > net/core/filter.c | 8 ++++---- > net/ipv4/tcp_input.c | 8 ++++---- > net/ipv4/tcp_ipv4.c | 4 ++-- > net/ipv6/tcp_ipv6.c | 4 ++-- > 5 files changed, 16 insertions(+), 16 deletions(-) > > diff --git a/include/net/tcp.h b/include/net/tcp.h > index 869e780fcb48..c5348b358290 100644 > --- a/include/net/tcp.h > +++ b/include/net/tcp.h > @@ -547,13 +547,13 @@ void tcp_parse_options(const struct net *net, > const struct sk_buff *skb, > * BPF SKB-less helpers > */ > u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph, > - struct tcphdr *th, u32 *cookie); > + struct tcphdr *th, u32 th_len, u32 *cookie); > u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph, > - struct tcphdr *th, u32 *cookie); > -u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss); > + struct tcphdr *th, u32 th_len, u32 *cookie); > +u16 tcp_parse_mss_option(const struct tcphdr *th, u32 th_len, u16 > user_mss); > u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops, > const struct tcp_request_sock_ops *af_ops, > - struct sock *sk, struct tcphdr *th); > + struct sock *sk, struct tcphdr *th, u32 th_len); > /* > * TCP v4 functions exported for the inet6 API > */ > diff --git a/net/core/filter.c b/net/core/filter.c > index ea20fd2be65c..7f90099ae99c 100644 > --- a/net/core/filter.c > +++ b/net/core/filter.c > @@ -8073,7 +8073,7 @@ BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, > sk, void *, iph, u32, iph_len, > if (sk->sk_family == AF_INET6 && ipv6_only_sock(sk)) > return -EINVAL; > > - mss = tcp_v4_get_syncookie(sk, iph, th, &cookie); > + mss = tcp_v4_get_syncookie(sk, iph, th, th_len, &cookie); > break; > > #if IS_ENABLED(CONFIG_IPV6) > @@ -8084,7 +8084,7 @@ BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *, > sk, void *, iph, u32, iph_len, > if (sk->sk_family != AF_INET6) > return -EINVAL; > > - mss = tcp_v6_get_syncookie(sk, iph, th, &cookie); > + mss = tcp_v6_get_syncookie(sk, iph, th, th_len, &cookie); > break; > #endif /* CONFIG_IPV6 */ > > @@ -8434,7 +8434,7 @@ BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struct > iphdr *, iph, > if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4)) > return -EINVAL; > > - mss = tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT; > + mss = tcp_parse_mss_option(th, th_len, 0) ?: TCP_MSS_DEFAULT; > cookie = __cookie_v4_init_sequence(iph, th, &mss); > > return cookie | ((u64)mss << 32); > @@ -8463,7 +8463,7 @@ BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struct > ipv6hdr *, iph, > if (unlikely(th_len < sizeof(*th) || th_len != th->doff * 4)) > return -EINVAL; > > - mss = tcp_parse_mss_option(th, 0) ?: mss_clamp; > + mss = tcp_parse_mss_option(th, th_len, 0) ?: mss_clamp; > cookie = __cookie_v6_init_sequence(iph, th, &mss); > > return cookie | ((u64)mss << 32); > diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c > index d9a66a1342ef..0b14b92a37e5 100644 > --- a/net/ipv4/tcp_input.c > +++ b/net/ipv4/tcp_input.c > @@ -4560,10 +4560,10 @@ static bool smc_parse_options(const struct > tcphdr *th, > /* Try to parse the MSS option from the TCP header. Return 0 on > failure, clamped > * value on success. > */ > -u16 tcp_parse_mss_option(const struct tcphdr *th, u16 user_mss) > +u16 tcp_parse_mss_option(const struct tcphdr *th, u32 th_len, u16 > user_mss) > { > const unsigned char *ptr = (const unsigned char *)(th + 1); > - int length = (th->doff * 4) - sizeof(struct tcphdr); > + int length = th_len - sizeof(struct tcphdr); > u16 mss = 0; > > while (length > 0) { > @@ -7703,7 +7703,7 @@ static void tcp_reqsk_record_syn(const struct > sock *sk, > */ > u16 tcp_get_syncookie_mss(struct request_sock_ops *rsk_ops, > const struct tcp_request_sock_ops *af_ops, > - struct sock *sk, struct tcphdr *th) > + struct sock *sk, struct tcphdr *th, u32 th_len) > { > struct tcp_sock *tp = tcp_sk(sk); > u16 mss; > @@ -7720,7 +7720,7 @@ u16 tcp_get_syncookie_mss(struct request_sock_ops > *rsk_ops, > return 0; > } > > - mss = tcp_parse_mss_option(th, READ_ONCE(tp->rx_opt.user_mss)); > + mss = tcp_parse_mss_option(th, th_len, READ_ONCE(tp->rx_opt.user_mss)); > if (!mss) > mss = af_ops->mss_clamp; > > diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c > index 2fc92a7884fc..6c004f42f3b4 100644 > --- a/net/ipv4/tcp_ipv4.c > +++ b/net/ipv4/tcp_ipv4.c > @@ -1808,12 +1808,12 @@ static struct sock *tcp_v4_cookie_check(struct > sock *sk, struct sk_buff *skb) > } > > u16 tcp_v4_get_syncookie(struct sock *sk, struct iphdr *iph, > - struct tcphdr *th, u32 *cookie) > + struct tcphdr *th, u32 th_len, u32 *cookie) > { > u16 mss = 0; > #ifdef CONFIG_SYN_COOKIES > mss = tcp_get_syncookie_mss(&tcp_request_sock_ops, > - &tcp_request_sock_ipv4_ops, sk, th); > + &tcp_request_sock_ipv4_ops, sk, th, th_len); > if (mss) { > *cookie = __cookie_v4_init_sequence(iph, th, &mss); > tcp_synq_overflow(sk); > diff --git a/net/ipv6/tcp_ipv6.c b/net/ipv6/tcp_ipv6.c > index 5c7ed38f1f56..82849d80faaa 100644 > --- a/net/ipv6/tcp_ipv6.c > +++ b/net/ipv6/tcp_ipv6.c > @@ -1303,12 +1303,12 @@ static struct sock *tcp_v6_cookie_check(struct > sock *sk, struct sk_buff *skb) > } > > u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph, > - struct tcphdr *th, u32 *cookie) > + struct tcphdr *th, u32 th_len, u32 *cookie) > { > u16 mss = 0; > #ifdef CONFIG_SYN_COOKIES > mss = tcp_get_syncookie_mss(&tcp6_request_sock_ops, > - &tcp_request_sock_ipv6_ops, sk, th); > + &tcp_request_sock_ipv6_ops, sk, th, th_len); > if (mss) { > *cookie = __cookie_v6_init_sequence(iph, th, &mss); > tcp_synq_overflow(sk); > > base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046 > -- > 2.56.1