Blame SOURCES/0523-tmpfiles-rework-file-attribute-code.patch

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