poll/select: avoid arithmetic overflow in __estimate_accuracy()
authorGuillaume Knispel <gknispel@proformatique.com>
Tue, 22 Sep 2009 23:43:30 +0000 (16:43 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Wed, 23 Sep 2009 14:39:27 +0000 (07:39 -0700)
__estimate_accuracy() was prone to integer overflow, for example if *tv ==
{2147, 483648000} on a 32 bit computer (or even for delays as small as
{429, 500000000} if the task is niced).

Because the result was already forced between 0 and 100ms, the effect of
the overflow was not too problematic, but the use of the hrtimer range
feature was not optimal in overflow cases.

This patch ensures that there can not be an integer overflow in this
function.

Signed-off-by: Guillaume Knispel <gknispel@proformatique.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Cc: Arjan van de Ven <arjan@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Heiko Carstens <heiko.carstens@de.ibm.com>
Cc: Tejun Heo <tj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/select.c

index 8084834..a201fc3 100644 (file)
  * better solutions..
  */
 
+#define MAX_SLACK      (100 * NSEC_PER_MSEC)
+
 static long __estimate_accuracy(struct timespec *tv)
 {
        long slack;
        int divfactor = 1000;
 
+       if (tv->tv_sec < 0)
+               return 0;
+
        if (task_nice(current) > 0)
                divfactor = divfactor / 5;
 
+       if (tv->tv_sec > MAX_SLACK / (NSEC_PER_SEC/divfactor))
+               return MAX_SLACK;
+
        slack = tv->tv_nsec / divfactor;
        slack += tv->tv_sec * (NSEC_PER_SEC/divfactor);
 
-       if (slack > 100 * NSEC_PER_MSEC)
-               slack =  100 * NSEC_PER_MSEC;
+       if (slack > MAX_SLACK)
+               return MAX_SLACK;
 
-       if (slack < 0)
-               slack = 0;
        return slack;
 }