Skip to contentSkip to conversation

Refactor verifier object relationship tracking

PATCH Superseded Amery Hung 7 participants 36 messages Apr 21 – Apr 28

Subsystems (6)

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Amery Hung

author
#0 lore raw

v3 cover Refactor verifier object relationship tracking

Hi all,

This patchset cleans up dynptr handling, refactors object relationship tracking in the verifier by introducing parent_id, and fixes dynptr use-after-free bugs where file/skb dynptrs are not invalidated when the parent referenced object is freed.

* Motivation *

In BPF qdisc programs, an skb can be freed through kfuncs. However, since dynptr does not track the parent referenced object (e.g., skb), the verifier does not invalidate the dynptr after the skb is freed, resulting in use-after-free. The same issue also affects file dynptr.

The figure below shows the current state of object tracking. The verifier tracks objects using three fields: id for nullness tracking, ref_obj_id for lifetime tracking, and dynptr_id for tracking the parent dynptr of a slice (PTR_TO_MEM only). While dynptr_id links slices to their parent dynptr, there is no field that links a dynptr back to its parent skb. When the skb is freed via release_reference(ref_obj_id=1), only objects with ref_obj_id=1 are invalidated. Since skb dynptr is non-referenced (ref_obj_id=0), the dynptr and its derived slices remain accessible.

Current: object (id, ref_obj_id, dynptr_id)
id = unique id of the object (for nullness tracking)
ref_obj_id = id of the referenced object (for lifetime tracking) dynptr_id = id of the parent dynptr (only for PTR_TO_MEM slices)

                      skb (0,1,0)
                             ^
                             ! No link from dynptr to skb
                             +-------------------------------+
                             |           bpf_dynptr_clone    |
                 dynptr A (2,0,0)                dynptr C (4,0,0)
                           ^                               ^
        bpf_dynptr_slice   |                               |
                           |                               |
              slice B (3,0,2)                 slice D (5,0,4)

* Why not simply use ref_obj_id to track the parent? *

A natural first approach is to link dynptr to its parent by sharing the parent's ref_obj_id and propagating it to slices. Now, releasing the skb via release_reference(ref_obj_id=1) correctly invalidates all derived objects.

Attempted fix: share parent's ref_obj_id

                      skb (0,1,0)
                             ^
                             +-------------------------------+
                             |           bpf_dynptr_clone    |
                 dynptr A (2,1,0)                dynptr C (4,1,0)
                           ^                               ^
        bpf_dynptr_slice   |                               |
                           |                               |
              slice B (3,1,2)                 slice D (5,1,4)

However, this approach does not generalize to all dynptr types. Referenced dynptrs such as file dynptr acquire their own ref_obj_id to track the dynptr's lifetime. Since ref_obj_id is already used for the dynptr's own reference, it cannot also be used to point to the parent file object. While it is possible to add specialized handling for individual dynptr types [0], it adds complexity and does not generalize.

An alternative approach is to avoid introducing a new field and instead repurpose ref_obj_id as parent_id by folding lifetime tracking into id [1]. In this design, each object is represented as (id, ref_obj_id) where id is used for both nullness and lifetime tracking, and ref_obj_id tracks the parent object's id.

Attempted: object (id, ref_obj_id)
id = id of the object (for nullness and lifetime tracking)
ref_obj_id = id of the parent object
' = id is referenced

                        skb (1',0)
                             ^
        bpf_dynptr_from_skb  +-------------------------------+
                             |      bpf_dynptr_clone(A, C)   |
                 dynptr A (2,1')                 dynptr C (4,1')
                           ^                               ^
        bpf_dynptr_slice   |                               |
                           |                               |
                slice B (3,2)                   slice D (5,4)

However, this design cannot express the relationship between referenced socket pointers and their casted counterparts. After pointer casting, the original and casted pointers need the same lifetime (same ref_obj_id in the current design) but different nullness (different id). The casted pointer may be NULL even if the original is valid. With id serving as the only field for both nullness and lifetime, and ref_obj_id repurposed as parent, there is no way to express "different identity, same lifetime."

Referenced socket pointer (expressed using current design):

C = ptr_casting_function(A)
ptr A (1,1,0) ptr C (2,1,0)
^ ^
| |
ptr C may be NULL even if ptr A is valid but they have the same lifetime

* New Design: parent_id *

To track precise object relationships, u32 parent_id is added to bpf_reg_state. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id, and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

After: object (id, ref_obj_id, parent_id)
id = unique id of the object (for nullness tracking)
ref_obj_id = id of the referenced object; objects with the same
ref_obj_id share the same lifetime
parent_id = id of the parent object; points to parent's id

               (for object relationship tracking)

                          skb (1,1,0)
                               ^
          bpf_dynptr_from_skb  +-------------------------------+
                               |      bpf_dynptr_clone(A, C)   |
                 dynptr A (2,0,1)               dynptr C (4,0,1)
                           ^                              ^
        bpf_dynptr_slice   |                              |
                           |                              |
              slice B (3,0,2)                slice D (5,0,4)
                       ^
  bpf_dynptr_from_mem  |
  (NOT allowed yet)    |
         dynptr E (6,0,3)

With parent_id, the verifier can precisely track object trees. When the skb is freed, the verifier traverses the tree rooted at skb (id=1) and invalidates all descendants — dynptr A, dynptr C, and their slices. When dynptr A is destroyed by overwriting the stack slot, only dynptr A and its children (slice B, dynptr E) are invalidated; skb, dynptr C, and slice D remain valid.

For referenced dynptr (e.g., file dynptr), the original and its clones share the same ref_obj_id so they are all invalidated together when any one of them is released. For non-referenced dynptr (e.g., skb dynptr), clones live independently since they have ref_obj_id=0.

To avoid recursive call chains when releasing objects (e.g., release_reference() -> unmark_stack_slots_dynptr() -> release_reference()), release_reference() now uses stack-based DFS to find and invalidate all registers and stack slots with matching id or ref_obj_id and all descendants whose parent_id matches. Currently, it skips id == 0, which could be a valid id (e.g., pkt pointer by reading ctx). Future work may start assigning > 0 id to them. This does not affect the current use cases where skb and file parents are both given id > 0.

* Preserving reg->id after null-check *

For parent_id tracking to work, child objects need to refer to the parent's id. This requires two preparatory changes: assigning reg->id when reading referenced kptrs from program context (patch 2), and preserving reg->id of pointer objects after null-check (patch 3). Previously, null-check would clear reg->id, making it impossible for children to reference the parent afterward. The latter causes a slight increase in verified states for some programs. One selftest object sees +19 states (+5.01%). For Meta BPF objects, the increase is also minor, with the largest being +34 states (+3.63%).

* Object relationship in different scenarios (for reference) *

The figures below show how the new design handles all four combinations of referenced/non-referenced dynptr with referenced/non-referenced parent. The relationship between slices and dynptrs is omitted as it is the same across all cases. The main difference is how cloned dynptrs are represented. Since bpf_dynptr_clone() does not initialize a new reference, clones of referenced dynptrs share the same ref_obj_id and must be invalidated together. For non-referenced dynptrs, the original and clones live independently.

(1) Non-referenced dynptr with referenced parent (e.g., skb in Qdisc):

                          skb (1,1,0)
                               ^
          bpf_dynptr_from_skb  +-------------------------------+
                               |      bpf_dynptr_clone(A, C)   |
                 dynptr A (2,0,1)                dynptr C (4,0,1)

(2) Non-referenced dynptr with non-referenced parent (e.g., skb in TC, always valid):

bpf_dynptr_from_skb
bpf_dynptr_clone(A, C)
dynptr A (1,0,0) dynptr C (2,0,0)

dynptr A and C live independently

(3) Referenced dynptr with referenced parent:

                     file (1,1,0)
                           ^ ^
     bpf_dynptr_from_file  | +-------------------------------+
                           |       bpf_dynptr_clone(A, C)    |
             dynptr A (2,3,1)                  dynptr C (4,3,1)
                         ^                                 ^
                         |                                 |
                         dynptr A and C have the same lifetime

(4) Referenced dynptr with non-referenced parent:

bpf_ringbuf_reserve_dynptr
bpf_dynptr_clone(A, C)
dynptr A (1,1,0) dynptr C (2,1,0)
^ ^
| |
dynptr A and C have the same lifetime

[0] https://lore.kernel.org/bpf/[email protected]/
[1] https://github.com/ameryhung/bpf/commits/obj_relationship_v2_no_parent_id/

Changelog:

v2 -> v3
- Rebase to bpf-next/master
- Update veristat numbers
- Update commit msg to explain multiple dropped checks (Mykyta, Andrii)
- Reuse idmap as idstack in release_reference() and check for duplicate id (Mykyta, Andrii)
- Change to use RUN_TEST for qdisc dynptr selftest (Eduard)

v1 -> v2
- Redesign: Use object (id, ref_obj_id, parent_id) instead of (id, ref_obj_id) as it cannot express ptr casting without introducing specialized code to handle the case
- Use stack-based DFS to release objects to avoid recursion (Andrii)
- Keep reg->id after null check
- Add dynptr cleanup
- Fix dynptr kfunc arg type determination
- Add a file dynptr UAF selftest

---

Amery Hung (9):
bpf: Unify dynptr handling in the verifier
bpf: Assign reg->id when getting referenced kptr from ctx
bpf: Preserve reg->id of pointer objects after null-check
bpf: Refactor object relationship tracking and fix dynptr UAF bug
bpf: Remove redundant dynptr arg check for helper selftests/bpf: Test creating dynptr from dynptr data and slice selftests/bpf: Test using dynptr after freeing the underlying object selftests/bpf: Test using slice after invalidating dynptr clone selftests/bpf: Test using file dynptr after the reference on file is
dropped

 include/linux/bpf_verifier.h                  |  34 +-
 kernel/bpf/log.c                              |   4 +-
 kernel/bpf/states.c                           |   9 +-
 kernel/bpf/verifier.c                         | 461 ++++++------------
 .../selftests/bpf/prog_tests/bpf_qdisc.c      |   8 +
 ..._qdisc_dynptr_use_after_invalidate_clone.c |  75 +++
 .../progs/bpf_qdisc_fail__invalid_dynptr.c    |  68 +++
 ...f_qdisc_fail__invalid_dynptr_cross_frame.c |  74 +++
 .../bpf_qdisc_fail__invalid_dynptr_slice.c    |  70 +++
 .../testing/selftests/bpf/progs/dynptr_fail.c |  48 +-
 .../selftests/bpf/progs/file_reader_fail.c    |  60 +++
 .../selftests/bpf/progs/user_ringbuf_fail.c   |   4 +-
 12 files changed, 593 insertions(+), 322 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_dynptr_use_after_invalidate_clone.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr_cross_frame.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr_slice.c
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH v3 1/9 bpf: Unify dynptr handling in the verifier

Simplify dynptr checking for helper and kfunc by unifying it. Remember the initialized dynptr (i.e.,g !(arg_type |= MEM_UNINIT)) pass to a dynptr kfunc during process_dynptr_func() so that we can easily retrieve the information for verification later. By saving it in meta->dynptr, there is no need to call dynptr helpers such as dynptr_id(), dynptr_ref_obj_id() and dynptr_type() in check_func_arg().

Remove and open code the helpers in process_dynptr_func() when saving id, ref_obj_id, and type. It is okay to drop spi < 0 check as is_dynptr_reg_valid_init() has made sure the dynptr is valid.

Besides, since dynptr ref_obj_id information is now pass around in meta->bpf_dynptr_desc, drop the check in helper_multiple_ref_obj_use.

---
 include/linux/bpf_verifier.h |  12 ++-
 kernel/bpf/verifier.c        | 178 +++++++----------------------------
 2 files changed, 41 insertions(+), 149 deletions(-)
include/linux/bpf_verifier.h +7 -5
index b148f816f25b..dc0cff59246d 100644
@@ -1319,6 +1319,12 @@ struct bpf_map_desc {
 	int uid;
 };
 
+struct bpf_dynptr_desc {
+	enum bpf_dynptr_type type;
+	u32 id;
+	u32 ref_obj_id;
+};
+
 struct bpf_kfunc_call_arg_meta {
 	/* In parameters */
 	struct btf *btf;
@@ -1359,16 +1365,12 @@ struct bpf_kfunc_call_arg_meta {
 	struct {
 		struct btf_field *field;
 	} arg_rbtree_root;
-	struct {
-		enum bpf_dynptr_type type;
-		u32 id;
-		u32 ref_obj_id;
-	} initialized_dynptr;
 	struct {
 		u8 spi;
 		u8 frameno;
 	} iter;
 	struct bpf_map_desc map;
+	struct bpf_dynptr_desc dynptr;
 	u64 mem_size;
 };
 
kernel/bpf/verifier.c +34 -144

294 lines not on this page · raw message

Signature · 1 line
2.52.0
#2 lore raw

Re: [PATCH bpf-next v3 1/9] bpf: Unify dynptr handling in the verifier

This isn't a bug, but as noted by Mykyta Yatsenko in v2 review, the new struct bpf_dynptr_desc could benefit from a comment explaining what the ref_obj_id field is for.
Quoted 5 lines
include/linux/bpf_verifier.h +0 -0
index b148f816f25b..dc0cff59246d 100644

include/linux/bpf_verifier.h struct bpf_map_desc { · line 1327

Quoted 9 lines
include/linux/bpf_verifier.h +6 -0
@@ -1319,6 +1319,12 @@ struct bpf_map_desc {
 	int uid;
 };
 
+struct bpf_dynptr_desc {
+	enum bpf_dynptr_type type;
+	u32 id;
+	u32 ref_obj_id;
+};
+

This isn't a bug, but as noted by Mykyta Yatsenko in v2 review, the new struct bpf_dynptr_desc could benefit from a comment explaining what the ref_obj_id field is for.

While you mentioned in your response that comments for id, ref_obj_id, and parent_id will be added in a later refactor patch (#6) since these fields apply to all objects tracked by the verifier, the struct currently lacks this inline documentation. Could this documentation be added now to help readers understand the field's purpose?

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

[ ... ]

---
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/24749741787

Acked-by on v3 1/9
#3 lore raw

Re: [PATCH bpf-next v3 1/9] bpf: Unify dynptr handling in the verifier

In reply to Amery Hung
Quoted 11 lines · Amery Hung

Simplify dynptr checking for helper and kfunc by unifying it. Remember the initialized dynptr (i.e.,g !(arg_type |= MEM_UNINIT)) pass to a dynptr kfunc during process_dynptr_func() so that we can easily retrieve the information for verification later. By saving it in meta->dynptr, there is no need to call dynptr helpers such as dynptr_id(), dynptr_ref_obj_id() and dynptr_type() in check_func_arg().

Remove and open code the helpers in process_dynptr_func() when saving id, ref_obj_id, and type. It is okay to drop spi < 0 check as is_dynptr_reg_valid_init() has made sure the dynptr is valid.

Quoted 6 lines

Besides, since dynptr ref_obj_id information is now pass around in meta->bpf_dynptr_desc, drop the check in helper_multiple_ref_obj_use.

---

Quoted 354 lines
  include/linux/bpf_verifier.h |  12 ++-
  kernel/bpf/verifier.c        | 178 +++++++----------------------------
  2 files changed, 41 insertions(+), 149 deletions(-)
include/linux/bpf_verifier.h +7 -5
index b148f816f25b..dc0cff59246d 100644
@@ -1319,6 +1319,12 @@ struct bpf_map_desc {
  	int uid;
  };
 
+struct bpf_dynptr_desc {
+	enum bpf_dynptr_type type;
+	u32 id;
+	u32 ref_obj_id;
+};
+
  struct bpf_kfunc_call_arg_meta {
  	/* In parameters */
  	struct btf *btf;
@@ -1359,16 +1365,12 @@ struct bpf_kfunc_call_arg_meta {
  	struct {
  		struct btf_field *field;
  	} arg_rbtree_root;
-	struct {
-		enum bpf_dynptr_type type;
-		u32 id;
-		u32 ref_obj_id;
-	} initialized_dynptr;
  	struct {
  		u8 spi;
  		u8 frameno;
  	} iter;
  	struct bpf_map_desc map;
+	struct bpf_dynptr_desc dynptr;
  	u64 mem_size;
  };
 
kernel/bpf/verifier.c +34 -144
index 185210b73385..41e4ea41c72e 100644
@@ -232,6 +232,7 @@ static void bpf_map_key_store(struct bpf_insn_aux_data *aux, u64 state)
 
  struct bpf_call_arg_meta {
  	struct bpf_map_desc map;
+	struct bpf_dynptr_desc dynptr;
  	bool raw_mode;
  	bool pkt_access;
  	u8 release_regno;
@@ -240,7 +241,6 @@ struct bpf_call_arg_meta {
  	int mem_size;
  	u64 msize_max_value;
  	int ref_obj_id;
-	int dynptr_id;
  	int func_id;
  	struct btf *btf;
  	u32 btf_id;
@@ -434,11 +434,6 @@ static bool is_ptr_cast_function(enum bpf_func_id func_id)
  		func_id == BPF_FUNC_skc_to_tcp_request_sock;
  }
 
-static bool is_dynptr_ref_function(enum bpf_func_id func_id)
-{
-	return func_id == BPF_FUNC_dynptr_data;
-}
-
  static bool is_sync_callback_calling_kfunc(u32 btf_id);
  static bool is_async_callback_calling_kfunc(u32 btf_id);
  static bool is_callback_calling_kfunc(u32 btf_id);
@@ -507,8 +502,6 @@ static bool helper_multiple_ref_obj_use(enum bpf_func_id func_id,
  		ref_obj_uses++;
  	if (is_acquire_function(func_id, map))
  		ref_obj_uses++;
-	if (is_dynptr_ref_function(func_id))
-		ref_obj_uses++;
 
  	return ref_obj_uses > 1;
  }
@@ -7433,7 +7426,8 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
   * and checked dynamically during runtime.
   */
  static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
-			       enum bpf_arg_type arg_type, int clone_ref_obj_id)
+			       enum bpf_arg_type arg_type, int clone_ref_obj_id,
+			       struct bpf_dynptr_desc *dynptr)
  {
  	struct bpf_reg_state *reg = reg_state(env, regno);
  	int err;
@@ -7499,6 +7493,20 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
  		}
 
  		err = mark_dynptr_read(env, reg);
+
+		if (dynptr) {
+			struct bpf_func_state *state = bpf_func(env, reg);
+			int spi;
+
+			if (reg->type != CONST_PTR_TO_DYNPTR) {
+				spi = dynptr_get_spi(env, reg);
+				reg = &state->stack[spi].spilled_ptr;
+			}
+
+			dynptr->id = reg->id;
+			dynptr->type = reg->dynptr.type;
+			dynptr->ref_obj_id = reg->ref_obj_id;
+		}
  	}
  	return err;
  }
@@ -8263,72 +8271,6 @@ static int check_func_arg_reg_off(struct bpf_verifier_env *env,
  	}
  }
 
-static struct bpf_reg_state *get_dynptr_arg_reg(struct bpf_verifier_env *env,
-						const struct bpf_func_proto *fn,
-						struct bpf_reg_state *regs)
-{
-	struct bpf_reg_state *state = NULL;
-	int i;
-
-	for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++)
-		if (arg_type_is_dynptr(fn->arg_type[i])) {
-			if (state) {
-				verbose(env, "verifier internal error: multiple dynptr args\n");
-				return NULL;
-			}
-			state = &regs[BPF_REG_1 + i];
-		}
-
-	if (!state)
-		verbose(env, "verifier internal error: no dynptr arg found\n");
-
-	return state;
-}
-
-static int dynptr_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
-{
-	struct bpf_func_state *state = bpf_func(env, reg);
-	int spi;
-
-	if (reg->type == CONST_PTR_TO_DYNPTR)
-		return reg->id;
-	spi = dynptr_get_spi(env, reg);
-	if (spi < 0)
-		return spi;
-	return state->stack[spi].spilled_ptr.id;
-}
-
-static int dynptr_ref_obj_id(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
-{
-	struct bpf_func_state *state = bpf_func(env, reg);
-	int spi;
-
-	if (reg->type == CONST_PTR_TO_DYNPTR)
-		return reg->ref_obj_id;
-	spi = dynptr_get_spi(env, reg);
-	if (spi < 0)
-		return spi;
-	return state->stack[spi].spilled_ptr.ref_obj_id;
-}
-
-static enum bpf_dynptr_type dynptr_get_type(struct bpf_verifier_env *env,
-					    struct bpf_reg_state *reg)
-{
-	struct bpf_func_state *state = bpf_func(env, reg);
-	int spi;
-
-	if (reg->type == CONST_PTR_TO_DYNPTR)
-		return reg->dynptr.type;
-
-	spi = bpf_get_spi(reg->var_off.value);
-	if (spi < 0) {
-		verbose(env, "verifier internal error: invalid spi when querying dynptr type\n");
-		return BPF_DYNPTR_TYPE_INVALID;
-	}
-
-	return state->stack[spi].spilled_ptr.dynptr.type;
-}
-
  static int check_reg_const_str(struct bpf_verifier_env *env,
  			       struct bpf_reg_state *reg, u32 regno)
  {
@@ -8683,7 +8625,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
  					 true, meta);
  		break;
  	case ARG_PTR_TO_DYNPTR:
-		err = process_dynptr_func(env, regno, insn_idx, arg_type, 0);
+		err = process_dynptr_func(env, regno, insn_idx, arg_type, 0, &meta->dynptr);
  		if (err)
  			return err;
  		break;
@@ -9342,7 +9284,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
  			if (ret)
  				return ret;
 
-			ret = process_dynptr_func(env, regno, -1, arg->arg_type, 0);
+			ret = process_dynptr_func(env, regno, -1, arg->arg_type, 0, NULL);
  			if (ret)
  				return ret;
  		} else if (base_type(arg->arg_type) == ARG_PTR_TO_BTF_ID) {
@@ -10429,52 +10371,10 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  			}
  		}
  		break;
-	case BPF_FUNC_dynptr_data:
-	{
-		struct bpf_reg_state *reg;
-		int id, ref_obj_id;
-
-		reg = get_dynptr_arg_reg(env, fn, regs);
-		if (!reg)
-			return -EFAULT;
-
-
-		if (meta.dynptr_id) {
-			verifier_bug(env, "meta.dynptr_id already set");
-			return -EFAULT;
-		}
-		if (meta.ref_obj_id) {
-			verifier_bug(env, "meta.ref_obj_id already set");
-			return -EFAULT;
-		}
-
-		id = dynptr_id(env, reg);
-		if (id < 0) {
-			verifier_bug(env, "failed to obtain dynptr id");
-			return id;
-		}
-
-		ref_obj_id = dynptr_ref_obj_id(env, reg);
-		if (ref_obj_id < 0) {
-			verifier_bug(env, "failed to obtain dynptr ref_obj_id");
-			return ref_obj_id;
-		}
-
-		meta.dynptr_id = id;
-		meta.ref_obj_id = ref_obj_id;
-
-		break;
-	}
  	case BPF_FUNC_dynptr_write:
  	{
-		enum bpf_dynptr_type dynptr_type;
-		struct bpf_reg_state *reg;
+		enum bpf_dynptr_type dynptr_type = meta.dynptr.type;
 
-		reg = get_dynptr_arg_reg(env, fn, regs);
-		if (!reg)
-			return -EFAULT;
-
-		dynptr_type = dynptr_get_type(env, reg);
  		if (dynptr_type == BPF_DYNPTR_TYPE_INVALID)
  			return -EFAULT;
 
@@ -10665,10 +10565,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  		return -EFAULT;
  	}
 
-	if (is_dynptr_ref_function(func_id))
-		regs[BPF_REG_0].dynptr_id = meta.dynptr_id;
-
-	if (is_ptr_cast_function(func_id) || is_dynptr_ref_function(func_id)) {
+	if (is_ptr_cast_function(func_id)) {
  		/* For release_reference() */
  		regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
  	} else if (is_acquire_function(func_id, meta.map.ptr)) {
@@ -10682,6 +10579,11 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  		regs[BPF_REG_0].ref_obj_id = id;
  	}
 
+	if (func_id == BPF_FUNC_dynptr_data) {
+		regs[BPF_REG_0].dynptr_id = meta.dynptr.id;
+		regs[BPF_REG_0].ref_obj_id = meta.dynptr.ref_obj_id;
+	}
+
  	err = do_refine_retval_range(env, regs, fn->ret_type, func_id, &meta);
  	if (err)
  		return err;
@@ -12260,7 +12162,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
  				meta->release_regno = regno;
  			} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_clone] &&
  				   (dynptr_arg_type & MEM_UNINIT)) {
-				enum bpf_dynptr_type parent_type = meta->initialized_dynptr.type;
+				enum bpf_dynptr_type parent_type = meta->dynptr.type;
 
  				if (parent_type == BPF_DYNPTR_TYPE_INVALID) {
  					verifier_bug(env, "no dynptr type for parent of clone");
@@ -12268,29 +12170,17 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
  				}
 
  				dynptr_arg_type |= (unsigned int)get_dynptr_type_flag(parent_type);
-				clone_ref_obj_id = meta->initialized_dynptr.ref_obj_id;
+				clone_ref_obj_id = meta->dynptr.ref_obj_id;
  				if (dynptr_type_refcounted(parent_type) && !clone_ref_obj_id) {
  					verifier_bug(env, "missing ref obj id for parent of clone");
  					return -EFAULT;
  				}
  			}
 
-			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id);
+			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id,
+						  &meta->dynptr);
  			if (ret < 0)
  				return ret;
-
-			if (!(dynptr_arg_type & MEM_UNINIT)) {
-				int id = dynptr_id(env, reg);
-
-				if (id < 0) {
-					verifier_bug(env, "failed to obtain dynptr id");
-					return id;
-				}
-				meta->initialized_dynptr.id = id;
-				meta->initialized_dynptr.type = dynptr_get_type(env, reg);
-				meta->initialized_dynptr.ref_obj_id = dynptr_ref_obj_id(env, reg);
-			}
-
  			break;
  		}
  		case KF_ARG_PTR_TO_ITER:
@@ -12894,7 +12784,7 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_ca
  		}
  	} else if (meta->func_id == special_kfunc_list[KF_bpf_dynptr_slice] ||
  		   meta->func_id == special_kfunc_list[KF_bpf_dynptr_slice_rdwr]) {
-		enum bpf_type_flag type_flag = get_dynptr_type_flag(meta->initialized_dynptr.type);
+		enum bpf_type_flag type_flag = get_dynptr_type_flag(meta->dynptr.type);
 
  		mark_reg_known_zero(env, regs, BPF_REG_0);
 
@@ -12918,11 +12808,11 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_ca
  			}
  		}
 
-		if (!meta->initialized_dynptr.id) {
+		if (!meta->dynptr.id) {
  			verifier_bug(env, "no dynptr id");
  			return -EFAULT;
  		}
-		regs[BPF_REG_0].dynptr_id = meta->initialized_dynptr.id;
+		regs[BPF_REG_0].dynptr_id = meta->dynptr.id;
 
  		/* we don't need to set BPF_REG_0's ref obj id
  		 * because packet slices are not refcounted (see
@@ -13110,7 +13000,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  	if (meta.release_regno) {
  		struct bpf_reg_state *reg = &regs[meta.release_regno];
 
-		if (meta.initialized_dynptr.ref_obj_id) {
+		if (meta.dynptr.ref_obj_id) {
  			err = unmark_stack_slots_dynptr(env, reg);
  		} else {
  			err = release_reference(env, reg->ref_obj_id);
#4 lore raw

Re: [PATCH bpf-next v3 1/9] bpf: Unify dynptr handling in the verifier

In reply to Amery Hung
Quoted 22 lines · Amery Hung

Simplify dynptr checking for helper and kfunc by unifying it. Remember the initialized dynptr (i.e.,g !(arg_type |= MEM_UNINIT)) pass to a dynptr kfunc during process_dynptr_func() so that we can easily retrieve the information for verification later. By saving it in meta->dynptr, there is no need to call dynptr helpers such as dynptr_id(), dynptr_ref_obj_id() and dynptr_type() in check_func_arg().

Remove and open code the helpers in process_dynptr_func() when saving id, ref_obj_id, and type. It is okay to drop spi < 0 check as is_dynptr_reg_valid_init() has made sure the dynptr is valid.

Besides, since dynptr ref_obj_id information is now pass around in meta->bpf_dynptr_desc, drop the check in helper_multiple_ref_obj_use.

---
 include/linux/bpf_verifier.h |  12 ++-
 kernel/bpf/verifier.c        | 178 +++++++----------------------------
 2 files changed, 41 insertions(+), 149 deletions(-)

[...]

Quoted 11 lines
kernel/bpf/verifier.c +2 -1
@@ -7433,7 +7426,8 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
  * and checked dynamically during runtime.
  */
 static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
-                              enum bpf_arg_type arg_type, int clone_ref_obj_id)
+                              enum bpf_arg_type arg_type, int clone_ref_obj_id,
+                              struct bpf_dynptr_desc *dynptr)
 {
        struct bpf_reg_state *reg = reg_state(env, regno);
        int err;

kernel/bpf/verifier.c static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn · line 7495

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
@@ -7499,6 +7493,20 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
                }
 
                err = mark_dynptr_read(env, reg);

if mark_dynptr_read() fails, should we exit instead of proceeding to fill out dynptr info?.. even if mark_dynptr_read() cannot fail because of is_dynptr_reg_valid_init(), it still looks wrong and error-prone

Quoted 17 lines
kernel/bpf/verifier.c +14 -0
+
+               if (dynptr) {
+                       struct bpf_func_state *state = bpf_func(env, reg);
+                       int spi;
+
+                       if (reg->type != CONST_PTR_TO_DYNPTR) {
+                               spi = dynptr_get_spi(env, reg);
+                               reg = &state->stack[spi].spilled_ptr;
+                       }
+
+                       dynptr->id = reg->id;
+                       dynptr->type = reg->dynptr.type;
+                       dynptr->ref_obj_id = reg->ref_obj_id;
+               }
        }
        return err;
 }

[...]

author
#5 lore raw

Re: [PATCH bpf-next v3 1/9] bpf: Unify dynptr handling in the verifier

Quoted 40 lines · Andrii Nakryiko
Quoted 22 lines · Amery Hung

Simplify dynptr checking for helper and kfunc by unifying it. Remember the initialized dynptr (i.e.,g !(arg_type |= MEM_UNINIT)) pass to a dynptr kfunc during process_dynptr_func() so that we can easily retrieve the information for verification later. By saving it in meta->dynptr, there is no need to call dynptr helpers such as dynptr_id(), dynptr_ref_obj_id() and dynptr_type() in check_func_arg().

Remove and open code the helpers in process_dynptr_func() when saving id, ref_obj_id, and type. It is okay to drop spi < 0 check as is_dynptr_reg_valid_init() has made sure the dynptr is valid.

Besides, since dynptr ref_obj_id information is now pass around in meta->bpf_dynptr_desc, drop the check in helper_multiple_ref_obj_use.

---
 include/linux/bpf_verifier.h |  12 ++-
 kernel/bpf/verifier.c        | 178 +++++++----------------------------
 2 files changed, 41 insertions(+), 149 deletions(-)

[...]

Quoted 13 lines
kernel/bpf/verifier.c +2 -1
@@ -7433,7 +7426,8 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
  * and checked dynamically during runtime.
  */
 static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
-                              enum bpf_arg_type arg_type, int clone_ref_obj_id)
+                              enum bpf_arg_type arg_type, int clone_ref_obj_id,
+                              struct bpf_dynptr_desc *dynptr)
 {
        struct bpf_reg_state *reg = reg_state(env, regno);
        int err;
@@ -7499,6 +7493,20 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
                }
 
Quoted 5 lines

kernel/bpf/verifier.c static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn · line 7495

Quoted 1 line
kernel/bpf/verifier.c +0 -0
                err = mark_dynptr_read(env, reg);

if mark_dynptr_read() fails, should we exit instead of proceeding to fill out dynptr info?.. even if mark_dynptr_read() cannot fail because of is_dynptr_reg_valid_init(), it still looks wrong and error-prone

I think I will change mark_dynptr_read() to receive spi from the caller and make the function return void (It is technically nofail after liveness tracking code is removed). Then, move dynptr_get_spi() before it and don't error out. A comment seems to be enough.

/* Cannot fail as we already make sure reg is valid dynptr */ spi = dynptr_get_spi(env, reg);

mark_dynptr_read(env, reg, spi);

Quoted 8 lines

kernel/bpf/verifier.c static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn · line 7502

Quoted 8 lines
kernel/bpf/verifier.c +7 -0
+
+               if (dynptr) {
+                       struct bpf_func_state *state = bpf_func(env, reg);
+                       int spi;
+
+                       if (reg->type != CONST_PTR_TO_DYNPTR) {
+                               spi = dynptr_get_spi(env, reg);

^ move this up

Quoted 12 lines
Quoted 10 lines
kernel/bpf/verifier.c +7 -0
+                               reg = &state->stack[spi].spilled_ptr;
+                       }
+
+                       dynptr->id = reg->id;
+                       dynptr->type = reg->dynptr.type;
+                       dynptr->ref_obj_id = reg->ref_obj_id;
+               }
        }
        return err;
 }

[...]

#6 lore raw

Re: [PATCH bpf-next v3 1/9] bpf: Unify dynptr handling in the verifier

Quoted 49 lines · Amery Hung
Quoted 40 lines · Andrii Nakryiko
Quoted 22 lines · Amery Hung

Simplify dynptr checking for helper and kfunc by unifying it. Remember the initialized dynptr (i.e.,g !(arg_type |= MEM_UNINIT)) pass to a dynptr kfunc during process_dynptr_func() so that we can easily retrieve the information for verification later. By saving it in meta->dynptr, there is no need to call dynptr helpers such as dynptr_id(), dynptr_ref_obj_id() and dynptr_type() in check_func_arg().

Remove and open code the helpers in process_dynptr_func() when saving id, ref_obj_id, and type. It is okay to drop spi < 0 check as is_dynptr_reg_valid_init() has made sure the dynptr is valid.

Besides, since dynptr ref_obj_id information is now pass around in meta->bpf_dynptr_desc, drop the check in helper_multiple_ref_obj_use.

---
 include/linux/bpf_verifier.h |  12 ++-
 kernel/bpf/verifier.c        | 178 +++++++----------------------------
 2 files changed, 41 insertions(+), 149 deletions(-)

[...]

Quoted 13 lines
kernel/bpf/verifier.c +2 -1
@@ -7433,7 +7426,8 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
  * and checked dynamically during runtime.
  */
 static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
-                              enum bpf_arg_type arg_type, int clone_ref_obj_id)
+                              enum bpf_arg_type arg_type, int clone_ref_obj_id,
+                              struct bpf_dynptr_desc *dynptr)
 {
        struct bpf_reg_state *reg = reg_state(env, regno);
        int err;
@@ -7499,6 +7493,20 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
                }
 
Quoted 5 lines

kernel/bpf/verifier.c static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn · line 7495

Quoted 1 line
kernel/bpf/verifier.c +0 -0
                err = mark_dynptr_read(env, reg);

if mark_dynptr_read() fails, should we exit instead of proceeding to fill out dynptr info?.. even if mark_dynptr_read() cannot fail because of is_dynptr_reg_valid_init(), it still looks wrong and error-prone

Quoted 4 lines

I think I will change mark_dynptr_read() to receive spi from the caller and make the function return void (It is technically nofail after liveness tracking code is removed). Then, move dynptr_get_spi() before it and don't error out. A comment seems to be enough.

yep, makes sense

Quoted 29 lines

/* Cannot fail as we already make sure reg is valid dynptr */ spi = dynptr_get_spi(env, reg);

mark_dynptr_read(env, reg, spi);

Quoted 8 lines

kernel/bpf/verifier.c static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn · line 7502

Quoted 8 lines
kernel/bpf/verifier.c +7 -0
+
+               if (dynptr) {
+                       struct bpf_func_state *state = bpf_func(env, reg);
+                       int spi;
+
+                       if (reg->type != CONST_PTR_TO_DYNPTR) {
+                               spi = dynptr_get_spi(env, reg);

^ move this up

Quoted 12 lines
Quoted 10 lines
kernel/bpf/verifier.c +7 -0
+                               reg = &state->stack[spi].spilled_ptr;
+                       }
+
+                       dynptr->id = reg->id;
+                       dynptr->type = reg->dynptr.type;
+                       dynptr->ref_obj_id = reg->ref_obj_id;
+               }
        }
        return err;
 }

[...]

author
#7 lore raw

PATCH v3 2/9 bpf: Assign reg->id when getting referenced kptr from ctx

In reply to Amery Hung

Assign reg->id when getting referenced kptr from read program context to be consistent with R0 of KF_ACQUIRE kfunc. skb dynptr will track the referenced skb in qdisc programs using a new field reg->parent_id in a later patch.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +3 -2
index 41e4ea41c72e..93003a2a96b0 100644
@@ -6448,8 +6448,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 			} else {
 				mark_reg_known_zero(env, regs,
 						    value_regno);
-				if (type_may_be_null(info.reg_type))
-					regs[value_regno].id = ++env->id_gen;
 				/* A load of ctx field could have different
 				 * actual load size with the one encoded in the
 				 * insn. When the dst is PTR, it is for sure not
@@ -6459,8 +6457,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 				if (base_type(info.reg_type) == PTR_TO_BTF_ID) {
 					regs[value_regno].btf = info.btf;
 					regs[value_regno].btf_id = info.btf_id;
+					regs[value_regno].id = info.ref_obj_id;
 					regs[value_regno].ref_obj_id = info.ref_obj_id;
 				}
+				if (type_may_be_null(info.reg_type) && !regs[value_regno].id)
+					regs[value_regno].id = ++env->id_gen;
 			}
 			regs[value_regno].type = info.reg_type;
 		}
Signature · 1 line
2.52.0
maintainer
#8 lore raw

Re: [PATCH bpf-next v3 2/9] bpf: Assign reg->id when getting referenced kptr from ctx

Quoted 24 lines · Amery Hung

Assign reg->id when getting referenced kptr from read program context to be consistent with R0 of KF_ACQUIRE kfunc. skb dynptr will track the referenced skb in qdisc programs using a new field reg->parent_id in a later patch.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -2
index 41e4ea41c72e..93003a2a96b0 100644
@@ -6448,8 +6448,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 			} else {
 				mark_reg_known_zero(env, regs,
 						    value_regno);
-				if (type_may_be_null(info.reg_type))
-					regs[value_regno].id = ++env->id_gen;
 				/* A load of ctx field could have different
 				 * actual load size with the one encoded in the
 				 * insn. When the dst is PTR, it is for sure not

kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 6462

Quoted 6 lines
kernel/bpf/verifier.c +1 -0
@@ -6459,8 +6457,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
 				if (base_type(info.reg_type) == PTR_TO_BTF_ID) {
 					regs[value_regno].btf = info.btf;
 					regs[value_regno].btf_id = info.btf_id;
+					regs[value_regno].id = info.ref_obj_id;
 					regs[value_regno].ref_obj_id = info.ref_obj_id;
 				}

Hi Amery,

Could you please help me double-check my understanding of this change?

It adds a new behavior, if struct_ops method parameter is annotated with __ref, then if a field is read from such struct_ops context the destination register would now have it's .id == ref_obj_id allocated for the parameter at the beginning of do_check(). E.g. for rB = *(T *)(rA + off), if rA is a pointer to struct_ops context and parameter corresponding to off has __ref annotation the rB.id will be assigned, contrary to old behavior, when it remained 0.

This change in behavior, however, becomes visible only after the changes to release_reference() later in the series.
There the rB.id is used to initiate mark_reg_invalid(env, reg) for the rB.

Are the above observations correct?

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+				if (type_may_be_null(info.reg_type) && !regs[value_regno].id)
+					regs[value_regno].id = ++env->id_gen;
 			}
 			regs[value_regno].type = info.reg_type;
 		}
author
#9 lore raw

Re: [PATCH bpf-next v3 2/9] bpf: Assign reg->id when getting referenced kptr from ctx

Quoted 37 lines · Eduard Zingerman
Quoted 24 lines · Amery Hung

Assign reg->id when getting referenced kptr from read program context to be consistent with R0 of KF_ACQUIRE kfunc. skb dynptr will track the referenced skb in qdisc programs using a new field reg->parent_id in a later patch.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -2
index 41e4ea41c72e..93003a2a96b0 100644
@@ -6448,8 +6448,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                      } else {
                              mark_reg_known_zero(env, regs,
                                                  value_regno);
-                             if (type_may_be_null(info.reg_type))
-                                     regs[value_regno].id = ++env->id_gen;
                              /* A load of ctx field could have different
                               * actual load size with the one encoded in the
                               * insn. When the dst is PTR, it is for sure not

kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 6462

Quoted 6 lines
kernel/bpf/verifier.c +1 -0
@@ -6459,8 +6457,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                              if (base_type(info.reg_type) == PTR_TO_BTF_ID) {
                                      regs[value_regno].btf = info.btf;
                                      regs[value_regno].btf_id = info.btf_id;
+                                     regs[value_regno].id = info.ref_obj_id;
                                      regs[value_regno].ref_obj_id = info.ref_obj_id;
                              }

Hi Amery,

Could you please help me double-check my understanding of this change?

Quoted 8 lines

It adds a new behavior, if struct_ops method parameter is annotated with __ref, then if a field is read from such struct_ops context the destination register would now have it's .id == ref_obj_id allocated for the parameter at the beginning of do_check(). E.g. for rB = *(T *)(rA + off), if rA is a pointer to struct_ops context and parameter corresponding to off has __ref annotation the rB.id will be assigned, contrary to old behavior, when it remained 0.

Right

Quoted 5 lines

This change in behavior, however, becomes visible only after the changes to release_reference() later in the series.
There the rB.id is used to initiate mark_reg_invalid(env, reg) for the rB.

I probably should mention it in the commit message. The purpose of the patch is to make the implementation consistent with the design:
parent_id tracks the id of the parent objects.

Without this patch, it should still work since in this case ref_obj_id always equals to id. When release_reference() is called on a KF_RELEASE kfunc, the children objects will still be invalidated correctly.

Quoted 7 lines

Are the above observations correct?

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+                             if (type_may_be_null(info.reg_type) && !regs[value_regno].id)
+                                     regs[value_regno].id = ++env->id_gen;
                      }
                      regs[value_regno].type = info.reg_type;
              }
maintainer
#10 lore raw

Re: [PATCH bpf-next v3 2/9] bpf: Assign reg->id when getting referenced kptr from ctx

Quoted 47 lines · Amery Hung
Quoted 37 lines · Eduard Zingerman
Quoted 24 lines · Amery Hung

Assign reg->id when getting referenced kptr from read program context to be consistent with R0 of KF_ACQUIRE kfunc. skb dynptr will track the referenced skb in qdisc programs using a new field reg->parent_id in a later patch.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -2
index 41e4ea41c72e..93003a2a96b0 100644
@@ -6448,8 +6448,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                      } else {
                              mark_reg_known_zero(env, regs,
                                                  value_regno);
-                             if (type_may_be_null(info.reg_type))
-                                     regs[value_regno].id = ++env->id_gen;
                              /* A load of ctx field could have different
                               * actual load size with the one encoded in the
                               * insn. When the dst is PTR, it is for sure not

kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 6462

Quoted 6 lines
kernel/bpf/verifier.c +1 -0
@@ -6459,8 +6457,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                              if (base_type(info.reg_type) == PTR_TO_BTF_ID) {
                                      regs[value_regno].btf = info.btf;
                                      regs[value_regno].btf_id = info.btf_id;
+                                     regs[value_regno].id = info.ref_obj_id;
                                      regs[value_regno].ref_obj_id = info.ref_obj_id;
                              }

Hi Amery,

Could you please help me double-check my understanding of this change?

Quoted 8 lines

It adds a new behavior, if struct_ops method parameter is annotated with __ref, then if a field is read from such struct_ops context the destination register would now have it's .id == ref_obj_id allocated for the parameter at the beginning of do_check(). E.g. for rB = *(T *)(rA + off), if rA is a pointer to struct_ops context and parameter corresponding to off has __ref annotation the rB.id will be assigned, contrary to old behavior, when it remained 0.

Quoted 1 line

Right

Ack, thank you.

Quoted 6 lines
Quoted 5 lines

This change in behavior, however, becomes visible only after the changes to release_reference() later in the series.
There the rB.id is used to initiate mark_reg_invalid(env, reg) for the rB.

Quoted 8 lines

I probably should mention it in the commit message. The purpose of the patch is to make the implementation consistent with the design:
parent_id tracks the id of the parent objects.

Without this patch, it should still work since in this case ref_obj_id always equals to id. When release_reference() is called on a KF_RELEASE kfunc, the children objects will still be invalidated correctly.

bpf_qdisc_fail__invalid_dynptr_cross_frame.c:invalid_dynptr_cross_frame fails if I comment out the line regs[value_regno].id = info.ref_obj_id;. The added .id link is used to propagate invalidation triggered by free_skb(skb); to hdr.
The patch seem mandatory for the series.

#11 lore raw

Re: [PATCH bpf-next v3 2/9] bpf: Assign reg->id when getting referenced kptr from ctx

Quoted 68 lines · Eduard Zingerman
Quoted 47 lines · Amery Hung
Quoted 37 lines · Eduard Zingerman
Quoted 24 lines · Amery Hung

Assign reg->id when getting referenced kptr from read program context to be consistent with R0 of KF_ACQUIRE kfunc. skb dynptr will track the referenced skb in qdisc programs using a new field reg->parent_id in a later patch.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +0 -2
index 41e4ea41c72e..93003a2a96b0 100644
@@ -6448,8 +6448,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                      } else {
                              mark_reg_known_zero(env, regs,
                                                  value_regno);
-                             if (type_may_be_null(info.reg_type))
-                                     regs[value_regno].id = ++env->id_gen;
                              /* A load of ctx field could have different
                               * actual load size with the one encoded in the
                               * insn. When the dst is PTR, it is for sure not

kernel/bpf/verifier.c static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn · line 6462

Quoted 6 lines
kernel/bpf/verifier.c +1 -0
@@ -6459,8 +6457,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                              if (base_type(info.reg_type) == PTR_TO_BTF_ID) {
                                      regs[value_regno].btf = info.btf;
                                      regs[value_regno].btf_id = info.btf_id;
+                                     regs[value_regno].id = info.ref_obj_id;
                                      regs[value_regno].ref_obj_id = info.ref_obj_id;
                              }

Hi Amery,

Could you please help me double-check my understanding of this change?

Quoted 8 lines

It adds a new behavior, if struct_ops method parameter is annotated with __ref, then if a field is read from such struct_ops context the destination register would now have it's .id == ref_obj_id allocated for the parameter at the beginning of do_check(). E.g. for rB = *(T *)(rA + off), if rA is a pointer to struct_ops context and parameter corresponding to off has __ref annotation the rB.id will be assigned, contrary to old behavior, when it remained 0.

Quoted 1 line

Right

Ack, thank you.

Quoted 6 lines
Quoted 5 lines

This change in behavior, however, becomes visible only after the changes to release_reference() later in the series.
There the rB.id is used to initiate mark_reg_invalid(env, reg) for the rB.

Quoted 8 lines

I probably should mention it in the commit message. The purpose of the patch is to make the implementation consistent with the design:
parent_id tracks the id of the parent objects.

Without this patch, it should still work since in this case ref_obj_id always equals to id. When release_reference() is called on a KF_RELEASE kfunc, the children objects will still be invalidated correctly.

Quoted 5 lines

bpf_qdisc_fail__invalid_dynptr_cross_frame.c:invalid_dynptr_cross_frame fails if I comment out the line regs[value_regno].id = info.ref_obj_id;. The added .id link is used to propagate invalidation triggered by free_skb(skb); to hdr.
The patch seem mandatory for the series.

Ah yes... because an skb dynptr do track parent's id in check_kfunc_args(). Thanks for the correction.

ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type,

                          meta->ref_obj_id ? meta->id : 0, &meta->dynptr);
Acked-by on v3 2/9
#12 lore raw

Re: [PATCH bpf-next v3 2/9] bpf: Assign reg->id when getting referenced kptr from ctx

In reply to Amery Hung
Quoted 11 lines · Amery Hung

Assign reg->id when getting referenced kptr from read program context to be consistent with R0 of KF_ACQUIRE kfunc. skb dynptr will track the referenced skb in qdisc programs using a new field reg->parent_id in a later patch.

---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

LGTM

Quoted 28 lines
kernel/bpf/verifier.c +3 -3
index 41e4ea41c72e..93003a2a96b0 100644
@@ -6448,8 +6448,6 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                        } else {
                                mark_reg_known_zero(env, regs,
                                                    value_regno);
-                               if (type_may_be_null(info.reg_type))
-                                       regs[value_regno].id = ++env->id_gen;
                                /* A load of ctx field could have different
                                 * actual load size with the one encoded in the
                                 * insn. When the dst is PTR, it is for sure not
@@ -6459,8 +6457,11 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn
                                if (base_type(info.reg_type) == PTR_TO_BTF_ID) {
                                        regs[value_regno].btf = info.btf;
                                        regs[value_regno].btf_id = info.btf_id;
+                                       regs[value_regno].id = info.ref_obj_id;
                                        regs[value_regno].ref_obj_id = info.ref_obj_id;
                                }
+                               if (type_may_be_null(info.reg_type) && !regs[value_regno].id)
+                                       regs[value_regno].id = ++env->id_gen;
                        }
                        regs[value_regno].type = info.reg_type;
                }
--

2.52.0

author
#13 lore raw

PATCH v3 3/9 bpf: Preserve reg->id of pointer objects after null-check

In reply to Amery Hung

Preserve reg->id of pointer objects after null-checking the register so that children objects derived from it can still refer to it in the new object relationship tracking mechanism introduced in a later patch. This change incurs a slight increase in the number of states in one selftest bpf object, rbtree_search.bpf.o. For Meta bpf objects, the increase of states is also negligible.

Selftest BPF objects with insns_diff > 0

Program                   Insns (A)  Insns (B)  Insns   (DIFF)  States (A)  States (B)  States (DIFF)
------------------------  ---------  ---------  --------------  ----------  ----------  -------------

rbtree_search 6820 7326 +506 (+7.42%) 379 398 +19 (+5.01%)

Meta BPF objects with insns_diff > 0

Program                   Insns (A)  Insns (B)  Insns   (DIFF)  States (A)  States (B)  States (DIFF)
------------------------  ---------  ---------  --------------  ----------  ----------  -------------
ned_imex_be_tclass               52         57     +5 (+9.62%)           5           6   +1 (+20.00%)
ned_imex_be_tclass               52         57     +5 (+9.62%)           5           6   +1 (+20.00%)
ned_skop_auto_flowlabel         523        526     +3 (+0.57%)          39          40    +1 (+2.56%)
ned_skop_mss                    289        292     +3 (+1.04%)          20          20    +0 (+0.00%)
ned_skopt_bet_classifier         78         82     +4 (+5.13%)           8           8    +0 (+0.00%)
dctcp_update_alpha              252        320   +68 (+26.98%)          21          27   +6 (+28.57%)
dctcp_update_alpha              252        320   +68 (+26.98%)          21          27   +6 (+28.57%)
ned_ts_func                     119        126     +7 (+5.88%)           6           7   +1 (+16.67%)
tw_egress                      1119       1128     +9 (+0.80%)          95          96    +1 (+1.05%)
tw_ingress                     1128       1137     +9 (+0.80%)          95          96    +1 (+1.05%)
tw_tproxy_router               4380       4465    +85 (+1.94%)         114         118    +4 (+3.51%)
tw_tproxy_router4              3093       3170    +77 (+2.49%)          83          88    +5 (+6.02%)
ttls_tc_ingress               34656      35717  +1061 (+3.06%)         936         970   +34 (+3.63%)
tw_twfw_egress               222327     222338    +11 (+0.00%)       10563       10564    +1 (+0.01%)
tw_twfw_ingress               78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_tc_eg                222839     222859    +20 (+0.01%)       10584       10585    +1 (+0.01%)
tw_twfw_tc_in                 78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_egress                 8080       8085     +5 (+0.06%)         456         456    +0 (+0.00%)
tw_twfw_ingress                8053       8056     +3 (+0.04%)         454         454    +0 (+0.00%)
tw_twfw_tc_eg                  8154       8174    +20 (+0.25%)         456         457    +1 (+0.22%)
tw_twfw_tc_in                  8060       8063     +3 (+0.04%)         455         455    +0 (+0.00%)
tw_twfw_egress               222327     222338    +11 (+0.00%)       10563       10564    +1 (+0.01%)
tw_twfw_ingress               78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_tc_eg                222839     222859    +20 (+0.01%)       10584       10585    +1 (+0.01%)
tw_twfw_tc_in                 78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_egress                 8080       8085     +5 (+0.06%)         456         456    +0 (+0.00%)
tw_twfw_ingress                8053       8056     +3 (+0.04%)         454         454    +0 (+0.00%)
tw_twfw_tc_eg                  8154       8174    +20 (+0.25%)         456         457    +1 (+0.22%)
tw_twfw_tc_in                  8060       8063     +3 (+0.04%)         455         455    +0 (+0.00%)

Looking into rbtree_search, the reason for such increase is that the verifier has to explore the main loop shown below for one more iteration until state pruning decides the current state is safe.

long rbtree_search(void *ctx)

{
	...
	bpf_spin_lock(&glock0);
	rb_n = bpf_rbtree_root(&groot0);
	while (can_loop) {
		if (!rb_n) {
			bpf_spin_unlock(&glock0);
			return __LINE__;
		}

		n = rb_entry(rb_n, struct node_data, r0);
		if (lookup_key == n->key0)
			break;
		if (nr_gc < NR_NODES)
			gc_ns[nr_gc++] = rb_n;
		if (lookup_key < n->key0)
			rb_n = bpf_rbtree_left(&groot0, rb_n);
		else
			rb_n = bpf_rbtree_right(&groot0, rb_n);
	}
	...
}

Below is what the verifier sees at the start of each iteration (65: may_goto) after preserving id of rb_n. Without id of rb_n, the verifier stops exploring the loop at iter 16.

           rb_n  gc_ns[15]

iter 15 257 257

iter 16 290 257 rb_n: idmap add 257->290

                        gc_ns[15]: check 257 != 290 --> state not equal

iter 17 325 257 rb_n: idmap add 290->325

                        gc_ns[15]: idmap add 257->257 --> state safe
---
 kernel/bpf/verifier.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +4 -9
index 93003a2a96b0..0313b7d5f6c9 100644
@@ -15886,15 +15886,10 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
 
 		mark_ptr_not_null_reg(reg);
 
-		if (!reg_may_point_to_spin_lock(reg)) {
-			/* For not-NULL ptr, reg->ref_obj_id will be reset
-			 * in release_reference().
-			 *
-			 * reg->id is still used by spin_lock ptr. Other
-			 * than spin_lock ptr type, reg->id can be reset.
-			 */
-			reg->id = 0;
-		}
+		/*
+		 * reg->id is preserved for object relationship tracking
+		 * and spin_lock lock state tracking
+		 */
 	}
 }
 
Signature · 1 line
2.52.0
#14 lore raw

Re: [PATCH bpf-next v3 3/9] bpf: Preserve reg->id of pointer objects after null-check

How does preserving reg->id interact with reg_is_init_pkt_pointer()?
Quoted 11 lines
kernel/bpf/verifier.c +0 -3
index 93003a2a96b0..0313b7d5f6c9 100644
@@ -15886,15 +15886,10 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
 
 		mark_ptr_not_null_reg(reg);
 
-		if (!reg_may_point_to_spin_lock(reg)) {
-			/* For not-NULL ptr, reg->ref_obj_id will be reset
-			 * in release_reference().

kernel/bpf/verifier.c static void mark_ptr_or_null_reg(struct bpf_func_state *state, · line 15892

Quoted 10 lines
kernel/bpf/verifier.c +4 -6
-			 *
-			 * reg->id is still used by spin_lock ptr. Other
-			 * than spin_lock ptr type, reg->id can be reset.
-			 */
-			reg->id = 0;
-		}
+		/*
+		 * reg->id is preserved for object relationship tracking
+		 * and spin_lock lock state tracking
+		 */

How does preserving reg->id interact with reg_is_init_pkt_pointer()?

In v2 review, Alexei Starovoitov raised this concern: "That pointer has to have id == 0". Eduard Zingerman responded that dropping the id == 0 check should be fine, but this change was not made in v3.

The function reg_is_init_pkt_pointer() still checks reg->id == 0:

    return reg->type == which &&
           reg->id == 0 &&
           reg->off == 0 &&

With this patch, if a PTR_TO_PACKET register goes through null-checking in mark_ptr_or_null_reg(), it will now preserve its reg->id instead of resetting it to 0. Would this cause reg_is_init_pkt_pointer() to incorrectly return false for init packet pointers?

Reference: https://lore.kernel.org/bpf/CAADnVQLCKi_QL1FeybW+6MH6FAOAoRJyW06UvtX3my3yNSH5eQ@mail.gmail.com/

kernel/bpf/verifier.c static void mark_ptr_or_null_reg(struct bpf_func_state *state, · line 15894

Quoted 2 lines
kernel/bpf/verifier.c +0 -0
 	}
 }

---
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/24749741787

maintainer
#15 lore raw

Re: [PATCH bpf-next v3 3/9] bpf: Preserve reg->id of pointer objects after null-check

Quoted 34 lines · On Tue, 2026-04-21 at 22:52 +0000, [email protected] wrote:
Quoted 11 lines
kernel/bpf/verifier.c +0 -3
index 93003a2a96b0..0313b7d5f6c9 100644
@@ -15886,15 +15886,10 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
 
  		mark_ptr_not_null_reg(reg);
 
-		if (!reg_may_point_to_spin_lock(reg)) {
-			/* For not-NULL ptr, reg->ref_obj_id will be reset
-			 * in release_reference().

kernel/bpf/verifier.c static void mark_ptr_or_null_reg(struct bpf_func_state *state, · line 15892

Quoted 10 lines
kernel/bpf/verifier.c +4 -6
-			 *
-			 * reg->id is still used by spin_lock ptr. Other
-			 * than spin_lock ptr type, reg->id can be reset.
-			 */
-			reg->id = 0;
-		}
+		/*
+		 * reg->id is preserved for object relationship tracking
+		 * and spin_lock lock state tracking
+		 */

How does preserving reg->id interact with reg_is_init_pkt_pointer()?

In v2 review, Alexei Starovoitov raised this concern: "That pointer has to have id == 0". Eduard Zingerman responded that dropping the id == 0 check should be fine, but this change was not made in v3.

The function reg_is_init_pkt_pointer() still checks reg->id == 0:

    return reg->type == which &&
           reg->id == 0 &&
           reg->off == 0 &&

Quoted 9 lines

With this patch, if a PTR_TO_PACKET register goes through null-checking in mark_ptr_or_null_reg(), it will now preserve its reg->id instead of resetting it to 0. Would this cause reg_is_init_pkt_pointer() to incorrectly return false for init packet pointers?

Reference: https://lore.kernel.org/bpf/CAADnVQLCKi_QL1FeybW+6MH6FAOAoRJyW06UvtX3my3yNSH5eQ@mail.gmail.com/

Quoted 2 lines
kernel/bpf/verifier.c +0 -0
  	}
  }

Bot concern is bogus here, reg_is_init_pkt_pointer() is called only for PTR_TO_PACKET. mark_ptr_or_null_reg() checks for type_may_be_null(reg->type), meaning that for it to trigger one would need a register of type PTR_TO_PACKET | PTR_MAYBE_NULL.
It appears there is no way to construct such type at the moment.

Acked-by on v3 3/9
#16 lore raw

Re: [PATCH bpf-next v3 3/9] bpf: Preserve reg->id of pointer objects after null-check

In reply to Amery Hung
Quoted 86 lines · Amery Hung

Preserve reg->id of pointer objects after null-checking the register so that children objects derived from it can still refer to it in the new object relationship tracking mechanism introduced in a later patch. This change incurs a slight increase in the number of states in one selftest bpf object, rbtree_search.bpf.o. For Meta bpf objects, the increase of states is also negligible.

Selftest BPF objects with insns_diff > 0

Program                   Insns (A)  Insns (B)  Insns   (DIFF)  States (A)  States (B)  States (DIFF)
------------------------  ---------  ---------  --------------  ----------  ----------  -------------

rbtree_search 6820 7326 +506 (+7.42%) 379 398 +19 (+5.01%)

Meta BPF objects with insns_diff > 0

Program                   Insns (A)  Insns (B)  Insns   (DIFF)  States (A)  States (B)  States (DIFF)
------------------------  ---------  ---------  --------------  ----------  ----------  -------------
ned_imex_be_tclass               52         57     +5 (+9.62%)           5           6   +1 (+20.00%)
ned_imex_be_tclass               52         57     +5 (+9.62%)           5           6   +1 (+20.00%)
ned_skop_auto_flowlabel         523        526     +3 (+0.57%)          39          40    +1 (+2.56%)
ned_skop_mss                    289        292     +3 (+1.04%)          20          20    +0 (+0.00%)
ned_skopt_bet_classifier         78         82     +4 (+5.13%)           8           8    +0 (+0.00%)
dctcp_update_alpha              252        320   +68 (+26.98%)          21          27   +6 (+28.57%)
dctcp_update_alpha              252        320   +68 (+26.98%)          21          27   +6 (+28.57%)
ned_ts_func                     119        126     +7 (+5.88%)           6           7   +1 (+16.67%)
tw_egress                      1119       1128     +9 (+0.80%)          95          96    +1 (+1.05%)
tw_ingress                     1128       1137     +9 (+0.80%)          95          96    +1 (+1.05%)
tw_tproxy_router               4380       4465    +85 (+1.94%)         114         118    +4 (+3.51%)
tw_tproxy_router4              3093       3170    +77 (+2.49%)          83          88    +5 (+6.02%)
ttls_tc_ingress               34656      35717  +1061 (+3.06%)         936         970   +34 (+3.63%)
tw_twfw_egress               222327     222338    +11 (+0.00%)       10563       10564    +1 (+0.01%)
tw_twfw_ingress               78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_tc_eg                222839     222859    +20 (+0.01%)       10584       10585    +1 (+0.01%)
tw_twfw_tc_in                 78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_egress                 8080       8085     +5 (+0.06%)         456         456    +0 (+0.00%)
tw_twfw_ingress                8053       8056     +3 (+0.04%)         454         454    +0 (+0.00%)
tw_twfw_tc_eg                  8154       8174    +20 (+0.25%)         456         457    +1 (+0.22%)
tw_twfw_tc_in                  8060       8063     +3 (+0.04%)         455         455    +0 (+0.00%)
tw_twfw_egress               222327     222338    +11 (+0.00%)       10563       10564    +1 (+0.01%)
tw_twfw_ingress               78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_tc_eg                222839     222859    +20 (+0.01%)       10584       10585    +1 (+0.01%)
tw_twfw_tc_in                 78295      78299     +4 (+0.01%)        3825        3826    +1 (+0.03%)
tw_twfw_egress                 8080       8085     +5 (+0.06%)         456         456    +0 (+0.00%)
tw_twfw_ingress                8053       8056     +3 (+0.04%)         454         454    +0 (+0.00%)
tw_twfw_tc_eg                  8154       8174    +20 (+0.25%)         456         457    +1 (+0.22%)
tw_twfw_tc_in                  8060       8063     +3 (+0.04%)         455         455    +0 (+0.00%)

Looking into rbtree_search, the reason for such increase is that the verifier has to explore the main loop shown below for one more iteration until state pruning decides the current state is safe.

long rbtree_search(void *ctx)

{
        ...
        bpf_spin_lock(&glock0);
        rb_n = bpf_rbtree_root(&groot0);
        while (can_loop) {
                if (!rb_n) {
                        bpf_spin_unlock(&glock0);
                        return __LINE__;
                }

                n = rb_entry(rb_n, struct node_data, r0);
                if (lookup_key == n->key0)
                        break;
                if (nr_gc < NR_NODES)
                        gc_ns[nr_gc++] = rb_n;
                if (lookup_key < n->key0)
                        rb_n = bpf_rbtree_left(&groot0, rb_n);
                else
                        rb_n = bpf_rbtree_right(&groot0, rb_n);
        }
        ...
}

Below is what the verifier sees at the start of each iteration (65: may_goto) after preserving id of rb_n. Without id of rb_n, the verifier stops exploring the loop at iter 16.

           rb_n  gc_ns[15]

iter 15 257 257

iter 16 290 257 rb_n: idmap add 257->290

                        gc_ns[15]: check 257 != 290 --> state not equal
Quoted 8 lines

iter 17 325 257 rb_n: idmap add 290->325

                        gc_ns[15]: idmap add 257->257 --> state safe
---
 kernel/bpf/verifier.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

totally seems worth doing

Quoted 27 lines
kernel/bpf/verifier.c +4 -10
index 93003a2a96b0..0313b7d5f6c9 100644
@@ -15886,15 +15886,10 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,
 
                mark_ptr_not_null_reg(reg);
 
-               if (!reg_may_point_to_spin_lock(reg)) {
-                       /* For not-NULL ptr, reg->ref_obj_id will be reset
-                        * in release_reference().
-                        *
-                        * reg->id is still used by spin_lock ptr. Other
-                        * than spin_lock ptr type, reg->id can be reset.
-                        */
-                       reg->id = 0;
-               }
+               /*
+                * reg->id is preserved for object relationship tracking
+                * and spin_lock lock state tracking
+                */
        }
 }
 
--

2.52.0

author
#17 lore raw

PATCH v3 4/9 bpf: Refactor object relationship tracking and fix dynptr UAF bug

In reply to Amery Hung

Refactor object relationship tracking in the verifier and fix a dynptr use-after-free bug where file/skb dynptrs are not invalidated when the parent referenced object is freed.

Add parent_id to bpf_reg_state to precisely track child-parent relationships. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

When calling dynptr constructors (i.e., process_dynptr_func() with MEM_UNINIT argument), track the parent's id if the parent is a referenced object. This only applies to file dynptr and skb dynptr, so only pass parent reg->id to kfunc constructors.

For release_reference(), invalidating an object now also invalidates all descendants by traversing the object tree. This is done using stack-based DFS to avoid recursive call chains of release_reference() -> unmark_stack_slots_dynptr() -> release_reference(). Referenced objects encountered during tree traversal cannot be indirectly released. They require an explicit helper/kfunc call to release the acquired resources.

While the new design changes how object relationships are tracked in the verifier, it does not change the verifier's behavior. Here is the implication for dynptr, pointer casting, and owning/non-owning references:

Dynptr:

When initializing a dynptr, referenced dynptrs acquire a reference for ref_obj_id. If the dynptr has a referenced parent, parent_id tracks the parent's id. When cloning, ref_obj_id and parent_id are copied from the original. Releasing a referenced dynptr via release_reference(ref_obj_id) invalidates all clones and derived slices. For non-referenced dynptrs, only the specific dynptr and its children are invalidated.

Pointer casting:

Referenced socket pointers and their casted counterparts share the same lifetime but have different nullness — they have different id but the same ref_obj_id.

Owning to non-owning reference conversion:

After converting owning to non-owning by clearing ref_obj_id (e.g., object(id=1, ref_obj_id=1) -> object(id=1, ref_obj_id=0)), the verifier only needs to release the reference state, so it calls release_reference_nomark() instead of release_reference().

Note that the error message "reference has not been acquired before" in the helper and kfunc release paths is removed. This message was already unreachable. The verifier only calls release_reference() after confirming meta.ref_obj_id is valid, so the condition could never trigger in practice (no selftest exercises it either). With the refactor, release_reference() can now be called with non-acquired ids and have different error conditions. Report directly in release_reference() instead.

Fixes 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
---
 include/linux/bpf_verifier.h |  22 ++-
 kernel/bpf/log.c             |   4 +-
 kernel/bpf/states.c          |   9 +-
 kernel/bpf/verifier.c        | 264 +++++++++++++++++------------------
 4 files changed, 152 insertions(+), 147 deletions(-)
include/linux/bpf_verifier.h +18 -4
index dc0cff59246d..1314299c3763 100644
@@ -65,7 +65,6 @@ struct bpf_reg_state {
 
 		struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
 			u32 mem_size;
-			u32 dynptr_id; /* for dynptr slices */
 		};
 
 		/* For dynptr stack slots */
@@ -193,6 +192,13 @@ struct bpf_reg_state {
 	 * allowed and has the same effect as bpf_sk_release(sk).
 	 */
 	u32 ref_obj_id;
+	/* Tracks the parent object this register was derived from.
+	 * Used for cascading invalidation: when the parent object is
+	 * released or invalidated, all registers with matching parent_id
+	 * are also invalidated. For example, a slice from bpf_dynptr_data()
+	 * gets parent_id set to the dynptr's id.
+	 */
+	u32 parent_id;
 	/* Inside the callee two registers can be both PTR_TO_STACK like
 	 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
 	 * while another to the caller's stack. To differentiate them 'frameno'
@@ -508,7 +514,7 @@ struct bpf_verifier_state {
 	     iter < frame->allocated_stack / BPF_REG_SIZE;		\
 	     iter++, reg = bpf_get_spilled_reg(iter, frame, mask))
 
-#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __mask, __expr)   \
+#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __stack, __mask, __expr)   \
 	({                                                               \
 		struct bpf_verifier_state *___vstate = __vst;            \
 		int ___i, ___j;                                          \
@@ -516,6 +522,7 @@ struct bpf_verifier_state {
 			struct bpf_reg_state *___regs;                   \
 			__state = ___vstate->frame[___i];                \
 			___regs = __state->regs;                         \
+			__stack = NULL;                                  \
 			for (___j = 0; ___j < MAX_BPF_REG; ___j++) {     \
 				__reg = &___regs[___j];                  \
 				(void)(__expr);                          \
@@ -523,14 +530,19 @@ struct bpf_verifier_state {
 			bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
 				if (!__reg)                              \
 					continue;                        \
+				__stack = &__state->stack[___j];         \
 				(void)(__expr);                          \
 			}                                                \
 		}                                                        \
 	})
 
 /* Invoke __expr over regsiters in __vst, setting __state and __reg */
-#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \
-	bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, 1 << STACK_SPILL, __expr)
+#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr)		\
+	({									\
+		struct bpf_stack_state * ___stack;                        	\
+		bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, ___stack,\
+						1 << STACK_SPILL, __expr);	\
+	})
 
 /* linked list of verifier states used to prune search */
 struct bpf_verifier_state_list {
@@ -1323,6 +1335,7 @@ struct bpf_dynptr_desc {
 	enum bpf_dynptr_type type;
 	u32 id;
 	u32 ref_obj_id;
+	u32 parent_id;
 };
 
 struct bpf_kfunc_call_arg_meta {
@@ -1334,6 +1347,7 @@ struct bpf_kfunc_call_arg_meta {
 	const char *func_name;
 	/* Out parameters */
 	u32 ref_obj_id;
+	u32 id;
 	u8 release_regno;
 	bool r0_rdonly;
 	u32 ret_btf_id;
kernel/bpf/log.c +2 -2
index 011e4ec25acd..d8dd372e45cd 100644
@@ -667,6 +667,8 @@ static void print_reg_state(struct bpf_verifier_env *env,
 		verbose(env, "%+d", reg->delta);
 	if (reg->ref_obj_id)
 		verbose_a("ref_obj_id=%d", reg->ref_obj_id);
+	if (reg->parent_id)
+		verbose_a("parent_id=%d", reg->parent_id);
 	if (type_is_non_owning_ref(reg->type))
 		verbose_a("%s", "non_own_ref");
 	if (type_is_map_ptr(t)) {
@@ -770,8 +772,6 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
 				verbose_a("id=%d", reg->id);
 			if (reg->ref_obj_id)
 				verbose_a("ref_id=%d", reg->ref_obj_id);
-			if (reg->dynptr_id)
-				verbose_a("dynptr_id=%d", reg->dynptr_id);
 			verbose(env, ")");
 			break;
 		case STACK_ITER:
kernel/bpf/states.c +6 -3
index 8478d2c6ed5b..72bd3bcda5fb 100644
@@ -494,7 +494,8 @@ static bool regs_exact(const struct bpf_reg_state *rold,
 {
 	return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
 	       check_ids(rold->id, rcur->id, idmap) &&
-	       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+	       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+	       check_ids(rold->parent_id, rcur->parent_id, idmap);
 }
 
 enum exact_level {
@@ -619,7 +620,8 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
 		       range_within(rold, rcur) &&
 		       tnum_in(rold->var_off, rcur->var_off) &&
 		       check_ids(rold->id, rcur->id, idmap) &&
-		       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+		       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+		       check_ids(rold->parent_id, rcur->parent_id, idmap);
 	case PTR_TO_PACKET_META:
 	case PTR_TO_PACKET:
 		/* We must have at least as much range as the old ptr
@@ -799,7 +801,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
 			cur_reg = &cur->stack[spi].spilled_ptr;
 			if (old_reg->dynptr.type != cur_reg->dynptr.type ||
 			    old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
-			    !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap))
+			    !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap) ||
+			    !check_ids(old_reg->parent_id, cur_reg->parent_id, idmap))
 				return false;
 			break;
 		case STACK_ITER:
kernel/bpf/verifier.c +126 -138

491 lines not on this page · raw message

Signature · 1 line
2.52.0
#18 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

Quoted 286 lines · Amery Hung

Refactor object relationship tracking in the verifier and fix a dynptr use-after-free bug where file/skb dynptrs are not invalidated when the parent referenced object is freed.

Add parent_id to bpf_reg_state to precisely track child-parent relationships. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

When calling dynptr constructors (i.e., process_dynptr_func() with MEM_UNINIT argument), track the parent's id if the parent is a referenced object. This only applies to file dynptr and skb dynptr, so only pass parent reg->id to kfunc constructors.

For release_reference(), invalidating an object now also invalidates all descendants by traversing the object tree. This is done using stack-based DFS to avoid recursive call chains of release_reference() -> unmark_stack_slots_dynptr() -> release_reference(). Referenced objects encountered during tree traversal cannot be indirectly released. They require an explicit helper/kfunc call to release the acquired resources.

While the new design changes how object relationships are tracked in the verifier, it does not change the verifier's behavior. Here is the implication for dynptr, pointer casting, and owning/non-owning references:

Dynptr:

When initializing a dynptr, referenced dynptrs acquire a reference for ref_obj_id. If the dynptr has a referenced parent, parent_id tracks the parent's id. When cloning, ref_obj_id and parent_id are copied from the original. Releasing a referenced dynptr via release_reference(ref_obj_id) invalidates all clones and derived slices. For non-referenced dynptrs, only the specific dynptr and its children are invalidated.

Pointer casting:

Referenced socket pointers and their casted counterparts share the same lifetime but have different nullness — they have different id but the same ref_obj_id.

Owning to non-owning reference conversion:

After converting owning to non-owning by clearing ref_obj_id (e.g., object(id=1, ref_obj_id=1) -> object(id=1, ref_obj_id=0)), the verifier only needs to release the reference state, so it calls release_reference_nomark() instead of release_reference().

Note that the error message "reference has not been acquired before" in the helper and kfunc release paths is removed. This message was already unreachable. The verifier only calls release_reference() after confirming meta.ref_obj_id is valid, so the condition could never trigger in practice (no selftest exercises it either). With the refactor, release_reference() can now be called with non-acquired ids and have different error conditions. Report directly in release_reference() instead.

Fixes 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
---
  include/linux/bpf_verifier.h |  22 ++-
  kernel/bpf/log.c             |   4 +-
  kernel/bpf/states.c          |   9 +-
  kernel/bpf/verifier.c        | 264 +++++++++++++++++------------------
  4 files changed, 152 insertions(+), 147 deletions(-)
include/linux/bpf_verifier.h +18 -4
index dc0cff59246d..1314299c3763 100644
@@ -65,7 +65,6 @@ struct bpf_reg_state {
 
  		struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
  			u32 mem_size;
-			u32 dynptr_id; /* for dynptr slices */
  		};
 
  		/* For dynptr stack slots */
@@ -193,6 +192,13 @@ struct bpf_reg_state {
  	 * allowed and has the same effect as bpf_sk_release(sk).
  	 */
  	u32 ref_obj_id;
+	/* Tracks the parent object this register was derived from.
+	 * Used for cascading invalidation: when the parent object is
+	 * released or invalidated, all registers with matching parent_id
+	 * are also invalidated. For example, a slice from bpf_dynptr_data()
+	 * gets parent_id set to the dynptr's id.
+	 */
+	u32 parent_id;
  	/* Inside the callee two registers can be both PTR_TO_STACK like
  	 * R1=fp-8 and R2=fp-8, but one of them points to this function stack
  	 * while another to the caller's stack. To differentiate them 'frameno'
@@ -508,7 +514,7 @@ struct bpf_verifier_state {
  	     iter < frame->allocated_stack / BPF_REG_SIZE;		\
  	     iter++, reg = bpf_get_spilled_reg(iter, frame, mask))
 
-#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __mask, __expr)   \
+#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __stack, __mask, __expr)   \
  	({                                                               \
  		struct bpf_verifier_state *___vstate = __vst;            \
  		int ___i, ___j;                                          \
@@ -516,6 +522,7 @@ struct bpf_verifier_state {
  			struct bpf_reg_state *___regs;                   \
  			__state = ___vstate->frame[___i];                \
  			___regs = __state->regs;                         \
+			__stack = NULL;                                  \
  			for (___j = 0; ___j < MAX_BPF_REG; ___j++) {     \
  				__reg = &___regs[___j];                  \
  				(void)(__expr);                          \
@@ -523,14 +530,19 @@ struct bpf_verifier_state {
  			bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
  				if (!__reg)                              \
  					continue;                        \
+				__stack = &__state->stack[___j];         \
  				(void)(__expr);                          \
  			}                                                \
  		}                                                        \
  	})
 
  /* Invoke __expr over regsiters in __vst, setting __state and __reg */
-#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \
-	bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, 1 << STACK_SPILL, __expr)
+#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr)		\
+	({									\
+		struct bpf_stack_state * ___stack;                        	\
+		bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, ___stack,\
+						1 << STACK_SPILL, __expr);	\
+	})
 
  /* linked list of verifier states used to prune search */
  struct bpf_verifier_state_list {
@@ -1323,6 +1335,7 @@ struct bpf_dynptr_desc {
  	enum bpf_dynptr_type type;
  	u32 id;
  	u32 ref_obj_id;
+	u32 parent_id;
  };
 
  struct bpf_kfunc_call_arg_meta {
@@ -1334,6 +1347,7 @@ struct bpf_kfunc_call_arg_meta {
  	const char *func_name;
  	/* Out parameters */
  	u32 ref_obj_id;
+	u32 id;
  	u8 release_regno;
  	bool r0_rdonly;
  	u32 ret_btf_id;
kernel/bpf/log.c +2 -2
index 011e4ec25acd..d8dd372e45cd 100644
@@ -667,6 +667,8 @@ static void print_reg_state(struct bpf_verifier_env *env,
  		verbose(env, "%+d", reg->delta);
  	if (reg->ref_obj_id)
  		verbose_a("ref_obj_id=%d", reg->ref_obj_id);
+	if (reg->parent_id)
+		verbose_a("parent_id=%d", reg->parent_id);
  	if (type_is_non_owning_ref(reg->type))
  		verbose_a("%s", "non_own_ref");
  	if (type_is_map_ptr(t)) {
@@ -770,8 +772,6 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
  				verbose_a("id=%d", reg->id);
  			if (reg->ref_obj_id)
  				verbose_a("ref_id=%d", reg->ref_obj_id);
-			if (reg->dynptr_id)
-				verbose_a("dynptr_id=%d", reg->dynptr_id);
  			verbose(env, ")");
  			break;
  		case STACK_ITER:
kernel/bpf/states.c +6 -3
index 8478d2c6ed5b..72bd3bcda5fb 100644
@@ -494,7 +494,8 @@ static bool regs_exact(const struct bpf_reg_state *rold,
  {
  	return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
  	       check_ids(rold->id, rcur->id, idmap) &&
-	       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+	       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+	       check_ids(rold->parent_id, rcur->parent_id, idmap);
  }
 
  enum exact_level {
@@ -619,7 +620,8 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
  		       range_within(rold, rcur) &&
  		       tnum_in(rold->var_off, rcur->var_off) &&
  		       check_ids(rold->id, rcur->id, idmap) &&
-		       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+		       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+		       check_ids(rold->parent_id, rcur->parent_id, idmap);
  	case PTR_TO_PACKET_META:
  	case PTR_TO_PACKET:
  		/* We must have at least as much range as the old ptr
@@ -799,7 +801,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
  			cur_reg = &cur->stack[spi].spilled_ptr;
  			if (old_reg->dynptr.type != cur_reg->dynptr.type ||
  			    old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
-			    !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap))
+			    !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap) ||
+			    !check_ids(old_reg->parent_id, cur_reg->parent_id, idmap))
  				return false;
  			break;
  		case STACK_ITER:
kernel/bpf/verifier.c +16 -19
index 0313b7d5f6c9..908a3af0e7c4 100644
@@ -201,7 +201,7 @@ struct bpf_verifier_stack_elem {
 
  static int acquire_reference(struct bpf_verifier_env *env, int insn_idx);
  static int release_reference_nomark(struct bpf_verifier_state *state, int ref_obj_id);
-static int release_reference(struct bpf_verifier_env *env, int ref_obj_id);
+static int release_reference(struct bpf_verifier_env *env, int id);
  static void invalidate_non_owning_refs(struct bpf_verifier_env *env);
  static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env);
  static int ref_set_non_owning(struct bpf_verifier_env *env,
@@ -241,6 +241,7 @@ struct bpf_call_arg_meta {
  	int mem_size;
  	u64 msize_max_value;
  	int ref_obj_id;
+	u32 id;
  	int func_id;
  	struct btf *btf;
  	u32 btf_id;
@@ -603,14 +604,14 @@ static enum bpf_type_flag get_dynptr_type_flag(enum bpf_dynptr_type type)
  	}
  }
 
-static bool dynptr_type_refcounted(enum bpf_dynptr_type type)
+static bool dynptr_type_referenced(enum bpf_dynptr_type type)
  {
  	return type == BPF_DYNPTR_TYPE_RINGBUF || type == BPF_DYNPTR_TYPE_FILE;
  }
 
  static void __mark_dynptr_reg(struct bpf_reg_state *reg,
  			      enum bpf_dynptr_type type,
-			      bool first_slot, int dynptr_id);
+			      bool first_slot, int id);
 
 
  static void mark_dynptr_stack_regs(struct bpf_verifier_env *env,
@@ -635,11 +636,12 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
  				        struct bpf_func_state *state, int spi);
 
  static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
-				   enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id)
+				   enum bpf_arg_type arg_type, int insn_idx, int parent_id,
+				   struct bpf_dynptr_desc *dynptr)
  {
  	struct bpf_func_state *state = bpf_func(env, reg);
+	int spi, i, err, ref_obj_id = 0;
  	enum bpf_dynptr_type type;
-	int spi, i, err;
 
  	spi = dynptr_get_spi(env, reg);
  	if (spi < 0)
@@ -673,82 +675,56 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
  	mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
  			       &state->stack[spi - 1].spilled_ptr, type);
 
-	if (dynptr_type_refcounted(type)) {
-		/* The id is used to track proper releasing */
-		int id;
-
-		if (clone_ref_obj_id)
-			id = clone_ref_obj_id;
-		else
-			id = acquire_reference(env, insn_idx);
-
-		if (id < 0)
-			return id;
-
-		state->stack[spi].spilled_ptr.ref_obj_id = id;
-		state->stack[spi - 1].spilled_ptr.ref_obj_id = id;
+	if (dynptr->type == BPF_DYNPTR_TYPE_INVALID) { /* dynptr constructors */
+		if (dynptr_type_referenced(type)) {
+			ref_obj_id = acquire_reference(env, insn_idx);
+			if (ref_obj_id < 0)
+				return ref_obj_id;
+		}
+	} else { /* bpf_dynptr_clone() */
+		ref_obj_id = dynptr->ref_obj_id;
+		parent_id = dynptr->parent_id;
  	}
 

kernel/bpf/verifier.c static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_ · line 697

Quoted 10 lines
kernel/bpf/verifier.c +6 -1
+	state->stack[spi].spilled_ptr.ref_obj_id = ref_obj_id;
+	state->stack[spi - 1].spilled_ptr.ref_obj_id = ref_obj_id;
+	state->stack[spi].spilled_ptr.parent_id = parent_id;
+	state->stack[spi - 1].spilled_ptr.parent_id = parent_id;
+
  	return 0;
  }
 
-static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi)
+static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state,

it looks like state is no longer needed.

Quoted 173 lines
kernel/bpf/verifier.c +23 -73
+			      struct bpf_stack_state *stack)
  {
  	int i;
 
  	for (i = 0; i < BPF_REG_SIZE; i++) {
-		state->stack[spi].slot_type[i] = STACK_INVALID;
-		state->stack[spi - 1].slot_type[i] = STACK_INVALID;
+		stack[0].slot_type[i] = STACK_INVALID;
+		stack[1].slot_type[i] = STACK_INVALID;
  	}
 
-	bpf_mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
-	bpf_mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
+	bpf_mark_reg_not_init(env, &stack[0].spilled_ptr);
+	bpf_mark_reg_not_init(env, &stack[1].spilled_ptr);
  }
 
  static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
  {
  	struct bpf_func_state *state = bpf_func(env, reg);
-	int spi, ref_obj_id, i;
+	int spi;
 
  	spi = dynptr_get_spi(env, reg);
  	if (spi < 0)
  		return spi;
 
-	if (!dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
-		invalidate_dynptr(env, state, spi);
-		return 0;
-	}
-
-	ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
-
-	/* If the dynptr has a ref_obj_id, then we need to invalidate
-	 * two things:
-	 *
-	 * 1) Any dynptrs with a matching ref_obj_id (clones)
-	 * 2) Any slices derived from this dynptr.
+	/*
+	 * For referenced dynptr, the clones share the same ref_obj_id and will be
+	 * invalidated too. For non-referenced dynptr, only the dynptr and slices
+	 * derived from it will be invalidated.
  	 */
-
-	/* Invalidate any slices associated with this dynptr */
-	WARN_ON_ONCE(release_reference(env, ref_obj_id));
-
-	/* Invalidate any dynptr clones */
-	for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) {
-		if (state->stack[i].spilled_ptr.ref_obj_id != ref_obj_id)
-			continue;
-
-		/* it should always be the case that if the ref obj id
-		 * matches then the stack slot also belongs to a
-		 * dynptr
-		 */
-		if (state->stack[i].slot_type[0] != STACK_DYNPTR) {
-			verifier_bug(env, "misconfigured ref_obj_id");
-			return -EFAULT;
-		}
-		if (state->stack[i].spilled_ptr.dynptr.first_slot)
-			invalidate_dynptr(env, state, i);
-	}
-
-	return 0;
+	reg = &state->stack[spi].spilled_ptr;
+	return release_reference(env, dynptr_type_referenced(reg->dynptr.type) ?
+				      reg->ref_obj_id : reg->id);
  }
 
  static void __mark_reg_unknown(const struct bpf_verifier_env *env,
@@ -765,10 +741,6 @@ static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_
  static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
  				        struct bpf_func_state *state, int spi)
  {
-	struct bpf_func_state *fstate;
-	struct bpf_reg_state *dreg;
-	int i, dynptr_id;
-
  	/* We always ensure that STACK_DYNPTR is never set partially,
  	 * hence just checking for slot_type[0] is enough. This is
  	 * different for STACK_SPILL, where it may be only set for
@@ -781,9 +753,9 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
  	if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
  		spi = spi + 1;
 
-	if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+	if (dynptr_type_referenced(state->stack[spi].spilled_ptr.dynptr.type)) {
  		int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
-		int ref_cnt = 0;
+		int i, ref_cnt = 0;
 
  		/*
  		 * A referenced dynptr can be overwritten only if there is at
@@ -808,29 +780,8 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
  	mark_stack_slot_scratched(env, spi);
  	mark_stack_slot_scratched(env, spi - 1);
 
-	/* Writing partially to one dynptr stack slot destroys both. */
-	for (i = 0; i < BPF_REG_SIZE; i++) {
-		state->stack[spi].slot_type[i] = STACK_INVALID;
-		state->stack[spi - 1].slot_type[i] = STACK_INVALID;
-	}
-
-	dynptr_id = state->stack[spi].spilled_ptr.id;
-	/* Invalidate any slices associated with this dynptr */
-	bpf_for_each_reg_in_vstate(env->cur_state, fstate, dreg, ({
-		/* Dynptr slices are only PTR_TO_MEM_OR_NULL and PTR_TO_MEM */
-		if (dreg->type != (PTR_TO_MEM | PTR_MAYBE_NULL) && dreg->type != PTR_TO_MEM)
-			continue;
-		if (dreg->dynptr_id == dynptr_id)
-			mark_reg_invalid(env, dreg);
-	}));
-
-	/* Do not release reference state, we are destroying dynptr on stack,
-	 * not using some helper to release it. Just reset register.
-	 */
-	bpf_mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
-	bpf_mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
-
-	return 0;
+	/* Invalidate the dynptr and any derived slices */
+	return release_reference(env, state->stack[spi].spilled_ptr.id);
  }
 
  static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
@@ -1449,15 +1400,15 @@ static void release_reference_state(struct bpf_verifier_state *state, int idx)
  	return;
  }
 
-static bool find_reference_state(struct bpf_verifier_state *state, int ptr_id)
+static struct bpf_reference_state *find_reference_state(struct bpf_verifier_state *state, int ptr_id)
  {
  	int i;
 
  	for (i = 0; i < state->acquired_refs; i++)
  		if (state->refs[i].id == ptr_id)
-			return true;
+			return &state->refs[i];
 
-	return false;
+	return NULL;
  }
 
  static int release_lock_state(struct bpf_verifier_state *state, int type, int id, void *ptr)
@@ -1764,6 +1715,7 @@ static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
  	       offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
  	reg->id = 0;
  	reg->ref_obj_id = 0;
+	reg->parent_id = 0;
  	___mark_reg_known(reg, imm);
  }
 
@@ -1801,7 +1753,7 @@ static void mark_reg_known_zero(struct bpf_verifier_env *env,
  }
 
  static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type type,
-			      bool first_slot, int dynptr_id)
+			      bool first_slot, int id)
  {
  	/* reg->type has no meaning for STACK_DYNPTR, but when we set reg for
  	 * callback arguments, it does need to be CONST_PTR_TO_DYNPTR, so simply
@@ -1810,7 +1762,7 @@ static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type ty
  	__mark_reg_known_zero(reg);
  	reg->type = CONST_PTR_TO_DYNPTR;
  	/* Give each dynptr a unique id to uniquely associate slices to it. */
-	reg->id = dynptr_id;
+	reg->id = id;
  	reg->dynptr.type = type;
  	reg->dynptr.first_slot = first_slot;
  }

kernel/bpf/verifier.c void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg) · line 2406

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -2451,6 +2403,7 @@ void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg)
  	reg->type = SCALAR_VALUE;
  	reg->id = 0;
  	reg->ref_obj_id = 0;
+	reg->parent_id = 0;

nit: it looks like we can do memset(0) on the entire reg and then set fields that need to be non-zero: type, var_off + __mark_reg_unbounded().

Quoted 60 lines
kernel/bpf/verifier.c +9 -9
  	reg->var_off = tnum_unknown;
  	reg->frameno = 0;
  	reg->precise = false;
@@ -7427,7 +7380,7 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
   * and checked dynamically during runtime.
   */
  static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
-			       enum bpf_arg_type arg_type, int clone_ref_obj_id,
+			       enum bpf_arg_type arg_type, int parent_id,
  			       struct bpf_dynptr_desc *dynptr)
  {
  	struct bpf_reg_state *reg = reg_state(env, regno);
@@ -7470,7 +7423,8 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
  				return err;
  		}
 
-		err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, clone_ref_obj_id);
+		err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, parent_id,
+					      dynptr);
  	} else /* OBJ_RELEASE and None case from above */ {
  		/* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */
  		if (reg->type == CONST_PTR_TO_DYNPTR && (arg_type & OBJ_RELEASE)) {
@@ -7507,6 +7461,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
  			dynptr->id = reg->id;
  			dynptr->type = reg->dynptr.type;
  			dynptr->ref_obj_id = reg->ref_obj_id;
+			dynptr->parent_id = reg->parent_id;
  		}
  	}
  	return err;
@@ -8461,7 +8416,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
  			 */
  			if (reg->type == PTR_TO_STACK) {
  				spi = dynptr_get_spi(env, reg);
-				if (spi < 0 || !state->stack[spi].spilled_ptr.ref_obj_id) {
+				if (spi < 0 || !state->stack[spi].spilled_ptr.id) {
  					verbose(env, "arg %d is an unacquired reference\n", regno);
  					return -EINVAL;
  				}
@@ -8489,6 +8444,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
  			return -EACCES;
  		}
  		meta->ref_obj_id = reg->ref_obj_id;
+		meta->id = reg->id;
  	}
 
  	switch (base_type(arg_type)) {
@@ -9111,26 +9067,75 @@ static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob
  	return -EINVAL;
  }
 
-/* The pointer with the specified id has released its reference to kernel
- * resources. Identify all copies of the same pointer and clear the reference.
- *
- * This is the release function corresponding to acquire_reference(). Idempotent.
- */
-static int release_reference(struct bpf_verifier_env *env, int ref_obj_id)
+static int idstack_push(struct bpf_idmap *idmap, u32 id)
+{
+	int i;

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9082

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+
+	if (!id)
+		return 0;
+
+	for (i = 0; i < idmap->cnt; i++)
+		if (idmap->map[i].old == id)
+			return 0;
+
+	if (WARN_ON_ONCE(idmap->cnt >= BPF_ID_MAP_SIZE))
+		return -EFAULT;

It feels like this check belongs above, maybe the first thing to do.

Quoted 34 lines
kernel/bpf/verifier.c +21 -7
+
+	idmap->map[idmap->cnt++].old = id;
+	return 0;
+}
+
+static int idstack_pop(struct bpf_idmap *idmap)
  {
+	if (!idmap->cnt)
+		return 0;
+
+	return idmap->map[--idmap->cnt].old;
+}
+
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+	u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
  	struct bpf_verifier_state *vstate = env->cur_state;
+	struct bpf_idmap *idstack = &env->idmap_scratch;
+	struct bpf_stack_state *stack;
  	struct bpf_func_state *state;
  	struct bpf_reg_state *reg;
-	int err;
+	int root_id = id, err;
 
-	err = release_reference_nomark(vstate, ref_obj_id);
-	if (err)
-		return err;
+	idstack->cnt = 0;
+	idstack_push(idstack, id);
 
-	bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-		if (reg->ref_obj_id == ref_obj_id)
-			mark_reg_invalid(env, reg);

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9118

Quoted 10 lines
kernel/bpf/verifier.c +9 -1
-	}));
+	if (find_reference_state(vstate, id))
+		WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+	while ((id = idstack_pop(idstack))) {
+		bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+			if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+				continue;
+
+			if (reg->ref_obj_id && id != root_id) {

Does this line check that the only ref_obj_id we should see is either 0 or root_id? can we rewrite it as
if (reg->ref_obj_id && reg->ref_obj_id != root_id)

this is simpler, because id can also be reg->id/reg->parent_id, which is hard to reason what that means.

Quoted 142 lines
kernel/bpf/verifier.c +31 -28
+				struct bpf_reference_state *ref_state;
+
+				ref_state = find_reference_state(env->cur_state, reg->ref_obj_id);
+				verbose(env, "Unreleased reference id=%d alloc_insn=%d when releasing id=%d\n",
+					ref_state->id, ref_state->insn_idx, root_id);
+				return -EINVAL;
+			}
+
+			if (reg->id != id) {
+				err = idstack_push(idstack, reg->id);
+				if (err)
+					return err;
+			}
+
+			if (!stack || stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL)
+				mark_reg_invalid(env, reg);
+			else if (stack->slot_type[BPF_REG_SIZE - 1] == STACK_DYNPTR)
+				invalidate_dynptr(env, state, stack);
+		}));
+	}
 
  	return 0;
  }
@@ -10298,11 +10303,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  			 */
  			err = 0;
  		}
-		if (err) {
-			verbose(env, "func %s#%d reference has not been acquired before\n",
-				func_id_name(func_id), func_id);
+		if (err)
  			return err;
-		}
  	}
 
  	switch (func_id) {
@@ -10580,10 +10582,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
  		regs[BPF_REG_0].ref_obj_id = id;
  	}
 
-	if (func_id == BPF_FUNC_dynptr_data) {
-		regs[BPF_REG_0].dynptr_id = meta.dynptr.id;
-		regs[BPF_REG_0].ref_obj_id = meta.dynptr.ref_obj_id;
-	}
+	if (func_id == BPF_FUNC_dynptr_data)
+		regs[BPF_REG_0].parent_id = meta.dynptr.id;
 
  	err = do_refine_retval_range(env, regs, fn->ret_type, func_id, &meta);
  	if (err)
@@ -12009,6 +12009,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
  				return -EFAULT;
  			}
  			meta->ref_obj_id = reg->ref_obj_id;
+			meta->id = reg->id;
  			if (is_kfunc_release(meta))
  				meta->release_regno = regno;
  		}
@@ -12145,7 +12146,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
  		case KF_ARG_PTR_TO_DYNPTR:
  		{
  			enum bpf_arg_type dynptr_arg_type = ARG_PTR_TO_DYNPTR;
-			int clone_ref_obj_id = 0;
 
  			if (is_kfunc_arg_uninit(btf, &args[i]))
  				dynptr_arg_type |= MEM_UNINIT;
@@ -12171,15 +12171,10 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
  				}
 
  				dynptr_arg_type |= (unsigned int)get_dynptr_type_flag(parent_type);
-				clone_ref_obj_id = meta->dynptr.ref_obj_id;
-				if (dynptr_type_refcounted(parent_type) && !clone_ref_obj_id) {
-					verifier_bug(env, "missing ref obj id for parent of clone");
-					return -EFAULT;
-				}
  			}
 
-			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id,
-						  &meta->dynptr);
+			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type,
+						  meta->ref_obj_id ? meta->id : 0, &meta->dynptr);
  			if (ret < 0)
  				return ret;
  			break;
@@ -12813,12 +12808,7 @@ static int check_special_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_ca
  			verifier_bug(env, "no dynptr id");
  			return -EFAULT;
  		}
-		regs[BPF_REG_0].dynptr_id = meta->dynptr.id;
-
-		/* we don't need to set BPF_REG_0's ref obj id
-		 * because packet slices are not refcounted (see
-		 * dynptr_type_refcounted)
-		 */
+		regs[BPF_REG_0].parent_id = meta->dynptr.id;
  	} else {
  		return 0;
  	}
@@ -12953,6 +12943,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  	if (rcu_lock) {
  		env->cur_state->active_rcu_locks++;
  	} else if (rcu_unlock) {
+		struct bpf_stack_state *stack;
  		struct bpf_func_state *state;
  		struct bpf_reg_state *reg;
  		u32 clear_mask = (1 << STACK_SPILL) | (1 << STACK_ITER);
@@ -12962,7 +12953,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  			return -EINVAL;
  		}
  		if (--env->cur_state->active_rcu_locks == 0) {
-			bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, clear_mask, ({
+			bpf_for_each_reg_in_vstate_mask(env->cur_state, state, reg, stack, clear_mask, ({
  				if (reg->type & MEM_RCU) {
  					reg->type &= ~(MEM_RCU | PTR_MAYBE_NULL);
  					reg->type |= PTR_UNTRUSTED;
@@ -13005,9 +12996,6 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  			err = unmark_stack_slots_dynptr(env, reg);
  		} else {
  			err = release_reference(env, reg->ref_obj_id);
-			if (err)
-				verbose(env, "kfunc %s#%d reference has not been acquired before\n",
-					func_name, meta.func_id);
  		}
  		if (err)
  			return err;
@@ -13024,7 +13012,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
  			return err;
  		}
 
-		err = release_reference(env, release_ref_obj_id);
+		err = release_reference_nomark(env->cur_state, release_ref_obj_id);
  		if (err) {
  			verbose(env, "kfunc %s#%d reference has not been acquired before\n",
  				func_name, meta.func_id);
@@ -13114,7 +13102,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 
  			/* Ensures we don't access the memory after a release_reference() */
  			if (meta.ref_obj_id)
-				regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
+				regs[BPF_REG_0].parent_id = meta.ref_obj_id;
 
  			if (is_kfunc_rcu_protected(&meta))
  				regs[BPF_REG_0].type |= MEM_RCU;
author
#19 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

Quoted 298 lines · Mykyta Yatsenko
Quoted 294 lines · Amery Hung

Refactor object relationship tracking in the verifier and fix a dynptr use-after-free bug where file/skb dynptrs are not invalidated when the parent referenced object is freed.

Add parent_id to bpf_reg_state to precisely track child-parent relationships. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

When calling dynptr constructors (i.e., process_dynptr_func() with MEM_UNINIT argument), track the parent's id if the parent is a referenced object. This only applies to file dynptr and skb dynptr, so only pass parent reg->id to kfunc constructors.

For release_reference(), invalidating an object now also invalidates all descendants by traversing the object tree. This is done using stack-based DFS to avoid recursive call chains of release_reference() -> unmark_stack_slots_dynptr() -> release_reference(). Referenced objects encountered during tree traversal cannot be indirectly released. They require an explicit helper/kfunc call to release the acquired resources.

While the new design changes how object relationships are tracked in the verifier, it does not change the verifier's behavior. Here is the implication for dynptr, pointer casting, and owning/non-owning references:

Dynptr:

When initializing a dynptr, referenced dynptrs acquire a reference for ref_obj_id. If the dynptr has a referenced parent, parent_id tracks the parent's id. When cloning, ref_obj_id and parent_id are copied from the original. Releasing a referenced dynptr via release_reference(ref_obj_id) invalidates all clones and derived slices. For non-referenced dynptrs, only the specific dynptr and its children are invalidated.

Pointer casting:

Referenced socket pointers and their casted counterparts share the same lifetime but have different nullness — they have different id but the same ref_obj_id.

Owning to non-owning reference conversion:

After converting owning to non-owning by clearing ref_obj_id (e.g., object(id=1, ref_obj_id=1) -> object(id=1, ref_obj_id=0)), the verifier only needs to release the reference state, so it calls release_reference_nomark() instead of release_reference().

Note that the error message "reference has not been acquired before" in the helper and kfunc release paths is removed. This message was already unreachable. The verifier only calls release_reference() after confirming meta.ref_obj_id is valid, so the condition could never trigger in practice (no selftest exercises it either). With the refactor, release_reference() can now be called with non-acquired ids and have different error conditions. Report directly in release_reference() instead.

Fixes 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
---
  include/linux/bpf_verifier.h |  22 ++-
  kernel/bpf/log.c             |   4 +-
  kernel/bpf/states.c          |   9 +-
  kernel/bpf/verifier.c        | 264 +++++++++++++++++------------------
  4 files changed, 152 insertions(+), 147 deletions(-)
include/linux/bpf_verifier.h +18 -4
index dc0cff59246d..1314299c3763 100644
@@ -65,7 +65,6 @@ struct bpf_reg_state {
 
              struct { /* for PTR_TO_MEM | PTR_TO_MEM_OR_NULL */
                      u32 mem_size;
-                     u32 dynptr_id; /* for dynptr slices */
              };
 
              /* For dynptr stack slots */
@@ -193,6 +192,13 @@ struct bpf_reg_state {
       * allowed and has the same effect as bpf_sk_release(sk).
       */
      u32 ref_obj_id;
+     /* Tracks the parent object this register was derived from.
+      * Used for cascading invalidation: when the parent object is
+      * released or invalidated, all registers with matching parent_id
+      * are also invalidated. For example, a slice from bpf_dynptr_data()
+      * gets parent_id set to the dynptr's id.
+      */
+     u32 parent_id;
      /* Inside the callee two registers can be both PTR_TO_STACK like
       * R1=fp-8 and R2=fp-8, but one of them points to this function stack
       * while another to the caller's stack. To differentiate them 'frameno'
@@ -508,7 +514,7 @@ struct bpf_verifier_state {
           iter < frame->allocated_stack / BPF_REG_SIZE;              \
           iter++, reg = bpf_get_spilled_reg(iter, frame, mask))
 
-#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __mask, __expr)   \
+#define bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, __stack, __mask, __expr)   \
      ({                                                               \
              struct bpf_verifier_state *___vstate = __vst;            \
              int ___i, ___j;                                          \
@@ -516,6 +522,7 @@ struct bpf_verifier_state {
                      struct bpf_reg_state *___regs;                   \
                      __state = ___vstate->frame[___i];                \
                      ___regs = __state->regs;                         \
+                     __stack = NULL;                                  \
                      for (___j = 0; ___j < MAX_BPF_REG; ___j++) {     \
                              __reg = &___regs[___j];                  \
                              (void)(__expr);                          \
@@ -523,14 +530,19 @@ struct bpf_verifier_state {
                      bpf_for_each_spilled_reg(___j, __state, __reg, __mask) { \
                              if (!__reg)                              \
                                      continue;                        \
+                             __stack = &__state->stack[___j];         \
                              (void)(__expr);                          \
                      }                                                \
              }                                                        \
      })
 
  /* Invoke __expr over regsiters in __vst, setting __state and __reg */
-#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr) \
-     bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, 1 << STACK_SPILL, __expr)
+#define bpf_for_each_reg_in_vstate(__vst, __state, __reg, __expr)            \
+     ({                                                                      \
+             struct bpf_stack_state * ___stack;                              \
+             bpf_for_each_reg_in_vstate_mask(__vst, __state, __reg, ___stack,\
+                                             1 << STACK_SPILL, __expr);      \
+     })
 
  /* linked list of verifier states used to prune search */
  struct bpf_verifier_state_list {
@@ -1323,6 +1335,7 @@ struct bpf_dynptr_desc {
      enum bpf_dynptr_type type;
      u32 id;
      u32 ref_obj_id;
+     u32 parent_id;
  };
 
  struct bpf_kfunc_call_arg_meta {
@@ -1334,6 +1347,7 @@ struct bpf_kfunc_call_arg_meta {
      const char *func_name;
      /* Out parameters */
      u32 ref_obj_id;
+     u32 id;
      u8 release_regno;
      bool r0_rdonly;
      u32 ret_btf_id;
kernel/bpf/log.c +2 -2
index 011e4ec25acd..d8dd372e45cd 100644
@@ -667,6 +667,8 @@ static void print_reg_state(struct bpf_verifier_env *env,
              verbose(env, "%+d", reg->delta);
      if (reg->ref_obj_id)
              verbose_a("ref_obj_id=%d", reg->ref_obj_id);
+     if (reg->parent_id)
+             verbose_a("parent_id=%d", reg->parent_id);
      if (type_is_non_owning_ref(reg->type))
              verbose_a("%s", "non_own_ref");
      if (type_is_map_ptr(t)) {
@@ -770,8 +772,6 @@ void print_verifier_state(struct bpf_verifier_env *env, const struct bpf_verifie
                              verbose_a("id=%d", reg->id);
                      if (reg->ref_obj_id)
                              verbose_a("ref_id=%d", reg->ref_obj_id);
-                     if (reg->dynptr_id)
-                             verbose_a("dynptr_id=%d", reg->dynptr_id);
                      verbose(env, ")");
                      break;
              case STACK_ITER:
kernel/bpf/states.c +6 -3
index 8478d2c6ed5b..72bd3bcda5fb 100644
@@ -494,7 +494,8 @@ static bool regs_exact(const struct bpf_reg_state *rold,
  {
      return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
             check_ids(rold->id, rcur->id, idmap) &&
-            check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+            check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+            check_ids(rold->parent_id, rcur->parent_id, idmap);
  }
 
  enum exact_level {
@@ -619,7 +620,8 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
                     range_within(rold, rcur) &&
                     tnum_in(rold->var_off, rcur->var_off) &&
                     check_ids(rold->id, rcur->id, idmap) &&
-                    check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+                    check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+                    check_ids(rold->parent_id, rcur->parent_id, idmap);
      case PTR_TO_PACKET_META:
      case PTR_TO_PACKET:
              /* We must have at least as much range as the old ptr
@@ -799,7 +801,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
                      cur_reg = &cur->stack[spi].spilled_ptr;
                      if (old_reg->dynptr.type != cur_reg->dynptr.type ||
                          old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
-                         !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap))
+                         !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap) ||
+                         !check_ids(old_reg->parent_id, cur_reg->parent_id, idmap))
                              return false;
                      break;
              case STACK_ITER:
kernel/bpf/verifier.c +21 -19
index 0313b7d5f6c9..908a3af0e7c4 100644
@@ -201,7 +201,7 @@ struct bpf_verifier_stack_elem {
 
  static int acquire_reference(struct bpf_verifier_env *env, int insn_idx);
  static int release_reference_nomark(struct bpf_verifier_state *state, int ref_obj_id);
-static int release_reference(struct bpf_verifier_env *env, int ref_obj_id);
+static int release_reference(struct bpf_verifier_env *env, int id);
  static void invalidate_non_owning_refs(struct bpf_verifier_env *env);
  static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env);
  static int ref_set_non_owning(struct bpf_verifier_env *env,
@@ -241,6 +241,7 @@ struct bpf_call_arg_meta {
      int mem_size;
      u64 msize_max_value;
      int ref_obj_id;
+     u32 id;
      int func_id;
      struct btf *btf;
      u32 btf_id;
@@ -603,14 +604,14 @@ static enum bpf_type_flag get_dynptr_type_flag(enum bpf_dynptr_type type)
      }
  }
 
-static bool dynptr_type_refcounted(enum bpf_dynptr_type type)
+static bool dynptr_type_referenced(enum bpf_dynptr_type type)
  {
      return type == BPF_DYNPTR_TYPE_RINGBUF || type == BPF_DYNPTR_TYPE_FILE;
  }
 
  static void __mark_dynptr_reg(struct bpf_reg_state *reg,
                            enum bpf_dynptr_type type,
-                           bool first_slot, int dynptr_id);
+                           bool first_slot, int id);
 
 
  static void mark_dynptr_stack_regs(struct bpf_verifier_env *env,
@@ -635,11 +636,12 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
                                      struct bpf_func_state *state, int spi);
 
  static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
-                                enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id)
+                                enum bpf_arg_type arg_type, int insn_idx, int parent_id,
+                                struct bpf_dynptr_desc *dynptr)
  {
      struct bpf_func_state *state = bpf_func(env, reg);
+     int spi, i, err, ref_obj_id = 0;
      enum bpf_dynptr_type type;
-     int spi, i, err;
 
      spi = dynptr_get_spi(env, reg);
      if (spi < 0)
@@ -673,82 +675,56 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
      mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
                             &state->stack[spi - 1].spilled_ptr, type);
 
-     if (dynptr_type_refcounted(type)) {
-             /* The id is used to track proper releasing */
-             int id;
-
-             if (clone_ref_obj_id)
-                     id = clone_ref_obj_id;
-             else
-                     id = acquire_reference(env, insn_idx);
-
-             if (id < 0)
-                     return id;
-
-             state->stack[spi].spilled_ptr.ref_obj_id = id;
-             state->stack[spi - 1].spilled_ptr.ref_obj_id = id;
+     if (dynptr->type == BPF_DYNPTR_TYPE_INVALID) { /* dynptr constructors */
+             if (dynptr_type_referenced(type)) {
+                     ref_obj_id = acquire_reference(env, insn_idx);
+                     if (ref_obj_id < 0)
+                             return ref_obj_id;
+             }
+     } else { /* bpf_dynptr_clone() */
+             ref_obj_id = dynptr->ref_obj_id;
+             parent_id = dynptr->parent_id;
      }
 
+     state->stack[spi].spilled_ptr.ref_obj_id = ref_obj_id;
+     state->stack[spi - 1].spilled_ptr.ref_obj_id = ref_obj_id;
+     state->stack[spi].spilled_ptr.parent_id = parent_id;
+     state->stack[spi - 1].spilled_ptr.parent_id = parent_id;
+
      return 0;
  }
 
Quoted 4 lines

kernel/bpf/verifier.c static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_ · line 697

Quoted 2 lines
kernel/bpf/verifier.c +1 -1
-static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi)
+static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state,

it looks like state is no longer needed.

Ack.

Quoted 246 lines
Quoted 174 lines
kernel/bpf/verifier.c +23 -73
+                           struct bpf_stack_state *stack)
  {
      int i;
 
      for (i = 0; i < BPF_REG_SIZE; i++) {
-             state->stack[spi].slot_type[i] = STACK_INVALID;
-             state->stack[spi - 1].slot_type[i] = STACK_INVALID;
+             stack[0].slot_type[i] = STACK_INVALID;
+             stack[1].slot_type[i] = STACK_INVALID;
      }
 
-     bpf_mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
-     bpf_mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
+     bpf_mark_reg_not_init(env, &stack[0].spilled_ptr);
+     bpf_mark_reg_not_init(env, &stack[1].spilled_ptr);
  }
 
  static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
  {
      struct bpf_func_state *state = bpf_func(env, reg);
-     int spi, ref_obj_id, i;
+     int spi;
 
      spi = dynptr_get_spi(env, reg);
      if (spi < 0)
              return spi;
 
-     if (!dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
-             invalidate_dynptr(env, state, spi);
-             return 0;
-     }
-
-     ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
-
-     /* If the dynptr has a ref_obj_id, then we need to invalidate
-      * two things:
-      *
-      * 1) Any dynptrs with a matching ref_obj_id (clones)
-      * 2) Any slices derived from this dynptr.
+     /*
+      * For referenced dynptr, the clones share the same ref_obj_id and will be
+      * invalidated too. For non-referenced dynptr, only the dynptr and slices
+      * derived from it will be invalidated.
       */
-
-     /* Invalidate any slices associated with this dynptr */
-     WARN_ON_ONCE(release_reference(env, ref_obj_id));
-
-     /* Invalidate any dynptr clones */
-     for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) {
-             if (state->stack[i].spilled_ptr.ref_obj_id != ref_obj_id)
-                     continue;
-
-             /* it should always be the case that if the ref obj id
-              * matches then the stack slot also belongs to a
-              * dynptr
-              */
-             if (state->stack[i].slot_type[0] != STACK_DYNPTR) {
-                     verifier_bug(env, "misconfigured ref_obj_id");
-                     return -EFAULT;
-             }
-             if (state->stack[i].spilled_ptr.dynptr.first_slot)
-                     invalidate_dynptr(env, state, i);
-     }
-
-     return 0;
+     reg = &state->stack[spi].spilled_ptr;
+     return release_reference(env, dynptr_type_referenced(reg->dynptr.type) ?
+                                   reg->ref_obj_id : reg->id);
  }
 
  static void __mark_reg_unknown(const struct bpf_verifier_env *env,
@@ -765,10 +741,6 @@ static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_
  static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
                                      struct bpf_func_state *state, int spi)
  {
-     struct bpf_func_state *fstate;
-     struct bpf_reg_state *dreg;
-     int i, dynptr_id;
-
      /* We always ensure that STACK_DYNPTR is never set partially,
       * hence just checking for slot_type[0] is enough. This is
       * different for STACK_SPILL, where it may be only set for
@@ -781,9 +753,9 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
      if (!state->stack[spi].spilled_ptr.dynptr.first_slot)
              spi = spi + 1;
 
-     if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+     if (dynptr_type_referenced(state->stack[spi].spilled_ptr.dynptr.type)) {
              int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
-             int ref_cnt = 0;
+             int i, ref_cnt = 0;
 
              /*
               * A referenced dynptr can be overwritten only if there is at
@@ -808,29 +780,8 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
      mark_stack_slot_scratched(env, spi);
      mark_stack_slot_scratched(env, spi - 1);
 
-     /* Writing partially to one dynptr stack slot destroys both. */
-     for (i = 0; i < BPF_REG_SIZE; i++) {
-             state->stack[spi].slot_type[i] = STACK_INVALID;
-             state->stack[spi - 1].slot_type[i] = STACK_INVALID;
-     }
-
-     dynptr_id = state->stack[spi].spilled_ptr.id;
-     /* Invalidate any slices associated with this dynptr */
-     bpf_for_each_reg_in_vstate(env->cur_state, fstate, dreg, ({
-             /* Dynptr slices are only PTR_TO_MEM_OR_NULL and PTR_TO_MEM */
-             if (dreg->type != (PTR_TO_MEM | PTR_MAYBE_NULL) && dreg->type != PTR_TO_MEM)
-                     continue;
-             if (dreg->dynptr_id == dynptr_id)
-                     mark_reg_invalid(env, dreg);
-     }));
-
-     /* Do not release reference state, we are destroying dynptr on stack,
-      * not using some helper to release it. Just reset register.
-      */
-     bpf_mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
-     bpf_mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
-
-     return 0;
+     /* Invalidate the dynptr and any derived slices */
+     return release_reference(env, state->stack[spi].spilled_ptr.id);
  }
 
  static bool is_dynptr_reg_valid_uninit(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
@@ -1449,15 +1400,15 @@ static void release_reference_state(struct bpf_verifier_state *state, int idx)
      return;
  }
 
-static bool find_reference_state(struct bpf_verifier_state *state, int ptr_id)
+static struct bpf_reference_state *find_reference_state(struct bpf_verifier_state *state, int ptr_id)
  {
      int i;
 
      for (i = 0; i < state->acquired_refs; i++)
              if (state->refs[i].id == ptr_id)
-                     return true;
+                     return &state->refs[i];
 
-     return false;
+     return NULL;
  }
 
  static int release_lock_state(struct bpf_verifier_state *state, int type, int id, void *ptr)
@@ -1764,6 +1715,7 @@ static void __mark_reg_known(struct bpf_reg_state *reg, u64 imm)
             offsetof(struct bpf_reg_state, var_off) - sizeof(reg->type));
      reg->id = 0;
      reg->ref_obj_id = 0;
+     reg->parent_id = 0;
      ___mark_reg_known(reg, imm);
  }
 
@@ -1801,7 +1753,7 @@ static void mark_reg_known_zero(struct bpf_verifier_env *env,
  }
 
  static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type type,
-                           bool first_slot, int dynptr_id)
+                           bool first_slot, int id)
  {
      /* reg->type has no meaning for STACK_DYNPTR, but when we set reg for
       * callback arguments, it does need to be CONST_PTR_TO_DYNPTR, so simply
@@ -1810,7 +1762,7 @@ static void __mark_dynptr_reg(struct bpf_reg_state *reg, enum bpf_dynptr_type ty
      __mark_reg_known_zero(reg);
      reg->type = CONST_PTR_TO_DYNPTR;
      /* Give each dynptr a unique id to uniquely associate slices to it. */
-     reg->id = dynptr_id;
+     reg->id = id;
      reg->dynptr.type = type;
      reg->dynptr.first_slot = first_slot;
  }

kernel/bpf/verifier.c void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg) · line 2406

Quoted 4 lines
kernel/bpf/verifier.c +1 -0
@@ -2451,6 +2403,7 @@ void bpf_mark_reg_unknown_imprecise(struct bpf_reg_state *reg)
      reg->type = SCALAR_VALUE;
      reg->id = 0;
      reg->ref_obj_id = 0;
+     reg->parent_id = 0;

nit: it looks like we can do memset(0) on the entire reg and then set fields that need to be non-zero: type, var_off + __mark_reg_unbounded().

Quoted 64 lines
kernel/bpf/verifier.c +13 -9
      reg->var_off = tnum_unknown;
      reg->frameno = 0;
      reg->precise = false;
@@ -7427,7 +7380,7 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,
   * and checked dynamically during runtime.
   */
  static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn_idx,
-                            enum bpf_arg_type arg_type, int clone_ref_obj_id,
+                            enum bpf_arg_type arg_type, int parent_id,
                             struct bpf_dynptr_desc *dynptr)
  {
      struct bpf_reg_state *reg = reg_state(env, regno);
@@ -7470,7 +7423,8 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
                              return err;
              }
 
-             err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, clone_ref_obj_id);
+             err = mark_stack_slots_dynptr(env, reg, arg_type, insn_idx, parent_id,
+                                           dynptr);
      } else /* OBJ_RELEASE and None case from above */ {
              /* For the reg->type == PTR_TO_STACK case, bpf_dynptr is never const */
              if (reg->type == CONST_PTR_TO_DYNPTR && (arg_type & OBJ_RELEASE)) {
@@ -7507,6 +7461,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn
                      dynptr->id = reg->id;
                      dynptr->type = reg->dynptr.type;
                      dynptr->ref_obj_id = reg->ref_obj_id;
+                     dynptr->parent_id = reg->parent_id;
              }
      }
      return err;
@@ -8461,7 +8416,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                       */
                      if (reg->type == PTR_TO_STACK) {
                              spi = dynptr_get_spi(env, reg);
-                             if (spi < 0 || !state->stack[spi].spilled_ptr.ref_obj_id) {
+                             if (spi < 0 || !state->stack[spi].spilled_ptr.id) {
                                      verbose(env, "arg %d is an unacquired reference\n", regno);
                                      return -EINVAL;
                              }
@@ -8489,6 +8444,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                      return -EACCES;
              }
              meta->ref_obj_id = reg->ref_obj_id;
+             meta->id = reg->id;
      }
 
      switch (base_type(arg_type)) {
@@ -9111,26 +9067,75 @@ static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob
      return -EINVAL;
  }
 
-/* The pointer with the specified id has released its reference to kernel
- * resources. Identify all copies of the same pointer and clear the reference.
- *
- * This is the release function corresponding to acquire_reference(). Idempotent.
- */
-static int release_reference(struct bpf_verifier_env *env, int ref_obj_id)
+static int idstack_push(struct bpf_idmap *idmap, u32 id)
+{
+     int i;
+
+     if (!id)
+             return 0;
+
Quoted 8 lines

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9082

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+     for (i = 0; i < idmap->cnt; i++)
+             if (idmap->map[i].old == id)
+                     return 0;
+
+     if (WARN_ON_ONCE(idmap->cnt >= BPF_ID_MAP_SIZE))
+             return -EFAULT;

It feels like this check belongs above, maybe the first thing to do.

Shouldn't it allow the verification to continue when pushing an id that already exists in the stack?

Quoted 50 lines
Quoted 35 lines
kernel/bpf/verifier.c +21 -7
+
+     idmap->map[idmap->cnt++].old = id;
+     return 0;
+}
+
+static int idstack_pop(struct bpf_idmap *idmap)
  {
+     if (!idmap->cnt)
+             return 0;
+
+     return idmap->map[--idmap->cnt].old;
+}
+
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+     u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
      struct bpf_verifier_state *vstate = env->cur_state;
+     struct bpf_idmap *idstack = &env->idmap_scratch;
+     struct bpf_stack_state *stack;
      struct bpf_func_state *state;
      struct bpf_reg_state *reg;
-     int err;
+     int root_id = id, err;
 
-     err = release_reference_nomark(vstate, ref_obj_id);
-     if (err)
-             return err;
+     idstack->cnt = 0;
+     idstack_push(idstack, id);
 
-     bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-             if (reg->ref_obj_id == ref_obj_id)
-                     mark_reg_invalid(env, reg);

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9118

Quoted 10 lines
kernel/bpf/verifier.c +9 -1
-     }));
+     if (find_reference_state(vstate, id))
+             WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+     while ((id = idstack_pop(idstack))) {
+             bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+                     if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+                             continue;
+
+                     if (reg->ref_obj_id && id != root_id) {

Does this line check that the only ref_obj_id we should see is either 0 or root_id? can we rewrite it as
if (reg->ref_obj_id && reg->ref_obj_id != root_id)

Quoted 3 lines

this is simpler, because id can also be reg->id/reg->parent_id, which is hard to reason what that means.

Looking at it again, I think my release_reference() is wrong. It first calls release_reference_nomark() if the id is a ref_obj_id. This will invalidate all registers with the same ref_obj_id without traversing their children.

I should remember the root_ref_obj_id instead of calling release_reference_nomark(). Then, the check you mentioned should be:
if (reg->ref_obj_id && reg->ref_obj_id != root_ref_obj_id)

I will write a selftest to confirm the issue and include it in the next respin if this turns out to be a bug.

#20 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

Quoted 74 lines · Amery Hung
Quoted 71 lines · Mykyta Yatsenko
Quoted 67 lines · Amery Hung

Refactor object relationship tracking in the verifier and fix a dynptr use-after-free bug where file/skb dynptrs are not invalidated when the parent referenced object is freed.

Add parent_id to bpf_reg_state to precisely track child-parent relationships. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

When calling dynptr constructors (i.e., process_dynptr_func() with MEM_UNINIT argument), track the parent's id if the parent is a referenced object. This only applies to file dynptr and skb dynptr, so only pass parent reg->id to kfunc constructors.

For release_reference(), invalidating an object now also invalidates all descendants by traversing the object tree. This is done using stack-based DFS to avoid recursive call chains of release_reference() -> unmark_stack_slots_dynptr() -> release_reference(). Referenced objects encountered during tree traversal cannot be indirectly released. They require an explicit helper/kfunc call to release the acquired resources.

While the new design changes how object relationships are tracked in the verifier, it does not change the verifier's behavior. Here is the implication for dynptr, pointer casting, and owning/non-owning references:

Dynptr:

When initializing a dynptr, referenced dynptrs acquire a reference for ref_obj_id. If the dynptr has a referenced parent, parent_id tracks the parent's id. When cloning, ref_obj_id and parent_id are copied from the original. Releasing a referenced dynptr via release_reference(ref_obj_id) invalidates all clones and derived slices. For non-referenced dynptrs, only the specific dynptr and its children are invalidated.

Pointer casting:

Referenced socket pointers and their casted counterparts share the same lifetime but have different nullness — they have different id but the same ref_obj_id.

Owning to non-owning reference conversion:

After converting owning to non-owning by clearing ref_obj_id (e.g., object(id=1, ref_obj_id=1) -> object(id=1, ref_obj_id=0)), the verifier only needs to release the reference state, so it calls release_reference_nomark() instead of release_reference().

Note that the error message "reference has not been acquired before" in the helper and kfunc release paths is removed. This message was already unreachable. The verifier only calls release_reference() after confirming meta.ref_obj_id is valid, so the condition could never trigger in practice (no selftest exercises it either). With the refactor, release_reference() can now be called with non-acquired ids and have different error conditions. Report directly in release_reference() instead.

Fixes 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
---
  include/linux/bpf_verifier.h |  22 ++-
  kernel/bpf/log.c             |   4 +-
  kernel/bpf/states.c          |   9 +-
  kernel/bpf/verifier.c        | 264 +++++++++++++++++------------------
  4 files changed, 152 insertions(+), 147 deletions(-)

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0
@@ -673,82 +675,56 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
      mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
                             &state->stack[spi - 1].spilled_ptr, type);
 
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
kernel/bpf/verifier.c +0 -6
-     if (dynptr_type_refcounted(type)) {
-             /* The id is used to track proper releasing */
-             int id;
-
-             if (clone_ref_obj_id)
-                     id = clone_ref_obj_id;
Quoted 8 lines

kernel/bpf/verifier.c static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_ · line 689

Quoted 8 lines
kernel/bpf/verifier.c +0 -8
-             else
-                     id = acquire_reference(env, insn_idx);
-
-             if (id < 0)
-                     return id;
-
-             state->stack[spi].spilled_ptr.ref_obj_id = id;
-             state->stack[spi - 1].spilled_ptr.ref_obj_id = id;

is id assigned somewhere else now? where?

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
kernel/bpf/verifier.c +14 -0
+     if (dynptr->type == BPF_DYNPTR_TYPE_INVALID) { /* dynptr constructors */
+             if (dynptr_type_referenced(type)) {
+                     ref_obj_id = acquire_reference(env, insn_idx);
+                     if (ref_obj_id < 0)
+                             return ref_obj_id;
+             }
+     } else { /* bpf_dynptr_clone() */
+             ref_obj_id = dynptr->ref_obj_id;
+             parent_id = dynptr->parent_id;
      }
 
+     state->stack[spi].spilled_ptr.ref_obj_id = ref_obj_id;
+     state->stack[spi - 1].spilled_ptr.ref_obj_id = ref_obj_id;
+     state->stack[spi].spilled_ptr.parent_id = parent_id;
+     state->stack[spi - 1].spilled_ptr.parent_id = parent_id;
+
      return 0;
  }
 

[...]

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
kernel/bpf/verifier.c +7 -6
-/* The pointer with the specified id has released its reference to kernel
- * resources. Identify all copies of the same pointer and clear the reference.
- *
- * This is the release function corresponding to acquire_reference(). Idempotent.
- */
-static int release_reference(struct bpf_verifier_env *env, int ref_obj_id)
+static int idstack_push(struct bpf_idmap *idmap, u32 id)
+{
+     int i;
+
+     if (!id)
+             return 0;
+
Quoted 8 lines

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9082

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+     for (i = 0; i < idmap->cnt; i++)
+             if (idmap->map[i].old == id)
+                     return 0;
+
+     if (WARN_ON_ONCE(idmap->cnt >= BPF_ID_MAP_SIZE))
+             return -EFAULT;

It feels like this check belongs above, maybe the first thing to do.

Quoted 2 lines

Shouldn't it allow the verification to continue when pushing an id that already exists in the stack?

yep

Quoted 51 lines
Quoted 51 lines
Quoted 36 lines
kernel/bpf/verifier.c +21 -7
+
+     idmap->map[idmap->cnt++].old = id;
+     return 0;
+}
+
+static int idstack_pop(struct bpf_idmap *idmap)
  {
+     if (!idmap->cnt)
+             return 0;
+
+     return idmap->map[--idmap->cnt].old;
+}
+
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+     u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
      struct bpf_verifier_state *vstate = env->cur_state;
+     struct bpf_idmap *idstack = &env->idmap_scratch;
+     struct bpf_stack_state *stack;
      struct bpf_func_state *state;
      struct bpf_reg_state *reg;
-     int err;
+     int root_id = id, err;
 
-     err = release_reference_nomark(vstate, ref_obj_id);
-     if (err)
-             return err;
+     idstack->cnt = 0;
+     idstack_push(idstack, id);
 
-     bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-             if (reg->ref_obj_id == ref_obj_id)
-                     mark_reg_invalid(env, reg);

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9118

Quoted 10 lines
kernel/bpf/verifier.c +9 -1
-     }));
+     if (find_reference_state(vstate, id))
+             WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+     while ((id = idstack_pop(idstack))) {
+             bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+                     if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+                             continue;
+
+                     if (reg->ref_obj_id && id != root_id) {

Does this line check that the only ref_obj_id we should see is either 0 or root_id? can we rewrite it as
if (reg->ref_obj_id && reg->ref_obj_id != root_id)

Quoted 9 lines
Quoted 3 lines

this is simpler, because id can also be reg->id/reg->parent_id, which is hard to reason what that means.

Looking at it again, I think my release_reference() is wrong. It first calls release_reference_nomark() if the id is a ref_obj_id. This will invalidate all registers with the same ref_obj_id without traversing their children.

hm, will it? from what I see, release_reference_nomark() doesn't do anything with registers/stack slots, just removes the reference from state->refs

Quoted 6 lines

I should remember the root_ref_obj_id instead of calling release_reference_nomark(). Then, the check you mentioned should be:
if (reg->ref_obj_id && reg->ref_obj_id != root_ref_obj_id)

I will write a selftest to confirm the issue and include it in the next respin if this turns out to be a bug.

author
#21 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

Quoted 210 lines · Andrii Nakryiko
Quoted 74 lines · Amery Hung
Quoted 71 lines · Mykyta Yatsenko
Quoted 67 lines · Amery Hung

Refactor object relationship tracking in the verifier and fix a dynptr use-after-free bug where file/skb dynptrs are not invalidated when the parent referenced object is freed.

Add parent_id to bpf_reg_state to precisely track child-parent relationships. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

When calling dynptr constructors (i.e., process_dynptr_func() with MEM_UNINIT argument), track the parent's id if the parent is a referenced object. This only applies to file dynptr and skb dynptr, so only pass parent reg->id to kfunc constructors.

For release_reference(), invalidating an object now also invalidates all descendants by traversing the object tree. This is done using stack-based DFS to avoid recursive call chains of release_reference() -> unmark_stack_slots_dynptr() -> release_reference(). Referenced objects encountered during tree traversal cannot be indirectly released. They require an explicit helper/kfunc call to release the acquired resources.

While the new design changes how object relationships are tracked in the verifier, it does not change the verifier's behavior. Here is the implication for dynptr, pointer casting, and owning/non-owning references:

Dynptr:

When initializing a dynptr, referenced dynptrs acquire a reference for ref_obj_id. If the dynptr has a referenced parent, parent_id tracks the parent's id. When cloning, ref_obj_id and parent_id are copied from the original. Releasing a referenced dynptr via release_reference(ref_obj_id) invalidates all clones and derived slices. For non-referenced dynptrs, only the specific dynptr and its children are invalidated.

Pointer casting:

Referenced socket pointers and their casted counterparts share the same lifetime but have different nullness — they have different id but the same ref_obj_id.

Owning to non-owning reference conversion:

After converting owning to non-owning by clearing ref_obj_id (e.g., object(id=1, ref_obj_id=1) -> object(id=1, ref_obj_id=0)), the verifier only needs to release the reference state, so it calls release_reference_nomark() instead of release_reference().

Note that the error message "reference has not been acquired before" in the helper and kfunc release paths is removed. This message was already unreachable. The verifier only calls release_reference() after confirming meta.ref_obj_id is valid, so the condition could never trigger in practice (no selftest exercises it either). With the refactor, release_reference() can now be called with non-acquired ids and have different error conditions. Report directly in release_reference() instead.

Fixes 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
---
  include/linux/bpf_verifier.h |  22 ++-
  kernel/bpf/log.c             |   4 +-
  kernel/bpf/states.c          |   9 +-
  kernel/bpf/verifier.c        | 264 +++++++++++++++++------------------
  4 files changed, 152 insertions(+), 147 deletions(-)

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0
@@ -673,82 +675,56 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
      mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
                             &state->stack[spi - 1].spilled_ptr, type);
 
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
kernel/bpf/verifier.c +0 -6
-     if (dynptr_type_refcounted(type)) {
-             /* The id is used to track proper releasing */
-             int id;
-
-             if (clone_ref_obj_id)
-                     id = clone_ref_obj_id;
Quoted 8 lines

kernel/bpf/verifier.c static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_ · line 689

Quoted 8 lines
kernel/bpf/verifier.c +0 -8
-             else
-                     id = acquire_reference(env, insn_idx);
-
-             if (id < 0)
-                     return id;
-
-             state->stack[spi].spilled_ptr.ref_obj_id = id;
-             state->stack[spi - 1].spilled_ptr.ref_obj_id = id;

is id assigned somewhere else now? where?

Quoted 19 lines
Quoted 19 lines
Quoted 19 lines
kernel/bpf/verifier.c +14 -0
+     if (dynptr->type == BPF_DYNPTR_TYPE_INVALID) { /* dynptr constructors */
+             if (dynptr_type_referenced(type)) {
+                     ref_obj_id = acquire_reference(env, insn_idx);
+                     if (ref_obj_id < 0)
+                             return ref_obj_id;
+             }
+     } else { /* bpf_dynptr_clone() */
+             ref_obj_id = dynptr->ref_obj_id;
+             parent_id = dynptr->parent_id;
      }
 
+     state->stack[spi].spilled_ptr.ref_obj_id = ref_obj_id;
+     state->stack[spi - 1].spilled_ptr.ref_obj_id = ref_obj_id;
+     state->stack[spi].spilled_ptr.parent_id = parent_id;
+     state->stack[spi - 1].spilled_ptr.parent_id = parent_id;
+
      return 0;
  }
 

[...]

Quoted 22 lines
Quoted 13 lines
Quoted 13 lines
kernel/bpf/verifier.c +7 -6
-/* The pointer with the specified id has released its reference to kernel
- * resources. Identify all copies of the same pointer and clear the reference.
- *
- * This is the release function corresponding to acquire_reference(). Idempotent.
- */
-static int release_reference(struct bpf_verifier_env *env, int ref_obj_id)
+static int idstack_push(struct bpf_idmap *idmap, u32 id)
+{
+     int i;
+
+     if (!id)
+             return 0;
+
Quoted 8 lines

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9082

Quoted 6 lines
kernel/bpf/verifier.c +6 -0
+     for (i = 0; i < idmap->cnt; i++)
+             if (idmap->map[i].old == id)
+                     return 0;
+
+     if (WARN_ON_ONCE(idmap->cnt >= BPF_ID_MAP_SIZE))
+             return -EFAULT;

It feels like this check belongs above, maybe the first thing to do.

Quoted 2 lines

Shouldn't it allow the verification to continue when pushing an id that already exists in the stack?

yep

Quoted 51 lines
Quoted 51 lines
Quoted 36 lines
kernel/bpf/verifier.c +21 -7
+
+     idmap->map[idmap->cnt++].old = id;
+     return 0;
+}
+
+static int idstack_pop(struct bpf_idmap *idmap)
  {
+     if (!idmap->cnt)
+             return 0;
+
+     return idmap->map[--idmap->cnt].old;
+}
+
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+     u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
      struct bpf_verifier_state *vstate = env->cur_state;
+     struct bpf_idmap *idstack = &env->idmap_scratch;
+     struct bpf_stack_state *stack;
      struct bpf_func_state *state;
      struct bpf_reg_state *reg;
-     int err;
+     int root_id = id, err;
 
-     err = release_reference_nomark(vstate, ref_obj_id);
-     if (err)
-             return err;
+     idstack->cnt = 0;
+     idstack_push(idstack, id);
 
-     bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-             if (reg->ref_obj_id == ref_obj_id)
-                     mark_reg_invalid(env, reg);

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9118

Quoted 10 lines
kernel/bpf/verifier.c +9 -1
-     }));
+     if (find_reference_state(vstate, id))
+             WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+     while ((id = idstack_pop(idstack))) {
+             bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+                     if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+                             continue;
+
+                     if (reg->ref_obj_id && id != root_id) {

Does this line check that the only ref_obj_id we should see is either 0 or root_id? can we rewrite it as
if (reg->ref_obj_id && reg->ref_obj_id != root_id)

Quoted 9 lines
Quoted 3 lines

this is simpler, because id can also be reg->id/reg->parent_id, which is hard to reason what that means.

Looking at it again, I think my release_reference() is wrong. It first calls release_reference_nomark() if the id is a ref_obj_id. This will invalidate all registers with the same ref_obj_id without traversing their children.

Quoted 3 lines

hm, will it? from what I see, release_reference_nomark() doesn't do anything with registers/stack slots, just removes the reference from state->refs

You are right... I was conflating it with something else.

I think Mykyta's suggestion seems to be more straightforward so I will change it in the next version.

Quoted 8 lines
Quoted 8 lines

I should remember the root_ref_obj_id instead of calling release_reference_nomark(). Then, the check you mentioned should be:
if (reg->ref_obj_id && reg->ref_obj_id != root_ref_obj_id)

I will write a selftest to confirm the issue and include it in the next respin if this turns out to be a bug.

author
#22 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

In reply to Andrii Nakryiko
Quoted 83 lines · Andrii Nakryiko
Quoted 74 lines · Amery Hung
Quoted 71 lines · Mykyta Yatsenko
Quoted 67 lines · Amery Hung

Refactor object relationship tracking in the verifier and fix a dynptr use-after-free bug where file/skb dynptrs are not invalidated when the parent referenced object is freed.

Add parent_id to bpf_reg_state to precisely track child-parent relationships. A child object's parent_id points to the parent object's id. This replaces the PTR_TO_MEM-specific dynptr_id and does not increase the size of bpf_reg_state on 64-bit machines as there is existing padding.

When calling dynptr constructors (i.e., process_dynptr_func() with MEM_UNINIT argument), track the parent's id if the parent is a referenced object. This only applies to file dynptr and skb dynptr, so only pass parent reg->id to kfunc constructors.

For release_reference(), invalidating an object now also invalidates all descendants by traversing the object tree. This is done using stack-based DFS to avoid recursive call chains of release_reference() -> unmark_stack_slots_dynptr() -> release_reference(). Referenced objects encountered during tree traversal cannot be indirectly released. They require an explicit helper/kfunc call to release the acquired resources.

While the new design changes how object relationships are tracked in the verifier, it does not change the verifier's behavior. Here is the implication for dynptr, pointer casting, and owning/non-owning references:

Dynptr:

When initializing a dynptr, referenced dynptrs acquire a reference for ref_obj_id. If the dynptr has a referenced parent, parent_id tracks the parent's id. When cloning, ref_obj_id and parent_id are copied from the original. Releasing a referenced dynptr via release_reference(ref_obj_id) invalidates all clones and derived slices. For non-referenced dynptrs, only the specific dynptr and its children are invalidated.

Pointer casting:

Referenced socket pointers and their casted counterparts share the same lifetime but have different nullness — they have different id but the same ref_obj_id.

Owning to non-owning reference conversion:

After converting owning to non-owning by clearing ref_obj_id (e.g., object(id=1, ref_obj_id=1) -> object(id=1, ref_obj_id=0)), the verifier only needs to release the reference state, so it calls release_reference_nomark() instead of release_reference().

Note that the error message "reference has not been acquired before" in the helper and kfunc release paths is removed. This message was already unreachable. The verifier only calls release_reference() after confirming meta.ref_obj_id is valid, so the condition could never trigger in practice (no selftest exercises it either). With the refactor, release_reference() can now be called with non-acquired ids and have different error conditions. Report directly in release_reference() instead.

Fixes 870c28588afa ("bpf: net_sched: Add basic bpf qdisc kfuncs")
---
  include/linux/bpf_verifier.h |  22 ++-
  kernel/bpf/log.c             |   4 +-
  kernel/bpf/states.c          |   9 +-
  kernel/bpf/verifier.c        | 264 +++++++++++++++++------------------
  4 files changed, 152 insertions(+), 147 deletions(-)

[...]

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0
@@ -673,82 +675,56 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
      mark_dynptr_stack_regs(env, &state->stack[spi].spilled_ptr,
                             &state->stack[spi - 1].spilled_ptr, type);
 
Quoted 17 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
kernel/bpf/verifier.c +0 -6
-     if (dynptr_type_refcounted(type)) {
-             /* The id is used to track proper releasing */
-             int id;
-
-             if (clone_ref_obj_id)
-                     id = clone_ref_obj_id;
Quoted 8 lines
Quoted 8 lines

kernel/bpf/verifier.c static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_ · line 689

Quoted 8 lines
kernel/bpf/verifier.c +0 -8
-             else
-                     id = acquire_reference(env, insn_idx);
-
-             if (id < 0)
-                     return id;
-
-             state->stack[spi].spilled_ptr.ref_obj_id = id;
-             state->stack[spi - 1].spilled_ptr.ref_obj_id = id;

is id assigned somewhere else now? where?

I figured that I missed this question. The existing mark_dynptr_stack_regs() above already assigned an id.

maintainer
#23 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

In reply to Amery Hung

On Tue, 2026-04-21 at 15:10 -0700, Amery Hung wrote:

Tbh, I find current state of affairs with id/ref_obj_id/parent_id hard to follow. The release_reference() is an improvement, but the means by which the fields are propagated to bpf_reg_state objects are convoluted. I wonder if having a separate "object table" in bpf_verifier_state and having bpf_reg_state->id refer to objects within this table would make things more straight forward.

A few nits below.

[...]

Quoted 1 line
include/linux/bpf_verifier.h +0 -0

[...]

include/linux/bpf_verifier.h struct bpf_dynptr_desc { · line 1339

Quoted 6 lines
include/linux/bpf_verifier.h +1 -0
@@ -1323,6 +1335,7 @@ struct bpf_dynptr_desc {
 	enum bpf_dynptr_type type;
 	u32 id;
 	u32 ref_obj_id;
+	u32 parent_id;
 };

Nit: would be nice to have a comment describing when the above fields
are populated.

include/linux/bpf_verifier.h struct bpf_kfunc_call_arg_meta { · line 1350

Quoted 7 lines
include/linux/bpf_verifier.h +1 -0
 struct bpf_kfunc_call_arg_meta {
@@ -1334,6 +1347,7 @@ struct bpf_kfunc_call_arg_meta {
 	const char *func_name;
 	/* Out parameters */
 	u32 ref_obj_id;
+	u32 id;

Nit: would be nice to have a comment here too.

[...]

kernel/bpf/states.c static bool regs_exact(const struct bpf_reg_state *rold, · line 498

Quoted 11 lines
kernel/bpf/states.c +2 -1
index 8478d2c6ed5b..72bd3bcda5fb 100644
@@ -494,7 +494,8 @@ static bool regs_exact(const struct bpf_reg_state *rold,
 {
 	return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
 	       check_ids(rold->id, rcur->id, idmap) &&
-	       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+	       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+	       check_ids(rold->parent_id, rcur->parent_id, idmap);

Nit: these check_ids() become repetitive, maybe add a utility function
checking id/ref_obj_id/parent_id?

Quoted 14 lines
kernel/bpf/states.c +2 -1
 }
 
 enum exact_level {
@@ -619,7 +620,8 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
 		       range_within(rold, rcur) &&
 		       tnum_in(rold->var_off, rcur->var_off) &&
 		       check_ids(rold->id, rcur->id, idmap) &&
-		       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+		       check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+		       check_ids(rold->parent_id, rcur->parent_id, idmap);
 	case PTR_TO_PACKET_META:
 	case PTR_TO_PACKET:
 		/* We must have at least as much range as the old ptr

kernel/bpf/states.c static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, · line 805

Quoted 6 lines
kernel/bpf/states.c +2 -1
@@ -799,7 +801,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
 			cur_reg = &cur->stack[spi].spilled_ptr;
 			if (old_reg->dynptr.type != cur_reg->dynptr.type ||
 			    old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
-			    !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap))
+			    !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap) ||
+			    !check_ids(old_reg->parent_id, cur_reg->parent_id, idmap))

Not something changed by the current patch, but still a question:
this path ignores old_reg->id, is it a bug?

Quoted 5 lines
kernel/bpf/states.c +0 -0
 				return false;
 			break;
 		case STACK_ITER:
kernel/bpf/verifier.c +0 -0
index 0313b7d5f6c9..908a3af0e7c4 100644

[...]

kernel/bpf/verifier.c struct bpf_call_arg_meta { · line 244

Quoted 5 lines
kernel/bpf/verifier.c +1 -0
@@ -241,6 +241,7 @@ struct bpf_call_arg_meta {
 	int mem_size;
 	u64 msize_max_value;
 	int ref_obj_id;
+	u32 id;

Nit: please add a comment here as well.

[...]

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

Quoted 7 lines
kernel/bpf/verifier.c +2 -1
@@ -635,11 +636,12 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
 				        struct bpf_func_state *state, int spi);
 
 static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
-				   enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id)
+				   enum bpf_arg_type arg_type, int insn_idx, int parent_id,
+				   struct bpf_dynptr_desc *dynptr)

Having both parent_id and dynptr->parent_id as parameters of this function is very confusing, but I don't have a suggestion on how to better deal with it.

[...]

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8447

Quoted 5 lines
kernel/bpf/verifier.c +1 -0
@@ -8489,6 +8444,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 			return -EACCES;
 		}
 		meta->ref_obj_id = reg->ref_obj_id;
+		meta->id = reg->id;

Could you please leave a comment here explaining when e.g. meta->ref_obj_id and meta->id would have same or different ids here? Maybe a few lines of BPF C code.

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
 	}
 
 	switch (base_type(arg_type)) {
@@ -9111,26 +9067,75 @@ static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob

[...]

Quoted 39 lines
kernel/bpf/verifier.c +26 -8
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+	u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
 	struct bpf_verifier_state *vstate = env->cur_state;
+	struct bpf_idmap *idstack = &env->idmap_scratch;
+	struct bpf_stack_state *stack;
 	struct bpf_func_state *state;
 	struct bpf_reg_state *reg;
-	int err;
+	int root_id = id, err;
 
-	err = release_reference_nomark(vstate, ref_obj_id);
-	if (err)
-		return err;
+	idstack->cnt = 0;
+	idstack_push(idstack, id);
 
-	bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-		if (reg->ref_obj_id == ref_obj_id)
-			mark_reg_invalid(env, reg);
-	}));
+	if (find_reference_state(vstate, id))
+		WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+	while ((id = idstack_pop(idstack))) {
+		bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+			if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+				continue;
+
+			if (reg->ref_obj_id && id != root_id) {
+				struct bpf_reference_state *ref_state;
+
+				ref_state = find_reference_state(env->cur_state, reg->ref_obj_id);
+				verbose(env, "Unreleased reference id=%d alloc_insn=%d when releasing id=%d\n",
+					ref_state->id, ref_state->insn_idx, root_id);
+				return -EINVAL;
+			}
+

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9136

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+			if (reg->id != id) {
+				err = idstack_push(idstack, reg->id);
+				if (err)
+					return err;
+			}
+
+			if (!stack || stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL)
+				mark_reg_invalid(env, reg);
+			else if (stack->slot_type[BPF_REG_SIZE - 1] == STACK_DYNPTR)
+				invalidate_dynptr(env, state, stack);

invalidate_dynptr() rewrites to stack slots, can it be the case that this body of bpf_for_each_reg_in_vstate_mask() is computed for first and second dynptr stack slots, hence triggering invalidate_dynptr() to rewrite three slots instead of two?

Quoted 5 lines
kernel/bpf/verifier.c +2 -0
+		}));
+	}
 
 	return 0;
 }

[...]

kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · line 12012

Quoted 5 lines
kernel/bpf/verifier.c +1 -0
@@ -12009,6 +12009,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 				return -EFAULT;
 			}
 			meta->ref_obj_id = reg->ref_obj_id;
+			meta->id = reg->id;

And here a comment describing when this happens would be helpful.

[...]

Quoted 5 lines
kernel/bpf/verifier.c +0 -1
@@ -12171,15 +12171,10 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
 				}
 
 				dynptr_arg_type |= (unsigned int)get_dynptr_type_flag(parent_type);
-				clone_ref_obj_id = meta->dynptr.ref_obj_id;

kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · line 12177

Quoted 10 lines
kernel/bpf/verifier.c +2 -6
-				if (dynptr_type_refcounted(parent_type) && !clone_ref_obj_id) {
-					verifier_bug(env, "missing ref obj id for parent of clone");
-					return -EFAULT;
-				}
 			}
 
-			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id,
-						  &meta->dynptr);
+			ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type,
+						  meta->ref_obj_id ? meta->id : 0, &meta->dynptr);

And an example here as well.

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
 			if (ret < 0)
 				return ret;
 			break;

[...]

author
#24 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

Quoted 9 lines · Eduard Zingerman

On Tue, 2026-04-21 at 15:10 -0700, Amery Hung wrote:

Tbh, I find current state of affairs with id/ref_obj_id/parent_id hard to follow. The release_reference() is an improvement, but the means by which the fields are propagated to bpf_reg_state objects are convoluted. I wonder if having a separate "object table" in bpf_verifier_state and having bpf_reg_state->id refer to objects within this table would make things more straight forward.

I think this is a good idea in the long term. First, we need to make id a stable and unique object identifier (i.e., always assign id to objects; different objects have different ids). Then, we can create the object table indexed by id and each entry contains the ref_obj_id and parent_id moved from bpf_reg_state. Then, there will be helpers managing the lifetime, relationship (e.g., bpf_obj_create, bpf_obj_release, bpf_obj_clone) with clear semantics to centralize how id,parent_id,ref_obj_id are manipulated.

If this makes sense, I can send this as a follow up patchset.

Quoted 9 lines

A few nits below.

[...]

Quoted 1 line
include/linux/bpf_verifier.h +0 -0

[...]

Quoted 9 lines

include/linux/bpf_verifier.h struct bpf_dynptr_desc { · line 1339

Quoted 6 lines
include/linux/bpf_verifier.h +1 -0
@@ -1323,6 +1335,7 @@ struct bpf_dynptr_desc {
      enum bpf_dynptr_type type;
      u32 id;
      u32 ref_obj_id;
+     u32 parent_id;
 };

Nit: would be nice to have a comment describing when the above fields
are populated.

Ack

Quoted 10 lines

include/linux/bpf_verifier.h struct bpf_kfunc_call_arg_meta { · line 1350

Quoted 8 lines
include/linux/bpf_verifier.h +1 -0
 struct bpf_kfunc_call_arg_meta {
@@ -1334,6 +1347,7 @@ struct bpf_kfunc_call_arg_meta {
      const char *func_name;
      /* Out parameters */
      u32 ref_obj_id;
+     u32 id;

Nit: would be nice to have a comment here too.

Ack

Quoted 17 lines

[...]

kernel/bpf/states.c static bool regs_exact(const struct bpf_reg_state *rold, · line 498

Quoted 11 lines
kernel/bpf/states.c +2 -1
index 8478d2c6ed5b..72bd3bcda5fb 100644
@@ -494,7 +494,8 @@ static bool regs_exact(const struct bpf_reg_state *rold,
 {
      return memcmp(rold, rcur, offsetof(struct bpf_reg_state, id)) == 0 &&
             check_ids(rold->id, rcur->id, idmap) &&
-            check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+            check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+            check_ids(rold->parent_id, rcur->parent_id, idmap);

Nit: these check_ids() become repetitive, maybe add a utility function
checking id/ref_obj_id/parent_id?

Will add a utility function.

Quoted 16 lines
Quoted 16 lines
kernel/bpf/states.c +2 -1
 }
 
 enum exact_level {
@@ -619,7 +620,8 @@ static bool regsafe(struct bpf_verifier_env *env, struct bpf_reg_state *rold,
                     range_within(rold, rcur) &&
                     tnum_in(rold->var_off, rcur->var_off) &&
                     check_ids(rold->id, rcur->id, idmap) &&
-                    check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap);
+                    check_ids(rold->ref_obj_id, rcur->ref_obj_id, idmap) &&
+                    check_ids(rold->parent_id, rcur->parent_id, idmap);
      case PTR_TO_PACKET_META:
      case PTR_TO_PACKET:
              /* We must have at least as much range as the old ptr
@@ -799,7 +801,8 @@ static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old,
                      cur_reg = &cur->stack[spi].spilled_ptr;
Quoted 8 lines

kernel/bpf/states.c static bool stacksafe(struct bpf_verifier_env *env, struct bpf_func_state *old, · line 805

Quoted 5 lines
kernel/bpf/states.c +2 -1
                      if (old_reg->dynptr.type != cur_reg->dynptr.type ||
                          old_reg->dynptr.first_slot != cur_reg->dynptr.first_slot ||
-                         !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap))
+                         !check_ids(old_reg->ref_obj_id, cur_reg->ref_obj_id, idmap) ||
+                         !check_ids(old_reg->parent_id, cur_reg->parent_id, idmap))

Not something changed by the current patch, but still a question:
this path ignores old_reg->id, is it a bug?

Yes. I will also check id.

Quoted 9 lines
Quoted 6 lines
kernel/bpf/states.c +0 -0
                              return false;
                      break;
              case STACK_ITER:
kernel/bpf/verifier.c +0 -0
index 0313b7d5f6c9..908a3af0e7c4 100644

[...]

Quoted 7 lines

kernel/bpf/verifier.c struct bpf_call_arg_meta { · line 244

Quoted 5 lines
kernel/bpf/verifier.c +1 -0
@@ -241,6 +241,7 @@ struct bpf_call_arg_meta {
      int mem_size;
      u64 msize_max_value;
      int ref_obj_id;
+     u32 id;

Nit: please add a comment here as well.

Ack

Quoted 6 lines

[...]

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
@@ -635,11 +636,12 @@ static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
                                      struct bpf_func_state *state, int spi);
 
Quoted 8 lines

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

Quoted 4 lines
kernel/bpf/verifier.c +2 -1
 static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
-                                enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id)
+                                enum bpf_arg_type arg_type, int insn_idx, int parent_id,
+                                struct bpf_dynptr_desc *dynptr)

Having both parent_id and dynptr->parent_id as parameters of this function is very confusing, but I don't have a suggestion on how to better deal with it.

Does tweaking the argument name and reorder the if-else block make it more obvious how these two arguments are used?

if (clone_dynptr->type != BPF_DYNPTR_TYPE_INVALID) { /* bpf_dynptr_clone() */

        ref_obj_id = clone_dynptr->ref_obj_id;
        parent_id = clone_dynptr->parent_id;
} else { /* dynptr constructors */
        if (dynptr_type_referenced(type)) {
                ref_obj_id = acquire_reference(env, insn_idx);
                if (ref_obj_id < 0)
                        return ref_obj_id;
        }
}
Quoted 12 lines

[...]

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8447

Quoted 5 lines
kernel/bpf/verifier.c +1 -0
@@ -8489,6 +8444,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                      return -EACCES;
              }
              meta->ref_obj_id = reg->ref_obj_id;
+             meta->id = reg->id;

Could you please leave a comment here explaining when e.g. meta->ref_obj_id and meta->id would have same or different ids here? Maybe a few lines of BPF C code.

Ack

Quoted 54 lines
Quoted 5 lines
kernel/bpf/verifier.c +0 -0
      }
 
      switch (base_type(arg_type)) {
@@ -9111,26 +9067,75 @@ static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob

[...]

Quoted 46 lines
kernel/bpf/verifier.c +33 -8
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+     u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
      struct bpf_verifier_state *vstate = env->cur_state;
+     struct bpf_idmap *idstack = &env->idmap_scratch;
+     struct bpf_stack_state *stack;
      struct bpf_func_state *state;
      struct bpf_reg_state *reg;
-     int err;
+     int root_id = id, err;
 
-     err = release_reference_nomark(vstate, ref_obj_id);
-     if (err)
-             return err;
+     idstack->cnt = 0;
+     idstack_push(idstack, id);
 
-     bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-             if (reg->ref_obj_id == ref_obj_id)
-                     mark_reg_invalid(env, reg);
-     }));
+     if (find_reference_state(vstate, id))
+             WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+     while ((id = idstack_pop(idstack))) {
+             bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+                     if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+                             continue;
+
+                     if (reg->ref_obj_id && id != root_id) {
+                             struct bpf_reference_state *ref_state;
+
+                             ref_state = find_reference_state(env->cur_state, reg->ref_obj_id);
+                             verbose(env, "Unreleased reference id=%d alloc_insn=%d when releasing id=%d\n",
+                                     ref_state->id, ref_state->insn_idx, root_id);
+                             return -EINVAL;
+                     }
+
+                     if (reg->id != id) {
+                             err = idstack_push(idstack, reg->id);
+                             if (err)
+                                     return err;
+                     }
+
+                     if (!stack || stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL)
Quoted 8 lines

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9136

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+                             mark_reg_invalid(env, reg);
+                     else if (stack->slot_type[BPF_REG_SIZE - 1] == STACK_DYNPTR)
+                             invalidate_dynptr(env, state, stack);

invalidate_dynptr() rewrites to stack slots, can it be the case that this body of bpf_for_each_reg_in_vstate_mask() is computed for first and second dynptr stack slots, hence triggering invalidate_dynptr() to rewrite three slots instead of two?

invalidate_dynptr() will mark the two stack slots as STACK_INVALID so I didn't bother to check whether it is the first slot or not. Am I missing anything?

Quoted 9 lines
Quoted 6 lines
kernel/bpf/verifier.c +2 -0
+             }));
+     }
 
      return 0;
 }

[...]

Quoted 8 lines

kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · line 12012

Quoted 5 lines
kernel/bpf/verifier.c +1 -0
@@ -12009,6 +12009,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
                              return -EFAULT;
                      }
                      meta->ref_obj_id = reg->ref_obj_id;
+                     meta->id = reg->id;

And here a comment describing when this happens would be helpful.

Ack

Quoted 13 lines

[...]

Quoted 11 lines
kernel/bpf/verifier.c +0 -5
@@ -12171,15 +12171,10 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
                              }
 
                              dynptr_arg_type |= (unsigned int)get_dynptr_type_flag(parent_type);
-                             clone_ref_obj_id = meta->dynptr.ref_obj_id;
-                             if (dynptr_type_refcounted(parent_type) && !clone_ref_obj_id) {
-                                     verifier_bug(env, "missing ref obj id for parent of clone");
-                                     return -EFAULT;
-                             }
                      }
 
Quoted 6 lines

kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ · line 12177

Quoted 4 lines
kernel/bpf/verifier.c +2 -2
-                     ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id,
-                                               &meta->dynptr);
+                     ret = process_dynptr_func(env, regno, insn_idx, dynptr_arg_type,
+                                               meta->ref_obj_id ? meta->id : 0, &meta->dynptr);

