linux/Documentation/bpf
Xu Yunxiang d09c2aa310
bpf: Track borrowed iterator results by default
An open-coded iterator can return a struct pointer borrowed from its
current element. The verifier has not tied such results to iterator
state since the task_vma iterator was introduced, so a saved result
can still be used after destroy. Commit bee9ef4a40 ("bpf: switch
task_vma iterator from mmap_lock to per-VMA locks") also made next
release the protection of the prior element. A later snapshot
conversion made vm_file explicitly borrowed from that element.

Associate non-RCU, non-acquired struct results with the iterator
reference by default. Invalidate dependents before next forks its
NULL and non-NULL paths, while retaining the iterator itself. Let
destroy propagate child-reference errors. Add KF_ITER_RET_INDEPENDENT
for results that outlive the current element, preserving existing
RCU and acquired-reference rules.

Trusted pointer field loads also need to preserve the source lifetime.
Loading a trusted field does not acquire a reference to the pointed-to
object. Set the field pointer's parent_id to the source's reference ID
if the source is referenced, or inherit its parent_id if it is borrowed.
This is a generic rule, including accesses unrelated to iterators. For
example, the following access is now rejected:

  file = bpf_get_task_exe_file(task);
  if (!file)
          return 0;
  inode = file->f_inode;
  bpf_put_file(file);
  ino = inode->i_ino;

The inode pointer borrows the file's lifetime. Releasing the acquired
file reference removes that lifetime guarantee, so the verifier must
invalidate the derived inode pointer as well. This does not imply that
bpf_put_file() immediately frees the file or inode. Reading the inode
while the file reference is held remains valid. Fields with independent
RCU protection retain their existing lifetime rules.

For an iterator result, the same propagation keeps trusted fields such
as vm_file and f_inode tied to the current element. Propagate the backing
object's lifetime through dynptr construction too, so its descendants
remain in the invalidation graph. Share the invalidation traversal
between next and release, so advancing the iterator preserves its own
reference.

Fixes: 4ac4546821 ("bpf: Introduce task_vma open-coded iterator kfuncs")
Suggested-by: Amery Hung <ameryhung@gmail.com>
Signed-off-by: Xu Yunxiang <xyx2021@mail.ustc.edu.cn>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Reviewed-by: Amery Hung <ameryhung@gmail.com>
Link: https://patch.msgid.link/20261024100647.2579244-2-xyx2021@mail.ustc.edu.cn
2026-10-25 01:20:28 +00:00
..
libbpf docs/bpf: add missing fsession attach type to docs 2026-04-12 12:42:38 -07:00
standardization bpf, doc: Improve wording of docs 2025-06-11 12:30:41 -07:00
bpf_design_QA.rst bpf, arm64: Allow programs 2 KiB of stack 2026-09-24 18:04:46 +00:00
bpf_devel_QA.rst selftests/bpf: Remove the old test_bpf module 2026-10-15 02:40:13 +02:00
bpf_iterators.rst bpf: Track borrowed iterator results by default 2026-10-25 01:20:28 +00:00
bpf_licensing.rst
bpf_prog_run.rst bpf: Update BPF_PROG_RUN documentation 2025-12-31 13:45:26 -08:00
btf.rst Documentation/bpf: Describe new location-related BTF kinds 2026-09-24 15:59:46 +00:00
clang-notes.rst bpf, docs: Document callx instruction 2026-09-24 05:39:50 +00:00
classic_vs_extended.rst
cpumasks.rst
drgn.rst docs/bpf: add missing fsession attach type to docs 2026-04-12 12:42:38 -07:00
faq.rst
fs_kfuncs.rst
graph_ds_impl.rst
helpers.rst
index.rst Documentation/bpf: Add BPF signing and enforcement doc 2026-07-08 20:05:16 +02:00
kfuncs.rst Merge git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf 7.3-rc4 2026-09-24 16:15:14 +00:00
linux-notes.rst bpf, docs: Document callx instruction 2026-09-24 05:39:50 +00:00
llvm_reloc.rst
map_array.rst docs: bpf: map_array: Specify BPF_MAP_TYPE_PERCPU_ARRAY value size limit 2025-11-25 14:32:00 -08:00
map_bloom_filter.rst
map_cgroup_storage.rst
map_cgrp_storage.rst
map_cpumap.rst
map_devmap.rst
map_hash.rst bpf: Adjust free target to avoid global starvation of LRU map 2025-06-18 18:50:14 -07:00
map_lpm_trie.rst
map_lru_hash_update.dot Documentation/bpf: Refresh map_lru_hash_update.dot for rqspinlock 2026-06-07 18:46:13 -07:00
map_of_maps.rst
map_queue_stack.rst
map_sk_storage.rst
map_sockmap.rst
map_xskmap.rst
maps.rst
other.rst
prog_cgroup_sockopt.rst
prog_cgroup_sysctl.rst
prog_flow_dissector.rst
prog_lsm.rst
prog_sk_lookup.rst
programs.rst
redirect.rst
ringbuf.rst
s390.rst
signing.rst Documentation/bpf: Document the bpf keyring and improve examples 2026-08-29 18:17:51 -07:00
syscall_api.rst
test_debug.rst
verifier.rst bpf: disable and remove registers chain based liveness 2025-09-19 09:27:23 -07:00