[systemd-devel] [PATCHv4] tmpfiles, man: Add xattr support to tmpfiles

Maciej Wereski m.wereski at partner.samsung.com
Thu Aug 8 04:54:11 PDT 2013


This patch makes it possible to set extended attributes on files created
by tmpfiles. This can be especially used to set SMACK security labels on
volatile files and directories.

It is done by adding new line of type "t". Such line should contain
attributes in Argument field, using following format:

name=value

All other fields are ignored.

If value contains spaces, then it must be surrounded by quotation marks.
User can also put quotation mark in value by escaping it with backslash.

Example:
D /var/run/cups - - - -
t /var/run/cups - - - - security.SMACK64=printing
---
v4:
* grammar fix in man
* style fix

v3:
* "may be used" instead of "should be used" in manpage
* use strv_isempty() instead of != NULL
* rework item_set_xattrs() with split_pair()
* remove copy_item_contents()
* use hashmap_replace() instead of removed copy_item_contents()
* use strv_extend() instead of strv_append()
* cleanup
---
 man/tmpfiles.d.xml      |  26 ++++++-
 src/tmpfiles/tmpfiles.c | 203 +++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 213 insertions(+), 16 deletions(-)

diff --git a/man/tmpfiles.d.xml b/man/tmpfiles.d.xml
index 6a2193d..41226c3 100644
--- a/man/tmpfiles.d.xml
+++ b/man/tmpfiles.d.xml
@@ -229,6 +229,21 @@ L    /tmp/foobar -    -    -    -   /dev/null</programlisting>
                                         place of normal path
                                         names.</para></listitem>
                                 </varlistentry>
+
+                                <varlistentry>
+                                        <term><varname>t</varname></term>
+                                        <listitem><para>Set extended
+                                        attributes on item. It may be
+                                        used in conjunction with other
+                                        types (only d, D, f, F, L, p, c, b, z
+                                        makes sense). If used as a standalone
+                                        line, then <command>systemd-tmpfiles
+                                        </command> will try to set extended
+                                        attributes on specified path.
+                                        This can be especially used to set
+                                        SMACK labels.
+                                        </para></listitem>
+                                </varlistentry>
                         </variablelist>
                 </refsect2>
 
@@ -242,7 +257,7 @@ L    /tmp/foobar -    -    -    -   /dev/null</programlisting>
                         objects. For z, Z lines if omitted or when set
                         to - the file access mode will not be
                         modified. This parameter is ignored for x, r,
-                        R, L lines.</para>
+                        R, L, t lines.</para>
                 </refsect2>
 
                 <refsect2>
@@ -254,7 +269,7 @@ L    /tmp/foobar -    -    -    -   /dev/null</programlisting>
                         omitted or when set to - the default 0 (root)
                         is used. For z, Z lines when omitted or when set to -
                         the file ownership will not be modified.
-                        These parameters are ignored for x, r, R, L lines.</para>
+                        These parameters are ignored for x, r, R, L, t lines.</para>
                 </refsect2>
 
                 <refsect2>
@@ -307,8 +322,10 @@ L    /tmp/foobar -    -    -    -   /dev/null</programlisting>
                         minor formatted as integers, separated by :,
                         e.g. "1:3". For f, F, w may be used to specify
                         a short string that is written to the file,
-                        suffixed by a newline. Ignored for all other
+                        suffixed by a newline. Fot t determines extended
+                        attributes to be set. Ignored for all other
                         lines.</para>
+
                 </refsect2>
 
         </refsect1>
@@ -320,7 +337,8 @@ L    /tmp/foobar -    -    -    -   /dev/null</programlisting>
                         <para><command>screen</command> needs two directories created at boot with specific modes and ownership.</para>
 
                         <programlisting>d /var/run/screens  1777 root root 10d
-d /var/run/uscreens 0755 root root 10d12h</programlisting>
+d /var/run/uscreens 0755 root root 10d12h
+t /var/run/screen - - - - user.name="John Koval" security.SMACK64=screen</programlisting>
                 </example>
         </refsect1>
 
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index 5eca82a..a6594b1 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -39,6 +39,9 @@
 #include <glob.h>
 #include <fnmatch.h>
 #include <sys/capability.h>
+#ifdef HAVE_XATTR
+#include <attr/xattr.h>
+#endif
 
 #include "log.h"
 #include "util.h"
@@ -75,7 +78,10 @@ typedef enum ItemType {
         REMOVE_PATH = 'r',
         RECURSIVE_REMOVE_PATH = 'R',
         RELABEL_PATH = 'z',
-        RECURSIVE_RELABEL_PATH = 'Z'
+        RECURSIVE_RELABEL_PATH = 'Z',
+
+        /* These ones are options/additional operations */
+        SET_XATTR = 't'
 } ItemType;
 
 typedef struct Item {
@@ -83,6 +89,7 @@ typedef struct Item {
 
         char *path;
         char *argument;
+        char **xattrs;
         uid_t uid;
         gid_t gid;
         mode_t mode;
@@ -448,6 +455,45 @@ static int item_set_perms(Item *i, const char *path) {
         return label_fix(path, false, false);
 }
 
+static int item_set_xattrs(Item *i, const char *path) {
+#ifdef HAVE_XATTR
+        char **x;
+        int n;
+        if (strv_isempty(i->xattrs))
+                return 0;
+        STRV_FOREACH(x, i->xattrs) {
+                _cleanup_free_ char *name = NULL, *value = NULL, *tmp = NULL;
+                n = split_pair(*x, "=", &name, &value);
+                if (n < 0)
+                        return n;
+                tmp = unquote(value, "\"");
+                if (!tmp)
+                        return log_oom();
+                free(value);
+                value = strreplace(tmp, "\\\"", "\"");
+                if (!value)
+                        return log_oom();
+                n = strlen(value);
+                if (i->type == CREATE_SYMLINK) {
+                        if (lsetxattr(path, name, value, n+1, 0) < 0) {
+                                log_error("Setting extended attribute %s=%s on symlink %s failed: %m", name, value, path);
+                                return -errno;
+                        }
+                }
+                else if (setxattr(path, name, value, n+1, 0) < 0) {
+                        log_error("Setting extended attribute %s=%s on %s failed: %m", name, value, path);
+                        return -errno;
+                }
+        }
+        return 0;
+#else
+        (void)i;
+        (void)path;
+        log_error("Setting extended attributes requested, but tmpfiles was compiled without XATTR support!");
+        return -ENOTSUP;
+#endif
+}
+
 static int write_one_file(Item *i, const char *path) {
         int r, e, fd, flags;
         struct stat st;
@@ -508,6 +554,10 @@ static int write_one_file(Item *i, const char *path) {
         if (r < 0)
                 return r;
 
+        r = item_set_xattrs(i, i->path);
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
@@ -596,6 +646,67 @@ static int recursive_relabel(Item *i, const char *path) {
         return r;
 }
 
+#ifdef HAVE_XATTR
+static int get_xattrs_from_arg(Item *i){
+        _cleanup_free_ char *xattr = NULL;
+        _cleanup_strv_free_ char **tmp = NULL;
+        char *p;
+        unsigned n, len;
+
+        assert(i);
+        assert(i->type == SET_XATTR);
+
+        if (!i->argument) {
+                log_error("%s: Argument can't be empty!", i->path);
+                return -EBADMSG;
+        }
+
+        xattr = new0(char, strlen(i->argument)+1);
+        if (!xattr)
+                return log_oom();
+
+        tmp = strv_split(i->argument, WHITESPACE);
+        if (!tmp)
+                return log_oom();
+
+        for (n = 0; n < strv_length(tmp); ++n) {
+                len = strlen(tmp[n]);
+                strncpy(xattr, tmp[n], len+1);
+                p = strchr(xattr, '=');
+                if (!p) {
+                        log_error("%s: Attribute has incorrect format.", i->path);
+                        return -EBADMSG;
+                }
+                if (p[1] == '\"') {
+                        while (true) {
+                                if (!p)
+                                        p = tmp[n];
+                                else
+                                        p += 2;
+                                p = strchr(p, '\"');
+                                if (p && xattr[p-xattr-1] != '\\')
+                                        break;
+                                p = NULL;
+                                ++n;
+                                if (n == strv_length(tmp))
+                                        break;
+                                len += strlen(tmp[n]) + 1;
+                                strncat(xattr, " ", 1);
+                                strncat(xattr, tmp[n], len);
+                        }
+                }
+                strstrip(xattr);
+                if (strv_extend(&i->xattrs, xattr) < 0)
+                        return log_oom();
+        }
+
+        free(i->argument);
+        i->argument = NULL;
+
+        return 0;
+}
+#endif
+
 static int glob_item(Item *i, int (*action)(Item *, const char *)) {
         int r = 0, k;
         _cleanup_globfree_ glob_t g = {};
@@ -675,6 +786,10 @@ static int create_item(Item *i) {
                 if (r < 0)
                         return r;
 
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                        return r;
+
                 break;
 
         case CREATE_FIFO:
@@ -702,6 +817,10 @@ static int create_item(Item *i) {
                 if (r < 0)
                         return r;
 
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                        return r;
+
                 break;
 
         case CREATE_SYMLINK: {
@@ -731,6 +850,11 @@ static int create_item(Item *i) {
                 }
 
                 free(x);
+
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                       return r;
+
                 break;
         }
 
@@ -777,6 +901,10 @@ static int create_item(Item *i) {
                 if (r < 0)
                         return r;
 
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                        return r;
+
                 break;
         }
 
@@ -785,6 +913,12 @@ static int create_item(Item *i) {
                 r = glob_item(i, item_set_perms);
                 if (r < 0)
                         return r;
+
+                if (i->xattrs) {
+                        r = glob_item(i, item_set_xattrs);
+                        if (r < 0)
+                                return r;
+                }
                 break;
 
         case RECURSIVE_RELABEL_PATH:
@@ -792,6 +926,15 @@ static int create_item(Item *i) {
                 r = glob_item(i, recursive_relabel);
                 if (r < 0)
                         return r;
+                break;
+
+        case SET_XATTR:
+                r = get_xattrs_from_arg(i);
+                if (r < 0)
+                        return r;
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                        return r;
         }
 
         log_debug("%s created successfully.", i->path);
@@ -818,6 +961,7 @@ static int remove_item_instance(Item *i, const char *instance) {
         case RELABEL_PATH:
         case RECURSIVE_RELABEL_PATH:
         case WRITE_FILE:
+        case SET_XATTR:
                 break;
 
         case REMOVE_PATH:
@@ -863,6 +1007,7 @@ static int remove_item(Item *i) {
         case RELABEL_PATH:
         case RECURSIVE_RELABEL_PATH:
         case WRITE_FILE:
+        case SET_XATTR:
                 break;
 
         case REMOVE_PATH:
@@ -969,6 +1114,7 @@ static void item_free(Item *i) {
 
         free(i->path);
         free(i->argument);
+        strv_free(i->xattrs);
         free(i);
 }
 
@@ -1124,6 +1270,13 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                 break;
         }
 
+        case SET_XATTR:
+                if (!i->argument) {
+                        log_error("[%s:%u] Set extended attribute requires argument.", fname, line);
+                        return -EBADMSG;
+                }
+                break;
+
         default:
                 log_error("[%s:%u] Unknown file type '%c'.", fname, line, type);
                 return -EBADMSG;
@@ -1201,17 +1354,43 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
         existing = hashmap_get(h, i->path);
         if (existing) {
 
-                /* Two identical items are fine */
-                if (!item_equal(existing, i))
-                        log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
-
-                return 0;
-        }
-
-        r = hashmap_put(h, i->path, i);
-        if (r < 0) {
-                log_error("Failed to insert item %s: %s", i->path, strerror(-r));
-                return r;
+                if (i->type == SET_XATTR) {
+                        char **tmp;
+                        r = get_xattrs_from_arg(i);
+                        if (r < 0)
+                                return r;
+                        tmp = existing->xattrs;
+                        existing->xattrs = strv_merge(existing->xattrs, i->xattrs);
+                        if (!existing->xattrs) {
+                                strv_free(tmp);
+                                return log_oom();
+                        }
+                        return 0;
+                } else if (existing->type == SET_XATTR) {
+                        r = get_xattrs_from_arg(existing);
+                        if (r < 0)
+                                return r;
+                        i->xattrs = strv_merge(i->xattrs, existing->xattrs);
+                        if (!i->xattrs)
+                                return log_oom();
+                        r = hashmap_replace(h, i->path, i);
+                        if (r < 0) {
+                                log_error("Failed to replace item for %s.", i->path);
+                                return r;
+                        }
+                        item_free(existing);
+                } else {
+                        /* Two identical items are fine */
+                        if (!item_equal(existing, i))
+                                log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
+                        return 0;
+                }
+        } else {
+                r = hashmap_put(h, i->path, i);
+                if (r < 0) {
+                        log_error("Failed to insert item %s: %s", i->path, strerror(-r));
+                        return r;
+                }
         }
 
         i = NULL; /* avoid cleanup */
-- 
1.8.3.4



More information about the systemd-devel mailing list