BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v1 1/2] bpf: Fix ref_obj_id for dynptr data slices in verifier
@ 2022-07-21  2:48 Joanne Koong
  2022-07-21  2:48 ` [PATCH bpf-next v1 2/2] selftests/bpf: add extra test for using dynptr data slice after release Joanne Koong
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Joanne Koong @ 2022-07-21  2:48 UTC (permalink / raw)
  To: bpf; +Cc: andrii, daniel, ast, Joanne Koong

When a data slice is obtained from a dynptr (through the bpf_dynptr_data API),
the ref obj id of the dynptr must be found and then associated with the data
slice.

The ref obj id of the dynptr must be found *before* the caller saved regs are
reset. Without this fix, the ref obj id tracking is not correct for
dynptrs that are at an offset from the frame pointer.

Please also note that the data slice's ref obj id must be assigned after the
ret types are parsed, since RET_PTR_TO_ALLOC_MEM-type return regs get
zero-marked.

Fixes: 34d4ef5775f7("bpf: Add dynptr data slices");
Signed-off-by: Joanne Koong <joannelkoong@gmail.com>
---
 kernel/bpf/verifier.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index c59c3df0fea6..00f9b5a77734 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -7341,6 +7341,22 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 			}
 		}
 		break;
+	case BPF_FUNC_dynptr_data:
+		/* Find the id of the dynptr we're tracking the reference of.
+		 * We must do this before we reset caller saved regs.
+		 *
+		 * Please note as well that meta.ref_obj_id after the check_func_arg() calls doesn't
+		 * already contain the dynptr ref obj id, since dynptrs are stored on the stack.
+		 */
+		for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
+			if (arg_type_is_dynptr(fn->arg_type[i])) {
+				if (meta.ref_obj_id) {
+					verbose(env, "verifier internal error: multiple refcounted args in func\n");
+					return -EFAULT;
+				}
+				meta.ref_obj_id = stack_slot_get_id(env, &regs[BPF_REG_1 + i]);
+			}
+		}
 	}
 
 	if (err)
@@ -7470,20 +7486,8 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		/* For release_reference() */
 		regs[BPF_REG_0].ref_obj_id = id;
 	} else if (func_id == BPF_FUNC_dynptr_data) {
-		int dynptr_id = 0, i;
-
-		/* Find the id of the dynptr we're acquiring a reference to */
-		for (i = 0; i < MAX_BPF_FUNC_REG_ARGS; i++) {
-			if (arg_type_is_dynptr(fn->arg_type[i])) {
-				if (dynptr_id) {
-					verbose(env, "verifier internal error: multiple dynptr args in func\n");
-					return -EFAULT;
-				}
-				dynptr_id = stack_slot_get_id(env, &regs[BPF_REG_1 + i]);
-			}
-		}
 		/* For release_reference() */
-		regs[BPF_REG_0].ref_obj_id = dynptr_id;
+		regs[BPF_REG_0].ref_obj_id = meta.ref_obj_id;
 	}
 
 	do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-07-22 19:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-21  2:48 [PATCH bpf-next v1 1/2] bpf: Fix ref_obj_id for dynptr data slices in verifier Joanne Koong
2022-07-21  2:48 ` [PATCH bpf-next v1 2/2] selftests/bpf: add extra test for using dynptr data slice after release Joanne Koong
2022-07-21 17:27   ` Hao Luo
2022-07-22 16:40     ` Joanne Koong
2022-07-22 19:25       ` Hao Luo
2022-07-22 19:33         ` Hao Luo
2022-07-21  7:50 ` [PATCH bpf-next v1 1/2] bpf: Fix ref_obj_id for dynptr data slices in verifier Jiri Olsa
2022-07-21 17:02 ` Martin KaFai Lau
2022-07-22 16:52   ` Joanne Koong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox