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