Skip to contentSkip to conversation

bpf: Invalidate dynptr slices by dynptr_id on release

PATCH In review Xu Yunxiang 2 participants 2 messages Aug 28 1 message not in the archive

Subsystems (5)
lore
Share & tools
lore
#1 lore raw

PATCH 1/2 bpf: Invalidate dynptr slices by dynptr_id on release

In reply to a message not in the archive

unmark_stack_slots_dynptr() comments say it invalidates slices associated with the released dynptr, but the implementation only calls release_reference(ref_obj_id). bpf_dynptr_slice() / slice_rdwr() stamp dynptr_id and deliberately leave ref_obj_id at 0, so those slices survive bpf_ringbuf_submit_dynptr() / discard_dynptr() and the verifier still allows LDX/STX through them. bpf_dynptr_data() slices are already covered because they carry ref_obj_id (see data_slice_use_after_release1).

Walk registers with a matching dynptr_id, the same way destroy_if_dynptr_stack_slot() already does when a dynptr stack slot is overwritten. Do this for the released dynptr, for each clone, and for the non-refcounted path.

This is the minimal stable equivalent of the slice half of 308c7a0ae8859 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug"). That series is an 11-patch bpf-next refactor (parent_id, DFS, folding ref_obj_id into id) aimed at qdisc/file parent-object UAF; it is not a viable cherry-pick for 6.6.y / 6.12.y / 6.18.y. This hunk is.

The same change applies to 6.6.y and 6.18.y; unmark_stack_slots_dynptr() is the same shape there.

Cc [email protected] # 6.6.y 6.12.y 6.18.y
---
 kernel/bpf/verifier.c | 32 +++++++++++++++++++++
 tools/testing/selftests/bpf/progs/dynptr_fail.c | 25 +++++++++++++++++++++
 2 files changed
kernel/bpf/verifier.c +30 -2
@@ -805,6 +805,9 @@
 	state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN;
 }
 
+static void mark_reg_invalid(const struct bpf_verifier_env *env, struct bpf_reg_state *reg);
+static void invalidate_slices_of_dynptr(struct bpf_verifier_env *env, int dynptr_id);
+
 static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
 {
 	struct bpf_func_state *state = func(env, reg);
@@ -815,6 +818,7 @@
 		return spi;
 
 	if (!dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {
+		invalidate_slices_of_dynptr(env, state->stack[spi].spilled_ptr.id);
 		invalidate_dynptr(env, state, spi);
 		return 0;
 	}
@@ -828,8 +832,16 @@
 	 * 2) Any slices derived from this dynptr.
 	 */
 
-	/* Invalidate any slices associated with this dynptr */
+	/* Invalidate any slices associated with this dynptr.
+	 * release_reference() only walks registers that carry ref_obj_id;
+	 * slices are PTR_TO_MEM with dynptr_id set and ref_obj_id left 0
+	 * (the stamping comment talks about packet slices, but the code
+	 * applies to every dynptr type, including refcounted ringbuf).
+	 * Mirror destroy_if_dynptr_stack_slot(), which already invalidates
+	 * slices by dynptr_id.
+	 */
 	WARN_ON_ONCE(release_reference(env, ref_obj_id));
+	invalidate_slices_of_dynptr(env, state->stack[spi].spilled_ptr.id);
 
 	/* Invalidate any dynptr clones */
 	for (i = 1; i < state->allocated_stack / BPF_REG_SIZE; i++) {
@@ -844,8 +856,10 @@
 			verbose(env, "verifier internal error: misconfigured ref_obj_id\n");
 			return -EFAULT;
 		}
-		if (state->stack[i].spilled_ptr.dynptr.first_slot)
+		if (state->stack[i].spilled_ptr.dynptr.first_slot) {
+			invalidate_slices_of_dynptr(env, state->stack[i].spilled_ptr.id);
 			invalidate_dynptr(env, state, i);
+		}
 	}
 
 	return 0;
@@ -862,6 +876,20 @@
 		__mark_reg_unknown(env, reg);
 }
 
+static void invalidate_slices_of_dynptr(struct bpf_verifier_env *env, int dynptr_id)
+{
+	struct bpf_func_state *fstate;
+	struct bpf_reg_state *dreg;
+
+	bpf_for_each_reg_in_vstate(env->cur_state, fstate, dreg, ({
+		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);
+	}));
+}
+
+
 static int destroy_if_dynptr_stack_slot(struct bpf_verifier_env *env,
 				        struct bpf_func_state *state, int spi)
 {
tools/testing/selftests/bpf/progs/dynptr_fail.c +25 -0
@@ -316,6 +316,31 @@
 	return 0;
 }
 
+/* bpf_dynptr_slice() does not stamp ref_obj_id (unlike bpf_dynptr_data).
+ * submit/discard must still invalidate those slices by dynptr_id.
+ */
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int slice_kfunc_use_after_submit(void *ctx)
+{
+	struct bpf_dynptr ptr;
+	struct sample *sample;
+
+	bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(*sample), 0, &ptr);
+	sample = bpf_dynptr_slice_rdwr(&ptr, 0, NULL, sizeof(*sample));
+	if (!sample)
+		goto done;
+
+	sample->pid = 123;
+	bpf_ringbuf_submit_dynptr(&ptr, 0);
+	/* this should fail */
+	val = sample->pid;
+	return 0;
+done:
+	bpf_ringbuf_discard_dynptr(&ptr, 0);
+	return 0;
+}
+
 /* A data slice can't be used after it has been released.
  *
  * This tests the case where the data slice tracks a dynptr (ptr2)
Signature · 1 line
BPF-SemSan disclosure follow-up
#2 lore raw
Quoted 19 lines · Xu Yunxiang

unmark_stack_slots_dynptr() comments say it invalidates slices associated with the released dynptr, but the implementation only calls release_reference(ref_obj_id). bpf_dynptr_slice() / slice_rdwr() stamp dynptr_id and deliberately leave ref_obj_id at 0, so those slices survive bpf_ringbuf_submit_dynptr() / discard_dynptr() and the verifier still allows LDX/STX through them. bpf_dynptr_data() slices are already covered because they carry ref_obj_id (see data_slice_use_after_release1).

Walk registers with a matching dynptr_id, the same way destroy_if_dynptr_stack_slot() already does when a dynptr stack slot is overwritten. Do this for the released dynptr, for each clone, and for the non-refcounted path.

This is the minimal stable equivalent of the slice half of 308c7a0ae8859 ("bpf: Refactor object relationship tracking and fix dynptr UAF bug"). That series is an 11-patch bpf-next refactor (parent_id, DFS, folding ref_obj_id into id) aimed at qdisc/file parent-object UAF; it is not a viable cherry-pick for 6.6.y / 6.12.y / 6.18.y. This hunk is.

Quoted 6 lines

The same change applies to 6.6.y and 6.18.y; unmark_stack_slots_dynptr() is the same shape there.

Cc [email protected] # 6.6.y 6.12.y 6.18.y

And again, no authorship :(