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