posix-cpu-timers: Avoid undefined behaviour in timespec64_to_ns()
PATCH v3 1/1 posix-cpu-timers: Avoid undefined behaviour in timespec64_to_ns()
When I ran Syzkaller testsuite, I got the following call trace.
================================================================================
UBSAN: Undefined behaviour in ./include/linux/time64.h:120:27 signed integer overflow: 8243129037239968815 * 1000000000 cannot be represented in type 'long long int' CPU: 5 PID: 28854 Comm: syz-executor.1 Not tainted 4.19.24 #4 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.11.0-0-g63451fca13-prebuilt.qemu-project.org 04/01/2014 Call Trace: __dump_stack lib/dump_stack.c:77 [inline] dump_stack+0xca/0x13e lib/dump_stack.c:113 ubsan_epilogue+0xe/0x81 lib/ubsan.c:159 handle_overflow+0x193/0x1e2 lib/ubsan.c:190 timespec64_to_ns include/linux/time64.h:120 [inline] posix_cpu_timer_set+0x95a/0xb70 kernel/time/posix-cpu-timers.c:687 do_timer_settime+0x198/0x2a0 kernel/time/posix-timers.c:892 __do_sys_timer_settime kernel/time/posix-timers.c:918 [inline] __se_sys_timer_settime kernel/time/posix-timers.c:904 [inline] __x64_sys_timer_settime+0x18d/0x260 kernel/time/posix-timers.c:904 do_syscall_64+0xc8/0x580 arch/x86/entry/common.c:290 entry_SYSCALL_64_after_hwframe+0x49/0xbe RIP: 0033:0x462eb9 Code: f7 d8 64 89 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 bc ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007f14e4127c58 EFLAGS: 00000246 ORIG_RAX: 00000000000000df RAX: ffffffffffffffda RBX: 000000000073bfa0 RCX: 0000000000462eb9 RDX: 0000000020000080 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000004 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 00007f14e41286bc R13: 00000000004c54cc R14: 0000000000704278 R15: 00000000ffffffff ================================================================================
It is because 'it_interval.tv_sec' is larger than 'KTIME_SEC_MAX' and 'it_interval.tv_sec * NSEC_PER_SEC' overflows in 'timespec64_to_ns()'.
This patch checks whether 'it_interval.tv_sec' is larger than 'KTIME_SEC_MAX' and saturate if that is the case.
---
v3:
- add limit check and saturate
v2:
- use 'timespec64_valid_restrict()' to check the input and return -EINVAL if check fails.
--- kernel/time/posix-timers.c | 4 ++++ 1 file changed, 4 insertions(+)
kernel/time/posix-timers.c +4 -0
@@ -856,6 +856,10 @@ static int do_timer_settime(timer_t timer_id, int flags,if (!timespec64_valid (&new_spec64->it_interval) || !timespec64_valid (&new_spec64->it_value))return -EINVAL ; +if (new_spec64->it_interval.tv_sec >KTIME_SEC_MAX ) + new_spec64->it_interval.tv_sec =KTIME_SEC_MAX ; +if (new_spec64->it_value.tv_sec >KTIME_SEC_MAX ) + new_spec64->it_value.tv_sec =KTIME_SEC_MAX ;if (old_spec64)memset (old_spec64,0 ,sizeof (*old_spec64));
Signature · 1 line
1.7.12.4
Quoted 7 lines · Xiongfeng Wang
kernel/time/posix-timers.c +0
-0
kernel/time/posix-timers.c static int do_timer_settime(timer_t timer_id, int flags, · line 863
Quoted 8 lines
kernel/time/posix-timers.c +4
-0@@ -856,6 +856,10 @@ static int do_timer_settime(timer_t timer_id, int flags,if (!timespec64_valid (&new_spec64->it_interval) || !timespec64_valid (&new_spec64->it_value))return -EINVAL ; +if (new_spec64->it_interval.tv_sec >KTIME_SEC_MAX ) + new_spec64->it_interval.tv_sec =KTIME_SEC_MAX ; +if (new_spec64->it_value.tv_sec >KTIME_SEC_MAX ) + new_spec64->it_value.tv_sec =KTIME_SEC_MAX ;
I looked at the calculation we do later, and I think this can still overflow if tv_nsec is too large. The largest timespec value we can support is
(struct timespec64) { .tv_sec = 9223372036, .tv_nsec = 854775807 }
Your patch caps the tv_sec value to 9223372036, but it does not cap the tv_nsec. The easiest fix would be to always set tv_nsec to 0 if tv_sec>=9223372036, or a more correct calculation would have to limit tv_nsec if tv_sec==9223372036. I don't know if that matters or not (it should not, unless we explicitly compare the ktime_t for equality with KTIME_MAX later).
Arnd
Quoted 20 lines · Arnd Bergmann
Quoted 7 lines · Xiongfeng Wang
kernel/time/posix-timers.c +0
-0kernel/time/posix-timers.c static int do_timer_settime(timer_t timer_id, int flags, · line 863
Quoted 8 lines
kernel/time/posix-timers.c +4
-0@@ -856,6 +856,10 @@ static int do_timer_settime(timer_t timer_id, int flags,if (!timespec64_valid (&new_spec64->it_interval) || !timespec64_valid (&new_spec64->it_value))return -EINVAL ; +if (new_spec64->it_interval.tv_sec >KTIME_SEC_MAX ) + new_spec64->it_interval.tv_sec =KTIME_SEC_MAX ; +if (new_spec64->it_value.tv_sec >KTIME_SEC_MAX ) + new_spec64->it_value.tv_sec =KTIME_SEC_MAX ;I looked at the calculation we do later, and I think this can still overflow if
tv_nsecis too large. The largest timespec value we can support is
Quoted 8 lines
(struct timespec64) { .tv_sec = 9223372036, .tv_nsec = 854775807 }
Your patch caps the
tv_secvalue to 9223372036, but it does not cap thetv_nsec. The easiest fix would be to always settv_nsecto 0 if tv_sec>=9223372036, or a more correct calculation would have to limittv_nsecif tv_sec==9223372036. I don't know if that matters or not (it should not, unless we explicitly compare thektime_tfor equality withKTIME_MAXlater).
Thanks for your advice. I will send another one to set tv_nsec
to 0 if tv_sec>=9223372036.
Do need to add a helper to clamp timespec64, such as 'saturate_timespec64_valid()' to limit it between 0 and KTIME_MAX(or KTIME_SEC_MAX, it's easier.)
Thanks,
Xiongfeng
Quoted 5 lines
Arnd
.