xref: /freebsd/sys/kern/kern_jail.c (revision 2bfc50bc4fbc3ad7e8bdfeb6b55d72ea55c4f18b)
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>
61530c0060SRobert Watson 
6275c13541SPoul-Henning Kamp #include <net/if.h>
63530c0060SRobert Watson #include <net/vnet.h>
64530c0060SRobert Watson 
6575c13541SPoul-Henning Kamp #include <netinet/in.h>
66530c0060SRobert Watson 
67413628a7SBjoern A. Zeeb #ifdef DDB
68413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
69413628a7SBjoern A. Zeeb #ifdef INET6
70413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h>
71413628a7SBjoern A. Zeeb #endif /* INET6 */
72413628a7SBjoern A. Zeeb #endif /* DDB */
7375c13541SPoul-Henning Kamp 
74aed55708SRobert Watson #include <security/mac/mac_framework.h>
75aed55708SRobert Watson 
768986e3a0SJamie Gritton #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
778986e3a0SJamie Gritton 
7875c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
7975c13541SPoul-Henning Kamp 
80592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
81592bcae8SBjoern A. Zeeb #ifdef INET
82592bcae8SBjoern A. Zeeb #ifdef INET6
83592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
84592bcae8SBjoern A. Zeeb #else
85592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
86592bcae8SBjoern A. Zeeb #endif
87592bcae8SBjoern A. Zeeb #else /* !INET */
88592bcae8SBjoern A. Zeeb #ifdef INET6
89592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
90592bcae8SBjoern A. Zeeb #else
91592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
92592bcae8SBjoern A. Zeeb #endif
93592bcae8SBjoern A. Zeeb #endif
94592bcae8SBjoern A. Zeeb 
950304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
960304c731SJamie Gritton struct prison prison0 = {
970304c731SJamie Gritton 	.pr_id		= 0,
980304c731SJamie Gritton 	.pr_name	= "0",
990304c731SJamie Gritton 	.pr_ref		= 1,
1000304c731SJamie Gritton 	.pr_uref	= 1,
1010304c731SJamie Gritton 	.pr_path	= "/",
1020304c731SJamie Gritton 	.pr_securelevel	= -1,
103b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1048986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
10513e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
106eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
107592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
108eb79e1c7SBjoern A. Zeeb #else
109592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
110eb79e1c7SBjoern A. Zeeb #endif
1110304c731SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL,
1120304c731SJamie Gritton };
1130304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11483f1e257SRobert Watson 
1150304c731SJamie Gritton /* allprison and lastprid are protected by allprison_lock. */
116dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
117b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
118b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
1192110d913SXin LI int	lastprid = 0;
120fd7a8150SMike Barcroft 
121b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
122b3059e09SRobert Watson static void prison_complete(void *context, int pending);
123b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1240304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1250304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
126413628a7SBjoern A. Zeeb #ifdef INET
1278571af59SJamie Gritton static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
1280304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
129413628a7SBjoern A. Zeeb #endif
130413628a7SBjoern A. Zeeb #ifdef INET6
1318571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
1320304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
133413628a7SBjoern A. Zeeb #endif
134fd7a8150SMike Barcroft 
135b38ff370SJamie Gritton /* Flags for prison_deref */
136b38ff370SJamie Gritton #define	PD_DEREF	0x01
137b38ff370SJamie Gritton #define	PD_DEUREF	0x02
138b38ff370SJamie Gritton #define	PD_LOCKED	0x04
139b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
140b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
141fd7a8150SMike Barcroft 
1420304c731SJamie Gritton /*
1435cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1445cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1455cc70397SBjoern A. Zeeb  * terminating entry.
1460304c731SJamie Gritton  */
1470304c731SJamie Gritton static char *pr_flag_names[] = {
1480304c731SJamie Gritton 	[0] = "persist",
149592bcae8SBjoern A. Zeeb #ifdef INET
150592bcae8SBjoern A. Zeeb 	[7] = "ip4.saddrsel",
151592bcae8SBjoern A. Zeeb #endif
152592bcae8SBjoern A. Zeeb #ifdef INET6
153592bcae8SBjoern A. Zeeb 	[8] = "ip6.saddrsel",
154592bcae8SBjoern A. Zeeb #endif
1550304c731SJamie Gritton };
1565cc70397SBjoern A. Zeeb const size_t pr_flag_names_size = sizeof(pr_flag_names);
1570304c731SJamie Gritton 
1580304c731SJamie Gritton static char *pr_flag_nonames[] = {
1590304c731SJamie Gritton 	[0] = "nopersist",
160592bcae8SBjoern A. Zeeb #ifdef INET
161592bcae8SBjoern A. Zeeb 	[7] = "ip4.nosaddrsel",
162592bcae8SBjoern A. Zeeb #endif
163592bcae8SBjoern A. Zeeb #ifdef INET6
164592bcae8SBjoern A. Zeeb 	[8] = "ip6.nosaddrsel",
165592bcae8SBjoern A. Zeeb #endif
1667cbf7213SJamie Gritton };
1675cc70397SBjoern A. Zeeb const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
1687cbf7213SJamie Gritton 
1697cbf7213SJamie Gritton struct jailsys_flags {
1707cbf7213SJamie Gritton 	const char	*name;
1717cbf7213SJamie Gritton 	unsigned	 disable;
1727cbf7213SJamie Gritton 	unsigned	 new;
1737cbf7213SJamie Gritton } pr_flag_jailsys[] = {
1747cbf7213SJamie Gritton 	{ "host", 0, PR_HOST },
1757cbf7213SJamie Gritton #ifdef VIMAGE
1767cbf7213SJamie Gritton 	{ "vnet", 0, PR_VNET },
1777cbf7213SJamie Gritton #endif
1780304c731SJamie Gritton #ifdef INET
1797cbf7213SJamie Gritton 	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
1800304c731SJamie Gritton #endif
1810304c731SJamie Gritton #ifdef INET6
1827cbf7213SJamie Gritton 	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
183679e1390SJamie Gritton #endif
1840304c731SJamie Gritton };
1855cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1860304c731SJamie Gritton 
1870304c731SJamie Gritton static char *pr_allow_names[] = {
1880304c731SJamie Gritton 	"allow.set_hostname",
1890304c731SJamie Gritton 	"allow.sysvipc",
1900304c731SJamie Gritton 	"allow.raw_sockets",
1910304c731SJamie Gritton 	"allow.chflags",
1920304c731SJamie Gritton 	"allow.mount",
1930304c731SJamie Gritton 	"allow.quotas",
1940304c731SJamie Gritton 	"allow.socket_af",
1950304c731SJamie Gritton };
1965cc70397SBjoern A. Zeeb const size_t pr_allow_names_size = sizeof(pr_allow_names);
1970304c731SJamie Gritton 
1980304c731SJamie Gritton static char *pr_allow_nonames[] = {
1990304c731SJamie Gritton 	"allow.noset_hostname",
2000304c731SJamie Gritton 	"allow.nosysvipc",
2010304c731SJamie Gritton 	"allow.noraw_sockets",
2020304c731SJamie Gritton 	"allow.nochflags",
2030304c731SJamie Gritton 	"allow.nomount",
2040304c731SJamie Gritton 	"allow.noquotas",
2050304c731SJamie Gritton 	"allow.nosocket_af",
2060304c731SJamie Gritton };
2075cc70397SBjoern A. Zeeb const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
2080304c731SJamie Gritton 
2090304c731SJamie Gritton #define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
21042bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
2110304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
21242bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2130304c731SJamie Gritton #if defined(INET) || defined(INET6)
214e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2150304c731SJamie Gritton #endif
2160304c731SJamie Gritton 
217413628a7SBjoern A. Zeeb #ifdef INET
218413628a7SBjoern A. Zeeb static int
219413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2)
220413628a7SBjoern A. Zeeb {
221413628a7SBjoern A. Zeeb 	in_addr_t iaa, iab;
222413628a7SBjoern A. Zeeb 
223413628a7SBjoern A. Zeeb 	/*
224413628a7SBjoern A. Zeeb 	 * We need to compare in HBO here to get the list sorted as expected
225413628a7SBjoern A. Zeeb 	 * by the result of the code.  Sorting NBO addresses gives you
226413628a7SBjoern A. Zeeb 	 * interesting results.  If you do not understand, do not try.
227413628a7SBjoern A. Zeeb 	 */
228413628a7SBjoern A. Zeeb 	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
229413628a7SBjoern A. Zeeb 	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
230413628a7SBjoern A. Zeeb 
231413628a7SBjoern A. Zeeb 	/*
232413628a7SBjoern A. Zeeb 	 * Do not simply return the difference of the two numbers, the int is
233413628a7SBjoern A. Zeeb 	 * not wide enough.
234413628a7SBjoern A. Zeeb 	 */
235413628a7SBjoern A. Zeeb 	if (iaa > iab)
236413628a7SBjoern A. Zeeb 		return (1);
237413628a7SBjoern A. Zeeb 	else if (iaa < iab)
238413628a7SBjoern A. Zeeb 		return (-1);
239413628a7SBjoern A. Zeeb 	else
240413628a7SBjoern A. Zeeb 		return (0);
241413628a7SBjoern A. Zeeb }
242413628a7SBjoern A. Zeeb #endif
243413628a7SBjoern A. Zeeb 
244413628a7SBjoern A. Zeeb #ifdef INET6
245413628a7SBjoern A. Zeeb static int
246413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2)
247413628a7SBjoern A. Zeeb {
248413628a7SBjoern A. Zeeb 	const struct in6_addr *ia6a, *ia6b;
249413628a7SBjoern A. Zeeb 	int i, rc;
250413628a7SBjoern A. Zeeb 
251413628a7SBjoern A. Zeeb 	ia6a = (const struct in6_addr *)ip1;
252413628a7SBjoern A. Zeeb 	ia6b = (const struct in6_addr *)ip2;
253413628a7SBjoern A. Zeeb 
254413628a7SBjoern A. Zeeb 	rc = 0;
255413628a7SBjoern A. Zeeb 	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
256413628a7SBjoern A. Zeeb 		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
257413628a7SBjoern A. Zeeb 			rc = 1;
258413628a7SBjoern A. Zeeb 		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
259413628a7SBjoern A. Zeeb 			rc = -1;
260413628a7SBjoern A. Zeeb 	}
261413628a7SBjoern A. Zeeb 	return (rc);
262413628a7SBjoern A. Zeeb }
263413628a7SBjoern A. Zeeb #endif
264413628a7SBjoern A. Zeeb 
265116734c4SMatthew Dillon /*
2669ddb7954SMike Barcroft  * struct jail_args {
2679ddb7954SMike Barcroft  *	struct jail *jail;
2689ddb7954SMike Barcroft  * };
269116734c4SMatthew Dillon  */
27075c13541SPoul-Henning Kamp int
2719ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap)
27275c13541SPoul-Henning Kamp {
273413628a7SBjoern A. Zeeb 	uint32_t version;
274413628a7SBjoern A. Zeeb 	int error;
2750304c731SJamie Gritton 	struct jail j;
276413628a7SBjoern A. Zeeb 
277413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
278413628a7SBjoern A. Zeeb 	if (error)
279413628a7SBjoern A. Zeeb 		return (error);
280413628a7SBjoern A. Zeeb 
281413628a7SBjoern A. Zeeb 	switch (version) {
282413628a7SBjoern A. Zeeb 	case 0:
283413628a7SBjoern A. Zeeb 	{
284413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
285413628a7SBjoern A. Zeeb 
2860304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2870304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
288413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
289413628a7SBjoern A. Zeeb 		if (error)
290413628a7SBjoern A. Zeeb 			return (error);
2910304c731SJamie Gritton 		j.version = j0.version;
2920304c731SJamie Gritton 		j.path = j0.path;
2930304c731SJamie Gritton 		j.hostname = j0.hostname;
2940304c731SJamie Gritton 		j.ip4s = j0.ip_number;
295413628a7SBjoern A. Zeeb 		break;
296413628a7SBjoern A. Zeeb 	}
297413628a7SBjoern A. Zeeb 
298413628a7SBjoern A. Zeeb 	case 1:
299413628a7SBjoern A. Zeeb 		/*
300413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
301413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
302413628a7SBjoern A. Zeeb 		 */
303413628a7SBjoern A. Zeeb 		return (EINVAL);
304413628a7SBjoern A. Zeeb 
305413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
306413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
307413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
308413628a7SBjoern A. Zeeb 		if (error)
309413628a7SBjoern A. Zeeb 			return (error);
3100304c731SJamie Gritton 		break;
3110304c731SJamie Gritton 
3120304c731SJamie Gritton 	default:
3130304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3140304c731SJamie Gritton 		return (EINVAL);
3150304c731SJamie Gritton 	}
3160304c731SJamie Gritton 	return (kern_jail(td, &j));
3170304c731SJamie Gritton }
3180304c731SJamie Gritton 
3190304c731SJamie Gritton int
3200304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j)
3210304c731SJamie Gritton {
322e92e0574SJamie Gritton 	struct iovec optiov[2 * (4
323e92e0574SJamie Gritton 			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
324e92e0574SJamie Gritton #ifdef INET
325e92e0574SJamie Gritton 			    + 1
326e92e0574SJamie Gritton #endif
327e92e0574SJamie Gritton #ifdef INET6
328e92e0574SJamie Gritton 			    + 1
329e92e0574SJamie Gritton #endif
330e92e0574SJamie Gritton 			    )];
3310304c731SJamie Gritton 	struct uio opt;
3320304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
3330304c731SJamie Gritton #ifdef INET
334e92e0574SJamie Gritton 	uint32_t ip4s;
3350304c731SJamie Gritton 	struct in_addr *u_ip4;
3360304c731SJamie Gritton #endif
3370304c731SJamie Gritton #ifdef INET6
3380304c731SJamie Gritton 	struct in6_addr *u_ip6;
3390304c731SJamie Gritton #endif
3400304c731SJamie Gritton 	size_t tmplen;
3410304c731SJamie Gritton 	int error, enforce_statfs, fi;
3420304c731SJamie Gritton 
3430304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3440304c731SJamie Gritton 	opt.uio_iov = optiov;
3450304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3460304c731SJamie Gritton 	opt.uio_offset = -1;
3470304c731SJamie Gritton 	opt.uio_resid = -1;
3480304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3490304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3500304c731SJamie Gritton 	opt.uio_td = td;
3510304c731SJamie Gritton 
3520304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3530304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
3540304c731SJamie Gritton 		for (fi = 0; fi < sizeof(pr_allow_names) /
3550304c731SJamie Gritton 		     sizeof(pr_allow_names[0]); fi++) {
3560304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_base =
3570304c731SJamie Gritton 			    (jail_default_allow & (1 << fi))
3580304c731SJamie Gritton 			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
3590304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3600304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3610304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3620304c731SJamie Gritton 		}
3630304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3640304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3650304c731SJamie Gritton 		opt.uio_iovcnt++;
3660304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3670304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3680304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3690304c731SJamie Gritton 		opt.uio_iovcnt++;
3700304c731SJamie Gritton 	}
3710304c731SJamie Gritton 
372b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
373b38ff370SJamie Gritton #ifdef INET
3740304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3750304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
376b38ff370SJamie Gritton 		return (EINVAL);
3770304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
378b38ff370SJamie Gritton #else
3790304c731SJamie Gritton 	if (j->ip4s > 0)
380b38ff370SJamie Gritton 		return (EINVAL);
381b38ff370SJamie Gritton #endif
382b38ff370SJamie Gritton #ifdef INET6
3830304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
384b38ff370SJamie Gritton 		return (EINVAL);
3850304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
386b38ff370SJamie Gritton #else
3870304c731SJamie Gritton 	if (j->ip6s > 0)
388b38ff370SJamie Gritton 		return (EINVAL);
389b38ff370SJamie Gritton #endif
390b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
391b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
392b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
393b38ff370SJamie Gritton #ifdef INET
394b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
395b38ff370SJamie Gritton #endif
396b38ff370SJamie Gritton #ifdef INET6
397b38ff370SJamie Gritton #ifdef INET
3980304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
399b38ff370SJamie Gritton #else
400b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
401b38ff370SJamie Gritton #endif
402b38ff370SJamie Gritton #endif
4030304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4040304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4050304c731SJamie Gritton 	opt.uio_iovcnt++;
4060304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4070304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4080304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
409b38ff370SJamie Gritton 	if (error) {
410b38ff370SJamie Gritton 		free(u_path, M_TEMP);
411b38ff370SJamie Gritton 		return (error);
412b38ff370SJamie Gritton 	}
4130304c731SJamie Gritton 	opt.uio_iovcnt++;
4140304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4150304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4160304c731SJamie Gritton 	opt.uio_iovcnt++;
4170304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4180304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4190304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
420b38ff370SJamie Gritton 	if (error) {
421b38ff370SJamie Gritton 		free(u_path, M_TEMP);
422b38ff370SJamie Gritton 		return (error);
423b38ff370SJamie Gritton 	}
4240304c731SJamie Gritton 	opt.uio_iovcnt++;
4250304c731SJamie Gritton 	if (j->jailname != NULL) {
426b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
427b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
428b38ff370SJamie Gritton 		opt.uio_iovcnt++;
429b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4300304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
431b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
432b38ff370SJamie Gritton 		if (error) {
433b38ff370SJamie Gritton 			free(u_path, M_TEMP);
434b38ff370SJamie Gritton 			return (error);
435b38ff370SJamie Gritton 		}
436b38ff370SJamie Gritton 		opt.uio_iovcnt++;
437b38ff370SJamie Gritton 	}
438b38ff370SJamie Gritton #ifdef INET
439b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
440b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
441b38ff370SJamie Gritton 	opt.uio_iovcnt++;
442b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4430304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4440304c731SJamie Gritton 	if (j->version == 0)
4450304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4460304c731SJamie Gritton 	else {
4470304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
448b38ff370SJamie Gritton 		if (error) {
449b38ff370SJamie Gritton 			free(u_path, M_TEMP);
450b38ff370SJamie Gritton 			return (error);
451b38ff370SJamie Gritton 		}
4520304c731SJamie Gritton 	}
453b38ff370SJamie Gritton 	opt.uio_iovcnt++;
454b38ff370SJamie Gritton #endif
455b38ff370SJamie Gritton #ifdef INET6
456b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
457b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
458b38ff370SJamie Gritton 	opt.uio_iovcnt++;
459b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4600304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4610304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
462b38ff370SJamie Gritton 	if (error) {
463b38ff370SJamie Gritton 		free(u_path, M_TEMP);
464b38ff370SJamie Gritton 		return (error);
465b38ff370SJamie Gritton 	}
466b38ff370SJamie Gritton 	opt.uio_iovcnt++;
467b38ff370SJamie Gritton #endif
4680304c731SJamie Gritton 	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
4690304c731SJamie Gritton 	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
470b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
471b38ff370SJamie Gritton 	free(u_path, M_TEMP);
472b38ff370SJamie Gritton 	return (error);
473b38ff370SJamie Gritton }
474b38ff370SJamie Gritton 
4750304c731SJamie Gritton 
476b38ff370SJamie Gritton /*
477b38ff370SJamie Gritton  * struct jail_set_args {
478b38ff370SJamie Gritton  *	struct iovec *iovp;
479b38ff370SJamie Gritton  *	unsigned int iovcnt;
480b38ff370SJamie Gritton  *	int flags;
481b38ff370SJamie Gritton  * };
482b38ff370SJamie Gritton  */
483b38ff370SJamie Gritton int
484b38ff370SJamie Gritton jail_set(struct thread *td, struct jail_set_args *uap)
485b38ff370SJamie Gritton {
486b38ff370SJamie Gritton 	struct uio *auio;
487b38ff370SJamie Gritton 	int error;
488b38ff370SJamie Gritton 
489b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
490b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
491b38ff370SJamie Gritton 		return (EINVAL);
492b38ff370SJamie Gritton 
493b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
494b38ff370SJamie Gritton 	if (error)
495b38ff370SJamie Gritton 		return (error);
496b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
497b38ff370SJamie Gritton 	free(auio, M_IOV);
498b38ff370SJamie Gritton 	return (error);
499413628a7SBjoern A. Zeeb }
500413628a7SBjoern A. Zeeb 
501413628a7SBjoern A. Zeeb int
502b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
503413628a7SBjoern A. Zeeb {
504fd7a8150SMike Barcroft 	struct nameidata nd;
505b38ff370SJamie Gritton #ifdef INET
506b38ff370SJamie Gritton 	struct in_addr *ip4;
507413628a7SBjoern A. Zeeb #endif
508b38ff370SJamie Gritton #ifdef INET6
509b38ff370SJamie Gritton 	struct in6_addr *ip6;
510b38ff370SJamie Gritton #endif
511b38ff370SJamie Gritton 	struct vfsopt *opt;
512b38ff370SJamie Gritton 	struct vfsoptlist *opts;
51357aea6dfSBjoern A. Zeeb 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
514b38ff370SJamie Gritton 	struct vnode *root;
515babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
5160304c731SJamie Gritton #if defined(INET) || defined(INET6)
51757aea6dfSBjoern A. Zeeb 	struct prison *tppr;
518b38ff370SJamie Gritton 	void *op;
5190304c731SJamie Gritton #endif
52076ca6f88SJamie Gritton 	unsigned long hid;
5210304c731SJamie Gritton 	size_t namelen, onamelen;
5220304c731SJamie Gritton 	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
5237cbf7213SJamie Gritton 	int gotchildmax, gotenforce, gothid, gotslevel;
5247cbf7213SJamie Gritton 	int fi, jid, jsys, len, level;
525b97457e2SJamie Gritton 	int childmax, slevel, vfslocked;
526d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
5270304c731SJamie Gritton 	int ii, ij;
528413628a7SBjoern A. Zeeb #endif
529413628a7SBjoern A. Zeeb #ifdef INET
5302b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
531413628a7SBjoern A. Zeeb #endif
532b38ff370SJamie Gritton #ifdef INET6
5332b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
534b38ff370SJamie Gritton #endif
535b38ff370SJamie Gritton 	unsigned pr_flags, ch_flags;
5360304c731SJamie Gritton 	unsigned pr_allow, ch_allow, tallow;
537b38ff370SJamie Gritton 	char numbuf[12];
538b38ff370SJamie Gritton 
539b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
540b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
541b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
542b38ff370SJamie Gritton 	if (error)
54375c13541SPoul-Henning Kamp 		return (error);
5440304c731SJamie Gritton 	mypr = ppr = td->td_ucred->cr_prison;
545b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5460304c731SJamie Gritton 		return (EPERM);
547b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
548b38ff370SJamie Gritton 		return (EINVAL);
549b38ff370SJamie Gritton 
550b38ff370SJamie Gritton 	/*
551b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
552b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
553b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
554b38ff370SJamie Gritton 	 * the allprison lock.
555b38ff370SJamie Gritton 	 *
556b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
557b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
558b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
559b38ff370SJamie Gritton 	 */
560b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
561b38ff370SJamie Gritton 	if (error)
562b38ff370SJamie Gritton 		return (error);
563b38ff370SJamie Gritton #ifdef INET
564b38ff370SJamie Gritton 	ip4 = NULL;
565b38ff370SJamie Gritton #endif
566b38ff370SJamie Gritton #ifdef INET6
567b38ff370SJamie Gritton 	ip6 = NULL;
568b38ff370SJamie Gritton #endif
569b38ff370SJamie Gritton 
570b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
571b38ff370SJamie Gritton 	if (error == ENOENT)
572b38ff370SJamie Gritton 		jid = 0;
573b38ff370SJamie Gritton 	else if (error != 0)
574b38ff370SJamie Gritton 		goto done_free;
575b38ff370SJamie Gritton 
576b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
577b38ff370SJamie Gritton 	if (error == ENOENT)
578b38ff370SJamie Gritton 		gotslevel = 0;
579b38ff370SJamie Gritton 	else if (error != 0)
580b38ff370SJamie Gritton 		goto done_free;
581b38ff370SJamie Gritton 	else
582b38ff370SJamie Gritton 		gotslevel = 1;
583b38ff370SJamie Gritton 
584b97457e2SJamie Gritton 	error =
585b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
586b97457e2SJamie Gritton 	if (error == ENOENT)
587b97457e2SJamie Gritton 		gotchildmax = 0;
588b97457e2SJamie Gritton 	else if (error != 0)
589b97457e2SJamie Gritton 		goto done_free;
590b97457e2SJamie Gritton 	else
591b97457e2SJamie Gritton 		gotchildmax = 1;
592b97457e2SJamie Gritton 
5930304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
594f337198dSJamie Gritton 	if (error == ENOENT)
595f337198dSJamie Gritton 		gotenforce = 0;
596f337198dSJamie Gritton 	else if (error != 0)
5970304c731SJamie Gritton 		goto done_free;
598f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
599f337198dSJamie Gritton 		error = EINVAL;
600f337198dSJamie Gritton 		goto done_free;
601f337198dSJamie Gritton 	} else
602f337198dSJamie Gritton 		gotenforce = 1;
6030304c731SJamie Gritton 
604b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
6050304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
6060304c731SJamie Gritton 	    fi++) {
6070304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
6080304c731SJamie Gritton 			continue;
6090304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
6100304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
6110304c731SJamie Gritton 	}
612b38ff370SJamie Gritton 	ch_flags |= pr_flags;
6137cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
6147cbf7213SJamie Gritton 	    fi++) {
6157cbf7213SJamie Gritton 		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
6167cbf7213SJamie Gritton 		    sizeof(jsys));
6177cbf7213SJamie Gritton 		if (error == ENOENT)
6187cbf7213SJamie Gritton 			continue;
6197cbf7213SJamie Gritton 		if (error != 0)
6207cbf7213SJamie Gritton 			goto done_free;
6217cbf7213SJamie Gritton 		switch (jsys) {
6227cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
6237cbf7213SJamie Gritton 			if (!pr_flag_jailsys[fi].disable) {
6247cbf7213SJamie Gritton 				error = EINVAL;
6257cbf7213SJamie Gritton 				goto done_free;
6267cbf7213SJamie Gritton 			}
6277cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].disable;
6287cbf7213SJamie Gritton 			break;
6297cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
6307cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].new;
6317cbf7213SJamie Gritton 			break;
6327cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6337cbf7213SJamie Gritton 			break;
6347cbf7213SJamie Gritton 		default:
6357cbf7213SJamie Gritton 			error = EINVAL;
6367cbf7213SJamie Gritton 			goto done_free;
6377cbf7213SJamie Gritton 		}
6387cbf7213SJamie Gritton 		ch_flags |=
6397cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
6407cbf7213SJamie Gritton 	}
6414affa14cSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
6424affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6434affa14cSJamie Gritton 		error = EINVAL;
6444affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6454affa14cSJamie Gritton 		goto done_errmsg;
6464affa14cSJamie Gritton 	}
647679e1390SJamie Gritton #ifdef VIMAGE
648679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
649679e1390SJamie Gritton 		error = EINVAL;
650679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
651679e1390SJamie Gritton 		goto done_errmsg;
652679e1390SJamie Gritton 	}
653679e1390SJamie Gritton #endif
6542b0d6f81SJamie Gritton #ifdef INET
6552b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
6562b0d6f81SJamie Gritton 		error = EINVAL;
6572b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
6582b0d6f81SJamie Gritton 		goto done_errmsg;
6592b0d6f81SJamie Gritton 	}
6602b0d6f81SJamie Gritton #endif
6612b0d6f81SJamie Gritton #ifdef INET6
6622b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
6632b0d6f81SJamie Gritton 		error = EINVAL;
6642b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
6652b0d6f81SJamie Gritton 		goto done_errmsg;
6662b0d6f81SJamie Gritton 	}
6672b0d6f81SJamie Gritton #endif
668b38ff370SJamie Gritton 
6690304c731SJamie Gritton 	pr_allow = ch_allow = 0;
6700304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
6710304c731SJamie Gritton 	    fi++) {
6720304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
6730304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
6740304c731SJamie Gritton 	}
6750304c731SJamie Gritton 	ch_allow |= pr_allow;
6760304c731SJamie Gritton 
677b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
678b38ff370SJamie Gritton 	if (error == ENOENT)
679b38ff370SJamie Gritton 		name = NULL;
680b38ff370SJamie Gritton 	else if (error != 0)
681b38ff370SJamie Gritton 		goto done_free;
682b38ff370SJamie Gritton 	else {
683b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
684b38ff370SJamie Gritton 			error = EINVAL;
685b38ff370SJamie Gritton 			goto done_free;
686b38ff370SJamie Gritton 		}
687b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
688b38ff370SJamie Gritton 			error = ENAMETOOLONG;
689b38ff370SJamie Gritton 			goto done_free;
690b38ff370SJamie Gritton 		}
691b38ff370SJamie Gritton 	}
692b38ff370SJamie Gritton 
693b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
694b38ff370SJamie Gritton 	if (error == ENOENT)
695b38ff370SJamie Gritton 		host = NULL;
696b38ff370SJamie Gritton 	else if (error != 0)
697b38ff370SJamie Gritton 		goto done_free;
698b38ff370SJamie Gritton 	else {
69976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
70076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
701b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
702b38ff370SJamie Gritton 			error = EINVAL;
703b38ff370SJamie Gritton 			goto done_free;
704b38ff370SJamie Gritton 		}
705b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
706b38ff370SJamie Gritton 			error = ENAMETOOLONG;
707b38ff370SJamie Gritton 			goto done_free;
708b38ff370SJamie Gritton 		}
709b38ff370SJamie Gritton 	}
710b38ff370SJamie Gritton 
71176ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
71276ca6f88SJamie Gritton 	if (error == ENOENT)
71376ca6f88SJamie Gritton 		domain = NULL;
71476ca6f88SJamie Gritton 	else if (error != 0)
71576ca6f88SJamie Gritton 		goto done_free;
71676ca6f88SJamie Gritton 	else {
71776ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
71876ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
71976ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
72076ca6f88SJamie Gritton 			error = EINVAL;
72176ca6f88SJamie Gritton 			goto done_free;
72276ca6f88SJamie Gritton 		}
72376ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
72476ca6f88SJamie Gritton 			error = ENAMETOOLONG;
72576ca6f88SJamie Gritton 			goto done_free;
72676ca6f88SJamie Gritton 		}
72776ca6f88SJamie Gritton 	}
72876ca6f88SJamie Gritton 
72976ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
73076ca6f88SJamie Gritton 	if (error == ENOENT)
73176ca6f88SJamie Gritton 		uuid = NULL;
73276ca6f88SJamie Gritton 	else if (error != 0)
73376ca6f88SJamie Gritton 		goto done_free;
73476ca6f88SJamie Gritton 	else {
73576ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
73676ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
73776ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
73876ca6f88SJamie Gritton 			error = EINVAL;
73976ca6f88SJamie Gritton 			goto done_free;
74076ca6f88SJamie Gritton 		}
74176ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
74276ca6f88SJamie Gritton 			error = ENAMETOOLONG;
74376ca6f88SJamie Gritton 			goto done_free;
74476ca6f88SJamie Gritton 		}
74576ca6f88SJamie Gritton 	}
74676ca6f88SJamie Gritton 
747841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
748a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
74976ca6f88SJamie Gritton 		uint32_t hid32;
75076ca6f88SJamie Gritton 
75176ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
75276ca6f88SJamie Gritton 		hid = hid32;
75376ca6f88SJamie Gritton 	} else
75476ca6f88SJamie Gritton #endif
75576ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
75676ca6f88SJamie Gritton 	if (error == ENOENT)
75776ca6f88SJamie Gritton 		gothid = 0;
75876ca6f88SJamie Gritton 	else if (error != 0)
75976ca6f88SJamie Gritton 		goto done_free;
76076ca6f88SJamie Gritton 	else {
76176ca6f88SJamie Gritton 		gothid = 1;
76276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
76376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
76476ca6f88SJamie Gritton 	}
76576ca6f88SJamie Gritton 
766b38ff370SJamie Gritton #ifdef INET
767b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
768b38ff370SJamie Gritton 	if (error == ENOENT)
7697cbf7213SJamie Gritton 		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
770b38ff370SJamie Gritton 	else if (error != 0)
771b38ff370SJamie Gritton 		goto done_free;
772b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
773b38ff370SJamie Gritton 		error = EINVAL;
774b38ff370SJamie Gritton 		goto done_free;
7750304c731SJamie Gritton 	} else {
7767cbf7213SJamie Gritton 		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
7777cbf7213SJamie Gritton 		if (ip4s == 0)
7787cbf7213SJamie Gritton 			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
7797cbf7213SJamie Gritton 		else {
7807cbf7213SJamie Gritton 			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
781b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
782b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
783b38ff370SJamie Gritton 				error = EINVAL;
784b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
785b38ff370SJamie Gritton 				goto done_errmsg;
786b38ff370SJamie Gritton 			}
7872b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
788b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
789b38ff370SJamie Gritton 			/*
7900304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
7910304c731SJamie Gritton 			 * the primary IP address as given from userland.
7920304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
793bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
794bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
795bef916f9SBjoern A. Zeeb 			 * address to connect from.
796b38ff370SJamie Gritton 			 */
797b38ff370SJamie Gritton 			if (ip4s > 1)
798b38ff370SJamie Gritton 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
799b38ff370SJamie Gritton 			/*
8000304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
8010304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
8020304c731SJamie Gritton 			 * addresses it is their problem.
803b38ff370SJamie Gritton 			 *
8040304c731SJamie Gritton 			 * We do not have to care about byte order for these
8050304c731SJamie Gritton 			 * checks so we will do them in NBO.
806b38ff370SJamie Gritton 			 */
807b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
808b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
809b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
810b38ff370SJamie Gritton 					error = EINVAL;
811b38ff370SJamie Gritton 					goto done_free;
812b38ff370SJamie Gritton 				}
813b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
814b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
815b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
816b38ff370SJamie Gritton 					error = EINVAL;
817b38ff370SJamie Gritton 					goto done_free;
818b38ff370SJamie Gritton 				}
819b38ff370SJamie Gritton 			}
820b38ff370SJamie Gritton 		}
8210304c731SJamie Gritton 	}
822b38ff370SJamie Gritton #endif
823b38ff370SJamie Gritton 
824b38ff370SJamie Gritton #ifdef INET6
825b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
826b38ff370SJamie Gritton 	if (error == ENOENT)
8277cbf7213SJamie Gritton 		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
828b38ff370SJamie Gritton 	else if (error != 0)
829b38ff370SJamie Gritton 		goto done_free;
830b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
831b38ff370SJamie Gritton 		error = EINVAL;
832b38ff370SJamie Gritton 		goto done_free;
8330304c731SJamie Gritton 	} else {
8347cbf7213SJamie Gritton 		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
8357cbf7213SJamie Gritton 		if (ip6s == 0)
8367cbf7213SJamie Gritton 			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
8377cbf7213SJamie Gritton 		else {
8387cbf7213SJamie Gritton 			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
839b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
840b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
841b38ff370SJamie Gritton 				error = EINVAL;
842b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
843b38ff370SJamie Gritton 				goto done_errmsg;
844b38ff370SJamie Gritton 			}
8452b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
846b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
847b38ff370SJamie Gritton 			if (ip6s > 1)
848b38ff370SJamie Gritton 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
849b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8500304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
851b38ff370SJamie Gritton 					error = EINVAL;
852b38ff370SJamie Gritton 					goto done_free;
853b38ff370SJamie Gritton 				}
854b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
855b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
856b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
857b38ff370SJamie Gritton 				{
858b38ff370SJamie Gritton 					error = EINVAL;
859b38ff370SJamie Gritton 					goto done_free;
860b38ff370SJamie Gritton 				}
861b38ff370SJamie Gritton 			}
862b38ff370SJamie Gritton 		}
8630304c731SJamie Gritton 	}
864b38ff370SJamie Gritton #endif
865b38ff370SJamie Gritton 
866bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
867bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
868bdfc8cc4SJamie Gritton 		error = EINVAL;
869bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
870bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
871bdfc8cc4SJamie Gritton 		goto done_errmsg;
872bdfc8cc4SJamie Gritton 	}
873bdfc8cc4SJamie Gritton #endif
874bdfc8cc4SJamie Gritton 
875b38ff370SJamie Gritton 	root = NULL;
876b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
877b38ff370SJamie Gritton 	if (error == ENOENT)
878b38ff370SJamie Gritton 		path = NULL;
879b38ff370SJamie Gritton 	else if (error != 0)
880b38ff370SJamie Gritton 		goto done_free;
881b38ff370SJamie Gritton 	else {
882b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
883b38ff370SJamie Gritton 			error = EINVAL;
884b38ff370SJamie Gritton 			vfs_opterror(opts,
885b38ff370SJamie Gritton 			    "path cannot be changed after creation");
886b38ff370SJamie Gritton 			goto done_errmsg;
887b38ff370SJamie Gritton 		}
888b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
889b38ff370SJamie Gritton 			error = EINVAL;
890b38ff370SJamie Gritton 			goto done_free;
891b38ff370SJamie Gritton 		}
892b38ff370SJamie Gritton 		if (len < 2 || (len == 2 && path[0] == '/'))
893b38ff370SJamie Gritton 			path = NULL;
894b38ff370SJamie Gritton 		else {
8950304c731SJamie Gritton 			/* Leave room for a real-root full pathname. */
8960304c731SJamie Gritton 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
8970304c731SJamie Gritton 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
8980304c731SJamie Gritton 				error = ENAMETOOLONG;
8990304c731SJamie Gritton 				goto done_free;
9000304c731SJamie Gritton 			}
901b38ff370SJamie Gritton 			NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
902b38ff370SJamie Gritton 			    path, td);
903b38ff370SJamie Gritton 			error = namei(&nd);
904b38ff370SJamie Gritton 			if (error)
905b38ff370SJamie Gritton 				goto done_free;
906b38ff370SJamie Gritton 			vfslocked = NDHASGIANT(&nd);
907b38ff370SJamie Gritton 			root = nd.ni_vp;
908b38ff370SJamie Gritton 			NDFREE(&nd, NDF_ONLY_PNBUF);
909b38ff370SJamie Gritton 			if (root->v_type != VDIR) {
910b38ff370SJamie Gritton 				error = ENOTDIR;
911b38ff370SJamie Gritton 				vrele(root);
912b38ff370SJamie Gritton 				VFS_UNLOCK_GIANT(vfslocked);
913b38ff370SJamie Gritton 				goto done_free;
914b38ff370SJamie Gritton 			}
915b38ff370SJamie Gritton 			VFS_UNLOCK_GIANT(vfslocked);
916b38ff370SJamie Gritton 		}
917b38ff370SJamie Gritton 	}
918b38ff370SJamie Gritton 
919b38ff370SJamie Gritton 	/*
920b38ff370SJamie Gritton 	 * Grab the allprison lock before letting modules check their
921b38ff370SJamie Gritton 	 * parameters.  Once we have it, do not let go so we'll have a
922b38ff370SJamie Gritton 	 * consistent view of the OSD list.
923b38ff370SJamie Gritton 	 */
924b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
925b38ff370SJamie Gritton 	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
926b38ff370SJamie Gritton 	if (error)
927b38ff370SJamie Gritton 		goto done_unlock_list;
928b38ff370SJamie Gritton 
929b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
930b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
931b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
932b38ff370SJamie Gritton 			error = EINVAL;
933b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
934b38ff370SJamie Gritton 			goto done_unlock_list;
935b38ff370SJamie Gritton 		}
936b38ff370SJamie Gritton 	}
937b38ff370SJamie Gritton 
938b38ff370SJamie Gritton 	/*
939b38ff370SJamie Gritton 	 * See if we are creating a new record or updating an existing one.
940b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
941b38ff370SJamie Gritton 	 */
942b38ff370SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
943b38ff370SJamie Gritton 	if (!cuflags) {
944b38ff370SJamie Gritton 		error = EINVAL;
945b38ff370SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
946b38ff370SJamie Gritton 		goto done_unlock_list;
947b38ff370SJamie Gritton 	}
948b38ff370SJamie Gritton 	pr = NULL;
949babbbb9cSJamie Gritton 	namelc = NULL;
950babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
951babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
952babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
953babbbb9cSJamie Gritton 		if (*p != '\0')
954babbbb9cSJamie Gritton 			jid = 0;
955babbbb9cSJamie Gritton 	}
956b38ff370SJamie Gritton 	if (jid != 0) {
9570304c731SJamie Gritton 		/*
9580304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
9590304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
9600304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
9610304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
9620304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
9630304c731SJamie Gritton 		 */
964b38ff370SJamie Gritton 		if (jid < 0) {
965b38ff370SJamie Gritton 			error = EINVAL;
966b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
967b38ff370SJamie Gritton 			goto done_unlock_list;
968b38ff370SJamie Gritton 		}
969b38ff370SJamie Gritton 		pr = prison_find(jid);
970b38ff370SJamie Gritton 		if (pr != NULL) {
9710304c731SJamie Gritton 			ppr = pr->pr_parent;
972b38ff370SJamie Gritton 			/* Create: jid must not exist. */
973b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
974b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
975b38ff370SJamie Gritton 				error = EEXIST;
976b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
977b38ff370SJamie Gritton 				    jid);
978b38ff370SJamie Gritton 				goto done_unlock_list;
979b38ff370SJamie Gritton 			}
9800304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
9810304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
9820304c731SJamie Gritton 				pr = NULL;
9830304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
984b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
985b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
986b38ff370SJamie Gritton 					error = ENOENT;
987b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
988b38ff370SJamie Gritton 					    jid);
989b38ff370SJamie Gritton 					goto done_unlock_list;
990b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
991b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
992b38ff370SJamie Gritton 					/*
993b38ff370SJamie Gritton 					 * A dying jail might be resurrected
994b38ff370SJamie Gritton 					 * (via attach or persist), but first
995b38ff370SJamie Gritton 					 * it must determine if another jail
996b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
997b38ff370SJamie Gritton 					 * this by implicitly re-setting the
998b38ff370SJamie Gritton 					 * name.
999b38ff370SJamie Gritton 					 */
1000b38ff370SJamie Gritton 					if (name == NULL)
10010304c731SJamie Gritton 						name = prison_name(mypr, pr);
1002b38ff370SJamie Gritton 				}
1003b38ff370SJamie Gritton 			}
1004b38ff370SJamie Gritton 		}
1005b38ff370SJamie Gritton 		if (pr == NULL) {
1006b38ff370SJamie Gritton 			/* Update: jid must exist. */
1007b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1008b38ff370SJamie Gritton 				error = ENOENT;
1009b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
1010b38ff370SJamie Gritton 				goto done_unlock_list;
1011b38ff370SJamie Gritton 			}
1012b38ff370SJamie Gritton 		}
1013b38ff370SJamie Gritton 	}
1014b38ff370SJamie Gritton 	/*
1015b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1016b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1017b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1018b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1019b38ff370SJamie Gritton 	 * because that is the jail being updated).
1020b38ff370SJamie Gritton 	 */
1021b38ff370SJamie Gritton 	if (name != NULL) {
1022babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1023babbbb9cSJamie Gritton 		if (namelc == NULL)
1024babbbb9cSJamie Gritton 			namelc = name;
1025babbbb9cSJamie Gritton 		else {
10260304c731SJamie Gritton 			/*
10270304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
10280304c731SJamie Gritton 			 * parent and child names, and make sure the parent
10290304c731SJamie Gritton 			 * exists or matches an already found jail.
10300304c731SJamie Gritton 			 */
1031babbbb9cSJamie Gritton 			*namelc = '\0';
10320304c731SJamie Gritton 			if (pr != NULL) {
1033babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1034babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
10350304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
10360304c731SJamie Gritton 					error = EINVAL;
10370304c731SJamie Gritton 					vfs_opterror(opts,
10380304c731SJamie Gritton 					    "cannot change jail's parent");
10390304c731SJamie Gritton 					goto done_unlock_list;
10400304c731SJamie Gritton 				}
10410304c731SJamie Gritton 			} else {
10420304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
10430304c731SJamie Gritton 				if (ppr == NULL) {
10440304c731SJamie Gritton 					error = ENOENT;
10450304c731SJamie Gritton 					vfs_opterror(opts,
10460304c731SJamie Gritton 					    "jail \"%s\" not found", name);
10470304c731SJamie Gritton 					goto done_unlock_list;
10480304c731SJamie Gritton 				}
10490304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
10500304c731SJamie Gritton 			}
1051babbbb9cSJamie Gritton 			name = ++namelc;
10520304c731SJamie Gritton 		}
1053b38ff370SJamie Gritton 		if (name[0] != '\0') {
10540304c731SJamie Gritton 			namelen =
10550304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1056b38ff370SJamie Gritton  name_again:
10570304c731SJamie Gritton 			deadpr = NULL;
10580304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
1059b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
10600304c731SJamie Gritton 				    !strcmp(tpr->pr_name + namelen, name)) {
1061b38ff370SJamie Gritton 					if (pr == NULL &&
1062b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
1063b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
1064b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
1065b38ff370SJamie Gritton 							/*
1066b38ff370SJamie Gritton 							 * Use this jail
1067b38ff370SJamie Gritton 							 * for updates.
1068b38ff370SJamie Gritton 							 */
1069b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
1070b38ff370SJamie Gritton 								pr = tpr;
1071b38ff370SJamie Gritton 								break;
1072b38ff370SJamie Gritton 							}
1073b38ff370SJamie Gritton 							deadpr = tpr;
1074b38ff370SJamie Gritton 						}
1075b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1076b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
1077b38ff370SJamie Gritton 						/*
1078b38ff370SJamie Gritton 						 * Create, or update(jid):
1079b38ff370SJamie Gritton 						 * name must not exist in an
10800304c731SJamie Gritton 						 * active sibling jail.
1081b38ff370SJamie Gritton 						 */
1082b38ff370SJamie Gritton 						error = EEXIST;
1083b38ff370SJamie Gritton 						if (pr != NULL)
1084b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
1085b38ff370SJamie Gritton 						vfs_opterror(opts,
1086b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1087b38ff370SJamie Gritton 						   name);
1088b38ff370SJamie Gritton 						goto done_unlock_list;
1089b38ff370SJamie Gritton 					}
1090b38ff370SJamie Gritton 				}
1091b38ff370SJamie Gritton 			}
1092b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1093b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1094b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1095b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
1096b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
1097b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1098b38ff370SJamie Gritton 						goto name_again;
1099b38ff370SJamie Gritton 					}
1100b38ff370SJamie Gritton 					pr = deadpr;
1101b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1102b38ff370SJamie Gritton 					error = ENOENT;
1103b38ff370SJamie Gritton 					vfs_opterror(opts,
1104b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1105b38ff370SJamie Gritton 					goto done_unlock_list;
1106b38ff370SJamie Gritton 				}
1107b38ff370SJamie Gritton 			}
1108b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1109b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1110b38ff370SJamie Gritton 				error = ENOENT;
1111b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1112b38ff370SJamie Gritton 				    name);
1113b38ff370SJamie Gritton 				goto done_unlock_list;
1114b38ff370SJamie Gritton 			}
1115b38ff370SJamie Gritton 		}
1116b38ff370SJamie Gritton 	}
1117b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1118b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1119b38ff370SJamie Gritton 		error = ENOENT;
1120b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1121b38ff370SJamie Gritton 		goto done_unlock_list;
1122b38ff370SJamie Gritton 	}
1123b38ff370SJamie Gritton 
1124b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1125b38ff370SJamie Gritton 	if (pr == NULL) {
1126b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1127b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1128b97457e2SJamie Gritton 				error = EPERM;
1129b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
1130b97457e2SJamie Gritton 				goto done_unlock_list;
1131b97457e2SJamie Gritton 			}
1132b38ff370SJamie Gritton 		created = 1;
11330304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
11340304c731SJamie Gritton 		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
11350304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
11360304c731SJamie Gritton 			error = ENOENT;
11370304c731SJamie Gritton 			vfs_opterror(opts, "parent jail went away!");
11380304c731SJamie Gritton 			goto done_unlock_list;
11390304c731SJamie Gritton 		}
11400304c731SJamie Gritton 		ppr->pr_ref++;
11410304c731SJamie Gritton 		ppr->pr_uref++;
11420304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1143b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1144b38ff370SJamie Gritton 		if (jid == 0) {
1145b38ff370SJamie Gritton 			/* Find the next free jid. */
1146b38ff370SJamie Gritton 			jid = lastprid + 1;
1147b38ff370SJamie Gritton  findnext:
1148b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1149b38ff370SJamie Gritton 				jid = 1;
1150b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1151b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1152b38ff370SJamie Gritton 					continue;
1153b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1154b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1155b38ff370SJamie Gritton 					break;
1156b38ff370SJamie Gritton 				}
1157b38ff370SJamie Gritton 				if (jid == lastprid) {
1158b38ff370SJamie Gritton 					error = EAGAIN;
1159b38ff370SJamie Gritton 					vfs_opterror(opts,
1160b38ff370SJamie Gritton 					    "no available jail IDs");
1161b38ff370SJamie Gritton 					free(pr, M_PRISON);
11620304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
11630304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
11640304c731SJamie Gritton 					goto done_releroot;
1165b38ff370SJamie Gritton 				}
1166b38ff370SJamie Gritton 				jid++;
1167b38ff370SJamie Gritton 				goto findnext;
1168b38ff370SJamie Gritton 			}
1169b38ff370SJamie Gritton 			lastprid = jid;
1170b38ff370SJamie Gritton 		} else {
1171b38ff370SJamie Gritton 			/*
1172b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1173b38ff370SJamie Gritton 			 * so just find where to insert it.
1174b38ff370SJamie Gritton 			 */
1175b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1176b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1177b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1178b38ff370SJamie Gritton 					break;
1179b38ff370SJamie Gritton 				}
1180b38ff370SJamie Gritton 		}
1181b38ff370SJamie Gritton 		if (tpr == NULL)
1182b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
11830304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
11840304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1185b97457e2SJamie Gritton 			tpr->pr_childcount++;
1186b38ff370SJamie Gritton 
11870304c731SJamie Gritton 		pr->pr_parent = ppr;
1188b38ff370SJamie Gritton 		pr->pr_id = jid;
11890304c731SJamie Gritton 
11900304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1191b38ff370SJamie Gritton 		if (name == NULL)
1192b38ff370SJamie Gritton 			name = "";
1193b38ff370SJamie Gritton 		if (path == NULL) {
1194b38ff370SJamie Gritton 			path = "/";
11950304c731SJamie Gritton 			root = mypr->pr_root;
1196b38ff370SJamie Gritton 			vref(root);
1197b38ff370SJamie Gritton 		}
11988986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
11998986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1200bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1201bdfc8cc4SJamie Gritton #ifdef VIMAGE
1202bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1203bdfc8cc4SJamie Gritton #endif
1204bdfc8cc4SJamie Gritton 		{
12050304c731SJamie Gritton #ifdef INET
12062b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12072b0d6f81SJamie Gritton 				pr->pr_flags |=
12082b0d6f81SJamie Gritton 				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
12092b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12102b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12112b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12122b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12132b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12142b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12152b0d6f81SJamie Gritton 					    M_WAITOK);
12162b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12172b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12182b0d6f81SJamie Gritton 				}
12192b0d6f81SJamie Gritton 			}
12200304c731SJamie Gritton #endif
12210304c731SJamie Gritton #ifdef INET6
12222b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
12232b0d6f81SJamie Gritton 				pr->pr_flags |=
12242b0d6f81SJamie Gritton 				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
12252b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
12262b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
12272b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
12282b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
12292b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
12302b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
12312b0d6f81SJamie Gritton 					    M_WAITOK);
12322b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
12332b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
12342b0d6f81SJamie Gritton 				}
12352b0d6f81SJamie Gritton 			}
12360304c731SJamie Gritton #endif
1237bdfc8cc4SJamie Gritton 		}
1238bdfc8cc4SJamie Gritton #endif
1239592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1240592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1241592bcae8SBjoern A. Zeeb 
12420304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
12430304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
124442bebd82SJamie Gritton 		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1245b38ff370SJamie Gritton 
12460304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
12470304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1248b38ff370SJamie Gritton 
1249679e1390SJamie Gritton #ifdef VIMAGE
1250679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1251679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1252679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1253679e1390SJamie Gritton #endif
1254b38ff370SJamie Gritton 		/*
1255b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1256b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1257b38ff370SJamie Gritton 		 */
12580304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1259b38ff370SJamie Gritton 		if (error) {
1260b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1261b38ff370SJamie Gritton 			goto done_releroot;
1262b38ff370SJamie Gritton 		}
1263b38ff370SJamie Gritton 
1264b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1265b38ff370SJamie Gritton 		/*
1266b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1267b38ff370SJamie Gritton 		 * want other to see the incomplete prison once the
1268b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1269b38ff370SJamie Gritton 		 */
1270b38ff370SJamie Gritton 	} else {
1271b38ff370SJamie Gritton 		created = 0;
12722b0d6f81SJamie Gritton 		/*
12732b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
12742b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
12752b0d6f81SJamie Gritton 		 */
12762b0d6f81SJamie Gritton 		pr->pr_ref++;
1277bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1278bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1279bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1280bdfc8cc4SJamie Gritton 			error = EINVAL;
1281bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1282bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
1283bdfc8cc4SJamie Gritton 			goto done_deref_locked;
1284bdfc8cc4SJamie Gritton 		}
1285bdfc8cc4SJamie Gritton #endif
12862b0d6f81SJamie Gritton #ifdef INET
12872b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
12882b0d6f81SJamie Gritton 			error = EINVAL;
12892b0d6f81SJamie Gritton 			vfs_opterror(opts,
12902b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
12912b0d6f81SJamie Gritton 			goto done_deref_locked;
12922b0d6f81SJamie Gritton 		}
12932b0d6f81SJamie Gritton #endif
12942b0d6f81SJamie Gritton #ifdef INET6
12952b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
12962b0d6f81SJamie Gritton 			error = EINVAL;
12972b0d6f81SJamie Gritton 			vfs_opterror(opts,
12982b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
12992b0d6f81SJamie Gritton 			goto done_deref_locked;
13002b0d6f81SJamie Gritton 		}
13012b0d6f81SJamie Gritton #endif
1302b38ff370SJamie Gritton 	}
1303b38ff370SJamie Gritton 
1304b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13050304c731SJamie Gritton 	if (gotslevel) {
13060304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13070304c731SJamie Gritton 			error = EPERM;
13080304c731SJamie Gritton 			goto done_deref_locked;
13090304c731SJamie Gritton 		}
13100304c731SJamie Gritton 	}
1311b97457e2SJamie Gritton 	if (gotchildmax) {
1312b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1313b97457e2SJamie Gritton 			error = EPERM;
1314b97457e2SJamie Gritton 			goto done_deref_locked;
1315b97457e2SJamie Gritton 		}
1316b97457e2SJamie Gritton 	}
13170304c731SJamie Gritton 	if (gotenforce) {
13180304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
13190304c731SJamie Gritton 			error = EPERM;
13200304c731SJamie Gritton 			goto done_deref_locked;
13210304c731SJamie Gritton 		}
13220304c731SJamie Gritton 	}
1323b38ff370SJamie Gritton #ifdef INET
13242b0d6f81SJamie Gritton 	if (ip4s > 0) {
13250304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
13260304c731SJamie Gritton 			/*
13270304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
13280304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
13290304c731SJamie Gritton 			 * about the parent being unlocked, as any
13300304c731SJamie Gritton 			 * setting is done with allprison_lock held.
13310304c731SJamie Gritton 			 */
13320304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
13332b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
13340304c731SJamie Gritton 					break;
13350304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
13360304c731SJamie Gritton 				error = EPERM;
13370304c731SJamie Gritton 				goto done_deref_locked;
13380304c731SJamie Gritton 			}
13390304c731SJamie Gritton 			if (ip4s > 1) {
13400304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
13410304c731SJamie Gritton 					if (ip4[ii].s_addr ==
13420304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1343b38ff370SJamie Gritton 						continue;
13440304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
13450304c731SJamie Gritton 						if (ip4[ii].s_addr ==
13460304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
13470304c731SJamie Gritton 							break;
13480304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
13490304c731SJamie Gritton 						break;
13500304c731SJamie Gritton 				}
13510304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
13520304c731SJamie Gritton 					error = EPERM;
13530304c731SJamie Gritton 					goto done_deref_locked;
13540304c731SJamie Gritton 				}
13550304c731SJamie Gritton 			}
13560304c731SJamie Gritton 		}
13570304c731SJamie Gritton 		/*
13580304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
13590304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
13600304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
13610304c731SJamie Gritton 		 * IP stop checking and return error.
13620304c731SJamie Gritton 		 */
1363bdfc8cc4SJamie Gritton 		tppr = ppr;
1364bdfc8cc4SJamie Gritton #ifdef VIMAGE
1365bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1366bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1367bdfc8cc4SJamie Gritton 				break;
1368bdfc8cc4SJamie Gritton #endif
1369bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1370bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1371bdfc8cc4SJamie Gritton #ifdef VIMAGE
13722b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1373bdfc8cc4SJamie Gritton #endif
1374bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
13750304c731SJamie Gritton 				descend = 0;
13760304c731SJamie Gritton 				continue;
13770304c731SJamie Gritton 			}
13780304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
13790304c731SJamie Gritton 				continue;
13800304c731SJamie Gritton 			descend = 0;
13810304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
13820304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
13830304c731SJamie Gritton 				continue;
13840304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
13852b0d6f81SJamie Gritton 				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
13860304c731SJamie Gritton 					error = EADDRINUSE;
1387b38ff370SJamie Gritton 					vfs_opterror(opts,
1388b38ff370SJamie Gritton 					    "IPv4 addresses clash");
1389b38ff370SJamie Gritton 					goto done_deref_locked;
1390b38ff370SJamie Gritton 				}
13910304c731SJamie Gritton 			}
13920304c731SJamie Gritton 		}
13930304c731SJamie Gritton 	}
1394b38ff370SJamie Gritton #endif
1395b38ff370SJamie Gritton #ifdef INET6
13962b0d6f81SJamie Gritton 	if (ip6s > 0) {
13970304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
13980304c731SJamie Gritton 			/*
13990304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14000304c731SJamie Gritton 			 * subset of the parent's list.
14010304c731SJamie Gritton 			 */
14020304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
14030304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14040304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
14050304c731SJamie Gritton 					break;
14060304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
14070304c731SJamie Gritton 				error = EPERM;
14080304c731SJamie Gritton 				goto done_deref_locked;
14090304c731SJamie Gritton 			}
14100304c731SJamie Gritton 			if (ip6s > 1) {
14110304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
14120304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14130304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
14140304c731SJamie Gritton 						continue;
14150304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
14160304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
14172b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
14180304c731SJamie Gritton 							break;
14190304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
14200304c731SJamie Gritton 						break;
14210304c731SJamie Gritton 				}
14220304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14230304c731SJamie Gritton 					error = EPERM;
14240304c731SJamie Gritton 					goto done_deref_locked;
14250304c731SJamie Gritton 				}
14260304c731SJamie Gritton 			}
14270304c731SJamie Gritton 		}
14280304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1429bdfc8cc4SJamie Gritton 		tppr = ppr;
1430bdfc8cc4SJamie Gritton #ifdef VIMAGE
1431bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1432bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1433bdfc8cc4SJamie Gritton 				break;
1434bdfc8cc4SJamie Gritton #endif
1435bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1436bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1437bdfc8cc4SJamie Gritton #ifdef VIMAGE
14382b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1439bdfc8cc4SJamie Gritton #endif
1440bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14410304c731SJamie Gritton 				descend = 0;
14420304c731SJamie Gritton 				continue;
14430304c731SJamie Gritton 			}
14440304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
14450304c731SJamie Gritton 				continue;
14460304c731SJamie Gritton 			descend = 0;
14470304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
14480304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
14490304c731SJamie Gritton 				continue;
14500304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
14512b0d6f81SJamie Gritton 				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
14520304c731SJamie Gritton 					error = EADDRINUSE;
1453b38ff370SJamie Gritton 					vfs_opterror(opts,
1454b38ff370SJamie Gritton 					    "IPv6 addresses clash");
1455b38ff370SJamie Gritton 					goto done_deref_locked;
1456b38ff370SJamie Gritton 				}
14570304c731SJamie Gritton 			}
14580304c731SJamie Gritton 		}
14590304c731SJamie Gritton 	}
1460b38ff370SJamie Gritton #endif
14610304c731SJamie Gritton 	onamelen = namelen = 0;
14620304c731SJamie Gritton 	if (name != NULL) {
1463b38ff370SJamie Gritton 		/* Give a default name of the jid. */
1464b38ff370SJamie Gritton 		if (name[0] == '\0')
1465b38ff370SJamie Gritton 			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1466babbbb9cSJamie Gritton 		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1467babbbb9cSJamie Gritton 		    *p == '\0')) {
1468b38ff370SJamie Gritton 			error = EINVAL;
1469babbbb9cSJamie Gritton 			vfs_opterror(opts,
1470babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
14710304c731SJamie Gritton 			goto done_deref_locked;
1472b38ff370SJamie Gritton 		}
1473b38ff370SJamie Gritton 		/*
14740304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
14750304c731SJamie Gritton 		 * children.
1476b38ff370SJamie Gritton 		 */
14770304c731SJamie Gritton 		onamelen = strlen(pr->pr_name);
14780304c731SJamie Gritton 		namelen = strlen(name);
14790304c731SJamie Gritton 		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
14800304c731SJamie Gritton 			error = ENAMETOOLONG;
14810304c731SJamie Gritton 			goto done_deref_locked;
14820304c731SJamie Gritton 		}
14830304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
14840304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
14850304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
14860304c731SJamie Gritton 				error = ENAMETOOLONG;
14870304c731SJamie Gritton 				goto done_deref_locked;
14880304c731SJamie Gritton 			}
14890304c731SJamie Gritton 		}
14900304c731SJamie Gritton 	}
14910304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
14920304c731SJamie Gritton 		error = EPERM;
14930304c731SJamie Gritton 		goto done_deref_locked;
1494b38ff370SJamie Gritton 	}
1495b38ff370SJamie Gritton 
1496b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1497b38ff370SJamie Gritton #ifdef INET
14980304c731SJamie Gritton 	redo_ip4 = 0;
14990304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
15000304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1501b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
15020304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1503b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1504b38ff370SJamie Gritton 		ip4 = NULL;
15050304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1506bdfc8cc4SJamie Gritton #ifdef VIMAGE
1507bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1508bdfc8cc4SJamie Gritton 				descend = 0;
1509bdfc8cc4SJamie Gritton 				continue;
1510bdfc8cc4SJamie Gritton 			}
1511bdfc8cc4SJamie Gritton #endif
15120304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
15130304c731SJamie Gritton 				redo_ip4 = 1;
15140304c731SJamie Gritton 				descend = 0;
15150304c731SJamie Gritton 			}
15160304c731SJamie Gritton 		}
15170304c731SJamie Gritton 	}
1518b38ff370SJamie Gritton #endif
1519b38ff370SJamie Gritton #ifdef INET6
15200304c731SJamie Gritton 	redo_ip6 = 0;
15210304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
15220304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1523b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
15240304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1525b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1526b38ff370SJamie Gritton 		ip6 = NULL;
15270304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1528bdfc8cc4SJamie Gritton #ifdef VIMAGE
1529bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1530bdfc8cc4SJamie Gritton 				descend = 0;
1531bdfc8cc4SJamie Gritton 				continue;
1532bdfc8cc4SJamie Gritton 			}
1533bdfc8cc4SJamie Gritton #endif
15340304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
15350304c731SJamie Gritton 				redo_ip6 = 1;
15360304c731SJamie Gritton 				descend = 0;
15370304c731SJamie Gritton 			}
15380304c731SJamie Gritton 		}
15390304c731SJamie Gritton 	}
1540b38ff370SJamie Gritton #endif
15410304c731SJamie Gritton 	if (gotslevel) {
1542b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
15430304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
15440304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15450304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
15460304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
15470304c731SJamie Gritton 	}
1548b97457e2SJamie Gritton 	if (gotchildmax) {
1549b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1550b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1551b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1552b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1553b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1554b97457e2SJamie Gritton 				    ? childmax - level : 0;
1555b97457e2SJamie Gritton 	}
15560304c731SJamie Gritton 	if (gotenforce) {
15570304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
15580304c731SJamie Gritton 		/* Pass this restriction on to the children. */
15590304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
15600304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
15610304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
15620304c731SJamie Gritton 	}
15630304c731SJamie Gritton 	if (name != NULL) {
15640304c731SJamie Gritton 		if (ppr == &prison0)
1565b38ff370SJamie Gritton 			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
15660304c731SJamie Gritton 		else
15670304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
15680304c731SJamie Gritton 			    ppr->pr_name, name);
15690304c731SJamie Gritton 		/* Change this component of child names. */
15700304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
15710304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
15720304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
15730304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
15740304c731SJamie Gritton 		}
15750304c731SJamie Gritton 	}
1576b38ff370SJamie Gritton 	if (path != NULL) {
15770304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
15780304c731SJamie Gritton 		if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
15790304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
15800304c731SJamie Gritton 			    mypr->pr_path, path);
15810304c731SJamie Gritton 		else
1582b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1583b38ff370SJamie Gritton 		pr->pr_root = root;
1584b38ff370SJamie Gritton 	}
158576ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
158676ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
158776ca6f88SJamie Gritton 			/*
158876ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
158976ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
159076ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
159176ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
159276ca6f88SJamie Gritton 			 */
1593c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1594c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1595c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1596c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1597c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1598c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
159976ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
160076ca6f88SJamie Gritton 		}
160176ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
160276ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1603b38ff370SJamie Gritton 		if (host != NULL)
1604c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
160576ca6f88SJamie Gritton 		if (domain != NULL)
1606c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1607c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
160876ca6f88SJamie Gritton 		if (uuid != NULL)
1609c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
161076ca6f88SJamie Gritton 		if (gothid)
161176ca6f88SJamie Gritton 			pr->pr_hostid = hid;
161276ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
161376ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
161476ca6f88SJamie Gritton 				descend = 0;
161576ca6f88SJamie Gritton 			else {
161676ca6f88SJamie Gritton 				if (host != NULL)
1617c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1618c1f19219SJamie Gritton 					    pr->pr_hostname,
1619c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
162076ca6f88SJamie Gritton 				if (domain != NULL)
1621c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1622c1f19219SJamie Gritton 					    pr->pr_domainname,
1623c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
162476ca6f88SJamie Gritton 				if (uuid != NULL)
1625c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1626c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1627c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
162876ca6f88SJamie Gritton 				if (gothid)
162976ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
163076ca6f88SJamie Gritton 			}
163176ca6f88SJamie Gritton 		}
163276ca6f88SJamie Gritton 	}
16330304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
16340304c731SJamie Gritton 		/* Clear allow bits in all children. */
16350304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16360304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
16370304c731SJamie Gritton 	}
16380304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1639b38ff370SJamie Gritton 	/*
1640b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1641b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1642b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1643b38ff370SJamie Gritton 	 * handlers have completed.
1644b38ff370SJamie Gritton 	 */
1645b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1646b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1647b38ff370SJamie Gritton 			pr->pr_ref++;
1648b38ff370SJamie Gritton 			pr->pr_uref++;
1649b38ff370SJamie Gritton 		} else {
1650b38ff370SJamie Gritton 			pr->pr_ref--;
1651b38ff370SJamie Gritton 			pr->pr_uref--;
1652b38ff370SJamie Gritton 		}
1653b38ff370SJamie Gritton 	}
1654b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1655b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1656b38ff370SJamie Gritton 
16570304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
16580304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
16590304c731SJamie Gritton 	 */
16600304c731SJamie Gritton #ifdef INET
16610304c731SJamie Gritton 	while (redo_ip4) {
16620304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
16630304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
16640304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16650304c731SJamie Gritton 		redo_ip4 = 0;
16660304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1667bdfc8cc4SJamie Gritton #ifdef VIMAGE
1668bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1669bdfc8cc4SJamie Gritton 				descend = 0;
1670bdfc8cc4SJamie Gritton 				continue;
1671bdfc8cc4SJamie Gritton 			}
1672bdfc8cc4SJamie Gritton #endif
16730304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
16740304c731SJamie Gritton 				if (ip4 != NULL)
16750304c731SJamie Gritton 					ip4 = NULL;
16760304c731SJamie Gritton 				else
16770304c731SJamie Gritton 					redo_ip4 = 1;
16780304c731SJamie Gritton 			}
16790304c731SJamie Gritton 		}
16800304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
16810304c731SJamie Gritton 	}
16820304c731SJamie Gritton #endif
16830304c731SJamie Gritton #ifdef INET6
16840304c731SJamie Gritton 	while (redo_ip6) {
16850304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
16860304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
16870304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
16880304c731SJamie Gritton 		redo_ip6 = 0;
16890304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1690bdfc8cc4SJamie Gritton #ifdef VIMAGE
1691bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1692bdfc8cc4SJamie Gritton 				descend = 0;
1693bdfc8cc4SJamie Gritton 				continue;
1694bdfc8cc4SJamie Gritton 			}
1695bdfc8cc4SJamie Gritton #endif
16960304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
16970304c731SJamie Gritton 				if (ip6 != NULL)
16980304c731SJamie Gritton 					ip6 = NULL;
16990304c731SJamie Gritton 				else
17000304c731SJamie Gritton 					redo_ip6 = 1;
17010304c731SJamie Gritton 			}
17020304c731SJamie Gritton 		}
17030304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17040304c731SJamie Gritton 	}
17050304c731SJamie Gritton #endif
17060304c731SJamie Gritton 
1707b38ff370SJamie Gritton 	/* Let the modules do their work. */
1708b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1709b38ff370SJamie Gritton 	if (created) {
1710b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1711b38ff370SJamie Gritton 		if (error) {
1712b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_SLOCKED);
1713b38ff370SJamie Gritton 			goto done_errmsg;
1714b38ff370SJamie Gritton 		}
1715b38ff370SJamie Gritton 	}
1716b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1717b38ff370SJamie Gritton 	if (error) {
1718b38ff370SJamie Gritton 		prison_deref(pr, created
1719b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1720b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1721b38ff370SJamie Gritton 		goto done_errmsg;
1722b38ff370SJamie Gritton 	}
1723b38ff370SJamie Gritton 
1724b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1725b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1726b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1727b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1728b38ff370SJamie Gritton 		if (error) {
1729b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1730b38ff370SJamie Gritton 			if (!created)
1731b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1732b38ff370SJamie Gritton 			goto done_errmsg;
1733b38ff370SJamie Gritton 		}
1734b38ff370SJamie Gritton 	}
1735b38ff370SJamie Gritton 
1736b38ff370SJamie Gritton 	/*
1737b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1738b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1739b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1740b38ff370SJamie Gritton 	 * visible).
1741b38ff370SJamie Gritton 	 */
1742b38ff370SJamie Gritton 	if (!created) {
1743b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1744b38ff370SJamie Gritton 		    ? PD_DEREF
1745b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1746b38ff370SJamie Gritton 	} else {
1747b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1748b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1749b38ff370SJamie Gritton 			pr->pr_ref++;
1750b38ff370SJamie Gritton 			pr->pr_uref++;
1751b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1752b38ff370SJamie Gritton 		}
1753b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1754b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1755b38ff370SJamie Gritton 	}
1756b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
1757b38ff370SJamie Gritton 	goto done_errmsg;
1758b38ff370SJamie Gritton 
17590304c731SJamie Gritton  done_deref_locked:
17600304c731SJamie Gritton 	prison_deref(pr, created
17610304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
17620304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
17630304c731SJamie Gritton 	goto done_releroot;
1764b38ff370SJamie Gritton  done_unlock_list:
1765b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1766b38ff370SJamie Gritton  done_releroot:
1767b38ff370SJamie Gritton 	if (root != NULL) {
1768b38ff370SJamie Gritton 		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1769b38ff370SJamie Gritton 		vrele(root);
1770b38ff370SJamie Gritton 		VFS_UNLOCK_GIANT(vfslocked);
1771b38ff370SJamie Gritton 	}
1772b38ff370SJamie Gritton  done_errmsg:
1773b38ff370SJamie Gritton 	if (error) {
1774b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1775b38ff370SJamie Gritton 		if (errmsg_len > 0) {
1776b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1777b38ff370SJamie Gritton 			if (errmsg_pos > 0) {
1778b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE)
1779b38ff370SJamie Gritton 					bcopy(errmsg,
1780b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1781b38ff370SJamie Gritton 					   errmsg_len);
1782b38ff370SJamie Gritton 				else
1783b38ff370SJamie Gritton 					copyout(errmsg,
1784b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1785b38ff370SJamie Gritton 					   errmsg_len);
1786b38ff370SJamie Gritton 			}
1787b38ff370SJamie Gritton 		}
1788b38ff370SJamie Gritton 	}
1789b38ff370SJamie Gritton  done_free:
1790b38ff370SJamie Gritton #ifdef INET
1791b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1792b38ff370SJamie Gritton #endif
1793b38ff370SJamie Gritton #ifdef INET6
1794b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1795b38ff370SJamie Gritton #endif
1796b38ff370SJamie Gritton 	vfs_freeopts(opts);
1797b38ff370SJamie Gritton 	return (error);
1798b38ff370SJamie Gritton }
1799b38ff370SJamie Gritton 
1800b38ff370SJamie Gritton 
1801b38ff370SJamie Gritton /*
1802b38ff370SJamie Gritton  * struct jail_get_args {
1803b38ff370SJamie Gritton  *	struct iovec *iovp;
1804b38ff370SJamie Gritton  *	unsigned int iovcnt;
1805b38ff370SJamie Gritton  *	int flags;
1806b38ff370SJamie Gritton  * };
1807b38ff370SJamie Gritton  */
1808b38ff370SJamie Gritton int
1809b38ff370SJamie Gritton jail_get(struct thread *td, struct jail_get_args *uap)
1810b38ff370SJamie Gritton {
1811b38ff370SJamie Gritton 	struct uio *auio;
1812b38ff370SJamie Gritton 	int error;
1813b38ff370SJamie Gritton 
1814b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1815b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1816b38ff370SJamie Gritton 		return (EINVAL);
1817b38ff370SJamie Gritton 
1818b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1819b38ff370SJamie Gritton 	if (error)
1820b38ff370SJamie Gritton 		return (error);
1821b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1822b38ff370SJamie Gritton 	if (error == 0)
1823b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1824b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1825b38ff370SJamie Gritton 	free(auio, M_IOV);
1826b38ff370SJamie Gritton 	return (error);
1827b38ff370SJamie Gritton }
1828b38ff370SJamie Gritton 
1829b38ff370SJamie Gritton int
1830b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1831b38ff370SJamie Gritton {
18320304c731SJamie Gritton 	struct prison *pr, *mypr;
1833b38ff370SJamie Gritton 	struct vfsopt *opt;
1834b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1835b38ff370SJamie Gritton 	char *errmsg, *name;
18360304c731SJamie Gritton 	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1837b38ff370SJamie Gritton 
1838b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1839b38ff370SJamie Gritton 		return (EINVAL);
1840b38ff370SJamie Gritton 
1841b38ff370SJamie Gritton 	/* Get the parameter list. */
1842b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1843b38ff370SJamie Gritton 	if (error)
1844b38ff370SJamie Gritton 		return (error);
1845b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
18460304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
18471e2a13e6SJamie Gritton 
1848b38ff370SJamie Gritton 	/*
1849b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
1850b38ff370SJamie Gritton 	 */
1851b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
1852b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1853b38ff370SJamie Gritton 	if (error == 0) {
1854b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
18550304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1856b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
1857b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
1858b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1859b38ff370SJamie Gritton 					break;
1860b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1861b38ff370SJamie Gritton 			}
1862b38ff370SJamie Gritton 		}
1863b38ff370SJamie Gritton 		if (pr != NULL)
1864b38ff370SJamie Gritton 			goto found_prison;
1865b38ff370SJamie Gritton 		error = ENOENT;
1866b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
1867b38ff370SJamie Gritton 		goto done_unlock_list;
1868b38ff370SJamie Gritton 	} else if (error != ENOENT)
1869b38ff370SJamie Gritton 		goto done_unlock_list;
1870b38ff370SJamie Gritton 
1871b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1872b38ff370SJamie Gritton 	if (error == 0) {
1873b38ff370SJamie Gritton 		if (jid != 0) {
18740304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
1875b38ff370SJamie Gritton 			if (pr != NULL) {
1876b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1877b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1878b38ff370SJamie Gritton 					error = ENOENT;
1879b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1880b38ff370SJamie Gritton 					    jid);
1881b38ff370SJamie Gritton 					goto done_unlock_list;
1882b38ff370SJamie Gritton 				}
1883b38ff370SJamie Gritton 				goto found_prison;
1884b38ff370SJamie Gritton 			}
1885b38ff370SJamie Gritton 			error = ENOENT;
1886b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
1887b38ff370SJamie Gritton 			goto done_unlock_list;
1888b38ff370SJamie Gritton 		}
1889b38ff370SJamie Gritton 	} else if (error != ENOENT)
1890b38ff370SJamie Gritton 		goto done_unlock_list;
1891b38ff370SJamie Gritton 
1892b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1893b38ff370SJamie Gritton 	if (error == 0) {
1894b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1895b38ff370SJamie Gritton 			error = EINVAL;
1896b38ff370SJamie Gritton 			goto done_unlock_list;
1897b38ff370SJamie Gritton 		}
18980304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
1899b38ff370SJamie Gritton 		if (pr != NULL) {
1900b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1901b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1902b38ff370SJamie Gritton 				error = ENOENT;
1903b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
1904b38ff370SJamie Gritton 				    name);
1905b38ff370SJamie Gritton 				goto done_unlock_list;
1906b38ff370SJamie Gritton 			}
1907b38ff370SJamie Gritton 			goto found_prison;
1908b38ff370SJamie Gritton 		}
1909b38ff370SJamie Gritton 		error = ENOENT;
1910b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
1911b38ff370SJamie Gritton 		goto done_unlock_list;
1912b38ff370SJamie Gritton 	} else if (error != ENOENT)
1913b38ff370SJamie Gritton 		goto done_unlock_list;
1914b38ff370SJamie Gritton 
1915b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
1916b38ff370SJamie Gritton 	error = ENOENT;
1917b38ff370SJamie Gritton 	goto done_unlock_list;
1918b38ff370SJamie Gritton 
1919b38ff370SJamie Gritton  found_prison:
1920b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
1921b38ff370SJamie Gritton 	pr->pr_ref++;
1922b38ff370SJamie Gritton 	locked = PD_LOCKED;
1923b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
1924b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1925b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1926b38ff370SJamie Gritton 		goto done_deref;
19270304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
19280304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1929b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1930b38ff370SJamie Gritton 		goto done_deref;
19310304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
19320304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
19330304c731SJamie Gritton 		goto done_deref;
19340304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1935b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
1936b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1937b38ff370SJamie Gritton 		goto done_deref;
19380304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1939b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1940b38ff370SJamie Gritton 		goto done_deref;
1941b38ff370SJamie Gritton #ifdef INET
1942b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1943b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1944b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1945b38ff370SJamie Gritton 		goto done_deref;
1946b38ff370SJamie Gritton #endif
1947b38ff370SJamie Gritton #ifdef INET6
1948b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1949b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1950b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1951b38ff370SJamie Gritton 		goto done_deref;
1952b38ff370SJamie Gritton #endif
1953b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1954b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
1955b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1956b38ff370SJamie Gritton 		goto done_deref;
1957b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1958b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
1959b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
1960b97457e2SJamie Gritton 		goto done_deref;
1961b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1962b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
1963b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
1964b97457e2SJamie Gritton 		goto done_deref;
1965c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1966b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
1967b38ff370SJamie Gritton 		goto done_deref;
1968c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
196976ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
197076ca6f88SJamie Gritton 		goto done_deref;
1971c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
197276ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
197376ca6f88SJamie Gritton 		goto done_deref;
1974841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
1975a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
197676ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
197776ca6f88SJamie Gritton 
197876ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
197976ca6f88SJamie Gritton 	} else
198076ca6f88SJamie Gritton #endif
198176ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
198276ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
198376ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
198476ca6f88SJamie Gritton 		goto done_deref;
19850304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
19860304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
19870304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
19880304c731SJamie Gritton 		goto done_deref;
19890304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
19900304c731SJamie Gritton 	    fi++) {
19910304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
19920304c731SJamie Gritton 			continue;
19930304c731SJamie Gritton 		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
19940304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1995b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
1996b38ff370SJamie Gritton 			goto done_deref;
1997b38ff370SJamie Gritton 		i = !i;
19980304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1999b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2000b38ff370SJamie Gritton 			goto done_deref;
20010304c731SJamie Gritton 	}
20027cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
20037cbf7213SJamie Gritton 	    fi++) {
20047cbf7213SJamie Gritton 		i = pr->pr_flags &
20057cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
20067cbf7213SJamie Gritton 		i = pr_flag_jailsys[fi].disable &&
20077cbf7213SJamie Gritton 		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
20087cbf7213SJamie Gritton 		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
20097cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
20107cbf7213SJamie Gritton 		error =
20117cbf7213SJamie Gritton 		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
20127cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
20137cbf7213SJamie Gritton 			goto done_deref;
20147cbf7213SJamie Gritton 	}
20150304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
20160304c731SJamie Gritton 	    fi++) {
20170304c731SJamie Gritton 		if (pr_allow_names[fi] == NULL)
20180304c731SJamie Gritton 			continue;
20190304c731SJamie Gritton 		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
20200304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
20210304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
20220304c731SJamie Gritton 			goto done_deref;
20230304c731SJamie Gritton 		i = !i;
20240304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
20250304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
20260304c731SJamie Gritton 			goto done_deref;
20270304c731SJamie Gritton 	}
2028b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
2029b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2030b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2031b38ff370SJamie Gritton 		goto done_deref;
2032b38ff370SJamie Gritton 	i = !i;
2033b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2034b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2035b38ff370SJamie Gritton 		goto done_deref;
2036b38ff370SJamie Gritton 
2037b38ff370SJamie Gritton 	/* Get the module parameters. */
2038b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2039b38ff370SJamie Gritton 	locked = 0;
2040b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2041b38ff370SJamie Gritton 	if (error)
2042b38ff370SJamie Gritton 		goto done_deref;
2043b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2044b38ff370SJamie Gritton 
2045b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2046b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2047b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2048b38ff370SJamie Gritton 			error = EINVAL;
2049b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2050b38ff370SJamie Gritton 			goto done_errmsg;
2051b38ff370SJamie Gritton 		}
2052b38ff370SJamie Gritton 	}
2053b38ff370SJamie Gritton 
2054b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2055b38ff370SJamie Gritton 	error = 0;
2056b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2057b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2058b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2059b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2060b38ff370SJamie Gritton 			if (opt->value != NULL) {
2061b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2062b38ff370SJamie Gritton 					bcopy(opt->value,
2063b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2064b38ff370SJamie Gritton 					    opt->len);
2065b38ff370SJamie Gritton 				} else {
2066b38ff370SJamie Gritton 					error = copyout(opt->value,
2067b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2068b38ff370SJamie Gritton 					    opt->len);
2069b38ff370SJamie Gritton 					if (error)
2070b38ff370SJamie Gritton 						break;
2071b38ff370SJamie Gritton 				}
2072b38ff370SJamie Gritton 			}
2073b38ff370SJamie Gritton 		}
2074b38ff370SJamie Gritton 	}
2075b38ff370SJamie Gritton 	goto done_errmsg;
2076b38ff370SJamie Gritton 
2077b38ff370SJamie Gritton  done_deref:
2078b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2079b38ff370SJamie Gritton 	goto done_errmsg;
2080b38ff370SJamie Gritton 
2081b38ff370SJamie Gritton  done_unlock_list:
2082b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2083b38ff370SJamie Gritton  done_errmsg:
2084b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2085b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2086b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2087b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2088b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2089b38ff370SJamie Gritton 				bcopy(errmsg,
2090b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2091b38ff370SJamie Gritton 				    errmsg_len);
2092b38ff370SJamie Gritton 			else
2093b38ff370SJamie Gritton 				copyout(errmsg,
2094b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2095b38ff370SJamie Gritton 				    errmsg_len);
2096b38ff370SJamie Gritton 		}
2097b38ff370SJamie Gritton 	}
2098b38ff370SJamie Gritton 	vfs_freeopts(opts);
2099b38ff370SJamie Gritton 	return (error);
2100b38ff370SJamie Gritton }
2101b38ff370SJamie Gritton 
21020304c731SJamie Gritton 
2103b38ff370SJamie Gritton /*
2104b38ff370SJamie Gritton  * struct jail_remove_args {
2105b38ff370SJamie Gritton  *	int jid;
2106b38ff370SJamie Gritton  * };
2107b38ff370SJamie Gritton  */
2108b38ff370SJamie Gritton int
2109b38ff370SJamie Gritton jail_remove(struct thread *td, struct jail_remove_args *uap)
2110b38ff370SJamie Gritton {
21110304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
21120304c731SJamie Gritton 	int descend, error;
2113b38ff370SJamie Gritton 
2114b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2115b38ff370SJamie Gritton 	if (error)
2116b38ff370SJamie Gritton 		return (error);
2117b38ff370SJamie Gritton 
2118b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
21190304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2120b38ff370SJamie Gritton 	if (pr == NULL) {
2121b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2122b38ff370SJamie Gritton 		return (EINVAL);
2123b38ff370SJamie Gritton 	}
2124b38ff370SJamie Gritton 
21250304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
21260304c731SJamie Gritton 	pr->pr_ref++;
21270304c731SJamie Gritton 	pr->pr_flags |= PR_REMOVE;
21280304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
21290304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
21300304c731SJamie Gritton 		lpr = NULL;
21310304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
21320304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
21330304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
21340304c731SJamie Gritton 				tpr = cpr;
21350304c731SJamie Gritton 				cpr->pr_ref++;
21360304c731SJamie Gritton 				cpr->pr_flags |= PR_REMOVE;
21370304c731SJamie Gritton 			} else {
21380304c731SJamie Gritton 				/* Already removed - do not do it again. */
21390304c731SJamie Gritton 				tpr = NULL;
21400304c731SJamie Gritton 			}
21410304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
21420304c731SJamie Gritton 			if (lpr != NULL) {
21430304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
21440304c731SJamie Gritton 				prison_remove_one(lpr);
21450304c731SJamie Gritton 				sx_xlock(&allprison_lock);
21460304c731SJamie Gritton 			}
21470304c731SJamie Gritton 			lpr = tpr;
21480304c731SJamie Gritton 		}
21490304c731SJamie Gritton 		if (lpr != NULL) {
21500304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
21510304c731SJamie Gritton 			prison_remove_one(lpr);
21520304c731SJamie Gritton 			sx_xlock(&allprison_lock);
21530304c731SJamie Gritton 		}
21540304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
21550304c731SJamie Gritton 	}
21560304c731SJamie Gritton 	prison_remove_one(pr);
21570304c731SJamie Gritton 	return (0);
21580304c731SJamie Gritton }
21590304c731SJamie Gritton 
21600304c731SJamie Gritton static void
21610304c731SJamie Gritton prison_remove_one(struct prison *pr)
21620304c731SJamie Gritton {
21630304c731SJamie Gritton 	struct proc *p;
21640304c731SJamie Gritton 	int deuref;
21650304c731SJamie Gritton 
2166b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2167b38ff370SJamie Gritton 	deuref = 0;
2168b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2169b38ff370SJamie Gritton 		pr->pr_ref--;
2170b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2171b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2172b38ff370SJamie Gritton 	}
2173b38ff370SJamie Gritton 
21740304c731SJamie Gritton 	/*
21750304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
21760304c731SJamie Gritton 	 * the prison now.
21770304c731SJamie Gritton 	 */
21780304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
21790304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
21800304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2181b38ff370SJamie Gritton 		prison_deref(pr,
2182b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
21830304c731SJamie Gritton 		return;
2184b38ff370SJamie Gritton 	}
2185b38ff370SJamie Gritton 
2186b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2187b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2188b38ff370SJamie Gritton 	/*
2189b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2190b38ff370SJamie Gritton 	 */
2191b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
2192b38ff370SJamie Gritton 	LIST_FOREACH(p, &allproc, p_list) {
2193b38ff370SJamie Gritton 		PROC_LOCK(p);
2194b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2195b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
2196b38ff370SJamie Gritton 			psignal(p, SIGKILL);
2197b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2198b38ff370SJamie Gritton 	}
2199b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
22000304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2201b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
220275c13541SPoul-Henning Kamp }
220375c13541SPoul-Henning Kamp 
22048571af59SJamie Gritton 
2205fd7a8150SMike Barcroft /*
22069ddb7954SMike Barcroft  * struct jail_attach_args {
22079ddb7954SMike Barcroft  *	int jid;
22089ddb7954SMike Barcroft  * };
2209fd7a8150SMike Barcroft  */
2210fd7a8150SMike Barcroft int
22119ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap)
2212fd7a8150SMike Barcroft {
2213b38ff370SJamie Gritton 	struct prison *pr;
2214b38ff370SJamie Gritton 	int error;
2215b38ff370SJamie Gritton 
2216b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2217b38ff370SJamie Gritton 	if (error)
2218b38ff370SJamie Gritton 		return (error);
2219b38ff370SJamie Gritton 
2220b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
22210304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2222b38ff370SJamie Gritton 	if (pr == NULL) {
2223b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2224b38ff370SJamie Gritton 		return (EINVAL);
2225b38ff370SJamie Gritton 	}
2226b38ff370SJamie Gritton 
2227b38ff370SJamie Gritton 	/*
2228b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2229b38ff370SJamie Gritton 	 * considered to be "alive".
2230b38ff370SJamie Gritton 	 */
2231b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2232b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2233b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2234b38ff370SJamie Gritton 		return (EINVAL);
2235b38ff370SJamie Gritton 	}
2236b38ff370SJamie Gritton 
2237b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2238b38ff370SJamie Gritton }
2239b38ff370SJamie Gritton 
2240b38ff370SJamie Gritton static int
2241b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2242b38ff370SJamie Gritton {
22430304c731SJamie Gritton 	struct prison *ppr;
2244fd7a8150SMike Barcroft 	struct proc *p;
2245fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
2246453f7d53SChristian S.J. Peron 	int vfslocked, error;
2247fd7a8150SMike Barcroft 
224857f22bd4SJacques Vidrine 	/*
224957f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
225057f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
225157f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
225257f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
225357f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
225457f22bd4SJacques Vidrine 	 * of another.
225557f22bd4SJacques Vidrine 	 */
2256fd7a8150SMike Barcroft 	pr->pr_ref++;
2257b38ff370SJamie Gritton 	pr->pr_uref++;
2258fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2259b38ff370SJamie Gritton 
2260b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2261b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2262b38ff370SJamie Gritton 	if (error) {
2263b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2264b38ff370SJamie Gritton 		return (error);
2265b38ff370SJamie Gritton 	}
2266dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2267fd7a8150SMike Barcroft 
2268413628a7SBjoern A. Zeeb 	/*
2269413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2270413628a7SBjoern A. Zeeb 	 */
22710304c731SJamie Gritton 	ppr = td->td_ucred->cr_prison;
2272b38ff370SJamie Gritton 	p = td->td_proc;
2273413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2274413628a7SBjoern A. Zeeb 	if (error)
2275b38ff370SJamie Gritton 		goto e_revert_osd;
2276413628a7SBjoern A. Zeeb 
2277453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2278cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2279fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2280fd7a8150SMike Barcroft 		goto e_unlock;
2281fd7a8150SMike Barcroft #ifdef MAC
228230d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2283fd7a8150SMike Barcroft 		goto e_unlock;
2284fd7a8150SMike Barcroft #endif
228522db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2286b38ff370SJamie Gritton 	if ((error = change_root(pr->pr_root, td)))
2287b38ff370SJamie Gritton 		goto e_unlock_giant;
2288453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
2289fd7a8150SMike Barcroft 
2290fd7a8150SMike Barcroft 	newcred = crget();
2291fd7a8150SMike Barcroft 	PROC_LOCK(p);
2292fd7a8150SMike Barcroft 	oldcred = p->p_ucred;
2293fd7a8150SMike Barcroft 	setsugid(p);
2294fd7a8150SMike Barcroft 	crcopy(newcred, oldcred);
229569c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2296fd7a8150SMike Barcroft 	p->p_ucred = newcred;
2297fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
2298fd7a8150SMike Barcroft 	crfree(oldcred);
22990304c731SJamie Gritton 	prison_deref(ppr, PD_DEREF | PD_DEUREF);
2300fd7a8150SMike Barcroft 	return (0);
2301fd7a8150SMike Barcroft  e_unlock:
230222db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2303b38ff370SJamie Gritton  e_unlock_giant:
2304453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
2305b38ff370SJamie Gritton  e_revert_osd:
2306b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
23070304c731SJamie Gritton 	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2308b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2309fd7a8150SMike Barcroft 	return (error);
2310fd7a8150SMike Barcroft }
2311fd7a8150SMike Barcroft 
23120304c731SJamie Gritton 
2313fd7a8150SMike Barcroft /*
2314fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2315fd7a8150SMike Barcroft  */
231654b369c1SPawel Jakub Dawidek struct prison *
2317fd7a8150SMike Barcroft prison_find(int prid)
2318fd7a8150SMike Barcroft {
2319fd7a8150SMike Barcroft 	struct prison *pr;
2320fd7a8150SMike Barcroft 
2321dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2322b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2323fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2324fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2325b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2326fd7a8150SMike Barcroft 				return (pr);
2327b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2328fd7a8150SMike Barcroft 		}
2329fd7a8150SMike Barcroft 	}
2330fd7a8150SMike Barcroft 	return (NULL);
2331fd7a8150SMike Barcroft }
2332fd7a8150SMike Barcroft 
2333b38ff370SJamie Gritton /*
23340304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2335b38ff370SJamie Gritton  */
2336b38ff370SJamie Gritton struct prison *
23370304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2338b38ff370SJamie Gritton {
23390304c731SJamie Gritton 	struct prison *pr;
23400304c731SJamie Gritton 	int descend;
2341b38ff370SJamie Gritton 
2342b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
23430304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
23440304c731SJamie Gritton 		if (pr->pr_id == prid) {
23450304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
23460304c731SJamie Gritton 			if (pr->pr_ref > 0)
23470304c731SJamie Gritton 				return (pr);
23480304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
23490304c731SJamie Gritton 		}
23500304c731SJamie Gritton 	}
23510304c731SJamie Gritton 	return (NULL);
23520304c731SJamie Gritton }
23530304c731SJamie Gritton 
23540304c731SJamie Gritton /*
23550304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
23560304c731SJamie Gritton  */
23570304c731SJamie Gritton struct prison *
23580304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
23590304c731SJamie Gritton {
23600304c731SJamie Gritton 	struct prison *pr, *deadpr;
23610304c731SJamie Gritton 	size_t mylen;
23620304c731SJamie Gritton 	int descend;
23630304c731SJamie Gritton 
23640304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
23650304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2366b38ff370SJamie Gritton  again:
2367b38ff370SJamie Gritton 	deadpr = NULL;
23680304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
23690304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2370b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2371b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2372b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2373b38ff370SJamie Gritton 					return (pr);
2374b38ff370SJamie Gritton 				deadpr = pr;
2375b38ff370SJamie Gritton 			}
2376b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2377b38ff370SJamie Gritton 		}
2378b38ff370SJamie Gritton 	}
23790304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2380b38ff370SJamie Gritton 	if (deadpr != NULL) {
2381b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2382b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2383b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2384b38ff370SJamie Gritton 			goto again;
2385b38ff370SJamie Gritton 		}
2386b38ff370SJamie Gritton 	}
2387b38ff370SJamie Gritton 	return (deadpr);
2388b38ff370SJamie Gritton }
2389b38ff370SJamie Gritton 
2390b38ff370SJamie Gritton /*
23910304c731SJamie Gritton  * See if a prison has the specific flag set.
23920304c731SJamie Gritton  */
23930304c731SJamie Gritton int
23940304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
23950304c731SJamie Gritton {
23960304c731SJamie Gritton 
23970304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
23980304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
23990304c731SJamie Gritton }
24000304c731SJamie Gritton 
24010304c731SJamie Gritton int
24020304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
24030304c731SJamie Gritton {
24040304c731SJamie Gritton 
24050304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
24060304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
24070304c731SJamie Gritton }
24080304c731SJamie Gritton 
24090304c731SJamie Gritton /*
2410b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2411b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2412b38ff370SJamie Gritton  */
241391421ba2SRobert Watson void
24141ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
241591421ba2SRobert Watson {
241691421ba2SRobert Watson 
24171ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
241891421ba2SRobert Watson 	pr->pr_ref--;
241991421ba2SRobert Watson 	if (pr->pr_ref == 0) {
242001137630SRobert Watson 		mtx_unlock(&pr->pr_mtx);
2421c2cda609SPawel Jakub Dawidek 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2422c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2423c2cda609SPawel Jakub Dawidek 		return;
2424c2cda609SPawel Jakub Dawidek 	}
2425c2cda609SPawel Jakub Dawidek 	mtx_unlock(&pr->pr_mtx);
2426c2cda609SPawel Jakub Dawidek }
2427c2cda609SPawel Jakub Dawidek 
24281ba4a712SPawel Jakub Dawidek void
24291ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
24301ba4a712SPawel Jakub Dawidek {
24311ba4a712SPawel Jakub Dawidek 
24321ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
24331ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
24341ba4a712SPawel Jakub Dawidek }
24351ba4a712SPawel Jakub Dawidek 
2436c2cda609SPawel Jakub Dawidek static void
2437c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2438c2cda609SPawel Jakub Dawidek {
2439b38ff370SJamie Gritton 
2440b38ff370SJamie Gritton 	prison_deref((struct prison *)context, 0);
2441b38ff370SJamie Gritton }
2442b38ff370SJamie Gritton 
2443b38ff370SJamie Gritton /*
2444b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2445b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2446b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2447b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2448b38ff370SJamie Gritton  */
2449b38ff370SJamie Gritton static void
2450b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2451b38ff370SJamie Gritton {
24520304c731SJamie Gritton 	struct prison *ppr, *tpr;
2453c2cda609SPawel Jakub Dawidek 	int vfslocked;
2454c2cda609SPawel Jakub Dawidek 
2455b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2456b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
24570304c731SJamie Gritton 	/* Decrement the user references in a separate loop. */
2458b38ff370SJamie Gritton 	if (flags & PD_DEUREF) {
24590304c731SJamie Gritton 		for (tpr = pr;; tpr = tpr->pr_parent) {
24600304c731SJamie Gritton 			if (tpr != pr)
24610304c731SJamie Gritton 				mtx_lock(&tpr->pr_mtx);
24620304c731SJamie Gritton 			if (--tpr->pr_uref > 0)
24630304c731SJamie Gritton 				break;
24640304c731SJamie Gritton 			KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
24650304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
24660304c731SJamie Gritton 		}
2467b38ff370SJamie Gritton 		/* Done if there were only user references to remove. */
2468b38ff370SJamie Gritton 		if (!(flags & PD_DEREF)) {
24690304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
2470b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2471b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2472b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2473b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2474b38ff370SJamie Gritton 			return;
2475b38ff370SJamie Gritton 		}
24760304c731SJamie Gritton 		if (tpr != pr) {
24770304c731SJamie Gritton 			mtx_unlock(&tpr->pr_mtx);
24780304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2479b38ff370SJamie Gritton 		}
24800304c731SJamie Gritton 	}
24810304c731SJamie Gritton 
24820304c731SJamie Gritton 	for (;;) {
2483b38ff370SJamie Gritton 		if (flags & PD_DEREF)
2484b38ff370SJamie Gritton 			pr->pr_ref--;
2485b38ff370SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2486b38ff370SJamie Gritton 		if (pr->pr_ref > 0) {
2487b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2488b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2489b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2490b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2491b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2492b38ff370SJamie Gritton 			return;
2493b38ff370SJamie Gritton 		}
2494c2cda609SPawel Jakub Dawidek 
2495b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2496b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2497b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2498b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2499c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2500b38ff370SJamie Gritton 			}
2501b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2502b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2503b38ff370SJamie Gritton 
2504b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
25050304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
25060304c731SJamie Gritton 		ppr = pr->pr_parent;
25070304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2508b97457e2SJamie Gritton 			tpr->pr_childcount--;
2509c4884ffaSJamie Gritton 		sx_xunlock(&allprison_lock);
25101ba4a712SPawel Jakub Dawidek 
2511679e1390SJamie Gritton #ifdef VIMAGE
25120cb8b6a9SMarko Zec 		if (pr->pr_vnet != ppr->pr_vnet)
2513679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2514679e1390SJamie Gritton #endif
2515b38ff370SJamie Gritton 		if (pr->pr_root != NULL) {
2516453f7d53SChristian S.J. Peron 			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2517b3059e09SRobert Watson 			vrele(pr->pr_root);
2518453f7d53SChristian S.J. Peron 			VFS_UNLOCK_GIANT(vfslocked);
2519b38ff370SJamie Gritton 		}
2520b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2521413628a7SBjoern A. Zeeb #ifdef INET
2522413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2523413628a7SBjoern A. Zeeb #endif
2524b38ff370SJamie Gritton #ifdef INET6
2525b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2526b38ff370SJamie Gritton #endif
2527b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2528b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2529b38ff370SJamie Gritton 		osd_jail_exit(pr);
25301ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
25310304c731SJamie Gritton 
25320304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
25330304c731SJamie Gritton 		pr = ppr;
25340304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
2535c4884ffaSJamie Gritton 		flags = PD_DEREF;
25360304c731SJamie Gritton 	}
2537b3059e09SRobert Watson }
2538b3059e09SRobert Watson 
253991421ba2SRobert Watson void
25401ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
25411ba4a712SPawel Jakub Dawidek {
25421ba4a712SPawel Jakub Dawidek 
25431ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
25441ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
2545af7bd9a4SJamie Gritton 	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
25461ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
25471ba4a712SPawel Jakub Dawidek }
25481ba4a712SPawel Jakub Dawidek 
25491ba4a712SPawel Jakub Dawidek void
255091421ba2SRobert Watson prison_hold(struct prison *pr)
255191421ba2SRobert Watson {
255291421ba2SRobert Watson 
255301137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
25541ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
255501137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
255601137630SRobert Watson }
255701137630SRobert Watson 
2558413628a7SBjoern A. Zeeb void
2559413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
256001137630SRobert Watson {
256101137630SRobert Watson 
2562413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2563b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2564b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2565b38ff370SJamie Gritton 	pr->pr_uref++;
2566413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
256775c13541SPoul-Henning Kamp }
256875c13541SPoul-Henning Kamp 
256975c13541SPoul-Henning Kamp void
2570413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
257175c13541SPoul-Henning Kamp {
2572413628a7SBjoern A. Zeeb 
2573413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2574b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2575b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2576b38ff370SJamie Gritton 	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2577413628a7SBjoern A. Zeeb }
2578413628a7SBjoern A. Zeeb 
2579413628a7SBjoern A. Zeeb 
2580413628a7SBjoern A. Zeeb #ifdef INET
2581413628a7SBjoern A. Zeeb /*
25820304c731SJamie Gritton  * Restrict a prison's IP address list with its parent's, possibly replacing
25830304c731SJamie Gritton  * it.  Return true if the replacement buffer was used (or would have been).
25840304c731SJamie Gritton  */
25850304c731SJamie Gritton static int
25860304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
25870304c731SJamie Gritton {
25880304c731SJamie Gritton 	int ii, ij, used;
25890304c731SJamie Gritton 	struct prison *ppr;
25900304c731SJamie Gritton 
25910304c731SJamie Gritton 	ppr = pr->pr_parent;
25920304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4_USER)) {
25930304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
25940304c731SJamie Gritton 		if (pr->pr_ip4s < ppr->pr_ip4s) {
25950304c731SJamie Gritton 			/*
25960304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
25970304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
25980304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
25990304c731SJamie Gritton 			 * allocate one.
26000304c731SJamie Gritton 			 */
26010304c731SJamie Gritton 			used = 1;
26020304c731SJamie Gritton 			if (newip4 == NULL) {
26030304c731SJamie Gritton 				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
26040304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
26050304c731SJamie Gritton 				if (newip4 != NULL)
26060304c731SJamie Gritton 					used = 0;
26070304c731SJamie Gritton 			}
26080304c731SJamie Gritton 			if (newip4 != NULL) {
26090304c731SJamie Gritton 				bcopy(ppr->pr_ip4, newip4,
26100304c731SJamie Gritton 				    ppr->pr_ip4s * sizeof(*newip4));
26110304c731SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
26120304c731SJamie Gritton 				pr->pr_ip4 = newip4;
26130304c731SJamie Gritton 				pr->pr_ip4s = ppr->pr_ip4s;
26140304c731SJamie Gritton 			}
26150304c731SJamie Gritton 			return (used);
26160304c731SJamie Gritton 		}
26170304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
26180304c731SJamie Gritton 		if (pr->pr_ip4s > 0)
26190304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
26200304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*newip4));
26210304c731SJamie Gritton 		else if (pr->pr_ip4 != NULL) {
26220304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
26230304c731SJamie Gritton 			pr->pr_ip4 = NULL;
26240304c731SJamie Gritton 		}
26252b0d6f81SJamie Gritton 	} else if (pr->pr_ip4s > 0) {
26260304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
26270304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip4s; ij++)
26280304c731SJamie Gritton 			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
26290304c731SJamie Gritton 				break;
26300304c731SJamie Gritton 		if (ij < ppr->pr_ip4s)
26310304c731SJamie Gritton 			ii = 1;
26320304c731SJamie Gritton 		else {
26330304c731SJamie Gritton 			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
26340304c731SJamie Gritton 			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
26350304c731SJamie Gritton 			ii = 0;
26360304c731SJamie Gritton 		}
26370304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip4s; ) {
26380304c731SJamie Gritton 			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
26390304c731SJamie Gritton 				ii++;
26400304c731SJamie Gritton 				continue;
26410304c731SJamie Gritton 			}
26420304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
26430304c731SJamie Gritton 				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
26440304c731SJamie Gritton 			case -1:
26450304c731SJamie Gritton 				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
26460304c731SJamie Gritton 				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
26470304c731SJamie Gritton 				break;
26480304c731SJamie Gritton 			case 0:
26490304c731SJamie Gritton 				ii++;
26500304c731SJamie Gritton 				ij++;
26510304c731SJamie Gritton 				break;
26520304c731SJamie Gritton 			case 1:
26530304c731SJamie Gritton 				ij++;
26540304c731SJamie Gritton 				break;
26550304c731SJamie Gritton 			}
26560304c731SJamie Gritton 		}
26570304c731SJamie Gritton 		if (pr->pr_ip4s == 0) {
26587cbf7213SJamie Gritton 			pr->pr_flags |= PR_IP4_DISABLE;
26590304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
26600304c731SJamie Gritton 			pr->pr_ip4 = NULL;
26610304c731SJamie Gritton 		}
26620304c731SJamie Gritton 	}
26630304c731SJamie Gritton 	return (0);
26640304c731SJamie Gritton }
26650304c731SJamie Gritton 
26660304c731SJamie Gritton /*
2667413628a7SBjoern A. Zeeb  * Pass back primary IPv4 address of this jail.
2668413628a7SBjoern A. Zeeb  *
26690304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
26700304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. INADDR_ANY).
2671413628a7SBjoern A. Zeeb  *
2672b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2673b89e82ddSJamie Gritton  * Address returned in NBO.
2674413628a7SBjoern A. Zeeb  */
2675413628a7SBjoern A. Zeeb int
26761cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2677413628a7SBjoern A. Zeeb {
2678b38ff370SJamie Gritton 	struct prison *pr;
2679413628a7SBjoern A. Zeeb 
2680413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2681413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
268275c13541SPoul-Henning Kamp 
2683b38ff370SJamie Gritton 	pr = cred->cr_prison;
26840304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
26850304c731SJamie Gritton 		return (0);
2686b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
26870304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
26880304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
26890304c731SJamie Gritton 		return (0);
26900304c731SJamie Gritton 	}
2691b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2692b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2693b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2694b38ff370SJamie Gritton 	}
2695413628a7SBjoern A. Zeeb 
2696b38ff370SJamie Gritton 	ia->s_addr = pr->pr_ip4[0].s_addr;
2697b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2698413628a7SBjoern A. Zeeb 	return (0);
269975c13541SPoul-Henning Kamp }
2700413628a7SBjoern A. Zeeb 
2701413628a7SBjoern A. Zeeb /*
2702592bcae8SBjoern A. Zeeb  * Return 1 if we should do proper source address selection or are not jailed.
2703592bcae8SBjoern A. Zeeb  * We will return 0 if we should bypass source address selection in favour
2704592bcae8SBjoern A. Zeeb  * of the primary jail IPv4 address. Only in this case *ia will be updated and
2705592bcae8SBjoern A. Zeeb  * returned in NBO.
2706592bcae8SBjoern A. Zeeb  * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2707592bcae8SBjoern A. Zeeb  */
2708592bcae8SBjoern A. Zeeb int
2709592bcae8SBjoern A. Zeeb prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2710592bcae8SBjoern A. Zeeb {
2711592bcae8SBjoern A. Zeeb 	struct prison *pr;
2712592bcae8SBjoern A. Zeeb 	struct in_addr lia;
2713592bcae8SBjoern A. Zeeb 	int error;
2714592bcae8SBjoern A. Zeeb 
2715592bcae8SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2716592bcae8SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2717592bcae8SBjoern A. Zeeb 
2718592bcae8SBjoern A. Zeeb 	if (!jailed(cred))
2719592bcae8SBjoern A. Zeeb 		return (1);
2720592bcae8SBjoern A. Zeeb 
2721592bcae8SBjoern A. Zeeb 	pr = cred->cr_prison;
2722592bcae8SBjoern A. Zeeb 	if (pr->pr_flags & PR_IP4_SADDRSEL)
2723592bcae8SBjoern A. Zeeb 		return (1);
2724592bcae8SBjoern A. Zeeb 
2725592bcae8SBjoern A. Zeeb 	lia.s_addr = INADDR_ANY;
2726592bcae8SBjoern A. Zeeb 	error = prison_get_ip4(cred, &lia);
2727592bcae8SBjoern A. Zeeb 	if (error)
2728592bcae8SBjoern A. Zeeb 		return (error);
2729592bcae8SBjoern A. Zeeb 	if (lia.s_addr == INADDR_ANY)
2730592bcae8SBjoern A. Zeeb 		return (1);
2731592bcae8SBjoern A. Zeeb 
2732592bcae8SBjoern A. Zeeb 	ia->s_addr = lia.s_addr;
2733592bcae8SBjoern A. Zeeb 	return (0);
2734592bcae8SBjoern A. Zeeb }
2735592bcae8SBjoern A. Zeeb 
2736592bcae8SBjoern A. Zeeb /*
27370304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv4 address restrictions.
27380304c731SJamie Gritton  */
27390304c731SJamie Gritton int
27400304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2)
27410304c731SJamie Gritton {
27420304c731SJamie Gritton 
27430304c731SJamie Gritton 	if (pr1 == pr2)
27440304c731SJamie Gritton 		return (1);
27450304c731SJamie Gritton 
27460304c731SJamie Gritton 	/*
27472b0d6f81SJamie Gritton 	 * No need to lock since the PR_IP4_USER flag can't be altered for
27482b0d6f81SJamie Gritton 	 * existing prisons.
27490304c731SJamie Gritton 	 */
2750bdfc8cc4SJamie Gritton 	while (pr1 != &prison0 &&
2751bdfc8cc4SJamie Gritton #ifdef VIMAGE
2752bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_VNET) &&
2753bdfc8cc4SJamie Gritton #endif
2754bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_IP4_USER))
27550304c731SJamie Gritton 		pr1 = pr1->pr_parent;
2756bdfc8cc4SJamie Gritton 	while (pr2 != &prison0 &&
2757bdfc8cc4SJamie Gritton #ifdef VIMAGE
2758bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_VNET) &&
2759bdfc8cc4SJamie Gritton #endif
2760bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_IP4_USER))
27610304c731SJamie Gritton 		pr2 = pr2->pr_parent;
27620304c731SJamie Gritton 	return (pr1 == pr2);
27630304c731SJamie Gritton }
27640304c731SJamie Gritton 
27650304c731SJamie Gritton /*
2766413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this
2767413628a7SBjoern A. Zeeb  * jail.
2768413628a7SBjoern A. Zeeb  *
27690304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
27700304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
27710304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2772413628a7SBjoern A. Zeeb  */
2773413628a7SBjoern A. Zeeb int
2774413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2775413628a7SBjoern A. Zeeb {
2776b38ff370SJamie Gritton 	struct prison *pr;
2777413628a7SBjoern A. Zeeb 	struct in_addr ia0;
2778b38ff370SJamie Gritton 	int error;
2779413628a7SBjoern A. Zeeb 
2780413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2781413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2782413628a7SBjoern A. Zeeb 
2783b38ff370SJamie Gritton 	pr = cred->cr_prison;
27840304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
27850304c731SJamie Gritton 		return (0);
2786b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
27870304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
27880304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
27890304c731SJamie Gritton 		return (0);
27900304c731SJamie Gritton 	}
2791b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2792b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2793b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2794b38ff370SJamie Gritton 	}
2795413628a7SBjoern A. Zeeb 
2796413628a7SBjoern A. Zeeb 	ia0.s_addr = ntohl(ia->s_addr);
2797413628a7SBjoern A. Zeeb 	if (ia0.s_addr == INADDR_LOOPBACK) {
2798b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2799b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2800413628a7SBjoern A. Zeeb 		return (0);
2801413628a7SBjoern A. Zeeb 	}
2802413628a7SBjoern A. Zeeb 
2803b89e82ddSJamie Gritton 	if (ia0.s_addr == INADDR_ANY) {
2804413628a7SBjoern A. Zeeb 		/*
2805413628a7SBjoern A. Zeeb 		 * In case there is only 1 IPv4 address, bind directly.
2806413628a7SBjoern A. Zeeb 		 */
2807b38ff370SJamie Gritton 		if (pr->pr_ip4s == 1)
2808b38ff370SJamie Gritton 			ia->s_addr = pr->pr_ip4[0].s_addr;
2809b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2810413628a7SBjoern A. Zeeb 		return (0);
2811413628a7SBjoern A. Zeeb 	}
2812413628a7SBjoern A. Zeeb 
2813b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2814b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2815b38ff370SJamie Gritton 	return (error);
2816413628a7SBjoern A. Zeeb }
2817413628a7SBjoern A. Zeeb 
2818413628a7SBjoern A. Zeeb /*
2819413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
2820413628a7SBjoern A. Zeeb  *
2821b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2822b89e82ddSJamie Gritton  * Address passed in in NBO and returned in NBO.
2823413628a7SBjoern A. Zeeb  */
2824413628a7SBjoern A. Zeeb int
2825413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2826413628a7SBjoern A. Zeeb {
2827b38ff370SJamie Gritton 	struct prison *pr;
2828413628a7SBjoern A. Zeeb 
2829413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2830413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2831413628a7SBjoern A. Zeeb 
2832b38ff370SJamie Gritton 	pr = cred->cr_prison;
28330304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
28340304c731SJamie Gritton 		return (0);
2835b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
28360304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
28370304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
28380304c731SJamie Gritton 		return (0);
28390304c731SJamie Gritton 	}
2840b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2841b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2842b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2843b38ff370SJamie Gritton 	}
2844b89e82ddSJamie Gritton 
2845413628a7SBjoern A. Zeeb 	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2846b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2847b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2848413628a7SBjoern A. Zeeb 		return (0);
2849413628a7SBjoern A. Zeeb 	}
2850413628a7SBjoern A. Zeeb 
2851413628a7SBjoern A. Zeeb 	/*
2852413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
2853413628a7SBjoern A. Zeeb 	 */
2854b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2855413628a7SBjoern A. Zeeb 	return (0);
2856413628a7SBjoern A. Zeeb }
2857413628a7SBjoern A. Zeeb 
2858413628a7SBjoern A. Zeeb /*
2859b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
2860413628a7SBjoern A. Zeeb  *
28610304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
28620304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
28630304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO.
2864413628a7SBjoern A. Zeeb  */
2865413628a7SBjoern A. Zeeb static int
2866413628a7SBjoern A. Zeeb _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2867413628a7SBjoern A. Zeeb {
2868413628a7SBjoern A. Zeeb 	int i, a, z, d;
2869413628a7SBjoern A. Zeeb 
2870413628a7SBjoern A. Zeeb 	/*
2871413628a7SBjoern A. Zeeb 	 * Check the primary IP.
2872413628a7SBjoern A. Zeeb 	 */
2873413628a7SBjoern A. Zeeb 	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2874b89e82ddSJamie Gritton 		return (0);
2875413628a7SBjoern A. Zeeb 
2876413628a7SBjoern A. Zeeb 	/*
2877413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
2878413628a7SBjoern A. Zeeb 	 */
2879413628a7SBjoern A. Zeeb 	a = 0;
2880413628a7SBjoern A. Zeeb 	z = pr->pr_ip4s - 2;
2881413628a7SBjoern A. Zeeb 	while (a <= z) {
2882413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
2883413628a7SBjoern A. Zeeb 		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2884413628a7SBjoern A. Zeeb 		if (d > 0)
2885413628a7SBjoern A. Zeeb 			z = i - 1;
2886413628a7SBjoern A. Zeeb 		else if (d < 0)
2887413628a7SBjoern A. Zeeb 			a = i + 1;
2888413628a7SBjoern A. Zeeb 		else
2889413628a7SBjoern A. Zeeb 			return (0);
289075c13541SPoul-Henning Kamp 	}
289175c13541SPoul-Henning Kamp 
2892b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
2893b89e82ddSJamie Gritton }
2894b89e82ddSJamie Gritton 
289575c13541SPoul-Henning Kamp int
2896413628a7SBjoern A. Zeeb prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2897413628a7SBjoern A. Zeeb {
2898b38ff370SJamie Gritton 	struct prison *pr;
2899b38ff370SJamie Gritton 	int error;
2900413628a7SBjoern A. Zeeb 
2901413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2902413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2903413628a7SBjoern A. Zeeb 
2904b38ff370SJamie Gritton 	pr = cred->cr_prison;
29050304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
29060304c731SJamie Gritton 		return (0);
2907b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29080304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
29090304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29100304c731SJamie Gritton 		return (0);
29110304c731SJamie Gritton 	}
2912b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2913b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2914b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2915b38ff370SJamie Gritton 	}
2916413628a7SBjoern A. Zeeb 
2917b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2918b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2919b38ff370SJamie Gritton 	return (error);
2920413628a7SBjoern A. Zeeb }
2921413628a7SBjoern A. Zeeb #endif
2922413628a7SBjoern A. Zeeb 
2923413628a7SBjoern A. Zeeb #ifdef INET6
29240304c731SJamie Gritton static int
29250304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
29260304c731SJamie Gritton {
29270304c731SJamie Gritton 	int ii, ij, used;
29280304c731SJamie Gritton 	struct prison *ppr;
29290304c731SJamie Gritton 
29300304c731SJamie Gritton 	ppr = pr->pr_parent;
29310304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6_USER)) {
29320304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
29330304c731SJamie Gritton 		if (pr->pr_ip6s < ppr->pr_ip6s) {
29340304c731SJamie Gritton 			/*
29350304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
29360304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
29370304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
29380304c731SJamie Gritton 			 * allocate one.
29390304c731SJamie Gritton 			 */
29400304c731SJamie Gritton 			used = 1;
29410304c731SJamie Gritton 			if (newip6 == NULL) {
29420304c731SJamie Gritton 				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
29430304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
29440304c731SJamie Gritton 				if (newip6 != NULL)
29450304c731SJamie Gritton 					used = 0;
29460304c731SJamie Gritton 			}
29470304c731SJamie Gritton 			if (newip6 != NULL) {
29480304c731SJamie Gritton 				bcopy(ppr->pr_ip6, newip6,
29490304c731SJamie Gritton 				    ppr->pr_ip6s * sizeof(*newip6));
29500304c731SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
29510304c731SJamie Gritton 				pr->pr_ip6 = newip6;
29520304c731SJamie Gritton 				pr->pr_ip6s = ppr->pr_ip6s;
29530304c731SJamie Gritton 			}
29540304c731SJamie Gritton 			return (used);
29550304c731SJamie Gritton 		}
29560304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
29570304c731SJamie Gritton 		if (pr->pr_ip6s > 0)
29580304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
29590304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*newip6));
29600304c731SJamie Gritton 		else if (pr->pr_ip6 != NULL) {
29610304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
29620304c731SJamie Gritton 			pr->pr_ip6 = NULL;
29630304c731SJamie Gritton 		}
29642b0d6f81SJamie Gritton 	} else if (pr->pr_ip6s > 0) {
29650304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
29660304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip6s; ij++)
29670304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
29680304c731SJamie Gritton 			    &ppr->pr_ip6[ij]))
29690304c731SJamie Gritton 				break;
29700304c731SJamie Gritton 		if (ij < ppr->pr_ip6s)
29710304c731SJamie Gritton 			ii = 1;
29720304c731SJamie Gritton 		else {
29730304c731SJamie Gritton 			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
29740304c731SJamie Gritton 			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
29750304c731SJamie Gritton 			ii = 0;
29760304c731SJamie Gritton 		}
29770304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip6s; ) {
29780304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
29790304c731SJamie Gritton 			    &ppr->pr_ip6[0])) {
29800304c731SJamie Gritton 				ii++;
29810304c731SJamie Gritton 				continue;
29820304c731SJamie Gritton 			}
29830304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
29840304c731SJamie Gritton 				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
29850304c731SJamie Gritton 			case -1:
29860304c731SJamie Gritton 				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
29870304c731SJamie Gritton 				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
29880304c731SJamie Gritton 				break;
29890304c731SJamie Gritton 			case 0:
29900304c731SJamie Gritton 				ii++;
29910304c731SJamie Gritton 				ij++;
29920304c731SJamie Gritton 				break;
29930304c731SJamie Gritton 			case 1:
29940304c731SJamie Gritton 				ij++;
29950304c731SJamie Gritton 				break;
29960304c731SJamie Gritton 			}
29970304c731SJamie Gritton 		}
29980304c731SJamie Gritton 		if (pr->pr_ip6s == 0) {
29997cbf7213SJamie Gritton 			pr->pr_flags |= PR_IP6_DISABLE;
30000304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
30010304c731SJamie Gritton 			pr->pr_ip6 = NULL;
30020304c731SJamie Gritton 		}
30030304c731SJamie Gritton 	}
30040304c731SJamie Gritton 	return 0;
30050304c731SJamie Gritton }
30060304c731SJamie Gritton 
3007413628a7SBjoern A. Zeeb /*
3008413628a7SBjoern A. Zeeb  * Pass back primary IPv6 address for this jail.
3009413628a7SBjoern A. Zeeb  *
30100304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
30110304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3012413628a7SBjoern A. Zeeb  *
3013b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3014413628a7SBjoern A. Zeeb  */
3015413628a7SBjoern A. Zeeb int
30161cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3017413628a7SBjoern A. Zeeb {
3018b38ff370SJamie Gritton 	struct prison *pr;
3019413628a7SBjoern A. Zeeb 
3020413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3021413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3022413628a7SBjoern A. Zeeb 
3023b38ff370SJamie Gritton 	pr = cred->cr_prison;
30240304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
30250304c731SJamie Gritton 		return (0);
3026b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30270304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
30280304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30290304c731SJamie Gritton 		return (0);
30300304c731SJamie Gritton 	}
3031b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3032b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3033b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3034b38ff370SJamie Gritton 	}
3035b89e82ddSJamie Gritton 
3036b38ff370SJamie Gritton 	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3037b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3038413628a7SBjoern A. Zeeb 	return (0);
3039413628a7SBjoern A. Zeeb }
3040413628a7SBjoern A. Zeeb 
3041413628a7SBjoern A. Zeeb /*
3042592bcae8SBjoern A. Zeeb  * Return 1 if we should do proper source address selection or are not jailed.
3043592bcae8SBjoern A. Zeeb  * We will return 0 if we should bypass source address selection in favour
3044592bcae8SBjoern A. Zeeb  * of the primary jail IPv6 address. Only in this case *ia will be updated and
3045592bcae8SBjoern A. Zeeb  * returned in NBO.
3046592bcae8SBjoern A. Zeeb  * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3047592bcae8SBjoern A. Zeeb  */
3048592bcae8SBjoern A. Zeeb int
3049592bcae8SBjoern A. Zeeb prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3050592bcae8SBjoern A. Zeeb {
3051592bcae8SBjoern A. Zeeb 	struct prison *pr;
3052592bcae8SBjoern A. Zeeb 	struct in6_addr lia6;
3053592bcae8SBjoern A. Zeeb 	int error;
3054592bcae8SBjoern A. Zeeb 
3055592bcae8SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3056592bcae8SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3057592bcae8SBjoern A. Zeeb 
3058592bcae8SBjoern A. Zeeb 	if (!jailed(cred))
3059592bcae8SBjoern A. Zeeb 		return (1);
3060592bcae8SBjoern A. Zeeb 
3061592bcae8SBjoern A. Zeeb 	pr = cred->cr_prison;
3062592bcae8SBjoern A. Zeeb 	if (pr->pr_flags & PR_IP6_SADDRSEL)
3063592bcae8SBjoern A. Zeeb 		return (1);
3064592bcae8SBjoern A. Zeeb 
3065592bcae8SBjoern A. Zeeb 	lia6 = in6addr_any;
3066592bcae8SBjoern A. Zeeb 	error = prison_get_ip6(cred, &lia6);
3067592bcae8SBjoern A. Zeeb 	if (error)
3068592bcae8SBjoern A. Zeeb 		return (error);
3069592bcae8SBjoern A. Zeeb 	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3070592bcae8SBjoern A. Zeeb 		return (1);
3071592bcae8SBjoern A. Zeeb 
3072592bcae8SBjoern A. Zeeb 	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3073592bcae8SBjoern A. Zeeb 	return (0);
3074592bcae8SBjoern A. Zeeb }
3075592bcae8SBjoern A. Zeeb 
3076592bcae8SBjoern A. Zeeb /*
30770304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv6 address restrictions.
30780304c731SJamie Gritton  */
30790304c731SJamie Gritton int
30800304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2)
30810304c731SJamie Gritton {
30820304c731SJamie Gritton 
30830304c731SJamie Gritton 	if (pr1 == pr2)
30840304c731SJamie Gritton 		return (1);
30850304c731SJamie Gritton 
3086bdfc8cc4SJamie Gritton 	while (pr1 != &prison0 &&
3087bdfc8cc4SJamie Gritton #ifdef VIMAGE
3088bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_VNET) &&
3089bdfc8cc4SJamie Gritton #endif
3090bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_IP6_USER))
30910304c731SJamie Gritton 		pr1 = pr1->pr_parent;
3092bdfc8cc4SJamie Gritton 	while (pr2 != &prison0 &&
3093bdfc8cc4SJamie Gritton #ifdef VIMAGE
3094bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_VNET) &&
3095bdfc8cc4SJamie Gritton #endif
3096bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_IP6_USER))
30970304c731SJamie Gritton 		pr2 = pr2->pr_parent;
30980304c731SJamie Gritton 	return (pr1 == pr2);
30990304c731SJamie Gritton }
31000304c731SJamie Gritton 
31010304c731SJamie Gritton /*
3102413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this jail.
3103413628a7SBjoern A. Zeeb  *
3104413628a7SBjoern A. Zeeb  * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3105413628a7SBjoern A. Zeeb  * when needed while binding.
3106413628a7SBjoern A. Zeeb  *
31070304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
31080304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
31090304c731SJamie Gritton  * doesn't allow IPv6.
3110413628a7SBjoern A. Zeeb  */
3111413628a7SBjoern A. Zeeb int
3112413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3113413628a7SBjoern A. Zeeb {
3114b38ff370SJamie Gritton 	struct prison *pr;
3115b38ff370SJamie Gritton 	int error;
3116413628a7SBjoern A. Zeeb 
3117413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3118413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3119413628a7SBjoern A. Zeeb 
3120b38ff370SJamie Gritton 	pr = cred->cr_prison;
31210304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
31220304c731SJamie Gritton 		return (0);
3123b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
31240304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
31250304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31260304c731SJamie Gritton 		return (0);
31270304c731SJamie Gritton 	}
3128b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3129b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3130b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3131b38ff370SJamie Gritton 	}
3132b89e82ddSJamie Gritton 
3133413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3134b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3135b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3136413628a7SBjoern A. Zeeb 		return (0);
3137413628a7SBjoern A. Zeeb 	}
3138413628a7SBjoern A. Zeeb 
3139b89e82ddSJamie Gritton 	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3140413628a7SBjoern A. Zeeb 		/*
3141b89e82ddSJamie Gritton 		 * In case there is only 1 IPv6 address, and v6only is true,
3142b89e82ddSJamie Gritton 		 * then bind directly.
3143413628a7SBjoern A. Zeeb 		 */
3144b38ff370SJamie Gritton 		if (v6only != 0 && pr->pr_ip6s == 1)
3145b38ff370SJamie Gritton 			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3146b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3147413628a7SBjoern A. Zeeb 		return (0);
3148413628a7SBjoern A. Zeeb 	}
3149b89e82ddSJamie Gritton 
3150b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3151b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3152b38ff370SJamie Gritton 	return (error);
3153413628a7SBjoern A. Zeeb }
3154413628a7SBjoern A. Zeeb 
3155413628a7SBjoern A. Zeeb /*
3156413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
3157413628a7SBjoern A. Zeeb  *
3158b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3159413628a7SBjoern A. Zeeb  */
3160413628a7SBjoern A. Zeeb int
3161413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3162413628a7SBjoern A. Zeeb {
3163b38ff370SJamie Gritton 	struct prison *pr;
3164413628a7SBjoern A. Zeeb 
3165413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3166413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3167413628a7SBjoern A. Zeeb 
3168b38ff370SJamie Gritton 	pr = cred->cr_prison;
31690304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
31700304c731SJamie Gritton 		return (0);
3171b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
31720304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
31730304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31740304c731SJamie Gritton 		return (0);
31750304c731SJamie Gritton 	}
3176b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3177b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3178b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3179b38ff370SJamie Gritton 	}
3180b89e82ddSJamie Gritton 
3181413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3182b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3183b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3184413628a7SBjoern A. Zeeb 		return (0);
3185413628a7SBjoern A. Zeeb 	}
3186413628a7SBjoern A. Zeeb 
3187413628a7SBjoern A. Zeeb 	/*
3188413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
3189413628a7SBjoern A. Zeeb 	 */
3190b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3191413628a7SBjoern A. Zeeb 	return (0);
3192413628a7SBjoern A. Zeeb }
3193413628a7SBjoern A. Zeeb 
3194413628a7SBjoern A. Zeeb /*
3195b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
3196413628a7SBjoern A. Zeeb  *
31970304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
31980304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
31990304c731SJamie Gritton  * doesn't allow IPv6.
3200413628a7SBjoern A. Zeeb  */
3201413628a7SBjoern A. Zeeb static int
3202413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3203413628a7SBjoern A. Zeeb {
3204413628a7SBjoern A. Zeeb 	int i, a, z, d;
3205413628a7SBjoern A. Zeeb 
3206413628a7SBjoern A. Zeeb 	/*
3207413628a7SBjoern A. Zeeb 	 * Check the primary IP.
3208413628a7SBjoern A. Zeeb 	 */
3209413628a7SBjoern A. Zeeb 	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3210b89e82ddSJamie Gritton 		return (0);
3211413628a7SBjoern A. Zeeb 
3212413628a7SBjoern A. Zeeb 	/*
3213413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
3214413628a7SBjoern A. Zeeb 	 */
3215413628a7SBjoern A. Zeeb 	a = 0;
3216413628a7SBjoern A. Zeeb 	z = pr->pr_ip6s - 2;
3217413628a7SBjoern A. Zeeb 	while (a <= z) {
3218413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
3219413628a7SBjoern A. Zeeb 		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3220413628a7SBjoern A. Zeeb 		if (d > 0)
3221413628a7SBjoern A. Zeeb 			z = i - 1;
3222413628a7SBjoern A. Zeeb 		else if (d < 0)
3223413628a7SBjoern A. Zeeb 			a = i + 1;
3224413628a7SBjoern A. Zeeb 		else
3225413628a7SBjoern A. Zeeb 			return (0);
3226413628a7SBjoern A. Zeeb 	}
3227413628a7SBjoern A. Zeeb 
3228b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
3229b89e82ddSJamie Gritton }
3230b89e82ddSJamie Gritton 
3231413628a7SBjoern A. Zeeb int
3232413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3233413628a7SBjoern A. Zeeb {
3234b38ff370SJamie Gritton 	struct prison *pr;
3235b38ff370SJamie Gritton 	int error;
3236413628a7SBjoern A. Zeeb 
3237413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3238413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3239413628a7SBjoern A. Zeeb 
3240b38ff370SJamie Gritton 	pr = cred->cr_prison;
32410304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
32420304c731SJamie Gritton 		return (0);
3243b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
32440304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
32450304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
32460304c731SJamie Gritton 		return (0);
32470304c731SJamie Gritton 	}
3248b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3249b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3250b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3251b38ff370SJamie Gritton 	}
3252413628a7SBjoern A. Zeeb 
3253b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3254b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3255b38ff370SJamie Gritton 	return (error);
3256413628a7SBjoern A. Zeeb }
3257413628a7SBjoern A. Zeeb #endif
3258413628a7SBjoern A. Zeeb 
3259413628a7SBjoern A. Zeeb /*
3260ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3261ca04ba64SJamie Gritton  *
3262ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3263ca04ba64SJamie Gritton  * if not.
3264ca04ba64SJamie Gritton  */
3265ca04ba64SJamie Gritton int
3266ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3267ca04ba64SJamie Gritton {
32680304c731SJamie Gritton 	struct prison *pr;
3269ca04ba64SJamie Gritton 	int error;
3270ca04ba64SJamie Gritton 
3271ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3272ca04ba64SJamie Gritton 
32730304c731SJamie Gritton 	pr = cred->cr_prison;
3274499650a0SJamie Gritton #ifdef VIMAGE
32756bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3276de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
32776bb79563SJamie Gritton 		return (0);
3278499650a0SJamie Gritton #endif
32796bb79563SJamie Gritton 
3280ca04ba64SJamie Gritton 	error = 0;
3281ca04ba64SJamie Gritton 	switch (af)
3282ca04ba64SJamie Gritton 	{
3283ca04ba64SJamie Gritton #ifdef INET
3284ca04ba64SJamie Gritton 	case AF_INET:
32850304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
32860304c731SJamie Gritton 		{
32870304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
32880304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3289ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
32900304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
32910304c731SJamie Gritton 		}
3292ca04ba64SJamie Gritton 		break;
3293ca04ba64SJamie Gritton #endif
3294ca04ba64SJamie Gritton #ifdef INET6
3295ca04ba64SJamie Gritton 	case AF_INET6:
32960304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
32970304c731SJamie Gritton 		{
32980304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
32990304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3300ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33010304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33020304c731SJamie Gritton 		}
3303ca04ba64SJamie Gritton 		break;
3304ca04ba64SJamie Gritton #endif
3305ca04ba64SJamie Gritton 	case AF_LOCAL:
3306ca04ba64SJamie Gritton 	case AF_ROUTE:
3307ca04ba64SJamie Gritton 		break;
3308ca04ba64SJamie Gritton 	default:
33090304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3310ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3311ca04ba64SJamie Gritton 	}
3312ca04ba64SJamie Gritton 	return (error);
3313ca04ba64SJamie Gritton }
3314ca04ba64SJamie Gritton 
3315ca04ba64SJamie Gritton /*
3316413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3317413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3318413628a7SBjoern A. Zeeb  *
33190304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
33200304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
33210304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3322413628a7SBjoern A. Zeeb  */
3323413628a7SBjoern A. Zeeb int
332491421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
332575c13541SPoul-Henning Kamp {
3326413628a7SBjoern A. Zeeb #ifdef INET
33279ddb7954SMike Barcroft 	struct sockaddr_in *sai;
3328413628a7SBjoern A. Zeeb #endif
3329413628a7SBjoern A. Zeeb #ifdef INET6
3330413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
3331413628a7SBjoern A. Zeeb #endif
3332b89e82ddSJamie Gritton 	int error;
333375c13541SPoul-Henning Kamp 
3334413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3335413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3336413628a7SBjoern A. Zeeb 
3337de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3338de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3339de0bd6f7SBjoern A. Zeeb 		return (0);
3340de0bd6f7SBjoern A. Zeeb #endif
3341de0bd6f7SBjoern A. Zeeb 
3342b89e82ddSJamie Gritton 	error = 0;
3343413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3344413628a7SBjoern A. Zeeb 	{
3345413628a7SBjoern A. Zeeb #ifdef INET
3346413628a7SBjoern A. Zeeb 	case AF_INET:
33479ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
3348b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3349413628a7SBjoern A. Zeeb 		break;
3350413628a7SBjoern A. Zeeb #endif
3351413628a7SBjoern A. Zeeb #ifdef INET6
3352413628a7SBjoern A. Zeeb 	case AF_INET6:
3353413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
3354b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3355413628a7SBjoern A. Zeeb 		break;
3356413628a7SBjoern A. Zeeb #endif
3357413628a7SBjoern A. Zeeb 	default:
33580304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3359b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3360413628a7SBjoern A. Zeeb 	}
3361b89e82ddSJamie Gritton 	return (error);
336275c13541SPoul-Henning Kamp }
336391421ba2SRobert Watson 
336491421ba2SRobert Watson /*
336591421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
336691421ba2SRobert Watson  */
336791421ba2SRobert Watson int
33689ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
336991421ba2SRobert Watson {
337091421ba2SRobert Watson 
33710304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
33720304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
33730304c731SJamie Gritton }
337491421ba2SRobert Watson 
33750304c731SJamie Gritton /*
33760304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
33770304c731SJamie Gritton  */
33780304c731SJamie Gritton int
33790304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
33800304c731SJamie Gritton {
33810304c731SJamie Gritton 
33820304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
33830304c731SJamie Gritton 		if (pr1 == pr2)
33840304c731SJamie Gritton 			return (1);
338591421ba2SRobert Watson 	return (0);
338691421ba2SRobert Watson }
338791421ba2SRobert Watson 
338891421ba2SRobert Watson /*
338991421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
339091421ba2SRobert Watson  */
339191421ba2SRobert Watson int
33929ddb7954SMike Barcroft jailed(struct ucred *cred)
339391421ba2SRobert Watson {
339491421ba2SRobert Watson 
33950304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
339691421ba2SRobert Watson }
33979484d0c0SRobert Drehmel 
33989484d0c0SRobert Drehmel /*
3399de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3400de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3401de0bd6f7SBjoern A. Zeeb  */
3402de0bd6f7SBjoern A. Zeeb int
3403de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3404de0bd6f7SBjoern A. Zeeb {
3405de0bd6f7SBjoern A. Zeeb 
3406de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3407de0bd6f7SBjoern A. Zeeb 		return (0);
3408de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3409de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3410de0bd6f7SBjoern A. Zeeb 		return (0);
3411de0bd6f7SBjoern A. Zeeb #endif
3412de0bd6f7SBjoern A. Zeeb 
3413de0bd6f7SBjoern A. Zeeb 	return (1);
3414de0bd6f7SBjoern A. Zeeb }
3415de0bd6f7SBjoern A. Zeeb 
3416de0bd6f7SBjoern A. Zeeb /*
34177455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
34189484d0c0SRobert Drehmel  */
3419ad1ff099SRobert Drehmel void
34209ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
34219484d0c0SRobert Drehmel {
342276ca6f88SJamie Gritton 	struct prison *pr;
34239484d0c0SRobert Drehmel 
34247455b100SJamie Gritton 	/*
34257455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
34267455b100SJamie Gritton 	 * system's hostname.
34277455b100SJamie Gritton 	 */
342876ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
342976ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3430c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
343176ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
34329484d0c0SRobert Drehmel }
3433fd7a8150SMike Barcroft 
34347455b100SJamie Gritton void
34357455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
34367455b100SJamie Gritton {
34377455b100SJamie Gritton 
34387455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3439c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
34407455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
34417455b100SJamie Gritton }
34427455b100SJamie Gritton 
34437455b100SJamie Gritton void
34447455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
34457455b100SJamie Gritton {
34467455b100SJamie Gritton 
34477455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3448c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
34497455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
34507455b100SJamie Gritton }
34517455b100SJamie Gritton 
34527455b100SJamie Gritton void
34537455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
34547455b100SJamie Gritton {
34557455b100SJamie Gritton 
34567455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
34577455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
34587455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
34597455b100SJamie Gritton }
34607455b100SJamie Gritton 
3461eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3462eb79e1c7SBjoern A. Zeeb /*
3463eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3464eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3465eb79e1c7SBjoern A. Zeeb  *
3466eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3467eb79e1c7SBjoern A. Zeeb  */
3468eb79e1c7SBjoern A. Zeeb int
3469eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3470eb79e1c7SBjoern A. Zeeb {
3471eb79e1c7SBjoern A. Zeeb 
3472eb79e1c7SBjoern A. Zeeb 	/*
3473eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3474eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3475eb79e1c7SBjoern A. Zeeb 	 */
3476eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3477eb79e1c7SBjoern A. Zeeb }
3478eb79e1c7SBjoern A. Zeeb #endif
3479eb79e1c7SBjoern A. Zeeb 
3480f08df373SRobert Watson /*
3481820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3482820a0de9SPawel Jakub Dawidek  * status of a mount point.
3483820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3484820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3485820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3486f08df373SRobert Watson  */
3487f08df373SRobert Watson int
3488820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3489f08df373SRobert Watson {
3490820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3491820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3492820a0de9SPawel Jakub Dawidek 	size_t len;
3493f08df373SRobert Watson 
3494820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
34950304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
34960304c731SJamie Gritton 		return (0);
3497820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3498820a0de9SPawel Jakub Dawidek 		return (0);
34990304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3500820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3501820a0de9SPawel Jakub Dawidek 	/*
3502820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3503820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3504820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3505820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3506820a0de9SPawel Jakub Dawidek 	 */
3507820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3508820a0de9SPawel Jakub Dawidek 		return (0);
3509820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3510820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3511820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3512820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3513820a0de9SPawel Jakub Dawidek 	/*
3514820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3515820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3516820a0de9SPawel Jakub Dawidek 	 */
3517820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3518820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3519f08df373SRobert Watson 	return (0);
3520f08df373SRobert Watson }
3521820a0de9SPawel Jakub Dawidek 
3522820a0de9SPawel Jakub Dawidek void
3523820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3524820a0de9SPawel Jakub Dawidek {
3525820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3526820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3527820a0de9SPawel Jakub Dawidek 	size_t len;
3528820a0de9SPawel Jakub Dawidek 
3529820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
35300304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
35310304c731SJamie Gritton 		return;
3532820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3533820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3534820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3535820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3536820a0de9SPawel Jakub Dawidek 		return;
3537820a0de9SPawel Jakub Dawidek 	}
3538820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3539820a0de9SPawel Jakub Dawidek 		/*
3540820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3541820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3542820a0de9SPawel Jakub Dawidek 		 */
3543820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3544820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3545820a0de9SPawel Jakub Dawidek 		return;
3546820a0de9SPawel Jakub Dawidek 	}
3547820a0de9SPawel Jakub Dawidek 	/*
3548820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3549820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3550820a0de9SPawel Jakub Dawidek 	 */
3551820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3552820a0de9SPawel Jakub Dawidek 		return;
3553820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3554820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3555820a0de9SPawel Jakub Dawidek 	/*
3556820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3557820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3558820a0de9SPawel Jakub Dawidek 	 */
3559820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3560820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3561820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3562820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3563820a0de9SPawel Jakub Dawidek 	} else {
3564820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3565820a0de9SPawel Jakub Dawidek 	}
3566f08df373SRobert Watson }
3567f08df373SRobert Watson 
3568800c9408SRobert Watson /*
3569800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3570800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3571800c9408SRobert Watson  */
3572800c9408SRobert Watson int
3573800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3574800c9408SRobert Watson {
3575800c9408SRobert Watson 
3576800c9408SRobert Watson 	if (!jailed(cred))
3577800c9408SRobert Watson 		return (0);
3578800c9408SRobert Watson 
35796bb79563SJamie Gritton #ifdef VIMAGE
35806bb79563SJamie Gritton 	/*
35816bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
35826bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
35836bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
35846bb79563SJamie Gritton 	 */
35856bb79563SJamie Gritton 	switch (priv) {
35866bb79563SJamie Gritton #ifdef notyet
35876bb79563SJamie Gritton 		/*
35886bb79563SJamie Gritton 		 * NFS-specific privileges.
35896bb79563SJamie Gritton 		 */
35906bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
35916bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
35926bb79563SJamie Gritton #endif
35936bb79563SJamie Gritton 		/*
35946bb79563SJamie Gritton 		 * Network stack privileges.
35956bb79563SJamie Gritton 		 */
35966bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
35976bb79563SJamie Gritton 	case PRIV_NET_GRE:
35986bb79563SJamie Gritton 	case PRIV_NET_BPF:
35996bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
36006bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
36016bb79563SJamie Gritton 	case PRIV_NET_TAP:
36026bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
36036bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
36046bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3605215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
36066bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
36076bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
36086bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
36096bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
36106bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
36116bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
36126bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
36136bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
36146bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
36156bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
36166bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
36176bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
36186bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
36196bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
36206bb79563SJamie Gritton 	case PRIV_NET_LAGG:
36216bb79563SJamie Gritton 	case PRIV_NET_GIF:
36226bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
36236bb79563SJamie Gritton 
36246bb79563SJamie Gritton 		/*
36256bb79563SJamie Gritton 		 * 802.11-related privileges.
36266bb79563SJamie Gritton 		 */
36276bb79563SJamie Gritton 	case PRIV_NET80211_GETKEY:
36286bb79563SJamie Gritton #ifdef notyet
36296bb79563SJamie Gritton 	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
36306bb79563SJamie Gritton #endif
36316bb79563SJamie Gritton 
36326bb79563SJamie Gritton #ifdef notyet
36336bb79563SJamie Gritton 		/*
36346bb79563SJamie Gritton 		 * AppleTalk privileges.
36356bb79563SJamie Gritton 		 */
36366bb79563SJamie Gritton 	case PRIV_NETATALK_RESERVEDPORT:
36376bb79563SJamie Gritton 
36386bb79563SJamie Gritton 		/*
36396bb79563SJamie Gritton 		 * ATM privileges.
36406bb79563SJamie Gritton 		 */
36416bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
36426bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
36436bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
36446bb79563SJamie Gritton 	case PRIV_NETATM_SET:
36456bb79563SJamie Gritton 
36466bb79563SJamie Gritton 		/*
36476bb79563SJamie Gritton 		 * Bluetooth privileges.
36486bb79563SJamie Gritton 		 */
36496bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
36506bb79563SJamie Gritton #endif
36516bb79563SJamie Gritton 
36526bb79563SJamie Gritton 		/*
36536bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
36546bb79563SJamie Gritton 		 */
36556bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
36566bb79563SJamie Gritton #ifdef notyet
36576bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
36586bb79563SJamie Gritton #endif
36596bb79563SJamie Gritton 
36606bb79563SJamie Gritton 		/*
36616bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
36626bb79563SJamie Gritton 		 */
36636bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
36646bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
36656bb79563SJamie Gritton 	case PRIV_NETINET_PF:
36666bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
36676bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
36686bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
36696bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
36706bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
36716bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
36726bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
36736bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
36746bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
36756bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
36766bb79563SJamie Gritton 
36776bb79563SJamie Gritton #ifdef notyet
36786bb79563SJamie Gritton 		/*
36796bb79563SJamie Gritton 		 * IPX/SPX privileges.
36806bb79563SJamie Gritton 		 */
36816bb79563SJamie Gritton 	case PRIV_NETIPX_RESERVEDPORT:
36826bb79563SJamie Gritton 	case PRIV_NETIPX_RAW:
36836bb79563SJamie Gritton 
36846bb79563SJamie Gritton 		/*
36856bb79563SJamie Gritton 		 * NCP privileges.
36866bb79563SJamie Gritton 		 */
36876bb79563SJamie Gritton 	case PRIV_NETNCP:
36886bb79563SJamie Gritton 
36896bb79563SJamie Gritton 		/*
36906bb79563SJamie Gritton 		 * SMB privileges.
36916bb79563SJamie Gritton 		 */
36926bb79563SJamie Gritton 	case PRIV_NETSMB:
36936bb79563SJamie Gritton #endif
36946bb79563SJamie Gritton 
36956bb79563SJamie Gritton 	/*
36966bb79563SJamie Gritton 	 * No default: or deny here.
36976bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
36986bb79563SJamie Gritton 	 */
36996bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
37006bb79563SJamie Gritton 			return (0);
37016bb79563SJamie Gritton 	}
37026bb79563SJamie Gritton #endif /* VIMAGE */
37036bb79563SJamie Gritton 
3704800c9408SRobert Watson 	switch (priv) {
3705800c9408SRobert Watson 
3706800c9408SRobert Watson 		/*
3707800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3708800c9408SRobert Watson 		 */
3709800c9408SRobert Watson 	case PRIV_KTRACE:
3710800c9408SRobert Watson 
3711c3c1b5e6SRobert Watson #if 0
3712800c9408SRobert Watson 		/*
3713800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3714800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3715800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3716800c9408SRobert Watson 		 * jail.
3717800c9408SRobert Watson 		 */
3718800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3719800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3720800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3721c3c1b5e6SRobert Watson #endif
3722800c9408SRobert Watson 
3723800c9408SRobert Watson 		/*
3724800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3725800c9408SRobert Watson 		 * credentials in any way they see fit.
3726800c9408SRobert Watson 		 */
3727800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3728800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3729800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3730800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3731800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3732800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3733800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3734800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3735800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3736800c9408SRobert Watson 
3737800c9408SRobert Watson 		/*
3738800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3739800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3740800c9408SRobert Watson 		 */
3741800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3742800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3743800c9408SRobert Watson 
3744800c9408SRobert Watson 		/*
3745800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3746800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3747800c9408SRobert Watson 		 */
3748800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3749800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3750800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3751800c9408SRobert Watson 
3752800c9408SRobert Watson 		/*
3753800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3754800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3755800c9408SRobert Watson 		 */
3756800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3757800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3758800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3759800c9408SRobert Watson 
3760800c9408SRobert Watson 		/*
3761800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3762800c9408SRobert Watson 		 */
3763800c9408SRobert Watson 	case PRIV_IPC_READ:
3764800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3765800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3766800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3767800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3768800c9408SRobert Watson 
3769800c9408SRobert Watson 		/*
37700304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
37710304c731SJamie Gritton 		 */
37720304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
37730304c731SJamie Gritton 	case PRIV_JAIL_SET:
37740304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
37750304c731SJamie Gritton 
37760304c731SJamie Gritton 		/*
3777800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3778800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3779800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3780800c9408SRobert Watson 		 */
3781800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3782413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3783800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3784800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3785800c9408SRobert Watson 
3786800c9408SRobert Watson 		/*
3787800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3788800c9408SRobert Watson 		 * writable.
3789800c9408SRobert Watson 		 */
3790800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3791800c9408SRobert Watson 
3792800c9408SRobert Watson 		/*
3793800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3794e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3795e82d0201SRobert Watson 		 * configuration option.
3796800c9408SRobert Watson 		 */
379795b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
379895b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3799800c9408SRobert Watson 
3800800c9408SRobert Watson 		/*
3801800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3802800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3803800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3804800c9408SRobert Watson 		 * privileges.
3805800c9408SRobert Watson 		 */
3806800c9408SRobert Watson 	case PRIV_VFS_READ:
3807800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3808800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3809800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3810800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3811800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3812800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3813800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3814800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3815bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3816800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3817800c9408SRobert Watson 	case PRIV_VFS_LINK:
3818800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3819e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3820800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3821800c9408SRobert Watson 		return (0);
3822800c9408SRobert Watson 
3823800c9408SRobert Watson 		/*
3824800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3825800c9408SRobert Watson 		 * setting system flags.
3826800c9408SRobert Watson 		 */
3827800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
38280304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3829800c9408SRobert Watson 			return (0);
3830800c9408SRobert Watson 		else
3831800c9408SRobert Watson 			return (EPERM);
3832800c9408SRobert Watson 
3833800c9408SRobert Watson 		/*
3834f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3835f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3836f3a8d2f9SPawel Jakub Dawidek 		 */
3837f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3838f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3839f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
384024b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
38410304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3842f3a8d2f9SPawel Jakub Dawidek 			return (0);
3843f3a8d2f9SPawel Jakub Dawidek 		else
3844f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
3845f3a8d2f9SPawel Jakub Dawidek 
3846f3a8d2f9SPawel Jakub Dawidek 		/*
38474b084056SRobert Watson 		 * Allow jailed root to bind reserved ports and reuse in-use
38484b084056SRobert Watson 		 * ports.
3849800c9408SRobert Watson 		 */
3850800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
38514b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3852800c9408SRobert Watson 		return (0);
3853800c9408SRobert Watson 
3854800c9408SRobert Watson 		/*
385579ba3952SBjoern A. Zeeb 		 * Allow jailed root to set certian IPv4/6 (option) headers.
385679ba3952SBjoern A. Zeeb 		 */
385779ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
385879ba3952SBjoern A. Zeeb 		return (0);
385979ba3952SBjoern A. Zeeb 
386079ba3952SBjoern A. Zeeb 		/*
3861800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3862800c9408SRobert Watson 		 */
3863800c9408SRobert Watson 	case PRIV_NETINET_RAW:
38640304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3865800c9408SRobert Watson 			return (0);
3866800c9408SRobert Watson 		else
3867800c9408SRobert Watson 			return (EPERM);
3868800c9408SRobert Watson 
3869800c9408SRobert Watson 		/*
3870800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3871800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3872800c9408SRobert Watson 		 * jail.
3873800c9408SRobert Watson 		 */
3874800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3875800c9408SRobert Watson 		return (0);
3876800c9408SRobert Watson 
3877*2bfc50bcSEdward Tomasz Napierala 		/*
3878*2bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
3879*2bfc50bcSEdward Tomasz Napierala 		 */
3880*2bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
3881*2bfc50bcSEdward Tomasz Napierala 		return (0);
3882*2bfc50bcSEdward Tomasz Napierala 
3883800c9408SRobert Watson 	default:
3884800c9408SRobert Watson 		/*
3885800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3886800c9408SRobert Watson 		 * includes almost all network privileges, many system
3887800c9408SRobert Watson 		 * configuration privileges.
3888800c9408SRobert Watson 		 */
3889800c9408SRobert Watson 		return (EPERM);
3890800c9408SRobert Watson 	}
3891800c9408SRobert Watson }
3892800c9408SRobert Watson 
38930304c731SJamie Gritton /*
38940304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
38950304c731SJamie Gritton  * if it does not directly follow.
38960304c731SJamie Gritton  */
38970304c731SJamie Gritton 
38980304c731SJamie Gritton char *
38990304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
39000304c731SJamie Gritton {
39010304c731SJamie Gritton 	char *name;
39020304c731SJamie Gritton 
39030304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
39040304c731SJamie Gritton 	if (pr1 == pr2)
39050304c731SJamie Gritton 		return "0";
39060304c731SJamie Gritton 	name = pr2->pr_name;
39070304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
39080304c731SJamie Gritton 		/*
39090304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
39100304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
39110304c731SJamie Gritton 		 * can be counted on - and counted.
39120304c731SJamie Gritton 		 */
39130304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
39140304c731SJamie Gritton 			name = strchr(name, '.') + 1;
39150304c731SJamie Gritton 	}
39160304c731SJamie Gritton 	return (name);
39170304c731SJamie Gritton }
39180304c731SJamie Gritton 
39190304c731SJamie Gritton /*
39200304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
39210304c731SJamie Gritton  * if it does not directly follow.
39220304c731SJamie Gritton  */
39230304c731SJamie Gritton static char *
39240304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
39250304c731SJamie Gritton {
39260304c731SJamie Gritton 	char *path1, *path2;
39270304c731SJamie Gritton 	int len1;
39280304c731SJamie Gritton 
39290304c731SJamie Gritton 	path1 = pr1->pr_path;
39300304c731SJamie Gritton 	path2 = pr2->pr_path;
39310304c731SJamie Gritton 	if (!strcmp(path1, "/"))
39320304c731SJamie Gritton 		return (path2);
39330304c731SJamie Gritton 	len1 = strlen(path1);
39340304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
39350304c731SJamie Gritton 		return (path2);
39360304c731SJamie Gritton 	if (path2[len1] == '\0')
39370304c731SJamie Gritton 		return "/";
39380304c731SJamie Gritton 	if (path2[len1] == '/')
39390304c731SJamie Gritton 		return (path2 + len1);
39400304c731SJamie Gritton 	return (path2);
39410304c731SJamie Gritton }
39420304c731SJamie Gritton 
39430304c731SJamie Gritton 
39440304c731SJamie Gritton /*
39450304c731SJamie Gritton  * Jail-related sysctls.
39460304c731SJamie Gritton  */
39470304c731SJamie Gritton SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
39480304c731SJamie Gritton     "Jails");
39490304c731SJamie Gritton 
3950fd7a8150SMike Barcroft static int
3951fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3952fd7a8150SMike Barcroft {
3953b38ff370SJamie Gritton 	struct xprison *xp;
39540304c731SJamie Gritton 	struct prison *pr, *cpr;
3955b38ff370SJamie Gritton #ifdef INET
3956b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3957b38ff370SJamie Gritton 	int ip4s = 0;
3958b38ff370SJamie Gritton #endif
3959b38ff370SJamie Gritton #ifdef INET6
39603beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
3961b38ff370SJamie Gritton 	int ip6s = 0;
3962b38ff370SJamie Gritton #endif
39630304c731SJamie Gritton 	int descend, error;
3964fd7a8150SMike Barcroft 
3965b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
39660304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3967b38ff370SJamie Gritton 	error = 0;
3968dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
39690304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
39700304c731SJamie Gritton #if defined(INET) || defined(INET6)
3971b38ff370SJamie Gritton  again:
39720304c731SJamie Gritton #endif
39730304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3974413628a7SBjoern A. Zeeb #ifdef INET
39750304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
39760304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
39770304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
39780304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3979b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3980b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3981b38ff370SJamie Gritton 				goto again;
3982b38ff370SJamie Gritton 			}
39830304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
39840304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3985b38ff370SJamie Gritton 		}
3986413628a7SBjoern A. Zeeb #endif
3987413628a7SBjoern A. Zeeb #ifdef INET6
39880304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
39890304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
39900304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
39910304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3992b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3993b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3994b38ff370SJamie Gritton 				goto again;
3995413628a7SBjoern A. Zeeb 			}
39960304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
39970304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3998b38ff370SJamie Gritton 		}
3999b38ff370SJamie Gritton #endif
40000304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
40010304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
4002b38ff370SJamie Gritton 			continue;
4003b38ff370SJamie Gritton 		}
4004b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
4005fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
40060304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
40070304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
4008b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
40090304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4010c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
40110304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4012413628a7SBjoern A. Zeeb #ifdef INET
40130304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
4014413628a7SBjoern A. Zeeb #endif
4015413628a7SBjoern A. Zeeb #ifdef INET6
40160304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
4017413628a7SBjoern A. Zeeb #endif
40180304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
4019b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4020b38ff370SJamie Gritton 		if (error)
4021b38ff370SJamie Gritton 			break;
4022413628a7SBjoern A. Zeeb #ifdef INET
4023b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
4024b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
4025b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
4026b38ff370SJamie Gritton 			if (error)
4027b38ff370SJamie Gritton 				break;
4028413628a7SBjoern A. Zeeb 		}
4029413628a7SBjoern A. Zeeb #endif
4030413628a7SBjoern A. Zeeb #ifdef INET6
4031b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
4032b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
4033b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
4034b38ff370SJamie Gritton 			if (error)
4035b38ff370SJamie Gritton 				break;
4036413628a7SBjoern A. Zeeb 		}
4037413628a7SBjoern A. Zeeb #endif
4038fd7a8150SMike Barcroft 	}
4039dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
4040b38ff370SJamie Gritton 	free(xp, M_TEMP);
4041b38ff370SJamie Gritton #ifdef INET
4042b38ff370SJamie Gritton 	free(ip4, M_TEMP);
4043b38ff370SJamie Gritton #endif
4044b38ff370SJamie Gritton #ifdef INET6
4045b38ff370SJamie Gritton 	free(ip6, M_TEMP);
4046b38ff370SJamie Gritton #endif
4047fd7a8150SMike Barcroft 	return (error);
4048fd7a8150SMike Barcroft }
4049fd7a8150SMike Barcroft 
4050f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
4051f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4052f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
4053461167c2SPawel Jakub Dawidek 
4054461167c2SPawel Jakub Dawidek static int
4055461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4056461167c2SPawel Jakub Dawidek {
4057461167c2SPawel Jakub Dawidek 	int error, injail;
4058461167c2SPawel Jakub Dawidek 
4059461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
4060461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4061461167c2SPawel Jakub Dawidek 
4062461167c2SPawel Jakub Dawidek 	return (error);
4063461167c2SPawel Jakub Dawidek }
40640304c731SJamie Gritton 
4065f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4066f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4067f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
4068413628a7SBjoern A. Zeeb 
40690304c731SJamie Gritton #if defined(INET) || defined(INET6)
4070e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
40710304c731SJamie Gritton     &jail_max_af_ips, 0,
40720304c731SJamie Gritton     "Number of IP addresses a jail may have at most per address family");
40730304c731SJamie Gritton #endif
40740304c731SJamie Gritton 
40750304c731SJamie Gritton /*
40760304c731SJamie Gritton  * Default parameters for jail(2) compatability.  For historical reasons,
40770304c731SJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
40780304c731SJamie Gritton  * just see their own parameters, and can't change them.
40790304c731SJamie Gritton  */
40800304c731SJamie Gritton static int
40810304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
40820304c731SJamie Gritton {
40830304c731SJamie Gritton 	struct prison *pr;
40840304c731SJamie Gritton 	int allow, error, i;
40850304c731SJamie Gritton 
40860304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
40870304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
40880304c731SJamie Gritton 
40890304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
40900304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
40910304c731SJamie Gritton 	if (arg1 != NULL)
40920304c731SJamie Gritton 		i = !i;
40930304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
40940304c731SJamie Gritton 	if (error || !req->newptr)
40950304c731SJamie Gritton 		return (error);
40960304c731SJamie Gritton 	i = i ? arg2 : 0;
40970304c731SJamie Gritton 	if (arg1 != NULL)
40980304c731SJamie Gritton 		i ^= arg2;
40990304c731SJamie Gritton 	/*
41000304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
41010304c731SJamie Gritton 	 * for writing.
41020304c731SJamie Gritton 	 */
41030304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
41040304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
41050304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
41060304c731SJamie Gritton 	return (0);
41070304c731SJamie Gritton }
41080304c731SJamie Gritton 
41090304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
41100304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41110304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
41120304c731SJamie Gritton     "Processes in jail can set their hostnames");
41130304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
41140304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41150304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
41160304c731SJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only");
41170304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
41180304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41190304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
41200304c731SJamie Gritton     "Processes in jail can use System V IPC primitives");
41210304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
41220304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41230304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
41240304c731SJamie Gritton     "Prison root can create raw sockets");
41250304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
41260304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41270304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
41280304c731SJamie Gritton     "Processes in jail can alter system file flags");
41290304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
41300304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41310304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
41320304c731SJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems");
41330304c731SJamie Gritton 
41340304c731SJamie Gritton static int
41350304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
41360304c731SJamie Gritton {
41370304c731SJamie Gritton 	struct prison *pr;
41380304c731SJamie Gritton 	int level, error;
41390304c731SJamie Gritton 
41400304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
41410304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
41420304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
41430304c731SJamie Gritton 	if (error || !req->newptr)
41440304c731SJamie Gritton 		return (error);
41450304c731SJamie Gritton 	*(int *)arg1 = level;
41460304c731SJamie Gritton 	return (0);
41470304c731SJamie Gritton }
41480304c731SJamie Gritton 
41490304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
41500304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41510304c731SJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
41520304c731SJamie Gritton     sysctl_jail_default_level, "I",
41530304c731SJamie Gritton     "Processes in jail cannot see all mounted file systems");
41540304c731SJamie Gritton 
41550304c731SJamie Gritton /*
41560304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
41570304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
41580304c731SJamie Gritton  * to make themselves and their types known.
41590304c731SJamie Gritton  */
41600304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
41610304c731SJamie Gritton     "Jail parameters");
41620304c731SJamie Gritton 
41630304c731SJamie Gritton int
41640304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
41650304c731SJamie Gritton {
41660304c731SJamie Gritton 	int i;
41670304c731SJamie Gritton 	long l;
41680304c731SJamie Gritton 	size_t s;
41690304c731SJamie Gritton 	char numbuf[12];
41700304c731SJamie Gritton 
41710304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
41720304c731SJamie Gritton 	{
41730304c731SJamie Gritton 	case CTLTYPE_LONG:
41740304c731SJamie Gritton 	case CTLTYPE_ULONG:
41750304c731SJamie Gritton 		l = 0;
41760304c731SJamie Gritton #ifdef SCTL_MASK32
41770304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
41780304c731SJamie Gritton #endif
41790304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
41800304c731SJamie Gritton 	case CTLTYPE_INT:
41810304c731SJamie Gritton 	case CTLTYPE_UINT:
41820304c731SJamie Gritton 		i = 0;
41830304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
41840304c731SJamie Gritton 	case CTLTYPE_STRING:
41850304c731SJamie Gritton 		snprintf(numbuf, sizeof(numbuf), "%d", arg2);
41860304c731SJamie Gritton 		return
41870304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
41880304c731SJamie Gritton 	case CTLTYPE_STRUCT:
41890304c731SJamie Gritton 		s = (size_t)arg2;
41900304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
41910304c731SJamie Gritton 	}
41920304c731SJamie Gritton 	return (0);
41930304c731SJamie Gritton }
41940304c731SJamie Gritton 
41950304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
41960304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
41970304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
41980304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
41990304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
42000304c731SJamie Gritton     "I", "Jail secure level");
42010304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
42020304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
42030304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
42040304c731SJamie Gritton     "B", "Jail persistence");
4205679e1390SJamie Gritton #ifdef VIMAGE
4206679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
42077cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4208679e1390SJamie Gritton #endif
42090304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
42100304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
42110304c731SJamie Gritton 
4212b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4213b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4214b97457e2SJamie Gritton     "I", "Current number of child jails");
4215b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4216b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4217b97457e2SJamie Gritton 
42187cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
42190304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
42200304c731SJamie Gritton     "Jail hostname");
422176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
422276ca6f88SJamie Gritton     "Jail NIS domainname");
422376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
422476ca6f88SJamie Gritton     "Jail host UUID");
422576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
422676ca6f88SJamie Gritton     "LU", "Jail host ID");
42270304c731SJamie Gritton 
42280304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
42290304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
42300304c731SJamie Gritton 
42310304c731SJamie Gritton #ifdef INET
42322b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
42332b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
42340304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
42350304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4236592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4237592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4238592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
42390304c731SJamie Gritton #endif
42400304c731SJamie Gritton #ifdef INET6
42412b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
42422b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
42430304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
42440304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4245592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4246592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4247592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
42480304c731SJamie Gritton #endif
42490304c731SJamie Gritton 
42500304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
42510304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
42520304c731SJamie Gritton     "B", "Jail may set hostname");
42530304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
42540304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
42550304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
42560304c731SJamie Gritton     "B", "Jail may create raw sockets");
42570304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
42580304c731SJamie Gritton     "B", "Jail may alter system file flags");
42590304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
42600304c731SJamie Gritton     "B", "Jail may mount/unmount jail-friendly file systems");
42610304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
42620304c731SJamie Gritton     "B", "Jail may set file quotas");
42630304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
42640304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
42650304c731SJamie Gritton 
42660304c731SJamie Gritton 
4267413628a7SBjoern A. Zeeb #ifdef DDB
4268b38ff370SJamie Gritton 
4269b38ff370SJamie Gritton static void
4270b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4271413628a7SBjoern A. Zeeb {
42720304c731SJamie Gritton 	int fi;
4273b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4274b38ff370SJamie Gritton 	int ii;
4275413628a7SBjoern A. Zeeb #endif
42767cbf7213SJamie Gritton 	unsigned jsf;
4277413628a7SBjoern A. Zeeb #ifdef INET6
4278413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4279413628a7SBjoern A. Zeeb #endif
4280413628a7SBjoern A. Zeeb 
4281b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4282b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4283b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
42840304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4285b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4286b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4287b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4288b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4289b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4290679e1390SJamie Gritton #ifdef VIMAGE
4291679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4292679e1390SJamie Gritton #endif
4293b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4294b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4295fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4296fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
42970304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
42980304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4299fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
43000304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
43010304c731SJamie Gritton 	    fi++)
43020304c731SJamie Gritton 		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
43030304c731SJamie Gritton 			db_printf(" %s", pr_flag_names[fi]);
43047cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
43057cbf7213SJamie Gritton 	    fi++) {
43067cbf7213SJamie Gritton 		jsf = pr->pr_flags &
43077cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
43087cbf7213SJamie Gritton 		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
43097cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].disable &&
43107cbf7213SJamie Gritton 		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
43117cbf7213SJamie Gritton 		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
43127cbf7213SJamie Gritton 		    : "inherit");
43137cbf7213SJamie Gritton 	}
4314fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
43150304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
43160304c731SJamie Gritton 	    fi++)
43170304c731SJamie Gritton 		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
43180304c731SJamie Gritton 			db_printf(" %s", pr_allow_names[fi]);
4319b38ff370SJamie Gritton 	db_printf("\n");
43200304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4321c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4322c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4323c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
432476ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4325413628a7SBjoern A. Zeeb #ifdef INET
4326b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4327b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4328b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4329fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
4330b38ff370SJamie Gritton 		    inet_ntoa(pr->pr_ip4[ii]));
4331413628a7SBjoern A. Zeeb #endif
4332413628a7SBjoern A. Zeeb #ifdef INET6
4333b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4334b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4335b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4336fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4337b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4338b38ff370SJamie Gritton #endif
4339b38ff370SJamie Gritton }
4340b38ff370SJamie Gritton 
4341b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4342b38ff370SJamie Gritton {
4343b38ff370SJamie Gritton 	struct prison *pr;
4344b38ff370SJamie Gritton 
4345b38ff370SJamie Gritton 	if (!have_addr) {
43460304c731SJamie Gritton 		/*
43470304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
43480304c731SJamie Gritton 		 * listed.
43490304c731SJamie Gritton 		 */
43500304c731SJamie Gritton 		db_show_prison(&prison0);
43510304c731SJamie Gritton 		if (!db_pager_quit) {
4352b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4353b38ff370SJamie Gritton 				db_show_prison(pr);
4354413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4355413628a7SBjoern A. Zeeb 					break;
4356413628a7SBjoern A. Zeeb 			}
43570304c731SJamie Gritton 		}
4358b38ff370SJamie Gritton 		return;
4359413628a7SBjoern A. Zeeb 	}
4360b38ff370SJamie Gritton 
43610304c731SJamie Gritton 	if (addr == 0)
43620304c731SJamie Gritton 		pr = &prison0;
43630304c731SJamie Gritton 	else {
4364b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4365b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4366b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4367b38ff370SJamie Gritton 				break;
4368b38ff370SJamie Gritton 		if (pr == NULL)
4369b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4370b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4371b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4372b38ff370SJamie Gritton 					break;
4373b38ff370SJamie Gritton 		if (pr == NULL)
4374b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4375b38ff370SJamie Gritton 			pr = (struct prison *)addr;
43760304c731SJamie Gritton 	}
4377b38ff370SJamie Gritton 	db_show_prison(pr);
4378b38ff370SJamie Gritton }
4379b38ff370SJamie Gritton 
4380413628a7SBjoern A. Zeeb #endif /* DDB */
4381