Article View: linux.kernel.netdev
Article #25119[PATCH bpf] bpf: Use the checked TCP header length in the syncookie helpers
From: Björn Töpel
Date: Tue, 01 Dec 2026 09:17
Date: Tue, 01 Dec 2026 09:17
188 lines
15619 bytes
15619 bytes
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, 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 <bjorn@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
Technical Details
Message-ID:
<20261201091755.1130605-1-bjorn@kernel.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374890-linux-kernel-netdev=dewy.ruShow Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374890-linux-kernel-netdev=dewy.ru From: bjorn@kernel.org (Björn Töpel) Newsgroups: linux.kernel.netdev Subject: [PATCH bpf] bpf: Use the checked TCP header length in the syncookie helpers Date: Tue, 1 Dec 2026 10:17:53 +0100 Organization: Usenet Network Message-ID: <20261201091755.1130605-1-bjorn@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: usenet.network; logging-data="1671963"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:N/omwGmaV2miGKvd12GKq8Nhg1I= sha256:mZPth/WEFdZKa6CgirN0Vn4X0DlrnaJCYrLuQRvN4LI= sha1:C9MGGzT4ccyIALg7/sK4Xi7O4Yc= sha256:WjW1uPRQxMfEVjinoQ2Ca9hG3TzQV7egVnpFpLtOxvI= List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 2EFFB1172F6; Tue, 1 Dec 2026 12:18:51 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.501 X-Spam-Status: No, score=-3.501 tagged_above=-9999 required=6.31 tests=[DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=kernel.org X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id zEFCAhOMw-aT; Tue, 1 Dec 2026 12:18:48 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-374890-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org X-Received: from tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114]) by mail.dewy.ru (Postfix) with ESMTPS id 12D1B1172F4; Tue, 1 Dec 2026 12:18:40 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by tor.lore.kernel.org (Postfix) with ESMTP id 9424B294FE; Tue, 1 Dec 2026 09:18:22 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796116691; b=njtbAlkxyAeijNiyJIXB0BbdP5A8pk81zmkRo0Hwp7+LYcrb2WY4jcLZPjOz5wS7LQRh AORivekaWquRFJaRHW8I/jbgKN5fRDHQpHkQYNhCYC+G5dl8dBbkGHnWdmUxXkSaWX+ns 2rAbYLh9q5yqboDfm83Syz9IAMpT7JXnynbsCMpgTfSBEXrx92A1gRwAIw/9gwLUCfBT9 eu7f3dbfpd6Pm4+b+XTS55+eAw67hWjemJJZINL07rCNzs8JR2TgLvwslTZ7esQfQPg7U 6iP6fTLtcPN6K67Cerv4fcQIMWUlONIvDNy4yWRnvrsUO/pfwB25+2mKyL0AfBsGiNg== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796116691; h=X-Original-To:Received:Received:DKIM-Signature:From:To:Cc:Subject: Date:Message-ID:X-Mailer:X-Mailing-List:List-Id:List-Post:List-Help: List-Subscribe:List-Unsubscribe:List-Owner:Precedence:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=j8JcPlQDugTAWcnaSCw0xA7BspYmMVXkMS8O2rDMCV8=; b=tqvBofybOpzDIcfZB2RLTAu3fVfkP/dHIlfarJYr8XJH/arY/jr0tllCgSBWc2983FGo q3UDR3mmHemzE4/FbVswwFgz7+yX2t8SMMTB9R4kN+gVhOqSaULdgheiQNf2oz/bjsgtu X1wFAjhT9Nc+lXcNU/PytnVVBpaZBQZcnOlD4Gq89cDVxFkO0CpkBCYvX99qkfYjHtywg dyz8abb7jMGdzlK8ld/E0hFs1gAhyLF38NHOIf9oxCxWTPxMgwFbNxJNHFDfk+lcZd3SB 6qX5j1MPIdRcSzSYpQtkaGokJvafOs94iIfqzE8jQZsTyyOKeZJoDluvjma2T4eSeLw== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kvVXuHSx; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id EDDF9606A6A0; Tue, 1 Dec 2026 09:18:10 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kvVXuHSx X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796116690; b=38vr1IAKhFqgprb2yz2Tob71x418d0szzYe/QKaBp7sOsfgz1arHBmrXnzWcoH0tJYdq wFYp7m9/8T3L9Eo7vibJoAMlOuH6cmwHNPGlyVQMVOHm1OzdDrwLd7N/WRfWlerI827H7 gz02nqjlfgbp815Ouznf82S03/xxs4GKSKcXGeQ3bfyX5CyzKvbJm/0HKA/nhND13r0YI pNeDvlg6kl+X6L2pToCbmNe1Jj0L0yurUClU2MwpFZw9JTgf0dpL44WZe3lSK1gXsP+nl L8vah0ABQgKeiOedIeBR4ZKU3nPDW0bzWXt4673tKalBsYpBCJEJOUh3xFKua+eavRg== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796116690; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: MIME-Version:Content-Type:Content-Transfer-Encoding; bh=j8JcPlQDugTAWcnaSCw0xA7BspYmMVXkMS8O2rDMCV8=; b=xipLSJ04yiFjamRGlVZ9JvhW87y/vLlzhoIykgKxKstweZlz7DLPQv1ErFl513SQ9gRp dynNsX2mCpZIL78O92Oa29pKHUFF4q4BCTS39XIe768iCW8/s46bDrCOO2r5r6vWLMdy9 8PojDongM+b5wRQQoMowIrz8rNd0v8DDXFSqUjm0oS2+yNp6SpYH6tpms0nEGuHpWxCZX WDQXjP/qvwF8OohnZ+JRxU/WxJDlqjdmhhyOpFc5lOZIp+fRjn+Y6lxDDJIKJPEhobTVR 7FQdWIU81By0wI6RlbACem0nRZfkQLAxCK8KK4Zh1E6BIyfS7rY4JnTDTNsBxbdvrYg== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kvVXuHSx; arc=none smtp.remote-ip=100.103.45.18 X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (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 1BE8D606A695; Tue, 1 Dec 2026 09:18:09 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9170B1F000C1; Tue, 1 Dec 2026 09:18:03 +0000 (UTC) X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796116689; bh=j8JcPlQDugTAWcnaSCw0xA7BspYmMVXkMS8O2rDMCV8=; h=From:To:Cc:Subject:Date; b=kvVXuHSxcoSCQzW2nQIsrgKinkzLCCEOHgHul+lyCKL/CTEYz7UDF/jyKuGWPqLOf SuM+8gqdQcDaFCFfNzSj6kyyyApeWoj7lrL8ZKpw0jxW9HQKiVHwd2usfbO3v0gu4Q AMeuwL/C/av2nXdLL2c6tjGjeqW1tY4dr6+LHRGfLDaj96r2Utqn+ZBYxSA33Gx8yY B9AisvxtvZ7L64LYhC876S5vtrAob8p6OeEHUG7a9HdQKCf8qjWPSg/vn52ayiLqnw tzjenySrGXQ9H08tqzIIaNXVWc4Al5o0PNp3o657nGa7FELS37CdoA+oc7hKeKlt6F P8A740OJQVPJA== X-Cc: Björn Töpel <bjorn@kernel.org>, Kuniyuki Iwashima <kuniyu@google.com>, Simon Horman <horms@kernel.org>, Martin KaFai Lau <martin.lau@linux.dev>, John Fastabend <john.fastabend@gmail.com>, Stanislav Fomichev <sdf@fomichev.me>, Song Liu <song@kernel.org>, Yonghong Song <yonghong.song@linux.dev>, Jiri Olsa <jolsa@kernel.org>, Emil Tsalapatis <emil@etsalapatis.com>, Ihor Solodrai <ihor.solodrai@linux.dev>, Lorenz Bauer <lmb@cloudflare.com>, Petar Penkov <ppenkov@google.com>, Tariq Toukan <tariqt@nvidia.com>, Alice Mikityanska <alice.kernel@fastmail.im> X-Mailer: git-send-email 2.56.1 X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list