yama: fix use-after-free read of a dead PR_SET_PTRACER tracer
Subsystems (3)
PATCH 1/1 yama: fix use-after-free read of a dead PR_SET_PTRACER tracer
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.
--- security/yama/yama_lsm.c | 42 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-)
security/yama/yama_lsm.c +38 -4
@@ -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)))
Signature · 1 line
2.53.0
[email protected]
AI reviewSashiko has reviewed this patch and found no issues.
Quoted 15 lines
A
PR_SET_PTRACERrelation stores a bare pointer to the tracer'stask_struct. When the tracer dies, itstask_structis freed viacall_rcu(), and the relation is only marked invalid fromsecurity_task_free(), i.e. from the same RCU callback that frees the task. A reader that entersrcu_read_lock()after the callback was queued is not protected against it:ptracer_exception_found()can readrelation->invalidwhile it is still false and then dereference the already freed tracer intask_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
Quoted 6 lines
[ ... ]
This issue was discovered by Tencent CodeBuddy Security.
Fixes 93b69d437eff ("Yama: add RCU to drop read locking")Signed-off-by Henry Martin <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1