ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
dd65c9
From 3a68810cd6ac23f7107491ab6e1fbd565ed52bf0 Mon Sep 17 00:00:00 2001
dd65c9
From: Lennart Poettering <lennart@poettering.net>
dd65c9
Date: Wed, 8 Apr 2015 22:35:52 +0200
dd65c9
Subject: [PATCH] tmpfiles: rework file attribute code
dd65c9
dd65c9
- Stick to one type for the flags field: unsigned. This appears to be
dd65c9
  what the kernel uses, and there's no point in using something else.
dd65c9
dd65c9
- compress the flags array by avoiding sparse entries
dd65c9
dd65c9
- extend some error messages to not use abbreviated words
dd65c9
dd65c9
- avoid TTOCTTOU issues by invoking fstat() after open() when applying
dd65c9
  file flags
dd65c9
dd65c9
- add explanation why we need to check the file type with fstat().
dd65c9
dd65c9
- don't needlessly abbreviate "attribute" as "attrib", in particually as
dd65c9
  "chattr" abbreviates it as "attr" rather than "attrib".
dd65c9
dd65c9
(cherry picked from commit 88ec4dfa289cd97496dbb9670365a3d4be13d41c)
dd65c9
dd65c9
Conflicts:
dd65c9
	src/tmpfiles/tmpfiles.c
dd65c9
dd65c9
Related: #1299714
dd65c9
---
23b3cf
 src/tmpfiles/tmpfiles.c | 207 ++++++++++++++++++++++------------------
dd65c9
 1 file changed, 114 insertions(+), 93 deletions(-)
dd65c9
dd65c9
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
dd65c9
index c8c56c722..800e620bc 100644
dd65c9
--- a/src/tmpfiles/tmpfiles.c
dd65c9
+++ b/src/tmpfiles/tmpfiles.c
dd65c9
@@ -92,8 +92,8 @@ typedef enum ItemType {
dd65c9
         RELABEL_PATH = 'z',
dd65c9
         RECURSIVE_RELABEL_PATH = 'Z',
dd65c9
         ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
dd65c9
-        SET_ATTRIB = 'h',
dd65c9
-        RECURSIVE_SET_ATTRIB = 'H',
dd65c9
+        SET_ATTRIBUTE = 'h',
dd65c9
+        RECURSIVE_SET_ATTRIBUTE = 'H',
dd65c9
 } ItemType;
dd65c9
 
