entable value (i.e. year 2232) to avoid the overflow, assuming the system will not run for more than 30 years. Enforce the same limit for PTP clocks. Don't allow negative values and values closer than 30 years to the maximum value. Drivers may implement an even lower limit if the hardware registers cannot represent the whole interval between years 1970 and 2262 in the required resolution. Signed-off-by: Miroslav Lichvar Cc: Richard Cochran Cc: Thomas Gleixner Cc: John Stultz Cc: Arnd Bergmann Reviewed-by: Vadim Fedorenko Link: https://patch.msgid.link/20250828103300.1387025-1-mlichvar@redhat.com Signed-off-by: Jakub Kicinski Signed-off-by: Sasha Levin --- LLM Generated explanations, may be completely bogus: YES - Fixes a real bug: Prevents PTP clocks and timestamping from overflowing 64-bit signed ktime_t (wrap to year 1667) when a clock is set near the representable maximum or when a large offset is injected via ADJ_SETOFFSET. This is user-visible and can be triggered immediately by userspace with large offsets, not only in year 2262. - Small, contained change in the PTP core: - Validates absolute settime requests using the established helper. Added check in `ptp_clock_settime()` to reject invalid targets: `drivers/ptp/ptp_clock.c:104`. - Validates relative ADJ_SETOFFSET by first reading current time, adding the offset, then rejecting if the resulting time would be invalid: `ptp_clock_gettime()` call `drivers/ptp/ptp_clock.c:151`, compute sum `drivers/ptp/ptp_clock.c:154`, and validate with `timespec64_valid_settod()` `drivers/ptp/ptp_clock.c:155`. - Aligns PTP behavior with system clock rules: Uses the same upper-bound policy as CLOCK_REALTIME by calling `timespec64_valid_settod()`, which rejects negative times and values within 30 years of ktime’s max (`include/linux/time64.h:118`). This matches the prior “timekeeping: Force upper bound for setting CLOCK_REALTIME” change and ensures consistent semantics across clocks. - Minimal regression risk: - Only rejects out-of-range inputs that previously produced overflowed timestamps; returns `-EINVAL` instead of silently wrapping. - No architectural changes; no driver APIs change; ADJ_OFFSET and ADJ_FREQUENCY paths are untouched (besides existing range checks). - Matches existing kernel timekeeping validation patterns (system clock already enforces the same limits). - Touches a non-core subsystem (PTP POSIX clock ops) and is straightforward to review and backport. - Backport note: The change depends on `timespec64_valid_settod()` and related defines in `include/linux/time64.h`. If a target stable branch predates this helper, a trivial adaptation (or backport of the helper) is needed. Given the clear correctness benefit, minimal scope, and alignment with existing timekeeping policy, this is a good candidate for stable backport. drivers/ptp/ptp_clock.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/drivers/ptp/ptp_clock.c b/drivers/ptp/ptp_clock.c index 1cc06b7cb17ef..3e0726c6f55b3 100644 --- a/drivers/ptp/ptp_clock.c +++ b/drivers/ptp/ptp_clock.c @@ -100,6 +100,9 @@ static int ptp_clock_settime(struct posix_clock *pc, const struct timespec64 *tp return -EBUSY; } + if (!timespec64_valid_settod(tp)) + return -EINVAL; + return ptp->info->settime64(ptp->info, tp); } @@ -130,7 +133,7 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) ops = ptp->info; if (tx->modes & ADJ_SETOFFSET) { - struct timespec64 ts; + struct timespec64 ts, ts2; ktime_t kt; s64 delta; @@ -143,6 +146,14 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx) if ((unsigned long) ts.tv_nsec >= NSEC_PER_SEC) return -EINVAL; + /* Make sure the offset is valid */ + err = ptp_clock_gettime(pc, &ts2); + if (err) + return err; + ts2 = timespec64_add(ts2, ts); + if (!timespec64_valid_settod(&ts2)) + return -EINVAL; + kt = timespec64_to_ktime(ts); delta = ktime_to_ns(kt); err = ops->adjtime(ops, delta); -- 2.51.0[PATCH AUTOSEL 6.17-5.15] ptp: Limit time setting of PTP clocksSasha Levin undefinedpatches@lists.linux.dev, stable@vger.kernel.org undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined undefined