Skip to contentSkip to conversation

proc: Fix timerslack_ns CAP_SYS_NICE check when adjusting self

PATCH Superseded John Stultz 3 participants 9 messages Aug 9, 2016 – Aug 10, 2016 Proc filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 proc: Fix timerslack_ns CAP_SYS_NICE check when adjusting self

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +19 -15
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
 	if (!p)
 		return -ESRCH;
 
-	if (!capable(CAP_SYS_NICE)) {
-		count = -EPERM;
-		goto out;
-	}
+	if (p != current) {
+		if (!capable(CAP_SYS_NICE)) {
+			count = -EPERM;
+			goto out;
+		}
 
-	err = security_task_setscheduler(p);
-	if (err) {
-		count = err;
-		goto out;
+		err = security_task_setscheduler(p);
+		if (err) {
+			count = err;
+			goto out;
+		}
 	}
 
 	task_lock(p);
@@ -2315,14 +2317,16 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 	if (!p)
 		return -ESRCH;
 
-	if (!capable(CAP_SYS_NICE)) {
-		err = -EPERM;
-		goto out;
-	}
+	if (p != current) {
 
-	err = security_task_getscheduler(p);
-	if (err)
-		goto out;
+		if (!capable(CAP_SYS_NICE)) {
+			err = -EPERM;
+			goto out;
+		}
+		err = security_task_getscheduler(p);
+		if (err)
+			goto out;
+	}
 
 	task_lock(p);
 	seq_printf(m, "%llu\n", p->timer_slack_ns);
Signature · 1 line
1.9.1
#1 lore raw
Quoted 51 lines · John Stultz

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +5 -4
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               count = -EPERM;
-               goto out;
-       }
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 

fs/proc/base.c static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, · line 2295

Quoted 10 lines
fs/proc/base.c +5 -4
-       err = security_task_setscheduler(p);
-       if (err) {
-               count = err;
-               goto out;
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
        }

This entirely bypasses LSM when p == current. Is that intended?

-Kees

Quoted 29 lines
fs/proc/base.c +9 -8
        task_lock(p);
@@ -2315,14 +2317,16 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               err = -EPERM;
-               goto out;
-       }
+       if (p != current) {
 
-       err = security_task_getscheduler(p);
-       if (err)
-               goto out;
+               if (!capable(CAP_SYS_NICE)) {
+                       err = -EPERM;
+                       goto out;
+               }
+               err = security_task_getscheduler(p);
+               if (err)
+                       goto out;
+       }
 
        task_lock(p);
        seq_printf(m, "%llu\n", p->timer_slack_ns);
--

1.9.1

Signature · 2 lines
Kees Cook
Nexus Security
#2 lore raw
Quoted 52 lines · Kees Cook
Quoted 51 lines · John Stultz

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +5 -4
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               count = -EPERM;
-               goto out;
-       }
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 
Quoted 12 lines

fs/proc/base.c static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, · line 2295

Quoted 10 lines
fs/proc/base.c +5 -4
-       err = security_task_setscheduler(p);
-       if (err) {
-               count = err;
-               goto out;
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
        }

This entirely bypasses LSM when p == current. Is that intended?

I wasn't entierly sure. I didn't think PR_SET_TIMERSLACK has a security hook, but looking again I now see the top-level security_task_prctl() check, so maybe not skipping it in this case would be good?

thanks
-john

#3 lore raw
Quoted 66 lines · John Stultz
Quoted 52 lines · Kees Cook
Quoted 51 lines · John Stultz

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +5 -4
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               count = -EPERM;
-               goto out;
-       }
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 
Quoted 12 lines

fs/proc/base.c static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, · line 2295

Quoted 10 lines
fs/proc/base.c +5 -4
-       err = security_task_setscheduler(p);
-       if (err) {
-               count = err;
-               goto out;
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
        }

This entirely bypasses LSM when p == current. Is that intended?

Quoted 4 lines

I wasn't entierly sure. I didn't think PR_SET_TIMERSLACK has a security hook, but looking again I now see the top-level security_task_prctl() check, so maybe not skipping it in this case would be good?

So thinking about this some more. I'm really not sure what the right thing is. Since the LSM check for security_task_setscheduler(), is different from the security_task_prctl() check, it seems odd to have different checks for different interfaces which in the p==current case are really are the same.

Suggestions?

thanks
-john

#4 lore raw
In reply to John Stultz
Quoted 4 lines · John Stultz

I wasn't entierly sure. I didn't think PR_SET_TIMERSLACK has a security hook, but looking again I now see the top-level security_task_prctl() check, so maybe not skipping it in this case would be good?

the easy fix would be to add back the ptrace check.. just either ptrace-able OR CAP_SYS_NICE ;) then you can prove you only added new stuff as well, and have all the LSM from before

#5 lore raw
Quoted 10 lines · Arjan van de Ven
Quoted 4 lines · John Stultz

I wasn't entierly sure. I didn't think PR_SET_TIMERSLACK has a security hook, but looking again I now see the top-level security_task_prctl() check, so maybe not skipping it in this case would be good?

the easy fix would be to add back the ptrace check.. just either ptrace-able OR CAP_SYS_NICE ;)

Well, I worry that just adds more complexity to trying to understand it. p==current OR CAP_SYS_NICE makes the most sense to me.

Quoted 2 lines

then you can prove you only added new stuff as well, and have all the LSM from before

The LSM bits (and how consistent or inconsistent they can be) is really the part that I have the most concern about, and I'm not sure what the best approach would be.

thanks
-john

Acked-by
#6 lore raw
In reply to Kees Cook
Quoted 52 lines · Kees Cook
Quoted 51 lines · John Stultz

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +5 -4
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               count = -EPERM;
-               goto out;
-       }
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 
Quoted 12 lines

