2017-12-28 06:49:35 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <glib/gstdio.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2019-05-10 13:59:16 +02:00
|
|
|
#include <sys/syscall.h>
|
2017-12-28 06:49:35 +01:00
|
|
|
|
2019-05-11 10:32:47 +02:00
|
|
|
#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err))
|
|
|
|
#define fail_if(expr) \
|
|
|
|
if (expr) \
|
|
|
|
fail(#expr, errno);
|
2019-05-26 17:19:06 +02:00
|
|
|
|
2019-05-12 08:35:12 +02:00
|
|
|
const gchar *bind_blacklist[] = {"bin", "etc", "host", "real-host", "usr", "lib", "lib64", "lib32", "sbin", NULL};
|
2017-12-28 06:49:35 +01:00
|
|
|
|
2019-05-10 13:59:16 +02:00
|
|
|
int pivot_root(const char *new_root, const char *put_old) {
|
|
|
|
return syscall(SYS_pivot_root, new_root, put_old);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mount_tmpfs(const gchar *target) {
|
|
|
|
fail_if(mount("none", target, "tmpfs", 0, NULL));
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:49:35 +01:00
|
|
|
void bind_mount(const gchar *source, const gchar *target) {
|
|
|
|
fail_if(g_mkdir(target, 0755));
|
2019-05-11 10:32:47 +02:00
|
|
|
fail_if(mount(source, target, NULL, MS_BIND | MS_REC, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
const gchar *create_tmpdir() {
|
|
|
|
gchar *prefix =
|
|
|
|
g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL);
|
|
|
|
fail_if(!g_mkdtemp_full(prefix, 0755));
|
|
|
|
return prefix;
|
2017-12-28 06:49:35 +01:00
|
|
|
}
|
|
|
|
|
2019-05-11 10:32:47 +02:00
|
|
|
void pivot_host(const gchar *guest) {
|
2017-12-28 06:49:35 +01:00
|
|
|
g_autofree gchar *point = g_build_filename(guest, "host", NULL);
|
2019-05-10 13:59:16 +02:00
|
|
|
fail_if(g_mkdir(point, 0755));
|
|
|
|
fail_if(pivot_root(guest, point));
|
2017-12-28 06:49:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void bind_mount_item(const gchar *host, const gchar *guest, const gchar *name) {
|
|
|
|
g_autofree gchar *source = g_build_filename(host, name, NULL);
|
|
|
|
g_autofree gchar *target = g_build_filename(guest, name, NULL);
|
|
|
|
|
|
|
|
if (G_LIKELY(g_file_test(source, G_FILE_TEST_IS_DIR)))
|
|
|
|
bind_mount(source, target);
|
|
|
|
}
|
|
|
|
|
|
|
|
void bind(const gchar *host, const gchar *guest) {
|
2019-05-10 13:59:16 +02:00
|
|
|
mount_tmpfs(guest);
|
2020-12-08 12:10:47 +01:00
|
|
|
|
2019-05-12 08:35:12 +02:00
|
|
|
pivot_host(guest);
|
|
|
|
|
|
|
|
g_autofree gchar *host_dir = g_build_filename("/host", host, NULL);
|
2019-05-10 13:59:16 +02:00
|
|
|
|
2017-12-28 06:49:35 +01:00
|
|
|
g_autoptr(GError) err = NULL;
|
2019-05-12 08:35:12 +02:00
|
|
|
g_autoptr(GDir) dir = g_dir_open(host_dir, 0, &err);
|
2017-12-28 06:49:35 +01:00
|
|
|
|
|
|
|
if (err != NULL)
|
|
|
|
fail("g_dir_open", errno);
|
|
|
|
|
|
|
|
const gchar *item;
|
|
|
|
|
2019-05-10 13:59:16 +02:00
|
|
|
while ((item = g_dir_read_name(dir)))
|
2017-12-28 06:49:35 +01:00
|
|
|
if (!g_strv_contains(bind_blacklist, item))
|
2019-05-12 08:35:12 +02:00
|
|
|
bind_mount_item(host_dir, "/", item);
|
2017-12-28 06:49:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void spit(const char *path, char *fmt, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
|
|
|
FILE *f = g_fopen(path, "w");
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
fail("g_fopen", errno);
|
|
|
|
|
|
|
|
g_vfprintf(f, fmt, args);
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(gint argc, gchar **argv) {
|
|
|
|
const gchar *self = *argv++;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
g_message("%s command [arguments...]", self);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2019-05-11 10:32:47 +02:00
|
|
|
g_autofree const gchar *prefix = create_tmpdir();
|
2017-12-28 06:49:35 +01:00
|
|
|
|
|
|
|
pid_t cpid = fork();
|
|
|
|
|
|
|
|
if (cpid < 0)
|
|
|
|
fail("fork", errno);
|
|
|
|
|
|
|
|
else if (cpid == 0) {
|
|
|
|
uid_t uid = getuid();
|
|
|
|
gid_t gid = getgid();
|
|
|
|
|
2020-12-08 12:10:47 +01:00
|
|
|
int namespaces = CLONE_NEWNS;
|
|
|
|
if (uid != 0) {
|
|
|
|
namespaces |= CLONE_NEWUSER;
|
|
|
|
}
|
|
|
|
if (unshare(namespaces) < 0) {
|
2017-12-28 06:49:35 +01:00
|
|
|
int unshare_errno = errno;
|
|
|
|
|
|
|
|
g_message("Requires Linux version >= 3.19 built with CONFIG_USER_NS");
|
|
|
|
if (g_file_test("/proc/sys/kernel/unprivileged_userns_clone",
|
|
|
|
G_FILE_TEST_EXISTS))
|
|
|
|
g_message("Run: sudo sysctl -w kernel.unprivileged_userns_clone=1");
|
|
|
|
|
|
|
|
fail("unshare", unshare_errno);
|
|
|
|
}
|
|
|
|
|
2021-06-05 16:45:10 +02:00
|
|
|
// hide all mounts we do from the parent
|
2021-09-02 03:37:54 +02:00
|
|
|
fail_if(mount(0, "/", 0, MS_SLAVE | MS_REC, 0));
|
2021-06-05 16:45:10 +02:00
|
|
|
|
2020-12-08 12:10:47 +01:00
|
|
|
if (uid != 0) {
|
|
|
|
spit("/proc/self/setgroups", "deny");
|
|
|
|
spit("/proc/self/uid_map", "%d %d 1", uid, uid);
|
|
|
|
spit("/proc/self/gid_map", "%d %d 1", gid, gid);
|
|
|
|
}
|
2017-12-28 06:49:35 +01:00
|
|
|
|
2019-05-12 08:35:12 +02:00
|
|
|
// If there is a /host directory, assume this is nested chrootenv and use it as host instead.
|
|
|
|
gboolean nested_host = g_file_test("/host", G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
|
|
|
|
g_autofree const gchar *host = nested_host ? "/host" : "/";
|
|
|
|
|
|
|
|
bind(host, prefix);
|
|
|
|
|
|
|
|
// Replace /host by an actual (inner) /host.
|
|
|
|
if (nested_host) {
|
|
|
|
fail_if(g_mkdir("/real-host", 0755));
|
|
|
|
fail_if(mount("/host/host", "/real-host", NULL, MS_BIND | MS_REC, NULL));
|
|
|
|
// For some reason umount("/host") returns EBUSY even immediately after
|
|
|
|
// pivot_root. We detach it at least to keep `/proc/mounts` from blowing
|
|
|
|
// up in nested cases.
|
|
|
|
fail_if(umount2("/host", MNT_DETACH));
|
|
|
|
fail_if(mount("/real-host", "/host", NULL, MS_MOVE, NULL));
|
|
|
|
fail_if(rmdir("/real-host"));
|
|
|
|
}
|
2017-12-28 06:49:35 +01:00
|
|
|
|
2018-04-15 14:43:42 +02:00
|
|
|
fail_if(chdir("/"));
|
2017-12-28 06:49:35 +01:00
|
|
|
fail_if(execvp(*argv, argv));
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
int status;
|
|
|
|
|
|
|
|
fail_if(waitpid(cpid, &status, 0) != cpid);
|
2019-05-11 10:32:47 +02:00
|
|
|
fail_if(rmdir(prefix));
|
2017-12-28 06:49:35 +01:00
|
|
|
|
|
|
|
if (WIFEXITED(status))
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
|
|
|
|
else if (WIFSIGNALED(status))
|
|
|
|
kill(getpid(), WTERMSIG(status));
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|