Introduce kfuncs for memory reads into dynptrs
Subsystems (7)
v3 cover Introduce kfuncs for memory reads into dynptrs
From: Mykyta Yatsenko <[email protected]>
This patch adds new kfuncs that enable reading variable-length user or kernel data directly into dynptrs.
These kfuncs provide a way to perform dynamically-sized reads while maintaining memory safety. Unlike existing bpf_probe_read_{user|kernel} APIs, which are limited to constant-sized reads, these new kfuncs allow for more flexible data access.
Mykyta Yatsenko (3):
helpers: make few bpf helpers public
bpf: implement dynptr copy kfuncs selftests/bpf: introduce tests for dynptr copy kfuncs
include/linux/bpf.h | 14 ++ kernel/bpf/helpers.c | 22 +- kernel/trace/bpf_trace.c | 193 ++++++++++++++++ tools/testing/selftests/bpf/DENYLIST | 1 + .../testing/selftests/bpf/prog_tests/dynptr.c | 13 ++ .../selftests/bpf/progs/dynptr_success.c | 218 ++++++++++++++++++ 6 files changed, 449 insertions(+), 12 deletions(-)
Signature · 1 line
2.49.0
PATCH v3 1/3 helpers: make few bpf helpers public
From: Mykyta Yatsenko <[email protected]>
Make bpf_dynptr_slice_rdwr, bpf_dynptr_check_off_len and __bpf_dynptr_write available outside of the helpers.c by adding their prototypes into linux/include/bpf.h. bpf_dynptr_check_off_len() implementation is moved to header and made inline explicitly, as small function should typically be inlined.
These functions are going to be used from bpf_trace.c in the next patch of this series.
--- include/linux/bpf.h | 14 ++++++++++++++ kernel/bpf/helpers.c | 14 ++------------ 2 files changed, 16 insertions(+), 12 deletions(-)
include/linux/bpf.h +14 -0
@@ -1349,6 +1349,20 @@ u32 __bpf_dynptr_size(const struct bpf_dynptr_kern *ptr);const void *__bpf_dynptr_data (const struct bpf_dynptr_kern *ptr,u32 len);void *__bpf_dynptr_data_rw (const struct bpf_dynptr_kern *ptr,u32 len);bool __bpf_dynptr_is_rdonly (const struct bpf_dynptr_kern *ptr); +int __bpf_dynptr_write (const struct bpf_dynptr_kern *dst,u32 offset, +void *src,u32 len,u64 flags); +void *bpf_dynptr_slice_rdwr (const struct bpf_dynptr *p,u32 offset, +void *buffer__opt,u32 buffer__szk); + +static inline int bpf_dynptr_check_off_len (const struct bpf_dynptr_kern *ptr,u32 offset,u32 len) +{ +u32 size =__bpf_dynptr_size (ptr); + +if (len > size || offset > size - len) +return -E2BIG ; + +return 0 ; +}#ifdef CONFIG_BPF_JIT int bpf_trampoline_link_prog(struct bpf_tramp_link *link,
kernel/bpf/helpers.c +2 -12
@@ -1713,16 +1713,6 @@ void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)memset (ptr,0 ,sizeof (*ptr)); } -static int bpf_dynptr_check_off_len (const struct bpf_dynptr_kern *ptr,u32 offset,u32 len) -{ -u32 size =__bpf_dynptr_size (ptr); - -if (len > size || offset > size - len) -return -E2BIG ; - -return 0 ; -} - BPF_CALL_4(bpf_dynptr_from_mem, void *, data, u32, size, u64, flags, struct bpf_dynptr_kern *, ptr) {int err;
@@ -1809,8 +1799,8 @@ static const struct bpf_func_proto bpf_dynptr_read_proto = { .arg5_type = ARG_ANYTHING, }; -static int __bpf_dynptr_write (const struct bpf_dynptr_kern *dst,u32 offset,void *src, -u32 len,u64 flags) +int __bpf_dynptr_write (const struct bpf_dynptr_kern *dst,u32 offset,void *src, +u32 len,u64 flags) {enum bpf_dynptr_type type;int err;
Signature · 1 line
2.49.0
PATCH v3 2/3 bpf: implement dynptr copy kfuncs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
This patch introduces a new set of kfuncs for working with dynptrs in BPF programs, enabling reading variable-length user or kernel data into dynptr directly. To enable memory-safety, verifier allows only constant-sized reads via existing bpf_probe_read_{user|kernel} etc. kfuncs, dynptr-based kfuncs allow dynamically-sized reads without memory safety shortcomings.
The following kfuncs are introduced:
* bpf_probe_read_kernel_dynptr(): probes kernel-space data into a dynptr
* bpf_probe_read_user_dynptr(): probes user-space data into a dynptr
* bpf_probe_read_kernel_str_dynptr(): probes kernel-space string into a dynptr
* bpf_probe_read_user_str_dynptr(): probes user-space string into a dynptr
* bpf_copy_from_user_dynptr(): sleepable, copies user-space data into a dynptr for the current task
* bpf_copy_from_user_str_dynptr(): sleepable, copies user-space string into a dynptr for the current task
* bpf_copy_from_user_task_dynptr(): sleepable, copies user-space data of the task into a dynptr
* bpf_copy_from_user_task_str_dynptr(): sleepable, copies user-space string of the task into a dynptr
The implementation is built on two generic functions:
* __bpf_dynptr_copy
* __bpf_dynptr_copy_str
These functions take function pointers as arguments, enabling the copying of data from various sources, including both kernel and user space.
Use __always_inline for generic functions and callbacks to make sure the compiler doesn't generate indirect calls into callbacks, which is more expensive, especially on some kernel configurations. Inlining allows compiler to put direct calls into all the specific callback implementations (copy_user_data_sleepable, copy_user_data_nofault, and so on).
--- kernel/bpf/helpers.c | 8 ++ kernel/trace/bpf_trace.c | 193 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+)
kernel/bpf/helpers.c +8 -0
@@ -3284,6 +3284,14 @@ BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLE BTF_ID_FLAGS(func, bpf_iter_kmem_cache_destroy, KF_ITER_DESTROY | KF_SLEEPABLE) BTF_ID_FLAGS(func, bpf_local_irq_save) BTF_ID_FLAGS(func, bpf_local_irq_restore) +BTF_ID_FLAGS(func, bpf_probe_read_user_dynptr) +BTF_ID_FLAGS(func, bpf_probe_read_kernel_dynptr) +BTF_ID_FLAGS(func, bpf_probe_read_user_str_dynptr) +BTF_ID_FLAGS(func, bpf_probe_read_kernel_str_dynptr) +BTF_ID_FLAGS(func, bpf_copy_from_user_dynptr, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_copy_from_user_str_dynptr, KF_SLEEPABLE) +BTF_ID_FLAGS(func, bpf_copy_from_user_task_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS) +BTF_ID_FLAGS(func, bpf_copy_from_user_task_str_dynptr, KF_SLEEPABLE | KF_TRUSTED_ARGS)BTF_KFUNCS_END (common_btf_ids) static const struct btf_kfunc_id_set common_kfunc_set = {
kernel/trace/bpf_trace.c +193 -0
203 lines not on this page · raw message
Signature · 1 line
2.49.0
PATCH v3 3/3 selftests/bpf: introduce tests for dynptr copy kfuncs
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Introduce selftests verifying newly-added dynptr copy kfuncs. Covering contiguous and non-contiguous memory backed dynptrs.
Disable test_probe_read_user_str_dynptr that triggers bug in strncpy_from_user_nofault. Patch to fix the issue [1].
[1] https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/
--- tools/testing/selftests/bpf/DENYLIST | 1 + .../testing/selftests/bpf/prog_tests/dynptr.c | 13 ++ .../selftests/bpf/progs/dynptr_success.c | 218 ++++++++++++++++++ 3 files changed, 232 insertions(+)
tools/testing/selftests/bpf/DENYLIST +1 -0
@@ -1,5 +1,6 @@ # TEMPORARY # Alphabetical order +dynptr/test_probe_read_user_str_dynptr # disabled until https://patchwork.kernel.org/project/linux-mm/patch/[email protected]/ makes it into the bpf-next get_stack_raw_tp # spams with kernel warnings until next bpf -> bpf-next merge stacktrace_build_id stacktrace_build_id_nmi
tools/testing/selftests/bpf/prog_tests/dynptr.c +13 -0
@@ -33,10 +33,19 @@ static struct { {"test_dynptr_skb_no_buff", SETUP_SKB_PROG}, {"test_dynptr_skb_strcmp", SETUP_SKB_PROG}, {"test_dynptr_skb_tp_btf", SETUP_SKB_PROG_TP}, + {"test_probe_read_user_dynptr", SETUP_XDP_PROG}, + {"test_probe_read_kernel_dynptr", SETUP_XDP_PROG}, + {"test_probe_read_user_str_dynptr", SETUP_XDP_PROG}, + {"test_probe_read_kernel_str_dynptr", SETUP_XDP_PROG}, + {"test_copy_from_user_dynptr", SETUP_SYSCALL_SLEEP}, + {"test_copy_from_user_str_dynptr", SETUP_SYSCALL_SLEEP}, + {"test_copy_from_user_task_dynptr", SETUP_SYSCALL_SLEEP}, + {"test_copy_from_user_task_str_dynptr", SETUP_SYSCALL_SLEEP}, };static void verify_success (const char *prog_name,enum test_setup_type setup_type) { +char user_data[384 ] = {[0 ...382 ] ='a' ,'\0' };struct dynptr_success *skel;struct bpf_program *prog;struct bpf_link *link;
@@ -58,6 +67,10 @@ static void verify_success(const char *prog_name, enum test_setup_type setup_typif (!ASSERT_OK (err,"dynptr_success__load" )) goto cleanup; + skel->bss->user_ptr = user_data; + skel->data->test_len[0 ] =sizeof (user_data); +memcpy (skel->bss->expected_str, user_data,sizeof (user_data)); +switch (setup_type) {case SETUP_SYSCALL_SLEEP : link =bpf_program__attach (prog);
tools/testing/selftests/bpf/progs/dynptr_success.c +218 -0
221 lines not on this page · raw message
Signature · 1 line
2.49.0
Re: [PATCH bpf-next v3 3/3] selftests/bpf: introduce tests for dynptr copy kfuncs
Quoted 14 lines · Mykyta Yatsenko
tools/testing/selftests/bpf/progs/dynptr_success.c +14
-0+ +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_dynptr (void *ctx) +{ +test_dynptr_probe (user_ptr, bpf_copy_from_user_dynptr); +return 0 ; +} + +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_str_dynptr (void *ctx) +{ +test_dynptr_probe_str (user_ptr, bpf_copy_from_user_str_dynptr); +return 0 ; +}
...
tools/testing/selftests/bpf/progs/dynptr_success.c int test_dynptr_copy_xdp(struct xdp_md *xdp) · line 900
Quoted 13 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +13
-0+SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_task_dynptr (void *ctx) +{ +test_dynptr_probe (user_ptr, bpf_copy_data_from_user_task); +return 0 ; +} + +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_task_str_dynptr (void *ctx) +{ +test_dynptr_probe_str (user_ptr, bpf_copy_data_from_user_task_str); +return 0 ; +}
you probably need pid filtering here.
Otherwise in test_progs -j case these progs might trigger before test_progs had a chance to setup:
+ skel->bss->user_ptr = user_data;
+ skel->data->test_len[0] = sizeof(user_data);
Or use syscall prog type instead.
pw-bot: cr
Re: [PATCH bpf-next v3 3/3] selftests/bpf: introduce tests for dynptr copy kfuncs
Quoted 36 lines · Alexei Starovoitov
Quoted 14 lines · Mykyta Yatsenko
tools/testing/selftests/bpf/progs/dynptr_success.c +14
-0+ +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_dynptr (void *ctx) +{ +test_dynptr_probe (user_ptr, bpf_copy_from_user_dynptr); +return 0 ; +} + +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_str_dynptr (void *ctx) +{ +test_dynptr_probe_str (user_ptr, bpf_copy_from_user_str_dynptr); +return 0 ; +}...
tools/testing/selftests/bpf/progs/dynptr_success.c int test_dynptr_copy_xdp(struct xdp_md *xdp) · line 900
Quoted 13 lines
tools/testing/selftests/bpf/progs/dynptr_success.c +13
-0+SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_task_dynptr (void *ctx) +{ +test_dynptr_probe (user_ptr, bpf_copy_data_from_user_task); +return 0 ; +} + +SEC("fentry.s/" SYS_PREFIX "sys_nanosleep") +int test_copy_from_user_task_str_dynptr (void *ctx) +{ +test_dynptr_probe_str (user_ptr, bpf_copy_data_from_user_task_str); +return 0 ; +}you probably need pid filtering here.
Otherwise intest_progs-j case these progs might trigger beforetest_progshad a chance to setup:tools/testing/selftests/bpf/prog_tests/dynptr.c +2
-0+ skel->bss->user_ptr = user_data; + skel->data->test_len[0 ] =sizeof (user_data);
Quoted 1 line
Or use syscall prog type instead.
Makes sense, thanks, I'll send next version with the pid filtering.
Quoted 1 line
pw-bot: cr