naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
984f77
From f62231a54abff6566415ea82aa8773e61f5688d6 Mon Sep 17 00:00:00 2001
984f77
From: Michal Sekletar <msekleta@redhat.com>
984f77
Date: Tue, 15 Mar 2022 19:02:05 +0100
984f77
Subject: [PATCH] core: shorten long unit names that are based on paths and
984f77
 append path hash at the end
984f77
984f77
Fixes #18077
984f77
984f77
(cherry picked from commit 1d0727e76fd5e9a07cc9991ec9a10ea1d78a99c7)
984f77
984f77
Resolves: #1940973
984f77
---
984f77
 src/basic/string-util.h   | 23 +++++-----
984f77
 src/basic/unit-name.c     | 88 ++++++++++++++++++++++++++++++++++++++-
984f77
 src/basic/unit-name.h     |  3 ++
984f77
 src/core/mount.c          |  3 ++
984f77
 src/test/test-unit-name.c | 25 ++++++++++-
984f77
 5 files changed, 129 insertions(+), 13 deletions(-)
984f77
984f77
diff --git a/src/basic/string-util.h b/src/basic/string-util.h
984f77
index 742b566932..0d406ff64a 100644
984f77
--- a/src/basic/string-util.h
984f77
+++ b/src/basic/string-util.h
984f77
@@ -9,17 +9,18 @@
984f77
 #include "macro.h"
984f77
 
984f77
 /* What is interpreted as whitespace? */
984f77
-#define WHITESPACE        " \t\n\r"
984f77
-#define NEWLINE           "\n\r"
984f77
-#define QUOTES            "\"\'"
984f77
-#define COMMENTS          "#;"
984f77
-#define GLOB_CHARS        "*?["
984f77
-#define DIGITS            "0123456789"
984f77
-#define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
984f77
-#define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
984f77
-#define LETTERS           LOWERCASE_LETTERS UPPERCASE_LETTERS
984f77
-#define ALPHANUMERICAL    LETTERS DIGITS
984f77
-#define HEXDIGITS         DIGITS "abcdefABCDEF"
984f77
+#define WHITESPACE          " \t\n\r"
984f77
+#define NEWLINE             "\n\r"
984f77
+#define QUOTES              "\"\'"
984f77
+#define COMMENTS            "#;"
984f77
+#define GLOB_CHARS          "*?["
984f77
+#define DIGITS              "0123456789"
984f77
+#define LOWERCASE_LETTERS   "abcdefghijklmnopqrstuvwxyz"
984f77
+#define UPPERCASE_LETTERS   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
984f77
+#define LETTERS             LOWERCASE_LETTERS UPPERCASE_LETTERS
984f77
+#define ALPHANUMERICAL      LETTERS DIGITS
984f77
+#define HEXDIGITS           DIGITS "abcdefABCDEF"
984f77
+#define LOWERCASE_HEXDIGITS DIGITS "abcdef"
984f77
 
984f77
 #define streq(a,b) (strcmp((a),(b)) == 0)
984f77
 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
984f77
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
984f77
index f9b3fafd4d..65ed979e39 100644
984f77
--- a/src/basic/unit-name.c
984f77
+++ b/src/basic/unit-name.c
984f77
@@ -6,11 +6,17 @@
984f77
 #include <stdlib.h>
984f77
 #include <string.h>
984f77
 
984f77
+#include "sd-id128.h"
984f77
+
984f77
 #include "alloc-util.h"
984f77
 #include "glob-util.h"
984f77
 #include "hexdecoct.h"
984f77
 #include "path-util.h"
984f77
+#include "random-util.h"
984f77
+#include "siphash24.h"
984f77
+#include "sparse-endian.h"
984f77
 #include "special.h"
984f77
+#include "stdio-util.h"
984f77
 #include "string-util.h"
984f77
 #include "strv.h"
984f77
 #include "unit-name.h"
984f77
@@ -31,6 +37,9 @@
984f77
         VALID_CHARS_WITH_AT                     \
984f77
         "[]!-*?"
984f77
 
984f77
+#define LONG_UNIT_NAME_HASH_KEY SD_ID128_MAKE(ec,f2,37,fb,58,32,4a,32,84,9f,06,9b,0d,21,eb,9a)
984f77
+#define UNIT_NAME_HASH_LENGTH_CHARS 16
984f77
+
984f77
 bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
984f77
         const char *e, *i, *at;
984f77
 
984f77
@@ -513,6 +522,68 @@ int unit_name_template(const char *f, char **ret) {
984f77
         return 0;
984f77
 }
984f77
 
984f77
+bool unit_name_is_hashed(const char *name) {
984f77
+        char *s;
984f77
+
984f77
+        if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
984f77
+                return false;
984f77
+
984f77
+        assert_se(s = strrchr(name, '.'));
984f77
+
984f77
+        if (s - name < UNIT_NAME_HASH_LENGTH_CHARS + 1)
984f77
+                return false;
984f77
+
984f77
+        s -= UNIT_NAME_HASH_LENGTH_CHARS;
984f77
+        if (s[-1] != '_')
984f77
+                return false;
984f77
+
984f77
+        for (size_t i = 0; i < UNIT_NAME_HASH_LENGTH_CHARS; i++)
984f77
+                if (!strchr(LOWERCASE_HEXDIGITS, s[i]))
984f77
+                        return false;
984f77
+
984f77
+        return true;
984f77
+}
984f77
+
984f77
+int unit_name_hash_long(const char *name, char **ret) {
984f77
+        _cleanup_free_ char *n = NULL, *hash = NULL;
984f77
+        char *suffix;
984f77
+        le64_t h;
984f77
+        size_t len;
984f77
+
984f77
+        if (strlen(name) < UNIT_NAME_MAX)
984f77
+                return -EMSGSIZE;
984f77
+
984f77
+        suffix = strrchr(name, '.');
984f77
+        if (!suffix)
984f77
+                return -EINVAL;
984f77
+
984f77
+        if (unit_type_from_string(suffix+1) < 0)
984f77
+                return -EINVAL;
984f77
+
984f77
+        h = htole64(siphash24(name, strlen(name) + 1, LONG_UNIT_NAME_HASH_KEY.bytes));
984f77
+
984f77
+        hash = hexmem(&h, sizeof(h));
984f77
+        if (!hash)
984f77
+                return -ENOMEM;
984f77
+
984f77
+        assert_se(strlen(hash) == UNIT_NAME_HASH_LENGTH_CHARS);
984f77
+
984f77
+        len = UNIT_NAME_MAX - 1 - strlen(suffix+1) - UNIT_NAME_HASH_LENGTH_CHARS - 2;
984f77
+        assert(len > 0 && len < UNIT_NAME_MAX);
984f77
+
984f77
+        n = strndup(name, len);
984f77
+        if (!n)
984f77
+                return -ENOMEM;
984f77
+
984f77
+        if (!strextend(&n, "_", hash, suffix, NULL))
984f77
+                return -ENOMEM;
984f77
+        assert_se(unit_name_is_valid(n, UNIT_NAME_PLAIN));
984f77
+
984f77
+        *ret = TAKE_PTR(n);
984f77
+
984f77
+        return 0;
984f77
+}
984f77
+
984f77
 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
984f77
         _cleanup_free_ char *p = NULL, *s = NULL;
984f77
         int r;
984f77
@@ -532,7 +603,19 @@ int unit_name_from_path(const char *path, const char *suffix, char **ret) {
984f77
         if (!s)
984f77
                 return -ENOMEM;
984f77
 
984f77
-        /* Refuse this if this got too long or for some other reason didn't result in a valid name */
984f77
+        if (strlen(s) >= UNIT_NAME_MAX) {
984f77
+                _cleanup_free_ char *n = NULL;
984f77
+
984f77
+                log_debug("Unit name \"%s\" too long, falling back to hashed unit name.", s);
984f77
+
984f77
+                r = unit_name_hash_long(s, &n);
984f77
+                if (r < 0)
984f77
+                        return r;
984f77
+
984f77
+                free_and_replace(s, n);
984f77
+        }
984f77
+
984f77
+        /* Refuse if this for some other reason didn't result in a valid name */
984f77
         if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
984f77
                 return -EINVAL;
984f77
 
984f77
@@ -582,6 +665,9 @@ int unit_name_to_path(const char *name, char **ret) {
984f77
         if (r < 0)
984f77
                 return r;
984f77
 
984f77
+        if (unit_name_is_hashed(name))
984f77
+                return -ENAMETOOLONG;
984f77
+
984f77
         return unit_name_path_unescape(prefix, ret);
984f77
 }
984f77
 
984f77
diff --git a/src/basic/unit-name.h b/src/basic/unit-name.h
984f77
index 61abcd585b..602295af8f 100644
984f77
--- a/src/basic/unit-name.h
984f77
+++ b/src/basic/unit-name.h
984f77
@@ -45,6 +45,9 @@ int unit_name_replace_instance(const char *f, const char *i, char **ret);
984f77
 
984f77
 int unit_name_template(const char *f, char **ret);
984f77
 
984f77
+int unit_name_hash_long(const char *name, char **ret);
984f77
+bool unit_name_is_hashed(const char *name);
984f77
+
984f77
 int unit_name_from_path(const char *path, const char *suffix, char **ret);
984f77
 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret);
984f77
 int unit_name_to_path(const char *name, char **ret);
984f77
diff --git a/src/core/mount.c b/src/core/mount.c
984f77
index d37b5731f8..e69ecb7ce3 100644
984f77
--- a/src/core/mount.c
984f77
+++ b/src/core/mount.c
984f77
@@ -572,6 +572,9 @@ static int mount_add_extras(Mount *m) {
984f77
 
984f77
         if (!m->where) {
984f77
                 r = unit_name_to_path(u->id, &m->where);
984f77
+                if (r == -ENAMETOOLONG)
984f77
+                        log_unit_error_errno(u, r, "Failed to derive mount point path from unit name, because unit name is hashed. "
984f77
+                                                   "Set \"Where=\" in the unit file explicitly.");
984f77
                 if (r < 0)
984f77
                         return r;
984f77
         }
984f77
diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
984f77
index 2b00ef8cb7..35cfaafd30 100644
984f77
--- a/src/test/test-unit-name.c
984f77
+++ b/src/test/test-unit-name.c
984f77
@@ -82,6 +82,7 @@ static void test_unit_name_replace_instance(void) {
984f77
 
984f77
 static void test_unit_name_from_path_one(const char *path, const char *suffix, const char *expected, int ret) {
984f77
         _cleanup_free_ char *t = NULL;
984f77
+        int r;
984f77
 
984f77
         assert_se(unit_name_from_path(path, suffix, &t) == ret);
984f77
         puts(strna(t));
984f77
@@ -89,12 +90,31 @@ static void test_unit_name_from_path_one(const char *path, const char *suffix, c
984f77
 
984f77
         if (t) {
984f77
                 _cleanup_free_ char *k = NULL;
984f77
-                assert_se(unit_name_to_path(t, &k) == 0);
984f77
+
984f77
+                /* We don't support converting hashed unit names back to paths */
984f77
+                r = unit_name_to_path(t, &k);
984f77
+                if (r == -ENAMETOOLONG)
984f77
+                        return;
984f77
+                assert(r == 0);
984f77
+
984f77
                 puts(strna(k));
984f77
                 assert_se(path_equal(k, empty_to_root(path)));
984f77
         }
984f77
 }
984f77
 
984f77
+static void test_unit_name_is_hashed(void) {
984f77
+        assert_se(!unit_name_is_hashed(""));
984f77
+        assert_se(!unit_name_is_hashed("foo@bar.service"));
984f77
+        assert_se(!unit_name_is_hashed("foo@.service"));
984f77
+        assert_se(unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736d9ed33c2ec55.mount"));
984f77
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736D9ED33C2EC55.mount"));
984f77
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!7736d9ed33c2ec55.mount"));
984f77
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736d9gd33c2ec55.mount"));
984f77
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_.mount"));
984f77
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_2103e1466b87f7f7@waldo.mount"));
984f77
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_2103e1466b87f7f7@.mount"));
984f77
+}
984f77
+
984f77
 static void test_unit_name_from_path(void) {
984f77
         puts("-------------------------------------------------");
984f77
         test_unit_name_from_path_one("/waldo", ".mount", "waldo.mount", 0);
984f77
@@ -105,6 +125,8 @@ static void test_unit_name_from_path(void) {
984f77
         test_unit_name_from_path_one("///", ".mount", "-.mount", 0);
984f77
         test_unit_name_from_path_one("/foo/../bar", ".mount", NULL, -EINVAL);
984f77
         test_unit_name_from_path_one("/foo/./bar", ".mount", NULL, -EINVAL);
984f77
+        test_unit_name_from_path_one("/waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ".mount",
984f77
+                                     "waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736d9ed33c2ec55.mount", 0);
984f77
 }
984f77
 
984f77
 static void test_unit_name_from_path_instance_one(const char *pattern, const char *path, const char *suffix, const char *expected, int ret) {
984f77
@@ -824,6 +846,7 @@ int main(int argc, char* argv[]) {
984f77
 
984f77
         test_unit_name_is_valid();
984f77
         test_unit_name_replace_instance();
984f77
+        test_unit_name_is_hashed();
984f77
         test_unit_name_from_path();
984f77
         test_unit_name_from_path_instance();
984f77
         test_unit_name_mangle();