fs/proc/base.c static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, · line 2295

Quoted 10 lines
fs/proc/base.c +5 -4
-       err = security_task_setscheduler(p);
-       if (err) {
-               count = err;
-               goto out;
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
        }

This entirely bypasses LSM when p == current. Is that intended?

I take back my concern. :) I think this is correct (as you mention in the thread: the prctl LSM hook already fired), so until there is a specific use-case that wants to block current from these actions, we can adjust the logic then.

-Kees

Quoted 38 lines

-Kees

Quoted 27 lines
fs/proc/base.c +9 -8
        task_lock(p);
@@ -2315,14 +2317,16 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               err = -EPERM;
-               goto out;
-       }
+       if (p != current) {
 
-       err = security_task_getscheduler(p);
-       if (err)
-               goto out;
+               if (!capable(CAP_SYS_NICE)) {
+                       err = -EPERM;
+                       goto out;
+               }
+               err = security_task_getscheduler(p);
+               if (err)
+                       goto out;
+       }
 
        task_lock(p);
        seq_printf(m, "%llu\n", p->timer_slack_ns);
--
Quoted 2 lines

1.9.1

--
Kees Cook
Nexus Security

Signature · 2 lines
Kees Cook
Nexus Security
#7 lore raw
Quoted 66 lines · Kees Cook
Quoted 52 lines · Kees Cook
Quoted 51 lines · John Stultz

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +5 -4
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               count = -EPERM;
-               goto out;
-       }
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 
Quoted 12 lines

fs/proc/base.c static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, · line 2295

Quoted 10 lines
fs/proc/base.c +5 -4
-       err = security_task_setscheduler(p);
-       if (err) {
-               count = err;
-               goto out;
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
        }

This entirely bypasses LSM when p == current. Is that intended?

Quoted 2 lines

I take back my concern. :) I think this is correct (as you mention in the thread: the prctl LSM hook already fired), so until there is a

But did it? The prctrl hook is just for the prctrl interface. The proc/<tid>/timerslack_ns is separate.

This is part of my confusion here, mostly in that I'm not really sure I have a good sense of philosophy for LSM hooks.
Are these just interface guards/hooks, or are we trying to map the hook to the underlying action being taken?

As with the prctrl interface, it seems like its just an interface guard, but the /proc/<tid>/timerslack_ns interface checking security_task_setscheduler() seems to be more connected to the underlying action being done by changing the timerslack value.

thanks
-john

#8 lore raw
Quoted 67 lines · John Stultz
Quoted 66 lines · Kees Cook
Quoted 64 lines · Kees Cook
Quoted 51 lines · John Stultz

In changing from checking ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS) to capable(CAP_SYS_NICE), I missed that ptrace_my_access succeeds when p == current, but the CAP_SYS_NICE doesn't.

Thus while the previous commit was intended to loosen the needed privledges to modify a processes timerslack, it needlessly restricted a task modifying its own timerslack via the proc/<tid>/timerslack_ns (which is permitted also via the PR_SET_TIMERSLACK method).

This patch corrects this by checking if p == current before checking the CAP_SYS_NICE value.

This patch applies on top of my two previous patches currently in -mm

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Thomas Gleixner <[email protected]>
CC Arjan van de Ven <[email protected]>
Cc Oren Laadan <[email protected]>
Cc Ruchi Kandoi <[email protected]>
Cc Rom Lemarchand <[email protected]>
Cc Todd Kjos <[email protected]>
Cc Colin Cross <[email protected]>
Cc Nick Kralevich <[email protected]>
Cc Dmitry Shmidt <[email protected]>
Cc Elliott Hughes <[email protected]>
Cc Android Kernel Team <[email protected]>
---
 fs/proc/base.c | 34 +++++++++++++++++++---------------
 1 file changed, 19 insertions(+), 15 deletions(-)
fs/proc/base.c +5 -4
index 02f8389..01c3c2d 100644
@@ -2281,15 +2281,17 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (!capable(CAP_SYS_NICE)) {
-               count = -EPERM;
-               goto out;
-       }
+       if (p != current) {
+               if (!capable(CAP_SYS_NICE)) {
+                       count = -EPERM;
+                       goto out;
+               }
 

fs/proc/base.c static ssize_t timerslack_ns_write(struct file *file, const char __user *buf, · line 2295

Quoted 10 lines
fs/proc/base.c +5 -4
-       err = security_task_setscheduler(p);
-       if (err) {
-               count = err;
-               goto out;
+               err = security_task_setscheduler(p);
+               if (err) {
+                       count = err;
+                       goto out;
+               }
        }

This entirely bypasses LSM when p == current. Is that intended?

Quoted 5 lines
Quoted 2 lines

I take back my concern. :) I think this is correct (as you mention in the thread: the prctl LSM hook already fired), so until there is a

But did it? The prctrl hook is just for the prctrl interface. The proc/<tid>/timerslack_ns is separate.

Oh, hrm, well, I think I'm still fine with it as-is: if we end up needing to tighten this to block current, we can change it. Nick, would this be something you'd want to be able to do with the hook?

-Kees

Quoted 9 lines

This is part of my confusion here, mostly in that I'm not really sure I have a good sense of philosophy for LSM hooks.
Are these just interface guards/hooks, or are we trying to map the hook to the underlying action being taken?

As with the prctrl interface, it seems like its just an interface guard, but the /proc/<tid>/timerslack_ns interface checking security_task_setscheduler() seems to be more connected to the underlying action being done by changing the timerslack value.

Signature · 2 lines
Kees Cook
Nexus Security