posix-timers: fix deletion race
authorThomas Gleixner <tglx@linutronix.de>
Wed, 22 Aug 2007 21:01:37 +0000 (14:01 -0700)
committerLinus Torvalds <torvalds@woody.linux-foundation.org>
Thu, 23 Aug 2007 02:52:45 +0000 (19:52 -0700)
timer_delete does:
lock_timer();
timer->it_process = NULL;
unlock_timer();
release_posix_timer();

timer->it_process is checked in lock_timer() to prevent access to a
timer, which is on the way to be deleted, but the check happens after
idr_lock is dropped. This allows release_posix_timer() to delete the
timer before the lock code can check the timer:

  CPU 0 CPU 1

  lock_timer();
  timer->it_process = NULL;
  unlock_timer();
lock_timer()
spin_lock(idr_lock);
timer = idr_find();
spin_lock(timer->lock);
spin_unlock(idr_lock);
  release_posix_timer();
spin_lock(idr_lock);
idr_remove(timer);
spin_unlock(idr_lock);
free_timer(timer);
if (timer->......)

Change the locking to prevent this.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
kernel/posix-timers.c

index 55b3761edaa9afe42273a7b3adaa42b1547fb5ee..6923ad8a5983dc8ddc8772d8461e1f98fa28e183 100644 (file)
@@ -605,13 +605,14 @@ static struct k_itimer * lock_timer(timer_t timer_id, unsigned long *flags)
        timr = (struct k_itimer *) idr_find(&posix_timers_id, (int) timer_id);
        if (timr) {
                spin_lock(&timr->it_lock);
-               spin_unlock(&idr_lock);
 
                if ((timr->it_id != timer_id) || !(timr->it_process) ||
                                timr->it_process->tgid != current->tgid) {
-                       unlock_timer(timr, *flags);
+                       spin_unlock(&timr->it_lock);
+                       spin_unlock_irqrestore(&idr_lock, *flags);
                        timr = NULL;
-               }
+               } else
+                       spin_unlock(&idr_lock);
        } else
                spin_unlock_irqrestore(&idr_lock, *flags);