]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: fix finalization for async resolver
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 22 Aug 2024 07:26:59 +0000 (09:26 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 22 Aug 2024 07:32:36 +0000 (09:32 +0200)
If an attempt to resolve addresses of an NTP server is made right before
starting the termination sequence, the asynchronous resolver thread
could read the server name when it was already freed.

Leave unresolved sources allocated in NSR_Finalise() if the async
resolver did not finish yet, at least for now. Waiting for the resolving
result or cancelling the thread would complicate the code. The scheduler
is not expected to be running at this point.

ntp_sources.c

index 29c99acfe10d56632f9a5720f8bb96d9eb1596ef..d2cd1134c90881ba68b5d3ce758bb33d31b5242a 100644 (file)
@@ -219,8 +219,14 @@ NSR_Finalise(void)
   ARR_DestroyInstance(pools);
 
   SCH_RemoveTimeout(resolving_id);
-  while (unresolved_sources)
-    remove_unresolved_source(unresolved_sources);
+
+  /* Leave the unresolved sources allocated if the async resolver is running
+     to avoid reading the name from freed memory.  The handler will not be
+     called as the scheduler should no longer be running at this point. */
+  if (!resolving_source) {
+    while (unresolved_sources)
+      remove_unresolved_source(unresolved_sources);
+  }
 
   initialised = 0;
 }