xref: /freebsd/sys/kern/kern_jail.c (revision 5ecb5444aa349246568dc6f9a615630e29191f7c)
19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
38a36da99SPedro F. Giffuni  *
4413628a7SBjoern A. Zeeb  * Copyright (c) 1999 Poul-Henning Kamp.
5413628a7SBjoern A. Zeeb  * Copyright (c) 2008 Bjoern A. Zeeb.
6b38ff370SJamie Gritton  * Copyright (c) 2009 James Gritton.
7413628a7SBjoern A. Zeeb  * All rights reserved.
8b9f0b66cSBjoern A. Zeeb  *
9b9f0b66cSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
10b9f0b66cSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
11b9f0b66cSBjoern A. Zeeb  * are met:
12b9f0b66cSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
13b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
14b9f0b66cSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
15b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
16b9f0b66cSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
17b9f0b66cSBjoern A. Zeeb  *
18b9f0b66cSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b9f0b66cSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b9f0b66cSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b9f0b66cSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b9f0b66cSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b9f0b66cSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b9f0b66cSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b9f0b66cSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b9f0b66cSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b9f0b66cSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b9f0b66cSBjoern A. Zeeb  * SUCH DAMAGE.
2907901f22SPoul-Henning Kamp  */
3075c13541SPoul-Henning Kamp 
31677b542eSDavid E. O'Brien #include <sys/cdefs.h>
32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
33677b542eSDavid E. O'Brien 
34413628a7SBjoern A. Zeeb #include "opt_ddb.h"
35413628a7SBjoern A. Zeeb #include "opt_inet.h"
36413628a7SBjoern A. Zeeb #include "opt_inet6.h"
3746e3b1cbSPawel Jakub Dawidek 
3875c13541SPoul-Henning Kamp #include <sys/param.h>
3975c13541SPoul-Henning Kamp #include <sys/types.h>
4075c13541SPoul-Henning Kamp #include <sys/kernel.h>
4175c13541SPoul-Henning Kamp #include <sys/systm.h>
4275c13541SPoul-Henning Kamp #include <sys/errno.h>
4375c13541SPoul-Henning Kamp #include <sys/sysproto.h>
4475c13541SPoul-Henning Kamp #include <sys/malloc.h>
450304c731SJamie Gritton #include <sys/osd.h>
46800c9408SRobert Watson #include <sys/priv.h>
4775c13541SPoul-Henning Kamp #include <sys/proc.h>
48eb8dcdeaSGleb Smirnoff #include <sys/epoch.h>
49b3059e09SRobert Watson #include <sys/taskqueue.h>
5057b4252eSKonstantin Belousov #include <sys/fcntl.h>
5175c13541SPoul-Henning Kamp #include <sys/jail.h>
52c3188289SKyle Evans #include <sys/linker.h>
5301137630SRobert Watson #include <sys/lock.h>
547060da62SJamie Gritton #include <sys/mman.h>
5501137630SRobert Watson #include <sys/mutex.h>
56097055e2SEdward Tomasz Napierala #include <sys/racct.h>
57f87beb93SAndriy Gapon #include <sys/rctl.h>
58a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
59dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
6076ca6f88SJamie Gritton #include <sys/sysent.h>
61fd7a8150SMike Barcroft #include <sys/namei.h>
62820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
63fd7a8150SMike Barcroft #include <sys/queue.h>
6475c13541SPoul-Henning Kamp #include <sys/socket.h>
65fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6683f1e257SRobert Watson #include <sys/sysctl.h>
67c3188289SKyle Evans #include <sys/uuid.h>
68fd7a8150SMike Barcroft #include <sys/vnode.h>
69530c0060SRobert Watson 
7075c13541SPoul-Henning Kamp #include <net/if.h>
71530c0060SRobert Watson #include <net/vnet.h>
72530c0060SRobert Watson 
7375c13541SPoul-Henning Kamp #include <netinet/in.h>
74530c0060SRobert Watson 
75413628a7SBjoern A. Zeeb #ifdef DDB
76413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
77413628a7SBjoern A. Zeeb #endif /* DDB */
7875c13541SPoul-Henning Kamp 
79aed55708SRobert Watson #include <security/mac/mac_framework.h>
80aed55708SRobert Watson 
81c3188289SKyle Evans #define	PRISON0_HOSTUUID_MODULE	"hostuuid"
828986e3a0SJamie Gritton 
8375c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
84d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8575c13541SPoul-Henning Kamp 
86592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
87592bcae8SBjoern A. Zeeb #ifdef INET
88592bcae8SBjoern A. Zeeb #ifdef INET6
89592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
90592bcae8SBjoern A. Zeeb #else
91592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
92592bcae8SBjoern A. Zeeb #endif
93592bcae8SBjoern A. Zeeb #else /* !INET */
94592bcae8SBjoern A. Zeeb #ifdef INET6
95592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
96592bcae8SBjoern A. Zeeb #else
97592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
98592bcae8SBjoern A. Zeeb #endif
99592bcae8SBjoern A. Zeeb #endif
100592bcae8SBjoern A. Zeeb 
1010304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
1020304c731SJamie Gritton struct prison prison0 = {
1030304c731SJamie Gritton 	.pr_id		= 0,
1040304c731SJamie Gritton 	.pr_name	= "0",
1050304c731SJamie Gritton 	.pr_ref		= 1,
1060304c731SJamie Gritton 	.pr_uref	= 1,
1070304c731SJamie Gritton 	.pr_path	= "/",
1080304c731SJamie Gritton 	.pr_securelevel	= -1,
1090cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
1101158508aSJamie Gritton 	.pr_state	= PRISON_STATE_ALIVE,
111b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1128986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
11313e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
114eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
115592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
116eb79e1c7SBjoern A. Zeeb #else
117592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
118eb79e1c7SBjoern A. Zeeb #endif
1190e5c6bd4SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL_STATIC,
1200304c731SJamie Gritton };
1210304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
12283f1e257SRobert Watson 
123672756aaSJamie Gritton struct bool_flags {
124672756aaSJamie Gritton 	const char	*name;
125672756aaSJamie Gritton 	const char	*noname;
1265d58f959SJamie Gritton 	volatile u_int	 flag;
127672756aaSJamie Gritton };
128672756aaSJamie Gritton struct jailsys_flags {
129672756aaSJamie Gritton 	const char	*name;
130672756aaSJamie Gritton 	unsigned	 disable;
131672756aaSJamie Gritton 	unsigned	 new;
132672756aaSJamie Gritton };
133672756aaSJamie Gritton 
134a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
135dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
136b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
137b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
138a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1392110d913SXin LI int	lastprid = 0;
140fd7a8150SMike Barcroft 
1417de883c8SJamie Gritton static int get_next_prid(struct prison **insprp);
142f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
143b3059e09SRobert Watson static void prison_complete(void *context, int pending);
144b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
145c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
146f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags);
147a9f7455cSJamie Gritton static void prison_cleanup(struct prison *pr);
148f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr);
149c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr);
150*5ecb5444SMateusz Guzik static void prison_proc_relink(struct prison *opr, struct prison *npr,
151*5ecb5444SMateusz Guzik     struct proc *p);
1520fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag,
1530fe74ae6SJamie Gritton     int enable);
1540304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
155a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
156a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
157c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
158a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
159a7ad07bfSEdward Tomasz Napierala #endif
160fd7a8150SMike Barcroft 
161b38ff370SJamie Gritton /* Flags for prison_deref */
1622a4b2251SJamie Gritton #define	PD_DEREF	0x01	/* Decrement pr_ref */
1632a4b2251SJamie Gritton #define	PD_DEUREF	0x02	/* Decrement pr_uref */
164c861373bSJamie Gritton #define	PD_KILL		0x04	/* Remove jail, kill processes, etc */
165c861373bSJamie Gritton #define	PD_LOCKED	0x10	/* pr_mtx is held */
166c861373bSJamie Gritton #define	PD_LIST_SLOCKED	0x20	/* allprison_lock is held shared */
167c861373bSJamie Gritton #define	PD_LIST_XLOCKED	0x40	/* allprison_lock is held exclusive */
168589e4c1dSJamie Gritton #define PD_OP_FLAGS	0x07	/* Operation flags */
169589e4c1dSJamie Gritton #define PD_LOCK_FLAGS	0x70	/* Lock status flags */
170fd7a8150SMike Barcroft 
1710304c731SJamie Gritton /*
1725cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1735cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1745cc70397SBjoern A. Zeeb  * terminating entry.
1750304c731SJamie Gritton  */
176672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = {
177672756aaSJamie Gritton 	{"persist", "nopersist", PR_PERSIST},
178592bcae8SBjoern A. Zeeb #ifdef INET
179672756aaSJamie Gritton 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
180592bcae8SBjoern A. Zeeb #endif
181592bcae8SBjoern A. Zeeb #ifdef INET6
182672756aaSJamie Gritton 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
183592bcae8SBjoern A. Zeeb #endif
1840304c731SJamie Gritton };
185672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
1860304c731SJamie Gritton 
187672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = {
1887cbf7213SJamie Gritton 	{"host", 0, PR_HOST},
1897cbf7213SJamie Gritton #ifdef VIMAGE
1907cbf7213SJamie Gritton 	{"vnet", 0, PR_VNET},
1917cbf7213SJamie Gritton #endif
1920304c731SJamie Gritton #ifdef INET
1936a3f2779SJamie Gritton 	{"ip4", PR_IP4_USER, PR_IP4_USER},
1940304c731SJamie Gritton #endif
1950304c731SJamie Gritton #ifdef INET6
1966a3f2779SJamie Gritton 	{"ip6", PR_IP6_USER, PR_IP6_USER},
197679e1390SJamie Gritton #endif
1980304c731SJamie Gritton };
1995cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
2000304c731SJamie Gritton 
2015d58f959SJamie Gritton /*
2025d58f959SJamie Gritton  * Make this array full-size so dynamic parameters can be added.
2035d58f959SJamie Gritton  * It is protected by prison0.mtx, but lockless reading is allowed
2045d58f959SJamie Gritton  * with an atomic check of the flag values.
2055d58f959SJamie Gritton  */
2060e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
207672756aaSJamie Gritton 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
208672756aaSJamie Gritton 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
209672756aaSJamie Gritton 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
210672756aaSJamie Gritton 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
211672756aaSJamie Gritton 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
212672756aaSJamie Gritton 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
213672756aaSJamie Gritton 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
214ccd6ac9fSAntoine Brodin 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
215672756aaSJamie Gritton 	{"allow.reserved_ports", "allow.noreserved_ports",
216672756aaSJamie Gritton 	 PR_ALLOW_RESERVED_PORTS},
217b19d66fdSJamie Gritton 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
218b3079544SJamie Gritton 	{"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
219b3079544SJamie Gritton 	 PR_ALLOW_UNPRIV_DEBUG},
22005e1e482SMariusz Zaborski 	{"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
2210304c731SJamie Gritton };
2225d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
223672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
2240304c731SJamie Gritton 
225b3079544SJamie Gritton #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | \
226b3079544SJamie Gritton 					 PR_ALLOW_RESERVED_PORTS | \
22705e1e482SMariusz Zaborski 					 PR_ALLOW_UNPRIV_DEBUG | \
22805e1e482SMariusz Zaborski 					 PR_ALLOW_SUSER)
22942bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
230bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2310304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
23242bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2330cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2340304c731SJamie Gritton #if defined(INET) || defined(INET6)
235e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2360304c731SJamie Gritton #endif
2370304c731SJamie Gritton 
238b96bd95bSIan Lepore /*
239b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
240b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
241b96bd95bSIan Lepore  */
242b96bd95bSIan Lepore void
243b96bd95bSIan Lepore prison0_init(void)
244b96bd95bSIan Lepore {
245c3188289SKyle Evans 	uint8_t *file, *data;
246c3188289SKyle Evans 	size_t size;
247d2ef3774SJessica Clarke 	char buf[sizeof(prison0.pr_hostuuid)];
248d2ef3774SJessica Clarke 	bool valid;
249b96bd95bSIan Lepore 
250b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
251b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
252b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
253c3188289SKyle Evans 
254c3188289SKyle Evans 	/* If we have a preloaded hostuuid, use it. */
255c3188289SKyle Evans 	file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
256c3188289SKyle Evans 	if (file != NULL) {
257c3188289SKyle Evans 		data = preload_fetch_addr(file);
258c3188289SKyle Evans 		size = preload_fetch_size(file);
259c3188289SKyle Evans 		if (data != NULL) {
260c3188289SKyle Evans 			/*
261c3188289SKyle Evans 			 * The preloaded data may include trailing whitespace, almost
262c3188289SKyle Evans 			 * certainly a newline; skip over any whitespace or
263c3188289SKyle Evans 			 * non-printable characters to be safe.
264c3188289SKyle Evans 			 */
265c3188289SKyle Evans 			while (size > 0 && data[size - 1] <= 0x20) {
266b6be9566SColin Percival 				size--;
267c3188289SKyle Evans 			}
268d2ef3774SJessica Clarke 
269d2ef3774SJessica Clarke 			valid = false;
270d2ef3774SJessica Clarke 
271d2ef3774SJessica Clarke 			/*
272d2ef3774SJessica Clarke 			 * Not NUL-terminated when passed from loader, but
273d2ef3774SJessica Clarke 			 * validate_uuid requires that due to using sscanf (as
274d2ef3774SJessica Clarke 			 * does the subsequent strlcpy, since it still reads
275d2ef3774SJessica Clarke 			 * past the given size to return the true length);
276d2ef3774SJessica Clarke 			 * bounce to a temporary buffer to fix.
277d2ef3774SJessica Clarke 			 */
278d2ef3774SJessica Clarke 			if (size >= sizeof(buf))
279d2ef3774SJessica Clarke 				goto done;
280d2ef3774SJessica Clarke 
281d2ef3774SJessica Clarke 			memcpy(buf, data, size);
282d2ef3774SJessica Clarke 			buf[size] = '\0';
283d2ef3774SJessica Clarke 
284d2ef3774SJessica Clarke 			if (validate_uuid(buf, size, NULL, 0) != 0)
285d2ef3774SJessica Clarke 				goto done;
286d2ef3774SJessica Clarke 
287d2ef3774SJessica Clarke 			valid = true;
288d2ef3774SJessica Clarke 			(void)strlcpy(prison0.pr_hostuuid, buf,
289d2ef3774SJessica Clarke 			    sizeof(prison0.pr_hostuuid));
290d2ef3774SJessica Clarke 
291d2ef3774SJessica Clarke done:
292d2ef3774SJessica Clarke 			if (bootverbose && !valid) {
293330f110bSColin Percival 				printf("hostuuid: preload data malformed: '%.*s'\n",
294330f110bSColin Percival 				    (int)size, data);
295c3188289SKyle Evans 			}
296c3188289SKyle Evans 		}
297c3188289SKyle Evans 	}
298c3188289SKyle Evans 	if (bootverbose)
299c3188289SKyle Evans 		printf("hostuuid: using %s\n", prison0.pr_hostuuid);
300b96bd95bSIan Lepore }
301b96bd95bSIan Lepore 
302116734c4SMatthew Dillon /*
3039ddb7954SMike Barcroft  * struct jail_args {
3049ddb7954SMike Barcroft  *	struct jail *jail;
3059ddb7954SMike Barcroft  * };
306116734c4SMatthew Dillon  */
30775c13541SPoul-Henning Kamp int
308c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap)
30975c13541SPoul-Henning Kamp {
310413628a7SBjoern A. Zeeb 	uint32_t version;
311413628a7SBjoern A. Zeeb 	int error;
3120304c731SJamie Gritton 	struct jail j;
313413628a7SBjoern A. Zeeb 
314413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
315413628a7SBjoern A. Zeeb 	if (error)
316413628a7SBjoern A. Zeeb 		return (error);
317413628a7SBjoern A. Zeeb 
318413628a7SBjoern A. Zeeb 	switch (version) {
319413628a7SBjoern A. Zeeb 	case 0:
320413628a7SBjoern A. Zeeb 	{
321413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
322413628a7SBjoern A. Zeeb 
3230304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
3240304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
325413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
326413628a7SBjoern A. Zeeb 		if (error)
327413628a7SBjoern A. Zeeb 			return (error);
3280304c731SJamie Gritton 		j.version = j0.version;
3290304c731SJamie Gritton 		j.path = j0.path;
3300304c731SJamie Gritton 		j.hostname = j0.hostname;
331b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
332413628a7SBjoern A. Zeeb 		break;
333413628a7SBjoern A. Zeeb 	}
334413628a7SBjoern A. Zeeb 
335413628a7SBjoern A. Zeeb 	case 1:
336413628a7SBjoern A. Zeeb 		/*
337413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
338413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
339413628a7SBjoern A. Zeeb 		 */
340413628a7SBjoern A. Zeeb 		return (EINVAL);
341413628a7SBjoern A. Zeeb 
342413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
343413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
344413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
345413628a7SBjoern A. Zeeb 		if (error)
346413628a7SBjoern A. Zeeb 			return (error);
3470304c731SJamie Gritton 		break;
3480304c731SJamie Gritton 
3490304c731SJamie Gritton 	default:
3500304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3510304c731SJamie Gritton 		return (EINVAL);
3520304c731SJamie Gritton 	}
353c542c43eSJamie Gritton 	return (kern_jail(td, &j));
3540304c731SJamie Gritton }
3550304c731SJamie Gritton 
3560304c731SJamie Gritton int
357c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j)
3580304c731SJamie Gritton {
359c542c43eSJamie Gritton 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
360e92e0574SJamie Gritton #ifdef INET
361e92e0574SJamie Gritton 			    + 1
362e92e0574SJamie Gritton #endif
363e92e0574SJamie Gritton #ifdef INET6
364e92e0574SJamie Gritton 			    + 1
365e92e0574SJamie Gritton #endif
366e92e0574SJamie Gritton 			    )];
3670304c731SJamie Gritton 	struct uio opt;
3680304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
369672756aaSJamie Gritton 	struct bool_flags *bf;
3700304c731SJamie Gritton #ifdef INET
371e92e0574SJamie Gritton 	uint32_t ip4s;
3720304c731SJamie Gritton 	struct in_addr *u_ip4;
3730304c731SJamie Gritton #endif
3740304c731SJamie Gritton #ifdef INET6
3750304c731SJamie Gritton 	struct in6_addr *u_ip6;
3760304c731SJamie Gritton #endif
3770304c731SJamie Gritton 	size_t tmplen;
378c542c43eSJamie Gritton 	int error, enforce_statfs;
3790304c731SJamie Gritton 
3800304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3810304c731SJamie Gritton 	opt.uio_iov = optiov;
3820304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3830304c731SJamie Gritton 	opt.uio_offset = -1;
3840304c731SJamie Gritton 	opt.uio_resid = -1;
3850304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3860304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3870304c731SJamie Gritton 	opt.uio_td = td;
3880304c731SJamie Gritton 
3890304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3900304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
391672756aaSJamie Gritton 		for (bf = pr_flag_allow;
3920e5c6bd4SJamie Gritton 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
3935d58f959SJamie Gritton 			atomic_load_int(&bf->flag) != 0;
394672756aaSJamie Gritton 		     bf++) {
395672756aaSJamie Gritton 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
396672756aaSJamie Gritton 			    (jail_default_allow & bf->flag)
397672756aaSJamie Gritton 			    ? bf->name : bf->noname);
3980304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3990304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
4000304c731SJamie Gritton 			opt.uio_iovcnt += 2;
4010304c731SJamie Gritton 		}
4020304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
4030304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
4040304c731SJamie Gritton 		opt.uio_iovcnt++;
4050304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
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 	}
4100304c731SJamie Gritton 
411b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
412b38ff370SJamie Gritton #ifdef INET
4130304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
4140304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
415b38ff370SJamie Gritton 		return (EINVAL);
4160304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
417b38ff370SJamie Gritton #else
4180304c731SJamie Gritton 	if (j->ip4s > 0)
419b38ff370SJamie Gritton 		return (EINVAL);
420b38ff370SJamie Gritton #endif
421b38ff370SJamie Gritton #ifdef INET6
4220304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
423b38ff370SJamie Gritton 		return (EINVAL);
4240304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
425b38ff370SJamie Gritton #else
4260304c731SJamie Gritton 	if (j->ip6s > 0)
427b38ff370SJamie Gritton 		return (EINVAL);
428b38ff370SJamie Gritton #endif
429b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
430b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
431b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
432b38ff370SJamie Gritton #ifdef INET
433b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
434b38ff370SJamie Gritton #endif
435b38ff370SJamie Gritton #ifdef INET6
436b38ff370SJamie Gritton #ifdef INET
4370304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
438b38ff370SJamie Gritton #else
439b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
440b38ff370SJamie Gritton #endif
441b38ff370SJamie Gritton #endif
4420304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4430304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4440304c731SJamie Gritton 	opt.uio_iovcnt++;
4450304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4460304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4470304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
448b38ff370SJamie Gritton 	if (error) {
449b38ff370SJamie Gritton 		free(u_path, M_TEMP);
450b38ff370SJamie Gritton 		return (error);
451b38ff370SJamie Gritton 	}
4520304c731SJamie Gritton 	opt.uio_iovcnt++;
4530304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4540304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4550304c731SJamie Gritton 	opt.uio_iovcnt++;
4560304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4570304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4580304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
459b38ff370SJamie Gritton 	if (error) {
460b38ff370SJamie Gritton 		free(u_path, M_TEMP);
461b38ff370SJamie Gritton 		return (error);
462b38ff370SJamie Gritton 	}
4630304c731SJamie Gritton 	opt.uio_iovcnt++;
4640304c731SJamie Gritton 	if (j->jailname != NULL) {
465b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
466b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
467b38ff370SJamie Gritton 		opt.uio_iovcnt++;
468b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4690304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
470b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
471b38ff370SJamie Gritton 		if (error) {
472b38ff370SJamie Gritton 			free(u_path, M_TEMP);
473b38ff370SJamie Gritton 			return (error);
474b38ff370SJamie Gritton 		}
475b38ff370SJamie Gritton 		opt.uio_iovcnt++;
476b38ff370SJamie Gritton 	}
477b38ff370SJamie Gritton #ifdef INET
478b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
479b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
480b38ff370SJamie Gritton 	opt.uio_iovcnt++;
481b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4820304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4830304c731SJamie Gritton 	if (j->version == 0)
4840304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4850304c731SJamie Gritton 	else {
4860304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
487b38ff370SJamie Gritton 		if (error) {
488b38ff370SJamie Gritton 			free(u_path, M_TEMP);
489b38ff370SJamie Gritton 			return (error);
490b38ff370SJamie Gritton 		}
4910304c731SJamie Gritton 	}
492b38ff370SJamie Gritton 	opt.uio_iovcnt++;
493b38ff370SJamie Gritton #endif
494b38ff370SJamie Gritton #ifdef INET6
495b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
496b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
497b38ff370SJamie Gritton 	opt.uio_iovcnt++;
498b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4990304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
5000304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
501b38ff370SJamie Gritton 	if (error) {
502b38ff370SJamie Gritton 		free(u_path, M_TEMP);
503b38ff370SJamie Gritton 		return (error);
504b38ff370SJamie Gritton 	}
505b38ff370SJamie Gritton 	opt.uio_iovcnt++;
506b38ff370SJamie Gritton #endif
50702abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
5080304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
509b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
510b38ff370SJamie Gritton 	free(u_path, M_TEMP);
511b38ff370SJamie Gritton 	return (error);
512b38ff370SJamie Gritton }
513b38ff370SJamie Gritton 
514b38ff370SJamie Gritton /*
515b38ff370SJamie Gritton  * struct jail_set_args {
516b38ff370SJamie Gritton  *	struct iovec *iovp;
517b38ff370SJamie Gritton  *	unsigned int iovcnt;
518b38ff370SJamie Gritton  *	int flags;
519b38ff370SJamie Gritton  * };
520b38ff370SJamie Gritton  */
521b38ff370SJamie Gritton int
5228451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
523b38ff370SJamie Gritton {
524b38ff370SJamie Gritton 	struct uio *auio;
525b38ff370SJamie Gritton 	int error;
526b38ff370SJamie Gritton 
527b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
528b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
529b38ff370SJamie Gritton 		return (EINVAL);
530b38ff370SJamie Gritton 
531b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
532b38ff370SJamie Gritton 	if (error)
533b38ff370SJamie Gritton 		return (error);
534b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
535b38ff370SJamie Gritton 	free(auio, M_IOV);
536b38ff370SJamie Gritton 	return (error);
537413628a7SBjoern A. Zeeb }
538413628a7SBjoern A. Zeeb 
539eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
540eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *);
541eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *);
542eb8dcdeaSGleb Smirnoff static const struct pr_family {
543eb8dcdeaSGleb Smirnoff 	size_t			size;
544eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t	*cmp;
545eb8dcdeaSGleb Smirnoff 	prison_addr_valid_t	*valid;
546eb8dcdeaSGleb Smirnoff 	int			ip_flag;
547eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = {
548eb8dcdeaSGleb Smirnoff #ifdef INET
549eb8dcdeaSGleb Smirnoff 	[PR_INET] = {
550eb8dcdeaSGleb Smirnoff 		.size = sizeof(struct in_addr),
551eb8dcdeaSGleb Smirnoff 		.cmp = prison_qcmp_v4,
552eb8dcdeaSGleb Smirnoff 		.valid = prison_valid_v4,
553eb8dcdeaSGleb Smirnoff 		.ip_flag = PR_IP4_USER,
554eb8dcdeaSGleb Smirnoff 	 },
555eb8dcdeaSGleb Smirnoff #endif
556eb8dcdeaSGleb Smirnoff #ifdef INET6
557eb8dcdeaSGleb Smirnoff 	[PR_INET6] = {
558eb8dcdeaSGleb Smirnoff 		.size = sizeof(struct in6_addr),
559eb8dcdeaSGleb Smirnoff 		.cmp = prison_qcmp_v6,
560eb8dcdeaSGleb Smirnoff 		.valid = prison_valid_v6,
561eb8dcdeaSGleb Smirnoff 		.ip_flag = PR_IP6_USER,
562eb8dcdeaSGleb Smirnoff 	},
563eb8dcdeaSGleb Smirnoff #endif
564eb8dcdeaSGleb Smirnoff };
565eb8dcdeaSGleb Smirnoff 
566eb8dcdeaSGleb Smirnoff /*
567eb8dcdeaSGleb Smirnoff  * Network address lists (pr_addrs) allocation for jails.  The addresses
568eb8dcdeaSGleb Smirnoff  * are accessed locklessly by the network stack, thus need to be protected by
569eb8dcdeaSGleb Smirnoff  * the network epoch.
570eb8dcdeaSGleb Smirnoff  */
571eb8dcdeaSGleb Smirnoff struct prison_ip {
572eb8dcdeaSGleb Smirnoff 	struct epoch_context ctx;
573eb8dcdeaSGleb Smirnoff 	uint32_t	ips;
574eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C
575eb8dcdeaSGleb Smirnoff 	union {
576eb8dcdeaSGleb Smirnoff 		struct in_addr pr_ip4[];
577eb8dcdeaSGleb Smirnoff 		struct in6_addr pr_ip6[];
578eb8dcdeaSGleb Smirnoff 	};
579eb8dcdeaSGleb Smirnoff #else /* No future C :( */
580eb8dcdeaSGleb Smirnoff #define	PR_IP(pip, i)	((const char *)((pip) + 1) + pr_families[af].size * (i))
581eb8dcdeaSGleb Smirnoff #define	PR_IPD(pip, i)	((char *)((pip) + 1) + pr_families[af].size * (i))
582eb8dcdeaSGleb Smirnoff #endif
583eb8dcdeaSGleb Smirnoff };
584eb8dcdeaSGleb Smirnoff 
585eb8dcdeaSGleb Smirnoff static struct prison_ip *
586eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
587eb8dcdeaSGleb Smirnoff {
588eb8dcdeaSGleb Smirnoff 	struct prison_ip *pip;
589eb8dcdeaSGleb Smirnoff 
590eb8dcdeaSGleb Smirnoff 	pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
591eb8dcdeaSGleb Smirnoff 	    M_PRISON, flags);
592eb8dcdeaSGleb Smirnoff 	if (pip != NULL)
593eb8dcdeaSGleb Smirnoff 		pip->ips = cnt;
594eb8dcdeaSGleb Smirnoff 	return (pip);
595eb8dcdeaSGleb Smirnoff }
596eb8dcdeaSGleb Smirnoff 
597eb8dcdeaSGleb Smirnoff /*
598eb8dcdeaSGleb Smirnoff  * Allocate and copyin user supplied address list, sorting and validating.
599eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
600eb8dcdeaSGleb Smirnoff  */
601eb8dcdeaSGleb Smirnoff static struct prison_ip *
602eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
603eb8dcdeaSGleb Smirnoff {
604eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
605eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
606eb8dcdeaSGleb Smirnoff 	struct prison_ip *pip;
607eb8dcdeaSGleb Smirnoff 
608eb8dcdeaSGleb Smirnoff 	pip = prison_ip_alloc(af, cnt, M_WAITOK);
609eb8dcdeaSGleb Smirnoff 	bcopy(op, pip + 1, cnt * size);
610eb8dcdeaSGleb Smirnoff 	/*
611eb8dcdeaSGleb Smirnoff 	 * IP addresses are all sorted but ip[0] to preserve
612eb8dcdeaSGleb Smirnoff 	 * the primary IP address as given from userland.
613eb8dcdeaSGleb Smirnoff 	 * This special IP is used for unbound outgoing
614eb8dcdeaSGleb Smirnoff 	 * connections as well for "loopback" traffic in case
615eb8dcdeaSGleb Smirnoff 	 * source address selection cannot find any more fitting
616eb8dcdeaSGleb Smirnoff 	 * address to connect from.
617eb8dcdeaSGleb Smirnoff 	 */
618eb8dcdeaSGleb Smirnoff 	if (cnt > 1)
619eb8dcdeaSGleb Smirnoff 		qsort((char *)(pip + 1) + size, cnt - 1, size,
620eb8dcdeaSGleb Smirnoff 		    pr_families[af].cmp);
621eb8dcdeaSGleb Smirnoff 	/*
622eb8dcdeaSGleb Smirnoff 	 * Check for duplicate addresses and do some simple
623eb8dcdeaSGleb Smirnoff 	 * zero and broadcast checks. If users give other bogus
624eb8dcdeaSGleb Smirnoff 	 * addresses it is their problem.
625eb8dcdeaSGleb Smirnoff 	 */
626eb8dcdeaSGleb Smirnoff 	for (int i = 0; i < cnt; i++) {
627eb8dcdeaSGleb Smirnoff 		if (!pr_families[af].valid(PR_IP(pip, i))) {
628eb8dcdeaSGleb Smirnoff 			free(pip, M_PRISON);
629eb8dcdeaSGleb Smirnoff 			return (NULL);
630eb8dcdeaSGleb Smirnoff 		}
631eb8dcdeaSGleb Smirnoff 		if (i + 1 < cnt &&
632eb8dcdeaSGleb Smirnoff 		    (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 ||
633eb8dcdeaSGleb Smirnoff 		     cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) {
634eb8dcdeaSGleb Smirnoff 			free(pip, M_PRISON);
635eb8dcdeaSGleb Smirnoff 			return (NULL);
636eb8dcdeaSGleb Smirnoff 		}
637eb8dcdeaSGleb Smirnoff 	}
638eb8dcdeaSGleb Smirnoff 
639eb8dcdeaSGleb Smirnoff 	return (pip);
640eb8dcdeaSGleb Smirnoff }
641eb8dcdeaSGleb Smirnoff 
642eb8dcdeaSGleb Smirnoff /*
643eb8dcdeaSGleb Smirnoff  * Allocate and dup parent prison address list.
644eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
645eb8dcdeaSGleb Smirnoff  */
646eb8dcdeaSGleb Smirnoff static void
647eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
648eb8dcdeaSGleb Smirnoff {
649eb8dcdeaSGleb Smirnoff 
650eb8dcdeaSGleb Smirnoff 	if (ppr->pr_addrs[af] != NULL) {
651eb8dcdeaSGleb Smirnoff 		pr->pr_addrs[af] = prison_ip_alloc(af,
652eb8dcdeaSGleb Smirnoff 		    ppr->pr_addrs[af]->ips, M_WAITOK);
653eb8dcdeaSGleb Smirnoff 		bcopy(ppr->pr_addrs[af], pr->pr_addrs[af],
654eb8dcdeaSGleb Smirnoff 		    pr->pr_addrs[af]->ips * pr_families[af].size);
655eb8dcdeaSGleb Smirnoff 	}
656eb8dcdeaSGleb Smirnoff }
657eb8dcdeaSGleb Smirnoff 
658eb8dcdeaSGleb Smirnoff /*
659eb8dcdeaSGleb Smirnoff  * Make sure the new set of IP addresses is a subset of the parent's list.
660eb8dcdeaSGleb Smirnoff  * Don't worry about the parent being unlocked, as any setting is done with
661eb8dcdeaSGleb Smirnoff  * allprison_lock held.
662eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
663eb8dcdeaSGleb Smirnoff  */
664eb8dcdeaSGleb Smirnoff static bool
665eb8dcdeaSGleb Smirnoff prison_ip_parent_match(const struct prison_ip *ppip,
666eb8dcdeaSGleb Smirnoff     const struct prison_ip *pip, const pr_family_t af)
667eb8dcdeaSGleb Smirnoff {
668eb8dcdeaSGleb Smirnoff 	prison_addr_cmp_t *const cmp = pr_families[af].cmp;
669eb8dcdeaSGleb Smirnoff 	int i, j;
670eb8dcdeaSGleb Smirnoff 
671eb8dcdeaSGleb Smirnoff 	if (ppip == NULL)
672eb8dcdeaSGleb Smirnoff 		return (false);
673eb8dcdeaSGleb Smirnoff 
674eb8dcdeaSGleb Smirnoff 	for (i = 0; i < ppip->ips; i++)
675eb8dcdeaSGleb Smirnoff 		if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0)
676eb8dcdeaSGleb Smirnoff 			break;
677eb8dcdeaSGleb Smirnoff 
678eb8dcdeaSGleb Smirnoff 	if (i == ppip->ips)
679eb8dcdeaSGleb Smirnoff 		/* Main address not present in parent. */
680eb8dcdeaSGleb Smirnoff 		return (false);
681eb8dcdeaSGleb Smirnoff 
682eb8dcdeaSGleb Smirnoff 	if (pip->ips > 1) {
683eb8dcdeaSGleb Smirnoff 		for (i = j = 1; i < pip->ips; i++) {
684eb8dcdeaSGleb Smirnoff 			if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0)
685eb8dcdeaSGleb Smirnoff 				/* Equals to parent primary address. */
686eb8dcdeaSGleb Smirnoff 				continue;
687eb8dcdeaSGleb Smirnoff 			for (; j < ppip->ips; j++)
688eb8dcdeaSGleb Smirnoff 				if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0)
689eb8dcdeaSGleb Smirnoff 					break;
690eb8dcdeaSGleb Smirnoff 			if (j == ppip->ips)
691eb8dcdeaSGleb Smirnoff 				break;
692eb8dcdeaSGleb Smirnoff 		}
693eb8dcdeaSGleb Smirnoff 		if (j == ppip->ips)
694eb8dcdeaSGleb Smirnoff 			/* Address not present in parent. */
695eb8dcdeaSGleb Smirnoff 			return (false);
696eb8dcdeaSGleb Smirnoff 	}
697eb8dcdeaSGleb Smirnoff 	return (true);
698eb8dcdeaSGleb Smirnoff }
699eb8dcdeaSGleb Smirnoff 
700eb8dcdeaSGleb Smirnoff /*
701eb8dcdeaSGleb Smirnoff  * Check for conflicting IP addresses.  We permit them if there is no more
702eb8dcdeaSGleb Smirnoff  * than one IP on each jail.  If there is a duplicate on a jail with more
703eb8dcdeaSGleb Smirnoff  * than one IP stop checking and return error.
704eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
705eb8dcdeaSGleb Smirnoff  */
706eb8dcdeaSGleb Smirnoff static bool
707eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
708eb8dcdeaSGleb Smirnoff     const struct prison_ip *pip, pr_family_t af)
709eb8dcdeaSGleb Smirnoff {
710eb8dcdeaSGleb Smirnoff 	const struct prison *tppr, *tpr;
711eb8dcdeaSGleb Smirnoff 	int descend;
712eb8dcdeaSGleb Smirnoff 
713eb8dcdeaSGleb Smirnoff #ifdef VIMAGE
714eb8dcdeaSGleb Smirnoff 	for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
715eb8dcdeaSGleb Smirnoff 		if (tppr->pr_flags & PR_VNET)
716eb8dcdeaSGleb Smirnoff 			break;
717eb8dcdeaSGleb Smirnoff #else
718eb8dcdeaSGleb Smirnoff 	tppr = &prison0;
719eb8dcdeaSGleb Smirnoff #endif
720eb8dcdeaSGleb Smirnoff 	FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
721eb8dcdeaSGleb Smirnoff 		if (tpr == pr ||
722eb8dcdeaSGleb Smirnoff #ifdef VIMAGE
723eb8dcdeaSGleb Smirnoff 		    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
724eb8dcdeaSGleb Smirnoff #endif
725eb8dcdeaSGleb Smirnoff 		    !prison_isalive(tpr)) {
726eb8dcdeaSGleb Smirnoff 			descend = 0;
727eb8dcdeaSGleb Smirnoff 			continue;
728eb8dcdeaSGleb Smirnoff 		}
729eb8dcdeaSGleb Smirnoff 		if (!(tpr->pr_flags & pr_families[af].ip_flag))
730eb8dcdeaSGleb Smirnoff 			continue;
731eb8dcdeaSGleb Smirnoff 		descend = 0;
732eb8dcdeaSGleb Smirnoff 		if (tpr->pr_addrs[af] == NULL ||
733eb8dcdeaSGleb Smirnoff 		    (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
734eb8dcdeaSGleb Smirnoff 			continue;
735eb8dcdeaSGleb Smirnoff 		for (int i = 0; i < pip->ips; i++)
736eb8dcdeaSGleb Smirnoff 			if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0)
737eb8dcdeaSGleb Smirnoff 				return (false);
738eb8dcdeaSGleb Smirnoff 	}
739eb8dcdeaSGleb Smirnoff 
740eb8dcdeaSGleb Smirnoff 	return (true);
741eb8dcdeaSGleb Smirnoff }
742eb8dcdeaSGleb Smirnoff 
743eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0,
744eb8dcdeaSGleb Smirnoff     "prison must start with epoch context");
745eb8dcdeaSGleb Smirnoff static void
746eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx)
747eb8dcdeaSGleb Smirnoff {
748eb8dcdeaSGleb Smirnoff 
749eb8dcdeaSGleb Smirnoff 	free(ctx, M_PRISON);
750eb8dcdeaSGleb Smirnoff }
751eb8dcdeaSGleb Smirnoff 
752eb8dcdeaSGleb Smirnoff static void
753eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip)
754eb8dcdeaSGleb Smirnoff {
755eb8dcdeaSGleb Smirnoff 
756eb8dcdeaSGleb Smirnoff 	if (pip != NULL)
757eb8dcdeaSGleb Smirnoff 		NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
758eb8dcdeaSGleb Smirnoff }
759eb8dcdeaSGleb Smirnoff 
760eb8dcdeaSGleb Smirnoff static void
761eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
762eb8dcdeaSGleb Smirnoff {
763eb8dcdeaSGleb Smirnoff 	struct prison_ip **mem, *old;
764eb8dcdeaSGleb Smirnoff 
765eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
766eb8dcdeaSGleb Smirnoff 
767eb8dcdeaSGleb Smirnoff 	mem = &pr->pr_addrs[af];
768eb8dcdeaSGleb Smirnoff 
769eb8dcdeaSGleb Smirnoff 	old = *mem;
770eb8dcdeaSGleb Smirnoff 	ck_pr_store_ptr(mem, new);
771eb8dcdeaSGleb Smirnoff 	prison_ip_free(old);
772eb8dcdeaSGleb Smirnoff }
773eb8dcdeaSGleb Smirnoff 
774eb8dcdeaSGleb Smirnoff /*
775eb8dcdeaSGleb Smirnoff  * Restrict a prison's IP address list with its parent's, possibly replacing
776eb8dcdeaSGleb Smirnoff  * it.  Return true if the replacement buffer was used (or would have been).
777eb8dcdeaSGleb Smirnoff  * kern_jail_set() helper.
778eb8dcdeaSGleb Smirnoff  */
779eb8dcdeaSGleb Smirnoff static bool
780eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af,
781eb8dcdeaSGleb Smirnoff     struct prison_ip *new)
782eb8dcdeaSGleb Smirnoff {
783eb8dcdeaSGleb Smirnoff 	const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
784eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip = pr->pr_addrs[af];
785eb8dcdeaSGleb Smirnoff 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
786eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
787eb8dcdeaSGleb Smirnoff 	uint32_t ips;
788eb8dcdeaSGleb Smirnoff 	bool alloced;
789eb8dcdeaSGleb Smirnoff 
790eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
791eb8dcdeaSGleb Smirnoff 
792eb8dcdeaSGleb Smirnoff 	/*
793eb8dcdeaSGleb Smirnoff 	 * Due to epoch-synchronized access to the IP address lists we always
794eb8dcdeaSGleb Smirnoff 	 * allocate a new list even if the old one has enough space.  We could
795eb8dcdeaSGleb Smirnoff 	 * atomically update an IPv4 address inside a list, but that would
796eb8dcdeaSGleb Smirnoff 	 * screw up sorting, and in case of IPv6 we can't even atomically write
797eb8dcdeaSGleb Smirnoff 	 * one.
798eb8dcdeaSGleb Smirnoff 	 */
799eb8dcdeaSGleb Smirnoff 	ips = (pr->pr_flags & pr_families[af].ip_flag) ? pip->ips : ppip->ips;
800eb8dcdeaSGleb Smirnoff 	if (ips == 0) {
801eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, af, NULL);
802eb8dcdeaSGleb Smirnoff 		return (false);
803eb8dcdeaSGleb Smirnoff 	}
804eb8dcdeaSGleb Smirnoff 	if (new == NULL) {
805eb8dcdeaSGleb Smirnoff 		new = prison_ip_alloc(af, ips, M_NOWAIT);
806eb8dcdeaSGleb Smirnoff 		if (new == NULL)
807eb8dcdeaSGleb Smirnoff 			return (true);
808eb8dcdeaSGleb Smirnoff 		alloced = true;
809eb8dcdeaSGleb Smirnoff 	} else
810eb8dcdeaSGleb Smirnoff 		alloced = false;
811eb8dcdeaSGleb Smirnoff 	if (!(pr->pr_flags & pr_families[af].ip_flag)) {
812eb8dcdeaSGleb Smirnoff 		/* This has no user settings, so just copy the parent's list. */
813eb8dcdeaSGleb Smirnoff 		bcopy(ppip, new, ips * size);
814eb8dcdeaSGleb Smirnoff 	} else {
815eb8dcdeaSGleb Smirnoff 		/* Remove addresses that aren't in the parent. */
816eb8dcdeaSGleb Smirnoff 		int i;
817eb8dcdeaSGleb Smirnoff 
818eb8dcdeaSGleb Smirnoff 		i = 0; /* index in pip */
819eb8dcdeaSGleb Smirnoff 		ips = 0; /* index in new */
820eb8dcdeaSGleb Smirnoff 
821eb8dcdeaSGleb Smirnoff 		for (int pi = 0; pi < ppip->ips; pi++)
822eb8dcdeaSGleb Smirnoff 			if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) {
823eb8dcdeaSGleb Smirnoff 				/* Found our primary address in parent. */
824eb8dcdeaSGleb Smirnoff 				bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
825eb8dcdeaSGleb Smirnoff 				i++;
826eb8dcdeaSGleb Smirnoff 				ips++;
827eb8dcdeaSGleb Smirnoff 				break;
828eb8dcdeaSGleb Smirnoff 			}
829eb8dcdeaSGleb Smirnoff 		for (int pi = 1; i < pip->ips; ) {
830eb8dcdeaSGleb Smirnoff 			/* Check against primary, which is unsorted. */
831eb8dcdeaSGleb Smirnoff 			if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) {
832eb8dcdeaSGleb Smirnoff 				/* Matches parent's primary address. */
833eb8dcdeaSGleb Smirnoff 				bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
834eb8dcdeaSGleb Smirnoff 				i++;
835eb8dcdeaSGleb Smirnoff 				ips++;
836eb8dcdeaSGleb Smirnoff 				continue;
837eb8dcdeaSGleb Smirnoff 			}
838eb8dcdeaSGleb Smirnoff 			/* The rest are sorted. */
839eb8dcdeaSGleb Smirnoff 			switch (pi >= ppip->ips ? -1 :
840eb8dcdeaSGleb Smirnoff 				cmp(PR_IP(pip, i), PR_IP(ppip, pi))) {
841eb8dcdeaSGleb Smirnoff 			case -1:
842eb8dcdeaSGleb Smirnoff 				i++;
843eb8dcdeaSGleb Smirnoff 				break;
844eb8dcdeaSGleb Smirnoff 			case 0:
845eb8dcdeaSGleb Smirnoff 				bcopy(PR_IP(pr, i), PR_IPD(new, ips), size);
846eb8dcdeaSGleb Smirnoff 				i++;
847eb8dcdeaSGleb Smirnoff 				pi++;
848eb8dcdeaSGleb Smirnoff 				ips++;
849eb8dcdeaSGleb Smirnoff 				break;
850eb8dcdeaSGleb Smirnoff 			case 1:
851eb8dcdeaSGleb Smirnoff 				pi++;
852eb8dcdeaSGleb Smirnoff 				break;
853eb8dcdeaSGleb Smirnoff 			}
854eb8dcdeaSGleb Smirnoff 		}
855eb8dcdeaSGleb Smirnoff 		if (ips == 0) {
856eb8dcdeaSGleb Smirnoff 			if (alloced)
857eb8dcdeaSGleb Smirnoff 				prison_ip_free(new);
858eb8dcdeaSGleb Smirnoff 			new = NULL;
859eb8dcdeaSGleb Smirnoff 		}
860eb8dcdeaSGleb Smirnoff 	}
861eb8dcdeaSGleb Smirnoff 	prison_ip_set(pr, af, new);
862eb8dcdeaSGleb Smirnoff 	return (new != NULL ? true : false);
863eb8dcdeaSGleb Smirnoff }
864eb8dcdeaSGleb Smirnoff 
865eb8dcdeaSGleb Smirnoff /*
866eb8dcdeaSGleb Smirnoff  * Fast-path check if an address belongs to a prison.
867eb8dcdeaSGleb Smirnoff  */
868eb8dcdeaSGleb Smirnoff int
869eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af,
870eb8dcdeaSGleb Smirnoff     const void *addr)
871eb8dcdeaSGleb Smirnoff {
872eb8dcdeaSGleb Smirnoff 	int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
873eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip;
874eb8dcdeaSGleb Smirnoff 	int i, a, z, d;
875eb8dcdeaSGleb Smirnoff 
876eb8dcdeaSGleb Smirnoff 	MPASS(mtx_owned(&pr->pr_mtx) ||
877eb8dcdeaSGleb Smirnoff 	    in_epoch(net_epoch_preempt) ||
878eb8dcdeaSGleb Smirnoff 	    sx_xlocked(&allprison_lock));
879eb8dcdeaSGleb Smirnoff 
880eb8dcdeaSGleb Smirnoff 	pip = ck_pr_load_ptr(&pr->pr_addrs[af]);
881eb8dcdeaSGleb Smirnoff 	if (__predict_false(pip == NULL))
882eb8dcdeaSGleb Smirnoff 		return (EAFNOSUPPORT);
883eb8dcdeaSGleb Smirnoff 
884eb8dcdeaSGleb Smirnoff 	/* Check the primary IP. */
885eb8dcdeaSGleb Smirnoff 	if (cmp(PR_IP(pip, 0), addr) == 0)
886eb8dcdeaSGleb Smirnoff 		return (0);
887eb8dcdeaSGleb Smirnoff 
888eb8dcdeaSGleb Smirnoff 	/*
889eb8dcdeaSGleb Smirnoff 	 * All the other IPs are sorted so we can do a binary search.
890eb8dcdeaSGleb Smirnoff 	 */
891eb8dcdeaSGleb Smirnoff 	a = 0;
892eb8dcdeaSGleb Smirnoff 	z = pip->ips - 2;
893eb8dcdeaSGleb Smirnoff 	while (a <= z) {
894eb8dcdeaSGleb Smirnoff 		i = (a + z) / 2;
895eb8dcdeaSGleb Smirnoff 		d = cmp(PR_IP(pip, i + 1), addr);
896eb8dcdeaSGleb Smirnoff 		if (d > 0)
897eb8dcdeaSGleb Smirnoff 			z = i - 1;
898eb8dcdeaSGleb Smirnoff 		else if (d < 0)
899eb8dcdeaSGleb Smirnoff 			a = i + 1;
900eb8dcdeaSGleb Smirnoff 		else
901eb8dcdeaSGleb Smirnoff 			return (0);
902eb8dcdeaSGleb Smirnoff 	}
903eb8dcdeaSGleb Smirnoff 
904eb8dcdeaSGleb Smirnoff 	return (EADDRNOTAVAIL);
905eb8dcdeaSGleb Smirnoff }
906eb8dcdeaSGleb Smirnoff 
907eb8dcdeaSGleb Smirnoff /*
908eb8dcdeaSGleb Smirnoff  * Grab primary IP.  Historically required mutex, but nothing prevents
909eb8dcdeaSGleb Smirnoff  * us to support epoch-protected access.  Is it used in fast path?
910eb8dcdeaSGleb Smirnoff  * in{6}_jail.c helper
911eb8dcdeaSGleb Smirnoff  */
912eb8dcdeaSGleb Smirnoff const void *
913eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af)
914eb8dcdeaSGleb Smirnoff {
915eb8dcdeaSGleb Smirnoff 	const struct prison_ip *pip = pr->pr_addrs[af];
916eb8dcdeaSGleb Smirnoff 
917eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
918eb8dcdeaSGleb Smirnoff 	MPASS(pip);
919eb8dcdeaSGleb Smirnoff 
920eb8dcdeaSGleb Smirnoff 	return (pip + 1);
921eb8dcdeaSGleb Smirnoff }
922eb8dcdeaSGleb Smirnoff 
923eb8dcdeaSGleb Smirnoff u_int
924eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af)
925eb8dcdeaSGleb Smirnoff {
926eb8dcdeaSGleb Smirnoff 
927eb8dcdeaSGleb Smirnoff 	return (pr->pr_addrs[af]->ips);
928eb8dcdeaSGleb Smirnoff }
929eb8dcdeaSGleb Smirnoff #endif	/* defined(INET) || defined(INET6) */
930eb8dcdeaSGleb Smirnoff 
931413628a7SBjoern A. Zeeb int
932b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
933413628a7SBjoern A. Zeeb {
934fd7a8150SMike Barcroft 	struct nameidata nd;
935b38ff370SJamie Gritton #ifdef INET
936eb8dcdeaSGleb Smirnoff 	struct prison_ip *ip4;
937413628a7SBjoern A. Zeeb #endif
938b38ff370SJamie Gritton #ifdef INET6
939eb8dcdeaSGleb Smirnoff 	struct prison_ip *ip6;
940b38ff370SJamie Gritton #endif
941b38ff370SJamie Gritton 	struct vfsopt *opt;
942b38ff370SJamie Gritton 	struct vfsoptlist *opts;
9437de883c8SJamie Gritton 	struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
944b38ff370SJamie Gritton 	struct vnode *root;
945babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
946b96bd95bSIan Lepore 	char *g_path, *osrelstr;
947672756aaSJamie Gritton 	struct bool_flags *bf;
948672756aaSJamie Gritton 	struct jailsys_flags *jsf;
9490304c731SJamie Gritton #if defined(INET) || defined(INET6)
950b38ff370SJamie Gritton 	void *op;
9510304c731SJamie Gritton #endif
95276ca6f88SJamie Gritton 	unsigned long hid;
953b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
9542a4b2251SJamie Gritton 	int born, created, cuflags, descend, drflags, enforce;
955cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
9560cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
957672756aaSJamie Gritton 	int jid, jsys, len, level;
958b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
959413628a7SBjoern A. Zeeb #ifdef INET
9602b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
961413628a7SBjoern A. Zeeb #endif
962b38ff370SJamie Gritton #ifdef INET6
9632b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
964b38ff370SJamie Gritton #endif
9656beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
966b3079544SJamie Gritton 	uint64_t pr_allow_diff;
9676beb3bb4SKirk McKusick 	unsigned tallow;
968b38ff370SJamie Gritton 	char numbuf[12];
969b38ff370SJamie Gritton 
970b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
971b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
972b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
973b38ff370SJamie Gritton 	if (error)
97475c13541SPoul-Henning Kamp 		return (error);
975b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
976b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
9770304c731SJamie Gritton 		return (EPERM);
978b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
979b38ff370SJamie Gritton 		return (EINVAL);
980b38ff370SJamie Gritton 
981b38ff370SJamie Gritton 	/*
982b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
983b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
984b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
985b38ff370SJamie Gritton 	 * the allprison lock.
986b38ff370SJamie Gritton 	 *
987b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
988b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
989b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
990b38ff370SJamie Gritton 	 */
991b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
992b38ff370SJamie Gritton 	if (error)
993b38ff370SJamie Gritton 		return (error);
994b38ff370SJamie Gritton #ifdef INET
995b38ff370SJamie Gritton 	ip4 = NULL;
996b38ff370SJamie Gritton #endif
997b38ff370SJamie Gritton #ifdef INET6
998b38ff370SJamie Gritton 	ip6 = NULL;
999b38ff370SJamie Gritton #endif
10006dfe0a3dSMartin Matuska 	g_path = NULL;
1001b38ff370SJamie Gritton 
1002b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1003b6f47c23SJamie Gritton 	if (!cuflags) {
1004b6f47c23SJamie Gritton 		error = EINVAL;
1005b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
1006b6f47c23SJamie Gritton 		goto done_errmsg;
1007b6f47c23SJamie Gritton 	}
1008b6f47c23SJamie Gritton 
1009b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1010b38ff370SJamie Gritton 	if (error == ENOENT)
1011b38ff370SJamie Gritton 		jid = 0;
1012b38ff370SJamie Gritton 	else if (error != 0)
1013b38ff370SJamie Gritton 		goto done_free;
1014b38ff370SJamie Gritton 
1015b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1016b38ff370SJamie Gritton 	if (error == ENOENT)
1017b38ff370SJamie Gritton 		gotslevel = 0;
1018b38ff370SJamie Gritton 	else if (error != 0)
1019b38ff370SJamie Gritton 		goto done_free;
1020b38ff370SJamie Gritton 	else
1021b38ff370SJamie Gritton 		gotslevel = 1;
1022b38ff370SJamie Gritton 
1023b97457e2SJamie Gritton 	error =
1024b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1025b97457e2SJamie Gritton 	if (error == ENOENT)
1026b97457e2SJamie Gritton 		gotchildmax = 0;
1027b97457e2SJamie Gritton 	else if (error != 0)
1028b97457e2SJamie Gritton 		goto done_free;
1029b97457e2SJamie Gritton 	else
1030b97457e2SJamie Gritton 		gotchildmax = 1;
1031b97457e2SJamie Gritton 
10320304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1033f337198dSJamie Gritton 	if (error == ENOENT)
1034f337198dSJamie Gritton 		gotenforce = 0;
1035f337198dSJamie Gritton 	else if (error != 0)
10360304c731SJamie Gritton 		goto done_free;
1037f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
1038f337198dSJamie Gritton 		error = EINVAL;
1039f337198dSJamie Gritton 		goto done_free;
1040f337198dSJamie Gritton 	} else
1041f337198dSJamie Gritton 		gotenforce = 1;
10420304c731SJamie Gritton 
10430cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
10440cc207a6SMartin Matuska 	if (error == ENOENT)
10450cc207a6SMartin Matuska 		gotrsnum = 0;
10460cc207a6SMartin Matuska 	else if (error != 0)
10470cc207a6SMartin Matuska 		goto done_free;
10480cc207a6SMartin Matuska 	else
10490cc207a6SMartin Matuska 		gotrsnum = 1;
10500cc207a6SMartin Matuska 
1051b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
1052672756aaSJamie Gritton 	for (bf = pr_flag_bool;
1053672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
1054672756aaSJamie Gritton 	     bf++) {
1055672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1056672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
10570304c731SJamie Gritton 	}
1058b38ff370SJamie Gritton 	ch_flags |= pr_flags;
1059672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
1060672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1061672756aaSJamie Gritton 	     jsf++) {
1062672756aaSJamie Gritton 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
10637cbf7213SJamie Gritton 		if (error == ENOENT)
10647cbf7213SJamie Gritton 			continue;
10657cbf7213SJamie Gritton 		if (error != 0)
10667cbf7213SJamie Gritton 			goto done_free;
10677cbf7213SJamie Gritton 		switch (jsys) {
10687cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
1069672756aaSJamie Gritton 			if (!jsf->disable) {
10707cbf7213SJamie Gritton 				error = EINVAL;
10717cbf7213SJamie Gritton 				goto done_free;
10727cbf7213SJamie Gritton 			}
1073672756aaSJamie Gritton 			pr_flags |= jsf->disable;
10747cbf7213SJamie Gritton 			break;
10757cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
1076672756aaSJamie Gritton 			pr_flags |= jsf->new;
10777cbf7213SJamie Gritton 			break;
10787cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
10797cbf7213SJamie Gritton 			break;
10807cbf7213SJamie Gritton 		default:
10817cbf7213SJamie Gritton 			error = EINVAL;
10827cbf7213SJamie Gritton 			goto done_free;
10837cbf7213SJamie Gritton 		}
1084672756aaSJamie Gritton 		ch_flags |= jsf->new | jsf->disable;
10857cbf7213SJamie Gritton 	}
10861158508aSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
10874affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
10884affa14cSJamie Gritton 		error = EINVAL;
10894affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
10904affa14cSJamie Gritton 		goto done_errmsg;
10914affa14cSJamie Gritton 	}
1092679e1390SJamie Gritton #ifdef VIMAGE
1093679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1094679e1390SJamie Gritton 		error = EINVAL;
1095679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
1096679e1390SJamie Gritton 		goto done_errmsg;
1097679e1390SJamie Gritton 	}
1098679e1390SJamie Gritton #endif
10992b0d6f81SJamie Gritton #ifdef INET
11002b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
11012b0d6f81SJamie Gritton 		error = EINVAL;
11022b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
11032b0d6f81SJamie Gritton 		goto done_errmsg;
11042b0d6f81SJamie Gritton 	}
11052b0d6f81SJamie Gritton #endif
11062b0d6f81SJamie Gritton #ifdef INET6
11072b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
11082b0d6f81SJamie Gritton 		error = EINVAL;
11092b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
11102b0d6f81SJamie Gritton 		goto done_errmsg;
11112b0d6f81SJamie Gritton 	}
11122b0d6f81SJamie Gritton #endif
1113b38ff370SJamie Gritton 
11140304c731SJamie Gritton 	pr_allow = ch_allow = 0;
1115672756aaSJamie Gritton 	for (bf = pr_flag_allow;
11165d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
11175d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
1118672756aaSJamie Gritton 	     bf++) {
1119672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1120672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
11210304c731SJamie Gritton 	}
11220304c731SJamie Gritton 	ch_allow |= pr_allow;
11230304c731SJamie Gritton 
1124b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1125b38ff370SJamie Gritton 	if (error == ENOENT)
1126b38ff370SJamie Gritton 		name = NULL;
1127b38ff370SJamie Gritton 	else if (error != 0)
1128b38ff370SJamie Gritton 		goto done_free;
1129b38ff370SJamie Gritton 	else {
1130b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1131b38ff370SJamie Gritton 			error = EINVAL;
1132b38ff370SJamie Gritton 			goto done_free;
1133b38ff370SJamie Gritton 		}
1134b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
1135b38ff370SJamie Gritton 			error = ENAMETOOLONG;
1136b38ff370SJamie Gritton 			goto done_free;
1137b38ff370SJamie Gritton 		}
1138b38ff370SJamie Gritton 	}
1139b38ff370SJamie Gritton 
1140b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1141b38ff370SJamie Gritton 	if (error == ENOENT)
1142b38ff370SJamie Gritton 		host = NULL;
1143b38ff370SJamie Gritton 	else if (error != 0)
1144b38ff370SJamie Gritton 		goto done_free;
1145b38ff370SJamie Gritton 	else {
114676ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
114776ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
1148b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
1149b38ff370SJamie Gritton 			error = EINVAL;
1150b38ff370SJamie Gritton 			goto done_free;
1151b38ff370SJamie Gritton 		}
1152b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
1153b38ff370SJamie Gritton 			error = ENAMETOOLONG;
1154b38ff370SJamie Gritton 			goto done_free;
1155b38ff370SJamie Gritton 		}
1156b38ff370SJamie Gritton 	}
1157b38ff370SJamie Gritton 
115876ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
115976ca6f88SJamie Gritton 	if (error == ENOENT)
116076ca6f88SJamie Gritton 		domain = NULL;
116176ca6f88SJamie Gritton 	else if (error != 0)
116276ca6f88SJamie Gritton 		goto done_free;
116376ca6f88SJamie Gritton 	else {
116476ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
116576ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
116676ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
116776ca6f88SJamie Gritton 			error = EINVAL;
116876ca6f88SJamie Gritton 			goto done_free;
116976ca6f88SJamie Gritton 		}
117076ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
117176ca6f88SJamie Gritton 			error = ENAMETOOLONG;
117276ca6f88SJamie Gritton 			goto done_free;
117376ca6f88SJamie Gritton 		}
117476ca6f88SJamie Gritton 	}
117576ca6f88SJamie Gritton 
117676ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
117776ca6f88SJamie Gritton 	if (error == ENOENT)
117876ca6f88SJamie Gritton 		uuid = NULL;
117976ca6f88SJamie Gritton 	else if (error != 0)
118076ca6f88SJamie Gritton 		goto done_free;
118176ca6f88SJamie Gritton 	else {
118276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
118376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
118476ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
118576ca6f88SJamie Gritton 			error = EINVAL;
118676ca6f88SJamie Gritton 			goto done_free;
118776ca6f88SJamie Gritton 		}
118876ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
118976ca6f88SJamie Gritton 			error = ENAMETOOLONG;
119076ca6f88SJamie Gritton 			goto done_free;
119176ca6f88SJamie Gritton 		}
119276ca6f88SJamie Gritton 	}
119376ca6f88SJamie Gritton 
1194841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
1195a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
119676ca6f88SJamie Gritton 		uint32_t hid32;
119776ca6f88SJamie Gritton 
119876ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
119976ca6f88SJamie Gritton 		hid = hid32;
120076ca6f88SJamie Gritton 	} else
120176ca6f88SJamie Gritton #endif
120276ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
120376ca6f88SJamie Gritton 	if (error == ENOENT)
120476ca6f88SJamie Gritton 		gothid = 0;
120576ca6f88SJamie Gritton 	else if (error != 0)
120676ca6f88SJamie Gritton 		goto done_free;
120776ca6f88SJamie Gritton 	else {
120876ca6f88SJamie Gritton 		gothid = 1;
120976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
121076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
121176ca6f88SJamie Gritton 	}
121276ca6f88SJamie Gritton 
1213b38ff370SJamie Gritton #ifdef INET
1214b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1215b38ff370SJamie Gritton 	if (error == ENOENT)
12160e5e396eSJamie Gritton 		ip4s = 0;
1217b38ff370SJamie Gritton 	else if (error != 0)
1218b38ff370SJamie Gritton 		goto done_free;
1219eb8dcdeaSGleb Smirnoff 	else if (ip4s & (sizeof(struct in_addr) - 1)) {
1220b38ff370SJamie Gritton 		error = EINVAL;
1221b38ff370SJamie Gritton 		goto done_free;
12220304c731SJamie Gritton 	} else {
12236a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
12246a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
12256a3f2779SJamie Gritton 		if (ip4s > 0) {
1226eb8dcdeaSGleb Smirnoff 			ip4s /= sizeof(struct in_addr);
1227b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
1228b38ff370SJamie Gritton 				error = EINVAL;
1229b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
1230b38ff370SJamie Gritton 				goto done_errmsg;
1231b38ff370SJamie Gritton 			}
1232eb8dcdeaSGleb Smirnoff 			ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1233eb8dcdeaSGleb Smirnoff 			if (ip4 == NULL) {
1234b38ff370SJamie Gritton 				error = EINVAL;
1235b38ff370SJamie Gritton 				goto done_free;
1236b38ff370SJamie Gritton 			}
1237b38ff370SJamie Gritton 		}
12380304c731SJamie Gritton 	}
1239b38ff370SJamie Gritton #endif
1240b38ff370SJamie Gritton 
1241b38ff370SJamie Gritton #ifdef INET6
1242b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1243b38ff370SJamie Gritton 	if (error == ENOENT)
12440e5e396eSJamie Gritton 		ip6s = 0;
1245b38ff370SJamie Gritton 	else if (error != 0)
1246b38ff370SJamie Gritton 		goto done_free;
1247eb8dcdeaSGleb Smirnoff 	else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1248b38ff370SJamie Gritton 		error = EINVAL;
1249b38ff370SJamie Gritton 		goto done_free;
12500304c731SJamie Gritton 	} else {
12516a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
12526a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
12536a3f2779SJamie Gritton 		if (ip6s > 0) {
1254eb8dcdeaSGleb Smirnoff 			ip6s /= sizeof(struct in6_addr);
1255b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
1256b38ff370SJamie Gritton 				error = EINVAL;
1257b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
1258b38ff370SJamie Gritton 				goto done_errmsg;
1259b38ff370SJamie Gritton 			}
1260eb8dcdeaSGleb Smirnoff 			ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1261eb8dcdeaSGleb Smirnoff 			if (ip6 == NULL) {
1262b38ff370SJamie Gritton 				error = EINVAL;
1263b38ff370SJamie Gritton 				goto done_free;
1264b38ff370SJamie Gritton 			}
1265b38ff370SJamie Gritton 		}
12660304c731SJamie Gritton 	}
1267b38ff370SJamie Gritton #endif
1268b38ff370SJamie Gritton 
1269bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1270bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1271bdfc8cc4SJamie Gritton 		error = EINVAL;
1272bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
1273bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
1274bdfc8cc4SJamie Gritton 		goto done_errmsg;
1275bdfc8cc4SJamie Gritton 	}
1276bdfc8cc4SJamie Gritton #endif
1277bdfc8cc4SJamie Gritton 
1278cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1279cf0313c6SJamie Gritton 	if (error == ENOENT)
1280cf0313c6SJamie Gritton 		osrelstr = NULL;
1281cf0313c6SJamie Gritton 	else if (error != 0)
1282cf0313c6SJamie Gritton 		goto done_free;
1283cf0313c6SJamie Gritton 	else {
1284cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
1285cf0313c6SJamie Gritton 			error = EINVAL;
1286cf0313c6SJamie Gritton 			vfs_opterror(opts,
1287cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
1288cf0313c6SJamie Gritton 			goto done_errmsg;
1289cf0313c6SJamie Gritton 		}
12901b786d01SBjoern A. Zeeb 		if (len == 0 || osrelstr[len - 1] != '\0') {
1291cf0313c6SJamie Gritton 			error = EINVAL;
12921b786d01SBjoern A. Zeeb 			goto done_free;
12931b786d01SBjoern A. Zeeb 		}
12941b786d01SBjoern A. Zeeb 		if (len >= OSRELEASELEN) {
12951b786d01SBjoern A. Zeeb 			error = ENAMETOOLONG;
1296cf0313c6SJamie Gritton 			vfs_opterror(opts,
1297cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
1298cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
1299cf0313c6SJamie Gritton 			goto done_errmsg;
1300cf0313c6SJamie Gritton 		}
1301cf0313c6SJamie Gritton 	}
1302cf0313c6SJamie Gritton 
1303cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1304cf0313c6SJamie Gritton 	if (error == ENOENT)
1305cf0313c6SJamie Gritton 		osreldt = 0;
1306cf0313c6SJamie Gritton 	else if (error != 0)
1307cf0313c6SJamie Gritton 		goto done_free;
1308cf0313c6SJamie Gritton 	else {
1309cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
1310cf0313c6SJamie Gritton 			error = EINVAL;
1311cf0313c6SJamie Gritton 			vfs_opterror(opts,
1312cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
1313cf0313c6SJamie Gritton 			goto done_errmsg;
1314cf0313c6SJamie Gritton 		}
1315cf0313c6SJamie Gritton 		if (osreldt == 0) {
1316cf0313c6SJamie Gritton 			error = EINVAL;
1317cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
1318cf0313c6SJamie Gritton 			goto done_errmsg;
1319cf0313c6SJamie Gritton 		}
1320cf0313c6SJamie Gritton 	}
1321cf0313c6SJamie Gritton 
1322b38ff370SJamie Gritton 	root = NULL;
1323b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
1324b38ff370SJamie Gritton 	if (error == ENOENT)
1325b38ff370SJamie Gritton 		path = NULL;
1326b38ff370SJamie Gritton 	else if (error != 0)
1327b38ff370SJamie Gritton 		goto done_free;
1328b38ff370SJamie Gritton 	else {
1329b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
1330b38ff370SJamie Gritton 			error = EINVAL;
1331b38ff370SJamie Gritton 			vfs_opterror(opts,
1332b38ff370SJamie Gritton 			    "path cannot be changed after creation");
1333b38ff370SJamie Gritton 			goto done_errmsg;
1334b38ff370SJamie Gritton 		}
1335b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
1336b38ff370SJamie Gritton 			error = EINVAL;
1337b38ff370SJamie Gritton 			goto done_free;
1338b38ff370SJamie Gritton 		}
13397e1d3eefSMateusz Guzik 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1340b38ff370SJamie Gritton 		error = namei(&nd);
1341b38ff370SJamie Gritton 		if (error)
1342b38ff370SJamie Gritton 			goto done_free;
1343b38ff370SJamie Gritton 		root = nd.ni_vp;
1344bb92cd7bSMateusz Guzik 		NDFREE_PNBUF(&nd);
13456dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
13466dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
13476dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
13483eb6b656SMateusz Guzik 		if (error == 0) {
13496dfe0a3dSMartin Matuska 			path = g_path;
13506dfe0a3dSMartin Matuska 		} else {
1351f6e633a9SMartin Matuska 			/* exit on other errors */
1352b38ff370SJamie Gritton 			goto done_free;
1353b38ff370SJamie Gritton 		}
1354f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
1355f6e633a9SMartin Matuska 			error = ENOTDIR;
1356f6e633a9SMartin Matuska 			vput(root);
1357f6e633a9SMartin Matuska 			goto done_free;
1358f6e633a9SMartin Matuska 		}
1359b249ce48SMateusz Guzik 		VOP_UNLOCK(root);
1360b38ff370SJamie Gritton 	}
1361b38ff370SJamie Gritton 
1362b38ff370SJamie Gritton 	/*
1363b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
1364b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
1365b38ff370SJamie Gritton 	 */
1366b38ff370SJamie Gritton 	pr = NULL;
13677de883c8SJamie Gritton 	inspr = NULL;
1368babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1369babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1370babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1371babbbb9cSJamie Gritton 		if (*p != '\0')
1372babbbb9cSJamie Gritton 			jid = 0;
1373babbbb9cSJamie Gritton 	}
1374b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
1375108a9384SJamie Gritton 	drflags = PD_LIST_XLOCKED;
13760a2a96f3SJamie Gritton 	ppr = mypr;
13770a2a96f3SJamie Gritton 	if (!prison_isalive(ppr)) {
13780a2a96f3SJamie Gritton 		/* This jail is dying.  This process will surely follow. */
13790a2a96f3SJamie Gritton 		error = EAGAIN;
13800a2a96f3SJamie Gritton 		goto done_deref;
13810a2a96f3SJamie Gritton 	}
1382b38ff370SJamie Gritton 	if (jid != 0) {
1383b38ff370SJamie Gritton 		if (jid < 0) {
1384b38ff370SJamie Gritton 			error = EINVAL;
1385b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
13862a4b2251SJamie Gritton 			goto done_deref;
1387b38ff370SJamie Gritton 		}
13887de883c8SJamie Gritton 		/*
13897de883c8SJamie Gritton 		 * See if a requested jid already exists.  Keep track of
13907de883c8SJamie Gritton 		 * where it can be inserted later.
13917de883c8SJamie Gritton 		 */
13927de883c8SJamie Gritton 		TAILQ_FOREACH(inspr, &allprison, pr_list) {
1393f7496dcaSJamie Gritton 			if (inspr->pr_id < jid)
1394f7496dcaSJamie Gritton 				continue;
1395f7496dcaSJamie Gritton 			if (inspr->pr_id > jid)
1396f7496dcaSJamie Gritton 				break;
13977de883c8SJamie Gritton 			pr = inspr;
1398f7496dcaSJamie Gritton 			mtx_lock(&pr->pr_mtx);
13992a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
14007de883c8SJamie Gritton 			inspr = NULL;
14017de883c8SJamie Gritton 			break;
14027de883c8SJamie Gritton 		}
1403b38ff370SJamie Gritton 		if (pr != NULL) {
1404b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1405b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
14067de883c8SJamie Gritton 				/*
14077de883c8SJamie Gritton 				 * Even creators that cannot see the jail will
14087de883c8SJamie Gritton 				 * get EEXIST.
14097de883c8SJamie Gritton 				 */
1410b38ff370SJamie Gritton 				error = EEXIST;
1411b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1412b38ff370SJamie Gritton 				    jid);
14132a4b2251SJamie Gritton 				goto done_deref;
1414b38ff370SJamie Gritton 			}
14150304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
14167de883c8SJamie Gritton 				/*
14177de883c8SJamie Gritton 				 * Updaters get ENOENT if they cannot see the
14187de883c8SJamie Gritton 				 * jail.  This is true even for CREATE | UPDATE,
14197de883c8SJamie Gritton 				 * which normally cannot give this error.
14207de883c8SJamie Gritton 				 */
14212a4b2251SJamie Gritton 				error = ENOENT;
14222a4b2251SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
14232a4b2251SJamie Gritton 				goto done_deref;
1424f7496dcaSJamie Gritton 			}
14250a2a96f3SJamie Gritton 			ppr = pr->pr_parent;
14260a2a96f3SJamie Gritton 			if (!prison_isalive(ppr)) {
14270a2a96f3SJamie Gritton 				error = ENOENT;
14280a2a96f3SJamie Gritton 				vfs_opterror(opts, "jail %d is dying",
14290a2a96f3SJamie Gritton 				    ppr->pr_id);
14300a2a96f3SJamie Gritton 				goto done_deref;
14310a2a96f3SJamie Gritton 			}
1432f7496dcaSJamie Gritton 			if (!prison_isalive(pr)) {
1433b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1434b38ff370SJamie Gritton 					error = ENOENT;
1435b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1436b38ff370SJamie Gritton 					    jid);
14372a4b2251SJamie Gritton 					goto done_deref;
1438f7496dcaSJamie Gritton 				}
1439f7496dcaSJamie Gritton 				if ((flags & JAIL_ATTACH) ||
1440b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1441b38ff370SJamie Gritton 					/*
1442b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1443b38ff370SJamie Gritton 					 * (via attach or persist), but first
1444b38ff370SJamie Gritton 					 * it must determine if another jail
1445b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1446b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1447b38ff370SJamie Gritton 					 * name.
1448b38ff370SJamie Gritton 					 */
1449b38ff370SJamie Gritton 					if (name == NULL)
14500304c731SJamie Gritton 						name = prison_name(mypr, pr);
1451b38ff370SJamie Gritton 				}
1452b38ff370SJamie Gritton 			}
14532a4b2251SJamie Gritton 		} else {
1454b38ff370SJamie Gritton 			/* Update: jid must exist. */
1455b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1456b38ff370SJamie Gritton 				error = ENOENT;
1457b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
14582a4b2251SJamie Gritton 				goto done_deref;
1459b38ff370SJamie Gritton 			}
1460b38ff370SJamie Gritton 		}
1461b38ff370SJamie Gritton 	}
1462b38ff370SJamie Gritton 	/*
1463b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1464b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1465b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1466b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1467b38ff370SJamie Gritton 	 * because that is the jail being updated).
1468b38ff370SJamie Gritton 	 */
1469b6f47c23SJamie Gritton 	namelc = NULL;
1470b38ff370SJamie Gritton 	if (name != NULL) {
1471babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1472babbbb9cSJamie Gritton 		if (namelc == NULL)
1473babbbb9cSJamie Gritton 			namelc = name;
1474babbbb9cSJamie Gritton 		else {
14750304c731SJamie Gritton 			/*
14760304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
14770304c731SJamie Gritton 			 * parent and child names, and make sure the parent
14780304c731SJamie Gritton 			 * exists or matches an already found jail.
14790304c731SJamie Gritton 			 */
14800304c731SJamie Gritton 			if (pr != NULL) {
1481babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1482babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
14830304c731SJamie Gritton 					error = EINVAL;
14840304c731SJamie Gritton 					vfs_opterror(opts,
14850304c731SJamie Gritton 					    "cannot change jail's parent");
14862a4b2251SJamie Gritton 					goto done_deref;
14870304c731SJamie Gritton 				}
14880304c731SJamie Gritton 			} else {
1489b6f47c23SJamie Gritton 				*namelc = '\0';
14900304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
14910304c731SJamie Gritton 				if (ppr == NULL) {
14920304c731SJamie Gritton 					error = ENOENT;
14930304c731SJamie Gritton 					vfs_opterror(opts,
14940304c731SJamie Gritton 					    "jail \"%s\" not found", name);
14952a4b2251SJamie Gritton 					goto done_deref;
14960304c731SJamie Gritton 				}
1497c050ea80SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
14980a2a96f3SJamie Gritton 				if (!prison_isalive(ppr)) {
1499c050ea80SJamie Gritton 					error = ENOENT;
1500c050ea80SJamie Gritton 					vfs_opterror(opts,
1501c050ea80SJamie Gritton 					    "jail \"%s\" is dying", name);
1502c050ea80SJamie Gritton 					goto done_deref;
1503c050ea80SJamie Gritton 				}
1504b6f47c23SJamie Gritton 				*namelc = '.';
15050304c731SJamie Gritton 			}
1506b6f47c23SJamie Gritton 			namelc++;
15070304c731SJamie Gritton 		}
1508b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1509b6f47c23SJamie Gritton 			pnamelen =
15100304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
15110304c731SJamie Gritton 			deadpr = NULL;
15120304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
15136754ae25SJamie Gritton 				if (tpr != pr &&
1514b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
151576ad42abSJamie Gritton 					if (prison_isalive(tpr)) {
1516b38ff370SJamie Gritton 						if (pr == NULL &&
1517b38ff370SJamie Gritton 						    cuflags != JAIL_CREATE) {
1518b38ff370SJamie Gritton 							/*
1519b38ff370SJamie Gritton 							 * Use this jail
1520b38ff370SJamie Gritton 							 * for updates.
1521b38ff370SJamie Gritton 							 */
1522b38ff370SJamie Gritton 							pr = tpr;
1523f7496dcaSJamie Gritton 							mtx_lock(&pr->pr_mtx);
152483bc72a0SJamie Gritton 							drflags |= PD_LOCKED;
1525b38ff370SJamie Gritton 							break;
1526b38ff370SJamie Gritton 						}
1527b38ff370SJamie Gritton 						/*
1528b38ff370SJamie Gritton 						 * Create, or update(jid):
1529b38ff370SJamie Gritton 						 * name must not exist in an
15300304c731SJamie Gritton 						 * active sibling jail.
1531b38ff370SJamie Gritton 						 */
1532b38ff370SJamie Gritton 						error = EEXIST;
1533b38ff370SJamie Gritton 						vfs_opterror(opts,
1534b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1535b38ff370SJamie Gritton 						   name);
15362a4b2251SJamie Gritton 						goto done_deref;
1537b38ff370SJamie Gritton 					}
153876ad42abSJamie Gritton 					if (pr == NULL &&
1539f7496dcaSJamie Gritton 					    cuflags != JAIL_CREATE) {
154076ad42abSJamie Gritton 						deadpr = tpr;
1541f7496dcaSJamie Gritton 					}
1542b38ff370SJamie Gritton 				}
1543b38ff370SJamie Gritton 			}
1544b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1545b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1546b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1547b38ff370SJamie Gritton 					pr = deadpr;
1548f7496dcaSJamie Gritton 					mtx_lock(&pr->pr_mtx);
15492a4b2251SJamie Gritton 					drflags |= PD_LOCKED;
1550b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1551b38ff370SJamie Gritton 					error = ENOENT;
1552b38ff370SJamie Gritton 					vfs_opterror(opts,
1553b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
15542a4b2251SJamie Gritton 					goto done_deref;
1555b38ff370SJamie Gritton 				}
1556b38ff370SJamie Gritton 			}
1557b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1558b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1559b38ff370SJamie Gritton 				error = ENOENT;
1560b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1561b38ff370SJamie Gritton 				    name);
15622a4b2251SJamie Gritton 				goto done_deref;
1563b38ff370SJamie Gritton 			}
1564b38ff370SJamie Gritton 		}
1565b38ff370SJamie Gritton 	}
1566b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1567b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1568b38ff370SJamie Gritton 		error = ENOENT;
1569b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
15702a4b2251SJamie Gritton 		goto done_deref;
1571b38ff370SJamie Gritton 	}
1572b38ff370SJamie Gritton 
1573b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
15742a4b2251SJamie Gritton 	created = pr == NULL;
15752a4b2251SJamie Gritton 	if (created) {
1576b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1577b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1578b97457e2SJamie Gritton 				error = EPERM;
1579b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
15802a4b2251SJamie Gritton 				goto done_deref;
1581b97457e2SJamie Gritton 			}
15827de883c8SJamie Gritton 		if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1583b38ff370SJamie Gritton 			error = EAGAIN;
15847de883c8SJamie Gritton 			vfs_opterror(opts, "no available jail IDs");
15852a4b2251SJamie Gritton 			goto done_deref;
1586b38ff370SJamie Gritton 		}
15872a4b2251SJamie Gritton 
15882a4b2251SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
15891158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_INVALID;
15901158508aSJamie Gritton 		refcount_init(&pr->pr_ref, 1);
1591f7496dcaSJamie Gritton 		refcount_init(&pr->pr_uref, 0);
15921158508aSJamie Gritton 		drflags |= PD_DEREF;
15932a4b2251SJamie Gritton 		LIST_INIT(&pr->pr_children);
15942a4b2251SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
15952a4b2251SJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
15962a4b2251SJamie Gritton 
15977de883c8SJamie Gritton 		pr->pr_id = jid;
15987de883c8SJamie Gritton 		if (inspr != NULL)
15997de883c8SJamie Gritton 			TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
16007de883c8SJamie Gritton 		else
1601b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
16027de883c8SJamie Gritton 
16037de883c8SJamie Gritton 		pr->pr_parent = ppr;
16040a2a96f3SJamie Gritton 		prison_hold(ppr);
16050a2a96f3SJamie Gritton 		prison_proc_hold(ppr);
16060304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
16070304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1608b97457e2SJamie Gritton 			tpr->pr_childcount++;
1609b38ff370SJamie Gritton 
16100304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1611b6f47c23SJamie Gritton 		if (namelc == NULL)
1612b6f47c23SJamie Gritton 			namelc = "";
1613b38ff370SJamie Gritton 		if (path == NULL) {
1614b38ff370SJamie Gritton 			path = "/";
16150304c731SJamie Gritton 			root = mypr->pr_root;
1616b38ff370SJamie Gritton 			vref(root);
1617b38ff370SJamie Gritton 		}
16188986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
16198986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1620bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1621bdfc8cc4SJamie Gritton #ifdef VIMAGE
1622bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1623bdfc8cc4SJamie Gritton #endif
1624bdfc8cc4SJamie Gritton 		{
16250304c731SJamie Gritton #ifdef INET
16262b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
16276a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
16282b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
16292b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1630eb8dcdeaSGleb Smirnoff 				prison_ip_dup(ppr, pr, PR_INET);
16312b0d6f81SJamie Gritton 			}
16320304c731SJamie Gritton #endif
16330304c731SJamie Gritton #ifdef INET6
16342b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
16356a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
16362b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
16372b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1638eb8dcdeaSGleb Smirnoff 				prison_ip_dup(ppr, pr, PR_INET6);
16392b0d6f81SJamie Gritton 			}
16400304c731SJamie Gritton #endif
1641bdfc8cc4SJamie Gritton 		}
1642bdfc8cc4SJamie Gritton #endif
1643592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1644592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1645592bcae8SBjoern A. Zeeb 
16460304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
16470304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1648af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1649bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1650b38ff370SJamie Gritton 
1651b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1652b96bd95bSIan Lepore 		if (osrelstr == NULL)
16531b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
16541b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1655b96bd95bSIan Lepore 		else
16561b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, osrelstr,
16571b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1658b96bd95bSIan Lepore 
1659679e1390SJamie Gritton #ifdef VIMAGE
1660679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1661679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1662679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1663679e1390SJamie Gritton #endif
1664b38ff370SJamie Gritton 		/*
1665b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
16668771ff75SGordon Bergling 		 * Unlike other initial settings, this may return an error.
1667b38ff370SJamie Gritton 		 */
16680304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
16692a4b2251SJamie Gritton 		if (error)
16702a4b2251SJamie Gritton 			goto done_deref;
1671b38ff370SJamie Gritton 
1672b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16732a4b2251SJamie Gritton 		drflags |= PD_LOCKED;
1674b38ff370SJamie Gritton 	} else {
16752b0d6f81SJamie Gritton 		/*
16762b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
16772b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
16782b0d6f81SJamie Gritton 		 */
16796754ae25SJamie Gritton 		prison_hold(pr);
16802a4b2251SJamie Gritton 		drflags |= PD_DEREF;
1681bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1682bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1683bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1684bdfc8cc4SJamie Gritton 			error = EINVAL;
1685bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1686bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
16872a4b2251SJamie Gritton 			goto done_deref;
1688bdfc8cc4SJamie Gritton 		}
1689bdfc8cc4SJamie Gritton #endif
16902b0d6f81SJamie Gritton #ifdef INET
16912b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
16922b0d6f81SJamie Gritton 			error = EINVAL;
16932b0d6f81SJamie Gritton 			vfs_opterror(opts,
16942b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
16952a4b2251SJamie Gritton 			goto done_deref;
16962b0d6f81SJamie Gritton 		}
16972b0d6f81SJamie Gritton #endif
16982b0d6f81SJamie Gritton #ifdef INET6
16992b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
17002b0d6f81SJamie Gritton 			error = EINVAL;
17012b0d6f81SJamie Gritton 			vfs_opterror(opts,
17022b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
17032a4b2251SJamie Gritton 			goto done_deref;
17042b0d6f81SJamie Gritton 		}
17052b0d6f81SJamie Gritton #endif
1706b38ff370SJamie Gritton 	}
1707b38ff370SJamie Gritton 
1708b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
17090304c731SJamie Gritton 	if (gotslevel) {
17100304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
17110304c731SJamie Gritton 			error = EPERM;
17122a4b2251SJamie Gritton 			goto done_deref;
17130304c731SJamie Gritton 		}
17140304c731SJamie Gritton 	}
1715b97457e2SJamie Gritton 	if (gotchildmax) {
1716b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1717b97457e2SJamie Gritton 			error = EPERM;
17182a4b2251SJamie Gritton 			goto done_deref;
1719b97457e2SJamie Gritton 		}
1720b97457e2SJamie Gritton 	}
17210304c731SJamie Gritton 	if (gotenforce) {
17220304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
17230304c731SJamie Gritton 			error = EPERM;
17242a4b2251SJamie Gritton 			goto done_deref;
17250304c731SJamie Gritton 		}
17260304c731SJamie Gritton 	}
17270cc207a6SMartin Matuska 	if (gotrsnum) {
17280cc207a6SMartin Matuska 		/*
17290cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
17300cc207a6SMartin Matuska 		 */
1731bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
17320cc207a6SMartin Matuska 			error = EINVAL;
17332a4b2251SJamie Gritton 			goto done_deref;
17340cc207a6SMartin Matuska 		}
17350cc207a6SMartin Matuska 		/*
1736bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
17370cc207a6SMartin Matuska 		 */
17380cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
17390cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
17400cc207a6SMartin Matuska 				error = EPERM;
17412a4b2251SJamie Gritton 				goto done_deref;
1742bf3db8aaSMartin Matuska 			} else
17430cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
17440cc207a6SMartin Matuska 		}
17450cc207a6SMartin Matuska 	}
1746b38ff370SJamie Gritton #ifdef INET
17472b0d6f81SJamie Gritton 	if (ip4s > 0) {
1748eb8dcdeaSGleb Smirnoff 		if ((ppr->pr_flags & PR_IP4) &&
1749eb8dcdeaSGleb Smirnoff 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1750eb8dcdeaSGleb Smirnoff 		    PR_INET)) {
17510304c731SJamie Gritton 			error = EPERM;
17522a4b2251SJamie Gritton 			goto done_deref;
17530304c731SJamie Gritton 		}
1754eb8dcdeaSGleb Smirnoff 		if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
17550304c731SJamie Gritton 			error = EADDRINUSE;
1756eb8dcdeaSGleb Smirnoff 			vfs_opterror(opts, "IPv4 addresses clash");
17572a4b2251SJamie Gritton 			goto done_deref;
1758b38ff370SJamie Gritton 		}
17590304c731SJamie Gritton 	}
1760b38ff370SJamie Gritton #endif
1761b38ff370SJamie Gritton #ifdef INET6
17622b0d6f81SJamie Gritton 	if (ip6s > 0) {
1763eb8dcdeaSGleb Smirnoff 		if ((ppr->pr_flags & PR_IP6) &&
1764eb8dcdeaSGleb Smirnoff 		    !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1765eb8dcdeaSGleb Smirnoff 		    PR_INET6)) {
17660304c731SJamie Gritton 			error = EPERM;
17672a4b2251SJamie Gritton 			goto done_deref;
17680304c731SJamie Gritton 		}
1769eb8dcdeaSGleb Smirnoff 		if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
17700304c731SJamie Gritton 			error = EADDRINUSE;
1771eb8dcdeaSGleb Smirnoff 			vfs_opterror(opts, "IPv6 addresses clash");
17722a4b2251SJamie Gritton 			goto done_deref;
1773b38ff370SJamie Gritton 		}
17740304c731SJamie Gritton 	}
1775b38ff370SJamie Gritton #endif
17760304c731SJamie Gritton 	onamelen = namelen = 0;
1777b6f47c23SJamie Gritton 	if (namelc != NULL) {
17786ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
17796ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
17806ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
17816ab6058eSJamie Gritton 		 */
1782b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1783b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
17846ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
17856ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1786b38ff370SJamie Gritton 			error = EINVAL;
1787babbbb9cSJamie Gritton 			vfs_opterror(opts,
1788babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
17892a4b2251SJamie Gritton 			goto done_deref;
1790b38ff370SJamie Gritton 		}
1791b38ff370SJamie Gritton 		/*
17920304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
17930304c731SJamie Gritton 		 * children.
1794b38ff370SJamie Gritton 		 */
1795b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1796b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1797b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1798b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
17990304c731SJamie Gritton 			error = ENAMETOOLONG;
18002a4b2251SJamie Gritton 			goto done_deref;
18010304c731SJamie Gritton 		}
18020304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
18030304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
18040304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
18050304c731SJamie Gritton 				error = ENAMETOOLONG;
18062a4b2251SJamie Gritton 				goto done_deref;
18070304c731SJamie Gritton 			}
18080304c731SJamie Gritton 		}
18090304c731SJamie Gritton 	}
1810b3079544SJamie Gritton 	pr_allow_diff = pr_allow & ~ppr->pr_allow;
1811b3079544SJamie Gritton 	if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
18120304c731SJamie Gritton 		error = EPERM;
18132a4b2251SJamie Gritton 		goto done_deref;
1814b38ff370SJamie Gritton 	}
1815b38ff370SJamie Gritton 
1816b6f47c23SJamie Gritton 	/*
1817b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1818b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1819b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1820b6f47c23SJamie Gritton 	 */
1821b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
18222a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1823b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
18242a4b2251SJamie Gritton 	if (error != 0)
18252a4b2251SJamie Gritton 		goto done_deref;
1826b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
18272a4b2251SJamie Gritton 	drflags |= PD_LOCKED;
1828b6f47c23SJamie Gritton 
1829b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1830b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1831b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1832b6f47c23SJamie Gritton 			error = EINVAL;
1833b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
18342a4b2251SJamie Gritton 			goto done_deref;
1835b6f47c23SJamie Gritton 		}
1836b6f47c23SJamie Gritton 	}
1837b6f47c23SJamie Gritton 
1838b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1839b38ff370SJamie Gritton #ifdef INET
18400304c731SJamie Gritton 	redo_ip4 = 0;
18410304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
18420304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1843eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, PR_INET, ip4);
1844b38ff370SJamie Gritton 		ip4 = NULL;
18450304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1846bdfc8cc4SJamie Gritton #ifdef VIMAGE
1847bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1848bdfc8cc4SJamie Gritton 				descend = 0;
1849bdfc8cc4SJamie Gritton 				continue;
1850bdfc8cc4SJamie Gritton 			}
1851bdfc8cc4SJamie Gritton #endif
1852eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET, NULL)) {
18530304c731SJamie Gritton 				redo_ip4 = 1;
18540304c731SJamie Gritton 				descend = 0;
18550304c731SJamie Gritton 			}
18560304c731SJamie Gritton 		}
18570304c731SJamie Gritton 	}
1858b38ff370SJamie Gritton #endif
1859b38ff370SJamie Gritton #ifdef INET6
18600304c731SJamie Gritton 	redo_ip6 = 0;
18610304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
18620304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1863eb8dcdeaSGleb Smirnoff 		prison_ip_set(pr, PR_INET6, ip6);
1864b38ff370SJamie Gritton 		ip6 = NULL;
18650304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1866bdfc8cc4SJamie Gritton #ifdef VIMAGE
1867bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1868bdfc8cc4SJamie Gritton 				descend = 0;
1869bdfc8cc4SJamie Gritton 				continue;
1870bdfc8cc4SJamie Gritton 			}
1871bdfc8cc4SJamie Gritton #endif
1872eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET6, NULL)) {
18730304c731SJamie Gritton 				redo_ip6 = 1;
18740304c731SJamie Gritton 				descend = 0;
18750304c731SJamie Gritton 			}
18760304c731SJamie Gritton 		}
18770304c731SJamie Gritton 	}
1878b38ff370SJamie Gritton #endif
18790304c731SJamie Gritton 	if (gotslevel) {
1880b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
18810304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
18820304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
18830304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
18840304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
18850304c731SJamie Gritton 	}
1886b97457e2SJamie Gritton 	if (gotchildmax) {
1887b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1888b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1889b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1890b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1891b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1892b97457e2SJamie Gritton 				    ? childmax - level : 0;
1893b97457e2SJamie Gritton 	}
18940304c731SJamie Gritton 	if (gotenforce) {
18950304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
18960304c731SJamie Gritton 		/* Pass this restriction on to the children. */
18970304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
18980304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
18990304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
19000304c731SJamie Gritton 	}
19010cc207a6SMartin Matuska 	if (gotrsnum) {
19020cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
19030cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
19040cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
19050cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
19060cc207a6SMartin Matuska 	}
1907b6f47c23SJamie Gritton 	if (namelc != NULL) {
19080304c731SJamie Gritton 		if (ppr == &prison0)
1909b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
19100304c731SJamie Gritton 		else
19110304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1912b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
19130304c731SJamie Gritton 		/* Change this component of child names. */
19140304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
19150304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
19160304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
19170304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
19180304c731SJamie Gritton 		}
19190304c731SJamie Gritton 	}
1920b38ff370SJamie Gritton 	if (path != NULL) {
19210304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1922b38ff370SJamie Gritton 		strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1923b38ff370SJamie Gritton 		pr->pr_root = root;
19242a4b2251SJamie Gritton 		root = NULL;
1925b38ff370SJamie Gritton 	}
192676ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
192776ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
192876ca6f88SJamie Gritton 			/*
192976ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
193076ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
193176ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
193276ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
193376ca6f88SJamie Gritton 			 */
1934c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1935c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1936c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1937c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1938c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1939c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
194076ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
194176ca6f88SJamie Gritton 		}
194276ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
194376ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1944b38ff370SJamie Gritton 		if (host != NULL)
1945c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
194676ca6f88SJamie Gritton 		if (domain != NULL)
1947c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1948c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
194976ca6f88SJamie Gritton 		if (uuid != NULL)
1950c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
195176ca6f88SJamie Gritton 		if (gothid)
195276ca6f88SJamie Gritton 			pr->pr_hostid = hid;
195376ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
195476ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
195576ca6f88SJamie Gritton 				descend = 0;
195676ca6f88SJamie Gritton 			else {
195776ca6f88SJamie Gritton 				if (host != NULL)
1958c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1959c1f19219SJamie Gritton 					    pr->pr_hostname,
1960c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
196176ca6f88SJamie Gritton 				if (domain != NULL)
1962c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1963c1f19219SJamie Gritton 					    pr->pr_domainname,
1964c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
196576ca6f88SJamie Gritton 				if (uuid != NULL)
1966c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1967c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1968c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
196976ca6f88SJamie Gritton 				if (gothid)
197076ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
197176ca6f88SJamie Gritton 			}
197276ca6f88SJamie Gritton 		}
197376ca6f88SJamie Gritton 	}
19740304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
19750fe74ae6SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow))
19760fe74ae6SJamie Gritton 		prison_set_allow_locked(pr, tallow, 0);
1977b38ff370SJamie Gritton 	/*
1978b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
19791158508aSJamie Gritton 	 * persist flag lose that reference.
1980b38ff370SJamie Gritton 	 */
198176ad42abSJamie Gritton 	born = !prison_isalive(pr);
19821158508aSJamie Gritton 	if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1983b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
19846754ae25SJamie Gritton 			prison_hold(pr);
19851158508aSJamie Gritton 			/*
19861158508aSJamie Gritton 			 * This may make a dead prison alive again, but wait
19871158508aSJamie Gritton 			 * to label it as such until after OSD calls have had
19881158508aSJamie Gritton 			 * a chance to run (and perhaps to fail).
19891158508aSJamie Gritton 			 */
19906754ae25SJamie Gritton 			refcount_acquire(&pr->pr_uref);
1991b38ff370SJamie Gritton 		} else {
199239c8ef90SJamie Gritton 			drflags |= PD_DEUREF;
1993f7496dcaSJamie Gritton 			prison_free_not_last(pr);
1994b38ff370SJamie Gritton 		}
1995b38ff370SJamie Gritton 	}
1996b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1997b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
19982a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1999c861373bSJamie Gritton 	/*
2000c861373bSJamie Gritton 	 * Any errors past this point will need to de-persist newly created
2001c861373bSJamie Gritton 	 * prisons, as well as call remove methods.
2002c861373bSJamie Gritton 	 */
2003c861373bSJamie Gritton 	if (born)
2004c861373bSJamie Gritton 		drflags |= PD_KILL;
2005b38ff370SJamie Gritton 
2006a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
20074b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
2008a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
2009a7ad07bfSEdward Tomasz Napierala #endif
2010a7ad07bfSEdward Tomasz Napierala 
20110304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
20120304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
20130304c731SJamie Gritton 	 */
20140304c731SJamie Gritton #ifdef INET
20150304c731SJamie Gritton 	while (redo_ip4) {
2016eb8dcdeaSGleb Smirnoff 		ip4s = pr->pr_addrs[PR_INET]->ips;
2017eb8dcdeaSGleb Smirnoff 		ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
20180304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20190304c731SJamie Gritton 		redo_ip4 = 0;
20200304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2021bdfc8cc4SJamie Gritton #ifdef VIMAGE
2022bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
2023bdfc8cc4SJamie Gritton 				descend = 0;
2024bdfc8cc4SJamie Gritton 				continue;
2025bdfc8cc4SJamie Gritton 			}
2026bdfc8cc4SJamie Gritton #endif
2027eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET, ip4)) {
20280304c731SJamie Gritton 				if (ip4 != NULL)
20290304c731SJamie Gritton 					ip4 = NULL;
20300304c731SJamie Gritton 				else
20310304c731SJamie Gritton 					redo_ip4 = 1;
20320304c731SJamie Gritton 			}
20330304c731SJamie Gritton 		}
20340304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20350304c731SJamie Gritton 	}
20360304c731SJamie Gritton #endif
20370304c731SJamie Gritton #ifdef INET6
20380304c731SJamie Gritton 	while (redo_ip6) {
2039eb8dcdeaSGleb Smirnoff 		ip6s = pr->pr_addrs[PR_INET6]->ips;
2040eb8dcdeaSGleb Smirnoff 		ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
20410304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20420304c731SJamie Gritton 		redo_ip6 = 0;
20430304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2044bdfc8cc4SJamie Gritton #ifdef VIMAGE
2045bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
2046bdfc8cc4SJamie Gritton 				descend = 0;
2047bdfc8cc4SJamie Gritton 				continue;
2048bdfc8cc4SJamie Gritton 			}
2049bdfc8cc4SJamie Gritton #endif
2050eb8dcdeaSGleb Smirnoff 			if (prison_ip_restrict(tpr, PR_INET6, ip6)) {
20510304c731SJamie Gritton 				if (ip6 != NULL)
20520304c731SJamie Gritton 					ip6 = NULL;
20530304c731SJamie Gritton 				else
20540304c731SJamie Gritton 					redo_ip6 = 1;
20550304c731SJamie Gritton 			}
20560304c731SJamie Gritton 		}
20570304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20580304c731SJamie Gritton 	}
20590304c731SJamie Gritton #endif
20600304c731SJamie Gritton 
2061b38ff370SJamie Gritton 	/* Let the modules do their work. */
2062cc5fd8c7SJamie Gritton 	if (born) {
2063b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2064c861373bSJamie Gritton 		if (error)
20652a4b2251SJamie Gritton 			goto done_deref;
2066b38ff370SJamie Gritton 	}
2067b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
2068c861373bSJamie Gritton 	if (error)
20692a4b2251SJamie Gritton 		goto done_deref;
2070b38ff370SJamie Gritton 
20711158508aSJamie Gritton 	/*
20721158508aSJamie Gritton 	 * A new prison is now ready to be seen; either it has gained a user
20731158508aSJamie Gritton 	 * reference via persistence, or is about to gain one via attachment.
20741158508aSJamie Gritton 	 */
20751158508aSJamie Gritton 	if (born) {
20761158508aSJamie Gritton 		drflags = prison_lock_xlock(pr, drflags);
20771158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_ALIVE;
20781158508aSJamie Gritton 	}
20791158508aSJamie Gritton 
2080b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
2081b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
2082c861373bSJamie Gritton 		error = do_jail_attach(td, pr,
2083589e4c1dSJamie Gritton 		    prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
2084f7496dcaSJamie Gritton 		drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2085b38ff370SJamie Gritton 		if (error) {
2086b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
20872a4b2251SJamie Gritton 			goto done_deref;
2088b38ff370SJamie Gritton 		}
2089b38ff370SJamie Gritton 	}
2090b38ff370SJamie Gritton 
20911fb24974SEdward Tomasz Napierala #ifdef RACCT
20924b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
2093701d6b50SJamie Gritton 		if (drflags & PD_LOCKED) {
2094701d6b50SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2095701d6b50SJamie Gritton 			drflags &= ~PD_LOCKED;
2096701d6b50SJamie Gritton 		}
2097f7496dcaSJamie Gritton 		if (drflags & PD_LIST_XLOCKED) {
2098f7496dcaSJamie Gritton 			sx_xunlock(&allprison_lock);
2099f7496dcaSJamie Gritton 			drflags &= ~PD_LIST_XLOCKED;
2100b4e87a63SJamie Gritton 		}
21011fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
21021fb24974SEdward Tomasz Napierala 	}
21031fb24974SEdward Tomasz Napierala #endif
21041fb24974SEdward Tomasz Napierala 
2105c861373bSJamie Gritton 	drflags &= ~PD_KILL;
21061fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
21071fb24974SEdward Tomasz Napierala 
21082a4b2251SJamie Gritton  done_deref:
21092a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
21102a4b2251SJamie Gritton 	if (pr != NULL)
21112a4b2251SJamie Gritton 		prison_deref(pr, drflags);
21122a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2113b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
21142a4b2251SJamie Gritton 	else if (drflags & PD_LIST_XLOCKED)
2115b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
21165050aa86SKonstantin Belousov 	if (root != NULL)
2117b38ff370SJamie Gritton 		vrele(root);
2118b38ff370SJamie Gritton  done_errmsg:
2119b38ff370SJamie Gritton 	if (error) {
21202a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2121176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2122176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
2123b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2124b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2125b38ff370SJamie Gritton 				bcopy(errmsg,
2126b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2127b38ff370SJamie Gritton 				    errmsg_len);
2128b38ff370SJamie Gritton 			else
2129b38ff370SJamie Gritton 				copyout(errmsg,
2130b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2131b38ff370SJamie Gritton 				    errmsg_len);
2132b38ff370SJamie Gritton 		}
2133b38ff370SJamie Gritton 	}
2134b38ff370SJamie Gritton  done_free:
2135b38ff370SJamie Gritton #ifdef INET
2136eb8dcdeaSGleb Smirnoff 	prison_ip_free(ip4);
2137b38ff370SJamie Gritton #endif
2138b38ff370SJamie Gritton #ifdef INET6
2139eb8dcdeaSGleb Smirnoff 	prison_ip_free(ip6);
2140b38ff370SJamie Gritton #endif
21416dfe0a3dSMartin Matuska 	if (g_path != NULL)
21426dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
2143b38ff370SJamie Gritton 	vfs_freeopts(opts);
2144b38ff370SJamie Gritton 	return (error);
2145b38ff370SJamie Gritton }
2146b38ff370SJamie Gritton 
2147b38ff370SJamie Gritton /*
21487de883c8SJamie Gritton  * Find the next available prison ID.  Return the ID on success, or zero
21497de883c8SJamie Gritton  * on failure.  Also set a pointer to the allprison list entry the prison
21507de883c8SJamie Gritton  * should be inserted before.
21517de883c8SJamie Gritton  */
21527de883c8SJamie Gritton static int
21537de883c8SJamie Gritton get_next_prid(struct prison **insprp)
21547de883c8SJamie Gritton {
21557de883c8SJamie Gritton 	struct prison *inspr;
21567de883c8SJamie Gritton 	int jid, maxid;
21577de883c8SJamie Gritton 
21587de883c8SJamie Gritton 	jid = lastprid % JAIL_MAX + 1;
21597de883c8SJamie Gritton 	if (TAILQ_EMPTY(&allprison) ||
21607de883c8SJamie Gritton 	    TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
21617de883c8SJamie Gritton 		/*
21627de883c8SJamie Gritton 		 * A common case is for all jails to be implicitly numbered,
21637de883c8SJamie Gritton 		 * which means they'll go on the end of the list, at least
21647de883c8SJamie Gritton 		 * for the first JAIL_MAX times.
21657de883c8SJamie Gritton 		 */
21667de883c8SJamie Gritton 		inspr = NULL;
21677de883c8SJamie Gritton 	} else {
21687de883c8SJamie Gritton 		/*
21697de883c8SJamie Gritton 		 * Take two passes through the allprison list: first starting
21707de883c8SJamie Gritton 		 * with the proposed jid, then ending with it.
21717de883c8SJamie Gritton 		 */
21727de883c8SJamie Gritton 		for (maxid = JAIL_MAX; maxid != 0; ) {
21737de883c8SJamie Gritton 			TAILQ_FOREACH(inspr, &allprison, pr_list) {
21747de883c8SJamie Gritton 				if (inspr->pr_id < jid)
21757de883c8SJamie Gritton 					continue;
2176f7496dcaSJamie Gritton 				if (inspr->pr_id > jid) {
2177f7496dcaSJamie Gritton 					/* Found an opening. */
21787de883c8SJamie Gritton 					maxid = 0;
21797de883c8SJamie Gritton 					break;
21807de883c8SJamie Gritton 				}
21817de883c8SJamie Gritton 				if (++jid > maxid) {
21827de883c8SJamie Gritton 					if (lastprid == maxid || lastprid == 0)
21837de883c8SJamie Gritton 					{
21847de883c8SJamie Gritton 						/*
21857de883c8SJamie Gritton 						 * The entire legal range
21867de883c8SJamie Gritton 						 * has been traversed
21877de883c8SJamie Gritton 						 */
21887de883c8SJamie Gritton 						return 0;
21897de883c8SJamie Gritton 					}
21907de883c8SJamie Gritton 					/* Try again from the start. */
21917de883c8SJamie Gritton 					jid = 1;
21927de883c8SJamie Gritton 					maxid = lastprid;
21937de883c8SJamie Gritton 					break;
21947de883c8SJamie Gritton 				}
21957de883c8SJamie Gritton 			}
21967de883c8SJamie Gritton 			if (inspr == NULL) {
21977de883c8SJamie Gritton 				/* Found room at the end of the list. */
21987de883c8SJamie Gritton 				break;
21997de883c8SJamie Gritton 			}
22007de883c8SJamie Gritton 		}
22017de883c8SJamie Gritton 	}
22027de883c8SJamie Gritton 	*insprp = inspr;
22037de883c8SJamie Gritton 	lastprid = jid;
22047de883c8SJamie Gritton 	return (jid);
22057de883c8SJamie Gritton }
22067de883c8SJamie Gritton 
22077de883c8SJamie Gritton /*
2208b38ff370SJamie Gritton  * struct jail_get_args {
2209b38ff370SJamie Gritton  *	struct iovec *iovp;
2210b38ff370SJamie Gritton  *	unsigned int iovcnt;
2211b38ff370SJamie Gritton  *	int flags;
2212b38ff370SJamie Gritton  * };
2213b38ff370SJamie Gritton  */
2214b38ff370SJamie Gritton int
22158451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
2216b38ff370SJamie Gritton {
2217b38ff370SJamie Gritton 	struct uio *auio;
2218b38ff370SJamie Gritton 	int error;
2219b38ff370SJamie Gritton 
2220b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
2221b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
2222b38ff370SJamie Gritton 		return (EINVAL);
2223b38ff370SJamie Gritton 
2224b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2225b38ff370SJamie Gritton 	if (error)
2226b38ff370SJamie Gritton 		return (error);
2227b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
2228b38ff370SJamie Gritton 	if (error == 0)
2229b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
2230b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
2231b38ff370SJamie Gritton 	free(auio, M_IOV);
2232b38ff370SJamie Gritton 	return (error);
2233b38ff370SJamie Gritton }
2234b38ff370SJamie Gritton 
2235b38ff370SJamie Gritton int
2236b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2237b38ff370SJamie Gritton {
2238672756aaSJamie Gritton 	struct bool_flags *bf;
2239672756aaSJamie Gritton 	struct jailsys_flags *jsf;
22400304c731SJamie Gritton 	struct prison *pr, *mypr;
2241b38ff370SJamie Gritton 	struct vfsopt *opt;
2242b38ff370SJamie Gritton 	struct vfsoptlist *opts;
2243b38ff370SJamie Gritton 	char *errmsg, *name;
22442a4b2251SJamie Gritton 	int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2245672756aaSJamie Gritton 	unsigned f;
2246b38ff370SJamie Gritton 
2247b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
2248b38ff370SJamie Gritton 		return (EINVAL);
2249b38ff370SJamie Gritton 
2250b38ff370SJamie Gritton 	/* Get the parameter list. */
2251b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
2252b38ff370SJamie Gritton 	if (error)
2253b38ff370SJamie Gritton 		return (error);
2254b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
22550304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
22562a4b2251SJamie Gritton 	pr = NULL;
22571e2a13e6SJamie Gritton 
2258b38ff370SJamie Gritton 	/*
2259b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
2260b38ff370SJamie Gritton 	 */
2261b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
22622a4b2251SJamie Gritton 	drflags = PD_LIST_SLOCKED;
2263b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2264b38ff370SJamie Gritton 	if (error == 0) {
2265b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
2266f7496dcaSJamie Gritton 			if (pr->pr_id > jid &&
2267f7496dcaSJamie Gritton 			    ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2268f7496dcaSJamie Gritton 			    prison_ischild(mypr, pr)) {
2269b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
22702a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
2271b38ff370SJamie Gritton 				goto found_prison;
22722a4b2251SJamie Gritton 			}
2273f7496dcaSJamie Gritton 		}
2274b38ff370SJamie Gritton 		error = ENOENT;
2275b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
22762a4b2251SJamie Gritton 		goto done;
2277b38ff370SJamie Gritton 	} else if (error != ENOENT)
22782a4b2251SJamie Gritton 		goto done;
2279b38ff370SJamie Gritton 
2280b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2281b38ff370SJamie Gritton 	if (error == 0) {
2282b38ff370SJamie Gritton 		if (jid != 0) {
22830304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
2284b38ff370SJamie Gritton 			if (pr != NULL) {
22852a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
228676ad42abSJamie Gritton 				if (!(prison_isalive(pr) ||
228776ad42abSJamie Gritton 				    (flags & JAIL_DYING))) {
2288b38ff370SJamie Gritton 					error = ENOENT;
2289b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
2290b38ff370SJamie Gritton 					    jid);
22912a4b2251SJamie Gritton 					goto done;
2292b38ff370SJamie Gritton 				}
2293b38ff370SJamie Gritton 				goto found_prison;
2294b38ff370SJamie Gritton 			}
2295b38ff370SJamie Gritton 			error = ENOENT;
2296b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
22972a4b2251SJamie Gritton 			goto done;
2298b38ff370SJamie Gritton 		}
2299b38ff370SJamie Gritton 	} else if (error != ENOENT)
23002a4b2251SJamie Gritton 		goto done;
2301b38ff370SJamie Gritton 
2302b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2303b38ff370SJamie Gritton 	if (error == 0) {
2304b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2305b38ff370SJamie Gritton 			error = EINVAL;
23062a4b2251SJamie Gritton 			goto done;
2307b38ff370SJamie Gritton 		}
23080304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2309b38ff370SJamie Gritton 		if (pr != NULL) {
23102a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
231176ad42abSJamie Gritton 			if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2312b38ff370SJamie Gritton 				error = ENOENT;
2313b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2314b38ff370SJamie Gritton 				    name);
23152a4b2251SJamie Gritton 				goto done;
2316b38ff370SJamie Gritton 			}
2317b38ff370SJamie Gritton 			goto found_prison;
2318b38ff370SJamie Gritton 		}
2319b38ff370SJamie Gritton 		error = ENOENT;
2320b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
23212a4b2251SJamie Gritton 		goto done;
2322b38ff370SJamie Gritton 	} else if (error != ENOENT)
23232a4b2251SJamie Gritton 		goto done;
2324b38ff370SJamie Gritton 
2325b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2326b38ff370SJamie Gritton 	error = ENOENT;
23272a4b2251SJamie Gritton 	goto done;
2328b38ff370SJamie Gritton 
2329b38ff370SJamie Gritton  found_prison:
2330b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
23316754ae25SJamie Gritton 	prison_hold(pr);
23322a4b2251SJamie Gritton 	drflags |= PD_DEREF;
2333b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2334b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2335b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23362a4b2251SJamie Gritton 		goto done;
23370304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
23380304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2339b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23402a4b2251SJamie Gritton 		goto done;
23410304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
23420304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
23432a4b2251SJamie Gritton 		goto done;
23440304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2345b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2346b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23472a4b2251SJamie Gritton 		goto done;
23480304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2349b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23502a4b2251SJamie Gritton 		goto done;
2351b38ff370SJamie Gritton #ifdef INET
2352eb8dcdeaSGleb Smirnoff 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1,
2353eb8dcdeaSGleb Smirnoff 	    pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2354eb8dcdeaSGleb Smirnoff 	    pr_families[PR_INET].size : 0 );
2355b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23562a4b2251SJamie Gritton 		goto done;
2357b38ff370SJamie Gritton #endif
2358b38ff370SJamie Gritton #ifdef INET6
2359eb8dcdeaSGleb Smirnoff 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1,
2360eb8dcdeaSGleb Smirnoff 	    pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2361eb8dcdeaSGleb Smirnoff 	    pr_families[PR_INET6].size : 0 );
2362b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23632a4b2251SJamie Gritton 		goto done;
2364b38ff370SJamie Gritton #endif
2365b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2366b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2367b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23682a4b2251SJamie Gritton 		goto done;
2369b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2370b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2371b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
23722a4b2251SJamie Gritton 		goto done;
2373b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2374b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2375b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
23762a4b2251SJamie Gritton 		goto done;
2377c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2378b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
23792a4b2251SJamie Gritton 		goto done;
2380c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
238176ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
23822a4b2251SJamie Gritton 		goto done;
2383c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
238476ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
23852a4b2251SJamie Gritton 		goto done;
2386841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2387a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
238876ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
238976ca6f88SJamie Gritton 
239076ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
239176ca6f88SJamie Gritton 	} else
239276ca6f88SJamie Gritton #endif
239376ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
239476ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
239576ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
23962a4b2251SJamie Gritton 		goto done;
23970304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
23980304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
23990304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
24002a4b2251SJamie Gritton 		goto done;
24010cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
24020cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
24030cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
24042a4b2251SJamie Gritton 		goto done;
2405672756aaSJamie Gritton 	for (bf = pr_flag_bool;
2406672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2407672756aaSJamie Gritton 	     bf++) {
2408672756aaSJamie Gritton 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2409672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2410b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
24112a4b2251SJamie Gritton 			goto done;
2412b38ff370SJamie Gritton 		i = !i;
2413672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2414b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
24152a4b2251SJamie Gritton 			goto done;
24160304c731SJamie Gritton 	}
2417672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
2418672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2419672756aaSJamie Gritton 	     jsf++) {
2420672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
2421672756aaSJamie Gritton 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2422672756aaSJamie Gritton 		    : (f == jsf->new) ? JAIL_SYS_NEW
24237cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
2424672756aaSJamie Gritton 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
24257cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
24262a4b2251SJamie Gritton 			goto done;
24277cbf7213SJamie Gritton 	}
2428672756aaSJamie Gritton 	for (bf = pr_flag_allow;
24295d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
24305d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
2431672756aaSJamie Gritton 	     bf++) {
2432672756aaSJamie Gritton 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2433672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
24340304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
24352a4b2251SJamie Gritton 			goto done;
24360304c731SJamie Gritton 		i = !i;
2437672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
24380304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
24392a4b2251SJamie Gritton 			goto done;
24400304c731SJamie Gritton 	}
244176ad42abSJamie Gritton 	i = !prison_isalive(pr);
2442b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2443b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24442a4b2251SJamie Gritton 		goto done;
2445b38ff370SJamie Gritton 	i = !i;
2446b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2447b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
24482a4b2251SJamie Gritton 		goto done;
2449bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2450bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2451a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
24522a4b2251SJamie Gritton 		goto done;
2453a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2454a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
24552a4b2251SJamie Gritton 		goto done;
2456b38ff370SJamie Gritton 
2457b38ff370SJamie Gritton 	/* Get the module parameters. */
2458b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
24592a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
2460b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2461b38ff370SJamie Gritton 	if (error)
24622a4b2251SJamie Gritton 		goto done;
24632a4b2251SJamie Gritton 	prison_deref(pr, drflags);
24642a4b2251SJamie Gritton 	pr = NULL;
24652a4b2251SJamie Gritton 	drflags = 0;
2466b38ff370SJamie Gritton 
2467b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2468b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2469b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2470b38ff370SJamie Gritton 			error = EINVAL;
2471b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
24722a4b2251SJamie Gritton 			goto done;
2473b38ff370SJamie Gritton 		}
2474b38ff370SJamie Gritton 	}
2475b38ff370SJamie Gritton 
2476b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2477b38ff370SJamie Gritton 	error = 0;
2478b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2479b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2480b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2481b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2482b38ff370SJamie Gritton 			if (opt->value != NULL) {
2483b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2484b38ff370SJamie Gritton 					bcopy(opt->value,
2485b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2486b38ff370SJamie Gritton 					    opt->len);
2487b38ff370SJamie Gritton 				} else {
2488b38ff370SJamie Gritton 					error = copyout(opt->value,
2489b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2490b38ff370SJamie Gritton 					    opt->len);
2491b38ff370SJamie Gritton 					if (error)
2492b38ff370SJamie Gritton 						break;
2493b38ff370SJamie Gritton 				}
2494b38ff370SJamie Gritton 			}
2495b38ff370SJamie Gritton 		}
2496b38ff370SJamie Gritton 	}
2497b38ff370SJamie Gritton 
24982a4b2251SJamie Gritton  done:
24992a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
25002a4b2251SJamie Gritton 	if (pr != NULL)
25012a4b2251SJamie Gritton 		prison_deref(pr, drflags);
25022a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2503b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2504b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
25052a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2506b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2507b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2508b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2509b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2510b38ff370SJamie Gritton 				bcopy(errmsg,
2511b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2512b38ff370SJamie Gritton 				    errmsg_len);
2513b38ff370SJamie Gritton 			else
2514b38ff370SJamie Gritton 				copyout(errmsg,
2515b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2516b38ff370SJamie Gritton 				    errmsg_len);
2517b38ff370SJamie Gritton 		}
2518b38ff370SJamie Gritton 	}
2519b38ff370SJamie Gritton 	vfs_freeopts(opts);
2520b38ff370SJamie Gritton 	return (error);
2521b38ff370SJamie Gritton }
2522b38ff370SJamie Gritton 
2523b38ff370SJamie Gritton /*
2524b38ff370SJamie Gritton  * struct jail_remove_args {
2525b38ff370SJamie Gritton  *	int jid;
2526b38ff370SJamie Gritton  * };
2527b38ff370SJamie Gritton  */
2528b38ff370SJamie Gritton int
25298451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2530b38ff370SJamie Gritton {
2531c861373bSJamie Gritton 	struct prison *pr;
2532c861373bSJamie Gritton 	int error;
2533b38ff370SJamie Gritton 
2534b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2535b38ff370SJamie Gritton 	if (error)
2536b38ff370SJamie Gritton 		return (error);
2537b38ff370SJamie Gritton 
2538b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
25390304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2540b38ff370SJamie Gritton 	if (pr == NULL) {
2541b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2542b38ff370SJamie Gritton 		return (EINVAL);
2543b38ff370SJamie Gritton 	}
2544c861373bSJamie Gritton 	if (!prison_isalive(pr)) {
2545c861373bSJamie Gritton 		/* Silently ignore already-dying prisons. */
25460304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2547c861373bSJamie Gritton 		sx_xunlock(&allprison_lock);
25480304c731SJamie Gritton 		return (0);
25490304c731SJamie Gritton 	}
2550c861373bSJamie Gritton 	prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2551c861373bSJamie Gritton 	return (0);
255275c13541SPoul-Henning Kamp }
255375c13541SPoul-Henning Kamp 
2554fd7a8150SMike Barcroft /*
25559ddb7954SMike Barcroft  * struct jail_attach_args {
25569ddb7954SMike Barcroft  *	int jid;
25579ddb7954SMike Barcroft  * };
2558fd7a8150SMike Barcroft  */
2559fd7a8150SMike Barcroft int
25608451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2561fd7a8150SMike Barcroft {
2562b38ff370SJamie Gritton 	struct prison *pr;
2563b38ff370SJamie Gritton 	int error;
2564b38ff370SJamie Gritton 
2565b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2566b38ff370SJamie Gritton 	if (error)
2567b38ff370SJamie Gritton 		return (error);
2568b38ff370SJamie Gritton 
2569f7496dcaSJamie Gritton 	sx_slock(&allprison_lock);
25700304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2571b38ff370SJamie Gritton 	if (pr == NULL) {
2572b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2573b38ff370SJamie Gritton 		return (EINVAL);
2574b38ff370SJamie Gritton 	}
2575b38ff370SJamie Gritton 
257676ad42abSJamie Gritton 	/* Do not allow a process to attach to a prison that is not alive. */
257776ad42abSJamie Gritton 	if (!prison_isalive(pr)) {
2578b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2579b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2580b38ff370SJamie Gritton 		return (EINVAL);
2581b38ff370SJamie Gritton 	}
2582b38ff370SJamie Gritton 
2583f7496dcaSJamie Gritton 	return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2584b38ff370SJamie Gritton }
2585b38ff370SJamie Gritton 
2586b38ff370SJamie Gritton static int
2587f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2588b38ff370SJamie Gritton {
2589fd7a8150SMike Barcroft 	struct proc *p;
2590fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
25915050aa86SKonstantin Belousov 	int error;
2592fd7a8150SMike Barcroft 
2593f7496dcaSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2594f7496dcaSJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
2595589e4c1dSJamie Gritton 	drflags &= PD_LOCK_FLAGS;
259657f22bd4SJacques Vidrine 	/*
259757f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
259857f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
259957f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
260057f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
260157f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
260257f22bd4SJacques Vidrine 	 * of another.
260357f22bd4SJacques Vidrine 	 */
26041158508aSJamie Gritton 	prison_hold(pr);
26056754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
2606f7496dcaSJamie Gritton 	drflags |= PD_DEREF | PD_DEUREF;
2607fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2608f7496dcaSJamie Gritton 	drflags &= ~PD_LOCKED;
2609b38ff370SJamie Gritton 
2610b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2611b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2612b38ff370SJamie Gritton 	if (error) {
2613f7496dcaSJamie Gritton 		prison_deref(pr, drflags);
2614b38ff370SJamie Gritton 		return (error);
2615b38ff370SJamie Gritton 	}
2616f7496dcaSJamie Gritton 	sx_unlock(&allprison_lock);
2617f7496dcaSJamie Gritton 	drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2618fd7a8150SMike Barcroft 
2619413628a7SBjoern A. Zeeb 	/*
2620413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2621413628a7SBjoern A. Zeeb 	 */
2622b38ff370SJamie Gritton 	p = td->td_proc;
2623413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2624413628a7SBjoern A. Zeeb 	if (error)
2625b38ff370SJamie Gritton 		goto e_revert_osd;
2626413628a7SBjoern A. Zeeb 
2627cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2628fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2629fd7a8150SMike Barcroft 		goto e_unlock;
2630fd7a8150SMike Barcroft #ifdef MAC
263130d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2632fd7a8150SMike Barcroft 		goto e_unlock;
2633fd7a8150SMike Barcroft #endif
2634b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2635d4380c0cSJamie Gritton 	if ((error = pwd_chroot_chdir(td, pr->pr_root)))
26365050aa86SKonstantin Belousov 		goto e_revert_osd;
2637fd7a8150SMike Barcroft 
2638fd7a8150SMike Barcroft 	newcred = crget();
2639fd7a8150SMike Barcroft 	PROC_LOCK(p);
26401fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
264169c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2642daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
26431fb6767dSJamie Gritton 	setsugid(p);
2644097055e2SEdward Tomasz Napierala #ifdef RACCT
2645097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2646f87beb93SAndriy Gapon 	crhold(newcred);
2647f87beb93SAndriy Gapon #endif
2648f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
2649f87beb93SAndriy Gapon #ifdef RCTL
2650f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
2651f87beb93SAndriy Gapon 	crfree(newcred);
2652097055e2SEdward Tomasz Napierala #endif
2653*5ecb5444SMateusz Guzik 	prison_proc_relink(oldcred->cr_prison, pr, p);
2654f7496dcaSJamie Gritton 	prison_deref(oldcred->cr_prison, drflags);
2655fd7a8150SMike Barcroft 	crfree(oldcred);
2656cc7b7306SJamie Gritton 
2657cc7b7306SJamie Gritton 	/*
2658cc7b7306SJamie Gritton 	 * If the prison was killed while changing credentials, die along
2659cc7b7306SJamie Gritton 	 * with it.
2660cc7b7306SJamie Gritton 	 */
2661cc7b7306SJamie Gritton 	if (!prison_isalive(pr)) {
2662cc7b7306SJamie Gritton 		PROC_LOCK(p);
2663cc7b7306SJamie Gritton 		kern_psignal(p, SIGKILL);
2664cc7b7306SJamie Gritton 		PROC_UNLOCK(p);
2665cc7b7306SJamie Gritton 	}
2666cc7b7306SJamie Gritton 
2667fd7a8150SMike Barcroft 	return (0);
26681fb6767dSJamie Gritton 
2669fd7a8150SMike Barcroft  e_unlock:
2670b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2671b38ff370SJamie Gritton  e_revert_osd:
2672b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
26737f4e7248SJamie Gritton 	sx_slock(&allprison_lock);
2674f7496dcaSJamie Gritton 	drflags |= PD_LIST_SLOCKED;
26751fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2676f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2677fd7a8150SMike Barcroft 	return (error);
2678fd7a8150SMike Barcroft }
2679fd7a8150SMike Barcroft 
2680fd7a8150SMike Barcroft /*
2681fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2682fd7a8150SMike Barcroft  */
268354b369c1SPawel Jakub Dawidek struct prison *
2684fd7a8150SMike Barcroft prison_find(int prid)
2685fd7a8150SMike Barcroft {
2686fd7a8150SMike Barcroft 	struct prison *pr;
2687fd7a8150SMike Barcroft 
2688dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2689b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2690f7496dcaSJamie Gritton 		if (pr->pr_id < prid)
2691f7496dcaSJamie Gritton 			continue;
26927de883c8SJamie Gritton 		if (pr->pr_id > prid)
26937de883c8SJamie Gritton 			break;
2694f7496dcaSJamie Gritton 		KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2695f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
2696f7496dcaSJamie Gritton 		return (pr);
2697fd7a8150SMike Barcroft 	}
2698fd7a8150SMike Barcroft 	return (NULL);
2699fd7a8150SMike Barcroft }
2700fd7a8150SMike Barcroft 
2701b38ff370SJamie Gritton /*
27020304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2703b38ff370SJamie Gritton  */
2704b38ff370SJamie Gritton struct prison *
27050304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2706b38ff370SJamie Gritton {
27070304c731SJamie Gritton 	struct prison *pr;
27080304c731SJamie Gritton 	int descend;
2709b38ff370SJamie Gritton 
2710b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
27110304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
27120304c731SJamie Gritton 		if (pr->pr_id == prid) {
2713f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2714f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
27150304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27160304c731SJamie Gritton 			return (pr);
27170304c731SJamie Gritton 		}
27180304c731SJamie Gritton 	}
27190304c731SJamie Gritton 	return (NULL);
27200304c731SJamie Gritton }
27210304c731SJamie Gritton 
27220304c731SJamie Gritton /*
27230304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
27240304c731SJamie Gritton  */
27250304c731SJamie Gritton struct prison *
27260304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
27270304c731SJamie Gritton {
27280304c731SJamie Gritton 	struct prison *pr, *deadpr;
27290304c731SJamie Gritton 	size_t mylen;
27300304c731SJamie Gritton 	int descend;
27310304c731SJamie Gritton 
27320304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
27330304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2734b38ff370SJamie Gritton 	deadpr = NULL;
27350304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
27360304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2737f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2738f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
2739f7496dcaSJamie Gritton 			if (prison_isalive(pr)) {
2740b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
2741b38ff370SJamie Gritton 				return (pr);
2742f7496dcaSJamie Gritton 			}
2743b38ff370SJamie Gritton 			deadpr = pr;
2744b38ff370SJamie Gritton 		}
2745b38ff370SJamie Gritton 	}
27460304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2747f7496dcaSJamie Gritton 	if (deadpr != NULL)
2748b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2749b38ff370SJamie Gritton 	return (deadpr);
2750b38ff370SJamie Gritton }
2751b38ff370SJamie Gritton 
2752b38ff370SJamie Gritton /*
27530fe74ae6SJamie Gritton  * See if a prison has the specific flag set.  The prison should be locked,
27540fe74ae6SJamie Gritton  * unless checking for flags that are only set at jail creation (such as
27550fe74ae6SJamie Gritton  * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
27560fe74ae6SJamie Gritton  * to any other prison data.
27570304c731SJamie Gritton  */
27580304c731SJamie Gritton int
27590304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
27600304c731SJamie Gritton {
27610304c731SJamie Gritton 
27620304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
27630304c731SJamie Gritton }
27640304c731SJamie Gritton 
27650304c731SJamie Gritton int
27660304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
27670304c731SJamie Gritton {
27680304c731SJamie Gritton 
27690fe74ae6SJamie Gritton 	return ((cred->cr_prison->pr_allow & flag) != 0);
27700304c731SJamie Gritton }
27710304c731SJamie Gritton 
27720304c731SJamie Gritton /*
2773effad35eSJamie Gritton  * Hold a prison reference, by incrementing pr_ref.  It is generally
2774effad35eSJamie Gritton  * an error to hold a prison that does not already have a reference.
2775effad35eSJamie Gritton  * A prison record will remain valid as long as it has at least one
2776effad35eSJamie Gritton  * reference, and will not be removed as long as either the prison
2777effad35eSJamie Gritton  * mutex or the allprison lock is held (allprison_lock may be shared).
2778effad35eSJamie Gritton  */
2779effad35eSJamie Gritton void
2780effad35eSJamie Gritton prison_hold_locked(struct prison *pr)
2781effad35eSJamie Gritton {
2782effad35eSJamie Gritton 
27836754ae25SJamie Gritton 	/* Locking is no longer required. */
27846754ae25SJamie Gritton 	prison_hold(pr);
2785effad35eSJamie Gritton }
2786effad35eSJamie Gritton 
2787effad35eSJamie Gritton void
2788effad35eSJamie Gritton prison_hold(struct prison *pr)
2789effad35eSJamie Gritton {
27906754ae25SJamie Gritton #ifdef INVARIANTS
27916754ae25SJamie Gritton 	int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2792effad35eSJamie Gritton 
27936754ae25SJamie Gritton 	KASSERT(was_valid,
27946754ae25SJamie Gritton 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
27956754ae25SJamie Gritton #else
27966754ae25SJamie Gritton 	refcount_acquire(&pr->pr_ref);
27976754ae25SJamie Gritton #endif
2798effad35eSJamie Gritton }
2799effad35eSJamie Gritton 
2800effad35eSJamie Gritton /*
2801effad35eSJamie Gritton  * Remove a prison reference.  If that was the last reference, the
2802f7496dcaSJamie Gritton  * prison will be removed (at a later time).
2803b38ff370SJamie Gritton  */
280491421ba2SRobert Watson void
28051ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
280691421ba2SRobert Watson {
280791421ba2SRobert Watson 
28081ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2809effad35eSJamie Gritton 	/*
2810f7496dcaSJamie Gritton 	 * Locking is no longer required, but unlock because the caller
2811f7496dcaSJamie Gritton 	 * expects it.
2812effad35eSJamie Gritton 	 */
2813f7496dcaSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2814f7496dcaSJamie Gritton 	prison_free(pr);
2815effad35eSJamie Gritton }
2816c2cda609SPawel Jakub Dawidek 
28171ba4a712SPawel Jakub Dawidek void
28181ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
28191ba4a712SPawel Jakub Dawidek {
28201ba4a712SPawel Jakub Dawidek 
28216754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
28226754ae25SJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
28236754ae25SJamie Gritton 	     pr, pr->pr_id));
2824f7496dcaSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_ref)) {
2825f7496dcaSJamie Gritton 		/*
2826f7496dcaSJamie Gritton 		 * Don't remove the last reference in this context,
2827f7496dcaSJamie Gritton 		 * in case there are locks held.
2828f7496dcaSJamie Gritton 		 */
2829f7496dcaSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2830f7496dcaSJamie Gritton 	}
2831f7496dcaSJamie Gritton }
2832f7496dcaSJamie Gritton 
2833f7496dcaSJamie Gritton static void
2834f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr)
2835f7496dcaSJamie Gritton {
2836f7496dcaSJamie Gritton #ifdef INVARIANTS
2837f7496dcaSJamie Gritton 	int lastref;
2838f7496dcaSJamie Gritton 
2839f7496dcaSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
2840f7496dcaSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2841f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2842f7496dcaSJamie Gritton 	lastref = refcount_release(&pr->pr_ref);
2843f7496dcaSJamie Gritton 	KASSERT(!lastref,
2844f7496dcaSJamie Gritton 	    ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2845f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2846f7496dcaSJamie Gritton #else
2847ee9b37aeSMateusz Guzik 	refcount_release(&pr->pr_ref);
2848f7496dcaSJamie Gritton #endif
28491ba4a712SPawel Jakub Dawidek }
28501ba4a712SPawel Jakub Dawidek 
285173d9e52dSJamie Gritton /*
2852f171938cSGordon Bergling  * Hold a prison for user visibility, by incrementing pr_uref.
2853effad35eSJamie Gritton  * It is generally an error to hold a prison that isn't already
285449a033d8SGordon Bergling  * user-visible, except through the jail system calls.  It is also
2855effad35eSJamie Gritton  * an error to hold an invalid prison.  A prison record will remain
2856effad35eSJamie Gritton  * alive as long as it has at least one user reference, and will not
2857f7496dcaSJamie Gritton  * be set to the dying state until the prison mutex and allprison_lock
2858f7496dcaSJamie Gritton  * are both freed.
2859effad35eSJamie Gritton  */
2860effad35eSJamie Gritton void
2861effad35eSJamie Gritton prison_proc_hold(struct prison *pr)
2862effad35eSJamie Gritton {
28636754ae25SJamie Gritton #ifdef INVARIANTS
28646754ae25SJamie Gritton 	int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2865effad35eSJamie Gritton 
28666754ae25SJamie Gritton 	KASSERT(was_alive,
2867effad35eSJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
28686754ae25SJamie Gritton #else
28696754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
28706754ae25SJamie Gritton #endif
2871effad35eSJamie Gritton }
2872effad35eSJamie Gritton 
2873effad35eSJamie Gritton /*
2874effad35eSJamie Gritton  * Remove a prison user reference.  If it was the last reference, the
2875effad35eSJamie Gritton  * prison will be considered "dying", and may be removed once all of
2876effad35eSJamie Gritton  * its references are dropped.
2877effad35eSJamie Gritton  */
2878effad35eSJamie Gritton void
2879effad35eSJamie Gritton prison_proc_free(struct prison *pr)
2880effad35eSJamie Gritton {
2881effad35eSJamie Gritton 
28826754ae25SJamie Gritton 	/*
28836754ae25SJamie Gritton 	 * Locking is only required when releasing the last reference.
28846754ae25SJamie Gritton 	 * This allows assurance that a locked prison will remain alive
28856754ae25SJamie Gritton 	 * until it is unlocked.
28866754ae25SJamie Gritton 	 */
28876754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2888effad35eSJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2889195cd6aeSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_uref)) {
2890effad35eSJamie Gritton 		/*
2891effad35eSJamie Gritton 		 * Don't remove the last user reference in this context,
2892effad35eSJamie Gritton 		 * which is expected to be a process that is not only locked,
2893effad35eSJamie Gritton 		 * but also half dead.  Add a reference so any calls to
2894effad35eSJamie Gritton 		 * prison_free() won't re-submit the task.
2895effad35eSJamie Gritton 		 */
2896f7496dcaSJamie Gritton 		prison_hold(pr);
28971158508aSJamie Gritton 		mtx_lock(&pr->pr_mtx);
28981158508aSJamie Gritton 		KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
28991158508aSJamie Gritton 		    ("Redundant last reference in prison_proc_free (jid=%d)",
29001158508aSJamie Gritton 		     pr->pr_id));
29011158508aSJamie Gritton 		pr->pr_flags |= PR_COMPLETE_PROC;
29021158508aSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2903effad35eSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2904effad35eSJamie Gritton 	}
2905effad35eSJamie Gritton }
2906effad35eSJamie Gritton 
2907c861373bSJamie Gritton static void
2908c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr)
2909c861373bSJamie Gritton {
2910c861373bSJamie Gritton #ifdef INVARIANTS
2911c861373bSJamie Gritton 	int lastref;
2912c861373bSJamie Gritton 
2913c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2914c861373bSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2915c861373bSJamie Gritton 	     pr, pr->pr_id));
2916c861373bSJamie Gritton 	lastref = refcount_release(&pr->pr_uref);
2917c861373bSJamie Gritton 	KASSERT(!lastref,
2918c861373bSJamie Gritton 	    ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2919c861373bSJamie Gritton 	     pr, pr->pr_id));
2920c861373bSJamie Gritton #else
2921c861373bSJamie Gritton 	refcount_release(&pr->pr_uref);
2922c861373bSJamie Gritton #endif
2923c861373bSJamie Gritton }
2924c861373bSJamie Gritton 
2925*5ecb5444SMateusz Guzik void
2926*5ecb5444SMateusz Guzik prison_proc_link(struct prison *pr, struct proc *p)
2927*5ecb5444SMateusz Guzik {
2928*5ecb5444SMateusz Guzik 
2929*5ecb5444SMateusz Guzik 	sx_assert(&allproc_lock, SA_XLOCKED);
2930*5ecb5444SMateusz Guzik 	LIST_INSERT_HEAD(&pr->pr_proclist, p, p_jaillist);
2931*5ecb5444SMateusz Guzik }
2932*5ecb5444SMateusz Guzik 
2933*5ecb5444SMateusz Guzik void
2934*5ecb5444SMateusz Guzik prison_proc_unlink(struct prison *pr, struct proc *p)
2935*5ecb5444SMateusz Guzik {
2936*5ecb5444SMateusz Guzik 
2937*5ecb5444SMateusz Guzik 	sx_assert(&allproc_lock, SA_XLOCKED);
2938*5ecb5444SMateusz Guzik 	LIST_REMOVE(p, p_jaillist);
2939*5ecb5444SMateusz Guzik }
2940*5ecb5444SMateusz Guzik 
2941*5ecb5444SMateusz Guzik static void
2942*5ecb5444SMateusz Guzik prison_proc_relink(struct prison *opr, struct prison *npr, struct proc *p)
2943*5ecb5444SMateusz Guzik {
2944*5ecb5444SMateusz Guzik 
2945*5ecb5444SMateusz Guzik 	sx_xlock(&allproc_lock);
2946*5ecb5444SMateusz Guzik 	prison_proc_unlink(opr, p);
2947*5ecb5444SMateusz Guzik 	prison_proc_link(npr, p);
2948*5ecb5444SMateusz Guzik 	sx_xunlock(&allproc_lock);
2949*5ecb5444SMateusz Guzik }
2950*5ecb5444SMateusz Guzik 
2951effad35eSJamie Gritton /*
295273d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
295373d9e52dSJamie Gritton  */
2954c2cda609SPawel Jakub Dawidek static void
2955c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2956c2cda609SPawel Jakub Dawidek {
295773d9e52dSJamie Gritton 	struct prison *pr = context;
2958f7496dcaSJamie Gritton 	int drflags;
2959b38ff370SJamie Gritton 
2960effad35eSJamie Gritton 	/*
29611158508aSJamie Gritton 	 * This could be called to release the last reference, or the last
29621158508aSJamie Gritton 	 * user reference (plus the reference held in prison_proc_free).
2963effad35eSJamie Gritton 	 */
2964f7496dcaSJamie Gritton 	drflags = prison_lock_xlock(pr, PD_DEREF);
29651158508aSJamie Gritton 	if (pr->pr_flags & PR_COMPLETE_PROC) {
29661158508aSJamie Gritton 		pr->pr_flags &= ~PR_COMPLETE_PROC;
2967f7496dcaSJamie Gritton 		drflags |= PD_DEUREF;
29681158508aSJamie Gritton 	}
2969f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2970b38ff370SJamie Gritton }
2971b38ff370SJamie Gritton 
2972*5ecb5444SMateusz Guzik static void
2973*5ecb5444SMateusz Guzik prison_kill_processes_cb(struct proc *p, void *arg __unused)
2974*5ecb5444SMateusz Guzik {
2975*5ecb5444SMateusz Guzik 
2976*5ecb5444SMateusz Guzik 	kern_psignal(p, SIGKILL);
2977*5ecb5444SMateusz Guzik }
2978*5ecb5444SMateusz Guzik 
2979*5ecb5444SMateusz Guzik /*
2980*5ecb5444SMateusz Guzik  * Note the iteration does not guarantee acting on all processes.
2981*5ecb5444SMateusz Guzik  * Most notably there may be fork or jail_attach in progress.
2982*5ecb5444SMateusz Guzik  */
2983*5ecb5444SMateusz Guzik void
2984*5ecb5444SMateusz Guzik prison_proc_iterate(struct prison *pr, void (*cb)(struct proc *, void *),
2985*5ecb5444SMateusz Guzik     void *cbarg)
2986*5ecb5444SMateusz Guzik {
2987*5ecb5444SMateusz Guzik 	struct prison *ppr;
2988*5ecb5444SMateusz Guzik 	struct proc *p;
2989*5ecb5444SMateusz Guzik 
2990*5ecb5444SMateusz Guzik 	if (atomic_load_int(&pr->pr_childcount) == 0) {
2991*5ecb5444SMateusz Guzik 		sx_slock(&allproc_lock);
2992*5ecb5444SMateusz Guzik 		LIST_FOREACH(p, &pr->pr_proclist, p_jaillist) {
2993*5ecb5444SMateusz Guzik 			if (p->p_state == PRS_NEW)
2994*5ecb5444SMateusz Guzik 				continue;
2995*5ecb5444SMateusz Guzik 			PROC_LOCK(p);
2996*5ecb5444SMateusz Guzik 			cb(p, cbarg);
2997*5ecb5444SMateusz Guzik 			PROC_UNLOCK(p);
2998*5ecb5444SMateusz Guzik 		}
2999*5ecb5444SMateusz Guzik 		sx_sunlock(&allproc_lock);
3000*5ecb5444SMateusz Guzik 		if (atomic_load_int(&pr->pr_childcount) == 0)
3001*5ecb5444SMateusz Guzik 			return;
3002*5ecb5444SMateusz Guzik 		/*
3003*5ecb5444SMateusz Guzik 		 * Some jails popped up during the iteration, fall through to a
3004*5ecb5444SMateusz Guzik 		 * system-wide search.
3005*5ecb5444SMateusz Guzik 		 */
3006*5ecb5444SMateusz Guzik 	}
3007*5ecb5444SMateusz Guzik 
3008*5ecb5444SMateusz Guzik 	sx_slock(&allproc_lock);
3009*5ecb5444SMateusz Guzik 	FOREACH_PROC_IN_SYSTEM(p) {
3010*5ecb5444SMateusz Guzik 		PROC_LOCK(p);
3011*5ecb5444SMateusz Guzik 		if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
3012*5ecb5444SMateusz Guzik 			for (ppr = p->p_ucred->cr_prison;
3013*5ecb5444SMateusz Guzik 			    ppr != &prison0;
3014*5ecb5444SMateusz Guzik 			    ppr = ppr->pr_parent) {
3015*5ecb5444SMateusz Guzik 				if (ppr == pr) {
3016*5ecb5444SMateusz Guzik 					cb(p, cbarg);
3017*5ecb5444SMateusz Guzik 					break;
3018*5ecb5444SMateusz Guzik 				}
3019*5ecb5444SMateusz Guzik 			}
3020*5ecb5444SMateusz Guzik 		}
3021*5ecb5444SMateusz Guzik 		PROC_UNLOCK(p);
3022*5ecb5444SMateusz Guzik 	}
3023*5ecb5444SMateusz Guzik 	sx_sunlock(&allproc_lock);
3024*5ecb5444SMateusz Guzik }
3025*5ecb5444SMateusz Guzik 
3026b38ff370SJamie Gritton /*
3027effad35eSJamie Gritton  * Remove a prison reference and/or user reference (usually).
3028effad35eSJamie Gritton  * This assumes context that allows sleeping (for allprison_lock),
3029effad35eSJamie Gritton  * with no non-sleeping locks held, except perhaps the prison itself.
3030effad35eSJamie Gritton  * If there are no more references, release and delist the prison.
3031effad35eSJamie Gritton  * On completion, the prison lock and the allprison lock are both
3032effad35eSJamie Gritton  * unlocked.
3033b38ff370SJamie Gritton  */
3034b38ff370SJamie Gritton static void
3035b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
3036b38ff370SJamie Gritton {
30376e1d1bfcSJamie Gritton 	struct prisonlist freeprison;
3038c861373bSJamie Gritton 	struct prison *killpr, *rpr, *ppr, *tpr;
3039c2cda609SPawel Jakub Dawidek 
3040c861373bSJamie Gritton 	killpr = NULL;
30416e1d1bfcSJamie Gritton 	TAILQ_INIT(&freeprison);
30426e1d1bfcSJamie Gritton 	/*
30436e1d1bfcSJamie Gritton 	 * Release this prison as requested, which may cause its parent
30446e1d1bfcSJamie Gritton 	 * to be released, and then maybe its grandparent, etc.
30456e1d1bfcSJamie Gritton 	 */
30460304c731SJamie Gritton 	for (;;) {
3047c861373bSJamie Gritton 		if (flags & PD_KILL) {
3048c861373bSJamie Gritton 			/* Kill the prison and its descendents. */
3049589e4c1dSJamie Gritton 			KASSERT(pr != &prison0,
3050589e4c1dSJamie Gritton 			    ("prison_deref trying to kill prison0"));
3051c861373bSJamie Gritton 			if (!(flags & PD_DEREF)) {
3052c861373bSJamie Gritton 				prison_hold(pr);
3053c861373bSJamie Gritton 				flags |= PD_DEREF;
3054c861373bSJamie Gritton 			}
3055c861373bSJamie Gritton 			flags = prison_lock_xlock(pr, flags);
3056c861373bSJamie Gritton 			prison_deref_kill(pr, &freeprison);
3057c861373bSJamie Gritton 		}
3058e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
3059f7496dcaSJamie Gritton 			/* Drop a user reference. */
30606754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_uref) > 0,
306173d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
306273d9e52dSJamie Gritton 			     pr->pr_id));
3063f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_uref)) {
3064f7496dcaSJamie Gritton 				if (!(flags & PD_DEREF)) {
3065f7496dcaSJamie Gritton 					prison_hold(pr);
3066f7496dcaSJamie Gritton 					flags |= PD_DEREF;
3067f7496dcaSJamie Gritton 				}
3068f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
30691158508aSJamie Gritton 				if (refcount_release(&pr->pr_uref) &&
30701158508aSJamie Gritton 				    pr->pr_state == PRISON_STATE_ALIVE) {
3071f7496dcaSJamie Gritton 					/*
3072f7496dcaSJamie Gritton 					 * When the last user references goes,
3073f7496dcaSJamie Gritton 					 * this becomes a dying prison.
3074f7496dcaSJamie Gritton 					 */
3075f7496dcaSJamie Gritton 					KASSERT(
3076f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_uref) > 0,
30776754ae25SJamie Gritton 					    ("prison0 pr_uref=0"));
30781158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_DYING;
3079f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
3080f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
3081a9f7455cSJamie Gritton 					prison_cleanup(pr);
3082f7496dcaSJamie Gritton 				}
3083f7496dcaSJamie Gritton 			}
3084f7496dcaSJamie Gritton 		}
3085c861373bSJamie Gritton 		if (flags & PD_KILL) {
3086c861373bSJamie Gritton 			/*
3087c861373bSJamie Gritton 			 * Any remaining user references are probably processes
3088c861373bSJamie Gritton 			 * that need to be killed, either in this prison or its
3089c861373bSJamie Gritton 			 * descendants.
3090c861373bSJamie Gritton 			 */
3091c861373bSJamie Gritton 			if (refcount_load(&pr->pr_uref) > 0)
3092c861373bSJamie Gritton 				killpr = pr;
3093589e4c1dSJamie Gritton 			/* Make sure the parent prison doesn't get killed. */
3094589e4c1dSJamie Gritton 			flags &= ~PD_KILL;
3095c861373bSJamie Gritton 		}
309673d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
3097f7496dcaSJamie Gritton 			/* Drop a reference. */
30986754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_ref) > 0,
309973d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
310073d9e52dSJamie Gritton 			     pr->pr_id));
3101f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_ref)) {
3102f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
3103f7496dcaSJamie Gritton 				if (refcount_release(&pr->pr_ref)) {
3104cc5fd8c7SJamie Gritton 					/*
3105f7496dcaSJamie Gritton 					 * When the last reference goes,
3106f7496dcaSJamie Gritton 					 * unlink the prison and set it aside.
3107cc5fd8c7SJamie Gritton 					 */
3108f7496dcaSJamie Gritton 					KASSERT(
3109f7496dcaSJamie Gritton 					    refcount_load(&pr->pr_uref) == 0,
3110f7496dcaSJamie Gritton 					    ("prison_deref: last ref, "
3111f7496dcaSJamie Gritton 					     "but still has %d urefs (jid=%d)",
3112f7496dcaSJamie Gritton 					     pr->pr_uref, pr->pr_id));
3113f7496dcaSJamie Gritton 					KASSERT(
3114f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_ref) != 0,
3115f7496dcaSJamie Gritton 					    ("prison0 pr_ref=0"));
31161158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_INVALID;
3117b38ff370SJamie Gritton 					TAILQ_REMOVE(&allprison, pr, pr_list);
31180304c731SJamie Gritton 					LIST_REMOVE(pr, pr_sibling);
3119f7496dcaSJamie Gritton 					TAILQ_INSERT_TAIL(&freeprison, pr,
3120f7496dcaSJamie Gritton 					    pr_list);
3121f7496dcaSJamie Gritton 					for (ppr = pr->pr_parent;
3122f7496dcaSJamie Gritton 					     ppr != NULL;
3123f7496dcaSJamie Gritton 					     ppr = ppr->pr_parent)
3124f7496dcaSJamie Gritton 						ppr->pr_childcount--;
3125f7496dcaSJamie Gritton 					/*
3126f7496dcaSJamie Gritton 					 * Removing a prison frees references
3127f7496dcaSJamie Gritton 					 * from its parent.
3128f7496dcaSJamie Gritton 					 */
3129f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
3130f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
31316e1d1bfcSJamie Gritton 					pr = pr->pr_parent;
31326e1d1bfcSJamie Gritton 					flags |= PD_DEREF | PD_DEUREF;
3133f7496dcaSJamie Gritton 					continue;
3134f7496dcaSJamie Gritton 				}
3135f7496dcaSJamie Gritton 			}
3136f7496dcaSJamie Gritton 		}
3137f7496dcaSJamie Gritton 		break;
31386e1d1bfcSJamie Gritton 	}
31396e1d1bfcSJamie Gritton 
31406e1d1bfcSJamie Gritton 	/* Release all the prison locks. */
3141f7496dcaSJamie Gritton 	if (flags & PD_LOCKED)
3142f7496dcaSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31436e1d1bfcSJamie Gritton 	if (flags & PD_LIST_SLOCKED)
31446e1d1bfcSJamie Gritton 		sx_sunlock(&allprison_lock);
31456e1d1bfcSJamie Gritton 	else if (flags & PD_LIST_XLOCKED)
31466e1d1bfcSJamie Gritton 		sx_xunlock(&allprison_lock);
31476e1d1bfcSJamie Gritton 
3148c861373bSJamie Gritton 	/* Kill any processes attached to a killed prison. */
3149*5ecb5444SMateusz Guzik 	if (killpr != NULL)
3150*5ecb5444SMateusz Guzik 		prison_proc_iterate(killpr, prison_kill_processes_cb, NULL);
3151c861373bSJamie Gritton 
31526e1d1bfcSJamie Gritton 	/*
31536e1d1bfcSJamie Gritton 	 * Finish removing any unreferenced prisons, which couldn't happen
31546e1d1bfcSJamie Gritton 	 * while allprison_lock was held (to avoid a LOR on vrele).
31556e1d1bfcSJamie Gritton 	 */
31566e1d1bfcSJamie Gritton 	TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
31576e1d1bfcSJamie Gritton #ifdef VIMAGE
31586e1d1bfcSJamie Gritton 		if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
31596e1d1bfcSJamie Gritton 			vnet_destroy(rpr->pr_vnet);
31606e1d1bfcSJamie Gritton #endif
31616e1d1bfcSJamie Gritton 		if (rpr->pr_root != NULL)
31626e1d1bfcSJamie Gritton 			vrele(rpr->pr_root);
31636e1d1bfcSJamie Gritton 		mtx_destroy(&rpr->pr_mtx);
31646e1d1bfcSJamie Gritton #ifdef INET
3165eb8dcdeaSGleb Smirnoff 		prison_ip_free(rpr->pr_addrs[PR_INET]);
31666e1d1bfcSJamie Gritton #endif
31676e1d1bfcSJamie Gritton #ifdef INET6
3168eb8dcdeaSGleb Smirnoff 		prison_ip_free(rpr->pr_addrs[PR_INET6]);
31696e1d1bfcSJamie Gritton #endif
31706e1d1bfcSJamie Gritton 		if (rpr->pr_cpuset != NULL)
31716e1d1bfcSJamie Gritton 			cpuset_rel(rpr->pr_cpuset);
31726e1d1bfcSJamie Gritton 		osd_jail_exit(rpr);
31736e1d1bfcSJamie Gritton #ifdef RACCT
31746e1d1bfcSJamie Gritton 		if (racct_enable)
31756e1d1bfcSJamie Gritton 			prison_racct_detach(rpr);
31766e1d1bfcSJamie Gritton #endif
3177f7496dcaSJamie Gritton 		TAILQ_REMOVE(&freeprison, rpr, pr_list);
31786e1d1bfcSJamie Gritton 		free(rpr, M_PRISON);
31790304c731SJamie Gritton 	}
3180b3059e09SRobert Watson }
3181b3059e09SRobert Watson 
3182413628a7SBjoern A. Zeeb /*
3183c861373bSJamie Gritton  * Kill the prison and its descendants.  Mark them as dying, clear the
3184c861373bSJamie Gritton  * persist flag, and call module remove methods.
3185c861373bSJamie Gritton  */
3186c861373bSJamie Gritton static void
3187c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3188c861373bSJamie Gritton {
3189c861373bSJamie Gritton 	struct prison *cpr, *ppr, *rpr;
3190c861373bSJamie Gritton 	bool descend;
3191c861373bSJamie Gritton 
3192c861373bSJamie Gritton 	/*
3193c861373bSJamie Gritton 	 * Unlike the descendants, the target prison can be killed
3194c861373bSJamie Gritton 	 * even if it is currently dying.  This is useful for failed
3195c861373bSJamie Gritton 	 * creation in jail_set(2).
3196c861373bSJamie Gritton 	 */
3197c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
3198c861373bSJamie Gritton 	    ("Trying to kill dead prison %p (jid=%d).",
3199c861373bSJamie Gritton 	     pr, pr->pr_id));
3200c861373bSJamie Gritton 	refcount_acquire(&pr->pr_uref);
3201c861373bSJamie Gritton 	pr->pr_state = PRISON_STATE_DYING;
3202c861373bSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3203c861373bSJamie Gritton 
3204c861373bSJamie Gritton 	rpr = NULL;
3205c861373bSJamie Gritton 	FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3206c861373bSJamie Gritton 		if (descend) {
3207c861373bSJamie Gritton 			if (!prison_isalive(cpr)) {
3208c861373bSJamie Gritton 				descend = false;
3209c861373bSJamie Gritton 				continue;
3210c861373bSJamie Gritton 			}
3211c861373bSJamie Gritton 			prison_hold(cpr);
3212c861373bSJamie Gritton 			prison_proc_hold(cpr);
3213c861373bSJamie Gritton 			mtx_lock(&cpr->pr_mtx);
3214c861373bSJamie Gritton 			cpr->pr_state = PRISON_STATE_DYING;
3215c861373bSJamie Gritton 			cpr->pr_flags |= PR_REMOVE;
3216c861373bSJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3217c861373bSJamie Gritton 			continue;
3218c861373bSJamie Gritton 		}
3219c861373bSJamie Gritton 		if (!(cpr->pr_flags & PR_REMOVE))
3220c861373bSJamie Gritton 			continue;
3221a9f7455cSJamie Gritton 		prison_cleanup(cpr);
3222c861373bSJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3223c861373bSJamie Gritton 		cpr->pr_flags &= ~PR_REMOVE;
3224c861373bSJamie Gritton 		if (cpr->pr_flags & PR_PERSIST) {
3225c861373bSJamie Gritton 			cpr->pr_flags &= ~PR_PERSIST;
3226c861373bSJamie Gritton 			prison_proc_free_not_last(cpr);
3227c861373bSJamie Gritton 			prison_free_not_last(cpr);
3228c861373bSJamie Gritton 		}
3229c861373bSJamie Gritton 		(void)refcount_release(&cpr->pr_uref);
3230c861373bSJamie Gritton 		if (refcount_release(&cpr->pr_ref)) {
3231c861373bSJamie Gritton 			/*
3232c861373bSJamie Gritton 			 * When the last reference goes, unlink the prison
3233c861373bSJamie Gritton 			 * and set it aside for prison_deref() to handle.
3234c861373bSJamie Gritton 			 * Delay unlinking the sibling list to keep the loop
3235c861373bSJamie Gritton 			 * safe.
3236c861373bSJamie Gritton 			 */
3237c861373bSJamie Gritton 			if (rpr != NULL)
3238c861373bSJamie Gritton 				LIST_REMOVE(rpr, pr_sibling);
3239c861373bSJamie Gritton 			rpr = cpr;
3240c861373bSJamie Gritton 			rpr->pr_state = PRISON_STATE_INVALID;
3241c861373bSJamie Gritton 			TAILQ_REMOVE(&allprison, rpr, pr_list);
3242c861373bSJamie Gritton 			TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3243c861373bSJamie Gritton 			/*
3244c861373bSJamie Gritton 			 * Removing a prison frees references from its parent.
3245c861373bSJamie Gritton 			 */
3246c861373bSJamie Gritton 			ppr = rpr->pr_parent;
3247c861373bSJamie Gritton 			prison_proc_free_not_last(ppr);
3248c861373bSJamie Gritton 			prison_free_not_last(ppr);
3249c861373bSJamie Gritton 			for (; ppr != NULL; ppr = ppr->pr_parent)
3250c861373bSJamie Gritton 				ppr->pr_childcount--;
3251c861373bSJamie Gritton 		}
3252c861373bSJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3253c861373bSJamie Gritton 	}
3254c861373bSJamie Gritton 	if (rpr != NULL)
3255c861373bSJamie Gritton 		LIST_REMOVE(rpr, pr_sibling);
3256c861373bSJamie Gritton 
3257a9f7455cSJamie Gritton 	prison_cleanup(pr);
3258c861373bSJamie Gritton 	mtx_lock(&pr->pr_mtx);
3259c861373bSJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
3260c861373bSJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
3261c861373bSJamie Gritton 		prison_proc_free_not_last(pr);
3262c861373bSJamie Gritton 		prison_free_not_last(pr);
3263c861373bSJamie Gritton 	}
3264c861373bSJamie Gritton 	(void)refcount_release(&pr->pr_uref);
3265c861373bSJamie Gritton }
3266c861373bSJamie Gritton 
3267c861373bSJamie Gritton /*
3268f7496dcaSJamie Gritton  * Given the current locking state in the flags, make sure allprison_lock
3269f7496dcaSJamie Gritton  * is held exclusive, and the prison is locked.  Return flags indicating
3270f7496dcaSJamie Gritton  * the new state.
3271f7496dcaSJamie Gritton  */
3272f7496dcaSJamie Gritton static int
3273f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags)
3274f7496dcaSJamie Gritton {
3275f7496dcaSJamie Gritton 
3276f7496dcaSJamie Gritton 	if (!(flags & PD_LIST_XLOCKED)) {
3277f7496dcaSJamie Gritton 		/*
3278f7496dcaSJamie Gritton 		 * Get allprison_lock, which may be an upgrade,
3279f7496dcaSJamie Gritton 		 * and may require unlocking the prison.
3280f7496dcaSJamie Gritton 		 */
3281f7496dcaSJamie Gritton 		if (flags & PD_LOCKED) {
3282f7496dcaSJamie Gritton 			mtx_unlock(&pr->pr_mtx);
3283f7496dcaSJamie Gritton 			flags &= ~PD_LOCKED;
3284f7496dcaSJamie Gritton 		}
3285f7496dcaSJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
3286f7496dcaSJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
3287f7496dcaSJamie Gritton 				sx_sunlock(&allprison_lock);
3288f7496dcaSJamie Gritton 				sx_xlock(&allprison_lock);
3289f7496dcaSJamie Gritton 			}
3290f7496dcaSJamie Gritton 			flags &= ~PD_LIST_SLOCKED;
3291f7496dcaSJamie Gritton 		} else
3292f7496dcaSJamie Gritton 			sx_xlock(&allprison_lock);
3293f7496dcaSJamie Gritton 		flags |= PD_LIST_XLOCKED;
3294f7496dcaSJamie Gritton 	}
3295f7496dcaSJamie Gritton 	if (!(flags & PD_LOCKED)) {
3296f7496dcaSJamie Gritton 		/* Lock the prison mutex. */
3297f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
3298f7496dcaSJamie Gritton 		flags |= PD_LOCKED;
3299f7496dcaSJamie Gritton 	}
3300f7496dcaSJamie Gritton 	return flags;
3301f7496dcaSJamie Gritton }
3302f7496dcaSJamie Gritton 
3303f7496dcaSJamie Gritton /*
3304a9f7455cSJamie Gritton  * Release a prison's resources when it starts dying (when the last user
3305a9f7455cSJamie Gritton  * reference is dropped, or when it is killed).
3306a9f7455cSJamie Gritton  */
3307a9f7455cSJamie Gritton static void
3308a9f7455cSJamie Gritton prison_cleanup(struct prison *pr)
3309a9f7455cSJamie Gritton {
3310a9f7455cSJamie Gritton 	sx_assert(&allprison_lock, SA_XLOCKED);
3311a9f7455cSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
33127060da62SJamie Gritton 	shm_remove_prison(pr);
3313a9f7455cSJamie Gritton 	(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3314a9f7455cSJamie Gritton }
3315a9f7455cSJamie Gritton 
3316a9f7455cSJamie Gritton /*
33170fe74ae6SJamie Gritton  * Set or clear a permission bit in the pr_allow field, passing restrictions
33180fe74ae6SJamie Gritton  * (cleared permission) down to child jails.
33190fe74ae6SJamie Gritton  */
33200fe74ae6SJamie Gritton void
33210fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable)
33220fe74ae6SJamie Gritton {
33230fe74ae6SJamie Gritton 	struct prison *pr;
33240fe74ae6SJamie Gritton 
33250fe74ae6SJamie Gritton 	pr = cred->cr_prison;
33260fe74ae6SJamie Gritton 	sx_slock(&allprison_lock);
33270fe74ae6SJamie Gritton 	mtx_lock(&pr->pr_mtx);
33280fe74ae6SJamie Gritton 	prison_set_allow_locked(pr, flag, enable);
33290fe74ae6SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
33300fe74ae6SJamie Gritton 	sx_sunlock(&allprison_lock);
33310fe74ae6SJamie Gritton }
33320fe74ae6SJamie Gritton 
33330fe74ae6SJamie Gritton static void
33340fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
33350fe74ae6SJamie Gritton {
33360fe74ae6SJamie Gritton 	struct prison *cpr;
33370fe74ae6SJamie Gritton 	int descend;
33380fe74ae6SJamie Gritton 
33390fe74ae6SJamie Gritton 	if (enable != 0)
33400fe74ae6SJamie Gritton 		pr->pr_allow |= flag;
33410fe74ae6SJamie Gritton 	else {
33420fe74ae6SJamie Gritton 		pr->pr_allow &= ~flag;
33430fe74ae6SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
33440fe74ae6SJamie Gritton 			cpr->pr_allow &= ~flag;
33450fe74ae6SJamie Gritton 	}
33460fe74ae6SJamie Gritton }
33470fe74ae6SJamie Gritton 
33480fe74ae6SJamie Gritton /*
3349ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3350ca04ba64SJamie Gritton  *
3351ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3352ca04ba64SJamie Gritton  * if not.
3353ca04ba64SJamie Gritton  */
3354ca04ba64SJamie Gritton int
3355ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3356ca04ba64SJamie Gritton {
33570304c731SJamie Gritton 	struct prison *pr;
3358ca04ba64SJamie Gritton 	int error;
3359ca04ba64SJamie Gritton 
3360ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3361ca04ba64SJamie Gritton 
33620304c731SJamie Gritton 	pr = cred->cr_prison;
3363499650a0SJamie Gritton #ifdef VIMAGE
33646bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3365de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
33666bb79563SJamie Gritton 		return (0);
3367499650a0SJamie Gritton #endif
33686bb79563SJamie Gritton 
3369ca04ba64SJamie Gritton 	error = 0;
3370ca04ba64SJamie Gritton 	switch (af)
3371ca04ba64SJamie Gritton 	{
3372ca04ba64SJamie Gritton #ifdef INET
3373ca04ba64SJamie Gritton 	case AF_INET:
33740304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
33750304c731SJamie Gritton 		{
33760304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
3377eb8dcdeaSGleb Smirnoff 			if ((pr->pr_flags & PR_IP4) &&
3378eb8dcdeaSGleb Smirnoff 			    pr->pr_addrs[PR_INET] == NULL)
3379ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33800304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33810304c731SJamie Gritton 		}
3382ca04ba64SJamie Gritton 		break;
3383ca04ba64SJamie Gritton #endif
3384ca04ba64SJamie Gritton #ifdef INET6
3385ca04ba64SJamie Gritton 	case AF_INET6:
33860304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
33870304c731SJamie Gritton 		{
33880304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
3389eb8dcdeaSGleb Smirnoff 			if ((pr->pr_flags & PR_IP6) &&
3390eb8dcdeaSGleb Smirnoff 			    pr->pr_addrs[PR_INET6] == NULL)
3391ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33920304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33930304c731SJamie Gritton 		}
3394ca04ba64SJamie Gritton 		break;
3395ca04ba64SJamie Gritton #endif
3396ca04ba64SJamie Gritton 	case AF_LOCAL:
3397ca04ba64SJamie Gritton 	case AF_ROUTE:
3398ca04ba64SJamie Gritton 		break;
3399ca04ba64SJamie Gritton 	default:
34000304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3401ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3402ca04ba64SJamie Gritton 	}
3403ca04ba64SJamie Gritton 	return (error);
3404ca04ba64SJamie Gritton }
3405ca04ba64SJamie Gritton 
3406ca04ba64SJamie Gritton /*
3407413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3408413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3409413628a7SBjoern A. Zeeb  *
34100304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
34110304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
34120304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3413413628a7SBjoern A. Zeeb  */
3414413628a7SBjoern A. Zeeb int
3415c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa)
341675c13541SPoul-Henning Kamp {
3417413628a7SBjoern A. Zeeb #ifdef INET
3418c83dda36SAlexander V. Chernikov 	const struct sockaddr_in *sai;
3419413628a7SBjoern A. Zeeb #endif
3420413628a7SBjoern A. Zeeb #ifdef INET6
3421c83dda36SAlexander V. Chernikov 	const struct sockaddr_in6 *sai6;
3422413628a7SBjoern A. Zeeb #endif
3423b89e82ddSJamie Gritton 	int error;
342475c13541SPoul-Henning Kamp 
3425413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3426413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3427413628a7SBjoern A. Zeeb 
3428de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3429de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3430de0bd6f7SBjoern A. Zeeb 		return (0);
3431de0bd6f7SBjoern A. Zeeb #endif
3432de0bd6f7SBjoern A. Zeeb 
3433b89e82ddSJamie Gritton 	error = 0;
3434413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3435413628a7SBjoern A. Zeeb 	{
3436413628a7SBjoern A. Zeeb #ifdef INET
3437413628a7SBjoern A. Zeeb 	case AF_INET:
3438c83dda36SAlexander V. Chernikov 		sai = (const struct sockaddr_in *)sa;
3439b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3440413628a7SBjoern A. Zeeb 		break;
3441413628a7SBjoern A. Zeeb #endif
3442413628a7SBjoern A. Zeeb #ifdef INET6
3443413628a7SBjoern A. Zeeb 	case AF_INET6:
3444c83dda36SAlexander V. Chernikov 		sai6 = (const struct sockaddr_in6 *)sa;
3445b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3446413628a7SBjoern A. Zeeb 		break;
3447413628a7SBjoern A. Zeeb #endif
3448413628a7SBjoern A. Zeeb 	default:
34490304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3450b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3451413628a7SBjoern A. Zeeb 	}
3452b89e82ddSJamie Gritton 	return (error);
345375c13541SPoul-Henning Kamp }
345491421ba2SRobert Watson 
345591421ba2SRobert Watson /*
345691421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
345791421ba2SRobert Watson  */
345891421ba2SRobert Watson int
34599ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
346091421ba2SRobert Watson {
346191421ba2SRobert Watson 
34620304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
34630304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
34640304c731SJamie Gritton }
346591421ba2SRobert Watson 
34660304c731SJamie Gritton /*
34670304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
34680304c731SJamie Gritton  */
34690304c731SJamie Gritton int
34700304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
34710304c731SJamie Gritton {
34720304c731SJamie Gritton 
34730304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
34740304c731SJamie Gritton 		if (pr1 == pr2)
34750304c731SJamie Gritton 			return (1);
347691421ba2SRobert Watson 	return (0);
347791421ba2SRobert Watson }
347891421ba2SRobert Watson 
347991421ba2SRobert Watson /*
3480f7496dcaSJamie Gritton  * Return true if the prison is currently alive.  A prison is alive if it
3481f7496dcaSJamie Gritton  * holds user references and it isn't being removed.
348276ad42abSJamie Gritton  */
348376ad42abSJamie Gritton bool
3484eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr)
348576ad42abSJamie Gritton {
348676ad42abSJamie Gritton 
34871158508aSJamie Gritton 	if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3488cc7b7306SJamie Gritton 		return (false);
348976ad42abSJamie Gritton 	return (true);
349076ad42abSJamie Gritton }
349176ad42abSJamie Gritton 
349276ad42abSJamie Gritton /*
349376ad42abSJamie Gritton  * Return true if the prison is currently valid.  A prison is valid if it has
349476ad42abSJamie Gritton  * been fully created, and is not being destroyed.  Note that dying prisons
3495f7496dcaSJamie Gritton  * are still considered valid.  Invalid prisons won't be found under normal
3496f7496dcaSJamie Gritton  * circumstances, as they're only put in that state by functions that have
3497f7496dcaSJamie Gritton  * an exclusive hold on allprison_lock.
349876ad42abSJamie Gritton  */
349976ad42abSJamie Gritton bool
350076ad42abSJamie Gritton prison_isvalid(struct prison *pr)
350176ad42abSJamie Gritton {
350276ad42abSJamie Gritton 
35031158508aSJamie Gritton 	if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
35041158508aSJamie Gritton 		return (false);
35056754ae25SJamie Gritton 	if (__predict_false(refcount_load(&pr->pr_ref) == 0))
350676ad42abSJamie Gritton 		return (false);
350776ad42abSJamie Gritton 	return (true);
350876ad42abSJamie Gritton }
350976ad42abSJamie Gritton 
351076ad42abSJamie Gritton /*
3511de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3512de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3513de0bd6f7SBjoern A. Zeeb  */
3514de0bd6f7SBjoern A. Zeeb int
3515de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3516de0bd6f7SBjoern A. Zeeb {
3517de0bd6f7SBjoern A. Zeeb 
3518de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3519de0bd6f7SBjoern A. Zeeb 		return (0);
3520de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3521de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3522de0bd6f7SBjoern A. Zeeb 		return (0);
3523de0bd6f7SBjoern A. Zeeb #endif
3524de0bd6f7SBjoern A. Zeeb 
3525de0bd6f7SBjoern A. Zeeb 	return (1);
3526de0bd6f7SBjoern A. Zeeb }
3527de0bd6f7SBjoern A. Zeeb 
3528de0bd6f7SBjoern A. Zeeb /*
35297455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
35309484d0c0SRobert Drehmel  */
3531ad1ff099SRobert Drehmel void
35329ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
35339484d0c0SRobert Drehmel {
353476ca6f88SJamie Gritton 	struct prison *pr;
35359484d0c0SRobert Drehmel 
35367455b100SJamie Gritton 	/*
35377455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
35387455b100SJamie Gritton 	 * system's hostname.
35397455b100SJamie Gritton 	 */
354076ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
354176ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3542c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
354376ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
35449484d0c0SRobert Drehmel }
3545fd7a8150SMike Barcroft 
35467455b100SJamie Gritton void
35477455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
35487455b100SJamie Gritton {
35497455b100SJamie Gritton 
35507455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3551c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
35527455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35537455b100SJamie Gritton }
35547455b100SJamie Gritton 
35557455b100SJamie Gritton void
35567455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
35577455b100SJamie Gritton {
35587455b100SJamie Gritton 
35597455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3560c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
35617455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35627455b100SJamie Gritton }
35637455b100SJamie Gritton 
35647455b100SJamie Gritton void
35657455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
35667455b100SJamie Gritton {
35677455b100SJamie Gritton 
35687455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
35697455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
35707455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35717455b100SJamie Gritton }
35727455b100SJamie Gritton 
35733f8bc99cSKristof Provost void
35743f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len)
35753f8bc99cSKristof Provost {
35763f8bc99cSKristof Provost 
35773f8bc99cSKristof Provost 	mtx_lock(&cred->cr_prison->pr_mtx);
35783f8bc99cSKristof Provost 	strlcpy(name, cred->cr_prison->pr_name, len);
35793f8bc99cSKristof Provost 	mtx_unlock(&cred->cr_prison->pr_mtx);
35803f8bc99cSKristof Provost }
35813f8bc99cSKristof Provost 
3582eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3583eb79e1c7SBjoern A. Zeeb /*
3584eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3585eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3586eb79e1c7SBjoern A. Zeeb  *
3587eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3588eb79e1c7SBjoern A. Zeeb  */
3589eb79e1c7SBjoern A. Zeeb int
3590eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3591eb79e1c7SBjoern A. Zeeb {
3592eb79e1c7SBjoern A. Zeeb 
3593eb79e1c7SBjoern A. Zeeb 	/*
3594eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3595eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3596eb79e1c7SBjoern A. Zeeb 	 */
3597eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3598eb79e1c7SBjoern A. Zeeb }
3599eb79e1c7SBjoern A. Zeeb #endif
3600eb79e1c7SBjoern A. Zeeb 
3601f08df373SRobert Watson /*
3602820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3603820a0de9SPawel Jakub Dawidek  * status of a mount point.
3604820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3605820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3606820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3607f08df373SRobert Watson  */
3608f08df373SRobert Watson int
3609820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3610f08df373SRobert Watson {
3611820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3612820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3613820a0de9SPawel Jakub Dawidek 	size_t len;
3614f08df373SRobert Watson 
3615820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
36160304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
36170304c731SJamie Gritton 		return (0);
3618820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3619820a0de9SPawel Jakub Dawidek 		return (0);
36200304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3621820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3622820a0de9SPawel Jakub Dawidek 	/*
3623820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3624820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3625820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3626820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3627820a0de9SPawel Jakub Dawidek 	 */
3628820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3629820a0de9SPawel Jakub Dawidek 		return (0);
3630820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3631820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3632820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3633820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3634820a0de9SPawel Jakub Dawidek 	/*
3635820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3636820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3637820a0de9SPawel Jakub Dawidek 	 */
3638820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3639820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3640f08df373SRobert Watson 	return (0);
3641f08df373SRobert Watson }
3642820a0de9SPawel Jakub Dawidek 
3643820a0de9SPawel Jakub Dawidek void
3644820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3645820a0de9SPawel Jakub Dawidek {
3646820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3647820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3648820a0de9SPawel Jakub Dawidek 	size_t len;
3649820a0de9SPawel Jakub Dawidek 
3650820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
36510304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
36520304c731SJamie Gritton 		return;
3653820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3654820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3655820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3656820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3657820a0de9SPawel Jakub Dawidek 		return;
3658820a0de9SPawel Jakub Dawidek 	}
3659820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3660820a0de9SPawel Jakub Dawidek 		/*
3661820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3662820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3663820a0de9SPawel Jakub Dawidek 		 */
3664820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3665820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3666820a0de9SPawel Jakub Dawidek 		return;
3667820a0de9SPawel Jakub Dawidek 	}
3668820a0de9SPawel Jakub Dawidek 	/*
3669820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3670820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3671820a0de9SPawel Jakub Dawidek 	 */
3672820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3673820a0de9SPawel Jakub Dawidek 		return;
3674820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3675820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3676820a0de9SPawel Jakub Dawidek 	/*
3677820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3678820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3679820a0de9SPawel Jakub Dawidek 	 */
3680820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3681820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3682820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3683820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3684820a0de9SPawel Jakub Dawidek 	} else {
3685820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3686820a0de9SPawel Jakub Dawidek 	}
3687f08df373SRobert Watson }
3688f08df373SRobert Watson 
3689800c9408SRobert Watson /*
3690800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3691800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3692800c9408SRobert Watson  */
3693800c9408SRobert Watson int
3694800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3695800c9408SRobert Watson {
36960fe74ae6SJamie Gritton 	struct prison *pr;
36970fe74ae6SJamie Gritton 	int error;
3698800c9408SRobert Watson 
36997b2ff0dcSMateusz Guzik 	/*
37007b2ff0dcSMateusz Guzik 	 * Some policies have custom handlers. This routine should not be
37017b2ff0dcSMateusz Guzik 	 * called for them. See priv_check_cred().
37027b2ff0dcSMateusz Guzik 	 */
37037b2ff0dcSMateusz Guzik 	switch (priv) {
3704a459a6cfSMateusz Guzik 	case PRIV_VFS_LOOKUP:
37057b2ff0dcSMateusz Guzik 	case PRIV_VFS_GENERATION:
37067b2ff0dcSMateusz Guzik 		KASSERT(0, ("prison_priv_check instead of a custom handler "
37077b2ff0dcSMateusz Guzik 		    "called for %d\n", priv));
37087b2ff0dcSMateusz Guzik 	}
37097b2ff0dcSMateusz Guzik 
3710800c9408SRobert Watson 	if (!jailed(cred))
3711800c9408SRobert Watson 		return (0);
3712800c9408SRobert Watson 
37136bb79563SJamie Gritton #ifdef VIMAGE
37146bb79563SJamie Gritton 	/*
37156bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
37166bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
37176bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
37186bb79563SJamie Gritton 	 */
37196bb79563SJamie Gritton 	switch (priv) {
37206bb79563SJamie Gritton #ifdef notyet
37216bb79563SJamie Gritton 		/*
37226bb79563SJamie Gritton 		 * NFS-specific privileges.
37236bb79563SJamie Gritton 		 */
37246bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
37256bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
37266bb79563SJamie Gritton #endif
37276bb79563SJamie Gritton 		/*
37286bb79563SJamie Gritton 		 * Network stack privileges.
37296bb79563SJamie Gritton 		 */
37306bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
37316bb79563SJamie Gritton 	case PRIV_NET_GRE:
37326bb79563SJamie Gritton 	case PRIV_NET_BPF:
37336bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
37346bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
37356bb79563SJamie Gritton 	case PRIV_NET_TAP:
37366bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
37376bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
37386bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3739215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
37406bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
37416bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
37426bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
37436bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
3744389474c1SKonstantin Belousov 	case PRIV_NET_SETLANPCP:
37456bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
37466bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
37476bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
37486bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
37496bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
37506bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
37516bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
37526bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
37536bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
37546bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
37556bb79563SJamie Gritton 	case PRIV_NET_LAGG:
37566bb79563SJamie Gritton 	case PRIV_NET_GIF:
37576bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
375835fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
3759ab91feabSKristof Provost 	case PRIV_NET_OVPN:
37606bb79563SJamie Gritton 
37616bb79563SJamie Gritton 		/*
37626bb79563SJamie Gritton 		 * 802.11-related privileges.
37636bb79563SJamie Gritton 		 */
3764f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_GETKEY:
3765f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_MANAGE:
37666bb79563SJamie Gritton 
37676bb79563SJamie Gritton #ifdef notyet
37686bb79563SJamie Gritton 		/*
37696bb79563SJamie Gritton 		 * ATM privileges.
37706bb79563SJamie Gritton 		 */
37716bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
37726bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
37736bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
37746bb79563SJamie Gritton 	case PRIV_NETATM_SET:
37756bb79563SJamie Gritton 
37766bb79563SJamie Gritton 		/*
37776bb79563SJamie Gritton 		 * Bluetooth privileges.
37786bb79563SJamie Gritton 		 */
37796bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
37806bb79563SJamie Gritton #endif
37816bb79563SJamie Gritton 
37826bb79563SJamie Gritton 		/*
37836bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
37846bb79563SJamie Gritton 		 */
37856bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
37866bb79563SJamie Gritton #ifdef notyet
37876bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
37886bb79563SJamie Gritton #endif
37896bb79563SJamie Gritton 
37906bb79563SJamie Gritton 		/*
37916bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
37926bb79563SJamie Gritton 		 */
37936bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
37946bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
37956bb79563SJamie Gritton 	case PRIV_NETINET_PF:
37966bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
37976bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
37986bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
37996bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
38006bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
38016bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
38026bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
38036bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
38046bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
38056bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
38066bb79563SJamie Gritton 
38076bb79563SJamie Gritton #ifdef notyet
38086bb79563SJamie Gritton 		/*
38096bb79563SJamie Gritton 		 * NCP privileges.
38106bb79563SJamie Gritton 		 */
38116bb79563SJamie Gritton 	case PRIV_NETNCP:
38126bb79563SJamie Gritton 
38136bb79563SJamie Gritton 		/*
38146bb79563SJamie Gritton 		 * SMB privileges.
38156bb79563SJamie Gritton 		 */
38166bb79563SJamie Gritton 	case PRIV_NETSMB:
38176bb79563SJamie Gritton #endif
38186bb79563SJamie Gritton 
38196bb79563SJamie Gritton 	/*
38206bb79563SJamie Gritton 	 * No default: or deny here.
38216bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
38226bb79563SJamie Gritton 	 */
38236bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
38246bb79563SJamie Gritton 			return (0);
38256bb79563SJamie Gritton 	}
38266bb79563SJamie Gritton #endif /* VIMAGE */
38276bb79563SJamie Gritton 
3828800c9408SRobert Watson 	switch (priv) {
3829800c9408SRobert Watson 		/*
3830800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3831800c9408SRobert Watson 		 */
3832800c9408SRobert Watson 	case PRIV_KTRACE:
3833800c9408SRobert Watson 
3834c3c1b5e6SRobert Watson #if 0
3835800c9408SRobert Watson 		/*
3836800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3837800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3838800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3839800c9408SRobert Watson 		 * jail.
3840800c9408SRobert Watson 		 */
3841800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3842800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3843800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3844c3c1b5e6SRobert Watson #endif
3845800c9408SRobert Watson 
3846800c9408SRobert Watson 		/*
3847800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3848800c9408SRobert Watson 		 * credentials in any way they see fit.
3849800c9408SRobert Watson 		 */
3850800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3851800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3852800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3853800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3854800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3855800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3856800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3857800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3858800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3859800c9408SRobert Watson 
3860800c9408SRobert Watson 		/*
3861800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3862800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3863800c9408SRobert Watson 		 */
3864800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3865800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3866800c9408SRobert Watson 
3867800c9408SRobert Watson 		/*
3868800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3869800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3870800c9408SRobert Watson 		 */
3871800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3872800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3873800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3874800c9408SRobert Watson 
3875800c9408SRobert Watson 		/*
3876800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3877800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3878800c9408SRobert Watson 		 */
3879800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3880800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3881800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3882800c9408SRobert Watson 
3883800c9408SRobert Watson 		/*
3884800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3885800c9408SRobert Watson 		 */
3886800c9408SRobert Watson 	case PRIV_IPC_READ:
3887800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3888800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3889800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3890800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3891800c9408SRobert Watson 
3892800c9408SRobert Watson 		/*
38930304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
38940304c731SJamie Gritton 		 */
38950304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
38960304c731SJamie Gritton 	case PRIV_JAIL_SET:
38970304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
38980304c731SJamie Gritton 
38990304c731SJamie Gritton 		/*
3900800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3901800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3902800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3903800c9408SRobert Watson 		 */
3904800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3905413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3906800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3907800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3908800c9408SRobert Watson 
3909800c9408SRobert Watson 		/*
3910800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3911800c9408SRobert Watson 		 * writable.
3912800c9408SRobert Watson 		 */
3913800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3914800c9408SRobert Watson 
3915800c9408SRobert Watson 		/*
3916800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3917e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3918e82d0201SRobert Watson 		 * configuration option.
3919800c9408SRobert Watson 		 */
392095b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
392195b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3922800c9408SRobert Watson 
3923800c9408SRobert Watson 		/*
3924800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3925800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3926800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3927800c9408SRobert Watson 		 * privileges.
3928800c9408SRobert Watson 		 */
3929800c9408SRobert Watson 	case PRIV_VFS_READ:
3930800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3931800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3932800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3933800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3934800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3935800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3936800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3937bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3938800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3939800c9408SRobert Watson 	case PRIV_VFS_LINK:
3940800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3941e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3942800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3943bb56d716SJamie Gritton 
3944bb56d716SJamie Gritton 		/*
3945bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
394670de1003SGordon Bergling 		 * able to read kernel/physical memory (provided /dev/[k]mem
3947bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3948bb56d716SJamie Gritton 		 */
3949bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3950800c9408SRobert Watson 		return (0);
3951800c9408SRobert Watson 
3952800c9408SRobert Watson 		/*
3953800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3954800c9408SRobert Watson 		 * setting system flags.
3955800c9408SRobert Watson 		 */
3956800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
39570304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3958800c9408SRobert Watson 			return (0);
3959800c9408SRobert Watson 		else
3960800c9408SRobert Watson 			return (EPERM);
3961800c9408SRobert Watson 
3962800c9408SRobert Watson 		/*
3963f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3964f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3965f3a8d2f9SPawel Jakub Dawidek 		 */
3966f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3967f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3968f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
396924b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
39700fe74ae6SJamie Gritton 		pr = cred->cr_prison;
39710fe74ae6SJamie Gritton 		prison_lock(pr);
39720fe74ae6SJamie Gritton 		if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
39730fe74ae6SJamie Gritton 			error = 0;
3974f3a8d2f9SPawel Jakub Dawidek 		else
39750fe74ae6SJamie Gritton 			error = EPERM;
39760fe74ae6SJamie Gritton 		prison_unlock(pr);
39770fe74ae6SJamie Gritton 		return (error);
3978f3a8d2f9SPawel Jakub Dawidek 
3979f3a8d2f9SPawel Jakub Dawidek 		/*
398063619b6dSKyle Evans 		 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
398163619b6dSKyle Evans 		 * policy.  priv_check_cred will not specifically allow it, and
398263619b6dSKyle Evans 		 * we may want a MAC policy to allow it.
398363619b6dSKyle Evans 		 */
398463619b6dSKyle Evans 	case PRIV_VFS_READ_DIR:
398563619b6dSKyle Evans 		return (0);
398663619b6dSKyle Evans 
398763619b6dSKyle Evans 		/*
3988ccd6ac9fSAntoine Brodin 		 * Conditionnaly allow locking (unlocking) physical pages
3989ccd6ac9fSAntoine Brodin 		 * in memory.
3990ccd6ac9fSAntoine Brodin 		 */
3991ccd6ac9fSAntoine Brodin 	case PRIV_VM_MLOCK:
3992ccd6ac9fSAntoine Brodin 	case PRIV_VM_MUNLOCK:
3993ccd6ac9fSAntoine Brodin 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3994ccd6ac9fSAntoine Brodin 			return (0);
3995ccd6ac9fSAntoine Brodin 		else
3996ccd6ac9fSAntoine Brodin 			return (EPERM);
3997ccd6ac9fSAntoine Brodin 
3998ccd6ac9fSAntoine Brodin 		/*
3999e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
4000800c9408SRobert Watson 		 */
4001800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
4002e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
4003e28f9b7dSAllan Jude 			return (0);
4004e28f9b7dSAllan Jude 		else
4005e28f9b7dSAllan Jude 			return (EPERM);
4006e28f9b7dSAllan Jude 
4007e28f9b7dSAllan Jude 		/*
4008e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
4009e28f9b7dSAllan Jude 		 */
40104b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
4011800c9408SRobert Watson 		return (0);
4012800c9408SRobert Watson 
4013800c9408SRobert Watson 		/*
4014e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
401579ba3952SBjoern A. Zeeb 		 */
401679ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
401779ba3952SBjoern A. Zeeb 		return (0);
401879ba3952SBjoern A. Zeeb 
401979ba3952SBjoern A. Zeeb 		/*
4020800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
4021800c9408SRobert Watson 		 */
4022800c9408SRobert Watson 	case PRIV_NETINET_RAW:
40230304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4024800c9408SRobert Watson 			return (0);
4025800c9408SRobert Watson 		else
4026800c9408SRobert Watson 			return (EPERM);
4027800c9408SRobert Watson 
4028800c9408SRobert Watson 		/*
4029800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
4030800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
4031800c9408SRobert Watson 		 * jail.
4032800c9408SRobert Watson 		 */
4033800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
4034800c9408SRobert Watson 		return (0);
4035800c9408SRobert Watson 
40362bfc50bcSEdward Tomasz Napierala 		/*
40372bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
40382bfc50bcSEdward Tomasz Napierala 		 */
40392bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
40402bfc50bcSEdward Tomasz Napierala 		return (0);
40412bfc50bcSEdward Tomasz Napierala 
4042b19d66fdSJamie Gritton 		/*
40434520f617SJamie Gritton 		 * Do not allow a process inside a jail to read the kernel
4044b19d66fdSJamie Gritton 		 * message buffer unless explicitly permitted.
4045b19d66fdSJamie Gritton 		 */
4046b19d66fdSJamie Gritton 	case PRIV_MSGBUF:
4047b19d66fdSJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
4048b19d66fdSJamie Gritton 			return (0);
4049b19d66fdSJamie Gritton 		return (EPERM);
4050b19d66fdSJamie Gritton 
4051800c9408SRobert Watson 	default:
4052800c9408SRobert Watson 		/*
4053800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
4054800c9408SRobert Watson 		 * includes almost all network privileges, many system
4055800c9408SRobert Watson 		 * configuration privileges.
4056800c9408SRobert Watson 		 */
4057800c9408SRobert Watson 		return (EPERM);
4058800c9408SRobert Watson 	}
4059800c9408SRobert Watson }
4060800c9408SRobert Watson 
40610304c731SJamie Gritton /*
40620304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
40630304c731SJamie Gritton  * if it does not directly follow.
40640304c731SJamie Gritton  */
40650304c731SJamie Gritton 
40660304c731SJamie Gritton char *
40670304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
40680304c731SJamie Gritton {
40690304c731SJamie Gritton 	char *name;
40700304c731SJamie Gritton 
40710304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
40720304c731SJamie Gritton 	if (pr1 == pr2)
40730304c731SJamie Gritton 		return "0";
40740304c731SJamie Gritton 	name = pr2->pr_name;
40750304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
40760304c731SJamie Gritton 		/*
40770304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
40780304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
40790304c731SJamie Gritton 		 * can be counted on - and counted.
40800304c731SJamie Gritton 		 */
40810304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
40820304c731SJamie Gritton 			name = strchr(name, '.') + 1;
40830304c731SJamie Gritton 	}
40840304c731SJamie Gritton 	return (name);
40850304c731SJamie Gritton }
40860304c731SJamie Gritton 
40870304c731SJamie Gritton /*
40880304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
40890304c731SJamie Gritton  * if it does not directly follow.
40900304c731SJamie Gritton  */
40910304c731SJamie Gritton static char *
40920304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
40930304c731SJamie Gritton {
40940304c731SJamie Gritton 	char *path1, *path2;
40950304c731SJamie Gritton 	int len1;
40960304c731SJamie Gritton 
40970304c731SJamie Gritton 	path1 = pr1->pr_path;
40980304c731SJamie Gritton 	path2 = pr2->pr_path;
40990304c731SJamie Gritton 	if (!strcmp(path1, "/"))
41000304c731SJamie Gritton 		return (path2);
41010304c731SJamie Gritton 	len1 = strlen(path1);
41020304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
41030304c731SJamie Gritton 		return (path2);
41040304c731SJamie Gritton 	if (path2[len1] == '\0')
41050304c731SJamie Gritton 		return "/";
41060304c731SJamie Gritton 	if (path2[len1] == '/')
41070304c731SJamie Gritton 		return (path2 + len1);
41080304c731SJamie Gritton 	return (path2);
41090304c731SJamie Gritton }
41100304c731SJamie Gritton 
41110304c731SJamie Gritton /*
41120304c731SJamie Gritton  * Jail-related sysctls.
41130304c731SJamie Gritton  */
41147029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
41150304c731SJamie Gritton     "Jails");
41160304c731SJamie Gritton 
4117eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6)
4118eb8dcdeaSGleb Smirnoff /*
4119eb8dcdeaSGleb Smirnoff  * Copy address array to memory that would be then SYSCTL_OUT-ed.
4120eb8dcdeaSGleb Smirnoff  * sysctl_jail_list() helper.
4121eb8dcdeaSGleb Smirnoff  */
4122eb8dcdeaSGleb Smirnoff static void
4123eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4124eb8dcdeaSGleb Smirnoff {
4125eb8dcdeaSGleb Smirnoff 	const size_t size = pr_families[af].size;
4126eb8dcdeaSGleb Smirnoff 
4127eb8dcdeaSGleb Smirnoff  again:
4128eb8dcdeaSGleb Smirnoff 	mtx_assert(&pr->pr_mtx, MA_OWNED);
4129eb8dcdeaSGleb Smirnoff 	if (pr->pr_addrs[af] != NULL) {
4130eb8dcdeaSGleb Smirnoff 		if (*len < pr->pr_addrs[af]->ips) {
4131eb8dcdeaSGleb Smirnoff 			*len = pr->pr_addrs[af]->ips;
4132eb8dcdeaSGleb Smirnoff 			mtx_unlock(&pr->pr_mtx);
4133eb8dcdeaSGleb Smirnoff 			*out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4134eb8dcdeaSGleb Smirnoff 			mtx_lock(&pr->pr_mtx);
4135eb8dcdeaSGleb Smirnoff 			goto again;
4136eb8dcdeaSGleb Smirnoff 		}
4137eb8dcdeaSGleb Smirnoff 		bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size);
4138eb8dcdeaSGleb Smirnoff 	}
4139eb8dcdeaSGleb Smirnoff }
4140eb8dcdeaSGleb Smirnoff #endif
4141eb8dcdeaSGleb Smirnoff 
4142fd7a8150SMike Barcroft static int
4143fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4144fd7a8150SMike Barcroft {
4145b38ff370SJamie Gritton 	struct xprison *xp;
41460304c731SJamie Gritton 	struct prison *pr, *cpr;
4147b38ff370SJamie Gritton #ifdef INET
4148b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
4149b38ff370SJamie Gritton 	int ip4s = 0;
4150b38ff370SJamie Gritton #endif
4151b38ff370SJamie Gritton #ifdef INET6
41523beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
4153b38ff370SJamie Gritton 	int ip6s = 0;
4154b38ff370SJamie Gritton #endif
41550304c731SJamie Gritton 	int descend, error;
4156fd7a8150SMike Barcroft 
4157b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
41580304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
4159b38ff370SJamie Gritton 	error = 0;
4160dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
41610304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
41620304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
4163413628a7SBjoern A. Zeeb #ifdef INET
4164eb8dcdeaSGleb Smirnoff 		prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4165413628a7SBjoern A. Zeeb #endif
4166413628a7SBjoern A. Zeeb #ifdef INET6
4167eb8dcdeaSGleb Smirnoff 		prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4168b38ff370SJamie Gritton #endif
4169b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
4170fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
41710304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
41721158508aSJamie Gritton 		xp->pr_state = cpr->pr_state;
41730304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4174c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
41750304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4176413628a7SBjoern A. Zeeb #ifdef INET
4177eb8dcdeaSGleb Smirnoff 		xp->pr_ip4s = ip4s;
4178413628a7SBjoern A. Zeeb #endif
4179413628a7SBjoern A. Zeeb #ifdef INET6
4180eb8dcdeaSGleb Smirnoff 		xp->pr_ip6s = ip6s;
4181413628a7SBjoern A. Zeeb #endif
41820304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
4183b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4184b38ff370SJamie Gritton 		if (error)
4185b38ff370SJamie Gritton 			break;
4186413628a7SBjoern A. Zeeb #ifdef INET
4187b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
4188b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
4189b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
4190b38ff370SJamie Gritton 			if (error)
4191b38ff370SJamie Gritton 				break;
4192413628a7SBjoern A. Zeeb 		}
4193413628a7SBjoern A. Zeeb #endif
4194413628a7SBjoern A. Zeeb #ifdef INET6
4195b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
4196b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
4197b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
4198b38ff370SJamie Gritton 			if (error)
4199b38ff370SJamie Gritton 				break;
4200413628a7SBjoern A. Zeeb 		}
4201413628a7SBjoern A. Zeeb #endif
4202fd7a8150SMike Barcroft 	}
4203dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
4204b38ff370SJamie Gritton 	free(xp, M_TEMP);
4205b38ff370SJamie Gritton #ifdef INET
4206b38ff370SJamie Gritton 	free(ip4, M_TEMP);
4207b38ff370SJamie Gritton #endif
4208b38ff370SJamie Gritton #ifdef INET6
4209b38ff370SJamie Gritton 	free(ip6, M_TEMP);
4210b38ff370SJamie Gritton #endif
4211fd7a8150SMike Barcroft 	return (error);
4212fd7a8150SMike Barcroft }
4213fd7a8150SMike Barcroft 
4214f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
4215f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4216f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
4217461167c2SPawel Jakub Dawidek 
4218461167c2SPawel Jakub Dawidek static int
4219461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4220461167c2SPawel Jakub Dawidek {
4221461167c2SPawel Jakub Dawidek 	int error, injail;
4222461167c2SPawel Jakub Dawidek 
4223461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
4224461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4225461167c2SPawel Jakub Dawidek 
4226461167c2SPawel Jakub Dawidek 	return (error);
4227461167c2SPawel Jakub Dawidek }
42280304c731SJamie Gritton 
4229f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4230f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4231f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
4232413628a7SBjoern A. Zeeb 
4233761d2bb5SJamie Gritton static int
4234761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4235761d2bb5SJamie Gritton {
4236761d2bb5SJamie Gritton 	int error, havevnet;
4237761d2bb5SJamie Gritton #ifdef VIMAGE
4238761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
4239761d2bb5SJamie Gritton 
4240761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
4241761d2bb5SJamie Gritton #else
4242761d2bb5SJamie Gritton 	havevnet = 0;
4243761d2bb5SJamie Gritton #endif
4244761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4245761d2bb5SJamie Gritton 
4246761d2bb5SJamie Gritton 	return (error);
4247761d2bb5SJamie Gritton }
4248761d2bb5SJamie Gritton 
4249761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4250761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4251bb8f1623SBjoern A. Zeeb     sysctl_jail_vnet, "I", "Jail owns vnet?");
4252761d2bb5SJamie Gritton 
42530304c731SJamie Gritton #if defined(INET) || defined(INET6)
4254e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
42550304c731SJamie Gritton     &jail_max_af_ips, 0,
4256c542c43eSJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
42570304c731SJamie Gritton #endif
42580304c731SJamie Gritton 
42590304c731SJamie Gritton /*
4260c542c43eSJamie Gritton  * Default parameters for jail(2) compatibility.  For historical reasons,
4261c542c43eSJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
4262c542c43eSJamie Gritton  * just see their own parameters, and can't change them.
42630304c731SJamie Gritton  */
42640304c731SJamie Gritton static int
42650304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
42660304c731SJamie Gritton {
42670fe74ae6SJamie Gritton 	int error, i;
42680304c731SJamie Gritton 
42690304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
42700fe74ae6SJamie Gritton 	if (req->td->td_ucred->cr_prison == &prison0) {
42710fe74ae6SJamie Gritton 		mtx_lock(&prison0.pr_mtx);
42720fe74ae6SJamie Gritton 		i = (jail_default_allow & arg2) != 0;
42730fe74ae6SJamie Gritton 		mtx_unlock(&prison0.pr_mtx);
42740fe74ae6SJamie Gritton 	} else
42750fe74ae6SJamie Gritton 		i = prison_allow(req->td->td_ucred, arg2);
42760fe74ae6SJamie Gritton 
42770304c731SJamie Gritton 	if (arg1 != NULL)
42780304c731SJamie Gritton 		i = !i;
42790304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
4280c542c43eSJamie Gritton 	if (error || !req->newptr)
42810304c731SJamie Gritton 		return (error);
42820304c731SJamie Gritton 	i = i ? arg2 : 0;
42830304c731SJamie Gritton 	if (arg1 != NULL)
42840304c731SJamie Gritton 		i ^= arg2;
42850304c731SJamie Gritton 	/*
42860304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
42870304c731SJamie Gritton 	 * for writing.
42880304c731SJamie Gritton 	 */
42890304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
42900304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
42910304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
42920304c731SJamie Gritton 	return (0);
42930304c731SJamie Gritton }
42940304c731SJamie Gritton 
42950304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4296c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42970304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4298c542c43eSJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
42990304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4300c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43010304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4302c542c43eSJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
43030304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4304c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43050304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4306c542c43eSJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
43070304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4308c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43090304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4310c542c43eSJamie Gritton     "Prison root can create raw sockets (deprecated)");
43110304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4312c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43130304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4314c542c43eSJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
43150304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4316c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
43170304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4318c542c43eSJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
43190304c731SJamie Gritton 
43200304c731SJamie Gritton static int
43210304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
43220304c731SJamie Gritton {
43230304c731SJamie Gritton 	struct prison *pr;
43240304c731SJamie Gritton 	int level, error;
43250304c731SJamie Gritton 
43260304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
43270304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
43280304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
4329c542c43eSJamie Gritton 	if (error || !req->newptr)
43300304c731SJamie Gritton 		return (error);
43310304c731SJamie Gritton 	*(int *)arg1 = level;
43320304c731SJamie Gritton 	return (0);
43330304c731SJamie Gritton }
43340304c731SJamie Gritton 
43350304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4336c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4337c542c43eSJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
43380304c731SJamie Gritton     sysctl_jail_default_level, "I",
4339c542c43eSJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
4340c542c43eSJamie Gritton 
43410cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4342c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4343c542c43eSJamie Gritton     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
43440cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
4345c542c43eSJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
43460cc207a6SMartin Matuska 
43470304c731SJamie Gritton /*
43480304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
43490304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
43500304c731SJamie Gritton  * to make themselves and their types known.
43510304c731SJamie Gritton  */
43527029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
43530304c731SJamie Gritton     "Jail parameters");
43540304c731SJamie Gritton 
43550304c731SJamie Gritton int
43560304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
43570304c731SJamie Gritton {
43580304c731SJamie Gritton 	int i;
43590304c731SJamie Gritton 	long l;
43600304c731SJamie Gritton 	size_t s;
43610304c731SJamie Gritton 	char numbuf[12];
43620304c731SJamie Gritton 
43630304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
43640304c731SJamie Gritton 	{
43650304c731SJamie Gritton 	case CTLTYPE_LONG:
43660304c731SJamie Gritton 	case CTLTYPE_ULONG:
43670304c731SJamie Gritton 		l = 0;
43680304c731SJamie Gritton #ifdef SCTL_MASK32
43690304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
43700304c731SJamie Gritton #endif
43710304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
43720304c731SJamie Gritton 	case CTLTYPE_INT:
43730304c731SJamie Gritton 	case CTLTYPE_UINT:
43740304c731SJamie Gritton 		i = 0;
43750304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
43760304c731SJamie Gritton 	case CTLTYPE_STRING:
4377e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
43780304c731SJamie Gritton 		return
43790304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
43800304c731SJamie Gritton 	case CTLTYPE_STRUCT:
43810304c731SJamie Gritton 		s = (size_t)arg2;
43820304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
43830304c731SJamie Gritton 	}
43840304c731SJamie Gritton 	return (0);
43850304c731SJamie Gritton }
43860304c731SJamie Gritton 
4387b96bd95bSIan Lepore /*
4388b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4389b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
4390b96bd95bSIan Lepore  */
43910304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
43920304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
43930304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
43940304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
43950304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
43960304c731SJamie Gritton     "I", "Jail secure level");
4397b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4398b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
4399b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4400b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
44010304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
44020304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
44030cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
44040cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
44050304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
44060304c731SJamie Gritton     "B", "Jail persistence");
4407679e1390SJamie Gritton #ifdef VIMAGE
4408679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
44097cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4410679e1390SJamie Gritton #endif
44110304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
44120304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
44130304c731SJamie Gritton 
4414b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4415b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4416b97457e2SJamie Gritton     "I", "Current number of child jails");
4417b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4418b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4419b97457e2SJamie Gritton 
44207cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
44210304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
44220304c731SJamie Gritton     "Jail hostname");
442376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
442476ca6f88SJamie Gritton     "Jail NIS domainname");
442576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
442676ca6f88SJamie Gritton     "Jail host UUID");
442776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
442876ca6f88SJamie Gritton     "LU", "Jail host ID");
44290304c731SJamie Gritton 
44300304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
44310304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
44320304c731SJamie Gritton 
44330304c731SJamie Gritton #ifdef INET
44342b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
44352b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
44360304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
44370304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4438592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4439592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4440592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
44410304c731SJamie Gritton #endif
44420304c731SJamie Gritton #ifdef INET6
44432b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
44442b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
44450304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
44460304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4447592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4448592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4449592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
44500304c731SJamie Gritton #endif
44510304c731SJamie Gritton 
44520304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
44530304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
44540304c731SJamie Gritton     "B", "Jail may set hostname");
44550304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
44560304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
44570304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
44580304c731SJamie Gritton     "B", "Jail may create raw sockets");
44590304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
44600304c731SJamie Gritton     "B", "Jail may alter system file flags");
44610304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
44620304c731SJamie Gritton     "B", "Jail may set file quotas");
44630304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
44640304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4465ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4466ccd6ac9fSAntoine Brodin     "B", "Jail may lock (unlock) physical pages in memory");
4467e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4468e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
4469b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4470b19d66fdSJamie Gritton     "B", "Jail may read the kernel message buffer");
4471b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4472b3079544SJamie Gritton     "B", "Unprivileged processes may use process debugging facilities");
447305e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
447405e1e482SMariusz Zaborski     "B", "Processes in jail with uid 0 have privilege");
44750304c731SJamie Gritton 
4476bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4477bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4478bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
44790e5c6bd4SJamie Gritton 
44800e5c6bd4SJamie Gritton /*
44810a172404SJamie Gritton  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
44820a172404SJamie Gritton  * its associated bit in the pr_allow bitmask, or zero if the parameter was
44830a172404SJamie Gritton  * not created.
44840e5c6bd4SJamie Gritton  */
44850a172404SJamie Gritton unsigned
44860a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
44870a172404SJamie Gritton     const char *descr)
44880e5c6bd4SJamie Gritton {
44890e5c6bd4SJamie Gritton 	struct bool_flags *bf;
44900a172404SJamie Gritton 	struct sysctl_oid *parent;
44910a172404SJamie Gritton 	char *allow_name, *allow_noname, *allowed;
4492c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4493c542c43eSJamie Gritton 	char *descr_deprecated;
4494c542c43eSJamie Gritton #endif
44955d58f959SJamie Gritton 	u_int allow_flag;
44960e5c6bd4SJamie Gritton 
44970a172404SJamie Gritton 	if (prefix
44980a172404SJamie Gritton 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
44990a172404SJamie Gritton 		< 0 ||
45000a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
45010a172404SJamie Gritton 		< 0
45020a172404SJamie Gritton 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
45030a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
45040e5c6bd4SJamie Gritton 		free(allow_name, M_PRISON);
45050a172404SJamie Gritton 		return 0;
45060e5c6bd4SJamie Gritton 	}
45070e5c6bd4SJamie Gritton 
45080e5c6bd4SJamie Gritton 	/*
45090a172404SJamie Gritton 	 * See if this parameter has already beed added, i.e. a module was
45100a172404SJamie Gritton 	 * previously loaded/unloaded.
45110e5c6bd4SJamie Gritton 	 */
45120e5c6bd4SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
45130e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow;
45145d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
45155d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
45160e5c6bd4SJamie Gritton 	     bf++) {
45170e5c6bd4SJamie Gritton 		if (strcmp(bf->name, allow_name) == 0) {
45180a172404SJamie Gritton 			allow_flag = bf->flag;
45190e5c6bd4SJamie Gritton 			goto no_add;
45200e5c6bd4SJamie Gritton 		}
45210e5c6bd4SJamie Gritton 	}
45220e5c6bd4SJamie Gritton 
45230e5c6bd4SJamie Gritton 	/*
45245d58f959SJamie Gritton 	 * Find a free bit in pr_allow_all, failing if there are none
45250e5c6bd4SJamie Gritton 	 * (which shouldn't happen as long as we keep track of how many
45260a172404SJamie Gritton 	 * potential dynamic flags exist).
45270e5c6bd4SJamie Gritton 	 */
45280e5c6bd4SJamie Gritton 	for (allow_flag = 1;; allow_flag <<= 1) {
45290e5c6bd4SJamie Gritton 		if (allow_flag == 0)
45300e5c6bd4SJamie Gritton 			goto no_add;
45315d58f959SJamie Gritton 		if ((pr_allow_all & allow_flag) == 0)
45320e5c6bd4SJamie Gritton 			break;
45330e5c6bd4SJamie Gritton 	}
45340e5c6bd4SJamie Gritton 
45355d58f959SJamie Gritton 	/* Note the parameter in the next open slot in pr_flag_allow. */
45365d58f959SJamie Gritton 	for (bf = pr_flag_allow; ; bf++) {
45370e5c6bd4SJamie Gritton 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
45380e5c6bd4SJamie Gritton 			/* This should never happen, but is not fatal. */
45390a172404SJamie Gritton 			allow_flag = 0;
45400e5c6bd4SJamie Gritton 			goto no_add;
45410e5c6bd4SJamie Gritton 		}
45425d58f959SJamie Gritton 		if (atomic_load_int(&bf->flag) == 0)
45435d58f959SJamie Gritton 			break;
45445d58f959SJamie Gritton 	}
45450e5c6bd4SJamie Gritton 	bf->name = allow_name;
45460e5c6bd4SJamie Gritton 	bf->noname = allow_noname;
45475d58f959SJamie Gritton 	pr_allow_all |= allow_flag;
45485d58f959SJamie Gritton 	/*
45495d58f959SJamie Gritton 	 * prison0 always has permission for the new parameter.
45505d58f959SJamie Gritton 	 * Other jails must have it granted to them.
45515d58f959SJamie Gritton 	 */
45525d58f959SJamie Gritton 	prison0.pr_allow |= allow_flag;
45535d58f959SJamie Gritton 	/* The flag indicates a valid entry, so make sure it is set last. */
45545d58f959SJamie Gritton 	atomic_store_rel_int(&bf->flag, allow_flag);
45550e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
45560e5c6bd4SJamie Gritton 
4557c542c43eSJamie Gritton 	/*
4558c542c43eSJamie Gritton 	 * Create sysctls for the paramter, and the back-compat global
4559c542c43eSJamie Gritton 	 * permission.
4560c542c43eSJamie Gritton 	 */
45610a172404SJamie Gritton 	parent = prefix
45620a172404SJamie Gritton 	    ? SYSCTL_ADD_NODE(NULL,
45630a172404SJamie Gritton 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
45647029da5cSPawel Biernacki 		  OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
45650a172404SJamie Gritton 	    : &sysctl___security_jail_param_allow;
45660a172404SJamie Gritton 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
45670a172404SJamie Gritton 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
45680e5c6bd4SJamie Gritton 	    NULL, 0, sysctl_jail_param, "B", descr);
45690a172404SJamie Gritton 	if ((prefix
45700a172404SJamie Gritton 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
45710a172404SJamie Gritton 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4572c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4573c542c43eSJamie Gritton 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4574c542c43eSJamie Gritton 		    descr);
4575c542c43eSJamie Gritton #endif
45760e5c6bd4SJamie Gritton 		(void)SYSCTL_ADD_PROC(NULL,
45770a172404SJamie Gritton 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4578c542c43eSJamie Gritton 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4579c542c43eSJamie Gritton 		    sysctl_jail_default_allow, "I", descr_deprecated);
4580c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4581c542c43eSJamie Gritton 		free(descr_deprecated, M_TEMP);
4582c542c43eSJamie Gritton #endif
45830a172404SJamie Gritton 		free(allowed, M_TEMP);
45840e5c6bd4SJamie Gritton 	}
45850a172404SJamie Gritton 	return allow_flag;
45860e5c6bd4SJamie Gritton 
45870e5c6bd4SJamie Gritton  no_add:
45880e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
45890e5c6bd4SJamie Gritton 	free(allow_name, M_PRISON);
45900e5c6bd4SJamie Gritton 	free(allow_noname, M_PRISON);
45910a172404SJamie Gritton 	return allow_flag;
45920a172404SJamie Gritton }
45930a172404SJamie Gritton 
45940a172404SJamie Gritton /*
45950a172404SJamie Gritton  * The VFS system will register jail-aware filesystems here.  They each get
45960a172404SJamie Gritton  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
45970a172404SJamie Gritton  * attempts to mount.
45980a172404SJamie Gritton  */
45990a172404SJamie Gritton void
46000a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp)
46010a172404SJamie Gritton {
46020a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR
46030a172404SJamie Gritton 
46040a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
46050a172404SJamie Gritton 	    NULL, NULL);
46060a172404SJamie Gritton #else
46070a172404SJamie Gritton 	char *descr;
46080a172404SJamie Gritton 
46090a172404SJamie Gritton 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
46100a172404SJamie Gritton 	    vfsp->vfc_name);
46110a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
46120a172404SJamie Gritton 	    NULL, descr);
46130a172404SJamie Gritton 	free(descr, M_TEMP);
46140a172404SJamie Gritton #endif
46150e5c6bd4SJamie Gritton }
4616bf3db8aaSMartin Matuska 
46174b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4618097055e2SEdward Tomasz Napierala void
4619097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
462015db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
462115db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
4622097055e2SEdward Tomasz Napierala {
4623a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4624097055e2SEdward Tomasz Napierala 
46254b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46264b5c9cf6SEdward Tomasz Napierala 
4627097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
462815db3c07SEdward Tomasz Napierala 	if (pre != NULL)
462915db3c07SEdward Tomasz Napierala 		(pre)();
4630a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4631a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
463215db3c07SEdward Tomasz Napierala 	if (post != NULL)
463315db3c07SEdward Tomasz Napierala 		(post)();
4634097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4635097055e2SEdward Tomasz Napierala }
46360304c731SJamie Gritton 
4637a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4638a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4639a7ad07bfSEdward Tomasz Napierala {
4640a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4641a7ad07bfSEdward Tomasz Napierala 
46424b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4643a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4644a7ad07bfSEdward Tomasz Napierala 
4645a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4646a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4647a7ad07bfSEdward Tomasz Napierala 
4648a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4649a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4650a7ad07bfSEdward Tomasz Napierala 			continue;
4651a7ad07bfSEdward Tomasz Napierala 
4652a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4653a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4654a7ad07bfSEdward Tomasz Napierala 		return (prr);
4655a7ad07bfSEdward Tomasz Napierala 	}
4656a7ad07bfSEdward Tomasz Napierala 
4657a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4658a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4659a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4660a7ad07bfSEdward Tomasz Napierala 
4661a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4662a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4663a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4664a7ad07bfSEdward Tomasz Napierala 
4665a7ad07bfSEdward Tomasz Napierala 	return (prr);
4666a7ad07bfSEdward Tomasz Napierala }
4667a7ad07bfSEdward Tomasz Napierala 
4668a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4669a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4670a7ad07bfSEdward Tomasz Napierala {
4671a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4672a7ad07bfSEdward Tomasz Napierala 
46734b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46744b5c9cf6SEdward Tomasz Napierala 
4675a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4676a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4677a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4678a7ad07bfSEdward Tomasz Napierala 	return (prr);
4679a7ad07bfSEdward Tomasz Napierala }
4680a7ad07bfSEdward Tomasz Napierala 
4681a7ad07bfSEdward Tomasz Napierala void
4682a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4683a7ad07bfSEdward Tomasz Napierala {
4684a7ad07bfSEdward Tomasz Napierala 
46854b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
46864b5c9cf6SEdward Tomasz Napierala 
4687a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4688a7ad07bfSEdward Tomasz Napierala }
4689a7ad07bfSEdward Tomasz Napierala 
4690c34bbd2aSEdward Tomasz Napierala static void
4691c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4692c34bbd2aSEdward Tomasz Napierala {
4693c34bbd2aSEdward Tomasz Napierala 
46944b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4695c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4696c34bbd2aSEdward Tomasz Napierala 
4697c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4698c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4699c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4700c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4701c34bbd2aSEdward Tomasz Napierala 	}
4702c34bbd2aSEdward Tomasz Napierala }
4703c34bbd2aSEdward Tomasz Napierala 
4704a7ad07bfSEdward Tomasz Napierala void
4705a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4706a7ad07bfSEdward Tomasz Napierala {
4707a7ad07bfSEdward Tomasz Napierala 
47084b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4709c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4710c34bbd2aSEdward Tomasz Napierala 
47116ff4688bSMateusz Guzik 	if (refcount_release_if_not_last(&prr->prr_refcount))
4712a7ad07bfSEdward Tomasz Napierala 		return;
4713a7ad07bfSEdward Tomasz Napierala 
4714a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4715c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4716a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4717a7ad07bfSEdward Tomasz Napierala }
4718a7ad07bfSEdward Tomasz Napierala 
4719a7ad07bfSEdward Tomasz Napierala static void
4720a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4721a7ad07bfSEdward Tomasz Napierala {
4722a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4723a7ad07bfSEdward Tomasz Napierala 
47244b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4725c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4726c34bbd2aSEdward Tomasz Napierala 
4727a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4728a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4729a7ad07bfSEdward Tomasz Napierala 
4730a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4731a7ad07bfSEdward Tomasz Napierala }
4732a7ad07bfSEdward Tomasz Napierala 
4733c34bbd2aSEdward Tomasz Napierala /*
4734c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4735c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4736c34bbd2aSEdward Tomasz Napierala  */
4737c34bbd2aSEdward Tomasz Napierala static void
4738c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4739c34bbd2aSEdward Tomasz Napierala {
4740dbadb015SKonstantin Belousov #ifdef RCTL
4741c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4742c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4743dbadb015SKonstantin Belousov #endif
4744c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4745c34bbd2aSEdward Tomasz Napierala 
47464b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
47474b5c9cf6SEdward Tomasz Napierala 
4748c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4749c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4750c34bbd2aSEdward Tomasz Napierala 
4751e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4752e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4753e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4754c34bbd2aSEdward Tomasz Napierala 		return;
4755e30345e7SEdward Tomasz Napierala 	}
4756c34bbd2aSEdward Tomasz Napierala 
4757c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4758c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4759c34bbd2aSEdward Tomasz Napierala 
4760c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4761c34bbd2aSEdward Tomasz Napierala 
4762c34bbd2aSEdward Tomasz Napierala 	/*
4763c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4764c34bbd2aSEdward Tomasz Napierala 	 */
4765c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4766c34bbd2aSEdward Tomasz Napierala 
4767f87beb93SAndriy Gapon #ifdef RCTL
4768c34bbd2aSEdward Tomasz Napierala 	/*
4769c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4770c34bbd2aSEdward Tomasz Napierala 	 */
4771c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4772c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4773c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4774c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4775f87beb93SAndriy Gapon 		rctl_proc_ucred_changed(p, cred);
4776c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4777c34bbd2aSEdward Tomasz Napierala 	}
4778f87beb93SAndriy Gapon #endif
4779c34bbd2aSEdward Tomasz Napierala 
4780c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4781c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4782c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4783c34bbd2aSEdward Tomasz Napierala }
4784c34bbd2aSEdward Tomasz Napierala 
4785a7ad07bfSEdward Tomasz Napierala static void
4786a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4787a7ad07bfSEdward Tomasz Napierala {
4788c34bbd2aSEdward Tomasz Napierala 
47894b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4790c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4791c34bbd2aSEdward Tomasz Napierala 
4792af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4793af3c786cSMateusz Guzik 		return;
4794a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4795a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4796a7ad07bfSEdward Tomasz Napierala }
4797a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4798a7ad07bfSEdward Tomasz Napierala 
4799413628a7SBjoern A. Zeeb #ifdef DDB
4800b38ff370SJamie Gritton 
4801b38ff370SJamie Gritton static void
4802b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4803413628a7SBjoern A. Zeeb {
4804672756aaSJamie Gritton 	struct bool_flags *bf;
4805672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4806b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4807b38ff370SJamie Gritton 	int ii;
4808413628a7SBjoern A. Zeeb #endif
4809672756aaSJamie Gritton 	unsigned f;
48108144690aSEric van Gyzen #ifdef INET
48118144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
48128144690aSEric van Gyzen #endif
4813413628a7SBjoern A. Zeeb #ifdef INET6
4814413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4815413628a7SBjoern A. Zeeb #endif
4816413628a7SBjoern A. Zeeb 
4817b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4818b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4819b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
48200304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4821b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4822b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
48231158508aSJamie Gritton 	db_printf(" state           = %s\n",
48241158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
48251158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_DYING ? "dying" :
48261158508aSJamie Gritton 	    "invalid");
4827b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4828b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4829b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4830679e1390SJamie Gritton #ifdef VIMAGE
4831679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4832679e1390SJamie Gritton #endif
4833b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4834b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
48350cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4836fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4837fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
48380304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
48390304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4840fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
4841672756aaSJamie Gritton 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4842672756aaSJamie Gritton 		if (pr->pr_flags & bf->flag)
4843672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4844672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
4845672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4846672756aaSJamie Gritton 	     jsf++) {
4847672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
4848672756aaSJamie Gritton 		db_printf(" %-16s= %s\n", jsf->name,
4849672756aaSJamie Gritton 		    (f != 0 && f == jsf->disable) ? "disable"
4850672756aaSJamie Gritton 		    : (f == jsf->new) ? "new"
48517cbf7213SJamie Gritton 		    : "inherit");
48527cbf7213SJamie Gritton 	}
4853fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
4854672756aaSJamie Gritton 	for (bf = pr_flag_allow;
48555d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
48565d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
4857672756aaSJamie Gritton 	     bf++)
4858672756aaSJamie Gritton 		if (pr->pr_allow & bf->flag)
4859672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4860b38ff370SJamie Gritton 	db_printf("\n");
48610304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4862c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4863c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4864c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
486576ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4866413628a7SBjoern A. Zeeb #ifdef INET
4867eb8dcdeaSGleb Smirnoff 	if (pr->pr_addrs[PR_INET] != NULL) {
4868eb8dcdeaSGleb Smirnoff 		pr_family_t af = PR_INET;
4869eb8dcdeaSGleb Smirnoff 
4870eb8dcdeaSGleb Smirnoff 		db_printf(" ip4s            = %d\n", pr->pr_addrs[af]->ips);
4871eb8dcdeaSGleb Smirnoff 		for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4872b38ff370SJamie Gritton 			db_printf(" %s %s\n",
4873fe0518e9SBjoern A. Zeeb 			    ii == 0 ? "ip4.addr        =" : "                 ",
4874eb8dcdeaSGleb Smirnoff 			    inet_ntoa_r(
4875eb8dcdeaSGleb Smirnoff 			    *(const struct in_addr *)PR_IP(pr, ii),
4876eb8dcdeaSGleb Smirnoff 			    ip4buf));
4877eb8dcdeaSGleb Smirnoff 	}
4878413628a7SBjoern A. Zeeb #endif
4879413628a7SBjoern A. Zeeb #ifdef INET6
4880eb8dcdeaSGleb Smirnoff 	if (pr->pr_addrs[PR_INET6] != NULL) {
4881eb8dcdeaSGleb Smirnoff 		pr_family_t af = PR_INET6;
4882eb8dcdeaSGleb Smirnoff 
4883eb8dcdeaSGleb Smirnoff 		db_printf(" ip6s            = %d\n", pr->pr_addrs[af]->ips);
4884eb8dcdeaSGleb Smirnoff 		for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4885b38ff370SJamie Gritton 			db_printf(" %s %s\n",
4886fe0518e9SBjoern A. Zeeb 			    ii == 0 ? "ip6.addr        =" : "                 ",
4887eb8dcdeaSGleb Smirnoff 			    ip6_sprintf(ip6buf,
4888eb8dcdeaSGleb Smirnoff 			    (const struct in6_addr *)PR_IP(pr, ii)));
4889eb8dcdeaSGleb Smirnoff 	}
4890b38ff370SJamie Gritton #endif
4891b38ff370SJamie Gritton }
4892b38ff370SJamie Gritton 
4893b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4894b38ff370SJamie Gritton {
4895b38ff370SJamie Gritton 	struct prison *pr;
4896b38ff370SJamie Gritton 
4897b38ff370SJamie Gritton 	if (!have_addr) {
48980304c731SJamie Gritton 		/*
48990304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
49000304c731SJamie Gritton 		 * listed.
49010304c731SJamie Gritton 		 */
49020304c731SJamie Gritton 		db_show_prison(&prison0);
49030304c731SJamie Gritton 		if (!db_pager_quit) {
4904b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4905b38ff370SJamie Gritton 				db_show_prison(pr);
4906413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4907413628a7SBjoern A. Zeeb 					break;
4908413628a7SBjoern A. Zeeb 			}
49090304c731SJamie Gritton 		}
4910b38ff370SJamie Gritton 		return;
4911413628a7SBjoern A. Zeeb 	}
4912b38ff370SJamie Gritton 
49130304c731SJamie Gritton 	if (addr == 0)
49140304c731SJamie Gritton 		pr = &prison0;
49150304c731SJamie Gritton 	else {
4916b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4917b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4918b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4919b38ff370SJamie Gritton 				break;
4920b38ff370SJamie Gritton 		if (pr == NULL)
4921b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4922b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4923b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4924b38ff370SJamie Gritton 					break;
4925b38ff370SJamie Gritton 		if (pr == NULL)
4926b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4927b38ff370SJamie Gritton 			pr = (struct prison *)addr;
49280304c731SJamie Gritton 	}
4929b38ff370SJamie Gritton 	db_show_prison(pr);
4930b38ff370SJamie Gritton }
4931b38ff370SJamie Gritton 
4932413628a7SBjoern A. Zeeb #endif /* DDB */
4933