ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
8d419f
From 4bc17b038971160f94321c7be9cd924b256d9ef8 Mon Sep 17 00:00:00 2001
8d419f
From: Michal Sekletar <msekleta@redhat.com>
8d419f
Date: Tue, 15 Mar 2022 19:02:05 +0100
8d419f
Subject: [PATCH] core: shorten long unit names that are based on paths and
8d419f
 append path hash at the end
8d419f
8d419f
Fixes #18077
8d419f
8d419f
(cherry picked from commit 1d0727e76fd5e9a07cc9991ec9a10ea1d78a99c7)
8d419f
8d419f
Resolves: #2083493
8d419f
---
8d419f
 src/basic/string-util.h   | 23 ++++++-----
8d419f
 src/basic/unit-name.c     | 86 ++++++++++++++++++++++++++++++++++++++-
8d419f
 src/basic/unit-name.h     |  3 ++
8d419f
 src/core/mount.c          |  3 ++
8d419f
 src/test/test-unit-name.c | 26 ++++++++++--
8d419f
 5 files changed, 125 insertions(+), 16 deletions(-)
8d419f
8d419f
diff --git a/src/basic/string-util.h b/src/basic/string-util.h
8d419f
index a1d88fbb95..ffb69e69cc 100644
8d419f
--- a/src/basic/string-util.h
8d419f
+++ b/src/basic/string-util.h
8d419f
@@ -10,17 +10,18 @@
8d419f
 #include "string-util-fundamental.h"
8d419f
 
8d419f
 /* What is interpreted as whitespace? */
8d419f
-#define WHITESPACE        " \t\n\r"
8d419f
-#define NEWLINE           "\n\r"
8d419f
-#define QUOTES            "\"\'"
8d419f
-#define COMMENTS          "#;"
8d419f
-#define GLOB_CHARS        "*?["
8d419f
-#define DIGITS            "0123456789"
8d419f
-#define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
8d419f
-#define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8d419f
-#define LETTERS           LOWERCASE_LETTERS UPPERCASE_LETTERS
8d419f
-#define ALPHANUMERICAL    LETTERS DIGITS
8d419f
-#define HEXDIGITS         DIGITS "abcdefABCDEF"
8d419f
+#define WHITESPACE          " \t\n\r"
8d419f
+#define NEWLINE             "\n\r"
8d419f
+#define QUOTES              "\"\'"
8d419f
+#define COMMENTS            "#;"
8d419f
+#define GLOB_CHARS          "*?["
8d419f
+#define DIGITS              "0123456789"
8d419f
+#define LOWERCASE_LETTERS   "abcdefghijklmnopqrstuvwxyz"
8d419f
+#define UPPERCASE_LETTERS   "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
8d419f
+#define LETTERS             LOWERCASE_LETTERS UPPERCASE_LETTERS
8d419f
+#define ALPHANUMERICAL      LETTERS DIGITS
8d419f
+#define HEXDIGITS           DIGITS "abcdefABCDEF"
8d419f
+#define LOWERCASE_HEXDIGITS DIGITS "abcdef"
8d419f
 
8d419f
 static inline char* strstr_ptr(const char *haystack, const char *needle) {
8d419f
         if (!haystack || !needle)
8d419f
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
8d419f
index 671e30a53f..6cba8ba140 100644
8d419f
--- a/src/basic/unit-name.c
8d419f
+++ b/src/basic/unit-name.c
8d419f
@@ -5,12 +5,17 @@
8d419f
 #include <stdint.h>
8d419f
 #include <stdlib.h>
8d419f
 
8d419f
+#include "sd-id128.h"
8d419f
+
8d419f
 #include "alloc-util.h"
8d419f
 #include "glob-util.h"
8d419f
 #include "hexdecoct.h"
8d419f
 #include "memory-util.h"
8d419f
 #include "path-util.h"
8d419f
+#include "random-util.h"
8d419f
+#include "sparse-endian.h"
8d419f
 #include "special.h"
8d419f
+#include "stdio-util.h"
8d419f
 #include "string-util.h"
8d419f
 #include "strv.h"
8d419f
 #include "unit-name.h"
8d419f
@@ -31,6 +36,9 @@
8d419f
         VALID_CHARS_WITH_AT                     \
8d419f
         "[]!-*?"
8d419f
 
8d419f
+#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)
8d419f
+#define UNIT_NAME_HASH_LENGTH_CHARS 16
8d419f
+
8d419f
 bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
8d419f
         const char *e, *i, *at;
8d419f
 
8d419f
@@ -507,6 +515,68 @@ int unit_name_template(const char *f, char **ret) {
8d419f
         return 0;
8d419f
 }
8d419f
 
8d419f
+bool unit_name_is_hashed(const char *name) {
8d419f
+        char *s;
8d419f
+
8d419f
+        if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
8d419f
+                return false;
8d419f
+
8d419f
+        assert_se(s = strrchr(name, '.'));
8d419f
+
8d419f
+        if (s - name < UNIT_NAME_HASH_LENGTH_CHARS + 1)
8d419f
+                return false;
8d419f
+
8d419f
+        s -= UNIT_NAME_HASH_LENGTH_CHARS;
8d419f
+        if (s[-1] != '_')
8d419f
+                return false;
8d419f
+
8d419f
+        for (size_t i = 0; i < UNIT_NAME_HASH_LENGTH_CHARS; i++)
8d419f
+                if (!strchr(LOWERCASE_HEXDIGITS, s[i]))
8d419f
+                        return false;
8d419f
+
8d419f
+        return true;
8d419f
+}
8d419f
+
8d419f
+int unit_name_hash_long(const char *name, char **ret) {
8d419f
+        _cleanup_free_ char *n = NULL, *hash = NULL;
8d419f
+        char *suffix;
8d419f
+        le64_t h;
8d419f
+        size_t len;
8d419f
+
8d419f
+        if (strlen(name) < UNIT_NAME_MAX)
8d419f
+                return -EMSGSIZE;
8d419f
+
8d419f
+        suffix = strrchr(name, '.');
8d419f
+        if (!suffix)
8d419f
+                return -EINVAL;
8d419f
+
8d419f
+        if (unit_type_from_string(suffix+1) < 0)
8d419f
+                return -EINVAL;
8d419f
+
8d419f
+        h = htole64(siphash24_string(name, LONG_UNIT_NAME_HASH_KEY.bytes));
8d419f
+
8d419f
+        hash = hexmem(&h, sizeof(h));
8d419f
+        if (!hash)
8d419f
+                return -ENOMEM;
8d419f
+
8d419f
+        assert_se(strlen(hash) == UNIT_NAME_HASH_LENGTH_CHARS);
8d419f
+
8d419f
+        len = UNIT_NAME_MAX - 1 - strlen(suffix+1) - UNIT_NAME_HASH_LENGTH_CHARS - 2;
8d419f
+        assert(len > 0 && len < UNIT_NAME_MAX);
8d419f
+
8d419f
+        n = strndup(name, len);
8d419f
+        if (!n)
8d419f
+                return -ENOMEM;
8d419f
+
8d419f
+        if (!strextend(&n, "_", hash, suffix))
8d419f
+                return -ENOMEM;
8d419f
+        assert_se(unit_name_is_valid(n, UNIT_NAME_PLAIN));
8d419f
+
8d419f
+        *ret = TAKE_PTR(n);
8d419f
+
8d419f
+        return 0;
8d419f
+}
8d419f
+
8d419f
 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
8d419f
         _cleanup_free_ char *p = NULL, *s = NULL;
8d419f
         int r;
8d419f
@@ -526,8 +596,17 @@ int unit_name_from_path(const char *path, const char *suffix, char **ret) {
8d419f
         if (!s)
8d419f
                 return -ENOMEM;
8d419f
 
8d419f
-        if (strlen(s) >= UNIT_NAME_MAX) /* Return a slightly more descriptive error for this specific condition */
8d419f
-                return -ENAMETOOLONG;
8d419f
+        if (strlen(s) >= UNIT_NAME_MAX) {
8d419f
+                _cleanup_free_ char *n = NULL;
8d419f
+
8d419f
+                log_debug("Unit name \"%s\" too long, falling back to hashed unit name.", s);
8d419f
+
8d419f
+                r = unit_name_hash_long(s, &n);
8d419f
+                if (r < 0)
8d419f
+                        return r;
8d419f
+
8d419f
+                free_and_replace(s, n);
8d419f
+        }
8d419f
 
8d419f
         /* Refuse if this for some other reason didn't result in a valid name */
8d419f
         if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
8d419f
@@ -581,6 +660,9 @@ int unit_name_to_path(const char *name, char **ret) {
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
+        if (unit_name_is_hashed(name))
8d419f
+                return -ENAMETOOLONG;
8d419f
+
8d419f
         return unit_name_path_unescape(prefix, ret);
8d419f
 }
8d419f
 
8d419f
diff --git a/src/basic/unit-name.h b/src/basic/unit-name.h
8d419f
index b62b3e034e..eaa701e9f6 100644
8d419f
--- a/src/basic/unit-name.h
8d419f
+++ b/src/basic/unit-name.h
8d419f
@@ -44,6 +44,9 @@ int unit_name_replace_instance(const char *f, const char *i, char **ret);
8d419f
 
8d419f
 int unit_name_template(const char *f, char **ret);
8d419f
 
8d419f
+int unit_name_hash_long(const char *name, char **ret);
8d419f
+bool unit_name_is_hashed(const char *name);
8d419f
+
8d419f
 int unit_name_from_path(const char *path, const char *suffix, char **ret);
8d419f
 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret);
8d419f
 int unit_name_to_path(const char *name, char **ret);
8d419f
diff --git a/src/core/mount.c b/src/core/mount.c
8d419f
index 4d407ca4e5..d63884e47e 100644
8d419f
--- a/src/core/mount.c
8d419f
+++ b/src/core/mount.c
8d419f
@@ -622,6 +622,9 @@ static int mount_add_extras(Mount *m) {
8d419f
 
8d419f
         if (!m->where) {
8d419f
                 r = unit_name_to_path(u->id, &m->where);
8d419f
+                if (r == -ENAMETOOLONG)
8d419f
+                        log_unit_error_errno(u, r, "Failed to derive mount point path from unit name, because unit name is hashed. "
8d419f
+                                                   "Set \"Where=\" in the unit file explicitly.");
8d419f
                 if (r < 0)
8d419f
                         return r;
8d419f
         }
8d419f
diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
8d419f
index 8cd0e0b4a1..b6137333aa 100644
8d419f
--- a/src/test/test-unit-name.c
8d419f
+++ b/src/test/test-unit-name.c
8d419f
@@ -106,6 +106,7 @@ TEST(unit_name_replace_instance) {
8d419f
 
8d419f
 static void test_unit_name_from_path_one(const char *path, const char *suffix, const char *expected, int ret) {
8d419f
         _cleanup_free_ char *t = NULL;
8d419f
+        int r;
8d419f
 
8d419f
         assert_se(unit_name_from_path(path, suffix, &t) == ret);
8d419f
         puts(strna(t));
8d419f
@@ -113,12 +114,31 @@ static void test_unit_name_from_path_one(const char *path, const char *suffix, c
8d419f
 
8d419f
         if (t) {
8d419f
                 _cleanup_free_ char *k = NULL;
8d419f
-                assert_se(unit_name_to_path(t, &k) == 0);
8d419f
+
8d419f
+                /* We don't support converting hashed unit names back to paths */
8d419f
+                r = unit_name_to_path(t, &k);
8d419f
+                if (r == -ENAMETOOLONG)
8d419f
+                        return;
8d419f
+                assert(r == 0);
8d419f
+
8d419f
                 puts(strna(k));
8d419f
                 assert_se(path_equal(k, empty_to_root(path)));
8d419f
         }
8d419f
 }
8d419f
 
8d419f
+TEST(unit_name_is_hashed) {
8d419f
+        assert_se(!unit_name_is_hashed(""));
8d419f
+        assert_se(!unit_name_is_hashed("foo@bar.service"));
8d419f
+        assert_se(!unit_name_is_hashed("foo@.service"));
8d419f
+        assert_se(unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736d9ed33c2ec55.mount"));
8d419f
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736D9ED33C2EC55.mount"));
8d419f
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!7736d9ed33c2ec55.mount"));
8d419f
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736d9gd33c2ec55.mount"));
8d419f
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_.mount"));
8d419f
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_2103e1466b87f7f7@waldo.mount"));
8d419f
+        assert_se(!unit_name_is_hashed("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_2103e1466b87f7f7@.mount"));
8d419f
+}
8d419f
+
8d419f
 TEST(unit_name_from_path) {
8d419f
         test_unit_name_from_path_one("/waldo", ".mount", "waldo.mount", 0);
8d419f
         test_unit_name_from_path_one("/waldo/quuix", ".mount", "waldo-quuix.mount", 0);
8d419f
@@ -128,7 +148,8 @@ TEST(unit_name_from_path) {
8d419f
         test_unit_name_from_path_one("///", ".mount", "-.mount", 0);
8d419f
         test_unit_name_from_path_one("/foo/../bar", ".mount", NULL, -EINVAL);
8d419f
         test_unit_name_from_path_one("/foo/./bar", ".mount", "foo-bar.mount", 0);
8d419f
-        test_unit_name_from_path_one("/waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ".mount", NULL, -ENAMETOOLONG);
8d419f
+        test_unit_name_from_path_one("/waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", ".mount",
8d419f
+                                     "waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa_7736d9ed33c2ec55.mount", 0);
8d419f
 }
8d419f
 
8d419f
 static void test_unit_name_from_path_instance_one(const char *pattern, const char *path, const char *suffix, const char *expected, int ret) {
8d419f
@@ -156,7 +177,6 @@ TEST(unit_name_from_path_instance) {
8d419f
         test_unit_name_from_path_instance_one("waldo", "..", ".mount", NULL, -EINVAL);
8d419f
         test_unit_name_from_path_instance_one("waldo", "/foo", ".waldi", NULL, -EINVAL);
8d419f
         test_unit_name_from_path_instance_one("wa--ldo", "/--", ".mount", "wa--ldo@\\x2d\\x2d.mount", 0);
8d419f
-        test_unit_name_from_path_instance_one("waldoaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "/waldo", ".mount", NULL, -ENAMETOOLONG);
8d419f
 }
8d419f
 
8d419f
 static void test_unit_name_to_path_one(const char *unit, const char *path, int ret) {