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