From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Frederic Weisbecker <fweisbec@gmail.com>,
Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>, Oleg Nesterov <oleg@redhat.com>,
Michael Kerrisk <mtk.manpages@googlemail.com>,
Kees Cook <keescook@chromium.org>,
Frederic Weisbecker <frederic@kernel.org>
Subject: [patch V2 3/6] posix-cpu-timers: Sanitize thread clock permissions
Date: Mon, 23 Sep 2019 16:54:38 +0200 [thread overview]
Message-ID: <20190923145528.672384201@linutronix.de> (raw)
In-Reply-To: 20190923145435.507024424@linutronix.de
From: Thomas Gleixner <tglx@linutronix.de>
The thread clock permissions are restricted to tasks of the same thread
group, but that also prevents a ptracer from reading them. This is
inconsistent vs. the process restrictions and unnecessary strict.
Relax it to ptrace permissions in the same way as process permissions are
handled.
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
---
kernel/time/posix-cpu-timers.c | 66 +++++++++++++++++++----------------------
1 file changed, 32 insertions(+), 34 deletions(-)
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -51,6 +51,7 @@ void update_rlimit_cpu(struct task_struc
static struct task_struct *lookup_task(const pid_t pid, bool thread,
bool gettime)
{
+ unsigned int mode = PTRACE_MODE_ATTACH_REALCREDS;
struct task_struct *p;
/*
@@ -64,50 +65,47 @@ static struct task_struct *lookup_task(c
if (!p)
return p;
- if (thread)
- return same_thread_group(p, current) ? p : NULL;
-
if (gettime) {
/*
- * For clock_gettime(PROCESS) the task does not need to be
- * the actual group leader. tsk->sighand gives
- * access to the group's clock.
- *
- * Timers need the group leader because they take a
- * reference on it and store the task pointer until the
- * timer is destroyed.
+ * For clock_gettime() the task does not need to be the
+ * actual group leader. tsk->sighand gives access to the
+ * group's clock.
*
- * current can obviously access it's own process, so spare
- * the ptrace check below.
+ * The trivial case is that p is current or in the same
+ * thread group, i.e. sharing p->signal. Spare the ptrace
+ * check in that case.
*/
- if (p == current)
+ if (same_thread_group(p, current))
return p;
- if (!thread_group_leader(p))
- return NULL;
+ mode = PTRACE_MODE_READ_REALCREDS;
- if (!ptrace_may_access(p, PTRACE_MODE_READ_REALCREDS))
- return NULL;
- return p;
- }
+ } else if (thread) {
+ /*
+ * Timer is going to be attached to a thread. If p is
+ * current or in the same thread group, granted.
+ */
+ if (same_thread_group(p, current))
+ return p;
- /*
- * For processes require that p is group leader.
- */
- if (!has_group_leader_pid(p))
- return NULL;
+ } else {
+ /*
+ * Process wide timers need the group leader because they
+ * take a reference on it and store the task pointer until
+ * the timer is destroyed.
+ */
+ 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;
+ /*
+ * 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;
+ /* Decide based on the ptrace permissions. */
+ return ptrace_may_access(p, mode) ? p : NULL;
}
static struct task_struct *__get_task_for_clock(const clockid_t clock,
next prev parent reply other threads:[~2019-09-23 14:56 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-09-23 14:54 [patch V2 0/6] posix-cpu-timers: Fix bogus permission checks Thomas Gleixner
2019-09-23 14:54 ` [patch V2 1/6] posix-cpu-timers: Restrict timer_create() permissions Thomas Gleixner
2019-09-23 14:54 ` [patch V2 2/6] posix-cpu-timers: Restrict clock_gettime() permissions Thomas Gleixner
2019-09-23 14:54 ` Thomas Gleixner [this message]
2019-09-23 14:54 ` [patch V2 4/6] posix-cpu-timers: Make PID=0 and PID=self handling consistent Thomas Gleixner
2019-09-23 14:54 ` [patch V2 5/6] posix-cpu-timers: Return PTR_ERR() from lookup_task() Thomas Gleixner
2019-09-23 15:43 ` Frederic Weisbecker
2019-09-23 14:54 ` [patch V2 6/6] posix-cpu-timers: Return -EPERM if ptrace permission check fails Thomas Gleixner
2019-09-23 15:46 ` Frederic Weisbecker
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190923145528.672384201@linutronix.de \
--to=tglx@linutronix.de \
--cc=frederic@kernel.org \
--cc=fweisbec@gmail.com \
--cc=keescook@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=mtk.manpages@googlemail.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox