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

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