bpf: Use the checked TCP header length in the syncookie helpers
Subsystems (5)
PATCH 1/1 bpf: Use the checked TCP header length in the syncookie helpers
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.
--- 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(-)
include/net/tcp.h +4 -4
@@ -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 */
net/core/filter.c +4 -4
@@ -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 );
net/ipv4/tcp_input.c +4 -4
@@ -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;
net/ipv4/tcp_ipv4.c +2 -2
@@ -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);
net/ipv6/tcp_ipv6.c +2 -2
@@ -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
Signature · 1 line
2.56.1
Quoted 10 lines · Björn Töpel
Each call to
bpf_tcp_gen_syncookie(),bpf_tcp_raw_gen_syncookie_ipv4()orbpf_tcp_raw_gen_syncookie_ipv6()readsth->dofftwice. The helper first checks thatth->doffmatchesth_len, the header length that the verifier allowed.tcp_parse_mss_option(), called directly or throughtcp_get_syncookie_mss(), then readsth->doffagain 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_XDPzero-copy socket,
Good catch, thanks for the fix!
Luckily, packets XDP_PASSed from UMEM are copied, otherwise such corruptions would be inevitable.
Quoted 191 lines
and for values of a
BPF_F_MMAPABLEarray map.
Ifth->doffgrows 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 readsth->doffonce.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_RUNloop that callsbpf_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->doffbetween 5 and 15. The kernel oopses intcp_parse_mss_option()on the guard page after the UMEM within seconds. ABPF_PROG_TEST_RUNprogram with the header at the end of an mmap()ed array map value oopses the same way. With this patch, neither oopses, and thetest_progstestsxdp_synproxy,btf_skc_cls_ingress,tcp_custom_syncookieandcls_redirectpass.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 <[email protected]>--- 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(-)include/net/tcp.h +4
-4@@ -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
*/net/core/filter.c +1
-1@@ -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)net/core/filter.c +2
-2@@ -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);net/core/filter.c +1
-1return 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);net/core/filter.c +0
-0return cookie | ((u64 )mss <<32 );net/ipv4/tcp_input.c +0
-0@@ -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.
*/net/ipv4/tcp_input.c +1
-1-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)
{net/ipv4/tcp_input.c +2
-2const 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 structsock *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);net/ipv4/tcp_input.c +1
-1u16 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;net/ipv4/tcp_ipv4.c +2
-2@@ -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);net/ipv4/tcp_ipv4.c +0
-0tcp_synq_overflow (sk);net/ipv6/tcp_ipv6.c +2
-2@@ -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:
7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046Signature · 1 line
2.56.1