Skip to contentSkip to conversation

Yama: allow access for the current ptrace parent

PATCH In review Josh Stone 2 participants 4 messages Dec 1, 2016 – Dec 5, 2016

Subsystems (2)
lore
Share & tools
lore

Josh Stone

author
#0 lore raw

PATCH 1/1 Yama: allow access for the current ptrace parent

Under ptrace_scope=1, it's possible to have a tracee that is already ptrace-attached, but is no longer a direct descendant. For instance, a forking daemon will be re-parented to init, losing its ancestry to the tracer that launched it.

The tracer can continue using ptrace in that state, but it will be denied other accesses that check PTRACE_MODE_ATTACH, like process_vm_rw and various procfs files. There's no reason to prevent such access for a tracer that already has ptrace control anyway.

This patch adds a case to ptracer_exception_found to allow access for any task in the same thread group as the current ptrace parent.

Cc Kees Cook <[email protected]>
Cc James Morris <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
---
 security/yama/yama_lsm.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
security/yama/yama_lsm.c +14 -1
index 0309f2111c70..da67a6e07a60 100644
@@ -309,7 +309,7 @@ static int task_is_descendant(struct task_struct *parent,
  * @tracer: the task_struct of the process attempting ptrace
  * @tracee: the task_struct of the process to be ptraced
  *
- * Returns 1 if tracer has is ptracer exception ancestor for tracee.
+ * Returns 1 if tracer has a ptracer exception ancestor for tracee.
  */
 static int ptracer_exception_found(struct task_struct *tracer,
 				   struct task_struct *tracee)
@@ -320,6 +320,17 @@ static int ptracer_exception_found(struct task_struct *tracer,
 	bool found = false;
 
 	rcu_read_lock();
+
+	/* If there's already an active tracing relationship, then make an
+	 * exception for the sake of other accesses, like process_vm_rw.
+	 */
+	parent = ptrace_parent(tracee);
+	if (parent != NULL && same_thread_group(parent, tracer)) {
+		rc = 1;
+		goto unlock;
+	}
+
+	/* Look for a PR_SET_PTRACER relationship. */
 	if (!thread_group_leader(tracee))
 		tracee = rcu_dereference(tracee->group_leader);
 	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
@@ -334,6 +345,8 @@ static int ptracer_exception_found(struct task_struct *tracer,
 
 	if (found && (parent == NULL || task_is_descendant(parent, tracer)))
 		rc = 1;
+
+unlock:
 	rcu_read_unlock();
 
 	return rc;
Signature · 1 line
2.9.3
#1 lore raw
Quoted 5 lines · Josh Stone

Under ptrace_scope=1, it's possible to have a tracee that is already ptrace-attached, but is no longer a direct descendant. For instance, a forking daemon will be re-parented to init, losing its ancestry to the tracer that launched it.

Quoted 7 lines

The tracer can continue using ptrace in that state, but it will be denied other accesses that check PTRACE_MODE_ATTACH, like process_vm_rw and various procfs files. There's no reason to prevent such access for a tracer that already has ptrace control anyway.

This patch adds a case to ptracer_exception_found to allow access for any task in the same thread group as the current ptrace parent.

Nice catch, thanks!

Quoted 23 lines
Cc Kees Cook <[email protected]>
Cc James Morris <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
---
 security/yama/yama_lsm.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
security/yama/yama_lsm.c +1 -1
index 0309f2111c70..da67a6e07a60 100644
@@ -309,7 +309,7 @@ static int task_is_descendant(struct task_struct *parent,
  * @tracer: the task_struct of the process attempting ptrace
  * @tracee: the task_struct of the process to be ptraced
  *
- * Returns 1 if tracer has is ptracer exception ancestor for tracee.
+ * Returns 1 if tracer has a ptracer exception ancestor for tracee.
  */
 static int ptracer_exception_found(struct task_struct *tracer,
                                   struct task_struct *tracee)

security/yama/yama_lsm.c static int ptracer_exception_found(struct task_struct *tracer, · line 324

Quoted 5 lines
security/yama/yama_lsm.c +2 -0
@@ -320,6 +320,17 @@ static int ptracer_exception_found(struct task_struct *tracer,
        bool found = false;
 
        rcu_read_lock();
+
+       /* If there's already an active tracing relationship, then make an

I'll adjust the comment style here and add it to my tree for -next.

Quoted 22 lines
security/yama/yama_lsm.c +11 -1
+        * exception for the sake of other accesses, like process_vm_rw.
+        */
+       parent = ptrace_parent(tracee);
+       if (parent != NULL && same_thread_group(parent, tracer)) {
+               rc = 1;
+               goto unlock;
+       }
+
+       /* Look for a PR_SET_PTRACER relationship. */
        if (!thread_group_leader(tracee))
                tracee = rcu_dereference(tracee->group_leader);
        list_for_each_entry_rcu(relation, &ptracer_relations, node) {
@@ -334,6 +345,8 @@ static int ptracer_exception_found(struct task_struct *tracer,
 
        if (found && (parent == NULL || task_is_descendant(parent, tracer)))
                rc = 1;
+
+unlock:
        rcu_read_unlock();
 
        return rc;
--
Quoted 2 lines

2.9.3

Thanks!

-Kees

Signature · 2 lines
Kees Cook
Nexus Security
author
#2 lore raw
Quoted 3 lines · Kees Cook
Quoted 1 line

+ /* If there's already an active tracing relationship, then make an

I'll adjust the comment style here and add it to my tree for -next.

Thanks!

I guess the tweak is that it should have an empty "/*" line?

FWIW, checkpatch.pl doesn't warn about this -- perhaps it should? I only see the opposite check for NETWORKING_BLOCK_COMMENT_STYLE.

#3 lore raw
Quoted 11 lines · Josh Stone
Quoted 3 lines · Kees Cook
Quoted 1 line

+ /* If there's already an active tracing relationship, then make an

I'll adjust the comment style here and add it to my tree for -next.

Thanks!

I guess the tweak is that it should have an empty "/*" line?

FWIW, checkpatch.pl doesn't warn about this -- perhaps it should? I only see the opposite check for NETWORKING_BLOCK_COMMENT_STYLE.

Hrm, I thought it did warn. But yeah, the networking subsystem uses this style but everywhere else doesn't. :P Wheee. :)

-Kees

Signature · 2 lines
Kees Cook
Nexus Security