08c3a6
commit 94ab2088c37d8e4285354af120b7ed6b887b9e53
08c3a6
Author: Sam James <sam@gentoo.org>
08c3a6
Date:   Sun Jun 5 04:57:10 2022 +0100
08c3a6
08c3a6
    nss: handle stat failure in check_reload_and_get (BZ #28752)
08c3a6
    
08c3a6
    Skip the chroot test if the database isn't loaded
08c3a6
    correctly (because the chroot test uses some
08c3a6
    existing DB state).
08c3a6
    
08c3a6
    The __stat64_time64 -> fstatat call can fail if
08c3a6
    running under an (aggressive) seccomp filter,
08c3a6
    like Firefox seems to use.
08c3a6
    
08c3a6
    This manifested in a crash when using glib built
08c3a6
    with FAM support with such a Firefox build.
08c3a6
    
08c3a6
    Suggested-by: DJ Delorie <dj@redhat.com>
08c3a6
    Signed-off-by: Sam James <sam@gentoo.org>
08c3a6
    Reviewed-by: DJ Delorie <dj@redhat.com>
08c3a6
    (cherry picked from commit ace9e3edbca62d978b1e8f392d8a5d78500272d9)
08c3a6
08c3a6
diff --git a/nss/nss_database.c b/nss/nss_database.c
08c3a6
index 54561f03287db2e4..e807e9d84ca03680 100644
08c3a6
--- a/nss/nss_database.c
08c3a6
+++ b/nss/nss_database.c
08c3a6
@@ -420,23 +420,32 @@ nss_database_check_reload_and_get (struct nss_database_state *local,
08c3a6
       return true;
08c3a6
     }
08c3a6
 
08c3a6
-  /* Before we reload, verify that "/" hasn't changed.  We assume that
08c3a6
-     errors here are very unlikely, but the chance that we're entering
08c3a6
-     a container is also very unlikely, so we err on the side of both
08c3a6
-     very unlikely things not happening at the same time.  */
08c3a6
-  if (__stat64_time64 ("/", &str) != 0
08c3a6
-      || (local->root_ino != 0
08c3a6
-	  && (str.st_ino != local->root_ino
08c3a6
-	      ||  str.st_dev != local->root_dev)))
08c3a6
+  int stat_rv = __stat64_time64 ("/", &str);
08c3a6
+
08c3a6
+  if (local->data.services[database_index] != NULL)
08c3a6
     {
08c3a6
-      /* Change detected; disable reloading and return current state.  */
08c3a6
-      atomic_store_release (&local->data.reload_disabled, 1);
08c3a6
-      *result = local->data.services[database_index];
08c3a6
-      __libc_lock_unlock (local->lock);
08c3a6
-      return true;
08c3a6
+      /* Before we reload, verify that "/" hasn't changed.  We assume that
08c3a6
+        errors here are very unlikely, but the chance that we're entering
08c3a6
+        a container is also very unlikely, so we err on the side of both
08c3a6
+        very unlikely things not happening at the same time.  */
08c3a6
+      if (stat_rv != 0
08c3a6
+	  || (local->root_ino != 0
08c3a6
+	      && (str.st_ino != local->root_ino
08c3a6
+		  ||  str.st_dev != local->root_dev)))
08c3a6
+	{
08c3a6
+        /* Change detected; disable reloading and return current state.  */
08c3a6
+        atomic_store_release (&local->data.reload_disabled, 1);
08c3a6
+        *result = local->data.services[database_index];
08c3a6
+        __libc_lock_unlock (local->lock);
08c3a6
+        return true;
08c3a6
+      }
08c3a6
+    }
08c3a6
+  if (stat_rv == 0)
08c3a6
+    {
08c3a6
+      local->root_ino = str.st_ino;
08c3a6
+      local->root_dev = str.st_dev;
08c3a6
     }
08c3a6
-  local->root_ino = str.st_ino;
08c3a6
-  local->root_dev = str.st_dev;
08c3a6
+
08c3a6
   __libc_lock_unlock (local->lock);
08c3a6
 
08c3a6
   /* Avoid overwriting the global configuration until we have loaded