xref: /freebsd/sys/kern/kern_jail.c (revision 8771ff75384dec8c9f95ce25b6ca9a639c4b208c)
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>
48b3059e09SRobert Watson #include <sys/taskqueue.h>
4957b4252eSKonstantin Belousov #include <sys/fcntl.h>
5075c13541SPoul-Henning Kamp #include <sys/jail.h>
51c3188289SKyle Evans #include <sys/linker.h>
5201137630SRobert Watson #include <sys/lock.h>
5301137630SRobert Watson #include <sys/mutex.h>
54097055e2SEdward Tomasz Napierala #include <sys/racct.h>
55f87beb93SAndriy Gapon #include <sys/rctl.h>
56a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
57dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5876ca6f88SJamie Gritton #include <sys/sysent.h>
59fd7a8150SMike Barcroft #include <sys/namei.h>
60820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
61fd7a8150SMike Barcroft #include <sys/queue.h>
6275c13541SPoul-Henning Kamp #include <sys/socket.h>
63fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6483f1e257SRobert Watson #include <sys/sysctl.h>
65c3188289SKyle Evans #include <sys/uuid.h>
66fd7a8150SMike Barcroft #include <sys/vnode.h>
67530c0060SRobert Watson 
6875c13541SPoul-Henning Kamp #include <net/if.h>
69530c0060SRobert Watson #include <net/vnet.h>
70530c0060SRobert Watson 
7175c13541SPoul-Henning Kamp #include <netinet/in.h>
72530c0060SRobert Watson 
73413628a7SBjoern A. Zeeb #ifdef DDB
74413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
75413628a7SBjoern A. Zeeb #endif /* DDB */
7675c13541SPoul-Henning Kamp 
77aed55708SRobert Watson #include <security/mac/mac_framework.h>
78aed55708SRobert Watson 
79c3188289SKyle Evans #define	PRISON0_HOSTUUID_MODULE	"hostuuid"
808986e3a0SJamie Gritton 
8175c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
82d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8375c13541SPoul-Henning Kamp 
84592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
85592bcae8SBjoern A. Zeeb #ifdef INET
86592bcae8SBjoern A. Zeeb #ifdef INET6
87592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
88592bcae8SBjoern A. Zeeb #else
89592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
90592bcae8SBjoern A. Zeeb #endif
91592bcae8SBjoern A. Zeeb #else /* !INET */
92592bcae8SBjoern A. Zeeb #ifdef INET6
93592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
94592bcae8SBjoern A. Zeeb #else
95592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
96592bcae8SBjoern A. Zeeb #endif
97592bcae8SBjoern A. Zeeb #endif
98592bcae8SBjoern A. Zeeb 
990304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
1000304c731SJamie Gritton struct prison prison0 = {
1010304c731SJamie Gritton 	.pr_id		= 0,
1020304c731SJamie Gritton 	.pr_name	= "0",
1030304c731SJamie Gritton 	.pr_ref		= 1,
1040304c731SJamie Gritton 	.pr_uref	= 1,
1050304c731SJamie Gritton 	.pr_path	= "/",
1060304c731SJamie Gritton 	.pr_securelevel	= -1,
1070cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
1081158508aSJamie Gritton 	.pr_state	= PRISON_STATE_ALIVE,
109b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1108986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
11113e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
112eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
113592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
114eb79e1c7SBjoern A. Zeeb #else
115592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
116eb79e1c7SBjoern A. Zeeb #endif
1170e5c6bd4SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL_STATIC,
1180304c731SJamie Gritton };
1190304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
12083f1e257SRobert Watson 
121672756aaSJamie Gritton struct bool_flags {
122672756aaSJamie Gritton 	const char	*name;
123672756aaSJamie Gritton 	const char	*noname;
1245d58f959SJamie Gritton 	volatile u_int	 flag;
125672756aaSJamie Gritton };
126672756aaSJamie Gritton struct jailsys_flags {
127672756aaSJamie Gritton 	const char	*name;
128672756aaSJamie Gritton 	unsigned	 disable;
129672756aaSJamie Gritton 	unsigned	 new;
130672756aaSJamie Gritton };
131672756aaSJamie Gritton 
132a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
133dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
134b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
135b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
136a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1372110d913SXin LI int	lastprid = 0;
138fd7a8150SMike Barcroft 
1397de883c8SJamie Gritton static int get_next_prid(struct prison **insprp);
140f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
141b3059e09SRobert Watson static void prison_complete(void *context, int pending);
142b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
143c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
144f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags);
145f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr);
146c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr);
1470fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag,
1480fe74ae6SJamie Gritton     int enable);
1490304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
150a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
151a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
152c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
153a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
154a7ad07bfSEdward Tomasz Napierala #endif
155fd7a8150SMike Barcroft 
156b38ff370SJamie Gritton /* Flags for prison_deref */
1572a4b2251SJamie Gritton #define	PD_DEREF	0x01	/* Decrement pr_ref */
1582a4b2251SJamie Gritton #define	PD_DEUREF	0x02	/* Decrement pr_uref */
159c861373bSJamie Gritton #define	PD_KILL		0x04	/* Remove jail, kill processes, etc */
160c861373bSJamie Gritton #define	PD_LOCKED	0x10	/* pr_mtx is held */
161c861373bSJamie Gritton #define	PD_LIST_SLOCKED	0x20	/* allprison_lock is held shared */
162c861373bSJamie Gritton #define	PD_LIST_XLOCKED	0x40	/* allprison_lock is held exclusive */
163589e4c1dSJamie Gritton #define PD_OP_FLAGS	0x07	/* Operation flags */
164589e4c1dSJamie Gritton #define PD_LOCK_FLAGS	0x70	/* Lock status flags */
165fd7a8150SMike Barcroft 
1660304c731SJamie Gritton /*
1675cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1685cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1695cc70397SBjoern A. Zeeb  * terminating entry.
1700304c731SJamie Gritton  */
171672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = {
172672756aaSJamie Gritton 	{"persist", "nopersist", PR_PERSIST},
173592bcae8SBjoern A. Zeeb #ifdef INET
174672756aaSJamie Gritton 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
175592bcae8SBjoern A. Zeeb #endif
176592bcae8SBjoern A. Zeeb #ifdef INET6
177672756aaSJamie Gritton 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
178592bcae8SBjoern A. Zeeb #endif
1790304c731SJamie Gritton };
180672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
1810304c731SJamie Gritton 
182672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = {
1837cbf7213SJamie Gritton 	{"host", 0, PR_HOST},
1847cbf7213SJamie Gritton #ifdef VIMAGE
1857cbf7213SJamie Gritton 	{"vnet", 0, PR_VNET},
1867cbf7213SJamie Gritton #endif
1870304c731SJamie Gritton #ifdef INET
1886a3f2779SJamie Gritton 	{"ip4", PR_IP4_USER, PR_IP4_USER},
1890304c731SJamie Gritton #endif
1900304c731SJamie Gritton #ifdef INET6
1916a3f2779SJamie Gritton 	{"ip6", PR_IP6_USER, PR_IP6_USER},
192679e1390SJamie Gritton #endif
1930304c731SJamie Gritton };
1945cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1950304c731SJamie Gritton 
1965d58f959SJamie Gritton /*
1975d58f959SJamie Gritton  * Make this array full-size so dynamic parameters can be added.
1985d58f959SJamie Gritton  * It is protected by prison0.mtx, but lockless reading is allowed
1995d58f959SJamie Gritton  * with an atomic check of the flag values.
2005d58f959SJamie Gritton  */
2010e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
202672756aaSJamie Gritton 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
203672756aaSJamie Gritton 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
204672756aaSJamie Gritton 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
205672756aaSJamie Gritton 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
206672756aaSJamie Gritton 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
207672756aaSJamie Gritton 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
208672756aaSJamie Gritton 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
209ccd6ac9fSAntoine Brodin 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
210672756aaSJamie Gritton 	{"allow.reserved_ports", "allow.noreserved_ports",
211672756aaSJamie Gritton 	 PR_ALLOW_RESERVED_PORTS},
212b19d66fdSJamie Gritton 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
213b3079544SJamie Gritton 	{"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
214b3079544SJamie Gritton 	 PR_ALLOW_UNPRIV_DEBUG},
21505e1e482SMariusz Zaborski 	{"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
2160304c731SJamie Gritton };
2175d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
218672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
2190304c731SJamie Gritton 
220b3079544SJamie Gritton #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | \
221b3079544SJamie Gritton 					 PR_ALLOW_RESERVED_PORTS | \
22205e1e482SMariusz Zaborski 					 PR_ALLOW_UNPRIV_DEBUG | \
22305e1e482SMariusz Zaborski 					 PR_ALLOW_SUSER)
22442bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
225bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2260304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
22742bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2280cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2290304c731SJamie Gritton #if defined(INET) || defined(INET6)
230e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2310304c731SJamie Gritton #endif
2320304c731SJamie Gritton 
233b96bd95bSIan Lepore /*
234b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
235b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
236b96bd95bSIan Lepore  */
237b96bd95bSIan Lepore void
238b96bd95bSIan Lepore prison0_init(void)
239b96bd95bSIan Lepore {
240c3188289SKyle Evans 	uint8_t *file, *data;
241c3188289SKyle Evans 	size_t size;
242b96bd95bSIan Lepore 
243b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
244b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
245b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
246c3188289SKyle Evans 
247c3188289SKyle Evans 	/* If we have a preloaded hostuuid, use it. */
248c3188289SKyle Evans 	file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
249c3188289SKyle Evans 	if (file != NULL) {
250c3188289SKyle Evans 		data = preload_fetch_addr(file);
251c3188289SKyle Evans 		size = preload_fetch_size(file);
252c3188289SKyle Evans 		if (data != NULL) {
253c3188289SKyle Evans 			/*
254c3188289SKyle Evans 			 * The preloaded data may include trailing whitespace, almost
255c3188289SKyle Evans 			 * certainly a newline; skip over any whitespace or
256c3188289SKyle Evans 			 * non-printable characters to be safe.
257c3188289SKyle Evans 			 */
258c3188289SKyle Evans 			while (size > 0 && data[size - 1] <= 0x20) {
259b6be9566SColin Percival 				size--;
260c3188289SKyle Evans 			}
261c3188289SKyle Evans 			if (validate_uuid(data, size, NULL, 0) == 0) {
262c3188289SKyle Evans 				(void)strlcpy(prison0.pr_hostuuid, data,
263c3188289SKyle Evans 				    size + 1);
264c3188289SKyle Evans 			} else if (bootverbose) {
265330f110bSColin Percival 				printf("hostuuid: preload data malformed: '%.*s'\n",
266330f110bSColin Percival 				    (int)size, data);
267c3188289SKyle Evans 			}
268c3188289SKyle Evans 		}
269c3188289SKyle Evans 	}
270c3188289SKyle Evans 	if (bootverbose)
271c3188289SKyle Evans 		printf("hostuuid: using %s\n", prison0.pr_hostuuid);
272b96bd95bSIan Lepore }
273b96bd95bSIan Lepore 
274116734c4SMatthew Dillon /*
2759ddb7954SMike Barcroft  * struct jail_args {
2769ddb7954SMike Barcroft  *	struct jail *jail;
2779ddb7954SMike Barcroft  * };
278116734c4SMatthew Dillon  */
27975c13541SPoul-Henning Kamp int
280c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap)
28175c13541SPoul-Henning Kamp {
282413628a7SBjoern A. Zeeb 	uint32_t version;
283413628a7SBjoern A. Zeeb 	int error;
2840304c731SJamie Gritton 	struct jail j;
285413628a7SBjoern A. Zeeb 
286413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
287413628a7SBjoern A. Zeeb 	if (error)
288413628a7SBjoern A. Zeeb 		return (error);
289413628a7SBjoern A. Zeeb 
290413628a7SBjoern A. Zeeb 	switch (version) {
291413628a7SBjoern A. Zeeb 	case 0:
292413628a7SBjoern A. Zeeb 	{
293413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
294413628a7SBjoern A. Zeeb 
2950304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2960304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
297413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
298413628a7SBjoern A. Zeeb 		if (error)
299413628a7SBjoern A. Zeeb 			return (error);
3000304c731SJamie Gritton 		j.version = j0.version;
3010304c731SJamie Gritton 		j.path = j0.path;
3020304c731SJamie Gritton 		j.hostname = j0.hostname;
303b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
304413628a7SBjoern A. Zeeb 		break;
305413628a7SBjoern A. Zeeb 	}
306413628a7SBjoern A. Zeeb 
307413628a7SBjoern A. Zeeb 	case 1:
308413628a7SBjoern A. Zeeb 		/*
309413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
310413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
311413628a7SBjoern A. Zeeb 		 */
312413628a7SBjoern A. Zeeb 		return (EINVAL);
313413628a7SBjoern A. Zeeb 
314413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
315413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
316413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
317413628a7SBjoern A. Zeeb 		if (error)
318413628a7SBjoern A. Zeeb 			return (error);
3190304c731SJamie Gritton 		break;
3200304c731SJamie Gritton 
3210304c731SJamie Gritton 	default:
3220304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3230304c731SJamie Gritton 		return (EINVAL);
3240304c731SJamie Gritton 	}
325c542c43eSJamie Gritton 	return (kern_jail(td, &j));
3260304c731SJamie Gritton }
3270304c731SJamie Gritton 
3280304c731SJamie Gritton int
329c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j)
3300304c731SJamie Gritton {
331c542c43eSJamie Gritton 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
332e92e0574SJamie Gritton #ifdef INET
333e92e0574SJamie Gritton 			    + 1
334e92e0574SJamie Gritton #endif
335e92e0574SJamie Gritton #ifdef INET6
336e92e0574SJamie Gritton 			    + 1
337e92e0574SJamie Gritton #endif
338e92e0574SJamie Gritton 			    )];
3390304c731SJamie Gritton 	struct uio opt;
3400304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
341672756aaSJamie Gritton 	struct bool_flags *bf;
3420304c731SJamie Gritton #ifdef INET
343e92e0574SJamie Gritton 	uint32_t ip4s;
3440304c731SJamie Gritton 	struct in_addr *u_ip4;
3450304c731SJamie Gritton #endif
3460304c731SJamie Gritton #ifdef INET6
3470304c731SJamie Gritton 	struct in6_addr *u_ip6;
3480304c731SJamie Gritton #endif
3490304c731SJamie Gritton 	size_t tmplen;
350c542c43eSJamie Gritton 	int error, enforce_statfs;
3510304c731SJamie Gritton 
3520304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3530304c731SJamie Gritton 	opt.uio_iov = optiov;
3540304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3550304c731SJamie Gritton 	opt.uio_offset = -1;
3560304c731SJamie Gritton 	opt.uio_resid = -1;
3570304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3580304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3590304c731SJamie Gritton 	opt.uio_td = td;
3600304c731SJamie Gritton 
3610304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3620304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
363672756aaSJamie Gritton 		for (bf = pr_flag_allow;
3640e5c6bd4SJamie Gritton 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
3655d58f959SJamie Gritton 			atomic_load_int(&bf->flag) != 0;
366672756aaSJamie Gritton 		     bf++) {
367672756aaSJamie Gritton 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
368672756aaSJamie Gritton 			    (jail_default_allow & bf->flag)
369672756aaSJamie Gritton 			    ? bf->name : bf->noname);
3700304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3710304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3720304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3730304c731SJamie Gritton 		}
3740304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3750304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3760304c731SJamie Gritton 		opt.uio_iovcnt++;
3770304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3780304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3790304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3800304c731SJamie Gritton 		opt.uio_iovcnt++;
3810304c731SJamie Gritton 	}
3820304c731SJamie Gritton 
383b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
384b38ff370SJamie Gritton #ifdef INET
3850304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3860304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
387b38ff370SJamie Gritton 		return (EINVAL);
3880304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
389b38ff370SJamie Gritton #else
3900304c731SJamie Gritton 	if (j->ip4s > 0)
391b38ff370SJamie Gritton 		return (EINVAL);
392b38ff370SJamie Gritton #endif
393b38ff370SJamie Gritton #ifdef INET6
3940304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
395b38ff370SJamie Gritton 		return (EINVAL);
3960304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
397b38ff370SJamie Gritton #else
3980304c731SJamie Gritton 	if (j->ip6s > 0)
399b38ff370SJamie Gritton 		return (EINVAL);
400b38ff370SJamie Gritton #endif
401b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
402b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
403b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
404b38ff370SJamie Gritton #ifdef INET
405b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
406b38ff370SJamie Gritton #endif
407b38ff370SJamie Gritton #ifdef INET6
408b38ff370SJamie Gritton #ifdef INET
4090304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
410b38ff370SJamie Gritton #else
411b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
412b38ff370SJamie Gritton #endif
413b38ff370SJamie Gritton #endif
4140304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4150304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4160304c731SJamie Gritton 	opt.uio_iovcnt++;
4170304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4180304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4190304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
420b38ff370SJamie Gritton 	if (error) {
421b38ff370SJamie Gritton 		free(u_path, M_TEMP);
422b38ff370SJamie Gritton 		return (error);
423b38ff370SJamie Gritton 	}
4240304c731SJamie Gritton 	opt.uio_iovcnt++;
4250304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4260304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4270304c731SJamie Gritton 	opt.uio_iovcnt++;
4280304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4290304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4300304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
431b38ff370SJamie Gritton 	if (error) {
432b38ff370SJamie Gritton 		free(u_path, M_TEMP);
433b38ff370SJamie Gritton 		return (error);
434b38ff370SJamie Gritton 	}
4350304c731SJamie Gritton 	opt.uio_iovcnt++;
4360304c731SJamie Gritton 	if (j->jailname != NULL) {
437b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
438b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
439b38ff370SJamie Gritton 		opt.uio_iovcnt++;
440b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4410304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
442b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
443b38ff370SJamie Gritton 		if (error) {
444b38ff370SJamie Gritton 			free(u_path, M_TEMP);
445b38ff370SJamie Gritton 			return (error);
446b38ff370SJamie Gritton 		}
447b38ff370SJamie Gritton 		opt.uio_iovcnt++;
448b38ff370SJamie Gritton 	}
449b38ff370SJamie Gritton #ifdef INET
450b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
451b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
452b38ff370SJamie Gritton 	opt.uio_iovcnt++;
453b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4540304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4550304c731SJamie Gritton 	if (j->version == 0)
4560304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4570304c731SJamie Gritton 	else {
4580304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, 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 	}
464b38ff370SJamie Gritton 	opt.uio_iovcnt++;
465b38ff370SJamie Gritton #endif
466b38ff370SJamie Gritton #ifdef INET6
467b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
468b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
469b38ff370SJamie Gritton 	opt.uio_iovcnt++;
470b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4710304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4720304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
473b38ff370SJamie Gritton 	if (error) {
474b38ff370SJamie Gritton 		free(u_path, M_TEMP);
475b38ff370SJamie Gritton 		return (error);
476b38ff370SJamie Gritton 	}
477b38ff370SJamie Gritton 	opt.uio_iovcnt++;
478b38ff370SJamie Gritton #endif
47902abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
4800304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
481b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
482b38ff370SJamie Gritton 	free(u_path, M_TEMP);
483b38ff370SJamie Gritton 	return (error);
484b38ff370SJamie Gritton }
485b38ff370SJamie Gritton 
486b38ff370SJamie Gritton /*
487b38ff370SJamie Gritton  * struct jail_set_args {
488b38ff370SJamie Gritton  *	struct iovec *iovp;
489b38ff370SJamie Gritton  *	unsigned int iovcnt;
490b38ff370SJamie Gritton  *	int flags;
491b38ff370SJamie Gritton  * };
492b38ff370SJamie Gritton  */
493b38ff370SJamie Gritton int
4948451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
495b38ff370SJamie Gritton {
496b38ff370SJamie Gritton 	struct uio *auio;
497b38ff370SJamie Gritton 	int error;
498b38ff370SJamie Gritton 
499b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
500b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
501b38ff370SJamie Gritton 		return (EINVAL);
502b38ff370SJamie Gritton 
503b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
504b38ff370SJamie Gritton 	if (error)
505b38ff370SJamie Gritton 		return (error);
506b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
507b38ff370SJamie Gritton 	free(auio, M_IOV);
508b38ff370SJamie Gritton 	return (error);
509413628a7SBjoern A. Zeeb }
510413628a7SBjoern A. Zeeb 
511413628a7SBjoern A. Zeeb int
512b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
513413628a7SBjoern A. Zeeb {
514fd7a8150SMike Barcroft 	struct nameidata nd;
515b38ff370SJamie Gritton #ifdef INET
516b38ff370SJamie Gritton 	struct in_addr *ip4;
517413628a7SBjoern A. Zeeb #endif
518b38ff370SJamie Gritton #ifdef INET6
519b38ff370SJamie Gritton 	struct in6_addr *ip6;
520b38ff370SJamie Gritton #endif
521b38ff370SJamie Gritton 	struct vfsopt *opt;
522b38ff370SJamie Gritton 	struct vfsoptlist *opts;
5237de883c8SJamie Gritton 	struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
524b38ff370SJamie Gritton 	struct vnode *root;
525babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
526b96bd95bSIan Lepore 	char *g_path, *osrelstr;
527672756aaSJamie Gritton 	struct bool_flags *bf;
528672756aaSJamie Gritton 	struct jailsys_flags *jsf;
5290304c731SJamie Gritton #if defined(INET) || defined(INET6)
53057aea6dfSBjoern A. Zeeb 	struct prison *tppr;
531b38ff370SJamie Gritton 	void *op;
5320304c731SJamie Gritton #endif
53376ca6f88SJamie Gritton 	unsigned long hid;
534b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
5352a4b2251SJamie Gritton 	int born, created, cuflags, descend, drflags, enforce;
536cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
5370cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
538672756aaSJamie Gritton 	int jid, jsys, len, level;
539b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
540d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
5410304c731SJamie Gritton 	int ii, ij;
542413628a7SBjoern A. Zeeb #endif
543413628a7SBjoern A. Zeeb #ifdef INET
5442b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
545413628a7SBjoern A. Zeeb #endif
546b38ff370SJamie Gritton #ifdef INET6
5472b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
548b38ff370SJamie Gritton #endif
5496beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
550b3079544SJamie Gritton 	uint64_t pr_allow_diff;
5516beb3bb4SKirk McKusick 	unsigned tallow;
552b38ff370SJamie Gritton 	char numbuf[12];
553b38ff370SJamie Gritton 
554b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
555b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
556b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
557b38ff370SJamie Gritton 	if (error)
55875c13541SPoul-Henning Kamp 		return (error);
559b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
560b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5610304c731SJamie Gritton 		return (EPERM);
562b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
563b38ff370SJamie Gritton 		return (EINVAL);
564b38ff370SJamie Gritton 
565b38ff370SJamie Gritton 	/*
566b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
567b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
568b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
569b38ff370SJamie Gritton 	 * the allprison lock.
570b38ff370SJamie Gritton 	 *
571b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
572b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
573b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
574b38ff370SJamie Gritton 	 */
575b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
576b38ff370SJamie Gritton 	if (error)
577b38ff370SJamie Gritton 		return (error);
578b38ff370SJamie Gritton #ifdef INET
579b38ff370SJamie Gritton 	ip4 = NULL;
580b38ff370SJamie Gritton #endif
581b38ff370SJamie Gritton #ifdef INET6
582b38ff370SJamie Gritton 	ip6 = NULL;
583b38ff370SJamie Gritton #endif
5846dfe0a3dSMartin Matuska 	g_path = NULL;
585b38ff370SJamie Gritton 
586b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
587b6f47c23SJamie Gritton 	if (!cuflags) {
588b6f47c23SJamie Gritton 		error = EINVAL;
589b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
590b6f47c23SJamie Gritton 		goto done_errmsg;
591b6f47c23SJamie Gritton 	}
592b6f47c23SJamie Gritton 
593b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
594b38ff370SJamie Gritton 	if (error == ENOENT)
595b38ff370SJamie Gritton 		jid = 0;
596b38ff370SJamie Gritton 	else if (error != 0)
597b38ff370SJamie Gritton 		goto done_free;
598b38ff370SJamie Gritton 
599b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
600b38ff370SJamie Gritton 	if (error == ENOENT)
601b38ff370SJamie Gritton 		gotslevel = 0;
602b38ff370SJamie Gritton 	else if (error != 0)
603b38ff370SJamie Gritton 		goto done_free;
604b38ff370SJamie Gritton 	else
605b38ff370SJamie Gritton 		gotslevel = 1;
606b38ff370SJamie Gritton 
607b97457e2SJamie Gritton 	error =
608b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
609b97457e2SJamie Gritton 	if (error == ENOENT)
610b97457e2SJamie Gritton 		gotchildmax = 0;
611b97457e2SJamie Gritton 	else if (error != 0)
612b97457e2SJamie Gritton 		goto done_free;
613b97457e2SJamie Gritton 	else
614b97457e2SJamie Gritton 		gotchildmax = 1;
615b97457e2SJamie Gritton 
6160304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
617f337198dSJamie Gritton 	if (error == ENOENT)
618f337198dSJamie Gritton 		gotenforce = 0;
619f337198dSJamie Gritton 	else if (error != 0)
6200304c731SJamie Gritton 		goto done_free;
621f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
622f337198dSJamie Gritton 		error = EINVAL;
623f337198dSJamie Gritton 		goto done_free;
624f337198dSJamie Gritton 	} else
625f337198dSJamie Gritton 		gotenforce = 1;
6260304c731SJamie Gritton 
6270cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
6280cc207a6SMartin Matuska 	if (error == ENOENT)
6290cc207a6SMartin Matuska 		gotrsnum = 0;
6300cc207a6SMartin Matuska 	else if (error != 0)
6310cc207a6SMartin Matuska 		goto done_free;
6320cc207a6SMartin Matuska 	else
6330cc207a6SMartin Matuska 		gotrsnum = 1;
6340cc207a6SMartin Matuska 
635b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
636672756aaSJamie Gritton 	for (bf = pr_flag_bool;
637672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
638672756aaSJamie Gritton 	     bf++) {
639672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
640672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
6410304c731SJamie Gritton 	}
642b38ff370SJamie Gritton 	ch_flags |= pr_flags;
643672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
644672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
645672756aaSJamie Gritton 	     jsf++) {
646672756aaSJamie Gritton 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
6477cbf7213SJamie Gritton 		if (error == ENOENT)
6487cbf7213SJamie Gritton 			continue;
6497cbf7213SJamie Gritton 		if (error != 0)
6507cbf7213SJamie Gritton 			goto done_free;
6517cbf7213SJamie Gritton 		switch (jsys) {
6527cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
653672756aaSJamie Gritton 			if (!jsf->disable) {
6547cbf7213SJamie Gritton 				error = EINVAL;
6557cbf7213SJamie Gritton 				goto done_free;
6567cbf7213SJamie Gritton 			}
657672756aaSJamie Gritton 			pr_flags |= jsf->disable;
6587cbf7213SJamie Gritton 			break;
6597cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
660672756aaSJamie Gritton 			pr_flags |= jsf->new;
6617cbf7213SJamie Gritton 			break;
6627cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6637cbf7213SJamie Gritton 			break;
6647cbf7213SJamie Gritton 		default:
6657cbf7213SJamie Gritton 			error = EINVAL;
6667cbf7213SJamie Gritton 			goto done_free;
6677cbf7213SJamie Gritton 		}
668672756aaSJamie Gritton 		ch_flags |= jsf->new | jsf->disable;
6697cbf7213SJamie Gritton 	}
6701158508aSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
6714affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6724affa14cSJamie Gritton 		error = EINVAL;
6734affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6744affa14cSJamie Gritton 		goto done_errmsg;
6754affa14cSJamie Gritton 	}
676679e1390SJamie Gritton #ifdef VIMAGE
677679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
678679e1390SJamie Gritton 		error = EINVAL;
679679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
680679e1390SJamie Gritton 		goto done_errmsg;
681679e1390SJamie Gritton 	}
682679e1390SJamie Gritton #endif
6832b0d6f81SJamie Gritton #ifdef INET
6842b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
6852b0d6f81SJamie Gritton 		error = EINVAL;
6862b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
6872b0d6f81SJamie Gritton 		goto done_errmsg;
6882b0d6f81SJamie Gritton 	}
6892b0d6f81SJamie Gritton #endif
6902b0d6f81SJamie Gritton #ifdef INET6
6912b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
6922b0d6f81SJamie Gritton 		error = EINVAL;
6932b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
6942b0d6f81SJamie Gritton 		goto done_errmsg;
6952b0d6f81SJamie Gritton 	}
6962b0d6f81SJamie Gritton #endif
697b38ff370SJamie Gritton 
6980304c731SJamie Gritton 	pr_allow = ch_allow = 0;
699672756aaSJamie Gritton 	for (bf = pr_flag_allow;
7005d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
7015d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
702672756aaSJamie Gritton 	     bf++) {
703672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
704672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
7050304c731SJamie Gritton 	}
7060304c731SJamie Gritton 	ch_allow |= pr_allow;
7070304c731SJamie Gritton 
708b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
709b38ff370SJamie Gritton 	if (error == ENOENT)
710b38ff370SJamie Gritton 		name = NULL;
711b38ff370SJamie Gritton 	else if (error != 0)
712b38ff370SJamie Gritton 		goto done_free;
713b38ff370SJamie Gritton 	else {
714b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
715b38ff370SJamie Gritton 			error = EINVAL;
716b38ff370SJamie Gritton 			goto done_free;
717b38ff370SJamie Gritton 		}
718b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
719b38ff370SJamie Gritton 			error = ENAMETOOLONG;
720b38ff370SJamie Gritton 			goto done_free;
721b38ff370SJamie Gritton 		}
722b38ff370SJamie Gritton 	}
723b38ff370SJamie Gritton 
724b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
725b38ff370SJamie Gritton 	if (error == ENOENT)
726b38ff370SJamie Gritton 		host = NULL;
727b38ff370SJamie Gritton 	else if (error != 0)
728b38ff370SJamie Gritton 		goto done_free;
729b38ff370SJamie Gritton 	else {
73076ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
73176ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
732b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
733b38ff370SJamie Gritton 			error = EINVAL;
734b38ff370SJamie Gritton 			goto done_free;
735b38ff370SJamie Gritton 		}
736b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
737b38ff370SJamie Gritton 			error = ENAMETOOLONG;
738b38ff370SJamie Gritton 			goto done_free;
739b38ff370SJamie Gritton 		}
740b38ff370SJamie Gritton 	}
741b38ff370SJamie Gritton 
74276ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
74376ca6f88SJamie Gritton 	if (error == ENOENT)
74476ca6f88SJamie Gritton 		domain = NULL;
74576ca6f88SJamie Gritton 	else if (error != 0)
74676ca6f88SJamie Gritton 		goto done_free;
74776ca6f88SJamie Gritton 	else {
74876ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
74976ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
75076ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
75176ca6f88SJamie Gritton 			error = EINVAL;
75276ca6f88SJamie Gritton 			goto done_free;
75376ca6f88SJamie Gritton 		}
75476ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
75576ca6f88SJamie Gritton 			error = ENAMETOOLONG;
75676ca6f88SJamie Gritton 			goto done_free;
75776ca6f88SJamie Gritton 		}
75876ca6f88SJamie Gritton 	}
75976ca6f88SJamie Gritton 
76076ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
76176ca6f88SJamie Gritton 	if (error == ENOENT)
76276ca6f88SJamie Gritton 		uuid = NULL;
76376ca6f88SJamie Gritton 	else if (error != 0)
76476ca6f88SJamie Gritton 		goto done_free;
76576ca6f88SJamie Gritton 	else {
76676ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
76776ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
76876ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
76976ca6f88SJamie Gritton 			error = EINVAL;
77076ca6f88SJamie Gritton 			goto done_free;
77176ca6f88SJamie Gritton 		}
77276ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
77376ca6f88SJamie Gritton 			error = ENAMETOOLONG;
77476ca6f88SJamie Gritton 			goto done_free;
77576ca6f88SJamie Gritton 		}
77676ca6f88SJamie Gritton 	}
77776ca6f88SJamie Gritton 
778841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
779a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
78076ca6f88SJamie Gritton 		uint32_t hid32;
78176ca6f88SJamie Gritton 
78276ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
78376ca6f88SJamie Gritton 		hid = hid32;
78476ca6f88SJamie Gritton 	} else
78576ca6f88SJamie Gritton #endif
78676ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
78776ca6f88SJamie Gritton 	if (error == ENOENT)
78876ca6f88SJamie Gritton 		gothid = 0;
78976ca6f88SJamie Gritton 	else if (error != 0)
79076ca6f88SJamie Gritton 		goto done_free;
79176ca6f88SJamie Gritton 	else {
79276ca6f88SJamie Gritton 		gothid = 1;
79376ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
79476ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
79576ca6f88SJamie Gritton 	}
79676ca6f88SJamie Gritton 
797b38ff370SJamie Gritton #ifdef INET
798b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
799b38ff370SJamie Gritton 	if (error == ENOENT)
8000e5e396eSJamie Gritton 		ip4s = 0;
801b38ff370SJamie Gritton 	else if (error != 0)
802b38ff370SJamie Gritton 		goto done_free;
803b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
804b38ff370SJamie Gritton 		error = EINVAL;
805b38ff370SJamie Gritton 		goto done_free;
8060304c731SJamie Gritton 	} else {
8076a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
8086a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
8096a3f2779SJamie Gritton 		if (ip4s > 0) {
810b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
811b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
812b38ff370SJamie Gritton 				error = EINVAL;
813b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
814b38ff370SJamie Gritton 				goto done_errmsg;
815b38ff370SJamie Gritton 			}
8162b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
817b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
818b38ff370SJamie Gritton 			/*
8190304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
8200304c731SJamie Gritton 			 * the primary IP address as given from userland.
8210304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
822bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
823bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
824bef916f9SBjoern A. Zeeb 			 * address to connect from.
825b38ff370SJamie Gritton 			 */
826b38ff370SJamie Gritton 			if (ip4s > 1)
8270ce1624dSStephen J. Kiernan 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
8280ce1624dSStephen J. Kiernan 				    prison_qcmp_v4);
829b38ff370SJamie Gritton 			/*
8300304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
8310304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
8320304c731SJamie Gritton 			 * addresses it is their problem.
833b38ff370SJamie Gritton 			 *
8340304c731SJamie Gritton 			 * We do not have to care about byte order for these
8350304c731SJamie Gritton 			 * checks so we will do them in NBO.
836b38ff370SJamie Gritton 			 */
837b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
838b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
839b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
840b38ff370SJamie Gritton 					error = EINVAL;
841b38ff370SJamie Gritton 					goto done_free;
842b38ff370SJamie Gritton 				}
843b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
844b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
845b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
846b38ff370SJamie Gritton 					error = EINVAL;
847b38ff370SJamie Gritton 					goto done_free;
848b38ff370SJamie Gritton 				}
849b38ff370SJamie Gritton 			}
850b38ff370SJamie Gritton 		}
8510304c731SJamie Gritton 	}
852b38ff370SJamie Gritton #endif
853b38ff370SJamie Gritton 
854b38ff370SJamie Gritton #ifdef INET6
855b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
856b38ff370SJamie Gritton 	if (error == ENOENT)
8570e5e396eSJamie Gritton 		ip6s = 0;
858b38ff370SJamie Gritton 	else if (error != 0)
859b38ff370SJamie Gritton 		goto done_free;
860b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
861b38ff370SJamie Gritton 		error = EINVAL;
862b38ff370SJamie Gritton 		goto done_free;
8630304c731SJamie Gritton 	} else {
8646a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
8656a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
8666a3f2779SJamie Gritton 		if (ip6s > 0) {
867b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
868b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
869b38ff370SJamie Gritton 				error = EINVAL;
870b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
871b38ff370SJamie Gritton 				goto done_errmsg;
872b38ff370SJamie Gritton 			}
8732b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
874b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
875b38ff370SJamie Gritton 			if (ip6s > 1)
8760ce1624dSStephen J. Kiernan 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
8770ce1624dSStephen J. Kiernan 				    prison_qcmp_v6);
878b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8790304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
880b38ff370SJamie Gritton 					error = EINVAL;
881b38ff370SJamie Gritton 					goto done_free;
882b38ff370SJamie Gritton 				}
883b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
884b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
885b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
886b38ff370SJamie Gritton 				{
887b38ff370SJamie Gritton 					error = EINVAL;
888b38ff370SJamie Gritton 					goto done_free;
889b38ff370SJamie Gritton 				}
890b38ff370SJamie Gritton 			}
891b38ff370SJamie Gritton 		}
8920304c731SJamie Gritton 	}
893b38ff370SJamie Gritton #endif
894b38ff370SJamie Gritton 
895bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
896bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
897bdfc8cc4SJamie Gritton 		error = EINVAL;
898bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
899bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
900bdfc8cc4SJamie Gritton 		goto done_errmsg;
901bdfc8cc4SJamie Gritton 	}
902bdfc8cc4SJamie Gritton #endif
903bdfc8cc4SJamie Gritton 
904cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
905cf0313c6SJamie Gritton 	if (error == ENOENT)
906cf0313c6SJamie Gritton 		osrelstr = NULL;
907cf0313c6SJamie Gritton 	else if (error != 0)
908cf0313c6SJamie Gritton 		goto done_free;
909cf0313c6SJamie Gritton 	else {
910cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
911cf0313c6SJamie Gritton 			error = EINVAL;
912cf0313c6SJamie Gritton 			vfs_opterror(opts,
913cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
914cf0313c6SJamie Gritton 			goto done_errmsg;
915cf0313c6SJamie Gritton 		}
9161b786d01SBjoern A. Zeeb 		if (len == 0 || osrelstr[len - 1] != '\0') {
917cf0313c6SJamie Gritton 			error = EINVAL;
9181b786d01SBjoern A. Zeeb 			goto done_free;
9191b786d01SBjoern A. Zeeb 		}
9201b786d01SBjoern A. Zeeb 		if (len >= OSRELEASELEN) {
9211b786d01SBjoern A. Zeeb 			error = ENAMETOOLONG;
922cf0313c6SJamie Gritton 			vfs_opterror(opts,
923cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
924cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
925cf0313c6SJamie Gritton 			goto done_errmsg;
926cf0313c6SJamie Gritton 		}
927cf0313c6SJamie Gritton 	}
928cf0313c6SJamie Gritton 
929cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
930cf0313c6SJamie Gritton 	if (error == ENOENT)
931cf0313c6SJamie Gritton 		osreldt = 0;
932cf0313c6SJamie Gritton 	else if (error != 0)
933cf0313c6SJamie Gritton 		goto done_free;
934cf0313c6SJamie Gritton 	else {
935cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
936cf0313c6SJamie Gritton 			error = EINVAL;
937cf0313c6SJamie Gritton 			vfs_opterror(opts,
938cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
939cf0313c6SJamie Gritton 			goto done_errmsg;
940cf0313c6SJamie Gritton 		}
941cf0313c6SJamie Gritton 		if (osreldt == 0) {
942cf0313c6SJamie Gritton 			error = EINVAL;
943cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
944cf0313c6SJamie Gritton 			goto done_errmsg;
945cf0313c6SJamie Gritton 		}
946cf0313c6SJamie Gritton 	}
947cf0313c6SJamie Gritton 
948b38ff370SJamie Gritton 	root = NULL;
949b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
950b38ff370SJamie Gritton 	if (error == ENOENT)
951b38ff370SJamie Gritton 		path = NULL;
952b38ff370SJamie Gritton 	else if (error != 0)
953b38ff370SJamie Gritton 		goto done_free;
954b38ff370SJamie Gritton 	else {
955b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
956b38ff370SJamie Gritton 			error = EINVAL;
957b38ff370SJamie Gritton 			vfs_opterror(opts,
958b38ff370SJamie Gritton 			    "path cannot be changed after creation");
959b38ff370SJamie Gritton 			goto done_errmsg;
960b38ff370SJamie Gritton 		}
961b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
962b38ff370SJamie Gritton 			error = EINVAL;
963b38ff370SJamie Gritton 			goto done_free;
964b38ff370SJamie Gritton 		}
9655050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
966b38ff370SJamie Gritton 		    path, td);
967b38ff370SJamie Gritton 		error = namei(&nd);
968b38ff370SJamie Gritton 		if (error)
969b38ff370SJamie Gritton 			goto done_free;
970b38ff370SJamie Gritton 		root = nd.ni_vp;
971b38ff370SJamie Gritton 		NDFREE(&nd, NDF_ONLY_PNBUF);
9726dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
9736dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
9746dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
9753eb6b656SMateusz Guzik 		if (error == 0) {
9766dfe0a3dSMartin Matuska 			path = g_path;
9776dfe0a3dSMartin Matuska 		} else {
978f6e633a9SMartin Matuska 			/* exit on other errors */
979b38ff370SJamie Gritton 			goto done_free;
980b38ff370SJamie Gritton 		}
981f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
982f6e633a9SMartin Matuska 			error = ENOTDIR;
983f6e633a9SMartin Matuska 			vput(root);
984f6e633a9SMartin Matuska 			goto done_free;
985f6e633a9SMartin Matuska 		}
986b249ce48SMateusz Guzik 		VOP_UNLOCK(root);
987b38ff370SJamie Gritton 	}
988b38ff370SJamie Gritton 
989b38ff370SJamie Gritton 	/*
990b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
991b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
992b38ff370SJamie Gritton 	 */
993b38ff370SJamie Gritton 	pr = NULL;
9947de883c8SJamie Gritton 	inspr = NULL;
995babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
996babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
997babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
998babbbb9cSJamie Gritton 		if (*p != '\0')
999babbbb9cSJamie Gritton 			jid = 0;
1000babbbb9cSJamie Gritton 	}
1001b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
1002108a9384SJamie Gritton 	drflags = PD_LIST_XLOCKED;
10030a2a96f3SJamie Gritton 	ppr = mypr;
10040a2a96f3SJamie Gritton 	if (!prison_isalive(ppr)) {
10050a2a96f3SJamie Gritton 		/* This jail is dying.  This process will surely follow. */
10060a2a96f3SJamie Gritton 		error = EAGAIN;
10070a2a96f3SJamie Gritton 		goto done_deref;
10080a2a96f3SJamie Gritton 	}
1009b38ff370SJamie Gritton 	if (jid != 0) {
1010b38ff370SJamie Gritton 		if (jid < 0) {
1011b38ff370SJamie Gritton 			error = EINVAL;
1012b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
10132a4b2251SJamie Gritton 			goto done_deref;
1014b38ff370SJamie Gritton 		}
10157de883c8SJamie Gritton 		/*
10167de883c8SJamie Gritton 		 * See if a requested jid already exists.  Keep track of
10177de883c8SJamie Gritton 		 * where it can be inserted later.
10187de883c8SJamie Gritton 		 */
10197de883c8SJamie Gritton 		TAILQ_FOREACH(inspr, &allprison, pr_list) {
1020f7496dcaSJamie Gritton 			if (inspr->pr_id < jid)
1021f7496dcaSJamie Gritton 				continue;
1022f7496dcaSJamie Gritton 			if (inspr->pr_id > jid)
1023f7496dcaSJamie Gritton 				break;
10247de883c8SJamie Gritton 			pr = inspr;
1025f7496dcaSJamie Gritton 			mtx_lock(&pr->pr_mtx);
10262a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
10277de883c8SJamie Gritton 			inspr = NULL;
10287de883c8SJamie Gritton 			break;
10297de883c8SJamie Gritton 		}
1030b38ff370SJamie Gritton 		if (pr != NULL) {
1031b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1032b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
10337de883c8SJamie Gritton 				/*
10347de883c8SJamie Gritton 				 * Even creators that cannot see the jail will
10357de883c8SJamie Gritton 				 * get EEXIST.
10367de883c8SJamie Gritton 				 */
1037b38ff370SJamie Gritton 				error = EEXIST;
1038b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1039b38ff370SJamie Gritton 				    jid);
10402a4b2251SJamie Gritton 				goto done_deref;
1041b38ff370SJamie Gritton 			}
10420304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
10437de883c8SJamie Gritton 				/*
10447de883c8SJamie Gritton 				 * Updaters get ENOENT if they cannot see the
10457de883c8SJamie Gritton 				 * jail.  This is true even for CREATE | UPDATE,
10467de883c8SJamie Gritton 				 * which normally cannot give this error.
10477de883c8SJamie Gritton 				 */
10482a4b2251SJamie Gritton 				error = ENOENT;
10492a4b2251SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
10502a4b2251SJamie Gritton 				goto done_deref;
1051f7496dcaSJamie Gritton 			}
10520a2a96f3SJamie Gritton 			ppr = pr->pr_parent;
10530a2a96f3SJamie Gritton 			if (!prison_isalive(ppr)) {
10540a2a96f3SJamie Gritton 				error = ENOENT;
10550a2a96f3SJamie Gritton 				vfs_opterror(opts, "jail %d is dying",
10560a2a96f3SJamie Gritton 				    ppr->pr_id);
10570a2a96f3SJamie Gritton 				goto done_deref;
10580a2a96f3SJamie Gritton 			}
1059f7496dcaSJamie Gritton 			if (!prison_isalive(pr)) {
1060b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1061b38ff370SJamie Gritton 					error = ENOENT;
1062b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1063b38ff370SJamie Gritton 					    jid);
10642a4b2251SJamie Gritton 					goto done_deref;
1065f7496dcaSJamie Gritton 				}
1066f7496dcaSJamie Gritton 				if ((flags & JAIL_ATTACH) ||
1067b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1068b38ff370SJamie Gritton 					/*
1069b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1070b38ff370SJamie Gritton 					 * (via attach or persist), but first
1071b38ff370SJamie Gritton 					 * it must determine if another jail
1072b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1073b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1074b38ff370SJamie Gritton 					 * name.
1075b38ff370SJamie Gritton 					 */
1076b38ff370SJamie Gritton 					if (name == NULL)
10770304c731SJamie Gritton 						name = prison_name(mypr, pr);
1078b38ff370SJamie Gritton 				}
1079b38ff370SJamie Gritton 			}
10802a4b2251SJamie Gritton 		} else {
1081b38ff370SJamie Gritton 			/* Update: jid must exist. */
1082b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1083b38ff370SJamie Gritton 				error = ENOENT;
1084b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
10852a4b2251SJamie Gritton 				goto done_deref;
1086b38ff370SJamie Gritton 			}
1087b38ff370SJamie Gritton 		}
1088b38ff370SJamie Gritton 	}
1089b38ff370SJamie Gritton 	/*
1090b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1091b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1092b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1093b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1094b38ff370SJamie Gritton 	 * because that is the jail being updated).
1095b38ff370SJamie Gritton 	 */
1096b6f47c23SJamie Gritton 	namelc = NULL;
1097b38ff370SJamie Gritton 	if (name != NULL) {
1098babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1099babbbb9cSJamie Gritton 		if (namelc == NULL)
1100babbbb9cSJamie Gritton 			namelc = name;
1101babbbb9cSJamie Gritton 		else {
11020304c731SJamie Gritton 			/*
11030304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
11040304c731SJamie Gritton 			 * parent and child names, and make sure the parent
11050304c731SJamie Gritton 			 * exists or matches an already found jail.
11060304c731SJamie Gritton 			 */
11070304c731SJamie Gritton 			if (pr != NULL) {
1108babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1109babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
11100304c731SJamie Gritton 					error = EINVAL;
11110304c731SJamie Gritton 					vfs_opterror(opts,
11120304c731SJamie Gritton 					    "cannot change jail's parent");
11132a4b2251SJamie Gritton 					goto done_deref;
11140304c731SJamie Gritton 				}
11150304c731SJamie Gritton 			} else {
1116b6f47c23SJamie Gritton 				*namelc = '\0';
11170304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
11180304c731SJamie Gritton 				if (ppr == NULL) {
11190304c731SJamie Gritton 					error = ENOENT;
11200304c731SJamie Gritton 					vfs_opterror(opts,
11210304c731SJamie Gritton 					    "jail \"%s\" not found", name);
11222a4b2251SJamie Gritton 					goto done_deref;
11230304c731SJamie Gritton 				}
1124c050ea80SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
11250a2a96f3SJamie Gritton 				if (!prison_isalive(ppr)) {
1126c050ea80SJamie Gritton 					error = ENOENT;
1127c050ea80SJamie Gritton 					vfs_opterror(opts,
1128c050ea80SJamie Gritton 					    "jail \"%s\" is dying", name);
1129c050ea80SJamie Gritton 					goto done_deref;
1130c050ea80SJamie Gritton 				}
1131b6f47c23SJamie Gritton 				*namelc = '.';
11320304c731SJamie Gritton 			}
1133b6f47c23SJamie Gritton 			namelc++;
11340304c731SJamie Gritton 		}
1135b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1136b6f47c23SJamie Gritton 			pnamelen =
11370304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
11380304c731SJamie Gritton 			deadpr = NULL;
11390304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
11406754ae25SJamie Gritton 				if (tpr != pr &&
1141b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
114276ad42abSJamie Gritton 					if (prison_isalive(tpr)) {
1143b38ff370SJamie Gritton 						if (pr == NULL &&
1144b38ff370SJamie Gritton 						    cuflags != JAIL_CREATE) {
1145b38ff370SJamie Gritton 							/*
1146b38ff370SJamie Gritton 							 * Use this jail
1147b38ff370SJamie Gritton 							 * for updates.
1148b38ff370SJamie Gritton 							 */
1149b38ff370SJamie Gritton 							pr = tpr;
1150f7496dcaSJamie Gritton 							mtx_lock(&pr->pr_mtx);
115183bc72a0SJamie Gritton 							drflags |= PD_LOCKED;
1152b38ff370SJamie Gritton 							break;
1153b38ff370SJamie Gritton 						}
1154b38ff370SJamie Gritton 						/*
1155b38ff370SJamie Gritton 						 * Create, or update(jid):
1156b38ff370SJamie Gritton 						 * name must not exist in an
11570304c731SJamie Gritton 						 * active sibling jail.
1158b38ff370SJamie Gritton 						 */
1159b38ff370SJamie Gritton 						error = EEXIST;
1160b38ff370SJamie Gritton 						vfs_opterror(opts,
1161b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1162b38ff370SJamie Gritton 						   name);
11632a4b2251SJamie Gritton 						goto done_deref;
1164b38ff370SJamie Gritton 					}
116576ad42abSJamie Gritton 					if (pr == NULL &&
1166f7496dcaSJamie Gritton 					    cuflags != JAIL_CREATE) {
116776ad42abSJamie Gritton 						deadpr = tpr;
1168f7496dcaSJamie Gritton 					}
1169b38ff370SJamie Gritton 				}
1170b38ff370SJamie Gritton 			}
1171b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1172b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1173b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1174b38ff370SJamie Gritton 					pr = deadpr;
1175f7496dcaSJamie Gritton 					mtx_lock(&pr->pr_mtx);
11762a4b2251SJamie Gritton 					drflags |= PD_LOCKED;
1177b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1178b38ff370SJamie Gritton 					error = ENOENT;
1179b38ff370SJamie Gritton 					vfs_opterror(opts,
1180b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
11812a4b2251SJamie Gritton 					goto done_deref;
1182b38ff370SJamie Gritton 				}
1183b38ff370SJamie Gritton 			}
1184b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1185b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1186b38ff370SJamie Gritton 				error = ENOENT;
1187b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1188b38ff370SJamie Gritton 				    name);
11892a4b2251SJamie Gritton 				goto done_deref;
1190b38ff370SJamie Gritton 			}
1191b38ff370SJamie Gritton 		}
1192b38ff370SJamie Gritton 	}
1193b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1194b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1195b38ff370SJamie Gritton 		error = ENOENT;
1196b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
11972a4b2251SJamie Gritton 		goto done_deref;
1198b38ff370SJamie Gritton 	}
1199b38ff370SJamie Gritton 
1200b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
12012a4b2251SJamie Gritton 	created = pr == NULL;
12022a4b2251SJamie Gritton 	if (created) {
1203b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1204b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1205b97457e2SJamie Gritton 				error = EPERM;
1206b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
12072a4b2251SJamie Gritton 				goto done_deref;
1208b97457e2SJamie Gritton 			}
12097de883c8SJamie Gritton 		if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1210b38ff370SJamie Gritton 			error = EAGAIN;
12117de883c8SJamie Gritton 			vfs_opterror(opts, "no available jail IDs");
12122a4b2251SJamie Gritton 			goto done_deref;
1213b38ff370SJamie Gritton 		}
12142a4b2251SJamie Gritton 
12152a4b2251SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
12161158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_INVALID;
12171158508aSJamie Gritton 		refcount_init(&pr->pr_ref, 1);
1218f7496dcaSJamie Gritton 		refcount_init(&pr->pr_uref, 0);
12191158508aSJamie Gritton 		drflags |= PD_DEREF;
12202a4b2251SJamie Gritton 		LIST_INIT(&pr->pr_children);
12212a4b2251SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
12222a4b2251SJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
12232a4b2251SJamie Gritton 
12247de883c8SJamie Gritton 		pr->pr_id = jid;
12257de883c8SJamie Gritton 		if (inspr != NULL)
12267de883c8SJamie Gritton 			TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
12277de883c8SJamie Gritton 		else
1228b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
12297de883c8SJamie Gritton 
12307de883c8SJamie Gritton 		pr->pr_parent = ppr;
12310a2a96f3SJamie Gritton 		prison_hold(ppr);
12320a2a96f3SJamie Gritton 		prison_proc_hold(ppr);
12330304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
12340304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1235b97457e2SJamie Gritton 			tpr->pr_childcount++;
1236b38ff370SJamie Gritton 
12370304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1238b6f47c23SJamie Gritton 		if (namelc == NULL)
1239b6f47c23SJamie Gritton 			namelc = "";
1240b38ff370SJamie Gritton 		if (path == NULL) {
1241b38ff370SJamie Gritton 			path = "/";
12420304c731SJamie Gritton 			root = mypr->pr_root;
1243b38ff370SJamie Gritton 			vref(root);
1244b38ff370SJamie Gritton 		}
12458986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
12468986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1247bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1248bdfc8cc4SJamie Gritton #ifdef VIMAGE
1249bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1250bdfc8cc4SJamie Gritton #endif
1251bdfc8cc4SJamie Gritton 		{
12520304c731SJamie Gritton #ifdef INET
12532b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12546a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
12552b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12562b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12572b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12582b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12592b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12602b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12612b0d6f81SJamie Gritton 					    M_WAITOK);
12622b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12632b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12642b0d6f81SJamie Gritton 				}
12652b0d6f81SJamie Gritton 			}
12660304c731SJamie Gritton #endif
12670304c731SJamie Gritton #ifdef INET6
12682b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
12696a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
12702b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
12712b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
12722b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
12732b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
12742b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
12752b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
12762b0d6f81SJamie Gritton 					    M_WAITOK);
12772b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
12782b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
12792b0d6f81SJamie Gritton 				}
12802b0d6f81SJamie Gritton 			}
12810304c731SJamie Gritton #endif
1282bdfc8cc4SJamie Gritton 		}
1283bdfc8cc4SJamie Gritton #endif
1284592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1285592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1286592bcae8SBjoern A. Zeeb 
12870304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
12880304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1289af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1290bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1291b38ff370SJamie Gritton 
1292b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1293b96bd95bSIan Lepore 		if (osrelstr == NULL)
12941b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
12951b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1296b96bd95bSIan Lepore 		else
12971b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, osrelstr,
12981b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1299b96bd95bSIan Lepore 
1300679e1390SJamie Gritton #ifdef VIMAGE
1301679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1302679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1303679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1304679e1390SJamie Gritton #endif
1305b38ff370SJamie Gritton 		/*
1306b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1307*8771ff75SGordon Bergling 		 * Unlike other initial settings, this may return an error.
1308b38ff370SJamie Gritton 		 */
13090304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
13102a4b2251SJamie Gritton 		if (error)
13112a4b2251SJamie Gritton 			goto done_deref;
1312b38ff370SJamie Gritton 
1313b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
13142a4b2251SJamie Gritton 		drflags |= PD_LOCKED;
1315b38ff370SJamie Gritton 	} else {
13162b0d6f81SJamie Gritton 		/*
13172b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
13182b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
13192b0d6f81SJamie Gritton 		 */
13206754ae25SJamie Gritton 		prison_hold(pr);
13212a4b2251SJamie Gritton 		drflags |= PD_DEREF;
1322bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1323bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1324bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1325bdfc8cc4SJamie Gritton 			error = EINVAL;
1326bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1327bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
13282a4b2251SJamie Gritton 			goto done_deref;
1329bdfc8cc4SJamie Gritton 		}
1330bdfc8cc4SJamie Gritton #endif
13312b0d6f81SJamie Gritton #ifdef INET
13322b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13332b0d6f81SJamie Gritton 			error = EINVAL;
13342b0d6f81SJamie Gritton 			vfs_opterror(opts,
13352b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
13362a4b2251SJamie Gritton 			goto done_deref;
13372b0d6f81SJamie Gritton 		}
13382b0d6f81SJamie Gritton #endif
13392b0d6f81SJamie Gritton #ifdef INET6
13402b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13412b0d6f81SJamie Gritton 			error = EINVAL;
13422b0d6f81SJamie Gritton 			vfs_opterror(opts,
13432b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
13442a4b2251SJamie Gritton 			goto done_deref;
13452b0d6f81SJamie Gritton 		}
13462b0d6f81SJamie Gritton #endif
1347b38ff370SJamie Gritton 	}
1348b38ff370SJamie Gritton 
1349b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13500304c731SJamie Gritton 	if (gotslevel) {
13510304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13520304c731SJamie Gritton 			error = EPERM;
13532a4b2251SJamie Gritton 			goto done_deref;
13540304c731SJamie Gritton 		}
13550304c731SJamie Gritton 	}
1356b97457e2SJamie Gritton 	if (gotchildmax) {
1357b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1358b97457e2SJamie Gritton 			error = EPERM;
13592a4b2251SJamie Gritton 			goto done_deref;
1360b97457e2SJamie Gritton 		}
1361b97457e2SJamie Gritton 	}
13620304c731SJamie Gritton 	if (gotenforce) {
13630304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
13640304c731SJamie Gritton 			error = EPERM;
13652a4b2251SJamie Gritton 			goto done_deref;
13660304c731SJamie Gritton 		}
13670304c731SJamie Gritton 	}
13680cc207a6SMartin Matuska 	if (gotrsnum) {
13690cc207a6SMartin Matuska 		/*
13700cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
13710cc207a6SMartin Matuska 		 */
1372bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
13730cc207a6SMartin Matuska 			error = EINVAL;
13742a4b2251SJamie Gritton 			goto done_deref;
13750cc207a6SMartin Matuska 		}
13760cc207a6SMartin Matuska 		/*
1377bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
13780cc207a6SMartin Matuska 		 */
13790cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
13800cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
13810cc207a6SMartin Matuska 				error = EPERM;
13822a4b2251SJamie Gritton 				goto done_deref;
1383bf3db8aaSMartin Matuska 			} else
13840cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
13850cc207a6SMartin Matuska 		}
13860cc207a6SMartin Matuska 	}
1387b38ff370SJamie Gritton #ifdef INET
13882b0d6f81SJamie Gritton 	if (ip4s > 0) {
13890304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
13900304c731SJamie Gritton 			/*
13910304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
13920304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
13930304c731SJamie Gritton 			 * about the parent being unlocked, as any
13940304c731SJamie Gritton 			 * setting is done with allprison_lock held.
13950304c731SJamie Gritton 			 */
13960304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
13972b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
13980304c731SJamie Gritton 					break;
13990304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
14000304c731SJamie Gritton 				error = EPERM;
14012a4b2251SJamie Gritton 				goto done_deref;
14020304c731SJamie Gritton 			}
14030304c731SJamie Gritton 			if (ip4s > 1) {
14040304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
14050304c731SJamie Gritton 					if (ip4[ii].s_addr ==
14060304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1407b38ff370SJamie Gritton 						continue;
14080304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
14090304c731SJamie Gritton 						if (ip4[ii].s_addr ==
14100304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
14110304c731SJamie Gritton 							break;
14120304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
14130304c731SJamie Gritton 						break;
14140304c731SJamie Gritton 				}
14150304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
14160304c731SJamie Gritton 					error = EPERM;
14172a4b2251SJamie Gritton 					goto done_deref;
14180304c731SJamie Gritton 				}
14190304c731SJamie Gritton 			}
14200304c731SJamie Gritton 		}
14210304c731SJamie Gritton 		/*
14220304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
14230304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
14240304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
14250304c731SJamie Gritton 		 * IP stop checking and return error.
14260304c731SJamie Gritton 		 */
1427bdfc8cc4SJamie Gritton #ifdef VIMAGE
142808b43333SJamie Gritton 		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1429bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1430bdfc8cc4SJamie Gritton 				break;
143108b43333SJamie Gritton #else
143208b43333SJamie Gritton 		tppr = &prison0;
1433bdfc8cc4SJamie Gritton #endif
1434bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1435bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1436bdfc8cc4SJamie Gritton #ifdef VIMAGE
14372b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1438bdfc8cc4SJamie Gritton #endif
1439f7496dcaSJamie Gritton 			    !prison_isalive(tpr)) {
14400304c731SJamie Gritton 				descend = 0;
14410304c731SJamie Gritton 				continue;
14420304c731SJamie Gritton 			}
14430304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
14440304c731SJamie Gritton 				continue;
14450304c731SJamie Gritton 			descend = 0;
14460304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
14470304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
14480304c731SJamie Gritton 				continue;
14490304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
14500ce1624dSStephen J. Kiernan 				if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
14510ce1624dSStephen J. Kiernan 				    0) {
14520304c731SJamie Gritton 					error = EADDRINUSE;
1453b38ff370SJamie Gritton 					vfs_opterror(opts,
1454b38ff370SJamie Gritton 					    "IPv4 addresses clash");
14552a4b2251SJamie Gritton 					goto done_deref;
1456b38ff370SJamie Gritton 				}
14570304c731SJamie Gritton 			}
14580304c731SJamie Gritton 		}
14590304c731SJamie Gritton 	}
1460b38ff370SJamie Gritton #endif
1461b38ff370SJamie Gritton #ifdef INET6
14622b0d6f81SJamie Gritton 	if (ip6s > 0) {
14630304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
14640304c731SJamie Gritton 			/*
14650304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14660304c731SJamie Gritton 			 * subset of the parent's list.
14670304c731SJamie Gritton 			 */
14680304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
14690304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14700304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
14710304c731SJamie Gritton 					break;
14720304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
14730304c731SJamie Gritton 				error = EPERM;
14742a4b2251SJamie Gritton 				goto done_deref;
14750304c731SJamie Gritton 			}
14760304c731SJamie Gritton 			if (ip6s > 1) {
14770304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
14780304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14790304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
14800304c731SJamie Gritton 						continue;
14810304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
14820304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
14832b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
14840304c731SJamie Gritton 							break;
14850304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
14860304c731SJamie Gritton 						break;
14870304c731SJamie Gritton 				}
14880304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14890304c731SJamie Gritton 					error = EPERM;
14902a4b2251SJamie Gritton 					goto done_deref;
14910304c731SJamie Gritton 				}
14920304c731SJamie Gritton 			}
14930304c731SJamie Gritton 		}
14940304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1495bdfc8cc4SJamie Gritton #ifdef VIMAGE
149608b43333SJamie Gritton 		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1497bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1498bdfc8cc4SJamie Gritton 				break;
149908b43333SJamie Gritton #else
150008b43333SJamie Gritton 		tppr = &prison0;
1501bdfc8cc4SJamie Gritton #endif
1502bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1503bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1504bdfc8cc4SJamie Gritton #ifdef VIMAGE
15052b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1506bdfc8cc4SJamie Gritton #endif
1507f7496dcaSJamie Gritton 			    !prison_isalive(tpr)) {
15080304c731SJamie Gritton 				descend = 0;
15090304c731SJamie Gritton 				continue;
15100304c731SJamie Gritton 			}
15110304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
15120304c731SJamie Gritton 				continue;
15130304c731SJamie Gritton 			descend = 0;
15140304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
15150304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
15160304c731SJamie Gritton 				continue;
15170304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
15180ce1624dSStephen J. Kiernan 				if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
15190ce1624dSStephen J. Kiernan 				    0) {
15200304c731SJamie Gritton 					error = EADDRINUSE;
1521b38ff370SJamie Gritton 					vfs_opterror(opts,
1522b38ff370SJamie Gritton 					    "IPv6 addresses clash");
15232a4b2251SJamie Gritton 					goto done_deref;
1524b38ff370SJamie Gritton 				}
15250304c731SJamie Gritton 			}
15260304c731SJamie Gritton 		}
15270304c731SJamie Gritton 	}
1528b38ff370SJamie Gritton #endif
15290304c731SJamie Gritton 	onamelen = namelen = 0;
1530b6f47c23SJamie Gritton 	if (namelc != NULL) {
15316ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
15326ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
15336ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
15346ab6058eSJamie Gritton 		 */
1535b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1536b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
15376ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
15386ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1539b38ff370SJamie Gritton 			error = EINVAL;
1540babbbb9cSJamie Gritton 			vfs_opterror(opts,
1541babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
15422a4b2251SJamie Gritton 			goto done_deref;
1543b38ff370SJamie Gritton 		}
1544b38ff370SJamie Gritton 		/*
15450304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
15460304c731SJamie Gritton 		 * children.
1547b38ff370SJamie Gritton 		 */
1548b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1549b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1550b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1551b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
15520304c731SJamie Gritton 			error = ENAMETOOLONG;
15532a4b2251SJamie Gritton 			goto done_deref;
15540304c731SJamie Gritton 		}
15550304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
15560304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
15570304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
15580304c731SJamie Gritton 				error = ENAMETOOLONG;
15592a4b2251SJamie Gritton 				goto done_deref;
15600304c731SJamie Gritton 			}
15610304c731SJamie Gritton 		}
15620304c731SJamie Gritton 	}
1563b3079544SJamie Gritton 	pr_allow_diff = pr_allow & ~ppr->pr_allow;
1564b3079544SJamie Gritton 	if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
15650304c731SJamie Gritton 		error = EPERM;
15662a4b2251SJamie Gritton 		goto done_deref;
1567b38ff370SJamie Gritton 	}
1568b38ff370SJamie Gritton 
1569b6f47c23SJamie Gritton 	/*
1570b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1571b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1572b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1573b6f47c23SJamie Gritton 	 */
1574b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
15752a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1576b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
15772a4b2251SJamie Gritton 	if (error != 0)
15782a4b2251SJamie Gritton 		goto done_deref;
1579b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
15802a4b2251SJamie Gritton 	drflags |= PD_LOCKED;
1581b6f47c23SJamie Gritton 
1582b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1583b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1584b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1585b6f47c23SJamie Gritton 			error = EINVAL;
1586b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
15872a4b2251SJamie Gritton 			goto done_deref;
1588b6f47c23SJamie Gritton 		}
1589b6f47c23SJamie Gritton 	}
1590b6f47c23SJamie Gritton 
1591b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1592b38ff370SJamie Gritton #ifdef INET
15930304c731SJamie Gritton 	redo_ip4 = 0;
15940304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
15950304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1596b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
15970304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1598b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1599b38ff370SJamie Gritton 		ip4 = NULL;
16000304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1601bdfc8cc4SJamie Gritton #ifdef VIMAGE
1602bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1603bdfc8cc4SJamie Gritton 				descend = 0;
1604bdfc8cc4SJamie Gritton 				continue;
1605bdfc8cc4SJamie Gritton 			}
1606bdfc8cc4SJamie Gritton #endif
16070304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
16080304c731SJamie Gritton 				redo_ip4 = 1;
16090304c731SJamie Gritton 				descend = 0;
16100304c731SJamie Gritton 			}
16110304c731SJamie Gritton 		}
16120304c731SJamie Gritton 	}
1613b38ff370SJamie Gritton #endif
1614b38ff370SJamie Gritton #ifdef INET6
16150304c731SJamie Gritton 	redo_ip6 = 0;
16160304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
16170304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1618b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
16190304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1620b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1621b38ff370SJamie Gritton 		ip6 = NULL;
16220304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1623bdfc8cc4SJamie Gritton #ifdef VIMAGE
1624bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1625bdfc8cc4SJamie Gritton 				descend = 0;
1626bdfc8cc4SJamie Gritton 				continue;
1627bdfc8cc4SJamie Gritton 			}
1628bdfc8cc4SJamie Gritton #endif
16290304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
16300304c731SJamie Gritton 				redo_ip6 = 1;
16310304c731SJamie Gritton 				descend = 0;
16320304c731SJamie Gritton 			}
16330304c731SJamie Gritton 		}
16340304c731SJamie Gritton 	}
1635b38ff370SJamie Gritton #endif
16360304c731SJamie Gritton 	if (gotslevel) {
1637b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
16380304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
16390304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16400304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
16410304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
16420304c731SJamie Gritton 	}
1643b97457e2SJamie Gritton 	if (gotchildmax) {
1644b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1645b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1646b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1647b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1648b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1649b97457e2SJamie Gritton 				    ? childmax - level : 0;
1650b97457e2SJamie Gritton 	}
16510304c731SJamie Gritton 	if (gotenforce) {
16520304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
16530304c731SJamie Gritton 		/* Pass this restriction on to the children. */
16540304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16550304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
16560304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
16570304c731SJamie Gritton 	}
16580cc207a6SMartin Matuska 	if (gotrsnum) {
16590cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
16600cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
16610cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16620cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
16630cc207a6SMartin Matuska 	}
1664b6f47c23SJamie Gritton 	if (namelc != NULL) {
16650304c731SJamie Gritton 		if (ppr == &prison0)
1666b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
16670304c731SJamie Gritton 		else
16680304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1669b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
16700304c731SJamie Gritton 		/* Change this component of child names. */
16710304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16720304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
16730304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
16740304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
16750304c731SJamie Gritton 		}
16760304c731SJamie Gritton 	}
1677b38ff370SJamie Gritton 	if (path != NULL) {
16780304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1679b38ff370SJamie Gritton 		strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1680b38ff370SJamie Gritton 		pr->pr_root = root;
16812a4b2251SJamie Gritton 		root = NULL;
1682b38ff370SJamie Gritton 	}
168376ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
168476ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
168576ca6f88SJamie Gritton 			/*
168676ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
168776ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
168876ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
168976ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
169076ca6f88SJamie Gritton 			 */
1691c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1692c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1693c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1694c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1695c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1696c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
169776ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
169876ca6f88SJamie Gritton 		}
169976ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
170076ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1701b38ff370SJamie Gritton 		if (host != NULL)
1702c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
170376ca6f88SJamie Gritton 		if (domain != NULL)
1704c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1705c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
170676ca6f88SJamie Gritton 		if (uuid != NULL)
1707c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
170876ca6f88SJamie Gritton 		if (gothid)
170976ca6f88SJamie Gritton 			pr->pr_hostid = hid;
171076ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
171176ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
171276ca6f88SJamie Gritton 				descend = 0;
171376ca6f88SJamie Gritton 			else {
171476ca6f88SJamie Gritton 				if (host != NULL)
1715c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1716c1f19219SJamie Gritton 					    pr->pr_hostname,
1717c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
171876ca6f88SJamie Gritton 				if (domain != NULL)
1719c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1720c1f19219SJamie Gritton 					    pr->pr_domainname,
1721c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
172276ca6f88SJamie Gritton 				if (uuid != NULL)
1723c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1724c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1725c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
172676ca6f88SJamie Gritton 				if (gothid)
172776ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
172876ca6f88SJamie Gritton 			}
172976ca6f88SJamie Gritton 		}
173076ca6f88SJamie Gritton 	}
17310304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
17320fe74ae6SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow))
17330fe74ae6SJamie Gritton 		prison_set_allow_locked(pr, tallow, 0);
1734b38ff370SJamie Gritton 	/*
1735b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
17361158508aSJamie Gritton 	 * persist flag lose that reference.
1737b38ff370SJamie Gritton 	 */
173876ad42abSJamie Gritton 	born = !prison_isalive(pr);
17391158508aSJamie Gritton 	if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1740b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
17416754ae25SJamie Gritton 			prison_hold(pr);
17421158508aSJamie Gritton 			/*
17431158508aSJamie Gritton 			 * This may make a dead prison alive again, but wait
17441158508aSJamie Gritton 			 * to label it as such until after OSD calls have had
17451158508aSJamie Gritton 			 * a chance to run (and perhaps to fail).
17461158508aSJamie Gritton 			 */
17476754ae25SJamie Gritton 			refcount_acquire(&pr->pr_uref);
1748b38ff370SJamie Gritton 		} else {
174939c8ef90SJamie Gritton 			drflags |= PD_DEUREF;
1750f7496dcaSJamie Gritton 			prison_free_not_last(pr);
1751b38ff370SJamie Gritton 		}
1752b38ff370SJamie Gritton 	}
1753b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1754b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
17552a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1756c861373bSJamie Gritton 	/*
1757c861373bSJamie Gritton 	 * Any errors past this point will need to de-persist newly created
1758c861373bSJamie Gritton 	 * prisons, as well as call remove methods.
1759c861373bSJamie Gritton 	 */
1760c861373bSJamie Gritton 	if (born)
1761c861373bSJamie Gritton 		drflags |= PD_KILL;
1762b38ff370SJamie Gritton 
1763a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
17644b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
1765a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
1766a7ad07bfSEdward Tomasz Napierala #endif
1767a7ad07bfSEdward Tomasz Napierala 
17680304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
17690304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
17700304c731SJamie Gritton 	 */
17710304c731SJamie Gritton #ifdef INET
17720304c731SJamie Gritton 	while (redo_ip4) {
17730304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
17740304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
17750304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17760304c731SJamie Gritton 		redo_ip4 = 0;
17770304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1778bdfc8cc4SJamie Gritton #ifdef VIMAGE
1779bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1780bdfc8cc4SJamie Gritton 				descend = 0;
1781bdfc8cc4SJamie Gritton 				continue;
1782bdfc8cc4SJamie Gritton 			}
1783bdfc8cc4SJamie Gritton #endif
17840304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
17850304c731SJamie Gritton 				if (ip4 != NULL)
17860304c731SJamie Gritton 					ip4 = NULL;
17870304c731SJamie Gritton 				else
17880304c731SJamie Gritton 					redo_ip4 = 1;
17890304c731SJamie Gritton 			}
17900304c731SJamie Gritton 		}
17910304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17920304c731SJamie Gritton 	}
17930304c731SJamie Gritton #endif
17940304c731SJamie Gritton #ifdef INET6
17950304c731SJamie Gritton 	while (redo_ip6) {
17960304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
17970304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
17980304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17990304c731SJamie Gritton 		redo_ip6 = 0;
18000304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1801bdfc8cc4SJamie Gritton #ifdef VIMAGE
1802bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1803bdfc8cc4SJamie Gritton 				descend = 0;
1804bdfc8cc4SJamie Gritton 				continue;
1805bdfc8cc4SJamie Gritton 			}
1806bdfc8cc4SJamie Gritton #endif
18070304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
18080304c731SJamie Gritton 				if (ip6 != NULL)
18090304c731SJamie Gritton 					ip6 = NULL;
18100304c731SJamie Gritton 				else
18110304c731SJamie Gritton 					redo_ip6 = 1;
18120304c731SJamie Gritton 			}
18130304c731SJamie Gritton 		}
18140304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
18150304c731SJamie Gritton 	}
18160304c731SJamie Gritton #endif
18170304c731SJamie Gritton 
1818b38ff370SJamie Gritton 	/* Let the modules do their work. */
1819cc5fd8c7SJamie Gritton 	if (born) {
1820b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1821c861373bSJamie Gritton 		if (error)
18222a4b2251SJamie Gritton 			goto done_deref;
1823b38ff370SJamie Gritton 	}
1824b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1825c861373bSJamie Gritton 	if (error)
18262a4b2251SJamie Gritton 		goto done_deref;
1827b38ff370SJamie Gritton 
18281158508aSJamie Gritton 	/*
18291158508aSJamie Gritton 	 * A new prison is now ready to be seen; either it has gained a user
18301158508aSJamie Gritton 	 * reference via persistence, or is about to gain one via attachment.
18311158508aSJamie Gritton 	 */
18321158508aSJamie Gritton 	if (born) {
18331158508aSJamie Gritton 		drflags = prison_lock_xlock(pr, drflags);
18341158508aSJamie Gritton 		pr->pr_state = PRISON_STATE_ALIVE;
18351158508aSJamie Gritton 	}
18361158508aSJamie Gritton 
1837b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1838b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1839c861373bSJamie Gritton 		error = do_jail_attach(td, pr,
1840589e4c1dSJamie Gritton 		    prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
1841f7496dcaSJamie Gritton 		drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1842b38ff370SJamie Gritton 		if (error) {
1843b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
18442a4b2251SJamie Gritton 			goto done_deref;
1845b38ff370SJamie Gritton 		}
1846b38ff370SJamie Gritton 	}
1847b38ff370SJamie Gritton 
18481fb24974SEdward Tomasz Napierala #ifdef RACCT
18494b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
1850701d6b50SJamie Gritton 		if (drflags & PD_LOCKED) {
1851701d6b50SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1852701d6b50SJamie Gritton 			drflags &= ~PD_LOCKED;
1853701d6b50SJamie Gritton 		}
1854f7496dcaSJamie Gritton 		if (drflags & PD_LIST_XLOCKED) {
1855f7496dcaSJamie Gritton 			sx_xunlock(&allprison_lock);
1856f7496dcaSJamie Gritton 			drflags &= ~PD_LIST_XLOCKED;
1857b4e87a63SJamie Gritton 		}
18581fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
18591fb24974SEdward Tomasz Napierala 	}
18601fb24974SEdward Tomasz Napierala #endif
18611fb24974SEdward Tomasz Napierala 
1862c861373bSJamie Gritton 	drflags &= ~PD_KILL;
18631fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
18641fb24974SEdward Tomasz Napierala 
18652a4b2251SJamie Gritton  done_deref:
18662a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
18672a4b2251SJamie Gritton 	if (pr != NULL)
18682a4b2251SJamie Gritton 		prison_deref(pr, drflags);
18692a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
1870b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
18712a4b2251SJamie Gritton 	else if (drflags & PD_LIST_XLOCKED)
1872b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
18735050aa86SKonstantin Belousov 	if (root != NULL)
1874b38ff370SJamie Gritton 		vrele(root);
1875b38ff370SJamie Gritton  done_errmsg:
1876b38ff370SJamie Gritton 	if (error) {
18772a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
1878176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1879176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
1880b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1881b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
1882b38ff370SJamie Gritton 				bcopy(errmsg,
1883b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1884b38ff370SJamie Gritton 				    errmsg_len);
1885b38ff370SJamie Gritton 			else
1886b38ff370SJamie Gritton 				copyout(errmsg,
1887b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1888b38ff370SJamie Gritton 				    errmsg_len);
1889b38ff370SJamie Gritton 		}
1890b38ff370SJamie Gritton 	}
1891b38ff370SJamie Gritton  done_free:
1892b38ff370SJamie Gritton #ifdef INET
1893b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1894b38ff370SJamie Gritton #endif
1895b38ff370SJamie Gritton #ifdef INET6
1896b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1897b38ff370SJamie Gritton #endif
18986dfe0a3dSMartin Matuska 	if (g_path != NULL)
18996dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
1900b38ff370SJamie Gritton 	vfs_freeopts(opts);
1901b38ff370SJamie Gritton 	return (error);
1902b38ff370SJamie Gritton }
1903b38ff370SJamie Gritton 
1904b38ff370SJamie Gritton /*
19057de883c8SJamie Gritton  * Find the next available prison ID.  Return the ID on success, or zero
19067de883c8SJamie Gritton  * on failure.  Also set a pointer to the allprison list entry the prison
19077de883c8SJamie Gritton  * should be inserted before.
19087de883c8SJamie Gritton  */
19097de883c8SJamie Gritton static int
19107de883c8SJamie Gritton get_next_prid(struct prison **insprp)
19117de883c8SJamie Gritton {
19127de883c8SJamie Gritton 	struct prison *inspr;
19137de883c8SJamie Gritton 	int jid, maxid;
19147de883c8SJamie Gritton 
19157de883c8SJamie Gritton 	jid = lastprid % JAIL_MAX + 1;
19167de883c8SJamie Gritton 	if (TAILQ_EMPTY(&allprison) ||
19177de883c8SJamie Gritton 	    TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
19187de883c8SJamie Gritton 		/*
19197de883c8SJamie Gritton 		 * A common case is for all jails to be implicitly numbered,
19207de883c8SJamie Gritton 		 * which means they'll go on the end of the list, at least
19217de883c8SJamie Gritton 		 * for the first JAIL_MAX times.
19227de883c8SJamie Gritton 		 */
19237de883c8SJamie Gritton 		inspr = NULL;
19247de883c8SJamie Gritton 	} else {
19257de883c8SJamie Gritton 		/*
19267de883c8SJamie Gritton 		 * Take two passes through the allprison list: first starting
19277de883c8SJamie Gritton 		 * with the proposed jid, then ending with it.
19287de883c8SJamie Gritton 		 */
19297de883c8SJamie Gritton 		for (maxid = JAIL_MAX; maxid != 0; ) {
19307de883c8SJamie Gritton 			TAILQ_FOREACH(inspr, &allprison, pr_list) {
19317de883c8SJamie Gritton 				if (inspr->pr_id < jid)
19327de883c8SJamie Gritton 					continue;
1933f7496dcaSJamie Gritton 				if (inspr->pr_id > jid) {
1934f7496dcaSJamie Gritton 					/* Found an opening. */
19357de883c8SJamie Gritton 					maxid = 0;
19367de883c8SJamie Gritton 					break;
19377de883c8SJamie Gritton 				}
19387de883c8SJamie Gritton 				if (++jid > maxid) {
19397de883c8SJamie Gritton 					if (lastprid == maxid || lastprid == 0)
19407de883c8SJamie Gritton 					{
19417de883c8SJamie Gritton 						/*
19427de883c8SJamie Gritton 						 * The entire legal range
19437de883c8SJamie Gritton 						 * has been traversed
19447de883c8SJamie Gritton 						 */
19457de883c8SJamie Gritton 						return 0;
19467de883c8SJamie Gritton 					}
19477de883c8SJamie Gritton 					/* Try again from the start. */
19487de883c8SJamie Gritton 					jid = 1;
19497de883c8SJamie Gritton 					maxid = lastprid;
19507de883c8SJamie Gritton 					break;
19517de883c8SJamie Gritton 				}
19527de883c8SJamie Gritton 			}
19537de883c8SJamie Gritton 			if (inspr == NULL) {
19547de883c8SJamie Gritton 				/* Found room at the end of the list. */
19557de883c8SJamie Gritton 				break;
19567de883c8SJamie Gritton 			}
19577de883c8SJamie Gritton 		}
19587de883c8SJamie Gritton 	}
19597de883c8SJamie Gritton 	*insprp = inspr;
19607de883c8SJamie Gritton 	lastprid = jid;
19617de883c8SJamie Gritton 	return (jid);
19627de883c8SJamie Gritton }
19637de883c8SJamie Gritton 
19647de883c8SJamie Gritton /*
1965b38ff370SJamie Gritton  * struct jail_get_args {
1966b38ff370SJamie Gritton  *	struct iovec *iovp;
1967b38ff370SJamie Gritton  *	unsigned int iovcnt;
1968b38ff370SJamie Gritton  *	int flags;
1969b38ff370SJamie Gritton  * };
1970b38ff370SJamie Gritton  */
1971b38ff370SJamie Gritton int
19728451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
1973b38ff370SJamie Gritton {
1974b38ff370SJamie Gritton 	struct uio *auio;
1975b38ff370SJamie Gritton 	int error;
1976b38ff370SJamie Gritton 
1977b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1978b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1979b38ff370SJamie Gritton 		return (EINVAL);
1980b38ff370SJamie Gritton 
1981b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1982b38ff370SJamie Gritton 	if (error)
1983b38ff370SJamie Gritton 		return (error);
1984b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1985b38ff370SJamie Gritton 	if (error == 0)
1986b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1987b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1988b38ff370SJamie Gritton 	free(auio, M_IOV);
1989b38ff370SJamie Gritton 	return (error);
1990b38ff370SJamie Gritton }
1991b38ff370SJamie Gritton 
1992b38ff370SJamie Gritton int
1993b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1994b38ff370SJamie Gritton {
1995672756aaSJamie Gritton 	struct bool_flags *bf;
1996672756aaSJamie Gritton 	struct jailsys_flags *jsf;
19970304c731SJamie Gritton 	struct prison *pr, *mypr;
1998b38ff370SJamie Gritton 	struct vfsopt *opt;
1999b38ff370SJamie Gritton 	struct vfsoptlist *opts;
2000b38ff370SJamie Gritton 	char *errmsg, *name;
20012a4b2251SJamie Gritton 	int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2002672756aaSJamie Gritton 	unsigned f;
2003b38ff370SJamie Gritton 
2004b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
2005b38ff370SJamie Gritton 		return (EINVAL);
2006b38ff370SJamie Gritton 
2007b38ff370SJamie Gritton 	/* Get the parameter list. */
2008b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
2009b38ff370SJamie Gritton 	if (error)
2010b38ff370SJamie Gritton 		return (error);
2011b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
20120304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
20132a4b2251SJamie Gritton 	pr = NULL;
20141e2a13e6SJamie Gritton 
2015b38ff370SJamie Gritton 	/*
2016b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
2017b38ff370SJamie Gritton 	 */
2018b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
20192a4b2251SJamie Gritton 	drflags = PD_LIST_SLOCKED;
2020b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2021b38ff370SJamie Gritton 	if (error == 0) {
2022b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
2023f7496dcaSJamie Gritton 			if (pr->pr_id > jid &&
2024f7496dcaSJamie Gritton 			    ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2025f7496dcaSJamie Gritton 			    prison_ischild(mypr, pr)) {
2026b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
20272a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
2028b38ff370SJamie Gritton 				goto found_prison;
20292a4b2251SJamie Gritton 			}
2030f7496dcaSJamie Gritton 		}
2031b38ff370SJamie Gritton 		error = ENOENT;
2032b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
20332a4b2251SJamie Gritton 		goto done;
2034b38ff370SJamie Gritton 	} else if (error != ENOENT)
20352a4b2251SJamie Gritton 		goto done;
2036b38ff370SJamie Gritton 
2037b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2038b38ff370SJamie Gritton 	if (error == 0) {
2039b38ff370SJamie Gritton 		if (jid != 0) {
20400304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
2041b38ff370SJamie Gritton 			if (pr != NULL) {
20422a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
204376ad42abSJamie Gritton 				if (!(prison_isalive(pr) ||
204476ad42abSJamie Gritton 				    (flags & JAIL_DYING))) {
2045b38ff370SJamie Gritton 					error = ENOENT;
2046b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
2047b38ff370SJamie Gritton 					    jid);
20482a4b2251SJamie Gritton 					goto done;
2049b38ff370SJamie Gritton 				}
2050b38ff370SJamie Gritton 				goto found_prison;
2051b38ff370SJamie Gritton 			}
2052b38ff370SJamie Gritton 			error = ENOENT;
2053b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
20542a4b2251SJamie Gritton 			goto done;
2055b38ff370SJamie Gritton 		}
2056b38ff370SJamie Gritton 	} else if (error != ENOENT)
20572a4b2251SJamie Gritton 		goto done;
2058b38ff370SJamie Gritton 
2059b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2060b38ff370SJamie Gritton 	if (error == 0) {
2061b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2062b38ff370SJamie Gritton 			error = EINVAL;
20632a4b2251SJamie Gritton 			goto done;
2064b38ff370SJamie Gritton 		}
20650304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2066b38ff370SJamie Gritton 		if (pr != NULL) {
20672a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
206876ad42abSJamie Gritton 			if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2069b38ff370SJamie Gritton 				error = ENOENT;
2070b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2071b38ff370SJamie Gritton 				    name);
20722a4b2251SJamie Gritton 				goto done;
2073b38ff370SJamie Gritton 			}
2074b38ff370SJamie Gritton 			goto found_prison;
2075b38ff370SJamie Gritton 		}
2076b38ff370SJamie Gritton 		error = ENOENT;
2077b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
20782a4b2251SJamie Gritton 		goto done;
2079b38ff370SJamie Gritton 	} else if (error != ENOENT)
20802a4b2251SJamie Gritton 		goto done;
2081b38ff370SJamie Gritton 
2082b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2083b38ff370SJamie Gritton 	error = ENOENT;
20842a4b2251SJamie Gritton 	goto done;
2085b38ff370SJamie Gritton 
2086b38ff370SJamie Gritton  found_prison:
2087b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
20886754ae25SJamie Gritton 	prison_hold(pr);
20892a4b2251SJamie Gritton 	drflags |= PD_DEREF;
2090b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2091b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2092b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
20932a4b2251SJamie Gritton 		goto done;
20940304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
20950304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2096b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
20972a4b2251SJamie Gritton 		goto done;
20980304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
20990304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
21002a4b2251SJamie Gritton 		goto done;
21010304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2102b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2103b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21042a4b2251SJamie Gritton 		goto done;
21050304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2106b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21072a4b2251SJamie Gritton 		goto done;
2108b38ff370SJamie Gritton #ifdef INET
2109b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2110b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2111b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21122a4b2251SJamie Gritton 		goto done;
2113b38ff370SJamie Gritton #endif
2114b38ff370SJamie Gritton #ifdef INET6
2115b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2116b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2117b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21182a4b2251SJamie Gritton 		goto done;
2119b38ff370SJamie Gritton #endif
2120b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2121b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2122b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21232a4b2251SJamie Gritton 		goto done;
2124b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2125b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2126b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
21272a4b2251SJamie Gritton 		goto done;
2128b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2129b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2130b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
21312a4b2251SJamie Gritton 		goto done;
2132c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2133b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21342a4b2251SJamie Gritton 		goto done;
2135c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
213676ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
21372a4b2251SJamie Gritton 		goto done;
2138c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
213976ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
21402a4b2251SJamie Gritton 		goto done;
2141841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2142a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
214376ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
214476ca6f88SJamie Gritton 
214576ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
214676ca6f88SJamie Gritton 	} else
214776ca6f88SJamie Gritton #endif
214876ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
214976ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
215076ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
21512a4b2251SJamie Gritton 		goto done;
21520304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
21530304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
21540304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
21552a4b2251SJamie Gritton 		goto done;
21560cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
21570cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
21580cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
21592a4b2251SJamie Gritton 		goto done;
2160672756aaSJamie Gritton 	for (bf = pr_flag_bool;
2161672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2162672756aaSJamie Gritton 	     bf++) {
2163672756aaSJamie Gritton 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2164672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2165b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
21662a4b2251SJamie Gritton 			goto done;
2167b38ff370SJamie Gritton 		i = !i;
2168672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2169b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
21702a4b2251SJamie Gritton 			goto done;
21710304c731SJamie Gritton 	}
2172672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
2173672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2174672756aaSJamie Gritton 	     jsf++) {
2175672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
2176672756aaSJamie Gritton 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2177672756aaSJamie Gritton 		    : (f == jsf->new) ? JAIL_SYS_NEW
21787cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
2179672756aaSJamie Gritton 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
21807cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
21812a4b2251SJamie Gritton 			goto done;
21827cbf7213SJamie Gritton 	}
2183672756aaSJamie Gritton 	for (bf = pr_flag_allow;
21845d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
21855d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
2186672756aaSJamie Gritton 	     bf++) {
2187672756aaSJamie Gritton 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2188672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
21890304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21902a4b2251SJamie Gritton 			goto done;
21910304c731SJamie Gritton 		i = !i;
2192672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
21930304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21942a4b2251SJamie Gritton 			goto done;
21950304c731SJamie Gritton 	}
219676ad42abSJamie Gritton 	i = !prison_isalive(pr);
2197b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2198b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21992a4b2251SJamie Gritton 		goto done;
2200b38ff370SJamie Gritton 	i = !i;
2201b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2202b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
22032a4b2251SJamie Gritton 		goto done;
2204bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2205bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2206a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
22072a4b2251SJamie Gritton 		goto done;
2208a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2209a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
22102a4b2251SJamie Gritton 		goto done;
2211b38ff370SJamie Gritton 
2212b38ff370SJamie Gritton 	/* Get the module parameters. */
2213b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
22142a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
2215b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2216b38ff370SJamie Gritton 	if (error)
22172a4b2251SJamie Gritton 		goto done;
22182a4b2251SJamie Gritton 	prison_deref(pr, drflags);
22192a4b2251SJamie Gritton 	pr = NULL;
22202a4b2251SJamie Gritton 	drflags = 0;
2221b38ff370SJamie Gritton 
2222b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2223b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2224b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2225b38ff370SJamie Gritton 			error = EINVAL;
2226b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
22272a4b2251SJamie Gritton 			goto done;
2228b38ff370SJamie Gritton 		}
2229b38ff370SJamie Gritton 	}
2230b38ff370SJamie Gritton 
2231b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2232b38ff370SJamie Gritton 	error = 0;
2233b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2234b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2235b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2236b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2237b38ff370SJamie Gritton 			if (opt->value != NULL) {
2238b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2239b38ff370SJamie Gritton 					bcopy(opt->value,
2240b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2241b38ff370SJamie Gritton 					    opt->len);
2242b38ff370SJamie Gritton 				} else {
2243b38ff370SJamie Gritton 					error = copyout(opt->value,
2244b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2245b38ff370SJamie Gritton 					    opt->len);
2246b38ff370SJamie Gritton 					if (error)
2247b38ff370SJamie Gritton 						break;
2248b38ff370SJamie Gritton 				}
2249b38ff370SJamie Gritton 			}
2250b38ff370SJamie Gritton 		}
2251b38ff370SJamie Gritton 	}
2252b38ff370SJamie Gritton 
22532a4b2251SJamie Gritton  done:
22542a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
22552a4b2251SJamie Gritton 	if (pr != NULL)
22562a4b2251SJamie Gritton 		prison_deref(pr, drflags);
22572a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2258b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2259b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
22602a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2261b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2262b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2263b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2264b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2265b38ff370SJamie Gritton 				bcopy(errmsg,
2266b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2267b38ff370SJamie Gritton 				    errmsg_len);
2268b38ff370SJamie Gritton 			else
2269b38ff370SJamie Gritton 				copyout(errmsg,
2270b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2271b38ff370SJamie Gritton 				    errmsg_len);
2272b38ff370SJamie Gritton 		}
2273b38ff370SJamie Gritton 	}
2274b38ff370SJamie Gritton 	vfs_freeopts(opts);
2275b38ff370SJamie Gritton 	return (error);
2276b38ff370SJamie Gritton }
2277b38ff370SJamie Gritton 
2278b38ff370SJamie Gritton /*
2279b38ff370SJamie Gritton  * struct jail_remove_args {
2280b38ff370SJamie Gritton  *	int jid;
2281b38ff370SJamie Gritton  * };
2282b38ff370SJamie Gritton  */
2283b38ff370SJamie Gritton int
22848451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2285b38ff370SJamie Gritton {
2286c861373bSJamie Gritton 	struct prison *pr;
2287c861373bSJamie Gritton 	int error;
2288b38ff370SJamie Gritton 
2289b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2290b38ff370SJamie Gritton 	if (error)
2291b38ff370SJamie Gritton 		return (error);
2292b38ff370SJamie Gritton 
2293b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
22940304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2295b38ff370SJamie Gritton 	if (pr == NULL) {
2296b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2297b38ff370SJamie Gritton 		return (EINVAL);
2298b38ff370SJamie Gritton 	}
2299c861373bSJamie Gritton 	if (!prison_isalive(pr)) {
2300c861373bSJamie Gritton 		/* Silently ignore already-dying prisons. */
23010304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2302c861373bSJamie Gritton 		sx_xunlock(&allprison_lock);
23030304c731SJamie Gritton 		return (0);
23040304c731SJamie Gritton 	}
2305c861373bSJamie Gritton 	prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2306c861373bSJamie Gritton 	return (0);
230775c13541SPoul-Henning Kamp }
230875c13541SPoul-Henning Kamp 
2309fd7a8150SMike Barcroft /*
23109ddb7954SMike Barcroft  * struct jail_attach_args {
23119ddb7954SMike Barcroft  *	int jid;
23129ddb7954SMike Barcroft  * };
2313fd7a8150SMike Barcroft  */
2314fd7a8150SMike Barcroft int
23158451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2316fd7a8150SMike Barcroft {
2317b38ff370SJamie Gritton 	struct prison *pr;
2318b38ff370SJamie Gritton 	int error;
2319b38ff370SJamie Gritton 
2320b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2321b38ff370SJamie Gritton 	if (error)
2322b38ff370SJamie Gritton 		return (error);
2323b38ff370SJamie Gritton 
2324f7496dcaSJamie Gritton 	sx_slock(&allprison_lock);
23250304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2326b38ff370SJamie Gritton 	if (pr == NULL) {
2327b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2328b38ff370SJamie Gritton 		return (EINVAL);
2329b38ff370SJamie Gritton 	}
2330b38ff370SJamie Gritton 
233176ad42abSJamie Gritton 	/* Do not allow a process to attach to a prison that is not alive. */
233276ad42abSJamie Gritton 	if (!prison_isalive(pr)) {
2333b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2334b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2335b38ff370SJamie Gritton 		return (EINVAL);
2336b38ff370SJamie Gritton 	}
2337b38ff370SJamie Gritton 
2338f7496dcaSJamie Gritton 	return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2339b38ff370SJamie Gritton }
2340b38ff370SJamie Gritton 
2341b38ff370SJamie Gritton static int
2342f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2343b38ff370SJamie Gritton {
2344fd7a8150SMike Barcroft 	struct proc *p;
2345fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
23465050aa86SKonstantin Belousov 	int error;
2347fd7a8150SMike Barcroft 
2348f7496dcaSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2349f7496dcaSJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
2350589e4c1dSJamie Gritton 	drflags &= PD_LOCK_FLAGS;
235157f22bd4SJacques Vidrine 	/*
235257f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
235357f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
235457f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
235557f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
235657f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
235757f22bd4SJacques Vidrine 	 * of another.
235857f22bd4SJacques Vidrine 	 */
23591158508aSJamie Gritton 	prison_hold(pr);
23606754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
2361f7496dcaSJamie Gritton 	drflags |= PD_DEREF | PD_DEUREF;
2362fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2363f7496dcaSJamie Gritton 	drflags &= ~PD_LOCKED;
2364b38ff370SJamie Gritton 
2365b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2366b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2367b38ff370SJamie Gritton 	if (error) {
2368f7496dcaSJamie Gritton 		prison_deref(pr, drflags);
2369b38ff370SJamie Gritton 		return (error);
2370b38ff370SJamie Gritton 	}
2371f7496dcaSJamie Gritton 	sx_unlock(&allprison_lock);
2372f7496dcaSJamie Gritton 	drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2373fd7a8150SMike Barcroft 
2374413628a7SBjoern A. Zeeb 	/*
2375413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2376413628a7SBjoern A. Zeeb 	 */
2377b38ff370SJamie Gritton 	p = td->td_proc;
2378413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2379413628a7SBjoern A. Zeeb 	if (error)
2380b38ff370SJamie Gritton 		goto e_revert_osd;
2381413628a7SBjoern A. Zeeb 
2382cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2383fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2384fd7a8150SMike Barcroft 		goto e_unlock;
2385fd7a8150SMike Barcroft #ifdef MAC
238630d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2387fd7a8150SMike Barcroft 		goto e_unlock;
2388fd7a8150SMike Barcroft #endif
2389b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2390d4380c0cSJamie Gritton 	if ((error = pwd_chroot_chdir(td, pr->pr_root)))
23915050aa86SKonstantin Belousov 		goto e_revert_osd;
2392fd7a8150SMike Barcroft 
2393fd7a8150SMike Barcroft 	newcred = crget();
2394fd7a8150SMike Barcroft 	PROC_LOCK(p);
23951fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
239669c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2397daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
23981fb6767dSJamie Gritton 	setsugid(p);
2399097055e2SEdward Tomasz Napierala #ifdef RACCT
2400097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2401f87beb93SAndriy Gapon 	crhold(newcred);
2402f87beb93SAndriy Gapon #endif
2403f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
2404f87beb93SAndriy Gapon #ifdef RCTL
2405f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
2406f87beb93SAndriy Gapon 	crfree(newcred);
2407097055e2SEdward Tomasz Napierala #endif
2408f7496dcaSJamie Gritton 	prison_deref(oldcred->cr_prison, drflags);
2409fd7a8150SMike Barcroft 	crfree(oldcred);
2410cc7b7306SJamie Gritton 
2411cc7b7306SJamie Gritton 	/*
2412cc7b7306SJamie Gritton 	 * If the prison was killed while changing credentials, die along
2413cc7b7306SJamie Gritton 	 * with it.
2414cc7b7306SJamie Gritton 	 */
2415cc7b7306SJamie Gritton 	if (!prison_isalive(pr)) {
2416cc7b7306SJamie Gritton 		PROC_LOCK(p);
2417cc7b7306SJamie Gritton 		kern_psignal(p, SIGKILL);
2418cc7b7306SJamie Gritton 		PROC_UNLOCK(p);
2419cc7b7306SJamie Gritton 	}
2420cc7b7306SJamie Gritton 
2421fd7a8150SMike Barcroft 	return (0);
24221fb6767dSJamie Gritton 
2423fd7a8150SMike Barcroft  e_unlock:
2424b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2425b38ff370SJamie Gritton  e_revert_osd:
2426b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
24277f4e7248SJamie Gritton 	sx_slock(&allprison_lock);
2428f7496dcaSJamie Gritton 	drflags |= PD_LIST_SLOCKED;
24291fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2430f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2431fd7a8150SMike Barcroft 	return (error);
2432fd7a8150SMike Barcroft }
2433fd7a8150SMike Barcroft 
2434fd7a8150SMike Barcroft /*
2435fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2436fd7a8150SMike Barcroft  */
243754b369c1SPawel Jakub Dawidek struct prison *
2438fd7a8150SMike Barcroft prison_find(int prid)
2439fd7a8150SMike Barcroft {
2440fd7a8150SMike Barcroft 	struct prison *pr;
2441fd7a8150SMike Barcroft 
2442dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2443b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2444f7496dcaSJamie Gritton 		if (pr->pr_id < prid)
2445f7496dcaSJamie Gritton 			continue;
24467de883c8SJamie Gritton 		if (pr->pr_id > prid)
24477de883c8SJamie Gritton 			break;
2448f7496dcaSJamie Gritton 		KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2449f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
2450f7496dcaSJamie Gritton 		return (pr);
2451fd7a8150SMike Barcroft 	}
2452fd7a8150SMike Barcroft 	return (NULL);
2453fd7a8150SMike Barcroft }
2454fd7a8150SMike Barcroft 
2455b38ff370SJamie Gritton /*
24560304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2457b38ff370SJamie Gritton  */
2458b38ff370SJamie Gritton struct prison *
24590304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2460b38ff370SJamie Gritton {
24610304c731SJamie Gritton 	struct prison *pr;
24620304c731SJamie Gritton 	int descend;
2463b38ff370SJamie Gritton 
2464b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24650304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24660304c731SJamie Gritton 		if (pr->pr_id == prid) {
2467f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2468f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
24690304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
24700304c731SJamie Gritton 			return (pr);
24710304c731SJamie Gritton 		}
24720304c731SJamie Gritton 	}
24730304c731SJamie Gritton 	return (NULL);
24740304c731SJamie Gritton }
24750304c731SJamie Gritton 
24760304c731SJamie Gritton /*
24770304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
24780304c731SJamie Gritton  */
24790304c731SJamie Gritton struct prison *
24800304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
24810304c731SJamie Gritton {
24820304c731SJamie Gritton 	struct prison *pr, *deadpr;
24830304c731SJamie Gritton 	size_t mylen;
24840304c731SJamie Gritton 	int descend;
24850304c731SJamie Gritton 
24860304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24870304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2488b38ff370SJamie Gritton 	deadpr = NULL;
24890304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24900304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2491f7496dcaSJamie Gritton 			KASSERT(prison_isvalid(pr),
2492f7496dcaSJamie Gritton 			    ("Found invalid prison %p", pr));
2493f7496dcaSJamie Gritton 			if (prison_isalive(pr)) {
2494b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
2495b38ff370SJamie Gritton 				return (pr);
2496f7496dcaSJamie Gritton 			}
2497b38ff370SJamie Gritton 			deadpr = pr;
2498b38ff370SJamie Gritton 		}
2499b38ff370SJamie Gritton 	}
25000304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2501f7496dcaSJamie Gritton 	if (deadpr != NULL)
2502b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2503b38ff370SJamie Gritton 	return (deadpr);
2504b38ff370SJamie Gritton }
2505b38ff370SJamie Gritton 
2506b38ff370SJamie Gritton /*
25070fe74ae6SJamie Gritton  * See if a prison has the specific flag set.  The prison should be locked,
25080fe74ae6SJamie Gritton  * unless checking for flags that are only set at jail creation (such as
25090fe74ae6SJamie Gritton  * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
25100fe74ae6SJamie Gritton  * to any other prison data.
25110304c731SJamie Gritton  */
25120304c731SJamie Gritton int
25130304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
25140304c731SJamie Gritton {
25150304c731SJamie Gritton 
25160304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
25170304c731SJamie Gritton }
25180304c731SJamie Gritton 
25190304c731SJamie Gritton int
25200304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
25210304c731SJamie Gritton {
25220304c731SJamie Gritton 
25230fe74ae6SJamie Gritton 	return ((cred->cr_prison->pr_allow & flag) != 0);
25240304c731SJamie Gritton }
25250304c731SJamie Gritton 
25260304c731SJamie Gritton /*
2527effad35eSJamie Gritton  * Hold a prison reference, by incrementing pr_ref.  It is generally
2528effad35eSJamie Gritton  * an error to hold a prison that does not already have a reference.
2529effad35eSJamie Gritton  * A prison record will remain valid as long as it has at least one
2530effad35eSJamie Gritton  * reference, and will not be removed as long as either the prison
2531effad35eSJamie Gritton  * mutex or the allprison lock is held (allprison_lock may be shared).
2532effad35eSJamie Gritton  */
2533effad35eSJamie Gritton void
2534effad35eSJamie Gritton prison_hold_locked(struct prison *pr)
2535effad35eSJamie Gritton {
2536effad35eSJamie Gritton 
25376754ae25SJamie Gritton 	/* Locking is no longer required. */
25386754ae25SJamie Gritton 	prison_hold(pr);
2539effad35eSJamie Gritton }
2540effad35eSJamie Gritton 
2541effad35eSJamie Gritton void
2542effad35eSJamie Gritton prison_hold(struct prison *pr)
2543effad35eSJamie Gritton {
25446754ae25SJamie Gritton #ifdef INVARIANTS
25456754ae25SJamie Gritton 	int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2546effad35eSJamie Gritton 
25476754ae25SJamie Gritton 	KASSERT(was_valid,
25486754ae25SJamie Gritton 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
25496754ae25SJamie Gritton #else
25506754ae25SJamie Gritton 	refcount_acquire(&pr->pr_ref);
25516754ae25SJamie Gritton #endif
2552effad35eSJamie Gritton }
2553effad35eSJamie Gritton 
2554effad35eSJamie Gritton /*
2555effad35eSJamie Gritton  * Remove a prison reference.  If that was the last reference, the
2556f7496dcaSJamie Gritton  * prison will be removed (at a later time).
2557b38ff370SJamie Gritton  */
255891421ba2SRobert Watson void
25591ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
256091421ba2SRobert Watson {
256191421ba2SRobert Watson 
25621ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2563effad35eSJamie Gritton 	/*
2564f7496dcaSJamie Gritton 	 * Locking is no longer required, but unlock because the caller
2565f7496dcaSJamie Gritton 	 * expects it.
2566effad35eSJamie Gritton 	 */
2567f7496dcaSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2568f7496dcaSJamie Gritton 	prison_free(pr);
2569effad35eSJamie Gritton }
2570c2cda609SPawel Jakub Dawidek 
25711ba4a712SPawel Jakub Dawidek void
25721ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
25731ba4a712SPawel Jakub Dawidek {
25741ba4a712SPawel Jakub Dawidek 
25756754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
25766754ae25SJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
25776754ae25SJamie Gritton 	     pr, pr->pr_id));
2578f7496dcaSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_ref)) {
2579f7496dcaSJamie Gritton 		/*
2580f7496dcaSJamie Gritton 		 * Don't remove the last reference in this context,
2581f7496dcaSJamie Gritton 		 * in case there are locks held.
2582f7496dcaSJamie Gritton 		 */
2583f7496dcaSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2584f7496dcaSJamie Gritton 	}
2585f7496dcaSJamie Gritton }
2586f7496dcaSJamie Gritton 
2587f7496dcaSJamie Gritton static void
2588f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr)
2589f7496dcaSJamie Gritton {
2590f7496dcaSJamie Gritton #ifdef INVARIANTS
2591f7496dcaSJamie Gritton 	int lastref;
2592f7496dcaSJamie Gritton 
2593f7496dcaSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
2594f7496dcaSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2595f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2596f7496dcaSJamie Gritton 	lastref = refcount_release(&pr->pr_ref);
2597f7496dcaSJamie Gritton 	KASSERT(!lastref,
2598f7496dcaSJamie Gritton 	    ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2599f7496dcaSJamie Gritton 	     pr, pr->pr_id));
2600f7496dcaSJamie Gritton #else
2601ee9b37aeSMateusz Guzik 	refcount_release(&pr->pr_ref);
2602f7496dcaSJamie Gritton #endif
26031ba4a712SPawel Jakub Dawidek }
26041ba4a712SPawel Jakub Dawidek 
260573d9e52dSJamie Gritton /*
2606effad35eSJamie Gritton  * Hold a a prison for user visibility, by incrementing pr_uref.
2607effad35eSJamie Gritton  * It is generally an error to hold a prison that isn't already
2608effad35eSJamie Gritton  * user-visible, except through the the jail system calls.  It is also
2609effad35eSJamie Gritton  * an error to hold an invalid prison.  A prison record will remain
2610effad35eSJamie Gritton  * alive as long as it has at least one user reference, and will not
2611f7496dcaSJamie Gritton  * be set to the dying state until the prison mutex and allprison_lock
2612f7496dcaSJamie Gritton  * are both freed.
2613effad35eSJamie Gritton  */
2614effad35eSJamie Gritton void
2615effad35eSJamie Gritton prison_proc_hold(struct prison *pr)
2616effad35eSJamie Gritton {
26176754ae25SJamie Gritton #ifdef INVARIANTS
26186754ae25SJamie Gritton 	int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2619effad35eSJamie Gritton 
26206754ae25SJamie Gritton 	KASSERT(was_alive,
2621effad35eSJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
26226754ae25SJamie Gritton #else
26236754ae25SJamie Gritton 	refcount_acquire(&pr->pr_uref);
26246754ae25SJamie Gritton #endif
2625effad35eSJamie Gritton }
2626effad35eSJamie Gritton 
2627effad35eSJamie Gritton /*
2628effad35eSJamie Gritton  * Remove a prison user reference.  If it was the last reference, the
2629effad35eSJamie Gritton  * prison will be considered "dying", and may be removed once all of
2630effad35eSJamie Gritton  * its references are dropped.
2631effad35eSJamie Gritton  */
2632effad35eSJamie Gritton void
2633effad35eSJamie Gritton prison_proc_free(struct prison *pr)
2634effad35eSJamie Gritton {
2635effad35eSJamie Gritton 
26366754ae25SJamie Gritton 	/*
26376754ae25SJamie Gritton 	 * Locking is only required when releasing the last reference.
26386754ae25SJamie Gritton 	 * This allows assurance that a locked prison will remain alive
26396754ae25SJamie Gritton 	 * until it is unlocked.
26406754ae25SJamie Gritton 	 */
26416754ae25SJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2642effad35eSJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2643195cd6aeSJamie Gritton 	if (!refcount_release_if_not_last(&pr->pr_uref)) {
2644effad35eSJamie Gritton 		/*
2645effad35eSJamie Gritton 		 * Don't remove the last user reference in this context,
2646effad35eSJamie Gritton 		 * which is expected to be a process that is not only locked,
2647effad35eSJamie Gritton 		 * but also half dead.  Add a reference so any calls to
2648effad35eSJamie Gritton 		 * prison_free() won't re-submit the task.
2649effad35eSJamie Gritton 		 */
2650f7496dcaSJamie Gritton 		prison_hold(pr);
26511158508aSJamie Gritton 		mtx_lock(&pr->pr_mtx);
26521158508aSJamie Gritton 		KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
26531158508aSJamie Gritton 		    ("Redundant last reference in prison_proc_free (jid=%d)",
26541158508aSJamie Gritton 		     pr->pr_id));
26551158508aSJamie Gritton 		pr->pr_flags |= PR_COMPLETE_PROC;
26561158508aSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2657effad35eSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2658effad35eSJamie Gritton 	}
2659effad35eSJamie Gritton }
2660effad35eSJamie Gritton 
2661c861373bSJamie Gritton static void
2662c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr)
2663c861373bSJamie Gritton {
2664c861373bSJamie Gritton #ifdef INVARIANTS
2665c861373bSJamie Gritton 	int lastref;
2666c861373bSJamie Gritton 
2667c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_uref) > 0,
2668c861373bSJamie Gritton 	    ("Trying to free dead prison %p (jid=%d).",
2669c861373bSJamie Gritton 	     pr, pr->pr_id));
2670c861373bSJamie Gritton 	lastref = refcount_release(&pr->pr_uref);
2671c861373bSJamie Gritton 	KASSERT(!lastref,
2672c861373bSJamie Gritton 	    ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2673c861373bSJamie Gritton 	     pr, pr->pr_id));
2674c861373bSJamie Gritton #else
2675c861373bSJamie Gritton 	refcount_release(&pr->pr_uref);
2676c861373bSJamie Gritton #endif
2677c861373bSJamie Gritton }
2678c861373bSJamie Gritton 
2679effad35eSJamie Gritton /*
268073d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
268173d9e52dSJamie Gritton  */
2682c2cda609SPawel Jakub Dawidek static void
2683c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2684c2cda609SPawel Jakub Dawidek {
268573d9e52dSJamie Gritton 	struct prison *pr = context;
2686f7496dcaSJamie Gritton 	int drflags;
2687b38ff370SJamie Gritton 
2688effad35eSJamie Gritton 	/*
26891158508aSJamie Gritton 	 * This could be called to release the last reference, or the last
26901158508aSJamie Gritton 	 * user reference (plus the reference held in prison_proc_free).
2691effad35eSJamie Gritton 	 */
2692f7496dcaSJamie Gritton 	drflags = prison_lock_xlock(pr, PD_DEREF);
26931158508aSJamie Gritton 	if (pr->pr_flags & PR_COMPLETE_PROC) {
26941158508aSJamie Gritton 		pr->pr_flags &= ~PR_COMPLETE_PROC;
2695f7496dcaSJamie Gritton 		drflags |= PD_DEUREF;
26961158508aSJamie Gritton 	}
2697f7496dcaSJamie Gritton 	prison_deref(pr, drflags);
2698b38ff370SJamie Gritton }
2699b38ff370SJamie Gritton 
2700b38ff370SJamie Gritton /*
2701effad35eSJamie Gritton  * Remove a prison reference and/or user reference (usually).
2702effad35eSJamie Gritton  * This assumes context that allows sleeping (for allprison_lock),
2703effad35eSJamie Gritton  * with no non-sleeping locks held, except perhaps the prison itself.
2704effad35eSJamie Gritton  * If there are no more references, release and delist the prison.
2705effad35eSJamie Gritton  * On completion, the prison lock and the allprison lock are both
2706effad35eSJamie Gritton  * unlocked.
2707b38ff370SJamie Gritton  */
2708b38ff370SJamie Gritton static void
2709b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2710b38ff370SJamie Gritton {
27116e1d1bfcSJamie Gritton 	struct prisonlist freeprison;
2712c861373bSJamie Gritton 	struct prison *killpr, *rpr, *ppr, *tpr;
2713c861373bSJamie Gritton 	struct proc *p;
2714c2cda609SPawel Jakub Dawidek 
2715c861373bSJamie Gritton 	killpr = NULL;
27166e1d1bfcSJamie Gritton 	TAILQ_INIT(&freeprison);
27176e1d1bfcSJamie Gritton 	/*
27186e1d1bfcSJamie Gritton 	 * Release this prison as requested, which may cause its parent
27196e1d1bfcSJamie Gritton 	 * to be released, and then maybe its grandparent, etc.
27206e1d1bfcSJamie Gritton 	 */
27210304c731SJamie Gritton 	for (;;) {
2722c861373bSJamie Gritton 		if (flags & PD_KILL) {
2723c861373bSJamie Gritton 			/* Kill the prison and its descendents. */
2724589e4c1dSJamie Gritton 			KASSERT(pr != &prison0,
2725589e4c1dSJamie Gritton 			    ("prison_deref trying to kill prison0"));
2726c861373bSJamie Gritton 			if (!(flags & PD_DEREF)) {
2727c861373bSJamie Gritton 				prison_hold(pr);
2728c861373bSJamie Gritton 				flags |= PD_DEREF;
2729c861373bSJamie Gritton 			}
2730c861373bSJamie Gritton 			flags = prison_lock_xlock(pr, flags);
2731c861373bSJamie Gritton 			prison_deref_kill(pr, &freeprison);
2732c861373bSJamie Gritton 		}
2733e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
2734f7496dcaSJamie Gritton 			/* Drop a user reference. */
27356754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_uref) > 0,
273673d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
273773d9e52dSJamie Gritton 			     pr->pr_id));
2738f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_uref)) {
2739f7496dcaSJamie Gritton 				if (!(flags & PD_DEREF)) {
2740f7496dcaSJamie Gritton 					prison_hold(pr);
2741f7496dcaSJamie Gritton 					flags |= PD_DEREF;
2742f7496dcaSJamie Gritton 				}
2743f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
27441158508aSJamie Gritton 				if (refcount_release(&pr->pr_uref) &&
27451158508aSJamie Gritton 				    pr->pr_state == PRISON_STATE_ALIVE) {
2746f7496dcaSJamie Gritton 					/*
2747f7496dcaSJamie Gritton 					 * When the last user references goes,
2748f7496dcaSJamie Gritton 					 * this becomes a dying prison.
2749f7496dcaSJamie Gritton 					 */
2750f7496dcaSJamie Gritton 					KASSERT(
2751f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_uref) > 0,
27526754ae25SJamie Gritton 					    ("prison0 pr_uref=0"));
27531158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_DYING;
2754f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
2755f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
2756f7496dcaSJamie Gritton 					(void)osd_jail_call(pr,
2757f7496dcaSJamie Gritton 					    PR_METHOD_REMOVE, NULL);
2758f7496dcaSJamie Gritton 				}
2759f7496dcaSJamie Gritton 			}
2760f7496dcaSJamie Gritton 		}
2761c861373bSJamie Gritton 		if (flags & PD_KILL) {
2762c861373bSJamie Gritton 			/*
2763c861373bSJamie Gritton 			 * Any remaining user references are probably processes
2764c861373bSJamie Gritton 			 * that need to be killed, either in this prison or its
2765c861373bSJamie Gritton 			 * descendants.
2766c861373bSJamie Gritton 			 */
2767c861373bSJamie Gritton 			if (refcount_load(&pr->pr_uref) > 0)
2768c861373bSJamie Gritton 				killpr = pr;
2769589e4c1dSJamie Gritton 			/* Make sure the parent prison doesn't get killed. */
2770589e4c1dSJamie Gritton 			flags &= ~PD_KILL;
2771c861373bSJamie Gritton 		}
277273d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
2773f7496dcaSJamie Gritton 			/* Drop a reference. */
27746754ae25SJamie Gritton 			KASSERT(refcount_load(&pr->pr_ref) > 0,
277573d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
277673d9e52dSJamie Gritton 			     pr->pr_id));
2777f7496dcaSJamie Gritton 			if (!refcount_release_if_not_last(&pr->pr_ref)) {
2778f7496dcaSJamie Gritton 				flags = prison_lock_xlock(pr, flags);
2779f7496dcaSJamie Gritton 				if (refcount_release(&pr->pr_ref)) {
2780cc5fd8c7SJamie Gritton 					/*
2781f7496dcaSJamie Gritton 					 * When the last reference goes,
2782f7496dcaSJamie Gritton 					 * unlink the prison and set it aside.
2783cc5fd8c7SJamie Gritton 					 */
2784f7496dcaSJamie Gritton 					KASSERT(
2785f7496dcaSJamie Gritton 					    refcount_load(&pr->pr_uref) == 0,
2786f7496dcaSJamie Gritton 					    ("prison_deref: last ref, "
2787f7496dcaSJamie Gritton 					     "but still has %d urefs (jid=%d)",
2788f7496dcaSJamie Gritton 					     pr->pr_uref, pr->pr_id));
2789f7496dcaSJamie Gritton 					KASSERT(
2790f7496dcaSJamie Gritton 					    refcount_load(&prison0.pr_ref) != 0,
2791f7496dcaSJamie Gritton 					    ("prison0 pr_ref=0"));
27921158508aSJamie Gritton 					pr->pr_state = PRISON_STATE_INVALID;
2793b38ff370SJamie Gritton 					TAILQ_REMOVE(&allprison, pr, pr_list);
27940304c731SJamie Gritton 					LIST_REMOVE(pr, pr_sibling);
2795f7496dcaSJamie Gritton 					TAILQ_INSERT_TAIL(&freeprison, pr,
2796f7496dcaSJamie Gritton 					    pr_list);
2797f7496dcaSJamie Gritton 					for (ppr = pr->pr_parent;
2798f7496dcaSJamie Gritton 					     ppr != NULL;
2799f7496dcaSJamie Gritton 					     ppr = ppr->pr_parent)
2800f7496dcaSJamie Gritton 						ppr->pr_childcount--;
2801f7496dcaSJamie Gritton 					/*
2802f7496dcaSJamie Gritton 					 * Removing a prison frees references
2803f7496dcaSJamie Gritton 					 * from its parent.
2804f7496dcaSJamie Gritton 					 */
2805f7496dcaSJamie Gritton 					mtx_unlock(&pr->pr_mtx);
2806f7496dcaSJamie Gritton 					flags &= ~PD_LOCKED;
28076e1d1bfcSJamie Gritton 					pr = pr->pr_parent;
28086e1d1bfcSJamie Gritton 					flags |= PD_DEREF | PD_DEUREF;
2809f7496dcaSJamie Gritton 					continue;
2810f7496dcaSJamie Gritton 				}
2811f7496dcaSJamie Gritton 			}
2812f7496dcaSJamie Gritton 		}
2813f7496dcaSJamie Gritton 		break;
28146e1d1bfcSJamie Gritton 	}
28156e1d1bfcSJamie Gritton 
28166e1d1bfcSJamie Gritton 	/* Release all the prison locks. */
2817f7496dcaSJamie Gritton 	if (flags & PD_LOCKED)
2818f7496dcaSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
28196e1d1bfcSJamie Gritton 	if (flags & PD_LIST_SLOCKED)
28206e1d1bfcSJamie Gritton 		sx_sunlock(&allprison_lock);
28216e1d1bfcSJamie Gritton 	else if (flags & PD_LIST_XLOCKED)
28226e1d1bfcSJamie Gritton 		sx_xunlock(&allprison_lock);
28236e1d1bfcSJamie Gritton 
2824c861373bSJamie Gritton 	/* Kill any processes attached to a killed prison. */
2825c861373bSJamie Gritton 	if (killpr != NULL) {
2826c861373bSJamie Gritton 		sx_slock(&allproc_lock);
2827c861373bSJamie Gritton 		FOREACH_PROC_IN_SYSTEM(p) {
2828c861373bSJamie Gritton 			PROC_LOCK(p);
2829c861373bSJamie Gritton 			if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
2830c861373bSJamie Gritton 				for (ppr = p->p_ucred->cr_prison;
2831c861373bSJamie Gritton 				     ppr != &prison0;
2832c861373bSJamie Gritton 				     ppr = ppr->pr_parent)
2833c861373bSJamie Gritton 					if (ppr == killpr) {
2834c861373bSJamie Gritton 						kern_psignal(p, SIGKILL);
2835c861373bSJamie Gritton 						break;
2836c861373bSJamie Gritton 					}
2837c861373bSJamie Gritton 			}
2838c861373bSJamie Gritton 			PROC_UNLOCK(p);
2839c861373bSJamie Gritton 		}
2840c861373bSJamie Gritton 		sx_sunlock(&allproc_lock);
2841c861373bSJamie Gritton 	}
2842c861373bSJamie Gritton 
28436e1d1bfcSJamie Gritton 	/*
28446e1d1bfcSJamie Gritton 	 * Finish removing any unreferenced prisons, which couldn't happen
28456e1d1bfcSJamie Gritton 	 * while allprison_lock was held (to avoid a LOR on vrele).
28466e1d1bfcSJamie Gritton 	 */
28476e1d1bfcSJamie Gritton 	TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
28486e1d1bfcSJamie Gritton #ifdef VIMAGE
28496e1d1bfcSJamie Gritton 		if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
28506e1d1bfcSJamie Gritton 			vnet_destroy(rpr->pr_vnet);
28516e1d1bfcSJamie Gritton #endif
28526e1d1bfcSJamie Gritton 		if (rpr->pr_root != NULL)
28536e1d1bfcSJamie Gritton 			vrele(rpr->pr_root);
28546e1d1bfcSJamie Gritton 		mtx_destroy(&rpr->pr_mtx);
28556e1d1bfcSJamie Gritton #ifdef INET
28566e1d1bfcSJamie Gritton 		free(rpr->pr_ip4, M_PRISON);
28576e1d1bfcSJamie Gritton #endif
28586e1d1bfcSJamie Gritton #ifdef INET6
28596e1d1bfcSJamie Gritton 		free(rpr->pr_ip6, M_PRISON);
28606e1d1bfcSJamie Gritton #endif
28616e1d1bfcSJamie Gritton 		if (rpr->pr_cpuset != NULL)
28626e1d1bfcSJamie Gritton 			cpuset_rel(rpr->pr_cpuset);
28636e1d1bfcSJamie Gritton 		osd_jail_exit(rpr);
28646e1d1bfcSJamie Gritton #ifdef RACCT
28656e1d1bfcSJamie Gritton 		if (racct_enable)
28666e1d1bfcSJamie Gritton 			prison_racct_detach(rpr);
28676e1d1bfcSJamie Gritton #endif
2868f7496dcaSJamie Gritton 		TAILQ_REMOVE(&freeprison, rpr, pr_list);
28696e1d1bfcSJamie Gritton 		free(rpr, M_PRISON);
28700304c731SJamie Gritton 	}
2871b3059e09SRobert Watson }
2872b3059e09SRobert Watson 
2873413628a7SBjoern A. Zeeb /*
2874c861373bSJamie Gritton  * Kill the prison and its descendants.  Mark them as dying, clear the
2875c861373bSJamie Gritton  * persist flag, and call module remove methods.
2876c861373bSJamie Gritton  */
2877c861373bSJamie Gritton static void
2878c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
2879c861373bSJamie Gritton {
2880c861373bSJamie Gritton 	struct prison *cpr, *ppr, *rpr;
2881c861373bSJamie Gritton 	bool descend;
2882c861373bSJamie Gritton 
2883c861373bSJamie Gritton 	/*
2884c861373bSJamie Gritton 	 * Unlike the descendants, the target prison can be killed
2885c861373bSJamie Gritton 	 * even if it is currently dying.  This is useful for failed
2886c861373bSJamie Gritton 	 * creation in jail_set(2).
2887c861373bSJamie Gritton 	 */
2888c861373bSJamie Gritton 	KASSERT(refcount_load(&pr->pr_ref) > 0,
2889c861373bSJamie Gritton 	    ("Trying to kill dead prison %p (jid=%d).",
2890c861373bSJamie Gritton 	     pr, pr->pr_id));
2891c861373bSJamie Gritton 	refcount_acquire(&pr->pr_uref);
2892c861373bSJamie Gritton 	pr->pr_state = PRISON_STATE_DYING;
2893c861373bSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2894c861373bSJamie Gritton 
2895c861373bSJamie Gritton 	rpr = NULL;
2896c861373bSJamie Gritton 	FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
2897c861373bSJamie Gritton 		if (descend) {
2898c861373bSJamie Gritton 			if (!prison_isalive(cpr)) {
2899c861373bSJamie Gritton 				descend = false;
2900c861373bSJamie Gritton 				continue;
2901c861373bSJamie Gritton 			}
2902c861373bSJamie Gritton 			prison_hold(cpr);
2903c861373bSJamie Gritton 			prison_proc_hold(cpr);
2904c861373bSJamie Gritton 			mtx_lock(&cpr->pr_mtx);
2905c861373bSJamie Gritton 			cpr->pr_state = PRISON_STATE_DYING;
2906c861373bSJamie Gritton 			cpr->pr_flags |= PR_REMOVE;
2907c861373bSJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
2908c861373bSJamie Gritton 			continue;
2909c861373bSJamie Gritton 		}
2910c861373bSJamie Gritton 		if (!(cpr->pr_flags & PR_REMOVE))
2911c861373bSJamie Gritton 			continue;
2912c861373bSJamie Gritton 		(void)osd_jail_call(cpr, PR_METHOD_REMOVE, NULL);
2913c861373bSJamie Gritton 		mtx_lock(&cpr->pr_mtx);
2914c861373bSJamie Gritton 		cpr->pr_flags &= ~PR_REMOVE;
2915c861373bSJamie Gritton 		if (cpr->pr_flags & PR_PERSIST) {
2916c861373bSJamie Gritton 			cpr->pr_flags &= ~PR_PERSIST;
2917c861373bSJamie Gritton 			prison_proc_free_not_last(cpr);
2918c861373bSJamie Gritton 			prison_free_not_last(cpr);
2919c861373bSJamie Gritton 		}
2920c861373bSJamie Gritton 		(void)refcount_release(&cpr->pr_uref);
2921c861373bSJamie Gritton 		if (refcount_release(&cpr->pr_ref)) {
2922c861373bSJamie Gritton 			/*
2923c861373bSJamie Gritton 			 * When the last reference goes, unlink the prison
2924c861373bSJamie Gritton 			 * and set it aside for prison_deref() to handle.
2925c861373bSJamie Gritton 			 * Delay unlinking the sibling list to keep the loop
2926c861373bSJamie Gritton 			 * safe.
2927c861373bSJamie Gritton 			 */
2928c861373bSJamie Gritton 			if (rpr != NULL)
2929c861373bSJamie Gritton 				LIST_REMOVE(rpr, pr_sibling);
2930c861373bSJamie Gritton 			rpr = cpr;
2931c861373bSJamie Gritton 			rpr->pr_state = PRISON_STATE_INVALID;
2932c861373bSJamie Gritton 			TAILQ_REMOVE(&allprison, rpr, pr_list);
2933c861373bSJamie Gritton 			TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
2934c861373bSJamie Gritton 			/*
2935c861373bSJamie Gritton 			 * Removing a prison frees references from its parent.
2936c861373bSJamie Gritton 			 */
2937c861373bSJamie Gritton 			ppr = rpr->pr_parent;
2938c861373bSJamie Gritton 			prison_proc_free_not_last(ppr);
2939c861373bSJamie Gritton 			prison_free_not_last(ppr);
2940c861373bSJamie Gritton 			for (; ppr != NULL; ppr = ppr->pr_parent)
2941c861373bSJamie Gritton 				ppr->pr_childcount--;
2942c861373bSJamie Gritton 		}
2943c861373bSJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
2944c861373bSJamie Gritton 	}
2945c861373bSJamie Gritton 	if (rpr != NULL)
2946c861373bSJamie Gritton 		LIST_REMOVE(rpr, pr_sibling);
2947c861373bSJamie Gritton 
2948c861373bSJamie Gritton 	(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2949c861373bSJamie Gritton 	mtx_lock(&pr->pr_mtx);
2950c861373bSJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2951c861373bSJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2952c861373bSJamie Gritton 		prison_proc_free_not_last(pr);
2953c861373bSJamie Gritton 		prison_free_not_last(pr);
2954c861373bSJamie Gritton 	}
2955c861373bSJamie Gritton 	(void)refcount_release(&pr->pr_uref);
2956c861373bSJamie Gritton }
2957c861373bSJamie Gritton 
2958c861373bSJamie Gritton /*
2959f7496dcaSJamie Gritton  * Given the current locking state in the flags, make sure allprison_lock
2960f7496dcaSJamie Gritton  * is held exclusive, and the prison is locked.  Return flags indicating
2961f7496dcaSJamie Gritton  * the new state.
2962f7496dcaSJamie Gritton  */
2963f7496dcaSJamie Gritton static int
2964f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags)
2965f7496dcaSJamie Gritton {
2966f7496dcaSJamie Gritton 
2967f7496dcaSJamie Gritton 	if (!(flags & PD_LIST_XLOCKED)) {
2968f7496dcaSJamie Gritton 		/*
2969f7496dcaSJamie Gritton 		 * Get allprison_lock, which may be an upgrade,
2970f7496dcaSJamie Gritton 		 * and may require unlocking the prison.
2971f7496dcaSJamie Gritton 		 */
2972f7496dcaSJamie Gritton 		if (flags & PD_LOCKED) {
2973f7496dcaSJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2974f7496dcaSJamie Gritton 			flags &= ~PD_LOCKED;
2975f7496dcaSJamie Gritton 		}
2976f7496dcaSJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2977f7496dcaSJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2978f7496dcaSJamie Gritton 				sx_sunlock(&allprison_lock);
2979f7496dcaSJamie Gritton 				sx_xlock(&allprison_lock);
2980f7496dcaSJamie Gritton 			}
2981f7496dcaSJamie Gritton 			flags &= ~PD_LIST_SLOCKED;
2982f7496dcaSJamie Gritton 		} else
2983f7496dcaSJamie Gritton 			sx_xlock(&allprison_lock);
2984f7496dcaSJamie Gritton 		flags |= PD_LIST_XLOCKED;
2985f7496dcaSJamie Gritton 	}
2986f7496dcaSJamie Gritton 	if (!(flags & PD_LOCKED)) {
2987f7496dcaSJamie Gritton 		/* Lock the prison mutex. */
2988f7496dcaSJamie Gritton 		mtx_lock(&pr->pr_mtx);
2989f7496dcaSJamie Gritton 		flags |= PD_LOCKED;
2990f7496dcaSJamie Gritton 	}
2991f7496dcaSJamie Gritton 	return flags;
2992f7496dcaSJamie Gritton }
2993f7496dcaSJamie Gritton 
2994f7496dcaSJamie Gritton /*
29950fe74ae6SJamie Gritton  * Set or clear a permission bit in the pr_allow field, passing restrictions
29960fe74ae6SJamie Gritton  * (cleared permission) down to child jails.
29970fe74ae6SJamie Gritton  */
29980fe74ae6SJamie Gritton void
29990fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable)
30000fe74ae6SJamie Gritton {
30010fe74ae6SJamie Gritton 	struct prison *pr;
30020fe74ae6SJamie Gritton 
30030fe74ae6SJamie Gritton 	pr = cred->cr_prison;
30040fe74ae6SJamie Gritton 	sx_slock(&allprison_lock);
30050fe74ae6SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30060fe74ae6SJamie Gritton 	prison_set_allow_locked(pr, flag, enable);
30070fe74ae6SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
30080fe74ae6SJamie Gritton 	sx_sunlock(&allprison_lock);
30090fe74ae6SJamie Gritton }
30100fe74ae6SJamie Gritton 
30110fe74ae6SJamie Gritton static void
30120fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
30130fe74ae6SJamie Gritton {
30140fe74ae6SJamie Gritton 	struct prison *cpr;
30150fe74ae6SJamie Gritton 	int descend;
30160fe74ae6SJamie Gritton 
30170fe74ae6SJamie Gritton 	if (enable != 0)
30180fe74ae6SJamie Gritton 		pr->pr_allow |= flag;
30190fe74ae6SJamie Gritton 	else {
30200fe74ae6SJamie Gritton 		pr->pr_allow &= ~flag;
30210fe74ae6SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
30220fe74ae6SJamie Gritton 			cpr->pr_allow &= ~flag;
30230fe74ae6SJamie Gritton 	}
30240fe74ae6SJamie Gritton }
30250fe74ae6SJamie Gritton 
30260fe74ae6SJamie Gritton /*
3027ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3028ca04ba64SJamie Gritton  *
3029ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3030ca04ba64SJamie Gritton  * if not.
3031ca04ba64SJamie Gritton  */
3032ca04ba64SJamie Gritton int
3033ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3034ca04ba64SJamie Gritton {
30350304c731SJamie Gritton 	struct prison *pr;
3036ca04ba64SJamie Gritton 	int error;
3037ca04ba64SJamie Gritton 
3038ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3039ca04ba64SJamie Gritton 
30400304c731SJamie Gritton 	pr = cred->cr_prison;
3041499650a0SJamie Gritton #ifdef VIMAGE
30426bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3043de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
30446bb79563SJamie Gritton 		return (0);
3045499650a0SJamie Gritton #endif
30466bb79563SJamie Gritton 
3047ca04ba64SJamie Gritton 	error = 0;
3048ca04ba64SJamie Gritton 	switch (af)
3049ca04ba64SJamie Gritton 	{
3050ca04ba64SJamie Gritton #ifdef INET
3051ca04ba64SJamie Gritton 	case AF_INET:
30520304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
30530304c731SJamie Gritton 		{
30540304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
30550304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3056ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
30570304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
30580304c731SJamie Gritton 		}
3059ca04ba64SJamie Gritton 		break;
3060ca04ba64SJamie Gritton #endif
3061ca04ba64SJamie Gritton #ifdef INET6
3062ca04ba64SJamie Gritton 	case AF_INET6:
30630304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
30640304c731SJamie Gritton 		{
30650304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
30660304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3067ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
30680304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
30690304c731SJamie Gritton 		}
3070ca04ba64SJamie Gritton 		break;
3071ca04ba64SJamie Gritton #endif
3072ca04ba64SJamie Gritton 	case AF_LOCAL:
3073ca04ba64SJamie Gritton 	case AF_ROUTE:
3074ca04ba64SJamie Gritton 		break;
3075ca04ba64SJamie Gritton 	default:
30760304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3077ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3078ca04ba64SJamie Gritton 	}
3079ca04ba64SJamie Gritton 	return (error);
3080ca04ba64SJamie Gritton }
3081ca04ba64SJamie Gritton 
3082ca04ba64SJamie Gritton /*
3083413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3084413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3085413628a7SBjoern A. Zeeb  *
30860304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
30870304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
30880304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3089413628a7SBjoern A. Zeeb  */
3090413628a7SBjoern A. Zeeb int
3091c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa)
309275c13541SPoul-Henning Kamp {
3093413628a7SBjoern A. Zeeb #ifdef INET
3094c83dda36SAlexander V. Chernikov 	const struct sockaddr_in *sai;
3095413628a7SBjoern A. Zeeb #endif
3096413628a7SBjoern A. Zeeb #ifdef INET6
3097c83dda36SAlexander V. Chernikov 	const struct sockaddr_in6 *sai6;
3098413628a7SBjoern A. Zeeb #endif
3099b89e82ddSJamie Gritton 	int error;
310075c13541SPoul-Henning Kamp 
3101413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3102413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3103413628a7SBjoern A. Zeeb 
3104de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3105de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3106de0bd6f7SBjoern A. Zeeb 		return (0);
3107de0bd6f7SBjoern A. Zeeb #endif
3108de0bd6f7SBjoern A. Zeeb 
3109b89e82ddSJamie Gritton 	error = 0;
3110413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3111413628a7SBjoern A. Zeeb 	{
3112413628a7SBjoern A. Zeeb #ifdef INET
3113413628a7SBjoern A. Zeeb 	case AF_INET:
3114c83dda36SAlexander V. Chernikov 		sai = (const struct sockaddr_in *)sa;
3115b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3116413628a7SBjoern A. Zeeb 		break;
3117413628a7SBjoern A. Zeeb #endif
3118413628a7SBjoern A. Zeeb #ifdef INET6
3119413628a7SBjoern A. Zeeb 	case AF_INET6:
3120c83dda36SAlexander V. Chernikov 		sai6 = (const struct sockaddr_in6 *)sa;
3121b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3122413628a7SBjoern A. Zeeb 		break;
3123413628a7SBjoern A. Zeeb #endif
3124413628a7SBjoern A. Zeeb 	default:
31250304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3126b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3127413628a7SBjoern A. Zeeb 	}
3128b89e82ddSJamie Gritton 	return (error);
312975c13541SPoul-Henning Kamp }
313091421ba2SRobert Watson 
313191421ba2SRobert Watson /*
313291421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
313391421ba2SRobert Watson  */
313491421ba2SRobert Watson int
31359ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
313691421ba2SRobert Watson {
313791421ba2SRobert Watson 
31380304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
31390304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
31400304c731SJamie Gritton }
314191421ba2SRobert Watson 
31420304c731SJamie Gritton /*
31430304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
31440304c731SJamie Gritton  */
31450304c731SJamie Gritton int
31460304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
31470304c731SJamie Gritton {
31480304c731SJamie Gritton 
31490304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
31500304c731SJamie Gritton 		if (pr1 == pr2)
31510304c731SJamie Gritton 			return (1);
315291421ba2SRobert Watson 	return (0);
315391421ba2SRobert Watson }
315491421ba2SRobert Watson 
315591421ba2SRobert Watson /*
3156f7496dcaSJamie Gritton  * Return true if the prison is currently alive.  A prison is alive if it
3157f7496dcaSJamie Gritton  * holds user references and it isn't being removed.
315876ad42abSJamie Gritton  */
315976ad42abSJamie Gritton bool
316076ad42abSJamie Gritton prison_isalive(struct prison *pr)
316176ad42abSJamie Gritton {
316276ad42abSJamie Gritton 
31631158508aSJamie Gritton 	if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3164cc7b7306SJamie Gritton 		return (false);
316576ad42abSJamie Gritton 	return (true);
316676ad42abSJamie Gritton }
316776ad42abSJamie Gritton 
316876ad42abSJamie Gritton /*
316976ad42abSJamie Gritton  * Return true if the prison is currently valid.  A prison is valid if it has
317076ad42abSJamie Gritton  * been fully created, and is not being destroyed.  Note that dying prisons
3171f7496dcaSJamie Gritton  * are still considered valid.  Invalid prisons won't be found under normal
3172f7496dcaSJamie Gritton  * circumstances, as they're only put in that state by functions that have
3173f7496dcaSJamie Gritton  * an exclusive hold on allprison_lock.
317476ad42abSJamie Gritton  */
317576ad42abSJamie Gritton bool
317676ad42abSJamie Gritton prison_isvalid(struct prison *pr)
317776ad42abSJamie Gritton {
317876ad42abSJamie Gritton 
31791158508aSJamie Gritton 	if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
31801158508aSJamie Gritton 		return (false);
31816754ae25SJamie Gritton 	if (__predict_false(refcount_load(&pr->pr_ref) == 0))
318276ad42abSJamie Gritton 		return (false);
318376ad42abSJamie Gritton 	return (true);
318476ad42abSJamie Gritton }
318576ad42abSJamie Gritton 
318676ad42abSJamie Gritton /*
3187de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3188de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3189de0bd6f7SBjoern A. Zeeb  */
3190de0bd6f7SBjoern A. Zeeb int
3191de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3192de0bd6f7SBjoern A. Zeeb {
3193de0bd6f7SBjoern A. Zeeb 
3194de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3195de0bd6f7SBjoern A. Zeeb 		return (0);
3196de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3197de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3198de0bd6f7SBjoern A. Zeeb 		return (0);
3199de0bd6f7SBjoern A. Zeeb #endif
3200de0bd6f7SBjoern A. Zeeb 
3201de0bd6f7SBjoern A. Zeeb 	return (1);
3202de0bd6f7SBjoern A. Zeeb }
3203de0bd6f7SBjoern A. Zeeb 
3204de0bd6f7SBjoern A. Zeeb /*
32057455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
32069484d0c0SRobert Drehmel  */
3207ad1ff099SRobert Drehmel void
32089ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
32099484d0c0SRobert Drehmel {
321076ca6f88SJamie Gritton 	struct prison *pr;
32119484d0c0SRobert Drehmel 
32127455b100SJamie Gritton 	/*
32137455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
32147455b100SJamie Gritton 	 * system's hostname.
32157455b100SJamie Gritton 	 */
321676ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
321776ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3218c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
321976ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
32209484d0c0SRobert Drehmel }
3221fd7a8150SMike Barcroft 
32227455b100SJamie Gritton void
32237455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
32247455b100SJamie Gritton {
32257455b100SJamie Gritton 
32267455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3227c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
32287455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
32297455b100SJamie Gritton }
32307455b100SJamie Gritton 
32317455b100SJamie Gritton void
32327455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
32337455b100SJamie Gritton {
32347455b100SJamie Gritton 
32357455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3236c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
32377455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
32387455b100SJamie Gritton }
32397455b100SJamie Gritton 
32407455b100SJamie Gritton void
32417455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
32427455b100SJamie Gritton {
32437455b100SJamie Gritton 
32447455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
32457455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
32467455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
32477455b100SJamie Gritton }
32487455b100SJamie Gritton 
32493f8bc99cSKristof Provost void
32503f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len)
32513f8bc99cSKristof Provost {
32523f8bc99cSKristof Provost 
32533f8bc99cSKristof Provost 	mtx_lock(&cred->cr_prison->pr_mtx);
32543f8bc99cSKristof Provost 	strlcpy(name, cred->cr_prison->pr_name, len);
32553f8bc99cSKristof Provost 	mtx_unlock(&cred->cr_prison->pr_mtx);
32563f8bc99cSKristof Provost }
32573f8bc99cSKristof Provost 
3258eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3259eb79e1c7SBjoern A. Zeeb /*
3260eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3261eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3262eb79e1c7SBjoern A. Zeeb  *
3263eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3264eb79e1c7SBjoern A. Zeeb  */
3265eb79e1c7SBjoern A. Zeeb int
3266eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3267eb79e1c7SBjoern A. Zeeb {
3268eb79e1c7SBjoern A. Zeeb 
3269eb79e1c7SBjoern A. Zeeb 	/*
3270eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3271eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3272eb79e1c7SBjoern A. Zeeb 	 */
3273eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3274eb79e1c7SBjoern A. Zeeb }
3275eb79e1c7SBjoern A. Zeeb #endif
3276eb79e1c7SBjoern A. Zeeb 
3277f08df373SRobert Watson /*
3278820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3279820a0de9SPawel Jakub Dawidek  * status of a mount point.
3280820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3281820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3282820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3283f08df373SRobert Watson  */
3284f08df373SRobert Watson int
3285820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3286f08df373SRobert Watson {
3287820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3288820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3289820a0de9SPawel Jakub Dawidek 	size_t len;
3290f08df373SRobert Watson 
3291820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
32920304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
32930304c731SJamie Gritton 		return (0);
3294820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3295820a0de9SPawel Jakub Dawidek 		return (0);
32960304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3297820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3298820a0de9SPawel Jakub Dawidek 	/*
3299820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3300820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3301820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3302820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3303820a0de9SPawel Jakub Dawidek 	 */
3304820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3305820a0de9SPawel Jakub Dawidek 		return (0);
3306820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3307820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3308820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3309820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3310820a0de9SPawel Jakub Dawidek 	/*
3311820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3312820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3313820a0de9SPawel Jakub Dawidek 	 */
3314820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3315820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3316f08df373SRobert Watson 	return (0);
3317f08df373SRobert Watson }
3318820a0de9SPawel Jakub Dawidek 
3319820a0de9SPawel Jakub Dawidek void
3320820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3321820a0de9SPawel Jakub Dawidek {
3322820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3323820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3324820a0de9SPawel Jakub Dawidek 	size_t len;
3325820a0de9SPawel Jakub Dawidek 
3326820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
33270304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
33280304c731SJamie Gritton 		return;
3329820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3330820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3331820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3332820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3333820a0de9SPawel Jakub Dawidek 		return;
3334820a0de9SPawel Jakub Dawidek 	}
3335820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3336820a0de9SPawel Jakub Dawidek 		/*
3337820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3338820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3339820a0de9SPawel Jakub Dawidek 		 */
3340820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3341820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3342820a0de9SPawel Jakub Dawidek 		return;
3343820a0de9SPawel Jakub Dawidek 	}
3344820a0de9SPawel Jakub Dawidek 	/*
3345820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3346820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3347820a0de9SPawel Jakub Dawidek 	 */
3348820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3349820a0de9SPawel Jakub Dawidek 		return;
3350820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3351820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3352820a0de9SPawel Jakub Dawidek 	/*
3353820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3354820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3355820a0de9SPawel Jakub Dawidek 	 */
3356820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3357820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3358820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3359820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3360820a0de9SPawel Jakub Dawidek 	} else {
3361820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3362820a0de9SPawel Jakub Dawidek 	}
3363f08df373SRobert Watson }
3364f08df373SRobert Watson 
3365800c9408SRobert Watson /*
3366800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3367800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3368800c9408SRobert Watson  */
3369800c9408SRobert Watson int
3370800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3371800c9408SRobert Watson {
33720fe74ae6SJamie Gritton 	struct prison *pr;
33730fe74ae6SJamie Gritton 	int error;
3374800c9408SRobert Watson 
33757b2ff0dcSMateusz Guzik 	/*
33767b2ff0dcSMateusz Guzik 	 * Some policies have custom handlers. This routine should not be
33777b2ff0dcSMateusz Guzik 	 * called for them. See priv_check_cred().
33787b2ff0dcSMateusz Guzik 	 */
33797b2ff0dcSMateusz Guzik 	switch (priv) {
3380a459a6cfSMateusz Guzik 	case PRIV_VFS_LOOKUP:
33817b2ff0dcSMateusz Guzik 	case PRIV_VFS_GENERATION:
33827b2ff0dcSMateusz Guzik 		KASSERT(0, ("prison_priv_check instead of a custom handler "
33837b2ff0dcSMateusz Guzik 		    "called for %d\n", priv));
33847b2ff0dcSMateusz Guzik 	}
33857b2ff0dcSMateusz Guzik 
3386800c9408SRobert Watson 	if (!jailed(cred))
3387800c9408SRobert Watson 		return (0);
3388800c9408SRobert Watson 
33896bb79563SJamie Gritton #ifdef VIMAGE
33906bb79563SJamie Gritton 	/*
33916bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
33926bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
33936bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
33946bb79563SJamie Gritton 	 */
33956bb79563SJamie Gritton 	switch (priv) {
33966bb79563SJamie Gritton #ifdef notyet
33976bb79563SJamie Gritton 		/*
33986bb79563SJamie Gritton 		 * NFS-specific privileges.
33996bb79563SJamie Gritton 		 */
34006bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
34016bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
34026bb79563SJamie Gritton #endif
34036bb79563SJamie Gritton 		/*
34046bb79563SJamie Gritton 		 * Network stack privileges.
34056bb79563SJamie Gritton 		 */
34066bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
34076bb79563SJamie Gritton 	case PRIV_NET_GRE:
34086bb79563SJamie Gritton 	case PRIV_NET_BPF:
34096bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
34106bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
34116bb79563SJamie Gritton 	case PRIV_NET_TAP:
34126bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
34136bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
34146bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3415215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
34166bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
34176bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
34186bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
34196bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
3420389474c1SKonstantin Belousov 	case PRIV_NET_SETLANPCP:
34216bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
34226bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
34236bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
34246bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
34256bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
34266bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
34276bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
34286bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
34296bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
34306bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
34316bb79563SJamie Gritton 	case PRIV_NET_LAGG:
34326bb79563SJamie Gritton 	case PRIV_NET_GIF:
34336bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
343435fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
34356bb79563SJamie Gritton 
34366bb79563SJamie Gritton 		/*
34376bb79563SJamie Gritton 		 * 802.11-related privileges.
34386bb79563SJamie Gritton 		 */
3439f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_GETKEY:
3440f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_MANAGE:
34416bb79563SJamie Gritton 
34426bb79563SJamie Gritton #ifdef notyet
34436bb79563SJamie Gritton 		/*
34446bb79563SJamie Gritton 		 * ATM privileges.
34456bb79563SJamie Gritton 		 */
34466bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
34476bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
34486bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
34496bb79563SJamie Gritton 	case PRIV_NETATM_SET:
34506bb79563SJamie Gritton 
34516bb79563SJamie Gritton 		/*
34526bb79563SJamie Gritton 		 * Bluetooth privileges.
34536bb79563SJamie Gritton 		 */
34546bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
34556bb79563SJamie Gritton #endif
34566bb79563SJamie Gritton 
34576bb79563SJamie Gritton 		/*
34586bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
34596bb79563SJamie Gritton 		 */
34606bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
34616bb79563SJamie Gritton #ifdef notyet
34626bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
34636bb79563SJamie Gritton #endif
34646bb79563SJamie Gritton 
34656bb79563SJamie Gritton 		/*
34666bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
34676bb79563SJamie Gritton 		 */
34686bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
34696bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
34706bb79563SJamie Gritton 	case PRIV_NETINET_PF:
34716bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
34726bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
34736bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
34746bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
34756bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
34766bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
34776bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
34786bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
34796bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
34806bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
34816bb79563SJamie Gritton 
34826bb79563SJamie Gritton #ifdef notyet
34836bb79563SJamie Gritton 		/*
34846bb79563SJamie Gritton 		 * NCP privileges.
34856bb79563SJamie Gritton 		 */
34866bb79563SJamie Gritton 	case PRIV_NETNCP:
34876bb79563SJamie Gritton 
34886bb79563SJamie Gritton 		/*
34896bb79563SJamie Gritton 		 * SMB privileges.
34906bb79563SJamie Gritton 		 */
34916bb79563SJamie Gritton 	case PRIV_NETSMB:
34926bb79563SJamie Gritton #endif
34936bb79563SJamie Gritton 
34946bb79563SJamie Gritton 	/*
34956bb79563SJamie Gritton 	 * No default: or deny here.
34966bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
34976bb79563SJamie Gritton 	 */
34986bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
34996bb79563SJamie Gritton 			return (0);
35006bb79563SJamie Gritton 	}
35016bb79563SJamie Gritton #endif /* VIMAGE */
35026bb79563SJamie Gritton 
3503800c9408SRobert Watson 	switch (priv) {
3504800c9408SRobert Watson 		/*
3505800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3506800c9408SRobert Watson 		 */
3507800c9408SRobert Watson 	case PRIV_KTRACE:
3508800c9408SRobert Watson 
3509c3c1b5e6SRobert Watson #if 0
3510800c9408SRobert Watson 		/*
3511800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3512800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3513800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3514800c9408SRobert Watson 		 * jail.
3515800c9408SRobert Watson 		 */
3516800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3517800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3518800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3519c3c1b5e6SRobert Watson #endif
3520800c9408SRobert Watson 
3521800c9408SRobert Watson 		/*
3522800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3523800c9408SRobert Watson 		 * credentials in any way they see fit.
3524800c9408SRobert Watson 		 */
3525800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3526800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3527800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3528800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3529800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3530800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3531800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3532800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3533800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3534800c9408SRobert Watson 
3535800c9408SRobert Watson 		/*
3536800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3537800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3538800c9408SRobert Watson 		 */
3539800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3540800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3541800c9408SRobert Watson 
3542800c9408SRobert Watson 		/*
3543800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3544800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3545800c9408SRobert Watson 		 */
3546800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3547800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3548800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3549800c9408SRobert Watson 
3550800c9408SRobert Watson 		/*
3551800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3552800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3553800c9408SRobert Watson 		 */
3554800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3555800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3556800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3557800c9408SRobert Watson 
3558800c9408SRobert Watson 		/*
3559800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3560800c9408SRobert Watson 		 */
3561800c9408SRobert Watson 	case PRIV_IPC_READ:
3562800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3563800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3564800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3565800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3566800c9408SRobert Watson 
3567800c9408SRobert Watson 		/*
35680304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
35690304c731SJamie Gritton 		 */
35700304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
35710304c731SJamie Gritton 	case PRIV_JAIL_SET:
35720304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
35730304c731SJamie Gritton 
35740304c731SJamie Gritton 		/*
3575800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3576800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3577800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3578800c9408SRobert Watson 		 */
3579800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3580413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3581800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3582800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3583800c9408SRobert Watson 
3584800c9408SRobert Watson 		/*
3585800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3586800c9408SRobert Watson 		 * writable.
3587800c9408SRobert Watson 		 */
3588800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3589800c9408SRobert Watson 
3590800c9408SRobert Watson 		/*
3591800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3592e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3593e82d0201SRobert Watson 		 * configuration option.
3594800c9408SRobert Watson 		 */
359595b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
359695b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3597800c9408SRobert Watson 
3598800c9408SRobert Watson 		/*
3599800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3600800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3601800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3602800c9408SRobert Watson 		 * privileges.
3603800c9408SRobert Watson 		 */
3604800c9408SRobert Watson 	case PRIV_VFS_READ:
3605800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3606800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3607800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3608800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3609800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3610800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3611800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3612bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3613800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3614800c9408SRobert Watson 	case PRIV_VFS_LINK:
3615800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3616e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3617800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3618bb56d716SJamie Gritton 
3619bb56d716SJamie Gritton 		/*
3620bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
3621bb56d716SJamie Gritton 		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3622bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3623bb56d716SJamie Gritton 		 */
3624bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3625800c9408SRobert Watson 		return (0);
3626800c9408SRobert Watson 
3627800c9408SRobert Watson 		/*
3628800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3629800c9408SRobert Watson 		 * setting system flags.
3630800c9408SRobert Watson 		 */
3631800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
36320304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3633800c9408SRobert Watson 			return (0);
3634800c9408SRobert Watson 		else
3635800c9408SRobert Watson 			return (EPERM);
3636800c9408SRobert Watson 
3637800c9408SRobert Watson 		/*
3638f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3639f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3640f3a8d2f9SPawel Jakub Dawidek 		 */
3641f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3642f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3643f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
364424b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
36450fe74ae6SJamie Gritton 		pr = cred->cr_prison;
36460fe74ae6SJamie Gritton 		prison_lock(pr);
36470fe74ae6SJamie Gritton 		if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
36480fe74ae6SJamie Gritton 			error = 0;
3649f3a8d2f9SPawel Jakub Dawidek 		else
36500fe74ae6SJamie Gritton 			error = EPERM;
36510fe74ae6SJamie Gritton 		prison_unlock(pr);
36520fe74ae6SJamie Gritton 		return (error);
3653f3a8d2f9SPawel Jakub Dawidek 
3654f3a8d2f9SPawel Jakub Dawidek 		/*
365563619b6dSKyle Evans 		 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
365663619b6dSKyle Evans 		 * policy.  priv_check_cred will not specifically allow it, and
365763619b6dSKyle Evans 		 * we may want a MAC policy to allow it.
365863619b6dSKyle Evans 		 */
365963619b6dSKyle Evans 	case PRIV_VFS_READ_DIR:
366063619b6dSKyle Evans 		return (0);
366163619b6dSKyle Evans 
366263619b6dSKyle Evans 		/*
3663ccd6ac9fSAntoine Brodin 		 * Conditionnaly allow locking (unlocking) physical pages
3664ccd6ac9fSAntoine Brodin 		 * in memory.
3665ccd6ac9fSAntoine Brodin 		 */
3666ccd6ac9fSAntoine Brodin 	case PRIV_VM_MLOCK:
3667ccd6ac9fSAntoine Brodin 	case PRIV_VM_MUNLOCK:
3668ccd6ac9fSAntoine Brodin 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3669ccd6ac9fSAntoine Brodin 			return (0);
3670ccd6ac9fSAntoine Brodin 		else
3671ccd6ac9fSAntoine Brodin 			return (EPERM);
3672ccd6ac9fSAntoine Brodin 
3673ccd6ac9fSAntoine Brodin 		/*
3674e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
3675800c9408SRobert Watson 		 */
3676800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
3677e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3678e28f9b7dSAllan Jude 			return (0);
3679e28f9b7dSAllan Jude 		else
3680e28f9b7dSAllan Jude 			return (EPERM);
3681e28f9b7dSAllan Jude 
3682e28f9b7dSAllan Jude 		/*
3683e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
3684e28f9b7dSAllan Jude 		 */
36854b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3686800c9408SRobert Watson 		return (0);
3687800c9408SRobert Watson 
3688800c9408SRobert Watson 		/*
3689e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
369079ba3952SBjoern A. Zeeb 		 */
369179ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
369279ba3952SBjoern A. Zeeb 		return (0);
369379ba3952SBjoern A. Zeeb 
369479ba3952SBjoern A. Zeeb 		/*
3695800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3696800c9408SRobert Watson 		 */
3697800c9408SRobert Watson 	case PRIV_NETINET_RAW:
36980304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3699800c9408SRobert Watson 			return (0);
3700800c9408SRobert Watson 		else
3701800c9408SRobert Watson 			return (EPERM);
3702800c9408SRobert Watson 
3703800c9408SRobert Watson 		/*
3704800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3705800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3706800c9408SRobert Watson 		 * jail.
3707800c9408SRobert Watson 		 */
3708800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3709800c9408SRobert Watson 		return (0);
3710800c9408SRobert Watson 
37112bfc50bcSEdward Tomasz Napierala 		/*
37122bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
37132bfc50bcSEdward Tomasz Napierala 		 */
37142bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
37152bfc50bcSEdward Tomasz Napierala 		return (0);
37162bfc50bcSEdward Tomasz Napierala 
3717b19d66fdSJamie Gritton 		/*
37184520f617SJamie Gritton 		 * Do not allow a process inside a jail to read the kernel
3719b19d66fdSJamie Gritton 		 * message buffer unless explicitly permitted.
3720b19d66fdSJamie Gritton 		 */
3721b19d66fdSJamie Gritton 	case PRIV_MSGBUF:
3722b19d66fdSJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3723b19d66fdSJamie Gritton 			return (0);
3724b19d66fdSJamie Gritton 		return (EPERM);
3725b19d66fdSJamie Gritton 
3726800c9408SRobert Watson 	default:
3727800c9408SRobert Watson 		/*
3728800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3729800c9408SRobert Watson 		 * includes almost all network privileges, many system
3730800c9408SRobert Watson 		 * configuration privileges.
3731800c9408SRobert Watson 		 */
3732800c9408SRobert Watson 		return (EPERM);
3733800c9408SRobert Watson 	}
3734800c9408SRobert Watson }
3735800c9408SRobert Watson 
37360304c731SJamie Gritton /*
37370304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
37380304c731SJamie Gritton  * if it does not directly follow.
37390304c731SJamie Gritton  */
37400304c731SJamie Gritton 
37410304c731SJamie Gritton char *
37420304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
37430304c731SJamie Gritton {
37440304c731SJamie Gritton 	char *name;
37450304c731SJamie Gritton 
37460304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
37470304c731SJamie Gritton 	if (pr1 == pr2)
37480304c731SJamie Gritton 		return "0";
37490304c731SJamie Gritton 	name = pr2->pr_name;
37500304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
37510304c731SJamie Gritton 		/*
37520304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
37530304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
37540304c731SJamie Gritton 		 * can be counted on - and counted.
37550304c731SJamie Gritton 		 */
37560304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
37570304c731SJamie Gritton 			name = strchr(name, '.') + 1;
37580304c731SJamie Gritton 	}
37590304c731SJamie Gritton 	return (name);
37600304c731SJamie Gritton }
37610304c731SJamie Gritton 
37620304c731SJamie Gritton /*
37630304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
37640304c731SJamie Gritton  * if it does not directly follow.
37650304c731SJamie Gritton  */
37660304c731SJamie Gritton static char *
37670304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
37680304c731SJamie Gritton {
37690304c731SJamie Gritton 	char *path1, *path2;
37700304c731SJamie Gritton 	int len1;
37710304c731SJamie Gritton 
37720304c731SJamie Gritton 	path1 = pr1->pr_path;
37730304c731SJamie Gritton 	path2 = pr2->pr_path;
37740304c731SJamie Gritton 	if (!strcmp(path1, "/"))
37750304c731SJamie Gritton 		return (path2);
37760304c731SJamie Gritton 	len1 = strlen(path1);
37770304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
37780304c731SJamie Gritton 		return (path2);
37790304c731SJamie Gritton 	if (path2[len1] == '\0')
37800304c731SJamie Gritton 		return "/";
37810304c731SJamie Gritton 	if (path2[len1] == '/')
37820304c731SJamie Gritton 		return (path2 + len1);
37830304c731SJamie Gritton 	return (path2);
37840304c731SJamie Gritton }
37850304c731SJamie Gritton 
37860304c731SJamie Gritton /*
37870304c731SJamie Gritton  * Jail-related sysctls.
37880304c731SJamie Gritton  */
37897029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
37900304c731SJamie Gritton     "Jails");
37910304c731SJamie Gritton 
3792fd7a8150SMike Barcroft static int
3793fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3794fd7a8150SMike Barcroft {
3795b38ff370SJamie Gritton 	struct xprison *xp;
37960304c731SJamie Gritton 	struct prison *pr, *cpr;
3797b38ff370SJamie Gritton #ifdef INET
3798b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3799b38ff370SJamie Gritton 	int ip4s = 0;
3800b38ff370SJamie Gritton #endif
3801b38ff370SJamie Gritton #ifdef INET6
38023beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
3803b38ff370SJamie Gritton 	int ip6s = 0;
3804b38ff370SJamie Gritton #endif
38050304c731SJamie Gritton 	int descend, error;
3806fd7a8150SMike Barcroft 
3807b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
38080304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3809b38ff370SJamie Gritton 	error = 0;
3810dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
38110304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
38120304c731SJamie Gritton #if defined(INET) || defined(INET6)
3813b38ff370SJamie Gritton  again:
38140304c731SJamie Gritton #endif
38150304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3816413628a7SBjoern A. Zeeb #ifdef INET
38170304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
38180304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
38190304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
38200304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3821b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3822b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3823b38ff370SJamie Gritton 				goto again;
3824b38ff370SJamie Gritton 			}
38250304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
38260304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3827b38ff370SJamie Gritton 		}
3828413628a7SBjoern A. Zeeb #endif
3829413628a7SBjoern A. Zeeb #ifdef INET6
38300304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
38310304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
38320304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
38330304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3834b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3835b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3836b38ff370SJamie Gritton 				goto again;
3837413628a7SBjoern A. Zeeb 			}
38380304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
38390304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3840b38ff370SJamie Gritton 		}
3841b38ff370SJamie Gritton #endif
3842b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
3843fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
38440304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
38451158508aSJamie Gritton 		xp->pr_state = cpr->pr_state;
38460304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3847c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
38480304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3849413628a7SBjoern A. Zeeb #ifdef INET
38500304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
3851413628a7SBjoern A. Zeeb #endif
3852413628a7SBjoern A. Zeeb #ifdef INET6
38530304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
3854413628a7SBjoern A. Zeeb #endif
38550304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3856b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3857b38ff370SJamie Gritton 		if (error)
3858b38ff370SJamie Gritton 			break;
3859413628a7SBjoern A. Zeeb #ifdef INET
3860b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
3861b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
3862b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
3863b38ff370SJamie Gritton 			if (error)
3864b38ff370SJamie Gritton 				break;
3865413628a7SBjoern A. Zeeb 		}
3866413628a7SBjoern A. Zeeb #endif
3867413628a7SBjoern A. Zeeb #ifdef INET6
3868b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
3869b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
3870b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
3871b38ff370SJamie Gritton 			if (error)
3872b38ff370SJamie Gritton 				break;
3873413628a7SBjoern A. Zeeb 		}
3874413628a7SBjoern A. Zeeb #endif
3875fd7a8150SMike Barcroft 	}
3876dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
3877b38ff370SJamie Gritton 	free(xp, M_TEMP);
3878b38ff370SJamie Gritton #ifdef INET
3879b38ff370SJamie Gritton 	free(ip4, M_TEMP);
3880b38ff370SJamie Gritton #endif
3881b38ff370SJamie Gritton #ifdef INET6
3882b38ff370SJamie Gritton 	free(ip6, M_TEMP);
3883b38ff370SJamie Gritton #endif
3884fd7a8150SMike Barcroft 	return (error);
3885fd7a8150SMike Barcroft }
3886fd7a8150SMike Barcroft 
3887f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
3888f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3889f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
3890461167c2SPawel Jakub Dawidek 
3891461167c2SPawel Jakub Dawidek static int
3892461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3893461167c2SPawel Jakub Dawidek {
3894461167c2SPawel Jakub Dawidek 	int error, injail;
3895461167c2SPawel Jakub Dawidek 
3896461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
3897461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3898461167c2SPawel Jakub Dawidek 
3899461167c2SPawel Jakub Dawidek 	return (error);
3900461167c2SPawel Jakub Dawidek }
39010304c731SJamie Gritton 
3902f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3903f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3904f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
3905413628a7SBjoern A. Zeeb 
3906761d2bb5SJamie Gritton static int
3907761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3908761d2bb5SJamie Gritton {
3909761d2bb5SJamie Gritton 	int error, havevnet;
3910761d2bb5SJamie Gritton #ifdef VIMAGE
3911761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
3912761d2bb5SJamie Gritton 
3913761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
3914761d2bb5SJamie Gritton #else
3915761d2bb5SJamie Gritton 	havevnet = 0;
3916761d2bb5SJamie Gritton #endif
3917761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3918761d2bb5SJamie Gritton 
3919761d2bb5SJamie Gritton 	return (error);
3920761d2bb5SJamie Gritton }
3921761d2bb5SJamie Gritton 
3922761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3923761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3924bb8f1623SBjoern A. Zeeb     sysctl_jail_vnet, "I", "Jail owns vnet?");
3925761d2bb5SJamie Gritton 
39260304c731SJamie Gritton #if defined(INET) || defined(INET6)
3927e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
39280304c731SJamie Gritton     &jail_max_af_ips, 0,
3929c542c43eSJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
39300304c731SJamie Gritton #endif
39310304c731SJamie Gritton 
39320304c731SJamie Gritton /*
3933c542c43eSJamie Gritton  * Default parameters for jail(2) compatibility.  For historical reasons,
3934c542c43eSJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
3935c542c43eSJamie Gritton  * just see their own parameters, and can't change them.
39360304c731SJamie Gritton  */
39370304c731SJamie Gritton static int
39380304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
39390304c731SJamie Gritton {
39400fe74ae6SJamie Gritton 	int error, i;
39410304c731SJamie Gritton 
39420304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
39430fe74ae6SJamie Gritton 	if (req->td->td_ucred->cr_prison == &prison0) {
39440fe74ae6SJamie Gritton 		mtx_lock(&prison0.pr_mtx);
39450fe74ae6SJamie Gritton 		i = (jail_default_allow & arg2) != 0;
39460fe74ae6SJamie Gritton 		mtx_unlock(&prison0.pr_mtx);
39470fe74ae6SJamie Gritton 	} else
39480fe74ae6SJamie Gritton 		i = prison_allow(req->td->td_ucred, arg2);
39490fe74ae6SJamie Gritton 
39500304c731SJamie Gritton 	if (arg1 != NULL)
39510304c731SJamie Gritton 		i = !i;
39520304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
3953c542c43eSJamie Gritton 	if (error || !req->newptr)
39540304c731SJamie Gritton 		return (error);
39550304c731SJamie Gritton 	i = i ? arg2 : 0;
39560304c731SJamie Gritton 	if (arg1 != NULL)
39570304c731SJamie Gritton 		i ^= arg2;
39580304c731SJamie Gritton 	/*
39590304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
39600304c731SJamie Gritton 	 * for writing.
39610304c731SJamie Gritton 	 */
39620304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
39630304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
39640304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
39650304c731SJamie Gritton 	return (0);
39660304c731SJamie Gritton }
39670304c731SJamie Gritton 
39680304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3969c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39700304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3971c542c43eSJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
39720304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3973c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39740304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3975c542c43eSJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
39760304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3977c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39780304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3979c542c43eSJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
39800304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3981c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39820304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3983c542c43eSJamie Gritton     "Prison root can create raw sockets (deprecated)");
39840304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3985c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39860304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3987c542c43eSJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
39880304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3989c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
39900304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3991c542c43eSJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
39920304c731SJamie Gritton 
39930304c731SJamie Gritton static int
39940304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
39950304c731SJamie Gritton {
39960304c731SJamie Gritton 	struct prison *pr;
39970304c731SJamie Gritton 	int level, error;
39980304c731SJamie Gritton 
39990304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
40000304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
40010304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
4002c542c43eSJamie Gritton 	if (error || !req->newptr)
40030304c731SJamie Gritton 		return (error);
40040304c731SJamie Gritton 	*(int *)arg1 = level;
40050304c731SJamie Gritton 	return (0);
40060304c731SJamie Gritton }
40070304c731SJamie Gritton 
40080304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4009c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4010c542c43eSJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
40110304c731SJamie Gritton     sysctl_jail_default_level, "I",
4012c542c43eSJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
4013c542c43eSJamie Gritton 
40140cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4015c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4016c542c43eSJamie Gritton     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
40170cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
4018c542c43eSJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
40190cc207a6SMartin Matuska 
40200304c731SJamie Gritton /*
40210304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
40220304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
40230304c731SJamie Gritton  * to make themselves and their types known.
40240304c731SJamie Gritton  */
40257029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
40260304c731SJamie Gritton     "Jail parameters");
40270304c731SJamie Gritton 
40280304c731SJamie Gritton int
40290304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
40300304c731SJamie Gritton {
40310304c731SJamie Gritton 	int i;
40320304c731SJamie Gritton 	long l;
40330304c731SJamie Gritton 	size_t s;
40340304c731SJamie Gritton 	char numbuf[12];
40350304c731SJamie Gritton 
40360304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
40370304c731SJamie Gritton 	{
40380304c731SJamie Gritton 	case CTLTYPE_LONG:
40390304c731SJamie Gritton 	case CTLTYPE_ULONG:
40400304c731SJamie Gritton 		l = 0;
40410304c731SJamie Gritton #ifdef SCTL_MASK32
40420304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
40430304c731SJamie Gritton #endif
40440304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
40450304c731SJamie Gritton 	case CTLTYPE_INT:
40460304c731SJamie Gritton 	case CTLTYPE_UINT:
40470304c731SJamie Gritton 		i = 0;
40480304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
40490304c731SJamie Gritton 	case CTLTYPE_STRING:
4050e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
40510304c731SJamie Gritton 		return
40520304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
40530304c731SJamie Gritton 	case CTLTYPE_STRUCT:
40540304c731SJamie Gritton 		s = (size_t)arg2;
40550304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
40560304c731SJamie Gritton 	}
40570304c731SJamie Gritton 	return (0);
40580304c731SJamie Gritton }
40590304c731SJamie Gritton 
4060b96bd95bSIan Lepore /*
4061b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4062b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
4063b96bd95bSIan Lepore  */
40640304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
40650304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
40660304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
40670304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
40680304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
40690304c731SJamie Gritton     "I", "Jail secure level");
4070b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4071b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
4072b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4073b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
40740304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
40750304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
40760cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
40770cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
40780304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
40790304c731SJamie Gritton     "B", "Jail persistence");
4080679e1390SJamie Gritton #ifdef VIMAGE
4081679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
40827cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4083679e1390SJamie Gritton #endif
40840304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
40850304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
40860304c731SJamie Gritton 
4087b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4088b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4089b97457e2SJamie Gritton     "I", "Current number of child jails");
4090b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4091b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4092b97457e2SJamie Gritton 
40937cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
40940304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
40950304c731SJamie Gritton     "Jail hostname");
409676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
409776ca6f88SJamie Gritton     "Jail NIS domainname");
409876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
409976ca6f88SJamie Gritton     "Jail host UUID");
410076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
410176ca6f88SJamie Gritton     "LU", "Jail host ID");
41020304c731SJamie Gritton 
41030304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
41040304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
41050304c731SJamie Gritton 
41060304c731SJamie Gritton #ifdef INET
41072b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
41082b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
41090304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
41100304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4111592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4112592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4113592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
41140304c731SJamie Gritton #endif
41150304c731SJamie Gritton #ifdef INET6
41162b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
41172b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
41180304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
41190304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4120592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4121592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4122592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
41230304c731SJamie Gritton #endif
41240304c731SJamie Gritton 
41250304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
41260304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
41270304c731SJamie Gritton     "B", "Jail may set hostname");
41280304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
41290304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
41300304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
41310304c731SJamie Gritton     "B", "Jail may create raw sockets");
41320304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
41330304c731SJamie Gritton     "B", "Jail may alter system file flags");
41340304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
41350304c731SJamie Gritton     "B", "Jail may set file quotas");
41360304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
41370304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4138ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4139ccd6ac9fSAntoine Brodin     "B", "Jail may lock (unlock) physical pages in memory");
4140e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4141e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
4142b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4143b19d66fdSJamie Gritton     "B", "Jail may read the kernel message buffer");
4144b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4145b3079544SJamie Gritton     "B", "Unprivileged processes may use process debugging facilities");
414605e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
414705e1e482SMariusz Zaborski     "B", "Processes in jail with uid 0 have privilege");
41480304c731SJamie Gritton 
4149bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4150bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4151bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
41520e5c6bd4SJamie Gritton 
41530e5c6bd4SJamie Gritton /*
41540a172404SJamie Gritton  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
41550a172404SJamie Gritton  * its associated bit in the pr_allow bitmask, or zero if the parameter was
41560a172404SJamie Gritton  * not created.
41570e5c6bd4SJamie Gritton  */
41580a172404SJamie Gritton unsigned
41590a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
41600a172404SJamie Gritton     const char *descr)
41610e5c6bd4SJamie Gritton {
41620e5c6bd4SJamie Gritton 	struct bool_flags *bf;
41630a172404SJamie Gritton 	struct sysctl_oid *parent;
41640a172404SJamie Gritton 	char *allow_name, *allow_noname, *allowed;
4165c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4166c542c43eSJamie Gritton 	char *descr_deprecated;
4167c542c43eSJamie Gritton #endif
41685d58f959SJamie Gritton 	u_int allow_flag;
41690e5c6bd4SJamie Gritton 
41700a172404SJamie Gritton 	if (prefix
41710a172404SJamie Gritton 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
41720a172404SJamie Gritton 		< 0 ||
41730a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
41740a172404SJamie Gritton 		< 0
41750a172404SJamie Gritton 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
41760a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
41770e5c6bd4SJamie Gritton 		free(allow_name, M_PRISON);
41780a172404SJamie Gritton 		return 0;
41790e5c6bd4SJamie Gritton 	}
41800e5c6bd4SJamie Gritton 
41810e5c6bd4SJamie Gritton 	/*
41820a172404SJamie Gritton 	 * See if this parameter has already beed added, i.e. a module was
41830a172404SJamie Gritton 	 * previously loaded/unloaded.
41840e5c6bd4SJamie Gritton 	 */
41850e5c6bd4SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
41860e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow;
41875d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
41885d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
41890e5c6bd4SJamie Gritton 	     bf++) {
41900e5c6bd4SJamie Gritton 		if (strcmp(bf->name, allow_name) == 0) {
41910a172404SJamie Gritton 			allow_flag = bf->flag;
41920e5c6bd4SJamie Gritton 			goto no_add;
41930e5c6bd4SJamie Gritton 		}
41940e5c6bd4SJamie Gritton 	}
41950e5c6bd4SJamie Gritton 
41960e5c6bd4SJamie Gritton 	/*
41975d58f959SJamie Gritton 	 * Find a free bit in pr_allow_all, failing if there are none
41980e5c6bd4SJamie Gritton 	 * (which shouldn't happen as long as we keep track of how many
41990a172404SJamie Gritton 	 * potential dynamic flags exist).
42000e5c6bd4SJamie Gritton 	 */
42010e5c6bd4SJamie Gritton 	for (allow_flag = 1;; allow_flag <<= 1) {
42020e5c6bd4SJamie Gritton 		if (allow_flag == 0)
42030e5c6bd4SJamie Gritton 			goto no_add;
42045d58f959SJamie Gritton 		if ((pr_allow_all & allow_flag) == 0)
42050e5c6bd4SJamie Gritton 			break;
42060e5c6bd4SJamie Gritton 	}
42070e5c6bd4SJamie Gritton 
42085d58f959SJamie Gritton 	/* Note the parameter in the next open slot in pr_flag_allow. */
42095d58f959SJamie Gritton 	for (bf = pr_flag_allow; ; bf++) {
42100e5c6bd4SJamie Gritton 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
42110e5c6bd4SJamie Gritton 			/* This should never happen, but is not fatal. */
42120a172404SJamie Gritton 			allow_flag = 0;
42130e5c6bd4SJamie Gritton 			goto no_add;
42140e5c6bd4SJamie Gritton 		}
42155d58f959SJamie Gritton 		if (atomic_load_int(&bf->flag) == 0)
42165d58f959SJamie Gritton 			break;
42175d58f959SJamie Gritton 	}
42180e5c6bd4SJamie Gritton 	bf->name = allow_name;
42190e5c6bd4SJamie Gritton 	bf->noname = allow_noname;
42205d58f959SJamie Gritton 	pr_allow_all |= allow_flag;
42215d58f959SJamie Gritton 	/*
42225d58f959SJamie Gritton 	 * prison0 always has permission for the new parameter.
42235d58f959SJamie Gritton 	 * Other jails must have it granted to them.
42245d58f959SJamie Gritton 	 */
42255d58f959SJamie Gritton 	prison0.pr_allow |= allow_flag;
42265d58f959SJamie Gritton 	/* The flag indicates a valid entry, so make sure it is set last. */
42275d58f959SJamie Gritton 	atomic_store_rel_int(&bf->flag, allow_flag);
42280e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
42290e5c6bd4SJamie Gritton 
4230c542c43eSJamie Gritton 	/*
4231c542c43eSJamie Gritton 	 * Create sysctls for the paramter, and the back-compat global
4232c542c43eSJamie Gritton 	 * permission.
4233c542c43eSJamie Gritton 	 */
42340a172404SJamie Gritton 	parent = prefix
42350a172404SJamie Gritton 	    ? SYSCTL_ADD_NODE(NULL,
42360a172404SJamie Gritton 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
42377029da5cSPawel Biernacki 		  OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
42380a172404SJamie Gritton 	    : &sysctl___security_jail_param_allow;
42390a172404SJamie Gritton 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
42400a172404SJamie Gritton 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42410e5c6bd4SJamie Gritton 	    NULL, 0, sysctl_jail_param, "B", descr);
42420a172404SJamie Gritton 	if ((prefix
42430a172404SJamie Gritton 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
42440a172404SJamie Gritton 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4245c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4246c542c43eSJamie Gritton 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4247c542c43eSJamie Gritton 		    descr);
4248c542c43eSJamie Gritton #endif
42490e5c6bd4SJamie Gritton 		(void)SYSCTL_ADD_PROC(NULL,
42500a172404SJamie Gritton 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4251c542c43eSJamie Gritton 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4252c542c43eSJamie Gritton 		    sysctl_jail_default_allow, "I", descr_deprecated);
4253c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4254c542c43eSJamie Gritton 		free(descr_deprecated, M_TEMP);
4255c542c43eSJamie Gritton #endif
42560a172404SJamie Gritton 		free(allowed, M_TEMP);
42570e5c6bd4SJamie Gritton 	}
42580a172404SJamie Gritton 	return allow_flag;
42590e5c6bd4SJamie Gritton 
42600e5c6bd4SJamie Gritton  no_add:
42610e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
42620e5c6bd4SJamie Gritton 	free(allow_name, M_PRISON);
42630e5c6bd4SJamie Gritton 	free(allow_noname, M_PRISON);
42640a172404SJamie Gritton 	return allow_flag;
42650a172404SJamie Gritton }
42660a172404SJamie Gritton 
42670a172404SJamie Gritton /*
42680a172404SJamie Gritton  * The VFS system will register jail-aware filesystems here.  They each get
42690a172404SJamie Gritton  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
42700a172404SJamie Gritton  * attempts to mount.
42710a172404SJamie Gritton  */
42720a172404SJamie Gritton void
42730a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp)
42740a172404SJamie Gritton {
42750a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR
42760a172404SJamie Gritton 
42770a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
42780a172404SJamie Gritton 	    NULL, NULL);
42790a172404SJamie Gritton #else
42800a172404SJamie Gritton 	char *descr;
42810a172404SJamie Gritton 
42820a172404SJamie Gritton 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
42830a172404SJamie Gritton 	    vfsp->vfc_name);
42840a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
42850a172404SJamie Gritton 	    NULL, descr);
42860a172404SJamie Gritton 	free(descr, M_TEMP);
42870a172404SJamie Gritton #endif
42880e5c6bd4SJamie Gritton }
4289bf3db8aaSMartin Matuska 
42904b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4291097055e2SEdward Tomasz Napierala void
4292097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
429315db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
429415db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
4295097055e2SEdward Tomasz Napierala {
4296a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4297097055e2SEdward Tomasz Napierala 
42984b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
42994b5c9cf6SEdward Tomasz Napierala 
4300097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
430115db3c07SEdward Tomasz Napierala 	if (pre != NULL)
430215db3c07SEdward Tomasz Napierala 		(pre)();
4303a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4304a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
430515db3c07SEdward Tomasz Napierala 	if (post != NULL)
430615db3c07SEdward Tomasz Napierala 		(post)();
4307097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4308097055e2SEdward Tomasz Napierala }
43090304c731SJamie Gritton 
4310a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4311a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4312a7ad07bfSEdward Tomasz Napierala {
4313a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4314a7ad07bfSEdward Tomasz Napierala 
43154b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4316a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4317a7ad07bfSEdward Tomasz Napierala 
4318a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4319a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4320a7ad07bfSEdward Tomasz Napierala 
4321a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4322a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4323a7ad07bfSEdward Tomasz Napierala 			continue;
4324a7ad07bfSEdward Tomasz Napierala 
4325a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4326a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4327a7ad07bfSEdward Tomasz Napierala 		return (prr);
4328a7ad07bfSEdward Tomasz Napierala 	}
4329a7ad07bfSEdward Tomasz Napierala 
4330a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4331a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4332a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4333a7ad07bfSEdward Tomasz Napierala 
4334a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4335a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4336a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4337a7ad07bfSEdward Tomasz Napierala 
4338a7ad07bfSEdward Tomasz Napierala 	return (prr);
4339a7ad07bfSEdward Tomasz Napierala }
4340a7ad07bfSEdward Tomasz Napierala 
4341a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4342a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4343a7ad07bfSEdward Tomasz Napierala {
4344a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4345a7ad07bfSEdward Tomasz Napierala 
43464b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
43474b5c9cf6SEdward Tomasz Napierala 
4348a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4349a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4350a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4351a7ad07bfSEdward Tomasz Napierala 	return (prr);
4352a7ad07bfSEdward Tomasz Napierala }
4353a7ad07bfSEdward Tomasz Napierala 
4354a7ad07bfSEdward Tomasz Napierala void
4355a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4356a7ad07bfSEdward Tomasz Napierala {
4357a7ad07bfSEdward Tomasz Napierala 
43584b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
43594b5c9cf6SEdward Tomasz Napierala 
4360a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4361a7ad07bfSEdward Tomasz Napierala }
4362a7ad07bfSEdward Tomasz Napierala 
4363c34bbd2aSEdward Tomasz Napierala static void
4364c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4365c34bbd2aSEdward Tomasz Napierala {
4366c34bbd2aSEdward Tomasz Napierala 
43674b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4368c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4369c34bbd2aSEdward Tomasz Napierala 
4370c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4371c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4372c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4373c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4374c34bbd2aSEdward Tomasz Napierala 	}
4375c34bbd2aSEdward Tomasz Napierala }
4376c34bbd2aSEdward Tomasz Napierala 
4377a7ad07bfSEdward Tomasz Napierala void
4378a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4379a7ad07bfSEdward Tomasz Napierala {
4380a7ad07bfSEdward Tomasz Napierala 
43814b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4382c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4383c34bbd2aSEdward Tomasz Napierala 
43846ff4688bSMateusz Guzik 	if (refcount_release_if_not_last(&prr->prr_refcount))
4385a7ad07bfSEdward Tomasz Napierala 		return;
4386a7ad07bfSEdward Tomasz Napierala 
4387a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4388c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4389a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4390a7ad07bfSEdward Tomasz Napierala }
4391a7ad07bfSEdward Tomasz Napierala 
4392a7ad07bfSEdward Tomasz Napierala static void
4393a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4394a7ad07bfSEdward Tomasz Napierala {
4395a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4396a7ad07bfSEdward Tomasz Napierala 
43974b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4398c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4399c34bbd2aSEdward Tomasz Napierala 
4400a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4401a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4402a7ad07bfSEdward Tomasz Napierala 
4403a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4404a7ad07bfSEdward Tomasz Napierala }
4405a7ad07bfSEdward Tomasz Napierala 
4406c34bbd2aSEdward Tomasz Napierala /*
4407c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4408c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4409c34bbd2aSEdward Tomasz Napierala  */
4410c34bbd2aSEdward Tomasz Napierala static void
4411c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4412c34bbd2aSEdward Tomasz Napierala {
4413dbadb015SKonstantin Belousov #ifdef RCTL
4414c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4415c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4416dbadb015SKonstantin Belousov #endif
4417c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4418c34bbd2aSEdward Tomasz Napierala 
44194b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
44204b5c9cf6SEdward Tomasz Napierala 
4421c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4422c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4423c34bbd2aSEdward Tomasz Napierala 
4424e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4425e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4426e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4427c34bbd2aSEdward Tomasz Napierala 		return;
4428e30345e7SEdward Tomasz Napierala 	}
4429c34bbd2aSEdward Tomasz Napierala 
4430c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4431c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4432c34bbd2aSEdward Tomasz Napierala 
4433c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4434c34bbd2aSEdward Tomasz Napierala 
4435c34bbd2aSEdward Tomasz Napierala 	/*
4436c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4437c34bbd2aSEdward Tomasz Napierala 	 */
4438c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4439c34bbd2aSEdward Tomasz Napierala 
4440f87beb93SAndriy Gapon #ifdef RCTL
4441c34bbd2aSEdward Tomasz Napierala 	/*
4442c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4443c34bbd2aSEdward Tomasz Napierala 	 */
4444c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4445c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4446c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4447c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4448f87beb93SAndriy Gapon 		rctl_proc_ucred_changed(p, cred);
4449c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4450c34bbd2aSEdward Tomasz Napierala 	}
4451f87beb93SAndriy Gapon #endif
4452c34bbd2aSEdward Tomasz Napierala 
4453c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4454c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4455c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4456c34bbd2aSEdward Tomasz Napierala }
4457c34bbd2aSEdward Tomasz Napierala 
4458a7ad07bfSEdward Tomasz Napierala static void
4459a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4460a7ad07bfSEdward Tomasz Napierala {
4461c34bbd2aSEdward Tomasz Napierala 
44624b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4463c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4464c34bbd2aSEdward Tomasz Napierala 
4465af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4466af3c786cSMateusz Guzik 		return;
4467a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4468a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4469a7ad07bfSEdward Tomasz Napierala }
4470a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4471a7ad07bfSEdward Tomasz Napierala 
4472413628a7SBjoern A. Zeeb #ifdef DDB
4473b38ff370SJamie Gritton 
4474b38ff370SJamie Gritton static void
4475b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4476413628a7SBjoern A. Zeeb {
4477672756aaSJamie Gritton 	struct bool_flags *bf;
4478672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4479b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4480b38ff370SJamie Gritton 	int ii;
4481413628a7SBjoern A. Zeeb #endif
4482672756aaSJamie Gritton 	unsigned f;
44838144690aSEric van Gyzen #ifdef INET
44848144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
44858144690aSEric van Gyzen #endif
4486413628a7SBjoern A. Zeeb #ifdef INET6
4487413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4488413628a7SBjoern A. Zeeb #endif
4489413628a7SBjoern A. Zeeb 
4490b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4491b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4492b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
44930304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4494b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4495b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
44961158508aSJamie Gritton 	db_printf(" state           = %s\n",
44971158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
44981158508aSJamie Gritton 	    pr->pr_state == PRISON_STATE_DYING ? "dying" :
44991158508aSJamie Gritton 	    "invalid");
4500b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4501b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4502b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4503679e1390SJamie Gritton #ifdef VIMAGE
4504679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4505679e1390SJamie Gritton #endif
4506b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4507b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
45080cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4509fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4510fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
45110304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
45120304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4513fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
4514672756aaSJamie Gritton 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4515672756aaSJamie Gritton 		if (pr->pr_flags & bf->flag)
4516672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4517672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
4518672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4519672756aaSJamie Gritton 	     jsf++) {
4520672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
4521672756aaSJamie Gritton 		db_printf(" %-16s= %s\n", jsf->name,
4522672756aaSJamie Gritton 		    (f != 0 && f == jsf->disable) ? "disable"
4523672756aaSJamie Gritton 		    : (f == jsf->new) ? "new"
45247cbf7213SJamie Gritton 		    : "inherit");
45257cbf7213SJamie Gritton 	}
4526fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
4527672756aaSJamie Gritton 	for (bf = pr_flag_allow;
45285d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
45295d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
4530672756aaSJamie Gritton 	     bf++)
4531672756aaSJamie Gritton 		if (pr->pr_allow & bf->flag)
4532672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4533b38ff370SJamie Gritton 	db_printf("\n");
45340304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4535c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4536c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4537c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
453876ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4539413628a7SBjoern A. Zeeb #ifdef INET
4540b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4541b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4542b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4543fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
45448144690aSEric van Gyzen 		    inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4545413628a7SBjoern A. Zeeb #endif
4546413628a7SBjoern A. Zeeb #ifdef INET6
4547b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4548b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4549b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4550fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4551b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4552b38ff370SJamie Gritton #endif
4553b38ff370SJamie Gritton }
4554b38ff370SJamie Gritton 
4555b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4556b38ff370SJamie Gritton {
4557b38ff370SJamie Gritton 	struct prison *pr;
4558b38ff370SJamie Gritton 
4559b38ff370SJamie Gritton 	if (!have_addr) {
45600304c731SJamie Gritton 		/*
45610304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
45620304c731SJamie Gritton 		 * listed.
45630304c731SJamie Gritton 		 */
45640304c731SJamie Gritton 		db_show_prison(&prison0);
45650304c731SJamie Gritton 		if (!db_pager_quit) {
4566b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4567b38ff370SJamie Gritton 				db_show_prison(pr);
4568413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4569413628a7SBjoern A. Zeeb 					break;
4570413628a7SBjoern A. Zeeb 			}
45710304c731SJamie Gritton 		}
4572b38ff370SJamie Gritton 		return;
4573413628a7SBjoern A. Zeeb 	}
4574b38ff370SJamie Gritton 
45750304c731SJamie Gritton 	if (addr == 0)
45760304c731SJamie Gritton 		pr = &prison0;
45770304c731SJamie Gritton 	else {
4578b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4579b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4580b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4581b38ff370SJamie Gritton 				break;
4582b38ff370SJamie Gritton 		if (pr == NULL)
4583b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4584b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4585b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4586b38ff370SJamie Gritton 					break;
4587b38ff370SJamie Gritton 		if (pr == NULL)
4588b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4589b38ff370SJamie Gritton 			pr = (struct prison *)addr;
45900304c731SJamie Gritton 	}
4591b38ff370SJamie Gritton 	db_show_prison(pr);
4592b38ff370SJamie Gritton }
4593b38ff370SJamie Gritton 
4594413628a7SBjoern A. Zeeb #endif /* DDB */
4595