core,systemctl: refuse switching root to current root properly

Fixes #28970
This commit is contained in:
Mike Yuan
2023-09-02 15:56:24 +08:00
parent b63beb4d28
commit b0c5f0e1f4
2 changed files with 16 additions and 2 deletions
+8 -1
View 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";