All of lore.kernel.org
 help / color / mirror / Atom feed
* [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; 4+ 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] 4+ 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   ` sashiko-bot
  2026-11-18 10:40 ` [RFC PATCH 6.18.y 0/1] bpf: reject " Greg KH
  1 sibling, 1 reply; 4+ 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] 4+ messages in thread

* Re: [RFC PATCH 6.18.y 1/1] bpf: Reject NULL arguments to four kfuncs
  2026-11-18 10:17 ` [RFC PATCH 6.18.y 1/1] bpf: Reject " asatsuyu.liu
@ 2026-11-18 10:40   ` sashiko-bot
  0 siblings, 0 replies; 4+ messages in thread
From: sashiko-bot @ 2026-11-18 10:40 UTC (permalink / raw)
  To: asatsuyu.liu; +Cc: bpf

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Low] bpf: redundant is_kfunc_arg_ignore() check in check_kfunc_args()
--

commit ddaa1152b113559b77a39d01057a7c747399f22a
Author: Yi Liu <asatsuyu.liu@gmail.com>

bpf: Reject NULL arguments to four kfuncs

This commit marks bpf_copy_from_user_str(), bpf_copy_from_user_task_str(),
bpf_crypto_ctx_create(), and bpf_rbtree_remove() with KF_TRUSTED_ARGS so
the verifier rejects NULL or nullable pointer arguments to them.

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index b89a5d717e422..aac687b81b84c 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);

[Severity: Low]
Is this new is_kfunc_arg_ignore() check reachable?  check_kfunc_args()
already skips __ign parameters at the start of the argument loop (where btf
is meta->btf):

kernel/bpf/verifier.c:check_kfunc_args() {
	...
	for (i = 0; i < nargs; i++) {
		...
		if (is_kfunc_arg_ignore(btf, &args[i]))
			continue;
	...
}

Because __ign arguments never reach the NULL-pointer check, both this hunk
and the commit message paragraph describing it appear unnecessary.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118101748.2335815-1-asatsuyu.liu@gmail.com?part=1

^ permalink raw reply	[flat|nested] 4+ 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; 4+ 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] 4+ messages in thread

end of thread, other threads:[~2026-11-18 10:40 UTC | newest]

Thread overview: 4+ 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   ` sashiko-bot
2026-11-18 10:40 ` [RFC PATCH 6.18.y 0/1] bpf: reject " Greg KH

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.