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