xref: /freebsd/sys/kern/kern_jail.c (revision b19d66fd5a21c3045c6d4c395dac73d1585f77ed)
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>
5101137630SRobert Watson #include <sys/lock.h>
5201137630SRobert Watson #include <sys/mutex.h>
53097055e2SEdward Tomasz Napierala #include <sys/racct.h>
54f87beb93SAndriy Gapon #include <sys/rctl.h>
55a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
56dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5776ca6f88SJamie Gritton #include <sys/sysent.h>
58fd7a8150SMike Barcroft #include <sys/namei.h>
59820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
60fd7a8150SMike Barcroft #include <sys/queue.h>
6175c13541SPoul-Henning Kamp #include <sys/socket.h>
62fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6383f1e257SRobert Watson #include <sys/sysctl.h>
64fd7a8150SMike Barcroft #include <sys/vnode.h>
65530c0060SRobert Watson 
6675c13541SPoul-Henning Kamp #include <net/if.h>
67530c0060SRobert Watson #include <net/vnet.h>
68530c0060SRobert Watson 
6975c13541SPoul-Henning Kamp #include <netinet/in.h>
70530c0060SRobert Watson 
71413628a7SBjoern A. Zeeb #ifdef DDB
72413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
73413628a7SBjoern A. Zeeb #endif /* DDB */
7475c13541SPoul-Henning Kamp 
75aed55708SRobert Watson #include <security/mac/mac_framework.h>
76aed55708SRobert Watson 
778986e3a0SJamie Gritton #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
788986e3a0SJamie Gritton 
7975c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
80d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8175c13541SPoul-Henning Kamp 
82592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
83592bcae8SBjoern A. Zeeb #ifdef INET
84592bcae8SBjoern A. Zeeb #ifdef INET6
85592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
86592bcae8SBjoern A. Zeeb #else
87592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
88592bcae8SBjoern A. Zeeb #endif
89592bcae8SBjoern A. Zeeb #else /* !INET */
90592bcae8SBjoern A. Zeeb #ifdef INET6
91592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
92592bcae8SBjoern A. Zeeb #else
93592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
94592bcae8SBjoern A. Zeeb #endif
95592bcae8SBjoern A. Zeeb #endif
96592bcae8SBjoern A. Zeeb 
970304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
980304c731SJamie Gritton struct prison prison0 = {
990304c731SJamie Gritton 	.pr_id		= 0,
1000304c731SJamie Gritton 	.pr_name	= "0",
1010304c731SJamie Gritton 	.pr_ref		= 1,
1020304c731SJamie Gritton 	.pr_uref	= 1,
1030304c731SJamie Gritton 	.pr_path	= "/",
1040304c731SJamie Gritton 	.pr_securelevel	= -1,
1050cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
106b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1078986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
10813e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
109eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
110592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
111eb79e1c7SBjoern A. Zeeb #else
112592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
113eb79e1c7SBjoern A. Zeeb #endif
1140e5c6bd4SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL_STATIC,
1150304c731SJamie Gritton };
1160304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11783f1e257SRobert Watson 
118672756aaSJamie Gritton struct bool_flags {
119672756aaSJamie Gritton 	const char	*name;
120672756aaSJamie Gritton 	const char	*noname;
121672756aaSJamie Gritton 	unsigned	 flag;
122672756aaSJamie Gritton };
123672756aaSJamie Gritton struct jailsys_flags {
124672756aaSJamie Gritton 	const char	*name;
125672756aaSJamie Gritton 	unsigned	 disable;
126672756aaSJamie Gritton 	unsigned	 new;
127672756aaSJamie Gritton };
128672756aaSJamie Gritton 
129a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
130dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
131b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
132b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
133a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1342110d913SXin LI int	lastprid = 0;
135fd7a8150SMike Barcroft 
136b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
137b3059e09SRobert Watson static void prison_complete(void *context, int pending);
138b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1390304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1400304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
141a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
142a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
143c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
144a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
145a7ad07bfSEdward Tomasz Napierala #endif
146fd7a8150SMike Barcroft 
147b38ff370SJamie Gritton /* Flags for prison_deref */
148b38ff370SJamie Gritton #define	PD_DEREF	0x01
149b38ff370SJamie Gritton #define	PD_DEUREF	0x02
150b38ff370SJamie Gritton #define	PD_LOCKED	0x04
151b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
152b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
153fd7a8150SMike Barcroft 
1540304c731SJamie Gritton /*
1555cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1565cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1575cc70397SBjoern A. Zeeb  * terminating entry.
1580304c731SJamie Gritton  */
159672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = {
160672756aaSJamie Gritton 	{"persist", "nopersist", PR_PERSIST},
161592bcae8SBjoern A. Zeeb #ifdef INET
162672756aaSJamie Gritton 	{"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
163592bcae8SBjoern A. Zeeb #endif
164592bcae8SBjoern A. Zeeb #ifdef INET6
165672756aaSJamie Gritton 	{"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
166592bcae8SBjoern A. Zeeb #endif
1670304c731SJamie Gritton };
168672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
1690304c731SJamie Gritton 
170672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = {
1717cbf7213SJamie Gritton 	{"host", 0, PR_HOST},
1727cbf7213SJamie Gritton #ifdef VIMAGE
1737cbf7213SJamie Gritton 	{"vnet", 0, PR_VNET},
1747cbf7213SJamie Gritton #endif
1750304c731SJamie Gritton #ifdef INET
1766a3f2779SJamie Gritton 	{"ip4", PR_IP4_USER, PR_IP4_USER},
1770304c731SJamie Gritton #endif
1780304c731SJamie Gritton #ifdef INET6
1796a3f2779SJamie Gritton 	{"ip6", PR_IP6_USER, PR_IP6_USER},
180679e1390SJamie Gritton #endif
1810304c731SJamie Gritton };
1825cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1830304c731SJamie Gritton 
1840e5c6bd4SJamie Gritton /* Make this array full-size so dynamic parameters can be added. */
1850e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
186672756aaSJamie Gritton 	{"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
187672756aaSJamie Gritton 	{"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
188672756aaSJamie Gritton 	{"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
189672756aaSJamie Gritton 	{"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
190672756aaSJamie Gritton 	{"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
191672756aaSJamie Gritton 	{"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
192672756aaSJamie Gritton 	{"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
193ccd6ac9fSAntoine Brodin 	{"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
194672756aaSJamie Gritton 	{"allow.reserved_ports", "allow.noreserved_ports",
195672756aaSJamie Gritton 	 PR_ALLOW_RESERVED_PORTS},
196*b19d66fdSJamie Gritton 	{"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
1970304c731SJamie Gritton };
198672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
1990304c731SJamie Gritton 
200e28f9b7dSAllan Jude #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | PR_ALLOW_RESERVED_PORTS)
20142bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
202bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2030304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
20442bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2050cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2060304c731SJamie Gritton #if defined(INET) || defined(INET6)
207e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2080304c731SJamie Gritton #endif
2090304c731SJamie Gritton 
210b96bd95bSIan Lepore /*
211b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
212b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
213b96bd95bSIan Lepore  */
214b96bd95bSIan Lepore void
215b96bd95bSIan Lepore prison0_init(void)
216b96bd95bSIan Lepore {
217b96bd95bSIan Lepore 
218b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
219b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
220b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
221b96bd95bSIan Lepore }
222b96bd95bSIan Lepore 
223116734c4SMatthew Dillon /*
2249ddb7954SMike Barcroft  * struct jail_args {
2259ddb7954SMike Barcroft  *	struct jail *jail;
2269ddb7954SMike Barcroft  * };
227116734c4SMatthew Dillon  */
22875c13541SPoul-Henning Kamp int
229c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap)
23075c13541SPoul-Henning Kamp {
231413628a7SBjoern A. Zeeb 	uint32_t version;
232413628a7SBjoern A. Zeeb 	int error;
2330304c731SJamie Gritton 	struct jail j;
234413628a7SBjoern A. Zeeb 
235413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
236413628a7SBjoern A. Zeeb 	if (error)
237413628a7SBjoern A. Zeeb 		return (error);
238413628a7SBjoern A. Zeeb 
239413628a7SBjoern A. Zeeb 	switch (version) {
240413628a7SBjoern A. Zeeb 	case 0:
241413628a7SBjoern A. Zeeb 	{
242413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
243413628a7SBjoern A. Zeeb 
2440304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2450304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
246413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
247413628a7SBjoern A. Zeeb 		if (error)
248413628a7SBjoern A. Zeeb 			return (error);
2490304c731SJamie Gritton 		j.version = j0.version;
2500304c731SJamie Gritton 		j.path = j0.path;
2510304c731SJamie Gritton 		j.hostname = j0.hostname;
252b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
253413628a7SBjoern A. Zeeb 		break;
254413628a7SBjoern A. Zeeb 	}
255413628a7SBjoern A. Zeeb 
256413628a7SBjoern A. Zeeb 	case 1:
257413628a7SBjoern A. Zeeb 		/*
258413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
259413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
260413628a7SBjoern A. Zeeb 		 */
261413628a7SBjoern A. Zeeb 		return (EINVAL);
262413628a7SBjoern A. Zeeb 
263413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
264413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
265413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
266413628a7SBjoern A. Zeeb 		if (error)
267413628a7SBjoern A. Zeeb 			return (error);
2680304c731SJamie Gritton 		break;
2690304c731SJamie Gritton 
2700304c731SJamie Gritton 	default:
2710304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
2720304c731SJamie Gritton 		return (EINVAL);
2730304c731SJamie Gritton 	}
274c542c43eSJamie Gritton 	return (kern_jail(td, &j));
2750304c731SJamie Gritton }
2760304c731SJamie Gritton 
2770304c731SJamie Gritton int
278c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j)
2790304c731SJamie Gritton {
280c542c43eSJamie Gritton 	struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
281e92e0574SJamie Gritton #ifdef INET
282e92e0574SJamie Gritton 			    + 1
283e92e0574SJamie Gritton #endif
284e92e0574SJamie Gritton #ifdef INET6
285e92e0574SJamie Gritton 			    + 1
286e92e0574SJamie Gritton #endif
287e92e0574SJamie Gritton 			    )];
2880304c731SJamie Gritton 	struct uio opt;
2890304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
290672756aaSJamie Gritton 	struct bool_flags *bf;
2910304c731SJamie Gritton #ifdef INET
292e92e0574SJamie Gritton 	uint32_t ip4s;
2930304c731SJamie Gritton 	struct in_addr *u_ip4;
2940304c731SJamie Gritton #endif
2950304c731SJamie Gritton #ifdef INET6
2960304c731SJamie Gritton 	struct in6_addr *u_ip6;
2970304c731SJamie Gritton #endif
2980304c731SJamie Gritton 	size_t tmplen;
299c542c43eSJamie Gritton 	int error, enforce_statfs;
3000304c731SJamie Gritton 
3010304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3020304c731SJamie Gritton 	opt.uio_iov = optiov;
3030304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3040304c731SJamie Gritton 	opt.uio_offset = -1;
3050304c731SJamie Gritton 	opt.uio_resid = -1;
3060304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3070304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3080304c731SJamie Gritton 	opt.uio_td = td;
3090304c731SJamie Gritton 
3100304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3110304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
312672756aaSJamie Gritton 		for (bf = pr_flag_allow;
3130e5c6bd4SJamie Gritton 		     bf < pr_flag_allow + nitems(pr_flag_allow) &&
3140e5c6bd4SJamie Gritton 			bf->flag != 0;
315672756aaSJamie Gritton 		     bf++) {
316672756aaSJamie Gritton 			optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
317672756aaSJamie Gritton 			    (jail_default_allow & bf->flag)
318672756aaSJamie Gritton 			    ? bf->name : bf->noname);
3190304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3200304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3210304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3220304c731SJamie Gritton 		}
3230304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3240304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3250304c731SJamie Gritton 		opt.uio_iovcnt++;
3260304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3270304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3280304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3290304c731SJamie Gritton 		opt.uio_iovcnt++;
3300304c731SJamie Gritton 	}
3310304c731SJamie Gritton 
332b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
333b38ff370SJamie Gritton #ifdef INET
3340304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3350304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
336b38ff370SJamie Gritton 		return (EINVAL);
3370304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
338b38ff370SJamie Gritton #else
3390304c731SJamie Gritton 	if (j->ip4s > 0)
340b38ff370SJamie Gritton 		return (EINVAL);
341b38ff370SJamie Gritton #endif
342b38ff370SJamie Gritton #ifdef INET6
3430304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
344b38ff370SJamie Gritton 		return (EINVAL);
3450304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
346b38ff370SJamie Gritton #else
3470304c731SJamie Gritton 	if (j->ip6s > 0)
348b38ff370SJamie Gritton 		return (EINVAL);
349b38ff370SJamie Gritton #endif
350b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
351b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
352b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
353b38ff370SJamie Gritton #ifdef INET
354b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
355b38ff370SJamie Gritton #endif
356b38ff370SJamie Gritton #ifdef INET6
357b38ff370SJamie Gritton #ifdef INET
3580304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
359b38ff370SJamie Gritton #else
360b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
361b38ff370SJamie Gritton #endif
362b38ff370SJamie Gritton #endif
3630304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
3640304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
3650304c731SJamie Gritton 	opt.uio_iovcnt++;
3660304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
3670304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
3680304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
369b38ff370SJamie Gritton 	if (error) {
370b38ff370SJamie Gritton 		free(u_path, M_TEMP);
371b38ff370SJamie Gritton 		return (error);
372b38ff370SJamie Gritton 	}
3730304c731SJamie Gritton 	opt.uio_iovcnt++;
3740304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
3750304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
3760304c731SJamie Gritton 	opt.uio_iovcnt++;
3770304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
3780304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
3790304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
380b38ff370SJamie Gritton 	if (error) {
381b38ff370SJamie Gritton 		free(u_path, M_TEMP);
382b38ff370SJamie Gritton 		return (error);
383b38ff370SJamie Gritton 	}
3840304c731SJamie Gritton 	opt.uio_iovcnt++;
3850304c731SJamie Gritton 	if (j->jailname != NULL) {
386b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
387b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
388b38ff370SJamie Gritton 		opt.uio_iovcnt++;
389b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
3900304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
391b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
392b38ff370SJamie Gritton 		if (error) {
393b38ff370SJamie Gritton 			free(u_path, M_TEMP);
394b38ff370SJamie Gritton 			return (error);
395b38ff370SJamie Gritton 		}
396b38ff370SJamie Gritton 		opt.uio_iovcnt++;
397b38ff370SJamie Gritton 	}
398b38ff370SJamie Gritton #ifdef INET
399b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
400b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
401b38ff370SJamie Gritton 	opt.uio_iovcnt++;
402b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4030304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4040304c731SJamie Gritton 	if (j->version == 0)
4050304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4060304c731SJamie Gritton 	else {
4070304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
408b38ff370SJamie Gritton 		if (error) {
409b38ff370SJamie Gritton 			free(u_path, M_TEMP);
410b38ff370SJamie Gritton 			return (error);
411b38ff370SJamie Gritton 		}
4120304c731SJamie Gritton 	}
413b38ff370SJamie Gritton 	opt.uio_iovcnt++;
414b38ff370SJamie Gritton #endif
415b38ff370SJamie Gritton #ifdef INET6
416b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
417b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
418b38ff370SJamie Gritton 	opt.uio_iovcnt++;
419b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4200304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4210304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
422b38ff370SJamie Gritton 	if (error) {
423b38ff370SJamie Gritton 		free(u_path, M_TEMP);
424b38ff370SJamie Gritton 		return (error);
425b38ff370SJamie Gritton 	}
426b38ff370SJamie Gritton 	opt.uio_iovcnt++;
427b38ff370SJamie Gritton #endif
42802abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
4290304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
430b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
431b38ff370SJamie Gritton 	free(u_path, M_TEMP);
432b38ff370SJamie Gritton 	return (error);
433b38ff370SJamie Gritton }
434b38ff370SJamie Gritton 
4350304c731SJamie Gritton 
436b38ff370SJamie Gritton /*
437b38ff370SJamie Gritton  * struct jail_set_args {
438b38ff370SJamie Gritton  *	struct iovec *iovp;
439b38ff370SJamie Gritton  *	unsigned int iovcnt;
440b38ff370SJamie Gritton  *	int flags;
441b38ff370SJamie Gritton  * };
442b38ff370SJamie Gritton  */
443b38ff370SJamie Gritton int
4448451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
445b38ff370SJamie Gritton {
446b38ff370SJamie Gritton 	struct uio *auio;
447b38ff370SJamie Gritton 	int error;
448b38ff370SJamie Gritton 
449b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
450b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
451b38ff370SJamie Gritton 		return (EINVAL);
452b38ff370SJamie Gritton 
453b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
454b38ff370SJamie Gritton 	if (error)
455b38ff370SJamie Gritton 		return (error);
456b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
457b38ff370SJamie Gritton 	free(auio, M_IOV);
458b38ff370SJamie Gritton 	return (error);
459413628a7SBjoern A. Zeeb }
460413628a7SBjoern A. Zeeb 
461413628a7SBjoern A. Zeeb int
462b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
463413628a7SBjoern A. Zeeb {
464fd7a8150SMike Barcroft 	struct nameidata nd;
465b38ff370SJamie Gritton #ifdef INET
466b38ff370SJamie Gritton 	struct in_addr *ip4;
467413628a7SBjoern A. Zeeb #endif
468b38ff370SJamie Gritton #ifdef INET6
469b38ff370SJamie Gritton 	struct in6_addr *ip6;
470b38ff370SJamie Gritton #endif
471b38ff370SJamie Gritton 	struct vfsopt *opt;
472b38ff370SJamie Gritton 	struct vfsoptlist *opts;
47357aea6dfSBjoern A. Zeeb 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
474b38ff370SJamie Gritton 	struct vnode *root;
475babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
476b96bd95bSIan Lepore 	char *g_path, *osrelstr;
477672756aaSJamie Gritton 	struct bool_flags *bf;
478672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4790304c731SJamie Gritton #if defined(INET) || defined(INET6)
48057aea6dfSBjoern A. Zeeb 	struct prison *tppr;
481b38ff370SJamie Gritton 	void *op;
4820304c731SJamie Gritton #endif
48376ca6f88SJamie Gritton 	unsigned long hid;
484b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
485cc5fd8c7SJamie Gritton 	int born, created, cuflags, descend, enforce;
486cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
4870cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
488672756aaSJamie Gritton 	int jid, jsys, len, level;
489b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
490f6e633a9SMartin Matuska 	int fullpath_disabled;
491d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
4920304c731SJamie Gritton 	int ii, ij;
493413628a7SBjoern A. Zeeb #endif
494413628a7SBjoern A. Zeeb #ifdef INET
4952b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
496413628a7SBjoern A. Zeeb #endif
497b38ff370SJamie Gritton #ifdef INET6
4982b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
499b38ff370SJamie Gritton #endif
5006beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
5016beb3bb4SKirk McKusick 	unsigned tallow;
502b38ff370SJamie Gritton 	char numbuf[12];
503b38ff370SJamie Gritton 
504b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
505b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
506b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
507b38ff370SJamie Gritton 	if (error)
50875c13541SPoul-Henning Kamp 		return (error);
509b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
510b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5110304c731SJamie Gritton 		return (EPERM);
512b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
513b38ff370SJamie Gritton 		return (EINVAL);
514b38ff370SJamie Gritton 
515b38ff370SJamie Gritton 	/*
516b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
517b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
518b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
519b38ff370SJamie Gritton 	 * the allprison lock.
520b38ff370SJamie Gritton 	 *
521b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
522b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
523b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
524b38ff370SJamie Gritton 	 */
525b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
526b38ff370SJamie Gritton 	if (error)
527b38ff370SJamie Gritton 		return (error);
528b38ff370SJamie Gritton #ifdef INET
529b38ff370SJamie Gritton 	ip4 = NULL;
530b38ff370SJamie Gritton #endif
531b38ff370SJamie Gritton #ifdef INET6
532b38ff370SJamie Gritton 	ip6 = NULL;
533b38ff370SJamie Gritton #endif
5346dfe0a3dSMartin Matuska 	g_path = NULL;
535b38ff370SJamie Gritton 
536b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
537b6f47c23SJamie Gritton 	if (!cuflags) {
538b6f47c23SJamie Gritton 		error = EINVAL;
539b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
540b6f47c23SJamie Gritton 		goto done_errmsg;
541b6f47c23SJamie Gritton 	}
542b6f47c23SJamie Gritton 
543b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
544b38ff370SJamie Gritton 	if (error == ENOENT)
545b38ff370SJamie Gritton 		jid = 0;
546b38ff370SJamie Gritton 	else if (error != 0)
547b38ff370SJamie Gritton 		goto done_free;
548b38ff370SJamie Gritton 
549b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
550b38ff370SJamie Gritton 	if (error == ENOENT)
551b38ff370SJamie Gritton 		gotslevel = 0;
552b38ff370SJamie Gritton 	else if (error != 0)
553b38ff370SJamie Gritton 		goto done_free;
554b38ff370SJamie Gritton 	else
555b38ff370SJamie Gritton 		gotslevel = 1;
556b38ff370SJamie Gritton 
557b97457e2SJamie Gritton 	error =
558b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
559b97457e2SJamie Gritton 	if (error == ENOENT)
560b97457e2SJamie Gritton 		gotchildmax = 0;
561b97457e2SJamie Gritton 	else if (error != 0)
562b97457e2SJamie Gritton 		goto done_free;
563b97457e2SJamie Gritton 	else
564b97457e2SJamie Gritton 		gotchildmax = 1;
565b97457e2SJamie Gritton 
5660304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
567f337198dSJamie Gritton 	if (error == ENOENT)
568f337198dSJamie Gritton 		gotenforce = 0;
569f337198dSJamie Gritton 	else if (error != 0)
5700304c731SJamie Gritton 		goto done_free;
571f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
572f337198dSJamie Gritton 		error = EINVAL;
573f337198dSJamie Gritton 		goto done_free;
574f337198dSJamie Gritton 	} else
575f337198dSJamie Gritton 		gotenforce = 1;
5760304c731SJamie Gritton 
5770cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
5780cc207a6SMartin Matuska 	if (error == ENOENT)
5790cc207a6SMartin Matuska 		gotrsnum = 0;
5800cc207a6SMartin Matuska 	else if (error != 0)
5810cc207a6SMartin Matuska 		goto done_free;
5820cc207a6SMartin Matuska 	else
5830cc207a6SMartin Matuska 		gotrsnum = 1;
5840cc207a6SMartin Matuska 
585b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
586672756aaSJamie Gritton 	for (bf = pr_flag_bool;
587672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
588672756aaSJamie Gritton 	     bf++) {
589672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
590672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
5910304c731SJamie Gritton 	}
592b38ff370SJamie Gritton 	ch_flags |= pr_flags;
593672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
594672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
595672756aaSJamie Gritton 	     jsf++) {
596672756aaSJamie Gritton 		error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
5977cbf7213SJamie Gritton 		if (error == ENOENT)
5987cbf7213SJamie Gritton 			continue;
5997cbf7213SJamie Gritton 		if (error != 0)
6007cbf7213SJamie Gritton 			goto done_free;
6017cbf7213SJamie Gritton 		switch (jsys) {
6027cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
603672756aaSJamie Gritton 			if (!jsf->disable) {
6047cbf7213SJamie Gritton 				error = EINVAL;
6057cbf7213SJamie Gritton 				goto done_free;
6067cbf7213SJamie Gritton 			}
607672756aaSJamie Gritton 			pr_flags |= jsf->disable;
6087cbf7213SJamie Gritton 			break;
6097cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
610672756aaSJamie Gritton 			pr_flags |= jsf->new;
6117cbf7213SJamie Gritton 			break;
6127cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6137cbf7213SJamie Gritton 			break;
6147cbf7213SJamie Gritton 		default:
6157cbf7213SJamie Gritton 			error = EINVAL;
6167cbf7213SJamie Gritton 			goto done_free;
6177cbf7213SJamie Gritton 		}
618672756aaSJamie Gritton 		ch_flags |= jsf->new | jsf->disable;
6197cbf7213SJamie Gritton 	}
6204affa14cSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
6214affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6224affa14cSJamie Gritton 		error = EINVAL;
6234affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6244affa14cSJamie Gritton 		goto done_errmsg;
6254affa14cSJamie Gritton 	}
626679e1390SJamie Gritton #ifdef VIMAGE
627679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
628679e1390SJamie Gritton 		error = EINVAL;
629679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
630679e1390SJamie Gritton 		goto done_errmsg;
631679e1390SJamie Gritton 	}
632679e1390SJamie Gritton #endif
6332b0d6f81SJamie Gritton #ifdef INET
6342b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
6352b0d6f81SJamie Gritton 		error = EINVAL;
6362b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
6372b0d6f81SJamie Gritton 		goto done_errmsg;
6382b0d6f81SJamie Gritton 	}
6392b0d6f81SJamie Gritton #endif
6402b0d6f81SJamie Gritton #ifdef INET6
6412b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
6422b0d6f81SJamie Gritton 		error = EINVAL;
6432b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
6442b0d6f81SJamie Gritton 		goto done_errmsg;
6452b0d6f81SJamie Gritton 	}
6462b0d6f81SJamie Gritton #endif
647b38ff370SJamie Gritton 
6480304c731SJamie Gritton 	pr_allow = ch_allow = 0;
649672756aaSJamie Gritton 	for (bf = pr_flag_allow;
6500e5c6bd4SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0;
651672756aaSJamie Gritton 	     bf++) {
652672756aaSJamie Gritton 		vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
653672756aaSJamie Gritton 		vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
6540304c731SJamie Gritton 	}
6550304c731SJamie Gritton 	ch_allow |= pr_allow;
6560304c731SJamie Gritton 
657b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
658b38ff370SJamie Gritton 	if (error == ENOENT)
659b38ff370SJamie Gritton 		name = NULL;
660b38ff370SJamie Gritton 	else if (error != 0)
661b38ff370SJamie Gritton 		goto done_free;
662b38ff370SJamie Gritton 	else {
663b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
664b38ff370SJamie Gritton 			error = EINVAL;
665b38ff370SJamie Gritton 			goto done_free;
666b38ff370SJamie Gritton 		}
667b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
668b38ff370SJamie Gritton 			error = ENAMETOOLONG;
669b38ff370SJamie Gritton 			goto done_free;
670b38ff370SJamie Gritton 		}
671b38ff370SJamie Gritton 	}
672b38ff370SJamie Gritton 
673b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
674b38ff370SJamie Gritton 	if (error == ENOENT)
675b38ff370SJamie Gritton 		host = NULL;
676b38ff370SJamie Gritton 	else if (error != 0)
677b38ff370SJamie Gritton 		goto done_free;
678b38ff370SJamie Gritton 	else {
67976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
68076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
681b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
682b38ff370SJamie Gritton 			error = EINVAL;
683b38ff370SJamie Gritton 			goto done_free;
684b38ff370SJamie Gritton 		}
685b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
686b38ff370SJamie Gritton 			error = ENAMETOOLONG;
687b38ff370SJamie Gritton 			goto done_free;
688b38ff370SJamie Gritton 		}
689b38ff370SJamie Gritton 	}
690b38ff370SJamie Gritton 
69176ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
69276ca6f88SJamie Gritton 	if (error == ENOENT)
69376ca6f88SJamie Gritton 		domain = NULL;
69476ca6f88SJamie Gritton 	else if (error != 0)
69576ca6f88SJamie Gritton 		goto done_free;
69676ca6f88SJamie Gritton 	else {
69776ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
69876ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
69976ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
70076ca6f88SJamie Gritton 			error = EINVAL;
70176ca6f88SJamie Gritton 			goto done_free;
70276ca6f88SJamie Gritton 		}
70376ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
70476ca6f88SJamie Gritton 			error = ENAMETOOLONG;
70576ca6f88SJamie Gritton 			goto done_free;
70676ca6f88SJamie Gritton 		}
70776ca6f88SJamie Gritton 	}
70876ca6f88SJamie Gritton 
70976ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
71076ca6f88SJamie Gritton 	if (error == ENOENT)
71176ca6f88SJamie Gritton 		uuid = NULL;
71276ca6f88SJamie Gritton 	else if (error != 0)
71376ca6f88SJamie Gritton 		goto done_free;
71476ca6f88SJamie Gritton 	else {
71576ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
71676ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
71776ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
71876ca6f88SJamie Gritton 			error = EINVAL;
71976ca6f88SJamie Gritton 			goto done_free;
72076ca6f88SJamie Gritton 		}
72176ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
72276ca6f88SJamie Gritton 			error = ENAMETOOLONG;
72376ca6f88SJamie Gritton 			goto done_free;
72476ca6f88SJamie Gritton 		}
72576ca6f88SJamie Gritton 	}
72676ca6f88SJamie Gritton 
727841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
728a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
72976ca6f88SJamie Gritton 		uint32_t hid32;
73076ca6f88SJamie Gritton 
73176ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
73276ca6f88SJamie Gritton 		hid = hid32;
73376ca6f88SJamie Gritton 	} else
73476ca6f88SJamie Gritton #endif
73576ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
73676ca6f88SJamie Gritton 	if (error == ENOENT)
73776ca6f88SJamie Gritton 		gothid = 0;
73876ca6f88SJamie Gritton 	else if (error != 0)
73976ca6f88SJamie Gritton 		goto done_free;
74076ca6f88SJamie Gritton 	else {
74176ca6f88SJamie Gritton 		gothid = 1;
74276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
74376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
74476ca6f88SJamie Gritton 	}
74576ca6f88SJamie Gritton 
746b38ff370SJamie Gritton #ifdef INET
747b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
748b38ff370SJamie Gritton 	if (error == ENOENT)
7490e5e396eSJamie Gritton 		ip4s = 0;
750b38ff370SJamie Gritton 	else if (error != 0)
751b38ff370SJamie Gritton 		goto done_free;
752b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
753b38ff370SJamie Gritton 		error = EINVAL;
754b38ff370SJamie Gritton 		goto done_free;
7550304c731SJamie Gritton 	} else {
7566a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
7576a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
7586a3f2779SJamie Gritton 		if (ip4s > 0) {
759b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
760b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
761b38ff370SJamie Gritton 				error = EINVAL;
762b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
763b38ff370SJamie Gritton 				goto done_errmsg;
764b38ff370SJamie Gritton 			}
7652b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
766b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
767b38ff370SJamie Gritton 			/*
7680304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
7690304c731SJamie Gritton 			 * the primary IP address as given from userland.
7700304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
771bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
772bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
773bef916f9SBjoern A. Zeeb 			 * address to connect from.
774b38ff370SJamie Gritton 			 */
775b38ff370SJamie Gritton 			if (ip4s > 1)
7760ce1624dSStephen J. Kiernan 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
7770ce1624dSStephen J. Kiernan 				    prison_qcmp_v4);
778b38ff370SJamie Gritton 			/*
7790304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
7800304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
7810304c731SJamie Gritton 			 * addresses it is their problem.
782b38ff370SJamie Gritton 			 *
7830304c731SJamie Gritton 			 * We do not have to care about byte order for these
7840304c731SJamie Gritton 			 * checks so we will do them in NBO.
785b38ff370SJamie Gritton 			 */
786b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
787b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
788b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
789b38ff370SJamie Gritton 					error = EINVAL;
790b38ff370SJamie Gritton 					goto done_free;
791b38ff370SJamie Gritton 				}
792b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
793b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
794b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
795b38ff370SJamie Gritton 					error = EINVAL;
796b38ff370SJamie Gritton 					goto done_free;
797b38ff370SJamie Gritton 				}
798b38ff370SJamie Gritton 			}
799b38ff370SJamie Gritton 		}
8000304c731SJamie Gritton 	}
801b38ff370SJamie Gritton #endif
802b38ff370SJamie Gritton 
803b38ff370SJamie Gritton #ifdef INET6
804b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
805b38ff370SJamie Gritton 	if (error == ENOENT)
8060e5e396eSJamie Gritton 		ip6s = 0;
807b38ff370SJamie Gritton 	else if (error != 0)
808b38ff370SJamie Gritton 		goto done_free;
809b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
810b38ff370SJamie Gritton 		error = EINVAL;
811b38ff370SJamie Gritton 		goto done_free;
8120304c731SJamie Gritton 	} else {
8136a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
8146a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
8156a3f2779SJamie Gritton 		if (ip6s > 0) {
816b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
817b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
818b38ff370SJamie Gritton 				error = EINVAL;
819b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
820b38ff370SJamie Gritton 				goto done_errmsg;
821b38ff370SJamie Gritton 			}
8222b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
823b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
824b38ff370SJamie Gritton 			if (ip6s > 1)
8250ce1624dSStephen J. Kiernan 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
8260ce1624dSStephen J. Kiernan 				    prison_qcmp_v6);
827b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8280304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
829b38ff370SJamie Gritton 					error = EINVAL;
830b38ff370SJamie Gritton 					goto done_free;
831b38ff370SJamie Gritton 				}
832b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
833b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
834b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
835b38ff370SJamie Gritton 				{
836b38ff370SJamie Gritton 					error = EINVAL;
837b38ff370SJamie Gritton 					goto done_free;
838b38ff370SJamie Gritton 				}
839b38ff370SJamie Gritton 			}
840b38ff370SJamie Gritton 		}
8410304c731SJamie Gritton 	}
842b38ff370SJamie Gritton #endif
843b38ff370SJamie Gritton 
844bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
845bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
846bdfc8cc4SJamie Gritton 		error = EINVAL;
847bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
848bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
849bdfc8cc4SJamie Gritton 		goto done_errmsg;
850bdfc8cc4SJamie Gritton 	}
851bdfc8cc4SJamie Gritton #endif
852bdfc8cc4SJamie Gritton 
853cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
854cf0313c6SJamie Gritton 	if (error == ENOENT)
855cf0313c6SJamie Gritton 		osrelstr = NULL;
856cf0313c6SJamie Gritton 	else if (error != 0)
857cf0313c6SJamie Gritton 		goto done_free;
858cf0313c6SJamie Gritton 	else {
859cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
860cf0313c6SJamie Gritton 			error = EINVAL;
861cf0313c6SJamie Gritton 			vfs_opterror(opts,
862cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
863cf0313c6SJamie Gritton 			goto done_errmsg;
864cf0313c6SJamie Gritton 		}
865cf0313c6SJamie Gritton 		if (len == 0 || len >= OSRELEASELEN) {
866cf0313c6SJamie Gritton 			error = EINVAL;
867cf0313c6SJamie Gritton 			vfs_opterror(opts,
868cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
869cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
870cf0313c6SJamie Gritton 			goto done_errmsg;
871cf0313c6SJamie Gritton 		}
872cf0313c6SJamie Gritton 	}
873cf0313c6SJamie Gritton 
874cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
875cf0313c6SJamie Gritton 	if (error == ENOENT)
876cf0313c6SJamie Gritton 		osreldt = 0;
877cf0313c6SJamie Gritton 	else if (error != 0)
878cf0313c6SJamie Gritton 		goto done_free;
879cf0313c6SJamie Gritton 	else {
880cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
881cf0313c6SJamie Gritton 			error = EINVAL;
882cf0313c6SJamie Gritton 			vfs_opterror(opts,
883cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
884cf0313c6SJamie Gritton 			goto done_errmsg;
885cf0313c6SJamie Gritton 		}
886cf0313c6SJamie Gritton 		if (osreldt == 0) {
887cf0313c6SJamie Gritton 			error = EINVAL;
888cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
889cf0313c6SJamie Gritton 			goto done_errmsg;
890cf0313c6SJamie Gritton 		}
891cf0313c6SJamie Gritton 	}
892cf0313c6SJamie Gritton 
8939cbe30e1SMartin Matuska 	fullpath_disabled = 0;
894b38ff370SJamie Gritton 	root = NULL;
895b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
896b38ff370SJamie Gritton 	if (error == ENOENT)
897b38ff370SJamie Gritton 		path = NULL;
898b38ff370SJamie Gritton 	else if (error != 0)
899b38ff370SJamie Gritton 		goto done_free;
900b38ff370SJamie Gritton 	else {
901b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
902b38ff370SJamie Gritton 			error = EINVAL;
903b38ff370SJamie Gritton 			vfs_opterror(opts,
904b38ff370SJamie Gritton 			    "path cannot be changed after creation");
905b38ff370SJamie Gritton 			goto done_errmsg;
906b38ff370SJamie Gritton 		}
907b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
908b38ff370SJamie Gritton 			error = EINVAL;
909b38ff370SJamie Gritton 			goto done_free;
910b38ff370SJamie Gritton 		}
9115050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
912b38ff370SJamie Gritton 		    path, td);
913b38ff370SJamie Gritton 		error = namei(&nd);
914b38ff370SJamie Gritton 		if (error)
915b38ff370SJamie Gritton 			goto done_free;
916b38ff370SJamie Gritton 		root = nd.ni_vp;
917b38ff370SJamie Gritton 		NDFREE(&nd, NDF_ONLY_PNBUF);
9186dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
9196dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
9206dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
9216dfe0a3dSMartin Matuska 		if (error == 0)
9226dfe0a3dSMartin Matuska 			path = g_path;
9236dfe0a3dSMartin Matuska 		else if (error == ENODEV) {
924f6e633a9SMartin Matuska 			/* proceed if sysctl debug.disablefullpath == 1 */
925f6e633a9SMartin Matuska 			fullpath_disabled = 1;
926f6e633a9SMartin Matuska 			if (len < 2 || (len == 2 && path[0] == '/'))
927f6e633a9SMartin Matuska 				path = NULL;
9286dfe0a3dSMartin Matuska 		} else {
929f6e633a9SMartin Matuska 			/* exit on other errors */
930b38ff370SJamie Gritton 			goto done_free;
931b38ff370SJamie Gritton 		}
932f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
933f6e633a9SMartin Matuska 			error = ENOTDIR;
934f6e633a9SMartin Matuska 			vput(root);
935f6e633a9SMartin Matuska 			goto done_free;
936f6e633a9SMartin Matuska 		}
937f6e633a9SMartin Matuska 		VOP_UNLOCK(root, 0);
938f6e633a9SMartin Matuska 		if (fullpath_disabled) {
939f6e633a9SMartin Matuska 			/* Leave room for a real-root full pathname. */
940f6e633a9SMartin Matuska 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
941f6e633a9SMartin Matuska 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
942f6e633a9SMartin Matuska 				error = ENAMETOOLONG;
943932a6e43SJamie Gritton 				vrele(root);
944f6e633a9SMartin Matuska 				goto done_free;
945f6e633a9SMartin Matuska 			}
946b38ff370SJamie Gritton 		}
947b38ff370SJamie Gritton 	}
948b38ff370SJamie Gritton 
949b38ff370SJamie Gritton 	/*
950b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
951b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
952b38ff370SJamie Gritton 	 */
953b38ff370SJamie Gritton 	pr = NULL;
954b6f47c23SJamie Gritton 	ppr = mypr;
955babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
956babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
957babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
958babbbb9cSJamie Gritton 		if (*p != '\0')
959babbbb9cSJamie Gritton 			jid = 0;
960babbbb9cSJamie Gritton 	}
961b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
962b38ff370SJamie Gritton 	if (jid != 0) {
9630304c731SJamie Gritton 		/*
9640304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
9650304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
9660304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
9670304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
9680304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
9690304c731SJamie Gritton 		 */
970b38ff370SJamie Gritton 		if (jid < 0) {
971b38ff370SJamie Gritton 			error = EINVAL;
972b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
973b38ff370SJamie Gritton 			goto done_unlock_list;
974b38ff370SJamie Gritton 		}
975b38ff370SJamie Gritton 		pr = prison_find(jid);
976b38ff370SJamie Gritton 		if (pr != NULL) {
9770304c731SJamie Gritton 			ppr = pr->pr_parent;
978b38ff370SJamie Gritton 			/* Create: jid must not exist. */
979b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
980b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
981b38ff370SJamie Gritton 				error = EEXIST;
982b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
983b38ff370SJamie Gritton 				    jid);
984b38ff370SJamie Gritton 				goto done_unlock_list;
985b38ff370SJamie Gritton 			}
9860304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
9870304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
9880304c731SJamie Gritton 				pr = NULL;
9890304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
990b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
991b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
992b38ff370SJamie Gritton 					error = ENOENT;
993b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
994b38ff370SJamie Gritton 					    jid);
995b38ff370SJamie Gritton 					goto done_unlock_list;
996b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
997b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
998b38ff370SJamie Gritton 					/*
999b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1000b38ff370SJamie Gritton 					 * (via attach or persist), but first
1001b38ff370SJamie Gritton 					 * it must determine if another jail
1002b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1003b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1004b38ff370SJamie Gritton 					 * name.
1005b38ff370SJamie Gritton 					 */
1006b38ff370SJamie Gritton 					if (name == NULL)
10070304c731SJamie Gritton 						name = prison_name(mypr, pr);
1008b38ff370SJamie Gritton 				}
1009b38ff370SJamie Gritton 			}
1010b38ff370SJamie Gritton 		}
1011b38ff370SJamie Gritton 		if (pr == NULL) {
1012b38ff370SJamie Gritton 			/* Update: jid must exist. */
1013b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1014b38ff370SJamie Gritton 				error = ENOENT;
1015b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
1016b38ff370SJamie Gritton 				goto done_unlock_list;
1017b38ff370SJamie Gritton 			}
1018b38ff370SJamie Gritton 		}
1019b38ff370SJamie Gritton 	}
1020b38ff370SJamie Gritton 	/*
1021b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1022b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1023b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1024b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1025b38ff370SJamie Gritton 	 * because that is the jail being updated).
1026b38ff370SJamie Gritton 	 */
1027b6f47c23SJamie Gritton 	namelc = NULL;
1028b38ff370SJamie Gritton 	if (name != NULL) {
1029babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1030babbbb9cSJamie Gritton 		if (namelc == NULL)
1031babbbb9cSJamie Gritton 			namelc = name;
1032babbbb9cSJamie Gritton 		else {
10330304c731SJamie Gritton 			/*
10340304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
10350304c731SJamie Gritton 			 * parent and child names, and make sure the parent
10360304c731SJamie Gritton 			 * exists or matches an already found jail.
10370304c731SJamie Gritton 			 */
10380304c731SJamie Gritton 			if (pr != NULL) {
1039babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1040babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
10410304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
10420304c731SJamie Gritton 					error = EINVAL;
10430304c731SJamie Gritton 					vfs_opterror(opts,
10440304c731SJamie Gritton 					    "cannot change jail's parent");
10450304c731SJamie Gritton 					goto done_unlock_list;
10460304c731SJamie Gritton 				}
10470304c731SJamie Gritton 			} else {
1048b6f47c23SJamie Gritton 				*namelc = '\0';
10490304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
10500304c731SJamie Gritton 				if (ppr == NULL) {
10510304c731SJamie Gritton 					error = ENOENT;
10520304c731SJamie Gritton 					vfs_opterror(opts,
10530304c731SJamie Gritton 					    "jail \"%s\" not found", name);
10540304c731SJamie Gritton 					goto done_unlock_list;
10550304c731SJamie Gritton 				}
10560304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
1057b6f47c23SJamie Gritton 				*namelc = '.';
10580304c731SJamie Gritton 			}
1059b6f47c23SJamie Gritton 			namelc++;
10600304c731SJamie Gritton 		}
1061b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1062b6f47c23SJamie Gritton 			pnamelen =
10630304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1064b38ff370SJamie Gritton  name_again:
10650304c731SJamie Gritton 			deadpr = NULL;
10660304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
1067b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
1068b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
1069b38ff370SJamie Gritton 					if (pr == NULL &&
1070b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
1071b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
1072b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
1073b38ff370SJamie Gritton 							/*
1074b38ff370SJamie Gritton 							 * Use this jail
1075b38ff370SJamie Gritton 							 * for updates.
1076b38ff370SJamie Gritton 							 */
1077b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
1078b38ff370SJamie Gritton 								pr = tpr;
1079b38ff370SJamie Gritton 								break;
1080b38ff370SJamie Gritton 							}
1081b38ff370SJamie Gritton 							deadpr = tpr;
1082b38ff370SJamie Gritton 						}
1083b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1084b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
1085b38ff370SJamie Gritton 						/*
1086b38ff370SJamie Gritton 						 * Create, or update(jid):
1087b38ff370SJamie Gritton 						 * name must not exist in an
10880304c731SJamie Gritton 						 * active sibling jail.
1089b38ff370SJamie Gritton 						 */
1090b38ff370SJamie Gritton 						error = EEXIST;
1091b38ff370SJamie Gritton 						if (pr != NULL)
1092b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
1093b38ff370SJamie Gritton 						vfs_opterror(opts,
1094b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1095b38ff370SJamie Gritton 						   name);
1096b38ff370SJamie Gritton 						goto done_unlock_list;
1097b38ff370SJamie Gritton 					}
1098b38ff370SJamie Gritton 				}
1099b38ff370SJamie Gritton 			}
1100b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1101b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1102b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1103b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
1104b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
1105b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1106b38ff370SJamie Gritton 						goto name_again;
1107b38ff370SJamie Gritton 					}
1108b38ff370SJamie Gritton 					pr = deadpr;
1109b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1110b38ff370SJamie Gritton 					error = ENOENT;
1111b38ff370SJamie Gritton 					vfs_opterror(opts,
1112b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1113b38ff370SJamie Gritton 					goto done_unlock_list;
1114b38ff370SJamie Gritton 				}
1115b38ff370SJamie Gritton 			}
1116b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1117b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1118b38ff370SJamie Gritton 				error = ENOENT;
1119b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1120b38ff370SJamie Gritton 				    name);
1121b38ff370SJamie Gritton 				goto done_unlock_list;
1122b38ff370SJamie Gritton 			}
1123b38ff370SJamie Gritton 		}
1124b38ff370SJamie Gritton 	}
1125b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1126b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1127b38ff370SJamie Gritton 		error = ENOENT;
1128b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1129b38ff370SJamie Gritton 		goto done_unlock_list;
1130b38ff370SJamie Gritton 	}
1131b38ff370SJamie Gritton 
1132b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1133b38ff370SJamie Gritton 	if (pr == NULL) {
1134b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1135b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1136b97457e2SJamie Gritton 				error = EPERM;
1137b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
1138b97457e2SJamie Gritton 				goto done_unlock_list;
1139b97457e2SJamie Gritton 			}
1140b38ff370SJamie Gritton 		created = 1;
11410304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
11422a549507SJamie Gritton 		if (ppr->pr_ref == 0) {
11430304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
11440304c731SJamie Gritton 			error = ENOENT;
1145b6f47c23SJamie Gritton 			vfs_opterror(opts, "jail \"%s\" not found",
1146b6f47c23SJamie Gritton 			    prison_name(mypr, ppr));
11470304c731SJamie Gritton 			goto done_unlock_list;
11480304c731SJamie Gritton 		}
11490304c731SJamie Gritton 		ppr->pr_ref++;
11500304c731SJamie Gritton 		ppr->pr_uref++;
11510304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1152b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1153b38ff370SJamie Gritton 		if (jid == 0) {
1154b38ff370SJamie Gritton 			/* Find the next free jid. */
1155b38ff370SJamie Gritton 			jid = lastprid + 1;
1156b38ff370SJamie Gritton  findnext:
1157b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1158b38ff370SJamie Gritton 				jid = 1;
1159b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1160b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1161b38ff370SJamie Gritton 					continue;
1162b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1163b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1164b38ff370SJamie Gritton 					break;
1165b38ff370SJamie Gritton 				}
1166b38ff370SJamie Gritton 				if (jid == lastprid) {
1167b38ff370SJamie Gritton 					error = EAGAIN;
1168b38ff370SJamie Gritton 					vfs_opterror(opts,
1169b38ff370SJamie Gritton 					    "no available jail IDs");
1170b38ff370SJamie Gritton 					free(pr, M_PRISON);
11710304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
11720304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
11730304c731SJamie Gritton 					goto done_releroot;
1174b38ff370SJamie Gritton 				}
1175b38ff370SJamie Gritton 				jid++;
1176b38ff370SJamie Gritton 				goto findnext;
1177b38ff370SJamie Gritton 			}
1178b38ff370SJamie Gritton 			lastprid = jid;
1179b38ff370SJamie Gritton 		} else {
1180b38ff370SJamie Gritton 			/*
1181b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1182b38ff370SJamie Gritton 			 * so just find where to insert it.
1183b38ff370SJamie Gritton 			 */
1184b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1185b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1186b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1187b38ff370SJamie Gritton 					break;
1188b38ff370SJamie Gritton 				}
1189b38ff370SJamie Gritton 		}
1190b38ff370SJamie Gritton 		if (tpr == NULL)
1191b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
11920304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
11930304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1194b97457e2SJamie Gritton 			tpr->pr_childcount++;
1195b38ff370SJamie Gritton 
11960304c731SJamie Gritton 		pr->pr_parent = ppr;
1197b38ff370SJamie Gritton 		pr->pr_id = jid;
11980304c731SJamie Gritton 
11990304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1200b6f47c23SJamie Gritton 		if (namelc == NULL)
1201b6f47c23SJamie Gritton 			namelc = "";
1202b38ff370SJamie Gritton 		if (path == NULL) {
1203b38ff370SJamie Gritton 			path = "/";
12040304c731SJamie Gritton 			root = mypr->pr_root;
1205b38ff370SJamie Gritton 			vref(root);
1206b38ff370SJamie Gritton 		}
12078986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
12088986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1209bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1210bdfc8cc4SJamie Gritton #ifdef VIMAGE
1211bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1212bdfc8cc4SJamie Gritton #endif
1213bdfc8cc4SJamie Gritton 		{
12140304c731SJamie Gritton #ifdef INET
12152b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12166a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
12172b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12182b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12192b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12202b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12212b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12222b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12232b0d6f81SJamie Gritton 					    M_WAITOK);
12242b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12252b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12262b0d6f81SJamie Gritton 				}
12272b0d6f81SJamie Gritton 			}
12280304c731SJamie Gritton #endif
12290304c731SJamie Gritton #ifdef INET6
12302b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
12316a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
12322b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
12332b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
12342b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
12352b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
12362b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
12372b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
12382b0d6f81SJamie Gritton 					    M_WAITOK);
12392b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
12402b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
12412b0d6f81SJamie Gritton 				}
12422b0d6f81SJamie Gritton 			}
12430304c731SJamie Gritton #endif
1244bdfc8cc4SJamie Gritton 		}
1245bdfc8cc4SJamie Gritton #endif
1246592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1247592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1248592bcae8SBjoern A. Zeeb 
12490304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
12500304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1251af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1252bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1253b38ff370SJamie Gritton 
1254b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1255b96bd95bSIan Lepore 		if (osrelstr == NULL)
1256b96bd95bSIan Lepore 		    strcpy(pr->pr_osrelease, ppr->pr_osrelease);
1257b96bd95bSIan Lepore 		else
1258b96bd95bSIan Lepore 		    strcpy(pr->pr_osrelease, osrelstr);
1259b96bd95bSIan Lepore 
12600304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
12610304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
126273d9e52dSJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1263b38ff370SJamie Gritton 
1264679e1390SJamie Gritton #ifdef VIMAGE
1265679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1266679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1267679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1268679e1390SJamie Gritton #endif
1269b38ff370SJamie Gritton 		/*
1270b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1271b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1272b38ff370SJamie Gritton 		 */
12730304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1274b38ff370SJamie Gritton 		if (error) {
1275b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1276b38ff370SJamie Gritton 			goto done_releroot;
1277b38ff370SJamie Gritton 		}
1278b38ff370SJamie Gritton 
1279b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1280b38ff370SJamie Gritton 		/*
1281b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1282b6f47c23SJamie Gritton 		 * want others to see the incomplete prison once the
1283b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1284b38ff370SJamie Gritton 		 */
1285b38ff370SJamie Gritton 	} else {
1286b38ff370SJamie Gritton 		created = 0;
12872b0d6f81SJamie Gritton 		/*
12882b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
12892b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
12902b0d6f81SJamie Gritton 		 */
12912b0d6f81SJamie Gritton 		pr->pr_ref++;
1292bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1293bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1294bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1295bdfc8cc4SJamie Gritton 			error = EINVAL;
1296bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1297bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
1298bdfc8cc4SJamie Gritton 			goto done_deref_locked;
1299bdfc8cc4SJamie Gritton 		}
1300bdfc8cc4SJamie Gritton #endif
13012b0d6f81SJamie Gritton #ifdef INET
13022b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13032b0d6f81SJamie Gritton 			error = EINVAL;
13042b0d6f81SJamie Gritton 			vfs_opterror(opts,
13052b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
13062b0d6f81SJamie Gritton 			goto done_deref_locked;
13072b0d6f81SJamie Gritton 		}
13082b0d6f81SJamie Gritton #endif
13092b0d6f81SJamie Gritton #ifdef INET6
13102b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13112b0d6f81SJamie Gritton 			error = EINVAL;
13122b0d6f81SJamie Gritton 			vfs_opterror(opts,
13132b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
13142b0d6f81SJamie Gritton 			goto done_deref_locked;
13152b0d6f81SJamie Gritton 		}
13162b0d6f81SJamie Gritton #endif
1317b38ff370SJamie Gritton 	}
1318b38ff370SJamie Gritton 
1319b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13200304c731SJamie Gritton 	if (gotslevel) {
13210304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13220304c731SJamie Gritton 			error = EPERM;
13230304c731SJamie Gritton 			goto done_deref_locked;
13240304c731SJamie Gritton 		}
13250304c731SJamie Gritton 	}
1326b97457e2SJamie Gritton 	if (gotchildmax) {
1327b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1328b97457e2SJamie Gritton 			error = EPERM;
1329b97457e2SJamie Gritton 			goto done_deref_locked;
1330b97457e2SJamie Gritton 		}
1331b97457e2SJamie Gritton 	}
13320304c731SJamie Gritton 	if (gotenforce) {
13330304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
13340304c731SJamie Gritton 			error = EPERM;
13350304c731SJamie Gritton 			goto done_deref_locked;
13360304c731SJamie Gritton 		}
13370304c731SJamie Gritton 	}
13380cc207a6SMartin Matuska 	if (gotrsnum) {
13390cc207a6SMartin Matuska 		/*
13400cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
13410cc207a6SMartin Matuska 		 */
1342bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
13430cc207a6SMartin Matuska 			error = EINVAL;
13440cc207a6SMartin Matuska 			goto done_deref_locked;
13450cc207a6SMartin Matuska 		}
13460cc207a6SMartin Matuska 		/*
1347bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
13480cc207a6SMartin Matuska 		 */
13490cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
13500cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
13510cc207a6SMartin Matuska 				error = EPERM;
13520cc207a6SMartin Matuska 				goto done_deref_locked;
1353bf3db8aaSMartin Matuska 			} else
13540cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
13550cc207a6SMartin Matuska 		}
13560cc207a6SMartin Matuska 	}
1357b38ff370SJamie Gritton #ifdef INET
13582b0d6f81SJamie Gritton 	if (ip4s > 0) {
13590304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
13600304c731SJamie Gritton 			/*
13610304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
13620304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
13630304c731SJamie Gritton 			 * about the parent being unlocked, as any
13640304c731SJamie Gritton 			 * setting is done with allprison_lock held.
13650304c731SJamie Gritton 			 */
13660304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
13672b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
13680304c731SJamie Gritton 					break;
13690304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
13700304c731SJamie Gritton 				error = EPERM;
13710304c731SJamie Gritton 				goto done_deref_locked;
13720304c731SJamie Gritton 			}
13730304c731SJamie Gritton 			if (ip4s > 1) {
13740304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
13750304c731SJamie Gritton 					if (ip4[ii].s_addr ==
13760304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1377b38ff370SJamie Gritton 						continue;
13780304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
13790304c731SJamie Gritton 						if (ip4[ii].s_addr ==
13800304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
13810304c731SJamie Gritton 							break;
13820304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
13830304c731SJamie Gritton 						break;
13840304c731SJamie Gritton 				}
13850304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
13860304c731SJamie Gritton 					error = EPERM;
13870304c731SJamie Gritton 					goto done_deref_locked;
13880304c731SJamie Gritton 				}
13890304c731SJamie Gritton 			}
13900304c731SJamie Gritton 		}
13910304c731SJamie Gritton 		/*
13920304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
13930304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
13940304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
13950304c731SJamie Gritton 		 * IP stop checking and return error.
13960304c731SJamie Gritton 		 */
1397bdfc8cc4SJamie Gritton #ifdef VIMAGE
139808b43333SJamie Gritton 		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1399bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1400bdfc8cc4SJamie Gritton 				break;
140108b43333SJamie Gritton #else
140208b43333SJamie Gritton 		tppr = &prison0;
1403bdfc8cc4SJamie Gritton #endif
1404bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1405bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1406bdfc8cc4SJamie Gritton #ifdef VIMAGE
14072b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1408bdfc8cc4SJamie Gritton #endif
1409bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14100304c731SJamie Gritton 				descend = 0;
14110304c731SJamie Gritton 				continue;
14120304c731SJamie Gritton 			}
14130304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
14140304c731SJamie Gritton 				continue;
14150304c731SJamie Gritton 			descend = 0;
14160304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
14170304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
14180304c731SJamie Gritton 				continue;
14190304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
14200ce1624dSStephen J. Kiernan 				if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
14210ce1624dSStephen J. Kiernan 				    0) {
14220304c731SJamie Gritton 					error = EADDRINUSE;
1423b38ff370SJamie Gritton 					vfs_opterror(opts,
1424b38ff370SJamie Gritton 					    "IPv4 addresses clash");
1425b38ff370SJamie Gritton 					goto done_deref_locked;
1426b38ff370SJamie Gritton 				}
14270304c731SJamie Gritton 			}
14280304c731SJamie Gritton 		}
14290304c731SJamie Gritton 	}
1430b38ff370SJamie Gritton #endif
1431b38ff370SJamie Gritton #ifdef INET6
14322b0d6f81SJamie Gritton 	if (ip6s > 0) {
14330304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
14340304c731SJamie Gritton 			/*
14350304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14360304c731SJamie Gritton 			 * subset of the parent's list.
14370304c731SJamie Gritton 			 */
14380304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
14390304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14400304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
14410304c731SJamie Gritton 					break;
14420304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
14430304c731SJamie Gritton 				error = EPERM;
14440304c731SJamie Gritton 				goto done_deref_locked;
14450304c731SJamie Gritton 			}
14460304c731SJamie Gritton 			if (ip6s > 1) {
14470304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
14480304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14490304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
14500304c731SJamie Gritton 						continue;
14510304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
14520304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
14532b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
14540304c731SJamie Gritton 							break;
14550304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
14560304c731SJamie Gritton 						break;
14570304c731SJamie Gritton 				}
14580304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14590304c731SJamie Gritton 					error = EPERM;
14600304c731SJamie Gritton 					goto done_deref_locked;
14610304c731SJamie Gritton 				}
14620304c731SJamie Gritton 			}
14630304c731SJamie Gritton 		}
14640304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1465bdfc8cc4SJamie Gritton #ifdef VIMAGE
146608b43333SJamie Gritton 		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1467bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1468bdfc8cc4SJamie Gritton 				break;
146908b43333SJamie Gritton #else
147008b43333SJamie Gritton 		tppr = &prison0;
1471bdfc8cc4SJamie Gritton #endif
1472bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1473bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1474bdfc8cc4SJamie Gritton #ifdef VIMAGE
14752b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1476bdfc8cc4SJamie Gritton #endif
1477bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14780304c731SJamie Gritton 				descend = 0;
14790304c731SJamie Gritton 				continue;
14800304c731SJamie Gritton 			}
14810304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
14820304c731SJamie Gritton 				continue;
14830304c731SJamie Gritton 			descend = 0;
14840304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
14850304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
14860304c731SJamie Gritton 				continue;
14870304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
14880ce1624dSStephen J. Kiernan 				if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
14890ce1624dSStephen J. Kiernan 				    0) {
14900304c731SJamie Gritton 					error = EADDRINUSE;
1491b38ff370SJamie Gritton 					vfs_opterror(opts,
1492b38ff370SJamie Gritton 					    "IPv6 addresses clash");
1493b38ff370SJamie Gritton 					goto done_deref_locked;
1494b38ff370SJamie Gritton 				}
14950304c731SJamie Gritton 			}
14960304c731SJamie Gritton 		}
14970304c731SJamie Gritton 	}
1498b38ff370SJamie Gritton #endif
14990304c731SJamie Gritton 	onamelen = namelen = 0;
1500b6f47c23SJamie Gritton 	if (namelc != NULL) {
15016ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
15026ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
15036ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
15046ab6058eSJamie Gritton 		 */
1505b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1506b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
15076ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
15086ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1509b38ff370SJamie Gritton 			error = EINVAL;
1510babbbb9cSJamie Gritton 			vfs_opterror(opts,
1511babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
15120304c731SJamie Gritton 			goto done_deref_locked;
1513b38ff370SJamie Gritton 		}
1514b38ff370SJamie Gritton 		/*
15150304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
15160304c731SJamie Gritton 		 * children.
1517b38ff370SJamie Gritton 		 */
1518b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1519b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1520b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1521b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
15220304c731SJamie Gritton 			error = ENAMETOOLONG;
15230304c731SJamie Gritton 			goto done_deref_locked;
15240304c731SJamie Gritton 		}
15250304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
15260304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
15270304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
15280304c731SJamie Gritton 				error = ENAMETOOLONG;
15290304c731SJamie Gritton 				goto done_deref_locked;
15300304c731SJamie Gritton 			}
15310304c731SJamie Gritton 		}
15320304c731SJamie Gritton 	}
15330304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
15340304c731SJamie Gritton 		error = EPERM;
15350304c731SJamie Gritton 		goto done_deref_locked;
1536b38ff370SJamie Gritton 	}
1537b38ff370SJamie Gritton 
1538b6f47c23SJamie Gritton 	/*
1539b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1540b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1541b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1542b6f47c23SJamie Gritton 	 */
1543b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1544b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1545b6f47c23SJamie Gritton 	if (error != 0) {
1546b6f47c23SJamie Gritton 		prison_deref(pr, created
1547b6f47c23SJamie Gritton 		    ? PD_LIST_XLOCKED
1548b6f47c23SJamie Gritton 		    : PD_DEREF | PD_LIST_XLOCKED);
1549b6f47c23SJamie Gritton 		goto done_releroot;
1550b6f47c23SJamie Gritton 	}
1551b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
1552b6f47c23SJamie Gritton 
1553b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1554b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1555b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1556b6f47c23SJamie Gritton 			error = EINVAL;
1557b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
1558b6f47c23SJamie Gritton 			goto done_deref_locked;
1559b6f47c23SJamie Gritton 		}
1560b6f47c23SJamie Gritton 	}
1561b6f47c23SJamie Gritton 
1562b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1563b38ff370SJamie Gritton #ifdef INET
15640304c731SJamie Gritton 	redo_ip4 = 0;
15650304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
15660304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1567b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
15680304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1569b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1570b38ff370SJamie Gritton 		ip4 = NULL;
15710304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1572bdfc8cc4SJamie Gritton #ifdef VIMAGE
1573bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1574bdfc8cc4SJamie Gritton 				descend = 0;
1575bdfc8cc4SJamie Gritton 				continue;
1576bdfc8cc4SJamie Gritton 			}
1577bdfc8cc4SJamie Gritton #endif
15780304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
15790304c731SJamie Gritton 				redo_ip4 = 1;
15800304c731SJamie Gritton 				descend = 0;
15810304c731SJamie Gritton 			}
15820304c731SJamie Gritton 		}
15830304c731SJamie Gritton 	}
1584b38ff370SJamie Gritton #endif
1585b38ff370SJamie Gritton #ifdef INET6
15860304c731SJamie Gritton 	redo_ip6 = 0;
15870304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
15880304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1589b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
15900304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1591b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1592b38ff370SJamie Gritton 		ip6 = NULL;
15930304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1594bdfc8cc4SJamie Gritton #ifdef VIMAGE
1595bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1596bdfc8cc4SJamie Gritton 				descend = 0;
1597bdfc8cc4SJamie Gritton 				continue;
1598bdfc8cc4SJamie Gritton 			}
1599bdfc8cc4SJamie Gritton #endif
16000304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
16010304c731SJamie Gritton 				redo_ip6 = 1;
16020304c731SJamie Gritton 				descend = 0;
16030304c731SJamie Gritton 			}
16040304c731SJamie Gritton 		}
16050304c731SJamie Gritton 	}
1606b38ff370SJamie Gritton #endif
16070304c731SJamie Gritton 	if (gotslevel) {
1608b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
16090304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
16100304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16110304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
16120304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
16130304c731SJamie Gritton 	}
1614b97457e2SJamie Gritton 	if (gotchildmax) {
1615b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1616b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1617b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1618b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1619b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1620b97457e2SJamie Gritton 				    ? childmax - level : 0;
1621b97457e2SJamie Gritton 	}
16220304c731SJamie Gritton 	if (gotenforce) {
16230304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
16240304c731SJamie Gritton 		/* Pass this restriction on to the children. */
16250304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16260304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
16270304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
16280304c731SJamie Gritton 	}
16290cc207a6SMartin Matuska 	if (gotrsnum) {
16300cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
16310cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
16320cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16330cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
16340cc207a6SMartin Matuska 	}
1635b6f47c23SJamie Gritton 	if (namelc != NULL) {
16360304c731SJamie Gritton 		if (ppr == &prison0)
1637b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
16380304c731SJamie Gritton 		else
16390304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1640b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
16410304c731SJamie Gritton 		/* Change this component of child names. */
16420304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16430304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
16440304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
16450304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
16460304c731SJamie Gritton 		}
16470304c731SJamie Gritton 	}
1648b38ff370SJamie Gritton 	if (path != NULL) {
16490304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1650f6e633a9SMartin Matuska 		if (fullpath_disabled && path[0] == '/' &&
1651f6e633a9SMartin Matuska 		    strcmp(mypr->pr_path, "/"))
16520304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
16530304c731SJamie Gritton 			    mypr->pr_path, path);
16540304c731SJamie Gritton 		else
1655b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1656b38ff370SJamie Gritton 		pr->pr_root = root;
1657b38ff370SJamie Gritton 	}
165876ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
165976ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
166076ca6f88SJamie Gritton 			/*
166176ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
166276ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
166376ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
166476ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
166576ca6f88SJamie Gritton 			 */
1666c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1667c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1668c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1669c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1670c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1671c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
167276ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
167376ca6f88SJamie Gritton 		}
167476ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
167576ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1676b38ff370SJamie Gritton 		if (host != NULL)
1677c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
167876ca6f88SJamie Gritton 		if (domain != NULL)
1679c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1680c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
168176ca6f88SJamie Gritton 		if (uuid != NULL)
1682c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
168376ca6f88SJamie Gritton 		if (gothid)
168476ca6f88SJamie Gritton 			pr->pr_hostid = hid;
168576ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
168676ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
168776ca6f88SJamie Gritton 				descend = 0;
168876ca6f88SJamie Gritton 			else {
168976ca6f88SJamie Gritton 				if (host != NULL)
1690c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1691c1f19219SJamie Gritton 					    pr->pr_hostname,
1692c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
169376ca6f88SJamie Gritton 				if (domain != NULL)
1694c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1695c1f19219SJamie Gritton 					    pr->pr_domainname,
1696c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
169776ca6f88SJamie Gritton 				if (uuid != NULL)
1698c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1699c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1700c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
170176ca6f88SJamie Gritton 				if (gothid)
170276ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
170376ca6f88SJamie Gritton 			}
170476ca6f88SJamie Gritton 		}
170576ca6f88SJamie Gritton 	}
17060304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
17070304c731SJamie Gritton 		/* Clear allow bits in all children. */
17080304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
17090304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
17100304c731SJamie Gritton 	}
17110304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1712b38ff370SJamie Gritton 	/*
1713b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1714b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1715b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1716b38ff370SJamie Gritton 	 * handlers have completed.
1717b38ff370SJamie Gritton 	 */
1718cc5fd8c7SJamie Gritton 	born = pr->pr_uref == 0;
1719b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1720b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1721b38ff370SJamie Gritton 			pr->pr_ref++;
1722b38ff370SJamie Gritton 			pr->pr_uref++;
1723b38ff370SJamie Gritton 		} else {
1724b38ff370SJamie Gritton 			pr->pr_ref--;
1725b38ff370SJamie Gritton 			pr->pr_uref--;
1726b38ff370SJamie Gritton 		}
1727b38ff370SJamie Gritton 	}
1728b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1729b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1730b38ff370SJamie Gritton 
1731a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
17324b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
1733a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
1734a7ad07bfSEdward Tomasz Napierala #endif
1735a7ad07bfSEdward Tomasz Napierala 
17360304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
17370304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
17380304c731SJamie Gritton 	 */
17390304c731SJamie Gritton #ifdef INET
17400304c731SJamie Gritton 	while (redo_ip4) {
17410304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
17420304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
17430304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17440304c731SJamie Gritton 		redo_ip4 = 0;
17450304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1746bdfc8cc4SJamie Gritton #ifdef VIMAGE
1747bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1748bdfc8cc4SJamie Gritton 				descend = 0;
1749bdfc8cc4SJamie Gritton 				continue;
1750bdfc8cc4SJamie Gritton 			}
1751bdfc8cc4SJamie Gritton #endif
17520304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
17530304c731SJamie Gritton 				if (ip4 != NULL)
17540304c731SJamie Gritton 					ip4 = NULL;
17550304c731SJamie Gritton 				else
17560304c731SJamie Gritton 					redo_ip4 = 1;
17570304c731SJamie Gritton 			}
17580304c731SJamie Gritton 		}
17590304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17600304c731SJamie Gritton 	}
17610304c731SJamie Gritton #endif
17620304c731SJamie Gritton #ifdef INET6
17630304c731SJamie Gritton 	while (redo_ip6) {
17640304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
17650304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
17660304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17670304c731SJamie Gritton 		redo_ip6 = 0;
17680304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1769bdfc8cc4SJamie Gritton #ifdef VIMAGE
1770bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1771bdfc8cc4SJamie Gritton 				descend = 0;
1772bdfc8cc4SJamie Gritton 				continue;
1773bdfc8cc4SJamie Gritton 			}
1774bdfc8cc4SJamie Gritton #endif
17750304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
17760304c731SJamie Gritton 				if (ip6 != NULL)
17770304c731SJamie Gritton 					ip6 = NULL;
17780304c731SJamie Gritton 				else
17790304c731SJamie Gritton 					redo_ip6 = 1;
17800304c731SJamie Gritton 			}
17810304c731SJamie Gritton 		}
17820304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17830304c731SJamie Gritton 	}
17840304c731SJamie Gritton #endif
17850304c731SJamie Gritton 
1786b38ff370SJamie Gritton 	/* Let the modules do their work. */
1787b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1788cc5fd8c7SJamie Gritton 	if (born) {
1789b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1790b38ff370SJamie Gritton 		if (error) {
1791cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1792cc5fd8c7SJamie Gritton 			prison_deref(pr, created
1793cc5fd8c7SJamie Gritton 			    ? PD_LIST_SLOCKED
1794cc5fd8c7SJamie Gritton 			    : PD_DEREF | PD_LIST_SLOCKED);
1795b38ff370SJamie Gritton 			goto done_errmsg;
1796b38ff370SJamie Gritton 		}
1797b38ff370SJamie Gritton 	}
1798b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1799b38ff370SJamie Gritton 	if (error) {
1800cc5fd8c7SJamie Gritton 		if (born)
1801cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1802b38ff370SJamie Gritton 		prison_deref(pr, created
1803b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1804b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1805b38ff370SJamie Gritton 		goto done_errmsg;
1806b38ff370SJamie Gritton 	}
1807b38ff370SJamie Gritton 
1808b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1809b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1810b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1811b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1812b38ff370SJamie Gritton 		if (error) {
1813b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1814b38ff370SJamie Gritton 			if (!created)
1815b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1816b38ff370SJamie Gritton 			goto done_errmsg;
1817b38ff370SJamie Gritton 		}
1818b38ff370SJamie Gritton 	}
1819b38ff370SJamie Gritton 
18201fb24974SEdward Tomasz Napierala #ifdef RACCT
18214b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
1822f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18231fb24974SEdward Tomasz Napierala 			sx_sunlock(&allprison_lock);
18241fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
1825f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18261fb24974SEdward Tomasz Napierala 			sx_slock(&allprison_lock);
18271fb24974SEdward Tomasz Napierala 	}
18281fb24974SEdward Tomasz Napierala #endif
18291fb24974SEdward Tomasz Napierala 
18301fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
18311fb24974SEdward Tomasz Napierala 
1832b38ff370SJamie Gritton 	/*
1833b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1834b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1835b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1836b38ff370SJamie Gritton 	 * visible).
1837b38ff370SJamie Gritton 	 */
1838b38ff370SJamie Gritton 	if (!created) {
1839b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1840b38ff370SJamie Gritton 		    ? PD_DEREF
1841b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1842b38ff370SJamie Gritton 	} else {
1843b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1844b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1845b38ff370SJamie Gritton 			pr->pr_ref++;
1846b38ff370SJamie Gritton 			pr->pr_uref++;
1847b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1848b38ff370SJamie Gritton 		}
1849b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1850b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1851b38ff370SJamie Gritton 	}
1852c34bbd2aSEdward Tomasz Napierala 
1853cc5fd8c7SJamie Gritton 	goto done_free;
1854b38ff370SJamie Gritton 
18550304c731SJamie Gritton  done_deref_locked:
18560304c731SJamie Gritton 	prison_deref(pr, created
18570304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
18580304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
18590304c731SJamie Gritton 	goto done_releroot;
1860b38ff370SJamie Gritton  done_unlock_list:
1861b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1862b38ff370SJamie Gritton  done_releroot:
18635050aa86SKonstantin Belousov 	if (root != NULL)
1864b38ff370SJamie Gritton 		vrele(root);
1865b38ff370SJamie Gritton  done_errmsg:
1866b38ff370SJamie Gritton 	if (error) {
1867176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1868176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
1869b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1870b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
1871b38ff370SJamie Gritton 				bcopy(errmsg,
1872b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1873b38ff370SJamie Gritton 				    errmsg_len);
1874b38ff370SJamie Gritton 			else
1875b38ff370SJamie Gritton 				copyout(errmsg,
1876b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1877b38ff370SJamie Gritton 				    errmsg_len);
1878b38ff370SJamie Gritton 		}
1879b38ff370SJamie Gritton 	}
1880b38ff370SJamie Gritton  done_free:
1881b38ff370SJamie Gritton #ifdef INET
1882b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1883b38ff370SJamie Gritton #endif
1884b38ff370SJamie Gritton #ifdef INET6
1885b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1886b38ff370SJamie Gritton #endif
18876dfe0a3dSMartin Matuska 	if (g_path != NULL)
18886dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
1889b38ff370SJamie Gritton 	vfs_freeopts(opts);
1890b38ff370SJamie Gritton 	return (error);
1891b38ff370SJamie Gritton }
1892b38ff370SJamie Gritton 
1893b38ff370SJamie Gritton 
1894b38ff370SJamie Gritton /*
1895b38ff370SJamie Gritton  * struct jail_get_args {
1896b38ff370SJamie Gritton  *	struct iovec *iovp;
1897b38ff370SJamie Gritton  *	unsigned int iovcnt;
1898b38ff370SJamie Gritton  *	int flags;
1899b38ff370SJamie Gritton  * };
1900b38ff370SJamie Gritton  */
1901b38ff370SJamie Gritton int
19028451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
1903b38ff370SJamie Gritton {
1904b38ff370SJamie Gritton 	struct uio *auio;
1905b38ff370SJamie Gritton 	int error;
1906b38ff370SJamie Gritton 
1907b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1908b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1909b38ff370SJamie Gritton 		return (EINVAL);
1910b38ff370SJamie Gritton 
1911b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1912b38ff370SJamie Gritton 	if (error)
1913b38ff370SJamie Gritton 		return (error);
1914b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1915b38ff370SJamie Gritton 	if (error == 0)
1916b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1917b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1918b38ff370SJamie Gritton 	free(auio, M_IOV);
1919b38ff370SJamie Gritton 	return (error);
1920b38ff370SJamie Gritton }
1921b38ff370SJamie Gritton 
1922b38ff370SJamie Gritton int
1923b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1924b38ff370SJamie Gritton {
1925672756aaSJamie Gritton 	struct bool_flags *bf;
1926672756aaSJamie Gritton 	struct jailsys_flags *jsf;
19270304c731SJamie Gritton 	struct prison *pr, *mypr;
1928b38ff370SJamie Gritton 	struct vfsopt *opt;
1929b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1930b38ff370SJamie Gritton 	char *errmsg, *name;
1931672756aaSJamie Gritton 	int error, errmsg_len, errmsg_pos, i, jid, len, locked, pos;
1932672756aaSJamie Gritton 	unsigned f;
1933b38ff370SJamie Gritton 
1934b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1935b38ff370SJamie Gritton 		return (EINVAL);
1936b38ff370SJamie Gritton 
1937b38ff370SJamie Gritton 	/* Get the parameter list. */
1938b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1939b38ff370SJamie Gritton 	if (error)
1940b38ff370SJamie Gritton 		return (error);
1941b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
19420304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
19431e2a13e6SJamie Gritton 
1944b38ff370SJamie Gritton 	/*
1945b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
1946b38ff370SJamie Gritton 	 */
1947b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
1948b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1949b38ff370SJamie Gritton 	if (error == 0) {
1950b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
19510304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1952b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
1953b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
1954b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1955b38ff370SJamie Gritton 					break;
1956b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1957b38ff370SJamie Gritton 			}
1958b38ff370SJamie Gritton 		}
1959b38ff370SJamie Gritton 		if (pr != NULL)
1960b38ff370SJamie Gritton 			goto found_prison;
1961b38ff370SJamie Gritton 		error = ENOENT;
1962b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
1963b38ff370SJamie Gritton 		goto done_unlock_list;
1964b38ff370SJamie Gritton 	} else if (error != ENOENT)
1965b38ff370SJamie Gritton 		goto done_unlock_list;
1966b38ff370SJamie Gritton 
1967b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1968b38ff370SJamie Gritton 	if (error == 0) {
1969b38ff370SJamie Gritton 		if (jid != 0) {
19700304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
1971b38ff370SJamie Gritton 			if (pr != NULL) {
1972b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1973b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1974b38ff370SJamie Gritton 					error = ENOENT;
1975b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1976b38ff370SJamie Gritton 					    jid);
1977b38ff370SJamie Gritton 					goto done_unlock_list;
1978b38ff370SJamie Gritton 				}
1979b38ff370SJamie Gritton 				goto found_prison;
1980b38ff370SJamie Gritton 			}
1981b38ff370SJamie Gritton 			error = ENOENT;
1982b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
1983b38ff370SJamie Gritton 			goto done_unlock_list;
1984b38ff370SJamie Gritton 		}
1985b38ff370SJamie Gritton 	} else if (error != ENOENT)
1986b38ff370SJamie Gritton 		goto done_unlock_list;
1987b38ff370SJamie Gritton 
1988b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1989b38ff370SJamie Gritton 	if (error == 0) {
1990b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1991b38ff370SJamie Gritton 			error = EINVAL;
1992b38ff370SJamie Gritton 			goto done_unlock_list;
1993b38ff370SJamie Gritton 		}
19940304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
1995b38ff370SJamie Gritton 		if (pr != NULL) {
1996b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1997b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1998b38ff370SJamie Gritton 				error = ENOENT;
1999b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2000b38ff370SJamie Gritton 				    name);
2001b38ff370SJamie Gritton 				goto done_unlock_list;
2002b38ff370SJamie Gritton 			}
2003b38ff370SJamie Gritton 			goto found_prison;
2004b38ff370SJamie Gritton 		}
2005b38ff370SJamie Gritton 		error = ENOENT;
2006b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
2007b38ff370SJamie Gritton 		goto done_unlock_list;
2008b38ff370SJamie Gritton 	} else if (error != ENOENT)
2009b38ff370SJamie Gritton 		goto done_unlock_list;
2010b38ff370SJamie Gritton 
2011b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2012b38ff370SJamie Gritton 	error = ENOENT;
2013b38ff370SJamie Gritton 	goto done_unlock_list;
2014b38ff370SJamie Gritton 
2015b38ff370SJamie Gritton  found_prison:
2016b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
2017b38ff370SJamie Gritton 	pr->pr_ref++;
2018b38ff370SJamie Gritton 	locked = PD_LOCKED;
2019b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2020b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2021b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2022b38ff370SJamie Gritton 		goto done_deref;
20230304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
20240304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2025b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2026b38ff370SJamie Gritton 		goto done_deref;
20270304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
20280304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20290304c731SJamie Gritton 		goto done_deref;
20300304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2031b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2032b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2033b38ff370SJamie Gritton 		goto done_deref;
20340304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2035b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2036b38ff370SJamie Gritton 		goto done_deref;
2037b38ff370SJamie Gritton #ifdef INET
2038b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2039b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2040b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2041b38ff370SJamie Gritton 		goto done_deref;
2042b38ff370SJamie Gritton #endif
2043b38ff370SJamie Gritton #ifdef INET6
2044b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2045b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2046b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2047b38ff370SJamie Gritton 		goto done_deref;
2048b38ff370SJamie Gritton #endif
2049b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2050b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2051b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2052b38ff370SJamie Gritton 		goto done_deref;
2053b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2054b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2055b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2056b97457e2SJamie Gritton 		goto done_deref;
2057b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2058b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2059b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2060b97457e2SJamie Gritton 		goto done_deref;
2061c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2062b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2063b38ff370SJamie Gritton 		goto done_deref;
2064c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
206576ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
206676ca6f88SJamie Gritton 		goto done_deref;
2067c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
206876ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
206976ca6f88SJamie Gritton 		goto done_deref;
2070841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2071a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
207276ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
207376ca6f88SJamie Gritton 
207476ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
207576ca6f88SJamie Gritton 	} else
207676ca6f88SJamie Gritton #endif
207776ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
207876ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
207976ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
208076ca6f88SJamie Gritton 		goto done_deref;
20810304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
20820304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
20830304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20840304c731SJamie Gritton 		goto done_deref;
20850cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
20860cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
20870cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
20880cc207a6SMartin Matuska 		goto done_deref;
2089672756aaSJamie Gritton 	for (bf = pr_flag_bool;
2090672756aaSJamie Gritton 	     bf < pr_flag_bool + nitems(pr_flag_bool);
2091672756aaSJamie Gritton 	     bf++) {
2092672756aaSJamie Gritton 		i = (pr->pr_flags & bf->flag) ? 1 : 0;
2093672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2094b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2095b38ff370SJamie Gritton 			goto done_deref;
2096b38ff370SJamie Gritton 		i = !i;
2097672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2098b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2099b38ff370SJamie Gritton 			goto done_deref;
21000304c731SJamie Gritton 	}
2101672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
2102672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2103672756aaSJamie Gritton 	     jsf++) {
2104672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
2105672756aaSJamie Gritton 		i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2106672756aaSJamie Gritton 		    : (f == jsf->new) ? JAIL_SYS_NEW
21077cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
2108672756aaSJamie Gritton 		error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
21097cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
21107cbf7213SJamie Gritton 			goto done_deref;
21117cbf7213SJamie Gritton 	}
2112672756aaSJamie Gritton 	for (bf = pr_flag_allow;
21130e5c6bd4SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0;
2114672756aaSJamie Gritton 	     bf++) {
2115672756aaSJamie Gritton 		i = (pr->pr_allow & bf->flag) ? 1 : 0;
2116672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->name, &i, sizeof(i));
21170304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21180304c731SJamie Gritton 			goto done_deref;
21190304c731SJamie Gritton 		i = !i;
2120672756aaSJamie Gritton 		error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
21210304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21220304c731SJamie Gritton 			goto done_deref;
21230304c731SJamie Gritton 	}
2124b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
2125b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2126b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2127b38ff370SJamie Gritton 		goto done_deref;
2128b38ff370SJamie Gritton 	i = !i;
2129b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2130b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2131b38ff370SJamie Gritton 		goto done_deref;
2132bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2133bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2134a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
2135a1a4c1b0SIan Lepore 		goto done_deref;
2136a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2137a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
2138a1a4c1b0SIan Lepore 		goto done_deref;
2139b38ff370SJamie Gritton 
2140b38ff370SJamie Gritton 	/* Get the module parameters. */
2141b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2142b38ff370SJamie Gritton 	locked = 0;
2143b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2144b38ff370SJamie Gritton 	if (error)
2145b38ff370SJamie Gritton 		goto done_deref;
2146b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2147b38ff370SJamie Gritton 
2148b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2149b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2150b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2151b38ff370SJamie Gritton 			error = EINVAL;
2152b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2153b38ff370SJamie Gritton 			goto done_errmsg;
2154b38ff370SJamie Gritton 		}
2155b38ff370SJamie Gritton 	}
2156b38ff370SJamie Gritton 
2157b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2158b38ff370SJamie Gritton 	error = 0;
2159b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2160b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2161b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2162b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2163b38ff370SJamie Gritton 			if (opt->value != NULL) {
2164b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2165b38ff370SJamie Gritton 					bcopy(opt->value,
2166b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2167b38ff370SJamie Gritton 					    opt->len);
2168b38ff370SJamie Gritton 				} else {
2169b38ff370SJamie Gritton 					error = copyout(opt->value,
2170b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2171b38ff370SJamie Gritton 					    opt->len);
2172b38ff370SJamie Gritton 					if (error)
2173b38ff370SJamie Gritton 						break;
2174b38ff370SJamie Gritton 				}
2175b38ff370SJamie Gritton 			}
2176b38ff370SJamie Gritton 		}
2177b38ff370SJamie Gritton 	}
2178b38ff370SJamie Gritton 	goto done_errmsg;
2179b38ff370SJamie Gritton 
2180b38ff370SJamie Gritton  done_deref:
2181b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2182b38ff370SJamie Gritton 	goto done_errmsg;
2183b38ff370SJamie Gritton 
2184b38ff370SJamie Gritton  done_unlock_list:
2185b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2186b38ff370SJamie Gritton  done_errmsg:
2187b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2188b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2189b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2190b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2191b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2192b38ff370SJamie Gritton 				bcopy(errmsg,
2193b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2194b38ff370SJamie Gritton 				    errmsg_len);
2195b38ff370SJamie Gritton 			else
2196b38ff370SJamie Gritton 				copyout(errmsg,
2197b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2198b38ff370SJamie Gritton 				    errmsg_len);
2199b38ff370SJamie Gritton 		}
2200b38ff370SJamie Gritton 	}
2201b38ff370SJamie Gritton 	vfs_freeopts(opts);
2202b38ff370SJamie Gritton 	return (error);
2203b38ff370SJamie Gritton }
2204b38ff370SJamie Gritton 
22050304c731SJamie Gritton 
2206b38ff370SJamie Gritton /*
2207b38ff370SJamie Gritton  * struct jail_remove_args {
2208b38ff370SJamie Gritton  *	int jid;
2209b38ff370SJamie Gritton  * };
2210b38ff370SJamie Gritton  */
2211b38ff370SJamie Gritton int
22128451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2213b38ff370SJamie Gritton {
22140304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
22150304c731SJamie Gritton 	int descend, error;
2216b38ff370SJamie Gritton 
2217b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2218b38ff370SJamie Gritton 	if (error)
2219b38ff370SJamie Gritton 		return (error);
2220b38ff370SJamie Gritton 
2221b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
22220304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2223b38ff370SJamie Gritton 	if (pr == NULL) {
2224b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2225b38ff370SJamie Gritton 		return (EINVAL);
2226b38ff370SJamie Gritton 	}
2227b38ff370SJamie Gritton 
22280304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
22290304c731SJamie Gritton 	pr->pr_ref++;
22300304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
22310304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
22320304c731SJamie Gritton 		lpr = NULL;
22330304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
22340304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
22350304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
22360304c731SJamie Gritton 				tpr = cpr;
22370304c731SJamie Gritton 				cpr->pr_ref++;
22380304c731SJamie Gritton 			} else {
22390304c731SJamie Gritton 				/* Already removed - do not do it again. */
22400304c731SJamie Gritton 				tpr = NULL;
22410304c731SJamie Gritton 			}
22420304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
22430304c731SJamie Gritton 			if (lpr != NULL) {
22440304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
22450304c731SJamie Gritton 				prison_remove_one(lpr);
22460304c731SJamie Gritton 				sx_xlock(&allprison_lock);
22470304c731SJamie Gritton 			}
22480304c731SJamie Gritton 			lpr = tpr;
22490304c731SJamie Gritton 		}
22500304c731SJamie Gritton 		if (lpr != NULL) {
22510304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
22520304c731SJamie Gritton 			prison_remove_one(lpr);
22530304c731SJamie Gritton 			sx_xlock(&allprison_lock);
22540304c731SJamie Gritton 		}
22550304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
22560304c731SJamie Gritton 	}
22570304c731SJamie Gritton 	prison_remove_one(pr);
22580304c731SJamie Gritton 	return (0);
22590304c731SJamie Gritton }
22600304c731SJamie Gritton 
22610304c731SJamie Gritton static void
22620304c731SJamie Gritton prison_remove_one(struct prison *pr)
22630304c731SJamie Gritton {
22640304c731SJamie Gritton 	struct proc *p;
22650304c731SJamie Gritton 	int deuref;
22660304c731SJamie Gritton 
2267b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2268b38ff370SJamie Gritton 	deuref = 0;
2269b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2270b38ff370SJamie Gritton 		pr->pr_ref--;
2271b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2272b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2273b38ff370SJamie Gritton 	}
2274b38ff370SJamie Gritton 
22750304c731SJamie Gritton 	/*
22760304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
22770304c731SJamie Gritton 	 * the prison now.
22780304c731SJamie Gritton 	 */
22790304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
22800304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
22810304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2282b38ff370SJamie Gritton 		prison_deref(pr,
2283b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
22840304c731SJamie Gritton 		return;
2285b38ff370SJamie Gritton 	}
2286b38ff370SJamie Gritton 
2287b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2288b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2289b38ff370SJamie Gritton 	/*
2290b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2291b38ff370SJamie Gritton 	 */
2292b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
22937938a442SBjoern A. Zeeb 	FOREACH_PROC_IN_SYSTEM(p) {
2294b38ff370SJamie Gritton 		PROC_LOCK(p);
2295b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2296b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
22978451d0ddSKip Macy 			kern_psignal(p, SIGKILL);
2298b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2299b38ff370SJamie Gritton 	}
2300b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
23010304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2302b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
230375c13541SPoul-Henning Kamp }
230475c13541SPoul-Henning Kamp 
23058571af59SJamie Gritton 
2306fd7a8150SMike Barcroft /*
23079ddb7954SMike Barcroft  * struct jail_attach_args {
23089ddb7954SMike Barcroft  *	int jid;
23099ddb7954SMike Barcroft  * };
2310fd7a8150SMike Barcroft  */
2311fd7a8150SMike Barcroft int
23128451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2313fd7a8150SMike Barcroft {
2314b38ff370SJamie Gritton 	struct prison *pr;
2315b38ff370SJamie Gritton 	int error;
2316b38ff370SJamie Gritton 
2317b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2318b38ff370SJamie Gritton 	if (error)
2319b38ff370SJamie Gritton 		return (error);
2320b38ff370SJamie Gritton 
2321ef0ddea3SJamie Gritton 	/*
2322ef0ddea3SJamie Gritton 	 * Start with exclusive hold on allprison_lock to ensure that a possible
2323ef0ddea3SJamie Gritton 	 * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove.
2324ef0ddea3SJamie Gritton 	 * But then immediately downgrade it since we don't need to stop
2325ef0ddea3SJamie Gritton 	 * readers.
2326ef0ddea3SJamie Gritton 	 */
2327ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
2328ef0ddea3SJamie Gritton 	sx_downgrade(&allprison_lock);
23290304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2330b38ff370SJamie Gritton 	if (pr == NULL) {
2331b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2332b38ff370SJamie Gritton 		return (EINVAL);
2333b38ff370SJamie Gritton 	}
2334b38ff370SJamie Gritton 
2335b38ff370SJamie Gritton 	/*
2336b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2337b38ff370SJamie Gritton 	 * considered to be "alive".
2338b38ff370SJamie Gritton 	 */
2339b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2340b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2341b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2342b38ff370SJamie Gritton 		return (EINVAL);
2343b38ff370SJamie Gritton 	}
2344b38ff370SJamie Gritton 
2345b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2346b38ff370SJamie Gritton }
2347b38ff370SJamie Gritton 
2348b38ff370SJamie Gritton static int
2349b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2350b38ff370SJamie Gritton {
2351fd7a8150SMike Barcroft 	struct proc *p;
2352fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
23535050aa86SKonstantin Belousov 	int error;
2354fd7a8150SMike Barcroft 
235557f22bd4SJacques Vidrine 	/*
235657f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
235757f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
235857f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
235957f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
236057f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
236157f22bd4SJacques Vidrine 	 * of another.
236257f22bd4SJacques Vidrine 	 */
2363fd7a8150SMike Barcroft 	pr->pr_ref++;
2364b38ff370SJamie Gritton 	pr->pr_uref++;
2365fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2366b38ff370SJamie Gritton 
2367b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2368b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2369b38ff370SJamie Gritton 	if (error) {
2370b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2371b38ff370SJamie Gritton 		return (error);
2372b38ff370SJamie Gritton 	}
2373dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2374fd7a8150SMike Barcroft 
2375413628a7SBjoern A. Zeeb 	/*
2376413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2377413628a7SBjoern A. Zeeb 	 */
2378b38ff370SJamie Gritton 	p = td->td_proc;
2379413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2380413628a7SBjoern A. Zeeb 	if (error)
2381b38ff370SJamie Gritton 		goto e_revert_osd;
2382413628a7SBjoern A. Zeeb 
2383cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2384fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2385fd7a8150SMike Barcroft 		goto e_unlock;
2386fd7a8150SMike Barcroft #ifdef MAC
238730d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2388fd7a8150SMike Barcroft 		goto e_unlock;
2389fd7a8150SMike Barcroft #endif
239022db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2391f0725a8eSMateusz Guzik 	if ((error = pwd_chroot(td, pr->pr_root)))
23925050aa86SKonstantin Belousov 		goto e_revert_osd;
2393fd7a8150SMike Barcroft 
2394fd7a8150SMike Barcroft 	newcred = crget();
2395fd7a8150SMike Barcroft 	PROC_LOCK(p);
23961fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
239769c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2398daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
23991fb6767dSJamie Gritton 	setsugid(p);
2400097055e2SEdward Tomasz Napierala #ifdef RACCT
2401097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2402f87beb93SAndriy Gapon 	crhold(newcred);
2403f87beb93SAndriy Gapon #endif
2404f87beb93SAndriy Gapon 	PROC_UNLOCK(p);
2405f87beb93SAndriy Gapon #ifdef RCTL
2406f87beb93SAndriy Gapon 	rctl_proc_ucred_changed(p, newcred);
2407f87beb93SAndriy Gapon 	crfree(newcred);
2408097055e2SEdward Tomasz Napierala #endif
24091fb6767dSJamie Gritton 	prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
2410fd7a8150SMike Barcroft 	crfree(oldcred);
2411fd7a8150SMike Barcroft 	return (0);
24121fb6767dSJamie Gritton 
2413fd7a8150SMike Barcroft  e_unlock:
241422db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2415b38ff370SJamie Gritton  e_revert_osd:
2416b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
24171fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2418b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2419fd7a8150SMike Barcroft 	return (error);
2420fd7a8150SMike Barcroft }
2421fd7a8150SMike Barcroft 
24220304c731SJamie Gritton 
2423fd7a8150SMike Barcroft /*
2424fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2425fd7a8150SMike Barcroft  */
242654b369c1SPawel Jakub Dawidek struct prison *
2427fd7a8150SMike Barcroft prison_find(int prid)
2428fd7a8150SMike Barcroft {
2429fd7a8150SMike Barcroft 	struct prison *pr;
2430fd7a8150SMike Barcroft 
2431dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2432b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2433fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2434fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2435b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2436fd7a8150SMike Barcroft 				return (pr);
2437b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2438fd7a8150SMike Barcroft 		}
2439fd7a8150SMike Barcroft 	}
2440fd7a8150SMike Barcroft 	return (NULL);
2441fd7a8150SMike Barcroft }
2442fd7a8150SMike Barcroft 
2443b38ff370SJamie Gritton /*
24440304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2445b38ff370SJamie Gritton  */
2446b38ff370SJamie Gritton struct prison *
24470304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2448b38ff370SJamie Gritton {
24490304c731SJamie Gritton 	struct prison *pr;
24500304c731SJamie Gritton 	int descend;
2451b38ff370SJamie Gritton 
2452b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24530304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24540304c731SJamie Gritton 		if (pr->pr_id == prid) {
24550304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
24560304c731SJamie Gritton 			if (pr->pr_ref > 0)
24570304c731SJamie Gritton 				return (pr);
24580304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
24590304c731SJamie Gritton 		}
24600304c731SJamie Gritton 	}
24610304c731SJamie Gritton 	return (NULL);
24620304c731SJamie Gritton }
24630304c731SJamie Gritton 
24640304c731SJamie Gritton /*
24650304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
24660304c731SJamie Gritton  */
24670304c731SJamie Gritton struct prison *
24680304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
24690304c731SJamie Gritton {
24700304c731SJamie Gritton 	struct prison *pr, *deadpr;
24710304c731SJamie Gritton 	size_t mylen;
24720304c731SJamie Gritton 	int descend;
24730304c731SJamie Gritton 
24740304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24750304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2476b38ff370SJamie Gritton  again:
2477b38ff370SJamie Gritton 	deadpr = NULL;
24780304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24790304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2480b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2481b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2482b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2483b38ff370SJamie Gritton 					return (pr);
2484b38ff370SJamie Gritton 				deadpr = pr;
2485b38ff370SJamie Gritton 			}
2486b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2487b38ff370SJamie Gritton 		}
2488b38ff370SJamie Gritton 	}
24890304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2490b38ff370SJamie Gritton 	if (deadpr != NULL) {
2491b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2492b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2493b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2494b38ff370SJamie Gritton 			goto again;
2495b38ff370SJamie Gritton 		}
2496b38ff370SJamie Gritton 	}
2497b38ff370SJamie Gritton 	return (deadpr);
2498b38ff370SJamie Gritton }
2499b38ff370SJamie Gritton 
2500b38ff370SJamie Gritton /*
25010304c731SJamie Gritton  * See if a prison has the specific flag set.
25020304c731SJamie Gritton  */
25030304c731SJamie Gritton int
25040304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
25050304c731SJamie Gritton {
25060304c731SJamie Gritton 
25070304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
25080304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
25090304c731SJamie Gritton }
25100304c731SJamie Gritton 
25110304c731SJamie Gritton int
25120304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
25130304c731SJamie Gritton {
25140304c731SJamie Gritton 
25150304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
25160304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
25170304c731SJamie Gritton }
25180304c731SJamie Gritton 
25190304c731SJamie Gritton /*
2520b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2521b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2522b38ff370SJamie Gritton  */
252391421ba2SRobert Watson void
25241ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
252591421ba2SRobert Watson {
252673d9e52dSJamie Gritton 	int ref;
252791421ba2SRobert Watson 
25281ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
252973d9e52dSJamie Gritton 	ref = --pr->pr_ref;
253001137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
253173d9e52dSJamie Gritton 	if (ref == 0)
2532c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2533c2cda609SPawel Jakub Dawidek }
2534c2cda609SPawel Jakub Dawidek 
25351ba4a712SPawel Jakub Dawidek void
25361ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
25371ba4a712SPawel Jakub Dawidek {
25381ba4a712SPawel Jakub Dawidek 
25391ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
25401ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
25411ba4a712SPawel Jakub Dawidek }
25421ba4a712SPawel Jakub Dawidek 
254373d9e52dSJamie Gritton /*
254473d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
254573d9e52dSJamie Gritton  */
2546c2cda609SPawel Jakub Dawidek static void
2547c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2548c2cda609SPawel Jakub Dawidek {
254973d9e52dSJamie Gritton 	struct prison *pr = context;
2550b38ff370SJamie Gritton 
2551ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
255273d9e52dSJamie Gritton 	mtx_lock(&pr->pr_mtx);
255373d9e52dSJamie Gritton 	prison_deref(pr, pr->pr_uref
2554ef0ddea3SJamie Gritton 	    ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED
2555ef0ddea3SJamie Gritton 	    : PD_LOCKED | PD_LIST_XLOCKED);
2556b38ff370SJamie Gritton }
2557b38ff370SJamie Gritton 
2558b38ff370SJamie Gritton /*
2559b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2560b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2561b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2562b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2563b38ff370SJamie Gritton  */
2564b38ff370SJamie Gritton static void
2565b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2566b38ff370SJamie Gritton {
25670304c731SJamie Gritton 	struct prison *ppr, *tpr;
2568cc5fd8c7SJamie Gritton 	int ref, lasturef;
2569c2cda609SPawel Jakub Dawidek 
2570b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2571b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
25720304c731SJamie Gritton 	for (;;) {
2573e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
257473d9e52dSJamie Gritton 			KASSERT(pr->pr_uref > 0,
257573d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
257673d9e52dSJamie Gritton 			     pr->pr_id));
2577e6d5cb63SJamie Gritton 			pr->pr_uref--;
2578cc5fd8c7SJamie Gritton 			lasturef = pr->pr_uref == 0;
2579cc5fd8c7SJamie Gritton 			if (lasturef)
2580cc5fd8c7SJamie Gritton 				pr->pr_ref++;
2581e6d5cb63SJamie Gritton 			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2582cc5fd8c7SJamie Gritton 		} else
2583cc5fd8c7SJamie Gritton 			lasturef = 0;
258473d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
258573d9e52dSJamie Gritton 			KASSERT(pr->pr_ref > 0,
258673d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
258773d9e52dSJamie Gritton 			     pr->pr_id));
2588b38ff370SJamie Gritton 			pr->pr_ref--;
258973d9e52dSJamie Gritton 		}
2590cc5fd8c7SJamie Gritton 		ref = pr->pr_ref;
2591b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2592cc5fd8c7SJamie Gritton 
2593cc5fd8c7SJamie Gritton 		/*
2594cc5fd8c7SJamie Gritton 		 * Tell the modules if the last user reference was removed
2595cc5fd8c7SJamie Gritton 		 * (even it sticks around in dying state).
2596cc5fd8c7SJamie Gritton 		 */
2597cc5fd8c7SJamie Gritton 		if (lasturef) {
2598cc5fd8c7SJamie Gritton 			if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
2599cc5fd8c7SJamie Gritton 				sx_xlock(&allprison_lock);
2600cc5fd8c7SJamie Gritton 				flags |= PD_LIST_XLOCKED;
2601cc5fd8c7SJamie Gritton 			}
2602cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2603cc5fd8c7SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2604cc5fd8c7SJamie Gritton 			ref = --pr->pr_ref;
2605cc5fd8c7SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2606cc5fd8c7SJamie Gritton 		}
2607cc5fd8c7SJamie Gritton 
2608cc5fd8c7SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2609cc5fd8c7SJamie Gritton 		if (ref > 0) {
2610b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2611b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2612b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2613b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2614b38ff370SJamie Gritton 			return;
2615b38ff370SJamie Gritton 		}
2616c2cda609SPawel Jakub Dawidek 
2617b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2618b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2619b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2620c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2621b38ff370SJamie Gritton 			}
2622b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2623b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2624b38ff370SJamie Gritton 
2625b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
26260304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
26270304c731SJamie Gritton 		ppr = pr->pr_parent;
26280304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2629b97457e2SJamie Gritton 			tpr->pr_childcount--;
2630c4884ffaSJamie Gritton 		sx_xunlock(&allprison_lock);
26311ba4a712SPawel Jakub Dawidek 
2632679e1390SJamie Gritton #ifdef VIMAGE
26330cb8b6a9SMarko Zec 		if (pr->pr_vnet != ppr->pr_vnet)
2634679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2635679e1390SJamie Gritton #endif
26365050aa86SKonstantin Belousov 		if (pr->pr_root != NULL)
2637b3059e09SRobert Watson 			vrele(pr->pr_root);
2638b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2639413628a7SBjoern A. Zeeb #ifdef INET
2640413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2641413628a7SBjoern A. Zeeb #endif
2642b38ff370SJamie Gritton #ifdef INET6
2643b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2644b38ff370SJamie Gritton #endif
2645b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2646b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2647b38ff370SJamie Gritton 		osd_jail_exit(pr);
2648a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
26494b5c9cf6SEdward Tomasz Napierala 		if (racct_enable)
2650a7ad07bfSEdward Tomasz Napierala 			prison_racct_detach(pr);
2651ec125fbbSEdward Tomasz Napierala #endif
26521ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
26530304c731SJamie Gritton 
26540304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
26550304c731SJamie Gritton 		pr = ppr;
26560304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
2657e6d5cb63SJamie Gritton 		flags = PD_DEREF | PD_DEUREF;
26580304c731SJamie Gritton 	}
2659b3059e09SRobert Watson }
2660b3059e09SRobert Watson 
266191421ba2SRobert Watson void
26621ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
26631ba4a712SPawel Jakub Dawidek {
26641ba4a712SPawel Jakub Dawidek 
26651ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
26661ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
266736b41cc3SBjoern A. Zeeb 	    ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
26681ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
26691ba4a712SPawel Jakub Dawidek }
26701ba4a712SPawel Jakub Dawidek 
26711ba4a712SPawel Jakub Dawidek void
267291421ba2SRobert Watson prison_hold(struct prison *pr)
267391421ba2SRobert Watson {
267491421ba2SRobert Watson 
267501137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
26761ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
267701137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
267801137630SRobert Watson }
267901137630SRobert Watson 
2680413628a7SBjoern A. Zeeb void
2681413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
268201137630SRobert Watson {
268301137630SRobert Watson 
2684413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2685b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2686b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2687b38ff370SJamie Gritton 	pr->pr_uref++;
2688413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
268975c13541SPoul-Henning Kamp }
269075c13541SPoul-Henning Kamp 
269175c13541SPoul-Henning Kamp void
2692413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
269375c13541SPoul-Henning Kamp {
2694413628a7SBjoern A. Zeeb 
2695413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2696b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2697b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
269873d9e52dSJamie Gritton 	if (pr->pr_uref > 1)
269973d9e52dSJamie Gritton 		pr->pr_uref--;
270073d9e52dSJamie Gritton 	else {
270173d9e52dSJamie Gritton 		/*
270273d9e52dSJamie Gritton 		 * Don't remove the last user reference in this context, which
270373d9e52dSJamie Gritton 		 * is expected to be a process that is not only locked, but
270473d9e52dSJamie Gritton 		 * also half dead.
270573d9e52dSJamie Gritton 		 */
270673d9e52dSJamie Gritton 		pr->pr_ref++;
270773d9e52dSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
270873d9e52dSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
270973d9e52dSJamie Gritton 		return;
271073d9e52dSJamie Gritton 	}
271173d9e52dSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2712413628a7SBjoern A. Zeeb }
2713413628a7SBjoern A. Zeeb 
2714413628a7SBjoern A. Zeeb /*
2715ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
2716ca04ba64SJamie Gritton  *
2717ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
2718ca04ba64SJamie Gritton  * if not.
2719ca04ba64SJamie Gritton  */
2720ca04ba64SJamie Gritton int
2721ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
2722ca04ba64SJamie Gritton {
27230304c731SJamie Gritton 	struct prison *pr;
2724ca04ba64SJamie Gritton 	int error;
2725ca04ba64SJamie Gritton 
2726ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2727ca04ba64SJamie Gritton 
27280304c731SJamie Gritton 	pr = cred->cr_prison;
2729499650a0SJamie Gritton #ifdef VIMAGE
27306bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
2731de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
27326bb79563SJamie Gritton 		return (0);
2733499650a0SJamie Gritton #endif
27346bb79563SJamie Gritton 
2735ca04ba64SJamie Gritton 	error = 0;
2736ca04ba64SJamie Gritton 	switch (af)
2737ca04ba64SJamie Gritton 	{
2738ca04ba64SJamie Gritton #ifdef INET
2739ca04ba64SJamie Gritton 	case AF_INET:
27400304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
27410304c731SJamie Gritton 		{
27420304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27430304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
2744ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
27450304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
27460304c731SJamie Gritton 		}
2747ca04ba64SJamie Gritton 		break;
2748ca04ba64SJamie Gritton #endif
2749ca04ba64SJamie Gritton #ifdef INET6
2750ca04ba64SJamie Gritton 	case AF_INET6:
27510304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
27520304c731SJamie Gritton 		{
27530304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27540304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
2755ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
27560304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
27570304c731SJamie Gritton 		}
2758ca04ba64SJamie Gritton 		break;
2759ca04ba64SJamie Gritton #endif
2760ca04ba64SJamie Gritton 	case AF_LOCAL:
2761ca04ba64SJamie Gritton 	case AF_ROUTE:
2762ca04ba64SJamie Gritton 		break;
2763ca04ba64SJamie Gritton 	default:
27640304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
2765ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
2766ca04ba64SJamie Gritton 	}
2767ca04ba64SJamie Gritton 	return (error);
2768ca04ba64SJamie Gritton }
2769ca04ba64SJamie Gritton 
2770ca04ba64SJamie Gritton /*
2771413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
2772413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
2773413628a7SBjoern A. Zeeb  *
27740304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
27750304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
27760304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
2777413628a7SBjoern A. Zeeb  */
2778413628a7SBjoern A. Zeeb int
277991421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
278075c13541SPoul-Henning Kamp {
2781413628a7SBjoern A. Zeeb #ifdef INET
27829ddb7954SMike Barcroft 	struct sockaddr_in *sai;
2783413628a7SBjoern A. Zeeb #endif
2784413628a7SBjoern A. Zeeb #ifdef INET6
2785413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
2786413628a7SBjoern A. Zeeb #endif
2787b89e82ddSJamie Gritton 	int error;
278875c13541SPoul-Henning Kamp 
2789413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2790413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
2791413628a7SBjoern A. Zeeb 
2792de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
2793de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
2794de0bd6f7SBjoern A. Zeeb 		return (0);
2795de0bd6f7SBjoern A. Zeeb #endif
2796de0bd6f7SBjoern A. Zeeb 
2797b89e82ddSJamie Gritton 	error = 0;
2798413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
2799413628a7SBjoern A. Zeeb 	{
2800413628a7SBjoern A. Zeeb #ifdef INET
2801413628a7SBjoern A. Zeeb 	case AF_INET:
28029ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
2803b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
2804413628a7SBjoern A. Zeeb 		break;
2805413628a7SBjoern A. Zeeb #endif
2806413628a7SBjoern A. Zeeb #ifdef INET6
2807413628a7SBjoern A. Zeeb 	case AF_INET6:
2808413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
2809b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
2810413628a7SBjoern A. Zeeb 		break;
2811413628a7SBjoern A. Zeeb #endif
2812413628a7SBjoern A. Zeeb 	default:
28130304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
2814b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
2815413628a7SBjoern A. Zeeb 	}
2816b89e82ddSJamie Gritton 	return (error);
281775c13541SPoul-Henning Kamp }
281891421ba2SRobert Watson 
281991421ba2SRobert Watson /*
282091421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
282191421ba2SRobert Watson  */
282291421ba2SRobert Watson int
28239ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
282491421ba2SRobert Watson {
282591421ba2SRobert Watson 
28260304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
28270304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
28280304c731SJamie Gritton }
282991421ba2SRobert Watson 
28300304c731SJamie Gritton /*
28310304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
28320304c731SJamie Gritton  */
28330304c731SJamie Gritton int
28340304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
28350304c731SJamie Gritton {
28360304c731SJamie Gritton 
28370304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
28380304c731SJamie Gritton 		if (pr1 == pr2)
28390304c731SJamie Gritton 			return (1);
284091421ba2SRobert Watson 	return (0);
284191421ba2SRobert Watson }
284291421ba2SRobert Watson 
284391421ba2SRobert Watson /*
284491421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
284591421ba2SRobert Watson  */
284691421ba2SRobert Watson int
28479ddb7954SMike Barcroft jailed(struct ucred *cred)
284891421ba2SRobert Watson {
284991421ba2SRobert Watson 
28500304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
285191421ba2SRobert Watson }
28529484d0c0SRobert Drehmel 
28539484d0c0SRobert Drehmel /*
2854de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
2855de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
2856de0bd6f7SBjoern A. Zeeb  */
2857de0bd6f7SBjoern A. Zeeb int
2858de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
2859de0bd6f7SBjoern A. Zeeb {
2860de0bd6f7SBjoern A. Zeeb 
2861de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
2862de0bd6f7SBjoern A. Zeeb 		return (0);
2863de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
2864de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
2865de0bd6f7SBjoern A. Zeeb 		return (0);
2866de0bd6f7SBjoern A. Zeeb #endif
2867de0bd6f7SBjoern A. Zeeb 
2868de0bd6f7SBjoern A. Zeeb 	return (1);
2869de0bd6f7SBjoern A. Zeeb }
2870de0bd6f7SBjoern A. Zeeb 
2871de0bd6f7SBjoern A. Zeeb /*
28727455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
28739484d0c0SRobert Drehmel  */
2874ad1ff099SRobert Drehmel void
28759ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
28769484d0c0SRobert Drehmel {
287776ca6f88SJamie Gritton 	struct prison *pr;
28789484d0c0SRobert Drehmel 
28797455b100SJamie Gritton 	/*
28807455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
28817455b100SJamie Gritton 	 * system's hostname.
28827455b100SJamie Gritton 	 */
288376ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
288476ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
2885c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
288676ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
28879484d0c0SRobert Drehmel }
2888fd7a8150SMike Barcroft 
28897455b100SJamie Gritton void
28907455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
28917455b100SJamie Gritton {
28927455b100SJamie Gritton 
28937455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
2894c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
28957455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
28967455b100SJamie Gritton }
28977455b100SJamie Gritton 
28987455b100SJamie Gritton void
28997455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
29007455b100SJamie Gritton {
29017455b100SJamie Gritton 
29027455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
2903c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
29047455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
29057455b100SJamie Gritton }
29067455b100SJamie Gritton 
29077455b100SJamie Gritton void
29087455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
29097455b100SJamie Gritton {
29107455b100SJamie Gritton 
29117455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
29127455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
29137455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
29147455b100SJamie Gritton }
29157455b100SJamie Gritton 
2916eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
2917eb79e1c7SBjoern A. Zeeb /*
2918eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
2919eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
2920eb79e1c7SBjoern A. Zeeb  *
2921eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
2922eb79e1c7SBjoern A. Zeeb  */
2923eb79e1c7SBjoern A. Zeeb int
2924eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
2925eb79e1c7SBjoern A. Zeeb {
2926eb79e1c7SBjoern A. Zeeb 
2927eb79e1c7SBjoern A. Zeeb 	/*
2928eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
2929eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
2930eb79e1c7SBjoern A. Zeeb 	 */
2931eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
2932eb79e1c7SBjoern A. Zeeb }
2933eb79e1c7SBjoern A. Zeeb #endif
2934eb79e1c7SBjoern A. Zeeb 
2935f08df373SRobert Watson /*
2936820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
2937820a0de9SPawel Jakub Dawidek  * status of a mount point.
2938820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
2939820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
2940820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
2941f08df373SRobert Watson  */
2942f08df373SRobert Watson int
2943820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
2944f08df373SRobert Watson {
2945820a0de9SPawel Jakub Dawidek 	struct prison *pr;
2946820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
2947820a0de9SPawel Jakub Dawidek 	size_t len;
2948f08df373SRobert Watson 
2949820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
29500304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
29510304c731SJamie Gritton 		return (0);
2952820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
2953820a0de9SPawel Jakub Dawidek 		return (0);
29540304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
2955820a0de9SPawel Jakub Dawidek 		return (ENOENT);
2956820a0de9SPawel Jakub Dawidek 	/*
2957820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
2958820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
2959820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
2960820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
2961820a0de9SPawel Jakub Dawidek 	 */
2962820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
2963820a0de9SPawel Jakub Dawidek 		return (0);
2964820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
2965820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
2966820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
2967820a0de9SPawel Jakub Dawidek 		return (ENOENT);
2968820a0de9SPawel Jakub Dawidek 	/*
2969820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
2970820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
2971820a0de9SPawel Jakub Dawidek 	 */
2972820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
2973820a0de9SPawel Jakub Dawidek 		return (ENOENT);
2974f08df373SRobert Watson 	return (0);
2975f08df373SRobert Watson }
2976820a0de9SPawel Jakub Dawidek 
2977820a0de9SPawel Jakub Dawidek void
2978820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
2979820a0de9SPawel Jakub Dawidek {
2980820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
2981820a0de9SPawel Jakub Dawidek 	struct prison *pr;
2982820a0de9SPawel Jakub Dawidek 	size_t len;
2983820a0de9SPawel Jakub Dawidek 
2984820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
29850304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
29860304c731SJamie Gritton 		return;
2987820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
2988820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
2989820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
2990820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
2991820a0de9SPawel Jakub Dawidek 		return;
2992820a0de9SPawel Jakub Dawidek 	}
2993820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
2994820a0de9SPawel Jakub Dawidek 		/*
2995820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
2996820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
2997820a0de9SPawel Jakub Dawidek 		 */
2998820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
2999820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3000820a0de9SPawel Jakub Dawidek 		return;
3001820a0de9SPawel Jakub Dawidek 	}
3002820a0de9SPawel Jakub Dawidek 	/*
3003820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3004820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3005820a0de9SPawel Jakub Dawidek 	 */
3006820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3007820a0de9SPawel Jakub Dawidek 		return;
3008820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3009820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3010820a0de9SPawel Jakub Dawidek 	/*
3011820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3012820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3013820a0de9SPawel Jakub Dawidek 	 */
3014820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3015820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3016820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3017820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3018820a0de9SPawel Jakub Dawidek 	} else {
3019820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3020820a0de9SPawel Jakub Dawidek 	}
3021f08df373SRobert Watson }
3022f08df373SRobert Watson 
3023800c9408SRobert Watson /*
3024800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3025800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3026800c9408SRobert Watson  */
3027800c9408SRobert Watson int
3028800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3029800c9408SRobert Watson {
3030800c9408SRobert Watson 
3031800c9408SRobert Watson 	if (!jailed(cred))
3032800c9408SRobert Watson 		return (0);
3033800c9408SRobert Watson 
30346bb79563SJamie Gritton #ifdef VIMAGE
30356bb79563SJamie Gritton 	/*
30366bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
30376bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
30386bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
30396bb79563SJamie Gritton 	 */
30406bb79563SJamie Gritton 	switch (priv) {
30416bb79563SJamie Gritton #ifdef notyet
30426bb79563SJamie Gritton 		/*
30436bb79563SJamie Gritton 		 * NFS-specific privileges.
30446bb79563SJamie Gritton 		 */
30456bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
30466bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
30476bb79563SJamie Gritton #endif
30486bb79563SJamie Gritton 		/*
30496bb79563SJamie Gritton 		 * Network stack privileges.
30506bb79563SJamie Gritton 		 */
30516bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
30526bb79563SJamie Gritton 	case PRIV_NET_GRE:
30536bb79563SJamie Gritton 	case PRIV_NET_BPF:
30546bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
30556bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
30566bb79563SJamie Gritton 	case PRIV_NET_TAP:
30576bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
30586bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
30596bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3060215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
30616bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
30626bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
30636bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
30646bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
30656bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
30666bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
30676bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
30686bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
30696bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
30706bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
30716bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
30726bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
30736bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
30746bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
30756bb79563SJamie Gritton 	case PRIV_NET_LAGG:
30766bb79563SJamie Gritton 	case PRIV_NET_GIF:
30776bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
307835fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
30796bb79563SJamie Gritton 
30806bb79563SJamie Gritton 		/*
30816bb79563SJamie Gritton 		 * 802.11-related privileges.
30826bb79563SJamie Gritton 		 */
30836bb79563SJamie Gritton 	case PRIV_NET80211_GETKEY:
30846bb79563SJamie Gritton #ifdef notyet
30856bb79563SJamie Gritton 	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
30866bb79563SJamie Gritton #endif
30876bb79563SJamie Gritton 
30886bb79563SJamie Gritton #ifdef notyet
30896bb79563SJamie Gritton 		/*
30906bb79563SJamie Gritton 		 * ATM privileges.
30916bb79563SJamie Gritton 		 */
30926bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
30936bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
30946bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
30956bb79563SJamie Gritton 	case PRIV_NETATM_SET:
30966bb79563SJamie Gritton 
30976bb79563SJamie Gritton 		/*
30986bb79563SJamie Gritton 		 * Bluetooth privileges.
30996bb79563SJamie Gritton 		 */
31006bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
31016bb79563SJamie Gritton #endif
31026bb79563SJamie Gritton 
31036bb79563SJamie Gritton 		/*
31046bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
31056bb79563SJamie Gritton 		 */
31066bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
31076bb79563SJamie Gritton #ifdef notyet
31086bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
31096bb79563SJamie Gritton #endif
31106bb79563SJamie Gritton 
31116bb79563SJamie Gritton 		/*
31126bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
31136bb79563SJamie Gritton 		 */
31146bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
31156bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
31166bb79563SJamie Gritton 	case PRIV_NETINET_PF:
31176bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
31186bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
31196bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
31206bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
31216bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
31226bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
31236bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
31246bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
31256bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
31266bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
31276bb79563SJamie Gritton 
31286bb79563SJamie Gritton #ifdef notyet
31296bb79563SJamie Gritton 		/*
31306bb79563SJamie Gritton 		 * NCP privileges.
31316bb79563SJamie Gritton 		 */
31326bb79563SJamie Gritton 	case PRIV_NETNCP:
31336bb79563SJamie Gritton 
31346bb79563SJamie Gritton 		/*
31356bb79563SJamie Gritton 		 * SMB privileges.
31366bb79563SJamie Gritton 		 */
31376bb79563SJamie Gritton 	case PRIV_NETSMB:
31386bb79563SJamie Gritton #endif
31396bb79563SJamie Gritton 
31406bb79563SJamie Gritton 	/*
31416bb79563SJamie Gritton 	 * No default: or deny here.
31426bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
31436bb79563SJamie Gritton 	 */
31446bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
31456bb79563SJamie Gritton 			return (0);
31466bb79563SJamie Gritton 	}
31476bb79563SJamie Gritton #endif /* VIMAGE */
31486bb79563SJamie Gritton 
3149800c9408SRobert Watson 	switch (priv) {
3150800c9408SRobert Watson 
3151800c9408SRobert Watson 		/*
3152800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3153800c9408SRobert Watson 		 */
3154800c9408SRobert Watson 	case PRIV_KTRACE:
3155800c9408SRobert Watson 
3156c3c1b5e6SRobert Watson #if 0
3157800c9408SRobert Watson 		/*
3158800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3159800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3160800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3161800c9408SRobert Watson 		 * jail.
3162800c9408SRobert Watson 		 */
3163800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3164800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3165800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3166c3c1b5e6SRobert Watson #endif
3167800c9408SRobert Watson 
3168800c9408SRobert Watson 		/*
3169800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3170800c9408SRobert Watson 		 * credentials in any way they see fit.
3171800c9408SRobert Watson 		 */
3172800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3173800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3174800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3175800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3176800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3177800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3178800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3179800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3180800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3181800c9408SRobert Watson 
3182800c9408SRobert Watson 		/*
3183800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3184800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3185800c9408SRobert Watson 		 */
3186800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3187800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3188800c9408SRobert Watson 
3189800c9408SRobert Watson 		/*
3190800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3191800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3192800c9408SRobert Watson 		 */
3193800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3194800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3195800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3196800c9408SRobert Watson 
3197800c9408SRobert Watson 		/*
3198800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3199800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3200800c9408SRobert Watson 		 */
3201800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3202800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3203800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3204800c9408SRobert Watson 
3205800c9408SRobert Watson 		/*
3206800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3207800c9408SRobert Watson 		 */
3208800c9408SRobert Watson 	case PRIV_IPC_READ:
3209800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3210800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3211800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3212800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3213800c9408SRobert Watson 
3214800c9408SRobert Watson 		/*
32150304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
32160304c731SJamie Gritton 		 */
32170304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
32180304c731SJamie Gritton 	case PRIV_JAIL_SET:
32190304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
32200304c731SJamie Gritton 
32210304c731SJamie Gritton 		/*
3222800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3223800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3224800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3225800c9408SRobert Watson 		 */
3226800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3227413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3228800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3229800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3230800c9408SRobert Watson 
3231800c9408SRobert Watson 		/*
3232800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3233800c9408SRobert Watson 		 * writable.
3234800c9408SRobert Watson 		 */
3235800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3236800c9408SRobert Watson 
3237800c9408SRobert Watson 		/*
3238800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3239e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3240e82d0201SRobert Watson 		 * configuration option.
3241800c9408SRobert Watson 		 */
324295b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
324395b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3244800c9408SRobert Watson 
3245800c9408SRobert Watson 		/*
3246800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3247800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3248800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3249800c9408SRobert Watson 		 * privileges.
3250800c9408SRobert Watson 		 */
3251800c9408SRobert Watson 	case PRIV_VFS_READ:
3252800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3253800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3254800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3255800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3256800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3257800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3258800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3259800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3260bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3261800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3262800c9408SRobert Watson 	case PRIV_VFS_LINK:
3263800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3264e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3265800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3266bb56d716SJamie Gritton 
3267bb56d716SJamie Gritton 		/*
3268bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
3269bb56d716SJamie Gritton 		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3270bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3271bb56d716SJamie Gritton 		 */
3272bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3273800c9408SRobert Watson 		return (0);
3274800c9408SRobert Watson 
3275800c9408SRobert Watson 		/*
3276800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3277800c9408SRobert Watson 		 * setting system flags.
3278800c9408SRobert Watson 		 */
3279800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
32800304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3281800c9408SRobert Watson 			return (0);
3282800c9408SRobert Watson 		else
3283800c9408SRobert Watson 			return (EPERM);
3284800c9408SRobert Watson 
3285800c9408SRobert Watson 		/*
3286f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3287f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3288f3a8d2f9SPawel Jakub Dawidek 		 */
3289f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3290f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3291f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
329224b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
3293435d4667SMartin Matuska 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3294435d4667SMartin Matuska 		    cred->cr_prison->pr_enforce_statfs < 2)
3295f3a8d2f9SPawel Jakub Dawidek 			return (0);
3296f3a8d2f9SPawel Jakub Dawidek 		else
3297f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
3298f3a8d2f9SPawel Jakub Dawidek 
3299f3a8d2f9SPawel Jakub Dawidek 		/*
3300ccd6ac9fSAntoine Brodin 		 * Conditionnaly allow locking (unlocking) physical pages
3301ccd6ac9fSAntoine Brodin 		 * in memory.
3302ccd6ac9fSAntoine Brodin 		 */
3303ccd6ac9fSAntoine Brodin 	case PRIV_VM_MLOCK:
3304ccd6ac9fSAntoine Brodin 	case PRIV_VM_MUNLOCK:
3305ccd6ac9fSAntoine Brodin 		if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3306ccd6ac9fSAntoine Brodin 			return (0);
3307ccd6ac9fSAntoine Brodin 		else
3308ccd6ac9fSAntoine Brodin 			return (EPERM);
3309ccd6ac9fSAntoine Brodin 
3310ccd6ac9fSAntoine Brodin 		/*
3311e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
3312800c9408SRobert Watson 		 */
3313800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
3314e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3315e28f9b7dSAllan Jude 			return (0);
3316e28f9b7dSAllan Jude 		else
3317e28f9b7dSAllan Jude 			return (EPERM);
3318e28f9b7dSAllan Jude 
3319e28f9b7dSAllan Jude 		/*
3320e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
3321e28f9b7dSAllan Jude 		 */
33224b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3323800c9408SRobert Watson 		return (0);
3324800c9408SRobert Watson 
3325800c9408SRobert Watson 		/*
3326e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
332779ba3952SBjoern A. Zeeb 		 */
332879ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
332979ba3952SBjoern A. Zeeb 		return (0);
333079ba3952SBjoern A. Zeeb 
333179ba3952SBjoern A. Zeeb 		/*
3332800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3333800c9408SRobert Watson 		 */
3334800c9408SRobert Watson 	case PRIV_NETINET_RAW:
33350304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3336800c9408SRobert Watson 			return (0);
3337800c9408SRobert Watson 		else
3338800c9408SRobert Watson 			return (EPERM);
3339800c9408SRobert Watson 
3340800c9408SRobert Watson 		/*
3341800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3342800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3343800c9408SRobert Watson 		 * jail.
3344800c9408SRobert Watson 		 */
3345800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3346800c9408SRobert Watson 		return (0);
3347800c9408SRobert Watson 
33482bfc50bcSEdward Tomasz Napierala 		/*
33492bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
33502bfc50bcSEdward Tomasz Napierala 		 */
33512bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
33522bfc50bcSEdward Tomasz Napierala 		return (0);
33532bfc50bcSEdward Tomasz Napierala 
3354*b19d66fdSJamie Gritton 		/*
3355*b19d66fdSJamie Gritton 		 * Do not allow a process inside a jail read the kernel
3356*b19d66fdSJamie Gritton 		 * message buffer unless explicitly permitted.
3357*b19d66fdSJamie Gritton 		 */
3358*b19d66fdSJamie Gritton 	case PRIV_MSGBUF:
3359*b19d66fdSJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3360*b19d66fdSJamie Gritton 			return (0);
3361*b19d66fdSJamie Gritton 		return (EPERM);
3362*b19d66fdSJamie Gritton 
3363800c9408SRobert Watson 	default:
3364800c9408SRobert Watson 		/*
3365800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3366800c9408SRobert Watson 		 * includes almost all network privileges, many system
3367800c9408SRobert Watson 		 * configuration privileges.
3368800c9408SRobert Watson 		 */
3369800c9408SRobert Watson 		return (EPERM);
3370800c9408SRobert Watson 	}
3371800c9408SRobert Watson }
3372800c9408SRobert Watson 
33730304c731SJamie Gritton /*
33740304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
33750304c731SJamie Gritton  * if it does not directly follow.
33760304c731SJamie Gritton  */
33770304c731SJamie Gritton 
33780304c731SJamie Gritton char *
33790304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
33800304c731SJamie Gritton {
33810304c731SJamie Gritton 	char *name;
33820304c731SJamie Gritton 
33830304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
33840304c731SJamie Gritton 	if (pr1 == pr2)
33850304c731SJamie Gritton 		return "0";
33860304c731SJamie Gritton 	name = pr2->pr_name;
33870304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
33880304c731SJamie Gritton 		/*
33890304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
33900304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
33910304c731SJamie Gritton 		 * can be counted on - and counted.
33920304c731SJamie Gritton 		 */
33930304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
33940304c731SJamie Gritton 			name = strchr(name, '.') + 1;
33950304c731SJamie Gritton 	}
33960304c731SJamie Gritton 	return (name);
33970304c731SJamie Gritton }
33980304c731SJamie Gritton 
33990304c731SJamie Gritton /*
34000304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
34010304c731SJamie Gritton  * if it does not directly follow.
34020304c731SJamie Gritton  */
34030304c731SJamie Gritton static char *
34040304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
34050304c731SJamie Gritton {
34060304c731SJamie Gritton 	char *path1, *path2;
34070304c731SJamie Gritton 	int len1;
34080304c731SJamie Gritton 
34090304c731SJamie Gritton 	path1 = pr1->pr_path;
34100304c731SJamie Gritton 	path2 = pr2->pr_path;
34110304c731SJamie Gritton 	if (!strcmp(path1, "/"))
34120304c731SJamie Gritton 		return (path2);
34130304c731SJamie Gritton 	len1 = strlen(path1);
34140304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
34150304c731SJamie Gritton 		return (path2);
34160304c731SJamie Gritton 	if (path2[len1] == '\0')
34170304c731SJamie Gritton 		return "/";
34180304c731SJamie Gritton 	if (path2[len1] == '/')
34190304c731SJamie Gritton 		return (path2 + len1);
34200304c731SJamie Gritton 	return (path2);
34210304c731SJamie Gritton }
34220304c731SJamie Gritton 
34230304c731SJamie Gritton 
34240304c731SJamie Gritton /*
34250304c731SJamie Gritton  * Jail-related sysctls.
34260304c731SJamie Gritton  */
34276472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
34280304c731SJamie Gritton     "Jails");
34290304c731SJamie Gritton 
3430fd7a8150SMike Barcroft static int
3431fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3432fd7a8150SMike Barcroft {
3433b38ff370SJamie Gritton 	struct xprison *xp;
34340304c731SJamie Gritton 	struct prison *pr, *cpr;
3435b38ff370SJamie Gritton #ifdef INET
3436b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3437b38ff370SJamie Gritton 	int ip4s = 0;
3438b38ff370SJamie Gritton #endif
3439b38ff370SJamie Gritton #ifdef INET6
34403beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
3441b38ff370SJamie Gritton 	int ip6s = 0;
3442b38ff370SJamie Gritton #endif
34430304c731SJamie Gritton 	int descend, error;
3444fd7a8150SMike Barcroft 
3445b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
34460304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3447b38ff370SJamie Gritton 	error = 0;
3448dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
34490304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
34500304c731SJamie Gritton #if defined(INET) || defined(INET6)
3451b38ff370SJamie Gritton  again:
34520304c731SJamie Gritton #endif
34530304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3454413628a7SBjoern A. Zeeb #ifdef INET
34550304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
34560304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
34570304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
34580304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3459b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3460b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3461b38ff370SJamie Gritton 				goto again;
3462b38ff370SJamie Gritton 			}
34630304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
34640304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3465b38ff370SJamie Gritton 		}
3466413628a7SBjoern A. Zeeb #endif
3467413628a7SBjoern A. Zeeb #ifdef INET6
34680304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
34690304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
34700304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
34710304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3472b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3473b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3474b38ff370SJamie Gritton 				goto again;
3475413628a7SBjoern A. Zeeb 			}
34760304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
34770304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3478b38ff370SJamie Gritton 		}
3479b38ff370SJamie Gritton #endif
34800304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
34810304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3482b38ff370SJamie Gritton 			continue;
3483b38ff370SJamie Gritton 		}
3484b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
3485fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
34860304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
34870304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
3488b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
34890304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3490c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
34910304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3492413628a7SBjoern A. Zeeb #ifdef INET
34930304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
3494413628a7SBjoern A. Zeeb #endif
3495413628a7SBjoern A. Zeeb #ifdef INET6
34960304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
3497413628a7SBjoern A. Zeeb #endif
34980304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3499b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3500b38ff370SJamie Gritton 		if (error)
3501b38ff370SJamie Gritton 			break;
3502413628a7SBjoern A. Zeeb #ifdef INET
3503b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
3504b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
3505b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
3506b38ff370SJamie Gritton 			if (error)
3507b38ff370SJamie Gritton 				break;
3508413628a7SBjoern A. Zeeb 		}
3509413628a7SBjoern A. Zeeb #endif
3510413628a7SBjoern A. Zeeb #ifdef INET6
3511b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
3512b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
3513b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
3514b38ff370SJamie Gritton 			if (error)
3515b38ff370SJamie Gritton 				break;
3516413628a7SBjoern A. Zeeb 		}
3517413628a7SBjoern A. Zeeb #endif
3518fd7a8150SMike Barcroft 	}
3519dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
3520b38ff370SJamie Gritton 	free(xp, M_TEMP);
3521b38ff370SJamie Gritton #ifdef INET
3522b38ff370SJamie Gritton 	free(ip4, M_TEMP);
3523b38ff370SJamie Gritton #endif
3524b38ff370SJamie Gritton #ifdef INET6
3525b38ff370SJamie Gritton 	free(ip6, M_TEMP);
3526b38ff370SJamie Gritton #endif
3527fd7a8150SMike Barcroft 	return (error);
3528fd7a8150SMike Barcroft }
3529fd7a8150SMike Barcroft 
3530f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
3531f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3532f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
3533461167c2SPawel Jakub Dawidek 
3534461167c2SPawel Jakub Dawidek static int
3535461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3536461167c2SPawel Jakub Dawidek {
3537461167c2SPawel Jakub Dawidek 	int error, injail;
3538461167c2SPawel Jakub Dawidek 
3539461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
3540461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3541461167c2SPawel Jakub Dawidek 
3542461167c2SPawel Jakub Dawidek 	return (error);
3543461167c2SPawel Jakub Dawidek }
35440304c731SJamie Gritton 
3545f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3546f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3547f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
3548413628a7SBjoern A. Zeeb 
3549761d2bb5SJamie Gritton static int
3550761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3551761d2bb5SJamie Gritton {
3552761d2bb5SJamie Gritton 	int error, havevnet;
3553761d2bb5SJamie Gritton #ifdef VIMAGE
3554761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
3555761d2bb5SJamie Gritton 
3556761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
3557761d2bb5SJamie Gritton #else
3558761d2bb5SJamie Gritton 	havevnet = 0;
3559761d2bb5SJamie Gritton #endif
3560761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3561761d2bb5SJamie Gritton 
3562761d2bb5SJamie Gritton 	return (error);
3563761d2bb5SJamie Gritton }
3564761d2bb5SJamie Gritton 
3565761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3566761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3567bb8f1623SBjoern A. Zeeb     sysctl_jail_vnet, "I", "Jail owns vnet?");
3568761d2bb5SJamie Gritton 
35690304c731SJamie Gritton #if defined(INET) || defined(INET6)
3570e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
35710304c731SJamie Gritton     &jail_max_af_ips, 0,
3572c542c43eSJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
35730304c731SJamie Gritton #endif
35740304c731SJamie Gritton 
35750304c731SJamie Gritton /*
3576c542c43eSJamie Gritton  * Default parameters for jail(2) compatibility.  For historical reasons,
3577c542c43eSJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
3578c542c43eSJamie Gritton  * just see their own parameters, and can't change them.
35790304c731SJamie Gritton  */
35800304c731SJamie Gritton static int
35810304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
35820304c731SJamie Gritton {
35830304c731SJamie Gritton 	struct prison *pr;
35840304c731SJamie Gritton 	int allow, error, i;
35850304c731SJamie Gritton 
35860304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
35870304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
35880304c731SJamie Gritton 
35890304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
35900304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
35910304c731SJamie Gritton 	if (arg1 != NULL)
35920304c731SJamie Gritton 		i = !i;
35930304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
3594c542c43eSJamie Gritton 	if (error || !req->newptr)
35950304c731SJamie Gritton 		return (error);
35960304c731SJamie Gritton 	i = i ? arg2 : 0;
35970304c731SJamie Gritton 	if (arg1 != NULL)
35980304c731SJamie Gritton 		i ^= arg2;
35990304c731SJamie Gritton 	/*
36000304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
36010304c731SJamie Gritton 	 * for writing.
36020304c731SJamie Gritton 	 */
36030304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
36040304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
36050304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
36060304c731SJamie Gritton 	return (0);
36070304c731SJamie Gritton }
36080304c731SJamie Gritton 
36090304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3610c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36110304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3612c542c43eSJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
36130304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3614c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36150304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3616c542c43eSJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
36170304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3618c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36190304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3620c542c43eSJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
36210304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3622c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36230304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3624c542c43eSJamie Gritton     "Prison root can create raw sockets (deprecated)");
36250304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3626c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36270304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3628c542c43eSJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
36290304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3630c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36310304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3632c542c43eSJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
36330304c731SJamie Gritton 
36340304c731SJamie Gritton static int
36350304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
36360304c731SJamie Gritton {
36370304c731SJamie Gritton 	struct prison *pr;
36380304c731SJamie Gritton 	int level, error;
36390304c731SJamie Gritton 
36400304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
36410304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
36420304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
3643c542c43eSJamie Gritton 	if (error || !req->newptr)
36440304c731SJamie Gritton 		return (error);
36450304c731SJamie Gritton 	*(int *)arg1 = level;
36460304c731SJamie Gritton 	return (0);
36470304c731SJamie Gritton }
36480304c731SJamie Gritton 
36490304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3650c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3651c542c43eSJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
36520304c731SJamie Gritton     sysctl_jail_default_level, "I",
3653c542c43eSJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
3654c542c43eSJamie Gritton 
36550cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
3656c542c43eSJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
3657c542c43eSJamie Gritton     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
36580cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
3659c542c43eSJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
36600cc207a6SMartin Matuska 
36610304c731SJamie Gritton /*
36620304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
36630304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
36640304c731SJamie Gritton  * to make themselves and their types known.
36650304c731SJamie Gritton  */
36660304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
36670304c731SJamie Gritton     "Jail parameters");
36680304c731SJamie Gritton 
36690304c731SJamie Gritton int
36700304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
36710304c731SJamie Gritton {
36720304c731SJamie Gritton 	int i;
36730304c731SJamie Gritton 	long l;
36740304c731SJamie Gritton 	size_t s;
36750304c731SJamie Gritton 	char numbuf[12];
36760304c731SJamie Gritton 
36770304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
36780304c731SJamie Gritton 	{
36790304c731SJamie Gritton 	case CTLTYPE_LONG:
36800304c731SJamie Gritton 	case CTLTYPE_ULONG:
36810304c731SJamie Gritton 		l = 0;
36820304c731SJamie Gritton #ifdef SCTL_MASK32
36830304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
36840304c731SJamie Gritton #endif
36850304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
36860304c731SJamie Gritton 	case CTLTYPE_INT:
36870304c731SJamie Gritton 	case CTLTYPE_UINT:
36880304c731SJamie Gritton 		i = 0;
36890304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
36900304c731SJamie Gritton 	case CTLTYPE_STRING:
3691e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
36920304c731SJamie Gritton 		return
36930304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
36940304c731SJamie Gritton 	case CTLTYPE_STRUCT:
36950304c731SJamie Gritton 		s = (size_t)arg2;
36960304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
36970304c731SJamie Gritton 	}
36980304c731SJamie Gritton 	return (0);
36990304c731SJamie Gritton }
37000304c731SJamie Gritton 
3701b96bd95bSIan Lepore /*
3702b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
3703b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
3704b96bd95bSIan Lepore  */
37050304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
37060304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
37070304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
37080304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
37090304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
37100304c731SJamie Gritton     "I", "Jail secure level");
3711b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
3712b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
3713b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
3714b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
37150304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
37160304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
37170cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
37180cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
37190304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
37200304c731SJamie Gritton     "B", "Jail persistence");
3721679e1390SJamie Gritton #ifdef VIMAGE
3722679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
37237cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
3724679e1390SJamie Gritton #endif
37250304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
37260304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
37270304c731SJamie Gritton 
3728b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
3729b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
3730b97457e2SJamie Gritton     "I", "Current number of child jails");
3731b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
3732b97457e2SJamie Gritton     "I", "Maximum number of child jails");
3733b97457e2SJamie Gritton 
37347cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
37350304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
37360304c731SJamie Gritton     "Jail hostname");
373776ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
373876ca6f88SJamie Gritton     "Jail NIS domainname");
373976ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
374076ca6f88SJamie Gritton     "Jail host UUID");
374176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
374276ca6f88SJamie Gritton     "LU", "Jail host ID");
37430304c731SJamie Gritton 
37440304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
37450304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
37460304c731SJamie Gritton 
37470304c731SJamie Gritton #ifdef INET
37482b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
37492b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
37500304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
37510304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
3752592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
3753592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
3754592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
37550304c731SJamie Gritton #endif
37560304c731SJamie Gritton #ifdef INET6
37572b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
37582b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
37590304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
37600304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
3761592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
3762592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
3763592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
37640304c731SJamie Gritton #endif
37650304c731SJamie Gritton 
37660304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
37670304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
37680304c731SJamie Gritton     "B", "Jail may set hostname");
37690304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
37700304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
37710304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
37720304c731SJamie Gritton     "B", "Jail may create raw sockets");
37730304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
37740304c731SJamie Gritton     "B", "Jail may alter system file flags");
37750304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
37760304c731SJamie Gritton     "B", "Jail may set file quotas");
37770304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
37780304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
3779ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
3780ccd6ac9fSAntoine Brodin     "B", "Jail may lock (unlock) physical pages in memory");
3781e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
3782e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
3783*b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
3784*b19d66fdSJamie Gritton     "B", "Jail may read the kernel message buffer");
37850304c731SJamie Gritton 
3786bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
3787bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
3788bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
37890e5c6bd4SJamie Gritton 
37900e5c6bd4SJamie Gritton /*
37910a172404SJamie Gritton  * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>.  Return
37920a172404SJamie Gritton  * its associated bit in the pr_allow bitmask, or zero if the parameter was
37930a172404SJamie Gritton  * not created.
37940e5c6bd4SJamie Gritton  */
37950a172404SJamie Gritton unsigned
37960a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
37970a172404SJamie Gritton     const char *descr)
37980e5c6bd4SJamie Gritton {
37990e5c6bd4SJamie Gritton 	struct bool_flags *bf;
38000a172404SJamie Gritton 	struct sysctl_oid *parent;
38010a172404SJamie Gritton 	char *allow_name, *allow_noname, *allowed;
3802c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
3803c542c43eSJamie Gritton 	char *descr_deprecated;
3804c542c43eSJamie Gritton #endif
38050e5c6bd4SJamie Gritton 	unsigned allow_flag;
38060e5c6bd4SJamie Gritton 
38070a172404SJamie Gritton 	if (prefix
38080a172404SJamie Gritton 	    ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
38090a172404SJamie Gritton 		< 0 ||
38100a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
38110a172404SJamie Gritton 		< 0
38120a172404SJamie Gritton 	    : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
38130a172404SJamie Gritton 	      asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
38140e5c6bd4SJamie Gritton 		free(allow_name, M_PRISON);
38150a172404SJamie Gritton 		return 0;
38160e5c6bd4SJamie Gritton 	}
38170e5c6bd4SJamie Gritton 
38180e5c6bd4SJamie Gritton 	/*
38190a172404SJamie Gritton 	 * See if this parameter has already beed added, i.e. a module was
38200a172404SJamie Gritton 	 * previously loaded/unloaded.
38210e5c6bd4SJamie Gritton 	 */
38220e5c6bd4SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
38230e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow;
38240e5c6bd4SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0;
38250e5c6bd4SJamie Gritton 	     bf++) {
38260e5c6bd4SJamie Gritton 		if (strcmp(bf->name, allow_name) == 0) {
38270a172404SJamie Gritton 			allow_flag = bf->flag;
38280e5c6bd4SJamie Gritton 			goto no_add;
38290e5c6bd4SJamie Gritton 		}
38300e5c6bd4SJamie Gritton 	}
38310e5c6bd4SJamie Gritton 
38320e5c6bd4SJamie Gritton 	/*
38330e5c6bd4SJamie Gritton 	 * Find a free bit in prison0's pr_allow, failing if there are none
38340e5c6bd4SJamie Gritton 	 * (which shouldn't happen as long as we keep track of how many
38350a172404SJamie Gritton 	 * potential dynamic flags exist).
38360e5c6bd4SJamie Gritton 	 */
38370e5c6bd4SJamie Gritton 	for (allow_flag = 1;; allow_flag <<= 1) {
38380e5c6bd4SJamie Gritton 		if (allow_flag == 0)
38390e5c6bd4SJamie Gritton 			goto no_add;
38400e5c6bd4SJamie Gritton 		if ((prison0.pr_allow & allow_flag) == 0)
38410e5c6bd4SJamie Gritton 			break;
38420e5c6bd4SJamie Gritton 	}
38430e5c6bd4SJamie Gritton 
38440e5c6bd4SJamie Gritton 	/*
38450e5c6bd4SJamie Gritton 	 * Note the parameter in the next open slot in pr_flag_allow.
38460e5c6bd4SJamie Gritton 	 * Set the flag last so code that checks pr_flag_allow can do so
38470e5c6bd4SJamie Gritton 	 * without locking.
38480e5c6bd4SJamie Gritton 	 */
38490e5c6bd4SJamie Gritton 	for (bf = pr_flag_allow; bf->flag != 0; bf++)
38500e5c6bd4SJamie Gritton 		if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
38510e5c6bd4SJamie Gritton 			/* This should never happen, but is not fatal. */
38520a172404SJamie Gritton 			allow_flag = 0;
38530e5c6bd4SJamie Gritton 			goto no_add;
38540e5c6bd4SJamie Gritton 		}
38550e5c6bd4SJamie Gritton 	prison0.pr_allow |= allow_flag;
38560e5c6bd4SJamie Gritton 	bf->name = allow_name;
38570e5c6bd4SJamie Gritton 	bf->noname = allow_noname;
38580e5c6bd4SJamie Gritton 	bf->flag = allow_flag;
38590e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
38600e5c6bd4SJamie Gritton 
3861c542c43eSJamie Gritton 	/*
3862c542c43eSJamie Gritton 	 * Create sysctls for the paramter, and the back-compat global
3863c542c43eSJamie Gritton 	 * permission.
3864c542c43eSJamie Gritton 	 */
38650a172404SJamie Gritton 	parent = prefix
38660a172404SJamie Gritton 	    ? SYSCTL_ADD_NODE(NULL,
38670a172404SJamie Gritton 		  SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
38680a172404SJamie Gritton 		  OID_AUTO, prefix, 0, 0, prefix_descr)
38690a172404SJamie Gritton 	    : &sysctl___security_jail_param_allow;
38700a172404SJamie Gritton 	(void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
38710a172404SJamie Gritton 	    name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38720e5c6bd4SJamie Gritton 	    NULL, 0, sysctl_jail_param, "B", descr);
38730a172404SJamie Gritton 	if ((prefix
38740a172404SJamie Gritton 	     ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
38750a172404SJamie Gritton 	     : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
3876c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
3877c542c43eSJamie Gritton 		(void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
3878c542c43eSJamie Gritton 		    descr);
3879c542c43eSJamie Gritton #endif
38800e5c6bd4SJamie Gritton 		(void)SYSCTL_ADD_PROC(NULL,
38810a172404SJamie Gritton 		    SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
3882c542c43eSJamie Gritton 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
3883c542c43eSJamie Gritton 		    sysctl_jail_default_allow, "I", descr_deprecated);
3884c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR
3885c542c43eSJamie Gritton 		free(descr_deprecated, M_TEMP);
3886c542c43eSJamie Gritton #endif
38870a172404SJamie Gritton 		free(allowed, M_TEMP);
38880e5c6bd4SJamie Gritton 	}
38890a172404SJamie Gritton 	return allow_flag;
38900e5c6bd4SJamie Gritton 
38910e5c6bd4SJamie Gritton  no_add:
38920e5c6bd4SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
38930e5c6bd4SJamie Gritton 	free(allow_name, M_PRISON);
38940e5c6bd4SJamie Gritton 	free(allow_noname, M_PRISON);
38950a172404SJamie Gritton 	return allow_flag;
38960a172404SJamie Gritton }
38970a172404SJamie Gritton 
38980a172404SJamie Gritton /*
38990a172404SJamie Gritton  * The VFS system will register jail-aware filesystems here.  They each get
39000a172404SJamie Gritton  * a parameter allow.mount.xxxfs and a flag to check when a jailed user
39010a172404SJamie Gritton  * attempts to mount.
39020a172404SJamie Gritton  */
39030a172404SJamie Gritton void
39040a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp)
39050a172404SJamie Gritton {
39060a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR
39070a172404SJamie Gritton 
39080a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
39090a172404SJamie Gritton 	    NULL, NULL);
39100a172404SJamie Gritton #else
39110a172404SJamie Gritton 	char *descr;
39120a172404SJamie Gritton 
39130a172404SJamie Gritton 	(void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
39140a172404SJamie Gritton 	    vfsp->vfc_name);
39150a172404SJamie Gritton 	vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
39160a172404SJamie Gritton 	    NULL, descr);
39170a172404SJamie Gritton 	free(descr, M_TEMP);
39180a172404SJamie Gritton #endif
39190e5c6bd4SJamie Gritton }
3920bf3db8aaSMartin Matuska 
39214b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
3922097055e2SEdward Tomasz Napierala void
3923097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
392415db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
392515db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
3926097055e2SEdward Tomasz Napierala {
3927a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3928097055e2SEdward Tomasz Napierala 
39294b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
39304b5c9cf6SEdward Tomasz Napierala 
3931097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
393215db3c07SEdward Tomasz Napierala 	if (pre != NULL)
393315db3c07SEdward Tomasz Napierala 		(pre)();
3934a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
3935a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
393615db3c07SEdward Tomasz Napierala 	if (post != NULL)
393715db3c07SEdward Tomasz Napierala 		(post)();
3938097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
3939097055e2SEdward Tomasz Napierala }
39400304c731SJamie Gritton 
3941a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
3942a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
3943a7ad07bfSEdward Tomasz Napierala {
3944a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3945a7ad07bfSEdward Tomasz Napierala 
39464b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3947a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
3948a7ad07bfSEdward Tomasz Napierala 
3949a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
3950a7ad07bfSEdward Tomasz Napierala 		return (NULL);
3951a7ad07bfSEdward Tomasz Napierala 
3952a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
3953a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
3954a7ad07bfSEdward Tomasz Napierala 			continue;
3955a7ad07bfSEdward Tomasz Napierala 
3956a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
3957a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
3958a7ad07bfSEdward Tomasz Napierala 		return (prr);
3959a7ad07bfSEdward Tomasz Napierala 	}
3960a7ad07bfSEdward Tomasz Napierala 
3961a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
3962a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
3963a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
3964a7ad07bfSEdward Tomasz Napierala 
3965a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
3966a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
3967a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
3968a7ad07bfSEdward Tomasz Napierala 
3969a7ad07bfSEdward Tomasz Napierala 	return (prr);
3970a7ad07bfSEdward Tomasz Napierala }
3971a7ad07bfSEdward Tomasz Napierala 
3972a7ad07bfSEdward Tomasz Napierala struct prison_racct *
3973a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
3974a7ad07bfSEdward Tomasz Napierala {
3975a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3976a7ad07bfSEdward Tomasz Napierala 
39774b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
39784b5c9cf6SEdward Tomasz Napierala 
3979a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
3980a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
3981a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
3982a7ad07bfSEdward Tomasz Napierala 	return (prr);
3983a7ad07bfSEdward Tomasz Napierala }
3984a7ad07bfSEdward Tomasz Napierala 
3985a7ad07bfSEdward Tomasz Napierala void
3986a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
3987a7ad07bfSEdward Tomasz Napierala {
3988a7ad07bfSEdward Tomasz Napierala 
39894b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
39904b5c9cf6SEdward Tomasz Napierala 
3991a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
3992a7ad07bfSEdward Tomasz Napierala }
3993a7ad07bfSEdward Tomasz Napierala 
3994c34bbd2aSEdward Tomasz Napierala static void
3995c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
3996c34bbd2aSEdward Tomasz Napierala {
3997c34bbd2aSEdward Tomasz Napierala 
39984b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3999c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4000c34bbd2aSEdward Tomasz Napierala 
4001c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4002c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4003c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4004c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4005c34bbd2aSEdward Tomasz Napierala 	}
4006c34bbd2aSEdward Tomasz Napierala }
4007c34bbd2aSEdward Tomasz Napierala 
4008a7ad07bfSEdward Tomasz Napierala void
4009a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4010a7ad07bfSEdward Tomasz Napierala {
4011a7ad07bfSEdward Tomasz Napierala 	int old;
4012a7ad07bfSEdward Tomasz Napierala 
40134b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4014c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4015c34bbd2aSEdward Tomasz Napierala 
4016a7ad07bfSEdward Tomasz Napierala 	old = prr->prr_refcount;
4017a7ad07bfSEdward Tomasz Napierala 	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4018a7ad07bfSEdward Tomasz Napierala 		return;
4019a7ad07bfSEdward Tomasz Napierala 
4020a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4021c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4022a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4023a7ad07bfSEdward Tomasz Napierala }
4024a7ad07bfSEdward Tomasz Napierala 
4025a7ad07bfSEdward Tomasz Napierala static void
4026a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4027a7ad07bfSEdward Tomasz Napierala {
4028a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4029a7ad07bfSEdward Tomasz Napierala 
40304b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4031c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4032c34bbd2aSEdward Tomasz Napierala 
4033a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4034a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4035a7ad07bfSEdward Tomasz Napierala 
4036a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4037a7ad07bfSEdward Tomasz Napierala }
4038a7ad07bfSEdward Tomasz Napierala 
4039c34bbd2aSEdward Tomasz Napierala /*
4040c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4041c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4042c34bbd2aSEdward Tomasz Napierala  */
4043c34bbd2aSEdward Tomasz Napierala static void
4044c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4045c34bbd2aSEdward Tomasz Napierala {
4046dbadb015SKonstantin Belousov #ifdef RCTL
4047c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4048c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4049dbadb015SKonstantin Belousov #endif
4050c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4051c34bbd2aSEdward Tomasz Napierala 
40524b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
40534b5c9cf6SEdward Tomasz Napierala 
4054c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4055c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4056c34bbd2aSEdward Tomasz Napierala 
4057e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4058e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4059e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4060c34bbd2aSEdward Tomasz Napierala 		return;
4061e30345e7SEdward Tomasz Napierala 	}
4062c34bbd2aSEdward Tomasz Napierala 
4063c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4064c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4065c34bbd2aSEdward Tomasz Napierala 
4066c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4067c34bbd2aSEdward Tomasz Napierala 
4068c34bbd2aSEdward Tomasz Napierala 	/*
4069c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4070c34bbd2aSEdward Tomasz Napierala 	 */
4071c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4072c34bbd2aSEdward Tomasz Napierala 
4073f87beb93SAndriy Gapon #ifdef RCTL
4074c34bbd2aSEdward Tomasz Napierala 	/*
4075c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4076c34bbd2aSEdward Tomasz Napierala 	 */
4077c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4078c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4079c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4080c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4081f87beb93SAndriy Gapon 		rctl_proc_ucred_changed(p, cred);
4082c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4083c34bbd2aSEdward Tomasz Napierala 	}
4084f87beb93SAndriy Gapon #endif
4085c34bbd2aSEdward Tomasz Napierala 
4086c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4087c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4088c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4089c34bbd2aSEdward Tomasz Napierala }
4090c34bbd2aSEdward Tomasz Napierala 
4091a7ad07bfSEdward Tomasz Napierala static void
4092a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4093a7ad07bfSEdward Tomasz Napierala {
4094c34bbd2aSEdward Tomasz Napierala 
40954b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
4096c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4097c34bbd2aSEdward Tomasz Napierala 
4098af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4099af3c786cSMateusz Guzik 		return;
4100a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4101a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4102a7ad07bfSEdward Tomasz Napierala }
4103a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4104a7ad07bfSEdward Tomasz Napierala 
4105413628a7SBjoern A. Zeeb #ifdef DDB
4106b38ff370SJamie Gritton 
4107b38ff370SJamie Gritton static void
4108b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4109413628a7SBjoern A. Zeeb {
4110672756aaSJamie Gritton 	struct bool_flags *bf;
4111672756aaSJamie Gritton 	struct jailsys_flags *jsf;
4112b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4113b38ff370SJamie Gritton 	int ii;
4114413628a7SBjoern A. Zeeb #endif
4115672756aaSJamie Gritton 	unsigned f;
41168144690aSEric van Gyzen #ifdef INET
41178144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
41188144690aSEric van Gyzen #endif
4119413628a7SBjoern A. Zeeb #ifdef INET6
4120413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4121413628a7SBjoern A. Zeeb #endif
4122413628a7SBjoern A. Zeeb 
4123b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4124b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4125b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
41260304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4127b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4128b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4129b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4130b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4131b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4132679e1390SJamie Gritton #ifdef VIMAGE
4133679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4134679e1390SJamie Gritton #endif
4135b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4136b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
41370cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4138fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4139fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
41400304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
41410304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4142fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
4143672756aaSJamie Gritton 	for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4144672756aaSJamie Gritton 		if (pr->pr_flags & bf->flag)
4145672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4146672756aaSJamie Gritton 	for (jsf = pr_flag_jailsys;
4147672756aaSJamie Gritton 	     jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4148672756aaSJamie Gritton 	     jsf++) {
4149672756aaSJamie Gritton 		f = pr->pr_flags & (jsf->disable | jsf->new);
4150672756aaSJamie Gritton 		db_printf(" %-16s= %s\n", jsf->name,
4151672756aaSJamie Gritton 		    (f != 0 && f == jsf->disable) ? "disable"
4152672756aaSJamie Gritton 		    : (f == jsf->new) ? "new"
41537cbf7213SJamie Gritton 		    : "inherit");
41547cbf7213SJamie Gritton 	}
4155fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
4156672756aaSJamie Gritton 	for (bf = pr_flag_allow;
41570e5c6bd4SJamie Gritton 	     bf < pr_flag_allow + nitems(pr_flag_allow) && bf->flag != 0;
4158672756aaSJamie Gritton 	     bf++)
4159672756aaSJamie Gritton 		if (pr->pr_allow & bf->flag)
4160672756aaSJamie Gritton 			db_printf(" %s", bf->name);
4161b38ff370SJamie Gritton 	db_printf("\n");
41620304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4163c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4164c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4165c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
416676ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4167413628a7SBjoern A. Zeeb #ifdef INET
4168b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4169b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4170b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4171fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
41728144690aSEric van Gyzen 		    inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4173413628a7SBjoern A. Zeeb #endif
4174413628a7SBjoern A. Zeeb #ifdef INET6
4175b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4176b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4177b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4178fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4179b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4180b38ff370SJamie Gritton #endif
4181b38ff370SJamie Gritton }
4182b38ff370SJamie Gritton 
4183b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4184b38ff370SJamie Gritton {
4185b38ff370SJamie Gritton 	struct prison *pr;
4186b38ff370SJamie Gritton 
4187b38ff370SJamie Gritton 	if (!have_addr) {
41880304c731SJamie Gritton 		/*
41890304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
41900304c731SJamie Gritton 		 * listed.
41910304c731SJamie Gritton 		 */
41920304c731SJamie Gritton 		db_show_prison(&prison0);
41930304c731SJamie Gritton 		if (!db_pager_quit) {
4194b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4195b38ff370SJamie Gritton 				db_show_prison(pr);
4196413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4197413628a7SBjoern A. Zeeb 					break;
4198413628a7SBjoern A. Zeeb 			}
41990304c731SJamie Gritton 		}
4200b38ff370SJamie Gritton 		return;
4201413628a7SBjoern A. Zeeb 	}
4202b38ff370SJamie Gritton 
42030304c731SJamie Gritton 	if (addr == 0)
42040304c731SJamie Gritton 		pr = &prison0;
42050304c731SJamie Gritton 	else {
4206b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4207b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4208b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4209b38ff370SJamie Gritton 				break;
4210b38ff370SJamie Gritton 		if (pr == NULL)
4211b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4212b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4213b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4214b38ff370SJamie Gritton 					break;
4215b38ff370SJamie Gritton 		if (pr == NULL)
4216b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4217b38ff370SJamie Gritton 			pr = (struct prison *)addr;
42180304c731SJamie Gritton 	}
4219b38ff370SJamie Gritton 	db_show_prison(pr);
4220b38ff370SJamie Gritton }
4221b38ff370SJamie Gritton 
4222413628a7SBjoern A. Zeeb #endif /* DDB */
4223