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