core,systemctl: refuse switching root if we're not in initrd

This commit is contained in:
Mike Yuan
2023-09-02 15:56:24 +08:00
parent b0c5f0e1f4
commit 4da159bc53
4 changed files with 24 additions and 16 deletions
+16 -11
View File
@@ -26,6 +26,7 @@
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "initrd-util.h"
#include "install.h"
#include "log.h"
#include "manager-dump.h"
@@ -1880,17 +1881,21 @@ static int method_switch_root(sd_bus_message *message, void *userdata, sd_bus_er
}
/* Safety check */
if (isempty(init)) {
r = path_is_os_tree(root);
if (r < 0)
return sd_bus_error_set_errnof(error, r,
"Failed to determine whether root path '%s' contains an OS tree: %m",
root);
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Specified switch root path '%s' does not seem to be an OS tree. os-release file is missing.",
root);
} else {
if (!in_initrd())
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Not in initrd, refusing switch-root operation.");
r = path_is_os_tree(root);
if (r < 0)
return sd_bus_error_set_errnof(error, r,
"Failed to determine whether root path '%s' contains an OS tree: %m",
root);
if (r == 0)
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Specified switch root path '%s' does not seem to be an OS tree. os-release file is missing.",
root);
if (!isempty(init)) {
if (!path_is_valid(init))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"Path to init binary '%s' is not a valid path.", init);
+4
View File
@@ -5,6 +5,7 @@
#include "bus-locator.h"
#include "chase.h"
#include "fd-util.h"
#include "initrd-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "proc-cmdline.h"
@@ -63,6 +64,9 @@ int verb_switch_root(int argc, char *argv[], void *userdata) {
} else
root = "/sysroot";
if (!in_initrd())
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not in initrd, refusing switch-root operation.");
if (argc >= 3)
init = argv[2];
else {