dd65c9
 typedef struct Item {
dd65c9
@@ -112,15 +112,15 @@ typedef struct Item {
dd65c9
         usec_t age;
dd65c9
 
dd65c9
         dev_t major_minor;
dd65c9
-        unsigned long attrib_value;
dd65c9
-        unsigned long attrib_mask;
dd65c9
+        unsigned attribute_value;
dd65c9
+        unsigned attribute_mask;
dd65c9
 
dd65c9
         bool uid_set:1;
dd65c9
         bool gid_set:1;
dd65c9
         bool mode_set:1;
dd65c9
         bool age_set:1;
dd65c9
         bool mask_perms:1;
dd65c9
-        bool attrib_set:1;
dd65c9
+        bool attribute_set:1;
dd65c9
 
dd65c9
         bool keep_first_level:1;
dd65c9
 
dd65c9
@@ -823,123 +823,144 @@ static int path_set_acls(Item *item, const char *path) {
dd65c9
         return r;
dd65c9
 }
dd65c9
 
dd65c9
-#define ALL_ATTRIBS          \
dd65c9
-        FS_NOATIME_FL      | \
dd65c9
-        FS_SYNC_FL         | \
dd65c9
-        FS_DIRSYNC_FL      | \
dd65c9
-        FS_APPEND_FL       | \
dd65c9
-        FS_COMPR_FL        | \
dd65c9
-        FS_NODUMP_FL       | \
dd65c9
-        FS_EXTENT_FL       | \
dd65c9
-        FS_IMMUTABLE_FL    | \
dd65c9
-        FS_JOURNAL_DATA_FL | \
dd65c9
-        FS_SECRM_FL        | \
dd65c9
-        FS_UNRM_FL         | \
dd65c9
-        FS_NOTAIL_FL       | \
dd65c9
-        FS_TOPDIR_FL       | \
dd65c9
-        FS_NOCOW_FL
dd65c9
-
dd65c9
-static int get_attrib_from_arg(Item *item) {
dd65c9
-        static const unsigned attributes[] = {
dd65c9
-                [(uint8_t)'A'] = FS_NOATIME_FL,      /* do not update atime */
dd65c9
-                [(uint8_t)'S'] = FS_SYNC_FL,         /* Synchronous updates */
dd65c9
-                [(uint8_t)'D'] = FS_DIRSYNC_FL,      /* dirsync behaviour (directories only) */
dd65c9
-                [(uint8_t)'a'] = FS_APPEND_FL,       /* writes to file may only append */
dd65c9
-                [(uint8_t)'c'] = FS_COMPR_FL,        /* Compress file */
dd65c9
-                [(uint8_t)'d'] = FS_NODUMP_FL,       /* do not dump file */
dd65c9
-                [(uint8_t)'e'] = FS_EXTENT_FL,       /* Top of directory hierarchies*/
dd65c9
-                [(uint8_t)'i'] = FS_IMMUTABLE_FL,    /* Immutable file */
dd65c9
-                [(uint8_t)'j'] = FS_JOURNAL_DATA_FL, /* Reserved for ext3 */
dd65c9
-                [(uint8_t)'s'] = FS_SECRM_FL,        /* Secure deletion */
dd65c9
-                [(uint8_t)'u'] = FS_UNRM_FL,         /* Undelete */
dd65c9
-                [(uint8_t)'t'] = FS_NOTAIL_FL,       /* file tail should not be merged */
dd65c9
-                [(uint8_t)'T'] = FS_TOPDIR_FL,       /* Top of directory hierarchies*/
dd65c9
-                [(uint8_t)'C'] = FS_NOCOW_FL,        /* Do not cow file */
dd65c9
+#define ATTRIBUTES_ALL                          \
dd65c9
+        (FS_NOATIME_FL      |                   \
dd65c9
+         FS_SYNC_FL         |                   \
dd65c9
+         FS_DIRSYNC_FL      |                   \
dd65c9
+         FS_APPEND_FL       |                   \
dd65c9
+         FS_COMPR_FL        |                   \
dd65c9
+         FS_NODUMP_FL       |                   \
dd65c9
+         FS_EXTENT_FL       |                   \
dd65c9
+         FS_IMMUTABLE_FL    |                   \
dd65c9
+         FS_JOURNAL_DATA_FL |                   \
dd65c9
+         FS_SECRM_FL        |                   \
dd65c9
+         FS_UNRM_FL         |                   \
dd65c9
+         FS_NOTAIL_FL       |                   \
dd65c9
+         FS_TOPDIR_FL       |                   \
dd65c9
+         FS_NOCOW_FL)
dd65c9
+
dd65c9
+static int get_attribute_from_arg(Item *item) {
dd65c9
+
dd65c9
+        static const struct {
dd65c9
+                char character;
dd65c9
+                unsigned value;
dd65c9
+        } attributes[] = {
dd65c9
+                { 'A', FS_NOATIME_FL },      /* do not update atime */
dd65c9
+                { 'S', FS_SYNC_FL },         /* Synchronous updates */
dd65c9
+                { 'D', FS_DIRSYNC_FL },      /* dirsync behaviour (directories only) */
dd65c9
+                { 'a', FS_APPEND_FL },       /* writes to file may only append */
dd65c9
+                { 'c', FS_COMPR_FL },        /* Compress file */
dd65c9
+                { 'd', FS_NODUMP_FL },       /* do not dump file */
dd65c9
+                { 'e', FS_EXTENT_FL },       /* Top of directory hierarchies*/
dd65c9
+                { 'i', FS_IMMUTABLE_FL },    /* Immutable file */
dd65c9
+                { 'j', FS_JOURNAL_DATA_FL }, /* Reserved for ext3 */
dd65c9
+                { 's', FS_SECRM_FL },        /* Secure deletion */
dd65c9
+                { 'u', FS_UNRM_FL },         /* Undelete */
dd65c9
+                { 't', FS_NOTAIL_FL },       /* file tail should not be merged */
dd65c9
+                { 'T', FS_TOPDIR_FL },       /* Top of directory hierarchies*/
dd65c9
+                { 'C', FS_NOCOW_FL },        /* Do not cow file */
dd65c9
         };
dd65c9
-        char *p = item->argument;
dd65c9
+
dd65c9
         enum {
dd65c9
                 MODE_ADD,
dd65c9
                 MODE_DEL,
dd65c9
                 MODE_SET
dd65c9
         } mode = MODE_ADD;
dd65c9
-        unsigned long value = 0, mask = 0;
dd65c9
 
dd65c9
-        if (!p) {
dd65c9
-                log_error("\"%s\": setting ATTR need an argument", item->path);
dd65c9
-                return -EINVAL;
dd65c9
-        }
dd65c9
+        unsigned value = 0, mask = 0;
dd65c9
+        const char *p;
dd65c9
 
dd65c9
-        if (*p == '+') {
dd65c9
-                mode = MODE_ADD;
dd65c9
-                p++;
dd65c9
-        } else if (*p == '-') {
dd65c9
-                mode = MODE_DEL;
dd65c9
-                p++;
dd65c9
-        } else  if (*p == '=') {
dd65c9
-                mode = MODE_SET;
dd65c9
-                p++;
dd65c9
+        assert(item);
dd65c9
+
dd65c9
+        p = item->argument;
dd65c9
+        if (p) {
dd65c9
+                if (*p == '+') {
dd65c9
+                        mode = MODE_ADD;
dd65c9
+                        p++;
dd65c9
+                } else if (*p == '-') {
dd65c9
+                        mode = MODE_DEL;
dd65c9
+                        p++;
dd65c9
+                } else  if (*p == '=') {
dd65c9
+                        mode = MODE_SET;
dd65c9
+                        p++;
dd65c9
+                }
dd65c9
         }
dd65c9
 
dd65c9
-        if (!*p && mode != MODE_SET) {
dd65c9
-                log_error("\"%s\": setting ATTR: argument is empty", item->path);
dd65c9
+        if (isempty(p) && mode != MODE_SET) {
dd65c9
+                log_error("Setting file attribute on '%s' needs an attribute specification.", item->path);
dd65c9
                 return -EINVAL;
dd65c9
         }
dd65c9
-        for (; *p ; p++) {
dd65c9
-                if ((uint8_t)*p > ELEMENTSOF(attributes) || attributes[(uint8_t)*p] == 0) {
dd65c9
-                        log_error("\"%s\": setting ATTR: unknown attr '%c'", item->path, *p);
dd65c9
+
dd65c9
+        for (; p && *p ; p++) {
dd65c9
+                unsigned i, v;
dd65c9
+
dd65c9
+                for (i = 0; i < ELEMENTSOF(attributes); i++)
dd65c9
+                        if (*p == attributes[i].character)
dd65c9
+                                break;
dd65c9
+
dd65c9
+                if (i >= ELEMENTSOF(attributes)) {
dd65c9
+                        log_error("Unknown file attribute '%c' on '%s'.", *p, item->path);
dd65c9
                         return -EINVAL;
dd65c9
                 }
dd65c9
+
dd65c9
+                v = attributes[i].value;
dd65c9
+
dd65c9
                 if (mode == MODE_ADD || mode == MODE_SET)
dd65c9
-                        value |= attributes[(uint8_t)*p];
dd65c9
+                        value |= v;
dd65c9
                 else
dd65c9
-                        value &= ~attributes[(uint8_t)*p];
dd65c9
-                mask |= attributes[(uint8_t)*p];
dd65c9
+                        value &= ~v;
dd65c9
+
dd65c9
+                mask |= v;
dd65c9
         }
dd65c9
 
dd65c9
         if (mode == MODE_SET)
dd65c9
-                mask |= ALL_ATTRIBS;
dd65c9
+                mask |= ATTRIBUTES_ALL;
dd65c9
 
dd65c9
-        assert(mask);
dd65c9
+        assert(mask != 0);
dd65c9
 
dd65c9
-        item->attrib_mask = mask;
dd65c9
-        item->attrib_value = value;
dd65c9
-        item->attrib_set = true;
dd65c9
+        item->attribute_mask = mask;
dd65c9
+        item->attribute_value = value;
dd65c9
+        item->attribute_set = true;
dd65c9
 
dd65c9
         return 0;
dd65c9
-
dd65c9
 }
dd65c9
 
dd65c9
-static int path_set_attrib(Item *item, const char *path) {
dd65c9
+static int path_set_attribute(Item *item, const char *path) {
dd65c9
         _cleanup_close_ int fd = -1;
dd65c9
-        int r;
dd65c9
-        unsigned f;
dd65c9
         struct stat st;
dd65c9
+        unsigned f;
dd65c9
+        int r;
dd65c9
 
dd65c9
-        /* do nothing */
dd65c9
-        if (item->attrib_mask == 0 || !item->attrib_set)
dd65c9
-                return 0;
dd65c9
-        /*
dd65c9
-         * It is OK to ignore an lstat() error, because the error
dd65c9
-         * will be catch by the open() below anyway
dd65c9
-         */
dd65c9
-        if (lstat(path, &st) == 0 &&
dd65c9
-            !S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
dd65c9
+        if (!item->attribute_set || item->attribute_mask == 0)
dd65c9
                 return 0;
dd65c9
-        }
dd65c9
 
dd65c9
         fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
dd65c9
 
dd65c9
         if (fd < 0)
dd65c9
-                return log_error_errno(errno, "Cannot open \"%s\": %m", path);
dd65c9
+                return log_error_errno(errno, "Cannot open '%s': %m", path);
dd65c9
 
dd65c9
-        f = item->attrib_value & item->attrib_mask;
dd65c9
+        if (fstat(fd, &st) < 0)
dd65c9
+                return log_error_errno(errno, "Cannot stat '%s': %m", path);
dd65c9
+
dd65c9
+        /* Issuing the file attribute ioctls on device nodes is not
dd65c9
+         * safe, as that will be delivered to the drivers, not the
dd65c9
+         * file system containing the device node. */
dd65c9
+        if (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
dd65c9
+                log_error("Setting file flags is only supported on regular files and directories, cannot set on '%s'.", path);
dd65c9
+                return -EINVAL;
dd65c9
+        }
dd65c9
+
dd65c9
+        f = item->attribute_value & item->attribute_mask;
dd65c9
+
dd65c9
+        /* Mask away directory-specific flags */
dd65c9
         if (!S_ISDIR(st.st_mode))
dd65c9
                 f &= ~FS_DIRSYNC_FL;
dd65c9
-        r = change_attr_fd(fd, f, item->attrib_mask);
dd65c9
+
dd65c9
+        r = chattr_fd(fd, f, item->attribute_mask);
dd65c9
         if (r < 0)
dd65c9
-                return log_error_errno(errno,
dd65c9
-                        "Cannot set attrib for \"%s\", value=0x%08lx, mask=0x%08lx: %m",
dd65c9
-                        path, item->attrib_value, item->attrib_mask);
dd65c9
+                return log_error_errno(r,
dd65c9
+                        "Cannot set file attribute for '%s', value=0x%08x, mask=0x%08x: %m",
dd65c9
+                        path, item->attribute_value, item->attribute_mask);
dd65c9
 
dd65c9
         return 0;
dd65c9
 }
dd65c9
@@ -1394,14 +1415,14 @@ static int create_item(Item *i) {
dd65c9
                         return r;
dd65c9
                 break;
dd65c9
 
dd65c9
-        case SET_ATTRIB:
dd65c9
-                r = glob_item(i, path_set_attrib, false);
dd65c9
+        case SET_ATTRIBUTE:
dd65c9
+                r = glob_item(i, path_set_attribute, false);
dd65c9
                 if (r < 0)
dd65c9
                         return r;
dd65c9
                 break;
dd65c9
 
dd65c9
-        case RECURSIVE_SET_ATTRIB:
dd65c9
-                r = glob_item(i, path_set_attrib, true);
dd65c9
+        case RECURSIVE_SET_ATTRIBUTE:
dd65c9
+                r = glob_item(i, path_set_attribute, true);
dd65c9
                 if (r < 0)
dd65c9
                         return r;
dd65c9
                 break;
dd65c9
@@ -1851,13 +1872,13 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
dd65c9
                         return r;
dd65c9
                 break;
dd65c9
 
dd65c9
-        case SET_ATTRIB:
dd65c9
-        case RECURSIVE_SET_ATTRIB:
dd65c9
+        case SET_ATTRIBUTE:
dd65c9
+        case RECURSIVE_SET_ATTRIBUTE:
dd65c9
                 if (!i.argument) {
dd65c9
-                        log_error("[%s:%u] Set attrib requires argument.", fname, line);
dd65c9
+                        log_error("[%s:%u] Set file attribute requires argument.", fname, line);
dd65c9
                         return -EBADMSG;
dd65c9
                 }
dd65c9
-                r = get_attrib_from_arg(&i);
dd65c9
+                r = get_attribute_from_arg(&i);
dd65c9
                 if (r < 0)
dd65c9
                         return r;
dd65c9
                 break;