[patch 3/6] posix-cpu-timers: Restrict timer_create() permissions
From: Thomas Gleixner
Date: Thu Sep 05 2019 - 08:12:08 EST
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>
---
kernel/time/posix-cpu-timers.c | 18 +++++++++++++++++-
1 file changed, 17 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>
@@ -78,7 +79,22 @@ 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:
+ */
+ if (!ptrace_may_access(p, PTRACE_MODE_ATTACH_REALCREDS))
+ return NULL;
+ return p;
}
static struct task_struct *__get_task_for_clock(const clockid_t clock,