Skip to content

Commit d5b36a4

Browse files
oleg-nesterovtorvalds
authored andcommitted
fix race between exit_itimers() and /proc/pid/timers
As Chris explains, the comment above exit_itimers() is not correct, we can race with proc_timers_seq_ops. Change exit_itimers() to clear signal->posix_timers with ->siglock held. Cc: <[email protected]> Reported-by: [email protected] Signed-off-by: Oleg Nesterov <[email protected]> Signed-off-by: Linus Torvalds <[email protected]>
1 parent 3234649 commit d5b36a4

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

fs/exec.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ int begin_new_exec(struct linux_binprm * bprm)
13011301
bprm->mm = NULL;
13021302

13031303
#ifdef CONFIG_POSIX_TIMERS
1304-
exit_itimers(me->signal);
1304+
exit_itimers(me);
13051305
flush_itimer_signals();
13061306
#endif
13071307

include/linux/sched/task.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static inline void exit_thread(struct task_struct *tsk)
8585
extern __noreturn void do_group_exit(int);
8686

8787
extern void exit_files(struct task_struct *);
88-
extern void exit_itimers(struct signal_struct *);
88+
extern void exit_itimers(struct task_struct *);
8989

9090
extern pid_t kernel_clone(struct kernel_clone_args *kargs);
9191
struct task_struct *create_io_thread(int (*fn)(void *), void *arg, int node);

kernel/exit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ void __noreturn do_exit(long code)
766766

767767
#ifdef CONFIG_POSIX_TIMERS
768768
hrtimer_cancel(&tsk->signal->real_timer);
769-
exit_itimers(tsk->signal);
769+
exit_itimers(tsk);
770770
#endif
771771
if (tsk->mm)
772772
setmax_mm_hiwater_rss(&tsk->signal->maxrss, tsk->mm);

kernel/time/posix-timers.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,15 +1051,24 @@ static void itimer_delete(struct k_itimer *timer)
10511051
}
10521052

10531053
/*
1054-
* This is called by do_exit or de_thread, only when there are no more
1055-
* references to the shared signal_struct.
1054+
* This is called by do_exit or de_thread, only when nobody else can
1055+
* modify the signal->posix_timers list. Yet we need sighand->siglock
1056+
* to prevent the race with /proc/pid/timers.
10561057
*/
1057-
void exit_itimers(struct signal_struct *sig)
1058+
void exit_itimers(struct task_struct *tsk)
10581059
{
1060+
struct list_head timers;
10591061
struct k_itimer *tmr;
10601062

1061-
while (!list_empty(&sig->posix_timers)) {
1062-
tmr = list_entry(sig->posix_timers.next, struct k_itimer, list);
1063+
if (list_empty(&tsk->signal->posix_timers))
1064+
return;
1065+
1066+
spin_lock_irq(&tsk->sighand->siglock);
1067+
list_replace_init(&tsk->signal->posix_timers, &timers);
1068+
spin_unlock_irq(&tsk->sighand->siglock);
1069+
1070+
while (!list_empty(&timers)) {
1071+
tmr = list_first_entry(&timers, struct k_itimer, list);
10631072
itimer_delete(tmr);
10641073
}
10651074
}

0 commit comments

Comments
 (0)