xref: /freebsd/usr.sbin/jail/command.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2011 James Gritton
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/mount.h>
33 #include <sys/stat.h>
34 #include <sys/sysctl.h>
35 #include <sys/user.h>
36 #include <sys/wait.h>
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <kvm.h>
42 #include <login_cap.h>
43 #include <paths.h>
44 #include <pwd.h>
45 #include <signal.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 
51 #include "jailp.h"
52 
53 #define DEFAULT_STOP_TIMEOUT	10
54 #define PHASH_SIZE		256
55 
56 LIST_HEAD(phhead, phash);
57 
58 struct phash {
59 	LIST_ENTRY(phash)	le;
60 	struct cfjail		*j;
61 	pid_t			pid;
62 };
63 
64 int paralimit = -1;
65 
66 extern char **environ;
67 
68 static int run_command(struct cfjail *j);
69 static int add_proc(struct cfjail *j, pid_t pid);
70 static void clear_procs(struct cfjail *j);
71 static struct cfjail *find_proc(pid_t pid);
72 static int term_procs(struct cfjail *j);
73 static int get_user_info(struct cfjail *j, const char *username,
74     const struct passwd **pwdp, login_cap_t **lcapp);
75 static int check_path(struct cfjail *j, const char *pname, const char *path,
76     int isfile, const char *umount_type);
77 
78 static struct cfjails sleeping = TAILQ_HEAD_INITIALIZER(sleeping);
79 static struct cfjails runnable = TAILQ_HEAD_INITIALIZER(runnable);
80 static struct cfstring dummystring = { .len = 1 };
81 static struct phhead phash[PHASH_SIZE];
82 static int kq;
83 
84 /*
85  * Run the next command associated with a jail.
86  */
87 int
88 next_command(struct cfjail *j)
89 {
90 	enum intparam comparam;
91 	int create_failed, stopping;
92 
93 	if (paralimit == 0) {
94 		requeue(j, &runnable);
95 		return 1;
96 	}
97 	create_failed = (j->flags & (JF_STOP | JF_FAILED)) == JF_FAILED;
98 	stopping = (j->flags & JF_STOP) != 0;
99 	comparam = *j->comparam;
100 	for (;;) {
101 		if (j->comstring == NULL) {
102 			j->comparam += create_failed ? -1 : 1;
103 			switch ((comparam = *j->comparam)) {
104 			case IP__NULL:
105 				return 0;
106 			case IP_MOUNT_DEVFS:
107 				if (!bool_param(j->intparams[IP_MOUNT_DEVFS]))
108 					continue;
109 				j->comstring = &dummystring;
110 				break;
111 			case IP_MOUNT_FDESCFS:
112 				if (!bool_param(j->intparams[IP_MOUNT_FDESCFS]))
113 					continue;
114 				j->comstring = &dummystring;
115 				break;
116 			case IP_MOUNT_PROCFS:
117 				if (!bool_param(j->intparams[IP_MOUNT_PROCFS]))
118 					continue;
119 				j->comstring = &dummystring;
120 				break;
121 			case IP__OP:
122 			case IP_STOP_TIMEOUT:
123 				j->comstring = &dummystring;
124 				break;
125 			default:
126 				if (j->intparams[comparam] == NULL)
127 					continue;
128 				j->comstring = create_failed || (stopping &&
129 				    (j->intparams[comparam]->flags & PF_REV))
130 				    ? TAILQ_LAST(&j->intparams[comparam]->val,
131 					cfstrings)
132 				    : TAILQ_FIRST(&j->intparams[comparam]->val);
133 			}
134 		} else {
135 			j->comstring = j->comstring == &dummystring ? NULL :
136 			    create_failed || (stopping &&
137 			    (j->intparams[comparam]->flags & PF_REV))
138 			    ? TAILQ_PREV(j->comstring, cfstrings, tq)
139 			    : TAILQ_NEXT(j->comstring, tq);
140 		}
141 		if (j->comstring == NULL || j->comstring->len == 0 ||
142 		    (create_failed && (comparam == IP_EXEC_PRESTART ||
143 		    comparam == IP_EXEC_START || comparam == IP_COMMAND ||
144 		    comparam == IP_EXEC_POSTSTART)))
145 			continue;
146 		switch (run_command(j)) {
147 		case -1:
148 			failed(j);
149 			/* FALLTHROUGH */
150 		case 1:
151 			return 1;
152 		}
153 	}
154 }
155 
156 /*
157  * Check command exit status
158  */
159 int
160 finish_command(struct cfjail *j)
161 {
162 	int error;
163 
164 	if (!(j->flags & JF_SLEEPQ))
165 		return 0;
166 	j->flags &= ~JF_SLEEPQ;
167 	if (*j->comparam == IP_STOP_TIMEOUT)
168 	{
169 		j->flags &= ~JF_TIMEOUT;
170 		j->pstatus = 0;
171 		return 0;
172 	}
173 	paralimit++;
174 	if (!TAILQ_EMPTY(&runnable))
175 		requeue(TAILQ_FIRST(&runnable), &ready);
176 	error = 0;
177 	if (j->flags & JF_TIMEOUT) {
178 		j->flags &= ~JF_TIMEOUT;
179 		if (*j->comparam != IP_STOP_TIMEOUT) {
180 			jail_warnx(j, "%s: timed out", j->comline);
181 			failed(j);
182 			error = -1;
183 		} else if (verbose > 0)
184 			jail_note(j, "timed out\n");
185 	} else if (j->pstatus != 0) {
186 		if (WIFSIGNALED(j->pstatus))
187 			jail_warnx(j, "%s: exited on signal %d",
188 			    j->comline, WTERMSIG(j->pstatus));
189 		else
190 			jail_warnx(j, "%s: failed", j->comline);
191 		j->pstatus = 0;
192 		failed(j);
193 		error = -1;
194 	}
195 	free(j->comline);
196 	j->comline = NULL;
197 	return error;
198 }
199 
200 /*
201  * Check for finished processes or timeouts.
202  */
203 struct cfjail *
204 next_proc(int nonblock)
205 {
206 	struct kevent ke;
207 	struct timespec ts;
208 	struct timespec *tsp;
209 	struct cfjail *j;
210 
211 	if (!TAILQ_EMPTY(&sleeping)) {
212 	again:
213 		tsp = NULL;
214 		if ((j = TAILQ_FIRST(&sleeping)) && j->timeout.tv_sec) {
215 			clock_gettime(CLOCK_REALTIME, &ts);
216 			ts.tv_sec = j->timeout.tv_sec - ts.tv_sec;
217 			ts.tv_nsec = j->timeout.tv_nsec - ts.tv_nsec;
218 			if (ts.tv_nsec < 0) {
219 				ts.tv_sec--;
220 				ts.tv_nsec += 1000000000;
221 			}
222 			if (ts.tv_sec < 0 ||
223 			    (ts.tv_sec == 0 && ts.tv_nsec == 0)) {
224 				j->flags |= JF_TIMEOUT;
225 				clear_procs(j);
226 				return j;
227 			}
228 			tsp = &ts;
229 		}
230 		if (nonblock) {
231 			ts.tv_sec = 0;
232 			ts.tv_nsec = 0;
233 			tsp = &ts;
234 		}
235 		switch (kevent(kq, NULL, 0, &ke, 1, tsp)) {
236 		case -1:
237 			if (errno != EINTR)
238 				err(1, "kevent");
239 			goto again;
240 		case 0:
241 			if (!nonblock) {
242 				j = TAILQ_FIRST(&sleeping);
243 				j->flags |= JF_TIMEOUT;
244 				clear_procs(j);
245 				return j;
246 			}
247 			break;
248 		case 1:
249 			(void)waitpid(ke.ident, NULL, WNOHANG);
250 			if ((j = find_proc(ke.ident))) {
251 				j->pstatus = ke.data;
252 				return j;
253 			}
254 			goto again;
255 		}
256 	}
257 	return NULL;
258 }
259 
260 /*
261  * Run a single command for a jail, possible inside the jail.
262  */
263 static int
264 run_command(struct cfjail *j)
265 {
266 	const struct passwd *pwd;
267 	const struct cfstring *comstring, *s;
268 	login_cap_t *lcap;
269 	const char **argv;
270 	char *acs, *cs, *comcs, *devpath;
271 	const char *jidstr, *conslog, *path, *ruleset, *term, *username;
272 	enum intparam comparam;
273 	size_t comlen;
274 	pid_t pid;
275 	int argc, bg, clean, consfd, down, fib, i, injail, sjuser, timeout;
276 #if defined(INET) || defined(INET6)
277 	char *addr, *extrap, *p, *val;
278 #endif
279 
280 	static char *cleanenv;
281 
282 	/* Perform some operations that aren't actually commands */
283 	comparam = *j->comparam;
284 	down = j->flags & (JF_STOP | JF_FAILED);
285 	switch (comparam) {
286 	case IP_STOP_TIMEOUT:
287 		return term_procs(j);
288 
289 	case IP__OP:
290 		if (down) {
291 			if (jail_remove(j->jid) < 0 && errno == EPERM) {
292 				jail_warnx(j, "jail_remove: %s",
293 					   strerror(errno));
294 				return -1;
295 			}
296 			if (verbose > 0 || (verbose == 0 && (j->flags & JF_STOP
297 			    ? note_remove : j->name != NULL)))
298 			    jail_note(j, "removed\n");
299 			j->jid = -1;
300 			if (j->flags & JF_STOP)
301 				dep_done(j, DF_LIGHT);
302 			else
303 				j->flags &= ~JF_PERSIST;
304 		} else {
305 			if (create_jail(j) < 0)
306 				return -1;
307 			if (iflag)
308 				printf("%d\n", j->jid);
309 			if (verbose >= 0 && (j->name || verbose > 0))
310 				jail_note(j, "created\n");
311 			dep_done(j, DF_LIGHT);
312 		}
313 		return 0;
314 
315 	default: ;
316 	}
317 	/*
318 	 * Collect exec arguments.  Internal commands for network and
319 	 * mounting build their own argument lists.
320 	 */
321 	comstring = j->comstring;
322 	bg = 0;
323 	switch (comparam) {
324 #ifdef INET
325 	case IP__IP4_IFADDR:
326 		argc = 0;
327 		val = alloca(strlen(comstring->s) + 1);
328 		strcpy(val, comstring->s);
329 		cs = val;
330 		extrap = NULL;
331 		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
332 			if (extrap == NULL) {
333 				*p = '\0';
334 				extrap = p + 1;
335 			}
336 			cs = p + 1;
337 			argc++;
338 		}
339 
340 		argv = alloca((8 + argc) * sizeof(char *));
341 		argv[0] = _PATH_IFCONFIG;
342 		if ((cs = strchr(val, '|'))) {
343 			argv[1] = acs = alloca(cs - val + 1);
344 			strlcpy(acs, val, cs - val + 1);
345 			addr = cs + 1;
346 		} else {
347 			argv[1] = string_param(j->intparams[IP_INTERFACE]);
348 			addr = val;
349 		}
350 		argv[2] = "inet";
351 		if (!(cs = strchr(addr, '/'))) {
352 			argv[3] = addr;
353 			argv[4] = "netmask";
354 			argv[5] = "255.255.255.255";
355 			argc = 6;
356 		} else if (strchr(cs + 1, '.')) {
357 			argv[3] = acs = alloca(cs - addr + 1);
358 			strlcpy(acs, addr, cs - addr + 1);
359 			argv[4] = "netmask";
360 			argv[5] = cs + 1;
361 			argc = 6;
362 		} else {
363 			argv[3] = addr;
364 			argc = 4;
365 		}
366 
367 		if (!down) {
368 			for (cs = strtok(extrap, " "); cs;
369 			     cs = strtok(NULL, " ")) {
370 				size_t len = strlen(cs) + 1;
371 				argv[argc++] = acs = alloca(len);
372 				strlcpy(acs, cs, len);
373 			}
374 		}
375 
376 		argv[argc] = down ? "-alias" : "alias";
377 		argv[argc + 1] = NULL;
378 		break;
379 #endif
380 
381 #ifdef INET6
382 	case IP__IP6_IFADDR:
383 		argc = 0;
384 		val = alloca(strlen(comstring->s) + 1);
385 		strcpy(val, comstring->s);
386 		cs = val;
387 		extrap = NULL;
388 		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
389 			if (extrap == NULL) {
390 				*p = '\0';
391 				extrap = p + 1;
392 			}
393 			cs = p + 1;
394 			argc++;
395 		}
396 
397 		argv = alloca((8 + argc) * sizeof(char *));
398 		argv[0] = _PATH_IFCONFIG;
399 		if ((cs = strchr(val, '|'))) {
400 			argv[1] = acs = alloca(cs - val + 1);
401 			strlcpy(acs, val, cs - val + 1);
402 			addr = cs + 1;
403 		} else {
404 			argv[1] = string_param(j->intparams[IP_INTERFACE]);
405 			addr = val;
406 		}
407 		argv[2] = "inet6";
408 		argv[3] = addr;
409 		if (!(cs = strchr(addr, '/'))) {
410 			argv[4] = "prefixlen";
411 			argv[5] = "128";
412 			argc = 6;
413 		} else
414 			argc = 4;
415 
416 		if (!down) {
417 			for (cs = strtok(extrap, " "); cs;
418 			     cs = strtok(NULL, " ")) {
419 				size_t len = strlen(cs) + 1;
420 				argv[argc++] = acs = alloca(len);
421 				strlcpy(acs, cs, len);
422 			}
423 		}
424 
425 		argv[argc] = down ? "-alias" : "alias";
426 		argv[argc + 1] = NULL;
427 		break;
428 #endif
429 
430 	case IP_VNET_INTERFACE:
431 		argv = alloca(5 * sizeof(char *));
432 		argv[0] = _PATH_IFCONFIG;
433 		argv[1] = comstring->s;
434 		argv[2] = down ? "-vnet" : "vnet";
435 		jidstr = string_param(j->intparams[KP_JID]);
436 		argv[3] = jidstr ? jidstr : string_param(j->intparams[KP_NAME]);
437 		argv[4] = NULL;
438 		break;
439 
440 	case IP_MOUNT:
441 	case IP__MOUNT_FROM_FSTAB:
442 		argv = alloca(8 * sizeof(char *));
443 		comcs = alloca(comstring->len + 1);
444 		strcpy(comcs, comstring->s);
445 		argc = 0;
446 		for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
447 		     cs = strtok(NULL, " \t\f\v\r\n"))
448 			argv[argc++] = cs;
449 		if (argc == 0)
450 			return 0;
451 		if (argc < 3) {
452 			jail_warnx(j, "%s: %s: missing information",
453 			    j->intparams[comparam]->name, comstring->s);
454 			return -1;
455 		}
456 		if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
457 		    down ? argv[2] : NULL) < 0)
458 			return -1;
459 		if (down) {
460 			argv[4] = NULL;
461 			argv[3] = argv[1];
462 			argv[0] = "/sbin/umount";
463 		} else {
464 			if (argc == 4) {
465 				argv[7] = NULL;
466 				argv[6] = argv[1];
467 				argv[5] = argv[0];
468 				argv[4] = argv[3];
469 				argv[3] = "-o";
470 			} else {
471 				argv[5] = NULL;
472 				argv[4] = argv[1];
473 				argv[3] = argv[0];
474 			}
475 			argv[0] = _PATH_MOUNT;
476 		}
477 		argv[1] = "-t";
478 		break;
479 
480 	case IP_MOUNT_DEVFS:
481 		argv = alloca(7 * sizeof(char *));
482 		path = string_param(j->intparams[KP_PATH]);
483 		if (path == NULL) {
484 			jail_warnx(j, "mount.devfs: no path");
485 			return -1;
486 		}
487 		devpath = alloca(strlen(path) + 5);
488 		sprintf(devpath, "%s/dev", path);
489 		if (check_path(j, "mount.devfs", devpath, 0,
490 		    down ? "devfs" : NULL) < 0)
491 			return -1;
492 		if (down) {
493 			argv[0] = "/sbin/umount";
494 			argv[1] = devpath;
495 			argv[2] = NULL;
496 		} else {
497 			argv[0] = _PATH_MOUNT;
498 			argv[1] = "-t";
499 			argv[2] = "devfs";
500 			ruleset = string_param(j->intparams[KP_DEVFS_RULESET]);
501 			if (!ruleset)
502 			    ruleset = "4";	/* devfsrules_jail */
503 			argv[3] = acs = alloca(11 + strlen(ruleset));
504 			sprintf(acs, "-oruleset=%s", ruleset);
505 			argv[4] = ".";
506 			argv[5] = devpath;
507 			argv[6] = NULL;
508 		}
509 		break;
510 
511 	case IP_MOUNT_FDESCFS:
512 		argv = alloca(7 * sizeof(char *));
513 		path = string_param(j->intparams[KP_PATH]);
514 		if (path == NULL) {
515 			jail_warnx(j, "mount.fdescfs: no path");
516 			return -1;
517 		}
518 		devpath = alloca(strlen(path) + 8);
519 		sprintf(devpath, "%s/dev/fd", path);
520 		if (check_path(j, "mount.fdescfs", devpath, 0,
521 		    down ? "fdescfs" : NULL) < 0)
522 			return -1;
523 		if (down) {
524 			argv[0] = "/sbin/umount";
525 			argv[1] = devpath;
526 			argv[2] = NULL;
527 		} else {
528 			argv[0] = _PATH_MOUNT;
529 			argv[1] = "-t";
530 			argv[2] = "fdescfs";
531 			argv[3] = ".";
532 			argv[4] = devpath;
533 			argv[5] = NULL;
534 		}
535 		break;
536 
537 	case IP_MOUNT_PROCFS:
538 		argv = alloca(7 * sizeof(char *));
539 		path = string_param(j->intparams[KP_PATH]);
540 		if (path == NULL) {
541 			jail_warnx(j, "mount.procfs: no path");
542 			return -1;
543 		}
544 		devpath = alloca(strlen(path) + 6);
545 		sprintf(devpath, "%s/proc", path);
546 		if (check_path(j, "mount.procfs", devpath, 0,
547 		    down ? "procfs" : NULL) < 0)
548 			return -1;
549 		if (down) {
550 			argv[0] = "/sbin/umount";
551 			argv[1] = devpath;
552 			argv[2] = NULL;
553 		} else {
554 			argv[0] = _PATH_MOUNT;
555 			argv[1] = "-t";
556 			argv[2] = "procfs";
557 			argv[3] = ".";
558 			argv[4] = devpath;
559 			argv[5] = NULL;
560 		}
561 		break;
562 
563 	case IP_COMMAND:
564 		if (j->name != NULL)
565 			goto default_command;
566 		argc = 0;
567 		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
568 			argc++;
569 		argv = alloca((argc + 1) * sizeof(char *));
570 		argc = 0;
571 		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
572 			argv[argc++] = s->s;
573 		argv[argc] = NULL;
574 		j->comstring = &dummystring;
575 		break;
576 
577 	default:
578 	default_command:
579 		if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
580 		    !(cs[0] == '&' && cs[1] == '\0')) {
581 			argv = alloca(4 * sizeof(char *));
582 			argv[0] = _PATH_BSHELL;
583 			argv[1] = "-c";
584 			argv[2] = comstring->s;
585 			argv[3] = NULL;
586 		} else {
587 			if (cs) {
588 				*cs = 0;
589 				bg = 1;
590 			}
591 			comcs = alloca(comstring->len + 1);
592 			strcpy(comcs, comstring->s);
593 			argc = 0;
594 			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
595 			     cs = strtok(NULL, " \t\f\v\r\n"))
596 				argc++;
597 			argv = alloca((argc + 1) * sizeof(char *));
598 			strcpy(comcs, comstring->s);
599 			argc = 0;
600 			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
601 			     cs = strtok(NULL, " \t\f\v\r\n"))
602 				argv[argc++] = cs;
603 			argv[argc] = NULL;
604 		}
605 	}
606 	if (argv[0] == NULL)
607 		return 0;
608 
609 	if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
610 	    timeout != 0) {
611 		clock_gettime(CLOCK_REALTIME, &j->timeout);
612 		j->timeout.tv_sec += timeout;
613 	} else
614 		j->timeout.tv_sec = 0;
615 
616 	injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
617 	    comparam == IP_EXEC_STOP;
618 	clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
619 	username = string_param(j->intparams[injail
620 	    ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
621 	sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
622 
623 	consfd = 0;
624 	if (injail &&
625 	    (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
626 		if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0)
627 			return -1;
628 		consfd =
629 		    open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
630 		if (consfd < 0) {
631 			jail_warnx(j, "open %s: %s", conslog, strerror(errno));
632 			return -1;
633 		}
634 	}
635 
636 	comlen = 0;
637 	for (i = 0; argv[i]; i++)
638 		comlen += strlen(argv[i]) + 1;
639 	j->comline = cs = emalloc(comlen);
640 	for (i = 0; argv[i]; i++) {
641 		strcpy(cs, argv[i]);
642 		if (argv[i + 1]) {
643 			cs += strlen(argv[i]) + 1;
644 			cs[-1] = ' ';
645 		}
646 	}
647 	if (verbose > 0)
648 		jail_note(j, "run command%s%s%s: %s\n",
649 		    injail ? " in jail" : "", username ? " as " : "",
650 		    username ? username : "", j->comline);
651 
652 	pid = fork();
653 	if (pid < 0)
654 		err(1, "fork");
655 	if (pid > 0) {
656 		if (bg || !add_proc(j, pid)) {
657 			free(j->comline);
658 			j->comline = NULL;
659 			return 0;
660 		} else {
661 			paralimit--;
662 			return 1;
663 		}
664 	}
665 	if (bg)
666 		setsid();
667 
668 	/* Set up the environment and run the command */
669 	pwd = NULL;
670 	lcap = NULL;
671 	if ((clean || username) && injail && sjuser &&
672 	    get_user_info(j, username, &pwd, &lcap) < 0)
673 		exit(1);
674 	if (injail) {
675 		/* jail_attach won't chdir along with its chroot. */
676 		path = string_param(j->intparams[KP_PATH]);
677 		if (path && chdir(path) < 0) {
678 			jail_warnx(j, "chdir %s: %s", path, strerror(errno));
679 			exit(1);
680 		}
681 		if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
682 		    setfib(fib) < 0) {
683 			jail_warnx(j, "setfib: %s", strerror(errno));
684 			exit(1);
685 		}
686 		if (jail_attach(j->jid) < 0) {
687 			jail_warnx(j, "jail_attach: %s", strerror(errno));
688 			exit(1);
689 		}
690 	}
691 	if (clean || username) {
692 		if (!(injail && sjuser) &&
693 		    get_user_info(j, username, &pwd, &lcap) < 0)
694 			exit(1);
695 		if (clean) {
696 			term = getenv("TERM");
697 			environ = &cleanenv;
698 			setenv("PATH", "/bin:/usr/bin", 0);
699 			if (term != NULL)
700 				setenv("TERM", term, 1);
701 		}
702 		if (setgid(pwd->pw_gid) < 0) {
703 			jail_warnx(j, "setgid %d: %s", pwd->pw_gid,
704 			    strerror(errno));
705 			exit(1);
706 		}
707 		if (setusercontext(lcap, pwd, pwd->pw_uid, username
708 		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
709 		    : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
710 			jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
711 			    strerror(errno));
712 			exit(1);
713 		}
714 		login_close(lcap);
715 		setenv("USER", pwd->pw_name, 1);
716 		setenv("HOME", pwd->pw_dir, 1);
717 		setenv("SHELL",
718 		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
719 		if (clean && chdir(pwd->pw_dir) < 0) {
720 			jail_warnx(j, "chdir %s: %s",
721 			    pwd->pw_dir, strerror(errno));
722 			exit(1);
723 		}
724 		endpwent();
725 	}
726 
727 	if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
728 		jail_warnx(j, "exec.consolelog: %s", strerror(errno));
729 		exit(1);
730 	}
731 	closefrom(3);
732 	execvp(argv[0], __DECONST(char *const*, argv));
733 	jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
734 	exit(1);
735 }
736 
737 /*
738  * Add a process to the hash, tied to a jail.
739  */
740 static int
741 add_proc(struct cfjail *j, pid_t pid)
742 {
743 	struct kevent ke;
744 	struct cfjail *tj;
745 	struct phash *ph;
746 
747 	if (!kq && (kq = kqueue()) < 0)
748 		err(1, "kqueue");
749 	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
750 	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
751 		if (errno == ESRCH)
752 			return 0;
753 		err(1, "kevent");
754 	}
755 	ph = emalloc(sizeof(struct phash));
756 	ph->j = j;
757 	ph->pid = pid;
758 	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
759 	j->nprocs++;
760 	j->flags |= JF_SLEEPQ;
761 	if (j->timeout.tv_sec == 0)
762 		requeue(j, &sleeping);
763 	else {
764 		/* File the jail in the sleep queue according to its timeout. */
765 		TAILQ_REMOVE(j->queue, j, tq);
766 		TAILQ_FOREACH(tj, &sleeping, tq) {
767 			if (!tj->timeout.tv_sec ||
768 			    j->timeout.tv_sec < tj->timeout.tv_sec ||
769 			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
770 			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
771 				TAILQ_INSERT_BEFORE(tj, j, tq);
772 				break;
773 			}
774 		}
775 		if (tj == NULL)
776 			TAILQ_INSERT_TAIL(&sleeping, j, tq);
777 		j->queue = &sleeping;
778 	}
779 	return 1;
780 }
781 
782 /*
783  * Remove any processes from the hash that correspond to a jail.
784  */
785 static void
786 clear_procs(struct cfjail *j)
787 {
788 	struct kevent ke;
789 	struct phash *ph, *tph;
790 	int i;
791 
792 	j->nprocs = 0;
793 	for (i = 0; i < PHASH_SIZE; i++)
794 		LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
795 			if (ph->j == j) {
796 				EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
797 				    NOTE_EXIT, 0, NULL);
798 				(void)kevent(kq, &ke, 1, NULL, 0, NULL);
799 				LIST_REMOVE(ph, le);
800 				free(ph);
801 			}
802 }
803 
804 /*
805  * Find the jail that corresponds to an exited process.
806  */
807 static struct cfjail *
808 find_proc(pid_t pid)
809 {
810 	struct cfjail *j;
811 	struct phash *ph;
812 
813 	LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
814 		if (ph->pid == pid) {
815 			j = ph->j;
816 			LIST_REMOVE(ph, le);
817 			free(ph);
818 			return --j->nprocs ? NULL : j;
819 		}
820 	return NULL;
821 }
822 
823 /*
824  * Send SIGTERM to all processes in a jail and wait for them to die.
825  */
826 static int
827 term_procs(struct cfjail *j)
828 {
829 	struct kinfo_proc *ki;
830 	int i, noted, pcnt, timeout;
831 
832 	static kvm_t *kd;
833 
834 	if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
835 		timeout = DEFAULT_STOP_TIMEOUT;
836 	else if (timeout == 0)
837 		return 0;
838 
839 	if (kd == NULL) {
840 		kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
841 		if (kd == NULL)
842 			return 0;
843 	}
844 
845 	ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
846 	if (ki == NULL)
847 		return 0;
848 	noted = 0;
849 	for (i = 0; i < pcnt; i++)
850 		if (ki[i].ki_jid == j->jid &&
851 		    kill(ki[i].ki_pid, SIGTERM) == 0) {
852 			(void)add_proc(j, ki[i].ki_pid);
853 			if (verbose > 0) {
854 				if (!noted) {
855 					noted = 1;
856 					jail_note(j, "sent SIGTERM to:");
857 				}
858 				printf(" %d", ki[i].ki_pid);
859 			}
860 		}
861 	if (noted)
862 		printf("\n");
863 	if (j->nprocs > 0) {
864 		clock_gettime(CLOCK_REALTIME, &j->timeout);
865 		j->timeout.tv_sec += timeout;
866 		return 1;
867 	}
868 	return 0;
869 }
870 
871 /*
872  * Look up a user in the passwd and login.conf files.
873  */
874 static int
875 get_user_info(struct cfjail *j, const char *username,
876     const struct passwd **pwdp, login_cap_t **lcapp)
877 {
878 	const struct passwd *pwd;
879 
880 	errno = 0;
881 	*pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
882 	if (pwd == NULL) {
883 		if (errno)
884 			jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
885 			    username ? username : "", strerror(errno));
886 		else if (username)
887 			jail_warnx(j, "%s: no such user", username);
888 		else
889 			jail_warnx(j, "unknown uid %d", getuid());
890 		return -1;
891 	}
892 	*lcapp = login_getpwclass(pwd);
893 	if (*lcapp == NULL) {
894 		jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
895 		    strerror(errno));
896 		return -1;
897 	}
898 	/* Set the groups while the group file is still available */
899 	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
900 		jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
901 		    strerror(errno));
902 		return -1;
903 	}
904 	return 0;
905 }
906 
907 /*
908  * Make sure a mount or consolelog path is a valid absolute pathname
909  * with no symlinks.
910  */
911 static int
912 check_path(struct cfjail *j, const char *pname, const char *path, int isfile,
913     const char *umount_type)
914 {
915 	struct stat st, mpst;
916 	struct statfs stfs;
917 	char *tpath, *p;
918 	const char *jailpath;
919 	size_t jplen;
920 
921 	if (path[0] != '/') {
922 		jail_warnx(j, "%s: %s: not an absolute pathname",
923 		    pname, path);
924 		return -1;
925 	}
926 	/*
927 	 * Only check for symlinks in components below the jail's path,
928 	 * since that's where the security risk lies.
929 	 */
930 	jailpath = string_param(j->intparams[KP_PATH]);
931 	if (jailpath == NULL)
932 		jailpath = "";
933 	jplen = strlen(jailpath);
934 	if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
935 		tpath = alloca(strlen(path) + 1);
936 		strcpy(tpath, path);
937 		for (p = tpath + jplen; p != NULL; ) {
938 			p = strchr(p + 1, '/');
939 			if (p)
940 				*p = '\0';
941 			if (lstat(tpath, &st) < 0) {
942 				if (errno == ENOENT && isfile && !p)
943 					break;
944 				jail_warnx(j, "%s: %s: %s", pname, tpath,
945 				    strerror(errno));
946 				return -1;
947 			}
948 			if (S_ISLNK(st.st_mode)) {
949 				jail_warnx(j, "%s: %s is a symbolic link",
950 				    pname, tpath);
951 				return -1;
952 			}
953 			if (p)
954 				*p = '/';
955 		}
956 	}
957 	if (umount_type != NULL) {
958 		if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
959 			jail_warnx(j, "%s: %s: %s", pname, path,
960 			    strerror(errno));
961 			return -1;
962 		}
963 		if (stat(stfs.f_mntonname, &mpst) < 0) {
964 			jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
965 			    strerror(errno));
966 			return -1;
967 		}
968 		if (st.st_ino != mpst.st_ino) {
969 			jail_warnx(j, "%s: %s: not a mount point",
970 			    pname, path);
971 			return -1;
972 		}
973 		if (strcmp(stfs.f_fstypename, umount_type)) {
974 			jail_warnx(j, "%s: %s: not a %s mount",
975 			    pname, path, umount_type);
976 			return -1;
977 		}
978 	}
979 	return 0;
980 }
981