xref: /freebsd/usr.sbin/jail/command.c (revision 02944d8c4969ffe97fcf84cb2ccb672e828c1d04)
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
root_cpuset_id(void)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
next_command(struct cfjail * j)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
finish_command(struct cfjail * j)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 *
next_proc(int nonblock)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
run_command(struct cfjail * j)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, *fmt, *path, *ruleset, *term, *username;
295 	enum intparam comparam;
296 	size_t comlen, ret;
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 
336 			/*
337 			 * Populate our jid and name parameters if they were not
338 			 * provided.  This simplifies later logic that wants to
339 			 * use the jid or name to be able to do so reliably.
340 			 */
341 			if (j->intparams[KP_JID] == NULL) {
342 				char ljidstr[16];
343 
344 				(void)snprintf(ljidstr, sizeof(ljidstr), "%d",
345 				    j->jid);
346 				add_param(j, NULL, KP_JID, ljidstr);
347 			}
348 
349 			/* This matches the kernel behavior. */
350 			if (j->intparams[KP_NAME] == NULL)
351 				add_param(j, j->intparams[KP_JID], KP_NAME,
352 				    NULL);
353 
354 			dep_done(j, DF_LIGHT);
355 		}
356 		return 0;
357 
358 	default: ;
359 	}
360 	/*
361 	 * Collect exec arguments.  Internal commands for network and
362 	 * mounting build their own argument lists.
363 	 */
364 	comstring = j->comstring;
365 	bg = 0;
366 	switch (comparam) {
367 #ifdef INET
368 	case IP__IP4_IFADDR:
369 		argc = 0;
370 		val = alloca(strlen(comstring->s) + 1);
371 		strcpy(val, comstring->s);
372 		cs = val;
373 		extrap = NULL;
374 		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
375 			if (extrap == NULL) {
376 				*p = '\0';
377 				extrap = p + 1;
378 			}
379 			cs = p + 1;
380 			argc++;
381 		}
382 
383 		argv = alloca((8 + argc) * sizeof(char *));
384 		argv[0] = _PATH_IFCONFIG;
385 		if ((cs = strchr(val, '|'))) {
386 			argv[1] = acs = alloca(cs - val + 1);
387 			strlcpy(acs, val, cs - val + 1);
388 			addr = cs + 1;
389 		} else {
390 			argv[1] = string_param(j->intparams[IP_INTERFACE]);
391 			addr = val;
392 		}
393 		argv[2] = "inet";
394 		if (!(cs = strchr(addr, '/'))) {
395 			argv[3] = addr;
396 			argv[4] = "netmask";
397 			argv[5] = "255.255.255.255";
398 			argc = 6;
399 		} else if (strchr(cs + 1, '.')) {
400 			argv[3] = acs = alloca(cs - addr + 1);
401 			strlcpy(acs, addr, cs - addr + 1);
402 			argv[4] = "netmask";
403 			argv[5] = cs + 1;
404 			argc = 6;
405 		} else {
406 			argv[3] = addr;
407 			argc = 4;
408 		}
409 
410 		if (!down && extrap != NULL) {
411 			for (cs = strtok(extrap, " "); cs;
412 			     cs = strtok(NULL, " ")) {
413 				size_t len = strlen(cs) + 1;
414 				argv[argc++] = acs = alloca(len);
415 				strlcpy(acs, cs, len);
416 			}
417 		}
418 
419 		argv[argc] = down ? "-alias" : "alias";
420 		argv[argc + 1] = NULL;
421 		break;
422 #endif
423 
424 #ifdef INET6
425 	case IP__IP6_IFADDR:
426 		argc = 0;
427 		val = alloca(strlen(comstring->s) + 1);
428 		strcpy(val, comstring->s);
429 		cs = val;
430 		extrap = NULL;
431 		while ((p = strchr(cs, ' ')) != NULL && strlen(p) > 1) {
432 			if (extrap == NULL) {
433 				*p = '\0';
434 				extrap = p + 1;
435 			}
436 			cs = p + 1;
437 			argc++;
438 		}
439 
440 		argv = alloca((8 + argc) * sizeof(char *));
441 		argv[0] = _PATH_IFCONFIG;
442 		if ((cs = strchr(val, '|'))) {
443 			argv[1] = acs = alloca(cs - val + 1);
444 			strlcpy(acs, val, cs - val + 1);
445 			addr = cs + 1;
446 		} else {
447 			argv[1] = string_param(j->intparams[IP_INTERFACE]);
448 			addr = val;
449 		}
450 		argv[2] = "inet6";
451 		argv[3] = addr;
452 		if (!(cs = strchr(addr, '/'))) {
453 			argv[4] = "prefixlen";
454 			argv[5] = "128";
455 			argc = 6;
456 		} else
457 			argc = 4;
458 
459 		if (!down && extrap != NULL) {
460 			for (cs = strtok(extrap, " "); cs;
461 			     cs = strtok(NULL, " ")) {
462 				size_t len = strlen(cs) + 1;
463 				argv[argc++] = acs = alloca(len);
464 				strlcpy(acs, cs, len);
465 			}
466 		}
467 
468 		argv[argc] = down ? "-alias" : "alias";
469 		argv[argc + 1] = NULL;
470 		break;
471 #endif
472 
473 	case IP_VNET_INTERFACE:
474 		argv = alloca(5 * sizeof(char *));
475 		argv[0] = _PATH_IFCONFIG;
476 		argv[1] = comstring->s;
477 		argv[2] = down ? "-vnet" : "vnet";
478 		argv[3] = string_param(j->intparams[KP_JID]);
479 		argv[4] = NULL;
480 		break;
481 
482 	case IP_MOUNT:
483 	case IP__MOUNT_FROM_FSTAB:
484 		argv = alloca(8 * sizeof(char *));
485 		comcs = alloca(comstring->len + 1);
486 		strcpy(comcs, comstring->s);
487 		argc = 0;
488 		for (cs = strtok(comcs, " \t\f\v\r\n"); cs && argc < 4;
489 		     cs = strtok(NULL, " \t\f\v\r\n")) {
490 			if (argc <= 1 && strunvis(cs, cs) < 0) {
491 				jail_warnx(j, "%s: %s: fstab parse error",
492 				    j->intparams[comparam]->name, comstring->s);
493 				return -1;
494 			}
495 			argv[argc++] = cs;
496 		}
497 		if (argc == 0)
498 			return 0;
499 		if (argc < 3) {
500 			jail_warnx(j, "%s: %s: missing information",
501 			    j->intparams[comparam]->name, comstring->s);
502 			return -1;
503 		}
504 		if (check_path(j, j->intparams[comparam]->name, argv[1], 0,
505 		    down ? argv[2] : NULL) < 0)
506 			return -1;
507 		if (down) {
508 			argv[4] = NULL;
509 			argv[3] = argv[1];
510 			argv[0] = "/sbin/umount";
511 		} else {
512 			if (argc == 4) {
513 				argv[7] = NULL;
514 				argv[6] = argv[1];
515 				argv[5] = argv[0];
516 				argv[4] = argv[3];
517 				argv[3] = "-o";
518 			} else {
519 				argv[5] = NULL;
520 				argv[4] = argv[1];
521 				argv[3] = argv[0];
522 			}
523 			argv[0] = _PATH_MOUNT;
524 		}
525 		argv[1] = "-t";
526 		break;
527 
528 	case IP_MOUNT_DEVFS:
529 		argv = alloca(7 * sizeof(char *));
530 		path = string_param(j->intparams[KP_PATH]);
531 		if (path == NULL) {
532 			jail_warnx(j, "mount.devfs: no jail root path defined");
533 			return -1;
534 		}
535 		devpath = alloca(strlen(path) + 5);
536 		sprintf(devpath, "%s/dev", path);
537 		if (check_path(j, "mount.devfs", devpath, 0,
538 		    down ? "devfs" : NULL) < 0)
539 			return -1;
540 		if (down) {
541 			argv[0] = "/sbin/umount";
542 			argv[1] = devpath;
543 			argv[2] = NULL;
544 		} else {
545 			argv[0] = _PATH_MOUNT;
546 			argv[1] = "-t";
547 			argv[2] = "devfs";
548 			ruleset = string_param(j->intparams[KP_DEVFS_RULESET]);
549 			if (!ruleset)
550 			    ruleset = "4";	/* devfsrules_jail */
551 			argv[3] = acs = alloca(11 + strlen(ruleset));
552 			sprintf(acs, "-oruleset=%s", ruleset);
553 			argv[4] = ".";
554 			argv[5] = devpath;
555 			argv[6] = NULL;
556 		}
557 		break;
558 
559 	case IP_MOUNT_FDESCFS:
560 		argv = alloca(7 * sizeof(char *));
561 		path = string_param(j->intparams[KP_PATH]);
562 		if (path == NULL) {
563 			jail_warnx(j, "mount.fdescfs: no jail root path defined");
564 			return -1;
565 		}
566 		devpath = alloca(strlen(path) + 8);
567 		sprintf(devpath, "%s/dev/fd", path);
568 		if (check_path(j, "mount.fdescfs", devpath, 0,
569 		    down ? "fdescfs" : NULL) < 0)
570 			return -1;
571 		if (down) {
572 			argv[0] = "/sbin/umount";
573 			argv[1] = devpath;
574 			argv[2] = NULL;
575 		} else {
576 			argv[0] = _PATH_MOUNT;
577 			argv[1] = "-t";
578 			argv[2] = "fdescfs";
579 			argv[3] = ".";
580 			argv[4] = devpath;
581 			argv[5] = NULL;
582 		}
583 		break;
584 
585 	case IP_MOUNT_PROCFS:
586 		argv = alloca(7 * sizeof(char *));
587 		path = string_param(j->intparams[KP_PATH]);
588 		if (path == NULL) {
589 			jail_warnx(j, "mount.procfs: no jail root path defined");
590 			return -1;
591 		}
592 		devpath = alloca(strlen(path) + 6);
593 		sprintf(devpath, "%s/proc", path);
594 		if (check_path(j, "mount.procfs", devpath, 0,
595 		    down ? "procfs" : NULL) < 0)
596 			return -1;
597 		if (down) {
598 			argv[0] = "/sbin/umount";
599 			argv[1] = devpath;
600 			argv[2] = NULL;
601 		} else {
602 			argv[0] = _PATH_MOUNT;
603 			argv[1] = "-t";
604 			argv[2] = "procfs";
605 			argv[3] = ".";
606 			argv[4] = devpath;
607 			argv[5] = NULL;
608 		}
609 		break;
610 
611 	case IP_ZFS_DATASET:
612 		argv = alloca(4 * sizeof(char *));
613 		jidstr = string_param(j->intparams[KP_JID]);
614 		fmt = "if [ $(/sbin/zfs get -H -o value jailed %s) = on ]; then /sbin/zfs jail %s %s || echo error, attaching %s to jail %s failed; else echo error, you need to set jailed=on for dataset %s; fi";
615 		comlen = strlen(fmt)
616 		    + 2 * strlen(jidstr)
617 		    + 4 * comstring->len
618 		    - 6 * 2	/* 6 * "%s" */
619 		    + 1;
620 		comcs = alloca(comlen);
621 		ret = snprintf(comcs, comlen, fmt, comstring->s,
622 		    jidstr, comstring->s, comstring->s, jidstr,
623 		    comstring->s);
624 		if (ret >= comlen) {
625 			jail_warnx(j, "internal error in ZFS dataset handling");
626 			exit(1);
627 		}
628 		argv[0] = _PATH_BSHELL;
629 		argv[1] = "-c";
630 		argv[2] = comcs;
631 		argv[3] = NULL;
632 		break;
633 
634 	case IP_COMMAND:
635 		if (j->name != NULL)
636 			goto default_command;
637 		argc = 0;
638 		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
639 			argc++;
640 		argv = alloca((argc + 1) * sizeof(char *));
641 		argc = 0;
642 		TAILQ_FOREACH(s, &j->intparams[IP_COMMAND]->val, tq)
643 			argv[argc++] = s->s;
644 		argv[argc] = NULL;
645 		j->comstring = &dummystring;
646 		break;
647 
648 	default:
649 	default_command:
650 		if ((cs = strpbrk(comstring->s, "!\"$&'()*;<>?[\\]`{|}~")) &&
651 		    !(cs[0] == '&' && cs[1] == '\0')) {
652 			argv = alloca(4 * sizeof(char *));
653 			argv[0] = _PATH_BSHELL;
654 			argv[1] = "-c";
655 			argv[2] = comstring->s;
656 			argv[3] = NULL;
657 		} else {
658 			if (cs) {
659 				*cs = 0;
660 				bg = 1;
661 			}
662 			comcs = alloca(comstring->len + 1);
663 			strcpy(comcs, comstring->s);
664 			argc = 0;
665 			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
666 			     cs = strtok(NULL, " \t\f\v\r\n"))
667 				argc++;
668 			argv = alloca((argc + 1) * sizeof(char *));
669 			strcpy(comcs, comstring->s);
670 			argc = 0;
671 			for (cs = strtok(comcs, " \t\f\v\r\n"); cs;
672 			     cs = strtok(NULL, " \t\f\v\r\n"))
673 				argv[argc++] = cs;
674 			argv[argc] = NULL;
675 		}
676 	}
677 	if (argv[0] == NULL)
678 		return 0;
679 
680 	if (int_param(j->intparams[IP_EXEC_TIMEOUT], &timeout) &&
681 	    timeout != 0) {
682 		clock_gettime(CLOCK_REALTIME, &j->timeout);
683 		j->timeout.tv_sec += timeout;
684 	} else
685 		j->timeout.tv_sec = 0;
686 
687 	injail = comparam == IP_EXEC_START || comparam == IP_COMMAND ||
688 	    comparam == IP_EXEC_STOP;
689 	if (injail)
690 		setid = root_cpuset_id();
691 	else
692 		setid = CPUSET_INVALID;
693 	clean = bool_param(j->intparams[IP_EXEC_CLEAN]);
694 	username = string_param(j->intparams[injail
695 	    ? IP_EXEC_JAIL_USER : IP_EXEC_SYSTEM_USER]);
696 	sjuser = bool_param(j->intparams[IP_EXEC_SYSTEM_JAIL_USER]);
697 
698 	consfd = 0;
699 	if (injail &&
700 	    (conslog = string_param(j->intparams[IP_EXEC_CONSOLELOG]))) {
701 		if (check_path(j, "exec.consolelog", conslog, 1, NULL) < 0)
702 			return -1;
703 		consfd =
704 		    open(conslog, O_WRONLY | O_CREAT | O_APPEND, DEFFILEMODE);
705 		if (consfd < 0) {
706 			jail_warnx(j, "open %s: %s", conslog, strerror(errno));
707 			return -1;
708 		}
709 	}
710 
711 	comlen = 0;
712 	for (i = 0; argv[i]; i++)
713 		comlen += strlen(argv[i]) + 1;
714 	j->comline = cs = emalloc(comlen);
715 	for (i = 0; argv[i]; i++) {
716 		strcpy(cs, argv[i]);
717 		if (argv[i + 1]) {
718 			cs += strlen(argv[i]) + 1;
719 			cs[-1] = ' ';
720 		}
721 	}
722 	if (verbose > 0)
723 		jail_note(j, "run command%s%s%s: %s\n",
724 		    injail ? " in jail" : "", username ? " as " : "",
725 		    username ? username : "", j->comline);
726 
727 	pid = fork();
728 	if (pid < 0)
729 		err(1, "fork");
730 	if (pid > 0) {
731 		if (bg || !add_proc(j, pid)) {
732 			free(j->comline);
733 			j->comline = NULL;
734 			return 0;
735 		} else {
736 			paralimit--;
737 			return 1;
738 		}
739 	}
740 	if (bg)
741 		setsid();
742 
743 	/* Set up the environment and run the command */
744 	pwd = NULL;
745 	lcap = NULL;
746 	if ((clean || username) && injail && sjuser &&
747 	    get_user_info(j, username, &pwd, &lcap) < 0)
748 		exit(1);
749 	if (injail) {
750 		/* jail_attach won't chdir along with its chroot. */
751 		path = string_param(j->intparams[KP_PATH]);
752 		if (path && chdir(path) < 0) {
753 			jail_warnx(j, "chdir %s: %s", path, strerror(errno));
754 			exit(1);
755 		}
756 		if (int_param(j->intparams[IP_EXEC_FIB], &fib) &&
757 		    setfib(fib) < 0) {
758 			jail_warnx(j, "setfib: %s", strerror(errno));
759 			exit(1);
760 		}
761 
762 		/*
763 		 * We wouldn't have specialized our affinity, so just setid to
764 		 * root.  We do this prior to attaching to avoid the kernel
765 		 * having to create a transient cpuset that we'll promptly
766 		 * free up with a reset to the jail's cpuset.
767 		 *
768 		 * This is just a best-effort to use as wide of mask as
769 		 * possible.
770 		 */
771 		if (setid != CPUSET_INVALID)
772 			(void)cpuset_setid(CPU_WHICH_PID, -1, setid);
773 
774 		if (jail_attach(j->jid) < 0) {
775 			jail_warnx(j, "jail_attach: %s", strerror(errno));
776 			exit(1);
777 		}
778 	}
779 	if (clean || username) {
780 		if (!(injail && sjuser) &&
781 		    get_user_info(j, username, &pwd, &lcap) < 0)
782 			exit(1);
783 		if (clean) {
784 			term = getenv("TERM");
785 			environ = &cleanenv;
786 			setenv("PATH", "/bin:/usr/bin", 0);
787 			if (term != NULL)
788 				setenv("TERM", term, 1);
789 		}
790 		if (setgid(pwd->pw_gid) < 0) {
791 			jail_warnx(j, "setgid %d: %s", pwd->pw_gid,
792 			    strerror(errno));
793 			exit(1);
794 		}
795 		if (setusercontext(lcap, pwd, pwd->pw_uid, username
796 		    ? LOGIN_SETALL & ~LOGIN_SETGROUP & ~LOGIN_SETLOGIN
797 		    : LOGIN_SETPATH | LOGIN_SETENV) < 0) {
798 			jail_warnx(j, "setusercontext %s: %s", pwd->pw_name,
799 			    strerror(errno));
800 			exit(1);
801 		}
802 		login_close(lcap);
803 		setenv("USER", pwd->pw_name, 1);
804 		setenv("HOME", pwd->pw_dir, 1);
805 		setenv("SHELL",
806 		    *pwd->pw_shell ? pwd->pw_shell : _PATH_BSHELL, 1);
807 		if (clean && username && chdir(pwd->pw_dir) < 0) {
808 			jail_warnx(j, "chdir %s: %s",
809 			    pwd->pw_dir, strerror(errno));
810 			exit(1);
811 		}
812 		endpwent();
813 	}
814 	if (!injail) {
815 		if (string_param(j->intparams[KP_JID]))
816 			setenv("JID", string_param(j->intparams[KP_JID]), 1);
817 		setenv("JNAME", string_param(j->intparams[KP_NAME]), 1);
818 
819 		path = string_param(j->intparams[KP_PATH]);
820 		setenv("JPATH", path ? path : "", 1);
821 	}
822 
823 	if (consfd != 0 && (dup2(consfd, 1) < 0 || dup2(consfd, 2) < 0)) {
824 		jail_warnx(j, "exec.consolelog: %s", strerror(errno));
825 		exit(1);
826 	}
827 	closefrom(3);
828 	execvp(argv[0], __DECONST(char *const*, argv));
829 	jail_warnx(j, "exec %s: %s", argv[0], strerror(errno));
830 	exit(1);
831 }
832 
833 /*
834  * Add a process to the hash, tied to a jail.
835  */
836 static int
add_proc(struct cfjail * j,pid_t pid)837 add_proc(struct cfjail *j, pid_t pid)
838 {
839 	struct kevent ke;
840 	struct cfjail *tj;
841 	struct phash *ph;
842 
843 	if (!kq && (kq = kqueue()) < 0)
844 		err(1, "kqueue");
845 	EV_SET(&ke, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
846 	if (kevent(kq, &ke, 1, NULL, 0, NULL) < 0) {
847 		if (errno == ESRCH)
848 			return 0;
849 		err(1, "kevent");
850 	}
851 	ph = emalloc(sizeof(struct phash));
852 	ph->j = j;
853 	ph->pid = pid;
854 	LIST_INSERT_HEAD(&phash[pid % PHASH_SIZE], ph, le);
855 	j->nprocs++;
856 	j->flags |= JF_SLEEPQ;
857 	if (j->timeout.tv_sec == 0)
858 		requeue(j, &sleeping);
859 	else {
860 		/* File the jail in the sleep queue according to its timeout. */
861 		TAILQ_REMOVE(j->queue, j, tq);
862 		TAILQ_FOREACH(tj, &sleeping, tq) {
863 			if (!tj->timeout.tv_sec ||
864 			    j->timeout.tv_sec < tj->timeout.tv_sec ||
865 			    (j->timeout.tv_sec == tj->timeout.tv_sec &&
866 			    j->timeout.tv_nsec <= tj->timeout.tv_nsec)) {
867 				TAILQ_INSERT_BEFORE(tj, j, tq);
868 				break;
869 			}
870 		}
871 		if (tj == NULL)
872 			TAILQ_INSERT_TAIL(&sleeping, j, tq);
873 		j->queue = &sleeping;
874 	}
875 	return 1;
876 }
877 
878 /*
879  * Remove any processes from the hash that correspond to a jail.
880  */
881 static void
clear_procs(struct cfjail * j)882 clear_procs(struct cfjail *j)
883 {
884 	struct kevent ke;
885 	struct phash *ph, *tph;
886 	int i;
887 
888 	j->nprocs = 0;
889 	for (i = 0; i < PHASH_SIZE; i++)
890 		LIST_FOREACH_SAFE(ph, &phash[i], le, tph)
891 			if (ph->j == j) {
892 				EV_SET(&ke, ph->pid, EVFILT_PROC, EV_DELETE,
893 				    NOTE_EXIT, 0, NULL);
894 				(void)kevent(kq, &ke, 1, NULL, 0, NULL);
895 				LIST_REMOVE(ph, le);
896 				free(ph);
897 			}
898 }
899 
900 /*
901  * Find the jail that corresponds to an exited process.
902  */
903 static struct cfjail *
find_proc(pid_t pid)904 find_proc(pid_t pid)
905 {
906 	struct cfjail *j;
907 	struct phash *ph;
908 
909 	LIST_FOREACH(ph, &phash[pid % PHASH_SIZE], le)
910 		if (ph->pid == pid) {
911 			j = ph->j;
912 			LIST_REMOVE(ph, le);
913 			free(ph);
914 			return --j->nprocs ? NULL : j;
915 		}
916 	return NULL;
917 }
918 
919 /*
920  * Send SIGTERM to all processes in a jail and wait for them to die.
921  */
922 static int
term_procs(struct cfjail * j)923 term_procs(struct cfjail *j)
924 {
925 	struct kinfo_proc *ki;
926 	int i, noted, pcnt, timeout;
927 
928 	static kvm_t *kd;
929 
930 	if (!int_param(j->intparams[IP_STOP_TIMEOUT], &timeout))
931 		timeout = DEFAULT_STOP_TIMEOUT;
932 	else if (timeout == 0)
933 		return 0;
934 
935 	if (kd == NULL) {
936 		kd = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
937 		if (kd == NULL)
938 			return 0;
939 	}
940 
941 	ki = kvm_getprocs(kd, KERN_PROC_PROC, 0, &pcnt);
942 	if (ki == NULL)
943 		return 0;
944 	noted = 0;
945 	for (i = 0; i < pcnt; i++)
946 		if (ki[i].ki_jid == j->jid &&
947 		    kill(ki[i].ki_pid, SIGTERM) == 0) {
948 			(void)add_proc(j, ki[i].ki_pid);
949 			if (verbose > 0) {
950 				if (!noted) {
951 					noted = 1;
952 					jail_note(j, "sent SIGTERM to:");
953 				}
954 				printf(" %d", ki[i].ki_pid);
955 			}
956 		}
957 	if (noted)
958 		printf("\n");
959 	if (j->nprocs > 0) {
960 		clock_gettime(CLOCK_REALTIME, &j->timeout);
961 		j->timeout.tv_sec += timeout;
962 		return 1;
963 	}
964 	return 0;
965 }
966 
967 /*
968  * Look up a user in the passwd and login.conf files.
969  */
970 static int
get_user_info(struct cfjail * j,const char * username,const struct passwd ** pwdp,login_cap_t ** lcapp)971 get_user_info(struct cfjail *j, const char *username,
972     const struct passwd **pwdp, login_cap_t **lcapp)
973 {
974 	const struct passwd *pwd;
975 
976 	errno = 0;
977 	*pwdp = pwd = username ? getpwnam(username) : getpwuid(getuid());
978 	if (pwd == NULL) {
979 		if (errno)
980 			jail_warnx(j, "getpwnam%s%s: %s", username ? " " : "",
981 			    username ? username : "", strerror(errno));
982 		else if (username)
983 			jail_warnx(j, "%s: no such user", username);
984 		else
985 			jail_warnx(j, "unknown uid %d", getuid());
986 		return -1;
987 	}
988 	*lcapp = login_getpwclass(pwd);
989 	if (*lcapp == NULL) {
990 		jail_warnx(j, "getpwclass %s: %s", pwd->pw_name,
991 		    strerror(errno));
992 		return -1;
993 	}
994 	/* Set the groups while the group file is still available */
995 	if (initgroups(pwd->pw_name, pwd->pw_gid) < 0) {
996 		jail_warnx(j, "initgroups %s: %s", pwd->pw_name,
997 		    strerror(errno));
998 		return -1;
999 	}
1000 	return 0;
1001 }
1002 
1003 /*
1004  * Make sure a mount or consolelog path is a valid absolute pathname
1005  * with no symlinks.
1006  */
1007 static int
check_path(struct cfjail * j,const char * pname,const char * path,int isfile,const char * umount_type)1008 check_path(struct cfjail *j, const char *pname, const char *path, int isfile,
1009     const char *umount_type)
1010 {
1011 	struct stat st, mpst;
1012 	struct statfs stfs;
1013 	char *tpath, *p;
1014 	const char *jailpath;
1015 	size_t jplen;
1016 
1017 	if (path[0] != '/') {
1018 		jail_warnx(j, "%s: %s: not an absolute pathname",
1019 		    pname, path);
1020 		return -1;
1021 	}
1022 	/*
1023 	 * Only check for symlinks in components below the jail's path,
1024 	 * since that's where the security risk lies.
1025 	 */
1026 	jailpath = string_param(j->intparams[KP_PATH]);
1027 	if (jailpath == NULL)
1028 		jailpath = "";
1029 	jplen = strlen(jailpath);
1030 	if (!strncmp(path, jailpath, jplen) && path[jplen] == '/') {
1031 		tpath = alloca(strlen(path) + 1);
1032 		strcpy(tpath, path);
1033 		for (p = tpath + jplen; p != NULL; ) {
1034 			p = strchr(p + 1, '/');
1035 			if (p)
1036 				*p = '\0';
1037 			if (lstat(tpath, &st) < 0) {
1038 				if (errno == ENOENT && isfile && !p)
1039 					break;
1040 				jail_warnx(j, "%s: %s: %s", pname, tpath,
1041 				    strerror(errno));
1042 				return -1;
1043 			}
1044 			if (S_ISLNK(st.st_mode)) {
1045 				jail_warnx(j, "%s: %s is a symbolic link",
1046 				    pname, tpath);
1047 				return -1;
1048 			}
1049 			if (p)
1050 				*p = '/';
1051 		}
1052 	}
1053 	if (umount_type != NULL) {
1054 		if (stat(path, &st) < 0 || statfs(path, &stfs) < 0) {
1055 			jail_warnx(j, "%s: %s: %s", pname, path,
1056 			    strerror(errno));
1057 			return -1;
1058 		}
1059 		if (stat(stfs.f_mntonname, &mpst) < 0) {
1060 			jail_warnx(j, "%s: %s: %s", pname, stfs.f_mntonname,
1061 			    strerror(errno));
1062 			return -1;
1063 		}
1064 		if (st.st_ino != mpst.st_ino) {
1065 			jail_warnx(j, "%s: %s: not a mount point",
1066 			    pname, path);
1067 			return -1;
1068 		}
1069 		if (strcmp(stfs.f_fstypename, umount_type)) {
1070 			jail_warnx(j, "%s: %s: not a %s mount",
1071 			    pname, path, umount_type);
1072 			return -1;
1073 		}
1074 	}
1075 	return 0;
1076 }
1077