Skip to contentSkip to conversation

bpf: Fix ref_obj_id for dynptr data slices in verifier

PATCH Superseded Joanne Koong 4 participants 9 messages Jul 21, 2022 – Jul 22, 2022

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bpf: Fix ref_obj_id for dynptr data slices in verifier

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");
---
 kernel/bpf/verifier.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
kernel/bpf/verifier.c +17 -13
index c59c3df0fea6..00f9b5a77734 100644
@@ -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);
Signature · 1 line
2.30.2
#1 lore raw

PATCH 2/2 selftests/bpf: add extra test for using dynptr data slice after release

Add an additional test, "data_slice_use_after_release2", for ensuring that data slices are correctly invalidated by the verifier after the dynptr whose ref obj id they track is released. In particular, this tests data slice invalidation for dynptrs located at a non-zero offset from the frame pointer.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  3 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 32 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +2 -1
index 3c7aa82b98e2..bcf80b9f7c27 100644
@@ -22,7 +22,8 @@ static struct {
 	{"add_dynptr_to_map2", "invalid indirect read from stack"},
 	{"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"},
 	{"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"},
-	{"data_slice_use_after_release", "invalid mem access 'scalar'"},
+	{"data_slice_use_after_release1", "invalid mem access 'scalar'"},
+	{"data_slice_use_after_release2", "invalid mem access 'scalar'"},
 	{"data_slice_missing_null_check1", "invalid mem access 'mem_or_null'"},
 	{"data_slice_missing_null_check2", "invalid mem access 'mem_or_null'"},
 	{"invalid_helper1", "invalid indirect read from stack"},
tools/testing/selftests/bpf/progs/dynptr_fail.c +31 -1
index d811cff73597..d8c4ed3ee146 100644
@@ -248,7 +248,7 @@ int data_slice_out_of_bounds_map_value(void *ctx)
 
 /* A data slice can't be used after it has been released */
 SEC("?raw_tp/sys_nanosleep")
-int data_slice_use_after_release(void *ctx)
+int data_slice_use_after_release1(void *ctx)
 {
 	struct bpf_dynptr ptr;
 	struct sample *sample;
@@ -272,6 +272,36 @@ int data_slice_use_after_release(void *ctx)
 	return 0;
 }
 
+SEC("?raw_tp/sys_nanosleep")
+int data_slice_use_after_release2(void *ctx)
+{
+	struct bpf_dynptr ptr1, ptr2;
+	struct sample *sample;
+
+	bpf_ringbuf_reserve_dynptr(&ringbuf, 64, 0, &ptr1);
+	bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(*sample), 0, &ptr2);
+
+	sample = bpf_dynptr_data(&ptr2, 0, sizeof(*sample));
+	if (!sample)
+		goto done;
+
+	sample->pid = 23;
+
+	bpf_ringbuf_submit_dynptr(&ptr2, 0);
+
+	/* this should fail */
+	sample->pid = 23;
+
+	bpf_ringbuf_submit_dynptr(&ptr1, 0);
+
+	return 0;
+
+done:
+	bpf_ringbuf_discard_dynptr(&ptr2, 0);
+	bpf_ringbuf_discard_dynptr(&ptr1, 0);
+	return 0;
+}
+
 /* A data slice must be first checked for NULL */
 SEC("?raw_tp/sys_nanosleep")
 int data_slice_missing_null_check1(void *ctx)
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH bpf-next v1 2/2] selftests/bpf: add extra test for using dynptr data slice after release

Hi Joanne,

Quoted 41 lines · Joanne Koong

Add an additional test, "data_slice_use_after_release2", for ensuring that data slices are correctly invalidated by the verifier after the dynptr whose ref obj id they track is released. In particular, this tests data slice invalidation for dynptrs located at a non-zero offset from the frame pointer.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  3 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 32 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +2 -1
index 3c7aa82b98e2..bcf80b9f7c27 100644
@@ -22,7 +22,8 @@ static struct {
        {"add_dynptr_to_map2", "invalid indirect read from stack"},
        {"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"},
        {"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"},
-       {"data_slice_use_after_release", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release1", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release2", "invalid mem access 'scalar'"},
        {"data_slice_missing_null_check1", "invalid mem access 'mem_or_null'"},
        {"data_slice_missing_null_check2", "invalid mem access 'mem_or_null'"},
        {"invalid_helper1", "invalid indirect read from stack"},
tools/testing/selftests/bpf/progs/dynptr_fail.c +1 -1
index d811cff73597..d8c4ed3ee146 100644
@@ -248,7 +248,7 @@ int data_slice_out_of_bounds_map_value(void *ctx)
 
 /* A data slice can't be used after it has been released */
 SEC("?raw_tp/sys_nanosleep")
-int data_slice_use_after_release(void *ctx)
+int data_slice_use_after_release1(void *ctx)
 {
        struct bpf_dynptr ptr;
        struct sample *sample;

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 276

Quoted 5 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -0
@@ -272,6 +272,36 @@ int data_slice_use_after_release(void *ctx)
        return 0;
 }
 
+SEC("?raw_tp/sys_nanosleep")
+int data_slice_use_after_release2(void *ctx)

Could you put comments explaining the reason for failure, like other test cases?

Quoted 18 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +18 -0
+{
+       struct bpf_dynptr ptr1, ptr2;
+       struct sample *sample;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, 64, 0, &ptr1);
+       bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(*sample), 0, &ptr2);
+
+       sample = bpf_dynptr_data(&ptr2, 0, sizeof(*sample));
+       if (!sample)
+               goto done;
+
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr2, 0);
+
+       /* this should fail */
+       sample->pid = 23;
+

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 304

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +10 -0
+       bpf_ringbuf_submit_dynptr(&ptr1, 0);
+
+       return 0;
+
+done:
+       bpf_ringbuf_discard_dynptr(&ptr2, 0);
+       bpf_ringbuf_discard_dynptr(&ptr1, 0);
+       return 0;
+}
+

Joanne, I haven't been following the effort of dynptr, so I am still learning dynptr. Is there any use of ptr1 in this test case?

Quoted 6 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +0 -1
 /* A data slice must be first checked for NULL */
 SEC("?raw_tp/sys_nanosleep")
 int data_slice_missing_null_check1(void *ctx)
--

2.30.2

#3 lore raw

Re: [PATCH bpf-next v1 2/2] selftests/bpf: add extra test for using dynptr data slice after release

Quoted 48 lines · Hao Luo

Hi Joanne,

Quoted 44 lines · Joanne Koong

Add an additional test, "data_slice_use_after_release2", for ensuring that data slices are correctly invalidated by the verifier after the dynptr whose ref obj id they track is released. In particular, this tests data slice invalidation for dynptrs located at a non-zero offset from the frame pointer.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  3 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 32 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +2 -1
index 3c7aa82b98e2..bcf80b9f7c27 100644
@@ -22,7 +22,8 @@ static struct {
        {"add_dynptr_to_map2", "invalid indirect read from stack"},
        {"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"},
        {"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"},
-       {"data_slice_use_after_release", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release1", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release2", "invalid mem access 'scalar'"},
        {"data_slice_missing_null_check1", "invalid mem access 'mem_or_null'"},
        {"data_slice_missing_null_check2", "invalid mem access 'mem_or_null'"},
        {"invalid_helper1", "invalid indirect read from stack"},
tools/testing/selftests/bpf/progs/dynptr_fail.c +1 -1
index d811cff73597..d8c4ed3ee146 100644
@@ -248,7 +248,7 @@ int data_slice_out_of_bounds_map_value(void *ctx)
 
 /* A data slice can't be used after it has been released */
 SEC("?raw_tp/sys_nanosleep")
-int data_slice_use_after_release(void *ctx)
+int data_slice_use_after_release1(void *ctx)
 {
        struct bpf_dynptr ptr;
        struct sample *sample;
@@ -272,6 +272,36 @@ int data_slice_use_after_release(void *ctx)
        return 0;
 }
 
Quoted 5 lines

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 276

Quoted 2 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -0
+SEC("?raw_tp/sys_nanosleep")
+int data_slice_use_after_release2(void *ctx)

Could you put comments explaining the reason for failure, like other test cases?

Hi Hao. The explanation for the data_slice_use_after_release test cases is above the "data_slice_use_after_release1" case, but I can also copy/paste that comment to above "data_slice_use_after_release2" as well to make it easier to spot. I'll do that for v2.

Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +23 -0
+{
+       struct bpf_dynptr ptr1, ptr2;
+       struct sample *sample;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, 64, 0, &ptr1);
+       bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(*sample), 0, &ptr2);
+
+       sample = bpf_dynptr_data(&ptr2, 0, sizeof(*sample));
+       if (!sample)
+               goto done;
+
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr2, 0);
+
+       /* this should fail */
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr1, 0);
+
+       return 0;
+
+done:
Quoted 8 lines

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 304

Quoted 5 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +5 -0
+       bpf_ringbuf_discard_dynptr(&ptr2, 0);
+       bpf_ringbuf_discard_dynptr(&ptr1, 0);
+       return 0;
+}
+

Joanne, I haven't been following the effort of dynptr, so I am still learning dynptr. Is there any use of ptr1 in this test case?

The use of ptr1 is so that ptr2 will be at a non-zero offset from the frame pointer. This bug previously was unspotted because we were only testing invalidated data slices for ptrs that were at a zero offset.

I will include a comment about this in the test to make it more clear :)

Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +0 -1
 /* A data slice must be first checked for NULL */
 SEC("?raw_tp/sys_nanosleep")
 int data_slice_missing_null_check1(void *ctx)
--

2.30.2

#4 lore raw

Re: [PATCH bpf-next v1 2/2] selftests/bpf: add extra test for using dynptr data slice after release

Quoted 55 lines · Joanne Koong
Quoted 48 lines · Hao Luo

Hi Joanne,

Quoted 44 lines · Joanne Koong

Add an additional test, "data_slice_use_after_release2", for ensuring that data slices are correctly invalidated by the verifier after the dynptr whose ref obj id they track is released. In particular, this tests data slice invalidation for dynptrs located at a non-zero offset from the frame pointer.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  3 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 32 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +2 -1
index 3c7aa82b98e2..bcf80b9f7c27 100644
@@ -22,7 +22,8 @@ static struct {
        {"add_dynptr_to_map2", "invalid indirect read from stack"},
        {"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"},
        {"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"},
-       {"data_slice_use_after_release", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release1", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release2", "invalid mem access 'scalar'"},
        {"data_slice_missing_null_check1", "invalid mem access 'mem_or_null'"},
        {"data_slice_missing_null_check2", "invalid mem access 'mem_or_null'"},
        {"invalid_helper1", "invalid indirect read from stack"},
tools/testing/selftests/bpf/progs/dynptr_fail.c +1 -1
index d811cff73597..d8c4ed3ee146 100644
@@ -248,7 +248,7 @@ int data_slice_out_of_bounds_map_value(void *ctx)
 
 /* A data slice can't be used after it has been released */
 SEC("?raw_tp/sys_nanosleep")
-int data_slice_use_after_release(void *ctx)
+int data_slice_use_after_release1(void *ctx)
 {
        struct bpf_dynptr ptr;
        struct sample *sample;
@@ -272,6 +272,36 @@ int data_slice_use_after_release(void *ctx)
        return 0;
 }
 
Quoted 5 lines

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 276

Quoted 2 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -0
+SEC("?raw_tp/sys_nanosleep")
+int data_slice_use_after_release2(void *ctx)

Could you put comments explaining the reason for failure, like other test cases?

Quoted 4 lines

Hi Hao. The explanation for the data_slice_use_after_release test cases is above the "data_slice_use_after_release1" case, but I can also copy/paste that comment to above "data_slice_use_after_release2" as well to make it easier to spot. I'll do that for v2.

Thanks Joanne, appreciate the comment about the cause of failure! I noticed that in other test cases like

ringbuf_missing_release1()
ringbuf_missing_release2()

There is a general description before the release1, and more details inside each function. Maybe following that convention is better? Sorry for having many requests from me. :)

Quoted 32 lines
Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +23 -0
+{
+       struct bpf_dynptr ptr1, ptr2;
+       struct sample *sample;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, 64, 0, &ptr1);
+       bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(*sample), 0, &ptr2);
+
+       sample = bpf_dynptr_data(&ptr2, 0, sizeof(*sample));
+       if (!sample)
+               goto done;
+
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr2, 0);
+
+       /* this should fail */
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr1, 0);
+
+       return 0;
+
+done:
Quoted 8 lines

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 304

Quoted 5 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +5 -0
+       bpf_ringbuf_discard_dynptr(&ptr2, 0);
+       bpf_ringbuf_discard_dynptr(&ptr1, 0);
+       return 0;
+}
+

Joanne, I haven't been following the effort of dynptr, so I am still learning dynptr. Is there any use of ptr1 in this test case?

Quoted 6 lines

The use of ptr1 is so that ptr2 will be at a non-zero offset from the frame pointer. This bug previously was unspotted because we were only testing invalidated data slices for ptrs that were at a zero offset.

I will include a comment about this in the test to make it more clear :)

Sounds good. It would be great if we can fix that bug and spare the use of ptr1 here.

Thanks!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +0 -1
 /* A data slice must be first checked for NULL */
 SEC("?raw_tp/sys_nanosleep")
 int data_slice_missing_null_check1(void *ctx)
--

2.30.2

#5 lore raw

Re: [PATCH bpf-next v1 2/2] selftests/bpf: add extra test for using dynptr data slice after release

Quoted 108 lines · Hao Luo
Quoted 55 lines · Joanne Koong
Quoted 48 lines · Hao Luo

Hi Joanne,

Quoted 44 lines · Joanne Koong

Add an additional test, "data_slice_use_after_release2", for ensuring that data slices are correctly invalidated by the verifier after the dynptr whose ref obj id they track is released. In particular, this tests data slice invalidation for dynptrs located at a non-zero offset from the frame pointer.

---
 .../testing/selftests/bpf/prog_tests/dynptr.c |  3 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c | 32 ++++++++++++++++++-
 2 files changed, 33 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/dynptr.c +2 -1
index 3c7aa82b98e2..bcf80b9f7c27 100644
@@ -22,7 +22,8 @@ static struct {
        {"add_dynptr_to_map2", "invalid indirect read from stack"},
        {"data_slice_out_of_bounds_ringbuf", "value is outside of the allowed memory range"},
        {"data_slice_out_of_bounds_map_value", "value is outside of the allowed memory range"},
-       {"data_slice_use_after_release", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release1", "invalid mem access 'scalar'"},
+       {"data_slice_use_after_release2", "invalid mem access 'scalar'"},
        {"data_slice_missing_null_check1", "invalid mem access 'mem_or_null'"},
        {"data_slice_missing_null_check2", "invalid mem access 'mem_or_null'"},
        {"invalid_helper1", "invalid indirect read from stack"},
tools/testing/selftests/bpf/progs/dynptr_fail.c +1 -1
index d811cff73597..d8c4ed3ee146 100644
@@ -248,7 +248,7 @@ int data_slice_out_of_bounds_map_value(void *ctx)
 
 /* A data slice can't be used after it has been released */
 SEC("?raw_tp/sys_nanosleep")
-int data_slice_use_after_release(void *ctx)
+int data_slice_use_after_release1(void *ctx)
 {
        struct bpf_dynptr ptr;
        struct sample *sample;
@@ -272,6 +272,36 @@ int data_slice_use_after_release(void *ctx)
        return 0;
 }
 
Quoted 5 lines

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 276

Quoted 2 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -0
+SEC("?raw_tp/sys_nanosleep")
+int data_slice_use_after_release2(void *ctx)

Could you put comments explaining the reason for failure, like other test cases?

Quoted 4 lines

Hi Hao. The explanation for the data_slice_use_after_release test cases is above the "data_slice_use_after_release1" case, but I can also copy/paste that comment to above "data_slice_use_after_release2" as well to make it easier to spot. I'll do that for v2.

Thanks Joanne, appreciate the comment about the cause of failure! I noticed that in other test cases like

ringbuf_missing_release1()
ringbuf_missing_release2()

There is a general description before the release1, and more details inside each function. Maybe following that convention is better? Sorry for having many requests from me. :)

Quoted 36 lines
Quoted 23 lines
Quoted 23 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +23 -0
+{
+       struct bpf_dynptr ptr1, ptr2;
+       struct sample *sample;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, 64, 0, &ptr1);
+       bpf_ringbuf_reserve_dynptr(&ringbuf, sizeof(*sample), 0, &ptr2);
+
+       sample = bpf_dynptr_data(&ptr2, 0, sizeof(*sample));
+       if (!sample)
+               goto done;
+
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr2, 0);
+
+       /* this should fail */
+       sample->pid = 23;
+
+       bpf_ringbuf_submit_dynptr(&ptr1, 0);
+
+       return 0;
+
+done:
Quoted 8 lines

tools/testing/selftests/bpf/progs/dynptr_fail.c int data_slice_use_after_release(void *ctx) · line 304

Quoted 5 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +5 -0
+       bpf_ringbuf_discard_dynptr(&ptr2, 0);
+       bpf_ringbuf_discard_dynptr(&ptr1, 0);
+       return 0;
+}
+

Joanne, I haven't been following the effort of dynptr, so I am still learning dynptr. Is there any use of ptr1 in this test case?

The use of ptr1 is so that ptr2 will be at a non-zero offset from the frame pointer. This bug previously was unspotted because we were only testing invalidated data slices for ptrs that were at a zero offset.

Quoted 5 lines
Quoted 2 lines

I will include a comment about this in the test to make it more clear :)

Sounds good. It would be great if we can fix that bug and spare the use of ptr1 here.

Sorry, I don't mean we should do it in this patchset.

Quoted 10 lines

Thanks!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +0 -1
 /* A data slice must be first checked for NULL */
 SEC("?raw_tp/sys_nanosleep")
 int data_slice_missing_null_check1(void *ctx)
--

2.30.2

Acked-by on v1 1/2
#6 lore raw
In reply to Joanne Koong
Quoted 8 lines · 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.

Quoted 6 lines

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");

LGTM

jirka

Quoted 56 lines
---
 kernel/bpf/verifier.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
kernel/bpf/verifier.c +17 -13
index c59c3df0fea6..00f9b5a77734 100644
@@ -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);
Signature · 1 line
2.30.2
#7 lore raw
In reply to Joanne Koong
Quoted 30 lines · 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");
---
 kernel/bpf/verifier.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
kernel/bpf/verifier.c +4 -0
index c59c3df0fea6..00f9b5a77734 100644
@@ -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.
+		 *

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7357

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+		 * 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]);

check_func_arg() is setting meta->ref_obj_id for each arg. Can this meta.ref_obj_id assignment be done in check_func_arg() instead of looping all args again here.

Quoted 14 lines
kernel/bpf/verifier.c +2 -5
+			}
+		}
 	}
 
 	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])) {

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7490

Quoted 10 lines
kernel/bpf/verifier.c +1 -8
-				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;

nit. This will be the same as the earlier is_ptr_cast_function(). Merge the if statements ?

Quoted 6 lines
kernel/bpf/verifier.c +0 -0
 	}
 
 	do_refine_retval_range(regs, fn->ret_type, func_id, &meta);
Signature · 1 line
2.30.2
#8 lore raw
Quoted 37 lines · Martin KaFai Lau
Quoted 35 lines · 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");
---
 kernel/bpf/verifier.c | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)
kernel/bpf/verifier.c +9 -0
index c59c3df0fea6..00f9b5a77734 100644
@@ -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])) {
Quoted 9 lines

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7357

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+                             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]);

check_func_arg() is setting meta->ref_obj_id for each arg. Can this meta.ref_obj_id assignment be done in check_func_arg() instead of looping all args again here.

I think so! Will update for v2.

Quoted 18 lines
Quoted 18 lines
kernel/bpf/verifier.c +2 -9
+                     }
+             }
      }
 
      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;
-                             }
Quoted 8 lines

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7490

Quoted 6 lines
kernel/bpf/verifier.c +1 -4
-                             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;

nit. This will be the same as the earlier is_ptr_cast_function(). Merge the if statements ?

Will do for v2

Quoted 4 lines

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 7493

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
      }
 
      do_refine_retval_range(regs, fn->ret_type, func_id, &meta);

Thanks for taking a look at this patch, Jiri and Martin!

Quoted 3 lines
Quoted 3 lines

--
2.30.2