Skip to content

Commit 77b4b54

Browse files
committed
posix-cpu-timers: Fix permission check regression
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 <[email protected]> Reviewed-by: Frederic Weisbecker <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 00d9e47 commit 77b4b54

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

kernel/time/posix-cpu-timers.c

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,46 @@ void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
4747
/*
4848
* Functions for validating access to tasks.
4949
*/
50-
static struct task_struct *lookup_task(const pid_t pid, bool thread)
50+
static struct task_struct *lookup_task(const pid_t pid, bool thread,
51+
bool gettime)
5152
{
5253
struct task_struct *p;
5354

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

5762
p = find_task_by_vpid(pid);
58-
if (!p || p == current)
63+
if (!p)
5964
return p;
65+
6066
if (thread)
6167
return same_thread_group(p, current) ? p : NULL;
62-
if (p == current)
63-
return p;
68+
69+
if (gettime) {
70+
/*
71+
* For clock_gettime(PROCESS) the task does not need to be
72+
* the actual group leader. tsk->sighand gives
73+
* access to the group's clock.
74+
*
75+
* Timers need the group leader because they take a
76+
* reference on it and store the task pointer until the
77+
* timer is destroyed.
78+
*/
79+
return (p == current || thread_group_leader(p)) ? p : NULL;
80+
}
81+
82+
/*
83+
* For processes require that p is group leader.
84+
*/
6485
return has_group_leader_pid(p) ? p : NULL;
6586
}
6687

6788
static struct task_struct *__get_task_for_clock(const clockid_t clock,
68-
bool getref)
89+
bool getref, bool gettime)
6990
{
7091
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
7192
const pid_t pid = CPUCLOCK_PID(clock);
@@ -75,7 +96,7 @@ static struct task_struct *__get_task_for_clock(const clockid_t clock,
7596
return NULL;
7697

7798
rcu_read_lock();
78-
p = lookup_task(pid, thread);
99+
p = lookup_task(pid, thread, gettime);
79100
if (p && getref)
80101
get_task_struct(p);
81102
rcu_read_unlock();
@@ -84,12 +105,17 @@ static struct task_struct *__get_task_for_clock(const clockid_t clock,
84105

85106
static inline struct task_struct *get_task_for_clock(const clockid_t clock)
86107
{
87-
return __get_task_for_clock(clock, true);
108+
return __get_task_for_clock(clock, true, false);
109+
}
110+
111+
static inline struct task_struct *get_task_for_clock_get(const clockid_t clock)
112+
{
113+
return __get_task_for_clock(clock, true, true);
88114
}
89115

90116
static inline int validate_clock_permissions(const clockid_t clock)
91117
{
92-
return __get_task_for_clock(clock, false) ? 0 : -EINVAL;
118+
return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL;
93119
}
94120

95121
/*
@@ -339,7 +365,7 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
339365
struct task_struct *tsk;
340366
u64 t;
341367

342-
tsk = get_task_for_clock(clock);
368+
tsk = get_task_for_clock_get(clock);
343369
if (!tsk)
344370
return -EINVAL;
345371

0 commit comments

Comments
 (0)