richardphibel / rpms / systemd

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