Received: by 2002:ac0:aed5:0:0:0:0:0 with SMTP id t21csp3720855imb; Tue, 5 Mar 2019 17:37:56 -0800 (PST) X-Google-Smtp-Source: APXvYqycWVvXA3Zr6Dxe3auaB6ggWlr+u3GiAq1uIGpqLFpMTnVlNjslzYW6hrFy4zOBSORtun6s X-Received: by 2002:a17:902:298a:: with SMTP id h10mr4316533plb.312.1551836276884; Tue, 05 Mar 2019 17:37:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1551836276; cv=none; d=google.com; s=arc-20160816; b=okaJdehY2kxaWXqfl7OLiFU+gEAqNI9sE2+MtsI8+zpJ32s0bOOJoZqtAUa/L2soj8 Uwz0pw5ml0R3P9pca9cJw1w7OMAdI3BeGAdeEqiB7BebvEBrld9DaCaAYgL9NUjr2xRz RO2ZJ9fCe2w4ZZUgBEcUMFtq4x/fb+gJVHvlR7K7dwrVzhfCvwTSx0YjxcT6h0JgcFYH yUDo0J4xk5jWOoat5SA4shIaUrXd75xS1aBZ3vacwa8d8xYcvr9fjKkGJyfLDpbR9U6u u+p+idbAby8z7aKWSFsrLSEuRYyHC+ug8RkmbMu3uVLHjySfYaZn6czDCCmnstWleK/G 98CQ== 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=WskobZT8HyLTaj15p0YlXBONrB2Jy3dS36ZN5i/O4k8=; b=nUWClZSoSJwj3yNGfUSw8nJaTBz01vm79pYWQqf8MIjJxv0NMms1hMJyolKBhPlQ67 r1kHkOfqs04ZOJm8smN6+mDlRFKBCxhHTXcxKYigPJpYetCoLudCylnLz3aGcwA8GEW1 c2kE2Z0B3PUzv5PzIWzGNll+i/hz/PblUmw3a4XkSXQdXoX2jcmr8yrgO6KRLe8lN9Kc qt7E0N/RgZdBXJxO1K1yy5ue3SWplHJRCOm1vhuQd0aKm8FzKDMVnH23Iapk36S69upD 5FmANVN6A6+KDwSWbUKacJK0M3KDnq9lTlcs64/cqJFHkScTrLxCO4u9pBLvNmRdsMDh w/xA== 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 p65si349105pfa.27.2019.03.05.17.37.41; Tue, 05 Mar 2019 17:37:56 -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 S1728447AbfCFB3j (ORCPT + 99 others); Tue, 5 Mar 2019 20:29:39 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:4772 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726210AbfCFB3i (ORCPT ); Tue, 5 Mar 2019 20:29:38 -0500 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by Forcepoint Email with ESMTP id C0B61644CA90D1FDDBBC; Wed, 6 Mar 2019 09:29:36 +0800 (CST) Received: from linux-ibm.site (10.175.102.37) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.408.0; Wed, 6 Mar 2019 09:29:26 +0800 From: Xiongfeng Wang To: , , CC: , Subject: [RFC PATCH] ntp: Avoid undefined behaviour in second_overflow() Date: Wed, 6 Mar 2019 09:28:30 +0800 Message-ID: <1551835710-53773-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/ntp.c:457:16 signed integer overflow: 9223372036854775807 + 500 cannot be represented in type 'long int' CPU: 3 PID: 0 Comm: swapper/3 Not tainted 4.19.25-dirty #2 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 second_overflow+0x403/0x540 kernel/time/ntp.c:457 accumulate_nsecs_to_secs kernel/time/timekeeping.c:2002 [inline] logarithmic_accumulation kernel/time/timekeeping.c:2046 [inline] timekeeping_advance+0x2bb/0xec0 kernel/time/timekeeping.c:2114 tick_do_update_jiffies64.part.2+0x1a0/0x350 kernel/time/tick-sched.c:97 tick_do_update_jiffies64 kernel/time/tick-sched.c:1229 [inline] tick_nohz_update_jiffies kernel/time/tick-sched.c:499 [inline] tick_nohz_irq_enter kernel/time/tick-sched.c:1232 [inline] tick_irq_enter+0x1fd/0x240 kernel/time/tick-sched.c:1249 irq_enter+0xc4/0x100 kernel/softirq.c:353 entering_irq arch/x86/include/asm/apic.h:517 [inline] entering_ack_irq arch/x86/include/asm/apic.h:523 [inline] smp_apic_timer_interrupt+0x20/0x480 arch/x86/kernel/apic/apic.c:1052 apic_timer_interrupt+0xf/0x20 arch/x86/entry/entry_64.S:864 RIP: 0010:native_safe_halt+0x2/0x10 arch/x86/include/asm/irqflags.h:58 Code: 01 f0 0f 82 bc fd ff ff 48 c7 c7 c0 21 b1 83 e8 a1 0a 02 ff e9 ab fd ff ff 4c 89 e7 e8 77 b6 a5 fe e9 6a ff ff ff 90 90 fb f4 0f 1f 00 66 2e 0f 1f 84 00 00 00 00 00 f4 c3 90 90 90 90 90 90 RSP: 0018:ffff888106307d20 EFLAGS: 00000246 ORIG_RAX: ffffffffffffff13 RAX: 0000000000000007 RBX: dffffc0000000000 RCX: 0000000000000000 RDX: 0000000000000000 RSI: 0000000000000001 RDI: ffff8881062e4f1c RBP: 0000000000000003 R08: ffffed107c5dc77b R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000000 R12: ffffffff848c78a0 R13: 0000000000000003 R14: 1ffff11020c60fae R15: 0000000000000000 arch_safe_halt arch/x86/include/asm/paravirt.h:94 [inline] default_idle+0x24/0x2b0 arch/x86/kernel/process.c:561 cpuidle_idle_call kernel/sched/idle.c:153 [inline] do_idle+0x2ca/0x420 kernel/sched/idle.c:262 cpu_startup_entry+0xcb/0xe0 kernel/sched/idle.c:368 start_secondary+0x421/0x570 arch/x86/kernel/smpboot.c:271 secondary_startup_64+0xa4/0xb0 arch/x86/kernel/head_64.S:243 ================================================================================ It is because time_maxerror is set as 0x7FFFFFFFFFFFFFFF by user. It overflows when we add it with 'MAXFREQ / NSEC_PER_USEC' in 'second_overflow()'. This patch add a limit check and saturate it when the user set 'time_maxerror'. Signed-off-by: Xiongfeng Wang --- kernel/time/ntp.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/time/ntp.c b/kernel/time/ntp.c index 36a2bef..38e1b65 100644 --- a/kernel/time/ntp.c +++ b/kernel/time/ntp.c @@ -677,6 +677,8 @@ static inline void process_adjtimex_modes(const struct timex *txc, s32 *time_tai if (txc->modes & ADJ_MAXERROR) time_maxerror = txc->maxerror; + if (time_maxerror > NTP_PHASE_LIMIT) + time_maxerror = NTP_PHASE_LIMIT; if (txc->modes & ADJ_ESTERROR) time_esterror = txc->esterror; -- 1.7.12.4