HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/nspawn/nspawn-mount.c

59,976 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 3b57a75070b82f74118a39a8d5c451901f6a05a6

Lines 1001–1516 of 1,516

 
static int mount_inaccessible(const char *dest, CustomMount *m) {
        _cleanup_free_ char *where = NULL, *source = NULL;
        struct stat st;
        int r;
 
        assert(dest);
        assert(m);
 
        r = chase_and_stat(m->destination, dest, CHASE_PREFIX_ROOT, &where, &st);
        if (r < 0) {
                log_full_errno(m->graceful ? LOG_DEBUG : LOG_ERR, r, "Failed to resolve %s/%s: %m", dest, m->destination);
                return m->graceful ? 0 : r;
        }
 
        r = mode_to_inaccessible_node(NULL, st.st_mode, &source);
        if (r < 0)
                return m->graceful ? 0 : r;
 
        r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, source, where, NULL, MS_BIND, NULL);
        if (r < 0)
                return m->graceful ? 0 : r;
 
        r = mount_nofollow_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, NULL, where, NULL, MS_BIND|MS_RDONLY|MS_REMOUNT, NULL);
        if (r < 0) {
                (void) umount_verbose(m->graceful ? LOG_DEBUG : LOG_ERR, where, UMOUNT_NOFOLLOW);
                return m->graceful ? 0 : r;
        }
 
        return 0;
}
 
