Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3940509pxj; Mon, 21 Jun 2021 09:49:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyDNaodahjyKau/EfvSO8E7blnQRIxQEK5hSkvI5GuU3yrQwgV0xk9ykT+86i/okP5h8Ur9 X-Received: by 2002:a17:906:c010:: with SMTP id e16mr25883382ejz.214.1624294179601; Mon, 21 Jun 2021 09:49:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624294179; cv=none; d=google.com; s=arc-20160816; b=z38/VwxjKoEy7i45gjSNdHjZsQbY480eJOOmRyTYysyrEYResP0iHH9iYgwRTAybyz iH2wx6LpYc8MSFGLRptyDfzkb5n7YO0Dg0IYt7EpUbopjAs6KSvL3KWk1EnIegnjinoq o7/3pnbzQLtv++0ncpeONuOQU35Ed4FWdPZ5dJ0fHFVGgYWWNG3QU5qJlW5oyxxVQ0AW tvOoBhtcmsQ3sQgYdTMN3o6vqvjdtuvsjGAc4KswOO7D5vr4y3+l4fK/Nn+1bAuUPUw0 h80y8KhyQO7T9s57ZcZKhG60Pk5L4Q+M6VotHuN+YYOjP8shYIBuKMJx/LhHuMvK0KCx anqg== 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=J5JYAdchCAv8kYtIsY2vGXYc/BsnA0CHimfDUguMUuI=; b=uzGvwJJZMBUlQl6lyRfnn+NzdAKk556WdGTlBLzLG6iVWWFkZOVHo/unkH5jr+Gy78 QN1D+zYTcD8igI8ZH3hhQWV5dLGhCGExpI5sMmexd8r8ITnaO4OyDeZ2uW1ZTsw0tXts +Zl9W4RLDn4MWIa9niYtC7PTd6si4X2Fvfh1+LTFpNwJ3k+sLph23qm9t75vVCoy0ZVc eIkVgDrd0Y2DH3VqqOZ1k3/+KS8Rs+9/fAs68LB3mBKyHSrMItIR6XNh4GSMvb9a1mx9 u6pDayqCl6YN9W6pwHHtmCsLulqivQGcIOtcQrh75in6mrX41qRrea8pFkK0qS1TjLUV 320g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="sk/Dd2Fm"; 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 w20si17043692edd.408.2021.06.21.09.49.16; Mon, 21 Jun 2021 09:49:39 -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="sk/Dd2Fm"; 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 S232681AbhFUQrz (ORCPT + 99 others); Mon, 21 Jun 2021 12:47:55 -0400 Received: from mail.kernel.org ([198.145.29.99]:37050 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232716AbhFUQnS (ORCPT ); Mon, 21 Jun 2021 12:43:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id C93AE61412; Mon, 21 Jun 2021 16:31:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624293097; bh=uhszjho4aRPirS9cPP6l0JRi41+84ick6GLuqAaxSIM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sk/Dd2FmTn3F3OzsGr/ic6p0N6GtM7vHnweTqveR/mSG3Qx6dLB9ig+6fDOt8Tf+Q mJXgXngHmvVHL/z85tMopwIr9qQ/t5TPMxilyynY3IBsyKhK0PPDhk1D2Q44T2zEY0 /fdvY84T0CXscn4WOEpz3zxLjPfJ66I1ey2rjfSE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jakub Kicinski , Richard Cochran , "David S. Miller" , Sasha Levin Subject: [PATCH 5.12 064/178] ptp: improve max_adj check against unreasonable values Date: Mon, 21 Jun 2021 18:14:38 +0200 Message-Id: <20210621154924.681391357@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154921.212599475@linuxfoundation.org> References: <20210621154921.212599475@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: Jakub Kicinski [ Upstream commit 475b92f932168a78da8109acd10bfb7578b8f2bb ] Scaled PPM conversion to PPB may (on 64bit systems) result in a value larger than s32 can hold (freq/scaled_ppm is a long). This means the kernel will not correctly reject unreasonably high ->freq values (e.g. > 4294967295ppb, 281474976645 scaled PPM). The conversion is equivalent to a division by ~66 (65.536), so the value of ppb is always smaller than ppm, but not small enough to assume narrowing the type from long -> s32 is okay. Note that reasonable user space (e.g. ptp4l) will not use such high values, anyway, 4289046510ppb ~= 4.3x, so the fix is somewhat pedantic. Fixes: d39a743511cd ("ptp: validate the requested frequency adjustment.") Fixes: d94ba80ebbea ("ptp: Added a brand new class driver for ptp clocks.") Signed-off-by: Jakub Kicinski Acked-by: Richard Cochran Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/ptp/ptp_clock.c | 6 +++--- include/linux/ptp_clock_kernel.h | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index 03a246e60fd9..21c4c34c52d8 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -63,7 +63,7 @@ static void enqueue_external_timestamp(struct timestamp_event_queue *queue, spin_unlock_irqrestore(&queue->lock, flags); } -s32 scaled_ppm_to_ppb(long ppm) +long scaled_ppm_to_ppb(long ppm) { /* * The 'freq' field in the 'struct timex' is in parts per @@ -80,7 +80,7 @@ s32 scaled_ppm_to_ppb(long ppm) s64 ppb = 1 + ppm; ppb *= 125; ppb >>= 13; - return (s32) ppb; + return (long) ppb; } EXPORT_SYMBOL(scaled_ppm_to_ppb); @@ -138,7 +138,7 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) delta = ktime_to_ns(kt); err = ops->adjtime(ops, delta); } else if (tx->modes & ADJ_FREQUENCY) { - s32 ppb = scaled_ppm_to_ppb(tx->freq); + long ppb = scaled_ppm_to_ppb(tx->freq); if (ppb > ops->max_adj || ppb < -ops->max_adj) return -ERANGE; if (ops->adjfine) diff --git a/include/linux/ptp_clock_kernel.h b/include/linux/ptp_clock_kernel.h index 0d47fd33b228..51d7f1b8b32a 100644 --- a/include/linux/ptp_clock_kernel.h +++ b/include/linux/ptp_clock_kernel.h @@ -235,7 +235,7 @@ extern int ptp_clock_index(struct ptp_clock *ptp); * @ppm: Parts per million, but with a 16 bit binary fractional field */ -extern s32 scaled_ppm_to_ppb(long ppm); +extern long scaled_ppm_to_ppb(long ppm); /** * ptp_find_pin() - obtain the pin index of a given auxiliary function -- 2.30.2