teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone

Blame SOURCES/0744-core-disallow-using-.service-as-a-service-name.patch

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