]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
sourcestats: check samples loaded from dump files
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 4 May 2021 12:06:33 +0000 (14:06 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 6 May 2021 11:10:51 +0000 (13:10 +0200)
When loading a dump file with the -r option, check also sanity of the
sample time, offset, peer/root delay/dispersion, and the sample order to
better handle corrupted files.

sourcestats.c

index d8b1646f999ae3c73ebe322867d047646218150c..066d0e4cc942b39d95f72eb6420111fb8117fa38 100644 (file)
@@ -885,6 +885,7 @@ int
 SST_LoadFromFile(SST_Stats inst, FILE *in)
 {
   int i, n_samples, arun;
+  struct timespec now;
   double sample_time;
   char line[256];
 
@@ -895,6 +896,8 @@ SST_LoadFromFile(SST_Stats inst, FILE *in)
 
   SST_ResetInstance(inst);
 
+  LCL_ReadCookedTime(&now, NULL);
+
   for (i = 0; i < n_samples; i++) {
     if (!fgets(line, sizeof (line), in) ||
         sscanf(line, "%lf %lf %lf %lf %lf %lf %lf",
@@ -903,8 +906,19 @@ SST_LoadFromFile(SST_Stats inst, FILE *in)
                &inst->root_delays[i], &inst->root_dispersions[i]) != 7)
       return 0;
 
+    if (!UTI_IsTimeOffsetSane(&now, sample_time - UTI_TimespecToDouble(&now)))
+      return 0;
+
     /* Some resolution is lost in the double format, but that's ok */
     UTI_DoubleToTimespec(sample_time, &inst->sample_times[i]);
+
+    /* Make sure the samples are sane and they are in order */
+    if (!UTI_IsTimeOffsetSane(&inst->sample_times[i], -inst->offsets[i]) ||
+        !(fabs(inst->peer_delays[i]) < 1.0e6 && fabs(inst->peer_dispersions[i]) < 1.0e6 &&
+          fabs(inst->root_delays[i]) < 1.0e6 && fabs(inst->root_dispersions[i]) < 1.0e6) ||
+        (i > 0 && UTI_CompareTimespecs(&inst->sample_times[i],
+                                       &inst->sample_times[i - 1]) <= 0))
+      return 0;
   }
 
   inst->n_samples = n_samples;