bpf: Preserve pointer state for commuted arithmetic
Subsystems (4)
v4 cover bpf: Preserve pointer state for commuted arithmetic
This series fixes pointer-state propagation for commuted scalar += pointer arithmetic in the verifier.
Patch 1 simplifies sanitize_err() before the pointer-state change. Patch 2 keeps the full pointer register state when the pointer operand is the source of the add, preserving fields such as the stack frame number and parent id. Patch 3 moves the untrusted PTR_TO_MEM early return after state propagation, so scalar += untrusted_pointer remains usable through the probe-read path. Patch 4 adds verifier selftests for stack frame number preservation, readonly-untrusted memory access, and dynptr data-slice invalidation.
Changes in v4:
- Target the bpf fixes tree because the affected behavior is present in released kernels.
- Add Eduard's sanitize_err() cleanup and revised pointer-state patch, reusing the caller's temporary offset register instead of verifier-env scratch storage.
- Correct the stack-frame Fixes tag to the BPF-to-BPF call verification commit identified by Shung-Hsi.
- Fix the dynptr test comment style and retain the source-register clear so the test isolates parent-id propagation.
- Carry Daniel Wade's Tested-by and Eduard's selftest Acked-by from the v3 thread.
- Rebase to bpf base 0ce37745d4bf.
Changes in v3:
- Preserve the complete pointer register state with verifier-env scratch storage, addressing Eduard's comment that copying selected fields is fragile and avoiding a temporary bpf_reg_state on the verifier stack.
- Keep the existing RUN(verifier_basic_stack) dispatch unchanged and add the stack regression directly to the existing verifier_basic_stack program.
- Keep the original operand direction inside adjust_ptr_min_max_vals() by saving the scalar operand in env->fake_reg[0].
- Move untrusted PTR_TO_MEM handling after the unified pointer-state copy so the commuted form remains PTR_TO_MEM before the early return.
- Add readonly-untrusted and dynptr selftest coverage, responding to the bpf-ci/static review finding that the untrusted pointer case needs a regression test.
- Clear the original dynptr data-slice register after deriving the commuted alias so the regression test isolates parent-id propagation.
- Make the readonly-untrusted return value endian-neutral by loading an int.
- Rebase to bpf-next base a23a71823352.
v3: https://lore.kernel.org/bpf/[email protected]/
v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/
---
Eduard Zingerman (1):
bpf: simplify sanitize_err() signature
Yiyang Chen (3):
bpf: Preserve pointer state for commuted arithmetic
bpf: Propagate untrusted pointer state in commuted arithmetic selftests/bpf: Cover commuted pointer state propagation
kernel/bpf/verifier.c | 45 +++++++++++----------- tools/testing/selftests/bpf/progs/dynptr_fail.c | 30 +++++++++++++++ .../selftests/bpf/progs/mem_rdonly_untrusted.c | 17 ++++++++ .../selftests/bpf/progs/verifier_basic_stack.c | 41 ++++++++++++++++++++ 4 files changed, 110 insertions(+), 23 deletions(-)
---
base-commit: 0ce37745d4bfbc493f718169c3974898ffec8ee7
change-id: 20260729-c3-035-public-bpf-v4-0b28a4e0e1ef
Best regards,
Signature · 1 line
Yiyang Chen <[email protected]>
PATCH v4 1/4 bpf: simplify sanitize_err() signature
From: Eduard Zingerman <[email protected]>
The sanitize_err() function is called when:
- ptr += scalar
- scalar += ptr
- scalar += scalar
ALU operations are processed.
This commit drops offset and pointer registers parameters from its signature to simplify the follow-up changes for 'scalar += ptr' case. regs[src].type is safe to access, as it is not mutated by the callers.
--- kernel/bpf/verifier.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +7 -9
@@ -13557,23 +13557,21 @@ static void sanitize_mark_insn_seen(struct bpf_verifier_env *env) env->insn_aux_data[env->insn_idx].seen = env->pass_cnt; } -static int sanitize_err (struct bpf_verifier_env *env, -const struct bpf_insn *insn,int reason, -const struct bpf_reg_state *off_reg, -const struct bpf_reg_state *dst_reg) +static int sanitize_err (struct bpf_verifier_env *env,const struct bpf_insn *insn,int reason) {static const char *err ="pointer arithmetic with it prohibited for !root" ;const char *op =BPF_OP (insn->code) ==BPF_ADD ?"add" :"sub" ;u32 dst = insn->dst_reg, src = insn->src_reg; +struct bpf_reg_state *regs =cur_regs (env);switch (reason) {case REASON_BOUNDS :verbose (env,"R%d has unknown scalar with mixed signed bounds, %s\n" , - off_reg == dst_reg ? dst : src, err); + regs[src].type ==SCALAR_VALUE ? src : dst, err);break ;case REASON_TYPE :verbose (env,"R%d has pointer with unsupported alu operation, %s\n" , - off_reg == dst_reg ? src : dst, err); + regs[src].type ==SCALAR_VALUE ? dst : src, err);break ;case REASON_PATHS : verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n",
@@ -13762,7 +13760,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, ret =sanitize_ptr_alu (env, insn, ptr_reg, off_reg, dst_reg, &info, false);if (ret <0 ) -return sanitize_err (env, insn, ret, off_reg, dst_reg); +return sanitize_err (env, insn, ret); }switch (opcode) {
@@ -13855,7 +13853,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EFAULT ; }if (ret <0 ) -return sanitize_err (env, insn, ret, off_reg, dst_reg); +return sanitize_err (env, insn, ret); }return 0 ;
@@ -14607,7 +14605,7 @@ static int adjust_scalar_min_max_vals(struct bpf_verifier_env *env,if (sanitize_needed (opcode)) { ret =sanitize_val_alu (env, insn);if (ret <0 ) -return sanitize_err (env, insn, ret,NULL ,NULL ); +return sanitize_err (env, insn, ret); }/* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops.
Signature · 1 line
2.43.0
Re: [PATCH bpf v4 1/4] bpf: simplify sanitize_err() signature
Quoted 8 lines · On Wed, Jul 29, 2026 at 03:18:27PM +0000, Yiyang Chen via B4 Relay wrote:
From: Eduard Zingerman <[email protected]>
The
sanitize_err()function is called when:
- ptr += scalar
- scalar += ptr
- scalar += scalar
ALU operations are processed.
Quoted 6 lines
This commit drops offset and pointer registers parameters from its signature to simplify the follow-up changes for 'scalar += ptr' case. regs[src].type is safe to access, as it is not mutated by the callers.
Signed-off-by Eduard Zingerman <[email protected]>Signed-off-by Yiyang Chen <[email protected]>
PATCH v4 2/4 bpf: Preserve pointer state for commuted arithmetic
In reply to Yiyang ChenWhen scalar += pointer is handled in adjust_ptr_min_max_vals(), the destination register inherits the pointer state from the source pointer. Copying only selected fields is fragile because pointer provenance is tracked by several bpf_reg_state fields.
Use the caller's temporary offset register to preserve the scalar operand while replacing the destination with the full pointer state. This preserves the frame number for PTR_TO_STACK registers and keeps parent identity fields consistent.
--- kernel/bpf/verifier.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +8 -7
@@ -13743,11 +13743,12 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; } -/* In case of 'scalar += pointer', dst_reg inherits pointer type and id. -* The id may be overwritten later if we create a new variable offset. +/* For 'scalar += pointer', dst_reg inherits the complete pointer +* register state. Individual fields may be adjusted later by pointer +* arithmetic. Callers guarantee that below does not overwrite off_reg. */ - dst_reg->type = ptr_reg->type; - dst_reg->id = ptr_reg->id; +if (dst_reg != ptr_reg) + *dst_reg = *ptr_reg;if (!check_reg_sane_offset_scalar (env, off_reg, ptr_reg->type) || !check_reg_sane_offset_ptr (env, ptr_reg, ptr_reg->type))
@@ -13790,7 +13791,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, }break ;case BPF_SUB : -if (dst_reg == off_reg) { +if (dst_reg != ptr_reg) {/* scalar -= pointer. Creates an unknown scalar */ verbose (env,"R%d tried to subtract pointer from scalar\n" , dst);
@@ -14808,8 +14809,8 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env, err =mark_chain_precision (env, insn->dst_reg);if (err)return err; -return adjust_ptr_min_max_vals (env, insn, - src_reg, dst_reg); + off_reg = *dst_reg; +return adjust_ptr_min_max_vals (env, insn, src_reg, &off_reg); } }else if (ptr_reg) {/* pointer += scalar */
Signature · 1 line
2.43.0
Quoted 12 lines · On Wed, Jul 29, 2026 at 03:18:28PM +0000, Yiyang Chen via B4 Relay wrote:
From: Yiyang Chen <[email protected]>
When scalar += pointer is handled in
adjust_ptr_min_max_vals(), the destination register inherits the pointer state from the source pointer. Copying only selected fields is fragile because pointer provenance is tracked by severalbpf_reg_statefields.Use the caller's temporary offset register to preserve the scalar operand while replacing the destination with the full pointer state. This preserves the frame number for
PTR_TO_STACKregisters and keeps parent identity fields consistent.
Quoted 4 lines
Fixes f4d7e40a5b71 ("bpf: introduce function calls (verification)")Signed-off-by Yiyang Chen <[email protected]>Tested-by Daniel Wade <[email protected]>Signed-off-by Eduard Zingerman <[email protected]>
PATCH v4 3/4 bpf: Propagate untrusted pointer state in commuted arithmetic
In reply to Yiyang ChenThe untrusted PTR_TO_MEM early return skips pointer offset tracking because accesses go through probe-read handling. Moving it after full pointer-state propagation ensures scalar += untrusted_pointer leaves the destination as PTR_TO_MEM instead of an unrelated scalar.
--- kernel/bpf/verifier.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +7 -7
@@ -13707,13 +13707,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; } -/* -* Accesses to untrusted PTR_TO_MEM are done through probe -* instructions, hence no need to track offsets. -*/ -if (base_type (ptr_reg->type) ==PTR_TO_MEM && (ptr_reg->type &PTR_UNTRUSTED )) -return 0 ; -switch (base_type (ptr_reg->type)) {case PTR_TO_CTX :case PTR_TO_MAP_VALUE :
@@ -13750,6 +13743,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,if (dst_reg != ptr_reg) *dst_reg = *ptr_reg; +/* +* Accesses to untrusted PTR_TO_MEM are done through probe +* instructions, hence no need to track offsets. +*/ +if (base_type (ptr_reg->type) ==PTR_TO_MEM && (ptr_reg->type &PTR_UNTRUSTED )) +return 0 ; +if (!check_reg_sane_offset_scalar (env, off_reg, ptr_reg->type) || !check_reg_sane_offset_ptr (env, ptr_reg, ptr_reg->type))return -EINVAL ;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in commuted arithmetic
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] sanitize_err prints the wrong register name for immediate operations (BPF_K) when R0 contains a…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] sanitize_err prints the wrong register name for immediate operations (BPF_K) when R0 contains a pointer. --
--- Patch [3]: [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in commuted arithmetic --- commit 74ae77e327bede792130d0bf76a485d3ad9ef6b4
Author: Yiyang Chen <[email protected]>
bpf: Propagate untrusted pointer state in commuted arithmetic
This patch modifies the BPF verifier's pointer arithmetic logic by moving the early return for untrusted PTR_TO_MEM accesses. Placing it after the pointer state is fully propagated ensures that operations like scalar += untrusted_pointer correctly leave the destination register as a PTR_TO_MEM.
Quoted 18 lines
kernel/bpf/verifier.c +0
-7@@ -13707,13 +13707,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; } -/* -* Accesses to untrusted PTR_TO_MEM are done through probe -* instructions, hence no need to track offsets. -*/ -if (base_type (ptr_reg->type) ==PTR_TO_MEM && (ptr_reg->type &PTR_UNTRUSTED )) -return 0 ; -switch (base_type (ptr_reg->type)) {case PTR_TO_CTX :case PTR_TO_MAP_VALUE :
[ ... ]
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -13750,6 +13743,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,if (dst_reg != ptr_reg) *dst_reg = *ptr_reg;
kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · line 13755
Quoted 10 lines
kernel/bpf/verifier.c +7
-0+/* +* Accesses to untrusted PTR_TO_MEM are done through probe +* instructions, hence no need to track offsets. +*/ +if (base_type (ptr_reg->type) ==PTR_TO_MEM && (ptr_reg->type &PTR_UNTRUSTED )) +return 0 ; +if (!check_reg_sane_offset_scalar (env, off_reg, ptr_reg->type) || !check_reg_sane_offset_ptr (env, ptr_reg, ptr_reg->type))return -EINVAL ;
[Severity: Medium]
Does sanitize_err() print the wrong register name for immediate operations (BPF_K) when R0 contains a pointer?
When an immediate pointer arithmetic operation is rejected, sanitize_err() is called downstream of this offset checking logic:
kernel/bpf/verifier.c:sanitize_err() {
...
u32 dst = insn->dst_reg, src = insn->src_reg;
...
case REASON_TYPE:
verbose(env, "R%d has pointer with unsupported alu operation, %s\n",
regs[src].type == SCALAR_VALUE ? dst : src, err);
}For BPF_K operations, insn->src_reg is 0. If a pointer was previously placed in R0, or if R0 is uninitialized, regs[0].type will not be SCALAR_VALUE. The ternary operation will then incorrectly choose src (R0) instead of the destination register, emitting a confusing diagnostic message.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/20260729-c3-035-public-bpf-v4-v4-0-8ee297e2346b@mails.tsinghua.edu.cn?part=3
Eduard Zingerman
maintainerRe: [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in commuted arithmetic
Quoted 67 lines · On Wed, 2026-07-29 at 15:35 +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium]sanitize_errprints the wrong register name for immediate operations (BPF_K) when R0 contains a pointer. ----- Patch [3]: [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in commuted arithmetic --- commit
74ae77e327bede792130d0bf76a485d3ad9ef6b4
Author: Yiyang Chen <[email protected]>bpf: Propagate untrusted pointer state in commuted arithmetic
This patch modifies the BPF verifier's pointer arithmetic logic by moving the early return for untrusted
PTR_TO_MEMaccesses. Placing it after the pointer state is fully propagated ensures that operations like scalar +=untrusted_pointercorrectly leave the destination register as aPTR_TO_MEM.Quoted 18 lines
kernel/bpf/verifier.c +0
-7@@ -13707,13 +13707,6 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,return -EACCES ; } -/* -* Accesses to untrusted PTR_TO_MEM are done through probe -* instructions, hence no need to track offsets. -*/ -if (base_type (ptr_reg->type) ==PTR_TO_MEM && (ptr_reg->type &PTR_UNTRUSTED )) -return 0 ; -switch (base_type (ptr_reg->type)) {case PTR_TO_CTX :case PTR_TO_MAP_VALUE :[ ... ]
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -13750,6 +13743,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,if (dst_reg != ptr_reg) *dst_reg = *ptr_reg;kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · line 13755
Quoted 10 lines
kernel/bpf/verifier.c +7
-0+/* +* Accesses to untrusted PTR_TO_MEM are done through probe +* instructions, hence no need to track offsets. +*/ +if (base_type (ptr_reg->type) ==PTR_TO_MEM && (ptr_reg->type &PTR_UNTRUSTED )) +return 0 ; +if (!check_reg_sane_offset_scalar (env, off_reg, ptr_reg->type) || !check_reg_sane_offset_ptr (env, ptr_reg, ptr_reg->type))return -EINVAL ;[Severity: Medium]
Doessanitize_err()print the wrong register name for immediate operations (BPF_K) when R0 contains a pointer?When an immediate pointer arithmetic operation is rejected,
sanitize_err()is called downstream of this offset checking logic:kernel/bpf/verifier.c:sanitize_err() { ... u32 dst = insn->dst_reg, src = insn->src_reg; ... case REASON_TYPE: verbose(env, "R%d has pointer with unsupported alu operation, %s\n", regs[src].type == SCALAR_VALUE ? dst : src, err); }
Quoted 4 lines
For
BPF_Koperations,insn->src_regis 0. If a pointer was previously placed in R0, or if R0 is uninitialized, regs[0].type will not beSCALAR_VALUE. The ternary operation will then incorrectly choose src (R0) instead of the destination register, emitting a confusing diagnostic message.
This seem to be a valid point.
Yiyang, could you please special-case BPF_K in the patch-set #1? Let's do it as a follow-up.
PATCH v4 4/4 selftests/bpf: Cover commuted pointer state propagation
In reply to Yiyang ChenAdd verifier coverage for the three cases affected by preserving the full pointer state across scalar += pointer: stack frame number inheritance, readonly-untrusted memory access, and dynptr data-slice invalidation.
--- tools/testing/selftests/bpf/progs/dynptr_fail.c | 30 ++++++++++++++++ .../selftests/bpf/progs/mem_rdonly_untrusted.c | 17 +++++++++ .../selftests/bpf/progs/verifier_basic_stack.c | 41 ++++++++++++++++++++++ 3 files changed, 88 insertions(+)
tools/testing/selftests/bpf/progs/dynptr_fail.c +30 -0
@@ -1635,6 +1635,36 @@ static int callback(__u32 index, void *data)return 0 ; } +/* A commuted add should preserve the parent id of a dynptr data slice. */ +SEC("?raw_tp") +__failure __msg("invalid mem access 'scalar'") +int dynptr_slice_commuted_invalidate(void *ctx) +{ +struct bpf_dynptr ptr; +__u32 *slice, *derived; + +bpf_ringbuf_reserve_dynptr (&ringbuf,sizeof (__u32),0 , &ptr); + + slice =bpf_dynptr_data (&ptr,0 ,sizeof (__u32)); +if (!slice) + goto done; + + asmvolatile ("%[dst] = 0;" +"%[dst] += %[src];" +"%[src] = 0;" + : [dst]"=&r" (derived), [src]"+r" (slice) + : + :"memory" ); + +bpf_ringbuf_discard_dynptr (&ptr,0 ); + val = *derived; +return 0 ; + +done: +bpf_ringbuf_discard_dynptr (&ptr,0 ); +return 0 ; +} +/* If the dynptr is written into in a callback function, its data * slices should be invalidated as well. */
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +17 -0
@@ -226,4 +226,21 @@ int null_check(void *ctx)return 0 ; } +SEC("socket") +__success +__retval(1) +int ldx_is_ok_commuted_addr(void *ctx) +{ +int v, *p, *derived; + + v =1 ; + p =bpf_rdonly_cast (&v,0 ); + asmvolatile ("%[dst] = 0;" +"%[dst] += %[src];" + : [dst]"=&r" (derived) + : [src]"r" (p) + :"memory" ); +return *derived; +} + char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/verifier_basic_stack.c +41 -0
45 lines not on this page · raw message
Signature · 1 line
2.43.0
This series was applied to bpf/bpf.git (master) by Eduard Zingerman <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Eduard Zingerman <[email protected]>:
Quoted 11 lines · On Wed, 29 Jul 2026 15:18:26 +0000 you wrote:
This series fixes pointer-state propagation for commuted scalar += pointer arithmetic in the verifier.
Patch 1 simplifies
sanitize_err()before the pointer-state change. Patch 2 keeps the full pointer register state when the pointer operand is the source of the add, preserving fields such as the stack frame number and parent id. Patch 3 moves the untrustedPTR_TO_MEMearly return after state propagation, so scalar +=untrusted_pointerremains usable through the probe-read path. Patch 4 adds verifier selftests for stack frame number preservation, readonly-untrusted memory access, and dynptr data-slice invalidation.
Quoted 1 line
[...]
Here is the summary with links:
- [bpf,v4,1/4] bpf: simplify sanitize_err() signature
https://git.kernel.org/bpf/bpf/c/a15970d916b3 - [bpf,v4,2/4] bpf: Preserve pointer state for commuted arithmetic https://git.kernel.org/bpf/bpf/c/a4c6f804b44c - [bpf,v4,3/4] bpf: Propagate untrusted pointer state in commuted arithmetic https://git.kernel.org/bpf/bpf/c/cdf19b1b3c01 - [bpf,v4,4/4] selftests/bpf: Cover commuted pointer state propagation https://git.kernel.org/bpf/bpf/c/21596761ff37
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html