dryang / rpms / systemd

Forked from rpms/systemd a year ago
Clone
62b301
From e3e479fc9c51e0b6f2c21087e2e78e7c6f55169c Mon Sep 17 00:00:00 2001
62b301
From: Lennart Poettering <lennart@poettering.net>
62b301
Date: Mon, 25 May 2020 00:34:58 +0200
62b301
Subject: [PATCH] unit-name: tighten checks for building valid unit names
62b301
62b301
Let's be more thorough that whenever we build a unit name based on
62b301
parameters, that the result is actually a valid user name. If it isn't
62b301
fail early.
62b301
62b301
This should allows us to catch various issues earlier, in particular
62b301
when we synthesize mount units from /proc/self/mountinfo: instead of
62b301
actually attempting to allocate a mount unit we will fail much earlier
62b301
when we build the name to synthesize the unit under. Failing early is a
62b301
good thing generally.
62b301
62b301
(cherry picked from commit ab19db01ae1826efb3cbdf6dcb6a14412f8844d4)
62b301
62b301
Related: #1940973
62b301
---
62b301
 src/basic/unit-name.c | 61 ++++++++++++++++++++++++++++++-------------
62b301
 1 file changed, 43 insertions(+), 18 deletions(-)
62b301
62b301
diff --git a/src/basic/unit-name.c b/src/basic/unit-name.c
62b301
index 614eb8649b..f9b3fafd4d 100644
62b301
--- a/src/basic/unit-name.c
62b301
+++ b/src/basic/unit-name.c
62b301
@@ -207,8 +207,9 @@ UnitType unit_name_to_type(const char *n) {
62b301
 }
62b301
 
62b301
 int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
62b301
-        char *e, *s;
62b301
+        _cleanup_free_ char *s = NULL;
62b301
         size_t a, b;
62b301
+        char *e;
62b301
 
62b301
         assert(n);
62b301
         assert(suffix);
62b301
@@ -230,8 +231,12 @@ int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
62b301
                 return -ENOMEM;
62b301
 
62b301
         strcpy(mempcpy(s, n, a), suffix);
62b301
-        *ret = s;
62b301
 
62b301
+        /* Make sure the name is still valid (i.e. didn't grow too large due to longer suffix) */
62b301
+        if (!unit_name_is_valid(s, UNIT_NAME_ANY))
62b301
+                return -EINVAL;
62b301
+
62b301
+        *ret = TAKE_PTR(s);
62b301
         return 0;
62b301
 }
62b301
 
62b301
@@ -253,8 +258,8 @@ int unit_name_build(const char *prefix, const char *instance, const char *suffix
62b301
 }
62b301
 
62b301
 int unit_name_build_from_type(const char *prefix, const char *instance, UnitType type, char **ret) {
62b301
+        _cleanup_free_ char *s = NULL;
62b301
         const char *ut;
62b301
-        char *s;
62b301
 
62b301
         assert(prefix);
62b301
         assert(type >= 0);
62b301
@@ -264,19 +269,23 @@ int unit_name_build_from_type(const char *prefix, const char *instance, UnitType
62b301
         if (!unit_prefix_is_valid(prefix))
62b301
                 return -EINVAL;
62b301
 
62b301
-        if (instance && !unit_instance_is_valid(instance))
62b301
-                return -EINVAL;
62b301
-
62b301
         ut = unit_type_to_string(type);
62b301
 
62b301
-        if (!instance)
62b301
-                s = strjoin(prefix, ".", ut);
62b301
-        else
62b301
+        if (instance) {
62b301
+                if (!unit_instance_is_valid(instance))
62b301
+                        return -EINVAL;
62b301
+
62b301
                 s = strjoin(prefix, "@", instance, ".", ut);
62b301
+        } else
62b301
+                s = strjoin(prefix, ".", ut);
62b301
         if (!s)
62b301
                 return -ENOMEM;
62b301
 
62b301
-        *ret = s;
62b301
+        /* Verify that this didn't grow too large (or otherwise is invalid) */
62b301
+        if (!unit_name_is_valid(s, instance ? UNIT_NAME_INSTANCE : UNIT_NAME_PLAIN))
62b301
+                return -EINVAL;
62b301
+
62b301
+        *ret = TAKE_PTR(s);
62b301
         return 0;
62b301
 }
62b301
 
62b301
@@ -445,8 +454,8 @@ int unit_name_path_unescape(const char *f, char **ret) {
62b301
 }
62b301
 
62b301
 int unit_name_replace_instance(const char *f, const char *i, char **ret) {
62b301
+        _cleanup_free_ char *s = NULL;
62b301
         const char *p, *e;
62b301
-        char *s;
62b301
         size_t a, b;
62b301
 
62b301
         assert(f);
62b301
@@ -470,7 +479,11 @@ int unit_name_replace_instance(const char *f, const char *i, char **ret) {
62b301
 
62b301
         strcpy(mempcpy(mempcpy(s, f, a + 1), i, b), e);
62b301
 
62b301
-        *ret = s;
62b301
+        /* Make sure the resulting name still is valid, i.e. didn't grow too large */
62b301
+        if (!unit_name_is_valid(s, UNIT_NAME_INSTANCE))
62b301
+                return -EINVAL;
62b301
+
62b301
+        *ret = TAKE_PTR(s);
62b301
         return 0;
62b301
 }
62b301
 
62b301
@@ -501,8 +514,7 @@ int unit_name_template(const char *f, char **ret) {
62b301
 }
62b301
 
62b301
 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
62b301
-        _cleanup_free_ char *p = NULL;
62b301
-        char *s = NULL;
62b301
+        _cleanup_free_ char *p = NULL, *s = NULL;
62b301
         int r;
62b301
 
62b301
         assert(path);
62b301
@@ -520,7 +532,11 @@ int unit_name_from_path(const char *path, const char *suffix, char **ret) {
62b301
         if (!s)
62b301
                 return -ENOMEM;
62b301
 
62b301
-        *ret = s;
62b301
+        /* Refuse this if this got too long or for some other reason didn't result in a valid name */
62b301
+        if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
62b301
+                return -EINVAL;
62b301
+
62b301
+        *ret = TAKE_PTR(s);
62b301
         return 0;
62b301
 }
62b301
 
62b301
@@ -548,6 +564,10 @@ int unit_name_from_path_instance(const char *prefix, const char *path, const cha
62b301
         if (!s)
62b301
                 return -ENOMEM;
62b301
 
62b301
+        /* Refuse this if this got too long or for some other reason didn't result in a valid name */
62b301
+        if (!unit_name_is_valid(s, UNIT_NAME_INSTANCE))
62b301
+                return -EINVAL;
62b301
+
62b301
         *ret = s;
62b301
         return 0;
62b301
 }
62b301
@@ -601,7 +621,7 @@ static bool do_escape_mangle(const char *f, bool allow_globs, char *t) {
62b301
  *  If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
62b301
  */
62b301
 int unit_name_mangle_with_suffix(const char *name, UnitNameMangle flags, const char *suffix, char **ret) {
62b301
-        char *s;
62b301
+        _cleanup_free_ char *s = NULL;
62b301
         int r;
62b301
         bool mangled;
62b301
 
62b301
@@ -656,7 +676,12 @@ int unit_name_mangle_with_suffix(const char *name, UnitNameMangle flags, const c
62b301
         if ((!(flags & UNIT_NAME_MANGLE_GLOB) || !string_is_glob(s)) && unit_name_to_type(s) < 0)
62b301
                 strcat(s, suffix);
62b301
 
62b301
-        *ret = s;
62b301
+        /* Make sure mangling didn't grow this too large (but don't do this check if globbing is allowed,
62b301
+         * since globs generally do not qualify as valid unit names) */
62b301
+        if (!FLAGS_SET(flags, UNIT_NAME_MANGLE_GLOB) && !unit_name_is_valid(s, UNIT_NAME_ANY))
62b301
+                return -EINVAL;
62b301
+
62b301
+        *ret = TAKE_PTR(s);
62b301
         return 1;
62b301
 
62b301
 good:
62b301
@@ -664,7 +689,7 @@ good:
62b301
         if (!s)
62b301
                 return -ENOMEM;
62b301
 
62b301
-        *ret = s;
62b301
+        *ret = TAKE_PTR(s);
62b301
         return 0;
62b301
 }
62b301