Skip to contentSkip to conversation

Dynptr helpers

PATCH Applied Joanne Koong 6 participants 23 messages Apr 20, 2023 – Apr 28, 2023

Subsystems (9)
Networking Kselftest BPF Tracing +5 more subsystems
lore
Share & tools
lore
#0 lore raw

v2 cover Dynptr helpers

This patchset is the 3rd in the dynptr series. The 1st (dynptr fundamentals) can be found here [0] and the second (skb + xdp dynptrs) can be found here [1].

This patchset adds the following helpers for interacting with dynptrs:

int bpf_dynptr_adjust(struct bpf_dynptr *ptr, __u32 start, __u32 end) __ksym; int bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym;
int bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym;
__u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym;
int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym;

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

v1 -> v2:
v1: https://lore.kernel.org/bpf/[email protected]/
* change bpf_dynptr_advance/trim to bpf_dynptr_adjust
* rename bpf_dynptr_get_size to bpf_dynptr_size
* refactor handling clone for process_dynptr_func, maintain unique ids for parent and clone
* remove bpf_dynptr_get_offset()

Joanne Koong (5):
bpf: Add bpf_dynptr_adjust
bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly
bpf: Add bpf_dynptr_size
bpf: Add bpf_dynptr_clone
selftests/bpf: add tests for dynptr convenience helpers

 include/linux/bpf.h                           |   2 +-
 kernel/bpf/helpers.c                          |  76 ++++-
 kernel/bpf/verifier.c                         | 105 ++++--
 kernel/trace/bpf_trace.c                      |   4 +-
 tools/testing/selftests/bpf/bpf_kfuncs.h      |   6 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |   6 +
 .../testing/selftests/bpf/progs/dynptr_fail.c | 287 +++++++++++++++++
 .../selftests/bpf/progs/dynptr_success.c      | 298 ++++++++++++++++++
 8 files changed, 755 insertions(+), 29 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/5 bpf: Add bpf_dynptr_adjust

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +26 -0
index 00e5fb0682ac..7ddf63ac93ce 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+	u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+	ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
 	return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2297,6 +2304,24 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
 	return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
+__bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)
+{
+	u32 size;
+
+	if (!ptr->data || start > end)
+		return -EINVAL;
+
+	size = bpf_dynptr_get_size(ptr);
+
+	if (start > size || end > size)
+		return -ERANGE;
+
+	ptr->offset += start;
+	bpf_dynptr_set_size(ptr, end - start);
+
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) · line 2397

Quoted 6 lines · Joanne Koong
kernel/bpf/helpers.c +1 -0
 	return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)

I've missed this earlier.
Shouldn't we change all the existing dynptr kfuncs to be KF_TRUSTED_ARGS? Otherwise when people start passing bpf_dynptr-s from kernel code (like fuse-bpf is planning to do)
the bpf prog might get vanilla ptr_to_btf_id to bpf_dynptr_kern. It's probably not possible right now, so not a high-pri issue, but still. Or something in the verifier makes sure that dynptr-s are all trusted?

#3 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 16 lines · Alexei Starovoitov

kernel/bpf/helpers.c BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) · line 2397

Quoted 6 lines · Joanne Koong
      return obj;
kernel/bpf/helpers.c +1 -0
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)

I've missed this earlier.
Shouldn't we change all the existing dynptr kfuncs to be KF_TRUSTED_ARGS? Otherwise when people start passing bpf_dynptr-s from kernel code (like fuse-bpf is planning to do)
the bpf prog might get vanilla ptr_to_btf_id to bpf_dynptr_kern. It's probably not possible right now, so not a high-pri issue, but still. Or something in the verifier makes sure that dynptr-s are all trusted?

In my understanding, the checks the verifier enforces for KF_TRUSTED_ARGS are that the reg->offset is 0 and the reg may not be null. The verifier logic does this for dynptrs currently, it enforces that reg->offset is 0 (in stack_slot_obj_get_spi()) and that the reg->type is PTR_TO_STACK or CONST_PTR_TO_DYNPTR (in check_kfunc_args() for KF_ARG_PTR_TO_DYNPTR case). But maybe it's a good idea to add the KF_TRUSTED_ARGS flag anyways in case more safety checks are added to KF_TRUSTED_ARGS in the future?

#4 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 20 lines · Joanne Koong
Quoted 16 lines · Alexei Starovoitov

Review BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) · line 2397

Quoted 6 lines · Joanne Koong
      return obj;
(quoted diff) +1 -0
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)

I've missed this earlier.
Shouldn't we change all the existing dynptr kfuncs to be KF_TRUSTED_ARGS? Otherwise when people start passing bpf_dynptr-s from kernel code (like fuse-bpf is planning to do)
the bpf prog might get vanilla ptr_to_btf_id to bpf_dynptr_kern. It's probably not possible right now, so not a high-pri issue, but still. Or something in the verifier makes sure that dynptr-s are all trusted?

Quoted 8 lines

In my understanding, the checks the verifier enforces for KF_TRUSTED_ARGS are that the reg->offset is 0 and the reg may not be null. The verifier logic does this for dynptrs currently, it enforces that reg->offset is 0 (in stack_slot_obj_get_spi()) and that the reg->type is PTR_TO_STACK or CONST_PTR_TO_DYNPTR (in check_kfunc_args() for KF_ARG_PTR_TO_DYNPTR case). But maybe it's a good idea to add the KF_TRUSTED_ARGS flag anyways in case more safety checks are added to KF_TRUSTED_ARGS in the future?

Yeah. You're right.
The verifier is doing the same checks for dynptr and for trusted ptrs. So adding KF_TRUSTED_ARGS to bpf_dynptr_adjust is not mandatory. Maybe an opportunity to generalize the checks between KF_ARG_PTR_TO_BTF_ID and KF_ARG_PTR_TO_DYNPTR.
But KF_TRUSTED_ARGS is necessary for bpf_dynptr_from_skb otherwise old style ptr_to_btf_id skb can be passed in.

For example the following passes test_progs:

net/core/filter.c +0 -0
index d9ce04ca22ce..abb14036b455 100644
@@ -11718,6 +11718,7 @@ static int __init bpf_kfunc_init(void)
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT,

