From: Henry Martin <bsdhenrymartin@gmail.com>
To: kees@kernel.org, paul@paul-moore.com, jmorris@namei.org,
serge@hallyn.com, john.johansen@canonical.com
Cc: linux-security-module@vger.kernel.org,
Henry Martin <bsdhenrymartin@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH] yama: fix use-after-free read of a dead PR_SET_PTRACER tracer
Date: Tue, 24 Nov 2026 00:53:57 +0800 [thread overview]
Message-ID: <20261123165357.4092273-1-bsdhenrymartin@gmail.com> (raw)
A PR_SET_PTRACER relation stores a bare pointer to the tracer's
task_struct. When the tracer dies, its task_struct is freed via
call_rcu(), and the relation is only marked invalid from
security_task_free(), i.e. from the same RCU callback that frees the
task. A reader that enters rcu_read_lock() after the callback was
queued is not protected against it: ptracer_exception_found() can read
relation->invalid while it is still false and then dereference the
already freed tracer in task_is_descendant().
This is a slab-use-after-free read during ptrace access checks, e.g.
via process_vm_readv(), on mainline v7.4-rc4 (KASAN build):
BUG: KASAN: slab-use-after-free in task_is_descendant+0x1a3/0x1b0
Read of size 4 at addr ffff88810a46c598 by task poc/167
CPU: 6 UID: 1000 PID: 167 Comm: poc
Call Trace:
<TASK>
kasan_report+0xce/0x100
task_is_descendant+0x1a3/0x1b0
yama_ptrace_access_check+0x2ea/0x410
security_ptrace_access_check+0x34/0x50
ptrace_may_access+0x26/0x40
mm_access+0x139/0x200
process_vm_rw_core.constprop.0+0x1ab/0x7c0
process_vm_rw+0x188/0x230
__x64_sys_process_vm_readv+0xdd/0x1b0
do_syscall_64+0xea/0x4f0
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Keep the tracer's struct pid alive for the lifetime of the relation and
resolve it with pid_task() under RCU at access-check time. Pinning the
task_struct itself would not work: the relation is only invalidated from
yama_task_free(), which runs when the tracer's task_struct is finally
released, so a task reference would keep the relation, and therefore the
task, alive forever.
This issue was discovered by Tencent CodeBuddy Security.
Cc: stable@vger.kernel.org
Fixes: 93b69d437eff ("Yama: add RCU to drop read locking")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
security/yama/yama_lsm.c | 42 ++++++++++++++++++++++++++++++++++++----
1 file changed, 38 insertions(+), 4 deletions(-)
diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
index cef3776cf3b2d..fd394c24e4357 100644
--- a/security/yama/yama_lsm.c
+++ b/security/yama/yama_lsm.c
@@ -29,7 +29,16 @@ static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
/* describe a ptrace relationship for potential exception */
struct ptrace_relation {
+ /*
+ * The tracer task cannot be pinned here: the relation is only
+ * invalidated from yama_task_free(), which runs when the tracer's
+ * task_struct is finally released. Holding a task reference would
+ * stop that from ever happening. Keep the tracer's struct pid
+ * alive instead, which is enough to look the task up safely under
+ * RCU, and use the plain pointer for identity comparisons only.
+ */
struct task_struct *tracer;
+ struct pid *tracer_pid;
struct task_struct *tracee;
bool invalid;
struct list_head node;
@@ -39,6 +48,19 @@ struct ptrace_relation {
static LIST_HEAD(ptracer_relations);
static DEFINE_SPINLOCK(ptracer_relations_lock);
+/*
+ * Free a relation after an RCU grace period, dropping the reference
+ * the relation holds on the tracer's pid.
+ */
+static void yama_relation_free(struct rcu_head *rcu)
+{
+ struct ptrace_relation *relation =
+ container_of(rcu, struct ptrace_relation, rcu);
+
+ put_pid(relation->tracer_pid);
+ kfree(relation);
+}
+
static void yama_relation_cleanup(struct work_struct *work);
static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);
@@ -121,7 +143,7 @@ static void yama_relation_cleanup(struct work_struct *work)
list_for_each_entry_rcu(relation, &ptracer_relations, node) {
if (relation->invalid) {
list_del_rcu(&relation->node);
- kfree_rcu(relation, rcu);
+ call_rcu(&relation->rcu, yama_relation_free);
}
}
rcu_read_unlock();
@@ -149,6 +171,7 @@ static int yama_ptracer_add(struct task_struct *tracer,
added->tracee = tracee;
added->tracer = tracer;
+ added->tracer_pid = tracer ? get_task_pid(tracer, PIDTYPE_PID) : NULL;
added->invalid = false;
spin_lock(&ptracer_relations_lock);
@@ -158,7 +181,7 @@ static int yama_ptracer_add(struct task_struct *tracer,
continue;
if (relation->tracee == tracee) {
list_replace_rcu(&relation->node, &added->node);
- kfree_rcu(relation, rcu);
+ call_rcu(&relation->rcu, yama_relation_free);
goto out;
}
}
@@ -323,11 +346,22 @@ static int ptracer_exception_found(struct task_struct *tracer,
list_for_each_entry_rcu(relation, &ptracer_relations, node) {
if (relation->invalid)
continue;
- if (relation->tracee == tracee) {
- parent = relation->tracer;
+ if (relation->tracee != tracee)
+ continue;
+
+ if (!relation->tracer) {
+ /* PR_SET_PTRACER_ANY: any tracer is allowed. */
+ parent = NULL;
+ found = true;
+ break;
+ }
+
+ parent = pid_task(relation->tracer_pid, PIDTYPE_PID);
+ if (parent) {
found = true;
break;
}
+ /* The tracer is gone; this relation grants no exception. */
}
if (found && (parent == NULL || task_is_descendant(parent, tracer)))
--
2.53.0
next reply other threads:[~2026-11-23 16:54 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 16:53 Henry Martin [this message]
2026-11-23 17:01 ` [PATCH] yama: fix use-after-free read of a dead PR_SET_PTRACER tracer sashiko-bot
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=20261123165357.4092273-1-bsdhenrymartin@gmail.com \
--to=bsdhenrymartin@gmail.com \
--cc=jmorris@namei.org \
--cc=john.johansen@canonical.com \
--cc=kees@kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=paul@paul-moore.com \
--cc=serge@hallyn.com \
--cc=stable@vger.kernel.org \
/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