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