]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
refclock: validate normality of SHM and SOCK timestamps
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 24 Jun 2026 12:31:05 +0000 (14:31 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 30 Jun 2026 07:19:34 +0000 (09:19 +0200)
In the SHM and SOCK refclock drivers verify that received timestamps are
normal (microseconds and nanoseconds are in the expected range) to avoid
undefined behavior in their conversion and normalization.

refclock_shm.c
refclock_sock.c

index 38f7337618539ca2590ce8ca1b993b17cdb2809a..c3458794752070af5b4db457584b0a23a9d06cac 100644 (file)
@@ -94,6 +94,7 @@ static void shm_finalise(RCL_Instance instance)
 static int shm_poll(RCL_Instance instance)
 {
   struct timespec receive_ts, clock_ts;
+  struct timeval receive_tv, clock_tv;
   struct shmTime t, *shm;
 
   shm = (struct shmTime *)RCL_GetDriverData(instance);
@@ -109,21 +110,32 @@ static int shm_poll(RCL_Instance instance)
 
   shm->valid = 0;
 
-  receive_ts.tv_sec = t.receiveTimeStampSec;
-  clock_ts.tv_sec = t.clockTimeStampSec;
-
   if (t.clockTimeStampNSec / 1000 == t.clockTimeStampUSec &&
       t.receiveTimeStampNSec / 1000 == t.receiveTimeStampUSec) {
+    receive_ts.tv_sec = t.receiveTimeStampSec;
     receive_ts.tv_nsec = t.receiveTimeStampNSec;
+    clock_ts.tv_sec = t.clockTimeStampSec;
     clock_ts.tv_nsec = t.clockTimeStampNSec;
+
+    if (!UTI_IsTimespecNormal(&receive_ts) || !UTI_IsTimespecNormal(&clock_ts)) {
+      DEBUG_LOG("Invalid timestamp in SHM sample");
+      return 0;
+    }
   } else {
-    receive_ts.tv_nsec = 1000 * t.receiveTimeStampUSec;
-    clock_ts.tv_nsec = 1000 * t.clockTimeStampUSec;
+    receive_tv.tv_sec = t.receiveTimeStampSec;
+    receive_tv.tv_usec = t.receiveTimeStampUSec;
+    clock_tv.tv_sec = t.clockTimeStampSec;
+    clock_tv.tv_usec = t.clockTimeStampUSec;
+
+    if (!UTI_IsTimevalNormal(&receive_tv) || !UTI_IsTimevalNormal(&clock_tv)) {
+      DEBUG_LOG("Invalid timestamp in SHM sample");
+      return 0;
+    }
+
+    UTI_TimevalToTimespec(&receive_tv, &receive_ts);
+    UTI_TimevalToTimespec(&clock_tv, &clock_ts);
   }
 
-  UTI_NormaliseTimespec(&clock_ts);
-  UTI_NormaliseTimespec(&receive_ts);
-
   return RCL_AddSample(instance, &receive_ts, &clock_ts, t.leap, 1);
 }
 
index da0b0206373d59eba75ad0ac01cd1fe07e56b3e2..a8d04dd88674f3331ab2e1d0ab3b8c590df38b8a 100644 (file)
@@ -126,8 +126,12 @@ static void read_sample(int sockfd, int event, void *anything)
     return;
   }
 
+  if (!UTI_IsTimevalNormal(&sample.tv)) {
+    DEBUG_LOG("Invalid timestamp in SOCK sample");
+    return;
+  }
+
   UTI_TimevalToTimespec(&sample.tv, &sys_ts);
-  UTI_NormaliseTimespec(&sys_ts);
 
   if (!UTI_IsTimeOffsetSane(&sys_ts, sample.offset))
     return;