[patch 4/6] posix-cpu-timers: Restrict clock_gettime() permissions

From: Thomas Gleixner
Date: Thu Sep 05 2019 - 08:12:12 EST


Similar to creating timers on a process there is no restriction at all to
read the Posix CPU clocks of any process in the system. Per thread CPU
clock access is limited to threads in the same thread group.

The per process CPU clocks can be used to observe activity of tasks and
reading them can affect the execution of the process to which they are
attached as reading can require to lock sighand lock and sum up the fine
grained accounting for all threads in the process.

Restrict it by checking ptrace MODE_READ permissions of the reader on the
target process.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
---
kernel/time/posix-cpu-timers.c | 17 ++++++++++++-----
1 file changed, 12 insertions(+), 5 deletions(-)

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -71,9 +71,18 @@ static struct task_struct *lookup_task(c
/*
* For clock_gettime() the task does not need to be the
* actual group leader. tsk->sighand gives access to the
- * group's clock.
+ * group's clock. current can obviously access itself, so
+ * spare the ptrace check below.
*/
- return (p == current || thread_group_leader(p)) ? p : NULL;
+ if (p == current)
+ return p;
+
+ if (!thread_group_leader(p))
+ return NULL;
+
+ if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
+ return NULL;
+ return p;
}

/*
@@ -92,9 +101,7 @@ static struct task_struct *lookup_task(c
* Creating timers on processes which cannot be ptraced is not
* permitted:
*/
- if (!ptrace_may_access(p, PTRACE_MODE_ATTACH_REALCREDS))
- return NULL;
- return p;
+ return ptrace_may_access(p, PTRACE_MODE_ATTACH_REALCREDS) ? p : NULL;
}

static struct task_struct *__get_task_for_clock(const clockid_t clock,