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