public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] selinux: selinux_setprocattr()->ptrace_parent() needs rcu_read_lock()
@ 2013-12-05 16:59 Oleg Nesterov
  2013-12-05 21:53 ` Paul Moore
  0 siblings, 1 reply; 8+ messages in thread
From: Oleg Nesterov @ 2013-12-05 16:59 UTC (permalink / raw)
  To: Stephen Smalley, James Morris, Eric Paris, Paul Moore
  Cc: Evan McNabb, Jan Stancek, linux-kernel

selinux_setprocattr() does ptrace_parent(p) under task_lock(p),
but task_struct->alloc_lock doesn't pin ->parent or ->ptrace,
this looks confusing and triggers the "suspicious RCU usage"
warning because ptrace_parent() does rcu_dereference_check().

And in theory this is wrong, spin_lock()->preempt_disable()
doesn't necessarily imply rcu_read_lock() we need to access
the ->parent.

The patch also checks pid_alive(p) before ptrace_parent(p) to
ensure that this task can't be dead even before rcu_read_lock(),
in this case its ->parent points to nowhere. This is not really
needed "in practice", task->ptrace must be already cleared in
this case but we should not rely on this.

Note: perhaps we should simply kill ptrace_parent(), it buys
almost nothing and it is obviously racy. Or perhaps we should
change it to ensure it can't wrongly return the natural parent
if it races with ptrace_detach.

Reported-by: Evan McNabb <emcnabb@redhat.com>
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
---
 security/selinux/hooks.c |   13 ++++++++-----
 1 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 794c3ca..2adfd7a 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -5503,11 +5503,14 @@ static int selinux_setprocattr(struct task_struct *p,
 		/* Check for ptracing, and update the task SID if ok.
 		   Otherwise, leave SID unchanged and fail. */
 		ptsid = 0;
-		task_lock(p);
-		tracer = ptrace_parent(p);
-		if (tracer)
-			ptsid = task_sid(tracer);
-		task_unlock(p);
+		tracer = NULL;
+		rcu_read_lock();
+		if (pid_alive(p)) {
+			tracer = ptrace_parent(p);
+			if (tracer)
+				ptsid = task_sid(tracer);
+		}
+		rcu_read_unlock();
 
 		if (tracer) {
 			error = avc_has_perm(ptsid, sid, SECCLASS_PROCESS,
-- 
1.5.5.1



^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2013-12-16 21:12 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-05 16:59 [PATCH] selinux: selinux_setprocattr()->ptrace_parent() needs rcu_read_lock() Oleg Nesterov
2013-12-05 21:53 ` Paul Moore
2013-12-06 14:47   ` Oleg Nesterov
2013-12-14 15:16     ` Paul Moore
2013-12-14 16:32       ` Oleg Nesterov
2013-12-14 16:33         ` [PATCH v2] " Oleg Nesterov
2013-12-16 21:12           ` Paul Moore
2013-12-16 21:11         ` [PATCH] " Paul Moore

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox