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

Maciej Wereski m.wereski at partner.samsung.com
Wed Dec 4 06:27:45 PST 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
---
I'm sorry for late reply, but there were some unexpected events which
excluded me from life for a while.

v5:
* fixes for HAVE_XATTR undefined
* use cunescape() instead of strreplace()
* cache result of strv_length()
* fix typo in manpage

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 | 216 +++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 225 insertions(+), 17 deletions(-)

diff --git a/man/tmpfiles.d.xml b/man/tmpfiles.d.xml
index 1c079f6..676eded 100644
--- a/man/tmpfiles.d.xml
+++ b/man/tmpfiles.d.xml
@@ -248,6 +248,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>
 
@@ -312,7 +327,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>
@@ -324,7 +339,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>
@@ -377,8 +392,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. For t determines extended
+                        attributes to be set. Ignored for all other
                         lines.</para>
+
                 </refsect2>
 
         </refsect1>
@@ -390,7 +407,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>
                 <example>
                         <title>/etc/tmpfiles.d/abrt.conf example</title>
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index b7f6a2e..ec5efb6 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"
@@ -78,7 +81,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 {
@@ -86,6 +92,7 @@ typedef struct Item {
 
         char *path;
         char *argument;
+        char **xattrs;
         uid_t uid;
         gid_t gid;
         mode_t mode;
@@ -465,6 +472,113 @@ static int item_set_perms(Item *i, const char *path) {
         return item_set_perms_full(i, path, false);
 }
 
+#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, strv_len;
+
+        assert(i);
+        if (i->type != SET_XATTR)
+                return 0;
+
+        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();
+
+        strv_len = strv_length(tmp);
+        for (n = 0; n < strv_len; ++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_len)
+                                        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 item_set_xattrs(Item *i, const char *path) {
+#ifdef HAVE_XATTR
+        char **x;
+        int n;
+
+        assert(i);
+        assert(path);
+
+        n = get_xattrs_from_arg(i);
+        if (n < 0)
+                return 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 = cunescape(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;
+                }
+        }
+#else
+        (void)i;
+        (void)path;
+#endif
+        return 0;
+}
+
 static int write_one_file(Item *i, const char *path) {
         int e, flags;
         int fd = -1;
@@ -527,6 +641,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;
 }
 
@@ -703,6 +821,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:
@@ -730,6 +852,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: {
@@ -759,6 +885,11 @@ static int create_item(Item *i) {
                 }
 
                 free(x);
+
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                       return r;
+
                 break;
         }
 
@@ -805,6 +936,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;
         }
 
@@ -813,6 +948,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:
@@ -820,6 +961,12 @@ static int create_item(Item *i) {
                 r = glob_item(i, recursive_relabel);
                 if (r < 0)
                         return r;
+                break;
+
+        case SET_XATTR:
+                r = item_set_xattrs(i, i->path);
+                if (r < 0)
+                        return r;
         }
 
         log_debug("%s created successfully.", i->path);
@@ -847,6 +994,7 @@ static int remove_item_instance(Item *i, const char *instance) {
         case RECURSIVE_RELABEL_PATH:
         case WRITE_FILE:
         case ADJUST_MODE:
+        case SET_XATTR:
                 break;
 
         case REMOVE_PATH:
@@ -893,6 +1041,7 @@ static int remove_item(Item *i) {
         case RECURSIVE_RELABEL_PATH:
         case WRITE_FILE:
         case ADJUST_MODE:
+        case SET_XATTR:
                 break;
 
         case REMOVE_PATH:
@@ -999,6 +1148,7 @@ static void item_free(Item *i) {
 
         free(i->path);
         free(i->argument);
+        strv_free(i->xattrs);
         free(i);
 }
 
@@ -1167,6 +1317,17 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
                 break;
         }
 
+        case SET_XATTR:
+#ifndef HAVE_XATTR
+                log_error("Setting extended attributes requested, but tmpfiles was compiled without XATTR support!");
+                return -ENOTSUP;
+#endif
+                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;
@@ -1243,18 +1404,47 @@ 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;
+#ifdef HAVE_XATTR
+                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 {
+#endif
+                        /* 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;
+#ifdef HAVE_XATTR
+                }
+#endif
+        } 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.4.2



More information about the systemd-devel mailing list