Skip to content

Non const src shift#12382

Open
ziyefbk wants to merge 3 commits into
kernel-patches:bpf-next_basefrom
ziyefbk:non-const-src-shift
Open

Non const src shift#12382
ziyefbk wants to merge 3 commits into
kernel-patches:bpf-next_basefrom
ziyefbk:non-const-src-shift

Conversation

@ziyefbk

@ziyefbk ziyefbk commented Jun 7, 2026

Copy link
Copy Markdown

No description provided.

@kernel-patches-daemon-bpf kernel-patches-daemon-bpf Bot force-pushed the bpf-next_base branch 3 times, most recently from 970af1b to cf55c66 Compare June 8, 2026 01:59
@ziyefbk ziyefbk force-pushed the non-const-src-shift branch 5 times, most recently from 1389c2d to 7da11c7 Compare June 8, 2026 09:50
ziyefbk added 2 commits June 8, 2026 18:55
Currently, the BPF verifier only allows shift operations when the shift
amount is a known constant. This is overly restrictive for cases where
the shift amount is bounded but not fully determined at verification time.
For example, the following code is rejected by the verifier even though
the shift amount is bounded to [1, 4]:
    u32 shift = bpf_get_prandom_u32();
    shift &= 3;    // shift is in range [0, 3]
    shift += 1;    // shift is in range [1, 4]
    r1 <<= shift;  // non-const but bounded shift amount
Modify the shift helper functions (scalar_min_max_lsh, scalar32_min_max_lsh,
scalar_min_max_rsh, scalar32_min_max_rsh, scalar_min_max_arsh,
scalar32_min_max_arsh) to handle non-const but bounded shift amounts.

Updated is_safe_to_compute_dst_reg_range() to allow shift operations
when the shift amount is within the valid range (< 32 for 32-bit shifts,
< 64 for 64-bit shifts), rather than requiring it to be a constant.
This approach ensures the verifier remains sound while allowing more
programs to pass verification.

Also modify the comment on is_safe_to_compute_dst_reg_range.
Shifts by more than insn bitness are legal in the BPF ISA; they are
implementation-defined behaviour [of the underlying architecture],
rather than UB, and have been made legal for performance reasons.
Add test cases for shift operations with non-const but bounded source operand:
- shift_with_non_const_src_lsh: Tests left shift (BPF_LSH) where the shift
  amount is in range [1, 4] and the destination is a known constant (1). The
  verifier should compute correct bounds [2, 16] for the result.
- shift_with_non_const_src_rsh: Tests logical right shift (BPF_RSH) where the
  shift amount is in range [1, 4] and the destination is 0xff. The verifier
  should compute correct bounds [15, 127] for the result.
- shift_with_non_const_src_arsh: Tests arithmetic right shift (BPF_ARSH) where
  the shift amount is in range [1, 4] and the destination is a negative constant
  (-8). The verifier applies the three-branch signed bound logic to derive
  result bounds [-4, -1]. When the shift amount is non-constant, the var_off
  is conservatively set to tnum_unknown.
@kernel-patches-daemon-bpf kernel-patches-daemon-bpf Bot force-pushed the bpf-next_base branch 7 times, most recently from 3a26044 to 818f7b1 Compare June 10, 2026 04:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant