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;
}
}