Received: by 2002:a05:6a11:4021:0:0:0:0 with SMTP id ky33csp101216pxb; Mon, 13 Sep 2021 14:07:25 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzc0iAhL5F1xIQg2P6Q6KVNpyvprXqv5Z0DAKxoSL3rANpWiSP0UVQb8c2G4KLETPR2+KM+ X-Received: by 2002:a92:c70e:: with SMTP id a14mr8885009ilp.299.1631567245577; Mon, 13 Sep 2021 14:07:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1631567245; cv=none; d=google.com; s=arc-20160816; b=Ho9c+I5XrZEYCF5hcOdARNG9ZC3F9H4bygfux0UJ6fyfvPwzZA04fVkHKG/STcQFs7 iuTh50oav69+LWacyAQkrqHfqfeezaPNJ+F/USCnL8dLT8NDCn/WoWr/bosm/RJXNnfO KYtnI1wA/+Ll+k4zaWKOOB9K7saB0xj/+Ls2XupfYmfQPGFvzW1xJCvq8iVQYkryOW+g fNAjSsOjz9Mcgu9LgvtALA5CUVxu5AxgGyH2wgETbJtWegmSBlqZ3Yb7u63oaSAZRYOb 2jZG9CvKSWaaoni3f5lzI46qa3DVbOJhV+++oGRomoRSXlWIV8Ef6pzK88AMq0mz/EKZ 3rDA== 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=k7c8t/wm7/9ATFuyrLQ8ASodUQPpInDs+JfX9MOJPF7uqlL072Zxl5tfXO5LXmw+vA l7c1+8ehUA3aeZMxf7L6truO0+1KpaXCHPnkOwTFoOH2UXNgOghrVDd+syNdbqFS6yQg A4ydAVI6nBnmbsKzwNCG+sctrp8oNmE/sVXdV4kChmxv2vhkoW7zc44T1VBJEVak3CgN A/YlAw3cw9cPVu8WsO4g9Om7K4SyHcUNCJo2mQs1wnQls7sesJoML6DnQC/GdnRxDzKr /rU722ZFuNSpCOZk2rcS7vraOcNvYq2RTn8E1ZRXHiDWDqhswlT1Sd+QM8ukbJL8MLfO xJwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=Y67IXkT5; 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 b135si7511048iof.27.2021.09.13.14.07.14; Mon, 13 Sep 2021 14:07:25 -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=Y67IXkT5; 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 S244836AbhIMNup (ORCPT + 99 others); Mon, 13 Sep 2021 09:50:45 -0400 Received: from mail.kernel.org ([198.145.29.99]:51782 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243749AbhIMNqw (ORCPT ); Mon, 13 Sep 2021 09:46:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 03B516137E; Mon, 13 Sep 2021 13:32:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1631539928; bh=qVu/R1PERx92QevYEvJMZewyoqWI0tU6zqzoEYAu/xw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Y67IXkT5dz8YhD5nGfYn8Jz7z3xB6LIujerjgm9K2fsp/g4KP/FQe+4KDiBwe5TGw 4jCHKejsKcdpZnc44qP9IjH2+ksf893qZsOHD7VL5VSNBIeEXi1loa+4/Pl+PDIaH7 NsolaZ21RsaAWYhMf7YXqY6D3G1pExo6t11/XHmg= 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.10 218/236] time: Handle negative seconds correctly in timespec64_to_ns() Date: Mon, 13 Sep 2021 15:15:23 +0200 Message-Id: <20210913131107.782810696@linuxfoundation.org> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20210913131100.316353015@linuxfoundation.org> References: <20210913131100.316353015@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; }