BPF List
 help / color / mirror / Atom feed
From: Ning Ding <dingning04@gmail.com>
To: bpf@vger.kernel.org
Cc: memxor@gmail.com, greg@kroah.com, Ning Ding <dingning04@gmail.com>
Subject: [PATCH bpf v3 0/4] bpf: Fix refcount_acquire handling for borrowed kptrs
Date: Mon,  3 Aug 2026 04:22:07 -0700	[thread overview]
Message-ID: <20260803112218.3361213-1-dingning04@gmail.com> (raw)

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 commit e5fd3f514e27
("bpf: tcp: Fix use-after-free in bpf_iter_tcp_established_batch()").

Testing used fresh virtme-ng KVM boots with KASAN enabled:

  pre-fix graph write:       accepted, as expected
  post-fix focused cases:    8/8 passed
  full refcount groups:      47/47 passed
  related list/rbtree groups: 167/167 passed
  skipped or failed cases:   none
  KASAN/Oops/panic markers:  none

---
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: stable@vger.kernel.org to both fixes, as requested by Greg
    Kroah-Hartman.
  - Split each fix from its selftests.

v2: https://lore.kernel.org/r/20260726235030.1152542-1-dingning04@gmail.com
v1: https://lore.kernel.org/r/20260726015330.705259-1-dingning04@gmail.com

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: e5fd3f514e27db1f05fbd72ba615d74941e23c51
-- 
2.43.0

             reply	other threads:[~2026-08-03 11:25 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-03 11:22 Ning Ding [this message]
2026-08-03 11:22 ` [PATCH bpf v3 1/4] bpf: Keep refcount_acquire nullable for borrowed RCU kptrs Ning Ding
2026-08-03 12:53   ` bot+bpf-ci
2026-08-03 13:45     ` Amery Hung
2026-08-03 11:22 ` [PATCH bpf v3 2/4] selftests/bpf: Test refcount_acquire return nullability Ning Ding
2026-08-03 13:59   ` Amery Hung
2026-08-03 22:48     ` Ning Ding
2026-08-03 11:22 ` [PATCH bpf v3 3/4] bpf: Reject untrusted allocated-object pointers Ning Ding
2026-08-03 11:22 ` [PATCH bpf v3 4/4] selftests/bpf: Test " Ning Ding

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260803112218.3361213-1-dingning04@gmail.com \
    --to=dingning04@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=greg@kroah.com \
    --cc=memxor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox