git.iko.soy/typeable/nixpkgs
on master help sign in
chrootenv: propagate nested chrootenv /host

To avoid symlink loops to /host in nested chrootenvs we need to remove one level of indirection. This is also what's generally expected of /host contents.

commit06f27dc2e932778cf1d2ce6b0bf30b32575dd16a authorNikolay Amiantov <ab@fmap.me> dateSun 12 May 2019, 09:35 (8 years ago) parent7664ffb
1 file changed+23−6
pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c+23−6
pkgs/build-support/build-fhs-userenv/chrootenv/chrootenv.c+23−6
@@ -18,7 +18,7 @@
1818 if (expr) \
1919 fail(#expr, errno);
2020
21−const gchar *bind_blacklist[] = {"bin", "etc", "host", "usr", "lib", "lib64", "lib32", "sbin", NULL};
21+const gchar *bind_blacklist[] = {"bin", "etc", "host", "real-host", "usr", "lib", "lib64", "lib32", "sbin", NULL};
2222
2323int pivot_root(const char *new_root, const char *put_old) {
2424 return syscall(SYS_pivot_root, new_root, put_old);
@@ -56,9 +56,12 @@ void bind_mount_item(const gchar *host, const gchar *guest, const gchar *name) {
5656
5757void bind(const gchar *host, const gchar *guest) {
5858 mount_tmpfs(guest);
59+ pivot_host(guest);
60+
61+ g_autofree gchar *host_dir = g_build_filename("/host", host, NULL);
5962
6063 g_autoptr(GError) err = NULL;
61− g_autoptr(GDir) dir = g_dir_open(host, 0, &err);
64+ g_autoptr(GDir) dir = g_dir_open(host_dir, 0, &err);
6265
6366 if (err != NULL)
6467 fail("g_dir_open", errno);
@@ -67,9 +70,7 @@ void bind(const gchar *host, const gchar *guest) {
6770
6871 while ((item = g_dir_read_name(dir)))
6972 if (!g_strv_contains(bind_blacklist, item))
70− bind_mount_item(host, guest, item);
71−
72− pivot_host(guest);
73+ bind_mount_item(host_dir, "/", item);
7374}
7475
7576void spit(const char *path, char *fmt, ...) {
@@ -119,7 +120,23 @@ 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
124141 fail_if(chdir("/"));
125142 fail_if(execvp(*argv, argv));