From patchwork Tue Nov 3 20:37:20 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Greg Kroah-Hartman X-Patchwork-Id: 320457 Delivered-To: patch@linaro.org Received: by 2002:a92:7b12:0:0:0:0:0 with SMTP id w18csp4902586ilc; Tue, 3 Nov 2020 12:52:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJzRQpDWKBsaEwWbP7yW8tg4ClX7lfFjyp9OqUNJmZMDI/4w2p+u0+Ht0hYJ02Slb7BjC85U X-Received: by 2002:a17:906:fcc2:: with SMTP id qx2mr12538726ejb.549.1604436777526; Tue, 03 Nov 2020 12:52:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604436777; cv=none; d=google.com; s=arc-20160816; b=JgWandfv4pQQ39BXjUr0sKoSm6UKELC7XKhVvR4s6KWVleGufOccy4J1pFUb0MZJQc kXPEADMZ+FgjdDUt6opyJm/mp0cnE/jlbzAq+hMOTH0389hkv/A1OGMigNwQhDbmhMwo ABmLlYgoBpWIUMMe6Hku6YuxKqYqYzxOmsSccsXvvtQCA/QdnL3iAQ/GXNKu8gJXcVom ssNT4tC0K9+uncCVTb4nbXRYzBKYghxr7cfK8LVMJQqqmzLpqk4FpgP4MmalmvQDQkuY rwwvCXxKY0q70UJRjs2sbDqDWX9BUzWrFKB33CIeQ3m+7aNoC72C3lsAGpMXAAvpwsfq sU/Q== 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=Us4OGwiY6FFWdU02C7H/FBpcCs0gKvn1odWkQEAZSCw=; b=ORkr70iernCaiT/AdAV+9haMtkxEEtBroISKODywWDQtFUSYuPH8J6jiMoazoMDCXQ SPmjV2AJBSEznFqtWiDZVhSYqqF6G7zCj4d59uCGAj0sGOTqfn423visM+YJS890zPXe zrmimw1Knz69/KANE3dxsy6SGUKf20njZCyvFUql53FTIObeKZYOKSg3cwixjDHJ3nUv xS48PeUtytBQ3LGA+mf5s7rX+82ge7MyDDWMp1BnEgIcDG1zxxMmTtAe0t0d0Rzx+g2q OsyA9dbahYglNdzoQS0MsWGxXHCAu0GCDnqwmp9nt9NcmwsvXdF+LhWTIBT9hDVASvSv wZrw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=B9854egf; 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 g14si2191288edp.246.2020.11.03.12.52.57; Tue, 03 Nov 2020 12:52:57 -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=B9854egf; 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 S1732165AbgKCUwy (ORCPT + 15 others); Tue, 3 Nov 2020 15:52:54 -0500 Received: from mail.kernel.org ([198.145.29.99]:50130 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732176AbgKCUwy (ORCPT ); Tue, 3 Nov 2020 15:52:54 -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 523B9223BD; Tue, 3 Nov 2020 20:52:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1604436772; bh=Ejj2lQd3if6gvR5Fuw0RwKBYxEi4IHn7T7kxLxdHEtc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=B9854egfStgRhx6vMG19csKRvhJzzPPVzPjAYomprFqldOk8yA9ZBWI4IHi5zDKcU 7eookOU6glZN48O7yUpwQJnXI6EEHmeH4DpFI103loaGP51rUhmUKz3xeqz8+Tt0QC 79TH4gPhy37+fAsztSsaWT6n6G+mq8/ECM2PjU0s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Zeng Tao , Thomas Gleixner , Arnd Bergmann Subject: [PATCH 5.9 389/391] time: Prevent undefined behaviour in timespec64_to_ns() Date: Tue, 3 Nov 2020 21:37:20 +0100 Message-Id: <20201103203413.413569789@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201103203348.153465465@linuxfoundation.org> References: <20201103203348.153465465@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 commit cb47755725da7b90fecbb2aa82ac3b24a7adb89b upstream. 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: Greg Kroah-Hartman --- include/linux/time64.h | 4 ++++ kernel/time/itimer.c | 4 ---- 2 files changed, 4 insertions(+), 4 deletions(-) --- a/include/linux/time64.h +++ b/include/linux/time64.h @@ -124,6 +124,10 @@ static inline bool timespec64_valid_sett */ 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; } --- a/kernel/time/itimer.c +++ b/kernel/time/itimer.c @@ -172,10 +172,6 @@ static void set_cpu_itimer(struct task_s 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 = timespec64_to_ns(&value->it_value); ninterval = timespec64_to_ns(&value->it_interval);