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