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