Skip to contentSkip to conversation

posix-timers: fix creation race

PATCH In review Oleg Nesterov 2 participants 5 messages Aug 12, 2007 – Aug 13, 2007

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 posix-timers: fix creation race

sys_timer_create() sets ->it_process and unlocks ->siglock, then checks tmr->it_sigev_notify to define if get_task_struct() is needed.

We already passed ->it_id to the caller, another thread can delete this timer and free its memory in between.

As a minimal fix, move this code under ->siglock, sys_timer_delete() takes it too before calling release_posix_timer(). A proper serialization would be to take ->it_lock, we add a partly initialized timer on posix_timers_id, not good.

kernel/posix-timers.c renamed from kernel/posix-timers.c~2_CREATE +1 -2
@@ -547,13 +547,12 @@ sys_timer_create(const clockid_t which_c
 				new_timer->it_process = process;
 				list_add(&new_timer->list,
 					 &process->signal->posix_timers);
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
 					get_task_struct(process);
 			} else {
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				process = NULL;
 			}
+			spin_unlock_irqrestore(&process->sighand->siglock, flags);
 		}
 		read_unlock(&tasklist_lock);
 		if (!process) {
Acked-by on v1 2/4
#1 lore raw
Quoted 6 lines · Oleg Nesterov

sys_timer_create() sets ->it_process and unlocks ->siglock, then checks tmr->it_sigev_notify to define if get_task_struct() is needed.

We already passed ->it_id to the caller, another thread can delete this timer and free its memory in between.

Quoted 6 lines

As a minimal fix, move this code under ->siglock, sys_timer_delete() takes it too before calling release_posix_timer(). A proper serialization would be to take ->it_lock, we add a partly initialized timer on posix_timers_id, not good.

Quoted 18 lines
kernel/posix-timers.c renamed from kernel/posix-timers.c~2_CREATE +1 -2
@@ -547,13 +547,12 @@ sys_timer_create(const clockid_t which_c
 				new_timer->it_process = process;
 				list_add(&new_timer->list,
 					 &process->signal->posix_timers);
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
 					get_task_struct(process);
 			} else {
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				process = NULL;
 			}
+			spin_unlock_irqrestore(&process->sighand->siglock, flags);
 		}
 		read_unlock(&tasklist_lock);
 		if (!process) {
#2 lore raw
Quoted 8 lines · Thomas Gleixner
Quoted 6 lines · Oleg Nesterov

sys_timer_create() sets ->it_process and unlocks ->siglock, then checks tmr->it_sigev_notify to define if get_task_struct() is needed.

We already passed ->it_id to the caller, another thread can delete this timer and free its memory in between.

Quoted 8 lines
Quoted 6 lines

As a minimal fix, move this code under ->siglock, sys_timer_delete() takes it too before calling release_posix_timer(). A proper serialization would be to take ->it_lock, we add a partly initialized timer on posix_timers_id, not good.

Oooh, thanks, but...

Quoted 6 lines
Quoted 6 lines
kernel/posix-timers.c renamed from kernel/posix-timers.c~2_CREATE +0 -0
@@ -547,13 +547,12 @@ sys_timer_create(const clockid_t which_c
 				new_timer->it_process = process;
 				list_add(&new_timer->list,
 					 &process->signal->posix_timers);
Quoted 8 lines

kernel/posix-timers.c sys_timer_create(const clockid_t which_c · line 555

Quoted 8 lines
kernel/posix-timers.c renamed from kernel/posix-timers.c~2_CREATE +1 -2
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
 					get_task_struct(process);
 			} else {
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				process = NULL;
 			}
+			spin_unlock_irqrestore(&process->sighand->siglock, flags);

i'll resend this patch tomorrow. We can't do spin_unlock_irqrestore(&process->...) if we set process = NULL above.

This all needs a cleanup anyway. The PF_EXITING check and related comment are bogus.

Other patches do not depend on this one.

Oleg.

#3 lore raw
Quoted 8 lines · Oleg Nesterov
Quoted 5 lines

Review

Quoted 5 lines
(quoted diff) +1 -1
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				process = NULL;
 			}
+			spin_unlock_irqrestore(&process->sighand->siglock, flags);

i'll resend this patch tomorrow. We can't do spin_unlock_irqrestore(&process->...) if we set process = NULL above.

Another attempt, no cleanups, just a minimal fix for now.

[PATCH 2/4] posix-timers: fix creation race

sys_timer_create() sets ->it_process and unlocks ->siglock, then checks tmr->it_sigev_notify to define if get_task_struct() is needed.

We already passed ->it_id to the caller, another thread can delete this timer and free its memory in between.

As a minimal fix, move this code under ->siglock, sys_timer_delete() takes it too before calling release_posix_timer(). A proper serialization would be to take ->it_lock, we add a partly initialized timer on posix_timers_id, not good.

kernel/posix-timers.c renamed from kernel/posix-timers.c~ +1 -1
@@ -547,9 +547,9 @@ sys_timer_create(const clockid_t which_c
 				new_timer->it_process = process;
 				list_add(&new_timer->list,
 					 &process->signal->posix_timers);
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
 					get_task_struct(process);
+				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 			} else {
 				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				process = NULL;
#4 lore raw
In reply to Oleg Nesterov
Quoted 9 lines · Oleg Nesterov
Quoted 9 lines
Quoted 9 lines
kernel/posix-timers.c renamed from kernel/posix-timers.c~2_CREATE +0 -1
@@ -547,13 +547,12 @@ sys_timer_create(const clockid_t which_c
 				new_timer->it_process = process;
 				list_add(&new_timer->list,
 					 &process->signal->posix_timers);
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				if (new_timer->it_sigev_notify == (SIGEV_SIGNAL|SIGEV_THREAD_ID))
 					get_task_struct(process);
Quoted 8 lines
Quoted 5 lines

kernel/posix-timers.c sys_timer_create(const clockid_t which_c · line 555

Quoted 5 lines
kernel/posix-timers.c renamed from kernel/posix-timers.c~2_CREATE +1 -1
 			} else {
-				spin_unlock_irqrestore(&process->sighand->siglock, flags);
 				process = NULL;
 			}
+			spin_unlock_irqrestore(&process->sighand->siglock, flags);

i'll resend this patch tomorrow. We can't do spin_unlock_irqrestore(&process->...) if we set process = NULL above.

Doh, half of my brain seems to be still on vacation.

tglx