Skip to contentSkip to conversation

Object relationship tracking refactor followup

PATCH Applied Amery Hung 3 participants 9 messages Jun 5

Subsystems (6)
lore
Share & tools
lore

Amery Hung

author
#0 lore raw

v3 cover Object relationship tracking refactor followup

Hi,

The main patchset refactoring object relationship tracking in the verifier has landed and this is a followup that addresses the remaining feedback in v6 [0].

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

v2 -> v3
- Fix cleanup in patch 2 (AI bots)

v1 -> v2
- Add patch 2 fixing silent failure when acquiring reference for struct_ops argument
- Add patch 4 removing WARN_ON_ONCE in check_ids()
- Add fix tags

Amery Hung (5):
bpf: Fix dead error check on acquire_reference() in check_kfunc_call
bpf: Check acquire_reference() error for "__ref" struct_ops arguments
bpf: Compare parent_id in refsafe() for REF_TYPE_PTR
bpf: Remove WARN_ON_ONCE in check_ids()
selftests/bpf: Use bpf_dynptr_slice() to read file dynptr in leak test

 kernel/bpf/states.c                                 | 11 +++++++++--
 kernel/bpf/verifier.c                               | 13 +++++++++----
 .../testing/selftests/bpf/progs/file_reader_fail.c  |  8 ++++----
 3 files changed, 22 insertions(+), 10 deletions(-)
Signature · 1 line
2.53.0-Meta
author
#1 lore raw

PATCH v3 1/5 bpf: Fix dead error check on acquire_reference() in check_kfunc_call

acquire_reference() returns a signed int that may be a negative errno but was converted to unsigned, which makes the subsequent error check deadcode. Fix it by declaring 'id' as int so the error path is taken correctly.

Fixes 308c7a0ae885 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")
---
 kernel/bpf/verifier.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
index 8ed484cb1a8a..6446db9628ae 100644
@@ -12817,9 +12817,10 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	struct bpf_kfunc_call_arg_meta meta;
 	struct bpf_insn_aux_data *insn_aux;
 	int err, insn_idx = *insn_idx_p;
-	u32 i, nargs, ptr_type_id, id;
 	const struct btf_param *args;
+	u32 i, nargs, ptr_type_id;
 	struct btf *desc_btf;
+	int id;
 
 	/* skip for now, but return error when we find this in fixup_kfunc_call */
 	if (!insn->imm)
Signature · 1 line
2.53.0-Meta
author
#2 lore raw

PATCH v3 2/5 bpf: Check acquire_reference() error for "__ref" struct_ops arguments

In reply to Amery Hung

When acquiring references for struct_ops program arguments tagged with "__ref", the return value of acquire_reference() was stored directly into u32 ctx_arg_info[i].ref_id without checking for failure. acquire_reference() returns -ENOMEM when acquire_reference_state() fails to allocate, so the error was silently stored as a ref_id instead of aborting verification. Fix it by checking the return.

Fixes a687df2008f6 ("bpf: Support getting referenced kptr from struct_ops argument")
---
 kernel/bpf/verifier.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +7 -3
index 6446db9628ae..80a72402463d 100644
@@ -18362,9 +18362,13 @@ static int do_check_common(struct bpf_verifier_env *env, int subprog)
 
 	/* Acquire references for struct_ops program arguments tagged with "__ref" */
 	if (!subprog && env->prog->type == BPF_PROG_TYPE_STRUCT_OPS) {
-		for (i = 0; i < aux->ctx_arg_info_size; i++)
-			aux->ctx_arg_info[i].ref_id = aux->ctx_arg_info[i].refcounted ?
-						      acquire_reference(env, 0, 0) : 0;
+		for (i = 0; i < aux->ctx_arg_info_size; i++) {
+			ret = aux->ctx_arg_info[i].refcounted ? acquire_reference(env, 0, 0) : 0;
+			if (ret < 0)
+				goto out;
+
+			aux->ctx_arg_info[i].ref_id = ret;
+		}
 	}
 
 	ret = do_check(env);
Signature · 1 line
2.53.0-Meta
author
#3 lore raw

PATCH v3 3/5 bpf: Compare parent_id in refsafe() for REF_TYPE_PTR

In reply to Amery Hung

refsafe() compared each reference's id and type but not its parent_id, so two states whose PTR references differ only in the parent object they were derived from could be wrongly treated as equivalent and pruned. Fix it by checking parent_id too.