static int mount_arbitrary(const char *dest, CustomMount *m) {
        _cleanup_free_ char *where = NULL;
        int r;
 
        assert(dest);
        assert(m);
 
        r = chase(m->destination, dest, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &where, NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to resolve %s/%s: %m", dest, m->destination);
        if (r == 0) { /* Doesn't exist yet? */
                r = mkdir_p_label(where, 0755);
                if (r < 0)
                        return log_error_errno(r, "Creating mount point for mount %s failed: %m", where);
        }
 
        return mount_nofollow_verbose(LOG_ERR, m->source, where, m->type_argument, 0, m->options);
}
 
int mount_custom(
                const char *dest,
                CustomMount *mounts, size_t n,
                uid_t uid_shift,
                uid_t uid_range,
                const char *selinux_apifs_context,
                MountSettingsMask mount_settings) {
        int r;
 
        assert(dest);
 
        FOREACH_ARRAY(m, mounts, n) {
                if (FLAGS_SET(mount_settings, MOUNT_IN_USERNS) != m->in_userns)
                        continue;
 
                if (FLAGS_SET(mount_settings, MOUNT_ROOT_ONLY) && !path_equal(m->destination, "/"))
                        continue;
 
                if (FLAGS_SET(mount_settings, MOUNT_NON_ROOT_ONLY) && path_equal(m->destination, "/"))
                        continue;
 
                switch (m->type) {
 
                case CUSTOM_MOUNT_BIND:
                        r = mount_bind(dest, m, uid_shift, uid_range);
                        break;
 
                case CUSTOM_MOUNT_TMPFS:
                        r = mount_tmpfs(dest, m, uid_shift, selinux_apifs_context);
                        break;
 
                case CUSTOM_MOUNT_OVERLAY:
                        r = mount_overlay(dest, m);
                        break;
 
                case CUSTOM_MOUNT_INACCESSIBLE:
                        r = mount_inaccessible(dest, m);
                        break;
 
                case CUSTOM_MOUNT_ARBITRARY:
                        r = mount_arbitrary(dest, m);
                        break;
 
                default:
                        assert_not_reached();
                }
 
                if (r < 0)
                        return r;
        }
 
        return 0;
}
 
bool has_custom_root_mount(const CustomMount *mounts, size_t n) {
        FOREACH_ARRAY(m, mounts, n)
                if (path_equal(m->destination, "/"))
                        return true;
 
        return false;
}
 
static int setup_volatile_state(const char *directory) {
        int r;
 
        assert(directory);
 
        /* --volatile=state means we simply overmount /var with a tmpfs, and the rest read-only. */
 
        /* First, remount the root directory. */
        r = bind_remount_recursive(directory, MS_RDONLY, MS_RDONLY, NULL);
        if (r < 0)
                return log_error_errno(r, "Failed to remount %s read-only: %m", directory);
 
        return 0;
}
 
static int setup_volatile_state_after_remount_idmap(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
        _cleanup_free_ char *buf = NULL;
        int r;
 
        assert(directory);
 
        /* Then, after remount_idmap(), overmount /var/ with a tmpfs. */
 
        _cleanup_free_ char *p = path_join(directory, "/var");
        if (!p)
                return log_oom();
 
        r = mkdir(p, 0755);
        if (r < 0 && errno != EEXIST)
                return log_error_errno(errno, "Failed to create %s: %m", directory);
 
        const char *options = "mode=0755" TMPFS_LIMITS_VOLATILE_STATE;
        r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
        if (r < 0)
                return log_oom();
        if (r > 0)
                options = buf;
 
        return mount_nofollow_verbose(LOG_ERR, "tmpfs", p, "tmpfs", MS_STRICTATIME, options);
}
 
static int setup_volatile_yes(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
        bool tmpfs_mounted = false, bind_mounted = false;
        _cleanup_(rmdir_and_freep) char *template = NULL;
        _cleanup_free_ char *buf = NULL, *bindir = NULL, *f = NULL, *t = NULL;
        struct stat st;
        int r;
 
        assert(directory);
 
        /* --volatile=yes means we mount a tmpfs to the root dir, and the original /usr to use inside it, and
         * that read-only. Before we start setting this up let's validate if the image has the /usr merge
         * implemented, and let's output a friendly log message if it hasn't. */
 
        bindir = path_join(directory, "/bin");
        if (!bindir)
                return log_oom();
        if (lstat(bindir, &st) < 0) {
                if (errno != ENOENT)
                        return log_error_errno(errno, "Failed to stat /bin directory below image: %m");
 
                /* ENOENT is fine, just means the image is probably just a naked /usr and we can create the
                 * rest. */
        } else if (S_ISDIR(st.st_mode))
                return log_error_errno(SYNTHETIC_ERRNO(EISDIR),
                                       "Sorry, --volatile=yes mode is not supported with OS images that have not merged /bin/, /sbin/, /lib/, /lib64/ into /usr/. "
                                       "Please work with your distribution and help them adopt the merged /usr scheme.");
        else if (!S_ISLNK(st.st_mode))
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                       "Error starting image: if --volatile=yes is used /bin must be a symlink (for merged /usr support) or non-existent (in which case a symlink is created automatically).");
 
        r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template);
        if (r < 0)
                return log_error_errno(r, "Failed to create temporary directory: %m");
 
        const char *options = "mode=0755" TMPFS_LIMITS_ROOTFS;
        r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
        if (r < 0)
                goto fail;
        if (r > 0)
                options = buf;
 
        r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
        if (r < 0)
                goto fail;
 
        tmpfs_mounted = true;
 
        f = path_join(directory, "/usr");
        if (!f) {
                r = log_oom();
                goto fail;
        }
 
        t = path_join(template, "/usr");
        if (!t) {
                r = log_oom();
                goto fail;
        }
 
        r = mkdir(t, 0755);
        if (r < 0 && errno != EEXIST) {
                r = log_error_errno(errno, "Failed to create %s: %m", t);
                goto fail;
        }
 
        r = mount_nofollow_verbose(LOG_ERR, f, t, NULL, MS_BIND|MS_REC, NULL);
        if (r < 0)
                goto fail;
 
        bind_mounted = true;
 
        r = bind_remount_recursive(t, MS_RDONLY, MS_RDONLY, NULL);
        if (r < 0) {
                log_error_errno(r, "Failed to remount %s read-only: %m", t);
                goto fail;
        }
 
        r = mount_nofollow_verbose(LOG_ERR, template, directory, NULL, MS_MOVE, NULL);
        if (r < 0)
                goto fail;
 
        (void) rmdir(template);
 
        return 0;
 
fail:
        if (bind_mounted)
                (void) umount_verbose(LOG_ERR, t, UMOUNT_NOFOLLOW);
 
        if (tmpfs_mounted)
                (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
 
        return r;
}
 
