Skip to content

bpf: Make KF_TRUSTED_ARGS default

Puranjay Mohan bpf 3 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest10 patches 0 repliesApplied 10/10
  2. v29 patches 24 repliesSuperseded
  3. v17 patches 7 repliesSuperseded

Changes in v3 from the cover letter

- Fix documentation: add a new section for kfunc parameters (Eduard)
- Remove all occurances of KF_TRUSTED from comments, etc. (Eduard)
- Fix the netfilter kfuncs to drop dead NULL checks.
- Fix selftest for netfilter kfuncs to check for verification failures and remove the runtime failure that are not possible after this changes

Patches in v3 All 10 applied

#Subject and filesSizeRepliesApplied
1/10bpf: Make KF_TRUSTED_ARGS the default for all kfuncsDocumentation/bpf/kfuncs.rst fs/bpf_fs_kfuncs.c kernel/bpf/verifier.c+97 -1110 repliesApplied
2/10bpf: Remove redundant KF_TRUSTED_ARGS flag from all kfuncsfs/bpf_fs_kfuncs.c fs/verity/measure.c include/linux/bpf.h +16 more+63 -650 repliesApplied
3/10bpf: net: netfilter: drop dead NULL checksnet/netfilter/nf_conntrack_bpf.c+4 -100 repliesApplied
4/10bpf: xfrm: drop dead NULL check in bpf_xdp_get_xfrm_state()net/xfrm/xfrm_state_bpf.c+1 -10 repliesApplied
5/10HID: bpf: drop dead NULL checks in kfuncsdrivers/hid/bpf/hid_bpf_dispatch.c+1 -40 repliesApplied
6/10selftests: bpf: Update kfunc_param_nullable test for new error messagetools/testing/selftests/bpf/progs/test_kfunc_param_nullable.c+1 -10 repliesApplied
7/10selftests: bpf: Update failure message for rbtree_failtools/testing/selftests/bpf/progs/rbtree_fail.c+1 -10 repliesApplied
8/10selftests: bpf: fix test_kfunc_dynptr_paramtools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c+2 -30 repliesApplied
9/10selftests: bpf: fix cgroup_hierarchical_statstools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c+3 -30 repliesApplied
10/10selftests: bpf: Fix test_bpf_nf for trusted args becoming defaulttools/testing/selftests/bpf/prog_tests/bpf_nf.c tools/testing/selftests/bpf/progs/test_bpf_nf.c tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c+61 -80 repliesApplied

Reviews trailers per patch

7 of 10 patches have a Reviewed-by or Acked-by.

PatchEduard ZingermanEmil Tsalapatis
1/10A-b R-b
2/10A-b R-b
3/10
4/10A-b
5/10
6/10A-b R-b
7/10A-b R-b
8/10A-b R-b
9/10A-b R-b
10/10

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH bpf-next v3 00/10] bpf: Make KF_TRUSTED_ARGS default

v2: https://lore.kernel.org/all/[email protected]/
Changes in v2->v3:
- Fix documentation: add a new section for kfunc parameters (Eduard)
- Remove all occurances of KF_TRUSTED from comments, etc. (Eduard)
- Fix the netfilter kfuncs to drop dead NULL checks.
- Fix selftest for netfilter kfuncs to check for verification failures and remove the runtime failure that are not possible after this changes

v1: https://lore.kernel.org/all/[email protected]/
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

All bpf selftests are passing. The hid_bpf tests are also passing:
# PASSED: 20 / 20 tests passed.
# Totals: pass:20 fail:0 xfail:0 xpass:0 skip:0 error:0

bpf programs in drivers/hid/bpf/progs/ show no regression as shown by veristat:

Done. Processed 24 files, 62 programs. Skipped 0 files, 0 programs.

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)

This series introduces two intentional breaking changes to the BPF verifier's kfunc handling:

1. NULL pointer rejection timing: Kfuncs that previously accepted NULL pointers without KF_TRUSTED_ARGS will now reject NULL at verification time instead of returning runtime errors. This affects netfilter connection tracking functions (bpf_xdp_ct_lookup, bpf_skb_ct_lookup, bpf_xdp_ct_alloc, bpf_skb_ct_alloc), which now enforce their documented "Cannot be NULL" requirements at load time rather than returning -EINVAL at runtime.

2. Fentry/fexit program restrictions: BPF programs using fentry/fexit attachment points can no longer pass their callback arguments directly to kfuncs, as these arguments are not marked as trusted by default. Programs requiring trusted argument semantics should migrate to tp_btf (tracepoint with BTF) attachment points where arguments are guaranteed trusted by the verifier.

Both changes strengthen the verifier's safety guarantees by catching errors earlier in the development cycle and are accompanied by comprehensive test updates demonstrating the new expected behaviors.

Puranjay Mohan (10):
bpf: Make KF_TRUSTED_ARGS the default for all kfuncs
bpf: Remove redundant KF_TRUSTED_ARGS flag from all kfuncs
bpf: net: netfilter: drop dead NULL checks
bpf: xfrm: drop dead NULL check in bpf_xdp_get_xfrm_state()
HID: bpf: drop dead NULL checks in 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
selftests: bpf: Fix test_bpf_nf for trusted args becoming default

 Documentation/bpf/kfuncs.rst                  | 184 +++++++++---------
 drivers/hid/bpf/hid_bpf_dispatch.c            |   5 +-
 fs/bpf_fs_kfuncs.c                            |  23 +--
 fs/verity/measure.c                           |   2 +-
 include/linux/bpf.h                           |   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                         |  16 +-
 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              |  22 +--
 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 +-
 .../testing/selftests/bpf/prog_tests/bpf_nf.c |   5 +-
 .../bpf/progs/cgroup_hierarchical_stats.c     |   6 +-
 .../selftests/bpf/progs/cpumask_failure.c     |   2 +-
 .../testing/selftests/bpf/progs/rbtree_fail.c |   2 +-
 .../testing/selftests/bpf/progs/test_bpf_nf.c |   7 -
 .../selftests/bpf/progs/test_bpf_nf_fail.c    |  57 ++++++
 .../bpf/progs/test_kfunc_dynptr_param.c       |   5 +-
 .../bpf/progs/test_kfunc_param_nullable.c     |   2 +-
 .../selftests/bpf/test_kmods/bpf_testmod.c    |  20 +-
 29 files changed, 234 insertions(+), 207 deletions(-)

base-commit: c286e7e9d1f1f3d90ad11c37e896f582b02d19c4

Signature · 1 line
2.47.3