daandemeyer / rpms / systemd

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