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