BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Leon Hwang" <leon.hwang@linux.dev>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH bpf v3 1/2] bpf: Update transport_header when encapsulating UDP tunnel in lwt
Date: Mon, 01 Jun 2026 15:17:07 +0000	[thread overview]
Message-ID: <20260601151707.C51601F00893@smtp.kernel.org> (raw)
In-Reply-To: <20260601150203.20352-2-leon.hwang@linux.dev>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Time-of-Check to Time-of-Use (TOCTOU) vulnerability in transport header offset calculation leads to out-of-bounds write.

Pre-existing issues:
- [Critical] Missing validation of `iph->ihl >= 5` allows unprivileged BPF programs to trigger a kernel panic in `ip_fast_csum`.
--

commit 0be2d8b388e17b2000afd2e38ff89f5f956084ae
Author: Leon Hwang <leon.hwang@linux.dev>

bpf: Update transport_header when encapsulating UDP tunnel in lwt

This commit updates bpf_lwt_push_ip_encap() to set the skb transport
header whenever the encapsulated packet uses UDP. This ensures that
drivers relying on the transport header for hardware checksum offload
do not drop packets due to incorrect checksums.

Cc: Leon Hwang <leon.huangfu@shopee.com>

> diff --git a/net/core/lwt_bpf.c b/net/core/lwt_bpf.c
> index f71ef82a5f3d3..65d1dfbf3312b 100644
> --- a/net/core/lwt_bpf.c
> +++ b/net/core/lwt_bpf.c
> @@ -612,10 +613,16 @@ int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
>  		ipv4 = true;
>  		if (unlikely(len < iph->ihl * 4))
>  			return -EINVAL;

[Severity: Critical]
This is a pre-existing issue, but does this code properly validate the
minimum IPv4 header length?

Because len is an unsigned u32, if a BPF program provides a buffer where
iph->ihl is 0, iph->ihl * 4 will be 0. Since len < 0 is never true, the
check trivially passes without returning -EINVAL.

Later in the function, ip_fast_csum() is called with iph->ihl. On some
architectures (like PowerPC and MIPS), the ip_fast_csum() implementation
assumes ihl >= 5 and subtracts from it, causing an integer underflow in the
loop counter that leads to a deterministic kernel panic.

Should there be an explicit check that enforces iph->ihl >= 5 before the
length calculation?

> +		is_udp_tunnel = iph->protocol == IPPROTO_UDP;
> +		if (unlikely(is_udp_tunnel && len < iph->ihl * 4 + sizeof(struct udphdr)))
> +			return -EINVAL;
>  	} else if (iph->version == 6) {

[ ... ]

> @@ -637,6 +644,10 @@ int bpf_lwt_push_ip_encap(struct sk_buff *skb, void *hdr, u32 len, bool ingress)
>  	if (ingress)
>  		skb_postpush_rcsum(skb, iph, len);
>  	skb_reset_network_header(skb);
> +	if (ipv4 && is_udp_tunnel)
> +		skb_set_transport_header(skb, skb_network_offset(skb) + iph->ihl * 4);

[Severity: High]
Can iph->ihl change between the initial length validation and this call?

The hdr pointer (and thus iph) points to a buffer provided by the BPF
program, which might be concurrently modified by userspace via a shared
BPF map. Because skb_cow_head() acts as a compiler barrier, the compiler
will reload iph->ihl from this untrusted memory here.

If it is concurrently changed to a larger value (like 15) after the initial
validation, the transport header offset could be set beyond the end of the
SKB data (skb->tail).

Could this lead to out-of-bounds memory writes when network drivers later
dereference this transport header to update checksums?

> +	else if (!ipv4 && is_udp_tunnel)
> +		skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct ipv6hdr));
>  	memcpy(skb_network_header(skb), hdr, len);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260601150203.20352-1-leon.hwang@linux.dev?part=1

  reply	other threads:[~2026-06-01 15:17 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-01 15:02 [PATCH bpf v3 0/2] bpf: Update transport_header when encapsulating UDP tunnel in lwt Leon Hwang
2026-06-01 15:02 ` [PATCH bpf v3 1/2] " Leon Hwang
2026-06-01 15:17   ` sashiko-bot [this message]
2026-06-01 15:28   ` Eric Dumazet
2026-06-01 15:33     ` Eric Dumazet
2026-06-01 15:45   ` bot+bpf-ci
2026-06-01 17:03   ` Emil Tsalapatis
2026-06-02  2:11     ` Leon Hwang
2026-06-02  3:17   ` Jiayuan Chen
2026-06-02  5:38     ` Leon Hwang
2026-06-01 15:02 ` [PATCH bpf v3 2/2] selftests/bpf: Add tests to verify the fix of encapsulating VxLAN " Leon Hwang
2026-06-01 18:24   ` Emil Tsalapatis
2026-06-02  2:27     ` Leon Hwang

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=20260601151707.C51601F00893@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=leon.hwang@linux.dev \
    --cc=sashiko-reviews@lists.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