xref: /freebsd/sys/kern/kern_jail.c (revision 0e5e396ede602859e17686239dbb9a542f94868d)
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>
52097055e2SEdward Tomasz Napierala #include <sys/racct.h>
53a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
54dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5576ca6f88SJamie Gritton #include <sys/sysent.h>
56fd7a8150SMike Barcroft #include <sys/namei.h>
57820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
58fd7a8150SMike Barcroft #include <sys/queue.h>
5975c13541SPoul-Henning Kamp #include <sys/socket.h>
60fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6183f1e257SRobert Watson #include <sys/sysctl.h>
62fd7a8150SMike Barcroft #include <sys/vnode.h>
63530c0060SRobert Watson 
6475c13541SPoul-Henning Kamp #include <net/if.h>
6576039bc8SGleb Smirnoff #include <net/if_var.h>
66530c0060SRobert Watson #include <net/vnet.h>
67530c0060SRobert Watson 
6875c13541SPoul-Henning Kamp #include <netinet/in.h>
69530c0060SRobert Watson 
70413628a7SBjoern A. Zeeb #ifdef DDB
71413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
72413628a7SBjoern A. Zeeb #ifdef INET6
73413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h>
74413628a7SBjoern A. Zeeb #endif /* INET6 */
75413628a7SBjoern A. Zeeb #endif /* DDB */
7675c13541SPoul-Henning Kamp 
77aed55708SRobert Watson #include <security/mac/mac_framework.h>
78aed55708SRobert Watson 
798986e3a0SJamie Gritton #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
808986e3a0SJamie Gritton 
8175c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
82d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8375c13541SPoul-Henning Kamp 
84592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
85592bcae8SBjoern A. Zeeb #ifdef INET
86592bcae8SBjoern A. Zeeb #ifdef INET6
87592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
88592bcae8SBjoern A. Zeeb #else
89592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
90592bcae8SBjoern A. Zeeb #endif
91592bcae8SBjoern A. Zeeb #else /* !INET */
92592bcae8SBjoern A. Zeeb #ifdef INET6
93592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
94592bcae8SBjoern A. Zeeb #else
95592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
96592bcae8SBjoern A. Zeeb #endif
97592bcae8SBjoern A. Zeeb #endif
98592bcae8SBjoern A. Zeeb 
990304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
1000304c731SJamie Gritton struct prison prison0 = {
1010304c731SJamie Gritton 	.pr_id		= 0,
1020304c731SJamie Gritton 	.pr_name	= "0",
1030304c731SJamie Gritton 	.pr_ref		= 1,
1040304c731SJamie Gritton 	.pr_uref	= 1,
1050304c731SJamie Gritton 	.pr_path	= "/",
1060304c731SJamie Gritton 	.pr_securelevel	= -1,
1070cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
108b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1098986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
11013e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
111eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
112592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
113eb79e1c7SBjoern A. Zeeb #else
114592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
115eb79e1c7SBjoern A. Zeeb #endif
1160304c731SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL,
1170304c731SJamie Gritton };
1180304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11983f1e257SRobert Watson 
120a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
121dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
122b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
123b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
124a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1252110d913SXin LI int	lastprid = 0;
126fd7a8150SMike Barcroft 
127b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
128b3059e09SRobert Watson static void prison_complete(void *context, int pending);
129b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1300304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1310304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
132a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
133a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
134c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
135a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
136a7ad07bfSEdward Tomasz Napierala #endif
137413628a7SBjoern A. Zeeb #ifdef INET
1380d168b8dSGleb Smirnoff static int _prison_check_ip4(const struct prison *, const struct in_addr *);
1390304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
140413628a7SBjoern A. Zeeb #endif
141413628a7SBjoern A. Zeeb #ifdef INET6
1428571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
1430304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
144413628a7SBjoern A. Zeeb #endif
145fd7a8150SMike Barcroft 
146b38ff370SJamie Gritton /* Flags for prison_deref */
147b38ff370SJamie Gritton #define	PD_DEREF	0x01
148b38ff370SJamie Gritton #define	PD_DEUREF	0x02
149b38ff370SJamie Gritton #define	PD_LOCKED	0x04
150b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
151b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
152fd7a8150SMike Barcroft 
1530304c731SJamie Gritton /*
1545cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1555cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1565cc70397SBjoern A. Zeeb  * terminating entry.
1570304c731SJamie Gritton  */
1580304c731SJamie Gritton static char *pr_flag_names[] = {
1590304c731SJamie Gritton 	[0] = "persist",
160592bcae8SBjoern A. Zeeb #ifdef INET
161592bcae8SBjoern A. Zeeb 	[7] = "ip4.saddrsel",
162592bcae8SBjoern A. Zeeb #endif
163592bcae8SBjoern A. Zeeb #ifdef INET6
164592bcae8SBjoern A. Zeeb 	[8] = "ip6.saddrsel",
165592bcae8SBjoern A. Zeeb #endif
1660304c731SJamie Gritton };
1675cc70397SBjoern A. Zeeb const size_t pr_flag_names_size = sizeof(pr_flag_names);
1680304c731SJamie Gritton 
1690304c731SJamie Gritton static char *pr_flag_nonames[] = {
1700304c731SJamie Gritton 	[0] = "nopersist",
171592bcae8SBjoern A. Zeeb #ifdef INET
172592bcae8SBjoern A. Zeeb 	[7] = "ip4.nosaddrsel",
173592bcae8SBjoern A. Zeeb #endif
174592bcae8SBjoern A. Zeeb #ifdef INET6
175592bcae8SBjoern A. Zeeb 	[8] = "ip6.nosaddrsel",
176592bcae8SBjoern A. Zeeb #endif
1777cbf7213SJamie Gritton };
1785cc70397SBjoern A. Zeeb const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
1797cbf7213SJamie Gritton 
1807cbf7213SJamie Gritton struct jailsys_flags {
1817cbf7213SJamie Gritton 	const char	*name;
1827cbf7213SJamie Gritton 	unsigned	 disable;
1837cbf7213SJamie Gritton 	unsigned	 new;
1847cbf7213SJamie Gritton } pr_flag_jailsys[] = {
1857cbf7213SJamie Gritton 	{ "host", 0, PR_HOST },
1867cbf7213SJamie Gritton #ifdef VIMAGE
1877cbf7213SJamie Gritton 	{ "vnet", 0, PR_VNET },
1887cbf7213SJamie Gritton #endif
1890304c731SJamie Gritton #ifdef INET
1907cbf7213SJamie Gritton 	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
1910304c731SJamie Gritton #endif
1920304c731SJamie Gritton #ifdef INET6
1937cbf7213SJamie Gritton 	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
194679e1390SJamie Gritton #endif
1950304c731SJamie Gritton };
1965cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1970304c731SJamie Gritton 
1980304c731SJamie Gritton static char *pr_allow_names[] = {
1990304c731SJamie Gritton 	"allow.set_hostname",
2000304c731SJamie Gritton 	"allow.sysvipc",
2010304c731SJamie Gritton 	"allow.raw_sockets",
2020304c731SJamie Gritton 	"allow.chflags",
2030304c731SJamie Gritton 	"allow.mount",
2040304c731SJamie Gritton 	"allow.quotas",
2050304c731SJamie Gritton 	"allow.socket_af",
206bf3db8aaSMartin Matuska 	"allow.mount.devfs",
207bf3db8aaSMartin Matuska 	"allow.mount.nullfs",
208e7af90abSMartin Matuska 	"allow.mount.zfs",
20941c0675eSMartin Matuska 	"allow.mount.procfs",
2102454886eSXin LI 	"allow.mount.tmpfs",
2110304c731SJamie Gritton };
2125cc70397SBjoern A. Zeeb const size_t pr_allow_names_size = sizeof(pr_allow_names);
2130304c731SJamie Gritton 
2140304c731SJamie Gritton static char *pr_allow_nonames[] = {
2150304c731SJamie Gritton 	"allow.noset_hostname",
2160304c731SJamie Gritton 	"allow.nosysvipc",
2170304c731SJamie Gritton 	"allow.noraw_sockets",
2180304c731SJamie Gritton 	"allow.nochflags",
2190304c731SJamie Gritton 	"allow.nomount",
2200304c731SJamie Gritton 	"allow.noquotas",
2210304c731SJamie Gritton 	"allow.nosocket_af",
222bf3db8aaSMartin Matuska 	"allow.mount.nodevfs",
223bf3db8aaSMartin Matuska 	"allow.mount.nonullfs",
224e7af90abSMartin Matuska 	"allow.mount.nozfs",
22541c0675eSMartin Matuska 	"allow.mount.noprocfs",
2262454886eSXin LI 	"allow.mount.notmpfs",
2270304c731SJamie Gritton };
2285cc70397SBjoern A. Zeeb const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
2290304c731SJamie Gritton 
2300304c731SJamie Gritton #define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
23142bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
232bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2330304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
23442bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2350cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2360304c731SJamie Gritton #if defined(INET) || defined(INET6)
237e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2380304c731SJamie Gritton #endif
2390304c731SJamie Gritton 
240413628a7SBjoern A. Zeeb #ifdef INET
241413628a7SBjoern A. Zeeb static int
242413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2)
243413628a7SBjoern A. Zeeb {
244413628a7SBjoern A. Zeeb 	in_addr_t iaa, iab;
245413628a7SBjoern A. Zeeb 
246413628a7SBjoern A. Zeeb 	/*
247413628a7SBjoern A. Zeeb 	 * We need to compare in HBO here to get the list sorted as expected
248413628a7SBjoern A. Zeeb 	 * by the result of the code.  Sorting NBO addresses gives you
249413628a7SBjoern A. Zeeb 	 * interesting results.  If you do not understand, do not try.
250413628a7SBjoern A. Zeeb 	 */
251413628a7SBjoern A. Zeeb 	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
252413628a7SBjoern A. Zeeb 	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
253413628a7SBjoern A. Zeeb 
254413628a7SBjoern A. Zeeb 	/*
255413628a7SBjoern A. Zeeb 	 * Do not simply return the difference of the two numbers, the int is
256413628a7SBjoern A. Zeeb 	 * not wide enough.
257413628a7SBjoern A. Zeeb 	 */
258413628a7SBjoern A. Zeeb 	if (iaa > iab)
259413628a7SBjoern A. Zeeb 		return (1);
260413628a7SBjoern A. Zeeb 	else if (iaa < iab)
261413628a7SBjoern A. Zeeb 		return (-1);
262413628a7SBjoern A. Zeeb 	else
263413628a7SBjoern A. Zeeb 		return (0);
264413628a7SBjoern A. Zeeb }
265413628a7SBjoern A. Zeeb #endif
266413628a7SBjoern A. Zeeb 
267413628a7SBjoern A. Zeeb #ifdef INET6
268413628a7SBjoern A. Zeeb static int
269413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2)
270413628a7SBjoern A. Zeeb {
271413628a7SBjoern A. Zeeb 	const struct in6_addr *ia6a, *ia6b;
272413628a7SBjoern A. Zeeb 	int i, rc;
273413628a7SBjoern A. Zeeb 
274413628a7SBjoern A. Zeeb 	ia6a = (const struct in6_addr *)ip1;
275413628a7SBjoern A. Zeeb 	ia6b = (const struct in6_addr *)ip2;
276413628a7SBjoern A. Zeeb 
277413628a7SBjoern A. Zeeb 	rc = 0;
278413628a7SBjoern A. Zeeb 	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
279413628a7SBjoern A. Zeeb 		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
280413628a7SBjoern A. Zeeb 			rc = 1;
281413628a7SBjoern A. Zeeb 		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
282413628a7SBjoern A. Zeeb 			rc = -1;
283413628a7SBjoern A. Zeeb 	}
284413628a7SBjoern A. Zeeb 	return (rc);
285413628a7SBjoern A. Zeeb }
286413628a7SBjoern A. Zeeb #endif
287413628a7SBjoern A. Zeeb 
288116734c4SMatthew Dillon /*
2899ddb7954SMike Barcroft  * struct jail_args {
2909ddb7954SMike Barcroft  *	struct jail *jail;
2919ddb7954SMike Barcroft  * };
292116734c4SMatthew Dillon  */
29375c13541SPoul-Henning Kamp int
2948451d0ddSKip Macy sys_jail(struct thread *td, struct jail_args *uap)
29575c13541SPoul-Henning Kamp {
296413628a7SBjoern A. Zeeb 	uint32_t version;
297413628a7SBjoern A. Zeeb 	int error;
2980304c731SJamie Gritton 	struct jail j;
299413628a7SBjoern A. Zeeb 
300413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
301413628a7SBjoern A. Zeeb 	if (error)
302413628a7SBjoern A. Zeeb 		return (error);
303413628a7SBjoern A. Zeeb 
304413628a7SBjoern A. Zeeb 	switch (version) {
305413628a7SBjoern A. Zeeb 	case 0:
306413628a7SBjoern A. Zeeb 	{
307413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
308413628a7SBjoern A. Zeeb 
3090304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
3100304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
311413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
312413628a7SBjoern A. Zeeb 		if (error)
313413628a7SBjoern A. Zeeb 			return (error);
3140304c731SJamie Gritton 		j.version = j0.version;
3150304c731SJamie Gritton 		j.path = j0.path;
3160304c731SJamie Gritton 		j.hostname = j0.hostname;
317b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
318413628a7SBjoern A. Zeeb 		break;
319413628a7SBjoern A. Zeeb 	}
320413628a7SBjoern A. Zeeb 
321413628a7SBjoern A. Zeeb 	case 1:
322413628a7SBjoern A. Zeeb 		/*
323413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
324413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
325413628a7SBjoern A. Zeeb 		 */
326413628a7SBjoern A. Zeeb 		return (EINVAL);
327413628a7SBjoern A. Zeeb 
328413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
329413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
330413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
331413628a7SBjoern A. Zeeb 		if (error)
332413628a7SBjoern A. Zeeb 			return (error);
3330304c731SJamie Gritton 		break;
3340304c731SJamie Gritton 
3350304c731SJamie Gritton 	default:
3360304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3370304c731SJamie Gritton 		return (EINVAL);
3380304c731SJamie Gritton 	}
3390304c731SJamie Gritton 	return (kern_jail(td, &j));
3400304c731SJamie Gritton }
3410304c731SJamie Gritton 
3420304c731SJamie Gritton int
3430304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j)
3440304c731SJamie Gritton {
345e92e0574SJamie Gritton 	struct iovec optiov[2 * (4
346e92e0574SJamie Gritton 			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
347e92e0574SJamie Gritton #ifdef INET
348e92e0574SJamie Gritton 			    + 1
349e92e0574SJamie Gritton #endif
350e92e0574SJamie Gritton #ifdef INET6
351e92e0574SJamie Gritton 			    + 1
352e92e0574SJamie Gritton #endif
353e92e0574SJamie Gritton 			    )];
3540304c731SJamie Gritton 	struct uio opt;
3550304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
3560304c731SJamie Gritton #ifdef INET
357e92e0574SJamie Gritton 	uint32_t ip4s;
3580304c731SJamie Gritton 	struct in_addr *u_ip4;
3590304c731SJamie Gritton #endif
3600304c731SJamie Gritton #ifdef INET6
3610304c731SJamie Gritton 	struct in6_addr *u_ip6;
3620304c731SJamie Gritton #endif
3630304c731SJamie Gritton 	size_t tmplen;
3640304c731SJamie Gritton 	int error, enforce_statfs, fi;
3650304c731SJamie Gritton 
3660304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3670304c731SJamie Gritton 	opt.uio_iov = optiov;
3680304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3690304c731SJamie Gritton 	opt.uio_offset = -1;
3700304c731SJamie Gritton 	opt.uio_resid = -1;
3710304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3720304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3730304c731SJamie Gritton 	opt.uio_td = td;
3740304c731SJamie Gritton 
3750304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3760304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
3770304c731SJamie Gritton 		for (fi = 0; fi < sizeof(pr_allow_names) /
3780304c731SJamie Gritton 		     sizeof(pr_allow_names[0]); fi++) {
3790304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_base =
3800304c731SJamie Gritton 			    (jail_default_allow & (1 << fi))
3810304c731SJamie Gritton 			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
3820304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3830304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3840304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3850304c731SJamie Gritton 		}
3860304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3870304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3880304c731SJamie Gritton 		opt.uio_iovcnt++;
3890304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3900304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3910304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3920304c731SJamie Gritton 		opt.uio_iovcnt++;
3930304c731SJamie Gritton 	}
3940304c731SJamie Gritton 
395b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
396b38ff370SJamie Gritton #ifdef INET
3970304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3980304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
399b38ff370SJamie Gritton 		return (EINVAL);
4000304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
401b38ff370SJamie Gritton #else
4020304c731SJamie Gritton 	if (j->ip4s > 0)
403b38ff370SJamie Gritton 		return (EINVAL);
404b38ff370SJamie Gritton #endif
405b38ff370SJamie Gritton #ifdef INET6
4060304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
407b38ff370SJamie Gritton 		return (EINVAL);
4080304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
409b38ff370SJamie Gritton #else
4100304c731SJamie Gritton 	if (j->ip6s > 0)
411b38ff370SJamie Gritton 		return (EINVAL);
412b38ff370SJamie Gritton #endif
413b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
414b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
415b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
416b38ff370SJamie Gritton #ifdef INET
417b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
418b38ff370SJamie Gritton #endif
419b38ff370SJamie Gritton #ifdef INET6
420b38ff370SJamie Gritton #ifdef INET
4210304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
422b38ff370SJamie Gritton #else
423b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
424b38ff370SJamie Gritton #endif
425b38ff370SJamie Gritton #endif
4260304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
4270304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
4280304c731SJamie Gritton 	opt.uio_iovcnt++;
4290304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
4300304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
4310304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
432b38ff370SJamie Gritton 	if (error) {
433b38ff370SJamie Gritton 		free(u_path, M_TEMP);
434b38ff370SJamie Gritton 		return (error);
435b38ff370SJamie Gritton 	}
4360304c731SJamie Gritton 	opt.uio_iovcnt++;
4370304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
4380304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4390304c731SJamie Gritton 	opt.uio_iovcnt++;
4400304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4410304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4420304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
443b38ff370SJamie Gritton 	if (error) {
444b38ff370SJamie Gritton 		free(u_path, M_TEMP);
445b38ff370SJamie Gritton 		return (error);
446b38ff370SJamie Gritton 	}
4470304c731SJamie Gritton 	opt.uio_iovcnt++;
4480304c731SJamie Gritton 	if (j->jailname != NULL) {
449b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
450b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
451b38ff370SJamie Gritton 		opt.uio_iovcnt++;
452b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4530304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
454b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
455b38ff370SJamie Gritton 		if (error) {
456b38ff370SJamie Gritton 			free(u_path, M_TEMP);
457b38ff370SJamie Gritton 			return (error);
458b38ff370SJamie Gritton 		}
459b38ff370SJamie Gritton 		opt.uio_iovcnt++;
460b38ff370SJamie Gritton 	}
461b38ff370SJamie Gritton #ifdef INET
462b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
463b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
464b38ff370SJamie Gritton 	opt.uio_iovcnt++;
465b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4660304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4670304c731SJamie Gritton 	if (j->version == 0)
4680304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4690304c731SJamie Gritton 	else {
4700304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
471b38ff370SJamie Gritton 		if (error) {
472b38ff370SJamie Gritton 			free(u_path, M_TEMP);
473b38ff370SJamie Gritton 			return (error);
474b38ff370SJamie Gritton 		}
4750304c731SJamie Gritton 	}
476b38ff370SJamie Gritton 	opt.uio_iovcnt++;
477b38ff370SJamie Gritton #endif
478b38ff370SJamie Gritton #ifdef INET6
479b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
480b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
481b38ff370SJamie Gritton 	opt.uio_iovcnt++;
482b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4830304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4840304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
485b38ff370SJamie Gritton 	if (error) {
486b38ff370SJamie Gritton 		free(u_path, M_TEMP);
487b38ff370SJamie Gritton 		return (error);
488b38ff370SJamie Gritton 	}
489b38ff370SJamie Gritton 	opt.uio_iovcnt++;
490b38ff370SJamie Gritton #endif
4910304c731SJamie Gritton 	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
4920304c731SJamie Gritton 	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
493b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
494b38ff370SJamie Gritton 	free(u_path, M_TEMP);
495b38ff370SJamie Gritton 	return (error);
496b38ff370SJamie Gritton }
497b38ff370SJamie Gritton 
4980304c731SJamie Gritton 
499b38ff370SJamie Gritton /*
500b38ff370SJamie Gritton  * struct jail_set_args {
501b38ff370SJamie Gritton  *	struct iovec *iovp;
502b38ff370SJamie Gritton  *	unsigned int iovcnt;
503b38ff370SJamie Gritton  *	int flags;
504b38ff370SJamie Gritton  * };
505b38ff370SJamie Gritton  */
506b38ff370SJamie Gritton int
5078451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
508b38ff370SJamie Gritton {
509b38ff370SJamie Gritton 	struct uio *auio;
510b38ff370SJamie Gritton 	int error;
511b38ff370SJamie Gritton 
512b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
513b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
514b38ff370SJamie Gritton 		return (EINVAL);
515b38ff370SJamie Gritton 
516b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
517b38ff370SJamie Gritton 	if (error)
518b38ff370SJamie Gritton 		return (error);
519b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
520b38ff370SJamie Gritton 	free(auio, M_IOV);
521b38ff370SJamie Gritton 	return (error);
522413628a7SBjoern A. Zeeb }
523413628a7SBjoern A. Zeeb 
524413628a7SBjoern A. Zeeb int
525b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
526413628a7SBjoern A. Zeeb {
527fd7a8150SMike Barcroft 	struct nameidata nd;
528b38ff370SJamie Gritton #ifdef INET
529b38ff370SJamie Gritton 	struct in_addr *ip4;
530413628a7SBjoern A. Zeeb #endif
531b38ff370SJamie Gritton #ifdef INET6
532b38ff370SJamie Gritton 	struct in6_addr *ip6;
533b38ff370SJamie Gritton #endif
534b38ff370SJamie Gritton 	struct vfsopt *opt;
535b38ff370SJamie Gritton 	struct vfsoptlist *opts;
53657aea6dfSBjoern A. Zeeb 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
537b38ff370SJamie Gritton 	struct vnode *root;
538babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
5396dfe0a3dSMartin Matuska 	char *g_path;
5400304c731SJamie Gritton #if defined(INET) || defined(INET6)
54157aea6dfSBjoern A. Zeeb 	struct prison *tppr;
542b38ff370SJamie Gritton 	void *op;
5430304c731SJamie Gritton #endif
54476ca6f88SJamie Gritton 	unsigned long hid;
5450304c731SJamie Gritton 	size_t namelen, onamelen;
5460304c731SJamie Gritton 	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
5470cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
5487cbf7213SJamie Gritton 	int fi, jid, jsys, len, level;
5495050aa86SKonstantin Belousov 	int childmax, rsnum, slevel;
550f6e633a9SMartin Matuska 	int fullpath_disabled;
551d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
5520304c731SJamie Gritton 	int ii, ij;
553413628a7SBjoern A. Zeeb #endif
554413628a7SBjoern A. Zeeb #ifdef INET
5552b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
556413628a7SBjoern A. Zeeb #endif
557b38ff370SJamie Gritton #ifdef INET6
5582b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
559b38ff370SJamie Gritton #endif
5606beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
5616beb3bb4SKirk McKusick 	unsigned tallow;
562b38ff370SJamie Gritton 	char numbuf[12];
563b38ff370SJamie Gritton 
564b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
565b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
566b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
567b38ff370SJamie Gritton 	if (error)
56875c13541SPoul-Henning Kamp 		return (error);
5690304c731SJamie Gritton 	mypr = ppr = td->td_ucred->cr_prison;
570b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5710304c731SJamie Gritton 		return (EPERM);
572b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
573b38ff370SJamie Gritton 		return (EINVAL);
574b38ff370SJamie Gritton 
575b38ff370SJamie Gritton 	/*
576b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
577b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
578b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
579b38ff370SJamie Gritton 	 * the allprison lock.
580b38ff370SJamie Gritton 	 *
581b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
582b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
583b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
584b38ff370SJamie Gritton 	 */
585b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
586b38ff370SJamie Gritton 	if (error)
587b38ff370SJamie Gritton 		return (error);
588b38ff370SJamie Gritton #ifdef INET
589b38ff370SJamie Gritton 	ip4 = NULL;
590b38ff370SJamie Gritton #endif
591b38ff370SJamie Gritton #ifdef INET6
592b38ff370SJamie Gritton 	ip6 = NULL;
593b38ff370SJamie Gritton #endif
5946dfe0a3dSMartin Matuska 	g_path = NULL;
595b38ff370SJamie Gritton 
596b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
597b38ff370SJamie Gritton 	if (error == ENOENT)
598b38ff370SJamie Gritton 		jid = 0;
599b38ff370SJamie Gritton 	else if (error != 0)
600b38ff370SJamie Gritton 		goto done_free;
601b38ff370SJamie Gritton 
602b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
603b38ff370SJamie Gritton 	if (error == ENOENT)
604b38ff370SJamie Gritton 		gotslevel = 0;
605b38ff370SJamie Gritton 	else if (error != 0)
606b38ff370SJamie Gritton 		goto done_free;
607b38ff370SJamie Gritton 	else
608b38ff370SJamie Gritton 		gotslevel = 1;
609b38ff370SJamie Gritton 
610b97457e2SJamie Gritton 	error =
611b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
612b97457e2SJamie Gritton 	if (error == ENOENT)
613b97457e2SJamie Gritton 		gotchildmax = 0;
614b97457e2SJamie Gritton 	else if (error != 0)
615b97457e2SJamie Gritton 		goto done_free;
616b97457e2SJamie Gritton 	else
617b97457e2SJamie Gritton 		gotchildmax = 1;
618b97457e2SJamie Gritton 
6190304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
620f337198dSJamie Gritton 	if (error == ENOENT)
621f337198dSJamie Gritton 		gotenforce = 0;
622f337198dSJamie Gritton 	else if (error != 0)
6230304c731SJamie Gritton 		goto done_free;
624f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
625f337198dSJamie Gritton 		error = EINVAL;
626f337198dSJamie Gritton 		goto done_free;
627f337198dSJamie Gritton 	} else
628f337198dSJamie Gritton 		gotenforce = 1;
6290304c731SJamie Gritton 
6300cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
6310cc207a6SMartin Matuska 	if (error == ENOENT)
6320cc207a6SMartin Matuska 		gotrsnum = 0;
6330cc207a6SMartin Matuska 	else if (error != 0)
6340cc207a6SMartin Matuska 		goto done_free;
6350cc207a6SMartin Matuska 	else
6360cc207a6SMartin Matuska 		gotrsnum = 1;
6370cc207a6SMartin Matuska 
638b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
6390304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
6400304c731SJamie Gritton 	    fi++) {
6410304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
6420304c731SJamie Gritton 			continue;
6430304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
6440304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
6450304c731SJamie Gritton 	}
646b38ff370SJamie Gritton 	ch_flags |= pr_flags;
6477cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
6487cbf7213SJamie Gritton 	    fi++) {
6497cbf7213SJamie Gritton 		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
6507cbf7213SJamie Gritton 		    sizeof(jsys));
6517cbf7213SJamie Gritton 		if (error == ENOENT)
6527cbf7213SJamie Gritton 			continue;
6537cbf7213SJamie Gritton 		if (error != 0)
6547cbf7213SJamie Gritton 			goto done_free;
6557cbf7213SJamie Gritton 		switch (jsys) {
6567cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
6577cbf7213SJamie Gritton 			if (!pr_flag_jailsys[fi].disable) {
6587cbf7213SJamie Gritton 				error = EINVAL;
6597cbf7213SJamie Gritton 				goto done_free;
6607cbf7213SJamie Gritton 			}
6617cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].disable;
6627cbf7213SJamie Gritton 			break;
6637cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
6647cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].new;
6657cbf7213SJamie Gritton 			break;
6667cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6677cbf7213SJamie Gritton 			break;
6687cbf7213SJamie Gritton 		default:
6697cbf7213SJamie Gritton 			error = EINVAL;
6707cbf7213SJamie Gritton 			goto done_free;
6717cbf7213SJamie Gritton 		}
6727cbf7213SJamie Gritton 		ch_flags |=
6737cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
6747cbf7213SJamie Gritton 	}
6754affa14cSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
6764affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6774affa14cSJamie Gritton 		error = EINVAL;
6784affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6794affa14cSJamie Gritton 		goto done_errmsg;
6804affa14cSJamie Gritton 	}
681679e1390SJamie Gritton #ifdef VIMAGE
682679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
683679e1390SJamie Gritton 		error = EINVAL;
684679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
685679e1390SJamie Gritton 		goto done_errmsg;
686679e1390SJamie Gritton 	}
687679e1390SJamie Gritton #endif
6882b0d6f81SJamie Gritton #ifdef INET
6892b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
6902b0d6f81SJamie Gritton 		error = EINVAL;
6912b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
6922b0d6f81SJamie Gritton 		goto done_errmsg;
6932b0d6f81SJamie Gritton 	}
6942b0d6f81SJamie Gritton #endif
6952b0d6f81SJamie Gritton #ifdef INET6
6962b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
6972b0d6f81SJamie Gritton 		error = EINVAL;
6982b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
6992b0d6f81SJamie Gritton 		goto done_errmsg;
7002b0d6f81SJamie Gritton 	}
7012b0d6f81SJamie Gritton #endif
702b38ff370SJamie Gritton 
7030304c731SJamie Gritton 	pr_allow = ch_allow = 0;
7040304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
7050304c731SJamie Gritton 	    fi++) {
7060304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
7070304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
7080304c731SJamie Gritton 	}
7090304c731SJamie Gritton 	ch_allow |= pr_allow;
7100304c731SJamie Gritton 
711b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
712b38ff370SJamie Gritton 	if (error == ENOENT)
713b38ff370SJamie Gritton 		name = NULL;
714b38ff370SJamie Gritton 	else if (error != 0)
715b38ff370SJamie Gritton 		goto done_free;
716b38ff370SJamie Gritton 	else {
717b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
718b38ff370SJamie Gritton 			error = EINVAL;
719b38ff370SJamie Gritton 			goto done_free;
720b38ff370SJamie Gritton 		}
721b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
722b38ff370SJamie Gritton 			error = ENAMETOOLONG;
723b38ff370SJamie Gritton 			goto done_free;
724b38ff370SJamie Gritton 		}
725b38ff370SJamie Gritton 	}
726b38ff370SJamie Gritton 
727b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
728b38ff370SJamie Gritton 	if (error == ENOENT)
729b38ff370SJamie Gritton 		host = NULL;
730b38ff370SJamie Gritton 	else if (error != 0)
731b38ff370SJamie Gritton 		goto done_free;
732b38ff370SJamie Gritton 	else {
73376ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
73476ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
735b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
736b38ff370SJamie Gritton 			error = EINVAL;
737b38ff370SJamie Gritton 			goto done_free;
738b38ff370SJamie Gritton 		}
739b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
740b38ff370SJamie Gritton 			error = ENAMETOOLONG;
741b38ff370SJamie Gritton 			goto done_free;
742b38ff370SJamie Gritton 		}
743b38ff370SJamie Gritton 	}
744b38ff370SJamie Gritton 
74576ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
74676ca6f88SJamie Gritton 	if (error == ENOENT)
74776ca6f88SJamie Gritton 		domain = NULL;
74876ca6f88SJamie Gritton 	else if (error != 0)
74976ca6f88SJamie Gritton 		goto done_free;
75076ca6f88SJamie Gritton 	else {
75176ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
75276ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
75376ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
75476ca6f88SJamie Gritton 			error = EINVAL;
75576ca6f88SJamie Gritton 			goto done_free;
75676ca6f88SJamie Gritton 		}
75776ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
75876ca6f88SJamie Gritton 			error = ENAMETOOLONG;
75976ca6f88SJamie Gritton 			goto done_free;
76076ca6f88SJamie Gritton 		}
76176ca6f88SJamie Gritton 	}
76276ca6f88SJamie Gritton 
76376ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
76476ca6f88SJamie Gritton 	if (error == ENOENT)
76576ca6f88SJamie Gritton 		uuid = NULL;
76676ca6f88SJamie Gritton 	else if (error != 0)
76776ca6f88SJamie Gritton 		goto done_free;
76876ca6f88SJamie Gritton 	else {
76976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
77076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
77176ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
77276ca6f88SJamie Gritton 			error = EINVAL;
77376ca6f88SJamie Gritton 			goto done_free;
77476ca6f88SJamie Gritton 		}
77576ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
77676ca6f88SJamie Gritton 			error = ENAMETOOLONG;
77776ca6f88SJamie Gritton 			goto done_free;
77876ca6f88SJamie Gritton 		}
77976ca6f88SJamie Gritton 	}
78076ca6f88SJamie Gritton 
781841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
782a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
78376ca6f88SJamie Gritton 		uint32_t hid32;
78476ca6f88SJamie Gritton 
78576ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
78676ca6f88SJamie Gritton 		hid = hid32;
78776ca6f88SJamie Gritton 	} else
78876ca6f88SJamie Gritton #endif
78976ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
79076ca6f88SJamie Gritton 	if (error == ENOENT)
79176ca6f88SJamie Gritton 		gothid = 0;
79276ca6f88SJamie Gritton 	else if (error != 0)
79376ca6f88SJamie Gritton 		goto done_free;
79476ca6f88SJamie Gritton 	else {
79576ca6f88SJamie Gritton 		gothid = 1;
79676ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
79776ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
79876ca6f88SJamie Gritton 	}
79976ca6f88SJamie Gritton 
800b38ff370SJamie Gritton #ifdef INET
801b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
802b38ff370SJamie Gritton 	if (error == ENOENT)
803*0e5e396eSJamie Gritton 		ip4s = 0;
804b38ff370SJamie Gritton 	else if (error != 0)
805b38ff370SJamie Gritton 		goto done_free;
806b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
807b38ff370SJamie Gritton 		error = EINVAL;
808b38ff370SJamie Gritton 		goto done_free;
8090304c731SJamie Gritton 	} else {
8107cbf7213SJamie Gritton 		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
8117cbf7213SJamie Gritton 		if (ip4s == 0)
8127cbf7213SJamie Gritton 			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
8137cbf7213SJamie Gritton 		else {
8147cbf7213SJamie Gritton 			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
815b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
816b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
817b38ff370SJamie Gritton 				error = EINVAL;
818b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
819b38ff370SJamie Gritton 				goto done_errmsg;
820b38ff370SJamie Gritton 			}
8212b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
822b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
823b38ff370SJamie Gritton 			/*
8240304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
8250304c731SJamie Gritton 			 * the primary IP address as given from userland.
8260304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
827bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
828bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
829bef916f9SBjoern A. Zeeb 			 * address to connect from.
830b38ff370SJamie Gritton 			 */
831b38ff370SJamie Gritton 			if (ip4s > 1)
832b38ff370SJamie Gritton 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
833b38ff370SJamie Gritton 			/*
8340304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
8350304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
8360304c731SJamie Gritton 			 * addresses it is their problem.
837b38ff370SJamie Gritton 			 *
8380304c731SJamie Gritton 			 * We do not have to care about byte order for these
8390304c731SJamie Gritton 			 * checks so we will do them in NBO.
840b38ff370SJamie Gritton 			 */
841b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
842b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
843b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
844b38ff370SJamie Gritton 					error = EINVAL;
845b38ff370SJamie Gritton 					goto done_free;
846b38ff370SJamie Gritton 				}
847b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
848b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
849b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
850b38ff370SJamie Gritton 					error = EINVAL;
851b38ff370SJamie Gritton 					goto done_free;
852b38ff370SJamie Gritton 				}
853b38ff370SJamie Gritton 			}
854b38ff370SJamie Gritton 		}
8550304c731SJamie Gritton 	}
856b38ff370SJamie Gritton #endif
857b38ff370SJamie Gritton 
858b38ff370SJamie Gritton #ifdef INET6
859b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
860b38ff370SJamie Gritton 	if (error == ENOENT)
861*0e5e396eSJamie Gritton 		ip6s = 0;
862b38ff370SJamie Gritton 	else if (error != 0)
863b38ff370SJamie Gritton 		goto done_free;
864b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
865b38ff370SJamie Gritton 		error = EINVAL;
866b38ff370SJamie Gritton 		goto done_free;
8670304c731SJamie Gritton 	} else {
8687cbf7213SJamie Gritton 		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
8697cbf7213SJamie Gritton 		if (ip6s == 0)
8707cbf7213SJamie Gritton 			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
8717cbf7213SJamie Gritton 		else {
8727cbf7213SJamie Gritton 			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
873b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
874b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
875b38ff370SJamie Gritton 				error = EINVAL;
876b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
877b38ff370SJamie Gritton 				goto done_errmsg;
878b38ff370SJamie Gritton 			}
8792b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
880b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
881b38ff370SJamie Gritton 			if (ip6s > 1)
882b38ff370SJamie Gritton 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
883b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8840304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
885b38ff370SJamie Gritton 					error = EINVAL;
886b38ff370SJamie Gritton 					goto done_free;
887b38ff370SJamie Gritton 				}
888b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
889b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
890b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
891b38ff370SJamie Gritton 				{
892b38ff370SJamie Gritton 					error = EINVAL;
893b38ff370SJamie Gritton 					goto done_free;
894b38ff370SJamie Gritton 				}
895b38ff370SJamie Gritton 			}
896b38ff370SJamie Gritton 		}
8970304c731SJamie Gritton 	}
898b38ff370SJamie Gritton #endif
899b38ff370SJamie Gritton 
900bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
901bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
902bdfc8cc4SJamie Gritton 		error = EINVAL;
903bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
904bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
905bdfc8cc4SJamie Gritton 		goto done_errmsg;
906bdfc8cc4SJamie Gritton 	}
907bdfc8cc4SJamie Gritton #endif
908bdfc8cc4SJamie Gritton 
9099cbe30e1SMartin Matuska 	fullpath_disabled = 0;
910b38ff370SJamie Gritton 	root = NULL;
911b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
912b38ff370SJamie Gritton 	if (error == ENOENT)
913b38ff370SJamie Gritton 		path = NULL;
914b38ff370SJamie Gritton 	else if (error != 0)
915b38ff370SJamie Gritton 		goto done_free;
916b38ff370SJamie Gritton 	else {
917b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
918b38ff370SJamie Gritton 			error = EINVAL;
919b38ff370SJamie Gritton 			vfs_opterror(opts,
920b38ff370SJamie Gritton 			    "path cannot be changed after creation");
921b38ff370SJamie Gritton 			goto done_errmsg;
922b38ff370SJamie Gritton 		}
923b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
924b38ff370SJamie Gritton 			error = EINVAL;
925b38ff370SJamie Gritton 			goto done_free;
926b38ff370SJamie Gritton 		}
9275050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
928b38ff370SJamie Gritton 		    path, td);
929b38ff370SJamie Gritton 		error = namei(&nd);
930b38ff370SJamie Gritton 		if (error)
931b38ff370SJamie Gritton 			goto done_free;
932b38ff370SJamie Gritton 		root = nd.ni_vp;
933b38ff370SJamie Gritton 		NDFREE(&nd, NDF_ONLY_PNBUF);
9346dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
9356dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
9366dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
9376dfe0a3dSMartin Matuska 		if (error == 0)
9386dfe0a3dSMartin Matuska 			path = g_path;
9396dfe0a3dSMartin Matuska 		else if (error == ENODEV) {
940f6e633a9SMartin Matuska 			/* proceed if sysctl debug.disablefullpath == 1 */
941f6e633a9SMartin Matuska 			fullpath_disabled = 1;
942f6e633a9SMartin Matuska 			if (len < 2 || (len == 2 && path[0] == '/'))
943f6e633a9SMartin Matuska 				path = NULL;
9446dfe0a3dSMartin Matuska 		} else {
945f6e633a9SMartin Matuska 			/* exit on other errors */
946b38ff370SJamie Gritton 			goto done_free;
947b38ff370SJamie Gritton 		}
948f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
949f6e633a9SMartin Matuska 			error = ENOTDIR;
950f6e633a9SMartin Matuska 			vput(root);
951f6e633a9SMartin Matuska 			goto done_free;
952f6e633a9SMartin Matuska 		}
953f6e633a9SMartin Matuska 		VOP_UNLOCK(root, 0);
954f6e633a9SMartin Matuska 		if (fullpath_disabled) {
955f6e633a9SMartin Matuska 			/* Leave room for a real-root full pathname. */
956f6e633a9SMartin Matuska 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
957f6e633a9SMartin Matuska 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
958f6e633a9SMartin Matuska 				error = ENAMETOOLONG;
959f6e633a9SMartin Matuska 				goto done_free;
960f6e633a9SMartin Matuska 			}
961b38ff370SJamie Gritton 		}
962b38ff370SJamie Gritton 	}
963b38ff370SJamie Gritton 
964b38ff370SJamie Gritton 	/*
965b38ff370SJamie Gritton 	 * Grab the allprison lock before letting modules check their
966b38ff370SJamie Gritton 	 * parameters.  Once we have it, do not let go so we'll have a
967b38ff370SJamie Gritton 	 * consistent view of the OSD list.
968b38ff370SJamie Gritton 	 */
969b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
970b38ff370SJamie Gritton 	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
971b38ff370SJamie Gritton 	if (error)
972b38ff370SJamie Gritton 		goto done_unlock_list;
973b38ff370SJamie Gritton 
974b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
975b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
976b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
977b38ff370SJamie Gritton 			error = EINVAL;
978b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
979b38ff370SJamie Gritton 			goto done_unlock_list;
980b38ff370SJamie Gritton 		}
981b38ff370SJamie Gritton 	}
982b38ff370SJamie Gritton 
983b38ff370SJamie Gritton 	/*
984b38ff370SJamie Gritton 	 * See if we are creating a new record or updating an existing one.
985b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
986b38ff370SJamie Gritton 	 */
987b38ff370SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
988b38ff370SJamie Gritton 	if (!cuflags) {
989b38ff370SJamie Gritton 		error = EINVAL;
990b38ff370SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
991b38ff370SJamie Gritton 		goto done_unlock_list;
992b38ff370SJamie Gritton 	}
993b38ff370SJamie Gritton 	pr = NULL;
994babbbb9cSJamie Gritton 	namelc = NULL;
995babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
996babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
997babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
998babbbb9cSJamie Gritton 		if (*p != '\0')
999babbbb9cSJamie Gritton 			jid = 0;
1000babbbb9cSJamie Gritton 	}
1001b38ff370SJamie Gritton 	if (jid != 0) {
10020304c731SJamie Gritton 		/*
10030304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
10040304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
10050304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
10060304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
10070304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
10080304c731SJamie Gritton 		 */
1009b38ff370SJamie Gritton 		if (jid < 0) {
1010b38ff370SJamie Gritton 			error = EINVAL;
1011b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
1012b38ff370SJamie Gritton 			goto done_unlock_list;
1013b38ff370SJamie Gritton 		}
1014b38ff370SJamie Gritton 		pr = prison_find(jid);
1015b38ff370SJamie Gritton 		if (pr != NULL) {
10160304c731SJamie Gritton 			ppr = pr->pr_parent;
1017b38ff370SJamie Gritton 			/* Create: jid must not exist. */
1018b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
1019b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1020b38ff370SJamie Gritton 				error = EEXIST;
1021b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1022b38ff370SJamie Gritton 				    jid);
1023b38ff370SJamie Gritton 				goto done_unlock_list;
1024b38ff370SJamie Gritton 			}
10250304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
10260304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
10270304c731SJamie Gritton 				pr = NULL;
10280304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
1029b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1030b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1031b38ff370SJamie Gritton 					error = ENOENT;
1032b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1033b38ff370SJamie Gritton 					    jid);
1034b38ff370SJamie Gritton 					goto done_unlock_list;
1035b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
1036b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1037b38ff370SJamie Gritton 					/*
1038b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1039b38ff370SJamie Gritton 					 * (via attach or persist), but first
1040b38ff370SJamie Gritton 					 * it must determine if another jail
1041b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1042b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1043b38ff370SJamie Gritton 					 * name.
1044b38ff370SJamie Gritton 					 */
1045b38ff370SJamie Gritton 					if (name == NULL)
10460304c731SJamie Gritton 						name = prison_name(mypr, pr);
1047b38ff370SJamie Gritton 				}
1048b38ff370SJamie Gritton 			}
1049b38ff370SJamie Gritton 		}
1050b38ff370SJamie Gritton 		if (pr == NULL) {
1051b38ff370SJamie Gritton 			/* Update: jid must exist. */
1052b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1053b38ff370SJamie Gritton 				error = ENOENT;
1054b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
1055b38ff370SJamie Gritton 				goto done_unlock_list;
1056b38ff370SJamie Gritton 			}
1057b38ff370SJamie Gritton 		}
1058b38ff370SJamie Gritton 	}
1059b38ff370SJamie Gritton 	/*
1060b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1061b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1062b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1063b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1064b38ff370SJamie Gritton 	 * because that is the jail being updated).
1065b38ff370SJamie Gritton 	 */
1066b38ff370SJamie Gritton 	if (name != NULL) {
1067babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1068babbbb9cSJamie Gritton 		if (namelc == NULL)
1069babbbb9cSJamie Gritton 			namelc = name;
1070babbbb9cSJamie Gritton 		else {
10710304c731SJamie Gritton 			/*
10720304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
10730304c731SJamie Gritton 			 * parent and child names, and make sure the parent
10740304c731SJamie Gritton 			 * exists or matches an already found jail.
10750304c731SJamie Gritton 			 */
1076babbbb9cSJamie Gritton 			*namelc = '\0';
10770304c731SJamie Gritton 			if (pr != NULL) {
1078babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1079babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
10800304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
10810304c731SJamie Gritton 					error = EINVAL;
10820304c731SJamie Gritton 					vfs_opterror(opts,
10830304c731SJamie Gritton 					    "cannot change jail's parent");
10840304c731SJamie Gritton 					goto done_unlock_list;
10850304c731SJamie Gritton 				}
10860304c731SJamie Gritton 			} else {
10870304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
10880304c731SJamie Gritton 				if (ppr == NULL) {
10890304c731SJamie Gritton 					error = ENOENT;
10900304c731SJamie Gritton 					vfs_opterror(opts,
10910304c731SJamie Gritton 					    "jail \"%s\" not found", name);
10920304c731SJamie Gritton 					goto done_unlock_list;
10930304c731SJamie Gritton 				}
10940304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
10950304c731SJamie Gritton 			}
1096babbbb9cSJamie Gritton 			name = ++namelc;
10970304c731SJamie Gritton 		}
1098b38ff370SJamie Gritton 		if (name[0] != '\0') {
10990304c731SJamie Gritton 			namelen =
11000304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1101b38ff370SJamie Gritton  name_again:
11020304c731SJamie Gritton 			deadpr = NULL;
11030304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
1104b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
11050304c731SJamie Gritton 				    !strcmp(tpr->pr_name + namelen, name)) {
1106b38ff370SJamie Gritton 					if (pr == NULL &&
1107b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
1108b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
1109b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
1110b38ff370SJamie Gritton 							/*
1111b38ff370SJamie Gritton 							 * Use this jail
1112b38ff370SJamie Gritton 							 * for updates.
1113b38ff370SJamie Gritton 							 */
1114b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
1115b38ff370SJamie Gritton 								pr = tpr;
1116b38ff370SJamie Gritton 								break;
1117b38ff370SJamie Gritton 							}
1118b38ff370SJamie Gritton 							deadpr = tpr;
1119b38ff370SJamie Gritton 						}
1120b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1121b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
1122b38ff370SJamie Gritton 						/*
1123b38ff370SJamie Gritton 						 * Create, or update(jid):
1124b38ff370SJamie Gritton 						 * name must not exist in an
11250304c731SJamie Gritton 						 * active sibling jail.
1126b38ff370SJamie Gritton 						 */
1127b38ff370SJamie Gritton 						error = EEXIST;
1128b38ff370SJamie Gritton 						if (pr != NULL)
1129b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
1130b38ff370SJamie Gritton 						vfs_opterror(opts,
1131b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1132b38ff370SJamie Gritton 						   name);
1133b38ff370SJamie Gritton 						goto done_unlock_list;
1134b38ff370SJamie Gritton 					}
1135b38ff370SJamie Gritton 				}
1136b38ff370SJamie Gritton 			}
1137b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1138b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1139b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1140b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
1141b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
1142b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1143b38ff370SJamie Gritton 						goto name_again;
1144b38ff370SJamie Gritton 					}
1145b38ff370SJamie Gritton 					pr = deadpr;
1146b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1147b38ff370SJamie Gritton 					error = ENOENT;
1148b38ff370SJamie Gritton 					vfs_opterror(opts,
1149b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1150b38ff370SJamie Gritton 					goto done_unlock_list;
1151b38ff370SJamie Gritton 				}
1152b38ff370SJamie Gritton 			}
1153b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1154b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1155b38ff370SJamie Gritton 				error = ENOENT;
1156b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1157b38ff370SJamie Gritton 				    name);
1158b38ff370SJamie Gritton 				goto done_unlock_list;
1159b38ff370SJamie Gritton 			}
1160b38ff370SJamie Gritton 		}
1161b38ff370SJamie Gritton 	}
1162b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1163b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1164b38ff370SJamie Gritton 		error = ENOENT;
1165b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1166b38ff370SJamie Gritton 		goto done_unlock_list;
1167b38ff370SJamie Gritton 	}
1168b38ff370SJamie Gritton 
1169b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1170b38ff370SJamie Gritton 	if (pr == NULL) {
1171b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1172b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1173b97457e2SJamie Gritton 				error = EPERM;
1174b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
1175b97457e2SJamie Gritton 				goto done_unlock_list;
1176b97457e2SJamie Gritton 			}
1177b38ff370SJamie Gritton 		created = 1;
11780304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
11790304c731SJamie Gritton 		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
11800304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
11810304c731SJamie Gritton 			error = ENOENT;
11820304c731SJamie Gritton 			vfs_opterror(opts, "parent jail went away!");
11830304c731SJamie Gritton 			goto done_unlock_list;
11840304c731SJamie Gritton 		}
11850304c731SJamie Gritton 		ppr->pr_ref++;
11860304c731SJamie Gritton 		ppr->pr_uref++;
11870304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1188b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1189b38ff370SJamie Gritton 		if (jid == 0) {
1190b38ff370SJamie Gritton 			/* Find the next free jid. */
1191b38ff370SJamie Gritton 			jid = lastprid + 1;
1192b38ff370SJamie Gritton  findnext:
1193b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1194b38ff370SJamie Gritton 				jid = 1;
1195b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1196b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1197b38ff370SJamie Gritton 					continue;
1198b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1199b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1200b38ff370SJamie Gritton 					break;
1201b38ff370SJamie Gritton 				}
1202b38ff370SJamie Gritton 				if (jid == lastprid) {
1203b38ff370SJamie Gritton 					error = EAGAIN;
1204b38ff370SJamie Gritton 					vfs_opterror(opts,
1205b38ff370SJamie Gritton 					    "no available jail IDs");
1206b38ff370SJamie Gritton 					free(pr, M_PRISON);
12070304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
12080304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
12090304c731SJamie Gritton 					goto done_releroot;
1210b38ff370SJamie Gritton 				}
1211b38ff370SJamie Gritton 				jid++;
1212b38ff370SJamie Gritton 				goto findnext;
1213b38ff370SJamie Gritton 			}
1214b38ff370SJamie Gritton 			lastprid = jid;
1215b38ff370SJamie Gritton 		} else {
1216b38ff370SJamie Gritton 			/*
1217b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1218b38ff370SJamie Gritton 			 * so just find where to insert it.
1219b38ff370SJamie Gritton 			 */
1220b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1221b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1222b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1223b38ff370SJamie Gritton 					break;
1224b38ff370SJamie Gritton 				}
1225b38ff370SJamie Gritton 		}
1226b38ff370SJamie Gritton 		if (tpr == NULL)
1227b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
12280304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
12290304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1230b97457e2SJamie Gritton 			tpr->pr_childcount++;
1231b38ff370SJamie Gritton 
12320304c731SJamie Gritton 		pr->pr_parent = ppr;
1233b38ff370SJamie Gritton 		pr->pr_id = jid;
12340304c731SJamie Gritton 
12350304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1236b38ff370SJamie Gritton 		if (name == NULL)
1237b38ff370SJamie Gritton 			name = "";
1238b38ff370SJamie Gritton 		if (path == NULL) {
1239b38ff370SJamie Gritton 			path = "/";
12400304c731SJamie Gritton 			root = mypr->pr_root;
1241b38ff370SJamie Gritton 			vref(root);
1242b38ff370SJamie Gritton 		}
12438986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
12448986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1245bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1246bdfc8cc4SJamie Gritton #ifdef VIMAGE
1247bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1248bdfc8cc4SJamie Gritton #endif
1249bdfc8cc4SJamie Gritton 		{
12500304c731SJamie Gritton #ifdef INET
12512b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12522b0d6f81SJamie Gritton 				pr->pr_flags |=
12532b0d6f81SJamie Gritton 				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
12542b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12552b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12562b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12572b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12582b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12592b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12602b0d6f81SJamie Gritton 					    M_WAITOK);
12612b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12622b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12632b0d6f81SJamie Gritton 				}
12642b0d6f81SJamie Gritton 			}
12650304c731SJamie Gritton #endif
12660304c731SJamie Gritton #ifdef INET6
12672b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
12682b0d6f81SJamie Gritton 				pr->pr_flags |=
12692b0d6f81SJamie Gritton 				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
12702b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
12712b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
12722b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
12732b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
12742b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
12752b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
12762b0d6f81SJamie Gritton 					    M_WAITOK);
12772b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
12782b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
12792b0d6f81SJamie Gritton 				}
12802b0d6f81SJamie Gritton 			}
12810304c731SJamie Gritton #endif
1282bdfc8cc4SJamie Gritton 		}
1283bdfc8cc4SJamie Gritton #endif
1284592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1285592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1286592bcae8SBjoern A. Zeeb 
12870304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
12880304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
128942bebd82SJamie Gritton 		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1290bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1291b38ff370SJamie Gritton 
12920304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
12930304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1294b38ff370SJamie Gritton 
1295679e1390SJamie Gritton #ifdef VIMAGE
1296679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1297679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1298679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1299679e1390SJamie Gritton #endif
1300b38ff370SJamie Gritton 		/*
1301b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1302b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1303b38ff370SJamie Gritton 		 */
13040304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1305b38ff370SJamie Gritton 		if (error) {
1306b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1307b38ff370SJamie Gritton 			goto done_releroot;
1308b38ff370SJamie Gritton 		}
1309b38ff370SJamie Gritton 
1310b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1311b38ff370SJamie Gritton 		/*
1312b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1313b38ff370SJamie Gritton 		 * want other to see the incomplete prison once the
1314b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1315b38ff370SJamie Gritton 		 */
1316b38ff370SJamie Gritton 	} else {
1317b38ff370SJamie Gritton 		created = 0;
13182b0d6f81SJamie Gritton 		/*
13192b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
13202b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
13212b0d6f81SJamie Gritton 		 */
13222b0d6f81SJamie Gritton 		pr->pr_ref++;
1323bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1324bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1325bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1326bdfc8cc4SJamie Gritton 			error = EINVAL;
1327bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1328bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
1329bdfc8cc4SJamie Gritton 			goto done_deref_locked;
1330bdfc8cc4SJamie Gritton 		}
1331bdfc8cc4SJamie Gritton #endif
13322b0d6f81SJamie Gritton #ifdef INET
13332b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13342b0d6f81SJamie Gritton 			error = EINVAL;
13352b0d6f81SJamie Gritton 			vfs_opterror(opts,
13362b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
13372b0d6f81SJamie Gritton 			goto done_deref_locked;
13382b0d6f81SJamie Gritton 		}
13392b0d6f81SJamie Gritton #endif
13402b0d6f81SJamie Gritton #ifdef INET6
13412b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13422b0d6f81SJamie Gritton 			error = EINVAL;
13432b0d6f81SJamie Gritton 			vfs_opterror(opts,
13442b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
13452b0d6f81SJamie Gritton 			goto done_deref_locked;
13462b0d6f81SJamie Gritton 		}
13472b0d6f81SJamie Gritton #endif
1348b38ff370SJamie Gritton 	}
1349b38ff370SJamie Gritton 
1350b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13510304c731SJamie Gritton 	if (gotslevel) {
13520304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13530304c731SJamie Gritton 			error = EPERM;
13540304c731SJamie Gritton 			goto done_deref_locked;
13550304c731SJamie Gritton 		}
13560304c731SJamie Gritton 	}
1357b97457e2SJamie Gritton 	if (gotchildmax) {
1358b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1359b97457e2SJamie Gritton 			error = EPERM;
1360b97457e2SJamie Gritton 			goto done_deref_locked;
1361b97457e2SJamie Gritton 		}
1362b97457e2SJamie Gritton 	}
13630304c731SJamie Gritton 	if (gotenforce) {
13640304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
13650304c731SJamie Gritton 			error = EPERM;
13660304c731SJamie Gritton 			goto done_deref_locked;
13670304c731SJamie Gritton 		}
13680304c731SJamie Gritton 	}
13690cc207a6SMartin Matuska 	if (gotrsnum) {
13700cc207a6SMartin Matuska 		/*
13710cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
13720cc207a6SMartin Matuska 		 */
1373bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
13740cc207a6SMartin Matuska 			error = EINVAL;
13750cc207a6SMartin Matuska 			goto done_deref_locked;
13760cc207a6SMartin Matuska 		}
13770cc207a6SMartin Matuska 		/*
1378bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
13790cc207a6SMartin Matuska 		 */
13800cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
13810cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
13820cc207a6SMartin Matuska 				error = EPERM;
13830cc207a6SMartin Matuska 				goto done_deref_locked;
1384bf3db8aaSMartin Matuska 			} else
13850cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
13860cc207a6SMartin Matuska 		}
13870cc207a6SMartin Matuska 	}
1388b38ff370SJamie Gritton #ifdef INET
13892b0d6f81SJamie Gritton 	if (ip4s > 0) {
13900304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
13910304c731SJamie Gritton 			/*
13920304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
13930304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
13940304c731SJamie Gritton 			 * about the parent being unlocked, as any
13950304c731SJamie Gritton 			 * setting is done with allprison_lock held.
13960304c731SJamie Gritton 			 */
13970304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
13982b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
13990304c731SJamie Gritton 					break;
14000304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
14010304c731SJamie Gritton 				error = EPERM;
14020304c731SJamie Gritton 				goto done_deref_locked;
14030304c731SJamie Gritton 			}
14040304c731SJamie Gritton 			if (ip4s > 1) {
14050304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
14060304c731SJamie Gritton 					if (ip4[ii].s_addr ==
14070304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1408b38ff370SJamie Gritton 						continue;
14090304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
14100304c731SJamie Gritton 						if (ip4[ii].s_addr ==
14110304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
14120304c731SJamie Gritton 							break;
14130304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
14140304c731SJamie Gritton 						break;
14150304c731SJamie Gritton 				}
14160304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
14170304c731SJamie Gritton 					error = EPERM;
14180304c731SJamie Gritton 					goto done_deref_locked;
14190304c731SJamie Gritton 				}
14200304c731SJamie Gritton 			}
14210304c731SJamie Gritton 		}
14220304c731SJamie Gritton 		/*
14230304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
14240304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
14250304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
14260304c731SJamie Gritton 		 * IP stop checking and return error.
14270304c731SJamie Gritton 		 */
1428bdfc8cc4SJamie Gritton 		tppr = ppr;
1429bdfc8cc4SJamie Gritton #ifdef VIMAGE
1430bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1431bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1432bdfc8cc4SJamie Gritton 				break;
1433bdfc8cc4SJamie Gritton #endif
1434bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1435bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1436bdfc8cc4SJamie Gritton #ifdef VIMAGE
14372b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1438bdfc8cc4SJamie Gritton #endif
1439bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14400304c731SJamie Gritton 				descend = 0;
14410304c731SJamie Gritton 				continue;
14420304c731SJamie Gritton 			}
14430304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
14440304c731SJamie Gritton 				continue;
14450304c731SJamie Gritton 			descend = 0;
14460304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
14470304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
14480304c731SJamie Gritton 				continue;
14490304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
14502b0d6f81SJamie Gritton 				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
14510304c731SJamie Gritton 					error = EADDRINUSE;
1452b38ff370SJamie Gritton 					vfs_opterror(opts,
1453b38ff370SJamie Gritton 					    "IPv4 addresses clash");
1454b38ff370SJamie Gritton 					goto done_deref_locked;
1455b38ff370SJamie Gritton 				}
14560304c731SJamie Gritton 			}
14570304c731SJamie Gritton 		}
14580304c731SJamie Gritton 	}
1459b38ff370SJamie Gritton #endif
1460b38ff370SJamie Gritton #ifdef INET6
14612b0d6f81SJamie Gritton 	if (ip6s > 0) {
14620304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
14630304c731SJamie Gritton 			/*
14640304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14650304c731SJamie Gritton 			 * subset of the parent's list.
14660304c731SJamie Gritton 			 */
14670304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
14680304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14690304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
14700304c731SJamie Gritton 					break;
14710304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
14720304c731SJamie Gritton 				error = EPERM;
14730304c731SJamie Gritton 				goto done_deref_locked;
14740304c731SJamie Gritton 			}
14750304c731SJamie Gritton 			if (ip6s > 1) {
14760304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
14770304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14780304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
14790304c731SJamie Gritton 						continue;
14800304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
14810304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
14822b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
14830304c731SJamie Gritton 							break;
14840304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
14850304c731SJamie Gritton 						break;
14860304c731SJamie Gritton 				}
14870304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14880304c731SJamie Gritton 					error = EPERM;
14890304c731SJamie Gritton 					goto done_deref_locked;
14900304c731SJamie Gritton 				}
14910304c731SJamie Gritton 			}
14920304c731SJamie Gritton 		}
14930304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1494bdfc8cc4SJamie Gritton 		tppr = ppr;
1495bdfc8cc4SJamie Gritton #ifdef VIMAGE
1496bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1497bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1498bdfc8cc4SJamie Gritton 				break;
1499bdfc8cc4SJamie Gritton #endif
1500bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1501bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1502bdfc8cc4SJamie Gritton #ifdef VIMAGE
15032b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1504bdfc8cc4SJamie Gritton #endif
1505bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
15060304c731SJamie Gritton 				descend = 0;
15070304c731SJamie Gritton 				continue;
15080304c731SJamie Gritton 			}
15090304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
15100304c731SJamie Gritton 				continue;
15110304c731SJamie Gritton 			descend = 0;
15120304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
15130304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
15140304c731SJamie Gritton 				continue;
15150304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
15162b0d6f81SJamie Gritton 				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
15170304c731SJamie Gritton 					error = EADDRINUSE;
1518b38ff370SJamie Gritton 					vfs_opterror(opts,
1519b38ff370SJamie Gritton 					    "IPv6 addresses clash");
1520b38ff370SJamie Gritton 					goto done_deref_locked;
1521b38ff370SJamie Gritton 				}
15220304c731SJamie Gritton 			}
15230304c731SJamie Gritton 		}
15240304c731SJamie Gritton 	}
1525b38ff370SJamie Gritton #endif
15260304c731SJamie Gritton 	onamelen = namelen = 0;
15270304c731SJamie Gritton 	if (name != NULL) {
1528b38ff370SJamie Gritton 		/* Give a default name of the jid. */
1529b38ff370SJamie Gritton 		if (name[0] == '\0')
1530b38ff370SJamie Gritton 			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1531babbbb9cSJamie Gritton 		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1532babbbb9cSJamie Gritton 		    *p == '\0')) {
1533b38ff370SJamie Gritton 			error = EINVAL;
1534babbbb9cSJamie Gritton 			vfs_opterror(opts,
1535babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
15360304c731SJamie Gritton 			goto done_deref_locked;
1537b38ff370SJamie Gritton 		}
1538b38ff370SJamie Gritton 		/*
15390304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
15400304c731SJamie Gritton 		 * children.
1541b38ff370SJamie Gritton 		 */
15420304c731SJamie Gritton 		onamelen = strlen(pr->pr_name);
15430304c731SJamie Gritton 		namelen = strlen(name);
15440304c731SJamie Gritton 		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
15450304c731SJamie Gritton 			error = ENAMETOOLONG;
15460304c731SJamie Gritton 			goto done_deref_locked;
15470304c731SJamie Gritton 		}
15480304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
15490304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
15500304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
15510304c731SJamie Gritton 				error = ENAMETOOLONG;
15520304c731SJamie Gritton 				goto done_deref_locked;
15530304c731SJamie Gritton 			}
15540304c731SJamie Gritton 		}
15550304c731SJamie Gritton 	}
15560304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
15570304c731SJamie Gritton 		error = EPERM;
15580304c731SJamie Gritton 		goto done_deref_locked;
1559b38ff370SJamie Gritton 	}
1560b38ff370SJamie Gritton 
1561b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1562b38ff370SJamie Gritton #ifdef INET
15630304c731SJamie Gritton 	redo_ip4 = 0;
15640304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
15650304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1566b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
15670304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1568b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1569b38ff370SJamie Gritton 		ip4 = NULL;
15700304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1571bdfc8cc4SJamie Gritton #ifdef VIMAGE
1572bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1573bdfc8cc4SJamie Gritton 				descend = 0;
1574bdfc8cc4SJamie Gritton 				continue;
1575bdfc8cc4SJamie Gritton 			}
1576bdfc8cc4SJamie Gritton #endif
15770304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
15780304c731SJamie Gritton 				redo_ip4 = 1;
15790304c731SJamie Gritton 				descend = 0;
15800304c731SJamie Gritton 			}
15810304c731SJamie Gritton 		}
15820304c731SJamie Gritton 	}
1583b38ff370SJamie Gritton #endif
1584b38ff370SJamie Gritton #ifdef INET6
15850304c731SJamie Gritton 	redo_ip6 = 0;
15860304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
15870304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1588b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
15890304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1590b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1591b38ff370SJamie Gritton 		ip6 = NULL;
15920304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1593bdfc8cc4SJamie Gritton #ifdef VIMAGE
1594bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1595bdfc8cc4SJamie Gritton 				descend = 0;
1596bdfc8cc4SJamie Gritton 				continue;
1597bdfc8cc4SJamie Gritton 			}
1598bdfc8cc4SJamie Gritton #endif
15990304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
16000304c731SJamie Gritton 				redo_ip6 = 1;
16010304c731SJamie Gritton 				descend = 0;
16020304c731SJamie Gritton 			}
16030304c731SJamie Gritton 		}
16040304c731SJamie Gritton 	}
1605b38ff370SJamie Gritton #endif
16060304c731SJamie Gritton 	if (gotslevel) {
1607b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
16080304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
16090304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16100304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
16110304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
16120304c731SJamie Gritton 	}
1613b97457e2SJamie Gritton 	if (gotchildmax) {
1614b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1615b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1616b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1617b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1618b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1619b97457e2SJamie Gritton 				    ? childmax - level : 0;
1620b97457e2SJamie Gritton 	}
16210304c731SJamie Gritton 	if (gotenforce) {
16220304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
16230304c731SJamie Gritton 		/* Pass this restriction on to the children. */
16240304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16250304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
16260304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
16270304c731SJamie Gritton 	}
16280cc207a6SMartin Matuska 	if (gotrsnum) {
16290cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
16300cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
16310cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16320cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
16330cc207a6SMartin Matuska 	}
16340304c731SJamie Gritton 	if (name != NULL) {
16350304c731SJamie Gritton 		if (ppr == &prison0)
1636b38ff370SJamie Gritton 			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
16370304c731SJamie Gritton 		else
16380304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
16390304c731SJamie Gritton 			    ppr->pr_name, name);
16400304c731SJamie Gritton 		/* Change this component of child names. */
16410304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16420304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
16430304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
16440304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
16450304c731SJamie Gritton 		}
16460304c731SJamie Gritton 	}
1647b38ff370SJamie Gritton 	if (path != NULL) {
16480304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1649f6e633a9SMartin Matuska 		if (fullpath_disabled && path[0] == '/' &&
1650f6e633a9SMartin Matuska 		    strcmp(mypr->pr_path, "/"))
16510304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
16520304c731SJamie Gritton 			    mypr->pr_path, path);
16530304c731SJamie Gritton 		else
1654b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1655b38ff370SJamie Gritton 		pr->pr_root = root;
1656b38ff370SJamie Gritton 	}
165776ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
165876ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
165976ca6f88SJamie Gritton 			/*
166076ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
166176ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
166276ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
166376ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
166476ca6f88SJamie Gritton 			 */
1665c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1666c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1667c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1668c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1669c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1670c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
167176ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
167276ca6f88SJamie Gritton 		}
167376ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
167476ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1675b38ff370SJamie Gritton 		if (host != NULL)
1676c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
167776ca6f88SJamie Gritton 		if (domain != NULL)
1678c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1679c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
168076ca6f88SJamie Gritton 		if (uuid != NULL)
1681c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
168276ca6f88SJamie Gritton 		if (gothid)
168376ca6f88SJamie Gritton 			pr->pr_hostid = hid;
168476ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
168576ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
168676ca6f88SJamie Gritton 				descend = 0;
168776ca6f88SJamie Gritton 			else {
168876ca6f88SJamie Gritton 				if (host != NULL)
1689c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1690c1f19219SJamie Gritton 					    pr->pr_hostname,
1691c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
169276ca6f88SJamie Gritton 				if (domain != NULL)
1693c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1694c1f19219SJamie Gritton 					    pr->pr_domainname,
1695c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
169676ca6f88SJamie Gritton 				if (uuid != NULL)
1697c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1698c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1699c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
170076ca6f88SJamie Gritton 				if (gothid)
170176ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
170276ca6f88SJamie Gritton 			}
170376ca6f88SJamie Gritton 		}
170476ca6f88SJamie Gritton 	}
17050304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
17060304c731SJamie Gritton 		/* Clear allow bits in all children. */
17070304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
17080304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
17090304c731SJamie Gritton 	}
17100304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1711b38ff370SJamie Gritton 	/*
1712b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1713b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1714b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1715b38ff370SJamie Gritton 	 * handlers have completed.
1716b38ff370SJamie Gritton 	 */
1717b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1718b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1719b38ff370SJamie Gritton 			pr->pr_ref++;
1720b38ff370SJamie Gritton 			pr->pr_uref++;
1721b38ff370SJamie Gritton 		} else {
1722b38ff370SJamie Gritton 			pr->pr_ref--;
1723b38ff370SJamie Gritton 			pr->pr_uref--;
1724b38ff370SJamie Gritton 		}
1725b38ff370SJamie Gritton 	}
1726b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1727b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1728b38ff370SJamie Gritton 
1729a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
1730a7ad07bfSEdward Tomasz Napierala 	if (created)
1731a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
1732a7ad07bfSEdward Tomasz Napierala #endif
1733a7ad07bfSEdward Tomasz Napierala 
17340304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
17350304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
17360304c731SJamie Gritton 	 */
17370304c731SJamie Gritton #ifdef INET
17380304c731SJamie Gritton 	while (redo_ip4) {
17390304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
17400304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
17410304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17420304c731SJamie Gritton 		redo_ip4 = 0;
17430304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1744bdfc8cc4SJamie Gritton #ifdef VIMAGE
1745bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1746bdfc8cc4SJamie Gritton 				descend = 0;
1747bdfc8cc4SJamie Gritton 				continue;
1748bdfc8cc4SJamie Gritton 			}
1749bdfc8cc4SJamie Gritton #endif
17500304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
17510304c731SJamie Gritton 				if (ip4 != NULL)
17520304c731SJamie Gritton 					ip4 = NULL;
17530304c731SJamie Gritton 				else
17540304c731SJamie Gritton 					redo_ip4 = 1;
17550304c731SJamie Gritton 			}
17560304c731SJamie Gritton 		}
17570304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17580304c731SJamie Gritton 	}
17590304c731SJamie Gritton #endif
17600304c731SJamie Gritton #ifdef INET6
17610304c731SJamie Gritton 	while (redo_ip6) {
17620304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
17630304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
17640304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17650304c731SJamie Gritton 		redo_ip6 = 0;
17660304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1767bdfc8cc4SJamie Gritton #ifdef VIMAGE
1768bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1769bdfc8cc4SJamie Gritton 				descend = 0;
1770bdfc8cc4SJamie Gritton 				continue;
1771bdfc8cc4SJamie Gritton 			}
1772bdfc8cc4SJamie Gritton #endif
17730304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
17740304c731SJamie Gritton 				if (ip6 != NULL)
17750304c731SJamie Gritton 					ip6 = NULL;
17760304c731SJamie Gritton 				else
17770304c731SJamie Gritton 					redo_ip6 = 1;
17780304c731SJamie Gritton 			}
17790304c731SJamie Gritton 		}
17800304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17810304c731SJamie Gritton 	}
17820304c731SJamie Gritton #endif
17830304c731SJamie Gritton 
1784b38ff370SJamie Gritton 	/* Let the modules do their work. */
1785b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1786b38ff370SJamie Gritton 	if (created) {
1787b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1788b38ff370SJamie Gritton 		if (error) {
1789b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_SLOCKED);
1790b38ff370SJamie Gritton 			goto done_errmsg;
1791b38ff370SJamie Gritton 		}
1792b38ff370SJamie Gritton 	}
1793b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1794b38ff370SJamie Gritton 	if (error) {
1795b38ff370SJamie Gritton 		prison_deref(pr, created
1796b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1797b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1798b38ff370SJamie Gritton 		goto done_errmsg;
1799b38ff370SJamie Gritton 	}
1800b38ff370SJamie Gritton 
1801b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1802b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1803b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1804b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1805b38ff370SJamie Gritton 		if (error) {
1806b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1807b38ff370SJamie Gritton 			if (!created)
1808b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1809b38ff370SJamie Gritton 			goto done_errmsg;
1810b38ff370SJamie Gritton 		}
1811b38ff370SJamie Gritton 	}
1812b38ff370SJamie Gritton 
18131fb24974SEdward Tomasz Napierala #ifdef RACCT
18141fb24974SEdward Tomasz Napierala 	if (!created) {
1815f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18161fb24974SEdward Tomasz Napierala 			sx_sunlock(&allprison_lock);
18171fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
1818f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18191fb24974SEdward Tomasz Napierala 			sx_slock(&allprison_lock);
18201fb24974SEdward Tomasz Napierala 	}
18211fb24974SEdward Tomasz Napierala #endif
18221fb24974SEdward Tomasz Napierala 
18231fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
18241fb24974SEdward Tomasz Napierala 
1825b38ff370SJamie Gritton 	/*
1826b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1827b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1828b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1829b38ff370SJamie Gritton 	 * visible).
1830b38ff370SJamie Gritton 	 */
1831b38ff370SJamie Gritton 	if (!created) {
1832b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1833b38ff370SJamie Gritton 		    ? PD_DEREF
1834b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1835b38ff370SJamie Gritton 	} else {
1836b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1837b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1838b38ff370SJamie Gritton 			pr->pr_ref++;
1839b38ff370SJamie Gritton 			pr->pr_uref++;
1840b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1841b38ff370SJamie Gritton 		}
1842b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1843b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1844b38ff370SJamie Gritton 	}
1845c34bbd2aSEdward Tomasz Napierala 
1846b38ff370SJamie Gritton 	goto done_errmsg;
1847b38ff370SJamie Gritton 
18480304c731SJamie Gritton  done_deref_locked:
18490304c731SJamie Gritton 	prison_deref(pr, created
18500304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
18510304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
18520304c731SJamie Gritton 	goto done_releroot;
1853b38ff370SJamie Gritton  done_unlock_list:
1854b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1855b38ff370SJamie Gritton  done_releroot:
18565050aa86SKonstantin Belousov 	if (root != NULL)
1857b38ff370SJamie Gritton 		vrele(root);
1858b38ff370SJamie Gritton  done_errmsg:
1859b38ff370SJamie Gritton 	if (error) {
1860b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1861b38ff370SJamie Gritton 		if (errmsg_len > 0) {
1862b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1863b38ff370SJamie Gritton 			if (errmsg_pos > 0) {
1864b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE)
1865b38ff370SJamie Gritton 					bcopy(errmsg,
1866b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1867b38ff370SJamie Gritton 					   errmsg_len);
1868b38ff370SJamie Gritton 				else
1869b38ff370SJamie Gritton 					copyout(errmsg,
1870b38ff370SJamie Gritton 					   optuio->uio_iov[errmsg_pos].iov_base,
1871b38ff370SJamie Gritton 					   errmsg_len);
1872b38ff370SJamie Gritton 			}
1873b38ff370SJamie Gritton 		}
1874b38ff370SJamie Gritton 	}
1875b38ff370SJamie Gritton  done_free:
1876b38ff370SJamie Gritton #ifdef INET
1877b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1878b38ff370SJamie Gritton #endif
1879b38ff370SJamie Gritton #ifdef INET6
1880b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1881b38ff370SJamie Gritton #endif
18826dfe0a3dSMartin Matuska 	if (g_path != NULL)
18836dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
1884b38ff370SJamie Gritton 	vfs_freeopts(opts);
1885b38ff370SJamie Gritton 	return (error);
1886b38ff370SJamie Gritton }
1887b38ff370SJamie Gritton 
1888b38ff370SJamie Gritton 
1889b38ff370SJamie Gritton /*
1890b38ff370SJamie Gritton  * struct jail_get_args {
1891b38ff370SJamie Gritton  *	struct iovec *iovp;
1892b38ff370SJamie Gritton  *	unsigned int iovcnt;
1893b38ff370SJamie Gritton  *	int flags;
1894b38ff370SJamie Gritton  * };
1895b38ff370SJamie Gritton  */
1896b38ff370SJamie Gritton int
18978451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
1898b38ff370SJamie Gritton {
1899b38ff370SJamie Gritton 	struct uio *auio;
1900b38ff370SJamie Gritton 	int error;
1901b38ff370SJamie Gritton 
1902b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1903b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1904b38ff370SJamie Gritton 		return (EINVAL);
1905b38ff370SJamie Gritton 
1906b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1907b38ff370SJamie Gritton 	if (error)
1908b38ff370SJamie Gritton 		return (error);
1909b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1910b38ff370SJamie Gritton 	if (error == 0)
1911b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1912b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1913b38ff370SJamie Gritton 	free(auio, M_IOV);
1914b38ff370SJamie Gritton 	return (error);
1915b38ff370SJamie Gritton }
1916b38ff370SJamie Gritton 
1917b38ff370SJamie Gritton int
1918b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1919b38ff370SJamie Gritton {
19200304c731SJamie Gritton 	struct prison *pr, *mypr;
1921b38ff370SJamie Gritton 	struct vfsopt *opt;
1922b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1923b38ff370SJamie Gritton 	char *errmsg, *name;
19240304c731SJamie Gritton 	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1925b38ff370SJamie Gritton 
1926b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1927b38ff370SJamie Gritton 		return (EINVAL);
1928b38ff370SJamie Gritton 
1929b38ff370SJamie Gritton 	/* Get the parameter list. */
1930b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1931b38ff370SJamie Gritton 	if (error)
1932b38ff370SJamie Gritton 		return (error);
1933b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
19340304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
19351e2a13e6SJamie Gritton 
1936b38ff370SJamie Gritton 	/*
1937b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
1938b38ff370SJamie Gritton 	 */
1939b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
1940b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1941b38ff370SJamie Gritton 	if (error == 0) {
1942b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
19430304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1944b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
1945b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
1946b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1947b38ff370SJamie Gritton 					break;
1948b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1949b38ff370SJamie Gritton 			}
1950b38ff370SJamie Gritton 		}
1951b38ff370SJamie Gritton 		if (pr != NULL)
1952b38ff370SJamie Gritton 			goto found_prison;
1953b38ff370SJamie Gritton 		error = ENOENT;
1954b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
1955b38ff370SJamie Gritton 		goto done_unlock_list;
1956b38ff370SJamie Gritton 	} else if (error != ENOENT)
1957b38ff370SJamie Gritton 		goto done_unlock_list;
1958b38ff370SJamie Gritton 
1959b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1960b38ff370SJamie Gritton 	if (error == 0) {
1961b38ff370SJamie Gritton 		if (jid != 0) {
19620304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
1963b38ff370SJamie Gritton 			if (pr != NULL) {
1964b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1965b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1966b38ff370SJamie Gritton 					error = ENOENT;
1967b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1968b38ff370SJamie Gritton 					    jid);
1969b38ff370SJamie Gritton 					goto done_unlock_list;
1970b38ff370SJamie Gritton 				}
1971b38ff370SJamie Gritton 				goto found_prison;
1972b38ff370SJamie Gritton 			}
1973b38ff370SJamie Gritton 			error = ENOENT;
1974b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
1975b38ff370SJamie Gritton 			goto done_unlock_list;
1976b38ff370SJamie Gritton 		}
1977b38ff370SJamie Gritton 	} else if (error != ENOENT)
1978b38ff370SJamie Gritton 		goto done_unlock_list;
1979b38ff370SJamie Gritton 
1980b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
1981b38ff370SJamie Gritton 	if (error == 0) {
1982b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
1983b38ff370SJamie Gritton 			error = EINVAL;
1984b38ff370SJamie Gritton 			goto done_unlock_list;
1985b38ff370SJamie Gritton 		}
19860304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
1987b38ff370SJamie Gritton 		if (pr != NULL) {
1988b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1989b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1990b38ff370SJamie Gritton 				error = ENOENT;
1991b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
1992b38ff370SJamie Gritton 				    name);
1993b38ff370SJamie Gritton 				goto done_unlock_list;
1994b38ff370SJamie Gritton 			}
1995b38ff370SJamie Gritton 			goto found_prison;
1996b38ff370SJamie Gritton 		}
1997b38ff370SJamie Gritton 		error = ENOENT;
1998b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
1999b38ff370SJamie Gritton 		goto done_unlock_list;
2000b38ff370SJamie Gritton 	} else if (error != ENOENT)
2001b38ff370SJamie Gritton 		goto done_unlock_list;
2002b38ff370SJamie Gritton 
2003b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2004b38ff370SJamie Gritton 	error = ENOENT;
2005b38ff370SJamie Gritton 	goto done_unlock_list;
2006b38ff370SJamie Gritton 
2007b38ff370SJamie Gritton  found_prison:
2008b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
2009b38ff370SJamie Gritton 	pr->pr_ref++;
2010b38ff370SJamie Gritton 	locked = PD_LOCKED;
2011b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2012b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2013b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2014b38ff370SJamie Gritton 		goto done_deref;
20150304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
20160304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2017b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2018b38ff370SJamie Gritton 		goto done_deref;
20190304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
20200304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20210304c731SJamie Gritton 		goto done_deref;
20220304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2023b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2024b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2025b38ff370SJamie Gritton 		goto done_deref;
20260304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2027b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2028b38ff370SJamie Gritton 		goto done_deref;
2029b38ff370SJamie Gritton #ifdef INET
2030b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2031b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2032b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2033b38ff370SJamie Gritton 		goto done_deref;
2034b38ff370SJamie Gritton #endif
2035b38ff370SJamie Gritton #ifdef INET6
2036b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2037b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2038b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2039b38ff370SJamie Gritton 		goto done_deref;
2040b38ff370SJamie Gritton #endif
2041b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2042b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2043b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2044b38ff370SJamie Gritton 		goto done_deref;
2045b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2046b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2047b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2048b97457e2SJamie Gritton 		goto done_deref;
2049b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2050b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2051b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2052b97457e2SJamie Gritton 		goto done_deref;
2053c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2054b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2055b38ff370SJamie Gritton 		goto done_deref;
2056c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
205776ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
205876ca6f88SJamie Gritton 		goto done_deref;
2059c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
206076ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
206176ca6f88SJamie Gritton 		goto done_deref;
2062841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2063a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
206476ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
206576ca6f88SJamie Gritton 
206676ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
206776ca6f88SJamie Gritton 	} else
206876ca6f88SJamie Gritton #endif
206976ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
207076ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
207176ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
207276ca6f88SJamie Gritton 		goto done_deref;
20730304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
20740304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
20750304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20760304c731SJamie Gritton 		goto done_deref;
20770cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
20780cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
20790cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
20800cc207a6SMartin Matuska 		goto done_deref;
20810304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
20820304c731SJamie Gritton 	    fi++) {
20830304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
20840304c731SJamie Gritton 			continue;
20850304c731SJamie Gritton 		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
20860304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2087b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2088b38ff370SJamie Gritton 			goto done_deref;
2089b38ff370SJamie Gritton 		i = !i;
20900304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2091b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2092b38ff370SJamie Gritton 			goto done_deref;
20930304c731SJamie Gritton 	}
20947cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
20957cbf7213SJamie Gritton 	    fi++) {
20967cbf7213SJamie Gritton 		i = pr->pr_flags &
20977cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
20987cbf7213SJamie Gritton 		i = pr_flag_jailsys[fi].disable &&
20997cbf7213SJamie Gritton 		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
21007cbf7213SJamie Gritton 		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
21017cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
21027cbf7213SJamie Gritton 		error =
21037cbf7213SJamie Gritton 		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
21047cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
21057cbf7213SJamie Gritton 			goto done_deref;
21067cbf7213SJamie Gritton 	}
21070304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
21080304c731SJamie Gritton 	    fi++) {
21090304c731SJamie Gritton 		if (pr_allow_names[fi] == NULL)
21100304c731SJamie Gritton 			continue;
21110304c731SJamie Gritton 		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
21120304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
21130304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21140304c731SJamie Gritton 			goto done_deref;
21150304c731SJamie Gritton 		i = !i;
21160304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
21170304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21180304c731SJamie Gritton 			goto done_deref;
21190304c731SJamie Gritton 	}
2120b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
2121b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2122b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2123b38ff370SJamie Gritton 		goto done_deref;
2124b38ff370SJamie Gritton 	i = !i;
2125b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2126b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2127b38ff370SJamie Gritton 		goto done_deref;
2128b38ff370SJamie Gritton 
2129b38ff370SJamie Gritton 	/* Get the module parameters. */
2130b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2131b38ff370SJamie Gritton 	locked = 0;
2132b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2133b38ff370SJamie Gritton 	if (error)
2134b38ff370SJamie Gritton 		goto done_deref;
2135b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2136b38ff370SJamie Gritton 
2137b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2138b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2139b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2140b38ff370SJamie Gritton 			error = EINVAL;
2141b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2142b38ff370SJamie Gritton 			goto done_errmsg;
2143b38ff370SJamie Gritton 		}
2144b38ff370SJamie Gritton 	}
2145b38ff370SJamie Gritton 
2146b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2147b38ff370SJamie Gritton 	error = 0;
2148b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2149b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2150b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2151b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2152b38ff370SJamie Gritton 			if (opt->value != NULL) {
2153b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2154b38ff370SJamie Gritton 					bcopy(opt->value,
2155b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2156b38ff370SJamie Gritton 					    opt->len);
2157b38ff370SJamie Gritton 				} else {
2158b38ff370SJamie Gritton 					error = copyout(opt->value,
2159b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2160b38ff370SJamie Gritton 					    opt->len);
2161b38ff370SJamie Gritton 					if (error)
2162b38ff370SJamie Gritton 						break;
2163b38ff370SJamie Gritton 				}
2164b38ff370SJamie Gritton 			}
2165b38ff370SJamie Gritton 		}
2166b38ff370SJamie Gritton 	}
2167b38ff370SJamie Gritton 	goto done_errmsg;
2168b38ff370SJamie Gritton 
2169b38ff370SJamie Gritton  done_deref:
2170b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2171b38ff370SJamie Gritton 	goto done_errmsg;
2172b38ff370SJamie Gritton 
2173b38ff370SJamie Gritton  done_unlock_list:
2174b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2175b38ff370SJamie Gritton  done_errmsg:
2176b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2177b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2178b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2179b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2180b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2181b38ff370SJamie Gritton 				bcopy(errmsg,
2182b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2183b38ff370SJamie Gritton 				    errmsg_len);
2184b38ff370SJamie Gritton 			else
2185b38ff370SJamie Gritton 				copyout(errmsg,
2186b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2187b38ff370SJamie Gritton 				    errmsg_len);
2188b38ff370SJamie Gritton 		}
2189b38ff370SJamie Gritton 	}
2190b38ff370SJamie Gritton 	vfs_freeopts(opts);
2191b38ff370SJamie Gritton 	return (error);
2192b38ff370SJamie Gritton }
2193b38ff370SJamie Gritton 
21940304c731SJamie Gritton 
2195b38ff370SJamie Gritton /*
2196b38ff370SJamie Gritton  * struct jail_remove_args {
2197b38ff370SJamie Gritton  *	int jid;
2198b38ff370SJamie Gritton  * };
2199b38ff370SJamie Gritton  */
2200b38ff370SJamie Gritton int
22018451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2202b38ff370SJamie Gritton {
22030304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
22040304c731SJamie Gritton 	int descend, error;
2205b38ff370SJamie Gritton 
2206b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2207b38ff370SJamie Gritton 	if (error)
2208b38ff370SJamie Gritton 		return (error);
2209b38ff370SJamie Gritton 
2210b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
22110304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2212b38ff370SJamie Gritton 	if (pr == NULL) {
2213b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2214b38ff370SJamie Gritton 		return (EINVAL);
2215b38ff370SJamie Gritton 	}
2216b38ff370SJamie Gritton 
22170304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
22180304c731SJamie Gritton 	pr->pr_ref++;
22190304c731SJamie Gritton 	pr->pr_flags |= PR_REMOVE;
22200304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
22210304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
22220304c731SJamie Gritton 		lpr = NULL;
22230304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
22240304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
22250304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
22260304c731SJamie Gritton 				tpr = cpr;
22270304c731SJamie Gritton 				cpr->pr_ref++;
22280304c731SJamie Gritton 				cpr->pr_flags |= PR_REMOVE;
22290304c731SJamie Gritton 			} else {
22300304c731SJamie Gritton 				/* Already removed - do not do it again. */
22310304c731SJamie Gritton 				tpr = NULL;
22320304c731SJamie Gritton 			}
22330304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
22340304c731SJamie Gritton 			if (lpr != NULL) {
22350304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
22360304c731SJamie Gritton 				prison_remove_one(lpr);
22370304c731SJamie Gritton 				sx_xlock(&allprison_lock);
22380304c731SJamie Gritton 			}
22390304c731SJamie Gritton 			lpr = tpr;
22400304c731SJamie Gritton 		}
22410304c731SJamie Gritton 		if (lpr != NULL) {
22420304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
22430304c731SJamie Gritton 			prison_remove_one(lpr);
22440304c731SJamie Gritton 			sx_xlock(&allprison_lock);
22450304c731SJamie Gritton 		}
22460304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
22470304c731SJamie Gritton 	}
22480304c731SJamie Gritton 	prison_remove_one(pr);
22490304c731SJamie Gritton 	return (0);
22500304c731SJamie Gritton }
22510304c731SJamie Gritton 
22520304c731SJamie Gritton static void
22530304c731SJamie Gritton prison_remove_one(struct prison *pr)
22540304c731SJamie Gritton {
22550304c731SJamie Gritton 	struct proc *p;
22560304c731SJamie Gritton 	int deuref;
22570304c731SJamie Gritton 
2258b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2259b38ff370SJamie Gritton 	deuref = 0;
2260b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2261b38ff370SJamie Gritton 		pr->pr_ref--;
2262b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2263b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2264b38ff370SJamie Gritton 	}
2265b38ff370SJamie Gritton 
22660304c731SJamie Gritton 	/*
22670304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
22680304c731SJamie Gritton 	 * the prison now.
22690304c731SJamie Gritton 	 */
22700304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
22710304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
22720304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2273b38ff370SJamie Gritton 		prison_deref(pr,
2274b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
22750304c731SJamie Gritton 		return;
2276b38ff370SJamie Gritton 	}
2277b38ff370SJamie Gritton 
2278b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2279b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2280b38ff370SJamie Gritton 	/*
2281b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2282b38ff370SJamie Gritton 	 */
2283b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
2284b38ff370SJamie Gritton 	LIST_FOREACH(p, &allproc, p_list) {
2285b38ff370SJamie Gritton 		PROC_LOCK(p);
2286b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2287b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
22888451d0ddSKip Macy 			kern_psignal(p, SIGKILL);
2289b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2290b38ff370SJamie Gritton 	}
2291b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
22920304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2293b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
229475c13541SPoul-Henning Kamp }
229575c13541SPoul-Henning Kamp 
22968571af59SJamie Gritton 
2297fd7a8150SMike Barcroft /*
22989ddb7954SMike Barcroft  * struct jail_attach_args {
22999ddb7954SMike Barcroft  *	int jid;
23009ddb7954SMike Barcroft  * };
2301fd7a8150SMike Barcroft  */
2302fd7a8150SMike Barcroft int
23038451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2304fd7a8150SMike Barcroft {
2305b38ff370SJamie Gritton 	struct prison *pr;
2306b38ff370SJamie Gritton 	int error;
2307b38ff370SJamie Gritton 
2308b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2309b38ff370SJamie Gritton 	if (error)
2310b38ff370SJamie Gritton 		return (error);
2311b38ff370SJamie Gritton 
2312b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
23130304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2314b38ff370SJamie Gritton 	if (pr == NULL) {
2315b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2316b38ff370SJamie Gritton 		return (EINVAL);
2317b38ff370SJamie Gritton 	}
2318b38ff370SJamie Gritton 
2319b38ff370SJamie Gritton 	/*
2320b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2321b38ff370SJamie Gritton 	 * considered to be "alive".
2322b38ff370SJamie Gritton 	 */
2323b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2324b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2325b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2326b38ff370SJamie Gritton 		return (EINVAL);
2327b38ff370SJamie Gritton 	}
2328b38ff370SJamie Gritton 
2329b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2330b38ff370SJamie Gritton }
2331b38ff370SJamie Gritton 
2332b38ff370SJamie Gritton static int
2333b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2334b38ff370SJamie Gritton {
23350304c731SJamie Gritton 	struct prison *ppr;
2336fd7a8150SMike Barcroft 	struct proc *p;
2337fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
23385050aa86SKonstantin Belousov 	int error;
2339fd7a8150SMike Barcroft 
234057f22bd4SJacques Vidrine 	/*
234157f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
234257f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
234357f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
234457f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
234557f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
234657f22bd4SJacques Vidrine 	 * of another.
234757f22bd4SJacques Vidrine 	 */
2348fd7a8150SMike Barcroft 	pr->pr_ref++;
2349b38ff370SJamie Gritton 	pr->pr_uref++;
2350fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2351b38ff370SJamie Gritton 
2352b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2353b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2354b38ff370SJamie Gritton 	if (error) {
2355b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2356b38ff370SJamie Gritton 		return (error);
2357b38ff370SJamie Gritton 	}
2358dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2359fd7a8150SMike Barcroft 
2360413628a7SBjoern A. Zeeb 	/*
2361413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2362413628a7SBjoern A. Zeeb 	 */
23630304c731SJamie Gritton 	ppr = td->td_ucred->cr_prison;
2364b38ff370SJamie Gritton 	p = td->td_proc;
2365413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2366413628a7SBjoern A. Zeeb 	if (error)
2367b38ff370SJamie Gritton 		goto e_revert_osd;
2368413628a7SBjoern A. Zeeb 
2369cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2370fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2371fd7a8150SMike Barcroft 		goto e_unlock;
2372fd7a8150SMike Barcroft #ifdef MAC
237330d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2374fd7a8150SMike Barcroft 		goto e_unlock;
2375fd7a8150SMike Barcroft #endif
237622db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2377b38ff370SJamie Gritton 	if ((error = change_root(pr->pr_root, td)))
23785050aa86SKonstantin Belousov 		goto e_revert_osd;
2379fd7a8150SMike Barcroft 
2380fd7a8150SMike Barcroft 	newcred = crget();
2381fd7a8150SMike Barcroft 	PROC_LOCK(p);
2382fd7a8150SMike Barcroft 	oldcred = p->p_ucred;
2383fd7a8150SMike Barcroft 	setsugid(p);
2384fd7a8150SMike Barcroft 	crcopy(newcred, oldcred);
238569c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2386fd7a8150SMike Barcroft 	p->p_ucred = newcred;
2387fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
2388097055e2SEdward Tomasz Napierala #ifdef RACCT
2389097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2390097055e2SEdward Tomasz Napierala #endif
2391fd7a8150SMike Barcroft 	crfree(oldcred);
23920304c731SJamie Gritton 	prison_deref(ppr, PD_DEREF | PD_DEUREF);
2393fd7a8150SMike Barcroft 	return (0);
2394fd7a8150SMike Barcroft  e_unlock:
239522db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2396b38ff370SJamie Gritton  e_revert_osd:
2397b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
23980304c731SJamie Gritton 	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2399b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2400fd7a8150SMike Barcroft 	return (error);
2401fd7a8150SMike Barcroft }
2402fd7a8150SMike Barcroft 
24030304c731SJamie Gritton 
2404fd7a8150SMike Barcroft /*
2405fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2406fd7a8150SMike Barcroft  */
240754b369c1SPawel Jakub Dawidek struct prison *
2408fd7a8150SMike Barcroft prison_find(int prid)
2409fd7a8150SMike Barcroft {
2410fd7a8150SMike Barcroft 	struct prison *pr;
2411fd7a8150SMike Barcroft 
2412dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2413b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2414fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2415fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2416b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2417fd7a8150SMike Barcroft 				return (pr);
2418b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2419fd7a8150SMike Barcroft 		}
2420fd7a8150SMike Barcroft 	}
2421fd7a8150SMike Barcroft 	return (NULL);
2422fd7a8150SMike Barcroft }
2423fd7a8150SMike Barcroft 
2424b38ff370SJamie Gritton /*
24250304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2426b38ff370SJamie Gritton  */
2427b38ff370SJamie Gritton struct prison *
24280304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2429b38ff370SJamie Gritton {
24300304c731SJamie Gritton 	struct prison *pr;
24310304c731SJamie Gritton 	int descend;
2432b38ff370SJamie Gritton 
2433b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24340304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24350304c731SJamie Gritton 		if (pr->pr_id == prid) {
24360304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
24370304c731SJamie Gritton 			if (pr->pr_ref > 0)
24380304c731SJamie Gritton 				return (pr);
24390304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
24400304c731SJamie Gritton 		}
24410304c731SJamie Gritton 	}
24420304c731SJamie Gritton 	return (NULL);
24430304c731SJamie Gritton }
24440304c731SJamie Gritton 
24450304c731SJamie Gritton /*
24460304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
24470304c731SJamie Gritton  */
24480304c731SJamie Gritton struct prison *
24490304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
24500304c731SJamie Gritton {
24510304c731SJamie Gritton 	struct prison *pr, *deadpr;
24520304c731SJamie Gritton 	size_t mylen;
24530304c731SJamie Gritton 	int descend;
24540304c731SJamie Gritton 
24550304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24560304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2457b38ff370SJamie Gritton  again:
2458b38ff370SJamie Gritton 	deadpr = NULL;
24590304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24600304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2461b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2462b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2463b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2464b38ff370SJamie Gritton 					return (pr);
2465b38ff370SJamie Gritton 				deadpr = pr;
2466b38ff370SJamie Gritton 			}
2467b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2468b38ff370SJamie Gritton 		}
2469b38ff370SJamie Gritton 	}
24700304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2471b38ff370SJamie Gritton 	if (deadpr != NULL) {
2472b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2473b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2474b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2475b38ff370SJamie Gritton 			goto again;
2476b38ff370SJamie Gritton 		}
2477b38ff370SJamie Gritton 	}
2478b38ff370SJamie Gritton 	return (deadpr);
2479b38ff370SJamie Gritton }
2480b38ff370SJamie Gritton 
2481b38ff370SJamie Gritton /*
24820304c731SJamie Gritton  * See if a prison has the specific flag set.
24830304c731SJamie Gritton  */
24840304c731SJamie Gritton int
24850304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
24860304c731SJamie Gritton {
24870304c731SJamie Gritton 
24880304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
24890304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
24900304c731SJamie Gritton }
24910304c731SJamie Gritton 
24920304c731SJamie Gritton int
24930304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
24940304c731SJamie Gritton {
24950304c731SJamie Gritton 
24960304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
24970304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
24980304c731SJamie Gritton }
24990304c731SJamie Gritton 
25000304c731SJamie Gritton /*
2501b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2502b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2503b38ff370SJamie Gritton  */
250491421ba2SRobert Watson void
25051ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
250691421ba2SRobert Watson {
250791421ba2SRobert Watson 
25081ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
250991421ba2SRobert Watson 	pr->pr_ref--;
251091421ba2SRobert Watson 	if (pr->pr_ref == 0) {
251101137630SRobert Watson 		mtx_unlock(&pr->pr_mtx);
2512c2cda609SPawel Jakub Dawidek 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2513c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2514c2cda609SPawel Jakub Dawidek 		return;
2515c2cda609SPawel Jakub Dawidek 	}
2516c2cda609SPawel Jakub Dawidek 	mtx_unlock(&pr->pr_mtx);
2517c2cda609SPawel Jakub Dawidek }
2518c2cda609SPawel Jakub Dawidek 
25191ba4a712SPawel Jakub Dawidek void
25201ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
25211ba4a712SPawel Jakub Dawidek {
25221ba4a712SPawel Jakub Dawidek 
25231ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
25241ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
25251ba4a712SPawel Jakub Dawidek }
25261ba4a712SPawel Jakub Dawidek 
2527c2cda609SPawel Jakub Dawidek static void
2528c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2529c2cda609SPawel Jakub Dawidek {
2530b38ff370SJamie Gritton 
2531b38ff370SJamie Gritton 	prison_deref((struct prison *)context, 0);
2532b38ff370SJamie Gritton }
2533b38ff370SJamie Gritton 
2534b38ff370SJamie Gritton /*
2535b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2536b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2537b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2538b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2539b38ff370SJamie Gritton  */
2540b38ff370SJamie Gritton static void
2541b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2542b38ff370SJamie Gritton {
25430304c731SJamie Gritton 	struct prison *ppr, *tpr;
2544c2cda609SPawel Jakub Dawidek 
2545b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2546b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
25470304c731SJamie Gritton 	for (;;) {
2548e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
2549e6d5cb63SJamie Gritton 			pr->pr_uref--;
2550e6d5cb63SJamie Gritton 			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2551e6d5cb63SJamie Gritton 		}
2552b38ff370SJamie Gritton 		if (flags & PD_DEREF)
2553b38ff370SJamie Gritton 			pr->pr_ref--;
2554b38ff370SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2555b38ff370SJamie Gritton 		if (pr->pr_ref > 0) {
2556b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2557b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2558b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2559b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2560b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2561b38ff370SJamie Gritton 			return;
2562b38ff370SJamie Gritton 		}
2563c2cda609SPawel Jakub Dawidek 
2564b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2565b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2566b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2567b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2568c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2569b38ff370SJamie Gritton 			}
2570b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2571b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2572b38ff370SJamie Gritton 
2573b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
25740304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
25750304c731SJamie Gritton 		ppr = pr->pr_parent;
25760304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2577b97457e2SJamie Gritton 			tpr->pr_childcount--;
2578c4884ffaSJamie Gritton 		sx_xunlock(&allprison_lock);
25791ba4a712SPawel Jakub Dawidek 
2580679e1390SJamie Gritton #ifdef VIMAGE
25810cb8b6a9SMarko Zec 		if (pr->pr_vnet != ppr->pr_vnet)
2582679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2583679e1390SJamie Gritton #endif
25845050aa86SKonstantin Belousov 		if (pr->pr_root != NULL)
2585b3059e09SRobert Watson 			vrele(pr->pr_root);
2586b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2587413628a7SBjoern A. Zeeb #ifdef INET
2588413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2589413628a7SBjoern A. Zeeb #endif
2590b38ff370SJamie Gritton #ifdef INET6
2591b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2592b38ff370SJamie Gritton #endif
2593b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2594b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2595b38ff370SJamie Gritton 		osd_jail_exit(pr);
2596a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
2597a7ad07bfSEdward Tomasz Napierala 		prison_racct_detach(pr);
2598ec125fbbSEdward Tomasz Napierala #endif
25991ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
26000304c731SJamie Gritton 
26010304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
26020304c731SJamie Gritton 		pr = ppr;
26030304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
2604e6d5cb63SJamie Gritton 		flags = PD_DEREF | PD_DEUREF;
26050304c731SJamie Gritton 	}
2606b3059e09SRobert Watson }
2607b3059e09SRobert Watson 
260891421ba2SRobert Watson void
26091ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
26101ba4a712SPawel Jakub Dawidek {
26111ba4a712SPawel Jakub Dawidek 
26121ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
26131ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
2614af7bd9a4SJamie Gritton 	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
26151ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
26161ba4a712SPawel Jakub Dawidek }
26171ba4a712SPawel Jakub Dawidek 
26181ba4a712SPawel Jakub Dawidek void
261991421ba2SRobert Watson prison_hold(struct prison *pr)
262091421ba2SRobert Watson {
262191421ba2SRobert Watson 
262201137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
26231ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
262401137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
262501137630SRobert Watson }
262601137630SRobert Watson 
2627413628a7SBjoern A. Zeeb void
2628413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
262901137630SRobert Watson {
263001137630SRobert Watson 
2631413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2632b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2633b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2634b38ff370SJamie Gritton 	pr->pr_uref++;
2635413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
263675c13541SPoul-Henning Kamp }
263775c13541SPoul-Henning Kamp 
263875c13541SPoul-Henning Kamp void
2639413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
264075c13541SPoul-Henning Kamp {
2641413628a7SBjoern A. Zeeb 
2642413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2643b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2644b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2645b38ff370SJamie Gritton 	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2646413628a7SBjoern A. Zeeb }
2647413628a7SBjoern A. Zeeb 
2648413628a7SBjoern A. Zeeb 
2649413628a7SBjoern A. Zeeb #ifdef INET
2650413628a7SBjoern A. Zeeb /*
26510304c731SJamie Gritton  * Restrict a prison's IP address list with its parent's, possibly replacing
26520304c731SJamie Gritton  * it.  Return true if the replacement buffer was used (or would have been).
26530304c731SJamie Gritton  */
26540304c731SJamie Gritton static int
26550304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
26560304c731SJamie Gritton {
26570304c731SJamie Gritton 	int ii, ij, used;
26580304c731SJamie Gritton 	struct prison *ppr;
26590304c731SJamie Gritton 
26600304c731SJamie Gritton 	ppr = pr->pr_parent;
26610304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4_USER)) {
26620304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
26630304c731SJamie Gritton 		if (pr->pr_ip4s < ppr->pr_ip4s) {
26640304c731SJamie Gritton 			/*
26650304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
26660304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
26670304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
26680304c731SJamie Gritton 			 * allocate one.
26690304c731SJamie Gritton 			 */
26700304c731SJamie Gritton 			used = 1;
26710304c731SJamie Gritton 			if (newip4 == NULL) {
26720304c731SJamie Gritton 				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
26730304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
26740304c731SJamie Gritton 				if (newip4 != NULL)
26750304c731SJamie Gritton 					used = 0;
26760304c731SJamie Gritton 			}
26770304c731SJamie Gritton 			if (newip4 != NULL) {
26780304c731SJamie Gritton 				bcopy(ppr->pr_ip4, newip4,
26790304c731SJamie Gritton 				    ppr->pr_ip4s * sizeof(*newip4));
26800304c731SJamie Gritton 				free(pr->pr_ip4, M_PRISON);
26810304c731SJamie Gritton 				pr->pr_ip4 = newip4;
26820304c731SJamie Gritton 				pr->pr_ip4s = ppr->pr_ip4s;
26830304c731SJamie Gritton 			}
26840304c731SJamie Gritton 			return (used);
26850304c731SJamie Gritton 		}
26860304c731SJamie Gritton 		pr->pr_ip4s = ppr->pr_ip4s;
26870304c731SJamie Gritton 		if (pr->pr_ip4s > 0)
26880304c731SJamie Gritton 			bcopy(ppr->pr_ip4, pr->pr_ip4,
26890304c731SJamie Gritton 			    pr->pr_ip4s * sizeof(*newip4));
26900304c731SJamie Gritton 		else if (pr->pr_ip4 != NULL) {
26910304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
26920304c731SJamie Gritton 			pr->pr_ip4 = NULL;
26930304c731SJamie Gritton 		}
26942b0d6f81SJamie Gritton 	} else if (pr->pr_ip4s > 0) {
26950304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
26960304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip4s; ij++)
26970304c731SJamie Gritton 			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
26980304c731SJamie Gritton 				break;
26990304c731SJamie Gritton 		if (ij < ppr->pr_ip4s)
27000304c731SJamie Gritton 			ii = 1;
27010304c731SJamie Gritton 		else {
27020304c731SJamie Gritton 			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
27030304c731SJamie Gritton 			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
27040304c731SJamie Gritton 			ii = 0;
27050304c731SJamie Gritton 		}
27060304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip4s; ) {
27070304c731SJamie Gritton 			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
27080304c731SJamie Gritton 				ii++;
27090304c731SJamie Gritton 				continue;
27100304c731SJamie Gritton 			}
27110304c731SJamie Gritton 			switch (ij >= ppr->pr_ip4s ? -1 :
27120304c731SJamie Gritton 				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
27130304c731SJamie Gritton 			case -1:
27140304c731SJamie Gritton 				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
27150304c731SJamie Gritton 				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
27160304c731SJamie Gritton 				break;
27170304c731SJamie Gritton 			case 0:
27180304c731SJamie Gritton 				ii++;
27190304c731SJamie Gritton 				ij++;
27200304c731SJamie Gritton 				break;
27210304c731SJamie Gritton 			case 1:
27220304c731SJamie Gritton 				ij++;
27230304c731SJamie Gritton 				break;
27240304c731SJamie Gritton 			}
27250304c731SJamie Gritton 		}
27260304c731SJamie Gritton 		if (pr->pr_ip4s == 0) {
27277cbf7213SJamie Gritton 			pr->pr_flags |= PR_IP4_DISABLE;
27280304c731SJamie Gritton 			free(pr->pr_ip4, M_PRISON);
27290304c731SJamie Gritton 			pr->pr_ip4 = NULL;
27300304c731SJamie Gritton 		}
27310304c731SJamie Gritton 	}
27320304c731SJamie Gritton 	return (0);
27330304c731SJamie Gritton }
27340304c731SJamie Gritton 
27350304c731SJamie Gritton /*
2736413628a7SBjoern A. Zeeb  * Pass back primary IPv4 address of this jail.
2737413628a7SBjoern A. Zeeb  *
27380304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
27390304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. INADDR_ANY).
2740413628a7SBjoern A. Zeeb  *
2741b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2742b89e82ddSJamie Gritton  * Address returned in NBO.
2743413628a7SBjoern A. Zeeb  */
2744413628a7SBjoern A. Zeeb int
27451cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2746413628a7SBjoern A. Zeeb {
2747b38ff370SJamie Gritton 	struct prison *pr;
2748413628a7SBjoern A. Zeeb 
2749413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2750413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
275175c13541SPoul-Henning Kamp 
2752b38ff370SJamie Gritton 	pr = cred->cr_prison;
27530304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
27540304c731SJamie Gritton 		return (0);
2755b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
27560304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
27570304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
27580304c731SJamie Gritton 		return (0);
27590304c731SJamie Gritton 	}
2760b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2761b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2762b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2763b38ff370SJamie Gritton 	}
2764413628a7SBjoern A. Zeeb 
2765b38ff370SJamie Gritton 	ia->s_addr = pr->pr_ip4[0].s_addr;
2766b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2767413628a7SBjoern A. Zeeb 	return (0);
276875c13541SPoul-Henning Kamp }
2769413628a7SBjoern A. Zeeb 
2770413628a7SBjoern A. Zeeb /*
2771592bcae8SBjoern A. Zeeb  * Return 1 if we should do proper source address selection or are not jailed.
2772592bcae8SBjoern A. Zeeb  * We will return 0 if we should bypass source address selection in favour
2773592bcae8SBjoern A. Zeeb  * of the primary jail IPv4 address. Only in this case *ia will be updated and
2774592bcae8SBjoern A. Zeeb  * returned in NBO.
2775592bcae8SBjoern A. Zeeb  * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2776592bcae8SBjoern A. Zeeb  */
2777592bcae8SBjoern A. Zeeb int
2778592bcae8SBjoern A. Zeeb prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2779592bcae8SBjoern A. Zeeb {
2780592bcae8SBjoern A. Zeeb 	struct prison *pr;
2781592bcae8SBjoern A. Zeeb 	struct in_addr lia;
2782592bcae8SBjoern A. Zeeb 	int error;
2783592bcae8SBjoern A. Zeeb 
2784592bcae8SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2785592bcae8SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2786592bcae8SBjoern A. Zeeb 
2787592bcae8SBjoern A. Zeeb 	if (!jailed(cred))
2788592bcae8SBjoern A. Zeeb 		return (1);
2789592bcae8SBjoern A. Zeeb 
2790592bcae8SBjoern A. Zeeb 	pr = cred->cr_prison;
2791592bcae8SBjoern A. Zeeb 	if (pr->pr_flags & PR_IP4_SADDRSEL)
2792592bcae8SBjoern A. Zeeb 		return (1);
2793592bcae8SBjoern A. Zeeb 
2794592bcae8SBjoern A. Zeeb 	lia.s_addr = INADDR_ANY;
2795592bcae8SBjoern A. Zeeb 	error = prison_get_ip4(cred, &lia);
2796592bcae8SBjoern A. Zeeb 	if (error)
2797592bcae8SBjoern A. Zeeb 		return (error);
2798592bcae8SBjoern A. Zeeb 	if (lia.s_addr == INADDR_ANY)
2799592bcae8SBjoern A. Zeeb 		return (1);
2800592bcae8SBjoern A. Zeeb 
2801592bcae8SBjoern A. Zeeb 	ia->s_addr = lia.s_addr;
2802592bcae8SBjoern A. Zeeb 	return (0);
2803592bcae8SBjoern A. Zeeb }
2804592bcae8SBjoern A. Zeeb 
2805592bcae8SBjoern A. Zeeb /*
28060304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv4 address restrictions.
28070304c731SJamie Gritton  */
28080304c731SJamie Gritton int
28090304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2)
28100304c731SJamie Gritton {
28110304c731SJamie Gritton 
28120304c731SJamie Gritton 	if (pr1 == pr2)
28130304c731SJamie Gritton 		return (1);
28140304c731SJamie Gritton 
28150304c731SJamie Gritton 	/*
28162b0d6f81SJamie Gritton 	 * No need to lock since the PR_IP4_USER flag can't be altered for
28172b0d6f81SJamie Gritton 	 * existing prisons.
28180304c731SJamie Gritton 	 */
2819bdfc8cc4SJamie Gritton 	while (pr1 != &prison0 &&
2820bdfc8cc4SJamie Gritton #ifdef VIMAGE
2821bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_VNET) &&
2822bdfc8cc4SJamie Gritton #endif
2823bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_IP4_USER))
28240304c731SJamie Gritton 		pr1 = pr1->pr_parent;
2825bdfc8cc4SJamie Gritton 	while (pr2 != &prison0 &&
2826bdfc8cc4SJamie Gritton #ifdef VIMAGE
2827bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_VNET) &&
2828bdfc8cc4SJamie Gritton #endif
2829bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_IP4_USER))
28300304c731SJamie Gritton 		pr2 = pr2->pr_parent;
28310304c731SJamie Gritton 	return (pr1 == pr2);
28320304c731SJamie Gritton }
28330304c731SJamie Gritton 
28340304c731SJamie Gritton /*
2835413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this
2836413628a7SBjoern A. Zeeb  * jail.
2837413628a7SBjoern A. Zeeb  *
28380304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
28390304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
28400304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2841413628a7SBjoern A. Zeeb  */
2842413628a7SBjoern A. Zeeb int
2843413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2844413628a7SBjoern A. Zeeb {
2845b38ff370SJamie Gritton 	struct prison *pr;
2846413628a7SBjoern A. Zeeb 	struct in_addr ia0;
2847b38ff370SJamie Gritton 	int error;
2848413628a7SBjoern A. Zeeb 
2849413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2850413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2851413628a7SBjoern A. Zeeb 
2852b38ff370SJamie Gritton 	pr = cred->cr_prison;
28530304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
28540304c731SJamie Gritton 		return (0);
2855b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
28560304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
28570304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
28580304c731SJamie Gritton 		return (0);
28590304c731SJamie Gritton 	}
2860b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2861b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2862b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2863b38ff370SJamie Gritton 	}
2864413628a7SBjoern A. Zeeb 
2865413628a7SBjoern A. Zeeb 	ia0.s_addr = ntohl(ia->s_addr);
2866413628a7SBjoern A. Zeeb 	if (ia0.s_addr == INADDR_LOOPBACK) {
2867b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2868b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2869413628a7SBjoern A. Zeeb 		return (0);
2870413628a7SBjoern A. Zeeb 	}
2871413628a7SBjoern A. Zeeb 
2872b89e82ddSJamie Gritton 	if (ia0.s_addr == INADDR_ANY) {
2873413628a7SBjoern A. Zeeb 		/*
2874413628a7SBjoern A. Zeeb 		 * In case there is only 1 IPv4 address, bind directly.
2875413628a7SBjoern A. Zeeb 		 */
2876b38ff370SJamie Gritton 		if (pr->pr_ip4s == 1)
2877b38ff370SJamie Gritton 			ia->s_addr = pr->pr_ip4[0].s_addr;
2878b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2879413628a7SBjoern A. Zeeb 		return (0);
2880413628a7SBjoern A. Zeeb 	}
2881413628a7SBjoern A. Zeeb 
2882b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2883b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2884b38ff370SJamie Gritton 	return (error);
2885413628a7SBjoern A. Zeeb }
2886413628a7SBjoern A. Zeeb 
2887413628a7SBjoern A. Zeeb /*
2888413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
2889413628a7SBjoern A. Zeeb  *
2890b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2891b89e82ddSJamie Gritton  * Address passed in in NBO and returned in NBO.
2892413628a7SBjoern A. Zeeb  */
2893413628a7SBjoern A. Zeeb int
2894413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2895413628a7SBjoern A. Zeeb {
2896b38ff370SJamie Gritton 	struct prison *pr;
2897413628a7SBjoern A. Zeeb 
2898413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2899413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2900413628a7SBjoern A. Zeeb 
2901b38ff370SJamie Gritton 	pr = cred->cr_prison;
29020304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
29030304c731SJamie Gritton 		return (0);
2904b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29050304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
29060304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29070304c731SJamie Gritton 		return (0);
29080304c731SJamie Gritton 	}
2909b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2910b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2911b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2912b38ff370SJamie Gritton 	}
2913b89e82ddSJamie Gritton 
2914413628a7SBjoern A. Zeeb 	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2915b38ff370SJamie Gritton 		ia->s_addr = pr->pr_ip4[0].s_addr;
2916b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2917413628a7SBjoern A. Zeeb 		return (0);
2918413628a7SBjoern A. Zeeb 	}
2919413628a7SBjoern A. Zeeb 
2920413628a7SBjoern A. Zeeb 	/*
2921413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
2922413628a7SBjoern A. Zeeb 	 */
2923b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2924413628a7SBjoern A. Zeeb 	return (0);
2925413628a7SBjoern A. Zeeb }
2926413628a7SBjoern A. Zeeb 
2927413628a7SBjoern A. Zeeb /*
2928b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
2929413628a7SBjoern A. Zeeb  *
29300304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
29310304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
29320304c731SJamie Gritton  * doesn't allow IPv4.  Address passed in in NBO.
2933413628a7SBjoern A. Zeeb  */
2934413628a7SBjoern A. Zeeb static int
29350d168b8dSGleb Smirnoff _prison_check_ip4(const struct prison *pr, const struct in_addr *ia)
2936413628a7SBjoern A. Zeeb {
2937413628a7SBjoern A. Zeeb 	int i, a, z, d;
2938413628a7SBjoern A. Zeeb 
2939413628a7SBjoern A. Zeeb 	/*
2940413628a7SBjoern A. Zeeb 	 * Check the primary IP.
2941413628a7SBjoern A. Zeeb 	 */
2942413628a7SBjoern A. Zeeb 	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2943b89e82ddSJamie Gritton 		return (0);
2944413628a7SBjoern A. Zeeb 
2945413628a7SBjoern A. Zeeb 	/*
2946413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
2947413628a7SBjoern A. Zeeb 	 */
2948413628a7SBjoern A. Zeeb 	a = 0;
2949413628a7SBjoern A. Zeeb 	z = pr->pr_ip4s - 2;
2950413628a7SBjoern A. Zeeb 	while (a <= z) {
2951413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
2952413628a7SBjoern A. Zeeb 		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2953413628a7SBjoern A. Zeeb 		if (d > 0)
2954413628a7SBjoern A. Zeeb 			z = i - 1;
2955413628a7SBjoern A. Zeeb 		else if (d < 0)
2956413628a7SBjoern A. Zeeb 			a = i + 1;
2957413628a7SBjoern A. Zeeb 		else
2958413628a7SBjoern A. Zeeb 			return (0);
295975c13541SPoul-Henning Kamp 	}
296075c13541SPoul-Henning Kamp 
2961b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
2962b89e82ddSJamie Gritton }
2963b89e82ddSJamie Gritton 
296475c13541SPoul-Henning Kamp int
29650d168b8dSGleb Smirnoff prison_check_ip4(const struct ucred *cred, const struct in_addr *ia)
2966413628a7SBjoern A. Zeeb {
2967b38ff370SJamie Gritton 	struct prison *pr;
2968b38ff370SJamie Gritton 	int error;
2969413628a7SBjoern A. Zeeb 
2970413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2971413628a7SBjoern A. Zeeb 	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2972413628a7SBjoern A. Zeeb 
2973b38ff370SJamie Gritton 	pr = cred->cr_prison;
29740304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4))
29750304c731SJamie Gritton 		return (0);
2976b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
29770304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP4)) {
29780304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
29790304c731SJamie Gritton 		return (0);
29800304c731SJamie Gritton 	}
2981b38ff370SJamie Gritton 	if (pr->pr_ip4 == NULL) {
2982b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2983b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
2984b38ff370SJamie Gritton 	}
2985413628a7SBjoern A. Zeeb 
2986b38ff370SJamie Gritton 	error = _prison_check_ip4(pr, ia);
2987b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2988b38ff370SJamie Gritton 	return (error);
2989413628a7SBjoern A. Zeeb }
2990413628a7SBjoern A. Zeeb #endif
2991413628a7SBjoern A. Zeeb 
2992413628a7SBjoern A. Zeeb #ifdef INET6
29930304c731SJamie Gritton static int
29940304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
29950304c731SJamie Gritton {
29960304c731SJamie Gritton 	int ii, ij, used;
29970304c731SJamie Gritton 	struct prison *ppr;
29980304c731SJamie Gritton 
29990304c731SJamie Gritton 	ppr = pr->pr_parent;
30000304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6_USER)) {
30010304c731SJamie Gritton 		/* This has no user settings, so just copy the parent's list. */
30020304c731SJamie Gritton 		if (pr->pr_ip6s < ppr->pr_ip6s) {
30030304c731SJamie Gritton 			/*
30040304c731SJamie Gritton 			 * There's no room for the parent's list.  Use the
30050304c731SJamie Gritton 			 * new list buffer, which is assumed to be big enough
30060304c731SJamie Gritton 			 * (if it was passed).  If there's no buffer, try to
30070304c731SJamie Gritton 			 * allocate one.
30080304c731SJamie Gritton 			 */
30090304c731SJamie Gritton 			used = 1;
30100304c731SJamie Gritton 			if (newip6 == NULL) {
30110304c731SJamie Gritton 				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
30120304c731SJamie Gritton 				    M_PRISON, M_NOWAIT);
30130304c731SJamie Gritton 				if (newip6 != NULL)
30140304c731SJamie Gritton 					used = 0;
30150304c731SJamie Gritton 			}
30160304c731SJamie Gritton 			if (newip6 != NULL) {
30170304c731SJamie Gritton 				bcopy(ppr->pr_ip6, newip6,
30180304c731SJamie Gritton 				    ppr->pr_ip6s * sizeof(*newip6));
30190304c731SJamie Gritton 				free(pr->pr_ip6, M_PRISON);
30200304c731SJamie Gritton 				pr->pr_ip6 = newip6;
30210304c731SJamie Gritton 				pr->pr_ip6s = ppr->pr_ip6s;
30220304c731SJamie Gritton 			}
30230304c731SJamie Gritton 			return (used);
30240304c731SJamie Gritton 		}
30250304c731SJamie Gritton 		pr->pr_ip6s = ppr->pr_ip6s;
30260304c731SJamie Gritton 		if (pr->pr_ip6s > 0)
30270304c731SJamie Gritton 			bcopy(ppr->pr_ip6, pr->pr_ip6,
30280304c731SJamie Gritton 			    pr->pr_ip6s * sizeof(*newip6));
30290304c731SJamie Gritton 		else if (pr->pr_ip6 != NULL) {
30300304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
30310304c731SJamie Gritton 			pr->pr_ip6 = NULL;
30320304c731SJamie Gritton 		}
30332b0d6f81SJamie Gritton 	} else if (pr->pr_ip6s > 0) {
30340304c731SJamie Gritton 		/* Remove addresses that aren't in the parent. */
30350304c731SJamie Gritton 		for (ij = 0; ij < ppr->pr_ip6s; ij++)
30360304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
30370304c731SJamie Gritton 			    &ppr->pr_ip6[ij]))
30380304c731SJamie Gritton 				break;
30390304c731SJamie Gritton 		if (ij < ppr->pr_ip6s)
30400304c731SJamie Gritton 			ii = 1;
30410304c731SJamie Gritton 		else {
30420304c731SJamie Gritton 			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
30430304c731SJamie Gritton 			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
30440304c731SJamie Gritton 			ii = 0;
30450304c731SJamie Gritton 		}
30460304c731SJamie Gritton 		for (ij = 1; ii < pr->pr_ip6s; ) {
30470304c731SJamie Gritton 			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
30480304c731SJamie Gritton 			    &ppr->pr_ip6[0])) {
30490304c731SJamie Gritton 				ii++;
30500304c731SJamie Gritton 				continue;
30510304c731SJamie Gritton 			}
3052da0770bdSAndrey V. Elsukov 			switch (ij >= ppr->pr_ip6s ? -1 :
30530304c731SJamie Gritton 				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
30540304c731SJamie Gritton 			case -1:
30550304c731SJamie Gritton 				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
30560304c731SJamie Gritton 				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
30570304c731SJamie Gritton 				break;
30580304c731SJamie Gritton 			case 0:
30590304c731SJamie Gritton 				ii++;
30600304c731SJamie Gritton 				ij++;
30610304c731SJamie Gritton 				break;
30620304c731SJamie Gritton 			case 1:
30630304c731SJamie Gritton 				ij++;
30640304c731SJamie Gritton 				break;
30650304c731SJamie Gritton 			}
30660304c731SJamie Gritton 		}
30670304c731SJamie Gritton 		if (pr->pr_ip6s == 0) {
30687cbf7213SJamie Gritton 			pr->pr_flags |= PR_IP6_DISABLE;
30690304c731SJamie Gritton 			free(pr->pr_ip6, M_PRISON);
30700304c731SJamie Gritton 			pr->pr_ip6 = NULL;
30710304c731SJamie Gritton 		}
30720304c731SJamie Gritton 	}
30730304c731SJamie Gritton 	return 0;
30740304c731SJamie Gritton }
30750304c731SJamie Gritton 
3076413628a7SBjoern A. Zeeb /*
3077413628a7SBjoern A. Zeeb  * Pass back primary IPv6 address for this jail.
3078413628a7SBjoern A. Zeeb  *
30790304c731SJamie Gritton  * If not restricted return success but do not alter the address.  Caller has
30800304c731SJamie Gritton  * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3081413628a7SBjoern A. Zeeb  *
3082b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3083413628a7SBjoern A. Zeeb  */
3084413628a7SBjoern A. Zeeb int
30851cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3086413628a7SBjoern A. Zeeb {
3087b38ff370SJamie Gritton 	struct prison *pr;
3088413628a7SBjoern A. Zeeb 
3089413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3090413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3091413628a7SBjoern A. Zeeb 
3092b38ff370SJamie Gritton 	pr = cred->cr_prison;
30930304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
30940304c731SJamie Gritton 		return (0);
3095b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
30960304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
30970304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
30980304c731SJamie Gritton 		return (0);
30990304c731SJamie Gritton 	}
3100b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3101b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3102b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3103b38ff370SJamie Gritton 	}
3104b89e82ddSJamie Gritton 
3105b38ff370SJamie Gritton 	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3106b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3107413628a7SBjoern A. Zeeb 	return (0);
3108413628a7SBjoern A. Zeeb }
3109413628a7SBjoern A. Zeeb 
3110413628a7SBjoern A. Zeeb /*
3111592bcae8SBjoern A. Zeeb  * Return 1 if we should do proper source address selection or are not jailed.
3112592bcae8SBjoern A. Zeeb  * We will return 0 if we should bypass source address selection in favour
3113592bcae8SBjoern A. Zeeb  * of the primary jail IPv6 address. Only in this case *ia will be updated and
3114592bcae8SBjoern A. Zeeb  * returned in NBO.
3115592bcae8SBjoern A. Zeeb  * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3116592bcae8SBjoern A. Zeeb  */
3117592bcae8SBjoern A. Zeeb int
3118592bcae8SBjoern A. Zeeb prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3119592bcae8SBjoern A. Zeeb {
3120592bcae8SBjoern A. Zeeb 	struct prison *pr;
3121592bcae8SBjoern A. Zeeb 	struct in6_addr lia6;
3122592bcae8SBjoern A. Zeeb 	int error;
3123592bcae8SBjoern A. Zeeb 
3124592bcae8SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3125592bcae8SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3126592bcae8SBjoern A. Zeeb 
3127592bcae8SBjoern A. Zeeb 	if (!jailed(cred))
3128592bcae8SBjoern A. Zeeb 		return (1);
3129592bcae8SBjoern A. Zeeb 
3130592bcae8SBjoern A. Zeeb 	pr = cred->cr_prison;
3131592bcae8SBjoern A. Zeeb 	if (pr->pr_flags & PR_IP6_SADDRSEL)
3132592bcae8SBjoern A. Zeeb 		return (1);
3133592bcae8SBjoern A. Zeeb 
3134592bcae8SBjoern A. Zeeb 	lia6 = in6addr_any;
3135592bcae8SBjoern A. Zeeb 	error = prison_get_ip6(cred, &lia6);
3136592bcae8SBjoern A. Zeeb 	if (error)
3137592bcae8SBjoern A. Zeeb 		return (error);
3138592bcae8SBjoern A. Zeeb 	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3139592bcae8SBjoern A. Zeeb 		return (1);
3140592bcae8SBjoern A. Zeeb 
3141592bcae8SBjoern A. Zeeb 	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3142592bcae8SBjoern A. Zeeb 	return (0);
3143592bcae8SBjoern A. Zeeb }
3144592bcae8SBjoern A. Zeeb 
3145592bcae8SBjoern A. Zeeb /*
31460304c731SJamie Gritton  * Return true if pr1 and pr2 have the same IPv6 address restrictions.
31470304c731SJamie Gritton  */
31480304c731SJamie Gritton int
31490304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2)
31500304c731SJamie Gritton {
31510304c731SJamie Gritton 
31520304c731SJamie Gritton 	if (pr1 == pr2)
31530304c731SJamie Gritton 		return (1);
31540304c731SJamie Gritton 
3155bdfc8cc4SJamie Gritton 	while (pr1 != &prison0 &&
3156bdfc8cc4SJamie Gritton #ifdef VIMAGE
3157bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_VNET) &&
3158bdfc8cc4SJamie Gritton #endif
3159bdfc8cc4SJamie Gritton 	       !(pr1->pr_flags & PR_IP6_USER))
31600304c731SJamie Gritton 		pr1 = pr1->pr_parent;
3161bdfc8cc4SJamie Gritton 	while (pr2 != &prison0 &&
3162bdfc8cc4SJamie Gritton #ifdef VIMAGE
3163bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_VNET) &&
3164bdfc8cc4SJamie Gritton #endif
3165bdfc8cc4SJamie Gritton 	       !(pr2->pr_flags & PR_IP6_USER))
31660304c731SJamie Gritton 		pr2 = pr2->pr_parent;
31670304c731SJamie Gritton 	return (pr1 == pr2);
31680304c731SJamie Gritton }
31690304c731SJamie Gritton 
31700304c731SJamie Gritton /*
3171413628a7SBjoern A. Zeeb  * Make sure our (source) address is set to something meaningful to this jail.
3172413628a7SBjoern A. Zeeb  *
3173413628a7SBjoern A. Zeeb  * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3174413628a7SBjoern A. Zeeb  * when needed while binding.
3175413628a7SBjoern A. Zeeb  *
31760304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
31770304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
31780304c731SJamie Gritton  * doesn't allow IPv6.
3179413628a7SBjoern A. Zeeb  */
3180413628a7SBjoern A. Zeeb int
3181413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3182413628a7SBjoern A. Zeeb {
3183b38ff370SJamie Gritton 	struct prison *pr;
3184b38ff370SJamie Gritton 	int error;
3185413628a7SBjoern A. Zeeb 
3186413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3187413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3188413628a7SBjoern A. Zeeb 
3189b38ff370SJamie Gritton 	pr = cred->cr_prison;
31900304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
31910304c731SJamie Gritton 		return (0);
3192b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
31930304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
31940304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
31950304c731SJamie Gritton 		return (0);
31960304c731SJamie Gritton 	}
3197b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3198b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3199b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3200b38ff370SJamie Gritton 	}
3201b89e82ddSJamie Gritton 
3202413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3203b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3204b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3205413628a7SBjoern A. Zeeb 		return (0);
3206413628a7SBjoern A. Zeeb 	}
3207413628a7SBjoern A. Zeeb 
3208b89e82ddSJamie Gritton 	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3209413628a7SBjoern A. Zeeb 		/*
3210b89e82ddSJamie Gritton 		 * In case there is only 1 IPv6 address, and v6only is true,
3211b89e82ddSJamie Gritton 		 * then bind directly.
3212413628a7SBjoern A. Zeeb 		 */
3213b38ff370SJamie Gritton 		if (v6only != 0 && pr->pr_ip6s == 1)
3214b38ff370SJamie Gritton 			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3215b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3216413628a7SBjoern A. Zeeb 		return (0);
3217413628a7SBjoern A. Zeeb 	}
3218b89e82ddSJamie Gritton 
3219b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3220b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3221b38ff370SJamie Gritton 	return (error);
3222413628a7SBjoern A. Zeeb }
3223413628a7SBjoern A. Zeeb 
3224413628a7SBjoern A. Zeeb /*
3225413628a7SBjoern A. Zeeb  * Rewrite destination address in case we will connect to loopback address.
3226413628a7SBjoern A. Zeeb  *
3227b89e82ddSJamie Gritton  * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3228413628a7SBjoern A. Zeeb  */
3229413628a7SBjoern A. Zeeb int
3230413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3231413628a7SBjoern A. Zeeb {
3232b38ff370SJamie Gritton 	struct prison *pr;
3233413628a7SBjoern A. Zeeb 
3234413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3235413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3236413628a7SBjoern A. Zeeb 
3237b38ff370SJamie Gritton 	pr = cred->cr_prison;
32380304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
32390304c731SJamie Gritton 		return (0);
3240b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
32410304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
32420304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
32430304c731SJamie Gritton 		return (0);
32440304c731SJamie Gritton 	}
3245b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3246b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3247b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3248b38ff370SJamie Gritton 	}
3249b89e82ddSJamie Gritton 
3250413628a7SBjoern A. Zeeb 	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3251b38ff370SJamie Gritton 		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3252b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3253413628a7SBjoern A. Zeeb 		return (0);
3254413628a7SBjoern A. Zeeb 	}
3255413628a7SBjoern A. Zeeb 
3256413628a7SBjoern A. Zeeb 	/*
3257413628a7SBjoern A. Zeeb 	 * Return success because nothing had to be changed.
3258413628a7SBjoern A. Zeeb 	 */
3259b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3260413628a7SBjoern A. Zeeb 	return (0);
3261413628a7SBjoern A. Zeeb }
3262413628a7SBjoern A. Zeeb 
3263413628a7SBjoern A. Zeeb /*
3264b89e82ddSJamie Gritton  * Check if given address belongs to the jail referenced by cred/prison.
3265413628a7SBjoern A. Zeeb  *
32660304c731SJamie Gritton  * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
32670304c731SJamie Gritton  * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
32680304c731SJamie Gritton  * doesn't allow IPv6.
3269413628a7SBjoern A. Zeeb  */
3270413628a7SBjoern A. Zeeb static int
3271413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3272413628a7SBjoern A. Zeeb {
3273413628a7SBjoern A. Zeeb 	int i, a, z, d;
3274413628a7SBjoern A. Zeeb 
3275413628a7SBjoern A. Zeeb 	/*
3276413628a7SBjoern A. Zeeb 	 * Check the primary IP.
3277413628a7SBjoern A. Zeeb 	 */
3278413628a7SBjoern A. Zeeb 	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3279b89e82ddSJamie Gritton 		return (0);
3280413628a7SBjoern A. Zeeb 
3281413628a7SBjoern A. Zeeb 	/*
3282413628a7SBjoern A. Zeeb 	 * All the other IPs are sorted so we can do a binary search.
3283413628a7SBjoern A. Zeeb 	 */
3284413628a7SBjoern A. Zeeb 	a = 0;
3285413628a7SBjoern A. Zeeb 	z = pr->pr_ip6s - 2;
3286413628a7SBjoern A. Zeeb 	while (a <= z) {
3287413628a7SBjoern A. Zeeb 		i = (a + z) / 2;
3288413628a7SBjoern A. Zeeb 		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3289413628a7SBjoern A. Zeeb 		if (d > 0)
3290413628a7SBjoern A. Zeeb 			z = i - 1;
3291413628a7SBjoern A. Zeeb 		else if (d < 0)
3292413628a7SBjoern A. Zeeb 			a = i + 1;
3293413628a7SBjoern A. Zeeb 		else
3294413628a7SBjoern A. Zeeb 			return (0);
3295413628a7SBjoern A. Zeeb 	}
3296413628a7SBjoern A. Zeeb 
3297b89e82ddSJamie Gritton 	return (EADDRNOTAVAIL);
3298b89e82ddSJamie Gritton }
3299b89e82ddSJamie Gritton 
3300413628a7SBjoern A. Zeeb int
3301413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3302413628a7SBjoern A. Zeeb {
3303b38ff370SJamie Gritton 	struct prison *pr;
3304b38ff370SJamie Gritton 	int error;
3305413628a7SBjoern A. Zeeb 
3306413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3307413628a7SBjoern A. Zeeb 	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3308413628a7SBjoern A. Zeeb 
3309b38ff370SJamie Gritton 	pr = cred->cr_prison;
33100304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6))
33110304c731SJamie Gritton 		return (0);
3312b38ff370SJamie Gritton 	mtx_lock(&pr->pr_mtx);
33130304c731SJamie Gritton 	if (!(pr->pr_flags & PR_IP6)) {
33140304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
33150304c731SJamie Gritton 		return (0);
33160304c731SJamie Gritton 	}
3317b38ff370SJamie Gritton 	if (pr->pr_ip6 == NULL) {
3318b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
3319b89e82ddSJamie Gritton 		return (EAFNOSUPPORT);
3320b38ff370SJamie Gritton 	}
3321413628a7SBjoern A. Zeeb 
3322b38ff370SJamie Gritton 	error = _prison_check_ip6(pr, ia6);
3323b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
3324b38ff370SJamie Gritton 	return (error);
3325413628a7SBjoern A. Zeeb }
3326413628a7SBjoern A. Zeeb #endif
3327413628a7SBjoern A. Zeeb 
3328413628a7SBjoern A. Zeeb /*
3329ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
3330ca04ba64SJamie Gritton  *
3331ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3332ca04ba64SJamie Gritton  * if not.
3333ca04ba64SJamie Gritton  */
3334ca04ba64SJamie Gritton int
3335ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
3336ca04ba64SJamie Gritton {
33370304c731SJamie Gritton 	struct prison *pr;
3338ca04ba64SJamie Gritton 	int error;
3339ca04ba64SJamie Gritton 
3340ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3341ca04ba64SJamie Gritton 
33420304c731SJamie Gritton 	pr = cred->cr_prison;
3343499650a0SJamie Gritton #ifdef VIMAGE
33446bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
3345de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
33466bb79563SJamie Gritton 		return (0);
3347499650a0SJamie Gritton #endif
33486bb79563SJamie Gritton 
3349ca04ba64SJamie Gritton 	error = 0;
3350ca04ba64SJamie Gritton 	switch (af)
3351ca04ba64SJamie Gritton 	{
3352ca04ba64SJamie Gritton #ifdef INET
3353ca04ba64SJamie Gritton 	case AF_INET:
33540304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
33550304c731SJamie Gritton 		{
33560304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
33570304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3358ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33590304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33600304c731SJamie Gritton 		}
3361ca04ba64SJamie Gritton 		break;
3362ca04ba64SJamie Gritton #endif
3363ca04ba64SJamie Gritton #ifdef INET6
3364ca04ba64SJamie Gritton 	case AF_INET6:
33650304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
33660304c731SJamie Gritton 		{
33670304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
33680304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3369ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
33700304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
33710304c731SJamie Gritton 		}
3372ca04ba64SJamie Gritton 		break;
3373ca04ba64SJamie Gritton #endif
3374ca04ba64SJamie Gritton 	case AF_LOCAL:
3375ca04ba64SJamie Gritton 	case AF_ROUTE:
3376ca04ba64SJamie Gritton 		break;
3377ca04ba64SJamie Gritton 	default:
33780304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3379ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
3380ca04ba64SJamie Gritton 	}
3381ca04ba64SJamie Gritton 	return (error);
3382ca04ba64SJamie Gritton }
3383ca04ba64SJamie Gritton 
3384ca04ba64SJamie Gritton /*
3385413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
3386413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
3387413628a7SBjoern A. Zeeb  *
33880304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
33890304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
33900304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3391413628a7SBjoern A. Zeeb  */
3392413628a7SBjoern A. Zeeb int
339391421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
339475c13541SPoul-Henning Kamp {
3395413628a7SBjoern A. Zeeb #ifdef INET
33969ddb7954SMike Barcroft 	struct sockaddr_in *sai;
3397413628a7SBjoern A. Zeeb #endif
3398413628a7SBjoern A. Zeeb #ifdef INET6
3399413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
3400413628a7SBjoern A. Zeeb #endif
3401b89e82ddSJamie Gritton 	int error;
340275c13541SPoul-Henning Kamp 
3403413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3404413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3405413628a7SBjoern A. Zeeb 
3406de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3407de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3408de0bd6f7SBjoern A. Zeeb 		return (0);
3409de0bd6f7SBjoern A. Zeeb #endif
3410de0bd6f7SBjoern A. Zeeb 
3411b89e82ddSJamie Gritton 	error = 0;
3412413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
3413413628a7SBjoern A. Zeeb 	{
3414413628a7SBjoern A. Zeeb #ifdef INET
3415413628a7SBjoern A. Zeeb 	case AF_INET:
34169ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
3417b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
3418413628a7SBjoern A. Zeeb 		break;
3419413628a7SBjoern A. Zeeb #endif
3420413628a7SBjoern A. Zeeb #ifdef INET6
3421413628a7SBjoern A. Zeeb 	case AF_INET6:
3422413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
3423b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
3424413628a7SBjoern A. Zeeb 		break;
3425413628a7SBjoern A. Zeeb #endif
3426413628a7SBjoern A. Zeeb 	default:
34270304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3428b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
3429413628a7SBjoern A. Zeeb 	}
3430b89e82ddSJamie Gritton 	return (error);
343175c13541SPoul-Henning Kamp }
343291421ba2SRobert Watson 
343391421ba2SRobert Watson /*
343491421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
343591421ba2SRobert Watson  */
343691421ba2SRobert Watson int
34379ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
343891421ba2SRobert Watson {
343991421ba2SRobert Watson 
34400304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
34410304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
34420304c731SJamie Gritton }
344391421ba2SRobert Watson 
34440304c731SJamie Gritton /*
34450304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
34460304c731SJamie Gritton  */
34470304c731SJamie Gritton int
34480304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
34490304c731SJamie Gritton {
34500304c731SJamie Gritton 
34510304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
34520304c731SJamie Gritton 		if (pr1 == pr2)
34530304c731SJamie Gritton 			return (1);
345491421ba2SRobert Watson 	return (0);
345591421ba2SRobert Watson }
345691421ba2SRobert Watson 
345791421ba2SRobert Watson /*
345891421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
345991421ba2SRobert Watson  */
346091421ba2SRobert Watson int
34619ddb7954SMike Barcroft jailed(struct ucred *cred)
346291421ba2SRobert Watson {
346391421ba2SRobert Watson 
34640304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
346591421ba2SRobert Watson }
34669484d0c0SRobert Drehmel 
34679484d0c0SRobert Drehmel /*
3468de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
3469de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
3470de0bd6f7SBjoern A. Zeeb  */
3471de0bd6f7SBjoern A. Zeeb int
3472de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
3473de0bd6f7SBjoern A. Zeeb {
3474de0bd6f7SBjoern A. Zeeb 
3475de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
3476de0bd6f7SBjoern A. Zeeb 		return (0);
3477de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
3478de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
3479de0bd6f7SBjoern A. Zeeb 		return (0);
3480de0bd6f7SBjoern A. Zeeb #endif
3481de0bd6f7SBjoern A. Zeeb 
3482de0bd6f7SBjoern A. Zeeb 	return (1);
3483de0bd6f7SBjoern A. Zeeb }
3484de0bd6f7SBjoern A. Zeeb 
3485de0bd6f7SBjoern A. Zeeb /*
34867455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
34879484d0c0SRobert Drehmel  */
3488ad1ff099SRobert Drehmel void
34899ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
34909484d0c0SRobert Drehmel {
349176ca6f88SJamie Gritton 	struct prison *pr;
34929484d0c0SRobert Drehmel 
34937455b100SJamie Gritton 	/*
34947455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
34957455b100SJamie Gritton 	 * system's hostname.
34967455b100SJamie Gritton 	 */
349776ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
349876ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
3499c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
350076ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
35019484d0c0SRobert Drehmel }
3502fd7a8150SMike Barcroft 
35037455b100SJamie Gritton void
35047455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
35057455b100SJamie Gritton {
35067455b100SJamie Gritton 
35077455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3508c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
35097455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35107455b100SJamie Gritton }
35117455b100SJamie Gritton 
35127455b100SJamie Gritton void
35137455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
35147455b100SJamie Gritton {
35157455b100SJamie Gritton 
35167455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
3517c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
35187455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35197455b100SJamie Gritton }
35207455b100SJamie Gritton 
35217455b100SJamie Gritton void
35227455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
35237455b100SJamie Gritton {
35247455b100SJamie Gritton 
35257455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
35267455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
35277455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
35287455b100SJamie Gritton }
35297455b100SJamie Gritton 
3530eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
3531eb79e1c7SBjoern A. Zeeb /*
3532eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
3533eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
3534eb79e1c7SBjoern A. Zeeb  *
3535eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
3536eb79e1c7SBjoern A. Zeeb  */
3537eb79e1c7SBjoern A. Zeeb int
3538eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
3539eb79e1c7SBjoern A. Zeeb {
3540eb79e1c7SBjoern A. Zeeb 
3541eb79e1c7SBjoern A. Zeeb 	/*
3542eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
3543eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
3544eb79e1c7SBjoern A. Zeeb 	 */
3545eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3546eb79e1c7SBjoern A. Zeeb }
3547eb79e1c7SBjoern A. Zeeb #endif
3548eb79e1c7SBjoern A. Zeeb 
3549f08df373SRobert Watson /*
3550820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
3551820a0de9SPawel Jakub Dawidek  * status of a mount point.
3552820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
3553820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
3554820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
3555f08df373SRobert Watson  */
3556f08df373SRobert Watson int
3557820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
3558f08df373SRobert Watson {
3559820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3560820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
3561820a0de9SPawel Jakub Dawidek 	size_t len;
3562f08df373SRobert Watson 
3563820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
35640304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
35650304c731SJamie Gritton 		return (0);
3566820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
3567820a0de9SPawel Jakub Dawidek 		return (0);
35680304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
3569820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3570820a0de9SPawel Jakub Dawidek 	/*
3571820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3572820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3573820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
3574820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
3575820a0de9SPawel Jakub Dawidek 	 */
3576820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3577820a0de9SPawel Jakub Dawidek 		return (0);
3578820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3579820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
3580820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3581820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3582820a0de9SPawel Jakub Dawidek 	/*
3583820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
3584820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
3585820a0de9SPawel Jakub Dawidek 	 */
3586820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3587820a0de9SPawel Jakub Dawidek 		return (ENOENT);
3588f08df373SRobert Watson 	return (0);
3589f08df373SRobert Watson }
3590820a0de9SPawel Jakub Dawidek 
3591820a0de9SPawel Jakub Dawidek void
3592820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3593820a0de9SPawel Jakub Dawidek {
3594820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
3595820a0de9SPawel Jakub Dawidek 	struct prison *pr;
3596820a0de9SPawel Jakub Dawidek 	size_t len;
3597820a0de9SPawel Jakub Dawidek 
3598820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
35990304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
36000304c731SJamie Gritton 		return;
3601820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
3602820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3603820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3604820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3605820a0de9SPawel Jakub Dawidek 		return;
3606820a0de9SPawel Jakub Dawidek 	}
3607820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3608820a0de9SPawel Jakub Dawidek 		/*
3609820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3610820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3611820a0de9SPawel Jakub Dawidek 		 */
3612820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3613820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3614820a0de9SPawel Jakub Dawidek 		return;
3615820a0de9SPawel Jakub Dawidek 	}
3616820a0de9SPawel Jakub Dawidek 	/*
3617820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3618820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3619820a0de9SPawel Jakub Dawidek 	 */
3620820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3621820a0de9SPawel Jakub Dawidek 		return;
3622820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3623820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3624820a0de9SPawel Jakub Dawidek 	/*
3625820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3626820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3627820a0de9SPawel Jakub Dawidek 	 */
3628820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3629820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3630820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3631820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3632820a0de9SPawel Jakub Dawidek 	} else {
3633820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3634820a0de9SPawel Jakub Dawidek 	}
3635f08df373SRobert Watson }
3636f08df373SRobert Watson 
3637800c9408SRobert Watson /*
3638800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3639800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3640800c9408SRobert Watson  */
3641800c9408SRobert Watson int
3642800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3643800c9408SRobert Watson {
3644800c9408SRobert Watson 
3645800c9408SRobert Watson 	if (!jailed(cred))
3646800c9408SRobert Watson 		return (0);
3647800c9408SRobert Watson 
36486bb79563SJamie Gritton #ifdef VIMAGE
36496bb79563SJamie Gritton 	/*
36506bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
36516bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
36526bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
36536bb79563SJamie Gritton 	 */
36546bb79563SJamie Gritton 	switch (priv) {
36556bb79563SJamie Gritton #ifdef notyet
36566bb79563SJamie Gritton 		/*
36576bb79563SJamie Gritton 		 * NFS-specific privileges.
36586bb79563SJamie Gritton 		 */
36596bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
36606bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
36616bb79563SJamie Gritton #endif
36626bb79563SJamie Gritton 		/*
36636bb79563SJamie Gritton 		 * Network stack privileges.
36646bb79563SJamie Gritton 		 */
36656bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
36666bb79563SJamie Gritton 	case PRIV_NET_GRE:
36676bb79563SJamie Gritton 	case PRIV_NET_BPF:
36686bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
36696bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
36706bb79563SJamie Gritton 	case PRIV_NET_TAP:
36716bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
36726bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
36736bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3674215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
36756bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
36766bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
36776bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
36786bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
36796bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
36806bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
36816bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
36826bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
36836bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
36846bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
36856bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
36866bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
36876bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
36886bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
36896bb79563SJamie Gritton 	case PRIV_NET_LAGG:
36906bb79563SJamie Gritton 	case PRIV_NET_GIF:
36916bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
369235fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
36936bb79563SJamie Gritton 
36946bb79563SJamie Gritton 		/*
36956bb79563SJamie Gritton 		 * 802.11-related privileges.
36966bb79563SJamie Gritton 		 */
36976bb79563SJamie Gritton 	case PRIV_NET80211_GETKEY:
36986bb79563SJamie Gritton #ifdef notyet
36996bb79563SJamie Gritton 	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
37006bb79563SJamie Gritton #endif
37016bb79563SJamie Gritton 
37026bb79563SJamie Gritton #ifdef notyet
37036bb79563SJamie Gritton 		/*
37046bb79563SJamie Gritton 		 * ATM privileges.
37056bb79563SJamie Gritton 		 */
37066bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
37076bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
37086bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
37096bb79563SJamie Gritton 	case PRIV_NETATM_SET:
37106bb79563SJamie Gritton 
37116bb79563SJamie Gritton 		/*
37126bb79563SJamie Gritton 		 * Bluetooth privileges.
37136bb79563SJamie Gritton 		 */
37146bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
37156bb79563SJamie Gritton #endif
37166bb79563SJamie Gritton 
37176bb79563SJamie Gritton 		/*
37186bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
37196bb79563SJamie Gritton 		 */
37206bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
37216bb79563SJamie Gritton #ifdef notyet
37226bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
37236bb79563SJamie Gritton #endif
37246bb79563SJamie Gritton 
37256bb79563SJamie Gritton 		/*
37266bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
37276bb79563SJamie Gritton 		 */
37286bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
37296bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
37306bb79563SJamie Gritton 	case PRIV_NETINET_PF:
37316bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
37326bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
37336bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
37346bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
37356bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
37366bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
37376bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
37386bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
37396bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
37406bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
37416bb79563SJamie Gritton 
37426bb79563SJamie Gritton #ifdef notyet
37436bb79563SJamie Gritton 		/*
37446bb79563SJamie Gritton 		 * NCP privileges.
37456bb79563SJamie Gritton 		 */
37466bb79563SJamie Gritton 	case PRIV_NETNCP:
37476bb79563SJamie Gritton 
37486bb79563SJamie Gritton 		/*
37496bb79563SJamie Gritton 		 * SMB privileges.
37506bb79563SJamie Gritton 		 */
37516bb79563SJamie Gritton 	case PRIV_NETSMB:
37526bb79563SJamie Gritton #endif
37536bb79563SJamie Gritton 
37546bb79563SJamie Gritton 	/*
37556bb79563SJamie Gritton 	 * No default: or deny here.
37566bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
37576bb79563SJamie Gritton 	 */
37586bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
37596bb79563SJamie Gritton 			return (0);
37606bb79563SJamie Gritton 	}
37616bb79563SJamie Gritton #endif /* VIMAGE */
37626bb79563SJamie Gritton 
3763800c9408SRobert Watson 	switch (priv) {
3764800c9408SRobert Watson 
3765800c9408SRobert Watson 		/*
3766800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3767800c9408SRobert Watson 		 */
3768800c9408SRobert Watson 	case PRIV_KTRACE:
3769800c9408SRobert Watson 
3770c3c1b5e6SRobert Watson #if 0
3771800c9408SRobert Watson 		/*
3772800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3773800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3774800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3775800c9408SRobert Watson 		 * jail.
3776800c9408SRobert Watson 		 */
3777800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3778800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3779800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3780c3c1b5e6SRobert Watson #endif
3781800c9408SRobert Watson 
3782800c9408SRobert Watson 		/*
3783800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3784800c9408SRobert Watson 		 * credentials in any way they see fit.
3785800c9408SRobert Watson 		 */
3786800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3787800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3788800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3789800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3790800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3791800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3792800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3793800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3794800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3795800c9408SRobert Watson 
3796800c9408SRobert Watson 		/*
3797800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3798800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3799800c9408SRobert Watson 		 */
3800800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3801800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3802800c9408SRobert Watson 
3803800c9408SRobert Watson 		/*
3804800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3805800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3806800c9408SRobert Watson 		 */
3807800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3808800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3809800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3810800c9408SRobert Watson 
3811800c9408SRobert Watson 		/*
3812800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3813800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3814800c9408SRobert Watson 		 */
3815800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3816800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3817800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3818800c9408SRobert Watson 
3819800c9408SRobert Watson 		/*
3820800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3821800c9408SRobert Watson 		 */
3822800c9408SRobert Watson 	case PRIV_IPC_READ:
3823800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3824800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3825800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3826800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3827800c9408SRobert Watson 
3828800c9408SRobert Watson 		/*
38290304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
38300304c731SJamie Gritton 		 */
38310304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
38320304c731SJamie Gritton 	case PRIV_JAIL_SET:
38330304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
38340304c731SJamie Gritton 
38350304c731SJamie Gritton 		/*
3836800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3837800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3838800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3839800c9408SRobert Watson 		 */
3840800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3841413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3842800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3843800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3844800c9408SRobert Watson 
3845800c9408SRobert Watson 		/*
3846800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3847800c9408SRobert Watson 		 * writable.
3848800c9408SRobert Watson 		 */
3849800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3850800c9408SRobert Watson 
3851800c9408SRobert Watson 		/*
3852800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3853e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3854e82d0201SRobert Watson 		 * configuration option.
3855800c9408SRobert Watson 		 */
385695b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
385795b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3858800c9408SRobert Watson 
3859800c9408SRobert Watson 		/*
3860800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3861800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3862800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3863800c9408SRobert Watson 		 * privileges.
3864800c9408SRobert Watson 		 */
3865800c9408SRobert Watson 	case PRIV_VFS_READ:
3866800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3867800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3868800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3869800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3870800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3871800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3872800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3873800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3874bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3875800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3876800c9408SRobert Watson 	case PRIV_VFS_LINK:
3877800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3878e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3879800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3880bb56d716SJamie Gritton 
3881bb56d716SJamie Gritton 		/*
3882bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
3883bb56d716SJamie Gritton 		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3884bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3885bb56d716SJamie Gritton 		 */
3886bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3887800c9408SRobert Watson 		return (0);
3888800c9408SRobert Watson 
3889800c9408SRobert Watson 		/*
3890800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3891800c9408SRobert Watson 		 * setting system flags.
3892800c9408SRobert Watson 		 */
3893800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
38940304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3895800c9408SRobert Watson 			return (0);
3896800c9408SRobert Watson 		else
3897800c9408SRobert Watson 			return (EPERM);
3898800c9408SRobert Watson 
3899800c9408SRobert Watson 		/*
3900f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3901f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3902f3a8d2f9SPawel Jakub Dawidek 		 */
3903f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3904f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3905f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
390624b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
3907435d4667SMartin Matuska 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3908435d4667SMartin Matuska 		    cred->cr_prison->pr_enforce_statfs < 2)
3909f3a8d2f9SPawel Jakub Dawidek 			return (0);
3910f3a8d2f9SPawel Jakub Dawidek 		else
3911f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
3912f3a8d2f9SPawel Jakub Dawidek 
3913f3a8d2f9SPawel Jakub Dawidek 		/*
39144b084056SRobert Watson 		 * Allow jailed root to bind reserved ports and reuse in-use
39154b084056SRobert Watson 		 * ports.
3916800c9408SRobert Watson 		 */
3917800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
39184b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3919800c9408SRobert Watson 		return (0);
3920800c9408SRobert Watson 
3921800c9408SRobert Watson 		/*
392279ba3952SBjoern A. Zeeb 		 * Allow jailed root to set certian IPv4/6 (option) headers.
392379ba3952SBjoern A. Zeeb 		 */
392479ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
392579ba3952SBjoern A. Zeeb 		return (0);
392679ba3952SBjoern A. Zeeb 
392779ba3952SBjoern A. Zeeb 		/*
3928800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3929800c9408SRobert Watson 		 */
3930800c9408SRobert Watson 	case PRIV_NETINET_RAW:
39310304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3932800c9408SRobert Watson 			return (0);
3933800c9408SRobert Watson 		else
3934800c9408SRobert Watson 			return (EPERM);
3935800c9408SRobert Watson 
3936800c9408SRobert Watson 		/*
3937800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3938800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3939800c9408SRobert Watson 		 * jail.
3940800c9408SRobert Watson 		 */
3941800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3942800c9408SRobert Watson 		return (0);
3943800c9408SRobert Watson 
39442bfc50bcSEdward Tomasz Napierala 		/*
39452bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
39462bfc50bcSEdward Tomasz Napierala 		 */
39472bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
39482bfc50bcSEdward Tomasz Napierala 		return (0);
39492bfc50bcSEdward Tomasz Napierala 
3950800c9408SRobert Watson 	default:
3951800c9408SRobert Watson 		/*
3952800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3953800c9408SRobert Watson 		 * includes almost all network privileges, many system
3954800c9408SRobert Watson 		 * configuration privileges.
3955800c9408SRobert Watson 		 */
3956800c9408SRobert Watson 		return (EPERM);
3957800c9408SRobert Watson 	}
3958800c9408SRobert Watson }
3959800c9408SRobert Watson 
39600304c731SJamie Gritton /*
39610304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
39620304c731SJamie Gritton  * if it does not directly follow.
39630304c731SJamie Gritton  */
39640304c731SJamie Gritton 
39650304c731SJamie Gritton char *
39660304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
39670304c731SJamie Gritton {
39680304c731SJamie Gritton 	char *name;
39690304c731SJamie Gritton 
39700304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
39710304c731SJamie Gritton 	if (pr1 == pr2)
39720304c731SJamie Gritton 		return "0";
39730304c731SJamie Gritton 	name = pr2->pr_name;
39740304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
39750304c731SJamie Gritton 		/*
39760304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
39770304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
39780304c731SJamie Gritton 		 * can be counted on - and counted.
39790304c731SJamie Gritton 		 */
39800304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
39810304c731SJamie Gritton 			name = strchr(name, '.') + 1;
39820304c731SJamie Gritton 	}
39830304c731SJamie Gritton 	return (name);
39840304c731SJamie Gritton }
39850304c731SJamie Gritton 
39860304c731SJamie Gritton /*
39870304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
39880304c731SJamie Gritton  * if it does not directly follow.
39890304c731SJamie Gritton  */
39900304c731SJamie Gritton static char *
39910304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
39920304c731SJamie Gritton {
39930304c731SJamie Gritton 	char *path1, *path2;
39940304c731SJamie Gritton 	int len1;
39950304c731SJamie Gritton 
39960304c731SJamie Gritton 	path1 = pr1->pr_path;
39970304c731SJamie Gritton 	path2 = pr2->pr_path;
39980304c731SJamie Gritton 	if (!strcmp(path1, "/"))
39990304c731SJamie Gritton 		return (path2);
40000304c731SJamie Gritton 	len1 = strlen(path1);
40010304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
40020304c731SJamie Gritton 		return (path2);
40030304c731SJamie Gritton 	if (path2[len1] == '\0')
40040304c731SJamie Gritton 		return "/";
40050304c731SJamie Gritton 	if (path2[len1] == '/')
40060304c731SJamie Gritton 		return (path2 + len1);
40070304c731SJamie Gritton 	return (path2);
40080304c731SJamie Gritton }
40090304c731SJamie Gritton 
40100304c731SJamie Gritton 
40110304c731SJamie Gritton /*
40120304c731SJamie Gritton  * Jail-related sysctls.
40130304c731SJamie Gritton  */
40146472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
40150304c731SJamie Gritton     "Jails");
40160304c731SJamie Gritton 
4017fd7a8150SMike Barcroft static int
4018fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4019fd7a8150SMike Barcroft {
4020b38ff370SJamie Gritton 	struct xprison *xp;
40210304c731SJamie Gritton 	struct prison *pr, *cpr;
4022b38ff370SJamie Gritton #ifdef INET
4023b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
4024b38ff370SJamie Gritton 	int ip4s = 0;
4025b38ff370SJamie Gritton #endif
4026b38ff370SJamie Gritton #ifdef INET6
40273beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
4028b38ff370SJamie Gritton 	int ip6s = 0;
4029b38ff370SJamie Gritton #endif
40300304c731SJamie Gritton 	int descend, error;
4031fd7a8150SMike Barcroft 
4032b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
40330304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
4034b38ff370SJamie Gritton 	error = 0;
4035dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
40360304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
40370304c731SJamie Gritton #if defined(INET) || defined(INET6)
4038b38ff370SJamie Gritton  again:
40390304c731SJamie Gritton #endif
40400304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
4041413628a7SBjoern A. Zeeb #ifdef INET
40420304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
40430304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
40440304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
40450304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
4046b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
4047b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4048b38ff370SJamie Gritton 				goto again;
4049b38ff370SJamie Gritton 			}
40500304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
40510304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
4052b38ff370SJamie Gritton 		}
4053413628a7SBjoern A. Zeeb #endif
4054413628a7SBjoern A. Zeeb #ifdef INET6
40550304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
40560304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
40570304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
40580304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
4059b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
4060b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4061b38ff370SJamie Gritton 				goto again;
4062413628a7SBjoern A. Zeeb 			}
40630304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
40640304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
4065b38ff370SJamie Gritton 		}
4066b38ff370SJamie Gritton #endif
40670304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
40680304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
4069b38ff370SJamie Gritton 			continue;
4070b38ff370SJamie Gritton 		}
4071b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
4072fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
40730304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
40740304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
4075b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
40760304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4077c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
40780304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4079413628a7SBjoern A. Zeeb #ifdef INET
40800304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
4081413628a7SBjoern A. Zeeb #endif
4082413628a7SBjoern A. Zeeb #ifdef INET6
40830304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
4084413628a7SBjoern A. Zeeb #endif
40850304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
4086b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4087b38ff370SJamie Gritton 		if (error)
4088b38ff370SJamie Gritton 			break;
4089413628a7SBjoern A. Zeeb #ifdef INET
4090b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
4091b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
4092b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
4093b38ff370SJamie Gritton 			if (error)
4094b38ff370SJamie Gritton 				break;
4095413628a7SBjoern A. Zeeb 		}
4096413628a7SBjoern A. Zeeb #endif
4097413628a7SBjoern A. Zeeb #ifdef INET6
4098b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
4099b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
4100b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
4101b38ff370SJamie Gritton 			if (error)
4102b38ff370SJamie Gritton 				break;
4103413628a7SBjoern A. Zeeb 		}
4104413628a7SBjoern A. Zeeb #endif
4105fd7a8150SMike Barcroft 	}
4106dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
4107b38ff370SJamie Gritton 	free(xp, M_TEMP);
4108b38ff370SJamie Gritton #ifdef INET
4109b38ff370SJamie Gritton 	free(ip4, M_TEMP);
4110b38ff370SJamie Gritton #endif
4111b38ff370SJamie Gritton #ifdef INET6
4112b38ff370SJamie Gritton 	free(ip6, M_TEMP);
4113b38ff370SJamie Gritton #endif
4114fd7a8150SMike Barcroft 	return (error);
4115fd7a8150SMike Barcroft }
4116fd7a8150SMike Barcroft 
4117f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
4118f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4119f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
4120461167c2SPawel Jakub Dawidek 
4121461167c2SPawel Jakub Dawidek static int
4122461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4123461167c2SPawel Jakub Dawidek {
4124461167c2SPawel Jakub Dawidek 	int error, injail;
4125461167c2SPawel Jakub Dawidek 
4126461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
4127461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4128461167c2SPawel Jakub Dawidek 
4129461167c2SPawel Jakub Dawidek 	return (error);
4130461167c2SPawel Jakub Dawidek }
41310304c731SJamie Gritton 
4132f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4133f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4134f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
4135413628a7SBjoern A. Zeeb 
4136761d2bb5SJamie Gritton static int
4137761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4138761d2bb5SJamie Gritton {
4139761d2bb5SJamie Gritton 	int error, havevnet;
4140761d2bb5SJamie Gritton #ifdef VIMAGE
4141761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
4142761d2bb5SJamie Gritton 
4143761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
4144761d2bb5SJamie Gritton #else
4145761d2bb5SJamie Gritton 	havevnet = 0;
4146761d2bb5SJamie Gritton #endif
4147761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4148761d2bb5SJamie Gritton 
4149761d2bb5SJamie Gritton 	return (error);
4150761d2bb5SJamie Gritton }
4151761d2bb5SJamie Gritton 
4152761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4153761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4154761d2bb5SJamie Gritton     sysctl_jail_vnet, "I", "Jail owns VNET?");
4155761d2bb5SJamie Gritton 
41560304c731SJamie Gritton #if defined(INET) || defined(INET6)
4157e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
41580304c731SJamie Gritton     &jail_max_af_ips, 0,
41590304c731SJamie Gritton     "Number of IP addresses a jail may have at most per address family");
41600304c731SJamie Gritton #endif
41610304c731SJamie Gritton 
41620304c731SJamie Gritton /*
41630304c731SJamie Gritton  * Default parameters for jail(2) compatability.  For historical reasons,
41640304c731SJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
41650304c731SJamie Gritton  * just see their own parameters, and can't change them.
41660304c731SJamie Gritton  */
41670304c731SJamie Gritton static int
41680304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
41690304c731SJamie Gritton {
41700304c731SJamie Gritton 	struct prison *pr;
41710304c731SJamie Gritton 	int allow, error, i;
41720304c731SJamie Gritton 
41730304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
41740304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
41750304c731SJamie Gritton 
41760304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
41770304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
41780304c731SJamie Gritton 	if (arg1 != NULL)
41790304c731SJamie Gritton 		i = !i;
41800304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
41810304c731SJamie Gritton 	if (error || !req->newptr)
41820304c731SJamie Gritton 		return (error);
41830304c731SJamie Gritton 	i = i ? arg2 : 0;
41840304c731SJamie Gritton 	if (arg1 != NULL)
41850304c731SJamie Gritton 		i ^= arg2;
41860304c731SJamie Gritton 	/*
41870304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
41880304c731SJamie Gritton 	 * for writing.
41890304c731SJamie Gritton 	 */
41900304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
41910304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
41920304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
41930304c731SJamie Gritton 	return (0);
41940304c731SJamie Gritton }
41950304c731SJamie Gritton 
41960304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
41970304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
41980304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
41990304c731SJamie Gritton     "Processes in jail can set their hostnames");
42000304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
42010304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42020304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
42030304c731SJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only");
42040304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
42050304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42060304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
42070304c731SJamie Gritton     "Processes in jail can use System V IPC primitives");
42080304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
42090304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42100304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
42110304c731SJamie Gritton     "Prison root can create raw sockets");
42120304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
42130304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42140304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
42150304c731SJamie Gritton     "Processes in jail can alter system file flags");
42160304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
42170304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42180304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
42190304c731SJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems");
4220bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4221bf3db8aaSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4222bf3db8aaSMartin Matuska     NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4223e7af90abSMartin Matuska     "Processes in jail can mount the devfs file system");
4224bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4225bf3db8aaSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4226bf3db8aaSMartin Matuska     NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4227e7af90abSMartin Matuska     "Processes in jail can mount the nullfs file system");
422841c0675eSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
422941c0675eSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
423041c0675eSMartin Matuska     NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
423141c0675eSMartin Matuska     "Processes in jail can mount the procfs file system");
42322454886eSXin LI SYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed,
42332454886eSXin LI     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42342454886eSXin LI     NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I",
42352454886eSXin LI     "Processes in jail can mount the tmpfs file system");
4236e7af90abSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4237e7af90abSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4238e7af90abSMartin Matuska     NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4239e7af90abSMartin Matuska     "Processes in jail can mount the zfs file system");
42400304c731SJamie Gritton 
42410304c731SJamie Gritton static int
42420304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
42430304c731SJamie Gritton {
42440304c731SJamie Gritton 	struct prison *pr;
42450304c731SJamie Gritton 	int level, error;
42460304c731SJamie Gritton 
42470304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
42480304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
42490304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
42500304c731SJamie Gritton 	if (error || !req->newptr)
42510304c731SJamie Gritton 		return (error);
42520304c731SJamie Gritton 	*(int *)arg1 = level;
42530304c731SJamie Gritton 	return (0);
42540304c731SJamie Gritton }
42550304c731SJamie Gritton 
42560304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
42570304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
42580304c731SJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
42590304c731SJamie Gritton     sysctl_jail_default_level, "I",
42600304c731SJamie Gritton     "Processes in jail cannot see all mounted file systems");
42610304c731SJamie Gritton 
42620cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
42630cc207a6SMartin Matuska     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
42640cc207a6SMartin Matuska     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
42650cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
42660cc207a6SMartin Matuska     "Ruleset for the devfs filesystem in jail");
42670cc207a6SMartin Matuska 
42680304c731SJamie Gritton /*
42690304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
42700304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
42710304c731SJamie Gritton  * to make themselves and their types known.
42720304c731SJamie Gritton  */
42730304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
42740304c731SJamie Gritton     "Jail parameters");
42750304c731SJamie Gritton 
42760304c731SJamie Gritton int
42770304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
42780304c731SJamie Gritton {
42790304c731SJamie Gritton 	int i;
42800304c731SJamie Gritton 	long l;
42810304c731SJamie Gritton 	size_t s;
42820304c731SJamie Gritton 	char numbuf[12];
42830304c731SJamie Gritton 
42840304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
42850304c731SJamie Gritton 	{
42860304c731SJamie Gritton 	case CTLTYPE_LONG:
42870304c731SJamie Gritton 	case CTLTYPE_ULONG:
42880304c731SJamie Gritton 		l = 0;
42890304c731SJamie Gritton #ifdef SCTL_MASK32
42900304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
42910304c731SJamie Gritton #endif
42920304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
42930304c731SJamie Gritton 	case CTLTYPE_INT:
42940304c731SJamie Gritton 	case CTLTYPE_UINT:
42950304c731SJamie Gritton 		i = 0;
42960304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
42970304c731SJamie Gritton 	case CTLTYPE_STRING:
4298e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
42990304c731SJamie Gritton 		return
43000304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
43010304c731SJamie Gritton 	case CTLTYPE_STRUCT:
43020304c731SJamie Gritton 		s = (size_t)arg2;
43030304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
43040304c731SJamie Gritton 	}
43050304c731SJamie Gritton 	return (0);
43060304c731SJamie Gritton }
43070304c731SJamie Gritton 
43080304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
43090304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
43100304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
43110304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
43120304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
43130304c731SJamie Gritton     "I", "Jail secure level");
43140304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
43150304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
43160cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
43170cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
43180304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
43190304c731SJamie Gritton     "B", "Jail persistence");
4320679e1390SJamie Gritton #ifdef VIMAGE
4321679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
43227cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
4323679e1390SJamie Gritton #endif
43240304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
43250304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
43260304c731SJamie Gritton 
4327b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4328b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4329b97457e2SJamie Gritton     "I", "Current number of child jails");
4330b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4331b97457e2SJamie Gritton     "I", "Maximum number of child jails");
4332b97457e2SJamie Gritton 
43337cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
43340304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
43350304c731SJamie Gritton     "Jail hostname");
433676ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
433776ca6f88SJamie Gritton     "Jail NIS domainname");
433876ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
433976ca6f88SJamie Gritton     "Jail host UUID");
434076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
434176ca6f88SJamie Gritton     "LU", "Jail host ID");
43420304c731SJamie Gritton 
43430304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
43440304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
43450304c731SJamie Gritton 
43460304c731SJamie Gritton #ifdef INET
43472b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
43482b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
43490304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
43500304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
4351592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4352592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
4353592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
43540304c731SJamie Gritton #endif
43550304c731SJamie Gritton #ifdef INET6
43562b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
43572b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
43580304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
43590304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
4360592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4361592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
4362592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
43630304c731SJamie Gritton #endif
43640304c731SJamie Gritton 
43650304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
43660304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
43670304c731SJamie Gritton     "B", "Jail may set hostname");
43680304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
43690304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
43700304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
43710304c731SJamie Gritton     "B", "Jail may create raw sockets");
43720304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
43730304c731SJamie Gritton     "B", "Jail may alter system file flags");
43740304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
43750304c731SJamie Gritton     "B", "Jail may set file quotas");
43760304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
43770304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
43780304c731SJamie Gritton 
4379bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4380bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4381bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
4382bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4383e7af90abSMartin Matuska     "B", "Jail may mount the devfs file system");
4384bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4385e7af90abSMartin Matuska     "B", "Jail may mount the nullfs file system");
438641c0675eSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
438741c0675eSMartin Matuska     "B", "Jail may mount the procfs file system");
43882454886eSXin LI SYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW,
43892454886eSXin LI     "B", "Jail may mount the tmpfs file system");
4390e7af90abSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4391e7af90abSMartin Matuska     "B", "Jail may mount the zfs file system");
4392bf3db8aaSMartin Matuska 
4393097055e2SEdward Tomasz Napierala void
4394097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
4395097055e2SEdward Tomasz Napierala     void *arg2, void *arg3), void *arg2, void *arg3)
4396097055e2SEdward Tomasz Napierala {
4397a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4398097055e2SEdward Tomasz Napierala 
4399097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
4400a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
4401a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
4402097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
4403097055e2SEdward Tomasz Napierala }
44040304c731SJamie Gritton 
4405a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
4406a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
4407a7ad07bfSEdward Tomasz Napierala {
4408a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4409a7ad07bfSEdward Tomasz Napierala 
4410a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4411a7ad07bfSEdward Tomasz Napierala 
4412a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4413a7ad07bfSEdward Tomasz Napierala 		return (NULL);
4414a7ad07bfSEdward Tomasz Napierala 
4415a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4416a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
4417a7ad07bfSEdward Tomasz Napierala 			continue;
4418a7ad07bfSEdward Tomasz Napierala 
4419a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
4420a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
4421a7ad07bfSEdward Tomasz Napierala 		return (prr);
4422a7ad07bfSEdward Tomasz Napierala 	}
4423a7ad07bfSEdward Tomasz Napierala 
4424a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
4425a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4426a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
4427a7ad07bfSEdward Tomasz Napierala 
4428a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
4429a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
4430a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4431a7ad07bfSEdward Tomasz Napierala 
4432a7ad07bfSEdward Tomasz Napierala 	return (prr);
4433a7ad07bfSEdward Tomasz Napierala }
4434a7ad07bfSEdward Tomasz Napierala 
4435a7ad07bfSEdward Tomasz Napierala struct prison_racct *
4436a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
4437a7ad07bfSEdward Tomasz Napierala {
4438a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4439a7ad07bfSEdward Tomasz Napierala 
4440a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4441a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
4442a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4443a7ad07bfSEdward Tomasz Napierala 	return (prr);
4444a7ad07bfSEdward Tomasz Napierala }
4445a7ad07bfSEdward Tomasz Napierala 
4446a7ad07bfSEdward Tomasz Napierala void
4447a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
4448a7ad07bfSEdward Tomasz Napierala {
4449a7ad07bfSEdward Tomasz Napierala 
4450a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
4451a7ad07bfSEdward Tomasz Napierala }
4452a7ad07bfSEdward Tomasz Napierala 
4453c34bbd2aSEdward Tomasz Napierala static void
4454c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
4455c34bbd2aSEdward Tomasz Napierala {
4456c34bbd2aSEdward Tomasz Napierala 
4457c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4458c34bbd2aSEdward Tomasz Napierala 
4459c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
4460c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
4461c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
4462c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
4463c34bbd2aSEdward Tomasz Napierala 	}
4464c34bbd2aSEdward Tomasz Napierala }
4465c34bbd2aSEdward Tomasz Napierala 
4466a7ad07bfSEdward Tomasz Napierala void
4467a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
4468a7ad07bfSEdward Tomasz Napierala {
4469a7ad07bfSEdward Tomasz Napierala 	int old;
4470a7ad07bfSEdward Tomasz Napierala 
4471c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4472c34bbd2aSEdward Tomasz Napierala 
4473a7ad07bfSEdward Tomasz Napierala 	old = prr->prr_refcount;
4474a7ad07bfSEdward Tomasz Napierala 	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4475a7ad07bfSEdward Tomasz Napierala 		return;
4476a7ad07bfSEdward Tomasz Napierala 
4477a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4478c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
4479a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4480a7ad07bfSEdward Tomasz Napierala }
4481a7ad07bfSEdward Tomasz Napierala 
4482a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
4483a7ad07bfSEdward Tomasz Napierala static void
4484a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
4485a7ad07bfSEdward Tomasz Napierala {
4486a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
4487a7ad07bfSEdward Tomasz Napierala 
4488c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
4489c34bbd2aSEdward Tomasz Napierala 
4490a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
4491a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
4492a7ad07bfSEdward Tomasz Napierala 
4493a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
4494a7ad07bfSEdward Tomasz Napierala }
4495a7ad07bfSEdward Tomasz Napierala 
4496c34bbd2aSEdward Tomasz Napierala /*
4497c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
4498c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
4499c34bbd2aSEdward Tomasz Napierala  */
4500c34bbd2aSEdward Tomasz Napierala static void
4501c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
4502c34bbd2aSEdward Tomasz Napierala {
4503c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
4504c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
4505c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
4506c34bbd2aSEdward Tomasz Napierala 
4507c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
4508c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
4509c34bbd2aSEdward Tomasz Napierala 
4510e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4511e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
4512e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
4513c34bbd2aSEdward Tomasz Napierala 		return;
4514e30345e7SEdward Tomasz Napierala 	}
4515c34bbd2aSEdward Tomasz Napierala 
4516c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
4517c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4518c34bbd2aSEdward Tomasz Napierala 
4519c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
4520c34bbd2aSEdward Tomasz Napierala 
4521c34bbd2aSEdward Tomasz Napierala 	/*
4522c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
4523c34bbd2aSEdward Tomasz Napierala 	 */
4524c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4525c34bbd2aSEdward Tomasz Napierala 
4526c34bbd2aSEdward Tomasz Napierala 	/*
4527c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
4528c34bbd2aSEdward Tomasz Napierala 	 */
4529c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
4530c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
4531c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
4532c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
4533c34bbd2aSEdward Tomasz Napierala 		racct_proc_ucred_changed(p, cred, cred);
4534c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
4535c34bbd2aSEdward Tomasz Napierala 	}
4536c34bbd2aSEdward Tomasz Napierala 
4537c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
4538c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
4539c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
4540c34bbd2aSEdward Tomasz Napierala }
4541c34bbd2aSEdward Tomasz Napierala 
4542a7ad07bfSEdward Tomasz Napierala static void
4543a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
4544a7ad07bfSEdward Tomasz Napierala {
4545c34bbd2aSEdward Tomasz Napierala 
4546c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
4547c34bbd2aSEdward Tomasz Napierala 
4548af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
4549af3c786cSMateusz Guzik 		return;
4550a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4551a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4552a7ad07bfSEdward Tomasz Napierala }
4553a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4554a7ad07bfSEdward Tomasz Napierala 
4555413628a7SBjoern A. Zeeb #ifdef DDB
4556b38ff370SJamie Gritton 
4557b38ff370SJamie Gritton static void
4558b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4559413628a7SBjoern A. Zeeb {
45600304c731SJamie Gritton 	int fi;
4561b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4562b38ff370SJamie Gritton 	int ii;
4563413628a7SBjoern A. Zeeb #endif
45647cbf7213SJamie Gritton 	unsigned jsf;
4565413628a7SBjoern A. Zeeb #ifdef INET6
4566413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4567413628a7SBjoern A. Zeeb #endif
4568413628a7SBjoern A. Zeeb 
4569b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4570b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4571b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
45720304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4573b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4574b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4575b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4576b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4577b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4578679e1390SJamie Gritton #ifdef VIMAGE
4579679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4580679e1390SJamie Gritton #endif
4581b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4582b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
45830cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4584fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4585fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
45860304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
45870304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4588fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
45890304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
45900304c731SJamie Gritton 	    fi++)
45910304c731SJamie Gritton 		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
45920304c731SJamie Gritton 			db_printf(" %s", pr_flag_names[fi]);
45937cbf7213SJamie Gritton 	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
45947cbf7213SJamie Gritton 	    fi++) {
45957cbf7213SJamie Gritton 		jsf = pr->pr_flags &
45967cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
45977cbf7213SJamie Gritton 		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
45987cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].disable &&
45997cbf7213SJamie Gritton 		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
46007cbf7213SJamie Gritton 		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
46017cbf7213SJamie Gritton 		    : "inherit");
46027cbf7213SJamie Gritton 	}
4603fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
46040304c731SJamie Gritton 	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
46050304c731SJamie Gritton 	    fi++)
46060304c731SJamie Gritton 		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
46070304c731SJamie Gritton 			db_printf(" %s", pr_allow_names[fi]);
4608b38ff370SJamie Gritton 	db_printf("\n");
46090304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4610c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4611c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4612c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
461376ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4614413628a7SBjoern A. Zeeb #ifdef INET
4615b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4616b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4617b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4618fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
4619b38ff370SJamie Gritton 		    inet_ntoa(pr->pr_ip4[ii]));
4620413628a7SBjoern A. Zeeb #endif
4621413628a7SBjoern A. Zeeb #ifdef INET6
4622b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4623b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4624b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4625fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4626b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4627b38ff370SJamie Gritton #endif
4628b38ff370SJamie Gritton }
4629b38ff370SJamie Gritton 
4630b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4631b38ff370SJamie Gritton {
4632b38ff370SJamie Gritton 	struct prison *pr;
4633b38ff370SJamie Gritton 
4634b38ff370SJamie Gritton 	if (!have_addr) {
46350304c731SJamie Gritton 		/*
46360304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
46370304c731SJamie Gritton 		 * listed.
46380304c731SJamie Gritton 		 */
46390304c731SJamie Gritton 		db_show_prison(&prison0);
46400304c731SJamie Gritton 		if (!db_pager_quit) {
4641b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4642b38ff370SJamie Gritton 				db_show_prison(pr);
4643413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4644413628a7SBjoern A. Zeeb 					break;
4645413628a7SBjoern A. Zeeb 			}
46460304c731SJamie Gritton 		}
4647b38ff370SJamie Gritton 		return;
4648413628a7SBjoern A. Zeeb 	}
4649b38ff370SJamie Gritton 
46500304c731SJamie Gritton 	if (addr == 0)
46510304c731SJamie Gritton 		pr = &prison0;
46520304c731SJamie Gritton 	else {
4653b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4654b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4655b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4656b38ff370SJamie Gritton 				break;
4657b38ff370SJamie Gritton 		if (pr == NULL)
4658b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4659b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4660b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4661b38ff370SJamie Gritton 					break;
4662b38ff370SJamie Gritton 		if (pr == NULL)
4663b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4664b38ff370SJamie Gritton 			pr = (struct prison *)addr;
46650304c731SJamie Gritton 	}
4666b38ff370SJamie Gritton 	db_show_prison(pr);
4667b38ff370SJamie Gritton }
4668b38ff370SJamie Gritton 
4669413628a7SBjoern A. Zeeb #endif /* DDB */
4670