* [PATCH bpf v4 1/4] bpf: simplify sanitize_err() signature
2026-07-29 15:18 [PATCH bpf v4 0/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
@ 2026-07-29 15:18 ` Yiyang Chen via B4 Relay
2026-07-30 8:22 ` Shung-Hsi Yu
2026-07-29 15:18 ` [PATCH bpf v4 2/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
` (2 subsequent siblings)
3 siblings, 1 reply; 7+ messages in thread
From: Yiyang Chen via B4 Relay @ 2026-07-29 15:18 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Song Liu, Yonghong Song,
Jiri Olsa, Shuah Khan, Emil Tsalapatis, Ihor Solodrai,
Shung-Hsi Yu, Daniel Wade, bpf, linux-kselftest, linux-kernel,
Yiyang Chen
From: Eduard Zingerman <eddyz87@gmail.com>
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.
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
---
kernel/bpf/verifier.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 7aa47342dc659..9792d6622ffd6 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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.
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH bpf v4 1/4] bpf: simplify sanitize_err() signature
2026-07-29 15:18 ` [PATCH bpf v4 1/4] bpf: simplify sanitize_err() signature Yiyang Chen via B4 Relay
@ 2026-07-30 8:22 ` Shung-Hsi Yu
0 siblings, 0 replies; 7+ messages in thread
From: Shung-Hsi Yu @ 2026-07-30 8:22 UTC (permalink / raw)
To: chenyy23
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, John Fastabend,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Shuah Khan,
Emil Tsalapatis, Ihor Solodrai, Daniel Wade, bpf, linux-kselftest,
linux-kernel
On Wed, Jul 29, 2026 at 03:18:27PM +0000, Yiyang Chen via B4 Relay wrote:
> From: Eduard Zingerman <eddyz87@gmail.com>
>
> 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.
>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
> Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf v4 2/4] bpf: Preserve pointer state for commuted arithmetic
2026-07-29 15:18 [PATCH bpf v4 0/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
2026-07-29 15:18 ` [PATCH bpf v4 1/4] bpf: simplify sanitize_err() signature Yiyang Chen via B4 Relay
@ 2026-07-29 15:18 ` Yiyang Chen via B4 Relay
2026-07-30 8:25 ` Shung-Hsi Yu
2026-07-29 15:18 ` [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in " Yiyang Chen via B4 Relay
2026-07-29 15:18 ` [PATCH bpf v4 4/4] selftests/bpf: Cover commuted pointer state propagation Yiyang Chen via B4 Relay
3 siblings, 1 reply; 7+ messages in thread
From: Yiyang Chen via B4 Relay @ 2026-07-29 15:18 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Song Liu, Yonghong Song,
Jiri Olsa, Shuah Khan, Emil Tsalapatis, Ihor Solodrai,
Shung-Hsi Yu, Daniel Wade, bpf, linux-kselftest, linux-kernel,
Yiyang Chen
From: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
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 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.
Fixes: f4d7e40a5b71 ("bpf: introduce function calls (verification)")
Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
Tested-by: Daniel Wade <danjwade95@gmail.com>
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
---
kernel/bpf/verifier.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index 9792d6622ffd6..cdb61fab84351 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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 */
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* Re: [PATCH bpf v4 2/4] bpf: Preserve pointer state for commuted arithmetic
2026-07-29 15:18 ` [PATCH bpf v4 2/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
@ 2026-07-30 8:25 ` Shung-Hsi Yu
0 siblings, 0 replies; 7+ messages in thread
From: Shung-Hsi Yu @ 2026-07-30 8:25 UTC (permalink / raw)
To: chenyy23
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi, John Fastabend,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa, Shuah Khan,
Emil Tsalapatis, Ihor Solodrai, Daniel Wade, bpf, linux-kselftest,
linux-kernel
On Wed, Jul 29, 2026 at 03:18:28PM +0000, Yiyang Chen via B4 Relay wrote:
> From: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
>
> 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 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.
>
> Fixes: f4d7e40a5b71 ("bpf: introduce function calls (verification)")
> Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
> Tested-by: Daniel Wade <danjwade95@gmail.com>
> Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>
Acked-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in commuted arithmetic
2026-07-29 15:18 [PATCH bpf v4 0/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
2026-07-29 15:18 ` [PATCH bpf v4 1/4] bpf: simplify sanitize_err() signature Yiyang Chen via B4 Relay
2026-07-29 15:18 ` [PATCH bpf v4 2/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
@ 2026-07-29 15:18 ` Yiyang Chen via B4 Relay
2026-07-29 15:18 ` [PATCH bpf v4 4/4] selftests/bpf: Cover commuted pointer state propagation Yiyang Chen via B4 Relay
3 siblings, 0 replies; 7+ messages in thread
From: Yiyang Chen via B4 Relay @ 2026-07-29 15:18 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Song Liu, Yonghong Song,
Jiri Olsa, Shuah Khan, Emil Tsalapatis, Ihor Solodrai,
Shung-Hsi Yu, Daniel Wade, bpf, linux-kselftest, linux-kernel,
Yiyang Chen
From: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
The 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.
Fixes: f2362a57aeff ("bpf: allow void* cast using bpf_rdonly_cast()")
Tested-by: Daniel Wade <danjwade95@gmail.com>
Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
---
kernel/bpf/verifier.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index cdb61fab84351..fdc5fbb1f78ca 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -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;
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread* [PATCH bpf v4 4/4] selftests/bpf: Cover commuted pointer state propagation
2026-07-29 15:18 [PATCH bpf v4 0/4] bpf: Preserve pointer state for commuted arithmetic Yiyang Chen via B4 Relay
` (2 preceding siblings ...)
2026-07-29 15:18 ` [PATCH bpf v4 3/4] bpf: Propagate untrusted pointer state in " Yiyang Chen via B4 Relay
@ 2026-07-29 15:18 ` Yiyang Chen via B4 Relay
3 siblings, 0 replies; 7+ messages in thread
From: Yiyang Chen via B4 Relay @ 2026-07-29 15:18 UTC (permalink / raw)
To: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Eduard Zingerman, Kumar Kartikeya Dwivedi
Cc: John Fastabend, Martin KaFai Lau, Song Liu, Yonghong Song,
Jiri Olsa, Shuah Khan, Emil Tsalapatis, Ihor Solodrai,
Shung-Hsi Yu, Daniel Wade, bpf, linux-kselftest, linux-kernel,
Yiyang Chen
From: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
Add 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.
Tested-by: Daniel Wade <danjwade95@gmail.com>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Signed-off-by: Yiyang Chen <chenyy23@mails.tsinghua.edu.cn>
---
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(+)
diff --git a/tools/testing/selftests/bpf/progs/dynptr_fail.c b/tools/testing/selftests/bpf/progs/dynptr_fail.c
index 344fb2aa0813d..29c6361d8820a 100644
--- a/tools/testing/selftests/bpf/progs/dynptr_fail.c
+++ b/tools/testing/selftests/bpf/progs/dynptr_fail.c
@@ -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;
+
+ asm volatile ("%[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.
*/
diff --git a/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c b/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
index 5b4453747c230..f166fff8f2176 100644
--- a/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
+++ b/tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c
@@ -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);
+ asm volatile ("%[dst] = 0;"
+ "%[dst] += %[src];"
+ : [dst]"=&r"(derived)
+ : [src]"r"(p)
+ : "memory");
+ return *derived;
+}
+
char _license[] SEC("license") = "GPL";
diff --git a/tools/testing/selftests/bpf/progs/verifier_basic_stack.c b/tools/testing/selftests/bpf/progs/verifier_basic_stack.c
index fb62e09f21146..d3df7a9f1d8c8 100644
--- a/tools/testing/selftests/bpf/progs/verifier_basic_stack.c
+++ b/tools/testing/selftests/bpf/progs/verifier_basic_stack.c
@@ -97,4 +97,45 @@ __naked void misaligned_read_from_stack(void)
" ::: __clobber_all);
}
+SEC("socket")
+__description("stack pointer arithmetic preserves frame number")
+__failure __msg("R7 invalid mem access 'scalar'")
+__naked void stack_ptr_arith_preserves_frameno(void)
+{
+ asm volatile ("\
+ r3 = 0; \
+ *(u64 *)(r10 - 8) = r3; \
+ r1 = %[map_hash_8b] ll; \
+ r2 = r10; \
+ r2 += -8; \
+ call %[bpf_map_lookup_elem]; \
+ if r0 != 0 goto +2; \
+ r0 = 0; \
+ exit; \
+ r1 = r0; \
+ r2 = 0; \
+ r3 = 0; \
+ call stack_ptr_arith_preserves_frameno_subprog;\
+ r0 = 0; \
+ exit; \
+ ":
+ : __imm(bpf_map_lookup_elem),
+ __imm_addr(map_hash_8b)
+ : __clobber_all);
+}
+
+static __used __naked void stack_ptr_arith_preserves_frameno_subprog(void)
+{
+ asm volatile ("\
+ *(u64 *)(r10 - 8) = r1; \
+ r6 = -8; \
+ r6 += r10; \
+ *(u64 *)(r6 + 0) = r2; \
+ r7 = *(u64 *)(r10 - 8); \
+ *(u64 *)(r7 + 0) = r3; \
+ r0 = 0; \
+ exit; \
+ "::: __clobber_all);
+}
+
char _license[] SEC("license") = "GPL";
--
2.43.0
^ permalink raw reply related [flat|nested] 7+ messages in thread