Skip to contentSkip to conversation

proc: Relax /proc/<tid>/timerslack_ns capability requirements

RFC PATCH Superseded John Stultz 5 participants 11 messages Jul 18, 2016 – Jul 21, 2016

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v3 1/2 proc: Relax /proc/<tid>/timerslack_ns capability requirements

When an interface to allow a task to change another tasks timerslack was first proposed, it was suggested that something greater then CAP_SYS_NICE would be needed, as a task could be delayed further then what normally could be done with nice adjustments.

So CAP_SYS_PTRACE was adopted instead for what became the /proc/<tid>/timerslack_ns interface. However, for Android (where this feature originates), giving the system_server CAP_SYS_PTRACE would allow it to observe and modify all tasks memory. This is considered too high a privilege level for only needing to change the timerslack.

After some discussion, it was realized that a CAP_SYS_NICE process can set a task as SCHED_FIFO, so they could fork some spinning processes and set them all SCHED_FIFO 99, in effect delaying all other tasks for an infinite amount of time.

So as a CAP_SYS_NICE task can already cause trouble for other tasks, using it as a required capability for accessing and modifying /proc/<tid>/timerslack_ns seems sufficient.

Thus, this patch loosens the capability requirements to CAP_SYS_NICE and removes CAP_SYS_PTRACE, simplifying some of the code flow as well.

This is technically an ABI change, but as the feature just landed in 4.6, I suspect no one is yet using it.

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]>

---
v2: Removed CAP_SYS_PTRACE check and simplified code flow
v3: Tweaked where CAP_SYS_NICE check is made, suggeded by NickK

 fs/proc/base.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)
fs/proc/base.c +20 -14
index a11eb71..c94abae 100644
@@ -2281,16 +2281,19 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
 	if (!p)
 		return -ESRCH;
 
-	if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-		task_lock(p);
-		if (slack_ns == 0)
-			p->timer_slack_ns = p->default_timer_slack_ns;
-		else
-			p->timer_slack_ns = slack_ns;
-		task_unlock(p);
-	} else
+	if (!capable(CAP_SYS_NICE)) {
 		count = -EPERM;
+		goto out;
+	}
+
+	task_lock(p);
+	if (slack_ns == 0)
+		p->timer_slack_ns = p->default_timer_slack_ns;
+	else
+		p->timer_slack_ns = slack_ns;
+	task_unlock(p);
 
+out:
 	put_task_struct(p);
 
 	return count;
@@ -2300,19 +2303,22 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 {
 	struct inode *inode = m->private;
 	struct task_struct *p;
-	int err =  0;
+	int err = 0;
 
 	p = get_proc_task(inode);
 	if (!p)
 		return -ESRCH;
 
-	if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-		task_lock(p);
-		seq_printf(m, "%llu\n", p->timer_slack_ns);
-		task_unlock(p);
-	} else
+	if (!capable(CAP_SYS_NICE)) {
 		err = -EPERM;
+		goto out;
+	}
 
+	task_lock(p);
+	seq_printf(m, "%llu\n", p->timer_slack_ns);
+	task_unlock(p);
+
+out:
 	put_task_struct(p);
 
 	return err;
Signature · 1 line
1.9.1
#1 lore raw

RFC PATCH v3 2/2 security: Add task_settimerslack/task_gettimerslack LSM hook

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

Don't really know what I'm doing here, so close review would be appreciated!

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]>

---
v2:
* Initial swing at adding settimerslack LSM hook
v3:
* Fix current/p switchup bug noted by NickK
* Add gettimerslack hook suggested by NickK

 fs/proc/base.c            | 10 ++++++++++
 include/linux/lsm_hooks.h | 13 +++++++++++++
 include/linux/security.h  | 12 ++++++++++++
 security/security.c       | 14 ++++++++++++++
 security/selinux/hooks.c  | 12 ++++++++++++
 5 files changed, 61 insertions(+)
fs/proc/base.c +10 -0
index c94abae..cc66aa8 100644
@@ -2286,6 +2286,12 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
 		goto out;
 	}
 
+	err = security_task_settimerslack(p, slack_ns);
+	if (err) {
+		count = err;
+		goto out;
+	}
+
 	task_lock(p);
 	if (slack_ns == 0)
 		p->timer_slack_ns = p->default_timer_slack_ns;
@@ -2314,6 +2320,10 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 		goto out;
 	}
 
+	ret = security_task_gettimerslack(p);
+	if (ret)
+		goto out;
+
 	task_lock(p);
 	seq_printf(m, "%llu\n", p->timer_slack_ns);
 	task_unlock(p);
include/linux/lsm_hooks.h +13 -0
index 7ae3976..290483e 100644
@@ -627,6 +627,15 @@
  *	Check permission before moving memory owned by process @p.
  *	@p contains the task_struct for process.
  *	Return 0 if permission is granted.
+ * @task_settimerslack:
+ *	Check permission before setting timerslack value of @p to @slack.
+ *	@p contains the task_struct of a process.
+ *	@slack contains the new slack value.
+ *	Return 0 if permission is granted.
+ * @task_gettimerslack:
+ *	Check permission before returning the timerslack value of @p.
+ *	@p contains the task_struct of a process.
+ *	Return 0 if permission is granted.
  * @task_kill:
  *	Check permission before sending signal @sig to @p.  @info can be NULL,
  *	the constant 1, or a pointer to a siginfo structure.  If @info is 1 or
@@ -1473,6 +1482,8 @@ union security_list_options {
 	int (*task_setscheduler)(struct task_struct *p);
 	int (*task_getscheduler)(struct task_struct *p);
 	int (*task_movememory)(struct task_struct *p);
+	int (*task_settimerslack)(struct task_struct *p, u64 slack);
+	int (*task_gettimerslack)(struct task_struct *p);
 	int (*task_kill)(struct task_struct *p, struct siginfo *info,
 				int sig, u32 secid);
 	int (*task_wait)(struct task_struct *p);
@@ -1732,6 +1743,8 @@ struct security_hook_heads {
 	struct list_head task_setscheduler;
 	struct list_head task_getscheduler;
 	struct list_head task_movememory;
+	struct list_head task_settimerslack;
+	struct list_head task_gettimerslack;
 	struct list_head task_kill;
 	struct list_head task_wait;
 	struct list_head task_prctl;
include/linux/security.h +12 -0
index 14df373..ab70f47 100644
@@ -325,6 +325,8 @@ int security_task_setrlimit(struct task_struct *p, unsigned int resource,
 int security_task_setscheduler(struct task_struct *p);
 int security_task_getscheduler(struct task_struct *p);
 int security_task_movememory(struct task_struct *p);
+int security_task_settimerslack(struct task_struct *p, u64 slack);
+int security_task_gettimerslack(struct task_struct *p);
 int security_task_kill(struct task_struct *p, struct siginfo *info,
 			int sig, u32 secid);
 int security_task_wait(struct task_struct *p);
@@ -950,6 +952,16 @@ static inline int security_task_movememory(struct task_struct *p)
 	return 0;
 }
 
+static inline int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+	return 0;
+}
+
+static inline int security_task_gettimerslack(struct task_struct *p)
+{
+	return 0;
+}
+
 static inline int security_task_kill(struct task_struct *p,
 				     struct siginfo *info, int sig,
 				     u32 secid)
security/security.c +14 -0
index 7095693..4ced236 100644
@@ -977,6 +977,16 @@ int security_task_movememory(struct task_struct *p)
 	return call_int_hook(task_movememory, 0, p);
 }
 
+int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+	return call_int_hook(task_settimerslack, 0, p, slack);
+}
+
+int security_task_gettimerslack(struct task_struct *p)
+{
+	return call_int_hook(task_gettimerslack, 0, p);
+}
+
 int security_task_kill(struct task_struct *p, struct siginfo *info,
 			int sig, u32 secid)
 {
@@ -1720,6 +1730,10 @@ struct security_hook_heads security_hook_heads = {
 		LIST_HEAD_INIT(security_hook_heads.task_getscheduler),
 	.task_movememory =
 		LIST_HEAD_INIT(security_hook_heads.task_movememory),
+	.task_settimerslack =
+		LIST_HEAD_INIT(security_hook_heads.task_settimerslack),
+	.task_gettimerslack =
+		LIST_HEAD_INIT(security_hook_heads.task_gettimerslack),
 	.task_kill =	LIST_HEAD_INIT(security_hook_heads.task_kill),
 	.task_wait =	LIST_HEAD_INIT(security_hook_heads.task_wait),
 	.task_prctl =	LIST_HEAD_INIT(security_hook_heads.task_prctl),
security/selinux/hooks.c +12 -0
index a86d537..aac9599 100644
@@ -3849,6 +3849,16 @@ static int selinux_task_movememory(struct task_struct *p)
 	return current_has_perm(p, PROCESS__SETSCHED);
 }
 
+static int selinux_task_settimerslack(struct task_struct *p, u64 slack)
+{
+	return current_has_perm(p, PROCESS__SETSCHED);
+}
+
+static int selinux_task_gettimerslack(struct task_struct *p)
+{
+	return current_has_perm(p, PROCESS__GETSCHED);
+}
+
 static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
 				int sig, u32 secid)
 {
@@ -6092,6 +6102,8 @@ static struct security_hook_list selinux_hooks[] = {
 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
+	LSM_HOOK_INIT(task_settimerslack, selinux_task_settimerslack),
+	LSM_HOOK_INIT(task_gettimerslack, selinux_task_gettimerslack),
 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
 	LSM_HOOK_INIT(task_wait, selinux_task_wait),
 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Signature · 1 line
1.9.1
Reviewed-by on v3 2/2
#2 lore raw

Re: [RFC][PATCH 2/2 v3] security: Add task_settimerslack/task_gettimerslack LSM hook

Quoted 10 lines · John Stultz

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

Don't really know what I'm doing here, so close review would be appreciated!

Looks good. Thanks!

Quoted 193 lines
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]>

---
v2:
* Initial swing at adding settimerslack LSM hook
v3:
* Fix current/p switchup bug noted by NickK
* Add gettimerslack hook suggested by NickK

 fs/proc/base.c            | 10 ++++++++++
 include/linux/lsm_hooks.h | 13 +++++++++++++
 include/linux/security.h  | 12 ++++++++++++
 security/security.c       | 14 ++++++++++++++
 security/selinux/hooks.c  | 12 ++++++++++++
 5 files changed, 61 insertions(+)
fs/proc/base.c +10 -0
index c94abae..cc66aa8 100644
@@ -2286,6 +2286,12 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
                goto out;
        }
 
+       err = security_task_settimerslack(p, slack_ns);
+       if (err) {
+               count = err;
+               goto out;
+       }
+
        task_lock(p);
        if (slack_ns == 0)
                p->timer_slack_ns = p->default_timer_slack_ns;
@@ -2314,6 +2320,10 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
                goto out;
        }
 
+       ret = security_task_gettimerslack(p);
+       if (ret)
+               goto out;
+
        task_lock(p);
        seq_printf(m, "%llu\n", p->timer_slack_ns);
        task_unlock(p);
include/linux/lsm_hooks.h +13 -0
index 7ae3976..290483e 100644
@@ -627,6 +627,15 @@
  *     Check permission before moving memory owned by process @p.
  *     @p contains the task_struct for process.
  *     Return 0 if permission is granted.
+ * @task_settimerslack:
+ *     Check permission before setting timerslack value of @p to @slack.
+ *     @p contains the task_struct of a process.
+ *     @slack contains the new slack value.
+ *     Return 0 if permission is granted.
+ * @task_gettimerslack:
+ *     Check permission before returning the timerslack value of @p.
+ *     @p contains the task_struct of a process.
+ *     Return 0 if permission is granted.
  * @task_kill:
  *     Check permission before sending signal @sig to @p.  @info can be NULL,
  *     the constant 1, or a pointer to a siginfo structure.  If @info is 1 or
@@ -1473,6 +1482,8 @@ union security_list_options {
        int (*task_setscheduler)(struct task_struct *p);
        int (*task_getscheduler)(struct task_struct *p);
        int (*task_movememory)(struct task_struct *p);
+       int (*task_settimerslack)(struct task_struct *p, u64 slack);
+       int (*task_gettimerslack)(struct task_struct *p);
        int (*task_kill)(struct task_struct *p, struct siginfo *info,
                                int sig, u32 secid);
        int (*task_wait)(struct task_struct *p);
@@ -1732,6 +1743,8 @@ struct security_hook_heads {
        struct list_head task_setscheduler;
        struct list_head task_getscheduler;
        struct list_head task_movememory;
+       struct list_head task_settimerslack;
+       struct list_head task_gettimerslack;
        struct list_head task_kill;
        struct list_head task_wait;
        struct list_head task_prctl;
include/linux/security.h +12 -0
index 14df373..ab70f47 100644
@@ -325,6 +325,8 @@ int security_task_setrlimit(struct task_struct *p, unsigned int resource,
 int security_task_setscheduler(struct task_struct *p);
 int security_task_getscheduler(struct task_struct *p);
 int security_task_movememory(struct task_struct *p);
+int security_task_settimerslack(struct task_struct *p, u64 slack);
+int security_task_gettimerslack(struct task_struct *p);
 int security_task_kill(struct task_struct *p, struct siginfo *info,
                        int sig, u32 secid);
 int security_task_wait(struct task_struct *p);
@@ -950,6 +952,16 @@ static inline int security_task_movememory(struct task_struct *p)
        return 0;
 }
 
+static inline int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+       return 0;
+}
+
+static inline int security_task_gettimerslack(struct task_struct *p)
+{
+       return 0;
+}
+
 static inline int security_task_kill(struct task_struct *p,
                                     struct siginfo *info, int sig,
                                     u32 secid)
security/security.c +14 -0
index 7095693..4ced236 100644
@@ -977,6 +977,16 @@ int security_task_movememory(struct task_struct *p)
        return call_int_hook(task_movememory, 0, p);
 }
 
+int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+       return call_int_hook(task_settimerslack, 0, p, slack);
+}
+
+int security_task_gettimerslack(struct task_struct *p)
+{
+       return call_int_hook(task_gettimerslack, 0, p);
+}
+
 int security_task_kill(struct task_struct *p, struct siginfo *info,
                        int sig, u32 secid)
 {
@@ -1720,6 +1730,10 @@ struct security_hook_heads security_hook_heads = {
                LIST_HEAD_INIT(security_hook_heads.task_getscheduler),
        .task_movememory =
                LIST_HEAD_INIT(security_hook_heads.task_movememory),
+       .task_settimerslack =
+               LIST_HEAD_INIT(security_hook_heads.task_settimerslack),
+       .task_gettimerslack =
+               LIST_HEAD_INIT(security_hook_heads.task_gettimerslack),
        .task_kill =    LIST_HEAD_INIT(security_hook_heads.task_kill),
        .task_wait =    LIST_HEAD_INIT(security_hook_heads.task_wait),
        .task_prctl =   LIST_HEAD_INIT(security_hook_heads.task_prctl),
security/selinux/hooks.c +12 -1
index a86d537..aac9599 100644
@@ -3849,6 +3849,16 @@ static int selinux_task_movememory(struct task_struct *p)
        return current_has_perm(p, PROCESS__SETSCHED);
 }
 
+static int selinux_task_settimerslack(struct task_struct *p, u64 slack)
+{
+       return current_has_perm(p, PROCESS__SETSCHED);
+}
+
+static int selinux_task_gettimerslack(struct task_struct *p)
+{
+       return current_has_perm(p, PROCESS__GETSCHED);
+}
+
 static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
                                int sig, u32 secid)
 {
@@ -6092,6 +6102,8 @@ static struct security_hook_list selinux_hooks[] = {
        LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
        LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
        LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
+       LSM_HOOK_INIT(task_settimerslack, selinux_task_settimerslack),
+       LSM_HOOK_INIT(task_gettimerslack, selinux_task_gettimerslack),
        LSM_HOOK_INIT(task_kill, selinux_task_kill),
        LSM_HOOK_INIT(task_wait, selinux_task_wait),
        LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
--

1.9.1

Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
maintainer Acked-by on v3 2/2
#3 lore raw

Re: [RFC][PATCH 2/2 v3] security: Add task_settimerslack/task_gettimerslack LSM hook

In reply to John Stultz
Quoted 8 lines · John Stultz

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

Quoted 5 lines

Don't really know what I'm doing here, so close review would be appreciated!

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Quoted 189 lines
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]>

---
v2:
* Initial swing at adding settimerslack LSM hook
v3:
* Fix current/p switchup bug noted by NickK
* Add gettimerslack hook suggested by NickK

 fs/proc/base.c            | 10 ++++++++++
 include/linux/lsm_hooks.h | 13 +++++++++++++
 include/linux/security.h  | 12 ++++++++++++
 security/security.c       | 14 ++++++++++++++
 security/selinux/hooks.c  | 12 ++++++++++++
 5 files changed, 61 insertions(+)
fs/proc/base.c +10 -0
index c94abae..cc66aa8 100644
@@ -2286,6 +2286,12 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
 		goto out;
 	}
 
+	err = security_task_settimerslack(p, slack_ns);
+	if (err) {
+		count = err;
+		goto out;
+	}
+
 	task_lock(p);
 	if (slack_ns == 0)
 		p->timer_slack_ns = p->default_timer_slack_ns;
@@ -2314,6 +2320,10 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 		goto out;
 	}
 
+	ret = security_task_gettimerslack(p);
+	if (ret)
+		goto out;
+
 	task_lock(p);
 	seq_printf(m, "%llu\n", p->timer_slack_ns);
 	task_unlock(p);
include/linux/lsm_hooks.h +13 -0
index 7ae3976..290483e 100644
@@ -627,6 +627,15 @@
  *	Check permission before moving memory owned by process @p.
  *	@p contains the task_struct for process.
  *	Return 0 if permission is granted.
+ * @task_settimerslack:
+ *	Check permission before setting timerslack value of @p to @slack.
+ *	@p contains the task_struct of a process.
+ *	@slack contains the new slack value.
+ *	Return 0 if permission is granted.
+ * @task_gettimerslack:
+ *	Check permission before returning the timerslack value of @p.
+ *	@p contains the task_struct of a process.
+ *	Return 0 if permission is granted.
  * @task_kill:
  *	Check permission before sending signal @sig to @p.  @info can be NULL,
  *	the constant 1, or a pointer to a siginfo structure.  If @info is 1 or
@@ -1473,6 +1482,8 @@ union security_list_options {
 	int (*task_setscheduler)(struct task_struct *p);
 	int (*task_getscheduler)(struct task_struct *p);
 	int (*task_movememory)(struct task_struct *p);
+	int (*task_settimerslack)(struct task_struct *p, u64 slack);
+	int (*task_gettimerslack)(struct task_struct *p);
 	int (*task_kill)(struct task_struct *p, struct siginfo *info,
 				int sig, u32 secid);
 	int (*task_wait)(struct task_struct *p);
@@ -1732,6 +1743,8 @@ struct security_hook_heads {
 	struct list_head task_setscheduler;
 	struct list_head task_getscheduler;
 	struct list_head task_movememory;
+	struct list_head task_settimerslack;
+	struct list_head task_gettimerslack;
 	struct list_head task_kill;
 	struct list_head task_wait;
 	struct list_head task_prctl;
include/linux/security.h +12 -0
index 14df373..ab70f47 100644
@@ -325,6 +325,8 @@ int security_task_setrlimit(struct task_struct *p, unsigned int resource,
 int security_task_setscheduler(struct task_struct *p);
 int security_task_getscheduler(struct task_struct *p);
 int security_task_movememory(struct task_struct *p);
+int security_task_settimerslack(struct task_struct *p, u64 slack);
+int security_task_gettimerslack(struct task_struct *p);
 int security_task_kill(struct task_struct *p, struct siginfo *info,
 			int sig, u32 secid);
 int security_task_wait(struct task_struct *p);
@@ -950,6 +952,16 @@ static inline int security_task_movememory(struct task_struct *p)
 	return 0;
 }
 
+static inline int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+	return 0;
+}
+
+static inline int security_task_gettimerslack(struct task_struct *p)
+{
+	return 0;
+}
+
 static inline int security_task_kill(struct task_struct *p,
 				     struct siginfo *info, int sig,
 				     u32 secid)
security/security.c +14 -0
index 7095693..4ced236 100644
@@ -977,6 +977,16 @@ int security_task_movememory(struct task_struct *p)
 	return call_int_hook(task_movememory, 0, p);
 }
 
+int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+	return call_int_hook(task_settimerslack, 0, p, slack);
+}
+
+int security_task_gettimerslack(struct task_struct *p)
+{
+	return call_int_hook(task_gettimerslack, 0, p);
+}
+
 int security_task_kill(struct task_struct *p, struct siginfo *info,
 			int sig, u32 secid)
 {
@@ -1720,6 +1730,10 @@ struct security_hook_heads security_hook_heads = {
 		LIST_HEAD_INIT(security_hook_heads.task_getscheduler),
 	.task_movememory =
 		LIST_HEAD_INIT(security_hook_heads.task_movememory),
+	.task_settimerslack =
+		LIST_HEAD_INIT(security_hook_heads.task_settimerslack),
+	.task_gettimerslack =
+		LIST_HEAD_INIT(security_hook_heads.task_gettimerslack),
 	.task_kill =	LIST_HEAD_INIT(security_hook_heads.task_kill),
 	.task_wait =	LIST_HEAD_INIT(security_hook_heads.task_wait),
 	.task_prctl =	LIST_HEAD_INIT(security_hook_heads.task_prctl),
security/selinux/hooks.c +12 -0
index a86d537..aac9599 100644
@@ -3849,6 +3849,16 @@ static int selinux_task_movememory(struct task_struct *p)
 	return current_has_perm(p, PROCESS__SETSCHED);
 }
 
+static int selinux_task_settimerslack(struct task_struct *p, u64 slack)
+{
+	return current_has_perm(p, PROCESS__SETSCHED);
+}
+
+static int selinux_task_gettimerslack(struct task_struct *p)
+{
+	return current_has_perm(p, PROCESS__GETSCHED);
+}
+
 static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
 				int sig, u32 secid)
 {
@@ -6092,6 +6102,8 @@ static struct security_hook_list selinux_hooks[] = {
 	LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
 	LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
 	LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
+	LSM_HOOK_INIT(task_settimerslack, selinux_task_settimerslack),
+	LSM_HOOK_INIT(task_gettimerslack, selinux_task_gettimerslack),
 	LSM_HOOK_INIT(task_kill, selinux_task_kill),
 	LSM_HOOK_INIT(task_wait, selinux_task_wait),
 	LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
Signature · 1 line
1.9.1
Acked-by on v3 2/2
#4 lore raw

Re: [RFC][PATCH 2/2 v3] security: Add task_settimerslack/task_gettimerslack LSM hook

In reply to John Stultz
Quoted 7 lines · John Stultz

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

Yeah, I think this does make it more readable in the end.

Quoted 13 lines

Don't really know what I'm doing here, so close review would be appreciated!

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]>
Quoted 8 lines
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]>

-Kees

Quoted 175 lines

---
v2:
* Initial swing at adding settimerslack LSM hook
v3:
* Fix current/p switchup bug noted by NickK
* Add gettimerslack hook suggested by NickK

 fs/proc/base.c            | 10 ++++++++++
 include/linux/lsm_hooks.h | 13 +++++++++++++
 include/linux/security.h  | 12 ++++++++++++
 security/security.c       | 14 ++++++++++++++
 security/selinux/hooks.c  | 12 ++++++++++++
 5 files changed, 61 insertions(+)
fs/proc/base.c +10 -0
index c94abae..cc66aa8 100644
@@ -2286,6 +2286,12 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
                goto out;
        }
 
+       err = security_task_settimerslack(p, slack_ns);
+       if (err) {
+               count = err;
+               goto out;
+       }
+
        task_lock(p);
        if (slack_ns == 0)
                p->timer_slack_ns = p->default_timer_slack_ns;
@@ -2314,6 +2320,10 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
                goto out;
        }
 
+       ret = security_task_gettimerslack(p);
+       if (ret)
+               goto out;
+
        task_lock(p);
        seq_printf(m, "%llu\n", p->timer_slack_ns);
        task_unlock(p);
include/linux/lsm_hooks.h +13 -0
index 7ae3976..290483e 100644
@@ -627,6 +627,15 @@
  *     Check permission before moving memory owned by process @p.
  *     @p contains the task_struct for process.
  *     Return 0 if permission is granted.
+ * @task_settimerslack:
+ *     Check permission before setting timerslack value of @p to @slack.
+ *     @p contains the task_struct of a process.
+ *     @slack contains the new slack value.
+ *     Return 0 if permission is granted.
+ * @task_gettimerslack:
+ *     Check permission before returning the timerslack value of @p.
+ *     @p contains the task_struct of a process.
+ *     Return 0 if permission is granted.
  * @task_kill:
  *     Check permission before sending signal @sig to @p.  @info can be NULL,
  *     the constant 1, or a pointer to a siginfo structure.  If @info is 1 or
@@ -1473,6 +1482,8 @@ union security_list_options {
        int (*task_setscheduler)(struct task_struct *p);
        int (*task_getscheduler)(struct task_struct *p);
        int (*task_movememory)(struct task_struct *p);
+       int (*task_settimerslack)(struct task_struct *p, u64 slack);
+       int (*task_gettimerslack)(struct task_struct *p);
        int (*task_kill)(struct task_struct *p, struct siginfo *info,
                                int sig, u32 secid);
        int (*task_wait)(struct task_struct *p);
@@ -1732,6 +1743,8 @@ struct security_hook_heads {
        struct list_head task_setscheduler;
        struct list_head task_getscheduler;
        struct list_head task_movememory;
+       struct list_head task_settimerslack;
+       struct list_head task_gettimerslack;
        struct list_head task_kill;
        struct list_head task_wait;
        struct list_head task_prctl;
include/linux/security.h +12 -0
index 14df373..ab70f47 100644
@@ -325,6 +325,8 @@ int security_task_setrlimit(struct task_struct *p, unsigned int resource,
 int security_task_setscheduler(struct task_struct *p);
 int security_task_getscheduler(struct task_struct *p);
 int security_task_movememory(struct task_struct *p);
+int security_task_settimerslack(struct task_struct *p, u64 slack);
+int security_task_gettimerslack(struct task_struct *p);
 int security_task_kill(struct task_struct *p, struct siginfo *info,
                        int sig, u32 secid);
 int security_task_wait(struct task_struct *p);
@@ -950,6 +952,16 @@ static inline int security_task_movememory(struct task_struct *p)
        return 0;
 }
 
+static inline int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+       return 0;
+}
+
+static inline int security_task_gettimerslack(struct task_struct *p)
+{
+       return 0;
+}
+
 static inline int security_task_kill(struct task_struct *p,
                                     struct siginfo *info, int sig,
                                     u32 secid)
security/security.c +14 -0
index 7095693..4ced236 100644
@@ -977,6 +977,16 @@ int security_task_movememory(struct task_struct *p)
        return call_int_hook(task_movememory, 0, p);
 }
 
+int security_task_settimerslack(struct task_struct *p, u64 slack)
+{
+       return call_int_hook(task_settimerslack, 0, p, slack);
+}
+
+int security_task_gettimerslack(struct task_struct *p)
+{
+       return call_int_hook(task_gettimerslack, 0, p);
+}
+
 int security_task_kill(struct task_struct *p, struct siginfo *info,
                        int sig, u32 secid)
 {
@@ -1720,6 +1730,10 @@ struct security_hook_heads security_hook_heads = {
                LIST_HEAD_INIT(security_hook_heads.task_getscheduler),
        .task_movememory =
                LIST_HEAD_INIT(security_hook_heads.task_movememory),
+       .task_settimerslack =
+               LIST_HEAD_INIT(security_hook_heads.task_settimerslack),
+       .task_gettimerslack =
+               LIST_HEAD_INIT(security_hook_heads.task_gettimerslack),
        .task_kill =    LIST_HEAD_INIT(security_hook_heads.task_kill),
        .task_wait =    LIST_HEAD_INIT(security_hook_heads.task_wait),
        .task_prctl =   LIST_HEAD_INIT(security_hook_heads.task_prctl),
security/selinux/hooks.c +12 -1
index a86d537..aac9599 100644
@@ -3849,6 +3849,16 @@ static int selinux_task_movememory(struct task_struct *p)
        return current_has_perm(p, PROCESS__SETSCHED);
 }
 
+static int selinux_task_settimerslack(struct task_struct *p, u64 slack)
+{
+       return current_has_perm(p, PROCESS__SETSCHED);
+}
+
+static int selinux_task_gettimerslack(struct task_struct *p)
+{
+       return current_has_perm(p, PROCESS__GETSCHED);
+}
+
 static int selinux_task_kill(struct task_struct *p, struct siginfo *info,
                                int sig, u32 secid)
 {
@@ -6092,6 +6102,8 @@ static struct security_hook_list selinux_hooks[] = {
        LSM_HOOK_INIT(task_setscheduler, selinux_task_setscheduler),
        LSM_HOOK_INIT(task_getscheduler, selinux_task_getscheduler),
        LSM_HOOK_INIT(task_movememory, selinux_task_movememory),
+       LSM_HOOK_INIT(task_settimerslack, selinux_task_settimerslack),
+       LSM_HOOK_INIT(task_gettimerslack, selinux_task_gettimerslack),
        LSM_HOOK_INIT(task_kill, selinux_task_kill),
        LSM_HOOK_INIT(task_wait, selinux_task_wait),
        LSM_HOOK_INIT(task_to_inode, selinux_task_to_inode),
--

1.9.1

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security
maintainer
#5 lore raw

Re: [RFC][PATCH 2/2 v3] security: Add task_settimerslack/task_gettimerslack LSM hook

In reply to John Stultz
Quoted 8 lines · John Stultz

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

I may have missed something in the earlier discussion, but why do we need new LSM hooks here vs. calling the existing set/getscheduler hooks?

Signature · 2 lines
James Morris
<[email protected]>
#6 lore raw

Re: [RFC][PATCH 2/2 v3] security: Add task_settimerslack/task_gettimerslack LSM hook

Quoted 6 lines · James Morris
Quoted 4 lines · John Stultz

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

Quoted 7 lines
Quoted 4 lines

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

I may have missed something in the earlier discussion, but why do we need new LSM hooks here vs. calling the existing set/getscheduler hooks?

Mostly since adding a new hook was suggested originally. I don't think there's much difference as it stands, but I guess more fine grained checks could be added on the slack amounts, etc.

I can rework it, so let me know if using the existing hooks would be preferred, but otherwise I'll be sending out the non-rfc patches tomorrow.

thanks
-john

maintainer
#7 lore raw

Re: [RFC][PATCH 2/2 v3] security: Add task_settimerslack/task_gettimerslack LSM hook

Quoted 15 lines · John Stultz
Quoted 6 lines · James Morris
Quoted 4 lines · John Stultz

As requested, this patch implements a task_settimerslack and task_gettimerslack LSM hooks so that the /proc/<tid>/timerslack_ns interface can have finer grained security policies applied to it.

Quoted 7 lines
Quoted 4 lines

I've kept the CAP_SYS_NICE check in the timerslack_ns_write/show functions, as hiding it in the LSM hook seems too opaque, and doesn't seem like a widely enough adopted practice.

I may have missed something in the earlier discussion, but why do we need new LSM hooks here vs. calling the existing set/getscheduler hooks?

Quoted 7 lines

Mostly since adding a new hook was suggested originally. I don't think there's much difference as it stands, but I guess more fine grained checks could be added on the slack amounts, etc.

I can rework it, so let me know if using the existing hooks would be preferred, but otherwise I'll be sending out the non-rfc patches tomorrow.

I'd prefer to re-use the existing hooks, unless there is a specific need for the extra granularity.

Signature · 2 lines
James Morris
<[email protected]>
Reviewed-by on v3 1/2
#8 lore raw
In reply to John Stultz
Quoted 22 lines · John Stultz

When an interface to allow a task to change another tasks timerslack was first proposed, it was suggested that something greater then CAP_SYS_NICE would be needed, as a task could be delayed further then what normally could be done with nice adjustments.

So CAP_SYS_PTRACE was adopted instead for what became the /proc/<tid>/timerslack_ns interface. However, for Android (where this feature originates), giving the system_server CAP_SYS_PTRACE would allow it to observe and modify all tasks memory. This is considered too high a privilege level for only needing to change the timerslack.

After some discussion, it was realized that a CAP_SYS_NICE process can set a task as SCHED_FIFO, so they could fork some spinning processes and set them all SCHED_FIFO 99, in effect delaying all other tasks for an infinite amount of time.

So as a CAP_SYS_NICE task can already cause trouble for other tasks, using it as a required capability for accessing and modifying /proc/<tid>/timerslack_ns seems sufficient.

Quoted 6 lines

Thus, this patch loosens the capability requirements to CAP_SYS_NICE and removes CAP_SYS_PTRACE, simplifying some of the code flow as well.

This is technically an ABI change, but as the feature just landed in 4.6, I suspect no one is yet using it.

Looks good. Thanks!

Quoted 87 lines
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]>

---
v2: Removed CAP_SYS_PTRACE check and simplified code flow
v3: Tweaked where CAP_SYS_NICE check is made, suggeded by NickK

 fs/proc/base.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)
fs/proc/base.c +20 -15
index a11eb71..c94abae 100644
@@ -2281,16 +2281,19 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-               task_lock(p);
-               if (slack_ns == 0)
-                       p->timer_slack_ns = p->default_timer_slack_ns;
-               else
-                       p->timer_slack_ns = slack_ns;
-               task_unlock(p);
-       } else
+       if (!capable(CAP_SYS_NICE)) {
                count = -EPERM;
+               goto out;
+       }
+
+       task_lock(p);
+       if (slack_ns == 0)
+               p->timer_slack_ns = p->default_timer_slack_ns;
+       else
+               p->timer_slack_ns = slack_ns;
+       task_unlock(p);
 
+out:
        put_task_struct(p);
 
        return count;
@@ -2300,19 +2303,22 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 {
        struct inode *inode = m->private;
        struct task_struct *p;
-       int err =  0;
+       int err = 0;
 
        p = get_proc_task(inode);
        if (!p)
                return -ESRCH;
 
-       if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-               task_lock(p);
-               seq_printf(m, "%llu\n", p->timer_slack_ns);
-               task_unlock(p);
-       } else
+       if (!capable(CAP_SYS_NICE)) {
                err = -EPERM;
+               goto out;
+       }
 
+       task_lock(p);
+       seq_printf(m, "%llu\n", p->timer_slack_ns);
+       task_unlock(p);
+
+out:
        put_task_struct(p);
 
        return err;
--

1.9.1

Signature · 1 line
Nick Kralevich | Android Security | [email protected] | 650.214.4037
maintainer Acked-by on v3 1/2
#9 lore raw
In reply to John Stultz
Quoted 26 lines · John Stultz

When an interface to allow a task to change another tasks timerslack was first proposed, it was suggested that something greater then CAP_SYS_NICE would be needed, as a task could be delayed further then what normally could be done with nice adjustments.

So CAP_SYS_PTRACE was adopted instead for what became the /proc/<tid>/timerslack_ns interface. However, for Android (where this feature originates), giving the system_server CAP_SYS_PTRACE would allow it to observe and modify all tasks memory. This is considered too high a privilege level for only needing to change the timerslack.

After some discussion, it was realized that a CAP_SYS_NICE process can set a task as SCHED_FIFO, so they could fork some spinning processes and set them all SCHED_FIFO 99, in effect delaying all other tasks for an infinite amount of time.

So as a CAP_SYS_NICE task can already cause trouble for other tasks, using it as a required capability for accessing and modifying /proc/<tid>/timerslack_ns seems sufficient.

Thus, this patch loosens the capability requirements to CAP_SYS_NICE and removes CAP_SYS_PTRACE, simplifying some of the code flow as well.

Quoted 5 lines

This is technically an ABI change, but as the feature just landed in 4.6, I suspect no one is yet using it.

Cc Kees Cook <[email protected]>
Cc "Serge E. Hallyn" <[email protected]>
Quoted 83 lines
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]>

---
v2: Removed CAP_SYS_PTRACE check and simplified code flow
v3: Tweaked where CAP_SYS_NICE check is made, suggeded by NickK

 fs/proc/base.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)
fs/proc/base.c +20 -14
index a11eb71..c94abae 100644
@@ -2281,16 +2281,19 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
 	if (!p)
 		return -ESRCH;
 
-	if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-		task_lock(p);
-		if (slack_ns == 0)
-			p->timer_slack_ns = p->default_timer_slack_ns;
-		else
-			p->timer_slack_ns = slack_ns;
-		task_unlock(p);
-	} else
+	if (!capable(CAP_SYS_NICE)) {
 		count = -EPERM;
+		goto out;
+	}
+
+	task_lock(p);
+	if (slack_ns == 0)
+		p->timer_slack_ns = p->default_timer_slack_ns;
+	else
+		p->timer_slack_ns = slack_ns;
+	task_unlock(p);
 
+out:
 	put_task_struct(p);
 
 	return count;
@@ -2300,19 +2303,22 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 {
 	struct inode *inode = m->private;
 	struct task_struct *p;
-	int err =  0;
+	int err = 0;
 
 	p = get_proc_task(inode);
 	if (!p)
 		return -ESRCH;
 
-	if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-		task_lock(p);
-		seq_printf(m, "%llu\n", p->timer_slack_ns);
-		task_unlock(p);
-	} else
+	if (!capable(CAP_SYS_NICE)) {
 		err = -EPERM;
+		goto out;
+	}
 
+	task_lock(p);
+	seq_printf(m, "%llu\n", p->timer_slack_ns);
+	task_unlock(p);
+
+out:
 	put_task_struct(p);
 
 	return err;
Signature · 1 line
1.9.1
Acked-by on v3 1/2
#10 lore raw
In reply to John Stultz
Quoted 29 lines · John Stultz

When an interface to allow a task to change another tasks timerslack was first proposed, it was suggested that something greater then CAP_SYS_NICE would be needed, as a task could be delayed further then what normally could be done with nice adjustments.

So CAP_SYS_PTRACE was adopted instead for what became the /proc/<tid>/timerslack_ns interface. However, for Android (where this feature originates), giving the system_server CAP_SYS_PTRACE would allow it to observe and modify all tasks memory. This is considered too high a privilege level for only needing to change the timerslack.

After some discussion, it was realized that a CAP_SYS_NICE process can set a task as SCHED_FIFO, so they could fork some spinning processes and set them all SCHED_FIFO 99, in effect delaying all other tasks for an infinite amount of time.

So as a CAP_SYS_NICE task can already cause trouble for other tasks, using it as a required capability for accessing and modifying /proc/<tid>/timerslack_ns seems sufficient.

Thus, this patch loosens the capability requirements to CAP_SYS_NICE and removes CAP_SYS_PTRACE, simplifying some of the code flow as well.

This is technically an ABI change, but as the feature just landed in 4.6, I suspect no one is yet using it.

Quoted 15 lines
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]>

-Kees

Quoted 71 lines

---
v2: Removed CAP_SYS_PTRACE check and simplified code flow
v3: Tweaked where CAP_SYS_NICE check is made, suggeded by NickK

 fs/proc/base.c | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)
fs/proc/base.c +20 -15
index a11eb71..c94abae 100644
@@ -2281,16 +2281,19 @@ static ssize_t timerslack_ns_write(struct file *file, const char __user *buf,
        if (!p)
                return -ESRCH;
 
-       if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-               task_lock(p);
-               if (slack_ns == 0)
-                       p->timer_slack_ns = p->default_timer_slack_ns;
-               else
-                       p->timer_slack_ns = slack_ns;
-               task_unlock(p);
-       } else
+       if (!capable(CAP_SYS_NICE)) {
                count = -EPERM;
+               goto out;
+       }
+
+       task_lock(p);
+       if (slack_ns == 0)
+               p->timer_slack_ns = p->default_timer_slack_ns;
+       else
+               p->timer_slack_ns = slack_ns;
+       task_unlock(p);
 
+out:
        put_task_struct(p);
 
        return count;
@@ -2300,19 +2303,22 @@ static int timerslack_ns_show(struct seq_file *m, void *v)
 {
        struct inode *inode = m->private;
        struct task_struct *p;
-       int err =  0;
+       int err = 0;
 
        p = get_proc_task(inode);
        if (!p)
                return -ESRCH;
 
-       if (ptrace_may_access(p, PTRACE_MODE_ATTACH_FSCREDS)) {
-               task_lock(p);
-               seq_printf(m, "%llu\n", p->timer_slack_ns);
-               task_unlock(p);
-       } else
+       if (!capable(CAP_SYS_NICE)) {
                err = -EPERM;
+               goto out;
+       }
 
+       task_lock(p);
+       seq_printf(m, "%llu\n", p->timer_slack_ns);
+       task_unlock(p);
+
+out:
        put_task_struct(p);
 
        return err;
--

1.9.1

Signature · 2 lines
Kees Cook
Chrome OS & Brillo Security