]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
bpf: return VMA snapshot from task_vma iterator
authorPuranjay Mohan <puranjay@kernel.org>
Wed, 8 Apr 2026 15:45:37 +0000 (08:45 -0700)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 10 Apr 2026 19:05:16 +0000 (12:05 -0700)
commit4cbee026db54cad39c39db4d356100cb133412b3
treeb56b6ad62966a92ed2976ab04d50f345066190b6
parentbee9ef4a40a277bf401be43d39ba7f7f063cf39c
bpf: return VMA snapshot from task_vma iterator

Holding the per-VMA lock across the BPF program body creates a lock
ordering problem when helpers acquire locks that depend on mmap_lock:

  vm_lock -> i_rwsem -> mmap_lock -> vm_lock

Snapshot the VMA under the per-VMA lock in _next() via memcpy(), then
drop the lock before returning. The BPF program accesses only the
snapshot.

The verifier only trusts vm_mm and vm_file pointers (see
BTF_TYPE_SAFE_TRUSTED_OR_NULL in verifier.c). vm_file is reference-
counted with get_file() under the lock and released via fput() on the
next iteration or in _destroy(). vm_mm is already correct because
lock_vma_under_rcu() verifies vma->vm_mm == mm. All other pointers
are left as-is by memcpy() since the verifier treats them as untrusted.

Fixes: 4ac454682158 ("bpf: Introduce task_vma open-coded iterator kfuncs")
Signed-off-by: Puranjay Mohan <puranjay@kernel.org>
Acked-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Mykyta Yatsenko <yatsenko@meta.com>
Link: https://lore.kernel.org/r/20260408154539.3832150-4-puranjay@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
kernel/bpf/task_iter.c