Received: by 2002:ac0:b08d:0:0:0:0:0 with SMTP id l13csp4460695imc; Mon, 25 Feb 2019 05:25:01 -0800 (PST) X-Google-Smtp-Source: AHgI3IZWARi0BUliaJintkaqvJtBQPJFbESz21lJJlhSNfBH05ctX0OBx8BI7uyTrNh234GUQKNt X-Received: by 2002:a17:902:b416:: with SMTP id x22mr20642042plr.285.1551101101777; Mon, 25 Feb 2019 05:25:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551101101; cv=none; d=google.com; s=arc-20160816; b=CJBXJidrgisZno/fXPMBcOlNWj8eMywsuNH2VXZCJzWN1g8Gjc+7Z0AENed3l7FQTa V3vZm95Jx8g34nFRd7ihk8VZzrYOYPda/YLoGNhLaoCUeh3NSzBwVtstGa+CKsRnY3bp 6qOZAomQfINmK2pxGJCld/7/x1dOIsM2aZ6ttce7dxwLcDoMb5mBOF+cgCiimCQ/tUEe xzbsWIZsQVHgn7dRGXwS1G6LUUMNNyqLxjXAFO7uytJmz3dg/ybQyeduCaLMs9nhOR/y BkB7IcsxzxqBaSI92h/WHWnngWuIvlu0H7GgmeXP5D2XTrOZNPKngVtCDwNw6a78QUbD +QJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=ZMuX0WpftLieulcyhpwIucyzQOIPlXb3sT8Swxhhjjs=; b=dENzoUmWKaY+DU3TE8/5M5GRH/NlD1bgXN6ltbyk8YC3ujYcD5cY36hinDQmgADSSO MHKg03fDup7mVKwpjOl8HR/z2Gb/Kpy7tA02cpYY2j2bYsIjjkjLBCaRlJpubfbgmHMf kpDdv3UbJIZOqj3R9UhuTr5pGYQjgIq1UF1WokZfDy/A8ckAfwCVVMZxfJe9sFrgelA0 cHxCtTu8vZzhiGj03VmDhI7D+koR5VPlpBmNcrlpacGCzaiaCOaQIknBcWjQTgTX5qR9 MsAqeWl9l7KxCHfXkj+XBD7p16z3V3oxE94+E4ogeu7W1Uk61PdzDhY/sUq4SvB5UZwb /CDQ== 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 d63si9579849pfg.40.2019.02.25.05.24.46; Mon, 25 Feb 2019 05:25:01 -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 S1727176AbfBYNYN (ORCPT + 99 others); Mon, 25 Feb 2019 08:24:13 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:4731 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726921AbfBYNYN (ORCPT ); Mon, 25 Feb 2019 08:24:13 -0500 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.58]) by Forcepoint Email with ESMTP id 5AA60AFB2DEFA92FBEEE for ; Mon, 25 Feb 2019 21:24:11 +0800 (CST) Received: from [127.0.0.1] (10.57.71.8) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server id 14.3.408.0; Mon, 25 Feb 2019 21:24:05 +0800 Subject: Re: [PATCH] time64: Avoid undefined behaviour in timespec64_add() To: Deepa Dinamani CC: Thomas Gleixner , Arnd Bergmann , "Linux Kernel Mailing List" , References: <20190225032619.17070-1-yaohongbo@huawei.com> From: Yao HongBo Message-ID: <6a50f6ed-aa5f-cf3b-71c5-ebd9cbfc29f6@huawei.com> Date: Mon, 25 Feb 2019 21:22:01 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.57.71.8] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 2/25/2019 12:53 PM, Deepa Dinamani wrote: > On Sun, Feb 24, 2019 at 7:13 PM Hongbo Yao wrote: >> >> I ran into this: >> ========================================================================= >> UBSAN: Undefined behaviour in ./include/linux/time64.h:70:2 >> signed integer overflow: >> 1551059291 + 9223372036854775807 cannot be represented in type 'long >> long int' >> CPU: 5 PID: 20064 Comm: syz-executor.2 Not tainted 4.19.24 #4 >> 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 >> timespec64_add include/linux/time64.h:70 [inline] >> timekeeping_inject_offset+0x3ed/0x4e0 kernel/time/timekeeping.c:1301 >> do_adjtimex+0x1e5/0x6c0 kernel/time/timekeeping.c:2360 >> __do_sys_clock_adjtime+0x122/0x200 kernel/time/posix-timers.c:1086 >> 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:00007f888aa2dc58 EFLAGS: 00000246 ORIG_RAX: 0000000000000131 >> RAX: ffffffffffffffda RBX: 000000000073bf00 RCX: 0000000000462eb9 >> RDX: 0000000000000000 RSI: 00000000200003c0 RDI: 0000000000000000 >> RBP: 0000000000000002 R08: 0000000000000000 R09: 0000000000000000 >> R10: 0000000000000000 R11: 0000000000000246 R12: 00007f888aa2e6bc >> R13: 00000000004bcae8 R14: 00000000006f6868 R15: 00000000ffffffff >> ========================================================================== >> >> Since lhs.tv_sec and rhs.tv_sec are both time64_t, this is a signed >> addition which will cause undefined behaviour on overflow. >> >> The easiest way to avoid the overflow is to cast one of the arguments to >> unsigned (so the addition will be done using unsigned arithmetic). >> This patch doesn't change generated code. >> >> Signed-off-by: Hongbo Yao >> --- >> include/linux/time64.h | 2 +- >> 1 file changed, 1 insertion(+), 1 deletion(-) >> >> diff --git a/include/linux/time64.h b/include/linux/time64.h >> index 05634afba0db..5926bdd4167f 100644 >> --- a/include/linux/time64.h >> +++ b/include/linux/time64.h >> @@ -67,7 +67,7 @@ static inline struct timespec64 timespec64_add(struct timespec64 lhs, >> struct timespec64 rhs) >> { >> struct timespec64 ts_delta; >> - set_normalized_timespec64(&ts_delta, lhs.tv_sec + rhs.tv_sec, >> + set_normalized_timespec64(&ts_delta, (timeu64_t)lhs.tv_sec + rhs.tv_sec, >> lhs.tv_nsec + rhs.tv_nsec); >> return ts_delta; >> } > > There is already a timespec64_add_safe() to account for such > overflows. That assumes both the timespec64 values are positive. > But, timekeeping_inject_offset() cannot use that as one of the values > can be negative. Thanks for your reply. > Are you running some kind of a fuzzer that would cause a overflow? Yes, I am running syzkaller testsuite. > You seem to be adding INT64_MAX here. Maybe the right thing to do is > to add a check at the syscall interface rather than here. Thanks for this suggestion. Looks like that is a better way. I will try it. Thanks, HongBo > -Deepa > > . >