Non const src shift#12382
Open
ziyefbk wants to merge 3 commits into
Open
Conversation
970af1b to
cf55c66
Compare
1389c2d to
7da11c7
Compare
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.
3a26044 to
818f7b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.