Skip to content

Commit 2dd8083

Browse files
committed
posix-cpu-timers: Use pids not tasks in lookup
The current posix-cpu-timer code uses pids when holding persistent references in timers. However the lookups from clock_id_t still return tasks that need to be converted into pids for use. This results in usage being pid->task->pid and that can race with release_task and de_thread. This can lead to some not wrong but surprising results. Surprising enough that Oleg and I both thought there were some bugs in the code for a while. This set of changes modifies the code to just lookup, verify, and return pids from the clockid_t lookups to remove those potentialy troublesome races. Eric W. Biederman (3): posix-cpu-timers: Extend rcu_read_lock removing task_struct references posix-cpu-timers: Replace cpu_timer_pid_type with clock_pid_type posix-cpu-timers: Replace __get_task_for_clock with pid_for_clock kernel/time/posix-cpu-timers.c | 102 ++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 57 deletions(-) Suggested-by: Oleg Nesterov <[email protected]> Signed-off-by: "Eric W. Biederman" <[email protected]>
2 parents 1dd694a + 9649877 commit 2dd8083

File tree

1 file changed

+45
-57
lines changed

1 file changed

+45
-57
lines changed

kernel/time/posix-cpu-timers.c

Lines changed: 45 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -47,80 +47,65 @@ 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,
51-
bool gettime)
50+
static struct pid *pid_for_clock(const clockid_t clock, bool gettime)
5251
{
53-
struct task_struct *p;
52+
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
53+
const pid_t upid = CPUCLOCK_PID(clock);
54+
struct pid *pid;
55+
56+
if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
57+
return NULL;
5458

5559
/*
5660
* If the encoded PID is 0, then the timer is targeted at current
5761
* or the process to which current belongs.
5862
*/
59-
if (!pid)
60-
return thread ? current : current->group_leader;
63+
if (upid == 0)
64+
return thread ? task_pid(current) : task_tgid(current);
6165

62-
p = find_task_by_vpid(pid);
63-
if (!p)
64-
return p;
66+
pid = find_vpid(upid);
67+
if (!pid)
68+
return NULL;
6569

66-
if (thread)
67-
return same_thread_group(p, current) ? p : NULL;
70+
if (thread) {
71+
struct task_struct *tsk = pid_task(pid, PIDTYPE_PID);
72+
return (tsk && same_thread_group(tsk, current)) ? pid : NULL;
73+
}
6874

6975
/*
70-
* For clock_gettime(PROCESS) the task does not need to be
71-
* the actual group leader. task->signal gives
72-
* access to the group's clock.
76+
* For clock_gettime(PROCESS) allow finding the process by
77+
* with the pid of the current task. The code needs the tgid
78+
* of the process so that pid_task(pid, PIDTYPE_TGID) can be
79+
* used to find the process.
7380
*/
74-
if (gettime && (p == current))
75-
return p;
81+
if (gettime && (pid == task_pid(current)))
82+
return task_tgid(current);
7683

7784
/*
78-
* For processes require that p is group leader.
85+
* For processes require that pid identifies a process.
7986
*/
80-
return thread_group_leader(p) ? p : NULL;
87+
return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL;
8188
}
8289

83-
static struct task_struct *__get_task_for_clock(const clockid_t clock,
84-
bool getref, bool gettime)
90+
static inline int validate_clock_permissions(const clockid_t clock)
8591
{
86-
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
87-
const pid_t pid = CPUCLOCK_PID(clock);
88-
struct task_struct *p;
89-
90-
if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
91-
return NULL;
92+
int ret;
9293

9394
rcu_read_lock();
94-
p = lookup_task(pid, thread, gettime);
95-
if (p && getref)
96-
get_task_struct(p);
95+
ret = pid_for_clock(clock, false) ? 0 : -EINVAL;
9796
rcu_read_unlock();
98-
return p;
99-
}
10097

101-
static inline struct task_struct *get_task_for_clock(const clockid_t clock)
102-
{
103-
return __get_task_for_clock(clock, true, false);
104-
}
105-
106-
static inline struct task_struct *get_task_for_clock_get(const clockid_t clock)
107-
{
108-
return __get_task_for_clock(clock, true, true);
109-
}
110-
111-
static inline int validate_clock_permissions(const clockid_t clock)
112-
{
113-
return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL;
98+
return ret;
11499
}
115100

116-
static inline enum pid_type cpu_timer_pid_type(struct k_itimer *timer)
101+
static inline enum pid_type clock_pid_type(const clockid_t clock)
117102
{
118-
return CPUCLOCK_PERTHREAD(timer->it_clock) ? PIDTYPE_PID : PIDTYPE_TGID;
103+
return CPUCLOCK_PERTHREAD(clock) ? PIDTYPE_PID : PIDTYPE_TGID;
119104
}
120105

121106
static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer)
122107
{
123-
return pid_task(timer->it.cpu.pid, cpu_timer_pid_type(timer));
108+
return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock));
124109
}
125110

126111
/*
@@ -368,15 +353,18 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
368353
struct task_struct *tsk;
369354
u64 t;
370355

371-
tsk = get_task_for_clock_get(clock);
372-
if (!tsk)
356+
rcu_read_lock();
357+
tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock));
358+
if (!tsk) {
359+
rcu_read_unlock();
373360
return -EINVAL;
361+
}
374362

375363
if (CPUCLOCK_PERTHREAD(clock))
376364
t = cpu_clock_sample(clkid, tsk);
377365
else
378366
t = cpu_clock_sample_group(clkid, tsk, false);
379-
put_task_struct(tsk);
367+
rcu_read_unlock();
380368

381369
*tp = ns_to_timespec64(t);
382370
return 0;
@@ -389,19 +377,19 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
389377
*/
390378
static int posix_cpu_timer_create(struct k_itimer *new_timer)
391379
{
392-
struct task_struct *p = get_task_for_clock(new_timer->it_clock);
380+
struct pid *pid;
393381

394-
if (!p)
382+
rcu_read_lock();
383+
pid = pid_for_clock(new_timer->it_clock, false);
384+
if (!pid) {
385+
rcu_read_unlock();
395386
return -EINVAL;
387+
}
396388

397389
new_timer->kclock = &clock_posix_cpu;
398390
timerqueue_init(&new_timer->it.cpu.node);
399-
new_timer->it.cpu.pid = get_task_pid(p, cpu_timer_pid_type(new_timer));
400-
/*
401-
* get_task_for_clock() took a reference on @p. Drop it as the timer
402-
* holds a reference on the pid of @p.
403-
*/
404-
put_task_struct(p);
391+
new_timer->it.cpu.pid = get_pid(pid);
392+
rcu_read_unlock();
405393
return 0;
406394
}
407395

0 commit comments

Comments
 (0)