* [RFC PATCH 6.18.y 0/1] bpf: reject NULL arguments to four kfuncs
@ 2026-11-18 10:17 asatsuyu.liu
2026-11-18 10:17 ` [RFC PATCH 6.18.y 1/1] bpf: Reject " asatsuyu.liu
2026-11-18 10:40 ` [RFC PATCH 6.18.y 0/1] bpf: reject " Greg KH
0 siblings, 2 replies; 7+ 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>
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.
The four deterministic reproducers on 6.18.64 are:
1. bpf_copy_from_user_str(dst=NULL, dst__sz=8, src=NULL,
BPF_F_PAD_ZEROS)
-> memset(NULL, 0, 8) in bpf_copy_from_user_str
RIP: memset_orig+0x72, call trace bpf_copy_from_user_str+0x69
2. bpf_copy_from_user_task_str(dst=NULL, dst__sz=16, src=NULL,
tsk=current, BPF_F_PAD_ZEROS)
-> copy_remote_vm_str() writes to dst
RIP: copy_remote_vm_str+0x45,
call trace bpf_copy_from_user_task_str+0x2f
3. bpf_crypto_ctx_create(params, params__sz=0, err=NULL)
-> *err = -EINVAL
RIP: bpf_crypto_ctx_create+0x1f8
faulting insn: c7 45 00 ea ff ff ff (movl $-22, 0(%rbp))
4. bpf_rbtree_remove(root, node=bpf_rbtree_first(root))
with an empty tree; first() returns NULL and remove() reads
node_internal->owner
RIP: bpf_rbtree_remove+0xb, address 0x18
faulting insn: 48 8b 46 18 (mov 0x18(%rsi), %rax, RSI=0)
All four are LOAD_OK on 6.18.64; the four safe controls (non-NULL
dst/err, valid add+remove) load and run.
The 6.18.64 kernel is built from commit fd7cfd39c885 and the QEMU
tests use oops=panic panic=1.
This is a local DoS from a context that can load the relevant
program type. It requires CAP_BPF/CAP_SYS_ADMIN (the tc/rbtree
reproducer also needs CAP_NET_ADMIN), so it is not a privilege
escalation and I have not demonstrated an unprivileged path. With
panic_on_oops it turns into a kernel panic.
The following patch adds KF_TRUSTED_ARGS to the four kfuncs and
teaches the pre-v7.0 NULL guard to skip __ign arguments. The latter is
needed because bpf_copy_from_user_str/task_str intentionally allow
unsafe_ptr__ign == NULL with BPF_F_PAD_ZEROS; without the skip, the
legitimate safe controls fail to load. The diffstat is:
kernel/bpf/crypto.c | 2 +-
kernel/bpf/helpers.c | 6 +++---
kernel/bpf/verifier.c | 1 +
Tested on a patched 6.18.64 kernel:
test unpatched 6.18.64 patched
---------------------------------------------------------------------
copy_str_null_dst LOAD_OK + oops LOAD_FAIL -EACCES (arg0)
copy_task_str_null_dst LOAD_OK + oops LOAD_FAIL -EACCES (arg0)
crypto_null_err LOAD_OK + oops LOAD_FAIL -EACCES (arg2)
rbtree_null_first LOAD_OK + oops LOAD_FAIL -EACCES (arg1)
copy_str_safe (NULL src) LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
copy_task_str_safe (NULL src) LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
crypto_safe LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
rbtree_valid_remove LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
rbtree_safe LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
copy_str_zero_null LOAD_OK, RUN_DONE LOAD_FAIL -EACCES
One behavior change: dst == NULL with dst__sz == 0 is now rejected.
I confirmed that mainline 7.4.0-rc3+ rejects this too; its
copy_str_safe/copy_task_str_safe/rbtree_valid_remove controls still load
and run. If 6.18.y must preserve that case, the
copy helpers can instead get an implementation-side
if (!dst)
return -EINVAL;
after the existing dst__sz == 0 early return. That preserves the
zero-length semantics but leaves the verifier accepting NULL; it only
prevents the oops at runtime.
Fixes tags for a split submission:
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")
Thanks,
Yi Liu
Yi Liu (1):
bpf: Reject NULL arguments to four kfuncs
kernel/bpf/crypto.c | 2 +-
kernel/bpf/helpers.c | 6 +++---
kernel/bpf/verifier.c | 1 +
3 files changed, 5 insertions(+), 4 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [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; 7+ 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] 7+ 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
2026-11-18 14:26 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs asatsuyu.liu
1 sibling, 1 reply; 7+ 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] 7+ messages in thread
* [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
2026-11-18 10:40 ` [RFC PATCH 6.18.y 0/1] bpf: reject " Greg KH
@ 2026-11-18 14:26 ` asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 1/2] " asatsuyu.liu
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: asatsuyu.liu @ 2026-11-18 14:26 UTC (permalink / raw)
To: gregkh; +Cc: asatsuyu.liu, ast, bpf, daniel, puranjay, stable, vadim.fedorenko
From: Yi Liu <asatsuyu.liu@gmail.com>
Hi Greg,
Thanks for the guidance. Here is a direct backport of upstream commit
1a5c01d2508a ("bpf: Make KF_TRUSTED_ARGS the default for all kfuncs")
to v6.18.64.
The series has two direct cherry-picks:
1. 1a5c01d2508a bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
2. cf503eb2c6c3 selftests: bpf: Fix test_bpf_nf for trusted args
becoming default
Both apply cleanly to v6.18.64. Patch 2 is the immediate upstream selftest
fix for the behavior change: 6.18's test_bpf_nf still passed a NULL tuple
to the conntrack lookup kfunc to test a runtime error, and after patch 1
that program is rejected at verification time.
I tested the code backport in QEMU with the same reproducers:
test unpatched 6.18.64 backport
---------------------------------------------------------------------
copy_str_null_dst LOAD_OK + oops LOAD_FAIL -EACCES (arg0)
copy_task_str_null_dst LOAD_OK + oops LOAD_FAIL -EACCES (arg0)
crypto_null_err LOAD_OK + oops LOAD_FAIL -EACCES (arg2)
rbtree_null_first LOAD_OK + oops LOAD_FAIL -EACCES (arg1)
copy_str_safe (NULL src) LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
copy_task_str_safe (NULL src) LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
crypto_safe LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
rbtree_valid_remove LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
rbtree_safe LOAD_OK, RUN_DONE LOAD_OK, RUN_DONE
copy_str_zero_null LOAD_OK, RUN_DONE LOAD_FAIL -EACCES
No oops with the backport. As documented in the upstream commit, this
is a verifier behavior change: kfuncs that previously accepted NULL
without KF_TRUSTED_ARGS now reject it.
The commit message specifically mentions the netfilter kfuncs affected
by this, so I ran the targeted selftests in the BPF CI container:
bpf_nf: 14/14 PASSED, including lookup_null_bpf_tuple/opts and
xdp_lookup_null_bpf_tuple/opts.
Thanks,
Yi Liu
Puranjay Mohan (2):
bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
selftests: bpf: Fix test_bpf_nf for trusted args becoming default
Documentation/bpf/kfuncs.rst | 184 +++++++++---------
fs/bpf_fs_kfuncs.c | 10 +-
kernel/bpf/verifier.c | 14 +-
.../testing/selftests/bpf/prog_tests/bpf_nf.c | 5 +-
.../testing/selftests/bpf/progs/test_bpf_nf.c | 7 -
.../selftests/bpf/progs/test_bpf_nf_fail.c | 57 ++++++
6 files changed, 158 insertions(+), 119 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 6.18.y 1/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
2026-11-18 14:26 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs asatsuyu.liu
@ 2026-11-18 14:26 ` asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 2/2] selftests: bpf: Fix test_bpf_nf for trusted args becoming default asatsuyu.liu
2026-11-19 16:08 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs Sasha Levin
2 siblings, 0 replies; 7+ messages in thread
From: asatsuyu.liu @ 2026-11-18 14:26 UTC (permalink / raw)
To: gregkh; +Cc: asatsuyu.liu, ast, bpf, daniel, puranjay, stable, vadim.fedorenko
From: Puranjay Mohan <puranjay@kernel.org>
Change the verifier to make trusted args the default requirement for
all kfuncs by removing is_kfunc_trusted_args() assuming it be to always
return true.
This works because:
1. Context pointers (xdp_md, __sk_buff, etc.) are handled through their
own KF_ARG_PTR_TO_CTX case label and bypass the trusted check
2. Struct_ops callback arguments are already marked as PTR_TRUSTED during
initialization and pass is_trusted_reg()
3. KF_RCU kfuncs are handled separately via is_kfunc_rcu() checks at
call sites (always checked with || alongside is_kfunc_trusted_args)
This simple change makes all kfuncs require trusted args by default
while maintaining correct behavior for all existing special cases.
Note: This change means kfuncs that previously accepted NULL pointers
without KF_TRUSTED_ARGS will now reject NULL at verification time.
Several netfilter kfuncs are affected: bpf_xdp_ct_lookup(),
bpf_skb_ct_lookup(), bpf_xdp_ct_alloc(), and bpf_skb_ct_alloc() all
accept NULL for their bpf_tuple and opts parameters internally (checked
in __bpf_nf_ct_lookup), but after this change the verifier rejects NULL
before the kfunc is even called. This is acceptable because these kfuncs
don't work with NULL parameters in their proper usage. Now they will be
rejected rather than returning an error, which shouldn't make a
difference to BPF programs that were using these kfuncs properly.
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Reviewed-by: Emil Tsalapatis <emil@etsalapatis.com>
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20260102180038.2708325-2-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
(cherry picked from commit 1a5c01d2508a845825eece360c6145d7f436dbf8)
Signed-off-by: Yi Liu <asatsuyu.liu@gmail.com>
---
Documentation/bpf/kfuncs.rst | 184 +++++++++++++++++------------------
fs/bpf_fs_kfuncs.c | 10 +-
kernel/bpf/verifier.c | 14 +--
3 files changed, 97 insertions(+), 111 deletions(-)
diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
index e38941370b90..6cb6857bfa6f 100644
--- a/Documentation/bpf/kfuncs.rst
+++ b/Documentation/bpf/kfuncs.rst
@@ -50,7 +50,70 @@ A wrapper kfunc is often needed when we need to annotate parameters of the
kfunc. Otherwise one may directly make the kfunc visible to the BPF program by
registering it with the BPF subsystem. See :ref:`BPF_kfunc_nodef`.
-2.2 Annotating kfunc parameters
+2.2 kfunc Parameters
+--------------------
+
+All kfuncs now require trusted arguments by default. This means that all
+pointer arguments must be valid, and all pointers to BTF objects must be
+passed in their unmodified form (at a zero offset, and without having been
+obtained from walking another pointer, with exceptions described below).
+
+There are two types of pointers to kernel objects which are considered "trusted":
+
+1. Pointers which are passed as tracepoint or struct_ops callback arguments.
+2. Pointers which were returned from a KF_ACQUIRE kfunc.
+
+Pointers to non-BTF objects (e.g. scalar pointers) may also be passed to
+kfuncs, and may have a non-zero offset.
+
+The definition of "valid" pointers is subject to change at any time, and has
+absolutely no ABI stability guarantees.
+
+As mentioned above, a nested pointer obtained from walking a trusted pointer is
+no longer trusted, with one exception. If a struct type has a field that is
+guaranteed to be valid (trusted or rcu, as in KF_RCU description below) as long
+as its parent pointer is valid, the following macros can be used to express
+that to the verifier:
+
+* ``BTF_TYPE_SAFE_TRUSTED``
+* ``BTF_TYPE_SAFE_RCU``
+* ``BTF_TYPE_SAFE_RCU_OR_NULL``
+
+For example,
+
+.. code-block:: c
+
+ BTF_TYPE_SAFE_TRUSTED(struct socket) {
+ struct sock *sk;
+ };
+
+or
+
+.. code-block:: c
+
+ BTF_TYPE_SAFE_RCU(struct task_struct) {
+ const cpumask_t *cpus_ptr;
+ struct css_set __rcu *cgroups;
+ struct task_struct __rcu *real_parent;
+ struct task_struct *group_leader;
+ };
+
+In other words, you must:
+
+1. Wrap the valid pointer type in a ``BTF_TYPE_SAFE_*`` macro.
+
+2. Specify the type and name of the valid nested field. This field must match
+ the field in the original type definition exactly.
+
+A new type declared by a ``BTF_TYPE_SAFE_*`` macro also needs to be emitted so
+that it appears in BTF. For example, ``BTF_TYPE_SAFE_TRUSTED(struct socket)``
+is emitted in the ``type_is_trusted()`` function as follows:
+
+.. code-block:: c
+
+ BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct socket));
+
+2.3 Annotating kfunc parameters
-------------------------------
Similar to BPF helpers, there is sometime need for additional context required
@@ -58,7 +121,7 @@ by the verifier to make the usage of kernel functions safer and more useful.
Hence, we can annotate a parameter by suffixing the name of the argument of the
kfunc with a __tag, where tag may be one of the supported annotations.
-2.2.1 __sz Annotation
+2.3.1 __sz Annotation
---------------------
This annotation is used to indicate a memory and size pair in the argument list.
@@ -74,7 +137,7 @@ argument as its size. By default, without __sz annotation, the size of the type
of the pointer is used. Without __sz annotation, a kfunc cannot accept a void
pointer.
-2.2.2 __k Annotation
+2.3.2 __k Annotation
--------------------
This annotation is only understood for scalar arguments, where it indicates that
@@ -98,7 +161,7 @@ Hence, whenever a constant scalar argument is accepted by a kfunc which is not a
size parameter, and the value of the constant matters for program safety, __k
suffix should be used.
-2.2.3 __uninit Annotation
+2.3.3 __uninit Annotation
-------------------------
This annotation is used to indicate that the argument will be treated as
@@ -115,7 +178,7 @@ Here, the dynptr will be treated as an uninitialized dynptr. Without this
annotation, the verifier will reject the program if the dynptr passed in is
not initialized.
-2.2.4 __opt Annotation
+2.3.4 __opt Annotation
-------------------------
This annotation is used to indicate that the buffer associated with an __sz or __szk
@@ -135,7 +198,7 @@ Either way, the returned buffer is either NULL, or of size buffer_szk. Without t
annotation, the verifier will reject the program if a null pointer is passed in with
a nonzero size.
-2.2.5 __str Annotation
+2.3.5 __str Annotation
----------------------------
This annotation is used to indicate that the argument is a constant string.
@@ -160,7 +223,7 @@ Or::
...
}
-2.2.6 __prog Annotation
+2.3.6 __prog Annotation
---------------------------
This annotation is used to indicate that the argument needs to be fixed up to
the bpf_prog_aux of the caller BPF program. Any value passed into this argument
@@ -179,7 +242,7 @@ An example is given below::
.. _BPF_kfunc_nodef:
-2.3 Using an existing kernel function
+2.4 Using an existing kernel function
-------------------------------------
When an existing function in the kernel is fit for consumption by BPF programs,
@@ -187,7 +250,7 @@ it can be directly registered with the BPF subsystem. However, care must still
be taken to review the context in which it will be invoked by the BPF program
and whether it is safe to do so.
-2.4 Annotating kfuncs
+2.5 Annotating kfuncs
---------------------
In addition to kfuncs' arguments, verifier may need more information about the
@@ -216,7 +279,7 @@ protected. An example is given below::
...
}
-2.4.1 KF_ACQUIRE flag
+2.5.1 KF_ACQUIRE flag
---------------------
The KF_ACQUIRE flag is used to indicate that the kfunc returns a pointer to a
@@ -226,7 +289,7 @@ referenced kptr (by invoking bpf_kptr_xchg). If not, the verifier fails the
loading of the BPF program until no lingering references remain in all possible
explored states of the program.
-2.4.2 KF_RET_NULL flag
+2.5.2 KF_RET_NULL flag
----------------------
The KF_RET_NULL flag is used to indicate that the pointer returned by the kfunc
@@ -235,87 +298,21 @@ returned from the kfunc before making use of it (dereferencing or passing to
another helper). This flag is often used in pairing with KF_ACQUIRE flag, but
both are orthogonal to each other.
-2.4.3 KF_RELEASE flag
+2.5.3 KF_RELEASE flag
---------------------
The KF_RELEASE flag is used to indicate that the kfunc releases the pointer
passed in to it. There can be only one referenced pointer that can be passed
in. All copies of the pointer being released are invalidated as a result of
-invoking kfunc with this flag. KF_RELEASE kfuncs automatically receive the
-protection afforded by the KF_TRUSTED_ARGS flag described below.
-
-2.4.4 KF_TRUSTED_ARGS flag
---------------------------
-
-The KF_TRUSTED_ARGS flag is used for kfuncs taking pointer arguments. It
-indicates that the all pointer arguments are valid, and that all pointers to
-BTF objects have been passed in their unmodified form (that is, at a zero
-offset, and without having been obtained from walking another pointer, with one
-exception described below).
-
-There are two types of pointers to kernel objects which are considered "valid":
-
-1. Pointers which are passed as tracepoint or struct_ops callback arguments.
-2. Pointers which were returned from a KF_ACQUIRE kfunc.
-
-Pointers to non-BTF objects (e.g. scalar pointers) may also be passed to
-KF_TRUSTED_ARGS kfuncs, and may have a non-zero offset.
-
-The definition of "valid" pointers is subject to change at any time, and has
-absolutely no ABI stability guarantees.
-
-As mentioned above, a nested pointer obtained from walking a trusted pointer is
-no longer trusted, with one exception. If a struct type has a field that is
-guaranteed to be valid (trusted or rcu, as in KF_RCU description below) as long
-as its parent pointer is valid, the following macros can be used to express
-that to the verifier:
-
-* ``BTF_TYPE_SAFE_TRUSTED``
-* ``BTF_TYPE_SAFE_RCU``
-* ``BTF_TYPE_SAFE_RCU_OR_NULL``
-
-For example,
-
-.. code-block:: c
-
- BTF_TYPE_SAFE_TRUSTED(struct socket) {
- struct sock *sk;
- };
-
-or
-
-.. code-block:: c
-
- BTF_TYPE_SAFE_RCU(struct task_struct) {
- const cpumask_t *cpus_ptr;
- struct css_set __rcu *cgroups;
- struct task_struct __rcu *real_parent;
- struct task_struct *group_leader;
- };
-
-In other words, you must:
-
-1. Wrap the valid pointer type in a ``BTF_TYPE_SAFE_*`` macro.
-
-2. Specify the type and name of the valid nested field. This field must match
- the field in the original type definition exactly.
-
-A new type declared by a ``BTF_TYPE_SAFE_*`` macro also needs to be emitted so
-that it appears in BTF. For example, ``BTF_TYPE_SAFE_TRUSTED(struct socket)``
-is emitted in the ``type_is_trusted()`` function as follows:
-
-.. code-block:: c
-
- BTF_TYPE_EMIT(BTF_TYPE_SAFE_TRUSTED(struct socket));
-
+invoking kfunc with this flag.
-2.4.5 KF_SLEEPABLE flag
+2.5.4 KF_SLEEPABLE flag
-----------------------
The KF_SLEEPABLE flag is used for kfuncs that may sleep. Such kfuncs can only
be called by sleepable BPF programs (BPF_F_SLEEPABLE).
-2.4.6 KF_DESTRUCTIVE flag
+2.5.5 KF_DESTRUCTIVE flag
--------------------------
The KF_DESTRUCTIVE flag is used to indicate functions calling which is
@@ -324,18 +321,19 @@ rebooting or panicking. Due to this additional restrictions apply to these
calls. At the moment they only require CAP_SYS_BOOT capability, but more can be
added later.
-2.4.7 KF_RCU flag
+2.5.6 KF_RCU flag
-----------------
-The KF_RCU flag is a weaker version of KF_TRUSTED_ARGS. The kfuncs marked with
-KF_RCU expect either PTR_TRUSTED or MEM_RCU arguments. The verifier guarantees
-that the objects are valid and there is no use-after-free. The pointers are not
-NULL, but the object's refcount could have reached zero. The kfuncs need to
-consider doing refcnt != 0 check, especially when returning a KF_ACQUIRE
-pointer. Note as well that a KF_ACQUIRE kfunc that is KF_RCU should very likely
-also be KF_RET_NULL.
+The KF_RCU flag allows kfuncs to opt out of the default trusted args
+requirement and accept RCU pointers with weaker guarantees. The kfuncs marked
+with KF_RCU expect either PTR_TRUSTED or MEM_RCU arguments. The verifier
+guarantees that the objects are valid and there is no use-after-free. The
+pointers are not NULL, but the object's refcount could have reached zero. The
+kfuncs need to consider doing refcnt != 0 check, especially when returning a
+KF_ACQUIRE pointer. Note as well that a KF_ACQUIRE kfunc that is KF_RCU should
+very likely also be KF_RET_NULL.
-2.4.8 KF_RCU_PROTECTED flag
+2.5.7 KF_RCU_PROTECTED flag
---------------------------
The KF_RCU_PROTECTED flag is used to indicate that the kfunc must be invoked in
@@ -354,7 +352,7 @@ RCU protection but do not take RCU protected arguments.
.. _KF_deprecated_flag:
-2.4.9 KF_DEPRECATED flag
+2.5.8 KF_DEPRECATED flag
------------------------
The KF_DEPRECATED flag is used for kfuncs which are scheduled to be
@@ -374,7 +372,7 @@ encouraged to make their use-cases known as early as possible, and participate
in upstream discussions regarding whether to keep, change, deprecate, or remove
those kfuncs if and when such discussions occur.
-2.5 Registering the kfuncs
+2.6 Registering the kfuncs
--------------------------
Once the kfunc is prepared for use, the final step to making it visible is
@@ -397,7 +395,7 @@ type. An example is shown below::
}
late_initcall(init_subsystem);
-2.6 Specifying no-cast aliases with ___init
+2.7 Specifying no-cast aliases with ___init
--------------------------------------------
The verifier will always enforce that the BTF type of a pointer passed to a
diff --git a/fs/bpf_fs_kfuncs.c b/fs/bpf_fs_kfuncs.c
index b60e0698ce13..97cb06877754 100644
--- a/fs/bpf_fs_kfuncs.c
+++ b/fs/bpf_fs_kfuncs.c
@@ -68,10 +68,7 @@ __bpf_kfunc void bpf_put_file(struct file *file)
*
* Resolve the pathname for the supplied *path* and store it in *buf*. This BPF
* kfunc is the safer variant of the legacy bpf_d_path() helper and should be
- * used in place of bpf_d_path() whenever possible. It enforces KF_TRUSTED_ARGS
- * semantics, meaning that the supplied *path* must itself hold a valid
- * reference, or else the BPF program will be outright rejected by the BPF
- * verifier.
+ * used in place of bpf_d_path() whenever possible.
*
* This BPF kfunc may only be called from BPF LSM programs.
*
@@ -383,9 +380,8 @@ static int bpf_fs_kfuncs_filter(const struct bpf_prog *prog, u32 kfunc_id)
return -EACCES;
}
-/* bpf_[set|remove]_dentry_xattr.* hooks have KF_TRUSTED_ARGS and
- * KF_SLEEPABLE, so they are only available to sleepable hooks with
- * dentry arguments.
+/* bpf_[set|remove]_dentry_xattr.* hooks have KF_SLEEPABLE, so they are only
+ * available to sleepable hooks with dentry arguments.
*
* Setting and removing xattr requires exclusive lock on dentry->d_inode.
* Some hooks already locked d_inode, while some hooks have not locked
diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
index b89a5d717e42..3ef78a702685 100644
--- a/kernel/bpf/verifier.c
+++ b/kernel/bpf/verifier.c
@@ -12281,11 +12281,6 @@ static bool is_kfunc_release(struct bpf_kfunc_call_arg_meta *meta)
return meta->kfunc_flags & KF_RELEASE;
}
-static bool is_kfunc_trusted_args(struct bpf_kfunc_call_arg_meta *meta)
-{
- return (meta->kfunc_flags & KF_TRUSTED_ARGS) || is_kfunc_release(meta);
-}
-
static bool is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta)
{
return meta->kfunc_flags & KF_SLEEPABLE;
@@ -13493,9 +13488,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
return -EINVAL;
}
- if ((is_kfunc_trusted_args(meta) || is_kfunc_rcu(meta)) &&
- (register_is_null(reg) || type_may_be_null(reg->type)) &&
- !is_kfunc_arg_nullable(meta->btf, &args[i])) {
+ if ((register_is_null(reg) || type_may_be_null(reg->type)) &&
+ !is_kfunc_arg_nullable(meta->btf, &args[i]) &&
+ !is_kfunc_arg_optional(meta->btf, &args[i])) {
verbose(env, "Possibly NULL pointer passed to trusted arg%d\n", i);
return -EACCES;
}
@@ -13560,9 +13555,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_
fallthrough;
case KF_ARG_PTR_TO_ALLOC_BTF_ID:
case KF_ARG_PTR_TO_BTF_ID:
- if (!is_kfunc_trusted_args(meta) && !is_kfunc_rcu(meta))
- break;
-
if (!is_trusted_reg(reg)) {
if (!is_kfunc_rcu(meta)) {
verbose(env, "R%d must be referenced or trusted\n", regno);
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH 6.18.y 2/2] selftests: bpf: Fix test_bpf_nf for trusted args becoming default
2026-11-18 14:26 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 1/2] " asatsuyu.liu
@ 2026-11-18 14:26 ` asatsuyu.liu
2026-11-19 16:08 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs Sasha Levin
2 siblings, 0 replies; 7+ messages in thread
From: asatsuyu.liu @ 2026-11-18 14:26 UTC (permalink / raw)
To: gregkh; +Cc: asatsuyu.liu, ast, bpf, daniel, puranjay, stable, vadim.fedorenko
From: Puranjay Mohan <puranjay@kernel.org>
With trusted args now being the default, passing NULL to kfunc
parameters that are pointers causes verifier rejection rather than a
runtime error. The test_bpf_nf test was failing because it attempted to
pass NULL to bpf_xdp_ct_lookup() to verify runtime error handling.
Since the NULL check now happens at verification time, remove the
runtime test case that passed NULL to the bpf_tuple parameter and
instead add verification-time tests to ensure the verifier correctly
rejects programs that pass NULL to trusted arguments.
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Link: https://lore.kernel.org/r/20260102180038.2708325-11-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
(cherry picked from commit cf503eb2c6c38bf449063f33790a96218a067718)
Signed-off-by: Yi Liu <asatsuyu.liu@gmail.com>
---
.../testing/selftests/bpf/prog_tests/bpf_nf.c | 5 +-
.../testing/selftests/bpf/progs/test_bpf_nf.c | 7 ---
.../selftests/bpf/progs/test_bpf_nf_fail.c | 57 +++++++++++++++++++
3 files changed, 61 insertions(+), 8 deletions(-)
diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c
index dd6512fa652b..215878ea04de 100644
--- a/tools/testing/selftests/bpf/prog_tests/bpf_nf.c
+++ b/tools/testing/selftests/bpf/prog_tests/bpf_nf.c
@@ -19,6 +19,10 @@ struct {
{ "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout args#0 expected pointer to STRUCT nf_conn but" },
{ "change_status_after_alloc", "kernel function bpf_ct_change_status args#0 expected pointer to STRUCT nf_conn but" },
{ "write_not_allowlisted_field", "no write support to nf_conn at off" },
+ { "lookup_null_bpf_tuple", "Possibly NULL pointer passed to trusted arg1" },
+ { "lookup_null_bpf_opts", "Possibly NULL pointer passed to trusted arg3" },
+ { "xdp_lookup_null_bpf_tuple", "Possibly NULL pointer passed to trusted arg1" },
+ { "xdp_lookup_null_bpf_opts", "Possibly NULL pointer passed to trusted arg3" },
};
enum {
@@ -111,7 +115,6 @@ static void test_bpf_nf_ct(int mode)
if (!ASSERT_OK(err, "bpf_prog_test_run"))
goto end;
- ASSERT_EQ(skel->bss->test_einval_bpf_tuple, -EINVAL, "Test EINVAL for NULL bpf_tuple");
ASSERT_EQ(skel->bss->test_einval_reserved, -EINVAL, "Test EINVAL for reserved not set to 0");
ASSERT_EQ(skel->bss->test_einval_reserved_new, -EINVAL, "Test EINVAL for reserved in new struct not set to 0");
ASSERT_EQ(skel->bss->test_einval_netns_id, -EINVAL, "Test EINVAL for netns_id < -1");
diff --git a/tools/testing/selftests/bpf/progs/test_bpf_nf.c b/tools/testing/selftests/bpf/progs/test_bpf_nf.c
index b42e6e149e26..ad03ee504cec 100644
--- a/tools/testing/selftests/bpf/progs/test_bpf_nf.c
+++ b/tools/testing/selftests/bpf/progs/test_bpf_nf.c
@@ -15,7 +15,6 @@
extern unsigned long CONFIG_HZ __kconfig;
-int test_einval_bpf_tuple = 0;
int test_einval_reserved = 0;
int test_einval_reserved_new = 0;
int test_einval_netns_id = 0;
@@ -99,12 +98,6 @@ nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,
__builtin_memset(&bpf_tuple, 0, sizeof(bpf_tuple.ipv4));
- ct = lookup_fn(ctx, NULL, 0, &opts_def, sizeof(opts_def));
- if (ct)
- bpf_ct_release(ct);
- else
- test_einval_bpf_tuple = opts_def.error;
-
opts_def.reserved[0] = 1;
ct = lookup_fn(ctx, &bpf_tuple, sizeof(bpf_tuple.ipv4), &opts_def,
sizeof(opts_def));
diff --git a/tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c b/tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c
index a586f087ffeb..2c156cd166af 100644
--- a/tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c
+++ b/tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c
@@ -4,6 +4,7 @@
#include <bpf/bpf_tracing.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
+#include "bpf_misc.h"
struct nf_conn;
@@ -18,6 +19,10 @@ struct nf_conn *bpf_skb_ct_alloc(struct __sk_buff *, struct bpf_sock_tuple *, u3
struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_skb_ct_lookup(struct __sk_buff *, struct bpf_sock_tuple *, u32,
struct bpf_ct_opts___local *, u32) __ksym;
+struct nf_conn *bpf_xdp_ct_alloc(struct xdp_md *, struct bpf_sock_tuple *, u32,
+ struct bpf_ct_opts___local *, u32) __ksym;
+struct nf_conn *bpf_xdp_ct_lookup(struct xdp_md *, struct bpf_sock_tuple *, u32,
+ struct bpf_ct_opts___local *, u32) __ksym;
struct nf_conn *bpf_ct_insert_entry(struct nf_conn *) __ksym;
void bpf_ct_release(struct nf_conn *) __ksym;
void bpf_ct_set_timeout(struct nf_conn *, u32) __ksym;
@@ -146,4 +151,56 @@ int change_status_after_alloc(struct __sk_buff *ctx)
return 0;
}
+SEC("?tc")
+__failure __msg("Possibly NULL pointer passed to trusted arg1")
+int lookup_null_bpf_tuple(struct __sk_buff *ctx)
+{
+ struct bpf_ct_opts___local opts = {};
+ struct nf_conn *ct;
+
+ ct = bpf_skb_ct_lookup(ctx, NULL, 0, &opts, sizeof(opts));
+ if (ct)
+ bpf_ct_release(ct);
+ return 0;
+}
+
+SEC("?tc")
+__failure __msg("Possibly NULL pointer passed to trusted arg3")
+int lookup_null_bpf_opts(struct __sk_buff *ctx)
+{
+ struct bpf_sock_tuple tup = {};
+ struct nf_conn *ct;
+
+ ct = bpf_skb_ct_lookup(ctx, &tup, sizeof(tup.ipv4), NULL, sizeof(struct bpf_ct_opts___local));
+ if (ct)
+ bpf_ct_release(ct);
+ return 0;
+}
+
+SEC("?xdp")
+__failure __msg("Possibly NULL pointer passed to trusted arg1")
+int xdp_lookup_null_bpf_tuple(struct xdp_md *ctx)
+{
+ struct bpf_ct_opts___local opts = {};
+ struct nf_conn *ct;
+
+ ct = bpf_xdp_ct_lookup(ctx, NULL, 0, &opts, sizeof(opts));
+ if (ct)
+ bpf_ct_release(ct);
+ return 0;
+}
+
+SEC("?xdp")
+__failure __msg("Possibly NULL pointer passed to trusted arg3")
+int xdp_lookup_null_bpf_opts(struct xdp_md *ctx)
+{
+ struct bpf_sock_tuple tup = {};
+ struct nf_conn *ct;
+
+ ct = bpf_xdp_ct_lookup(ctx, &tup, sizeof(tup.ipv4), NULL, sizeof(struct bpf_ct_opts___local));
+ if (ct)
+ bpf_ct_release(ct);
+ return 0;
+}
+
char _license[] SEC("license") = "GPL";
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
2026-11-18 14:26 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 1/2] " asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 2/2] selftests: bpf: Fix test_bpf_nf for trusted args becoming default asatsuyu.liu
@ 2026-11-19 16:08 ` Sasha Levin
2 siblings, 0 replies; 7+ messages in thread
From: Sasha Levin @ 2026-11-19 16:08 UTC (permalink / raw)
To: gregkh
Cc: Sasha Levin, asatsuyu.liu, ast, bpf, daniel, puranjay, stable,
vadim.fedorenko
> Here is a direct backport of upstream commit
> 1a5c01d2508a ("bpf: Make KF_TRUSTED_ARGS the default for all kfuncs")
> to v6.18.64.
This changes verifier behaviour for every kfunc: programs that load today
(for instance NULL arguments to the netfilter conntrack kfuncs) will be
rejected. The series also leaves out the other selftest fixes that went in
with 1a5c01d2508a ("bpf: Make KF_TRUSTED_ARGS the default for all kfuncs").
I'm holding this until a BPF maintainer acks it for stable.
--
Thanks,
Sasha
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-11-19 16:08 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH 6.18.y 0/1] bpf: reject " Greg KH
2026-11-18 14:26 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 1/2] " asatsuyu.liu
2026-11-18 14:26 ` [PATCH 6.18.y 2/2] selftests: bpf: Fix test_bpf_nf for trusted args becoming default asatsuyu.liu
2026-11-19 16:08 ` [PATCH 6.18.y 0/2] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs Sasha Levin
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox