dryang / rpms / systemd

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