public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Serge Hallyn <serge.hallyn@canonical.com>
To: Kees Cook <keescook@chromium.org>
Cc: linux-kernel@vger.kernel.org,
	James Morris <james.l.morris@oracle.com>,
	John Johansen <john.johansen@canonical.com>,
	"Serge E. Hallyn" <serge.hallyn@ubuntu.com>,
	Eric Paris <eparis@redhat.com>,
	linux-security-module@vger.kernel.org
Subject: Re: [PATCH] Yama: remove locking from delete path
Date: Mon, 19 Nov 2012 21:45:15 -0600	[thread overview]
Message-ID: <20121120034515.GA5212@sergelap> (raw)
In-Reply-To: <20121119233459.GA9524@www.outflux.net>

Quoting Kees Cook (keescook@chromium.org):
> Instead of locking the list during a delete, mark entries as invalid
> and trigger a workqueue to clean them up. This lets us easily handle
> task_free from interrupt context.
> 
> Cc: Sasha Levin <sasha.levin@oracle.com>
> Signed-off-by: Kees Cook <keescook@chromium.org>
> ---
>  security/yama/yama_lsm.c |   43 ++++++++++++++++++++++++++++++++++++-------
>  1 file changed, 36 insertions(+), 7 deletions(-)
> 
> diff --git a/security/yama/yama_lsm.c b/security/yama/yama_lsm.c
> index 17da6ca..1cba901 100644
> --- a/security/yama/yama_lsm.c
> +++ b/security/yama/yama_lsm.c
> @@ -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
> @@ -29,6 +30,7 @@ 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;
>  };
> @@ -36,6 +38,27 @@ struct ptrace_relation {
>  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);
> +	list_for_each_entry_rcu(relation, &ptracer_relations, node) {
> +		if (relation->invalid) {
> +			list_del_rcu(&relation->node);
> +			kfree_rcu(relation, rcu);
> +		}
> +	}
> +	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
> @@ -57,9 +80,12 @@ 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);
>  	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);
> @@ -70,7 +96,7 @@ static int yama_ptracer_add(struct task_struct *tracer,
>  	list_add_rcu(&added->node, &ptracer_relations);
>  
>  out:
> -	spin_unlock_bh(&ptracer_relations_lock);
> +	spin_unlock(&ptracer_relations_lock);
>  	return 0;
>  }
>  
> @@ -84,15 +110,15 @@ static void yama_ptracer_del(struct task_struct *tracer,
>  {
>  	struct ptrace_relation *relation;
>  
> -	spin_lock_bh(&ptracer_relations_lock);

I don't understand - is there a patch I don't have sitting around
which puts the calls to yama_ptracer_del() under rcu_read_lock()?
If not, I don't see how it's safe to walk the list here and risk
racing against another yama_relation_cleanup() run.

I'm probably missing something really cool about the locking,
but it doesn't look right to me.  I would think you'd want to
do the loop under rcu_read_lock(), set a boolean if one is
changed, and call schedule_work() once at the end if the boolean
is set.

>  	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;
> +			schedule_work(&yama_relation_work);
>  		}
>  	}
> -	spin_unlock_bh(&ptracer_relations_lock);
>  }
>  
>  /**
> @@ -219,12 +245,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;
> -- 
> 1.7.9.5
> 
> 
> -- 
> Kees Cook
> Chrome OS Security

  parent reply	other threads:[~2012-11-20  3:45 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-19 23:34 [PATCH] Yama: remove locking from delete path Kees Cook
2012-11-20  2:23 ` James Morris
2012-11-20  3:01   ` Kees Cook
2012-11-20  3:46     ` James Morris
2012-11-20  3:45 ` Serge Hallyn [this message]
2012-11-20  6:14   ` Kees Cook
2012-11-20  6:53     ` Kees Cook
2012-11-20 12:24 ` Tetsuo Handa
2012-11-20 16:07   ` Kees Cook

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121120034515.GA5212@sergelap \
    --to=serge.hallyn@canonical.com \
    --cc=eparis@redhat.com \
    --cc=james.l.morris@oracle.com \
    --cc=john.johansen@canonical.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=serge.hallyn@ubuntu.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox