dfa500
From: Florian Weimer <fweimer@redhat.com>
dfa500
Date: Tue, 21 Jan 2020 16:38:15 +0000 (+0100)
dfa500
Subject: resolv: Fix ABA race in /etc/resolv.conf change detection [BZ #25420]
dfa500
X-Git-Url: https://sourceware.org/git/?p=glibc.git;a=commitdiff_plain;h=fa00db0a6eb755837ae5d413515e0da582b304f3
dfa500
dfa500
resolv: Fix ABA race in /etc/resolv.conf change detection [BZ #25420]
dfa500
dfa500
__resolv_conf_get_current should only record the initial file
dfa500
change data if after verifying that file just read matches the
dfa500
original measurement.  Fixes commit aef16cc8a4c670036d45590877
dfa500
("resolv: Automatically reload a changed /etc/resolv.conf file
dfa500
[BZ #984]").
dfa500
dfa500
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
dfa500
---
dfa500
dfa500
diff --git a/resolv/resolv_conf.c b/resolv/resolv_conf.c
dfa500
index bdd2ebb909..29a1f4fb94 100644
dfa500
--- a/resolv/resolv_conf.c
dfa500
+++ b/resolv/resolv_conf.c
dfa500
@@ -136,18 +136,25 @@ __resolv_conf_get_current (void)
dfa500
     {
dfa500
       /* Parse configuration while holding the lock.  This avoids
dfa500
          duplicate work.  */
dfa500
-      conf = __resolv_conf_load (NULL, NULL);
dfa500
+      struct file_change_detection after_load;
dfa500
+      conf = __resolv_conf_load (NULL, &after_load);
dfa500
       if (conf != NULL)
dfa500
         {
dfa500
           if (global_copy->conf_current != NULL)
dfa500
             conf_decrement (global_copy->conf_current);
dfa500
           global_copy->conf_current = conf; /* Takes ownership.  */
dfa500
 
dfa500
-          /* Update file modification stamps.  The configuration we
dfa500
-             read could be a newer version of the file, but this does
dfa500
-             not matter because this will lead to an extraneous reload
dfa500
-             later.  */
dfa500
-          global_copy->file_resolve_conf = initial;
dfa500
+          /* Update file change detection data, but only if it matches
dfa500
+             the initial measurement.  This avoids an ABA race in case
dfa500
+             /etc/resolv.conf is temporarily replaced while the file
dfa500
+             is read (after the initial measurement), and restored to
dfa500
+             the initial version later.  */
dfa500
+          if (file_is_unchanged (&initial, &after_load))
dfa500
+            global_copy->file_resolve_conf = after_load;
dfa500
+          else
dfa500
+            /* If there is a discrepancy, trigger a reload during the
dfa500
+               next use.  */
dfa500
+            global_copy->file_resolve_conf.size = -1;
dfa500
         }
dfa500
     }
dfa500