[patch V2 1/6] posix-cpu-timers: Restrict timer_create() permissions

From: Thomas Gleixner
Date: Mon Sep 23 2019 - 10:56:48 EST


From: Thomas Gleixner <tglx@xxxxxxxxxxxxx>

Right now there is no restriction at all to attach a Posix CPU timer to any
process in the system. Per thread CPU timers are limited to be created by
threads in the same thread group.

Timers can be used to observe activity of tasks and also impose overhead on
the process to which they are attached because that process needs to do the
fine grained CPU time accounting.

Limit the ability to attach timers to a process by checking whether the
task which is creating the timer has permissions to attach ptrace on the
target process.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Reviewed-by: Frederic Weisbecker <frederic@xxxxxxxxxx>

---
kernel/time/posix-cpu-timers.c | 16 +++++++++++++++-
1 file changed, 15 insertions(+), 1 deletion(-)

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -6,6 +6,7 @@
#include <linux/sched/signal.h>
#include <linux/sched/cputime.h>
#include <linux/posix-timers.h>
+#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/math64.h>
#include <linux/uaccess.h>
@@ -82,7 +83,20 @@ static struct task_struct *lookup_task(c
/*
* For processes require that p is group leader.
*/
- return has_group_leader_pid(p) ? p : NULL;
+ if (!has_group_leader_pid(p))
+ return NULL;
+
+ /*
+ * Avoid the ptrace overhead when this is current's process
+ */
+ if (same_thread_group(p, current))
+ return p;
+
+ /*
+ * Creating timers on processes which cannot be ptraced is not
+ * permitted:
+ */
+ return ptrace_may_access(p, PTRACE_MODE_ATTACH_REALCREDS) ? p : NULL;
}

static struct task_struct *__get_task_for_clock(const clockid_t clock,