mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bot+bpf-ci@kernel.org
To: mason@kernel.org,brauner@kernel.org,oleg@redhat.com,ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,bpf@vger.kernel.org,linux-kernel@vger.kernel.org
Cc: akpm@linux-foundation.org,peterz@infradead.org,mingo@redhat.com,kees@kernel.org,tj@kernel.org,eddyz87@gmail.com,memxor@gmail.com,yonghong.song@linux.dev,martin.lau@linux.dev,song@kernel.org,jolsa@kernel.org,emil@etsalapatis.com,ihor.solodrai@linux.dev,sched-ext@lists.linux.dev,mason@kernel.org,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev
Subject: Re: [PATCH 3/3] bpf: Don't start a thread walk at a released task
Date: Thu,  3 Dec 2026 20:42:21 +0000 (UTC)	[thread overview]
Message-ID: <ec767a8d63f570ef093017e06c7545e942f47b225adba3f6ad7bf27d3962d9e4@mail.kernel.org> (raw)
In-Reply-To: <20261203200612.26376-4-mason@kernel.org>

[-- Attachment #1: Type: text/plain, Size: 3155 bytes --]

> bpf: Don't start a thread walk at a released task

TL;DR: bpf_iter_task_new()'s pid_alive() check may miss a task still inside
copy_process(). A BPF program could then walk a stale thread_node into a freed
sibling thread, a possible use-after-free.

> diff --git a/kernel/bpf/task_iter.c b/kernel/bpf/task_iter.c
> --- a/kernel/bpf/task_iter.c
> +++ b/kernel/bpf/task_iter.c
> @@ -1161,6 +1161,9 @@ __bpf_kfunc int bpf_iter_task_new(struct bpf_iter_task *it,
>  	case BPF_TASK_ITER_PROC_THREADS:
>  		if (!task__nullable)
>  			return -EINVAL;
> +		/* ->thread_node of a released task is stale, see next_tid() */
> +		if (!pid_alive(task__nullable))
> +			return -ESRCH;
>  		break;

This isn't a bug in the patch itself, but does the pid_alive() check leave
a similar case uncovered for a task that is still inside copy_process()?

dup_task_struct() copies the parent with arch_dup_task_struct(), and
nothing in copy_process() touches ->thread_pid until:

    init_task_pid(p, PIDTYPE_PID, pid)

so pid_alive(p) is true for the whole fork.  In that window
p->thread_node is a byte copy of current->thread_node, and its ->next
points at &X->thread_node for a sibling thread X.  That pointer holds no
reference and no RCU section covers it, so X can exit, go through
release_task() and delayed_put_task_struct(), and be freed while
copy_process() sleeps in copy_creds(), alloc_pid(), cgroup_can_fork() and
so on.

BPF programs get a trusted pointer to p in this window, from the LSM
task_alloc hook (security_task_alloc(), bpf_lsm_task_alloc is not in
untrusted_lsm_hooks), from sched_ext ops.init_task() via
sched_cgroup_fork()->scx_fork(), and from ops.exit_task() via
scx_cancel_fork().  bpf_iter_task_new() is in common_kfunc_set, so these
programs can call it:

    copy_process()                       sibling X of current
      dup_task_struct()
        p->thread_node.next = &X->thread_node
                                         do_exit() -> release_task(X)
                                         <grace period>, X freed
      security_task_alloc(p) / scx_fork(p)
        bpf_iter_task_new(&it, p, BPF_TASK_ITER_PROC_THREADS)
          pid_alive(p) == 1              thread_pid copied from current
        bpf_iter_task_next()             returns p
          kit->pos = __next_thread(p)    reads stale p->thread_node.next == X
        bpf_iter_task_next()             returns freed X to the program
          __next_thread(X)               reads X->signal

next_tid() in fs/proc/base.c can rely on pid_alive() because procfs only
reaches hashed tasks, but that doesn't hold for the start tasks BPF can
pass here.

Is the PROC_THREADS walk intended to be safe for a task that is still being
forked?  If so, could the start-task check also reject unlinked tasks, for
example a task whose ->thread_pid is not yet attached, or one with
rcu_users == 0 which bpf_task_acquire() already treats as unusable?


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47195022171

  reply	other threads:[~2026-12-03 20:42 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 20:06 [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Chris Mason
2026-12-03 20:06 ` [PATCH 1/3] fork: make the creator the parent of an unlinked task Chris Mason
2026-12-03 20:06 ` [PATCH 2/3] exit: don't leave stale parent and leader pointers Chris Mason
2026-12-03 20:55   ` bot+bpf-ci
2026-12-03 20:06 ` [PATCH 3/3] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-03 20:42   ` bot+bpf-ci [this message]
2026-12-04  2:36 ` [PATCH 0/3] fork, exit: fix BPF use-after-frees on task pointers Alexei Starovoitov

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=ec767a8d63f570ef093017e06c7545e942f47b225adba3f6ad7bf27d3962d9e4@mail.kernel.org \
    --to=bot+bpf-ci@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=brauner@kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=ihor.solodrai@linux.dev \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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

all inboxes | Powered by JetHome®