]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
hrtimer: check relative timeouts for overflow
authorThomas Gleixner <tglx@linutronix.de>
Wed, 20 Feb 2008 00:03:00 +0000 (01:03 +0100)
committerGreg Kroah-Hartman <gregkh@suse.de>
Tue, 26 Feb 2008 00:18:57 +0000 (16:18 -0800)
commit: 5a7780e725d1bb4c3094fcc12f1c5c5faea1e988

Various user space callers ask for relative timeouts. While we fixed
that overflow issue in hrtimer_start(), the sites which convert
relative user space values to absolute timeouts themself were uncovered.

Instead of putting overflow checks into each place add a function
which does the sanity checking and convert all affected callers to use
it.

Thanks to Frans Pop, who reported the problem and tested the fixes.

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Acked-by: Ingo Molnar <mingo@elte.hu>
Tested-by: Frans Pop <elendil@planet.nl>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
include/linux/ktime.h
kernel/futex.c
kernel/futex_compat.c
kernel/hrtimer.c
kernel/posix-timers.c

index a6ddec141f96c83752ddb060079d0efa4e517aa4..816cf4eaa5179772b820e214079cc372623a7b45 100644 (file)
@@ -310,6 +310,8 @@ static inline ktime_t ktime_sub_us(const ktime_t kt, const u64 usec)
        return ktime_sub_ns(kt, usec * 1000);
 }
 
+extern ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs);
+
 /*
  * The resolution of the clocks. The resolution value is returned in
  * the clock_getres() system call to give application programmers an
index db9824de8bf061323b734d88377a6fa606fb9a86..55d78b5a9622ed43441cc7c398ea17ec2e36f695 100644 (file)
@@ -2094,7 +2094,7 @@ asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
 
                t = timespec_to_ktime(ts);
                if (cmd == FUTEX_WAIT)
-                       t = ktime_add(ktime_get(), t);
+                       t = ktime_add_safe(ktime_get(), t);
                tp = &t;
        }
        /*
index 0a43def6fee7de877f43bdd0d7276efc05b8a48e..8682c79220f5931aa7a1aaa790f28360be782215 100644 (file)
@@ -175,7 +175,7 @@ asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
 
                t = timespec_to_ktime(ts);
                if (cmd == FUTEX_WAIT)
-                       t = ktime_add(ktime_get(), t);
+                       t = ktime_add_safe(ktime_get(), t);
                tp = &t;
        }
        if (cmd == FUTEX_REQUEUE || cmd == FUTEX_CMP_REQUEUE)
index fac807688eb31bedd0f84917955d45860268fa66..2ed800218e5e04676ca295e9e8a41091174803a5 100644 (file)
@@ -325,6 +325,24 @@ unsigned long ktime_divns(const ktime_t kt, s64 div)
 }
 #endif /* BITS_PER_LONG >= 64 */
 
+/*
+ * Add two ktime values and do a safety check for overflow:
+ */
+
+ktime_t ktime_add_safe(const ktime_t lhs, const ktime_t rhs)
+{
+       ktime_t res = ktime_add(lhs, rhs);
+
+       /*
+        * We use KTIME_SEC_MAX here, the maximum timeout which we can
+        * return to user space in a timespec:
+        */
+       if (res.tv64 < 0 || res.tv64 < lhs.tv64 || res.tv64 < rhs.tv64)
+               res = ktime_set(KTIME_SEC_MAX, 0);
+
+       return res;
+}
+
 /* High resolution timer related functions */
 #ifdef CONFIG_HIGH_RES_TIMERS
 
@@ -682,13 +700,7 @@ hrtimer_forward(struct hrtimer *timer, ktime_t now, ktime_t interval)
                 */
                orun++;
        }
-       timer->expires = ktime_add(timer->expires, interval);
-       /*
-        * Make sure, that the result did not wrap with a very large
-        * interval.
-        */
-       if (timer->expires.tv64 < 0)
-               timer->expires = ktime_set(KTIME_SEC_MAX, 0);
+       timer->expires = ktime_add_safe(timer->expires, interval);
 
        return orun;
 }
@@ -839,7 +851,7 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
        new_base = switch_hrtimer_base(timer, base);
 
        if (mode == HRTIMER_MODE_REL) {
-               tim = ktime_add(tim, new_base->get_time());
+               tim = ktime_add_safe(tim, new_base->get_time());
                /*
                 * CONFIG_TIME_LOW_RES is a temporary way for architectures
                 * to signal that they simply return xtime in
@@ -848,16 +860,8 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
                 * timeouts. This will go away with the GTOD framework.
                 */
 #ifdef CONFIG_TIME_LOW_RES
-               tim = ktime_add(tim, base->resolution);
+               tim = ktime_add_safe(tim, base->resolution);
 #endif
-               /*
-                * Careful here: User space might have asked for a
-                * very long sleep, so the add above might result in a
-                * negative number, which enqueues the timer in front
-                * of the queue.
-                */
-               if (tim.tv64 < 0)
-                       tim.tv64 = KTIME_MAX;
        }
        timer->expires = tim;
 
index 6cf671499a9165d9e5569e3601f0dfed60880ef9..9076432492c6e69bd92af0403ca058f3871be2e0 100644 (file)
@@ -766,9 +766,11 @@ common_timer_set(struct k_itimer *timr, int flags,
        /* SIGEV_NONE timers are not queued ! See common_timer_get */
        if (((timr->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE)) {
                /* Setup correct expiry time for relative timers */
-               if (mode == HRTIMER_MODE_REL)
-                       timer->expires = ktime_add(timer->expires,
-                                                  timer->base->get_time());
+               if (mode == HRTIMER_MODE_REL) {
+                       timer->expires =
+                               ktime_add_safe(timer->expires,
+                                              timer->base->get_time());
+               }
                return 0;
        }