Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 344834
b: refs/heads/master
c: 235e752
h: refs/heads/master
v: v3
  • Loading branch information
Kees Cook committed Nov 20, 2012
1 parent 3718fa5 commit a09161a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 93b69d437effff11b1c37f330d3265c37ec2f84b
refs/heads/master: 235e752789eb65a81477bb82845323dfcbf93012
49 changes: 42 additions & 7 deletions trunk/security/yama/yama_lsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <linux/ptrace.h>
#include <linux/prctl.h>
#include <linux/ratelimit.h>
#include <linux/workqueue.h>

#define YAMA_SCOPE_DISABLED 0
#define YAMA_SCOPE_RELATIONAL 1
Expand All @@ -29,13 +30,37 @@ static int ptrace_scope = YAMA_SCOPE_RELATIONAL;
struct ptrace_relation {
struct task_struct *tracer;
struct task_struct *tracee;
bool invalid;
struct list_head node;
struct rcu_head rcu;
};

static LIST_HEAD(ptracer_relations);
static DEFINE_SPINLOCK(ptracer_relations_lock);

static void yama_relation_cleanup(struct work_struct *work);
static DECLARE_WORK(yama_relation_work, yama_relation_cleanup);

/**
* yama_relation_cleanup - remove invalid entries from the relation list
*
*/
static void yama_relation_cleanup(struct work_struct *work)
{
struct ptrace_relation *relation;

spin_lock(&ptracer_relations_lock);
rcu_read_lock();
list_for_each_entry_rcu(relation, &ptracer_relations, node) {
if (relation->invalid) {
list_del_rcu(&relation->node);
kfree_rcu(relation, rcu);
}
}
rcu_read_unlock();
spin_unlock(&ptracer_relations_lock);
}

/**
* yama_ptracer_add - add/replace an exception for this tracer/tracee pair
* @tracer: the task_struct of the process doing the ptrace
Expand All @@ -57,10 +82,13 @@ static int yama_ptracer_add(struct task_struct *tracer,

added->tracee = tracee;
added->tracer = tracer;
added->invalid = false;

spin_lock_bh(&ptracer_relations_lock);
spin_lock(&ptracer_relations_lock);
rcu_read_lock();
list_for_each_entry_rcu(relation, &ptracer_relations, node) {
if (relation->invalid)
continue;
if (relation->tracee == tracee) {
list_replace_rcu(&relation->node, &added->node);
kfree_rcu(relation, rcu);
Expand All @@ -72,7 +100,7 @@ static int yama_ptracer_add(struct task_struct *tracer,

out:
rcu_read_unlock();
spin_unlock_bh(&ptracer_relations_lock);
spin_unlock(&ptracer_relations_lock);
return 0;
}

Expand All @@ -85,18 +113,22 @@ static void yama_ptracer_del(struct task_struct *tracer,
struct task_struct *tracee)
{
struct ptrace_relation *relation;
bool marked = false;

spin_lock_bh(&ptracer_relations_lock);
rcu_read_lock();
list_for_each_entry_rcu(relation, &ptracer_relations, node) {
if (relation->invalid)
continue;
if (relation->tracee == tracee ||
(tracer && relation->tracer == tracer)) {
list_del_rcu(&relation->node);
kfree_rcu(relation, rcu);
relation->invalid = true;
marked = true;
}
}
rcu_read_unlock();
spin_unlock_bh(&ptracer_relations_lock);

if (marked)
schedule_work(&yama_relation_work);
}

/**
Expand Down Expand Up @@ -223,12 +255,15 @@ static int ptracer_exception_found(struct task_struct *tracer,
rcu_read_lock();
if (!thread_group_leader(tracee))
tracee = rcu_dereference(tracee->group_leader);
list_for_each_entry_rcu(relation, &ptracer_relations, node)
list_for_each_entry_rcu(relation, &ptracer_relations, node) {
if (relation->invalid)
continue;
if (relation->tracee == tracee) {
parent = relation->tracer;
found = true;
break;
}
}

if (found && (parent == NULL || task_is_descendant(parent, tracer)))
rc = 1;
Expand Down

0 comments on commit a09161a

Please sign in to comment.