All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Chris Mason" <mason@kernel.org>,
	brauner@kernel.org, oleg@redhat.com, ast@kernel.org,
	daniel@iogearbox.net, andrii@kernel.org, bpf@vger.kernel.org
Cc: akpm@linux-foundation.org, peterz@infradead.org,
	mingo@redhat.com, kees@kernel.org, viro@zeniv.linux.org.uk,
	jack@suse.cz, tj@kernel.org, hannes@cmpxchg.org,
	mkoutny@suse.com, bsingharora@gmail.com, 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,
	linux-mm@kvack.org
Subject: Re: [PATCH v2 3/8] exit: reset a released task's parent and leader
Date: Sat, 05 Dec 2026 13:36:07 +0000	[thread overview]
Message-ID: <sashiko-outbox-251583@kernel.org> (raw)
In-Reply-To: <20261204233428.14861-4-mason@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] exit: reset_parent_and_leader() breaks lockless RCU real_parent
  walks
--

commit 4d9f47097374975327e6ccb2ac8993c54f45179d
Author: Chris Mason <mason@kernel.org>

exit: reset a released task's parent and leader

This commit resets real_parent and parent to init_task and group_leader
to the task itself in release_task() so BPF programs holding a task
reference cannot follow stale pointers after the parent or group leader
is freed.

> diff --git a/include/linux/sched/task.h b/include/linux/sched/task.h
> index 90ed5bc3c7afe..b30a66748af2c 100644
> --- a/include/linux/sched/task.h
> +++ b/include/linux/sched/task.h
> @@ -80,6 +80,17 @@ extern void fork_init(void);
>  
>  extern void release_task(struct task_struct * p);
>  
> +/*
> + * Nobody updates these pointers if @p isn't in the process tree, but BPF
> + * programs can still follow them. So don't leave any that can go stale.
> + */
> +static inline void reset_parent_and_leader(struct task_struct *p)
> +{
> +	RCU_INIT_POINTER(p->real_parent, &init_task);
> +	RCU_INIT_POINTER(p->parent, &init_task);
> +	WRITE_ONCE(p->group_leader, p);
> +}
> +
>  extern int copy_thread(struct task_struct *, const struct kernel_clone_args *);
>  
>  extern void flush_thread(void);
> diff --git a/kernel/exit.c b/kernel/exit.c
> index d5e7bc2af804a..8a5985821e17d 100644
> --- a/kernel/exit.c
> +++ b/kernel/exit.c
> @@ -288,6 +288,7 @@ void release_task(struct task_struct *p)
>  			leader->exit_state = EXIT_DEAD;
>  	}
>  
> +	reset_parent_and_leader(p);
>  	write_unlock_irq(&tasklist_lock);
>  	proc_flush_pid(thread_pid);
>  	put_pid(thread_pid);

[Severity: High]
Can resetting p->real_parent to &init_task in release_task() before an
RCU grace period elapses break concurrent lockless RCU ancestor walks?

CPU0 (task M exiting)             CPU1 (RCU walk from child C)
                                  rcu_read_lock()
                                  t = rcu_dereference(C->real_parent)
exit_notify()
  forget_original_parent(M)
  release_task(M)
    reset_parent_and_leader(M)
      M->real_parent = &init_task
                                  t = rcu_dereference(M->real_parent)
                                    <-- jumps to init_task, skips A

When M is autoreaped (for example with SIG_IGN on SIGCHLD), exit_notify()
reparents C to ancestor A via forget_original_parent() and immediately
calls release_task(M).  A concurrent RCU reader that already loaded M
then jumps to init_task and skips A and all higher ancestors.

An unprivileged process inside a monitored subtree can repeatedly trigger
this fork/exit race to bypass a BPF LSM ancestor-deny policy.  In-tree,
task_is_descendant() in security/yama/yama_lsm.c terminates early at
init_task and spuriously fails PTRACE_ATTACH with -EPERM.

The commit message also misses nfs_access_login_time() in fs/nfs/dir.c,
which walks real_parent locklessly under RCU:

fs/nfs/dir.c:nfs_access_login_time() {
	for (;;) {
		parent = rcu_dereference(task->real_parent);
		pcred = __task_cred(parent);
		if (parent == task || cred_fscmp(pcred, cred) != 0)
			break;
		task = parent;
	}
	ret = task->start_time;

Jumping to init_task stops the walk early for non-root credentials
(spurious cache invalidation).  For root credentials under a non-root
ancestor, it matches init_cred and returns init_task.start_time (0),
allowing stale pre-session cached access entries to be reused.

In-tree walkers could detect stepping onto init_task from a !pid_alive()
task and restart from the live starting task, whose real_parent was
already reparented before release_task() ran.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261204233428.14861-1-mason@kernel.org?part=3

  reply	other threads:[~2026-12-05 13:36 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 13:11 [PATCH v2 0/8] fork, exit: fix use-after-frees on task pointers Chris Mason
2026-12-05 13:11 ` [PATCH v2 1/8] tsacct: don't dereference a stale group_leader Chris Mason
2026-12-05 13:12 ` [PATCH v2 2/8] exec, cgroup, prlimit: prepare for group_leader resets Chris Mason
2026-12-05 15:35   ` Oleg Nesterov
2026-12-05 13:12 ` [PATCH v2 3/8] exit: reset a released task's parent and leader Chris Mason
2026-12-05 13:36   ` sashiko-bot [this message]
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 13:12 ` [PATCH v2 4/8] fork: don't leave stale parent pointers in a new task Chris Mason
2026-12-05 13:12 ` [PATCH v2 5/8] bpf: Don't start a thread walk at a released task Chris Mason
2026-12-05 13:12 ` [PATCH v2 6/8] drm/amdkfd: fix crashes if a debugged process exits Chris Mason
2026-12-05 13:40   ` sashiko-bot
2026-12-05 13:13 ` [RFC PATCH v2 7/8] signal: add task_group_leader() Chris Mason
2026-12-05 15:29   ` Oleg Nesterov
2026-12-05 13:13 ` [RFC PATCH v2 8/8] signal: move group_leader into signal_struct Chris Mason
2026-12-05 13:54   ` bot+bpf-ci
2026-12-05 15:24   ` Oleg Nesterov
2026-12-05 15:59 ` [PATCH] change next_thread() to use list_next_entry_circular() Oleg Nesterov
2026-12-05 16:11   ` sashiko-bot
2026-12-05 16:19   ` Oleg Nesterov
2026-12-05 17:46   ` Chris Mason
2026-12-05 18:17     ` Oleg Nesterov

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=sashiko-outbox-251583@kernel.org \
    --to=sashiko-bot@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=bsingharora@gmail.com \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=emil@etsalapatis.com \
    --cc=hannes@cmpxchg.org \
    --cc=ihor.solodrai@linux.dev \
    --cc=jack@suse.cz \
    --cc=jolsa@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=martin.lau@linux.dev \
    --cc=mason@kernel.org \
    --cc=memxor@gmail.com \
    --cc=mingo@redhat.com \
    --cc=mkoutny@suse.com \
    --cc=oleg@redhat.com \
    --cc=peterz@infradead.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=sched-ext@lists.linux.dev \
    --cc=song@kernel.org \
    --cc=tj@kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.