bpf: make trusted args nullable
Subsystems (6)
v3 cover bpf: make trusted args nullable
Current verifier checks for the arg to be nullable after checking for certain pointer types. It prevents programs to pass NULL to kfunc args even if they are marked as nullable. This patchset adjusts verifier and changes bpf crypto kfuncs to allow null for IV parameter which is optional for some ciphers. Benchmark shows ~4% improvements when there is no need to initialise 0-sized dynptr.
v3:
- add special selftest for nullable parameters
v2:
- adjust kdoc accordingly
Vadim Fedorenko (5):
bpf: verifier: make kfuncs args nullalble
bpf: crypto: make state and IV dynptr nullable
selftests: bpf: crypto: use NULL instead of 0-sized dynptr
selftests: bpf: crypto: adjust bench to use nullable IV
selftests: bpf: add testmod kfunc for nullable params
kernel/bpf/crypto.c | 26 +++++------ kernel/bpf/verifier.c | 6 +-- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/kfunc_param_nullable.c | 11 +++++ .../selftests/bpf/progs/crypto_bench.c | 10 ++--- .../selftests/bpf/progs/crypto_sanity.c | 16 ++----- .../bpf/progs/test_kfunc_param_nullable.c | 43 +++++++++++++++++++ 8 files changed, 85 insertions(+), 34 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_param_nullable.c create mode 100644 tools/testing/selftests/bpf/progs/test_kfunc_param_nullable.c
Signature · 1 line
2.43.0
PATCH RESEND v3 1/5 bpf: verifier: make kfuncs args nullalble
Some arguments to kfuncs might be NULL in some cases. But currently it's not possible to pass NULL to any BTF structures because the check for the suffix is located after all type checks. Move it to earlier place to allow nullable args.
--- kernel/bpf/verifier.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
kernel/bpf/verifier.c +3 -3
@@ -11187,6 +11187,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (btf_is_prog_ctx_type (&env->log, meta->btf, t,resolve_prog_type (env->prog), argno))return KF_ARG_PTR_TO_CTX ; +if (is_kfunc_arg_nullable (meta->btf, &args[argno]) &®ister_is_null (reg)) +return KF_ARG_PTR_TO_NULL ; +if (is_kfunc_arg_alloc_obj (meta->btf, &args[argno]))return KF_ARG_PTR_TO_ALLOC_BTF_ID ;
@@ -11232,9 +11235,6 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (is_kfunc_arg_callback (env, meta->btf, &args[argno]))return KF_ARG_PTR_TO_CALLBACK ; -if (is_kfunc_arg_nullable (meta->btf, &args[argno]) &®ister_is_null (reg)) -return KF_ARG_PTR_TO_NULL ; -if (argno +1 < nargs && (is_kfunc_arg_mem_size (meta->btf, &args[argno +1 ], ®s[regno +1 ]) ||is_kfunc_arg_const_mem_size (meta->btf, &args[argno +1 ], ®s[regno +1 ])))
Signature · 1 line
2.43.0
PATCH RESEND v3 2/5 bpf: crypto: make state and IV dynptr nullable
In reply to Vadim FedorenkoSome ciphers do not require state and IV buffer, but with current implementation 0-sized dynptr is always needed. With adjustment to verifier we can provide NULL instead of 0-sized dynptr. Make crypto kfuncs ready for this.
--- kernel/bpf/crypto.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
kernel/bpf/crypto.c +13 -13
@@ -275,7 +275,7 @@ static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,if (__bpf_dynptr_is_rdonly (dst))return -EINVAL ; - siv_len =__bpf_dynptr_size (siv); + siv_len = siv ?__bpf_dynptr_size (siv) :0 ; src_len =__bpf_dynptr_size (src); dst_len =__bpf_dynptr_size (dst);if (!src_len || !dst_len)
@@ -303,42 +303,42 @@ static int bpf_crypto_crypt(const struct bpf_crypto_ctx *ctx,/** * bpf_crypto_decrypt() - Decrypt buffer using configured context and IV provided. -* @ctx: The crypto context being used. The ctx must be a trusted pointer. -* @src: bpf_dynptr to the encrypted data. Must be a trusted pointer. -* @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer. -* @siv: bpf_dynptr to IV data and state data to be used by decryptor. +* @ctx: The crypto context being used. The ctx must be a trusted pointer. +* @src: bpf_dynptr to the encrypted data. Must be a trusted pointer. +* @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer. +* @siv__nullable: bpf_dynptr to IV data and state data to be used by decryptor. May be NULL. * * Decrypts provided buffer using IV data and the crypto context. Crypto context must be configured. */ __bpf_kfunc int bpf_crypto_decrypt(struct bpf_crypto_ctx *ctx,const struct bpf_dynptr *src,const struct bpf_dynptr *dst, -const struct bpf_dynptr *siv) +const struct bpf_dynptr *siv__nullable) {const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst; -const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv; +const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv__nullable;return bpf_crypto_crypt (ctx, src_kern, dst_kern, siv_kern, true); }/** * bpf_crypto_encrypt() - Encrypt buffer using configured context and IV provided. -* @ctx: The crypto context being used. The ctx must be a trusted pointer. -* @src: bpf_dynptr to the plain data. Must be a trusted pointer. -* @dst: bpf_dynptr to buffer where to store the result. Must be a trusted pointer. -* @siv: bpf_dynptr to IV data and state data to be used by decryptor. +* @ctx: The crypto context being used. The ctx must be a trusted pointer. +* @src: bpf_dynptr to the plain data. Must be a trusted pointer. +* @dst: bpf_dynptr to the buffer where to store the result. Must be a trusted pointer. +* @siv__nullable: bpf_dynptr to IV data and state data to be used by decryptor. May be NULL. * * Encrypts provided buffer using IV data and the crypto context. Crypto context must be configured. */ __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx,const struct bpf_dynptr *src,const struct bpf_dynptr *dst, -const struct bpf_dynptr *siv) +const struct bpf_dynptr *siv__nullable) {const struct bpf_dynptr_kern *src_kern = (struct bpf_dynptr_kern *)src;const struct bpf_dynptr_kern *dst_kern = (struct bpf_dynptr_kern *)dst; -const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv; +const struct bpf_dynptr_kern *siv_kern = (struct bpf_dynptr_kern *)siv__nullable;return bpf_crypto_crypt (ctx, src_kern, dst_kern, siv_kern, false); }
Signature · 1 line
2.43.0
PATCH RESEND v3 3/5 selftests: bpf: crypto: use NULL instead of 0-sized dynptr
In reply to Vadim FedorenkoAdjust selftests to use nullable option for state and IV arg.
--- .../testing/selftests/bpf/progs/crypto_sanity.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-)
tools/testing/selftests/bpf/progs/crypto_sanity.c +4 -12
@@ -89,7 +89,7 @@ int decrypt_sanity(struct __sk_buff *skb) {struct __crypto_ctx_value *v;struct bpf_crypto_ctx *ctx; -struct bpf_dynptr psrc, pdst, iv; +struct bpf_dynptr psrc, pdst;int err; err =skb_dynptr_validate (skb, &psrc);
@@ -114,12 +114,8 @@ int decrypt_sanity(struct __sk_buff *skb)* production code, a percpu map should be used to store the result. */ bpf_dynptr_from_mem (dst,sizeof (dst),0 , &pdst); -/* iv dynptr has to be initialized with 0 size, but proper memory region -* has to be provided anyway -*/ -bpf_dynptr_from_mem (dst,0 ,0 , &iv); - status =bpf_crypto_decrypt (ctx, &psrc, &pdst, &iv); + status =bpf_crypto_decrypt (ctx, &psrc, &pdst,NULL );return TC_ACT_SHOT ; }
@@ -129,7 +125,7 @@ int encrypt_sanity(struct __sk_buff *skb) {struct __crypto_ctx_value *v;struct bpf_crypto_ctx *ctx; -struct bpf_dynptr psrc, pdst, iv; +struct bpf_dynptr psrc, pdst;int err; status =0 ;
@@ -156,12 +152,8 @@ int encrypt_sanity(struct __sk_buff *skb)* production code, a percpu map should be used to store the result. */ bpf_dynptr_from_mem (dst,sizeof (dst),0 , &pdst); -/* iv dynptr has to be initialized with 0 size, but proper memory region -* has to be provided anyway -*/ -bpf_dynptr_from_mem (dst,0 ,0 , &iv); - status =bpf_crypto_encrypt (ctx, &psrc, &pdst, &iv); + status =bpf_crypto_encrypt (ctx, &psrc, &pdst,NULL );return TC_ACT_SHOT ; }
Signature · 1 line
2.43.0
PATCH RESEND v3 4/5 selftests: bpf: crypto: adjust bench to use nullable IV
In reply to Vadim FedorenkoThe bench shows some improvements, around 4% faster on decrypt.
Before:
Benchmark 'crypto-decrypt' started.
Iter 0 (325.719us): hits 5.105M/s ( 5.105M/prod), drops 0.000M/s, total operations 5.105M/s
Iter 1 (-17.295us): hits 5.224M/s ( 5.224M/prod), drops 0.000M/s, total operations 5.224M/s
Iter 2 ( 5.504us): hits 4.630M/s ( 4.630M/prod), drops 0.000M/s, total operations 4.630M/s
Iter 3 ( 9.239us): hits 5.148M/s ( 5.148M/prod), drops 0.000M/s, total operations 5.148M/s
Iter 4 ( 37.885us): hits 5.198M/s ( 5.198M/prod), drops 0.000M/s, total operations 5.198M/s
Iter 5 (-53.282us): hits 5.167M/s ( 5.167M/prod), drops 0.000M/s, total operations 5.167M/s
Iter 6 (-17.809us): hits 5.186M/s ( 5.186M/prod), drops 0.000M/s, total operations 5.186M/s
Summary: hits 5.092 ± 0.228M/s ( 5.092M/prod), drops 0.000 ±0.000M/s, total operations 5.092 ± 0.228M/s
After:
Benchmark 'crypto-decrypt' started.
Iter 0 (268.912us): hits 5.312M/s ( 5.312M/prod), drops 0.000M/s, total operations 5.312M/s
Iter 1 (124.869us): hits 5.354M/s ( 5.354M/prod), drops 0.000M/s, total operations 5.354M/s
Iter 2 (-36.801us): hits 5.334M/s ( 5.334M/prod), drops 0.000M/s, total operations 5.334M/s
Iter 3 (254.628us): hits 5.334M/s ( 5.334M/prod), drops 0.000M/s, total operations 5.334M/s
Iter 4 (-77.691us): hits 5.275M/s ( 5.275M/prod), drops 0.000M/s, total operations 5.275M/s
Iter 5 (-164.510us): hits 5.313M/s ( 5.313M/prod), drops 0.000M/s, total operations 5.313M/s
Iter 6 (-81.376us): hits 5.346M/s ( 5.346M/prod), drops 0.000M/s, total operations 5.346M/s
Summary: hits 5.326 ± 0.029M/s ( 5.326M/prod), drops 0.000 ±0.000M/s, total operations 5.326 ± 0.029M/s
--- tools/testing/selftests/bpf/progs/crypto_bench.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/progs/crypto_bench.c +4 -6
@@ -57,7 +57,7 @@ int crypto_encrypt(struct __sk_buff *skb) {struct __crypto_ctx_value *v;struct bpf_crypto_ctx *ctx; -struct bpf_dynptr psrc, pdst, iv; +struct bpf_dynptr psrc, pdst; v =crypto_ctx_value_lookup ();if (!v) {
@@ -73,9 +73,8 @@ int crypto_encrypt(struct __sk_buff *skb)bpf_dynptr_from_skb (skb,0 , &psrc);bpf_dynptr_from_mem (dst, len,0 , &pdst); -bpf_dynptr_from_mem (dst,0 ,0 , &iv); - status =bpf_crypto_encrypt (ctx, &psrc, &pdst, &iv); + status =bpf_crypto_encrypt (ctx, &psrc, &pdst,NULL );__sync_add_and_fetch (&hits,1 );return 0 ;
@@ -84,7 +83,7 @@ int crypto_encrypt(struct __sk_buff *skb) SEC("tc")int crypto_decrypt (struct __sk_buff *skb) { -struct bpf_dynptr psrc, pdst, iv; +struct bpf_dynptr psrc, pdst;struct __crypto_ctx_value *v;struct bpf_crypto_ctx *ctx;
@@ -98,9 +97,8 @@ int crypto_decrypt(struct __sk_buff *skb)bpf_dynptr_from_skb (skb,0 , &psrc);bpf_dynptr_from_mem (dst, len,0 , &pdst); -bpf_dynptr_from_mem (dst,0 ,0 , &iv); - status =bpf_crypto_decrypt (ctx, &psrc, &pdst, &iv); + status =bpf_crypto_decrypt (ctx, &psrc, &pdst,NULL );__sync_add_and_fetch (&hits,1 );return 0 ;
Signature · 1 line
2.43.0
PATCH RESEND v3 5/5 selftests: bpf: add testmod kfunc for nullable params
In reply to Vadim FedorenkoAdd special test to be sure that only __nullable BTF params can be replaced by NULL. This patch adds fake kfuncs in bpf_testmod to properly test different params.
--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 6 +++ .../bpf/bpf_testmod/bpf_testmod_kfunc.h | 1 + .../bpf/prog_tests/kfunc_param_nullable.c | 11 +++++ .../bpf/progs/test_kfunc_param_nullable.c | 43 +++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_param_nullable.c create mode 100644 tools/testing/selftests/bpf/progs/test_kfunc_param_nullable.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +6 -0
@@ -154,6 +154,11 @@ __bpf_kfunc void bpf_kfunc_common_test(void) { } +__bpf_kfunc void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, +struct bpf_dynptr *ptr__nullable) +{ +} +struct bpf_testmod_btf_type_tag_1 {int a; };
@@ -363,6 +368,7 @@ BTF_ID_FLAGS(func, bpf_iter_testmod_seq_new, KF_ITER_NEW) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_next, KF_ITER_NEXT | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_iter_testmod_seq_destroy, KF_ITER_DESTROY) BTF_ID_FLAGS(func, bpf_kfunc_common_test) +BTF_ID_FLAGS(func, bpf_kfunc_dynptr_test)BTF_KFUNCS_END (bpf_testmod_common_kfunc_ids) static const struct btf_kfunc_id_set bpf_testmod_common_kfunc_set = {
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod_kfunc.h +1 -0
@@ -134,4 +134,5 @@ int bpf_kfunc_call_sock_sendmsg(struct sendmsg_args *args) __ksym; int bpf_kfunc_call_kernel_getsockname(struct addr_args *args) __ksym; int bpf_kfunc_call_kernel_getpeername(struct addr_args *args) __ksym; +void bpf_kfunc_dynptr_test(struct bpf_dynptr *ptr, struct bpf_dynptr *ptr__nullable) __ksym;#endif /* _BPF_TESTMOD_KFUNC_H */
tools/testing/selftests/bpf/prog_tests/kfunc_param_nullable.c new file +11 -0
@@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* Copyright (c) 2024 Meta Platforms, Inc */ + +#include <test_progs.h> +#include "test_kfunc_param_nullable.skel.h" + +void test_kfunc_param_nullable (void ) +{ +RUN_TESTS (test_kfunc_param_nullable); +}
tools/testing/selftests/bpf/progs/test_kfunc_param_nullable.c new file +43 -0
43 lines not on this page · raw message
Signature · 1 line
2.43.0
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 8 lines · On Thu, 13 Jun 2024 14:18:12 -0700 you wrote:
Current verifier checks for the arg to be nullable after checking for certain pointer types. It prevents programs to pass NULL to kfunc args even if they are marked as nullable. This patchset adjusts verifier and changes bpf crypto kfuncs to allow null for IV parameter which is optional for some ciphers. Benchmark shows ~4% improvements when there is no need to initialise 0-sized dynptr.
[...]
Here is the summary with links:
- [RESEND,bpf-next,v3,1/5] bpf: verifier: make kfuncs args nullalble
https://git.kernel.org/bpf/bpf-next/c/a90797993afc - [RESEND,bpf-next,v3,2/5] bpf: crypto: make state and IV dynptr nullable https://git.kernel.org/bpf/bpf-next/c/65d6d61d2596 - [RESEND,bpf-next,v3,3/5] selftests: bpf: crypto: use NULL instead of 0-sized dynptr https://git.kernel.org/bpf/bpf-next/c/9363dc8ddc4e - [RESEND,bpf-next,v3,4/5] selftests: bpf: crypto: adjust bench to use nullable IV https://git.kernel.org/bpf/bpf-next/c/9b560751f75f - [RESEND,bpf-next,v3,5/5] selftests: bpf: add testmod kfunc for nullable params https://git.kernel.org/bpf/bpf-next/c/2d45ab1eda46
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html