Skip to content

bpf: Fix refcount_acquire handling for borrowed kptrs

Ning Ding bpf linux-kselftest lkml 4 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v4 Latest4 patches 2 repliesIn review
  2. v34 patches 5 repliesSuperseded
  3. v22 patches 12 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v11 patch0 repliesSuperseded

Changes in v4 from the cover letter

- Rebase and retarget the series onto bpf-next.
- Change only the affected verifier test program sections from ?tc to sleepable ?syscall, as suggested by Kumar Kartikeya Dwivedi; leave the test logic unchanged.
- Drop Cc: [email protected].
- Rerun the full unfiltered test_progs suite.

Patches in v4 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4bpf: Keep refcount_acquire nullable for borrowed RCU kptrskernel/bpf/verifier.c+1 -10 replies
2/4selftests/bpf: Test refcount_acquire return nullabilitytools/testing/selftests/bpf/progs/refcounted_kptr.c tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c+108 -00 replies
3/4bpf: Reject untrusted allocated-object pointersinclude/linux/bpf_verifier.h kernel/bpf/verifier.c+17 -21 reply
4/4selftests/bpf: Test untrusted allocated-object pointerstools/testing/selftests/bpf/progs/refcounted_kptr.c tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c+127 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v4 yet.

Read the v4 thread

Cover letter [PATCH bpf-next v4 0/4] bpf: Fix refcount_acquire handling for borrowed kptrs

Fix two verifier bugs involving bpf_refcount_acquire() and map kptrs.

First, the verifier can mistake an RCU-loaded map kptr for an owned reference and assume bpf_refcount_acquire() cannot return NULL. The kfunc can return NULL after the last reference is dropped, so passing an unchecked result to bpf_obj_drop() can crash the kernel.

Second, type_is_ptr_alloc_obj() accepts PTR_UNTRUSTED pointers. This lets a pointer that is no longer protected by RCU reach bpf_refcount_acquire(). If the object was freed and its address reused, the verifier may acquire a reference through stale memory.

Patches 1 and 2 fix and test the return-value nullability. Patches 3 and 4 reject untrusted allocated-object pointers while keeping safe BPF_PROBE_MEM reads accepted.

The series is based on bpf-next commit 806c1a185215 ("Merge branch 'bpf-introduce-global-percpu-data'").

Testing used a fresh virtme-ng KVM boot with KASAN enabled:

full test_progs: 757/6685 passed, 7858 skipped, 0/0 failed
KASAN/Oops/panic markers: none

---
v4:
- Rebase and retarget the series onto bpf-next.
- Change only the affected verifier test program sections from ?tc to sleepable ?syscall, as suggested by Kumar Kartikeya Dwivedi; leave the test logic unchanged.
- Drop Cc: [email protected].
- Rerun the full unfiltered test_progs suite.

v3:
- Make type_is_ptr_alloc_obj() reject PTR_UNTRUSTED, as suggested by Kumar Kartikeya Dwivedi.
- Add a separate helper for the BPF_PROBE_MEM read path.
- Add read, refcount-acquire, and direct-write verifier tests.
- Add Cc: [email protected] to both fixes, as requested by Greg Kroah-Hartman.
- Split each fix from its selftests.

v3: https://lore.kernel.org/r/[email protected]
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]

Ning Ding (4):
bpf: Keep refcount_acquire nullable for borrowed RCU kptrs selftests/bpf: Test refcount_acquire return nullability
bpf: Reject untrusted allocated-object pointers selftests/bpf: Test untrusted allocated-object pointers

 include/linux/bpf_verifier.h                  |  11 +-
 kernel/bpf/verifier.c                         |  10 +-
 .../selftests/bpf/progs/refcounted_kptr.c     | 161 ++++++++++++++++++
 .../bpf/progs/refcounted_kptr_fail.c          |  74 ++++++++
 4 files changed, 253 insertions(+), 3 deletions(-)

base-commit: 806c1a185215382fab5a7fafc74e30070b2fb043

Signature · 1 line
2.43.0