594167
From 5ec751ab9a06dadc62b30dc07e9dd7a41f8da403 Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Fri, 4 Mar 2022 18:47:31 +0100
594167
Subject: [PATCH] shared/install: reuse the standard symlink verification
594167
 subroutine
594167
MIME-Version: 1.0
594167
Content-Type: text/plain; charset=UTF-8
594167
Content-Transfer-Encoding: 8bit
594167
594167
We save a few lines, but the important thing is that we don't have two
594167
different implementations with slightly different rules used for enablement
594167
and loading. Fixes #22000.
594167
594167
Tested with:
594167
- the report in #22000, it now says:
594167
$ SYSTEMD_LOG_LEVEL=debug systemctl --root=/ enable test.service
594167
Suspicious symlink /etc/systemd/system/test.service→/etc/systemd/system/myown.d/test.service, treating as alias.
594167
unit_file_resolve_symlink: self-alias: /etc/systemd/system/test.service → test.service, ignoring.
594167
running_in_chroot(): Permission denied
594167
Suspicious symlink /etc/systemd/system/test.service→/etc/systemd/system/myown.d/test.service, treating as alias.
594167
unit_file_resolve_symlink: self-alias: /etc/systemd/system/test.service → test.service, ignoring.
594167
Failed to enable unit, refusing to operate on linked unit file test.service
594167
594167
- a symlink to /dev/null:
594167
...
594167
unit_file_resolve_symlink: linked unit file: /etc/systemd/system/test3.service → /dev/null
594167
Failed to enable unit, unit /etc/systemd/system/test3.service is masked.
594167
594167
- the same from the host:
594167
...
594167
unit_file_resolve_symlink: linked unit file: /var/lib/machines/rawhide/etc/systemd/system/test3.service → /var/lib/machines/rawhide/dev/null
594167
Failed to enable unit, unit /var/lib/machines/rawhide/etc/systemd/system/test3.service is masked.
594167
594167
- through the manager:
594167
$ sudo systemctl enable test.service
594167
Failed to enable unit: Refusing to operate on alias name or linked unit file: test.service
594167
$ sudo systemctl enable test3.service
594167
Failed to enable unit: Unit file /etc/systemd/system/test3.service is masked.
594167
594167
As seen in the first example, the warning is repeated. This is because we call
594167
the lookup logic twice: first for sysv-compat, and then again for real. I think
594167
that since this is only for broken setups, and when sysv-compat is enabled, and
594167
in an infrequent manual operation, at debug level, this is OK.
594167
594167
(cherry picked from commit 047d37dc3d376d912275c14d217f7a0dda9a5f0e)
594167
594167
Related: #2082131
594167
---
594167
 src/basic/unit-file.c | 70 +++++++++++++++++++++++++++++++----------
594167
 src/basic/unit-file.h | 10 ++++++
594167
 src/shared/install.c  | 72 +++++++------------------------------------
594167
 3 files changed, 75 insertions(+), 77 deletions(-)
594167
594167
diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c
594167
index 25abce932a..f7a10b22c6 100644
594167
--- a/src/basic/unit-file.c
594167
+++ b/src/basic/unit-file.c
594167
@@ -260,27 +260,50 @@ static int directory_name_is_valid(const char *name) {
594167
         return false;
594167
 }
594167
 
594167
-static int unit_file_resolve_symlink(
594167
+int unit_file_resolve_symlink(
594167
                 const char *root_dir,
594167
                 char **search_path,
594167
                 const char *dir,
594167
                 int dirfd,
594167
                 const char *filename,
594167
+                bool resolve_destination_target,
594167
                 char **ret_destination) {
594167
 
594167
-        _cleanup_free_ char *target = NULL, *simplified = NULL, *dst = NULL;
594167
+        _cleanup_free_ char *target = NULL, *simplified = NULL, *dst = NULL, *_dir = NULL, *_filename = NULL;
594167
         int r;
594167
 
594167
-        assert(dir);
594167
-        assert(dirfd >= 0);
594167
+        /* This can be called with either dir+dirfd valid and filename just a name,
594167
+         * or !dir && dirfd==AT_FDCWD, and filename being a full path.
594167
+         *
594167
+         * If resolve_destination_target is true, an absolute path will be returned.
594167
+         * If not, an absolute path is returned for linked unit files, and a relative
594167
+         * path otherwise. */
594167
+
594167
         assert(filename);
594167
         assert(ret_destination);
594167
+        assert(dir || path_is_absolute(filename));
594167
+        assert(dirfd >= 0 || dirfd == AT_FDCWD);
594167
 
594167
         r = readlinkat_malloc(dirfd, filename, &target);
594167
         if (r < 0)
594167
                 return log_warning_errno(r, "Failed to read symlink %s%s%s: %m",
594167
                                          dir, dir ? "/" : "", filename);
594167
 
594167
+        if (!dir) {
594167
+                r = path_extract_directory(filename, &_dir);
594167
+                if (r < 0)
594167
+                        return r;
594167
+                dir = _dir;
594167
+
594167
+                r = path_extract_filename(filename, &_filename);
594167
+                if (r < 0)
594167
+                        return r;
594167
+                if (r == O_DIRECTORY)
594167
+                        return log_warning_errno(SYNTHETIC_ERRNO(EISDIR),
594167
+                                                 "Unexpected path to a directory \"%s\", refusing.", filename);
594167
+                filename = _filename;
594167
+        }
594167
+
594167
         bool is_abs = path_is_absolute(target);
594167
         if (root_dir || !is_abs) {
594167
                 char *target_abs = path_join(is_abs ? root_dir : dir, target);
594167
@@ -296,24 +319,36 @@ static int unit_file_resolve_symlink(
594167
                 return log_warning_errno(r, "Failed to resolve symlink %s/%s pointing to %s: %m",
594167
                                          dir, filename, target);
594167
 
594167
+        assert(path_is_absolute(simplified));
594167
+
594167
         /* Check if the symlink goes outside of our search path.
594167
-         * If yes, it's a linked unit file or mask, and we don't care about the target name.
594167
-         * Let's just store the link source directly.
594167
-         * If not, let's verify that it's a good symlink. */
594167
+         * If yes, it's a linked unit file or mask, and we don't care about the target name
594167
+         * when loading units, and we return the link *source* (resolve_destination_target == false);
594167
+         * When this is called for installation purposes, we want the final destination,
594167
+         * so we return the *target*.
594167
+         *
594167
+         * Otherwise, let's verify that it's a good alias.
594167
+         */
594167
         const char *tail = path_startswith_strv(simplified, search_path);
594167
         if (!tail) {
594167
                 log_debug("Linked unit file: %s/%s → %s", dir, filename, simplified);
594167
 
594167
-                dst = path_join(dir, filename);
594167
-                if (!dst)
594167
-                        return log_oom();
594167
+                if (resolve_destination_target)
594167
+                        dst = TAKE_PTR(simplified);
594167
+                else {
594167
+                        dst = path_join(dir, filename);
594167
+                        if (!dst)
594167
+                                return log_oom();
594167
+                }
594167
 
594167
         } else {
594167
-                r = path_extract_filename(simplified, &dst);
594167
+                _cleanup_free_ char *target_name = NULL;
594167
+
594167
+                r = path_extract_filename(simplified, &target_name);
594167
                 if (r < 0)
594167
                         return r;
594167
 
594167
-                bool self_alias = streq(dst, filename);
594167
+                bool self_alias = streq(target_name, filename);
594167
 
594167
                 if (is_path(tail))
594167
                         log_full(self_alias ? LOG_DEBUG : LOG_WARNING,
594167
@@ -324,13 +359,15 @@ static int unit_file_resolve_symlink(
594167
                 if (r < 0)
594167
                         return r;
594167
 
594167
-                if (self_alias)
594167
-                        /* A self-alias that has no effect */
594167
+                if (self_alias && !resolve_destination_target)
594167
+                        /* A self-alias that has no effect when loading, let's just ignore it. */
594167
                         return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
594167
                                                "Unit file self-alias: %s/%s → %s, ignoring.",
594167
-                                               dir, filename, dst);
594167
+                                               dir, filename, target_name);
594167
+
594167
+                log_debug("Unit file alias: %s/%s → %s", dir, filename, target_name);
594167
 
594167
-                log_debug("Unit file alias: %s/%s → %s", dir, filename, dst);
594167
+                dst = resolve_destination_target ? TAKE_PTR(simplified) : TAKE_PTR(target_name);
594167
         }
594167
 
594167
         *ret_destination = TAKE_PTR(dst);
594167
@@ -475,6 +512,7 @@ int unit_file_build_name_map(
594167
 
594167
                                 r = unit_file_resolve_symlink(lp->root_dir, lp->search_path,
594167
                                                               *dir, dirfd(d), de->d_name,
594167
+                                                              /* resolve_destination_target= */ false,
594167
                                                               &dst);
594167
                                 if (r == -ENOMEM)
594167
                                         return r;
594167
diff --git a/src/basic/unit-file.h b/src/basic/unit-file.h
594167
index cc731a9e06..e29e878cfd 100644
594167
--- a/src/basic/unit-file.h
594167
+++ b/src/basic/unit-file.h
594167
@@ -44,6 +44,16 @@ int unit_symlink_name_compatible(const char *symlink, const char *target, bool i
594167
 int unit_validate_alias_symlink_and_warn(const char *filename, const char *target);
594167
 
594167
 bool lookup_paths_timestamp_hash_same(const LookupPaths *lp, uint64_t timestamp_hash, uint64_t *ret_new);
594167
+
594167
+int unit_file_resolve_symlink(
594167
+                const char *root_dir,
594167
+                char **search_path,
594167
+                const char *dir,
594167
+                int dirfd,
594167
+                const char *filename,
594167
+                bool resolve_destination_target,
594167
+                char **ret_destination);
594167
+
594167
 int unit_file_build_name_map(
594167
                 const LookupPaths *lp,
594167
                 uint64_t *cache_timestamp_hash,
594167
diff --git a/src/shared/install.c b/src/shared/install.c
594167
index 79e5109ce1..e07ca31797 100644
594167
--- a/src/shared/install.c
594167
+++ b/src/shared/install.c
594167
@@ -1338,76 +1338,26 @@ static int unit_file_load_or_readlink(
594167
                 const char *path,
594167
                 const LookupPaths *lp,
594167
                 SearchFlags flags) {
594167
-
594167
-        _cleanup_free_ char *resolved = NULL;
594167
         int r;
594167
 
594167
         r = unit_file_load(c, info, path, lp->root_dir, flags);
594167
         if (r != -ELOOP || (flags & SEARCH_DROPIN))
594167
                 return r;
594167
 
594167
-        r = chase_symlinks(path, lp->root_dir, CHASE_WARN | CHASE_NONEXISTENT, &resolved, NULL);
594167
-        if (r >= 0 &&
594167
-            lp->root_dir &&
594167
-            path_equal_ptr(path_startswith(resolved, lp->root_dir), "dev/null"))
594167
-                /* When looking under root_dir, we can't expect /dev/ to be mounted,
594167
-                 * so let's see if the path is a (possibly dangling) symlink to /dev/null. */
594167
-                info->type = UNIT_FILE_TYPE_MASKED;
594167
-
594167
-        else if (r > 0 && null_or_empty_path(resolved) > 0)
594167
+        /* This is a symlink, let's read and verify it. */
594167
+        r = unit_file_resolve_symlink(lp->root_dir, lp->search_path,
594167
+                                      NULL, AT_FDCWD, path,
594167
+                                      true, &info->symlink_target);
594167
+        if (r < 0)
594167
+                return r;
594167
 
594167
+        r = null_or_empty_path_with_root(info->symlink_target, lp->root_dir);
594167
+        if (r < 0 && r != -ENOENT)
594167
+                return log_debug_errno(r, "Failed to stat %s: %m", info->symlink_target);
594167
+        if (r > 0)
594167
                 info->type = UNIT_FILE_TYPE_MASKED;
594167
-
594167
-        else {
594167
-                _cleanup_free_ char *target = NULL;
594167
-                const char *bn;
594167
-                UnitType a, b;
594167
-
594167
-                /* This is a symlink, let's read it. We read the link again, because last time
594167
-                 * we followed the link until resolution, and here we need to do one step. */
594167
-
594167
-                r = readlink_malloc(path, &target);
594167
-                if (r < 0)
594167
-                        return r;
594167
-
594167
-                bn = basename(target);
594167
-
594167
-                if (unit_name_is_valid(info->name, UNIT_NAME_PLAIN)) {
594167
-
594167
-                        if (!unit_name_is_valid(bn, UNIT_NAME_PLAIN))
594167
-                                return -EINVAL;
594167
-
594167
-                } else if (unit_name_is_valid(info->name, UNIT_NAME_INSTANCE)) {
594167
-
594167
-                        if (!unit_name_is_valid(bn, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
594167
-                                return -EINVAL;
594167
-
594167
-                } else if (unit_name_is_valid(info->name, UNIT_NAME_TEMPLATE)) {
594167
-
594167
-                        if (!unit_name_is_valid(bn, UNIT_NAME_TEMPLATE))
594167
-                                return -EINVAL;
594167
-                } else
594167
-                        return -EINVAL;
594167
-
594167
-                /* Enforce that the symlink destination does not
594167
-                 * change the unit file type. */
594167
-
594167
-                a = unit_name_to_type(info->name);
594167
-                b = unit_name_to_type(bn);
594167
-                if (a < 0 || b < 0 || a != b)
594167
-                        return -EINVAL;
594167
-
594167
-                if (path_is_absolute(target))
594167
-                        /* This is an absolute path, prefix the root so that we always deal with fully qualified paths */
594167
-                        info->symlink_target = path_join(lp->root_dir, target);
594167
-                else
594167
-                        /* This is a relative path, take it relative to the dir the symlink is located in. */
594167
-                        info->symlink_target = file_in_same_dir(path, target);
594167
-                if (!info->symlink_target)
594167
-                        return -ENOMEM;
594167
-
594167
+        else
594167
                 info->type = UNIT_FILE_TYPE_SYMLINK;
594167
-        }
594167
 
594167
         return 0;
594167
 }