* [RFC PATCH 6.18.y 1/1] bpf: Reject NULL arguments to four kfuncs
2026-11-18 10:17 [RFC PATCH 6.18.y 0/1] bpf: reject NULL arguments to four kfuncs asatsuyu.liu
@ 2026-11-18 10:17 ` asatsuyu.liu
2026-11-18 10:40 ` [RFC PATCH 6.18.y 0/1] bpf: reject " Greg KH
1 sibling, 0 replies; 3+ messages in thread
From: asatsuyu.liu @ 2026-11-18 10:17 UTC (permalink / raw)
To: stable; +Cc: bpf, ast, daniel, puranjay, vadim.fedorenko, Yi Liu
From: Yi Liu <asatsuyu.liu@gmail.com>
6.18.y only applies the kfunc NULL-argument check to functions carrying
KF_TRUSTED_ARGS or KF_RCU. Four kfuncs dereference pointer arguments
unconditionally but do not carry either flag:
bpf_copy_from_user_str()
bpf_copy_from_user_task_str()
bpf_crypto_ctx_create()
bpf_rbtree_remove()
A BPF program can therefore pass a NULL or PTR_MAYBE_NULL argument and
trigger a kernel NULL dereference. Mark the four kfuncs KF_TRUSTED_ARGS
so the existing verifier check rejects such calls.
Skip __ign parameters in that check. bpf_copy_from_user_str() and
bpf_copy_from_user_task_str() intentionally accept
unsafe_ptr__ign == NULL together with BPF_F_PAD_ZEROS, which is used to
clear the destination buffer. Without the skip, the new flag would
reject this valid use.
The patch is intended as a minimal 6.18.y stable fix; mainline has
already made KF_TRUSTED_ARGS the default in v7.0.
Fixes: 65ab5ac4df01 ("bpf: Add bpf_copy_from_user_str kfunc")
Fixes: f0f8a5b58f78 ("bpf: Add bpf_copy_from_user_task_str() kfunc")
Fixes: 3e1c6f35409f ("bpf: make common crypto API for TC/XDP programs")
Fixes: bd1279ae8a69 ("bpf: Add bpf_rbtree_{add,remove,first} kfuncs")
Signed-off-by: Yi Liu <asatsuyu.liu@gmail.com>
---
kernel/bpf/crypto.c | 2 +-
kernel/bpf/helpers.c | 6 +++---
kernel/bpf/verifier.c | 1 +
3 files changed, 5 insertions(+), 4 deletions(-)
diff --git a/kernel/bpf/crypto.c b/kernel/bpf/crypto.c
index 3837d4dcf48d..d1566f76a2c1 100644
--- a/kernel/bpf/crypto.c
+++ b/kernel/bpf/crypto.c
@@ -353,7 +353,7 @@ __bpf_kfunc int bpf_crypto_encrypt(struct bpf_crypto_ctx *ctx,
__bpf_kfunc_end_defs();
BTF_KFUNCS_START(crypt_init_kfunc_btf_ids)
-BTF_ID_FLAGS(func, bpf_crypto_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_crypto_ctx_create, KF_ACQUIRE | KF_RET_NULL | KF_SLEEPABLE | KF_TRUSTED_ARGS)
BTF_ID_FLAGS(func, bpf_crypto_ctx_release, KF_RELEASE)
BTF_ID_FLAGS(func, bpf_crypto_ctx_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_KFUNCS_END(crypt_init_kfunc_btf_ids)
diff --git a/kernel/bpf/helpers.c b/kernel/bpf/helpers.c
index 72757f7290ec..d9e0c95bce02 100644
--- a/kernel/bpf/helpers.c
+++ b/kernel/bpf/helpers.c
@@ -4278,7 +4278,7 @@ BTF_ID_FLAGS(func, bpf_list_front, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_list_back, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_acquire, KF_ACQUIRE | KF_RCU | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_task_release, KF_RELEASE)
-BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL)
+BTF_ID_FLAGS(func, bpf_rbtree_remove, KF_ACQUIRE | KF_RET_NULL | KF_TRUSTED_ARGS)
BTF_ID_FLAGS(func, bpf_rbtree_add_impl)
BTF_ID_FLAGS(func, bpf_rbtree_first, KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_rbtree_root, KF_RET_NULL)
@@ -4365,8 +4365,8 @@ BTF_ID_FLAGS(func, bpf_preempt_enable)
BTF_ID_FLAGS(func, bpf_iter_bits_new, KF_ITER_NEW)
BTF_ID_FLAGS(func, bpf_iter_bits_next, KF_ITER_NEXT | KF_RET_NULL)
BTF_ID_FLAGS(func, bpf_iter_bits_destroy, KF_ITER_DESTROY)
-BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE)
-BTF_ID_FLAGS(func, bpf_copy_from_user_task_str, KF_SLEEPABLE)
+BTF_ID_FLAGS(func, bpf_copy_from_user_str, KF_SLEEPABLE | KF_TRUSTED_ARGS)
+BTF_ID_FLAGS(func, bpf_copy_from_user_task_str, KF_SLEEPABLE | KF_TRUSTED_ARGS)
BTF_ID_FLAGS(func, bpf_get_kmem_cache)
BTF_ID_FLAGS(func, bpf_iter_kmem_cache_new, KF_ITER_NEW | KF_SLEEPABLE)
BTF_ID_FLAGS(func, bpf_iter_kmem_cache_next, KF_ITER_NEXT | KF_RET_NULL | KF_SLEEPABLE)
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b89a5d717e42..aac687b81b84 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -13494,6 +13494,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
}
if ((is_kfunc_trusted_args(meta) || is_kfunc_rcu(meta)) &&
+ !is_kfunc_arg_ignore(meta->btf, &args[i]) &&
(register_is_null(reg) || type_may_be_null(reg->type)) &&
!is_kfunc_arg_nullable(meta->btf, &args[i])) {
verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i);
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [RFC PATCH 6.18.y 0/1] bpf: reject NULL arguments to four kfuncs
2026-11-18 10:17 [RFC PATCH 6.18.y 0/1] bpf: reject NULL arguments to four kfuncs asatsuyu.liu
2026-11-18 10:17 ` [RFC PATCH 6.18.y 1/1] bpf: Reject " asatsuyu.liu
@ 2026-11-18 10:40 ` Greg KH
1 sibling, 0 replies; 3+ messages in thread
From: Greg KH @ 2026-11-18 10:40 UTC (permalink / raw)
To: asatsuyu.liu; +Cc: stable, bpf, ast, daniel, puranjay, vadim.fedorenko
On Wed, Nov 18, 2026 at 10:17:47AM +0000, asatsuyu.liu@gmail.com wrote:
> From: Yi Liu <asatsuyu.liu@gmail.com>
>
> Hi,
>
> 6.18.64 still accepts NULL and PTR_MAYBE_NULL arguments for four kfuncs
> whose implementations dereference them. Mainline fixed this class in
> v7.0 with
>
> 1a5c01d2508a ("bpf: Make KF_TRUSTED_ARGS the default for all kfuncs")
>
> but that commit is a broad verifier behavior change, it was not
> tagged for stable, and it is not in 6.18.y or 6.19.y. 6.18.64 is the
> current 6.18.y tip; 6.19.14 is the current 6.19.y tip.
Ok, but then properly backport that commit, do not od a one-off change,
that is not ok.
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread