594167
From c892aaa100fa04f877c7bf66e3cce846a7aa834c Mon Sep 17 00:00:00 2001
594167
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
594167
Date: Thu, 10 Mar 2022 20:26:59 +0100
594167
Subject: [PATCH] shared/install: also check for self-aliases during
594167
 installation and ignore them
594167
594167
We had a check that was done in unit_file_resolve_symlink(). Let's move
594167
the check to unit_validate_alias_symlink_or_warn(), which makes it available
594167
to the code in install.c.
594167
594167
With this, unit_file_resolve_symlink() behaves almost the same. The warning
594167
about "suspicious symlink" is done a bit later. I think this should be OK.
594167
594167
(cherry picked from commit f663e6468ff6f667a67fa1a0f9ca5c4962d4c605)
594167
594167
Related: #2082131
594167
---
594167
 src/basic/unit-file.c         | 26 ++++++++++----------------
594167
 src/shared/install.c          |  9 +++++++++
594167
 test/test-systemctl-enable.sh |  6 ++----
594167
 3 files changed, 21 insertions(+), 20 deletions(-)
594167
594167
diff --git a/src/basic/unit-file.c b/src/basic/unit-file.c
594167
index 105dacc1b2..142ba006f8 100644
594167
--- a/src/basic/unit-file.c
594167
+++ b/src/basic/unit-file.c
594167
@@ -82,7 +82,8 @@ int unit_validate_alias_symlink_or_warn(int log_level, const char *filename, con
594167
          *
594167
          * -EINVAL is returned if the something is wrong with the source filename or the source unit type is
594167
          *         not allowed to symlink,
594167
-         * -EXDEV if the target filename is not a valid unit name or doesn't match the source.
594167
+         * -EXDEV if the target filename is not a valid unit name or doesn't match the source,
594167
+         * -ELOOP for an alias to self.
594167
          */
594167
 
594167
         src = basename(filename);
594167
@@ -111,6 +112,11 @@ int unit_validate_alias_symlink_or_warn(int log_level, const char *filename, con
594167
 
594167
         /* dst checks */
594167
 
594167
+        if (streq(src, dst))
594167
+                return log_debug_errno(SYNTHETIC_ERRNO(ELOOP),
594167
+                                       "%s: unit self-alias: %s → %s, ignoring.",
594167
+                                       filename, src, dst);
594167
+
594167
         dst_name_type = unit_name_to_instance(dst, &dst_instance);
594167
         if (dst_name_type < 0)
594167
                 return log_full_errno(log_level, dst_name_type == -EINVAL ? SYNTHETIC_ERRNO(EXDEV) : dst_name_type,
594167
@@ -348,24 +354,12 @@ int unit_file_resolve_symlink(
594167
                 if (r < 0)
594167
                         return r;
594167
 
594167
-                bool self_alias = streq(target_name, filename);
594167
-
594167
-                if (is_path(tail))
594167
-                        log_full(self_alias ? LOG_DEBUG : LOG_WARNING,
594167
-                                 "Suspicious symlink %s/%s→%s, treating as alias.",
594167
-                                 dir, filename, simplified);
594167
-
594167
                 r = unit_validate_alias_symlink_or_warn(LOG_NOTICE, filename, simplified);
594167
                 if (r < 0)
594167
                         return r;
594167
-
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, target_name);
594167
-
594167
-                log_debug("Unit file alias: %s/%s → %s", dir, filename, target_name);
594167
+                if (is_path(tail))
594167
+                        log_warning("Suspicious symlink %s/%s→%s, treating as alias.",
594167
+                                    dir, filename, simplified);
594167
 
594167
                 dst = resolve_destination_target ? TAKE_PTR(simplified) : TAKE_PTR(target_name);
594167
         }
594167
diff --git a/src/shared/install.c b/src/shared/install.c
594167
index f1a8b7eb9b..459e8a6951 100644
594167
--- a/src/shared/install.c
594167
+++ b/src/shared/install.c
594167
@@ -1702,6 +1702,11 @@ int unit_file_verify_alias(
594167
          * ret_dst is set in cases where "instance propagation" happens, i.e. when the instance part is
594167
          * inserted into dst. It is not normally set, even on success, so that the caller can easily
594167
          * distinguish the case where instance propagation occurred.
594167
+         *
594167
+         * Returns:
594167
+         * -EXDEV when the alias doesn't match the unit,
594167
+         * -EUCLEAN when the name is invalid,
594167
+         * -ELOOP when the alias it to the unit itself.
594167
          */
594167
 
594167
         const char *path_alias = strrchr(dst, '/');
594167
@@ -1767,6 +1772,8 @@ int unit_file_verify_alias(
594167
                 }
594167
 
594167
                 r = unit_validate_alias_symlink_or_warn(LOG_DEBUG, dst_updated ?: dst, info->name);
594167
+                if (r == -ELOOP)  /* -ELOOP means self-alias, which we (quietly) ignore */
594167
+                        return r;
594167
                 if (r < 0) {
594167
                         unit_file_changes_add(changes, n_changes,
594167
                                               r == -EINVAL ? -EXDEV : r,
594167
@@ -1807,6 +1814,8 @@ static int install_info_symlink_alias(
594167
                 }
594167
 
594167
                 q = unit_file_verify_alias(info, dst, &dst_updated, changes, n_changes);
594167
+                if (q == -ELOOP)
594167
+                        continue;
594167
                 if (q < 0) {
594167
                         r = r < 0 ? r : q;
594167
                         continue;
594167
diff --git a/test/test-systemctl-enable.sh b/test/test-systemctl-enable.sh
594167
index 32bc6e5ef7..4117436462 100644
594167
--- a/test/test-systemctl-enable.sh
594167
+++ b/test/test-systemctl-enable.sh
594167
@@ -328,8 +328,7 @@ test ! -h "$root/etc/systemd/system/services.target.wants/templ1@two.service"
594167
 test ! -e "$root/etc/systemd/system/link4.service"
594167
 cat >"$root/etc/systemd/system/link4.service" <
594167
 [Install]
594167
-# FIXME: self-alias should be ignored
594167
-# Alias=link4.service
594167
+Alias=link4.service
594167
 Alias=link4@.service
594167
 Alias=link4@inst.service
594167
 Alias=link4alias.service
594167
@@ -372,8 +371,7 @@ test ! -h "$root/etc/systemd/system/link4alias2.service"
594167
 test ! -e "$root/etc/systemd/system/link5.service"
594167
 cat >"$root/etc/systemd/system/link5.service" <
594167
 [Install]
594167
-# FIXME: self-alias should be ignored
594167
-# Alias=link5.service
594167
+Alias=link5.service
594167
 Alias=link5alias.service
594167
 Alias=link5alias2.service
594167
 EOF