BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Tianci Cao <ziye@zju.edu.cn>, bpf@vger.kernel.org
Cc: ast@kernel.org, daniel@iogearbox.net, john.fastabend@gmail.com,
	 andrii@kernel.org, martin.lau@linux.dev, song@kernel.org,
	yonghong.song@linux.dev, 	kpsingh@kernel.org, sdf@fomichev.me,
	haoluo@google.com, jolsa@kernel.org, 	tangyazhou518@outlook.com,
	shenghaoyuan0928@163.com
Subject: Re: [PATCH bpf-next 1/2] bpf: Add bitwise tracking for BPF_END
Date: Mon, 02 Feb 2026 12:03:20 -0800	[thread overview]
Message-ID: <e62c874af12c74ec4442df6e098c6d5000f9540e.camel@gmail.com> (raw)
In-Reply-To: <20260202133536.66207-2-ziye@zju.edu.cn>

On Mon, 2026-02-02 at 21:35 +0800, Tianci Cao wrote:
> This patch implements bitwise tracking (tnum analysis) for BPF_END
> (byte swap) operation.
> 
> Currently, the BPF verifier does not track value for BPF_END operation,
> treating the result as completely unknown. This limits the verifier's
> ability to prove safety of programs that perform endianness conversions,
> which are common in networking code.
> 
> For example, the following code pattern for port number validation:
> 
> int test(struct pt_regs *ctx) {
>     __u64 x = bpf_get_prandom_u32();
>     x &= 0x3f00;           // Range: [0, 0x3f00], var_off: (0x0; 0x3f00)
>     x = bswap16(x);        // Should swap to range [0, 0x3f], var_off: (0x0; 0x3f)
>     if (x > 0x3f) goto trap;
>     return 0;
> trap:
>     return *(u64 *)NULL;   // Should be unreachable
> }
> 
> Currently generates verifier output:
> 
> 1: (54) w0 &= 16128                   ; R0=scalar(smin=smin32=0,smax=umax=smax32=umax32=16128,var_off=(0x0; 0x3f00))
> 2: (d7) r0 = bswap16 r0               ; R0=scalar()
> 3: (25) if r0 > 0x3f goto pc+2        ; R0=scalar(smin=smin32=0,smax=umax=smax32=umax32=63,var_off=(0x0; 0x3f))
> 
> Without this patch, even though the verifier knows `x` has certain bits
> set, after bswap16, it loses all tracking information and treats port
> as having a completely unknown value [0, 65535].
> 
> According to the BPF instruction set[1], there are 3 kinds of BPF_END:
> 
> 1. `bswap(16|32|64)`: opcode=0xd7 (BPF_END | BPF_ALU64 | BPF_TO_LE)
>    - do unconditional swap
> 2. `le(16|32|64)`: opcode=0xd4 (BPF_END | BPF_ALU | BPF_TO_LE)
>    - on big-endian: do swap
>    - on little-endian: truncation (16/32-bit) or no-op (64-bit)
> 3. `be(16|32|64)`: opcode=0xdc (BPF_END | BPF_ALU | BPF_TO_BE)
>    - on little-endian: do swap
>    - on big-endian: truncation (16/32-bit) or no-op (64-bit)
> 
> Since BPF_END operations are inherently bit-wise permutations, tnum
> (bitwise tracking) offers the most efficient and precise mechanism
> for value analysis. By implementing `tnum_bswap16`, `tnum_bswap32`,
> and `tnum_bswap64`, we can derive exact `var_off` values concisely,
> directly reflecting the bit-level changes.
> 
> Here is the overview of changes:
> 
> 1. In `tnum_bswap(16|32|64)` (kernel/bpf/tnum.c):
> 
> Call `swab(16|32|64)` function on the value and mask of `var_off`, and
> do truncation for 16/32-bit cases.
> 
> 2. In `adjust_scalar_min_max_vals` (kernel/bpf/verifier.c):
> 
> Call helper function `scalar_byte_swap`.
> - Only do byte swap when
>   * alu64 (unconditional swap) OR
>   * switching between big-endian and little-endian machines.
> - If need do byte swap:
>   * Firstly call `tnum_bswap(16|32|64)` to update `var_off`.
>   * Then reset the bound since byte swap scrambles the range.
> - For 16/32-bit cases, truncate dst register to match the swapped size.
> 
> This enables better verification of networking code that frequently uses
> byte swaps for protocol processing, reducing false positive rejections.
> 
> [1] https://www.kernel.org/doc/Documentation/bpf/standardization/instruction-set.rst
> 
> Co-developed-by: Shenghao Yuan <shenghaoyuan0928@163.com>
> Signed-off-by: Shenghao Yuan <shenghaoyuan0928@163.com>
> Co-developed-by: Yazhou Tang <tangyazhou518@outlook.com>
> Signed-off-by: Yazhou Tang <tangyazhou518@outlook.com>
> Signed-off-by: Tianci Cao <ziye@zju.edu.cn>
> ---

Hi Tianci, the code looks correct, a single nit below.

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

[...]

> @@ -15986,12 +16029,19 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,
>  		else
>  			scalar_min_max_arsh(dst_reg, &src_reg);
>  		break;
> +	case BPF_END:
> +		scalar_byte_swap(dst_reg, insn);
> +		break;
>  	default:
>  		break;
>  	}
>  
> -	/* ALU32 ops are zero extended into 64bit register */
> -	if (alu32)
> +	/*
> +	 * ALU32 ops are zero extended into 64bit register.
> +	 * BPF_END is already handled inside the helper (truncation),
> +	 * so skip zext here.
> +	 */
> +	if (alu32 && opcode != BPF_END)

Nit: zext after scalar_byte_swap() won't change anything, right?
     If so, I'd just avoid the special case and skip 'opcode != BPF_END'.

>  		zext_32_to_64(dst_reg);
>  	reg_bounds_sync(dst_reg);
>  	return 0;

[...]

  reply	other threads:[~2026-02-02 20:03 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-02 13:35 [PATCH bpf-next 0/2] bpf: Add bitwise tracking for BPF_END Tianci Cao
2026-02-02 13:35 ` [PATCH bpf-next 1/2] " Tianci Cao
2026-02-02 20:03   ` Eduard Zingerman [this message]
2026-02-03 10:17     ` Tianci Cao
2026-02-03 22:36       ` Eduard Zingerman
2026-02-02 13:35 ` [PATCH bpf-next 2/2] selftests/bpf: Add tests for BPF_END bitwise tracking Tianci Cao
2026-02-02 20:23   ` Eduard Zingerman
2026-02-03 10:20     ` Tianci Cao
2026-02-03 23:16       ` Eduard Zingerman
2026-02-04  6:08         ` Tianci Cao

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=e62c874af12c74ec4442df6e098c6d5000f9540e.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@fomichev.me \
    --cc=shenghaoyuan0928@163.com \
    --cc=song@kernel.org \
    --cc=tangyazhou518@outlook.com \
    --cc=yonghong.song@linux.dev \
    --cc=ziye@zju.edu.cn \
    /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