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