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