linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [patch 0/2] timers: Prevent multiplication overflow in timespec/val conversions
@ 2017-06-20 15:37 Thomas Gleixner
  2017-06-20 15:37 ` [patch 1/2] itimer: Make timeval to nsec conversion range limited Thomas Gleixner
  2017-06-20 15:37 ` [patch 2/2] posix-cpu-timers: Make timespec to nsec conversion safe Thomas Gleixner
  0 siblings, 2 replies; 5+ messages in thread
From: Thomas Gleixner @ 2017-06-20 15:37 UTC (permalink / raw)
  To: LKML; +Cc: John Stultz, Peter Zijlstra, Xishi Qiu

KASAN detected a multiplication overflow in the sys_setitimer() timeval
conversion. The same issue is possible the posix-cpu-timer sys_timer_set()
implementation.

In both cases the conversion of the tv_sec part overflows when multiplied
with NSEC_PER_SEC, i.e. 1e9.

This can be mitigated by using the timespec/val_to_ktime() helper, which
prevents the overflow and clamps the result to KTIME_MAX (~292 years).

Thanks,

	tglx

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

* [patch 1/2] itimer: Make timeval to nsec conversion range limited
  2017-06-20 15:37 [patch 0/2] timers: Prevent multiplication overflow in timespec/val conversions Thomas Gleixner
@ 2017-06-20 15:37 ` Thomas Gleixner
  2017-06-20 19:39   ` [tip:timers/core] " tip-bot for Thomas Gleixner
  2017-06-20 15:37 ` [patch 2/2] posix-cpu-timers: Make timespec to nsec conversion safe Thomas Gleixner
  1 sibling, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2017-06-20 15:37 UTC (permalink / raw)
  To: LKML; +Cc: John Stultz, Peter Zijlstra, Xishi Qiu

[-- Attachment #1: itimer--Make-timeval-to-nsec-conversion-range-limited.patch --]
[-- Type: text/plain, Size: 1476 bytes --]

The expiry time of a itimer is supplied through sys_setitimer() via a
struct timeval. The timeval is validated for correctness.

In the actual set timer implementation the timeval is converted to a
scalar nanoseconds value. If the tv_sec part of the time spec is large
enough the conversion to nanoseconds (sec * NSEC_PER_SEC) overflows 64bit.

Mitigate that by using the timeval_to_ktime() conversion function, which
checks the tv_sec part for a potential mult overflow and clamps the result
to KTIME_MAX, which is about 292 years. 

Reported-by: Xishi Qiu <qiuxishi@huawei.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/itimer.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)


Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/itimer.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

--- a/kernel/time/itimer.c
+++ b/kernel/time/itimer.c
@@ -138,8 +138,12 @@ static void set_cpu_itimer(struct task_s
 	u64 oval, nval, ointerval, ninterval;
 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
 
-	nval = timeval_to_ns(&value->it_value);
-	ninterval = timeval_to_ns(&value->it_interval);
+	/*
+	 * Use the to_ktime conversion because that clamps the maximum
+	 * value to KTIME_MAX and avoid multiplication overflows.
+	 */
+	nval = ktime_to_ns(timeval_to_ktime(value->it_value));
+	ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
 
 	spin_lock_irq(&tsk->sighand->siglock);
 

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

* [patch 2/2] posix-cpu-timers: Make timespec to nsec conversion safe
  2017-06-20 15:37 [patch 0/2] timers: Prevent multiplication overflow in timespec/val conversions Thomas Gleixner
  2017-06-20 15:37 ` [patch 1/2] itimer: Make timeval to nsec conversion range limited Thomas Gleixner
@ 2017-06-20 15:37 ` Thomas Gleixner
  2017-06-20 19:40   ` [tip:timers/core] " tip-bot for Thomas Gleixner
  1 sibling, 1 reply; 5+ messages in thread
From: Thomas Gleixner @ 2017-06-20 15:37 UTC (permalink / raw)
  To: LKML; +Cc: John Stultz, Peter Zijlstra, Xishi Qiu

[-- Attachment #1: posix-cpu-timers--Make-timespec-to-nsec-conversion-safe.patch --]
[-- Type: text/plain, Size: 1199 bytes --]

The expiry time of a posix cpu timer is supplied through sys_timer_set()
via a struct timespec. The timespec is validated for correctness.

In the actual set timer implementation the timespec is converted to a
scalar nanoseconds value. If the tv_sec part of the time spec is large
enough the conversion to nanoseconds (sec * NSEC_PER_SEC) overflows 64bit.

Mitigate that by using the timespec_to_ktime() conversion function, which
checks the tv_sec part for a potential mult overflow and clamps the result
to KTIME_MAX, which is about 292 years. 

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
 kernel/time/posix-cpu-timers.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -572,7 +572,11 @@ static int posix_cpu_timer_set(struct k_
 
 	WARN_ON_ONCE(p == NULL);
 
-	new_expires = timespec64_to_ns(&new->it_value);
+	/*
+	 * Use the to_ktime conversion because that clamps the maximum
+	 * value to KTIME_MAX and avoid multiplication overflows.
+	 */
+	new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
 
 	/*
 	 * Protect against sighand release/switch in exit/exec and p->cpu_timers

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

* [tip:timers/core] itimer: Make timeval to nsec conversion range limited
  2017-06-20 15:37 ` [patch 1/2] itimer: Make timeval to nsec conversion range limited Thomas Gleixner
@ 2017-06-20 19:39   ` tip-bot for Thomas Gleixner
  0 siblings, 0 replies; 5+ messages in thread
From: tip-bot for Thomas Gleixner @ 2017-06-20 19:39 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: mingo, linux-kernel, hpa, peterz, qiuxishi, tglx, john.stultz

Commit-ID:  35eb7258c009dc478338e674a5a84d25d0929c56
Gitweb:     http://git.kernel.org/tip/35eb7258c009dc478338e674a5a84d25d0929c56
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 20 Jun 2017 17:37:35 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Tue, 20 Jun 2017 21:33:56 +0200

itimer: Make timeval to nsec conversion range limited

The expiry time of a itimer is supplied through sys_setitimer() via a
struct timeval. The timeval is validated for correctness.

In the actual set timer implementation the timeval is converted to a
scalar nanoseconds value. If the tv_sec part of the time spec is large
enough the conversion to nanoseconds (sec * NSEC_PER_SEC) overflows 64bit.

Mitigate that by using the timeval_to_ktime() conversion function, which
checks the tv_sec part for a potential mult overflow and clamps the result
to KTIME_MAX, which is about 292 years. 

Reported-by: Xishi Qiu <qiuxishi@huawei.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170620154113.505981643@linutronix.de

---
 kernel/time/itimer.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c
index 9dd7ff5..2ef98a0 100644
--- a/kernel/time/itimer.c
+++ b/kernel/time/itimer.c
@@ -152,8 +152,12 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
 	u64 oval, nval, ointerval, ninterval;
 	struct cpu_itimer *it = &tsk->signal->it[clock_id];
 
-	nval = timeval_to_ns(&value->it_value);
-	ninterval = timeval_to_ns(&value->it_interval);
+	/*
+	 * Use the to_ktime conversion because that clamps the maximum
+	 * value to KTIME_MAX and avoid multiplication overflows.
+	 */
+	nval = ktime_to_ns(timeval_to_ktime(value->it_value));
+	ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));
 
 	spin_lock_irq(&tsk->sighand->siglock);
 

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

* [tip:timers/core] posix-cpu-timers: Make timespec to nsec conversion safe
  2017-06-20 15:37 ` [patch 2/2] posix-cpu-timers: Make timespec to nsec conversion safe Thomas Gleixner
@ 2017-06-20 19:40   ` tip-bot for Thomas Gleixner
  0 siblings, 0 replies; 5+ messages in thread
From: tip-bot for Thomas Gleixner @ 2017-06-20 19:40 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: john.stultz, peterz, tglx, hpa, qiuxishi, linux-kernel, mingo

Commit-ID:  098b0e01a91c42aaaf0425605cd126b03fcb0bcf
Gitweb:     http://git.kernel.org/tip/098b0e01a91c42aaaf0425605cd126b03fcb0bcf
Author:     Thomas Gleixner <tglx@linutronix.de>
AuthorDate: Tue, 20 Jun 2017 17:37:36 +0200
Committer:  Thomas Gleixner <tglx@linutronix.de>
CommitDate: Tue, 20 Jun 2017 21:33:56 +0200

posix-cpu-timers: Make timespec to nsec conversion safe

The expiry time of a posix cpu timer is supplied through sys_timer_set()
via a struct timespec. The timespec is validated for correctness.

In the actual set timer implementation the timespec is converted to a
scalar nanoseconds value. If the tv_sec part of the time spec is large
enough the conversion to nanoseconds (sec * NSEC_PER_SEC) overflows 64bit.

Mitigate that by using the timespec_to_ktime() conversion function, which
checks the tv_sec part for a potential mult overflow and clamps the result
to KTIME_MAX, which is about 292 years. 

Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Xishi Qiu <qiuxishi@huawei.com>
Cc: John Stultz <john.stultz@linaro.org>
Link: http://lkml.kernel.org/r/20170620154113.588276707@linutronix.de

---
 kernel/time/posix-cpu-timers.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index 9df618e..60cb24a 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -580,7 +580,11 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
 
 	WARN_ON_ONCE(p == NULL);
 
-	new_expires = timespec64_to_ns(&new->it_value);
+	/*
+	 * Use the to_ktime conversion because that clamps the maximum
+	 * value to KTIME_MAX and avoid multiplication overflows.
+	 */
+	new_expires = ktime_to_ns(timespec64_to_ktime(new->it_value));
 
 	/*
 	 * Protect against sighand release/switch in exit/exec and p->cpu_timers

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

end of thread, other threads:[~2017-06-20 19:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-20 15:37 [patch 0/2] timers: Prevent multiplication overflow in timespec/val conversions Thomas Gleixner
2017-06-20 15:37 ` [patch 1/2] itimer: Make timeval to nsec conversion range limited Thomas Gleixner
2017-06-20 19:39   ` [tip:timers/core] " tip-bot for Thomas Gleixner
2017-06-20 15:37 ` [patch 2/2] posix-cpu-timers: Make timespec to nsec conversion safe Thomas Gleixner
2017-06-20 19:40   ` [tip:timers/core] " tip-bot for Thomas Gleixner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).