[PATCH 3/4] posix-cpu-timers: Wider access to the thread clocks

From: Tomasz Buchert
Date: Fri Jul 30 2010 - 05:59:19 EST


Per-thread clocks are so far accessible only within
the same thread group. This patch makes it possible to get
this information for any thread of the same user and for
a user with CAP_SYS_ADMIN capability.

same_thread_group is not used anymore because if the thread is in
the same thread group as 'current' then they will have the same
owner anyway.

Signed-off-by: Tomasz Buchert <tomasz.buchert@xxxxxxxx>
Signed-off-by: Lucas Nussbaum <lucas.nussbaum@xxxxxxxx>
---
kernel/posix-cpu-timers.c | 34 +++++++++++++++++++++++++++-------
1 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/kernel/posix-cpu-timers.c b/kernel/posix-cpu-timers.c
index 78ad536..04f49ce 100644
--- a/kernel/posix-cpu-timers.c
+++ b/kernel/posix-cpu-timers.c
@@ -25,6 +25,26 @@ void update_rlimit_cpu(unsigned long rlim_new)
spin_unlock_irq(&current->sighand->siglock);
}

+/* RCU lock needed */
+static int thread_clock_allowed(struct task_struct *p)
+{
+ const struct cred *cred, *pcred;
+
+ if (unlikely(capable(CAP_SYS_ADMIN)))
+ return 1;
+ cred = current_cred();
+ pcred = __task_cred(p);
+
+ return (cred->euid == pcred->euid ||
+ cred->euid == pcred->uid);
+}
+
+/* RCU read lock needed */
+static inline int process_clock_allowed(struct task_struct *p)
+{
+ return thread_group_leader(p);
+}
+
static int check_task_clock(const clockid_t which_clock)
{
int error = 0;
@@ -37,7 +57,7 @@ static int check_task_clock(const clockid_t which_clock)
rcu_read_lock();
p = find_task_by_vpid(pid);
if (!p || !(POSIX_CLOCK_PERTHREAD(which_clock) ?
- same_thread_group(p, current) : thread_group_leader(p))) {
+ thread_clock_allowed(p) : process_clock_allowed(p))) {
error = -EINVAL;
}
rcu_read_unlock();
@@ -68,9 +88,9 @@ static int get_start_time(clockid_t which_clock, struct timespec *start)
find_task_by_vpid(POSIX_CLOCK_PID(which_clock));
if (p && (
(POSIX_CLOCK_PERTHREAD(which_clock) &&
- same_thread_group(p, current)) ||
+ thread_clock_allowed(p)) ||
(!POSIX_CLOCK_PERTHREAD(which_clock) &&
- thread_group_leader(p) && p->sighand))) {
+ process_clock_allowed(p) && p->sighand))) {
*start = p->start_time;
return 0;
}
@@ -529,13 +549,13 @@ int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
p = find_task_by_vpid(pid);
if (p) {
if (POSIX_CLOCK_PERTHREAD(which_clock)) {
- if (same_thread_group(p, current)) {
+ if (thread_clock_allowed(p)) {
error = cpu_clock_sample(which_clock,
p, &rtn);
}
} else {
read_lock(&tasklist_lock);
- if (thread_group_leader(p) && p->sighand) {
+ if (process_clock_allowed(p) && p->sighand) {
error =
cpu_clock_sample_group(which_clock,
p, &rtn);
@@ -575,7 +595,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
p = current;
} else {
p = find_task_by_vpid(pid);
- if (p && !same_thread_group(p, current))
+ if (p && !thread_clock_allowed(p))
p = NULL;
}
} else {
@@ -583,7 +603,7 @@ int posix_cpu_timer_create(struct k_itimer *new_timer)
p = current->group_leader;
} else {
p = find_task_by_vpid(pid);
- if (p && !thread_group_leader(p))
+ if (p && !process_clock_allowed(p))
p = NULL;
}
}
--
1.6.3.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/