Skip to contentSkip to conversation

bpf: Preserve pointer state for commuted arithmetic

PATCH Superseded Yiyang Chen 5 participants 14 messages Jul 22 – Jul 24

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover bpf: Preserve pointer state for commuted arithmetic

This series fixes pointer-state propagation for commuted scalar += pointer arithmetic in the verifier.

Patch 1 keeps the full pointer register state when the pointer operand is the source of the add, which preserves fields such as the stack frame number and parent id instead of copying only type and id.

Patch 2 builds on that state propagation and moves the untrusted PTR_TO_MEM early return after it, so scalar += untrusted_pointer is modeled as PTR_TO_MEM and remains usable through the probe-read path.

Patch 3 adds verifier selftests for stack frame number preservation, readonly-untrusted memory access, and dynptr data-slice invalidation.

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.

v2: https://lore.kernel.org/bpf/[email protected]/
v1: https://lore.kernel.org/bpf/[email protected]/

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                         | 35 +++++++++-------
 .../testing/selftests/bpf/progs/dynptr_fail.c | 31 ++++++++++++++
 .../bpf/progs/mem_rdonly_untrusted.c          | 17 ++++++++
 .../bpf/progs/verifier_basic_stack.c          | 41 +++++++++++++++++++
 4 files changed, 110 insertions(+), 14 deletions(-)

base-commit: a23a71823352e2d792dcaae25f1ebb744acbfc0b

Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 bpf: Preserve pointer state for commuted arithmetic

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 verifier-env scratch storage 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 f1174f77b50c ("bpf/verifier: rework value tracking")
---
 kernel/bpf/verifier.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +14 -7
index 52be0a118cce0..085cbd5222737 100644
@@ -13726,11 +13726,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 	s64 smin_val = reg_smin(off_reg), smax_val = reg_smax(off_reg);
 	u64 umin_val = reg_umin(off_reg), umax_val = reg_umax(off_reg);
 	struct bpf_sanitize_info info = {};
+	const struct bpf_reg_state *orig_off_reg = off_reg;
+	bool ptr_is_dst_reg;
 	u8 opcode = BPF_OP(insn->code);
 	u32 dst = insn->dst_reg;
 	int ret, bounds_ret;
 
 	dst_reg = &regs[dst];
+	ptr_is_dst_reg = ptr_reg == dst_reg;
 
 	if ((known && (smin_val != smax_val || umin_val != umax_val)) ||
 	    smin_val > smax_val || umin_val > umax_val) {
@@ -13796,11 +13799,15 @@ 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.
 	 */
-	dst_reg->type = ptr_reg->type;
-	dst_reg->id = ptr_reg->id;
+	if (!ptr_is_dst_reg) {
+		env->fake_reg[0] = *off_reg;
+		off_reg = &env->fake_reg[0];
+		*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))
@@ -13813,7 +13820,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, orig_off_reg, dst_reg);
 	}
 
 	switch (opcode) {
@@ -13843,7 +13850,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 		}
 		break;
 	case BPF_SUB:
-		if (dst_reg == off_reg) {
+		if (!ptr_is_dst_reg) {
 			/* scalar -= pointer.  Creates an unknown scalar */
 			verbose(env, "R%d tried to subtract pointer from scalar\n",
 				dst);
@@ -13906,7 +13913,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, orig_off_reg, dst_reg);
 	}
 
 	return 0;
Signature · 1 line
2.34.1
#2 lore raw

Oops, missed there was already v3 yesterday. Reposting comments here.

Quoted 11 lines · Yiyang Chen

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 verifier-env scratch storage 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 f1174f77b50c ("bpf/verifier: rework value tracking")

Nit: This doesn't look right. Without bpf2bpf there is just one frame, hence there isn't a bug at this point. I think the commit f4d7e40a5b71 ("bpf: introduce function calls (verification)") the you mentioned in v1 should be used here instead.

[...]

#3 lore raw
In reply to Yiyang Chen

On Wed, Jul 22, 2026 at 05:27:31AM +0000, Yiyang Chen wrote:
[...]

Quoted 18 lines
kernel/bpf/verifier.c +3 -0
@@ -13726,11 +13726,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 	s64 smin_val = reg_smin(off_reg), smax_val = reg_smax(off_reg);
 	u64 umin_val = reg_umin(off_reg), umax_val = reg_umax(off_reg);
 	struct bpf_sanitize_info info = {};
+	const struct bpf_reg_state *orig_off_reg = off_reg;
+	bool ptr_is_dst_reg;
 
 	u8 opcode = BPF_OP(insn->code);
 	u32 dst = insn->dst_reg;
 	int ret, bounds_ret;
 
 	dst_reg = &regs[dst];
+	ptr_is_dst_reg = ptr_reg == dst_reg;
 
 	if ((known && (smin_val != smax_val || umin_val != umax_val)) ||

smin_val > smax_val || umin_val > umax_val) {

[...]

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
@@ -13813,7 +13820,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, orig_off_reg, dst_reg);
 	}
 
 	switch (opcode) {

[...]

kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env, · line 13919

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
@@ -13906,7 +13913,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, orig_off_reg, dst_reg);
 	}
 
 	return 0;

The sole purpose of having orig_off_reg seem to be for satisfying the 'off_reg == dst_reg' conditional in sanitize_err(). If that's the case, maybe it is better to change sanitize_err to accept an ptr_is_dst_reg argument too.

And given now that we are starting to make more use of scratch register states, it seems better to get rid the likes of 'off_reg == dst_reg' all together, and simply have ptr_is_dst_reg passed down by adjust_scalar_min_max_vals().

kernel/bpf/verifier.c +17 -15
index 52be0a118cce..72d49f2a57a3 100644
@@ -13505,13 +13505,13 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
 			    const struct bpf_reg_state *off_reg,
 			    struct bpf_reg_state *dst_reg,
 			    struct bpf_sanitize_info *info,
-			    const bool commit_window)
+			    const bool commit_window,
+			    const bool ptr_is_dst_reg)
 {
 	struct bpf_insn_aux_data *aux = commit_window ? cur_aux(env) : &info->aux;
 	struct bpf_verifier_state *vstate = env->cur_state;
 	bool off_is_imm = tnum_is_const(off_reg->var_off);
 	bool off_is_neg = reg_smin(off_reg) < 0;
-	bool ptr_is_dst_reg = ptr_reg == dst_reg;
 	u8 opcode = BPF_OP(insn->code);
 	u32 alu_state, alu_limit;
 	struct bpf_reg_state tmp;
@@ -13611,7 +13611,8 @@ static void sanitize_mark_insn_seen(struct bpf_verifier_env *env)
 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)
+			const struct bpf_reg_state *dst_reg,
+			const bool ptr_is_dst_reg)
 {
 	static const char *err = "pointer arithmetic with it prohibited for !root";
 	const char *op = BPF_OP(insn->code) == BPF_ADD ? "add" : "sub";
@@ -13620,11 +13621,11 @@ static int sanitize_err(struct bpf_verifier_env *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);
+			!ptr_is_dst_reg ? dst : src, err);
 		break;
 	case REASON_TYPE:
 		verbose(env, "R%d has pointer with unsupported alu operation, %s\n",
-			off_reg == dst_reg ? src : dst, err);
+			!ptr_is_dst_reg ? src : dst, err);
 		break;
 	case REASON_PATHS:
 		verbose(env, "R%d tried to %s from different maps, paths or scalars, %s\n",
@@ -13717,7 +13718,8 @@ static int sanitize_check_bounds(struct bpf_verifier_env *env,
 static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 				   struct bpf_insn *insn,
 				   const struct bpf_reg_state *ptr_reg,
-				   const struct bpf_reg_state *off_reg)
+				   const struct bpf_reg_state *off_reg,
+				   const bool ptr_is_dst_reg)
 {
 	struct bpf_verifier_state *vstate = env->cur_state;
 	struct bpf_func_state *state = vstate->frame[vstate->curframe];
@@ -13811,9 +13813,9 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 
 	if (sanitize_needed(opcode)) {
 		ret = sanitize_ptr_alu(env, insn, ptr_reg, off_reg, dst_reg,
-				       &info, false);
+				       &info, false, ptr_is_dst_reg);
 		if (ret < 0)
-			return sanitize_err(env, insn, ret, off_reg, dst_reg);
+			return sanitize_err(env, insn, ret, off_reg, dst_reg, ptr_is_dst_reg);
 	}
 
 	switch (opcode) {
@@ -13843,7 +13845,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 		}
 		break;
 	case BPF_SUB:
-		if (dst_reg == off_reg) {
+		if (!ptr_is_dst_reg) {
 			/* scalar -= pointer.  Creates an unknown scalar */
 			verbose(env, "R%d tried to subtract pointer from scalar\n",
 				dst);
@@ -13897,7 +13899,7 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 		return bounds_ret;
 	if (sanitize_needed(opcode)) {
 		ret = sanitize_ptr_alu(env, insn, dst_reg, off_reg, dst_reg,
-				       &info, true);
+				       &info, true, ptr_is_dst_reg);
 		if (verifier_bug_if(!can_skip_alu_sanitation(env, insn)
 				    && !env->cur_state->speculative
 				    && bounds_ret
@@ -13906,7 +13908,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, off_reg, dst_reg, ptr_is_dst_reg);
 	}
 
 	return 0;
@@ -14658,7 +14660,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, NULL, NULL, true /* does it matter here? */);
 	}
 
 	/* Calculate sign/unsigned bounds and tnum for alu32 and alu64 bit ops.
@@ -14862,7 +14864,7 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
 				if (err)
 					return err;
 				return adjust_ptr_min_max_vals(env, insn,
-							       src_reg, dst_reg);
+							       src_reg, dst_reg, false);
 			}
 		} else if (ptr_reg) {
 			/* pointer += scalar */
@@ -14870,7 +14872,7 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
 			if (err)
 				return err;
 			return adjust_ptr_min_max_vals(env, insn,
-						       dst_reg, src_reg);
+						       dst_reg, src_reg, true);
 		} else if (dst_reg->precise) {
 			/* if dst_reg is precise, src_reg should be precise as well */
 			err = mark_chain_precision(env, insn->src_reg);
@@ -14886,7 +14888,7 @@ static int adjust_reg_min_max_vals(struct bpf_verifier_env *env,
 		src_reg = &off_reg;
 		if (ptr_reg) /* pointer += K */
 			return adjust_ptr_min_max_vals(env, insn,
-						       ptr_reg, src_reg);
+						       ptr_reg, src_reg, true);
 	}
 
 	/* Got here implies adding two SCALAR_VALUEs */
maintainer
#4 lore raw

On Thu, 2026-07-23 at 12:25 +0800, Shung-Hsi Yu wrote:

...

Quoted 16 lines

The sole purpose of having orig_off_reg seem to be for satisfying the 'off_reg == dst_reg' conditional in sanitize_err(). If that's the case, maybe it is better to change sanitize_err to accept an ptr_is_dst_reg argument too.

And given now that we are starting to make more use of scratch register states, it seems better to get rid the likes of 'off_reg == dst_reg' all together, and simply have ptr_is_dst_reg passed down by adjust_scalar_min_max_vals().

kernel/bpf/verifier.c +0 -0
index 52be0a118cce..72d49f2a57a3 100644

kernel/bpf/verifier.c static int sanitize_ptr_alu(struct bpf_verifier_env *env, · line 13509

Quoted 6 lines
kernel/bpf/verifier.c +2 -1
@@ -13505,13 +13505,13 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
 			    const struct bpf_reg_state *off_reg,
 			    struct bpf_reg_state *dst_reg,
 			    struct bpf_sanitize_info *info,
-			    const bool commit_window)
+			    const bool commit_window,
+			    const bool ptr_is_dst_reg)

That's a bit invasive. I think the simplest thing to do is to drop off_reg and dst_reg parameters from sanitize_err() and re-derive the information about which one is a pointer.

...

maintainer
#5 lore raw
Quoted 15 lines · Eduard Zingerman

On Thu, 2026-07-23 at 12:25 +0800, Shung-Hsi Yu wrote:

...

Quoted 11 lines

The sole purpose of having orig_off_reg seem to be for satisfying the 'off_reg == dst_reg' conditional in sanitize_err(). If that's the case, maybe it is better to change sanitize_err to accept an ptr_is_dst_reg argument too.

And given now that we are starting to make more use of scratch register states, it seems better to get rid the likes of 'off_reg == dst_reg' all together, and simply have ptr_is_dst_reg passed down by adjust_scalar_min_max_vals().

Quoted 15 lines

kernel/bpf/verifier.c static int sanitize_ptr_alu(struct bpf_verifier_env *env, · line 13509

Quoted 11 lines
kernel/bpf/verifier.c +2 -1
index 52be0a118cce..72d49f2a57a3 100644
@@ -13505,13 +13505,13 @@ static int sanitize_ptr_alu(struct bpf_verifier_env *env,
 			    const struct bpf_reg_state *off_reg,
 			    struct bpf_reg_state *dst_reg,
 			    struct bpf_sanitize_info *info,
-			    const bool commit_window)
+			    const bool commit_window,
+			    const bool ptr_is_dst_reg)

That's a bit invasive. I think the simplest thing to do is to drop off_reg and dst_reg parameters from sanitize_err() and re-derive the information about which one is a pointer.

Scratch that, dst type is gone when sanitize_err() called.

maintainer Tested-by Daniel Wade on v3 1/3
#6 lore raw
In reply to Yiyang Chen

kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,

Quoted 22 lines · Yiyang Chen

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 verifier-env scratch storage 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 f1174f77b50c ("bpf/verifier: rework value tracking")

---

 kernel/bpf/verifier.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +0 -0
index 52be0a118cce0..085cbd5222737 100644
@@ -13726,11 +13726,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,

Yiyang,

I noticed there there is a temporary 'off' reg allocated on stack by the caller of this function. So, let's reuse it, and also adjust the sanitize_err signature to minimize changes.

Could you please re-spin using the attached patches?

Shung-Hsi, wdyt?

...

From e2404beea988a35d8ecb16ed2ac0c36d8729e1b8 Mon Sep 17 00:00:00 2001
From: Yiyang Chen <[email protected]>
Date: Wed, 22 Jul 2026 05:27:31 +0000
Subject: [PATCH 2/4] bpf: Preserve pointer state for commuted arithmetic

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 verifier-env scratch storage 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 f1174f77b50c ("bpf/verifier: rework value tracking")
---
 kernel/bpf/verifier.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +8 -7
index 712a77693ef5..44a40c766f82 100644
@@ -13794,11 +13794,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))
@@ -13841,7 +13842,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);
@@ -14859,8 +14860,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.53.0-Meta

From df9145952923ef35e78527701200d53b76ec87cd Mon Sep 17 00:00:00 2001
From: Eduard Zingerman <[email protected]>
Date: Thu, 23 Jul 2026 11:04:55 -0700
Subject: [PATCH 1/4] bpf: simplify sanitize_err() signature

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
index 52be0a118cce..712a77693ef5 100644
@@ -13608,23 +13608,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",
@@ -13813,7 +13811,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) {
@@ -13906,7 +13904,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;
@@ -14658,7 +14656,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.53.0-Meta
#7 lore raw
Quoted 26 lines · Eduard Zingerman

kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,

Quoted 22 lines · Yiyang Chen

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 verifier-env scratch storage 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 f1174f77b50c ("bpf/verifier: rework value tracking")

---

 kernel/bpf/verifier.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +0 -0
index 52be0a118cce0..085cbd5222737 100644
@@ -13726,11 +13726,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,

Yiyang,

Quoted 2 lines

I noticed there there is a temporary 'off' reg allocated on stack by the caller of this function. So, let's reuse it, ...

'off_reg' perhaps?

+1

Quoted 6 lines

and also adjust the
sanitize_err signature to minimize changes.

Could you please re-spin using the attached patches?

Shung-Hsi, wdyt?

Refactoring LGTM. Feels like the alias check 'dst_reg != ptr_reg' would still bite us later, but perhaps that something for another time.

...

maintainer
#8 lore raw
Quoted 37 lines · Shung-Hsi Yu
Quoted 26 lines · Eduard Zingerman

kernel/bpf/verifier.c static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,

Quoted 22 lines · Yiyang Chen

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 verifier-env scratch storage 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 f1174f77b50c ("bpf/verifier: rework value tracking")

---

 kernel/bpf/verifier.c | 21 ++++++++++++++-------

 1 file changed, 14 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +0 -0
index 52be0a118cce0..085cbd5222737 100644
@@ -13726,11 +13726,14 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,

Yiyang,

Quoted 2 lines

I noticed there there is a temporary 'off' reg allocated on stack by the caller of this function. So, let's reuse it, ...

'off_reg' perhaps?

+1

Quoted 3 lines

and also adjust the
sanitize_err signature to minimize changes.

Quoted 6 lines
Quoted 3 lines

Could you please re-spin using the attached patches?

Shung-Hsi, wdyt?

Refactoring LGTM. Feels like the alias check 'dst_reg != ptr_reg' would still bite us later, but perhaps that something for another time.

Might be the case, we can extract it as a boolean at the top of the function, same way Yiyang had it, or just pass as a boolean to adjust_ptr_min_max_vals().

#9 lore raw

PATCH v3 2/3 bpf: Propagate untrusted pointer state in commuted arithmetic

In reply to Yiyang Chen

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()")
---
 kernel/bpf/verifier.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
kernel/bpf/verifier.c +7 -7
index 085cbd5222737..18fb6267692c5 100644
@@ -13763,13 +13763,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:
@@ -13809,6 +13802,13 @@ static int adjust_ptr_min_max_vals(struct bpf_verifier_env *env,
 		*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.34.1
#10 lore raw

PATCH v3 3/3 selftests/bpf: Cover commuted pointer state propagation

In reply to Yiyang Chen

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.

---
 .../testing/selftests/bpf/progs/dynptr_fail.c | 31 ++++++++++++++
 .../bpf/progs/mem_rdonly_untrusted.c          | 17 ++++++++
 .../bpf/progs/verifier_basic_stack.c          | 41 +++++++++++++++++++
 3 files changed, 89 insertions(+)
tools/testing/selftests/bpf/progs/dynptr_fail.c +31 -0
index 94489ac64da8e..7da4266bae09b 100644
@@ -1635,6 +1635,37 @@ 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.
  */
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +17 -0
index 5b4453747c230..f166fff8f2176 100644
@@ -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";
tools/testing/selftests/bpf/progs/verifier_basic_stack.c +41 -0

45 lines not on this page · raw message

Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: Cover commuted pointer state propagation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Multi-line comment uses the deprecated style where the text starts on the same line as the opening…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Multi-line comment uses the deprecated style where the text starts on the same line as the opening /*. --

--- Patch [3]: [PATCH bpf-next v3 3/3] selftests/bpf: Cover commuted pointer state propagation --- commit 39ba76cb345769c48c9359e58e1fcdae124fef19
Author: Yiyang Chen <[email protected]>

selftests/bpf: Cover commuted pointer state propagation

This commit adds verifier coverage for three cases affected by preserving the full pointer state across scalar += pointer operations. These cases include stack frame number inheritance, readonly-untrusted memory access, and dynptr data-slice invalidation.

tools/testing/selftests/bpf/progs/dynptr_fail.c static int callback(__u32 index, void *data) · line 1639

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -0
index 94489ac64da8e..7da4266bae09b 100644
@@ -1635,6 +1635,37 @@ static int callback(__u32 index, void *data)
         return 0;
 }
 
+/* A commuted add should preserve the parent id of a dynptr data slice.
+ */

[Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred BPF subsystem style? It looks like the text starts on the same line as the opening /*, whereas the modern kernel style guidelines typically prefer the opening /* to be on its own line for multi-line comments.

Quoted 5 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +5 -0
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int dynptr_slice_commuted_invalidate(void *ctx)
+{
+	struct bpf_dynptr ptr;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Acked-by on v3 3/3
#12 lore raw

Re: [PATCH bpf-next v3 3/3] selftests/bpf: Cover commuted pointer state propagation

In reply to Yiyang Chen
Quoted 6 lines · Yiyang Chen

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.

---

Quoted 10 lines
 .../testing/selftests/bpf/progs/dynptr_fail.c | 31 ++++++++++++++
 .../bpf/progs/mem_rdonly_untrusted.c          | 17 ++++++++
 .../bpf/progs/verifier_basic_stack.c          | 41 +++++++++++++++++++
 3 files changed, 89 insertions(+)
tools/testing/selftests/bpf/progs/dynptr_fail.c +0 -0
index 94489ac64da8e..7da4266bae09b 100644

tools/testing/selftests/bpf/progs/dynptr_fail.c static int callback(__u32 index, void *data) · line 1639

Quoted 5 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -0
@@ -1635,6 +1635,37 @@ static int callback(__u32 index, void *data)
         return 0;
 }
 
+/* A commuted add should preserve the parent id of a dynptr data slice.
+ */

Nit: comments style is:

/* foo */

or

/*
* foo
* bar
*/

Quoted 6 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +6 -0
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int dynptr_slice_commuted_invalidate(void *ctx)
+{
+	struct bpf_dynptr ptr;
+	__u32 *slice, *derived;

tools/testing/selftests/bpf/progs/dynptr_fail.c static int callback(__u32 index, void *data) · line 1655

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +10 -0
+
+	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;"

Nit: no need to zero out 'src'?

Quoted 13 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +13 -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;
+}
+

...

Tested-by on the v3 series
#13 lore raw
In reply to Yiyang Chen

Hi Eduard, Yiyang,

Thanks for the pointer. I independently tested Yiyang's v3 series on the source-matched f105f3631d51 base with the same kernel config used for my original report.

Before the series, my impact witness reached chosen-address kernel read/write and changed UID/GID 65534 with CAP_BPF and CAP_PERFMON to UID/GID 0 in 3/3 runs. With all three v3 patches applied, the intended untrusted-pointer positive control still passed, while the impact program was rejected at "R0 pointer += pointer prohibited" before any address leak or privilege transition in 3/3 runs.

I also ran the three focused cases added by the v3 series. All passed:

mem_rdonly_untrusted/ldx_is_ok_commuted_addr dynptr/dynptr_slice_commuted_invalidate verifier_basic_stack/stack pointer arithmetic preserves frame number

Given that the affected code has shipped and the issue reaches kernel read/write from a CAP_BPF and CAP_PERFMON context, should this be routed through bpf and marked for stable rather than bpf-next?

For the v3 series:

Regards,
Daniel