@@ -7,32 +7,43 @@
77#include <sched.h>
88#include <unistd.h>
99
10+#include <sys/mount.h>
11+#include <sys/stat.h>
12+#include <sys/types.h>
13+#include <sys/wait.h>
14+#include <sys/syscall.h>
15+
1016#define fail(s, err) g_error("%s: %s: %s", __func__, s, g_strerror(err))
1117#define fail_if(expr) \
1218 if (expr) \
1319 fail(#expr, errno);
1420
15−#include <ftw.h>
21+const gchar *bind_blacklist[] = {"bin", "etc", "host", "real-host", "usr", "lib", "lib64", "lib32", "sbin", NULL};
1622
17−#include <sys/mount.h>
18−#include <sys/stat.h>
19−#include <sys/types.h>
20−#include <sys/wait.h>
21−
22−int min(int a, int b) {
23− return a > b ? b : a;
23+int pivot_root(const char *new_root, const char *put_old) {
24+ return syscall(SYS_pivot_root, new_root, put_old);
2425}
2526
26−const gchar *bind_blacklist[] = {"bin", "etc", "host", "usr", "lib", "lib64", "lib32", "sbin", NULL};
27+void mount_tmpfs(const gchar *target) {
28+ fail_if(mount("none", target, "tmpfs", 0, NULL));
29+}
2730
2831void bind_mount(const gchar *source, const gchar *target) {
2932 fail_if(g_mkdir(target, 0755));
30− fail_if(mount(source, target, "bind", MS_BIND | MS_REC, NULL));
33+ fail_if(mount(source, target, NULL, MS_BIND | MS_REC, NULL));
3134}
3235
33−void bind_mount_host(const gchar *host, const gchar *guest) {
36+const gchar *create_tmpdir() {
37+ gchar *prefix =
38+ g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL);
39+ fail_if(!g_mkdtemp_full(prefix, 0755));
40+ return prefix;
41+}
42+
43+void pivot_host(const gchar *guest) {
3444 g_autofree gchar *point = g_build_filename(guest, "host", NULL);
35− bind_mount(host, point);
45+ fail_if(g_mkdir(point, 0755));
46+ fail_if(pivot_root(guest, point));
3647}
3748
3849void bind_mount_item(const gchar *host, const gchar *guest, const gchar *name) {
@@ -44,19 +55,22 @@ void bind_mount_item(const gchar *host, const gchar *guest, const gchar *name) {
4455}
4556
4657void bind(const gchar *host, const gchar *guest) {
58+ mount_tmpfs(guest);
59+ pivot_host(guest);
60+
61+ g_autofree gchar *host_dir = g_build_filename("/host", host, NULL);
62+
4763 g_autoptr(GError) err = NULL;
48− g_autoptr(GDir) dir = g_dir_open(host, 0, &err);
64+ g_autoptr(GDir) dir = g_dir_open(host_dir, 0, &err);
4965
5066 if (err != NULL)
5167 fail("g_dir_open", errno);
5268
5369 const gchar *item;
5470
55− while (item = g_dir_read_name(dir))
71+ while ((item = g_dir_read_name(dir)))
5672 if (!g_strv_contains(bind_blacklist, item))
57− bind_mount_item(host, guest, item);
58−
59− bind_mount_host(host, guest);
73+ bind_mount_item(host_dir, "/", item);
6074}
6175
6276void spit(const char *path, char *fmt, ...) {
@@ -72,11 +86,6 @@ void spit(const char *path, char *fmt, ...) {
7286 fclose(f);
7387}
7488
75−int nftw_remove(const char *path, const struct stat *sb, int type,
76− struct FTW *ftw) {
77− return remove(path);
78−}
79−
8089int main(gint argc, gchar **argv) {
8190 const gchar *self = *argv++;
8291
@@ -85,15 +94,7 @@ int main(gint argc, gchar **argv) {
8594 return 1;
8695 }
8796
88− if (g_getenv("NIX_CHROOTENV"))
89− g_warning("chrootenv doesn't stack!");
90− else
91− g_setenv("NIX_CHROOTENV", "", TRUE);
92−
93− g_autofree gchar *prefix =
94− g_build_filename(g_get_tmp_dir(), "chrootenvXXXXXX", NULL);
95−
96− fail_if(!g_mkdtemp_full(prefix, 0755));
97+ g_autofree const gchar *prefix = create_tmpdir();
9798
9899 pid_t cpid = fork();
99100
@@ -119,9 +120,24 @@ int main(gint argc, gchar **argv) {
119120 spit("/proc/self/uid_map", "%d %d 1", uid, uid);
120121 spit("/proc/self/gid_map", "%d %d 1", gid, gid);
121122
122− bind("/", prefix);
123+ // If there is a /host directory, assume this is nested chrootenv and use it as host instead.
124+ gboolean nested_host = g_file_test("/host", G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR);
125+ g_autofree const gchar *host = nested_host ? "/host" : "/";
126+
127+ bind(host, prefix);
128+
129+ // Replace /host by an actual (inner) /host.
130+ if (nested_host) {
131+ fail_if(g_mkdir("/real-host", 0755));
132+ fail_if(mount("/host/host", "/real-host", NULL, MS_BIND | MS_REC, NULL));
133+ // For some reason umount("/host") returns EBUSY even immediately after
134+ // pivot_root. We detach it at least to keep `/proc/mounts` from blowing
135+ // up in nested cases.
136+ fail_if(umount2("/host", MNT_DETACH));
137+ fail_if(mount("/real-host", "/host", NULL, MS_MOVE, NULL));
138+ fail_if(rmdir("/real-host"));
139+ }
123140
124− fail_if(chroot(prefix));
125141 fail_if(chdir("/"));
126142 fail_if(execvp(*argv, argv));
127143 }
@@ -130,10 +146,7 @@ int main(gint argc, gchar **argv) {
130146 int status;
131147
132148 fail_if(waitpid(cpid, &status, 0) != cpid);
133− // glibc 2.27 (and possibly other versions) can't handle
134− // an nopenfd value larger than 2^19
135− fail_if(nftw(prefix, nftw_remove, min(getdtablesize(), 1<<19),
136− FTW_DEPTH | FTW_MOUNT | FTW_PHYS));
149+ fail_if(rmdir(prefix));
137150
138151 if (WIFEXITED(status))
139152 return WEXITSTATUS(status);