]> git.proxmox.com Git - mirror_lxc.git/commitdiff
always check whether rootfs is shared
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Tue, 25 Feb 2014 05:08:26 +0000 (23:08 -0600)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 25 Feb 2014 16:16:08 +0000 (11:16 -0500)
(this expands on Dwight's recent patch, commit c597baa8f9)

After unshare(CLONE_NEWNS) and before doing any mounting, always
check whether rootfs is shared.  Otherwise template runs or clone
scripts can bleed mount activity to the host.

Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Dwight Engen <dwight.engen@oracle.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lxc/attach.c
src/lxc/bdev.c
src/lxc/conf.c
src/lxc/lxc_usernsexec.c
src/lxc/lxccontainer.c
src/lxc/utils.c
src/lxc/utils.h

index f12d216f54ed968d0b4fe4562c2c2f11287e93c2..325ce53a6b6a0b245465fff7e6da4343b72d29ba 100644 (file)
@@ -220,6 +220,13 @@ static int lxc_attach_remount_sys_proc(void)
                return -1;
        }
 
+       if (detect_shared_rootfs()) {
+               if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
+                       SYSERROR("Failed to make / rslave");
+                       ERROR("Continuing...");
+               }
+       }
+
        /* assume /proc is always mounted, so remount it */
        ret = umount2("/proc", MNT_DETACH);
        if (ret < 0) {
index 952cfebecbd6fb90cb76de2e98abd27a903cd494..18f17709d617768d516ab04cd792f3ece4d6cd5a 100644 (file)
@@ -302,6 +302,13 @@ static int detect_fs(struct bdev *bdev, char *type, int len)
        if (unshare(CLONE_NEWNS) < 0)
                exit(1);
 
+       if (detect_shared_rootfs()) {
+               if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
+                       SYSERROR("Failed to make / rslave");
+                       ERROR("Continuing...");
+               }
+       }
+
        ret = mount_unknown_fs(srcdev, bdev->dest, bdev->mntopts);
        if (ret < 0) {
                ERROR("failed mounting %s onto %s to detect fstype", srcdev, bdev->dest);
@@ -2418,7 +2425,7 @@ static int rsync_rootfs(struct rsync_data *data)
        }
        if (detect_shared_rootfs()) {
                if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
-                       SYSERROR("Failed to make / rslave to run rsync");
+                       SYSERROR("Failed to make / rslave");
                        ERROR("Continuing...");
                }
        }
index c9dd7ac5a28fcb8648677c6b7f4dc3bce854a2db..fc39897fe925f864f68ff63c8edcffef22465cbc 100644 (file)
@@ -1426,46 +1426,6 @@ static int setup_autodev(const char *root)
        return 0;
 }
 
-/*
- * Detect whether / is mounted MS_SHARED.  The only way I know of to
- * check that is through /proc/self/mountinfo.
- * I'm only checking for /.  If the container rootfs or mount location
- * is MS_SHARED, but not '/', then you're out of luck - figuring that
- * out would be too much work to be worth it.
- */
-#define LINELEN 4096
-int detect_shared_rootfs(void)
-{
-       char buf[LINELEN], *p;
-       FILE *f;
-       int i;
-       char *p2;
-
-       f = fopen("/proc/self/mountinfo", "r");
-       if (!f)
-               return 0;
-       while (fgets(buf, LINELEN, f)) {
-               for (p = buf, i=0; p && i < 4; i++)
-                       p = index(p+1, ' ');
-               if (!p)
-                       continue;
-               p2 = index(p+1, ' ');
-               if (!p2)
-                       continue;
-               *p2 = '\0';
-               if (strcmp(p+1, "/") == 0) {
-                       // this is '/'.  is it shared?
-                       p = index(p2+1, ' ');
-                       if (p && strstr(p, "shared:")) {
-                               fclose(f);
-                               return 1;
-                       }
-               }
-       }
-       fclose(f);
-       return 0;
-}
-
 /*
  * I'll forgive you for asking whether all of this is needed :)  The
  * answer is yes.
index e40fa53dffb004b658f2aff266ae41afd2651db2..f06cf5b2f451f95c6eda592c1bd7d71a158136de 100644 (file)
@@ -35,6 +35,7 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <sys/mount.h>
 #include <sys/wait.h>
 #include <sched.h>
 #include <pwd.h>
@@ -111,6 +112,12 @@ static int do_child(void *vargv)
                perror("unshare CLONE_NEWNS");
                return -1;
        }
+       if (detect_shared_rootfs()) {
+               if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
+                       printf("Failed to make / rslave");
+                       return -1;
+               }
+       }
        execvp(argv[0], argv);
        perror("execvpe");
        return -1;
index 0d89d1166b1a5f2509314149b8ae1ba84be0d426..04277912fd09cd1ae58a208668a2bde3705fad57 100644 (file)
@@ -2502,6 +2502,12 @@ static int clone_update_rootfs(struct clone_update_data *data)
                        bdev_put(bdev);
                        return -1;
                }
+               if (detect_shared_rootfs()) {
+                       if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL)) {
+                               SYSERROR("Failed to make / rslave");
+                               ERROR("Continuing...");
+                       }
+               }
                if (bdev->ops->mount(bdev) < 0) {
                        bdev_put(bdev);
                        return -1;
index 52e4c86f919ddfca7d532705fdeae761e4b03499..da7c3b4709dd63f7997f48344628dec4c5d91287 100644 (file)
@@ -1194,3 +1194,43 @@ uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
 
        return hval;
 }
+
+/*
+ * Detect whether / is mounted MS_SHARED.  The only way I know of to
+ * check that is through /proc/self/mountinfo.
+ * I'm only checking for /.  If the container rootfs or mount location
+ * is MS_SHARED, but not '/', then you're out of luck - figuring that
+ * out would be too much work to be worth it.
+ */
+#define LINELEN 4096
+int detect_shared_rootfs(void)
+{
+       char buf[LINELEN], *p;
+       FILE *f;
+       int i;
+       char *p2;
+
+       f = fopen("/proc/self/mountinfo", "r");
+       if (!f)
+               return 0;
+       while (fgets(buf, LINELEN, f)) {
+               for (p = buf, i=0; p && i < 4; i++)
+                       p = index(p+1, ' ');
+               if (!p)
+                       continue;
+               p2 = index(p+1, ' ');
+               if (!p2)
+                       continue;
+               *p2 = '\0';
+               if (strcmp(p+1, "/") == 0) {
+                       // this is '/'.  is it shared?
+                       p = index(p2+1, ' ');
+                       if (p && strstr(p, "shared:")) {
+                               fclose(f);
+                               return 1;
+                       }
+               }
+       }
+       fclose(f);
+       return 0;
+}
index f6f3373e4c17e47db5bcd6161a18c59c718f4eea..dcf0e344590c0a095ff0475e2eab9fc3b7779e86 100644 (file)
@@ -276,3 +276,5 @@ extern bool dir_exists(const char *path);
 #define FNV1A_64_INIT ((uint64_t)0xcbf29ce484222325ULL)
 uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
 #endif
+
+int detect_shared_rootfs(void);