xref: /freebsd/sys/kern/kern_jail.c (revision 7060da62ff18e8e52c5e41f0794cc4f10dadfc6e)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
4413628a7SBjoern A. Zeeb  * Copyright (c) 1999 Poul-Henning Kamp.
5413628a7SBjoern A. Zeeb  * Copyright (c) 2008 Bjoern A. Zeeb.
6b38ff370SJamie Gritton  * Copyright (c) 2009 James Gritton.
7413628a7SBjoern A. Zeeb  * All rights reserved.
8b9f0b66cSBjoern A. Zeeb  *
9b9f0b66cSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
10b9f0b66cSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
11b9f0b66cSBjoern A. Zeeb  * are met:
12b9f0b66cSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
13b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
14b9f0b66cSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
15b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
16b9f0b66cSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
17b9f0b66cSBjoern A. Zeeb  *
18b9f0b66cSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b9f0b66cSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b9f0b66cSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b9f0b66cSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b9f0b66cSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b9f0b66cSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b9f0b66cSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b9f0b66cSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b9f0b66cSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b9f0b66cSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b9f0b66cSBjoern A. Zeeb  * SUCH DAMAGE.
2907901f22SPoul-Henning Kamp  */
3075c13541SPoul-Henning Kamp 
31677b542eSDavid E. O'Brien #include <sys/cdefs.h>
32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
33677b542eSDavid E. O'Brien 
34413628a7SBjoern A. Zeeb #include "opt_ddb.h"
35413628a7SBjoern A. Zeeb #include "opt_inet.h"
36413628a7SBjoern A. Zeeb #include "opt_inet6.h"
3746e3b1cbSPawel Jakub Dawidek 
3875c13541SPoul-Henning Kamp #include <sys/param.h>
3975c13541SPoul-Henning Kamp #include <sys/types.h>
4075c13541SPoul-Henning Kamp #include <sys/kernel.h>
4175c13541SPoul-Henning Kamp #include <sys/systm.h>
4275c13541SPoul-Henning Kamp #include <sys/errno.h>
4375c13541SPoul-Henning Kamp #include <sys/sysproto.h>
4475c13541SPoul-Henning Kamp #include <sys/malloc.h>
450304c731SJamie Gritton #include <sys/osd.h>
46800c9408SRobert Watson #include <sys/priv.h>
4775c13541SPoul-Henning Kamp #include <sys/proc.h>
48eb8dcdeaSGleb Smirnoff #include <sys/epoch.h>
49b3059e09SRobert Watson #include <sys/taskqueue.h>
5057b4252eSKonstantin Belousov #include <sys/fcntl.h>
5175c13541SPoul-Henning Kamp #include <sys/jail.h>
52c3188289SKyle Evans #include <sys/linker.h>
5301137630SRobert Watson #include <sys/lock.h>
54*7060da62SJamie Gritton #include <sys/mman.h>
5501137630SRobert Watson #include <sys/mutex.h>
56097055e2SEdward Tomasz Napierala #include <sys/racct.h>
57f87beb93SAndriy Gapon #include <sys/rctl.h>
58a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
59dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
6076ca6f88SJamie Gritton #include <sys/sysent.h>
61fd7a8150SMike Barcroft #include <sys/namei.h>
62820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
63fd7a8150SMike Barcroft #include <sys/queue.h>
6475c13541SPoul-Henning Kamp #include <sys/socket.h>
65fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6683f1e257SRobert Watson #include <sys/sysctl.h>
67c3188289SKyle Evans #include <sys/uuid.h>
68fd7a8150SMike Barcroft #include <sys/vnode.h>
69530c0060SRobert Watson 
7075c13541SPoul-Henning Kamp #include <net/if.h>
71530c0060SRobert Watson #include <net/vnet.h>
72530c0060SRobert Watson 
7375c13541SPoul-Henning Kamp #include <netinet/in.h>
74530c0060SRobert Watson 
75413628a7SBjoern A. Zeeb #ifdef DDB
76413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
77413628a7SBjoern A. Zeeb #endif /* DDB */
7875c13541SPoul-Henning Kamp 
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80aed55708SRobert Watson 
81c3188289SKyle Evans #define	PRISON0_HOSTUUID_MODULE	"hostuuid"
828986e3a0SJamie Gritton 
8375c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
84d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8575c13541SPoul-Henning Kamp 
86592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
87592bcae8SBjoern A. Zeeb #ifdef INET
88592bcae8SBjoern A. Zeeb #ifdef INET6
89592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
90592bcae8SBjoern A. Zeeb #else
91592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
92592bcae8SBjoern A. Zeeb #endif
93592bcae8SBjoern A. Zeeb #else /* !INET */
94592bcae8SBjoern A. Zeeb #ifdef INET6
95592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
96592bcae8SBjoern A. Zeeb #else
97592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
98592bcae8SBjoern A. Zeeb #endif
99592bcae8SBjoern A. Zeeb #endif
100592bcae8SBjoern A. Zeeb 
1010304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
1020304c731SJamie Gritton struct prison prison0 = {
1030304c731SJamie Gritton 	.pr_id		= 0,
1040304c731SJamie Gritton 	.pr_name	= "0",
1050304c731SJamie Gritton 	.pr_ref		= 1,
1060304c731SJamie Gritton 	.pr_uref	= 1,
1070304c731SJamie Gritton 	.pr_path	= "/",
1080304c731SJamie Gritton 	.pr_securelevel	= -1,
1090cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
1101158508aSJamie Gritton 	.pr_state	= PRISON_STATE_ALIVE,
111b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1128986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
11313e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
114eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
115592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
116eb79e1c7SBjoern A. Zeeb #else
117592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
118eb79e1c7SBjoern A. Zeeb #endif
1190e5c6bd4SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL_STATIC,
1200304c731SJamie Gritton };
1210304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
12283f1e257SRobert Watson 
123672756aaSJamie Gritton struct bool_flags {
124672756aaSJamie Gritton 	const char	*name;
125672756aaSJamie Gritton 	const char	*noname;
1265d58f959SJamie Gritton 	volatile u_int	 flag;
127672756aaSJamie Gritton };
128672756aaSJamie Gritton struct jailsys_flags {
129672756aaSJamie Gritton 	const char	*name;
130672756aaSJamie Gritton 	unsigned	 disable;
131672756aaSJamie Gritton 	unsigned	 new;
132672756aaSJamie Gritton };
133672756aaSJamie Gritton 
134a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
135dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
136b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
137b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
138a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1392110d913SXin LI int	lastprid = 0;
140fd7a8150SMike Barcroft 
1417de883c8SJamie Gritton static int get_next_prid(struct prison **insprp);
142f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
143b3059e09SRobert Watson static void prison_complete(void *context, int pending);
144b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
145c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
146f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags);
147a9f7455cSJamie Gritton static void prison_cleanup(struct prison *pr);
148f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr);
149c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr);
1500fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag,
1510fe74ae6SJamie Gritton     int enable);
1520304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
153a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
154a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
155c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
156a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
157a7ad07bfSEdward Tomasz Napierala #endif
158fd7a8150SMike Barcroft 
159b38ff370SJamie Gritton /* Flags for prison_deref */
1602a4b2251SJamie Gritton #define	PD_DEREF	0x01	/* Decrement pr_ref */
1612a4b2251SJamie Gritton #define	PD_DEUREF	0x02	/* Decrement pr_uref */
162c861373bSJamie Gritton #define	PD_KILL		0x04	/* Remove jail, kill processes, etc */
163c861373bSJamie Gritton #define	PD_LOCKED	0x10	/* pr_mtx is held */
164c861373bSJamie Gritton #define	PD_LIST_SLOCKED	0x20	/* allprison_lock is held shared */
165c861373bSJamie Gritton #define	PD_LIST_XLOCKED	0x40	/* allprison_lock is held exclusive */
166589e4c1dSJamie Gritton #define PD_OP_FLAGS	0x07	/* Operation flags */
167589e4c1dSJamie Gritton #define PD_LOCK_FLAGS	0x70	/* Lock status flags */
168fd7a8150SMike Barcroft 
1690304c731SJamie Gritton /*
1705cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1715cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1725cc70397SBjoern A. Zeeb  * terminating entry.
1730304c731SJamie Gritton  */
174672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = {
175672756aaSJamie Gritton 	{"persist", "nopersist", PR_PERSIST},
176592bcae8SBjoern A. Zeeb #ifdef INET
177672756aaSJamie Gritton 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
178592bcae8SBjoern A. Zeeb #endif
179592bcae8SBjoern A. Zeeb #ifdef INET6
180672756aaSJamie Gritton 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
181592bcae8SBjoern A. Zeeb #endif
1820304c731SJamie Gritton };
183672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
1840304c731SJamie Gritton 
185672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = {
1867cbf7213SJamie Gritton 	{"host", 0, PR_HOST},
1877cbf7213SJamie Gritton #ifdef VIMAGE
1887cbf7213SJamie Gritton 	{"vnet", 0, PR_VNET},
1897cbf7213SJamie Gritton #endif
1900304c731SJamie Gritton #ifdef INET
1916a3f2779SJamie Gritton 	{"ip4", PR_IP4_USER, PR_IP4_USER},
1920304c731SJamie Gritton #endif
1930304c731SJamie Gritton #ifdef INET6
1946a3f2779SJamie Gritton 	{"ip6", PR_IP6_USER, PR_IP6_USER},
195679e1390SJamie Gritton #endif
1960304c731SJamie Gritton };
1975cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1980304c731SJamie Gritton 
1995d58f959SJamie Gritton /*
2005d58f959SJamie Gritton  * Make this array full-size so dynamic parameters can be added.
2015d58f959SJamie Gritton  * It is protected by prison0.mtx, but lockless reading is allowed
2025d58f959SJamie Gritton  * with an atomic check of the flag values.
2035d58f959SJamie Gritton  */
2040e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
205672756aaSJamie Gritton 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
206672756aaSJamie Gritton 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
207672756aaSJamie Gritton 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
208672756aaSJamie Gritton 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
209672756aaSJamie Gritton 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
210672756aaSJamie Gritton 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
211672756aaSJamie Gritton 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
212ccd6ac9fSAntoine Brodin 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
213672756aaSJamie Gritton 	{"allow.reserved_ports", "allow.noreserved_ports",
214672756aaSJamie Gritton 	 PR_ALLOW_RESERVED_PORTS},
215b19d66fdSJamie Gritton 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
216b3079544SJamie Gritton 	{"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
217b3079544SJamie Gritton 	 PR_ALLOW_UNPRIV_DEBUG},
21805e1e482SMariusz Zaborski 	{"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
2190304c731SJamie Gritton };
2205d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
221672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
2220304c731SJamie Gritton 
223b3079544SJamie Gritton #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | \
224b3079544SJamie Gritton 					 PR_ALLOW_RESERVED_PORTS | \
22505e1e482SMariusz Zaborski 					 PR_ALLOW_UNPRIV_DEBUG | \
22605e1e482SMariusz Zaborski 					 PR_ALLOW_SUSER)
22742bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
228bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2290304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
23042bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2310cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2320304c731SJamie Gritton #if defined(INET) || defined(INET6)
233e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2340304c731SJamie Gritton #endif
2350304c731SJamie Gritton 
236b96bd95bSIan Lepore /*
237b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
238b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
239b96bd95bSIan Lepore  */
240b96bd95bSIan Lepore void
241b96bd95bSIan Lepore prison0_init(void)
242b96bd95bSIan Lepore {
243c3188289SKyle Evans 	uint8_t *file, *data;
244c3188289SKyle Evans 	size_t size;
245d2ef3774SJessica Clarke 	char buf[sizeof(prison0.pr_hostuuid)];
246d2ef3774SJessica Clarke 	bool valid;
247b96bd95bSIan Lepore 
248b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
249b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
250b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
251c3188289SKyle Evans 
252c3188289SKyle Evans 	/* If we have a preloaded hostuuid, use it. */
253c3188289SKyle Evans 	file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
254c3188289SKyle Evans 	if (file != NULL) {
255c3188289SKyle Evans 		data = preload_fetch_addr(file);
256c3188289SKyle Evans 		size = preload_fetch_size(file);
257c3188289SKyle Evans 		if (data != NULL) {
258c3188289SKyle Evans 			/*
259c3188289SKyle Evans 			 * The preloaded data may include trailing whitespace, almost
260c3188289SKyle Evans 			 * certainly a newline; skip over any whitespace or
261c3188289SKyle Evans 			 * non-printable characters to be safe.
262c3188289SKyle Evans 			 */
263c3188289SKyle Evans 			while (size > 0 && data[size - 1] <= 0x20) {
264b6be9566SColin Percival 				size--;
265c3188289SKyle Evans 			}
266d2ef3774SJessica Clarke 
267d2ef3774SJessica Clarke 			valid = false;
268d2ef3774SJessica Clarke 
269d2ef3774SJessica Clarke 			/*
270d2ef3774SJessica Clarke 			 * Not NUL-terminated when passed from loader, but
271d2ef3774SJessica Clarke 			 * validate_uuid requires that due to using sscanf (as
272d2ef3774SJessica Clarke 			 * does the subsequent strlcpy, since it still reads
273d2ef3774SJessica Clarke 			 * past the given size to return the true length);
274d2ef3774SJessica Clarke 			 * bounce to a temporary buffer to fix.
275d2ef3774SJessica Clarke 			 */
276d2ef3774SJessica Clarke 			if (size >= sizeof(buf))
277d2ef3774SJessica Clarke 				goto done;
278d2ef3774SJessica Clarke 
279d2ef3774SJessica Clarke 			memcpy(buf, data, size);
280d2ef3774SJessica Clarke 			buf[size] = '\0';
281d2ef3774SJessica Clarke 
282d2ef3774SJessica Clarke 			if (validate_uuid(buf, size, NULL, 0) != 0)
283d2ef3774SJessica Clarke 				goto done;
284d2ef3774SJessica Clarke 
285d2ef3774SJessica Clarke 			valid = true;
286d2ef3774SJessica Clarke 			(void)strlcpy(prison0.pr_hostuuid, buf,
287d2ef3774SJessica Clarke 			    sizeof(prison0.pr_hostuuid));
288d2ef3774SJessica Clarke 
289d2ef3774SJessica Clarke done:
290d2ef3774SJessica Clarke 			if (bootverbose && !valid) {
291330f110bSColin Percival 				printf("hostuuid: preload data malformed: '%.*s'\n",
292330f110bSColin Percival 				    (int)size, data);
293c3188289SKyle Evans 			}
294c3188289SKyle Evans 		}
295c3188289SKyle Evans 	}
296c3188289SKyle Evans 	if (bootverbose)
297c3188289SKyle Evans 		printf("hostuuid: using %s\n", prison0.pr_hostuuid);
298b96bd95bSIan Lepore }
299b96bd95bSIan Lepore 
300116734c4SMatthew Dillon /*
3019ddb7954SMike Barcroft  * struct jail_args {
3029ddb7954SMike Barcroft  *	struct jail *jail;
3039ddb7954SMike Barcroft  * };
304116734c4SMatthew Dillon  */
30575c13541SPoul-Henning Kamp int
306c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap)
30775c13541SPoul-Henning Kamp {
308413628a7SBjoern A. Zeeb 	uint32_t version;
309413628a7SBjoern A. Zeeb 	int error;
3100304c731SJamie Gritton 	struct jail j;
311413628a7SBjoern A. Zeeb 
312413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
313413628a7SBjoern A. Zeeb 	if (error)
314413628a7SBjoern A. Zeeb 		return (error);
315413628a7SBjoern A. Zeeb 
316413628a7SBjoern A. Zeeb 	switch (version) {
317413628a7SBjoern A. Zeeb 	case 0:
318413628a7SBjoern A. Zeeb 	{
319413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
320413628a7SBjoern A. Zeeb 
3210304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
3220304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
323413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
324413628a7SBjoern A. Zeeb 		if (error)
325413628a7SBjoern A. Zeeb 			return (error);
3260304c731SJamie Gritton 		j.version = j0.version;
3270304c731SJamie Gritton 		j.path = j0.path;
3280304c731SJamie Gritton 		j.hostname = j0.hostname;
329b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
330413628a7SBjoern A. Zeeb 		break;
331413628a7SBjoern A. Zeeb 	}
332413628a7SBjoern A. Zeeb 
333413628a7SBjoern A. Zeeb 	case 1:
334413628a7SBjoern A. Zeeb 		/*
335413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
336413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
337413628a7SBjoern A. Zeeb 		 */
338413628a7SBjoern A. Zeeb 		return (EINVAL);
339413628a7SBjoern A. Zeeb 
340413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
341413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
342413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
343413628a7SBjoern A. Zeeb 		if (error)
344413628a7SBjoern A. Zeeb 			return (error);
3450304c731SJamie Gritton 		break;
3460304c731SJamie Gritton 
3470304c731SJamie Gritton 	default:
3480304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3490304c731SJamie Gritton 		return (EINVAL);
3500304c731SJamie Gritton 	}
351c542c43eSJamie Gritton 	return (kern_jail(td, &j));
3520304c731SJamie Gritton }
3530304c731SJamie Gritton 
3540304c731SJamie Gritton int
355c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j)
3560304c731SJamie Gritton {
357c542c43eSJamie Gritton 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
358e92e0574SJamie Gritton #ifdef INET
359e92e0574SJamie Gritton 			    + 1
360e92e0574SJamie Gritton #endif
361e92e0574SJamie Gritton #ifdef INET6
362e92e0574SJamie Gritton 			    + 1
363e92e0574SJamie Gritton #endif
364e92e0574SJamie Gritton 			    )];
3650304c731SJamie Gritton 	struct uio opt;
3660304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
367672756aaSJamie Gritton 	struct bool_flags *bf;
3680304c731SJamie Gritton #ifdef INET
369e92e0574SJamie Gritton 	uint32_t ip4s;
3700304c731SJamie Gritton 	struct in_addr *u_ip4;
3710304c731SJamie Gritton #endif
3720304c731SJamie Gritton #ifdef INET6
3730304c731SJamie Gritton 	struct in6_addr *u_ip6;
3740304c731SJamie Gritton #endif
3750304c731SJamie Gritton 	size_t tmplen;
376c542c43eSJamie Gritton 	int error, enforce_statfs;
3770304c731SJamie Gritton 
3780304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3790304c731SJamie Gritton 	opt.uio_iov = optiov;
3800304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3810304c731SJamie Gritton 	opt.uio_offset = -1;
3820304c731SJamie Gritton 	opt.uio_resid = -1;
3830304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3840304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3850304c731SJamie Gritton 	opt.uio_td = td;
3860304c731SJamie Gritton 
3870304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3880304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
389672756aaSJamie Gritton 		for (bf = pr_flag_allow;
3900e5c6bd4SJamie Gritton 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
3915d58f959SJamie Gritton 			atomic_load_int(&bf->flag) != 0;
392672756aaSJamie Gritton 		     bf++) {
393672756aaSJamie Gritton 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
394672756aaSJamie Gritton 			    (jail_default_allow & bf->flag)
395672756aaSJamie Gritton 			    ? bf->name : bf->noname);
3960304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3970304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3980304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3990304c731SJamie Gritton 		}
4000304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
4010304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
4020304c731SJamie Gritton 		opt.uio_iovcnt++;
4030304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
4040304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
4050304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
4060304c731SJamie Gritton 		opt.uio_iovcnt++;
4070304c731SJamie Gritton 	}
4080304c731SJamie Gritton 
409b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
410b38ff370SJamie Gritton #ifdef INET
4110304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
4120304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
413b38ff370SJamie Gritton 		return (EINVAL);
4140304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
415b38ff370SJamie Gritton #else
4160304c731SJamie Gritton 	if (j->ip4s > 0)
417b38ff370SJamie Gritton 		return (EINVAL);
418b38ff370SJamie Gritton #endif
419b38ff370SJamie Gritton #ifdef INET6
4200304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
421b38ff370SJamie Gritton 		return (EINVAL);
4220304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
423b38ff370SJamie Gritton #else
4240304c731SJamie Gritton 	if (j->ip6s > 0)
425b38ff370SJamie Gritton 		return (EINVAL);
426b38ff370SJamie Gritton #endif
427b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
428b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
429b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
430b38ff370SJamie Gritton #ifdef INET
431b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
432b38ff370SJamie Gritton #endif
433b38ff370SJamie Gritton #ifdef INET6
434b38ff370SJamie Gritton #ifdef INET
4350304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
436b38ff370SJamie Gritton #else
437b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
438b38ff370SJamie Gritton #endif
439b38ff370SJamie Gritton #endif
4400304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4410304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4420304c731SJamie Gritton 	opt.uio_iovcnt++;
4430304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4440304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4450304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
446b38ff370SJamie Gritton 	if (error) {
447b38ff370SJamie Gritton 		free(u_path, M_TEMP);
448b38ff370SJamie Gritton 		return (error);
449b38ff370SJamie Gritton 	}
4500304c731SJamie Gritton 	opt.uio_iovcnt++;
4510304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4520304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4530304c731SJamie Gritton 	opt.uio_iovcnt++;
4540304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4550304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4560304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
457b38ff370SJamie Gritton 	if (error) {
458b38ff370SJamie Gritton 		free(u_path, M_TEMP);
459b38ff370SJamie Gritton 		return (error);
460b38ff370SJamie Gritton 	}
4610304c731SJamie Gritton 	opt.uio_iovcnt++;
4620304c731SJamie Gritton 	if (j->jailname != NULL) {
463b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
464b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
465b38ff370SJamie Gritton 		opt.uio_iovcnt++;
466b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4670304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
468b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
469b38ff370SJamie Gritton 		if (error) {
470b38ff370SJamie Gritton 			free(u_path, M_TEMP);
471b38ff370SJamie Gritton 			return (error);
472b38ff370SJamie Gritton 		}
473b38ff370SJamie Gritton 		opt.uio_iovcnt++;
474b38ff370SJamie Gritton 	}
475b38ff370SJamie Gritton #ifdef INET
476b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
477b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
478b38ff370SJamie Gritton 	opt.uio_iovcnt++;
479b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4800304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4810304c731SJamie Gritton 	if (j->version == 0)
4820304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4830304c731SJamie Gritton 	else {
4840304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
485b38ff370SJamie Gritton 		if (error) {
486b38ff370SJamie Gritton 			free(u_path, M_TEMP);
487b38ff370SJamie Gritton 			return (error);
488b38ff370SJamie Gritton 		}
4890304c731SJamie Gritton 	}
490b38ff370SJamie Gritton 	opt.uio_iovcnt++;
491b38ff370SJamie Gritton #endif
492b38ff370SJamie Gritton #ifdef INET6
493b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
494b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
495b38ff370SJamie Gritton 	opt.uio_iovcnt++;
496b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4970304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4980304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
499b38ff370SJamie Gritton 	if (error) {
500b38ff370SJamie Gritton 		free(u_path, M_TEMP);
501b38ff370SJamie Gritton 		return (error);
502b38ff370SJamie Gritton 	}
503b38ff370SJamie Gritton 	opt.uio_iovcnt++;
504b38ff370SJamie Gritton #endif
50502abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
5060304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
507b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
508b38ff370SJamie Gritton 	free(u_path, M_TEMP);
509b38ff370SJamie Gritton 	return (error);
510b38ff370SJamie Gritton }
511b38ff370SJamie Gritton 
512b38ff370SJamie Gritton /*
513b38ff370SJamie Gritton  * struct jail_set_args {
514b38ff370SJamie Gritton  *	struct iovec *iovp;
515b38ff370SJamie Gritton  *	unsigned int iovcnt;
516b38ff370SJamie Gritton  *	int flags;
517b38ff370SJamie Gritton  * };
518b38ff370SJamie Gritton  */
519b38ff370SJamie Gritton int
5208451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
521b38ff370SJamie Gritton {
522b38ff370SJamie Gritton 	struct uio *auio;
523b38ff370SJamie Gritton 	int error;
524b38ff370SJamie Gritton 
525b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
526b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
527b38ff370SJamie Gritton 		return (EINVAL);
528b38ff370SJamie Gritton 
529b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
530b38ff370SJamie Gritton 	if (error)
531b38ff370SJamie Gritton 		return (error);
532b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
533b38ff370SJamie Gritton 	free(auio, M_IOV);
534b38ff370SJamie Gritton 	return (error);
535413628a7SBjoern A. Zeeb }
536413628a7SBjoern A. Zeeb 
537eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
538eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *);
539eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *);
540eb8dcdeaSGleb Smirnoff static const struct pr_family {
541eb8dcdeaSGleb Smirnoff 	size_t			size;
542eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t	*cmp;
543eb8dcdeaSGleb Smirnoff 	prison_addr_valid_t	*valid;
544eb8dcdeaSGleb Smirnoff 	int			ip_flag;
545eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = {
546eb8dcdeaSGleb Smirnoff #ifdef INET
547eb8dcdeaSGleb Smirnoff 	[PR_INET] = {
548eb8dcdeaSGleb Smirnoff 		.size = sizeof(struct in_addr),
549eb8dcdeaSGleb Smirnoff 		.cmp = prison_qcmp_v4,
550eb8dcdeaSGleb Smirnoff 		.valid = prison_valid_v4,
551eb8dcdeaSGleb Smirnoff 		.ip_flag = PR_IP4_USER,
552eb8dcdeaSGleb Smirnoff 	 },
553eb8dcdeaSGleb Smirnoff #endif
554eb8dcdeaSGleb Smirnoff #ifdef INET6
555eb8dcdeaSGleb Smirnoff 	[PR_INET6] = {
556eb8dcdeaSGleb Smirnoff 		.size = sizeof(struct in6_addr),
557eb8dcdeaSGleb Smirnoff 		.cmp = prison_qcmp_v6,
558eb8dcdeaSGleb Smirnoff 		.valid = prison_valid_v6,
559eb8dcdeaSGleb Smirnoff 		.ip_flag = PR_IP6_USER,
560eb8dcdeaSGleb Smirnoff 	},
561eb8dcdeaSGleb Smirnoff #endif
562eb8dcdeaSGleb Smirnoff };
563eb8dcdeaSGleb Smirnoff 
564eb8dcdeaSGleb Smirnoff /*
565eb8dcdeaSGleb Smirnoff  * Network address lists (pr_addrs) allocation for jails.  The addresses
566eb8dcdeaSGleb Smirnoff  * are accessed locklessly by the network stack, thus need to be protected by
567eb8dcdeaSGleb Smirnoff  * the network epoch.
568eb8dcdeaSGleb Smirnoff  */
569eb8dcdeaSGleb Smirnoff struct prison_ip {
570eb8dcdeaSGleb Smirnoff 	struct epoch_context ctx;
571eb8dcdeaSGleb Smirnoff 	uint32_t	ips;
572eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C
573eb8dcdeaSGleb Smirnoff 	union {
574eb8dcdeaSGleb Smirnoff 		struct in_addr pr_ip4[];
575eb8dcdeaSGleb Smirnoff 		struct in6_addr pr_ip6[];
576eb8dcdeaSGleb Smirnoff 	};
577eb8dcdeaSGleb Smirnoff #else /* No future C :( */
578eb8dcdeaSGleb Smirnoff #define	PR_IP(pip, i)	((const char *)((pip) + 1) + pr_families[af].size * (i))
579eb8dcdeaSGleb Smirnoff #define	PR_IPD(pip, i)	((char *)((pip) + 1) + pr_families[af].size * (i))
580eb8dcdeaSGleb Smirnoff #endif
581eb8dcdeaSGleb Smirnoff };
582eb8dcdeaSGleb Smirnoff 
583eb8dcdeaSGleb Smirnoff static struct prison_ip *
584eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
585eb8dcdeaSGleb Smirnoff {
586eb8dcdeaSGleb Smirnoff 	struct prison_ip *pip;
587eb8dcdeaSGleb Smirnoff 
588eb8dcdeaSGleb Smirnoff 	pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
589eb8dcdeaSGleb Smirnoff 	    M_PRISON, flags);
590eb8dcdeaSGleb Smirnoff 	if (pip != NULL)
591eb8dcdeaSGleb Smirnoff 		pip->ips = cnt;
592eb8dcdeaSGleb Smirnoff 	return (pip);
593eb8dcdeaSGleb Smirnoff }
594eb8dcdeaSGleb Smirnoff 
595eb8dcdeaSGleb Smirnoff /*
596eb8dcdeaSGleb Smirnoff  * Allocate and copyin user supplied address list, sorting and validating.
597eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
598eb8dcdeaSGleb Smirnoff  */
599eb8dcdeaSGleb Smirnoff static struct prison_ip *
600eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
601eb8dcdeaSGleb Smirnoff {
602eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
603eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
604eb8dcdeaSGleb Smirnoff 	struct prison_ip *pip;
605eb8dcdeaSGleb Smirnoff 
606eb8dcdeaSGleb Smirnoff 	pip = prison_ip_alloc(af, cnt, M_WAITOK);
607eb8dcdeaSGleb Smirnoff 	bcopy(op, pip + 1, cnt * size);
608eb8dcdeaSGleb Smirnoff 	/*
609eb8dcdeaSGleb Smirnoff 	 * IP addresses are all sorted but ip[0] to preserve
610eb8dcdeaSGleb Smirnoff 	 * the primary IP address as given from userland.
611eb8dcdeaSGleb Smirnoff 	 * This special IP is used for unbound outgoing
612eb8dcdeaSGleb Smirnoff 	 * connections as well for "loopback" traffic in case
613eb8dcdeaSGleb Smirnoff 	 * source address selection cannot find any more fitting
614eb8dcdeaSGleb Smirnoff 	 * address to connect from.
615eb8dcdeaSGleb Smirnoff 	 */
616eb8dcdeaSGleb Smirnoff 	if (cnt > 1)
617eb8dcdeaSGleb Smirnoff 		qsort((char *)(pip + 1) + size, cnt - 1, size,
618eb8dcdeaSGleb Smirnoff 		    pr_families[af].cmp);
619eb8dcdeaSGleb Smirnoff 	/*
620eb8dcdeaSGleb Smirnoff 	 * Check for duplicate addresses and do some simple
621eb8dcdeaSGleb Smirnoff 	 * zero and broadcast checks. If users give other bogus
622eb8dcdeaSGleb Smirnoff 	 * addresses it is their problem.
623eb8dcdeaSGleb Smirnoff 	 */
624eb8dcdeaSGleb Smirnoff 	for (int i = 0; i < cnt; i++) {
625eb8dcdeaSGleb Smirnoff 		if (!pr_families[af].valid(PR_IP(pip, i))) {
626eb8dcdeaSGleb Smirnoff 			free(pip, M_PRISON);
627eb8dcdeaSGleb Smirnoff 			return (NULL);
628eb8dcdeaSGleb Smirnoff 		}
629eb8dcdeaSGleb Smirnoff 		if (i + 1 < cnt &&
630eb8dcdeaSGleb Smirnoff 		    (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 ||
631eb8dcdeaSGleb Smirnoff 		     cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) {
632eb8dcdeaSGleb Smirnoff 			free(pip, M_PRISON);
633eb8dcdeaSGleb Smirnoff 			return (NULL);
634eb8dcdeaSGleb Smirnoff 		}
635eb8dcdeaSGleb Smirnoff 	}
636eb8dcdeaSGleb Smirnoff 
637eb8dcdeaSGleb Smirnoff 	return (pip);
638eb8dcdeaSGleb Smirnoff }
639eb8dcdeaSGleb Smirnoff 
640eb8dcdeaSGleb Smirnoff /*
641eb8dcdeaSGleb Smirnoff  * Allocate and dup parent prison address list.
642eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
643eb8dcdeaSGleb Smirnoff  */
644eb8dcdeaSGleb Smirnoff static void
645eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
646eb8dcdeaSGleb Smirnoff {
647eb8dcdeaSGleb Smirnoff 
648eb8dcdeaSGleb Smirnoff 	if (ppr->pr_addrs[af] != NULL) {
649eb8dcdeaSGleb Smirnoff 		pr->pr_addrs[af] = prison_ip_alloc(af,
650eb8dcdeaSGleb Smirnoff 		    ppr->pr_addrs[af]->ips, M_WAITOK);
651eb8dcdeaSGleb Smirnoff 		bcopy(ppr->pr_addrs[af], pr->pr_addrs[af],
652eb8dcdeaSGleb Smirnoff 		    pr->pr_addrs[af]->ips * pr_families[af].size);
653eb8dcdeaSGleb Smirnoff 	}
654eb8dcdeaSGleb Smirnoff }
655eb8dcdeaSGleb Smirnoff 
656eb8dcdeaSGleb Smirnoff /*
657eb8dcdeaSGleb Smirnoff  * Make sure the new set of IP addresses is a subset of the parent's list.
658eb8dcdeaSGleb Smirnoff  * Don't worry about the parent being unlocked, as any setting is done with
659eb8dcdeaSGleb Smirnoff  * allprison_lock held.
660eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
661eb8dcdeaSGleb Smirnoff  */
662eb8dcdeaSGleb Smirnoff static bool
663eb8dcdeaSGleb Smirnoff prison_ip_parent_match(const struct prison_ip *ppip,
664eb8dcdeaSGleb Smirnoff     const struct prison_ip *pip, const pr_family_t af)
665eb8dcdeaSGleb Smirnoff {
666eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
667eb8dcdeaSGleb Smirnoff 	int i, j;
668eb8dcdeaSGleb Smirnoff 
669eb8dcdeaSGleb Smirnoff 	if (ppip == NULL)
670eb8dcdeaSGleb Smirnoff 		return (false);
671eb8dcdeaSGleb Smirnoff 
672eb8dcdeaSGleb Smirnoff 	for (i = 0; i < ppip->ips; i++)
673eb8dcdeaSGleb Smirnoff 		if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0)
674eb8dcdeaSGleb Smirnoff 			break;
675eb8dcdeaSGleb Smirnoff 
676eb8dcdeaSGleb Smirnoff 	if (i == ppip->ips)
677eb8dcdeaSGleb Smirnoff 		/* Main address not present in parent. */
678eb8dcdeaSGleb Smirnoff 		return (false);
679eb8dcdeaSGleb Smirnoff 
680eb8dcdeaSGleb Smirnoff 	if (pip->ips > 1) {
681eb8dcdeaSGleb Smirnoff 		for (i = j = 1; i < pip->ips; i++) {
682eb8dcdeaSGleb Smirnoff 			if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0)
683eb8dcdeaSGleb Smirnoff 				/* Equals to parent primary address. */
684eb8dcdeaSGleb Smirnoff 				continue;
685eb8dcdeaSGleb Smirnoff 			for (; j < ppip->ips; j++)
686eb8dcdeaSGleb Smirnoff 				if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0)
687eb8dcdeaSGleb Smirnoff 					break;
688eb8dcdeaSGleb Smirnoff 			if (j == ppip->ips)
689eb8dcdeaSGleb Smirnoff 				break;
690eb8dcdeaSGleb Smirnoff 		}
691eb8dcdeaSGleb Smirnoff 		if (j == ppip->ips)
692eb8dcdeaSGleb Smirnoff 			/* Address not present in parent. */
693eb8dcdeaSGleb Smirnoff 			return (false);
694eb8dcdeaSGleb Smirnoff 	}
695eb8dcdeaSGleb Smirnoff 	return (true);
696eb8dcdeaSGleb Smirnoff }
697eb8dcdeaSGleb Smirnoff 
698eb8dcdeaSGleb Smirnoff /*
699eb8dcdeaSGleb Smirnoff  * Check for conflicting IP addresses.  We permit them if there is no more
700eb8dcdeaSGleb Smirnoff  * than one IP on each jail.  If there is a duplicate on a jail with more
701eb8dcdeaSGleb Smirnoff  * than one IP stop checking and return error.
702eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
703eb8dcdeaSGleb Smirnoff  */
704eb8dcdeaSGleb Smirnoff static bool
705eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
706eb8dcdeaSGleb Smirnoff     const struct prison_ip *pip, pr_family_t af)
707eb8dcdeaSGleb Smirnoff {
708eb8dcdeaSGleb Smirnoff 	const struct prison *tppr, *tpr;
709eb8dcdeaSGleb Smirnoff 	int descend;
710eb8dcdeaSGleb Smirnoff 
711eb8dcdeaSGleb Smirnoff #ifdef VIMAGE
712eb8dcdeaSGleb Smirnoff 	for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
713eb8dcdeaSGleb Smirnoff 		if (tppr->pr_flags & PR_VNET)
714eb8dcdeaSGleb Smirnoff 			break;
715eb8dcdeaSGleb Smirnoff #else
716eb8dcdeaSGleb Smirnoff 	tppr = &prison0;
717eb8dcdeaSGleb Smirnoff #endif
718eb8dcdeaSGleb Smirnoff 	FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
719eb8dcdeaSGleb Smirnoff 		if (tpr == pr ||
720eb8dcdeaSGleb Smirnoff #ifdef VIMAGE
721eb8dcdeaSGleb Smirnoff 		    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
722eb8dcdeaSGleb Smirnoff #endif
723eb8dcdeaSGleb Smirnoff 		    !prison_isalive(tpr)) {
724eb8dcdeaSGleb Smirnoff 			descend = 0;
725eb8dcdeaSGleb Smirnoff 			continue;
726eb8dcdeaSGleb Smirnoff 		}
727eb8dcdeaSGleb Smirnoff 		if (!(tpr->pr_flags & pr_families[af].ip_flag))
728eb8dcdeaSGleb Smirnoff 			continue;
729eb8dcdeaSGleb Smirnoff 		descend = 0;
730eb8dcdeaSGleb Smirnoff 		if (tpr->pr_addrs[af] == NULL ||
731eb8dcdeaSGleb Smirnoff 		    (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
732eb8dcdeaSGleb Smirnoff 			continue;
733eb8dcdeaSGleb Smirnoff 		for (int i = 0; i < pip->ips; i++)
734eb8dcdeaSGleb Smirnoff 			if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0)
735eb8dcdeaSGleb Smirnoff 				return (false);
736eb8dcdeaSGleb Smirnoff 	}
737eb8dcdeaSGleb Smirnoff 
738eb8dcdeaSGleb Smirnoff 	return (true);
739eb8dcdeaSGleb Smirnoff }
740eb8dcdeaSGleb Smirnoff 
741eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0,
742eb8dcdeaSGleb Smirnoff     "prison must start with epoch context");
743eb8dcdeaSGleb Smirnoff static void
744eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx)
745eb8dcdeaSGleb Smirnoff {
746eb8dcdeaSGleb Smirnoff 
747eb8dcdeaSGleb Smirnoff 	free(ctx, M_PRISON);
748eb8dcdeaSGleb Smirnoff }
749eb8dcdeaSGleb Smirnoff 
750eb8dcdeaSGleb Smirnoff static void
751eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip)
752eb8dcdeaSGleb Smirnoff {
753eb8dcdeaSGleb Smirnoff 
754eb8dcdeaSGleb Smirnoff 	if (pip != NULL)
755eb8dcdeaSGleb Smirnoff 		NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
756eb8dcdeaSGleb Smirnoff }
757eb8dcdeaSGleb Smirnoff 
758eb8dcdeaSGleb Smirnoff static void
759eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
760eb8dcdeaSGleb Smirnoff {
761eb8dcdeaSGleb Smirnoff 	struct prison_ip **mem, *old;
762eb8dcdeaSGleb Smirnoff 
763eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
764eb8dcdeaSGleb Smirnoff 
765eb8dcdeaSGleb Smirnoff 	mem = &pr->pr_addrs[af];
766eb8dcdeaSGleb Smirnoff 
767eb8dcdeaSGleb Smirnoff 	old = *mem;
768eb8dcdeaSGleb Smirnoff 	ck_pr_store_ptr(mem, new);
769eb8dcdeaSGleb Smirnoff 	prison_ip_free(old);
770eb8dcdeaSGleb Smirnoff }
771eb8dcdeaSGleb Smirnoff 
772eb8dcdeaSGleb Smirnoff /*
773eb8dcdeaSGleb Smirnoff  * Restrict a prison's IP address list with its parent's, possibly replacing
774eb8dcdeaSGleb Smirnoff  * it.  Return true if the replacement buffer was used (or would have been).
775eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
776eb8dcdeaSGleb Smirnoff  */
777eb8dcdeaSGleb Smirnoff static bool
778eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af,
779eb8dcdeaSGleb Smirnoff     struct prison_ip *new)
780eb8dcdeaSGleb Smirnoff {
781eb8dcdeaSGleb Smirnoff 	const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
782eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip = pr->pr_addrs[af];
783eb8dcdeaSGleb Smirnoff 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
784eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
785eb8dcdeaSGleb Smirnoff 	uint32_t ips;
786eb8dcdeaSGleb Smirnoff 	bool alloced;
787eb8dcdeaSGleb Smirnoff 
788eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
789eb8dcdeaSGleb Smirnoff 
790eb8dcdeaSGleb Smirnoff 	/*
791eb8dcdeaSGleb Smirnoff 	 * Due to epoch-synchronized access to the IP address lists we always
792eb8dcdeaSGleb Smirnoff 	 * allocate a new list even if the old one has enough space.  We could
793eb8dcdeaSGleb Smirnoff 	 * atomically update an IPv4 address inside a list, but that would
794eb8dcdeaSGleb Smirnoff 	 * screw up sorting, and in case of IPv6 we can't even atomically write
795eb8dcdeaSGleb Smirnoff 	 * one.
796eb8dcdeaSGleb Smirnoff 	 */
797eb8dcdeaSGleb Smirnoff 	ips = (pr->pr_flags & pr_families[af].ip_flag) ? pip->ips : ppip->ips;
798eb8dcdeaSGleb Smirnoff 	if (ips == 0) {
799eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, af, NULL);
800eb8dcdeaSGleb Smirnoff 		return (false);
801eb8dcdeaSGleb Smirnoff 	}
802eb8dcdeaSGleb Smirnoff 	if (new == NULL) {
803eb8dcdeaSGleb Smirnoff 		new = prison_ip_alloc(af, ips, M_NOWAIT);
804eb8dcdeaSGleb Smirnoff 		if (new == NULL)
805eb8dcdeaSGleb Smirnoff 			return (true);
806eb8dcdeaSGleb Smirnoff 		alloced = true;
807eb8dcdeaSGleb Smirnoff 	} else
808eb8dcdeaSGleb Smirnoff 		alloced = false;
809eb8dcdeaSGleb Smirnoff 	if (!(pr->pr_flags & pr_families[af].ip_flag)) {
810eb8dcdeaSGleb Smirnoff 		/* This has no user settings, so just copy the parent's list. */
811eb8dcdeaSGleb Smirnoff 		bcopy(ppip, new, ips * size);
812eb8dcdeaSGleb Smirnoff 	} else {
813eb8dcdeaSGleb Smirnoff 		/* Remove addresses that aren't in the parent. */
814eb8dcdeaSGleb Smirnoff 		int i;
815eb8dcdeaSGleb Smirnoff 
816eb8dcdeaSGleb Smirnoff 		i = 0; /* index in pip */
817eb8dcdeaSGleb Smirnoff 		ips = 0; /* index in new */
818eb8dcdeaSGleb Smirnoff 
819eb8dcdeaSGleb Smirnoff 		for (int pi = 0; pi < ppip->ips; pi++)
820eb8dcdeaSGleb Smirnoff 			if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) {
821eb8dcdeaSGleb Smirnoff 				/* Found our primary address in parent. */
822eb8dcdeaSGleb Smirnoff 				bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
823eb8dcdeaSGleb Smirnoff 				i++;
824eb8dcdeaSGleb Smirnoff 				ips++;
825eb8dcdeaSGleb Smirnoff 				break;
826eb8dcdeaSGleb Smirnoff 			}
827eb8dcdeaSGleb Smirnoff 		for (int pi = 1; i < pip->ips; ) {
828eb8dcdeaSGleb Smirnoff 			/* Check against primary, which is unsorted. */
829eb8dcdeaSGleb Smirnoff 			if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) {
830eb8dcdeaSGleb Smirnoff 				/* Matches parent's primary address. */
831eb8dcdeaSGleb Smirnoff 				bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
832eb8dcdeaSGleb Smirnoff 				i++;
833eb8dcdeaSGleb Smirnoff 				ips++;
834eb8dcdeaSGleb Smirnoff 				continue;
835eb8dcdeaSGleb Smirnoff 			}
836eb8dcdeaSGleb Smirnoff 			/* The rest are sorted. */
837eb8dcdeaSGleb Smirnoff 			switch (pi >= ppip->ips ? -1 :
838eb8dcdeaSGleb Smirnoff 				cmp(PR_IP(pip, i), PR_IP(ppip, pi))) {
839eb8dcdeaSGleb Smirnoff 			case -1:
840eb8dcdeaSGleb Smirnoff 				i++;
841eb8dcdeaSGleb Smirnoff 				break;
842eb8dcdeaSGleb Smirnoff 			case 0:
843eb8dcdeaSGleb Smirnoff 				bcopy(PR_IP(pr, i), PR_IPD(new, ips), size);
844eb8dcdeaSGleb Smirnoff 				i++;
845eb8dcdeaSGleb Smirnoff 				pi++;
846eb8dcdeaSGleb Smirnoff 				ips++;
847eb8dcdeaSGleb Smirnoff 				break;
848eb8dcdeaSGleb Smirnoff 			case 1:
849eb8dcdeaSGleb Smirnoff 				pi++;
850eb8dcdeaSGleb Smirnoff 				break;
851eb8dcdeaSGleb Smirnoff 			}
852eb8dcdeaSGleb Smirnoff 		}
853eb8dcdeaSGleb Smirnoff 		if (ips == 0) {
854eb8dcdeaSGleb Smirnoff 			if (alloced)
855eb8dcdeaSGleb Smirnoff 				prison_ip_free(new);
856eb8dcdeaSGleb Smirnoff 			new = NULL;
857eb8dcdeaSGleb Smirnoff 		}
858eb8dcdeaSGleb Smirnoff 	}
859eb8dcdeaSGleb Smirnoff 	prison_ip_set(pr, af, new);
860eb8dcdeaSGleb Smirnoff 	return (new != NULL ? true : false);
861eb8dcdeaSGleb Smirnoff }
862eb8dcdeaSGleb Smirnoff 
863eb8dcdeaSGleb Smirnoff /*
864eb8dcdeaSGleb Smirnoff  * Fast-path check if an address belongs to a prison.
865eb8dcdeaSGleb Smirnoff  */
866eb8dcdeaSGleb Smirnoff int
867eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af,
868eb8dcdeaSGleb Smirnoff     const void *addr)
869eb8dcdeaSGleb Smirnoff {
870eb8dcdeaSGleb Smirnoff 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
871eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip;
872eb8dcdeaSGleb Smirnoff 	int i, a, z, d;
873eb8dcdeaSGleb Smirnoff 
874eb8dcdeaSGleb Smirnoff 	MPASS(mtx_owned(&pr->pr_mtx) ||
875eb8dcdeaSGleb Smirnoff 	    in_epoch(net_epoch_preempt) ||
876eb8dcdeaSGleb Smirnoff 	    sx_xlocked(&allprison_lock));
877eb8dcdeaSGleb Smirnoff 
878eb8dcdeaSGleb Smirnoff 	pip = ck_pr_load_ptr(&pr->pr_addrs[af]);
879eb8dcdeaSGleb Smirnoff 	if (__predict_false(pip == NULL))
880eb8dcdeaSGleb Smirnoff 		return (EAFNOSUPPORT);
881eb8dcdeaSGleb Smirnoff 
882eb8dcdeaSGleb Smirnoff 	/* Check the primary IP. */
883eb8dcdeaSGleb Smirnoff 	if (cmp(PR_IP(pip, 0), addr) == 0)
884eb8dcdeaSGleb Smirnoff 		return (0);
885eb8dcdeaSGleb Smirnoff 
886eb8dcdeaSGleb Smirnoff 	/*
887eb8dcdeaSGleb Smirnoff 	 * All the other IPs are sorted so we can do a binary search.
888eb8dcdeaSGleb Smirnoff 	 */
889eb8dcdeaSGleb Smirnoff 	a = 0;
890eb8dcdeaSGleb Smirnoff 	z = pip->ips - 2;
891eb8dcdeaSGleb Smirnoff 	while (a <= z) {
892eb8dcdeaSGleb Smirnoff 		i = (a + z) / 2;
893eb8dcdeaSGleb Smirnoff 		d = cmp(PR_IP(pip, i + 1), addr);
894eb8dcdeaSGleb Smirnoff 		if (d > 0)
895eb8dcdeaSGleb Smirnoff 			z = i - 1;
896eb8dcdeaSGleb Smirnoff 		else if (d < 0)
897eb8dcdeaSGleb Smirnoff 			a = i + 1;
898eb8dcdeaSGleb Smirnoff 		else
899eb8dcdeaSGleb Smirnoff 			return (0);
900eb8dcdeaSGleb Smirnoff 	}
901eb8dcdeaSGleb Smirnoff 
902eb8dcdeaSGleb Smirnoff 	return (EADDRNOTAVAIL);
903eb8dcdeaSGleb Smirnoff }
904eb8dcdeaSGleb Smirnoff 
905eb8dcdeaSGleb Smirnoff /*
906eb8dcdeaSGleb Smirnoff  * Grab primary IP.  Historically required mutex, but nothing prevents
907eb8dcdeaSGleb Smirnoff  * us to support epoch-protected access.  Is it used in fast path?
908eb8dcdeaSGleb Smirnoff  * in{6}_jail.c helper
909eb8dcdeaSGleb Smirnoff  */
910eb8dcdeaSGleb Smirnoff const void *
911eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af)
912eb8dcdeaSGleb Smirnoff {
913eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip = pr->pr_addrs[af];
914eb8dcdeaSGleb Smirnoff 
915eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
916eb8dcdeaSGleb Smirnoff 	MPASS(pip);
917eb8dcdeaSGleb Smirnoff 
918eb8dcdeaSGleb Smirnoff 	return (pip + 1);
919eb8dcdeaSGleb Smirnoff }
920eb8dcdeaSGleb Smirnoff 
921eb8dcdeaSGleb Smirnoff u_int
922eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af)
923eb8dcdeaSGleb Smirnoff {
924eb8dcdeaSGleb Smirnoff 
925eb8dcdeaSGleb Smirnoff 	return (pr->pr_addrs[af]->ips);
926eb8dcdeaSGleb Smirnoff }
927eb8dcdeaSGleb Smirnoff #endif	/* defined(INET) || defined(INET6) */
928eb8dcdeaSGleb Smirnoff 
929413628a7SBjoern A. Zeeb int
930b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
931413628a7SBjoern A. Zeeb {
932fd7a8150SMike Barcroft 	struct nameidata nd;
933b38ff370SJamie Gritton #ifdef INET
934eb8dcdeaSGleb Smirnoff 	struct prison_ip *ip4;
935413628a7SBjoern A. Zeeb #endif
936b38ff370SJamie Gritton #ifdef INET6
937eb8dcdeaSGleb Smirnoff 	struct prison_ip *ip6;
938b38ff370SJamie Gritton #endif
939b38ff370SJamie Gritton 	struct vfsopt *opt;
940b38ff370SJamie Gritton 	struct vfsoptlist *opts;
9417de883c8SJamie Gritton 	struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
942b38ff370SJamie Gritton 	struct vnode *root;
943babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
944b96bd95bSIan Lepore 	char *g_path, *osrelstr;
945672756aaSJamie Gritton 	struct bool_flags *bf;
946672756aaSJamie Gritton 	struct jailsys_flags *jsf;
9470304c731SJamie Gritton #if defined(INET) || defined(INET6)
948b38ff370SJamie Gritton 	void *op;
9490304c731SJamie Gritton #endif
95076ca6f88SJamie Gritton 	unsigned long hid;
951b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
9522a4b2251SJamie Gritton 	int born, created, cuflags, descend, drflags, enforce;
953cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
9540cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
955672756aaSJamie Gritton 	int jid, jsys, len, level;
956b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
957413628a7SBjoern A. Zeeb #ifdef INET
9582b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
959413628a7SBjoern A. Zeeb #endif
960b38ff370SJamie Gritton #ifdef INET6
9612b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
962b38ff370SJamie Gritton #endif
9636beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
964b3079544SJamie Gritton 	uint64_t pr_allow_diff;
9656beb3bb4SKirk McKusick 	unsigned tallow;
966b38ff370SJamie Gritton 	char numbuf[12];
967b38ff370SJamie Gritton 
968b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
969b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
970b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
971b38ff370SJamie Gritton 	if (error)
97275c13541SPoul-Henning Kamp 		return (error);
973b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
974b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
9750304c731SJamie Gritton 		return (EPERM);
976b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
977b38ff370SJamie Gritton 		return (EINVAL);
978b38ff370SJamie Gritton 
979b38ff370SJamie Gritton 	/*
980b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
981b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
982b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
983b38ff370SJamie Gritton 	 * the allprison lock.
984b38ff370SJamie Gritton 	 *
985b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
986b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
987b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
988b38ff370SJamie Gritton 	 */
989b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
990b38ff370SJamie Gritton 	if (error)
991b38ff370SJamie Gritton 		return (error);
992b38ff370SJamie Gritton #ifdef INET
993b38ff370SJamie Gritton 	ip4 = NULL;
994b38ff370SJamie Gritton #endif
995b38ff370SJamie Gritton #ifdef INET6
996b38ff370SJamie Gritton 	ip6 = NULL;
997b38ff370SJamie Gritton #endif
9986dfe0a3dSMartin Matuska 	g_path = NULL;
999b38ff370SJamie Gritton 
1000b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1001b6f47c23SJamie Gritton 	if (!cuflags) {
1002b6f47c23SJamie Gritton 		error = EINVAL;
1003b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
1004b6f47c23SJamie Gritton 		goto done_errmsg;
1005b6f47c23SJamie Gritton 	}
1006b6f47c23SJamie Gritton 
1007b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1008b38ff370SJamie Gritton 	if (error == ENOENT)
1009b38ff370SJamie Gritton 		jid = 0;
1010b38ff370SJamie Gritton 	else if (error != 0)
1011b38ff370SJamie Gritton 		goto done_free;
1012b38ff370SJamie Gritton 
1013b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1014b38ff370SJamie Gritton 	if (error == ENOENT)
1015b38ff370SJamie Gritton 		gotslevel = 0;
1016b38ff370SJamie Gritton 	else if (error != 0)
1017b38ff370SJamie Gritton 		goto done_free;
1018b38ff370SJamie Gritton 	else
1019b38ff370SJamie Gritton 		gotslevel = 1;
1020b38ff370SJamie Gritton 
1021b97457e2SJamie Gritton 	error =
1022b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1023b97457e2SJamie Gritton 	if (error == ENOENT)
1024b97457e2SJamie Gritton 		gotchildmax = 0;
1025b97457e2SJamie Gritton 	else if (error != 0)
1026b97457e2SJamie Gritton 		goto done_free;
1027b97457e2SJamie Gritton 	else
1028b97457e2SJamie Gritton 		gotchildmax = 1;
1029b97457e2SJamie Gritton 
10300304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1031f337198dSJamie Gritton 	if (error == ENOENT)
1032f337198dSJamie Gritton 		gotenforce = 0;
1033f337198dSJamie Gritton 	else if (error != 0)
10340304c731SJamie Gritton 		goto done_free;
1035f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
1036f337198dSJamie Gritton 		error = EINVAL;
1037f337198dSJamie Gritton 		goto done_free;
1038f337198dSJamie Gritton 	} else
1039f337198dSJamie Gritton 		gotenforce = 1;
10400304c731SJamie Gritton 
10410cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
10420cc207a6SMartin Matuska 	if (error == ENOENT)
10430cc207a6SMartin Matuska 		gotrsnum = 0;
10440cc207a6SMartin Matuska 	else if (error != 0)
10450cc207a6SMartin Matuska 		goto done_free;
10460cc207a6SMartin Matuska 	else
10470cc207a6SMartin Matuska 		gotrsnum = 1;
10480cc207a6SMartin Matuska 
1049b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
1050672756aaSJamie Gritton 	for (bf = pr_flag_bool;
1051672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
1052672756aaSJamie Gritton 	     bf++) {
1053672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1054672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
10550304c731SJamie Gritton 	}
1056b38ff370SJamie Gritton 	ch_flags |= pr_flags;
1057672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
1058672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1059672756aaSJamie Gritton 	     jsf++) {
1060672756aaSJamie Gritton 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
10617cbf7213SJamie Gritton 		if (error == ENOENT)
10627cbf7213SJamie Gritton 			continue;
10637cbf7213SJamie Gritton 		if (error != 0)
10647cbf7213SJamie Gritton 			goto done_free;
10657cbf7213SJamie Gritton 		switch (jsys) {
10667cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
1067672756aaSJamie Gritton 			if (!jsf->disable) {
10687cbf7213SJamie Gritton 				error = EINVAL;
10697cbf7213SJamie Gritton 				goto done_free;
10707cbf7213SJamie Gritton 			}
1071672756aaSJamie Gritton 			pr_flags |= jsf->disable;
10727cbf7213SJamie Gritton 			break;
10737cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
1074672756aaSJamie Gritton 			pr_flags |= jsf->new;
10757cbf7213SJamie Gritton 			break;
10767cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
10777cbf7213SJamie Gritton 			break;
10787cbf7213SJamie Gritton 		default:
10797cbf7213SJamie Gritton 			error = EINVAL;
10807cbf7213SJamie Gritton 			goto done_free;
10817cbf7213SJamie Gritton 		}
1082672756aaSJamie Gritton 		ch_flags |= jsf->new | jsf->disable;
10837cbf7213SJamie Gritton 	}
10841158508aSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
10854affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
10864affa14cSJamie Gritton 		error = EINVAL;
10874affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
10884affa14cSJamie Gritton 		goto done_errmsg;
10894affa14cSJamie Gritton 	}
1090679e1390SJamie Gritton #ifdef VIMAGE
1091679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1092679e1390SJamie Gritton 		error = EINVAL;
1093679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
1094679e1390SJamie Gritton 		goto done_errmsg;
1095679e1390SJamie Gritton 	}
1096679e1390SJamie Gritton #endif
10972b0d6f81SJamie Gritton #ifdef INET
10982b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
10992b0d6f81SJamie Gritton 		error = EINVAL;
11002b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
11012b0d6f81SJamie Gritton 		goto done_errmsg;
11022b0d6f81SJamie Gritton 	}
11032b0d6f81SJamie Gritton #endif
11042b0d6f81SJamie Gritton #ifdef INET6
11052b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
11062b0d6f81SJamie Gritton 		error = EINVAL;
11072b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
11082b0d6f81SJamie Gritton 		goto done_errmsg;
11092b0d6f81SJamie Gritton 	}
11102b0d6f81SJamie Gritton #endif
1111b38ff370SJamie Gritton 
11120304c731SJamie Gritton 	pr_allow = ch_allow = 0;
1113672756aaSJamie Gritton 	for (bf = pr_flag_allow;
11145d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
11155d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
1116672756aaSJamie Gritton 	     bf++) {
1117672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1118672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
11190304c731SJamie Gritton 	}
11200304c731SJamie Gritton 	ch_allow |= pr_allow;
11210304c731SJamie Gritton 
1122b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1123b38ff370SJamie Gritton 	if (error == ENOENT)
1124b38ff370SJamie Gritton 		name = NULL;
1125b38ff370SJamie Gritton 	else if (error != 0)
1126b38ff370SJamie Gritton 		goto done_free;
1127b38ff370SJamie Gritton 	else {
1128b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1129b38ff370SJamie Gritton 			error = EINVAL;
1130b38ff370SJamie Gritton 			goto done_free;
1131b38ff370SJamie Gritton 		}
1132b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
1133b38ff370SJamie Gritton 			error = ENAMETOOLONG;
1134b38ff370SJamie Gritton 			goto done_free;
1135b38ff370SJamie Gritton 		}
1136b38ff370SJamie Gritton 	}
1137b38ff370SJamie Gritton 
1138b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1139b38ff370SJamie Gritton 	if (error == ENOENT)
1140b38ff370SJamie Gritton 		host = NULL;
1141b38ff370SJamie Gritton 	else if (error != 0)
1142b38ff370SJamie Gritton 		goto done_free;
1143b38ff370SJamie Gritton 	else {
114476ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
114576ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
1146b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
1147b38ff370SJamie Gritton 			error = EINVAL;
1148b38ff370SJamie Gritton 			goto done_free;
1149b38ff370SJamie Gritton 		}
1150b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
1151b38ff370SJamie Gritton 			error = ENAMETOOLONG;
1152b38ff370SJamie Gritton 			goto done_free;
1153b38ff370SJamie Gritton 		}
1154b38ff370SJamie Gritton 	}
1155b38ff370SJamie Gritton 
115676ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
115776ca6f88SJamie Gritton 	if (error == ENOENT)
115876ca6f88SJamie Gritton 		domain = NULL;
115976ca6f88SJamie Gritton 	else if (error != 0)
116076ca6f88SJamie Gritton 		goto done_free;
116176ca6f88SJamie Gritton 	else {
116276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
116376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
116476ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
116576ca6f88SJamie Gritton 			error = EINVAL;
116676ca6f88SJamie Gritton 			goto done_free;
116776ca6f88SJamie Gritton 		}
116876ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
116976ca6f88SJamie Gritton 			error = ENAMETOOLONG;
117076ca6f88SJamie Gritton 			goto done_free;
117176ca6f88SJamie Gritton 		}
117276ca6f88SJamie Gritton 	}
117376ca6f88SJamie Gritton 
117476ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
117576ca6f88SJamie Gritton 	if (error == ENOENT)
117676ca6f88SJamie Gritton 		uuid = NULL;
117776ca6f88SJamie Gritton 	else if (error != 0)
117876ca6f88SJamie Gritton 		goto done_free;
117976ca6f88SJamie Gritton 	else {
118076ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
118176ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
118276ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
118376ca6f88SJamie Gritton 			error = EINVAL;
118476ca6f88SJamie Gritton 			goto done_free;
118576ca6f88SJamie Gritton 		}
118676ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
118776ca6f88SJamie Gritton 			error = ENAMETOOLONG;
118876ca6f88SJamie Gritton 			goto done_free;
118976ca6f88SJamie Gritton 		}
119076ca6f88SJamie Gritton 	}
119176ca6f88SJamie Gritton 
1192841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
1193a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
119476ca6f88SJamie Gritton 		uint32_t hid32;
119576ca6f88SJamie Gritton 
119676ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
119776ca6f88SJamie Gritton 		hid = hid32;
119876ca6f88SJamie Gritton 	} else
119976ca6f88SJamie Gritton #endif
120076ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
120176ca6f88SJamie Gritton 	if (error == ENOENT)
120276ca6f88SJamie Gritton 		gothid = 0;
120376ca6f88SJamie Gritton 	else if (error != 0)
120476ca6f88SJamie Gritton 		goto done_free;
120576ca6f88SJamie Gritton 	else {
120676ca6f88SJamie Gritton 		gothid = 1;
120776ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
120876ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
120976ca6f88SJamie Gritton 	}
121076ca6f88SJamie Gritton 
1211b38ff370SJamie Gritton #ifdef INET
1212b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1213b38ff370SJamie Gritton 	if (error == ENOENT)
12140e5e396eSJamie Gritton 		ip4s = 0;
1215b38ff370SJamie Gritton 	else if (error != 0)
1216b38ff370SJamie Gritton 		goto done_free;
1217eb8dcdeaSGleb Smirnoff 	else if (ip4s & (sizeof(struct in_addr) - 1)) {
1218b38ff370SJamie Gritton 		error = EINVAL;
1219b38ff370SJamie Gritton 		goto done_free;
12200304c731SJamie Gritton 	} else {
12216a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
12226a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
12236a3f2779SJamie Gritton 		if (ip4s > 0) {
1224eb8dcdeaSGleb Smirnoff 			ip4s /= sizeof(struct in_addr);
1225b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
1226b38ff370SJamie Gritton 				error = EINVAL;
1227b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
1228b38ff370SJamie Gritton 				goto done_errmsg;
1229b38ff370SJamie Gritton 			}
1230eb8dcdeaSGleb Smirnoff 			ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1231eb8dcdeaSGleb Smirnoff 			if (ip4 == NULL) {
1232b38ff370SJamie Gritton 				error = EINVAL;
1233b38ff370SJamie Gritton 				goto done_free;
1234b38ff370SJamie Gritton 			}
1235b38ff370SJamie Gritton 		}
12360304c731SJamie Gritton 	}
1237b38ff370SJamie Gritton #endif
1238b38ff370SJamie Gritton 
1239b38ff370SJamie Gritton #ifdef INET6
1240b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1241b38ff370SJamie Gritton 	if (error == ENOENT)
12420e5e396eSJamie Gritton 		ip6s = 0;
1243b38ff370SJamie Gritton 	else if (error != 0)
1244b38ff370SJamie Gritton 		goto done_free;
1245eb8dcdeaSGleb Smirnoff 	else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1246b38ff370SJamie Gritton 		error = EINVAL;
1247b38ff370SJamie Gritton 		goto done_free;
12480304c731SJamie Gritton 	} else {
12496a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
12506a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
12516a3f2779SJamie Gritton 		if (ip6s > 0) {
1252eb8dcdeaSGleb Smirnoff 			ip6s /= sizeof(struct in6_addr);
1253b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
1254b38ff370SJamie Gritton 				error = EINVAL;
1255b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
1256b38ff370SJamie Gritton 				goto done_errmsg;
1257b38ff370SJamie Gritton 			}
1258eb8dcdeaSGleb Smirnoff 			ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1259eb8dcdeaSGleb Smirnoff 			if (ip6 == NULL) {
1260b38ff370SJamie Gritton 				error = EINVAL;
1261b38ff370SJamie Gritton 				goto done_free;
1262b38ff370SJamie Gritton 			}
1263b38ff370SJamie Gritton 		}
12640304c731SJamie Gritton 	}
1265b38ff370SJamie Gritton #endif
1266b38ff370SJamie Gritton 
1267bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1268bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1269bdfc8cc4SJamie Gritton 		error = EINVAL;
1270bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
1271bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
1272bdfc8cc4SJamie Gritton 		goto done_errmsg;
1273bdfc8cc4SJamie Gritton 	}
1274bdfc8cc4SJamie Gritton #endif
1275bdfc8cc4SJamie Gritton 
1276cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1277cf0313c6SJamie Gritton 	if (error == ENOENT)
1278cf0313c6SJamie Gritton 		osrelstr = NULL;
1279cf0313c6SJamie Gritton 	else if (error != 0)
1280cf0313c6SJamie Gritton 		goto done_free;
1281cf0313c6SJamie Gritton 	else {
1282cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
1283cf0313c6SJamie Gritton 			error = EINVAL;
1284cf0313c6SJamie Gritton 			vfs_opterror(opts,
1285cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
1286cf0313c6SJamie Gritton 			goto done_errmsg;
1287cf0313c6SJamie Gritton 		}
12881b786d01SBjoern A. Zeeb 		if (len == 0 || osrelstr[len - 1] != '\0') {
1289cf0313c6SJamie Gritton 			error = EINVAL;
12901b786d01SBjoern A. Zeeb 			goto done_free;
12911b786d01SBjoern A. Zeeb 		}
12921b786d01SBjoern A. Zeeb 		if (len >= OSRELEASELEN) {
12931b786d01SBjoern A. Zeeb 			error = ENAMETOOLONG;
1294cf0313c6SJamie Gritton 			vfs_opterror(opts,
1295cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
1296cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
1297cf0313c6SJamie Gritton 			goto done_errmsg;
1298cf0313c6SJamie Gritton 		}
1299cf0313c6SJamie Gritton 	}
1300cf0313c6SJamie Gritton 
1301cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1302cf0313c6SJamie Gritton 	if (error == ENOENT)
1303cf0313c6SJamie Gritton 		osreldt = 0;
1304cf0313c6SJamie Gritton 	else if (error != 0)
1305cf0313c6SJamie Gritton 		goto done_free;
1306cf0313c6SJamie Gritton 	else {
1307cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
1308cf0313c6SJamie Gritton 			error = EINVAL;
1309cf0313c6SJamie Gritton 			vfs_opterror(opts,
1310cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
1311cf0313c6SJamie Gritton 			goto done_errmsg;
1312cf0313c6SJamie Gritton 		}
1313cf0313c6SJamie Gritton 		if (osreldt == 0) {
1314cf0313c6SJamie Gritton 			error = EINVAL;
1315cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
1316cf0313c6SJamie Gritton 			goto done_errmsg;
1317cf0313c6SJamie Gritton 		}
1318cf0313c6SJamie Gritton 	}
1319cf0313c6SJamie Gritton 
1320b38ff370SJamie Gritton 	root = NULL;
1321b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
1322b38ff370SJamie Gritton 	if (error == ENOENT)
1323b38ff370SJamie Gritton 		path = NULL;
1324b38ff370SJamie Gritton 	else if (error != 0)
1325b38ff370SJamie Gritton 		goto done_free;
1326b38ff370SJamie Gritton 	else {
1327b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
1328b38ff370SJamie Gritton 			error = EINVAL;
1329b38ff370SJamie Gritton 			vfs_opterror(opts,
1330b38ff370SJamie Gritton 			    "path cannot be changed after creation");
1331b38ff370SJamie Gritton 			goto done_errmsg;
1332b38ff370SJamie Gritton 		}
1333b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
1334b38ff370SJamie Gritton 			error = EINVAL;
1335b38ff370SJamie Gritton 			goto done_free;
1336b38ff370SJamie Gritton 		}
13377e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1338b38ff370SJamie Gritton 		error = namei(&nd);
1339b38ff370SJamie Gritton 		if (error)
1340b38ff370SJamie Gritton 			goto done_free;
1341b38ff370SJamie Gritton 		root = nd.ni_vp;
1342bb92cd7bSMateusz Guzik 		NDFREE_PNBUF(&nd);
13436dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
13446dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
13456dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
13463eb6b656SMateusz Guzik 		if (error == 0) {
13476dfe0a3dSMartin Matuska 			path = g_path;
13486dfe0a3dSMartin Matuska 		} else {
1349f6e633a9SMartin Matuska 			/* exit on other errors */
1350b38ff370SJamie Gritton 			goto done_free;
1351b38ff370SJamie Gritton 		}
1352f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
1353f6e633a9SMartin Matuska 			error = ENOTDIR;
1354f6e633a9SMartin Matuska 			vput(root);
1355f6e633a9SMartin Matuska 			goto done_free;
1356f6e633a9SMartin Matuska 		}
1357b249ce48SMateusz Guzik 		VOP_UNLOCK(root);
1358b38ff370SJamie Gritton 	}
1359b38ff370SJamie Gritton 
1360b38ff370SJamie Gritton 	/*
1361b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
1362b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
1363b38ff370SJamie Gritton 	 */
1364b38ff370SJamie Gritton 	pr = NULL;
13657de883c8SJamie Gritton 	inspr = NULL;
1366babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1367babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1368babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1369babbbb9cSJamie Gritton 		if (*p != '\0')
1370babbbb9cSJamie Gritton 			jid = 0;
1371babbbb9cSJamie Gritton 	}
1372b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
1373108a9384SJamie Gritton 	drflags = PD_LIST_XLOCKED;
13740a2a96f3SJamie Gritton 	ppr = mypr;
13750a2a96f3SJamie Gritton 	if (!prison_isalive(ppr)) {
13760a2a96f3SJamie Gritton 		/* This jail is dying.  This process will surely follow. */
13770a2a96f3SJamie Gritton 		error = EAGAIN;
13780a2a96f3SJamie Gritton 		goto done_deref;
13790a2a96f3SJamie Gritton 	}
1380b38ff370SJamie Gritton 	if (jid != 0) {
1381b38ff370SJamie Gritton 		if (jid < 0) {
1382b38ff370SJamie Gritton 			error = EINVAL;
1383b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
13842a4b2251SJamie Gritton 			goto done_deref;
1385b38ff370SJamie Gritton 		}
13867de883c8SJamie Gritton 		/*
13877de883c8SJamie Gritton 		 * See if a requested jid already exists.  Keep track of
13887de883c8SJamie Gritton 		 * where it can be inserted later.
13897de883c8SJamie Gritton 		 */
13907de883c8SJamie Gritton 		TAILQ_FOREACH(inspr, &allprison, pr_list) {
1391f7496dcaSJamie Gritton 			if (inspr->pr_id < jid)
1392f7496dcaSJamie Gritton 				continue;
1393f7496dcaSJamie Gritton 			if (inspr->pr_id > jid)
1394f7496dcaSJamie Gritton 				break;
13957de883c8SJamie Gritton 			pr = inspr;
1396f7496dcaSJamie Gritton 			mtx_lock(&pr->pr_mtx);
13972a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
13987de883c8SJamie Gritton 			inspr = NULL;
13997de883c8SJamie Gritton 			break;
14007de883c8SJamie Gritton 		}
1401b38ff370SJamie Gritton 		if (pr != NULL) {
1402b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1403b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
14047de883c8SJamie Gritton 				/*
14057de883c8SJamie Gritton 				 * Even creators that cannot see the jail will
14067de883c8SJamie Gritton 				 * get EEXIST.
14077de883c8SJamie Gritton 				 */
1408b38ff370SJamie Gritton 				error = EEXIST;
1409b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1410b38ff370SJamie Gritton 				    jid);
14112a4b2251SJamie Gritton 				goto done_deref;
1412b38ff370SJamie Gritton 			}
14130304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
14147de883c8SJamie Gritton 				/*
14157de883c8SJamie Gritton 				 * Updaters get ENOENT if they cannot see the
14167de883c8SJamie Gritton 				 * jail.  This is true even for CREATE | UPDATE,
14177de883c8SJamie Gritton 				 * which normally cannot give this error.
14187de883c8SJamie Gritton 				 */
14192a4b2251SJamie Gritton 				error = ENOENT;
14202a4b2251SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
14212a4b2251SJamie Gritton 				goto done_deref;
1422f7496dcaSJamie Gritton 			}
14230a2a96f3SJamie Gritton 			ppr = pr->pr_parent;
14240a2a96f3SJamie Gritton 			if (!prison_isalive(ppr)) {
14250a2a96f3SJamie Gritton 				error = ENOENT;
14260a2a96f3SJamie Gritton 				vfs_opterror(opts, "jail %d is dying",
14270a2a96f3SJamie Gritton 				    ppr->pr_id);
14280a2a96f3SJamie Gritton 				goto done_deref;
14290a2a96f3SJamie Gritton 			}
1430f7496dcaSJamie Gritton 			if (!prison_isalive(pr)) {
1431b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1432b38ff370SJamie Gritton 					error = ENOENT;
1433b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1434b38ff370SJamie Gritton 					    jid);
14352a4b2251SJamie Gritton 					goto done_deref;
1436f7496dcaSJamie Gritton 				}
1437f7496dcaSJamie Gritton 				if ((flags & JAIL_ATTACH) ||
1438b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1439b38ff370SJamie Gritton 					/*
1440b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1441b38ff370SJamie Gritton 					 * (via attach or persist), but first
1442b38ff370SJamie Gritton 					 * it must determine if another jail
1443b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1444b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1445b38ff370SJamie Gritton 					 * name.
1446b38ff370SJamie Gritton 					 */
1447b38ff370SJamie Gritton 					if (name == NULL)
14480304c731SJamie Gritton 						name = prison_name(mypr, pr);
1449b38ff370SJamie Gritton 				}
1450b38ff370SJamie Gritton 			}
14512a4b2251SJamie Gritton 		} else {
1452b38ff370SJamie Gritton 			/* Update: jid must exist. */
1453b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1454b38ff370SJamie Gritton 				error = ENOENT;
1455b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
14562a4b2251SJamie Gritton 				goto done_deref;
1457b38ff370SJamie Gritton 			}
1458b38ff370SJamie Gritton 		}
1459b38ff370SJamie Gritton 	}
1460b38ff370SJamie Gritton 	/*
1461b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1462b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1463b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1464b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1465b38ff370SJamie Gritton 	 * because that is the jail being updated).
1466b38ff370SJamie Gritton 	 */
1467b6f47c23SJamie Gritton 	namelc = NULL;
1468b38ff370SJamie Gritton 	if (name != NULL) {
1469babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1470babbbb9cSJamie Gritton 		if (namelc == NULL)
1471babbbb9cSJamie Gritton 			namelc = name;
1472babbbb9cSJamie Gritton 		else {
14730304c731SJamie Gritton 			/*
14740304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
14750304c731SJamie Gritton 			 * parent and child names, and make sure the parent
14760304c731SJamie Gritton 			 * exists or matches an already found jail.
14770304c731SJamie Gritton 			 */
14780304c731SJamie Gritton 			if (pr != NULL) {
1479babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1480babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
14810304c731SJamie Gritton 					error = EINVAL;
14820304c731SJamie Gritton 					vfs_opterror(opts,
14830304c731SJamie Gritton 					    "cannot change jail's parent");
14842a4b2251SJamie Gritton 					goto done_deref;
14850304c731SJamie Gritton 				}
14860304c731SJamie Gritton 			} else {
1487b6f47c23SJamie Gritton 				*namelc = '\0';
14880304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
14890304c731SJamie Gritton 				if (ppr == NULL) {
14900304c731SJamie Gritton 					error = ENOENT;
14910304c731SJamie Gritton 					vfs_opterror(opts,
14920304c731SJamie Gritton 					    "jail \"%s\" not found", name);
14932a4b2251SJamie Gritton 					goto done_deref;
14940304c731SJamie Gritton 				}
1495c050ea80SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
14960a2a96f3SJamie Gritton 				if (!prison_isalive(ppr)) {
1497c050ea80SJamie Gritton 					error = ENOENT;
1498c050ea80SJamie Gritton 					vfs_opterror(opts,
1499c050ea80SJamie Gritton 					    "jail \"%s\" is dying", name);
1500c050ea80SJamie Gritton 					goto done_deref;
1501c050ea80SJamie Gritton 				}
1502b6f47c23SJamie Gritton 				*namelc = '.';
15030304c731SJamie Gritton 			}
1504b6f47c23SJamie Gritton 			namelc++;
15050304c731SJamie Gritton 		}
1506b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1507b6f47c23SJamie Gritton 			pnamelen =
15080304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
15090304c731SJamie Gritton 			deadpr = NULL;
15100304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
15116754ae25SJamie Gritton 				if (tpr != pr &&
1512b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
151376ad42abSJamie Gritton 					if (prison_isalive(tpr)) {
1514b38ff370SJamie Gritton 						if (pr == NULL &&
1515b38ff370SJamie Gritton 						    cuflags != JAIL_CREATE) {
1516b38ff370SJamie Gritton 							/*
1517b38ff370SJamie Gritton 							 * Use this jail
1518b38ff370SJamie Gritton 							 * for updates.
1519b38ff370SJamie Gritton 							 */
1520b38ff370SJamie Gritton 							pr = tpr;
1521f7496dcaSJamie Gritton 							mtx_lock(&pr->pr_mtx);
152283bc72a0SJamie Gritton 							drflags |= PD_LOCKED;
1523b38ff370SJamie Gritton 							break;
1524b38ff370SJamie Gritton 						}
1525b38ff370SJamie Gritton 						/*
1526b38ff370SJamie Gritton 						 * Create, or update(jid):
1527b38ff370SJamie Gritton 						 * name must not exist in an
15280304c731SJamie Gritton 						 * active sibling jail.
1529b38ff370SJamie Gritton 						 */
1530b38ff370SJamie Gritton 						error = EEXIST;
1531b38ff370SJamie Gritton 						vfs_opterror(opts,
1532b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1533b38ff370SJamie Gritton 						   name);
15342a4b2251SJamie Gritton 						goto done_deref;
1535b38ff370SJamie Gritton 					}
153676ad42abSJamie Gritton 					if (pr == NULL &&
1537f7496dcaSJamie Gritton 					    cuflags != JAIL_CREATE) {
153876ad42abSJamie Gritton 						deadpr = tpr;
1539f7496dcaSJamie Gritton 					}
1540b38ff370SJamie Gritton 				}
1541b38ff370SJamie Gritton 			}
1542b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1543b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1544b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1545b38ff370SJamie Gritton 					pr = deadpr;
1546f7496dcaSJamie Gritton 					mtx_lock(&pr->pr_mtx);
15472a4b2251SJamie Gritton 					drflags |= PD_LOCKED;
1548b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1549b38ff370SJamie Gritton 					error = ENOENT;
1550b38ff370SJamie Gritton 					vfs_opterror(opts,
1551b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
15522a4b2251SJamie Gritton 					goto done_deref;
1553b38ff370SJamie Gritton 				}
1554b38ff370SJamie Gritton 			}
1555b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1556b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1557b38ff370SJamie Gritton 				error = ENOENT;
1558b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1559b38ff370SJamie Gritton 				    name);
15602a4b2251SJamie Gritton 				goto done_deref;
1561b38ff370SJamie Gritton 			}
1562b38ff370SJamie Gritton 		}
1563b38ff370SJamie Gritton 	}
1564b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1565b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1566b38ff370SJamie Gritton 		error = ENOENT;
1567b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
15682a4b2251SJamie Gritton 		goto done_deref;
1569b38ff370SJamie Gritton 	}
1570b38ff370SJamie Gritton 
1571b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
15722a4b2251SJamie Gritton 	created = pr == NULL;
15732a4b2251SJamie Gritton 	if (created) {
1574b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1575b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1576b97457e2SJamie Gritton 				error = EPERM;
1577b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
15782a4b2251SJamie Gritton 				goto done_deref;
1579b97457e2SJamie Gritton 			}
15807de883c8SJamie Gritton 		if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1581b38ff370SJamie Gritton 			error = EAGAIN;
15827de883c8SJamie Gritton 			vfs_opterror(opts, "no available jail IDs");
15832a4b2251SJamie Gritton 			goto done_deref;
1584b38ff370SJamie Gritton 		}
15852a4b2251SJamie Gritton 
15862a4b2251SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
15871158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_INVALID;
15881158508aSJamie Gritton 		refcount_init(&pr->pr_ref, 1);
1589f7496dcaSJamie Gritton 		refcount_init(&pr->pr_uref, 0);
15901158508aSJamie Gritton 		drflags |= PD_DEREF;
15912a4b2251SJamie Gritton 		LIST_INIT(&pr->pr_children);
15922a4b2251SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
15932a4b2251SJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
15942a4b2251SJamie Gritton 
15957de883c8SJamie Gritton 		pr->pr_id = jid;
15967de883c8SJamie Gritton 		if (inspr != NULL)
15977de883c8SJamie Gritton 			TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
15987de883c8SJamie Gritton 		else
1599b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
16007de883c8SJamie Gritton 
16017de883c8SJamie Gritton 		pr->pr_parent = ppr;
16020a2a96f3SJamie Gritton 		prison_hold(ppr);
16030a2a96f3SJamie Gritton 		prison_proc_hold(ppr);
16040304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
16050304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1606b97457e2SJamie Gritton 			tpr->pr_childcount++;
1607b38ff370SJamie Gritton 
16080304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1609b6f47c23SJamie Gritton 		if (namelc == NULL)
1610b6f47c23SJamie Gritton 			namelc = "";
1611b38ff370SJamie Gritton 		if (path == NULL) {
1612b38ff370SJamie Gritton 			path = "/";
16130304c731SJamie Gritton 			root = mypr->pr_root;
1614b38ff370SJamie Gritton 			vref(root);
1615b38ff370SJamie Gritton 		}
16168986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
16178986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1618bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1619bdfc8cc4SJamie Gritton #ifdef VIMAGE
1620bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1621bdfc8cc4SJamie Gritton #endif
1622bdfc8cc4SJamie Gritton 		{
16230304c731SJamie Gritton #ifdef INET
16242b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
16256a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
16262b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
16272b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1628eb8dcdeaSGleb Smirnoff 				prison_ip_dup(ppr, pr, PR_INET);
16292b0d6f81SJamie Gritton 			}
16300304c731SJamie Gritton #endif
16310304c731SJamie Gritton #ifdef INET6
16322b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
16336a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
16342b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
16352b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1636eb8dcdeaSGleb Smirnoff 				prison_ip_dup(ppr, pr, PR_INET6);
16372b0d6f81SJamie Gritton 			}
16380304c731SJamie Gritton #endif
1639bdfc8cc4SJamie Gritton 		}
1640bdfc8cc4SJamie Gritton #endif
1641592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1642592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1643592bcae8SBjoern A. Zeeb 
16440304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
16450304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1646af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1647bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1648b38ff370SJamie Gritton 
1649b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1650b96bd95bSIan Lepore 		if (osrelstr == NULL)
16511b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
16521b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1653b96bd95bSIan Lepore 		else
16541b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, osrelstr,
16551b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1656b96bd95bSIan Lepore 
1657679e1390SJamie Gritton #ifdef VIMAGE
1658679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1659679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1660679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1661679e1390SJamie Gritton #endif
1662b38ff370SJamie Gritton 		/*
1663b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
16648771ff75SGordon Bergling 		 * Unlike other initial settings, this may return an error.
1665b38ff370SJamie Gritton 		 */
16660304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
16672a4b2251SJamie Gritton 		if (error)
16682a4b2251SJamie Gritton 			goto done_deref;
1669b38ff370SJamie Gritton 
1670b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16712a4b2251SJamie Gritton 		drflags |= PD_LOCKED;
1672b38ff370SJamie Gritton 	} else {
16732b0d6f81SJamie Gritton 		/*
16742b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
16752b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
16762b0d6f81SJamie Gritton 		 */
16776754ae25SJamie Gritton 		prison_hold(pr);
16782a4b2251SJamie Gritton 		drflags |= PD_DEREF;
1679bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1680bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1681bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1682bdfc8cc4SJamie Gritton 			error = EINVAL;
1683bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1684bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
16852a4b2251SJamie Gritton 			goto done_deref;
1686bdfc8cc4SJamie Gritton 		}
1687bdfc8cc4SJamie Gritton #endif
16882b0d6f81SJamie Gritton #ifdef INET
16892b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
16902b0d6f81SJamie Gritton 			error = EINVAL;
16912b0d6f81SJamie Gritton 			vfs_opterror(opts,
16922b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
16932a4b2251SJamie Gritton 			goto done_deref;
16942b0d6f81SJamie Gritton 		}
16952b0d6f81SJamie Gritton #endif
16962b0d6f81SJamie Gritton #ifdef INET6
16972b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
16982b0d6f81SJamie Gritton 			error = EINVAL;
16992b0d6f81SJamie Gritton 			vfs_opterror(opts,
17002b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
17012a4b2251SJamie Gritton 			goto done_deref;
17022b0d6f81SJamie Gritton 		}
17032b0d6f81SJamie Gritton #endif
1704b38ff370SJamie Gritton 	}
1705b38ff370SJamie Gritton 
1706b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
17070304c731SJamie Gritton 	if (gotslevel) {
17080304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
17090304c731SJamie Gritton 			error = EPERM;
17102a4b2251SJamie Gritton 			goto done_deref;
17110304c731SJamie Gritton 		}
17120304c731SJamie Gritton 	}
1713b97457e2SJamie Gritton 	if (gotchildmax) {
1714b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1715b97457e2SJamie Gritton 			error = EPERM;
17162a4b2251SJamie Gritton 			goto done_deref;
1717b97457e2SJamie Gritton 		}
1718b97457e2SJamie Gritton 	}
17190304c731SJamie Gritton 	if (gotenforce) {
17200304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
17210304c731SJamie Gritton 			error = EPERM;
17222a4b2251SJamie Gritton 			goto done_deref;
17230304c731SJamie Gritton 		}
17240304c731SJamie Gritton 	}
17250cc207a6SMartin Matuska 	if (gotrsnum) {
17260cc207a6SMartin Matuska 		/*
17270cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
17280cc207a6SMartin Matuska 		 */
1729bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
17300cc207a6SMartin Matuska 			error = EINVAL;
17312a4b2251SJamie Gritton 			goto done_deref;
17320cc207a6SMartin Matuska 		}
17330cc207a6SMartin Matuska 		/*
1734bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
17350cc207a6SMartin Matuska 		 */
17360cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
17370cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
17380cc207a6SMartin Matuska 				error = EPERM;
17392a4b2251SJamie Gritton 				goto done_deref;
1740bf3db8aaSMartin Matuska 			} else
17410cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
17420cc207a6SMartin Matuska 		}
17430cc207a6SMartin Matuska 	}
1744b38ff370SJamie Gritton #ifdef INET
17452b0d6f81SJamie Gritton 	if (ip4s > 0) {
1746eb8dcdeaSGleb Smirnoff 		if ((ppr->pr_flags & PR_IP4) &&
1747eb8dcdeaSGleb Smirnoff 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1748eb8dcdeaSGleb Smirnoff 		    PR_INET)) {
17490304c731SJamie Gritton 			error = EPERM;
17502a4b2251SJamie Gritton 			goto done_deref;
17510304c731SJamie Gritton 		}
1752eb8dcdeaSGleb Smirnoff 		if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
17530304c731SJamie Gritton 			error = EADDRINUSE;
1754eb8dcdeaSGleb Smirnoff 			vfs_opterror(opts, "IPv4 addresses clash");
17552a4b2251SJamie Gritton 			goto done_deref;
1756b38ff370SJamie Gritton 		}
17570304c731SJamie Gritton 	}
1758b38ff370SJamie Gritton #endif
1759b38ff370SJamie Gritton #ifdef INET6
17602b0d6f81SJamie Gritton 	if (ip6s > 0) {
1761eb8dcdeaSGleb Smirnoff 		if ((ppr->pr_flags & PR_IP6) &&
1762eb8dcdeaSGleb Smirnoff 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1763eb8dcdeaSGleb Smirnoff 		    PR_INET6)) {
17640304c731SJamie Gritton 			error = EPERM;
17652a4b2251SJamie Gritton 			goto done_deref;
17660304c731SJamie Gritton 		}
1767eb8dcdeaSGleb Smirnoff 		if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
17680304c731SJamie Gritton 			error = EADDRINUSE;
1769eb8dcdeaSGleb Smirnoff 			vfs_opterror(opts, "IPv6 addresses clash");
17702a4b2251SJamie Gritton 			goto done_deref;
1771b38ff370SJamie Gritton 		}
17720304c731SJamie Gritton 	}
1773b38ff370SJamie Gritton #endif
17740304c731SJamie Gritton 	onamelen = namelen = 0;
1775b6f47c23SJamie Gritton 	if (namelc != NULL) {
17766ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
17776ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
17786ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
17796ab6058eSJamie Gritton 		 */
1780b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1781b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
17826ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
17836ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1784b38ff370SJamie Gritton 			error = EINVAL;
1785babbbb9cSJamie Gritton 			vfs_opterror(opts,
1786babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
17872a4b2251SJamie Gritton 			goto done_deref;
1788b38ff370SJamie Gritton 		}
1789b38ff370SJamie Gritton 		/*
17900304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
17910304c731SJamie Gritton 		 * children.
1792b38ff370SJamie Gritton 		 */
1793b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1794b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1795b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1796b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
17970304c731SJamie Gritton 			error = ENAMETOOLONG;
17982a4b2251SJamie Gritton 			goto done_deref;
17990304c731SJamie Gritton 		}
18000304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
18010304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
18020304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
18030304c731SJamie Gritton 				error = ENAMETOOLONG;
18042a4b2251SJamie Gritton 				goto done_deref;
18050304c731SJamie Gritton 			}
18060304c731SJamie Gritton 		}
18070304c731SJamie Gritton 	}
1808b3079544SJamie Gritton 	pr_allow_diff = pr_allow & ~ppr->pr_allow;
1809b3079544SJamie Gritton 	if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
18100304c731SJamie Gritton 		error = EPERM;
18112a4b2251SJamie Gritton 		goto done_deref;
1812b38ff370SJamie Gritton 	}
1813b38ff370SJamie Gritton 
1814b6f47c23SJamie Gritton 	/*
1815b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1816b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1817b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1818b6f47c23SJamie Gritton 	 */
1819b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
18202a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1821b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
18222a4b2251SJamie Gritton 	if (error != 0)
18232a4b2251SJamie Gritton 		goto done_deref;
1824b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
18252a4b2251SJamie Gritton 	drflags |= PD_LOCKED;
1826b6f47c23SJamie Gritton 
1827b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1828b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1829b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1830b6f47c23SJamie Gritton 			error = EINVAL;
1831b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
18322a4b2251SJamie Gritton 			goto done_deref;
1833b6f47c23SJamie Gritton 		}
1834b6f47c23SJamie Gritton 	}
1835b6f47c23SJamie Gritton 
1836b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1837b38ff370SJamie Gritton #ifdef INET
18380304c731SJamie Gritton 	redo_ip4 = 0;
18390304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
18400304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1841eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, PR_INET, ip4);
1842b38ff370SJamie Gritton 		ip4 = NULL;
18430304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1844bdfc8cc4SJamie Gritton #ifdef VIMAGE
1845bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1846bdfc8cc4SJamie Gritton 				descend = 0;
1847bdfc8cc4SJamie Gritton 				continue;
1848bdfc8cc4SJamie Gritton 			}
1849bdfc8cc4SJamie Gritton #endif
1850eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET, NULL)) {
18510304c731SJamie Gritton 				redo_ip4 = 1;
18520304c731SJamie Gritton 				descend = 0;
18530304c731SJamie Gritton 			}
18540304c731SJamie Gritton 		}
18550304c731SJamie Gritton 	}
1856b38ff370SJamie Gritton #endif
1857b38ff370SJamie Gritton #ifdef INET6
18580304c731SJamie Gritton 	redo_ip6 = 0;
18590304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
18600304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1861eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, PR_INET6, ip6);
1862b38ff370SJamie Gritton 		ip6 = NULL;
18630304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1864bdfc8cc4SJamie Gritton #ifdef VIMAGE
1865bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1866bdfc8cc4SJamie Gritton 				descend = 0;
1867bdfc8cc4SJamie Gritton 				continue;
1868bdfc8cc4SJamie Gritton 			}
1869bdfc8cc4SJamie Gritton #endif
1870eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET6, NULL)) {
18710304c731SJamie Gritton 				redo_ip6 = 1;
18720304c731SJamie Gritton 				descend = 0;
18730304c731SJamie Gritton 			}
18740304c731SJamie Gritton 		}
18750304c731SJamie Gritton 	}
1876b38ff370SJamie Gritton #endif
18770304c731SJamie Gritton 	if (gotslevel) {
1878b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
18790304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
18800304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
18810304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
18820304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
18830304c731SJamie Gritton 	}
1884b97457e2SJamie Gritton 	if (gotchildmax) {
1885b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1886b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1887b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1888b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1889b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1890b97457e2SJamie Gritton 				    ? childmax - level : 0;
1891b97457e2SJamie Gritton 	}
18920304c731SJamie Gritton 	if (gotenforce) {
18930304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
18940304c731SJamie Gritton 		/* Pass this restriction on to the children. */
18950304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
18960304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
18970304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
18980304c731SJamie Gritton 	}
18990cc207a6SMartin Matuska 	if (gotrsnum) {
19000cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
19010cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
19020cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
19030cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
19040cc207a6SMartin Matuska 	}
1905b6f47c23SJamie Gritton 	if (namelc != NULL) {
19060304c731SJamie Gritton 		if (ppr == &prison0)
1907b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
19080304c731SJamie Gritton 		else
19090304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1910b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
19110304c731SJamie Gritton 		/* Change this component of child names. */
19120304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
19130304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
19140304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
19150304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
19160304c731SJamie Gritton 		}
19170304c731SJamie Gritton 	}
1918b38ff370SJamie Gritton 	if (path != NULL) {
19190304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1920b38ff370SJamie Gritton 		strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1921b38ff370SJamie Gritton 		pr->pr_root = root;
19222a4b2251SJamie Gritton 		root = NULL;
1923b38ff370SJamie Gritton 	}
192476ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
192576ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
192676ca6f88SJamie Gritton 			/*
192776ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
192876ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
192976ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
193076ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
193176ca6f88SJamie Gritton 			 */
1932c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1933c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1934c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1935c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1936c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1937c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
193876ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
193976ca6f88SJamie Gritton 		}
194076ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
194176ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1942b38ff370SJamie Gritton 		if (host != NULL)
1943c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
194476ca6f88SJamie Gritton 		if (domain != NULL)
1945c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1946c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
194776ca6f88SJamie Gritton 		if (uuid != NULL)
1948c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
194976ca6f88SJamie Gritton 		if (gothid)
195076ca6f88SJamie Gritton 			pr->pr_hostid = hid;
195176ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
195276ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
195376ca6f88SJamie Gritton 				descend = 0;
195476ca6f88SJamie Gritton 			else {
195576ca6f88SJamie Gritton 				if (host != NULL)
1956c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1957c1f19219SJamie Gritton 					    pr->pr_hostname,
1958c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
195976ca6f88SJamie Gritton 				if (domain != NULL)
1960c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1961c1f19219SJamie Gritton 					    pr->pr_domainname,
1962c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
196376ca6f88SJamie Gritton 				if (uuid != NULL)
1964c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1965c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1966c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
196776ca6f88SJamie Gritton 				if (gothid)
196876ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
196976ca6f88SJamie Gritton 			}
197076ca6f88SJamie Gritton 		}
197176ca6f88SJamie Gritton 	}
19720304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
19730fe74ae6SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow))
19740fe74ae6SJamie Gritton 		prison_set_allow_locked(pr, tallow, 0);
1975b38ff370SJamie Gritton 	/*
1976b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
19771158508aSJamie Gritton 	 * persist flag lose that reference.
1978b38ff370SJamie Gritton 	 */
197976ad42abSJamie Gritton 	born = !prison_isalive(pr);
19801158508aSJamie Gritton 	if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1981b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
19826754ae25SJamie Gritton 			prison_hold(pr);
19831158508aSJamie Gritton 			/*
19841158508aSJamie Gritton 			 * This may make a dead prison alive again, but wait
19851158508aSJamie Gritton 			 * to label it as such until after OSD calls have had
19861158508aSJamie Gritton 			 * a chance to run (and perhaps to fail).
19871158508aSJamie Gritton 			 */
19886754ae25SJamie Gritton 			refcount_acquire(&pr->pr_uref);
1989b38ff370SJamie Gritton 		} else {
199039c8ef90SJamie Gritton 			drflags |= PD_DEUREF;
1991f7496dcaSJamie Gritton 			prison_free_not_last(pr);
1992b38ff370SJamie Gritton 		}
1993b38ff370SJamie Gritton 	}
1994b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1995b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
19962a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1997c861373bSJamie Gritton 	/*
1998c861373bSJamie Gritton 	 * Any errors past this point will need to de-persist newly created
1999c861373bSJamie Gritton 	 * prisons, as well as call remove methods.
2000c861373bSJamie Gritton 	 */
2001c861373bSJamie Gritton 	if (born)
2002c861373bSJamie Gritton 		drflags |= PD_KILL;
2003b38ff370SJamie Gritton 
2004a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
20054b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
2006a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
2007a7ad07bfSEdward Tomasz Napierala #endif
2008a7ad07bfSEdward Tomasz Napierala 
20090304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
20100304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
20110304c731SJamie Gritton 	 */
20120304c731SJamie Gritton #ifdef INET
20130304c731SJamie Gritton 	while (redo_ip4) {
2014eb8dcdeaSGleb Smirnoff 		ip4s = pr->pr_addrs[PR_INET]->ips;
2015eb8dcdeaSGleb Smirnoff 		ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
20160304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20170304c731SJamie Gritton 		redo_ip4 = 0;
20180304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2019bdfc8cc4SJamie Gritton #ifdef VIMAGE
2020bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
2021bdfc8cc4SJamie Gritton 				descend = 0;
2022bdfc8cc4SJamie Gritton 				continue;
2023bdfc8cc4SJamie Gritton 			}
2024bdfc8cc4SJamie Gritton #endif
2025eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET, ip4)) {
20260304c731SJamie Gritton 				if (ip4 != NULL)
20270304c731SJamie Gritton 					ip4 = NULL;
20280304c731SJamie Gritton 				else
20290304c731SJamie Gritton 					redo_ip4 = 1;
20300304c731SJamie Gritton 			}
20310304c731SJamie Gritton 		}
20320304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20330304c731SJamie Gritton 	}
20340304c731SJamie Gritton #endif
20350304c731SJamie Gritton #ifdef INET6
20360304c731SJamie Gritton 	while (redo_ip6) {
2037eb8dcdeaSGleb Smirnoff 		ip6s = pr->pr_addrs[PR_INET6]->ips;
2038eb8dcdeaSGleb Smirnoff 		ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
20390304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20400304c731SJamie Gritton 		redo_ip6 = 0;
20410304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2042bdfc8cc4SJamie Gritton #ifdef VIMAGE
2043bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
2044bdfc8cc4SJamie Gritton 				descend = 0;
2045bdfc8cc4SJamie Gritton 				continue;
2046bdfc8cc4SJamie Gritton 			}
2047bdfc8cc4SJamie Gritton #endif
2048eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET6, ip6)) {
20490304c731SJamie Gritton 				if (ip6 != NULL)
20500304c731SJamie Gritton 					ip6 = NULL;
20510304c731SJamie Gritton 				else
20520304c731SJamie Gritton 					redo_ip6 = 1;
20530304c731SJamie Gritton 			}
20540304c731SJamie Gritton 		}
20550304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20560304c731SJamie Gritton 	}
20570304c731SJamie Gritton #endif
20580304c731SJamie Gritton 
2059b38ff370SJamie Gritton 	/* Let the modules do their work. */
2060cc5fd8c7SJamie Gritton 	if (born) {
2061b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2062c861373bSJamie Gritton 		if (error)
20632a4b2251SJamie Gritton 			goto done_deref;
2064b38ff370SJamie Gritton 	}
2065b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
2066c861373bSJamie Gritton 	if (error)
20672a4b2251SJamie Gritton 		goto done_deref;
2068b38ff370SJamie Gritton 
20691158508aSJamie Gritton 	/*
20701158508aSJamie Gritton 	 * A new prison is now ready to be seen; either it has gained a user
20711158508aSJamie Gritton 	 * reference via persistence, or is about to gain one via attachment.
20721158508aSJamie Gritton 	 */
20731158508aSJamie Gritton 	if (born) {
20741158508aSJamie Gritton 		drflags = prison_lock_xlock(pr, drflags);
20751158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_ALIVE;
20761158508aSJamie Gritton 	}
20771158508aSJamie Gritton 
2078b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
2079b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
2080c861373bSJamie Gritton 		error = do_jail_attach(td, pr,
2081589e4c1dSJamie Gritton 		    prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
2082f7496dcaSJamie Gritton 		drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2083b38ff370SJamie Gritton 		if (error) {
2084b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
20852a4b2251SJamie Gritton 			goto done_deref;
2086b38ff370SJamie Gritton 		}
2087b38ff370SJamie Gritton 	}
2088b38ff370SJamie Gritton 
20891fb24974SEdward Tomasz Napierala #ifdef RACCT
20904b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
2091701d6b50SJamie Gritton 		if (drflags & PD_LOCKED) {
2092701d6b50SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2093701d6b50SJamie Gritton 			drflags &= ~PD_LOCKED;
2094701d6b50SJamie Gritton 		}
2095f7496dcaSJamie Gritton 		if (drflags & PD_LIST_XLOCKED) {
2096f7496dcaSJamie Gritton 			sx_xunlock(&allprison_lock);
2097f7496dcaSJamie Gritton 			drflags &= ~PD_LIST_XLOCKED;
2098b4e87a63SJamie Gritton 		}
20991fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
21001fb24974SEdward Tomasz Napierala 	}
21011fb24974SEdward Tomasz Napierala #endif
21021fb24974SEdward Tomasz Napierala 
2103c861373bSJamie Gritton 	drflags &= ~PD_KILL;
21041fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
21051fb24974SEdward Tomasz Napierala 
21062a4b2251SJamie Gritton  done_deref:
21072a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
21082a4b2251SJamie Gritton 	if (pr != NULL)
21092a4b2251SJamie Gritton 		prison_deref(pr, drflags);
21102a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2111b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
21122a4b2251SJamie Gritton 	else if (drflags & PD_LIST_XLOCKED)
2113b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
21145050aa86SKonstantin Belousov 	if (root != NULL)
2115b38ff370SJamie Gritton 		vrele(root);
2116b38ff370SJamie Gritton  done_errmsg:
2117b38ff370SJamie Gritton 	if (error) {
21182a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2119176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2120176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
2121b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2122b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2123b38ff370SJamie Gritton 				bcopy(errmsg,
2124b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2125b38ff370SJamie Gritton 				    errmsg_len);
2126b38ff370SJamie Gritton 			else
2127b38ff370SJamie Gritton 				copyout(errmsg,
2128b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2129b38ff370SJamie Gritton 				    errmsg_len);
2130b38ff370SJamie Gritton 		}
2131b38ff370SJamie Gritton 	}
2132b38ff370SJamie Gritton  done_free:
2133b38ff370SJamie Gritton #ifdef INET
2134eb8dcdeaSGleb Smirnoff 	prison_ip_free(ip4);
2135b38ff370SJamie Gritton #endif
2136b38ff370SJamie Gritton #ifdef INET6
2137eb8dcdeaSGleb Smirnoff 	prison_ip_free(ip6);
2138b38ff370SJamie Gritton #endif
21396dfe0a3dSMartin Matuska 	if (g_path != NULL)
21406dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
2141b38ff370SJamie Gritton 	vfs_freeopts(opts);
2142b38ff370SJamie Gritton 	return (error);
2143b38ff370SJamie Gritton }
2144b38ff370SJamie Gritton 
2145b38ff370SJamie Gritton /*
21467de883c8SJamie Gritton  * Find the next available prison ID.  Return the ID on success, or zero
21477de883c8SJamie Gritton  * on failure.  Also set a pointer to the allprison list entry the prison
21487de883c8SJamie Gritton  * should be inserted before.
21497de883c8SJamie Gritton  */
21507de883c8SJamie Gritton static int
21517de883c8SJamie Gritton get_next_prid(struct prison **insprp)
21527de883c8SJamie Gritton {
21537de883c8SJamie Gritton 	struct prison *inspr;
21547de883c8SJamie Gritton 	int jid, maxid;
21557de883c8SJamie Gritton 
21567de883c8SJamie Gritton 	jid = lastprid % JAIL_MAX + 1;
21577de883c8SJamie Gritton 	if (TAILQ_EMPTY(&allprison) ||
21587de883c8SJamie Gritton 	    TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
21597de883c8SJamie Gritton 		/*
21607de883c8SJamie Gritton 		 * A common case is for all jails to be implicitly numbered,
21617de883c8SJamie Gritton 		 * which means they'll go on the end of the list, at least
21627de883c8SJamie Gritton 		 * for the first JAIL_MAX times.
21637de883c8SJamie Gritton 		 */
21647de883c8SJamie Gritton 		inspr = NULL;
21657de883c8SJamie Gritton 	} else {
21667de883c8SJamie Gritton 		/*
21677de883c8SJamie Gritton 		 * Take two passes through the allprison list: first starting
21687de883c8SJamie Gritton 		 * with the proposed jid, then ending with it.
21697de883c8SJamie Gritton 		 */
21707de883c8SJamie Gritton 		for (maxid = JAIL_MAX; maxid != 0; ) {
21717de883c8SJamie Gritton 			TAILQ_FOREACH(inspr, &allprison, pr_list) {
21727de883c8SJamie Gritton 				if (inspr->pr_id < jid)
21737de883c8SJamie Gritton 					continue;
2174f7496dcaSJamie Gritton 				if (inspr->pr_id > jid) {
2175f7496dcaSJamie Gritton 					/* Found an opening. */
21767de883c8SJamie Gritton 					maxid = 0;
21777de883c8SJamie Gritton 					break;
21787de883c8SJamie Gritton 				}
21797de883c8SJamie Gritton 				if (++jid > maxid) {
21807de883c8SJamie Gritton 					if (lastprid == maxid || lastprid == 0)
21817de883c8SJamie Gritton 					{
21827de883c8SJamie Gritton 						/*
21837de883c8SJamie Gritton 						 * The entire legal range
21847de883c8SJamie Gritton 						 * has been traversed
21857de883c8SJamie Gritton 						 */
21867de883c8SJamie Gritton 						return 0;
21877de883c8SJamie Gritton 					}
21887de883c8SJamie Gritton 					/* Try again from the start. */
21897de883c8SJamie Gritton 					jid = 1;
21907de883c8SJamie Gritton 					maxid = lastprid;
21917de883c8SJamie Gritton 					break;
21927de883c8SJamie Gritton 				}
21937de883c8SJamie Gritton 			}
21947de883c8SJamie Gritton 			if (inspr == NULL) {
21957de883c8SJamie Gritton 				/* Found room at the end of the list. */
21967de883c8SJamie Gritton 				break;
21977de883c8SJamie Gritton 			}
21987de883c8SJamie Gritton 		}
21997de883c8SJamie Gritton 	}
22007de883c8SJamie Gritton 	*insprp = inspr;
22017de883c8SJamie Gritton 	lastprid = jid;
22027de883c8SJamie Gritton 	return (jid);
22037de883c8SJamie Gritton }
22047de883c8SJamie Gritton 
22057de883c8SJamie Gritton /*
2206b38ff370SJamie Gritton  * struct jail_get_args {
2207b38ff370SJamie Gritton  *	struct iovec *iovp;
2208b38ff370SJamie Gritton  *	unsigned int iovcnt;
2209b38ff370SJamie Gritton  *	int flags;
2210b38ff370SJamie Gritton  * };
2211b38ff370SJamie Gritton  */
2212b38ff370SJamie Gritton int
22138451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
2214b38ff370SJamie Gritton {
2215b38ff370SJamie Gritton 	struct uio *auio;
2216b38ff370SJamie Gritton 	int error;
2217b38ff370SJamie Gritton 
2218b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
2219b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
2220b38ff370SJamie Gritton 		return (EINVAL);
2221b38ff370SJamie Gritton 
2222b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2223b38ff370SJamie Gritton 	if (error)
2224b38ff370SJamie Gritton 		return (error);
2225b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
2226b38ff370SJamie Gritton 	if (error == 0)
2227b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
2228b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
2229b38ff370SJamie Gritton 	free(auio, M_IOV);
2230b38ff370SJamie Gritton 	return (error);
2231b38ff370SJamie Gritton }
2232b38ff370SJamie Gritton 
2233b38ff370SJamie Gritton int
2234b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2235b38ff370SJamie Gritton {
2236672756aaSJamie Gritton 	struct bool_flags *bf;
2237672756aaSJamie Gritton 	struct jailsys_flags *jsf;
22380304c731SJamie Gritton 	struct prison *pr, *mypr;
2239b38ff370SJamie Gritton 	struct vfsopt *opt;
2240b38ff370SJamie Gritton 	struct vfsoptlist *opts;
2241b38ff370SJamie Gritton 	char *errmsg, *name;
22422a4b2251SJamie Gritton 	int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2243672756aaSJamie Gritton 	unsigned f;
2244b38ff370SJamie Gritton 
2245b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
2246b38ff370SJamie Gritton 		return (EINVAL);
2247b38ff370SJamie Gritton 
2248b38ff370SJamie Gritton 	/* Get the parameter list. */
2249b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
2250b38ff370SJamie Gritton 	if (error)
2251b38ff370SJamie Gritton 		return (error);
2252b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
22530304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
22542a4b2251SJamie Gritton 	pr = NULL;
22551e2a13e6SJamie Gritton 
2256b38ff370SJamie Gritton 	/*
2257b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
2258b38ff370SJamie Gritton 	 */
2259b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
22602a4b2251SJamie Gritton 	drflags = PD_LIST_SLOCKED;
2261b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2262b38ff370SJamie Gritton 	if (error == 0) {
2263b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
2264f7496dcaSJamie Gritton 			if (pr->pr_id > jid &&
2265f7496dcaSJamie Gritton 			    ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2266f7496dcaSJamie Gritton 			    prison_ischild(mypr, pr)) {
2267b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
22682a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
2269b38ff370SJamie Gritton 				goto found_prison;
22702a4b2251SJamie Gritton 			}
2271f7496dcaSJamie Gritton 		}
2272b38ff370SJamie Gritton 		error = ENOENT;
2273b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
22742a4b2251SJamie Gritton 		goto done;
2275b38ff370SJamie Gritton 	} else if (error != ENOENT)
22762a4b2251SJamie Gritton 		goto done;
2277b38ff370SJamie Gritton 
2278b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2279b38ff370SJamie Gritton 	if (error == 0) {
2280b38ff370SJamie Gritton 		if (jid != 0) {
22810304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
2282b38ff370SJamie Gritton 			if (pr != NULL) {
22832a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
228476ad42abSJamie Gritton 				if (!(prison_isalive(pr) ||
228576ad42abSJamie Gritton 				    (flags & JAIL_DYING))) {
2286b38ff370SJamie Gritton 					error = ENOENT;
2287b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
2288b38ff370SJamie Gritton 					    jid);
22892a4b2251SJamie Gritton 					goto done;
2290b38ff370SJamie Gritton 				}
2291b38ff370SJamie Gritton 				goto found_prison;
2292b38ff370SJamie Gritton 			}
2293b38ff370SJamie Gritton 			error = ENOENT;
2294b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
22952a4b2251SJamie Gritton 			goto done;
2296b38ff370SJamie Gritton 		}
2297b38ff370SJamie Gritton 	} else if (error != ENOENT)
22982a4b2251SJamie Gritton 		goto done;
2299b38ff370SJamie Gritton 
2300b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2301b38ff370SJamie Gritton 	if (error == 0) {
2302b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2303b38ff370SJamie Gritton 			error = EINVAL;
23042a4b2251SJamie Gritton 			goto done;
2305b38ff370SJamie Gritton 		}
23060304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2307b38ff370SJamie Gritton 		if (pr != NULL) {
23082a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
230976ad42abSJamie Gritton 			if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2310b38ff370SJamie Gritton 				error = ENOENT;
2311b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2312b38ff370SJamie Gritton 				    name);
23132a4b2251SJamie Gritton 				goto done;
2314b38ff370SJamie Gritton 			}
2315b38ff370SJamie Gritton 			goto found_prison;
2316b38ff370SJamie Gritton 		}
2317b38ff370SJamie Gritton 		error = ENOENT;
2318b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
23192a4b2251SJamie Gritton 		goto done;
2320b38ff370SJamie Gritton 	} else if (error != ENOENT)
23212a4b2251SJamie Gritton 		goto done;
2322b38ff370SJamie Gritton 
2323b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2324b38ff370SJamie Gritton 	error = ENOENT;
23252a4b2251SJamie Gritton 	goto done;
2326b38ff370SJamie Gritton 
2327b38ff370SJamie Gritton  found_prison:
2328b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
23296754ae25SJamie Gritton 	prison_hold(pr);
23302a4b2251SJamie Gritton 	drflags |= PD_DEREF;
2331b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2332b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2333b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23342a4b2251SJamie Gritton 		goto done;
23350304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
23360304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2337b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23382a4b2251SJamie Gritton 		goto done;
23390304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
23400304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
23412a4b2251SJamie Gritton 		goto done;
23420304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2343b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2344b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23452a4b2251SJamie Gritton 		goto done;
23460304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2347b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23482a4b2251SJamie Gritton 		goto done;
2349b38ff370SJamie Gritton #ifdef INET
2350eb8dcdeaSGleb Smirnoff 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1,
2351eb8dcdeaSGleb Smirnoff 	    pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2352eb8dcdeaSGleb Smirnoff 	    pr_families[PR_INET].size : 0 );
2353b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23542a4b2251SJamie Gritton 		goto done;
2355b38ff370SJamie Gritton #endif
2356b38ff370SJamie Gritton #ifdef INET6
2357eb8dcdeaSGleb Smirnoff 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1,
2358eb8dcdeaSGleb Smirnoff 	    pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2359eb8dcdeaSGleb Smirnoff 	    pr_families[PR_INET6].size : 0 );
2360b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23612a4b2251SJamie Gritton 		goto done;
2362b38ff370SJamie Gritton #endif
2363b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2364b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2365b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23662a4b2251SJamie Gritton 		goto done;
2367b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2368b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2369b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
23702a4b2251SJamie Gritton 		goto done;
2371b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2372b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2373b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
23742a4b2251SJamie Gritton 		goto done;
2375c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2376b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23772a4b2251SJamie Gritton 		goto done;
2378c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
237976ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
23802a4b2251SJamie Gritton 		goto done;
2381c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
238276ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
23832a4b2251SJamie Gritton 		goto done;
2384841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2385a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
238676ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
238776ca6f88SJamie Gritton 
238876ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
238976ca6f88SJamie Gritton 	} else
239076ca6f88SJamie Gritton #endif
239176ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
239276ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
239376ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
23942a4b2251SJamie Gritton 		goto done;
23950304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
23960304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
23970304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
23982a4b2251SJamie Gritton 		goto done;
23990cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
24000cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
24010cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
24022a4b2251SJamie Gritton 		goto done;
2403672756aaSJamie Gritton 	for (bf = pr_flag_bool;
2404672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2405672756aaSJamie Gritton 	     bf++) {
2406672756aaSJamie Gritton 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2407672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2408b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
24092a4b2251SJamie Gritton 			goto done;
2410b38ff370SJamie Gritton 		i = !i;
2411672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2412b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
24132a4b2251SJamie Gritton 			goto done;
24140304c731SJamie Gritton 	}
2415672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
2416672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2417672756aaSJamie Gritton 	     jsf++) {
2418672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
2419672756aaSJamie Gritton 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2420672756aaSJamie Gritton 		    : (f == jsf->new) ? JAIL_SYS_NEW
24217cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
2422672756aaSJamie Gritton 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
24237cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
24242a4b2251SJamie Gritton 			goto done;
24257cbf7213SJamie Gritton 	}
2426672756aaSJamie Gritton 	for (bf = pr_flag_allow;
24275d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
24285d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
2429672756aaSJamie Gritton 	     bf++) {
2430672756aaSJamie Gritton 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2431672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
24320304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
24332a4b2251SJamie Gritton 			goto done;
24340304c731SJamie Gritton 		i = !i;
2435672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
24360304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
24372a4b2251SJamie Gritton 			goto done;
24380304c731SJamie Gritton 	}
243976ad42abSJamie Gritton 	i = !prison_isalive(pr);
2440b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2441b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24422a4b2251SJamie Gritton 		goto done;
2443b38ff370SJamie Gritton 	i = !i;
2444b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2445b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24462a4b2251SJamie Gritton 		goto done;
2447bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2448bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2449a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
24502a4b2251SJamie Gritton 		goto done;
2451a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2452a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
24532a4b2251SJamie Gritton 		goto done;
2454b38ff370SJamie Gritton 
2455b38ff370SJamie Gritton 	/* Get the module parameters. */
2456b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
24572a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
2458b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2459b38ff370SJamie Gritton 	if (error)
24602a4b2251SJamie Gritton 		goto done;
24612a4b2251SJamie Gritton 	prison_deref(pr, drflags);
24622a4b2251SJamie Gritton 	pr = NULL;
24632a4b2251SJamie Gritton 	drflags = 0;
2464b38ff370SJamie Gritton 
2465b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2466b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2467b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2468b38ff370SJamie Gritton 			error = EINVAL;
2469b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
24702a4b2251SJamie Gritton 			goto done;
2471b38ff370SJamie Gritton 		}
2472b38ff370SJamie Gritton 	}
2473b38ff370SJamie Gritton 
2474b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2475b38ff370SJamie Gritton 	error = 0;
2476b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2477b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2478b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2479b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2480b38ff370SJamie Gritton 			if (opt->value != NULL) {
2481b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2482b38ff370SJamie Gritton 					bcopy(opt->value,
2483b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2484b38ff370SJamie Gritton 					    opt->len);
2485b38ff370SJamie Gritton 				} else {
2486b38ff370SJamie Gritton 					error = copyout(opt->value,
2487b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2488b38ff370SJamie Gritton 					    opt->len);
2489b38ff370SJamie Gritton 					if (error)
2490b38ff370SJamie Gritton 						break;
2491b38ff370SJamie Gritton 				}
2492b38ff370SJamie Gritton 			}
2493b38ff370SJamie Gritton 		}
2494b38ff370SJamie Gritton 	}
2495b38ff370SJamie Gritton 
24962a4b2251SJamie Gritton  done:
24972a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
24982a4b2251SJamie Gritton 	if (pr != NULL)
24992a4b2251SJamie Gritton 		prison_deref(pr, drflags);
25002a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2501b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2502b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
25032a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2504b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2505b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2506b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2507b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2508b38ff370SJamie Gritton 				bcopy(errmsg,
2509b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2510b38ff370SJamie Gritton 				    errmsg_len);
2511b38ff370SJamie Gritton 			else
2512b38ff370SJamie Gritton 				copyout(errmsg,
2513b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2514b38ff370SJamie Gritton 				    errmsg_len);
2515b38ff370SJamie Gritton 		}
2516b38ff370SJamie Gritton 	}
2517b38ff370SJamie Gritton 	vfs_freeopts(opts);
2518b38ff370SJamie Gritton 	return (error);
2519b38ff370SJamie Gritton }
2520b38ff370SJamie Gritton 
2521b38ff370SJamie Gritton /*
2522b38ff370SJamie Gritton  * struct jail_remove_args {
2523b38ff370SJamie Gritton  *	int jid;
2524b38ff370SJamie Gritton  * };
2525b38ff370SJamie Gritton  */
2526b38ff370SJamie Gritton int
25278451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2528b38ff370SJamie Gritton {
2529c861373bSJamie Gritton 	struct prison *pr;
2530c861373bSJamie Gritton 	int error;
2531b38ff370SJamie Gritton 
2532b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2533b38ff370SJamie Gritton 	if (error)
2534b38ff370SJamie Gritton 		return (error);
2535b38ff370SJamie Gritton 
2536b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
25370304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2538b38ff370SJamie Gritton 	if (pr == NULL) {
2539b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2540b38ff370SJamie Gritton 		return (EINVAL);
2541b38ff370SJamie Gritton 	}
2542c861373bSJamie Gritton 	if (!prison_isalive(pr)) {
2543c861373bSJamie Gritton 		/* Silently ignore already-dying prisons. */
25440304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2545c861373bSJamie Gritton 		sx_xunlock(&allprison_lock);
25460304c731SJamie Gritton 		return (0);
25470304c731SJamie Gritton 	}
2548c861373bSJamie Gritton 	prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2549c861373bSJamie Gritton 	return (0);
255075c13541SPoul-Henning Kamp }
255175c13541SPoul-Henning Kamp 
2552fd7a8150SMike Barcroft /*
25539ddb7954SMike Barcroft  * struct jail_attach_args {
25549ddb7954SMike Barcroft  *	int jid;
25559ddb7954SMike Barcroft  * };
2556fd7a8150SMike Barcroft  */
2557fd7a8150SMike Barcroft int
25588451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2559fd7a8150SMike Barcroft {
2560b38ff370SJamie Gritton 	struct prison *pr;
2561b38ff370SJamie Gritton 	int error;
2562b38ff370SJamie Gritton 
2563b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2564b38ff370SJamie Gritton 	if (error)
2565b38ff370SJamie Gritton 		return (error);
2566b38ff370SJamie Gritton 
2567f7496dcaSJamie Gritton 	sx_slock(&allprison_lock);
25680304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2569b38ff370SJamie Gritton 	if (pr == NULL) {
2570b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2571b38ff370SJamie Gritton 		return (EINVAL);
2572b38ff370SJamie Gritton 	}
2573b38ff370SJamie Gritton 
257476ad42abSJamie Gritton 	/* Do not allow a process to attach to a prison that is not alive. */
257576ad42abSJamie Gritton 	if (!prison_isalive(pr)) {
2576b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2577b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2578b38ff370SJamie Gritton 		return (EINVAL);
2579b38ff370SJamie Gritton 	}
2580b38ff370SJamie Gritton 
2581f7496dcaSJamie Gritton 	return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2582b38ff370SJamie Gritton }
2583b38ff370SJamie Gritton 
2584b38ff370SJamie Gritton static int
2585f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2586b38ff370SJamie Gritton {
2587fd7a8150SMike Barcroft 	struct proc *p;
2588fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
25895050aa86SKonstantin Belousov 	int error;
2590fd7a8150SMike Barcroft 
2591f7496dcaSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2592f7496dcaSJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
2593589e4c1dSJamie Gritton 	drflags &= PD_LOCK_FLAGS;
259457f22bd4SJacques Vidrine 	/*
259557f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
259657f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
259757f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
259857f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
259957f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
260057f22bd4SJacques Vidrine 	 * of another.
260157f22bd4SJacques Vidrine 	 */
26021158508aSJamie Gritton 	prison_hold(pr);
26036754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
2604f7496dcaSJamie Gritton 	drflags |= PD_DEREF | PD_DEUREF;
2605fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2606f7496dcaSJamie Gritton 	drflags &= ~PD_LOCKED;
2607b38ff370SJamie Gritton 
2608b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2609b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2610b38ff370SJamie Gritton 	if (error) {
2611f7496dcaSJamie Gritton 		prison_deref(pr, drflags);
2612b38ff370SJamie Gritton 		return (error);
2613b38ff370SJamie Gritton 	}
2614f7496dcaSJamie Gritton 	sx_unlock(&allprison_lock);
2615f7496dcaSJamie Gritton 	drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2616fd7a8150SMike Barcroft 
2617413628a7SBjoern A. Zeeb 	/*
2618413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2619413628a7SBjoern A. Zeeb 	 */
2620b38ff370SJamie Gritton 	p = td->td_proc;
2621413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2622413628a7SBjoern A. Zeeb 	if (error)
2623b38ff370SJamie Gritton 		goto e_revert_osd;
2624413628a7SBjoern A. Zeeb 
2625cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2626fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2627fd7a8150SMike Barcroft 		goto e_unlock;
2628fd7a8150SMike Barcroft #ifdef MAC
262930d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2630fd7a8150SMike Barcroft 		goto e_unlock;
2631fd7a8150SMike Barcroft #endif
2632b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2633d4380c0cSJamie Gritton 	if ((error = pwd_chroot_chdir(td, pr->pr_root)))
26345050aa86SKonstantin Belousov 		goto e_revert_osd;
2635fd7a8150SMike Barcroft 
2636fd7a8150SMike Barcroft 	newcred = crget();
2637fd7a8150SMike Barcroft 	PROC_LOCK(p);
26381fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
263969c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2640daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
26411fb6767dSJamie Gritton 	setsugid(p);
2642097055e2SEdward Tomasz Napierala #ifdef RACCT
2643097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2644f87beb93SAndriy Gapon 	crhold(newcred);
2645f87beb93SAndriy Gapon #endif
2646f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
2647f87beb93SAndriy Gapon #ifdef RCTL
2648f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
2649f87beb93SAndriy Gapon 	crfree(newcred);
2650097055e2SEdward Tomasz Napierala #endif
2651f7496dcaSJamie Gritton 	prison_deref(oldcred->cr_prison, drflags);
2652fd7a8150SMike Barcroft 	crfree(oldcred);
2653cc7b7306SJamie Gritton 
2654cc7b7306SJamie Gritton 	/*
2655cc7b7306SJamie Gritton 	 * If the prison was killed while changing credentials, die along
2656cc7b7306SJamie Gritton 	 * with it.
2657cc7b7306SJamie Gritton 	 */
2658cc7b7306SJamie Gritton 	if (!prison_isalive(pr)) {
2659cc7b7306SJamie Gritton 		PROC_LOCK(p);
2660cc7b7306SJamie Gritton 		kern_psignal(p, SIGKILL);
2661cc7b7306SJamie Gritton 		PROC_UNLOCK(p);
2662cc7b7306SJamie Gritton 	}
2663cc7b7306SJamie Gritton 
2664fd7a8150SMike Barcroft 	return (0);
26651fb6767dSJamie Gritton 
2666fd7a8150SMike Barcroft  e_unlock:
2667b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2668b38ff370SJamie Gritton  e_revert_osd:
2669b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
26707f4e7248SJamie Gritton 	sx_slock(&allprison_lock);
2671f7496dcaSJamie Gritton 	drflags |= PD_LIST_SLOCKED;
26721fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2673f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2674fd7a8150SMike Barcroft 	return (error);
2675fd7a8150SMike Barcroft }
2676fd7a8150SMike Barcroft 
2677fd7a8150SMike Barcroft /*
2678fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2679fd7a8150SMike Barcroft  */
268054b369c1SPawel Jakub Dawidek struct prison *
2681fd7a8150SMike Barcroft prison_find(int prid)
2682fd7a8150SMike Barcroft {
2683fd7a8150SMike Barcroft 	struct prison *pr;
2684fd7a8150SMike Barcroft 
2685dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2686b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2687f7496dcaSJamie Gritton 		if (pr->pr_id < prid)
2688f7496dcaSJamie Gritton 			continue;
26897de883c8SJamie Gritton 		if (pr->pr_id > prid)
26907de883c8SJamie Gritton 			break;
2691f7496dcaSJamie Gritton 		KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2692f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
2693f7496dcaSJamie Gritton 		return (pr);
2694fd7a8150SMike Barcroft 	}
2695fd7a8150SMike Barcroft 	return (NULL);
2696fd7a8150SMike Barcroft }
2697fd7a8150SMike Barcroft 
2698b38ff370SJamie Gritton /*
26990304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2700b38ff370SJamie Gritton  */
2701b38ff370SJamie Gritton struct prison *
27020304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2703b38ff370SJamie Gritton {
27040304c731SJamie Gritton 	struct prison *pr;
27050304c731SJamie Gritton 	int descend;
2706b38ff370SJamie Gritton 
2707b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
27080304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
27090304c731SJamie Gritton 		if (pr->pr_id == prid) {
2710f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2711f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
27120304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27130304c731SJamie Gritton 			return (pr);
27140304c731SJamie Gritton 		}
27150304c731SJamie Gritton 	}
27160304c731SJamie Gritton 	return (NULL);
27170304c731SJamie Gritton }
27180304c731SJamie Gritton 
27190304c731SJamie Gritton /*
27200304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
27210304c731SJamie Gritton  */
27220304c731SJamie Gritton struct prison *
27230304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
27240304c731SJamie Gritton {
27250304c731SJamie Gritton 	struct prison *pr, *deadpr;
27260304c731SJamie Gritton 	size_t mylen;
27270304c731SJamie Gritton 	int descend;
27280304c731SJamie Gritton 
27290304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
27300304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2731b38ff370SJamie Gritton 	deadpr = NULL;
27320304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
27330304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2734f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2735f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
2736f7496dcaSJamie Gritton 			if (prison_isalive(pr)) {
2737b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
2738b38ff370SJamie Gritton 				return (pr);
2739f7496dcaSJamie Gritton 			}
2740b38ff370SJamie Gritton 			deadpr = pr;
2741b38ff370SJamie Gritton 		}
2742b38ff370SJamie Gritton 	}
27430304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2744f7496dcaSJamie Gritton 	if (deadpr != NULL)
2745b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2746b38ff370SJamie Gritton 	return (deadpr);
2747b38ff370SJamie Gritton }
2748b38ff370SJamie Gritton 
2749b38ff370SJamie Gritton /*
27500fe74ae6SJamie Gritton  * See if a prison has the specific flag set.  The prison should be locked,
27510fe74ae6SJamie Gritton  * unless checking for flags that are only set at jail creation (such as
27520fe74ae6SJamie Gritton  * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
27530fe74ae6SJamie Gritton  * to any other prison data.
27540304c731SJamie Gritton  */
27550304c731SJamie Gritton int
27560304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
27570304c731SJamie Gritton {
27580304c731SJamie Gritton 
27590304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
27600304c731SJamie Gritton }
27610304c731SJamie Gritton 
27620304c731SJamie Gritton int
27630304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
27640304c731SJamie Gritton {
27650304c731SJamie Gritton 
27660fe74ae6SJamie Gritton 	return ((cred->cr_prison->pr_allow & flag) != 0);
27670304c731SJamie Gritton }
27680304c731SJamie Gritton 
27690304c731SJamie Gritton /*
2770effad35eSJamie Gritton  * Hold a prison reference, by incrementing pr_ref.  It is generally
2771effad35eSJamie Gritton  * an error to hold a prison that does not already have a reference.
2772effad35eSJamie Gritton  * A prison record will remain valid as long as it has at least one
2773effad35eSJamie Gritton  * reference, and will not be removed as long as either the prison
2774effad35eSJamie Gritton  * mutex or the allprison lock is held (allprison_lock may be shared).
2775effad35eSJamie Gritton  */
2776effad35eSJamie Gritton void
2777effad35eSJamie Gritton prison_hold_locked(struct prison *pr)
2778effad35eSJamie Gritton {
2779effad35eSJamie Gritton 
27806754ae25SJamie Gritton 	/* Locking is no longer required. */
27816754ae25SJamie Gritton 	prison_hold(pr);
2782effad35eSJamie Gritton }
2783effad35eSJamie Gritton 
2784effad35eSJamie Gritton void
2785effad35eSJamie Gritton prison_hold(struct prison *pr)
2786effad35eSJamie Gritton {
27876754ae25SJamie Gritton #ifdef INVARIANTS
27886754ae25SJamie Gritton 	int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2789effad35eSJamie Gritton 
27906754ae25SJamie Gritton 	KASSERT(was_valid,
27916754ae25SJamie Gritton 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
27926754ae25SJamie Gritton #else
27936754ae25SJamie Gritton 	refcount_acquire(&pr->pr_ref);
27946754ae25SJamie Gritton #endif
2795effad35eSJamie Gritton }
2796effad35eSJamie Gritton 
2797effad35eSJamie Gritton /*
2798effad35eSJamie Gritton  * Remove a prison reference.  If that was the last reference, the
2799f7496dcaSJamie Gritton  * prison will be removed (at a later time).
2800b38ff370SJamie Gritton  */
280191421ba2SRobert Watson void
28021ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
280391421ba2SRobert Watson {
280491421ba2SRobert Watson 
28051ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2806effad35eSJamie Gritton 	/*
2807f7496dcaSJamie Gritton 	 * Locking is no longer required, but unlock because the caller
2808f7496dcaSJamie Gritton 	 * expects it.
2809effad35eSJamie Gritton 	 */
2810f7496dcaSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2811f7496dcaSJamie Gritton 	prison_free(pr);
2812effad35eSJamie Gritton }
2813c2cda609SPawel Jakub Dawidek 
28141ba4a712SPawel Jakub Dawidek void
28151ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
28161ba4a712SPawel Jakub Dawidek {
28171ba4a712SPawel Jakub Dawidek 
28186754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
28196754ae25SJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
28206754ae25SJamie Gritton 	     pr, pr->pr_id));
2821f7496dcaSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_ref)) {
2822f7496dcaSJamie Gritton 		/*
2823f7496dcaSJamie Gritton 		 * Don't remove the last reference in this context,
2824f7496dcaSJamie Gritton 		 * in case there are locks held.
2825f7496dcaSJamie Gritton 		 */
2826f7496dcaSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2827f7496dcaSJamie Gritton 	}
2828f7496dcaSJamie Gritton }
2829f7496dcaSJamie Gritton 
2830f7496dcaSJamie Gritton static void
2831f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr)
2832f7496dcaSJamie Gritton {
2833f7496dcaSJamie Gritton #ifdef INVARIANTS
2834f7496dcaSJamie Gritton 	int lastref;
2835f7496dcaSJamie Gritton 
2836f7496dcaSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
2837f7496dcaSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2838f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2839f7496dcaSJamie Gritton 	lastref = refcount_release(&pr->pr_ref);
2840f7496dcaSJamie Gritton 	KASSERT(!lastref,
2841f7496dcaSJamie Gritton 	    ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2842f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2843f7496dcaSJamie Gritton #else
2844ee9b37aeSMateusz Guzik 	refcount_release(&pr->pr_ref);
2845f7496dcaSJamie Gritton #endif
28461ba4a712SPawel Jakub Dawidek }
28471ba4a712SPawel Jakub Dawidek 
284873d9e52dSJamie Gritton /*
2849f171938cSGordon Bergling  * Hold a prison for user visibility, by incrementing pr_uref.
2850effad35eSJamie Gritton  * It is generally an error to hold a prison that isn't already
2851effad35eSJamie Gritton  * user-visible, except through the the jail system calls.  It is also
2852effad35eSJamie Gritton  * an error to hold an invalid prison.  A prison record will remain
2853effad35eSJamie Gritton  * alive as long as it has at least one user reference, and will not
2854f7496dcaSJamie Gritton  * be set to the dying state until the prison mutex and allprison_lock
2855f7496dcaSJamie Gritton  * are both freed.
2856effad35eSJamie Gritton  */
2857effad35eSJamie Gritton void
2858effad35eSJamie Gritton prison_proc_hold(struct prison *pr)
2859effad35eSJamie Gritton {
28606754ae25SJamie Gritton #ifdef INVARIANTS
28616754ae25SJamie Gritton 	int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2862effad35eSJamie Gritton 
28636754ae25SJamie Gritton 	KASSERT(was_alive,
2864effad35eSJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
28656754ae25SJamie Gritton #else
28666754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
28676754ae25SJamie Gritton #endif
2868effad35eSJamie Gritton }
2869effad35eSJamie Gritton 
2870effad35eSJamie Gritton /*
2871effad35eSJamie Gritton  * Remove a prison user reference.  If it was the last reference, the
2872effad35eSJamie Gritton  * prison will be considered "dying", and may be removed once all of
2873effad35eSJamie Gritton  * its references are dropped.
2874effad35eSJamie Gritton  */
2875effad35eSJamie Gritton void
2876effad35eSJamie Gritton prison_proc_free(struct prison *pr)
2877effad35eSJamie Gritton {
2878effad35eSJamie Gritton 
28796754ae25SJamie Gritton 	/*
28806754ae25SJamie Gritton 	 * Locking is only required when releasing the last reference.
28816754ae25SJamie Gritton 	 * This allows assurance that a locked prison will remain alive
28826754ae25SJamie Gritton 	 * until it is unlocked.
28836754ae25SJamie Gritton 	 */
28846754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2885effad35eSJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2886195cd6aeSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_uref)) {
2887effad35eSJamie Gritton 		/*
2888effad35eSJamie Gritton 		 * Don't remove the last user reference in this context,
2889effad35eSJamie Gritton 		 * which is expected to be a process that is not only locked,
2890effad35eSJamie Gritton 		 * but also half dead.  Add a reference so any calls to
2891effad35eSJamie Gritton 		 * prison_free() won't re-submit the task.
2892effad35eSJamie Gritton 		 */
2893f7496dcaSJamie Gritton 		prison_hold(pr);
28941158508aSJamie Gritton 		mtx_lock(&pr->pr_mtx);
28951158508aSJamie Gritton 		KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
28961158508aSJamie Gritton 		    ("Redundant last reference in prison_proc_free (jid=%d)",
28971158508aSJamie Gritton 		     pr->pr_id));
28981158508aSJamie Gritton 		pr->pr_flags |= PR_COMPLETE_PROC;
28991158508aSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2900effad35eSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2901effad35eSJamie Gritton 	}
2902effad35eSJamie Gritton }
2903effad35eSJamie Gritton 
2904c861373bSJamie Gritton static void
2905c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr)
2906c861373bSJamie Gritton {
2907c861373bSJamie Gritton #ifdef INVARIANTS
2908c861373bSJamie Gritton 	int lastref;
2909c861373bSJamie Gritton 
2910c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2911c861373bSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2912c861373bSJamie Gritton 	     pr, pr->pr_id));
2913c861373bSJamie Gritton 	lastref = refcount_release(&pr->pr_uref);
2914c861373bSJamie Gritton 	KASSERT(!lastref,
2915c861373bSJamie Gritton 	    ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2916c861373bSJamie Gritton 	     pr, pr->pr_id));
2917c861373bSJamie Gritton #else
2918c861373bSJamie Gritton 	refcount_release(&pr->pr_uref);
2919c861373bSJamie Gritton #endif
2920c861373bSJamie Gritton }
2921c861373bSJamie Gritton 
2922effad35eSJamie Gritton /*
292373d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
292473d9e52dSJamie Gritton  */
2925c2cda609SPawel Jakub Dawidek static void
2926c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2927c2cda609SPawel Jakub Dawidek {
292873d9e52dSJamie Gritton 	struct prison *pr = context;
2929f7496dcaSJamie Gritton 	int drflags;
2930b38ff370SJamie Gritton 
2931effad35eSJamie Gritton 	/*
29321158508aSJamie Gritton 	 * This could be called to release the last reference, or the last
29331158508aSJamie Gritton 	 * user reference (plus the reference held in prison_proc_free).
2934effad35eSJamie Gritton 	 */
2935f7496dcaSJamie Gritton 	drflags = prison_lock_xlock(pr, PD_DEREF);
29361158508aSJamie Gritton 	if (pr->pr_flags & PR_COMPLETE_PROC) {
29371158508aSJamie Gritton 		pr->pr_flags &= ~PR_COMPLETE_PROC;
2938f7496dcaSJamie Gritton 		drflags |= PD_DEUREF;
29391158508aSJamie Gritton 	}
2940f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2941b38ff370SJamie Gritton }
2942b38ff370SJamie Gritton 
2943b38ff370SJamie Gritton /*
2944effad35eSJamie Gritton  * Remove a prison reference and/or user reference (usually).
2945effad35eSJamie Gritton  * This assumes context that allows sleeping (for allprison_lock),
2946effad35eSJamie Gritton  * with no non-sleeping locks held, except perhaps the prison itself.
2947effad35eSJamie Gritton  * If there are no more references, release and delist the prison.
2948effad35eSJamie Gritton  * On completion, the prison lock and the allprison lock are both
2949effad35eSJamie Gritton  * unlocked.
2950b38ff370SJamie Gritton  */
2951b38ff370SJamie Gritton static void
2952b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2953b38ff370SJamie Gritton {
29546e1d1bfcSJamie Gritton 	struct prisonlist freeprison;
2955c861373bSJamie Gritton 	struct prison *killpr, *rpr, *ppr, *tpr;
2956c861373bSJamie Gritton 	struct proc *p;
2957c2cda609SPawel Jakub Dawidek 
2958c861373bSJamie Gritton 	killpr = NULL;
29596e1d1bfcSJamie Gritton 	TAILQ_INIT(&freeprison);
29606e1d1bfcSJamie Gritton 	/*
29616e1d1bfcSJamie Gritton 	 * Release this prison as requested, which may cause its parent
29626e1d1bfcSJamie Gritton 	 * to be released, and then maybe its grandparent, etc.
29636e1d1bfcSJamie Gritton 	 */
29640304c731SJamie Gritton 	for (;;) {
2965c861373bSJamie Gritton 		if (flags & PD_KILL) {
2966c861373bSJamie Gritton 			/* Kill the prison and its descendents. */
2967589e4c1dSJamie Gritton 			KASSERT(pr != &prison0,
2968589e4c1dSJamie Gritton 			    ("prison_deref trying to kill prison0"));
2969c861373bSJamie Gritton 			if (!(flags & PD_DEREF)) {
2970c861373bSJamie Gritton 				prison_hold(pr);
2971c861373bSJamie Gritton 				flags |= PD_DEREF;
2972c861373bSJamie Gritton 			}
2973c861373bSJamie Gritton 			flags = prison_lock_xlock(pr, flags);
2974c861373bSJamie Gritton 			prison_deref_kill(pr, &freeprison);
2975c861373bSJamie Gritton 		}
2976e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
2977f7496dcaSJamie Gritton 			/* Drop a user reference. */
29786754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_uref) > 0,
297973d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
298073d9e52dSJamie Gritton 			     pr->pr_id));
2981f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_uref)) {
2982f7496dcaSJamie Gritton 				if (!(flags & PD_DEREF)) {
2983f7496dcaSJamie Gritton 					prison_hold(pr);
2984f7496dcaSJamie Gritton 					flags |= PD_DEREF;
2985f7496dcaSJamie Gritton 				}
2986f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
29871158508aSJamie Gritton 				if (refcount_release(&pr->pr_uref) &&
29881158508aSJamie Gritton 				    pr->pr_state == PRISON_STATE_ALIVE) {
2989f7496dcaSJamie Gritton 					/*
2990f7496dcaSJamie Gritton 					 * When the last user references goes,
2991f7496dcaSJamie Gritton 					 * this becomes a dying prison.
2992f7496dcaSJamie Gritton 					 */
2993f7496dcaSJamie Gritton 					KASSERT(
2994f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_uref) > 0,
29956754ae25SJamie Gritton 					    ("prison0 pr_uref=0"));
29961158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_DYING;
2997f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
2998f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
2999a9f7455cSJamie Gritton 					prison_cleanup(pr);
3000f7496dcaSJamie Gritton 				}
3001f7496dcaSJamie Gritton 			}
3002f7496dcaSJamie Gritton 		}
3003c861373bSJamie Gritton 		if (flags & PD_KILL) {
3004c861373bSJamie Gritton 			/*
3005c861373bSJamie Gritton 			 * Any remaining user references are probably processes
3006c861373bSJamie Gritton 			 * that need to be killed, either in this prison or its
3007c861373bSJamie Gritton 			 * descendants.
3008c861373bSJamie Gritton 			 */
3009c861373bSJamie Gritton 			if (refcount_load(&pr->pr_uref) > 0)
3010c861373bSJamie Gritton 				killpr = pr;
3011589e4c1dSJamie Gritton 			/* Make sure the parent prison doesn't get killed. */
3012589e4c1dSJamie Gritton 			flags &= ~PD_KILL;
3013c861373bSJamie Gritton 		}
301473d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
3015f7496dcaSJamie Gritton 			/* Drop a reference. */
30166754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_ref) > 0,
301773d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
301873d9e52dSJamie Gritton 			     pr->pr_id));
3019f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_ref)) {
3020f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
3021f7496dcaSJamie Gritton 				if (refcount_release(&pr->pr_ref)) {
3022cc5fd8c7SJamie Gritton 					/*
3023f7496dcaSJamie Gritton 					 * When the last reference goes,
3024f7496dcaSJamie Gritton 					 * unlink the prison and set it aside.
3025cc5fd8c7SJamie Gritton 					 */
3026f7496dcaSJamie Gritton 					KASSERT(
3027f7496dcaSJamie Gritton 					    refcount_load(&pr->pr_uref) == 0,
3028f7496dcaSJamie Gritton 					    ("prison_deref: last ref, "
3029f7496dcaSJamie Gritton 					     "but still has %d urefs (jid=%d)",
3030f7496dcaSJamie Gritton 					     pr->pr_uref, pr->pr_id));
3031f7496dcaSJamie Gritton 					KASSERT(
3032f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_ref) != 0,
3033f7496dcaSJamie Gritton 					    ("prison0 pr_ref=0"));
30341158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_INVALID;
3035b38ff370SJamie Gritton 					TAILQ_REMOVE(&allprison, pr, pr_list);
30360304c731SJamie Gritton 					LIST_REMOVE(pr, pr_sibling);
3037f7496dcaSJamie Gritton 					TAILQ_INSERT_TAIL(&freeprison, pr,
3038f7496dcaSJamie Gritton 					    pr_list);
3039f7496dcaSJamie Gritton 					for (ppr = pr->pr_parent;
3040f7496dcaSJamie Gritton 					     ppr != NULL;
3041f7496dcaSJamie Gritton 					     ppr = ppr->pr_parent)
3042f7496dcaSJamie Gritton 						ppr->pr_childcount--;
3043f7496dcaSJamie Gritton 					/*
3044f7496dcaSJamie Gritton 					 * Removing a prison frees references
3045f7496dcaSJamie Gritton 					 * from its parent.
3046f7496dcaSJamie Gritton 					 */
3047f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
3048f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
30496e1d1bfcSJamie Gritton 					pr = pr->pr_parent;
30506e1d1bfcSJamie Gritton 					flags |= PD_DEREF | PD_DEUREF;
3051f7496dcaSJamie Gritton 					continue;
3052f7496dcaSJamie Gritton 				}
3053f7496dcaSJamie Gritton 			}
3054f7496dcaSJamie Gritton 		}
3055f7496dcaSJamie Gritton 		break;
30566e1d1bfcSJamie Gritton 	}
30576e1d1bfcSJamie Gritton 
30586e1d1bfcSJamie Gritton 	/* Release all the prison locks. */
3059f7496dcaSJamie Gritton 	if (flags & PD_LOCKED)
3060f7496dcaSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30616e1d1bfcSJamie Gritton 	if (flags & PD_LIST_SLOCKED)
30626e1d1bfcSJamie Gritton 		sx_sunlock(&allprison_lock);
30636e1d1bfcSJamie Gritton 	else if (flags & PD_LIST_XLOCKED)
30646e1d1bfcSJamie Gritton 		sx_xunlock(&allprison_lock);
30656e1d1bfcSJamie Gritton 
3066c861373bSJamie Gritton 	/* Kill any processes attached to a killed prison. */
3067c861373bSJamie Gritton 	if (killpr != NULL) {
3068c861373bSJamie Gritton 		sx_slock(&allproc_lock);
3069c861373bSJamie Gritton 		FOREACH_PROC_IN_SYSTEM(p) {
3070c861373bSJamie Gritton 			PROC_LOCK(p);
3071c861373bSJamie Gritton 			if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
3072c861373bSJamie Gritton 				for (ppr = p->p_ucred->cr_prison;
3073c861373bSJamie Gritton 				     ppr != &prison0;
3074c861373bSJamie Gritton 				     ppr = ppr->pr_parent)
3075c861373bSJamie Gritton 					if (ppr == killpr) {
3076c861373bSJamie Gritton 						kern_psignal(p, SIGKILL);
3077c861373bSJamie Gritton 						break;
3078c861373bSJamie Gritton 					}
3079c861373bSJamie Gritton 			}
3080c861373bSJamie Gritton 			PROC_UNLOCK(p);
3081c861373bSJamie Gritton 		}
3082c861373bSJamie Gritton 		sx_sunlock(&allproc_lock);
3083c861373bSJamie Gritton 	}
3084c861373bSJamie Gritton 
30856e1d1bfcSJamie Gritton 	/*
30866e1d1bfcSJamie Gritton 	 * Finish removing any unreferenced prisons, which couldn't happen
30876e1d1bfcSJamie Gritton 	 * while allprison_lock was held (to avoid a LOR on vrele).
30886e1d1bfcSJamie Gritton 	 */
30896e1d1bfcSJamie Gritton 	TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
30906e1d1bfcSJamie Gritton #ifdef VIMAGE
30916e1d1bfcSJamie Gritton 		if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
30926e1d1bfcSJamie Gritton 			vnet_destroy(rpr->pr_vnet);
30936e1d1bfcSJamie Gritton #endif
30946e1d1bfcSJamie Gritton 		if (rpr->pr_root != NULL)
30956e1d1bfcSJamie Gritton 			vrele(rpr->pr_root);
30966e1d1bfcSJamie Gritton 		mtx_destroy(&rpr->pr_mtx);
30976e1d1bfcSJamie Gritton #ifdef INET
3098eb8dcdeaSGleb Smirnoff 		prison_ip_free(rpr->pr_addrs[PR_INET]);
30996e1d1bfcSJamie Gritton #endif
31006e1d1bfcSJamie Gritton #ifdef INET6
3101eb8dcdeaSGleb Smirnoff 		prison_ip_free(rpr->pr_addrs[PR_INET6]);
31026e1d1bfcSJamie Gritton #endif
31036e1d1bfcSJamie Gritton 		if (rpr->pr_cpuset != NULL)
31046e1d1bfcSJamie Gritton 			cpuset_rel(rpr->pr_cpuset);
31056e1d1bfcSJamie Gritton 		osd_jail_exit(rpr);
31066e1d1bfcSJamie Gritton #ifdef RACCT
31076e1d1bfcSJamie Gritton 		if (racct_enable)
31086e1d1bfcSJamie Gritton 			prison_racct_detach(rpr);
31096e1d1bfcSJamie Gritton #endif
3110f7496dcaSJamie Gritton 		TAILQ_REMOVE(&freeprison, rpr, pr_list);
31116e1d1bfcSJamie Gritton 		free(rpr, M_PRISON);
31120304c731SJamie Gritton 	}
3113b3059e09SRobert Watson }
3114b3059e09SRobert Watson 
3115413628a7SBjoern A. Zeeb /*
3116c861373bSJamie Gritton  * Kill the prison and its descendants.  Mark them as dying, clear the
3117c861373bSJamie Gritton  * persist flag, and call module remove methods.
3118c861373bSJamie Gritton  */
3119c861373bSJamie Gritton static void
3120c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3121c861373bSJamie Gritton {
3122c861373bSJamie Gritton 	struct prison *cpr, *ppr, *rpr;
3123c861373bSJamie Gritton 	bool descend;
3124c861373bSJamie Gritton 
3125c861373bSJamie Gritton 	/*
3126c861373bSJamie Gritton 	 * Unlike the descendants, the target prison can be killed
3127c861373bSJamie Gritton 	 * even if it is currently dying.  This is useful for failed
3128c861373bSJamie Gritton 	 * creation in jail_set(2).
3129c861373bSJamie Gritton 	 */
3130c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
3131c861373bSJamie Gritton 	    ("Trying to kill dead prison %p (jid=%d).",
3132c861373bSJamie Gritton 	     pr, pr->pr_id));
3133c861373bSJamie Gritton 	refcount_acquire(&pr->pr_uref);
3134c861373bSJamie Gritton 	pr->pr_state = PRISON_STATE_DYING;
3135c861373bSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3136c861373bSJamie Gritton 
3137c861373bSJamie Gritton 	rpr = NULL;
3138c861373bSJamie Gritton 	FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3139c861373bSJamie Gritton 		if (descend) {
3140c861373bSJamie Gritton 			if (!prison_isalive(cpr)) {
3141c861373bSJamie Gritton 				descend = false;
3142c861373bSJamie Gritton 				continue;
3143c861373bSJamie Gritton 			}
3144c861373bSJamie Gritton 			prison_hold(cpr);
3145c861373bSJamie Gritton 			prison_proc_hold(cpr);
3146c861373bSJamie Gritton 			mtx_lock(&cpr->pr_mtx);
3147c861373bSJamie Gritton 			cpr->pr_state = PRISON_STATE_DYING;
3148c861373bSJamie Gritton 			cpr->pr_flags |= PR_REMOVE;
3149c861373bSJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3150c861373bSJamie Gritton 			continue;
3151c861373bSJamie Gritton 		}
3152c861373bSJamie Gritton 		if (!(cpr->pr_flags & PR_REMOVE))
3153c861373bSJamie Gritton 			continue;
3154a9f7455cSJamie Gritton 		prison_cleanup(cpr);
3155c861373bSJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3156c861373bSJamie Gritton 		cpr->pr_flags &= ~PR_REMOVE;
3157c861373bSJamie Gritton 		if (cpr->pr_flags & PR_PERSIST) {
3158c861373bSJamie Gritton 			cpr->pr_flags &= ~PR_PERSIST;
3159c861373bSJamie Gritton 			prison_proc_free_not_last(cpr);
3160c861373bSJamie Gritton 			prison_free_not_last(cpr);
3161c861373bSJamie Gritton 		}
3162c861373bSJamie Gritton 		(void)refcount_release(&cpr->pr_uref);
3163c861373bSJamie Gritton 		if (refcount_release(&cpr->pr_ref)) {
3164c861373bSJamie Gritton 			/*
3165c861373bSJamie Gritton 			 * When the last reference goes, unlink the prison
3166c861373bSJamie Gritton 			 * and set it aside for prison_deref() to handle.
3167c861373bSJamie Gritton 			 * Delay unlinking the sibling list to keep the loop
3168c861373bSJamie Gritton 			 * safe.
3169c861373bSJamie Gritton 			 */
3170c861373bSJamie Gritton 			if (rpr != NULL)
3171c861373bSJamie Gritton 				LIST_REMOVE(rpr, pr_sibling);
3172c861373bSJamie Gritton 			rpr = cpr;
3173c861373bSJamie Gritton 			rpr->pr_state = PRISON_STATE_INVALID;
3174c861373bSJamie Gritton 			TAILQ_REMOVE(&allprison, rpr, pr_list);
3175c861373bSJamie Gritton 			TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3176c861373bSJamie Gritton 			/*
3177c861373bSJamie Gritton 			 * Removing a prison frees references from its parent.
3178c861373bSJamie Gritton 			 */
3179c861373bSJamie Gritton 			ppr = rpr->pr_parent;
3180c861373bSJamie Gritton 			prison_proc_free_not_last(ppr);
3181c861373bSJamie Gritton 			prison_free_not_last(ppr);
3182c861373bSJamie Gritton 			for (; ppr != NULL; ppr = ppr->pr_parent)
3183c861373bSJamie Gritton 				ppr->pr_childcount--;
3184c861373bSJamie Gritton 		}
3185c861373bSJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3186c861373bSJamie Gritton 	}
3187c861373bSJamie Gritton 	if (rpr != NULL)
3188c861373bSJamie Gritton 		LIST_REMOVE(rpr, pr_sibling);
3189c861373bSJamie Gritton 
3190a9f7455cSJamie Gritton 	prison_cleanup(pr);
3191c861373bSJamie Gritton 	mtx_lock(&pr->pr_mtx);
3192c861373bSJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
3193c861373bSJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
3194c861373bSJamie Gritton 		prison_proc_free_not_last(pr);
3195c861373bSJamie Gritton 		prison_free_not_last(pr);
3196c861373bSJamie Gritton 	}
3197c861373bSJamie Gritton 	(void)refcount_release(&pr->pr_uref);
3198c861373bSJamie Gritton }
3199c861373bSJamie Gritton 
3200c861373bSJamie Gritton /*
3201f7496dcaSJamie Gritton  * Given the current locking state in the flags, make sure allprison_lock
3202f7496dcaSJamie Gritton  * is held exclusive, and the prison is locked.  Return flags indicating
3203f7496dcaSJamie Gritton  * the new state.
3204f7496dcaSJamie Gritton  */
3205f7496dcaSJamie Gritton static int
3206f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags)
3207f7496dcaSJamie Gritton {
3208f7496dcaSJamie Gritton 
3209f7496dcaSJamie Gritton 	if (!(flags & PD_LIST_XLOCKED)) {
3210f7496dcaSJamie Gritton 		/*
3211f7496dcaSJamie Gritton 		 * Get allprison_lock, which may be an upgrade,
3212f7496dcaSJamie Gritton 		 * and may require unlocking the prison.
3213f7496dcaSJamie Gritton 		 */
3214f7496dcaSJamie Gritton 		if (flags & PD_LOCKED) {
3215f7496dcaSJamie Gritton 			mtx_unlock(&pr->pr_mtx);
3216f7496dcaSJamie Gritton 			flags &= ~PD_LOCKED;
3217f7496dcaSJamie Gritton 		}
3218f7496dcaSJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
3219f7496dcaSJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
3220f7496dcaSJamie Gritton 				sx_sunlock(&allprison_lock);
3221f7496dcaSJamie Gritton 				sx_xlock(&allprison_lock);
3222f7496dcaSJamie Gritton 			}
3223f7496dcaSJamie Gritton 			flags &= ~PD_LIST_SLOCKED;
3224f7496dcaSJamie Gritton 		} else
3225f7496dcaSJamie Gritton 			sx_xlock(&allprison_lock);
3226f7496dcaSJamie Gritton 		flags |= PD_LIST_XLOCKED;
3227f7496dcaSJamie Gritton 	}
3228f7496dcaSJamie Gritton 	if (!(flags & PD_LOCKED)) {
3229f7496dcaSJamie Gritton 		/* Lock the prison mutex. */
3230f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
3231f7496dcaSJamie Gritton 		flags |= PD_LOCKED;
3232f7496dcaSJamie Gritton 	}
3233f7496dcaSJamie Gritton 	return flags;
3234f7496dcaSJamie Gritton }
3235f7496dcaSJamie Gritton 
3236f7496dcaSJamie Gritton /*
3237a9f7455cSJamie Gritton  * Release a prison's resources when it starts dying (when the last user
3238a9f7455cSJamie Gritton  * reference is dropped, or when it is killed).
3239a9f7455cSJamie Gritton  */
3240a9f7455cSJamie Gritton static void
3241a9f7455cSJamie Gritton prison_cleanup(struct prison *pr)
3242a9f7455cSJamie Gritton {
3243a9f7455cSJamie Gritton 	sx_assert(&allprison_lock, SA_XLOCKED);
3244a9f7455cSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
3245*7060da62SJamie Gritton 	shm_remove_prison(pr);
3246a9f7455cSJamie Gritton 	(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3247a9f7455cSJamie Gritton }
3248a9f7455cSJamie Gritton 
3249a9f7455cSJamie Gritton /*
32500fe74ae6SJamie Gritton  * Set or clear a permission bit in the pr_allow field, passing restrictions
32510fe74ae6SJamie Gritton  * (cleared permission) down to child jails.
32520fe74ae6SJamie Gritton  */
32530fe74ae6SJamie Gritton void
32540fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable)
32550fe74ae6SJamie Gritton {
32560fe74ae6SJamie Gritton 	struct prison *pr;
32570fe74ae6SJamie Gritton 
32580fe74ae6SJamie Gritton 	pr = cred->cr_prison;
32590fe74ae6SJamie Gritton 	sx_slock(&allprison_lock);
32600fe74ae6SJamie Gritton 	mtx_lock(&pr->pr_mtx);
32610fe74ae6SJamie Gritton 	prison_set_allow_locked(pr, flag, enable);
32620fe74ae6SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
32630fe74ae6SJamie Gritton 	sx_sunlock(&allprison_lock);
32640fe74ae6SJamie Gritton }
32650fe74ae6SJamie Gritton 
32660fe74ae6SJamie Gritton static void
32670fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
32680fe74ae6SJamie Gritton {
32690fe74ae6SJamie Gritton 	struct prison *cpr;
32700fe74ae6SJamie Gritton 	int descend;
32710fe74ae6SJamie Gritton 
32720fe74ae6SJamie Gritton 	if (enable != 0)
32730fe74ae6SJamie Gritton 		pr->pr_allow |= flag;
32740fe74ae6SJamie Gritton 	else {
32750fe74ae6SJamie Gritton 		pr->pr_allow &= ~flag;
32760fe74ae6SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
32770fe74ae6SJamie Gritton 			cpr->pr_allow &= ~flag;
32780fe74ae6SJamie Gritton 	}
32790fe74ae6SJamie Gritton }
32800fe74ae6SJamie Gritton 
32810fe74ae6SJamie Gritton /*
3282ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3283ca04ba64SJamie Gritton  *
3284ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3285ca04ba64SJamie Gritton  * if not.
3286ca04ba64SJamie Gritton  */
3287ca04ba64SJamie Gritton int
3288ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3289ca04ba64SJamie Gritton {
32900304c731SJamie Gritton 	struct prison *pr;
3291ca04ba64SJamie Gritton 	int error;
3292ca04ba64SJamie Gritton 
3293ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3294ca04ba64SJamie Gritton 
32950304c731SJamie Gritton 	pr = cred->cr_prison;
3296499650a0SJamie Gritton #ifdef VIMAGE
32976bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3298de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
32996bb79563SJamie Gritton 		return (0);
3300499650a0SJamie Gritton #endif
33016bb79563SJamie Gritton 
3302ca04ba64SJamie Gritton 	error = 0;
3303ca04ba64SJamie Gritton 	switch (af)
3304ca04ba64SJamie Gritton 	{
3305ca04ba64SJamie Gritton #ifdef INET
3306ca04ba64SJamie Gritton 	case AF_INET:
33070304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
33080304c731SJamie Gritton 		{
33090304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
3310eb8dcdeaSGleb Smirnoff 			if ((pr->pr_flags & PR_IP4) &&
3311eb8dcdeaSGleb Smirnoff 			    pr->pr_addrs[PR_INET] == NULL)
3312ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33130304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33140304c731SJamie Gritton 		}
3315ca04ba64SJamie Gritton 		break;
3316ca04ba64SJamie Gritton #endif
3317ca04ba64SJamie Gritton #ifdef INET6
3318ca04ba64SJamie Gritton 	case AF_INET6:
33190304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
33200304c731SJamie Gritton 		{
33210304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
3322eb8dcdeaSGleb Smirnoff 			if ((pr->pr_flags & PR_IP6) &&
3323eb8dcdeaSGleb Smirnoff 			    pr->pr_addrs[PR_INET6] == NULL)
3324ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33250304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33260304c731SJamie Gritton 		}
3327ca04ba64SJamie Gritton 		break;
3328ca04ba64SJamie Gritton #endif
3329ca04ba64SJamie Gritton 	case AF_LOCAL:
3330ca04ba64SJamie Gritton 	case AF_ROUTE:
3331ca04ba64SJamie Gritton 		break;
3332ca04ba64SJamie Gritton 	default:
33330304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3334ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3335ca04ba64SJamie Gritton 	}
3336ca04ba64SJamie Gritton 	return (error);
3337ca04ba64SJamie Gritton }
3338ca04ba64SJamie Gritton 
3339ca04ba64SJamie Gritton /*
3340413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3341413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3342413628a7SBjoern A. Zeeb  *
33430304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
33440304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
33450304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3346413628a7SBjoern A. Zeeb  */
3347413628a7SBjoern A. Zeeb int
3348c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa)
334975c13541SPoul-Henning Kamp {
3350413628a7SBjoern A. Zeeb #ifdef INET
3351c83dda36SAlexander V. Chernikov 	const struct sockaddr_in *sai;
3352413628a7SBjoern A. Zeeb #endif
3353413628a7SBjoern A. Zeeb #ifdef INET6
3354c83dda36SAlexander V. Chernikov 	const struct sockaddr_in6 *sai6;
3355413628a7SBjoern A. Zeeb #endif
3356b89e82ddSJamie Gritton 	int error;
335775c13541SPoul-Henning Kamp 
3358413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3359413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3360413628a7SBjoern A. Zeeb 
3361de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3362de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3363de0bd6f7SBjoern A. Zeeb 		return (0);
3364de0bd6f7SBjoern A. Zeeb #endif
3365de0bd6f7SBjoern A. Zeeb 
3366b89e82ddSJamie Gritton 	error = 0;
3367413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3368413628a7SBjoern A. Zeeb 	{
3369413628a7SBjoern A. Zeeb #ifdef INET
3370413628a7SBjoern A. Zeeb 	case AF_INET:
3371c83dda36SAlexander V. Chernikov 		sai = (const struct sockaddr_in *)sa;
3372b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3373413628a7SBjoern A. Zeeb 		break;
3374413628a7SBjoern A. Zeeb #endif
3375413628a7SBjoern A. Zeeb #ifdef INET6
3376413628a7SBjoern A. Zeeb 	case AF_INET6:
3377c83dda36SAlexander V. Chernikov 		sai6 = (const struct sockaddr_in6 *)sa;
3378b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3379413628a7SBjoern A. Zeeb 		break;
3380413628a7SBjoern A. Zeeb #endif
3381413628a7SBjoern A. Zeeb 	default:
33820304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3383b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3384413628a7SBjoern A. Zeeb 	}
3385b89e82ddSJamie Gritton 	return (error);
338675c13541SPoul-Henning Kamp }
338791421ba2SRobert Watson 
338891421ba2SRobert Watson /*
338991421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
339091421ba2SRobert Watson  */
339191421ba2SRobert Watson int
33929ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
339391421ba2SRobert Watson {
339491421ba2SRobert Watson 
33950304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
33960304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
33970304c731SJamie Gritton }
339891421ba2SRobert Watson 
33990304c731SJamie Gritton /*
34000304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
34010304c731SJamie Gritton  */
34020304c731SJamie Gritton int
34030304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
34040304c731SJamie Gritton {
34050304c731SJamie Gritton 
34060304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
34070304c731SJamie Gritton 		if (pr1 == pr2)
34080304c731SJamie Gritton 			return (1);
340991421ba2SRobert Watson 	return (0);
341091421ba2SRobert Watson }
341191421ba2SRobert Watson 
341291421ba2SRobert Watson /*
3413f7496dcaSJamie Gritton  * Return true if the prison is currently alive.  A prison is alive if it
3414f7496dcaSJamie Gritton  * holds user references and it isn't being removed.
341576ad42abSJamie Gritton  */
341676ad42abSJamie Gritton bool
3417eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr)
341876ad42abSJamie Gritton {
341976ad42abSJamie Gritton 
34201158508aSJamie Gritton 	if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3421cc7b7306SJamie Gritton 		return (false);
342276ad42abSJamie Gritton 	return (true);
342376ad42abSJamie Gritton }
342476ad42abSJamie Gritton 
342576ad42abSJamie Gritton /*
342676ad42abSJamie Gritton  * Return true if the prison is currently valid.  A prison is valid if it has
342776ad42abSJamie Gritton  * been fully created, and is not being destroyed.  Note that dying prisons
3428f7496dcaSJamie Gritton  * are still considered valid.  Invalid prisons won't be found under normal
3429f7496dcaSJamie Gritton  * circumstances, as they're only put in that state by functions that have
3430f7496dcaSJamie Gritton  * an exclusive hold on allprison_lock.
343176ad42abSJamie Gritton  */
343276ad42abSJamie Gritton bool
343376ad42abSJamie Gritton prison_isvalid(struct prison *pr)
343476ad42abSJamie Gritton {
343576ad42abSJamie Gritton 
34361158508aSJamie Gritton 	if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
34371158508aSJamie Gritton 		return (false);
34386754ae25SJamie Gritton 	if (__predict_false(refcount_load(&pr->pr_ref) == 0))
343976ad42abSJamie Gritton 		return (false);
344076ad42abSJamie Gritton 	return (true);
344176ad42abSJamie Gritton }
344276ad42abSJamie Gritton 
344376ad42abSJamie Gritton /*
3444de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3445de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3446de0bd6f7SBjoern A. Zeeb  */
3447de0bd6f7SBjoern A. Zeeb int
3448de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3449de0bd6f7SBjoern A. Zeeb {
3450de0bd6f7SBjoern A. Zeeb 
3451de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3452de0bd6f7SBjoern A. Zeeb 		return (0);
3453de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3454de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3455de0bd6f7SBjoern A. Zeeb 		return (0);
3456de0bd6f7SBjoern A. Zeeb #endif
3457de0bd6f7SBjoern A. Zeeb 
3458de0bd6f7SBjoern A. Zeeb 	return (1);
3459de0bd6f7SBjoern A. Zeeb }
3460de0bd6f7SBjoern A. Zeeb 
3461de0bd6f7SBjoern A. Zeeb /*
34627455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
34639484d0c0SRobert Drehmel  */
3464ad1ff099SRobert Drehmel void
34659ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
34669484d0c0SRobert Drehmel {
346776ca6f88SJamie Gritton 	struct prison *pr;
34689484d0c0SRobert Drehmel 
34697455b100SJamie Gritton 	/*
34707455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
34717455b100SJamie Gritton 	 * system's hostname.
34727455b100SJamie Gritton 	 */
347376ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
347476ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3475c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
347676ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
34779484d0c0SRobert Drehmel }
3478fd7a8150SMike Barcroft 
34797455b100SJamie Gritton void
34807455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
34817455b100SJamie Gritton {
34827455b100SJamie Gritton 
34837455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3484c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
34857455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
34867455b100SJamie Gritton }
34877455b100SJamie Gritton 
34887455b100SJamie Gritton void
34897455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
34907455b100SJamie Gritton {
34917455b100SJamie Gritton 
34927455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3493c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
34947455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
34957455b100SJamie Gritton }
34967455b100SJamie Gritton 
34977455b100SJamie Gritton void
34987455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
34997455b100SJamie Gritton {
35007455b100SJamie Gritton 
35017455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
35027455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
35037455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35047455b100SJamie Gritton }
35057455b100SJamie Gritton 
35063f8bc99cSKristof Provost void
35073f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len)
35083f8bc99cSKristof Provost {
35093f8bc99cSKristof Provost 
35103f8bc99cSKristof Provost 	mtx_lock(&cred->cr_prison->pr_mtx);
35113f8bc99cSKristof Provost 	strlcpy(name, cred->cr_prison->pr_name, len);
35123f8bc99cSKristof Provost 	mtx_unlock(&cred->cr_prison->pr_mtx);
35133f8bc99cSKristof Provost }
35143f8bc99cSKristof Provost 
3515eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3516eb79e1c7SBjoern A. Zeeb /*
3517eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3518eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3519eb79e1c7SBjoern A. Zeeb  *
3520eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3521eb79e1c7SBjoern A. Zeeb  */
3522eb79e1c7SBjoern A. Zeeb int
3523eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3524eb79e1c7SBjoern A. Zeeb {
3525eb79e1c7SBjoern A. Zeeb 
3526eb79e1c7SBjoern A. Zeeb 	/*
3527eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3528eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3529eb79e1c7SBjoern A. Zeeb 	 */
3530eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3531eb79e1c7SBjoern A. Zeeb }
3532eb79e1c7SBjoern A. Zeeb #endif
3533eb79e1c7SBjoern A. Zeeb 
3534f08df373SRobert Watson /*
3535820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3536820a0de9SPawel Jakub Dawidek  * status of a mount point.
3537820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3538820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3539820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3540f08df373SRobert Watson  */
3541f08df373SRobert Watson int
3542820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3543f08df373SRobert Watson {
3544820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3545820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3546820a0de9SPawel Jakub Dawidek 	size_t len;
3547f08df373SRobert Watson 
3548820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
35490304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
35500304c731SJamie Gritton 		return (0);
3551820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3552820a0de9SPawel Jakub Dawidek 		return (0);
35530304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3554820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3555820a0de9SPawel Jakub Dawidek 	/*
3556820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3557820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3558820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3559820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3560820a0de9SPawel Jakub Dawidek 	 */
3561820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3562820a0de9SPawel Jakub Dawidek 		return (0);
3563820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3564820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3565820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3566820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3567820a0de9SPawel Jakub Dawidek 	/*
3568820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3569820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3570820a0de9SPawel Jakub Dawidek 	 */
3571820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3572820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3573f08df373SRobert Watson 	return (0);
3574f08df373SRobert Watson }
3575820a0de9SPawel Jakub Dawidek 
3576820a0de9SPawel Jakub Dawidek void
3577820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3578820a0de9SPawel Jakub Dawidek {
3579820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3580820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3581820a0de9SPawel Jakub Dawidek 	size_t len;
3582820a0de9SPawel Jakub Dawidek 
3583820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
35840304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
35850304c731SJamie Gritton 		return;
3586820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3587820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3588820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3589820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3590820a0de9SPawel Jakub Dawidek 		return;
3591820a0de9SPawel Jakub Dawidek 	}
3592820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3593820a0de9SPawel Jakub Dawidek 		/*
3594820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3595820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3596820a0de9SPawel Jakub Dawidek 		 */
3597820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3598820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3599820a0de9SPawel Jakub Dawidek 		return;
3600820a0de9SPawel Jakub Dawidek 	}
3601820a0de9SPawel Jakub Dawidek 	/*
3602820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3603820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3604820a0de9SPawel Jakub Dawidek 	 */
3605820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3606820a0de9SPawel Jakub Dawidek 		return;
3607820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3608820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3609820a0de9SPawel Jakub Dawidek 	/*
3610820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3611820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3612820a0de9SPawel Jakub Dawidek 	 */
3613820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3614820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3615820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3616820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3617820a0de9SPawel Jakub Dawidek 	} else {
3618820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3619820a0de9SPawel Jakub Dawidek 	}
3620f08df373SRobert Watson }
3621f08df373SRobert Watson 
3622800c9408SRobert Watson /*
3623800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3624800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3625800c9408SRobert Watson  */
3626800c9408SRobert Watson int
3627800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3628800c9408SRobert Watson {
36290fe74ae6SJamie Gritton 	struct prison *pr;
36300fe74ae6SJamie Gritton 	int error;
3631800c9408SRobert Watson 
36327b2ff0dcSMateusz Guzik 	/*
36337b2ff0dcSMateusz Guzik 	 * Some policies have custom handlers. This routine should not be
36347b2ff0dcSMateusz Guzik 	 * called for them. See priv_check_cred().
36357b2ff0dcSMateusz Guzik 	 */
36367b2ff0dcSMateusz Guzik 	switch (priv) {
3637a459a6cfSMateusz Guzik 	case PRIV_VFS_LOOKUP:
36387b2ff0dcSMateusz Guzik 	case PRIV_VFS_GENERATION:
36397b2ff0dcSMateusz Guzik 		KASSERT(0, ("prison_priv_check instead of a custom handler "
36407b2ff0dcSMateusz Guzik 		    "called for %d\n", priv));
36417b2ff0dcSMateusz Guzik 	}
36427b2ff0dcSMateusz Guzik 
3643800c9408SRobert Watson 	if (!jailed(cred))
3644800c9408SRobert Watson 		return (0);
3645800c9408SRobert Watson 
36466bb79563SJamie Gritton #ifdef VIMAGE
36476bb79563SJamie Gritton 	/*
36486bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
36496bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
36506bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
36516bb79563SJamie Gritton 	 */
36526bb79563SJamie Gritton 	switch (priv) {
36536bb79563SJamie Gritton #ifdef notyet
36546bb79563SJamie Gritton 		/*
36556bb79563SJamie Gritton 		 * NFS-specific privileges.
36566bb79563SJamie Gritton 		 */
36576bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
36586bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
36596bb79563SJamie Gritton #endif
36606bb79563SJamie Gritton 		/*
36616bb79563SJamie Gritton 		 * Network stack privileges.
36626bb79563SJamie Gritton 		 */
36636bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
36646bb79563SJamie Gritton 	case PRIV_NET_GRE:
36656bb79563SJamie Gritton 	case PRIV_NET_BPF:
36666bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
36676bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
36686bb79563SJamie Gritton 	case PRIV_NET_TAP:
36696bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
36706bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
36716bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3672215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
36736bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
36746bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
36756bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
36766bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
3677389474c1SKonstantin Belousov 	case PRIV_NET_SETLANPCP:
36786bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
36796bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
36806bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
36816bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
36826bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
36836bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
36846bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
36856bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
36866bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
36876bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
36886bb79563SJamie Gritton 	case PRIV_NET_LAGG:
36896bb79563SJamie Gritton 	case PRIV_NET_GIF:
36906bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
369135fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
3692ab91feabSKristof Provost 	case PRIV_NET_OVPN:
36936bb79563SJamie Gritton 
36946bb79563SJamie Gritton 		/*
36956bb79563SJamie Gritton 		 * 802.11-related privileges.
36966bb79563SJamie Gritton 		 */
3697f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_GETKEY:
3698f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_MANAGE:
36996bb79563SJamie Gritton 
37006bb79563SJamie Gritton #ifdef notyet
37016bb79563SJamie Gritton 		/*
37026bb79563SJamie Gritton 		 * ATM privileges.
37036bb79563SJamie Gritton 		 */
37046bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
37056bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
37066bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
37076bb79563SJamie Gritton 	case PRIV_NETATM_SET:
37086bb79563SJamie Gritton 
37096bb79563SJamie Gritton 		/*
37106bb79563SJamie Gritton 		 * Bluetooth privileges.
37116bb79563SJamie Gritton 		 */
37126bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
37136bb79563SJamie Gritton #endif
37146bb79563SJamie Gritton 
37156bb79563SJamie Gritton 		/*
37166bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
37176bb79563SJamie Gritton 		 */
37186bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
37196bb79563SJamie Gritton #ifdef notyet
37206bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
37216bb79563SJamie Gritton #endif
37226bb79563SJamie Gritton 
37236bb79563SJamie Gritton 		/*
37246bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
37256bb79563SJamie Gritton 		 */
37266bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
37276bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
37286bb79563SJamie Gritton 	case PRIV_NETINET_PF:
37296bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
37306bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
37316bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
37326bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
37336bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
37346bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
37356bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
37366bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
37376bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
37386bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
37396bb79563SJamie Gritton 
37406bb79563SJamie Gritton #ifdef notyet
37416bb79563SJamie Gritton 		/*
37426bb79563SJamie Gritton 		 * NCP privileges.
37436bb79563SJamie Gritton 		 */
37446bb79563SJamie Gritton 	case PRIV_NETNCP:
37456bb79563SJamie Gritton 
37466bb79563SJamie Gritton 		/*
37476bb79563SJamie Gritton 		 * SMB privileges.
37486bb79563SJamie Gritton 		 */
37496bb79563SJamie Gritton 	case PRIV_NETSMB:
37506bb79563SJamie Gritton #endif
37516bb79563SJamie Gritton 
37526bb79563SJamie Gritton 	/*
37536bb79563SJamie Gritton 	 * No default: or deny here.
37546bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
37556bb79563SJamie Gritton 	 */
37566bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
37576bb79563SJamie Gritton 			return (0);
37586bb79563SJamie Gritton 	}
37596bb79563SJamie Gritton #endif /* VIMAGE */
37606bb79563SJamie Gritton 
3761800c9408SRobert Watson 	switch (priv) {
3762800c9408SRobert Watson 		/*
3763800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3764800c9408SRobert Watson 		 */
3765800c9408SRobert Watson 	case PRIV_KTRACE:
3766800c9408SRobert Watson 
3767c3c1b5e6SRobert Watson #if 0
3768800c9408SRobert Watson 		/*
3769800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3770800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3771800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3772800c9408SRobert Watson 		 * jail.
3773800c9408SRobert Watson 		 */
3774800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3775800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3776800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3777c3c1b5e6SRobert Watson #endif
3778800c9408SRobert Watson 
3779800c9408SRobert Watson 		/*
3780800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3781800c9408SRobert Watson 		 * credentials in any way they see fit.
3782800c9408SRobert Watson 		 */
3783800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3784800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3785800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3786800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3787800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3788800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3789800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3790800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3791800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3792800c9408SRobert Watson 
3793800c9408SRobert Watson 		/*
3794800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3795800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3796800c9408SRobert Watson 		 */
3797800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3798800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3799800c9408SRobert Watson 
3800800c9408SRobert Watson 		/*
3801800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3802800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3803800c9408SRobert Watson 		 */
3804800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3805800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3806800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3807800c9408SRobert Watson 
3808800c9408SRobert Watson 		/*
3809800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3810800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3811800c9408SRobert Watson 		 */
3812800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3813800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3814800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3815800c9408SRobert Watson 
3816800c9408SRobert Watson 		/*
3817800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3818800c9408SRobert Watson 		 */
3819800c9408SRobert Watson 	case PRIV_IPC_READ:
3820800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3821800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3822800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3823800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3824800c9408SRobert Watson 
3825800c9408SRobert Watson 		/*
38260304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
38270304c731SJamie Gritton 		 */
38280304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
38290304c731SJamie Gritton 	case PRIV_JAIL_SET:
38300304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
38310304c731SJamie Gritton 
38320304c731SJamie Gritton 		/*
3833800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3834800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3835800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3836800c9408SRobert Watson 		 */
3837800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3838413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3839800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3840800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3841800c9408SRobert Watson 
3842800c9408SRobert Watson 		/*
3843800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3844800c9408SRobert Watson 		 * writable.
3845800c9408SRobert Watson 		 */
3846800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3847800c9408SRobert Watson 
3848800c9408SRobert Watson 		/*
3849800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3850e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3851e82d0201SRobert Watson 		 * configuration option.
3852800c9408SRobert Watson 		 */
385395b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
385495b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3855800c9408SRobert Watson 
3856800c9408SRobert Watson 		/*
3857800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3858800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3859800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3860800c9408SRobert Watson 		 * privileges.
3861800c9408SRobert Watson 		 */
3862800c9408SRobert Watson 	case PRIV_VFS_READ:
3863800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3864800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3865800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3866800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3867800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3868800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3869800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3870bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3871800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3872800c9408SRobert Watson 	case PRIV_VFS_LINK:
3873800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3874e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3875800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3876bb56d716SJamie Gritton 
3877bb56d716SJamie Gritton 		/*
3878bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
387970de1003SGordon Bergling 		 * able to read kernel/physical memory (provided /dev/[k]mem
3880bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3881bb56d716SJamie Gritton 		 */
3882bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3883800c9408SRobert Watson 		return (0);
3884800c9408SRobert Watson 
3885800c9408SRobert Watson 		/*
3886800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3887800c9408SRobert Watson 		 * setting system flags.
3888800c9408SRobert Watson 		 */
3889800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
38900304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3891800c9408SRobert Watson 			return (0);
3892800c9408SRobert Watson 		else
3893800c9408SRobert Watson 			return (EPERM);
3894800c9408SRobert Watson 
3895800c9408SRobert Watson 		/*
3896f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3897f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3898f3a8d2f9SPawel Jakub Dawidek 		 */
3899f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3900f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3901f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
390224b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
39030fe74ae6SJamie Gritton 		pr = cred->cr_prison;
39040fe74ae6SJamie Gritton 		prison_lock(pr);
39050fe74ae6SJamie Gritton 		if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
39060fe74ae6SJamie Gritton 			error = 0;
3907f3a8d2f9SPawel Jakub Dawidek 		else
39080fe74ae6SJamie Gritton 			error = EPERM;
39090fe74ae6SJamie Gritton 		prison_unlock(pr);
39100fe74ae6SJamie Gritton 		return (error);
3911f3a8d2f9SPawel Jakub Dawidek 
3912f3a8d2f9SPawel Jakub Dawidek 		/*
391363619b6dSKyle Evans 		 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
391463619b6dSKyle Evans 		 * policy.  priv_check_cred will not specifically allow it, and
391563619b6dSKyle Evans 		 * we may want a MAC policy to allow it.
391663619b6dSKyle Evans 		 */
391763619b6dSKyle Evans 	case PRIV_VFS_READ_DIR:
391863619b6dSKyle Evans 		return (0);
391963619b6dSKyle Evans 
392063619b6dSKyle Evans 		/*
3921ccd6ac9fSAntoine Brodin 		 * Conditionnaly allow locking (unlocking) physical pages
3922ccd6ac9fSAntoine Brodin 		 * in memory.
3923ccd6ac9fSAntoine Brodin 		 */
3924ccd6ac9fSAntoine Brodin 	case PRIV_VM_MLOCK:
3925ccd6ac9fSAntoine Brodin 	case PRIV_VM_MUNLOCK:
3926ccd6ac9fSAntoine Brodin 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3927ccd6ac9fSAntoine Brodin 			return (0);
3928ccd6ac9fSAntoine Brodin 		else
3929ccd6ac9fSAntoine Brodin 			return (EPERM);
3930ccd6ac9fSAntoine Brodin 
3931ccd6ac9fSAntoine Brodin 		/*
3932e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
3933800c9408SRobert Watson 		 */
3934800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
3935e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3936e28f9b7dSAllan Jude 			return (0);
3937e28f9b7dSAllan Jude 		else
3938e28f9b7dSAllan Jude 			return (EPERM);
3939e28f9b7dSAllan Jude 
3940e28f9b7dSAllan Jude 		/*
3941e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
3942e28f9b7dSAllan Jude 		 */
39434b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3944800c9408SRobert Watson 		return (0);
3945800c9408SRobert Watson 
3946800c9408SRobert Watson 		/*
3947e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
394879ba3952SBjoern A. Zeeb 		 */
394979ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
395079ba3952SBjoern A. Zeeb 		return (0);
395179ba3952SBjoern A. Zeeb 
395279ba3952SBjoern A. Zeeb 		/*
3953800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3954800c9408SRobert Watson 		 */
3955800c9408SRobert Watson 	case PRIV_NETINET_RAW:
39560304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3957800c9408SRobert Watson 			return (0);
3958800c9408SRobert Watson 		else
3959800c9408SRobert Watson 			return (EPERM);
3960800c9408SRobert Watson 
3961800c9408SRobert Watson 		/*
3962800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3963800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3964800c9408SRobert Watson 		 * jail.
3965800c9408SRobert Watson 		 */
3966800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3967800c9408SRobert Watson 		return (0);
3968800c9408SRobert Watson 
39692bfc50bcSEdward Tomasz Napierala 		/*
39702bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
39712bfc50bcSEdward Tomasz Napierala 		 */
39722bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
39732bfc50bcSEdward Tomasz Napierala 		return (0);
39742bfc50bcSEdward Tomasz Napierala 
3975b19d66fdSJamie Gritton 		/*
39764520f617SJamie Gritton 		 * Do not allow a process inside a jail to read the kernel
3977b19d66fdSJamie Gritton 		 * message buffer unless explicitly permitted.
3978b19d66fdSJamie Gritton 		 */
3979b19d66fdSJamie Gritton 	case PRIV_MSGBUF:
3980b19d66fdSJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3981b19d66fdSJamie Gritton 			return (0);
3982b19d66fdSJamie Gritton 		return (EPERM);
3983b19d66fdSJamie Gritton 
3984800c9408SRobert Watson 	default:
3985800c9408SRobert Watson 		/*
3986800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3987800c9408SRobert Watson 		 * includes almost all network privileges, many system
3988800c9408SRobert Watson 		 * configuration privileges.
3989800c9408SRobert Watson 		 */
3990800c9408SRobert Watson 		return (EPERM);
3991800c9408SRobert Watson 	}
3992800c9408SRobert Watson }
3993800c9408SRobert Watson 
39940304c731SJamie Gritton /*
39950304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
39960304c731SJamie Gritton  * if it does not directly follow.
39970304c731SJamie Gritton  */
39980304c731SJamie Gritton 
39990304c731SJamie Gritton char *
40000304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
40010304c731SJamie Gritton {
40020304c731SJamie Gritton 	char *name;
40030304c731SJamie Gritton 
40040304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
40050304c731SJamie Gritton 	if (pr1 == pr2)
40060304c731SJamie Gritton 		return "0";
40070304c731SJamie Gritton 	name = pr2->pr_name;
40080304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
40090304c731SJamie Gritton 		/*
40100304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
40110304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
40120304c731SJamie Gritton 		 * can be counted on - and counted.
40130304c731SJamie Gritton 		 */
40140304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
40150304c731SJamie Gritton 			name = strchr(name, '.') + 1;
40160304c731SJamie Gritton 	}
40170304c731SJamie Gritton 	return (name);
40180304c731SJamie Gritton }
40190304c731SJamie Gritton 
40200304c731SJamie Gritton /*
40210304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
40220304c731SJamie Gritton  * if it does not directly follow.
40230304c731SJamie Gritton  */
40240304c731SJamie Gritton static char *
40250304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
40260304c731SJamie Gritton {
40270304c731SJamie Gritton 	char *path1, *path2;
40280304c731SJamie Gritton 	int len1;
40290304c731SJamie Gritton 
40300304c731SJamie Gritton 	path1 = pr1->pr_path;
40310304c731SJamie Gritton 	path2 = pr2->pr_path;
40320304c731SJamie Gritton 	if (!strcmp(path1, "/"))
40330304c731SJamie Gritton 		return (path2);
40340304c731SJamie Gritton 	len1 = strlen(path1);
40350304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
40360304c731SJamie Gritton 		return (path2);
40370304c731SJamie Gritton 	if (path2[len1] == '\0')
40380304c731SJamie Gritton 		return "/";
40390304c731SJamie Gritton 	if (path2[len1] == '/')
40400304c731SJamie Gritton 		return (path2 + len1);
40410304c731SJamie Gritton 	return (path2);
40420304c731SJamie Gritton }
40430304c731SJamie Gritton 
40440304c731SJamie Gritton /*
40450304c731SJamie Gritton  * Jail-related sysctls.
40460304c731SJamie Gritton  */
40477029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
40480304c731SJamie Gritton     "Jails");
40490304c731SJamie Gritton 
4050eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
4051eb8dcdeaSGleb Smirnoff /*
4052eb8dcdeaSGleb Smirnoff  * Copy address array to memory that would be then SYSCTL_OUT-ed.
4053eb8dcdeaSGleb Smirnoff  * sysctl_jail_list() helper.
4054eb8dcdeaSGleb Smirnoff  */
4055eb8dcdeaSGleb Smirnoff static void
4056eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4057eb8dcdeaSGleb Smirnoff {
4058eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
4059eb8dcdeaSGleb Smirnoff 
4060eb8dcdeaSGleb Smirnoff  again:
4061eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
4062eb8dcdeaSGleb Smirnoff 	if (pr->pr_addrs[af] != NULL) {
4063eb8dcdeaSGleb Smirnoff 		if (*len < pr->pr_addrs[af]->ips) {
4064eb8dcdeaSGleb Smirnoff 			*len = pr->pr_addrs[af]->ips;
4065eb8dcdeaSGleb Smirnoff 			mtx_unlock(&pr->pr_mtx);
4066eb8dcdeaSGleb Smirnoff 			*out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4067eb8dcdeaSGleb Smirnoff 			mtx_lock(&pr->pr_mtx);
4068eb8dcdeaSGleb Smirnoff 			goto again;
4069eb8dcdeaSGleb Smirnoff 		}
4070eb8dcdeaSGleb Smirnoff 		bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size);
4071eb8dcdeaSGleb Smirnoff 	}
4072eb8dcdeaSGleb Smirnoff }
4073eb8dcdeaSGleb Smirnoff #endif
4074eb8dcdeaSGleb Smirnoff 
4075fd7a8150SMike Barcroft static int
4076fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4077fd7a8150SMike Barcroft {
4078b38ff370SJamie Gritton 	struct xprison *xp;
40790304c731SJamie Gritton 	struct prison *pr, *cpr;
4080b38ff370SJamie Gritton #ifdef INET
4081b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
4082b38ff370SJamie Gritton 	int ip4s = 0;
4083b38ff370SJamie Gritton #endif
4084b38ff370SJamie Gritton #ifdef INET6
40853beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
4086b38ff370SJamie Gritton 	int ip6s = 0;
4087b38ff370SJamie Gritton #endif
40880304c731SJamie Gritton 	int descend, error;
4089fd7a8150SMike Barcroft 
4090b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
40910304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
4092b38ff370SJamie Gritton 	error = 0;
4093dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
40940304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
40950304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
4096413628a7SBjoern A. Zeeb #ifdef INET
4097eb8dcdeaSGleb Smirnoff 		prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4098413628a7SBjoern A. Zeeb #endif
4099413628a7SBjoern A. Zeeb #ifdef INET6
4100eb8dcdeaSGleb Smirnoff 		prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4101b38ff370SJamie Gritton #endif
4102b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
4103fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
41040304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
41051158508aSJamie Gritton 		xp->pr_state = cpr->pr_state;
41060304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4107c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
41080304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4109413628a7SBjoern A. Zeeb #ifdef INET
4110eb8dcdeaSGleb Smirnoff 		xp->pr_ip4s = ip4s;
4111413628a7SBjoern A. Zeeb #endif
4112413628a7SBjoern A. Zeeb #ifdef INET6
4113eb8dcdeaSGleb Smirnoff 		xp->pr_ip6s = ip6s;
4114413628a7SBjoern A. Zeeb #endif
41150304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
4116b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4117b38ff370SJamie Gritton 		if (error)
4118b38ff370SJamie Gritton 			break;
4119413628a7SBjoern A. Zeeb #ifdef INET
4120b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
4121b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
4122b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
4123b38ff370SJamie Gritton 			if (error)
4124b38ff370SJamie Gritton 				break;
4125413628a7SBjoern A. Zeeb 		}
4126413628a7SBjoern A. Zeeb #endif
4127413628a7SBjoern A. Zeeb #ifdef INET6
4128b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
4129b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
4130b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
4131b38ff370SJamie Gritton 			if (error)
4132b38ff370SJamie Gritton 				break;
4133413628a7SBjoern A. Zeeb 		}
4134413628a7SBjoern A. Zeeb #endif
4135fd7a8150SMike Barcroft 	}
4136dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
4137b38ff370SJamie Gritton 	free(xp, M_TEMP);
4138b38ff370SJamie Gritton #ifdef INET
4139b38ff370SJamie Gritton 	free(ip4, M_TEMP);
4140b38ff370SJamie Gritton #endif
4141b38ff370SJamie Gritton #ifdef INET6
4142b38ff370SJamie Gritton 	free(ip6, M_TEMP);
4143b38ff370SJamie Gritton #endif
4144fd7a8150SMike Barcroft 	return (error);
4145fd7a8150SMike Barcroft }
4146fd7a8150SMike Barcroft 
4147f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
4148f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4149f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
4150461167c2SPawel Jakub Dawidek 
4151461167c2SPawel Jakub Dawidek static int
4152461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4153461167c2SPawel Jakub Dawidek {
4154461167c2SPawel Jakub Dawidek 	int error, injail;
4155461167c2SPawel Jakub Dawidek 
4156461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
4157461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4158461167c2SPawel Jakub Dawidek 
4159461167c2SPawel Jakub Dawidek 	return (error);
4160461167c2SPawel Jakub Dawidek }
41610304c731SJamie Gritton 
4162f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4163f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4164f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
4165413628a7SBjoern A. Zeeb 
4166761d2bb5SJamie Gritton static int
4167761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4168761d2bb5SJamie Gritton {
4169761d2bb5SJamie Gritton 	int error, havevnet;
4170761d2bb5SJamie Gritton #ifdef VIMAGE
4171761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
4172761d2bb5SJamie Gritton 
4173761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
4174761d2bb5SJamie Gritton #else
4175761d2bb5SJamie Gritton 	havevnet = 0;
4176761d2bb5SJamie Gritton #endif
4177761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4178761d2bb5SJamie Gritton 
4179761d2bb5SJamie Gritton 	return (error);
4180761d2bb5SJamie Gritton }
4181761d2bb5SJamie Gritton 
4182761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4183761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4184bb8f1623SBjoern A. Zeeb     sysctl_jail_vnet, "I", "Jail owns vnet?");
4185761d2bb5SJamie Gritton 
41860304c731SJamie Gritton #if defined(INET) || defined(INET6)
4187e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
41880304c731SJamie Gritton     &jail_max_af_ips, 0,
4189c542c43eSJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
41900304c731SJamie Gritton #endif
41910304c731SJamie Gritton 
41920304c731SJamie Gritton /*
4193c542c43eSJamie Gritton  * Default parameters for jail(2) compatibility.  For historical reasons,
4194c542c43eSJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
4195c542c43eSJamie Gritton  * just see their own parameters, and can't change them.
41960304c731SJamie Gritton  */
41970304c731SJamie Gritton static int
41980304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
41990304c731SJamie Gritton {
42000fe74ae6SJamie Gritton 	int error, i;
42010304c731SJamie Gritton 
42020304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
42030fe74ae6SJamie Gritton 	if (req->td->td_ucred->cr_prison == &prison0) {
42040fe74ae6SJamie Gritton 		mtx_lock(&prison0.pr_mtx);
42050fe74ae6SJamie Gritton 		i = (jail_default_allow & arg2) != 0;
42060fe74ae6SJamie Gritton 		mtx_unlock(&prison0.pr_mtx);
42070fe74ae6SJamie Gritton 	} else
42080fe74ae6SJamie Gritton 		i = prison_allow(req->td->td_ucred, arg2);
42090fe74ae6SJamie Gritton 
42100304c731SJamie Gritton 	if (arg1 != NULL)
42110304c731SJamie Gritton 		i = !i;
42120304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
4213c542c43eSJamie Gritton 	if (error || !req->newptr)
42140304c731SJamie Gritton 		return (error);
42150304c731SJamie Gritton 	i = i ? arg2 : 0;
42160304c731SJamie Gritton 	if (arg1 != NULL)
42170304c731SJamie Gritton 		i ^= arg2;
42180304c731SJamie Gritton 	/*
42190304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
42200304c731SJamie Gritton 	 * for writing.
42210304c731SJamie Gritton 	 */
42220304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
42230304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
42240304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
42250304c731SJamie Gritton 	return (0);
42260304c731SJamie Gritton }
42270304c731SJamie Gritton 
42280304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4229c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42300304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4231c542c43eSJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
42320304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4233c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42340304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4235c542c43eSJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
42360304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4237c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42380304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4239c542c43eSJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
42400304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4241c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42420304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4243c542c43eSJamie Gritton     "Prison root can create raw sockets (deprecated)");
42440304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4245c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42460304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4247c542c43eSJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
42480304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4249c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42500304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4251c542c43eSJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
42520304c731SJamie Gritton 
42530304c731SJamie Gritton static int
42540304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
42550304c731SJamie Gritton {
42560304c731SJamie Gritton 	struct prison *pr;
42570304c731SJamie Gritton 	int level, error;
42580304c731SJamie Gritton 
42590304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
42600304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
42610304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
4262c542c43eSJamie Gritton 	if (error || !req->newptr)
42630304c731SJamie Gritton 		return (error);
42640304c731SJamie Gritton 	*(int *)arg1 = level;
42650304c731SJamie Gritton 	return (0);
42660304c731SJamie Gritton }
42670304c731SJamie Gritton 
42680304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4269c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4270c542c43eSJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
42710304c731SJamie Gritton     sysctl_jail_default_level, "I",
4272c542c43eSJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
4273c542c43eSJamie Gritton 
42740cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4275c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4276c542c43eSJamie Gritton     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
42770cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
4278c542c43eSJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
42790cc207a6SMartin Matuska 
42800304c731SJamie Gritton /*
42810304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
42820304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
42830304c731SJamie Gritton  * to make themselves and their types known.
42840304c731SJamie Gritton  */
42857029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
42860304c731SJamie Gritton     "Jail parameters");
42870304c731SJamie Gritton 
42880304c731SJamie Gritton int
42890304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
42900304c731SJamie Gritton {
42910304c731SJamie Gritton 	int i;
42920304c731SJamie Gritton 	long l;
42930304c731SJamie Gritton 	size_t s;
42940304c731SJamie Gritton 	char numbuf[12];
42950304c731SJamie Gritton 
42960304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
42970304c731SJamie Gritton 	{
42980304c731SJamie Gritton 	case CTLTYPE_LONG:
42990304c731SJamie Gritton 	case CTLTYPE_ULONG:
43000304c731SJamie Gritton 		l = 0;
43010304c731SJamie Gritton #ifdef SCTL_MASK32
43020304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
43030304c731SJamie Gritton #endif
43040304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
43050304c731SJamie Gritton 	case CTLTYPE_INT:
43060304c731SJamie Gritton 	case CTLTYPE_UINT:
43070304c731SJamie Gritton 		i = 0;
43080304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
43090304c731SJamie Gritton 	case CTLTYPE_STRING:
4310e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
43110304c731SJamie Gritton 		return
43120304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
43130304c731SJamie Gritton 	case CTLTYPE_STRUCT:
43140304c731SJamie Gritton 		s = (size_t)arg2;
43150304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
43160304c731SJamie Gritton 	}
43170304c731SJamie Gritton 	return (0);
43180304c731SJamie Gritton }
43190304c731SJamie Gritton 
4320b96bd95bSIan Lepore /*
4321b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4322b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
4323b96bd95bSIan Lepore  */
43240304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
43250304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
43260304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
43270304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
43280304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
43290304c731SJamie Gritton     "I", "Jail secure level");
4330b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4331b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
4332b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4333b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
43340304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
43350304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
43360cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
43370cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
43380304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
43390304c731SJamie Gritton     "B", "Jail persistence");
4340679e1390SJamie Gritton #ifdef VIMAGE
4341679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
43427cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4343679e1390SJamie Gritton #endif
43440304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
43450304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
43460304c731SJamie Gritton 
4347b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4348b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4349b97457e2SJamie Gritton     "I", "Current number of child jails");
4350b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4351b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4352b97457e2SJamie Gritton 
43537cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
43540304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
43550304c731SJamie Gritton     "Jail hostname");
435676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
435776ca6f88SJamie Gritton     "Jail NIS domainname");
435876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
435976ca6f88SJamie Gritton     "Jail host UUID");
436076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
436176ca6f88SJamie Gritton     "LU", "Jail host ID");
43620304c731SJamie Gritton 
43630304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
43640304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
43650304c731SJamie Gritton 
43660304c731SJamie Gritton #ifdef INET
43672b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
43682b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
43690304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
43700304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4371592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4372592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4373592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
43740304c731SJamie Gritton #endif
43750304c731SJamie Gritton #ifdef INET6
43762b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
43772b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
43780304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
43790304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4380592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4381592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4382592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
43830304c731SJamie Gritton #endif
43840304c731SJamie Gritton 
43850304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
43860304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
43870304c731SJamie Gritton     "B", "Jail may set hostname");
43880304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
43890304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
43900304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
43910304c731SJamie Gritton     "B", "Jail may create raw sockets");
43920304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
43930304c731SJamie Gritton     "B", "Jail may alter system file flags");
43940304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
43950304c731SJamie Gritton     "B", "Jail may set file quotas");
43960304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
43970304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4398ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4399ccd6ac9fSAntoine Brodin     "B", "Jail may lock (unlock) physical pages in memory");
4400e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4401e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
4402b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4403b19d66fdSJamie Gritton     "B", "Jail may read the kernel message buffer");
4404b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4405b3079544SJamie Gritton     "B", "Unprivileged processes may use process debugging facilities");
440605e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
440705e1e482SMariusz Zaborski     "B", "Processes in jail with uid 0 have privilege");
44080304c731SJamie Gritton 
4409bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4410bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4411bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
44120e5c6bd4SJamie Gritton 
44130e5c6bd4SJamie Gritton /*
44140a172404SJamie Gritton  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
44150a172404SJamie Gritton  * its associated bit in the pr_allow bitmask, or zero if the parameter was
44160a172404SJamie Gritton  * not created.
44170e5c6bd4SJamie Gritton  */
44180a172404SJamie Gritton unsigned
44190a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
44200a172404SJamie Gritton     const char *descr)
44210e5c6bd4SJamie Gritton {
44220e5c6bd4SJamie Gritton 	struct bool_flags *bf;
44230a172404SJamie Gritton 	struct sysctl_oid *parent;
44240a172404SJamie Gritton 	char *allow_name, *allow_noname, *allowed;
4425c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4426c542c43eSJamie Gritton 	char *descr_deprecated;
4427c542c43eSJamie Gritton #endif
44285d58f959SJamie Gritton 	u_int allow_flag;
44290e5c6bd4SJamie Gritton 
44300a172404SJamie Gritton 	if (prefix
44310a172404SJamie Gritton 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
44320a172404SJamie Gritton 		< 0 ||
44330a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
44340a172404SJamie Gritton 		< 0
44350a172404SJamie Gritton 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
44360a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
44370e5c6bd4SJamie Gritton 		free(allow_name, M_PRISON);
44380a172404SJamie Gritton 		return 0;
44390e5c6bd4SJamie Gritton 	}
44400e5c6bd4SJamie Gritton 
44410e5c6bd4SJamie Gritton 	/*
44420a172404SJamie Gritton 	 * See if this parameter has already beed added, i.e. a module was
44430a172404SJamie Gritton 	 * previously loaded/unloaded.
44440e5c6bd4SJamie Gritton 	 */
44450e5c6bd4SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
44460e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow;
44475d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
44485d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
44490e5c6bd4SJamie Gritton 	     bf++) {
44500e5c6bd4SJamie Gritton 		if (strcmp(bf->name, allow_name) == 0) {
44510a172404SJamie Gritton 			allow_flag = bf->flag;
44520e5c6bd4SJamie Gritton 			goto no_add;
44530e5c6bd4SJamie Gritton 		}
44540e5c6bd4SJamie Gritton 	}
44550e5c6bd4SJamie Gritton 
44560e5c6bd4SJamie Gritton 	/*
44575d58f959SJamie Gritton 	 * Find a free bit in pr_allow_all, failing if there are none
44580e5c6bd4SJamie Gritton 	 * (which shouldn't happen as long as we keep track of how many
44590a172404SJamie Gritton 	 * potential dynamic flags exist).
44600e5c6bd4SJamie Gritton 	 */
44610e5c6bd4SJamie Gritton 	for (allow_flag = 1;; allow_flag <<= 1) {
44620e5c6bd4SJamie Gritton 		if (allow_flag == 0)
44630e5c6bd4SJamie Gritton 			goto no_add;
44645d58f959SJamie Gritton 		if ((pr_allow_all & allow_flag) == 0)
44650e5c6bd4SJamie Gritton 			break;
44660e5c6bd4SJamie Gritton 	}
44670e5c6bd4SJamie Gritton 
44685d58f959SJamie Gritton 	/* Note the parameter in the next open slot in pr_flag_allow. */
44695d58f959SJamie Gritton 	for (bf = pr_flag_allow; ; bf++) {
44700e5c6bd4SJamie Gritton 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
44710e5c6bd4SJamie Gritton 			/* This should never happen, but is not fatal. */
44720a172404SJamie Gritton 			allow_flag = 0;
44730e5c6bd4SJamie Gritton 			goto no_add;
44740e5c6bd4SJamie Gritton 		}
44755d58f959SJamie Gritton 		if (atomic_load_int(&bf->flag) == 0)
44765d58f959SJamie Gritton 			break;
44775d58f959SJamie Gritton 	}
44780e5c6bd4SJamie Gritton 	bf->name = allow_name;
44790e5c6bd4SJamie Gritton 	bf->noname = allow_noname;
44805d58f959SJamie Gritton 	pr_allow_all |= allow_flag;
44815d58f959SJamie Gritton 	/*
44825d58f959SJamie Gritton 	 * prison0 always has permission for the new parameter.
44835d58f959SJamie Gritton 	 * Other jails must have it granted to them.
44845d58f959SJamie Gritton 	 */
44855d58f959SJamie Gritton 	prison0.pr_allow |= allow_flag;
44865d58f959SJamie Gritton 	/* The flag indicates a valid entry, so make sure it is set last. */
44875d58f959SJamie Gritton 	atomic_store_rel_int(&bf->flag, allow_flag);
44880e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
44890e5c6bd4SJamie Gritton 
4490c542c43eSJamie Gritton 	/*
4491c542c43eSJamie Gritton 	 * Create sysctls for the paramter, and the back-compat global
4492c542c43eSJamie Gritton 	 * permission.
4493c542c43eSJamie Gritton 	 */
44940a172404SJamie Gritton 	parent = prefix
44950a172404SJamie Gritton 	    ? SYSCTL_ADD_NODE(NULL,
44960a172404SJamie Gritton 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
44977029da5cSPawel Biernacki 		  OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
44980a172404SJamie Gritton 	    : &sysctl___security_jail_param_allow;
44990a172404SJamie Gritton 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
45000a172404SJamie Gritton 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
45010e5c6bd4SJamie Gritton 	    NULL, 0, sysctl_jail_param, "B", descr);
45020a172404SJamie Gritton 	if ((prefix
45030a172404SJamie Gritton 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
45040a172404SJamie Gritton 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4505c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4506c542c43eSJamie Gritton 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4507c542c43eSJamie Gritton 		    descr);
4508c542c43eSJamie Gritton #endif
45090e5c6bd4SJamie Gritton 		(void)SYSCTL_ADD_PROC(NULL,
45100a172404SJamie Gritton 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4511c542c43eSJamie Gritton 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4512c542c43eSJamie Gritton 		    sysctl_jail_default_allow, "I", descr_deprecated);
4513c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4514c542c43eSJamie Gritton 		free(descr_deprecated, M_TEMP);
4515c542c43eSJamie Gritton #endif
45160a172404SJamie Gritton 		free(allowed, M_TEMP);
45170e5c6bd4SJamie Gritton 	}
45180a172404SJamie Gritton 	return allow_flag;
45190e5c6bd4SJamie Gritton 
45200e5c6bd4SJamie Gritton  no_add:
45210e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
45220e5c6bd4SJamie Gritton 	free(allow_name, M_PRISON);
45230e5c6bd4SJamie Gritton 	free(allow_noname, M_PRISON);
45240a172404SJamie Gritton 	return allow_flag;
45250a172404SJamie Gritton }
45260a172404SJamie Gritton 
45270a172404SJamie Gritton /*
45280a172404SJamie Gritton  * The VFS system will register jail-aware filesystems here.  They each get
45290a172404SJamie Gritton  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
45300a172404SJamie Gritton  * attempts to mount.
45310a172404SJamie Gritton  */
45320a172404SJamie Gritton void
45330a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp)
45340a172404SJamie Gritton {
45350a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR
45360a172404SJamie Gritton 
45370a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
45380a172404SJamie Gritton 	    NULL, NULL);
45390a172404SJamie Gritton #else
45400a172404SJamie Gritton 	char *descr;
45410a172404SJamie Gritton 
45420a172404SJamie Gritton 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
45430a172404SJamie Gritton 	    vfsp->vfc_name);
45440a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
45450a172404SJamie Gritton 	    NULL, descr);
45460a172404SJamie Gritton 	free(descr, M_TEMP);
45470a172404SJamie Gritton #endif
45480e5c6bd4SJamie Gritton }
4549bf3db8aaSMartin Matuska 
45504b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4551097055e2SEdward Tomasz Napierala void
4552097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
455315db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
455415db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
4555097055e2SEdward Tomasz Napierala {
4556a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4557097055e2SEdward Tomasz Napierala 
45584b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
45594b5c9cf6SEdward Tomasz Napierala 
4560097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
456115db3c07SEdward Tomasz Napierala 	if (pre != NULL)
456215db3c07SEdward Tomasz Napierala 		(pre)();
4563a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4564a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
456515db3c07SEdward Tomasz Napierala 	if (post != NULL)
456615db3c07SEdward Tomasz Napierala 		(post)();
4567097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4568097055e2SEdward Tomasz Napierala }
45690304c731SJamie Gritton 
4570a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4571a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4572a7ad07bfSEdward Tomasz Napierala {
4573a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4574a7ad07bfSEdward Tomasz Napierala 
45754b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4576a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4577a7ad07bfSEdward Tomasz Napierala 
4578a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4579a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4580a7ad07bfSEdward Tomasz Napierala 
4581a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4582a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4583a7ad07bfSEdward Tomasz Napierala 			continue;
4584a7ad07bfSEdward Tomasz Napierala 
4585a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4586a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4587a7ad07bfSEdward Tomasz Napierala 		return (prr);
4588a7ad07bfSEdward Tomasz Napierala 	}
4589a7ad07bfSEdward Tomasz Napierala 
4590a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4591a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4592a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4593a7ad07bfSEdward Tomasz Napierala 
4594a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4595a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4596a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4597a7ad07bfSEdward Tomasz Napierala 
4598a7ad07bfSEdward Tomasz Napierala 	return (prr);
4599a7ad07bfSEdward Tomasz Napierala }
4600a7ad07bfSEdward Tomasz Napierala 
4601a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4602a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4603a7ad07bfSEdward Tomasz Napierala {
4604a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4605a7ad07bfSEdward Tomasz Napierala 
46064b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46074b5c9cf6SEdward Tomasz Napierala 
4608a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4609a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4610a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4611a7ad07bfSEdward Tomasz Napierala 	return (prr);
4612a7ad07bfSEdward Tomasz Napierala }
4613a7ad07bfSEdward Tomasz Napierala 
4614a7ad07bfSEdward Tomasz Napierala void
4615a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4616a7ad07bfSEdward Tomasz Napierala {
4617a7ad07bfSEdward Tomasz Napierala 
46184b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46194b5c9cf6SEdward Tomasz Napierala 
4620a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4621a7ad07bfSEdward Tomasz Napierala }
4622a7ad07bfSEdward Tomasz Napierala 
4623c34bbd2aSEdward Tomasz Napierala static void
4624c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4625c34bbd2aSEdward Tomasz Napierala {
4626c34bbd2aSEdward Tomasz Napierala 
46274b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4628c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4629c34bbd2aSEdward Tomasz Napierala 
4630c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4631c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4632c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4633c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4634c34bbd2aSEdward Tomasz Napierala 	}
4635c34bbd2aSEdward Tomasz Napierala }
4636c34bbd2aSEdward Tomasz Napierala 
4637a7ad07bfSEdward Tomasz Napierala void
4638a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4639a7ad07bfSEdward Tomasz Napierala {
4640a7ad07bfSEdward Tomasz Napierala 
46414b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4642c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4643c34bbd2aSEdward Tomasz Napierala 
46446ff4688bSMateusz Guzik 	if (refcount_release_if_not_last(&prr->prr_refcount))
4645a7ad07bfSEdward Tomasz Napierala 		return;
4646a7ad07bfSEdward Tomasz Napierala 
4647a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4648c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4649a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4650a7ad07bfSEdward Tomasz Napierala }
4651a7ad07bfSEdward Tomasz Napierala 
4652a7ad07bfSEdward Tomasz Napierala static void
4653a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4654a7ad07bfSEdward Tomasz Napierala {
4655a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4656a7ad07bfSEdward Tomasz Napierala 
46574b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4658c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4659c34bbd2aSEdward Tomasz Napierala 
4660a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4661a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4662a7ad07bfSEdward Tomasz Napierala 
4663a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4664a7ad07bfSEdward Tomasz Napierala }
4665a7ad07bfSEdward Tomasz Napierala 
4666c34bbd2aSEdward Tomasz Napierala /*
4667c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4668c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4669c34bbd2aSEdward Tomasz Napierala  */
4670c34bbd2aSEdward Tomasz Napierala static void
4671c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4672c34bbd2aSEdward Tomasz Napierala {
4673dbadb015SKonstantin Belousov #ifdef RCTL
4674c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4675c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4676dbadb015SKonstantin Belousov #endif
4677c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4678c34bbd2aSEdward Tomasz Napierala 
46794b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46804b5c9cf6SEdward Tomasz Napierala 
4681c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4682c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4683c34bbd2aSEdward Tomasz Napierala 
4684e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4685e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4686e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4687c34bbd2aSEdward Tomasz Napierala 		return;
4688e30345e7SEdward Tomasz Napierala 	}
4689c34bbd2aSEdward Tomasz Napierala 
4690c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4691c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4692c34bbd2aSEdward Tomasz Napierala 
4693c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4694c34bbd2aSEdward Tomasz Napierala 
4695c34bbd2aSEdward Tomasz Napierala 	/*
4696c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4697c34bbd2aSEdward Tomasz Napierala 	 */
4698c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4699c34bbd2aSEdward Tomasz Napierala 
4700f87beb93SAndriy Gapon #ifdef RCTL
4701c34bbd2aSEdward Tomasz Napierala 	/*
4702c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4703c34bbd2aSEdward Tomasz Napierala 	 */
4704c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4705c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4706c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4707c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4708f87beb93SAndriy Gapon 		rctl_proc_ucred_changed(p, cred);
4709c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4710c34bbd2aSEdward Tomasz Napierala 	}
4711f87beb93SAndriy Gapon #endif
4712c34bbd2aSEdward Tomasz Napierala 
4713c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4714c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4715c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4716c34bbd2aSEdward Tomasz Napierala }
4717c34bbd2aSEdward Tomasz Napierala 
4718a7ad07bfSEdward Tomasz Napierala static void
4719a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4720a7ad07bfSEdward Tomasz Napierala {
4721c34bbd2aSEdward Tomasz Napierala 
47224b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4723c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4724c34bbd2aSEdward Tomasz Napierala 
4725af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4726af3c786cSMateusz Guzik 		return;
4727a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4728a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4729a7ad07bfSEdward Tomasz Napierala }
4730a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4731a7ad07bfSEdward Tomasz Napierala 
4732413628a7SBjoern A. Zeeb #ifdef DDB
4733b38ff370SJamie Gritton 
4734b38ff370SJamie Gritton static void
4735b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4736413628a7SBjoern A. Zeeb {
4737672756aaSJamie Gritton 	struct bool_flags *bf;
4738672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4739b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4740b38ff370SJamie Gritton 	int ii;
4741413628a7SBjoern A. Zeeb #endif
4742672756aaSJamie Gritton 	unsigned f;
47438144690aSEric van Gyzen #ifdef INET
47448144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
47458144690aSEric van Gyzen #endif
4746413628a7SBjoern A. Zeeb #ifdef INET6
4747413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4748413628a7SBjoern A. Zeeb #endif
4749413628a7SBjoern A. Zeeb 
4750b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4751b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4752b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
47530304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4754b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4755b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
47561158508aSJamie Gritton 	db_printf(" state           = %s\n",
47571158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
47581158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_DYING ? "dying" :
47591158508aSJamie Gritton 	    "invalid");
4760b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4761b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4762b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4763679e1390SJamie Gritton #ifdef VIMAGE
4764679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4765679e1390SJamie Gritton #endif
4766b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4767b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
47680cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4769fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4770fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
47710304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
47720304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4773fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
4774672756aaSJamie Gritton 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4775672756aaSJamie Gritton 		if (pr->pr_flags & bf->flag)
4776672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4777672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
4778672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4779672756aaSJamie Gritton 	     jsf++) {
4780672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
4781672756aaSJamie Gritton 		db_printf(" %-16s= %s\n", jsf->name,
4782672756aaSJamie Gritton 		    (f != 0 && f == jsf->disable) ? "disable"
4783672756aaSJamie Gritton 		    : (f == jsf->new) ? "new"
47847cbf7213SJamie Gritton 		    : "inherit");
47857cbf7213SJamie Gritton 	}
4786fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
4787672756aaSJamie Gritton 	for (bf = pr_flag_allow;
47885d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
47895d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
4790672756aaSJamie Gritton 	     bf++)
4791672756aaSJamie Gritton 		if (pr->pr_allow & bf->flag)
4792672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4793b38ff370SJamie Gritton 	db_printf("\n");
47940304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4795c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4796c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4797c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
479876ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4799413628a7SBjoern A. Zeeb #ifdef INET
4800eb8dcdeaSGleb Smirnoff 	if (pr->pr_addrs[PR_INET] != NULL) {
4801eb8dcdeaSGleb Smirnoff 		pr_family_t af = PR_INET;
4802eb8dcdeaSGleb Smirnoff 
4803eb8dcdeaSGleb Smirnoff 		db_printf(" ip4s            = %d\n", pr->pr_addrs[af]->ips);
4804eb8dcdeaSGleb Smirnoff 		for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4805b38ff370SJamie Gritton 			db_printf(" %s %s\n",
4806fe0518e9SBjoern A. Zeeb 			    ii == 0 ? "ip4.addr        =" : "                 ",
4807eb8dcdeaSGleb Smirnoff 			    inet_ntoa_r(
4808eb8dcdeaSGleb Smirnoff 			    *(const struct in_addr *)PR_IP(pr, ii),
4809eb8dcdeaSGleb Smirnoff 			    ip4buf));
4810eb8dcdeaSGleb Smirnoff 	}
4811413628a7SBjoern A. Zeeb #endif
4812413628a7SBjoern A. Zeeb #ifdef INET6
4813eb8dcdeaSGleb Smirnoff 	if (pr->pr_addrs[PR_INET6] != NULL) {
4814eb8dcdeaSGleb Smirnoff 		pr_family_t af = PR_INET6;
4815eb8dcdeaSGleb Smirnoff 
4816eb8dcdeaSGleb Smirnoff 		db_printf(" ip6s            = %d\n", pr->pr_addrs[af]->ips);
4817eb8dcdeaSGleb Smirnoff 		for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4818b38ff370SJamie Gritton 			db_printf(" %s %s\n",
4819fe0518e9SBjoern A. Zeeb 			    ii == 0 ? "ip6.addr        =" : "                 ",
4820eb8dcdeaSGleb Smirnoff 			    ip6_sprintf(ip6buf,
4821eb8dcdeaSGleb Smirnoff 			    (const struct in6_addr *)PR_IP(pr, ii)));
4822eb8dcdeaSGleb Smirnoff 	}
4823b38ff370SJamie Gritton #endif
4824b38ff370SJamie Gritton }
4825b38ff370SJamie Gritton 
4826b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4827b38ff370SJamie Gritton {
4828b38ff370SJamie Gritton 	struct prison *pr;
4829b38ff370SJamie Gritton 
4830b38ff370SJamie Gritton 	if (!have_addr) {
48310304c731SJamie Gritton 		/*
48320304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
48330304c731SJamie Gritton 		 * listed.
48340304c731SJamie Gritton 		 */
48350304c731SJamie Gritton 		db_show_prison(&prison0);
48360304c731SJamie Gritton 		if (!db_pager_quit) {
4837b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4838b38ff370SJamie Gritton 				db_show_prison(pr);
4839413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4840413628a7SBjoern A. Zeeb 					break;
4841413628a7SBjoern A. Zeeb 			}
48420304c731SJamie Gritton 		}
4843b38ff370SJamie Gritton 		return;
4844413628a7SBjoern A. Zeeb 	}
4845b38ff370SJamie Gritton 
48460304c731SJamie Gritton 	if (addr == 0)
48470304c731SJamie Gritton 		pr = &prison0;
48480304c731SJamie Gritton 	else {
4849b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4850b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4851b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4852b38ff370SJamie Gritton 				break;
4853b38ff370SJamie Gritton 		if (pr == NULL)
4854b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4855b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4856b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4857b38ff370SJamie Gritton 					break;
4858b38ff370SJamie Gritton 		if (pr == NULL)
4859b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4860b38ff370SJamie Gritton 			pr = (struct prison *)addr;
48610304c731SJamie Gritton 	}
4862b38ff370SJamie Gritton 	db_show_prison(pr);
4863b38ff370SJamie Gritton }
4864b38ff370SJamie Gritton 
4865413628a7SBjoern A. Zeeb #endif /* DDB */
4866