Skip to contentSkip to conversation

bpf: Invalidate dynptr clones in every call frame on release

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

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

PATCH 2/2 bpf: Invalidate dynptr clones in every call frame on release

In reply to a message not in the archive

unmark_stack_slots_dynptr() walks clones only in the submitting pointer's frame. A program that clones a ringbuf dynptr in a subprog, submits the clone, then submits the original in the caller is therefore
accepted: the caller's STACK_DYNPTR slot is never cleared. The second bpf_ringbuf_commit() flips BUSY on an already-committed header and a userspace consumer stalls.

Walk every frame and invalidate matching STACK_DYNPTR slots, the same per-slot work destroy_if_dynptr_stack_slot() already does in one frame. Leave the existing WARN_ON_ONCE(release_reference()) as a swallow: a hard -EINVAL would reject legitimate programs that release once in a different frame than they acquired (6.6/6.12/6.13), and on 6.14+ the second lookup fails because the ref was already deleted.

Depends on "bpf: Invalidate dynptr slices by dynptr_id on release" only for the slice helper call inside the new loop; the frame walk is independent.

This is the minimal stable equivalent of the cross-frame half of 308c7a0ae8859. That refactor is not a viable cherry-pick for stable.

Cc [email protected] # 6.6.y 6.12.y 6.18.y
---
 kernel/bpf/verifier.c | 43 +++++++++++++++++++++
 1 file changed
kernel/bpf/verifier.c +27 -16
@@ -811,7 +811,7 @@
 static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
 {
 	struct bpf_func_state *state = func(env, reg);
-	int spi, ref_obj_id, i;
+	int spi, ref_obj_id, i, frm;
 
 	spi = dynptr_get_spi(env, reg);
 	if (spi < 0)
@@ -843,22 +843,33 @@
 	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++) {
-		if (state->stack[i].spilled_ptr.ref_obj_id != ref_obj_id)
-			continue;
+	/* Invalidate any dynptr clones, including those in other frames.
+	 * acquired_refs is per-frame on 6.6/6.12/6.13; a clone submitted in a
+	 * callee must still clear the caller's STACK_DYNPTR slot. Do not turn
+	 * a failing release_reference() into -EINVAL: that rejects legitimate
+	 * cross-frame single releases on those trees.
+	 */
+	for (frm = 0; frm <= env->cur_state->curframe; frm++) {
+		struct bpf_func_state *f = env->cur_state->frame[frm];
 
-		/* 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) {
-			verbose(env, "verifier internal error: misconfigured ref_obj_id\n");
-			return -EFAULT;
-		}
-		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);
+		if (!f)
+			continue;
+		for (i = 1; i < f->allocated_stack / BPF_REG_SIZE; i++) {
+			if (f->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 (f->stack[i].slot_type[0] != STACK_DYNPTR) {
+				verbose(env, "verifier internal error: misconfigured ref_obj_id\n");
+				return -EFAULT;
+			}
+			if (f->stack[i].spilled_ptr.dynptr.first_slot) {
+				invalidate_slices_of_dynptr(env, f->stack[i].spilled_ptr.id);
+				invalidate_dynptr(env, f, i);
+			}
 		}
 	}
 
Signature · 1 line
BPF-SemSan disclosure follow-up
#2 lore raw
Quoted 21 lines · Xu Yunxiang

unmark_stack_slots_dynptr() walks clones only in the submitting pointer's frame. A program that clones a ringbuf dynptr in a subprog, submits the clone, then submits the original in the caller is therefore
accepted: the caller's STACK_DYNPTR slot is never cleared. The second bpf_ringbuf_commit() flips BUSY on an already-committed header and a userspace consumer stalls.

Walk every frame and invalidate matching STACK_DYNPTR slots, the same per-slot work destroy_if_dynptr_stack_slot() already does in one frame. Leave the existing WARN_ON_ONCE(release_reference()) as a swallow: a hard -EINVAL would reject legitimate programs that release once in a different frame than they acquired (6.6/6.12/6.13), and on 6.14+ the second lookup fails because the ref was already deleted.

Depends on "bpf: Invalidate dynptr slices by dynptr_id on release" only for the slice helper call inside the new loop; the frame walk is independent.

This is the minimal stable equivalent of the cross-frame half of 308c7a0ae8859. That refactor is not a viable cherry-pick for stable.

Quoted 6 lines
Cc [email protected] # 6.6.y 6.12.y 6.18.y
---
 kernel/bpf/verifier.c | 43 +++++++++++++++++++++
 1 file changed

Same here, you lost the authorship and signed-off-by of the original.

greg k-h