&bpf_kfunc_set_skb);
ret = ret ?:
register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL, &bpf_kfunc_set_skb);
ret = ret ?:
register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
&bpf_kfunc_set_skb);
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP,
&bpf_kfunc_set_xdp);
}
late_initcall(bpf_kfunc_init);

(quoted diff) +0 -0

b/tools/testing/selftests/bpf/progs/dynptr_success.c index b2fa6c47ecc0..bd8fbc3e04ea 100644

tools/testing/selftests/bpf/progs/dynptr_success.c +9 -0
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
 #include "bpf_kfuncs.h"
 #include "errno.h"
@@ -187,6 +188,15 @@ int test_skb_readonly(struct __sk_buff *skb)
        return 1;
 }
 
+SEC("fentry/__kfree_skb")
+int BPF_PROG(test_skb, struct __sk_buff *skb)
+{
+       struct bpf_dynptr ptr;
+
+       bpf_dynptr_from_skb(skb, 0, &ptr);
+       return 0;
+}
 

but shouldn't. skb in fentry is not trusted.
It's not an issue right now, because bpf_dynptr_from_skb() is enabled for networking prog types only,
but BPF_PROG_TYPE_NETFILTER is already blending the boundary. It's more networking than tracing and normal tracing should be able to examine skb. dynptr allows to do it nicely. Not a blocker for this set. Just something to follow up.

#5 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 85 lines · Alexei Starovoitov
Quoted 20 lines · Joanne Koong
Quoted 16 lines · Alexei Starovoitov

Review BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) · line 2397

Quoted 6 lines · Joanne Koong
      return obj;
(quoted diff) +1 -0
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)

I've missed this earlier.
Shouldn't we change all the existing dynptr kfuncs to be KF_TRUSTED_ARGS? Otherwise when people start passing bpf_dynptr-s from kernel code (like fuse-bpf is planning to do)
the bpf prog might get vanilla ptr_to_btf_id to bpf_dynptr_kern. It's probably not possible right now, so not a high-pri issue, but still. Or something in the verifier makes sure that dynptr-s are all trusted?

Quoted 8 lines

In my understanding, the checks the verifier enforces for KF_TRUSTED_ARGS are that the reg->offset is 0 and the reg may not be null. The verifier logic does this for dynptrs currently, it enforces that reg->offset is 0 (in stack_slot_obj_get_spi()) and that the reg->type is PTR_TO_STACK or CONST_PTR_TO_DYNPTR (in check_kfunc_args() for KF_ARG_PTR_TO_DYNPTR case). But maybe it's a good idea to add the KF_TRUSTED_ARGS flag anyways in case more safety checks are added to KF_TRUSTED_ARGS in the future?

Yeah. You're right.
The verifier is doing the same checks for dynptr and for trusted ptrs. So adding KF_TRUSTED_ARGS to bpf_dynptr_adjust is not mandatory. Maybe an opportunity to generalize the checks between KF_ARG_PTR_TO_BTF_ID and KF_ARG_PTR_TO_DYNPTR.
But KF_TRUSTED_ARGS is necessary for bpf_dynptr_from_skb otherwise old style ptr_to_btf_id skb can be passed in.

For example the following passes test_progs:

net/core/filter.c +0 -0
index d9ce04ca22ce..abb14036b455 100644
@@ -11718,6 +11718,7 @@ static int __init bpf_kfunc_init(void)
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT,
 &bpf_kfunc_set_skb);
        ret = ret ?:
 register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL,
 &bpf_kfunc_set_skb);
        ret = ret ?:

register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
&bpf_kfunc_set_skb);
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP,
&bpf_kfunc_set_xdp);
}
late_initcall(bpf_kfunc_init);

tools/testing/selftests/bpf/progs/dynptr_success.c +9 -0
index b2fa6c47ecc0..bd8fbc3e04ea 100644
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
 #include "bpf_kfuncs.h"
 #include "errno.h"
@@ -187,6 +188,15 @@ int test_skb_readonly(struct __sk_buff *skb)
        return 1;
 }
 
+SEC("fentry/__kfree_skb")
+int BPF_PROG(test_skb, struct __sk_buff *skb)
+{
+       struct bpf_dynptr ptr;
+
+       bpf_dynptr_from_skb(skb, 0, &ptr);
+       return 0;
+}
 
Quoted 7 lines

but shouldn't. skb in fentry is not trusted.
It's not an issue right now, because bpf_dynptr_from_skb() is enabled for networking prog types only,
but BPF_PROG_TYPE_NETFILTER is already blending the boundary. It's more networking than tracing and normal tracing should be able to examine skb. dynptr allows to do it nicely. Not a blocker for this set. Just something to follow up.

Ahh I see, thanks for the explanation. I'm trying to find where this happens in the code - i see the check in the verifier for is_trusted_reg() (when we call check_kfunc_args() for the KF_ARG_PTR_TO_BTF_ID case) so it seems like the skb ctx reg is trusted if it's been marked as either MEM_ALLOC or PTR_TRUSTED, and it's untrusted if it's not. But where does this get marked as PTR_TRUSTED for networking prog types?

#6 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 87 lines · Joanne Koong
Quoted 85 lines · Alexei Starovoitov
Quoted 20 lines · Joanne Koong
Quoted 16 lines · Alexei Starovoitov

Review BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) · line 2397

Quoted 6 lines · Joanne Koong
      return obj;
(quoted diff) +1 -0
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)

I've missed this earlier.
Shouldn't we change all the existing dynptr kfuncs to be KF_TRUSTED_ARGS? Otherwise when people start passing bpf_dynptr-s from kernel code (like fuse-bpf is planning to do)
the bpf prog might get vanilla ptr_to_btf_id to bpf_dynptr_kern. It's probably not possible right now, so not a high-pri issue, but still. Or something in the verifier makes sure that dynptr-s are all trusted?

Quoted 8 lines

In my understanding, the checks the verifier enforces for KF_TRUSTED_ARGS are that the reg->offset is 0 and the reg may not be null. The verifier logic does this for dynptrs currently, it enforces that reg->offset is 0 (in stack_slot_obj_get_spi()) and that the reg->type is PTR_TO_STACK or CONST_PTR_TO_DYNPTR (in check_kfunc_args() for KF_ARG_PTR_TO_DYNPTR case). But maybe it's a good idea to add the KF_TRUSTED_ARGS flag anyways in case more safety checks are added to KF_TRUSTED_ARGS in the future?

