|
|
923a60 |
From 2b4894764e9e92ae9004524ed466b4bdf94b2a34 Mon Sep 17 00:00:00 2001
|
|
|
923a60 |
From: Alban Crequy <alban@endocode.com>
|
|
|
923a60 |
Date: Mon, 18 May 2015 12:20:28 +0200
|
|
|
923a60 |
Subject: [PATCH] core: Private*/Protect* options with RootDirectory
|
|
|
923a60 |
|
|
|
923a60 |
When a service is chrooted with the option RootDirectory=/opt/..., then
|
|
|
923a60 |
the options PrivateDevices, PrivateTmp, ProtectHome, ProtectSystem must
|
|
|
923a60 |
mount the directories under $RootDirectory/{dev,tmp,home,usr,boot}.
|
|
|
923a60 |
|
|
|
923a60 |
The test-ns tool can test setup_namespace() with and without chroot:
|
|
|
923a60 |
$ sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns
|
|
|
923a60 |
$ sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns
|
|
|
923a60 |
|
|
|
923a60 |
Cherry-picked from: ee818b89f4890b3a00e93772249fce810f60811e
|
|
|
923a60 |
Resolves: #1421181
|
|
|
923a60 |
---
|
|
|
923a60 |
src/core/execute.c | 8 +++--
|
|
|
923a60 |
src/core/namespace.c | 80 ++++++++++++++++++++++++++++++++++++++------
|
|
|
923a60 |
src/core/namespace.h | 3 +-
|
|
|
923a60 |
src/test/test-ns.c | 24 +++++++++++--
|
|
|
923a60 |
4 files changed, 100 insertions(+), 15 deletions(-)
|
|
|
923a60 |
|
|
|
923a60 |
diff --git a/src/core/execute.c b/src/core/execute.c
|
|
|
923a60 |
index 59340ec051..863babd761 100644
|
|
|
923a60 |
--- a/src/core/execute.c
|
|
|
923a60 |
+++ b/src/core/execute.c
|
|
|
923a60 |
@@ -1305,6 +1305,7 @@ static int exec_child(
|
|
|
923a60 |
uid_t uid = UID_INVALID;
|
|
|
923a60 |
gid_t gid = GID_INVALID;
|
|
|
923a60 |
int i, r;
|
|
|
923a60 |
+ bool needs_mount_namespace;
|
|
|
923a60 |
|
|
|
923a60 |
assert(command);
|
|
|
923a60 |
assert(context);
|
|
|
923a60 |
@@ -1593,7 +1594,9 @@ static int exec_child(
|
|
|
923a60 |
}
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
- if (exec_needs_mount_namespace(context, params, runtime)) {
|
|
|
923a60 |
+ needs_mount_namespace = exec_needs_mount_namespace(context, params, runtime);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (needs_mount_namespace) {
|
|
|
923a60 |
char *tmp = NULL, *var = NULL;
|
|
|
923a60 |
|
|
|
923a60 |
/* The runtime struct only contains the parent
|
|
|
923a60 |
@@ -1610,6 +1613,7 @@ static int exec_child(
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
r = setup_namespace(
|
|
|
923a60 |
+ params->apply_chroot ? context->root_directory : NULL,
|
|
|
923a60 |
context->read_write_dirs,
|
|
|
923a60 |
context->read_only_dirs,
|
|
|
923a60 |
context->inaccessible_dirs,
|
|
|
923a60 |
@@ -1635,7 +1639,7 @@ static int exec_child(
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
if (params->apply_chroot) {
|
|
|
923a60 |
- if (context->root_directory)
|
|
|
923a60 |
+ if (!needs_mount_namespace && context->root_directory)
|
|
|
923a60 |
if (chroot(context->root_directory) < 0) {
|
|
|
923a60 |
*exit_status = EXIT_CHROOT;
|
|
|
923a60 |
return -errno;
|
|
|
923a60 |
diff --git a/src/core/namespace.c b/src/core/namespace.c
|
|
|
923a60 |
index 00495c1446..5747462736 100644
|
|
|
923a60 |
--- a/src/core/namespace.c
|
|
|
923a60 |
+++ b/src/core/namespace.c
|
|
|
923a60 |
@@ -44,6 +44,7 @@
|
|
|
923a60 |
#include "label.h"
|
|
|
923a60 |
#include "selinux-util.h"
|
|
|
923a60 |
#include "namespace.h"
|
|
|
923a60 |
+#include "mkdir.h"
|
|
|
923a60 |
|
|
|
923a60 |
typedef enum MountMode {
|
|
|
923a60 |
/* This is ordered by priority! */
|
|
|
923a60 |
@@ -132,6 +133,22 @@ static void drop_duplicates(BindMount *m, unsigned *n) {
|
|
|
923a60 |
*n = t - m;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
+static int mount_move_root(const char *path) {
|
|
|
923a60 |
+ if (chdir(path) < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (mount(path, "/", NULL, MS_MOVE, NULL) < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (chroot(".") < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (chdir("/") < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ return 0;
|
|
|
923a60 |
+}
|
|
|
923a60 |
+
|
|
|
923a60 |
static int mount_dev(BindMount *m) {
|
|
|
923a60 |
static const char devnodes[] =
|
|
|
923a60 |
"/dev/null\0"
|
|
|
923a60 |
@@ -231,7 +248,13 @@ static int mount_dev(BindMount *m) {
|
|
|
923a60 |
|
|
|
923a60 |
dev_setup(temporary_mount);
|
|
|
923a60 |
|
|
|
923a60 |
- if (mount(dev, "/dev/", NULL, MS_MOVE, NULL) < 0) {
|
|
|
923a60 |
+ /* Create the /dev directory if missing. It is more likely to be
|
|
|
923a60 |
+ * missing when the service is started with RootDirectory. This is
|
|
|
923a60 |
+ * consistent with mount units creating the mount points when missing.
|
|
|
923a60 |
+ */
|
|
|
923a60 |
+ (void) mkdir_p_label(m->path, 0755);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (mount(dev, m->path, NULL, MS_MOVE, NULL) < 0) {
|
|
|
923a60 |
r = -errno;
|
|
|
923a60 |
goto fail;
|
|
|
923a60 |
}
|
|
|
923a60 |
@@ -410,6 +433,7 @@ static int make_read_only(BindMount *m) {
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
int setup_namespace(
|
|
|
923a60 |
+ const char* root_directory,
|
|
|
923a60 |
char** read_write_dirs,
|
|
|
923a60 |
char** read_only_dirs,
|
|
|
923a60 |
char** inaccessible_dirs,
|
|
|
923a60 |
@@ -455,37 +479,56 @@ int setup_namespace(
|
|
|
923a60 |
return r;
|
|
|
923a60 |
|
|
|
923a60 |
if (tmp_dir) {
|
|
|
923a60 |
- m->path = "/tmp";
|
|
|
923a60 |
+ m->path = prefix_roota(root_directory, "/tmp");
|
|
|
923a60 |
m->mode = PRIVATE_TMP;
|
|
|
923a60 |
m++;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
if (var_tmp_dir) {
|
|
|
923a60 |
- m->path = "/var/tmp";
|
|
|
923a60 |
+ m->path = prefix_roota(root_directory, "/var/tmp");
|
|
|
923a60 |
m->mode = PRIVATE_VAR_TMP;
|
|
|
923a60 |
m++;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
if (private_dev) {
|
|
|
923a60 |
- m->path = "/dev";
|
|
|
923a60 |
+ m->path = prefix_roota(root_directory, "/dev");
|
|
|
923a60 |
m->mode = PRIVATE_DEV;
|
|
|
923a60 |
m++;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
if (bus_endpoint_path) {
|
|
|
923a60 |
- m->path = bus_endpoint_path;
|
|
|
923a60 |
+ m->path = prefix_roota(root_directory, bus_endpoint_path);
|
|
|
923a60 |
m->mode = PRIVATE_BUS_ENDPOINT;
|
|
|
923a60 |
m++;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
if (protect_home != PROTECT_HOME_NO) {
|
|
|
923a60 |
- r = append_mounts(&m, STRV_MAKE("-/home", "-/run/user", "-/root"), protect_home == PROTECT_HOME_READ_ONLY ? READONLY : INACCESSIBLE);
|
|
|
923a60 |
+ const char *home_dir, *run_user_dir, *root_dir;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ home_dir = prefix_roota(root_directory, "/home");
|
|
|
923a60 |
+ home_dir = strjoina("-", home_dir);
|
|
|
923a60 |
+ run_user_dir = prefix_roota(root_directory, "/run/user");
|
|
|
923a60 |
+ run_user_dir = strjoina("-", run_user_dir);
|
|
|
923a60 |
+ root_dir = prefix_roota(root_directory, "/root");
|
|
|
923a60 |
+ root_dir = strjoina("-", root_dir);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = append_mounts(&m, STRV_MAKE(home_dir, run_user_dir, root_dir),
|
|
|
923a60 |
+ protect_home == PROTECT_HOME_READ_ONLY ? READONLY : INACCESSIBLE);
|
|
|
923a60 |
if (r < 0)
|
|
|
923a60 |
return r;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
if (protect_system != PROTECT_SYSTEM_NO) {
|
|
|
923a60 |
- r = append_mounts(&m, protect_system == PROTECT_SYSTEM_FULL ? STRV_MAKE("/usr", "-/boot", "/etc") : STRV_MAKE("/usr", "-/boot"), READONLY);
|
|
|
923a60 |
+ const char *usr_dir, *boot_dir, *etc_dir;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ usr_dir = prefix_roota(root_directory, "/home");
|
|
|
923a60 |
+ boot_dir = prefix_roota(root_directory, "/boot");
|
|
|
923a60 |
+ boot_dir = strjoina("-", boot_dir);
|
|
|
923a60 |
+ etc_dir = prefix_roota(root_directory, "/etc");
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = append_mounts(&m, protect_system == PROTECT_SYSTEM_FULL
|
|
|
923a60 |
+ ? STRV_MAKE(usr_dir, boot_dir, etc_dir)
|
|
|
923a60 |
+ : STRV_MAKE(usr_dir, boot_dir), READONLY);
|
|
|
923a60 |
if (r < 0)
|
|
|
923a60 |
return r;
|
|
|
923a60 |
}
|
|
|
923a60 |
@@ -496,12 +539,20 @@ int setup_namespace(
|
|
|
923a60 |
drop_duplicates(mounts, &n);
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
- if (n > 0) {
|
|
|
923a60 |
+ if (n > 0 || root_directory) {
|
|
|
923a60 |
/* Remount / as SLAVE so that nothing now mounted in the namespace
|
|
|
923a60 |
shows up in the parent */
|
|
|
923a60 |
if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
|
|
|
923a60 |
return -errno;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (root_directory) {
|
|
|
923a60 |
+ /* Turn directory into bind mount */
|
|
|
923a60 |
+ if (mount(root_directory, root_directory, NULL, MS_BIND|MS_REC, NULL) < 0)
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
|
|
|
923a60 |
+ if (n > 0) {
|
|
|
923a60 |
for (m = mounts; m < mounts + n; ++m) {
|
|
|
923a60 |
r = apply_mount(m, tmp_dir, var_tmp_dir);
|
|
|
923a60 |
if (r < 0)
|
|
|
923a60 |
@@ -515,12 +566,21 @@ int setup_namespace(
|
|
|
923a60 |
}
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
+ if (root_directory) {
|
|
|
923a60 |
+ /* MS_MOVE does not work on MS_SHARED so the remount MS_SHARED will be done later */
|
|
|
923a60 |
+ r = mount_move_root(root_directory);
|
|
|
923a60 |
+
|
|
|
923a60 |
+ /* at this point, we cannot rollback */
|
|
|
923a60 |
+ if (r < 0)
|
|
|
923a60 |
+ return r;
|
|
|
923a60 |
+ }
|
|
|
923a60 |
+
|
|
|
923a60 |
/* Remount / as the desired mode. Not that this will not
|
|
|
923a60 |
* reestablish propagation from our side to the host, since
|
|
|
923a60 |
* what's disconnected is disconnected. */
|
|
|
923a60 |
if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) {
|
|
|
923a60 |
- r = -errno;
|
|
|
923a60 |
- goto fail;
|
|
|
923a60 |
+ /* at this point, we cannot rollback */
|
|
|
923a60 |
+ return -errno;
|
|
|
923a60 |
}
|
|
|
923a60 |
|
|
|
923a60 |
return 0;
|
|
|
923a60 |
diff --git a/src/core/namespace.h b/src/core/namespace.h
|
|
|
923a60 |
index 42b92e7803..00ab22bf2e 100644
|
|
|
923a60 |
--- a/src/core/namespace.h
|
|
|
923a60 |
+++ b/src/core/namespace.h
|
|
|
923a60 |
@@ -41,7 +41,8 @@ typedef enum ProtectSystem {
|
|
|
923a60 |
_PROTECT_SYSTEM_INVALID = -1
|
|
|
923a60 |
} ProtectSystem;
|
|
|
923a60 |
|
|
|
923a60 |
-int setup_namespace(char **read_write_dirs,
|
|
|
923a60 |
+int setup_namespace(const char *chroot,
|
|
|
923a60 |
+ char **read_write_dirs,
|
|
|
923a60 |
char **read_only_dirs,
|
|
|
923a60 |
char **inaccessible_dirs,
|
|
|
923a60 |
const char *tmp_dir,
|
|
|
923a60 |
diff --git a/src/test/test-ns.c b/src/test/test-ns.c
|
|
|
923a60 |
index 7cd7b77153..72a0004e3e 100644
|
|
|
923a60 |
--- a/src/test/test-ns.c
|
|
|
923a60 |
+++ b/src/test/test-ns.c
|
|
|
923a60 |
@@ -42,10 +42,12 @@ int main(int argc, char *argv[]) {
|
|
|
923a60 |
NULL
|
|
|
923a60 |
};
|
|
|
923a60 |
|
|
|
923a60 |
- const char * const inaccessible[] = {
|
|
|
923a60 |
+ const char *inaccessible[] = {
|
|
|
923a60 |
"/home/lennart/projects",
|
|
|
923a60 |
NULL
|
|
|
923a60 |
};
|
|
|
923a60 |
+ char *root_directory;
|
|
|
923a60 |
+ char *projects_directory;
|
|
|
923a60 |
|
|
|
923a60 |
int r;
|
|
|
923a60 |
char tmp_dir[] = "/tmp/systemd-private-XXXXXX",
|
|
|
923a60 |
@@ -54,7 +56,20 @@ int main(int argc, char *argv[]) {
|
|
|
923a60 |
assert_se(mkdtemp(tmp_dir));
|
|
|
923a60 |
assert_se(mkdtemp(var_tmp_dir));
|
|
|
923a60 |
|
|
|
923a60 |
- r = setup_namespace((char **) writable,
|
|
|
923a60 |
+ root_directory = getenv("TEST_NS_CHROOT");
|
|
|
923a60 |
+ projects_directory = getenv("TEST_NS_PROJECTS");
|
|
|
923a60 |
+
|
|
|
923a60 |
+ if (projects_directory)
|
|
|
923a60 |
+ inaccessible[0] = projects_directory;
|
|
|
923a60 |
+
|
|
|
923a60 |
+ log_info("Inaccessible directory: '%s'", inaccessible[0]);
|
|
|
923a60 |
+ if (root_directory)
|
|
|
923a60 |
+ log_info("Chroot: '%s'", root_directory);
|
|
|
923a60 |
+ else
|
|
|
923a60 |
+ log_info("Not chrooted");
|
|
|
923a60 |
+
|
|
|
923a60 |
+ r = setup_namespace(root_directory,
|
|
|
923a60 |
+ (char **) writable,
|
|
|
923a60 |
(char **) readonly,
|
|
|
923a60 |
(char **) inaccessible,
|
|
|
923a60 |
tmp_dir,
|
|
|
923a60 |
@@ -66,6 +81,11 @@ int main(int argc, char *argv[]) {
|
|
|
923a60 |
0);
|
|
|
923a60 |
if (r < 0) {
|
|
|
923a60 |
log_error_errno(r, "Failed to setup namespace: %m");
|
|
|
923a60 |
+
|
|
|
923a60 |
+ log_info("Usage:\n"
|
|
|
923a60 |
+ " sudo TEST_NS_PROJECTS=/home/lennart/projects ./test-ns\n"
|
|
|
923a60 |
+ " sudo TEST_NS_CHROOT=/home/alban/debian-tree TEST_NS_PROJECTS=/home/alban/debian-tree/home/alban/Documents ./test-ns");
|
|
|
923a60 |
+
|
|
|
923a60 |
return 1;
|
|
|
923a60 |
}
|
|
|
923a60 |
|