time: Prevent undefined behaviour in timespec64_to_ns()
authorZeng Tao <prime.zeng@hisilicon.com>
Tue, 1 Sep 2020 09:30:13 +0000 (17:30 +0800)
committerThomas Gleixner <tglx@linutronix.de>
Mon, 26 Oct 2020 10:48:11 +0000 (11:48 +0100)
commitcb47755725da7b90fecbb2aa82ac3b24a7adb89b
treed1480795667ccb9760ea9b5fe6f36df8f6028aee
parent9010e3876e1c3f7b1c3769bee519d6a871589aca
time: Prevent undefined behaviour in timespec64_to_ns()

UBSAN reports:

Undefined behaviour in ./include/linux/time64.h:127:27
signed integer overflow:
17179869187 * 1000000000 cannot be represented in type 'long long int'
Call Trace:
 timespec64_to_ns include/linux/time64.h:127 [inline]
 set_cpu_itimer+0x65c/0x880 kernel/time/itimer.c:180
 do_setitimer+0x8e/0x740 kernel/time/itimer.c:245
 __x64_sys_setitimer+0x14c/0x2c0 kernel/time/itimer.c:336
 do_syscall_64+0xa1/0x540 arch/x86/entry/common.c:295

Commit bd40a175769d ("y2038: itimer: change implementation to timespec64")
replaced the original conversion which handled time clamping correctly with
timespec64_to_ns() which has no overflow protection.

Fix it in timespec64_to_ns() as this is not necessarily limited to the
usage in itimers.

[ tglx: Added comment and adjusted the fixes tag ]

Fixes: 361a3bf00582 ("time64: Add time64.h header and define struct timespec64")
Signed-off-by: Zeng Tao <prime.zeng@hisilicon.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Reviewed-by: Arnd Bergmann <arnd@arndb.de>
Cc: stable@vger.kernel.org
Link: https://lore.kernel.org/r/1598952616-6416-1-git-send-email-prime.zeng@hisilicon.com
include/linux/time64.h
kernel/time/itimer.c