From nobody Sat Jul 25 23:03:34 2026 Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D4FF22D739C for ; Sun, 12 Jul 2026 04:09:25 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783829367; cv=none; b=nzcVWoXABoJTFUHCwmYaSRUSPPRnNg1O8ZuZX9AIavWgtT00DOHVolbAOlzFFsUvvy+3R4T27QPF55iDWni/7Jd+jwaWmb5cq3OU11dmuuKhvJaCS/I+3vHl6KhpyndRop6gz20ttojHojlF7Ar+ymIP4TH89GkoTQJIM4WoKUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783829367; c=relaxed/simple; bh=VAj6oUe/TU0O43DOvhQx6ZvzmDHg3qu28qW30e0TS1o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hsQ5bCo9eHAP3uu0dyH6ad1sRr/LPnZMHBT7ttG3GxeHv0oMaCAwvBS1cDBTogaz6cuQKw8RMMjxj6eAA//H7BeeJzdlAgycG/dZQgSImbtqEdwBXqSqL7Co0E4v9RDb+xFm5U43BRcM7i/7u0NwkQ7JWPyxe5PWus5zPoju8X8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JRn/Ekcy; arc=none smtp.client-ip=209.85.219.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JRn/Ekcy" Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-8efcef23d21so16681396d6.2 for ; Sat, 11 Jul 2026 21:09:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783829365; x=1784434165; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=jfLzU+Ux3JyzhsCTRzc6rXQZzc52Sb+wpPjR6e6mN6s=; b=JRn/EkcyfRJ6M8Qp16TFbox85SAeTcXEsk7FV3P0Sne64W7hqL0TnrM12c3jCFXPcw 1rPgeB9PtPB5ejzU32hzIaHKPV9y3xSYFnsiqHRpfsEcgeO1y7UpQdQhrqTeOakwRWma hh6uWI6QuWD4jxuGPZwsNzTNXTqCQclzAqrLcghY3kjRgPGQ73xnEpYK0d8B5epqVmT4 Sc+RsLDNqt4S919je58iuqO8wSuMMb5VuK7nEam4+R7yO0v3OdCW7DOQpyEzVbAtlu5d fovN/25J/SALI13VtV7s9vQfsthhlHqf+bA+iR7/nyiltehKRKjiiEzrwu7s+bE5sWcw gfFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783829365; x=1784434165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=jfLzU+Ux3JyzhsCTRzc6rXQZzc52Sb+wpPjR6e6mN6s=; b=PRM9oT0IX/sWskDz8LImkETkiRk3H3V6OruWFGhS6tvakXUXyyqWtKH41Hdm3gBlWQ JI8o+5+KyhqhZp19nTGYW7wpLVQlgQgmivn1e/nIYL7KjDL2sbHQkpTEd81ThORj5o/r P1NObfN1If3ZGBoIsHA40IJhCAqmIC/m/7dC2gH+2V96ImT7xeZ3yGjKf4WhwvQsKDb0 ZSy4ifYZw+xNdvdFxwlI9CeRWb0y0WyChbaZSP4FSbPr/mBq7UUsxeUXDx+zuNaxK42i n7WNijJy/PV+oxKfhPzKT1446lohXWkK8QjMoynOu1ey7I+0i1OkXA3uJNap3eytZ4uX o0mg== X-Gm-Message-State: AOJu0Yx7u/U7CYmFSLdkcXsb9raDwYuGXwgH8yf9muAmUC41lx6QjU/l DUjhmzCxa3rrP5lZ7tTC8e0d8M49j20SUukBaKJpuiCyxrgwGkNsvhq9 X-Gm-Gg: AfdE7cmigmTY4Ve0IdkeaeQ6iKDARa2k3mkzAg+/gv8Vlb9wY1ebZ/HNaW6OqFld/f7 LJrUv+EgfMdm8DOK9OHiYlr+4nFsDTwybbPthZz1CG5CZPi7n9JP0JdQ51gKVvrx7NiOZfironn Zjw1n5BTevRKmu9EXiSqa57k71UeE6LXvTJf1z4B/a2wtOAKc/xpIunSacFNqbyt4M+gOwRRXgC GUu0ecFx6Rt9EME2ei8R4TKfatMpatF8IaypGDnwlllGoTTIODtnJN4n91tJMeEqX2zfV8GhHPy aS9OfDjTzd7w6OlzWipGvFVbqcXiQEc9jFp6EEWthebHdio25TO98z8iXpPCam7ZxKkzisSqWm6 5aPVejhchq13kymqh7ng4p0atl0ISzxi1DjdariyIVGoTm64VstAleReo0BfqhXnRpYYbk3fHMI q+WNNsD2CaoSouNgoC0KP6TP+0ZGzd+a9IDhAdROjpQ5d52GWH0V5x/By3YpAb5MLVSVLTXqs5G fweKWkdBIXMSc1/32UErTvqhcIfFC9Lm9o/AUygQwk= X-Received: by 2002:a05:620a:6403:b0:92e:c117:5ee6 with SMTP id af79cd13be357-92ef2c63c87mr502225185a.82.1783829364696; Sat, 11 Jul 2026 21:09:24 -0700 (PDT) Received: from ip-172-31-15-253.ec2.internal (ec2-44-200-214-82.compute-1.amazonaws.com. [44.200.214.82]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5cf9d9bsm687147085a.28.2026.07.11.21.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 21:09:24 -0700 (PDT) From: Deep Shah To: netdev@vger.kernel.org, Richard Cochran , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet , Andrew Lunn Cc: linux-kernel@vger.kernel.org, Shuah Khan , linux-kselftest@vger.kernel.org, Deep Shah Subject: [PATCH net 1/2] ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb() Date: Sun, 12 Jul 2026 04:09:21 +0000 Message-ID: <20260712040922.6403-2-deepshah146@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260712040922.6403-1-deepshah146@gmail.com> References: <20260712040922.6403-1-deepshah146@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" ptp_clock_adjtime() converts tx->freq to ppb with scaled_ppm_to_ppb() and rejects the request if it exceeds ops->max_adj. On 64-bit systems that conversion computes (1 + ppm) * 125 in s64, which can overflow for a large tx->freq and wrap the result back into [-max_adj, max_adj]. The check then passes and the original out-of-range value is handed to ->adjfine(). For example tx->freq =3D 147573952589676412 makes (1 + ppm) * 125 equal 2^64 + 9, which wraps to ppb =3D=3D 0 and is accepted. Reject the request with -ERANGE if either the addition or the multiplication overflows. This hardens the max_adj sanity check and is not a security fix. It follows up commit 475b92f93216 ("ptp: improve max_adj check against unreasonable values"), which fixed the analogous s32 narrowing but not this overflow. Fixes: d39a743511cd ("ptp: validate the requested frequency adjustment.") Signed-off-by: Deep Shah Reviewed-by: Vadim Fedorenko --- drivers/ptp/ptp_clock.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index d6f54ccaf93b..f83aa44b0a74 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -159,7 +160,18 @@ static int ptp_clock_adjtime(struct posix_clock *pc, s= truct __kernel_timex *tx) delta =3D ktime_to_ns(kt); err =3D ops->adjtime(ops, delta); } else if (tx->modes & ADJ_FREQUENCY) { - long ppb =3D scaled_ppm_to_ppb(tx->freq); + long ppb; + s64 tmp; + + /* + * scaled_ppm_to_ppb() multiplies (1 + freq) by 125 in s64; + * reject a ->freq large enough to overflow that, which could + * otherwise wrap the result back into the max_adj range. + */ + if (check_add_overflow(tx->freq, 1, &tmp) || + check_mul_overflow(tmp, 125, &tmp)) + return -ERANGE; + ppb =3D scaled_ppm_to_ppb(tx->freq); if (ppb > ops->max_adj || ppb < -ops->max_adj) return -ERANGE; err =3D ops->adjfine(ops, tx->freq); --=20 2.43.0 From nobody Sat Jul 25 23:03:34 2026 Received: from mail-qv1-f50.google.com (mail-qv1-f50.google.com [209.85.219.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C3121369D45 for ; Sun, 12 Jul 2026 04:09:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783829368; cv=none; b=sLIgXFP4f9hDKsRZx4HawTzJfPArVGPq3DfV/9tUHn/1bur5g4F+KXTfEWXXUcY1itM+b+Tj93CWvnpCKR77da48vUwkRLGSVBimuKMHQvJsWDCWDnqF5N7Kp8Gkr14FtoOJaQ3m5KkfoxThWj8FOFWtuYhNq5W7TjIdjk2Aj9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1783829368; c=relaxed/simple; bh=ht7k1vfyTfnKzBTKA4hWIj+a/zQqJ1S/l9ggwrpoISo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ouYb08yHiuF5iUjit+CFwKKqte3bJkMn9pJ17vnjZezUk0CpEAN+SFPBG2R5PIvTCW+Jk0dMsLtuA2zrCauu265m5MhJ5yNZgPNDwVOe10BmXe0Tf+qAlQwa8eDDT3LclhnSh7iFdjIf1fBiAAi8uN/WbARcQ0eG7TOFNDOi9lM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ljVUR4LY; arc=none smtp.client-ip=209.85.219.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ljVUR4LY" Received: by mail-qv1-f50.google.com with SMTP id 6a1803df08f44-902e4af2d9dso12550646d6.0 for ; Sat, 11 Jul 2026 21:09:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1783829365; x=1784434165; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=CY1zhHCs8Urd6z2dxMWVdrNocO2i3dPyfsdmlbGYaEg=; b=ljVUR4LYLTjfRSI3KGhnaHhxHKA2dn/11jD/S0aF9ftwYXsuo9oxFItpkTJLbIguJm 5/q6bNuC9IKtit2LiIScEYlIwtYvqKLqTPjQyvPwy+nlG86o8W3mbWvDDAd0ogFHtNt6 fKCWxM/j75Ang57K8NwZ+APwFaZpdrBAJfhP2xftQ3h6cTi43peEQf0TpvKExTNi+IV+ hBq/tj7zGUyoop+CSRbDS8PkY23sfm5/1/lAgd2BMu5VVW/Yb0j92eRlRTWAk2RFhtko SzcYXWphSq3jFyRdrigF2XtXAAmA1p95Bd5bnBM41ZB6kyHx3RhgqT5NuQBFDGVkKXA9 5pxQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1783829365; x=1784434165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=CY1zhHCs8Urd6z2dxMWVdrNocO2i3dPyfsdmlbGYaEg=; b=BnL8+pWPKaA3qcz0+gNR2X7j0zPBksr76bUoFQXnyPlGJ9ZtAHtrXprzobYhfE7Er8 l4t3RF+LRTycfE+3fDJb5PSvtwTZUszBWBLm59kDVWr14cjlwcplbSqdA4JM95tmn81Z 2d32EkZyjnQk4s9dVVJDrc8xmUhz3Vhz+BQk6y0sQR3XBZZr9X172dRzrTvervlGzLCI KXuurrAzc5j33IvH08BbQgxk3WLRMhebZOHvHMrrog4Saf09/co8l5mMXm+6atSIpts3 poZwfuMbF6NKBu9RzjSj19HDp+N3BkuSBfQXSk4AIaOWCJbdG733u1Uwrz0VAaVtA9fq +NKA== X-Gm-Message-State: AOJu0YwGIjXK2spd4iXhUiobxD2/lnLCGSUWQUwpKOs2/pJItbQ1clLd mHzEdNb2/xAWa+QjAzPOBisRt4qamH9dvaJo3ro64AgQTFmcnrgLCj1C X-Gm-Gg: AfdE7cm5b4TpnT4A/evNBCtskRrJuCtvPFXhlN1VojfuC6H5TQjA11AvaLdlLLgyxaL wDECHs3KOqCle5Ed5zua3zMGatxhqkgGK19HSMnyDUGUv/OrDuSldonKT4sBdH5V8a+Yh++SBUJ 6rJwXMrOq4PrGOwSAZEOV80yehKtFSSuVeqM2/B4W6wGkKGQxo0H8EFbhlT8q6PmyhKG0c2cXtV YQXe/y582Gh1LmqpUatm01umxefj0nqSy8JEBFQtVQ1j+Rc+isQ2okXLZCC1AY71xCMXLPUb8AW EngY2YKWMh5iuZGRji4JjCjQAEQuD2PIX1Hw3YGqWUh9P5hfBgLitViSVvrL9w/xaBp2IoAn5X0 R450U5YwnN0HBp5/jTjZccHeW86L0L9mxAB+T+EKuK09bbrzD6XHsLj7ez+Xyuyi1AJFEJ4jUtY 4YVvrXbZEmXYClpyySW9WOXFMqMqI5LdNeS8WQfC2NCaF46UF2lcVMf6PGh+cO3Dn69PWtOM0ZP izQMnTH0ONc9fqRW3HeH0o3TxQUVj+rpnVWYcoYfu8= X-Received: by 2002:a05:620a:44d4:b0:92e:e728:9d03 with SMTP id af79cd13be357-92ef3cc8072mr428999985a.6.1783829365498; Sat, 11 Jul 2026 21:09:25 -0700 (PDT) Received: from ip-172-31-15-253.ec2.internal (ec2-44-200-214-82.compute-1.amazonaws.com. [44.200.214.82]) by smtp.gmail.com with ESMTPSA id af79cd13be357-92ee5cf9d9bsm687147085a.28.2026.07.11.21.09.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 11 Jul 2026 21:09:24 -0700 (PDT) From: Deep Shah To: netdev@vger.kernel.org, Richard Cochran , "David S . Miller" , Jakub Kicinski , Paolo Abeni , Eric Dumazet , Andrew Lunn Cc: linux-kernel@vger.kernel.org, Shuah Khan , linux-kselftest@vger.kernel.org, Deep Shah Subject: [PATCH net 2/2] selftests: ptp: add a regression test for the frequency adjustment overflow Date: Sun, 12 Jul 2026 04:09:22 +0000 Message-ID: <20260712040922.6403-3-deepshah146@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260712040922.6403-1-deepshah146@gmail.com> References: <20260712040922.6403-1-deepshah146@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" testptp's -f option stores the requested adjustment as an int ppb and converts it to scaled ppm, so it cannot express the 64-bit scaled-ppm values needed to overflow scaled_ppm_to_ppb() and bypass the max_adj check enforced by ptp_clock_adjtime(). Add a small test that crafts struct timex.freq directly and verifies that an overflowing frequency adjustment is rejected with -ERANGE. The test skips when no frequency-adjustable PTP device is available. Signed-off-by: Deep Shah Reviewed-by: Vadim Fedorenko --- tools/testing/selftests/ptp/Makefile | 2 +- .../testing/selftests/ptp/ptp_freq_overflow.c | 74 +++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/ptp/ptp_freq_overflow.c diff --git a/tools/testing/selftests/ptp/Makefile b/tools/testing/selftests= /ptp/Makefile index 8f57f88ecadd..dd7376cc9bf5 100644 --- a/tools/testing/selftests/ptp/Makefile +++ b/tools/testing/selftests/ptp/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0 CFLAGS +=3D $(KHDR_INCLUDES) -TEST_GEN_PROGS :=3D testptp +TEST_GEN_PROGS :=3D testptp ptp_freq_overflow LDLIBS +=3D -lrt TEST_PROGS =3D phc.sh =20 diff --git a/tools/testing/selftests/ptp/ptp_freq_overflow.c b/tools/testin= g/selftests/ptp/ptp_freq_overflow.c new file mode 100644 index 000000000000..342b22689f67 --- /dev/null +++ b/tools/testing/selftests/ptp/ptp_freq_overflow.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Regression test for the scaled_ppm_to_ppb() integer overflow that allow= ed + * a crafted clock_adjtime(ADJ_FREQUENCY) to bypass the PTP max_adj check. + * + * testptp's -f option stores the adjustment as an int ppb and cannot expr= ess + * the 64-bit scaled-ppm values needed to overflow the conversion, so this + * test crafts struct timex.freq directly. + */ +#define _GNU_SOURCE +#define __SANE_USERSPACE_TYPES__ +#include +#include +#include +#include +#include +#include +#include +#include +#include "../kselftest.h" + +#define FD_TO_CLOCKID(fd) ((~(clockid_t)(fd) << 3) | 3) + +/* clock_adjtime is not available in GLIBC < 2.14 */ +#if !__GLIBC_PREREQ(2, 14) +#include +static int clock_adjtime(clockid_t id, struct timex *tx) +{ + return syscall(__NR_clock_adjtime, id, tx); +} +#endif + +int main(int argc, char *argv[]) +{ + const char *device =3D argc > 1 ? argv[1] : "/dev/ptp0"; + struct ptp_clock_caps caps; + struct timex tx =3D { 0 }; + clockid_t clkid; + int fd, ret; + + ksft_print_header(); + ksft_set_plan(1); + + if (sizeof(tx.freq) < 8) + ksft_exit_skip("the overflow only affects 64-bit kernels\n"); + + fd =3D open(device, O_RDWR); + if (fd < 0) + ksft_exit_skip("cannot open %s: %s\n", device, strerror(errno)); + + clkid =3D FD_TO_CLOCKID(fd); + + if (ioctl(fd, PTP_CLOCK_GETCAPS, &caps)) + ksft_exit_skip("PTP_CLOCK_GETCAPS on %s: %s\n", device, strerror(errno)); + if (!caps.max_adj) + ksft_exit_skip("%s does not support frequency adjustment\n", device); + + /* + * (1 + 147573952589676412) * 125 =3D=3D 2^64 + 9, which overflows s64 in + * scaled_ppm_to_ppb() and wraps the result to a ppb of 0. A kernel + * that does not detect the overflow lets this absurd frequency past + * the max_adj check; a fixed kernel rejects it with -ERANGE. + */ + tx.modes =3D ADJ_FREQUENCY; + tx.freq =3D 147573952589676412LL; + + ret =3D clock_adjtime(clkid, &tx); + ksft_test_result(ret < 0 && errno =3D=3D ERANGE, + "overflowing frequency adjustment is rejected (ret=3D%d errno=3D%d)\n", + ret, ret < 0 ? errno : 0); + + close(fd); + ksft_finished(); +} --=20 2.43.0