Skip to contentSkip to conversation

ptp: fix scaled_ppm_to_ppb() overflow bypassing the max_adj check

PATCH Superseded Deep Shah 3 participants 6 messages Jul 12 – Jul 21

Subsystems (4)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Deep Shah

author
#0 lore raw

cover ptp: fix scaled_ppm_to_ppb() overflow bypassing the max_adj check

ptp_clock_adjtime() validates an ADJ_FREQUENCY request by converting tx->freq to ppb and comparing it against ops->max_adj. On 64-bit systems that conversion can overflow s64 and wrap the result back into range, so a crafted tx->freq bypasses the check and reaches ->adjfine() unclamped.

Patch 1 rejects the overflow in ptp_clock_adjtime().
Patch 2 adds a regression test that crafts struct timex.freq directly (testptp's int-ppb path cannot express the value) and confirms it is rejected with -ERANGE.

Deep Shah (2):
ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb()
selftests: ptp: add a regression test for the frequency adjustment
overflow

 drivers/ptp/ptp_clock.c                       | 14 +++-
 tools/testing/selftests/ptp/Makefile          |  2 +-
 .../testing/selftests/ptp/ptp_freq_overflow.c | 74 +++++++++++++++++++
 3 files changed, 88 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/ptp/ptp_freq_overflow.c

base-commit: 3f1f755366687d051174739fb99f7d560202f60b

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb()

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 = 147573952589676412 makes (1 + ppm) * 125 equal 2^64 + 9, which wraps to ppb == 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.")
---
 drivers/ptp/ptp_clock.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
drivers/ptp/ptp_clock.c +13 -1
index d6f54ccaf93b..f83aa44b0a74 100644
@@ -9,6 +9,7 @@
 #include <linux/init.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
+#include <linux/overflow.h>
 #include <linux/posix-clock.h>
 #include <linux/pps_kernel.h>
 #include <linux/property.h>
@@ -159,7 +160,18 @@ 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) {
-		long ppb = 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 = scaled_ppm_to_ppb(tx->freq);
 		if (ppb > ops->max_adj || ppb < -ops->max_adj)
 			return -ERANGE;
 		err = ops->adjfine(ops, tx->freq);
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 selftests: ptp: add a regression test for the frequency adjustment overflow

In reply to Deep Shah

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.

---
 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
tools/testing/selftests/ptp/Makefile +1 -1
index 8f57f88ecadd..dd7376cc9bf5 100644
@@ -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
 
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +74 -0

74 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#3 lore raw

Re: [PATCH net 2/2] selftests: ptp: add a regression test for the frequency adjustment overflow

This is an AI-generated review of your patch. The human sending this
email has considered the AI review valid, or at least plausible.
https://sashiko.dev/#/patchset/[email protected]?part=2
---
--- Patch [2]: [PATCH net 2/2] selftests: ptp: add a regression test for the frequency adjustment overflow --- commit be5629a2612f961645c213deea88db61fa06f083
Author: Deep Shah <[email protected]>

selftests: ptp: add a regression test for the frequency adjustment overflow

This commit adds a regression test that crafts a struct timex.freq directly to verify if an overflowing frequency adjustment is correctly rejected by ptp_clock_adjtime() with -ERANGE.

Quoted 5 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +0 -0
new file mode 100644
index 0000000000000..342b22689f679

[ ... ]

tools/testing/selftests/ptp/ptp_freq_overflow.c line 22

Quoted 2 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +2 -0
+
+#define FD_TO_CLOCKID(fd)	((~(clockid_t)(fd) << 3) | 3)

[Severity: Medium]
Does this macro left-shift a negative signed integer?

Casting fd (a positive integer) to clockid_t (a signed 32-bit int), applying bitwise NOT, and left-shifting it by 3 yields a negative integer. Left-shifting a negative signed integer invokes undefined behavior in ISO C, which will trigger a runtime error if tests are run with Undefined Behavior Sanitizer (UBSan) enabled.

Could the inverted value be cast to an unsigned integer prior to the shift, similar to how it is handled in testptp.c?

[ ... ]

tools/testing/selftests/ptp/ptp_freq_overflow.c line 65

Quoted 8 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +8 -0
+	/*
+	 * (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;

[Severity: Low]
Will this assignment cause a -Woverflow compiler warning on 32-bit architectures?

On 32-bit platforms where tx.freq is a 32-bit long (without _TIME_BITS=64), assigning this 64-bit literal exceeds the maximum value of the signed integer type. Although a runtime check prevents execution, the compiler still parses the assignment and emits a -Woverflow warning during the build.

tools/testing/selftests/ptp/ptp_freq_overflow.c

Quoted 2 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +2 -0
+
+	ret = clock_adjtime(clkid, &tx);

[Severity: Medium]
Does this test permanently disrupt the hardware clock frequency on vulnerable kernels without restoring it?

If the kernel does not have the overflow fix, clock_adjtime() will bypass the max_adj check and push the corrupted frequency downstream to the driver via ops->adjfine(). Because the test exits immediately after verifying the error code, the original frequency is never restored, leaving time synchronization corrupted until a reboot or manual reset.

Should the test read the initial dialed_frequency and restore it at the end of the test?

tools/testing/selftests/ptp/ptp_freq_overflow.c line 70

Quoted 3 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +3 -0
+	ksft_test_result(ret < 0 && errno == ERANGE,
+			 "overflowing frequency adjustment is rejected (ret=%d errno=%d)\n",
+			 ret, ret < 0 ? errno : 0);

[Severity: Medium]
Will this test incorrectly fail if the PTP clock is in free-running mode?

If virtual clocks are active on the selected PTP device, ptp_clock_freerun() returns true and the kernel explicitly returns -EBUSY before evaluating the frequency overflow logic. Since the test expects -ERANGE, receiving -EBUSY will cause the KSFT assertion to fail.

Should -EBUSY be accounted for as a valid condition to gracefully skip the test?

Quoted 4 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c new file +4 -0
+
+	close(fd);
+	ksft_finished();
+}
Reviewed-by on the v1 series
#5 lore raw
In reply to Deep Shah
Quoted 15 lines · Deep Shah

ptp_clock_adjtime() validates an ADJ_FREQUENCY request by converting tx->freq to ppb and comparing it against ops->max_adj. On 64-bit systems that conversion can overflow s64 and wrap the result back into range, so a crafted tx->freq bypasses the check and reaches ->adjfine() unclamped.

Patch 1 rejects the overflow in ptp_clock_adjtime().
Patch 2 adds a regression test that crafts struct timex.freq directly (testptp's int-ppb path cannot express the value) and confirms it is rejected with -ERANGE.

Deep Shah (2):
ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb()
selftests: ptp: add a regression test for the frequency adjustment
overflow

Quoted 8 lines
  drivers/ptp/ptp_clock.c                       | 14 +++-
  tools/testing/selftests/ptp/Makefile          |  2 +-
  .../testing/selftests/ptp/ptp_freq_overflow.c | 74 +++++++++++++++++++
  3 files changed, 88 insertions(+), 2 deletions(-)

create mode 100644 tools/testing/selftests/ptp/ptp_freq_overflow.c

base-commit: 3f1f755366687d051174739fb99f7d560202f60b

For the series: