mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] yama: hold the PR_SET_PTRACER tracer by its struct pid
@ 2026-11-19  2:59 JunYoung Lee
  0 siblings, 0 replies; only message in thread
From: JunYoung Lee @ 2026-11-19  2:59 UTC (permalink / raw)
  To: Kees Cook
  Cc: Paul Moore, James Morris, Serge E . Hallyn,
	linux-security-module, linux-kernel

Hi Kees,

An unprivileged user can make Yama read a freed task_struct with the
default ptrace_scope=1. relation->tracer is stored without a reference,
and yama_task_free() marks the relation invalid only just before the
task is freed, so a concurrent ptracer_exception_found() can still
dereference it in task_is_descendant(). The KASAN report, the details
and the fix are in the patch below.

This is the problem Oleg Nesterov described on LKML on 2018-10-29
("yama: unsafe usage of ptrace_relation->tracer"). It was never fixed.

Affected (reproduced under KASAN as uid 65534):
- mainline 4405c18c95d2 (v7.4-rc3-230)
- stable 6.12.118

Conditions: CONFIG_SECURITY_YAMA, ptrace_scope=1 (default), no
capabilities or namespaces. The window is short; the reproducer hit it
7 times in 120 s on a 2-CPU VM.

Observed impact: a 4-byte read of a freed task_struct. The value only
affects Yama's allow/deny decision. No write was observed.

A C reproducer is available privately on request.

Testing of the patch on mainline with KASAN: the reproducer gives no
report (2.7M access checks). PR_SET_PTRACER behaviour is unchanged
(live and reaped tracer, ANY, clear, invalid pid, parent -> child: 13/13
checks). checkpatch --strict and W=1 are clean. A 6.12.y backport
(context changes only) is available.

Found with AI-assisted code analysis. All results come from test runs.

Thanks,
JunYoung Lee

-- >8 --
PR_SET_PTRACER records the allowed tracer as a raw task_struct pointer
in the relation list and takes no reference on it. The only thing that
retires that pointer is yama_task_free(), which runs from
__put_task_struct() just before free_task() and only sets
relation->invalid. Nothing separates that flag from the free with a
grace period.

ptracer_exception_found() walks the list under rcu_read_lock() alone.
If it has already passed the relation->invalid test when the tracer's
last reference is dropped (for example by the RCU callback queued by
release_task(), running from an interrupt on the same CPU),
task_is_descendant() then reads the freed task_struct:

  BUG: KASAN: slab-use-after-free in yama_ptrace_access_check+0x8b1/0x8e0
  Read of size 4 at addr ffff88800cdd2c78 by task yama_tracer_uaf/2865
  CPU: 0 UID: 65534 PID: 2865 Comm: yama_tracer_uaf Not tainted 7.4.0-rc3+ #1
  Call Trace:
   yama_ptrace_access_check+0x8b1/0x8e0
   security_ptrace_access_check+0xe6/0x200
   ptrace_may_access+0x31/0x50
   mm_access+0x12f/0x280
   process_vm_rw+0x2f5/0xa10
   __x64_sys_process_vm_readv+0xe5/0x100
  Freed by task 2865:
   kmem_cache_free+0x145/0x3d0
   rcu_core+0x7f4/0xe00
   handle_softirqs+0x1b8/0x590
   sysvec_apic_timer_interrupt+0x74/0x80
  Second to last potentially related work creation:
   call_rcu+0x44/0x220
   release_task+0xe46/0x1230
   wait_consider_task+0x18e6/0x2510

The faulting read is thread_group_leader(parent) in task_is_descendant()
(inlined), at offset 1528 of the 4616-byte task_struct (exit_signal).

Any unprivileged user can trigger this with the default ptrace_scope of
1: one process registers a short-lived child as its tracer and reaps it,
while an unrelated process runs PTRACE_MODE_ATTACH checks against the
first one. Before commit 93b69d437eff ("Yama: add RCU to drop read
locking") the reader held ptracer_relations_lock, which also excluded
the delete path.

Keep a reference on the tracer's struct pid instead, and resolve it
with pid_task() inside the reader's RCU section. A task found that way
stays valid until rcu_read_unlock(), since release_task() detaches the
pid before it queues the final put. Once the tracer has been reaped,
pid_task() returns NULL and the relation matches no tracer. That is
what an invalidated relation did before, so the tracer side of
yama_task_free() is no longer needed. The relation itself is still
removed when the tracee goes away or registers a new tracer. The pid
reference is dropped from an RCU callback after the relation is
unlinked.

The prctl() path looks the tracer up with find_get_pid() and
pid_has_task(). It keeps returning -EINVAL for a pid with no task, and
a tracer that is reaped right after the check leaves a pid that
resolves to nothing, never a NULL ("any tracer") relation.

Link: https://lore.kernel.org/all/0000000000004904df0578b7d3da@google.com/T/
Fixes: 93b69d437eff ("Yama: add RCU to drop read locking")
Cc: stable@vger.kernel.org
Signed-off-by: JunYoung Lee <ljyoung04@gmail.com>
---
 security/yama/yama_lsm.c | 59 ++++++++++++++++++++++++++--------------
 1 file changed, 38 insertions(+), 21 deletions(-)

diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
index cef3776cf3b2..d23a5e658aa1 100644
--- a/security/yama/yama_lsm.c
+++ b/security/yama/yama_lsm.c
@@ -29,7 +29,7 @@ static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
 
 /* describe a ptrace relationship for potential exception */
 struct ptrace_relation {
-	struct task_struct *tracer;
+	struct pid *tracer;
 	struct task_struct *tracee;
 	bool invalid;
 	struct list_head node;
@@ -107,6 +107,15 @@ static void report_access(const char *access, struct task_struct *target,
 	kfree(info);
 }
 
+static void yama_relation_free(struct rcu_head *head)
+{
+	struct ptrace_relation *relation =
+		container_of(head, struct ptrace_relation, rcu);
+
+	put_pid(relation->tracer);
+	kfree(relation);
+}
+
 /**
  * yama_relation_cleanup - remove invalid entries from the relation list
  * @work: unused
@@ -121,7 +130,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();
@@ -130,7 +139,7 @@ static void yama_relation_cleanup(struct work_struct *work)
 
 /**
  * yama_ptracer_add - add/replace an exception for this tracer/tracee pair
- * @tracer: the task_struct of the process doing the ptrace
+ * @tracer: the pid of the process doing the ptrace, NULL for any process
  * @tracee: the task_struct of the process to be ptraced
  *
  * Each tracee can have, at most, one tracer registered. Each time this
@@ -138,7 +147,7 @@ static void yama_relation_cleanup(struct work_struct *work)
  *
  * Returns 0 if relationship was added, -ve on error.
  */
-static int yama_ptracer_add(struct task_struct *tracer,
+static int yama_ptracer_add(struct pid *tracer,
 			    struct task_struct *tracee)
 {
 	struct ptrace_relation *relation, *added;
@@ -148,7 +157,7 @@ static int yama_ptracer_add(struct task_struct *tracer,
 		return -ENOMEM;
 
 	added->tracee = tracee;
-	added->tracer = tracer;
+	added->tracer = get_pid(tracer);
 	added->invalid = false;
 
 	spin_lock(&ptracer_relations_lock);
@@ -158,7 +167,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;
 		}
 	}
@@ -172,12 +181,14 @@ static int yama_ptracer_add(struct task_struct *tracer,
 }
 
 /**
- * yama_ptracer_del - remove exceptions related to the given tasks
- * @tracer: remove any relation where tracer task matches
+ * yama_ptracer_del - remove the exception registered for the given tracee
  * @tracee: remove any relation where tracee task matches
+ *
+ * A relation whose tracer has gone away is not removed here; its pid no
+ * longer resolves to a task, so it matches no tracer until it is replaced
+ * or the tracee goes away.
  */
-static void yama_ptracer_del(struct task_struct *tracer,
-			     struct task_struct *tracee)
+static void yama_ptracer_del(struct task_struct *tracee)
 {
 	struct ptrace_relation *relation;
 	bool marked = false;
@@ -186,8 +197,7 @@ static void yama_ptracer_del(struct task_struct *tracer,
 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
 		if (relation->invalid)
 			continue;
-		if (relation->tracee == tracee ||
-		    (tracer && relation->tracer == tracer)) {
+		if (relation->tracee == tracee) {
 			relation->invalid = true;
 			marked = true;
 		}
@@ -204,7 +214,7 @@ static void yama_ptracer_del(struct task_struct *tracer,
  */
 static void yama_task_free(struct task_struct *task)
 {
-	yama_ptracer_del(task, task);
+	yama_ptracer_del(task);
 }
 
 /**
@@ -235,20 +245,18 @@ static int yama_task_prctl(int option, unsigned long arg2, unsigned long arg3,
 		myself = current->group_leader;
 
 		if (arg2 == 0) {
-			yama_ptracer_del(NULL, myself);
+			yama_ptracer_del(myself);
 			rc = 0;
 		} else if (arg2 == PR_SET_PTRACER_ANY || (int)arg2 == -1) {
 			rc = yama_ptracer_add(NULL, myself);
 		} else {
-			struct task_struct *tracer;
+			struct pid *tracer = find_get_pid(arg2);
 
-			tracer = find_get_task_by_vpid(arg2);
-			if (!tracer) {
+			if (!tracer || !pid_has_task(tracer, PIDTYPE_PID))
 				rc = -EINVAL;
-			} else {
+			else
 				rc = yama_ptracer_add(tracer, myself);
-				put_task_struct(tracer);
-			}
+			put_pid(tracer);
 		}
 
 		break;
@@ -324,8 +332,17 @@ static int ptracer_exception_found(struct task_struct *tracer,
 		if (relation->invalid)
 			continue;
 		if (relation->tracee == tracee) {
-			parent = relation->tracer;
+			/*
+			 * The tracer is held by its pid: pid_task() only
+			 * returns a task that stays valid for this RCU
+			 * section, and NULL once the tracer has been reaped.
+			 */
+			parent = NULL;
 			found = true;
+			if (relation->tracer) {
+				parent = pid_task(relation->tracer, PIDTYPE_PID);
+				found = !!parent;
+			}
 			break;
 		}
 	}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-19  2:59 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19  2:59 [PATCH] yama: hold the PR_SET_PTRACER tracer by its struct pid JunYoung Lee

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®