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