Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp3935886imb; Wed, 6 Mar 2019 00:57:32 -0800 (PST) X-Google-Smtp-Source: APXvYqyPpVX0huqrXqO7aSoS339VQT7Fs3wtrkksG5FH6XGYs/7wjIGKO+wy0WzLr8lvid0xDWRx X-Received: by 2002:a65:6154:: with SMTP id o20mr5274784pgv.205.1551862652874; Wed, 06 Mar 2019 00:57:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551862652; cv=none; d=google.com; s=arc-20160816; b=G6N7wDlq36THRfOkgGAV3zkkhxfLUz9YImqEazeBfwYB8SxbrTAD/mxd2UsEowZn/C g56aJNefX+zZxQE1UtUr6WtNzeQgSFqstxULyNPIrSDhPOXFuKIAnXoQ0Rn6uW07Om5z C1Bwg+lOB4XCUO/vGEqkWYw5ai6ZYGiokHD9+eeDTb5JvBW41cP6uqp8NnoTXKy5OLVz h4X4QH5M0JnaYUi0bwI0nwFYhP7vGSWBMDMJ8Cs0/C40MfKwI27tTzy+CAl/l0d/sY20 sXjgSzKcBZfsWEOKlcl6QXPFKiGMal9z5CAGQsrO1ZgUZSDvT1+dpUhgZv/1dot1eebv KiPg== 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=+R+hioXNvLg4l1YEh0AEZVjARxXDAwbS+yrm+ZOlWGQ=; b=TcaNOXDViSbZ8O7GzGJgi4UHm0rqLMPB7V8/WwNzmh6l5vj0SSkvQuRZupUOSqk1eB uinBbVi9o8o+VFQiBuqF4SlfT7uCjWBijv1KUIO3xGYWU+DVfhFh8LmW6EzQxHcgesZz 5/tisGCgPFkHxwFs+eHAyynSU3XHjuyDFcsnoGGczKeuLSsoxemllb4HrDHw5Hs0mqbA SSt54Ks+JJ/vkh1ZexzeE6Pi9AK0cGeX6wd8HyX8J3Z5drjR65zM/coPU4iQBL7rNv9V FwZYsl7X6/8lye9JX6DB5OXD2JmPxGln46HQc9O0vcc4kyn0W/ErVKEbCDIKF6Aouz1N amHw== 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 o6si996252plh.397.2019.03.06.00.57.17; Wed, 06 Mar 2019 00:57:32 -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 S1729613AbfCFIlt (ORCPT + 99 others); Wed, 6 Mar 2019 03:41:49 -0500 Received: from szxga07-in.huawei.com ([45.249.212.35]:52510 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725747AbfCFIlt (ORCPT ); Wed, 6 Mar 2019 03:41:49 -0500 Received: from DGGEMS409-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id 31B05448B87E48562C11 for ; Wed, 6 Mar 2019 16:41:47 +0800 (CST) Received: from linux-ibm.site (10.175.102.37) by DGGEMS409-HUB.china.huawei.com (10.3.19.209) with Microsoft SMTP Server id 14.3.408.0; Wed, 6 Mar 2019 16:41:38 +0800 From: Xiongfeng Wang To: , , CC: , Subject: [PATCH v3] posix-cpu-timers: Avoid undefined behaviour in timespec64_to_ns() Date: Wed, 6 Mar 2019 16:40:46 +0800 Message-ID: <1551861646-52541-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 checks whether 'it_interval.tv_sec' is larger than 'KTIME_SEC_MAX' and saturate if that is the case. Signed-off-by: Xiongfeng Wang --- 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(+) diff --git a/kernel/time/posix-timers.c b/kernel/time/posix-timers.c index 0e84bb7..4b57566 100644 --- a/kernel/time/posix-timers.c +++ b/kernel/time/posix-timers.c @@ -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)); -- 1.7.12.4