Blame SOURCES/0002-main-Allow-cache-files-to-be-marked-immutable.patch

70a90c
From 12127d9c04e8151c51bd14114dce424ff8448345 Mon Sep 17 00:00:00 2001
70a90c
From: Ray Strode <rstrode@redhat.com>
70a90c
Date: Thu, 9 Sep 2021 09:40:49 -0400
70a90c
Subject: [PATCH 2/2] main: Allow cache files to be marked immutable
70a90c
70a90c
At the moment, at start up we unconditionally reset permission of all
70a90c
cache files in /var/lib/AccountsService/users.  If the mode of the files
70a90c
can't be reset, accountsservice fails to start.
70a90c
70a90c
But there's a situation where we should proceed anyway: If the
70a90c
mode is already correct, and the file is read-only, there is no reason
70a90c
to refuse to proceed.
70a90c
70a90c
This commit changes the code to explicitly validate the permissions of
70a90c
the file before failing.
70a90c
---
70a90c
 src/main.c | 29 +++++++++++++++++++++++++----
70a90c
 1 file changed, 25 insertions(+), 4 deletions(-)
70a90c
70a90c
diff --git a/src/main.c b/src/main.c
70a90c
index 01cb617..36a2d7e 100644
70a90c
--- a/src/main.c
70a90c
+++ b/src/main.c
70a90c
@@ -16,143 +16,164 @@
70a90c
  * along with this program; if not, write to the Free Software
70a90c
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
70a90c
  *
70a90c
  * Written by: Matthias Clasen <mclasen@redhat.com>
70a90c
  */
70a90c
 
70a90c
 #include "config.h"
70a90c
 
70a90c
 #include <stdlib.h>
70a90c
 #include <stdarg.h>
70a90c
 #include <locale.h>
70a90c
 #include <libintl.h>
70a90c
 #include <syslog.h>
70a90c
 #include <sys/stat.h>
70a90c
 #include <errno.h>
70a90c
 
70a90c
 #include <glib.h>
70a90c
 #include <glib/gi18n.h>
70a90c
 #include <glib/gstdio.h>
70a90c
 #include <glib-unix.h>
70a90c
 
70a90c
 #include "daemon.h"
70a90c
 
70a90c
 #define NAME_TO_CLAIM "org.freedesktop.Accounts"
70a90c
 
70a90c
 static gboolean
70a90c
 ensure_directory (const char  *path,
70a90c
                   gint         mode,
70a90c
                   GError     **error)
70a90c
 {
70a90c
+        GStatBuf stat_buffer = { 0 };
70a90c
+
70a90c
         if (g_mkdir_with_parents (path, mode) < 0) {
70a90c
                 g_set_error (error,
70a90c
                              G_FILE_ERROR,
70a90c
                              g_file_error_from_errno (errno),
70a90c
                              "Failed to create directory %s: %m",
70a90c
                              path);
70a90c
                 return FALSE;
70a90c
         }
70a90c
 
70a90c
-        if (g_chmod (path, mode) < 0) {
70a90c
+        g_chmod (path, mode);
70a90c
+
70a90c
+        if (g_stat (path, &stat_buffer) < 0) {
70a90c
+                g_clear_error (error);
70a90c
+
70a90c
                 g_set_error (error,
70a90c
                              G_FILE_ERROR,
70a90c
                              g_file_error_from_errno (errno),
70a90c
-                             "Failed to change permissions of directory %s: %m",
70a90c
+                             "Failed to validate permissions of directory %s: %m",
70a90c
                              path);
70a90c
                 return FALSE;
70a90c
         }
70a90c
 
70a90c
+        if ((stat_buffer.st_mode & ~S_IFMT) != mode) {
70a90c
+                g_set_error (error,
70a90c
+                             G_FILE_ERROR,
70a90c
+                             g_file_error_from_errno (errno),
70a90c
+                             "Directory %s has wrong mode %o; it should be %o",
70a90c
+                             path, stat_buffer.st_mode, mode);
70a90c
+                return FALSE;
70a90c
+        }
70a90c
+
70a90c
         return TRUE;
70a90c
 }
70a90c
 
70a90c
 static gboolean
70a90c
 ensure_file_permissions (const char  *dir_path,
70a90c
                          gint         file_mode,
70a90c
                          GError     **error)
70a90c
 {
70a90c
         GDir *dir = NULL;
70a90c
         const gchar *filename;
70a90c
         gint errsv = 0;
70a90c
 
70a90c
         dir = g_dir_open (dir_path, 0, error);
70a90c
         if (dir == NULL)
70a90c
                 return FALSE;
70a90c
 
70a90c
         while ((filename = g_dir_read_name (dir)) != NULL) {
70a90c
+                GStatBuf stat_buffer = { 0 };
70a90c
+
70a90c
                 gchar *file_path = g_build_filename (dir_path, filename, NULL);
70a90c
 
70a90c
                 g_debug ("Changing permission of %s to %04o", file_path, file_mode);
70a90c
-                if (g_chmod (file_path, file_mode) < 0)
70a90c
+                g_chmod (file_path, file_mode);
70a90c
+
70a90c
+                if (g_stat (file_path, &stat_buffer) < 0)
70a90c
                         errsv = errno;
70a90c
 
70a90c
+                if ((stat_buffer.st_mode & ~S_IFMT) != file_mode)
70a90c
+                        errsv = EACCES;
70a90c
+
70a90c
                 g_free (file_path);
70a90c
         }
70a90c
 
70a90c
         g_dir_close (dir);
70a90c
 
70a90c
         /* Report any errors after all chmod()s have been attempted. */
70a90c
         if (errsv != 0) {
70a90c
                 g_set_error (error,
70a90c
                              G_FILE_ERROR,
70a90c
                              g_file_error_from_errno (errsv),
70a90c
                              "Failed to change permissions of files in directory %s: %m",
70a90c
                              dir_path);
70a90c
                 return FALSE;
70a90c
         }
70a90c
 
70a90c
         return TRUE;
70a90c
 }
70a90c
 
70a90c
 static void
70a90c
 on_bus_acquired (GDBusConnection  *connection,
70a90c
                  const gchar      *name,
70a90c
                  gpointer          user_data)
70a90c
 {
70a90c
         GMainLoop *loop = user_data;
70a90c
         Daemon *daemon;
70a90c
         g_autoptr(GError) error = NULL;
70a90c
 
70a90c
         if (!ensure_directory (ICONDIR, 0775, &error) ||
70a90c
             !ensure_directory (USERDIR, 0700, &error) ||
70a90c
             !ensure_file_permissions (USERDIR, 0600, &error)) {
70a90c
                 g_printerr ("%s\n", error->message);
70a90c
                 g_main_loop_quit (loop);
70a90c
                 return;
70a90c
         }
70a90c
 
70a90c
         daemon = daemon_new ();
70a90c
         if (daemon == NULL) {
70a90c
                 g_printerr ("Failed to initialize daemon\n");
70a90c
                 g_main_loop_quit (loop);
70a90c
                 return;
70a90c
         }
70a90c
-
70a90c
         openlog ("accounts-daemon", LOG_PID, LOG_DAEMON);
70a90c
         syslog (LOG_INFO, "started daemon version %s", VERSION);
70a90c
         closelog ();
70a90c
         openlog ("accounts-daemon", 0, LOG_AUTHPRIV);
70a90c
 }
70a90c
 
70a90c
 static void
70a90c
 on_name_lost (GDBusConnection  *connection,
70a90c
               const gchar      *name,
70a90c
               gpointer          user_data)
70a90c
 {
70a90c
         GMainLoop *loop = user_data;
70a90c
 
70a90c
         g_debug ("got NameLost, exiting");
70a90c
         g_main_loop_quit (loop);
70a90c
 }
70a90c
 
70a90c
 static gboolean debug;
70a90c
 
70a90c
 static void
70a90c
 on_log_debug (const gchar *log_domain,
70a90c
               GLogLevelFlags log_level,
70a90c
               const gchar *message,
70a90c
               gpointer user_data)
70a90c
 {
70a90c
         g_autoptr(GString) string = NULL;
70a90c
         const gchar *progname;
70a90c
         int ret G_GNUC_UNUSED;
70a90c
 
70a90c
         string = g_string_new (NULL);
70a90c
-- 
70a90c
2.31.1
70a90c