xref: /freebsd/sys/kern/kern_jail.c (revision 490365e5d076db13e9fed085bbc5b649ad261fd1)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 1999 Poul-Henning Kamp.
5  * Copyright (c) 2008 Bjoern A. Zeeb.
6  * Copyright (c) 2009 James Gritton.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 #include "opt_ddb.h"
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #include "opt_nfs.h"
36 
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/ctype.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/file.h>
44 #include <sys/sysproto.h>
45 #include <sys/malloc.h>
46 #include <sys/osd.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/epoch.h>
50 #include <sys/event.h>
51 #include <sys/taskqueue.h>
52 #include <sys/fcntl.h>
53 #include <sys/jail.h>
54 #include <sys/jaildesc.h>
55 #include <sys/linker.h>
56 #include <sys/lock.h>
57 #include <sys/mman.h>
58 #include <sys/mutex.h>
59 #include <sys/racct.h>
60 #include <sys/rctl.h>
61 #include <sys/refcount.h>
62 #include <sys/sx.h>
63 #include <sys/sysent.h>
64 #include <sys/namei.h>
65 #include <sys/mount.h>
66 #include <sys/queue.h>
67 #include <sys/socket.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
70 #include <sys/uuid.h>
71 #include <sys/vnode.h>
72 
73 #include <net/if.h>
74 #include <net/vnet.h>
75 
76 #include <netinet/in.h>
77 
78 #ifdef DDB
79 #include <ddb/ddb.h>
80 #endif /* DDB */
81 
82 #include <security/mac/mac_framework.h>
83 #include <security/mac/mac_policy.h>
84 #include <security/mac/mac_syscalls.h>
85 
86 #define	PRISON0_HOSTUUID_MODULE	"hostuuid"
87 
88 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
89 #ifdef RACCT
90 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
91 #endif
92 
93 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
94 #ifdef INET
95 #ifdef INET6
96 #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
97 #else
98 #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
99 #endif
100 #else /* !INET */
101 #ifdef INET6
102 #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
103 #else
104 #define	_PR_IP_SADDRSEL	0
105 #endif
106 #endif
107 
108 /* prison0 describes what is "real" about the system. */
109 struct prison prison0 = {
110 	.pr_id		= 0,
111 	.pr_name	= "0",
112 	.pr_ref		= 1,
113 	.pr_uref	= 1,
114 	.pr_path	= "/",
115 	.pr_securelevel	= -1,
116 	.pr_devfs_rsnum = 0,
117 	.pr_state	= PRISON_STATE_ALIVE,
118 	.pr_childmax	= JAIL_MAX,
119 	.pr_hostuuid	= DEFAULT_HOSTUUID,
120 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
121 #ifdef VIMAGE
122 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
123 #else
124 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
125 #endif
126 	.pr_allow	= PR_ALLOW_PRISON0,
127 };
128 _Static_assert((PR_ALLOW_PRISON0 & ~PR_ALLOW_ALL_STATIC) == 0,
129     "Bits enabled in PR_ALLOW_PRISON0 that are not statically reserved");
130 
131 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
132 
133 struct bool_flags {
134 	const char	*name;
135 	const char	*noname;
136 	volatile u_int	 flag;
137 };
138 struct jailsys_flags {
139 	const char	*name;
140 	unsigned	 disable;
141 	unsigned	 new;
142 };
143 
144 /*
145  * Handle jail teardown in a dedicated thread to avoid deadlocks from
146  * vnet_destroy().
147  */
148 TASKQUEUE_DEFINE_THREAD(jail_remove);
149 
150 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
151 struct	sx allprison_lock;
152 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
153 struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
154 LIST_HEAD(, prison_racct) allprison_racct;
155 int	lastprid = 0;
156 int	lastdeadid = 0;
157 
158 static int get_next_prid(struct prison **insprp);
159 static int get_next_deadid(struct prison **insprp);
160 static int do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp);
161 static int prison_attach_thread_single(struct thread *td);
162 static void prison_attach_thread_single_end(struct thread *td);
163 static void prison_complete(void *context, int pending);
164 static void prison_deref(struct prison *pr, int flags);
165 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
166 static int prison_lock_xlock(struct prison *pr, int flags);
167 static void prison_cleanup_locked(struct prison *pr);
168 static void prison_cleanup_unlocked(struct prison *pr);
169 static void prison_free_not_last(struct prison *pr);
170 static void prison_proc_free_not_last(struct prison *pr);
171 static void prison_proc_relink(struct prison *opr, struct prison *npr,
172     struct proc *p);
173 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
174     int enable);
175 static char *prison_path(struct prison *pr1, struct prison *pr2);
176 #ifdef RACCT
177 static void prison_racct_attach(struct prison *pr);
178 static void prison_racct_modify(struct prison *pr);
179 static void prison_racct_detach(struct prison *pr);
180 #endif
181 static void prison_knote(struct prison *pr, long hint);
182 
183 /* Flags for prison_deref */
184 #define	PD_DEREF	0x01	/* Decrement pr_ref */
185 #define	PD_DEUREF	0x02	/* Decrement pr_uref */
186 #define	PD_KILL		0x04	/* Remove jail, kill processes, etc */
187 #define	PD_LOCKED	0x10	/* pr_mtx is held */
188 #define	PD_LIST_SLOCKED	0x20	/* allprison_lock is held shared */
189 #define	PD_LIST_XLOCKED	0x40	/* allprison_lock is held exclusive */
190 #define PD_OP_FLAGS	0x07	/* Operation flags */
191 #define PD_LOCK_FLAGS	0x70	/* Lock status flags */
192 
193 /*
194  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
195  * as we cannot figure out the size of a sparse array, or an array without a
196  * terminating entry.
197  */
198 static struct bool_flags pr_flag_bool[] = {
199 	{"persist", "nopersist", PR_PERSIST},
200 #ifdef INET
201 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
202 #endif
203 #ifdef INET6
204 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
205 #endif
206 };
207 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
208 
209 static struct jailsys_flags pr_flag_jailsys[] = {
210 	{"host", 0, PR_HOST},
211 #ifdef VIMAGE
212 	{"vnet", 0, PR_VNET},
213 #endif
214 #ifdef INET
215 	{"ip4", PR_IP4_USER, PR_IP4_USER},
216 #endif
217 #ifdef INET6
218 	{"ip6", PR_IP6_USER, PR_IP6_USER},
219 #endif
220 };
221 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
222 
223 /*
224  * Make this array full-size so dynamic parameters can be added.
225  * It is protected by prison0.mtx, but lockless reading is allowed
226  * with an atomic check of the flag values.
227  */
228 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
229 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
230 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
231 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
232 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
233 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
234 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
235 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
236 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
237 	{"allow.reserved_ports", "allow.noreserved_ports",
238 	 PR_ALLOW_RESERVED_PORTS},
239 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
240 	{"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
241 	 PR_ALLOW_UNPRIV_DEBUG},
242 	{"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
243 #ifdef VIMAGE
244 	{"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD},
245 #endif
246 	{"allow.extattr", "allow.noextattr", PR_ALLOW_EXTATTR},
247 	{"allow.adjtime", "allow.noadjtime", PR_ALLOW_ADJTIME},
248 	{"allow.settime", "allow.nosettime", PR_ALLOW_SETTIME},
249 	{"allow.routing", "allow.norouting", PR_ALLOW_ROUTING},
250 	{"allow.unprivileged_parent_tampering",
251 	    "allow.nounprivileged_parent_tampering",
252 	    PR_ALLOW_UNPRIV_PARENT_TAMPER},
253 #ifdef AUDIT
254 	{"allow.setaudit", "allow.nosetaudit", PR_ALLOW_SETAUDIT},
255 #endif
256 };
257 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
258 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
259 
260 #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | \
261 					 PR_ALLOW_RESERVED_PORTS | \
262 					 PR_ALLOW_UNPRIV_DEBUG | \
263 					 PR_ALLOW_SUSER)
264 #define	JAIL_DEFAULT_ENFORCE_STATFS	2
265 #define	JAIL_DEFAULT_DEVFS_RSNUM	0
266 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
267 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
268 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
269 #if defined(INET) || defined(INET6)
270 static unsigned jail_max_af_ips = 255;
271 #endif
272 
273 /*
274  * Initialize the parts of prison0 that can't be static-initialized with
275  * constants.  This is called from proc0_init() after creating thread0 cpuset.
276  */
277 void
prison0_init(void)278 prison0_init(void)
279 {
280 	uint8_t *file, *data;
281 	size_t size;
282 	char buf[sizeof(prison0.pr_hostuuid)];
283 #ifdef MAC
284 	int error __diagused;
285 #endif
286 	bool valid;
287 
288 #ifdef MAC
289 	error = mac_prison_init(&prison0, M_WAITOK);
290 	MPASS(error == 0);
291 
292 	mtx_unlock(&prison0.pr_mtx);
293 #endif
294 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
295 	prison0.pr_osreldate = osreldate;
296 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
297 
298 	/* If we have a preloaded hostuuid, use it. */
299 	file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
300 	if (file != NULL) {
301 		data = preload_fetch_addr(file);
302 		size = preload_fetch_size(file);
303 		if (data != NULL) {
304 			/*
305 			 * The preloaded data may include trailing whitespace, almost
306 			 * certainly a newline; skip over any whitespace or
307 			 * non-printable characters to be safe.
308 			 */
309 			while (size > 0 && data[size - 1] <= 0x20) {
310 				size--;
311 			}
312 
313 			valid = false;
314 
315 			/*
316 			 * Not NUL-terminated when passed from loader, but
317 			 * validate_uuid requires that due to using sscanf (as
318 			 * does the subsequent strlcpy, since it still reads
319 			 * past the given size to return the true length);
320 			 * bounce to a temporary buffer to fix.
321 			 */
322 			if (size >= sizeof(buf))
323 				goto done;
324 
325 			memcpy(buf, data, size);
326 			buf[size] = '\0';
327 
328 			if (validate_uuid(buf, size, NULL, 0) != 0)
329 				goto done;
330 
331 			valid = true;
332 			(void)strlcpy(prison0.pr_hostuuid, buf,
333 			    sizeof(prison0.pr_hostuuid));
334 
335 done:
336 			if (bootverbose && !valid) {
337 				printf("hostuuid: preload data malformed: '%.*s'\n",
338 				    (int)size, data);
339 			}
340 		}
341 	}
342 	if (bootverbose)
343 		printf("hostuuid: using %s\n", prison0.pr_hostuuid);
344 }
345 
346 /*
347  * struct jail_args {
348  *	struct jail *jail;
349  * };
350  */
351 int
sys_jail(struct thread * td,struct jail_args * uap)352 sys_jail(struct thread *td, struct jail_args *uap)
353 {
354 	uint32_t version;
355 	int error;
356 	struct jail j;
357 
358 	error = copyin(uap->jail, &version, sizeof(uint32_t));
359 	if (error)
360 		return (error);
361 
362 	switch (version) {
363 	case 0:
364 	{
365 		struct jail_v0 j0;
366 
367 		/* FreeBSD single IPv4 jails. */
368 		bzero(&j, sizeof(struct jail));
369 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
370 		if (error)
371 			return (error);
372 		j.version = j0.version;
373 		j.path = j0.path;
374 		j.hostname = j0.hostname;
375 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
376 		break;
377 	}
378 
379 	case 1:
380 		/*
381 		 * Version 1 was used by multi-IPv4 jail implementations
382 		 * that never made it into the official kernel.
383 		 */
384 		return (EINVAL);
385 
386 	case 2:	/* JAIL_API_VERSION */
387 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
388 		error = copyin(uap->jail, &j, sizeof(struct jail));
389 		if (error)
390 			return (error);
391 		break;
392 
393 	default:
394 		/* Sci-Fi jails are not supported, sorry. */
395 		return (EINVAL);
396 	}
397 	return (kern_jail(td, &j));
398 }
399 
400 int
kern_jail(struct thread * td,struct jail * j)401 kern_jail(struct thread *td, struct jail *j)
402 {
403 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
404 #ifdef INET
405 			    + 1
406 #endif
407 #ifdef INET6
408 			    + 1
409 #endif
410 			    )];
411 	struct uio opt;
412 	char *u_path, *u_hostname, *u_name;
413 	struct bool_flags *bf;
414 #ifdef INET
415 	uint32_t ip4s;
416 	struct in_addr *u_ip4;
417 #endif
418 #ifdef INET6
419 	struct in6_addr *u_ip6;
420 #endif
421 	size_t tmplen;
422 	int error, enforce_statfs;
423 
424 	bzero(&optiov, sizeof(optiov));
425 	opt.uio_iov = optiov;
426 	opt.uio_iovcnt = 0;
427 	opt.uio_offset = -1;
428 	opt.uio_resid = -1;
429 	opt.uio_segflg = UIO_SYSSPACE;
430 	opt.uio_rw = UIO_READ;
431 	opt.uio_td = td;
432 
433 	/* Set permissions for top-level jails from sysctls. */
434 	if (!jailed(td->td_ucred)) {
435 		for (bf = pr_flag_allow;
436 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
437 			atomic_load_int(&bf->flag) != 0;
438 		     bf++) {
439 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
440 			    (jail_default_allow & bf->flag)
441 			    ? bf->name : bf->noname);
442 			optiov[opt.uio_iovcnt].iov_len =
443 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
444 			opt.uio_iovcnt += 2;
445 		}
446 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
447 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
448 		opt.uio_iovcnt++;
449 		enforce_statfs = jail_default_enforce_statfs;
450 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
451 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
452 		opt.uio_iovcnt++;
453 	}
454 
455 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
456 #ifdef INET
457 	ip4s = (j->version == 0) ? 1 : j->ip4s;
458 	if (ip4s > jail_max_af_ips)
459 		return (EINVAL);
460 	tmplen += ip4s * sizeof(struct in_addr);
461 #else
462 	if (j->ip4s > 0)
463 		return (EINVAL);
464 #endif
465 #ifdef INET6
466 	if (j->ip6s > jail_max_af_ips)
467 		return (EINVAL);
468 	tmplen += j->ip6s * sizeof(struct in6_addr);
469 #else
470 	if (j->ip6s > 0)
471 		return (EINVAL);
472 #endif
473 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
474 	u_hostname = u_path + MAXPATHLEN;
475 	u_name = u_hostname + MAXHOSTNAMELEN;
476 #ifdef INET
477 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
478 #endif
479 #ifdef INET6
480 #ifdef INET
481 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
482 #else
483 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
484 #endif
485 #endif
486 	optiov[opt.uio_iovcnt].iov_base = "path";
487 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
488 	opt.uio_iovcnt++;
489 	optiov[opt.uio_iovcnt].iov_base = u_path;
490 	error = copyinstr(j->path, u_path, MAXPATHLEN,
491 	    &optiov[opt.uio_iovcnt].iov_len);
492 	if (error) {
493 		free(u_path, M_TEMP);
494 		return (error);
495 	}
496 	opt.uio_iovcnt++;
497 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
498 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
499 	opt.uio_iovcnt++;
500 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
501 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
502 	    &optiov[opt.uio_iovcnt].iov_len);
503 	if (error) {
504 		free(u_path, M_TEMP);
505 		return (error);
506 	}
507 	opt.uio_iovcnt++;
508 	if (j->jailname != NULL) {
509 		optiov[opt.uio_iovcnt].iov_base = "name";
510 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
511 		opt.uio_iovcnt++;
512 		optiov[opt.uio_iovcnt].iov_base = u_name;
513 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
514 		    &optiov[opt.uio_iovcnt].iov_len);
515 		if (error) {
516 			free(u_path, M_TEMP);
517 			return (error);
518 		}
519 		opt.uio_iovcnt++;
520 	}
521 #ifdef INET
522 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
523 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
524 	opt.uio_iovcnt++;
525 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
526 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
527 	if (j->version == 0)
528 		u_ip4->s_addr = j->ip4s;
529 	else {
530 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
531 		if (error) {
532 			free(u_path, M_TEMP);
533 			return (error);
534 		}
535 	}
536 	opt.uio_iovcnt++;
537 #endif
538 #ifdef INET6
539 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
540 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
541 	opt.uio_iovcnt++;
542 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
543 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
544 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
545 	if (error) {
546 		free(u_path, M_TEMP);
547 		return (error);
548 	}
549 	opt.uio_iovcnt++;
550 #endif
551 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
552 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
553 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
554 	free(u_path, M_TEMP);
555 	return (error);
556 }
557 
558 /*
559  * struct jail_set_args {
560  *	struct iovec *iovp;
561  *	unsigned int iovcnt;
562  *	int flags;
563  * };
564  */
565 int
sys_jail_set(struct thread * td,struct jail_set_args * uap)566 sys_jail_set(struct thread *td, struct jail_set_args *uap)
567 {
568 	struct uio *auio;
569 	int error;
570 
571 	/* Check that we have an even number of iovecs. */
572 	if (uap->iovcnt & 1)
573 		return (EINVAL);
574 
575 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
576 	if (error)
577 		return (error);
578 	error = kern_jail_set(td, auio, uap->flags);
579 	freeuio(auio);
580 	return (error);
581 }
582 
583 #if defined(INET) || defined(INET6)
584 typedef int prison_addr_cmp_t(const void *, const void *);
585 typedef bool prison_addr_valid_t(const void *);
586 static const struct pr_family {
587 	size_t			size;
588 	prison_addr_cmp_t	*cmp;
589 	prison_addr_valid_t	*valid;
590 	int			ip_flag;
591 } pr_families[PR_FAMILY_MAX] = {
592 #ifdef INET
593 	[PR_INET] = {
594 		.size = sizeof(struct in_addr),
595 		.cmp = prison_qcmp_v4,
596 		.valid = prison_valid_v4,
597 		.ip_flag = PR_IP4_USER,
598 	 },
599 #endif
600 #ifdef INET6
601 	[PR_INET6] = {
602 		.size = sizeof(struct in6_addr),
603 		.cmp = prison_qcmp_v6,
604 		.valid = prison_valid_v6,
605 		.ip_flag = PR_IP6_USER,
606 	},
607 #endif
608 };
609 
610 /*
611  * Network address lists (pr_addrs) allocation for jails.  The addresses
612  * are accessed locklessly by the network stack, thus need to be protected by
613  * the network epoch.
614  */
615 struct prison_ip {
616 	struct epoch_context ctx;
617 	uint32_t	ips;
618 #ifdef FUTURE_C
619 	/*
620 	 * XXX Variable-length automatic arrays in union may be
621 	 * supported in future C.
622 	 */
623 	union {
624 		char pr_ip[];
625 		struct in_addr pr_ip4[];
626 		struct in6_addr pr_ip6[];
627 	};
628 #else /* No future C :( */
629 	char pr_ip[];
630 #endif
631 };
632 
633 static char *
PR_IP(struct prison_ip * pip,const pr_family_t af,int idx)634 PR_IP(struct prison_ip *pip, const pr_family_t af, int idx)
635 {
636 	MPASS(pip);
637 	MPASS(af < PR_FAMILY_MAX);
638 	MPASS(idx >= 0 && idx < pip->ips);
639 
640 	return (pip->pr_ip + pr_families[af].size * idx);
641 }
642 
643 static struct prison_ip *
prison_ip_alloc(const pr_family_t af,uint32_t cnt,int flags)644 prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
645 {
646 	struct prison_ip *pip;
647 
648 	pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
649 	    M_PRISON, flags);
650 	if (pip != NULL)
651 		pip->ips = cnt;
652 	return (pip);
653 }
654 
655 /*
656  * Allocate and copyin user supplied address list, sorting and validating.
657  * kern_jail_set() helper.
658  */
659 static struct prison_ip *
prison_ip_copyin(const pr_family_t af,void * op,uint32_t cnt)660 prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
661 {
662 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
663 	const size_t size = pr_families[af].size;
664 	struct prison_ip *pip;
665 
666 	pip = prison_ip_alloc(af, cnt, M_WAITOK);
667 	bcopy(op, pip->pr_ip, cnt * size);
668 	/*
669 	 * IP addresses are all sorted but ip[0] to preserve
670 	 * the primary IP address as given from userland.
671 	 * This special IP is used for unbound outgoing
672 	 * connections as well for "loopback" traffic in case
673 	 * source address selection cannot find any more fitting
674 	 * address to connect from.
675 	 */
676 	if (cnt > 1)
677 		qsort(PR_IP(pip, af, 1), cnt - 1, size, cmp);
678 	/*
679 	 * Check for duplicate addresses and do some simple
680 	 * zero and broadcast checks. If users give other bogus
681 	 * addresses it is their problem.
682 	 */
683 	for (int i = 0; i < cnt; i++) {
684 		if (!pr_families[af].valid(PR_IP(pip, af, i))) {
685 			free(pip, M_PRISON);
686 			return (NULL);
687 		}
688 		if (i + 1 < cnt &&
689 		    (cmp(PR_IP(pip, af, 0), PR_IP(pip, af, i + 1)) == 0 ||
690 		     cmp(PR_IP(pip, af, i), PR_IP(pip, af, i + 1)) == 0)) {
691 			free(pip, M_PRISON);
692 			return (NULL);
693 		}
694 	}
695 
696 	return (pip);
697 }
698 
699 /*
700  * Allocate and dup parent prison address list.
701  * kern_jail_set() helper.
702  */
703 static void
prison_ip_dup(struct prison * ppr,struct prison * pr,const pr_family_t af)704 prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
705 {
706 	const struct prison_ip *ppip = ppr->pr_addrs[af];
707 	struct prison_ip *pip;
708 
709 	if (ppip != NULL) {
710 		pip = prison_ip_alloc(af, ppip->ips, M_WAITOK);
711 		bcopy(ppip->pr_ip, pip->pr_ip, pip->ips * pr_families[af].size);
712 		pr->pr_addrs[af] = pip;
713 	}
714 }
715 
716 /*
717  * Make sure the new set of IP addresses is a subset of the parent's list.
718  * Don't worry about the parent being unlocked, as any setting is done with
719  * allprison_lock held.
720  * kern_jail_set() helper.
721  */
722 static bool
prison_ip_parent_match(struct prison_ip * ppip,struct prison_ip * pip,const pr_family_t af)723 prison_ip_parent_match(struct prison_ip *ppip, struct prison_ip *pip,
724     const pr_family_t af)
725 {
726 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
727 	int i, j;
728 
729 	if (ppip == NULL)
730 		return (false);
731 
732 	for (i = 0; i < ppip->ips; i++)
733 		if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, i)) == 0)
734 			break;
735 
736 	if (i == ppip->ips)
737 		/* Main address not present in parent. */
738 		return (false);
739 
740 	if (pip->ips > 1) {
741 		for (i = j = 1; i < pip->ips; i++) {
742 			if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0)
743 				/* Equals to parent primary address. */
744 				continue;
745 			for (; j < ppip->ips; j++)
746 				if (cmp(PR_IP(pip, af, i),
747 				    PR_IP(ppip, af, j)) == 0)
748 					break;
749 			if (j == ppip->ips)
750 				break;
751 		}
752 		if (j == ppip->ips)
753 			/* Address not present in parent. */
754 			return (false);
755 	}
756 	return (true);
757 }
758 
759 /*
760  * Check for conflicting IP addresses.  We permit them if there is no more
761  * than one IP on each jail.  If there is a duplicate on a jail with more
762  * than one IP stop checking and return error.
763  * kern_jail_set() helper.
764  */
765 static bool
prison_ip_conflict_check(const struct prison * ppr,const struct prison * pr,struct prison_ip * pip,pr_family_t af)766 prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
767     struct prison_ip *pip, pr_family_t af)
768 {
769 	const struct prison *tppr, *tpr;
770 	int descend;
771 
772 #ifdef VIMAGE
773 	for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
774 		if (tppr->pr_flags & PR_VNET)
775 			break;
776 #else
777 	tppr = &prison0;
778 #endif
779 	FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
780 		if (tpr == pr ||
781 #ifdef VIMAGE
782 		    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
783 #endif
784 		    !prison_isalive(tpr)) {
785 			descend = 0;
786 			continue;
787 		}
788 		if (!(tpr->pr_flags & pr_families[af].ip_flag))
789 			continue;
790 		descend = 0;
791 		if (tpr->pr_addrs[af] == NULL ||
792 		    (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
793 			continue;
794 		for (int i = 0; i < pip->ips; i++)
795 			if (prison_ip_check(tpr, af, PR_IP(pip, af, i)) == 0)
796 				return (false);
797 	}
798 
799 	return (true);
800 }
801 
802 _Static_assert(offsetof(struct prison_ip, ctx) == 0,
803     "prison must start with epoch context");
804 static void
prison_ip_free_deferred(epoch_context_t ctx)805 prison_ip_free_deferred(epoch_context_t ctx)
806 {
807 
808 	free(ctx, M_PRISON);
809 }
810 
811 static void
prison_ip_free(struct prison_ip * pip)812 prison_ip_free(struct prison_ip *pip)
813 {
814 
815 	if (pip != NULL)
816 		NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
817 }
818 
819 static void
prison_ip_set(struct prison * pr,const pr_family_t af,struct prison_ip * new)820 prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
821 {
822 	struct prison_ip **mem, *old;
823 
824 	mtx_assert(&pr->pr_mtx, MA_OWNED);
825 
826 	mem = &pr->pr_addrs[af];
827 
828 	old = *mem;
829 	atomic_store_ptr(mem, new);
830 	prison_ip_free(old);
831 }
832 
833 /*
834  * Restrict a prison's IP address list with its parent's, possibly replacing
835  * it.  Return true if succeed, otherwise should redo.
836  * kern_jail_set() helper.
837  */
838 static bool
prison_ip_restrict(struct prison * pr,const pr_family_t af,struct prison_ip ** newp)839 prison_ip_restrict(struct prison *pr, const pr_family_t af,
840     struct prison_ip **newp)
841 {
842 	struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
843 	struct prison_ip *pip = pr->pr_addrs[af];
844 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
845 	const size_t size = pr_families[af].size;
846 	struct prison_ip *new = newp != NULL ? *newp : NULL;
847 	uint32_t ips;
848 
849 	mtx_assert(&pr->pr_mtx, MA_OWNED);
850 
851 	/*
852 	 * Due to epoch-synchronized access to the IP address lists we always
853 	 * allocate a new list even if the old one has enough space.  We could
854 	 * atomically update an IPv4 address inside a list, but that would
855 	 * screw up sorting, and in case of IPv6 we can't even atomically write
856 	 * one.
857 	 */
858 	if (ppip == NULL) {
859 		if (pip != NULL)
860 			prison_ip_set(pr, af, NULL);
861 		return (true);
862 	}
863 
864 	if (!(pr->pr_flags & pr_families[af].ip_flag)) {
865 		if (new == NULL) {
866 			new = prison_ip_alloc(af, ppip->ips, M_NOWAIT);
867 			if (new == NULL)
868 				return (false); /* Redo */
869 		}
870 		/* This has no user settings, so just copy the parent's list. */
871 		MPASS(new->ips == ppip->ips);
872 		bcopy(ppip->pr_ip, new->pr_ip, ppip->ips * size);
873 		prison_ip_set(pr, af, new);
874 		if (newp != NULL)
875 			*newp = NULL; /* Used */
876 	} else if (pip != NULL) {
877 		/* Remove addresses that aren't in the parent. */
878 		int i;
879 
880 		i = 0; /* index in pip */
881 		ips = 0; /* index in new */
882 
883 		if (new == NULL) {
884 			new = prison_ip_alloc(af, pip->ips, M_NOWAIT);
885 			if (new == NULL)
886 				return (false); /* Redo */
887 		}
888 
889 		for (int pi = 0; pi < ppip->ips; pi++)
890 			if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, pi)) == 0) {
891 				/* Found our primary address in parent. */
892 				bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
893 				    size);
894 				i++;
895 				ips++;
896 				break;
897 			}
898 		for (int pi = 1; i < pip->ips; ) {
899 			/* Check against primary, which is unsorted. */
900 			if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) {
901 				/* Matches parent's primary address. */
902 				bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
903 				    size);
904 				i++;
905 				ips++;
906 				continue;
907 			}
908 			/* The rest are sorted. */
909 			switch (pi >= ppip->ips ? -1 :
910 				cmp(PR_IP(pip, af, i), PR_IP(ppip, af, pi))) {
911 			case -1:
912 				i++;
913 				break;
914 			case 0:
915 				bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
916 				    size);
917 				i++;
918 				pi++;
919 				ips++;
920 				break;
921 			case 1:
922 				pi++;
923 				break;
924 			}
925 		}
926 		if (ips == 0) {
927 			if (newp == NULL || *newp == NULL)
928 				prison_ip_free(new);
929 			new = NULL;
930 		} else {
931 			/* Shrink to real size */
932 			KASSERT((new->ips >= ips),
933 			    ("Out-of-bounds write to prison_ip %p", new));
934 			new->ips = ips;
935 		}
936 		prison_ip_set(pr, af, new);
937 		if (newp != NULL)
938 			*newp = NULL; /* Used */
939 	}
940 	return (true);
941 }
942 
943 /*
944  * Fast-path check if an address belongs to a prison.
945  */
946 int
prison_ip_check(const struct prison * pr,const pr_family_t af,const void * addr)947 prison_ip_check(const struct prison *pr, const pr_family_t af,
948     const void *addr)
949 {
950 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
951 	struct prison_ip *pip;
952 	int i, a, z, d;
953 
954 	MPASS(mtx_owned(&pr->pr_mtx) ||
955 	    in_epoch(net_epoch_preempt) ||
956 	    sx_xlocked(&allprison_lock));
957 
958 	pip = atomic_load_ptr(&pr->pr_addrs[af]);
959 	if (__predict_false(pip == NULL))
960 		return (EAFNOSUPPORT);
961 
962 	/* Check the primary IP. */
963 	if (cmp(PR_IP(pip, af, 0), addr) == 0)
964 		return (0);
965 
966 	/*
967 	 * All the other IPs are sorted so we can do a binary search.
968 	 */
969 	a = 0;
970 	z = pip->ips - 2;
971 	while (a <= z) {
972 		i = (a + z) / 2;
973 		d = cmp(PR_IP(pip, af, i + 1), addr);
974 		if (d > 0)
975 			z = i - 1;
976 		else if (d < 0)
977 			a = i + 1;
978 		else
979 			return (0);
980 	}
981 
982 	return (EADDRNOTAVAIL);
983 }
984 
985 /*
986  * Grab primary IP.  Historically required mutex, but nothing prevents
987  * us to support epoch-protected access.  Is it used in fast path?
988  * in{6}_jail.c helper
989  */
990 const void *
prison_ip_get0(const struct prison * pr,const pr_family_t af)991 prison_ip_get0(const struct prison *pr, const pr_family_t af)
992 {
993 	const struct prison_ip *pip = pr->pr_addrs[af];
994 
995 	mtx_assert(&pr->pr_mtx, MA_OWNED);
996 	MPASS(pip);
997 
998 	return (pip->pr_ip);
999 }
1000 
1001 u_int
prison_ip_cnt(const struct prison * pr,const pr_family_t af)1002 prison_ip_cnt(const struct prison *pr, const pr_family_t af)
1003 {
1004 
1005 	return (pr->pr_addrs[af]->ips);
1006 }
1007 #endif	/* defined(INET) || defined(INET6) */
1008 
1009 int
kern_jail_set(struct thread * td,struct uio * optuio,int flags)1010 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
1011 {
1012 	struct file *jfp_out;
1013 	struct nameidata nd;
1014 #ifdef INET
1015 	struct prison_ip *ip4;
1016 #endif
1017 #ifdef INET6
1018 	struct prison_ip *ip6;
1019 #endif
1020 	struct vfsopt *opt;
1021 	struct vfsoptlist *opts;
1022 	struct prison *pr, *deadpr, *dinspr, *inspr, *mypr, *ppr, *tpr;
1023 	struct ucred *jdcred;
1024 	struct vnode *root;
1025 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
1026 	char *g_path, *osrelstr;
1027 	struct bool_flags *bf;
1028 	struct jailsys_flags *jsf;
1029 #if defined(INET) || defined(INET6)
1030 	void *op;
1031 #endif
1032 	unsigned long hid;
1033 	size_t namelen, onamelen, pnamelen;
1034 #ifdef MAC
1035 	void *mac_set_prison_data = NULL;
1036 	int gotmaclabel;
1037 #endif
1038 	int created, cuflags, descend, drflags, enforce;
1039 	int error, errmsg_len, errmsg_pos;
1040 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
1041 	int deadid, jfd_in, jfd_out, jfd_pos, jid, jsys, len, level;
1042 	int childmax, osreldt, rsnum, slevel;
1043 #ifdef INET
1044 	int ip4s;
1045 	bool redo_ip4;
1046 #endif
1047 #ifdef INET6
1048 	int ip6s;
1049 	bool redo_ip6;
1050 #endif
1051 	bool maybe_changed;
1052 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
1053 	uint64_t pr_allow_diff;
1054 	unsigned tallow;
1055 	char numbuf[12];
1056 
1057 	mypr = td->td_ucred->cr_prison;
1058 	if (((flags & (JAIL_CREATE | JAIL_AT_DESC)) == JAIL_CREATE) &&
1059 	    mypr->pr_childmax == 0)
1060 		return (EPERM);
1061 	if (flags & ~JAIL_SET_MASK)
1062 		return (EINVAL);
1063 	if ((flags & (JAIL_USE_DESC | JAIL_AT_DESC)) ==
1064 	    (JAIL_USE_DESC | JAIL_AT_DESC))
1065 		return (EINVAL);
1066 
1067 	/* Only let a single thread in the process try to attach at a time. */
1068 	if (flags & JAIL_ATTACH) {
1069 		error = prison_attach_thread_single(td);
1070 		if (error != 0)
1071 			return (error);
1072 	}
1073 
1074 #ifdef INET
1075 	ip4 = NULL;
1076 #endif
1077 #ifdef INET6
1078 	ip6 = NULL;
1079 #endif
1080 	g_path = NULL;
1081 	jfp_out = NULL;
1082 	jfd_out = -1;
1083 	prison_hold(mypr);
1084 	/*
1085 	 * Check all the parameters before committing to anything.  Not all
1086 	 * errors can be caught early, but we may as well try.  Also, this
1087 	 * takes care of some expensive stuff (path lookup) before getting
1088 	 * the allprison lock.
1089 	 *
1090 	 * XXX Jails are not filesystems, and jail parameters are not mount
1091 	 *     options.  But it makes more sense to re-use the vfsopt code
1092 	 *     than duplicate it under a different name.
1093 	 */
1094 	error = vfs_buildopts(optuio, &opts);
1095 	if (error) {
1096 		opts = NULL;
1097 		goto done_free;
1098 	}
1099 
1100 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1101 	if (!cuflags) {
1102 		error = EINVAL;
1103 		vfs_opterror(opts, "no valid operation (create or update)");
1104 		goto done_errmsg;
1105 	}
1106 
1107 	error = vfs_copyopt(opts, "desc", &jfd_in, sizeof(jfd_in));
1108 	if (error == ENOENT) {
1109 		if (flags & (JAIL_USE_DESC | JAIL_AT_DESC | JAIL_GET_DESC |
1110 		    JAIL_OWN_DESC)) {
1111 			vfs_opterror(opts, "missing desc");
1112 			goto done_errmsg;
1113 		}
1114 		jfd_in = -1;
1115 	} else if (error != 0)
1116 		goto done_free;
1117 	else {
1118 		if (!(flags & (JAIL_USE_DESC | JAIL_AT_DESC | JAIL_GET_DESC |
1119 		    JAIL_OWN_DESC))) {
1120 			error = EINVAL;
1121 			vfs_opterror(opts, "unexpected desc");
1122 			goto done_errmsg;
1123 		}
1124 		if (flags & JAIL_AT_DESC) {
1125 			/*
1126 			 * Look up and create jails based on the
1127 			 * descriptor's prison.
1128 			 */
1129 			struct prison *jdpr;
1130 
1131 			error = jaildesc_find(td, jfd_in, &jdpr, NULL);
1132 			if (error != 0) {
1133 				vfs_opterror(opts, error == ENOENT ?
1134 				    "descriptor to dead jail" :
1135 				    "not a jail descriptor");
1136 				goto done_errmsg;
1137 			}
1138 			prison_free(mypr);
1139 			mypr = jdpr;
1140 			if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) {
1141 				error = EPERM;
1142 				goto done_free;
1143 			}
1144 		}
1145 		if (flags & (JAIL_GET_DESC | JAIL_OWN_DESC)) {
1146 			/* Allocate a jail descriptor to return later. */
1147 			error = jaildesc_alloc(td, &jfp_out, &jfd_out,
1148 				flags & JAIL_OWN_DESC);
1149 			if (error)
1150 				goto done_free;
1151 		}
1152 	}
1153 
1154 	/*
1155 	 * Delay the permission check if using a jail descriptor,
1156 	 * until we get the descriptor's credentials.
1157 	 */
1158 	if (!(flags & JAIL_USE_DESC)) {
1159 		error = priv_check(td, PRIV_JAIL_SET);
1160 		if (error == 0 && (flags & JAIL_ATTACH))
1161 			error = priv_check(td, PRIV_JAIL_ATTACH);
1162 		if (error)
1163 			goto done_free;
1164 	}
1165 
1166 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1167 	if (error == ENOENT)
1168 		jid = 0;
1169 	else if (error != 0)
1170 		goto done_free;
1171 
1172 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1173 	if (error == ENOENT)
1174 		gotslevel = 0;
1175 	else if (error != 0)
1176 		goto done_free;
1177 	else
1178 		gotslevel = 1;
1179 
1180 	error =
1181 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1182 	if (error == ENOENT)
1183 		gotchildmax = 0;
1184 	else if (error != 0)
1185 		goto done_free;
1186 	else
1187 		gotchildmax = 1;
1188 
1189 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1190 	if (error == ENOENT)
1191 		gotenforce = 0;
1192 	else if (error != 0)
1193 		goto done_free;
1194 	else if (enforce < 0 || enforce > 2) {
1195 		error = EINVAL;
1196 		goto done_free;
1197 	} else
1198 		gotenforce = 1;
1199 
1200 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
1201 	if (error == ENOENT)
1202 		gotrsnum = 0;
1203 	else if (error != 0)
1204 		goto done_free;
1205 	else
1206 		gotrsnum = 1;
1207 
1208 	pr_flags = ch_flags = 0;
1209 	for (bf = pr_flag_bool;
1210 	     bf < pr_flag_bool + nitems(pr_flag_bool);
1211 	     bf++) {
1212 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1213 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
1214 	}
1215 	ch_flags |= pr_flags;
1216 	for (jsf = pr_flag_jailsys;
1217 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1218 	     jsf++) {
1219 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
1220 		if (error == ENOENT)
1221 			continue;
1222 		if (error != 0)
1223 			goto done_free;
1224 		switch (jsys) {
1225 		case JAIL_SYS_DISABLE:
1226 			if (!jsf->disable) {
1227 				error = EINVAL;
1228 				goto done_free;
1229 			}
1230 			pr_flags |= jsf->disable;
1231 			break;
1232 		case JAIL_SYS_NEW:
1233 			pr_flags |= jsf->new;
1234 			break;
1235 		case JAIL_SYS_INHERIT:
1236 			break;
1237 		default:
1238 			error = EINVAL;
1239 			goto done_free;
1240 		}
1241 		ch_flags |= jsf->new | jsf->disable;
1242 	}
1243 	if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
1244 	    && !(pr_flags & PR_PERSIST)) {
1245 		error = EINVAL;
1246 		vfs_opterror(opts, "new jail must persist or attach");
1247 		goto done_errmsg;
1248 	}
1249 #ifdef VIMAGE
1250 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1251 		error = EINVAL;
1252 		vfs_opterror(opts, "vnet cannot be changed after creation");
1253 		goto done_errmsg;
1254 	}
1255 #endif
1256 #ifdef INET
1257 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
1258 		error = EINVAL;
1259 		vfs_opterror(opts, "ip4 cannot be changed after creation");
1260 		goto done_errmsg;
1261 	}
1262 #endif
1263 #ifdef INET6
1264 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
1265 		error = EINVAL;
1266 		vfs_opterror(opts, "ip6 cannot be changed after creation");
1267 		goto done_errmsg;
1268 	}
1269 #endif
1270 
1271 	pr_allow = ch_allow = 0;
1272 	for (bf = pr_flag_allow;
1273 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
1274 		atomic_load_int(&bf->flag) != 0;
1275 	     bf++) {
1276 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1277 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
1278 	}
1279 	ch_allow |= pr_allow;
1280 
1281 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1282 	if (error == ENOENT)
1283 		name = NULL;
1284 	else if (error != 0)
1285 		goto done_free;
1286 	else {
1287 		if (len == 0 || name[len - 1] != '\0') {
1288 			error = EINVAL;
1289 			goto done_free;
1290 		}
1291 		if (len > MAXHOSTNAMELEN) {
1292 			error = ENAMETOOLONG;
1293 			goto done_free;
1294 		}
1295 	}
1296 
1297 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1298 	if (error == ENOENT)
1299 		host = NULL;
1300 	else if (error != 0)
1301 		goto done_free;
1302 	else {
1303 		ch_flags |= PR_HOST;
1304 		pr_flags |= PR_HOST;
1305 		if (len == 0 || host[len - 1] != '\0') {
1306 			error = EINVAL;
1307 			goto done_free;
1308 		}
1309 		if (len > MAXHOSTNAMELEN) {
1310 			error = ENAMETOOLONG;
1311 			goto done_free;
1312 		}
1313 	}
1314 
1315 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
1316 	if (error == ENOENT)
1317 		domain = NULL;
1318 	else if (error != 0)
1319 		goto done_free;
1320 	else {
1321 		ch_flags |= PR_HOST;
1322 		pr_flags |= PR_HOST;
1323 		if (len == 0 || domain[len - 1] != '\0') {
1324 			error = EINVAL;
1325 			goto done_free;
1326 		}
1327 		if (len > MAXHOSTNAMELEN) {
1328 			error = ENAMETOOLONG;
1329 			goto done_free;
1330 		}
1331 	}
1332 
1333 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
1334 	if (error == ENOENT)
1335 		uuid = NULL;
1336 	else if (error != 0)
1337 		goto done_free;
1338 	else {
1339 		ch_flags |= PR_HOST;
1340 		pr_flags |= PR_HOST;
1341 		if (len == 0 || uuid[len - 1] != '\0') {
1342 			error = EINVAL;
1343 			goto done_free;
1344 		}
1345 		if (len > HOSTUUIDLEN) {
1346 			error = ENAMETOOLONG;
1347 			goto done_free;
1348 		}
1349 	}
1350 
1351 #ifdef COMPAT_FREEBSD32
1352 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1353 		uint32_t hid32;
1354 
1355 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
1356 		hid = hid32;
1357 	} else
1358 #endif
1359 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
1360 	if (error == ENOENT)
1361 		gothid = 0;
1362 	else if (error != 0)
1363 		goto done_free;
1364 	else {
1365 		gothid = 1;
1366 		ch_flags |= PR_HOST;
1367 		pr_flags |= PR_HOST;
1368 	}
1369 
1370 #ifdef MAC
1371 	/* Process the mac.label vfsopt */
1372 	error = mac_set_prison_prepare(td, opts, &mac_set_prison_data);
1373 	if (error == ENOENT)
1374 		gotmaclabel = 0;
1375 	else if (error != 0)
1376 		goto done_errmsg;
1377 	else
1378 		gotmaclabel = 1;
1379 #endif
1380 
1381 #ifdef INET
1382 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1383 	if (error == ENOENT)
1384 		ip4s = 0;
1385 	else if (error != 0)
1386 		goto done_free;
1387 	else if (ip4s & (sizeof(struct in_addr) - 1)) {
1388 		error = EINVAL;
1389 		goto done_free;
1390 	} else {
1391 		ch_flags |= PR_IP4_USER;
1392 		pr_flags |= PR_IP4_USER;
1393 		if (ip4s > 0) {
1394 			ip4s /= sizeof(struct in_addr);
1395 			if (ip4s > jail_max_af_ips) {
1396 				error = EINVAL;
1397 				vfs_opterror(opts, "too many IPv4 addresses");
1398 				goto done_errmsg;
1399 			}
1400 			ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1401 			if (ip4 == NULL) {
1402 				error = EINVAL;
1403 				goto done_free;
1404 			}
1405 		}
1406 	}
1407 #endif
1408 
1409 #ifdef INET6
1410 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1411 	if (error == ENOENT)
1412 		ip6s = 0;
1413 	else if (error != 0)
1414 		goto done_free;
1415 	else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1416 		error = EINVAL;
1417 		goto done_free;
1418 	} else {
1419 		ch_flags |= PR_IP6_USER;
1420 		pr_flags |= PR_IP6_USER;
1421 		if (ip6s > 0) {
1422 			ip6s /= sizeof(struct in6_addr);
1423 			if (ip6s > jail_max_af_ips) {
1424 				error = EINVAL;
1425 				vfs_opterror(opts, "too many IPv6 addresses");
1426 				goto done_errmsg;
1427 			}
1428 			ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1429 			if (ip6 == NULL) {
1430 				error = EINVAL;
1431 				goto done_free;
1432 			}
1433 		}
1434 	}
1435 #endif
1436 
1437 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1438 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1439 		error = EINVAL;
1440 		vfs_opterror(opts,
1441 		    "vnet jails cannot have IP address restrictions");
1442 		goto done_errmsg;
1443 	}
1444 #endif
1445 
1446 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1447 	if (error == ENOENT)
1448 		osrelstr = NULL;
1449 	else if (error != 0)
1450 		goto done_free;
1451 	else {
1452 		if (flags & JAIL_UPDATE) {
1453 			error = EINVAL;
1454 			vfs_opterror(opts,
1455 			    "osrelease cannot be changed after creation");
1456 			goto done_errmsg;
1457 		}
1458 		if (len == 0 || osrelstr[len - 1] != '\0') {
1459 			error = EINVAL;
1460 			goto done_free;
1461 		}
1462 		if (len >= OSRELEASELEN) {
1463 			error = ENAMETOOLONG;
1464 			vfs_opterror(opts,
1465 			    "osrelease string must be 1-%d bytes long",
1466 			    OSRELEASELEN - 1);
1467 			goto done_errmsg;
1468 		}
1469 	}
1470 
1471 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1472 	if (error == ENOENT)
1473 		osreldt = 0;
1474 	else if (error != 0)
1475 		goto done_free;
1476 	else {
1477 		if (flags & JAIL_UPDATE) {
1478 			error = EINVAL;
1479 			vfs_opterror(opts,
1480 			    "osreldate cannot be changed after creation");
1481 			goto done_errmsg;
1482 		}
1483 		if (osreldt == 0) {
1484 			error = EINVAL;
1485 			vfs_opterror(opts, "osreldate cannot be 0");
1486 			goto done_errmsg;
1487 		}
1488 	}
1489 
1490 	root = NULL;
1491 	error = vfs_getopt(opts, "path", (void **)&path, &len);
1492 	if (error == ENOENT)
1493 		path = NULL;
1494 	else if (error != 0)
1495 		goto done_free;
1496 	else {
1497 		if (flags & JAIL_UPDATE) {
1498 			error = EINVAL;
1499 			vfs_opterror(opts,
1500 			    "path cannot be changed after creation");
1501 			goto done_errmsg;
1502 		}
1503 		if (len == 0 || path[len - 1] != '\0') {
1504 			error = EINVAL;
1505 			goto done_free;
1506 		}
1507 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1508 		error = namei(&nd);
1509 		if (error)
1510 			goto done_free;
1511 		root = nd.ni_vp;
1512 		NDFREE_PNBUF(&nd);
1513 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1514 		strlcpy(g_path, path, MAXPATHLEN);
1515 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1516 		if (error == 0) {
1517 			path = g_path;
1518 		} else {
1519 			/* exit on other errors */
1520 			goto done_free;
1521 		}
1522 		if (root->v_type != VDIR) {
1523 			error = ENOTDIR;
1524 			vput(root);
1525 			goto done_free;
1526 		}
1527 		VOP_UNLOCK(root);
1528 	}
1529 
1530 	/*
1531 	 * Find the specified jail, or at least its parent.
1532 	 * This abuses the file error codes ENOENT and EEXIST.
1533 	 */
1534 	pr = NULL;
1535 	inspr = NULL;
1536 	deadpr = NULL;
1537 	maybe_changed = false;
1538 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1539 		namelc = strrchr(name, '.');
1540 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1541 		if (*p != '\0')
1542 			jid = 0;
1543 	}
1544 	sx_xlock(&allprison_lock);
1545 	drflags = PD_LIST_XLOCKED;
1546 	ppr = mypr;
1547 	if (!prison_isalive(ppr)) {
1548 		/* This jail is dying.  This process will surely follow. */
1549 		error = EAGAIN;
1550 		goto done_deref;
1551 	}
1552 	if (flags & JAIL_USE_DESC) {
1553 		/* Get the jail from its descriptor. */
1554 		error = jaildesc_find(td, jfd_in, &pr, &jdcred);
1555 		if (error) {
1556 			vfs_opterror(opts, error == ENOENT ?
1557 			    "descriptor to dead jail" :
1558 			    "not a jail descriptor");
1559 			goto done_deref;
1560 		}
1561 		drflags |= PD_DEREF;
1562 		error = priv_check_cred(jdcred, PRIV_JAIL_SET);
1563 		if (error == 0 && (flags & JAIL_ATTACH))
1564 			error = priv_check_cred(jdcred, PRIV_JAIL_ATTACH);
1565 		crfree(jdcred);
1566 		if (error)
1567 			goto done_deref;
1568 		mtx_lock(&pr->pr_mtx);
1569 		drflags |= PD_LOCKED;
1570 		if (cuflags == JAIL_CREATE) {
1571 			error = EEXIST;
1572 			vfs_opterror(opts, "jail %d already exists",
1573 			    pr->pr_id);
1574 			goto done_deref;
1575 		}
1576 		if (!prison_isalive(pr)) {
1577 			/* While a jid can be resurrected, the prison
1578 			 * itself cannot.
1579 			 */
1580 			error = ENOENT;
1581 			vfs_opterror(opts, "jail %d is dying", pr->pr_id);
1582 			goto done_deref;
1583 		}
1584 		if (jid != 0 && jid != pr->pr_id) {
1585 			error = EINVAL;
1586 			vfs_opterror(opts, "cannot change jid");
1587 			goto done_deref;
1588 		}
1589 		jid = pr->pr_id;
1590 	} else if (jid != 0) {
1591 		if (jid < 0) {
1592 			error = EINVAL;
1593 			vfs_opterror(opts, "negative jid");
1594 			goto done_deref;
1595 		}
1596 		/*
1597 		 * See if a requested jid already exists.  Keep track of
1598 		 * where it can be inserted later.
1599 		 */
1600 		TAILQ_FOREACH(inspr, &allprison, pr_list) {
1601 			if (inspr->pr_id < jid)
1602 				continue;
1603 			if (inspr->pr_id > jid)
1604 				break;
1605 			if (prison_isalive(inspr)) {
1606 				pr = inspr;
1607 				mtx_lock(&pr->pr_mtx);
1608 				drflags |= PD_LOCKED;
1609 			} else {
1610 				/* Note a dying jail to handle later. */
1611 				deadpr = inspr;
1612 			}
1613 			inspr = NULL;
1614 			break;
1615 		}
1616 		if (cuflags == JAIL_CREATE && pr != NULL) {
1617 			/*
1618 			 * Even creators that cannot see the jail will
1619 			 * get EEXIST.
1620 			 */
1621 			error = EEXIST;
1622 			vfs_opterror(opts, "jail %d already exists", jid);
1623 			goto done_deref;
1624 		}
1625 		if ((pr == NULL)
1626 		    ? cuflags == JAIL_UPDATE
1627 		    : !prison_ischild(mypr, pr)) {
1628 			/*
1629 			 * Updaters get ENOENT for nonexistent jails,
1630 			 * or for jails they cannot see.  The latter
1631 			 * case is true even for CREATE | UPDATE,
1632 			 * which normally cannot give this error.
1633 			 */
1634 			error = ENOENT;
1635 			vfs_opterror(opts, "jail %d not found", jid);
1636 			goto done_deref;
1637 		}
1638 	}
1639 	/*
1640 	 * If the caller provided a name, look for a jail by that name.
1641 	 * This has different semantics for creates and updates keyed by jid
1642 	 * (where the name must not already exist in a different jail),
1643 	 * and updates keyed by the name itself (where the name must exist
1644 	 * because that is the jail being updated).
1645 	 */
1646 	namelc = NULL;
1647 	if (name != NULL) {
1648 		namelc = strrchr(name, '.');
1649 		if (namelc == NULL)
1650 			namelc = name;
1651 		else {
1652 			/*
1653 			 * This is a hierarchical name.  Split it into the
1654 			 * parent and child names, and make sure the parent
1655 			 * exists or matches an already found jail.
1656 			 */
1657 			if (pr != NULL) {
1658 				if (strncmp(name, ppr->pr_name, namelc - name)
1659 				    || ppr->pr_name[namelc - name] != '\0') {
1660 					error = EINVAL;
1661 					vfs_opterror(opts,
1662 					    "cannot change jail's parent");
1663 					goto done_deref;
1664 				}
1665 			} else {
1666 				*namelc = '\0';
1667 				ppr = prison_find_name(mypr, name);
1668 				if (ppr == NULL) {
1669 					error = ENOENT;
1670 					vfs_opterror(opts,
1671 					    "jail \"%s\" not found", name);
1672 					goto done_deref;
1673 				}
1674 				mtx_unlock(&ppr->pr_mtx);
1675 				if (!prison_isalive(ppr)) {
1676 					error = ENOENT;
1677 					vfs_opterror(opts,
1678 					    "jail \"%s\" is dying", name);
1679 					goto done_deref;
1680 				}
1681 				*namelc = '.';
1682 			}
1683 			namelc++;
1684 		}
1685 		if (namelc[0] != '\0') {
1686 			pnamelen =
1687 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1688 			FOREACH_PRISON_CHILD(ppr, tpr) {
1689 				if (tpr == pr || !prison_isalive(tpr) ||
1690 				    strcmp(tpr->pr_name + pnamelen, namelc))
1691 					continue;
1692 				if (cuflags == JAIL_CREATE || pr != NULL) {
1693 					/*
1694 					 * Create, or update(jid): name must
1695 					 * not exist in an active sibling jail.
1696 					 */
1697 					error = EEXIST;
1698 					vfs_opterror(opts,
1699 					    "jail \"%s\" already exists", name);
1700 					goto done_deref;
1701 				}
1702 				/* Use this jail for updates. */
1703 				pr = tpr;
1704 				mtx_lock(&pr->pr_mtx);
1705 				drflags |= PD_LOCKED;
1706 				break;
1707 			}
1708 			/*
1709 			 * Update: name must exist if no jid is specified.
1710 			 * As with the jid case, the jail must be currently
1711 			 * visible, or else even CREATE | UPDATE will get
1712 			 * an error.
1713 			 */
1714 			if ((pr == NULL)
1715 			    ? cuflags == JAIL_UPDATE
1716 			    : !prison_isalive(pr)) {
1717 				error = ENOENT;
1718 				vfs_opterror(opts, "jail \"%s\" not found",
1719 				    name);
1720 				goto done_deref;
1721 			}
1722 		}
1723 	}
1724 	/* Update: must provide a desc, jid, or name. */
1725 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1726 		error = ENOENT;
1727 		vfs_opterror(opts, "update specified no jail");
1728 		goto done_deref;
1729 	}
1730 
1731 	/* If there's no prison to update, create a new one and link it in. */
1732 	created = pr == NULL;
1733 	if (created) {
1734 #ifdef MAC
1735 		error = mac_prison_check_create(td->td_ucred, opts, flags);
1736 		if (error != 0)
1737 			goto done_deref;
1738 #endif
1739 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1740 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1741 				error = EPERM;
1742 				vfs_opterror(opts, "prison limit exceeded");
1743 				goto done_deref;
1744 			}
1745 
1746 		if (deadpr != NULL) {
1747 			/*
1748 			 * The prison being created has the same ID as a dying
1749 			 * one.  Handle this by giving the dying jail a new ID.
1750 			 * This may cause some confusion to user space, but
1751 			 * only to those listing dying jails.
1752 			 */
1753 			deadid = get_next_deadid(&dinspr);
1754 			if (deadid == 0) {
1755 				error = EAGAIN;
1756 				vfs_opterror(opts, "no available jail IDs");
1757 				goto done_deref;
1758 			}
1759 			mtx_lock(&deadpr->pr_mtx);
1760 			deadpr->pr_id = deadid;
1761 			mtx_unlock(&deadpr->pr_mtx);
1762 			if (dinspr == deadpr)
1763 				inspr = deadpr;
1764 			else {
1765 				inspr = TAILQ_NEXT(deadpr, pr_list);
1766 				TAILQ_REMOVE(&allprison, deadpr, pr_list);
1767 				if (dinspr != NULL)
1768 					TAILQ_INSERT_AFTER(&allprison, dinspr,
1769 					    deadpr, pr_list);
1770 				else
1771 					TAILQ_INSERT_HEAD(&allprison, deadpr,
1772 					    pr_list);
1773 			}
1774 		}
1775 		if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1776 			error = EAGAIN;
1777 			vfs_opterror(opts, "no available jail IDs");
1778 			goto done_deref;
1779 		}
1780 
1781 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1782 		pr->pr_state = PRISON_STATE_INVALID;
1783 		refcount_init(&pr->pr_ref, 1);
1784 		refcount_init(&pr->pr_uref, 0);
1785 		drflags |= PD_DEREF;
1786 		LIST_INIT(&pr->pr_children);
1787 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1788 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1789 
1790 		pr->pr_id = jid;
1791 		if (inspr != NULL)
1792 			TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1793 		else
1794 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1795 
1796 		pr->pr_parent = ppr;
1797 		prison_hold(ppr);
1798 		prison_proc_hold(ppr);
1799 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1800 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1801 			tpr->pr_childcount++;
1802 		pr->pr_klist = knlist_alloc(&pr->pr_mtx);
1803 
1804 		/* Set some default values, and inherit some from the parent. */
1805 		if (namelc == NULL)
1806 			namelc = "";
1807 		if (path == NULL) {
1808 			path = "/";
1809 			root = mypr->pr_root;
1810 			vref(root);
1811 		}
1812 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1813 		pr->pr_flags |= PR_HOST;
1814 #if defined(INET) || defined(INET6)
1815 #ifdef VIMAGE
1816 		if (!(pr_flags & PR_VNET))
1817 #endif
1818 		{
1819 #ifdef INET
1820 			if (!(ch_flags & PR_IP4_USER))
1821 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1822 			else if (!(pr_flags & PR_IP4_USER)) {
1823 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1824 				prison_ip_dup(ppr, pr, PR_INET);
1825 			}
1826 #endif
1827 #ifdef INET6
1828 			if (!(ch_flags & PR_IP6_USER))
1829 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1830 			else if (!(pr_flags & PR_IP6_USER)) {
1831 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1832 				prison_ip_dup(ppr, pr, PR_INET6);
1833 			}
1834 #endif
1835 		}
1836 #endif
1837 		/* Source address selection is always on by default. */
1838 		pr->pr_flags |= _PR_IP_SADDRSEL;
1839 
1840 		pr->pr_securelevel = ppr->pr_securelevel;
1841 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1842 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1843 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1844 
1845 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1846 		if (osrelstr == NULL)
1847 			strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1848 			    sizeof(pr->pr_osrelease));
1849 		else
1850 			strlcpy(pr->pr_osrelease, osrelstr,
1851 			    sizeof(pr->pr_osrelease));
1852 
1853 #ifdef VIMAGE
1854 		/*
1855 		 * Allocate a new vnet if specified.
1856 		 *
1857 		 * Set PR_VNET now if so, so that the vnet is disposed of
1858 		 * properly when the jail is destroyed.
1859 		 */
1860 		if (pr_flags & PR_VNET) {
1861 			pr->pr_flags |= PR_VNET;
1862 			pr->pr_vnet = vnet_alloc();
1863 		} else {
1864 			pr->pr_vnet = ppr->pr_vnet;
1865 		}
1866 #endif
1867 		/*
1868 		 * Allocate a dedicated cpuset for each jail.
1869 		 * Unlike other initial settings, this may return an error.
1870 		 */
1871 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1872 		if (error)
1873 			goto done_deref;
1874 
1875 #ifdef MAC
1876 		error = mac_prison_init(pr, M_WAITOK);
1877 		MPASS(error == 0);
1878 
1879 		mtx_assert(&pr->pr_mtx, MA_OWNED);
1880 #else
1881 		mtx_lock(&pr->pr_mtx);
1882 #endif
1883 		drflags |= PD_LOCKED;
1884 	} else {
1885 		/*
1886 		 * Grab a reference for existing prisons, to ensure they
1887 		 * continue to exist for the duration of the call.
1888 		 */
1889 		if (!(drflags & PD_DEREF)) {
1890 			prison_hold(pr);
1891 			drflags |= PD_DEREF;
1892 		}
1893 #ifdef MAC
1894 		error = mac_prison_check_set(td->td_ucred, pr, opts, flags);
1895 		if (error != 0)
1896 			goto done_deref;
1897 #endif
1898 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1899 		if ((pr->pr_flags & PR_VNET) &&
1900 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1901 			error = EINVAL;
1902 			vfs_opterror(opts,
1903 			    "vnet jails cannot have IP address restrictions");
1904 			goto done_deref;
1905 		}
1906 #endif
1907 #ifdef INET
1908 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1909 			error = EINVAL;
1910 			vfs_opterror(opts,
1911 			    "ip4 cannot be changed after creation");
1912 			goto done_deref;
1913 		}
1914 #endif
1915 #ifdef INET6
1916 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1917 			error = EINVAL;
1918 			vfs_opterror(opts,
1919 			    "ip6 cannot be changed after creation");
1920 			goto done_deref;
1921 		}
1922 #endif
1923 	}
1924 
1925 	/* Do final error checking before setting anything. */
1926 	if (gotslevel) {
1927 		if (slevel < ppr->pr_securelevel) {
1928 			error = EPERM;
1929 			goto done_deref;
1930 		}
1931 	}
1932 	if (gotchildmax) {
1933 		if (childmax >= ppr->pr_childmax) {
1934 			error = EPERM;
1935 			goto done_deref;
1936 		}
1937 	}
1938 	if (gotenforce) {
1939 		if (enforce < ppr->pr_enforce_statfs) {
1940 			error = EPERM;
1941 			goto done_deref;
1942 		}
1943 	}
1944 	if (gotrsnum) {
1945 		/*
1946 		 * devfs_rsnum is a uint16_t
1947 		 */
1948 		if (rsnum < 0 || rsnum > 65535) {
1949 			error = EINVAL;
1950 			goto done_deref;
1951 		}
1952 		/*
1953 		 * Nested jails always inherit parent's devfs ruleset
1954 		 */
1955 		if (jailed(td->td_ucred)) {
1956 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1957 				error = EPERM;
1958 				goto done_deref;
1959 			} else
1960 				rsnum = ppr->pr_devfs_rsnum;
1961 		}
1962 	}
1963 #ifdef INET
1964 	if (ip4s > 0) {
1965 		if ((ppr->pr_flags & PR_IP4) &&
1966 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1967 		    PR_INET)) {
1968 			error = EPERM;
1969 			goto done_deref;
1970 		}
1971 		if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
1972 			error = EADDRINUSE;
1973 			vfs_opterror(opts, "IPv4 addresses clash");
1974 			goto done_deref;
1975 		}
1976 	}
1977 #endif
1978 #ifdef INET6
1979 	if (ip6s > 0) {
1980 		if ((ppr->pr_flags & PR_IP6) &&
1981 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1982 		    PR_INET6)) {
1983 			error = EPERM;
1984 			goto done_deref;
1985 		}
1986 		if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
1987 			error = EADDRINUSE;
1988 			vfs_opterror(opts, "IPv6 addresses clash");
1989 			goto done_deref;
1990 		}
1991 	}
1992 #endif
1993 	onamelen = namelen = 0;
1994 	if (namelc != NULL) {
1995 		/* Give a default name of the jid.  Also allow the name to be
1996 		 * explicitly the jid - but not any other number, and only in
1997 		 * normal form (no leading zero/etc).
1998 		 */
1999 		if (namelc[0] == '\0')
2000 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
2001 		else if ((strtoul(namelc, &p, 10) != jid ||
2002 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
2003 			error = EINVAL;
2004 			vfs_opterror(opts,
2005 			    "name cannot be numeric (unless it is the jid)");
2006 			goto done_deref;
2007 		}
2008 		/*
2009 		 * Make sure the name isn't too long for the prison or its
2010 		 * children.
2011 		 */
2012 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
2013 		onamelen = strlen(pr->pr_name + pnamelen);
2014 		namelen = strlen(namelc);
2015 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
2016 			error = ENAMETOOLONG;
2017 			goto done_deref;
2018 		}
2019 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
2020 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
2021 			    sizeof(pr->pr_name)) {
2022 				error = ENAMETOOLONG;
2023 				goto done_deref;
2024 			}
2025 		}
2026 	}
2027 	pr_allow_diff = pr_allow & ~ppr->pr_allow;
2028 	if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
2029 		error = EPERM;
2030 		goto done_deref;
2031 	}
2032 
2033 	/*
2034 	 * Let modules check their parameters.  This requires unlocking and
2035 	 * then re-locking the prison, but this is still a valid state as long
2036 	 * as allprison_lock remains xlocked.
2037 	 */
2038 	mtx_unlock(&pr->pr_mtx);
2039 	drflags &= ~PD_LOCKED;
2040 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
2041 	if (error != 0)
2042 		goto done_deref;
2043 	mtx_lock(&pr->pr_mtx);
2044 	drflags |= PD_LOCKED;
2045 
2046 	/* At this point, all valid parameters should have been noted. */
2047 	TAILQ_FOREACH(opt, opts, link) {
2048 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2049 			error = EINVAL;
2050 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2051 			goto done_deref;
2052 		}
2053 	}
2054 	maybe_changed = true;
2055 
2056 	/* Set the parameters of the prison. */
2057 #ifdef INET
2058 	redo_ip4 = false;
2059 	if (pr_flags & PR_IP4_USER) {
2060 		pr->pr_flags |= PR_IP4;
2061 		prison_ip_set(pr, PR_INET, ip4);
2062 		ip4 = NULL;
2063 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2064 #ifdef VIMAGE
2065 			if (tpr->pr_flags & PR_VNET) {
2066 				descend = 0;
2067 				continue;
2068 			}
2069 #endif
2070 			if (!prison_ip_restrict(tpr, PR_INET, NULL)) {
2071 				redo_ip4 = true;
2072 				descend = 0;
2073 			}
2074 		}
2075 	}
2076 #endif
2077 #ifdef INET6
2078 	redo_ip6 = false;
2079 	if (pr_flags & PR_IP6_USER) {
2080 		pr->pr_flags |= PR_IP6;
2081 		prison_ip_set(pr, PR_INET6, ip6);
2082 		ip6 = NULL;
2083 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2084 #ifdef VIMAGE
2085 			if (tpr->pr_flags & PR_VNET) {
2086 				descend = 0;
2087 				continue;
2088 			}
2089 #endif
2090 			if (!prison_ip_restrict(tpr, PR_INET6, NULL)) {
2091 				redo_ip6 = true;
2092 				descend = 0;
2093 			}
2094 		}
2095 	}
2096 #endif
2097 	if (gotslevel) {
2098 		pr->pr_securelevel = slevel;
2099 		/* Set all child jails to be at least this level. */
2100 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
2101 			if (tpr->pr_securelevel < slevel)
2102 				tpr->pr_securelevel = slevel;
2103 	}
2104 	if (gotchildmax) {
2105 		pr->pr_childmax = childmax;
2106 		/* Set all child jails to under this limit. */
2107 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
2108 			if (tpr->pr_childmax > childmax - level)
2109 				tpr->pr_childmax = childmax > level
2110 				    ? childmax - level : 0;
2111 	}
2112 	if (gotenforce) {
2113 		pr->pr_enforce_statfs = enforce;
2114 		/* Pass this restriction on to the children. */
2115 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
2116 			if (tpr->pr_enforce_statfs < enforce)
2117 				tpr->pr_enforce_statfs = enforce;
2118 	}
2119 	if (gotrsnum) {
2120 		pr->pr_devfs_rsnum = rsnum;
2121 		/* Pass this restriction on to the children. */
2122 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
2123 			tpr->pr_devfs_rsnum = rsnum;
2124 	}
2125 	if (namelc != NULL) {
2126 		if (ppr == &prison0)
2127 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
2128 		else
2129 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
2130 			    ppr->pr_name, namelc);
2131 		/* Change this component of child names. */
2132 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2133 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
2134 			    strlen(tpr->pr_name + onamelen) + 1);
2135 			bcopy(pr->pr_name, tpr->pr_name, namelen);
2136 		}
2137 	}
2138 	if (path != NULL) {
2139 		/* Try to keep a real-rooted full pathname. */
2140 		strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
2141 		pr->pr_root = root;
2142 		root = NULL;
2143 	}
2144 	if (PR_HOST & ch_flags & ~pr_flags) {
2145 		if (pr->pr_flags & PR_HOST) {
2146 			/*
2147 			 * Copy the parent's host info.  As with pr_ip4 above,
2148 			 * the lack of a lock on the parent is not a problem;
2149 			 * it is always set with allprison_lock at least
2150 			 * shared, and is held exclusively here.
2151 			 */
2152 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
2153 			    sizeof(pr->pr_hostname));
2154 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
2155 			    sizeof(pr->pr_domainname));
2156 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
2157 			    sizeof(pr->pr_hostuuid));
2158 			pr->pr_hostid = pr->pr_parent->pr_hostid;
2159 		}
2160 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
2161 		/* Set this prison, and any descendants without PR_HOST. */
2162 		if (host != NULL)
2163 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
2164 		if (domain != NULL)
2165 			strlcpy(pr->pr_domainname, domain,
2166 			    sizeof(pr->pr_domainname));
2167 		if (uuid != NULL)
2168 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
2169 		if (gothid)
2170 			pr->pr_hostid = hid;
2171 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2172 			if (tpr->pr_flags & PR_HOST)
2173 				descend = 0;
2174 			else {
2175 				if (host != NULL)
2176 					strlcpy(tpr->pr_hostname,
2177 					    pr->pr_hostname,
2178 					    sizeof(tpr->pr_hostname));
2179 				if (domain != NULL)
2180 					strlcpy(tpr->pr_domainname,
2181 					    pr->pr_domainname,
2182 					    sizeof(tpr->pr_domainname));
2183 				if (uuid != NULL)
2184 					strlcpy(tpr->pr_hostuuid,
2185 					    pr->pr_hostuuid,
2186 					    sizeof(tpr->pr_hostuuid));
2187 				if (gothid)
2188 					tpr->pr_hostid = hid;
2189 			}
2190 		}
2191 	}
2192 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
2193 	if ((tallow = ch_allow & ~pr_allow))
2194 		prison_set_allow_locked(pr, tallow, 0);
2195 	/*
2196 	 * Persistent prisons get an extra reference, and prisons losing their
2197 	 * persist flag lose that reference.
2198 	 */
2199 	if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
2200 		if (pr_flags & PR_PERSIST) {
2201 			prison_hold(pr);
2202 			/*
2203 			 * This may be a new prison's first user reference,
2204 			 * but wait to call it alive until after OSD calls
2205 			 * have had a chance to run (and perhaps to fail).
2206 			 */
2207 			refcount_acquire(&pr->pr_uref);
2208 		} else {
2209 			drflags |= PD_DEUREF;
2210 			prison_free_not_last(pr);
2211 		}
2212 	}
2213 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
2214 
2215 #ifdef MAC
2216 	/* Apply any request MAC label before we let modules do their work. */
2217 	if (gotmaclabel) {
2218 		error = mac_set_prison_core(td, pr, mac_set_prison_data);
2219 		if (error) {
2220 			vfs_opterror(opts, "mac relabel denied");
2221 			goto done_deref;
2222 		}
2223 	}
2224 #endif
2225 	mtx_unlock(&pr->pr_mtx);
2226 	drflags &= ~PD_LOCKED;
2227 	/*
2228 	 * Any errors past this point will need to de-persist newly created
2229 	 * prisons, as well as call remove methods.
2230 	 */
2231 	if (created)
2232 		drflags |= PD_KILL;
2233 
2234 #ifdef RACCT
2235 	if (racct_enable && created)
2236 		prison_racct_attach(pr);
2237 #endif
2238 
2239 	/* Locks may have prevented a complete restriction of child IP
2240 	 * addresses.  If so, allocate some more memory and try again.
2241 	 */
2242 #ifdef INET
2243 	while (redo_ip4) {
2244 		ip4s = pr->pr_addrs[PR_INET]->ips;
2245 		MPASS(ip4 == NULL);
2246 		ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
2247 		mtx_lock(&pr->pr_mtx);
2248 		redo_ip4 = false;
2249 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2250 #ifdef VIMAGE
2251 			if (tpr->pr_flags & PR_VNET) {
2252 				descend = 0;
2253 				continue;
2254 			}
2255 #endif
2256 			if (!prison_ip_restrict(tpr, PR_INET, &ip4))
2257 				redo_ip4 = true;
2258 		}
2259 		mtx_unlock(&pr->pr_mtx);
2260 	}
2261 #endif
2262 #ifdef INET6
2263 	while (redo_ip6) {
2264 		ip6s = pr->pr_addrs[PR_INET6]->ips;
2265 		MPASS(ip6 == NULL);
2266 		ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
2267 		mtx_lock(&pr->pr_mtx);
2268 		redo_ip6 = false;
2269 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2270 #ifdef VIMAGE
2271 			if (tpr->pr_flags & PR_VNET) {
2272 				descend = 0;
2273 				continue;
2274 			}
2275 #endif
2276 			if (!prison_ip_restrict(tpr, PR_INET6, &ip6))
2277 				redo_ip6 = true;
2278 		}
2279 		mtx_unlock(&pr->pr_mtx);
2280 	}
2281 #endif
2282 
2283 	/* Let the modules do their work. */
2284 	if (created) {
2285 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2286 		if (error)
2287 			goto done_deref;
2288 	}
2289 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
2290 	if (error)
2291 		goto done_deref;
2292 
2293 	/*
2294 	 * A new prison is now ready to be seen; either it has gained a user
2295 	 * reference via persistence, or is about to gain one via attachment.
2296 	 */
2297 	if (created) {
2298 		sx_assert(&allprison_lock, SX_XLOCKED);
2299 		prison_knote(ppr, NOTE_JAIL_CHILD | pr->pr_id);
2300 #ifdef MAC
2301 		/*
2302 		 * Note that mac_prison_created() assumes that it's called in a
2303 		 * sleepable context.
2304 		 */
2305 		mac_prison_created(td->td_ucred, pr);
2306 #endif
2307 		mtx_lock(&pr->pr_mtx);
2308 		drflags |= PD_LOCKED;
2309 		pr->pr_state = PRISON_STATE_ALIVE;
2310 	}
2311 
2312 	/*
2313 	 * Attach this process to the prison if requested.  This will
2314 	 * unlock allprison_lock, meaning changes are now user-visible.
2315 	 */
2316 	if (flags & JAIL_ATTACH) {
2317 #ifdef MAC
2318 		error = mac_prison_check_attach(td->td_ucred, pr);
2319 		if (error != 0) {
2320 			vfs_opterror(opts,
2321 			    "attach operation denied by MAC policy");
2322 			goto done_deref;
2323 		}
2324 #endif
2325 		error = do_jail_attach(td, pr, &drflags);
2326 		if (error) {
2327 			vfs_opterror(opts, "attach failed");
2328 			goto done_deref;
2329 		}
2330 	}
2331 
2332 #ifdef RACCT
2333 	if (racct_enable && !created) {
2334 		if (drflags & PD_LOCKED) {
2335 			mtx_unlock(&pr->pr_mtx);
2336 			drflags &= ~PD_LOCKED;
2337 		}
2338 		if (drflags & PD_LIST_XLOCKED) {
2339 			sx_xunlock(&allprison_lock);
2340 			drflags &= ~PD_LIST_XLOCKED;
2341 		}
2342 		prison_racct_modify(pr);
2343 	}
2344 #endif
2345 
2346 	if (created && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 &&
2347 	    (pr->pr_root->v_vflag & VV_ROOT) == 0)
2348 		printf("Warning jail jid=%d: mountd/nfsd requires a separate"
2349 		   " file system\n", pr->pr_id);
2350 
2351 	/*
2352 	 * Now that the prison is fully created without error, set the
2353 	 * jail descriptor if one was requested.  This is the only
2354 	 * parameter that is returned to the caller (except the error
2355 	 * message).
2356 	 */
2357 	if (jfd_out >= 0) {
2358 		if (!(drflags & PD_LOCKED)) {
2359 			mtx_lock(&pr->pr_mtx);
2360 			drflags |= PD_LOCKED;
2361 		}
2362 		jfd_pos = 2 * vfs_getopt_pos(opts, "desc") + 1;
2363 		if (optuio->uio_segflg == UIO_SYSSPACE)
2364 			*(int*)optuio->uio_iov[jfd_pos].iov_base = jfd_out;
2365 		else
2366 			(void)copyout(&jfd_out,
2367 			    optuio->uio_iov[jfd_pos].iov_base, sizeof(jfd_out));
2368 		jaildesc_set_prison(jfp_out, pr);
2369 	}
2370 
2371 	drflags &= ~PD_KILL;
2372 	td->td_retval[0] = pr->pr_id;
2373 
2374  done_deref:
2375 	/*
2376 	 * Report changes to kevent.  This can happen even if the
2377 	 * system call fails, as changes might have been made before
2378 	 * the failure.
2379 	 */
2380 	if (maybe_changed && !created)
2381 		prison_knote(pr, NOTE_JAIL_SET);
2382 	/* Release any temporary prison holds and/or locks. */
2383 	if (pr != NULL)
2384 		prison_deref(pr, drflags);
2385 	else if (drflags & PD_LIST_SLOCKED)
2386 		sx_sunlock(&allprison_lock);
2387 	else if (drflags & PD_LIST_XLOCKED)
2388 		sx_xunlock(&allprison_lock);
2389 	if (root != NULL)
2390 		vrele(root);
2391  done_errmsg:
2392 	if (error) {
2393 		/* Write the error message back to userspace. */
2394 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2395 		    &errmsg_len) == 0 && errmsg_len > 0) {
2396 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2397 			if (optuio->uio_segflg == UIO_SYSSPACE)
2398 				bcopy(errmsg,
2399 				    optuio->uio_iov[errmsg_pos].iov_base,
2400 				    errmsg_len);
2401 			else
2402 				(void)copyout(errmsg,
2403 				    optuio->uio_iov[errmsg_pos].iov_base,
2404 				    errmsg_len);
2405 		}
2406 	}
2407  done_free:
2408 	/* Clean up other resources. */
2409 #ifdef INET
2410 	prison_ip_free(ip4);
2411 #endif
2412 #ifdef INET6
2413 	prison_ip_free(ip6);
2414 #endif
2415 #ifdef MAC
2416 	if (mac_set_prison_data != NULL)
2417 		mac_set_prison_finish(td, error == 0, mac_set_prison_data);
2418 #endif
2419 	if (jfp_out != NULL)
2420 		fdrop(jfp_out, td);
2421 	if (error && jfd_out >= 0)
2422 		(void)kern_close(td, jfd_out);
2423 	if (g_path != NULL)
2424 		free(g_path, M_TEMP);
2425 	if (opts != NULL)
2426 		vfs_freeopts(opts);
2427 	prison_free(mypr);
2428 	if (flags & JAIL_ATTACH)
2429 		prison_attach_thread_single_end(td);
2430 	return (error);
2431 }
2432 
2433 /*
2434  * Find the next available prison ID.  Return the ID on success, or zero
2435  * on failure.  Also set a pointer to the allprison list entry the prison
2436  * should be inserted before.
2437  */
2438 static int
get_next_prid(struct prison ** insprp)2439 get_next_prid(struct prison **insprp)
2440 {
2441 	struct prison *inspr;
2442 	int jid, maxid;
2443 
2444 	jid = lastprid % JAIL_MAX + 1;
2445 	if (TAILQ_EMPTY(&allprison) ||
2446 	    TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
2447 		/*
2448 		 * A common case is for all jails to be implicitly numbered,
2449 		 * which means they'll go on the end of the list, at least
2450 		 * for the first JAIL_MAX times.
2451 		 */
2452 		inspr = NULL;
2453 	} else {
2454 		/*
2455 		 * Take two passes through the allprison list: first starting
2456 		 * with the proposed jid, then ending with it.
2457 		 */
2458 		for (maxid = JAIL_MAX; maxid != 0; ) {
2459 			TAILQ_FOREACH(inspr, &allprison, pr_list) {
2460 				if (inspr->pr_id < jid)
2461 					continue;
2462 				if (inspr->pr_id > jid) {
2463 					/* Found an opening. */
2464 					maxid = 0;
2465 					break;
2466 				}
2467 				if (++jid > maxid) {
2468 					if (lastprid == maxid || lastprid == 0)
2469 					{
2470 						/*
2471 						 * The entire legal range
2472 						 * has been traversed
2473 						 */
2474 						return 0;
2475 					}
2476 					/* Try again from the start. */
2477 					jid = 1;
2478 					maxid = lastprid;
2479 					break;
2480 				}
2481 			}
2482 			if (inspr == NULL) {
2483 				/* Found room at the end of the list. */
2484 				break;
2485 			}
2486 		}
2487 	}
2488 	*insprp = inspr;
2489 	lastprid = jid;
2490 	return (jid);
2491 }
2492 
2493 /*
2494  * Find the next available ID for a renumbered dead prison.  This is the same
2495  * as get_next_prid, but counting backward from the end of the range.
2496  */
2497 static int
get_next_deadid(struct prison ** dinsprp)2498 get_next_deadid(struct prison **dinsprp)
2499 {
2500 	struct prison *dinspr;
2501 	int deadid, minid;
2502 
2503 	deadid = lastdeadid ? lastdeadid - 1 : JAIL_MAX;
2504 	/*
2505 	 * Take two reverse passes through the allprison list: first
2506 	 * starting with the proposed deadid, then ending with it.
2507 	 */
2508 	for (minid = 1; minid != 0; ) {
2509 		TAILQ_FOREACH_REVERSE(dinspr, &allprison, prisonlist, pr_list) {
2510 			if (dinspr->pr_id > deadid)
2511 				continue;
2512 			if (dinspr->pr_id < deadid) {
2513 				/* Found an opening. */
2514 				minid = 0;
2515 				break;
2516 			}
2517 			if (--deadid < minid) {
2518 				if (lastdeadid == minid || lastdeadid == 0)
2519 				{
2520 					/*
2521 					 * The entire legal range
2522 					 * has been traversed
2523 					 */
2524 					return 0;
2525 				}
2526 				/* Try again from the end. */
2527 				deadid = JAIL_MAX;
2528 				minid = lastdeadid;
2529 				break;
2530 			}
2531 		}
2532 		if (dinspr == NULL) {
2533 			/* Found room at the beginning of the list. */
2534 			break;
2535 		}
2536 	}
2537 	*dinsprp = dinspr;
2538 	lastdeadid = deadid;
2539 	return (deadid);
2540 }
2541 
2542 /*
2543  * struct jail_get_args {
2544  *	struct iovec *iovp;
2545  *	unsigned int iovcnt;
2546  *	int flags;
2547  * };
2548  */
2549 int
sys_jail_get(struct thread * td,struct jail_get_args * uap)2550 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2551 {
2552 	struct uio *auio;
2553 	int error;
2554 
2555 	/* Check that we have an even number of iovecs. */
2556 	if (uap->iovcnt & 1)
2557 		return (EINVAL);
2558 
2559 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2560 	if (error)
2561 		return (error);
2562 	error = kern_jail_get(td, auio, uap->flags);
2563 	if (error == 0)
2564 		error = copyout(auio->uio_iov, uap->iovp,
2565 		    uap->iovcnt * sizeof(struct iovec));
2566 	freeuio(auio);
2567 	return (error);
2568 }
2569 
2570 int
kern_jail_get(struct thread * td,struct uio * optuio,int flags)2571 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2572 {
2573 	struct bool_flags *bf;
2574 	struct file *jfp_out;
2575 	struct jailsys_flags *jsf;
2576 	struct prison *pr, *mypr;
2577 	struct vfsopt *opt;
2578 	struct vfsoptlist *opts;
2579 	char *errmsg, *name;
2580 	int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2581 	int jfd_in, jfd_out;
2582 	unsigned f;
2583 
2584 	if (flags & ~JAIL_GET_MASK)
2585 		return (EINVAL);
2586 	if ((flags & (JAIL_USE_DESC | JAIL_AT_DESC)) ==
2587 	    (JAIL_USE_DESC | JAIL_AT_DESC))
2588 		return (EINVAL);
2589 
2590 	/* Get the parameter list. */
2591 	error = vfs_buildopts(optuio, &opts);
2592 	if (error)
2593 		return (error);
2594 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2595 	mypr = td->td_ucred->cr_prison;
2596 	prison_hold(mypr);
2597 	pr = NULL;
2598 	jfp_out = NULL;
2599 	jfd_out = -1;
2600 
2601 	/*
2602 	 * Find the prison specified by one of: desc, lastjid, jid, name.
2603 	 */
2604 	sx_slock(&allprison_lock);
2605 	drflags = PD_LIST_SLOCKED;
2606 
2607 	error = vfs_copyopt(opts, "desc", &jfd_in, sizeof(jfd_in));
2608 	if (error == ENOENT) {
2609 		if (flags & (JAIL_AT_DESC | JAIL_GET_DESC | JAIL_OWN_DESC)) {
2610 			vfs_opterror(opts, "missing desc");
2611 			goto done;
2612 		}
2613 	} else if (error == 0) {
2614 		if (!(flags & (JAIL_USE_DESC | JAIL_AT_DESC | JAIL_GET_DESC |
2615 		    JAIL_OWN_DESC))) {
2616 			error = EINVAL;
2617 			vfs_opterror(opts, "unexpected desc");
2618 			goto done;
2619 		}
2620 		if (flags & JAIL_USE_DESC) {
2621 			/* Get the jail from its descriptor. */
2622 			error = jaildesc_find(td, jfd_in, &pr, NULL);
2623 			if (error) {
2624 				vfs_opterror(opts, error == ENOENT ?
2625 				    "descriptor to dead jail" :
2626 				    "not a jail descriptor");
2627 				goto done;
2628 			}
2629 			drflags |= PD_DEREF;
2630 			mtx_lock(&pr->pr_mtx);
2631 			drflags |= PD_LOCKED;
2632 			goto found_prison;
2633 		}
2634 		if (flags & JAIL_AT_DESC) {
2635 			/* Look up jails based on the descriptor's prison. */
2636 			struct prison *jdpr;
2637 
2638 			error = jaildesc_find(td, jfd_in, &jdpr, NULL);
2639 			if (error != 0) {
2640 				vfs_opterror(opts, error == ENOENT ?
2641 				    "descriptor to dead jail" :
2642 				    "not a jail descriptor");
2643 				goto done;
2644 			}
2645 			prison_free(mypr);
2646 			mypr = jdpr;
2647 		}
2648 		if (flags & (JAIL_GET_DESC | JAIL_OWN_DESC)) {
2649 			/* Allocate a jail descriptor to return later. */
2650 			error = jaildesc_alloc(td, &jfp_out, &jfd_out,
2651 				flags & JAIL_OWN_DESC);
2652 			if (error)
2653 				goto done;
2654 		}
2655 	} else
2656 		goto done;
2657 
2658 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2659 	if (error == 0) {
2660 		TAILQ_FOREACH(pr, &allprison, pr_list) {
2661 			if (pr->pr_id > jid &&
2662 			    ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2663 			    prison_ischild(mypr, pr)) {
2664 				mtx_lock(&pr->pr_mtx);
2665 				drflags |= PD_LOCKED;
2666 #ifdef MAC
2667 				/*
2668 				 * We special-case this one check because we
2669 				 * don't want MAC to break jail enumeration.  We
2670 				 * need to just move on to the next accessible
2671 				 * and alive prison.
2672 				 */
2673 				error = mac_prison_check_get(td->td_ucred, pr,
2674 				    opts, flags);
2675 				if (error != 0) {
2676 					mtx_unlock(&pr->pr_mtx);
2677 					drflags &= ~PD_LOCKED;
2678 					continue;
2679 				}
2680 
2681 				/*
2682 				 * Avoid potentially expensive trip back into
2683 				 * the MAC framework.
2684 				 */
2685 				goto found_prison_nomac_alive;
2686 #else
2687 				goto found_prison;
2688 #endif
2689 			}
2690 		}
2691 		error = ENOENT;
2692 		vfs_opterror(opts, "no jail after %d", jid);
2693 		goto done;
2694 	} else if (error != ENOENT)
2695 		goto done;
2696 
2697 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2698 	if (error == 0) {
2699 		if (jid != 0) {
2700 			pr = prison_find_child(mypr, jid);
2701 			if (pr != NULL) {
2702 				drflags |= PD_LOCKED;
2703 				goto found_prison;
2704 			}
2705 			error = ENOENT;
2706 			vfs_opterror(opts, "jail %d not found", jid);
2707 			goto done;
2708 		}
2709 	} else if (error != ENOENT)
2710 		goto done;
2711 
2712 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2713 	if (error == 0) {
2714 		if (len == 0 || name[len - 1] != '\0') {
2715 			error = EINVAL;
2716 			goto done;
2717 		}
2718 		pr = prison_find_name(mypr, name);
2719 		if (pr != NULL) {
2720 			drflags |= PD_LOCKED;
2721 			goto found_prison;
2722 		}
2723 		error = ENOENT;
2724 		vfs_opterror(opts, "jail \"%s\" not found", name);
2725 		goto done;
2726 	} else if (error != ENOENT)
2727 		goto done;
2728 
2729 	vfs_opterror(opts, "no jail specified");
2730 	error = ENOENT;
2731 	goto done;
2732 
2733  found_prison:
2734 #ifdef MAC
2735 	error = mac_prison_check_get(td->td_ucred, pr, opts, flags);
2736 	if (error != 0)
2737 		goto done;
2738 #endif
2739 	if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2740 		error = ENOENT;
2741 		if (pr->pr_name[0] != '0' && isdigit(pr->pr_name[0])) {
2742 			vfs_opterror(opts, "jail %d is dying",
2743 			    pr->pr_id);
2744 		} else {
2745 			vfs_opterror(opts, "jail \"%s\" (%d) is dying",
2746 			    pr->pr_name, pr->pr_id);
2747 		}
2748 		goto done;
2749 	}
2750 #ifdef MAC
2751  found_prison_nomac_alive:
2752 #endif
2753 	/* Get the parameters of the prison. */
2754 	if (!(drflags & PD_DEREF)) {
2755 		prison_hold(pr);
2756 		drflags |= PD_DEREF;
2757 	}
2758 	td->td_retval[0] = pr->pr_id;
2759 	if (jfd_out >= 0) {
2760 		error = vfs_setopt(opts, "desc", &jfd_out, sizeof(jfd_out));
2761 		if (error != 0 && error != ENOENT)
2762 			goto done;
2763 		jaildesc_set_prison(jfp_out, pr);
2764 	}
2765 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2766 	if (error != 0 && error != ENOENT)
2767 		goto done;
2768 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2769 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2770 	if (error != 0 && error != ENOENT)
2771 		goto done;
2772 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2773 	if (error != 0 && error != ENOENT)
2774 		goto done;
2775 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2776 	    sizeof(pr->pr_cpuset->cs_id));
2777 	if (error != 0 && error != ENOENT)
2778 		goto done;
2779 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2780 	if (error != 0 && error != ENOENT)
2781 		goto done;
2782 #ifdef INET
2783 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET]->pr_ip,
2784 	    pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2785 	    pr_families[PR_INET].size : 0 );
2786 	if (error != 0 && error != ENOENT)
2787 		goto done;
2788 #endif
2789 #ifdef INET6
2790 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6]->pr_ip,
2791 	    pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2792 	    pr_families[PR_INET6].size : 0 );
2793 	if (error != 0 && error != ENOENT)
2794 		goto done;
2795 #endif
2796 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2797 	    sizeof(pr->pr_securelevel));
2798 	if (error != 0 && error != ENOENT)
2799 		goto done;
2800 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2801 	    sizeof(pr->pr_childcount));
2802 	if (error != 0 && error != ENOENT)
2803 		goto done;
2804 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2805 	    sizeof(pr->pr_childmax));
2806 	if (error != 0 && error != ENOENT)
2807 		goto done;
2808 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2809 	if (error != 0 && error != ENOENT)
2810 		goto done;
2811 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2812 	if (error != 0 && error != ENOENT)
2813 		goto done;
2814 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2815 	if (error != 0 && error != ENOENT)
2816 		goto done;
2817 #ifdef COMPAT_FREEBSD32
2818 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2819 		uint32_t hid32 = pr->pr_hostid;
2820 
2821 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2822 	} else
2823 #endif
2824 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2825 	    sizeof(pr->pr_hostid));
2826 	if (error != 0 && error != ENOENT)
2827 		goto done;
2828 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2829 	    sizeof(pr->pr_enforce_statfs));
2830 	if (error != 0 && error != ENOENT)
2831 		goto done;
2832 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2833 	    sizeof(pr->pr_devfs_rsnum));
2834 	if (error != 0 && error != ENOENT)
2835 		goto done;
2836 	for (bf = pr_flag_bool;
2837 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2838 	     bf++) {
2839 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2840 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2841 		if (error != 0 && error != ENOENT)
2842 			goto done;
2843 		i = !i;
2844 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2845 		if (error != 0 && error != ENOENT)
2846 			goto done;
2847 	}
2848 	for (jsf = pr_flag_jailsys;
2849 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2850 	     jsf++) {
2851 		f = pr->pr_flags & (jsf->disable | jsf->new);
2852 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2853 		    : (f == jsf->new) ? JAIL_SYS_NEW
2854 		    : JAIL_SYS_INHERIT;
2855 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2856 		if (error != 0 && error != ENOENT)
2857 			goto done;
2858 	}
2859 	for (bf = pr_flag_allow;
2860 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
2861 		atomic_load_int(&bf->flag) != 0;
2862 	     bf++) {
2863 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2864 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2865 		if (error != 0 && error != ENOENT)
2866 			goto done;
2867 		i = !i;
2868 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2869 		if (error != 0 && error != ENOENT)
2870 			goto done;
2871 	}
2872 	i = !prison_isalive(pr);
2873 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2874 	if (error != 0 && error != ENOENT)
2875 		goto done;
2876 	i = !i;
2877 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2878 	if (error != 0 && error != ENOENT)
2879 		goto done;
2880 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2881 	    sizeof(pr->pr_osreldate));
2882 	if (error != 0 && error != ENOENT)
2883 		goto done;
2884 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2885 	if (error != 0 && error != ENOENT)
2886 		goto done;
2887 
2888 #ifdef MAC
2889 	/*
2890 	 * We get the MAC label last because we'll let the MAC framework drop
2891 	 * pr_mtx to externalize the label.
2892 	 */
2893 	error = mac_get_prison(td, pr, opts);
2894 	mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
2895 	drflags &= ~PD_LOCKED;
2896 	if (error != 0 && error != ENOENT)
2897 		goto done;
2898 #else
2899 	mtx_unlock(&pr->pr_mtx);
2900 	drflags &= ~PD_LOCKED;
2901 #endif
2902 
2903 	/* Get the module parameters. */
2904 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2905 	if (error)
2906 		goto done;
2907 	prison_deref(pr, drflags);
2908 	pr = NULL;
2909 	drflags = 0;
2910 
2911 	/* By now, all parameters should have been noted. */
2912 	TAILQ_FOREACH(opt, opts, link) {
2913 		if (!opt->seen &&
2914 		    (strstr(opt->name, JAIL_META_PRIVATE ".") == opt->name ||
2915 		    strstr(opt->name, JAIL_META_SHARED ".") == opt->name)) {
2916 			/* Communicate back a missing key. */
2917 			free(opt->value, M_MOUNT);
2918 			opt->value = NULL;
2919 			opt->len = 0;
2920 			continue;
2921 		}
2922 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2923 			error = EINVAL;
2924 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2925 			goto done;
2926 		}
2927 	}
2928 
2929 	/* Write the fetched parameters back to userspace. */
2930 	error = 0;
2931 	TAILQ_FOREACH(opt, opts, link) {
2932 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2933 			pos = 2 * opt->pos + 1;
2934 			optuio->uio_iov[pos].iov_len = opt->len;
2935 			if (opt->value != NULL) {
2936 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2937 					bcopy(opt->value,
2938 					    optuio->uio_iov[pos].iov_base,
2939 					    opt->len);
2940 				} else {
2941 					error = copyout(opt->value,
2942 					    optuio->uio_iov[pos].iov_base,
2943 					    opt->len);
2944 					if (error)
2945 						break;
2946 				}
2947 			}
2948 		}
2949 	}
2950 
2951  done:
2952 	/* Release any temporary prison holds and/or locks. */
2953 	if (pr != NULL)
2954 		prison_deref(pr, drflags);
2955 	else if (drflags & PD_LIST_SLOCKED)
2956 		sx_sunlock(&allprison_lock);
2957 	else if (drflags & PD_LIST_XLOCKED)
2958 		sx_xunlock(&allprison_lock);
2959 	/* Clean up other resources. */
2960 	if (jfp_out != NULL)
2961 		(void)fdrop(jfp_out, td);
2962 	if (error && jfd_out >= 0)
2963 		(void)kern_close(td, jfd_out);
2964 	if (error && errmsg_pos >= 0) {
2965 		/* Write the error message back to userspace. */
2966 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2967 		errmsg_pos = 2 * errmsg_pos + 1;
2968 		if (errmsg_len > 0) {
2969 			if (optuio->uio_segflg == UIO_SYSSPACE)
2970 				bcopy(errmsg,
2971 				    optuio->uio_iov[errmsg_pos].iov_base,
2972 				    errmsg_len);
2973 			else
2974 				(void)copyout(errmsg,
2975 				    optuio->uio_iov[errmsg_pos].iov_base,
2976 				    errmsg_len);
2977 		}
2978 	}
2979 	vfs_freeopts(opts);
2980 	prison_free(mypr);
2981 	return (error);
2982 }
2983 
2984 /*
2985  * struct jail_remove_args {
2986  *	int jid;
2987  * };
2988  */
2989 int
sys_jail_remove(struct thread * td,struct jail_remove_args * uap)2990 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2991 {
2992 	struct prison *pr;
2993 	int error;
2994 
2995 	error = priv_check(td, PRIV_JAIL_REMOVE);
2996 	if (error)
2997 		return (error);
2998 
2999 	sx_xlock(&allprison_lock);
3000 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
3001 	if (pr == NULL) {
3002 		sx_xunlock(&allprison_lock);
3003 		return (EINVAL);
3004 	}
3005 #ifdef MAC
3006 	error = mac_prison_check_remove(td->td_ucred, pr);
3007 	if (error != 0) {
3008 		mtx_unlock(&pr->pr_mtx);
3009 		sx_xunlock(&allprison_lock);
3010 		return (error);
3011 	}
3012 #endif
3013 	prison_hold(pr);
3014 	prison_remove(pr);
3015 	return (0);
3016 }
3017 
3018 /*
3019  * struct jail_remove_jd_args {
3020  *	int fd;
3021  * };
3022  */
3023 int
sys_jail_remove_jd(struct thread * td,struct jail_remove_jd_args * uap)3024 sys_jail_remove_jd(struct thread *td, struct jail_remove_jd_args *uap)
3025 {
3026 	struct prison *pr;
3027 	struct ucred *jdcred;
3028 	int error;
3029 
3030 	error = jaildesc_find(td, uap->fd, &pr, &jdcred);
3031 	if (error)
3032 		return (error);
3033 	error = priv_check_cred(jdcred, PRIV_JAIL_REMOVE);
3034 	crfree(jdcred);
3035 #ifdef MAC
3036 	if (error == 0)
3037 		error = mac_prison_check_remove(td->td_ucred, pr);
3038 #endif
3039 	if (error) {
3040 		prison_free(pr);
3041 		return (error);
3042 	}
3043 	sx_xlock(&allprison_lock);
3044 	mtx_lock(&pr->pr_mtx);
3045 	prison_remove(pr);
3046 	return (0);
3047 }
3048 
3049 /*
3050  * Begin the removal process for a prison.  The allprison lock should
3051  * be held exclusively, and the prison should be both locked and held.
3052  */
3053 void
prison_remove(struct prison * pr)3054 prison_remove(struct prison *pr)
3055 {
3056 	sx_assert(&allprison_lock, SA_XLOCKED);
3057 	mtx_assert(&pr->pr_mtx, MA_OWNED);
3058 	prison_deref(pr, PD_KILL | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
3059 }
3060 
3061 /*
3062  * struct jail_attach_args {
3063  *	int jid;
3064  * };
3065  */
3066 int
sys_jail_attach(struct thread * td,struct jail_attach_args * uap)3067 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
3068 {
3069 	struct prison *pr;
3070 	int drflags, error;
3071 
3072 	error = priv_check(td, PRIV_JAIL_ATTACH);
3073 	if (error)
3074 		return (error);
3075 	/* Only let a single thread in the process try to attach at a time. */
3076 	error = prison_attach_thread_single(td);
3077 	if (error != 0)
3078 		return (error);
3079 
3080 	sx_slock(&allprison_lock);
3081 	drflags = PD_LIST_SLOCKED;
3082 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
3083 	if (pr == NULL) {
3084 		error = EINVAL;
3085 		goto done;
3086 	}
3087 	drflags |= PD_LOCKED;
3088 
3089 #ifdef MAC
3090 	error = mac_prison_check_attach(td->td_ucred, pr);
3091 	if (error != 0)
3092 		goto done;
3093 #endif
3094 
3095 	/* Do not allow a process to attach to a prison that is not alive. */
3096 	if (!prison_isalive(pr)) {
3097 		error = EINVAL;
3098 		goto done;
3099 	}
3100 
3101 	error = do_jail_attach(td, pr, &drflags);
3102 
3103  done:
3104 	prison_deref(pr, drflags);
3105 	prison_attach_thread_single_end(td);
3106 	return (error);
3107 }
3108 
3109 /*
3110  * struct jail_attach_jd_args {
3111  *	int fd;
3112  * };
3113  */
3114 int
sys_jail_attach_jd(struct thread * td,struct jail_attach_jd_args * uap)3115 sys_jail_attach_jd(struct thread *td, struct jail_attach_jd_args *uap)
3116 {
3117 	struct prison *pr;
3118 	struct ucred *jdcred;
3119 	int drflags, error;
3120 
3121 	/* Only let a single thread in the process try to attach at a time. */
3122 	error = prison_attach_thread_single(td);
3123 	if (error != 0)
3124 		return (error);
3125 
3126 	sx_slock(&allprison_lock);
3127 	drflags = PD_LIST_SLOCKED;
3128 	pr = NULL;
3129 	error = jaildesc_find(td, uap->fd, &pr, &jdcred);
3130 	if (error)
3131 		goto done;
3132 	drflags |= PD_DEREF;
3133 	error = priv_check_cred(jdcred, PRIV_JAIL_ATTACH);
3134 #ifdef MAC
3135 	if (error == 0)
3136 		error = mac_prison_check_attach(td->td_ucred, pr);
3137 #endif
3138 	crfree(jdcred);
3139 	if (error)
3140 		goto done;
3141 
3142 	/* Do not allow a process to attach to a prison that is not alive. */
3143 	if (!prison_isalive(pr)) {
3144 		error = EINVAL;
3145 		goto done;
3146 	}
3147 
3148 	error = do_jail_attach(td, pr, &drflags);
3149 
3150  done:
3151 	prison_deref(pr, drflags);
3152 	prison_attach_thread_single_end(td);
3153 	return (error);
3154 }
3155 
3156 /*
3157  * Attach the current process to a prison.  On entry, the allprison
3158  * lock should be at least shared.  On exit, both it and the prison
3159  * itself will be unlocked, which will be refelected in *drflagsp.
3160  */
3161 static int
do_jail_attach(struct thread * td,struct prison * pr,int * drflagsp)3162 do_jail_attach(struct thread *td, struct prison *pr, int *drflagsp)
3163 {
3164 	struct proc *p;
3165 	struct ucred *newcred, *oldcred;
3166 	int drflags, error;
3167 
3168 	sx_assert(&allprison_lock, SX_LOCKED);
3169 	KASSERT(prison_isvalid(pr), ("Attaching to invalid prison %p", pr));
3170 	/*
3171 	 * Note the caller's locking state, but gain and track our own
3172 	 * references.  The caller will see that locks have been
3173 	 * dropped (which isn't true now, but will be after OSD calls).
3174 	 */
3175 	prison_hold(pr);
3176 	refcount_acquire(&pr->pr_uref);
3177 	drflags = PD_DEREF | PD_DEUREF | (*drflagsp & PD_LOCK_FLAGS);
3178 	*drflagsp &= PD_OP_FLAGS;
3179 
3180 	/* Let modules do whatever they need to prepare for attaching. */
3181 	if (drflags & PD_LOCKED) {
3182 		mtx_unlock(&pr->pr_mtx);
3183 		drflags &= ~PD_LOCKED;
3184 	}
3185 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
3186 	if (error)
3187 		goto e_revert_osd;
3188 	sx_unlock(&allprison_lock);
3189 	drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
3190 
3191 	/*
3192 	 * Reparent the newly attached process to this jail.
3193 	 */
3194 	p = td->td_proc;
3195 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
3196 	if (error)
3197 		goto e_revert_osd;
3198 
3199 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
3200 	if ((error = change_dir(pr->pr_root, td)) != 0)
3201 		goto e_unlock;
3202 #ifdef MAC
3203 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
3204 		goto e_unlock;
3205 #endif
3206 	VOP_UNLOCK(pr->pr_root);
3207 	if ((error = pwd_chroot_chdir(td, pr->pr_root)))
3208 		goto e_revert_osd;
3209 
3210 	newcred = crget();
3211 	PROC_LOCK(p);
3212 	oldcred = crcopysafe(p, newcred);
3213 	newcred->cr_prison = pr;
3214 #ifdef RACCT
3215 	racct_proc_ucred_changed(p, oldcred, newcred);
3216 #endif
3217 #ifdef RCTL
3218 	crhold(newcred);
3219 #endif
3220 	/*
3221 	 * Takes over 'newcred''s reference, so 'newcred' must not be used
3222 	 * besides this point except on RCTL where we took an additional
3223 	 * reference above.
3224 	 */
3225 	proc_set_cred(p, newcred);
3226 	setsugid(p);
3227 	PROC_UNLOCK(p);
3228 #ifdef RCTL
3229 	rctl_proc_ucred_changed(p, newcred);
3230 	crfree(newcred);
3231 #endif
3232 	prison_proc_relink(oldcred->cr_prison, pr, p);
3233 	prison_deref(oldcred->cr_prison, drflags);
3234 	crfree(oldcred);
3235 	prison_knote(pr, NOTE_JAIL_ATTACH | td->td_proc->p_pid);
3236 #ifdef MAC
3237 	/*
3238 	 * Note that mac_prison_attached() assumes that it's called in a
3239 	 * sleepable context.
3240 	 */
3241 	mac_prison_attached(td->td_ucred, pr, td->td_proc);
3242 #endif
3243 
3244 	/*
3245 	 * If the prison was killed while changing credentials, die along
3246 	 * with it.
3247 	 */
3248 	if (!prison_isalive(pr)) {
3249 		PROC_LOCK(p);
3250 		kern_psignal(p, SIGKILL);
3251 		PROC_UNLOCK(p);
3252 	}
3253 
3254 	return (0);
3255 
3256  e_unlock:
3257 	VOP_UNLOCK(pr->pr_root);
3258  e_revert_osd:
3259 	/* Tell modules this process is still in its old jail after all. */
3260 	if (!(drflags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
3261 		sx_slock(&allprison_lock);
3262 		drflags |= PD_LIST_SLOCKED;
3263 	}
3264 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
3265 	prison_deref(pr, drflags);
3266 	return (error);
3267 }
3268 
3269 /*
3270  * Only one thread in a process should try to attach to a jail, or
3271  * they might end up with a process root from one prison, but attached
3272  * to the jail of another.  Enforce this by making the process run in
3273  * single-threaded mode for the duration of the system call, which
3274  * also prevents other related calls such as chroot.
3275  */
3276 static int
prison_attach_thread_single(struct thread * td)3277 prison_attach_thread_single(struct thread *td)
3278 {
3279 	struct proc *p;
3280 	int error;
3281 
3282 	error = 0;
3283 	p = td->td_proc;
3284 	if ((atomic_load_int(&p->p_flag) & P_HADTHREADS) != 0) {
3285 		PROC_LOCK(p);
3286 		if (thread_single(p, SINGLE_BOUNDARY))
3287 			error = ERESTART;
3288 		PROC_UNLOCK(p);
3289 	}
3290 	return (error);
3291 }
3292 
3293 static void
prison_attach_thread_single_end(struct thread * td)3294 prison_attach_thread_single_end(struct thread *td)
3295 {
3296 	struct proc *p;
3297 
3298 	p = td->td_proc;
3299 	if ((atomic_load_int(&p->p_flag) & P_HADTHREADS) != 0) {
3300 		PROC_LOCK(p);
3301 		thread_single_end(p, SINGLE_BOUNDARY);
3302 		PROC_UNLOCK(p);
3303 	}
3304 }
3305 
3306 /*
3307  * Returns a locked prison instance, or NULL on failure.
3308  */
3309 struct prison *
prison_find(int prid)3310 prison_find(int prid)
3311 {
3312 	struct prison *pr;
3313 
3314 	sx_assert(&allprison_lock, SX_LOCKED);
3315 	TAILQ_FOREACH(pr, &allprison, pr_list) {
3316 		if (pr->pr_id < prid)
3317 			continue;
3318 		if (pr->pr_id > prid)
3319 			break;
3320 		KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
3321 		mtx_lock(&pr->pr_mtx);
3322 		return (pr);
3323 	}
3324 	return (NULL);
3325 }
3326 
3327 /*
3328  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
3329  */
3330 struct prison *
prison_find_child(struct prison * mypr,int prid)3331 prison_find_child(struct prison *mypr, int prid)
3332 {
3333 	struct prison *pr;
3334 	int descend;
3335 
3336 	sx_assert(&allprison_lock, SX_LOCKED);
3337 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
3338 		if (pr->pr_id == prid) {
3339 			KASSERT(prison_isvalid(pr),
3340 			    ("Found invalid prison %p", pr));
3341 			mtx_lock(&pr->pr_mtx);
3342 			return (pr);
3343 		}
3344 	}
3345 	return (NULL);
3346 }
3347 
3348 /*
3349  * Look for the name relative to mypr.  Returns a locked prison or NULL.
3350  */
3351 struct prison *
prison_find_name(struct prison * mypr,const char * name)3352 prison_find_name(struct prison *mypr, const char *name)
3353 {
3354 	struct prison *pr, *deadpr;
3355 	size_t mylen;
3356 	int descend;
3357 
3358 	sx_assert(&allprison_lock, SX_LOCKED);
3359 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
3360 	deadpr = NULL;
3361 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
3362 		if (!strcmp(pr->pr_name + mylen, name)) {
3363 			KASSERT(prison_isvalid(pr),
3364 			    ("Found invalid prison %p", pr));
3365 			if (prison_isalive(pr)) {
3366 				mtx_lock(&pr->pr_mtx);
3367 				return (pr);
3368 			}
3369 			deadpr = pr;
3370 		}
3371 	}
3372 	/* There was no valid prison - perhaps there was a dying one. */
3373 	if (deadpr != NULL)
3374 		mtx_lock(&deadpr->pr_mtx);
3375 	return (deadpr);
3376 }
3377 
3378 /*
3379  * See if a prison has the specific flag set.  The prison should be locked,
3380  * unless checking for flags that are only set at jail creation (such as
3381  * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
3382  * to any other prison data.
3383  */
3384 bool
prison_flag(struct ucred * cred,unsigned flag)3385 prison_flag(struct ucred *cred, unsigned flag)
3386 {
3387 
3388 	return ((cred->cr_prison->pr_flags & flag) != 0);
3389 }
3390 
3391 /*
3392  * See if a prison has the specific allow flag set.
3393  * The prison *should* be locked, or only a single bit is examined, without
3394  * regard to any other prison data.
3395  */
3396 bool
prison_allow(struct ucred * cred,unsigned flag)3397 prison_allow(struct ucred *cred, unsigned flag)
3398 {
3399 
3400 	return ((cred->cr_prison->pr_allow & flag) != 0);
3401 }
3402 
3403 /*
3404  * Hold a prison reference, by incrementing pr_ref.  It is generally
3405  * an error to hold a prison that does not already have a reference.
3406  * A prison record will remain valid as long as it has at least one
3407  * reference, and will not be removed as long as either the prison
3408  * mutex or the allprison lock is held (allprison_lock may be shared).
3409  */
3410 void
prison_hold_locked(struct prison * pr)3411 prison_hold_locked(struct prison *pr)
3412 {
3413 
3414 	/* Locking is no longer required. */
3415 	prison_hold(pr);
3416 }
3417 
3418 void
prison_hold(struct prison * pr)3419 prison_hold(struct prison *pr)
3420 {
3421 #ifdef INVARIANTS
3422 	int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
3423 
3424 	KASSERT(was_valid,
3425 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
3426 #else
3427 	refcount_acquire(&pr->pr_ref);
3428 #endif
3429 }
3430 
3431 /*
3432  * Remove a prison reference.  If that was the last reference, the
3433  * prison will be removed (at a later time).
3434  */
3435 void
prison_free_locked(struct prison * pr)3436 prison_free_locked(struct prison *pr)
3437 {
3438 
3439 	mtx_assert(&pr->pr_mtx, MA_OWNED);
3440 	/*
3441 	 * Locking is no longer required, but unlock because the caller
3442 	 * expects it.
3443 	 */
3444 	mtx_unlock(&pr->pr_mtx);
3445 	prison_free(pr);
3446 }
3447 
3448 void
prison_free(struct prison * pr)3449 prison_free(struct prison *pr)
3450 {
3451 
3452 	KASSERT(refcount_load(&pr->pr_ref) > 0,
3453 	    ("Trying to free dead prison %p (jid=%d).",
3454 	     pr, pr->pr_id));
3455 	if (!refcount_release_if_not_last(&pr->pr_ref)) {
3456 		/*
3457 		 * Don't remove the last reference in this context,
3458 		 * in case there are locks held.
3459 		 */
3460 		taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task);
3461 	}
3462 }
3463 
3464 static void
prison_free_not_last(struct prison * pr)3465 prison_free_not_last(struct prison *pr)
3466 {
3467 #ifdef INVARIANTS
3468 	int lastref;
3469 
3470 	KASSERT(refcount_load(&pr->pr_ref) > 0,
3471 	    ("Trying to free dead prison %p (jid=%d).",
3472 	     pr, pr->pr_id));
3473 	lastref = refcount_release(&pr->pr_ref);
3474 	KASSERT(!lastref,
3475 	    ("prison_free_not_last freed last ref on prison %p (jid=%d).",
3476 	     pr, pr->pr_id));
3477 #else
3478 	refcount_release(&pr->pr_ref);
3479 #endif
3480 }
3481 
3482 /*
3483  * Hold a prison for user visibility, by incrementing pr_uref.
3484  * It is generally an error to hold a prison that isn't already
3485  * user-visible, except through the jail system calls.  It is also
3486  * an error to hold an invalid prison.  A prison record will remain
3487  * alive as long as it has at least one user reference, and will not
3488  * be set to the dying state until the prison mutex and allprison_lock
3489  * are both freed.
3490  */
3491 void
prison_proc_hold(struct prison * pr)3492 prison_proc_hold(struct prison *pr)
3493 {
3494 #ifdef INVARIANTS
3495 	int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
3496 
3497 	KASSERT(was_alive,
3498 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
3499 #else
3500 	refcount_acquire(&pr->pr_uref);
3501 #endif
3502 }
3503 
3504 /*
3505  * Remove a prison user reference.  If it was the last reference, the
3506  * prison will be considered "dying", and may be removed once all of
3507  * its references are dropped.
3508  */
3509 void
prison_proc_free(struct prison * pr)3510 prison_proc_free(struct prison *pr)
3511 {
3512 
3513 	/*
3514 	 * Locking is only required when releasing the last reference.
3515 	 * This allows assurance that a locked prison will remain alive
3516 	 * until it is unlocked.
3517 	 */
3518 	KASSERT(refcount_load(&pr->pr_uref) > 0,
3519 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
3520 	if (!refcount_release_if_not_last(&pr->pr_uref)) {
3521 		/*
3522 		 * Don't remove the last user reference in this context,
3523 		 * which is expected to be a process that is not only locked,
3524 		 * but also half dead.  Add a reference so any calls to
3525 		 * prison_free() won't re-submit the task.
3526 		 */
3527 		prison_hold(pr);
3528 		mtx_lock(&pr->pr_mtx);
3529 		KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
3530 		    ("Redundant last reference in prison_proc_free (jid=%d)",
3531 		     pr->pr_id));
3532 		pr->pr_flags |= PR_COMPLETE_PROC;
3533 		mtx_unlock(&pr->pr_mtx);
3534 		taskqueue_enqueue(taskqueue_jail_remove, &pr->pr_task);
3535 	}
3536 }
3537 
3538 static void
prison_proc_free_not_last(struct prison * pr)3539 prison_proc_free_not_last(struct prison *pr)
3540 {
3541 #ifdef INVARIANTS
3542 	int lastref;
3543 
3544 	KASSERT(refcount_load(&pr->pr_uref) > 0,
3545 	    ("Trying to free dead prison %p (jid=%d).",
3546 	     pr, pr->pr_id));
3547 	lastref = refcount_release(&pr->pr_uref);
3548 	KASSERT(!lastref,
3549 	    ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
3550 	     pr, pr->pr_id));
3551 #else
3552 	refcount_release(&pr->pr_uref);
3553 #endif
3554 }
3555 
3556 void
prison_proc_link(struct prison * pr,struct proc * p)3557 prison_proc_link(struct prison *pr, struct proc *p)
3558 {
3559 
3560 	sx_assert(&allproc_lock, SA_XLOCKED);
3561 	LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist);
3562 }
3563 
3564 void
prison_proc_unlink(struct prison * pr,struct proc * p)3565 prison_proc_unlink(struct prison *pr, struct proc *p)
3566 {
3567 
3568 	sx_assert(&allproc_lock, SA_XLOCKED);
3569 	LIST_REMOVE(p, p_jaillist);
3570 }
3571 
3572 static void
prison_proc_relink(struct prison * opr,struct prison * npr,struct proc * p)3573 prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p)
3574 {
3575 
3576 	sx_xlock(&allproc_lock);
3577 	prison_proc_unlink(opr, p);
3578 	prison_proc_link(npr, p);
3579 	sx_xunlock(&allproc_lock);
3580 }
3581 
3582 /*
3583  * Complete a call to either prison_free or prison_proc_free.
3584  */
3585 static void
prison_complete(void * context,int pending)3586 prison_complete(void *context, int pending)
3587 {
3588 	struct prison *pr = context;
3589 	int drflags;
3590 
3591 	/*
3592 	 * This could be called to release the last reference, or the last
3593 	 * user reference (plus the reference held in prison_proc_free).
3594 	 */
3595 	drflags = prison_lock_xlock(pr, PD_DEREF);
3596 	if (pr->pr_flags & PR_COMPLETE_PROC) {
3597 		pr->pr_flags &= ~PR_COMPLETE_PROC;
3598 		drflags |= PD_DEUREF;
3599 	}
3600 	prison_deref(pr, drflags);
3601 }
3602 
3603 static void
prison_kill_processes_cb(struct proc * p,void * arg __unused)3604 prison_kill_processes_cb(struct proc *p, void *arg __unused)
3605 {
3606 
3607 	kern_psignal(p, SIGKILL);
3608 }
3609 
3610 /*
3611  * Note the iteration does not guarantee acting on all processes.
3612  * Most notably there may be fork or jail_attach in progress.
3613  */
3614 void
prison_proc_iterate(struct prison * pr,void (* cb)(struct proc *,void *),void * cbarg)3615 prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *),
3616     void *cbarg)
3617 {
3618 	struct prison *ppr;
3619 	struct proc *p;
3620 
3621 	if (atomic_load_int(&pr->pr_childcount) == 0) {
3622 		sx_slock(&allproc_lock);
3623 		LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) {
3624 			if (p->p_state == PRS_NEW)
3625 				continue;
3626 			PROC_LOCK(p);
3627 			cb(p, cbarg);
3628 			PROC_UNLOCK(p);
3629 		}
3630 		sx_sunlock(&allproc_lock);
3631 		if (atomic_load_int(&pr->pr_childcount) == 0)
3632 			return;
3633 		/*
3634 		 * Some jails popped up during the iteration, fall through to a
3635 		 * system-wide search.
3636 		 */
3637 	}
3638 
3639 	sx_slock(&allproc_lock);
3640 	FOREACH_PROC_IN_SYSTEM(p) {
3641 		PROC_LOCK(p);
3642 		if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
3643 			for (ppr = p->p_ucred->cr_prison; ppr != NULL;
3644 			    ppr = ppr->pr_parent) {
3645 				if (ppr == pr) {
3646 					cb(p, cbarg);
3647 					break;
3648 				}
3649 			}
3650 		}
3651 		PROC_UNLOCK(p);
3652 	}
3653 	sx_sunlock(&allproc_lock);
3654 }
3655 
3656 /*
3657  * Remove a prison reference and/or user reference (usually).
3658  * This assumes context that allows sleeping (for allprison_lock),
3659  * with no non-sleeping locks held, except perhaps the prison itself.
3660  * If there are no more references, release and delist the prison.
3661  * On completion, the prison lock and the allprison lock are both
3662  * unlocked.
3663  */
3664 static void
prison_deref(struct prison * pr,int flags)3665 prison_deref(struct prison *pr, int flags)
3666 {
3667 	struct prisonlist freeprison;
3668 	struct prison *killpr, *rpr, *ppr, *tpr;
3669 
3670 	killpr = NULL;
3671 	TAILQ_INIT(&freeprison);
3672 	/*
3673 	 * Release this prison as requested, which may cause its parent
3674 	 * to be released, and then maybe its grandparent, etc.
3675 	 */
3676 	for (;;) {
3677 		if (flags & PD_KILL) {
3678 			/* Kill the prison and its descendents. */
3679 			KASSERT(pr != &prison0,
3680 			    ("prison_deref trying to kill prison0"));
3681 			if (!prison_isalive(pr)) {
3682 				/* Silently ignore already-dying prisons. */
3683 				flags &= ~PD_KILL;
3684 			} else {
3685 				if (!(flags & PD_DEREF)) {
3686 					prison_hold(pr);
3687 					flags |= PD_DEREF;
3688 				}
3689 				flags = prison_lock_xlock(pr, flags);
3690 				prison_deref_kill(pr, &freeprison);
3691 			}
3692 		}
3693 		if (flags & PD_DEUREF) {
3694 			/* Drop a user reference. */
3695 			KASSERT(refcount_load(&pr->pr_uref) > 0,
3696 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
3697 			     pr->pr_id));
3698 			if (!refcount_release_if_not_last(&pr->pr_uref)) {
3699 				if (!(flags & PD_DEREF)) {
3700 					prison_hold(pr);
3701 					flags |= PD_DEREF;
3702 				}
3703 				flags = prison_lock_xlock(pr, flags);
3704 				if (refcount_release(&pr->pr_uref) &&
3705 				    pr->pr_state == PRISON_STATE_ALIVE) {
3706 					/*
3707 					 * When the last user references goes,
3708 					 * this becomes a dying prison.
3709 					 */
3710 					KASSERT(
3711 					    refcount_load(&prison0.pr_uref) > 0,
3712 					    ("prison0 pr_uref=0"));
3713 					pr->pr_state = PRISON_STATE_DYING;
3714 					prison_cleanup_locked(pr);
3715 					mtx_unlock(&pr->pr_mtx);
3716 					flags &= ~PD_LOCKED;
3717 					prison_cleanup_unlocked(pr);
3718 				}
3719 			}
3720 		}
3721 		if (flags & PD_KILL) {
3722 			/*
3723 			 * Any remaining user references are probably processes
3724 			 * that need to be killed, either in this prison or its
3725 			 * descendants.
3726 			 */
3727 			if (refcount_load(&pr->pr_uref) > 0)
3728 				killpr = pr;
3729 			/* Make sure the parent prison doesn't get killed. */
3730 			flags &= ~PD_KILL;
3731 		}
3732 		if (flags & PD_DEREF) {
3733 			/* Drop a reference. */
3734 			KASSERT(refcount_load(&pr->pr_ref) > 0,
3735 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
3736 			     pr->pr_id));
3737 			if (!refcount_release_if_not_last(&pr->pr_ref)) {
3738 				flags = prison_lock_xlock(pr, flags);
3739 				if (refcount_release(&pr->pr_ref)) {
3740 					/*
3741 					 * When the last reference goes,
3742 					 * unlink the prison and set it aside.
3743 					 */
3744 					KASSERT(
3745 					    refcount_load(&pr->pr_uref) == 0,
3746 					    ("prison_deref: last ref, "
3747 					     "but still has %d urefs (jid=%d)",
3748 					     pr->pr_uref, pr->pr_id));
3749 					KASSERT(
3750 					    refcount_load(&prison0.pr_ref) != 0,
3751 					    ("prison0 pr_ref=0"));
3752 #ifdef MAC
3753 					/*
3754 					 * The MAC framework will call into any
3755 					 * policies that want to hook
3756 					 * prison_destroy_label, so ideally we
3757 					 * call this prior to any final state
3758 					 * invalidation to be safe.
3759 					 */
3760 					mac_prison_destroy(pr);
3761 #endif
3762 					pr->pr_state = PRISON_STATE_INVALID;
3763 					TAILQ_REMOVE(&allprison, pr, pr_list);
3764 					LIST_REMOVE(pr, pr_sibling);
3765 					TAILQ_INSERT_TAIL(&freeprison, pr,
3766 					    pr_list);
3767 					for (ppr = pr->pr_parent;
3768 					     ppr != NULL;
3769 					     ppr = ppr->pr_parent)
3770 						ppr->pr_childcount--;
3771 					/*
3772 					 * Removing a prison frees references
3773 					 * from its parent.
3774 					 */
3775 					ppr = pr->pr_parent;
3776 					pr->pr_parent = NULL;
3777 					mtx_unlock(&pr->pr_mtx);
3778 
3779 					pr = ppr;
3780 					flags &= ~PD_LOCKED;
3781 					flags |= PD_DEREF | PD_DEUREF;
3782 					continue;
3783 				}
3784 			}
3785 		}
3786 		break;
3787 	}
3788 
3789 	/* Release all the prison locks. */
3790 	if (flags & PD_LOCKED)
3791 		mtx_unlock(&pr->pr_mtx);
3792 	if (flags & PD_LIST_SLOCKED)
3793 		sx_sunlock(&allprison_lock);
3794 	else if (flags & PD_LIST_XLOCKED)
3795 		sx_xunlock(&allprison_lock);
3796 
3797 	/* Kill any processes attached to a killed prison. */
3798 	if (killpr != NULL)
3799 		prison_proc_iterate(killpr, prison_kill_processes_cb, NULL);
3800 
3801 	/*
3802 	 * Finish removing any unreferenced prisons, which couldn't happen
3803 	 * while allprison_lock was held (to avoid a LOR on vrele).
3804 	 */
3805 	TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
3806 #ifdef VIMAGE
3807 		if (rpr->pr_flags & PR_VNET)
3808 			vnet_destroy(rpr->pr_vnet);
3809 #endif
3810 		if (rpr->pr_root != NULL)
3811 			vrele(rpr->pr_root);
3812 		mtx_destroy(&rpr->pr_mtx);
3813 #ifdef INET
3814 		prison_ip_free(rpr->pr_addrs[PR_INET]);
3815 #endif
3816 #ifdef INET6
3817 		prison_ip_free(rpr->pr_addrs[PR_INET6]);
3818 #endif
3819 		if (rpr->pr_cpuset != NULL)
3820 			cpuset_rel(rpr->pr_cpuset);
3821 		osd_jail_exit(rpr);
3822 #ifdef RACCT
3823 		if (racct_enable)
3824 			prison_racct_detach(rpr);
3825 #endif
3826 		TAILQ_REMOVE(&freeprison, rpr, pr_list);
3827 		free(rpr, M_PRISON);
3828 	}
3829 }
3830 
3831 /*
3832  * Kill the prison and its descendants.  Mark them as dying, clear the
3833  * persist flag, and call module remove methods.
3834  */
3835 static void
prison_deref_kill(struct prison * pr,struct prisonlist * freeprison)3836 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3837 {
3838 	struct prison *cpr, *ppr, *rpr;
3839 	bool descend;
3840 
3841 	/*
3842 	 * Unlike the descendants, the target prison can be killed
3843 	 * even if it is currently dying.  This is useful for failed
3844 	 * creation in jail_set(2).
3845 	 */
3846 	KASSERT(refcount_load(&pr->pr_ref) > 0,
3847 	    ("Trying to kill dead prison %p (jid=%d).",
3848 	     pr, pr->pr_id));
3849 	refcount_acquire(&pr->pr_uref);
3850 	pr->pr_state = PRISON_STATE_DYING;
3851 	mtx_unlock(&pr->pr_mtx);
3852 
3853 	rpr = NULL;
3854 	FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3855 		if (descend) {
3856 			if (!prison_isalive(cpr)) {
3857 				descend = false;
3858 				continue;
3859 			}
3860 			prison_hold(cpr);
3861 			prison_proc_hold(cpr);
3862 			mtx_lock(&cpr->pr_mtx);
3863 			cpr->pr_state = PRISON_STATE_DYING;
3864 			cpr->pr_flags |= PR_REMOVE;
3865 			mtx_unlock(&cpr->pr_mtx);
3866 			continue;
3867 		}
3868 		if (!(cpr->pr_flags & PR_REMOVE))
3869 			continue;
3870 		prison_cleanup_unlocked(cpr);
3871 		mtx_lock(&cpr->pr_mtx);
3872 		prison_cleanup_locked(cpr);
3873 		cpr->pr_flags &= ~PR_REMOVE;
3874 		if (cpr->pr_flags & PR_PERSIST) {
3875 			cpr->pr_flags &= ~PR_PERSIST;
3876 			prison_proc_free_not_last(cpr);
3877 			prison_free_not_last(cpr);
3878 		}
3879 		(void)refcount_release(&cpr->pr_uref);
3880 		if (refcount_release(&cpr->pr_ref)) {
3881 			/*
3882 			 * When the last reference goes, unlink the prison
3883 			 * and set it aside for prison_deref() to handle.
3884 			 * Delay unlinking the sibling list to keep the loop
3885 			 * safe.
3886 			 */
3887 			if (rpr != NULL)
3888 				LIST_REMOVE(rpr, pr_sibling);
3889 			rpr = cpr;
3890 			rpr->pr_state = PRISON_STATE_INVALID;
3891 			TAILQ_REMOVE(&allprison, rpr, pr_list);
3892 			TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3893 			/*
3894 			 * Removing a prison frees references from its parent.
3895 			 */
3896 			ppr = rpr->pr_parent;
3897 			prison_proc_free_not_last(ppr);
3898 			prison_free_not_last(ppr);
3899 			for (; ppr != NULL; ppr = ppr->pr_parent)
3900 				ppr->pr_childcount--;
3901 		}
3902 		mtx_unlock(&cpr->pr_mtx);
3903 	}
3904 	if (rpr != NULL)
3905 		LIST_REMOVE(rpr, pr_sibling);
3906 
3907 	prison_cleanup_unlocked(pr);
3908 	mtx_lock(&pr->pr_mtx);
3909 	prison_cleanup_locked(pr);
3910 	if (pr->pr_flags & PR_PERSIST) {
3911 		pr->pr_flags &= ~PR_PERSIST;
3912 		prison_proc_free_not_last(pr);
3913 		prison_free_not_last(pr);
3914 	}
3915 	(void)refcount_release(&pr->pr_uref);
3916 }
3917 
3918 /*
3919  * Given the current locking state in the flags, make sure allprison_lock
3920  * is held exclusive, and the prison is locked.  Return flags indicating
3921  * the new state.
3922  */
3923 static int
prison_lock_xlock(struct prison * pr,int flags)3924 prison_lock_xlock(struct prison *pr, int flags)
3925 {
3926 
3927 	if (!(flags & PD_LIST_XLOCKED)) {
3928 		/*
3929 		 * Get allprison_lock, which may be an upgrade,
3930 		 * and may require unlocking the prison.
3931 		 */
3932 		if (flags & PD_LOCKED) {
3933 			mtx_unlock(&pr->pr_mtx);
3934 			flags &= ~PD_LOCKED;
3935 		}
3936 		if (flags & PD_LIST_SLOCKED) {
3937 			if (!sx_try_upgrade(&allprison_lock)) {
3938 				sx_sunlock(&allprison_lock);
3939 				sx_xlock(&allprison_lock);
3940 			}
3941 			flags &= ~PD_LIST_SLOCKED;
3942 		} else
3943 			sx_xlock(&allprison_lock);
3944 		flags |= PD_LIST_XLOCKED;
3945 	}
3946 	if (!(flags & PD_LOCKED)) {
3947 		/* Lock the prison mutex. */
3948 		mtx_lock(&pr->pr_mtx);
3949 		flags |= PD_LOCKED;
3950 	}
3951 	return flags;
3952 }
3953 
3954 /*
3955  * Release a prison's resources when it starts dying (when the last user
3956  * reference is dropped, or when it is killed).  Two functions are called,
3957  * for work that requires a locked prison or an unlocked one.
3958  */
3959 static void
prison_cleanup_locked(struct prison * pr)3960 prison_cleanup_locked(struct prison *pr)
3961 {
3962 	sx_assert(&allprison_lock, SA_XLOCKED);
3963 	mtx_assert(&pr->pr_mtx, MA_OWNED);
3964 	prison_knote(pr, NOTE_JAIL_REMOVE);
3965 	knlist_detach(pr->pr_klist);
3966 	jaildesc_prison_cleanup(pr);
3967 	pr->pr_klist = NULL;
3968 }
3969 
3970 static void
prison_cleanup_unlocked(struct prison * pr)3971 prison_cleanup_unlocked(struct prison *pr)
3972 {
3973 	sx_assert(&allprison_lock, SA_XLOCKED);
3974 	mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
3975 	vfs_exjail_delete(pr);
3976 	shm_remove_prison(pr);
3977 	(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3978 }
3979 
3980 /*
3981  * Set or clear a permission bit in the pr_allow field, passing restrictions
3982  * (cleared permission) down to child jails.
3983  */
3984 void
prison_set_allow(struct ucred * cred,unsigned flag,int enable)3985 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3986 {
3987 	struct prison *pr;
3988 
3989 	pr = cred->cr_prison;
3990 	sx_slock(&allprison_lock);
3991 	mtx_lock(&pr->pr_mtx);
3992 	prison_set_allow_locked(pr, flag, enable);
3993 	mtx_unlock(&pr->pr_mtx);
3994 	sx_sunlock(&allprison_lock);
3995 }
3996 
3997 static void
prison_set_allow_locked(struct prison * pr,unsigned flag,int enable)3998 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3999 {
4000 	struct prison *cpr;
4001 	int descend;
4002 
4003 	if (enable != 0)
4004 		pr->pr_allow |= flag;
4005 	else {
4006 		pr->pr_allow &= ~flag;
4007 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
4008 			cpr->pr_allow &= ~flag;
4009 	}
4010 }
4011 
4012 /*
4013  * Check if a jail supports the given address family.
4014  *
4015  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
4016  * if not.
4017  */
4018 int
prison_check_af(struct ucred * cred,int af)4019 prison_check_af(struct ucred *cred, int af)
4020 {
4021 	struct prison *pr;
4022 	int error;
4023 
4024 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
4025 
4026 	pr = cred->cr_prison;
4027 #ifdef VIMAGE
4028 	/* Prisons with their own network stack are not limited. */
4029 	if (prison_owns_vnet(pr))
4030 		return (0);
4031 #endif
4032 
4033 	error = 0;
4034 	switch (af)
4035 	{
4036 #ifdef INET
4037 	case AF_INET:
4038 		if (pr->pr_flags & PR_IP4)
4039 		{
4040 			mtx_lock(&pr->pr_mtx);
4041 			if ((pr->pr_flags & PR_IP4) &&
4042 			    pr->pr_addrs[PR_INET] == NULL)
4043 				error = EAFNOSUPPORT;
4044 			mtx_unlock(&pr->pr_mtx);
4045 		}
4046 		break;
4047 #endif
4048 #ifdef INET6
4049 	case AF_INET6:
4050 		if (pr->pr_flags & PR_IP6)
4051 		{
4052 			mtx_lock(&pr->pr_mtx);
4053 			if ((pr->pr_flags & PR_IP6) &&
4054 			    pr->pr_addrs[PR_INET6] == NULL)
4055 				error = EAFNOSUPPORT;
4056 			mtx_unlock(&pr->pr_mtx);
4057 		}
4058 		break;
4059 #endif
4060 	case AF_LOCAL:
4061 	case AF_ROUTE:
4062 	case AF_NETLINK:
4063 		break;
4064 	default:
4065 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
4066 			error = EAFNOSUPPORT;
4067 	}
4068 	return (error);
4069 }
4070 
4071 /*
4072  * Check if given address belongs to the jail referenced by cred (wrapper to
4073  * prison_check_ip[46]).
4074  *
4075  * Returns 0 if jail doesn't restrict the address family or if address belongs
4076  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
4077  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
4078  */
4079 int
prison_if(struct ucred * cred,const struct sockaddr * sa)4080 prison_if(struct ucred *cred, const struct sockaddr *sa)
4081 {
4082 #ifdef INET
4083 	const struct sockaddr_in *sai;
4084 #endif
4085 #ifdef INET6
4086 	const struct sockaddr_in6 *sai6;
4087 #endif
4088 	int error;
4089 
4090 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
4091 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
4092 
4093 #ifdef VIMAGE
4094 	if (prison_owns_vnet(cred->cr_prison))
4095 		return (0);
4096 #endif
4097 
4098 	error = 0;
4099 	switch (sa->sa_family)
4100 	{
4101 #ifdef INET
4102 	case AF_INET:
4103 		sai = (const struct sockaddr_in *)sa;
4104 		error = prison_check_ip4(cred, &sai->sin_addr);
4105 		break;
4106 #endif
4107 #ifdef INET6
4108 	case AF_INET6:
4109 		sai6 = (const struct sockaddr_in6 *)sa;
4110 		error = prison_check_ip6(cred, &sai6->sin6_addr);
4111 		break;
4112 #endif
4113 	default:
4114 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
4115 			error = EAFNOSUPPORT;
4116 	}
4117 	return (error);
4118 }
4119 
4120 /*
4121  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
4122  */
4123 int
prison_check(struct ucred * cred1,struct ucred * cred2)4124 prison_check(struct ucred *cred1, struct ucred *cred2)
4125 {
4126 
4127 	return ((cred1->cr_prison == cred2->cr_prison ||
4128 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
4129 }
4130 
4131 /*
4132  * For mountd/nfsd to run within a prison, it must be:
4133  * - A vnet prison.
4134  * - PR_ALLOW_NFSD must be set on it.
4135  * - The root directory (pr_root) of the prison must be
4136  *   a file system mount point, so the mountd can hang
4137  *   export information on it.
4138  * - The prison's enforce_statfs cannot be 0, so that
4139  *   mountd(8) can do exports.
4140  */
4141 bool
prison_check_nfsd(struct ucred * cred)4142 prison_check_nfsd(struct ucred *cred)
4143 {
4144 
4145 	if (jailed_without_vnet(cred))
4146 		return (false);
4147 	if (!prison_allow(cred, PR_ALLOW_NFSD))
4148 		return (false);
4149 	if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0)
4150 		return (false);
4151 	if (cred->cr_prison->pr_enforce_statfs == 0)
4152 		return (false);
4153 	return (true);
4154 }
4155 
4156 /*
4157  * Return true if p2 is a child of p1, otherwise false.
4158  */
4159 bool
prison_ischild(struct prison * pr1,struct prison * pr2)4160 prison_ischild(struct prison *pr1, struct prison *pr2)
4161 {
4162 
4163 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
4164 		if (pr1 == pr2)
4165 			return (true);
4166 	return (false);
4167 }
4168 
4169 /*
4170  * Return true if the prison is currently alive.  A prison is alive if it
4171  * holds user references and it isn't being removed.
4172  */
4173 bool
prison_isalive(const struct prison * pr)4174 prison_isalive(const struct prison *pr)
4175 {
4176 
4177 	if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
4178 		return (false);
4179 	return (true);
4180 }
4181 
4182 /*
4183  * Return true if the prison is currently valid.  A prison is valid if it has
4184  * been fully created, and is not being destroyed.  Note that dying prisons
4185  * are still considered valid.  Invalid prisons won't be found under normal
4186  * circumstances, as they're only put in that state by functions that have
4187  * an exclusive hold on allprison_lock.
4188  */
4189 bool
prison_isvalid(struct prison * pr)4190 prison_isvalid(struct prison *pr)
4191 {
4192 
4193 	if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
4194 		return (false);
4195 	if (__predict_false(refcount_load(&pr->pr_ref) == 0))
4196 		return (false);
4197 	return (true);
4198 }
4199 
4200 /*
4201  * Return true if the passed credential is in a jail and that jail does not
4202  * have its own virtual network stack, otherwise false.
4203  */
4204 bool
jailed_without_vnet(struct ucred * cred)4205 jailed_without_vnet(struct ucred *cred)
4206 {
4207 
4208 	if (!jailed(cred))
4209 		return (false);
4210 #ifdef VIMAGE
4211 	if (prison_owns_vnet(cred->cr_prison))
4212 		return (false);
4213 #endif
4214 
4215 	return (true);
4216 }
4217 
4218 /*
4219  * Return the correct hostname (domainname, et al) for the passed credential.
4220  */
4221 void
getcredhostname(struct ucred * cred,char * buf,size_t size)4222 getcredhostname(struct ucred *cred, char *buf, size_t size)
4223 {
4224 	struct prison *pr;
4225 
4226 	/*
4227 	 * A NULL credential can be used to shortcut to the physical
4228 	 * system's hostname.
4229 	 */
4230 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
4231 	mtx_lock(&pr->pr_mtx);
4232 	strlcpy(buf, pr->pr_hostname, size);
4233 	mtx_unlock(&pr->pr_mtx);
4234 }
4235 
4236 void
getcreddomainname(struct ucred * cred,char * buf,size_t size)4237 getcreddomainname(struct ucred *cred, char *buf, size_t size)
4238 {
4239 
4240 	mtx_lock(&cred->cr_prison->pr_mtx);
4241 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
4242 	mtx_unlock(&cred->cr_prison->pr_mtx);
4243 }
4244 
4245 void
getcredhostuuid(struct ucred * cred,char * buf,size_t size)4246 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
4247 {
4248 
4249 	mtx_lock(&cred->cr_prison->pr_mtx);
4250 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
4251 	mtx_unlock(&cred->cr_prison->pr_mtx);
4252 }
4253 
4254 void
getcredhostid(struct ucred * cred,unsigned long * hostid)4255 getcredhostid(struct ucred *cred, unsigned long *hostid)
4256 {
4257 
4258 	mtx_lock(&cred->cr_prison->pr_mtx);
4259 	*hostid = cred->cr_prison->pr_hostid;
4260 	mtx_unlock(&cred->cr_prison->pr_mtx);
4261 }
4262 
4263 void
getjailname(struct ucred * cred,char * name,size_t len)4264 getjailname(struct ucred *cred, char *name, size_t len)
4265 {
4266 
4267 	mtx_lock(&cred->cr_prison->pr_mtx);
4268 	strlcpy(name, cred->cr_prison->pr_name, len);
4269 	mtx_unlock(&cred->cr_prison->pr_mtx);
4270 }
4271 
4272 #ifdef VIMAGE
4273 /*
4274  * Determine whether the prison owns its VNET.
4275  */
4276 bool
prison_owns_vnet(struct prison * pr)4277 prison_owns_vnet(struct prison *pr)
4278 {
4279 
4280 	/*
4281 	 * vnets cannot be added/removed after jail creation,
4282 	 * so no need to lock here.
4283 	 */
4284 	return ((pr->pr_flags & PR_VNET) != 0);
4285 }
4286 #endif
4287 
4288 /*
4289  * Determine whether the subject represented by cred can "see"
4290  * status of a mount point.
4291  * Returns: 0 for permitted, ENOENT otherwise.
4292  * XXX: This function should be called cr_canseemount() and should be
4293  *      placed in kern_prot.c.
4294  */
4295 int
prison_canseemount(struct ucred * cred,struct mount * mp)4296 prison_canseemount(struct ucred *cred, struct mount *mp)
4297 {
4298 	struct prison *pr;
4299 	struct statfs *sp;
4300 	size_t len;
4301 
4302 	pr = cred->cr_prison;
4303 	if (pr->pr_enforce_statfs == 0)
4304 		return (0);
4305 	if (pr->pr_root->v_mount == mp)
4306 		return (0);
4307 	if (pr->pr_enforce_statfs == 2)
4308 		return (ENOENT);
4309 	/*
4310 	 * If jail's chroot directory is set to "/" we should be able to see
4311 	 * all mount-points from inside a jail.
4312 	 * This is ugly check, but this is the only situation when jail's
4313 	 * directory ends with '/'.
4314 	 */
4315 	if (strcmp(pr->pr_path, "/") == 0)
4316 		return (0);
4317 	len = strlen(pr->pr_path);
4318 	sp = &mp->mnt_stat;
4319 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
4320 		return (ENOENT);
4321 	/*
4322 	 * Be sure that we don't have situation where jail's root directory
4323 	 * is "/some/path" and mount point is "/some/pathpath".
4324 	 */
4325 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
4326 		return (ENOENT);
4327 	return (0);
4328 }
4329 
4330 void
prison_enforce_statfs(struct ucred * cred,struct mount * mp,struct statfs * sp)4331 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
4332 {
4333 	char jpath[MAXPATHLEN];
4334 	struct prison *pr;
4335 	size_t len;
4336 
4337 	pr = cred->cr_prison;
4338 	if (pr->pr_enforce_statfs == 0)
4339 		return;
4340 	if (prison_canseemount(cred, mp) != 0) {
4341 		bzero(&sp->f_fsid, sizeof(sp->f_fsid));
4342 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4343 		strlcpy(sp->f_mntonname, "[restricted]",
4344 		    sizeof(sp->f_mntonname));
4345 		return;
4346 	}
4347 	if (pr->pr_enforce_statfs > 1)
4348 		bzero(&sp->f_fsid, sizeof(sp->f_fsid));
4349 	if (pr->pr_root->v_mount == mp) {
4350 		/*
4351 		 * Clear current buffer data, so we are sure nothing from
4352 		 * the valid path left there.
4353 		 */
4354 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4355 		*sp->f_mntonname = '/';
4356 		return;
4357 	}
4358 	/*
4359 	 * If jail's chroot directory is set to "/" we should be able to see
4360 	 * all mount-points from inside a jail.
4361 	 */
4362 	if (strcmp(pr->pr_path, "/") == 0)
4363 		return;
4364 	len = strlen(pr->pr_path);
4365 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
4366 	/*
4367 	 * Clear current buffer data, so we are sure nothing from
4368 	 * the valid path left there.
4369 	 */
4370 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
4371 	if (*jpath == '\0') {
4372 		/* Should never happen. */
4373 		*sp->f_mntonname = '/';
4374 	} else {
4375 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
4376 	}
4377 }
4378 
4379 /*
4380  * Check with permission for a specific privilege is granted within jail.  We
4381  * have a specific list of accepted privileges; the rest are denied.
4382  */
4383 int
prison_priv_check(struct ucred * cred,int priv)4384 prison_priv_check(struct ucred *cred, int priv)
4385 {
4386 	struct prison *pr;
4387 	int error;
4388 
4389 	/*
4390 	 * Some policies have custom handlers. This routine should not be
4391 	 * called for them. See priv_check_cred().
4392 	 */
4393 	switch (priv) {
4394 	case PRIV_VFS_LOOKUP:
4395 	case PRIV_VFS_GENERATION:
4396 		KASSERT(0, ("prison_priv_check instead of a custom handler "
4397 		    "called for %d\n", priv));
4398 	}
4399 
4400 	if (!jailed(cred))
4401 		return (0);
4402 
4403 #ifdef VIMAGE
4404 	/*
4405 	 * Privileges specific to prisons with a virtual network stack.
4406 	 * There might be a duplicate entry here in case the privilege
4407 	 * is only granted conditionally in the legacy jail case.
4408 	 */
4409 	switch (priv) {
4410 		/*
4411 		 * NFS-specific privileges.
4412 		 */
4413 	case PRIV_NFS_DAEMON:
4414 	case PRIV_VFS_GETFH:
4415 	case PRIV_VFS_MOUNT_EXPORTED:
4416 		if (!prison_check_nfsd(cred))
4417 			return (EPERM);
4418 #ifdef notyet
4419 	case PRIV_NFS_LOCKD:
4420 #endif
4421 		/*
4422 		 * Network stack privileges.
4423 		 */
4424 	case PRIV_NET_BRIDGE:
4425 	case PRIV_NET_GRE:
4426 	case PRIV_NET_BPF:
4427 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
4428 	case PRIV_NET_ROUTE:
4429 	case PRIV_NET_TAP:
4430 	case PRIV_NET_SETIFMTU:
4431 	case PRIV_NET_SETIFFLAGS:
4432 	case PRIV_NET_SETIFCAP:
4433 	case PRIV_NET_SETIFDESCR:
4434 	case PRIV_NET_SETIFNAME	:
4435 	case PRIV_NET_SETIFMETRIC:
4436 	case PRIV_NET_SETIFPHYS:
4437 	case PRIV_NET_SETIFMAC:
4438 	case PRIV_NET_SETLANPCP:
4439 	case PRIV_NET_ADDMULTI:
4440 	case PRIV_NET_DELMULTI:
4441 	case PRIV_NET_HWIOCTL:
4442 	case PRIV_NET_SETLLADDR:
4443 	case PRIV_NET_ADDIFGROUP:
4444 	case PRIV_NET_DELIFGROUP:
4445 	case PRIV_NET_IFCREATE:
4446 	case PRIV_NET_IFDESTROY:
4447 	case PRIV_NET_ADDIFADDR:
4448 	case PRIV_NET_DELIFADDR:
4449 	case PRIV_NET_LAGG:
4450 	case PRIV_NET_GIF:
4451 	case PRIV_NET_SETIFVNET:
4452 	case PRIV_NET_SETIFFIB:
4453 	case PRIV_NET_OVPN:
4454 	case PRIV_NET_GENEVE:
4455 	case PRIV_NET_ME:
4456 	case PRIV_NET_WG:
4457 
4458 		/*
4459 		 * 802.11-related privileges.
4460 		 */
4461 	case PRIV_NET80211_VAP_GETKEY:
4462 	case PRIV_NET80211_VAP_MANAGE:
4463 
4464 #ifdef notyet
4465 		/*
4466 		 * ATM privileges.
4467 		 */
4468 	case PRIV_NETATM_CFG:
4469 	case PRIV_NETATM_ADD:
4470 	case PRIV_NETATM_DEL:
4471 	case PRIV_NETATM_SET:
4472 
4473 		/*
4474 		 * Bluetooth privileges.
4475 		 */
4476 	case PRIV_NETBLUETOOTH_RAW:
4477 #endif
4478 
4479 		/*
4480 		 * Netgraph and netgraph module privileges.
4481 		 */
4482 	case PRIV_NETGRAPH_CONTROL:
4483 #ifdef notyet
4484 	case PRIV_NETGRAPH_TTY:
4485 #endif
4486 
4487 		/*
4488 		 * IPv4 and IPv6 privileges.
4489 		 */
4490 	case PRIV_NETINET_IPFW:
4491 	case PRIV_NETINET_DIVERT:
4492 	case PRIV_NETINET_PF:
4493 	case PRIV_NETINET_DUMMYNET:
4494 	case PRIV_NETINET_CARP:
4495 	case PRIV_NETINET_MROUTE:
4496 	case PRIV_NETINET_RAW:
4497 	case PRIV_NETINET_ADDRCTRL6:
4498 	case PRIV_NETINET_ND6:
4499 	case PRIV_NETINET_SCOPE6:
4500 	case PRIV_NETINET_ALIFETIME6:
4501 	case PRIV_NETINET_IPSEC:
4502 	case PRIV_NETINET_BINDANY:
4503 
4504 #ifdef notyet
4505 		/*
4506 		 * NCP privileges.
4507 		 */
4508 	case PRIV_NETNCP:
4509 
4510 		/*
4511 		 * SMB privileges.
4512 		 */
4513 	case PRIV_NETSMB:
4514 #endif
4515 
4516 	/*
4517 	 * No default: or deny here.
4518 	 * In case of no permit fall through to next switch().
4519 	 */
4520 		if (cred->cr_prison->pr_flags & PR_VNET)
4521 			return (0);
4522 	}
4523 #endif /* VIMAGE */
4524 
4525 	switch (priv) {
4526 		/*
4527 		 * Allow ktrace privileges for root in jail.
4528 		 */
4529 	case PRIV_KTRACE:
4530 		return (0);
4531 
4532 		/*
4533 		 * Allow jailed processes to configure audit identity and
4534 		 * submit audit records (login, etc).  In the future we may
4535 		 * want to further refine the relationship between audit and
4536 		 * jail.
4537 		 */
4538 	case PRIV_AUDIT_GETAUDIT:
4539 	case PRIV_AUDIT_SETAUDIT:
4540 		if (cred->cr_prison->pr_allow & PR_ALLOW_SETAUDIT)
4541 			return (0);
4542 		else
4543 			return (EPERM);
4544 #if 0
4545 	case PRIV_AUDIT_SUBMIT:
4546 #endif
4547 
4548 		/*
4549 		 * Allow jailed processes to manipulate process UNIX
4550 		 * credentials in any way they see fit.
4551 		 */
4552 	case PRIV_CRED_SETCRED:
4553 	case PRIV_CRED_SETUID:
4554 	case PRIV_CRED_SETEUID:
4555 	case PRIV_CRED_SETGID:
4556 	case PRIV_CRED_SETEGID:
4557 	case PRIV_CRED_SETGROUPS:
4558 	case PRIV_CRED_SETREUID:
4559 	case PRIV_CRED_SETREGID:
4560 	case PRIV_CRED_SETRESUID:
4561 	case PRIV_CRED_SETRESGID:
4562 
4563 		/*
4564 		 * Jail implements visibility constraints already, so allow
4565 		 * jailed root to override uid/gid-based constraints.
4566 		 */
4567 	case PRIV_SEEOTHERGIDS:
4568 	case PRIV_SEEOTHERUIDS:
4569 	case PRIV_SEEJAILPROC:
4570 
4571 		/*
4572 		 * Jail implements inter-process debugging limits already, so
4573 		 * allow jailed root various debugging privileges.
4574 		 */
4575 	case PRIV_DEBUG_DIFFCRED:
4576 	case PRIV_DEBUG_SUGID:
4577 	case PRIV_DEBUG_UNPRIV:
4578 	case PRIV_DEBUG_DIFFJAIL:
4579 
4580 		/*
4581 		 * Allow jail to set various resource limits and login
4582 		 * properties, and for now, exceed process resource limits.
4583 		 */
4584 	case PRIV_PROC_LIMIT:
4585 	case PRIV_PROC_SETLOGIN:
4586 	case PRIV_PROC_SETRLIMIT:
4587 
4588 		/*
4589 		 * Debuggers should work in jails.
4590 		 */
4591 	case PRIV_PROC_MEM_WRITE:
4592 
4593 		/*
4594 		 * System V and POSIX IPC privileges are granted in jail.
4595 		 */
4596 	case PRIV_IPC_READ:
4597 	case PRIV_IPC_WRITE:
4598 	case PRIV_IPC_ADMIN:
4599 	case PRIV_IPC_MSGSIZE:
4600 	case PRIV_MQ_ADMIN:
4601 
4602 		/*
4603 		 * Jail operations within a jail work on child jails.
4604 		 */
4605 	case PRIV_JAIL_ATTACH:
4606 	case PRIV_JAIL_SET:
4607 	case PRIV_JAIL_REMOVE:
4608 
4609 		/*
4610 		 * Jail implements its own inter-process limits, so allow
4611 		 * root processes in jail to change scheduling on other
4612 		 * processes in the same jail.  Likewise for signalling.
4613 		 */
4614 	case PRIV_SCHED_DIFFCRED:
4615 	case PRIV_SCHED_CPUSET:
4616 	case PRIV_SCHED_DIFFJAIL:
4617 	case PRIV_SIGNAL_DIFFCRED:
4618 	case PRIV_SIGNAL_SUGID:
4619 	case PRIV_SIGNAL_DIFFJAIL:
4620 
4621 		/*
4622 		 * Allow jailed processes to write to sysctls marked as jail
4623 		 * writable.
4624 		 */
4625 	case PRIV_SYSCTL_WRITEJAIL:
4626 
4627 		/*
4628 		 * Allow root in jail to manage a variety of quota
4629 		 * properties.  These should likely be conditional on a
4630 		 * configuration option.
4631 		 */
4632 	case PRIV_VFS_GETQUOTA:
4633 	case PRIV_VFS_SETQUOTA:
4634 
4635 		/*
4636 		 * Since Jail relies on chroot() to implement file system
4637 		 * protections, grant many VFS privileges to root in jail.
4638 		 * Be careful to exclude mount-related and NFS-related
4639 		 * privileges.
4640 		 */
4641 	case PRIV_VFS_READ:
4642 	case PRIV_VFS_WRITE:
4643 	case PRIV_VFS_ADMIN:
4644 	case PRIV_VFS_EXEC:
4645 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
4646 	case PRIV_VFS_CHFLAGS_DEV:
4647 	case PRIV_VFS_CHOWN:
4648 	case PRIV_VFS_CHROOT:
4649 	case PRIV_VFS_RETAINSUGID:
4650 	case PRIV_VFS_FCHROOT:
4651 	case PRIV_VFS_LINK:
4652 	case PRIV_VFS_SETGID:
4653 	case PRIV_VFS_STAT:
4654 	case PRIV_VFS_STICKYFILE:
4655 
4656 		/*
4657 		 * As in the non-jail case, non-root users are expected to be
4658 		 * able to read kernel/physical memory (provided /dev/[k]mem
4659 		 * exists in the jail and they have permission to access it).
4660 		 */
4661 	case PRIV_KMEM_READ:
4662 		return (0);
4663 
4664 		/*
4665 		 * Depending on the global setting, allow privilege of
4666 		 * setting system flags.
4667 		 */
4668 	case PRIV_VFS_SYSFLAGS:
4669 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
4670 			return (0);
4671 		else
4672 			return (EPERM);
4673 
4674 		/*
4675 		 * Depending on the global setting, allow privilege of
4676 		 * mounting/unmounting file systems.
4677 		 */
4678 	case PRIV_VFS_MOUNT:
4679 	case PRIV_VFS_UNMOUNT:
4680 	case PRIV_VFS_MOUNT_NONUSER:
4681 	case PRIV_VFS_MOUNT_OWNER:
4682 		pr = cred->cr_prison;
4683 		prison_lock(pr);
4684 		if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
4685 			error = 0;
4686 		else
4687 			error = EPERM;
4688 		prison_unlock(pr);
4689 		return (error);
4690 
4691 		/*
4692 		 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
4693 		 * policy.  priv_check_cred will not specifically allow it, and
4694 		 * we may want a MAC policy to allow it.
4695 		 */
4696 	case PRIV_VFS_READ_DIR:
4697 		return (0);
4698 
4699 		/*
4700 		 * Conditionally allow privileged process in the jail to
4701 		 * manipulate filesystem extended attributes in the system
4702 		 * namespace.
4703 		 */
4704 	case PRIV_VFS_EXTATTR_SYSTEM:
4705 		if ((cred->cr_prison->pr_allow & PR_ALLOW_EXTATTR) != 0)
4706 			return (0);
4707 		else
4708 			return (EPERM);
4709 
4710 		/*
4711 		 * Conditionnaly allow locking (unlocking) physical pages
4712 		 * in memory.
4713 		 */
4714 	case PRIV_VM_MLOCK:
4715 	case PRIV_VM_MUNLOCK:
4716 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
4717 			return (0);
4718 		else
4719 			return (EPERM);
4720 
4721 		/*
4722 		 * Conditionally allow jailed root to bind reserved ports.
4723 		 */
4724 	case PRIV_NETINET_RESERVEDPORT:
4725 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
4726 			return (0);
4727 		else
4728 			return (EPERM);
4729 
4730 		/*
4731 		 * Allow jailed root to reuse in-use ports.
4732 		 */
4733 	case PRIV_NETINET_REUSEPORT:
4734 		return (0);
4735 
4736 		/*
4737 		 * Allow jailed root to set certain IPv4/6 (option) headers.
4738 		 */
4739 	case PRIV_NETINET_SETHDROPTS:
4740 		return (0);
4741 
4742 		/*
4743 		 * Conditionally allow creating raw sockets in jail.
4744 		 */
4745 	case PRIV_NETINET_RAW:
4746 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4747 			return (0);
4748 		else
4749 			return (EPERM);
4750 
4751 		/*
4752 		 * Since jail implements its own visibility limits on netstat
4753 		 * sysctls, allow getcred.  This allows identd to work in
4754 		 * jail.
4755 		 */
4756 	case PRIV_NETINET_GETCRED:
4757 		return (0);
4758 
4759 		/*
4760 		 * Allow jailed root to set loginclass.
4761 		 */
4762 	case PRIV_PROC_SETLOGINCLASS:
4763 		return (0);
4764 
4765 		/*
4766 		 * Do not allow a process inside a jail to read the kernel
4767 		 * message buffer unless explicitly permitted.
4768 		 */
4769 	case PRIV_MSGBUF:
4770 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
4771 			return (0);
4772 		return (EPERM);
4773 
4774 		/*
4775 		 * Conditionally allow privileged process in the jail adjust
4776 		 * machine time.
4777 		 */
4778 	case PRIV_ADJTIME:
4779 	case PRIV_NTP_ADJTIME:
4780 		if (cred->cr_prison->pr_allow &
4781 		    (PR_ALLOW_ADJTIME | PR_ALLOW_SETTIME)) {
4782 			return (0);
4783 		}
4784 		return (EPERM);
4785 
4786 		/*
4787 		 * Conditionally allow privileged process in the jail set
4788 		 * machine time.
4789 		 */
4790 	case PRIV_SETTIMEOFDAY:
4791 	case PRIV_CLOCK_SETTIME:
4792 		if (cred->cr_prison->pr_allow & PR_ALLOW_SETTIME)
4793 			return (0);
4794 		else
4795 			return (EPERM);
4796 
4797 		/*
4798 		 * Conditionally allow privileged process in the jail to modify
4799 		 * the routing table.
4800 		 */
4801 	case PRIV_NET_ROUTE:
4802 		if (cred->cr_prison->pr_allow & PR_ALLOW_ROUTING)
4803 			return (0);
4804 		else
4805 			return (EPERM);
4806 
4807 	case PRIV_VMM_CREATE:
4808 	case PRIV_VMM_DESTROY:
4809 		/*
4810 		 * Jailed root can create and destroy VMs; the vmm module
4811 		 * additionally checks for the allow.vmm flag.
4812 		 */
4813 		return (0);
4814 
4815 	case PRIV_VMM_PPTDEV:
4816 		/*
4817 		 * Allow jailed root to manage passthrough devices.  vmm(4) also
4818 		 * checks for the dynamically added allow.vmm_ppt.
4819 		 */
4820 		return (0);
4821 
4822 	default:
4823 		/*
4824 		 * In all remaining cases, deny the privilege request.  This
4825 		 * includes almost all network privileges, many system
4826 		 * configuration privileges.
4827 		 */
4828 		return (EPERM);
4829 	}
4830 }
4831 
4832 /*
4833  * Return the part of pr2's name that is relative to pr1, or the whole name
4834  * if it does not directly follow.
4835  */
4836 
4837 char *
prison_name(struct prison * pr1,struct prison * pr2)4838 prison_name(struct prison *pr1, struct prison *pr2)
4839 {
4840 	char *name;
4841 
4842 	/* Jails see themselves as "0" (if they see themselves at all). */
4843 	if (pr1 == pr2)
4844 		return "0";
4845 	name = pr2->pr_name;
4846 	if (prison_ischild(pr1, pr2)) {
4847 		/*
4848 		 * pr1 isn't locked (and allprison_lock may not be either)
4849 		 * so its length can't be counted on.  But the number of dots
4850 		 * can be counted on - and counted.
4851 		 */
4852 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
4853 			name = strchr(name, '.') + 1;
4854 	}
4855 	return (name);
4856 }
4857 
4858 /*
4859  * Return the part of pr2's path that is relative to pr1, or the whole path
4860  * if it does not directly follow.
4861  */
4862 static char *
prison_path(struct prison * pr1,struct prison * pr2)4863 prison_path(struct prison *pr1, struct prison *pr2)
4864 {
4865 	char *path1, *path2;
4866 	int len1;
4867 
4868 	path1 = pr1->pr_path;
4869 	path2 = pr2->pr_path;
4870 	if (!strcmp(path1, "/"))
4871 		return (path2);
4872 	len1 = strlen(path1);
4873 	if (strncmp(path1, path2, len1))
4874 		return (path2);
4875 	if (path2[len1] == '\0')
4876 		return "/";
4877 	if (path2[len1] == '/')
4878 		return (path2 + len1);
4879 	return (path2);
4880 }
4881 
4882 /*
4883  * Jail-related sysctls.
4884  */
4885 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4886     "Jails");
4887 
4888 #if defined(INET) || defined(INET6)
4889 /*
4890  * Copy address array to memory that would be then SYSCTL_OUT-ed.
4891  * sysctl_jail_list() helper.
4892  */
4893 static void
prison_ip_copyout(struct prison * pr,const pr_family_t af,void ** out,int * len)4894 prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4895 {
4896 	const struct prison_ip *pip;
4897 	const size_t size = pr_families[af].size;
4898 
4899  again:
4900 	mtx_assert(&pr->pr_mtx, MA_OWNED);
4901 	if ((pip = pr->pr_addrs[af]) != NULL) {
4902 		if (*len < pip->ips) {
4903 			*len = pip->ips;
4904 			mtx_unlock(&pr->pr_mtx);
4905 			*out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4906 			mtx_lock(&pr->pr_mtx);
4907 			goto again;
4908 		}
4909 		bcopy(pip->pr_ip, *out, pip->ips * size);
4910 	}
4911 }
4912 #endif
4913 
4914 static int
sysctl_jail_list(SYSCTL_HANDLER_ARGS)4915 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4916 {
4917 	struct xprison *xp;
4918 	struct prison *pr, *cpr;
4919 #ifdef INET
4920 	struct in_addr *ip4 = NULL;
4921 	int ip4s = 0;
4922 #endif
4923 #ifdef INET6
4924 	struct in6_addr *ip6 = NULL;
4925 	int ip6s = 0;
4926 #endif
4927 	int descend, error;
4928 
4929 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4930 	pr = req->td->td_ucred->cr_prison;
4931 	error = 0;
4932 	sx_slock(&allprison_lock);
4933 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4934 		mtx_lock(&cpr->pr_mtx);
4935 #ifdef INET
4936 		prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4937 #endif
4938 #ifdef INET6
4939 		prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4940 #endif
4941 		bzero(xp, sizeof(*xp));
4942 		xp->pr_version = XPRISON_VERSION;
4943 		xp->pr_id = cpr->pr_id;
4944 		xp->pr_state = cpr->pr_state;
4945 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4946 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4947 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4948 #ifdef INET
4949 		xp->pr_ip4s = ip4s;
4950 #endif
4951 #ifdef INET6
4952 		xp->pr_ip6s = ip6s;
4953 #endif
4954 		mtx_unlock(&cpr->pr_mtx);
4955 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4956 		if (error)
4957 			break;
4958 #ifdef INET
4959 		if (xp->pr_ip4s > 0) {
4960 			error = SYSCTL_OUT(req, ip4,
4961 			    xp->pr_ip4s * sizeof(struct in_addr));
4962 			if (error)
4963 				break;
4964 		}
4965 #endif
4966 #ifdef INET6
4967 		if (xp->pr_ip6s > 0) {
4968 			error = SYSCTL_OUT(req, ip6,
4969 			    xp->pr_ip6s * sizeof(struct in6_addr));
4970 			if (error)
4971 				break;
4972 		}
4973 #endif
4974 	}
4975 	sx_sunlock(&allprison_lock);
4976 	free(xp, M_TEMP);
4977 #ifdef INET
4978 	free(ip4, M_TEMP);
4979 #endif
4980 #ifdef INET6
4981 	free(ip6, M_TEMP);
4982 #endif
4983 	return (error);
4984 }
4985 
4986 SYSCTL_OID(_security_jail, OID_AUTO, list,
4987     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4988     sysctl_jail_list, "S", "List of active jails");
4989 
4990 static int
sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)4991 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4992 {
4993 	int error, injail;
4994 
4995 	injail = jailed(req->td->td_ucred);
4996 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4997 
4998 	return (error);
4999 }
5000 
5001 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
5002     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
5003     sysctl_jail_jailed, "I", "Process in jail?");
5004 
5005 static int
sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)5006 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
5007 {
5008 	int error, havevnet;
5009 #ifdef VIMAGE
5010 	struct ucred *cred = req->td->td_ucred;
5011 
5012 	havevnet = jailed(cred) && prison_owns_vnet(cred->cr_prison);
5013 #else
5014 	havevnet = 0;
5015 #endif
5016 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
5017 
5018 	return (error);
5019 }
5020 
5021 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
5022     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
5023     sysctl_jail_vnet, "I", "Jail owns vnet?");
5024 
5025 #if defined(INET) || defined(INET6)
5026 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
5027     &jail_max_af_ips, 0,
5028     "Number of IP addresses a jail may have at most per address family (deprecated)");
5029 #endif
5030 
5031 /*
5032  * Default parameters for jail(2) compatibility.  For historical reasons,
5033  * the sysctl names have varying similarity to the parameter names.  Prisons
5034  * just see their own parameters, and can't change them.
5035  */
5036 static int
sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)5037 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
5038 {
5039 	int error, i;
5040 
5041 	/* Get the current flag value, and convert it to a boolean. */
5042 	if (req->td->td_ucred->cr_prison == &prison0) {
5043 		mtx_lock(&prison0.pr_mtx);
5044 		i = (jail_default_allow & arg2) != 0;
5045 		mtx_unlock(&prison0.pr_mtx);
5046 	} else
5047 		i = prison_allow(req->td->td_ucred, arg2);
5048 
5049 	if (arg1 != NULL)
5050 		i = !i;
5051 	error = sysctl_handle_int(oidp, &i, 0, req);
5052 	if (error || !req->newptr)
5053 		return (error);
5054 	i = i ? arg2 : 0;
5055 	if (arg1 != NULL)
5056 		i ^= arg2;
5057 	/*
5058 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
5059 	 * for writing.
5060 	 */
5061 	mtx_lock(&prison0.pr_mtx);
5062 	jail_default_allow = (jail_default_allow & ~arg2) | i;
5063 	mtx_unlock(&prison0.pr_mtx);
5064 	return (0);
5065 }
5066 
5067 /*
5068  * Do not add more here. Use SYSCTL_JAIL_PARAM (allow flags for jails)
5069  * instead.
5070  */
5071 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
5072     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5073     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
5074     "Processes in jail can set their hostnames (deprecated)");
5075 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
5076     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5077     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
5078     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
5079 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
5080     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5081     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
5082     "Processes in jail can use System V IPC primitives (deprecated)");
5083 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
5084     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5085     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
5086     "Prison root can create raw sockets (deprecated)");
5087 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
5088     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5089     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
5090     "Processes in jail can alter system file flags (deprecated)");
5091 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
5092     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5093     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
5094     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
5095 SYSCTL_PROC(_security_jail, OID_AUTO, mlock_allowed,
5096     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5097     NULL, PR_ALLOW_MLOCK, sysctl_jail_default_allow, "I",
5098     "Processes in jail can lock/unlock physical pages in memory (deprecated)");
5099 
5100 static int
sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)5101 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
5102 {
5103 	struct prison *pr;
5104 	int level, error;
5105 
5106 	pr = req->td->td_ucred->cr_prison;
5107 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
5108 	error = sysctl_handle_int(oidp, &level, 0, req);
5109 	if (error || !req->newptr)
5110 		return (error);
5111 	*(int *)arg1 = level;
5112 	return (0);
5113 }
5114 
5115 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
5116     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5117     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
5118     sysctl_jail_default_level, "I",
5119     "Processes in jail cannot see all mounted file systems (deprecated)");
5120 
5121 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
5122     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
5123     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
5124     sysctl_jail_default_level, "I",
5125     "Ruleset for the devfs filesystem in jail (deprecated)");
5126 
5127 SYSCTL_NODE(_security_jail, OID_AUTO, children, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
5128     "Limits and stats of child jails");
5129 
5130 static int
sysctl_jail_children(SYSCTL_HANDLER_ARGS)5131 sysctl_jail_children(SYSCTL_HANDLER_ARGS)
5132 {
5133 	struct prison *pr;
5134 	int i;
5135 
5136 	pr = req->td->td_ucred->cr_prison;
5137 
5138 	switch (oidp->oid_kind & CTLTYPE) {
5139 	case CTLTYPE_INT:
5140 		i = *(int *)((char *)pr + arg2);
5141 		return (SYSCTL_OUT(req, &i, sizeof(i)));
5142 	}
5143 
5144 	return (0);
5145 }
5146 
5147 SYSCTL_PROC(_security_jail_children, OID_AUTO, max,
5148     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
5149     NULL, offsetof(struct prison, pr_childmax), sysctl_jail_children,
5150     "I", "Maximum number of child jails");
5151 SYSCTL_PROC(_security_jail_children, OID_AUTO, cur,
5152     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
5153     NULL, offsetof(struct prison, pr_childcount), sysctl_jail_children,
5154     "I", "Current number of child jails");
5155 
5156 /*
5157  * Nodes to describe jail parameters.  Maximum length of string parameters
5158  * is returned in the string itself, and the other parameters exist merely
5159  * to make themselves and their types known.
5160  */
5161 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
5162     "Jail parameters");
5163 
5164 int
sysctl_jail_param(SYSCTL_HANDLER_ARGS)5165 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
5166 {
5167 	int i;
5168 	long l;
5169 	size_t s;
5170 	char numbuf[12];
5171 
5172 	switch (oidp->oid_kind & CTLTYPE)
5173 	{
5174 	case CTLTYPE_LONG:
5175 	case CTLTYPE_ULONG:
5176 		l = 0;
5177 #ifdef SCTL_MASK32
5178 		if (!(req->flags & SCTL_MASK32))
5179 #endif
5180 			return (SYSCTL_OUT(req, &l, sizeof(l)));
5181 	case CTLTYPE_INT:
5182 	case CTLTYPE_UINT:
5183 		i = 0;
5184 		return (SYSCTL_OUT(req, &i, sizeof(i)));
5185 	case CTLTYPE_STRING:
5186 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
5187 		return
5188 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
5189 	case CTLTYPE_STRUCT:
5190 		s = (size_t)arg2;
5191 		return (SYSCTL_OUT(req, &s, sizeof(s)));
5192 	}
5193 	return (0);
5194 }
5195 
5196 /*
5197  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
5198  * jail creation time but cannot be changed in an existing jail.
5199  */
5200 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
5201 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
5202 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
5203 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
5204 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
5205     "I", "Jail secure level");
5206 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
5207     "Jail value for kern.osreldate and uname -K");
5208 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
5209     "Jail value for kern.osrelease and uname -r");
5210 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
5211     "I", "Jail cannot see all mounted file systems");
5212 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
5213     "I", "Ruleset for in-jail devfs mounts");
5214 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
5215     "B", "Jail persistence");
5216 #ifdef VIMAGE
5217 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
5218     "E,jailsys", "Virtual network stack");
5219 #endif
5220 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
5221     "B", "Jail is in the process of shutting down");
5222 
5223 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
5224 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
5225     "I", "Current number of child jails");
5226 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
5227     "I", "Maximum number of child jails");
5228 
5229 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
5230 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
5231     "Jail hostname");
5232 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
5233     "Jail NIS domainname");
5234 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
5235     "Jail host UUID");
5236 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
5237     "LU", "Jail host ID");
5238 
5239 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
5240 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
5241 
5242 #ifdef MAC
5243 SYSCTL_JAIL_PARAM_STRUCT(_mac, label, CTLFLAG_RW, sizeof(struct mac),
5244     "S,mac", "Jail MAC label");
5245 #endif
5246 
5247 #ifdef INET
5248 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
5249     "Jail IPv4 address virtualization");
5250 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
5251     "S,in_addr,a", "Jail IPv4 addresses");
5252 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
5253     "B", "Do (not) use IPv4 source address selection rather than the "
5254     "primary jail IPv4 address.");
5255 #endif
5256 #ifdef INET6
5257 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
5258     "Jail IPv6 address virtualization");
5259 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
5260     "S,in6_addr,a", "Jail IPv6 addresses");
5261 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
5262     "B", "Do (not) use IPv6 source address selection rather than the "
5263     "primary jail IPv6 address.");
5264 #endif
5265 
5266 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
5267 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
5268     "B", "Jail may set hostname");
5269 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
5270     "B", "Jail may use SYSV IPC");
5271 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
5272     "B", "Jail may create raw sockets");
5273 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
5274     "B", "Jail may alter system file flags");
5275 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
5276     "B", "Jail may set file quotas");
5277 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
5278     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
5279 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
5280     "B", "Jail may lock (unlock) physical pages in memory");
5281 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
5282     "B", "Jail may bind sockets to reserved ports");
5283 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
5284     "B", "Jail may read the kernel message buffer");
5285 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
5286     "B", "Unprivileged processes may use process debugging facilities");
5287 SYSCTL_JAIL_PARAM(_allow, unprivileged_parent_tampering,
5288     CTLTYPE_INT | CTLFLAG_RW, "B",
5289     "Unprivileged parent jail processes may tamper with same-uid processes"
5290     " (signal/debug/cpuset)");
5291 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
5292     "B", "Processes in jail with uid 0 have privilege");
5293 #ifdef VIMAGE
5294 SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW,
5295     "B", "Mountd/nfsd may run in the jail");
5296 #endif
5297 SYSCTL_JAIL_PARAM(_allow, extattr, CTLTYPE_INT | CTLFLAG_RW,
5298     "B", "Jail may set system-level filesystem extended attributes");
5299 SYSCTL_JAIL_PARAM(_allow, adjtime, CTLTYPE_INT | CTLFLAG_RW,
5300     "B", "Jail may adjust system time");
5301 SYSCTL_JAIL_PARAM(_allow, settime, CTLTYPE_INT | CTLFLAG_RW,
5302     "B", "Jail may set system time");
5303 SYSCTL_JAIL_PARAM(_allow, routing, CTLTYPE_INT | CTLFLAG_RW,
5304     "B", "Jail may modify routing table");
5305 #ifdef AUDIT
5306 SYSCTL_JAIL_PARAM(_allow, setaudit, CTLTYPE_INT | CTLFLAG_RW,
5307     "B", "Jail may set and get audit session state");
5308 #endif
5309 
5310 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
5311 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
5312     "B", "Jail may mount/unmount jail-friendly file systems in general");
5313 
5314 /*
5315  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
5316  * its associated bit in the pr_allow bitmask, or zero if the parameter was
5317  * not created.
5318  */
5319 unsigned
prison_add_allow(const char * prefix,const char * name,const char * prefix_descr,const char * descr)5320 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
5321     const char *descr)
5322 {
5323 	struct bool_flags *bf;
5324 	struct sysctl_oid *parent;
5325 	char *allow_name, *allow_noname, *allowed;
5326 #ifndef NO_SYSCTL_DESCR
5327 	char *descr_deprecated;
5328 #endif
5329 	u_int allow_flag;
5330 
5331 	if (prefix
5332 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
5333 		< 0 ||
5334 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
5335 		< 0
5336 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
5337 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
5338 		free(allow_name, M_PRISON);
5339 		return 0;
5340 	}
5341 
5342 	/*
5343 	 * See if this parameter has already beed added, i.e. a module was
5344 	 * previously loaded/unloaded.
5345 	 */
5346 	mtx_lock(&prison0.pr_mtx);
5347 	for (bf = pr_flag_allow;
5348 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
5349 		atomic_load_int(&bf->flag) != 0;
5350 	     bf++) {
5351 		if (strcmp(bf->name, allow_name) == 0) {
5352 			allow_flag = bf->flag;
5353 			goto no_add;
5354 		}
5355 	}
5356 
5357 	/*
5358 	 * Find a free bit in pr_allow_all, failing if there are none
5359 	 * (which shouldn't happen as long as we keep track of how many
5360 	 * potential dynamic flags exist).
5361 	 */
5362 	for (allow_flag = 1;; allow_flag <<= 1) {
5363 		if (allow_flag == 0)
5364 			goto no_add;
5365 		if ((pr_allow_all & allow_flag) == 0)
5366 			break;
5367 	}
5368 
5369 	/* Note the parameter in the next open slot in pr_flag_allow. */
5370 	for (bf = pr_flag_allow; ; bf++) {
5371 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
5372 			/* This should never happen, but is not fatal. */
5373 			allow_flag = 0;
5374 			goto no_add;
5375 		}
5376 		if (atomic_load_int(&bf->flag) == 0)
5377 			break;
5378 	}
5379 	bf->name = allow_name;
5380 	bf->noname = allow_noname;
5381 	pr_allow_all |= allow_flag;
5382 	/*
5383 	 * prison0 always has permission for the new parameter.
5384 	 * Other jails must have it granted to them.
5385 	 */
5386 	prison0.pr_allow |= allow_flag;
5387 	/* The flag indicates a valid entry, so make sure it is set last. */
5388 	atomic_store_rel_int(&bf->flag, allow_flag);
5389 	mtx_unlock(&prison0.pr_mtx);
5390 
5391 	/*
5392 	 * Create sysctls for the parameter, and the back-compat global
5393 	 * permission.
5394 	 */
5395 	parent = prefix
5396 	    ? SYSCTL_ADD_NODE(NULL,
5397 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
5398 		  OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
5399 	    : &sysctl___security_jail_param_allow;
5400 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
5401 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
5402 	    NULL, 0, sysctl_jail_param, "B", descr);
5403 	if ((prefix
5404 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
5405 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
5406 #ifndef NO_SYSCTL_DESCR
5407 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
5408 		    descr);
5409 #endif
5410 		(void)SYSCTL_ADD_PROC(NULL,
5411 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
5412 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
5413 		    sysctl_jail_default_allow, "I", descr_deprecated);
5414 #ifndef NO_SYSCTL_DESCR
5415 		free(descr_deprecated, M_TEMP);
5416 #endif
5417 		free(allowed, M_TEMP);
5418 	}
5419 	return allow_flag;
5420 
5421  no_add:
5422 	mtx_unlock(&prison0.pr_mtx);
5423 	free(allow_name, M_PRISON);
5424 	free(allow_noname, M_PRISON);
5425 	return allow_flag;
5426 }
5427 
5428 /*
5429  * The VFS system will register jail-aware filesystems here.  They each get
5430  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
5431  * attempts to mount.
5432  */
5433 void
prison_add_vfs(struct vfsconf * vfsp)5434 prison_add_vfs(struct vfsconf *vfsp)
5435 {
5436 #ifdef NO_SYSCTL_DESCR
5437 
5438 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
5439 	    NULL, NULL);
5440 #else
5441 	char *descr;
5442 
5443 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
5444 	    vfsp->vfc_name);
5445 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
5446 	    NULL, descr);
5447 	free(descr, M_TEMP);
5448 #endif
5449 }
5450 
5451 #ifdef RACCT
5452 void
prison_racct_foreach(void (* callback)(struct racct * racct,void * arg2,void * arg3),void (* pre)(void),void (* post)(void),void * arg2,void * arg3)5453 prison_racct_foreach(void (*callback)(struct racct *racct,
5454     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
5455     void *arg2, void *arg3)
5456 {
5457 	struct prison_racct *prr;
5458 
5459 	ASSERT_RACCT_ENABLED();
5460 
5461 	sx_slock(&allprison_lock);
5462 	if (pre != NULL)
5463 		(pre)();
5464 	LIST_FOREACH(prr, &allprison_racct, prr_next)
5465 		(callback)(prr->prr_racct, arg2, arg3);
5466 	if (post != NULL)
5467 		(post)();
5468 	sx_sunlock(&allprison_lock);
5469 }
5470 
5471 static struct prison_racct *
prison_racct_find_locked(const char * name)5472 prison_racct_find_locked(const char *name)
5473 {
5474 	struct prison_racct *prr;
5475 
5476 	ASSERT_RACCT_ENABLED();
5477 	sx_assert(&allprison_lock, SA_XLOCKED);
5478 
5479 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
5480 		return (NULL);
5481 
5482 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
5483 		if (strcmp(name, prr->prr_name) != 0)
5484 			continue;
5485 
5486 		/* Found prison_racct with a matching name? */
5487 		prison_racct_hold(prr);
5488 		return (prr);
5489 	}
5490 
5491 	/* Add new prison_racct. */
5492 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
5493 	racct_create(&prr->prr_racct);
5494 
5495 	strcpy(prr->prr_name, name);
5496 	refcount_init(&prr->prr_refcount, 1);
5497 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
5498 
5499 	return (prr);
5500 }
5501 
5502 struct prison_racct *
prison_racct_find(const char * name)5503 prison_racct_find(const char *name)
5504 {
5505 	struct prison_racct *prr;
5506 
5507 	ASSERT_RACCT_ENABLED();
5508 
5509 	sx_xlock(&allprison_lock);
5510 	prr = prison_racct_find_locked(name);
5511 	sx_xunlock(&allprison_lock);
5512 	return (prr);
5513 }
5514 
5515 void
prison_racct_hold(struct prison_racct * prr)5516 prison_racct_hold(struct prison_racct *prr)
5517 {
5518 
5519 	ASSERT_RACCT_ENABLED();
5520 
5521 	refcount_acquire(&prr->prr_refcount);
5522 }
5523 
5524 static void
prison_racct_free_locked(struct prison_racct * prr)5525 prison_racct_free_locked(struct prison_racct *prr)
5526 {
5527 
5528 	ASSERT_RACCT_ENABLED();
5529 	sx_assert(&allprison_lock, SA_XLOCKED);
5530 
5531 	if (refcount_release(&prr->prr_refcount)) {
5532 		racct_destroy(&prr->prr_racct);
5533 		LIST_REMOVE(prr, prr_next);
5534 		free(prr, M_PRISON_RACCT);
5535 	}
5536 }
5537 
5538 void
prison_racct_free(struct prison_racct * prr)5539 prison_racct_free(struct prison_racct *prr)
5540 {
5541 
5542 	ASSERT_RACCT_ENABLED();
5543 	sx_assert(&allprison_lock, SA_UNLOCKED);
5544 
5545 	if (refcount_release_if_not_last(&prr->prr_refcount))
5546 		return;
5547 
5548 	sx_xlock(&allprison_lock);
5549 	prison_racct_free_locked(prr);
5550 	sx_xunlock(&allprison_lock);
5551 }
5552 
5553 static void
prison_racct_attach(struct prison * pr)5554 prison_racct_attach(struct prison *pr)
5555 {
5556 	struct prison_racct *prr;
5557 
5558 	ASSERT_RACCT_ENABLED();
5559 	sx_assert(&allprison_lock, SA_XLOCKED);
5560 
5561 	prr = prison_racct_find_locked(pr->pr_name);
5562 	KASSERT(prr != NULL, ("cannot find prison_racct"));
5563 
5564 	pr->pr_prison_racct = prr;
5565 }
5566 
5567 /*
5568  * Handle jail renaming.  From the racct point of view, renaming means
5569  * moving from one prison_racct to another.
5570  */
5571 static void
prison_racct_modify(struct prison * pr)5572 prison_racct_modify(struct prison *pr)
5573 {
5574 #ifdef RCTL
5575 	struct proc *p;
5576 	struct ucred *cred;
5577 #endif
5578 	struct prison_racct *oldprr;
5579 
5580 	ASSERT_RACCT_ENABLED();
5581 
5582 	sx_slock(&allproc_lock);
5583 	sx_xlock(&allprison_lock);
5584 
5585 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
5586 		sx_xunlock(&allprison_lock);
5587 		sx_sunlock(&allproc_lock);
5588 		return;
5589 	}
5590 
5591 	oldprr = pr->pr_prison_racct;
5592 	pr->pr_prison_racct = NULL;
5593 
5594 	prison_racct_attach(pr);
5595 
5596 	/*
5597 	 * Move resource utilisation records.
5598 	 */
5599 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
5600 
5601 #ifdef RCTL
5602 	/*
5603 	 * Force rctl to reattach rules to processes.
5604 	 */
5605 	FOREACH_PROC_IN_SYSTEM(p) {
5606 		PROC_LOCK(p);
5607 		cred = crhold(p->p_ucred);
5608 		PROC_UNLOCK(p);
5609 		rctl_proc_ucred_changed(p, cred);
5610 		crfree(cred);
5611 	}
5612 #endif
5613 
5614 	sx_sunlock(&allproc_lock);
5615 	prison_racct_free_locked(oldprr);
5616 	sx_xunlock(&allprison_lock);
5617 }
5618 
5619 static void
prison_racct_detach(struct prison * pr)5620 prison_racct_detach(struct prison *pr)
5621 {
5622 
5623 	ASSERT_RACCT_ENABLED();
5624 	sx_assert(&allprison_lock, SA_UNLOCKED);
5625 
5626 	if (pr->pr_prison_racct == NULL)
5627 		return;
5628 	prison_racct_free(pr->pr_prison_racct);
5629 	pr->pr_prison_racct = NULL;
5630 }
5631 #endif /* RACCT */
5632 
5633 /*
5634  * Submit a knote for a prison, locking if necessary.
5635  */
5636 static void
prison_knote(struct prison * pr,long hint)5637 prison_knote(struct prison *pr, long hint)
5638 {
5639 	int locked;
5640 
5641 	locked = mtx_owned(&pr->pr_mtx);
5642 	if (!locked)
5643 		mtx_lock(&pr->pr_mtx);
5644 	KNOTE_LOCKED(pr->pr_klist, hint);
5645 	jaildesc_knote(pr, hint);
5646 	if (!locked)
5647 		mtx_unlock(&pr->pr_mtx);
5648 }
5649 
5650 #ifdef DDB
5651 
5652 static void
db_show_prison(struct prison * pr)5653 db_show_prison(struct prison *pr)
5654 {
5655 	struct bool_flags *bf;
5656 	struct jailsys_flags *jsf;
5657 #if defined(INET) || defined(INET6)
5658 	int ii;
5659 	struct prison_ip *pip;
5660 #endif
5661 	unsigned f;
5662 #ifdef INET
5663 	char ip4buf[INET_ADDRSTRLEN];
5664 #endif
5665 #ifdef INET6
5666 	char ip6buf[INET6_ADDRSTRLEN];
5667 #endif
5668 
5669 	db_printf("prison %p:\n", pr);
5670 	db_printf(" jid             = %d\n", pr->pr_id);
5671 	db_printf(" name            = %s\n", pr->pr_name);
5672 	db_printf(" parent          = %p\n", pr->pr_parent);
5673 	db_printf(" ref             = %d\n", pr->pr_ref);
5674 	db_printf(" uref            = %d\n", pr->pr_uref);
5675 	db_printf(" state           = %s\n",
5676 	    pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
5677 	    pr->pr_state == PRISON_STATE_DYING ? "dying" :
5678 	    "invalid");
5679 	db_printf(" path            = %s\n", pr->pr_path);
5680 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
5681 	    ? pr->pr_cpuset->cs_id : -1);
5682 #ifdef VIMAGE
5683 	db_printf(" vnet            = %p\n", pr->pr_vnet);
5684 #endif
5685 	db_printf(" root            = %p\n", pr->pr_root);
5686 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
5687 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
5688 	db_printf(" children.max    = %d\n", pr->pr_childmax);
5689 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
5690 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
5691 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
5692 	db_printf(" flags           = 0x%x", pr->pr_flags);
5693 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
5694 		if (pr->pr_flags & bf->flag)
5695 			db_printf(" %s", bf->name);
5696 	for (jsf = pr_flag_jailsys;
5697 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
5698 	     jsf++) {
5699 		f = pr->pr_flags & (jsf->disable | jsf->new);
5700 		db_printf(" %-16s= %s\n", jsf->name,
5701 		    (f != 0 && f == jsf->disable) ? "disable"
5702 		    : (f == jsf->new) ? "new"
5703 		    : "inherit");
5704 	}
5705 	db_printf(" allow           = 0x%x", pr->pr_allow);
5706 	for (bf = pr_flag_allow;
5707 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
5708 		atomic_load_int(&bf->flag) != 0;
5709 	     bf++)
5710 		if (pr->pr_allow & bf->flag)
5711 			db_printf(" %s", bf->name);
5712 	db_printf("\n");
5713 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
5714 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
5715 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
5716 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
5717 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
5718 #ifdef INET
5719 	if ((pip = pr->pr_addrs[PR_INET]) != NULL) {
5720 		db_printf(" ip4s            = %d\n", pip->ips);
5721 		for (ii = 0; ii < pip->ips; ii++)
5722 			db_printf(" %s %s\n",
5723 			    ii == 0 ? "ip4.addr        =" : "                 ",
5724 			    inet_ntoa_r(
5725 			    *(const struct in_addr *)PR_IP(pip, PR_INET, ii),
5726 			    ip4buf));
5727 	}
5728 #endif
5729 #ifdef INET6
5730 	if ((pip = pr->pr_addrs[PR_INET6]) != NULL) {
5731 		db_printf(" ip6s            = %d\n", pip->ips);
5732 		for (ii = 0; ii < pip->ips; ii++)
5733 			db_printf(" %s %s\n",
5734 			    ii == 0 ? "ip6.addr        =" : "                 ",
5735 			    ip6_sprintf(ip6buf,
5736 			    (const struct in6_addr *)PR_IP(pip, PR_INET6, ii)));
5737 	}
5738 #endif
5739 }
5740 
DB_SHOW_COMMAND(prison,db_show_prison_command)5741 DB_SHOW_COMMAND(prison, db_show_prison_command)
5742 {
5743 	struct prison *pr;
5744 
5745 	if (!have_addr) {
5746 		/*
5747 		 * Show all prisons in the list, and prison0 which is not
5748 		 * listed.
5749 		 */
5750 		db_show_prison(&prison0);
5751 		if (!db_pager_quit) {
5752 			TAILQ_FOREACH(pr, &allprison, pr_list) {
5753 				db_show_prison(pr);
5754 				if (db_pager_quit)
5755 					break;
5756 			}
5757 		}
5758 		return;
5759 	}
5760 
5761 	if (addr == 0)
5762 		pr = &prison0;
5763 	else {
5764 		/* Look for a prison with the ID and with references. */
5765 		TAILQ_FOREACH(pr, &allprison, pr_list)
5766 			if (pr->pr_id == addr && pr->pr_ref > 0)
5767 				break;
5768 		if (pr == NULL)
5769 			/* Look again, without requiring a reference. */
5770 			TAILQ_FOREACH(pr, &allprison, pr_list)
5771 				if (pr->pr_id == addr)
5772 					break;
5773 		if (pr == NULL)
5774 			/* Assume address points to a valid prison. */
5775 			pr = (struct prison *)addr;
5776 	}
5777 	db_show_prison(pr);
5778 }
5779 
5780 #endif /* DDB */
5781