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; 8+ 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] 8+ messages in thread

end of thread, other threads:[~2026-11-19 16:08 UTC | newest]

Thread overview: 8+ 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
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 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.