Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp180479pxb; Mon, 13 Sep 2021 16:23:58 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyKkdmeQPUnF+Q5iuydIgq8op8uW6aSCcGBqt4AQaOK10G6BU50kHOEInAQv8kzpcrT3ef2 X-Received: by 2002:a17:906:2649:: with SMTP id i9mr15238764ejc.1.1631575437872; Mon, 13 Sep 2021 16:23:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631575437; cv=none; d=google.com; s=arc-20160816; b=ea6N7F+tvBZkqi7LXBwJslRPAu45G+/wREDhnajB309oYmNxtqErODkBgY7FVh9e2g /VLzO8/jy2eeSVobV8tPCQYh6/ZL40c8dvA+gk0s4d59cwKvVAdAc134O7t8VJBdQ4q2 tyVKTfpR30z9muhfzHPAxGxgEgK8ohetx/xlzZi+gcpZNXLx+RTW/MWwcl9TViqNwUSe ecJRw4Hi6gXGdVr7Hh07vJZFXacaCztC0k1/qHY3+OfhdjfLR9kTykLQQ+b6jklCwAzt /pK9p5mtS3p1APr/+q8FQJjcTa8b4Tsl9k6osGMDnbS9bdyMayRsqfquLa7ga5wQP3qv 2eww== 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=uDnCZuZxxLKQGGWOdJc/RkHrn79VPKc2vaQ59Ptw4qA=; b=K8/rQc8nowx213Tiz8VbplIyqKSZ8GCnA/eXV7/WSvrbyioR5DZlcPSFsgXobTiJ8X 4XyQt9Wx+35igiaUCOnhVD2dSUHywobr57ZmeLFom1fSFvZq21dHYKg2CKX+EMd/8XS3 e324nUXd9teejBjIqmMEaUYP7cOXCLPg4KpUW6lEuR919qeTaHCjfWWdEUxtsmHbTl86 NTPwnDG6XvvgXZSYaAMO023VrDxQbgfpxLe20GBPN9mBu0QC92fwcuhvZBC1SEsay1qC PJjSBi1AicSJYOmgBQP3HAda9x/aE8K8rvS2HENFT19rI1OvOXkZYJigTcx2L8utjetC OawA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=HKlpk9FO; 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 bs11si1413332ejb.438.2021.09.13.16.23.34; Mon, 13 Sep 2021 16:23:57 -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=HKlpk9FO; 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 S241296AbhIMOsr (ORCPT + 99 others); Mon, 13 Sep 2021 10:48:47 -0400 Received: from mail.kernel.org ([198.145.29.99]:58768 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345634AbhIMOoK (ORCPT ); Mon, 13 Sep 2021 10:44:10 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 75EE96320E; Mon, 13 Sep 2021 13:57:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631541441; bh=qVu/R1PERx92QevYEvJMZewyoqWI0tU6zqzoEYAu/xw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HKlpk9FOwMlN8RXg82OtSUEF8pJcuPhGTlx1phsyfhJn9UvijPAZkz0zwziLW2yRY FSL5wnCCMs5lOeTXPHWu3hLY/V3Vu9kWKqj8izu6+WBetQKKmJ2xtTpQQdCnXwfICN z4EN2QWJikh6vxmgzdzZ2F9wLh+LfFXXWxO6k/zw= 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.14 298/334] time: Handle negative seconds correctly in timespec64_to_ns() Date: Mon, 13 Sep 2021 15:15:52 +0200 Message-Id: <20210913131123.500712780@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131113.390368911@linuxfoundation.org> References: <20210913131113.390368911@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 @@ -25,7 +25,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(): @@ -124,10 +126,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; }