From: "Björn Töpel" <bjorn@kernel.org>
To: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Eric Dumazet <edumazet@kernel.org>,
Neal Cardwell <ncardwell@google.com>,
"David S . Miller" <davem@davemloft.net>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Jesper Dangaard Brouer <hawk@kernel.org>,
bpf@vger.kernel.org, netdev@vger.kernel.org
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>
Subject: [PATCH bpf] bpf: Use the checked TCP header length in the syncookie helpers
Date: Tue, 1 Dec 2026 10:17:53 +0100 [thread overview]
Message-ID: <20261201091755.1130605-1-bjorn@kernel.org> (raw)
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
reply other threads:[~2026-12-01 9:18 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261201091755.1130605-1-bjorn@kernel.org \
--to=bjorn@kernel.org \
--cc=alice.kernel@fastmail.im \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=lmb@cloudflare.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=ncardwell@google.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=ppenkov@google.com \
--cc=sdf@fomichev.me \
--cc=song@kernel.org \
--cc=tariqt@nvidia.com \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox