From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751590AbdFTPlx (ORCPT ); Tue, 20 Jun 2017 11:41:53 -0400 Received: from Galois.linutronix.de ([146.0.238.70]:44939 "EHLO Galois.linutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750711AbdFTPlv (ORCPT ); Tue, 20 Jun 2017 11:41:51 -0400 Message-Id: <20170620154113.588276707@linutronix.de> User-Agent: quilt/0.63-1 Date: Tue, 20 Jun 2017 17:37:36 +0200 From: Thomas Gleixner To: LKML Cc: John Stultz , Peter Zijlstra , Xishi Qiu Subject: [patch 2/2] posix-cpu-timers: Make timespec to nsec conversion safe References: <20170620153734.970526461@linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15 Content-Disposition: inline; filename=posix-cpu-timers--Make-timespec-to-nsec-conversion-safe.patch Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- 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