BPF List
 help / color / mirror / Atom feed
* [PATCH bpf-next v2 0/9] bpf: Make KF_TRUSTED_ARGS default
@ 2025-12-31 17:08 Puranjay Mohan
  2025-12-31 17:08 ` [PATCH bpf-next v2 1/9] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs Puranjay Mohan
                   ` (8 more replies)
  0 siblings, 9 replies; 35+ messages in thread
From: Puranjay Mohan @ 2025-12-31 17:08 UTC (permalink / raw)
  To: bpf
  Cc: Puranjay Mohan, Puranjay Mohan, Alexei Starovoitov,
	Andrii Nakryiko, Daniel Borkmann, Martin KaFai Lau,
	Eduard Zingerman, Kumar Kartikeya Dwivedi, kernel-team

v1: https://lore.kernel.org/all/20251224192448.3176531-1-puranjay@kernel.org/
Changes in v1->v2:
- Update kfunc_dynptr_param selftest to use a real pointer that is not
  ptr_to_stack and not CONST_PTR_TO_DYNPTR rather than casting 1
  (Alexei)
- Thoroughly review all kfuncs in the to find regressions or missing
  annotations. (Eduard)
- Fix kfuncs found from the above step.

This series makes trusted arguments the default requirement for all BPF
kfuncs, inverting the current opt-in model. Instead of requiring
explicit KF_TRUSTED_ARGS flags, kfuncs now require trusted arguments by
default and must explicitly opt-out using __nullable/__opt annotations
or the KF_RCU flag.

This improves security and type safety by preventing BPF programs from
passing untrusted or NULL pointers to kernel functions at verification
time, while maintaining flexibility for the small number of kfuncs that
legitimately need to accept NULL or RCU pointers.

MOTIVATION

The current opt-in model is error-prone and inconsistent. Most kfuncs already
require trusted pointers from sources like KF_ACQUIRE, struct_ops callbacks, or
tracepoints. Making trusted arguments the default:

- Prevents NULL pointer dereferences at verification time
- Reduces defensive NULL checks in kernel code
- Provides better error messages for invalid BPF programs
- Aligns with existing patterns (context pointers, struct_ops already trusted)

IMPACT ANALYSIS

Comprehensive analysis of all 304+ kfuncs across 37 kernel files found:
- Most kfuncs (299/304) are already safe and require no changes
- Only 4 kfuncs required fixes (all included in this series)
- 0 regressions found in independent verification

TECHNICAL DETAILS

The verifier now validates kfunc arguments in this order:
1. NULL check (runs first): Rejects NULL unless parameter has __nullable/__opt
2. Trusted check: Rejects untrusted pointers unless kfunc has KF_RCU

Special cases that bypass trusted checking:
- Context pointers (xdp_md, __sk_buff): Handled via KF_ARG_PTR_TO_CTX
- Struct_ops callbacks: Pre-marked as PTR_TRUSTED during initialization
- KF_RCU kfuncs: Have separate validation path for RCU pointers

BACKWARD COMPATIBILITY

This affects BPF program verification, not runtime:
- Valid programs passing trusted pointers: Continue to work
- Programs with bugs: May now fail verification (preventing runtime crashes)

Puranjay Mohan (9):
  bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
  bpf: net: netfilter: Mark kfuncs accurately
  bpf: Remove redundant KF_TRUSTED_ARGS flag from all kfuncs
  selftests: bpf: Update kfunc_param_nullable test for new error message
  selftests: bpf: Update failure message for rbtree_fail
  selftests: bpf: fix test_kfunc_dynptr_param
  selftests: bpf: fix cgroup_hierarchical_stats
  bpf: xfrm: drop dead NULL check in bpf_xdp_get_xfrm_state()
  HID: bpf: drop dead NULL checks in kfuncs

 Documentation/bpf/kfuncs.rst                  | 35 +++++++-------
 drivers/hid/bpf/hid_bpf_dispatch.c            |  5 +-
 fs/bpf_fs_kfuncs.c                            | 13 +++---
 fs/verity/measure.c                           |  2 +-
 include/linux/btf.h                           |  3 +-
 kernel/bpf/arena.c                            |  6 +--
 kernel/bpf/cpumask.c                          |  2 +-
 kernel/bpf/helpers.c                          | 20 ++++----
 kernel/bpf/map_iter.c                         |  2 +-
 kernel/bpf/verifier.c                         | 14 ++----
 kernel/sched/ext.c                            |  8 ++--
 mm/bpf_memcontrol.c                           | 10 ++--
 net/core/filter.c                             | 10 ++--
 net/core/xdp.c                                |  2 +-
 net/netfilter/nf_conntrack_bpf.c              | 46 ++++++++++---------
 net/netfilter/nf_flow_table_bpf.c             |  2 +-
 net/netfilter/nf_nat_bpf.c                    |  2 +-
 net/sched/bpf_qdisc.c                         | 12 ++---
 net/xfrm/xfrm_state_bpf.c                     |  2 +-
 .../bpf/progs/cgroup_hierarchical_stats.c     |  6 +--
 .../testing/selftests/bpf/progs/rbtree_fail.c |  2 +-
 .../bpf/progs/test_kfunc_dynptr_param.c       |  5 +-
 .../bpf/progs/test_kfunc_param_nullable.c     |  2 +-
 .../selftests/bpf/test_kmods/bpf_testmod.c    | 20 ++++----
 24 files changed, 109 insertions(+), 122 deletions(-)


