* [PATCH] yama: fix use-after-free read of a dead PR_SET_PTRACER tracer
@ 2026-11-23 16:53 Henry Martin
2026-11-23 17:01 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Henry Martin @ 2026-11-23 16:53 UTC (permalink / raw)
To: kees, paul, jmorris, serge, john.johansen
Cc: linux-security-module, Henry Martin, stable
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] yama: fix use-after-free read of a dead PR_SET_PTRACER tracer
2026-11-23 16:53 [PATCH] yama: fix use-after-free read of a dead PR_SET_PTRACER tracer Henry Martin
@ 2026-11-23 17:01 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-23 17:01 UTC (permalink / raw)
Cc: linux-security-module
> 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
> [ ... ]
>
> 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>
Sashiko has reviewed this patch and found no issues.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261123165357.4092273-1-bsdhenrymartin@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-23 17:01 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 16:53 [PATCH] yama: fix use-after-free read of a dead PR_SET_PTRACER tracer Henry Martin
2026-11-23 17:01 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox