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