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