base-commit: ccaa6d2c9635a8db06a494d67ef123b56b967a78
-- 
2.47.3


^ permalink raw reply	[flat|nested] 35+ messages in thread

end of thread, other threads:[~2026-01-05 14:52 UTC | newest]

Thread overview: 35+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-12-31 17:08 [PATCH bpf-next v2 0/9] bpf: Make KF_TRUSTED_ARGS default Puranjay Mohan
2025-12-31 17:08 ` [PATCH bpf-next v2 1/9] bpf: Make KF_TRUSTED_ARGS the default for all kfuncs Puranjay Mohan
2025-12-31 17:37   ` bot+bpf-ci
2025-12-31 18:37   ` Eduard Zingerman
2025-12-31 19:00     ` Puranjay Mohan
2025-12-31 19:10       ` Eduard Zingerman
2025-12-31 19:15         ` Puranjay Mohan
2026-01-02  0:15   ` Emil Tsalapatis
2025-12-31 17:08 ` [PATCH bpf-next v2 2/9] bpf: net: netfilter: Mark kfuncs accurately Puranjay Mohan
2025-12-31 17:08 ` [PATCH bpf-next v2 3/9] bpf: Remove redundant KF_TRUSTED_ARGS flag from all kfuncs Puranjay Mohan
2025-12-31 19:13   ` Eduard Zingerman
2026-01-02  0:19   ` Emil Tsalapatis
2025-12-31 17:08 ` [PATCH bpf-next v2 4/9] selftests: bpf: Update kfunc_param_nullable test for new error message Puranjay Mohan
2025-12-31 19:21   ` Eduard Zingerman
2026-01-02  1:45   ` Emil Tsalapatis
2025-12-31 17:08 ` [PATCH bpf-next v2 5/9] selftests: bpf: Update failure message for rbtree_fail Puranjay Mohan
2025-12-31 19:27   ` Eduard Zingerman
2025-12-31 19:44     ` Puranjay Mohan
2025-12-31 19:45       ` Eduard Zingerman
2026-01-02  1:44   ` Emil Tsalapatis
2025-12-31 17:08 ` [PATCH bpf-next v2 6/9] selftests: bpf: fix test_kfunc_dynptr_param Puranjay Mohan
2025-12-31 19:29   ` Eduard Zingerman
2025-12-31 19:39     ` Puranjay Mohan
2025-12-31 19:44       ` Eduard Zingerman
2025-12-31 23:29         ` Puranjay Mohan
2026-01-02  1:44   ` Emil Tsalapatis
2025-12-31 17:08 ` [PATCH bpf-next v2 7/9] selftests: bpf: fix cgroup_hierarchical_stats Puranjay Mohan
2025-12-31 19:40   ` Eduard Zingerman
2026-01-02  1:48   ` Emil Tsalapatis
2025-12-31 17:08 ` [PATCH bpf-next v2 8/9] bpf: xfrm: drop dead NULL check in bpf_xdp_get_xfrm_state() Puranjay Mohan
2025-12-31 19:48   ` Eduard Zingerman
2025-12-31 17:08 ` [PATCH bpf-next v2 9/9] HID: bpf: drop dead NULL checks in kfuncs Puranjay Mohan
2025-12-31 18:20   ` Alexei Starovoitov
2025-12-31 18:25     ` Puranjay Mohan
2026-01-05 14:52       ` Benjamin Tissoires

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox