From patchwork Tue Nov 17 13:03:52 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg KH X-Patchwork-Id: 324925 Delivered-To: patch@linaro.org Received: by 2002:a05:6e02:5ce:0:0:0:0 with SMTP id l14csp4163524ils; Tue, 17 Nov 2020 05:22:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJyZUbmlbvyPYjQxlV0erw4Ez6qN3yeZsYLXttRZqtw6VWbLnI1mlfT+ZJie4m5xY+H6PIpL X-Received: by 2002:a17:906:11c9:: with SMTP id o9mr3590577eja.279.1605619376097; Tue, 17 Nov 2020 05:22:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605619376; cv=none; d=google.com; s=arc-20160816; b=kidN1gUhnbxnV7mshESTqERcP67APO/VrvC3k4OzedHFE/VJV7y+MTQq2BwbtdN40r jOhk/V7Q6xkZNypH0FeT6cqRs1X7b3jkI937W5riUzzMFdpOusEtTau284YnICRS6ElR bziLGFNGuRsRitOlOXdvHaGc4jgw86O/wmmWNddvJ1g9iTU2Eirw0egrD/vRqQmOfiQD 4zV+wr2jIfBN5/ANuKcQbwdrNjwLf4RFXec8JHZgEGQo5UyHl0mgcXoFkCloSJEWcOVB O643BrqijzxXNqzhcwk4UbVTFEPTqOKbEnf4iX+ucXUSIALdUraK/eJcZg50zBFPtBRp PG8Q== 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=8GbS20n3bs+2j7bGQQJ+4tW9U4hmzVR3NZ0aNqoq2gU=; b=NedAS3YFp5dlvZe4mKNDW+Qi95gSAodQAJv6HrUIYx2xxrUFZxadaLamTUeNmOTLwv Xf15pQjmo9RN8l1tWYN3jC0TRvr6zW6Siv8pBRa4+puN0H04BKA/nzbIas6r+FQQwpmY oAEoZx3rcVMztgNrDrxDqfpkXVTK8HRvtfrsnfsx8zsCQe2wCoobVMcXmoz+hjx2U/c6 kl3uWsnQoIa494Y6ls4PhxvqpcW4IN5AGibHjUVUbiK/FR5z0neOwz+Za9yxHmZ4tUL7 Zj/CixJJy2IK7KiXc344XaUxg4trw2ssF2EdW+Sh0uTFgjiYH0SVAvyUqHmekwZWpcoN TcYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gnA8snib; 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 ca2si13919291edb.502.2020.11.17.05.22.55; Tue, 17 Nov 2020 05:22:56 -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=gnA8snib; 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 S1731169AbgKQNWe (ORCPT + 14 others); Tue, 17 Nov 2020 08:22:34 -0500 Received: from mail.kernel.org ([198.145.29.99]:56842 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731165AbgKQNWd (ORCPT ); Tue, 17 Nov 2020 08:22:33 -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 5DD872463D; Tue, 17 Nov 2020 13:22:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605619353; bh=F20bt+0kRKFre1oyDzE4JeYBa8bGfAouPpsSWFfYbk4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gnA8snib4AZModrmc1RsZENH0StA3Rp14YJvgBMbeSV/qGV9/EnKFhiYGkbI0Hjs8 Gx083bnJ8FM8n1zw84z+f4iRpo8WM9t4dsscfYNdgKJnG+U7vgurIwP6wd7ZeHufdJ mm1A4KdgKkbf9GPexKcGVA7Rb3eX9ljO+4OydiPk= 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 5.4 002/151] time: Prevent undefined behaviour in timespec64_to_ns() Date: Tue, 17 Nov 2020 14:03:52 +0100 Message-Id: <20201117122121.506990064@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201117122121.381905960@linuxfoundation.org> References: <20201117122121.381905960@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 ++++ kernel/time/itimer.c | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) -- 2.27.0 diff --git a/include/linux/time64.h b/include/linux/time64.h index 19125489ae948..5eab3f2635186 100644 --- a/include/linux/time64.h +++ b/include/linux/time64.h @@ -132,6 +132,10 @@ static inline bool timespec64_valid_settod(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; } diff --git a/kernel/time/itimer.c b/kernel/time/itimer.c index 77f1e5635cc18..62dc9757118c6 100644 --- a/kernel/time/itimer.c +++ b/kernel/time/itimer.c @@ -147,10 +147,6 @@ static void set_cpu_itimer(struct task_struct *tsk, unsigned int clock_id, u64 oval, nval, ointerval, ninterval; struct cpu_itimer *it = &tsk->signal->it[clock_id]; - /* - * Use the to_ktime conversion because that clamps the maximum - * value to KTIME_MAX and avoid multiplication overflows. - */ nval = ktime_to_ns(timeval_to_ktime(value->it_value)); ninterval = ktime_to_ns(timeval_to_ktime(value->it_interval));