Fixes 308c7a0ae885 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")
---
 kernel/bpf/states.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/bpf/states.c +3 -0
index 5945956a7573..06d9ae24f006 100644
@@ -890,6 +890,9 @@ static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c
 			return false;
 		switch (old->refs[i].type) {
 		case REF_TYPE_PTR:
+			if (!check_ids(old->refs[i].parent_id, cur->refs[i].parent_id, idmap))
+				return false;
+			break;
 		case REF_TYPE_IRQ:
 			break;
 		case REF_TYPE_LOCK:
Signature · 1 line
2.53.0-Meta
#4 lore raw

Re: [PATCH bpf-next v3 3/5] bpf: Compare parent_id in refsafe() for REF_TYPE_PTR

Feeding parent_id through check_ids() here was raised during review (by the bpf-ci and Sashiko bots and by Eduard Zingerman on v1) as something that makes BPF_ID_MAP_SIZE exhaustion more reachable…

kernel/bpf/states.c static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c · line 895

Quoted 11 lines
kernel/bpf/states.c +3 -0
index 5945956a7573..06d9ae24f006 100644
@@ -890,6 +890,9 @@ static bool refsafe(struct bpf_verifier_state *old, struct bpf_verifier_state *c
 			return false;
 		switch (old->refs[i].type) {
 		case REF_TYPE_PTR:
+			if (!check_ids(old->refs[i].parent_id, cur->refs[i].parent_id, idmap))
+				return false;
+			break;

Feeding parent_id through check_ids() here was raised during review (by the bpf-ci and Sashiko bots and by Eduard Zingerman on v1) as something that makes BPF_ID_MAP_SIZE exhaustion more reachable, which would hit the WARN_ON_ONCE(1) in check_ids() and become a DoS under panic_on_warn.

The agreed resolution in that discussion was to drop the WARN_ON_ONCE(1) in check_ids(), since not all ids correspond to register or stack slots anymore:

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

"I agree that it should be fine to just return false from check_ids()
if there is no space in the idmap. However, since not all ids correspond to register/stack slots anymore, I think that WARN_ON_ONCE(1) should be removed."

The reply indicated that removal lands in patch 4 of the series. In this branch only 3 patches are present, and check_ids() still contains the WARN_ON_ONCE(1):

kernel/bpf/states.c:check_ids() {
	...
	/* We ran out of idmap slots, which should be impossible */
	WARN_ON_ONCE(1);
	return false;
}

Should the WARN_ON_ONCE(1) removal be applied together with this change, so the parent_id comparison is not added before its dependent fix is in place?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27038754519

author
#5 lore raw

PATCH v3 4/5 bpf: Remove WARN_ON_ONCE in check_ids()

In reply to Amery Hung

check_ids() warned when it ran out of idmap slots, assuming this was impossible because the slots are bounded by the number of registers and stack slots. That assumption no longer holds: referenced dynptrs acquire an intermediate reference that lives in refs[] but is not backed by any register or stack slot [0], so a program can accumulate more reference ids than the idmap can hold and exhaust it.

Exhaustion is fine for verification correctness. check_ids() already returns false, which makes the states compare as not equivalent and prevents unsound pruning. The only effect of the WARN_ON_ONCE() is log noise, or a panic under panic_on_warn. Drop the warning and keep returning false.

[0] 308c7a0ae885 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")

---
 kernel/bpf/states.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/bpf/states.c +6 -2
index 06d9ae24f006..32f346ce3ffc 100644
@@ -343,8 +343,12 @@ static bool check_ids(u32 old_id, u32 cur_id, struct bpf_idmap *idmap)
 		return true;
 	}
 
-	/* We ran out of idmap slots, which should be impossible */
-	WARN_ON_ONCE(1);
+	/*
+	 * idmap slots are bounded by the number of registers and stack slots.
+	 * Since referenced dynptrs acquire intermediate references that do
+	 * not live in either, so the map can be exhausted. Since it is unlikely,
+	 * fail the verification by treating the states as not equivalent.
+	 */
 	return false;
 }
 
Signature · 1 line
2.53.0-Meta
#6 lore raw

Re: [PATCH bpf-next v3 4/5] bpf: Remove WARN_ON_ONCE in check_ids()

This looks like a bug fix for a warning that can panic under panic_on_warn, but it does not carry a Fixes: tag. The body attributes the newly reachable exhaustion to commit 308c7a0ae885, which…
Quoted 14 lines

bpf: Remove WARN_ON_ONCE in check_ids()

check_ids() warned when it ran out of idmap slots, assuming this was impossible because the slots are bounded by the number of registers and stack slots. That assumption no longer holds: referenced dynptrs acquire an intermediate reference that lives in refs[] but is not backed by any register or stack slot [0], so a program can accumulate more reference ids than the idmap can hold and exhaust it.
Exhaustion is fine for verification correctness. check_ids() already returns false, which makes the states compare as not equivalent and prevents unsound pruning. The only effect of the WARN_ON_ONCE() is log noise, or a panic under panic_on_warn. Drop the warning and keep returning false.
[0] 308c7a0ae885 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")

Quoted 1 line

This looks like a bug fix for a warning that can panic under panic_on_warn, but it does not carry a Fixes: tag. The body attributes the newly reachable exhaustion to commit 308c7a0ae885, which introduced referenced dynptrs that acquire an intermediate reference in refs[] not backed by any register or stack slot.

Should this include:

Fixes 308c7a0ae885 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug")

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/27038754519

author
#7 lore raw

PATCH v3 5/5 selftests/bpf: Use bpf_dynptr_slice() to read file dynptr in leak test

In reply to Amery Hung

use_file_dynptr_slice_after_put_file() reads the dynptr via bpf_dynptr_data(), which always returns NULL for a read-only file dynptr, making the example confusing. Switch to bpf_dynptr_slice(), the correct read API for file dynptrs, and read (rather than write) the slice since it is read-only. The test still fails as expected.

---
 tools/testing/selftests/bpf/progs/file_reader_fail.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
tools/testing/selftests/bpf/progs/file_reader_fail.c +4 -4
index d5fae5e4cf9a..3bb9e2612f8f 100644
@@ -87,7 +87,8 @@ int use_file_dynptr_slice_after_put_file(void *ctx)
 	struct task_struct *task = bpf_get_current_task_btf();
 	struct file *file = bpf_get_task_exe_file(task);
 	struct bpf_dynptr dynptr;
-	char *data;
+	char buf[1];
+	const char *data;
 
 	if (!file)
 		return 0;
@@ -95,15 +96,14 @@ int use_file_dynptr_slice_after_put_file(void *ctx)
 	if (bpf_dynptr_from_file(file, 0, &dynptr))
 		goto out;
 
-	data = bpf_dynptr_data(&dynptr, 0, 1);
+	data = bpf_dynptr_slice(&dynptr, 0, buf, sizeof(buf));
 	if (!data)
 		goto out;
 
 	/* this should fail - file dynptr should be discarded first to prevent resource leak */
 	bpf_put_file(file);
 
-	*data = 'x';
-	return 0;
+	return data[0];
 
 out:
 	bpf_dynptr_file_discard(&dynptr);
Signature · 1 line
2.53.0-Meta
#8 lore raw
In reply to Amery Hung
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Fri, 5 Jun 2026 13:20:51 -0700 you wrote:

Hi,

The main patchset refactoring object relationship tracking in the verifier has landed and this is a followup that addresses the remaining feedback in v6 [0].

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

[...]

Here is the summary with links:
- [bpf-next,v3,1/5] bpf: Fix dead error check on acquire_reference() in check_kfunc_call

    https://git.kernel.org/bpf/bpf-next/c/a3863aa4f55e
  - [bpf-next,v3,2/5] bpf: Check acquire_reference() error for "__ref" struct_ops arguments
    https://git.kernel.org/bpf/bpf-next/c/73d475dc6c13
  - [bpf-next,v3,3/5] bpf: Compare parent_id in refsafe() for REF_TYPE_PTR
    https://git.kernel.org/bpf/bpf-next/c/41025f441fe6
  - [bpf-next,v3,4/5] bpf: Remove WARN_ON_ONCE in check_ids()
    https://git.kernel.org/bpf/bpf-next/c/ac7f6c9da6b6
  - [bpf-next,v3,5/5] selftests/bpf: Use bpf_dynptr_slice() to read file dynptr in leak test
    https://git.kernel.org/bpf/bpf-next/c/d83d4f63cb8f

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html