xref: /freebsd/sys/kern/kern_jail.c (revision effad35ed1e52196469f8f2709b0f1f74cd2ce8c)
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 
798986e3a0SJamie Gritton #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
80c3188289SKyle Evans #define	PRISON0_HOSTUUID_MODULE	"hostuuid"
818986e3a0SJamie Gritton 
8275c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
83d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8475c13541SPoul-Henning Kamp 
85592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
86592bcae8SBjoern A. Zeeb #ifdef INET
87592bcae8SBjoern A. Zeeb #ifdef INET6
88592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
89592bcae8SBjoern A. Zeeb #else
90592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
91592bcae8SBjoern A. Zeeb #endif
92592bcae8SBjoern A. Zeeb #else /* !INET */
93592bcae8SBjoern A. Zeeb #ifdef INET6
94592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
95592bcae8SBjoern A. Zeeb #else
96592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
97592bcae8SBjoern A. Zeeb #endif
98592bcae8SBjoern A. Zeeb #endif
99592bcae8SBjoern A. Zeeb 
1000304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
1010304c731SJamie Gritton struct prison prison0 = {
1020304c731SJamie Gritton 	.pr_id		= 0,
1030304c731SJamie Gritton 	.pr_name	= "0",
1040304c731SJamie Gritton 	.pr_ref		= 1,
1050304c731SJamie Gritton 	.pr_uref	= 1,
1060304c731SJamie Gritton 	.pr_path	= "/",
1070304c731SJamie Gritton 	.pr_securelevel	= -1,
1080cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
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);
140b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
141b3059e09SRobert Watson static void prison_complete(void *context, int pending);
142b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1430fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag,
1440fe74ae6SJamie Gritton     int enable);
1450304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1460304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
147a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
148a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
149c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
150a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
151a7ad07bfSEdward Tomasz Napierala #endif
152fd7a8150SMike Barcroft 
153b38ff370SJamie Gritton /* Flags for prison_deref */
1542a4b2251SJamie Gritton #define	PD_DEREF	0x01	/* Decrement pr_ref */
1552a4b2251SJamie Gritton #define	PD_DEUREF	0x02	/* Decrement pr_uref */
1562a4b2251SJamie Gritton #define	PD_LOCKED	0x04	/* pr_mtx is held */
1572a4b2251SJamie Gritton #define	PD_LIST_SLOCKED	0x08	/* allprison_lock is held shared */
1582a4b2251SJamie Gritton #define	PD_LIST_XLOCKED	0x10	/* allprison_lock is held exclusive */
159fd7a8150SMike Barcroft 
1600304c731SJamie Gritton /*
1615cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1625cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1635cc70397SBjoern A. Zeeb  * terminating entry.
1640304c731SJamie Gritton  */
165672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = {
166672756aaSJamie Gritton 	{"persist", "nopersist", PR_PERSIST},
167592bcae8SBjoern A. Zeeb #ifdef INET
168672756aaSJamie Gritton 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
169592bcae8SBjoern A. Zeeb #endif
170592bcae8SBjoern A. Zeeb #ifdef INET6
171672756aaSJamie Gritton 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
172592bcae8SBjoern A. Zeeb #endif
1730304c731SJamie Gritton };
174672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
1750304c731SJamie Gritton 
176672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = {
1777cbf7213SJamie Gritton 	{"host", 0, PR_HOST},
1787cbf7213SJamie Gritton #ifdef VIMAGE
1797cbf7213SJamie Gritton 	{"vnet", 0, PR_VNET},
1807cbf7213SJamie Gritton #endif
1810304c731SJamie Gritton #ifdef INET
1826a3f2779SJamie Gritton 	{"ip4", PR_IP4_USER, PR_IP4_USER},
1830304c731SJamie Gritton #endif
1840304c731SJamie Gritton #ifdef INET6
1856a3f2779SJamie Gritton 	{"ip6", PR_IP6_USER, PR_IP6_USER},
186679e1390SJamie Gritton #endif
1870304c731SJamie Gritton };
1885cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1890304c731SJamie Gritton 
1905d58f959SJamie Gritton /*
1915d58f959SJamie Gritton  * Make this array full-size so dynamic parameters can be added.
1925d58f959SJamie Gritton  * It is protected by prison0.mtx, but lockless reading is allowed
1935d58f959SJamie Gritton  * with an atomic check of the flag values.
1945d58f959SJamie Gritton  */
1950e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
196672756aaSJamie Gritton 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
197672756aaSJamie Gritton 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
198672756aaSJamie Gritton 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
199672756aaSJamie Gritton 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
200672756aaSJamie Gritton 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
201672756aaSJamie Gritton 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
202672756aaSJamie Gritton 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
203ccd6ac9fSAntoine Brodin 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
204672756aaSJamie Gritton 	{"allow.reserved_ports", "allow.noreserved_ports",
205672756aaSJamie Gritton 	 PR_ALLOW_RESERVED_PORTS},
206b19d66fdSJamie Gritton 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
207b3079544SJamie Gritton 	{"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
208b3079544SJamie Gritton 	 PR_ALLOW_UNPRIV_DEBUG},
20905e1e482SMariusz Zaborski 	{"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
2100304c731SJamie Gritton };
2115d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
212672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
2130304c731SJamie Gritton 
214b3079544SJamie Gritton #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | \
215b3079544SJamie Gritton 					 PR_ALLOW_RESERVED_PORTS | \
21605e1e482SMariusz Zaborski 					 PR_ALLOW_UNPRIV_DEBUG | \
21705e1e482SMariusz Zaborski 					 PR_ALLOW_SUSER)
21842bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
219bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2200304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
22142bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2220cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2230304c731SJamie Gritton #if defined(INET) || defined(INET6)
224e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2250304c731SJamie Gritton #endif
2260304c731SJamie Gritton 
227b96bd95bSIan Lepore /*
228b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
229b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
230b96bd95bSIan Lepore  */
231b96bd95bSIan Lepore void
232b96bd95bSIan Lepore prison0_init(void)
233b96bd95bSIan Lepore {
234c3188289SKyle Evans 	uint8_t *file, *data;
235c3188289SKyle Evans 	size_t size;
236b96bd95bSIan Lepore 
237b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
238b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
239b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
240c3188289SKyle Evans 
241c3188289SKyle Evans 	/* If we have a preloaded hostuuid, use it. */
242c3188289SKyle Evans 	file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
243c3188289SKyle Evans 	if (file != NULL) {
244c3188289SKyle Evans 		data = preload_fetch_addr(file);
245c3188289SKyle Evans 		size = preload_fetch_size(file);
246c3188289SKyle Evans 		if (data != NULL) {
247c3188289SKyle Evans 			/*
248c3188289SKyle Evans 			 * The preloaded data may include trailing whitespace, almost
249c3188289SKyle Evans 			 * certainly a newline; skip over any whitespace or
250c3188289SKyle Evans 			 * non-printable characters to be safe.
251c3188289SKyle Evans 			 */
252c3188289SKyle Evans 			while (size > 0 && data[size - 1] <= 0x20) {
253c3188289SKyle Evans 				data[size--] = '\0';
254c3188289SKyle Evans 			}
255c3188289SKyle Evans 			if (validate_uuid(data, size, NULL, 0) == 0) {
256c3188289SKyle Evans 				(void)strlcpy(prison0.pr_hostuuid, data,
257c3188289SKyle Evans 				    size + 1);
258c3188289SKyle Evans 			} else if (bootverbose) {
259c3188289SKyle Evans 				printf("hostuuid: preload data malformed: '%s'",
260c3188289SKyle Evans 				    data);
261c3188289SKyle Evans 			}
262c3188289SKyle Evans 		}
263c3188289SKyle Evans 	}
264c3188289SKyle Evans 	if (bootverbose)
265c3188289SKyle Evans 		printf("hostuuid: using %s\n", prison0.pr_hostuuid);
266b96bd95bSIan Lepore }
267b96bd95bSIan Lepore 
268116734c4SMatthew Dillon /*
2699ddb7954SMike Barcroft  * struct jail_args {
2709ddb7954SMike Barcroft  *	struct jail *jail;
2719ddb7954SMike Barcroft  * };
272116734c4SMatthew Dillon  */
27375c13541SPoul-Henning Kamp int
274c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap)
27575c13541SPoul-Henning Kamp {
276413628a7SBjoern A. Zeeb 	uint32_t version;
277413628a7SBjoern A. Zeeb 	int error;
2780304c731SJamie Gritton 	struct jail j;
279413628a7SBjoern A. Zeeb 
280413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
281413628a7SBjoern A. Zeeb 	if (error)
282413628a7SBjoern A. Zeeb 		return (error);
283413628a7SBjoern A. Zeeb 
284413628a7SBjoern A. Zeeb 	switch (version) {
285413628a7SBjoern A. Zeeb 	case 0:
286413628a7SBjoern A. Zeeb 	{
287413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
288413628a7SBjoern A. Zeeb 
2890304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2900304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
291413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
292413628a7SBjoern A. Zeeb 		if (error)
293413628a7SBjoern A. Zeeb 			return (error);
2940304c731SJamie Gritton 		j.version = j0.version;
2950304c731SJamie Gritton 		j.path = j0.path;
2960304c731SJamie Gritton 		j.hostname = j0.hostname;
297b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
298413628a7SBjoern A. Zeeb 		break;
299413628a7SBjoern A. Zeeb 	}
300413628a7SBjoern A. Zeeb 
301413628a7SBjoern A. Zeeb 	case 1:
302413628a7SBjoern A. Zeeb 		/*
303413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
304413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
305413628a7SBjoern A. Zeeb 		 */
306413628a7SBjoern A. Zeeb 		return (EINVAL);
307413628a7SBjoern A. Zeeb 
308413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
309413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
310413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
311413628a7SBjoern A. Zeeb 		if (error)
312413628a7SBjoern A. Zeeb 			return (error);
3130304c731SJamie Gritton 		break;
3140304c731SJamie Gritton 
3150304c731SJamie Gritton 	default:
3160304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3170304c731SJamie Gritton 		return (EINVAL);
3180304c731SJamie Gritton 	}
319c542c43eSJamie Gritton 	return (kern_jail(td, &j));
3200304c731SJamie Gritton }
3210304c731SJamie Gritton 
3220304c731SJamie Gritton int
323c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j)
3240304c731SJamie Gritton {
325c542c43eSJamie Gritton 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
326e92e0574SJamie Gritton #ifdef INET
327e92e0574SJamie Gritton 			    + 1
328e92e0574SJamie Gritton #endif
329e92e0574SJamie Gritton #ifdef INET6
330e92e0574SJamie Gritton 			    + 1
331e92e0574SJamie Gritton #endif
332e92e0574SJamie Gritton 			    )];
3330304c731SJamie Gritton 	struct uio opt;
3340304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
335672756aaSJamie Gritton 	struct bool_flags *bf;
3360304c731SJamie Gritton #ifdef INET
337e92e0574SJamie Gritton 	uint32_t ip4s;
3380304c731SJamie Gritton 	struct in_addr *u_ip4;
3390304c731SJamie Gritton #endif
3400304c731SJamie Gritton #ifdef INET6
3410304c731SJamie Gritton 	struct in6_addr *u_ip6;
3420304c731SJamie Gritton #endif
3430304c731SJamie Gritton 	size_t tmplen;
344c542c43eSJamie Gritton 	int error, enforce_statfs;
3450304c731SJamie Gritton 
3460304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3470304c731SJamie Gritton 	opt.uio_iov = optiov;
3480304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3490304c731SJamie Gritton 	opt.uio_offset = -1;
3500304c731SJamie Gritton 	opt.uio_resid = -1;
3510304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3520304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3530304c731SJamie Gritton 	opt.uio_td = td;
3540304c731SJamie Gritton 
3550304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3560304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
357672756aaSJamie Gritton 		for (bf = pr_flag_allow;
3580e5c6bd4SJamie Gritton 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
3595d58f959SJamie Gritton 			atomic_load_int(&bf->flag) != 0;
360672756aaSJamie Gritton 		     bf++) {
361672756aaSJamie Gritton 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
362672756aaSJamie Gritton 			    (jail_default_allow & bf->flag)
363672756aaSJamie Gritton 			    ? bf->name : bf->noname);
3640304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3650304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3660304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3670304c731SJamie Gritton 		}
3680304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3690304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3700304c731SJamie Gritton 		opt.uio_iovcnt++;
3710304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3720304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3730304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3740304c731SJamie Gritton 		opt.uio_iovcnt++;
3750304c731SJamie Gritton 	}
3760304c731SJamie Gritton 
377b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
378b38ff370SJamie Gritton #ifdef INET
3790304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3800304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
381b38ff370SJamie Gritton 		return (EINVAL);
3820304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
383b38ff370SJamie Gritton #else
3840304c731SJamie Gritton 	if (j->ip4s > 0)
385b38ff370SJamie Gritton 		return (EINVAL);
386b38ff370SJamie Gritton #endif
387b38ff370SJamie Gritton #ifdef INET6
3880304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
389b38ff370SJamie Gritton 		return (EINVAL);
3900304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
391b38ff370SJamie Gritton #else
3920304c731SJamie Gritton 	if (j->ip6s > 0)
393b38ff370SJamie Gritton 		return (EINVAL);
394b38ff370SJamie Gritton #endif
395b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
396b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
397b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
398b38ff370SJamie Gritton #ifdef INET
399b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
400b38ff370SJamie Gritton #endif
401b38ff370SJamie Gritton #ifdef INET6
402b38ff370SJamie Gritton #ifdef INET
4030304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
404b38ff370SJamie Gritton #else
405b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
406b38ff370SJamie Gritton #endif
407b38ff370SJamie Gritton #endif
4080304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4090304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4100304c731SJamie Gritton 	opt.uio_iovcnt++;
4110304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4120304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4130304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
414b38ff370SJamie Gritton 	if (error) {
415b38ff370SJamie Gritton 		free(u_path, M_TEMP);
416b38ff370SJamie Gritton 		return (error);
417b38ff370SJamie Gritton 	}
4180304c731SJamie Gritton 	opt.uio_iovcnt++;
4190304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4200304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4210304c731SJamie Gritton 	opt.uio_iovcnt++;
4220304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4230304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4240304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
425b38ff370SJamie Gritton 	if (error) {
426b38ff370SJamie Gritton 		free(u_path, M_TEMP);
427b38ff370SJamie Gritton 		return (error);
428b38ff370SJamie Gritton 	}
4290304c731SJamie Gritton 	opt.uio_iovcnt++;
4300304c731SJamie Gritton 	if (j->jailname != NULL) {
431b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
432b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
433b38ff370SJamie Gritton 		opt.uio_iovcnt++;
434b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4350304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
436b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
437b38ff370SJamie Gritton 		if (error) {
438b38ff370SJamie Gritton 			free(u_path, M_TEMP);
439b38ff370SJamie Gritton 			return (error);
440b38ff370SJamie Gritton 		}
441b38ff370SJamie Gritton 		opt.uio_iovcnt++;
442b38ff370SJamie Gritton 	}
443b38ff370SJamie Gritton #ifdef INET
444b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
445b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
446b38ff370SJamie Gritton 	opt.uio_iovcnt++;
447b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4480304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4490304c731SJamie Gritton 	if (j->version == 0)
4500304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4510304c731SJamie Gritton 	else {
4520304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
453b38ff370SJamie Gritton 		if (error) {
454b38ff370SJamie Gritton 			free(u_path, M_TEMP);
455b38ff370SJamie Gritton 			return (error);
456b38ff370SJamie Gritton 		}
4570304c731SJamie Gritton 	}
458b38ff370SJamie Gritton 	opt.uio_iovcnt++;
459b38ff370SJamie Gritton #endif
460b38ff370SJamie Gritton #ifdef INET6
461b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
462b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
463b38ff370SJamie Gritton 	opt.uio_iovcnt++;
464b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4650304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4660304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
467b38ff370SJamie Gritton 	if (error) {
468b38ff370SJamie Gritton 		free(u_path, M_TEMP);
469b38ff370SJamie Gritton 		return (error);
470b38ff370SJamie Gritton 	}
471b38ff370SJamie Gritton 	opt.uio_iovcnt++;
472b38ff370SJamie Gritton #endif
47302abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
4740304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
475b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
476b38ff370SJamie Gritton 	free(u_path, M_TEMP);
477b38ff370SJamie Gritton 	return (error);
478b38ff370SJamie Gritton }
479b38ff370SJamie Gritton 
480b38ff370SJamie Gritton /*
481b38ff370SJamie Gritton  * struct jail_set_args {
482b38ff370SJamie Gritton  *	struct iovec *iovp;
483b38ff370SJamie Gritton  *	unsigned int iovcnt;
484b38ff370SJamie Gritton  *	int flags;
485b38ff370SJamie Gritton  * };
486b38ff370SJamie Gritton  */
487b38ff370SJamie Gritton int
4888451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
489b38ff370SJamie Gritton {
490b38ff370SJamie Gritton 	struct uio *auio;
491b38ff370SJamie Gritton 	int error;
492b38ff370SJamie Gritton 
493b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
494b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
495b38ff370SJamie Gritton 		return (EINVAL);
496b38ff370SJamie Gritton 
497b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
498b38ff370SJamie Gritton 	if (error)
499b38ff370SJamie Gritton 		return (error);
500b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
501b38ff370SJamie Gritton 	free(auio, M_IOV);
502b38ff370SJamie Gritton 	return (error);
503413628a7SBjoern A. Zeeb }
504413628a7SBjoern A. Zeeb 
505413628a7SBjoern A. Zeeb int
506b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
507413628a7SBjoern A. Zeeb {
508fd7a8150SMike Barcroft 	struct nameidata nd;
509b38ff370SJamie Gritton #ifdef INET
510b38ff370SJamie Gritton 	struct in_addr *ip4;
511413628a7SBjoern A. Zeeb #endif
512b38ff370SJamie Gritton #ifdef INET6
513b38ff370SJamie Gritton 	struct in6_addr *ip6;
514b38ff370SJamie Gritton #endif
515b38ff370SJamie Gritton 	struct vfsopt *opt;
516b38ff370SJamie Gritton 	struct vfsoptlist *opts;
5177de883c8SJamie Gritton 	struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
518b38ff370SJamie Gritton 	struct vnode *root;
519babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
520b96bd95bSIan Lepore 	char *g_path, *osrelstr;
521672756aaSJamie Gritton 	struct bool_flags *bf;
522672756aaSJamie Gritton 	struct jailsys_flags *jsf;
5230304c731SJamie Gritton #if defined(INET) || defined(INET6)
52457aea6dfSBjoern A. Zeeb 	struct prison *tppr;
525b38ff370SJamie Gritton 	void *op;
5260304c731SJamie Gritton #endif
52776ca6f88SJamie Gritton 	unsigned long hid;
528b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
5292a4b2251SJamie Gritton 	int born, created, cuflags, descend, drflags, enforce;
530cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
5310cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
532672756aaSJamie Gritton 	int jid, jsys, len, level;
533b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
534d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
5350304c731SJamie Gritton 	int ii, ij;
536413628a7SBjoern A. Zeeb #endif
537413628a7SBjoern A. Zeeb #ifdef INET
5382b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
539413628a7SBjoern A. Zeeb #endif
540b38ff370SJamie Gritton #ifdef INET6
5412b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
542b38ff370SJamie Gritton #endif
5436beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
544b3079544SJamie Gritton 	uint64_t pr_allow_diff;
5456beb3bb4SKirk McKusick 	unsigned tallow;
546b38ff370SJamie Gritton 	char numbuf[12];
547b38ff370SJamie Gritton 
548b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
549b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
550b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
551b38ff370SJamie Gritton 	if (error)
55275c13541SPoul-Henning Kamp 		return (error);
553b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
554b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5550304c731SJamie Gritton 		return (EPERM);
556b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
557b38ff370SJamie Gritton 		return (EINVAL);
558b38ff370SJamie Gritton 
559b38ff370SJamie Gritton 	/*
560b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
561b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
562b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
563b38ff370SJamie Gritton 	 * the allprison lock.
564b38ff370SJamie Gritton 	 *
565b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
566b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
567b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
568b38ff370SJamie Gritton 	 */
569b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
570b38ff370SJamie Gritton 	if (error)
571b38ff370SJamie Gritton 		return (error);
572b38ff370SJamie Gritton #ifdef INET
573b38ff370SJamie Gritton 	ip4 = NULL;
574b38ff370SJamie Gritton #endif
575b38ff370SJamie Gritton #ifdef INET6
576b38ff370SJamie Gritton 	ip6 = NULL;
577b38ff370SJamie Gritton #endif
5786dfe0a3dSMartin Matuska 	g_path = NULL;
579b38ff370SJamie Gritton 
580b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
581b6f47c23SJamie Gritton 	if (!cuflags) {
582b6f47c23SJamie Gritton 		error = EINVAL;
583b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
584b6f47c23SJamie Gritton 		goto done_errmsg;
585b6f47c23SJamie Gritton 	}
586b6f47c23SJamie Gritton 
587b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
588b38ff370SJamie Gritton 	if (error == ENOENT)
589b38ff370SJamie Gritton 		jid = 0;
590b38ff370SJamie Gritton 	else if (error != 0)
591b38ff370SJamie Gritton 		goto done_free;
592b38ff370SJamie Gritton 
593b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
594b38ff370SJamie Gritton 	if (error == ENOENT)
595b38ff370SJamie Gritton 		gotslevel = 0;
596b38ff370SJamie Gritton 	else if (error != 0)
597b38ff370SJamie Gritton 		goto done_free;
598b38ff370SJamie Gritton 	else
599b38ff370SJamie Gritton 		gotslevel = 1;
600b38ff370SJamie Gritton 
601b97457e2SJamie Gritton 	error =
602b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
603b97457e2SJamie Gritton 	if (error == ENOENT)
604b97457e2SJamie Gritton 		gotchildmax = 0;
605b97457e2SJamie Gritton 	else if (error != 0)
606b97457e2SJamie Gritton 		goto done_free;
607b97457e2SJamie Gritton 	else
608b97457e2SJamie Gritton 		gotchildmax = 1;
609b97457e2SJamie Gritton 
6100304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
611f337198dSJamie Gritton 	if (error == ENOENT)
612f337198dSJamie Gritton 		gotenforce = 0;
613f337198dSJamie Gritton 	else if (error != 0)
6140304c731SJamie Gritton 		goto done_free;
615f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
616f337198dSJamie Gritton 		error = EINVAL;
617f337198dSJamie Gritton 		goto done_free;
618f337198dSJamie Gritton 	} else
619f337198dSJamie Gritton 		gotenforce = 1;
6200304c731SJamie Gritton 
6210cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
6220cc207a6SMartin Matuska 	if (error == ENOENT)
6230cc207a6SMartin Matuska 		gotrsnum = 0;
6240cc207a6SMartin Matuska 	else if (error != 0)
6250cc207a6SMartin Matuska 		goto done_free;
6260cc207a6SMartin Matuska 	else
6270cc207a6SMartin Matuska 		gotrsnum = 1;
6280cc207a6SMartin Matuska 
629b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
630672756aaSJamie Gritton 	for (bf = pr_flag_bool;
631672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
632672756aaSJamie Gritton 	     bf++) {
633672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
634672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
6350304c731SJamie Gritton 	}
636b38ff370SJamie Gritton 	ch_flags |= pr_flags;
637672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
638672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
639672756aaSJamie Gritton 	     jsf++) {
640672756aaSJamie Gritton 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
6417cbf7213SJamie Gritton 		if (error == ENOENT)
6427cbf7213SJamie Gritton 			continue;
6437cbf7213SJamie Gritton 		if (error != 0)
6447cbf7213SJamie Gritton 			goto done_free;
6457cbf7213SJamie Gritton 		switch (jsys) {
6467cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
647672756aaSJamie Gritton 			if (!jsf->disable) {
6487cbf7213SJamie Gritton 				error = EINVAL;
6497cbf7213SJamie Gritton 				goto done_free;
6507cbf7213SJamie Gritton 			}
651672756aaSJamie Gritton 			pr_flags |= jsf->disable;
6527cbf7213SJamie Gritton 			break;
6537cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
654672756aaSJamie Gritton 			pr_flags |= jsf->new;
6557cbf7213SJamie Gritton 			break;
6567cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6577cbf7213SJamie Gritton 			break;
6587cbf7213SJamie Gritton 		default:
6597cbf7213SJamie Gritton 			error = EINVAL;
6607cbf7213SJamie Gritton 			goto done_free;
6617cbf7213SJamie Gritton 		}
662672756aaSJamie Gritton 		ch_flags |= jsf->new | jsf->disable;
6637cbf7213SJamie Gritton 	}
6644affa14cSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
6654affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6664affa14cSJamie Gritton 		error = EINVAL;
6674affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6684affa14cSJamie Gritton 		goto done_errmsg;
6694affa14cSJamie Gritton 	}
670679e1390SJamie Gritton #ifdef VIMAGE
671679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
672679e1390SJamie Gritton 		error = EINVAL;
673679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
674679e1390SJamie Gritton 		goto done_errmsg;
675679e1390SJamie Gritton 	}
676679e1390SJamie Gritton #endif
6772b0d6f81SJamie Gritton #ifdef INET
6782b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
6792b0d6f81SJamie Gritton 		error = EINVAL;
6802b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
6812b0d6f81SJamie Gritton 		goto done_errmsg;
6822b0d6f81SJamie Gritton 	}
6832b0d6f81SJamie Gritton #endif
6842b0d6f81SJamie Gritton #ifdef INET6
6852b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
6862b0d6f81SJamie Gritton 		error = EINVAL;
6872b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
6882b0d6f81SJamie Gritton 		goto done_errmsg;
6892b0d6f81SJamie Gritton 	}
6902b0d6f81SJamie Gritton #endif
691b38ff370SJamie Gritton 
6920304c731SJamie Gritton 	pr_allow = ch_allow = 0;
693672756aaSJamie Gritton 	for (bf = pr_flag_allow;
6945d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
6955d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
696672756aaSJamie Gritton 	     bf++) {
697672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
698672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
6990304c731SJamie Gritton 	}
7000304c731SJamie Gritton 	ch_allow |= pr_allow;
7010304c731SJamie Gritton 
702b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
703b38ff370SJamie Gritton 	if (error == ENOENT)
704b38ff370SJamie Gritton 		name = NULL;
705b38ff370SJamie Gritton 	else if (error != 0)
706b38ff370SJamie Gritton 		goto done_free;
707b38ff370SJamie Gritton 	else {
708b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
709b38ff370SJamie Gritton 			error = EINVAL;
710b38ff370SJamie Gritton 			goto done_free;
711b38ff370SJamie Gritton 		}
712b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
713b38ff370SJamie Gritton 			error = ENAMETOOLONG;
714b38ff370SJamie Gritton 			goto done_free;
715b38ff370SJamie Gritton 		}
716b38ff370SJamie Gritton 	}
717b38ff370SJamie Gritton 
718b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
719b38ff370SJamie Gritton 	if (error == ENOENT)
720b38ff370SJamie Gritton 		host = NULL;
721b38ff370SJamie Gritton 	else if (error != 0)
722b38ff370SJamie Gritton 		goto done_free;
723b38ff370SJamie Gritton 	else {
72476ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
72576ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
726b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
727b38ff370SJamie Gritton 			error = EINVAL;
728b38ff370SJamie Gritton 			goto done_free;
729b38ff370SJamie Gritton 		}
730b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
731b38ff370SJamie Gritton 			error = ENAMETOOLONG;
732b38ff370SJamie Gritton 			goto done_free;
733b38ff370SJamie Gritton 		}
734b38ff370SJamie Gritton 	}
735b38ff370SJamie Gritton 
73676ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
73776ca6f88SJamie Gritton 	if (error == ENOENT)
73876ca6f88SJamie Gritton 		domain = NULL;
73976ca6f88SJamie Gritton 	else if (error != 0)
74076ca6f88SJamie Gritton 		goto done_free;
74176ca6f88SJamie Gritton 	else {
74276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
74376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
74476ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
74576ca6f88SJamie Gritton 			error = EINVAL;
74676ca6f88SJamie Gritton 			goto done_free;
74776ca6f88SJamie Gritton 		}
74876ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
74976ca6f88SJamie Gritton 			error = ENAMETOOLONG;
75076ca6f88SJamie Gritton 			goto done_free;
75176ca6f88SJamie Gritton 		}
75276ca6f88SJamie Gritton 	}
75376ca6f88SJamie Gritton 
75476ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
75576ca6f88SJamie Gritton 	if (error == ENOENT)
75676ca6f88SJamie Gritton 		uuid = NULL;
75776ca6f88SJamie Gritton 	else if (error != 0)
75876ca6f88SJamie Gritton 		goto done_free;
75976ca6f88SJamie Gritton 	else {
76076ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
76176ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
76276ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
76376ca6f88SJamie Gritton 			error = EINVAL;
76476ca6f88SJamie Gritton 			goto done_free;
76576ca6f88SJamie Gritton 		}
76676ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
76776ca6f88SJamie Gritton 			error = ENAMETOOLONG;
76876ca6f88SJamie Gritton 			goto done_free;
76976ca6f88SJamie Gritton 		}
77076ca6f88SJamie Gritton 	}
77176ca6f88SJamie Gritton 
772841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
773a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
77476ca6f88SJamie Gritton 		uint32_t hid32;
77576ca6f88SJamie Gritton 
77676ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
77776ca6f88SJamie Gritton 		hid = hid32;
77876ca6f88SJamie Gritton 	} else
77976ca6f88SJamie Gritton #endif
78076ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
78176ca6f88SJamie Gritton 	if (error == ENOENT)
78276ca6f88SJamie Gritton 		gothid = 0;
78376ca6f88SJamie Gritton 	else if (error != 0)
78476ca6f88SJamie Gritton 		goto done_free;
78576ca6f88SJamie Gritton 	else {
78676ca6f88SJamie Gritton 		gothid = 1;
78776ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
78876ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
78976ca6f88SJamie Gritton 	}
79076ca6f88SJamie Gritton 
791b38ff370SJamie Gritton #ifdef INET
792b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
793b38ff370SJamie Gritton 	if (error == ENOENT)
7940e5e396eSJamie Gritton 		ip4s = 0;
795b38ff370SJamie Gritton 	else if (error != 0)
796b38ff370SJamie Gritton 		goto done_free;
797b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
798b38ff370SJamie Gritton 		error = EINVAL;
799b38ff370SJamie Gritton 		goto done_free;
8000304c731SJamie Gritton 	} else {
8016a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
8026a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
8036a3f2779SJamie Gritton 		if (ip4s > 0) {
804b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
805b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
806b38ff370SJamie Gritton 				error = EINVAL;
807b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
808b38ff370SJamie Gritton 				goto done_errmsg;
809b38ff370SJamie Gritton 			}
8102b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
811b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
812b38ff370SJamie Gritton 			/*
8130304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
8140304c731SJamie Gritton 			 * the primary IP address as given from userland.
8150304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
816bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
817bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
818bef916f9SBjoern A. Zeeb 			 * address to connect from.
819b38ff370SJamie Gritton 			 */
820b38ff370SJamie Gritton 			if (ip4s > 1)
8210ce1624dSStephen J. Kiernan 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
8220ce1624dSStephen J. Kiernan 				    prison_qcmp_v4);
823b38ff370SJamie Gritton 			/*
8240304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
8250304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
8260304c731SJamie Gritton 			 * addresses it is their problem.
827b38ff370SJamie Gritton 			 *
8280304c731SJamie Gritton 			 * We do not have to care about byte order for these
8290304c731SJamie Gritton 			 * checks so we will do them in NBO.
830b38ff370SJamie Gritton 			 */
831b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
832b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
833b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
834b38ff370SJamie Gritton 					error = EINVAL;
835b38ff370SJamie Gritton 					goto done_free;
836b38ff370SJamie Gritton 				}
837b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
838b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
839b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
840b38ff370SJamie Gritton 					error = EINVAL;
841b38ff370SJamie Gritton 					goto done_free;
842b38ff370SJamie Gritton 				}
843b38ff370SJamie Gritton 			}
844b38ff370SJamie Gritton 		}
8450304c731SJamie Gritton 	}
846b38ff370SJamie Gritton #endif
847b38ff370SJamie Gritton 
848b38ff370SJamie Gritton #ifdef INET6
849b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
850b38ff370SJamie Gritton 	if (error == ENOENT)
8510e5e396eSJamie Gritton 		ip6s = 0;
852b38ff370SJamie Gritton 	else if (error != 0)
853b38ff370SJamie Gritton 		goto done_free;
854b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
855b38ff370SJamie Gritton 		error = EINVAL;
856b38ff370SJamie Gritton 		goto done_free;
8570304c731SJamie Gritton 	} else {
8586a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
8596a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
8606a3f2779SJamie Gritton 		if (ip6s > 0) {
861b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
862b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
863b38ff370SJamie Gritton 				error = EINVAL;
864b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
865b38ff370SJamie Gritton 				goto done_errmsg;
866b38ff370SJamie Gritton 			}
8672b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
868b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
869b38ff370SJamie Gritton 			if (ip6s > 1)
8700ce1624dSStephen J. Kiernan 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
8710ce1624dSStephen J. Kiernan 				    prison_qcmp_v6);
872b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8730304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
874b38ff370SJamie Gritton 					error = EINVAL;
875b38ff370SJamie Gritton 					goto done_free;
876b38ff370SJamie Gritton 				}
877b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
878b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
879b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
880b38ff370SJamie Gritton 				{
881b38ff370SJamie Gritton 					error = EINVAL;
882b38ff370SJamie Gritton 					goto done_free;
883b38ff370SJamie Gritton 				}
884b38ff370SJamie Gritton 			}
885b38ff370SJamie Gritton 		}
8860304c731SJamie Gritton 	}
887b38ff370SJamie Gritton #endif
888b38ff370SJamie Gritton 
889bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
890bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
891bdfc8cc4SJamie Gritton 		error = EINVAL;
892bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
893bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
894bdfc8cc4SJamie Gritton 		goto done_errmsg;
895bdfc8cc4SJamie Gritton 	}
896bdfc8cc4SJamie Gritton #endif
897bdfc8cc4SJamie Gritton 
898cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
899cf0313c6SJamie Gritton 	if (error == ENOENT)
900cf0313c6SJamie Gritton 		osrelstr = NULL;
901cf0313c6SJamie Gritton 	else if (error != 0)
902cf0313c6SJamie Gritton 		goto done_free;
903cf0313c6SJamie Gritton 	else {
904cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
905cf0313c6SJamie Gritton 			error = EINVAL;
906cf0313c6SJamie Gritton 			vfs_opterror(opts,
907cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
908cf0313c6SJamie Gritton 			goto done_errmsg;
909cf0313c6SJamie Gritton 		}
9101b786d01SBjoern A. Zeeb 		if (len == 0 || osrelstr[len - 1] != '\0') {
911cf0313c6SJamie Gritton 			error = EINVAL;
9121b786d01SBjoern A. Zeeb 			goto done_free;
9131b786d01SBjoern A. Zeeb 		}
9141b786d01SBjoern A. Zeeb 		if (len >= OSRELEASELEN) {
9151b786d01SBjoern A. Zeeb 			error = ENAMETOOLONG;
916cf0313c6SJamie Gritton 			vfs_opterror(opts,
917cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
918cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
919cf0313c6SJamie Gritton 			goto done_errmsg;
920cf0313c6SJamie Gritton 		}
921cf0313c6SJamie Gritton 	}
922cf0313c6SJamie Gritton 
923cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
924cf0313c6SJamie Gritton 	if (error == ENOENT)
925cf0313c6SJamie Gritton 		osreldt = 0;
926cf0313c6SJamie Gritton 	else if (error != 0)
927cf0313c6SJamie Gritton 		goto done_free;
928cf0313c6SJamie Gritton 	else {
929cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
930cf0313c6SJamie Gritton 			error = EINVAL;
931cf0313c6SJamie Gritton 			vfs_opterror(opts,
932cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
933cf0313c6SJamie Gritton 			goto done_errmsg;
934cf0313c6SJamie Gritton 		}
935cf0313c6SJamie Gritton 		if (osreldt == 0) {
936cf0313c6SJamie Gritton 			error = EINVAL;
937cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
938cf0313c6SJamie Gritton 			goto done_errmsg;
939cf0313c6SJamie Gritton 		}
940cf0313c6SJamie Gritton 	}
941cf0313c6SJamie Gritton 
942b38ff370SJamie Gritton 	root = NULL;
943b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
944b38ff370SJamie Gritton 	if (error == ENOENT)
945b38ff370SJamie Gritton 		path = NULL;
946b38ff370SJamie Gritton 	else if (error != 0)
947b38ff370SJamie Gritton 		goto done_free;
948b38ff370SJamie Gritton 	else {
949b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
950b38ff370SJamie Gritton 			error = EINVAL;
951b38ff370SJamie Gritton 			vfs_opterror(opts,
952b38ff370SJamie Gritton 			    "path cannot be changed after creation");
953b38ff370SJamie Gritton 			goto done_errmsg;
954b38ff370SJamie Gritton 		}
955b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
956b38ff370SJamie Gritton 			error = EINVAL;
957b38ff370SJamie Gritton 			goto done_free;
958b38ff370SJamie Gritton 		}
9595050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
960b38ff370SJamie Gritton 		    path, td);
961b38ff370SJamie Gritton 		error = namei(&nd);
962b38ff370SJamie Gritton 		if (error)
963b38ff370SJamie Gritton 			goto done_free;
964b38ff370SJamie Gritton 		root = nd.ni_vp;
965b38ff370SJamie Gritton 		NDFREE(&nd, NDF_ONLY_PNBUF);
9666dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
9676dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
9686dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
9693eb6b656SMateusz Guzik 		if (error == 0) {
9706dfe0a3dSMartin Matuska 			path = g_path;
9716dfe0a3dSMartin Matuska 		} else {
972f6e633a9SMartin Matuska 			/* exit on other errors */
973b38ff370SJamie Gritton 			goto done_free;
974b38ff370SJamie Gritton 		}
975f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
976f6e633a9SMartin Matuska 			error = ENOTDIR;
977f6e633a9SMartin Matuska 			vput(root);
978f6e633a9SMartin Matuska 			goto done_free;
979f6e633a9SMartin Matuska 		}
980b249ce48SMateusz Guzik 		VOP_UNLOCK(root);
981b38ff370SJamie Gritton 	}
982b38ff370SJamie Gritton 
983b38ff370SJamie Gritton 	/*
984b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
985b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
986b38ff370SJamie Gritton 	 */
987b38ff370SJamie Gritton 	pr = NULL;
988b6f47c23SJamie Gritton 	ppr = mypr;
9897de883c8SJamie Gritton 	inspr = NULL;
990babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
991babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
992babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
993babbbb9cSJamie Gritton 		if (*p != '\0')
994babbbb9cSJamie Gritton 			jid = 0;
995babbbb9cSJamie Gritton 	}
996b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
9972a4b2251SJamie Gritton 	drflags = PD_LIST_XLOCKED;
998b38ff370SJamie Gritton 	if (jid != 0) {
999b38ff370SJamie Gritton 		if (jid < 0) {
1000b38ff370SJamie Gritton 			error = EINVAL;
1001b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
10022a4b2251SJamie Gritton 			goto done_deref;
1003b38ff370SJamie Gritton 		}
10047de883c8SJamie Gritton 		/*
10057de883c8SJamie Gritton 		 * See if a requested jid already exists.  Keep track of
10067de883c8SJamie Gritton 		 * where it can be inserted later.
10077de883c8SJamie Gritton 		 */
10087de883c8SJamie Gritton 		TAILQ_FOREACH(inspr, &allprison, pr_list) {
10097de883c8SJamie Gritton 			if (inspr->pr_id == jid) {
10107de883c8SJamie Gritton 				mtx_lock(&inspr->pr_mtx);
101176ad42abSJamie Gritton 				if (prison_isvalid(inspr)) {
10127de883c8SJamie Gritton 					pr = inspr;
10132a4b2251SJamie Gritton 					drflags |= PD_LOCKED;
10147de883c8SJamie Gritton 					inspr = NULL;
10157de883c8SJamie Gritton 				} else
10167de883c8SJamie Gritton 					mtx_unlock(&inspr->pr_mtx);
10177de883c8SJamie Gritton 				break;
10187de883c8SJamie Gritton 			}
10197de883c8SJamie Gritton 			if (inspr->pr_id > jid)
10207de883c8SJamie Gritton 				break;
10217de883c8SJamie Gritton 		}
1022b38ff370SJamie Gritton 		if (pr != NULL) {
10230304c731SJamie Gritton 			ppr = pr->pr_parent;
1024b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1025b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
10267de883c8SJamie Gritton 				/*
10277de883c8SJamie Gritton 				 * Even creators that cannot see the jail will
10287de883c8SJamie Gritton 				 * get EEXIST.
10297de883c8SJamie Gritton 				 */
1030b38ff370SJamie Gritton 				error = EEXIST;
1031b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1032b38ff370SJamie Gritton 				    jid);
10332a4b2251SJamie Gritton 				goto done_deref;
1034b38ff370SJamie Gritton 			}
10350304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
10367de883c8SJamie Gritton 				/*
10377de883c8SJamie Gritton 				 * Updaters get ENOENT if they cannot see the
10387de883c8SJamie Gritton 				 * jail.  This is true even for CREATE | UPDATE,
10397de883c8SJamie Gritton 				 * which normally cannot give this error.
10407de883c8SJamie Gritton 				 */
10412a4b2251SJamie Gritton 				error = ENOENT;
10422a4b2251SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
10432a4b2251SJamie Gritton 				goto done_deref;
104476ad42abSJamie Gritton 			} else if (!prison_isalive(pr)) {
1045b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1046b38ff370SJamie Gritton 					error = ENOENT;
1047b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1048b38ff370SJamie Gritton 					    jid);
10492a4b2251SJamie Gritton 					goto done_deref;
1050b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
1051b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1052b38ff370SJamie Gritton 					/*
1053b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1054b38ff370SJamie Gritton 					 * (via attach or persist), but first
1055b38ff370SJamie Gritton 					 * it must determine if another jail
1056b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1057b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1058b38ff370SJamie Gritton 					 * name.
1059b38ff370SJamie Gritton 					 */
1060b38ff370SJamie Gritton 					if (name == NULL)
10610304c731SJamie Gritton 						name = prison_name(mypr, pr);
1062b38ff370SJamie Gritton 				}
1063b38ff370SJamie Gritton 			}
10642a4b2251SJamie Gritton 		} else {
1065b38ff370SJamie Gritton 			/* Update: jid must exist. */
1066b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1067b38ff370SJamie Gritton 				error = ENOENT;
1068b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
10692a4b2251SJamie Gritton 				goto done_deref;
1070b38ff370SJamie Gritton 			}
1071b38ff370SJamie Gritton 		}
1072b38ff370SJamie Gritton 	}
1073b38ff370SJamie Gritton 	/*
1074b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1075b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1076b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1077b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1078b38ff370SJamie Gritton 	 * because that is the jail being updated).
1079b38ff370SJamie Gritton 	 */
1080b6f47c23SJamie Gritton 	namelc = NULL;
1081b38ff370SJamie Gritton 	if (name != NULL) {
1082babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1083babbbb9cSJamie Gritton 		if (namelc == NULL)
1084babbbb9cSJamie Gritton 			namelc = name;
1085babbbb9cSJamie Gritton 		else {
10860304c731SJamie Gritton 			/*
10870304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
10880304c731SJamie Gritton 			 * parent and child names, and make sure the parent
10890304c731SJamie Gritton 			 * exists or matches an already found jail.
10900304c731SJamie Gritton 			 */
10910304c731SJamie Gritton 			if (pr != NULL) {
1092babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1093babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
10940304c731SJamie Gritton 					error = EINVAL;
10950304c731SJamie Gritton 					vfs_opterror(opts,
10960304c731SJamie Gritton 					    "cannot change jail's parent");
10972a4b2251SJamie Gritton 					goto done_deref;
10980304c731SJamie Gritton 				}
10990304c731SJamie Gritton 			} else {
1100b6f47c23SJamie Gritton 				*namelc = '\0';
11010304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
11020304c731SJamie Gritton 				if (ppr == NULL) {
11030304c731SJamie Gritton 					error = ENOENT;
11040304c731SJamie Gritton 					vfs_opterror(opts,
11050304c731SJamie Gritton 					    "jail \"%s\" not found", name);
11062a4b2251SJamie Gritton 					goto done_deref;
11070304c731SJamie Gritton 				}
11080304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
1109b6f47c23SJamie Gritton 				*namelc = '.';
11100304c731SJamie Gritton 			}
1111b6f47c23SJamie Gritton 			namelc++;
11120304c731SJamie Gritton 		}
1113b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1114b6f47c23SJamie Gritton 			pnamelen =
11150304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1116b38ff370SJamie Gritton  name_again:
11170304c731SJamie Gritton 			deadpr = NULL;
11180304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
1119b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
1120b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
112176ad42abSJamie Gritton 					mtx_lock(&tpr->pr_mtx);
112276ad42abSJamie Gritton 					if (prison_isalive(tpr)) {
1123b38ff370SJamie Gritton 						if (pr == NULL &&
1124b38ff370SJamie Gritton 						    cuflags != JAIL_CREATE) {
1125b38ff370SJamie Gritton 							/*
1126b38ff370SJamie Gritton 							 * Use this jail
1127b38ff370SJamie Gritton 							 * for updates.
1128b38ff370SJamie Gritton 							 */
1129b38ff370SJamie Gritton 							pr = tpr;
113083bc72a0SJamie Gritton 							drflags |= PD_LOCKED;
1131b38ff370SJamie Gritton 							break;
1132b38ff370SJamie Gritton 						}
1133b38ff370SJamie Gritton 						/*
1134b38ff370SJamie Gritton 						 * Create, or update(jid):
1135b38ff370SJamie Gritton 						 * name must not exist in an
11360304c731SJamie Gritton 						 * active sibling jail.
1137b38ff370SJamie Gritton 						 */
1138b38ff370SJamie Gritton 						error = EEXIST;
113983bc72a0SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1140b38ff370SJamie Gritton 						vfs_opterror(opts,
1141b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1142b38ff370SJamie Gritton 						   name);
11432a4b2251SJamie Gritton 						goto done_deref;
1144b38ff370SJamie Gritton 					}
114576ad42abSJamie Gritton 					if (pr == NULL &&
114676ad42abSJamie Gritton 					    cuflags != JAIL_CREATE &&
114776ad42abSJamie Gritton 					    prison_isvalid(tpr))
114876ad42abSJamie Gritton 						deadpr = tpr;
114976ad42abSJamie Gritton 					mtx_unlock(&tpr->pr_mtx);
1150b38ff370SJamie Gritton 				}
1151b38ff370SJamie Gritton 			}
1152b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1153b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1154b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1155b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
115676ad42abSJamie Gritton 					if (!prison_isvalid(deadpr)) {
1157b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1158b38ff370SJamie Gritton 						goto name_again;
1159b38ff370SJamie Gritton 					}
1160b38ff370SJamie Gritton 					pr = deadpr;
11612a4b2251SJamie Gritton 					drflags |= PD_LOCKED;
1162b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1163b38ff370SJamie Gritton 					error = ENOENT;
1164b38ff370SJamie Gritton 					vfs_opterror(opts,
1165b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
11662a4b2251SJamie Gritton 					goto done_deref;
1167b38ff370SJamie Gritton 				}
1168b38ff370SJamie Gritton 			}
1169b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1170b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1171b38ff370SJamie Gritton 				error = ENOENT;
1172b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1173b38ff370SJamie Gritton 				    name);
11742a4b2251SJamie Gritton 				goto done_deref;
1175b38ff370SJamie Gritton 			}
1176b38ff370SJamie Gritton 		}
1177b38ff370SJamie Gritton 	}
1178b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1179b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1180b38ff370SJamie Gritton 		error = ENOENT;
1181b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
11822a4b2251SJamie Gritton 		goto done_deref;
1183b38ff370SJamie Gritton 	}
1184b38ff370SJamie Gritton 
1185b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
11862a4b2251SJamie Gritton 	created = pr == NULL;
11872a4b2251SJamie Gritton 	if (created) {
1188b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1189b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1190b97457e2SJamie Gritton 				error = EPERM;
1191b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
11922a4b2251SJamie Gritton 				goto done_deref;
1193b97457e2SJamie Gritton 			}
11940304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
119576ad42abSJamie Gritton 		if (!prison_isvalid(ppr)) {
11960304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
11970304c731SJamie Gritton 			error = ENOENT;
1198b6f47c23SJamie Gritton 			vfs_opterror(opts, "jail \"%s\" not found",
1199b6f47c23SJamie Gritton 			    prison_name(mypr, ppr));
12002a4b2251SJamie Gritton 			goto done_deref;
12010304c731SJamie Gritton 		}
12020304c731SJamie Gritton 		ppr->pr_ref++;
12030304c731SJamie Gritton 		ppr->pr_uref++;
12040304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
12057de883c8SJamie Gritton 
12067de883c8SJamie Gritton 		if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1207b38ff370SJamie Gritton 			error = EAGAIN;
12087de883c8SJamie Gritton 			vfs_opterror(opts, "no available jail IDs");
12092a4b2251SJamie Gritton 			pr = ppr;
12102a4b2251SJamie Gritton 			drflags |= PD_DEREF | PD_DEUREF;
12112a4b2251SJamie Gritton 			goto done_deref;
1212b38ff370SJamie Gritton 		}
12132a4b2251SJamie Gritton 
12142a4b2251SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
12152a4b2251SJamie Gritton 		LIST_INIT(&pr->pr_children);
12162a4b2251SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
12172a4b2251SJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
12182a4b2251SJamie Gritton 
12197de883c8SJamie Gritton 		pr->pr_id = jid;
12207de883c8SJamie Gritton 		if (inspr != NULL)
12217de883c8SJamie Gritton 			TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
12227de883c8SJamie Gritton 		else
1223b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
12247de883c8SJamie Gritton 
12257de883c8SJamie Gritton 		pr->pr_parent = ppr;
12260304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
12270304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1228b97457e2SJamie Gritton 			tpr->pr_childcount++;
1229b38ff370SJamie Gritton 
12300304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1231b6f47c23SJamie Gritton 		if (namelc == NULL)
1232b6f47c23SJamie Gritton 			namelc = "";
1233b38ff370SJamie Gritton 		if (path == NULL) {
1234b38ff370SJamie Gritton 			path = "/";
12350304c731SJamie Gritton 			root = mypr->pr_root;
1236b38ff370SJamie Gritton 			vref(root);
1237b38ff370SJamie Gritton 		}
12388986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
12398986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1240bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1241bdfc8cc4SJamie Gritton #ifdef VIMAGE
1242bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1243bdfc8cc4SJamie Gritton #endif
1244bdfc8cc4SJamie Gritton 		{
12450304c731SJamie Gritton #ifdef INET
12462b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12476a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
12482b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12492b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12502b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12512b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12522b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12532b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12542b0d6f81SJamie Gritton 					    M_WAITOK);
12552b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12562b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12572b0d6f81SJamie Gritton 				}
12582b0d6f81SJamie Gritton 			}
12590304c731SJamie Gritton #endif
12600304c731SJamie Gritton #ifdef INET6
12612b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
12626a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
12632b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
12642b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
12652b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
12662b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
12672b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
12682b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
12692b0d6f81SJamie Gritton 					    M_WAITOK);
12702b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
12712b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
12722b0d6f81SJamie Gritton 				}
12732b0d6f81SJamie Gritton 			}
12740304c731SJamie Gritton #endif
1275bdfc8cc4SJamie Gritton 		}
1276bdfc8cc4SJamie Gritton #endif
1277592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1278592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1279592bcae8SBjoern A. Zeeb 
12800304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
12810304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1282af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1283bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1284b38ff370SJamie Gritton 
1285b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1286b96bd95bSIan Lepore 		if (osrelstr == NULL)
12871b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
12881b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1289b96bd95bSIan Lepore 		else
12901b786d01SBjoern A. Zeeb 			strlcpy(pr->pr_osrelease, osrelstr,
12911b786d01SBjoern A. Zeeb 			    sizeof(pr->pr_osrelease));
1292b96bd95bSIan Lepore 
1293679e1390SJamie Gritton #ifdef VIMAGE
1294679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1295679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1296679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1297679e1390SJamie Gritton #endif
1298b38ff370SJamie Gritton 		/*
1299b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1300b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1301b38ff370SJamie Gritton 		 */
13020304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
13032a4b2251SJamie Gritton 		if (error)
13042a4b2251SJamie Gritton 			goto done_deref;
1305b38ff370SJamie Gritton 
1306b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
13072a4b2251SJamie Gritton 		drflags |= PD_LOCKED;
1308b38ff370SJamie Gritton 		/*
1309b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1310b6f47c23SJamie Gritton 		 * want others to see the incomplete prison once the
1311b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1312b38ff370SJamie Gritton 		 */
1313b38ff370SJamie Gritton 	} else {
13142b0d6f81SJamie Gritton 		/*
13152b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
13162b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
13172b0d6f81SJamie Gritton 		 */
13182b0d6f81SJamie Gritton 		pr->pr_ref++;
13192a4b2251SJamie Gritton 		drflags |= PD_DEREF;
1320bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1321bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1322bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1323bdfc8cc4SJamie Gritton 			error = EINVAL;
1324bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1325bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
13262a4b2251SJamie Gritton 			goto done_deref;
1327bdfc8cc4SJamie Gritton 		}
1328bdfc8cc4SJamie Gritton #endif
13292b0d6f81SJamie Gritton #ifdef INET
13302b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13312b0d6f81SJamie Gritton 			error = EINVAL;
13322b0d6f81SJamie Gritton 			vfs_opterror(opts,
13332b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
13342a4b2251SJamie Gritton 			goto done_deref;
13352b0d6f81SJamie Gritton 		}
13362b0d6f81SJamie Gritton #endif
13372b0d6f81SJamie Gritton #ifdef INET6
13382b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13392b0d6f81SJamie Gritton 			error = EINVAL;
13402b0d6f81SJamie Gritton 			vfs_opterror(opts,
13412b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
13422a4b2251SJamie Gritton 			goto done_deref;
13432b0d6f81SJamie Gritton 		}
13442b0d6f81SJamie Gritton #endif
1345b38ff370SJamie Gritton 	}
1346b38ff370SJamie Gritton 
1347b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13480304c731SJamie Gritton 	if (gotslevel) {
13490304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13500304c731SJamie Gritton 			error = EPERM;
13512a4b2251SJamie Gritton 			goto done_deref;
13520304c731SJamie Gritton 		}
13530304c731SJamie Gritton 	}
1354b97457e2SJamie Gritton 	if (gotchildmax) {
1355b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1356b97457e2SJamie Gritton 			error = EPERM;
13572a4b2251SJamie Gritton 			goto done_deref;
1358b97457e2SJamie Gritton 		}
1359b97457e2SJamie Gritton 	}
13600304c731SJamie Gritton 	if (gotenforce) {
13610304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
13620304c731SJamie Gritton 			error = EPERM;
13632a4b2251SJamie Gritton 			goto done_deref;
13640304c731SJamie Gritton 		}
13650304c731SJamie Gritton 	}
13660cc207a6SMartin Matuska 	if (gotrsnum) {
13670cc207a6SMartin Matuska 		/*
13680cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
13690cc207a6SMartin Matuska 		 */
1370bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
13710cc207a6SMartin Matuska 			error = EINVAL;
13722a4b2251SJamie Gritton 			goto done_deref;
13730cc207a6SMartin Matuska 		}
13740cc207a6SMartin Matuska 		/*
1375bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
13760cc207a6SMartin Matuska 		 */
13770cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
13780cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
13790cc207a6SMartin Matuska 				error = EPERM;
13802a4b2251SJamie Gritton 				goto done_deref;
1381bf3db8aaSMartin Matuska 			} else
13820cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
13830cc207a6SMartin Matuska 		}
13840cc207a6SMartin Matuska 	}
1385b38ff370SJamie Gritton #ifdef INET
13862b0d6f81SJamie Gritton 	if (ip4s > 0) {
13870304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
13880304c731SJamie Gritton 			/*
13890304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
13900304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
13910304c731SJamie Gritton 			 * about the parent being unlocked, as any
13920304c731SJamie Gritton 			 * setting is done with allprison_lock held.
13930304c731SJamie Gritton 			 */
13940304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
13952b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
13960304c731SJamie Gritton 					break;
13970304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
13980304c731SJamie Gritton 				error = EPERM;
13992a4b2251SJamie Gritton 				goto done_deref;
14000304c731SJamie Gritton 			}
14010304c731SJamie Gritton 			if (ip4s > 1) {
14020304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
14030304c731SJamie Gritton 					if (ip4[ii].s_addr ==
14040304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1405b38ff370SJamie Gritton 						continue;
14060304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
14070304c731SJamie Gritton 						if (ip4[ii].s_addr ==
14080304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
14090304c731SJamie Gritton 							break;
14100304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
14110304c731SJamie Gritton 						break;
14120304c731SJamie Gritton 				}
14130304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
14140304c731SJamie Gritton 					error = EPERM;
14152a4b2251SJamie Gritton 					goto done_deref;
14160304c731SJamie Gritton 				}
14170304c731SJamie Gritton 			}
14180304c731SJamie Gritton 		}
14190304c731SJamie Gritton 		/*
14200304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
14210304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
14220304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
14230304c731SJamie Gritton 		 * IP stop checking and return error.
14240304c731SJamie Gritton 		 */
1425bdfc8cc4SJamie Gritton #ifdef VIMAGE
142608b43333SJamie Gritton 		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1427bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1428bdfc8cc4SJamie Gritton 				break;
142908b43333SJamie Gritton #else
143008b43333SJamie Gritton 		tppr = &prison0;
1431bdfc8cc4SJamie Gritton #endif
1432bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1433bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1434bdfc8cc4SJamie Gritton #ifdef VIMAGE
14352b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1436bdfc8cc4SJamie Gritton #endif
1437bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14380304c731SJamie Gritton 				descend = 0;
14390304c731SJamie Gritton 				continue;
14400304c731SJamie Gritton 			}
14410304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
14420304c731SJamie Gritton 				continue;
14430304c731SJamie Gritton 			descend = 0;
14440304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
14450304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
14460304c731SJamie Gritton 				continue;
14470304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
14480ce1624dSStephen J. Kiernan 				if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
14490ce1624dSStephen J. Kiernan 				    0) {
14500304c731SJamie Gritton 					error = EADDRINUSE;
1451b38ff370SJamie Gritton 					vfs_opterror(opts,
1452b38ff370SJamie Gritton 					    "IPv4 addresses clash");
14532a4b2251SJamie Gritton 					goto done_deref;
1454b38ff370SJamie Gritton 				}
14550304c731SJamie Gritton 			}
14560304c731SJamie Gritton 		}
14570304c731SJamie Gritton 	}
1458b38ff370SJamie Gritton #endif
1459b38ff370SJamie Gritton #ifdef INET6
14602b0d6f81SJamie Gritton 	if (ip6s > 0) {
14610304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
14620304c731SJamie Gritton 			/*
14630304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14640304c731SJamie Gritton 			 * subset of the parent's list.
14650304c731SJamie Gritton 			 */
14660304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
14670304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14680304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
14690304c731SJamie Gritton 					break;
14700304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
14710304c731SJamie Gritton 				error = EPERM;
14722a4b2251SJamie Gritton 				goto done_deref;
14730304c731SJamie Gritton 			}
14740304c731SJamie Gritton 			if (ip6s > 1) {
14750304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
14760304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14770304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
14780304c731SJamie Gritton 						continue;
14790304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
14800304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
14812b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
14820304c731SJamie Gritton 							break;
14830304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
14840304c731SJamie Gritton 						break;
14850304c731SJamie Gritton 				}
14860304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14870304c731SJamie Gritton 					error = EPERM;
14882a4b2251SJamie Gritton 					goto done_deref;
14890304c731SJamie Gritton 				}
14900304c731SJamie Gritton 			}
14910304c731SJamie Gritton 		}
14920304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1493bdfc8cc4SJamie Gritton #ifdef VIMAGE
149408b43333SJamie Gritton 		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1495bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1496bdfc8cc4SJamie Gritton 				break;
149708b43333SJamie Gritton #else
149808b43333SJamie Gritton 		tppr = &prison0;
1499bdfc8cc4SJamie Gritton #endif
1500bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1501bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1502bdfc8cc4SJamie Gritton #ifdef VIMAGE
15032b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1504bdfc8cc4SJamie Gritton #endif
1505bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
15060304c731SJamie Gritton 				descend = 0;
15070304c731SJamie Gritton 				continue;
15080304c731SJamie Gritton 			}
15090304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
15100304c731SJamie Gritton 				continue;
15110304c731SJamie Gritton 			descend = 0;
15120304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
15130304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
15140304c731SJamie Gritton 				continue;
15150304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
15160ce1624dSStephen J. Kiernan 				if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
15170ce1624dSStephen J. Kiernan 				    0) {
15180304c731SJamie Gritton 					error = EADDRINUSE;
1519b38ff370SJamie Gritton 					vfs_opterror(opts,
1520b38ff370SJamie Gritton 					    "IPv6 addresses clash");
15212a4b2251SJamie Gritton 					goto done_deref;
1522b38ff370SJamie Gritton 				}
15230304c731SJamie Gritton 			}
15240304c731SJamie Gritton 		}
15250304c731SJamie Gritton 	}
1526b38ff370SJamie Gritton #endif
15270304c731SJamie Gritton 	onamelen = namelen = 0;
1528b6f47c23SJamie Gritton 	if (namelc != NULL) {
15296ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
15306ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
15316ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
15326ab6058eSJamie Gritton 		 */
1533b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1534b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
15356ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
15366ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1537b38ff370SJamie Gritton 			error = EINVAL;
1538babbbb9cSJamie Gritton 			vfs_opterror(opts,
1539babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
15402a4b2251SJamie Gritton 			goto done_deref;
1541b38ff370SJamie Gritton 		}
1542b38ff370SJamie Gritton 		/*
15430304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
15440304c731SJamie Gritton 		 * children.
1545b38ff370SJamie Gritton 		 */
1546b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1547b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1548b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1549b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
15500304c731SJamie Gritton 			error = ENAMETOOLONG;
15512a4b2251SJamie Gritton 			goto done_deref;
15520304c731SJamie Gritton 		}
15530304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
15540304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
15550304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
15560304c731SJamie Gritton 				error = ENAMETOOLONG;
15572a4b2251SJamie Gritton 				goto done_deref;
15580304c731SJamie Gritton 			}
15590304c731SJamie Gritton 		}
15600304c731SJamie Gritton 	}
1561b3079544SJamie Gritton 	pr_allow_diff = pr_allow & ~ppr->pr_allow;
1562b3079544SJamie Gritton 	if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
15630304c731SJamie Gritton 		error = EPERM;
15642a4b2251SJamie Gritton 		goto done_deref;
1565b38ff370SJamie Gritton 	}
1566b38ff370SJamie Gritton 
1567b6f47c23SJamie Gritton 	/*
1568b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1569b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1570b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1571b6f47c23SJamie Gritton 	 */
1572b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
15732a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1574b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
15752a4b2251SJamie Gritton 	if (error != 0)
15762a4b2251SJamie Gritton 		goto done_deref;
1577b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
15782a4b2251SJamie Gritton 	drflags |= PD_LOCKED;
1579b6f47c23SJamie Gritton 
1580b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1581b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1582b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1583b6f47c23SJamie Gritton 			error = EINVAL;
1584b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
15852a4b2251SJamie Gritton 			goto done_deref;
1586b6f47c23SJamie Gritton 		}
1587b6f47c23SJamie Gritton 	}
1588b6f47c23SJamie Gritton 
1589b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1590b38ff370SJamie Gritton #ifdef INET
15910304c731SJamie Gritton 	redo_ip4 = 0;
15920304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
15930304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1594b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
15950304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1596b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1597b38ff370SJamie Gritton 		ip4 = NULL;
15980304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1599bdfc8cc4SJamie Gritton #ifdef VIMAGE
1600bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1601bdfc8cc4SJamie Gritton 				descend = 0;
1602bdfc8cc4SJamie Gritton 				continue;
1603bdfc8cc4SJamie Gritton 			}
1604bdfc8cc4SJamie Gritton #endif
16050304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
16060304c731SJamie Gritton 				redo_ip4 = 1;
16070304c731SJamie Gritton 				descend = 0;
16080304c731SJamie Gritton 			}
16090304c731SJamie Gritton 		}
16100304c731SJamie Gritton 	}
1611b38ff370SJamie Gritton #endif
1612b38ff370SJamie Gritton #ifdef INET6
16130304c731SJamie Gritton 	redo_ip6 = 0;
16140304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
16150304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1616b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
16170304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1618b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1619b38ff370SJamie Gritton 		ip6 = NULL;
16200304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1621bdfc8cc4SJamie Gritton #ifdef VIMAGE
1622bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1623bdfc8cc4SJamie Gritton 				descend = 0;
1624bdfc8cc4SJamie Gritton 				continue;
1625bdfc8cc4SJamie Gritton 			}
1626bdfc8cc4SJamie Gritton #endif
16270304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
16280304c731SJamie Gritton 				redo_ip6 = 1;
16290304c731SJamie Gritton 				descend = 0;
16300304c731SJamie Gritton 			}
16310304c731SJamie Gritton 		}
16320304c731SJamie Gritton 	}
1633b38ff370SJamie Gritton #endif
16340304c731SJamie Gritton 	if (gotslevel) {
1635b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
16360304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
16370304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16380304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
16390304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
16400304c731SJamie Gritton 	}
1641b97457e2SJamie Gritton 	if (gotchildmax) {
1642b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1643b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1644b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1645b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1646b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1647b97457e2SJamie Gritton 				    ? childmax - level : 0;
1648b97457e2SJamie Gritton 	}
16490304c731SJamie Gritton 	if (gotenforce) {
16500304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
16510304c731SJamie Gritton 		/* Pass this restriction on to the children. */
16520304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16530304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
16540304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
16550304c731SJamie Gritton 	}
16560cc207a6SMartin Matuska 	if (gotrsnum) {
16570cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
16580cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
16590cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16600cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
16610cc207a6SMartin Matuska 	}
1662b6f47c23SJamie Gritton 	if (namelc != NULL) {
16630304c731SJamie Gritton 		if (ppr == &prison0)
1664b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
16650304c731SJamie Gritton 		else
16660304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1667b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
16680304c731SJamie Gritton 		/* Change this component of child names. */
16690304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16700304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
16710304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
16720304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
16730304c731SJamie Gritton 		}
16740304c731SJamie Gritton 	}
1675b38ff370SJamie Gritton 	if (path != NULL) {
16760304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1677b38ff370SJamie Gritton 		strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1678b38ff370SJamie Gritton 		pr->pr_root = root;
16792a4b2251SJamie Gritton 		root = NULL;
1680b38ff370SJamie Gritton 	}
168176ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
168276ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
168376ca6f88SJamie Gritton 			/*
168476ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
168576ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
168676ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
168776ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
168876ca6f88SJamie Gritton 			 */
1689c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1690c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1691c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1692c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1693c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1694c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
169576ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
169676ca6f88SJamie Gritton 		}
169776ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
169876ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1699b38ff370SJamie Gritton 		if (host != NULL)
1700c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
170176ca6f88SJamie Gritton 		if (domain != NULL)
1702c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1703c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
170476ca6f88SJamie Gritton 		if (uuid != NULL)
1705c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
170676ca6f88SJamie Gritton 		if (gothid)
170776ca6f88SJamie Gritton 			pr->pr_hostid = hid;
170876ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
170976ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
171076ca6f88SJamie Gritton 				descend = 0;
171176ca6f88SJamie Gritton 			else {
171276ca6f88SJamie Gritton 				if (host != NULL)
1713c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1714c1f19219SJamie Gritton 					    pr->pr_hostname,
1715c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
171676ca6f88SJamie Gritton 				if (domain != NULL)
1717c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1718c1f19219SJamie Gritton 					    pr->pr_domainname,
1719c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
172076ca6f88SJamie Gritton 				if (uuid != NULL)
1721c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1722c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1723c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
172476ca6f88SJamie Gritton 				if (gothid)
172576ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
172676ca6f88SJamie Gritton 			}
172776ca6f88SJamie Gritton 		}
172876ca6f88SJamie Gritton 	}
17290304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
17300fe74ae6SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow))
17310fe74ae6SJamie Gritton 		prison_set_allow_locked(pr, tallow, 0);
1732b38ff370SJamie Gritton 	/*
1733b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1734b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1735b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1736b38ff370SJamie Gritton 	 * handlers have completed.
1737b38ff370SJamie Gritton 	 */
173876ad42abSJamie Gritton 	born = !prison_isalive(pr);
1739b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1740b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1741b38ff370SJamie Gritton 			pr->pr_ref++;
1742b38ff370SJamie Gritton 			pr->pr_uref++;
1743b38ff370SJamie Gritton 		} else {
1744b38ff370SJamie Gritton 			pr->pr_ref--;
1745b38ff370SJamie Gritton 			pr->pr_uref--;
1746b38ff370SJamie Gritton 		}
1747b38ff370SJamie Gritton 	}
1748b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1749b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
17502a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
1751b38ff370SJamie Gritton 
1752a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
17534b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
1754a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
1755a7ad07bfSEdward Tomasz Napierala #endif
1756a7ad07bfSEdward Tomasz Napierala 
17570304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
17580304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
17590304c731SJamie Gritton 	 */
17600304c731SJamie Gritton #ifdef INET
17610304c731SJamie Gritton 	while (redo_ip4) {
17620304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
17630304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
17640304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17650304c731SJamie Gritton 		redo_ip4 = 0;
17660304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1767bdfc8cc4SJamie Gritton #ifdef VIMAGE
1768bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1769bdfc8cc4SJamie Gritton 				descend = 0;
1770bdfc8cc4SJamie Gritton 				continue;
1771bdfc8cc4SJamie Gritton 			}
1772bdfc8cc4SJamie Gritton #endif
17730304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
17740304c731SJamie Gritton 				if (ip4 != NULL)
17750304c731SJamie Gritton 					ip4 = NULL;
17760304c731SJamie Gritton 				else
17770304c731SJamie Gritton 					redo_ip4 = 1;
17780304c731SJamie Gritton 			}
17790304c731SJamie Gritton 		}
17800304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17810304c731SJamie Gritton 	}
17820304c731SJamie Gritton #endif
17830304c731SJamie Gritton #ifdef INET6
17840304c731SJamie Gritton 	while (redo_ip6) {
17850304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
17860304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
17870304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17880304c731SJamie Gritton 		redo_ip6 = 0;
17890304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1790bdfc8cc4SJamie Gritton #ifdef VIMAGE
1791bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1792bdfc8cc4SJamie Gritton 				descend = 0;
1793bdfc8cc4SJamie Gritton 				continue;
1794bdfc8cc4SJamie Gritton 			}
1795bdfc8cc4SJamie Gritton #endif
17960304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
17970304c731SJamie Gritton 				if (ip6 != NULL)
17980304c731SJamie Gritton 					ip6 = NULL;
17990304c731SJamie Gritton 				else
18000304c731SJamie Gritton 					redo_ip6 = 1;
18010304c731SJamie Gritton 			}
18020304c731SJamie Gritton 		}
18030304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
18040304c731SJamie Gritton 	}
18050304c731SJamie Gritton #endif
18060304c731SJamie Gritton 
1807b38ff370SJamie Gritton 	/* Let the modules do their work. */
1808b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
18092a4b2251SJamie Gritton 	drflags = (drflags & ~PD_LIST_XLOCKED) | PD_LIST_SLOCKED;
1810cc5fd8c7SJamie Gritton 	if (born) {
1811b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1812b38ff370SJamie Gritton 		if (error) {
1813cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
18142a4b2251SJamie Gritton 			goto done_deref;
1815b38ff370SJamie Gritton 		}
1816b38ff370SJamie Gritton 	}
1817b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1818b38ff370SJamie Gritton 	if (error) {
1819cc5fd8c7SJamie Gritton 		if (born)
1820cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
18212a4b2251SJamie Gritton 		goto done_deref;
1822b38ff370SJamie Gritton 	}
1823b38ff370SJamie Gritton 
1824b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1825b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1826b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1827b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
18282a4b2251SJamie Gritton 		drflags &= ~PD_LIST_SLOCKED;
1829b38ff370SJamie Gritton 		if (error) {
18302a4b2251SJamie Gritton 			if (created) {
18312a4b2251SJamie Gritton 				/* do_jail_attach has removed the prison. */
18322a4b2251SJamie Gritton 				pr = NULL;
18332a4b2251SJamie Gritton 			}
1834b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
18352a4b2251SJamie Gritton 			goto done_deref;
1836b38ff370SJamie Gritton 		}
1837b38ff370SJamie Gritton 	}
1838b38ff370SJamie Gritton 
18391fb24974SEdward Tomasz Napierala #ifdef RACCT
18404b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
18412a4b2251SJamie Gritton 		if (drflags & PD_LIST_SLOCKED) {
18421fb24974SEdward Tomasz Napierala 			sx_sunlock(&allprison_lock);
18432a4b2251SJamie Gritton 			drflags &= ~PD_LIST_SLOCKED;
1844b4e87a63SJamie Gritton 		}
18451fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
18461fb24974SEdward Tomasz Napierala 	}
18471fb24974SEdward Tomasz Napierala #endif
18481fb24974SEdward Tomasz Napierala 
18491fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
18501fb24974SEdward Tomasz Napierala 
18512a4b2251SJamie Gritton 	if (created) {
1852b38ff370SJamie Gritton 		/*
18532a4b2251SJamie Gritton 		 * Add a reference to newly created persistent prisons
18542a4b2251SJamie Gritton 		 * (which was not done earlier so that the prison would
18552a4b2251SJamie Gritton 		 * not be publicly visible).
1856b38ff370SJamie Gritton 		 */
1857b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1858b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
18592a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
1860b38ff370SJamie Gritton 			pr->pr_ref++;
1861b38ff370SJamie Gritton 			pr->pr_uref++;
18622a4b2251SJamie Gritton 		} else {
18632a4b2251SJamie Gritton 			/* Non-persistent jails need no further changes. */
18642a4b2251SJamie Gritton 			pr = NULL;
1865b38ff370SJamie Gritton 		}
18662a4b2251SJamie Gritton 	}
18672a4b2251SJamie Gritton 
18682a4b2251SJamie Gritton  done_deref:
18692a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
18702a4b2251SJamie Gritton 	if (pr != NULL)
18712a4b2251SJamie Gritton 		prison_deref(pr, drflags);
18722a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
1873b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
18742a4b2251SJamie Gritton 	else if (drflags & PD_LIST_XLOCKED)
1875b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
18765050aa86SKonstantin Belousov 	if (root != NULL)
1877b38ff370SJamie Gritton 		vrele(root);
1878b38ff370SJamie Gritton  done_errmsg:
1879b38ff370SJamie Gritton 	if (error) {
18802a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
1881176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1882176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
1883b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1884b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
1885b38ff370SJamie Gritton 				bcopy(errmsg,
1886b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1887b38ff370SJamie Gritton 				    errmsg_len);
1888b38ff370SJamie Gritton 			else
1889b38ff370SJamie Gritton 				copyout(errmsg,
1890b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1891b38ff370SJamie Gritton 				    errmsg_len);
1892b38ff370SJamie Gritton 		}
1893b38ff370SJamie Gritton 	}
1894b38ff370SJamie Gritton  done_free:
1895b38ff370SJamie Gritton #ifdef INET
1896b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1897b38ff370SJamie Gritton #endif
1898b38ff370SJamie Gritton #ifdef INET6
1899b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1900b38ff370SJamie Gritton #endif
19016dfe0a3dSMartin Matuska 	if (g_path != NULL)
19026dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
1903b38ff370SJamie Gritton 	vfs_freeopts(opts);
1904b38ff370SJamie Gritton 	return (error);
1905b38ff370SJamie Gritton }
1906b38ff370SJamie Gritton 
1907b38ff370SJamie Gritton /*
19087de883c8SJamie Gritton  * Find the next available prison ID.  Return the ID on success, or zero
19097de883c8SJamie Gritton  * on failure.  Also set a pointer to the allprison list entry the prison
19107de883c8SJamie Gritton  * should be inserted before.
19117de883c8SJamie Gritton  */
19127de883c8SJamie Gritton static int
19137de883c8SJamie Gritton get_next_prid(struct prison **insprp)
19147de883c8SJamie Gritton {
19157de883c8SJamie Gritton 	struct prison *inspr;
19167de883c8SJamie Gritton 	int jid, maxid;
19177de883c8SJamie Gritton 
19187de883c8SJamie Gritton 	jid = lastprid % JAIL_MAX + 1;
19197de883c8SJamie Gritton 	if (TAILQ_EMPTY(&allprison) ||
19207de883c8SJamie Gritton 	    TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
19217de883c8SJamie Gritton 		/*
19227de883c8SJamie Gritton 		 * A common case is for all jails to be implicitly numbered,
19237de883c8SJamie Gritton 		 * which means they'll go on the end of the list, at least
19247de883c8SJamie Gritton 		 * for the first JAIL_MAX times.
19257de883c8SJamie Gritton 		 */
19267de883c8SJamie Gritton 		inspr = NULL;
19277de883c8SJamie Gritton 	} else {
19287de883c8SJamie Gritton 		/*
19297de883c8SJamie Gritton 		 * Take two passes through the allprison list: first starting
19307de883c8SJamie Gritton 		 * with the proposed jid, then ending with it.
19317de883c8SJamie Gritton 		 */
19327de883c8SJamie Gritton 		for (maxid = JAIL_MAX; maxid != 0; ) {
19337de883c8SJamie Gritton 			TAILQ_FOREACH(inspr, &allprison, pr_list) {
19347de883c8SJamie Gritton 				if (inspr->pr_id < jid)
19357de883c8SJamie Gritton 					continue;
19367de883c8SJamie Gritton 				if (inspr->pr_id > jid || inspr->pr_ref == 0) {
19377de883c8SJamie Gritton 					/*
19387de883c8SJamie Gritton 					 * Found an opening.  This may be a gap
19397de883c8SJamie Gritton 					 * in the list, or a dead jail with the
19407de883c8SJamie Gritton 					 * same ID.
19417de883c8SJamie Gritton 					 */
19427de883c8SJamie Gritton 					maxid = 0;
19437de883c8SJamie Gritton 					break;
19447de883c8SJamie Gritton 				}
19457de883c8SJamie Gritton 				if (++jid > maxid) {
19467de883c8SJamie Gritton 					if (lastprid == maxid || lastprid == 0)
19477de883c8SJamie Gritton 					{
19487de883c8SJamie Gritton 						/*
19497de883c8SJamie Gritton 						 * The entire legal range
19507de883c8SJamie Gritton 						 * has been traversed
19517de883c8SJamie Gritton 						 */
19527de883c8SJamie Gritton 						return 0;
19537de883c8SJamie Gritton 					}
19547de883c8SJamie Gritton 					/* Try again from the start. */
19557de883c8SJamie Gritton 					jid = 1;
19567de883c8SJamie Gritton 					maxid = lastprid;
19577de883c8SJamie Gritton 					break;
19587de883c8SJamie Gritton 				}
19597de883c8SJamie Gritton 			}
19607de883c8SJamie Gritton 			if (inspr == NULL) {
19617de883c8SJamie Gritton 				/* Found room at the end of the list. */
19627de883c8SJamie Gritton 				break;
19637de883c8SJamie Gritton 			}
19647de883c8SJamie Gritton 		}
19657de883c8SJamie Gritton 	}
19667de883c8SJamie Gritton 	*insprp = inspr;
19677de883c8SJamie Gritton 	lastprid = jid;
19687de883c8SJamie Gritton 	return (jid);
19697de883c8SJamie Gritton }
19707de883c8SJamie Gritton 
19717de883c8SJamie Gritton /*
1972b38ff370SJamie Gritton  * struct jail_get_args {
1973b38ff370SJamie Gritton  *	struct iovec *iovp;
1974b38ff370SJamie Gritton  *	unsigned int iovcnt;
1975b38ff370SJamie Gritton  *	int flags;
1976b38ff370SJamie Gritton  * };
1977b38ff370SJamie Gritton  */
1978b38ff370SJamie Gritton int
19798451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
1980b38ff370SJamie Gritton {
1981b38ff370SJamie Gritton 	struct uio *auio;
1982b38ff370SJamie Gritton 	int error;
1983b38ff370SJamie Gritton 
1984b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1985b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1986b38ff370SJamie Gritton 		return (EINVAL);
1987b38ff370SJamie Gritton 
1988b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1989b38ff370SJamie Gritton 	if (error)
1990b38ff370SJamie Gritton 		return (error);
1991b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1992b38ff370SJamie Gritton 	if (error == 0)
1993b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1994b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1995b38ff370SJamie Gritton 	free(auio, M_IOV);
1996b38ff370SJamie Gritton 	return (error);
1997b38ff370SJamie Gritton }
1998b38ff370SJamie Gritton 
1999b38ff370SJamie Gritton int
2000b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2001b38ff370SJamie Gritton {
2002672756aaSJamie Gritton 	struct bool_flags *bf;
2003672756aaSJamie Gritton 	struct jailsys_flags *jsf;
20040304c731SJamie Gritton 	struct prison *pr, *mypr;
2005b38ff370SJamie Gritton 	struct vfsopt *opt;
2006b38ff370SJamie Gritton 	struct vfsoptlist *opts;
2007b38ff370SJamie Gritton 	char *errmsg, *name;
20082a4b2251SJamie Gritton 	int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2009672756aaSJamie Gritton 	unsigned f;
2010b38ff370SJamie Gritton 
2011b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
2012b38ff370SJamie Gritton 		return (EINVAL);
2013b38ff370SJamie Gritton 
2014b38ff370SJamie Gritton 	/* Get the parameter list. */
2015b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
2016b38ff370SJamie Gritton 	if (error)
2017b38ff370SJamie Gritton 		return (error);
2018b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
20190304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
20202a4b2251SJamie Gritton 	pr = NULL;
20211e2a13e6SJamie Gritton 
2022b38ff370SJamie Gritton 	/*
2023b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
2024b38ff370SJamie Gritton 	 */
2025b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
20262a4b2251SJamie Gritton 	drflags = PD_LIST_SLOCKED;
2027b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2028b38ff370SJamie Gritton 	if (error == 0) {
2029b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
20300304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
2031b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
203276ad42abSJamie Gritton 				if ((flags & JAIL_DYING)
203376ad42abSJamie Gritton 				    ? prison_isvalid(pr) : prison_isalive(pr))
2034b38ff370SJamie Gritton 					break;
2035b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
2036b38ff370SJamie Gritton 			}
2037b38ff370SJamie Gritton 		}
20382a4b2251SJamie Gritton 		if (pr != NULL) {
20392a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
2040b38ff370SJamie Gritton 			goto found_prison;
20412a4b2251SJamie Gritton 		}
2042b38ff370SJamie Gritton 		error = ENOENT;
2043b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
20442a4b2251SJamie Gritton 		goto done;
2045b38ff370SJamie Gritton 	} else if (error != ENOENT)
20462a4b2251SJamie Gritton 		goto done;
2047b38ff370SJamie Gritton 
2048b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2049b38ff370SJamie Gritton 	if (error == 0) {
2050b38ff370SJamie Gritton 		if (jid != 0) {
20510304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
2052b38ff370SJamie Gritton 			if (pr != NULL) {
20532a4b2251SJamie Gritton 				drflags |= PD_LOCKED;
205476ad42abSJamie Gritton 				if (!(prison_isalive(pr) ||
205576ad42abSJamie Gritton 				    (flags & JAIL_DYING))) {
2056b38ff370SJamie Gritton 					error = ENOENT;
2057b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
2058b38ff370SJamie Gritton 					    jid);
20592a4b2251SJamie Gritton 					goto done;
2060b38ff370SJamie Gritton 				}
2061b38ff370SJamie Gritton 				goto found_prison;
2062b38ff370SJamie Gritton 			}
2063b38ff370SJamie Gritton 			error = ENOENT;
2064b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
20652a4b2251SJamie Gritton 			goto done;
2066b38ff370SJamie Gritton 		}
2067b38ff370SJamie Gritton 	} else if (error != ENOENT)
20682a4b2251SJamie Gritton 		goto done;
2069b38ff370SJamie Gritton 
2070b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2071b38ff370SJamie Gritton 	if (error == 0) {
2072b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2073b38ff370SJamie Gritton 			error = EINVAL;
20742a4b2251SJamie Gritton 			goto done;
2075b38ff370SJamie Gritton 		}
20760304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2077b38ff370SJamie Gritton 		if (pr != NULL) {
20782a4b2251SJamie Gritton 			drflags |= PD_LOCKED;
207976ad42abSJamie Gritton 			if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2080b38ff370SJamie Gritton 				error = ENOENT;
2081b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2082b38ff370SJamie Gritton 				    name);
20832a4b2251SJamie Gritton 				goto done;
2084b38ff370SJamie Gritton 			}
2085b38ff370SJamie Gritton 			goto found_prison;
2086b38ff370SJamie Gritton 		}
2087b38ff370SJamie Gritton 		error = ENOENT;
2088b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
20892a4b2251SJamie Gritton 		goto done;
2090b38ff370SJamie Gritton 	} else if (error != ENOENT)
20912a4b2251SJamie Gritton 		goto done;
2092b38ff370SJamie Gritton 
2093b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2094b38ff370SJamie Gritton 	error = ENOENT;
20952a4b2251SJamie Gritton 	goto done;
2096b38ff370SJamie Gritton 
2097b38ff370SJamie Gritton  found_prison:
2098b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
2099b38ff370SJamie Gritton 	pr->pr_ref++;
21002a4b2251SJamie Gritton 	drflags |= PD_DEREF;
2101b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2102b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2103b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21042a4b2251SJamie Gritton 		goto done;
21050304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
21060304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2107b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21082a4b2251SJamie Gritton 		goto done;
21090304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
21100304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
21112a4b2251SJamie Gritton 		goto done;
21120304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2113b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2114b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21152a4b2251SJamie Gritton 		goto done;
21160304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2117b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21182a4b2251SJamie Gritton 		goto done;
2119b38ff370SJamie Gritton #ifdef INET
2120b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2121b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2122b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21232a4b2251SJamie Gritton 		goto done;
2124b38ff370SJamie Gritton #endif
2125b38ff370SJamie Gritton #ifdef INET6
2126b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2127b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2128b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21292a4b2251SJamie Gritton 		goto done;
2130b38ff370SJamie Gritton #endif
2131b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2132b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2133b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21342a4b2251SJamie Gritton 		goto done;
2135b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2136b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2137b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
21382a4b2251SJamie Gritton 		goto done;
2139b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2140b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2141b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
21422a4b2251SJamie Gritton 		goto done;
2143c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2144b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
21452a4b2251SJamie Gritton 		goto done;
2146c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
214776ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
21482a4b2251SJamie Gritton 		goto done;
2149c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
215076ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
21512a4b2251SJamie Gritton 		goto done;
2152841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2153a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
215476ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
215576ca6f88SJamie Gritton 
215676ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
215776ca6f88SJamie Gritton 	} else
215876ca6f88SJamie Gritton #endif
215976ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
216076ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
216176ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
21622a4b2251SJamie Gritton 		goto done;
21630304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
21640304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
21650304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
21662a4b2251SJamie Gritton 		goto done;
21670cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
21680cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
21690cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
21702a4b2251SJamie Gritton 		goto done;
2171672756aaSJamie Gritton 	for (bf = pr_flag_bool;
2172672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2173672756aaSJamie Gritton 	     bf++) {
2174672756aaSJamie Gritton 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2175672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2176b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
21772a4b2251SJamie Gritton 			goto done;
2178b38ff370SJamie Gritton 		i = !i;
2179672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2180b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
21812a4b2251SJamie Gritton 			goto done;
21820304c731SJamie Gritton 	}
2183672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
2184672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2185672756aaSJamie Gritton 	     jsf++) {
2186672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
2187672756aaSJamie Gritton 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2188672756aaSJamie Gritton 		    : (f == jsf->new) ? JAIL_SYS_NEW
21897cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
2190672756aaSJamie Gritton 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
21917cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
21922a4b2251SJamie Gritton 			goto done;
21937cbf7213SJamie Gritton 	}
2194672756aaSJamie Gritton 	for (bf = pr_flag_allow;
21955d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
21965d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
2197672756aaSJamie Gritton 	     bf++) {
2198672756aaSJamie Gritton 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2199672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
22000304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
22012a4b2251SJamie Gritton 			goto done;
22020304c731SJamie Gritton 		i = !i;
2203672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
22040304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
22052a4b2251SJamie Gritton 			goto done;
22060304c731SJamie Gritton 	}
220776ad42abSJamie Gritton 	i = !prison_isalive(pr);
2208b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2209b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
22102a4b2251SJamie Gritton 		goto done;
2211b38ff370SJamie Gritton 	i = !i;
2212b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2213b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
22142a4b2251SJamie Gritton 		goto done;
2215bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2216bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2217a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
22182a4b2251SJamie Gritton 		goto done;
2219a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2220a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
22212a4b2251SJamie Gritton 		goto done;
2222b38ff370SJamie Gritton 
2223b38ff370SJamie Gritton 	/* Get the module parameters. */
2224b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
22252a4b2251SJamie Gritton 	drflags &= ~PD_LOCKED;
2226b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2227b38ff370SJamie Gritton 	if (error)
22282a4b2251SJamie Gritton 		goto done;
22292a4b2251SJamie Gritton 	prison_deref(pr, drflags);
22302a4b2251SJamie Gritton 	pr = NULL;
22312a4b2251SJamie Gritton 	drflags = 0;
2232b38ff370SJamie Gritton 
2233b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2234b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2235b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2236b38ff370SJamie Gritton 			error = EINVAL;
2237b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
22382a4b2251SJamie Gritton 			goto done;
2239b38ff370SJamie Gritton 		}
2240b38ff370SJamie Gritton 	}
2241b38ff370SJamie Gritton 
2242b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2243b38ff370SJamie Gritton 	error = 0;
2244b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2245b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2246b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2247b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2248b38ff370SJamie Gritton 			if (opt->value != NULL) {
2249b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2250b38ff370SJamie Gritton 					bcopy(opt->value,
2251b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2252b38ff370SJamie Gritton 					    opt->len);
2253b38ff370SJamie Gritton 				} else {
2254b38ff370SJamie Gritton 					error = copyout(opt->value,
2255b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2256b38ff370SJamie Gritton 					    opt->len);
2257b38ff370SJamie Gritton 					if (error)
2258b38ff370SJamie Gritton 						break;
2259b38ff370SJamie Gritton 				}
2260b38ff370SJamie Gritton 			}
2261b38ff370SJamie Gritton 		}
2262b38ff370SJamie Gritton 	}
2263b38ff370SJamie Gritton 
22642a4b2251SJamie Gritton  done:
22652a4b2251SJamie Gritton 	/* Release any temporary prison holds and/or locks. */
22662a4b2251SJamie Gritton 	if (pr != NULL)
22672a4b2251SJamie Gritton 		prison_deref(pr, drflags);
22682a4b2251SJamie Gritton 	else if (drflags & PD_LIST_SLOCKED)
2269b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2270b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
22712a4b2251SJamie Gritton 		/* Write the error message back to userspace. */
2272b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2273b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2274b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2275b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2276b38ff370SJamie Gritton 				bcopy(errmsg,
2277b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2278b38ff370SJamie Gritton 				    errmsg_len);
2279b38ff370SJamie Gritton 			else
2280b38ff370SJamie Gritton 				copyout(errmsg,
2281b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2282b38ff370SJamie Gritton 				    errmsg_len);
2283b38ff370SJamie Gritton 		}
2284b38ff370SJamie Gritton 	}
2285b38ff370SJamie Gritton 	vfs_freeopts(opts);
2286b38ff370SJamie Gritton 	return (error);
2287b38ff370SJamie Gritton }
2288b38ff370SJamie Gritton 
2289b38ff370SJamie Gritton /*
2290b38ff370SJamie Gritton  * struct jail_remove_args {
2291b38ff370SJamie Gritton  *	int jid;
2292b38ff370SJamie Gritton  * };
2293b38ff370SJamie Gritton  */
2294b38ff370SJamie Gritton int
22958451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2296b38ff370SJamie Gritton {
22970304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
22980304c731SJamie Gritton 	int descend, error;
2299b38ff370SJamie Gritton 
2300b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2301b38ff370SJamie Gritton 	if (error)
2302b38ff370SJamie Gritton 		return (error);
2303b38ff370SJamie Gritton 
2304b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
23050304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2306b38ff370SJamie Gritton 	if (pr == NULL) {
2307b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2308b38ff370SJamie Gritton 		return (EINVAL);
2309b38ff370SJamie Gritton 	}
2310b38ff370SJamie Gritton 
23110304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
23120304c731SJamie Gritton 	pr->pr_ref++;
23130304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
23140304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
23150304c731SJamie Gritton 		lpr = NULL;
23160304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
23170304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
231876ad42abSJamie Gritton 			if (prison_isvalid(cpr)) {
23190304c731SJamie Gritton 				tpr = cpr;
23200304c731SJamie Gritton 				cpr->pr_ref++;
23210304c731SJamie Gritton 			} else {
23220304c731SJamie Gritton 				/* Already removed - do not do it again. */
23230304c731SJamie Gritton 				tpr = NULL;
23240304c731SJamie Gritton 			}
23250304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
23260304c731SJamie Gritton 			if (lpr != NULL) {
23270304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
23280304c731SJamie Gritton 				prison_remove_one(lpr);
23290304c731SJamie Gritton 				sx_xlock(&allprison_lock);
23300304c731SJamie Gritton 			}
23310304c731SJamie Gritton 			lpr = tpr;
23320304c731SJamie Gritton 		}
23330304c731SJamie Gritton 		if (lpr != NULL) {
23340304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
23350304c731SJamie Gritton 			prison_remove_one(lpr);
23360304c731SJamie Gritton 			sx_xlock(&allprison_lock);
23370304c731SJamie Gritton 		}
23380304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
23390304c731SJamie Gritton 	}
23400304c731SJamie Gritton 	prison_remove_one(pr);
23410304c731SJamie Gritton 	return (0);
23420304c731SJamie Gritton }
23430304c731SJamie Gritton 
23440304c731SJamie Gritton static void
23450304c731SJamie Gritton prison_remove_one(struct prison *pr)
23460304c731SJamie Gritton {
23470304c731SJamie Gritton 	struct proc *p;
23482a4b2251SJamie Gritton 	int drflags;
23492a4b2251SJamie Gritton 
23502a4b2251SJamie Gritton 	drflags = PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED;
23510304c731SJamie Gritton 
2352b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2353b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2354b38ff370SJamie Gritton 		pr->pr_ref--;
23552a4b2251SJamie Gritton 		drflags |= PD_DEUREF;
2356b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2357b38ff370SJamie Gritton 	}
2358b38ff370SJamie Gritton 
23590304c731SJamie Gritton 	/*
23600304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
23610304c731SJamie Gritton 	 * the prison now.
23620304c731SJamie Gritton 	 */
23630304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
23640304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
23650304c731SJamie Gritton 	if (pr->pr_ref == 1) {
23662a4b2251SJamie Gritton 		prison_deref(pr, drflags);
23670304c731SJamie Gritton 		return;
2368b38ff370SJamie Gritton 	}
2369b38ff370SJamie Gritton 
2370b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2371b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
23722a4b2251SJamie Gritton 	drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2373b38ff370SJamie Gritton 	/*
2374b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2375b38ff370SJamie Gritton 	 */
2376b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
23777938a442SBjoern A. Zeeb 	FOREACH_PROC_IN_SYSTEM(p) {
2378b38ff370SJamie Gritton 		PROC_LOCK(p);
2379b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2380b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
23818451d0ddSKip Macy 			kern_psignal(p, SIGKILL);
2382b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2383b38ff370SJamie Gritton 	}
2384b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
23850304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
23862a4b2251SJamie Gritton 	prison_deref(pr, drflags);
238775c13541SPoul-Henning Kamp }
238875c13541SPoul-Henning Kamp 
2389fd7a8150SMike Barcroft /*
23909ddb7954SMike Barcroft  * struct jail_attach_args {
23919ddb7954SMike Barcroft  *	int jid;
23929ddb7954SMike Barcroft  * };
2393fd7a8150SMike Barcroft  */
2394fd7a8150SMike Barcroft int
23958451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2396fd7a8150SMike Barcroft {
2397b38ff370SJamie Gritton 	struct prison *pr;
2398b38ff370SJamie Gritton 	int error;
2399b38ff370SJamie Gritton 
2400b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2401b38ff370SJamie Gritton 	if (error)
2402b38ff370SJamie Gritton 		return (error);
2403b38ff370SJamie Gritton 
2404ef0ddea3SJamie Gritton 	/*
2405ef0ddea3SJamie Gritton 	 * Start with exclusive hold on allprison_lock to ensure that a possible
2406ef0ddea3SJamie Gritton 	 * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove.
2407ef0ddea3SJamie Gritton 	 * But then immediately downgrade it since we don't need to stop
2408ef0ddea3SJamie Gritton 	 * readers.
2409ef0ddea3SJamie Gritton 	 */
2410ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
2411ef0ddea3SJamie Gritton 	sx_downgrade(&allprison_lock);
24120304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2413b38ff370SJamie Gritton 	if (pr == NULL) {
2414b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2415b38ff370SJamie Gritton 		return (EINVAL);
2416b38ff370SJamie Gritton 	}
2417b38ff370SJamie Gritton 
241876ad42abSJamie Gritton 	/* Do not allow a process to attach to a prison that is not alive. */
241976ad42abSJamie Gritton 	if (!prison_isalive(pr)) {
2420b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2421b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2422b38ff370SJamie Gritton 		return (EINVAL);
2423b38ff370SJamie Gritton 	}
2424b38ff370SJamie Gritton 
2425b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2426b38ff370SJamie Gritton }
2427b38ff370SJamie Gritton 
2428b38ff370SJamie Gritton static int
2429b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2430b38ff370SJamie Gritton {
2431fd7a8150SMike Barcroft 	struct proc *p;
2432fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
24335050aa86SKonstantin Belousov 	int error;
2434fd7a8150SMike Barcroft 
243557f22bd4SJacques Vidrine 	/*
243657f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
243757f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
243857f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
243957f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
244057f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
244157f22bd4SJacques Vidrine 	 * of another.
244257f22bd4SJacques Vidrine 	 */
2443fd7a8150SMike Barcroft 	pr->pr_ref++;
2444b38ff370SJamie Gritton 	pr->pr_uref++;
2445fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2446b38ff370SJamie Gritton 
2447b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2448b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2449b38ff370SJamie Gritton 	if (error) {
2450b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2451b38ff370SJamie Gritton 		return (error);
2452b38ff370SJamie Gritton 	}
2453dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2454fd7a8150SMike Barcroft 
2455413628a7SBjoern A. Zeeb 	/*
2456413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2457413628a7SBjoern A. Zeeb 	 */
2458b38ff370SJamie Gritton 	p = td->td_proc;
2459413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2460413628a7SBjoern A. Zeeb 	if (error)
2461b38ff370SJamie Gritton 		goto e_revert_osd;
2462413628a7SBjoern A. Zeeb 
2463cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2464fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2465fd7a8150SMike Barcroft 		goto e_unlock;
2466fd7a8150SMike Barcroft #ifdef MAC
246730d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2468fd7a8150SMike Barcroft 		goto e_unlock;
2469fd7a8150SMike Barcroft #endif
2470b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2471f0725a8eSMateusz Guzik 	if ((error = pwd_chroot(td, pr->pr_root)))
24725050aa86SKonstantin Belousov 		goto e_revert_osd;
2473fd7a8150SMike Barcroft 
2474fd7a8150SMike Barcroft 	newcred = crget();
2475fd7a8150SMike Barcroft 	PROC_LOCK(p);
24761fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
247769c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2478daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
24791fb6767dSJamie Gritton 	setsugid(p);
2480097055e2SEdward Tomasz Napierala #ifdef RACCT
2481097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2482f87beb93SAndriy Gapon 	crhold(newcred);
2483f87beb93SAndriy Gapon #endif
2484f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
2485f87beb93SAndriy Gapon #ifdef RCTL
2486f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
2487f87beb93SAndriy Gapon 	crfree(newcred);
2488097055e2SEdward Tomasz Napierala #endif
24891fb6767dSJamie Gritton 	prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
2490fd7a8150SMike Barcroft 	crfree(oldcred);
2491fd7a8150SMike Barcroft 	return (0);
24921fb6767dSJamie Gritton 
2493fd7a8150SMike Barcroft  e_unlock:
2494b249ce48SMateusz Guzik 	VOP_UNLOCK(pr->pr_root);
2495b38ff370SJamie Gritton  e_revert_osd:
2496b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
24977f4e7248SJamie Gritton 	sx_slock(&allprison_lock);
24981fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
24997f4e7248SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2500fd7a8150SMike Barcroft 	return (error);
2501fd7a8150SMike Barcroft }
2502fd7a8150SMike Barcroft 
2503fd7a8150SMike Barcroft /*
2504fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2505fd7a8150SMike Barcroft  */
250654b369c1SPawel Jakub Dawidek struct prison *
2507fd7a8150SMike Barcroft prison_find(int prid)
2508fd7a8150SMike Barcroft {
2509fd7a8150SMike Barcroft 	struct prison *pr;
2510fd7a8150SMike Barcroft 
2511dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2512b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2513fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2514fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
251576ad42abSJamie Gritton 			if (prison_isvalid(pr))
2516fd7a8150SMike Barcroft 				return (pr);
25177de883c8SJamie Gritton 			/*
25187de883c8SJamie Gritton 			 * Any active prison with the same ID would have
25197de883c8SJamie Gritton 			 * been inserted before a dead one.
25207de883c8SJamie Gritton 			 */
2521b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
25227de883c8SJamie Gritton 			break;
2523fd7a8150SMike Barcroft 		}
25247de883c8SJamie Gritton 		if (pr->pr_id > prid)
25257de883c8SJamie Gritton 			break;
2526fd7a8150SMike Barcroft 	}
2527fd7a8150SMike Barcroft 	return (NULL);
2528fd7a8150SMike Barcroft }
2529fd7a8150SMike Barcroft 
2530b38ff370SJamie Gritton /*
25310304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2532b38ff370SJamie Gritton  */
2533b38ff370SJamie Gritton struct prison *
25340304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2535b38ff370SJamie Gritton {
25360304c731SJamie Gritton 	struct prison *pr;
25370304c731SJamie Gritton 	int descend;
2538b38ff370SJamie Gritton 
2539b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
25400304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
25410304c731SJamie Gritton 		if (pr->pr_id == prid) {
25420304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
254376ad42abSJamie Gritton 			if (prison_isvalid(pr))
25440304c731SJamie Gritton 				return (pr);
25450304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
25460304c731SJamie Gritton 		}
25470304c731SJamie Gritton 	}
25480304c731SJamie Gritton 	return (NULL);
25490304c731SJamie Gritton }
25500304c731SJamie Gritton 
25510304c731SJamie Gritton /*
25520304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
25530304c731SJamie Gritton  */
25540304c731SJamie Gritton struct prison *
25550304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
25560304c731SJamie Gritton {
25570304c731SJamie Gritton 	struct prison *pr, *deadpr;
25580304c731SJamie Gritton 	size_t mylen;
25590304c731SJamie Gritton 	int descend;
25600304c731SJamie Gritton 
25610304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
25620304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2563b38ff370SJamie Gritton  again:
2564b38ff370SJamie Gritton 	deadpr = NULL;
25650304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
25660304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2567b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
256876ad42abSJamie Gritton 			if (prison_isalive(pr))
2569b38ff370SJamie Gritton 				return (pr);
257076ad42abSJamie Gritton 			if (prison_isvalid(pr))
2571b38ff370SJamie Gritton 				deadpr = pr;
2572b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2573b38ff370SJamie Gritton 		}
2574b38ff370SJamie Gritton 	}
25750304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2576b38ff370SJamie Gritton 	if (deadpr != NULL) {
2577b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
257876ad42abSJamie Gritton 		if (!prison_isvalid(deadpr)) {
2579b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2580b38ff370SJamie Gritton 			goto again;
2581b38ff370SJamie Gritton 		}
2582b38ff370SJamie Gritton 	}
2583b38ff370SJamie Gritton 	return (deadpr);
2584b38ff370SJamie Gritton }
2585b38ff370SJamie Gritton 
2586b38ff370SJamie Gritton /*
25870fe74ae6SJamie Gritton  * See if a prison has the specific flag set.  The prison should be locked,
25880fe74ae6SJamie Gritton  * unless checking for flags that are only set at jail creation (such as
25890fe74ae6SJamie Gritton  * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
25900fe74ae6SJamie Gritton  * to any other prison data.
25910304c731SJamie Gritton  */
25920304c731SJamie Gritton int
25930304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
25940304c731SJamie Gritton {
25950304c731SJamie Gritton 
25960304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
25970304c731SJamie Gritton }
25980304c731SJamie Gritton 
25990304c731SJamie Gritton int
26000304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
26010304c731SJamie Gritton {
26020304c731SJamie Gritton 
26030fe74ae6SJamie Gritton 	return ((cred->cr_prison->pr_allow & flag) != 0);
26040304c731SJamie Gritton }
26050304c731SJamie Gritton 
26060304c731SJamie Gritton /*
2607*effad35eSJamie Gritton  * Hold a prison reference, by incrementing pr_ref.  It is generally
2608*effad35eSJamie Gritton  * an error to hold a prison that does not already have a reference.
2609*effad35eSJamie Gritton  * A prison record will remain valid as long as it has at least one
2610*effad35eSJamie Gritton  * reference, and will not be removed as long as either the prison
2611*effad35eSJamie Gritton  * mutex or the allprison lock is held (allprison_lock may be shared).
2612*effad35eSJamie Gritton  */
2613*effad35eSJamie Gritton void
2614*effad35eSJamie Gritton prison_hold_locked(struct prison *pr)
2615*effad35eSJamie Gritton {
2616*effad35eSJamie Gritton 
2617*effad35eSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
2618*effad35eSJamie Gritton 	KASSERT(pr->pr_ref > 0,
2619*effad35eSJamie Gritton 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2620*effad35eSJamie Gritton 	pr->pr_ref++;
2621*effad35eSJamie Gritton }
2622*effad35eSJamie Gritton 
2623*effad35eSJamie Gritton void
2624*effad35eSJamie Gritton prison_hold(struct prison *pr)
2625*effad35eSJamie Gritton {
2626*effad35eSJamie Gritton 
2627*effad35eSJamie Gritton 	mtx_lock(&pr->pr_mtx);
2628*effad35eSJamie Gritton 	prison_hold_locked(pr);
2629*effad35eSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2630*effad35eSJamie Gritton }
2631*effad35eSJamie Gritton 
2632*effad35eSJamie Gritton /*
2633*effad35eSJamie Gritton  * Remove a prison reference.  If that was the last reference, the
2634*effad35eSJamie Gritton  * prison will be removed (at a later time).  Return with the prison
2635*effad35eSJamie Gritton  * unlocked.
2636b38ff370SJamie Gritton  */
263791421ba2SRobert Watson void
26381ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
263991421ba2SRobert Watson {
264073d9e52dSJamie Gritton 	int ref;
264191421ba2SRobert Watson 
26421ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
264373d9e52dSJamie Gritton 	ref = --pr->pr_ref;
264401137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
2645*effad35eSJamie Gritton 	if (ref == 0) {
2646*effad35eSJamie Gritton 		/*
2647*effad35eSJamie Gritton 		 * Don't remove the last reference in this context,
2648*effad35eSJamie Gritton 		 * in case there are locks held.
2649*effad35eSJamie Gritton 		 */
2650c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2651c2cda609SPawel Jakub Dawidek 	}
2652*effad35eSJamie Gritton }
2653c2cda609SPawel Jakub Dawidek 
26541ba4a712SPawel Jakub Dawidek void
26551ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
26561ba4a712SPawel Jakub Dawidek {
26571ba4a712SPawel Jakub Dawidek 
26581ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
26591ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
26601ba4a712SPawel Jakub Dawidek }
26611ba4a712SPawel Jakub Dawidek 
266273d9e52dSJamie Gritton /*
2663*effad35eSJamie Gritton  * Hold a a prison for user visibility, by incrementing pr_uref.
2664*effad35eSJamie Gritton  * It is generally an error to hold a prison that isn't already
2665*effad35eSJamie Gritton  * user-visible, except through the the jail system calls.  It is also
2666*effad35eSJamie Gritton  * an error to hold an invalid prison.  A prison record will remain
2667*effad35eSJamie Gritton  * alive as long as it has at least one user reference, and will not
2668*effad35eSJamie Gritton  * be set to the dying state was long as the prison mutex is held.
2669*effad35eSJamie Gritton  */
2670*effad35eSJamie Gritton void
2671*effad35eSJamie Gritton prison_proc_hold(struct prison *pr)
2672*effad35eSJamie Gritton {
2673*effad35eSJamie Gritton 
2674*effad35eSJamie Gritton 	mtx_lock(&pr->pr_mtx);
2675*effad35eSJamie Gritton 	KASSERT(pr->pr_uref > 0,
2676*effad35eSJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2677*effad35eSJamie Gritton 	pr->pr_uref++;
2678*effad35eSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2679*effad35eSJamie Gritton }
2680*effad35eSJamie Gritton 
2681*effad35eSJamie Gritton /*
2682*effad35eSJamie Gritton  * Remove a prison user reference.  If it was the last reference, the
2683*effad35eSJamie Gritton  * prison will be considered "dying", and may be removed once all of
2684*effad35eSJamie Gritton  * its references are dropped.
2685*effad35eSJamie Gritton  */
2686*effad35eSJamie Gritton void
2687*effad35eSJamie Gritton prison_proc_free(struct prison *pr)
2688*effad35eSJamie Gritton {
2689*effad35eSJamie Gritton 
2690*effad35eSJamie Gritton 	mtx_lock(&pr->pr_mtx);
2691*effad35eSJamie Gritton 	KASSERT(pr->pr_uref > 0,
2692*effad35eSJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2693*effad35eSJamie Gritton 	if (pr->pr_uref > 1)
2694*effad35eSJamie Gritton 		pr->pr_uref--;
2695*effad35eSJamie Gritton 	else {
2696*effad35eSJamie Gritton 		/*
2697*effad35eSJamie Gritton 		 * Don't remove the last user reference in this context,
2698*effad35eSJamie Gritton 		 * which is expected to be a process that is not only locked,
2699*effad35eSJamie Gritton 		 * but also half dead.  Add a reference so any calls to
2700*effad35eSJamie Gritton 		 * prison_free() won't re-submit the task.
2701*effad35eSJamie Gritton 		 */
2702*effad35eSJamie Gritton 		pr->pr_ref++;
2703*effad35eSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2704*effad35eSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2705*effad35eSJamie Gritton 		return;
2706*effad35eSJamie Gritton 	}
2707*effad35eSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2708*effad35eSJamie Gritton }
2709*effad35eSJamie Gritton 
2710*effad35eSJamie Gritton /*
271173d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
271273d9e52dSJamie Gritton  */
2713c2cda609SPawel Jakub Dawidek static void
2714c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2715c2cda609SPawel Jakub Dawidek {
271673d9e52dSJamie Gritton 	struct prison *pr = context;
2717b38ff370SJamie Gritton 
2718ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
271973d9e52dSJamie Gritton 	mtx_lock(&pr->pr_mtx);
2720*effad35eSJamie Gritton 	/*
2721*effad35eSJamie Gritton 	 * If this is completing a call to prison_proc_free, there will still
2722*effad35eSJamie Gritton 	 * be a user reference held; clear that as well as the reference that
2723*effad35eSJamie Gritton 	 * was added.  No references are expected if this is completing a call
2724*effad35eSJamie Gritton 	 * to prison_free, but prison_deref is still called for the cleanup.
2725*effad35eSJamie Gritton 	 */
2726*effad35eSJamie Gritton 	prison_deref(pr, pr->pr_uref > 0
2727ef0ddea3SJamie Gritton 	    ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED
2728ef0ddea3SJamie Gritton 	    : PD_LOCKED | PD_LIST_XLOCKED);
2729b38ff370SJamie Gritton }
2730b38ff370SJamie Gritton 
2731b38ff370SJamie Gritton /*
2732*effad35eSJamie Gritton  * Remove a prison reference and/or user reference (usually).
2733*effad35eSJamie Gritton  * This assumes context that allows sleeping (for allprison_lock),
2734*effad35eSJamie Gritton  * with no non-sleeping locks held, except perhaps the prison itself.
2735*effad35eSJamie Gritton  * If there are no more references, release and delist the prison.
2736*effad35eSJamie Gritton  * On completion, the prison lock and the allprison lock are both
2737*effad35eSJamie Gritton  * unlocked.
2738b38ff370SJamie Gritton  */
2739b38ff370SJamie Gritton static void
2740b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2741b38ff370SJamie Gritton {
27420304c731SJamie Gritton 	struct prison *ppr, *tpr;
2743cc5fd8c7SJamie Gritton 	int ref, lasturef;
2744c2cda609SPawel Jakub Dawidek 
2745b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2746b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
27470304c731SJamie Gritton 	for (;;) {
2748e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
274973d9e52dSJamie Gritton 			KASSERT(pr->pr_uref > 0,
275073d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
275173d9e52dSJamie Gritton 			     pr->pr_id));
2752e6d5cb63SJamie Gritton 			pr->pr_uref--;
2753cc5fd8c7SJamie Gritton 			lasturef = pr->pr_uref == 0;
2754cc5fd8c7SJamie Gritton 			if (lasturef)
2755cc5fd8c7SJamie Gritton 				pr->pr_ref++;
2756e6d5cb63SJamie Gritton 			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2757cc5fd8c7SJamie Gritton 		} else
2758cc5fd8c7SJamie Gritton 			lasturef = 0;
275973d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
276073d9e52dSJamie Gritton 			KASSERT(pr->pr_ref > 0,
276173d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
276273d9e52dSJamie Gritton 			     pr->pr_id));
2763b38ff370SJamie Gritton 			pr->pr_ref--;
276473d9e52dSJamie Gritton 		}
2765cc5fd8c7SJamie Gritton 		ref = pr->pr_ref;
2766b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2767cc5fd8c7SJamie Gritton 
2768cc5fd8c7SJamie Gritton 		/*
2769cc5fd8c7SJamie Gritton 		 * Tell the modules if the last user reference was removed
2770cc5fd8c7SJamie Gritton 		 * (even it sticks around in dying state).
2771cc5fd8c7SJamie Gritton 		 */
2772cc5fd8c7SJamie Gritton 		if (lasturef) {
2773cc5fd8c7SJamie Gritton 			if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
27747f4e7248SJamie Gritton 				if (ref > 1) {
27757f4e7248SJamie Gritton 					sx_slock(&allprison_lock);
27767f4e7248SJamie Gritton 					flags |= PD_LIST_SLOCKED;
27777f4e7248SJamie Gritton 				} else {
2778cc5fd8c7SJamie Gritton 					sx_xlock(&allprison_lock);
2779cc5fd8c7SJamie Gritton 					flags |= PD_LIST_XLOCKED;
2780cc5fd8c7SJamie Gritton 				}
27817f4e7248SJamie Gritton 			}
2782cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2783cc5fd8c7SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2784cc5fd8c7SJamie Gritton 			ref = --pr->pr_ref;
2785cc5fd8c7SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2786cc5fd8c7SJamie Gritton 		}
2787cc5fd8c7SJamie Gritton 
2788cc5fd8c7SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2789cc5fd8c7SJamie Gritton 		if (ref > 0) {
2790b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2791b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2792b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2793b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2794b38ff370SJamie Gritton 			return;
2795b38ff370SJamie Gritton 		}
2796c2cda609SPawel Jakub Dawidek 
2797b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2798b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2799b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2800c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2801b38ff370SJamie Gritton 			}
2802b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2803b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2804b38ff370SJamie Gritton 
2805b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
28060304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
28070304c731SJamie Gritton 		ppr = pr->pr_parent;
28080304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2809b97457e2SJamie Gritton 			tpr->pr_childcount--;
2810c4884ffaSJamie Gritton 		sx_xunlock(&allprison_lock);
28111ba4a712SPawel Jakub Dawidek 
2812679e1390SJamie Gritton #ifdef VIMAGE
28130cb8b6a9SMarko Zec 		if (pr->pr_vnet != ppr->pr_vnet)
2814679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2815679e1390SJamie Gritton #endif
28165050aa86SKonstantin Belousov 		if (pr->pr_root != NULL)
2817b3059e09SRobert Watson 			vrele(pr->pr_root);
2818b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2819413628a7SBjoern A. Zeeb #ifdef INET
2820413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2821413628a7SBjoern A. Zeeb #endif
2822b38ff370SJamie Gritton #ifdef INET6
2823b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2824b38ff370SJamie Gritton #endif
2825b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2826b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2827b38ff370SJamie Gritton 		osd_jail_exit(pr);
2828a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
28294b5c9cf6SEdward Tomasz Napierala 		if (racct_enable)
2830a7ad07bfSEdward Tomasz Napierala 			prison_racct_detach(pr);
2831ec125fbbSEdward Tomasz Napierala #endif
28321ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
28330304c731SJamie Gritton 
28340304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
28350304c731SJamie Gritton 		pr = ppr;
28360304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
2837e6d5cb63SJamie Gritton 		flags = PD_DEREF | PD_DEUREF;
28380304c731SJamie Gritton 	}
2839b3059e09SRobert Watson }
2840b3059e09SRobert Watson 
2841413628a7SBjoern A. Zeeb /*
28420fe74ae6SJamie Gritton  * Set or clear a permission bit in the pr_allow field, passing restrictions
28430fe74ae6SJamie Gritton  * (cleared permission) down to child jails.
28440fe74ae6SJamie Gritton  */
28450fe74ae6SJamie Gritton void
28460fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable)
28470fe74ae6SJamie Gritton {
28480fe74ae6SJamie Gritton 	struct prison *pr;
28490fe74ae6SJamie Gritton 
28500fe74ae6SJamie Gritton 	pr = cred->cr_prison;
28510fe74ae6SJamie Gritton 	sx_slock(&allprison_lock);
28520fe74ae6SJamie Gritton 	mtx_lock(&pr->pr_mtx);
28530fe74ae6SJamie Gritton 	prison_set_allow_locked(pr, flag, enable);
28540fe74ae6SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
28550fe74ae6SJamie Gritton 	sx_sunlock(&allprison_lock);
28560fe74ae6SJamie Gritton }
28570fe74ae6SJamie Gritton 
28580fe74ae6SJamie Gritton static void
28590fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
28600fe74ae6SJamie Gritton {
28610fe74ae6SJamie Gritton 	struct prison *cpr;
28620fe74ae6SJamie Gritton 	int descend;
28630fe74ae6SJamie Gritton 
28640fe74ae6SJamie Gritton 	if (enable != 0)
28650fe74ae6SJamie Gritton 		pr->pr_allow |= flag;
28660fe74ae6SJamie Gritton 	else {
28670fe74ae6SJamie Gritton 		pr->pr_allow &= ~flag;
28680fe74ae6SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
28690fe74ae6SJamie Gritton 			cpr->pr_allow &= ~flag;
28700fe74ae6SJamie Gritton 	}
28710fe74ae6SJamie Gritton }
28720fe74ae6SJamie Gritton 
28730fe74ae6SJamie Gritton /*
2874ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
2875ca04ba64SJamie Gritton  *
2876ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
2877ca04ba64SJamie Gritton  * if not.
2878ca04ba64SJamie Gritton  */
2879ca04ba64SJamie Gritton int
2880ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
2881ca04ba64SJamie Gritton {
28820304c731SJamie Gritton 	struct prison *pr;
2883ca04ba64SJamie Gritton 	int error;
2884ca04ba64SJamie Gritton 
2885ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2886ca04ba64SJamie Gritton 
28870304c731SJamie Gritton 	pr = cred->cr_prison;
2888499650a0SJamie Gritton #ifdef VIMAGE
28896bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
2890de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
28916bb79563SJamie Gritton 		return (0);
2892499650a0SJamie Gritton #endif
28936bb79563SJamie Gritton 
2894ca04ba64SJamie Gritton 	error = 0;
2895ca04ba64SJamie Gritton 	switch (af)
2896ca04ba64SJamie Gritton 	{
2897ca04ba64SJamie Gritton #ifdef INET
2898ca04ba64SJamie Gritton 	case AF_INET:
28990304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
29000304c731SJamie Gritton 		{
29010304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
29020304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
2903ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
29040304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
29050304c731SJamie Gritton 		}
2906ca04ba64SJamie Gritton 		break;
2907ca04ba64SJamie Gritton #endif
2908ca04ba64SJamie Gritton #ifdef INET6
2909ca04ba64SJamie Gritton 	case AF_INET6:
29100304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
29110304c731SJamie Gritton 		{
29120304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
29130304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
2914ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
29150304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
29160304c731SJamie Gritton 		}
2917ca04ba64SJamie Gritton 		break;
2918ca04ba64SJamie Gritton #endif
2919ca04ba64SJamie Gritton 	case AF_LOCAL:
2920ca04ba64SJamie Gritton 	case AF_ROUTE:
2921ca04ba64SJamie Gritton 		break;
2922ca04ba64SJamie Gritton 	default:
29230304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
2924ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
2925ca04ba64SJamie Gritton 	}
2926ca04ba64SJamie Gritton 	return (error);
2927ca04ba64SJamie Gritton }
2928ca04ba64SJamie Gritton 
2929ca04ba64SJamie Gritton /*
2930413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
2931413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
2932413628a7SBjoern A. Zeeb  *
29330304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
29340304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
29350304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
2936413628a7SBjoern A. Zeeb  */
2937413628a7SBjoern A. Zeeb int
2938c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa)
293975c13541SPoul-Henning Kamp {
2940413628a7SBjoern A. Zeeb #ifdef INET
2941c83dda36SAlexander V. Chernikov 	const struct sockaddr_in *sai;
2942413628a7SBjoern A. Zeeb #endif
2943413628a7SBjoern A. Zeeb #ifdef INET6
2944c83dda36SAlexander V. Chernikov 	const struct sockaddr_in6 *sai6;
2945413628a7SBjoern A. Zeeb #endif
2946b89e82ddSJamie Gritton 	int error;
294775c13541SPoul-Henning Kamp 
2948413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2949413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
2950413628a7SBjoern A. Zeeb 
2951de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
2952de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
2953de0bd6f7SBjoern A. Zeeb 		return (0);
2954de0bd6f7SBjoern A. Zeeb #endif
2955de0bd6f7SBjoern A. Zeeb 
2956b89e82ddSJamie Gritton 	error = 0;
2957413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
2958413628a7SBjoern A. Zeeb 	{
2959413628a7SBjoern A. Zeeb #ifdef INET
2960413628a7SBjoern A. Zeeb 	case AF_INET:
2961c83dda36SAlexander V. Chernikov 		sai = (const struct sockaddr_in *)sa;
2962b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
2963413628a7SBjoern A. Zeeb 		break;
2964413628a7SBjoern A. Zeeb #endif
2965413628a7SBjoern A. Zeeb #ifdef INET6
2966413628a7SBjoern A. Zeeb 	case AF_INET6:
2967c83dda36SAlexander V. Chernikov 		sai6 = (const struct sockaddr_in6 *)sa;
2968b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
2969413628a7SBjoern A. Zeeb 		break;
2970413628a7SBjoern A. Zeeb #endif
2971413628a7SBjoern A. Zeeb 	default:
29720304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
2973b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
2974413628a7SBjoern A. Zeeb 	}
2975b89e82ddSJamie Gritton 	return (error);
297675c13541SPoul-Henning Kamp }
297791421ba2SRobert Watson 
297891421ba2SRobert Watson /*
297991421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
298091421ba2SRobert Watson  */
298191421ba2SRobert Watson int
29829ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
298391421ba2SRobert Watson {
298491421ba2SRobert Watson 
29850304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
29860304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
29870304c731SJamie Gritton }
298891421ba2SRobert Watson 
29890304c731SJamie Gritton /*
29900304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
29910304c731SJamie Gritton  */
29920304c731SJamie Gritton int
29930304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
29940304c731SJamie Gritton {
29950304c731SJamie Gritton 
29960304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
29970304c731SJamie Gritton 		if (pr1 == pr2)
29980304c731SJamie Gritton 			return (1);
299991421ba2SRobert Watson 	return (0);
300091421ba2SRobert Watson }
300191421ba2SRobert Watson 
300291421ba2SRobert Watson /*
300376ad42abSJamie Gritton  * Return true if the prison is currently alive.  A prison is alive if it is
300476ad42abSJamie Gritton  * valid and it holds user references.
300576ad42abSJamie Gritton  */
300676ad42abSJamie Gritton bool
300776ad42abSJamie Gritton prison_isalive(struct prison *pr)
300876ad42abSJamie Gritton {
300976ad42abSJamie Gritton 
301076ad42abSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
301176ad42abSJamie Gritton 	if (__predict_false(pr->pr_ref == 0))
301276ad42abSJamie Gritton 		return (false);
301376ad42abSJamie Gritton 	if (__predict_false(pr->pr_uref == 0))
301476ad42abSJamie Gritton 		return (false);
301576ad42abSJamie Gritton 	return (true);
301676ad42abSJamie Gritton }
301776ad42abSJamie Gritton 
301876ad42abSJamie Gritton /*
301976ad42abSJamie Gritton  * Return true if the prison is currently valid.  A prison is valid if it has
302076ad42abSJamie Gritton  * been fully created, and is not being destroyed.  Note that dying prisons
302176ad42abSJamie Gritton  * are still considered valid.
302276ad42abSJamie Gritton  */
302376ad42abSJamie Gritton bool
302476ad42abSJamie Gritton prison_isvalid(struct prison *pr)
302576ad42abSJamie Gritton {
302676ad42abSJamie Gritton 
302776ad42abSJamie Gritton 	mtx_assert(&pr->pr_mtx, MA_OWNED);
302876ad42abSJamie Gritton 	if (__predict_false(pr->pr_ref == 0))
302976ad42abSJamie Gritton 		return (false);
303076ad42abSJamie Gritton 	return (true);
303176ad42abSJamie Gritton }
303276ad42abSJamie Gritton 
303376ad42abSJamie Gritton /*
3034de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3035de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3036de0bd6f7SBjoern A. Zeeb  */
3037de0bd6f7SBjoern A. Zeeb int
3038de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3039de0bd6f7SBjoern A. Zeeb {
3040de0bd6f7SBjoern A. Zeeb 
3041de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3042de0bd6f7SBjoern A. Zeeb 		return (0);
3043de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3044de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3045de0bd6f7SBjoern A. Zeeb 		return (0);
3046de0bd6f7SBjoern A. Zeeb #endif
3047de0bd6f7SBjoern A. Zeeb 
3048de0bd6f7SBjoern A. Zeeb 	return (1);
3049de0bd6f7SBjoern A. Zeeb }
3050de0bd6f7SBjoern A. Zeeb 
3051de0bd6f7SBjoern A. Zeeb /*
30527455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
30539484d0c0SRobert Drehmel  */
3054ad1ff099SRobert Drehmel void
30559ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
30569484d0c0SRobert Drehmel {
305776ca6f88SJamie Gritton 	struct prison *pr;
30589484d0c0SRobert Drehmel 
30597455b100SJamie Gritton 	/*
30607455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
30617455b100SJamie Gritton 	 * system's hostname.
30627455b100SJamie Gritton 	 */
306376ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
306476ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3065c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
306676ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
30679484d0c0SRobert Drehmel }
3068fd7a8150SMike Barcroft 
30697455b100SJamie Gritton void
30707455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
30717455b100SJamie Gritton {
30727455b100SJamie Gritton 
30737455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3074c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
30757455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
30767455b100SJamie Gritton }
30777455b100SJamie Gritton 
30787455b100SJamie Gritton void
30797455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
30807455b100SJamie Gritton {
30817455b100SJamie Gritton 
30827455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3083c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
30847455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
30857455b100SJamie Gritton }
30867455b100SJamie Gritton 
30877455b100SJamie Gritton void
30887455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
30897455b100SJamie Gritton {
30907455b100SJamie Gritton 
30917455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
30927455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
30937455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
30947455b100SJamie Gritton }
30957455b100SJamie Gritton 
30963f8bc99cSKristof Provost void
30973f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len)
30983f8bc99cSKristof Provost {
30993f8bc99cSKristof Provost 
31003f8bc99cSKristof Provost 	mtx_lock(&cred->cr_prison->pr_mtx);
31013f8bc99cSKristof Provost 	strlcpy(name, cred->cr_prison->pr_name, len);
31023f8bc99cSKristof Provost 	mtx_unlock(&cred->cr_prison->pr_mtx);
31033f8bc99cSKristof Provost }
31043f8bc99cSKristof Provost 
3105eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3106eb79e1c7SBjoern A. Zeeb /*
3107eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3108eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3109eb79e1c7SBjoern A. Zeeb  *
3110eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3111eb79e1c7SBjoern A. Zeeb  */
3112eb79e1c7SBjoern A. Zeeb int
3113eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3114eb79e1c7SBjoern A. Zeeb {
3115eb79e1c7SBjoern A. Zeeb 
3116eb79e1c7SBjoern A. Zeeb 	/*
3117eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3118eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3119eb79e1c7SBjoern A. Zeeb 	 */
3120eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3121eb79e1c7SBjoern A. Zeeb }
3122eb79e1c7SBjoern A. Zeeb #endif
3123eb79e1c7SBjoern A. Zeeb 
3124f08df373SRobert Watson /*
3125820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3126820a0de9SPawel Jakub Dawidek  * status of a mount point.
3127820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3128820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3129820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3130f08df373SRobert Watson  */
3131f08df373SRobert Watson int
3132820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3133f08df373SRobert Watson {
3134820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3135820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3136820a0de9SPawel Jakub Dawidek 	size_t len;
3137f08df373SRobert Watson 
3138820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
31390304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
31400304c731SJamie Gritton 		return (0);
3141820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3142820a0de9SPawel Jakub Dawidek 		return (0);
31430304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3144820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3145820a0de9SPawel Jakub Dawidek 	/*
3146820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3147820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3148820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3149820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3150820a0de9SPawel Jakub Dawidek 	 */
3151820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3152820a0de9SPawel Jakub Dawidek 		return (0);
3153820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3154820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3155820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3156820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3157820a0de9SPawel Jakub Dawidek 	/*
3158820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3159820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3160820a0de9SPawel Jakub Dawidek 	 */
3161820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3162820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3163f08df373SRobert Watson 	return (0);
3164f08df373SRobert Watson }
3165820a0de9SPawel Jakub Dawidek 
3166820a0de9SPawel Jakub Dawidek void
3167820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3168820a0de9SPawel Jakub Dawidek {
3169820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3170820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3171820a0de9SPawel Jakub Dawidek 	size_t len;
3172820a0de9SPawel Jakub Dawidek 
3173820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
31740304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
31750304c731SJamie Gritton 		return;
3176820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3177820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3178820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3179820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3180820a0de9SPawel Jakub Dawidek 		return;
3181820a0de9SPawel Jakub Dawidek 	}
3182820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3183820a0de9SPawel Jakub Dawidek 		/*
3184820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3185820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3186820a0de9SPawel Jakub Dawidek 		 */
3187820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3188820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3189820a0de9SPawel Jakub Dawidek 		return;
3190820a0de9SPawel Jakub Dawidek 	}
3191820a0de9SPawel Jakub Dawidek 	/*
3192820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3193820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3194820a0de9SPawel Jakub Dawidek 	 */
3195820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3196820a0de9SPawel Jakub Dawidek 		return;
3197820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3198820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3199820a0de9SPawel Jakub Dawidek 	/*
3200820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3201820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3202820a0de9SPawel Jakub Dawidek 	 */
3203820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3204820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3205820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3206820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3207820a0de9SPawel Jakub Dawidek 	} else {
3208820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3209820a0de9SPawel Jakub Dawidek 	}
3210f08df373SRobert Watson }
3211f08df373SRobert Watson 
3212800c9408SRobert Watson /*
3213800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3214800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3215800c9408SRobert Watson  */
3216800c9408SRobert Watson int
3217800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3218800c9408SRobert Watson {
32190fe74ae6SJamie Gritton 	struct prison *pr;
32200fe74ae6SJamie Gritton 	int error;
3221800c9408SRobert Watson 
32227b2ff0dcSMateusz Guzik 	/*
32237b2ff0dcSMateusz Guzik 	 * Some policies have custom handlers. This routine should not be
32247b2ff0dcSMateusz Guzik 	 * called for them. See priv_check_cred().
32257b2ff0dcSMateusz Guzik 	 */
32267b2ff0dcSMateusz Guzik 	switch (priv) {
3227a459a6cfSMateusz Guzik 	case PRIV_VFS_LOOKUP:
32287b2ff0dcSMateusz Guzik 	case PRIV_VFS_GENERATION:
32297b2ff0dcSMateusz Guzik 		KASSERT(0, ("prison_priv_check instead of a custom handler "
32307b2ff0dcSMateusz Guzik 		    "called for %d\n", priv));
32317b2ff0dcSMateusz Guzik 	}
32327b2ff0dcSMateusz Guzik 
3233800c9408SRobert Watson 	if (!jailed(cred))
3234800c9408SRobert Watson 		return (0);
3235800c9408SRobert Watson 
32366bb79563SJamie Gritton #ifdef VIMAGE
32376bb79563SJamie Gritton 	/*
32386bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
32396bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
32406bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
32416bb79563SJamie Gritton 	 */
32426bb79563SJamie Gritton 	switch (priv) {
32436bb79563SJamie Gritton #ifdef notyet
32446bb79563SJamie Gritton 		/*
32456bb79563SJamie Gritton 		 * NFS-specific privileges.
32466bb79563SJamie Gritton 		 */
32476bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
32486bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
32496bb79563SJamie Gritton #endif
32506bb79563SJamie Gritton 		/*
32516bb79563SJamie Gritton 		 * Network stack privileges.
32526bb79563SJamie Gritton 		 */
32536bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
32546bb79563SJamie Gritton 	case PRIV_NET_GRE:
32556bb79563SJamie Gritton 	case PRIV_NET_BPF:
32566bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
32576bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
32586bb79563SJamie Gritton 	case PRIV_NET_TAP:
32596bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
32606bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
32616bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3262215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
32636bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
32646bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
32656bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
32666bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
3267389474c1SKonstantin Belousov 	case PRIV_NET_SETLANPCP:
32686bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
32696bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
32706bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
32716bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
32726bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
32736bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
32746bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
32756bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
32766bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
32776bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
32786bb79563SJamie Gritton 	case PRIV_NET_LAGG:
32796bb79563SJamie Gritton 	case PRIV_NET_GIF:
32806bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
328135fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
32826bb79563SJamie Gritton 
32836bb79563SJamie Gritton 		/*
32846bb79563SJamie Gritton 		 * 802.11-related privileges.
32856bb79563SJamie Gritton 		 */
3286f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_GETKEY:
3287f7d38a13SAdrian Chadd 	case PRIV_NET80211_VAP_MANAGE:
32886bb79563SJamie Gritton 
32896bb79563SJamie Gritton #ifdef notyet
32906bb79563SJamie Gritton 		/*
32916bb79563SJamie Gritton 		 * ATM privileges.
32926bb79563SJamie Gritton 		 */
32936bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
32946bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
32956bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
32966bb79563SJamie Gritton 	case PRIV_NETATM_SET:
32976bb79563SJamie Gritton 
32986bb79563SJamie Gritton 		/*
32996bb79563SJamie Gritton 		 * Bluetooth privileges.
33006bb79563SJamie Gritton 		 */
33016bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
33026bb79563SJamie Gritton #endif
33036bb79563SJamie Gritton 
33046bb79563SJamie Gritton 		/*
33056bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
33066bb79563SJamie Gritton 		 */
33076bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
33086bb79563SJamie Gritton #ifdef notyet
33096bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
33106bb79563SJamie Gritton #endif
33116bb79563SJamie Gritton 
33126bb79563SJamie Gritton 		/*
33136bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
33146bb79563SJamie Gritton 		 */
33156bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
33166bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
33176bb79563SJamie Gritton 	case PRIV_NETINET_PF:
33186bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
33196bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
33206bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
33216bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
33226bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
33236bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
33246bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
33256bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
33266bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
33276bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
33286bb79563SJamie Gritton 
33296bb79563SJamie Gritton #ifdef notyet
33306bb79563SJamie Gritton 		/*
33316bb79563SJamie Gritton 		 * NCP privileges.
33326bb79563SJamie Gritton 		 */
33336bb79563SJamie Gritton 	case PRIV_NETNCP:
33346bb79563SJamie Gritton 
33356bb79563SJamie Gritton 		/*
33366bb79563SJamie Gritton 		 * SMB privileges.
33376bb79563SJamie Gritton 		 */
33386bb79563SJamie Gritton 	case PRIV_NETSMB:
33396bb79563SJamie Gritton #endif
33406bb79563SJamie Gritton 
33416bb79563SJamie Gritton 	/*
33426bb79563SJamie Gritton 	 * No default: or deny here.
33436bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
33446bb79563SJamie Gritton 	 */
33456bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
33466bb79563SJamie Gritton 			return (0);
33476bb79563SJamie Gritton 	}
33486bb79563SJamie Gritton #endif /* VIMAGE */
33496bb79563SJamie Gritton 
3350800c9408SRobert Watson 	switch (priv) {
3351800c9408SRobert Watson 		/*
3352800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3353800c9408SRobert Watson 		 */
3354800c9408SRobert Watson 	case PRIV_KTRACE:
3355800c9408SRobert Watson 
3356c3c1b5e6SRobert Watson #if 0
3357800c9408SRobert Watson 		/*
3358800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3359800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3360800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3361800c9408SRobert Watson 		 * jail.
3362800c9408SRobert Watson 		 */
3363800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3364800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3365800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3366c3c1b5e6SRobert Watson #endif
3367800c9408SRobert Watson 
3368800c9408SRobert Watson 		/*
3369800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3370800c9408SRobert Watson 		 * credentials in any way they see fit.
3371800c9408SRobert Watson 		 */
3372800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3373800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3374800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3375800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3376800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3377800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3378800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3379800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3380800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3381800c9408SRobert Watson 
3382800c9408SRobert Watson 		/*
3383800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3384800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3385800c9408SRobert Watson 		 */
3386800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3387800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3388800c9408SRobert Watson 
3389800c9408SRobert Watson 		/*
3390800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3391800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3392800c9408SRobert Watson 		 */
3393800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3394800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3395800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3396800c9408SRobert Watson 
3397800c9408SRobert Watson 		/*
3398800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3399800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3400800c9408SRobert Watson 		 */
3401800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3402800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3403800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3404800c9408SRobert Watson 
3405800c9408SRobert Watson 		/*
3406800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3407800c9408SRobert Watson 		 */
3408800c9408SRobert Watson 	case PRIV_IPC_READ:
3409800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3410800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3411800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3412800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3413800c9408SRobert Watson 
3414800c9408SRobert Watson 		/*
34150304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
34160304c731SJamie Gritton 		 */
34170304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
34180304c731SJamie Gritton 	case PRIV_JAIL_SET:
34190304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
34200304c731SJamie Gritton 
34210304c731SJamie Gritton 		/*
3422800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3423800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3424800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3425800c9408SRobert Watson 		 */
3426800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3427413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3428800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3429800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3430800c9408SRobert Watson 
3431800c9408SRobert Watson 		/*
3432800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3433800c9408SRobert Watson 		 * writable.
3434800c9408SRobert Watson 		 */
3435800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3436800c9408SRobert Watson 
3437800c9408SRobert Watson 		/*
3438800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3439e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3440e82d0201SRobert Watson 		 * configuration option.
3441800c9408SRobert Watson 		 */
344295b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
344395b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3444800c9408SRobert Watson 
3445800c9408SRobert Watson 		/*
3446800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3447800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3448800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3449800c9408SRobert Watson 		 * privileges.
3450800c9408SRobert Watson 		 */
3451800c9408SRobert Watson 	case PRIV_VFS_READ:
3452800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3453800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3454800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3455800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3456800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3457800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3458800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3459bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3460800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3461800c9408SRobert Watson 	case PRIV_VFS_LINK:
3462800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3463e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3464800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3465bb56d716SJamie Gritton 
3466bb56d716SJamie Gritton 		/*
3467bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
3468bb56d716SJamie Gritton 		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3469bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3470bb56d716SJamie Gritton 		 */
3471bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3472800c9408SRobert Watson 		return (0);
3473800c9408SRobert Watson 
3474800c9408SRobert Watson 		/*
3475800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3476800c9408SRobert Watson 		 * setting system flags.
3477800c9408SRobert Watson 		 */
3478800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
34790304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3480800c9408SRobert Watson 			return (0);
3481800c9408SRobert Watson 		else
3482800c9408SRobert Watson 			return (EPERM);
3483800c9408SRobert Watson 
3484800c9408SRobert Watson 		/*
3485f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3486f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3487f3a8d2f9SPawel Jakub Dawidek 		 */
3488f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3489f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3490f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
349124b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
34920fe74ae6SJamie Gritton 		pr = cred->cr_prison;
34930fe74ae6SJamie Gritton 		prison_lock(pr);
34940fe74ae6SJamie Gritton 		if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
34950fe74ae6SJamie Gritton 			error = 0;
3496f3a8d2f9SPawel Jakub Dawidek 		else
34970fe74ae6SJamie Gritton 			error = EPERM;
34980fe74ae6SJamie Gritton 		prison_unlock(pr);
34990fe74ae6SJamie Gritton 		return (error);
3500f3a8d2f9SPawel Jakub Dawidek 
3501f3a8d2f9SPawel Jakub Dawidek 		/*
350263619b6dSKyle Evans 		 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
350363619b6dSKyle Evans 		 * policy.  priv_check_cred will not specifically allow it, and
350463619b6dSKyle Evans 		 * we may want a MAC policy to allow it.
350563619b6dSKyle Evans 		 */
350663619b6dSKyle Evans 	case PRIV_VFS_READ_DIR:
350763619b6dSKyle Evans 		return (0);
350863619b6dSKyle Evans 
350963619b6dSKyle Evans 		/*
3510ccd6ac9fSAntoine Brodin 		 * Conditionnaly allow locking (unlocking) physical pages
3511ccd6ac9fSAntoine Brodin 		 * in memory.
3512ccd6ac9fSAntoine Brodin 		 */
3513ccd6ac9fSAntoine Brodin 	case PRIV_VM_MLOCK:
3514ccd6ac9fSAntoine Brodin 	case PRIV_VM_MUNLOCK:
3515ccd6ac9fSAntoine Brodin 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3516ccd6ac9fSAntoine Brodin 			return (0);
3517ccd6ac9fSAntoine Brodin 		else
3518ccd6ac9fSAntoine Brodin 			return (EPERM);
3519ccd6ac9fSAntoine Brodin 
3520ccd6ac9fSAntoine Brodin 		/*
3521e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
3522800c9408SRobert Watson 		 */
3523800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
3524e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3525e28f9b7dSAllan Jude 			return (0);
3526e28f9b7dSAllan Jude 		else
3527e28f9b7dSAllan Jude 			return (EPERM);
3528e28f9b7dSAllan Jude 
3529e28f9b7dSAllan Jude 		/*
3530e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
3531e28f9b7dSAllan Jude 		 */
35324b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3533800c9408SRobert Watson 		return (0);
3534800c9408SRobert Watson 
3535800c9408SRobert Watson 		/*
3536e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
353779ba3952SBjoern A. Zeeb 		 */
353879ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
353979ba3952SBjoern A. Zeeb 		return (0);
354079ba3952SBjoern A. Zeeb 
354179ba3952SBjoern A. Zeeb 		/*
3542800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3543800c9408SRobert Watson 		 */
3544800c9408SRobert Watson 	case PRIV_NETINET_RAW:
35450304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3546800c9408SRobert Watson 			return (0);
3547800c9408SRobert Watson 		else
3548800c9408SRobert Watson 			return (EPERM);
3549800c9408SRobert Watson 
3550800c9408SRobert Watson 		/*
3551800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3552800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3553800c9408SRobert Watson 		 * jail.
3554800c9408SRobert Watson 		 */
3555800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3556800c9408SRobert Watson 		return (0);
3557800c9408SRobert Watson 
35582bfc50bcSEdward Tomasz Napierala 		/*
35592bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
35602bfc50bcSEdward Tomasz Napierala 		 */
35612bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
35622bfc50bcSEdward Tomasz Napierala 		return (0);
35632bfc50bcSEdward Tomasz Napierala 
3564b19d66fdSJamie Gritton 		/*
35654520f617SJamie Gritton 		 * Do not allow a process inside a jail to read the kernel
3566b19d66fdSJamie Gritton 		 * message buffer unless explicitly permitted.
3567b19d66fdSJamie Gritton 		 */
3568b19d66fdSJamie Gritton 	case PRIV_MSGBUF:
3569b19d66fdSJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3570b19d66fdSJamie Gritton 			return (0);
3571b19d66fdSJamie Gritton 		return (EPERM);
3572b19d66fdSJamie Gritton 
3573800c9408SRobert Watson 	default:
3574800c9408SRobert Watson 		/*
3575800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3576800c9408SRobert Watson 		 * includes almost all network privileges, many system
3577800c9408SRobert Watson 		 * configuration privileges.
3578800c9408SRobert Watson 		 */
3579800c9408SRobert Watson 		return (EPERM);
3580800c9408SRobert Watson 	}
3581800c9408SRobert Watson }
3582800c9408SRobert Watson 
35830304c731SJamie Gritton /*
35840304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
35850304c731SJamie Gritton  * if it does not directly follow.
35860304c731SJamie Gritton  */
35870304c731SJamie Gritton 
35880304c731SJamie Gritton char *
35890304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
35900304c731SJamie Gritton {
35910304c731SJamie Gritton 	char *name;
35920304c731SJamie Gritton 
35930304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
35940304c731SJamie Gritton 	if (pr1 == pr2)
35950304c731SJamie Gritton 		return "0";
35960304c731SJamie Gritton 	name = pr2->pr_name;
35970304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
35980304c731SJamie Gritton 		/*
35990304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
36000304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
36010304c731SJamie Gritton 		 * can be counted on - and counted.
36020304c731SJamie Gritton 		 */
36030304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
36040304c731SJamie Gritton 			name = strchr(name, '.') + 1;
36050304c731SJamie Gritton 	}
36060304c731SJamie Gritton 	return (name);
36070304c731SJamie Gritton }
36080304c731SJamie Gritton 
36090304c731SJamie Gritton /*
36100304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
36110304c731SJamie Gritton  * if it does not directly follow.
36120304c731SJamie Gritton  */
36130304c731SJamie Gritton static char *
36140304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
36150304c731SJamie Gritton {
36160304c731SJamie Gritton 	char *path1, *path2;
36170304c731SJamie Gritton 	int len1;
36180304c731SJamie Gritton 
36190304c731SJamie Gritton 	path1 = pr1->pr_path;
36200304c731SJamie Gritton 	path2 = pr2->pr_path;
36210304c731SJamie Gritton 	if (!strcmp(path1, "/"))
36220304c731SJamie Gritton 		return (path2);
36230304c731SJamie Gritton 	len1 = strlen(path1);
36240304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
36250304c731SJamie Gritton 		return (path2);
36260304c731SJamie Gritton 	if (path2[len1] == '\0')
36270304c731SJamie Gritton 		return "/";
36280304c731SJamie Gritton 	if (path2[len1] == '/')
36290304c731SJamie Gritton 		return (path2 + len1);
36300304c731SJamie Gritton 	return (path2);
36310304c731SJamie Gritton }
36320304c731SJamie Gritton 
36330304c731SJamie Gritton /*
36340304c731SJamie Gritton  * Jail-related sysctls.
36350304c731SJamie Gritton  */
36367029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
36370304c731SJamie Gritton     "Jails");
36380304c731SJamie Gritton 
3639fd7a8150SMike Barcroft static int
3640fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3641fd7a8150SMike Barcroft {
3642b38ff370SJamie Gritton 	struct xprison *xp;
36430304c731SJamie Gritton 	struct prison *pr, *cpr;
3644b38ff370SJamie Gritton #ifdef INET
3645b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3646b38ff370SJamie Gritton 	int ip4s = 0;
3647b38ff370SJamie Gritton #endif
3648b38ff370SJamie Gritton #ifdef INET6
36493beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
3650b38ff370SJamie Gritton 	int ip6s = 0;
3651b38ff370SJamie Gritton #endif
36520304c731SJamie Gritton 	int descend, error;
3653fd7a8150SMike Barcroft 
3654b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
36550304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3656b38ff370SJamie Gritton 	error = 0;
3657dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
36580304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
36590304c731SJamie Gritton #if defined(INET) || defined(INET6)
3660b38ff370SJamie Gritton  again:
36610304c731SJamie Gritton #endif
36620304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3663413628a7SBjoern A. Zeeb #ifdef INET
36640304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
36650304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
36660304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
36670304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3668b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3669b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3670b38ff370SJamie Gritton 				goto again;
3671b38ff370SJamie Gritton 			}
36720304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
36730304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3674b38ff370SJamie Gritton 		}
3675413628a7SBjoern A. Zeeb #endif
3676413628a7SBjoern A. Zeeb #ifdef INET6
36770304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
36780304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
36790304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
36800304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3681b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3682b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3683b38ff370SJamie Gritton 				goto again;
3684413628a7SBjoern A. Zeeb 			}
36850304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
36860304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3687b38ff370SJamie Gritton 		}
3688b38ff370SJamie Gritton #endif
368976ad42abSJamie Gritton 		if (!prison_isvalid(cpr)) {
36900304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3691b38ff370SJamie Gritton 			continue;
3692b38ff370SJamie Gritton 		}
3693b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
3694fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
36950304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
369676ad42abSJamie Gritton 		xp->pr_state = prison_isalive(cpr)
3697b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
36980304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3699c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
37000304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3701413628a7SBjoern A. Zeeb #ifdef INET
37020304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
3703413628a7SBjoern A. Zeeb #endif
3704413628a7SBjoern A. Zeeb #ifdef INET6
37050304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
3706413628a7SBjoern A. Zeeb #endif
37070304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3708b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3709b38ff370SJamie Gritton 		if (error)
3710b38ff370SJamie Gritton 			break;
3711413628a7SBjoern A. Zeeb #ifdef INET
3712b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
3713b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
3714b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
3715b38ff370SJamie Gritton 			if (error)
3716b38ff370SJamie Gritton 				break;
3717413628a7SBjoern A. Zeeb 		}
3718413628a7SBjoern A. Zeeb #endif
3719413628a7SBjoern A. Zeeb #ifdef INET6
3720b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
3721b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
3722b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
3723b38ff370SJamie Gritton 			if (error)
3724b38ff370SJamie Gritton 				break;
3725413628a7SBjoern A. Zeeb 		}
3726413628a7SBjoern A. Zeeb #endif
3727fd7a8150SMike Barcroft 	}
3728dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
3729b38ff370SJamie Gritton 	free(xp, M_TEMP);
3730b38ff370SJamie Gritton #ifdef INET
3731b38ff370SJamie Gritton 	free(ip4, M_TEMP);
3732b38ff370SJamie Gritton #endif
3733b38ff370SJamie Gritton #ifdef INET6
3734b38ff370SJamie Gritton 	free(ip6, M_TEMP);
3735b38ff370SJamie Gritton #endif
3736fd7a8150SMike Barcroft 	return (error);
3737fd7a8150SMike Barcroft }
3738fd7a8150SMike Barcroft 
3739f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
3740f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3741f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
3742461167c2SPawel Jakub Dawidek 
3743461167c2SPawel Jakub Dawidek static int
3744461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3745461167c2SPawel Jakub Dawidek {
3746461167c2SPawel Jakub Dawidek 	int error, injail;
3747461167c2SPawel Jakub Dawidek 
3748461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
3749461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3750461167c2SPawel Jakub Dawidek 
3751461167c2SPawel Jakub Dawidek 	return (error);
3752461167c2SPawel Jakub Dawidek }
37530304c731SJamie Gritton 
3754f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3755f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3756f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
3757413628a7SBjoern A. Zeeb 
3758761d2bb5SJamie Gritton static int
3759761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3760761d2bb5SJamie Gritton {
3761761d2bb5SJamie Gritton 	int error, havevnet;
3762761d2bb5SJamie Gritton #ifdef VIMAGE
3763761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
3764761d2bb5SJamie Gritton 
3765761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
3766761d2bb5SJamie Gritton #else
3767761d2bb5SJamie Gritton 	havevnet = 0;
3768761d2bb5SJamie Gritton #endif
3769761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3770761d2bb5SJamie Gritton 
3771761d2bb5SJamie Gritton 	return (error);
3772761d2bb5SJamie Gritton }
3773761d2bb5SJamie Gritton 
3774761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3775761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3776bb8f1623SBjoern A. Zeeb     sysctl_jail_vnet, "I", "Jail owns vnet?");
3777761d2bb5SJamie Gritton 
37780304c731SJamie Gritton #if defined(INET) || defined(INET6)
3779e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
37800304c731SJamie Gritton     &jail_max_af_ips, 0,
3781c542c43eSJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
37820304c731SJamie Gritton #endif
37830304c731SJamie Gritton 
37840304c731SJamie Gritton /*
3785c542c43eSJamie Gritton  * Default parameters for jail(2) compatibility.  For historical reasons,
3786c542c43eSJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
3787c542c43eSJamie Gritton  * just see their own parameters, and can't change them.
37880304c731SJamie Gritton  */
37890304c731SJamie Gritton static int
37900304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
37910304c731SJamie Gritton {
37920fe74ae6SJamie Gritton 	int error, i;
37930304c731SJamie Gritton 
37940304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
37950fe74ae6SJamie Gritton 	if (req->td->td_ucred->cr_prison == &prison0) {
37960fe74ae6SJamie Gritton 		mtx_lock(&prison0.pr_mtx);
37970fe74ae6SJamie Gritton 		i = (jail_default_allow & arg2) != 0;
37980fe74ae6SJamie Gritton 		mtx_unlock(&prison0.pr_mtx);
37990fe74ae6SJamie Gritton 	} else
38000fe74ae6SJamie Gritton 		i = prison_allow(req->td->td_ucred, arg2);
38010fe74ae6SJamie Gritton 
38020304c731SJamie Gritton 	if (arg1 != NULL)
38030304c731SJamie Gritton 		i = !i;
38040304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
3805c542c43eSJamie Gritton 	if (error || !req->newptr)
38060304c731SJamie Gritton 		return (error);
38070304c731SJamie Gritton 	i = i ? arg2 : 0;
38080304c731SJamie Gritton 	if (arg1 != NULL)
38090304c731SJamie Gritton 		i ^= arg2;
38100304c731SJamie Gritton 	/*
38110304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
38120304c731SJamie Gritton 	 * for writing.
38130304c731SJamie Gritton 	 */
38140304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
38150304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
38160304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
38170304c731SJamie Gritton 	return (0);
38180304c731SJamie Gritton }
38190304c731SJamie Gritton 
38200304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3821c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38220304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3823c542c43eSJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
38240304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3825c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38260304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3827c542c43eSJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
38280304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3829c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38300304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3831c542c43eSJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
38320304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3833c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38340304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3835c542c43eSJamie Gritton     "Prison root can create raw sockets (deprecated)");
38360304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3837c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38380304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3839c542c43eSJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
38400304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3841c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38420304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3843c542c43eSJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
38440304c731SJamie Gritton 
38450304c731SJamie Gritton static int
38460304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
38470304c731SJamie Gritton {
38480304c731SJamie Gritton 	struct prison *pr;
38490304c731SJamie Gritton 	int level, error;
38500304c731SJamie Gritton 
38510304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
38520304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
38530304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
3854c542c43eSJamie Gritton 	if (error || !req->newptr)
38550304c731SJamie Gritton 		return (error);
38560304c731SJamie Gritton 	*(int *)arg1 = level;
38570304c731SJamie Gritton 	return (0);
38580304c731SJamie Gritton }
38590304c731SJamie Gritton 
38600304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3861c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3862c542c43eSJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
38630304c731SJamie Gritton     sysctl_jail_default_level, "I",
3864c542c43eSJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
3865c542c43eSJamie Gritton 
38660cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
3867c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
3868c542c43eSJamie Gritton     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
38690cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
3870c542c43eSJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
38710cc207a6SMartin Matuska 
38720304c731SJamie Gritton /*
38730304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
38740304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
38750304c731SJamie Gritton  * to make themselves and their types known.
38760304c731SJamie Gritton  */
38777029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
38780304c731SJamie Gritton     "Jail parameters");
38790304c731SJamie Gritton 
38800304c731SJamie Gritton int
38810304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
38820304c731SJamie Gritton {
38830304c731SJamie Gritton 	int i;
38840304c731SJamie Gritton 	long l;
38850304c731SJamie Gritton 	size_t s;
38860304c731SJamie Gritton 	char numbuf[12];
38870304c731SJamie Gritton 
38880304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
38890304c731SJamie Gritton 	{
38900304c731SJamie Gritton 	case CTLTYPE_LONG:
38910304c731SJamie Gritton 	case CTLTYPE_ULONG:
38920304c731SJamie Gritton 		l = 0;
38930304c731SJamie Gritton #ifdef SCTL_MASK32
38940304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
38950304c731SJamie Gritton #endif
38960304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
38970304c731SJamie Gritton 	case CTLTYPE_INT:
38980304c731SJamie Gritton 	case CTLTYPE_UINT:
38990304c731SJamie Gritton 		i = 0;
39000304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
39010304c731SJamie Gritton 	case CTLTYPE_STRING:
3902e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
39030304c731SJamie Gritton 		return
39040304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
39050304c731SJamie Gritton 	case CTLTYPE_STRUCT:
39060304c731SJamie Gritton 		s = (size_t)arg2;
39070304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
39080304c731SJamie Gritton 	}
39090304c731SJamie Gritton 	return (0);
39100304c731SJamie Gritton }
39110304c731SJamie Gritton 
3912b96bd95bSIan Lepore /*
3913b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
3914b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
3915b96bd95bSIan Lepore  */
39160304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
39170304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
39180304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
39190304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
39200304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
39210304c731SJamie Gritton     "I", "Jail secure level");
3922b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
3923b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
3924b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
3925b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
39260304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
39270304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
39280cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
39290cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
39300304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
39310304c731SJamie Gritton     "B", "Jail persistence");
3932679e1390SJamie Gritton #ifdef VIMAGE
3933679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
39347cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
3935679e1390SJamie Gritton #endif
39360304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
39370304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
39380304c731SJamie Gritton 
3939b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
3940b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
3941b97457e2SJamie Gritton     "I", "Current number of child jails");
3942b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
3943b97457e2SJamie Gritton     "I", "Maximum number of child jails");
3944b97457e2SJamie Gritton 
39457cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
39460304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
39470304c731SJamie Gritton     "Jail hostname");
394876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
394976ca6f88SJamie Gritton     "Jail NIS domainname");
395076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
395176ca6f88SJamie Gritton     "Jail host UUID");
395276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
395376ca6f88SJamie Gritton     "LU", "Jail host ID");
39540304c731SJamie Gritton 
39550304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
39560304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
39570304c731SJamie Gritton 
39580304c731SJamie Gritton #ifdef INET
39592b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
39602b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
39610304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
39620304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
3963592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
3964592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
3965592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
39660304c731SJamie Gritton #endif
39670304c731SJamie Gritton #ifdef INET6
39682b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
39692b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
39700304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
39710304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
3972592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
3973592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
3974592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
39750304c731SJamie Gritton #endif
39760304c731SJamie Gritton 
39770304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
39780304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
39790304c731SJamie Gritton     "B", "Jail may set hostname");
39800304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
39810304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
39820304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
39830304c731SJamie Gritton     "B", "Jail may create raw sockets");
39840304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
39850304c731SJamie Gritton     "B", "Jail may alter system file flags");
39860304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
39870304c731SJamie Gritton     "B", "Jail may set file quotas");
39880304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
39890304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
3990ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
3991ccd6ac9fSAntoine Brodin     "B", "Jail may lock (unlock) physical pages in memory");
3992e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
3993e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
3994b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
3995b19d66fdSJamie Gritton     "B", "Jail may read the kernel message buffer");
3996b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
3997b3079544SJamie Gritton     "B", "Unprivileged processes may use process debugging facilities");
399805e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
399905e1e482SMariusz Zaborski     "B", "Processes in jail with uid 0 have privilege");
40000304c731SJamie Gritton 
4001bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4002bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4003bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
40040e5c6bd4SJamie Gritton 
40050e5c6bd4SJamie Gritton /*
40060a172404SJamie Gritton  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
40070a172404SJamie Gritton  * its associated bit in the pr_allow bitmask, or zero if the parameter was
40080a172404SJamie Gritton  * not created.
40090e5c6bd4SJamie Gritton  */
40100a172404SJamie Gritton unsigned
40110a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
40120a172404SJamie Gritton     const char *descr)
40130e5c6bd4SJamie Gritton {
40140e5c6bd4SJamie Gritton 	struct bool_flags *bf;
40150a172404SJamie Gritton 	struct sysctl_oid *parent;
40160a172404SJamie Gritton 	char *allow_name, *allow_noname, *allowed;
4017c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4018c542c43eSJamie Gritton 	char *descr_deprecated;
4019c542c43eSJamie Gritton #endif
40205d58f959SJamie Gritton 	u_int allow_flag;
40210e5c6bd4SJamie Gritton 
40220a172404SJamie Gritton 	if (prefix
40230a172404SJamie Gritton 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
40240a172404SJamie Gritton 		< 0 ||
40250a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
40260a172404SJamie Gritton 		< 0
40270a172404SJamie Gritton 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
40280a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
40290e5c6bd4SJamie Gritton 		free(allow_name, M_PRISON);
40300a172404SJamie Gritton 		return 0;
40310e5c6bd4SJamie Gritton 	}
40320e5c6bd4SJamie Gritton 
40330e5c6bd4SJamie Gritton 	/*
40340a172404SJamie Gritton 	 * See if this parameter has already beed added, i.e. a module was
40350a172404SJamie Gritton 	 * previously loaded/unloaded.
40360e5c6bd4SJamie Gritton 	 */
40370e5c6bd4SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
40380e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow;
40395d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
40405d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
40410e5c6bd4SJamie Gritton 	     bf++) {
40420e5c6bd4SJamie Gritton 		if (strcmp(bf->name, allow_name) == 0) {
40430a172404SJamie Gritton 			allow_flag = bf->flag;
40440e5c6bd4SJamie Gritton 			goto no_add;
40450e5c6bd4SJamie Gritton 		}
40460e5c6bd4SJamie Gritton 	}
40470e5c6bd4SJamie Gritton 
40480e5c6bd4SJamie Gritton 	/*
40495d58f959SJamie Gritton 	 * Find a free bit in pr_allow_all, failing if there are none
40500e5c6bd4SJamie Gritton 	 * (which shouldn't happen as long as we keep track of how many
40510a172404SJamie Gritton 	 * potential dynamic flags exist).
40520e5c6bd4SJamie Gritton 	 */
40530e5c6bd4SJamie Gritton 	for (allow_flag = 1;; allow_flag <<= 1) {
40540e5c6bd4SJamie Gritton 		if (allow_flag == 0)
40550e5c6bd4SJamie Gritton 			goto no_add;
40565d58f959SJamie Gritton 		if ((pr_allow_all & allow_flag) == 0)
40570e5c6bd4SJamie Gritton 			break;
40580e5c6bd4SJamie Gritton 	}
40590e5c6bd4SJamie Gritton 
40605d58f959SJamie Gritton 	/* Note the parameter in the next open slot in pr_flag_allow. */
40615d58f959SJamie Gritton 	for (bf = pr_flag_allow; ; bf++) {
40620e5c6bd4SJamie Gritton 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
40630e5c6bd4SJamie Gritton 			/* This should never happen, but is not fatal. */
40640a172404SJamie Gritton 			allow_flag = 0;
40650e5c6bd4SJamie Gritton 			goto no_add;
40660e5c6bd4SJamie Gritton 		}
40675d58f959SJamie Gritton 		if (atomic_load_int(&bf->flag) == 0)
40685d58f959SJamie Gritton 			break;
40695d58f959SJamie Gritton 	}
40700e5c6bd4SJamie Gritton 	bf->name = allow_name;
40710e5c6bd4SJamie Gritton 	bf->noname = allow_noname;
40725d58f959SJamie Gritton 	pr_allow_all |= allow_flag;
40735d58f959SJamie Gritton 	/*
40745d58f959SJamie Gritton 	 * prison0 always has permission for the new parameter.
40755d58f959SJamie Gritton 	 * Other jails must have it granted to them.
40765d58f959SJamie Gritton 	 */
40775d58f959SJamie Gritton 	prison0.pr_allow |= allow_flag;
40785d58f959SJamie Gritton 	/* The flag indicates a valid entry, so make sure it is set last. */
40795d58f959SJamie Gritton 	atomic_store_rel_int(&bf->flag, allow_flag);
40800e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
40810e5c6bd4SJamie Gritton 
4082c542c43eSJamie Gritton 	/*
4083c542c43eSJamie Gritton 	 * Create sysctls for the paramter, and the back-compat global
4084c542c43eSJamie Gritton 	 * permission.
4085c542c43eSJamie Gritton 	 */
40860a172404SJamie Gritton 	parent = prefix
40870a172404SJamie Gritton 	    ? SYSCTL_ADD_NODE(NULL,
40880a172404SJamie Gritton 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
40897029da5cSPawel Biernacki 		  OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
40900a172404SJamie Gritton 	    : &sysctl___security_jail_param_allow;
40910a172404SJamie Gritton 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
40920a172404SJamie Gritton 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
40930e5c6bd4SJamie Gritton 	    NULL, 0, sysctl_jail_param, "B", descr);
40940a172404SJamie Gritton 	if ((prefix
40950a172404SJamie Gritton 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
40960a172404SJamie Gritton 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4097c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4098c542c43eSJamie Gritton 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4099c542c43eSJamie Gritton 		    descr);
4100c542c43eSJamie Gritton #endif
41010e5c6bd4SJamie Gritton 		(void)SYSCTL_ADD_PROC(NULL,
41020a172404SJamie Gritton 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4103c542c43eSJamie Gritton 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4104c542c43eSJamie Gritton 		    sysctl_jail_default_allow, "I", descr_deprecated);
4105c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
4106c542c43eSJamie Gritton 		free(descr_deprecated, M_TEMP);
4107c542c43eSJamie Gritton #endif
41080a172404SJamie Gritton 		free(allowed, M_TEMP);
41090e5c6bd4SJamie Gritton 	}
41100a172404SJamie Gritton 	return allow_flag;
41110e5c6bd4SJamie Gritton 
41120e5c6bd4SJamie Gritton  no_add:
41130e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
41140e5c6bd4SJamie Gritton 	free(allow_name, M_PRISON);
41150e5c6bd4SJamie Gritton 	free(allow_noname, M_PRISON);
41160a172404SJamie Gritton 	return allow_flag;
41170a172404SJamie Gritton }
41180a172404SJamie Gritton 
41190a172404SJamie Gritton /*
41200a172404SJamie Gritton  * The VFS system will register jail-aware filesystems here.  They each get
41210a172404SJamie Gritton  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
41220a172404SJamie Gritton  * attempts to mount.
41230a172404SJamie Gritton  */
41240a172404SJamie Gritton void
41250a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp)
41260a172404SJamie Gritton {
41270a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR
41280a172404SJamie Gritton 
41290a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
41300a172404SJamie Gritton 	    NULL, NULL);
41310a172404SJamie Gritton #else
41320a172404SJamie Gritton 	char *descr;
41330a172404SJamie Gritton 
41340a172404SJamie Gritton 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
41350a172404SJamie Gritton 	    vfsp->vfc_name);
41360a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
41370a172404SJamie Gritton 	    NULL, descr);
41380a172404SJamie Gritton 	free(descr, M_TEMP);
41390a172404SJamie Gritton #endif
41400e5c6bd4SJamie Gritton }
4141bf3db8aaSMartin Matuska 
41424b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4143097055e2SEdward Tomasz Napierala void
4144097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
414515db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
414615db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
4147097055e2SEdward Tomasz Napierala {
4148a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4149097055e2SEdward Tomasz Napierala 
41504b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
41514b5c9cf6SEdward Tomasz Napierala 
4152097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
415315db3c07SEdward Tomasz Napierala 	if (pre != NULL)
415415db3c07SEdward Tomasz Napierala 		(pre)();
4155a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4156a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
415715db3c07SEdward Tomasz Napierala 	if (post != NULL)
415815db3c07SEdward Tomasz Napierala 		(post)();
4159097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4160097055e2SEdward Tomasz Napierala }
41610304c731SJamie Gritton 
4162a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4163a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4164a7ad07bfSEdward Tomasz Napierala {
4165a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4166a7ad07bfSEdward Tomasz Napierala 
41674b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4168a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4169a7ad07bfSEdward Tomasz Napierala 
4170a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4171a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4172a7ad07bfSEdward Tomasz Napierala 
4173a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4174a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4175a7ad07bfSEdward Tomasz Napierala 			continue;
4176a7ad07bfSEdward Tomasz Napierala 
4177a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4178a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4179a7ad07bfSEdward Tomasz Napierala 		return (prr);
4180a7ad07bfSEdward Tomasz Napierala 	}
4181a7ad07bfSEdward Tomasz Napierala 
4182a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4183a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4184a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4185a7ad07bfSEdward Tomasz Napierala 
4186a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4187a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4188a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4189a7ad07bfSEdward Tomasz Napierala 
4190a7ad07bfSEdward Tomasz Napierala 	return (prr);
4191a7ad07bfSEdward Tomasz Napierala }
4192a7ad07bfSEdward Tomasz Napierala 
4193a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4194a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4195a7ad07bfSEdward Tomasz Napierala {
4196a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4197a7ad07bfSEdward Tomasz Napierala 
41984b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
41994b5c9cf6SEdward Tomasz Napierala 
4200a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4201a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4202a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4203a7ad07bfSEdward Tomasz Napierala 	return (prr);
4204a7ad07bfSEdward Tomasz Napierala }
4205a7ad07bfSEdward Tomasz Napierala 
4206a7ad07bfSEdward Tomasz Napierala void
4207a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4208a7ad07bfSEdward Tomasz Napierala {
4209a7ad07bfSEdward Tomasz Napierala 
42104b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
42114b5c9cf6SEdward Tomasz Napierala 
4212a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4213a7ad07bfSEdward Tomasz Napierala }
4214a7ad07bfSEdward Tomasz Napierala 
4215c34bbd2aSEdward Tomasz Napierala static void
4216c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4217c34bbd2aSEdward Tomasz Napierala {
4218c34bbd2aSEdward Tomasz Napierala 
42194b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4220c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4221c34bbd2aSEdward Tomasz Napierala 
4222c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4223c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4224c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4225c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4226c34bbd2aSEdward Tomasz Napierala 	}
4227c34bbd2aSEdward Tomasz Napierala }
4228c34bbd2aSEdward Tomasz Napierala 
4229a7ad07bfSEdward Tomasz Napierala void
4230a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4231a7ad07bfSEdward Tomasz Napierala {
4232a7ad07bfSEdward Tomasz Napierala 
42334b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4234c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4235c34bbd2aSEdward Tomasz Napierala 
42366ff4688bSMateusz Guzik 	if (refcount_release_if_not_last(&prr->prr_refcount))
4237a7ad07bfSEdward Tomasz Napierala 		return;
4238a7ad07bfSEdward Tomasz Napierala 
4239a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4240c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4241a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4242a7ad07bfSEdward Tomasz Napierala }
4243a7ad07bfSEdward Tomasz Napierala 
4244a7ad07bfSEdward Tomasz Napierala static void
4245a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4246a7ad07bfSEdward Tomasz Napierala {
4247a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4248a7ad07bfSEdward Tomasz Napierala 
42494b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4250c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4251c34bbd2aSEdward Tomasz Napierala 
4252a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4253a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4254a7ad07bfSEdward Tomasz Napierala 
4255a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4256a7ad07bfSEdward Tomasz Napierala }
4257a7ad07bfSEdward Tomasz Napierala 
4258c34bbd2aSEdward Tomasz Napierala /*
4259c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4260c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4261c34bbd2aSEdward Tomasz Napierala  */
4262c34bbd2aSEdward Tomasz Napierala static void
4263c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4264c34bbd2aSEdward Tomasz Napierala {
4265dbadb015SKonstantin Belousov #ifdef RCTL
4266c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4267c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4268dbadb015SKonstantin Belousov #endif
4269c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4270c34bbd2aSEdward Tomasz Napierala 
42714b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
42724b5c9cf6SEdward Tomasz Napierala 
4273c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4274c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4275c34bbd2aSEdward Tomasz Napierala 
4276e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4277e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4278e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4279c34bbd2aSEdward Tomasz Napierala 		return;
4280e30345e7SEdward Tomasz Napierala 	}
4281c34bbd2aSEdward Tomasz Napierala 
4282c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4283c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4284c34bbd2aSEdward Tomasz Napierala 
4285c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4286c34bbd2aSEdward Tomasz Napierala 
4287c34bbd2aSEdward Tomasz Napierala 	/*
4288c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4289c34bbd2aSEdward Tomasz Napierala 	 */
4290c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4291c34bbd2aSEdward Tomasz Napierala 
4292f87beb93SAndriy Gapon #ifdef RCTL
4293c34bbd2aSEdward Tomasz Napierala 	/*
4294c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4295c34bbd2aSEdward Tomasz Napierala 	 */
4296c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4297c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4298c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4299c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4300f87beb93SAndriy Gapon 		rctl_proc_ucred_changed(p, cred);
4301c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4302c34bbd2aSEdward Tomasz Napierala 	}
4303f87beb93SAndriy Gapon #endif
4304c34bbd2aSEdward Tomasz Napierala 
4305c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4306c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4307c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4308c34bbd2aSEdward Tomasz Napierala }
4309c34bbd2aSEdward Tomasz Napierala 
4310a7ad07bfSEdward Tomasz Napierala static void
4311a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4312a7ad07bfSEdward Tomasz Napierala {
4313c34bbd2aSEdward Tomasz Napierala 
43144b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4315c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4316c34bbd2aSEdward Tomasz Napierala 
4317af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4318af3c786cSMateusz Guzik 		return;
4319a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4320a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4321a7ad07bfSEdward Tomasz Napierala }
4322a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4323a7ad07bfSEdward Tomasz Napierala 
4324413628a7SBjoern A. Zeeb #ifdef DDB
4325b38ff370SJamie Gritton 
4326b38ff370SJamie Gritton static void
4327b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4328413628a7SBjoern A. Zeeb {
4329672756aaSJamie Gritton 	struct bool_flags *bf;
4330672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4331b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4332b38ff370SJamie Gritton 	int ii;
4333413628a7SBjoern A. Zeeb #endif
4334672756aaSJamie Gritton 	unsigned f;
43358144690aSEric van Gyzen #ifdef INET
43368144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
43378144690aSEric van Gyzen #endif
4338413628a7SBjoern A. Zeeb #ifdef INET6
4339413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4340413628a7SBjoern A. Zeeb #endif
4341413628a7SBjoern A. Zeeb 
4342b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4343b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4344b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
43450304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4346b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4347b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4348b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4349b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4350b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4351679e1390SJamie Gritton #ifdef VIMAGE
4352679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4353679e1390SJamie Gritton #endif
4354b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4355b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
43560cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4357fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4358fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
43590304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
43600304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4361fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
4362672756aaSJamie Gritton 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4363672756aaSJamie Gritton 		if (pr->pr_flags & bf->flag)
4364672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4365672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
4366672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4367672756aaSJamie Gritton 	     jsf++) {
4368672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
4369672756aaSJamie Gritton 		db_printf(" %-16s= %s\n", jsf->name,
4370672756aaSJamie Gritton 		    (f != 0 && f == jsf->disable) ? "disable"
4371672756aaSJamie Gritton 		    : (f == jsf->new) ? "new"
43727cbf7213SJamie Gritton 		    : "inherit");
43737cbf7213SJamie Gritton 	}
4374fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
4375672756aaSJamie Gritton 	for (bf = pr_flag_allow;
43765d58f959SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) &&
43775d58f959SJamie Gritton 		atomic_load_int(&bf->flag) != 0;
4378672756aaSJamie Gritton 	     bf++)
4379672756aaSJamie Gritton 		if (pr->pr_allow & bf->flag)
4380672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4381b38ff370SJamie Gritton 	db_printf("\n");
43820304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4383c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4384c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4385c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
438676ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4387413628a7SBjoern A. Zeeb #ifdef INET
4388b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4389b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4390b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4391fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
43928144690aSEric van Gyzen 		    inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4393413628a7SBjoern A. Zeeb #endif
4394413628a7SBjoern A. Zeeb #ifdef INET6
4395b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4396b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4397b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4398fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4399b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4400b38ff370SJamie Gritton #endif
4401b38ff370SJamie Gritton }
4402b38ff370SJamie Gritton 
4403b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4404b38ff370SJamie Gritton {
4405b38ff370SJamie Gritton 	struct prison *pr;
4406b38ff370SJamie Gritton 
4407b38ff370SJamie Gritton 	if (!have_addr) {
44080304c731SJamie Gritton 		/*
44090304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
44100304c731SJamie Gritton 		 * listed.
44110304c731SJamie Gritton 		 */
44120304c731SJamie Gritton 		db_show_prison(&prison0);
44130304c731SJamie Gritton 		if (!db_pager_quit) {
4414b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4415b38ff370SJamie Gritton 				db_show_prison(pr);
4416413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4417413628a7SBjoern A. Zeeb 					break;
4418413628a7SBjoern A. Zeeb 			}
44190304c731SJamie Gritton 		}
4420b38ff370SJamie Gritton 		return;
4421413628a7SBjoern A. Zeeb 	}
4422b38ff370SJamie Gritton 
44230304c731SJamie Gritton 	if (addr == 0)
44240304c731SJamie Gritton 		pr = &prison0;
44250304c731SJamie Gritton 	else {
4426b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4427b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4428b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4429b38ff370SJamie Gritton 				break;
4430b38ff370SJamie Gritton 		if (pr == NULL)
4431b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4432b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4433b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4434b38ff370SJamie Gritton 					break;
4435b38ff370SJamie Gritton 		if (pr == NULL)
4436b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4437b38ff370SJamie Gritton 			pr = (struct prison *)addr;
44380304c731SJamie Gritton 	}
4439b38ff370SJamie Gritton 	db_show_prison(pr);
4440b38ff370SJamie Gritton }
4441b38ff370SJamie Gritton 
4442413628a7SBjoern A. Zeeb #endif /* DDB */
4443