unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* nsswitch: do not reload if "/" changes
@ 2021-01-16  0:59 DJ Delorie via Libc-alpha
  2021-01-16 10:52 ` Florian Weimer via Libc-alpha
                   ` (2 more replies)
  0 siblings, 3 replies; 25+ messages in thread
From: DJ Delorie via Libc-alpha @ 2021-01-16  0:59 UTC (permalink / raw)
  To: libc-alpha


[Note: I tried putting this functionality in the file_change_detection
module, but that didn't have enough persistence.]

[Note: tested by instrumenting test-container.c and observing the
instrumentation with test containers on the root fs and on a separate
fs]

https://sourceware.org/bugzilla/show_bug.cgi?id=27077

Before reloading nsswitch.conf, verify that the root directory
hasn't changed - if it has, it's likely that we've entered a
container and should not trust the nsswitch inside the container
nor load any shared objects therein.

diff --git a/nss/nss_database.c b/nss/nss_database.c
index e719ec0865..580ea7b963 100644
--- a/nss/nss_database.c
+++ b/nss/nss_database.c
@@ -33,6 +33,11 @@ struct nss_database_state
 {
   struct nss_database_data data;
   __libc_lock_define (, lock);
+  /* If "/" changes, we switched into a container and do NOT want to
+     reload anything.  This data must be persistent across
+     reloads.  */
+  ino64_t root_ino;
+  ino64_t root_dev;
 };
 
 
@@ -53,6 +58,8 @@ global_state_allocate (void *closure)
       memset (result->data.services, 0, sizeof (result->data.services));
       result->data.initialized = true;
       result->data.reload_disabled = false;
+      result->root_ino = 0;
+      result->root_dev = 0;
       __libc_lock_init (result->lock);
     }
   return result;
@@ -356,6 +363,8 @@ nss_database_check_reload_and_get (struct nss_database_state *local,
                                    nss_action_list *result,
                                    enum nss_database database_index)
 {
+  struct stat64 str;
+
   /* Acquire MO is needed because the thread that sets reload_disabled
      may have loaded the configuration first, so synchronize with the
      Release MO store there.  */
@@ -379,6 +388,25 @@ nss_database_check_reload_and_get (struct nss_database_state *local,
       __libc_lock_unlock (local->lock);
       return true;
     }
+
+  /* Before we reload, verify that "/" hasn't changed.  We assume that
+     errors here are very unlikely, but the chance that we're entering
+     a container is also very unlikely, so we err on the side of both
+     very unlikely things not happening at the same time.  */
+  if (__stat64 ("/", &str) == 0)
+    {
+      if (local->root_ino != 0
+	  && (str.st_ino != local->root_ino
+	      ||  str.st_dev != local->root_dev))
+	{
+	  /* Change detected; disable reloading.  */
+	  atomic_store_release (&local->data.reload_disabled, 1);
+	  __libc_lock_unlock (local->lock);
+	  return true;
+	}
+      local->root_ino = str.st_ino;
+      local->root_dev = str.st_dev;
+    }
   __libc_lock_unlock (local->lock);
 
   /* Avoid overwriting the global configuration until we have loaded
diff --git a/nss/nss_database.h b/nss/nss_database.h
index 1f827e6def..f94c629174 100644
--- a/nss/nss_database.h
+++ b/nss/nss_database.h
@@ -75,6 +75,10 @@ struct nss_database_data
   nss_action_list services[NSS_DATABASE_COUNT];
   int reload_disabled;          /* Actually bool; int for atomic access.  */
   bool initialized;
+  /* If "/" changes, we switched into a container and do NOT want to
+     reload anything.  */
+  ino64_t root_ino;
+  ino64_t root_dev;
 };
 
 /* Called by fork in the parent process, before forking.  */


^ permalink raw reply related	[flat|nested] 25+ messages in thread

end of thread, other threads:[~2021-01-28  1:15 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-16  0:59 nsswitch: do not reload if "/" changes DJ Delorie via Libc-alpha
2021-01-16 10:52 ` Florian Weimer via Libc-alpha
2021-01-18  1:13   ` DJ Delorie via Libc-alpha
2021-01-18 10:47     ` Florian Weimer via Libc-alpha
2021-01-18 18:20       ` DJ Delorie via Libc-alpha
2021-01-19 16:37         ` Florian Weimer via Libc-alpha
2021-01-22 19:10           ` [v2] " DJ Delorie via Libc-alpha
2021-01-26  9:58             ` Florian Weimer via Libc-alpha
2021-01-26 16:19               ` DJ Delorie via Libc-alpha
2021-01-26 16:30                 ` Florian Weimer via Libc-alpha
2021-01-26 16:47                   ` DJ Delorie via Libc-alpha
2021-01-27 17:28                     ` Carlos O'Donell via Libc-alpha
2021-01-27 18:44                       ` DJ Delorie via Libc-alpha
2021-01-28  0:31                         ` Joseph Myers
2021-01-28  0:34                           ` DJ Delorie via Libc-alpha
2021-01-28  0:39                             ` Joseph Myers
2021-01-28  1:15                               ` DJ Delorie via Libc-alpha
2021-01-18 12:42 ` Andreas Schwab
2021-01-18 12:53   ` Florian Weimer via Libc-alpha
2021-01-18 18:27   ` DJ Delorie via Libc-alpha
2021-01-18 15:59 ` Carlos O'Donell via Libc-alpha
2021-01-18 16:53   ` Florian Weimer via Libc-alpha
2021-01-19 14:30     ` Carlos O'Donell via Libc-alpha
2021-01-19 14:40       ` Florian Weimer via Libc-alpha
2021-01-18 18:35   ` DJ Delorie via Libc-alpha

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).