Skip to content

Commit

Permalink
posix-cpu-timers: Fix permission check regression
Browse files Browse the repository at this point in the history
The recent consolidation of the three permission checks introduced a subtle
regression. For timer_create() with a process wide timer it returns the
current task if the lookup through the PID which is encoded into the
clockid results in returning current.

That's broken because it does not validate whether the current task is the
group leader.

That was caused by the two different variants of permission checks:

  - posix_cpu_timer_get() allowed access to the process wide clock when the
    looked up task is current. That's not an issue because the process wide
    clock is in the shared sighand.

  - posix_cpu_timer_create() made sure that the looked up task is the group
    leader.

Restore the previous state.

Note, that these permission checks are more than questionable, but that's
subject to follow up changes.

Fixes: 6ae40e3 ("posix-cpu-timers: Provide task validation functions")
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Frederic Weisbecker <frederic@kernel.org>
Link: https://lkml.kernel.org/r/alpine.DEB.2.21.1909052314110.1902@nanos.tec.linutronix.de
  • Loading branch information
Thomas Gleixner committed Sep 10, 2019
1 parent 00d9e47 commit 77b4b54
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions kernel/time/posix-cpu-timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,46 @@ void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
/*
* Functions for validating access to tasks.
*/
static struct task_struct *lookup_task(const pid_t pid, bool thread)
static struct task_struct *lookup_task(const pid_t pid, bool thread,
bool gettime)
{
struct task_struct *p;

/*
* If the encoded PID is 0, then the timer is targeted at current
* or the process to which current belongs.
*/
if (!pid)
return thread ? current : current->group_leader;

p = find_task_by_vpid(pid);
if (!p || p == current)
if (!p)
return p;

if (thread)
return same_thread_group(p, current) ? p : NULL;
if (p == current)
return p;

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.
*/
return (p == current || thread_group_leader(p)) ? p : NULL;
}

/*
* For processes require that p is group leader.
*/
return has_group_leader_pid(p) ? p : NULL;
}

static struct task_struct *__get_task_for_clock(const clockid_t clock,
bool getref)
bool getref, bool gettime)
{
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
const pid_t pid = CPUCLOCK_PID(clock);
Expand All @@ -75,7 +96,7 @@ static struct task_struct *__get_task_for_clock(const clockid_t clock,
return NULL;

rcu_read_lock();
p = lookup_task(pid, thread);
p = lookup_task(pid, thread, gettime);
if (p && getref)
get_task_struct(p);
rcu_read_unlock();
Expand All @@ -84,12 +105,17 @@ static struct task_struct *__get_task_for_clock(const clockid_t clock,

static inline struct task_struct *get_task_for_clock(const clockid_t clock)
{
return __get_task_for_clock(clock, true);
return __get_task_for_clock(clock, true, false);
}

static inline struct task_struct *get_task_for_clock_get(const clockid_t clock)
{
return __get_task_for_clock(clock, true, true);
}

static inline int validate_clock_permissions(const clockid_t clock)
{
return __get_task_for_clock(clock, false) ? 0 : -EINVAL;
return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL;
}

/*
Expand Down Expand Up @@ -339,7 +365,7 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
struct task_struct *tsk;
u64 t;

tsk = get_task_for_clock(clock);
tsk = get_task_for_clock_get(clock);
if (!tsk)
return -EINVAL;

Expand Down

0 comments on commit 77b4b54

Please sign in to comment.