[PATCH bpf 1/2] bpf: make task_vma iterator stay drained after NULL

From: Kang Chen

Date: Fri Nov 13 2026 - 03:43:41 EST


bpf_iter_task_vma_find_next() returns NULL when there is no VMA at or
after data->next_addr, but leaves next_addr unchanged. Since the
per-VMA lock conversion, the iterator does not hold mmap_lock between
next() calls, so a VMA mapped after exhaustion can be returned by a
later next() call.

The verifier relies on the iterator contract that once next() returns
NULL it keeps returning NULL: process_iter_next_call() marks the
iterator DRAINED and treats later next() results as the constant zero.
A restarted task_vma iteration therefore hands the BPF program a kernel
pointer while the verifier believes R0 == 0.

Add a done flag to the internal iterator data and set it when
bpf_iter_task_vma_find_next() returns NULL. Later next() calls return
NULL without walking the maple tree again.

Fixes: bee9ef4a40a2 ("bpf: switch task_vma iterator from mmap_lock to per-VMA locks")
Signed-off-by: Kang Chen <k.chen@xxxxxxxxxxxxxxxx>
---
kernel/bpf/task_iter.c | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
index 2ee7b3b3c10b..720ff7545072 100644
--- a/kernel/bpf/task_iter.c
+++ b/kernel/bpf/task_iter.c
@@ -854,6 +854,7 @@ struct bpf_iter_task_vma_kern_data {
struct mm_struct *mm;
struct vm_area_struct snapshot;
u64 next_addr;
+ bool done;
};

struct bpf_iter_task_vma {
@@ -931,6 +932,7 @@ __bpf_kfunc int bpf_iter_task_vma_new(struct bpf_iter_task_vma *it,

kit->data->snapshot.vm_file = NULL;
kit->data->next_addr = addr;
+ kit->data->done = false;
return 0;

err_cleanup_iter:
@@ -1007,13 +1009,19 @@ __bpf_kfunc struct vm_area_struct *bpf_iter_task_vma_next(struct bpf_iter_task_v
if (!kit->data) /* bpf_iter_task_vma_new failed */
return NULL;

+ /* Once NULL is returned, next() has to keep returning NULL. */
+ if (kit->data->done)
+ return NULL;
+
snap = &kit->data->snapshot;

bpf_iter_task_vma_snapshot_reset(snap);

vma = bpf_iter_task_vma_find_next(kit->data);
- if (!vma)
+ if (!vma) {
+ kit->data->done = true;
return NULL;
+ }

memcpy(snap, vma, sizeof(*snap));

--
2.34.1