]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core,systemctl: refuse switching root to current root properly
authorMike Yuan <me@yhndnzj.com>
Fri, 25 Aug 2023 16:18:25 +0000 (00:18 +0800)
committerMike Yuan <me@yhndnzj.com>
Sat, 2 Sep 2023 07:56:24 +0000 (15:56 +0800)
Fixes #28970

src/core/dbus-manager.c
src/systemctl/systemctl-switch-root.c

index 245c5f14f1fbd4d4bd4ebcb7604889a1ccc8486b..253c315277eafbe4679e41532432f78b5fef02d3 100644 (file)
@@ -1864,10 +1864,17 @@ static int method_switch_root(sd_bus_message *message, void *userdata, sd_bus_er
                 if (!path_is_valid(root))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                                  "New root directory must be a valid path.");
+
                 if (!path_is_absolute(root))
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                                  "New root path '%s' is not absolute.", root);
-                if (path_equal(root, "/"))
+
+                r = path_is_root(root);
+                if (r < 0)
+                        return sd_bus_error_set_errnof(error, r,
+                                                       "Failed to check if new root directory '%s' is the same as old root: %m",
+                                                       root);
+                if (r > 0)
                         return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
                                                  "New root directory cannot be the old root directory.");
         }
index b59ea701c91cfbe6a9451d7a4546de5bf15651f8..cc31cf0e196f5a2308cc7ab706e11a940b212803 100644 (file)
@@ -4,6 +4,7 @@
 #include "bus-error.h"
 #include "bus-locator.h"
 #include "chase.h"
+#include "fd-util.h"
 #include "parse-util.h"
 #include "path-util.h"
 #include "proc-cmdline.h"
@@ -47,11 +48,17 @@ int verb_switch_root(int argc, char *argv[], void *userdata) {
 
         if (argc >= 2) {
                 root = argv[1];
+
                 if (!path_is_valid(root))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid root path: %s", root);
+
                 if (!path_is_absolute(root))
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Root path is not absolute: %s", root);
-                if (path_equal(root, "/"))
+
+                r = path_is_root(root);
+                if (r < 0)
+                        return log_error_errno(r, "Failed to check if switch-root directory '%s' is current root: %m", root);
+                if (r > 0)
                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Cannot switch to current root directory: %s", root);
         } else
                 root = "/sysroot";