And an example here as well.

Ack

Quoted 6 lines
Quoted 4 lines
kernel/bpf/verifier.c +0 -0
                      if (ret < 0)
                              return ret;
                      break;

[...]

maintainer
#25 lore raw

Re: [PATCH bpf-next v3 4/9] bpf: Refactor object relationship tracking and fix dynptr UAF bug

Quoted 20 lines · Amery Hung
Quoted 9 lines · Eduard Zingerman

On Tue, 2026-04-21 at 15:10 -0700, Amery Hung wrote:

Tbh, I find current state of affairs with id/ref_obj_id/parent_id hard to follow. The release_reference() is an improvement, but the means by which the fields are propagated to bpf_reg_state objects are convoluted. I wonder if having a separate "object table" in bpf_verifier_state and having bpf_reg_state->id refer to objects within this table would make things more straight forward.

I think this is a good idea in the long term. First, we need to make id a stable and unique object identifier (i.e., always assign id to objects; different objects have different ids). Then, we can create the object table indexed by id and each entry contains the ref_obj_id and parent_id moved from bpf_reg_state. Then, there will be helpers managing the lifetime, relationship (e.g., bpf_obj_create, bpf_obj_release, bpf_obj_clone) with clear semantics to centralize how id,parent_id,ref_obj_id are manipulated.

