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