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 2/3] exit: don't leave stale parent and leader pointers
Date: Thu,  3 Dec 2026 20:55:19 +0000 (UTC)	[thread overview]
Message-ID: <55fd59dbd3965f7d12b18d7fcc2011a5a87046a7310b1c08bd3ca1cbcd9b1895@mail.kernel.org> (raw)
In-Reply-To: <20261203200612.26376-3-mason@kernel.org>

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

> exit: don't leave stale parent and leader pointers

TL;DR: Released threads now pin their group leader, which may create a
reference cycle with BPF task-storage kptrs, leaking both task_structs until
the map is destroyed. The commit message omits this lifetime change.

> diff --git a/kernel/exit.c b/kernel/exit.c
> --- a/kernel/exit.c
> +++ b/kernel/exit.c

[ ... ]

> @@ -262,6 +263,17 @@ void release_task(struct task_struct *p)
>  	 */
>  	thread_pid = get_pid(task_pid(p));
>
> +	/*
> +	 * A released thread pins its leader, which lockless next_thread()
> +	 * needs. Until @p is released its leader can neither change nor go
> +	 * away. notify_count < 0 means mt-exec: @p is the old leader, and
> +	 * must not pin the new one, or repeated execs could build a chain.
> +	 */
> +	leader = p->group_leader;
> +	pin_leader = leader != p && READ_ONCE(p->signal->notify_count) >= 0;
> +	if (pin_leader)
> +		get_task_struct(leader);
> +

Can this pin create a reference cycle between a thread and its leader?

Previously the reference to the leader L held on behalf of a released
thread T was not counted, so L's task_struct was freed once L itself was
reaped.  Now T holds a usage reference on L until T's own
__put_task_struct():

kernel/fork.c:__put_task_struct()
	/* release_task() has pinned the leader of a thread */
	if (tsk->group_leader != tsk)
		put_task_struct(tsk->group_leader);

Anything whose release of T is tied to L's final free now forms a cycle.
BPF task-local storage looks like one such case, since
bpf_task_storage_free() is only called from free_task(), and
map_check_btf() allows BPF_KPTR_REF fields in BPF_MAP_TYPE_TASK_STORAGE
values.  A program that stores a referenced kptr to thread T in L's task
storage:

	v = bpf_task_storage_get(&map, L, NULL, BPF_LOCAL_STORAGE_GET_F_CREATE);
	old = bpf_kptr_xchg(&v->t, bpf_task_from_pid(tid_of_T));

used to be fine, because L's free released the storage and dropped T.  With
this patch:

T exits -> exit_notify() -> release_task(T) -> get_task_struct(L)
L exits and is reaped -> put_task_struct_rcu_user(L) -> delayed_put_task_struct(L)
  -> put_task_struct(L): usage stays >= 1 (T's pin), L is not freed
free_task(L) never runs, so L's storage and its kptr to T are never released
T's usage stays >= 1, so __put_task_struct(T) never drops the pin on L

Both task_structs then stay allocated until the map itself is destroyed,
along with what __put_task_struct() would have released: creds,
signal_struct, css_set and LSM blobs.  A cleanup hook on lsm/task_free for L
also never runs.  A long-running BPF LSM or sched_ext program could leak
this way once per process.

The commit message also does not mention this new lifetime dependency:
holding any thread's task_struct now keeps its leader's task_struct alive.
Is that intended?


---
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:55 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 [this message]
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
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=55fd59dbd3965f7d12b18d7fcc2011a5a87046a7310b1c08bd3ca1cbcd9b1895@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®