mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] time: do a safe overflow check in ktime_add_safe
@ 2014-12-02  4:04 Sasha Levin
  2014-12-02  4:04 ` [PATCH] time: make sure tz_minuteswest is set to a valid value when setting time Sasha Levin
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Sasha Levin @ 2014-12-02  4:04 UTC (permalink / raw)
  To: linux-kernel; +Cc: Sasha Levin, Thomas Gleixner

ktime_add_safe would check for overflows, but since ktime variables are
signed, overflowing them is an undefined behaviour and should be avoided.

Rather than checking for wraparound after the overflow, check for
potential overflowing values prior to adding both ktimes.

Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
---
 kernel/time/hrtimer.c |    8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/kernel/time/hrtimer.c b/kernel/time/hrtimer.c
index 37e50aa..42fb631 100644
--- a/kernel/time/hrtimer.c
+++ b/kernel/time/hrtimer.c
@@ -290,16 +290,14 @@ EXPORT_SYMBOL_GPL(ktime_divns);
  */
 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);
+	if (lhs.tv64 > (KTIME_MAX - rhs.tv64))
+		return ktime_set(KTIME_SEC_MAX, 0);
 
-	return res;
+	return ktime_add(lhs, rhs);
 }
 
 EXPORT_SYMBOL_GPL(ktime_add_safe);
-- 
1.7.10.4


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-12-02 11:16 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-12-02  4:04 [PATCH] time: do a safe overflow check in ktime_add_safe Sasha Levin
2014-12-02  4:04 ` [PATCH] time: make sure tz_minuteswest is set to a valid value when setting time Sasha Levin
2014-12-02  4:04 ` [PATCH] vfs: calculate seek offsets using unsigned variables Sasha Levin
2014-12-02  4:04 ` [PATCH] mm: fadvise: avoid signed integer overflow calculating offset Sasha Levin
2014-12-02  4:04 ` [PATCH] time: settimeofday: validate the values of tv fomr user Sasha Levin
2014-12-02 11:16   ` Thomas Gleixner
2014-12-02  4:04 ` [PATCH] fs: sync_file_range: avoid overflowing signed calculation Sasha Levin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®