static int setup_volatile_overlay(const char *directory, uid_t uid_shift, const char *selinux_apifs_context) {
        _cleanup_free_ char *buf = NULL, *escaped_directory = NULL, *escaped_upper = NULL, *escaped_work = NULL;
        _cleanup_(rmdir_and_freep) char *template = NULL;
        const char *upper, *work, *options;
        bool tmpfs_mounted = false;
        int r;
 
        assert(directory);
 
        /* --volatile=overlay means we mount an overlayfs to the root dir. */
 
        r = mkdtemp_malloc("/tmp/nspawn-volatile-XXXXXX", &template);
        if (r < 0)
                return log_error_errno(r, "Failed to create temporary directory: %m");
 
        options = "mode=0755" TMPFS_LIMITS_ROOTFS;
        r = tmpfs_patch_options(options, uid_shift == 0 ? UID_INVALID : uid_shift, selinux_apifs_context, &buf);
        if (r < 0)
                goto finish;
        if (r > 0)
                options = buf;
 
        r = mount_nofollow_verbose(LOG_ERR, "tmpfs", template, "tmpfs", MS_STRICTATIME, options);
        if (r < 0)
                goto finish;
 
        tmpfs_mounted = true;
 
        upper = strjoina(template, "/upper");
        work = strjoina(template, "/work");
 
        if (mkdir(upper, 0755) < 0) {
                r = log_error_errno(errno, "Failed to create %s: %m", upper);
                goto finish;
        }
        if (mkdir(work, 0755) < 0) {
                r = log_error_errno(errno, "Failed to create %s: %m", work);
                goto finish;
        }
 
        /* And now, let's overmount the root dir with an overlayfs that uses the root dir as lower dir. It's kinda nice
         * that the kernel allows us to do that without going through some mount point rearrangements. */
 
        escaped_directory = shell_escape(directory, ",:");
        escaped_upper = shell_escape(upper, ",:");
        escaped_work = shell_escape(work, ",:");
        if (!escaped_directory || !escaped_upper || !escaped_work) {
                r = -ENOMEM;
                goto finish;
        }
 
        options = strjoina("lowerdir=", escaped_directory, ",upperdir=", escaped_upper, ",workdir=", escaped_work);
        r = mount_nofollow_verbose(LOG_ERR, "overlay", directory, "overlay", 0, options);
 
finish:
        if (tmpfs_mounted)
                (void) umount_verbose(LOG_ERR, template, UMOUNT_NOFOLLOW);
 
        return r;
}
 
int setup_volatile_mode(
                const char *directory,
                VolatileMode mode,
                uid_t uid_shift,
                const char *selinux_apifs_context) {
 
        switch (mode) {
 
        case VOLATILE_YES:
                return setup_volatile_yes(directory, uid_shift, selinux_apifs_context);
 
        case VOLATILE_STATE:
                return setup_volatile_state(directory);
 
        case VOLATILE_OVERLAY:
                return setup_volatile_overlay(directory, uid_shift, selinux_apifs_context);
 
        default:
                return 0;
        }
}
 
int setup_volatile_mode_after_remount_idmap(
                const char *directory,
                VolatileMode mode,
                uid_t uid_shift,
                const char *selinux_apifs_context) {
 
        switch (mode) {
 
        case VOLATILE_STATE:
                return setup_volatile_state_after_remount_idmap(directory, uid_shift, selinux_apifs_context);
 
        default:
                return 0;
        }
}
 
/* Expects *pivot_root_new and *pivot_root_old to be initialised to allocated memory or NULL. */
int pivot_root_parse(char **pivot_root_new, char **pivot_root_old, const char *s) {
        _cleanup_free_ char *root_new = NULL, *root_old = NULL;
        const char *p = s;
        int r;
 
        assert(pivot_root_new);
        assert(pivot_root_old);
 
        r = extract_first_word(&p, &root_new, ":", EXTRACT_DONT_COALESCE_SEPARATORS);
        if (r < 0)
                return r;
        if (r == 0)
                return -EINVAL;
 
        if (isempty(p))
                root_old = NULL;
        else {
                root_old = strdup(p);
                if (!root_old)
                        return -ENOMEM;
        }
 
        if (!path_is_absolute(root_new))
                return -EINVAL;
        if (!path_is_normalized(root_new))
                return -EINVAL;
        if (root_old && (!path_is_absolute(root_old) || !path_is_normalized(root_old)))
                return -EINVAL;
 
        free_and_replace(*pivot_root_new, root_new);
        free_and_replace(*pivot_root_old, root_old);
 
        return 0;
}
 
