@@ -26,6 +26,10 @@
26using namespace std;
26using namespace std;
28#define err_exit(format, ...) { fprintf(stderr, format ": %s\n", ##__VA_ARGS__, strerror(errno)); exit(EXIT_FAILURE); }
28#define err_exit(format, ...) { fprintf(stderr, format ": %s\n", ##__VA_ARGS__, strerror(errno)); exit(EXIT_FAILURE); }
29+static int child_proc(const char *rootdir, const char *nixdir, uint8_t clear_env, list<struct DirMapping> dirMappings, list<struct SetEnv> envMappings, const char *executable, char * const new_argv[]);
31+volatile uint8_t child_died = 0;
30static void usage(const char *pname) {
34static void usage(const char *pname) {
31 fprintf(stderr, "Usage: %s -n <nixpath> -- <command>\n", pname);
35 fprintf(stderr, "Usage: %s -n <nixpath> -- <command>\n", pname);
@@ -33,6 +37,7 @@ static void usage(const char *pname) {
33 fprintf(stderr, "\t-m <src>:<src>\tmap src on the host to dest in the sandbox\n");
37 fprintf(stderr, "\t-m <src>:<src>\tmap src on the host to dest in the sandbox\n");
34 fprintf(stderr, "\t-d\tdelete all default dir mappings, may break things\n");
38 fprintf(stderr, "\t-d\tdelete all default dir mappings, may break things\n");
35 fprintf(stderr, "\t-p <var>\tpreserve the value of a variable across the -c clear\n");
39 fprintf(stderr, "\t-p <var>\tpreserve the value of a variable across the -c clear\n");
40+ fprintf(stderr, "\t-e\tadd an /escape-hatch to the sandbox, and run (outside the sandbox) any strings written to it\n");
37 exit(EXIT_FAILURE);
42 exit(EXIT_FAILURE);
@@ -91,8 +96,15 @@ struct DirMapping parseMapping(string input) {
91 return (struct DirMapping){ src, dest };
96 return (struct DirMapping){ src, dest };
99+static void handle_child_death(int signo, siginfo_t *info, void *context) {
100+ if ( (child_pid == 0) || (info->si_pid == child_pid) ) {
94int main(int argc, char *argv[]) {
105int main(int argc, char *argv[]) {
95 uint8_t clear_env = 0;
106 uint8_t clear_env = 0;
107+ uint8_t enable_escape_hatch = 0;
96 char *nixdir = NULL;
108 char *nixdir = NULL;
97 list<struct DirMapping> dirMappings;
109 list<struct DirMapping> dirMappings;
98 list<struct SetEnv> envMappings;
110 list<struct SetEnv> envMappings;
@@ -105,14 +117,20 @@ int main(int argc, char *argv[]) {
105 x("run");
117 x("run");
106 x("tmp");
118 x("tmp");
107 x("var");
119 x("var");
111− while ((opt = getopt(argc, argv, "cn:m:dp:")) != -1) {
127+ while ((opt = getopt(argc, argv, "cen:m:dp:")) != -1) {
112 switch (opt) {
128 switch (opt) {
113 case 'c':
129 case 'c':
114 clear_env = 1;
130 clear_env = 1;
115− puts("clearing env");
131+ break;
133+ enable_escape_hatch = 1;
117 case 'n':
135 case 'n':
118 // determine absolute directory for nix dir
136 // determine absolute directory for nix dir
@@ -161,13 +179,74 @@ int main(int argc, char *argv[]) {
161 err_exit("mkdtemp(%s)", template_);
179 err_exit("mkdtemp(%s)", template_);
188+ struct sigaction handle_child;
189+ handle_child.sa_sigaction = handle_child_death;
190+ handle_child.sa_flags = SA_SIGINFO;
192+ struct sigaction old_handler;
193+ if (sigaction(SIGCHLD, &handle_child, &old_handler)) {
194+ err_exit("sigaction()");
198+ child = child_pid = fork();
201+ } else if (child == 0) {
202+ sigaction(SIGCHLD, &old_handler, NULL);
205+ read(unrace[0], buf, 10);
207+ return child_proc(rootdir, nixdir, clear_env, dirMappings, envMappings, argv[optind], argv + optind);
210+ char fifopath[PATH_MAX];
211+ if (enable_escape_hatch) {
212+ snprintf(fifopath, PATH_MAX, "%s/escape-hatch", rootdir);
213+ mkfifo(fifopath, 0600);
216+ if (enable_escape_hatch) {
218+ while (!child_died) {
219+ int fd = open(fifopath, O_RDONLY);
221+ if (errno == EINTR) continue;
222+ fprintf(stderr, "error opening escape-hatch: %s\n", strerror(errno));
225+ int size = read(fd, buffer, 1024);
232+ int ret = waitpid(child, &status, 0);
233+ return WEXITSTATUS(status);
237+static int child_proc(const char *rootdir, const char *nixdir, uint8_t clear_env, list<struct DirMapping> dirMappings, list<struct SetEnv> envMappings, const char *executable, char * const new_argv[]) {
164 // get uid, gid before going to new namespace
238 // get uid, gid before going to new namespace
165 uid_t uid = getuid();
239 uid_t uid = getuid();
166 gid_t gid = getgid();
240 gid_t gid = getgid();
168 // "unshare" into new namespace
242 // "unshare" into new namespace
169 if (unshare(CLONE_NEWNS | CLONE_NEWUSER) < 0) {
243 if (unshare(CLONE_NEWNS | CLONE_NEWUSER) < 0) {
244+ if (errno == EPERM) {
245+ fputs("Run the following to enable unprivileged namespace use:\nsudo bash -c \"sysctl -w kernel.unprivileged_userns_clone=1 ; echo kernel.unprivileged_userns_clone=1 > /etc/sysctl.d/nix-user-chroot.conf\"\n\n", stderr);
170− err_exit("unshare()");
248+ err_exit("unshare()");
173 // add necessary system stuff to rootdir namespace
252 // add necessary system stuff to rootdir namespace
@@ -177,57 +256,52 @@ int main(int argc, char *argv[]) {
177 add_path(m.src, m.dest, rootdir);
256 add_path(m.src, m.dest, rootdir);
180− char path_buf[PATH_MAX];
181− snprintf(path_buf, sizeof(path_buf), "%s/tmp", rootdir);
182− mkdir(path_buf, ~0);
183− snprintf(path_buf, sizeof(path_buf), "%s/var", rootdir);
185− // make sure nixdir exists
259+ // make sure nixdir exists
186− struct stat statbuf2;
260+ struct stat statbuf2;
187− if (stat(nixdir, &statbuf2) < 0) {
261+ if (stat(nixdir, &statbuf2) < 0) {
188− err_exit("stat(%s)", nixdir);
262+ err_exit("stat(%s)", nixdir);
265+ char path_buf[PATH_MAX];
191− // mount /nix to new namespace
266+ // mount /nix to new namespace
192− snprintf(path_buf, sizeof(path_buf), "%s/nix", rootdir);
267+ snprintf(path_buf, sizeof(path_buf), "%s/nix", rootdir);
193− mkdir(path_buf, statbuf2.st_mode & ~S_IFMT);
268+ mkdir(path_buf, statbuf2.st_mode & ~S_IFMT);
194− if (mount(nixdir, path_buf, "none", MS_BIND | MS_REC, NULL) < 0) {
269+ if (mount(nixdir, path_buf, "none", MS_BIND | MS_REC, NULL) < 0) {
195− err_exit("mount(%s, %s)", nixdir, path_buf);
270+ err_exit("mount(%s, %s)", nixdir, path_buf);
198− // fixes issue #1 where writing to /proc/self/gid_map fails
273+ // fixes issue #1 where writing to /proc/self/gid_map fails
199− // see user_namespaces(7) for more documentation
274+ // see user_namespaces(7) for more documentation
200− int fd_setgroups = open("/proc/self/setgroups", O_WRONLY);
275+ int fd_setgroups = open("/proc/self/setgroups", O_WRONLY);
201− if (fd_setgroups > 0) {
276+ if (fd_setgroups > 0) {
202− write(fd_setgroups, "deny", 4);
277+ write(fd_setgroups, "deny", 4);
278+ close(fd_setgroups);
205− // map the original uid/gid in the new ns
281+ // map the original uid/gid in the new ns
206− char map_buf[1024];
282+ char map_buf[1024];
207− snprintf(map_buf, sizeof(map_buf), "%d %d 1", uid, uid);
283+ snprintf(map_buf, sizeof(map_buf), "%d %d 1", uid, uid);
208− update_map(map_buf, "/proc/self/uid_map");
284+ update_map(map_buf, "/proc/self/uid_map");
209− snprintf(map_buf, sizeof(map_buf), "%d %d 1", gid, gid);
285+ snprintf(map_buf, sizeof(map_buf), "%d %d 1", gid, gid);
210− update_map(map_buf, "/proc/self/gid_map");
286+ update_map(map_buf, "/proc/self/gid_map");
212− // chroot to rootdir
288+ // chroot to rootdir
213− if (chroot(rootdir) < 0) {
289+ if (chroot(rootdir) < 0) {
214− err_exit("chroot(%s)", rootdir);
290+ err_exit("chroot(%s)", rootdir);
217− chdir("/");
293+ chdir("/");
219 if (clear_env) clearenv();
295 if (clear_env) clearenv();
220− setenv("NIX_PATH", NIX_PATH, 1);
221− setenv("NIX_SSL_CERT_FILE", NIX_SSL_CERT_FILE, 1);
222 setenv("PATH", ENV_PATH, 1);
296 setenv("PATH", ENV_PATH, 1);
224 for (list<struct SetEnv>::iterator it = envMappings.begin();
298 for (list<struct SetEnv>::iterator it = envMappings.begin();
225− it != envMappings.end(); ++it) {
299+ it != envMappings.end(); ++it) {
226 struct SetEnv e = *it;
300 struct SetEnv e = *it;
227 setenv(e.key.c_str(), e.value.c_str(), 1);
301 setenv(e.key.c_str(), e.value.c_str(), 1);
230 // execute the command
304 // execute the command
231− execvp(argv[optind], argv + optind);
305+ execvp(executable, new_argv);
232− err_exit("execvp(%s)", argv[optind]);
306+ err_exit("execvp(%s)", executable);