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