From patchwork Tue Nov 17 13:04:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 324582 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:5ce:0:0:0:0 with SMTP id l14csp4155355ils; Tue, 17 Nov 2020 05:11:10 -0800 (PST) X-Google-Smtp-Source: ABdhPJzHhEmh1OYOCKDQAVVwtUEChSTxKP75SyAUm6vmjl9QNfj4YEXykAmtopWfsw8vRtzXVOnt X-Received: by 2002:a05:6402:553:: with SMTP id i19mr20067659edx.194.1605618670802; Tue, 17 Nov 2020 05:11:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605618670; cv=none; d=google.com; s=arc-20160816; b=uXsTrTE2BR6PhEygFZeZEZNeXr0LHeAQyEhC3XE6zaryTXsbc3vESSysZ5MwLCjGr3 KLr1GK+S5mz6pdjO9Do6m8a/3XpoNhdMZMsKqaFdIlNQMBYokdMYY8I8oJmfVgq7hHUj Z67GbRY97iqqg2lsg/rkqx4xOGCF1MtoVTDsdr2z218rHqGXUIihSmwnCNajVuXmh/OC bs/2l3bgjq2DLNEkWD+hG3ihR7UPOiV2hcHA4MQflmykftCBUXT0oFKLtC9Pg8OrWYIp 6vuOrvw4aCoa/kSpWCiUmkCJL2phPe+aa33F3JDBoPJ5Mo6BmBFswvYDdgZZxrqn8RoE 5U5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qrOb86k/4f1dUJ8maDwVj8JjA4XH6kahJurJ/Fj5d0w=; b=OznIFicULKCvtUXP8IkuFTMVh+CK5joNSYdSH7yC6JBscfiU1XVMo+2EkgIBZZNSRJ 9fFOZf1JnZVcZKC0fLjWPfkJfxIcfiGZNNRP7dKL5hLfOxYsDl6ewbJHrrIev2yUu8sR 9NjkK3f4KPGAq+aJQBZPjqN94xxK5yFPp9MfTB0Hox14RlaTjiKQmY8MyQayjDNW4ZhO 1Spk6L4a++Ad1Oi/3VVWsDsbbpNIyAtqmJNc2b+3nIByLAnPJKEs6xenNkr0uTH6gVeA 513NNp2Du/3595WTp5V6VmYpwiF+LWSNiVjT5LvPQiKd1F1LK1SukU7FV+953YGdNN0o A7tA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LviORb2F; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id bu6si14358881edb.94.2020.11.17.05.11.10; Tue, 17 Nov 2020 05:11:10 -0800 (PST) Received-SPF: pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LviORb2F; spf=pass (google.com: domain of stable-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=stable-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729507AbgKQNKk (ORCPT + 14 others); Tue, 17 Nov 2020 08:10:40 -0500 Received: from mail.kernel.org ([198.145.29.99]:40038 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729503AbgKQNKj (ORCPT ); Tue, 17 Nov 2020 08:10:39 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0AACB2225B; Tue, 17 Nov 2020 13:10:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605618638; bh=2dQHQ4y/7d2lTuy439M7DmWm45Nj0t27ZhtaEcZzszQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LviORb2FZGYH22hK8irrlcKimqH04wLeuv2R8llDuhCGoYXi+wDcjjzAs9VY6Jb3O f/kdjDwJlUyiEv10PBrHq1M7yL/00RlgHYn2coPz2V58IdJ830F4gum/hgdKHWm3Ik Zxmikt8n5bs9a7n8fGCZJAmL59ESnRwrEkpGwT3g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zeng Tao , Thomas Gleixner , Arnd Bergmann , Sasha Levin Subject: [PATCH 4.9 05/78] time: Prevent undefined behaviour in timespec64_to_ns() Date: Tue, 17 Nov 2020 14:04:31 +0100 Message-Id: <20201117122109.366986454@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201117122109.116890262@linuxfoundation.org> References: <20201117122109.116890262@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Zeng Tao [ Upstream commit cb47755725da7b90fecbb2aa82ac3b24a7adb89b ] UBSAN reports: Undefined behaviour in ./include/linux/time64.h:127:27 signed integer overflow: 17179869187 * 1000000000 cannot be represented in type 'long long int' Call Trace: timespec64_to_ns include/linux/time64.h:127 [inline] set_cpu_itimer+0x65c/0x880 kernel/time/itimer.c:180 do_setitimer+0x8e/0x740 kernel/time/itimer.c:245 __x64_sys_setitimer+0x14c/0x2c0 kernel/time/itimer.c:336 do_syscall_64+0xa1/0x540 arch/x86/entry/common.c:295 Commit bd40a175769d ("y2038: itimer: change implementation to timespec64") replaced the original conversion which handled time clamping correctly with timespec64_to_ns() which has no overflow protection. Fix it in timespec64_to_ns() as this is not necessarily limited to the usage in itimers. [ tglx: Added comment and adjusted the fixes tag ] Fixes: 361a3bf00582 ("time64: Add time64.h header and define struct timespec64") Signed-off-by: Zeng Tao Signed-off-by: Thomas Gleixner Reviewed-by: Arnd Bergmann Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/1598952616-6416-1-git-send-email-prime.zeng@hisilicon.com Signed-off-by: Sasha Levin --- include/linux/time64.h | 4 ++++ 1 file changed, 4 insertions(+) -- 2.27.0 diff --git a/include/linux/time64.h b/include/linux/time64.h index 980c71b3001a5..2a45b8c87edbf 100644 --- a/include/linux/time64.h +++ b/include/linux/time64.h @@ -188,6 +188,10 @@ static inline bool timespec64_valid_strict(const struct timespec64 *ts) */ static inline s64 timespec64_to_ns(const struct timespec64 *ts) { + /* Prevent multiplication overflow */ + if ((unsigned long long)ts->tv_sec >= KTIME_SEC_MAX) + return KTIME_MAX; + return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; }