Yeah. You're right.
The verifier is doing the same checks for dynptr and for trusted ptrs. So adding KF_TRUSTED_ARGS to bpf_dynptr_adjust is not mandatory. Maybe an opportunity to generalize the checks between KF_ARG_PTR_TO_BTF_ID and KF_ARG_PTR_TO_DYNPTR.
But KF_TRUSTED_ARGS is necessary for bpf_dynptr_from_skb otherwise old style ptr_to_btf_id skb can be passed in.

For example the following passes test_progs:

net/core/filter.c +0 -0
index d9ce04ca22ce..abb14036b455 100644
@@ -11718,6 +11718,7 @@ static int __init bpf_kfunc_init(void)
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT,
 &bpf_kfunc_set_skb);
        ret = ret ?:
 register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL,
 &bpf_kfunc_set_skb);
        ret = ret ?:

register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
&bpf_kfunc_set_skb);
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP,
&bpf_kfunc_set_xdp);
}
late_initcall(bpf_kfunc_init);

tools/testing/selftests/bpf/progs/dynptr_success.c +9 -0
index b2fa6c47ecc0..bd8fbc3e04ea 100644
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
 #include "bpf_kfuncs.h"
 #include "errno.h"
@@ -187,6 +188,15 @@ int test_skb_readonly(struct __sk_buff *skb)
        return 1;
 }
 
+SEC("fentry/__kfree_skb")
+int BPF_PROG(test_skb, struct __sk_buff *skb)
+{
+       struct bpf_dynptr ptr;
+
+       bpf_dynptr_from_skb(skb, 0, &ptr);
+       return 0;
+}
 
Quoted 15 lines
Quoted 7 lines

but shouldn't. skb in fentry is not trusted.
It's not an issue right now, because bpf_dynptr_from_skb() is enabled for networking prog types only,
but BPF_PROG_TYPE_NETFILTER is already blending the boundary. It's more networking than tracing and normal tracing should be able to examine skb. dynptr allows to do it nicely. Not a blocker for this set. Just something to follow up.

Ahh I see, thanks for the explanation. I'm trying to find where this happens in the code - i see the check in the verifier for is_trusted_reg() (when we call check_kfunc_args() for the KF_ARG_PTR_TO_BTF_ID case) so it seems like the skb ctx reg is trusted if it's been marked as either MEM_ALLOC or PTR_TRUSTED, and it's untrusted if it's not. But where does this get marked as PTR_TRUSTED for networking prog types?

is_trusted_reg() applies to PTR_TO_BTF_ID pointers.
For networking progs skb comes as PTR_TO_CTX which are implicitly trusted and from safety pov equivalent to PTR_TO_BTF_ID | PTR_TRUSTED. But tracing progs are different. Arguments of tp_btf progs
are also trusted, but fexit args are not. They're old legacy PTR_TO_BTF_ID without flags. Neither PTR_TRUSTED nor PTR_UNTRUSTED.
The purpose of KF_TRUSTED_ARGS is to filter out such pointers.

#7 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 105 lines · Alexei Starovoitov
Quoted 87 lines · Joanne Koong
Quoted 85 lines · Alexei Starovoitov
Quoted 20 lines · Joanne Koong
Quoted 16 lines · Alexei Starovoitov

Review BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL) · line 2397

Quoted 6 lines · Joanne Koong
      return obj;
(quoted diff) +1 -0
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)

I've missed this earlier.
Shouldn't we change all the existing dynptr kfuncs to be KF_TRUSTED_ARGS? Otherwise when people start passing bpf_dynptr-s from kernel code (like fuse-bpf is planning to do)
the bpf prog might get vanilla ptr_to_btf_id to bpf_dynptr_kern. It's probably not possible right now, so not a high-pri issue, but still. Or something in the verifier makes sure that dynptr-s are all trusted?

Quoted 8 lines

In my understanding, the checks the verifier enforces for KF_TRUSTED_ARGS are that the reg->offset is 0 and the reg may not be null. The verifier logic does this for dynptrs currently, it enforces that reg->offset is 0 (in stack_slot_obj_get_spi()) and that the reg->type is PTR_TO_STACK or CONST_PTR_TO_DYNPTR (in check_kfunc_args() for KF_ARG_PTR_TO_DYNPTR case). But maybe it's a good idea to add the KF_TRUSTED_ARGS flag anyways in case more safety checks are added to KF_TRUSTED_ARGS in the future?

Yeah. You're right.
The verifier is doing the same checks for dynptr and for trusted ptrs. So adding KF_TRUSTED_ARGS to bpf_dynptr_adjust is not mandatory. Maybe an opportunity to generalize the checks between KF_ARG_PTR_TO_BTF_ID and KF_ARG_PTR_TO_DYNPTR.
But KF_TRUSTED_ARGS is necessary for bpf_dynptr_from_skb otherwise old style ptr_to_btf_id skb can be passed in.

For example the following passes test_progs:

net/core/filter.c +0 -0
index d9ce04ca22ce..abb14036b455 100644
@@ -11718,6 +11718,7 @@ static int __init bpf_kfunc_init(void)
        ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_XMIT,
 &bpf_kfunc_set_skb);
        ret = ret ?:
 register_btf_kfunc_id_set(BPF_PROG_TYPE_LWT_SEG6LOCAL,
 &bpf_kfunc_set_skb);
        ret = ret ?:

register_btf_kfunc_id_set(BPF_PROG_TYPE_NETFILTER, &bpf_kfunc_set_skb);
+ ret = ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING,
&bpf_kfunc_set_skb);
return ret ?: register_btf_kfunc_id_set(BPF_PROG_TYPE_XDP,
&bpf_kfunc_set_xdp);
}
late_initcall(bpf_kfunc_init);

tools/testing/selftests/bpf/progs/dynptr_success.c +9 -0
index b2fa6c47ecc0..bd8fbc3e04ea 100644
@@ -4,6 +4,7 @@
 #include <string.h>
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
 #include "bpf_kfuncs.h"
 #include "errno.h"
@@ -187,6 +188,15 @@ int test_skb_readonly(struct __sk_buff *skb)
        return 1;
 }
 
+SEC("fentry/__kfree_skb")
+int BPF_PROG(test_skb, struct __sk_buff *skb)
+{
+       struct bpf_dynptr ptr;
+
+       bpf_dynptr_from_skb(skb, 0, &ptr);
+       return 0;
+}
 
Quoted 15 lines
Quoted 7 lines

but shouldn't. skb in fentry is not trusted.
It's not an issue right now, because bpf_dynptr_from_skb() is enabled for networking prog types only,
but BPF_PROG_TYPE_NETFILTER is already blending the boundary. It's more networking than tracing and normal tracing should be able to examine skb. dynptr allows to do it nicely. Not a blocker for this set. Just something to follow up.

Ahh I see, thanks for the explanation. I'm trying to find where this happens in the code - i see the check in the verifier for is_trusted_reg() (when we call check_kfunc_args() for the KF_ARG_PTR_TO_BTF_ID case) so it seems like the skb ctx reg is trusted if it's been marked as either MEM_ALLOC or PTR_TRUSTED, and it's untrusted if it's not. But where does this get marked as PTR_TRUSTED for networking prog types?

Quoted 7 lines

is_trusted_reg() applies to PTR_TO_BTF_ID pointers.
For networking progs skb comes as PTR_TO_CTX which are implicitly trusted and from safety pov equivalent to PTR_TO_BTF_ID | PTR_TRUSTED. But tracing progs are different. Arguments of tp_btf progs
are also trusted, but fexit args are not. They're old legacy PTR_TO_BTF_ID without flags. Neither PTR_TRUSTED nor PTR_UNTRUSTED.
The purpose of KF_TRUSTED_ARGS is to filter out such pointers.

I need to do a thorough look at how FUSE BPF is using bpf_dynptr, but I'd rather us starting strict and not allowing to pass bpf_dynptr as PTR_TO_BTF_ID, at least right now until we can think this through thoroughly. One fundamental aspect of current on the stack dynptr or dynptr passed from user_ringbuf's drain helper is that we have a guarantee that no one else besides program (on a single current thread) can modify its state.

Anyways, I'd expect that if some kfunc accepts struct bpf_dynptr we force that it's PTR_TO_DYNPTR_ID or PTR_TO_STACK register, at least for now. If that's not the case right now, let's make sure it is?

maintainer
#8 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

In reply to Joanne Koong
Quoted 43 lines · Joanne Koong

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +8 -0
index 00e5fb0682ac..7ddf63ac93ce 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+	u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+	ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
 	return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2297,6 +2304,24 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
 	return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
+__bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2317

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+{
+	u32 size;
+
+	if (!ptr->data || start > end)
+		return -EINVAL;
+
+	size = bpf_dynptr_get_size(ptr);
+
+	if (start > size || end > size)
+		return -ERANGE;

If new size is computed as "end - start" should
the check above be "end >= size"?

Quoted 18 lines
kernel/bpf/helpers.c +8 -0
+
+	ptr->offset += start;
+	bpf_dynptr_set_size(ptr, end - start);
+
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
maintainer
#9 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 43 lines · Eduard Zingerman
Quoted 42 lines · Joanne Koong

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +7 -0
index 00e5fb0682ac..7ddf63ac93ce 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+	u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+	ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
 	return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2297,6 +2304,24 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
 	return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 14 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2317

Quoted 11 lines
kernel/bpf/helpers.c +11 -0
+__bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)
+{
+	u32 size;
+
+	if (!ptr->data || start > end)
+		return -EINVAL;
+
+	size = bpf_dynptr_get_size(ptr);
+
+	if (start > size || end > size)
+		return -ERANGE;

If new size is computed as "end - start" should
the check above be "end >= size"?

Please ignore this comment, I got confused.

Quoted 20 lines
Quoted 19 lines
kernel/bpf/helpers.c +8 -0
+
+	ptr->offset += start;
+	bpf_dynptr_set_size(ptr, end - start);
+
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
#10 lore raw

RE: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

In reply to Joanne Koong
Quoted 9 lines · Joanne Koong

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Quoted 5 lines

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

Would you want to prohibit creating an empty dynptr with [start, start)?

Quoted 29 lines
---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +8 -0
index 00e5fb0682ac..7ddf63ac93ce 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+	u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+	ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
 	return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2297,6 +2304,24 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
 	return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
+__bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2317

Quoted 10 lines
kernel/bpf/helpers.c +10 -0
+{
+	u32 size;
+
+	if (!ptr->data || start > end)
+		return -EINVAL;
+
+	size = bpf_dynptr_get_size(ptr);
+
+	if (start > size || end > size)
+		return -ERANGE;

maybe 'start >= size'? OTOH if the verifier doesn't mind I guess its OK to create the thing even if it doesn't make much sense.

Quoted 21 lines
kernel/bpf/helpers.c +8 -0
+
+	ptr->offset += start;
+	bpf_dynptr_set_size(ptr, end - start);
+
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.34.1
#11 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 11 lines · John Fastabend
Quoted 9 lines · Joanne Koong

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Quoted 7 lines
Quoted 5 lines

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

Would you want to prohibit creating an empty dynptr with [start, start)?

I'm open to either :) I don't reallysee a use case for creating an empty dynptr, but I think the concept of an empty dynptr might be useful in general, so maybe we should let this be okay as well?

Quoted 29 lines
Quoted 29 lines
---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +7 -0
index 00e5fb0682ac..7ddf63ac93ce 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
      return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+     u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+     ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
      return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2297,6 +2304,24 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
      return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 14 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2317

Quoted 11 lines
kernel/bpf/helpers.c +11 -0
+__bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)
+{
+     u32 size;
+
+     if (!ptr->data || start > end)
+             return -EINVAL;
+
+     size = bpf_dynptr_get_size(ptr);
+
+     if (start > size || end > size)
+             return -ERANGE;

maybe 'start >= size'? OTOH if the verifier doesn't mind I guess its OK to create the thing even if it doesn't make much sense.

I think there might be use cases where this is useful even though the zero-sized dynptr can't do anything. for example, if there's a helper function in a program that takes in a dynptr, parses some fixed-size chunk of its data, and then advances it, it might be useful to have the concept of a zero-sized dynptr, so that if we're parsing the last chunk of the data, then the last call to bpf_dynptr_adjust() will still succeed and the dynptr will be of size 0, which signals completion.

Quoted 22 lines
Quoted 22 lines
kernel/bpf/helpers.c +8 -1
+
+     ptr->offset += start;
+     bpf_dynptr_set_size(ptr, end - start);
+
+     return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
      return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#12 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 20 lines · Joanne Koong
Quoted 17 lines · John Fastabend
Quoted 14 lines · Joanne Koong

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

Quoted 5 lines
Quoted 1 line

Would you want to prohibit creating an empty dynptr with [start, start)?

I'm open to either :) I don't reallysee a use case for creating an empty dynptr, but I think the concept of an empty dynptr might be useful in general, so maybe we should let this be okay as well?

Yes, there is no need to artificially enforce a non-empty range. We already use pointers to zero-sized memory region in verifier (e.g., Alexei's recent kfunc existence check changes). In general, empty range is a valid range and we should strive to have that working without assumptions on who and how would use that. As long as it's conceptually safe, we should support it.

Quoted 45 lines
Quoted 30 lines
Quoted 30 lines
---
 kernel/bpf/helpers.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
kernel/bpf/helpers.c +7 -0
index 00e5fb0682ac..7ddf63ac93ce 100644
@@ -1448,6 +1448,13 @@ u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
      return ptr->size & DYNPTR_SIZE_MASK;
 }
 
+static void bpf_dynptr_set_size(struct bpf_dynptr_kern *ptr, u32 new_size)
+{
+     u32 metadata = ptr->size & ~DYNPTR_SIZE_MASK;
+
+     ptr->size = new_size | metadata;
+}
+
 int bpf_dynptr_check_size(u32 size)
 {
      return size > DYNPTR_MAX_SIZE ? -E2BIG : 0;
@@ -2297,6 +2304,24 @@ __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o
      return bpf_dynptr_slice(ptr, offset, buffer, buffer__szk);
 }
 
Quoted 14 lines

kernel/bpf/helpers.c __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 o · line 2317

Quoted 11 lines
kernel/bpf/helpers.c +11 -0
+__bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end)
+{
+     u32 size;
+
+     if (!ptr->data || start > end)
+             return -EINVAL;
+
+     size = bpf_dynptr_get_size(ptr);
+
+     if (start > size || end > size)
+             return -ERANGE;

maybe 'start >= size'? OTOH if the verifier doesn't mind I guess its OK to create the thing even if it doesn't make much sense.

Quoted 9 lines

I think there might be use cases where this is useful even though the zero-sized dynptr can't do anything. for example, if there's a helper function in a program that takes in a dynptr, parses some fixed-size chunk of its data, and then advances it, it might be useful to have the concept of a zero-sized dynptr, so that if we're parsing the last chunk of the data, then the last call to bpf_dynptr_adjust() will still succeed and the dynptr will be of size 0, which signals completion.

+1, empty range does happen in practice naturally, and having to special-case them is a hindrance. Let's keep it possible.

Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
kernel/bpf/helpers.c +8 -1
+
+     ptr->offset += start;
+     bpf_dynptr_set_size(ptr, end - start);
+
+     return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
      return obj;
@@ -2369,6 +2394,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_slice_rdwr, KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
+BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
--

2.34.1

#13 lore raw

Re: [PATCH v2 bpf-next 1/5] bpf: Add bpf_dynptr_adjust

Quoted 27 lines · Andrii Nakryiko
Quoted 20 lines · Joanne Koong
Quoted 17 lines · John Fastabend
Quoted 14 lines · Joanne Koong

Add a new kfunc

int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 end);

which adjusts the dynptr to reflect the new [start, end) interval. In particular, it advances the offset of the dynptr by "start" bytes, and if end is less than the size of the dynptr, then this will trim the dynptr accordingly.

Adjusting the dynptr interval may be useful in certain situations. For example, when hashing which takes in generic dynptrs, if the dynptr points to a struct but only a certain memory region inside the struct should be hashed, adjust can be used to narrow in on the specific region to hash.

Quoted 5 lines
Quoted 1 line

Would you want to prohibit creating an empty dynptr with [start, start)?

I'm open to either :) I don't reallysee a use case for creating an empty dynptr, but I think the concept of an empty dynptr might be useful in general, so maybe we should let this be okay as well?

Quoted 6 lines

Yes, there is no need to artificially enforce a non-empty range. We already use pointers to zero-sized memory region in verifier (e.g., Alexei's recent kfunc existence check changes). In general, empty range is a valid range and we should strive to have that working without assumptions on who and how would use that. As long as it's conceptually safe, we should support it.

Ack. Agree sounds good to me.

#14 lore raw

PATCH v2 2/5 bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly

In reply to Joanne Koong

bpf_dynptr_is_null returns true if the dynptr is null / invalid (determined by whether ptr->data is NULL), else false if the dynptr is a valid dynptr.

bpf_dynptr_is_rdonly returns true if the dynptr is read-only, else false if the dynptr is read-writable. If the dynptr is null / invalid, false is returned by default.

---
 kernel/bpf/helpers.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
kernel/bpf/helpers.c +19 -4
index 7ddf63ac93ce..566ed89f173b 100644
@@ -1423,7 +1423,7 @@ static const struct bpf_func_proto bpf_kptr_xchg_proto = {
 #define DYNPTR_SIZE_MASK	0xFFFFFF
 #define DYNPTR_RDONLY_BIT	BIT(31)
 
-static bool bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
+static bool __bpf_dynptr_is_rdonly(const struct bpf_dynptr_kern *ptr)
 {
 	return ptr->size & DYNPTR_RDONLY_BIT;
 }
@@ -1570,7 +1570,7 @@ BPF_CALL_5(bpf_dynptr_write, const struct bpf_dynptr_kern *, dst, u32, offset, v
 	enum bpf_dynptr_type type;
 	int err;
 
-	if (!dst->data || bpf_dynptr_is_rdonly(dst))
+	if (!dst->data || __bpf_dynptr_is_rdonly(dst))
 		return -EINVAL;
 
 	err = bpf_dynptr_check_off_len(dst, offset, len);
@@ -1626,7 +1626,7 @@ BPF_CALL_3(bpf_dynptr_data, const struct bpf_dynptr_kern *, ptr, u32, offset, u3
 	if (err)
 		return 0;
 
-	if (bpf_dynptr_is_rdonly(ptr))
+	if (__bpf_dynptr_is_rdonly(ptr))
 		return 0;
 
 	type = bpf_dynptr_get_type(ptr);
@@ -2276,7 +2276,7 @@ __bpf_kfunc void *bpf_dynptr_slice(const struct bpf_dynptr_kern *ptr, u32 offset
 __bpf_kfunc void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr_kern *ptr, u32 offset,
 					void *buffer, u32 buffer__szk)
 {
-	if (!ptr->data || bpf_dynptr_is_rdonly(ptr))
+	if (!ptr->data || __bpf_dynptr_is_rdonly(ptr))
 		return NULL;
 
 	/* bpf_dynptr_slice_rdwr is the same logic as bpf_dynptr_slice.
@@ -2322,6 +2322,19 @@ __bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 en
 	return 0;
 }
 
+__bpf_kfunc bool bpf_dynptr_is_null(struct bpf_dynptr_kern *ptr)
+{
+	return !ptr->data;
+}
+
+__bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
+{
+	if (!ptr->data)
+		return false;
+
+	return __bpf_dynptr_is_rdonly(ptr);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2395,6 +2408,8 @@ BTF_ID_FLAGS(func, bpf_iter_num_new, KF_ITER_NEW)
 BTF_ID_FLAGS(func, bpf_iter_num_next, KF_ITER_NEXT | KF_RET_NULL)
 BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_dynptr_adjust)
+BTF_ID_FLAGS(func, bpf_dynptr_is_null)
+BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
Signature · 1 line
2.34.1
reviewer Acked-by on v2 2/5
#15 lore raw

RE: [PATCH v2 bpf-next 2/5] bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly

Quoted 11 lines · Joanne Koong

bpf_dynptr_is_null returns true if the dynptr is null / invalid (determined by whether ptr->data is NULL), else false if the dynptr is a valid dynptr.

bpf_dynptr_is_rdonly returns true if the dynptr is read-only, else false if the dynptr is read-writable. If the dynptr is null / invalid, false is returned by default.

---

#16 lore raw

PATCH v2 3/5 bpf: Add bpf_dynptr_size

In reply to Joanne Koong

bpf_dynptr_size returns the number of useable bytes in a dynptr.

---
 include/linux/bpf.h      |  2 +-
 kernel/bpf/helpers.c     | 15 ++++++++++++---
 kernel/trace/bpf_trace.c |  4 ++--
 3 files changed, 15 insertions(+), 6 deletions(-)
include/linux/bpf.h +1 -1
index 18b592fde896..a4429dac3a1b 100644
@@ -1197,7 +1197,7 @@ enum bpf_dynptr_type {
 };
 
 int bpf_dynptr_check_size(u32 size);
-u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr);
+u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr);
 
 #ifdef CONFIG_BPF_JIT
 int bpf_trampoline_link_prog(struct bpf_tramp_link *link, struct bpf_trampoline *tr);
kernel/bpf/helpers.c +12 -3
index 566ed89f173b..9018646b86db 100644
@@ -1443,7 +1443,7 @@ static enum bpf_dynptr_type bpf_dynptr_get_type(const struct bpf_dynptr_kern *pt
 	return (ptr->size & ~(DYNPTR_RDONLY_BIT)) >> DYNPTR_TYPE_SHIFT;
 }
 
-u32 bpf_dynptr_get_size(const struct bpf_dynptr_kern *ptr)
+u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
 {
 	return ptr->size & DYNPTR_SIZE_MASK;
 }
@@ -1476,7 +1476,7 @@ void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
 
 static int bpf_dynptr_check_off_len(const struct bpf_dynptr_kern *ptr, u32 offset, u32 len)
 {
-	u32 size = bpf_dynptr_get_size(ptr);
+	u32 size = __bpf_dynptr_size(ptr);
 
 	if (len > size || offset > size - len)
 		return -E2BIG;
@@ -2311,7 +2311,7 @@ __bpf_kfunc int bpf_dynptr_adjust(struct bpf_dynptr_kern *ptr, u32 start, u32 en
 	if (!ptr->data || start > end)
 		return -EINVAL;
 
-	size = bpf_dynptr_get_size(ptr);
+	size = __bpf_dynptr_size(ptr);
 
 	if (start > size || end > size)
 		return -ERANGE;
@@ -2335,6 +2335,14 @@ __bpf_kfunc bool bpf_dynptr_is_rdonly(struct bpf_dynptr_kern *ptr)
 	return __bpf_dynptr_is_rdonly(ptr);
 }
 
+__bpf_kfunc __u32 bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
+{
+	if (!ptr->data)
+		return -EINVAL;
+
+	return __bpf_dynptr_size(ptr);
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2410,6 +2418,7 @@ BTF_ID_FLAGS(func, bpf_iter_num_destroy, KF_ITER_DESTROY)
 BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
+BTF_ID_FLAGS(func, bpf_dynptr_size)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/trace/bpf_trace.c +2 -2
index bcf91bc7bf71..8deb22a99abe 100644
@@ -1349,9 +1349,9 @@ __bpf_kfunc int bpf_verify_pkcs7_signature(struct bpf_dynptr_kern *data_ptr,
 	}
 
 	return verify_pkcs7_signature(data_ptr->data,
-				      bpf_dynptr_get_size(data_ptr),
+				      __bpf_dynptr_size(data_ptr),
 				      sig_ptr->data,
-				      bpf_dynptr_get_size(sig_ptr),
+				      __bpf_dynptr_size(sig_ptr),
 				      trusted_keyring->key,
 				      VERIFYING_UNSPECIFIED_SIGNATURE, NULL,
 				      NULL);
Signature · 1 line
2.34.1
#18 lore raw

PATCH v2 4/5 bpf: Add bpf_dynptr_clone

In reply to Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 ++++++
 kernel/bpf/verifier.c | 105 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 99 insertions(+), 20 deletions(-)
kernel/bpf/helpers.c +14 -0
index 9018646b86db..1ebdc7f1a574 100644
@@ -2343,6 +2343,19 @@ __bpf_kfunc __u32 bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
 	return __bpf_dynptr_size(ptr);
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+				 struct bpf_dynptr_kern *clone__uninit)
+{
+	if (!ptr->data) {
+		bpf_dynptr_set_null(clone__uninit);
+		return -EINVAL;
+	}
+
+	*clone__uninit = *ptr;
+
+	return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
 	return obj;
@@ -2419,6 +2432,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_size)
+BTF_ID_FLAGS(func, bpf_dynptr_clone)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +85 -20

202 lines not on this page · raw message

Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH v2 bpf-next 4/5] bpf: Add bpf_dynptr_clone

Quoted 137 lines · Joanne Koong

The cloned dynptr will point to the same data as its parent dynptr, with the same type, offset, size and read-only properties.

Any writes to a dynptr will be reflected across all instances (by 'instance', this means any dynptrs that point to the same underlying data).

Please note that data slice and dynptr invalidations will affect all instances as well. For example, if bpf_dynptr_write() is called on an skb-type dynptr, all data slices of dynptr instances to that skb will be invalidated as well (eg data slices of any clones, parents, grandparents, ...). Another example is if a ringbuf dynptr is submitted, any instance of that dynptr will be invalidated.

Changing the view of the dynptr (eg advancing the offset or trimming the size) will only affect that dynptr and not affect any other instances.

One example use case where cloning may be helpful is for hashing or iterating through dynptr data. Cloning will allow the user to maintain the original view of the dynptr for future use, while also allowing views to smaller subsets of the data after the offset is advanced or the size is trimmed.

---
 kernel/bpf/helpers.c  |  14 ++++++
 kernel/bpf/verifier.c | 105 ++++++++++++++++++++++++++++++++++--------
 2 files changed, 99 insertions(+), 20 deletions(-)
kernel/bpf/helpers.c +14 -0
index 9018646b86db..1ebdc7f1a574 100644
@@ -2343,6 +2343,19 @@ __bpf_kfunc __u32 bpf_dynptr_size(const struct bpf_dynptr_kern *ptr)
        return __bpf_dynptr_size(ptr);
 }
 
+__bpf_kfunc int bpf_dynptr_clone(struct bpf_dynptr_kern *ptr,
+                                struct bpf_dynptr_kern *clone__uninit)
+{
+       if (!ptr->data) {
+               bpf_dynptr_set_null(clone__uninit);
+               return -EINVAL;
+       }
+
+       *clone__uninit = *ptr;
+
+       return 0;
+}
+
 __bpf_kfunc void *bpf_cast_to_kern_ctx(void *obj)
 {
        return obj;
@@ -2419,6 +2432,7 @@ BTF_ID_FLAGS(func, bpf_dynptr_adjust)
 BTF_ID_FLAGS(func, bpf_dynptr_is_null)
 BTF_ID_FLAGS(func, bpf_dynptr_is_rdonly)
 BTF_ID_FLAGS(func, bpf_dynptr_size)
+BTF_ID_FLAGS(func, bpf_dynptr_clone)
 BTF_SET8_END(common_btf_ids)
 
 static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/bpf/verifier.c +14 -14
index 1e05355facdc..164726673086 100644
@@ -309,6 +309,7 @@ struct bpf_kfunc_call_arg_meta {
        struct {
                enum bpf_dynptr_type type;
                u32 id;
+               u32 ref_obj_id;
        } initialized_dynptr;
        struct {
                u8 spi;
@@ -847,11 +848,11 @@ 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)
+                                  enum bpf_arg_type arg_type, int insn_idx, int clone_ref_obj_id)
 {
        struct bpf_func_state *state = func(env, reg);
        enum bpf_dynptr_type type;
-       int spi, i, id, err;
+       int spi, i, err;
 
        spi = dynptr_get_spi(env, reg);
        if (spi < 0)
@@ -887,7 +888,13 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
 
        if (dynptr_type_refcounted(type)) {
                /* The id is used to track proper releasing */
-               id = acquire_reference_state(env, insn_idx);
+               int id;
+
+               if (clone_ref_obj_id)
+                       id = clone_ref_obj_id;
+               else
+                       id = acquire_reference_state(env, insn_idx);
+
                if (id < 0)
                        return id;
 
@@ -901,24 +908,15 @@ static int mark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_
        return 0;
 }
 
-static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_reg_state *reg)
+static void invalidate_dynptr(struct bpf_verifier_env *env, struct bpf_func_state *state, int spi)
 {
-       struct bpf_func_state *state = func(env, reg);
-       int spi, i;
-
-       spi = dynptr_get_spi(env, reg);
-       if (spi < 0)
-               return spi;
+       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;
        }
 
-       /* Invalidate any slices associated with this dynptr */
-       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type))
-               WARN_ON_ONCE(release_reference(env, state->stack[spi].spilled_ptr.ref_obj_id));
-
        __mark_reg_not_init(env, &state->stack[spi].spilled_ptr);
        __mark_reg_not_init(env, &state->stack[spi - 1].spilled_ptr);
 
@@ -945,6 +943,52 @@ static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re
         */
        state->stack[spi].spilled_ptr.live |= REG_LIVE_WRITTEN;
        state->stack[spi - 1].spilled_ptr.live |= REG_LIVE_WRITTEN;
+}
+

kernel/bpf/verifier.c static int unmark_stack_slots_dynptr(struct bpf_verifier_env *env, struct bpf_re · line 957

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+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;
+
+       spi = dynptr_get_spi(env, reg);
+       if (spi < 0)
+               return spi;
+
+       if (dynptr_type_refcounted(state->stack[spi].spilled_ptr.dynptr.type)) {

I inverted this condition, did invalidate_dynptr(spi), return 0. This reduced nestedness of refcounted case handling code below.

The rest looks great!

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+               int ref_obj_id = state->stack[spi].spilled_ptr.ref_obj_id;
+               int i;
+
+               /* 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.
+                */
+

[...]

#20 lore raw

PATCH v2 5/5 selftests/bpf: add tests for dynptr convenience helpers

In reply to Joanne Koong

Add various tests for the added dynptr convenience helpers.

---
 tools/testing/selftests/bpf/bpf_kfuncs.h      |   6 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |   6 +
 .../testing/selftests/bpf/progs/dynptr_fail.c | 287 +++++++++++++++++
 .../selftests/bpf/progs/dynptr_success.c      | 298 ++++++++++++++++++
 4 files changed, 597 insertions(+)
tools/testing/selftests/bpf/bpf_kfuncs.h +6 -0
index 8c993ec8ceea..f3c41f8902a0 100644
@@ -35,4 +35,10 @@ extern void *bpf_dynptr_slice(const struct bpf_dynptr *ptr, __u32 offset,
 extern void *bpf_dynptr_slice_rdwr(const struct bpf_dynptr *ptr, __u32 offset,
 			      void *buffer, __u32 buffer__szk) __ksym;
 
+extern int bpf_dynptr_adjust(const struct bpf_dynptr *ptr, __u32 start, __u32 end) __ksym;
+extern int bpf_dynptr_is_null(const struct bpf_dynptr *ptr) __ksym;
+extern int bpf_dynptr_is_rdonly(const struct bpf_dynptr *ptr) __ksym;
+extern __u32 bpf_dynptr_size(const struct bpf_dynptr *ptr) __ksym;
+extern int bpf_dynptr_clone(const struct bpf_dynptr *ptr, struct bpf_dynptr *clone__init) __ksym;
+
 #endif
tools/testing/selftests/bpf/prog_tests/dynptr.c +6 -0
index d176c34a7d2e..0478916aff37 100644
@@ -20,6 +20,12 @@ static struct {
 	{"test_ringbuf", SETUP_SYSCALL_SLEEP},
 	{"test_skb_readonly", SETUP_SKB_PROG},
 	{"test_dynptr_skb_data", SETUP_SKB_PROG},
+	{"test_adjust", SETUP_SYSCALL_SLEEP},
+	{"test_adjust_err", SETUP_SYSCALL_SLEEP},
+	{"test_zero_size_dynptr", SETUP_SYSCALL_SLEEP},
+	{"test_dynptr_is_null", SETUP_SYSCALL_SLEEP},
+	{"test_dynptr_is_rdonly", SETUP_SKB_PROG},
+	{"test_dynptr_clone", SETUP_SKB_PROG},
 };
 
 static void verify_success(const char *prog_name, enum test_setup_type setup_type)
tools/testing/selftests/bpf/progs/dynptr_fail.c +287 -0

290 lines not on this page · raw message

tools/testing/selftests/bpf/progs/dynptr_success.c +298 -0

301 lines not on this page · raw message

Signature · 1 line
2.34.1
#21 lore raw

Re: [PATCH v2 bpf-next 5/5] selftests/bpf: add tests for dynptr convenience helpers

Quoted 11 lines · Joanne Koong

Add various tests for the added dynptr convenience helpers.

---
 tools/testing/selftests/bpf/bpf_kfuncs.h      |   6 +
 .../testing/selftests/bpf/prog_tests/dynptr.c |   6 +
 .../testing/selftests/bpf/progs/dynptr_fail.c | 287 +++++++++++++++++
 .../selftests/bpf/progs/dynptr_success.c      | 298 ++++++++++++++++++
 4 files changed, 597 insertions(+)

Very nice and thorough set of tests, thanks a lot! I left a comment below, please follow up with requested improvement.

Applied the patch set to bpf-next. Let's continue discussion about TRUSTED_ARGS and PTR_TO_BTF_ID for dynptr and do necessary follow ups separately.

[...]

Quoted 6 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +6 -0
+/* Invalidating a dynptr should invalidate any data slices
+ * of its clones
+ */
+SEC("?raw_tp")
+__failure __msg("invalid mem access 'scalar'")
+int clone_invalidate4(void *ctx)

tools/testing/selftests/bpf/progs/dynptr_fail.c int invalid_slice_rdwr_rdonly(struct __sk_buff *skb) · line 1549

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +10 -0
+{
+       struct bpf_dynptr ptr;
+       struct bpf_dynptr clone;
+       int *data;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, val, 0, &ptr);
+
+       bpf_dynptr_clone(&ptr, &clone);
+       data = bpf_dynptr_data(&clone, 0, sizeof(val));
+       if (!data)

you'd need bpf_ringbuf_discard_dynptr() here to make sure that compiler code generation changes don't suddenly start failing due to missing dynptr release operation. Please send a small follow up making this more robust.

Quoted 14 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +14 -0
+               return 0;
+
+       bpf_ringbuf_submit_dynptr(&ptr, 0);
+
+       /* this should fail */
+       *data = 123;
+
+       return 0;
+}
+
+/* Invalidating a dynptr should invalidate any data slices
+ * of its parent
+ */
+SEC("?raw_tp")

tools/testing/selftests/bpf/progs/dynptr_fail.c int invalid_slice_rdwr_rdonly(struct __sk_buff *skb) · line 1573

Quoted 10 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +10 -0
+__failure __msg("invalid mem access 'scalar'")
+int clone_invalidate5(void *ctx)
+{
+       struct bpf_dynptr ptr;
+       struct bpf_dynptr clone;
+       int *data;
+
+       bpf_ringbuf_reserve_dynptr(&ringbuf, val, 0, &ptr);
+       data = bpf_dynptr_data(&ptr, 0, sizeof(val));
+       if (!data)

ditto, we need to make sure we violate only one rule (using invalidated slices), not the overall dynptr usage pattern

Quoted 12 lines
tools/testing/selftests/bpf/progs/dynptr_fail.c +12 -0
+               return 0;
+
+       bpf_dynptr_clone(&ptr, &clone);
+
+       bpf_ringbuf_submit_dynptr(&clone, 0);
+
+       /* this should fail */
+       *data = 123;
+
+       return 0;
+}
+

[...]

#22 lore raw
In reply to Joanne Koong
This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Thu, 20 Apr 2023 00:14:09 -0700 you wrote:

This patchset is the 3rd in the dynptr series. The 1st (dynptr fundamentals) can be found here [0] and the second (skb + xdp dynptrs) can be found here [1].

This patchset adds the following helpers for interacting with dynptrs:

[...]

Here is the summary with links:
- [v2,bpf-next,1/5] bpf: Add bpf_dynptr_adjust

    https://git.kernel.org/bpf/bpf-next/c/a18ce61a6c1f
  - [v2,bpf-next,2/5] bpf: Add bpf_dynptr_is_null and bpf_dynptr_is_rdonly
    https://git.kernel.org/bpf/bpf-next/c/8365e486d6b1
  - [v2,bpf-next,3/5] bpf: Add bpf_dynptr_size
    https://git.kernel.org/bpf/bpf-next/c/8acc1b16a56f
  - [v2,bpf-next,4/5] bpf: Add bpf_dynptr_clone
    https://git.kernel.org/bpf/bpf-next/c/cdb12a71426d
  - [v2,bpf-next,5/5] selftests/bpf: add tests for dynptr convenience helpers
    https://git.kernel.org/bpf/bpf-next/c/a63fcb5ba776

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html