timers: Prevent multiplication overflow in timespec/val conversions
cover timers: Prevent multiplication overflow in timespec/val conversions
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
PATCH 1/2 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.
--- kernel/time/itimer.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
--- kernel/time/itimer.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/time/itimer.c +6 -2
@@ -138,8 +138,12 @@ static void set_cpu_itimer(struct task_su64 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);
tip-bot for Thomas Gleixner
merge bot[tip:timers/core] itimer: Make timeval to nsec conversion range limited
itimer: Make timeval to nsec conversion range limited
Commit-ID: 35eb7258c009dc478338e674a5a84d25d0929c56 Gitweb: http://git.kernel.org/tip/35eb7258c009dc478338e674a5a84d25d0929c56 Author: Thomas Gleixner <[email protected]> AuthorDate: Tue, 20 Jun 2017 17:37:35 +0200 Committer: Thomas Gleixner <[email protected]> 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.
--- kernel/time/itimer.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
kernel/time/itimer.c +6 -2
@@ -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);
PATCH 2/2 posix-cpu-timers: Make timespec to nsec conversion safe
In reply to Thomas GleixnerThe 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.
--- kernel/time/posix-cpu-timers.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +5 -1
@@ -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
tip-bot for Thomas Gleixner
merge bot[tip:timers/core] posix-cpu-timers: Make timespec to nsec conversion safe
posix-cpu-timers: Make timespec to nsec conversion safe
Commit-ID: 098b0e01a91c42aaaf0425605cd126b03fcb0bcf Gitweb: http://git.kernel.org/tip/098b0e01a91c42aaaf0425605cd126b03fcb0bcf Author: Thomas Gleixner <[email protected]> AuthorDate: Tue, 20 Jun 2017 17:37:36 +0200 Committer: Thomas Gleixner <[email protected]> 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.
--- kernel/time/posix-cpu-timers.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/time/posix-cpu-timers.c +5 -1
@@ -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