Quoted 1 line

If this makes sense, I can send this as a follow up patchset.

I'd play with it a bit, might make sense if we move a lot of objects into this table. E.g. forgo find_good_pkt_pointers() and keep packet info as one of the objects, etc. On the other hand, dynptr and iters are inherently stack allocated objects, so it might be the case that the final implementation would be an over-complication.

[...]

Quoted 11 lines
Quoted 8 lines

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

Quoted 4 lines

static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg,

kernel/bpf/verifier.c +2 -1
-                                enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id)
+                                enum bpf_arg_type arg_type, int insn_idx, int parent_id,
+                                struct bpf_dynptr_desc *dynptr)

Having both parent_id and dynptr->parent_id as parameters of this function is very confusing, but I don't have a suggestion on how to better deal with it.

Does tweaking the argument name and reorder the if-else block make it more obvious how these two arguments are used?

Idk, let's go with your original code, maybe? The eventual goal is to have a unified handling for both helpers and kfuncs, right? Meaning that these would be packed into some common arg_info_meta, with some comments on the structure fields should look ok.

[...]

Quoted 55 lines
Quoted 46 lines
Quoted 46 lines
kernel/bpf/verifier.c +33 -8
+/* Release id and objects referencing the id iteratively in a DFS manner */
+static int release_reference(struct bpf_verifier_env *env, int id)
+{
+     u32 mask = (1 << STACK_SPILL) | (1 << STACK_DYNPTR);
      struct bpf_verifier_state *vstate = env->cur_state;
+     struct bpf_idmap *idstack = &env->idmap_scratch;
+     struct bpf_stack_state *stack;
      struct bpf_func_state *state;
      struct bpf_reg_state *reg;
-     int err;
+     int root_id = id, err;
 
-     err = release_reference_nomark(vstate, ref_obj_id);
-     if (err)
-             return err;
+     idstack->cnt = 0;
+     idstack_push(idstack, id);
 
-     bpf_for_each_reg_in_vstate(vstate, state, reg, ({
-             if (reg->ref_obj_id == ref_obj_id)
-                     mark_reg_invalid(env, reg);
-     }));
+     if (find_reference_state(vstate, id))
+             WARN_ON_ONCE(release_reference_nomark(vstate, id));
+
+     while ((id = idstack_pop(idstack))) {
+             bpf_for_each_reg_in_vstate_mask(vstate, state, reg, stack, mask, ({
+                     if (reg->id != id && reg->parent_id != id && reg->ref_obj_id != id)
+                             continue;
+
+                     if (reg->ref_obj_id && id != root_id) {
+                             struct bpf_reference_state *ref_state;
+
+                             ref_state = find_reference_state(env->cur_state, reg->ref_obj_id);
+                             verbose(env, "Unreleased reference id=%d alloc_insn=%d when releasing id=%d\n",
+                                     ref_state->id, ref_state->insn_idx, root_id);
+                             return -EINVAL;
+                     }
+
+                     if (reg->id != id) {
+                             err = idstack_push(idstack, reg->id);
+                             if (err)
+                                     return err;
+                     }
+
+                     if (!stack || stack->slot_type[BPF_REG_SIZE - 1] == STACK_SPILL)
Quoted 8 lines

kernel/bpf/verifier.c static int release_reference_nomark(struct bpf_verifier_state *state, int ref_ob · line 9136

Quoted 3 lines
kernel/bpf/verifier.c +3 -0
+                             mark_reg_invalid(env, reg);
+                     else if (stack->slot_type[BPF_REG_SIZE - 1] == STACK_DYNPTR)
+                             invalidate_dynptr(env, state, stack);

invalidate_dynptr() rewrites to stack slots, can it be the case that this body of bpf_for_each_reg_in_vstate_mask() is computed for first and second dynptr stack slots, hence triggering invalidate_dynptr() to rewrite three slots instead of two?

Quoted 3 lines

invalidate_dynptr() will mark the two stack slots as STACK_INVALID so I didn't bother to check whether it is the first slot or not. Am I missing anything?

Oh, right, the == STACK_DYNPTR won't fire for the second slot. Sorry for the noise.

[...]

author
#26 lore raw

PATCH v3 5/9 bpf: Remove redundant dynptr arg check for helper

In reply to Amery Hung

unmark_stack_slots_dynptr() already makes sure that CONST_PTR_TO_DYNPTR cannot be released. process_dynptr_func() also prevents passing uninitialized dynptr to helpers expecting initialized dynptr. Now that unmark_stack_slots_dynptr() also error returned from release_reference(), there should be no reason to keep these redundant checks.

---
 kernel/bpf/verifier.c                         | 21 +------------------
 .../testing/selftests/bpf/progs/dynptr_fail.c |  6 +++---
 .../selftests/bpf/progs/user_ringbuf_fail.c   |  4 ++--
 3 files changed, 6 insertions(+), 25 deletions(-)
kernel/bpf/verifier.c +1 -20
index 908a3af0e7c4..3ab9bc2fe0e3 100644
@@ -8405,26 +8405,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 
 skip_type_check:
 	if (arg_type_is_release(arg_type)) {
-		if (arg_type_is_dynptr(arg_type)) {
-			struct bpf_func_state *state = bpf_func(env, reg);
-			int spi;
-
-			/* Only dynptr created on stack can be released, thus
-			 * the get_spi and stack state checks for spilled_ptr
-			 * should only be done before process_dynptr_func for
-			 * PTR_TO_STACK.
-			 */
-			if (reg->type == PTR_TO_STACK) {
-				spi = dynptr_get_spi(env, reg);
-				if (spi < 0 || !state->stack[spi].spilled_ptr.id) {
-					verbose(env, "arg %d is an unacquired reference\n", regno);
-					return -EINVAL;
-				}
-			} else {
-				verbose(env, "cannot release unowned const bpf_dynptr\n");
-				return -EINVAL;
-			}
-		} else if (!reg->ref_obj_id && !bpf_register_is_null(reg)) {
+		if (!arg_type_is_dynptr(arg_type) && !reg->ref_obj_id && !bpf_register_is_null(reg)) {
 			verbose(env, "R%d must be referenced when passed to release function\n",
 				regno);
 			return -EINVAL;
tools/testing/selftests/bpf/progs/dynptr_fail.c +3 -3
index b62773ce5219..b5fbc9b5c484 100644
@@ -136,7 +136,7 @@ int ringbuf_missing_release_callback(void *ctx)
 
 /* Can't call bpf_ringbuf_submit/discard_dynptr on a non-initialized dynptr */
 SEC("?raw_tp")
-__failure __msg("arg 1 is an unacquired reference")
+__failure __msg("Expected an initialized dynptr as arg #0")
 int ringbuf_release_uninit_dynptr(void *ctx)
 {
 	struct bpf_dynptr ptr;
@@ -650,7 +650,7 @@ int invalid_offset(void *ctx)
 
 /* Can't release a dynptr twice */
 SEC("?raw_tp")
-__failure __msg("arg 1 is an unacquired reference")
+__failure __msg("Expected an initialized dynptr as arg #0")
 int release_twice(void *ctx)
 {
 	struct bpf_dynptr ptr;
@@ -677,7 +677,7 @@ static int release_twice_callback_fn(__u32 index, void *data)
  * within a callback function, fails
  */
 SEC("?raw_tp")
-__failure __msg("arg 1 is an unacquired reference")
+__failure __msg("Expected an initialized dynptr as arg #0")
 int release_twice_callback(void *ctx)
 {
 	struct bpf_dynptr ptr;
tools/testing/selftests/bpf/progs/user_ringbuf_fail.c +2 -2
index 54de0389f878..c0d0422b8030 100644
@@ -146,7 +146,7 @@ try_discard_dynptr(struct bpf_dynptr *dynptr, void *context)
  * not be able to read past the end of the pointer.
  */
 SEC("?raw_tp")
-__failure __msg("cannot release unowned const bpf_dynptr")
+__failure __msg("CONST_PTR_TO_DYNPTR cannot be released")
 int user_ringbuf_callback_discard_dynptr(void *ctx)
 {
 	bpf_user_ringbuf_drain(&user_ringbuf, try_discard_dynptr, NULL, 0);
@@ -166,7 +166,7 @@ try_submit_dynptr(struct bpf_dynptr *dynptr, void *context)
  * not be able to read past the end of the pointer.
  */
 SEC("?raw_tp")
-__failure __msg("cannot release unowned const bpf_dynptr")
+__failure __msg("CONST_PTR_TO_DYNPTR cannot be released")
 int user_ringbuf_callback_submit_dynptr(void *ctx)
 {
 	bpf_user_ringbuf_drain(&user_ringbuf, try_submit_dynptr, NULL, 0);
Signature · 1 line
2.52.0
author
#27 lore raw

PATCH v3 6/9 selftests/bpf: Test creating dynptr from dynptr data and slice

In reply to Amery Hung

The verifier currently does not allow creating dynptr from dynptr data or slice. Add a selftest to test this explicitly.

---
 .../testing/selftests/bpf/progs/dynptr_fail.c | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
tools/testing/selftests/bpf/progs/dynptr_fail.c +42 -0
index b5fbc9b5c484..43beb70f50ee 100644
@@ -705,6 +705,48 @@ int dynptr_from_mem_invalid_api(void *ctx)
 	return 0;
 }
 
+/* Cannot create dynptr from dynptr data */
+SEC("?raw_tp")
+__failure __msg("Unsupported reg type mem for bpf_dynptr_from_mem data")
+int dynptr_from_dynptr_data(void *ctx)
+{
+	struct bpf_dynptr ptr, ptr2;
+	__u8 *data;
+
+	if (get_map_val_dynptr(&ptr))
+		return 0;
+
+	data = bpf_dynptr_data(&ptr, 0, sizeof(__u32));
+	if (!data)
+		return 0;
+
+	/* this should fail */
+	bpf_dynptr_from_mem(data, sizeof(__u32), 0, &ptr2);
+
+	return 0;
+}
+
+/* Cannot create dynptr from dynptr slice */
+SEC("?tc")
+__failure __msg("Unsupported reg type mem for bpf_dynptr_from_mem data")
+int dynptr_from_dynptr_slice(struct __sk_buff *skb)
+{
+	struct bpf_dynptr ptr, ptr2;
+	struct ethhdr *hdr;
+	char buffer[sizeof(*hdr)] = {};
+
+	bpf_dynptr_from_skb(skb, 0, &ptr);
+
+	hdr = bpf_dynptr_slice_rdwr(&ptr, 0, buffer, sizeof(buffer));
+	if (!hdr)
+		return SK_DROP;
+
+	/* this should fail */
+	bpf_dynptr_from_mem(hdr, sizeof(*hdr), 0, &ptr2);
+
+	return SK_PASS;
+}
+
 SEC("?tc")
 __failure __msg("cannot overwrite referenced dynptr") __log_level(2)
 int dynptr_pruning_overwrite(struct __sk_buff *ctx)
Signature · 1 line
2.52.0
author
#28 lore raw

PATCH v3 7/9 selftests/bpf: Test using dynptr after freeing the underlying object

In reply to Amery Hung

Make sure the verifier invalidates the dynptr and dynptr slice derived from an skb after the skb is freed.

---
 .../selftests/bpf/prog_tests/bpf_qdisc.c      |  6 ++
 .../progs/bpf_qdisc_fail__invalid_dynptr.c    | 68 +++++++++++++++++
 ...f_qdisc_fail__invalid_dynptr_cross_frame.c | 74 +++++++++++++++++++
 .../bpf_qdisc_fail__invalid_dynptr_slice.c    | 70 ++++++++++++++++++
 4 files changed, 218 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr_cross_frame.c
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr_slice.c
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +6 -0
index 730357cd0c9a..65277c8fc887 100644
@@ -8,6 +8,9 @@
 #include "bpf_qdisc_fifo.skel.h"
 #include "bpf_qdisc_fq.skel.h"
 #include "bpf_qdisc_fail__incompl_ops.skel.h"
+#include "bpf_qdisc_fail__invalid_dynptr.skel.h"
+#include "bpf_qdisc_fail__invalid_dynptr_slice.skel.h"
+#include "bpf_qdisc_fail__invalid_dynptr_cross_frame.skel.h"
 
 #define LO_IFINDEX 1
 
@@ -223,6 +226,9 @@ void test_ns_bpf_qdisc(void)
 		test_qdisc_attach_to_non_root();
 	if (test__start_subtest("incompl_ops"))
 		test_incompl_ops();
+	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr);
+	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr_cross_frame);
+	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr_slice);
 }
 
 void serial_test_bpf_qdisc_default(void)
tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr.c new file +68 -0

68 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr_cross_frame.c new file +74 -0

74 lines not on this page · raw message

tools/testing/selftests/bpf/progs/bpf_qdisc_fail__invalid_dynptr_slice.c new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.52.0
author
#29 lore raw

PATCH v3 8/9 selftests/bpf: Test using slice after invalidating dynptr clone

In reply to Amery Hung

The parent object of a cloned dynptr is skb not the original dynptr. Invalidate the original dynptr should not prevent the program from using the slice derived from the cloned dynptr.

---
 .../selftests/bpf/prog_tests/bpf_qdisc.c      |  2 +
 ..._qdisc_dynptr_use_after_invalidate_clone.c | 75 +++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/bpf_qdisc_dynptr_use_after_invalidate_clone.c
tools/testing/selftests/bpf/prog_tests/bpf_qdisc.c +2 -0
index 65277c8fc887..77f1c0550c9b 100644
@@ -11,6 +11,7 @@
 #include "bpf_qdisc_fail__invalid_dynptr.skel.h"
 #include "bpf_qdisc_fail__invalid_dynptr_slice.skel.h"
 #include "bpf_qdisc_fail__invalid_dynptr_cross_frame.skel.h"
+#include "bpf_qdisc_dynptr_use_after_invalidate_clone.skel.h"
 
 #define LO_IFINDEX 1
 
@@ -229,6 +230,7 @@ void test_ns_bpf_qdisc(void)
 	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr);
 	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr_cross_frame);
 	RUN_TESTS(bpf_qdisc_fail__invalid_dynptr_slice);
+	RUN_TESTS(bpf_qdisc_dynptr_use_after_invalidate_clone);
 }
 
 void serial_test_bpf_qdisc_default(void)
tools/testing/selftests/bpf/progs/bpf_qdisc_dynptr_use_after_invalidate_clone.c new file +75 -0

75 lines not on this page · raw message

Signature · 1 line
2.52.0
author
#30 lore raw

PATCH v3 9/9 selftests/bpf: Test using file dynptr after the reference on file is dropped

In reply to Amery Hung

File dynptr and slice should be invalidated when the parent file's reference is dropped in the program. Without the verifier tracking dyntpr's parent referenced object, the dynptr would continute to be incorrectly used even if the underlying file is being tear down or gone.

---
 .../selftests/bpf/progs/file_reader_fail.c    | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)
tools/testing/selftests/bpf/progs/file_reader_fail.c +60 -0
index 32fe28ed2439..a7102737abfe 100644
@@ -50,3 +50,63 @@ int xdp_no_dynptr_type(struct xdp_md *xdp)
 	bpf_dynptr_file_discard(&dynptr);
 	return 0;
 }
+
+SEC("lsm/file_open")
+__failure
+__msg("Expected an initialized dynptr as arg #2")
+int use_file_dynptr_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 buf[64];
+
+	if (!file)
+		return 0;
+
+	if (bpf_dynptr_from_file(file, 0, &dynptr))
+		goto out;
+
+	bpf_put_file(file);
+
+	/* this should fail - dynptr is invalid after file ref is dropped */
+	bpf_dynptr_read(buf, sizeof(buf), &dynptr, 0, 0);
+	return 0;
+
+out:
+	bpf_dynptr_file_discard(&dynptr);
+	bpf_put_file(file);
+	return 0;
+}
+
+SEC("lsm/file_open")
+__failure
+__msg("invalid mem access 'scalar'")
+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;
+
+	if (!file)
+		return 0;
+
+	if (bpf_dynptr_from_file(file, 0, &dynptr))
+		goto out;
+
+	data = bpf_dynptr_data(&dynptr, 0, 1);
+	if (!data)
+		goto out;
+
+	bpf_put_file(file);
+
+	/* this should fail - data slice is invalid after file ref is dropped */
+	*data = 'x';
+	return 0;
+
+out:
+	bpf_dynptr_file_discard(&dynptr);
+	bpf_put_file(file);
+	return 0;
+}
Signature · 1 line
2.52.0
#31 lore raw
In reply to Amery Hung
Quoted 11 lines · Amery Hung

Hi all,

This patchset cleans up dynptr handling, refactors object relationship tracking in the verifier by introducing parent_id, and fixes dynptr use-after-free bugs where file/skb dynptrs are not invalidated when the parent referenced object is freed.

* Motivation *

Very nice and approachable cover letter, thanks for improving it!

Quoted 5 lines

In BPF qdisc programs, an skb can be freed through kfuncs. However, since dynptr does not track the parent referenced object (e.g., skb), the verifier does not invalidate the dynptr after the skb is freed, resulting in use-after-free. The same issue also affects file dynptr.

[...]

#32 lore raw
In reply to Amery Hung
Quoted 11 lines · Amery Hung

(3) Referenced dynptr with referenced parent:

                     file (1,1,0)
                           ^ ^
     bpf_dynptr_from_file  | +-------------------------------+
                           |       bpf_dynptr_clone(A, C)    |
             dynptr A (2,3,1)                  dynptr C (4,3,1)
                         ^                                 ^
                         |                                 |
                         dynptr A and C have the same lifetime

If I understand the proposal correctly above should be:

                     file (1,0,0)
                           ^
     bpf_dynptr_from_file  +-------------------------------+

ref_obj_id will be inited bpf_dynptr_clone() as 3 into A and C. but original file->ref_obj_id will be zero.

?

author
#33 lore raw
Quoted 16 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung

(3) Referenced dynptr with referenced parent:

                     file (1,1,0)
                           ^ ^
     bpf_dynptr_from_file  | +-------------------------------+
                           |       bpf_dynptr_clone(A, C)    |
             dynptr A (2,3,1)                  dynptr C (4,3,1)
                         ^                                 ^
                         |                                 |
                         dynptr A and C have the same lifetime

If I understand the proposal correctly above should be:

Quoted 8 lines
                     file (1,0,0)
                           ^
     bpf_dynptr_from_file  +-------------------------------+

ref_obj_id will be inited bpf_dynptr_clone() as 3 into A and C. but original file->ref_obj_id will be zero.

?

This is the existing behavior. bpf_get_task_exe_file() is tagged with (KF_ACQUIRE | KF_RET_NULL). Therefore, file will have id and ref_obj_id both initialized to the same value. Here is the corresponding verifier code:

if (is_kfunc_acquire(&meta)) {
        int id = acquire_reference(env, insn_idx);

        if (id < 0)
                return id;
        if (is_kfunc_ret_null(&meta))
                regs[BPF_REG_0].id = id;
        regs[BPF_REG_0].ref_obj_id = id;
#34 lore raw
Quoted 32 lines · Amery Hung
Quoted 16 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung

(3) Referenced dynptr with referenced parent:

                     file (1,1,0)
                           ^ ^
     bpf_dynptr_from_file  | +-------------------------------+
                           |       bpf_dynptr_clone(A, C)    |
             dynptr A (2,3,1)                  dynptr C (4,3,1)
                         ^                                 ^
                         |                                 |
                         dynptr A and C have the same lifetime

If I understand the proposal correctly above should be:

Quoted 8 lines
                     file (1,0,0)
                           ^
     bpf_dynptr_from_file  +-------------------------------+

ref_obj_id will be inited bpf_dynptr_clone() as 3 into A and C. but original file->ref_obj_id will be zero.

?

This is the existing behavior. bpf_get_task_exe_file() is tagged with (KF_ACQUIRE | KF_RET_NULL). Therefore, file will have id and ref_obj_id both initialized to the same value. Here is the corresponding verifier code:

Quoted 8 lines
if (is_kfunc_acquire(&meta)) {
        int id = acquire_reference(env, insn_idx);

        if (id < 0)
                return id;
        if (is_kfunc_ret_null(&meta))
                regs[BPF_REG_0].id = id;
        regs[BPF_REG_0].ref_obj_id = id;

ok, but the arrow should still point to first '1', since that's what parent_id inherits?

#35 lore raw
Quoted 37 lines · Alexei Starovoitov
Quoted 35 lines · Amery Hung
Quoted 16 lines · Alexei Starovoitov
Quoted 11 lines · Amery Hung

(3) Referenced dynptr with referenced parent:

                     file (1,1,0)
                           ^ ^
     bpf_dynptr_from_file  | +-------------------------------+
                           |       bpf_dynptr_clone(A, C)    |
             dynptr A (2,3,1)                  dynptr C (4,3,1)
                         ^                                 ^
                         |                                 |
                         dynptr A and C have the same lifetime

If I understand the proposal correctly above should be:

Quoted 8 lines
                     file (1,0,0)
                           ^
     bpf_dynptr_from_file  +-------------------------------+

ref_obj_id will be inited bpf_dynptr_clone() as 3 into A and C. but original file->ref_obj_id will be zero.

?

This is the existing behavior. bpf_get_task_exe_file() is tagged with (KF_ACQUIRE | KF_RET_NULL). Therefore, file will have id and ref_obj_id both initialized to the same value. Here is the corresponding verifier code:

if (is_kfunc_acquire(&meta)) {
        int id = acquire_reference(env, insn_idx);
Quoted 8 lines
Quoted 5 lines
        if (id < 0)
                return id;
        if (is_kfunc_ret_null(&meta))
                regs[BPF_REG_0].id = id;
        regs[BPF_REG_0].ref_obj_id = id;

ok, but the arrow should still point to first '1', since that's what parent_id inherits?

Ohh yes. I totally missed the error. Thanks for pointing out.