2015-03-05 23:14:20 +05:30
|
|
|
/*-
|
|
|
|
* Copyright (c) 2014-2015 Juan Romero Pardines.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/fsuid.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <grp.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <syscall.h>
|
2015-05-06 15:10:19 +05:30
|
|
|
#include <assert.h>
|
2015-06-20 15:48:45 +05:30
|
|
|
#include <getopt.h>
|
2015-05-06 15:10:19 +05:30
|
|
|
|
|
|
|
#include <xbps.h>
|
|
|
|
#include "queue.h"
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-05 23:54:33 +05:30
|
|
|
#ifdef __clang__
|
|
|
|
#pragma clang diagnostic ignored "-Wformat-nonliteral"
|
|
|
|
#endif
|
|
|
|
|
2015-05-06 15:10:19 +05:30
|
|
|
struct bindmnt {
|
|
|
|
SIMPLEQ_ENTRY(bindmnt) entries;
|
|
|
|
char *src;
|
|
|
|
const char *dest;
|
|
|
|
};
|
|
|
|
|
2015-03-05 23:14:20 +05:30
|
|
|
static int errval = 0;
|
2015-05-06 15:10:19 +05:30
|
|
|
static SIMPLEQ_HEAD(bindmnt_head, bindmnt) bindmnt_queue =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(bindmnt_queue);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
|
|
|
static void __attribute__((noreturn))
|
|
|
|
die(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fprintf(stderr, "ERROR ");
|
|
|
|
vfprintf(stderr, fmt, ap);
|
|
|
|
fprintf(stderr, " (%s)\n", strerror(save_errno));
|
|
|
|
va_end(ap);
|
|
|
|
exit(errval != 0 ? errval : EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __attribute__((noreturn))
|
|
|
|
usage(const char *p)
|
|
|
|
{
|
2019-11-05 21:00:39 +05:30
|
|
|
printf("Usage: %s [-b src:dest] [--] <dir> <cmd> [<cmdargs>]\n\n"
|
2015-05-06 15:10:19 +05:30
|
|
|
"-b src:dest Bind mounts <src> into <dir>/<dest> (may be specified multiple times)\n\n", p);
|
2015-03-05 23:14:20 +05:30
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2015-05-06 15:10:19 +05:30
|
|
|
static void
|
|
|
|
add_bindmount(char *bm)
|
|
|
|
{
|
|
|
|
struct bindmnt *bmnt;
|
|
|
|
char *b, *src, *dest;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
src = strdup(bm);
|
|
|
|
assert(src);
|
|
|
|
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;
|
|
|
|
SIMPLEQ_INSERT_TAIL(&bindmnt_queue, bmnt, entries);
|
|
|
|
}
|
|
|
|
|
2015-03-05 23:14:20 +05:30
|
|
|
static void
|
|
|
|
bindmount(const char *chrootdir, const char *dir, const char *dest)
|
|
|
|
{
|
|
|
|
char mountdir[PATH_MAX-1];
|
|
|
|
|
|
|
|
snprintf(mountdir, sizeof(mountdir), "%s/%s", chrootdir, dest ? dest : dir);
|
2015-03-05 23:36:39 +05:30
|
|
|
if (chdir(dir) == -1)
|
|
|
|
die("chdir to %s", chrootdir);
|
2015-03-05 23:14:20 +05:30
|
|
|
if (mount(".", mountdir, NULL, MS_BIND|MS_REC|MS_PRIVATE, NULL) == -1)
|
|
|
|
die("Failed to bind mount %s at %s", dir, mountdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2015-05-06 15:10:19 +05:30
|
|
|
struct bindmnt *bmnt;
|
2015-03-05 23:14:20 +05:30
|
|
|
uid_t uid = getuid();
|
|
|
|
gid_t gid = getgid();
|
2015-05-06 15:10:19 +05:30
|
|
|
const char *chrootdir, *cmd, *argv0;
|
2015-03-14 01:44:28 +05:30
|
|
|
char **cmdargs, buf[32];
|
2015-05-06 15:10:19 +05:30
|
|
|
int c, fd;
|
2015-06-20 15:48:45 +05:30
|
|
|
const struct option longopts[] = {
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-05-06 15:10:19 +05:30
|
|
|
chrootdir = cmd = NULL;
|
2015-03-05 23:14:20 +05:30
|
|
|
argv0 = argv[0];
|
|
|
|
|
2018-04-16 17:30:30 +05:30
|
|
|
while ((c = getopt_long(argc, argv, "b:V", longopts, NULL)) != -1) {
|
2015-05-06 15:10:19 +05:30
|
|
|
switch (c) {
|
|
|
|
case 'b':
|
|
|
|
if (optarg == NULL || *optarg == '\0')
|
|
|
|
break;
|
|
|
|
add_bindmount(optarg);
|
2015-03-05 23:14:20 +05:30
|
|
|
break;
|
2015-05-06 15:10:19 +05:30
|
|
|
case 'V':
|
|
|
|
printf("%s\n", XBPS_RELVER);
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage(argv0);
|
2015-03-05 23:14:20 +05:30
|
|
|
}
|
|
|
|
}
|
2015-05-06 15:10:19 +05:30
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 2)
|
2015-03-05 23:14:20 +05:30
|
|
|
usage(argv0);
|
|
|
|
|
2018-04-30 21:52:28 +05:30
|
|
|
chrootdir = argv[0];
|
2015-05-06 15:10:19 +05:30
|
|
|
cmd = argv[1];
|
|
|
|
cmdargs = argv + 1;
|
2015-03-05 23:14:20 +05:30
|
|
|
|
|
|
|
/* Never allow chrootdir == / */
|
|
|
|
if (strcmp(chrootdir, "/") == 0)
|
|
|
|
die("/ is not allowed to be used as chrootdir");
|
|
|
|
|
2016-09-26 01:52:44 +05:30
|
|
|
/* Make chrootdir absolute */
|
|
|
|
if (chrootdir[0] != '/') {
|
|
|
|
char cwd[PATH_MAX-1];
|
|
|
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
|
|
|
die("getcwd");
|
|
|
|
chrootdir = xbps_xasprintf("%s/%s", cwd, chrootdir);
|
|
|
|
}
|
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
/*
|
|
|
|
* Unshare from the current process namespaces and set ours.
|
|
|
|
*/
|
|
|
|
if (unshare(CLONE_NEWUSER|CLONE_NEWNS|CLONE_NEWIPC|CLONE_NEWUTS) == -1) {
|
2015-03-05 23:14:20 +05:30
|
|
|
errval = 99;
|
2015-03-14 01:44:28 +05:30
|
|
|
die("unshare");
|
2015-03-05 23:14:20 +05:30
|
|
|
}
|
2015-03-14 01:44:28 +05:30
|
|
|
/*
|
|
|
|
* Setup uid/gid user mappings and restrict setgroups().
|
|
|
|
*/
|
|
|
|
if ((fd = open("/proc/self/uid_map", O_RDWR)) == -1)
|
2018-11-05 23:09:37 +05:30
|
|
|
die("failed to open /proc/self/uid_map rw");
|
2015-03-14 01:44:28 +05:30
|
|
|
if (write(fd, buf, snprintf(buf, sizeof buf, "%u %u 1\n", uid, uid)) == -1)
|
|
|
|
die("failed to write to /proc/self/uid_map");
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if ((fd = open("/proc/self/setgroups", O_RDWR)) != -1) {
|
|
|
|
if (write(fd, "deny", 4) == -1)
|
|
|
|
die("failed to write to /proc/self/setgroups");
|
2015-03-05 23:14:20 +05:30
|
|
|
close(fd);
|
2015-03-14 01:44:28 +05:30
|
|
|
}
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
if ((fd = open("/proc/self/gid_map", O_RDWR)) == -1)
|
|
|
|
die("failed to open /proc/self/gid_map rw");
|
|
|
|
if (write(fd, buf, snprintf(buf, sizeof buf, "%u %u 1\n", gid, gid)) == -1)
|
|
|
|
die("failed to write to /proc/self/gid_map");
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
close(fd);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
/* bind mount /proc */
|
|
|
|
bindmount(chrootdir, "/proc", NULL);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
/* bind mount /sys */
|
|
|
|
bindmount(chrootdir, "/sys", NULL);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
/* bind mount /dev */
|
|
|
|
bindmount(chrootdir, "/dev", NULL);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-05-06 15:10:19 +05:30
|
|
|
/* bind mount all user specified mnts */
|
|
|
|
SIMPLEQ_FOREACH(bmnt, &bindmnt_queue, entries)
|
|
|
|
bindmount(chrootdir, bmnt->src, bmnt->dest);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
/* move chrootdir to / and chroot to it */
|
|
|
|
if (chdir(chrootdir) == -1)
|
|
|
|
die("chdir to %s", chrootdir);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
if (mount(".", ".", NULL, MS_BIND|MS_PRIVATE, NULL) == -1)
|
|
|
|
die("Failed to bind mount %s", chrootdir);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
if (mount(chrootdir, "/", NULL, MS_MOVE, NULL) == -1)
|
|
|
|
die("Failed to move %s as rootfs", chrootdir);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
if (chroot(".") == -1)
|
|
|
|
die("Failed to chroot to %s", chrootdir);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
if (execvp(cmd, cmdargs) == -1)
|
|
|
|
die("Failed to execute command %s", cmd);
|
2015-03-05 23:14:20 +05:30
|
|
|
|
2015-03-14 01:44:28 +05:30
|
|
|
/* NOTREACHED */
|
|
|
|
exit(EXIT_FAILURE);
|
2015-03-05 23:14:20 +05:30
|
|
|
}
|