2014-04-01 13:56:22 +05:30
|
|
|
/*-
|
2020-04-14 23:18:50 +05:30
|
|
|
* Copyright (c) 2014-2020 Juan Romero Pardines.
|
2014-04-01 13:56:22 +05:30
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is based on linux-user-chroot by Colin Walters, but has been adapted
|
|
|
|
* specifically for xbps-src use:
|
|
|
|
*
|
2015-04-21 00:14:42 +05:30
|
|
|
* - This uses IPC/PID/UTS namespaces, nothing more.
|
2016-07-18 00:11:12 +05:30
|
|
|
* - Disables namespace features if running inside containers.
|
2015-04-23 18:35:29 +05:30
|
|
|
* - Supports overlayfs on a temporary directory or a tmpfs mount.
|
2020-04-14 23:18:50 +05:30
|
|
|
* - Supports read-only bind mounts.
|
2014-04-01 13:56:22 +05:30
|
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
2015-04-23 18:35:29 +05:30
|
|
|
#define _XOPEN_SOURCE 700
|
2014-04-01 13:56:22 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/prctl.h>
|
|
|
|
#include <sys/fsuid.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
2015-04-23 18:35:29 +05:30
|
|
|
#include <ftw.h>
|
2015-06-11 14:51:35 +05:30
|
|
|
#include <signal.h>
|
2015-06-20 15:48:45 +05:30
|
|
|
#include <getopt.h>
|
2020-02-13 13:19:58 +05:30
|
|
|
#include <dirent.h>
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
#include <xbps.h>
|
2015-05-06 14:54:47 +05:30
|
|
|
#include "queue.h"
|
2015-03-26 07:45:17 +05:30
|
|
|
|
2014-04-01 13:56:22 +05:30
|
|
|
#ifndef SECBIT_NOROOT
|
|
|
|
#define SECBIT_NOROOT (1 << 0)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SECBIT_NOROOT_LOCKED
|
|
|
|
#define SECBIT_NOROOT_LOCKED (1 << 1)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef PR_SET_NO_NEW_PRIVS
|
|
|
|
#define PR_SET_NO_NEW_PRIVS 38
|
|
|
|
#endif
|
|
|
|
|
2014-05-15 20:35:11 +05:30
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
#endif
|
|
|
|
|
2015-05-06 14:54:47 +05:30
|
|
|
struct bindmnt {
|
|
|
|
SIMPLEQ_ENTRY(bindmnt) entries;
|
|
|
|
char *src;
|
|
|
|
const char *dest;
|
2020-04-14 23:18:50 +05:30
|
|
|
bool ro; /* readonly */
|
2015-05-06 14:54:47 +05:30
|
|
|
};
|
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
static char *tmpdir;
|
2015-04-23 18:35:29 +05:30
|
|
|
static bool overlayfs_on_tmpfs;
|
2015-05-06 14:54:47 +05:30
|
|
|
static SIMPLEQ_HEAD(bindmnt_head, bindmnt) bindmnt_queue =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(bindmnt_queue);
|
|
|
|
|
|
|
|
static void __attribute__((noreturn))
|
|
|
|
usage(const char *p)
|
|
|
|
{
|
2020-04-14 23:18:50 +05:30
|
|
|
printf("Usage: %s [-[B|b] src:dest] [-O -t -o <opts>] [--] <dir> <cmd> [<cmdargs>]\n\n"
|
|
|
|
"-B src:dest Bind mounts <src> into <dir>/<dest> (read-only)\n"
|
|
|
|
"-b src:dest Bind mounts <src> into <dir>/<dest> (read-write)\n"
|
2015-05-06 14:54:47 +05:30
|
|
|
"-O Creates a tempdir and mounts <dir> read-only via overlayfs\n"
|
2020-04-14 23:18:50 +05:30
|
|
|
"-t Creates a tempdir and mounts <dir> on tmpfs (for use with -O)\n"
|
2015-05-06 14:54:47 +05:30
|
|
|
"-o opts Options to be passed to the tmpfs mount (for use with -t)\n", p);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2015-03-26 07:45:17 +05:30
|
|
|
|
2014-05-15 20:35:11 +05:30
|
|
|
static void __attribute__((noreturn))
|
2014-04-01 13:56:22 +05:30
|
|
|
die(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2015-05-06 14:54:47 +05:30
|
|
|
fprintf(stderr, "ERROR: ");
|
2014-04-01 13:56:22 +05:30
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, " (%s)\n", strerror(save_errno));
|
|
|
|
va_end(ap);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-04-23 18:35:29 +05:30
|
|
|
static int
|
2020-02-13 13:19:58 +05:30
|
|
|
ftw_cb(const char *fpath, const struct stat *sb)
|
2015-04-23 18:35:29 +05:30
|
|
|
{
|
2020-02-09 01:13:23 +05:30
|
|
|
int sverrno = 0;
|
|
|
|
|
2020-02-13 13:19:58 +05:30
|
|
|
if (S_ISDIR(sb->st_mode)) {
|
2020-02-09 01:13:23 +05:30
|
|
|
if (rmdir(fpath) == -1)
|
|
|
|
sverrno = errno;
|
|
|
|
} else {
|
2020-02-13 13:19:58 +05:30
|
|
|
if (unlink(fpath) == -1)
|
|
|
|
sverrno = errno;
|
2020-02-09 01:13:23 +05:30
|
|
|
}
|
|
|
|
if (sverrno != 0) {
|
|
|
|
fprintf(stderr, "Failed to remove %s: %s\n", fpath, strerror(sverrno));
|
2015-04-23 18:35:29 +05:30
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-02-13 13:19:58 +05:30
|
|
|
static int
|
|
|
|
walk_dir(const char *path,
|
|
|
|
int (*fn)(const char *fpath, const struct stat *sb))
|
|
|
|
{
|
|
|
|
struct dirent **list;
|
|
|
|
struct stat sb;
|
|
|
|
const char *p;
|
|
|
|
char tmp_path[PATH_MAX] = {0};
|
2020-04-14 23:18:50 +05:30
|
|
|
int rv = 0, i;
|
2020-02-13 13:19:58 +05:30
|
|
|
|
|
|
|
i = scandir(path, &list, NULL, alphasort);
|
|
|
|
if (i == -1) {
|
|
|
|
rv = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
while (i--) {
|
|
|
|
p = list[i]->d_name;
|
|
|
|
if (strcmp(p, ".") == 0 || strcmp(p, "..") == 0)
|
2020-02-13 13:29:15 +05:30
|
|
|
continue;
|
|
|
|
if (strlen(path) + strlen(p) + 1 >= (PATH_MAX - 1)) {
|
|
|
|
errno = ENAMETOOLONG;
|
|
|
|
rv = -1;
|
2020-02-13 13:19:58 +05:30
|
|
|
break;
|
2020-02-13 13:29:15 +05:30
|
|
|
}
|
|
|
|
strncpy(tmp_path, path, PATH_MAX - 1);
|
|
|
|
strncat(tmp_path, "/", PATH_MAX - 1 - strlen(tmp_path));
|
|
|
|
strncat(tmp_path, p, PATH_MAX - 1 - strlen(tmp_path));
|
|
|
|
if (lstat(tmp_path, &sb) < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (S_ISDIR(sb.st_mode)) {
|
2020-02-13 13:19:58 +05:30
|
|
|
if (walk_dir(tmp_path, fn) < 0) {
|
|
|
|
rv = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = fn(tmp_path, &sb);
|
|
|
|
if (rv != 0) {
|
|
|
|
break;
|
|
|
|
}
|
2020-02-13 13:29:15 +05:30
|
|
|
}
|
2020-02-13 13:19:58 +05:30
|
|
|
out:
|
|
|
|
free(list);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
static void
|
|
|
|
cleanup_overlayfs(void)
|
|
|
|
{
|
2015-04-23 18:35:29 +05:30
|
|
|
if (tmpdir == NULL)
|
|
|
|
return;
|
|
|
|
|
2020-02-13 13:19:58 +05:30
|
|
|
if (overlayfs_on_tmpfs)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* recursively remove the temporary dir */
|
|
|
|
if (walk_dir(tmpdir, ftw_cb) != 0) {
|
|
|
|
fprintf(stderr, "Failed to remove directory tree %s: %s\n",
|
|
|
|
tmpdir, strerror(errno));
|
|
|
|
exit(EXIT_FAILURE);
|
2015-04-23 18:35:29 +05:30
|
|
|
}
|
2020-02-13 13:19:58 +05:30
|
|
|
out:
|
2015-04-23 18:35:29 +05:30
|
|
|
rmdir(tmpdir);
|
2015-03-26 07:45:17 +05:30
|
|
|
}
|
|
|
|
|
2015-06-22 11:32:57 +05:30
|
|
|
static void __attribute__((noreturn))
|
2015-06-11 14:51:35 +05:30
|
|
|
sighandler_cleanup(int signum)
|
|
|
|
{
|
|
|
|
switch (signum) {
|
|
|
|
case SIGINT:
|
|
|
|
case SIGTERM:
|
|
|
|
case SIGQUIT:
|
|
|
|
cleanup_overlayfs();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_exit(signum);
|
|
|
|
}
|
|
|
|
|
2015-05-06 14:54:47 +05:30
|
|
|
static void
|
2020-04-14 23:18:50 +05:30
|
|
|
add_bindmount(const char *bm, bool ro)
|
2014-04-01 13:56:22 +05:30
|
|
|
{
|
2015-05-06 14:54:47 +05:30
|
|
|
struct bindmnt *bmnt;
|
|
|
|
char *b, *src, *dest;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
src = strdup(bm);
|
2015-05-06 15:08:06 +05:30
|
|
|
assert(src);
|
2015-05-06 14:54:47 +05:30
|
|
|
dest = strchr(bm, ':');
|
|
|
|
if (dest == NULL || *dest == '\0') {
|
|
|
|
errno = EINVAL;
|
|
|
|
die("invalid argument for bindmount: %s", bm);
|
|
|
|
}
|
|
|
|
dest++;
|
|
|
|
b = strchr(bm, ':');
|
|
|
|
len = strlen(bm) - strlen(b);
|
|
|
|
src[len] = '\0';
|
|
|
|
|
|
|
|
bmnt = malloc(sizeof(struct bindmnt));
|
|
|
|
assert(bmnt);
|
|
|
|
|
|
|
|
bmnt->src = src;
|
|
|
|
bmnt->dest = dest;
|
2020-04-14 23:18:50 +05:30
|
|
|
bmnt->ro = ro;
|
2015-05-06 14:54:47 +05:30
|
|
|
SIMPLEQ_INSERT_TAIL(&bindmnt_queue, bmnt, entries);
|
2014-04-01 13:56:22 +05:30
|
|
|
}
|
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
static void
|
|
|
|
bindmount(const char *chrootdir, const char *dir, const char *dest)
|
2014-04-01 13:56:22 +05:30
|
|
|
{
|
2020-04-14 23:18:50 +05:30
|
|
|
char mountdir[PATH_MAX-1];
|
|
|
|
int flags = MS_BIND|MS_PRIVATE;
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
snprintf(mountdir, sizeof(mountdir), "%s%s", chrootdir, dest ? dest : dir);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
if (chdir(dir) == -1)
|
|
|
|
die("Couldn't chdir to %s", dir);
|
|
|
|
if (mount(".", mountdir, NULL, flags, NULL) == -1)
|
|
|
|
die("Failed to bind mount %s at %s", dir, mountdir);
|
2014-04-01 13:56:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-04-14 23:18:50 +05:30
|
|
|
remount_rdonly(const char *chrootdir, const char *dir, const char *dest, bool ro)
|
2014-04-01 13:56:22 +05:30
|
|
|
{
|
|
|
|
char mountdir[PATH_MAX-1];
|
2020-04-14 23:18:50 +05:30
|
|
|
int flags = MS_REMOUNT|MS_BIND|MS_RDONLY;
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
if (!ro)
|
|
|
|
return;
|
|
|
|
|
|
|
|
snprintf(mountdir, sizeof(mountdir), "%s%s", chrootdir, dest ? dest : dir);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
if (chdir(dir) == -1)
|
2014-04-01 13:56:22 +05:30
|
|
|
die("Couldn't chdir to %s", dir);
|
2020-04-14 23:18:50 +05:30
|
|
|
if (mount(".", mountdir, NULL, flags, NULL) == -1)
|
|
|
|
die("Failed to remount read-only %s at %s", dir, mountdir);
|
2014-04-01 13:56:22 +05:30
|
|
|
}
|
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
static char *
|
2015-04-23 18:35:29 +05:30
|
|
|
setup_overlayfs(const char *chrootdir, uid_t ruid, gid_t rgid, bool tmpfs, const char *tmpfs_opts)
|
2015-03-26 07:45:17 +05:30
|
|
|
{
|
|
|
|
char *upperdir, *workdir, *newchrootdir, *mopts;
|
2015-04-21 00:14:42 +05:30
|
|
|
const void *opts = NULL;
|
2015-04-23 18:35:29 +05:30
|
|
|
|
|
|
|
if (tmpfs) {
|
|
|
|
/*
|
|
|
|
* Create a temporary directory on tmpfs for overlayfs storage.
|
|
|
|
*/
|
|
|
|
opts = tmpfs_opts;
|
|
|
|
if (mount("tmpfs", tmpdir, "tmpfs", 0, opts) == -1)
|
|
|
|
die("failed to mount tmpfs on %s", tmpdir);
|
|
|
|
}
|
2015-03-26 07:45:17 +05:30
|
|
|
/*
|
|
|
|
* Create the upper/work dirs to setup overlayfs.
|
|
|
|
*/
|
|
|
|
upperdir = xbps_xasprintf("%s/upperdir", tmpdir);
|
|
|
|
if (mkdir(upperdir, 0755) == -1)
|
|
|
|
die("failed to create upperdir (%s)", upperdir);
|
|
|
|
|
|
|
|
workdir = xbps_xasprintf("%s/workdir", tmpdir);
|
|
|
|
if (mkdir(workdir, 0755) == -1)
|
|
|
|
die("failed to create workdir (%s)", workdir);
|
|
|
|
|
|
|
|
newchrootdir = xbps_xasprintf("%s/masterdir", tmpdir);
|
|
|
|
if (mkdir(newchrootdir, 0755) == -1)
|
|
|
|
die("failed to create newchrootdir (%s)", newchrootdir);
|
|
|
|
|
|
|
|
mopts = xbps_xasprintf("upperdir=%s,lowerdir=%s,workdir=%s",
|
|
|
|
upperdir, chrootdir, workdir);
|
|
|
|
|
|
|
|
opts = mopts;
|
|
|
|
if (mount(chrootdir, newchrootdir, "overlay", 0, opts) == -1)
|
|
|
|
die("failed to mount overlayfs on %s", newchrootdir);
|
|
|
|
|
2015-04-23 18:35:29 +05:30
|
|
|
if (chown(upperdir, ruid, rgid) == -1)
|
|
|
|
die("chown upperdir %s", upperdir);
|
|
|
|
if (chown(workdir, ruid, rgid) == -1)
|
|
|
|
die("chown workdir %s", workdir);
|
2015-03-26 07:45:17 +05:30
|
|
|
if (chown(newchrootdir, ruid, rgid) == -1)
|
|
|
|
die("chown newchrootdir %s", newchrootdir);
|
|
|
|
|
|
|
|
free(mopts);
|
|
|
|
free(upperdir);
|
|
|
|
free(workdir);
|
|
|
|
|
|
|
|
return newchrootdir;
|
|
|
|
}
|
|
|
|
|
2014-04-01 13:56:22 +05:30
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2015-06-11 14:51:35 +05:30
|
|
|
struct sigaction sa;
|
2014-04-01 13:56:22 +05:30
|
|
|
uid_t ruid, euid, suid;
|
|
|
|
gid_t rgid, egid, sgid;
|
2020-04-14 23:18:50 +05:30
|
|
|
const char *rootdir, *tmpfs_opts, *cmd, *argv0;
|
|
|
|
char **cmdargs, *b, *chrootdir, mountdir[PATH_MAX-1];
|
2016-07-18 00:11:12 +05:30
|
|
|
int c, clone_flags, container_flags, child_status = 0;
|
2014-04-01 13:56:22 +05:30
|
|
|
pid_t child;
|
2015-03-26 07:45:17 +05:30
|
|
|
bool overlayfs = false;
|
2015-06-20 15:48:45 +05:30
|
|
|
const struct option longopts[] = {
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
tmpfs_opts = rootdir = cmd = NULL;
|
2014-04-01 13:56:22 +05:30
|
|
|
argv0 = argv[0];
|
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
while ((c = getopt_long(argc, argv, "Oto:B:b:V", longopts, NULL)) != -1) {
|
2015-05-06 14:54:47 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'O':
|
2015-03-26 07:45:17 +05:30
|
|
|
overlayfs = true;
|
2015-05-06 14:54:47 +05:30
|
|
|
break;
|
|
|
|
case 't':
|
2015-04-23 18:35:29 +05:30
|
|
|
overlayfs_on_tmpfs = true;
|
2014-04-01 13:56:22 +05:30
|
|
|
break;
|
2015-05-06 14:54:47 +05:30
|
|
|
case 'o':
|
|
|
|
tmpfs_opts = optarg;
|
|
|
|
break;
|
2020-04-14 23:18:50 +05:30
|
|
|
case 'B':
|
|
|
|
if (optarg == NULL || *optarg == '\0')
|
|
|
|
break;
|
|
|
|
add_bindmount(optarg, true);
|
|
|
|
break;
|
2015-05-06 14:54:47 +05:30
|
|
|
case 'b':
|
|
|
|
if (optarg == NULL || *optarg == '\0')
|
|
|
|
break;
|
2020-04-14 23:18:50 +05:30
|
|
|
add_bindmount(optarg, false);
|
2015-05-06 14:54:47 +05:30
|
|
|
break;
|
|
|
|
case 'V':
|
|
|
|
printf("%s\n", XBPS_RELVER);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage(argv0);
|
2014-04-01 13:56:22 +05:30
|
|
|
}
|
|
|
|
}
|
2015-05-06 14:54:47 +05:30
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 2)
|
2014-04-01 13:56:22 +05:30
|
|
|
usage(argv0);
|
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
rootdir = argv[0];
|
2015-05-06 14:54:47 +05:30
|
|
|
cmd = argv[1];
|
|
|
|
cmdargs = argv + 1;
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
|
|
|
|
/* Make chrootdir absolute */
|
|
|
|
chrootdir = realpath(rootdir, NULL);
|
|
|
|
if (!chrootdir)
|
|
|
|
die("realpath rootdir");
|
|
|
|
|
2014-04-01 13:56:22 +05:30
|
|
|
/* Never allow chrootdir == / */
|
|
|
|
if (strcmp(chrootdir, "/") == 0)
|
|
|
|
die("/ is not allowed to be used as chrootdir");
|
|
|
|
|
|
|
|
if (getresgid(&rgid, &egid, &sgid) == -1)
|
|
|
|
die("getresgid");
|
|
|
|
|
|
|
|
if (getresuid(&ruid, &euid, &suid) == -1)
|
|
|
|
die("getresuid");
|
|
|
|
|
|
|
|
if (rgid == 0)
|
|
|
|
rgid = ruid;
|
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
if (overlayfs) {
|
|
|
|
b = xbps_xasprintf("%s.XXXXXXXXXX", chrootdir);
|
|
|
|
if ((tmpdir = mkdtemp(b)) == NULL)
|
|
|
|
die("failed to create tmpdir directory");
|
2015-04-23 18:35:29 +05:30
|
|
|
if (chown(tmpdir, ruid, rgid) == -1)
|
|
|
|
die("chown tmpdir %s", tmpdir);
|
2020-02-13 13:19:58 +05:30
|
|
|
/*
|
|
|
|
* Register a signal handler to clean up temporary masterdir.
|
|
|
|
*/
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sa_handler = sighandler_cleanup;
|
|
|
|
sigaction(SIGINT, &sa, NULL);
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
|
|
|
sigaction(SIGQUIT, &sa, NULL);
|
2015-03-26 07:45:17 +05:30
|
|
|
}
|
|
|
|
|
2014-04-01 13:56:22 +05:30
|
|
|
clone_flags = (SIGCHLD|CLONE_NEWNS|CLONE_NEWIPC|CLONE_NEWUTS|CLONE_NEWPID);
|
2016-07-18 00:11:12 +05:30
|
|
|
container_flags = clone_flags & ~(CLONE_NEWNS|CLONE_NEWIPC|CLONE_NEWUTS|CLONE_NEWPID);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
|
|
|
/* Issue the clone(2) syscall with our settings */
|
2016-09-20 04:56:33 +05:30
|
|
|
if ((child = syscall(__NR_clone, clone_flags, NULL)) == -1 &&
|
2016-07-18 00:11:12 +05:30
|
|
|
(child = syscall(__NR_clone, container_flags, NULL)) == -1)
|
2014-04-01 13:56:22 +05:30
|
|
|
die("clone");
|
|
|
|
|
|
|
|
if (child == 0) {
|
2015-05-06 14:54:47 +05:30
|
|
|
struct bindmnt *bmnt;
|
2014-04-01 13:56:22 +05:30
|
|
|
/*
|
|
|
|
* Restrict privileges on the child.
|
|
|
|
*/
|
|
|
|
if (prctl(PR_SET_NO_NEW_PRIVS, 1) == -1 && errno != EINVAL) {
|
|
|
|
die("prctl PR_SET_NO_NEW_PRIVS");
|
2020-04-14 23:18:50 +05:30
|
|
|
}
|
|
|
|
if (prctl(PR_SET_SECUREBITS,
|
2014-04-01 13:56:22 +05:30
|
|
|
SECBIT_NOROOT|SECBIT_NOROOT_LOCKED) == -1) {
|
2020-04-14 23:18:50 +05:30
|
|
|
die("prctl PR_SET_SECUREBITS");
|
2014-04-01 13:56:22 +05:30
|
|
|
}
|
2016-09-20 21:15:56 +05:30
|
|
|
|
|
|
|
/* mount as private, systemd mounts it as shared by default */
|
|
|
|
if (mount(NULL, "/", "none", MS_PRIVATE|MS_REC, NULL) == -1)
|
|
|
|
die("Failed to mount / private");
|
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
/* setup our overlayfs if set */
|
|
|
|
if (overlayfs)
|
2015-04-23 18:35:29 +05:30
|
|
|
chrootdir = setup_overlayfs(chrootdir, ruid, rgid,
|
|
|
|
overlayfs_on_tmpfs, tmpfs_opts);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
|
|
|
/* mount /proc */
|
|
|
|
snprintf(mountdir, sizeof(mountdir), "%s/proc", chrootdir);
|
2020-04-14 23:18:50 +05:30
|
|
|
if (mount("proc", mountdir, "proc",
|
|
|
|
MS_MGC_VAL|MS_PRIVATE|MS_RDONLY, NULL) == -1) {
|
2015-04-11 12:17:40 +05:30
|
|
|
/* try bind mount */
|
2020-04-14 23:18:50 +05:30
|
|
|
add_bindmount("/proc:/proc", true);
|
2015-04-11 12:17:40 +05:30
|
|
|
}
|
2020-04-14 23:18:50 +05:30
|
|
|
/* bind mount /sys, /dev (ro) and /dev/shm (rw) */
|
|
|
|
add_bindmount("/sys:/sys", true);
|
|
|
|
add_bindmount("/dev:/dev", true);
|
|
|
|
add_bindmount("/dev/shm:/dev/shm", false);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
/* bind mount all specified mnts */
|
|
|
|
SIMPLEQ_FOREACH(bmnt, &bindmnt_queue, entries)
|
|
|
|
bindmount(chrootdir, bmnt->src, bmnt->dest);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2020-04-14 23:18:50 +05:30
|
|
|
/* remount bind mounts as read-only if set */
|
2015-05-06 14:54:47 +05:30
|
|
|
SIMPLEQ_FOREACH(bmnt, &bindmnt_queue, entries)
|
2020-04-14 23:18:50 +05:30
|
|
|
remount_rdonly(chrootdir, bmnt->src, bmnt->dest, bmnt->ro);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
|
|
|
/* move chrootdir to / and chroot to it */
|
2020-04-14 23:18:50 +05:30
|
|
|
if (chdir(chrootdir) == -1)
|
2014-04-01 13:56:22 +05:30
|
|
|
die("Failed to chdir to %s", chrootdir);
|
|
|
|
|
|
|
|
if (mount(".", ".", NULL, MS_BIND|MS_PRIVATE, NULL) == -1)
|
|
|
|
die("Failed to bind mount %s", chrootdir);
|
|
|
|
|
2016-04-30 21:04:32 +05:30
|
|
|
mount(chrootdir, "/", NULL, MS_MOVE, NULL);
|
2014-04-01 13:56:22 +05:30
|
|
|
|
|
|
|
if (chroot(".") == -1)
|
|
|
|
die("Failed to chroot to %s", chrootdir);
|
|
|
|
|
|
|
|
/* Switch back to the gid/uid of invoking process */
|
|
|
|
if (setgid(rgid) == -1)
|
|
|
|
die("setgid child");
|
|
|
|
if (setuid(ruid) == -1)
|
|
|
|
die("setuid child");
|
|
|
|
|
|
|
|
if (execvp(cmd, cmdargs) == -1)
|
|
|
|
die("Failed to execute command %s", cmd);
|
|
|
|
}
|
|
|
|
/* Wait until the child terminates */
|
|
|
|
while (waitpid(child, &child_status, 0) < 0) {
|
|
|
|
if (errno != EINTR)
|
|
|
|
die("waitpid");
|
|
|
|
}
|
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
if (!WIFEXITED(child_status)) {
|
|
|
|
cleanup_overlayfs();
|
2014-04-01 13:56:22 +05:30
|
|
|
return -1;
|
2015-03-26 07:45:17 +05:30
|
|
|
}
|
2014-04-01 13:56:22 +05:30
|
|
|
|
2015-03-26 07:45:17 +05:30
|
|
|
cleanup_overlayfs();
|
2014-04-01 13:56:22 +05:30
|
|
|
return WEXITSTATUS(child_status);
|
|
|
|
}
|