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