Received: by 2002:ac0:8845:0:0:0:0:0 with SMTP id g63csp1568504img; Wed, 27 Feb 2019 01:25:09 -0800 (PST) X-Google-Smtp-Source: AHgI3IYwKeXHEMqnSy3/fjaOAaMpuGNhmhLABWsZQTLkSkabnlgh7WH2C1k2AsxjNJ12FXsWZB3e X-Received: by 2002:a17:902:56a:: with SMTP id 97mr1146368plf.15.1551259509411; Wed, 27 Feb 2019 01:25:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551259509; cv=none; d=google.com; s=arc-20160816; b=H3PotuMUM/5dAxEqGEa86uwZD20o8jYd2Fl6Z5w2XuWXoNPr9VhepRaFZ2CXYWeJui +yhzrMVPRxX89ORBxBCsY1Em8coorqBjOPwQB2N1kg6kajQJEJX5k14KlOvNYjUODyIy BRP9kxI7YXl2ufdmy0xhDf+5PZxzIw5OxdTDp0931wMJgSNx4P2airxe4ZDmzySdP2UG 67Dy6g/A+d84tUI1H6sNqCXq83ykVBvDN8iB1xN/G955La1LcM8dOuVuKL+qt/6ZFYL3 5ijDwcCnana2fgkKdmFYBCUFIK7Luj2z1bZZAn3dbK23bxrxr5IbGfXoydZKUIdKXdEL JILA== 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=1kLvUeke+W3b6jQIk5ZrxUYOfQqTfRG/mdNvAkU7PFU=; b=Y2o5NUocN5sRO7GFUAbn8Rc53UwSLLAjMAmKHcAt6DSrlVhjbJOleGvtHFc3TeXW7r PX/hJ7lCw9Ahfhhd4y+QY0TcjygTMn0qVY9Rwt//eKJcbUzG8zuf3D8p1FRUaYwHftck pPABBSUnyuvX2WFNmq1B1uYE2AvfaKloCzWOxqvRWfGXgWecIgfdBQDHBXYAi+zuv20R MiuJ5qPma9owqHnTbFjtauGlOer+rAoXqvgKqBg1dsor4VatNz8Lh2XObaElnr+eQ5X6 AuJ12Zan94KfxwEF6MQOlXPXC0sZHUlA1EERgOOAm3LbKpAoeMDrlavWlkrENfigDbUW QQQA== 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 p8si12422865plk.257.2019.02.27.01.24.54; Wed, 27 Feb 2019 01:25:09 -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 S1728692AbfB0JX4 (ORCPT + 99 others); Wed, 27 Feb 2019 04:23:56 -0500 Received: from szxga06-in.huawei.com ([45.249.212.32]:44564 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725881AbfB0JX4 (ORCPT ); Wed, 27 Feb 2019 04:23:56 -0500 Received: from DGGEMS408-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 1BCBC98127135C782527; Wed, 27 Feb 2019 17:23:54 +0800 (CST) Received: from linux-ibm.site (10.175.102.37) by DGGEMS408-HUB.china.huawei.com (10.3.19.208) with Microsoft SMTP Server id 14.3.408.0; Wed, 27 Feb 2019 17:23:47 +0800 From: Xiongfeng Wang To: , , CC: , Subject: [RFC PATCH] timekeeping: Avoid undefined behaviour in 'ktime_get_with_offset()' Date: Wed, 27 Feb 2019 17:23:01 +0800 Message-ID: <1551259381-15907-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 kernel/time/timekeeping.c:801:8 signed integer overflow: 500152103386 + 9223372036854775807 cannot be represented in type 'long long int' CPU: 6 PID: 13904 Comm: syz-executor.0 Not tainted 4.19.25 #5 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1ubuntu1 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 ktime_get_with_offset+0x26a/0x2d0 kernel/time/timekeeping.c:801 common_hrtimer_arm+0x14d/0x220 kernel/time/posix-timers.c:817 common_timer_set+0x337/0x530 kernel/time/posix-timers.c:863 do_timer_settime+0x198/0x290 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:00007f7968072c58 EFLAGS: 00000246 ORIG_RAX: 00000000000000df RAX: ffffffffffffffda RBX: 000000000073bf00 RCX: 0000000000462eb9 RDX: 00000000200000c0 RSI: 0000000000000000 RDI: 0000000000000000 RBP: 0000000000000004 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000246 R12: 00007f79680736bc R13: 00000000004c54cc R14: 0000000000704278 R15: 00000000ffffffff ================================================================================ It it because global variable 'offsets' is set with a very large but still valid value. It overflows when we add 'tk->tkr_mono.base' with 'offsets'. Because 'ktime_get_with_offset()' is a frequently used function, it may effect the performance if we use 'ktime_add_safe()' to avoid this undefined behaviour, so we use 'ktime_add_unsafe()' instead. Signed-off-by: Xiongfeng Wang --- kernel/time/timekeeping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/time/timekeeping.c b/kernel/time/timekeeping.c index ac5dbf2..f9c39a6 100644 --- a/kernel/time/timekeeping.c +++ b/kernel/time/timekeeping.c @@ -792,7 +792,7 @@ ktime_t ktime_get_with_offset(enum tk_offsets offs) do { seq = read_seqcount_begin(&tk_core.seq); - base = ktime_add(tk->tkr_mono.base, *offset); + base = ktime_add_unsafe(tk->tkr_mono.base, *offset); nsecs = timekeeping_get_ns(&tk->tkr_mono); } while (read_seqcount_retry(&tk_core.seq, seq)); -- 1.7.12.4