Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp1501087img; Tue, 26 Feb 2019 23:54:20 -0800 (PST) X-Google-Smtp-Source: AHgI3IZErCVuPRFCUvgViXUhGVbnQEXG3/aJZO2+BsoFbzb5VlOWyOYhqQAxGNoYR8IPYZtV+aLb X-Received: by 2002:a63:295:: with SMTP id 143mr1681096pgc.362.1551254060135; Tue, 26 Feb 2019 23:54:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551254060; cv=none; d=google.com; s=arc-20160816; b=u3ner3WSC3cYn2YNtq4qlVBokIpjzKZCM+6dQLty1rzxddoOf6rRBICm7foi72Uckn fJAOqUfZqzQQ2pD3Dvz9Alh8/G1kKdVv3ehbLodZoiz3VErZr+q1TScjq70/4ygK9JRD kh6fFI4zISViKAqUTE66AKvxEDQ0au1gpEHw5KHqsMp1bBMOKLsHkV7k7p6/XPolbV+U DqMmVSjP51aI0iPjcJTpV4ULAY91A+In0PXv5c/cznC/GWzPhiD865+ZPuZK9MY0kHh3 Dr5oFMQrZDZfQaFksSMKIvhu+nsJ34+KMGLm3Ptr5w7L9+sT6J0ga2d84a2VEoQJ02Kh qG0g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=wz08pomIT9x3H7Wnygw34Sun3aGtour5r5hk8Of/HxM=; b=k2mdtzoPUZ7zOczwcy3muc00XLHggv8fmGxyv2Gb6Vfw+AgoVimyITI14oJxi3tCLC vefiBFgRF9xhKbQZ12LWz6lzNBuGOh59TYXYjZjWYYP403B8HpxxzcmZQca95Wy5cjTZ 1tV6Y5gx6WUBdHcZsJk1R5kKd6e+Z1gBKzt+T93dMsqX6RBHC+hGRTGsiTq8uA28yT0D dkFtk8fpdWb6seIvUHE4lCoi02MtV67rXgFJ+i9PSoIc0vGM4IJy8OqdciSPIF/0brEb 6KDuvAguARr54jj6HeICyByasa8pLi6u6FMOiav79P7eFqzFGp4eNdUZV+Nr2kWtdWqK C1ZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id g14si7997676pfh.190.2019.02.26.23.54.04; Tue, 26 Feb 2019 23:54:20 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729542AbfB0Hw6 (ORCPT + 99 others); Wed, 27 Feb 2019 02:52:58 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:4176 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726702AbfB0Hw6 (ORCPT ); Wed, 27 Feb 2019 02:52:58 -0500 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 03F42E518EBE1F578CB8 for ; Wed, 27 Feb 2019 15:52:56 +0800 (CST) Received: from linux-ibm.site (10.175.102.37) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.408.0; Wed, 27 Feb 2019 15:52:49 +0800 From: Xiongfeng Wang To: , , CC: , Subject: [PATCH v2] posix-cpu-timers: Avoid undefined behaviour in timespec64_to_ns() Date: Wed, 27 Feb 2019 15:52:02 +0800 Message-ID: <1551253922-3307-1-git-send-email-wangxiongfeng2@huawei.com> X-Mailer: git-send-email 1.7.12.4 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.102.37] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 use 'timespec64_valid_restrict()' to check whether 'it_interval.tv_sec' is larger than 'KTIME_SEC_MAX'. Signed-off-by: Xiongfeng Wang --- kernel/time/posix-timers.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 0e84bb7..97b773c 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -853,8 +853,8 @@ static int do_timer_settime(timer_t timer_id, int flags, unsigned long flag; int error = 0; - if (!timespec64_valid(&new_spec64->it_interval) || - !timespec64_valid(&new_spec64->it_value)) + if (!timespec64_valid_strict(&new_spec64->it_interval) || + !timespec64_valid_strict(&new_spec64->it_value)) return -EINVAL; if (old_spec64) -- 1.7.12.4