xref: /freebsd/sys/kern/kern_jail.c (revision 0b0ae2e4cd22c21f3c1971b1cfff9893b30d9f6f)
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"
37bba7a2e8SRick Macklem #include "opt_nfs.h"
3846e3b1cbSPawel Jakub Dawidek 
3975c13541SPoul-Henning Kamp #include <sys/param.h>
4075c13541SPoul-Henning Kamp #include <sys/types.h>
4175c13541SPoul-Henning Kamp #include <sys/kernel.h>
4275c13541SPoul-Henning Kamp #include <sys/systm.h>
4375c13541SPoul-Henning Kamp #include <sys/errno.h>
4475c13541SPoul-Henning Kamp #include <sys/sysproto.h>
4575c13541SPoul-Henning Kamp #include <sys/malloc.h>
460304c731SJamie Gritton #include <sys/osd.h>
47800c9408SRobert Watson #include <sys/priv.h>
4875c13541SPoul-Henning Kamp #include <sys/proc.h>
49eb8dcdeaSGleb Smirnoff #include <sys/epoch.h>
50b3059e09SRobert Watson #include <sys/taskqueue.h>
5157b4252eSKonstantin Belousov #include <sys/fcntl.h>
5275c13541SPoul-Henning Kamp #include <sys/jail.h>
53c3188289SKyle Evans #include <sys/linker.h>
5401137630SRobert Watson #include <sys/lock.h>
557060da62SJamie Gritton #include <sys/mman.h>
5601137630SRobert Watson #include <sys/mutex.h>
57097055e2SEdward Tomasz Napierala #include <sys/racct.h>
58f87beb93SAndriy Gapon #include <sys/rctl.h>
59a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
60dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
6176ca6f88SJamie Gritton #include <sys/sysent.h>
62fd7a8150SMike Barcroft #include <sys/namei.h>
63820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
64fd7a8150SMike Barcroft #include <sys/queue.h>
6575c13541SPoul-Henning Kamp #include <sys/socket.h>
66fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6783f1e257SRobert Watson #include <sys/sysctl.h>
68c3188289SKyle Evans #include <sys/uuid.h>
69fd7a8150SMike Barcroft #include <sys/vnode.h>
70530c0060SRobert Watson 
7175c13541SPoul-Henning Kamp #include <net/if.h>
72530c0060SRobert Watson #include <net/vnet.h>
73530c0060SRobert Watson 
7475c13541SPoul-Henning Kamp #include <netinet/in.h>
75530c0060SRobert Watson 
76413628a7SBjoern A. Zeeb #ifdef DDB
77413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
78413628a7SBjoern A. Zeeb #endif /* DDB */
7975c13541SPoul-Henning Kamp 
80aed55708SRobert Watson #include <security/mac/mac_framework.h>
81aed55708SRobert Watson 
82c3188289SKyle Evans #define	PRISON0_HOSTUUID_MODULE	"hostuuid"
838986e3a0SJamie Gritton 
8475c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
85d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8675c13541SPoul-Henning Kamp 
87592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
88592bcae8SBjoern A. Zeeb #ifdef INET
89592bcae8SBjoern A. Zeeb #ifdef INET6
90592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
91592bcae8SBjoern A. Zeeb #else
92592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
93592bcae8SBjoern A. Zeeb #endif
94592bcae8SBjoern A. Zeeb #else /* !INET */
95592bcae8SBjoern A. Zeeb #ifdef INET6
96592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
97592bcae8SBjoern A. Zeeb #else
98592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
99592bcae8SBjoern A. Zeeb #endif
100592bcae8SBjoern A. Zeeb #endif
101592bcae8SBjoern A. Zeeb 
1020304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
1030304c731SJamie Gritton struct prison prison0 = {
1040304c731SJamie Gritton 	.pr_id		= 0,
1050304c731SJamie Gritton 	.pr_name	= "0",
1060304c731SJamie Gritton 	.pr_ref		= 1,
1070304c731SJamie Gritton 	.pr_uref	= 1,
1080304c731SJamie Gritton 	.pr_path	= "/",
1090304c731SJamie Gritton 	.pr_securelevel	= -1,
1100cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
1111158508aSJamie Gritton 	.pr_state	= PRISON_STATE_ALIVE,
112b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1138986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
11413e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
115eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
116592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
117eb79e1c7SBjoern A. Zeeb #else
118592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
119eb79e1c7SBjoern A. Zeeb #endif
1200e5c6bd4SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL_STATIC,
1210304c731SJamie Gritton };
1220304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
12383f1e257SRobert Watson 
124672756aaSJamie Gritton struct bool_flags {
125672756aaSJamie Gritton 	const char	*name;
126672756aaSJamie Gritton 	const char	*noname;
1275d58f959SJamie Gritton 	volatile u_int	 flag;
128672756aaSJamie Gritton };
129672756aaSJamie Gritton struct jailsys_flags {
130672756aaSJamie Gritton 	const char	*name;
131672756aaSJamie Gritton 	unsigned	 disable;
132672756aaSJamie Gritton 	unsigned	 new;
133672756aaSJamie Gritton };
134672756aaSJamie Gritton 
135a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
136dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
137b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
138b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
139a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1402110d913SXin LI int	lastprid = 0;
141fd7a8150SMike Barcroft 
1427de883c8SJamie Gritton static int get_next_prid(struct prison **insprp);
143f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
144b3059e09SRobert Watson static void prison_complete(void *context, int pending);
145b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
146c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
147f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags);
148a9f7455cSJamie Gritton static void prison_cleanup(struct prison *pr);
149f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr);
150c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr);
1515ecb5444SMateusz Guzik static void prison_proc_relink(struct prison *opr, struct prison *npr,
1525ecb5444SMateusz Guzik     struct proc *p);
1530fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag,
1540fe74ae6SJamie Gritton     int enable);
1550304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
156a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
157a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
158c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
159a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
160a7ad07bfSEdward Tomasz Napierala #endif
161fd7a8150SMike Barcroft 
162b38ff370SJamie Gritton /* Flags for prison_deref */
1632a4b2251SJamie Gritton #define	PD_DEREF	0x01	/* Decrement pr_ref */
1642a4b2251SJamie Gritton #define	PD_DEUREF	0x02	/* Decrement pr_uref */
165c861373bSJamie Gritton #define	PD_KILL		0x04	/* Remove jail, kill processes, etc */
166c861373bSJamie Gritton #define	PD_LOCKED	0x10	/* pr_mtx is held */
167c861373bSJamie Gritton #define	PD_LIST_SLOCKED	0x20	/* allprison_lock is held shared */
168c861373bSJamie Gritton #define	PD_LIST_XLOCKED	0x40	/* allprison_lock is held exclusive */
169589e4c1dSJamie Gritton #define PD_OP_FLAGS	0x07	/* Operation flags */
170589e4c1dSJamie Gritton #define PD_LOCK_FLAGS	0x70	/* Lock status flags */
171fd7a8150SMike Barcroft 
1720304c731SJamie Gritton /*
1735cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1745cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1755cc70397SBjoern A. Zeeb  * terminating entry.
1760304c731SJamie Gritton  */
177672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = {
178672756aaSJamie Gritton 	{"persist", "nopersist", PR_PERSIST},
179592bcae8SBjoern A. Zeeb #ifdef INET
180672756aaSJamie Gritton 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
181592bcae8SBjoern A. Zeeb #endif
182592bcae8SBjoern A. Zeeb #ifdef INET6
183672756aaSJamie Gritton 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
184592bcae8SBjoern A. Zeeb #endif
1850304c731SJamie Gritton };
186672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
1870304c731SJamie Gritton 
188672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = {
1897cbf7213SJamie Gritton 	{"host", 0, PR_HOST},
1907cbf7213SJamie Gritton #ifdef VIMAGE
1917cbf7213SJamie Gritton 	{"vnet", 0, PR_VNET},
1927cbf7213SJamie Gritton #endif
1930304c731SJamie Gritton #ifdef INET
1946a3f2779SJamie Gritton 	{"ip4", PR_IP4_USER, PR_IP4_USER},
1950304c731SJamie Gritton #endif
1960304c731SJamie Gritton #ifdef INET6
1976a3f2779SJamie Gritton 	{"ip6", PR_IP6_USER, PR_IP6_USER},
198679e1390SJamie Gritton #endif
1990304c731SJamie Gritton };
2005cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
2010304c731SJamie Gritton 
2025d58f959SJamie Gritton /*
2035d58f959SJamie Gritton  * Make this array full-size so dynamic parameters can be added.
2045d58f959SJamie Gritton  * It is protected by prison0.mtx, but lockless reading is allowed
2055d58f959SJamie Gritton  * with an atomic check of the flag values.
2065d58f959SJamie Gritton  */
2070e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
208672756aaSJamie Gritton 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
209672756aaSJamie Gritton 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
210672756aaSJamie Gritton 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
211672756aaSJamie Gritton 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
212672756aaSJamie Gritton 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
213672756aaSJamie Gritton 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
214672756aaSJamie Gritton 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
215ccd6ac9fSAntoine Brodin 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
216672756aaSJamie Gritton 	{"allow.reserved_ports", "allow.noreserved_ports",
217672756aaSJamie Gritton 	 PR_ALLOW_RESERVED_PORTS},
218b19d66fdSJamie Gritton 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
219b3079544SJamie Gritton 	{"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
220b3079544SJamie Gritton 	 PR_ALLOW_UNPRIV_DEBUG},
22105e1e482SMariusz Zaborski 	{"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
222cbbb2203SRick Macklem #ifdef VIMAGE
223bba7a2e8SRick Macklem 	{"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD},
224bba7a2e8SRick Macklem #endif
2250304c731SJamie Gritton };
2265d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
227672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
2280304c731SJamie Gritton 
229b3079544SJamie Gritton #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | \
230b3079544SJamie Gritton 					 PR_ALLOW_RESERVED_PORTS | \
23105e1e482SMariusz Zaborski 					 PR_ALLOW_UNPRIV_DEBUG | \
23205e1e482SMariusz Zaborski 					 PR_ALLOW_SUSER)
23342bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
234bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2350304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
23642bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2370cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2380304c731SJamie Gritton #if defined(INET) || defined(INET6)
239e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2400304c731SJamie Gritton #endif
2410304c731SJamie Gritton 
242b96bd95bSIan Lepore /*
243b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
244b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
245b96bd95bSIan Lepore  */
246b96bd95bSIan Lepore void
247b96bd95bSIan Lepore prison0_init(void)
248b96bd95bSIan Lepore {
249c3188289SKyle Evans 	uint8_t *file, *data;
250c3188289SKyle Evans 	size_t size;
251d2ef3774SJessica Clarke 	char buf[sizeof(prison0.pr_hostuuid)];
252d2ef3774SJessica Clarke 	bool valid;
253b96bd95bSIan Lepore 
254b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
255b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
256b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
257c3188289SKyle Evans 
258c3188289SKyle Evans 	/* If we have a preloaded hostuuid, use it. */
259c3188289SKyle Evans 	file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
260c3188289SKyle Evans 	if (file != NULL) {
261c3188289SKyle Evans 		data = preload_fetch_addr(file);
262c3188289SKyle Evans 		size = preload_fetch_size(file);
263c3188289SKyle Evans 		if (data != NULL) {
264c3188289SKyle Evans 			/*
265c3188289SKyle Evans 			 * The preloaded data may include trailing whitespace, almost
266c3188289SKyle Evans 			 * certainly a newline; skip over any whitespace or
267c3188289SKyle Evans 			 * non-printable characters to be safe.
268c3188289SKyle Evans 			 */
269c3188289SKyle Evans 			while (size > 0 && data[size - 1] <= 0x20) {
270b6be9566SColin Percival 				size--;
271c3188289SKyle Evans 			}
272d2ef3774SJessica Clarke 
273d2ef3774SJessica Clarke 			valid = false;
274d2ef3774SJessica Clarke 
275d2ef3774SJessica Clarke 			/*
276d2ef3774SJessica Clarke 			 * Not NUL-terminated when passed from loader, but
277d2ef3774SJessica Clarke 			 * validate_uuid requires that due to using sscanf (as
278d2ef3774SJessica Clarke 			 * does the subsequent strlcpy, since it still reads
279d2ef3774SJessica Clarke 			 * past the given size to return the true length);
280d2ef3774SJessica Clarke 			 * bounce to a temporary buffer to fix.
281d2ef3774SJessica Clarke 			 */
282d2ef3774SJessica Clarke 			if (size >= sizeof(buf))
283d2ef3774SJessica Clarke 				goto done;
284d2ef3774SJessica Clarke 
285d2ef3774SJessica Clarke 			memcpy(buf, data, size);
286d2ef3774SJessica Clarke 			buf[size] = '\0';
287d2ef3774SJessica Clarke 
288d2ef3774SJessica Clarke 			if (validate_uuid(buf, size, NULL, 0) != 0)
289d2ef3774SJessica Clarke 				goto done;
290d2ef3774SJessica Clarke 
291d2ef3774SJessica Clarke 			valid = true;
292d2ef3774SJessica Clarke 			(void)strlcpy(prison0.pr_hostuuid, buf,
293d2ef3774SJessica Clarke 			    sizeof(prison0.pr_hostuuid));
294d2ef3774SJessica Clarke 
295d2ef3774SJessica Clarke done:
296d2ef3774SJessica Clarke 			if (bootverbose && !valid) {
297330f110bSColin Percival 				printf("hostuuid: preload data malformed: '%.*s'\n",
298330f110bSColin Percival 				    (int)size, data);
299c3188289SKyle Evans 			}
300c3188289SKyle Evans 		}
301c3188289SKyle Evans 	}
302c3188289SKyle Evans 	if (bootverbose)
303c3188289SKyle Evans 		printf("hostuuid: using %s\n", prison0.pr_hostuuid);
304b96bd95bSIan Lepore }
305b96bd95bSIan Lepore 
306116734c4SMatthew Dillon /*
3079ddb7954SMike Barcroft  * struct jail_args {
3089ddb7954SMike Barcroft  *	struct jail *jail;
3099ddb7954SMike Barcroft  * };
310116734c4SMatthew Dillon  */
31175c13541SPoul-Henning Kamp int
312c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap)
31375c13541SPoul-Henning Kamp {
314413628a7SBjoern A. Zeeb 	uint32_t version;
315413628a7SBjoern A. Zeeb 	int error;
3160304c731SJamie Gritton 	struct jail j;
317413628a7SBjoern A. Zeeb 
318413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
319413628a7SBjoern A. Zeeb 	if (error)
320413628a7SBjoern A. Zeeb 		return (error);
321413628a7SBjoern A. Zeeb 
322413628a7SBjoern A. Zeeb 	switch (version) {
323413628a7SBjoern A. Zeeb 	case 0:
324413628a7SBjoern A. Zeeb 	{
325413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
326413628a7SBjoern A. Zeeb 
3270304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
3280304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
329413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
330413628a7SBjoern A. Zeeb 		if (error)
331413628a7SBjoern A. Zeeb 			return (error);
3320304c731SJamie Gritton 		j.version = j0.version;
3330304c731SJamie Gritton 		j.path = j0.path;
3340304c731SJamie Gritton 		j.hostname = j0.hostname;
335b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
336413628a7SBjoern A. Zeeb 		break;
337413628a7SBjoern A. Zeeb 	}
338413628a7SBjoern A. Zeeb 
339413628a7SBjoern A. Zeeb 	case 1:
340413628a7SBjoern A. Zeeb 		/*
341413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
342413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
343413628a7SBjoern A. Zeeb 		 */
344413628a7SBjoern A. Zeeb 		return (EINVAL);
345413628a7SBjoern A. Zeeb 
346413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
347413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
348413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
349413628a7SBjoern A. Zeeb 		if (error)
350413628a7SBjoern A. Zeeb 			return (error);
3510304c731SJamie Gritton 		break;
3520304c731SJamie Gritton 
3530304c731SJamie Gritton 	default:
3540304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3550304c731SJamie Gritton 		return (EINVAL);
3560304c731SJamie Gritton 	}
357c542c43eSJamie Gritton 	return (kern_jail(td, &j));
3580304c731SJamie Gritton }
3590304c731SJamie Gritton 
3600304c731SJamie Gritton int
361c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j)
3620304c731SJamie Gritton {
363c542c43eSJamie Gritton 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
364e92e0574SJamie Gritton #ifdef INET
365e92e0574SJamie Gritton 			    + 1
366e92e0574SJamie Gritton #endif
367e92e0574SJamie Gritton #ifdef INET6
368e92e0574SJamie Gritton 			    + 1
369e92e0574SJamie Gritton #endif
370e92e0574SJamie Gritton 			    )];
3710304c731SJamie Gritton 	struct uio opt;
3720304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
373672756aaSJamie Gritton 	struct bool_flags *bf;
3740304c731SJamie Gritton #ifdef INET
375e92e0574SJamie Gritton 	uint32_t ip4s;
3760304c731SJamie Gritton 	struct in_addr *u_ip4;
3770304c731SJamie Gritton #endif
3780304c731SJamie Gritton #ifdef INET6
3790304c731SJamie Gritton 	struct in6_addr *u_ip6;
3800304c731SJamie Gritton #endif
3810304c731SJamie Gritton 	size_t tmplen;
382c542c43eSJamie Gritton 	int error, enforce_statfs;
3830304c731SJamie Gritton 
3840304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3850304c731SJamie Gritton 	opt.uio_iov = optiov;
3860304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3870304c731SJamie Gritton 	opt.uio_offset = -1;
3880304c731SJamie Gritton 	opt.uio_resid = -1;
3890304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3900304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3910304c731SJamie Gritton 	opt.uio_td = td;
3920304c731SJamie Gritton 
3930304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3940304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
395672756aaSJamie Gritton 		for (bf = pr_flag_allow;
3960e5c6bd4SJamie Gritton 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
3975d58f959SJamie Gritton 			atomic_load_int(&bf->flag) != 0;
398672756aaSJamie Gritton 		     bf++) {
399672756aaSJamie Gritton 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
400672756aaSJamie Gritton 			    (jail_default_allow & bf->flag)
401672756aaSJamie Gritton 			    ? bf->name : bf->noname);
4020304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
4030304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
4040304c731SJamie Gritton 			opt.uio_iovcnt += 2;
4050304c731SJamie Gritton 		}
4060304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
4070304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
4080304c731SJamie Gritton 		opt.uio_iovcnt++;
4090304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
4100304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
4110304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
4120304c731SJamie Gritton 		opt.uio_iovcnt++;
4130304c731SJamie Gritton 	}
4140304c731SJamie Gritton 
415b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
416b38ff370SJamie Gritton #ifdef INET
4170304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
4180304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
419b38ff370SJamie Gritton 		return (EINVAL);
4200304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
421b38ff370SJamie Gritton #else
4220304c731SJamie Gritton 	if (j->ip4s > 0)
423b38ff370SJamie Gritton 		return (EINVAL);
424b38ff370SJamie Gritton #endif
425b38ff370SJamie Gritton #ifdef INET6
4260304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
427b38ff370SJamie Gritton 		return (EINVAL);
4280304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
429b38ff370SJamie Gritton #else
4300304c731SJamie Gritton 	if (j->ip6s > 0)
431b38ff370SJamie Gritton 		return (EINVAL);
432b38ff370SJamie Gritton #endif
433b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
434b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
435b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
436b38ff370SJamie Gritton #ifdef INET
437b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
438b38ff370SJamie Gritton #endif
439b38ff370SJamie Gritton #ifdef INET6
440b38ff370SJamie Gritton #ifdef INET
4410304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
442b38ff370SJamie Gritton #else
443b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
444b38ff370SJamie Gritton #endif
445b38ff370SJamie Gritton #endif
4460304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4470304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4480304c731SJamie Gritton 	opt.uio_iovcnt++;
4490304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4500304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4510304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
452b38ff370SJamie Gritton 	if (error) {
453b38ff370SJamie Gritton 		free(u_path, M_TEMP);
454b38ff370SJamie Gritton 		return (error);
455b38ff370SJamie Gritton 	}
4560304c731SJamie Gritton 	opt.uio_iovcnt++;
4570304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4580304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4590304c731SJamie Gritton 	opt.uio_iovcnt++;
4600304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4610304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4620304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
463b38ff370SJamie Gritton 	if (error) {
464b38ff370SJamie Gritton 		free(u_path, M_TEMP);
465b38ff370SJamie Gritton 		return (error);
466b38ff370SJamie Gritton 	}
4670304c731SJamie Gritton 	opt.uio_iovcnt++;
4680304c731SJamie Gritton 	if (j->jailname != NULL) {
469b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
470b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
471b38ff370SJamie Gritton 		opt.uio_iovcnt++;
472b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4730304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
474b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
475b38ff370SJamie Gritton 		if (error) {
476b38ff370SJamie Gritton 			free(u_path, M_TEMP);
477b38ff370SJamie Gritton 			return (error);
478b38ff370SJamie Gritton 		}
479b38ff370SJamie Gritton 		opt.uio_iovcnt++;
480b38ff370SJamie Gritton 	}
481b38ff370SJamie Gritton #ifdef INET
482b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
483b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
484b38ff370SJamie Gritton 	opt.uio_iovcnt++;
485b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4860304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4870304c731SJamie Gritton 	if (j->version == 0)
4880304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4890304c731SJamie Gritton 	else {
4900304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
491b38ff370SJamie Gritton 		if (error) {
492b38ff370SJamie Gritton 			free(u_path, M_TEMP);
493b38ff370SJamie Gritton 			return (error);
494b38ff370SJamie Gritton 		}
4950304c731SJamie Gritton 	}
496b38ff370SJamie Gritton 	opt.uio_iovcnt++;
497b38ff370SJamie Gritton #endif
498b38ff370SJamie Gritton #ifdef INET6
499b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
500b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
501b38ff370SJamie Gritton 	opt.uio_iovcnt++;
502b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
5030304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
5040304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
505b38ff370SJamie Gritton 	if (error) {
506b38ff370SJamie Gritton 		free(u_path, M_TEMP);
507b38ff370SJamie Gritton 		return (error);
508b38ff370SJamie Gritton 	}
509b38ff370SJamie Gritton 	opt.uio_iovcnt++;
510b38ff370SJamie Gritton #endif
51102abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
5120304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
513b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
514b38ff370SJamie Gritton 	free(u_path, M_TEMP);
515b38ff370SJamie Gritton 	return (error);
516b38ff370SJamie Gritton }
517b38ff370SJamie Gritton 
518b38ff370SJamie Gritton /*
519b38ff370SJamie Gritton  * struct jail_set_args {
520b38ff370SJamie Gritton  *	struct iovec *iovp;
521b38ff370SJamie Gritton  *	unsigned int iovcnt;
522b38ff370SJamie Gritton  *	int flags;
523b38ff370SJamie Gritton  * };
524b38ff370SJamie Gritton  */
525b38ff370SJamie Gritton int
5268451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
527b38ff370SJamie Gritton {
528b38ff370SJamie Gritton 	struct uio *auio;
529b38ff370SJamie Gritton 	int error;
530b38ff370SJamie Gritton 
531b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
532b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
533b38ff370SJamie Gritton 		return (EINVAL);
534b38ff370SJamie Gritton 
535b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
536b38ff370SJamie Gritton 	if (error)
537b38ff370SJamie Gritton 		return (error);
538b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
539b38ff370SJamie Gritton 	free(auio, M_IOV);
540b38ff370SJamie Gritton 	return (error);
541413628a7SBjoern A. Zeeb }
542413628a7SBjoern A. Zeeb 
543eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
544eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *);
545eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *);
546eb8dcdeaSGleb Smirnoff static const struct pr_family {
547eb8dcdeaSGleb Smirnoff 	size_t			size;
548eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t	*cmp;
549eb8dcdeaSGleb Smirnoff 	prison_addr_valid_t	*valid;
550eb8dcdeaSGleb Smirnoff 	int			ip_flag;
551eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = {
552eb8dcdeaSGleb Smirnoff #ifdef INET
553eb8dcdeaSGleb Smirnoff 	[PR_INET] = {
554eb8dcdeaSGleb Smirnoff 		.size = sizeof(struct in_addr),
555eb8dcdeaSGleb Smirnoff 		.cmp = prison_qcmp_v4,
556eb8dcdeaSGleb Smirnoff 		.valid = prison_valid_v4,
557eb8dcdeaSGleb Smirnoff 		.ip_flag = PR_IP4_USER,
558eb8dcdeaSGleb Smirnoff 	 },
559eb8dcdeaSGleb Smirnoff #endif
560eb8dcdeaSGleb Smirnoff #ifdef INET6
561eb8dcdeaSGleb Smirnoff 	[PR_INET6] = {
562eb8dcdeaSGleb Smirnoff 		.size = sizeof(struct in6_addr),
563eb8dcdeaSGleb Smirnoff 		.cmp = prison_qcmp_v6,
564eb8dcdeaSGleb Smirnoff 		.valid = prison_valid_v6,
565eb8dcdeaSGleb Smirnoff 		.ip_flag = PR_IP6_USER,
566eb8dcdeaSGleb Smirnoff 	},
567eb8dcdeaSGleb Smirnoff #endif
568eb8dcdeaSGleb Smirnoff };
569eb8dcdeaSGleb Smirnoff 
570eb8dcdeaSGleb Smirnoff /*
571eb8dcdeaSGleb Smirnoff  * Network address lists (pr_addrs) allocation for jails.  The addresses
572eb8dcdeaSGleb Smirnoff  * are accessed locklessly by the network stack, thus need to be protected by
573eb8dcdeaSGleb Smirnoff  * the network epoch.
574eb8dcdeaSGleb Smirnoff  */
575eb8dcdeaSGleb Smirnoff struct prison_ip {
576eb8dcdeaSGleb Smirnoff 	struct epoch_context ctx;
577eb8dcdeaSGleb Smirnoff 	uint32_t	ips;
578eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C
579500f82d6SZhenlei Huang 	/*
580500f82d6SZhenlei Huang 	 * XXX Variable-length automatic arrays in union may be
581500f82d6SZhenlei Huang 	 * supported in future C.
582500f82d6SZhenlei Huang 	 */
583eb8dcdeaSGleb Smirnoff 	union {
584500f82d6SZhenlei Huang 		char pr_ip[];
585eb8dcdeaSGleb Smirnoff 		struct in_addr pr_ip4[];
586eb8dcdeaSGleb Smirnoff 		struct in6_addr pr_ip6[];
587eb8dcdeaSGleb Smirnoff 	};
588eb8dcdeaSGleb Smirnoff #else /* No future C :( */
589500f82d6SZhenlei Huang 	char pr_ip[];
590eb8dcdeaSGleb Smirnoff #endif
591eb8dcdeaSGleb Smirnoff };
592eb8dcdeaSGleb Smirnoff 
593500f82d6SZhenlei Huang static char *
594500f82d6SZhenlei Huang PR_IP(struct prison_ip *pip, const pr_family_t af, int idx)
595500f82d6SZhenlei Huang {
596500f82d6SZhenlei Huang 	MPASS(pip);
597500f82d6SZhenlei Huang 	MPASS(af < PR_FAMILY_MAX);
598500f82d6SZhenlei Huang 	MPASS(idx >= 0 && idx < pip->ips);
599500f82d6SZhenlei Huang 
600500f82d6SZhenlei Huang 	return (pip->pr_ip + pr_families[af].size * idx);
601500f82d6SZhenlei Huang }
602500f82d6SZhenlei Huang 
603eb8dcdeaSGleb Smirnoff static struct prison_ip *
604eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
605eb8dcdeaSGleb Smirnoff {
606eb8dcdeaSGleb Smirnoff 	struct prison_ip *pip;
607eb8dcdeaSGleb Smirnoff 
608eb8dcdeaSGleb Smirnoff 	pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
609eb8dcdeaSGleb Smirnoff 	    M_PRISON, flags);
610eb8dcdeaSGleb Smirnoff 	if (pip != NULL)
611eb8dcdeaSGleb Smirnoff 		pip->ips = cnt;
612eb8dcdeaSGleb Smirnoff 	return (pip);
613eb8dcdeaSGleb Smirnoff }
614eb8dcdeaSGleb Smirnoff 
615eb8dcdeaSGleb Smirnoff /*
616eb8dcdeaSGleb Smirnoff  * Allocate and copyin user supplied address list, sorting and validating.
617eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
618eb8dcdeaSGleb Smirnoff  */
619eb8dcdeaSGleb Smirnoff static struct prison_ip *
620eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
621eb8dcdeaSGleb Smirnoff {
622eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
623eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
624eb8dcdeaSGleb Smirnoff 	struct prison_ip *pip;
625eb8dcdeaSGleb Smirnoff 
626eb8dcdeaSGleb Smirnoff 	pip = prison_ip_alloc(af, cnt, M_WAITOK);
627500f82d6SZhenlei Huang 	bcopy(op, pip->pr_ip, cnt * size);
628eb8dcdeaSGleb Smirnoff 	/*
629eb8dcdeaSGleb Smirnoff 	 * IP addresses are all sorted but ip[0] to preserve
630eb8dcdeaSGleb Smirnoff 	 * the primary IP address as given from userland.
631eb8dcdeaSGleb Smirnoff 	 * This special IP is used for unbound outgoing
632eb8dcdeaSGleb Smirnoff 	 * connections as well for "loopback" traffic in case
633eb8dcdeaSGleb Smirnoff 	 * source address selection cannot find any more fitting
634eb8dcdeaSGleb Smirnoff 	 * address to connect from.
635eb8dcdeaSGleb Smirnoff 	 */
636eb8dcdeaSGleb Smirnoff 	if (cnt > 1)
6372c33b456SZhenlei Huang 		qsort(PR_IP(pip, af, 1), cnt - 1, size, cmp);
638eb8dcdeaSGleb Smirnoff 	/*
639eb8dcdeaSGleb Smirnoff 	 * Check for duplicate addresses and do some simple
640eb8dcdeaSGleb Smirnoff 	 * zero and broadcast checks. If users give other bogus
641eb8dcdeaSGleb Smirnoff 	 * addresses it is their problem.
642eb8dcdeaSGleb Smirnoff 	 */
643eb8dcdeaSGleb Smirnoff 	for (int i = 0; i < cnt; i++) {
644500f82d6SZhenlei Huang 		if (!pr_families[af].valid(PR_IP(pip, af, i))) {
645eb8dcdeaSGleb Smirnoff 			free(pip, M_PRISON);
646eb8dcdeaSGleb Smirnoff 			return (NULL);
647eb8dcdeaSGleb Smirnoff 		}
648eb8dcdeaSGleb Smirnoff 		if (i + 1 < cnt &&
649500f82d6SZhenlei Huang 		    (cmp(PR_IP(pip, af, 0), PR_IP(pip, af, i + 1)) == 0 ||
650500f82d6SZhenlei Huang 		     cmp(PR_IP(pip, af, i), PR_IP(pip, af, i + 1)) == 0)) {
651eb8dcdeaSGleb Smirnoff 			free(pip, M_PRISON);
652eb8dcdeaSGleb Smirnoff 			return (NULL);
653eb8dcdeaSGleb Smirnoff 		}
654eb8dcdeaSGleb Smirnoff 	}
655eb8dcdeaSGleb Smirnoff 
656eb8dcdeaSGleb Smirnoff 	return (pip);
657eb8dcdeaSGleb Smirnoff }
658eb8dcdeaSGleb Smirnoff 
659eb8dcdeaSGleb Smirnoff /*
660eb8dcdeaSGleb Smirnoff  * Allocate and dup parent prison address list.
661eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
662eb8dcdeaSGleb Smirnoff  */
663eb8dcdeaSGleb Smirnoff static void
664eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
665eb8dcdeaSGleb Smirnoff {
6662c33b456SZhenlei Huang 	const struct prison_ip *ppip = ppr->pr_addrs[af];
6672c33b456SZhenlei Huang 	struct prison_ip *pip;
668eb8dcdeaSGleb Smirnoff 
6692c33b456SZhenlei Huang 	if (ppip != NULL) {
6702c33b456SZhenlei Huang 		pip = prison_ip_alloc(af, ppip->ips, M_WAITOK);
6712c33b456SZhenlei Huang 		bcopy(ppip->pr_ip, pip->pr_ip, pip->ips * pr_families[af].size);
6722c33b456SZhenlei Huang 		pr->pr_addrs[af] = pip;
673eb8dcdeaSGleb Smirnoff 	}
674eb8dcdeaSGleb Smirnoff }
675eb8dcdeaSGleb Smirnoff 
676eb8dcdeaSGleb Smirnoff /*
677eb8dcdeaSGleb Smirnoff  * Make sure the new set of IP addresses is a subset of the parent's list.
678eb8dcdeaSGleb Smirnoff  * Don't worry about the parent being unlocked, as any setting is done with
679eb8dcdeaSGleb Smirnoff  * allprison_lock held.
680eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
681eb8dcdeaSGleb Smirnoff  */
682eb8dcdeaSGleb Smirnoff static bool
683500f82d6SZhenlei Huang prison_ip_parent_match(struct prison_ip *ppip, struct prison_ip *pip,
684500f82d6SZhenlei Huang     const pr_family_t af)
685eb8dcdeaSGleb Smirnoff {
686eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
687eb8dcdeaSGleb Smirnoff 	int i, j;
688eb8dcdeaSGleb Smirnoff 
689eb8dcdeaSGleb Smirnoff 	if (ppip == NULL)
690eb8dcdeaSGleb Smirnoff 		return (false);
691eb8dcdeaSGleb Smirnoff 
692eb8dcdeaSGleb Smirnoff 	for (i = 0; i < ppip->ips; i++)
693500f82d6SZhenlei Huang 		if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, i)) == 0)
694eb8dcdeaSGleb Smirnoff 			break;
695eb8dcdeaSGleb Smirnoff 
696eb8dcdeaSGleb Smirnoff 	if (i == ppip->ips)
697eb8dcdeaSGleb Smirnoff 		/* Main address not present in parent. */
698eb8dcdeaSGleb Smirnoff 		return (false);
699eb8dcdeaSGleb Smirnoff 
700eb8dcdeaSGleb Smirnoff 	if (pip->ips > 1) {
701eb8dcdeaSGleb Smirnoff 		for (i = j = 1; i < pip->ips; i++) {
702500f82d6SZhenlei Huang 			if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0)
703eb8dcdeaSGleb Smirnoff 				/* Equals to parent primary address. */
704eb8dcdeaSGleb Smirnoff 				continue;
705eb8dcdeaSGleb Smirnoff 			for (; j < ppip->ips; j++)
706500f82d6SZhenlei Huang 				if (cmp(PR_IP(pip, af, i),
707500f82d6SZhenlei Huang 				    PR_IP(ppip, af, j)) == 0)
708eb8dcdeaSGleb Smirnoff 					break;
709eb8dcdeaSGleb Smirnoff 			if (j == ppip->ips)
710eb8dcdeaSGleb Smirnoff 				break;
711eb8dcdeaSGleb Smirnoff 		}
712eb8dcdeaSGleb Smirnoff 		if (j == ppip->ips)
713eb8dcdeaSGleb Smirnoff 			/* Address not present in parent. */
714eb8dcdeaSGleb Smirnoff 			return (false);
715eb8dcdeaSGleb Smirnoff 	}
716eb8dcdeaSGleb Smirnoff 	return (true);
717eb8dcdeaSGleb Smirnoff }
718eb8dcdeaSGleb Smirnoff 
719eb8dcdeaSGleb Smirnoff /*
720eb8dcdeaSGleb Smirnoff  * Check for conflicting IP addresses.  We permit them if there is no more
721eb8dcdeaSGleb Smirnoff  * than one IP on each jail.  If there is a duplicate on a jail with more
722eb8dcdeaSGleb Smirnoff  * than one IP stop checking and return error.
723eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
724eb8dcdeaSGleb Smirnoff  */
725eb8dcdeaSGleb Smirnoff static bool
726eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
727500f82d6SZhenlei Huang     struct prison_ip *pip, pr_family_t af)
728eb8dcdeaSGleb Smirnoff {
729eb8dcdeaSGleb Smirnoff 	const struct prison *tppr, *tpr;
730eb8dcdeaSGleb Smirnoff 	int descend;
731eb8dcdeaSGleb Smirnoff 
732eb8dcdeaSGleb Smirnoff #ifdef VIMAGE
733eb8dcdeaSGleb Smirnoff 	for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
734eb8dcdeaSGleb Smirnoff 		if (tppr->pr_flags & PR_VNET)
735eb8dcdeaSGleb Smirnoff 			break;
736eb8dcdeaSGleb Smirnoff #else
737eb8dcdeaSGleb Smirnoff 	tppr = &prison0;
738eb8dcdeaSGleb Smirnoff #endif
739eb8dcdeaSGleb Smirnoff 	FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
740eb8dcdeaSGleb Smirnoff 		if (tpr == pr ||
741eb8dcdeaSGleb Smirnoff #ifdef VIMAGE
742eb8dcdeaSGleb Smirnoff 		    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
743eb8dcdeaSGleb Smirnoff #endif
744eb8dcdeaSGleb Smirnoff 		    !prison_isalive(tpr)) {
745eb8dcdeaSGleb Smirnoff 			descend = 0;
746eb8dcdeaSGleb Smirnoff 			continue;
747eb8dcdeaSGleb Smirnoff 		}
748eb8dcdeaSGleb Smirnoff 		if (!(tpr->pr_flags & pr_families[af].ip_flag))
749eb8dcdeaSGleb Smirnoff 			continue;
750eb8dcdeaSGleb Smirnoff 		descend = 0;
751eb8dcdeaSGleb Smirnoff 		if (tpr->pr_addrs[af] == NULL ||
752eb8dcdeaSGleb Smirnoff 		    (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
753eb8dcdeaSGleb Smirnoff 			continue;
754eb8dcdeaSGleb Smirnoff 		for (int i = 0; i < pip->ips; i++)
755500f82d6SZhenlei Huang 			if (prison_ip_check(tpr, af, PR_IP(pip, af, i)) == 0)
756eb8dcdeaSGleb Smirnoff 				return (false);
757eb8dcdeaSGleb Smirnoff 	}
758eb8dcdeaSGleb Smirnoff 
759eb8dcdeaSGleb Smirnoff 	return (true);
760eb8dcdeaSGleb Smirnoff }
761eb8dcdeaSGleb Smirnoff 
762eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0,
763eb8dcdeaSGleb Smirnoff     "prison must start with epoch context");
764eb8dcdeaSGleb Smirnoff static void
765eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx)
766eb8dcdeaSGleb Smirnoff {
767eb8dcdeaSGleb Smirnoff 
768eb8dcdeaSGleb Smirnoff 	free(ctx, M_PRISON);
769eb8dcdeaSGleb Smirnoff }
770eb8dcdeaSGleb Smirnoff 
771eb8dcdeaSGleb Smirnoff static void
772eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip)
773eb8dcdeaSGleb Smirnoff {
774eb8dcdeaSGleb Smirnoff 
775eb8dcdeaSGleb Smirnoff 	if (pip != NULL)
776eb8dcdeaSGleb Smirnoff 		NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
777eb8dcdeaSGleb Smirnoff }
778eb8dcdeaSGleb Smirnoff 
779eb8dcdeaSGleb Smirnoff static void
780eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
781eb8dcdeaSGleb Smirnoff {
782eb8dcdeaSGleb Smirnoff 	struct prison_ip **mem, *old;
783eb8dcdeaSGleb Smirnoff 
784eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
785eb8dcdeaSGleb Smirnoff 
786eb8dcdeaSGleb Smirnoff 	mem = &pr->pr_addrs[af];
787eb8dcdeaSGleb Smirnoff 
788eb8dcdeaSGleb Smirnoff 	old = *mem;
78927202b98SMark Johnston 	atomic_store_ptr(mem, new);
790eb8dcdeaSGleb Smirnoff 	prison_ip_free(old);
791eb8dcdeaSGleb Smirnoff }
792eb8dcdeaSGleb Smirnoff 
793eb8dcdeaSGleb Smirnoff /*
794eb8dcdeaSGleb Smirnoff  * Restrict a prison's IP address list with its parent's, possibly replacing
7958bce8d28SZhenlei Huang  * it.  Return true if succeed, otherwise should redo.
796eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
797eb8dcdeaSGleb Smirnoff  */
798eb8dcdeaSGleb Smirnoff static bool
799eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af,
8008bce8d28SZhenlei Huang     struct prison_ip **newp)
801eb8dcdeaSGleb Smirnoff {
802500f82d6SZhenlei Huang 	struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
803500f82d6SZhenlei Huang 	struct prison_ip *pip = pr->pr_addrs[af];
804eb8dcdeaSGleb Smirnoff 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
805eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
8068bce8d28SZhenlei Huang 	struct prison_ip *new = newp != NULL ? *newp : NULL;
807eb8dcdeaSGleb Smirnoff 	uint32_t ips;
808eb8dcdeaSGleb Smirnoff 
809eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
810eb8dcdeaSGleb Smirnoff 
811eb8dcdeaSGleb Smirnoff 	/*
812eb8dcdeaSGleb Smirnoff 	 * Due to epoch-synchronized access to the IP address lists we always
813eb8dcdeaSGleb Smirnoff 	 * allocate a new list even if the old one has enough space.  We could
814eb8dcdeaSGleb Smirnoff 	 * atomically update an IPv4 address inside a list, but that would
815eb8dcdeaSGleb Smirnoff 	 * screw up sorting, and in case of IPv6 we can't even atomically write
816eb8dcdeaSGleb Smirnoff 	 * one.
817eb8dcdeaSGleb Smirnoff 	 */
81889ddfbbaSZhenlei Huang 	if (ppip == NULL) {
81989ddfbbaSZhenlei Huang 		if (pip != NULL)
820eb8dcdeaSGleb Smirnoff 			prison_ip_set(pr, af, NULL);
8218bce8d28SZhenlei Huang 		return (true);
822eb8dcdeaSGleb Smirnoff 	}
82389ddfbbaSZhenlei Huang 
824eb8dcdeaSGleb Smirnoff 	if (!(pr->pr_flags & pr_families[af].ip_flag)) {
82589ddfbbaSZhenlei Huang 		if (new == NULL) {
82689ddfbbaSZhenlei Huang 			new = prison_ip_alloc(af, ppip->ips, M_NOWAIT);
82789ddfbbaSZhenlei Huang 			if (new == NULL)
8288bce8d28SZhenlei Huang 				return (false); /* Redo */
8298bce8d28SZhenlei Huang 		}
830eb8dcdeaSGleb Smirnoff 		/* This has no user settings, so just copy the parent's list. */
83189ddfbbaSZhenlei Huang 		MPASS(new->ips == ppip->ips);
832500f82d6SZhenlei Huang 		bcopy(ppip->pr_ip, new->pr_ip, ppip->ips * size);
83389ddfbbaSZhenlei Huang 		prison_ip_set(pr, af, new);
8348bce8d28SZhenlei Huang 		if (newp != NULL)
8358bce8d28SZhenlei Huang 			*newp = NULL; /* Used */
83689ddfbbaSZhenlei Huang 	} else if (pip != NULL) {
837eb8dcdeaSGleb Smirnoff 		/* Remove addresses that aren't in the parent. */
838eb8dcdeaSGleb Smirnoff 		int i;
839eb8dcdeaSGleb Smirnoff 
840eb8dcdeaSGleb Smirnoff 		i = 0; /* index in pip */
841eb8dcdeaSGleb Smirnoff 		ips = 0; /* index in new */
842eb8dcdeaSGleb Smirnoff 
84389ddfbbaSZhenlei Huang 		if (new == NULL) {
84489ddfbbaSZhenlei Huang 			new = prison_ip_alloc(af, pip->ips, M_NOWAIT);
84589ddfbbaSZhenlei Huang 			if (new == NULL)
8468bce8d28SZhenlei Huang 				return (false); /* Redo */
84789ddfbbaSZhenlei Huang 		}
84889ddfbbaSZhenlei Huang 
849eb8dcdeaSGleb Smirnoff 		for (int pi = 0; pi < ppip->ips; pi++)
850500f82d6SZhenlei Huang 			if (cmp(PR_IP(pip, af, 0), PR_IP(ppip, af, pi)) == 0) {
851eb8dcdeaSGleb Smirnoff 				/* Found our primary address in parent. */
852500f82d6SZhenlei Huang 				bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
853500f82d6SZhenlei Huang 				    size);
854eb8dcdeaSGleb Smirnoff 				i++;
855eb8dcdeaSGleb Smirnoff 				ips++;
856eb8dcdeaSGleb Smirnoff 				break;
857eb8dcdeaSGleb Smirnoff 			}
858eb8dcdeaSGleb Smirnoff 		for (int pi = 1; i < pip->ips; ) {
859eb8dcdeaSGleb Smirnoff 			/* Check against primary, which is unsorted. */
860500f82d6SZhenlei Huang 			if (cmp(PR_IP(pip, af, i), PR_IP(ppip, af, 0)) == 0) {
861eb8dcdeaSGleb Smirnoff 				/* Matches parent's primary address. */
862500f82d6SZhenlei Huang 				bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
863500f82d6SZhenlei Huang 				    size);
864eb8dcdeaSGleb Smirnoff 				i++;
865eb8dcdeaSGleb Smirnoff 				ips++;
866eb8dcdeaSGleb Smirnoff 				continue;
867eb8dcdeaSGleb Smirnoff 			}
868eb8dcdeaSGleb Smirnoff 			/* The rest are sorted. */
869eb8dcdeaSGleb Smirnoff 			switch (pi >= ppip->ips ? -1 :
870500f82d6SZhenlei Huang 				cmp(PR_IP(pip, af, i), PR_IP(ppip, af, pi))) {
871eb8dcdeaSGleb Smirnoff 			case -1:
872eb8dcdeaSGleb Smirnoff 				i++;
873eb8dcdeaSGleb Smirnoff 				break;
874eb8dcdeaSGleb Smirnoff 			case 0:
875500f82d6SZhenlei Huang 				bcopy(PR_IP(pip, af, i), PR_IP(new, af, ips),
876500f82d6SZhenlei Huang 				    size);
877eb8dcdeaSGleb Smirnoff 				i++;
878eb8dcdeaSGleb Smirnoff 				pi++;
879eb8dcdeaSGleb Smirnoff 				ips++;
880eb8dcdeaSGleb Smirnoff 				break;
881eb8dcdeaSGleb Smirnoff 			case 1:
882eb8dcdeaSGleb Smirnoff 				pi++;
883eb8dcdeaSGleb Smirnoff 				break;
884eb8dcdeaSGleb Smirnoff 			}
885eb8dcdeaSGleb Smirnoff 		}
886eb8dcdeaSGleb Smirnoff 		if (ips == 0) {
8878bce8d28SZhenlei Huang 			if (newp == NULL || *newp == NULL)
888eb8dcdeaSGleb Smirnoff 				prison_ip_free(new);
889eb8dcdeaSGleb Smirnoff 			new = NULL;
89089ddfbbaSZhenlei Huang 		} else {
89189ddfbbaSZhenlei Huang 			/* Shrink to real size */
89289ddfbbaSZhenlei Huang 			KASSERT((new->ips >= ips),
89389ddfbbaSZhenlei Huang 			    ("Out-of-bounds write to prison_ip %p", new));
89489ddfbbaSZhenlei Huang 			new->ips = ips;
895eb8dcdeaSGleb Smirnoff 		}
896eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, af, new);
8978bce8d28SZhenlei Huang 		if (newp != NULL)
8988bce8d28SZhenlei Huang 			*newp = NULL; /* Used */
89989ddfbbaSZhenlei Huang 	}
9008bce8d28SZhenlei Huang 	return (true);
901eb8dcdeaSGleb Smirnoff }
902eb8dcdeaSGleb Smirnoff 
903eb8dcdeaSGleb Smirnoff /*
904eb8dcdeaSGleb Smirnoff  * Fast-path check if an address belongs to a prison.
905eb8dcdeaSGleb Smirnoff  */
906eb8dcdeaSGleb Smirnoff int
907eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af,
908eb8dcdeaSGleb Smirnoff     const void *addr)
909eb8dcdeaSGleb Smirnoff {
910eb8dcdeaSGleb Smirnoff 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
911500f82d6SZhenlei Huang 	struct prison_ip *pip;
912eb8dcdeaSGleb Smirnoff 	int i, a, z, d;
913eb8dcdeaSGleb Smirnoff 
914eb8dcdeaSGleb Smirnoff 	MPASS(mtx_owned(&pr->pr_mtx) ||
915eb8dcdeaSGleb Smirnoff 	    in_epoch(net_epoch_preempt) ||
916eb8dcdeaSGleb Smirnoff 	    sx_xlocked(&allprison_lock));
917eb8dcdeaSGleb Smirnoff 
91827202b98SMark Johnston 	pip = atomic_load_ptr(&pr->pr_addrs[af]);
919eb8dcdeaSGleb Smirnoff 	if (__predict_false(pip == NULL))
920eb8dcdeaSGleb Smirnoff 		return (EAFNOSUPPORT);
921eb8dcdeaSGleb Smirnoff 
922eb8dcdeaSGleb Smirnoff 	/* Check the primary IP. */
923500f82d6SZhenlei Huang 	if (cmp(PR_IP(pip, af, 0), addr) == 0)
924eb8dcdeaSGleb Smirnoff 		return (0);
925eb8dcdeaSGleb Smirnoff 
926eb8dcdeaSGleb Smirnoff 	/*
927eb8dcdeaSGleb Smirnoff 	 * All the other IPs are sorted so we can do a binary search.
928eb8dcdeaSGleb Smirnoff 	 */
929eb8dcdeaSGleb Smirnoff 	a = 0;
930eb8dcdeaSGleb Smirnoff 	z = pip->ips - 2;
931eb8dcdeaSGleb Smirnoff 	while (a <= z) {
932eb8dcdeaSGleb Smirnoff 		i = (a + z) / 2;
933500f82d6SZhenlei Huang 		d = cmp(PR_IP(pip, af, i + 1), addr);
934eb8dcdeaSGleb Smirnoff 		if (d > 0)
935eb8dcdeaSGleb Smirnoff 			z = i - 1;
936eb8dcdeaSGleb Smirnoff 		else if (d < 0)
937eb8dcdeaSGleb Smirnoff 			a = i + 1;
938eb8dcdeaSGleb Smirnoff 		else
939eb8dcdeaSGleb Smirnoff 			return (0);
940eb8dcdeaSGleb Smirnoff 	}
941eb8dcdeaSGleb Smirnoff 
942eb8dcdeaSGleb Smirnoff 	return (EADDRNOTAVAIL);
943eb8dcdeaSGleb Smirnoff }
944eb8dcdeaSGleb Smirnoff 
945eb8dcdeaSGleb Smirnoff /*
946eb8dcdeaSGleb Smirnoff  * Grab primary IP.  Historically required mutex, but nothing prevents
947eb8dcdeaSGleb Smirnoff  * us to support epoch-protected access.  Is it used in fast path?
948eb8dcdeaSGleb Smirnoff  * in{6}_jail.c helper
949eb8dcdeaSGleb Smirnoff  */
950eb8dcdeaSGleb Smirnoff const void *
951eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af)
952eb8dcdeaSGleb Smirnoff {
953eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip = pr->pr_addrs[af];
954eb8dcdeaSGleb Smirnoff 
955eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
956eb8dcdeaSGleb Smirnoff 	MPASS(pip);
957eb8dcdeaSGleb Smirnoff 
958500f82d6SZhenlei Huang 	return (pip->pr_ip);
959eb8dcdeaSGleb Smirnoff }
960eb8dcdeaSGleb Smirnoff 
961eb8dcdeaSGleb Smirnoff u_int
962eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af)
963eb8dcdeaSGleb Smirnoff {
964eb8dcdeaSGleb Smirnoff 
965eb8dcdeaSGleb Smirnoff 	return (pr->pr_addrs[af]->ips);
966eb8dcdeaSGleb Smirnoff }
967eb8dcdeaSGleb Smirnoff #endif	/* defined(INET) || defined(INET6) */
968eb8dcdeaSGleb Smirnoff 
969413628a7SBjoern A. Zeeb int
970b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
971413628a7SBjoern A. Zeeb {
972fd7a8150SMike Barcroft 	struct nameidata nd;
973b38ff370SJamie Gritton #ifdef INET
974eb8dcdeaSGleb Smirnoff 	struct prison_ip *ip4;
975413628a7SBjoern A. Zeeb #endif
976b38ff370SJamie Gritton #ifdef INET6
977eb8dcdeaSGleb Smirnoff 	struct prison_ip *ip6;
978b38ff370SJamie Gritton #endif
979b38ff370SJamie Gritton 	struct vfsopt *opt;
980b38ff370SJamie Gritton 	struct vfsoptlist *opts;
9817de883c8SJamie Gritton 	struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
982b38ff370SJamie Gritton 	struct vnode *root;
983babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
984b96bd95bSIan Lepore 	char *g_path, *osrelstr;
985672756aaSJamie Gritton 	struct bool_flags *bf;
986672756aaSJamie Gritton 	struct jailsys_flags *jsf;
9870304c731SJamie Gritton #if defined(INET) || defined(INET6)
988b38ff370SJamie Gritton 	void *op;
9890304c731SJamie Gritton #endif
99076ca6f88SJamie Gritton 	unsigned long hid;
991b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
9922a4b2251SJamie Gritton 	int born, created, cuflags, descend, drflags, enforce;
993cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
9940cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
995672756aaSJamie Gritton 	int jid, jsys, len, level;
996b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
997413628a7SBjoern A. Zeeb #ifdef INET
9988bce8d28SZhenlei Huang 	int ip4s;
9998bce8d28SZhenlei Huang 	bool redo_ip4;
1000413628a7SBjoern A. Zeeb #endif
1001b38ff370SJamie Gritton #ifdef INET6
10028bce8d28SZhenlei Huang 	int ip6s;
10038bce8d28SZhenlei Huang 	bool redo_ip6;
1004b38ff370SJamie Gritton #endif
10056beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
1006b3079544SJamie Gritton 	uint64_t pr_allow_diff;
10076beb3bb4SKirk McKusick 	unsigned tallow;
1008b38ff370SJamie Gritton 	char numbuf[12];
1009b38ff370SJamie Gritton 
1010b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
1011b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
1012b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
1013b38ff370SJamie Gritton 	if (error)
101475c13541SPoul-Henning Kamp 		return (error);
1015b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
1016b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
10170304c731SJamie Gritton 		return (EPERM);
1018b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
1019b38ff370SJamie Gritton 		return (EINVAL);
1020b38ff370SJamie Gritton 
1021b38ff370SJamie Gritton 	/*
1022b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
1023b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
1024b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
1025b38ff370SJamie Gritton 	 * the allprison lock.
1026b38ff370SJamie Gritton 	 *
1027b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
1028b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
1029b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
1030b38ff370SJamie Gritton 	 */
1031b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1032b38ff370SJamie Gritton 	if (error)
1033b38ff370SJamie Gritton 		return (error);
1034b38ff370SJamie Gritton #ifdef INET
1035b38ff370SJamie Gritton 	ip4 = NULL;
1036b38ff370SJamie Gritton #endif
1037b38ff370SJamie Gritton #ifdef INET6
1038b38ff370SJamie Gritton 	ip6 = NULL;
1039b38ff370SJamie Gritton #endif
10406dfe0a3dSMartin Matuska 	g_path = NULL;
1041b38ff370SJamie Gritton 
1042b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1043b6f47c23SJamie Gritton 	if (!cuflags) {
1044b6f47c23SJamie Gritton 		error = EINVAL;
1045b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
1046b6f47c23SJamie Gritton 		goto done_errmsg;
1047b6f47c23SJamie Gritton 	}
1048b6f47c23SJamie Gritton 
1049b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1050b38ff370SJamie Gritton 	if (error == ENOENT)
1051b38ff370SJamie Gritton 		jid = 0;
1052b38ff370SJamie Gritton 	else if (error != 0)
1053b38ff370SJamie Gritton 		goto done_free;
1054b38ff370SJamie Gritton 
1055b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1056b38ff370SJamie Gritton 	if (error == ENOENT)
1057b38ff370SJamie Gritton 		gotslevel = 0;
1058b38ff370SJamie Gritton 	else if (error != 0)
1059b38ff370SJamie Gritton 		goto done_free;
1060b38ff370SJamie Gritton 	else
1061b38ff370SJamie Gritton 		gotslevel = 1;
1062b38ff370SJamie Gritton 
1063b97457e2SJamie Gritton 	error =
1064b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1065b97457e2SJamie Gritton 	if (error == ENOENT)
1066b97457e2SJamie Gritton 		gotchildmax = 0;
1067b97457e2SJamie Gritton 	else if (error != 0)
1068b97457e2SJamie Gritton 		goto done_free;
1069b97457e2SJamie Gritton 	else
1070b97457e2SJamie Gritton 		gotchildmax = 1;
1071b97457e2SJamie Gritton 
10720304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1073f337198dSJamie Gritton 	if (error == ENOENT)
1074f337198dSJamie Gritton 		gotenforce = 0;
1075f337198dSJamie Gritton 	else if (error != 0)
10760304c731SJamie Gritton 		goto done_free;
1077f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
1078f337198dSJamie Gritton 		error = EINVAL;
1079f337198dSJamie Gritton 		goto done_free;
1080f337198dSJamie Gritton 	} else
1081f337198dSJamie Gritton 		gotenforce = 1;
10820304c731SJamie Gritton 
10830cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
10840cc207a6SMartin Matuska 	if (error == ENOENT)
10850cc207a6SMartin Matuska 		gotrsnum = 0;
10860cc207a6SMartin Matuska 	else if (error != 0)
10870cc207a6SMartin Matuska 		goto done_free;
10880cc207a6SMartin Matuska 	else
10890cc207a6SMartin Matuska 		gotrsnum = 1;
10900cc207a6SMartin Matuska 
1091b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
1092672756aaSJamie Gritton 	for (bf = pr_flag_bool;
1093672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
1094672756aaSJamie Gritton 	     bf++) {
1095672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1096672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
10970304c731SJamie Gritton 	}
1098b38ff370SJamie Gritton 	ch_flags |= pr_flags;
1099672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
1100672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1101672756aaSJamie Gritton 	     jsf++) {
1102672756aaSJamie Gritton 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
11037cbf7213SJamie Gritton 		if (error == ENOENT)
11047cbf7213SJamie Gritton 			continue;
11057cbf7213SJamie Gritton 		if (error != 0)
11067cbf7213SJamie Gritton 			goto done_free;
11077cbf7213SJamie Gritton 		switch (jsys) {
11087cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
1109672756aaSJamie Gritton 			if (!jsf->disable) {
11107cbf7213SJamie Gritton 				error = EINVAL;
11117cbf7213SJamie Gritton 				goto done_free;
11127cbf7213SJamie Gritton 			}
1113672756aaSJamie Gritton 			pr_flags |= jsf->disable;
11147cbf7213SJamie Gritton 			break;
11157cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
1116672756aaSJamie Gritton 			pr_flags |= jsf->new;
11177cbf7213SJamie Gritton 			break;
11187cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
11197cbf7213SJamie Gritton 			break;
11207cbf7213SJamie Gritton 		default:
11217cbf7213SJamie Gritton 			error = EINVAL;
11227cbf7213SJamie Gritton 			goto done_free;
11237cbf7213SJamie Gritton 		}
1124672756aaSJamie Gritton 		ch_flags |= jsf->new | jsf->disable;
11257cbf7213SJamie Gritton 	}
11261158508aSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
11274affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
11284affa14cSJamie Gritton 		error = EINVAL;
11294affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
11304affa14cSJamie Gritton 		goto done_errmsg;
11314affa14cSJamie Gritton 	}
1132679e1390SJamie Gritton #ifdef VIMAGE
1133679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1134679e1390SJamie Gritton 		error = EINVAL;
1135679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
1136679e1390SJamie Gritton 		goto done_errmsg;
1137679e1390SJamie Gritton 	}
1138679e1390SJamie Gritton #endif
11392b0d6f81SJamie Gritton #ifdef INET
11402b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
11412b0d6f81SJamie Gritton 		error = EINVAL;
11422b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
11432b0d6f81SJamie Gritton 		goto done_errmsg;
11442b0d6f81SJamie Gritton 	}
11452b0d6f81SJamie Gritton #endif
11462b0d6f81SJamie Gritton #ifdef INET6
11472b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
11482b0d6f81SJamie Gritton 		error = EINVAL;
11492b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
11502b0d6f81SJamie Gritton 		goto done_errmsg;
11512b0d6f81SJamie Gritton 	}
11522b0d6f81SJamie Gritton #endif
1153b38ff370SJamie Gritton 
11540304c731SJamie Gritton 	pr_allow = ch_allow = 0;
1155672756aaSJamie Gritton 	for (bf = pr_flag_allow;
11565d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
11575d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
1158672756aaSJamie Gritton 	     bf++) {
1159672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1160672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
11610304c731SJamie Gritton 	}
11620304c731SJamie Gritton 	ch_allow |= pr_allow;
11630304c731SJamie Gritton 
1164b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1165b38ff370SJamie Gritton 	if (error == ENOENT)
1166b38ff370SJamie Gritton 		name = NULL;
1167b38ff370SJamie Gritton 	else if (error != 0)
1168b38ff370SJamie Gritton 		goto done_free;
1169b38ff370SJamie Gritton 	else {
1170b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1171b38ff370SJamie Gritton 			error = EINVAL;
1172b38ff370SJamie Gritton 			goto done_free;
1173b38ff370SJamie Gritton 		}
1174b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
1175b38ff370SJamie Gritton 			error = ENAMETOOLONG;
1176b38ff370SJamie Gritton 			goto done_free;
1177b38ff370SJamie Gritton 		}
1178b38ff370SJamie Gritton 	}
1179b38ff370SJamie Gritton 
1180b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1181b38ff370SJamie Gritton 	if (error == ENOENT)
1182b38ff370SJamie Gritton 		host = NULL;
1183b38ff370SJamie Gritton 	else if (error != 0)
1184b38ff370SJamie Gritton 		goto done_free;
1185b38ff370SJamie Gritton 	else {
118676ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
118776ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
1188b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
1189b38ff370SJamie Gritton 			error = EINVAL;
1190b38ff370SJamie Gritton 			goto done_free;
1191b38ff370SJamie Gritton 		}
1192b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
1193b38ff370SJamie Gritton 			error = ENAMETOOLONG;
1194b38ff370SJamie Gritton 			goto done_free;
1195b38ff370SJamie Gritton 		}
1196b38ff370SJamie Gritton 	}
1197b38ff370SJamie Gritton 
119876ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
119976ca6f88SJamie Gritton 	if (error == ENOENT)
120076ca6f88SJamie Gritton 		domain = NULL;
120176ca6f88SJamie Gritton 	else if (error != 0)
120276ca6f88SJamie Gritton 		goto done_free;
120376ca6f88SJamie Gritton 	else {
120476ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
120576ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
120676ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
120776ca6f88SJamie Gritton 			error = EINVAL;
120876ca6f88SJamie Gritton 			goto done_free;
120976ca6f88SJamie Gritton 		}
121076ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
121176ca6f88SJamie Gritton 			error = ENAMETOOLONG;
121276ca6f88SJamie Gritton 			goto done_free;
121376ca6f88SJamie Gritton 		}
121476ca6f88SJamie Gritton 	}
121576ca6f88SJamie Gritton 
121676ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
121776ca6f88SJamie Gritton 	if (error == ENOENT)
121876ca6f88SJamie Gritton 		uuid = NULL;
121976ca6f88SJamie Gritton 	else if (error != 0)
122076ca6f88SJamie Gritton 		goto done_free;
122176ca6f88SJamie Gritton 	else {
122276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
122376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
122476ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
122576ca6f88SJamie Gritton 			error = EINVAL;
122676ca6f88SJamie Gritton 			goto done_free;
122776ca6f88SJamie Gritton 		}
122876ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
122976ca6f88SJamie Gritton 			error = ENAMETOOLONG;
123076ca6f88SJamie Gritton 			goto done_free;
123176ca6f88SJamie Gritton 		}
123276ca6f88SJamie Gritton 	}
123376ca6f88SJamie Gritton 
1234841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
1235a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
123676ca6f88SJamie Gritton 		uint32_t hid32;
123776ca6f88SJamie Gritton 
123876ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
123976ca6f88SJamie Gritton 		hid = hid32;
124076ca6f88SJamie Gritton 	} else
124176ca6f88SJamie Gritton #endif
124276ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
124376ca6f88SJamie Gritton 	if (error == ENOENT)
124476ca6f88SJamie Gritton 		gothid = 0;
124576ca6f88SJamie Gritton 	else if (error != 0)
124676ca6f88SJamie Gritton 		goto done_free;
124776ca6f88SJamie Gritton 	else {
124876ca6f88SJamie Gritton 		gothid = 1;
124976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
125076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
125176ca6f88SJamie Gritton 	}
125276ca6f88SJamie Gritton 
1253b38ff370SJamie Gritton #ifdef INET
1254b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1255b38ff370SJamie Gritton 	if (error == ENOENT)
12560e5e396eSJamie Gritton 		ip4s = 0;
1257b38ff370SJamie Gritton 	else if (error != 0)
1258b38ff370SJamie Gritton 		goto done_free;
1259eb8dcdeaSGleb Smirnoff 	else if (ip4s & (sizeof(struct in_addr) - 1)) {
1260b38ff370SJamie Gritton 		error = EINVAL;
1261b38ff370SJamie Gritton 		goto done_free;
12620304c731SJamie Gritton 	} else {
12636a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
12646a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
12656a3f2779SJamie Gritton 		if (ip4s > 0) {
1266eb8dcdeaSGleb Smirnoff 			ip4s /= sizeof(struct in_addr);
1267b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
1268b38ff370SJamie Gritton 				error = EINVAL;
1269b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
1270b38ff370SJamie Gritton 				goto done_errmsg;
1271b38ff370SJamie Gritton 			}
1272eb8dcdeaSGleb Smirnoff 			ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1273eb8dcdeaSGleb Smirnoff 			if (ip4 == NULL) {
1274b38ff370SJamie Gritton 				error = EINVAL;
1275b38ff370SJamie Gritton 				goto done_free;
1276b38ff370SJamie Gritton 			}
1277b38ff370SJamie Gritton 		}
12780304c731SJamie Gritton 	}
1279b38ff370SJamie Gritton #endif
1280b38ff370SJamie Gritton 
1281b38ff370SJamie Gritton #ifdef INET6
1282b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1283b38ff370SJamie Gritton 	if (error == ENOENT)
12840e5e396eSJamie Gritton 		ip6s = 0;
1285b38ff370SJamie Gritton 	else if (error != 0)
1286b38ff370SJamie Gritton 		goto done_free;
1287eb8dcdeaSGleb Smirnoff 	else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1288b38ff370SJamie Gritton 		error = EINVAL;
1289b38ff370SJamie Gritton 		goto done_free;
12900304c731SJamie Gritton 	} else {
12916a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
12926a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
12936a3f2779SJamie Gritton 		if (ip6s > 0) {
1294eb8dcdeaSGleb Smirnoff 			ip6s /= sizeof(struct in6_addr);
1295b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
1296b38ff370SJamie Gritton 				error = EINVAL;
1297b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
1298b38ff370SJamie Gritton 				goto done_errmsg;
1299b38ff370SJamie Gritton 			}
1300eb8dcdeaSGleb Smirnoff 			ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1301eb8dcdeaSGleb Smirnoff 			if (ip6 == NULL) {
1302b38ff370SJamie Gritton 				error = EINVAL;
1303b38ff370SJamie Gritton 				goto done_free;
1304b38ff370SJamie Gritton 			}
1305b38ff370SJamie Gritton 		}
13060304c731SJamie Gritton 	}
1307b38ff370SJamie Gritton #endif
1308b38ff370SJamie Gritton 
1309bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1310bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1311bdfc8cc4SJamie Gritton 		error = EINVAL;
1312bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
1313bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
1314bdfc8cc4SJamie Gritton 		goto done_errmsg;
1315bdfc8cc4SJamie Gritton 	}
1316bdfc8cc4SJamie Gritton #endif
1317bdfc8cc4SJamie Gritton 
1318cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1319cf0313c6SJamie Gritton 	if (error == ENOENT)
1320cf0313c6SJamie Gritton 		osrelstr = NULL;
1321cf0313c6SJamie Gritton 	else if (error != 0)
1322cf0313c6SJamie Gritton 		goto done_free;
1323cf0313c6SJamie Gritton 	else {
1324cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
1325cf0313c6SJamie Gritton 			error = EINVAL;
1326cf0313c6SJamie Gritton 			vfs_opterror(opts,
1327cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
1328cf0313c6SJamie Gritton 			goto done_errmsg;
1329cf0313c6SJamie Gritton 		}
13301b786d01SBjoern A. Zeeb 		if (len == 0 || osrelstr[len - 1] != '\0') {
1331cf0313c6SJamie Gritton 			error = EINVAL;
13321b786d01SBjoern A. Zeeb 			goto done_free;
13331b786d01SBjoern A. Zeeb 		}
13341b786d01SBjoern A. Zeeb 		if (len >= OSRELEASELEN) {
13351b786d01SBjoern A. Zeeb 			error = ENAMETOOLONG;
1336cf0313c6SJamie Gritton 			vfs_opterror(opts,
1337cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
1338cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
1339cf0313c6SJamie Gritton 			goto done_errmsg;
1340cf0313c6SJamie Gritton 		}
1341cf0313c6SJamie Gritton 	}
1342cf0313c6SJamie Gritton 
1343cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1344cf0313c6SJamie Gritton 	if (error == ENOENT)
1345cf0313c6SJamie Gritton 		osreldt = 0;
1346cf0313c6SJamie Gritton 	else if (error != 0)
1347cf0313c6SJamie Gritton 		goto done_free;
1348cf0313c6SJamie Gritton 	else {
1349cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
1350cf0313c6SJamie Gritton 			error = EINVAL;
1351cf0313c6SJamie Gritton 			vfs_opterror(opts,
1352cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
1353cf0313c6SJamie Gritton 			goto done_errmsg;
1354cf0313c6SJamie Gritton 		}
1355cf0313c6SJamie Gritton 		if (osreldt == 0) {
1356cf0313c6SJamie Gritton 			error = EINVAL;
1357cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
1358cf0313c6SJamie Gritton 			goto done_errmsg;
1359cf0313c6SJamie Gritton 		}
1360cf0313c6SJamie Gritton 	}
1361cf0313c6SJamie Gritton 
1362b38ff370SJamie Gritton 	root = NULL;
1363b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
1364b38ff370SJamie Gritton 	if (error == ENOENT)
1365b38ff370SJamie Gritton 		path = NULL;
1366b38ff370SJamie Gritton 	else if (error != 0)
1367b38ff370SJamie Gritton 		goto done_free;
1368b38ff370SJamie Gritton 	else {
1369b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
1370b38ff370SJamie Gritton 			error = EINVAL;
1371b38ff370SJamie Gritton 			vfs_opterror(opts,
1372b38ff370SJamie Gritton 			    "path cannot be changed after creation");
1373b38ff370SJamie Gritton 			goto done_errmsg;
1374b38ff370SJamie Gritton 		}
1375b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
1376b38ff370SJamie Gritton 			error = EINVAL;
1377b38ff370SJamie Gritton 			goto done_free;
1378b38ff370SJamie Gritton 		}
13797e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1380b38ff370SJamie Gritton 		error = namei(&nd);
1381b38ff370SJamie Gritton 		if (error)
1382b38ff370SJamie Gritton 			goto done_free;
1383b38ff370SJamie Gritton 		root = nd.ni_vp;
1384bb92cd7bSMateusz Guzik 		NDFREE_PNBUF(&nd);
13856dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
13866dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
13876dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
13883eb6b656SMateusz Guzik 		if (error == 0) {
13896dfe0a3dSMartin Matuska 			path = g_path;
13906dfe0a3dSMartin Matuska 		} else {
1391f6e633a9SMartin Matuska 			/* exit on other errors */
1392b38ff370SJamie Gritton 			goto done_free;
1393b38ff370SJamie Gritton 		}
1394f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
1395f6e633a9SMartin Matuska 			error = ENOTDIR;
1396f6e633a9SMartin Matuska 			vput(root);
1397f6e633a9SMartin Matuska 			goto done_free;
1398f6e633a9SMartin Matuska 		}
1399b249ce48SMateusz Guzik 		VOP_UNLOCK(root);
1400b38ff370SJamie Gritton 	}
1401b38ff370SJamie Gritton 
1402b38ff370SJamie Gritton 	/*
1403b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
1404b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
1405b38ff370SJamie Gritton 	 */
1406b38ff370SJamie Gritton 	pr = NULL;
14077de883c8SJamie Gritton 	inspr = NULL;
1408babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1409babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1410babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1411babbbb9cSJamie Gritton 		if (*p != '\0')
1412babbbb9cSJamie Gritton 			jid = 0;
1413babbbb9cSJamie Gritton 	}
1414b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
1415108a9384SJamie Gritton 	drflags = PD_LIST_XLOCKED;
14160a2a96f3SJamie Gritton 	ppr = mypr;
14170a2a96f3SJamie Gritton 	if (!prison_isalive(ppr)) {
14180a2a96f3SJamie Gritton 		/* This jail is dying.  This process will surely follow. */
14190a2a96f3SJamie Gritton 		error = EAGAIN;
14200a2a96f3SJamie Gritton 		goto done_deref;
14210a2a96f3SJamie Gritton 	}
1422b38ff370SJamie Gritton 	if (jid != 0) {
1423b38ff370SJamie Gritton 		if (jid < 0) {
1424b38ff370SJamie Gritton 			error = EINVAL;
1425b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
14262a4b2251SJamie Gritton 			goto done_deref;
1427b38ff370SJamie Gritton 		}
14287de883c8SJamie Gritton 		/*
14297de883c8SJamie Gritton 		 * See if a requested jid already exists.  Keep track of
14307de883c8SJamie Gritton 		 * where it can be inserted later.
14317de883c8SJamie Gritton 		 */
14327de883c8SJamie Gritton 		TAILQ_FOREACH(inspr, &allprison, pr_list) {
1433f7496dcaSJamie Gritton 			if (inspr->pr_id < jid)
1434f7496dcaSJamie Gritton 				continue;
1435f7496dcaSJamie Gritton 			if (inspr->pr_id > jid)
1436f7496dcaSJamie Gritton 				break;
14377de883c8SJamie Gritton 			pr = inspr;
1438f7496dcaSJamie Gritton 			mtx_lock(&pr->pr_mtx);
14392a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
14407de883c8SJamie Gritton 			inspr = NULL;
14417de883c8SJamie Gritton 			break;
14427de883c8SJamie Gritton 		}
1443b38ff370SJamie Gritton 		if (pr != NULL) {
1444b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1445b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
14467de883c8SJamie Gritton 				/*
14477de883c8SJamie Gritton 				 * Even creators that cannot see the jail will
14487de883c8SJamie Gritton 				 * get EEXIST.
14497de883c8SJamie Gritton 				 */
1450b38ff370SJamie Gritton 				error = EEXIST;
1451b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1452b38ff370SJamie Gritton 				    jid);
14532a4b2251SJamie Gritton 				goto done_deref;
1454b38ff370SJamie Gritton 			}
14550304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
14567de883c8SJamie Gritton 				/*
14577de883c8SJamie Gritton 				 * Updaters get ENOENT if they cannot see the
14587de883c8SJamie Gritton 				 * jail.  This is true even for CREATE | UPDATE,
14597de883c8SJamie Gritton 				 * which normally cannot give this error.
14607de883c8SJamie Gritton 				 */
14612a4b2251SJamie Gritton 				error = ENOENT;
14622a4b2251SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
14632a4b2251SJamie Gritton 				goto done_deref;
1464f7496dcaSJamie Gritton 			}
14650a2a96f3SJamie Gritton 			ppr = pr->pr_parent;
14660a2a96f3SJamie Gritton 			if (!prison_isalive(ppr)) {
14670a2a96f3SJamie Gritton 				error = ENOENT;
14680a2a96f3SJamie Gritton 				vfs_opterror(opts, "jail %d is dying",
14690a2a96f3SJamie Gritton 				    ppr->pr_id);
14700a2a96f3SJamie Gritton 				goto done_deref;
14710a2a96f3SJamie Gritton 			}
1472f7496dcaSJamie Gritton 			if (!prison_isalive(pr)) {
1473b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1474b38ff370SJamie Gritton 					error = ENOENT;
1475b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1476b38ff370SJamie Gritton 					    jid);
14772a4b2251SJamie Gritton 					goto done_deref;
1478f7496dcaSJamie Gritton 				}
1479f7496dcaSJamie Gritton 				if ((flags & JAIL_ATTACH) ||
1480b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1481b38ff370SJamie Gritton 					/*
1482b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1483b38ff370SJamie Gritton 					 * (via attach or persist), but first
1484b38ff370SJamie Gritton 					 * it must determine if another jail
1485b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1486b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1487b38ff370SJamie Gritton 					 * name.
1488b38ff370SJamie Gritton 					 */
1489b38ff370SJamie Gritton 					if (name == NULL)
14900304c731SJamie Gritton 						name = prison_name(mypr, pr);
1491b38ff370SJamie Gritton 				}
1492b38ff370SJamie Gritton 			}
14932a4b2251SJamie Gritton 		} else {
1494b38ff370SJamie Gritton 			/* Update: jid must exist. */
1495b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1496b38ff370SJamie Gritton 				error = ENOENT;
1497b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
14982a4b2251SJamie Gritton 				goto done_deref;
1499b38ff370SJamie Gritton 			}
1500b38ff370SJamie Gritton 		}
1501b38ff370SJamie Gritton 	}
1502b38ff370SJamie Gritton 	/*
1503b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1504b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1505b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1506b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1507b38ff370SJamie Gritton 	 * because that is the jail being updated).
1508b38ff370SJamie Gritton 	 */
1509b6f47c23SJamie Gritton 	namelc = NULL;
1510b38ff370SJamie Gritton 	if (name != NULL) {
1511babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1512babbbb9cSJamie Gritton 		if (namelc == NULL)
1513babbbb9cSJamie Gritton 			namelc = name;
1514babbbb9cSJamie Gritton 		else {
15150304c731SJamie Gritton 			/*
15160304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
15170304c731SJamie Gritton 			 * parent and child names, and make sure the parent
15180304c731SJamie Gritton 			 * exists or matches an already found jail.
15190304c731SJamie Gritton 			 */
15200304c731SJamie Gritton 			if (pr != NULL) {
1521babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1522babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
15230304c731SJamie Gritton 					error = EINVAL;
15240304c731SJamie Gritton 					vfs_opterror(opts,
15250304c731SJamie Gritton 					    "cannot change jail's parent");
15262a4b2251SJamie Gritton 					goto done_deref;
15270304c731SJamie Gritton 				}
15280304c731SJamie Gritton 			} else {
1529b6f47c23SJamie Gritton 				*namelc = '\0';
15300304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
15310304c731SJamie Gritton 				if (ppr == NULL) {
15320304c731SJamie Gritton 					error = ENOENT;
15330304c731SJamie Gritton 					vfs_opterror(opts,
15340304c731SJamie Gritton 					    "jail \"%s\" not found", name);
15352a4b2251SJamie Gritton 					goto done_deref;
15360304c731SJamie Gritton 				}
1537c050ea80SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
15380a2a96f3SJamie Gritton 				if (!prison_isalive(ppr)) {
1539c050ea80SJamie Gritton 					error = ENOENT;
1540c050ea80SJamie Gritton 					vfs_opterror(opts,
1541c050ea80SJamie Gritton 					    "jail \"%s\" is dying", name);
1542c050ea80SJamie Gritton 					goto done_deref;
1543c050ea80SJamie Gritton 				}
1544b6f47c23SJamie Gritton 				*namelc = '.';
15450304c731SJamie Gritton 			}
1546b6f47c23SJamie Gritton 			namelc++;
15470304c731SJamie Gritton 		}
1548b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1549b6f47c23SJamie Gritton 			pnamelen =
15500304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
15510304c731SJamie Gritton 			deadpr = NULL;
15520304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
15536754ae25SJamie Gritton 				if (tpr != pr &&
1554b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
155576ad42abSJamie Gritton 					if (prison_isalive(tpr)) {
1556b38ff370SJamie Gritton 						if (pr == NULL &&
1557b38ff370SJamie Gritton 						    cuflags != JAIL_CREATE) {
1558b38ff370SJamie Gritton 							/*
1559b38ff370SJamie Gritton 							 * Use this jail
1560b38ff370SJamie Gritton 							 * for updates.
1561b38ff370SJamie Gritton 							 */
1562b38ff370SJamie Gritton 							pr = tpr;
1563f7496dcaSJamie Gritton 							mtx_lock(&pr->pr_mtx);
156483bc72a0SJamie Gritton 							drflags |= PD_LOCKED;
1565b38ff370SJamie Gritton 							break;
1566b38ff370SJamie Gritton 						}
1567b38ff370SJamie Gritton 						/*
1568b38ff370SJamie Gritton 						 * Create, or update(jid):
1569b38ff370SJamie Gritton 						 * name must not exist in an
15700304c731SJamie Gritton 						 * active sibling jail.
1571b38ff370SJamie Gritton 						 */
1572b38ff370SJamie Gritton 						error = EEXIST;
1573b38ff370SJamie Gritton 						vfs_opterror(opts,
1574b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1575b38ff370SJamie Gritton 						   name);
15762a4b2251SJamie Gritton 						goto done_deref;
1577b38ff370SJamie Gritton 					}
157876ad42abSJamie Gritton 					if (pr == NULL &&
1579f7496dcaSJamie Gritton 					    cuflags != JAIL_CREATE) {
158076ad42abSJamie Gritton 						deadpr = tpr;
1581f7496dcaSJamie Gritton 					}
1582b38ff370SJamie Gritton 				}
1583b38ff370SJamie Gritton 			}
1584b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1585b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1586b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1587b38ff370SJamie Gritton 					pr = deadpr;
1588f7496dcaSJamie Gritton 					mtx_lock(&pr->pr_mtx);
15892a4b2251SJamie Gritton 					drflags |= PD_LOCKED;
1590b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1591b38ff370SJamie Gritton 					error = ENOENT;
1592b38ff370SJamie Gritton 					vfs_opterror(opts,
1593b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
15942a4b2251SJamie Gritton 					goto done_deref;
1595b38ff370SJamie Gritton 				}
1596b38ff370SJamie Gritton 			}
1597b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1598b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1599b38ff370SJamie Gritton 				error = ENOENT;
1600b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1601b38ff370SJamie Gritton 				    name);
16022a4b2251SJamie Gritton 				goto done_deref;
1603b38ff370SJamie Gritton 			}
1604b38ff370SJamie Gritton 		}
1605b38ff370SJamie Gritton 	}
1606b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1607b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1608b38ff370SJamie Gritton 		error = ENOENT;
1609b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
16102a4b2251SJamie Gritton 		goto done_deref;
1611b38ff370SJamie Gritton 	}
1612b38ff370SJamie Gritton 
1613b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
16142a4b2251SJamie Gritton 	created = pr == NULL;
16152a4b2251SJamie Gritton 	if (created) {
1616b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1617b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1618b97457e2SJamie Gritton 				error = EPERM;
1619b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
16202a4b2251SJamie Gritton 				goto done_deref;
1621b97457e2SJamie Gritton 			}
16227de883c8SJamie Gritton 		if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1623b38ff370SJamie Gritton 			error = EAGAIN;
16247de883c8SJamie Gritton 			vfs_opterror(opts, "no available jail IDs");
16252a4b2251SJamie Gritton 			goto done_deref;
1626b38ff370SJamie Gritton 		}
16272a4b2251SJamie Gritton 
16282a4b2251SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
16291158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_INVALID;
16301158508aSJamie Gritton 		refcount_init(&pr->pr_ref, 1);
1631f7496dcaSJamie Gritton 		refcount_init(&pr->pr_uref, 0);
16321158508aSJamie Gritton 		drflags |= PD_DEREF;
16332a4b2251SJamie Gritton 		LIST_INIT(&pr->pr_children);
16342a4b2251SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
16352a4b2251SJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
16362a4b2251SJamie Gritton 
16377de883c8SJamie Gritton 		pr->pr_id = jid;
16387de883c8SJamie Gritton 		if (inspr != NULL)
16397de883c8SJamie Gritton 			TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
16407de883c8SJamie Gritton 		else
1641b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
16427de883c8SJamie Gritton 
16437de883c8SJamie Gritton 		pr->pr_parent = ppr;
16440a2a96f3SJamie Gritton 		prison_hold(ppr);
16450a2a96f3SJamie Gritton 		prison_proc_hold(ppr);
16460304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
16470304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1648b97457e2SJamie Gritton 			tpr->pr_childcount++;
1649b38ff370SJamie Gritton 
16500304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1651b6f47c23SJamie Gritton 		if (namelc == NULL)
1652b6f47c23SJamie Gritton 			namelc = "";
1653b38ff370SJamie Gritton 		if (path == NULL) {
1654b38ff370SJamie Gritton 			path = "/";
16550304c731SJamie Gritton 			root = mypr->pr_root;
1656b38ff370SJamie Gritton 			vref(root);
1657b38ff370SJamie Gritton 		}
16588986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
16598986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1660bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1661bdfc8cc4SJamie Gritton #ifdef VIMAGE
1662bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1663bdfc8cc4SJamie Gritton #endif
1664bdfc8cc4SJamie Gritton 		{
16650304c731SJamie Gritton #ifdef INET
16662b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
16676a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
16682b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
16692b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1670eb8dcdeaSGleb Smirnoff 				prison_ip_dup(ppr, pr, PR_INET);
16712b0d6f81SJamie Gritton 			}
16720304c731SJamie Gritton #endif
16730304c731SJamie Gritton #ifdef INET6
16742b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
16756a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
16762b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
16772b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1678eb8dcdeaSGleb Smirnoff 				prison_ip_dup(ppr, pr, PR_INET6);
16792b0d6f81SJamie Gritton 			}
16800304c731SJamie Gritton #endif
1681bdfc8cc4SJamie Gritton 		}
1682bdfc8cc4SJamie Gritton #endif
1683592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1684592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1685592bcae8SBjoern A. Zeeb 
16860304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
16870304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1688af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1689bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1690b38ff370SJamie Gritton 
1691b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1692b96bd95bSIan Lepore 		if (osrelstr == NULL)
16931b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
16941b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1695b96bd95bSIan Lepore 		else
16961b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, osrelstr,
16971b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1698b96bd95bSIan Lepore 
1699679e1390SJamie Gritton #ifdef VIMAGE
1700679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1701679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1702679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1703679e1390SJamie Gritton #endif
1704b38ff370SJamie Gritton 		/*
1705b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
17068771ff75SGordon Bergling 		 * Unlike other initial settings, this may return an error.
1707b38ff370SJamie Gritton 		 */
17080304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
17092a4b2251SJamie Gritton 		if (error)
17102a4b2251SJamie Gritton 			goto done_deref;
1711b38ff370SJamie Gritton 
1712b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17132a4b2251SJamie Gritton 		drflags |= PD_LOCKED;
1714b38ff370SJamie Gritton 	} else {
17152b0d6f81SJamie Gritton 		/*
17162b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
17172b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
17182b0d6f81SJamie Gritton 		 */
17196754ae25SJamie Gritton 		prison_hold(pr);
17202a4b2251SJamie Gritton 		drflags |= PD_DEREF;
1721bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1722bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1723bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1724bdfc8cc4SJamie Gritton 			error = EINVAL;
1725bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1726bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
17272a4b2251SJamie Gritton 			goto done_deref;
1728bdfc8cc4SJamie Gritton 		}
1729bdfc8cc4SJamie Gritton #endif
17302b0d6f81SJamie Gritton #ifdef INET
17312b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
17322b0d6f81SJamie Gritton 			error = EINVAL;
17332b0d6f81SJamie Gritton 			vfs_opterror(opts,
17342b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
17352a4b2251SJamie Gritton 			goto done_deref;
17362b0d6f81SJamie Gritton 		}
17372b0d6f81SJamie Gritton #endif
17382b0d6f81SJamie Gritton #ifdef INET6
17392b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
17402b0d6f81SJamie Gritton 			error = EINVAL;
17412b0d6f81SJamie Gritton 			vfs_opterror(opts,
17422b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
17432a4b2251SJamie Gritton 			goto done_deref;
17442b0d6f81SJamie Gritton 		}
17452b0d6f81SJamie Gritton #endif
1746b38ff370SJamie Gritton 	}
1747b38ff370SJamie Gritton 
1748b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
17490304c731SJamie Gritton 	if (gotslevel) {
17500304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
17510304c731SJamie Gritton 			error = EPERM;
17522a4b2251SJamie Gritton 			goto done_deref;
17530304c731SJamie Gritton 		}
17540304c731SJamie Gritton 	}
1755b97457e2SJamie Gritton 	if (gotchildmax) {
1756b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1757b97457e2SJamie Gritton 			error = EPERM;
17582a4b2251SJamie Gritton 			goto done_deref;
1759b97457e2SJamie Gritton 		}
1760b97457e2SJamie Gritton 	}
17610304c731SJamie Gritton 	if (gotenforce) {
17620304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
17630304c731SJamie Gritton 			error = EPERM;
17642a4b2251SJamie Gritton 			goto done_deref;
17650304c731SJamie Gritton 		}
17660304c731SJamie Gritton 	}
17670cc207a6SMartin Matuska 	if (gotrsnum) {
17680cc207a6SMartin Matuska 		/*
17690cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
17700cc207a6SMartin Matuska 		 */
1771bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
17720cc207a6SMartin Matuska 			error = EINVAL;
17732a4b2251SJamie Gritton 			goto done_deref;
17740cc207a6SMartin Matuska 		}
17750cc207a6SMartin Matuska 		/*
1776bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
17770cc207a6SMartin Matuska 		 */
17780cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
17790cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
17800cc207a6SMartin Matuska 				error = EPERM;
17812a4b2251SJamie Gritton 				goto done_deref;
1782bf3db8aaSMartin Matuska 			} else
17830cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
17840cc207a6SMartin Matuska 		}
17850cc207a6SMartin Matuska 	}
1786b38ff370SJamie Gritton #ifdef INET
17872b0d6f81SJamie Gritton 	if (ip4s > 0) {
1788eb8dcdeaSGleb Smirnoff 		if ((ppr->pr_flags & PR_IP4) &&
1789eb8dcdeaSGleb Smirnoff 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1790eb8dcdeaSGleb Smirnoff 		    PR_INET)) {
17910304c731SJamie Gritton 			error = EPERM;
17922a4b2251SJamie Gritton 			goto done_deref;
17930304c731SJamie Gritton 		}
1794eb8dcdeaSGleb Smirnoff 		if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
17950304c731SJamie Gritton 			error = EADDRINUSE;
1796eb8dcdeaSGleb Smirnoff 			vfs_opterror(opts, "IPv4 addresses clash");
17972a4b2251SJamie Gritton 			goto done_deref;
1798b38ff370SJamie Gritton 		}
17990304c731SJamie Gritton 	}
1800b38ff370SJamie Gritton #endif
1801b38ff370SJamie Gritton #ifdef INET6
18022b0d6f81SJamie Gritton 	if (ip6s > 0) {
1803eb8dcdeaSGleb Smirnoff 		if ((ppr->pr_flags & PR_IP6) &&
1804eb8dcdeaSGleb Smirnoff 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1805eb8dcdeaSGleb Smirnoff 		    PR_INET6)) {
18060304c731SJamie Gritton 			error = EPERM;
18072a4b2251SJamie Gritton 			goto done_deref;
18080304c731SJamie Gritton 		}
1809eb8dcdeaSGleb Smirnoff 		if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
18100304c731SJamie Gritton 			error = EADDRINUSE;
1811eb8dcdeaSGleb Smirnoff 			vfs_opterror(opts, "IPv6 addresses clash");
18122a4b2251SJamie Gritton 			goto done_deref;
1813b38ff370SJamie Gritton 		}
18140304c731SJamie Gritton 	}
1815b38ff370SJamie Gritton #endif
18160304c731SJamie Gritton 	onamelen = namelen = 0;
1817b6f47c23SJamie Gritton 	if (namelc != NULL) {
18186ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
18196ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
18206ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
18216ab6058eSJamie Gritton 		 */
1822b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1823b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
18246ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
18256ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1826b38ff370SJamie Gritton 			error = EINVAL;
1827babbbb9cSJamie Gritton 			vfs_opterror(opts,
1828babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
18292a4b2251SJamie Gritton 			goto done_deref;
1830b38ff370SJamie Gritton 		}
1831b38ff370SJamie Gritton 		/*
18320304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
18330304c731SJamie Gritton 		 * children.
1834b38ff370SJamie Gritton 		 */
1835b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1836b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1837b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1838b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
18390304c731SJamie Gritton 			error = ENAMETOOLONG;
18402a4b2251SJamie Gritton 			goto done_deref;
18410304c731SJamie Gritton 		}
18420304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
18430304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
18440304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
18450304c731SJamie Gritton 				error = ENAMETOOLONG;
18462a4b2251SJamie Gritton 				goto done_deref;
18470304c731SJamie Gritton 			}
18480304c731SJamie Gritton 		}
18490304c731SJamie Gritton 	}
1850b3079544SJamie Gritton 	pr_allow_diff = pr_allow & ~ppr->pr_allow;
1851b3079544SJamie Gritton 	if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
18520304c731SJamie Gritton 		error = EPERM;
18532a4b2251SJamie Gritton 		goto done_deref;
1854b38ff370SJamie Gritton 	}
1855b38ff370SJamie Gritton 
1856b6f47c23SJamie Gritton 	/*
1857b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1858b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1859b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1860b6f47c23SJamie Gritton 	 */
1861b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
18622a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1863b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
18642a4b2251SJamie Gritton 	if (error != 0)
18652a4b2251SJamie Gritton 		goto done_deref;
1866b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
18672a4b2251SJamie Gritton 	drflags |= PD_LOCKED;
1868b6f47c23SJamie Gritton 
1869b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1870b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1871b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1872b6f47c23SJamie Gritton 			error = EINVAL;
1873b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
18742a4b2251SJamie Gritton 			goto done_deref;
1875b6f47c23SJamie Gritton 		}
1876b6f47c23SJamie Gritton 	}
1877b6f47c23SJamie Gritton 
1878b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1879b38ff370SJamie Gritton #ifdef INET
18808bce8d28SZhenlei Huang 	redo_ip4 = false;
18810304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
18820304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1883eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, PR_INET, ip4);
1884b38ff370SJamie Gritton 		ip4 = NULL;
18850304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1886bdfc8cc4SJamie Gritton #ifdef VIMAGE
1887bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1888bdfc8cc4SJamie Gritton 				descend = 0;
1889bdfc8cc4SJamie Gritton 				continue;
1890bdfc8cc4SJamie Gritton 			}
1891bdfc8cc4SJamie Gritton #endif
18928bce8d28SZhenlei Huang 			if (!prison_ip_restrict(tpr, PR_INET, NULL)) {
18938bce8d28SZhenlei Huang 				redo_ip4 = true;
18940304c731SJamie Gritton 				descend = 0;
18950304c731SJamie Gritton 			}
18960304c731SJamie Gritton 		}
18970304c731SJamie Gritton 	}
1898b38ff370SJamie Gritton #endif
1899b38ff370SJamie Gritton #ifdef INET6
19008bce8d28SZhenlei Huang 	redo_ip6 = false;
19010304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
19020304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1903eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, PR_INET6, ip6);
1904b38ff370SJamie Gritton 		ip6 = NULL;
19050304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1906bdfc8cc4SJamie Gritton #ifdef VIMAGE
1907bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1908bdfc8cc4SJamie Gritton 				descend = 0;
1909bdfc8cc4SJamie Gritton 				continue;
1910bdfc8cc4SJamie Gritton 			}
1911bdfc8cc4SJamie Gritton #endif
19128bce8d28SZhenlei Huang 			if (!prison_ip_restrict(tpr, PR_INET6, NULL)) {
19138bce8d28SZhenlei Huang 				redo_ip6 = true;
19140304c731SJamie Gritton 				descend = 0;
19150304c731SJamie Gritton 			}
19160304c731SJamie Gritton 		}
19170304c731SJamie Gritton 	}
1918b38ff370SJamie Gritton #endif
19190304c731SJamie Gritton 	if (gotslevel) {
1920b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
19210304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
19220304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
19230304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
19240304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
19250304c731SJamie Gritton 	}
1926b97457e2SJamie Gritton 	if (gotchildmax) {
1927b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1928b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1929b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1930b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1931b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1932b97457e2SJamie Gritton 				    ? childmax - level : 0;
1933b97457e2SJamie Gritton 	}
19340304c731SJamie Gritton 	if (gotenforce) {
19350304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
19360304c731SJamie Gritton 		/* Pass this restriction on to the children. */
19370304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
19380304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
19390304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
19400304c731SJamie Gritton 	}
19410cc207a6SMartin Matuska 	if (gotrsnum) {
19420cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
19430cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
19440cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
19450cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
19460cc207a6SMartin Matuska 	}
1947b6f47c23SJamie Gritton 	if (namelc != NULL) {
19480304c731SJamie Gritton 		if (ppr == &prison0)
1949b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
19500304c731SJamie Gritton 		else
19510304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1952b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
19530304c731SJamie Gritton 		/* Change this component of child names. */
19540304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
19550304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
19560304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
19570304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
19580304c731SJamie Gritton 		}
19590304c731SJamie Gritton 	}
1960b38ff370SJamie Gritton 	if (path != NULL) {
19610304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1962b38ff370SJamie Gritton 		strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1963b38ff370SJamie Gritton 		pr->pr_root = root;
19642a4b2251SJamie Gritton 		root = NULL;
1965b38ff370SJamie Gritton 	}
196676ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
196776ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
196876ca6f88SJamie Gritton 			/*
196976ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
197076ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
197176ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
197276ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
197376ca6f88SJamie Gritton 			 */
1974c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1975c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1976c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1977c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1978c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1979c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
198076ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
198176ca6f88SJamie Gritton 		}
198276ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
198376ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1984b38ff370SJamie Gritton 		if (host != NULL)
1985c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
198676ca6f88SJamie Gritton 		if (domain != NULL)
1987c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1988c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
198976ca6f88SJamie Gritton 		if (uuid != NULL)
1990c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
199176ca6f88SJamie Gritton 		if (gothid)
199276ca6f88SJamie Gritton 			pr->pr_hostid = hid;
199376ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
199476ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
199576ca6f88SJamie Gritton 				descend = 0;
199676ca6f88SJamie Gritton 			else {
199776ca6f88SJamie Gritton 				if (host != NULL)
1998c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1999c1f19219SJamie Gritton 					    pr->pr_hostname,
2000c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
200176ca6f88SJamie Gritton 				if (domain != NULL)
2002c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
2003c1f19219SJamie Gritton 					    pr->pr_domainname,
2004c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
200576ca6f88SJamie Gritton 				if (uuid != NULL)
2006c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
2007c1f19219SJamie Gritton 					    pr->pr_hostuuid,
2008c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
200976ca6f88SJamie Gritton 				if (gothid)
201076ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
201176ca6f88SJamie Gritton 			}
201276ca6f88SJamie Gritton 		}
201376ca6f88SJamie Gritton 	}
20140304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
20150fe74ae6SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow))
20160fe74ae6SJamie Gritton 		prison_set_allow_locked(pr, tallow, 0);
2017b38ff370SJamie Gritton 	/*
2018b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
20191158508aSJamie Gritton 	 * persist flag lose that reference.
2020b38ff370SJamie Gritton 	 */
202176ad42abSJamie Gritton 	born = !prison_isalive(pr);
20221158508aSJamie Gritton 	if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
2023b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
20246754ae25SJamie Gritton 			prison_hold(pr);
20251158508aSJamie Gritton 			/*
20261158508aSJamie Gritton 			 * This may make a dead prison alive again, but wait
20271158508aSJamie Gritton 			 * to label it as such until after OSD calls have had
20281158508aSJamie Gritton 			 * a chance to run (and perhaps to fail).
20291158508aSJamie Gritton 			 */
20306754ae25SJamie Gritton 			refcount_acquire(&pr->pr_uref);
2031b38ff370SJamie Gritton 		} else {
203239c8ef90SJamie Gritton 			drflags |= PD_DEUREF;
2033f7496dcaSJamie Gritton 			prison_free_not_last(pr);
2034b38ff370SJamie Gritton 		}
2035b38ff370SJamie Gritton 	}
2036b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
2037b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
20382a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
2039c861373bSJamie Gritton 	/*
2040c861373bSJamie Gritton 	 * Any errors past this point will need to de-persist newly created
2041c861373bSJamie Gritton 	 * prisons, as well as call remove methods.
2042c861373bSJamie Gritton 	 */
2043c861373bSJamie Gritton 	if (born)
2044c861373bSJamie Gritton 		drflags |= PD_KILL;
2045b38ff370SJamie Gritton 
2046a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
20474b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
2048a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
2049a7ad07bfSEdward Tomasz Napierala #endif
2050a7ad07bfSEdward Tomasz Napierala 
20510304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
20520304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
20530304c731SJamie Gritton 	 */
20540304c731SJamie Gritton #ifdef INET
20550304c731SJamie Gritton 	while (redo_ip4) {
2056eb8dcdeaSGleb Smirnoff 		ip4s = pr->pr_addrs[PR_INET]->ips;
20578bce8d28SZhenlei Huang 		MPASS(ip4 == NULL);
2058eb8dcdeaSGleb Smirnoff 		ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
20590304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20608bce8d28SZhenlei Huang 		redo_ip4 = false;
20610304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2062bdfc8cc4SJamie Gritton #ifdef VIMAGE
2063bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
2064bdfc8cc4SJamie Gritton 				descend = 0;
2065bdfc8cc4SJamie Gritton 				continue;
2066bdfc8cc4SJamie Gritton 			}
2067bdfc8cc4SJamie Gritton #endif
2068b2d76b52SZhenlei Huang 			if (!prison_ip_restrict(tpr, PR_INET, &ip4))
2069b2d76b52SZhenlei Huang 				redo_ip4 = true;
20700304c731SJamie Gritton 		}
20710304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20720304c731SJamie Gritton 	}
20730304c731SJamie Gritton #endif
20740304c731SJamie Gritton #ifdef INET6
20750304c731SJamie Gritton 	while (redo_ip6) {
2076eb8dcdeaSGleb Smirnoff 		ip6s = pr->pr_addrs[PR_INET6]->ips;
20778bce8d28SZhenlei Huang 		MPASS(ip6 == NULL);
2078eb8dcdeaSGleb Smirnoff 		ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
20790304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20808bce8d28SZhenlei Huang 		redo_ip6 = false;
20810304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2082bdfc8cc4SJamie Gritton #ifdef VIMAGE
2083bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
2084bdfc8cc4SJamie Gritton 				descend = 0;
2085bdfc8cc4SJamie Gritton 				continue;
2086bdfc8cc4SJamie Gritton 			}
2087bdfc8cc4SJamie Gritton #endif
2088b2d76b52SZhenlei Huang 			if (!prison_ip_restrict(tpr, PR_INET6, &ip6))
2089b2d76b52SZhenlei Huang 				redo_ip6 = true;
20900304c731SJamie Gritton 		}
20910304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20920304c731SJamie Gritton 	}
20930304c731SJamie Gritton #endif
20940304c731SJamie Gritton 
2095b38ff370SJamie Gritton 	/* Let the modules do their work. */
2096cc5fd8c7SJamie Gritton 	if (born) {
2097b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2098c861373bSJamie Gritton 		if (error)
20992a4b2251SJamie Gritton 			goto done_deref;
2100b38ff370SJamie Gritton 	}
2101b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
2102c861373bSJamie Gritton 	if (error)
21032a4b2251SJamie Gritton 		goto done_deref;
2104b38ff370SJamie Gritton 
21051158508aSJamie Gritton 	/*
21061158508aSJamie Gritton 	 * A new prison is now ready to be seen; either it has gained a user
21071158508aSJamie Gritton 	 * reference via persistence, or is about to gain one via attachment.
21081158508aSJamie Gritton 	 */
21091158508aSJamie Gritton 	if (born) {
21101158508aSJamie Gritton 		drflags = prison_lock_xlock(pr, drflags);
21111158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_ALIVE;
21121158508aSJamie Gritton 	}
21131158508aSJamie Gritton 
2114b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
2115b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
2116c861373bSJamie Gritton 		error = do_jail_attach(td, pr,
2117589e4c1dSJamie Gritton 		    prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
2118f7496dcaSJamie Gritton 		drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2119b38ff370SJamie Gritton 		if (error) {
2120b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
21212a4b2251SJamie Gritton 			goto done_deref;
2122b38ff370SJamie Gritton 		}
2123b38ff370SJamie Gritton 	}
2124b38ff370SJamie Gritton 
21251fb24974SEdward Tomasz Napierala #ifdef RACCT
21264b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
2127701d6b50SJamie Gritton 		if (drflags & PD_LOCKED) {
2128701d6b50SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2129701d6b50SJamie Gritton 			drflags &= ~PD_LOCKED;
2130701d6b50SJamie Gritton 		}
2131f7496dcaSJamie Gritton 		if (drflags & PD_LIST_XLOCKED) {
2132f7496dcaSJamie Gritton 			sx_xunlock(&allprison_lock);
2133f7496dcaSJamie Gritton 			drflags &= ~PD_LIST_XLOCKED;
2134b4e87a63SJamie Gritton 		}
21351fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
21361fb24974SEdward Tomasz Napierala 	}
21371fb24974SEdward Tomasz Napierala #endif
21381fb24974SEdward Tomasz Napierala 
2139bba7a2e8SRick Macklem 	if (born && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 &&
2140bba7a2e8SRick Macklem 	    (pr->pr_root->v_vflag & VV_ROOT) == 0)
2141bba7a2e8SRick Macklem 		printf("Warning jail jid=%d: mountd/nfsd requires a separate"
2142bba7a2e8SRick Macklem 		   " file system\n", pr->pr_id);
2143bba7a2e8SRick Macklem 
2144c861373bSJamie Gritton 	drflags &= ~PD_KILL;
21451fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
21461fb24974SEdward Tomasz Napierala 
21472a4b2251SJamie Gritton  done_deref:
21482a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
21492a4b2251SJamie Gritton 	if (pr != NULL)
21502a4b2251SJamie Gritton 		prison_deref(pr, drflags);
21512a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2152b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
21532a4b2251SJamie Gritton 	else if (drflags & PD_LIST_XLOCKED)
2154b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
21555050aa86SKonstantin Belousov 	if (root != NULL)
2156b38ff370SJamie Gritton 		vrele(root);
2157b38ff370SJamie Gritton  done_errmsg:
2158b38ff370SJamie Gritton 	if (error) {
21592a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2160176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2161176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
2162b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2163b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2164b38ff370SJamie Gritton 				bcopy(errmsg,
2165b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2166b38ff370SJamie Gritton 				    errmsg_len);
2167b38ff370SJamie Gritton 			else
2168b38ff370SJamie Gritton 				copyout(errmsg,
2169b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2170b38ff370SJamie Gritton 				    errmsg_len);
2171b38ff370SJamie Gritton 		}
2172b38ff370SJamie Gritton 	}
2173b38ff370SJamie Gritton  done_free:
2174b38ff370SJamie Gritton #ifdef INET
2175eb8dcdeaSGleb Smirnoff 	prison_ip_free(ip4);
2176b38ff370SJamie Gritton #endif
2177b38ff370SJamie Gritton #ifdef INET6
2178eb8dcdeaSGleb Smirnoff 	prison_ip_free(ip6);
2179b38ff370SJamie Gritton #endif
21806dfe0a3dSMartin Matuska 	if (g_path != NULL)
21816dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
2182b38ff370SJamie Gritton 	vfs_freeopts(opts);
2183b38ff370SJamie Gritton 	return (error);
2184b38ff370SJamie Gritton }
2185b38ff370SJamie Gritton 
2186b38ff370SJamie Gritton /*
21877de883c8SJamie Gritton  * Find the next available prison ID.  Return the ID on success, or zero
21887de883c8SJamie Gritton  * on failure.  Also set a pointer to the allprison list entry the prison
21897de883c8SJamie Gritton  * should be inserted before.
21907de883c8SJamie Gritton  */
21917de883c8SJamie Gritton static int
21927de883c8SJamie Gritton get_next_prid(struct prison **insprp)
21937de883c8SJamie Gritton {
21947de883c8SJamie Gritton 	struct prison *inspr;
21957de883c8SJamie Gritton 	int jid, maxid;
21967de883c8SJamie Gritton 
21977de883c8SJamie Gritton 	jid = lastprid % JAIL_MAX + 1;
21987de883c8SJamie Gritton 	if (TAILQ_EMPTY(&allprison) ||
21997de883c8SJamie Gritton 	    TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
22007de883c8SJamie Gritton 		/*
22017de883c8SJamie Gritton 		 * A common case is for all jails to be implicitly numbered,
22027de883c8SJamie Gritton 		 * which means they'll go on the end of the list, at least
22037de883c8SJamie Gritton 		 * for the first JAIL_MAX times.
22047de883c8SJamie Gritton 		 */
22057de883c8SJamie Gritton 		inspr = NULL;
22067de883c8SJamie Gritton 	} else {
22077de883c8SJamie Gritton 		/*
22087de883c8SJamie Gritton 		 * Take two passes through the allprison list: first starting
22097de883c8SJamie Gritton 		 * with the proposed jid, then ending with it.
22107de883c8SJamie Gritton 		 */
22117de883c8SJamie Gritton 		for (maxid = JAIL_MAX; maxid != 0; ) {
22127de883c8SJamie Gritton 			TAILQ_FOREACH(inspr, &allprison, pr_list) {
22137de883c8SJamie Gritton 				if (inspr->pr_id < jid)
22147de883c8SJamie Gritton 					continue;
2215f7496dcaSJamie Gritton 				if (inspr->pr_id > jid) {
2216f7496dcaSJamie Gritton 					/* Found an opening. */
22177de883c8SJamie Gritton 					maxid = 0;
22187de883c8SJamie Gritton 					break;
22197de883c8SJamie Gritton 				}
22207de883c8SJamie Gritton 				if (++jid > maxid) {
22217de883c8SJamie Gritton 					if (lastprid == maxid || lastprid == 0)
22227de883c8SJamie Gritton 					{
22237de883c8SJamie Gritton 						/*
22247de883c8SJamie Gritton 						 * The entire legal range
22257de883c8SJamie Gritton 						 * has been traversed
22267de883c8SJamie Gritton 						 */
22277de883c8SJamie Gritton 						return 0;
22287de883c8SJamie Gritton 					}
22297de883c8SJamie Gritton 					/* Try again from the start. */
22307de883c8SJamie Gritton 					jid = 1;
22317de883c8SJamie Gritton 					maxid = lastprid;
22327de883c8SJamie Gritton 					break;
22337de883c8SJamie Gritton 				}
22347de883c8SJamie Gritton 			}
22357de883c8SJamie Gritton 			if (inspr == NULL) {
22367de883c8SJamie Gritton 				/* Found room at the end of the list. */
22377de883c8SJamie Gritton 				break;
22387de883c8SJamie Gritton 			}
22397de883c8SJamie Gritton 		}
22407de883c8SJamie Gritton 	}
22417de883c8SJamie Gritton 	*insprp = inspr;
22427de883c8SJamie Gritton 	lastprid = jid;
22437de883c8SJamie Gritton 	return (jid);
22447de883c8SJamie Gritton }
22457de883c8SJamie Gritton 
22467de883c8SJamie Gritton /*
2247b38ff370SJamie Gritton  * struct jail_get_args {
2248b38ff370SJamie Gritton  *	struct iovec *iovp;
2249b38ff370SJamie Gritton  *	unsigned int iovcnt;
2250b38ff370SJamie Gritton  *	int flags;
2251b38ff370SJamie Gritton  * };
2252b38ff370SJamie Gritton  */
2253b38ff370SJamie Gritton int
22548451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
2255b38ff370SJamie Gritton {
2256b38ff370SJamie Gritton 	struct uio *auio;
2257b38ff370SJamie Gritton 	int error;
2258b38ff370SJamie Gritton 
2259b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
2260b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
2261b38ff370SJamie Gritton 		return (EINVAL);
2262b38ff370SJamie Gritton 
2263b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2264b38ff370SJamie Gritton 	if (error)
2265b38ff370SJamie Gritton 		return (error);
2266b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
2267b38ff370SJamie Gritton 	if (error == 0)
2268b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
2269b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
2270b38ff370SJamie Gritton 	free(auio, M_IOV);
2271b38ff370SJamie Gritton 	return (error);
2272b38ff370SJamie Gritton }
2273b38ff370SJamie Gritton 
2274b38ff370SJamie Gritton int
2275b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2276b38ff370SJamie Gritton {
2277672756aaSJamie Gritton 	struct bool_flags *bf;
2278672756aaSJamie Gritton 	struct jailsys_flags *jsf;
22790304c731SJamie Gritton 	struct prison *pr, *mypr;
2280b38ff370SJamie Gritton 	struct vfsopt *opt;
2281b38ff370SJamie Gritton 	struct vfsoptlist *opts;
2282b38ff370SJamie Gritton 	char *errmsg, *name;
22832a4b2251SJamie Gritton 	int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2284672756aaSJamie Gritton 	unsigned f;
2285b38ff370SJamie Gritton 
2286b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
2287b38ff370SJamie Gritton 		return (EINVAL);
2288b38ff370SJamie Gritton 
2289b38ff370SJamie Gritton 	/* Get the parameter list. */
2290b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
2291b38ff370SJamie Gritton 	if (error)
2292b38ff370SJamie Gritton 		return (error);
2293b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
22940304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
22952a4b2251SJamie Gritton 	pr = NULL;
22961e2a13e6SJamie Gritton 
2297b38ff370SJamie Gritton 	/*
2298b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
2299b38ff370SJamie Gritton 	 */
2300b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
23012a4b2251SJamie Gritton 	drflags = PD_LIST_SLOCKED;
2302b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2303b38ff370SJamie Gritton 	if (error == 0) {
2304b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
2305f7496dcaSJamie Gritton 			if (pr->pr_id > jid &&
2306f7496dcaSJamie Gritton 			    ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2307f7496dcaSJamie Gritton 			    prison_ischild(mypr, pr)) {
2308b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
23092a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
2310b38ff370SJamie Gritton 				goto found_prison;
23112a4b2251SJamie Gritton 			}
2312f7496dcaSJamie Gritton 		}
2313b38ff370SJamie Gritton 		error = ENOENT;
2314b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
23152a4b2251SJamie Gritton 		goto done;
2316b38ff370SJamie Gritton 	} else if (error != ENOENT)
23172a4b2251SJamie Gritton 		goto done;
2318b38ff370SJamie Gritton 
2319b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2320b38ff370SJamie Gritton 	if (error == 0) {
2321b38ff370SJamie Gritton 		if (jid != 0) {
23220304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
2323b38ff370SJamie Gritton 			if (pr != NULL) {
23242a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
232576ad42abSJamie Gritton 				if (!(prison_isalive(pr) ||
232676ad42abSJamie Gritton 				    (flags & JAIL_DYING))) {
2327b38ff370SJamie Gritton 					error = ENOENT;
2328b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
2329b38ff370SJamie Gritton 					    jid);
23302a4b2251SJamie Gritton 					goto done;
2331b38ff370SJamie Gritton 				}
2332b38ff370SJamie Gritton 				goto found_prison;
2333b38ff370SJamie Gritton 			}
2334b38ff370SJamie Gritton 			error = ENOENT;
2335b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
23362a4b2251SJamie Gritton 			goto done;
2337b38ff370SJamie Gritton 		}
2338b38ff370SJamie Gritton 	} else if (error != ENOENT)
23392a4b2251SJamie Gritton 		goto done;
2340b38ff370SJamie Gritton 
2341b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2342b38ff370SJamie Gritton 	if (error == 0) {
2343b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2344b38ff370SJamie Gritton 			error = EINVAL;
23452a4b2251SJamie Gritton 			goto done;
2346b38ff370SJamie Gritton 		}
23470304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2348b38ff370SJamie Gritton 		if (pr != NULL) {
23492a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
235076ad42abSJamie Gritton 			if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2351b38ff370SJamie Gritton 				error = ENOENT;
2352b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2353b38ff370SJamie Gritton 				    name);
23542a4b2251SJamie Gritton 				goto done;
2355b38ff370SJamie Gritton 			}
2356b38ff370SJamie Gritton 			goto found_prison;
2357b38ff370SJamie Gritton 		}
2358b38ff370SJamie Gritton 		error = ENOENT;
2359b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
23602a4b2251SJamie Gritton 		goto done;
2361b38ff370SJamie Gritton 	} else if (error != ENOENT)
23622a4b2251SJamie Gritton 		goto done;
2363b38ff370SJamie Gritton 
2364b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2365b38ff370SJamie Gritton 	error = ENOENT;
23662a4b2251SJamie Gritton 	goto done;
2367b38ff370SJamie Gritton 
2368b38ff370SJamie Gritton  found_prison:
2369b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
23706754ae25SJamie Gritton 	prison_hold(pr);
23712a4b2251SJamie Gritton 	drflags |= PD_DEREF;
2372b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2373b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2374b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23752a4b2251SJamie Gritton 		goto done;
23760304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
23770304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2378b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23792a4b2251SJamie Gritton 		goto done;
23800304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
23810304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
23822a4b2251SJamie Gritton 		goto done;
23830304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2384b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2385b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23862a4b2251SJamie Gritton 		goto done;
23870304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2388b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23892a4b2251SJamie Gritton 		goto done;
2390b38ff370SJamie Gritton #ifdef INET
2391500f82d6SZhenlei Huang 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET]->pr_ip,
2392eb8dcdeaSGleb Smirnoff 	    pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2393eb8dcdeaSGleb Smirnoff 	    pr_families[PR_INET].size : 0 );
2394b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23952a4b2251SJamie Gritton 		goto done;
2396b38ff370SJamie Gritton #endif
2397b38ff370SJamie Gritton #ifdef INET6
2398500f82d6SZhenlei Huang 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6]->pr_ip,
2399eb8dcdeaSGleb Smirnoff 	    pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2400eb8dcdeaSGleb Smirnoff 	    pr_families[PR_INET6].size : 0 );
2401b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24022a4b2251SJamie Gritton 		goto done;
2403b38ff370SJamie Gritton #endif
2404b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2405b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2406b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24072a4b2251SJamie Gritton 		goto done;
2408b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2409b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2410b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
24112a4b2251SJamie Gritton 		goto done;
2412b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2413b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2414b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
24152a4b2251SJamie Gritton 		goto done;
2416c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2417b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24182a4b2251SJamie Gritton 		goto done;
2419c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
242076ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
24212a4b2251SJamie Gritton 		goto done;
2422c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
242376ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
24242a4b2251SJamie Gritton 		goto done;
2425841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2426a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
242776ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
242876ca6f88SJamie Gritton 
242976ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
243076ca6f88SJamie Gritton 	} else
243176ca6f88SJamie Gritton #endif
243276ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
243376ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
243476ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
24352a4b2251SJamie Gritton 		goto done;
24360304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
24370304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
24380304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
24392a4b2251SJamie Gritton 		goto done;
24400cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
24410cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
24420cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
24432a4b2251SJamie Gritton 		goto done;
2444672756aaSJamie Gritton 	for (bf = pr_flag_bool;
2445672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2446672756aaSJamie Gritton 	     bf++) {
2447672756aaSJamie Gritton 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2448672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2449b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
24502a4b2251SJamie Gritton 			goto done;
2451b38ff370SJamie Gritton 		i = !i;
2452672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2453b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
24542a4b2251SJamie Gritton 			goto done;
24550304c731SJamie Gritton 	}
2456672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
2457672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2458672756aaSJamie Gritton 	     jsf++) {
2459672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
2460672756aaSJamie Gritton 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2461672756aaSJamie Gritton 		    : (f == jsf->new) ? JAIL_SYS_NEW
24627cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
2463672756aaSJamie Gritton 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
24647cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
24652a4b2251SJamie Gritton 			goto done;
24667cbf7213SJamie Gritton 	}
2467672756aaSJamie Gritton 	for (bf = pr_flag_allow;
24685d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
24695d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
2470672756aaSJamie Gritton 	     bf++) {
2471672756aaSJamie Gritton 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2472672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
24730304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
24742a4b2251SJamie Gritton 			goto done;
24750304c731SJamie Gritton 		i = !i;
2476672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
24770304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
24782a4b2251SJamie Gritton 			goto done;
24790304c731SJamie Gritton 	}
248076ad42abSJamie Gritton 	i = !prison_isalive(pr);
2481b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2482b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24832a4b2251SJamie Gritton 		goto done;
2484b38ff370SJamie Gritton 	i = !i;
2485b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2486b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24872a4b2251SJamie Gritton 		goto done;
2488bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2489bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2490a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
24912a4b2251SJamie Gritton 		goto done;
2492a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2493a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
24942a4b2251SJamie Gritton 		goto done;
2495b38ff370SJamie Gritton 
2496b38ff370SJamie Gritton 	/* Get the module parameters. */
2497b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
24982a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
2499b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2500b38ff370SJamie Gritton 	if (error)
25012a4b2251SJamie Gritton 		goto done;
25022a4b2251SJamie Gritton 	prison_deref(pr, drflags);
25032a4b2251SJamie Gritton 	pr = NULL;
25042a4b2251SJamie Gritton 	drflags = 0;
2505b38ff370SJamie Gritton 
2506b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2507b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2508b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2509b38ff370SJamie Gritton 			error = EINVAL;
2510b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
25112a4b2251SJamie Gritton 			goto done;
2512b38ff370SJamie Gritton 		}
2513b38ff370SJamie Gritton 	}
2514b38ff370SJamie Gritton 
2515b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2516b38ff370SJamie Gritton 	error = 0;
2517b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2518b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2519b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2520b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2521b38ff370SJamie Gritton 			if (opt->value != NULL) {
2522b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2523b38ff370SJamie Gritton 					bcopy(opt->value,
2524b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2525b38ff370SJamie Gritton 					    opt->len);
2526b38ff370SJamie Gritton 				} else {
2527b38ff370SJamie Gritton 					error = copyout(opt->value,
2528b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2529b38ff370SJamie Gritton 					    opt->len);
2530b38ff370SJamie Gritton 					if (error)
2531b38ff370SJamie Gritton 						break;
2532b38ff370SJamie Gritton 				}
2533b38ff370SJamie Gritton 			}
2534b38ff370SJamie Gritton 		}
2535b38ff370SJamie Gritton 	}
2536b38ff370SJamie Gritton 
25372a4b2251SJamie Gritton  done:
25382a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
25392a4b2251SJamie Gritton 	if (pr != NULL)
25402a4b2251SJamie Gritton 		prison_deref(pr, drflags);
25412a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2542b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2543b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
25442a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2545b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2546b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2547b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2548b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2549b38ff370SJamie Gritton 				bcopy(errmsg,
2550b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2551b38ff370SJamie Gritton 				    errmsg_len);
2552b38ff370SJamie Gritton 			else
2553b38ff370SJamie Gritton 				copyout(errmsg,
2554b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2555b38ff370SJamie Gritton 				    errmsg_len);
2556b38ff370SJamie Gritton 		}
2557b38ff370SJamie Gritton 	}
2558b38ff370SJamie Gritton 	vfs_freeopts(opts);
2559b38ff370SJamie Gritton 	return (error);
2560b38ff370SJamie Gritton }
2561b38ff370SJamie Gritton 
2562b38ff370SJamie Gritton /*
2563b38ff370SJamie Gritton  * struct jail_remove_args {
2564b38ff370SJamie Gritton  *	int jid;
2565b38ff370SJamie Gritton  * };
2566b38ff370SJamie Gritton  */
2567b38ff370SJamie Gritton int
25688451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2569b38ff370SJamie Gritton {
2570c861373bSJamie Gritton 	struct prison *pr;
2571c861373bSJamie Gritton 	int error;
2572b38ff370SJamie Gritton 
2573b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2574b38ff370SJamie Gritton 	if (error)
2575b38ff370SJamie Gritton 		return (error);
2576b38ff370SJamie Gritton 
2577b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
25780304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2579b38ff370SJamie Gritton 	if (pr == NULL) {
2580b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2581b38ff370SJamie Gritton 		return (EINVAL);
2582b38ff370SJamie Gritton 	}
2583c861373bSJamie Gritton 	if (!prison_isalive(pr)) {
2584c861373bSJamie Gritton 		/* Silently ignore already-dying prisons. */
25850304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2586c861373bSJamie Gritton 		sx_xunlock(&allprison_lock);
25870304c731SJamie Gritton 		return (0);
25880304c731SJamie Gritton 	}
2589c861373bSJamie Gritton 	prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2590c861373bSJamie Gritton 	return (0);
259175c13541SPoul-Henning Kamp }
259275c13541SPoul-Henning Kamp 
2593fd7a8150SMike Barcroft /*
25949ddb7954SMike Barcroft  * struct jail_attach_args {
25959ddb7954SMike Barcroft  *	int jid;
25969ddb7954SMike Barcroft  * };
2597fd7a8150SMike Barcroft  */
2598fd7a8150SMike Barcroft int
25998451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2600fd7a8150SMike Barcroft {
2601b38ff370SJamie Gritton 	struct prison *pr;
2602b38ff370SJamie Gritton 	int error;
2603b38ff370SJamie Gritton 
2604b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2605b38ff370SJamie Gritton 	if (error)
2606b38ff370SJamie Gritton 		return (error);
2607b38ff370SJamie Gritton 
2608f7496dcaSJamie Gritton 	sx_slock(&allprison_lock);
26090304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2610b38ff370SJamie Gritton 	if (pr == NULL) {
2611b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2612b38ff370SJamie Gritton 		return (EINVAL);
2613b38ff370SJamie Gritton 	}
2614b38ff370SJamie Gritton 
261576ad42abSJamie Gritton 	/* Do not allow a process to attach to a prison that is not alive. */
261676ad42abSJamie Gritton 	if (!prison_isalive(pr)) {
2617b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2618b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2619b38ff370SJamie Gritton 		return (EINVAL);
2620b38ff370SJamie Gritton 	}
2621b38ff370SJamie Gritton 
2622f7496dcaSJamie Gritton 	return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2623b38ff370SJamie Gritton }
2624b38ff370SJamie Gritton 
2625b38ff370SJamie Gritton static int
2626f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2627b38ff370SJamie Gritton {
2628fd7a8150SMike Barcroft 	struct proc *p;
2629fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
26305050aa86SKonstantin Belousov 	int error;
2631fd7a8150SMike Barcroft 
2632f7496dcaSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2633f7496dcaSJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
2634589e4c1dSJamie Gritton 	drflags &= PD_LOCK_FLAGS;
263557f22bd4SJacques Vidrine 	/*
263657f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
263757f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
263857f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
263957f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
264057f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
264157f22bd4SJacques Vidrine 	 * of another.
264257f22bd4SJacques Vidrine 	 */
26431158508aSJamie Gritton 	prison_hold(pr);
26446754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
2645f7496dcaSJamie Gritton 	drflags |= PD_DEREF | PD_DEUREF;
2646fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2647f7496dcaSJamie Gritton 	drflags &= ~PD_LOCKED;
2648b38ff370SJamie Gritton 
2649b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2650b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2651b38ff370SJamie Gritton 	if (error) {
2652f7496dcaSJamie Gritton 		prison_deref(pr, drflags);
2653b38ff370SJamie Gritton 		return (error);
2654b38ff370SJamie Gritton 	}
2655f7496dcaSJamie Gritton 	sx_unlock(&allprison_lock);
2656f7496dcaSJamie Gritton 	drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2657fd7a8150SMike Barcroft 
2658413628a7SBjoern A. Zeeb 	/*
2659413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2660413628a7SBjoern A. Zeeb 	 */
2661b38ff370SJamie Gritton 	p = td->td_proc;
2662413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2663413628a7SBjoern A. Zeeb 	if (error)
2664b38ff370SJamie Gritton 		goto e_revert_osd;
2665413628a7SBjoern A. Zeeb 
2666cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2667fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2668fd7a8150SMike Barcroft 		goto e_unlock;
2669fd7a8150SMike Barcroft #ifdef MAC
267030d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2671fd7a8150SMike Barcroft 		goto e_unlock;
2672fd7a8150SMike Barcroft #endif
2673b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2674d4380c0cSJamie Gritton 	if ((error = pwd_chroot_chdir(td, pr->pr_root)))
26755050aa86SKonstantin Belousov 		goto e_revert_osd;
2676fd7a8150SMike Barcroft 
2677fd7a8150SMike Barcroft 	newcred = crget();
2678fd7a8150SMike Barcroft 	PROC_LOCK(p);
26791fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
268069c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2681daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
26821fb6767dSJamie Gritton 	setsugid(p);
2683097055e2SEdward Tomasz Napierala #ifdef RACCT
2684097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2685f87beb93SAndriy Gapon 	crhold(newcred);
2686f87beb93SAndriy Gapon #endif
2687f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
2688f87beb93SAndriy Gapon #ifdef RCTL
2689f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
2690f87beb93SAndriy Gapon 	crfree(newcred);
2691097055e2SEdward Tomasz Napierala #endif
26925ecb5444SMateusz Guzik 	prison_proc_relink(oldcred->cr_prison, pr, p);
2693f7496dcaSJamie Gritton 	prison_deref(oldcred->cr_prison, drflags);
2694fd7a8150SMike Barcroft 	crfree(oldcred);
2695cc7b7306SJamie Gritton 
2696cc7b7306SJamie Gritton 	/*
2697cc7b7306SJamie Gritton 	 * If the prison was killed while changing credentials, die along
2698cc7b7306SJamie Gritton 	 * with it.
2699cc7b7306SJamie Gritton 	 */
2700cc7b7306SJamie Gritton 	if (!prison_isalive(pr)) {
2701cc7b7306SJamie Gritton 		PROC_LOCK(p);
2702cc7b7306SJamie Gritton 		kern_psignal(p, SIGKILL);
2703cc7b7306SJamie Gritton 		PROC_UNLOCK(p);
2704cc7b7306SJamie Gritton 	}
2705cc7b7306SJamie Gritton 
2706fd7a8150SMike Barcroft 	return (0);
27071fb6767dSJamie Gritton 
2708fd7a8150SMike Barcroft  e_unlock:
2709b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2710b38ff370SJamie Gritton  e_revert_osd:
2711b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
27127f4e7248SJamie Gritton 	sx_slock(&allprison_lock);
2713f7496dcaSJamie Gritton 	drflags |= PD_LIST_SLOCKED;
27141fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2715f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2716fd7a8150SMike Barcroft 	return (error);
2717fd7a8150SMike Barcroft }
2718fd7a8150SMike Barcroft 
2719fd7a8150SMike Barcroft /*
2720fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2721fd7a8150SMike Barcroft  */
272254b369c1SPawel Jakub Dawidek struct prison *
2723fd7a8150SMike Barcroft prison_find(int prid)
2724fd7a8150SMike Barcroft {
2725fd7a8150SMike Barcroft 	struct prison *pr;
2726fd7a8150SMike Barcroft 
2727dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2728b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2729f7496dcaSJamie Gritton 		if (pr->pr_id < prid)
2730f7496dcaSJamie Gritton 			continue;
27317de883c8SJamie Gritton 		if (pr->pr_id > prid)
27327de883c8SJamie Gritton 			break;
2733f7496dcaSJamie Gritton 		KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2734f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
2735f7496dcaSJamie Gritton 		return (pr);
2736fd7a8150SMike Barcroft 	}
2737fd7a8150SMike Barcroft 	return (NULL);
2738fd7a8150SMike Barcroft }
2739fd7a8150SMike Barcroft 
2740b38ff370SJamie Gritton /*
27410304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2742b38ff370SJamie Gritton  */
2743b38ff370SJamie Gritton struct prison *
27440304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2745b38ff370SJamie Gritton {
27460304c731SJamie Gritton 	struct prison *pr;
27470304c731SJamie Gritton 	int descend;
2748b38ff370SJamie Gritton 
2749b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
27500304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
27510304c731SJamie Gritton 		if (pr->pr_id == prid) {
2752f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2753f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
27540304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27550304c731SJamie Gritton 			return (pr);
27560304c731SJamie Gritton 		}
27570304c731SJamie Gritton 	}
27580304c731SJamie Gritton 	return (NULL);
27590304c731SJamie Gritton }
27600304c731SJamie Gritton 
27610304c731SJamie Gritton /*
27620304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
27630304c731SJamie Gritton  */
27640304c731SJamie Gritton struct prison *
27650304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
27660304c731SJamie Gritton {
27670304c731SJamie Gritton 	struct prison *pr, *deadpr;
27680304c731SJamie Gritton 	size_t mylen;
27690304c731SJamie Gritton 	int descend;
27700304c731SJamie Gritton 
27710304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
27720304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2773b38ff370SJamie Gritton 	deadpr = NULL;
27740304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
27750304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2776f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2777f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
2778f7496dcaSJamie Gritton 			if (prison_isalive(pr)) {
2779b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
2780b38ff370SJamie Gritton 				return (pr);
2781f7496dcaSJamie Gritton 			}
2782b38ff370SJamie Gritton 			deadpr = pr;
2783b38ff370SJamie Gritton 		}
2784b38ff370SJamie Gritton 	}
27850304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2786f7496dcaSJamie Gritton 	if (deadpr != NULL)
2787b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2788b38ff370SJamie Gritton 	return (deadpr);
2789b38ff370SJamie Gritton }
2790b38ff370SJamie Gritton 
2791b38ff370SJamie Gritton /*
27920fe74ae6SJamie Gritton  * See if a prison has the specific flag set.  The prison should be locked,
27930fe74ae6SJamie Gritton  * unless checking for flags that are only set at jail creation (such as
27940fe74ae6SJamie Gritton  * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
27950fe74ae6SJamie Gritton  * to any other prison data.
27960304c731SJamie Gritton  */
2797*0b0ae2e4SMina Galić bool
27980304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
27990304c731SJamie Gritton {
28000304c731SJamie Gritton 
2801*0b0ae2e4SMina Galić 	return ((cred->cr_prison->pr_flags & flag) != 0);
28020304c731SJamie Gritton }
28030304c731SJamie Gritton 
2804*0b0ae2e4SMina Galić /*
2805*0b0ae2e4SMina Galić  * See if a prison has the specific allow flag set.
2806*0b0ae2e4SMina Galić  * The prison *should* be locked, or only a single bit is examined, without
2807*0b0ae2e4SMina Galić  * regard to any other prison data.
2808*0b0ae2e4SMina Galić  */
2809*0b0ae2e4SMina Galić bool
28100304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
28110304c731SJamie Gritton {
28120304c731SJamie Gritton 
28130fe74ae6SJamie Gritton 	return ((cred->cr_prison->pr_allow & flag) != 0);
28140304c731SJamie Gritton }
28150304c731SJamie Gritton 
28160304c731SJamie Gritton /*
2817effad35eSJamie Gritton  * Hold a prison reference, by incrementing pr_ref.  It is generally
2818effad35eSJamie Gritton  * an error to hold a prison that does not already have a reference.
2819effad35eSJamie Gritton  * A prison record will remain valid as long as it has at least one
2820effad35eSJamie Gritton  * reference, and will not be removed as long as either the prison
2821effad35eSJamie Gritton  * mutex or the allprison lock is held (allprison_lock may be shared).
2822effad35eSJamie Gritton  */
2823effad35eSJamie Gritton void
2824effad35eSJamie Gritton prison_hold_locked(struct prison *pr)
2825effad35eSJamie Gritton {
2826effad35eSJamie Gritton 
28276754ae25SJamie Gritton 	/* Locking is no longer required. */
28286754ae25SJamie Gritton 	prison_hold(pr);
2829effad35eSJamie Gritton }
2830effad35eSJamie Gritton 
2831effad35eSJamie Gritton void
2832effad35eSJamie Gritton prison_hold(struct prison *pr)
2833effad35eSJamie Gritton {
28346754ae25SJamie Gritton #ifdef INVARIANTS
28356754ae25SJamie Gritton 	int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2836effad35eSJamie Gritton 
28376754ae25SJamie Gritton 	KASSERT(was_valid,
28386754ae25SJamie Gritton 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
28396754ae25SJamie Gritton #else
28406754ae25SJamie Gritton 	refcount_acquire(&pr->pr_ref);
28416754ae25SJamie Gritton #endif
2842effad35eSJamie Gritton }
2843effad35eSJamie Gritton 
2844effad35eSJamie Gritton /*
2845effad35eSJamie Gritton  * Remove a prison reference.  If that was the last reference, the
2846f7496dcaSJamie Gritton  * prison will be removed (at a later time).
2847b38ff370SJamie Gritton  */
284891421ba2SRobert Watson void
28491ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
285091421ba2SRobert Watson {
285191421ba2SRobert Watson 
28521ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2853effad35eSJamie Gritton 	/*
2854f7496dcaSJamie Gritton 	 * Locking is no longer required, but unlock because the caller
2855f7496dcaSJamie Gritton 	 * expects it.
2856effad35eSJamie Gritton 	 */
2857f7496dcaSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2858f7496dcaSJamie Gritton 	prison_free(pr);
2859effad35eSJamie Gritton }
2860c2cda609SPawel Jakub Dawidek 
28611ba4a712SPawel Jakub Dawidek void
28621ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
28631ba4a712SPawel Jakub Dawidek {
28641ba4a712SPawel Jakub Dawidek 
28656754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
28666754ae25SJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
28676754ae25SJamie Gritton 	     pr, pr->pr_id));
2868f7496dcaSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_ref)) {
2869f7496dcaSJamie Gritton 		/*
2870f7496dcaSJamie Gritton 		 * Don't remove the last reference in this context,
2871f7496dcaSJamie Gritton 		 * in case there are locks held.
2872f7496dcaSJamie Gritton 		 */
2873f7496dcaSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2874f7496dcaSJamie Gritton 	}
2875f7496dcaSJamie Gritton }
2876f7496dcaSJamie Gritton 
2877f7496dcaSJamie Gritton static void
2878f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr)
2879f7496dcaSJamie Gritton {
2880f7496dcaSJamie Gritton #ifdef INVARIANTS
2881f7496dcaSJamie Gritton 	int lastref;
2882f7496dcaSJamie Gritton 
2883f7496dcaSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
2884f7496dcaSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2885f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2886f7496dcaSJamie Gritton 	lastref = refcount_release(&pr->pr_ref);
2887f7496dcaSJamie Gritton 	KASSERT(!lastref,
2888f7496dcaSJamie Gritton 	    ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2889f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2890f7496dcaSJamie Gritton #else
2891ee9b37aeSMateusz Guzik 	refcount_release(&pr->pr_ref);
2892f7496dcaSJamie Gritton #endif
28931ba4a712SPawel Jakub Dawidek }
28941ba4a712SPawel Jakub Dawidek 
289573d9e52dSJamie Gritton /*
2896f171938cSGordon Bergling  * Hold a prison for user visibility, by incrementing pr_uref.
2897effad35eSJamie Gritton  * It is generally an error to hold a prison that isn't already
289849a033d8SGordon Bergling  * user-visible, except through the jail system calls.  It is also
2899effad35eSJamie Gritton  * an error to hold an invalid prison.  A prison record will remain
2900effad35eSJamie Gritton  * alive as long as it has at least one user reference, and will not
2901f7496dcaSJamie Gritton  * be set to the dying state until the prison mutex and allprison_lock
2902f7496dcaSJamie Gritton  * are both freed.
2903effad35eSJamie Gritton  */
2904effad35eSJamie Gritton void
2905effad35eSJamie Gritton prison_proc_hold(struct prison *pr)
2906effad35eSJamie Gritton {
29076754ae25SJamie Gritton #ifdef INVARIANTS
29086754ae25SJamie Gritton 	int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2909effad35eSJamie Gritton 
29106754ae25SJamie Gritton 	KASSERT(was_alive,
2911effad35eSJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
29126754ae25SJamie Gritton #else
29136754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
29146754ae25SJamie Gritton #endif
2915effad35eSJamie Gritton }
2916effad35eSJamie Gritton 
2917effad35eSJamie Gritton /*
2918effad35eSJamie Gritton  * Remove a prison user reference.  If it was the last reference, the
2919effad35eSJamie Gritton  * prison will be considered "dying", and may be removed once all of
2920effad35eSJamie Gritton  * its references are dropped.
2921effad35eSJamie Gritton  */
2922effad35eSJamie Gritton void
2923effad35eSJamie Gritton prison_proc_free(struct prison *pr)
2924effad35eSJamie Gritton {
2925effad35eSJamie Gritton 
29266754ae25SJamie Gritton 	/*
29276754ae25SJamie Gritton 	 * Locking is only required when releasing the last reference.
29286754ae25SJamie Gritton 	 * This allows assurance that a locked prison will remain alive
29296754ae25SJamie Gritton 	 * until it is unlocked.
29306754ae25SJamie Gritton 	 */
29316754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2932effad35eSJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2933195cd6aeSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_uref)) {
2934effad35eSJamie Gritton 		/*
2935effad35eSJamie Gritton 		 * Don't remove the last user reference in this context,
2936effad35eSJamie Gritton 		 * which is expected to be a process that is not only locked,
2937effad35eSJamie Gritton 		 * but also half dead.  Add a reference so any calls to
2938effad35eSJamie Gritton 		 * prison_free() won't re-submit the task.
2939effad35eSJamie Gritton 		 */
2940f7496dcaSJamie Gritton 		prison_hold(pr);
29411158508aSJamie Gritton 		mtx_lock(&pr->pr_mtx);
29421158508aSJamie Gritton 		KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
29431158508aSJamie Gritton 		    ("Redundant last reference in prison_proc_free (jid=%d)",
29441158508aSJamie Gritton 		     pr->pr_id));
29451158508aSJamie Gritton 		pr->pr_flags |= PR_COMPLETE_PROC;
29461158508aSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2947effad35eSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2948effad35eSJamie Gritton 	}
2949effad35eSJamie Gritton }
2950effad35eSJamie Gritton 
2951c861373bSJamie Gritton static void
2952c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr)
2953c861373bSJamie Gritton {
2954c861373bSJamie Gritton #ifdef INVARIANTS
2955c861373bSJamie Gritton 	int lastref;
2956c861373bSJamie Gritton 
2957c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2958c861373bSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2959c861373bSJamie Gritton 	     pr, pr->pr_id));
2960c861373bSJamie Gritton 	lastref = refcount_release(&pr->pr_uref);
2961c861373bSJamie Gritton 	KASSERT(!lastref,
2962c861373bSJamie Gritton 	    ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2963c861373bSJamie Gritton 	     pr, pr->pr_id));
2964c861373bSJamie Gritton #else
2965c861373bSJamie Gritton 	refcount_release(&pr->pr_uref);
2966c861373bSJamie Gritton #endif
2967c861373bSJamie Gritton }
2968c861373bSJamie Gritton 
29695ecb5444SMateusz Guzik void
29705ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p)
29715ecb5444SMateusz Guzik {
29725ecb5444SMateusz Guzik 
29735ecb5444SMateusz Guzik 	sx_assert(&allproc_lock, SA_XLOCKED);
29745ecb5444SMateusz Guzik 	LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist);
29755ecb5444SMateusz Guzik }
29765ecb5444SMateusz Guzik 
29775ecb5444SMateusz Guzik void
29785ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p)
29795ecb5444SMateusz Guzik {
29805ecb5444SMateusz Guzik 
29815ecb5444SMateusz Guzik 	sx_assert(&allproc_lock, SA_XLOCKED);
29825ecb5444SMateusz Guzik 	LIST_REMOVE(p, p_jaillist);
29835ecb5444SMateusz Guzik }
29845ecb5444SMateusz Guzik 
29855ecb5444SMateusz Guzik static void
29865ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p)
29875ecb5444SMateusz Guzik {
29885ecb5444SMateusz Guzik 
29895ecb5444SMateusz Guzik 	sx_xlock(&allproc_lock);
29905ecb5444SMateusz Guzik 	prison_proc_unlink(opr, p);
29915ecb5444SMateusz Guzik 	prison_proc_link(npr, p);
29925ecb5444SMateusz Guzik 	sx_xunlock(&allproc_lock);
29935ecb5444SMateusz Guzik }
29945ecb5444SMateusz Guzik 
2995effad35eSJamie Gritton /*
299673d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
299773d9e52dSJamie Gritton  */
2998c2cda609SPawel Jakub Dawidek static void
2999c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
3000c2cda609SPawel Jakub Dawidek {
300173d9e52dSJamie Gritton 	struct prison *pr = context;
3002f7496dcaSJamie Gritton 	int drflags;
3003b38ff370SJamie Gritton 
3004effad35eSJamie Gritton 	/*
30051158508aSJamie Gritton 	 * This could be called to release the last reference, or the last
30061158508aSJamie Gritton 	 * user reference (plus the reference held in prison_proc_free).
3007effad35eSJamie Gritton 	 */
3008f7496dcaSJamie Gritton 	drflags = prison_lock_xlock(pr, PD_DEREF);
30091158508aSJamie Gritton 	if (pr->pr_flags & PR_COMPLETE_PROC) {
30101158508aSJamie Gritton 		pr->pr_flags &= ~PR_COMPLETE_PROC;
3011f7496dcaSJamie Gritton 		drflags |= PD_DEUREF;
30121158508aSJamie Gritton 	}
3013f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
3014b38ff370SJamie Gritton }
3015b38ff370SJamie Gritton 
30165ecb5444SMateusz Guzik static void
30175ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused)
30185ecb5444SMateusz Guzik {
30195ecb5444SMateusz Guzik 
30205ecb5444SMateusz Guzik 	kern_psignal(p, SIGKILL);
30215ecb5444SMateusz Guzik }
30225ecb5444SMateusz Guzik 
30235ecb5444SMateusz Guzik /*
30245ecb5444SMateusz Guzik  * Note the iteration does not guarantee acting on all processes.
30255ecb5444SMateusz Guzik  * Most notably there may be fork or jail_attach in progress.
30265ecb5444SMateusz Guzik  */
30275ecb5444SMateusz Guzik void
30285ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *),
30295ecb5444SMateusz Guzik     void *cbarg)
30305ecb5444SMateusz Guzik {
30315ecb5444SMateusz Guzik 	struct prison *ppr;
30325ecb5444SMateusz Guzik 	struct proc *p;
30335ecb5444SMateusz Guzik 
30345ecb5444SMateusz Guzik 	if (atomic_load_int(&pr->pr_childcount) == 0) {
30355ecb5444SMateusz Guzik 		sx_slock(&allproc_lock);
30365ecb5444SMateusz Guzik 		LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) {
30375ecb5444SMateusz Guzik 			if (p->p_state == PRS_NEW)
30385ecb5444SMateusz Guzik 				continue;
30395ecb5444SMateusz Guzik 			PROC_LOCK(p);
30405ecb5444SMateusz Guzik 			cb(p, cbarg);
30415ecb5444SMateusz Guzik 			PROC_UNLOCK(p);
30425ecb5444SMateusz Guzik 		}
30435ecb5444SMateusz Guzik 		sx_sunlock(&allproc_lock);
30445ecb5444SMateusz Guzik 		if (atomic_load_int(&pr->pr_childcount) == 0)
30455ecb5444SMateusz Guzik 			return;
30465ecb5444SMateusz Guzik 		/*
30475ecb5444SMateusz Guzik 		 * Some jails popped up during the iteration, fall through to a
30485ecb5444SMateusz Guzik 		 * system-wide search.
30495ecb5444SMateusz Guzik 		 */
30505ecb5444SMateusz Guzik 	}
30515ecb5444SMateusz Guzik 
30525ecb5444SMateusz Guzik 	sx_slock(&allproc_lock);
30535ecb5444SMateusz Guzik 	FOREACH_PROC_IN_SYSTEM(p) {
30545ecb5444SMateusz Guzik 		PROC_LOCK(p);
30555ecb5444SMateusz Guzik 		if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
30565ecb5444SMateusz Guzik 			for (ppr = p->p_ucred->cr_prison;
30575ecb5444SMateusz Guzik 			    ppr != &prison0;
30585ecb5444SMateusz Guzik 			    ppr = ppr->pr_parent) {
30595ecb5444SMateusz Guzik 				if (ppr == pr) {
30605ecb5444SMateusz Guzik 					cb(p, cbarg);
30615ecb5444SMateusz Guzik 					break;
30625ecb5444SMateusz Guzik 				}
30635ecb5444SMateusz Guzik 			}
30645ecb5444SMateusz Guzik 		}
30655ecb5444SMateusz Guzik 		PROC_UNLOCK(p);
30665ecb5444SMateusz Guzik 	}
30675ecb5444SMateusz Guzik 	sx_sunlock(&allproc_lock);
30685ecb5444SMateusz Guzik }
30695ecb5444SMateusz Guzik 
3070b38ff370SJamie Gritton /*
3071effad35eSJamie Gritton  * Remove a prison reference and/or user reference (usually).
3072effad35eSJamie Gritton  * This assumes context that allows sleeping (for allprison_lock),
3073effad35eSJamie Gritton  * with no non-sleeping locks held, except perhaps the prison itself.
3074effad35eSJamie Gritton  * If there are no more references, release and delist the prison.
3075effad35eSJamie Gritton  * On completion, the prison lock and the allprison lock are both
3076effad35eSJamie Gritton  * unlocked.
3077b38ff370SJamie Gritton  */
3078b38ff370SJamie Gritton static void
3079b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
3080b38ff370SJamie Gritton {
30816e1d1bfcSJamie Gritton 	struct prisonlist freeprison;
3082c861373bSJamie Gritton 	struct prison *killpr, *rpr, *ppr, *tpr;
3083c2cda609SPawel Jakub Dawidek 
3084c861373bSJamie Gritton 	killpr = NULL;
30856e1d1bfcSJamie Gritton 	TAILQ_INIT(&freeprison);
30866e1d1bfcSJamie Gritton 	/*
30876e1d1bfcSJamie Gritton 	 * Release this prison as requested, which may cause its parent
30886e1d1bfcSJamie Gritton 	 * to be released, and then maybe its grandparent, etc.
30896e1d1bfcSJamie Gritton 	 */
30900304c731SJamie Gritton 	for (;;) {
3091c861373bSJamie Gritton 		if (flags & PD_KILL) {
3092c861373bSJamie Gritton 			/* Kill the prison and its descendents. */
3093589e4c1dSJamie Gritton 			KASSERT(pr != &prison0,
3094589e4c1dSJamie Gritton 			    ("prison_deref trying to kill prison0"));
3095c861373bSJamie Gritton 			if (!(flags & PD_DEREF)) {
3096c861373bSJamie Gritton 				prison_hold(pr);
3097c861373bSJamie Gritton 				flags |= PD_DEREF;
3098c861373bSJamie Gritton 			}
3099c861373bSJamie Gritton 			flags = prison_lock_xlock(pr, flags);
3100c861373bSJamie Gritton 			prison_deref_kill(pr, &freeprison);
3101c861373bSJamie Gritton 		}
3102e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
3103f7496dcaSJamie Gritton 			/* Drop a user reference. */
31046754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_uref) > 0,
310573d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
310673d9e52dSJamie Gritton 			     pr->pr_id));
3107f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_uref)) {
3108f7496dcaSJamie Gritton 				if (!(flags & PD_DEREF)) {
3109f7496dcaSJamie Gritton 					prison_hold(pr);
3110f7496dcaSJamie Gritton 					flags |= PD_DEREF;
3111f7496dcaSJamie Gritton 				}
3112f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
31131158508aSJamie Gritton 				if (refcount_release(&pr->pr_uref) &&
31141158508aSJamie Gritton 				    pr->pr_state == PRISON_STATE_ALIVE) {
3115f7496dcaSJamie Gritton 					/*
3116f7496dcaSJamie Gritton 					 * When the last user references goes,
3117f7496dcaSJamie Gritton 					 * this becomes a dying prison.
3118f7496dcaSJamie Gritton 					 */
3119f7496dcaSJamie Gritton 					KASSERT(
3120f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_uref) > 0,
31216754ae25SJamie Gritton 					    ("prison0 pr_uref=0"));
31221158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_DYING;
3123f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
3124f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
3125a9f7455cSJamie Gritton 					prison_cleanup(pr);
3126f7496dcaSJamie Gritton 				}
3127f7496dcaSJamie Gritton 			}
3128f7496dcaSJamie Gritton 		}
3129c861373bSJamie Gritton 		if (flags & PD_KILL) {
3130c861373bSJamie Gritton 			/*
3131c861373bSJamie Gritton 			 * Any remaining user references are probably processes
3132c861373bSJamie Gritton 			 * that need to be killed, either in this prison or its
3133c861373bSJamie Gritton 			 * descendants.
3134c861373bSJamie Gritton 			 */
3135c861373bSJamie Gritton 			if (refcount_load(&pr->pr_uref) > 0)
3136c861373bSJamie Gritton 				killpr = pr;
3137589e4c1dSJamie Gritton 			/* Make sure the parent prison doesn't get killed. */
3138589e4c1dSJamie Gritton 			flags &= ~PD_KILL;
3139c861373bSJamie Gritton 		}
314073d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
3141f7496dcaSJamie Gritton 			/* Drop a reference. */
31426754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_ref) > 0,
314373d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
314473d9e52dSJamie Gritton 			     pr->pr_id));
3145f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_ref)) {
3146f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
3147f7496dcaSJamie Gritton 				if (refcount_release(&pr->pr_ref)) {
3148cc5fd8c7SJamie Gritton 					/*
3149f7496dcaSJamie Gritton 					 * When the last reference goes,
3150f7496dcaSJamie Gritton 					 * unlink the prison and set it aside.
3151cc5fd8c7SJamie Gritton 					 */
3152f7496dcaSJamie Gritton 					KASSERT(
3153f7496dcaSJamie Gritton 					    refcount_load(&pr->pr_uref) == 0,
3154f7496dcaSJamie Gritton 					    ("prison_deref: last ref, "
3155f7496dcaSJamie Gritton 					     "but still has %d urefs (jid=%d)",
3156f7496dcaSJamie Gritton 					     pr->pr_uref, pr->pr_id));
3157f7496dcaSJamie Gritton 					KASSERT(
3158f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_ref) != 0,
3159f7496dcaSJamie Gritton 					    ("prison0 pr_ref=0"));
31601158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_INVALID;
3161b38ff370SJamie Gritton 					TAILQ_REMOVE(&allprison, pr, pr_list);
31620304c731SJamie Gritton 					LIST_REMOVE(pr, pr_sibling);
3163f7496dcaSJamie Gritton 					TAILQ_INSERT_TAIL(&freeprison, pr,
3164f7496dcaSJamie Gritton 					    pr_list);
3165f7496dcaSJamie Gritton 					for (ppr = pr->pr_parent;
3166f7496dcaSJamie Gritton 					     ppr != NULL;
3167f7496dcaSJamie Gritton 					     ppr = ppr->pr_parent)
3168f7496dcaSJamie Gritton 						ppr->pr_childcount--;
3169f7496dcaSJamie Gritton 					/*
3170f7496dcaSJamie Gritton 					 * Removing a prison frees references
3171f7496dcaSJamie Gritton 					 * from its parent.
3172f7496dcaSJamie Gritton 					 */
3173f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
3174f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
31756e1d1bfcSJamie Gritton 					pr = pr->pr_parent;
31766e1d1bfcSJamie Gritton 					flags |= PD_DEREF | PD_DEUREF;
3177f7496dcaSJamie Gritton 					continue;
3178f7496dcaSJamie Gritton 				}
3179f7496dcaSJamie Gritton 			}
3180f7496dcaSJamie Gritton 		}
3181f7496dcaSJamie Gritton 		break;
31826e1d1bfcSJamie Gritton 	}
31836e1d1bfcSJamie Gritton 
31846e1d1bfcSJamie Gritton 	/* Release all the prison locks. */
3185f7496dcaSJamie Gritton 	if (flags & PD_LOCKED)
3186f7496dcaSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31876e1d1bfcSJamie Gritton 	if (flags & PD_LIST_SLOCKED)
31886e1d1bfcSJamie Gritton 		sx_sunlock(&allprison_lock);
31896e1d1bfcSJamie Gritton 	else if (flags & PD_LIST_XLOCKED)
31906e1d1bfcSJamie Gritton 		sx_xunlock(&allprison_lock);
31916e1d1bfcSJamie Gritton 
3192c861373bSJamie Gritton 	/* Kill any processes attached to a killed prison. */
31935ecb5444SMateusz Guzik 	if (killpr != NULL)
31945ecb5444SMateusz Guzik 		prison_proc_iterate(killpr, prison_kill_processes_cb, NULL);
3195c861373bSJamie Gritton 
31966e1d1bfcSJamie Gritton 	/*
31976e1d1bfcSJamie Gritton 	 * Finish removing any unreferenced prisons, which couldn't happen
31986e1d1bfcSJamie Gritton 	 * while allprison_lock was held (to avoid a LOR on vrele).
31996e1d1bfcSJamie Gritton 	 */
32006e1d1bfcSJamie Gritton 	TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
32016e1d1bfcSJamie Gritton #ifdef VIMAGE
32026e1d1bfcSJamie Gritton 		if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
32036e1d1bfcSJamie Gritton 			vnet_destroy(rpr->pr_vnet);
32046e1d1bfcSJamie Gritton #endif
32056e1d1bfcSJamie Gritton 		if (rpr->pr_root != NULL)
32066e1d1bfcSJamie Gritton 			vrele(rpr->pr_root);
32076e1d1bfcSJamie Gritton 		mtx_destroy(&rpr->pr_mtx);
32086e1d1bfcSJamie Gritton #ifdef INET
3209eb8dcdeaSGleb Smirnoff 		prison_ip_free(rpr->pr_addrs[PR_INET]);
32106e1d1bfcSJamie Gritton #endif
32116e1d1bfcSJamie Gritton #ifdef INET6
3212eb8dcdeaSGleb Smirnoff 		prison_ip_free(rpr->pr_addrs[PR_INET6]);
32136e1d1bfcSJamie Gritton #endif
32146e1d1bfcSJamie Gritton 		if (rpr->pr_cpuset != NULL)
32156e1d1bfcSJamie Gritton 			cpuset_rel(rpr->pr_cpuset);
32166e1d1bfcSJamie Gritton 		osd_jail_exit(rpr);
32176e1d1bfcSJamie Gritton #ifdef RACCT
32186e1d1bfcSJamie Gritton 		if (racct_enable)
32196e1d1bfcSJamie Gritton 			prison_racct_detach(rpr);
32206e1d1bfcSJamie Gritton #endif
3221f7496dcaSJamie Gritton 		TAILQ_REMOVE(&freeprison, rpr, pr_list);
32226e1d1bfcSJamie Gritton 		free(rpr, M_PRISON);
32230304c731SJamie Gritton 	}
3224b3059e09SRobert Watson }
3225b3059e09SRobert Watson 
3226413628a7SBjoern A. Zeeb /*
3227c861373bSJamie Gritton  * Kill the prison and its descendants.  Mark them as dying, clear the
3228c861373bSJamie Gritton  * persist flag, and call module remove methods.
3229c861373bSJamie Gritton  */
3230c861373bSJamie Gritton static void
3231c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3232c861373bSJamie Gritton {
3233c861373bSJamie Gritton 	struct prison *cpr, *ppr, *rpr;
3234c861373bSJamie Gritton 	bool descend;
3235c861373bSJamie Gritton 
3236c861373bSJamie Gritton 	/*
3237c861373bSJamie Gritton 	 * Unlike the descendants, the target prison can be killed
3238c861373bSJamie Gritton 	 * even if it is currently dying.  This is useful for failed
3239c861373bSJamie Gritton 	 * creation in jail_set(2).
3240c861373bSJamie Gritton 	 */
3241c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
3242c861373bSJamie Gritton 	    ("Trying to kill dead prison %p (jid=%d).",
3243c861373bSJamie Gritton 	     pr, pr->pr_id));
3244c861373bSJamie Gritton 	refcount_acquire(&pr->pr_uref);
3245c861373bSJamie Gritton 	pr->pr_state = PRISON_STATE_DYING;
3246c861373bSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3247c861373bSJamie Gritton 
3248c861373bSJamie Gritton 	rpr = NULL;
3249c861373bSJamie Gritton 	FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3250c861373bSJamie Gritton 		if (descend) {
3251c861373bSJamie Gritton 			if (!prison_isalive(cpr)) {
3252c861373bSJamie Gritton 				descend = false;
3253c861373bSJamie Gritton 				continue;
3254c861373bSJamie Gritton 			}
3255c861373bSJamie Gritton 			prison_hold(cpr);
3256c861373bSJamie Gritton 			prison_proc_hold(cpr);
3257c861373bSJamie Gritton 			mtx_lock(&cpr->pr_mtx);
3258c861373bSJamie Gritton 			cpr->pr_state = PRISON_STATE_DYING;
3259c861373bSJamie Gritton 			cpr->pr_flags |= PR_REMOVE;
3260c861373bSJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3261c861373bSJamie Gritton 			continue;
3262c861373bSJamie Gritton 		}
3263c861373bSJamie Gritton 		if (!(cpr->pr_flags & PR_REMOVE))
3264c861373bSJamie Gritton 			continue;
3265a9f7455cSJamie Gritton 		prison_cleanup(cpr);
3266c861373bSJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3267c861373bSJamie Gritton 		cpr->pr_flags &= ~PR_REMOVE;
3268c861373bSJamie Gritton 		if (cpr->pr_flags & PR_PERSIST) {
3269c861373bSJamie Gritton 			cpr->pr_flags &= ~PR_PERSIST;
3270c861373bSJamie Gritton 			prison_proc_free_not_last(cpr);
3271c861373bSJamie Gritton 			prison_free_not_last(cpr);
3272c861373bSJamie Gritton 		}
3273c861373bSJamie Gritton 		(void)refcount_release(&cpr->pr_uref);
3274c861373bSJamie Gritton 		if (refcount_release(&cpr->pr_ref)) {
3275c861373bSJamie Gritton 			/*
3276c861373bSJamie Gritton 			 * When the last reference goes, unlink the prison
3277c861373bSJamie Gritton 			 * and set it aside for prison_deref() to handle.
3278c861373bSJamie Gritton 			 * Delay unlinking the sibling list to keep the loop
3279c861373bSJamie Gritton 			 * safe.
3280c861373bSJamie Gritton 			 */
3281c861373bSJamie Gritton 			if (rpr != NULL)
3282c861373bSJamie Gritton 				LIST_REMOVE(rpr, pr_sibling);
3283c861373bSJamie Gritton 			rpr = cpr;
3284c861373bSJamie Gritton 			rpr->pr_state = PRISON_STATE_INVALID;
3285c861373bSJamie Gritton 			TAILQ_REMOVE(&allprison, rpr, pr_list);
3286c861373bSJamie Gritton 			TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3287c861373bSJamie Gritton 			/*
3288c861373bSJamie Gritton 			 * Removing a prison frees references from its parent.
3289c861373bSJamie Gritton 			 */
3290c861373bSJamie Gritton 			ppr = rpr->pr_parent;
3291c861373bSJamie Gritton 			prison_proc_free_not_last(ppr);
3292c861373bSJamie Gritton 			prison_free_not_last(ppr);
3293c861373bSJamie Gritton 			for (; ppr != NULL; ppr = ppr->pr_parent)
3294c861373bSJamie Gritton 				ppr->pr_childcount--;
3295c861373bSJamie Gritton 		}
3296c861373bSJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3297c861373bSJamie Gritton 	}
3298c861373bSJamie Gritton 	if (rpr != NULL)
3299c861373bSJamie Gritton 		LIST_REMOVE(rpr, pr_sibling);
3300c861373bSJamie Gritton 
3301a9f7455cSJamie Gritton 	prison_cleanup(pr);
3302c861373bSJamie Gritton 	mtx_lock(&pr->pr_mtx);
3303c861373bSJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
3304c861373bSJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
3305c861373bSJamie Gritton 		prison_proc_free_not_last(pr);
3306c861373bSJamie Gritton 		prison_free_not_last(pr);
3307c861373bSJamie Gritton 	}
3308c861373bSJamie Gritton 	(void)refcount_release(&pr->pr_uref);
3309c861373bSJamie Gritton }
3310c861373bSJamie Gritton 
3311c861373bSJamie Gritton /*
3312f7496dcaSJamie Gritton  * Given the current locking state in the flags, make sure allprison_lock
3313f7496dcaSJamie Gritton  * is held exclusive, and the prison is locked.  Return flags indicating
3314f7496dcaSJamie Gritton  * the new state.
3315f7496dcaSJamie Gritton  */
3316f7496dcaSJamie Gritton static int
3317f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags)
3318f7496dcaSJamie Gritton {
3319f7496dcaSJamie Gritton 
3320f7496dcaSJamie Gritton 	if (!(flags & PD_LIST_XLOCKED)) {
3321f7496dcaSJamie Gritton 		/*
3322f7496dcaSJamie Gritton 		 * Get allprison_lock, which may be an upgrade,
3323f7496dcaSJamie Gritton 		 * and may require unlocking the prison.
3324f7496dcaSJamie Gritton 		 */
3325f7496dcaSJamie Gritton 		if (flags & PD_LOCKED) {
3326f7496dcaSJamie Gritton 			mtx_unlock(&pr->pr_mtx);
3327f7496dcaSJamie Gritton 			flags &= ~PD_LOCKED;
3328f7496dcaSJamie Gritton 		}
3329f7496dcaSJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
3330f7496dcaSJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
3331f7496dcaSJamie Gritton 				sx_sunlock(&allprison_lock);
3332f7496dcaSJamie Gritton 				sx_xlock(&allprison_lock);
3333f7496dcaSJamie Gritton 			}
3334f7496dcaSJamie Gritton 			flags &= ~PD_LIST_SLOCKED;
3335f7496dcaSJamie Gritton 		} else
3336f7496dcaSJamie Gritton 			sx_xlock(&allprison_lock);
3337f7496dcaSJamie Gritton 		flags |= PD_LIST_XLOCKED;
3338f7496dcaSJamie Gritton 	}
3339f7496dcaSJamie Gritton 	if (!(flags & PD_LOCKED)) {
3340f7496dcaSJamie Gritton 		/* Lock the prison mutex. */
3341f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
3342f7496dcaSJamie Gritton 		flags |= PD_LOCKED;
3343f7496dcaSJamie Gritton 	}
3344f7496dcaSJamie Gritton 	return flags;
3345f7496dcaSJamie Gritton }
3346f7496dcaSJamie Gritton 
3347f7496dcaSJamie Gritton /*
3348a9f7455cSJamie Gritton  * Release a prison's resources when it starts dying (when the last user
3349a9f7455cSJamie Gritton  * reference is dropped, or when it is killed).
3350a9f7455cSJamie Gritton  */
3351a9f7455cSJamie Gritton static void
3352a9f7455cSJamie Gritton prison_cleanup(struct prison *pr)
3353a9f7455cSJamie Gritton {
3354a9f7455cSJamie Gritton 	sx_assert(&allprison_lock, SA_XLOCKED);
3355a9f7455cSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
335688175af8SRick Macklem 	vfs_exjail_delete(pr);
33577060da62SJamie Gritton 	shm_remove_prison(pr);
3358a9f7455cSJamie Gritton 	(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3359a9f7455cSJamie Gritton }
3360a9f7455cSJamie Gritton 
3361a9f7455cSJamie Gritton /*
33620fe74ae6SJamie Gritton  * Set or clear a permission bit in the pr_allow field, passing restrictions
33630fe74ae6SJamie Gritton  * (cleared permission) down to child jails.
33640fe74ae6SJamie Gritton  */
33650fe74ae6SJamie Gritton void
33660fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable)
33670fe74ae6SJamie Gritton {
33680fe74ae6SJamie Gritton 	struct prison *pr;
33690fe74ae6SJamie Gritton 
33700fe74ae6SJamie Gritton 	pr = cred->cr_prison;
33710fe74ae6SJamie Gritton 	sx_slock(&allprison_lock);
33720fe74ae6SJamie Gritton 	mtx_lock(&pr->pr_mtx);
33730fe74ae6SJamie Gritton 	prison_set_allow_locked(pr, flag, enable);
33740fe74ae6SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
33750fe74ae6SJamie Gritton 	sx_sunlock(&allprison_lock);
33760fe74ae6SJamie Gritton }
33770fe74ae6SJamie Gritton 
33780fe74ae6SJamie Gritton static void
33790fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
33800fe74ae6SJamie Gritton {
33810fe74ae6SJamie Gritton 	struct prison *cpr;
33820fe74ae6SJamie Gritton 	int descend;
33830fe74ae6SJamie Gritton 
33840fe74ae6SJamie Gritton 	if (enable != 0)
33850fe74ae6SJamie Gritton 		pr->pr_allow |= flag;
33860fe74ae6SJamie Gritton 	else {
33870fe74ae6SJamie Gritton 		pr->pr_allow &= ~flag;
33880fe74ae6SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
33890fe74ae6SJamie Gritton 			cpr->pr_allow &= ~flag;
33900fe74ae6SJamie Gritton 	}
33910fe74ae6SJamie Gritton }
33920fe74ae6SJamie Gritton 
33930fe74ae6SJamie Gritton /*
3394ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3395ca04ba64SJamie Gritton  *
3396ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3397ca04ba64SJamie Gritton  * if not.
3398ca04ba64SJamie Gritton  */
3399ca04ba64SJamie Gritton int
3400ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3401ca04ba64SJamie Gritton {
34020304c731SJamie Gritton 	struct prison *pr;
3403ca04ba64SJamie Gritton 	int error;
3404ca04ba64SJamie Gritton 
3405ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3406ca04ba64SJamie Gritton 
34070304c731SJamie Gritton 	pr = cred->cr_prison;
3408499650a0SJamie Gritton #ifdef VIMAGE
34096bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3410de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
34116bb79563SJamie Gritton 		return (0);
3412499650a0SJamie Gritton #endif
34136bb79563SJamie Gritton 
3414ca04ba64SJamie Gritton 	error = 0;
3415ca04ba64SJamie Gritton 	switch (af)
3416ca04ba64SJamie Gritton 	{
3417ca04ba64SJamie Gritton #ifdef INET
3418ca04ba64SJamie Gritton 	case AF_INET:
34190304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
34200304c731SJamie Gritton 		{
34210304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
3422eb8dcdeaSGleb Smirnoff 			if ((pr->pr_flags & PR_IP4) &&
3423eb8dcdeaSGleb Smirnoff 			    pr->pr_addrs[PR_INET] == NULL)
3424ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
34250304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
34260304c731SJamie Gritton 		}
3427ca04ba64SJamie Gritton 		break;
3428ca04ba64SJamie Gritton #endif
3429ca04ba64SJamie Gritton #ifdef INET6
3430ca04ba64SJamie Gritton 	case AF_INET6:
34310304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
34320304c731SJamie Gritton 		{
34330304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
3434eb8dcdeaSGleb Smirnoff 			if ((pr->pr_flags & PR_IP6) &&
3435eb8dcdeaSGleb Smirnoff 			    pr->pr_addrs[PR_INET6] == NULL)
3436ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
34370304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
34380304c731SJamie Gritton 		}
3439ca04ba64SJamie Gritton 		break;
3440ca04ba64SJamie Gritton #endif
3441ca04ba64SJamie Gritton 	case AF_LOCAL:
3442ca04ba64SJamie Gritton 	case AF_ROUTE:
3443ca04ba64SJamie Gritton 		break;
3444ca04ba64SJamie Gritton 	default:
34450304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3446ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3447ca04ba64SJamie Gritton 	}
3448ca04ba64SJamie Gritton 	return (error);
3449ca04ba64SJamie Gritton }
3450ca04ba64SJamie Gritton 
3451ca04ba64SJamie Gritton /*
3452413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3453413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3454413628a7SBjoern A. Zeeb  *
34550304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
34560304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
34570304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3458413628a7SBjoern A. Zeeb  */
3459413628a7SBjoern A. Zeeb int
3460c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa)
346175c13541SPoul-Henning Kamp {
3462413628a7SBjoern A. Zeeb #ifdef INET
3463c83dda36SAlexander V. Chernikov 	const struct sockaddr_in *sai;
3464413628a7SBjoern A. Zeeb #endif
3465413628a7SBjoern A. Zeeb #ifdef INET6
3466c83dda36SAlexander V. Chernikov 	const struct sockaddr_in6 *sai6;
3467413628a7SBjoern A. Zeeb #endif
3468b89e82ddSJamie Gritton 	int error;
346975c13541SPoul-Henning Kamp 
3470413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3471413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3472413628a7SBjoern A. Zeeb 
3473de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3474de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3475de0bd6f7SBjoern A. Zeeb 		return (0);
3476de0bd6f7SBjoern A. Zeeb #endif
3477de0bd6f7SBjoern A. Zeeb 
3478b89e82ddSJamie Gritton 	error = 0;
3479413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3480413628a7SBjoern A. Zeeb 	{
3481413628a7SBjoern A. Zeeb #ifdef INET
3482413628a7SBjoern A. Zeeb 	case AF_INET:
3483c83dda36SAlexander V. Chernikov 		sai = (const struct sockaddr_in *)sa;
3484b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3485413628a7SBjoern A. Zeeb 		break;
3486413628a7SBjoern A. Zeeb #endif
3487413628a7SBjoern A. Zeeb #ifdef INET6
3488413628a7SBjoern A. Zeeb 	case AF_INET6:
3489c83dda36SAlexander V. Chernikov 		sai6 = (const struct sockaddr_in6 *)sa;
3490b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3491413628a7SBjoern A. Zeeb 		break;
3492413628a7SBjoern A. Zeeb #endif
3493413628a7SBjoern A. Zeeb 	default:
34940304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3495b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3496413628a7SBjoern A. Zeeb 	}
3497b89e82ddSJamie Gritton 	return (error);
349875c13541SPoul-Henning Kamp }
349991421ba2SRobert Watson 
350091421ba2SRobert Watson /*
350191421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
350291421ba2SRobert Watson  */
350391421ba2SRobert Watson int
35049ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
350591421ba2SRobert Watson {
350691421ba2SRobert Watson 
35070304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
35080304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
35090304c731SJamie Gritton }
351091421ba2SRobert Watson 
35110304c731SJamie Gritton /*
3512bba7a2e8SRick Macklem  * For mountd/nfsd to run within a prison, it must be:
3513bba7a2e8SRick Macklem  * - A vnet prison.
3514bba7a2e8SRick Macklem  * - PR_ALLOW_NFSD must be set on it.
3515bba7a2e8SRick Macklem  * - The root directory (pr_root) of the prison must be
3516bba7a2e8SRick Macklem  *   a file system mount point, so the mountd can hang
3517bba7a2e8SRick Macklem  *   export information on it.
351899187c3aSRick Macklem  * - The prison's enforce_statfs cannot be 0, so that
351999187c3aSRick Macklem  *   mountd(8) can do exports.
3520bba7a2e8SRick Macklem  */
3521bba7a2e8SRick Macklem bool
3522bba7a2e8SRick Macklem prison_check_nfsd(struct ucred *cred)
3523bba7a2e8SRick Macklem {
3524bba7a2e8SRick Macklem 
3525bba7a2e8SRick Macklem 	if (jailed_without_vnet(cred))
3526bba7a2e8SRick Macklem 		return (false);
3527bba7a2e8SRick Macklem 	if (!prison_allow(cred, PR_ALLOW_NFSD))
3528bba7a2e8SRick Macklem 		return (false);
3529bba7a2e8SRick Macklem 	if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0)
3530bba7a2e8SRick Macklem 		return (false);
353199187c3aSRick Macklem 	if (cred->cr_prison->pr_enforce_statfs == 0)
353299187c3aSRick Macklem 		return (false);
3533bba7a2e8SRick Macklem 	return (true);
3534bba7a2e8SRick Macklem }
3535bba7a2e8SRick Macklem 
3536bba7a2e8SRick Macklem /*
3537*0b0ae2e4SMina Galić  * Return true if p2 is a child of p1, otherwise false.
35380304c731SJamie Gritton  */
3539*0b0ae2e4SMina Galić bool
35400304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
35410304c731SJamie Gritton {
35420304c731SJamie Gritton 
35430304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
35440304c731SJamie Gritton 		if (pr1 == pr2)
3545*0b0ae2e4SMina Galić 			return (true);
3546*0b0ae2e4SMina Galić 	return (false);
354791421ba2SRobert Watson }
354891421ba2SRobert Watson 
354991421ba2SRobert Watson /*
3550f7496dcaSJamie Gritton  * Return true if the prison is currently alive.  A prison is alive if it
3551f7496dcaSJamie Gritton  * holds user references and it isn't being removed.
355276ad42abSJamie Gritton  */
355376ad42abSJamie Gritton bool
3554eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr)
355576ad42abSJamie Gritton {
355676ad42abSJamie Gritton 
35571158508aSJamie Gritton 	if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3558cc7b7306SJamie Gritton 		return (false);
355976ad42abSJamie Gritton 	return (true);
356076ad42abSJamie Gritton }
356176ad42abSJamie Gritton 
356276ad42abSJamie Gritton /*
356376ad42abSJamie Gritton  * Return true if the prison is currently valid.  A prison is valid if it has
356476ad42abSJamie Gritton  * been fully created, and is not being destroyed.  Note that dying prisons
3565f7496dcaSJamie Gritton  * are still considered valid.  Invalid prisons won't be found under normal
3566f7496dcaSJamie Gritton  * circumstances, as they're only put in that state by functions that have
3567f7496dcaSJamie Gritton  * an exclusive hold on allprison_lock.
356876ad42abSJamie Gritton  */
356976ad42abSJamie Gritton bool
357076ad42abSJamie Gritton prison_isvalid(struct prison *pr)
357176ad42abSJamie Gritton {
357276ad42abSJamie Gritton 
35731158508aSJamie Gritton 	if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
35741158508aSJamie Gritton 		return (false);
35756754ae25SJamie Gritton 	if (__predict_false(refcount_load(&pr->pr_ref) == 0))
357676ad42abSJamie Gritton 		return (false);
357776ad42abSJamie Gritton 	return (true);
357876ad42abSJamie Gritton }
357976ad42abSJamie Gritton 
358076ad42abSJamie Gritton /*
3581*0b0ae2e4SMina Galić  * Return true if the passed credential is in a jail and that jail does not
3582*0b0ae2e4SMina Galić  * have its own virtual network stack, otherwise false.
3583de0bd6f7SBjoern A. Zeeb  */
3584*0b0ae2e4SMina Galić bool
3585de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3586de0bd6f7SBjoern A. Zeeb {
3587de0bd6f7SBjoern A. Zeeb 
3588de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3589*0b0ae2e4SMina Galić 		return (false);
3590de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3591de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3592*0b0ae2e4SMina Galić 		return (false);
3593de0bd6f7SBjoern A. Zeeb #endif
3594de0bd6f7SBjoern A. Zeeb 
3595*0b0ae2e4SMina Galić 	return (true);
3596de0bd6f7SBjoern A. Zeeb }
3597de0bd6f7SBjoern A. Zeeb 
3598de0bd6f7SBjoern A. Zeeb /*
35997455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
36009484d0c0SRobert Drehmel  */
3601ad1ff099SRobert Drehmel void
36029ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
36039484d0c0SRobert Drehmel {
360476ca6f88SJamie Gritton 	struct prison *pr;
36059484d0c0SRobert Drehmel 
36067455b100SJamie Gritton 	/*
36077455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
36087455b100SJamie Gritton 	 * system's hostname.
36097455b100SJamie Gritton 	 */
361076ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
361176ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3612c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
361376ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
36149484d0c0SRobert Drehmel }
3615fd7a8150SMike Barcroft 
36167455b100SJamie Gritton void
36177455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
36187455b100SJamie Gritton {
36197455b100SJamie Gritton 
36207455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3621c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
36227455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
36237455b100SJamie Gritton }
36247455b100SJamie Gritton 
36257455b100SJamie Gritton void
36267455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
36277455b100SJamie Gritton {
36287455b100SJamie Gritton 
36297455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3630c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
36317455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
36327455b100SJamie Gritton }
36337455b100SJamie Gritton 
36347455b100SJamie Gritton void
36357455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
36367455b100SJamie Gritton {
36377455b100SJamie Gritton 
36387455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
36397455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
36407455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
36417455b100SJamie Gritton }
36427455b100SJamie Gritton 
36433f8bc99cSKristof Provost void
36443f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len)
36453f8bc99cSKristof Provost {
36463f8bc99cSKristof Provost 
36473f8bc99cSKristof Provost 	mtx_lock(&cred->cr_prison->pr_mtx);
36483f8bc99cSKristof Provost 	strlcpy(name, cred->cr_prison->pr_name, len);
36493f8bc99cSKristof Provost 	mtx_unlock(&cred->cr_prison->pr_mtx);
36503f8bc99cSKristof Provost }
36513f8bc99cSKristof Provost 
3652eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3653eb79e1c7SBjoern A. Zeeb /*
3654eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3655eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3656eb79e1c7SBjoern A. Zeeb  *
3657*0b0ae2e4SMina Galić  * Returns true in case the prison owns the vnet, false otherwise.
3658eb79e1c7SBjoern A. Zeeb  */
3659*0b0ae2e4SMina Galić bool
3660eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3661eb79e1c7SBjoern A. Zeeb {
3662eb79e1c7SBjoern A. Zeeb 
3663eb79e1c7SBjoern A. Zeeb 	/*
3664eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3665eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3666eb79e1c7SBjoern A. Zeeb 	 */
3667*0b0ae2e4SMina Galić 	return ((cred->cr_prison->pr_flags & PR_VNET) != 0);
3668eb79e1c7SBjoern A. Zeeb }
3669eb79e1c7SBjoern A. Zeeb #endif
3670eb79e1c7SBjoern A. Zeeb 
3671f08df373SRobert Watson /*
3672820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3673820a0de9SPawel Jakub Dawidek  * status of a mount point.
3674820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3675820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3676820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3677f08df373SRobert Watson  */
3678f08df373SRobert Watson int
3679820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3680f08df373SRobert Watson {
3681820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3682820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3683820a0de9SPawel Jakub Dawidek 	size_t len;
3684f08df373SRobert Watson 
3685820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
36860304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
36870304c731SJamie Gritton 		return (0);
3688820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3689820a0de9SPawel Jakub Dawidek 		return (0);
36900304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3691820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3692820a0de9SPawel Jakub Dawidek 	/*
3693820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3694820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3695820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3696820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3697820a0de9SPawel Jakub Dawidek 	 */
3698820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3699820a0de9SPawel Jakub Dawidek 		return (0);
3700820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3701820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3702820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3703820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3704820a0de9SPawel Jakub Dawidek 	/*
3705820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3706820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3707820a0de9SPawel Jakub Dawidek 	 */
3708820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3709820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3710f08df373SRobert Watson 	return (0);
3711f08df373SRobert Watson }
3712820a0de9SPawel Jakub Dawidek 
3713820a0de9SPawel Jakub Dawidek void
3714820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3715820a0de9SPawel Jakub Dawidek {
3716820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3717820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3718820a0de9SPawel Jakub Dawidek 	size_t len;
3719820a0de9SPawel Jakub Dawidek 
3720820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
37210304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
37220304c731SJamie Gritton 		return;
3723820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3724820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3725820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3726820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3727820a0de9SPawel Jakub Dawidek 		return;
3728820a0de9SPawel Jakub Dawidek 	}
3729820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3730820a0de9SPawel Jakub Dawidek 		/*
3731820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3732820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3733820a0de9SPawel Jakub Dawidek 		 */
3734820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3735820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3736820a0de9SPawel Jakub Dawidek 		return;
3737820a0de9SPawel Jakub Dawidek 	}
3738820a0de9SPawel Jakub Dawidek 	/*
3739820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3740820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3741820a0de9SPawel Jakub Dawidek 	 */
3742820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3743820a0de9SPawel Jakub Dawidek 		return;
3744820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3745820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3746820a0de9SPawel Jakub Dawidek 	/*
3747820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3748820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3749820a0de9SPawel Jakub Dawidek 	 */
3750820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3751820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3752820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3753820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3754820a0de9SPawel Jakub Dawidek 	} else {
3755820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3756820a0de9SPawel Jakub Dawidek 	}
3757f08df373SRobert Watson }
3758f08df373SRobert Watson 
3759800c9408SRobert Watson /*
3760800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3761800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3762800c9408SRobert Watson  */
3763800c9408SRobert Watson int
3764800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3765800c9408SRobert Watson {
37660fe74ae6SJamie Gritton 	struct prison *pr;
37670fe74ae6SJamie Gritton 	int error;
3768800c9408SRobert Watson 
37697b2ff0dcSMateusz Guzik 	/*
37707b2ff0dcSMateusz Guzik 	 * Some policies have custom handlers. This routine should not be
37717b2ff0dcSMateusz Guzik 	 * called for them. See priv_check_cred().
37727b2ff0dcSMateusz Guzik 	 */
37737b2ff0dcSMateusz Guzik 	switch (priv) {
3774a459a6cfSMateusz Guzik 	case PRIV_VFS_LOOKUP:
37757b2ff0dcSMateusz Guzik 	case PRIV_VFS_GENERATION:
37767b2ff0dcSMateusz Guzik 		KASSERT(0, ("prison_priv_check instead of a custom handler "
37777b2ff0dcSMateusz Guzik 		    "called for %d\n", priv));
37787b2ff0dcSMateusz Guzik 	}
37797b2ff0dcSMateusz Guzik 
3780800c9408SRobert Watson 	if (!jailed(cred))
3781800c9408SRobert Watson 		return (0);
3782800c9408SRobert Watson 
37836bb79563SJamie Gritton #ifdef VIMAGE
37846bb79563SJamie Gritton 	/*
37856bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
37866bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
37876bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
37886bb79563SJamie Gritton 	 */
37896bb79563SJamie Gritton 	switch (priv) {
37906bb79563SJamie Gritton 		/*
37916bb79563SJamie Gritton 		 * NFS-specific privileges.
37926bb79563SJamie Gritton 		 */
37936bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
3794bba7a2e8SRick Macklem 	case PRIV_VFS_GETFH:
3795bba7a2e8SRick Macklem 	case PRIV_VFS_MOUNT_EXPORTED:
3796bba7a2e8SRick Macklem 		if (!prison_check_nfsd(cred))
3797bba7a2e8SRick Macklem 			return (EPERM);
3798bba7a2e8SRick Macklem #ifdef notyet
37996bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
38006bb79563SJamie Gritton #endif
38016bb79563SJamie Gritton 		/*
38026bb79563SJamie Gritton 		 * Network stack privileges.
38036bb79563SJamie Gritton 		 */
38046bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
38056bb79563SJamie Gritton 	case PRIV_NET_GRE:
38066bb79563SJamie Gritton 	case PRIV_NET_BPF:
38076bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
38086bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
38096bb79563SJamie Gritton 	case PRIV_NET_TAP:
38106bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
38116bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
38126bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3813215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
38146bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
38156bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
38166bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
38176bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
3818389474c1SKonstantin Belousov 	case PRIV_NET_SETLANPCP:
38196bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
38206bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
38216bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
38226bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
38236bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
38246bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
38256bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
38266bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
38276bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
38286bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
38296bb79563SJamie Gritton 	case PRIV_NET_LAGG:
38306bb79563SJamie Gritton 	case PRIV_NET_GIF:
38316bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
383235fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
3833ab91feabSKristof Provost 	case PRIV_NET_OVPN:
383443f8c763SZhenlei Huang 	case PRIV_NET_ME:
3835744bfb21SJohn Baldwin 	case PRIV_NET_WG:
38366bb79563SJamie Gritton 
38376bb79563SJamie Gritton 		/*
38386bb79563SJamie Gritton 		 * 802.11-related privileges.
38396bb79563SJamie Gritton 		 */
3840f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_GETKEY:
3841f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_MANAGE:
38426bb79563SJamie Gritton 
38436bb79563SJamie Gritton #ifdef notyet
38446bb79563SJamie Gritton 		/*
38456bb79563SJamie Gritton 		 * ATM privileges.
38466bb79563SJamie Gritton 		 */
38476bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
38486bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
38496bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
38506bb79563SJamie Gritton 	case PRIV_NETATM_SET:
38516bb79563SJamie Gritton 
38526bb79563SJamie Gritton 		/*
38536bb79563SJamie Gritton 		 * Bluetooth privileges.
38546bb79563SJamie Gritton 		 */
38556bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
38566bb79563SJamie Gritton #endif
38576bb79563SJamie Gritton 
38586bb79563SJamie Gritton 		/*
38596bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
38606bb79563SJamie Gritton 		 */
38616bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
38626bb79563SJamie Gritton #ifdef notyet
38636bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
38646bb79563SJamie Gritton #endif
38656bb79563SJamie Gritton 
38666bb79563SJamie Gritton 		/*
38676bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
38686bb79563SJamie Gritton 		 */
38696bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
38706bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
38716bb79563SJamie Gritton 	case PRIV_NETINET_PF:
38726bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
38736bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
38746bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
38756bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
38766bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
38776bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
38786bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
38796bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
38806bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
38816bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
38826bb79563SJamie Gritton 
38836bb79563SJamie Gritton #ifdef notyet
38846bb79563SJamie Gritton 		/*
38856bb79563SJamie Gritton 		 * NCP privileges.
38866bb79563SJamie Gritton 		 */
38876bb79563SJamie Gritton 	case PRIV_NETNCP:
38886bb79563SJamie Gritton 
38896bb79563SJamie Gritton 		/*
38906bb79563SJamie Gritton 		 * SMB privileges.
38916bb79563SJamie Gritton 		 */
38926bb79563SJamie Gritton 	case PRIV_NETSMB:
38936bb79563SJamie Gritton #endif
38946bb79563SJamie Gritton 
38956bb79563SJamie Gritton 	/*
38966bb79563SJamie Gritton 	 * No default: or deny here.
38976bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
38986bb79563SJamie Gritton 	 */
38996bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
39006bb79563SJamie Gritton 			return (0);
39016bb79563SJamie Gritton 	}
39026bb79563SJamie Gritton #endif /* VIMAGE */
39036bb79563SJamie Gritton 
3904800c9408SRobert Watson 	switch (priv) {
3905800c9408SRobert Watson 		/*
3906800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3907800c9408SRobert Watson 		 */
3908800c9408SRobert Watson 	case PRIV_KTRACE:
3909800c9408SRobert Watson 
3910c3c1b5e6SRobert Watson #if 0
3911800c9408SRobert Watson 		/*
3912800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3913800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3914800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3915800c9408SRobert Watson 		 * jail.
3916800c9408SRobert Watson 		 */
3917800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3918800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3919800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3920c3c1b5e6SRobert Watson #endif
3921800c9408SRobert Watson 
3922800c9408SRobert Watson 		/*
3923800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3924800c9408SRobert Watson 		 * credentials in any way they see fit.
3925800c9408SRobert Watson 		 */
3926800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3927800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3928800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3929800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3930800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3931800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3932800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3933800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3934800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3935800c9408SRobert Watson 
3936800c9408SRobert Watson 		/*
3937800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3938800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3939800c9408SRobert Watson 		 */
3940800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3941800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3942800c9408SRobert Watson 
3943800c9408SRobert Watson 		/*
3944800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3945800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3946800c9408SRobert Watson 		 */
3947800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3948800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3949800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3950800c9408SRobert Watson 
3951800c9408SRobert Watson 		/*
3952800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3953800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3954800c9408SRobert Watson 		 */
3955800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3956800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3957800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3958800c9408SRobert Watson 
3959800c9408SRobert Watson 		/*
3960800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3961800c9408SRobert Watson 		 */
3962800c9408SRobert Watson 	case PRIV_IPC_READ:
3963800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3964800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3965800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3966800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3967800c9408SRobert Watson 
3968800c9408SRobert Watson 		/*
39690304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
39700304c731SJamie Gritton 		 */
39710304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
39720304c731SJamie Gritton 	case PRIV_JAIL_SET:
39730304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
39740304c731SJamie Gritton 
39750304c731SJamie Gritton 		/*
3976800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3977800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3978800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3979800c9408SRobert Watson 		 */
3980800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3981413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3982800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3983800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3984800c9408SRobert Watson 
3985800c9408SRobert Watson 		/*
3986800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3987800c9408SRobert Watson 		 * writable.
3988800c9408SRobert Watson 		 */
3989800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3990800c9408SRobert Watson 
3991800c9408SRobert Watson 		/*
3992800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3993e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3994e82d0201SRobert Watson 		 * configuration option.
3995800c9408SRobert Watson 		 */
399695b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
399795b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3998800c9408SRobert Watson 
3999800c9408SRobert Watson 		/*
4000800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
4001800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
4002800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
4003800c9408SRobert Watson 		 * privileges.
4004800c9408SRobert Watson 		 */
4005800c9408SRobert Watson 	case PRIV_VFS_READ:
4006800c9408SRobert Watson 	case PRIV_VFS_WRITE:
4007800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
4008800c9408SRobert Watson 	case PRIV_VFS_EXEC:
4009800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
4010800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
4011800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
4012800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
4013bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
4014800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
4015800c9408SRobert Watson 	case PRIV_VFS_LINK:
4016800c9408SRobert Watson 	case PRIV_VFS_SETGID:
4017e41966dcSRobert Watson 	case PRIV_VFS_STAT:
4018800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
4019bb56d716SJamie Gritton 
4020bb56d716SJamie Gritton 		/*
4021bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
402270de1003SGordon Bergling 		 * able to read kernel/physical memory (provided /dev/[k]mem
4023bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
4024bb56d716SJamie Gritton 		 */
4025bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
4026800c9408SRobert Watson 		return (0);
4027800c9408SRobert Watson 
4028800c9408SRobert Watson 		/*
4029800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
4030800c9408SRobert Watson 		 * setting system flags.
4031800c9408SRobert Watson 		 */
4032800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
40330304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
4034800c9408SRobert Watson 			return (0);
4035800c9408SRobert Watson 		else
4036800c9408SRobert Watson 			return (EPERM);
4037800c9408SRobert Watson 
4038800c9408SRobert Watson 		/*
4039f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
4040f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
4041f3a8d2f9SPawel Jakub Dawidek 		 */
4042f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
4043f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
4044f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
404524b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
40460fe74ae6SJamie Gritton 		pr = cred->cr_prison;
40470fe74ae6SJamie Gritton 		prison_lock(pr);
40480fe74ae6SJamie Gritton 		if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
40490fe74ae6SJamie Gritton 			error = 0;
4050f3a8d2f9SPawel Jakub Dawidek 		else
40510fe74ae6SJamie Gritton 			error = EPERM;
40520fe74ae6SJamie Gritton 		prison_unlock(pr);
40530fe74ae6SJamie Gritton 		return (error);
4054f3a8d2f9SPawel Jakub Dawidek 
4055f3a8d2f9SPawel Jakub Dawidek 		/*
405663619b6dSKyle Evans 		 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
405763619b6dSKyle Evans 		 * policy.  priv_check_cred will not specifically allow it, and
405863619b6dSKyle Evans 		 * we may want a MAC policy to allow it.
405963619b6dSKyle Evans 		 */
406063619b6dSKyle Evans 	case PRIV_VFS_READ_DIR:
406163619b6dSKyle Evans 		return (0);
406263619b6dSKyle Evans 
406363619b6dSKyle Evans 		/*
4064ccd6ac9fSAntoine Brodin 		 * Conditionnaly allow locking (unlocking) physical pages
4065ccd6ac9fSAntoine Brodin 		 * in memory.
4066ccd6ac9fSAntoine Brodin 		 */
4067ccd6ac9fSAntoine Brodin 	case PRIV_VM_MLOCK:
4068ccd6ac9fSAntoine Brodin 	case PRIV_VM_MUNLOCK:
4069ccd6ac9fSAntoine Brodin 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
4070ccd6ac9fSAntoine Brodin 			return (0);
4071ccd6ac9fSAntoine Brodin 		else
4072ccd6ac9fSAntoine Brodin 			return (EPERM);
4073ccd6ac9fSAntoine Brodin 
4074ccd6ac9fSAntoine Brodin 		/*
4075e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
4076800c9408SRobert Watson 		 */
4077800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
4078e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
4079e28f9b7dSAllan Jude 			return (0);
4080e28f9b7dSAllan Jude 		else
4081e28f9b7dSAllan Jude 			return (EPERM);
4082e28f9b7dSAllan Jude 
4083e28f9b7dSAllan Jude 		/*
4084e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
4085e28f9b7dSAllan Jude 		 */
40864b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
4087800c9408SRobert Watson 		return (0);
4088800c9408SRobert Watson 
4089800c9408SRobert Watson 		/*
4090e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
409179ba3952SBjoern A. Zeeb 		 */
409279ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
409379ba3952SBjoern A. Zeeb 		return (0);
409479ba3952SBjoern A. Zeeb 
409579ba3952SBjoern A. Zeeb 		/*
4096800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
4097800c9408SRobert Watson 		 */
4098800c9408SRobert Watson 	case PRIV_NETINET_RAW:
40990304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4100800c9408SRobert Watson 			return (0);
4101800c9408SRobert Watson 		else
4102800c9408SRobert Watson 			return (EPERM);
4103800c9408SRobert Watson 
4104800c9408SRobert Watson 		/*
4105800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
4106800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
4107800c9408SRobert Watson 		 * jail.
4108800c9408SRobert Watson 		 */
4109800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
4110800c9408SRobert Watson 		return (0);
4111800c9408SRobert Watson 
41122bfc50bcSEdward Tomasz Napierala 		/*
41132bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
41142bfc50bcSEdward Tomasz Napierala 		 */
41152bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
41162bfc50bcSEdward Tomasz Napierala 		return (0);
41172bfc50bcSEdward Tomasz Napierala 
4118b19d66fdSJamie Gritton 		/*
41194520f617SJamie Gritton 		 * Do not allow a process inside a jail to read the kernel
4120b19d66fdSJamie Gritton 		 * message buffer unless explicitly permitted.
4121b19d66fdSJamie Gritton 		 */
4122b19d66fdSJamie Gritton 	case PRIV_MSGBUF:
4123b19d66fdSJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
4124b19d66fdSJamie Gritton 			return (0);
4125b19d66fdSJamie Gritton 		return (EPERM);
4126b19d66fdSJamie Gritton 
4127800c9408SRobert Watson 	default:
4128800c9408SRobert Watson 		/*
4129800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
4130800c9408SRobert Watson 		 * includes almost all network privileges, many system
4131800c9408SRobert Watson 		 * configuration privileges.
4132800c9408SRobert Watson 		 */
4133800c9408SRobert Watson 		return (EPERM);
4134800c9408SRobert Watson 	}
4135800c9408SRobert Watson }
4136800c9408SRobert Watson 
41370304c731SJamie Gritton /*
41380304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
41390304c731SJamie Gritton  * if it does not directly follow.
41400304c731SJamie Gritton  */
41410304c731SJamie Gritton 
41420304c731SJamie Gritton char *
41430304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
41440304c731SJamie Gritton {
41450304c731SJamie Gritton 	char *name;
41460304c731SJamie Gritton 
41470304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
41480304c731SJamie Gritton 	if (pr1 == pr2)
41490304c731SJamie Gritton 		return "0";
41500304c731SJamie Gritton 	name = pr2->pr_name;
41510304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
41520304c731SJamie Gritton 		/*
41530304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
41540304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
41550304c731SJamie Gritton 		 * can be counted on - and counted.
41560304c731SJamie Gritton 		 */
41570304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
41580304c731SJamie Gritton 			name = strchr(name, '.') + 1;
41590304c731SJamie Gritton 	}
41600304c731SJamie Gritton 	return (name);
41610304c731SJamie Gritton }
41620304c731SJamie Gritton 
41630304c731SJamie Gritton /*
41640304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
41650304c731SJamie Gritton  * if it does not directly follow.
41660304c731SJamie Gritton  */
41670304c731SJamie Gritton static char *
41680304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
41690304c731SJamie Gritton {
41700304c731SJamie Gritton 	char *path1, *path2;
41710304c731SJamie Gritton 	int len1;
41720304c731SJamie Gritton 
41730304c731SJamie Gritton 	path1 = pr1->pr_path;
41740304c731SJamie Gritton 	path2 = pr2->pr_path;
41750304c731SJamie Gritton 	if (!strcmp(path1, "/"))
41760304c731SJamie Gritton 		return (path2);
41770304c731SJamie Gritton 	len1 = strlen(path1);
41780304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
41790304c731SJamie Gritton 		return (path2);
41800304c731SJamie Gritton 	if (path2[len1] == '\0')
41810304c731SJamie Gritton 		return "/";
41820304c731SJamie Gritton 	if (path2[len1] == '/')
41830304c731SJamie Gritton 		return (path2 + len1);
41840304c731SJamie Gritton 	return (path2);
41850304c731SJamie Gritton }
41860304c731SJamie Gritton 
41870304c731SJamie Gritton /*
41880304c731SJamie Gritton  * Jail-related sysctls.
41890304c731SJamie Gritton  */
41907029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
41910304c731SJamie Gritton     "Jails");
41920304c731SJamie Gritton 
4193eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
4194eb8dcdeaSGleb Smirnoff /*
4195eb8dcdeaSGleb Smirnoff  * Copy address array to memory that would be then SYSCTL_OUT-ed.
4196eb8dcdeaSGleb Smirnoff  * sysctl_jail_list() helper.
4197eb8dcdeaSGleb Smirnoff  */
4198eb8dcdeaSGleb Smirnoff static void
4199eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4200eb8dcdeaSGleb Smirnoff {
42012c33b456SZhenlei Huang 	const struct prison_ip *pip;
4202eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
4203eb8dcdeaSGleb Smirnoff 
4204eb8dcdeaSGleb Smirnoff  again:
4205eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
42062c33b456SZhenlei Huang 	if ((pip = pr->pr_addrs[af]) != NULL) {
42072c33b456SZhenlei Huang 		if (*len < pip->ips) {
42082c33b456SZhenlei Huang 			*len = pip->ips;
4209eb8dcdeaSGleb Smirnoff 			mtx_unlock(&pr->pr_mtx);
4210eb8dcdeaSGleb Smirnoff 			*out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4211eb8dcdeaSGleb Smirnoff 			mtx_lock(&pr->pr_mtx);
4212eb8dcdeaSGleb Smirnoff 			goto again;
4213eb8dcdeaSGleb Smirnoff 		}
42142c33b456SZhenlei Huang 		bcopy(pip->pr_ip, *out, pip->ips * size);
4215eb8dcdeaSGleb Smirnoff 	}
4216eb8dcdeaSGleb Smirnoff }
4217eb8dcdeaSGleb Smirnoff #endif
4218eb8dcdeaSGleb Smirnoff 
4219fd7a8150SMike Barcroft static int
4220fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4221fd7a8150SMike Barcroft {
4222b38ff370SJamie Gritton 	struct xprison *xp;
42230304c731SJamie Gritton 	struct prison *pr, *cpr;
4224b38ff370SJamie Gritton #ifdef INET
4225b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
4226b38ff370SJamie Gritton 	int ip4s = 0;
4227b38ff370SJamie Gritton #endif
4228b38ff370SJamie Gritton #ifdef INET6
42293beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
4230b38ff370SJamie Gritton 	int ip6s = 0;
4231b38ff370SJamie Gritton #endif
42320304c731SJamie Gritton 	int descend, error;
4233fd7a8150SMike Barcroft 
4234b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
42350304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
4236b38ff370SJamie Gritton 	error = 0;
4237dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
42380304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
42390304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
4240413628a7SBjoern A. Zeeb #ifdef INET
4241eb8dcdeaSGleb Smirnoff 		prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4242413628a7SBjoern A. Zeeb #endif
4243413628a7SBjoern A. Zeeb #ifdef INET6
4244eb8dcdeaSGleb Smirnoff 		prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4245b38ff370SJamie Gritton #endif
4246b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
4247fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
42480304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
42491158508aSJamie Gritton 		xp->pr_state = cpr->pr_state;
42500304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4251c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
42520304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4253413628a7SBjoern A. Zeeb #ifdef INET
4254eb8dcdeaSGleb Smirnoff 		xp->pr_ip4s = ip4s;
4255413628a7SBjoern A. Zeeb #endif
4256413628a7SBjoern A. Zeeb #ifdef INET6
4257eb8dcdeaSGleb Smirnoff 		xp->pr_ip6s = ip6s;
4258413628a7SBjoern A. Zeeb #endif
42590304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
4260b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4261b38ff370SJamie Gritton 		if (error)
4262b38ff370SJamie Gritton 			break;
4263413628a7SBjoern A. Zeeb #ifdef INET
4264b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
4265b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
4266b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
4267b38ff370SJamie Gritton 			if (error)
4268b38ff370SJamie Gritton 				break;
4269413628a7SBjoern A. Zeeb 		}
4270413628a7SBjoern A. Zeeb #endif
4271413628a7SBjoern A. Zeeb #ifdef INET6
4272b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
4273b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
4274b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
4275b38ff370SJamie Gritton 			if (error)
4276b38ff370SJamie Gritton 				break;
4277413628a7SBjoern A. Zeeb 		}
4278413628a7SBjoern A. Zeeb #endif
4279fd7a8150SMike Barcroft 	}
4280dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
4281b38ff370SJamie Gritton 	free(xp, M_TEMP);
4282b38ff370SJamie Gritton #ifdef INET
4283b38ff370SJamie Gritton 	free(ip4, M_TEMP);
4284b38ff370SJamie Gritton #endif
4285b38ff370SJamie Gritton #ifdef INET6
4286b38ff370SJamie Gritton 	free(ip6, M_TEMP);
4287b38ff370SJamie Gritton #endif
4288fd7a8150SMike Barcroft 	return (error);
4289fd7a8150SMike Barcroft }
4290fd7a8150SMike Barcroft 
4291f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
4292f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4293f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
4294461167c2SPawel Jakub Dawidek 
4295461167c2SPawel Jakub Dawidek static int
4296461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4297461167c2SPawel Jakub Dawidek {
4298461167c2SPawel Jakub Dawidek 	int error, injail;
4299461167c2SPawel Jakub Dawidek 
4300461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
4301461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4302461167c2SPawel Jakub Dawidek 
4303461167c2SPawel Jakub Dawidek 	return (error);
4304461167c2SPawel Jakub Dawidek }
43050304c731SJamie Gritton 
4306f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4307f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4308f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
4309413628a7SBjoern A. Zeeb 
4310761d2bb5SJamie Gritton static int
4311761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4312761d2bb5SJamie Gritton {
4313761d2bb5SJamie Gritton 	int error, havevnet;
4314761d2bb5SJamie Gritton #ifdef VIMAGE
4315761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
4316761d2bb5SJamie Gritton 
4317761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
4318761d2bb5SJamie Gritton #else
4319761d2bb5SJamie Gritton 	havevnet = 0;
4320761d2bb5SJamie Gritton #endif
4321761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4322761d2bb5SJamie Gritton 
4323761d2bb5SJamie Gritton 	return (error);
4324761d2bb5SJamie Gritton }
4325761d2bb5SJamie Gritton 
4326761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4327761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4328bb8f1623SBjoern A. Zeeb     sysctl_jail_vnet, "I", "Jail owns vnet?");
4329761d2bb5SJamie Gritton 
43300304c731SJamie Gritton #if defined(INET) || defined(INET6)
4331e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
43320304c731SJamie Gritton     &jail_max_af_ips, 0,
4333c542c43eSJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
43340304c731SJamie Gritton #endif
43350304c731SJamie Gritton 
43360304c731SJamie Gritton /*
4337c542c43eSJamie Gritton  * Default parameters for jail(2) compatibility.  For historical reasons,
4338c542c43eSJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
4339c542c43eSJamie Gritton  * just see their own parameters, and can't change them.
43400304c731SJamie Gritton  */
43410304c731SJamie Gritton static int
43420304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
43430304c731SJamie Gritton {
43440fe74ae6SJamie Gritton 	int error, i;
43450304c731SJamie Gritton 
43460304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
43470fe74ae6SJamie Gritton 	if (req->td->td_ucred->cr_prison == &prison0) {
43480fe74ae6SJamie Gritton 		mtx_lock(&prison0.pr_mtx);
43490fe74ae6SJamie Gritton 		i = (jail_default_allow & arg2) != 0;
43500fe74ae6SJamie Gritton 		mtx_unlock(&prison0.pr_mtx);
43510fe74ae6SJamie Gritton 	} else
43520fe74ae6SJamie Gritton 		i = prison_allow(req->td->td_ucred, arg2);
43530fe74ae6SJamie Gritton 
43540304c731SJamie Gritton 	if (arg1 != NULL)
43550304c731SJamie Gritton 		i = !i;
43560304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
4357c542c43eSJamie Gritton 	if (error || !req->newptr)
43580304c731SJamie Gritton 		return (error);
43590304c731SJamie Gritton 	i = i ? arg2 : 0;
43600304c731SJamie Gritton 	if (arg1 != NULL)
43610304c731SJamie Gritton 		i ^= arg2;
43620304c731SJamie Gritton 	/*
43630304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
43640304c731SJamie Gritton 	 * for writing.
43650304c731SJamie Gritton 	 */
43660304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
43670304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
43680304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
43690304c731SJamie Gritton 	return (0);
43700304c731SJamie Gritton }
43710304c731SJamie Gritton 
43720304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4373c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43740304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4375c542c43eSJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
43760304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4377c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43780304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4379c542c43eSJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
43800304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4381c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43820304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4383c542c43eSJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
43840304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4385c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43860304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4387c542c43eSJamie Gritton     "Prison root can create raw sockets (deprecated)");
43880304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4389c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43900304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4391c542c43eSJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
43920304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4393c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43940304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4395c542c43eSJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
43960304c731SJamie Gritton 
43970304c731SJamie Gritton static int
43980304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
43990304c731SJamie Gritton {
44000304c731SJamie Gritton 	struct prison *pr;
44010304c731SJamie Gritton 	int level, error;
44020304c731SJamie Gritton 
44030304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
44040304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
44050304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
4406c542c43eSJamie Gritton 	if (error || !req->newptr)
44070304c731SJamie Gritton 		return (error);
44080304c731SJamie Gritton 	*(int *)arg1 = level;
44090304c731SJamie Gritton 	return (0);
44100304c731SJamie Gritton }
44110304c731SJamie Gritton 
44120304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4413c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4414c542c43eSJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
44150304c731SJamie Gritton     sysctl_jail_default_level, "I",
4416c542c43eSJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
4417c542c43eSJamie Gritton 
44180cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4419c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4420c542c43eSJamie Gritton     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
44210cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
4422c542c43eSJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
44230cc207a6SMartin Matuska 
44240304c731SJamie Gritton /*
44250304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
44260304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
44270304c731SJamie Gritton  * to make themselves and their types known.
44280304c731SJamie Gritton  */
44297029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
44300304c731SJamie Gritton     "Jail parameters");
44310304c731SJamie Gritton 
44320304c731SJamie Gritton int
44330304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
44340304c731SJamie Gritton {
44350304c731SJamie Gritton 	int i;
44360304c731SJamie Gritton 	long l;
44370304c731SJamie Gritton 	size_t s;
44380304c731SJamie Gritton 	char numbuf[12];
44390304c731SJamie Gritton 
44400304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
44410304c731SJamie Gritton 	{
44420304c731SJamie Gritton 	case CTLTYPE_LONG:
44430304c731SJamie Gritton 	case CTLTYPE_ULONG:
44440304c731SJamie Gritton 		l = 0;
44450304c731SJamie Gritton #ifdef SCTL_MASK32
44460304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
44470304c731SJamie Gritton #endif
44480304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
44490304c731SJamie Gritton 	case CTLTYPE_INT:
44500304c731SJamie Gritton 	case CTLTYPE_UINT:
44510304c731SJamie Gritton 		i = 0;
44520304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
44530304c731SJamie Gritton 	case CTLTYPE_STRING:
4454e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
44550304c731SJamie Gritton 		return
44560304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
44570304c731SJamie Gritton 	case CTLTYPE_STRUCT:
44580304c731SJamie Gritton 		s = (size_t)arg2;
44590304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
44600304c731SJamie Gritton 	}
44610304c731SJamie Gritton 	return (0);
44620304c731SJamie Gritton }
44630304c731SJamie Gritton 
4464b96bd95bSIan Lepore /*
4465b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4466b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
4467b96bd95bSIan Lepore  */
44680304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
44690304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
44700304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
44710304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
44720304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
44730304c731SJamie Gritton     "I", "Jail secure level");
4474b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4475b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
4476b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4477b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
44780304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
44790304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
44800cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
44810cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
44820304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
44830304c731SJamie Gritton     "B", "Jail persistence");
4484679e1390SJamie Gritton #ifdef VIMAGE
4485679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
44867cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4487679e1390SJamie Gritton #endif
44880304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
44890304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
44900304c731SJamie Gritton 
4491b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4492b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4493b97457e2SJamie Gritton     "I", "Current number of child jails");
4494b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4495b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4496b97457e2SJamie Gritton 
44977cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
44980304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
44990304c731SJamie Gritton     "Jail hostname");
450076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
450176ca6f88SJamie Gritton     "Jail NIS domainname");
450276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
450376ca6f88SJamie Gritton     "Jail host UUID");
450476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
450576ca6f88SJamie Gritton     "LU", "Jail host ID");
45060304c731SJamie Gritton 
45070304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
45080304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
45090304c731SJamie Gritton 
45100304c731SJamie Gritton #ifdef INET
45112b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
45122b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
45130304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
45140304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4515592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4516592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4517592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
45180304c731SJamie Gritton #endif
45190304c731SJamie Gritton #ifdef INET6
45202b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
45212b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
45220304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
45230304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4524592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4525592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4526592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
45270304c731SJamie Gritton #endif
45280304c731SJamie Gritton 
45290304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
45300304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
45310304c731SJamie Gritton     "B", "Jail may set hostname");
45320304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
45330304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
45340304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
45350304c731SJamie Gritton     "B", "Jail may create raw sockets");
45360304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
45370304c731SJamie Gritton     "B", "Jail may alter system file flags");
45380304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
45390304c731SJamie Gritton     "B", "Jail may set file quotas");
45400304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
45410304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4542ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4543ccd6ac9fSAntoine Brodin     "B", "Jail may lock (unlock) physical pages in memory");
4544e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4545e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
4546b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4547b19d66fdSJamie Gritton     "B", "Jail may read the kernel message buffer");
4548b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4549b3079544SJamie Gritton     "B", "Unprivileged processes may use process debugging facilities");
455005e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
455105e1e482SMariusz Zaborski     "B", "Processes in jail with uid 0 have privilege");
4552cbbb2203SRick Macklem #ifdef VIMAGE
4553bba7a2e8SRick Macklem SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW,
4554bba7a2e8SRick Macklem     "B", "Mountd/nfsd may run in the jail");
4555bba7a2e8SRick Macklem #endif
45560304c731SJamie Gritton 
4557bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4558bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4559bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
45600e5c6bd4SJamie Gritton 
45610e5c6bd4SJamie Gritton /*
45620a172404SJamie Gritton  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
45630a172404SJamie Gritton  * its associated bit in the pr_allow bitmask, or zero if the parameter was
45640a172404SJamie Gritton  * not created.
45650e5c6bd4SJamie Gritton  */
45660a172404SJamie Gritton unsigned
45670a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
45680a172404SJamie Gritton     const char *descr)
45690e5c6bd4SJamie Gritton {
45700e5c6bd4SJamie Gritton 	struct bool_flags *bf;
45710a172404SJamie Gritton 	struct sysctl_oid *parent;
45720a172404SJamie Gritton 	char *allow_name, *allow_noname, *allowed;
4573c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4574c542c43eSJamie Gritton 	char *descr_deprecated;
4575c542c43eSJamie Gritton #endif
45765d58f959SJamie Gritton 	u_int allow_flag;
45770e5c6bd4SJamie Gritton 
45780a172404SJamie Gritton 	if (prefix
45790a172404SJamie Gritton 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
45800a172404SJamie Gritton 		< 0 ||
45810a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
45820a172404SJamie Gritton 		< 0
45830a172404SJamie Gritton 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
45840a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
45850e5c6bd4SJamie Gritton 		free(allow_name, M_PRISON);
45860a172404SJamie Gritton 		return 0;
45870e5c6bd4SJamie Gritton 	}
45880e5c6bd4SJamie Gritton 
45890e5c6bd4SJamie Gritton 	/*
45900a172404SJamie Gritton 	 * See if this parameter has already beed added, i.e. a module was
45910a172404SJamie Gritton 	 * previously loaded/unloaded.
45920e5c6bd4SJamie Gritton 	 */
45930e5c6bd4SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
45940e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow;
45955d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
45965d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
45970e5c6bd4SJamie Gritton 	     bf++) {
45980e5c6bd4SJamie Gritton 		if (strcmp(bf->name, allow_name) == 0) {
45990a172404SJamie Gritton 			allow_flag = bf->flag;
46000e5c6bd4SJamie Gritton 			goto no_add;
46010e5c6bd4SJamie Gritton 		}
46020e5c6bd4SJamie Gritton 	}
46030e5c6bd4SJamie Gritton 
46040e5c6bd4SJamie Gritton 	/*
46055d58f959SJamie Gritton 	 * Find a free bit in pr_allow_all, failing if there are none
46060e5c6bd4SJamie Gritton 	 * (which shouldn't happen as long as we keep track of how many
46070a172404SJamie Gritton 	 * potential dynamic flags exist).
46080e5c6bd4SJamie Gritton 	 */
46090e5c6bd4SJamie Gritton 	for (allow_flag = 1;; allow_flag <<= 1) {
46100e5c6bd4SJamie Gritton 		if (allow_flag == 0)
46110e5c6bd4SJamie Gritton 			goto no_add;
46125d58f959SJamie Gritton 		if ((pr_allow_all & allow_flag) == 0)
46130e5c6bd4SJamie Gritton 			break;
46140e5c6bd4SJamie Gritton 	}
46150e5c6bd4SJamie Gritton 
46165d58f959SJamie Gritton 	/* Note the parameter in the next open slot in pr_flag_allow. */
46175d58f959SJamie Gritton 	for (bf = pr_flag_allow; ; bf++) {
46180e5c6bd4SJamie Gritton 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
46190e5c6bd4SJamie Gritton 			/* This should never happen, but is not fatal. */
46200a172404SJamie Gritton 			allow_flag = 0;
46210e5c6bd4SJamie Gritton 			goto no_add;
46220e5c6bd4SJamie Gritton 		}
46235d58f959SJamie Gritton 		if (atomic_load_int(&bf->flag) == 0)
46245d58f959SJamie Gritton 			break;
46255d58f959SJamie Gritton 	}
46260e5c6bd4SJamie Gritton 	bf->name = allow_name;
46270e5c6bd4SJamie Gritton 	bf->noname = allow_noname;
46285d58f959SJamie Gritton 	pr_allow_all |= allow_flag;
46295d58f959SJamie Gritton 	/*
46305d58f959SJamie Gritton 	 * prison0 always has permission for the new parameter.
46315d58f959SJamie Gritton 	 * Other jails must have it granted to them.
46325d58f959SJamie Gritton 	 */
46335d58f959SJamie Gritton 	prison0.pr_allow |= allow_flag;
46345d58f959SJamie Gritton 	/* The flag indicates a valid entry, so make sure it is set last. */
46355d58f959SJamie Gritton 	atomic_store_rel_int(&bf->flag, allow_flag);
46360e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
46370e5c6bd4SJamie Gritton 
4638c542c43eSJamie Gritton 	/*
46394771011bSGordon Bergling 	 * Create sysctls for the parameter, and the back-compat global
4640c542c43eSJamie Gritton 	 * permission.
4641c542c43eSJamie Gritton 	 */
46420a172404SJamie Gritton 	parent = prefix
46430a172404SJamie Gritton 	    ? SYSCTL_ADD_NODE(NULL,
46440a172404SJamie Gritton 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
46457029da5cSPawel Biernacki 		  OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
46460a172404SJamie Gritton 	    : &sysctl___security_jail_param_allow;
46470a172404SJamie Gritton 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
46480a172404SJamie Gritton 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
46490e5c6bd4SJamie Gritton 	    NULL, 0, sysctl_jail_param, "B", descr);
46500a172404SJamie Gritton 	if ((prefix
46510a172404SJamie Gritton 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
46520a172404SJamie Gritton 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4653c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4654c542c43eSJamie Gritton 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4655c542c43eSJamie Gritton 		    descr);
4656c542c43eSJamie Gritton #endif
46570e5c6bd4SJamie Gritton 		(void)SYSCTL_ADD_PROC(NULL,
46580a172404SJamie Gritton 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4659c542c43eSJamie Gritton 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4660c542c43eSJamie Gritton 		    sysctl_jail_default_allow, "I", descr_deprecated);
4661c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4662c542c43eSJamie Gritton 		free(descr_deprecated, M_TEMP);
4663c542c43eSJamie Gritton #endif
46640a172404SJamie Gritton 		free(allowed, M_TEMP);
46650e5c6bd4SJamie Gritton 	}
46660a172404SJamie Gritton 	return allow_flag;
46670e5c6bd4SJamie Gritton 
46680e5c6bd4SJamie Gritton  no_add:
46690e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
46700e5c6bd4SJamie Gritton 	free(allow_name, M_PRISON);
46710e5c6bd4SJamie Gritton 	free(allow_noname, M_PRISON);
46720a172404SJamie Gritton 	return allow_flag;
46730a172404SJamie Gritton }
46740a172404SJamie Gritton 
46750a172404SJamie Gritton /*
46760a172404SJamie Gritton  * The VFS system will register jail-aware filesystems here.  They each get
46770a172404SJamie Gritton  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
46780a172404SJamie Gritton  * attempts to mount.
46790a172404SJamie Gritton  */
46800a172404SJamie Gritton void
46810a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp)
46820a172404SJamie Gritton {
46830a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR
46840a172404SJamie Gritton 
46850a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
46860a172404SJamie Gritton 	    NULL, NULL);
46870a172404SJamie Gritton #else
46880a172404SJamie Gritton 	char *descr;
46890a172404SJamie Gritton 
46900a172404SJamie Gritton 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
46910a172404SJamie Gritton 	    vfsp->vfc_name);
46920a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
46930a172404SJamie Gritton 	    NULL, descr);
46940a172404SJamie Gritton 	free(descr, M_TEMP);
46950a172404SJamie Gritton #endif
46960e5c6bd4SJamie Gritton }
4697bf3db8aaSMartin Matuska 
46984b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4699097055e2SEdward Tomasz Napierala void
4700097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
470115db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
470215db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
4703097055e2SEdward Tomasz Napierala {
4704a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4705097055e2SEdward Tomasz Napierala 
47064b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
47074b5c9cf6SEdward Tomasz Napierala 
4708097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
470915db3c07SEdward Tomasz Napierala 	if (pre != NULL)
471015db3c07SEdward Tomasz Napierala 		(pre)();
4711a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4712a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
471315db3c07SEdward Tomasz Napierala 	if (post != NULL)
471415db3c07SEdward Tomasz Napierala 		(post)();
4715097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4716097055e2SEdward Tomasz Napierala }
47170304c731SJamie Gritton 
4718a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4719a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4720a7ad07bfSEdward Tomasz Napierala {
4721a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4722a7ad07bfSEdward Tomasz Napierala 
47234b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4724a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4725a7ad07bfSEdward Tomasz Napierala 
4726a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4727a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4728a7ad07bfSEdward Tomasz Napierala 
4729a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4730a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4731a7ad07bfSEdward Tomasz Napierala 			continue;
4732a7ad07bfSEdward Tomasz Napierala 
4733a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4734a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4735a7ad07bfSEdward Tomasz Napierala 		return (prr);
4736a7ad07bfSEdward Tomasz Napierala 	}
4737a7ad07bfSEdward Tomasz Napierala 
4738a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4739a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4740a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4741a7ad07bfSEdward Tomasz Napierala 
4742a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4743a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4744a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4745a7ad07bfSEdward Tomasz Napierala 
4746a7ad07bfSEdward Tomasz Napierala 	return (prr);
4747a7ad07bfSEdward Tomasz Napierala }
4748a7ad07bfSEdward Tomasz Napierala 
4749a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4750a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4751a7ad07bfSEdward Tomasz Napierala {
4752a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4753a7ad07bfSEdward Tomasz Napierala 
47544b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
47554b5c9cf6SEdward Tomasz Napierala 
4756a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4757a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4758a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4759a7ad07bfSEdward Tomasz Napierala 	return (prr);
4760a7ad07bfSEdward Tomasz Napierala }
4761a7ad07bfSEdward Tomasz Napierala 
4762a7ad07bfSEdward Tomasz Napierala void
4763a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4764a7ad07bfSEdward Tomasz Napierala {
4765a7ad07bfSEdward Tomasz Napierala 
47664b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
47674b5c9cf6SEdward Tomasz Napierala 
4768a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4769a7ad07bfSEdward Tomasz Napierala }
4770a7ad07bfSEdward Tomasz Napierala 
4771c34bbd2aSEdward Tomasz Napierala static void
4772c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4773c34bbd2aSEdward Tomasz Napierala {
4774c34bbd2aSEdward Tomasz Napierala 
47754b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4776c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4777c34bbd2aSEdward Tomasz Napierala 
4778c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4779c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4780c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4781c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4782c34bbd2aSEdward Tomasz Napierala 	}
4783c34bbd2aSEdward Tomasz Napierala }
4784c34bbd2aSEdward Tomasz Napierala 
4785a7ad07bfSEdward Tomasz Napierala void
4786a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4787a7ad07bfSEdward Tomasz Napierala {
4788a7ad07bfSEdward Tomasz Napierala 
47894b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4790c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4791c34bbd2aSEdward Tomasz Napierala 
47926ff4688bSMateusz Guzik 	if (refcount_release_if_not_last(&prr->prr_refcount))
4793a7ad07bfSEdward Tomasz Napierala 		return;
4794a7ad07bfSEdward Tomasz Napierala 
4795a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4796c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4797a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4798a7ad07bfSEdward Tomasz Napierala }
4799a7ad07bfSEdward Tomasz Napierala 
4800a7ad07bfSEdward Tomasz Napierala static void
4801a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4802a7ad07bfSEdward Tomasz Napierala {
4803a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4804a7ad07bfSEdward Tomasz Napierala 
48054b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4806c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4807c34bbd2aSEdward Tomasz Napierala 
4808a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4809a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4810a7ad07bfSEdward Tomasz Napierala 
4811a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4812a7ad07bfSEdward Tomasz Napierala }
4813a7ad07bfSEdward Tomasz Napierala 
4814c34bbd2aSEdward Tomasz Napierala /*
4815c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4816c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4817c34bbd2aSEdward Tomasz Napierala  */
4818c34bbd2aSEdward Tomasz Napierala static void
4819c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4820c34bbd2aSEdward Tomasz Napierala {
4821dbadb015SKonstantin Belousov #ifdef RCTL
4822c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4823c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4824dbadb015SKonstantin Belousov #endif
4825c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4826c34bbd2aSEdward Tomasz Napierala 
48274b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
48284b5c9cf6SEdward Tomasz Napierala 
4829c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4830c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4831c34bbd2aSEdward Tomasz Napierala 
4832e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4833e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4834e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4835c34bbd2aSEdward Tomasz Napierala 		return;
4836e30345e7SEdward Tomasz Napierala 	}
4837c34bbd2aSEdward Tomasz Napierala 
4838c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4839c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4840c34bbd2aSEdward Tomasz Napierala 
4841c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4842c34bbd2aSEdward Tomasz Napierala 
4843c34bbd2aSEdward Tomasz Napierala 	/*
4844c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4845c34bbd2aSEdward Tomasz Napierala 	 */
4846c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4847c34bbd2aSEdward Tomasz Napierala 
4848f87beb93SAndriy Gapon #ifdef RCTL
4849c34bbd2aSEdward Tomasz Napierala 	/*
4850c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4851c34bbd2aSEdward Tomasz Napierala 	 */
4852c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4853c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4854c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4855c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4856f87beb93SAndriy Gapon 		rctl_proc_ucred_changed(p, cred);
4857c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4858c34bbd2aSEdward Tomasz Napierala 	}
4859f87beb93SAndriy Gapon #endif
4860c34bbd2aSEdward Tomasz Napierala 
4861c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4862c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4863c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4864c34bbd2aSEdward Tomasz Napierala }
4865c34bbd2aSEdward Tomasz Napierala 
4866a7ad07bfSEdward Tomasz Napierala static void
4867a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4868a7ad07bfSEdward Tomasz Napierala {
4869c34bbd2aSEdward Tomasz Napierala 
48704b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4871c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4872c34bbd2aSEdward Tomasz Napierala 
4873af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4874af3c786cSMateusz Guzik 		return;
4875a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4876a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4877a7ad07bfSEdward Tomasz Napierala }
4878a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4879a7ad07bfSEdward Tomasz Napierala 
4880413628a7SBjoern A. Zeeb #ifdef DDB
4881b38ff370SJamie Gritton 
4882b38ff370SJamie Gritton static void
4883b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4884413628a7SBjoern A. Zeeb {
4885672756aaSJamie Gritton 	struct bool_flags *bf;
4886672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4887b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4888b38ff370SJamie Gritton 	int ii;
4889500f82d6SZhenlei Huang 	struct prison_ip *pip;
4890413628a7SBjoern A. Zeeb #endif
4891672756aaSJamie Gritton 	unsigned f;
48928144690aSEric van Gyzen #ifdef INET
48938144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
48948144690aSEric van Gyzen #endif
4895413628a7SBjoern A. Zeeb #ifdef INET6
4896413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4897413628a7SBjoern A. Zeeb #endif
4898413628a7SBjoern A. Zeeb 
4899b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4900b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4901b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
49020304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4903b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4904b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
49051158508aSJamie Gritton 	db_printf(" state           = %s\n",
49061158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
49071158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_DYING ? "dying" :
49081158508aSJamie Gritton 	    "invalid");
4909b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4910b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4911b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4912679e1390SJamie Gritton #ifdef VIMAGE
4913679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4914679e1390SJamie Gritton #endif
4915b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4916b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
49170cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4918fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4919fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
49200304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
49210304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4922fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
4923672756aaSJamie Gritton 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4924672756aaSJamie Gritton 		if (pr->pr_flags & bf->flag)
4925672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4926672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
4927672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4928672756aaSJamie Gritton 	     jsf++) {
4929672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
4930672756aaSJamie Gritton 		db_printf(" %-16s= %s\n", jsf->name,
4931672756aaSJamie Gritton 		    (f != 0 && f == jsf->disable) ? "disable"
4932672756aaSJamie Gritton 		    : (f == jsf->new) ? "new"
49337cbf7213SJamie Gritton 		    : "inherit");
49347cbf7213SJamie Gritton 	}
4935fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
4936672756aaSJamie Gritton 	for (bf = pr_flag_allow;
49375d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
49385d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
4939672756aaSJamie Gritton 	     bf++)
4940672756aaSJamie Gritton 		if (pr->pr_allow & bf->flag)
4941672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4942b38ff370SJamie Gritton 	db_printf("\n");
49430304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4944c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4945c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4946c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
494776ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4948413628a7SBjoern A. Zeeb #ifdef INET
4949500f82d6SZhenlei Huang 	if ((pip = pr->pr_addrs[PR_INET]) != NULL) {
4950500f82d6SZhenlei Huang 		db_printf(" ip4s            = %d\n", pip->ips);
4951500f82d6SZhenlei Huang 		for (ii = 0; ii < pip->ips; ii++)
4952b38ff370SJamie Gritton 			db_printf(" %s %s\n",
4953fe0518e9SBjoern A. Zeeb 			    ii == 0 ? "ip4.addr        =" : "                 ",
4954eb8dcdeaSGleb Smirnoff 			    inet_ntoa_r(
4955500f82d6SZhenlei Huang 			    *(const struct in_addr *)PR_IP(pip, PR_INET, ii),
4956eb8dcdeaSGleb Smirnoff 			    ip4buf));
4957eb8dcdeaSGleb Smirnoff 	}
4958413628a7SBjoern A. Zeeb #endif
4959413628a7SBjoern A. Zeeb #ifdef INET6
4960500f82d6SZhenlei Huang 	if ((pip = pr->pr_addrs[PR_INET6]) != NULL) {
4961500f82d6SZhenlei Huang 		db_printf(" ip6s            = %d\n", pip->ips);
4962500f82d6SZhenlei Huang 		for (ii = 0; ii < pip->ips; ii++)
4963b38ff370SJamie Gritton 			db_printf(" %s %s\n",
4964fe0518e9SBjoern A. Zeeb 			    ii == 0 ? "ip6.addr        =" : "                 ",
4965eb8dcdeaSGleb Smirnoff 			    ip6_sprintf(ip6buf,
4966500f82d6SZhenlei Huang 			    (const struct in6_addr *)PR_IP(pip, PR_INET6, ii)));
4967eb8dcdeaSGleb Smirnoff 	}
4968b38ff370SJamie Gritton #endif
4969b38ff370SJamie Gritton }
4970b38ff370SJamie Gritton 
4971b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4972b38ff370SJamie Gritton {
4973b38ff370SJamie Gritton 	struct prison *pr;
4974b38ff370SJamie Gritton 
4975b38ff370SJamie Gritton 	if (!have_addr) {
49760304c731SJamie Gritton 		/*
49770304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
49780304c731SJamie Gritton 		 * listed.
49790304c731SJamie Gritton 		 */
49800304c731SJamie Gritton 		db_show_prison(&prison0);
49810304c731SJamie Gritton 		if (!db_pager_quit) {
4982b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4983b38ff370SJamie Gritton 				db_show_prison(pr);
4984413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4985413628a7SBjoern A. Zeeb 					break;
4986413628a7SBjoern A. Zeeb 			}
49870304c731SJamie Gritton 		}
4988b38ff370SJamie Gritton 		return;
4989413628a7SBjoern A. Zeeb 	}
4990b38ff370SJamie Gritton 
49910304c731SJamie Gritton 	if (addr == 0)
49920304c731SJamie Gritton 		pr = &prison0;
49930304c731SJamie Gritton 	else {
4994b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4995b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4996b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4997b38ff370SJamie Gritton 				break;
4998b38ff370SJamie Gritton 		if (pr == NULL)
4999b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
5000b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
5001b38ff370SJamie Gritton 				if (pr->pr_id == addr)
5002b38ff370SJamie Gritton 					break;
5003b38ff370SJamie Gritton 		if (pr == NULL)
5004b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
5005b38ff370SJamie Gritton 			pr = (struct prison *)addr;
50060304c731SJamie Gritton 	}
5007b38ff370SJamie Gritton 	db_show_prison(pr);
5008b38ff370SJamie Gritton }
5009b38ff370SJamie Gritton 
5010413628a7SBjoern A. Zeeb #endif /* DDB */
5011