teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

Blame SOURCES/0300-tmpfiles-don-t-follow-symlinks-when-adjusting-ACLs-f.patch

923a60
From 595a93d716680715a751737ec2f87b06ea582763 Mon Sep 17 00:00:00 2001
923a60
From: Lennart Poettering <lennart@poettering.net>
923a60
Date: Mon, 13 Apr 2015 15:16:54 +0200
923a60
Subject: [PATCH] tmpfiles: don't follow symlinks when adjusting ACLs, fille
923a60
 attributes, access modes or ownership
923a60
923a60
Cherry-picked from: 48b8aaa82724bc2d8440470f414fb0d2416f29c
923a60
Resolves: #1296288
923a60
---
923a60
 src/tmpfiles/tmpfiles.c | 112 ++++++++++++++++++++++++++--------------
923a60
 1 file changed, 74 insertions(+), 38 deletions(-)
923a60
923a60
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
923a60
index d0e6567d8a..64c733aaa0 100644
923a60
--- a/src/tmpfiles/tmpfiles.c
923a60
+++ b/src/tmpfiles/tmpfiles.c
923a60
@@ -570,51 +570,69 @@ finish:
923a60
 }
923a60
 
923a60
 static int path_set_perms(Item *i, const char *path) {
923a60
+        _cleanup_close_ int fd = -1;
923a60
         struct stat st;
923a60
-        bool st_valid;
923a60
 
923a60
         assert(i);
923a60
         assert(path);
923a60
 
923a60
-        st_valid = stat(path, &st) == 0;
923a60
+        /* We open the file with O_PATH here, to make the operation
923a60
+         * somewhat atomic. Also there's unfortunately no fchmodat()
923a60
+         * with AT_SYMLINK_NOFOLLOW, hence we emulate it here via
923a60
+         * O_PATH. */
923a60
+
923a60
+        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_NOATIME);
923a60
+        if (fd < 0)
923a60
+                return log_error_errno(errno, "Adjusting owner and mode for %s failed: %m", path);
923a60
+
923a60
+        if (fstatat(fd, "", &st, AT_EMPTY_PATH) < 0)
923a60
+                return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
923a60
+
923a60
+        if (S_ISLNK(st.st_mode))
923a60
+                log_debug("Skipping mode an owner fix for symlink %s.", path);
923a60
+        else {
923a60
+                char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
923a60
+                xsprintf(fn, "/proc/self/fd/%i", fd);
923a60
 
923a60
-        /* not using i->path directly because it may be a glob */
923a60
-        if (i->mode_set) {
923a60
-                mode_t m = i->mode;
923a60
+                /* not using i->path directly because it may be a glob */
923a60
+                if (i->mode_set) {
923a60
+                        mode_t m = i->mode;
923a60
 
923a60
-                if (i->mask_perms && st_valid) {
923a60
-                        if (!(st.st_mode & 0111))
923a60
-                                m &= ~0111;
923a60
-                        if (!(st.st_mode & 0222))
923a60
+                        if (i->mask_perms) {
923a60
+                                if (!(st.st_mode & 0111))
923a60
+                                        m &= ~0111;
923a60
+                                if (!(st.st_mode & 0222))
923a60
                                 m &= ~0222;
923a60
-                        if (!(st.st_mode & 0444))
923a60
-                                m &= ~0444;
923a60
-                        if (!S_ISDIR(st.st_mode))
923a60
-                                m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
923a60
-                }
923a60
+                                if (!(st.st_mode & 0444))
923a60
+                                        m &= ~0444;
923a60
+                                if (!S_ISDIR(st.st_mode))
923a60
+                                        m &= ~07000; /* remove sticky/sgid/suid bit, unless directory */
923a60
+                        }
923a60
 
923a60
-                if (st_valid && m == (st.st_mode & 07777))
923a60
-                        log_debug("\"%s\" has right mode %o", path, st.st_mode);
923a60
-                else {
923a60
-                        log_debug("chmod \"%s\" to mode %o", path, m);
923a60
-                        if (chmod(path, m) < 0)
923a60
-                                return log_error_errno(errno, "chmod(%s) failed: %m", path);
923a60
+                        if (m == (st.st_mode & 07777))
923a60
+                                log_debug("\"%s\" has right mode %o", path, st.st_mode);
923a60
+                        else {
923a60
+                                log_debug("chmod \"%s\" to mode %o", path, m);
923a60
+                                if (chmod(fn, m) < 0)
923a60
+                                        return log_error_errno(errno, "chmod(%s) failed: %m", path);
923a60
+                        }
923a60
                 }
923a60
-        }
923a60
-
923a60
-        if ((!st_valid || i->uid != st.st_uid || i->gid != st.st_gid) &&
923a60
-            (i->uid_set || i->gid_set)) {
923a60
-                log_debug("chown \"%s\" to "UID_FMT"."GID_FMT,
923a60
-                          path,
923a60
-                          i->uid_set ? i->uid : UID_INVALID,
923a60
-                          i->gid_set ? i->gid : GID_INVALID);
923a60
-                if (chown(path,
923a60
-                          i->uid_set ? i->uid : UID_INVALID,
923a60
-                          i->gid_set ? i->gid : GID_INVALID) < 0)
923a60
 
923a60
+                if ((i->uid != st.st_uid || i->gid != st.st_gid) &&
923a60
+                    (i->uid_set || i->gid_set)) {
923a60
+                        log_debug("chown \"%s\" to "UID_FMT"."GID_FMT,
923a60
+                                  path,
923a60
+                                  i->uid_set ? i->uid : UID_INVALID,
923a60
+                                  i->gid_set ? i->gid : GID_INVALID);
923a60
+                        if (chown(fn,
923a60
+                                  i->uid_set ? i->uid : UID_INVALID,
923a60
+                                  i->gid_set ? i->gid : GID_INVALID) < 0)
923a60
                         return log_error_errno(errno, "chown(%s) failed: %m", path);
923a60
+                }
923a60
         }
923a60
 
923a60
+        fd = safe_close(fd);
923a60
+
923a60
         return label_fix(path, false, false);
923a60
 }
923a60
 
923a60
@@ -699,10 +717,10 @@ static int get_acls_from_arg(Item *item) {
923a60
 }
923a60
 
923a60
 #ifdef HAVE_ACL
923a60
-static int path_set_acl(const char *path, acl_type_t type, acl_t acl, bool modify) {
923a60
+static int path_set_acl(const char *path, const char *pretty, acl_type_t type, acl_t acl, bool modify) {
923a60
+        _cleanup_(acl_free_charpp) char *t = NULL;
923a60
         _cleanup_(acl_freep) acl_t dup = NULL;
923a60
         int r;
923a60
-        _cleanup_(acl_free_charpp) char *t = NULL;
923a60
 
923a60
         /* Returns 0 for success, positive error if already warned,
923a60
          * negative error otherwise. */
923a60
@@ -728,16 +746,16 @@ static int path_set_acl(const char *path, acl_type_t type, acl_t acl, bool modif
923a60
                 return r;
923a60
 
923a60
         t = acl_to_any_text(dup, NULL, ',', TEXT_ABBREVIATE);
923a60
-        log_debug("\"%s\": setting %s ACL \"%s\"", path,
923a60
+        log_debug("Setting %s ACL %s on %s.",
923a60
                   type == ACL_TYPE_ACCESS ? "access" : "default",
923a60
-                  strna(t));
923a60
+                  strna(t), pretty);
923a60
 
923a60
         r = acl_set_file(path, type, dup);
923a60
         if (r < 0)
923a60
                 return -log_error_errno(errno,
923a60
                                         "Setting %s ACL \"%s\" on %s failed: %m",
923a60
                                         type == ACL_TYPE_ACCESS ? "access" : "default",
923a60
-                                        strna(t), path);
923a60
+                                        strna(t), pretty);
923a60
 
923a60
         return 0;
923a60
 }
923a60
@@ -746,14 +764,32 @@ static int path_set_acl(const char *path, acl_type_t type, acl_t acl, bool modif
923a60
 static int path_set_acls(Item *item, const char *path) {
923a60
         int r = 0;
923a60
 #ifdef HAVE_ACL
923a60
+        char fn[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
923a60
+        _cleanup_close_ int fd = -1;
923a60
+        struct stat st;
923a60
+
923a60
         assert(item);
923a60
         assert(path);
923a60
 
923a60
+        fd = open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC|O_PATH|O_NOATIME);
923a60
+        if (fd < 0)
923a60
+                return log_error_errno(errno, "Adjusting ACL of %s failed: %m", path);
923a60
+
923a60
+        if (fstatat(fd, "", &st, AT_EMPTY_PATH) < 0)
923a60
+                return log_error_errno(errno, "Failed to fstat() file %s: %m", path);
923a60
+
923a60
+        if (S_ISLNK(st.st_mode)) {
923a60
+                log_debug("Skipping ACL fix for symlink %s.", path);
923a60
+                return 0;
923a60
+        }
923a60
+
923a60
+        xsprintf(fn, "/proc/self/fd/%i", fd);
923a60
+
923a60
         if (item->acl_access)
923a60
-                r = path_set_acl(path, ACL_TYPE_ACCESS, item->acl_access, item->force);
923a60
+                r = path_set_acl(fn, path, ACL_TYPE_ACCESS, item->acl_access, item->force);
923a60
 
923a60
         if (r == 0 && item->acl_default)
923a60
-                r = path_set_acl(path, ACL_TYPE_DEFAULT, item->acl_default, item->force);
923a60
+                r = path_set_acl(fn, path, ACL_TYPE_DEFAULT, item->acl_default, item->force);
923a60
 
923a60
         if (r > 0)
923a60
                 return -r; /* already warned */