xref: /freebsd/sys/kern/kern_jail.c (revision 8a36da99deb0e19363ec04e4d3facd869c1028f5)
19454b2d8SWarner Losh /*-
2*8a36da99SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*8a36da99SPedro F. Giffuni  *
4413628a7SBjoern A. Zeeb  * Copyright (c) 1999 Poul-Henning Kamp.
5413628a7SBjoern A. Zeeb  * Copyright (c) 2008 Bjoern A. Zeeb.
6b38ff370SJamie Gritton  * Copyright (c) 2009 James Gritton.
7413628a7SBjoern A. Zeeb  * All rights reserved.
8b9f0b66cSBjoern A. Zeeb  *
9b9f0b66cSBjoern A. Zeeb  * Redistribution and use in source and binary forms, with or without
10b9f0b66cSBjoern A. Zeeb  * modification, are permitted provided that the following conditions
11b9f0b66cSBjoern A. Zeeb  * are met:
12b9f0b66cSBjoern A. Zeeb  * 1. Redistributions of source code must retain the above copyright
13b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer.
14b9f0b66cSBjoern A. Zeeb  * 2. Redistributions in binary form must reproduce the above copyright
15b9f0b66cSBjoern A. Zeeb  *    notice, this list of conditions and the following disclaimer in the
16b9f0b66cSBjoern A. Zeeb  *    documentation and/or other materials provided with the distribution.
17b9f0b66cSBjoern A. Zeeb  *
18b9f0b66cSBjoern A. Zeeb  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19b9f0b66cSBjoern A. Zeeb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b9f0b66cSBjoern A. Zeeb  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b9f0b66cSBjoern A. Zeeb  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22b9f0b66cSBjoern A. Zeeb  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23b9f0b66cSBjoern A. Zeeb  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24b9f0b66cSBjoern A. Zeeb  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25b9f0b66cSBjoern A. Zeeb  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26b9f0b66cSBjoern A. Zeeb  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27b9f0b66cSBjoern A. Zeeb  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28b9f0b66cSBjoern A. Zeeb  * SUCH DAMAGE.
2907901f22SPoul-Henning Kamp  */
3075c13541SPoul-Henning Kamp 
31677b542eSDavid E. O'Brien #include <sys/cdefs.h>
32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
33677b542eSDavid E. O'Brien 
3476ca6f88SJamie Gritton #include "opt_compat.h"
35413628a7SBjoern A. Zeeb #include "opt_ddb.h"
36413628a7SBjoern A. Zeeb #include "opt_inet.h"
37413628a7SBjoern A. Zeeb #include "opt_inet6.h"
3846e3b1cbSPawel Jakub Dawidek 
3975c13541SPoul-Henning Kamp #include <sys/param.h>
4075c13541SPoul-Henning Kamp #include <sys/types.h>
4175c13541SPoul-Henning Kamp #include <sys/kernel.h>
4275c13541SPoul-Henning Kamp #include <sys/systm.h>
4375c13541SPoul-Henning Kamp #include <sys/errno.h>
4475c13541SPoul-Henning Kamp #include <sys/sysproto.h>
4575c13541SPoul-Henning Kamp #include <sys/malloc.h>
460304c731SJamie Gritton #include <sys/osd.h>
47800c9408SRobert Watson #include <sys/priv.h>
4875c13541SPoul-Henning Kamp #include <sys/proc.h>
49b3059e09SRobert Watson #include <sys/taskqueue.h>
5057b4252eSKonstantin Belousov #include <sys/fcntl.h>
5175c13541SPoul-Henning Kamp #include <sys/jail.h>
5201137630SRobert Watson #include <sys/lock.h>
5301137630SRobert Watson #include <sys/mutex.h>
54097055e2SEdward Tomasz Napierala #include <sys/racct.h>
55a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h>
56dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
5776ca6f88SJamie Gritton #include <sys/sysent.h>
58fd7a8150SMike Barcroft #include <sys/namei.h>
59820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
60fd7a8150SMike Barcroft #include <sys/queue.h>
6175c13541SPoul-Henning Kamp #include <sys/socket.h>
62fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
6383f1e257SRobert Watson #include <sys/sysctl.h>
64fd7a8150SMike Barcroft #include <sys/vnode.h>
65530c0060SRobert Watson 
6675c13541SPoul-Henning Kamp #include <net/if.h>
67530c0060SRobert Watson #include <net/vnet.h>
68530c0060SRobert Watson 
6975c13541SPoul-Henning Kamp #include <netinet/in.h>
70530c0060SRobert Watson 
71413628a7SBjoern A. Zeeb #ifdef DDB
72413628a7SBjoern A. Zeeb #include <ddb/ddb.h>
73413628a7SBjoern A. Zeeb #endif /* DDB */
7475c13541SPoul-Henning Kamp 
75aed55708SRobert Watson #include <security/mac/mac_framework.h>
76aed55708SRobert Watson 
778986e3a0SJamie Gritton #define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
788986e3a0SJamie Gritton 
7975c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
80d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8175c13541SPoul-Henning Kamp 
82592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
83592bcae8SBjoern A. Zeeb #ifdef INET
84592bcae8SBjoern A. Zeeb #ifdef INET6
85592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
86592bcae8SBjoern A. Zeeb #else
87592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
88592bcae8SBjoern A. Zeeb #endif
89592bcae8SBjoern A. Zeeb #else /* !INET */
90592bcae8SBjoern A. Zeeb #ifdef INET6
91592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
92592bcae8SBjoern A. Zeeb #else
93592bcae8SBjoern A. Zeeb #define	_PR_IP_SADDRSEL	0
94592bcae8SBjoern A. Zeeb #endif
95592bcae8SBjoern A. Zeeb #endif
96592bcae8SBjoern A. Zeeb 
970304c731SJamie Gritton /* prison0 describes what is "real" about the system. */
980304c731SJamie Gritton struct prison prison0 = {
990304c731SJamie Gritton 	.pr_id		= 0,
1000304c731SJamie Gritton 	.pr_name	= "0",
1010304c731SJamie Gritton 	.pr_ref		= 1,
1020304c731SJamie Gritton 	.pr_uref	= 1,
1030304c731SJamie Gritton 	.pr_path	= "/",
1040304c731SJamie Gritton 	.pr_securelevel	= -1,
1050cc207a6SMartin Matuska 	.pr_devfs_rsnum = 0,
106b97457e2SJamie Gritton 	.pr_childmax	= JAIL_MAX,
1078986e3a0SJamie Gritton 	.pr_hostuuid	= DEFAULT_HOSTUUID,
10813e403fdSAntoine Brodin 	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
109eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
110592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
111eb79e1c7SBjoern A. Zeeb #else
112592bcae8SBjoern A. Zeeb 	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
113eb79e1c7SBjoern A. Zeeb #endif
1140304c731SJamie Gritton 	.pr_allow	= PR_ALLOW_ALL,
1150304c731SJamie Gritton };
1160304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11783f1e257SRobert Watson 
118a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
119dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
120b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
121b38ff370SJamie Gritton struct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
122a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct;
1232110d913SXin LI int	lastprid = 0;
124fd7a8150SMike Barcroft 
125b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr);
126b3059e09SRobert Watson static void prison_complete(void *context, int pending);
127b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags);
1280304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2);
1290304c731SJamie Gritton static void prison_remove_one(struct prison *pr);
130a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
131a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr);
132c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr);
133a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr);
134a7ad07bfSEdward Tomasz Napierala #endif
135fd7a8150SMike Barcroft 
136b38ff370SJamie Gritton /* Flags for prison_deref */
137b38ff370SJamie Gritton #define	PD_DEREF	0x01
138b38ff370SJamie Gritton #define	PD_DEUREF	0x02
139b38ff370SJamie Gritton #define	PD_LOCKED	0x04
140b38ff370SJamie Gritton #define	PD_LIST_SLOCKED	0x08
141b38ff370SJamie Gritton #define	PD_LIST_XLOCKED	0x10
142fd7a8150SMike Barcroft 
1430304c731SJamie Gritton /*
1445cc70397SBjoern A. Zeeb  * Parameter names corresponding to PR_* flag values.  Size values are for kvm
1455cc70397SBjoern A. Zeeb  * as we cannot figure out the size of a sparse array, or an array without a
1465cc70397SBjoern A. Zeeb  * terminating entry.
1470304c731SJamie Gritton  */
1480304c731SJamie Gritton static char *pr_flag_names[] = {
1490304c731SJamie Gritton 	[0] = "persist",
150592bcae8SBjoern A. Zeeb #ifdef INET
151592bcae8SBjoern A. Zeeb 	[7] = "ip4.saddrsel",
152592bcae8SBjoern A. Zeeb #endif
153592bcae8SBjoern A. Zeeb #ifdef INET6
154592bcae8SBjoern A. Zeeb 	[8] = "ip6.saddrsel",
155592bcae8SBjoern A. Zeeb #endif
1560304c731SJamie Gritton };
1575cc70397SBjoern A. Zeeb const size_t pr_flag_names_size = sizeof(pr_flag_names);
1580304c731SJamie Gritton 
1590304c731SJamie Gritton static char *pr_flag_nonames[] = {
1600304c731SJamie Gritton 	[0] = "nopersist",
161592bcae8SBjoern A. Zeeb #ifdef INET
162592bcae8SBjoern A. Zeeb 	[7] = "ip4.nosaddrsel",
163592bcae8SBjoern A. Zeeb #endif
164592bcae8SBjoern A. Zeeb #ifdef INET6
165592bcae8SBjoern A. Zeeb 	[8] = "ip6.nosaddrsel",
166592bcae8SBjoern A. Zeeb #endif
1677cbf7213SJamie Gritton };
1685cc70397SBjoern A. Zeeb const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
1697cbf7213SJamie Gritton 
1707cbf7213SJamie Gritton struct jailsys_flags {
1717cbf7213SJamie Gritton 	const char	*name;
1727cbf7213SJamie Gritton 	unsigned	 disable;
1737cbf7213SJamie Gritton 	unsigned	 new;
1747cbf7213SJamie Gritton } pr_flag_jailsys[] = {
1757cbf7213SJamie Gritton 	{ "host", 0, PR_HOST },
1767cbf7213SJamie Gritton #ifdef VIMAGE
1777cbf7213SJamie Gritton 	{ "vnet", 0, PR_VNET },
1787cbf7213SJamie Gritton #endif
1790304c731SJamie Gritton #ifdef INET
1806a3f2779SJamie Gritton 	{ "ip4", PR_IP4_USER, PR_IP4_USER },
1810304c731SJamie Gritton #endif
1820304c731SJamie Gritton #ifdef INET6
1836a3f2779SJamie Gritton 	{ "ip6", PR_IP6_USER, PR_IP6_USER },
184679e1390SJamie Gritton #endif
1850304c731SJamie Gritton };
1865cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
1870304c731SJamie Gritton 
1880304c731SJamie Gritton static char *pr_allow_names[] = {
1890304c731SJamie Gritton 	"allow.set_hostname",
1900304c731SJamie Gritton 	"allow.sysvipc",
1910304c731SJamie Gritton 	"allow.raw_sockets",
1920304c731SJamie Gritton 	"allow.chflags",
1930304c731SJamie Gritton 	"allow.mount",
1940304c731SJamie Gritton 	"allow.quotas",
1950304c731SJamie Gritton 	"allow.socket_af",
196bf3db8aaSMartin Matuska 	"allow.mount.devfs",
197bf3db8aaSMartin Matuska 	"allow.mount.nullfs",
198e7af90abSMartin Matuska 	"allow.mount.zfs",
19941c0675eSMartin Matuska 	"allow.mount.procfs",
2002454886eSXin LI 	"allow.mount.tmpfs",
201464aad14SJamie Gritton 	"allow.mount.fdescfs",
202f19e47d6SMarcelo Araujo 	"allow.mount.linprocfs",
203f19e47d6SMarcelo Araujo 	"allow.mount.linsysfs",
204e28f9b7dSAllan Jude 	"allow.reserved_ports",
2050304c731SJamie Gritton };
2065cc70397SBjoern A. Zeeb const size_t pr_allow_names_size = sizeof(pr_allow_names);
2070304c731SJamie Gritton 
2080304c731SJamie Gritton static char *pr_allow_nonames[] = {
2090304c731SJamie Gritton 	"allow.noset_hostname",
2100304c731SJamie Gritton 	"allow.nosysvipc",
2110304c731SJamie Gritton 	"allow.noraw_sockets",
2120304c731SJamie Gritton 	"allow.nochflags",
2130304c731SJamie Gritton 	"allow.nomount",
2140304c731SJamie Gritton 	"allow.noquotas",
2150304c731SJamie Gritton 	"allow.nosocket_af",
216bf3db8aaSMartin Matuska 	"allow.mount.nodevfs",
217bf3db8aaSMartin Matuska 	"allow.mount.nonullfs",
218e7af90abSMartin Matuska 	"allow.mount.nozfs",
21941c0675eSMartin Matuska 	"allow.mount.noprocfs",
2202454886eSXin LI 	"allow.mount.notmpfs",
221464aad14SJamie Gritton 	"allow.mount.nofdescfs",
222f19e47d6SMarcelo Araujo 	"allow.mount.nolinprocfs",
223f19e47d6SMarcelo Araujo 	"allow.mount.nolinsysfs",
224e28f9b7dSAllan Jude 	"allow.noreserved_ports",
2250304c731SJamie Gritton };
2265cc70397SBjoern A. Zeeb const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
2270304c731SJamie Gritton 
228e28f9b7dSAllan Jude #define	JAIL_DEFAULT_ALLOW		(PR_ALLOW_SET_HOSTNAME | PR_ALLOW_RESERVED_PORTS)
22942bebd82SJamie Gritton #define	JAIL_DEFAULT_ENFORCE_STATFS	2
230bf3db8aaSMartin Matuska #define	JAIL_DEFAULT_DEVFS_RSNUM	0
2310304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
23242bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
2330cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
2340304c731SJamie Gritton #if defined(INET) || defined(INET6)
235e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255;
2360304c731SJamie Gritton #endif
2370304c731SJamie Gritton 
238b96bd95bSIan Lepore /*
239b96bd95bSIan Lepore  * Initialize the parts of prison0 that can't be static-initialized with
240b96bd95bSIan Lepore  * constants.  This is called from proc0_init() after creating thread0 cpuset.
241b96bd95bSIan Lepore  */
242b96bd95bSIan Lepore void
243b96bd95bSIan Lepore prison0_init(void)
244b96bd95bSIan Lepore {
245b96bd95bSIan Lepore 
246b96bd95bSIan Lepore 	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
247b96bd95bSIan Lepore 	prison0.pr_osreldate = osreldate;
248b96bd95bSIan Lepore 	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
249b96bd95bSIan Lepore }
250b96bd95bSIan Lepore 
251116734c4SMatthew Dillon /*
2529ddb7954SMike Barcroft  * struct jail_args {
2539ddb7954SMike Barcroft  *	struct jail *jail;
2549ddb7954SMike Barcroft  * };
255116734c4SMatthew Dillon  */
25675c13541SPoul-Henning Kamp int
2578451d0ddSKip Macy sys_jail(struct thread *td, struct jail_args *uap)
25875c13541SPoul-Henning Kamp {
259413628a7SBjoern A. Zeeb 	uint32_t version;
260413628a7SBjoern A. Zeeb 	int error;
2610304c731SJamie Gritton 	struct jail j;
262413628a7SBjoern A. Zeeb 
263413628a7SBjoern A. Zeeb 	error = copyin(uap->jail, &version, sizeof(uint32_t));
264413628a7SBjoern A. Zeeb 	if (error)
265413628a7SBjoern A. Zeeb 		return (error);
266413628a7SBjoern A. Zeeb 
267413628a7SBjoern A. Zeeb 	switch (version) {
268413628a7SBjoern A. Zeeb 	case 0:
269413628a7SBjoern A. Zeeb 	{
270413628a7SBjoern A. Zeeb 		struct jail_v0 j0;
271413628a7SBjoern A. Zeeb 
2720304c731SJamie Gritton 		/* FreeBSD single IPv4 jails. */
2730304c731SJamie Gritton 		bzero(&j, sizeof(struct jail));
274413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
275413628a7SBjoern A. Zeeb 		if (error)
276413628a7SBjoern A. Zeeb 			return (error);
2770304c731SJamie Gritton 		j.version = j0.version;
2780304c731SJamie Gritton 		j.path = j0.path;
2790304c731SJamie Gritton 		j.hostname = j0.hostname;
280b5019bc4SPeter Wemm 		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
281413628a7SBjoern A. Zeeb 		break;
282413628a7SBjoern A. Zeeb 	}
283413628a7SBjoern A. Zeeb 
284413628a7SBjoern A. Zeeb 	case 1:
285413628a7SBjoern A. Zeeb 		/*
286413628a7SBjoern A. Zeeb 		 * Version 1 was used by multi-IPv4 jail implementations
287413628a7SBjoern A. Zeeb 		 * that never made it into the official kernel.
288413628a7SBjoern A. Zeeb 		 */
289413628a7SBjoern A. Zeeb 		return (EINVAL);
290413628a7SBjoern A. Zeeb 
291413628a7SBjoern A. Zeeb 	case 2:	/* JAIL_API_VERSION */
292413628a7SBjoern A. Zeeb 		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
293413628a7SBjoern A. Zeeb 		error = copyin(uap->jail, &j, sizeof(struct jail));
294413628a7SBjoern A. Zeeb 		if (error)
295413628a7SBjoern A. Zeeb 			return (error);
2960304c731SJamie Gritton 		break;
2970304c731SJamie Gritton 
2980304c731SJamie Gritton 	default:
2990304c731SJamie Gritton 		/* Sci-Fi jails are not supported, sorry. */
3000304c731SJamie Gritton 		return (EINVAL);
3010304c731SJamie Gritton 	}
3020304c731SJamie Gritton 	return (kern_jail(td, &j));
3030304c731SJamie Gritton }
3040304c731SJamie Gritton 
3050304c731SJamie Gritton int
3060304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j)
3070304c731SJamie Gritton {
30802abd400SPedro F. Giffuni 	struct iovec optiov[2 * (4 + nitems(pr_allow_names)
309e92e0574SJamie Gritton #ifdef INET
310e92e0574SJamie Gritton 			    + 1
311e92e0574SJamie Gritton #endif
312e92e0574SJamie Gritton #ifdef INET6
313e92e0574SJamie Gritton 			    + 1
314e92e0574SJamie Gritton #endif
315e92e0574SJamie Gritton 			    )];
3160304c731SJamie Gritton 	struct uio opt;
3170304c731SJamie Gritton 	char *u_path, *u_hostname, *u_name;
3180304c731SJamie Gritton #ifdef INET
319e92e0574SJamie Gritton 	uint32_t ip4s;
3200304c731SJamie Gritton 	struct in_addr *u_ip4;
3210304c731SJamie Gritton #endif
3220304c731SJamie Gritton #ifdef INET6
3230304c731SJamie Gritton 	struct in6_addr *u_ip6;
3240304c731SJamie Gritton #endif
3250304c731SJamie Gritton 	size_t tmplen;
3260304c731SJamie Gritton 	int error, enforce_statfs, fi;
3270304c731SJamie Gritton 
3280304c731SJamie Gritton 	bzero(&optiov, sizeof(optiov));
3290304c731SJamie Gritton 	opt.uio_iov = optiov;
3300304c731SJamie Gritton 	opt.uio_iovcnt = 0;
3310304c731SJamie Gritton 	opt.uio_offset = -1;
3320304c731SJamie Gritton 	opt.uio_resid = -1;
3330304c731SJamie Gritton 	opt.uio_segflg = UIO_SYSSPACE;
3340304c731SJamie Gritton 	opt.uio_rw = UIO_READ;
3350304c731SJamie Gritton 	opt.uio_td = td;
3360304c731SJamie Gritton 
3370304c731SJamie Gritton 	/* Set permissions for top-level jails from sysctls. */
3380304c731SJamie Gritton 	if (!jailed(td->td_ucred)) {
33902abd400SPedro F. Giffuni 		for (fi = 0; fi < nitems(pr_allow_names); fi++) {
3400304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_base =
3410304c731SJamie Gritton 			    (jail_default_allow & (1 << fi))
3420304c731SJamie Gritton 			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
3430304c731SJamie Gritton 			optiov[opt.uio_iovcnt].iov_len =
3440304c731SJamie Gritton 			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
3450304c731SJamie Gritton 			opt.uio_iovcnt += 2;
3460304c731SJamie Gritton 		}
3470304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
3480304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
3490304c731SJamie Gritton 		opt.uio_iovcnt++;
3500304c731SJamie Gritton 		enforce_statfs = jail_default_enforce_statfs;
3510304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
3520304c731SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
3530304c731SJamie Gritton 		opt.uio_iovcnt++;
3540304c731SJamie Gritton 	}
3550304c731SJamie Gritton 
356b38ff370SJamie Gritton 	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
357b38ff370SJamie Gritton #ifdef INET
3580304c731SJamie Gritton 	ip4s = (j->version == 0) ? 1 : j->ip4s;
3590304c731SJamie Gritton 	if (ip4s > jail_max_af_ips)
360b38ff370SJamie Gritton 		return (EINVAL);
3610304c731SJamie Gritton 	tmplen += ip4s * sizeof(struct in_addr);
362b38ff370SJamie Gritton #else
3630304c731SJamie Gritton 	if (j->ip4s > 0)
364b38ff370SJamie Gritton 		return (EINVAL);
365b38ff370SJamie Gritton #endif
366b38ff370SJamie Gritton #ifdef INET6
3670304c731SJamie Gritton 	if (j->ip6s > jail_max_af_ips)
368b38ff370SJamie Gritton 		return (EINVAL);
3690304c731SJamie Gritton 	tmplen += j->ip6s * sizeof(struct in6_addr);
370b38ff370SJamie Gritton #else
3710304c731SJamie Gritton 	if (j->ip6s > 0)
372b38ff370SJamie Gritton 		return (EINVAL);
373b38ff370SJamie Gritton #endif
374b38ff370SJamie Gritton 	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
375b38ff370SJamie Gritton 	u_hostname = u_path + MAXPATHLEN;
376b38ff370SJamie Gritton 	u_name = u_hostname + MAXHOSTNAMELEN;
377b38ff370SJamie Gritton #ifdef INET
378b38ff370SJamie Gritton 	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
379b38ff370SJamie Gritton #endif
380b38ff370SJamie Gritton #ifdef INET6
381b38ff370SJamie Gritton #ifdef INET
3820304c731SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
383b38ff370SJamie Gritton #else
384b38ff370SJamie Gritton 	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
385b38ff370SJamie Gritton #endif
386b38ff370SJamie Gritton #endif
3870304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "path";
3880304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
3890304c731SJamie Gritton 	opt.uio_iovcnt++;
3900304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_path;
3910304c731SJamie Gritton 	error = copyinstr(j->path, u_path, MAXPATHLEN,
3920304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
393b38ff370SJamie Gritton 	if (error) {
394b38ff370SJamie Gritton 		free(u_path, M_TEMP);
395b38ff370SJamie Gritton 		return (error);
396b38ff370SJamie Gritton 	}
3970304c731SJamie Gritton 	opt.uio_iovcnt++;
3980304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
3990304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
4000304c731SJamie Gritton 	opt.uio_iovcnt++;
4010304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_hostname;
4020304c731SJamie Gritton 	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
4030304c731SJamie Gritton 	    &optiov[opt.uio_iovcnt].iov_len);
404b38ff370SJamie Gritton 	if (error) {
405b38ff370SJamie Gritton 		free(u_path, M_TEMP);
406b38ff370SJamie Gritton 		return (error);
407b38ff370SJamie Gritton 	}
4080304c731SJamie Gritton 	opt.uio_iovcnt++;
4090304c731SJamie Gritton 	if (j->jailname != NULL) {
410b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = "name";
411b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
412b38ff370SJamie Gritton 		opt.uio_iovcnt++;
413b38ff370SJamie Gritton 		optiov[opt.uio_iovcnt].iov_base = u_name;
4140304c731SJamie Gritton 		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
415b38ff370SJamie Gritton 		    &optiov[opt.uio_iovcnt].iov_len);
416b38ff370SJamie Gritton 		if (error) {
417b38ff370SJamie Gritton 			free(u_path, M_TEMP);
418b38ff370SJamie Gritton 			return (error);
419b38ff370SJamie Gritton 		}
420b38ff370SJamie Gritton 		opt.uio_iovcnt++;
421b38ff370SJamie Gritton 	}
422b38ff370SJamie Gritton #ifdef INET
423b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
424b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
425b38ff370SJamie Gritton 	opt.uio_iovcnt++;
426b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip4;
4270304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
4280304c731SJamie Gritton 	if (j->version == 0)
4290304c731SJamie Gritton 		u_ip4->s_addr = j->ip4s;
4300304c731SJamie Gritton 	else {
4310304c731SJamie Gritton 		error = copyin(j->ip4, u_ip4, 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 	}
437b38ff370SJamie Gritton 	opt.uio_iovcnt++;
438b38ff370SJamie Gritton #endif
439b38ff370SJamie Gritton #ifdef INET6
440b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
441b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
442b38ff370SJamie Gritton 	opt.uio_iovcnt++;
443b38ff370SJamie Gritton 	optiov[opt.uio_iovcnt].iov_base = u_ip6;
4440304c731SJamie Gritton 	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
4450304c731SJamie Gritton 	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
446b38ff370SJamie Gritton 	if (error) {
447b38ff370SJamie Gritton 		free(u_path, M_TEMP);
448b38ff370SJamie Gritton 		return (error);
449b38ff370SJamie Gritton 	}
450b38ff370SJamie Gritton 	opt.uio_iovcnt++;
451b38ff370SJamie Gritton #endif
45202abd400SPedro F. Giffuni 	KASSERT(opt.uio_iovcnt <= nitems(optiov),
4530304c731SJamie Gritton 		("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
454b38ff370SJamie Gritton 	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
455b38ff370SJamie Gritton 	free(u_path, M_TEMP);
456b38ff370SJamie Gritton 	return (error);
457b38ff370SJamie Gritton }
458b38ff370SJamie Gritton 
4590304c731SJamie Gritton 
460b38ff370SJamie Gritton /*
461b38ff370SJamie Gritton  * struct jail_set_args {
462b38ff370SJamie Gritton  *	struct iovec *iovp;
463b38ff370SJamie Gritton  *	unsigned int iovcnt;
464b38ff370SJamie Gritton  *	int flags;
465b38ff370SJamie Gritton  * };
466b38ff370SJamie Gritton  */
467b38ff370SJamie Gritton int
4688451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap)
469b38ff370SJamie Gritton {
470b38ff370SJamie Gritton 	struct uio *auio;
471b38ff370SJamie Gritton 	int error;
472b38ff370SJamie Gritton 
473b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
474b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
475b38ff370SJamie Gritton 		return (EINVAL);
476b38ff370SJamie Gritton 
477b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
478b38ff370SJamie Gritton 	if (error)
479b38ff370SJamie Gritton 		return (error);
480b38ff370SJamie Gritton 	error = kern_jail_set(td, auio, uap->flags);
481b38ff370SJamie Gritton 	free(auio, M_IOV);
482b38ff370SJamie Gritton 	return (error);
483413628a7SBjoern A. Zeeb }
484413628a7SBjoern A. Zeeb 
485413628a7SBjoern A. Zeeb int
486b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags)
487413628a7SBjoern A. Zeeb {
488fd7a8150SMike Barcroft 	struct nameidata nd;
489b38ff370SJamie Gritton #ifdef INET
490b38ff370SJamie Gritton 	struct in_addr *ip4;
491413628a7SBjoern A. Zeeb #endif
492b38ff370SJamie Gritton #ifdef INET6
493b38ff370SJamie Gritton 	struct in6_addr *ip6;
494b38ff370SJamie Gritton #endif
495b38ff370SJamie Gritton 	struct vfsopt *opt;
496b38ff370SJamie Gritton 	struct vfsoptlist *opts;
49757aea6dfSBjoern A. Zeeb 	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
498b38ff370SJamie Gritton 	struct vnode *root;
499babbbb9cSJamie Gritton 	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
500b96bd95bSIan Lepore 	char *g_path, *osrelstr;
5010304c731SJamie Gritton #if defined(INET) || defined(INET6)
50257aea6dfSBjoern A. Zeeb 	struct prison *tppr;
503b38ff370SJamie Gritton 	void *op;
5040304c731SJamie Gritton #endif
50576ca6f88SJamie Gritton 	unsigned long hid;
506b6f47c23SJamie Gritton 	size_t namelen, onamelen, pnamelen;
507cc5fd8c7SJamie Gritton 	int born, created, cuflags, descend, enforce;
508cc5fd8c7SJamie Gritton 	int error, errmsg_len, errmsg_pos;
5090cc207a6SMartin Matuska 	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
5107cbf7213SJamie Gritton 	int fi, jid, jsys, len, level;
511b96bd95bSIan Lepore 	int childmax, osreldt, rsnum, slevel;
512f6e633a9SMartin Matuska 	int fullpath_disabled;
513d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6)
5140304c731SJamie Gritton 	int ii, ij;
515413628a7SBjoern A. Zeeb #endif
516413628a7SBjoern A. Zeeb #ifdef INET
5172b0d6f81SJamie Gritton 	int ip4s, redo_ip4;
518413628a7SBjoern A. Zeeb #endif
519b38ff370SJamie Gritton #ifdef INET6
5202b0d6f81SJamie Gritton 	int ip6s, redo_ip6;
521b38ff370SJamie Gritton #endif
5226beb3bb4SKirk McKusick 	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
5236beb3bb4SKirk McKusick 	unsigned tallow;
524b38ff370SJamie Gritton 	char numbuf[12];
525b38ff370SJamie Gritton 
526b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_SET);
527b38ff370SJamie Gritton 	if (!error && (flags & JAIL_ATTACH))
528b38ff370SJamie Gritton 		error = priv_check(td, PRIV_JAIL_ATTACH);
529b38ff370SJamie Gritton 	if (error)
53075c13541SPoul-Henning Kamp 		return (error);
531b6f47c23SJamie Gritton 	mypr = td->td_ucred->cr_prison;
532b97457e2SJamie Gritton 	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
5330304c731SJamie Gritton 		return (EPERM);
534b38ff370SJamie Gritton 	if (flags & ~JAIL_SET_MASK)
535b38ff370SJamie Gritton 		return (EINVAL);
536b38ff370SJamie Gritton 
537b38ff370SJamie Gritton 	/*
538b38ff370SJamie Gritton 	 * Check all the parameters before committing to anything.  Not all
539b38ff370SJamie Gritton 	 * errors can be caught early, but we may as well try.  Also, this
540b38ff370SJamie Gritton 	 * takes care of some expensive stuff (path lookup) before getting
541b38ff370SJamie Gritton 	 * the allprison lock.
542b38ff370SJamie Gritton 	 *
543b38ff370SJamie Gritton 	 * XXX Jails are not filesystems, and jail parameters are not mount
544b38ff370SJamie Gritton 	 *     options.  But it makes more sense to re-use the vfsopt code
545b38ff370SJamie Gritton 	 *     than duplicate it under a different name.
546b38ff370SJamie Gritton 	 */
547b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
548b38ff370SJamie Gritton 	if (error)
549b38ff370SJamie Gritton 		return (error);
550b38ff370SJamie Gritton #ifdef INET
551b38ff370SJamie Gritton 	ip4 = NULL;
552b38ff370SJamie Gritton #endif
553b38ff370SJamie Gritton #ifdef INET6
554b38ff370SJamie Gritton 	ip6 = NULL;
555b38ff370SJamie Gritton #endif
5566dfe0a3dSMartin Matuska 	g_path = NULL;
557b38ff370SJamie Gritton 
558b6f47c23SJamie Gritton 	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
559b6f47c23SJamie Gritton 	if (!cuflags) {
560b6f47c23SJamie Gritton 		error = EINVAL;
561b6f47c23SJamie Gritton 		vfs_opterror(opts, "no valid operation (create or update)");
562b6f47c23SJamie Gritton 		goto done_errmsg;
563b6f47c23SJamie Gritton 	}
564b6f47c23SJamie Gritton 
565b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
566b38ff370SJamie Gritton 	if (error == ENOENT)
567b38ff370SJamie Gritton 		jid = 0;
568b38ff370SJamie Gritton 	else if (error != 0)
569b38ff370SJamie Gritton 		goto done_free;
570b38ff370SJamie Gritton 
571b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
572b38ff370SJamie Gritton 	if (error == ENOENT)
573b38ff370SJamie Gritton 		gotslevel = 0;
574b38ff370SJamie Gritton 	else if (error != 0)
575b38ff370SJamie Gritton 		goto done_free;
576b38ff370SJamie Gritton 	else
577b38ff370SJamie Gritton 		gotslevel = 1;
578b38ff370SJamie Gritton 
579b97457e2SJamie Gritton 	error =
580b97457e2SJamie Gritton 	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
581b97457e2SJamie Gritton 	if (error == ENOENT)
582b97457e2SJamie Gritton 		gotchildmax = 0;
583b97457e2SJamie Gritton 	else if (error != 0)
584b97457e2SJamie Gritton 		goto done_free;
585b97457e2SJamie Gritton 	else
586b97457e2SJamie Gritton 		gotchildmax = 1;
587b97457e2SJamie Gritton 
5880304c731SJamie Gritton 	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
589f337198dSJamie Gritton 	if (error == ENOENT)
590f337198dSJamie Gritton 		gotenforce = 0;
591f337198dSJamie Gritton 	else if (error != 0)
5920304c731SJamie Gritton 		goto done_free;
593f337198dSJamie Gritton 	else if (enforce < 0 || enforce > 2) {
594f337198dSJamie Gritton 		error = EINVAL;
595f337198dSJamie Gritton 		goto done_free;
596f337198dSJamie Gritton 	} else
597f337198dSJamie Gritton 		gotenforce = 1;
5980304c731SJamie Gritton 
5990cc207a6SMartin Matuska 	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
6000cc207a6SMartin Matuska 	if (error == ENOENT)
6010cc207a6SMartin Matuska 		gotrsnum = 0;
6020cc207a6SMartin Matuska 	else if (error != 0)
6030cc207a6SMartin Matuska 		goto done_free;
6040cc207a6SMartin Matuska 	else
6050cc207a6SMartin Matuska 		gotrsnum = 1;
6060cc207a6SMartin Matuska 
607b38ff370SJamie Gritton 	pr_flags = ch_flags = 0;
60802abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_names); fi++) {
6090304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
6100304c731SJamie Gritton 			continue;
6110304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
6120304c731SJamie Gritton 		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
6130304c731SJamie Gritton 	}
614b38ff370SJamie Gritton 	ch_flags |= pr_flags;
61502abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_jailsys); fi++) {
6167cbf7213SJamie Gritton 		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
6177cbf7213SJamie Gritton 		    sizeof(jsys));
6187cbf7213SJamie Gritton 		if (error == ENOENT)
6197cbf7213SJamie Gritton 			continue;
6207cbf7213SJamie Gritton 		if (error != 0)
6217cbf7213SJamie Gritton 			goto done_free;
6227cbf7213SJamie Gritton 		switch (jsys) {
6237cbf7213SJamie Gritton 		case JAIL_SYS_DISABLE:
6247cbf7213SJamie Gritton 			if (!pr_flag_jailsys[fi].disable) {
6257cbf7213SJamie Gritton 				error = EINVAL;
6267cbf7213SJamie Gritton 				goto done_free;
6277cbf7213SJamie Gritton 			}
6287cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].disable;
6297cbf7213SJamie Gritton 			break;
6307cbf7213SJamie Gritton 		case JAIL_SYS_NEW:
6317cbf7213SJamie Gritton 			pr_flags |= pr_flag_jailsys[fi].new;
6327cbf7213SJamie Gritton 			break;
6337cbf7213SJamie Gritton 		case JAIL_SYS_INHERIT:
6347cbf7213SJamie Gritton 			break;
6357cbf7213SJamie Gritton 		default:
6367cbf7213SJamie Gritton 			error = EINVAL;
6377cbf7213SJamie Gritton 			goto done_free;
6387cbf7213SJamie Gritton 		}
6397cbf7213SJamie Gritton 		ch_flags |=
6407cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
6417cbf7213SJamie Gritton 	}
6424affa14cSJamie Gritton 	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
6434affa14cSJamie Gritton 	    && !(pr_flags & PR_PERSIST)) {
6444affa14cSJamie Gritton 		error = EINVAL;
6454affa14cSJamie Gritton 		vfs_opterror(opts, "new jail must persist or attach");
6464affa14cSJamie Gritton 		goto done_errmsg;
6474affa14cSJamie Gritton 	}
648679e1390SJamie Gritton #ifdef VIMAGE
649679e1390SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
650679e1390SJamie Gritton 		error = EINVAL;
651679e1390SJamie Gritton 		vfs_opterror(opts, "vnet cannot be changed after creation");
652679e1390SJamie Gritton 		goto done_errmsg;
653679e1390SJamie Gritton 	}
654679e1390SJamie Gritton #endif
6552b0d6f81SJamie Gritton #ifdef INET
6562b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
6572b0d6f81SJamie Gritton 		error = EINVAL;
6582b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip4 cannot be changed after creation");
6592b0d6f81SJamie Gritton 		goto done_errmsg;
6602b0d6f81SJamie Gritton 	}
6612b0d6f81SJamie Gritton #endif
6622b0d6f81SJamie Gritton #ifdef INET6
6632b0d6f81SJamie Gritton 	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
6642b0d6f81SJamie Gritton 		error = EINVAL;
6652b0d6f81SJamie Gritton 		vfs_opterror(opts, "ip6 cannot be changed after creation");
6662b0d6f81SJamie Gritton 		goto done_errmsg;
6672b0d6f81SJamie Gritton 	}
6682b0d6f81SJamie Gritton #endif
669b38ff370SJamie Gritton 
6700304c731SJamie Gritton 	pr_allow = ch_allow = 0;
67102abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_allow_names); fi++) {
6720304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
6730304c731SJamie Gritton 		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
6740304c731SJamie Gritton 	}
6750304c731SJamie Gritton 	ch_allow |= pr_allow;
6760304c731SJamie Gritton 
677b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
678b38ff370SJamie Gritton 	if (error == ENOENT)
679b38ff370SJamie Gritton 		name = NULL;
680b38ff370SJamie Gritton 	else if (error != 0)
681b38ff370SJamie Gritton 		goto done_free;
682b38ff370SJamie Gritton 	else {
683b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
684b38ff370SJamie Gritton 			error = EINVAL;
685b38ff370SJamie Gritton 			goto done_free;
686b38ff370SJamie Gritton 		}
687b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
688b38ff370SJamie Gritton 			error = ENAMETOOLONG;
689b38ff370SJamie Gritton 			goto done_free;
690b38ff370SJamie Gritton 		}
691b38ff370SJamie Gritton 	}
692b38ff370SJamie Gritton 
693b38ff370SJamie Gritton 	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
694b38ff370SJamie Gritton 	if (error == ENOENT)
695b38ff370SJamie Gritton 		host = NULL;
696b38ff370SJamie Gritton 	else if (error != 0)
697b38ff370SJamie Gritton 		goto done_free;
698b38ff370SJamie Gritton 	else {
69976ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
70076ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
701b38ff370SJamie Gritton 		if (len == 0 || host[len - 1] != '\0') {
702b38ff370SJamie Gritton 			error = EINVAL;
703b38ff370SJamie Gritton 			goto done_free;
704b38ff370SJamie Gritton 		}
705b38ff370SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
706b38ff370SJamie Gritton 			error = ENAMETOOLONG;
707b38ff370SJamie Gritton 			goto done_free;
708b38ff370SJamie Gritton 		}
709b38ff370SJamie Gritton 	}
710b38ff370SJamie Gritton 
71176ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
71276ca6f88SJamie Gritton 	if (error == ENOENT)
71376ca6f88SJamie Gritton 		domain = NULL;
71476ca6f88SJamie Gritton 	else if (error != 0)
71576ca6f88SJamie Gritton 		goto done_free;
71676ca6f88SJamie Gritton 	else {
71776ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
71876ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
71976ca6f88SJamie Gritton 		if (len == 0 || domain[len - 1] != '\0') {
72076ca6f88SJamie Gritton 			error = EINVAL;
72176ca6f88SJamie Gritton 			goto done_free;
72276ca6f88SJamie Gritton 		}
72376ca6f88SJamie Gritton 		if (len > MAXHOSTNAMELEN) {
72476ca6f88SJamie Gritton 			error = ENAMETOOLONG;
72576ca6f88SJamie Gritton 			goto done_free;
72676ca6f88SJamie Gritton 		}
72776ca6f88SJamie Gritton 	}
72876ca6f88SJamie Gritton 
72976ca6f88SJamie Gritton 	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
73076ca6f88SJamie Gritton 	if (error == ENOENT)
73176ca6f88SJamie Gritton 		uuid = NULL;
73276ca6f88SJamie Gritton 	else if (error != 0)
73376ca6f88SJamie Gritton 		goto done_free;
73476ca6f88SJamie Gritton 	else {
73576ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
73676ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
73776ca6f88SJamie Gritton 		if (len == 0 || uuid[len - 1] != '\0') {
73876ca6f88SJamie Gritton 			error = EINVAL;
73976ca6f88SJamie Gritton 			goto done_free;
74076ca6f88SJamie Gritton 		}
74176ca6f88SJamie Gritton 		if (len > HOSTUUIDLEN) {
74276ca6f88SJamie Gritton 			error = ENAMETOOLONG;
74376ca6f88SJamie Gritton 			goto done_free;
74476ca6f88SJamie Gritton 		}
74576ca6f88SJamie Gritton 	}
74676ca6f88SJamie Gritton 
747841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
748a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
74976ca6f88SJamie Gritton 		uint32_t hid32;
75076ca6f88SJamie Gritton 
75176ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
75276ca6f88SJamie Gritton 		hid = hid32;
75376ca6f88SJamie Gritton 	} else
75476ca6f88SJamie Gritton #endif
75576ca6f88SJamie Gritton 		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
75676ca6f88SJamie Gritton 	if (error == ENOENT)
75776ca6f88SJamie Gritton 		gothid = 0;
75876ca6f88SJamie Gritton 	else if (error != 0)
75976ca6f88SJamie Gritton 		goto done_free;
76076ca6f88SJamie Gritton 	else {
76176ca6f88SJamie Gritton 		gothid = 1;
76276ca6f88SJamie Gritton 		ch_flags |= PR_HOST;
76376ca6f88SJamie Gritton 		pr_flags |= PR_HOST;
76476ca6f88SJamie Gritton 	}
76576ca6f88SJamie Gritton 
766b38ff370SJamie Gritton #ifdef INET
767b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
768b38ff370SJamie Gritton 	if (error == ENOENT)
7690e5e396eSJamie Gritton 		ip4s = 0;
770b38ff370SJamie Gritton 	else if (error != 0)
771b38ff370SJamie Gritton 		goto done_free;
772b38ff370SJamie Gritton 	else if (ip4s & (sizeof(*ip4) - 1)) {
773b38ff370SJamie Gritton 		error = EINVAL;
774b38ff370SJamie Gritton 		goto done_free;
7750304c731SJamie Gritton 	} else {
7766a3f2779SJamie Gritton 		ch_flags |= PR_IP4_USER;
7776a3f2779SJamie Gritton 		pr_flags |= PR_IP4_USER;
7786a3f2779SJamie Gritton 		if (ip4s > 0) {
779b38ff370SJamie Gritton 			ip4s /= sizeof(*ip4);
780b38ff370SJamie Gritton 			if (ip4s > jail_max_af_ips) {
781b38ff370SJamie Gritton 				error = EINVAL;
782b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv4 addresses");
783b38ff370SJamie Gritton 				goto done_errmsg;
784b38ff370SJamie Gritton 			}
7852b0d6f81SJamie Gritton 			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
786b38ff370SJamie Gritton 			bcopy(op, ip4, ip4s * sizeof(*ip4));
787b38ff370SJamie Gritton 			/*
7880304c731SJamie Gritton 			 * IP addresses are all sorted but ip[0] to preserve
7890304c731SJamie Gritton 			 * the primary IP address as given from userland.
7900304c731SJamie Gritton 			 * This special IP is used for unbound outgoing
791bef916f9SBjoern A. Zeeb 			 * connections as well for "loopback" traffic in case
792bef916f9SBjoern A. Zeeb 			 * source address selection cannot find any more fitting
793bef916f9SBjoern A. Zeeb 			 * address to connect from.
794b38ff370SJamie Gritton 			 */
795b38ff370SJamie Gritton 			if (ip4s > 1)
7960ce1624dSStephen J. Kiernan 				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
7970ce1624dSStephen J. Kiernan 				    prison_qcmp_v4);
798b38ff370SJamie Gritton 			/*
7990304c731SJamie Gritton 			 * Check for duplicate addresses and do some simple
8000304c731SJamie Gritton 			 * zero and broadcast checks. If users give other bogus
8010304c731SJamie Gritton 			 * addresses it is their problem.
802b38ff370SJamie Gritton 			 *
8030304c731SJamie Gritton 			 * We do not have to care about byte order for these
8040304c731SJamie Gritton 			 * checks so we will do them in NBO.
805b38ff370SJamie Gritton 			 */
806b38ff370SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
807b38ff370SJamie Gritton 				if (ip4[ii].s_addr == INADDR_ANY ||
808b38ff370SJamie Gritton 				    ip4[ii].s_addr == INADDR_BROADCAST) {
809b38ff370SJamie Gritton 					error = EINVAL;
810b38ff370SJamie Gritton 					goto done_free;
811b38ff370SJamie Gritton 				}
812b38ff370SJamie Gritton 				if ((ii+1) < ip4s &&
813b38ff370SJamie Gritton 				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
814b38ff370SJamie Gritton 				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
815b38ff370SJamie Gritton 					error = EINVAL;
816b38ff370SJamie Gritton 					goto done_free;
817b38ff370SJamie Gritton 				}
818b38ff370SJamie Gritton 			}
819b38ff370SJamie Gritton 		}
8200304c731SJamie Gritton 	}
821b38ff370SJamie Gritton #endif
822b38ff370SJamie Gritton 
823b38ff370SJamie Gritton #ifdef INET6
824b38ff370SJamie Gritton 	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
825b38ff370SJamie Gritton 	if (error == ENOENT)
8260e5e396eSJamie Gritton 		ip6s = 0;
827b38ff370SJamie Gritton 	else if (error != 0)
828b38ff370SJamie Gritton 		goto done_free;
829b38ff370SJamie Gritton 	else if (ip6s & (sizeof(*ip6) - 1)) {
830b38ff370SJamie Gritton 		error = EINVAL;
831b38ff370SJamie Gritton 		goto done_free;
8320304c731SJamie Gritton 	} else {
8336a3f2779SJamie Gritton 		ch_flags |= PR_IP6_USER;
8346a3f2779SJamie Gritton 		pr_flags |= PR_IP6_USER;
8356a3f2779SJamie Gritton 		if (ip6s > 0) {
836b38ff370SJamie Gritton 			ip6s /= sizeof(*ip6);
837b38ff370SJamie Gritton 			if (ip6s > jail_max_af_ips) {
838b38ff370SJamie Gritton 				error = EINVAL;
839b38ff370SJamie Gritton 				vfs_opterror(opts, "too many IPv6 addresses");
840b38ff370SJamie Gritton 				goto done_errmsg;
841b38ff370SJamie Gritton 			}
8422b0d6f81SJamie Gritton 			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
843b38ff370SJamie Gritton 			bcopy(op, ip6, ip6s * sizeof(*ip6));
844b38ff370SJamie Gritton 			if (ip6s > 1)
8450ce1624dSStephen J. Kiernan 				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
8460ce1624dSStephen J. Kiernan 				    prison_qcmp_v6);
847b38ff370SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
8480304c731SJamie Gritton 				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
849b38ff370SJamie Gritton 					error = EINVAL;
850b38ff370SJamie Gritton 					goto done_free;
851b38ff370SJamie Gritton 				}
852b38ff370SJamie Gritton 				if ((ii+1) < ip6s &&
853b38ff370SJamie Gritton 				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
854b38ff370SJamie Gritton 				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
855b38ff370SJamie Gritton 				{
856b38ff370SJamie Gritton 					error = EINVAL;
857b38ff370SJamie Gritton 					goto done_free;
858b38ff370SJamie Gritton 				}
859b38ff370SJamie Gritton 			}
860b38ff370SJamie Gritton 		}
8610304c731SJamie Gritton 	}
862b38ff370SJamie Gritton #endif
863b38ff370SJamie Gritton 
864bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
865bdfc8cc4SJamie Gritton 	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
866bdfc8cc4SJamie Gritton 		error = EINVAL;
867bdfc8cc4SJamie Gritton 		vfs_opterror(opts,
868bdfc8cc4SJamie Gritton 		    "vnet jails cannot have IP address restrictions");
869bdfc8cc4SJamie Gritton 		goto done_errmsg;
870bdfc8cc4SJamie Gritton 	}
871bdfc8cc4SJamie Gritton #endif
872bdfc8cc4SJamie Gritton 
873cf0313c6SJamie Gritton 	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
874cf0313c6SJamie Gritton 	if (error == ENOENT)
875cf0313c6SJamie Gritton 		osrelstr = NULL;
876cf0313c6SJamie Gritton 	else if (error != 0)
877cf0313c6SJamie Gritton 		goto done_free;
878cf0313c6SJamie Gritton 	else {
879cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
880cf0313c6SJamie Gritton 			error = EINVAL;
881cf0313c6SJamie Gritton 			vfs_opterror(opts,
882cf0313c6SJamie Gritton 			    "osrelease cannot be changed after creation");
883cf0313c6SJamie Gritton 			goto done_errmsg;
884cf0313c6SJamie Gritton 		}
885cf0313c6SJamie Gritton 		if (len == 0 || len >= OSRELEASELEN) {
886cf0313c6SJamie Gritton 			error = EINVAL;
887cf0313c6SJamie Gritton 			vfs_opterror(opts,
888cf0313c6SJamie Gritton 			    "osrelease string must be 1-%d bytes long",
889cf0313c6SJamie Gritton 			    OSRELEASELEN - 1);
890cf0313c6SJamie Gritton 			goto done_errmsg;
891cf0313c6SJamie Gritton 		}
892cf0313c6SJamie Gritton 	}
893cf0313c6SJamie Gritton 
894cf0313c6SJamie Gritton 	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
895cf0313c6SJamie Gritton 	if (error == ENOENT)
896cf0313c6SJamie Gritton 		osreldt = 0;
897cf0313c6SJamie Gritton 	else if (error != 0)
898cf0313c6SJamie Gritton 		goto done_free;
899cf0313c6SJamie Gritton 	else {
900cf0313c6SJamie Gritton 		if (flags & JAIL_UPDATE) {
901cf0313c6SJamie Gritton 			error = EINVAL;
902cf0313c6SJamie Gritton 			vfs_opterror(opts,
903cf0313c6SJamie Gritton 			    "osreldate cannot be changed after creation");
904cf0313c6SJamie Gritton 			goto done_errmsg;
905cf0313c6SJamie Gritton 		}
906cf0313c6SJamie Gritton 		if (osreldt == 0) {
907cf0313c6SJamie Gritton 			error = EINVAL;
908cf0313c6SJamie Gritton 			vfs_opterror(opts, "osreldate cannot be 0");
909cf0313c6SJamie Gritton 			goto done_errmsg;
910cf0313c6SJamie Gritton 		}
911cf0313c6SJamie Gritton 	}
912cf0313c6SJamie Gritton 
9139cbe30e1SMartin Matuska 	fullpath_disabled = 0;
914b38ff370SJamie Gritton 	root = NULL;
915b38ff370SJamie Gritton 	error = vfs_getopt(opts, "path", (void **)&path, &len);
916b38ff370SJamie Gritton 	if (error == ENOENT)
917b38ff370SJamie Gritton 		path = NULL;
918b38ff370SJamie Gritton 	else if (error != 0)
919b38ff370SJamie Gritton 		goto done_free;
920b38ff370SJamie Gritton 	else {
921b38ff370SJamie Gritton 		if (flags & JAIL_UPDATE) {
922b38ff370SJamie Gritton 			error = EINVAL;
923b38ff370SJamie Gritton 			vfs_opterror(opts,
924b38ff370SJamie Gritton 			    "path cannot be changed after creation");
925b38ff370SJamie Gritton 			goto done_errmsg;
926b38ff370SJamie Gritton 		}
927b38ff370SJamie Gritton 		if (len == 0 || path[len - 1] != '\0') {
928b38ff370SJamie Gritton 			error = EINVAL;
929b38ff370SJamie Gritton 			goto done_free;
930b38ff370SJamie Gritton 		}
9315050aa86SKonstantin Belousov 		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
932b38ff370SJamie Gritton 		    path, td);
933b38ff370SJamie Gritton 		error = namei(&nd);
934b38ff370SJamie Gritton 		if (error)
935b38ff370SJamie Gritton 			goto done_free;
936b38ff370SJamie Gritton 		root = nd.ni_vp;
937b38ff370SJamie Gritton 		NDFREE(&nd, NDF_ONLY_PNBUF);
9386dfe0a3dSMartin Matuska 		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
9396dfe0a3dSMartin Matuska 		strlcpy(g_path, path, MAXPATHLEN);
9406dfe0a3dSMartin Matuska 		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
9416dfe0a3dSMartin Matuska 		if (error == 0)
9426dfe0a3dSMartin Matuska 			path = g_path;
9436dfe0a3dSMartin Matuska 		else if (error == ENODEV) {
944f6e633a9SMartin Matuska 			/* proceed if sysctl debug.disablefullpath == 1 */
945f6e633a9SMartin Matuska 			fullpath_disabled = 1;
946f6e633a9SMartin Matuska 			if (len < 2 || (len == 2 && path[0] == '/'))
947f6e633a9SMartin Matuska 				path = NULL;
9486dfe0a3dSMartin Matuska 		} else {
949f6e633a9SMartin Matuska 			/* exit on other errors */
950b38ff370SJamie Gritton 			goto done_free;
951b38ff370SJamie Gritton 		}
952f6e633a9SMartin Matuska 		if (root->v_type != VDIR) {
953f6e633a9SMartin Matuska 			error = ENOTDIR;
954f6e633a9SMartin Matuska 			vput(root);
955f6e633a9SMartin Matuska 			goto done_free;
956f6e633a9SMartin Matuska 		}
957f6e633a9SMartin Matuska 		VOP_UNLOCK(root, 0);
958f6e633a9SMartin Matuska 		if (fullpath_disabled) {
959f6e633a9SMartin Matuska 			/* Leave room for a real-root full pathname. */
960f6e633a9SMartin Matuska 			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
961f6e633a9SMartin Matuska 			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
962f6e633a9SMartin Matuska 				error = ENAMETOOLONG;
963932a6e43SJamie Gritton 				vrele(root);
964f6e633a9SMartin Matuska 				goto done_free;
965f6e633a9SMartin Matuska 			}
966b38ff370SJamie Gritton 		}
967b38ff370SJamie Gritton 	}
968b38ff370SJamie Gritton 
969b38ff370SJamie Gritton 	/*
970b6f47c23SJamie Gritton 	 * Find the specified jail, or at least its parent.
971b38ff370SJamie Gritton 	 * This abuses the file error codes ENOENT and EEXIST.
972b38ff370SJamie Gritton 	 */
973b38ff370SJamie Gritton 	pr = NULL;
974b6f47c23SJamie Gritton 	ppr = mypr;
975babbbb9cSJamie Gritton 	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
976babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
977babbbb9cSJamie Gritton 		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
978babbbb9cSJamie Gritton 		if (*p != '\0')
979babbbb9cSJamie Gritton 			jid = 0;
980babbbb9cSJamie Gritton 	}
981b6f47c23SJamie Gritton 	sx_xlock(&allprison_lock);
982b38ff370SJamie Gritton 	if (jid != 0) {
9830304c731SJamie Gritton 		/*
9840304c731SJamie Gritton 		 * See if a requested jid already exists.  There is an
9850304c731SJamie Gritton 		 * information leak here if the jid exists but is not within
9860304c731SJamie Gritton 		 * the caller's jail hierarchy.  Jail creators will get EEXIST
9870304c731SJamie Gritton 		 * even though they cannot see the jail, and CREATE | UPDATE
9880304c731SJamie Gritton 		 * will return ENOENT which is not normally a valid error.
9890304c731SJamie Gritton 		 */
990b38ff370SJamie Gritton 		if (jid < 0) {
991b38ff370SJamie Gritton 			error = EINVAL;
992b38ff370SJamie Gritton 			vfs_opterror(opts, "negative jid");
993b38ff370SJamie Gritton 			goto done_unlock_list;
994b38ff370SJamie Gritton 		}
995b38ff370SJamie Gritton 		pr = prison_find(jid);
996b38ff370SJamie Gritton 		if (pr != NULL) {
9970304c731SJamie Gritton 			ppr = pr->pr_parent;
998b38ff370SJamie Gritton 			/* Create: jid must not exist. */
999b38ff370SJamie Gritton 			if (cuflags == JAIL_CREATE) {
1000b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1001b38ff370SJamie Gritton 				error = EEXIST;
1002b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d already exists",
1003b38ff370SJamie Gritton 				    jid);
1004b38ff370SJamie Gritton 				goto done_unlock_list;
1005b38ff370SJamie Gritton 			}
10060304c731SJamie Gritton 			if (!prison_ischild(mypr, pr)) {
10070304c731SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
10080304c731SJamie Gritton 				pr = NULL;
10090304c731SJamie Gritton 			} else if (pr->pr_uref == 0) {
1010b38ff370SJamie Gritton 				if (!(flags & JAIL_DYING)) {
1011b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1012b38ff370SJamie Gritton 					error = ENOENT;
1013b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1014b38ff370SJamie Gritton 					    jid);
1015b38ff370SJamie Gritton 					goto done_unlock_list;
1016b38ff370SJamie Gritton 				} else if ((flags & JAIL_ATTACH) ||
1017b38ff370SJamie Gritton 				    (pr_flags & PR_PERSIST)) {
1018b38ff370SJamie Gritton 					/*
1019b38ff370SJamie Gritton 					 * A dying jail might be resurrected
1020b38ff370SJamie Gritton 					 * (via attach or persist), but first
1021b38ff370SJamie Gritton 					 * it must determine if another jail
1022b38ff370SJamie Gritton 					 * has claimed its name.  Accomplish
1023b38ff370SJamie Gritton 					 * this by implicitly re-setting the
1024b38ff370SJamie Gritton 					 * name.
1025b38ff370SJamie Gritton 					 */
1026b38ff370SJamie Gritton 					if (name == NULL)
10270304c731SJamie Gritton 						name = prison_name(mypr, pr);
1028b38ff370SJamie Gritton 				}
1029b38ff370SJamie Gritton 			}
1030b38ff370SJamie Gritton 		}
1031b38ff370SJamie Gritton 		if (pr == NULL) {
1032b38ff370SJamie Gritton 			/* Update: jid must exist. */
1033b38ff370SJamie Gritton 			if (cuflags == JAIL_UPDATE) {
1034b38ff370SJamie Gritton 				error = ENOENT;
1035b38ff370SJamie Gritton 				vfs_opterror(opts, "jail %d not found", jid);
1036b38ff370SJamie Gritton 				goto done_unlock_list;
1037b38ff370SJamie Gritton 			}
1038b38ff370SJamie Gritton 		}
1039b38ff370SJamie Gritton 	}
1040b38ff370SJamie Gritton 	/*
1041b38ff370SJamie Gritton 	 * If the caller provided a name, look for a jail by that name.
1042b38ff370SJamie Gritton 	 * This has different semantics for creates and updates keyed by jid
1043b38ff370SJamie Gritton 	 * (where the name must not already exist in a different jail),
1044b38ff370SJamie Gritton 	 * and updates keyed by the name itself (where the name must exist
1045b38ff370SJamie Gritton 	 * because that is the jail being updated).
1046b38ff370SJamie Gritton 	 */
1047b6f47c23SJamie Gritton 	namelc = NULL;
1048b38ff370SJamie Gritton 	if (name != NULL) {
1049babbbb9cSJamie Gritton 		namelc = strrchr(name, '.');
1050babbbb9cSJamie Gritton 		if (namelc == NULL)
1051babbbb9cSJamie Gritton 			namelc = name;
1052babbbb9cSJamie Gritton 		else {
10530304c731SJamie Gritton 			/*
10540304c731SJamie Gritton 			 * This is a hierarchical name.  Split it into the
10550304c731SJamie Gritton 			 * parent and child names, and make sure the parent
10560304c731SJamie Gritton 			 * exists or matches an already found jail.
10570304c731SJamie Gritton 			 */
10580304c731SJamie Gritton 			if (pr != NULL) {
1059babbbb9cSJamie Gritton 				if (strncmp(name, ppr->pr_name, namelc - name)
1060babbbb9cSJamie Gritton 				    || ppr->pr_name[namelc - name] != '\0') {
10610304c731SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
10620304c731SJamie Gritton 					error = EINVAL;
10630304c731SJamie Gritton 					vfs_opterror(opts,
10640304c731SJamie Gritton 					    "cannot change jail's parent");
10650304c731SJamie Gritton 					goto done_unlock_list;
10660304c731SJamie Gritton 				}
10670304c731SJamie Gritton 			} else {
1068b6f47c23SJamie Gritton 				*namelc = '\0';
10690304c731SJamie Gritton 				ppr = prison_find_name(mypr, name);
10700304c731SJamie Gritton 				if (ppr == NULL) {
10710304c731SJamie Gritton 					error = ENOENT;
10720304c731SJamie Gritton 					vfs_opterror(opts,
10730304c731SJamie Gritton 					    "jail \"%s\" not found", name);
10740304c731SJamie Gritton 					goto done_unlock_list;
10750304c731SJamie Gritton 				}
10760304c731SJamie Gritton 				mtx_unlock(&ppr->pr_mtx);
1077b6f47c23SJamie Gritton 				*namelc = '.';
10780304c731SJamie Gritton 			}
1079b6f47c23SJamie Gritton 			namelc++;
10800304c731SJamie Gritton 		}
1081b6f47c23SJamie Gritton 		if (namelc[0] != '\0') {
1082b6f47c23SJamie Gritton 			pnamelen =
10830304c731SJamie Gritton 			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1084b38ff370SJamie Gritton  name_again:
10850304c731SJamie Gritton 			deadpr = NULL;
10860304c731SJamie Gritton 			FOREACH_PRISON_CHILD(ppr, tpr) {
1087b38ff370SJamie Gritton 				if (tpr != pr && tpr->pr_ref > 0 &&
1088b6f47c23SJamie Gritton 				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
1089b38ff370SJamie Gritton 					if (pr == NULL &&
1090b38ff370SJamie Gritton 					    cuflags != JAIL_CREATE) {
1091b38ff370SJamie Gritton 						mtx_lock(&tpr->pr_mtx);
1092b38ff370SJamie Gritton 						if (tpr->pr_ref > 0) {
1093b38ff370SJamie Gritton 							/*
1094b38ff370SJamie Gritton 							 * Use this jail
1095b38ff370SJamie Gritton 							 * for updates.
1096b38ff370SJamie Gritton 							 */
1097b38ff370SJamie Gritton 							if (tpr->pr_uref > 0) {
1098b38ff370SJamie Gritton 								pr = tpr;
1099b38ff370SJamie Gritton 								break;
1100b38ff370SJamie Gritton 							}
1101b38ff370SJamie Gritton 							deadpr = tpr;
1102b38ff370SJamie Gritton 						}
1103b38ff370SJamie Gritton 						mtx_unlock(&tpr->pr_mtx);
1104b38ff370SJamie Gritton 					} else if (tpr->pr_uref > 0) {
1105b38ff370SJamie Gritton 						/*
1106b38ff370SJamie Gritton 						 * Create, or update(jid):
1107b38ff370SJamie Gritton 						 * name must not exist in an
11080304c731SJamie Gritton 						 * active sibling jail.
1109b38ff370SJamie Gritton 						 */
1110b38ff370SJamie Gritton 						error = EEXIST;
1111b38ff370SJamie Gritton 						if (pr != NULL)
1112b38ff370SJamie Gritton 							mtx_unlock(&pr->pr_mtx);
1113b38ff370SJamie Gritton 						vfs_opterror(opts,
1114b38ff370SJamie Gritton 						   "jail \"%s\" already exists",
1115b38ff370SJamie Gritton 						   name);
1116b38ff370SJamie Gritton 						goto done_unlock_list;
1117b38ff370SJamie Gritton 					}
1118b38ff370SJamie Gritton 				}
1119b38ff370SJamie Gritton 			}
1120b38ff370SJamie Gritton 			/* If no active jail is found, use a dying one. */
1121b38ff370SJamie Gritton 			if (deadpr != NULL && pr == NULL) {
1122b38ff370SJamie Gritton 				if (flags & JAIL_DYING) {
1123b38ff370SJamie Gritton 					mtx_lock(&deadpr->pr_mtx);
1124b38ff370SJamie Gritton 					if (deadpr->pr_ref == 0) {
1125b38ff370SJamie Gritton 						mtx_unlock(&deadpr->pr_mtx);
1126b38ff370SJamie Gritton 						goto name_again;
1127b38ff370SJamie Gritton 					}
1128b38ff370SJamie Gritton 					pr = deadpr;
1129b38ff370SJamie Gritton 				} else if (cuflags == JAIL_UPDATE) {
1130b38ff370SJamie Gritton 					error = ENOENT;
1131b38ff370SJamie Gritton 					vfs_opterror(opts,
1132b38ff370SJamie Gritton 					    "jail \"%s\" is dying", name);
1133b38ff370SJamie Gritton 					goto done_unlock_list;
1134b38ff370SJamie Gritton 				}
1135b38ff370SJamie Gritton 			}
1136b38ff370SJamie Gritton 			/* Update: name must exist if no jid. */
1137b38ff370SJamie Gritton 			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1138b38ff370SJamie Gritton 				error = ENOENT;
1139b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" not found",
1140b38ff370SJamie Gritton 				    name);
1141b38ff370SJamie Gritton 				goto done_unlock_list;
1142b38ff370SJamie Gritton 			}
1143b38ff370SJamie Gritton 		}
1144b38ff370SJamie Gritton 	}
1145b38ff370SJamie Gritton 	/* Update: must provide a jid or name. */
1146b38ff370SJamie Gritton 	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1147b38ff370SJamie Gritton 		error = ENOENT;
1148b38ff370SJamie Gritton 		vfs_opterror(opts, "update specified no jail");
1149b38ff370SJamie Gritton 		goto done_unlock_list;
1150b38ff370SJamie Gritton 	}
1151b38ff370SJamie Gritton 
1152b38ff370SJamie Gritton 	/* If there's no prison to update, create a new one and link it in. */
1153b38ff370SJamie Gritton 	if (pr == NULL) {
1154b97457e2SJamie Gritton 		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1155b97457e2SJamie Gritton 			if (tpr->pr_childcount >= tpr->pr_childmax) {
1156b97457e2SJamie Gritton 				error = EPERM;
1157b97457e2SJamie Gritton 				vfs_opterror(opts, "prison limit exceeded");
1158b97457e2SJamie Gritton 				goto done_unlock_list;
1159b97457e2SJamie Gritton 			}
1160b38ff370SJamie Gritton 		created = 1;
11610304c731SJamie Gritton 		mtx_lock(&ppr->pr_mtx);
11622a549507SJamie Gritton 		if (ppr->pr_ref == 0) {
11630304c731SJamie Gritton 			mtx_unlock(&ppr->pr_mtx);
11640304c731SJamie Gritton 			error = ENOENT;
1165b6f47c23SJamie Gritton 			vfs_opterror(opts, "jail \"%s\" not found",
1166b6f47c23SJamie Gritton 			    prison_name(mypr, ppr));
11670304c731SJamie Gritton 			goto done_unlock_list;
11680304c731SJamie Gritton 		}
11690304c731SJamie Gritton 		ppr->pr_ref++;
11700304c731SJamie Gritton 		ppr->pr_uref++;
11710304c731SJamie Gritton 		mtx_unlock(&ppr->pr_mtx);
1172b38ff370SJamie Gritton 		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1173b38ff370SJamie Gritton 		if (jid == 0) {
1174b38ff370SJamie Gritton 			/* Find the next free jid. */
1175b38ff370SJamie Gritton 			jid = lastprid + 1;
1176b38ff370SJamie Gritton  findnext:
1177b38ff370SJamie Gritton 			if (jid == JAIL_MAX)
1178b38ff370SJamie Gritton 				jid = 1;
1179b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1180b38ff370SJamie Gritton 				if (tpr->pr_id < jid)
1181b38ff370SJamie Gritton 					continue;
1182b38ff370SJamie Gritton 				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1183b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1184b38ff370SJamie Gritton 					break;
1185b38ff370SJamie Gritton 				}
1186b38ff370SJamie Gritton 				if (jid == lastprid) {
1187b38ff370SJamie Gritton 					error = EAGAIN;
1188b38ff370SJamie Gritton 					vfs_opterror(opts,
1189b38ff370SJamie Gritton 					    "no available jail IDs");
1190b38ff370SJamie Gritton 					free(pr, M_PRISON);
11910304c731SJamie Gritton 					prison_deref(ppr, PD_DEREF |
11920304c731SJamie Gritton 					    PD_DEUREF | PD_LIST_XLOCKED);
11930304c731SJamie Gritton 					goto done_releroot;
1194b38ff370SJamie Gritton 				}
1195b38ff370SJamie Gritton 				jid++;
1196b38ff370SJamie Gritton 				goto findnext;
1197b38ff370SJamie Gritton 			}
1198b38ff370SJamie Gritton 			lastprid = jid;
1199b38ff370SJamie Gritton 		} else {
1200b38ff370SJamie Gritton 			/*
1201b38ff370SJamie Gritton 			 * The jail already has a jid (that did not yet exist),
1202b38ff370SJamie Gritton 			 * so just find where to insert it.
1203b38ff370SJamie Gritton 			 */
1204b38ff370SJamie Gritton 			TAILQ_FOREACH(tpr, &allprison, pr_list)
1205b38ff370SJamie Gritton 				if (tpr->pr_id >= jid) {
1206b38ff370SJamie Gritton 					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1207b38ff370SJamie Gritton 					break;
1208b38ff370SJamie Gritton 				}
1209b38ff370SJamie Gritton 		}
1210b38ff370SJamie Gritton 		if (tpr == NULL)
1211b38ff370SJamie Gritton 			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
12120304c731SJamie Gritton 		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
12130304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1214b97457e2SJamie Gritton 			tpr->pr_childcount++;
1215b38ff370SJamie Gritton 
12160304c731SJamie Gritton 		pr->pr_parent = ppr;
1217b38ff370SJamie Gritton 		pr->pr_id = jid;
12180304c731SJamie Gritton 
12190304c731SJamie Gritton 		/* Set some default values, and inherit some from the parent. */
1220b6f47c23SJamie Gritton 		if (namelc == NULL)
1221b6f47c23SJamie Gritton 			namelc = "";
1222b38ff370SJamie Gritton 		if (path == NULL) {
1223b38ff370SJamie Gritton 			path = "/";
12240304c731SJamie Gritton 			root = mypr->pr_root;
1225b38ff370SJamie Gritton 			vref(root);
1226b38ff370SJamie Gritton 		}
12278986e3a0SJamie Gritton 		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
12288986e3a0SJamie Gritton 		pr->pr_flags |= PR_HOST;
1229bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6)
1230bdfc8cc4SJamie Gritton #ifdef VIMAGE
1231bdfc8cc4SJamie Gritton 		if (!(pr_flags & PR_VNET))
1232bdfc8cc4SJamie Gritton #endif
1233bdfc8cc4SJamie Gritton 		{
12340304c731SJamie Gritton #ifdef INET
12352b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP4_USER))
12366a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP4 | PR_IP4_USER;
12372b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP4_USER)) {
12382b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP4;
12392b0d6f81SJamie Gritton 				if (ppr->pr_ip4 != NULL) {
12402b0d6f81SJamie Gritton 					pr->pr_ip4s = ppr->pr_ip4s;
12412b0d6f81SJamie Gritton 					pr->pr_ip4 = malloc(pr->pr_ip4s *
12422b0d6f81SJamie Gritton 					    sizeof(struct in_addr), M_PRISON,
12432b0d6f81SJamie Gritton 					    M_WAITOK);
12442b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip4, pr->pr_ip4,
12452b0d6f81SJamie Gritton 					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
12462b0d6f81SJamie Gritton 				}
12472b0d6f81SJamie Gritton 			}
12480304c731SJamie Gritton #endif
12490304c731SJamie Gritton #ifdef INET6
12502b0d6f81SJamie Gritton 			if (!(ch_flags & PR_IP6_USER))
12516a3f2779SJamie Gritton 				pr->pr_flags |= PR_IP6 | PR_IP6_USER;
12522b0d6f81SJamie Gritton 			else if (!(pr_flags & PR_IP6_USER)) {
12532b0d6f81SJamie Gritton 				pr->pr_flags |= ppr->pr_flags & PR_IP6;
12542b0d6f81SJamie Gritton 				if (ppr->pr_ip6 != NULL) {
12552b0d6f81SJamie Gritton 					pr->pr_ip6s = ppr->pr_ip6s;
12562b0d6f81SJamie Gritton 					pr->pr_ip6 = malloc(pr->pr_ip6s *
12572b0d6f81SJamie Gritton 					    sizeof(struct in6_addr), M_PRISON,
12582b0d6f81SJamie Gritton 					    M_WAITOK);
12592b0d6f81SJamie Gritton 					bcopy(ppr->pr_ip6, pr->pr_ip6,
12602b0d6f81SJamie Gritton 					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
12612b0d6f81SJamie Gritton 				}
12622b0d6f81SJamie Gritton 			}
12630304c731SJamie Gritton #endif
1264bdfc8cc4SJamie Gritton 		}
1265bdfc8cc4SJamie Gritton #endif
1266592bcae8SBjoern A. Zeeb 		/* Source address selection is always on by default. */
1267592bcae8SBjoern A. Zeeb 		pr->pr_flags |= _PR_IP_SADDRSEL;
1268592bcae8SBjoern A. Zeeb 
12690304c731SJamie Gritton 		pr->pr_securelevel = ppr->pr_securelevel;
12700304c731SJamie Gritton 		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1271af10bf05SBjoern A. Zeeb 		pr->pr_enforce_statfs = jail_default_enforce_statfs;
1272bf3db8aaSMartin Matuska 		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1273b38ff370SJamie Gritton 
1274b96bd95bSIan Lepore 		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1275b96bd95bSIan Lepore 		if (osrelstr == NULL)
1276b96bd95bSIan Lepore 		    strcpy(pr->pr_osrelease, ppr->pr_osrelease);
1277b96bd95bSIan Lepore 		else
1278b96bd95bSIan Lepore 		    strcpy(pr->pr_osrelease, osrelstr);
1279b96bd95bSIan Lepore 
12800304c731SJamie Gritton 		LIST_INIT(&pr->pr_children);
12810304c731SJamie Gritton 		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
128273d9e52dSJamie Gritton 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1283b38ff370SJamie Gritton 
1284679e1390SJamie Gritton #ifdef VIMAGE
1285679e1390SJamie Gritton 		/* Allocate a new vnet if specified. */
1286679e1390SJamie Gritton 		pr->pr_vnet = (pr_flags & PR_VNET)
1287679e1390SJamie Gritton 		    ? vnet_alloc() : ppr->pr_vnet;
1288679e1390SJamie Gritton #endif
1289b38ff370SJamie Gritton 		/*
1290b38ff370SJamie Gritton 		 * Allocate a dedicated cpuset for each jail.
1291b38ff370SJamie Gritton 		 * Unlike other initial settings, this may return an erorr.
1292b38ff370SJamie Gritton 		 */
12930304c731SJamie Gritton 		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1294b38ff370SJamie Gritton 		if (error) {
1295b38ff370SJamie Gritton 			prison_deref(pr, PD_LIST_XLOCKED);
1296b38ff370SJamie Gritton 			goto done_releroot;
1297b38ff370SJamie Gritton 		}
1298b38ff370SJamie Gritton 
1299b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1300b38ff370SJamie Gritton 		/*
1301b38ff370SJamie Gritton 		 * New prisons do not yet have a reference, because we do not
1302b6f47c23SJamie Gritton 		 * want others to see the incomplete prison once the
1303b38ff370SJamie Gritton 		 * allprison_lock is downgraded.
1304b38ff370SJamie Gritton 		 */
1305b38ff370SJamie Gritton 	} else {
1306b38ff370SJamie Gritton 		created = 0;
13072b0d6f81SJamie Gritton 		/*
13082b0d6f81SJamie Gritton 		 * Grab a reference for existing prisons, to ensure they
13092b0d6f81SJamie Gritton 		 * continue to exist for the duration of the call.
13102b0d6f81SJamie Gritton 		 */
13112b0d6f81SJamie Gritton 		pr->pr_ref++;
1312bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1313bdfc8cc4SJamie Gritton 		if ((pr->pr_flags & PR_VNET) &&
1314bdfc8cc4SJamie Gritton 		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1315bdfc8cc4SJamie Gritton 			error = EINVAL;
1316bdfc8cc4SJamie Gritton 			vfs_opterror(opts,
1317bdfc8cc4SJamie Gritton 			    "vnet jails cannot have IP address restrictions");
1318bdfc8cc4SJamie Gritton 			goto done_deref_locked;
1319bdfc8cc4SJamie Gritton 		}
1320bdfc8cc4SJamie Gritton #endif
13212b0d6f81SJamie Gritton #ifdef INET
13222b0d6f81SJamie Gritton 		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13232b0d6f81SJamie Gritton 			error = EINVAL;
13242b0d6f81SJamie Gritton 			vfs_opterror(opts,
13252b0d6f81SJamie Gritton 			    "ip4 cannot be changed after creation");
13262b0d6f81SJamie Gritton 			goto done_deref_locked;
13272b0d6f81SJamie Gritton 		}
13282b0d6f81SJamie Gritton #endif
13292b0d6f81SJamie Gritton #ifdef INET6
13302b0d6f81SJamie Gritton 		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
13312b0d6f81SJamie Gritton 			error = EINVAL;
13322b0d6f81SJamie Gritton 			vfs_opterror(opts,
13332b0d6f81SJamie Gritton 			    "ip6 cannot be changed after creation");
13342b0d6f81SJamie Gritton 			goto done_deref_locked;
13352b0d6f81SJamie Gritton 		}
13362b0d6f81SJamie Gritton #endif
1337b38ff370SJamie Gritton 	}
1338b38ff370SJamie Gritton 
1339b38ff370SJamie Gritton 	/* Do final error checking before setting anything. */
13400304c731SJamie Gritton 	if (gotslevel) {
13410304c731SJamie Gritton 		if (slevel < ppr->pr_securelevel) {
13420304c731SJamie Gritton 			error = EPERM;
13430304c731SJamie Gritton 			goto done_deref_locked;
13440304c731SJamie Gritton 		}
13450304c731SJamie Gritton 	}
1346b97457e2SJamie Gritton 	if (gotchildmax) {
1347b97457e2SJamie Gritton 		if (childmax >= ppr->pr_childmax) {
1348b97457e2SJamie Gritton 			error = EPERM;
1349b97457e2SJamie Gritton 			goto done_deref_locked;
1350b97457e2SJamie Gritton 		}
1351b97457e2SJamie Gritton 	}
13520304c731SJamie Gritton 	if (gotenforce) {
13530304c731SJamie Gritton 		if (enforce < ppr->pr_enforce_statfs) {
13540304c731SJamie Gritton 			error = EPERM;
13550304c731SJamie Gritton 			goto done_deref_locked;
13560304c731SJamie Gritton 		}
13570304c731SJamie Gritton 	}
13580cc207a6SMartin Matuska 	if (gotrsnum) {
13590cc207a6SMartin Matuska 		/*
13600cc207a6SMartin Matuska 		 * devfs_rsnum is a uint16_t
13610cc207a6SMartin Matuska 		 */
1362bf3db8aaSMartin Matuska 		if (rsnum < 0 || rsnum > 65535) {
13630cc207a6SMartin Matuska 			error = EINVAL;
13640cc207a6SMartin Matuska 			goto done_deref_locked;
13650cc207a6SMartin Matuska 		}
13660cc207a6SMartin Matuska 		/*
1367bf3db8aaSMartin Matuska 		 * Nested jails always inherit parent's devfs ruleset
13680cc207a6SMartin Matuska 		 */
13690cc207a6SMartin Matuska 		if (jailed(td->td_ucred)) {
13700cc207a6SMartin Matuska 			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
13710cc207a6SMartin Matuska 				error = EPERM;
13720cc207a6SMartin Matuska 				goto done_deref_locked;
1373bf3db8aaSMartin Matuska 			} else
13740cc207a6SMartin Matuska 				rsnum = ppr->pr_devfs_rsnum;
13750cc207a6SMartin Matuska 		}
13760cc207a6SMartin Matuska 	}
1377b38ff370SJamie Gritton #ifdef INET
13782b0d6f81SJamie Gritton 	if (ip4s > 0) {
13790304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP4) {
13800304c731SJamie Gritton 			/*
13810304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
13820304c731SJamie Gritton 			 * subset of the parent's list.  Don't worry
13830304c731SJamie Gritton 			 * about the parent being unlocked, as any
13840304c731SJamie Gritton 			 * setting is done with allprison_lock held.
13850304c731SJamie Gritton 			 */
13860304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip4s; ij++)
13872b0d6f81SJamie Gritton 				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
13880304c731SJamie Gritton 					break;
13890304c731SJamie Gritton 			if (ij == ppr->pr_ip4s) {
13900304c731SJamie Gritton 				error = EPERM;
13910304c731SJamie Gritton 				goto done_deref_locked;
13920304c731SJamie Gritton 			}
13930304c731SJamie Gritton 			if (ip4s > 1) {
13940304c731SJamie Gritton 				for (ii = ij = 1; ii < ip4s; ii++) {
13950304c731SJamie Gritton 					if (ip4[ii].s_addr ==
13960304c731SJamie Gritton 					    ppr->pr_ip4[0].s_addr)
1397b38ff370SJamie Gritton 						continue;
13980304c731SJamie Gritton 					for (; ij < ppr->pr_ip4s; ij++)
13990304c731SJamie Gritton 						if (ip4[ii].s_addr ==
14000304c731SJamie Gritton 						    ppr->pr_ip4[ij].s_addr)
14010304c731SJamie Gritton 							break;
14020304c731SJamie Gritton 					if (ij == ppr->pr_ip4s)
14030304c731SJamie Gritton 						break;
14040304c731SJamie Gritton 				}
14050304c731SJamie Gritton 				if (ij == ppr->pr_ip4s) {
14060304c731SJamie Gritton 					error = EPERM;
14070304c731SJamie Gritton 					goto done_deref_locked;
14080304c731SJamie Gritton 				}
14090304c731SJamie Gritton 			}
14100304c731SJamie Gritton 		}
14110304c731SJamie Gritton 		/*
14120304c731SJamie Gritton 		 * Check for conflicting IP addresses.  We permit them
14130304c731SJamie Gritton 		 * if there is no more than one IP on each jail.  If
14140304c731SJamie Gritton 		 * there is a duplicate on a jail with more than one
14150304c731SJamie Gritton 		 * IP stop checking and return error.
14160304c731SJamie Gritton 		 */
1417bdfc8cc4SJamie Gritton 		tppr = ppr;
1418bdfc8cc4SJamie Gritton #ifdef VIMAGE
1419bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1420bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1421bdfc8cc4SJamie Gritton 				break;
1422bdfc8cc4SJamie Gritton #endif
1423bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1424bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1425bdfc8cc4SJamie Gritton #ifdef VIMAGE
14262b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1427bdfc8cc4SJamie Gritton #endif
1428bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14290304c731SJamie Gritton 				descend = 0;
14300304c731SJamie Gritton 				continue;
14310304c731SJamie Gritton 			}
14320304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP4_USER))
14330304c731SJamie Gritton 				continue;
14340304c731SJamie Gritton 			descend = 0;
14350304c731SJamie Gritton 			if (tpr->pr_ip4 == NULL ||
14360304c731SJamie Gritton 			    (ip4s == 1 && tpr->pr_ip4s == 1))
14370304c731SJamie Gritton 				continue;
14380304c731SJamie Gritton 			for (ii = 0; ii < ip4s; ii++) {
14390ce1624dSStephen J. Kiernan 				if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
14400ce1624dSStephen J. Kiernan 				    0) {
14410304c731SJamie Gritton 					error = EADDRINUSE;
1442b38ff370SJamie Gritton 					vfs_opterror(opts,
1443b38ff370SJamie Gritton 					    "IPv4 addresses clash");
1444b38ff370SJamie Gritton 					goto done_deref_locked;
1445b38ff370SJamie Gritton 				}
14460304c731SJamie Gritton 			}
14470304c731SJamie Gritton 		}
14480304c731SJamie Gritton 	}
1449b38ff370SJamie Gritton #endif
1450b38ff370SJamie Gritton #ifdef INET6
14512b0d6f81SJamie Gritton 	if (ip6s > 0) {
14520304c731SJamie Gritton 		if (ppr->pr_flags & PR_IP6) {
14530304c731SJamie Gritton 			/*
14540304c731SJamie Gritton 			 * Make sure the new set of IP addresses is a
14550304c731SJamie Gritton 			 * subset of the parent's list.
14560304c731SJamie Gritton 			 */
14570304c731SJamie Gritton 			for (ij = 0; ij < ppr->pr_ip6s; ij++)
14580304c731SJamie Gritton 				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
14590304c731SJamie Gritton 				    &ppr->pr_ip6[ij]))
14600304c731SJamie Gritton 					break;
14610304c731SJamie Gritton 			if (ij == ppr->pr_ip6s) {
14620304c731SJamie Gritton 				error = EPERM;
14630304c731SJamie Gritton 				goto done_deref_locked;
14640304c731SJamie Gritton 			}
14650304c731SJamie Gritton 			if (ip6s > 1) {
14660304c731SJamie Gritton 				for (ii = ij = 1; ii < ip6s; ii++) {
14670304c731SJamie Gritton 					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
14680304c731SJamie Gritton 					     &ppr->pr_ip6[0]))
14690304c731SJamie Gritton 						continue;
14700304c731SJamie Gritton 					for (; ij < ppr->pr_ip6s; ij++)
14710304c731SJamie Gritton 						if (IN6_ARE_ADDR_EQUAL(
14722b0d6f81SJamie Gritton 						    &ip6[ii], &ppr->pr_ip6[ij]))
14730304c731SJamie Gritton 							break;
14740304c731SJamie Gritton 					if (ij == ppr->pr_ip6s)
14750304c731SJamie Gritton 						break;
14760304c731SJamie Gritton 				}
14770304c731SJamie Gritton 				if (ij == ppr->pr_ip6s) {
14780304c731SJamie Gritton 					error = EPERM;
14790304c731SJamie Gritton 					goto done_deref_locked;
14800304c731SJamie Gritton 				}
14810304c731SJamie Gritton 			}
14820304c731SJamie Gritton 		}
14830304c731SJamie Gritton 		/* Check for conflicting IP addresses. */
1484bdfc8cc4SJamie Gritton 		tppr = ppr;
1485bdfc8cc4SJamie Gritton #ifdef VIMAGE
1486bdfc8cc4SJamie Gritton 		for (; tppr != &prison0; tppr = tppr->pr_parent)
1487bdfc8cc4SJamie Gritton 			if (tppr->pr_flags & PR_VNET)
1488bdfc8cc4SJamie Gritton 				break;
1489bdfc8cc4SJamie Gritton #endif
1490bdfc8cc4SJamie Gritton 		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1491bdfc8cc4SJamie Gritton 			if (tpr == pr ||
1492bdfc8cc4SJamie Gritton #ifdef VIMAGE
14932b0d6f81SJamie Gritton 			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1494bdfc8cc4SJamie Gritton #endif
1495bdfc8cc4SJamie Gritton 			    tpr->pr_uref == 0) {
14960304c731SJamie Gritton 				descend = 0;
14970304c731SJamie Gritton 				continue;
14980304c731SJamie Gritton 			}
14990304c731SJamie Gritton 			if (!(tpr->pr_flags & PR_IP6_USER))
15000304c731SJamie Gritton 				continue;
15010304c731SJamie Gritton 			descend = 0;
15020304c731SJamie Gritton 			if (tpr->pr_ip6 == NULL ||
15030304c731SJamie Gritton 			    (ip6s == 1 && tpr->pr_ip6s == 1))
15040304c731SJamie Gritton 				continue;
15050304c731SJamie Gritton 			for (ii = 0; ii < ip6s; ii++) {
15060ce1624dSStephen J. Kiernan 				if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
15070ce1624dSStephen J. Kiernan 				    0) {
15080304c731SJamie Gritton 					error = EADDRINUSE;
1509b38ff370SJamie Gritton 					vfs_opterror(opts,
1510b38ff370SJamie Gritton 					    "IPv6 addresses clash");
1511b38ff370SJamie Gritton 					goto done_deref_locked;
1512b38ff370SJamie Gritton 				}
15130304c731SJamie Gritton 			}
15140304c731SJamie Gritton 		}
15150304c731SJamie Gritton 	}
1516b38ff370SJamie Gritton #endif
15170304c731SJamie Gritton 	onamelen = namelen = 0;
1518b6f47c23SJamie Gritton 	if (namelc != NULL) {
15196ab6058eSJamie Gritton 		/* Give a default name of the jid.  Also allow the name to be
15206ab6058eSJamie Gritton 		 * explicitly the jid - but not any other number, and only in
15216ab6058eSJamie Gritton 		 * normal form (no leading zero/etc).
15226ab6058eSJamie Gritton 		 */
1523b6f47c23SJamie Gritton 		if (namelc[0] == '\0')
1524b6f47c23SJamie Gritton 			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
15256ab6058eSJamie Gritton 		else if ((strtoul(namelc, &p, 10) != jid ||
15266ab6058eSJamie Gritton 			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1527b38ff370SJamie Gritton 			error = EINVAL;
1528babbbb9cSJamie Gritton 			vfs_opterror(opts,
1529babbbb9cSJamie Gritton 			    "name cannot be numeric (unless it is the jid)");
15300304c731SJamie Gritton 			goto done_deref_locked;
1531b38ff370SJamie Gritton 		}
1532b38ff370SJamie Gritton 		/*
15330304c731SJamie Gritton 		 * Make sure the name isn't too long for the prison or its
15340304c731SJamie Gritton 		 * children.
1535b38ff370SJamie Gritton 		 */
1536b6f47c23SJamie Gritton 		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1537b6f47c23SJamie Gritton 		onamelen = strlen(pr->pr_name + pnamelen);
1538b6f47c23SJamie Gritton 		namelen = strlen(namelc);
1539b6f47c23SJamie Gritton 		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
15400304c731SJamie Gritton 			error = ENAMETOOLONG;
15410304c731SJamie Gritton 			goto done_deref_locked;
15420304c731SJamie Gritton 		}
15430304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
15440304c731SJamie Gritton 			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
15450304c731SJamie Gritton 			    sizeof(pr->pr_name)) {
15460304c731SJamie Gritton 				error = ENAMETOOLONG;
15470304c731SJamie Gritton 				goto done_deref_locked;
15480304c731SJamie Gritton 			}
15490304c731SJamie Gritton 		}
15500304c731SJamie Gritton 	}
15510304c731SJamie Gritton 	if (pr_allow & ~ppr->pr_allow) {
15520304c731SJamie Gritton 		error = EPERM;
15530304c731SJamie Gritton 		goto done_deref_locked;
1554b38ff370SJamie Gritton 	}
1555b38ff370SJamie Gritton 
1556b6f47c23SJamie Gritton 	/*
1557b6f47c23SJamie Gritton 	 * Let modules check their parameters.  This requires unlocking and
1558b6f47c23SJamie Gritton 	 * then re-locking the prison, but this is still a valid state as long
1559b6f47c23SJamie Gritton 	 * as allprison_lock remains xlocked.
1560b6f47c23SJamie Gritton 	 */
1561b6f47c23SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1562b6f47c23SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1563b6f47c23SJamie Gritton 	if (error != 0) {
1564b6f47c23SJamie Gritton 		prison_deref(pr, created
1565b6f47c23SJamie Gritton 		    ? PD_LIST_XLOCKED
1566b6f47c23SJamie Gritton 		    : PD_DEREF | PD_LIST_XLOCKED);
1567b6f47c23SJamie Gritton 		goto done_releroot;
1568b6f47c23SJamie Gritton 	}
1569b6f47c23SJamie Gritton 	mtx_lock(&pr->pr_mtx);
1570b6f47c23SJamie Gritton 
1571b6f47c23SJamie Gritton 	/* At this point, all valid parameters should have been noted. */
1572b6f47c23SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
1573b6f47c23SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1574b6f47c23SJamie Gritton 			error = EINVAL;
1575b6f47c23SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
1576b6f47c23SJamie Gritton 			goto done_deref_locked;
1577b6f47c23SJamie Gritton 		}
1578b6f47c23SJamie Gritton 	}
1579b6f47c23SJamie Gritton 
1580b38ff370SJamie Gritton 	/* Set the parameters of the prison. */
1581b38ff370SJamie Gritton #ifdef INET
15820304c731SJamie Gritton 	redo_ip4 = 0;
15830304c731SJamie Gritton 	if (pr_flags & PR_IP4_USER) {
15840304c731SJamie Gritton 		pr->pr_flags |= PR_IP4;
1585b38ff370SJamie Gritton 		free(pr->pr_ip4, M_PRISON);
15860304c731SJamie Gritton 		pr->pr_ip4s = ip4s;
1587b38ff370SJamie Gritton 		pr->pr_ip4 = ip4;
1588b38ff370SJamie Gritton 		ip4 = NULL;
15890304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1590bdfc8cc4SJamie Gritton #ifdef VIMAGE
1591bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1592bdfc8cc4SJamie Gritton 				descend = 0;
1593bdfc8cc4SJamie Gritton 				continue;
1594bdfc8cc4SJamie Gritton 			}
1595bdfc8cc4SJamie Gritton #endif
15960304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, NULL)) {
15970304c731SJamie Gritton 				redo_ip4 = 1;
15980304c731SJamie Gritton 				descend = 0;
15990304c731SJamie Gritton 			}
16000304c731SJamie Gritton 		}
16010304c731SJamie Gritton 	}
1602b38ff370SJamie Gritton #endif
1603b38ff370SJamie Gritton #ifdef INET6
16040304c731SJamie Gritton 	redo_ip6 = 0;
16050304c731SJamie Gritton 	if (pr_flags & PR_IP6_USER) {
16060304c731SJamie Gritton 		pr->pr_flags |= PR_IP6;
1607b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
16080304c731SJamie Gritton 		pr->pr_ip6s = ip6s;
1609b38ff370SJamie Gritton 		pr->pr_ip6 = ip6;
1610b38ff370SJamie Gritton 		ip6 = NULL;
16110304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1612bdfc8cc4SJamie Gritton #ifdef VIMAGE
1613bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1614bdfc8cc4SJamie Gritton 				descend = 0;
1615bdfc8cc4SJamie Gritton 				continue;
1616bdfc8cc4SJamie Gritton 			}
1617bdfc8cc4SJamie Gritton #endif
16180304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, NULL)) {
16190304c731SJamie Gritton 				redo_ip6 = 1;
16200304c731SJamie Gritton 				descend = 0;
16210304c731SJamie Gritton 			}
16220304c731SJamie Gritton 		}
16230304c731SJamie Gritton 	}
1624b38ff370SJamie Gritton #endif
16250304c731SJamie Gritton 	if (gotslevel) {
1626b38ff370SJamie Gritton 		pr->pr_securelevel = slevel;
16270304c731SJamie Gritton 		/* Set all child jails to be at least this level. */
16280304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16290304c731SJamie Gritton 			if (tpr->pr_securelevel < slevel)
16300304c731SJamie Gritton 				tpr->pr_securelevel = slevel;
16310304c731SJamie Gritton 	}
1632b97457e2SJamie Gritton 	if (gotchildmax) {
1633b97457e2SJamie Gritton 		pr->pr_childmax = childmax;
1634b97457e2SJamie Gritton 		/* Set all child jails to under this limit. */
1635b97457e2SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1636b97457e2SJamie Gritton 			if (tpr->pr_childmax > childmax - level)
1637b97457e2SJamie Gritton 				tpr->pr_childmax = childmax > level
1638b97457e2SJamie Gritton 				    ? childmax - level : 0;
1639b97457e2SJamie Gritton 	}
16400304c731SJamie Gritton 	if (gotenforce) {
16410304c731SJamie Gritton 		pr->pr_enforce_statfs = enforce;
16420304c731SJamie Gritton 		/* Pass this restriction on to the children. */
16430304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16440304c731SJamie Gritton 			if (tpr->pr_enforce_statfs < enforce)
16450304c731SJamie Gritton 				tpr->pr_enforce_statfs = enforce;
16460304c731SJamie Gritton 	}
16470cc207a6SMartin Matuska 	if (gotrsnum) {
16480cc207a6SMartin Matuska 		pr->pr_devfs_rsnum = rsnum;
16490cc207a6SMartin Matuska 		/* Pass this restriction on to the children. */
16500cc207a6SMartin Matuska 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
16510cc207a6SMartin Matuska 			tpr->pr_devfs_rsnum = rsnum;
16520cc207a6SMartin Matuska 	}
1653b6f47c23SJamie Gritton 	if (namelc != NULL) {
16540304c731SJamie Gritton 		if (ppr == &prison0)
1655b6f47c23SJamie Gritton 			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
16560304c731SJamie Gritton 		else
16570304c731SJamie Gritton 			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1658b6f47c23SJamie Gritton 			    ppr->pr_name, namelc);
16590304c731SJamie Gritton 		/* Change this component of child names. */
16600304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
16610304c731SJamie Gritton 			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
16620304c731SJamie Gritton 			    strlen(tpr->pr_name + onamelen) + 1);
16630304c731SJamie Gritton 			bcopy(pr->pr_name, tpr->pr_name, namelen);
16640304c731SJamie Gritton 		}
16650304c731SJamie Gritton 	}
1666b38ff370SJamie Gritton 	if (path != NULL) {
16670304c731SJamie Gritton 		/* Try to keep a real-rooted full pathname. */
1668f6e633a9SMartin Matuska 		if (fullpath_disabled && path[0] == '/' &&
1669f6e633a9SMartin Matuska 		    strcmp(mypr->pr_path, "/"))
16700304c731SJamie Gritton 			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
16710304c731SJamie Gritton 			    mypr->pr_path, path);
16720304c731SJamie Gritton 		else
1673b38ff370SJamie Gritton 			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1674b38ff370SJamie Gritton 		pr->pr_root = root;
1675b38ff370SJamie Gritton 	}
167676ca6f88SJamie Gritton 	if (PR_HOST & ch_flags & ~pr_flags) {
167776ca6f88SJamie Gritton 		if (pr->pr_flags & PR_HOST) {
167876ca6f88SJamie Gritton 			/*
167976ca6f88SJamie Gritton 			 * Copy the parent's host info.  As with pr_ip4 above,
168076ca6f88SJamie Gritton 			 * the lack of a lock on the parent is not a problem;
168176ca6f88SJamie Gritton 			 * it is always set with allprison_lock at least
168276ca6f88SJamie Gritton 			 * shared, and is held exclusively here.
168376ca6f88SJamie Gritton 			 */
1684c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1685c1f19219SJamie Gritton 			    sizeof(pr->pr_hostname));
1686c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1687c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
1688c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1689c1f19219SJamie Gritton 			    sizeof(pr->pr_hostuuid));
169076ca6f88SJamie Gritton 			pr->pr_hostid = pr->pr_parent->pr_hostid;
169176ca6f88SJamie Gritton 		}
169276ca6f88SJamie Gritton 	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
169376ca6f88SJamie Gritton 		/* Set this prison, and any descendants without PR_HOST. */
1694b38ff370SJamie Gritton 		if (host != NULL)
1695c1f19219SJamie Gritton 			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
169676ca6f88SJamie Gritton 		if (domain != NULL)
1697c1f19219SJamie Gritton 			strlcpy(pr->pr_domainname, domain,
1698c1f19219SJamie Gritton 			    sizeof(pr->pr_domainname));
169976ca6f88SJamie Gritton 		if (uuid != NULL)
1700c1f19219SJamie Gritton 			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
170176ca6f88SJamie Gritton 		if (gothid)
170276ca6f88SJamie Gritton 			pr->pr_hostid = hid;
170376ca6f88SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
170476ca6f88SJamie Gritton 			if (tpr->pr_flags & PR_HOST)
170576ca6f88SJamie Gritton 				descend = 0;
170676ca6f88SJamie Gritton 			else {
170776ca6f88SJamie Gritton 				if (host != NULL)
1708c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostname,
1709c1f19219SJamie Gritton 					    pr->pr_hostname,
1710c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostname));
171176ca6f88SJamie Gritton 				if (domain != NULL)
1712c1f19219SJamie Gritton 					strlcpy(tpr->pr_domainname,
1713c1f19219SJamie Gritton 					    pr->pr_domainname,
1714c1f19219SJamie Gritton 					    sizeof(tpr->pr_domainname));
171576ca6f88SJamie Gritton 				if (uuid != NULL)
1716c1f19219SJamie Gritton 					strlcpy(tpr->pr_hostuuid,
1717c1f19219SJamie Gritton 					    pr->pr_hostuuid,
1718c1f19219SJamie Gritton 					    sizeof(tpr->pr_hostuuid));
171976ca6f88SJamie Gritton 				if (gothid)
172076ca6f88SJamie Gritton 					tpr->pr_hostid = hid;
172176ca6f88SJamie Gritton 			}
172276ca6f88SJamie Gritton 		}
172376ca6f88SJamie Gritton 	}
17240304c731SJamie Gritton 	if ((tallow = ch_allow & ~pr_allow)) {
17250304c731SJamie Gritton 		/* Clear allow bits in all children. */
17260304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
17270304c731SJamie Gritton 			tpr->pr_allow &= ~tallow;
17280304c731SJamie Gritton 	}
17290304c731SJamie Gritton 	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1730b38ff370SJamie Gritton 	/*
1731b38ff370SJamie Gritton 	 * Persistent prisons get an extra reference, and prisons losing their
1732b38ff370SJamie Gritton 	 * persist flag lose that reference.  Only do this for existing prisons
1733b38ff370SJamie Gritton 	 * for now, so new ones will remain unseen until after the module
1734b38ff370SJamie Gritton 	 * handlers have completed.
1735b38ff370SJamie Gritton 	 */
1736cc5fd8c7SJamie Gritton 	born = pr->pr_uref == 0;
1737b38ff370SJamie Gritton 	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1738b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1739b38ff370SJamie Gritton 			pr->pr_ref++;
1740b38ff370SJamie Gritton 			pr->pr_uref++;
1741b38ff370SJamie Gritton 		} else {
1742b38ff370SJamie Gritton 			pr->pr_ref--;
1743b38ff370SJamie Gritton 			pr->pr_uref--;
1744b38ff370SJamie Gritton 		}
1745b38ff370SJamie Gritton 	}
1746b38ff370SJamie Gritton 	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1747b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
1748b38ff370SJamie Gritton 
1749a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
17504b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && created)
1751a7ad07bfSEdward Tomasz Napierala 		prison_racct_attach(pr);
1752a7ad07bfSEdward Tomasz Napierala #endif
1753a7ad07bfSEdward Tomasz Napierala 
17540304c731SJamie Gritton 	/* Locks may have prevented a complete restriction of child IP
17550304c731SJamie Gritton 	 * addresses.  If so, allocate some more memory and try again.
17560304c731SJamie Gritton 	 */
17570304c731SJamie Gritton #ifdef INET
17580304c731SJamie Gritton 	while (redo_ip4) {
17590304c731SJamie Gritton 		ip4s = pr->pr_ip4s;
17600304c731SJamie Gritton 		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
17610304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17620304c731SJamie Gritton 		redo_ip4 = 0;
17630304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1764bdfc8cc4SJamie Gritton #ifdef VIMAGE
1765bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1766bdfc8cc4SJamie Gritton 				descend = 0;
1767bdfc8cc4SJamie Gritton 				continue;
1768bdfc8cc4SJamie Gritton 			}
1769bdfc8cc4SJamie Gritton #endif
17700304c731SJamie Gritton 			if (prison_restrict_ip4(tpr, ip4)) {
17710304c731SJamie Gritton 				if (ip4 != NULL)
17720304c731SJamie Gritton 					ip4 = NULL;
17730304c731SJamie Gritton 				else
17740304c731SJamie Gritton 					redo_ip4 = 1;
17750304c731SJamie Gritton 			}
17760304c731SJamie Gritton 		}
17770304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
17780304c731SJamie Gritton 	}
17790304c731SJamie Gritton #endif
17800304c731SJamie Gritton #ifdef INET6
17810304c731SJamie Gritton 	while (redo_ip6) {
17820304c731SJamie Gritton 		ip6s = pr->pr_ip6s;
17830304c731SJamie Gritton 		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
17840304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
17850304c731SJamie Gritton 		redo_ip6 = 0;
17860304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1787bdfc8cc4SJamie Gritton #ifdef VIMAGE
1788bdfc8cc4SJamie Gritton 			if (tpr->pr_flags & PR_VNET) {
1789bdfc8cc4SJamie Gritton 				descend = 0;
1790bdfc8cc4SJamie Gritton 				continue;
1791bdfc8cc4SJamie Gritton 			}
1792bdfc8cc4SJamie Gritton #endif
17930304c731SJamie Gritton 			if (prison_restrict_ip6(tpr, ip6)) {
17940304c731SJamie Gritton 				if (ip6 != NULL)
17950304c731SJamie Gritton 					ip6 = NULL;
17960304c731SJamie Gritton 				else
17970304c731SJamie Gritton 					redo_ip6 = 1;
17980304c731SJamie Gritton 			}
17990304c731SJamie Gritton 		}
18000304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
18010304c731SJamie Gritton 	}
18020304c731SJamie Gritton #endif
18030304c731SJamie Gritton 
1804b38ff370SJamie Gritton 	/* Let the modules do their work. */
1805b38ff370SJamie Gritton 	sx_downgrade(&allprison_lock);
1806cc5fd8c7SJamie Gritton 	if (born) {
1807b38ff370SJamie Gritton 		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1808b38ff370SJamie Gritton 		if (error) {
1809cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1810cc5fd8c7SJamie Gritton 			prison_deref(pr, created
1811cc5fd8c7SJamie Gritton 			    ? PD_LIST_SLOCKED
1812cc5fd8c7SJamie Gritton 			    : PD_DEREF | PD_LIST_SLOCKED);
1813b38ff370SJamie Gritton 			goto done_errmsg;
1814b38ff370SJamie Gritton 		}
1815b38ff370SJamie Gritton 	}
1816b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1817b38ff370SJamie Gritton 	if (error) {
1818cc5fd8c7SJamie Gritton 		if (born)
1819cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1820b38ff370SJamie Gritton 		prison_deref(pr, created
1821b38ff370SJamie Gritton 		    ? PD_LIST_SLOCKED
1822b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1823b38ff370SJamie Gritton 		goto done_errmsg;
1824b38ff370SJamie Gritton 	}
1825b38ff370SJamie Gritton 
1826b38ff370SJamie Gritton 	/* Attach this process to the prison if requested. */
1827b38ff370SJamie Gritton 	if (flags & JAIL_ATTACH) {
1828b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
1829b38ff370SJamie Gritton 		error = do_jail_attach(td, pr);
1830b38ff370SJamie Gritton 		if (error) {
1831b38ff370SJamie Gritton 			vfs_opterror(opts, "attach failed");
1832b38ff370SJamie Gritton 			if (!created)
1833b38ff370SJamie Gritton 				prison_deref(pr, PD_DEREF);
1834b38ff370SJamie Gritton 			goto done_errmsg;
1835b38ff370SJamie Gritton 		}
1836b38ff370SJamie Gritton 	}
1837b38ff370SJamie Gritton 
18381fb24974SEdward Tomasz Napierala #ifdef RACCT
18394b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && !created) {
1840f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18411fb24974SEdward Tomasz Napierala 			sx_sunlock(&allprison_lock);
18421fb24974SEdward Tomasz Napierala 		prison_racct_modify(pr);
1843f514b97bSEdward Tomasz Napierala 		if (!(flags & JAIL_ATTACH))
18441fb24974SEdward Tomasz Napierala 			sx_slock(&allprison_lock);
18451fb24974SEdward Tomasz Napierala 	}
18461fb24974SEdward Tomasz Napierala #endif
18471fb24974SEdward Tomasz Napierala 
18481fb24974SEdward Tomasz Napierala 	td->td_retval[0] = pr->pr_id;
18491fb24974SEdward Tomasz Napierala 
1850b38ff370SJamie Gritton 	/*
1851b38ff370SJamie Gritton 	 * Now that it is all there, drop the temporary reference from existing
1852b38ff370SJamie Gritton 	 * prisons.  Or add a reference to newly created persistent prisons
1853b38ff370SJamie Gritton 	 * (which was not done earlier so that the prison would not be publicly
1854b38ff370SJamie Gritton 	 * visible).
1855b38ff370SJamie Gritton 	 */
1856b38ff370SJamie Gritton 	if (!created) {
1857b38ff370SJamie Gritton 		prison_deref(pr, (flags & JAIL_ATTACH)
1858b38ff370SJamie Gritton 		    ? PD_DEREF
1859b38ff370SJamie Gritton 		    : PD_DEREF | PD_LIST_SLOCKED);
1860b38ff370SJamie Gritton 	} else {
1861b38ff370SJamie Gritton 		if (pr_flags & PR_PERSIST) {
1862b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
1863b38ff370SJamie Gritton 			pr->pr_ref++;
1864b38ff370SJamie Gritton 			pr->pr_uref++;
1865b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
1866b38ff370SJamie Gritton 		}
1867b38ff370SJamie Gritton 		if (!(flags & JAIL_ATTACH))
1868b38ff370SJamie Gritton 			sx_sunlock(&allprison_lock);
1869b38ff370SJamie Gritton 	}
1870c34bbd2aSEdward Tomasz Napierala 
1871cc5fd8c7SJamie Gritton 	goto done_free;
1872b38ff370SJamie Gritton 
18730304c731SJamie Gritton  done_deref_locked:
18740304c731SJamie Gritton 	prison_deref(pr, created
18750304c731SJamie Gritton 	    ? PD_LOCKED | PD_LIST_XLOCKED
18760304c731SJamie Gritton 	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
18770304c731SJamie Gritton 	goto done_releroot;
1878b38ff370SJamie Gritton  done_unlock_list:
1879b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
1880b38ff370SJamie Gritton  done_releroot:
18815050aa86SKonstantin Belousov 	if (root != NULL)
1882b38ff370SJamie Gritton 		vrele(root);
1883b38ff370SJamie Gritton  done_errmsg:
1884b38ff370SJamie Gritton 	if (error) {
1885176ff3a0SJamie Gritton 		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1886176ff3a0SJamie Gritton 		    &errmsg_len) == 0 && errmsg_len > 0) {
1887b38ff370SJamie Gritton 			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1888b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
1889b38ff370SJamie Gritton 				bcopy(errmsg,
1890b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1891b38ff370SJamie Gritton 				    errmsg_len);
1892b38ff370SJamie Gritton 			else
1893b38ff370SJamie Gritton 				copyout(errmsg,
1894b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
1895b38ff370SJamie Gritton 				    errmsg_len);
1896b38ff370SJamie Gritton 		}
1897b38ff370SJamie Gritton 	}
1898b38ff370SJamie Gritton  done_free:
1899b38ff370SJamie Gritton #ifdef INET
1900b38ff370SJamie Gritton 	free(ip4, M_PRISON);
1901b38ff370SJamie Gritton #endif
1902b38ff370SJamie Gritton #ifdef INET6
1903b38ff370SJamie Gritton 	free(ip6, M_PRISON);
1904b38ff370SJamie Gritton #endif
19056dfe0a3dSMartin Matuska 	if (g_path != NULL)
19066dfe0a3dSMartin Matuska 		free(g_path, M_TEMP);
1907b38ff370SJamie Gritton 	vfs_freeopts(opts);
1908b38ff370SJamie Gritton 	return (error);
1909b38ff370SJamie Gritton }
1910b38ff370SJamie Gritton 
1911b38ff370SJamie Gritton 
1912b38ff370SJamie Gritton /*
1913b38ff370SJamie Gritton  * struct jail_get_args {
1914b38ff370SJamie Gritton  *	struct iovec *iovp;
1915b38ff370SJamie Gritton  *	unsigned int iovcnt;
1916b38ff370SJamie Gritton  *	int flags;
1917b38ff370SJamie Gritton  * };
1918b38ff370SJamie Gritton  */
1919b38ff370SJamie Gritton int
19208451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap)
1921b38ff370SJamie Gritton {
1922b38ff370SJamie Gritton 	struct uio *auio;
1923b38ff370SJamie Gritton 	int error;
1924b38ff370SJamie Gritton 
1925b38ff370SJamie Gritton 	/* Check that we have an even number of iovecs. */
1926b38ff370SJamie Gritton 	if (uap->iovcnt & 1)
1927b38ff370SJamie Gritton 		return (EINVAL);
1928b38ff370SJamie Gritton 
1929b38ff370SJamie Gritton 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1930b38ff370SJamie Gritton 	if (error)
1931b38ff370SJamie Gritton 		return (error);
1932b38ff370SJamie Gritton 	error = kern_jail_get(td, auio, uap->flags);
1933b38ff370SJamie Gritton 	if (error == 0)
1934b38ff370SJamie Gritton 		error = copyout(auio->uio_iov, uap->iovp,
1935b38ff370SJamie Gritton 		    uap->iovcnt * sizeof (struct iovec));
1936b38ff370SJamie Gritton 	free(auio, M_IOV);
1937b38ff370SJamie Gritton 	return (error);
1938b38ff370SJamie Gritton }
1939b38ff370SJamie Gritton 
1940b38ff370SJamie Gritton int
1941b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1942b38ff370SJamie Gritton {
19430304c731SJamie Gritton 	struct prison *pr, *mypr;
1944b38ff370SJamie Gritton 	struct vfsopt *opt;
1945b38ff370SJamie Gritton 	struct vfsoptlist *opts;
1946b38ff370SJamie Gritton 	char *errmsg, *name;
19470304c731SJamie Gritton 	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1948b38ff370SJamie Gritton 
1949b38ff370SJamie Gritton 	if (flags & ~JAIL_GET_MASK)
1950b38ff370SJamie Gritton 		return (EINVAL);
1951b38ff370SJamie Gritton 
1952b38ff370SJamie Gritton 	/* Get the parameter list. */
1953b38ff370SJamie Gritton 	error = vfs_buildopts(optuio, &opts);
1954b38ff370SJamie Gritton 	if (error)
1955b38ff370SJamie Gritton 		return (error);
1956b38ff370SJamie Gritton 	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
19570304c731SJamie Gritton 	mypr = td->td_ucred->cr_prison;
19581e2a13e6SJamie Gritton 
1959b38ff370SJamie Gritton 	/*
1960b38ff370SJamie Gritton 	 * Find the prison specified by one of: lastjid, jid, name.
1961b38ff370SJamie Gritton 	 */
1962b38ff370SJamie Gritton 	sx_slock(&allprison_lock);
1963b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1964b38ff370SJamie Gritton 	if (error == 0) {
1965b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list) {
19660304c731SJamie Gritton 			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1967b38ff370SJamie Gritton 				mtx_lock(&pr->pr_mtx);
1968b38ff370SJamie Gritton 				if (pr->pr_ref > 0 &&
1969b38ff370SJamie Gritton 				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1970b38ff370SJamie Gritton 					break;
1971b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
1972b38ff370SJamie Gritton 			}
1973b38ff370SJamie Gritton 		}
1974b38ff370SJamie Gritton 		if (pr != NULL)
1975b38ff370SJamie Gritton 			goto found_prison;
1976b38ff370SJamie Gritton 		error = ENOENT;
1977b38ff370SJamie Gritton 		vfs_opterror(opts, "no jail after %d", jid);
1978b38ff370SJamie Gritton 		goto done_unlock_list;
1979b38ff370SJamie Gritton 	} else if (error != ENOENT)
1980b38ff370SJamie Gritton 		goto done_unlock_list;
1981b38ff370SJamie Gritton 
1982b38ff370SJamie Gritton 	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1983b38ff370SJamie Gritton 	if (error == 0) {
1984b38ff370SJamie Gritton 		if (jid != 0) {
19850304c731SJamie Gritton 			pr = prison_find_child(mypr, jid);
1986b38ff370SJamie Gritton 			if (pr != NULL) {
1987b38ff370SJamie Gritton 				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1988b38ff370SJamie Gritton 					mtx_unlock(&pr->pr_mtx);
1989b38ff370SJamie Gritton 					error = ENOENT;
1990b38ff370SJamie Gritton 					vfs_opterror(opts, "jail %d is dying",
1991b38ff370SJamie Gritton 					    jid);
1992b38ff370SJamie Gritton 					goto done_unlock_list;
1993b38ff370SJamie Gritton 				}
1994b38ff370SJamie Gritton 				goto found_prison;
1995b38ff370SJamie Gritton 			}
1996b38ff370SJamie Gritton 			error = ENOENT;
1997b38ff370SJamie Gritton 			vfs_opterror(opts, "jail %d not found", jid);
1998b38ff370SJamie Gritton 			goto done_unlock_list;
1999b38ff370SJamie Gritton 		}
2000b38ff370SJamie Gritton 	} else if (error != ENOENT)
2001b38ff370SJamie Gritton 		goto done_unlock_list;
2002b38ff370SJamie Gritton 
2003b38ff370SJamie Gritton 	error = vfs_getopt(opts, "name", (void **)&name, &len);
2004b38ff370SJamie Gritton 	if (error == 0) {
2005b38ff370SJamie Gritton 		if (len == 0 || name[len - 1] != '\0') {
2006b38ff370SJamie Gritton 			error = EINVAL;
2007b38ff370SJamie Gritton 			goto done_unlock_list;
2008b38ff370SJamie Gritton 		}
20090304c731SJamie Gritton 		pr = prison_find_name(mypr, name);
2010b38ff370SJamie Gritton 		if (pr != NULL) {
2011b38ff370SJamie Gritton 			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2012b38ff370SJamie Gritton 				mtx_unlock(&pr->pr_mtx);
2013b38ff370SJamie Gritton 				error = ENOENT;
2014b38ff370SJamie Gritton 				vfs_opterror(opts, "jail \"%s\" is dying",
2015b38ff370SJamie Gritton 				    name);
2016b38ff370SJamie Gritton 				goto done_unlock_list;
2017b38ff370SJamie Gritton 			}
2018b38ff370SJamie Gritton 			goto found_prison;
2019b38ff370SJamie Gritton 		}
2020b38ff370SJamie Gritton 		error = ENOENT;
2021b38ff370SJamie Gritton 		vfs_opterror(opts, "jail \"%s\" not found", name);
2022b38ff370SJamie Gritton 		goto done_unlock_list;
2023b38ff370SJamie Gritton 	} else if (error != ENOENT)
2024b38ff370SJamie Gritton 		goto done_unlock_list;
2025b38ff370SJamie Gritton 
2026b38ff370SJamie Gritton 	vfs_opterror(opts, "no jail specified");
2027b38ff370SJamie Gritton 	error = ENOENT;
2028b38ff370SJamie Gritton 	goto done_unlock_list;
2029b38ff370SJamie Gritton 
2030b38ff370SJamie Gritton  found_prison:
2031b38ff370SJamie Gritton 	/* Get the parameters of the prison. */
2032b38ff370SJamie Gritton 	pr->pr_ref++;
2033b38ff370SJamie Gritton 	locked = PD_LOCKED;
2034b38ff370SJamie Gritton 	td->td_retval[0] = pr->pr_id;
2035b38ff370SJamie Gritton 	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2036b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2037b38ff370SJamie Gritton 		goto done_deref;
20380304c731SJamie Gritton 	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
20390304c731SJamie Gritton 	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2040b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2041b38ff370SJamie Gritton 		goto done_deref;
20420304c731SJamie Gritton 	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
20430304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20440304c731SJamie Gritton 		goto done_deref;
20450304c731SJamie Gritton 	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2046b38ff370SJamie Gritton 	    sizeof(pr->pr_cpuset->cs_id));
2047b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2048b38ff370SJamie Gritton 		goto done_deref;
20490304c731SJamie Gritton 	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2050b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2051b38ff370SJamie Gritton 		goto done_deref;
2052b38ff370SJamie Gritton #ifdef INET
2053b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2054b38ff370SJamie Gritton 	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2055b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2056b38ff370SJamie Gritton 		goto done_deref;
2057b38ff370SJamie Gritton #endif
2058b38ff370SJamie Gritton #ifdef INET6
2059b38ff370SJamie Gritton 	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2060b38ff370SJamie Gritton 	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2061b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2062b38ff370SJamie Gritton 		goto done_deref;
2063b38ff370SJamie Gritton #endif
2064b38ff370SJamie Gritton 	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2065b38ff370SJamie Gritton 	    sizeof(pr->pr_securelevel));
2066b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2067b38ff370SJamie Gritton 		goto done_deref;
2068b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2069b97457e2SJamie Gritton 	    sizeof(pr->pr_childcount));
2070b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2071b97457e2SJamie Gritton 		goto done_deref;
2072b97457e2SJamie Gritton 	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2073b97457e2SJamie Gritton 	    sizeof(pr->pr_childmax));
2074b97457e2SJamie Gritton 	if (error != 0 && error != ENOENT)
2075b97457e2SJamie Gritton 		goto done_deref;
2076c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2077b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2078b38ff370SJamie Gritton 		goto done_deref;
2079c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
208076ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
208176ca6f88SJamie Gritton 		goto done_deref;
2082c1f19219SJamie Gritton 	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
208376ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
208476ca6f88SJamie Gritton 		goto done_deref;
2085841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32
2086a5c1afadSDmitry Chagin 	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
208776ca6f88SJamie Gritton 		uint32_t hid32 = pr->pr_hostid;
208876ca6f88SJamie Gritton 
208976ca6f88SJamie Gritton 		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
209076ca6f88SJamie Gritton 	} else
209176ca6f88SJamie Gritton #endif
209276ca6f88SJamie Gritton 	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
209376ca6f88SJamie Gritton 	    sizeof(pr->pr_hostid));
209476ca6f88SJamie Gritton 	if (error != 0 && error != ENOENT)
209576ca6f88SJamie Gritton 		goto done_deref;
20960304c731SJamie Gritton 	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
20970304c731SJamie Gritton 	    sizeof(pr->pr_enforce_statfs));
20980304c731SJamie Gritton 	if (error != 0 && error != ENOENT)
20990304c731SJamie Gritton 		goto done_deref;
21000cc207a6SMartin Matuska 	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
21010cc207a6SMartin Matuska 	    sizeof(pr->pr_devfs_rsnum));
21020cc207a6SMartin Matuska 	if (error != 0 && error != ENOENT)
21030cc207a6SMartin Matuska 		goto done_deref;
210402abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_names); fi++) {
21050304c731SJamie Gritton 		if (pr_flag_names[fi] == NULL)
21060304c731SJamie Gritton 			continue;
21070304c731SJamie Gritton 		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
21080304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2109b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2110b38ff370SJamie Gritton 			goto done_deref;
2111b38ff370SJamie Gritton 		i = !i;
21120304c731SJamie Gritton 		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2113b38ff370SJamie Gritton 		if (error != 0 && error != ENOENT)
2114b38ff370SJamie Gritton 			goto done_deref;
21150304c731SJamie Gritton 	}
211602abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_jailsys); fi++) {
21177cbf7213SJamie Gritton 		i = pr->pr_flags &
21187cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
21197cbf7213SJamie Gritton 		i = pr_flag_jailsys[fi].disable &&
21207cbf7213SJamie Gritton 		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
21217cbf7213SJamie Gritton 		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
21227cbf7213SJamie Gritton 		    : JAIL_SYS_INHERIT;
21237cbf7213SJamie Gritton 		error =
21247cbf7213SJamie Gritton 		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
21257cbf7213SJamie Gritton 		if (error != 0 && error != ENOENT)
21267cbf7213SJamie Gritton 			goto done_deref;
21277cbf7213SJamie Gritton 	}
212802abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_allow_names); fi++) {
21290304c731SJamie Gritton 		if (pr_allow_names[fi] == NULL)
21300304c731SJamie Gritton 			continue;
21310304c731SJamie Gritton 		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
21320304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
21330304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21340304c731SJamie Gritton 			goto done_deref;
21350304c731SJamie Gritton 		i = !i;
21360304c731SJamie Gritton 		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
21370304c731SJamie Gritton 		if (error != 0 && error != ENOENT)
21380304c731SJamie Gritton 			goto done_deref;
21390304c731SJamie Gritton 	}
2140b38ff370SJamie Gritton 	i = (pr->pr_uref == 0);
2141b38ff370SJamie Gritton 	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2142b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2143b38ff370SJamie Gritton 		goto done_deref;
2144b38ff370SJamie Gritton 	i = !i;
2145b38ff370SJamie Gritton 	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2146b38ff370SJamie Gritton 	if (error != 0 && error != ENOENT)
2147b38ff370SJamie Gritton 		goto done_deref;
2148bd96bd15SIan Lepore 	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2149bd96bd15SIan Lepore 	    sizeof(pr->pr_osreldate));
2150a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
2151a1a4c1b0SIan Lepore 		goto done_deref;
2152a1a4c1b0SIan Lepore 	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2153a1a4c1b0SIan Lepore 	if (error != 0 && error != ENOENT)
2154a1a4c1b0SIan Lepore 		goto done_deref;
2155b38ff370SJamie Gritton 
2156b38ff370SJamie Gritton 	/* Get the module parameters. */
2157b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2158b38ff370SJamie Gritton 	locked = 0;
2159b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2160b38ff370SJamie Gritton 	if (error)
2161b38ff370SJamie Gritton 		goto done_deref;
2162b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2163b38ff370SJamie Gritton 
2164b38ff370SJamie Gritton 	/* By now, all parameters should have been noted. */
2165b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2166b38ff370SJamie Gritton 		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2167b38ff370SJamie Gritton 			error = EINVAL;
2168b38ff370SJamie Gritton 			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2169b38ff370SJamie Gritton 			goto done_errmsg;
2170b38ff370SJamie Gritton 		}
2171b38ff370SJamie Gritton 	}
2172b38ff370SJamie Gritton 
2173b38ff370SJamie Gritton 	/* Write the fetched parameters back to userspace. */
2174b38ff370SJamie Gritton 	error = 0;
2175b38ff370SJamie Gritton 	TAILQ_FOREACH(opt, opts, link) {
2176b38ff370SJamie Gritton 		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2177b38ff370SJamie Gritton 			pos = 2 * opt->pos + 1;
2178b38ff370SJamie Gritton 			optuio->uio_iov[pos].iov_len = opt->len;
2179b38ff370SJamie Gritton 			if (opt->value != NULL) {
2180b38ff370SJamie Gritton 				if (optuio->uio_segflg == UIO_SYSSPACE) {
2181b38ff370SJamie Gritton 					bcopy(opt->value,
2182b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2183b38ff370SJamie Gritton 					    opt->len);
2184b38ff370SJamie Gritton 				} else {
2185b38ff370SJamie Gritton 					error = copyout(opt->value,
2186b38ff370SJamie Gritton 					    optuio->uio_iov[pos].iov_base,
2187b38ff370SJamie Gritton 					    opt->len);
2188b38ff370SJamie Gritton 					if (error)
2189b38ff370SJamie Gritton 						break;
2190b38ff370SJamie Gritton 				}
2191b38ff370SJamie Gritton 			}
2192b38ff370SJamie Gritton 		}
2193b38ff370SJamie Gritton 	}
2194b38ff370SJamie Gritton 	goto done_errmsg;
2195b38ff370SJamie Gritton 
2196b38ff370SJamie Gritton  done_deref:
2197b38ff370SJamie Gritton 	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2198b38ff370SJamie Gritton 	goto done_errmsg;
2199b38ff370SJamie Gritton 
2200b38ff370SJamie Gritton  done_unlock_list:
2201b38ff370SJamie Gritton 	sx_sunlock(&allprison_lock);
2202b38ff370SJamie Gritton  done_errmsg:
2203b38ff370SJamie Gritton 	if (error && errmsg_pos >= 0) {
2204b38ff370SJamie Gritton 		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2205b38ff370SJamie Gritton 		errmsg_pos = 2 * errmsg_pos + 1;
2206b38ff370SJamie Gritton 		if (errmsg_len > 0) {
2207b38ff370SJamie Gritton 			if (optuio->uio_segflg == UIO_SYSSPACE)
2208b38ff370SJamie Gritton 				bcopy(errmsg,
2209b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2210b38ff370SJamie Gritton 				    errmsg_len);
2211b38ff370SJamie Gritton 			else
2212b38ff370SJamie Gritton 				copyout(errmsg,
2213b38ff370SJamie Gritton 				    optuio->uio_iov[errmsg_pos].iov_base,
2214b38ff370SJamie Gritton 				    errmsg_len);
2215b38ff370SJamie Gritton 		}
2216b38ff370SJamie Gritton 	}
2217b38ff370SJamie Gritton 	vfs_freeopts(opts);
2218b38ff370SJamie Gritton 	return (error);
2219b38ff370SJamie Gritton }
2220b38ff370SJamie Gritton 
22210304c731SJamie Gritton 
2222b38ff370SJamie Gritton /*
2223b38ff370SJamie Gritton  * struct jail_remove_args {
2224b38ff370SJamie Gritton  *	int jid;
2225b38ff370SJamie Gritton  * };
2226b38ff370SJamie Gritton  */
2227b38ff370SJamie Gritton int
22288451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2229b38ff370SJamie Gritton {
22300304c731SJamie Gritton 	struct prison *pr, *cpr, *lpr, *tpr;
22310304c731SJamie Gritton 	int descend, error;
2232b38ff370SJamie Gritton 
2233b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_REMOVE);
2234b38ff370SJamie Gritton 	if (error)
2235b38ff370SJamie Gritton 		return (error);
2236b38ff370SJamie Gritton 
2237b38ff370SJamie Gritton 	sx_xlock(&allprison_lock);
22380304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2239b38ff370SJamie Gritton 	if (pr == NULL) {
2240b38ff370SJamie Gritton 		sx_xunlock(&allprison_lock);
2241b38ff370SJamie Gritton 		return (EINVAL);
2242b38ff370SJamie Gritton 	}
2243b38ff370SJamie Gritton 
22440304c731SJamie Gritton 	/* Remove all descendants of this prison, then remove this prison. */
22450304c731SJamie Gritton 	pr->pr_ref++;
22460304c731SJamie Gritton 	if (!LIST_EMPTY(&pr->pr_children)) {
22470304c731SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
22480304c731SJamie Gritton 		lpr = NULL;
22490304c731SJamie Gritton 		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
22500304c731SJamie Gritton 			mtx_lock(&cpr->pr_mtx);
22510304c731SJamie Gritton 			if (cpr->pr_ref > 0) {
22520304c731SJamie Gritton 				tpr = cpr;
22530304c731SJamie Gritton 				cpr->pr_ref++;
22540304c731SJamie Gritton 			} else {
22550304c731SJamie Gritton 				/* Already removed - do not do it again. */
22560304c731SJamie Gritton 				tpr = NULL;
22570304c731SJamie Gritton 			}
22580304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
22590304c731SJamie Gritton 			if (lpr != NULL) {
22600304c731SJamie Gritton 				mtx_lock(&lpr->pr_mtx);
22610304c731SJamie Gritton 				prison_remove_one(lpr);
22620304c731SJamie Gritton 				sx_xlock(&allprison_lock);
22630304c731SJamie Gritton 			}
22640304c731SJamie Gritton 			lpr = tpr;
22650304c731SJamie Gritton 		}
22660304c731SJamie Gritton 		if (lpr != NULL) {
22670304c731SJamie Gritton 			mtx_lock(&lpr->pr_mtx);
22680304c731SJamie Gritton 			prison_remove_one(lpr);
22690304c731SJamie Gritton 			sx_xlock(&allprison_lock);
22700304c731SJamie Gritton 		}
22710304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
22720304c731SJamie Gritton 	}
22730304c731SJamie Gritton 	prison_remove_one(pr);
22740304c731SJamie Gritton 	return (0);
22750304c731SJamie Gritton }
22760304c731SJamie Gritton 
22770304c731SJamie Gritton static void
22780304c731SJamie Gritton prison_remove_one(struct prison *pr)
22790304c731SJamie Gritton {
22800304c731SJamie Gritton 	struct proc *p;
22810304c731SJamie Gritton 	int deuref;
22820304c731SJamie Gritton 
2283b38ff370SJamie Gritton 	/* If the prison was persistent, it is not anymore. */
2284b38ff370SJamie Gritton 	deuref = 0;
2285b38ff370SJamie Gritton 	if (pr->pr_flags & PR_PERSIST) {
2286b38ff370SJamie Gritton 		pr->pr_ref--;
2287b38ff370SJamie Gritton 		deuref = PD_DEUREF;
2288b38ff370SJamie Gritton 		pr->pr_flags &= ~PR_PERSIST;
2289b38ff370SJamie Gritton 	}
2290b38ff370SJamie Gritton 
22910304c731SJamie Gritton 	/*
22920304c731SJamie Gritton 	 * jail_remove added a reference.  If that's the only one, remove
22930304c731SJamie Gritton 	 * the prison now.
22940304c731SJamie Gritton 	 */
22950304c731SJamie Gritton 	KASSERT(pr->pr_ref > 0,
22960304c731SJamie Gritton 	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
22970304c731SJamie Gritton 	if (pr->pr_ref == 1) {
2298b38ff370SJamie Gritton 		prison_deref(pr,
2299b38ff370SJamie Gritton 		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
23000304c731SJamie Gritton 		return;
2301b38ff370SJamie Gritton 	}
2302b38ff370SJamie Gritton 
2303b38ff370SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2304b38ff370SJamie Gritton 	sx_xunlock(&allprison_lock);
2305b38ff370SJamie Gritton 	/*
2306b38ff370SJamie Gritton 	 * Kill all processes unfortunate enough to be attached to this prison.
2307b38ff370SJamie Gritton 	 */
2308b38ff370SJamie Gritton 	sx_slock(&allproc_lock);
2309b38ff370SJamie Gritton 	LIST_FOREACH(p, &allproc, p_list) {
2310b38ff370SJamie Gritton 		PROC_LOCK(p);
2311b38ff370SJamie Gritton 		if (p->p_state != PRS_NEW && p->p_ucred &&
2312b38ff370SJamie Gritton 		    p->p_ucred->cr_prison == pr)
23138451d0ddSKip Macy 			kern_psignal(p, SIGKILL);
2314b38ff370SJamie Gritton 		PROC_UNLOCK(p);
2315b38ff370SJamie Gritton 	}
2316b38ff370SJamie Gritton 	sx_sunlock(&allproc_lock);
23170304c731SJamie Gritton 	/* Remove the temporary reference added by jail_remove. */
2318b38ff370SJamie Gritton 	prison_deref(pr, deuref | PD_DEREF);
231975c13541SPoul-Henning Kamp }
232075c13541SPoul-Henning Kamp 
23218571af59SJamie Gritton 
2322fd7a8150SMike Barcroft /*
23239ddb7954SMike Barcroft  * struct jail_attach_args {
23249ddb7954SMike Barcroft  *	int jid;
23259ddb7954SMike Barcroft  * };
2326fd7a8150SMike Barcroft  */
2327fd7a8150SMike Barcroft int
23288451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2329fd7a8150SMike Barcroft {
2330b38ff370SJamie Gritton 	struct prison *pr;
2331b38ff370SJamie Gritton 	int error;
2332b38ff370SJamie Gritton 
2333b38ff370SJamie Gritton 	error = priv_check(td, PRIV_JAIL_ATTACH);
2334b38ff370SJamie Gritton 	if (error)
2335b38ff370SJamie Gritton 		return (error);
2336b38ff370SJamie Gritton 
2337ef0ddea3SJamie Gritton 	/*
2338ef0ddea3SJamie Gritton 	 * Start with exclusive hold on allprison_lock to ensure that a possible
2339ef0ddea3SJamie Gritton 	 * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove.
2340ef0ddea3SJamie Gritton 	 * But then immediately downgrade it since we don't need to stop
2341ef0ddea3SJamie Gritton 	 * readers.
2342ef0ddea3SJamie Gritton 	 */
2343ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
2344ef0ddea3SJamie Gritton 	sx_downgrade(&allprison_lock);
23450304c731SJamie Gritton 	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2346b38ff370SJamie Gritton 	if (pr == NULL) {
2347b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2348b38ff370SJamie Gritton 		return (EINVAL);
2349b38ff370SJamie Gritton 	}
2350b38ff370SJamie Gritton 
2351b38ff370SJamie Gritton 	/*
2352b38ff370SJamie Gritton 	 * Do not allow a process to attach to a prison that is not
2353b38ff370SJamie Gritton 	 * considered to be "alive".
2354b38ff370SJamie Gritton 	 */
2355b38ff370SJamie Gritton 	if (pr->pr_uref == 0) {
2356b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2357b38ff370SJamie Gritton 		sx_sunlock(&allprison_lock);
2358b38ff370SJamie Gritton 		return (EINVAL);
2359b38ff370SJamie Gritton 	}
2360b38ff370SJamie Gritton 
2361b38ff370SJamie Gritton 	return (do_jail_attach(td, pr));
2362b38ff370SJamie Gritton }
2363b38ff370SJamie Gritton 
2364b38ff370SJamie Gritton static int
2365b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr)
2366b38ff370SJamie Gritton {
2367fd7a8150SMike Barcroft 	struct proc *p;
2368fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
23695050aa86SKonstantin Belousov 	int error;
2370fd7a8150SMike Barcroft 
237157f22bd4SJacques Vidrine 	/*
237257f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
237357f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
237457f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
237557f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
237657f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
237757f22bd4SJacques Vidrine 	 * of another.
237857f22bd4SJacques Vidrine 	 */
2379fd7a8150SMike Barcroft 	pr->pr_ref++;
2380b38ff370SJamie Gritton 	pr->pr_uref++;
2381fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
2382b38ff370SJamie Gritton 
2383b38ff370SJamie Gritton 	/* Let modules do whatever they need to prepare for attaching. */
2384b38ff370SJamie Gritton 	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2385b38ff370SJamie Gritton 	if (error) {
2386b38ff370SJamie Gritton 		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2387b38ff370SJamie Gritton 		return (error);
2388b38ff370SJamie Gritton 	}
2389dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
2390fd7a8150SMike Barcroft 
2391413628a7SBjoern A. Zeeb 	/*
2392413628a7SBjoern A. Zeeb 	 * Reparent the newly attached process to this jail.
2393413628a7SBjoern A. Zeeb 	 */
2394b38ff370SJamie Gritton 	p = td->td_proc;
2395413628a7SBjoern A. Zeeb 	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2396413628a7SBjoern A. Zeeb 	if (error)
2397b38ff370SJamie Gritton 		goto e_revert_osd;
2398413628a7SBjoern A. Zeeb 
2399cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2400fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
2401fd7a8150SMike Barcroft 		goto e_unlock;
2402fd7a8150SMike Barcroft #ifdef MAC
240330d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2404fd7a8150SMike Barcroft 		goto e_unlock;
2405fd7a8150SMike Barcroft #endif
240622db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2407f0725a8eSMateusz Guzik 	if ((error = pwd_chroot(td, pr->pr_root)))
24085050aa86SKonstantin Belousov 		goto e_revert_osd;
2409fd7a8150SMike Barcroft 
2410fd7a8150SMike Barcroft 	newcred = crget();
2411fd7a8150SMike Barcroft 	PROC_LOCK(p);
24121fb6767dSJamie Gritton 	oldcred = crcopysafe(p, newcred);
241369c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
2414daf63fd2SMateusz Guzik 	proc_set_cred(p, newcred);
24151fb6767dSJamie Gritton 	setsugid(p);
2416fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
2417097055e2SEdward Tomasz Napierala #ifdef RACCT
2418097055e2SEdward Tomasz Napierala 	racct_proc_ucred_changed(p, oldcred, newcred);
2419097055e2SEdward Tomasz Napierala #endif
24201fb6767dSJamie Gritton 	prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
2421fd7a8150SMike Barcroft 	crfree(oldcred);
2422fd7a8150SMike Barcroft 	return (0);
24231fb6767dSJamie Gritton 
2424fd7a8150SMike Barcroft  e_unlock:
242522db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
2426b38ff370SJamie Gritton  e_revert_osd:
2427b38ff370SJamie Gritton 	/* Tell modules this thread is still in its old jail after all. */
24281fb6767dSJamie Gritton 	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2429b38ff370SJamie Gritton 	prison_deref(pr, PD_DEREF | PD_DEUREF);
2430fd7a8150SMike Barcroft 	return (error);
2431fd7a8150SMike Barcroft }
2432fd7a8150SMike Barcroft 
24330304c731SJamie Gritton 
2434fd7a8150SMike Barcroft /*
2435fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
2436fd7a8150SMike Barcroft  */
243754b369c1SPawel Jakub Dawidek struct prison *
2438fd7a8150SMike Barcroft prison_find(int prid)
2439fd7a8150SMike Barcroft {
2440fd7a8150SMike Barcroft 	struct prison *pr;
2441fd7a8150SMike Barcroft 
2442dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
2443b38ff370SJamie Gritton 	TAILQ_FOREACH(pr, &allprison, pr_list) {
2444fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
2445fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
2446b38ff370SJamie Gritton 			if (pr->pr_ref > 0)
2447fd7a8150SMike Barcroft 				return (pr);
2448b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2449fd7a8150SMike Barcroft 		}
2450fd7a8150SMike Barcroft 	}
2451fd7a8150SMike Barcroft 	return (NULL);
2452fd7a8150SMike Barcroft }
2453fd7a8150SMike Barcroft 
2454b38ff370SJamie Gritton /*
24550304c731SJamie Gritton  * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2456b38ff370SJamie Gritton  */
2457b38ff370SJamie Gritton struct prison *
24580304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid)
2459b38ff370SJamie Gritton {
24600304c731SJamie Gritton 	struct prison *pr;
24610304c731SJamie Gritton 	int descend;
2462b38ff370SJamie Gritton 
2463b38ff370SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24640304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24650304c731SJamie Gritton 		if (pr->pr_id == prid) {
24660304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
24670304c731SJamie Gritton 			if (pr->pr_ref > 0)
24680304c731SJamie Gritton 				return (pr);
24690304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
24700304c731SJamie Gritton 		}
24710304c731SJamie Gritton 	}
24720304c731SJamie Gritton 	return (NULL);
24730304c731SJamie Gritton }
24740304c731SJamie Gritton 
24750304c731SJamie Gritton /*
24760304c731SJamie Gritton  * Look for the name relative to mypr.  Returns a locked prison or NULL.
24770304c731SJamie Gritton  */
24780304c731SJamie Gritton struct prison *
24790304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name)
24800304c731SJamie Gritton {
24810304c731SJamie Gritton 	struct prison *pr, *deadpr;
24820304c731SJamie Gritton 	size_t mylen;
24830304c731SJamie Gritton 	int descend;
24840304c731SJamie Gritton 
24850304c731SJamie Gritton 	sx_assert(&allprison_lock, SX_LOCKED);
24860304c731SJamie Gritton 	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2487b38ff370SJamie Gritton  again:
2488b38ff370SJamie Gritton 	deadpr = NULL;
24890304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
24900304c731SJamie Gritton 		if (!strcmp(pr->pr_name + mylen, name)) {
2491b38ff370SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2492b38ff370SJamie Gritton 			if (pr->pr_ref > 0) {
2493b38ff370SJamie Gritton 				if (pr->pr_uref > 0)
2494b38ff370SJamie Gritton 					return (pr);
2495b38ff370SJamie Gritton 				deadpr = pr;
2496b38ff370SJamie Gritton 			}
2497b38ff370SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2498b38ff370SJamie Gritton 		}
2499b38ff370SJamie Gritton 	}
25000304c731SJamie Gritton 	/* There was no valid prison - perhaps there was a dying one. */
2501b38ff370SJamie Gritton 	if (deadpr != NULL) {
2502b38ff370SJamie Gritton 		mtx_lock(&deadpr->pr_mtx);
2503b38ff370SJamie Gritton 		if (deadpr->pr_ref == 0) {
2504b38ff370SJamie Gritton 			mtx_unlock(&deadpr->pr_mtx);
2505b38ff370SJamie Gritton 			goto again;
2506b38ff370SJamie Gritton 		}
2507b38ff370SJamie Gritton 	}
2508b38ff370SJamie Gritton 	return (deadpr);
2509b38ff370SJamie Gritton }
2510b38ff370SJamie Gritton 
2511b38ff370SJamie Gritton /*
25120304c731SJamie Gritton  * See if a prison has the specific flag set.
25130304c731SJamie Gritton  */
25140304c731SJamie Gritton int
25150304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag)
25160304c731SJamie Gritton {
25170304c731SJamie Gritton 
25180304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
25190304c731SJamie Gritton 	return (cred->cr_prison->pr_flags & flag);
25200304c731SJamie Gritton }
25210304c731SJamie Gritton 
25220304c731SJamie Gritton int
25230304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag)
25240304c731SJamie Gritton {
25250304c731SJamie Gritton 
25260304c731SJamie Gritton 	/* This is an atomic read, so no locking is necessary. */
25270304c731SJamie Gritton 	return (cred->cr_prison->pr_allow & flag);
25280304c731SJamie Gritton }
25290304c731SJamie Gritton 
25300304c731SJamie Gritton /*
2531b38ff370SJamie Gritton  * Remove a prison reference.  If that was the last reference, remove the
2532b38ff370SJamie Gritton  * prison itself - but not in this context in case there are locks held.
2533b38ff370SJamie Gritton  */
253491421ba2SRobert Watson void
25351ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
253691421ba2SRobert Watson {
253773d9e52dSJamie Gritton 	int ref;
253891421ba2SRobert Watson 
25391ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
254073d9e52dSJamie Gritton 	ref = --pr->pr_ref;
254101137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
254273d9e52dSJamie Gritton 	if (ref == 0)
2543c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2544c2cda609SPawel Jakub Dawidek }
2545c2cda609SPawel Jakub Dawidek 
25461ba4a712SPawel Jakub Dawidek void
25471ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
25481ba4a712SPawel Jakub Dawidek {
25491ba4a712SPawel Jakub Dawidek 
25501ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
25511ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
25521ba4a712SPawel Jakub Dawidek }
25531ba4a712SPawel Jakub Dawidek 
255473d9e52dSJamie Gritton /*
255573d9e52dSJamie Gritton  * Complete a call to either prison_free or prison_proc_free.
255673d9e52dSJamie Gritton  */
2557c2cda609SPawel Jakub Dawidek static void
2558c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
2559c2cda609SPawel Jakub Dawidek {
256073d9e52dSJamie Gritton 	struct prison *pr = context;
2561b38ff370SJamie Gritton 
2562ef0ddea3SJamie Gritton 	sx_xlock(&allprison_lock);
256373d9e52dSJamie Gritton 	mtx_lock(&pr->pr_mtx);
256473d9e52dSJamie Gritton 	prison_deref(pr, pr->pr_uref
2565ef0ddea3SJamie Gritton 	    ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED
2566ef0ddea3SJamie Gritton 	    : PD_LOCKED | PD_LIST_XLOCKED);
2567b38ff370SJamie Gritton }
2568b38ff370SJamie Gritton 
2569b38ff370SJamie Gritton /*
2570b38ff370SJamie Gritton  * Remove a prison reference (usually).  This internal version assumes no
2571b38ff370SJamie Gritton  * mutexes are held, except perhaps the prison itself.  If there are no more
2572b38ff370SJamie Gritton  * references, release and delist the prison.  On completion, the prison lock
2573b38ff370SJamie Gritton  * and the allprison lock are both unlocked.
2574b38ff370SJamie Gritton  */
2575b38ff370SJamie Gritton static void
2576b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags)
2577b38ff370SJamie Gritton {
25780304c731SJamie Gritton 	struct prison *ppr, *tpr;
2579cc5fd8c7SJamie Gritton 	int ref, lasturef;
2580c2cda609SPawel Jakub Dawidek 
2581b38ff370SJamie Gritton 	if (!(flags & PD_LOCKED))
2582b38ff370SJamie Gritton 		mtx_lock(&pr->pr_mtx);
25830304c731SJamie Gritton 	for (;;) {
2584e6d5cb63SJamie Gritton 		if (flags & PD_DEUREF) {
258573d9e52dSJamie Gritton 			KASSERT(pr->pr_uref > 0,
258673d9e52dSJamie Gritton 			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
258773d9e52dSJamie Gritton 			     pr->pr_id));
2588e6d5cb63SJamie Gritton 			pr->pr_uref--;
2589cc5fd8c7SJamie Gritton 			lasturef = pr->pr_uref == 0;
2590cc5fd8c7SJamie Gritton 			if (lasturef)
2591cc5fd8c7SJamie Gritton 				pr->pr_ref++;
2592e6d5cb63SJamie Gritton 			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2593cc5fd8c7SJamie Gritton 		} else
2594cc5fd8c7SJamie Gritton 			lasturef = 0;
259573d9e52dSJamie Gritton 		if (flags & PD_DEREF) {
259673d9e52dSJamie Gritton 			KASSERT(pr->pr_ref > 0,
259773d9e52dSJamie Gritton 			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
259873d9e52dSJamie Gritton 			     pr->pr_id));
2599b38ff370SJamie Gritton 			pr->pr_ref--;
260073d9e52dSJamie Gritton 		}
2601cc5fd8c7SJamie Gritton 		ref = pr->pr_ref;
2602b38ff370SJamie Gritton 		mtx_unlock(&pr->pr_mtx);
2603cc5fd8c7SJamie Gritton 
2604cc5fd8c7SJamie Gritton 		/*
2605cc5fd8c7SJamie Gritton 		 * Tell the modules if the last user reference was removed
2606cc5fd8c7SJamie Gritton 		 * (even it sticks around in dying state).
2607cc5fd8c7SJamie Gritton 		 */
2608cc5fd8c7SJamie Gritton 		if (lasturef) {
2609cc5fd8c7SJamie Gritton 			if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
2610cc5fd8c7SJamie Gritton 				sx_xlock(&allprison_lock);
2611cc5fd8c7SJamie Gritton 				flags |= PD_LIST_XLOCKED;
2612cc5fd8c7SJamie Gritton 			}
2613cc5fd8c7SJamie Gritton 			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2614cc5fd8c7SJamie Gritton 			mtx_lock(&pr->pr_mtx);
2615cc5fd8c7SJamie Gritton 			ref = --pr->pr_ref;
2616cc5fd8c7SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
2617cc5fd8c7SJamie Gritton 		}
2618cc5fd8c7SJamie Gritton 
2619cc5fd8c7SJamie Gritton 		/* If the prison still has references, nothing else to do. */
2620cc5fd8c7SJamie Gritton 		if (ref > 0) {
2621b38ff370SJamie Gritton 			if (flags & PD_LIST_SLOCKED)
2622b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2623b38ff370SJamie Gritton 			else if (flags & PD_LIST_XLOCKED)
2624b38ff370SJamie Gritton 				sx_xunlock(&allprison_lock);
2625b38ff370SJamie Gritton 			return;
2626b38ff370SJamie Gritton 		}
2627c2cda609SPawel Jakub Dawidek 
2628b38ff370SJamie Gritton 		if (flags & PD_LIST_SLOCKED) {
2629b38ff370SJamie Gritton 			if (!sx_try_upgrade(&allprison_lock)) {
2630b38ff370SJamie Gritton 				sx_sunlock(&allprison_lock);
2631c2cda609SPawel Jakub Dawidek 				sx_xlock(&allprison_lock);
2632b38ff370SJamie Gritton 			}
2633b38ff370SJamie Gritton 		} else if (!(flags & PD_LIST_XLOCKED))
2634b38ff370SJamie Gritton 			sx_xlock(&allprison_lock);
2635b38ff370SJamie Gritton 
2636b38ff370SJamie Gritton 		TAILQ_REMOVE(&allprison, pr, pr_list);
26370304c731SJamie Gritton 		LIST_REMOVE(pr, pr_sibling);
26380304c731SJamie Gritton 		ppr = pr->pr_parent;
26390304c731SJamie Gritton 		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2640b97457e2SJamie Gritton 			tpr->pr_childcount--;
2641c4884ffaSJamie Gritton 		sx_xunlock(&allprison_lock);
26421ba4a712SPawel Jakub Dawidek 
2643679e1390SJamie Gritton #ifdef VIMAGE
26440cb8b6a9SMarko Zec 		if (pr->pr_vnet != ppr->pr_vnet)
2645679e1390SJamie Gritton 			vnet_destroy(pr->pr_vnet);
2646679e1390SJamie Gritton #endif
26475050aa86SKonstantin Belousov 		if (pr->pr_root != NULL)
2648b3059e09SRobert Watson 			vrele(pr->pr_root);
2649b3059e09SRobert Watson 		mtx_destroy(&pr->pr_mtx);
2650413628a7SBjoern A. Zeeb #ifdef INET
2651413628a7SBjoern A. Zeeb 		free(pr->pr_ip4, M_PRISON);
2652413628a7SBjoern A. Zeeb #endif
2653b38ff370SJamie Gritton #ifdef INET6
2654b38ff370SJamie Gritton 		free(pr->pr_ip6, M_PRISON);
2655b38ff370SJamie Gritton #endif
2656b38ff370SJamie Gritton 		if (pr->pr_cpuset != NULL)
2657b38ff370SJamie Gritton 			cpuset_rel(pr->pr_cpuset);
2658b38ff370SJamie Gritton 		osd_jail_exit(pr);
2659a7ad07bfSEdward Tomasz Napierala #ifdef RACCT
26604b5c9cf6SEdward Tomasz Napierala 		if (racct_enable)
2661a7ad07bfSEdward Tomasz Napierala 			prison_racct_detach(pr);
2662ec125fbbSEdward Tomasz Napierala #endif
26631ede983cSDag-Erling Smørgrav 		free(pr, M_PRISON);
26640304c731SJamie Gritton 
26650304c731SJamie Gritton 		/* Removing a prison frees a reference on its parent. */
26660304c731SJamie Gritton 		pr = ppr;
26670304c731SJamie Gritton 		mtx_lock(&pr->pr_mtx);
2668e6d5cb63SJamie Gritton 		flags = PD_DEREF | PD_DEUREF;
26690304c731SJamie Gritton 	}
2670b3059e09SRobert Watson }
2671b3059e09SRobert Watson 
267291421ba2SRobert Watson void
26731ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
26741ba4a712SPawel Jakub Dawidek {
26751ba4a712SPawel Jakub Dawidek 
26761ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
26771ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
2678af7bd9a4SJamie Gritton 	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
26791ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
26801ba4a712SPawel Jakub Dawidek }
26811ba4a712SPawel Jakub Dawidek 
26821ba4a712SPawel Jakub Dawidek void
268391421ba2SRobert Watson prison_hold(struct prison *pr)
268491421ba2SRobert Watson {
268591421ba2SRobert Watson 
268601137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
26871ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
268801137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
268901137630SRobert Watson }
269001137630SRobert Watson 
2691413628a7SBjoern A. Zeeb void
2692413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr)
269301137630SRobert Watson {
269401137630SRobert Watson 
2695413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2696b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2697b38ff370SJamie Gritton 	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2698b38ff370SJamie Gritton 	pr->pr_uref++;
2699413628a7SBjoern A. Zeeb 	mtx_unlock(&pr->pr_mtx);
270075c13541SPoul-Henning Kamp }
270175c13541SPoul-Henning Kamp 
270275c13541SPoul-Henning Kamp void
2703413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr)
270475c13541SPoul-Henning Kamp {
2705413628a7SBjoern A. Zeeb 
2706413628a7SBjoern A. Zeeb 	mtx_lock(&pr->pr_mtx);
2707b38ff370SJamie Gritton 	KASSERT(pr->pr_uref > 0,
2708b38ff370SJamie Gritton 	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
270973d9e52dSJamie Gritton 	if (pr->pr_uref > 1)
271073d9e52dSJamie Gritton 		pr->pr_uref--;
271173d9e52dSJamie Gritton 	else {
271273d9e52dSJamie Gritton 		/*
271373d9e52dSJamie Gritton 		 * Don't remove the last user reference in this context, which
271473d9e52dSJamie Gritton 		 * is expected to be a process that is not only locked, but
271573d9e52dSJamie Gritton 		 * also half dead.
271673d9e52dSJamie Gritton 		 */
271773d9e52dSJamie Gritton 		pr->pr_ref++;
271873d9e52dSJamie Gritton 		mtx_unlock(&pr->pr_mtx);
271973d9e52dSJamie Gritton 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
272073d9e52dSJamie Gritton 		return;
272173d9e52dSJamie Gritton 	}
272273d9e52dSJamie Gritton 	mtx_unlock(&pr->pr_mtx);
2723413628a7SBjoern A. Zeeb }
2724413628a7SBjoern A. Zeeb 
2725413628a7SBjoern A. Zeeb /*
2726ca04ba64SJamie Gritton  * Check if a jail supports the given address family.
2727ca04ba64SJamie Gritton  *
2728ca04ba64SJamie Gritton  * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
2729ca04ba64SJamie Gritton  * if not.
2730ca04ba64SJamie Gritton  */
2731ca04ba64SJamie Gritton int
2732ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af)
2733ca04ba64SJamie Gritton {
27340304c731SJamie Gritton 	struct prison *pr;
2735ca04ba64SJamie Gritton 	int error;
2736ca04ba64SJamie Gritton 
2737ca04ba64SJamie Gritton 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2738ca04ba64SJamie Gritton 
27390304c731SJamie Gritton 	pr = cred->cr_prison;
2740499650a0SJamie Gritton #ifdef VIMAGE
27416bb79563SJamie Gritton 	/* Prisons with their own network stack are not limited. */
2742de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
27436bb79563SJamie Gritton 		return (0);
2744499650a0SJamie Gritton #endif
27456bb79563SJamie Gritton 
2746ca04ba64SJamie Gritton 	error = 0;
2747ca04ba64SJamie Gritton 	switch (af)
2748ca04ba64SJamie Gritton 	{
2749ca04ba64SJamie Gritton #ifdef INET
2750ca04ba64SJamie Gritton 	case AF_INET:
27510304c731SJamie Gritton 		if (pr->pr_flags & PR_IP4)
27520304c731SJamie Gritton 		{
27530304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27540304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
2755ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
27560304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
27570304c731SJamie Gritton 		}
2758ca04ba64SJamie Gritton 		break;
2759ca04ba64SJamie Gritton #endif
2760ca04ba64SJamie Gritton #ifdef INET6
2761ca04ba64SJamie Gritton 	case AF_INET6:
27620304c731SJamie Gritton 		if (pr->pr_flags & PR_IP6)
27630304c731SJamie Gritton 		{
27640304c731SJamie Gritton 			mtx_lock(&pr->pr_mtx);
27650304c731SJamie Gritton 			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
2766ca04ba64SJamie Gritton 				error = EAFNOSUPPORT;
27670304c731SJamie Gritton 			mtx_unlock(&pr->pr_mtx);
27680304c731SJamie Gritton 		}
2769ca04ba64SJamie Gritton 		break;
2770ca04ba64SJamie Gritton #endif
2771ca04ba64SJamie Gritton 	case AF_LOCAL:
2772ca04ba64SJamie Gritton 	case AF_ROUTE:
2773ca04ba64SJamie Gritton 		break;
2774ca04ba64SJamie Gritton 	default:
27750304c731SJamie Gritton 		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
2776ca04ba64SJamie Gritton 			error = EAFNOSUPPORT;
2777ca04ba64SJamie Gritton 	}
2778ca04ba64SJamie Gritton 	return (error);
2779ca04ba64SJamie Gritton }
2780ca04ba64SJamie Gritton 
2781ca04ba64SJamie Gritton /*
2782413628a7SBjoern A. Zeeb  * Check if given address belongs to the jail referenced by cred (wrapper to
2783413628a7SBjoern A. Zeeb  * prison_check_ip[46]).
2784413628a7SBjoern A. Zeeb  *
27850304c731SJamie Gritton  * Returns 0 if jail doesn't restrict the address family or if address belongs
27860304c731SJamie Gritton  * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
27870304c731SJamie Gritton  * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
2788413628a7SBjoern A. Zeeb  */
2789413628a7SBjoern A. Zeeb int
279091421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
279175c13541SPoul-Henning Kamp {
2792413628a7SBjoern A. Zeeb #ifdef INET
27939ddb7954SMike Barcroft 	struct sockaddr_in *sai;
2794413628a7SBjoern A. Zeeb #endif
2795413628a7SBjoern A. Zeeb #ifdef INET6
2796413628a7SBjoern A. Zeeb 	struct sockaddr_in6 *sai6;
2797413628a7SBjoern A. Zeeb #endif
2798b89e82ddSJamie Gritton 	int error;
279975c13541SPoul-Henning Kamp 
2800413628a7SBjoern A. Zeeb 	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2801413628a7SBjoern A. Zeeb 	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
2802413628a7SBjoern A. Zeeb 
2803de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
2804de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
2805de0bd6f7SBjoern A. Zeeb 		return (0);
2806de0bd6f7SBjoern A. Zeeb #endif
2807de0bd6f7SBjoern A. Zeeb 
2808b89e82ddSJamie Gritton 	error = 0;
2809413628a7SBjoern A. Zeeb 	switch (sa->sa_family)
2810413628a7SBjoern A. Zeeb 	{
2811413628a7SBjoern A. Zeeb #ifdef INET
2812413628a7SBjoern A. Zeeb 	case AF_INET:
28139ddb7954SMike Barcroft 		sai = (struct sockaddr_in *)sa;
2814b89e82ddSJamie Gritton 		error = prison_check_ip4(cred, &sai->sin_addr);
2815413628a7SBjoern A. Zeeb 		break;
2816413628a7SBjoern A. Zeeb #endif
2817413628a7SBjoern A. Zeeb #ifdef INET6
2818413628a7SBjoern A. Zeeb 	case AF_INET6:
2819413628a7SBjoern A. Zeeb 		sai6 = (struct sockaddr_in6 *)sa;
2820b89e82ddSJamie Gritton 		error = prison_check_ip6(cred, &sai6->sin6_addr);
2821413628a7SBjoern A. Zeeb 		break;
2822413628a7SBjoern A. Zeeb #endif
2823413628a7SBjoern A. Zeeb 	default:
28240304c731SJamie Gritton 		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
2825b89e82ddSJamie Gritton 			error = EAFNOSUPPORT;
2826413628a7SBjoern A. Zeeb 	}
2827b89e82ddSJamie Gritton 	return (error);
282875c13541SPoul-Henning Kamp }
282991421ba2SRobert Watson 
283091421ba2SRobert Watson /*
283191421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
283291421ba2SRobert Watson  */
283391421ba2SRobert Watson int
28349ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
283591421ba2SRobert Watson {
283691421ba2SRobert Watson 
28370304c731SJamie Gritton 	return ((cred1->cr_prison == cred2->cr_prison ||
28380304c731SJamie Gritton 	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
28390304c731SJamie Gritton }
284091421ba2SRobert Watson 
28410304c731SJamie Gritton /*
28420304c731SJamie Gritton  * Return 1 if p2 is a child of p1, otherwise 0.
28430304c731SJamie Gritton  */
28440304c731SJamie Gritton int
28450304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2)
28460304c731SJamie Gritton {
28470304c731SJamie Gritton 
28480304c731SJamie Gritton 	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
28490304c731SJamie Gritton 		if (pr1 == pr2)
28500304c731SJamie Gritton 			return (1);
285191421ba2SRobert Watson 	return (0);
285291421ba2SRobert Watson }
285391421ba2SRobert Watson 
285491421ba2SRobert Watson /*
285591421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
285691421ba2SRobert Watson  */
285791421ba2SRobert Watson int
28589ddb7954SMike Barcroft jailed(struct ucred *cred)
285991421ba2SRobert Watson {
286091421ba2SRobert Watson 
28610304c731SJamie Gritton 	return (cred->cr_prison != &prison0);
286291421ba2SRobert Watson }
28639484d0c0SRobert Drehmel 
28649484d0c0SRobert Drehmel /*
2865de0bd6f7SBjoern A. Zeeb  * Return 1 if the passed credential is in a jail and that jail does not
2866de0bd6f7SBjoern A. Zeeb  * have its own virtual network stack, otherwise 0.
2867de0bd6f7SBjoern A. Zeeb  */
2868de0bd6f7SBjoern A. Zeeb int
2869de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred)
2870de0bd6f7SBjoern A. Zeeb {
2871de0bd6f7SBjoern A. Zeeb 
2872de0bd6f7SBjoern A. Zeeb 	if (!jailed(cred))
2873de0bd6f7SBjoern A. Zeeb 		return (0);
2874de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE
2875de0bd6f7SBjoern A. Zeeb 	if (prison_owns_vnet(cred))
2876de0bd6f7SBjoern A. Zeeb 		return (0);
2877de0bd6f7SBjoern A. Zeeb #endif
2878de0bd6f7SBjoern A. Zeeb 
2879de0bd6f7SBjoern A. Zeeb 	return (1);
2880de0bd6f7SBjoern A. Zeeb }
2881de0bd6f7SBjoern A. Zeeb 
2882de0bd6f7SBjoern A. Zeeb /*
28837455b100SJamie Gritton  * Return the correct hostname (domainname, et al) for the passed credential.
28849484d0c0SRobert Drehmel  */
2885ad1ff099SRobert Drehmel void
28869ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
28879484d0c0SRobert Drehmel {
288876ca6f88SJamie Gritton 	struct prison *pr;
28899484d0c0SRobert Drehmel 
28907455b100SJamie Gritton 	/*
28917455b100SJamie Gritton 	 * A NULL credential can be used to shortcut to the physical
28927455b100SJamie Gritton 	 * system's hostname.
28937455b100SJamie Gritton 	 */
289476ca6f88SJamie Gritton 	pr = (cred != NULL) ? cred->cr_prison : &prison0;
289576ca6f88SJamie Gritton 	mtx_lock(&pr->pr_mtx);
2896c1f19219SJamie Gritton 	strlcpy(buf, pr->pr_hostname, size);
289776ca6f88SJamie Gritton 	mtx_unlock(&pr->pr_mtx);
28989484d0c0SRobert Drehmel }
2899fd7a8150SMike Barcroft 
29007455b100SJamie Gritton void
29017455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size)
29027455b100SJamie Gritton {
29037455b100SJamie Gritton 
29047455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
2905c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_domainname, size);
29067455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
29077455b100SJamie Gritton }
29087455b100SJamie Gritton 
29097455b100SJamie Gritton void
29107455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size)
29117455b100SJamie Gritton {
29127455b100SJamie Gritton 
29137455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
2914c1f19219SJamie Gritton 	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
29157455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
29167455b100SJamie Gritton }
29177455b100SJamie Gritton 
29187455b100SJamie Gritton void
29197455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid)
29207455b100SJamie Gritton {
29217455b100SJamie Gritton 
29227455b100SJamie Gritton 	mtx_lock(&cred->cr_prison->pr_mtx);
29237455b100SJamie Gritton 	*hostid = cred->cr_prison->pr_hostid;
29247455b100SJamie Gritton 	mtx_unlock(&cred->cr_prison->pr_mtx);
29257455b100SJamie Gritton }
29267455b100SJamie Gritton 
2927eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE
2928eb79e1c7SBjoern A. Zeeb /*
2929eb79e1c7SBjoern A. Zeeb  * Determine whether the prison represented by cred owns
2930eb79e1c7SBjoern A. Zeeb  * its vnet rather than having it inherited.
2931eb79e1c7SBjoern A. Zeeb  *
2932eb79e1c7SBjoern A. Zeeb  * Returns 1 in case the prison owns the vnet, 0 otherwise.
2933eb79e1c7SBjoern A. Zeeb  */
2934eb79e1c7SBjoern A. Zeeb int
2935eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred)
2936eb79e1c7SBjoern A. Zeeb {
2937eb79e1c7SBjoern A. Zeeb 
2938eb79e1c7SBjoern A. Zeeb 	/*
2939eb79e1c7SBjoern A. Zeeb 	 * vnets cannot be added/removed after jail creation,
2940eb79e1c7SBjoern A. Zeeb 	 * so no need to lock here.
2941eb79e1c7SBjoern A. Zeeb 	 */
2942eb79e1c7SBjoern A. Zeeb 	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
2943eb79e1c7SBjoern A. Zeeb }
2944eb79e1c7SBjoern A. Zeeb #endif
2945eb79e1c7SBjoern A. Zeeb 
2946f08df373SRobert Watson /*
2947820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
2948820a0de9SPawel Jakub Dawidek  * status of a mount point.
2949820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
2950820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
2951820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
2952f08df373SRobert Watson  */
2953f08df373SRobert Watson int
2954820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
2955f08df373SRobert Watson {
2956820a0de9SPawel Jakub Dawidek 	struct prison *pr;
2957820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
2958820a0de9SPawel Jakub Dawidek 	size_t len;
2959f08df373SRobert Watson 
2960820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
29610304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
29620304c731SJamie Gritton 		return (0);
2963820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
2964820a0de9SPawel Jakub Dawidek 		return (0);
29650304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 2)
2966820a0de9SPawel Jakub Dawidek 		return (ENOENT);
2967820a0de9SPawel Jakub Dawidek 	/*
2968820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
2969820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
2970820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
2971820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
2972820a0de9SPawel Jakub Dawidek 	 */
2973820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
2974820a0de9SPawel Jakub Dawidek 		return (0);
2975820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
2976820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
2977820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
2978820a0de9SPawel Jakub Dawidek 		return (ENOENT);
2979820a0de9SPawel Jakub Dawidek 	/*
2980820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
2981820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
2982820a0de9SPawel Jakub Dawidek 	 */
2983820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
2984820a0de9SPawel Jakub Dawidek 		return (ENOENT);
2985f08df373SRobert Watson 	return (0);
2986f08df373SRobert Watson }
2987820a0de9SPawel Jakub Dawidek 
2988820a0de9SPawel Jakub Dawidek void
2989820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
2990820a0de9SPawel Jakub Dawidek {
2991820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
2992820a0de9SPawel Jakub Dawidek 	struct prison *pr;
2993820a0de9SPawel Jakub Dawidek 	size_t len;
2994820a0de9SPawel Jakub Dawidek 
2995820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
29960304c731SJamie Gritton 	if (pr->pr_enforce_statfs == 0)
29970304c731SJamie Gritton 		return;
2998820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
2999820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3000820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
3001820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
3002820a0de9SPawel Jakub Dawidek 		return;
3003820a0de9SPawel Jakub Dawidek 	}
3004820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
3005820a0de9SPawel Jakub Dawidek 		/*
3006820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
3007820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
3008820a0de9SPawel Jakub Dawidek 		 */
3009820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3010820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3011820a0de9SPawel Jakub Dawidek 		return;
3012820a0de9SPawel Jakub Dawidek 	}
3013820a0de9SPawel Jakub Dawidek 	/*
3014820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
3015820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
3016820a0de9SPawel Jakub Dawidek 	 */
3017820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
3018820a0de9SPawel Jakub Dawidek 		return;
3019820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
3020820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3021820a0de9SPawel Jakub Dawidek 	/*
3022820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
3023820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
3024820a0de9SPawel Jakub Dawidek 	 */
3025820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3026820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
3027820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
3028820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
3029820a0de9SPawel Jakub Dawidek 	} else {
3030820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3031820a0de9SPawel Jakub Dawidek 	}
3032f08df373SRobert Watson }
3033f08df373SRobert Watson 
3034800c9408SRobert Watson /*
3035800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
3036800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
3037800c9408SRobert Watson  */
3038800c9408SRobert Watson int
3039800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
3040800c9408SRobert Watson {
3041800c9408SRobert Watson 
3042800c9408SRobert Watson 	if (!jailed(cred))
3043800c9408SRobert Watson 		return (0);
3044800c9408SRobert Watson 
30456bb79563SJamie Gritton #ifdef VIMAGE
30466bb79563SJamie Gritton 	/*
30476bb79563SJamie Gritton 	 * Privileges specific to prisons with a virtual network stack.
30486bb79563SJamie Gritton 	 * There might be a duplicate entry here in case the privilege
30496bb79563SJamie Gritton 	 * is only granted conditionally in the legacy jail case.
30506bb79563SJamie Gritton 	 */
30516bb79563SJamie Gritton 	switch (priv) {
30526bb79563SJamie Gritton #ifdef notyet
30536bb79563SJamie Gritton 		/*
30546bb79563SJamie Gritton 		 * NFS-specific privileges.
30556bb79563SJamie Gritton 		 */
30566bb79563SJamie Gritton 	case PRIV_NFS_DAEMON:
30576bb79563SJamie Gritton 	case PRIV_NFS_LOCKD:
30586bb79563SJamie Gritton #endif
30596bb79563SJamie Gritton 		/*
30606bb79563SJamie Gritton 		 * Network stack privileges.
30616bb79563SJamie Gritton 		 */
30626bb79563SJamie Gritton 	case PRIV_NET_BRIDGE:
30636bb79563SJamie Gritton 	case PRIV_NET_GRE:
30646bb79563SJamie Gritton 	case PRIV_NET_BPF:
30656bb79563SJamie Gritton 	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
30666bb79563SJamie Gritton 	case PRIV_NET_ROUTE:
30676bb79563SJamie Gritton 	case PRIV_NET_TAP:
30686bb79563SJamie Gritton 	case PRIV_NET_SETIFMTU:
30696bb79563SJamie Gritton 	case PRIV_NET_SETIFFLAGS:
30706bb79563SJamie Gritton 	case PRIV_NET_SETIFCAP:
3071215940b3SXin LI 	case PRIV_NET_SETIFDESCR:
30726bb79563SJamie Gritton 	case PRIV_NET_SETIFNAME	:
30736bb79563SJamie Gritton 	case PRIV_NET_SETIFMETRIC:
30746bb79563SJamie Gritton 	case PRIV_NET_SETIFPHYS:
30756bb79563SJamie Gritton 	case PRIV_NET_SETIFMAC:
30766bb79563SJamie Gritton 	case PRIV_NET_ADDMULTI:
30776bb79563SJamie Gritton 	case PRIV_NET_DELMULTI:
30786bb79563SJamie Gritton 	case PRIV_NET_HWIOCTL:
30796bb79563SJamie Gritton 	case PRIV_NET_SETLLADDR:
30806bb79563SJamie Gritton 	case PRIV_NET_ADDIFGROUP:
30816bb79563SJamie Gritton 	case PRIV_NET_DELIFGROUP:
30826bb79563SJamie Gritton 	case PRIV_NET_IFCREATE:
30836bb79563SJamie Gritton 	case PRIV_NET_IFDESTROY:
30846bb79563SJamie Gritton 	case PRIV_NET_ADDIFADDR:
30856bb79563SJamie Gritton 	case PRIV_NET_DELIFADDR:
30866bb79563SJamie Gritton 	case PRIV_NET_LAGG:
30876bb79563SJamie Gritton 	case PRIV_NET_GIF:
30886bb79563SJamie Gritton 	case PRIV_NET_SETIFVNET:
308935fd7bc0SBjoern A. Zeeb 	case PRIV_NET_SETIFFIB:
30906bb79563SJamie Gritton 
30916bb79563SJamie Gritton 		/*
30926bb79563SJamie Gritton 		 * 802.11-related privileges.
30936bb79563SJamie Gritton 		 */
30946bb79563SJamie Gritton 	case PRIV_NET80211_GETKEY:
30956bb79563SJamie Gritton #ifdef notyet
30966bb79563SJamie Gritton 	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
30976bb79563SJamie Gritton #endif
30986bb79563SJamie Gritton 
30996bb79563SJamie Gritton #ifdef notyet
31006bb79563SJamie Gritton 		/*
31016bb79563SJamie Gritton 		 * ATM privileges.
31026bb79563SJamie Gritton 		 */
31036bb79563SJamie Gritton 	case PRIV_NETATM_CFG:
31046bb79563SJamie Gritton 	case PRIV_NETATM_ADD:
31056bb79563SJamie Gritton 	case PRIV_NETATM_DEL:
31066bb79563SJamie Gritton 	case PRIV_NETATM_SET:
31076bb79563SJamie Gritton 
31086bb79563SJamie Gritton 		/*
31096bb79563SJamie Gritton 		 * Bluetooth privileges.
31106bb79563SJamie Gritton 		 */
31116bb79563SJamie Gritton 	case PRIV_NETBLUETOOTH_RAW:
31126bb79563SJamie Gritton #endif
31136bb79563SJamie Gritton 
31146bb79563SJamie Gritton 		/*
31156bb79563SJamie Gritton 		 * Netgraph and netgraph module privileges.
31166bb79563SJamie Gritton 		 */
31176bb79563SJamie Gritton 	case PRIV_NETGRAPH_CONTROL:
31186bb79563SJamie Gritton #ifdef notyet
31196bb79563SJamie Gritton 	case PRIV_NETGRAPH_TTY:
31206bb79563SJamie Gritton #endif
31216bb79563SJamie Gritton 
31226bb79563SJamie Gritton 		/*
31236bb79563SJamie Gritton 		 * IPv4 and IPv6 privileges.
31246bb79563SJamie Gritton 		 */
31256bb79563SJamie Gritton 	case PRIV_NETINET_IPFW:
31266bb79563SJamie Gritton 	case PRIV_NETINET_DIVERT:
31276bb79563SJamie Gritton 	case PRIV_NETINET_PF:
31286bb79563SJamie Gritton 	case PRIV_NETINET_DUMMYNET:
31296bb79563SJamie Gritton 	case PRIV_NETINET_CARP:
31306bb79563SJamie Gritton 	case PRIV_NETINET_MROUTE:
31316bb79563SJamie Gritton 	case PRIV_NETINET_RAW:
31326bb79563SJamie Gritton 	case PRIV_NETINET_ADDRCTRL6:
31336bb79563SJamie Gritton 	case PRIV_NETINET_ND6:
31346bb79563SJamie Gritton 	case PRIV_NETINET_SCOPE6:
31356bb79563SJamie Gritton 	case PRIV_NETINET_ALIFETIME6:
31366bb79563SJamie Gritton 	case PRIV_NETINET_IPSEC:
31376bb79563SJamie Gritton 	case PRIV_NETINET_BINDANY:
31386bb79563SJamie Gritton 
31396bb79563SJamie Gritton #ifdef notyet
31406bb79563SJamie Gritton 		/*
31416bb79563SJamie Gritton 		 * NCP privileges.
31426bb79563SJamie Gritton 		 */
31436bb79563SJamie Gritton 	case PRIV_NETNCP:
31446bb79563SJamie Gritton 
31456bb79563SJamie Gritton 		/*
31466bb79563SJamie Gritton 		 * SMB privileges.
31476bb79563SJamie Gritton 		 */
31486bb79563SJamie Gritton 	case PRIV_NETSMB:
31496bb79563SJamie Gritton #endif
31506bb79563SJamie Gritton 
31516bb79563SJamie Gritton 	/*
31526bb79563SJamie Gritton 	 * No default: or deny here.
31536bb79563SJamie Gritton 	 * In case of no permit fall through to next switch().
31546bb79563SJamie Gritton 	 */
31556bb79563SJamie Gritton 		if (cred->cr_prison->pr_flags & PR_VNET)
31566bb79563SJamie Gritton 			return (0);
31576bb79563SJamie Gritton 	}
31586bb79563SJamie Gritton #endif /* VIMAGE */
31596bb79563SJamie Gritton 
3160800c9408SRobert Watson 	switch (priv) {
3161800c9408SRobert Watson 
3162800c9408SRobert Watson 		/*
3163800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
3164800c9408SRobert Watson 		 */
3165800c9408SRobert Watson 	case PRIV_KTRACE:
3166800c9408SRobert Watson 
3167c3c1b5e6SRobert Watson #if 0
3168800c9408SRobert Watson 		/*
3169800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
3170800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
3171800c9408SRobert Watson 		 * want to further refine the relationship between audit and
3172800c9408SRobert Watson 		 * jail.
3173800c9408SRobert Watson 		 */
3174800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
3175800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
3176800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
3177c3c1b5e6SRobert Watson #endif
3178800c9408SRobert Watson 
3179800c9408SRobert Watson 		/*
3180800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
3181800c9408SRobert Watson 		 * credentials in any way they see fit.
3182800c9408SRobert Watson 		 */
3183800c9408SRobert Watson 	case PRIV_CRED_SETUID:
3184800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
3185800c9408SRobert Watson 	case PRIV_CRED_SETGID:
3186800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
3187800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
3188800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
3189800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
3190800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
3191800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
3192800c9408SRobert Watson 
3193800c9408SRobert Watson 		/*
3194800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
3195800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
3196800c9408SRobert Watson 		 */
3197800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
3198800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
3199800c9408SRobert Watson 
3200800c9408SRobert Watson 		/*
3201800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
3202800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
3203800c9408SRobert Watson 		 */
3204800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
3205800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
3206800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
3207800c9408SRobert Watson 
3208800c9408SRobert Watson 		/*
3209800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
3210800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
3211800c9408SRobert Watson 		 */
3212800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
3213800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
3214800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
3215800c9408SRobert Watson 
3216800c9408SRobert Watson 		/*
3217800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
3218800c9408SRobert Watson 		 */
3219800c9408SRobert Watson 	case PRIV_IPC_READ:
3220800c9408SRobert Watson 	case PRIV_IPC_WRITE:
3221800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
3222800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
3223800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
3224800c9408SRobert Watson 
3225800c9408SRobert Watson 		/*
32260304c731SJamie Gritton 		 * Jail operations within a jail work on child jails.
32270304c731SJamie Gritton 		 */
32280304c731SJamie Gritton 	case PRIV_JAIL_ATTACH:
32290304c731SJamie Gritton 	case PRIV_JAIL_SET:
32300304c731SJamie Gritton 	case PRIV_JAIL_REMOVE:
32310304c731SJamie Gritton 
32320304c731SJamie Gritton 		/*
3233800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
3234800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
3235800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
3236800c9408SRobert Watson 		 */
3237800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
3238413628a7SBjoern A. Zeeb 	case PRIV_SCHED_CPUSET:
3239800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
3240800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
3241800c9408SRobert Watson 
3242800c9408SRobert Watson 		/*
3243800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
3244800c9408SRobert Watson 		 * writable.
3245800c9408SRobert Watson 		 */
3246800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
3247800c9408SRobert Watson 
3248800c9408SRobert Watson 		/*
3249800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
3250e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
3251e82d0201SRobert Watson 		 * configuration option.
3252800c9408SRobert Watson 		 */
325395b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
325495b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
3255800c9408SRobert Watson 
3256800c9408SRobert Watson 		/*
3257800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
3258800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
3259800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
3260800c9408SRobert Watson 		 * privileges.
3261800c9408SRobert Watson 		 */
3262800c9408SRobert Watson 	case PRIV_VFS_READ:
3263800c9408SRobert Watson 	case PRIV_VFS_WRITE:
3264800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
3265800c9408SRobert Watson 	case PRIV_VFS_EXEC:
3266800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
3267800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3268800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
3269800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
3270800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
3271bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
3272800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
3273800c9408SRobert Watson 	case PRIV_VFS_LINK:
3274800c9408SRobert Watson 	case PRIV_VFS_SETGID:
3275e41966dcSRobert Watson 	case PRIV_VFS_STAT:
3276800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
3277bb56d716SJamie Gritton 
3278bb56d716SJamie Gritton 		/*
3279bb56d716SJamie Gritton 		 * As in the non-jail case, non-root users are expected to be
3280bb56d716SJamie Gritton 		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3281bb56d716SJamie Gritton 		 * exists in the jail and they have permission to access it).
3282bb56d716SJamie Gritton 		 */
3283bb56d716SJamie Gritton 	case PRIV_KMEM_READ:
3284800c9408SRobert Watson 		return (0);
3285800c9408SRobert Watson 
3286800c9408SRobert Watson 		/*
3287800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
3288800c9408SRobert Watson 		 * setting system flags.
3289800c9408SRobert Watson 		 */
3290800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
32910304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3292800c9408SRobert Watson 			return (0);
3293800c9408SRobert Watson 		else
3294800c9408SRobert Watson 			return (EPERM);
3295800c9408SRobert Watson 
3296800c9408SRobert Watson 		/*
3297f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
3298f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
3299f3a8d2f9SPawel Jakub Dawidek 		 */
3300f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
3301f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
3302f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
330324b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
3304435d4667SMartin Matuska 		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3305435d4667SMartin Matuska 		    cred->cr_prison->pr_enforce_statfs < 2)
3306f3a8d2f9SPawel Jakub Dawidek 			return (0);
3307f3a8d2f9SPawel Jakub Dawidek 		else
3308f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
3309f3a8d2f9SPawel Jakub Dawidek 
3310f3a8d2f9SPawel Jakub Dawidek 		/*
3311e28f9b7dSAllan Jude 		 * Conditionally allow jailed root to bind reserved ports.
3312800c9408SRobert Watson 		 */
3313800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
3314e28f9b7dSAllan Jude 		if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3315e28f9b7dSAllan Jude 			return (0);
3316e28f9b7dSAllan Jude 		else
3317e28f9b7dSAllan Jude 			return (EPERM);
3318e28f9b7dSAllan Jude 
3319e28f9b7dSAllan Jude 		/*
3320e28f9b7dSAllan Jude 		 * Allow jailed root to reuse in-use ports.
3321e28f9b7dSAllan Jude 		 */
33224b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
3323800c9408SRobert Watson 		return (0);
3324800c9408SRobert Watson 
3325800c9408SRobert Watson 		/*
3326e3043798SPedro F. Giffuni 		 * Allow jailed root to set certain IPv4/6 (option) headers.
332779ba3952SBjoern A. Zeeb 		 */
332879ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
332979ba3952SBjoern A. Zeeb 		return (0);
333079ba3952SBjoern A. Zeeb 
333179ba3952SBjoern A. Zeeb 		/*
3332800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
3333800c9408SRobert Watson 		 */
3334800c9408SRobert Watson 	case PRIV_NETINET_RAW:
33350304c731SJamie Gritton 		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3336800c9408SRobert Watson 			return (0);
3337800c9408SRobert Watson 		else
3338800c9408SRobert Watson 			return (EPERM);
3339800c9408SRobert Watson 
3340800c9408SRobert Watson 		/*
3341800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
3342800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
3343800c9408SRobert Watson 		 * jail.
3344800c9408SRobert Watson 		 */
3345800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
3346800c9408SRobert Watson 		return (0);
3347800c9408SRobert Watson 
33482bfc50bcSEdward Tomasz Napierala 		/*
33492bfc50bcSEdward Tomasz Napierala 		 * Allow jailed root to set loginclass.
33502bfc50bcSEdward Tomasz Napierala 		 */
33512bfc50bcSEdward Tomasz Napierala 	case PRIV_PROC_SETLOGINCLASS:
33522bfc50bcSEdward Tomasz Napierala 		return (0);
33532bfc50bcSEdward Tomasz Napierala 
3354800c9408SRobert Watson 	default:
3355800c9408SRobert Watson 		/*
3356800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
3357800c9408SRobert Watson 		 * includes almost all network privileges, many system
3358800c9408SRobert Watson 		 * configuration privileges.
3359800c9408SRobert Watson 		 */
3360800c9408SRobert Watson 		return (EPERM);
3361800c9408SRobert Watson 	}
3362800c9408SRobert Watson }
3363800c9408SRobert Watson 
33640304c731SJamie Gritton /*
33650304c731SJamie Gritton  * Return the part of pr2's name that is relative to pr1, or the whole name
33660304c731SJamie Gritton  * if it does not directly follow.
33670304c731SJamie Gritton  */
33680304c731SJamie Gritton 
33690304c731SJamie Gritton char *
33700304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2)
33710304c731SJamie Gritton {
33720304c731SJamie Gritton 	char *name;
33730304c731SJamie Gritton 
33740304c731SJamie Gritton 	/* Jails see themselves as "0" (if they see themselves at all). */
33750304c731SJamie Gritton 	if (pr1 == pr2)
33760304c731SJamie Gritton 		return "0";
33770304c731SJamie Gritton 	name = pr2->pr_name;
33780304c731SJamie Gritton 	if (prison_ischild(pr1, pr2)) {
33790304c731SJamie Gritton 		/*
33800304c731SJamie Gritton 		 * pr1 isn't locked (and allprison_lock may not be either)
33810304c731SJamie Gritton 		 * so its length can't be counted on.  But the number of dots
33820304c731SJamie Gritton 		 * can be counted on - and counted.
33830304c731SJamie Gritton 		 */
33840304c731SJamie Gritton 		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
33850304c731SJamie Gritton 			name = strchr(name, '.') + 1;
33860304c731SJamie Gritton 	}
33870304c731SJamie Gritton 	return (name);
33880304c731SJamie Gritton }
33890304c731SJamie Gritton 
33900304c731SJamie Gritton /*
33910304c731SJamie Gritton  * Return the part of pr2's path that is relative to pr1, or the whole path
33920304c731SJamie Gritton  * if it does not directly follow.
33930304c731SJamie Gritton  */
33940304c731SJamie Gritton static char *
33950304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2)
33960304c731SJamie Gritton {
33970304c731SJamie Gritton 	char *path1, *path2;
33980304c731SJamie Gritton 	int len1;
33990304c731SJamie Gritton 
34000304c731SJamie Gritton 	path1 = pr1->pr_path;
34010304c731SJamie Gritton 	path2 = pr2->pr_path;
34020304c731SJamie Gritton 	if (!strcmp(path1, "/"))
34030304c731SJamie Gritton 		return (path2);
34040304c731SJamie Gritton 	len1 = strlen(path1);
34050304c731SJamie Gritton 	if (strncmp(path1, path2, len1))
34060304c731SJamie Gritton 		return (path2);
34070304c731SJamie Gritton 	if (path2[len1] == '\0')
34080304c731SJamie Gritton 		return "/";
34090304c731SJamie Gritton 	if (path2[len1] == '/')
34100304c731SJamie Gritton 		return (path2 + len1);
34110304c731SJamie Gritton 	return (path2);
34120304c731SJamie Gritton }
34130304c731SJamie Gritton 
34140304c731SJamie Gritton 
34150304c731SJamie Gritton /*
34160304c731SJamie Gritton  * Jail-related sysctls.
34170304c731SJamie Gritton  */
34186472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
34190304c731SJamie Gritton     "Jails");
34200304c731SJamie Gritton 
3421fd7a8150SMike Barcroft static int
3422fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3423fd7a8150SMike Barcroft {
3424b38ff370SJamie Gritton 	struct xprison *xp;
34250304c731SJamie Gritton 	struct prison *pr, *cpr;
3426b38ff370SJamie Gritton #ifdef INET
3427b38ff370SJamie Gritton 	struct in_addr *ip4 = NULL;
3428b38ff370SJamie Gritton 	int ip4s = 0;
3429b38ff370SJamie Gritton #endif
3430b38ff370SJamie Gritton #ifdef INET6
34313beefaedSColin Percival 	struct in6_addr *ip6 = NULL;
3432b38ff370SJamie Gritton 	int ip6s = 0;
3433b38ff370SJamie Gritton #endif
34340304c731SJamie Gritton 	int descend, error;
3435fd7a8150SMike Barcroft 
3436b38ff370SJamie Gritton 	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
34370304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
3438b38ff370SJamie Gritton 	error = 0;
3439dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
34400304c731SJamie Gritton 	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
34410304c731SJamie Gritton #if defined(INET) || defined(INET6)
3442b38ff370SJamie Gritton  again:
34430304c731SJamie Gritton #endif
34440304c731SJamie Gritton 		mtx_lock(&cpr->pr_mtx);
3445413628a7SBjoern A. Zeeb #ifdef INET
34460304c731SJamie Gritton 		if (cpr->pr_ip4s > 0) {
34470304c731SJamie Gritton 			if (ip4s < cpr->pr_ip4s) {
34480304c731SJamie Gritton 				ip4s = cpr->pr_ip4s;
34490304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3450b38ff370SJamie Gritton 				ip4 = realloc(ip4, ip4s *
3451b38ff370SJamie Gritton 				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3452b38ff370SJamie Gritton 				goto again;
3453b38ff370SJamie Gritton 			}
34540304c731SJamie Gritton 			bcopy(cpr->pr_ip4, ip4,
34550304c731SJamie Gritton 			    cpr->pr_ip4s * sizeof(struct in_addr));
3456b38ff370SJamie Gritton 		}
3457413628a7SBjoern A. Zeeb #endif
3458413628a7SBjoern A. Zeeb #ifdef INET6
34590304c731SJamie Gritton 		if (cpr->pr_ip6s > 0) {
34600304c731SJamie Gritton 			if (ip6s < cpr->pr_ip6s) {
34610304c731SJamie Gritton 				ip6s = cpr->pr_ip6s;
34620304c731SJamie Gritton 				mtx_unlock(&cpr->pr_mtx);
3463b38ff370SJamie Gritton 				ip6 = realloc(ip6, ip6s *
3464b38ff370SJamie Gritton 				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3465b38ff370SJamie Gritton 				goto again;
3466413628a7SBjoern A. Zeeb 			}
34670304c731SJamie Gritton 			bcopy(cpr->pr_ip6, ip6,
34680304c731SJamie Gritton 			    cpr->pr_ip6s * sizeof(struct in6_addr));
3469b38ff370SJamie Gritton 		}
3470b38ff370SJamie Gritton #endif
34710304c731SJamie Gritton 		if (cpr->pr_ref == 0) {
34720304c731SJamie Gritton 			mtx_unlock(&cpr->pr_mtx);
3473b38ff370SJamie Gritton 			continue;
3474b38ff370SJamie Gritton 		}
3475b38ff370SJamie Gritton 		bzero(xp, sizeof(*xp));
3476fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
34770304c731SJamie Gritton 		xp->pr_id = cpr->pr_id;
34780304c731SJamie Gritton 		xp->pr_state = cpr->pr_uref > 0
3479b38ff370SJamie Gritton 		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
34800304c731SJamie Gritton 		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3481c1f19219SJamie Gritton 		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
34820304c731SJamie Gritton 		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3483413628a7SBjoern A. Zeeb #ifdef INET
34840304c731SJamie Gritton 		xp->pr_ip4s = cpr->pr_ip4s;
3485413628a7SBjoern A. Zeeb #endif
3486413628a7SBjoern A. Zeeb #ifdef INET6
34870304c731SJamie Gritton 		xp->pr_ip6s = cpr->pr_ip6s;
3488413628a7SBjoern A. Zeeb #endif
34890304c731SJamie Gritton 		mtx_unlock(&cpr->pr_mtx);
3490b38ff370SJamie Gritton 		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3491b38ff370SJamie Gritton 		if (error)
3492b38ff370SJamie Gritton 			break;
3493413628a7SBjoern A. Zeeb #ifdef INET
3494b38ff370SJamie Gritton 		if (xp->pr_ip4s > 0) {
3495b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip4,
3496b38ff370SJamie Gritton 			    xp->pr_ip4s * sizeof(struct in_addr));
3497b38ff370SJamie Gritton 			if (error)
3498b38ff370SJamie Gritton 				break;
3499413628a7SBjoern A. Zeeb 		}
3500413628a7SBjoern A. Zeeb #endif
3501413628a7SBjoern A. Zeeb #ifdef INET6
3502b38ff370SJamie Gritton 		if (xp->pr_ip6s > 0) {
3503b38ff370SJamie Gritton 			error = SYSCTL_OUT(req, ip6,
3504b38ff370SJamie Gritton 			    xp->pr_ip6s * sizeof(struct in6_addr));
3505b38ff370SJamie Gritton 			if (error)
3506b38ff370SJamie Gritton 				break;
3507413628a7SBjoern A. Zeeb 		}
3508413628a7SBjoern A. Zeeb #endif
3509fd7a8150SMike Barcroft 	}
3510dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
3511b38ff370SJamie Gritton 	free(xp, M_TEMP);
3512b38ff370SJamie Gritton #ifdef INET
3513b38ff370SJamie Gritton 	free(ip4, M_TEMP);
3514b38ff370SJamie Gritton #endif
3515b38ff370SJamie Gritton #ifdef INET6
3516b38ff370SJamie Gritton 	free(ip6, M_TEMP);
3517b38ff370SJamie Gritton #endif
3518fd7a8150SMike Barcroft 	return (error);
3519fd7a8150SMike Barcroft }
3520fd7a8150SMike Barcroft 
3521f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list,
3522f3b86a5fSEd Schouten     CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3523f3b86a5fSEd Schouten     sysctl_jail_list, "S", "List of active jails");
3524461167c2SPawel Jakub Dawidek 
3525461167c2SPawel Jakub Dawidek static int
3526461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3527461167c2SPawel Jakub Dawidek {
3528461167c2SPawel Jakub Dawidek 	int error, injail;
3529461167c2SPawel Jakub Dawidek 
3530461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
3531461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3532461167c2SPawel Jakub Dawidek 
3533461167c2SPawel Jakub Dawidek 	return (error);
3534461167c2SPawel Jakub Dawidek }
35350304c731SJamie Gritton 
3536f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3537f3b86a5fSEd Schouten     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3538f3b86a5fSEd Schouten     sysctl_jail_jailed, "I", "Process in jail?");
3539413628a7SBjoern A. Zeeb 
3540761d2bb5SJamie Gritton static int
3541761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3542761d2bb5SJamie Gritton {
3543761d2bb5SJamie Gritton 	int error, havevnet;
3544761d2bb5SJamie Gritton #ifdef VIMAGE
3545761d2bb5SJamie Gritton 	struct ucred *cred = req->td->td_ucred;
3546761d2bb5SJamie Gritton 
3547761d2bb5SJamie Gritton 	havevnet = jailed(cred) && prison_owns_vnet(cred);
3548761d2bb5SJamie Gritton #else
3549761d2bb5SJamie Gritton 	havevnet = 0;
3550761d2bb5SJamie Gritton #endif
3551761d2bb5SJamie Gritton 	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3552761d2bb5SJamie Gritton 
3553761d2bb5SJamie Gritton 	return (error);
3554761d2bb5SJamie Gritton }
3555761d2bb5SJamie Gritton 
3556761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3557761d2bb5SJamie Gritton     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3558761d2bb5SJamie Gritton     sysctl_jail_vnet, "I", "Jail owns VNET?");
3559761d2bb5SJamie Gritton 
35600304c731SJamie Gritton #if defined(INET) || defined(INET6)
3561e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
35620304c731SJamie Gritton     &jail_max_af_ips, 0,
3563ee8d6bd3SJamie Gritton     "Number of IP addresses a jail may have at most per address family (deprecated)");
35640304c731SJamie Gritton #endif
35650304c731SJamie Gritton 
35660304c731SJamie Gritton /*
3567e3043798SPedro F. Giffuni  * Default parameters for jail(2) compatibility.  For historical reasons,
35680304c731SJamie Gritton  * the sysctl names have varying similarity to the parameter names.  Prisons
35690304c731SJamie Gritton  * just see their own parameters, and can't change them.
35700304c731SJamie Gritton  */
35710304c731SJamie Gritton static int
35720304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
35730304c731SJamie Gritton {
35740304c731SJamie Gritton 	struct prison *pr;
35750304c731SJamie Gritton 	int allow, error, i;
35760304c731SJamie Gritton 
35770304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
35780304c731SJamie Gritton 	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
35790304c731SJamie Gritton 
35800304c731SJamie Gritton 	/* Get the current flag value, and convert it to a boolean. */
35810304c731SJamie Gritton 	i = (allow & arg2) ? 1 : 0;
35820304c731SJamie Gritton 	if (arg1 != NULL)
35830304c731SJamie Gritton 		i = !i;
35840304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &i, 0, req);
35850304c731SJamie Gritton 	if (error || !req->newptr)
35860304c731SJamie Gritton 		return (error);
35870304c731SJamie Gritton 	i = i ? arg2 : 0;
35880304c731SJamie Gritton 	if (arg1 != NULL)
35890304c731SJamie Gritton 		i ^= arg2;
35900304c731SJamie Gritton 	/*
35910304c731SJamie Gritton 	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
35920304c731SJamie Gritton 	 * for writing.
35930304c731SJamie Gritton 	 */
35940304c731SJamie Gritton 	mtx_lock(&prison0.pr_mtx);
35950304c731SJamie Gritton 	jail_default_allow = (jail_default_allow & ~arg2) | i;
35960304c731SJamie Gritton 	mtx_unlock(&prison0.pr_mtx);
35970304c731SJamie Gritton 	return (0);
35980304c731SJamie Gritton }
35990304c731SJamie Gritton 
36000304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
36010304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36020304c731SJamie Gritton     NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3603ee8d6bd3SJamie Gritton     "Processes in jail can set their hostnames (deprecated)");
36040304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
36050304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36060304c731SJamie Gritton     (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3607ee8d6bd3SJamie Gritton     "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
36080304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
36090304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36100304c731SJamie Gritton     NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3611ee8d6bd3SJamie Gritton     "Processes in jail can use System V IPC primitives (deprecated)");
36120304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
36130304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36140304c731SJamie Gritton     NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3615ee8d6bd3SJamie Gritton     "Prison root can create raw sockets (deprecated)");
36160304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
36170304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36180304c731SJamie Gritton     NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3619ee8d6bd3SJamie Gritton     "Processes in jail can alter system file flags (deprecated)");
36200304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
36210304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36220304c731SJamie Gritton     NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3623ee8d6bd3SJamie Gritton     "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
3624bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
3625bf3db8aaSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3626bf3db8aaSMartin Matuska     NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
3627ee8d6bd3SJamie Gritton     "Processes in jail can mount the devfs file system (deprecated)");
3628464aad14SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed,
3629464aad14SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3630464aad14SJamie Gritton     NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I",
3631ee8d6bd3SJamie Gritton     "Processes in jail can mount the fdescfs file system (deprecated)");
3632bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
3633bf3db8aaSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3634bf3db8aaSMartin Matuska     NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
3635ee8d6bd3SJamie Gritton     "Processes in jail can mount the nullfs file system (deprecated)");
363641c0675eSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
363741c0675eSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
363841c0675eSMartin Matuska     NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
3639ee8d6bd3SJamie Gritton     "Processes in jail can mount the procfs file system (deprecated)");
3640f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linprocfs_allowed,
3641f19e47d6SMarcelo Araujo     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3642f19e47d6SMarcelo Araujo     NULL, PR_ALLOW_MOUNT_LINPROCFS, sysctl_jail_default_allow, "I",
3643ee8d6bd3SJamie Gritton     "Processes in jail can mount the linprocfs file system (deprecated)");
3644f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linsysfs_allowed,
3645f19e47d6SMarcelo Araujo     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3646f19e47d6SMarcelo Araujo     NULL, PR_ALLOW_MOUNT_LINSYSFS, sysctl_jail_default_allow, "I",
3647ee8d6bd3SJamie Gritton     "Processes in jail can mount the linsysfs file system (deprecated)");
36482454886eSXin LI SYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed,
36492454886eSXin LI     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36502454886eSXin LI     NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I",
3651ee8d6bd3SJamie Gritton     "Processes in jail can mount the tmpfs file system (deprecated)");
3652e7af90abSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
3653e7af90abSMartin Matuska     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3654e7af90abSMartin Matuska     NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
3655ee8d6bd3SJamie Gritton     "Processes in jail can mount the zfs file system (deprecated)");
36560304c731SJamie Gritton 
36570304c731SJamie Gritton static int
36580304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
36590304c731SJamie Gritton {
36600304c731SJamie Gritton 	struct prison *pr;
36610304c731SJamie Gritton 	int level, error;
36620304c731SJamie Gritton 
36630304c731SJamie Gritton 	pr = req->td->td_ucred->cr_prison;
36640304c731SJamie Gritton 	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
36650304c731SJamie Gritton 	error = sysctl_handle_int(oidp, &level, 0, req);
36660304c731SJamie Gritton 	if (error || !req->newptr)
36670304c731SJamie Gritton 		return (error);
36680304c731SJamie Gritton 	*(int *)arg1 = level;
36690304c731SJamie Gritton 	return (0);
36700304c731SJamie Gritton }
36710304c731SJamie Gritton 
36720304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
36730304c731SJamie Gritton     CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
36740304c731SJamie Gritton     &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
36750304c731SJamie Gritton     sysctl_jail_default_level, "I",
3676ee8d6bd3SJamie Gritton     "Processes in jail cannot see all mounted file systems (deprecated)");
36770304c731SJamie Gritton 
36780cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
36790cc207a6SMartin Matuska     CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
36800cc207a6SMartin Matuska     &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
36810cc207a6SMartin Matuska     sysctl_jail_default_level, "I",
3682ee8d6bd3SJamie Gritton     "Ruleset for the devfs filesystem in jail (deprecated)");
36830cc207a6SMartin Matuska 
36840304c731SJamie Gritton /*
36850304c731SJamie Gritton  * Nodes to describe jail parameters.  Maximum length of string parameters
36860304c731SJamie Gritton  * is returned in the string itself, and the other parameters exist merely
36870304c731SJamie Gritton  * to make themselves and their types known.
36880304c731SJamie Gritton  */
36890304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
36900304c731SJamie Gritton     "Jail parameters");
36910304c731SJamie Gritton 
36920304c731SJamie Gritton int
36930304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS)
36940304c731SJamie Gritton {
36950304c731SJamie Gritton 	int i;
36960304c731SJamie Gritton 	long l;
36970304c731SJamie Gritton 	size_t s;
36980304c731SJamie Gritton 	char numbuf[12];
36990304c731SJamie Gritton 
37000304c731SJamie Gritton 	switch (oidp->oid_kind & CTLTYPE)
37010304c731SJamie Gritton 	{
37020304c731SJamie Gritton 	case CTLTYPE_LONG:
37030304c731SJamie Gritton 	case CTLTYPE_ULONG:
37040304c731SJamie Gritton 		l = 0;
37050304c731SJamie Gritton #ifdef SCTL_MASK32
37060304c731SJamie Gritton 		if (!(req->flags & SCTL_MASK32))
37070304c731SJamie Gritton #endif
37080304c731SJamie Gritton 			return (SYSCTL_OUT(req, &l, sizeof(l)));
37090304c731SJamie Gritton 	case CTLTYPE_INT:
37100304c731SJamie Gritton 	case CTLTYPE_UINT:
37110304c731SJamie Gritton 		i = 0;
37120304c731SJamie Gritton 		return (SYSCTL_OUT(req, &i, sizeof(i)));
37130304c731SJamie Gritton 	case CTLTYPE_STRING:
3714e4cd31ddSJeff Roberson 		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
37150304c731SJamie Gritton 		return
37160304c731SJamie Gritton 		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
37170304c731SJamie Gritton 	case CTLTYPE_STRUCT:
37180304c731SJamie Gritton 		s = (size_t)arg2;
37190304c731SJamie Gritton 		return (SYSCTL_OUT(req, &s, sizeof(s)));
37200304c731SJamie Gritton 	}
37210304c731SJamie Gritton 	return (0);
37220304c731SJamie Gritton }
37230304c731SJamie Gritton 
3724b96bd95bSIan Lepore /*
3725b96bd95bSIan Lepore  * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
3726b96bd95bSIan Lepore  * jail creation time but cannot be changed in an existing jail.
3727b96bd95bSIan Lepore  */
37280304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
37290304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
37300304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
37310304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
37320304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
37330304c731SJamie Gritton     "I", "Jail secure level");
3734b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
3735b96bd95bSIan Lepore     "Jail value for kern.osreldate and uname -K");
3736b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
3737b96bd95bSIan Lepore     "Jail value for kern.osrelease and uname -r");
37380304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
37390304c731SJamie Gritton     "I", "Jail cannot see all mounted file systems");
37400cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
37410cc207a6SMartin Matuska     "I", "Ruleset for in-jail devfs mounts");
37420304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
37430304c731SJamie Gritton     "B", "Jail persistence");
3744679e1390SJamie Gritton #ifdef VIMAGE
3745679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
37467cbf7213SJamie Gritton     "E,jailsys", "Virtual network stack");
3747679e1390SJamie Gritton #endif
37480304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
37490304c731SJamie Gritton     "B", "Jail is in the process of shutting down");
37500304c731SJamie Gritton 
3751b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
3752b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
3753b97457e2SJamie Gritton     "I", "Current number of child jails");
3754b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
3755b97457e2SJamie Gritton     "I", "Maximum number of child jails");
3756b97457e2SJamie Gritton 
37577cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
37580304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
37590304c731SJamie Gritton     "Jail hostname");
376076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
376176ca6f88SJamie Gritton     "Jail NIS domainname");
376276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
376376ca6f88SJamie Gritton     "Jail host UUID");
376476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
376576ca6f88SJamie Gritton     "LU", "Jail host ID");
37660304c731SJamie Gritton 
37670304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
37680304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
37690304c731SJamie Gritton 
37700304c731SJamie Gritton #ifdef INET
37712b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
37722b0d6f81SJamie Gritton     "Jail IPv4 address virtualization");
37730304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
37740304c731SJamie Gritton     "S,in_addr,a", "Jail IPv4 addresses");
3775592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
3776592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv4 source address selection rather than the "
3777592bcae8SBjoern A. Zeeb     "primary jail IPv4 address.");
37780304c731SJamie Gritton #endif
37790304c731SJamie Gritton #ifdef INET6
37802b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
37812b0d6f81SJamie Gritton     "Jail IPv6 address virtualization");
37820304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
37830304c731SJamie Gritton     "S,in6_addr,a", "Jail IPv6 addresses");
3784592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
3785592bcae8SBjoern A. Zeeb     "B", "Do (not) use IPv6 source address selection rather than the "
3786592bcae8SBjoern A. Zeeb     "primary jail IPv6 address.");
37870304c731SJamie Gritton #endif
37880304c731SJamie Gritton 
37890304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
37900304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
37910304c731SJamie Gritton     "B", "Jail may set hostname");
37920304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
37930304c731SJamie Gritton     "B", "Jail may use SYSV IPC");
37940304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
37950304c731SJamie Gritton     "B", "Jail may create raw sockets");
37960304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
37970304c731SJamie Gritton     "B", "Jail may alter system file flags");
37980304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
37990304c731SJamie Gritton     "B", "Jail may set file quotas");
38000304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
38010304c731SJamie Gritton     "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
3802e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
3803e28f9b7dSAllan Jude     "B", "Jail may bind sockets to reserved ports");
38040304c731SJamie Gritton 
3805bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
3806bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
3807bf3db8aaSMartin Matuska     "B", "Jail may mount/unmount jail-friendly file systems in general");
3808bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
3809e7af90abSMartin Matuska     "B", "Jail may mount the devfs file system");
3810464aad14SJamie Gritton SYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW,
3811464aad14SJamie Gritton     "B", "Jail may mount the fdescfs file system");
3812bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
3813e7af90abSMartin Matuska     "B", "Jail may mount the nullfs file system");
381441c0675eSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
381541c0675eSMartin Matuska     "B", "Jail may mount the procfs file system");
3816f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linprocfs, CTLTYPE_INT | CTLFLAG_RW,
3817f19e47d6SMarcelo Araujo     "B", "Jail may mount the linprocfs file system");
3818f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linsysfs, CTLTYPE_INT | CTLFLAG_RW,
3819f19e47d6SMarcelo Araujo     "B", "Jail may mount the linsysfs file system");
38202454886eSXin LI SYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW,
38212454886eSXin LI     "B", "Jail may mount the tmpfs file system");
3822e7af90abSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
3823e7af90abSMartin Matuska     "B", "Jail may mount the zfs file system");
3824bf3db8aaSMartin Matuska 
38254b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
3826097055e2SEdward Tomasz Napierala void
3827097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct,
382815db3c07SEdward Tomasz Napierala     void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
382915db3c07SEdward Tomasz Napierala     void *arg2, void *arg3)
3830097055e2SEdward Tomasz Napierala {
3831a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3832097055e2SEdward Tomasz Napierala 
38334b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
38344b5c9cf6SEdward Tomasz Napierala 
3835097055e2SEdward Tomasz Napierala 	sx_slock(&allprison_lock);
383615db3c07SEdward Tomasz Napierala 	if (pre != NULL)
383715db3c07SEdward Tomasz Napierala 		(pre)();
3838a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next)
3839a7ad07bfSEdward Tomasz Napierala 		(callback)(prr->prr_racct, arg2, arg3);
384015db3c07SEdward Tomasz Napierala 	if (post != NULL)
384115db3c07SEdward Tomasz Napierala 		(post)();
3842097055e2SEdward Tomasz Napierala 	sx_sunlock(&allprison_lock);
3843097055e2SEdward Tomasz Napierala }
38440304c731SJamie Gritton 
3845a7ad07bfSEdward Tomasz Napierala static struct prison_racct *
3846a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name)
3847a7ad07bfSEdward Tomasz Napierala {
3848a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3849a7ad07bfSEdward Tomasz Napierala 
38504b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3851a7ad07bfSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
3852a7ad07bfSEdward Tomasz Napierala 
3853a7ad07bfSEdward Tomasz Napierala 	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
3854a7ad07bfSEdward Tomasz Napierala 		return (NULL);
3855a7ad07bfSEdward Tomasz Napierala 
3856a7ad07bfSEdward Tomasz Napierala 	LIST_FOREACH(prr, &allprison_racct, prr_next) {
3857a7ad07bfSEdward Tomasz Napierala 		if (strcmp(name, prr->prr_name) != 0)
3858a7ad07bfSEdward Tomasz Napierala 			continue;
3859a7ad07bfSEdward Tomasz Napierala 
3860a7ad07bfSEdward Tomasz Napierala 		/* Found prison_racct with a matching name? */
3861a7ad07bfSEdward Tomasz Napierala 		prison_racct_hold(prr);
3862a7ad07bfSEdward Tomasz Napierala 		return (prr);
3863a7ad07bfSEdward Tomasz Napierala 	}
3864a7ad07bfSEdward Tomasz Napierala 
3865a7ad07bfSEdward Tomasz Napierala 	/* Add new prison_racct. */
3866a7ad07bfSEdward Tomasz Napierala 	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
3867a7ad07bfSEdward Tomasz Napierala 	racct_create(&prr->prr_racct);
3868a7ad07bfSEdward Tomasz Napierala 
3869a7ad07bfSEdward Tomasz Napierala 	strcpy(prr->prr_name, name);
3870a7ad07bfSEdward Tomasz Napierala 	refcount_init(&prr->prr_refcount, 1);
3871a7ad07bfSEdward Tomasz Napierala 	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
3872a7ad07bfSEdward Tomasz Napierala 
3873a7ad07bfSEdward Tomasz Napierala 	return (prr);
3874a7ad07bfSEdward Tomasz Napierala }
3875a7ad07bfSEdward Tomasz Napierala 
3876a7ad07bfSEdward Tomasz Napierala struct prison_racct *
3877a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name)
3878a7ad07bfSEdward Tomasz Napierala {
3879a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3880a7ad07bfSEdward Tomasz Napierala 
38814b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
38824b5c9cf6SEdward Tomasz Napierala 
3883a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
3884a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(name);
3885a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
3886a7ad07bfSEdward Tomasz Napierala 	return (prr);
3887a7ad07bfSEdward Tomasz Napierala }
3888a7ad07bfSEdward Tomasz Napierala 
3889a7ad07bfSEdward Tomasz Napierala void
3890a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr)
3891a7ad07bfSEdward Tomasz Napierala {
3892a7ad07bfSEdward Tomasz Napierala 
38934b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
38944b5c9cf6SEdward Tomasz Napierala 
3895a7ad07bfSEdward Tomasz Napierala 	refcount_acquire(&prr->prr_refcount);
3896a7ad07bfSEdward Tomasz Napierala }
3897a7ad07bfSEdward Tomasz Napierala 
3898c34bbd2aSEdward Tomasz Napierala static void
3899c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr)
3900c34bbd2aSEdward Tomasz Napierala {
3901c34bbd2aSEdward Tomasz Napierala 
39024b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3903c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
3904c34bbd2aSEdward Tomasz Napierala 
3905c34bbd2aSEdward Tomasz Napierala 	if (refcount_release(&prr->prr_refcount)) {
3906c34bbd2aSEdward Tomasz Napierala 		racct_destroy(&prr->prr_racct);
3907c34bbd2aSEdward Tomasz Napierala 		LIST_REMOVE(prr, prr_next);
3908c34bbd2aSEdward Tomasz Napierala 		free(prr, M_PRISON_RACCT);
3909c34bbd2aSEdward Tomasz Napierala 	}
3910c34bbd2aSEdward Tomasz Napierala }
3911c34bbd2aSEdward Tomasz Napierala 
3912a7ad07bfSEdward Tomasz Napierala void
3913a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr)
3914a7ad07bfSEdward Tomasz Napierala {
3915a7ad07bfSEdward Tomasz Napierala 	int old;
3916a7ad07bfSEdward Tomasz Napierala 
39174b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3918c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
3919c34bbd2aSEdward Tomasz Napierala 
3920a7ad07bfSEdward Tomasz Napierala 	old = prr->prr_refcount;
3921a7ad07bfSEdward Tomasz Napierala 	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
3922a7ad07bfSEdward Tomasz Napierala 		return;
3923a7ad07bfSEdward Tomasz Napierala 
3924a7ad07bfSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
3925c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(prr);
3926a7ad07bfSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
3927a7ad07bfSEdward Tomasz Napierala }
3928a7ad07bfSEdward Tomasz Napierala 
3929a7ad07bfSEdward Tomasz Napierala static void
3930a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr)
3931a7ad07bfSEdward Tomasz Napierala {
3932a7ad07bfSEdward Tomasz Napierala 	struct prison_racct *prr;
3933a7ad07bfSEdward Tomasz Napierala 
39344b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3935c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_XLOCKED);
3936c34bbd2aSEdward Tomasz Napierala 
3937a7ad07bfSEdward Tomasz Napierala 	prr = prison_racct_find_locked(pr->pr_name);
3938a7ad07bfSEdward Tomasz Napierala 	KASSERT(prr != NULL, ("cannot find prison_racct"));
3939a7ad07bfSEdward Tomasz Napierala 
3940a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = prr;
3941a7ad07bfSEdward Tomasz Napierala }
3942a7ad07bfSEdward Tomasz Napierala 
3943c34bbd2aSEdward Tomasz Napierala /*
3944c34bbd2aSEdward Tomasz Napierala  * Handle jail renaming.  From the racct point of view, renaming means
3945c34bbd2aSEdward Tomasz Napierala  * moving from one prison_racct to another.
3946c34bbd2aSEdward Tomasz Napierala  */
3947c34bbd2aSEdward Tomasz Napierala static void
3948c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr)
3949c34bbd2aSEdward Tomasz Napierala {
3950c34bbd2aSEdward Tomasz Napierala 	struct proc *p;
3951c34bbd2aSEdward Tomasz Napierala 	struct ucred *cred;
3952c34bbd2aSEdward Tomasz Napierala 	struct prison_racct *oldprr;
3953c34bbd2aSEdward Tomasz Napierala 
39544b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
39554b5c9cf6SEdward Tomasz Napierala 
3956c34bbd2aSEdward Tomasz Napierala 	sx_slock(&allproc_lock);
3957c34bbd2aSEdward Tomasz Napierala 	sx_xlock(&allprison_lock);
3958c34bbd2aSEdward Tomasz Napierala 
3959e30345e7SEdward Tomasz Napierala 	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
3960e30345e7SEdward Tomasz Napierala 		sx_xunlock(&allprison_lock);
3961e30345e7SEdward Tomasz Napierala 		sx_sunlock(&allproc_lock);
3962c34bbd2aSEdward Tomasz Napierala 		return;
3963e30345e7SEdward Tomasz Napierala 	}
3964c34bbd2aSEdward Tomasz Napierala 
3965c34bbd2aSEdward Tomasz Napierala 	oldprr = pr->pr_prison_racct;
3966c34bbd2aSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
3967c34bbd2aSEdward Tomasz Napierala 
3968c34bbd2aSEdward Tomasz Napierala 	prison_racct_attach(pr);
3969c34bbd2aSEdward Tomasz Napierala 
3970c34bbd2aSEdward Tomasz Napierala 	/*
3971c34bbd2aSEdward Tomasz Napierala 	 * Move resource utilisation records.
3972c34bbd2aSEdward Tomasz Napierala 	 */
3973c34bbd2aSEdward Tomasz Napierala 	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
3974c34bbd2aSEdward Tomasz Napierala 
3975c34bbd2aSEdward Tomasz Napierala 	/*
3976c34bbd2aSEdward Tomasz Napierala 	 * Force rctl to reattach rules to processes.
3977c34bbd2aSEdward Tomasz Napierala 	 */
3978c34bbd2aSEdward Tomasz Napierala 	FOREACH_PROC_IN_SYSTEM(p) {
3979c34bbd2aSEdward Tomasz Napierala 		PROC_LOCK(p);
3980c34bbd2aSEdward Tomasz Napierala 		cred = crhold(p->p_ucred);
3981c34bbd2aSEdward Tomasz Napierala 		PROC_UNLOCK(p);
3982c34bbd2aSEdward Tomasz Napierala 		racct_proc_ucred_changed(p, cred, cred);
3983c34bbd2aSEdward Tomasz Napierala 		crfree(cred);
3984c34bbd2aSEdward Tomasz Napierala 	}
3985c34bbd2aSEdward Tomasz Napierala 
3986c34bbd2aSEdward Tomasz Napierala 	sx_sunlock(&allproc_lock);
3987c34bbd2aSEdward Tomasz Napierala 	prison_racct_free_locked(oldprr);
3988c34bbd2aSEdward Tomasz Napierala 	sx_xunlock(&allprison_lock);
3989c34bbd2aSEdward Tomasz Napierala }
3990c34bbd2aSEdward Tomasz Napierala 
3991a7ad07bfSEdward Tomasz Napierala static void
3992a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr)
3993a7ad07bfSEdward Tomasz Napierala {
3994c34bbd2aSEdward Tomasz Napierala 
39954b5c9cf6SEdward Tomasz Napierala 	ASSERT_RACCT_ENABLED();
3996c34bbd2aSEdward Tomasz Napierala 	sx_assert(&allprison_lock, SA_UNLOCKED);
3997c34bbd2aSEdward Tomasz Napierala 
3998af3c786cSMateusz Guzik 	if (pr->pr_prison_racct == NULL)
3999af3c786cSMateusz Guzik 		return;
4000a7ad07bfSEdward Tomasz Napierala 	prison_racct_free(pr->pr_prison_racct);
4001a7ad07bfSEdward Tomasz Napierala 	pr->pr_prison_racct = NULL;
4002a7ad07bfSEdward Tomasz Napierala }
4003a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */
4004a7ad07bfSEdward Tomasz Napierala 
4005413628a7SBjoern A. Zeeb #ifdef DDB
4006b38ff370SJamie Gritton 
4007b38ff370SJamie Gritton static void
4008b38ff370SJamie Gritton db_show_prison(struct prison *pr)
4009413628a7SBjoern A. Zeeb {
40100304c731SJamie Gritton 	int fi;
4011b38ff370SJamie Gritton #if defined(INET) || defined(INET6)
4012b38ff370SJamie Gritton 	int ii;
4013413628a7SBjoern A. Zeeb #endif
40147cbf7213SJamie Gritton 	unsigned jsf;
40158144690aSEric van Gyzen #ifdef INET
40168144690aSEric van Gyzen 	char ip4buf[INET_ADDRSTRLEN];
40178144690aSEric van Gyzen #endif
4018413628a7SBjoern A. Zeeb #ifdef INET6
4019413628a7SBjoern A. Zeeb 	char ip6buf[INET6_ADDRSTRLEN];
4020413628a7SBjoern A. Zeeb #endif
4021413628a7SBjoern A. Zeeb 
4022b38ff370SJamie Gritton 	db_printf("prison %p:\n", pr);
4023b38ff370SJamie Gritton 	db_printf(" jid             = %d\n", pr->pr_id);
4024b38ff370SJamie Gritton 	db_printf(" name            = %s\n", pr->pr_name);
40250304c731SJamie Gritton 	db_printf(" parent          = %p\n", pr->pr_parent);
4026b38ff370SJamie Gritton 	db_printf(" ref             = %d\n", pr->pr_ref);
4027b38ff370SJamie Gritton 	db_printf(" uref            = %d\n", pr->pr_uref);
4028b38ff370SJamie Gritton 	db_printf(" path            = %s\n", pr->pr_path);
4029b38ff370SJamie Gritton 	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4030b38ff370SJamie Gritton 	    ? pr->pr_cpuset->cs_id : -1);
4031679e1390SJamie Gritton #ifdef VIMAGE
4032679e1390SJamie Gritton 	db_printf(" vnet            = %p\n", pr->pr_vnet);
4033679e1390SJamie Gritton #endif
4034b38ff370SJamie Gritton 	db_printf(" root            = %p\n", pr->pr_root);
4035b38ff370SJamie Gritton 	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
40360cc207a6SMartin Matuska 	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4037fe0518e9SBjoern A. Zeeb 	db_printf(" children.max    = %d\n", pr->pr_childmax);
4038fe0518e9SBjoern A. Zeeb 	db_printf(" children.cur    = %d\n", pr->pr_childcount);
40390304c731SJamie Gritton 	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
40400304c731SJamie Gritton 	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4041fe0518e9SBjoern A. Zeeb 	db_printf(" flags           = 0x%x", pr->pr_flags);
404202abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_names); fi++)
40430304c731SJamie Gritton 		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
40440304c731SJamie Gritton 			db_printf(" %s", pr_flag_names[fi]);
404502abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_flag_jailsys); fi++) {
40467cbf7213SJamie Gritton 		jsf = pr->pr_flags &
40477cbf7213SJamie Gritton 		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
40487cbf7213SJamie Gritton 		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
40497cbf7213SJamie Gritton 		    pr_flag_jailsys[fi].disable &&
40507cbf7213SJamie Gritton 		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
40517cbf7213SJamie Gritton 		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
40527cbf7213SJamie Gritton 		    : "inherit");
40537cbf7213SJamie Gritton 	}
4054fe0518e9SBjoern A. Zeeb 	db_printf(" allow           = 0x%x", pr->pr_allow);
405502abd400SPedro F. Giffuni 	for (fi = 0; fi < nitems(pr_allow_names); fi++)
40560304c731SJamie Gritton 		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
40570304c731SJamie Gritton 			db_printf(" %s", pr_allow_names[fi]);
4058b38ff370SJamie Gritton 	db_printf("\n");
40590304c731SJamie Gritton 	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4060c1f19219SJamie Gritton 	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4061c1f19219SJamie Gritton 	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4062c1f19219SJamie Gritton 	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
406376ca6f88SJamie Gritton 	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4064413628a7SBjoern A. Zeeb #ifdef INET
4065b38ff370SJamie Gritton 	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4066b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip4s; ii++)
4067b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4068fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip4.addr        =" : "                 ",
40698144690aSEric van Gyzen 		    inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4070413628a7SBjoern A. Zeeb #endif
4071413628a7SBjoern A. Zeeb #ifdef INET6
4072b38ff370SJamie Gritton 	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4073b38ff370SJamie Gritton 	for (ii = 0; ii < pr->pr_ip6s; ii++)
4074b38ff370SJamie Gritton 		db_printf(" %s %s\n",
4075fe0518e9SBjoern A. Zeeb 		    ii == 0 ? "ip6.addr        =" : "                 ",
4076b38ff370SJamie Gritton 		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4077b38ff370SJamie Gritton #endif
4078b38ff370SJamie Gritton }
4079b38ff370SJamie Gritton 
4080b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command)
4081b38ff370SJamie Gritton {
4082b38ff370SJamie Gritton 	struct prison *pr;
4083b38ff370SJamie Gritton 
4084b38ff370SJamie Gritton 	if (!have_addr) {
40850304c731SJamie Gritton 		/*
40860304c731SJamie Gritton 		 * Show all prisons in the list, and prison0 which is not
40870304c731SJamie Gritton 		 * listed.
40880304c731SJamie Gritton 		 */
40890304c731SJamie Gritton 		db_show_prison(&prison0);
40900304c731SJamie Gritton 		if (!db_pager_quit) {
4091b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list) {
4092b38ff370SJamie Gritton 				db_show_prison(pr);
4093413628a7SBjoern A. Zeeb 				if (db_pager_quit)
4094413628a7SBjoern A. Zeeb 					break;
4095413628a7SBjoern A. Zeeb 			}
40960304c731SJamie Gritton 		}
4097b38ff370SJamie Gritton 		return;
4098413628a7SBjoern A. Zeeb 	}
4099b38ff370SJamie Gritton 
41000304c731SJamie Gritton 	if (addr == 0)
41010304c731SJamie Gritton 		pr = &prison0;
41020304c731SJamie Gritton 	else {
4103b38ff370SJamie Gritton 		/* Look for a prison with the ID and with references. */
4104b38ff370SJamie Gritton 		TAILQ_FOREACH(pr, &allprison, pr_list)
4105b38ff370SJamie Gritton 			if (pr->pr_id == addr && pr->pr_ref > 0)
4106b38ff370SJamie Gritton 				break;
4107b38ff370SJamie Gritton 		if (pr == NULL)
4108b38ff370SJamie Gritton 			/* Look again, without requiring a reference. */
4109b38ff370SJamie Gritton 			TAILQ_FOREACH(pr, &allprison, pr_list)
4110b38ff370SJamie Gritton 				if (pr->pr_id == addr)
4111b38ff370SJamie Gritton 					break;
4112b38ff370SJamie Gritton 		if (pr == NULL)
4113b38ff370SJamie Gritton 			/* Assume address points to a valid prison. */
4114b38ff370SJamie Gritton 			pr = (struct prison *)addr;
41150304c731SJamie Gritton 	}
4116b38ff370SJamie Gritton 	db_show_prison(pr);
4117b38ff370SJamie Gritton }
4118b38ff370SJamie Gritton 
4119413628a7SBjoern A. Zeeb #endif /* DDB */
4120