Skip to content

Commit cb47755

Browse files
prime-zengKAGA-KOKO
authored andcommitted
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 bd40a17 ("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: 361a3bf ("time64: Add time64.h header and define struct timespec64") Signed-off-by: Zeng Tao <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Reviewed-by: Arnd Bergmann <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent 9010e38 commit cb47755

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

include/linux/time64.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ static inline bool timespec64_valid_settod(const struct timespec64 *ts)
124124
*/
125125
static inline s64 timespec64_to_ns(const struct timespec64 *ts)
126126
{
127+
/* Prevent multiplication overflow */
128+
if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX)
129+
return KTIME_MAX;
130+
127131
return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec;
128132
}
129133

kernel/time/itimer.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,6 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id,
172172
u64 oval, nval, ointerval, ninterval;
173173
struct cpu_itimer *it = &tsk->signal->it[clock_id];
174174

175-
/*
176-
* Use the to_ktime conversion because that clamps the maximum
177-
* value to KTIME_MAX and avoid multiplication overflows.
178-
*/
179175
nval = timespec64_to_ns(&value->it_value);
180176
ninterval = timespec64_to_ns(&value->it_interval);
181177

0 commit comments

Comments
 (0)