Skip to content

Commit 747128e

Browse files
KAGA-KOKOgregkh
authored andcommitted
alarmtimer: Prevent overflow for relative nanosleep
[ Upstream commit 5f936e19cc0ef97dbe3a56e9498922ad5ba1edef ] Air Icy reported: UBSAN: Undefined behaviour in kernel/time/alarmtimer.c:811:7 signed integer overflow: 1529859276030040771 + 9223372036854775807 cannot be represented in type 'long long int' Call Trace: alarm_timer_nsleep+0x44c/0x510 kernel/time/alarmtimer.c:811 __do_sys_clock_nanosleep kernel/time/posix-timers.c:1235 [inline] __se_sys_clock_nanosleep kernel/time/posix-timers.c:1213 [inline] __x64_sys_clock_nanosleep+0x326/0x4e0 kernel/time/posix-timers.c:1213 do_syscall_64+0xb8/0x3a0 arch/x86/entry/common.c:290 alarm_timer_nsleep() uses ktime_add() to add the current time and the relative expiry value. ktime_add() has no sanity checks so the addition can overflow when the relative timeout is large enough. Use ktime_add_safe() which has the necessary sanity checks in place and limits the result to the valid range. Fixes: 9a7adcf ("timers: Posix interface for alarm-timers") Reported-by: Team OWL337 <[email protected]> Signed-off-by: Thomas Gleixner <[email protected]> Cc: John Stultz <[email protected]> Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 47dd066 commit 747128e

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

kernel/time/alarmtimer.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,8 @@ static int alarm_timer_nsleep(const clockid_t which_clock, int flags,
786786
/* Convert (if necessary) to absolute time */
787787
if (flags != TIMER_ABSTIME) {
788788
ktime_t now = alarm_bases[type].gettime();
789-
exp = ktime_add(now, exp);
789+
790+
exp = ktime_add_safe(now, exp);
790791
}
791792

792793
if (alarmtimer_do_nsleep(&alarm, exp))

0 commit comments

Comments
 (0)