mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-29 16:18:09 -04:00
This commit consolidates static and varying pointer offset tracking logic. All offsets are now represented solely using `.var_off` and min/max fields. The reasons are twofold: - This simplifies pointer tracking code, as each relevant function needs to check the `.var_off` field anyway. - It makes it easier to widen pointer registers for the purpose of loop convergence checks, by forgoing the `regsafe()` logic demanding `.off` fields to be identical. The changes are spread across many functions and are hard to group into smaller patches. Some of the logical changes include: - Checks in __check_ptr_off_reg() are reordered so that the tnum_is_const() check is done before operating on reg->var_off.value. - check_packet_access() now uses check_mem_region_access() to handle possible 'off' overflow cases. - In check_helper_mem_access() utility functions like check_packet_access() are now called with 'off=0', as these utility functions now account for the complete register offset range. - In check_reg_type() a call to __check_ptr_off_reg() is added before a call to btf_struct_ids_match(). This prevents btf_struct_ids_match() from potentially working on non-constant reg->var_off.value. - regsafe() is relaxed to avoid comparing '.off' field for pointers. As a precaution, the changes are verified in [1] by adding a pass checking that no pointer has non-zero '.off' field on each do_check_insn() iteration. [1] https://github.com/eddyz87/bpf/tree/ptrs-off-migration Notable selftests changes: - `.var_off` value changed because it now combines static and varying offsets. Affected tests: - linked_list/incorrect_node_var_off - linked_list/incorrect_head_var_off2 - verifier_align/packet_variable_offset - Overflowing `smax_value` bound leads to a pointer with big negative or positive offset to be rejected immediately (previously overflowing `rX += const` instruction updated `.off` field avoiding the overflow). Affected tests: - verifier_align/dubious_pointer_arithmetic - verifier_bounds/var_off_insn_off_test1 - Invalid access to packet now reports full offset inside a packet. Affected tests: - verifier_direct_packet_access/test23_x_pkt_ptr_4 - A change in check_mem_region_access() behavior: when register `.smin_value` is negative, it reports "rX min value is negative..." before calling into __check_mem_access() which reports "invalid access to ...". In the tests below, the `.off` field was negative, while `.smin_value` remained positive. This is no longer the case after the changes in this commit. Affected tests: - verifier_gotox/jump_table_invalid_mem_acceess_neg - verifier_helper_packet_access/test15_cls_helper_fail_sub - verifier_helper_value_access/imm_out_of_bound_2 - verifier_helper_value_access/reg_out_of_bound_2 - verifier_meta_access/meta_access_test2 - verifier_value_ptr_arith/known_scalar_from_different_maps - lower_oob_arith_test_1 - value_ptr_known_scalar_3 - access_value_ptr_known_scalar - Usage of check_mem_region_access() instead of __check_mem_access() in check_packet_access() changes the reported message from "rX offset is outside ..." to "rX min/max value is outside ...". Affected tests: - verifier_xdp_direct_packet_access/* - In check_func_arg_reg_off() the check for zero offset now operates on `.var_off` field instead of `.off` field. For tests where the pattern looks like `kfunc(reg_with_var_off, ...)`, this changes the reported error: - previously the error "variable ... access ... disallowed" was reported by __check_ptr_off_reg(); - now "R1 must have zero offset ..." is reported by check_func_arg_reg_off() itself. Affected tests: - verifier/calls.c "calls: invalid kfunc call: PTR_TO_BTF_ID with variable offset" Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20260212-ptrs-off-migration-v2-2-00820e4d3438@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
156 lines
3.4 KiB
C
156 lines
3.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/* Converted from tools/testing/selftests/bpf/verifier/int_ptr.c */
|
|
|
|
#include <linux/bpf.h>
|
|
#include <bpf/bpf_helpers.h>
|
|
#include "bpf_misc.h"
|
|
|
|
SEC("socket")
|
|
__description("arg pointer to long uninitialized")
|
|
__success
|
|
__naked void arg_ptr_to_long_uninitialized(void)
|
|
{
|
|
asm volatile (" \
|
|
/* bpf_strtoul arg1 (buf) */ \
|
|
r7 = r10; \
|
|
r7 += -8; \
|
|
r0 = 0x00303036; \
|
|
*(u64*)(r7 + 0) = r0; \
|
|
r1 = r7; \
|
|
/* bpf_strtoul arg2 (buf_len) */ \
|
|
r2 = 4; \
|
|
/* bpf_strtoul arg3 (flags) */ \
|
|
r3 = 0; \
|
|
/* bpf_strtoul arg4 (res) */ \
|
|
r7 += -8; \
|
|
r4 = r7; \
|
|
/* bpf_strtoul() */ \
|
|
call %[bpf_strtoul]; \
|
|
r0 = 1; \
|
|
exit; \
|
|
" :
|
|
: __imm(bpf_strtoul)
|
|
: __clobber_all);
|
|
}
|
|
|
|
SEC("socket")
|
|
__description("arg pointer to long half-uninitialized")
|
|
__success
|
|
__retval(0)
|
|
__naked void ptr_to_long_half_uninitialized(void)
|
|
{
|
|
asm volatile (" \
|
|
/* bpf_strtoul arg1 (buf) */ \
|
|
r7 = r10; \
|
|
r7 += -8; \
|
|
r0 = 0x00303036; \
|
|
*(u64*)(r7 + 0) = r0; \
|
|
r1 = r7; \
|
|
/* bpf_strtoul arg2 (buf_len) */ \
|
|
r2 = 4; \
|
|
/* bpf_strtoul arg3 (flags) */ \
|
|
r3 = 0; \
|
|
/* bpf_strtoul arg4 (res) */ \
|
|
r7 += -8; \
|
|
*(u32*)(r7 + 0) = r0; \
|
|
r4 = r7; \
|
|
/* bpf_strtoul() */ \
|
|
call %[bpf_strtoul]; \
|
|
r0 = 0; \
|
|
exit; \
|
|
" :
|
|
: __imm(bpf_strtoul)
|
|
: __clobber_all);
|
|
}
|
|
|
|
SEC("cgroup/sysctl")
|
|
__description("arg pointer to long misaligned")
|
|
__failure __msg("misaligned stack access off -20+0 size 8")
|
|
__naked void arg_ptr_to_long_misaligned(void)
|
|
{
|
|
asm volatile (" \
|
|
/* bpf_strtoul arg1 (buf) */ \
|
|
r7 = r10; \
|
|
r7 += -8; \
|
|
r0 = 0x00303036; \
|
|
*(u64*)(r7 + 0) = r0; \
|
|
r1 = r7; \
|
|
/* bpf_strtoul arg2 (buf_len) */ \
|
|
r2 = 4; \
|
|
/* bpf_strtoul arg3 (flags) */ \
|
|
r3 = 0; \
|
|
/* bpf_strtoul arg4 (res) */ \
|
|
r7 += -12; \
|
|
r0 = 0; \
|
|
*(u32*)(r7 + 0) = r0; \
|
|
*(u64*)(r7 + 4) = r0; \
|
|
r4 = r7; \
|
|
/* bpf_strtoul() */ \
|
|
call %[bpf_strtoul]; \
|
|
r0 = 1; \
|
|
exit; \
|
|
" :
|
|
: __imm(bpf_strtoul)
|
|
: __clobber_all);
|
|
}
|
|
|
|
SEC("cgroup/sysctl")
|
|
__description("arg pointer to long size < sizeof(long)")
|
|
__failure __msg("invalid write to stack R4 off=-4 size=8")
|
|
__naked void to_long_size_sizeof_long(void)
|
|
{
|
|
asm volatile (" \
|
|
/* bpf_strtoul arg1 (buf) */ \
|
|
r7 = r10; \
|
|
r7 += -16; \
|
|
r0 = 0x00303036; \
|
|
*(u64*)(r7 + 0) = r0; \
|
|
r1 = r7; \
|
|
/* bpf_strtoul arg2 (buf_len) */ \
|
|
r2 = 4; \
|
|
/* bpf_strtoul arg3 (flags) */ \
|
|
r3 = 0; \
|
|
/* bpf_strtoul arg4 (res) */ \
|
|
r7 += 12; \
|
|
*(u32*)(r7 + 0) = r0; \
|
|
r4 = r7; \
|
|
/* bpf_strtoul() */ \
|
|
call %[bpf_strtoul]; \
|
|
r0 = 1; \
|
|
exit; \
|
|
" :
|
|
: __imm(bpf_strtoul)
|
|
: __clobber_all);
|
|
}
|
|
|
|
SEC("cgroup/sysctl")
|
|
__description("arg pointer to long initialized")
|
|
__success
|
|
__naked void arg_ptr_to_long_initialized(void)
|
|
{
|
|
asm volatile (" \
|
|
/* bpf_strtoul arg1 (buf) */ \
|
|
r7 = r10; \
|
|
r7 += -8; \
|
|
r0 = 0x00303036; \
|
|
*(u64*)(r7 + 0) = r0; \
|
|
r1 = r7; \
|
|
/* bpf_strtoul arg2 (buf_len) */ \
|
|
r2 = 4; \
|
|
/* bpf_strtoul arg3 (flags) */ \
|
|
r3 = 0; \
|
|
/* bpf_strtoul arg4 (res) */ \
|
|
r7 += -8; \
|
|
*(u64*)(r7 + 0) = r0; \
|
|
r4 = r7; \
|
|
/* bpf_strtoul() */ \
|
|
call %[bpf_strtoul]; \
|
|
r0 = 1; \
|
|
exit; \
|
|
" :
|
|
: __imm(bpf_strtoul)
|
|
: __clobber_all);
|
|
}
|
|
|
|
char _license[] SEC("license") = "GPL";
|