]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
bpf: use reg->var_off instead of reg->off for pointers
authorEduard Zingerman <eddyz87@gmail.com>
Thu, 12 Feb 2026 21:34:22 +0000 (13:34 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 13 Feb 2026 22:41:22 +0000 (14:41 -0800)
commit022ac075088366b62e130da5e1b200bc93a47191
treee871070d61744b4add161423de4b08ed854c6cfa
parented20a14309e09216d1fa86e12b1578fa822119b4
bpf: use reg->var_off instead of reg->off for pointers

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>
20 files changed:
include/linux/bpf_verifier.h
kernel/bpf/log.c
kernel/bpf/verifier.c
tools/testing/selftests/bpf/prog_tests/linked_list.c
tools/testing/selftests/bpf/progs/exceptions_assert.c
tools/testing/selftests/bpf/progs/iters.c
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
tools/testing/selftests/bpf/progs/verifier_align.c
tools/testing/selftests/bpf/progs/verifier_bounds.c
tools/testing/selftests/bpf/progs/verifier_direct_packet_access.c
tools/testing/selftests/bpf/progs/verifier_gotox.c
tools/testing/selftests/bpf/progs/verifier_helper_packet_access.c
tools/testing/selftests/bpf/progs/verifier_helper_value_access.c
tools/testing/selftests/bpf/progs/verifier_int_ptr.c
tools/testing/selftests/bpf/progs/verifier_meta_access.c
tools/testing/selftests/bpf/progs/verifier_spill_fill.c
tools/testing/selftests/bpf/progs/verifier_stack_ptr.c
tools/testing/selftests/bpf/progs/verifier_value_ptr_arith.c
tools/testing/selftests/bpf/progs/verifier_xdp_direct_packet_access.c
tools/testing/selftests/bpf/verifier/calls.c