Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=202.12.124.156 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796134029; b=gRDNfJFTGn2hod/W7y1bFxXH0tChPvqOKXuNl5cRgzBnI0R9FrEu75VakO0je0iPbS32 WgGKW2U15NLmYN2OimN08CTyPMrcVyNuxUDVBcELsy7YSK+nXcwYDKq/hp6POb0FY5Dj+ b2uy3xN/Jl63GKqm9zxV53wuB7wQWtor0+XPZSO2juQ6smxbms0yG3pk7KFIRUkS1d5m8 pDBTGtLPyc2L0ZgebGs74+8Vhfeu8p18LU1skoorPu59zWOgVvqyBqk4wUPmN2ebokMqv MWGqCvtl3w+J1lJippxRnh+oMv8UypgWuRfaQHKI5uVaAZkmq1peCe6sBqNui9MYJHg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134029; h=DMARC-Filter:Received:Received:DKIM-Signature:DKIM-Signature: X-DKIM2-Info:DKIM2-Signature:X-DKIM2-Info:Message-Instance: X-ME-Sender:X-ME-Proxy-Cause:X-ME-Proxy:Feedback-ID:Received:X-Mailer: MIME-Version:Date:From:To:Cc:Message-Id:In-Reply-To:References: Subject:Content-Type:Content-Transfer-Encoding; bh=Y2NacEGCr5cUU5pJ/UiR5w6AlzeeVaQwJ4z7BrjqmCY=; b=HvaJHUXIRjcM+eNO8zq/V7khrf+i4nrCK4z4uHi9Mwxw9gRAAf5SrKvTrB5gbwrdP5RX XCzihjsio4uSBEH9Um06zLJNNj/25sdDrn3XIQJpytEbjbbme69fEFfFxSUXckhXVJEty C+ZylJAsSyI7hr0fRZAZjDlytt4cTcKQrDY+gxi/JGpftG1Faf5KP+GIO3KwX7tKb+UQR hezRUr9KKtvGnV5IRnWzZjQwhpX4JUnc4bMlPqNi1DVe+SLsBy9hsNYMNFxS97LYjE3sE CBJbEgbJ9zhV3TNv7In+Ze9lDGQAB8C6hFTkC/gFy575Q7N3w4oyIOjyBBa55S9FmdA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=fastmail.im; spf=pass smtp.mailfrom=fastmail.im; dkim=pass header.d=fastmail.im header.i=@fastmail.im header.a=rsa-sha256 header.s=fm3 header.b=FxHFicbA; dkim=pass header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=uHy6GxaO; arc=none smtp.remote-ip=202.12.124.156 Received: from fhigh-b5-smtp.messagingengine.com (fhigh-b5-smtp.messagingengine.com [202.12.124.156]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FB12606A31A for ; Tue, 1 Dec 2026 14:07:08 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 8FB12606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=fastmail.im Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=fastmail.im Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=fastmail.im header.i=@fastmail.im header.a=rsa-sha256 header.s=fm3 header.b=FxHFicbA; dkim=pass (2048-bit key, unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.a=rsa-sha256 header.s=fm3 header.b=uHy6GxaO Received: from ams-compute-01.internal (ams-compute-01.internal [10.64.2.61]) by mailfhigh.stl.internal (Postfix) with ESMTP id 7BD607A03A8 for ; Tue, 1 Dec 2026 09:07:07 -0500 (EST) Received: from ams-imap-19 ([10.64.2.39]) by ams-compute-01.internal (MEProxy); Tue, 01 Dec 2026 09:07:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fastmail.im; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1796134010; x=1796220410; bh=Y2NacEGCr5cUU5pJ/UiR5w6AlzeeVaQwJ4z7BrjqmCY=; b= FxHFicbAa7GC7qvJU8eHgqNym0WtghQPxeG5pcCmyOttK/EI+yg7ntuclIcHSeEW 8U4ZC9JoqtvAHFb3RuIPuhzIOTaQ45rIZdkPTxNCE6jOTwGGNJaXyrF9n5rNGr0I WemROklpH5BrH+Mjrkd0PQl70rOYIeaZiyBQUVn5XI2D9Lz7W0mbUelEGnE+GApO p4DE5YthXzq2BXdkG0eicZyWl7o0r3GB1WOiHKK7Tv3wnaxHB2ZfM1d0nVwl+7uD tys1Dffilcun2vtEV4hMbFzAGRu67EqwnjodaS86Sz4eEwyPCQIBL7+OvY84jlZs VVZWz0oHCPyQqiW0wtrtCQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1796134010; x= 1796220410; bh=Y2NacEGCr5cUU5pJ/UiR5w6AlzeeVaQwJ4z7BrjqmCY=; b=u Hy6GxaOAynh1I40608DjfQXkF1FYze57JI91fs1wkkcOohtL50Lu8kyoPS0g2ggz cel9Cp864un8Lw1fbAGQq9GoOqh+6G1/cZufNUY40a5GJ8PE6e/uYMnKTaGWXlc6 Jwp6cCa2yj0vNfGJsq9f4v8VXHopbJJwE/BDRHS+j46+TBthekEJLTVVBNK3MVt2 Dg50mVLaKXoo0QIt4f3ON8F4LCJAuuWNqOOSKx1zAu2ydyUo/cGmX2DEe4oU82If QL2ValI8uKJL7yJSzFOnGVA2y4AxI+jnAV6qIvSKZkRE1ySrmBE89eyIJp3IOcIv mNLiytomIbuq9PvnSTRkQ== X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=sign d=fastmail.im a=rsa-sha256; DKIM2-Signature: i=1; m=1; t=1796134010; d=fastmail.im; mf=PGFsaWNlLmtlcm5lbEBmYXN0bWFpbC5pbT4=; rt=PGJwZkB2Z2VyLmtlcm5lbC5vcmc+; s=fm3:rsa-sha256:liGBzV6T4kxWh9cATiBbhmVR+lNGb8Ry4kI5WuacuRH/dUR ppA4rbd9BdsQKu7cEgrp9/JiUfG+P1I3nqJFyPJMXARbRAncAscuboxFsdSMwC/x OI3DpyE9Y/DdWjEwHh+7iwvHkur1oiAQfuWRDb32g5OU1s+tPSoe2/oKD4DRmEjK WfwxqTdZwH/a4wWTzFrJuf2BA9EmkpG4iHrCT72YU0/3WS9/4jkMnvG+D9xVtaq1 TQUHsbBMaZ1IHwks+18okN5dROYAnTgLlK9eitLoyBKHfCgdh4cWdue17osK1qUd ZNVmjmLfJlBXg90fX4knJwkav/ByxLPvPoPzG6g==; X-DKIM2-Info: draft=ietf-dkim-dkim2-spec-09; repo=github.com/dkim2wg/interop; date=2026-11-03; sw=lmtpprox; action=mi-m=1; hc=12; hn=cc,content-transfer-encoding,content-type,date,feedback-id, from,in-reply-to,message-id,mime-version,references,subject,to; Message-Instance: m=1; h=sha256:/SDDh+WslnKbIqEaJyNB3wg2BV9s1dJb6/rNc9jTIqo=:G7oGpQK/eiHomJ/sXn6EJ2PJ2CjFx1XMO/VpVaPYEhg=; X-ME-Sender: X-ME-Proxy-Cause: dmFkZTFzFwqURzZj1hnTQOc+tXmK8uOu+feYxP00GyNFiMtjQ/8ARIjYJspiUh8ciUEyDP PqLCSxU4VGGFXI4P6HMn865wN/yAw66VD19kwdPXKdZfOBviyFGWqCvNcHNthuuTQoSvkX 41iNQ04Boh3QW43APmBm6D3LA2RUqt1JqmdtlcF473AsMV8YFJ4UpfrXKVovnhXJZ0uc3d BvnEOX/lTYYeH6Z4zvo3RSOLcezelUXIcXTkOJuGh6VG5IZRSL0cGmv8hweMOv9pg6V/DQ HnSTEMpz6XHNtFqGWYC7o25Lh+zqvmMbfiwymv5Sbcr4g2IXOqw82p2sT8meNFlnL7laj9 lsrmWlXtinV6VVBT3+copmy9kUi/Ol2oGu5ryjLkJT6SFAy5Q/2HatN8lp4EgMjAZ9xvLo nRE9q6IzK5sGJhAdYNqm6K/W4WBfUwTxuCrDeEiUT4kebn6OwFSysyCnrV3c7wfbASKb9/ eSbNsJIpkBkATogvTrumbLlGwzgC0osjpRRPpr+VtqRAJpvp3Fk16u+zciyhIRaWofOzzR vfbamW31v5mxlSW9zKGLp3nQZZmJt1aMcX2j331SC+aM4unBt2uftCLDquzT+HFLIbyoNd 9AapLBbtpgmlULB8nAPEIuMta7avfSzjN7lgvTU+SrWfsi+j+Bw0yg+m3PMA X-ME-Proxy: Feedback-ID: i559e4809:Fastmail Received: by mailuser.ams.internal (Postfix, from userid 501) id B6D822F8005B; Tue, 1 Dec 2026 09:06:42 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Date: Tue, 01 Dec 2026 16:06:22 +0200 From: "Alice Mikityanska" To: =?UTF-8?Q?Bj=C3=B6rn_T=C3=B6pel?= Cc: "Kuniyuki Iwashima" , "Simon Horman" , "Martin KaFai Lau" , "John Fastabend" , "Stanislav Fomichev" , "Song Liu" , "Yonghong Song" , "Jiri Olsa" , "Emil Tsalapatis" , "Ihor Solodrai" , "Lorenz Bauer" , "Petar Penkov" , "Tariq Toukan" , "Alexei Starovoitov" , "Daniel Borkmann" , "Andrii Nakryiko" , "Eduard Zingerman" , "Kumar Kartikeya Dwivedi" , "Eric Dumazet" , "Neal Cardwell" , "David S. Miller" , "Jakub Kicinski" , "Paolo Abeni" , "Willem de Bruijn" , "Jesper Dangaard Brouer" , bpf@vger.kernel.org, netdev@vger.kernel.org Message-Id: <2dba3fd8-a599-456e-9c47-66fcfc73341d@app.fastmail.com> In-Reply-To: <20261201091755.1130605-1-bjorn@kernel.org> References: <20261201091755.1130605-1-bjorn@kernel.org> Subject: Re: [PATCH bpf] bpf: Use the checked TCP header length in the syncookie helpers Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Tue, Dec 1, 2026, at 11:17, Bj=C3=B6rn T=C3=B6pel 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 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=C3=B6rn T=C3=B6pel > --- > 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,=20 > 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=20 > 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 *,=20 > sk, void *, iph, u32, iph_len, > if (sk->sk_family =3D=3D AF_INET6 && ipv6_only_sock(sk)) > return -EINVAL; >=20 > - mss =3D tcp_v4_get_syncookie(sk, iph, th, &cookie); > + mss =3D tcp_v4_get_syncookie(sk, iph, th, th_len, &cookie); > break; >=20 > #if IS_ENABLED(CONFIG_IPV6) > @@ -8084,7 +8084,7 @@ BPF_CALL_5(bpf_tcp_gen_syncookie, struct sock *,=20 > sk, void *, iph, u32, iph_len, > if (sk->sk_family !=3D AF_INET6) > return -EINVAL; >=20 > - mss =3D tcp_v6_get_syncookie(sk, iph, th, &cookie); > + mss =3D tcp_v6_get_syncookie(sk, iph, th, th_len, &cookie); > break; > #endif /* CONFIG_IPV6 */ >=20 > @@ -8434,7 +8434,7 @@ BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv4, struc= t=20 > iphdr *, iph, > if (unlikely(th_len < sizeof(*th) || th_len !=3D th->doff * 4)) > return -EINVAL; >=20 > - mss =3D tcp_parse_mss_option(th, 0) ?: TCP_MSS_DEFAULT; > + mss =3D tcp_parse_mss_option(th, th_len, 0) ?: TCP_MSS_DEFAULT; > cookie =3D __cookie_v4_init_sequence(iph, th, &mss); >=20 > return cookie | ((u64)mss << 32); > @@ -8463,7 +8463,7 @@ BPF_CALL_3(bpf_tcp_raw_gen_syncookie_ipv6, struc= t=20 > ipv6hdr *, iph, > if (unlikely(th_len < sizeof(*th) || th_len !=3D th->doff * 4)) > return -EINVAL; >=20 > - mss =3D tcp_parse_mss_option(th, 0) ?: mss_clamp; > + mss =3D tcp_parse_mss_option(th, th_len, 0) ?: mss_clamp; > cookie =3D __cookie_v6_init_sequence(iph, th, &mss); >=20 > 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=20 > tcphdr *th, > /* Try to parse the MSS option from the TCP header. Return 0 on=20 > 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=20 > user_mss) > { > const unsigned char *ptr =3D (const unsigned char *)(th + 1); > - int length =3D (th->doff * 4) - sizeof(struct tcphdr); > + int length =3D th_len - sizeof(struct tcphdr); > u16 mss =3D 0; >=20 > while (length > 0) { > @@ -7703,7 +7703,7 @@ static void tcp_reqsk_record_syn(const struct=20 > 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 =3D tcp_sk(sk); > u16 mss; > @@ -7720,7 +7720,7 @@ u16 tcp_get_syncookie_mss(struct request_sock_op= s=20 > *rsk_ops, > return 0; > } >=20 > - mss =3D tcp_parse_mss_option(th, READ_ONCE(tp->rx_opt.user_mss)); > + mss =3D tcp_parse_mss_option(th, th_len, READ_ONCE(tp->rx_opt.user_m= ss)); > if (!mss) > mss =3D af_ops->mss_clamp; >=20 > 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=20 > sock *sk, struct sk_buff *skb) > } >=20 > 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 =3D 0; > #ifdef CONFIG_SYN_COOKIES > mss =3D 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 =3D __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=20 > sock *sk, struct sk_buff *skb) > } >=20 > 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 =3D 0; > #ifdef CONFIG_SYN_COOKIES > mss =3D 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 =3D __cookie_v6_init_sequence(iph, th, &mss); > tcp_synq_overflow(sk); > > base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046 > --=20 > 2.56.1