X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Fnamespace.c;h=9f15211cb62f0774ed78351afa87b086db804df7;hb=4e595329a93ed190795c2e24bf132d5028ec6a72;hp=c034bfd161f0a287deb027302fe523dbb0df564b;hpb=7f112f50fea585411ea2d493b3582bea77eb4d6e;p=elogind.git diff --git a/src/core/namespace.c b/src/core/namespace.c index c034bfd16..9f15211cb 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -69,7 +69,7 @@ static int append_mounts(BindMount **p, char **strv, MountMode mode) { (*p)->ignore = false; - if ((mode == INACCESSIBLE || mode == READONLY) && (*i)[0] == '-') { + if ((mode == INACCESSIBLE || mode == READONLY || mode == READWRITE) && (*i)[0] == '-') { (*p)->ignore = true; (*i)++; } @@ -142,9 +142,8 @@ static int mount_dev(BindMount *m) { "/dev/urandom\0" "/dev/tty\0"; - struct stat devnodes_stat[6] = {}; - const char *d; - unsigned n = 0; + char temporary_mount[] = "/tmp/namespace-dev-XXXXXX"; + const char *d, *dev = NULL, *devpts = NULL, *devshm = NULL, *devkdbus = NULL, *devhugepages = NULL, *devmqueue = NULL; _cleanup_umask_ mode_t u; int r; @@ -152,56 +151,115 @@ static int mount_dev(BindMount *m) { u = umask(0000); - /* First: record device mode_t and dev_t */ + if (!mkdtemp(temporary_mount)) + return -errno; + + dev = strappenda(temporary_mount, "/dev"); + mkdir(dev, 0755); + if (mount("tmpfs", dev, "tmpfs", MS_NOSUID|MS_STRICTATIME, "mode=755") < 0) { + r = -errno; + goto fail; + } + + devpts = strappenda(temporary_mount, "/dev/pts"); + mkdir(devpts, 0755); + if (mount("/dev/pts", devpts, NULL, MS_BIND, NULL) < 0) { + r = -errno; + goto fail; + } + + devshm = strappenda(temporary_mount, "/dev/shm"); + mkdir(devshm, 01777); + r = mount("/dev/shm", devshm, NULL, MS_BIND, NULL); + if (r < 0) { + r = -errno; + goto fail; + } + + devmqueue = strappenda(temporary_mount, "/dev/mqueue"); + mkdir(devmqueue, 0755); + mount("/dev/mqueue", devmqueue, NULL, MS_BIND, NULL); + + devkdbus = strappenda(temporary_mount, "/dev/kdbus"); + mkdir(devkdbus, 0755); + mount("/dev/kdbus", devkdbus, NULL, MS_BIND, NULL); + + devhugepages = strappenda(temporary_mount, "/dev/hugepages"); + mkdir(devhugepages, 0755); + mount("/dev/hugepages", devhugepages, NULL, MS_BIND, NULL); + NULSTR_FOREACH(d, devnodes) { - r = stat(d, &devnodes_stat[n]); + _cleanup_free_ char *dn = NULL; + struct stat st; + + r = stat(d, &st); if (r < 0) { - if (errno != ENOENT) - return -errno; - } else { - if (!S_ISBLK(devnodes_stat[n].st_mode) && - !S_ISCHR(devnodes_stat[n].st_mode)) - return -EINVAL; + + if (errno == ENOENT) + continue; + + r = -errno; + goto fail; + } + + if (!S_ISBLK(st.st_mode) && + !S_ISCHR(st.st_mode)) { + r = -EINVAL; + goto fail; + } + + if (st.st_rdev == 0) + continue; + + dn = strappend(temporary_mount, d); + if (!dn) { + r = -ENOMEM; + goto fail; } - n++; + r = mknod(dn, st.st_mode, st.st_rdev); + if (r < 0) { + r = -errno; + goto fail; + } } - assert(n == ELEMENTSOF(devnodes_stat)); + dev_setup(temporary_mount); - r = mount("tmpfs", "/dev", "tmpfs", MS_NOSUID|MS_STRICTATIME, "mode=755"); - if (r < 0) - return m->ignore ? 0 : -errno; + if (mount(dev, "/dev/", NULL, MS_MOVE, NULL) < 0) { + r = -errno; + goto fail; + } + rmdir(dev); + rmdir(temporary_mount); - mkdir_p("/dev/pts", 0755); + return 0; - r = mount("devpts", "/dev/pts", "devpts", MS_NOSUID|MS_NOEXEC, "newinstance,ptmxmode=0666,mode=620,gid=" STRINGIFY(TTY_GID)); - if (r < 0) - return m->ignore ? 0 : -errno; +fail: + if (devpts) + umount(devpts); - mkdir_p("/dev/shm", 0755); + if (devshm) + umount(devshm); - r = mount("tmpfs", "/dev/shm", "tmpfs", MS_NOSUID|MS_NODEV|MS_STRICTATIME, "mode=1777"); - if (r < 0) - return m->ignore ? 0 : -errno; + if (devkdbus) + umount(devkdbus); - /* Second: actually create it */ - n = 0; - NULSTR_FOREACH(d, devnodes) { - if (devnodes_stat[n].st_rdev == 0) - continue; + if (devhugepages) + umount(devhugepages); - r = mknod(d, devnodes_stat[n].st_mode, devnodes_stat[n].st_rdev); - if (r < 0) - return m->ignore ? 0 : -errno; + if (devmqueue) + umount(devmqueue); - n++; + if (dev) { + umount(dev); + rmdir(dev); } - dev_setup(NULL); + rmdir(temporary_mount); - return 0; + return r; } static int apply_mount( @@ -329,24 +387,28 @@ int setup_namespace( drop_duplicates(mounts, &n); } - /* Remount / as SLAVE so that nothing now mounted in the namespace - shows up in the parent */ - if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) - return -errno; + if (n > 0) { + /* Remount / as SLAVE so that nothing now mounted in the namespace + shows up in the parent */ + if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) + return -errno; - for (m = mounts; m < mounts + n; ++m) { - r = apply_mount(m, tmp_dir, var_tmp_dir); - if (r < 0) - goto fail; - } + for (m = mounts; m < mounts + n; ++m) { + r = apply_mount(m, tmp_dir, var_tmp_dir); + if (r < 0) + goto fail; + } - for (m = mounts; m < mounts + n; ++m) { - r = make_read_only(m); - if (r < 0) - goto fail; + for (m = mounts; m < mounts + n; ++m) { + r = make_read_only(m); + if (r < 0) + goto fail; + } } - /* Remount / as the desired mode */ + /* Remount / as the desired mode. Not that this will not + * reestablish propagation from our side to the host, since + * what's disconnected is disconnected. */ if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) { r = -errno; goto fail; @@ -355,9 +417,11 @@ int setup_namespace( return 0; fail: - for (m = mounts; m < mounts + n; ++m) - if (m->done) - umount2(m->path, MNT_DETACH); + if (n > 0) { + for (m = mounts; m < mounts + n; ++m) + if (m->done) + umount2(m->path, MNT_DETACH); + } return r; }