Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp25426pxb; Mon, 13 Sep 2021 11:59:21 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLdymQ+gQo17L/bqg7HJ/D7hbFHP72HerpcGiMaa5xZ9y943vnTr8aL2awz3Slm+GRD2Fv X-Received: by 2002:a17:906:6943:: with SMTP id c3mr14301245ejs.550.1631559560922; Mon, 13 Sep 2021 11:59:20 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631559560; cv=none; d=google.com; s=arc-20160816; b=EsR4qU2K8e9uVRgraSLpbISrgFdyME7qy0C2AnM1txakXakW4+Xs/dL0c3+cNXARxC DOsQhGxZN4ZGMT4l1fn5L2URuch9qJ28pYOQSMgadYjFTdYapznlGF6kU/rpbU1ZK0qt YyuEVI4wB0j5fYbF8dk75Ofgs1bsjQVf/P/806qPJRvienNQJvy/DvSD6y4hI+NdFqlJ ga+g70O2mZRlWS3sggiGCvLz8yvwCPA6KVUYtdXxX0Qpb/pxYynF2PJdDSr5rxLRQlBv IXrDisPpKvw6+Scd3fxh5/1Gg4IL+hx0PlJk5/+TQVncdjMkTzv9KSaI9GHFgbVLE4cP nskA== 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=nojFfTSuFuz+bH1I3rH8aOJf1Szf5tBOuUGmuTHhZec=; b=BLgNHlSeXj+8RHxItdr7rQE5gmWzjjJ8amSoNjz8Dd8QJwRXjlKHJFH3u8SwiKxYRH MUR7wlokJtseD+jyxIG8sHJhZcREEkojW8VnEK9iQiilvBaWEe1FAsYBQspg2cP3VQUT YVajBm8mLWRrXHyz+arH3A4BDyMFToiThvDimpR88imYwrDjFIX6Yq3P62BzacuqEHCW a7nS/G8zYjDO7MPvfa2NtwaNzkq2JTUm9gRHM1C1tqLoKPz49dZDC/zJYsS0wCGRP6d1 IVbslw1w4HmbDGFULPVwsCBwB4VpiuHk/NnDrZ2ElZdVlS19YZ8v42d1TfBUgsOKvtGt VYHg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Sf7d39Jz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (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 bi16si9533941edb.381.2021.09.13.11.58.46; Mon, 13 Sep 2021 11:59:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@linuxfoundation.org header.s=korg header.b=Sf7d39Jz; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240535AbhIMN3C (ORCPT + 99 others); Mon, 13 Sep 2021 09:29:02 -0400 Received: from mail.kernel.org ([198.145.29.99]:49440 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241374AbhIMN0u (ORCPT ); Mon, 13 Sep 2021 09:26:50 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 8BA1960FA0; Mon, 13 Sep 2021 13:22:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539380; bh=xYUUG1GYepWWzLoXPYBfuiP6JRYKZibY1PSpA4aIk7Q=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Sf7d39JzQLFepS1YNAYwBE130QXWTxPeZWVXLSWiSciCS1JgfdDMl+toawslD+rAA aih9OC7w71PCR7YJU3ESwk2pEcz+ZHBfk74/bLTeW9DG0U8uz+l2ssiRAhifqtDy3W NcQsS7jydMT1wTsjuXrc4wVrNqSCEhy7H2kZRn3o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lukas Hannen , Thomas Gleixner Subject: [PATCH 5.4 131/144] time: Handle negative seconds correctly in timespec64_to_ns() Date: Mon, 13 Sep 2021 15:15:12 +0200 Message-Id: <20210913131052.308441540@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131047.974309396@linuxfoundation.org> References: <20210913131047.974309396@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lukas Hannen commit 39ff83f2f6cc5cc1458dfcea9697f96338210beb upstream. timespec64_ns() prevents multiplication overflows by comparing the seconds value of the timespec to KTIME_SEC_MAX. If the value is greater or equal it returns KTIME_MAX. But that check casts the signed seconds value to unsigned which makes the comparision true for all negative values and therefore return wrongly KTIME_MAX. Negative second values are perfectly valid and required in some places, e.g. ptp_clock_adjtime(). Remove the cast and add a check for the negative boundary which is required to prevent undefined behaviour due to multiplication underflow. Fixes: cb47755725da ("time: Prevent undefined behaviour in timespec64_to_ns()")' Signed-off-by: Lukas Hannen Signed-off-by: Thomas Gleixner Cc: stable@vger.kernel.org Link: https://lore.kernel.org/r/AM6PR01MB541637BD6F336B8FFB72AF80EEC69@AM6PR01MB5416.eurprd01.prod.exchangelabs.com Signed-off-by: Greg Kroah-Hartman --- include/linux/time64.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) --- a/include/linux/time64.h +++ b/include/linux/time64.h @@ -33,7 +33,9 @@ struct itimerspec64 { #define TIME64_MIN (-TIME64_MAX - 1) #define KTIME_MAX ((s64)~((u64)1 << 63)) +#define KTIME_MIN (-KTIME_MAX - 1) #define KTIME_SEC_MAX (KTIME_MAX / NSEC_PER_SEC) +#define KTIME_SEC_MIN (KTIME_MIN / NSEC_PER_SEC) /* * Limits for settimeofday(): @@ -132,10 +134,13 @@ 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) + /* Prevent multiplication overflow / underflow */ + if (ts->tv_sec >= KTIME_SEC_MAX) return KTIME_MAX; + if (ts->tv_sec <= KTIME_SEC_MIN) + return KTIME_MIN; + return ((s64) ts->tv_sec * NSEC_PER_SEC) + ts->tv_nsec; }