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 21 – Aug 1

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Deep Shah

author
#0 lore raw

v2 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.

Changes in v2:
- patch 1: unchanged, added Reviewed-by from Vadim Fedorenko.
- patch 2 (all from Simon Horman's review):
- cast fd to unsigned before the shift in FD_TO_CLOCKID (UBSan)
- only set the 64-bit test value on 64-bit builds (32-bit -Woverflow)
- save and restore the clock frequency
- skip instead of fail on -EBUSY (free-running clock)

Link to v1:
https://lore.kernel.org/netdev/[email protected]/

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 | 99 +++++++++++++++++++
 3 files changed, 113 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/ptp/ptp_freq_overflow.c

base-commit: 1c975de3343cdef506f2eecc833cc1f14b0401c4

Signature · 1 line
2.43.0
author
#1 lore raw

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

ptp_clock_adjtime() validates an ADJ_FREQUENCY request by converting the requested scaled ppm to ppb and comparing it against ops->max_adj:

	long ppb = scaled_ppm_to_ppb(tx->freq);
	if (ppb > ops->max_adj || ppb < -ops->max_adj)
		return -ERANGE;

scaled_ppm_to_ppb() computes (1 + ppm) * 125 >> 13 in s64. For a sufficiently large tx->freq the multiplication overflows s64 and wraps, so the resulting ppb can fall back within [-max_adj, max_adj] and pass the check. The unclamped tx->freq is then handed to ->adjfine(), where drivers scale it again (e.g. scaled_ppm * 762939453125 in ptp_idt82p33) and program a bogus frequency word.

For example tx->freq = 147573952589676412 makes (1 + ppm) * 125 equal 2^64 + 9, which wraps to ppb == 0 and is accepted.

The caller already has write access to the PHC, so this hardens the max_adj sanity check rather than crossing a privilege boundary, and well-behaved user space (e.g. ptp4l) never requests such values. It is a follow-up to commit 475b92f93216 ("ptp: improve max_adj check against unreasonable values"), which handled the analogous s32 narrowing but not this multiplication overflow.

Detect the overflow with check_*_overflow() and reject the request in ptp_clock_adjtime() instead of acting on the wrapped value.

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..4111342d64f0 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 would
+		 * otherwise wrap the result back into the max_adj range.
+		 */
+		if (check_add_overflow((s64)tx->freq, (s64)1, &tmp) ||
+		    check_mul_overflow(tmp, (s64)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
#4 lore raw

PATCH v2 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 | 99 +++++++++++++++++++
 2 files changed, 100 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 +99 -0

99 lines not on this page · raw message

Signature · 1 line
2.43.0
reviewer
#5 lore raw

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

Quoted 10 lines · 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.

...

Quoted 1 line
tools/testing/selftests/ptp/ptp_freq_overflow.c +0 -0

...

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

Quoted 10 lines
tools/testing/selftests/ptp/ptp_freq_overflow.c +10 -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;
+#if __SIZEOF_LONG__ >= 8
+	tx.freq = 147573952589676412L;
+#endif

Thanks for the update here.
But unfortunately sashiko.dev still flags a problem.

"Will this preprocessor condition cause a false test failure on 32-bit
architectures configured with 64-bit time types (such as x32 or y2038 compliant systems)?

"On those architectures, the runtime check earlier in main() passes because
sizeof(tx.freq) is 8. However, __SIZEOF_LONG__ evaluates to 4, which causes the preprocessor to skip the assignment of tx.freq.

"This leaves tx.freq as 0, allowing clock_adjtime() to successfully apply
a zero adjustment instead of rejecting the call with -ERANGE, which triggers a test failure.

I wonder if dropping the #if/#endif would address that concern.

maintainer
#2 lore raw

Re: [PATCH v2 net 1/2] ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb()

In reply to Deep Shah
Quoted 6 lines · On Tue, 21 Jul 2026 01:42:55 +0000 Deep Shah wrote:

The caller already has write access to the PHC, so this hardens the max_adj sanity check rather than crossing a privilege boundary, and well-behaved user space (e.g. ptp4l) never requests such values. It is a follow-up to commit 475b92f93216 ("ptp: improve max_adj check against unreasonable values"), which handled the analogous s32 narrowing but not this multiplication overflow.

Exactly, so I think this sort of patch belongs in net-next without a Fixes tags. Because _nobody cares_. Please correct me if I'm wrong.

author
#3 lore raw

Re: [PATCH v2 net 1/2] ptp: reject frequency adjustments that overflow scaled_ppm_to_ppb()

That's fair. I'll resend to net-next without the Fixes tag.