From mboxrd@z Thu Jan 1 00:00:00 1970 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: 8bit 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 --- 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 += $(KHDR_INCLUDES) -TEST_GEN_PROGS := testptp +TEST_GEN_PROGS := testptp ptp_freq_overflow LDLIBS += -lrt TEST_PROGS = phc.sh diff --git a/tools/testing/selftests/ptp/ptp_freq_overflow.c b/tools/testing/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 allowed + * 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 express + * 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 = argc > 1 ? argv[1] : "/dev/ptp0"; + struct ptp_clock_caps caps; + struct timex tx = { 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 = open(device, O_RDWR); + if (fd < 0) + ksft_exit_skip("cannot open %s: %s\n", device, strerror(errno)); + + clkid = 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 == 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 = ADJ_FREQUENCY; + tx.freq = 147573952589676412LL; + + ret = clock_adjtime(clkid, &tx); + ksft_test_result(ret < 0 && errno == ERANGE, + "overflowing frequency adjustment is rejected (ret=%d errno=%d)\n", + ret, ret < 0 ? errno : 0); + + close(fd); + ksft_finished(); +} -- 2.43.0