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