int setup_pivot_root(const char *directory, const char *pivot_root_new, const char *pivot_root_old) {
        _cleanup_free_ char *directory_pivot_root_new = NULL;
        _cleanup_free_ char *pivot_tmp_pivot_root_old = NULL;
        _cleanup_(rmdir_and_freep) char *pivot_tmp = NULL;
        int r;
 
        assert(directory);
 
        if (!pivot_root_new)
                return 0;
 
        /* Pivot pivot_root_new to / and the existing / to pivot_root_old.
         * If pivot_root_old is NULL, the existing / disappears.
         * This requires a temporary directory, pivot_tmp, which is
         * not a child of either.
         *
         * This is typically used for OSTree-style containers, where the root partition contains several
         * sysroots which could be run. Normally, one would be chosen by the bootloader and pivoted to / by
         * initrd.
         *
         * For example, for an OSTree deployment, pivot_root_new
         * would be: /ostree/deploy/$os/deploy/$checksum. Note that this
         * code doesn’t do the /var mount which OSTree expects: use
         * --bind +/sysroot/ostree/deploy/$os/var:/var for that.
         *
         * So in the OSTree case, we’ll end up with something like:
         *  - directory = /tmp/nspawn-root-123456
         *  - pivot_root_new = /ostree/deploy/os/deploy/123abc
         *  - pivot_root_old = /sysroot
         *  - directory_pivot_root_new =
         *       /tmp/nspawn-root-123456/ostree/deploy/os/deploy/123abc
         *  - pivot_tmp = /tmp/nspawn-pivot-123456
         *  - pivot_tmp_pivot_root_old = /tmp/nspawn-pivot-123456/sysroot
         *
         * Requires all file systems at directory and below to be mounted
         * MS_PRIVATE or MS_SLAVE so they can be moved.
         */
        directory_pivot_root_new = path_join(directory, pivot_root_new);
        if (!directory_pivot_root_new)
                return log_oom();
 
        /* Remount directory_pivot_root_new to make it movable. */
        r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
        if (r < 0)
                return r;
 
        if (pivot_root_old) {
                r = mkdtemp_malloc("/tmp/nspawn-pivot-XXXXXX", &pivot_tmp);
                if (r < 0)
                        return log_error_errno(r, "Failed to create temporary directory: %m");
 
                pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old);
                if (!pivot_tmp_pivot_root_old)
                        return log_oom();
 
                r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
                if (r < 0)
                        return r;
 
                r = mount_nofollow_verbose(LOG_ERR, directory, pivot_tmp_pivot_root_old, NULL, MS_MOVE, NULL);
                if (r < 0)
                        return r;
 
                r = mount_nofollow_verbose(LOG_ERR, pivot_tmp, directory, NULL, MS_MOVE, NULL);
        } else
                r = mount_nofollow_verbose(LOG_ERR, directory_pivot_root_new, directory, NULL, MS_MOVE, NULL);
 
        if (r < 0)
                return r;
 
        return 0;
}
 
#define NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS "/run/host/proc"
#define NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS "/run/host/sys"
 
int pin_fully_visible_api_fs(void) {
        int r;
 
        log_debug("Pinning fully visible API FS");
 
        (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, 0755);
        (void) mkdir_p(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, 0755);
 
        r = mount_follow_verbose(LOG_ERR, "proc", NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, "proc", PROC_DEFAULT_MOUNT_FLAGS, NULL);
        if (r < 0)
                return r;
 
        r = mount_follow_verbose(LOG_ERR, "sysfs", NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, "sysfs", SYS_DEFAULT_MOUNT_FLAGS, NULL);
        if (r < 0)
                return r;
 
        return 0;
}
 
static int do_wipe_fully_visible_api_fs(void) {
        if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS, MNT_DETACH) < 0)
                return log_error_errno(errno, "Failed to unmount temporary proc: %m");
 
        if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_PROCFS) < 0)
                return log_error_errno(errno, "Failed to remove temporary proc mountpoint: %m");
 
        if (umount2(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS, MNT_DETACH) < 0)
                return log_error_errno(errno, "Failed to unmount temporary sys: %m");
 
        if (rmdir(NSPAWN_PRIVATE_FULLY_VISIBLE_SYSFS) < 0)
                return log_error_errno(errno, "Failed to remove temporary sys mountpoint: %m");
 
        return 0;
}
 
int wipe_fully_visible_api_fs(int mntns_fd) {
        _cleanup_close_ int orig_mntns_fd = -EBADF;
        int r;
 
        log_debug("Wiping fully visible API FS");
 
        orig_mntns_fd = namespace_open_by_type(NAMESPACE_MOUNT);
        if (orig_mntns_fd < 0)
                return log_error_errno(orig_mntns_fd, "Failed to pin originating mount namespace: %m");
 
        if (setns(mntns_fd, CLONE_NEWNS) < 0)
                return log_error_errno(errno, "Failed to enter mount namespace: %m");
 
        r = do_wipe_fully_visible_api_fs();
 
        if (setns(orig_mntns_fd, CLONE_NEWNS) < 0)
                return log_error_errno(errno, "Failed to enter original mount namespace: %m");
 
        return r;
}