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

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