xref: /freebsd/sys/kern/kern_jail.c (revision 679e13901cd9ae2a491e6ac63c29c7d60e581deb)
19454b2d8SWarner Losh /*-
2413628a7SBjoern A. Zeeb  * Copyright (c) 1999 Poul-Henning Kamp.
3413628a7SBjoern A. Zeeb  * Copyright (c) 2008 Bjoern A. Zeeb.
4b38ff370SJamie Gritton  * Copyright (c) 2009 James Gritton.
5413628a7SBjoern A. Zeeb  * All rights reserved.
6b9f0b66cSBjoern A. Zeeb  *
7b9f0b66cSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
8b9f0b66cSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
9b9f0b66cSBjoern A. Zeeb  * are met:
10b9f0b66cSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
11b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
12b9f0b66cSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
13b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
14b9f0b66cSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
15b9f0b66cSBjoern A. Zeeb  *
16b9f0b66cSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b9f0b66cSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b9f0b66cSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b9f0b66cSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b9f0b66cSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b9f0b66cSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b9f0b66cSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b9f0b66cSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b9f0b66cSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b9f0b66cSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b9f0b66cSBjoern A. Zeeb  * SUCH DAMAGE.
2707901f22SPoul-Henning Kamp  */
2875c13541SPoul-Henning Kamp 
29677b542eSDavid E. O'Brien #include <sys/cdefs.h>
30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
31677b542eSDavid E. O'Brien 
3276ca6f88SJamie Gritton #include "opt_compat.h"
33413628a7SBjoern A. Zeeb #include "opt_ddb.h"
34413628a7SBjoern A. Zeeb #include "opt_inet.h"
35413628a7SBjoern A. Zeeb #include "opt_inet6.h"
3646e3b1cbSPawel Jakub Dawidek 
3775c13541SPoul-Henning Kamp #include <sys/param.h>
3875c13541SPoul-Henning Kamp #include <sys/types.h>
3975c13541SPoul-Henning Kamp #include <sys/kernel.h>
4075c13541SPoul-Henning Kamp #include <sys/systm.h>
4175c13541SPoul-Henning Kamp #include <sys/errno.h>
4275c13541SPoul-Henning Kamp #include <sys/sysproto.h>
4375c13541SPoul-Henning Kamp #include <sys/malloc.h>
440304c731SJamie Gritton #include <sys/osd.h>
45800c9408SRobert Watson #include <sys/priv.h>
4675c13541SPoul-Henning Kamp #include <sys/proc.h>
47b3059e09SRobert Watson #include <sys/taskqueue.h>
4857b4252eSKonstantin Belousov #include <sys/fcntl.h>
4975c13541SPoul-Henning Kamp #include <sys/jail.h>
5001137630SRobert Watson #include <sys/lock.h>
5101137630SRobert Watson #include <sys/mutex.h>
52dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5376ca6f88SJamie Gritton #include <sys/sysent.h>
54fd7a8150SMike Barcroft #include <sys/namei.h>
55820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
56fd7a8150SMike Barcroft #include <sys/queue.h>
5775c13541SPoul-Henning Kamp #include <sys/socket.h>
58fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
5983f1e257SRobert Watson #include <sys/sysctl.h>
60fd7a8150SMike Barcroft #include <sys/vnode.h>
61603724d3SBjoern A. Zeeb #include <sys/vimage.h>
6275c13541SPoul-Henning Kamp #include <net/if.h>
6375c13541SPoul-Henning Kamp #include <netinet/in.h>
64413628a7SBjoern A. Zeeb #ifdef DDB
65413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
66413628a7SBjoern A. Zeeb #ifdef INET6
67413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h>
68413628a7SBjoern A. Zeeb #endif /* INET6 */
69413628a7SBjoern A. Zeeb #endif /* DDB */
7075c13541SPoul-Henning Kamp 
71aed55708SRobert Watson #include <security/mac/mac_framework.h>
72aed55708SRobert Watson 
7375c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
7475c13541SPoul-Henning Kamp 
750304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
760304c731SJamie Gritton struct prison prison0 = {
770304c731SJamie Gritton 	.pr_id		= 0,
780304c731SJamie Gritton 	.pr_name	= "0",
790304c731SJamie Gritton 	.pr_ref		= 1,
800304c731SJamie Gritton 	.pr_uref	= 1,
810304c731SJamie Gritton 	.pr_path	= "/",
820304c731SJamie Gritton 	.pr_securelevel	= -1,
83c1f19219SJamie Gritton 	.pr_hostuuid	= "00000000-0000-0000-0000-000000000000",
840304c731SJamie Gritton 	.pr_children	= LIST_HEAD_INITIALIZER(&prison0.pr_children),
8576ca6f88SJamie Gritton 	.pr_flags	= PR_HOST,
860304c731SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL,
870304c731SJamie Gritton };
880304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
8983f1e257SRobert Watson 
900304c731SJamie Gritton /* allprison and lastprid are protected by allprison_lock. */
91dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
92b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
93b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
942110d913SXin LI int	lastprid = 0;
95fd7a8150SMike Barcroft 
96b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
97b3059e09SRobert Watson static void prison_complete(void *context, int pending);
98b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
990304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1000304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
101413628a7SBjoern A. Zeeb #ifdef INET
1028571af59SJamie Gritton static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
1030304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
104413628a7SBjoern A. Zeeb #endif
105413628a7SBjoern A. Zeeb #ifdef INET6
1068571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
1070304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
108413628a7SBjoern A. Zeeb #endif
109fd7a8150SMike Barcroft 
110b38ff370SJamie Gritton /* Flags for prison_deref */
111b38ff370SJamie Gritton #define	PD_DEREF	0x01
112b38ff370SJamie Gritton #define	PD_DEUREF	0x02
113b38ff370SJamie Gritton #define	PD_LOCKED	0x04
114b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
115b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
116fd7a8150SMike Barcroft 
1170304c731SJamie Gritton /*
1180304c731SJamie Gritton  * Parameter names corresponding to PR_* flag values
1190304c731SJamie Gritton  */
1200304c731SJamie Gritton static char *pr_flag_names[] = {
1210304c731SJamie Gritton 	[0] = "persist",
12276ca6f88SJamie Gritton 	"host",
1230304c731SJamie Gritton #ifdef INET
12476ca6f88SJamie Gritton 	"ip4",
1250304c731SJamie Gritton #endif
1260304c731SJamie Gritton #ifdef INET6
1270304c731SJamie Gritton 	[3] = "ip6",
1280304c731SJamie Gritton #endif
129679e1390SJamie Gritton #ifdef VIMAGE
130679e1390SJamie Gritton 	[4] = "vnet",
131679e1390SJamie Gritton #endif
1320304c731SJamie Gritton };
1330304c731SJamie Gritton 
1340304c731SJamie Gritton static char *pr_flag_nonames[] = {
1350304c731SJamie Gritton 	[0] = "nopersist",
13676ca6f88SJamie Gritton 	"nohost",
1370304c731SJamie Gritton #ifdef INET
13876ca6f88SJamie Gritton 	"noip4",
1390304c731SJamie Gritton #endif
1400304c731SJamie Gritton #ifdef INET6
1410304c731SJamie Gritton 	[3] = "noip6",
1420304c731SJamie Gritton #endif
143679e1390SJamie Gritton #ifdef VIMAGE
144679e1390SJamie Gritton 	[4] = "novnet",
145679e1390SJamie Gritton #endif
1460304c731SJamie Gritton };
1470304c731SJamie Gritton 
1480304c731SJamie Gritton static char *pr_allow_names[] = {
1490304c731SJamie Gritton 	"allow.set_hostname",
1500304c731SJamie Gritton 	"allow.sysvipc",
1510304c731SJamie Gritton 	"allow.raw_sockets",
1520304c731SJamie Gritton 	"allow.chflags",
1530304c731SJamie Gritton 	"allow.mount",
1540304c731SJamie Gritton 	"allow.quotas",
1550304c731SJamie Gritton 	"allow.jails",
1560304c731SJamie Gritton 	"allow.socket_af",
1570304c731SJamie Gritton };
1580304c731SJamie Gritton 
1590304c731SJamie Gritton static char *pr_allow_nonames[] = {
1600304c731SJamie Gritton 	"allow.noset_hostname",
1610304c731SJamie Gritton 	"allow.nosysvipc",
1620304c731SJamie Gritton 	"allow.noraw_sockets",
1630304c731SJamie Gritton 	"allow.nochflags",
1640304c731SJamie Gritton 	"allow.nomount",
1650304c731SJamie Gritton 	"allow.noquotas",
1660304c731SJamie Gritton 	"allow.nojails",
1670304c731SJamie Gritton 	"allow.nosocket_af",
1680304c731SJamie Gritton };
1690304c731SJamie Gritton 
1700304c731SJamie Gritton #define	JAIL_DEFAULT_ALLOW	PR_ALLOW_SET_HOSTNAME
1710304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
1720304c731SJamie Gritton static int jail_default_enforce_statfs = 2;
1730304c731SJamie Gritton #if defined(INET) || defined(INET6)
174e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
1750304c731SJamie Gritton #endif
1760304c731SJamie Gritton 
177413628a7SBjoern A. Zeeb #ifdef INET
178413628a7SBjoern A. Zeeb static int
179413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2)
180413628a7SBjoern A. Zeeb {
181413628a7SBjoern A. Zeeb 	in_addr_t iaa, iab;
182413628a7SBjoern A. Zeeb 
183413628a7SBjoern A. Zeeb 	/*
184413628a7SBjoern A. Zeeb 	 * We need to compare in HBO here to get the list sorted as expected
185413628a7SBjoern A. Zeeb 	 * by the result of the code.  Sorting NBO addresses gives you
186413628a7SBjoern A. Zeeb 	 * interesting results.  If you do not understand, do not try.
187413628a7SBjoern A. Zeeb 	 */
188413628a7SBjoern A. Zeeb 	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
189413628a7SBjoern A. Zeeb 	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
190413628a7SBjoern A. Zeeb 
191413628a7SBjoern A. Zeeb 	/*
192413628a7SBjoern A. Zeeb 	 * Do not simply return the difference of the two numbers, the int is
193413628a7SBjoern A. Zeeb 	 * not wide enough.
194413628a7SBjoern A. Zeeb 	 */
195413628a7SBjoern A. Zeeb 	if (iaa > iab)
196413628a7SBjoern A. Zeeb 		return (1);
197413628a7SBjoern A. Zeeb 	else if (iaa < iab)
198413628a7SBjoern A. Zeeb 		return (-1);
199413628a7SBjoern A. Zeeb 	else
200413628a7SBjoern A. Zeeb 		return (0);
201413628a7SBjoern A. Zeeb }
202413628a7SBjoern A. Zeeb #endif
203413628a7SBjoern A. Zeeb 
204413628a7SBjoern A. Zeeb #ifdef INET6
205413628a7SBjoern A. Zeeb static int
206413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2)
207413628a7SBjoern A. Zeeb {
208413628a7SBjoern A. Zeeb 	const struct in6_addr *ia6a, *ia6b;
209413628a7SBjoern A. Zeeb 	int i, rc;
210413628a7SBjoern A. Zeeb 
211413628a7SBjoern A. Zeeb 	ia6a = (const struct in6_addr *)ip1;
212413628a7SBjoern A. Zeeb 	ia6b = (const struct in6_addr *)ip2;
213413628a7SBjoern A. Zeeb 
214413628a7SBjoern A. Zeeb 	rc = 0;
215413628a7SBjoern A. Zeeb 	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
216413628a7SBjoern A. Zeeb 		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
217413628a7SBjoern A. Zeeb 			rc = 1;
218413628a7SBjoern A. Zeeb 		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
219413628a7SBjoern A. Zeeb 			rc = -1;
220413628a7SBjoern A. Zeeb 	}
221413628a7SBjoern A. Zeeb 	return (rc);
222413628a7SBjoern A. Zeeb }
223413628a7SBjoern A. Zeeb #endif
224413628a7SBjoern A. Zeeb 
225116734c4SMatthew Dillon /*
2269ddb7954SMike Barcroft  * struct jail_args {
2279ddb7954SMike Barcroft  *	struct jail *jail;
2289ddb7954SMike Barcroft  * };
229116734c4SMatthew Dillon  */
23075c13541SPoul-Henning Kamp int
2319ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap)
23275c13541SPoul-Henning Kamp {
233413628a7SBjoern A. Zeeb 	uint32_t version;
234413628a7SBjoern A. Zeeb 	int error;
2350304c731SJamie Gritton 	struct jail j;
236413628a7SBjoern A. Zeeb 
237413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
238413628a7SBjoern A. Zeeb 	if (error)
239413628a7SBjoern A. Zeeb 		return (error);
240413628a7SBjoern A. Zeeb 
241413628a7SBjoern A. Zeeb 	switch (version) {
242413628a7SBjoern A. Zeeb 	case 0:
243413628a7SBjoern A. Zeeb 	{
244413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
245413628a7SBjoern A. Zeeb 
2460304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2470304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
248413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
249413628a7SBjoern A. Zeeb 		if (error)
250413628a7SBjoern A. Zeeb 			return (error);
2510304c731SJamie Gritton 		j.version = j0.version;
2520304c731SJamie Gritton 		j.path = j0.path;
2530304c731SJamie Gritton 		j.hostname = j0.hostname;
2540304c731SJamie Gritton 		j.ip4s = j0.ip_number;
255413628a7SBjoern A. Zeeb 		break;
256413628a7SBjoern A. Zeeb 	}
257413628a7SBjoern A. Zeeb 
258413628a7SBjoern A. Zeeb 	case 1:
259413628a7SBjoern A. Zeeb 		/*
260413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
261413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
262413628a7SBjoern A. Zeeb 		 */
263413628a7SBjoern A. Zeeb 		return (EINVAL);
264413628a7SBjoern A. Zeeb 
265413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
266413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
267413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
268413628a7SBjoern A. Zeeb 		if (error)
269413628a7SBjoern A. Zeeb 			return (error);
2700304c731SJamie Gritton 		break;
2710304c731SJamie Gritton 
2720304c731SJamie Gritton 	default:
2730304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
2740304c731SJamie Gritton 		return (EINVAL);
2750304c731SJamie Gritton 	}
2760304c731SJamie Gritton 	return (kern_jail(td, &j));
2770304c731SJamie Gritton }
2780304c731SJamie Gritton 
2790304c731SJamie Gritton int
2800304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j)
2810304c731SJamie Gritton {
282e92e0574SJamie Gritton 	struct iovec optiov[2 * (4
283e92e0574SJamie Gritton 			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
284e92e0574SJamie Gritton #ifdef INET
285e92e0574SJamie Gritton 			    + 1
286e92e0574SJamie Gritton #endif
287e92e0574SJamie Gritton #ifdef INET6
288e92e0574SJamie Gritton 			    + 1
289e92e0574SJamie Gritton #endif
290e92e0574SJamie Gritton 			    )];
2910304c731SJamie Gritton 	struct uio opt;
2920304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
2930304c731SJamie Gritton #ifdef INET
294e92e0574SJamie Gritton 	uint32_t ip4s;
2950304c731SJamie Gritton 	struct in_addr *u_ip4;
2960304c731SJamie Gritton #endif
2970304c731SJamie Gritton #ifdef INET6
2980304c731SJamie Gritton 	struct in6_addr *u_ip6;
2990304c731SJamie Gritton #endif
3000304c731SJamie Gritton 	size_t tmplen;
3010304c731SJamie Gritton 	int error, enforce_statfs, fi;
3020304c731SJamie Gritton 
3030304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3040304c731SJamie Gritton 	opt.uio_iov = optiov;
3050304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3060304c731SJamie Gritton 	opt.uio_offset = -1;
3070304c731SJamie Gritton 	opt.uio_resid = -1;
3080304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3090304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3100304c731SJamie Gritton 	opt.uio_td = td;
3110304c731SJamie Gritton 
3120304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3130304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
3140304c731SJamie Gritton 		for (fi = 0; fi < sizeof(pr_allow_names) /
3150304c731SJamie Gritton 		     sizeof(pr_allow_names[0]); fi++) {
3160304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_base =
3170304c731SJamie Gritton 			    (jail_default_allow & (1 << fi))
3180304c731SJamie Gritton 			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
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
4280304c731SJamie Gritton 	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
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
444b38ff370SJamie Gritton 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;
4730304c731SJamie Gritton 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
474b38ff370SJamie Gritton 	struct vnode *root;
47576ca6f88SJamie Gritton 	char *domain, *errmsg, *host, *name, *p, *path, *uuid;
4760304c731SJamie Gritton #if defined(INET) || defined(INET6)
477b38ff370SJamie Gritton 	void *op;
4780304c731SJamie Gritton #endif
47976ca6f88SJamie Gritton 	unsigned long hid;
4800304c731SJamie Gritton 	size_t namelen, onamelen;
4810304c731SJamie Gritton 	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
48276ca6f88SJamie Gritton 	int gotenforce, gothid, gotslevel, fi, jid, len;
483b38ff370SJamie Gritton 	int slevel, vfslocked;
484d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
4850304c731SJamie Gritton 	int ii, ij;
486413628a7SBjoern A. Zeeb #endif
487413628a7SBjoern A. Zeeb #ifdef INET
4880304c731SJamie Gritton 	int ip4s, ip4a, redo_ip4;
489413628a7SBjoern A. Zeeb #endif
490b38ff370SJamie Gritton #ifdef INET6
4910304c731SJamie Gritton 	int ip6s, ip6a, redo_ip6;
492b38ff370SJamie Gritton #endif
493b38ff370SJamie Gritton 	unsigned pr_flags, ch_flags;
4940304c731SJamie Gritton 	unsigned pr_allow, ch_allow, tallow;
495b38ff370SJamie Gritton 	char numbuf[12];
496b38ff370SJamie Gritton 
497b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
498b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
499b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
500b38ff370SJamie Gritton 	if (error)
50175c13541SPoul-Henning Kamp 		return (error);
5020304c731SJamie Gritton 	mypr = ppr = td->td_ucred->cr_prison;
5030304c731SJamie Gritton 	if ((flags & JAIL_CREATE) && !(mypr->pr_allow & PR_ALLOW_JAILS))
5040304c731SJamie Gritton 		return (EPERM);
505b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
506b38ff370SJamie Gritton 		return (EINVAL);
507b38ff370SJamie Gritton 
508b38ff370SJamie Gritton 	/*
509b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
510b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
511b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
512b38ff370SJamie Gritton 	 * the allprison lock.
513b38ff370SJamie Gritton 	 *
514b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
515b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
516b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
517b38ff370SJamie Gritton 	 */
518b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
519b38ff370SJamie Gritton 	if (error)
520b38ff370SJamie Gritton 		return (error);
521b38ff370SJamie Gritton #ifdef INET
5220304c731SJamie Gritton 	ip4a = 0;
523b38ff370SJamie Gritton 	ip4 = NULL;
524b38ff370SJamie Gritton #endif
525b38ff370SJamie Gritton #ifdef INET6
5260304c731SJamie Gritton 	ip6a = 0;
527b38ff370SJamie Gritton 	ip6 = NULL;
528b38ff370SJamie Gritton #endif
529b38ff370SJamie Gritton 
5300304c731SJamie Gritton #if defined(INET) || defined(INET6)
5310304c731SJamie Gritton  again:
5320304c731SJamie Gritton #endif
533b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
534b38ff370SJamie Gritton 	if (error == ENOENT)
535b38ff370SJamie Gritton 		jid = 0;
536b38ff370SJamie Gritton 	else if (error != 0)
537b38ff370SJamie Gritton 		goto done_free;
538b38ff370SJamie Gritton 
539b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
540b38ff370SJamie Gritton 	if (error == ENOENT)
541b38ff370SJamie Gritton 		gotslevel = 0;
542b38ff370SJamie Gritton 	else if (error != 0)
543b38ff370SJamie Gritton 		goto done_free;
544b38ff370SJamie Gritton 	else
545b38ff370SJamie Gritton 		gotslevel = 1;
546b38ff370SJamie Gritton 
5470304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
5480304c731SJamie Gritton 	gotenforce = (error == 0);
5490304c731SJamie Gritton 	if (gotenforce) {
5500304c731SJamie Gritton 		if (enforce < 0 || enforce > 2)
5510304c731SJamie Gritton 			return (EINVAL);
5520304c731SJamie Gritton 	} else if (error != ENOENT)
5530304c731SJamie Gritton 		goto done_free;
5540304c731SJamie Gritton 
555b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
5560304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
5570304c731SJamie Gritton 	    fi++) {
5580304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
5590304c731SJamie Gritton 			continue;
5600304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
5610304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
5620304c731SJamie Gritton 	}
563b38ff370SJamie Gritton 	ch_flags |= pr_flags;
564b38ff370SJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
565b38ff370SJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
566b38ff370SJamie Gritton 		error = EINVAL;
567b38ff370SJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
568b38ff370SJamie Gritton 		goto done_errmsg;
569b38ff370SJamie Gritton 	}
570679e1390SJamie Gritton #ifdef VIMAGE
571679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
572679e1390SJamie Gritton 		error = EINVAL;
573679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
574679e1390SJamie Gritton 		goto done_errmsg;
575679e1390SJamie Gritton 	}
576679e1390SJamie Gritton #endif
577b38ff370SJamie Gritton 
5780304c731SJamie Gritton 	pr_allow = ch_allow = 0;
5790304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
5800304c731SJamie Gritton 	    fi++) {
5810304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
5820304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
5830304c731SJamie Gritton 	}
5840304c731SJamie Gritton 	ch_allow |= pr_allow;
5850304c731SJamie Gritton 
586b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
587b38ff370SJamie Gritton 	if (error == ENOENT)
588b38ff370SJamie Gritton 		name = NULL;
589b38ff370SJamie Gritton 	else if (error != 0)
590b38ff370SJamie Gritton 		goto done_free;
591b38ff370SJamie Gritton 	else {
592b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
593b38ff370SJamie Gritton 			error = EINVAL;
594b38ff370SJamie Gritton 			goto done_free;
595b38ff370SJamie Gritton 		}
596b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
597b38ff370SJamie Gritton 			error = ENAMETOOLONG;
598b38ff370SJamie Gritton 			goto done_free;
599b38ff370SJamie Gritton 		}
600b38ff370SJamie Gritton 	}
601b38ff370SJamie Gritton 
602b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
603b38ff370SJamie Gritton 	if (error == ENOENT)
604b38ff370SJamie Gritton 		host = NULL;
605b38ff370SJamie Gritton 	else if (error != 0)
606b38ff370SJamie Gritton 		goto done_free;
607b38ff370SJamie Gritton 	else {
60876ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
60976ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
610b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
611b38ff370SJamie Gritton 			error = EINVAL;
612b38ff370SJamie Gritton 			goto done_free;
613b38ff370SJamie Gritton 		}
614b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
615b38ff370SJamie Gritton 			error = ENAMETOOLONG;
616b38ff370SJamie Gritton 			goto done_free;
617b38ff370SJamie Gritton 		}
618b38ff370SJamie Gritton 	}
619b38ff370SJamie Gritton 
62076ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
62176ca6f88SJamie Gritton 	if (error == ENOENT)
62276ca6f88SJamie Gritton 		domain = NULL;
62376ca6f88SJamie Gritton 	else if (error != 0)
62476ca6f88SJamie Gritton 		goto done_free;
62576ca6f88SJamie Gritton 	else {
62676ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
62776ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
62876ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
62976ca6f88SJamie Gritton 			error = EINVAL;
63076ca6f88SJamie Gritton 			goto done_free;
63176ca6f88SJamie Gritton 		}
63276ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
63376ca6f88SJamie Gritton 			error = ENAMETOOLONG;
63476ca6f88SJamie Gritton 			goto done_free;
63576ca6f88SJamie Gritton 		}
63676ca6f88SJamie Gritton 	}
63776ca6f88SJamie Gritton 
63876ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
63976ca6f88SJamie Gritton 	if (error == ENOENT)
64076ca6f88SJamie Gritton 		uuid = NULL;
64176ca6f88SJamie Gritton 	else if (error != 0)
64276ca6f88SJamie Gritton 		goto done_free;
64376ca6f88SJamie Gritton 	else {
64476ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
64576ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
64676ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
64776ca6f88SJamie Gritton 			error = EINVAL;
64876ca6f88SJamie Gritton 			goto done_free;
64976ca6f88SJamie Gritton 		}
65076ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
65176ca6f88SJamie Gritton 			error = ENAMETOOLONG;
65276ca6f88SJamie Gritton 			goto done_free;
65376ca6f88SJamie Gritton 		}
65476ca6f88SJamie Gritton 	}
65576ca6f88SJamie Gritton 
65676ca6f88SJamie Gritton #ifdef COMPAT_IA32
65776ca6f88SJamie Gritton 	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
65876ca6f88SJamie Gritton 		uint32_t hid32;
65976ca6f88SJamie Gritton 
66076ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
66176ca6f88SJamie Gritton 		hid = hid32;
66276ca6f88SJamie Gritton 	} else
66376ca6f88SJamie Gritton #endif
66476ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
66576ca6f88SJamie Gritton 	if (error == ENOENT)
66676ca6f88SJamie Gritton 		gothid = 0;
66776ca6f88SJamie Gritton 	else if (error != 0)
66876ca6f88SJamie Gritton 		goto done_free;
66976ca6f88SJamie Gritton 	else {
67076ca6f88SJamie Gritton 		gothid = 1;
67176ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
67276ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
67376ca6f88SJamie Gritton 	}
67476ca6f88SJamie Gritton 
6750304c731SJamie Gritton 	/* This might be the second time around for this option. */
676b38ff370SJamie Gritton #ifdef INET
677b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
678b38ff370SJamie Gritton 	if (error == ENOENT)
679b38ff370SJamie Gritton 		ip4s = -1;
680b38ff370SJamie Gritton 	else if (error != 0)
681b38ff370SJamie Gritton 		goto done_free;
682b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
683b38ff370SJamie Gritton 		error = EINVAL;
684b38ff370SJamie Gritton 		goto done_free;
6850304c731SJamie Gritton 	} else {
6860304c731SJamie Gritton 		ch_flags |= PR_IP4_USER;
6870304c731SJamie Gritton 		pr_flags |= PR_IP4_USER;
6880304c731SJamie Gritton 		if (ip4s > 0) {
689b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
690b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
691b38ff370SJamie Gritton 				error = EINVAL;
692b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
693b38ff370SJamie Gritton 				goto done_errmsg;
694b38ff370SJamie Gritton 			}
6950304c731SJamie Gritton 			if (ip4a < ip4s) {
6960304c731SJamie Gritton 				ip4a = ip4s;
6970304c731SJamie Gritton 				free(ip4, M_PRISON);
6980304c731SJamie Gritton 				ip4 = NULL;
6990304c731SJamie Gritton 			}
7000304c731SJamie Gritton 			if (ip4 == NULL)
7010304c731SJamie Gritton 				ip4 = malloc(ip4a * sizeof(*ip4), M_PRISON,
7020304c731SJamie Gritton 				    M_WAITOK);
703b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
704b38ff370SJamie Gritton 			/*
7050304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
7060304c731SJamie Gritton 			 * the primary IP address as given from userland.
7070304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
7080304c731SJamie Gritton 			 * connections as well for "loopback" traffic.
709b38ff370SJamie Gritton 			 */
710b38ff370SJamie Gritton 			if (ip4s > 1)
711b38ff370SJamie Gritton 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
712b38ff370SJamie Gritton 			/*
7130304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
7140304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
7150304c731SJamie Gritton 			 * addresses it is their problem.
716b38ff370SJamie Gritton 			 *
7170304c731SJamie Gritton 			 * We do not have to care about byte order for these
7180304c731SJamie Gritton 			 * checks so we will do them in NBO.
719b38ff370SJamie Gritton 			 */
720b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
721b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
722b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
723b38ff370SJamie Gritton 					error = EINVAL;
724b38ff370SJamie Gritton 					goto done_free;
725b38ff370SJamie Gritton 				}
726b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
727b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
728b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
729b38ff370SJamie Gritton 					error = EINVAL;
730b38ff370SJamie Gritton 					goto done_free;
731b38ff370SJamie Gritton 				}
732b38ff370SJamie Gritton 			}
733b38ff370SJamie Gritton 		}
7340304c731SJamie Gritton 	}
735b38ff370SJamie Gritton #endif
736b38ff370SJamie Gritton 
737b38ff370SJamie Gritton #ifdef INET6
738b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
739b38ff370SJamie Gritton 	if (error == ENOENT)
740b38ff370SJamie Gritton 		ip6s = -1;
741b38ff370SJamie Gritton 	else if (error != 0)
742b38ff370SJamie Gritton 		goto done_free;
743b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
744b38ff370SJamie Gritton 		error = EINVAL;
745b38ff370SJamie Gritton 		goto done_free;
7460304c731SJamie Gritton 	} else {
7470304c731SJamie Gritton 		ch_flags |= PR_IP6_USER;
7480304c731SJamie Gritton 		pr_flags |= PR_IP6_USER;
7490304c731SJamie Gritton 		if (ip6s > 0) {
750b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
751b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
752b38ff370SJamie Gritton 				error = EINVAL;
753b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
754b38ff370SJamie Gritton 				goto done_errmsg;
755b38ff370SJamie Gritton 			}
7560304c731SJamie Gritton 			if (ip6a < ip6s) {
7570304c731SJamie Gritton 				ip6a = ip6s;
7580304c731SJamie Gritton 				free(ip6, M_PRISON);
7590304c731SJamie Gritton 				ip6 = NULL;
7600304c731SJamie Gritton 			}
7610304c731SJamie Gritton 			if (ip6 == NULL)
7620304c731SJamie Gritton 				ip6 = malloc(ip6a * sizeof(*ip6), M_PRISON,
7630304c731SJamie Gritton 				    M_WAITOK);
764b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
765b38ff370SJamie Gritton 			if (ip6s > 1)
766b38ff370SJamie Gritton 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
767b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
7680304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
769b38ff370SJamie Gritton 					error = EINVAL;
770b38ff370SJamie Gritton 					goto done_free;
771b38ff370SJamie Gritton 				}
772b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
773b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
774b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
775b38ff370SJamie Gritton 				{
776b38ff370SJamie Gritton 					error = EINVAL;
777b38ff370SJamie Gritton 					goto done_free;
778b38ff370SJamie Gritton 				}
779b38ff370SJamie Gritton 			}
780b38ff370SJamie Gritton 		}
7810304c731SJamie Gritton 	}
782b38ff370SJamie Gritton #endif
783b38ff370SJamie Gritton 
784b38ff370SJamie Gritton 	root = NULL;
785b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
786b38ff370SJamie Gritton 	if (error == ENOENT)
787b38ff370SJamie Gritton 		path = NULL;
788b38ff370SJamie Gritton 	else if (error != 0)
789b38ff370SJamie Gritton 		goto done_free;
790b38ff370SJamie Gritton 	else {
791b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
792b38ff370SJamie Gritton 			error = EINVAL;
793b38ff370SJamie Gritton 			vfs_opterror(opts,
794b38ff370SJamie Gritton 			    "path cannot be changed after creation");
795b38ff370SJamie Gritton 			goto done_errmsg;
796b38ff370SJamie Gritton 		}
797b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
798b38ff370SJamie Gritton 			error = EINVAL;
799b38ff370SJamie Gritton 			goto done_free;
800b38ff370SJamie Gritton 		}
801b38ff370SJamie Gritton 		if (len < 2 || (len == 2 && path[0] == '/'))
802b38ff370SJamie Gritton 			path = NULL;
803b38ff370SJamie Gritton 		else {
8040304c731SJamie Gritton 			/* Leave room for a real-root full pathname. */
8050304c731SJamie Gritton 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
8060304c731SJamie Gritton 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
8070304c731SJamie Gritton 				error = ENAMETOOLONG;
8080304c731SJamie Gritton 				goto done_free;
8090304c731SJamie Gritton 			}
810b38ff370SJamie Gritton 			NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
811b38ff370SJamie Gritton 			    path, td);
812b38ff370SJamie Gritton 			error = namei(&nd);
813b38ff370SJamie Gritton 			if (error)
814b38ff370SJamie Gritton 				goto done_free;
815b38ff370SJamie Gritton 			vfslocked = NDHASGIANT(&nd);
816b38ff370SJamie Gritton 			root = nd.ni_vp;
817b38ff370SJamie Gritton 			NDFREE(&nd, NDF_ONLY_PNBUF);
818b38ff370SJamie Gritton 			if (root->v_type != VDIR) {
819b38ff370SJamie Gritton 				error = ENOTDIR;
820b38ff370SJamie Gritton 				vrele(root);
821b38ff370SJamie Gritton 				VFS_UNLOCK_GIANT(vfslocked);
822b38ff370SJamie Gritton 				goto done_free;
823b38ff370SJamie Gritton 			}
824b38ff370SJamie Gritton 			VFS_UNLOCK_GIANT(vfslocked);
825b38ff370SJamie Gritton 		}
826b38ff370SJamie Gritton 	}
827b38ff370SJamie Gritton 
828b38ff370SJamie Gritton 	/*
829b38ff370SJamie Gritton 	 * Grab the allprison lock before letting modules check their
830b38ff370SJamie Gritton 	 * parameters.  Once we have it, do not let go so we'll have a
831b38ff370SJamie Gritton 	 * consistent view of the OSD list.
832b38ff370SJamie Gritton 	 */
833b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
834b38ff370SJamie Gritton 	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
835b38ff370SJamie Gritton 	if (error)
836b38ff370SJamie Gritton 		goto done_unlock_list;
837b38ff370SJamie Gritton 
838b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
839b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
840b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
841b38ff370SJamie Gritton 			error = EINVAL;
842b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
843b38ff370SJamie Gritton 			goto done_unlock_list;
844b38ff370SJamie Gritton 		}
845b38ff370SJamie Gritton 	}
846b38ff370SJamie Gritton 
847b38ff370SJamie Gritton 	/*
848b38ff370SJamie Gritton 	 * See if we are creating a new record or updating an existing one.
849b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
850b38ff370SJamie Gritton 	 */
851b38ff370SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
852b38ff370SJamie Gritton 	if (!cuflags) {
853b38ff370SJamie Gritton 		error = EINVAL;
854b38ff370SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
855b38ff370SJamie Gritton 		goto done_unlock_list;
856b38ff370SJamie Gritton 	}
857b38ff370SJamie Gritton 	pr = NULL;
858b38ff370SJamie Gritton 	if (jid != 0) {
8590304c731SJamie Gritton 		/*
8600304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
8610304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
8620304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
8630304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
8640304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
8650304c731SJamie Gritton 		 */
866b38ff370SJamie Gritton 		if (jid < 0) {
867b38ff370SJamie Gritton 			error = EINVAL;
868b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
869b38ff370SJamie Gritton 			goto done_unlock_list;
870b38ff370SJamie Gritton 		}
871b38ff370SJamie Gritton 		pr = prison_find(jid);
872b38ff370SJamie Gritton 		if (pr != NULL) {
8730304c731SJamie Gritton 			ppr = pr->pr_parent;
874b38ff370SJamie Gritton 			/* Create: jid must not exist. */
875b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
876b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
877b38ff370SJamie Gritton 				error = EEXIST;
878b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
879b38ff370SJamie Gritton 				    jid);
880b38ff370SJamie Gritton 				goto done_unlock_list;
881b38ff370SJamie Gritton 			}
8820304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
8830304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
8840304c731SJamie Gritton 				pr = NULL;
8850304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
886b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
887b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
888b38ff370SJamie Gritton 					error = ENOENT;
889b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
890b38ff370SJamie Gritton 					    jid);
891b38ff370SJamie Gritton 					goto done_unlock_list;
892b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
893b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
894b38ff370SJamie Gritton 					/*
895b38ff370SJamie Gritton 					 * A dying jail might be resurrected
896b38ff370SJamie Gritton 					 * (via attach or persist), but first
897b38ff370SJamie Gritton 					 * it must determine if another jail
898b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
899b38ff370SJamie Gritton 					 * this by implicitly re-setting the
900b38ff370SJamie Gritton 					 * name.
901b38ff370SJamie Gritton 					 */
902b38ff370SJamie Gritton 					if (name == NULL)
9030304c731SJamie Gritton 						name = prison_name(mypr, pr);
904b38ff370SJamie Gritton 				}
905b38ff370SJamie Gritton 			}
906b38ff370SJamie Gritton 		}
907b38ff370SJamie Gritton 		if (pr == NULL) {
908b38ff370SJamie Gritton 			/* Update: jid must exist. */
909b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
910b38ff370SJamie Gritton 				error = ENOENT;
911b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
912b38ff370SJamie Gritton 				goto done_unlock_list;
913b38ff370SJamie Gritton 			}
914b38ff370SJamie Gritton 		}
915b38ff370SJamie Gritton 	}
916b38ff370SJamie Gritton 	/*
917b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
918b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
919b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
920b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
921b38ff370SJamie Gritton 	 * because that is the jail being updated).
922b38ff370SJamie Gritton 	 */
923b38ff370SJamie Gritton 	if (name != NULL) {
9240304c731SJamie Gritton 		p = strrchr(name, '.');
9250304c731SJamie Gritton 		if (p != NULL) {
9260304c731SJamie Gritton 			/*
9270304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
9280304c731SJamie Gritton 			 * parent and child names, and make sure the parent
9290304c731SJamie Gritton 			 * exists or matches an already found jail.
9300304c731SJamie Gritton 			 */
9310304c731SJamie Gritton 			*p = '\0';
9320304c731SJamie Gritton 			if (pr != NULL) {
9330304c731SJamie Gritton 				if (strncmp(name, ppr->pr_name, p - name) ||
9340304c731SJamie Gritton 				    ppr->pr_name[p - name] != '\0') {
9350304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
9360304c731SJamie Gritton 					error = EINVAL;
9370304c731SJamie Gritton 					vfs_opterror(opts,
9380304c731SJamie Gritton 					    "cannot change jail's parent");
9390304c731SJamie Gritton 					goto done_unlock_list;
9400304c731SJamie Gritton 				}
9410304c731SJamie Gritton 			} else {
9420304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
9430304c731SJamie Gritton 				if (ppr == NULL) {
9440304c731SJamie Gritton 					error = ENOENT;
9450304c731SJamie Gritton 					vfs_opterror(opts,
9460304c731SJamie Gritton 					    "jail \"%s\" not found", name);
9470304c731SJamie Gritton 					goto done_unlock_list;
9480304c731SJamie Gritton 				}
9490304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
9500304c731SJamie Gritton 			}
9510304c731SJamie Gritton 			name = p + 1;
9520304c731SJamie Gritton 		}
953b38ff370SJamie Gritton 		if (name[0] != '\0') {
9540304c731SJamie Gritton 			namelen =
9550304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
956b38ff370SJamie Gritton  name_again:
9570304c731SJamie Gritton 			deadpr = NULL;
9580304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
959b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
9600304c731SJamie Gritton 				    !strcmp(tpr->pr_name + namelen, name)) {
961b38ff370SJamie Gritton 					if (pr == NULL &&
962b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
963b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
964b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
965b38ff370SJamie Gritton 							/*
966b38ff370SJamie Gritton 							 * Use this jail
967b38ff370SJamie Gritton 							 * for updates.
968b38ff370SJamie Gritton 							 */
969b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
970b38ff370SJamie Gritton 								pr = tpr;
971b38ff370SJamie Gritton 								break;
972b38ff370SJamie Gritton 							}
973b38ff370SJamie Gritton 							deadpr = tpr;
974b38ff370SJamie Gritton 						}
975b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
976b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
977b38ff370SJamie Gritton 						/*
978b38ff370SJamie Gritton 						 * Create, or update(jid):
979b38ff370SJamie Gritton 						 * name must not exist in an
9800304c731SJamie Gritton 						 * active sibling jail.
981b38ff370SJamie Gritton 						 */
982b38ff370SJamie Gritton 						error = EEXIST;
983b38ff370SJamie Gritton 						if (pr != NULL)
984b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
985b38ff370SJamie Gritton 						vfs_opterror(opts,
986b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
987b38ff370SJamie Gritton 						   name);
988b38ff370SJamie Gritton 						goto done_unlock_list;
989b38ff370SJamie Gritton 					}
990b38ff370SJamie Gritton 				}
991b38ff370SJamie Gritton 			}
992b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
993b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
994b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
995b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
996b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
997b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
998b38ff370SJamie Gritton 						goto name_again;
999b38ff370SJamie Gritton 					}
1000b38ff370SJamie Gritton 					pr = deadpr;
1001b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1002b38ff370SJamie Gritton 					error = ENOENT;
1003b38ff370SJamie Gritton 					vfs_opterror(opts,
1004b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1005b38ff370SJamie Gritton 					goto done_unlock_list;
1006b38ff370SJamie Gritton 				}
1007b38ff370SJamie Gritton 			}
1008b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1009b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1010b38ff370SJamie Gritton 				error = ENOENT;
1011b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1012b38ff370SJamie Gritton 				    name);
1013b38ff370SJamie Gritton 				goto done_unlock_list;
1014b38ff370SJamie Gritton 			}
1015b38ff370SJamie Gritton 		}
1016b38ff370SJamie Gritton 	}
1017b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1018b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1019b38ff370SJamie Gritton 		error = ENOENT;
1020b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1021b38ff370SJamie Gritton 		goto done_unlock_list;
1022b38ff370SJamie Gritton 	}
1023b38ff370SJamie Gritton 
1024b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1025b38ff370SJamie Gritton 	if (pr == NULL) {
1026b38ff370SJamie Gritton 		created = 1;
10270304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
10280304c731SJamie Gritton 		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
10290304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
10300304c731SJamie Gritton 			error = ENOENT;
10310304c731SJamie Gritton 			vfs_opterror(opts, "parent jail went away!");
10320304c731SJamie Gritton 			goto done_unlock_list;
10330304c731SJamie Gritton 		}
10340304c731SJamie Gritton 		ppr->pr_ref++;
10350304c731SJamie Gritton 		ppr->pr_uref++;
10360304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1037b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1038b38ff370SJamie Gritton 		if (jid == 0) {
1039b38ff370SJamie Gritton 			/* Find the next free jid. */
1040b38ff370SJamie Gritton 			jid = lastprid + 1;
1041b38ff370SJamie Gritton  findnext:
1042b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1043b38ff370SJamie Gritton 				jid = 1;
1044b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1045b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1046b38ff370SJamie Gritton 					continue;
1047b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1048b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1049b38ff370SJamie Gritton 					break;
1050b38ff370SJamie Gritton 				}
1051b38ff370SJamie Gritton 				if (jid == lastprid) {
1052b38ff370SJamie Gritton 					error = EAGAIN;
1053b38ff370SJamie Gritton 					vfs_opterror(opts,
1054b38ff370SJamie Gritton 					    "no available jail IDs");
1055b38ff370SJamie Gritton 					free(pr, M_PRISON);
10560304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
10570304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
10580304c731SJamie Gritton 					goto done_releroot;
1059b38ff370SJamie Gritton 				}
1060b38ff370SJamie Gritton 				jid++;
1061b38ff370SJamie Gritton 				goto findnext;
1062b38ff370SJamie Gritton 			}
1063b38ff370SJamie Gritton 			lastprid = jid;
1064b38ff370SJamie Gritton 		} else {
1065b38ff370SJamie Gritton 			/*
1066b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1067b38ff370SJamie Gritton 			 * so just find where to insert it.
1068b38ff370SJamie Gritton 			 */
1069b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1070b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1071b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1072b38ff370SJamie Gritton 					break;
1073b38ff370SJamie Gritton 				}
1074b38ff370SJamie Gritton 		}
1075b38ff370SJamie Gritton 		if (tpr == NULL)
1076b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
10770304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
10780304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
10790304c731SJamie Gritton 			tpr->pr_prisoncount++;
1080b38ff370SJamie Gritton 
10810304c731SJamie Gritton 		pr->pr_parent = ppr;
1082b38ff370SJamie Gritton 		pr->pr_id = jid;
10830304c731SJamie Gritton 
10840304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1085b38ff370SJamie Gritton 		if (name == NULL)
1086b38ff370SJamie Gritton 			name = "";
108776ca6f88SJamie Gritton 		if (host != NULL || domain != NULL || uuid != NULL || gothid) {
108876ca6f88SJamie Gritton 			if (host == NULL)
1089c1f19219SJamie Gritton 				host = ppr->pr_hostname;
109076ca6f88SJamie Gritton 			if (domain == NULL)
1091c1f19219SJamie Gritton 				domain = ppr->pr_domainname;
109276ca6f88SJamie Gritton 			if (uuid == NULL)
1093c1f19219SJamie Gritton 				uuid = ppr->pr_hostuuid;
109476ca6f88SJamie Gritton 			if (!gothid)
109576ca6f88SJamie Gritton 				hid = ppr->pr_hostid;
109676ca6f88SJamie Gritton 		}
1097b38ff370SJamie Gritton 		if (path == NULL) {
1098b38ff370SJamie Gritton 			path = "/";
10990304c731SJamie Gritton 			root = mypr->pr_root;
1100b38ff370SJamie Gritton 			vref(root);
1101b38ff370SJamie Gritton 		}
11020304c731SJamie Gritton #ifdef INET
11030304c731SJamie Gritton 		pr->pr_flags |= ppr->pr_flags & PR_IP4;
11040304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
11050304c731SJamie Gritton 		if (ppr->pr_ip4 != NULL) {
11060304c731SJamie Gritton 			pr->pr_ip4 = malloc(pr->pr_ip4s *
11070304c731SJamie Gritton 			    sizeof(struct in_addr), M_PRISON, M_WAITOK);
11080304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
11090304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*pr->pr_ip4));
11100304c731SJamie Gritton 		}
11110304c731SJamie Gritton #endif
11120304c731SJamie Gritton #ifdef INET6
11130304c731SJamie Gritton 		pr->pr_flags |= ppr->pr_flags & PR_IP6;
11140304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
11150304c731SJamie Gritton 		if (ppr->pr_ip6 != NULL) {
11160304c731SJamie Gritton 			pr->pr_ip6 = malloc(pr->pr_ip6s *
11170304c731SJamie Gritton 			    sizeof(struct in6_addr), M_PRISON, M_WAITOK);
11180304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
11190304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*pr->pr_ip6));
11200304c731SJamie Gritton 		}
11210304c731SJamie Gritton #endif
11220304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
11230304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
11240304c731SJamie Gritton 		pr->pr_enforce_statfs = ppr->pr_enforce_statfs;
1125b38ff370SJamie Gritton 
11260304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
11270304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1128b38ff370SJamie Gritton 
1129679e1390SJamie Gritton #ifdef VIMAGE
1130679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1131679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1132679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1133679e1390SJamie Gritton #endif
1134b38ff370SJamie Gritton 		/*
1135b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1136b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1137b38ff370SJamie Gritton 		 */
11380304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1139b38ff370SJamie Gritton 		if (error) {
1140b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1141b38ff370SJamie Gritton 			goto done_releroot;
1142b38ff370SJamie Gritton 		}
1143b38ff370SJamie Gritton 
1144b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1145b38ff370SJamie Gritton 		/*
1146b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1147b38ff370SJamie Gritton 		 * want other to see the incomplete prison once the
1148b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1149b38ff370SJamie Gritton 		 */
1150b38ff370SJamie Gritton 	} else {
1151b38ff370SJamie Gritton 		created = 0;
1152b38ff370SJamie Gritton 		/*
1153b38ff370SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
1154b38ff370SJamie Gritton 		 * continue to exist for the duration of the call.
1155b38ff370SJamie Gritton 		 */
1156b38ff370SJamie Gritton 		pr->pr_ref++;
1157b38ff370SJamie Gritton 	}
1158b38ff370SJamie Gritton 
1159b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
11600304c731SJamie Gritton 	if (gotslevel) {
11610304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
11620304c731SJamie Gritton 			error = EPERM;
11630304c731SJamie Gritton 			goto done_deref_locked;
11640304c731SJamie Gritton 		}
11650304c731SJamie Gritton 	}
11660304c731SJamie Gritton 	if (gotenforce) {
11670304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
11680304c731SJamie Gritton 			error = EPERM;
11690304c731SJamie Gritton 			goto done_deref_locked;
11700304c731SJamie Gritton 		}
11710304c731SJamie Gritton 	}
1172b38ff370SJamie Gritton #ifdef INET
11730304c731SJamie Gritton 	if (ch_flags & PR_IP4_USER) {
11740304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
11750304c731SJamie Gritton 			if (!(pr_flags & PR_IP4_USER)) {
1176b38ff370SJamie Gritton 				/*
11770304c731SJamie Gritton 				 * Silently ignore attempts to make the IP
11780304c731SJamie Gritton 				 * addresses unrestricted when the parent is
11790304c731SJamie Gritton 				 * restricted; in other words, interpret
11800304c731SJamie Gritton 				 * "unrestricted" as "as unrestricted as
11810304c731SJamie Gritton 				 * possible".
1182b38ff370SJamie Gritton 				 */
11830304c731SJamie Gritton 				ip4s = ppr->pr_ip4s;
11840304c731SJamie Gritton 				if (ip4s == 0) {
11850304c731SJamie Gritton 					free(ip4, M_PRISON);
11860304c731SJamie Gritton 					ip4 = NULL;
11870304c731SJamie Gritton 				} else if (ip4s <= ip4a) {
11880304c731SJamie Gritton 					/* Inherit the parent's address(es). */
11890304c731SJamie Gritton 					bcopy(ppr->pr_ip4, ip4,
11900304c731SJamie Gritton 					    ip4s * sizeof(*ip4));
11910304c731SJamie Gritton 				} else {
11920304c731SJamie Gritton 					/*
11930304c731SJamie Gritton 					 * There's no room for the parent's
11940304c731SJamie Gritton 					 * address list.  Allocate some more.
11950304c731SJamie Gritton 					 */
11960304c731SJamie Gritton 					ip4a = ip4s;
11970304c731SJamie Gritton 					free(ip4, M_PRISON);
11980304c731SJamie Gritton 					ip4 = malloc(ip4a * sizeof(*ip4),
11990304c731SJamie Gritton 					    M_PRISON, M_NOWAIT);
12000304c731SJamie Gritton 					if (ip4 != NULL)
12010304c731SJamie Gritton 						bcopy(ppr->pr_ip4, ip4,
12020304c731SJamie Gritton 						    ip4s * sizeof(*ip4));
12030304c731SJamie Gritton 					else {
12040304c731SJamie Gritton 						/* Allocation failed without
12050304c731SJamie Gritton 						 * sleeping.  Unlocking the
12060304c731SJamie Gritton 						 * prison now will invalidate
12070304c731SJamie Gritton 						 * some checks and prematurely
12080304c731SJamie Gritton 						 * show an unfinished new jail.
12090304c731SJamie Gritton 						 * So let go of everything and
12100304c731SJamie Gritton 						 * start over.
12110304c731SJamie Gritton 						 */
12120304c731SJamie Gritton 						prison_deref(pr, created
12130304c731SJamie Gritton 						    ? PD_LOCKED |
12140304c731SJamie Gritton 						      PD_LIST_XLOCKED
12150304c731SJamie Gritton 						    : PD_DEREF | PD_LOCKED |
12160304c731SJamie Gritton 						      PD_LIST_XLOCKED);
12170304c731SJamie Gritton 						if (root != NULL) {
12180304c731SJamie Gritton 							vfslocked =
12190304c731SJamie Gritton 							    VFS_LOCK_GIANT(
12200304c731SJamie Gritton 							    root->v_mount);
12210304c731SJamie Gritton 							vrele(root);
12220304c731SJamie Gritton 							VFS_UNLOCK_GIANT(
12230304c731SJamie Gritton 							    vfslocked);
12240304c731SJamie Gritton 						}
12250304c731SJamie Gritton 						ip4 = malloc(ip4a *
12260304c731SJamie Gritton 						    sizeof(*ip4), M_PRISON,
12270304c731SJamie Gritton 						    M_WAITOK);
12280304c731SJamie Gritton 						goto again;
12290304c731SJamie Gritton 					}
12300304c731SJamie Gritton 				}
12310304c731SJamie Gritton 			} else if (ip4s > 0) {
12320304c731SJamie Gritton 				/*
12330304c731SJamie Gritton 				 * Make sure the new set of IP addresses is a
12340304c731SJamie Gritton 				 * subset of the parent's list.  Don't worry
12350304c731SJamie Gritton 				 * about the parent being unlocked, as any
12360304c731SJamie Gritton 				 * setting is done with allprison_lock held.
12370304c731SJamie Gritton 				 */
12380304c731SJamie Gritton 				for (ij = 0; ij < ppr->pr_ip4s; ij++)
12390304c731SJamie Gritton 					if (ip4[0].s_addr ==
12400304c731SJamie Gritton 					    ppr->pr_ip4[ij].s_addr)
12410304c731SJamie Gritton 						break;
12420304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
12430304c731SJamie Gritton 					error = EPERM;
12440304c731SJamie Gritton 					goto done_deref_locked;
12450304c731SJamie Gritton 				}
12460304c731SJamie Gritton 				if (ip4s > 1) {
12470304c731SJamie Gritton 					for (ii = ij = 1; ii < ip4s; ii++) {
12480304c731SJamie Gritton 						if (ip4[ii].s_addr ==
12490304c731SJamie Gritton 						    ppr->pr_ip4[0].s_addr)
1250b38ff370SJamie Gritton 							continue;
12510304c731SJamie Gritton 						for (; ij < ppr->pr_ip4s; ij++)
12520304c731SJamie Gritton 						    if (ip4[ii].s_addr ==
12530304c731SJamie Gritton 							ppr->pr_ip4[ij].s_addr)
12540304c731SJamie Gritton 							    break;
12550304c731SJamie Gritton 						if (ij == ppr->pr_ip4s)
12560304c731SJamie Gritton 							break;
12570304c731SJamie Gritton 					}
12580304c731SJamie Gritton 					if (ij == ppr->pr_ip4s) {
12590304c731SJamie Gritton 						error = EPERM;
12600304c731SJamie Gritton 						goto done_deref_locked;
12610304c731SJamie Gritton 					}
12620304c731SJamie Gritton 				}
12630304c731SJamie Gritton 			}
12640304c731SJamie Gritton 		}
12650304c731SJamie Gritton 		if (ip4s > 0) {
12660304c731SJamie Gritton 			/*
12670304c731SJamie Gritton 			 * Check for conflicting IP addresses.  We permit them
12680304c731SJamie Gritton 			 * if there is no more than one IP on each jail.  If
12690304c731SJamie Gritton 			 * there is a duplicate on a jail with more than one
12700304c731SJamie Gritton 			 * IP stop checking and return error.
12710304c731SJamie Gritton 			 */
12720304c731SJamie Gritton 			FOREACH_PRISON_DESCENDANT(&prison0, tpr, descend) {
12730304c731SJamie Gritton 				if (tpr == pr || tpr->pr_uref == 0) {
12740304c731SJamie Gritton 					descend = 0;
12750304c731SJamie Gritton 					continue;
12760304c731SJamie Gritton 				}
12770304c731SJamie Gritton 				if (!(tpr->pr_flags & PR_IP4_USER))
12780304c731SJamie Gritton 					continue;
12790304c731SJamie Gritton 				descend = 0;
12800304c731SJamie Gritton 				if (tpr->pr_ip4 == NULL ||
12810304c731SJamie Gritton 				    (ip4s == 1 && tpr->pr_ip4s == 1))
12820304c731SJamie Gritton 					continue;
12830304c731SJamie Gritton 				for (ii = 0; ii < ip4s; ii++) {
1284b38ff370SJamie Gritton 					if (_prison_check_ip4(tpr,
1285b38ff370SJamie Gritton 					    &ip4[ii]) == 0) {
12860304c731SJamie Gritton 						error = EADDRINUSE;
1287b38ff370SJamie Gritton 						vfs_opterror(opts,
1288b38ff370SJamie Gritton 						    "IPv4 addresses clash");
1289b38ff370SJamie Gritton 						goto done_deref_locked;
1290b38ff370SJamie Gritton 					}
12910304c731SJamie Gritton 				}
12920304c731SJamie Gritton 			}
12930304c731SJamie Gritton 		}
12940304c731SJamie Gritton 	}
1295b38ff370SJamie Gritton #endif
1296b38ff370SJamie Gritton #ifdef INET6
12970304c731SJamie Gritton 	if (ch_flags & PR_IP6_USER) {
12980304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
12990304c731SJamie Gritton 			if (!(pr_flags & PR_IP6_USER)) {
13000304c731SJamie Gritton 				/*
13010304c731SJamie Gritton 				 * Silently ignore attempts to make the IP
13020304c731SJamie Gritton 				 * addresses unrestricted when the parent is
13030304c731SJamie Gritton 				 * restricted.
13040304c731SJamie Gritton 				 */
13050304c731SJamie Gritton 				ip6s = ppr->pr_ip6s;
13060304c731SJamie Gritton 				if (ip6s == 0) {
13070304c731SJamie Gritton 					free(ip6, M_PRISON);
13080304c731SJamie Gritton 					ip6 = NULL;
13090304c731SJamie Gritton 				} else if (ip6s <= ip6a) {
13100304c731SJamie Gritton 					/* Inherit the parent's address(es). */
13110304c731SJamie Gritton 					bcopy(ppr->pr_ip6, ip6,
13120304c731SJamie Gritton 					    ip6s * sizeof(*ip6));
13130304c731SJamie Gritton 				} else {
13140304c731SJamie Gritton 					/*
13150304c731SJamie Gritton 					 * There's no room for the parent's
13160304c731SJamie Gritton 					 * address list.
13170304c731SJamie Gritton 					 */
13180304c731SJamie Gritton 					ip6a = ip6s;
13190304c731SJamie Gritton 					free(ip6, M_PRISON);
13200304c731SJamie Gritton 					ip6 = malloc(ip6a * sizeof(*ip6),
13210304c731SJamie Gritton 					    M_PRISON, M_NOWAIT);
13220304c731SJamie Gritton 					if (ip6 != NULL)
13230304c731SJamie Gritton 						bcopy(ppr->pr_ip6, ip6,
13240304c731SJamie Gritton 						    ip6s * sizeof(*ip6));
13250304c731SJamie Gritton 					else {
13260304c731SJamie Gritton 						prison_deref(pr, created
13270304c731SJamie Gritton 						    ? PD_LOCKED |
13280304c731SJamie Gritton 						      PD_LIST_XLOCKED
13290304c731SJamie Gritton 						    : PD_DEREF | PD_LOCKED |
13300304c731SJamie Gritton 						      PD_LIST_XLOCKED);
13310304c731SJamie Gritton 						if (root != NULL) {
13320304c731SJamie Gritton 							vfslocked =
13330304c731SJamie Gritton 							    VFS_LOCK_GIANT(
13340304c731SJamie Gritton 							    root->v_mount);
13350304c731SJamie Gritton 							vrele(root);
13360304c731SJamie Gritton 							VFS_UNLOCK_GIANT(
13370304c731SJamie Gritton 							    vfslocked);
13380304c731SJamie Gritton 						}
13390304c731SJamie Gritton 						ip6 = malloc(ip6a *
13400304c731SJamie Gritton 						    sizeof(*ip6), M_PRISON,
13410304c731SJamie Gritton 						    M_WAITOK);
13420304c731SJamie Gritton 						goto again;
13430304c731SJamie Gritton 					}
13440304c731SJamie Gritton 				}
13450304c731SJamie Gritton 			} else if (ip6s > 0) {
13460304c731SJamie Gritton 				/*
13470304c731SJamie Gritton 				 * Make sure the new set of IP addresses is a
13480304c731SJamie Gritton 				 * subset of the parent's list.
13490304c731SJamie Gritton 				 */
13500304c731SJamie Gritton 				for (ij = 0; ij < ppr->pr_ip6s; ij++)
13510304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[0],
13520304c731SJamie Gritton 					    &ppr->pr_ip6[ij]))
13530304c731SJamie Gritton 						break;
13540304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
13550304c731SJamie Gritton 					error = EPERM;
13560304c731SJamie Gritton 					goto done_deref_locked;
13570304c731SJamie Gritton 				}
13580304c731SJamie Gritton 				if (ip6s > 1) {
13590304c731SJamie Gritton 					for (ii = ij = 1; ii < ip6s; ii++) {
13600304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
13610304c731SJamie Gritton 						    &ppr->pr_ip6[0]))
13620304c731SJamie Gritton 							continue;
13630304c731SJamie Gritton 						for (; ij < ppr->pr_ip6s; ij++)
13640304c731SJamie Gritton 							if (IN6_ARE_ADDR_EQUAL(
13650304c731SJamie Gritton 							    &ip6[ii],
13660304c731SJamie Gritton 							    &ppr->pr_ip6[ij]))
13670304c731SJamie Gritton 								break;
13680304c731SJamie Gritton 						if (ij == ppr->pr_ip6s)
13690304c731SJamie Gritton 							break;
13700304c731SJamie Gritton 					}
13710304c731SJamie Gritton 					if (ij == ppr->pr_ip6s) {
13720304c731SJamie Gritton 						error = EPERM;
13730304c731SJamie Gritton 						goto done_deref_locked;
13740304c731SJamie Gritton 					}
13750304c731SJamie Gritton 				}
13760304c731SJamie Gritton 			}
13770304c731SJamie Gritton 		}
13780304c731SJamie Gritton 		if (ip6s > 0) {
13790304c731SJamie Gritton 			/* Check for conflicting IP addresses. */
13800304c731SJamie Gritton 			FOREACH_PRISON_DESCENDANT(&prison0, tpr, descend) {
13810304c731SJamie Gritton 				if (tpr == pr || tpr->pr_uref == 0) {
13820304c731SJamie Gritton 					descend = 0;
13830304c731SJamie Gritton 					continue;
13840304c731SJamie Gritton 				}
13850304c731SJamie Gritton 				if (!(tpr->pr_flags & PR_IP6_USER))
13860304c731SJamie Gritton 					continue;
13870304c731SJamie Gritton 				descend = 0;
13880304c731SJamie Gritton 				if (tpr->pr_ip6 == NULL ||
13890304c731SJamie Gritton 				    (ip6s == 1 && tpr->pr_ip6s == 1))
13900304c731SJamie Gritton 					continue;
13910304c731SJamie Gritton 				for (ii = 0; ii < ip6s; ii++) {
1392b38ff370SJamie Gritton 					if (_prison_check_ip6(tpr,
1393b38ff370SJamie Gritton 					    &ip6[ii]) == 0) {
13940304c731SJamie Gritton 						error = EADDRINUSE;
1395b38ff370SJamie Gritton 						vfs_opterror(opts,
1396b38ff370SJamie Gritton 						    "IPv6 addresses clash");
1397b38ff370SJamie Gritton 						goto done_deref_locked;
1398b38ff370SJamie Gritton 					}
13990304c731SJamie Gritton 				}
14000304c731SJamie Gritton 			}
14010304c731SJamie Gritton 		}
1402b38ff370SJamie Gritton 	}
1403b38ff370SJamie Gritton #endif
14040304c731SJamie Gritton 	onamelen = namelen = 0;
14050304c731SJamie Gritton 	if (name != NULL) {
1406b38ff370SJamie Gritton 		/* Give a default name of the jid. */
1407b38ff370SJamie Gritton 		if (name[0] == '\0')
1408b38ff370SJamie Gritton 			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1409b38ff370SJamie Gritton 		else if (strtoul(name, &p, 10) != jid && *p == '\0') {
1410b38ff370SJamie Gritton 			error = EINVAL;
1411b38ff370SJamie Gritton 			vfs_opterror(opts, "name cannot be numeric");
14120304c731SJamie Gritton 			goto done_deref_locked;
1413b38ff370SJamie Gritton 		}
1414b38ff370SJamie Gritton 		/*
14150304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
14160304c731SJamie Gritton 		 * children.
1417b38ff370SJamie Gritton 		 */
14180304c731SJamie Gritton 		onamelen = strlen(pr->pr_name);
14190304c731SJamie Gritton 		namelen = strlen(name);
14200304c731SJamie Gritton 		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
14210304c731SJamie Gritton 			error = ENAMETOOLONG;
14220304c731SJamie Gritton 			goto done_deref_locked;
14230304c731SJamie Gritton 		}
14240304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
14250304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
14260304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
14270304c731SJamie Gritton 				error = ENAMETOOLONG;
14280304c731SJamie Gritton 				goto done_deref_locked;
14290304c731SJamie Gritton 			}
14300304c731SJamie Gritton 		}
14310304c731SJamie Gritton 	}
14320304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
14330304c731SJamie Gritton 		error = EPERM;
14340304c731SJamie Gritton 		goto done_deref_locked;
1435b38ff370SJamie Gritton 	}
1436b38ff370SJamie Gritton 
1437b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1438b38ff370SJamie Gritton #ifdef INET
14390304c731SJamie Gritton 	redo_ip4 = 0;
14400304c731SJamie Gritton 	if (ch_flags & PR_IP4_USER) {
14410304c731SJamie Gritton 		if (pr_flags & PR_IP4_USER) {
14420304c731SJamie Gritton 			/* Some restriction set. */
14430304c731SJamie Gritton 			pr->pr_flags |= PR_IP4;
1444b38ff370SJamie Gritton 			if (ip4s >= 0) {
1445b38ff370SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
14460304c731SJamie Gritton 				pr->pr_ip4s = ip4s;
1447b38ff370SJamie Gritton 				pr->pr_ip4 = ip4;
1448b38ff370SJamie Gritton 				ip4 = NULL;
1449b38ff370SJamie Gritton 			}
14500304c731SJamie Gritton 		} else if (ppr->pr_flags & PR_IP4) {
14510304c731SJamie Gritton 			/* This restriction cleared, but keep inherited. */
14520304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
14530304c731SJamie Gritton 			pr->pr_ip4s = ip4s;
14540304c731SJamie Gritton 			pr->pr_ip4 = ip4;
14550304c731SJamie Gritton 			ip4 = NULL;
14560304c731SJamie Gritton 		} else {
14570304c731SJamie Gritton 			/* Restriction cleared, now unrestricted. */
14580304c731SJamie Gritton 			pr->pr_flags &= ~PR_IP4;
14590304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
14600304c731SJamie Gritton 			pr->pr_ip4s = 0;
14610304c731SJamie Gritton 		}
14620304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
14630304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
14640304c731SJamie Gritton 				redo_ip4 = 1;
14650304c731SJamie Gritton 				descend = 0;
14660304c731SJamie Gritton 			}
14670304c731SJamie Gritton 		}
14680304c731SJamie Gritton 	}
1469b38ff370SJamie Gritton #endif
1470b38ff370SJamie Gritton #ifdef INET6
14710304c731SJamie Gritton 	redo_ip6 = 0;
14720304c731SJamie Gritton 	if (ch_flags & PR_IP6_USER) {
14730304c731SJamie Gritton 		if (pr_flags & PR_IP6_USER) {
14740304c731SJamie Gritton 			/* Some restriction set. */
14750304c731SJamie Gritton 			pr->pr_flags |= PR_IP6;
1476b38ff370SJamie Gritton 			if (ip6s >= 0) {
1477b38ff370SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
14780304c731SJamie Gritton 				pr->pr_ip6s = ip6s;
1479b38ff370SJamie Gritton 				pr->pr_ip6 = ip6;
1480b38ff370SJamie Gritton 				ip6 = NULL;
1481b38ff370SJamie Gritton 			}
14820304c731SJamie Gritton 		} else if (ppr->pr_flags & PR_IP6) {
14830304c731SJamie Gritton 			/* This restriction cleared, but keep inherited. */
14840304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
14850304c731SJamie Gritton 			pr->pr_ip6s = ip6s;
14860304c731SJamie Gritton 			pr->pr_ip6 = ip6;
14870304c731SJamie Gritton 			ip6 = NULL;
14880304c731SJamie Gritton 		} else {
14890304c731SJamie Gritton 			/* Restriction cleared, now unrestricted. */
14900304c731SJamie Gritton 			pr->pr_flags &= ~PR_IP6;
14910304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
14920304c731SJamie Gritton 			pr->pr_ip6s = 0;
14930304c731SJamie Gritton 		}
14940304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
14950304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
14960304c731SJamie Gritton 				redo_ip6 = 1;
14970304c731SJamie Gritton 				descend = 0;
14980304c731SJamie Gritton 			}
14990304c731SJamie Gritton 		}
15000304c731SJamie Gritton 	}
1501b38ff370SJamie Gritton #endif
15020304c731SJamie Gritton 	if (gotslevel) {
1503b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
15040304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
15050304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15060304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
15070304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
15080304c731SJamie Gritton 	}
15090304c731SJamie Gritton 	if (gotenforce) {
15100304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
15110304c731SJamie Gritton 		/* Pass this restriction on to the children. */
15120304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15130304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
15140304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
15150304c731SJamie Gritton 	}
15160304c731SJamie Gritton 	if (name != NULL) {
15170304c731SJamie Gritton 		if (ppr == &prison0)
1518b38ff370SJamie Gritton 			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
15190304c731SJamie Gritton 		else
15200304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
15210304c731SJamie Gritton 			    ppr->pr_name, name);
15220304c731SJamie Gritton 		/* Change this component of child names. */
15230304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
15240304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
15250304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
15260304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
15270304c731SJamie Gritton 		}
15280304c731SJamie Gritton 	}
1529b38ff370SJamie Gritton 	if (path != NULL) {
15300304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
15310304c731SJamie Gritton 		if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
15320304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
15330304c731SJamie Gritton 			    mypr->pr_path, path);
15340304c731SJamie Gritton 		else
1535b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1536b38ff370SJamie Gritton 		pr->pr_root = root;
1537b38ff370SJamie Gritton 	}
153876ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
153976ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
154076ca6f88SJamie Gritton 			/*
154176ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
154276ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
154376ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
154476ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
154576ca6f88SJamie Gritton 			 */
1546c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1547c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1548c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1549c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1550c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1551c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
155276ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
155376ca6f88SJamie Gritton 		}
155476ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
155576ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1556b38ff370SJamie Gritton 		if (host != NULL)
1557c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
155876ca6f88SJamie Gritton 		if (domain != NULL)
1559c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1560c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
156176ca6f88SJamie Gritton 		if (uuid != NULL)
1562c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
156376ca6f88SJamie Gritton 		if (gothid)
156476ca6f88SJamie Gritton 			pr->pr_hostid = hid;
156576ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
156676ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
156776ca6f88SJamie Gritton 				descend = 0;
156876ca6f88SJamie Gritton 			else {
156976ca6f88SJamie Gritton 				if (host != NULL)
1570c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1571c1f19219SJamie Gritton 					    pr->pr_hostname,
1572c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
157376ca6f88SJamie Gritton 				if (domain != NULL)
1574c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1575c1f19219SJamie Gritton 					    pr->pr_domainname,
1576c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
157776ca6f88SJamie Gritton 				if (uuid != NULL)
1578c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1579c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1580c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
158176ca6f88SJamie Gritton 				if (gothid)
158276ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
158376ca6f88SJamie Gritton 			}
158476ca6f88SJamie Gritton 		}
158576ca6f88SJamie Gritton 	}
15860304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
15870304c731SJamie Gritton 		/* Clear allow bits in all children. */
15880304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15890304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
15900304c731SJamie Gritton 	}
15910304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1592b38ff370SJamie Gritton 	/*
1593b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1594b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1595b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1596b38ff370SJamie Gritton 	 * handlers have completed.
1597b38ff370SJamie Gritton 	 */
1598b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1599b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1600b38ff370SJamie Gritton 			pr->pr_ref++;
1601b38ff370SJamie Gritton 			pr->pr_uref++;
1602b38ff370SJamie Gritton 		} else {
1603b38ff370SJamie Gritton 			pr->pr_ref--;
1604b38ff370SJamie Gritton 			pr->pr_uref--;
1605b38ff370SJamie Gritton 		}
1606b38ff370SJamie Gritton 	}
1607b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1608b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1609b38ff370SJamie Gritton 
16100304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
16110304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
16120304c731SJamie Gritton 	 */
16130304c731SJamie Gritton #ifdef INET
16140304c731SJamie Gritton 	while (redo_ip4) {
16150304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
16160304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
16170304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16180304c731SJamie Gritton 		redo_ip4 = 0;
16190304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16200304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
16210304c731SJamie Gritton 				if (ip4 != NULL)
16220304c731SJamie Gritton 					ip4 = NULL;
16230304c731SJamie Gritton 				else
16240304c731SJamie Gritton 					redo_ip4 = 1;
16250304c731SJamie Gritton 			}
16260304c731SJamie Gritton 		}
16270304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
16280304c731SJamie Gritton 	}
16290304c731SJamie Gritton #endif
16300304c731SJamie Gritton #ifdef INET6
16310304c731SJamie Gritton 	while (redo_ip6) {
16320304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
16330304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
16340304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16350304c731SJamie Gritton 		redo_ip6 = 0;
16360304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16370304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
16380304c731SJamie Gritton 				if (ip6 != NULL)
16390304c731SJamie Gritton 					ip6 = NULL;
16400304c731SJamie Gritton 				else
16410304c731SJamie Gritton 					redo_ip6 = 1;
16420304c731SJamie Gritton 			}
16430304c731SJamie Gritton 		}
16440304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
16450304c731SJamie Gritton 	}
16460304c731SJamie Gritton #endif
16470304c731SJamie Gritton 
1648b38ff370SJamie Gritton 	/* Let the modules do their work. */
1649b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1650b38ff370SJamie Gritton 	if (created) {
1651b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1652b38ff370SJamie Gritton 		if (error) {
1653b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_SLOCKED);
1654b38ff370SJamie Gritton 			goto done_errmsg;
1655b38ff370SJamie Gritton 		}
1656b38ff370SJamie Gritton 	}
1657b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1658b38ff370SJamie Gritton 	if (error) {
1659b38ff370SJamie Gritton 		prison_deref(pr, created
1660b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1661b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1662b38ff370SJamie Gritton 		goto done_errmsg;
1663b38ff370SJamie Gritton 	}
1664b38ff370SJamie Gritton 
1665b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1666b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1667b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1668b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1669b38ff370SJamie Gritton 		if (error) {
1670b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1671b38ff370SJamie Gritton 			if (!created)
1672b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1673b38ff370SJamie Gritton 			goto done_errmsg;
1674b38ff370SJamie Gritton 		}
1675b38ff370SJamie Gritton 	}
1676b38ff370SJamie Gritton 
1677b38ff370SJamie Gritton 	/*
1678b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1679b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1680b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1681b38ff370SJamie Gritton 	 * visible).
1682b38ff370SJamie Gritton 	 */
1683b38ff370SJamie Gritton 	if (!created) {
1684b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1685b38ff370SJamie Gritton 		    ? PD_DEREF
1686b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1687b38ff370SJamie Gritton 	} else {
1688b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1689b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1690b38ff370SJamie Gritton 			pr->pr_ref++;
1691b38ff370SJamie Gritton 			pr->pr_uref++;
1692b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1693b38ff370SJamie Gritton 		}
1694b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1695b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1696b38ff370SJamie Gritton 	}
1697b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
1698b38ff370SJamie Gritton 	goto done_errmsg;
1699b38ff370SJamie Gritton 
17000304c731SJamie Gritton  done_deref_locked:
17010304c731SJamie Gritton 	prison_deref(pr, created
17020304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
17030304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
17040304c731SJamie Gritton 	goto done_releroot;
1705b38ff370SJamie Gritton  done_unlock_list:
1706b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1707b38ff370SJamie Gritton  done_releroot:
1708b38ff370SJamie Gritton 	if (root != NULL) {
1709b38ff370SJamie Gritton 		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1710b38ff370SJamie Gritton 		vrele(root);
1711b38ff370SJamie Gritton 		VFS_UNLOCK_GIANT(vfslocked);
1712b38ff370SJamie Gritton 	}
1713b38ff370SJamie Gritton  done_errmsg:
1714b38ff370SJamie Gritton 	if (error) {
1715b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1716b38ff370SJamie Gritton 		if (errmsg_len > 0) {
1717b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1718b38ff370SJamie Gritton 			if (errmsg_pos > 0) {
1719b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE)
1720b38ff370SJamie Gritton 					bcopy(errmsg,
1721b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1722b38ff370SJamie Gritton 					   errmsg_len);
1723b38ff370SJamie Gritton 				else
1724b38ff370SJamie Gritton 					copyout(errmsg,
1725b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1726b38ff370SJamie Gritton 					   errmsg_len);
1727b38ff370SJamie Gritton 			}
1728b38ff370SJamie Gritton 		}
1729b38ff370SJamie Gritton 	}
1730b38ff370SJamie Gritton  done_free:
1731b38ff370SJamie Gritton #ifdef INET
1732b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1733b38ff370SJamie Gritton #endif
1734b38ff370SJamie Gritton #ifdef INET6
1735b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1736b38ff370SJamie Gritton #endif
1737b38ff370SJamie Gritton 	vfs_freeopts(opts);
1738b38ff370SJamie Gritton 	return (error);
1739b38ff370SJamie Gritton }
1740b38ff370SJamie Gritton 
1741b38ff370SJamie Gritton 
1742b38ff370SJamie Gritton /*
1743b38ff370SJamie Gritton  * struct jail_get_args {
1744b38ff370SJamie Gritton  *	struct iovec *iovp;
1745b38ff370SJamie Gritton  *	unsigned int iovcnt;
1746b38ff370SJamie Gritton  *	int flags;
1747b38ff370SJamie Gritton  * };
1748b38ff370SJamie Gritton  */
1749b38ff370SJamie Gritton int
1750b38ff370SJamie Gritton jail_get(struct thread *td, struct jail_get_args *uap)
1751b38ff370SJamie Gritton {
1752b38ff370SJamie Gritton 	struct uio *auio;
1753b38ff370SJamie Gritton 	int error;
1754b38ff370SJamie Gritton 
1755b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1756b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1757b38ff370SJamie Gritton 		return (EINVAL);
1758b38ff370SJamie Gritton 
1759b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1760b38ff370SJamie Gritton 	if (error)
1761b38ff370SJamie Gritton 		return (error);
1762b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1763b38ff370SJamie Gritton 	if (error == 0)
1764b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1765b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1766b38ff370SJamie Gritton 	free(auio, M_IOV);
1767b38ff370SJamie Gritton 	return (error);
1768b38ff370SJamie Gritton }
1769b38ff370SJamie Gritton 
1770b38ff370SJamie Gritton int
1771b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1772b38ff370SJamie Gritton {
17730304c731SJamie Gritton 	struct prison *pr, *mypr;
1774b38ff370SJamie Gritton 	struct vfsopt *opt;
1775b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1776b38ff370SJamie Gritton 	char *errmsg, *name;
17770304c731SJamie Gritton 	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1778b38ff370SJamie Gritton 
1779b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1780b38ff370SJamie Gritton 		return (EINVAL);
1781b38ff370SJamie Gritton 
1782b38ff370SJamie Gritton 	/* Get the parameter list. */
1783b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1784b38ff370SJamie Gritton 	if (error)
1785b38ff370SJamie Gritton 		return (error);
1786b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
17870304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
17881e2a13e6SJamie Gritton 
1789b38ff370SJamie Gritton 	/*
1790b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
1791b38ff370SJamie Gritton 	 */
1792b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
1793b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1794b38ff370SJamie Gritton 	if (error == 0) {
1795b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
17960304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1797b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
1798b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
1799b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1800b38ff370SJamie Gritton 					break;
1801b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1802b38ff370SJamie Gritton 			}
1803b38ff370SJamie Gritton 		}
1804b38ff370SJamie Gritton 		if (pr != NULL)
1805b38ff370SJamie Gritton 			goto found_prison;
1806b38ff370SJamie Gritton 		error = ENOENT;
1807b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
1808b38ff370SJamie Gritton 		goto done_unlock_list;
1809b38ff370SJamie Gritton 	} else if (error != ENOENT)
1810b38ff370SJamie Gritton 		goto done_unlock_list;
1811b38ff370SJamie Gritton 
1812b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1813b38ff370SJamie Gritton 	if (error == 0) {
1814b38ff370SJamie Gritton 		if (jid != 0) {
18150304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
1816b38ff370SJamie Gritton 			if (pr != NULL) {
1817b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1818b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1819b38ff370SJamie Gritton 					error = ENOENT;
1820b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1821b38ff370SJamie Gritton 					    jid);
1822b38ff370SJamie Gritton 					goto done_unlock_list;
1823b38ff370SJamie Gritton 				}
1824b38ff370SJamie Gritton 				goto found_prison;
1825b38ff370SJamie Gritton 			}
1826b38ff370SJamie Gritton 			error = ENOENT;
1827b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
1828b38ff370SJamie Gritton 			goto done_unlock_list;
1829b38ff370SJamie Gritton 		}
1830b38ff370SJamie Gritton 	} else if (error != ENOENT)
1831b38ff370SJamie Gritton 		goto done_unlock_list;
1832b38ff370SJamie Gritton 
1833b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1834b38ff370SJamie Gritton 	if (error == 0) {
1835b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1836b38ff370SJamie Gritton 			error = EINVAL;
1837b38ff370SJamie Gritton 			goto done_unlock_list;
1838b38ff370SJamie Gritton 		}
18390304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
1840b38ff370SJamie Gritton 		if (pr != NULL) {
1841b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1842b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1843b38ff370SJamie Gritton 				error = ENOENT;
1844b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
1845b38ff370SJamie Gritton 				    name);
1846b38ff370SJamie Gritton 				goto done_unlock_list;
1847b38ff370SJamie Gritton 			}
1848b38ff370SJamie Gritton 			goto found_prison;
1849b38ff370SJamie Gritton 		}
1850b38ff370SJamie Gritton 		error = ENOENT;
1851b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
1852b38ff370SJamie Gritton 		goto done_unlock_list;
1853b38ff370SJamie Gritton 	} else if (error != ENOENT)
1854b38ff370SJamie Gritton 		goto done_unlock_list;
1855b38ff370SJamie Gritton 
1856b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
1857b38ff370SJamie Gritton 	error = ENOENT;
1858b38ff370SJamie Gritton 	goto done_unlock_list;
1859b38ff370SJamie Gritton 
1860b38ff370SJamie Gritton  found_prison:
1861b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
1862b38ff370SJamie Gritton 	pr->pr_ref++;
1863b38ff370SJamie Gritton 	locked = PD_LOCKED;
1864b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
1865b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1866b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1867b38ff370SJamie Gritton 		goto done_deref;
18680304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
18690304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1870b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1871b38ff370SJamie Gritton 		goto done_deref;
18720304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
18730304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
18740304c731SJamie Gritton 		goto done_deref;
18750304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1876b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
1877b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1878b38ff370SJamie Gritton 		goto done_deref;
18790304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1880b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1881b38ff370SJamie Gritton 		goto done_deref;
1882b38ff370SJamie Gritton #ifdef INET
1883b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1884b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1885b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1886b38ff370SJamie Gritton 		goto done_deref;
1887b38ff370SJamie Gritton #endif
1888b38ff370SJamie Gritton #ifdef INET6
1889b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1890b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1891b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1892b38ff370SJamie Gritton 		goto done_deref;
1893b38ff370SJamie Gritton #endif
1894b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1895b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
1896b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1897b38ff370SJamie Gritton 		goto done_deref;
1898c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1899b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1900b38ff370SJamie Gritton 		goto done_deref;
1901c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
190276ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
190376ca6f88SJamie Gritton 		goto done_deref;
1904c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
190576ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
190676ca6f88SJamie Gritton 		goto done_deref;
190776ca6f88SJamie Gritton #ifdef COMPAT_IA32
190876ca6f88SJamie Gritton 	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
190976ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
191076ca6f88SJamie Gritton 
191176ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
191276ca6f88SJamie Gritton 	} else
191376ca6f88SJamie Gritton #endif
191476ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
191576ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
191676ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
191776ca6f88SJamie Gritton 		goto done_deref;
19180304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
19190304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
19200304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
19210304c731SJamie Gritton 		goto done_deref;
19220304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
19230304c731SJamie Gritton 	    fi++) {
19240304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
19250304c731SJamie Gritton 			continue;
19260304c731SJamie Gritton 		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
19270304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1928b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
1929b38ff370SJamie Gritton 			goto done_deref;
1930b38ff370SJamie Gritton 		i = !i;
19310304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1932b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
1933b38ff370SJamie Gritton 			goto done_deref;
19340304c731SJamie Gritton 	}
19350304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
19360304c731SJamie Gritton 	    fi++) {
19370304c731SJamie Gritton 		if (pr_allow_names[fi] == NULL)
19380304c731SJamie Gritton 			continue;
19390304c731SJamie Gritton 		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
19400304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
19410304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
19420304c731SJamie Gritton 			goto done_deref;
19430304c731SJamie Gritton 		i = !i;
19440304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
19450304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
19460304c731SJamie Gritton 			goto done_deref;
19470304c731SJamie Gritton 	}
1948b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
1949b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
1950b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1951b38ff370SJamie Gritton 		goto done_deref;
1952b38ff370SJamie Gritton 	i = !i;
1953b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1954b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1955b38ff370SJamie Gritton 		goto done_deref;
1956b38ff370SJamie Gritton 
1957b38ff370SJamie Gritton 	/* Get the module parameters. */
1958b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1959b38ff370SJamie Gritton 	locked = 0;
1960b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
1961b38ff370SJamie Gritton 	if (error)
1962b38ff370SJamie Gritton 		goto done_deref;
1963b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
1964b38ff370SJamie Gritton 
1965b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
1966b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1967b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1968b38ff370SJamie Gritton 			error = EINVAL;
1969b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
1970b38ff370SJamie Gritton 			goto done_errmsg;
1971b38ff370SJamie Gritton 		}
1972b38ff370SJamie Gritton 	}
1973b38ff370SJamie Gritton 
1974b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
1975b38ff370SJamie Gritton 	error = 0;
1976b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1977b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
1978b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
1979b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
1980b38ff370SJamie Gritton 			if (opt->value != NULL) {
1981b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
1982b38ff370SJamie Gritton 					bcopy(opt->value,
1983b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
1984b38ff370SJamie Gritton 					    opt->len);
1985b38ff370SJamie Gritton 				} else {
1986b38ff370SJamie Gritton 					error = copyout(opt->value,
1987b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
1988b38ff370SJamie Gritton 					    opt->len);
1989b38ff370SJamie Gritton 					if (error)
1990b38ff370SJamie Gritton 						break;
1991b38ff370SJamie Gritton 				}
1992b38ff370SJamie Gritton 			}
1993b38ff370SJamie Gritton 		}
1994b38ff370SJamie Gritton 	}
1995b38ff370SJamie Gritton 	goto done_errmsg;
1996b38ff370SJamie Gritton 
1997b38ff370SJamie Gritton  done_deref:
1998b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
1999b38ff370SJamie Gritton 	goto done_errmsg;
2000b38ff370SJamie Gritton 
2001b38ff370SJamie Gritton  done_unlock_list:
2002b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2003b38ff370SJamie Gritton  done_errmsg:
2004b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2005b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2006b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2007b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2008b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2009b38ff370SJamie Gritton 				bcopy(errmsg,
2010b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2011b38ff370SJamie Gritton 				    errmsg_len);
2012b38ff370SJamie Gritton 			else
2013b38ff370SJamie Gritton 				copyout(errmsg,
2014b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2015b38ff370SJamie Gritton 				    errmsg_len);
2016b38ff370SJamie Gritton 		}
2017b38ff370SJamie Gritton 	}
2018b38ff370SJamie Gritton 	vfs_freeopts(opts);
2019b38ff370SJamie Gritton 	return (error);
2020b38ff370SJamie Gritton }
2021b38ff370SJamie Gritton 
20220304c731SJamie Gritton 
2023b38ff370SJamie Gritton /*
2024b38ff370SJamie Gritton  * struct jail_remove_args {
2025b38ff370SJamie Gritton  *	int jid;
2026b38ff370SJamie Gritton  * };
2027b38ff370SJamie Gritton  */
2028b38ff370SJamie Gritton int
2029b38ff370SJamie Gritton jail_remove(struct thread *td, struct jail_remove_args *uap)
2030b38ff370SJamie Gritton {
20310304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
20320304c731SJamie Gritton 	int descend, error;
2033b38ff370SJamie Gritton 
2034b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2035b38ff370SJamie Gritton 	if (error)
2036b38ff370SJamie Gritton 		return (error);
2037b38ff370SJamie Gritton 
2038b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
20390304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2040b38ff370SJamie Gritton 	if (pr == NULL) {
2041b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2042b38ff370SJamie Gritton 		return (EINVAL);
2043b38ff370SJamie Gritton 	}
2044b38ff370SJamie Gritton 
20450304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
20460304c731SJamie Gritton 	pr->pr_ref++;
20470304c731SJamie Gritton 	pr->pr_flags |= PR_REMOVE;
20480304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
20490304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
20500304c731SJamie Gritton 		lpr = NULL;
20510304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
20520304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
20530304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
20540304c731SJamie Gritton 				tpr = cpr;
20550304c731SJamie Gritton 				cpr->pr_ref++;
20560304c731SJamie Gritton 				cpr->pr_flags |= PR_REMOVE;
20570304c731SJamie Gritton 			} else {
20580304c731SJamie Gritton 				/* Already removed - do not do it again. */
20590304c731SJamie Gritton 				tpr = NULL;
20600304c731SJamie Gritton 			}
20610304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
20620304c731SJamie Gritton 			if (lpr != NULL) {
20630304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
20640304c731SJamie Gritton 				prison_remove_one(lpr);
20650304c731SJamie Gritton 				sx_xlock(&allprison_lock);
20660304c731SJamie Gritton 			}
20670304c731SJamie Gritton 			lpr = tpr;
20680304c731SJamie Gritton 		}
20690304c731SJamie Gritton 		if (lpr != NULL) {
20700304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
20710304c731SJamie Gritton 			prison_remove_one(lpr);
20720304c731SJamie Gritton 			sx_xlock(&allprison_lock);
20730304c731SJamie Gritton 		}
20740304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
20750304c731SJamie Gritton 	}
20760304c731SJamie Gritton 	prison_remove_one(pr);
20770304c731SJamie Gritton 	return (0);
20780304c731SJamie Gritton }
20790304c731SJamie Gritton 
20800304c731SJamie Gritton static void
20810304c731SJamie Gritton prison_remove_one(struct prison *pr)
20820304c731SJamie Gritton {
20830304c731SJamie Gritton 	struct proc *p;
20840304c731SJamie Gritton 	int deuref;
20850304c731SJamie Gritton 
2086b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2087b38ff370SJamie Gritton 	deuref = 0;
2088b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2089b38ff370SJamie Gritton 		pr->pr_ref--;
2090b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2091b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2092b38ff370SJamie Gritton 	}
2093b38ff370SJamie Gritton 
20940304c731SJamie Gritton 	/*
20950304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
20960304c731SJamie Gritton 	 * the prison now.
20970304c731SJamie Gritton 	 */
20980304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
20990304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
21000304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2101b38ff370SJamie Gritton 		prison_deref(pr,
2102b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
21030304c731SJamie Gritton 		return;
2104b38ff370SJamie Gritton 	}
2105b38ff370SJamie Gritton 
2106b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2107b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2108b38ff370SJamie Gritton 	/*
2109b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2110b38ff370SJamie Gritton 	 */
2111b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
2112b38ff370SJamie Gritton 	LIST_FOREACH(p, &allproc, p_list) {
2113b38ff370SJamie Gritton 		PROC_LOCK(p);
2114b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2115b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
2116b38ff370SJamie Gritton 			psignal(p, SIGKILL);
2117b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2118b38ff370SJamie Gritton 	}
2119b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
21200304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2121b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
212275c13541SPoul-Henning Kamp }
212375c13541SPoul-Henning Kamp 
21248571af59SJamie Gritton 
2125fd7a8150SMike Barcroft /*
21269ddb7954SMike Barcroft  * struct jail_attach_args {
21279ddb7954SMike Barcroft  *	int jid;
21289ddb7954SMike Barcroft  * };
2129fd7a8150SMike Barcroft  */
2130fd7a8150SMike Barcroft int
21319ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap)
2132fd7a8150SMike Barcroft {
2133b38ff370SJamie Gritton 	struct prison *pr;
2134b38ff370SJamie Gritton 	int error;
2135b38ff370SJamie Gritton 
2136b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2137b38ff370SJamie Gritton 	if (error)
2138b38ff370SJamie Gritton 		return (error);
2139b38ff370SJamie Gritton 
2140b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
21410304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2142b38ff370SJamie Gritton 	if (pr == NULL) {
2143b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2144b38ff370SJamie Gritton 		return (EINVAL);
2145b38ff370SJamie Gritton 	}
2146b38ff370SJamie Gritton 
2147b38ff370SJamie Gritton 	/*
2148b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2149b38ff370SJamie Gritton 	 * considered to be "alive".
2150b38ff370SJamie Gritton 	 */
2151b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2152b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2153b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2154b38ff370SJamie Gritton 		return (EINVAL);
2155b38ff370SJamie Gritton 	}
2156b38ff370SJamie Gritton 
2157b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2158b38ff370SJamie Gritton }
2159b38ff370SJamie Gritton 
2160b38ff370SJamie Gritton static int
2161b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2162b38ff370SJamie Gritton {
21630304c731SJamie Gritton 	struct prison *ppr;
2164fd7a8150SMike Barcroft 	struct proc *p;
2165fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
2166453f7d53SChristian S.J. Peron 	int vfslocked, error;
2167fd7a8150SMike Barcroft 
216857f22bd4SJacques Vidrine 	/*
216957f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
217057f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
217157f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
217257f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
217357f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
217457f22bd4SJacques Vidrine 	 * of another.
217557f22bd4SJacques Vidrine 	 */
2176fd7a8150SMike Barcroft 	pr->pr_ref++;
2177b38ff370SJamie Gritton 	pr->pr_uref++;
2178fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2179b38ff370SJamie Gritton 
2180b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2181b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2182b38ff370SJamie Gritton 	if (error) {
2183b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2184b38ff370SJamie Gritton 		return (error);
2185b38ff370SJamie Gritton 	}
2186dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2187fd7a8150SMike Barcroft 
2188413628a7SBjoern A. Zeeb 	/*
2189413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2190413628a7SBjoern A. Zeeb 	 */
21910304c731SJamie Gritton 	ppr = td->td_ucred->cr_prison;
2192b38ff370SJamie Gritton 	p = td->td_proc;
2193413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2194413628a7SBjoern A. Zeeb 	if (error)
2195b38ff370SJamie Gritton 		goto e_revert_osd;
2196413628a7SBjoern A. Zeeb 
2197453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2198cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2199fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2200fd7a8150SMike Barcroft 		goto e_unlock;
2201fd7a8150SMike Barcroft #ifdef MAC
220230d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2203fd7a8150SMike Barcroft 		goto e_unlock;
2204fd7a8150SMike Barcroft #endif
220522db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2206b38ff370SJamie Gritton 	if ((error = change_root(pr->pr_root, td)))
2207b38ff370SJamie Gritton 		goto e_unlock_giant;
2208453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
2209fd7a8150SMike Barcroft 
2210fd7a8150SMike Barcroft 	newcred = crget();
2211fd7a8150SMike Barcroft 	PROC_LOCK(p);
2212fd7a8150SMike Barcroft 	oldcred = p->p_ucred;
2213fd7a8150SMike Barcroft 	setsugid(p);
2214fd7a8150SMike Barcroft 	crcopy(newcred, oldcred);
221569c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2216fd7a8150SMike Barcroft 	p->p_ucred = newcred;
2217fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
2218fd7a8150SMike Barcroft 	crfree(oldcred);
22190304c731SJamie Gritton 	prison_deref(ppr, PD_DEREF | PD_DEUREF);
2220fd7a8150SMike Barcroft 	return (0);
2221fd7a8150SMike Barcroft  e_unlock:
222222db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2223b38ff370SJamie Gritton  e_unlock_giant:
2224453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
2225b38ff370SJamie Gritton  e_revert_osd:
2226b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
22270304c731SJamie Gritton 	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2228b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2229fd7a8150SMike Barcroft 	return (error);
2230fd7a8150SMike Barcroft }
2231fd7a8150SMike Barcroft 
22320304c731SJamie Gritton 
2233fd7a8150SMike Barcroft /*
2234fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2235fd7a8150SMike Barcroft  */
223654b369c1SPawel Jakub Dawidek struct prison *
2237fd7a8150SMike Barcroft prison_find(int prid)
2238fd7a8150SMike Barcroft {
2239fd7a8150SMike Barcroft 	struct prison *pr;
2240fd7a8150SMike Barcroft 
2241dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2242b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2243fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2244fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2245b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2246fd7a8150SMike Barcroft 				return (pr);
2247b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2248fd7a8150SMike Barcroft 		}
2249fd7a8150SMike Barcroft 	}
2250fd7a8150SMike Barcroft 	return (NULL);
2251fd7a8150SMike Barcroft }
2252fd7a8150SMike Barcroft 
2253b38ff370SJamie Gritton /*
22540304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2255b38ff370SJamie Gritton  */
2256b38ff370SJamie Gritton struct prison *
22570304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2258b38ff370SJamie Gritton {
22590304c731SJamie Gritton 	struct prison *pr;
22600304c731SJamie Gritton 	int descend;
2261b38ff370SJamie Gritton 
2262b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
22630304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
22640304c731SJamie Gritton 		if (pr->pr_id == prid) {
22650304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
22660304c731SJamie Gritton 			if (pr->pr_ref > 0)
22670304c731SJamie Gritton 				return (pr);
22680304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
22690304c731SJamie Gritton 		}
22700304c731SJamie Gritton 	}
22710304c731SJamie Gritton 	return (NULL);
22720304c731SJamie Gritton }
22730304c731SJamie Gritton 
22740304c731SJamie Gritton /*
22750304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
22760304c731SJamie Gritton  */
22770304c731SJamie Gritton struct prison *
22780304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
22790304c731SJamie Gritton {
22800304c731SJamie Gritton 	struct prison *pr, *deadpr;
22810304c731SJamie Gritton 	size_t mylen;
22820304c731SJamie Gritton 	int descend;
22830304c731SJamie Gritton 
22840304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
22850304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2286b38ff370SJamie Gritton  again:
2287b38ff370SJamie Gritton 	deadpr = NULL;
22880304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
22890304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2290b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2291b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2292b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2293b38ff370SJamie Gritton 					return (pr);
2294b38ff370SJamie Gritton 				deadpr = pr;
2295b38ff370SJamie Gritton 			}
2296b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2297b38ff370SJamie Gritton 		}
2298b38ff370SJamie Gritton 	}
22990304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2300b38ff370SJamie Gritton 	if (deadpr != NULL) {
2301b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2302b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2303b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2304b38ff370SJamie Gritton 			goto again;
2305b38ff370SJamie Gritton 		}
2306b38ff370SJamie Gritton 	}
2307b38ff370SJamie Gritton 	return (deadpr);
2308b38ff370SJamie Gritton }
2309b38ff370SJamie Gritton 
2310b38ff370SJamie Gritton /*
23110304c731SJamie Gritton  * See if a prison has the specific flag set.
23120304c731SJamie Gritton  */
23130304c731SJamie Gritton int
23140304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
23150304c731SJamie Gritton {
23160304c731SJamie Gritton 
23170304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
23180304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
23190304c731SJamie Gritton }
23200304c731SJamie Gritton 
23210304c731SJamie Gritton int
23220304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
23230304c731SJamie Gritton {
23240304c731SJamie Gritton 
23250304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
23260304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
23270304c731SJamie Gritton }
23280304c731SJamie Gritton 
23290304c731SJamie Gritton /*
2330b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2331b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2332b38ff370SJamie Gritton  */
233391421ba2SRobert Watson void
23341ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
233591421ba2SRobert Watson {
233691421ba2SRobert Watson 
23371ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
233891421ba2SRobert Watson 	pr->pr_ref--;
233991421ba2SRobert Watson 	if (pr->pr_ref == 0) {
234001137630SRobert Watson 		mtx_unlock(&pr->pr_mtx);
2341c2cda609SPawel Jakub Dawidek 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2342c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2343c2cda609SPawel Jakub Dawidek 		return;
2344c2cda609SPawel Jakub Dawidek 	}
2345c2cda609SPawel Jakub Dawidek 	mtx_unlock(&pr->pr_mtx);
2346c2cda609SPawel Jakub Dawidek }
2347c2cda609SPawel Jakub Dawidek 
23481ba4a712SPawel Jakub Dawidek void
23491ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
23501ba4a712SPawel Jakub Dawidek {
23511ba4a712SPawel Jakub Dawidek 
23521ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
23531ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
23541ba4a712SPawel Jakub Dawidek }
23551ba4a712SPawel Jakub Dawidek 
2356c2cda609SPawel Jakub Dawidek static void
2357c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2358c2cda609SPawel Jakub Dawidek {
2359b38ff370SJamie Gritton 
2360b38ff370SJamie Gritton 	prison_deref((struct prison *)context, 0);
2361b38ff370SJamie Gritton }
2362b38ff370SJamie Gritton 
2363b38ff370SJamie Gritton /*
2364b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2365b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2366b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2367b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2368b38ff370SJamie Gritton  */
2369b38ff370SJamie Gritton static void
2370b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2371b38ff370SJamie Gritton {
23720304c731SJamie Gritton 	struct prison *ppr, *tpr;
2373c2cda609SPawel Jakub Dawidek 	int vfslocked;
2374c2cda609SPawel Jakub Dawidek 
2375b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2376b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
23770304c731SJamie Gritton 	/* Decrement the user references in a separate loop. */
2378b38ff370SJamie Gritton 	if (flags & PD_DEUREF) {
23790304c731SJamie Gritton 		for (tpr = pr;; tpr = tpr->pr_parent) {
23800304c731SJamie Gritton 			if (tpr != pr)
23810304c731SJamie Gritton 				mtx_lock(&tpr->pr_mtx);
23820304c731SJamie Gritton 			if (--tpr->pr_uref > 0)
23830304c731SJamie Gritton 				break;
23840304c731SJamie Gritton 			KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
23850304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
23860304c731SJamie Gritton 		}
2387b38ff370SJamie Gritton 		/* Done if there were only user references to remove. */
2388b38ff370SJamie Gritton 		if (!(flags & PD_DEREF)) {
23890304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
2390b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2391b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2392b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2393b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2394b38ff370SJamie Gritton 			return;
2395b38ff370SJamie Gritton 		}
23960304c731SJamie Gritton 		if (tpr != pr) {
23970304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
23980304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2399b38ff370SJamie Gritton 		}
24000304c731SJamie Gritton 	}
24010304c731SJamie Gritton 
24020304c731SJamie Gritton 	for (;;) {
2403b38ff370SJamie Gritton 		if (flags & PD_DEREF)
2404b38ff370SJamie Gritton 			pr->pr_ref--;
2405b38ff370SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2406b38ff370SJamie Gritton 		if (pr->pr_ref > 0) {
2407b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2408b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2409b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2410b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2411b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2412b38ff370SJamie Gritton 			return;
2413b38ff370SJamie Gritton 		}
2414c2cda609SPawel Jakub Dawidek 
2415b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2416b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2417b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2418b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2419c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2420b38ff370SJamie Gritton 			}
2421b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2422b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2423b38ff370SJamie Gritton 
2424b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
24250304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
24260304c731SJamie Gritton 		ppr = pr->pr_parent;
24270304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
24280304c731SJamie Gritton 			tpr->pr_prisoncount--;
24290304c731SJamie Gritton 		sx_downgrade(&allprison_lock);
24301ba4a712SPawel Jakub Dawidek 
2431679e1390SJamie Gritton #ifdef VIMAGE
2432679e1390SJamie Gritton 		if (pr->pr_flags & PR_VNET)
2433679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2434679e1390SJamie Gritton #endif
2435b38ff370SJamie Gritton 		if (pr->pr_root != NULL) {
2436453f7d53SChristian S.J. Peron 			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2437b3059e09SRobert Watson 			vrele(pr->pr_root);
2438453f7d53SChristian S.J. Peron 			VFS_UNLOCK_GIANT(vfslocked);
2439b38ff370SJamie Gritton 		}
2440b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2441413628a7SBjoern A. Zeeb #ifdef INET
2442413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2443413628a7SBjoern A. Zeeb #endif
2444b38ff370SJamie Gritton #ifdef INET6
2445b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2446b38ff370SJamie Gritton #endif
2447b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2448b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2449b38ff370SJamie Gritton 		osd_jail_exit(pr);
24501ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
24510304c731SJamie Gritton 
24520304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
24530304c731SJamie Gritton 		pr = ppr;
24540304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
24550304c731SJamie Gritton 		flags = PD_DEREF | PD_LIST_SLOCKED;
24560304c731SJamie Gritton 	}
2457b3059e09SRobert Watson }
2458b3059e09SRobert Watson 
245991421ba2SRobert Watson void
24601ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
24611ba4a712SPawel Jakub Dawidek {
24621ba4a712SPawel Jakub Dawidek 
24631ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
24641ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
2465af7bd9a4SJamie Gritton 	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
24661ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
24671ba4a712SPawel Jakub Dawidek }
24681ba4a712SPawel Jakub Dawidek 
24691ba4a712SPawel Jakub Dawidek void
247091421ba2SRobert Watson prison_hold(struct prison *pr)
247191421ba2SRobert Watson {
247291421ba2SRobert Watson 
247301137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
24741ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
247501137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
247601137630SRobert Watson }
247701137630SRobert Watson 
2478413628a7SBjoern A. Zeeb void
2479413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
248001137630SRobert Watson {
248101137630SRobert Watson 
2482413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2483b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2484b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2485b38ff370SJamie Gritton 	pr->pr_uref++;
2486413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
248775c13541SPoul-Henning Kamp }
248875c13541SPoul-Henning Kamp 
248975c13541SPoul-Henning Kamp void
2490413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
249175c13541SPoul-Henning Kamp {
2492413628a7SBjoern A. Zeeb 
2493413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2494b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2495b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2496b38ff370SJamie Gritton 	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2497413628a7SBjoern A. Zeeb }
2498413628a7SBjoern A. Zeeb 
2499413628a7SBjoern A. Zeeb 
2500413628a7SBjoern A. Zeeb #ifdef INET
2501413628a7SBjoern A. Zeeb /*
25020304c731SJamie Gritton  * Restrict a prison's IP address list with its parent's, possibly replacing
25030304c731SJamie Gritton  * it.  Return true if the replacement buffer was used (or would have been).
25040304c731SJamie Gritton  */
25050304c731SJamie Gritton static int
25060304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
25070304c731SJamie Gritton {
25080304c731SJamie Gritton 	int ii, ij, used;
25090304c731SJamie Gritton 	struct prison *ppr;
25100304c731SJamie Gritton 
25110304c731SJamie Gritton 	ppr = pr->pr_parent;
25120304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4_USER)) {
25130304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
25140304c731SJamie Gritton 		if (pr->pr_ip4s < ppr->pr_ip4s) {
25150304c731SJamie Gritton 			/*
25160304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
25170304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
25180304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
25190304c731SJamie Gritton 			 * allocate one.
25200304c731SJamie Gritton 			 */
25210304c731SJamie Gritton 			used = 1;
25220304c731SJamie Gritton 			if (newip4 == NULL) {
25230304c731SJamie Gritton 				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
25240304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
25250304c731SJamie Gritton 				if (newip4 != NULL)
25260304c731SJamie Gritton 					used = 0;
25270304c731SJamie Gritton 			}
25280304c731SJamie Gritton 			if (newip4 != NULL) {
25290304c731SJamie Gritton 				bcopy(ppr->pr_ip4, newip4,
25300304c731SJamie Gritton 				    ppr->pr_ip4s * sizeof(*newip4));
25310304c731SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
25320304c731SJamie Gritton 				pr->pr_ip4 = newip4;
25330304c731SJamie Gritton 				pr->pr_ip4s = ppr->pr_ip4s;
25340304c731SJamie Gritton 				pr->pr_flags |= PR_IP4;
25350304c731SJamie Gritton 			}
25360304c731SJamie Gritton 			return (used);
25370304c731SJamie Gritton 		}
25380304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
25390304c731SJamie Gritton 		if (pr->pr_ip4s > 0)
25400304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
25410304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*newip4));
25420304c731SJamie Gritton 		else if (pr->pr_ip4 != NULL) {
25430304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
25440304c731SJamie Gritton 			pr->pr_ip4 = NULL;
25450304c731SJamie Gritton 		}
25460304c731SJamie Gritton 		pr->pr_flags =
25470304c731SJamie Gritton 			(pr->pr_flags & ~PR_IP4) | (ppr->pr_flags & PR_IP4);
25480304c731SJamie Gritton 	} else if (pr->pr_ip4s > 0 && (ppr->pr_flags & PR_IP4)) {
25490304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
25500304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip4s; ij++)
25510304c731SJamie Gritton 			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
25520304c731SJamie Gritton 				break;
25530304c731SJamie Gritton 		if (ij < ppr->pr_ip4s)
25540304c731SJamie Gritton 			ii = 1;
25550304c731SJamie Gritton 		else {
25560304c731SJamie Gritton 			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
25570304c731SJamie Gritton 			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
25580304c731SJamie Gritton 			ii = 0;
25590304c731SJamie Gritton 		}
25600304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip4s; ) {
25610304c731SJamie Gritton 			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
25620304c731SJamie Gritton 				ii++;
25630304c731SJamie Gritton 				continue;
25640304c731SJamie Gritton 			}
25650304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
25660304c731SJamie Gritton 				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
25670304c731SJamie Gritton 			case -1:
25680304c731SJamie Gritton 				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
25690304c731SJamie Gritton 				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
25700304c731SJamie Gritton 				break;
25710304c731SJamie Gritton 			case 0:
25720304c731SJamie Gritton 				ii++;
25730304c731SJamie Gritton 				ij++;
25740304c731SJamie Gritton 				break;
25750304c731SJamie Gritton 			case 1:
25760304c731SJamie Gritton 				ij++;
25770304c731SJamie Gritton 				break;
25780304c731SJamie Gritton 			}
25790304c731SJamie Gritton 		}
25800304c731SJamie Gritton 		if (pr->pr_ip4s == 0) {
25810304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
25820304c731SJamie Gritton 			pr->pr_ip4 = NULL;
25830304c731SJamie Gritton 		}
25840304c731SJamie Gritton 	}
25850304c731SJamie Gritton 	return (0);
25860304c731SJamie Gritton }
25870304c731SJamie Gritton 
25880304c731SJamie Gritton /*
2589413628a7SBjoern A. Zeeb  * Pass back primary IPv4 address of this jail.
2590413628a7SBjoern A. Zeeb  *
25910304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
25920304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. INADDR_ANY).
2593413628a7SBjoern A. Zeeb  *
2594b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2595b89e82ddSJamie Gritton  * Address returned in NBO.
2596413628a7SBjoern A. Zeeb  */
2597413628a7SBjoern A. Zeeb int
25981cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2599413628a7SBjoern A. Zeeb {
2600b38ff370SJamie Gritton 	struct prison *pr;
2601413628a7SBjoern A. Zeeb 
2602413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2603413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
260475c13541SPoul-Henning Kamp 
2605b38ff370SJamie Gritton 	pr = cred->cr_prison;
26060304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
26070304c731SJamie Gritton 		return (0);
2608b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
26090304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
26100304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
26110304c731SJamie Gritton 		return (0);
26120304c731SJamie Gritton 	}
2613b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2614b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2615b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2616b38ff370SJamie Gritton 	}
2617413628a7SBjoern A. Zeeb 
2618b38ff370SJamie Gritton 	ia->s_addr = pr->pr_ip4[0].s_addr;
2619b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2620413628a7SBjoern A. Zeeb 	return (0);
262175c13541SPoul-Henning Kamp }
2622413628a7SBjoern A. Zeeb 
2623413628a7SBjoern A. Zeeb /*
26240304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv4 address restrictions.
26250304c731SJamie Gritton  */
26260304c731SJamie Gritton int
26270304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2)
26280304c731SJamie Gritton {
26290304c731SJamie Gritton 
26300304c731SJamie Gritton 	if (pr1 == pr2)
26310304c731SJamie Gritton 		return (1);
26320304c731SJamie Gritton 
26330304c731SJamie Gritton 	/*
26340304c731SJamie Gritton 	 * jail_set maintains an exclusive hold on allprison_lock while it
26350304c731SJamie Gritton 	 * changes the IP addresses, so only a shared hold is needed.  This is
26360304c731SJamie Gritton 	 * easier than locking the two prisons which would require finding the
26370304c731SJamie Gritton 	 * proper locking order and end up needing allprison_lock anyway.
26380304c731SJamie Gritton 	 */
26390304c731SJamie Gritton 	sx_slock(&allprison_lock);
26400304c731SJamie Gritton 	while (pr1 != &prison0 && !(pr1->pr_flags & PR_IP4_USER))
26410304c731SJamie Gritton 		pr1 = pr1->pr_parent;
26420304c731SJamie Gritton 	while (pr2 != &prison0 && !(pr2->pr_flags & PR_IP4_USER))
26430304c731SJamie Gritton 		pr2 = pr2->pr_parent;
26440304c731SJamie Gritton 	sx_sunlock(&allprison_lock);
26450304c731SJamie Gritton 	return (pr1 == pr2);
26460304c731SJamie Gritton }
26470304c731SJamie Gritton 
26480304c731SJamie Gritton /*
2649413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this
2650413628a7SBjoern A. Zeeb  * jail.
2651413628a7SBjoern A. Zeeb  *
26520304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
26530304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
26540304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2655413628a7SBjoern A. Zeeb  */
2656413628a7SBjoern A. Zeeb int
2657413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2658413628a7SBjoern A. Zeeb {
2659b38ff370SJamie Gritton 	struct prison *pr;
2660413628a7SBjoern A. Zeeb 	struct in_addr ia0;
2661b38ff370SJamie Gritton 	int error;
2662413628a7SBjoern A. Zeeb 
2663413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2664413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2665413628a7SBjoern A. Zeeb 
2666b38ff370SJamie Gritton 	pr = cred->cr_prison;
26670304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
26680304c731SJamie Gritton 		return (0);
2669b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
26700304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
26710304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
26720304c731SJamie Gritton 		return (0);
26730304c731SJamie Gritton 	}
2674b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2675b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2676b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2677b38ff370SJamie Gritton 	}
2678413628a7SBjoern A. Zeeb 
2679413628a7SBjoern A. Zeeb 	ia0.s_addr = ntohl(ia->s_addr);
2680413628a7SBjoern A. Zeeb 	if (ia0.s_addr == INADDR_LOOPBACK) {
2681b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2682b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2683413628a7SBjoern A. Zeeb 		return (0);
2684413628a7SBjoern A. Zeeb 	}
2685413628a7SBjoern A. Zeeb 
2686b89e82ddSJamie Gritton 	if (ia0.s_addr == INADDR_ANY) {
2687413628a7SBjoern A. Zeeb 		/*
2688413628a7SBjoern A. Zeeb 		 * In case there is only 1 IPv4 address, bind directly.
2689413628a7SBjoern A. Zeeb 		 */
2690b38ff370SJamie Gritton 		if (pr->pr_ip4s == 1)
2691b38ff370SJamie Gritton 			ia->s_addr = pr->pr_ip4[0].s_addr;
2692b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2693413628a7SBjoern A. Zeeb 		return (0);
2694413628a7SBjoern A. Zeeb 	}
2695413628a7SBjoern A. Zeeb 
2696b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2697b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2698b38ff370SJamie Gritton 	return (error);
2699413628a7SBjoern A. Zeeb }
2700413628a7SBjoern A. Zeeb 
2701413628a7SBjoern A. Zeeb /*
2702413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
2703413628a7SBjoern A. Zeeb  *
2704b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2705b89e82ddSJamie Gritton  * Address passed in in NBO and returned in NBO.
2706413628a7SBjoern A. Zeeb  */
2707413628a7SBjoern A. Zeeb int
2708413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2709413628a7SBjoern A. Zeeb {
2710b38ff370SJamie Gritton 	struct prison *pr;
2711413628a7SBjoern A. Zeeb 
2712413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2713413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2714413628a7SBjoern A. Zeeb 
2715b38ff370SJamie Gritton 	pr = cred->cr_prison;
27160304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
27170304c731SJamie Gritton 		return (0);
2718b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
27190304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
27200304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
27210304c731SJamie Gritton 		return (0);
27220304c731SJamie Gritton 	}
2723b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2724b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2725b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2726b38ff370SJamie Gritton 	}
2727b89e82ddSJamie Gritton 
2728413628a7SBjoern A. Zeeb 	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2729b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2730b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2731413628a7SBjoern A. Zeeb 		return (0);
2732413628a7SBjoern A. Zeeb 	}
2733413628a7SBjoern A. Zeeb 
2734413628a7SBjoern A. Zeeb 	/*
2735413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
2736413628a7SBjoern A. Zeeb 	 */
2737b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2738413628a7SBjoern A. Zeeb 	return (0);
2739413628a7SBjoern A. Zeeb }
2740413628a7SBjoern A. Zeeb 
2741413628a7SBjoern A. Zeeb /*
2742b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
2743413628a7SBjoern A. Zeeb  *
27440304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
27450304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
27460304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO.
2747413628a7SBjoern A. Zeeb  */
2748413628a7SBjoern A. Zeeb static int
2749413628a7SBjoern A. Zeeb _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2750413628a7SBjoern A. Zeeb {
2751413628a7SBjoern A. Zeeb 	int i, a, z, d;
2752413628a7SBjoern A. Zeeb 
2753413628a7SBjoern A. Zeeb 	/*
2754413628a7SBjoern A. Zeeb 	 * Check the primary IP.
2755413628a7SBjoern A. Zeeb 	 */
2756413628a7SBjoern A. Zeeb 	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2757b89e82ddSJamie Gritton 		return (0);
2758413628a7SBjoern A. Zeeb 
2759413628a7SBjoern A. Zeeb 	/*
2760413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
2761413628a7SBjoern A. Zeeb 	 */
2762413628a7SBjoern A. Zeeb 	a = 0;
2763413628a7SBjoern A. Zeeb 	z = pr->pr_ip4s - 2;
2764413628a7SBjoern A. Zeeb 	while (a <= z) {
2765413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
2766413628a7SBjoern A. Zeeb 		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2767413628a7SBjoern A. Zeeb 		if (d > 0)
2768413628a7SBjoern A. Zeeb 			z = i - 1;
2769413628a7SBjoern A. Zeeb 		else if (d < 0)
2770413628a7SBjoern A. Zeeb 			a = i + 1;
2771413628a7SBjoern A. Zeeb 		else
2772413628a7SBjoern A. Zeeb 			return (0);
277375c13541SPoul-Henning Kamp 	}
277475c13541SPoul-Henning Kamp 
2775b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
2776b89e82ddSJamie Gritton }
2777b89e82ddSJamie Gritton 
277875c13541SPoul-Henning Kamp int
2779413628a7SBjoern A. Zeeb prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2780413628a7SBjoern A. Zeeb {
2781b38ff370SJamie Gritton 	struct prison *pr;
2782b38ff370SJamie Gritton 	int error;
2783413628a7SBjoern A. Zeeb 
2784413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2785413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2786413628a7SBjoern A. Zeeb 
2787b38ff370SJamie Gritton 	pr = cred->cr_prison;
27880304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
27890304c731SJamie Gritton 		return (0);
2790b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
27910304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
27920304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
27930304c731SJamie Gritton 		return (0);
27940304c731SJamie Gritton 	}
2795b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2796b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2797b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2798b38ff370SJamie Gritton 	}
2799413628a7SBjoern A. Zeeb 
2800b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2801b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2802b38ff370SJamie Gritton 	return (error);
2803413628a7SBjoern A. Zeeb }
2804413628a7SBjoern A. Zeeb #endif
2805413628a7SBjoern A. Zeeb 
2806413628a7SBjoern A. Zeeb #ifdef INET6
28070304c731SJamie Gritton static int
28080304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
28090304c731SJamie Gritton {
28100304c731SJamie Gritton 	int ii, ij, used;
28110304c731SJamie Gritton 	struct prison *ppr;
28120304c731SJamie Gritton 
28130304c731SJamie Gritton 	ppr = pr->pr_parent;
28140304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6_USER)) {
28150304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
28160304c731SJamie Gritton 		if (pr->pr_ip6s < ppr->pr_ip6s) {
28170304c731SJamie Gritton 			/*
28180304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
28190304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
28200304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
28210304c731SJamie Gritton 			 * allocate one.
28220304c731SJamie Gritton 			 */
28230304c731SJamie Gritton 			used = 1;
28240304c731SJamie Gritton 			if (newip6 == NULL) {
28250304c731SJamie Gritton 				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
28260304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
28270304c731SJamie Gritton 				if (newip6 != NULL)
28280304c731SJamie Gritton 					used = 0;
28290304c731SJamie Gritton 			}
28300304c731SJamie Gritton 			if (newip6 != NULL) {
28310304c731SJamie Gritton 				bcopy(ppr->pr_ip6, newip6,
28320304c731SJamie Gritton 				    ppr->pr_ip6s * sizeof(*newip6));
28330304c731SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
28340304c731SJamie Gritton 				pr->pr_ip6 = newip6;
28350304c731SJamie Gritton 				pr->pr_ip6s = ppr->pr_ip6s;
28360304c731SJamie Gritton 				pr->pr_flags |= PR_IP6;
28370304c731SJamie Gritton 			}
28380304c731SJamie Gritton 			return (used);
28390304c731SJamie Gritton 		}
28400304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
28410304c731SJamie Gritton 		if (pr->pr_ip6s > 0)
28420304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
28430304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*newip6));
28440304c731SJamie Gritton 		else if (pr->pr_ip6 != NULL) {
28450304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
28460304c731SJamie Gritton 			pr->pr_ip6 = NULL;
28470304c731SJamie Gritton 		}
28480304c731SJamie Gritton 		pr->pr_flags =
28490304c731SJamie Gritton 			(pr->pr_flags & ~PR_IP6) | (ppr->pr_flags & PR_IP6);
28500304c731SJamie Gritton 	} else if (pr->pr_ip6s > 0 && (ppr->pr_flags & PR_IP6)) {
28510304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
28520304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip6s; ij++)
28530304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
28540304c731SJamie Gritton 			    &ppr->pr_ip6[ij]))
28550304c731SJamie Gritton 				break;
28560304c731SJamie Gritton 		if (ij < ppr->pr_ip6s)
28570304c731SJamie Gritton 			ii = 1;
28580304c731SJamie Gritton 		else {
28590304c731SJamie Gritton 			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
28600304c731SJamie Gritton 			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
28610304c731SJamie Gritton 			ii = 0;
28620304c731SJamie Gritton 		}
28630304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip6s; ) {
28640304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
28650304c731SJamie Gritton 			    &ppr->pr_ip6[0])) {
28660304c731SJamie Gritton 				ii++;
28670304c731SJamie Gritton 				continue;
28680304c731SJamie Gritton 			}
28690304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
28700304c731SJamie Gritton 				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
28710304c731SJamie Gritton 			case -1:
28720304c731SJamie Gritton 				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
28730304c731SJamie Gritton 				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
28740304c731SJamie Gritton 				break;
28750304c731SJamie Gritton 			case 0:
28760304c731SJamie Gritton 				ii++;
28770304c731SJamie Gritton 				ij++;
28780304c731SJamie Gritton 				break;
28790304c731SJamie Gritton 			case 1:
28800304c731SJamie Gritton 				ij++;
28810304c731SJamie Gritton 				break;
28820304c731SJamie Gritton 			}
28830304c731SJamie Gritton 		}
28840304c731SJamie Gritton 		if (pr->pr_ip6s == 0) {
28850304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
28860304c731SJamie Gritton 			pr->pr_ip6 = NULL;
28870304c731SJamie Gritton 		}
28880304c731SJamie Gritton 	}
28890304c731SJamie Gritton 	return 0;
28900304c731SJamie Gritton }
28910304c731SJamie Gritton 
2892413628a7SBjoern A. Zeeb /*
2893413628a7SBjoern A. Zeeb  * Pass back primary IPv6 address for this jail.
2894413628a7SBjoern A. Zeeb  *
28950304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
28960304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2897413628a7SBjoern A. Zeeb  *
2898b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2899413628a7SBjoern A. Zeeb  */
2900413628a7SBjoern A. Zeeb int
29011cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2902413628a7SBjoern A. Zeeb {
2903b38ff370SJamie Gritton 	struct prison *pr;
2904413628a7SBjoern A. Zeeb 
2905413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2906413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2907413628a7SBjoern A. Zeeb 
2908b38ff370SJamie Gritton 	pr = cred->cr_prison;
29090304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
29100304c731SJamie Gritton 		return (0);
2911b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29120304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
29130304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29140304c731SJamie Gritton 		return (0);
29150304c731SJamie Gritton 	}
2916b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
2917b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2918b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2919b38ff370SJamie Gritton 	}
2920b89e82ddSJamie Gritton 
2921b38ff370SJamie Gritton 	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2922b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2923413628a7SBjoern A. Zeeb 	return (0);
2924413628a7SBjoern A. Zeeb }
2925413628a7SBjoern A. Zeeb 
2926413628a7SBjoern A. Zeeb /*
29270304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv6 address restrictions.
29280304c731SJamie Gritton  */
29290304c731SJamie Gritton int
29300304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2)
29310304c731SJamie Gritton {
29320304c731SJamie Gritton 
29330304c731SJamie Gritton 	if (pr1 == pr2)
29340304c731SJamie Gritton 		return (1);
29350304c731SJamie Gritton 
29360304c731SJamie Gritton 	sx_slock(&allprison_lock);
29370304c731SJamie Gritton 	while (pr1 != &prison0 && !(pr1->pr_flags & PR_IP6_USER))
29380304c731SJamie Gritton 		pr1 = pr1->pr_parent;
29390304c731SJamie Gritton 	while (pr2 != &prison0 && !(pr2->pr_flags & PR_IP6_USER))
29400304c731SJamie Gritton 		pr2 = pr2->pr_parent;
29410304c731SJamie Gritton 	sx_sunlock(&allprison_lock);
29420304c731SJamie Gritton 	return (pr1 == pr2);
29430304c731SJamie Gritton }
29440304c731SJamie Gritton 
29450304c731SJamie Gritton /*
2946413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this jail.
2947413628a7SBjoern A. Zeeb  *
2948413628a7SBjoern A. Zeeb  * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2949413628a7SBjoern A. Zeeb  * when needed while binding.
2950413628a7SBjoern A. Zeeb  *
29510304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
29520304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
29530304c731SJamie Gritton  * doesn't allow IPv6.
2954413628a7SBjoern A. Zeeb  */
2955413628a7SBjoern A. Zeeb int
2956413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
2957413628a7SBjoern A. Zeeb {
2958b38ff370SJamie Gritton 	struct prison *pr;
2959b38ff370SJamie Gritton 	int error;
2960413628a7SBjoern A. Zeeb 
2961413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2962413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2963413628a7SBjoern A. Zeeb 
2964b38ff370SJamie Gritton 	pr = cred->cr_prison;
29650304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
29660304c731SJamie Gritton 		return (0);
2967b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29680304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
29690304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29700304c731SJamie Gritton 		return (0);
29710304c731SJamie Gritton 	}
2972b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
2973b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2974b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2975b38ff370SJamie Gritton 	}
2976b89e82ddSJamie Gritton 
2977413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
2978b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2979b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2980413628a7SBjoern A. Zeeb 		return (0);
2981413628a7SBjoern A. Zeeb 	}
2982413628a7SBjoern A. Zeeb 
2983b89e82ddSJamie Gritton 	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
2984413628a7SBjoern A. Zeeb 		/*
2985b89e82ddSJamie Gritton 		 * In case there is only 1 IPv6 address, and v6only is true,
2986b89e82ddSJamie Gritton 		 * then bind directly.
2987413628a7SBjoern A. Zeeb 		 */
2988b38ff370SJamie Gritton 		if (v6only != 0 && pr->pr_ip6s == 1)
2989b38ff370SJamie Gritton 			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2990b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2991413628a7SBjoern A. Zeeb 		return (0);
2992413628a7SBjoern A. Zeeb 	}
2993b89e82ddSJamie Gritton 
2994b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
2995b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2996b38ff370SJamie Gritton 	return (error);
2997413628a7SBjoern A. Zeeb }
2998413628a7SBjoern A. Zeeb 
2999413628a7SBjoern A. Zeeb /*
3000413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
3001413628a7SBjoern A. Zeeb  *
3002b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3003413628a7SBjoern A. Zeeb  */
3004413628a7SBjoern A. Zeeb int
3005413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3006413628a7SBjoern A. Zeeb {
3007b38ff370SJamie Gritton 	struct prison *pr;
3008413628a7SBjoern A. Zeeb 
3009413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3010413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3011413628a7SBjoern A. Zeeb 
3012b38ff370SJamie Gritton 	pr = cred->cr_prison;
30130304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
30140304c731SJamie Gritton 		return (0);
3015b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30160304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
30170304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30180304c731SJamie Gritton 		return (0);
30190304c731SJamie Gritton 	}
3020b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3021b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3022b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3023b38ff370SJamie Gritton 	}
3024b89e82ddSJamie Gritton 
3025413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3026b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3027b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3028413628a7SBjoern A. Zeeb 		return (0);
3029413628a7SBjoern A. Zeeb 	}
3030413628a7SBjoern A. Zeeb 
3031413628a7SBjoern A. Zeeb 	/*
3032413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
3033413628a7SBjoern A. Zeeb 	 */
3034b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3035413628a7SBjoern A. Zeeb 	return (0);
3036413628a7SBjoern A. Zeeb }
3037413628a7SBjoern A. Zeeb 
3038413628a7SBjoern A. Zeeb /*
3039b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
3040413628a7SBjoern A. Zeeb  *
30410304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
30420304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
30430304c731SJamie Gritton  * doesn't allow IPv6.
3044413628a7SBjoern A. Zeeb  */
3045413628a7SBjoern A. Zeeb static int
3046413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3047413628a7SBjoern A. Zeeb {
3048413628a7SBjoern A. Zeeb 	int i, a, z, d;
3049413628a7SBjoern A. Zeeb 
3050413628a7SBjoern A. Zeeb 	/*
3051413628a7SBjoern A. Zeeb 	 * Check the primary IP.
3052413628a7SBjoern A. Zeeb 	 */
3053413628a7SBjoern A. Zeeb 	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3054b89e82ddSJamie Gritton 		return (0);
3055413628a7SBjoern A. Zeeb 
3056413628a7SBjoern A. Zeeb 	/*
3057413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
3058413628a7SBjoern A. Zeeb 	 */
3059413628a7SBjoern A. Zeeb 	a = 0;
3060413628a7SBjoern A. Zeeb 	z = pr->pr_ip6s - 2;
3061413628a7SBjoern A. Zeeb 	while (a <= z) {
3062413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
3063413628a7SBjoern A. Zeeb 		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3064413628a7SBjoern A. Zeeb 		if (d > 0)
3065413628a7SBjoern A. Zeeb 			z = i - 1;
3066413628a7SBjoern A. Zeeb 		else if (d < 0)
3067413628a7SBjoern A. Zeeb 			a = i + 1;
3068413628a7SBjoern A. Zeeb 		else
3069413628a7SBjoern A. Zeeb 			return (0);
3070413628a7SBjoern A. Zeeb 	}
3071413628a7SBjoern A. Zeeb 
3072b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
3073b89e82ddSJamie Gritton }
3074b89e82ddSJamie Gritton 
3075413628a7SBjoern A. Zeeb int
3076413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3077413628a7SBjoern A. Zeeb {
3078b38ff370SJamie Gritton 	struct prison *pr;
3079b38ff370SJamie Gritton 	int error;
3080413628a7SBjoern A. Zeeb 
3081413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3082413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3083413628a7SBjoern A. Zeeb 
3084b38ff370SJamie Gritton 	pr = cred->cr_prison;
30850304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
30860304c731SJamie Gritton 		return (0);
3087b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30880304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
30890304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30900304c731SJamie Gritton 		return (0);
30910304c731SJamie Gritton 	}
3092b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3093b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3094b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3095b38ff370SJamie Gritton 	}
3096413628a7SBjoern A. Zeeb 
3097b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3098b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3099b38ff370SJamie Gritton 	return (error);
3100413628a7SBjoern A. Zeeb }
3101413628a7SBjoern A. Zeeb #endif
3102413628a7SBjoern A. Zeeb 
3103413628a7SBjoern A. Zeeb /*
3104ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3105ca04ba64SJamie Gritton  *
3106ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3107ca04ba64SJamie Gritton  * if not.
3108ca04ba64SJamie Gritton  */
3109ca04ba64SJamie Gritton int
3110ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3111ca04ba64SJamie Gritton {
31120304c731SJamie Gritton 	struct prison *pr;
3113ca04ba64SJamie Gritton 	int error;
3114ca04ba64SJamie Gritton 
3115ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3116ca04ba64SJamie Gritton 
31170304c731SJamie Gritton 	pr = cred->cr_prison;
3118ca04ba64SJamie Gritton 	error = 0;
3119ca04ba64SJamie Gritton 	switch (af)
3120ca04ba64SJamie Gritton 	{
3121ca04ba64SJamie Gritton #ifdef INET
3122ca04ba64SJamie Gritton 	case AF_INET:
31230304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
31240304c731SJamie Gritton 		{
31250304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
31260304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3127ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
31280304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
31290304c731SJamie Gritton 		}
3130ca04ba64SJamie Gritton 		break;
3131ca04ba64SJamie Gritton #endif
3132ca04ba64SJamie Gritton #ifdef INET6
3133ca04ba64SJamie Gritton 	case AF_INET6:
31340304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
31350304c731SJamie Gritton 		{
31360304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
31370304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3138ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
31390304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
31400304c731SJamie Gritton 		}
3141ca04ba64SJamie Gritton 		break;
3142ca04ba64SJamie Gritton #endif
3143ca04ba64SJamie Gritton 	case AF_LOCAL:
3144ca04ba64SJamie Gritton 	case AF_ROUTE:
3145ca04ba64SJamie Gritton 		break;
3146ca04ba64SJamie Gritton 	default:
31470304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3148ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3149ca04ba64SJamie Gritton 	}
3150ca04ba64SJamie Gritton 	return (error);
3151ca04ba64SJamie Gritton }
3152ca04ba64SJamie Gritton 
3153ca04ba64SJamie Gritton /*
3154413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3155413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3156413628a7SBjoern A. Zeeb  *
31570304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
31580304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
31590304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3160413628a7SBjoern A. Zeeb  */
3161413628a7SBjoern A. Zeeb int
316291421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
316375c13541SPoul-Henning Kamp {
3164413628a7SBjoern A. Zeeb #ifdef INET
31659ddb7954SMike Barcroft 	struct sockaddr_in *sai;
3166413628a7SBjoern A. Zeeb #endif
3167413628a7SBjoern A. Zeeb #ifdef INET6
3168413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
3169413628a7SBjoern A. Zeeb #endif
3170b89e82ddSJamie Gritton 	int error;
317175c13541SPoul-Henning Kamp 
3172413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3173413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3174413628a7SBjoern A. Zeeb 
3175b89e82ddSJamie Gritton 	error = 0;
3176413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3177413628a7SBjoern A. Zeeb 	{
3178413628a7SBjoern A. Zeeb #ifdef INET
3179413628a7SBjoern A. Zeeb 	case AF_INET:
31809ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
3181b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3182413628a7SBjoern A. Zeeb 		break;
3183413628a7SBjoern A. Zeeb #endif
3184413628a7SBjoern A. Zeeb #ifdef INET6
3185413628a7SBjoern A. Zeeb 	case AF_INET6:
3186413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
3187b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3188413628a7SBjoern A. Zeeb 		break;
3189413628a7SBjoern A. Zeeb #endif
3190413628a7SBjoern A. Zeeb 	default:
31910304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3192b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3193413628a7SBjoern A. Zeeb 	}
3194b89e82ddSJamie Gritton 	return (error);
319575c13541SPoul-Henning Kamp }
319691421ba2SRobert Watson 
319791421ba2SRobert Watson /*
319891421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
319991421ba2SRobert Watson  */
320091421ba2SRobert Watson int
32019ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
320291421ba2SRobert Watson {
320391421ba2SRobert Watson 
320429b02909SMarko Zec #ifdef VIMAGE
320529b02909SMarko Zec 	if (cred2->cr_vimage->v_procg != cred1->cr_vimage->v_procg)
320629b02909SMarko Zec 		return (ESRCH);
320729b02909SMarko Zec #endif
32080304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
32090304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
32100304c731SJamie Gritton }
321191421ba2SRobert Watson 
32120304c731SJamie Gritton /*
32130304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
32140304c731SJamie Gritton  */
32150304c731SJamie Gritton int
32160304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
32170304c731SJamie Gritton {
32180304c731SJamie Gritton 
32190304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
32200304c731SJamie Gritton 		if (pr1 == pr2)
32210304c731SJamie Gritton 			return (1);
322291421ba2SRobert Watson 	return (0);
322391421ba2SRobert Watson }
322491421ba2SRobert Watson 
322591421ba2SRobert Watson /*
322691421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
322791421ba2SRobert Watson  */
322891421ba2SRobert Watson int
32299ddb7954SMike Barcroft jailed(struct ucred *cred)
323091421ba2SRobert Watson {
323191421ba2SRobert Watson 
32320304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
323391421ba2SRobert Watson }
32349484d0c0SRobert Drehmel 
32359484d0c0SRobert Drehmel /*
32367455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
32379484d0c0SRobert Drehmel  */
3238ad1ff099SRobert Drehmel void
32399ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
32409484d0c0SRobert Drehmel {
324176ca6f88SJamie Gritton 	struct prison *pr;
32429484d0c0SRobert Drehmel 
32437455b100SJamie Gritton 	/*
32447455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
32457455b100SJamie Gritton 	 * system's hostname.
32467455b100SJamie Gritton 	 */
324776ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
324876ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3249c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
325076ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
32519484d0c0SRobert Drehmel }
3252fd7a8150SMike Barcroft 
32537455b100SJamie Gritton void
32547455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
32557455b100SJamie Gritton {
32567455b100SJamie Gritton 
32577455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3258c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
32597455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
32607455b100SJamie Gritton }
32617455b100SJamie Gritton 
32627455b100SJamie Gritton void
32637455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
32647455b100SJamie Gritton {
32657455b100SJamie Gritton 
32667455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3267c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
32687455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
32697455b100SJamie Gritton }
32707455b100SJamie Gritton 
32717455b100SJamie Gritton void
32727455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
32737455b100SJamie Gritton {
32747455b100SJamie Gritton 
32757455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
32767455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
32777455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
32787455b100SJamie Gritton }
32797455b100SJamie Gritton 
3280f08df373SRobert Watson /*
3281820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3282820a0de9SPawel Jakub Dawidek  * status of a mount point.
3283820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3284820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3285820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3286f08df373SRobert Watson  */
3287f08df373SRobert Watson int
3288820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3289f08df373SRobert Watson {
3290820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3291820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3292820a0de9SPawel Jakub Dawidek 	size_t len;
3293f08df373SRobert Watson 
3294820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
32950304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
32960304c731SJamie Gritton 		return (0);
3297820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3298820a0de9SPawel Jakub Dawidek 		return (0);
32990304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3300820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3301820a0de9SPawel Jakub Dawidek 	/*
3302820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3303820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3304820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3305820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3306820a0de9SPawel Jakub Dawidek 	 */
3307820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3308820a0de9SPawel Jakub Dawidek 		return (0);
3309820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3310820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3311820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3312820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3313820a0de9SPawel Jakub Dawidek 	/*
3314820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3315820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3316820a0de9SPawel Jakub Dawidek 	 */
3317820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3318820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3319f08df373SRobert Watson 	return (0);
3320f08df373SRobert Watson }
3321820a0de9SPawel Jakub Dawidek 
3322820a0de9SPawel Jakub Dawidek void
3323820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3324820a0de9SPawel Jakub Dawidek {
3325820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3326820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3327820a0de9SPawel Jakub Dawidek 	size_t len;
3328820a0de9SPawel Jakub Dawidek 
3329820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
33300304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
33310304c731SJamie Gritton 		return;
3332820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3333820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3334820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3335820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3336820a0de9SPawel Jakub Dawidek 		return;
3337820a0de9SPawel Jakub Dawidek 	}
3338820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3339820a0de9SPawel Jakub Dawidek 		/*
3340820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3341820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3342820a0de9SPawel Jakub Dawidek 		 */
3343820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3344820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3345820a0de9SPawel Jakub Dawidek 		return;
3346820a0de9SPawel Jakub Dawidek 	}
3347820a0de9SPawel Jakub Dawidek 	/*
3348820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3349820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3350820a0de9SPawel Jakub Dawidek 	 */
3351820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3352820a0de9SPawel Jakub Dawidek 		return;
3353820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3354820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3355820a0de9SPawel Jakub Dawidek 	/*
3356820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3357820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3358820a0de9SPawel Jakub Dawidek 	 */
3359820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3360820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3361820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3362820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3363820a0de9SPawel Jakub Dawidek 	} else {
3364820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3365820a0de9SPawel Jakub Dawidek 	}
3366f08df373SRobert Watson }
3367f08df373SRobert Watson 
3368800c9408SRobert Watson /*
3369800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3370800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3371800c9408SRobert Watson  */
3372800c9408SRobert Watson int
3373800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3374800c9408SRobert Watson {
3375800c9408SRobert Watson 
3376800c9408SRobert Watson 	if (!jailed(cred))
3377800c9408SRobert Watson 		return (0);
3378800c9408SRobert Watson 
3379800c9408SRobert Watson 	switch (priv) {
3380800c9408SRobert Watson 
3381800c9408SRobert Watson 		/*
3382800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3383800c9408SRobert Watson 		 */
3384800c9408SRobert Watson 	case PRIV_KTRACE:
3385800c9408SRobert Watson 
3386c3c1b5e6SRobert Watson #if 0
3387800c9408SRobert Watson 		/*
3388800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3389800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3390800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3391800c9408SRobert Watson 		 * jail.
3392800c9408SRobert Watson 		 */
3393800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3394800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3395800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3396c3c1b5e6SRobert Watson #endif
3397800c9408SRobert Watson 
3398800c9408SRobert Watson 		/*
3399800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3400800c9408SRobert Watson 		 * credentials in any way they see fit.
3401800c9408SRobert Watson 		 */
3402800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3403800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3404800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3405800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3406800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3407800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3408800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3409800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3410800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3411800c9408SRobert Watson 
3412800c9408SRobert Watson 		/*
3413800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3414800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3415800c9408SRobert Watson 		 */
3416800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3417800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3418800c9408SRobert Watson 
3419800c9408SRobert Watson 		/*
3420800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3421800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3422800c9408SRobert Watson 		 */
3423800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3424800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3425800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3426800c9408SRobert Watson 
3427800c9408SRobert Watson 		/*
3428800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3429800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3430800c9408SRobert Watson 		 */
3431800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3432800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3433800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3434800c9408SRobert Watson 
3435800c9408SRobert Watson 		/*
3436800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3437800c9408SRobert Watson 		 */
3438800c9408SRobert Watson 	case PRIV_IPC_READ:
3439800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3440800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3441800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3442800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3443800c9408SRobert Watson 
3444800c9408SRobert Watson 		/*
34450304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
34460304c731SJamie Gritton 		 */
34470304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
34480304c731SJamie Gritton 	case PRIV_JAIL_SET:
34490304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
34500304c731SJamie Gritton 
34510304c731SJamie Gritton 		/*
3452800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3453800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3454800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3455800c9408SRobert Watson 		 */
3456800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3457413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3458800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3459800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3460800c9408SRobert Watson 
3461800c9408SRobert Watson 		/*
3462800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3463800c9408SRobert Watson 		 * writable.
3464800c9408SRobert Watson 		 */
3465800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3466800c9408SRobert Watson 
3467800c9408SRobert Watson 		/*
3468800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3469e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3470e82d0201SRobert Watson 		 * configuration option.
3471800c9408SRobert Watson 		 */
347295b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
347395b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3474800c9408SRobert Watson 
3475800c9408SRobert Watson 		/*
3476800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3477800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3478800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3479800c9408SRobert Watson 		 * privileges.
3480800c9408SRobert Watson 		 */
3481800c9408SRobert Watson 	case PRIV_VFS_READ:
3482800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3483800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3484800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3485800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3486800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3487800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3488800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3489800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3490bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3491800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3492800c9408SRobert Watson 	case PRIV_VFS_LINK:
3493800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3494e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3495800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3496800c9408SRobert Watson 		return (0);
3497800c9408SRobert Watson 
3498800c9408SRobert Watson 		/*
3499800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3500800c9408SRobert Watson 		 * setting system flags.
3501800c9408SRobert Watson 		 */
3502800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
35030304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3504800c9408SRobert Watson 			return (0);
3505800c9408SRobert Watson 		else
3506800c9408SRobert Watson 			return (EPERM);
3507800c9408SRobert Watson 
3508800c9408SRobert Watson 		/*
3509f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3510f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3511f3a8d2f9SPawel Jakub Dawidek 		 */
3512f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3513f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3514f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
351524b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
35160304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3517f3a8d2f9SPawel Jakub Dawidek 			return (0);
3518f3a8d2f9SPawel Jakub Dawidek 		else
3519f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
3520f3a8d2f9SPawel Jakub Dawidek 
3521f3a8d2f9SPawel Jakub Dawidek 		/*
35224b084056SRobert Watson 		 * Allow jailed root to bind reserved ports and reuse in-use
35234b084056SRobert Watson 		 * ports.
3524800c9408SRobert Watson 		 */
3525800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
35264b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3527800c9408SRobert Watson 		return (0);
3528800c9408SRobert Watson 
3529800c9408SRobert Watson 		/*
353079ba3952SBjoern A. Zeeb 		 * Allow jailed root to set certian IPv4/6 (option) headers.
353179ba3952SBjoern A. Zeeb 		 */
353279ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
353379ba3952SBjoern A. Zeeb 		return (0);
353479ba3952SBjoern A. Zeeb 
353579ba3952SBjoern A. Zeeb 		/*
3536800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3537800c9408SRobert Watson 		 */
3538800c9408SRobert Watson 	case PRIV_NETINET_RAW:
35390304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3540800c9408SRobert Watson 			return (0);
3541800c9408SRobert Watson 		else
3542800c9408SRobert Watson 			return (EPERM);
3543800c9408SRobert Watson 
3544800c9408SRobert Watson 		/*
3545800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3546800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3547800c9408SRobert Watson 		 * jail.
3548800c9408SRobert Watson 		 */
3549800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3550800c9408SRobert Watson 		return (0);
3551800c9408SRobert Watson 
3552800c9408SRobert Watson 	default:
3553800c9408SRobert Watson 		/*
3554800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3555800c9408SRobert Watson 		 * includes almost all network privileges, many system
3556800c9408SRobert Watson 		 * configuration privileges.
3557800c9408SRobert Watson 		 */
3558800c9408SRobert Watson 		return (EPERM);
3559800c9408SRobert Watson 	}
3560800c9408SRobert Watson }
3561800c9408SRobert Watson 
35620304c731SJamie Gritton /*
35630304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
35640304c731SJamie Gritton  * if it does not directly follow.
35650304c731SJamie Gritton  */
35660304c731SJamie Gritton 
35670304c731SJamie Gritton char *
35680304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
35690304c731SJamie Gritton {
35700304c731SJamie Gritton 	char *name;
35710304c731SJamie Gritton 
35720304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
35730304c731SJamie Gritton 	if (pr1 == pr2)
35740304c731SJamie Gritton 		return "0";
35750304c731SJamie Gritton 	name = pr2->pr_name;
35760304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
35770304c731SJamie Gritton 		/*
35780304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
35790304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
35800304c731SJamie Gritton 		 * can be counted on - and counted.
35810304c731SJamie Gritton 		 */
35820304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
35830304c731SJamie Gritton 			name = strchr(name, '.') + 1;
35840304c731SJamie Gritton 	}
35850304c731SJamie Gritton 	return (name);
35860304c731SJamie Gritton }
35870304c731SJamie Gritton 
35880304c731SJamie Gritton /*
35890304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
35900304c731SJamie Gritton  * if it does not directly follow.
35910304c731SJamie Gritton  */
35920304c731SJamie Gritton static char *
35930304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
35940304c731SJamie Gritton {
35950304c731SJamie Gritton 	char *path1, *path2;
35960304c731SJamie Gritton 	int len1;
35970304c731SJamie Gritton 
35980304c731SJamie Gritton 	path1 = pr1->pr_path;
35990304c731SJamie Gritton 	path2 = pr2->pr_path;
36000304c731SJamie Gritton 	if (!strcmp(path1, "/"))
36010304c731SJamie Gritton 		return (path2);
36020304c731SJamie Gritton 	len1 = strlen(path1);
36030304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
36040304c731SJamie Gritton 		return (path2);
36050304c731SJamie Gritton 	if (path2[len1] == '\0')
36060304c731SJamie Gritton 		return "/";
36070304c731SJamie Gritton 	if (path2[len1] == '/')
36080304c731SJamie Gritton 		return (path2 + len1);
36090304c731SJamie Gritton 	return (path2);
36100304c731SJamie Gritton }
36110304c731SJamie Gritton 
36120304c731SJamie Gritton 
36130304c731SJamie Gritton /*
36140304c731SJamie Gritton  * Jail-related sysctls.
36150304c731SJamie Gritton  */
36160304c731SJamie Gritton SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
36170304c731SJamie Gritton     "Jails");
36180304c731SJamie Gritton 
3619fd7a8150SMike Barcroft static int
3620fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3621fd7a8150SMike Barcroft {
3622b38ff370SJamie Gritton 	struct xprison *xp;
36230304c731SJamie Gritton 	struct prison *pr, *cpr;
3624b38ff370SJamie Gritton #ifdef INET
3625b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3626b38ff370SJamie Gritton 	int ip4s = 0;
3627b38ff370SJamie Gritton #endif
3628b38ff370SJamie Gritton #ifdef INET6
3629b38ff370SJamie Gritton 	struct in_addr *ip6 = NULL;
3630b38ff370SJamie Gritton 	int ip6s = 0;
3631b38ff370SJamie Gritton #endif
36320304c731SJamie Gritton 	int descend, error;
3633fd7a8150SMike Barcroft 
3634b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
36350304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3636b38ff370SJamie Gritton 	error = 0;
3637dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
36380304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
36390304c731SJamie Gritton #if defined(INET) || defined(INET6)
3640b38ff370SJamie Gritton  again:
36410304c731SJamie Gritton #endif
36420304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3643413628a7SBjoern A. Zeeb #ifdef INET
36440304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
36450304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
36460304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
36470304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3648b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3649b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3650b38ff370SJamie Gritton 				goto again;
3651b38ff370SJamie Gritton 			}
36520304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
36530304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3654b38ff370SJamie Gritton 		}
3655413628a7SBjoern A. Zeeb #endif
3656413628a7SBjoern A. Zeeb #ifdef INET6
36570304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
36580304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
36590304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
36600304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3661b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3662b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3663b38ff370SJamie Gritton 				goto again;
3664413628a7SBjoern A. Zeeb 			}
36650304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
36660304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3667b38ff370SJamie Gritton 		}
3668b38ff370SJamie Gritton #endif
36690304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
36700304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3671b38ff370SJamie Gritton 			continue;
3672b38ff370SJamie Gritton 		}
3673b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
3674fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
36750304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
36760304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
3677b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
36780304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3679c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
36800304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3681413628a7SBjoern A. Zeeb #ifdef INET
36820304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
3683413628a7SBjoern A. Zeeb #endif
3684413628a7SBjoern A. Zeeb #ifdef INET6
36850304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
3686413628a7SBjoern A. Zeeb #endif
36870304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3688b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3689b38ff370SJamie Gritton 		if (error)
3690b38ff370SJamie Gritton 			break;
3691413628a7SBjoern A. Zeeb #ifdef INET
3692b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
3693b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
3694b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
3695b38ff370SJamie Gritton 			if (error)
3696b38ff370SJamie Gritton 				break;
3697413628a7SBjoern A. Zeeb 		}
3698413628a7SBjoern A. Zeeb #endif
3699413628a7SBjoern A. Zeeb #ifdef INET6
3700b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
3701b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
3702b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
3703b38ff370SJamie Gritton 			if (error)
3704b38ff370SJamie Gritton 				break;
3705413628a7SBjoern A. Zeeb 		}
3706413628a7SBjoern A. Zeeb #endif
3707fd7a8150SMike Barcroft 	}
3708dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
3709b38ff370SJamie Gritton 	free(xp, M_TEMP);
3710b38ff370SJamie Gritton #ifdef INET
3711b38ff370SJamie Gritton 	free(ip4, M_TEMP);
3712b38ff370SJamie Gritton #endif
3713b38ff370SJamie Gritton #ifdef INET6
3714b38ff370SJamie Gritton 	free(ip6, M_TEMP);
3715b38ff370SJamie Gritton #endif
3716fd7a8150SMike Barcroft 	return (error);
3717fd7a8150SMike Barcroft }
3718fd7a8150SMike Barcroft 
3719f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
3720f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3721f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
3722461167c2SPawel Jakub Dawidek 
3723461167c2SPawel Jakub Dawidek static int
3724461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3725461167c2SPawel Jakub Dawidek {
3726461167c2SPawel Jakub Dawidek 	int error, injail;
3727461167c2SPawel Jakub Dawidek 
3728461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
3729461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3730461167c2SPawel Jakub Dawidek 
3731461167c2SPawel Jakub Dawidek 	return (error);
3732461167c2SPawel Jakub Dawidek }
37330304c731SJamie Gritton 
3734f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3735f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3736f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
3737413628a7SBjoern A. Zeeb 
37380304c731SJamie Gritton #if defined(INET) || defined(INET6)
3739e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
37400304c731SJamie Gritton     &jail_max_af_ips, 0,
37410304c731SJamie Gritton     "Number of IP addresses a jail may have at most per address family");
37420304c731SJamie Gritton #endif
37430304c731SJamie Gritton 
37440304c731SJamie Gritton /*
37450304c731SJamie Gritton  * Default parameters for jail(2) compatability.  For historical reasons,
37460304c731SJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
37470304c731SJamie Gritton  * just see their own parameters, and can't change them.
37480304c731SJamie Gritton  */
37490304c731SJamie Gritton static int
37500304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
37510304c731SJamie Gritton {
37520304c731SJamie Gritton 	struct prison *pr;
37530304c731SJamie Gritton 	int allow, error, i;
37540304c731SJamie Gritton 
37550304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
37560304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
37570304c731SJamie Gritton 
37580304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
37590304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
37600304c731SJamie Gritton 	if (arg1 != NULL)
37610304c731SJamie Gritton 		i = !i;
37620304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
37630304c731SJamie Gritton 	if (error || !req->newptr)
37640304c731SJamie Gritton 		return (error);
37650304c731SJamie Gritton 	i = i ? arg2 : 0;
37660304c731SJamie Gritton 	if (arg1 != NULL)
37670304c731SJamie Gritton 		i ^= arg2;
37680304c731SJamie Gritton 	/*
37690304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
37700304c731SJamie Gritton 	 * for writing.
37710304c731SJamie Gritton 	 */
37720304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
37730304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
37740304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
37750304c731SJamie Gritton 	return (0);
37760304c731SJamie Gritton }
37770304c731SJamie Gritton 
37780304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
37790304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
37800304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
37810304c731SJamie Gritton     "Processes in jail can set their hostnames");
37820304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
37830304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
37840304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
37850304c731SJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only");
37860304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
37870304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
37880304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
37890304c731SJamie Gritton     "Processes in jail can use System V IPC primitives");
37900304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
37910304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
37920304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
37930304c731SJamie Gritton     "Prison root can create raw sockets");
37940304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
37950304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
37960304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
37970304c731SJamie Gritton     "Processes in jail can alter system file flags");
37980304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
37990304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38000304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
38010304c731SJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems");
38020304c731SJamie Gritton 
38030304c731SJamie Gritton static int
38040304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
38050304c731SJamie Gritton {
38060304c731SJamie Gritton 	struct prison *pr;
38070304c731SJamie Gritton 	int level, error;
38080304c731SJamie Gritton 
38090304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
38100304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
38110304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
38120304c731SJamie Gritton 	if (error || !req->newptr)
38130304c731SJamie Gritton 		return (error);
38140304c731SJamie Gritton 	*(int *)arg1 = level;
38150304c731SJamie Gritton 	return (0);
38160304c731SJamie Gritton }
38170304c731SJamie Gritton 
38180304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
38190304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
38200304c731SJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
38210304c731SJamie Gritton     sysctl_jail_default_level, "I",
38220304c731SJamie Gritton     "Processes in jail cannot see all mounted file systems");
38230304c731SJamie Gritton 
38240304c731SJamie Gritton /*
38250304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
38260304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
38270304c731SJamie Gritton  * to make themselves and their types known.
38280304c731SJamie Gritton  */
38290304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
38300304c731SJamie Gritton     "Jail parameters");
38310304c731SJamie Gritton 
38320304c731SJamie Gritton int
38330304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
38340304c731SJamie Gritton {
38350304c731SJamie Gritton 	int i;
38360304c731SJamie Gritton 	long l;
38370304c731SJamie Gritton 	size_t s;
38380304c731SJamie Gritton 	char numbuf[12];
38390304c731SJamie Gritton 
38400304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
38410304c731SJamie Gritton 	{
38420304c731SJamie Gritton 	case CTLTYPE_LONG:
38430304c731SJamie Gritton 	case CTLTYPE_ULONG:
38440304c731SJamie Gritton 		l = 0;
38450304c731SJamie Gritton #ifdef SCTL_MASK32
38460304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
38470304c731SJamie Gritton #endif
38480304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
38490304c731SJamie Gritton 	case CTLTYPE_INT:
38500304c731SJamie Gritton 	case CTLTYPE_UINT:
38510304c731SJamie Gritton 		i = 0;
38520304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
38530304c731SJamie Gritton 	case CTLTYPE_STRING:
38540304c731SJamie Gritton 		snprintf(numbuf, sizeof(numbuf), "%d", arg2);
38550304c731SJamie Gritton 		return
38560304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
38570304c731SJamie Gritton 	case CTLTYPE_STRUCT:
38580304c731SJamie Gritton 		s = (size_t)arg2;
38590304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
38600304c731SJamie Gritton 	}
38610304c731SJamie Gritton 	return (0);
38620304c731SJamie Gritton }
38630304c731SJamie Gritton 
38640304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
38650304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
38660304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
38670304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
38680304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
38690304c731SJamie Gritton     "I", "Jail secure level");
38700304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
38710304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
38720304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
38730304c731SJamie Gritton     "B", "Jail persistence");
3874679e1390SJamie Gritton #ifdef VIMAGE
3875679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
3876679e1390SJamie Gritton     "B", "Virtual network stack");
3877679e1390SJamie Gritton #endif
38780304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
38790304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
38800304c731SJamie Gritton 
38810304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(host, "Jail host info");
388276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(, nohost, CTLTYPE_INT | CTLFLAG_RW,
388376ca6f88SJamie Gritton     "BN", "Jail w/ no host info");
38840304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
38850304c731SJamie Gritton     "Jail hostname");
388676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
388776ca6f88SJamie Gritton     "Jail NIS domainname");
388876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
388976ca6f88SJamie Gritton     "Jail host UUID");
389076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
389176ca6f88SJamie Gritton     "LU", "Jail host ID");
38920304c731SJamie Gritton 
38930304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
38940304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
38950304c731SJamie Gritton 
38960304c731SJamie Gritton #ifdef INET
38970304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(ip4, "Jail IPv4 address virtualization");
38980304c731SJamie Gritton SYSCTL_JAIL_PARAM(, noip4, CTLTYPE_INT | CTLFLAG_RW,
38990304c731SJamie Gritton     "BN", "Jail w/ no IP address virtualization");
39000304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
39010304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
39020304c731SJamie Gritton #endif
39030304c731SJamie Gritton #ifdef INET6
39040304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(ip6, "Jail IPv6 address virtualization");
39050304c731SJamie Gritton SYSCTL_JAIL_PARAM(, noip6, CTLTYPE_INT | CTLFLAG_RW,
39060304c731SJamie Gritton     "BN", "Jail w/ no IP address virtualization");
39070304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
39080304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
39090304c731SJamie Gritton #endif
39100304c731SJamie Gritton 
39110304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
39120304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
39130304c731SJamie Gritton     "B", "Jail may set hostname");
39140304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
39150304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
39160304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
39170304c731SJamie Gritton     "B", "Jail may create raw sockets");
39180304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
39190304c731SJamie Gritton     "B", "Jail may alter system file flags");
39200304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
39210304c731SJamie Gritton     "B", "Jail may mount/unmount jail-friendly file systems");
39220304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
39230304c731SJamie Gritton     "B", "Jail may set file quotas");
39240304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, jails, CTLTYPE_INT | CTLFLAG_RW,
39250304c731SJamie Gritton     "B", "Jail may create child jails");
39260304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
39270304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
39280304c731SJamie Gritton 
39290304c731SJamie Gritton 
3930413628a7SBjoern A. Zeeb #ifdef DDB
3931b38ff370SJamie Gritton 
3932b38ff370SJamie Gritton static void
3933b38ff370SJamie Gritton db_show_prison(struct prison *pr)
3934413628a7SBjoern A. Zeeb {
39350304c731SJamie Gritton 	int fi;
3936b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
3937b38ff370SJamie Gritton 	int ii;
3938413628a7SBjoern A. Zeeb #endif
3939413628a7SBjoern A. Zeeb #ifdef INET6
3940413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
3941413628a7SBjoern A. Zeeb #endif
3942413628a7SBjoern A. Zeeb 
3943b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
3944b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
3945b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
39460304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
3947b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
3948b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
3949b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
3950b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
3951b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
3952679e1390SJamie Gritton #ifdef VIMAGE
3953679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
3954679e1390SJamie Gritton #endif
3955b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
3956b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
39570304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
39580304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
3959b38ff370SJamie Gritton 	db_printf(" flags           = %x", pr->pr_flags);
39600304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
39610304c731SJamie Gritton 	    fi++)
39620304c731SJamie Gritton 		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
39630304c731SJamie Gritton 			db_printf(" %s", pr_flag_names[fi]);
39640304c731SJamie Gritton 	db_printf(" allow           = %x", pr->pr_allow);
39650304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
39660304c731SJamie Gritton 	    fi++)
39670304c731SJamie Gritton 		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
39680304c731SJamie Gritton 			db_printf(" %s", pr_allow_names[fi]);
3969b38ff370SJamie Gritton 	db_printf("\n");
39700304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
3971c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
3972c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
3973c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
397476ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
3975413628a7SBjoern A. Zeeb #ifdef INET
3976b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
3977b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
3978b38ff370SJamie Gritton 		db_printf(" %s %s\n",
3979b38ff370SJamie Gritton 		    ii == 0 ? "ip4             =" : "                 ",
3980b38ff370SJamie Gritton 		    inet_ntoa(pr->pr_ip4[ii]));
3981413628a7SBjoern A. Zeeb #endif
3982413628a7SBjoern A. Zeeb #ifdef INET6
3983b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
3984b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
3985b38ff370SJamie Gritton 		db_printf(" %s %s\n",
3986b38ff370SJamie Gritton 		    ii == 0 ? "ip6             =" : "                 ",
3987b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
3988b38ff370SJamie Gritton #endif
3989b38ff370SJamie Gritton }
3990b38ff370SJamie Gritton 
3991b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
3992b38ff370SJamie Gritton {
3993b38ff370SJamie Gritton 	struct prison *pr;
3994b38ff370SJamie Gritton 
3995b38ff370SJamie Gritton 	if (!have_addr) {
39960304c731SJamie Gritton 		/*
39970304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
39980304c731SJamie Gritton 		 * listed.
39990304c731SJamie Gritton 		 */
40000304c731SJamie Gritton 		db_show_prison(&prison0);
40010304c731SJamie Gritton 		if (!db_pager_quit) {
4002b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4003b38ff370SJamie Gritton 				db_show_prison(pr);
4004413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4005413628a7SBjoern A. Zeeb 					break;
4006413628a7SBjoern A. Zeeb 			}
40070304c731SJamie Gritton 		}
4008b38ff370SJamie Gritton 		return;
4009413628a7SBjoern A. Zeeb 	}
4010b38ff370SJamie Gritton 
40110304c731SJamie Gritton 	if (addr == 0)
40120304c731SJamie Gritton 		pr = &prison0;
40130304c731SJamie Gritton 	else {
4014b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4015b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4016b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4017b38ff370SJamie Gritton 				break;
4018b38ff370SJamie Gritton 		if (pr == NULL)
4019b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4020b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4021b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4022b38ff370SJamie Gritton 					break;
4023b38ff370SJamie Gritton 		if (pr == NULL)
4024b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4025b38ff370SJamie Gritton 			pr = (struct prison *)addr;
40260304c731SJamie Gritton 	}
4027b38ff370SJamie Gritton 	db_show_prison(pr);
4028b38ff370SJamie Gritton }
4029b38ff370SJamie Gritton 
4030413628a7SBjoern A. Zeeb #endif /* DDB */
4031