984f77
From a4e9cf5b5c5e4c4a6f05825cd9c159283a425ae2 Mon Sep 17 00:00:00 2001
984f77
From: Anita Zhang <the.anitazha@gmail.com>
984f77
Date: Fri, 4 Oct 2019 16:03:04 -0700
984f77
Subject: [PATCH] core: disallow using '-.service' as a service name
984f77
984f77
-.service.d will become a special top level drop in so don't let it be a
984f77
usable service name (otherwise the interaction gets complicated).
984f77
984f77
(cherry picked from commit e23d911664b4fd86eb2c24b64233cb9f23cffdd1)
984f77
984f77
Resolves: #2051520
984f77
---
984f77
 src/basic/special.h       |  4 ++++
984f77
 src/basic/unit-name.c     | 25 +++++++++++++++++++++++++
984f77
 src/basic/unit-name.h     |  2 ++
984f77
 src/core/service.c        |  5 +++++
984f77
 src/test/test-unit-name.c | 19 +++++++++++++++++++
984f77
 5 files changed, 55 insertions(+)
984f77
984f77
diff --git a/src/basic/special.h b/src/basic/special.h
984f77
index 379a3d7979..2915122929 100644
984f77
--- a/src/basic/special.h
984f77
+++ b/src/basic/special.h
984f77
@@ -103,3 +103,7 @@
984f77
 
984f77
 /* The root directory. */
984f77
 #define SPECIAL_ROOT_MOUNT "-.mount"
984f77
+
984f77
+/* Used to apply settings to all services through drop-ins.
984f77
+ * Should not exist as an actual service. */
984f77
+#define SPECIAL_ROOT_SERVICE "-.service"
984f77
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
984f77
index 614eb8649b..82a666a481 100644
984f77
--- a/src/basic/unit-name.c
984f77
+++ b/src/basic/unit-name.c
984f77
@@ -668,6 +668,31 @@ good:
984f77
         return 0;
984f77
 }
984f77
 
984f77
+bool service_unit_name_is_valid(const char *name) {
984f77
+        _cleanup_free_ char *prefix = NULL, *s = NULL;
984f77
+        const char *e, *service_name = name;
984f77
+
984f77
+        if (!unit_name_is_valid(name, UNIT_NAME_ANY))
984f77
+                return false;
984f77
+
984f77
+        e = endswith(name, ".service");
984f77
+        if (!e)
984f77
+                return false;
984f77
+
984f77
+        /* If it's a template or instance, get the prefix as a service name. */
984f77
+        if (unit_name_is_valid(name, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) {
984f77
+                assert_se(unit_name_to_prefix(name, &prefix) == 0);
984f77
+                assert_se(s = strjoin(prefix, ".service"));
984f77
+                service_name = s;
984f77
+        }
984f77
+
984f77
+        /* Reject reserved service name(s). */
984f77
+        if (streq(service_name, SPECIAL_ROOT_SERVICE))
984f77
+                return false;
984f77
+
984f77
+        return true;
984f77
+}
984f77
+
984f77
 int slice_build_parent_slice(const char *slice, char **ret) {
984f77
         char *s, *dash;
984f77
         int r;
984f77
diff --git a/src/basic/unit-name.h b/src/basic/unit-name.h
984f77
index 61abcd585b..21729cba83 100644
984f77
--- a/src/basic/unit-name.h
984f77
+++ b/src/basic/unit-name.h
984f77
@@ -60,6 +60,8 @@ static inline int unit_name_mangle(const char *name, UnitNameMangle flags, char
984f77
         return unit_name_mangle_with_suffix(name, flags, ".service", ret);
984f77
 }
984f77
 
984f77
+bool service_unit_name_is_valid(const char *name);
984f77
+
984f77
 int slice_build_parent_slice(const char *slice, char **ret);
984f77
 int slice_build_subslice(const char *slice, const char*name, char **subslice);
984f77
 bool slice_name_is_valid(const char *name);
984f77
diff --git a/src/core/service.c b/src/core/service.c
984f77
index e8ae1a5772..b7eb10c044 100644
984f77
--- a/src/core/service.c
984f77
+++ b/src/core/service.c
984f77
@@ -556,6 +556,11 @@ static int service_verify(Service *s) {
984f77
                 }
984f77
         }
984f77
 
984f77
+        if (!service_unit_name_is_valid(UNIT(s)->id)) {
984f77
+                log_unit_error(UNIT(s), "Service name is invalid or reserved. Refusing.");
984f77
+                return -ENOEXEC;
984f77
+        }
984f77
+
984f77
         if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]
984f77
             && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) {
984f77
                 /* FailureAction= only makes sense if one of the start or stop commands is specified.
984f77
diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c
984f77
index 2b00ef8cb7..b629df5aea 100644
984f77
--- a/src/test/test-unit-name.c
984f77
+++ b/src/test/test-unit-name.c
984f77
@@ -347,6 +347,24 @@ static void test_unit_name_build(void) {
984f77
         free(t);
984f77
 }
984f77
 
984f77
+static void test_service_unit_name_is_valid(void) {
984f77
+        assert_se(service_unit_name_is_valid("foo.service"));
984f77
+        assert_se(service_unit_name_is_valid("foo@bar.service"));
984f77
+        assert_se(service_unit_name_is_valid("foo@bar@bar.service"));
984f77
+        assert_se(service_unit_name_is_valid("--.service"));
984f77
+        assert_se(service_unit_name_is_valid(".-.service"));
984f77
+        assert_se(service_unit_name_is_valid("-foo-bar.service"));
984f77
+        assert_se(service_unit_name_is_valid("-foo-bar-.service"));
984f77
+        assert_se(service_unit_name_is_valid("foo-bar-.service"));
984f77
+
984f77
+        assert_se(!service_unit_name_is_valid("-.service"));
984f77
+        assert_se(!service_unit_name_is_valid(""));
984f77
+        assert_se(!service_unit_name_is_valid("foo.slice"));
984f77
+        assert_se(!service_unit_name_is_valid("@.service"));
984f77
+        assert_se(!service_unit_name_is_valid("@bar.service"));
984f77
+        assert_se(!service_unit_name_is_valid("-@.service"));
984f77
+}
984f77
+
984f77
 static void test_slice_name_is_valid(void) {
984f77
         assert_se( slice_name_is_valid(SPECIAL_ROOT_SLICE));
984f77
         assert_se( slice_name_is_valid("foo.slice"));
984f77
@@ -833,6 +851,7 @@ int main(int argc, char* argv[]) {
984f77
         test_unit_prefix_is_valid();
984f77
         test_unit_name_change_suffix();
984f77
         test_unit_name_build();
984f77
+        test_service_unit_name_is_valid();
984f77
         test_slice_name_is_valid();
984f77
         test_build_subslice();
984f77
         test_build_parent_slice();