1139804Simp/*-
2185435Sbz * Copyright (c) 1999 Poul-Henning Kamp.
3185435Sbz * Copyright (c) 2008 Bjoern A. Zeeb.
4191673Sjamie * Copyright (c) 2009 James Gritton.
5185435Sbz * All rights reserved.
6190466Sjamie *
7185404Sbz * Redistribution and use in source and binary forms, with or without
8185404Sbz * modification, are permitted provided that the following conditions
9185404Sbz * are met:
10185404Sbz * 1. Redistributions of source code must retain the above copyright
11185404Sbz *    notice, this list of conditions and the following disclaimer.
12185404Sbz * 2. Redistributions in binary form must reproduce the above copyright
13185404Sbz *    notice, this list of conditions and the following disclaimer in the
14185404Sbz *    documentation and/or other materials provided with the distribution.
15185404Sbz *
16185404Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17185404Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18185404Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19185404Sbz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20185404Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21185404Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22185404Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23185404Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24185404Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25185404Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26185404Sbz * SUCH DAMAGE.
2746197Sphk */
2846155Sphk
29116182Sobrien#include <sys/cdefs.h>
30116182Sobrien__FBSDID("$FreeBSD: stable/10/sys/kern/kern_jail.c 339410 2018-10-17 16:17:56Z jamie $");
31116182Sobrien
32193066Sjamie#include "opt_compat.h"
33185435Sbz#include "opt_ddb.h"
34185435Sbz#include "opt_inet.h"
35185435Sbz#include "opt_inet6.h"
36131177Spjd
3746155Sphk#include <sys/param.h>
3846155Sphk#include <sys/types.h>
3946155Sphk#include <sys/kernel.h>
4046155Sphk#include <sys/systm.h>
4146155Sphk#include <sys/errno.h>
4246155Sphk#include <sys/sysproto.h>
4346155Sphk#include <sys/malloc.h>
44192895Sjamie#include <sys/osd.h>
45164032Srwatson#include <sys/priv.h>
4646155Sphk#include <sys/proc.h>
47124882Srwatson#include <sys/taskqueue.h>
48177785Skib#include <sys/fcntl.h>
4946155Sphk#include <sys/jail.h>
5087275Srwatson#include <sys/lock.h>
5187275Srwatson#include <sys/mutex.h>
52220137Strasz#include <sys/racct.h>
53221362Strasz#include <sys/refcount.h>
54168401Spjd#include <sys/sx.h>
55193066Sjamie#include <sys/sysent.h>
56113275Smike#include <sys/namei.h>
57147185Spjd#include <sys/mount.h>
58113275Smike#include <sys/queue.h>
5946155Sphk#include <sys/socket.h>
60113275Smike#include <sys/syscallsubr.h>
6157163Srwatson#include <sys/sysctl.h>
62113275Smike#include <sys/vnode.h>
63196019Srwatson
6446155Sphk#include <net/if.h>
65196019Srwatson#include <net/vnet.h>
66196019Srwatson
6746155Sphk#include <netinet/in.h>
68196019Srwatson
69185435Sbz#ifdef DDB
70185435Sbz#include <ddb/ddb.h>
71185435Sbz#ifdef INET6
72185435Sbz#include <netinet6/in6_var.h>
73185435Sbz#endif /* INET6 */
74185435Sbz#endif /* DDB */
7546155Sphk
76163606Srwatson#include <security/mac/mac_framework.h>
77163606Srwatson
78195944Sjamie#define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
79195944Sjamie
8046155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
81227293Sedstatic MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8246155Sphk
83202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */
84202468Sbz#ifdef INET
85202468Sbz#ifdef INET6
86202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
87202468Sbz#else
88202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
89202468Sbz#endif
90202468Sbz#else /* !INET */
91202468Sbz#ifdef INET6
92202468Sbz#define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
93202468Sbz#else
94202468Sbz#define	_PR_IP_SADDRSEL	0
95202468Sbz#endif
96202468Sbz#endif
97202468Sbz
98192895Sjamie/* prison0 describes what is "real" about the system. */
99192895Sjamiestruct prison prison0 = {
100192895Sjamie	.pr_id		= 0,
101192895Sjamie	.pr_name	= "0",
102192895Sjamie	.pr_ref		= 1,
103192895Sjamie	.pr_uref	= 1,
104192895Sjamie	.pr_path	= "/",
105192895Sjamie	.pr_securelevel	= -1,
106231267Smm	.pr_devfs_rsnum = 0,
107194762Sjamie	.pr_childmax	= JAIL_MAX,
108195944Sjamie	.pr_hostuuid	= DEFAULT_HOSTUUID,
109201145Santoine	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
110196176Sbz#ifdef VIMAGE
111202468Sbz	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
112196176Sbz#else
113202468Sbz	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
114196176Sbz#endif
115192895Sjamie	.pr_allow	= PR_ALLOW_ALL,
116192895Sjamie};
117192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11857163Srwatson
119221362Strasz/* allprison, allprison_racct and lastprid are protected by allprison_lock. */
120168401Spjdstruct	sx allprison_lock;
121191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
122191673Sjamiestruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
123221362StraszLIST_HEAD(, prison_racct) allprison_racct;
124179881Sdelphijint	lastprid = 0;
125113275Smike
126191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr);
127190466Sjamiestatic void prison_complete(void *context, int pending);
128191673Sjamiestatic void prison_deref(struct prison *pr, int flags);
129192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2);
130192895Sjamiestatic void prison_remove_one(struct prison *pr);
131221362Strasz#ifdef RACCT
132221362Straszstatic void prison_racct_attach(struct prison *pr);
133232598Straszstatic void prison_racct_modify(struct prison *pr);
134221362Straszstatic void prison_racct_detach(struct prison *pr);
135221362Strasz#endif
136185435Sbz#ifdef INET
137190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
138192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
139185435Sbz#endif
140185435Sbz#ifdef INET6
141190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
142192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
143185435Sbz#endif
144113275Smike
145191673Sjamie/* Flags for prison_deref */
146191673Sjamie#define	PD_DEREF	0x01
147191673Sjamie#define	PD_DEUREF	0x02
148191673Sjamie#define	PD_LOCKED	0x04
149191673Sjamie#define	PD_LIST_SLOCKED	0x08
150191673Sjamie#define	PD_LIST_XLOCKED	0x10
151113275Smike
152192895Sjamie/*
153216861Sbz * Parameter names corresponding to PR_* flag values.  Size values are for kvm
154216861Sbz * as we cannot figure out the size of a sparse array, or an array without a
155216861Sbz * terminating entry.
156192895Sjamie */
157192895Sjamiestatic char *pr_flag_names[] = {
158192895Sjamie	[0] = "persist",
159202468Sbz#ifdef INET
160202468Sbz	[7] = "ip4.saddrsel",
161202468Sbz#endif
162202468Sbz#ifdef INET6
163202468Sbz	[8] = "ip6.saddrsel",
164202468Sbz#endif
165192895Sjamie};
166216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names);
167192895Sjamie
168192895Sjamiestatic char *pr_flag_nonames[] = {
169192895Sjamie	[0] = "nopersist",
170202468Sbz#ifdef INET
171202468Sbz	[7] = "ip4.nosaddrsel",
172202468Sbz#endif
173202468Sbz#ifdef INET6
174202468Sbz	[8] = "ip6.nosaddrsel",
175202468Sbz#endif
176195870Sjamie};
177216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
178195870Sjamie
179195870Sjamiestruct jailsys_flags {
180195870Sjamie	const char	*name;
181195870Sjamie	unsigned	 disable;
182195870Sjamie	unsigned	 new;
183195870Sjamie} pr_flag_jailsys[] = {
184195870Sjamie	{ "host", 0, PR_HOST },
185195870Sjamie#ifdef VIMAGE
186195870Sjamie	{ "vnet", 0, PR_VNET },
187195870Sjamie#endif
188192895Sjamie#ifdef INET
189195870Sjamie	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
190192895Sjamie#endif
191192895Sjamie#ifdef INET6
192195870Sjamie	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
193192895Sjamie#endif
194192895Sjamie};
195216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
196192895Sjamie
197192895Sjamiestatic char *pr_allow_names[] = {
198192895Sjamie	"allow.set_hostname",
199192895Sjamie	"allow.sysvipc",
200192895Sjamie	"allow.raw_sockets",
201192895Sjamie	"allow.chflags",
202192895Sjamie	"allow.mount",
203192895Sjamie	"allow.quotas",
204192895Sjamie	"allow.socket_af",
205232059Smm	"allow.mount.devfs",
206232059Smm	"allow.mount.nullfs",
207232186Smm	"allow.mount.zfs",
208232278Smm	"allow.mount.procfs",
209254741Sdelphij	"allow.mount.tmpfs",
210277985Sjamie	"allow.mount.fdescfs",
211295951Saraujo	"allow.mount.linprocfs",
212295951Saraujo	"allow.mount.linsysfs",
213192895Sjamie};
214216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names);
215192895Sjamie
216192895Sjamiestatic char *pr_allow_nonames[] = {
217192895Sjamie	"allow.noset_hostname",
218192895Sjamie	"allow.nosysvipc",
219192895Sjamie	"allow.noraw_sockets",
220192895Sjamie	"allow.nochflags",
221192895Sjamie	"allow.nomount",
222192895Sjamie	"allow.noquotas",
223192895Sjamie	"allow.nosocket_af",
224232059Smm	"allow.mount.nodevfs",
225232059Smm	"allow.mount.nonullfs",
226232186Smm	"allow.mount.nozfs",
227232278Smm	"allow.mount.noprocfs",
228254741Sdelphij	"allow.mount.notmpfs",
229277985Sjamie	"allow.mount.nofdescfs",
230295951Saraujo	"allow.mount.nolinprocfs",
231295951Saraujo	"allow.mount.nolinsysfs",
232192895Sjamie};
233216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
234192895Sjamie
235196002Sjamie#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
236196002Sjamie#define	JAIL_DEFAULT_ENFORCE_STATFS	2
237232059Smm#define	JAIL_DEFAULT_DEVFS_RSNUM	0
238192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
239196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
240231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
241192895Sjamie#if defined(INET) || defined(INET6)
242193865Sjamiestatic unsigned jail_max_af_ips = 255;
243192895Sjamie#endif
244192895Sjamie
245280632Sian/*
246280632Sian * Initialize the parts of prison0 that can't be static-initialized with
247280632Sian * constants.  This is called from proc0_init() after creating thread0 cpuset.
248280632Sian */
249280632Sianvoid
250280632Sianprison0_init(void)
251280632Sian{
252280632Sian
253280632Sian	prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
254280632Sian	prison0.pr_osreldate = osreldate;
255280632Sian	strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
256280632Sian}
257280632Sian
258192895Sjamie#ifdef INET
259185435Sbzstatic int
260185435Sbzqcmp_v4(const void *ip1, const void *ip2)
261185435Sbz{
262185435Sbz	in_addr_t iaa, iab;
263185435Sbz
264185435Sbz	/*
265185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
266185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
267185435Sbz	 * interesting results.  If you do not understand, do not try.
268185435Sbz	 */
269185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
270185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
271185435Sbz
272185435Sbz	/*
273185435Sbz	 * Do not simply return the difference of the two numbers, the int is
274185435Sbz	 * not wide enough.
275185435Sbz	 */
276185435Sbz	if (iaa > iab)
277185435Sbz		return (1);
278185435Sbz	else if (iaa < iab)
279185435Sbz		return (-1);
280185435Sbz	else
281185435Sbz		return (0);
282185435Sbz}
283185435Sbz#endif
284185435Sbz
285185435Sbz#ifdef INET6
286185435Sbzstatic int
287185435Sbzqcmp_v6(const void *ip1, const void *ip2)
288185435Sbz{
289185435Sbz	const struct in6_addr *ia6a, *ia6b;
290185435Sbz	int i, rc;
291185435Sbz
292185435Sbz	ia6a = (const struct in6_addr *)ip1;
293185435Sbz	ia6b = (const struct in6_addr *)ip2;
294185435Sbz
295185435Sbz	rc = 0;
296190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
297185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
298185435Sbz			rc = 1;
299185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
300185435Sbz			rc = -1;
301185435Sbz	}
302185435Sbz	return (rc);
303185435Sbz}
304185435Sbz#endif
305185435Sbz
306191673Sjamie/*
307191673Sjamie * struct jail_args {
308191673Sjamie *	struct jail *jail;
309191673Sjamie * };
310191673Sjamie */
311191673Sjamieint
312225617Skmacysys_jail(struct thread *td, struct jail_args *uap)
313185435Sbz{
314191673Sjamie	uint32_t version;
315191673Sjamie	int error;
316192895Sjamie	struct jail j;
317185435Sbz
318191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
319191673Sjamie	if (error)
320191673Sjamie		return (error);
321185435Sbz
322191673Sjamie	switch (version) {
323191673Sjamie	case 0:
324191673Sjamie	{
325191673Sjamie		struct jail_v0 j0;
326185435Sbz
327192895Sjamie		/* FreeBSD single IPv4 jails. */
328192895Sjamie		bzero(&j, sizeof(struct jail));
329191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
330191673Sjamie		if (error)
331191673Sjamie			return (error);
332192895Sjamie		j.version = j0.version;
333192895Sjamie		j.path = j0.path;
334192895Sjamie		j.hostname = j0.hostname;
335258929Speter		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
336191673Sjamie		break;
337191673Sjamie	}
338191673Sjamie
339191673Sjamie	case 1:
340185435Sbz		/*
341191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
342191673Sjamie		 * that never made it into the official kernel.
343185435Sbz		 */
344191673Sjamie		return (EINVAL);
345185435Sbz
346191673Sjamie	case 2:	/* JAIL_API_VERSION */
347191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
348191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
349191673Sjamie		if (error)
350191673Sjamie			return (error);
351192895Sjamie		break;
352192895Sjamie
353192895Sjamie	default:
354192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
355192895Sjamie		return (EINVAL);
356192895Sjamie	}
357192895Sjamie	return (kern_jail(td, &j));
358192895Sjamie}
359192895Sjamie
360192895Sjamieint
361192895Sjamiekern_jail(struct thread *td, struct jail *j)
362192895Sjamie{
363193865Sjamie	struct iovec optiov[2 * (4
364193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
365193865Sjamie#ifdef INET
366193865Sjamie			    + 1
367193865Sjamie#endif
368193865Sjamie#ifdef INET6
369193865Sjamie			    + 1
370193865Sjamie#endif
371193865Sjamie			    )];
372192895Sjamie	struct uio opt;
373192895Sjamie	char *u_path, *u_hostname, *u_name;
374185435Sbz#ifdef INET
375193865Sjamie	uint32_t ip4s;
376192895Sjamie	struct in_addr *u_ip4;
377192895Sjamie#endif
378192895Sjamie#ifdef INET6
379192895Sjamie	struct in6_addr *u_ip6;
380192895Sjamie#endif
381192895Sjamie	size_t tmplen;
382192895Sjamie	int error, enforce_statfs, fi;
383192895Sjamie
384192895Sjamie	bzero(&optiov, sizeof(optiov));
385192895Sjamie	opt.uio_iov = optiov;
386192895Sjamie	opt.uio_iovcnt = 0;
387192895Sjamie	opt.uio_offset = -1;
388192895Sjamie	opt.uio_resid = -1;
389192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
390192895Sjamie	opt.uio_rw = UIO_READ;
391192895Sjamie	opt.uio_td = td;
392192895Sjamie
393192895Sjamie	/* Set permissions for top-level jails from sysctls. */
394192895Sjamie	if (!jailed(td->td_ucred)) {
395192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
396192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
397192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
398192895Sjamie			    (jail_default_allow & (1 << fi))
399192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
400192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
401192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
402192895Sjamie			opt.uio_iovcnt += 2;
403192895Sjamie		}
404192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
405192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
406192895Sjamie		opt.uio_iovcnt++;
407192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
408192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
409192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
410192895Sjamie		opt.uio_iovcnt++;
411192895Sjamie	}
412192895Sjamie
413192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
414192895Sjamie#ifdef INET
415192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
416192895Sjamie	if (ip4s > jail_max_af_ips)
417192895Sjamie		return (EINVAL);
418192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
419191673Sjamie#else
420192895Sjamie	if (j->ip4s > 0)
421192895Sjamie		return (EINVAL);
422191673Sjamie#endif
423191673Sjamie#ifdef INET6
424192895Sjamie	if (j->ip6s > jail_max_af_ips)
425192895Sjamie		return (EINVAL);
426192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
427191673Sjamie#else
428192895Sjamie	if (j->ip6s > 0)
429192895Sjamie		return (EINVAL);
430191673Sjamie#endif
431192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
432192895Sjamie	u_hostname = u_path + MAXPATHLEN;
433192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
434191673Sjamie#ifdef INET
435192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
436191673Sjamie#endif
437191673Sjamie#ifdef INET6
438191673Sjamie#ifdef INET
439192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
440191673Sjamie#else
441192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
442191673Sjamie#endif
443191673Sjamie#endif
444192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
445192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
446192895Sjamie	opt.uio_iovcnt++;
447192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
448192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
449192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
450192895Sjamie	if (error) {
451192895Sjamie		free(u_path, M_TEMP);
452192895Sjamie		return (error);
453192895Sjamie	}
454192895Sjamie	opt.uio_iovcnt++;
455192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
456192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
457192895Sjamie	opt.uio_iovcnt++;
458192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
459192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
460192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
461192895Sjamie	if (error) {
462192895Sjamie		free(u_path, M_TEMP);
463192895Sjamie		return (error);
464192895Sjamie	}
465192895Sjamie	opt.uio_iovcnt++;
466192895Sjamie	if (j->jailname != NULL) {
467192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
468192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
469192895Sjamie		opt.uio_iovcnt++;
470192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
471192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
472192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
473191673Sjamie		if (error) {
474191673Sjamie			free(u_path, M_TEMP);
475191673Sjamie			return (error);
476191673Sjamie		}
477192895Sjamie		opt.uio_iovcnt++;
478192895Sjamie	}
479191673Sjamie#ifdef INET
480192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
481192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
482192895Sjamie	opt.uio_iovcnt++;
483192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
484192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
485192895Sjamie	if (j->version == 0)
486192895Sjamie		u_ip4->s_addr = j->ip4s;
487192895Sjamie	else {
488192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
489191673Sjamie		if (error) {
490191673Sjamie			free(u_path, M_TEMP);
491191673Sjamie			return (error);
492191673Sjamie		}
493192895Sjamie	}
494192895Sjamie	opt.uio_iovcnt++;
495185435Sbz#endif
496185435Sbz#ifdef INET6
497192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
498192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
499192895Sjamie	opt.uio_iovcnt++;
500192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
501192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
502192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
503192895Sjamie	if (error) {
504192895Sjamie		free(u_path, M_TEMP);
505192895Sjamie		return (error);
506192895Sjamie	}
507192895Sjamie	opt.uio_iovcnt++;
508185435Sbz#endif
509192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
510192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
511191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
512191673Sjamie	free(u_path, M_TEMP);
513191673Sjamie	return (error);
514185435Sbz}
515185435Sbz
516192895Sjamie
517191673Sjamie/*
518191673Sjamie * struct jail_set_args {
519191673Sjamie *	struct iovec *iovp;
520191673Sjamie *	unsigned int iovcnt;
521191673Sjamie *	int flags;
522191673Sjamie * };
523191673Sjamie */
524191673Sjamieint
525225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap)
526185435Sbz{
527191673Sjamie	struct uio *auio;
528191673Sjamie	int error;
529191673Sjamie
530191673Sjamie	/* Check that we have an even number of iovecs. */
531191673Sjamie	if (uap->iovcnt & 1)
532191673Sjamie		return (EINVAL);
533191673Sjamie
534191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
535191673Sjamie	if (error)
536191673Sjamie		return (error);
537191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
538191673Sjamie	free(auio, M_IOV);
539191673Sjamie	return (error);
540191673Sjamie}
541191673Sjamie
542191673Sjamieint
543191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
544191673Sjamie{
545191673Sjamie	struct nameidata nd;
546185435Sbz#ifdef INET
547190466Sjamie	struct in_addr *ip4;
548185435Sbz#endif
549185435Sbz#ifdef INET6
550185435Sbz	struct in6_addr *ip6;
551185435Sbz#endif
552191673Sjamie	struct vfsopt *opt;
553191673Sjamie	struct vfsoptlist *opts;
554196135Sbz	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
555191673Sjamie	struct vnode *root;
556196835Sjamie	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
557280632Sian	char *g_path, *osrelstr;
558192895Sjamie#if defined(INET) || defined(INET6)
559196135Sbz	struct prison *tppr;
560191673Sjamie	void *op;
561192895Sjamie#endif
562193066Sjamie	unsigned long hid;
563298833Sjamie	size_t namelen, onamelen, pnamelen;
564298833Sjamie	int born, created, cuflags, descend, enforce;
565298833Sjamie	int error, errmsg_len, errmsg_pos;
566231267Smm	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
567195870Sjamie	int fi, jid, jsys, len, level;
568280632Sian	int childmax, osreldt, rsnum, slevel;
569230129Smm	int fullpath_disabled;
570191673Sjamie#if defined(INET) || defined(INET6)
571192895Sjamie	int ii, ij;
572191673Sjamie#endif
573191673Sjamie#ifdef INET
574195974Sjamie	int ip4s, redo_ip4;
575191673Sjamie#endif
576191673Sjamie#ifdef INET6
577195974Sjamie	int ip6s, redo_ip6;
578191673Sjamie#endif
579224290Smckusick	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
580224290Smckusick	unsigned tallow;
581191673Sjamie	char numbuf[12];
582185435Sbz
583191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
584191673Sjamie	if (!error && (flags & JAIL_ATTACH))
585191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
586191673Sjamie	if (error)
587191673Sjamie		return (error);
588298833Sjamie	mypr = td->td_ucred->cr_prison;
589194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
590192895Sjamie		return (EPERM);
591191673Sjamie	if (flags & ~JAIL_SET_MASK)
592191673Sjamie		return (EINVAL);
593191673Sjamie
594185435Sbz	/*
595191673Sjamie	 * Check all the parameters before committing to anything.  Not all
596191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
597191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
598191673Sjamie	 * the allprison lock.
599185435Sbz	 *
600191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
601191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
602191673Sjamie	 *     than duplicate it under a different name.
603185435Sbz	 */
604191673Sjamie	error = vfs_buildopts(optuio, &opts);
605191673Sjamie	if (error)
606191673Sjamie		return (error);
607185435Sbz#ifdef INET
608185435Sbz	ip4 = NULL;
609185435Sbz#endif
610185435Sbz#ifdef INET6
611185435Sbz	ip6 = NULL;
612185435Sbz#endif
613230407Smm	g_path = NULL;
614191673Sjamie
615298833Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
616298833Sjamie	if (!cuflags) {
617298833Sjamie		error = EINVAL;
618298833Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
619298833Sjamie		goto done_errmsg;
620298833Sjamie	}
621298833Sjamie
622191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
623191673Sjamie	if (error == ENOENT)
624191673Sjamie		jid = 0;
625191673Sjamie	else if (error != 0)
626191673Sjamie		goto done_free;
627191673Sjamie
628191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
629191673Sjamie	if (error == ENOENT)
630191673Sjamie		gotslevel = 0;
631191673Sjamie	else if (error != 0)
632191673Sjamie		goto done_free;
633191673Sjamie	else
634191673Sjamie		gotslevel = 1;
635191673Sjamie
636194762Sjamie	error =
637194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
638194762Sjamie	if (error == ENOENT)
639194762Sjamie		gotchildmax = 0;
640194762Sjamie	else if (error != 0)
641194762Sjamie		goto done_free;
642194762Sjamie	else
643194762Sjamie		gotchildmax = 1;
644194762Sjamie
645192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
646212436Sjamie	if (error == ENOENT)
647212436Sjamie		gotenforce = 0;
648212436Sjamie	else if (error != 0)
649192895Sjamie		goto done_free;
650212436Sjamie	else if (enforce < 0 || enforce > 2) {
651212436Sjamie		error = EINVAL;
652212436Sjamie		goto done_free;
653212436Sjamie	} else
654212436Sjamie		gotenforce = 1;
655192895Sjamie
656231267Smm	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
657231267Smm	if (error == ENOENT)
658231267Smm		gotrsnum = 0;
659231267Smm	else if (error != 0)
660231267Smm		goto done_free;
661231267Smm	else
662231267Smm		gotrsnum = 1;
663231267Smm
664191673Sjamie	pr_flags = ch_flags = 0;
665192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
666192895Sjamie	    fi++) {
667192895Sjamie		if (pr_flag_names[fi] == NULL)
668192895Sjamie			continue;
669192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
670192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
671192895Sjamie	}
672191673Sjamie	ch_flags |= pr_flags;
673195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
674195870Sjamie	    fi++) {
675195870Sjamie		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
676195870Sjamie		    sizeof(jsys));
677195870Sjamie		if (error == ENOENT)
678195870Sjamie			continue;
679195870Sjamie		if (error != 0)
680195870Sjamie			goto done_free;
681195870Sjamie		switch (jsys) {
682195870Sjamie		case JAIL_SYS_DISABLE:
683195870Sjamie			if (!pr_flag_jailsys[fi].disable) {
684195870Sjamie				error = EINVAL;
685195870Sjamie				goto done_free;
686195870Sjamie			}
687195870Sjamie			pr_flags |= pr_flag_jailsys[fi].disable;
688195870Sjamie			break;
689195870Sjamie		case JAIL_SYS_NEW:
690195870Sjamie			pr_flags |= pr_flag_jailsys[fi].new;
691195870Sjamie			break;
692195870Sjamie		case JAIL_SYS_INHERIT:
693195870Sjamie			break;
694195870Sjamie		default:
695195870Sjamie			error = EINVAL;
696195870Sjamie			goto done_free;
697195870Sjamie		}
698195870Sjamie		ch_flags |=
699195870Sjamie		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
700195870Sjamie	}
701211085Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
702211085Sjamie	    && !(pr_flags & PR_PERSIST)) {
703211085Sjamie		error = EINVAL;
704211085Sjamie		vfs_opterror(opts, "new jail must persist or attach");
705211085Sjamie		goto done_errmsg;
706211085Sjamie	}
707194251Sjamie#ifdef VIMAGE
708194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
709194251Sjamie		error = EINVAL;
710194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
711194251Sjamie		goto done_errmsg;
712194251Sjamie	}
713194251Sjamie#endif
714195974Sjamie#ifdef INET
715195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
716195974Sjamie		error = EINVAL;
717195974Sjamie		vfs_opterror(opts, "ip4 cannot be changed after creation");
718195974Sjamie		goto done_errmsg;
719195974Sjamie	}
720195974Sjamie#endif
721195974Sjamie#ifdef INET6
722195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
723195974Sjamie		error = EINVAL;
724195974Sjamie		vfs_opterror(opts, "ip6 cannot be changed after creation");
725195974Sjamie		goto done_errmsg;
726195974Sjamie	}
727195974Sjamie#endif
728191673Sjamie
729192895Sjamie	pr_allow = ch_allow = 0;
730192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
731192895Sjamie	    fi++) {
732192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
733192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
734192895Sjamie	}
735192895Sjamie	ch_allow |= pr_allow;
736192895Sjamie
737191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
738191673Sjamie	if (error == ENOENT)
739191673Sjamie		name = NULL;
740191673Sjamie	else if (error != 0)
741191673Sjamie		goto done_free;
742191673Sjamie	else {
743191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
744191673Sjamie			error = EINVAL;
745191673Sjamie			goto done_free;
746191673Sjamie		}
747191673Sjamie		if (len > MAXHOSTNAMELEN) {
748191673Sjamie			error = ENAMETOOLONG;
749191673Sjamie			goto done_free;
750191673Sjamie		}
751191673Sjamie	}
752191673Sjamie
753191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
754191673Sjamie	if (error == ENOENT)
755191673Sjamie		host = NULL;
756191673Sjamie	else if (error != 0)
757191673Sjamie		goto done_free;
758191673Sjamie	else {
759193066Sjamie		ch_flags |= PR_HOST;
760193066Sjamie		pr_flags |= PR_HOST;
761191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
762191673Sjamie			error = EINVAL;
763191673Sjamie			goto done_free;
764191673Sjamie		}
765191673Sjamie		if (len > MAXHOSTNAMELEN) {
766191673Sjamie			error = ENAMETOOLONG;
767191673Sjamie			goto done_free;
768191673Sjamie		}
769191673Sjamie	}
770191673Sjamie
771193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
772193066Sjamie	if (error == ENOENT)
773193066Sjamie		domain = NULL;
774193066Sjamie	else if (error != 0)
775193066Sjamie		goto done_free;
776193066Sjamie	else {
777193066Sjamie		ch_flags |= PR_HOST;
778193066Sjamie		pr_flags |= PR_HOST;
779193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
780193066Sjamie			error = EINVAL;
781193066Sjamie			goto done_free;
782193066Sjamie		}
783193066Sjamie		if (len > MAXHOSTNAMELEN) {
784193066Sjamie			error = ENAMETOOLONG;
785193066Sjamie			goto done_free;
786193066Sjamie		}
787193066Sjamie	}
788193066Sjamie
789193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
790193066Sjamie	if (error == ENOENT)
791193066Sjamie		uuid = NULL;
792193066Sjamie	else if (error != 0)
793193066Sjamie		goto done_free;
794193066Sjamie	else {
795193066Sjamie		ch_flags |= PR_HOST;
796193066Sjamie		pr_flags |= PR_HOST;
797193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
798193066Sjamie			error = EINVAL;
799193066Sjamie			goto done_free;
800193066Sjamie		}
801193066Sjamie		if (len > HOSTUUIDLEN) {
802193066Sjamie			error = ENAMETOOLONG;
803193066Sjamie			goto done_free;
804193066Sjamie		}
805193066Sjamie	}
806193066Sjamie
807205014Snwhitehorn#ifdef COMPAT_FREEBSD32
808217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
809193066Sjamie		uint32_t hid32;
810193066Sjamie
811193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
812193066Sjamie		hid = hid32;
813193066Sjamie	} else
814193066Sjamie#endif
815193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
816193066Sjamie	if (error == ENOENT)
817193066Sjamie		gothid = 0;
818193066Sjamie	else if (error != 0)
819193066Sjamie		goto done_free;
820193066Sjamie	else {
821193066Sjamie		gothid = 1;
822193066Sjamie		ch_flags |= PR_HOST;
823193066Sjamie		pr_flags |= PR_HOST;
824193066Sjamie	}
825193066Sjamie
826185435Sbz#ifdef INET
827191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
828191673Sjamie	if (error == ENOENT)
829277279Sjamie		ip4s = 0;
830191673Sjamie	else if (error != 0)
831191673Sjamie		goto done_free;
832191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
833191673Sjamie		error = EINVAL;
834191673Sjamie		goto done_free;
835192895Sjamie	} else {
836195870Sjamie		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
837195870Sjamie		if (ip4s == 0)
838195870Sjamie			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
839195870Sjamie		else {
840195870Sjamie			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
841192895Sjamie			ip4s /= sizeof(*ip4);
842192895Sjamie			if (ip4s > jail_max_af_ips) {
843185435Sbz				error = EINVAL;
844192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
845192895Sjamie				goto done_errmsg;
846185435Sbz			}
847195974Sjamie			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
848192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
849192895Sjamie			/*
850192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
851192895Sjamie			 * the primary IP address as given from userland.
852192895Sjamie			 * This special IP is used for unbound outgoing
853202116Sbz			 * connections as well for "loopback" traffic in case
854202116Sbz			 * source address selection cannot find any more fitting
855202116Sbz			 * address to connect from.
856192895Sjamie			 */
857192895Sjamie			if (ip4s > 1)
858192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
859192895Sjamie			/*
860192895Sjamie			 * Check for duplicate addresses and do some simple
861192895Sjamie			 * zero and broadcast checks. If users give other bogus
862192895Sjamie			 * addresses it is their problem.
863192895Sjamie			 *
864192895Sjamie			 * We do not have to care about byte order for these
865192895Sjamie			 * checks so we will do them in NBO.
866192895Sjamie			 */
867192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
868192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
869192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
870192895Sjamie					error = EINVAL;
871192895Sjamie					goto done_free;
872192895Sjamie				}
873192895Sjamie				if ((ii+1) < ip4s &&
874192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
875192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
876192895Sjamie					error = EINVAL;
877192895Sjamie					goto done_free;
878192895Sjamie				}
879192895Sjamie			}
880185435Sbz		}
881191673Sjamie	}
882191673Sjamie#endif
883185435Sbz
884185435Sbz#ifdef INET6
885191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
886191673Sjamie	if (error == ENOENT)
887277279Sjamie		ip6s = 0;
888191673Sjamie	else if (error != 0)
889191673Sjamie		goto done_free;
890191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
891191673Sjamie		error = EINVAL;
892191673Sjamie		goto done_free;
893192895Sjamie	} else {
894195870Sjamie		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
895195870Sjamie		if (ip6s == 0)
896195870Sjamie			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
897195870Sjamie		else {
898195870Sjamie			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
899192895Sjamie			ip6s /= sizeof(*ip6);
900192895Sjamie			if (ip6s > jail_max_af_ips) {
901185435Sbz				error = EINVAL;
902192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
903192895Sjamie				goto done_errmsg;
904185435Sbz			}
905195974Sjamie			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
906192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
907192895Sjamie			if (ip6s > 1)
908192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
909192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
910192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
911192895Sjamie					error = EINVAL;
912192895Sjamie					goto done_free;
913192895Sjamie				}
914192895Sjamie				if ((ii+1) < ip6s &&
915192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
916192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
917192895Sjamie				{
918192895Sjamie					error = EINVAL;
919192895Sjamie					goto done_free;
920192895Sjamie				}
921192895Sjamie			}
922185435Sbz		}
923191673Sjamie	}
924185435Sbz#endif
925185435Sbz
926195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
927195945Sjamie	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
928195945Sjamie		error = EINVAL;
929195945Sjamie		vfs_opterror(opts,
930195945Sjamie		    "vnet jails cannot have IP address restrictions");
931195945Sjamie		goto done_errmsg;
932195945Sjamie	}
933195945Sjamie#endif
934195945Sjamie
935301909Sjamie	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
936301909Sjamie	if (error == ENOENT)
937301909Sjamie		osrelstr = NULL;
938301909Sjamie	else if (error != 0)
939301909Sjamie		goto done_free;
940301909Sjamie	else {
941301909Sjamie		if (flags & JAIL_UPDATE) {
942301909Sjamie			error = EINVAL;
943301909Sjamie			vfs_opterror(opts,
944301909Sjamie			    "osrelease cannot be changed after creation");
945301909Sjamie			goto done_errmsg;
946301909Sjamie		}
947301909Sjamie		if (len == 0 || len >= OSRELEASELEN) {
948301909Sjamie			error = EINVAL;
949301909Sjamie			vfs_opterror(opts,
950301909Sjamie			    "osrelease string must be 1-%d bytes long",
951301909Sjamie			    OSRELEASELEN - 1);
952301909Sjamie			goto done_errmsg;
953301909Sjamie		}
954301909Sjamie	}
955301909Sjamie
956301909Sjamie	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
957301909Sjamie	if (error == ENOENT)
958301909Sjamie		osreldt = 0;
959301909Sjamie	else if (error != 0)
960301909Sjamie		goto done_free;
961301909Sjamie	else {
962301909Sjamie		if (flags & JAIL_UPDATE) {
963301909Sjamie			error = EINVAL;
964301909Sjamie			vfs_opterror(opts,
965301909Sjamie			    "osreldate cannot be changed after creation");
966301909Sjamie			goto done_errmsg;
967301909Sjamie		}
968301909Sjamie		if (osreldt == 0) {
969301909Sjamie			error = EINVAL;
970301909Sjamie			vfs_opterror(opts, "osreldate cannot be 0");
971301909Sjamie			goto done_errmsg;
972301909Sjamie		}
973301909Sjamie	}
974301909Sjamie
975230143Smm	fullpath_disabled = 0;
976191673Sjamie	root = NULL;
977191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
978191673Sjamie	if (error == ENOENT)
979191673Sjamie		path = NULL;
980191673Sjamie	else if (error != 0)
981191673Sjamie		goto done_free;
982191673Sjamie	else {
983191673Sjamie		if (flags & JAIL_UPDATE) {
984191673Sjamie			error = EINVAL;
985191673Sjamie			vfs_opterror(opts,
986191673Sjamie			    "path cannot be changed after creation");
987191673Sjamie			goto done_errmsg;
988191673Sjamie		}
989191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
990191673Sjamie			error = EINVAL;
991191673Sjamie			goto done_free;
992191673Sjamie		}
993241896Skib		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
994230129Smm		    path, td);
995230129Smm		error = namei(&nd);
996230129Smm		if (error)
997230129Smm			goto done_free;
998230129Smm		root = nd.ni_vp;
999230129Smm		NDFREE(&nd, NDF_ONLY_PNBUF);
1000230407Smm		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1001230407Smm		strlcpy(g_path, path, MAXPATHLEN);
1002230407Smm		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1003230407Smm		if (error == 0)
1004230407Smm			path = g_path;
1005230407Smm		else if (error == ENODEV) {
1006230129Smm			/* proceed if sysctl debug.disablefullpath == 1 */
1007230129Smm			fullpath_disabled = 1;
1008230129Smm			if (len < 2 || (len == 2 && path[0] == '/'))
1009230129Smm				path = NULL;
1010230407Smm		} else {
1011230129Smm			/* exit on other errors */
1012230129Smm			goto done_free;
1013230129Smm		}
1014230129Smm		if (root->v_type != VDIR) {
1015230129Smm			error = ENOTDIR;
1016230129Smm			vput(root);
1017230129Smm			goto done_free;
1018230129Smm		}
1019230129Smm		VOP_UNLOCK(root, 0);
1020230129Smm		if (fullpath_disabled) {
1021192895Sjamie			/* Leave room for a real-root full pathname. */
1022192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
1023192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
1024192895Sjamie				error = ENAMETOOLONG;
1025301910Sjamie				vrele(root);
1026192895Sjamie				goto done_free;
1027192895Sjamie			}
1028191673Sjamie		}
1029191673Sjamie	}
1030185435Sbz
1031191673Sjamie	/*
1032298833Sjamie	 * Find the specified jail, or at least its parent.
1033191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
1034185435Sbz	 */
1035191673Sjamie	pr = NULL;
1036298833Sjamie	ppr = mypr;
1037196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1038196835Sjamie		namelc = strrchr(name, '.');
1039196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1040196835Sjamie		if (*p != '\0')
1041196835Sjamie			jid = 0;
1042196835Sjamie	}
1043298833Sjamie	sx_xlock(&allprison_lock);
1044191673Sjamie	if (jid != 0) {
1045192895Sjamie		/*
1046192895Sjamie		 * See if a requested jid already exists.  There is an
1047192895Sjamie		 * information leak here if the jid exists but is not within
1048192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
1049192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
1050192895Sjamie		 * will return ENOENT which is not normally a valid error.
1051192895Sjamie		 */
1052191673Sjamie		if (jid < 0) {
1053191673Sjamie			error = EINVAL;
1054191673Sjamie			vfs_opterror(opts, "negative jid");
1055191673Sjamie			goto done_unlock_list;
1056191673Sjamie		}
1057191673Sjamie		pr = prison_find(jid);
1058191673Sjamie		if (pr != NULL) {
1059192895Sjamie			ppr = pr->pr_parent;
1060191673Sjamie			/* Create: jid must not exist. */
1061191673Sjamie			if (cuflags == JAIL_CREATE) {
1062191673Sjamie				mtx_unlock(&pr->pr_mtx);
1063191673Sjamie				error = EEXIST;
1064191673Sjamie				vfs_opterror(opts, "jail %d already exists",
1065191673Sjamie				    jid);
1066191673Sjamie				goto done_unlock_list;
1067191673Sjamie			}
1068192895Sjamie			if (!prison_ischild(mypr, pr)) {
1069192895Sjamie				mtx_unlock(&pr->pr_mtx);
1070192895Sjamie				pr = NULL;
1071192895Sjamie			} else if (pr->pr_uref == 0) {
1072191673Sjamie				if (!(flags & JAIL_DYING)) {
1073191673Sjamie					mtx_unlock(&pr->pr_mtx);
1074191673Sjamie					error = ENOENT;
1075191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1076191673Sjamie					    jid);
1077191673Sjamie					goto done_unlock_list;
1078191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
1079191673Sjamie				    (pr_flags & PR_PERSIST)) {
1080191673Sjamie					/*
1081191673Sjamie					 * A dying jail might be resurrected
1082191673Sjamie					 * (via attach or persist), but first
1083191673Sjamie					 * it must determine if another jail
1084191673Sjamie					 * has claimed its name.  Accomplish
1085191673Sjamie					 * this by implicitly re-setting the
1086191673Sjamie					 * name.
1087191673Sjamie					 */
1088191673Sjamie					if (name == NULL)
1089192895Sjamie						name = prison_name(mypr, pr);
1090191673Sjamie				}
1091191673Sjamie			}
1092191673Sjamie		}
1093191673Sjamie		if (pr == NULL) {
1094191673Sjamie			/* Update: jid must exist. */
1095191673Sjamie			if (cuflags == JAIL_UPDATE) {
1096191673Sjamie				error = ENOENT;
1097191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1098191673Sjamie				goto done_unlock_list;
1099191673Sjamie			}
1100191673Sjamie		}
1101191673Sjamie	}
1102191673Sjamie	/*
1103191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1104191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1105191673Sjamie	 * (where the name must not already exist in a different jail),
1106191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1107191673Sjamie	 * because that is the jail being updated).
1108191673Sjamie	 */
1109298833Sjamie	namelc = NULL;
1110191673Sjamie	if (name != NULL) {
1111196835Sjamie		namelc = strrchr(name, '.');
1112196835Sjamie		if (namelc == NULL)
1113196835Sjamie			namelc = name;
1114196835Sjamie		else {
1115192895Sjamie			/*
1116192895Sjamie			 * This is a hierarchical name.  Split it into the
1117192895Sjamie			 * parent and child names, and make sure the parent
1118192895Sjamie			 * exists or matches an already found jail.
1119192895Sjamie			 */
1120192895Sjamie			if (pr != NULL) {
1121196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1122196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1123192895Sjamie					mtx_unlock(&pr->pr_mtx);
1124192895Sjamie					error = EINVAL;
1125192895Sjamie					vfs_opterror(opts,
1126192895Sjamie					    "cannot change jail's parent");
1127192895Sjamie					goto done_unlock_list;
1128192895Sjamie				}
1129192895Sjamie			} else {
1130298833Sjamie				*namelc = '\0';
1131192895Sjamie				ppr = prison_find_name(mypr, name);
1132192895Sjamie				if (ppr == NULL) {
1133192895Sjamie					error = ENOENT;
1134192895Sjamie					vfs_opterror(opts,
1135192895Sjamie					    "jail \"%s\" not found", name);
1136192895Sjamie					goto done_unlock_list;
1137192895Sjamie				}
1138192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1139298833Sjamie				*namelc = '.';
1140192895Sjamie			}
1141298833Sjamie			namelc++;
1142192895Sjamie		}
1143298833Sjamie		if (namelc[0] != '\0') {
1144298833Sjamie			pnamelen =
1145192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1146192895Sjamie name_again:
1147191673Sjamie			deadpr = NULL;
1148192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1149191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1150298833Sjamie				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
1151191673Sjamie					if (pr == NULL &&
1152191673Sjamie					    cuflags != JAIL_CREATE) {
1153191673Sjamie						mtx_lock(&tpr->pr_mtx);
1154191673Sjamie						if (tpr->pr_ref > 0) {
1155191673Sjamie							/*
1156191673Sjamie							 * Use this jail
1157191673Sjamie							 * for updates.
1158191673Sjamie							 */
1159191673Sjamie							if (tpr->pr_uref > 0) {
1160191673Sjamie								pr = tpr;
1161191673Sjamie								break;
1162191673Sjamie							}
1163191673Sjamie							deadpr = tpr;
1164191673Sjamie						}
1165191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1166191673Sjamie					} else if (tpr->pr_uref > 0) {
1167191673Sjamie						/*
1168191673Sjamie						 * Create, or update(jid):
1169191673Sjamie						 * name must not exist in an
1170192895Sjamie						 * active sibling jail.
1171191673Sjamie						 */
1172191673Sjamie						error = EEXIST;
1173191673Sjamie						if (pr != NULL)
1174191673Sjamie							mtx_unlock(&pr->pr_mtx);
1175191673Sjamie						vfs_opterror(opts,
1176191673Sjamie						   "jail \"%s\" already exists",
1177191673Sjamie						   name);
1178191673Sjamie						goto done_unlock_list;
1179191673Sjamie					}
1180191673Sjamie				}
1181191673Sjamie			}
1182191673Sjamie			/* If no active jail is found, use a dying one. */
1183191673Sjamie			if (deadpr != NULL && pr == NULL) {
1184191673Sjamie				if (flags & JAIL_DYING) {
1185191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1186191673Sjamie					if (deadpr->pr_ref == 0) {
1187191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1188191673Sjamie						goto name_again;
1189191673Sjamie					}
1190191673Sjamie					pr = deadpr;
1191191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1192191673Sjamie					error = ENOENT;
1193191673Sjamie					vfs_opterror(opts,
1194191673Sjamie					    "jail \"%s\" is dying", name);
1195191673Sjamie					goto done_unlock_list;
1196191673Sjamie				}
1197191673Sjamie			}
1198191673Sjamie			/* Update: name must exist if no jid. */
1199191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1200191673Sjamie				error = ENOENT;
1201191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1202191673Sjamie				    name);
1203191673Sjamie				goto done_unlock_list;
1204191673Sjamie			}
1205191673Sjamie		}
1206191673Sjamie	}
1207191673Sjamie	/* Update: must provide a jid or name. */
1208191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1209191673Sjamie		error = ENOENT;
1210191673Sjamie		vfs_opterror(opts, "update specified no jail");
1211191673Sjamie		goto done_unlock_list;
1212191673Sjamie	}
1213185435Sbz
1214191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1215191673Sjamie	if (pr == NULL) {
1216194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1217194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1218194762Sjamie				error = EPERM;
1219194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1220194762Sjamie				goto done_unlock_list;
1221194762Sjamie			}
1222191673Sjamie		created = 1;
1223192895Sjamie		mtx_lock(&ppr->pr_mtx);
1224298832Sjamie		if (ppr->pr_ref == 0) {
1225192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1226192895Sjamie			error = ENOENT;
1227298833Sjamie			vfs_opterror(opts, "jail \"%s\" not found",
1228298833Sjamie			    prison_name(mypr, ppr));
1229192895Sjamie			goto done_unlock_list;
1230192895Sjamie		}
1231192895Sjamie		ppr->pr_ref++;
1232192895Sjamie		ppr->pr_uref++;
1233192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1234191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1235191673Sjamie		if (jid == 0) {
1236191673Sjamie			/* Find the next free jid. */
1237191673Sjamie			jid = lastprid + 1;
1238191673Sjamie findnext:
1239191673Sjamie			if (jid == JAIL_MAX)
1240191673Sjamie				jid = 1;
1241191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1242191673Sjamie				if (tpr->pr_id < jid)
1243191673Sjamie					continue;
1244191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1245191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1246191673Sjamie					break;
1247191673Sjamie				}
1248191673Sjamie				if (jid == lastprid) {
1249191673Sjamie					error = EAGAIN;
1250191673Sjamie					vfs_opterror(opts,
1251191673Sjamie					    "no available jail IDs");
1252191673Sjamie					free(pr, M_PRISON);
1253192895Sjamie					prison_deref(ppr, PD_DEREF |
1254192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1255192895Sjamie					goto done_releroot;
1256191673Sjamie				}
1257191673Sjamie				jid++;
1258191673Sjamie				goto findnext;
1259191673Sjamie			}
1260191673Sjamie			lastprid = jid;
1261191673Sjamie		} else {
1262191673Sjamie			/*
1263191673Sjamie			 * The jail already has a jid (that did not yet exist),
1264191673Sjamie			 * so just find where to insert it.
1265191673Sjamie			 */
1266191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1267191673Sjamie				if (tpr->pr_id >= jid) {
1268191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1269191673Sjamie					break;
1270191673Sjamie				}
1271191673Sjamie		}
1272191673Sjamie		if (tpr == NULL)
1273191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1274192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1275192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1276194762Sjamie			tpr->pr_childcount++;
1277185435Sbz
1278192895Sjamie		pr->pr_parent = ppr;
1279191673Sjamie		pr->pr_id = jid;
1280192895Sjamie
1281192895Sjamie		/* Set some default values, and inherit some from the parent. */
1282298833Sjamie		if (namelc == NULL)
1283298833Sjamie			namelc = "";
1284191673Sjamie		if (path == NULL) {
1285191673Sjamie			path = "/";
1286192895Sjamie			root = mypr->pr_root;
1287191673Sjamie			vref(root);
1288191673Sjamie		}
1289195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1290195944Sjamie		pr->pr_flags |= PR_HOST;
1291195945Sjamie#if defined(INET) || defined(INET6)
1292195945Sjamie#ifdef VIMAGE
1293195945Sjamie		if (!(pr_flags & PR_VNET))
1294195945Sjamie#endif
1295195945Sjamie		{
1296192895Sjamie#ifdef INET
1297195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1298195974Sjamie				pr->pr_flags |=
1299195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1300195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1301195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1302195974Sjamie				if (ppr->pr_ip4 != NULL) {
1303195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1304195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1305195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1306195974Sjamie					    M_WAITOK);
1307195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1308195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1309195974Sjamie				}
1310195974Sjamie			}
1311192895Sjamie#endif
1312192895Sjamie#ifdef INET6
1313195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1314195974Sjamie				pr->pr_flags |=
1315195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1316195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1317195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1318195974Sjamie				if (ppr->pr_ip6 != NULL) {
1319195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1320195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1321195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1322195974Sjamie					    M_WAITOK);
1323195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1324195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1325195974Sjamie				}
1326195974Sjamie			}
1327192895Sjamie#endif
1328195945Sjamie		}
1329195945Sjamie#endif
1330202468Sbz		/* Source address selection is always on by default. */
1331202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1332202468Sbz
1333192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1334192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1335196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1336232059Smm		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1337191673Sjamie
1338280632Sian		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1339280632Sian		if (osrelstr == NULL)
1340280632Sian		    strcpy(pr->pr_osrelease, ppr->pr_osrelease);
1341280632Sian		else
1342280632Sian		    strcpy(pr->pr_osrelease, osrelstr);
1343280632Sian
1344192895Sjamie		LIST_INIT(&pr->pr_children);
1345192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1346298833Sjamie		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1347191673Sjamie
1348194251Sjamie#ifdef VIMAGE
1349194251Sjamie		/* Allocate a new vnet if specified. */
1350194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1351194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1352194251Sjamie#endif
1353185435Sbz		/*
1354191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1355191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1356185435Sbz		 */
1357192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1358191673Sjamie		if (error) {
1359191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1360191673Sjamie			goto done_releroot;
1361191673Sjamie		}
1362185435Sbz
1363191673Sjamie		mtx_lock(&pr->pr_mtx);
1364185435Sbz		/*
1365191673Sjamie		 * New prisons do not yet have a reference, because we do not
1366298833Sjamie		 * want others to see the incomplete prison once the
1367191673Sjamie		 * allprison_lock is downgraded.
1368185435Sbz		 */
1369191673Sjamie	} else {
1370191673Sjamie		created = 0;
1371195974Sjamie		/*
1372195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1373195974Sjamie		 * continue to exist for the duration of the call.
1374195974Sjamie		 */
1375195974Sjamie		pr->pr_ref++;
1376195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1377195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1378195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1379195945Sjamie			error = EINVAL;
1380195945Sjamie			vfs_opterror(opts,
1381195945Sjamie			    "vnet jails cannot have IP address restrictions");
1382195945Sjamie			goto done_deref_locked;
1383195945Sjamie		}
1384195945Sjamie#endif
1385195974Sjamie#ifdef INET
1386195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1387195974Sjamie			error = EINVAL;
1388195974Sjamie			vfs_opterror(opts,
1389195974Sjamie			    "ip4 cannot be changed after creation");
1390195974Sjamie			goto done_deref_locked;
1391195974Sjamie		}
1392195974Sjamie#endif
1393195974Sjamie#ifdef INET6
1394195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1395195974Sjamie			error = EINVAL;
1396195974Sjamie			vfs_opterror(opts,
1397195974Sjamie			    "ip6 cannot be changed after creation");
1398195974Sjamie			goto done_deref_locked;
1399195974Sjamie		}
1400195974Sjamie#endif
1401191673Sjamie	}
1402185435Sbz
1403191673Sjamie	/* Do final error checking before setting anything. */
1404192895Sjamie	if (gotslevel) {
1405192895Sjamie		if (slevel < ppr->pr_securelevel) {
1406192895Sjamie			error = EPERM;
1407192895Sjamie			goto done_deref_locked;
1408192895Sjamie		}
1409192895Sjamie	}
1410194762Sjamie	if (gotchildmax) {
1411194762Sjamie		if (childmax >= ppr->pr_childmax) {
1412194762Sjamie			error = EPERM;
1413194762Sjamie			goto done_deref_locked;
1414194762Sjamie		}
1415194762Sjamie	}
1416192895Sjamie	if (gotenforce) {
1417192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1418192895Sjamie			error = EPERM;
1419192895Sjamie			goto done_deref_locked;
1420192895Sjamie		}
1421192895Sjamie	}
1422231267Smm	if (gotrsnum) {
1423231267Smm		/*
1424231267Smm		 * devfs_rsnum is a uint16_t
1425231267Smm		 */
1426232059Smm		if (rsnum < 0 || rsnum > 65535) {
1427231267Smm			error = EINVAL;
1428231267Smm			goto done_deref_locked;
1429231267Smm		}
1430231267Smm		/*
1431232059Smm		 * Nested jails always inherit parent's devfs ruleset
1432231267Smm		 */
1433231267Smm		if (jailed(td->td_ucred)) {
1434231267Smm			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1435231267Smm				error = EPERM;
1436231267Smm				goto done_deref_locked;
1437232059Smm			} else
1438231267Smm				rsnum = ppr->pr_devfs_rsnum;
1439231267Smm		}
1440231267Smm	}
1441185435Sbz#ifdef INET
1442195974Sjamie	if (ip4s > 0) {
1443192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1444195974Sjamie			/*
1445195974Sjamie			 * Make sure the new set of IP addresses is a
1446195974Sjamie			 * subset of the parent's list.  Don't worry
1447195974Sjamie			 * about the parent being unlocked, as any
1448195974Sjamie			 * setting is done with allprison_lock held.
1449195974Sjamie			 */
1450195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1451195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1452195974Sjamie					break;
1453195974Sjamie			if (ij == ppr->pr_ip4s) {
1454195974Sjamie				error = EPERM;
1455195974Sjamie				goto done_deref_locked;
1456195974Sjamie			}
1457195974Sjamie			if (ip4s > 1) {
1458195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1459195974Sjamie					if (ip4[ii].s_addr ==
1460195974Sjamie					    ppr->pr_ip4[0].s_addr)
1461195974Sjamie						continue;
1462195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1463195974Sjamie						if (ip4[ii].s_addr ==
1464195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1465195974Sjamie							break;
1466195974Sjamie					if (ij == ppr->pr_ip4s)
1467195974Sjamie						break;
1468192895Sjamie				}
1469192895Sjamie				if (ij == ppr->pr_ip4s) {
1470192895Sjamie					error = EPERM;
1471192895Sjamie					goto done_deref_locked;
1472192895Sjamie				}
1473192895Sjamie			}
1474192895Sjamie		}
1475195974Sjamie		/*
1476195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1477195974Sjamie		 * if there is no more than one IP on each jail.  If
1478195974Sjamie		 * there is a duplicate on a jail with more than one
1479195974Sjamie		 * IP stop checking and return error.
1480195974Sjamie		 */
1481195945Sjamie#ifdef VIMAGE
1482339410Sjamie		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1483195974Sjamie			if (tppr->pr_flags & PR_VNET)
1484195974Sjamie				break;
1485339410Sjamie#else
1486339410Sjamie		tppr = &prison0;
1487195945Sjamie#endif
1488195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1489195974Sjamie			if (tpr == pr ||
1490195945Sjamie#ifdef VIMAGE
1491195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1492195945Sjamie#endif
1493195974Sjamie			    tpr->pr_uref == 0) {
1494192895Sjamie				descend = 0;
1495195974Sjamie				continue;
1496195974Sjamie			}
1497195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1498195974Sjamie				continue;
1499195974Sjamie			descend = 0;
1500195974Sjamie			if (tpr->pr_ip4 == NULL ||
1501195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1502195974Sjamie				continue;
1503195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1504195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1505195974Sjamie					error = EADDRINUSE;
1506195974Sjamie					vfs_opterror(opts,
1507195974Sjamie					    "IPv4 addresses clash");
1508195974Sjamie					goto done_deref_locked;
1509192895Sjamie				}
1510192895Sjamie			}
1511192895Sjamie		}
1512192895Sjamie	}
1513185435Sbz#endif
1514191673Sjamie#ifdef INET6
1515195974Sjamie	if (ip6s > 0) {
1516192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1517195974Sjamie			/*
1518195974Sjamie			 * Make sure the new set of IP addresses is a
1519195974Sjamie			 * subset of the parent's list.
1520195974Sjamie			 */
1521195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1522195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1523195974Sjamie				    &ppr->pr_ip6[ij]))
1524195974Sjamie					break;
1525195974Sjamie			if (ij == ppr->pr_ip6s) {
1526195974Sjamie				error = EPERM;
1527195974Sjamie				goto done_deref_locked;
1528195974Sjamie			}
1529195974Sjamie			if (ip6s > 1) {
1530195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1531195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1532195974Sjamie					     &ppr->pr_ip6[0]))
1533195974Sjamie						continue;
1534195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1535195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1536195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1537195974Sjamie							break;
1538195974Sjamie					if (ij == ppr->pr_ip6s)
1539195974Sjamie						break;
1540192895Sjamie				}
1541192895Sjamie				if (ij == ppr->pr_ip6s) {
1542192895Sjamie					error = EPERM;
1543192895Sjamie					goto done_deref_locked;
1544192895Sjamie				}
1545192895Sjamie			}
1546192895Sjamie		}
1547195974Sjamie		/* Check for conflicting IP addresses. */
1548195945Sjamie#ifdef VIMAGE
1549339410Sjamie		for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1550195974Sjamie			if (tppr->pr_flags & PR_VNET)
1551195974Sjamie				break;
1552339410Sjamie#else
1553339410Sjamie		tppr = &prison0;
1554195945Sjamie#endif
1555195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1556195974Sjamie			if (tpr == pr ||
1557195945Sjamie#ifdef VIMAGE
1558195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1559195945Sjamie#endif
1560195974Sjamie			    tpr->pr_uref == 0) {
1561192895Sjamie				descend = 0;
1562195974Sjamie				continue;
1563195974Sjamie			}
1564195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1565195974Sjamie				continue;
1566195974Sjamie			descend = 0;
1567195974Sjamie			if (tpr->pr_ip6 == NULL ||
1568195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1569195974Sjamie				continue;
1570195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1571195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1572195974Sjamie					error = EADDRINUSE;
1573195974Sjamie					vfs_opterror(opts,
1574195974Sjamie					    "IPv6 addresses clash");
1575195974Sjamie					goto done_deref_locked;
1576192895Sjamie				}
1577192895Sjamie			}
1578191673Sjamie		}
1579192895Sjamie	}
1580191673Sjamie#endif
1581192895Sjamie	onamelen = namelen = 0;
1582298833Sjamie	if (namelc != NULL) {
1583292416Sjamie		/* Give a default name of the jid.  Also allow the name to be
1584292416Sjamie		 * explicitly the jid - but not any other number, and only in
1585292416Sjamie		 * normal form (no leading zero/etc).
1586292416Sjamie		 */
1587298833Sjamie		if (namelc[0] == '\0')
1588298833Sjamie			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1589292416Sjamie		else if ((strtoul(namelc, &p, 10) != jid ||
1590292416Sjamie			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1591191673Sjamie			error = EINVAL;
1592196835Sjamie			vfs_opterror(opts,
1593196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1594192895Sjamie			goto done_deref_locked;
1595191673Sjamie		}
1596191673Sjamie		/*
1597192895Sjamie		 * Make sure the name isn't too long for the prison or its
1598192895Sjamie		 * children.
1599191673Sjamie		 */
1600298833Sjamie		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1601298833Sjamie		onamelen = strlen(pr->pr_name + pnamelen);
1602298833Sjamie		namelen = strlen(namelc);
1603298833Sjamie		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1604192895Sjamie			error = ENAMETOOLONG;
1605192895Sjamie			goto done_deref_locked;
1606192895Sjamie		}
1607192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1608192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1609192895Sjamie			    sizeof(pr->pr_name)) {
1610192895Sjamie				error = ENAMETOOLONG;
1611192895Sjamie				goto done_deref_locked;
1612192895Sjamie			}
1613192895Sjamie		}
1614191673Sjamie	}
1615192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1616192895Sjamie		error = EPERM;
1617192895Sjamie		goto done_deref_locked;
1618192895Sjamie	}
1619185435Sbz
1620298833Sjamie	/*
1621298833Sjamie	 * Let modules check their parameters.  This requires unlocking and
1622298833Sjamie	 * then re-locking the prison, but this is still a valid state as long
1623298833Sjamie	 * as allprison_lock remains xlocked.
1624298833Sjamie	 */
1625298833Sjamie	mtx_unlock(&pr->pr_mtx);
1626298833Sjamie	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1627298833Sjamie	if (error != 0) {
1628298833Sjamie		prison_deref(pr, created
1629298833Sjamie		    ? PD_LIST_XLOCKED
1630298833Sjamie		    : PD_DEREF | PD_LIST_XLOCKED);
1631298833Sjamie		goto done_releroot;
1632298833Sjamie	}
1633298833Sjamie	mtx_lock(&pr->pr_mtx);
1634298833Sjamie
1635298833Sjamie	/* At this point, all valid parameters should have been noted. */
1636298833Sjamie	TAILQ_FOREACH(opt, opts, link) {
1637298833Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1638298833Sjamie			error = EINVAL;
1639298833Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
1640298833Sjamie			goto done_deref_locked;
1641298833Sjamie		}
1642298833Sjamie	}
1643298833Sjamie
1644191673Sjamie	/* Set the parameters of the prison. */
1645191673Sjamie#ifdef INET
1646192895Sjamie	redo_ip4 = 0;
1647195974Sjamie	if (pr_flags & PR_IP4_USER) {
1648195974Sjamie		pr->pr_flags |= PR_IP4;
1649195974Sjamie		free(pr->pr_ip4, M_PRISON);
1650195974Sjamie		pr->pr_ip4s = ip4s;
1651195974Sjamie		pr->pr_ip4 = ip4;
1652195974Sjamie		ip4 = NULL;
1653192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1654195945Sjamie#ifdef VIMAGE
1655195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1656195945Sjamie				descend = 0;
1657195945Sjamie				continue;
1658195945Sjamie			}
1659195945Sjamie#endif
1660192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1661192895Sjamie				redo_ip4 = 1;
1662192895Sjamie				descend = 0;
1663192895Sjamie			}
1664192895Sjamie		}
1665185435Sbz	}
1666191673Sjamie#endif
1667191673Sjamie#ifdef INET6
1668192895Sjamie	redo_ip6 = 0;
1669195974Sjamie	if (pr_flags & PR_IP6_USER) {
1670195974Sjamie		pr->pr_flags |= PR_IP6;
1671195974Sjamie		free(pr->pr_ip6, M_PRISON);
1672195974Sjamie		pr->pr_ip6s = ip6s;
1673195974Sjamie		pr->pr_ip6 = ip6;
1674195974Sjamie		ip6 = NULL;
1675192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1676195945Sjamie#ifdef VIMAGE
1677195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1678195945Sjamie				descend = 0;
1679195945Sjamie				continue;
1680195945Sjamie			}
1681195945Sjamie#endif
1682192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1683192895Sjamie				redo_ip6 = 1;
1684192895Sjamie				descend = 0;
1685192895Sjamie			}
1686192895Sjamie		}
1687191673Sjamie	}
1688191673Sjamie#endif
1689192895Sjamie	if (gotslevel) {
1690191673Sjamie		pr->pr_securelevel = slevel;
1691192895Sjamie		/* Set all child jails to be at least this level. */
1692192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1693192895Sjamie			if (tpr->pr_securelevel < slevel)
1694192895Sjamie				tpr->pr_securelevel = slevel;
1695192895Sjamie	}
1696194762Sjamie	if (gotchildmax) {
1697194762Sjamie		pr->pr_childmax = childmax;
1698194762Sjamie		/* Set all child jails to under this limit. */
1699194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1700194762Sjamie			if (tpr->pr_childmax > childmax - level)
1701194762Sjamie				tpr->pr_childmax = childmax > level
1702194762Sjamie				    ? childmax - level : 0;
1703194762Sjamie	}
1704192895Sjamie	if (gotenforce) {
1705192895Sjamie		pr->pr_enforce_statfs = enforce;
1706192895Sjamie		/* Pass this restriction on to the children. */
1707192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1708192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1709192895Sjamie				tpr->pr_enforce_statfs = enforce;
1710192895Sjamie	}
1711231267Smm	if (gotrsnum) {
1712231267Smm		pr->pr_devfs_rsnum = rsnum;
1713231267Smm		/* Pass this restriction on to the children. */
1714231267Smm		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1715232059Smm			tpr->pr_devfs_rsnum = rsnum;
1716231267Smm	}
1717298833Sjamie	if (namelc != NULL) {
1718192895Sjamie		if (ppr == &prison0)
1719298833Sjamie			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1720192895Sjamie		else
1721192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1722298833Sjamie			    ppr->pr_name, namelc);
1723192895Sjamie		/* Change this component of child names. */
1724192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1725192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1726192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1727192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1728192895Sjamie		}
1729192895Sjamie	}
1730191673Sjamie	if (path != NULL) {
1731192895Sjamie		/* Try to keep a real-rooted full pathname. */
1732230129Smm		if (fullpath_disabled && path[0] == '/' &&
1733230129Smm		    strcmp(mypr->pr_path, "/"))
1734192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1735192895Sjamie			    mypr->pr_path, path);
1736192895Sjamie		else
1737192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1738191673Sjamie		pr->pr_root = root;
1739191673Sjamie	}
1740193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1741193066Sjamie		if (pr->pr_flags & PR_HOST) {
1742193066Sjamie			/*
1743193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1744193066Sjamie			 * the lack of a lock on the parent is not a problem;
1745193066Sjamie			 * it is always set with allprison_lock at least
1746193066Sjamie			 * shared, and is held exclusively here.
1747193066Sjamie			 */
1748194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1749194118Sjamie			    sizeof(pr->pr_hostname));
1750194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1751194118Sjamie			    sizeof(pr->pr_domainname));
1752194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1753194118Sjamie			    sizeof(pr->pr_hostuuid));
1754193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1755193066Sjamie		}
1756193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1757193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1758193066Sjamie		if (host != NULL)
1759194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1760193066Sjamie		if (domain != NULL)
1761194118Sjamie			strlcpy(pr->pr_domainname, domain,
1762194118Sjamie			    sizeof(pr->pr_domainname));
1763193066Sjamie		if (uuid != NULL)
1764194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1765193066Sjamie		if (gothid)
1766193066Sjamie			pr->pr_hostid = hid;
1767193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1768193066Sjamie			if (tpr->pr_flags & PR_HOST)
1769193066Sjamie				descend = 0;
1770193066Sjamie			else {
1771193066Sjamie				if (host != NULL)
1772194118Sjamie					strlcpy(tpr->pr_hostname,
1773194118Sjamie					    pr->pr_hostname,
1774194118Sjamie					    sizeof(tpr->pr_hostname));
1775193066Sjamie				if (domain != NULL)
1776194118Sjamie					strlcpy(tpr->pr_domainname,
1777194118Sjamie					    pr->pr_domainname,
1778194118Sjamie					    sizeof(tpr->pr_domainname));
1779193066Sjamie				if (uuid != NULL)
1780194118Sjamie					strlcpy(tpr->pr_hostuuid,
1781194118Sjamie					    pr->pr_hostuuid,
1782194118Sjamie					    sizeof(tpr->pr_hostuuid));
1783193066Sjamie				if (gothid)
1784193066Sjamie					tpr->pr_hostid = hid;
1785193066Sjamie			}
1786193066Sjamie		}
1787193066Sjamie	}
1788192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1789192895Sjamie		/* Clear allow bits in all children. */
1790192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1791192895Sjamie			tpr->pr_allow &= ~tallow;
1792192895Sjamie	}
1793192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1794191673Sjamie	/*
1795191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1796191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1797191673Sjamie	 * for now, so new ones will remain unseen until after the module
1798191673Sjamie	 * handlers have completed.
1799191673Sjamie	 */
1800298833Sjamie	born = pr->pr_uref == 0;
1801191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1802191673Sjamie		if (pr_flags & PR_PERSIST) {
1803191673Sjamie			pr->pr_ref++;
1804191673Sjamie			pr->pr_uref++;
1805191673Sjamie		} else {
1806191673Sjamie			pr->pr_ref--;
1807191673Sjamie			pr->pr_uref--;
1808191673Sjamie		}
1809191673Sjamie	}
1810191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1811191673Sjamie	mtx_unlock(&pr->pr_mtx);
1812185435Sbz
1813221362Strasz#ifdef RACCT
1814284665Strasz	if (racct_enable && created)
1815221362Strasz		prison_racct_attach(pr);
1816221362Strasz#endif
1817221362Strasz
1818192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1819192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1820192895Sjamie	 */
1821192895Sjamie#ifdef INET
1822192895Sjamie	while (redo_ip4) {
1823192895Sjamie		ip4s = pr->pr_ip4s;
1824192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1825192895Sjamie		mtx_lock(&pr->pr_mtx);
1826192895Sjamie		redo_ip4 = 0;
1827192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1828195945Sjamie#ifdef VIMAGE
1829195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1830195945Sjamie				descend = 0;
1831195945Sjamie				continue;
1832195945Sjamie			}
1833195945Sjamie#endif
1834192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1835192895Sjamie				if (ip4 != NULL)
1836192895Sjamie					ip4 = NULL;
1837192895Sjamie				else
1838192895Sjamie					redo_ip4 = 1;
1839192895Sjamie			}
1840192895Sjamie		}
1841192895Sjamie		mtx_unlock(&pr->pr_mtx);
1842192895Sjamie	}
1843192895Sjamie#endif
1844192895Sjamie#ifdef INET6
1845192895Sjamie	while (redo_ip6) {
1846192895Sjamie		ip6s = pr->pr_ip6s;
1847192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1848192895Sjamie		mtx_lock(&pr->pr_mtx);
1849192895Sjamie		redo_ip6 = 0;
1850192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1851195945Sjamie#ifdef VIMAGE
1852195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1853195945Sjamie				descend = 0;
1854195945Sjamie				continue;
1855195945Sjamie			}
1856195945Sjamie#endif
1857192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1858192895Sjamie				if (ip6 != NULL)
1859192895Sjamie					ip6 = NULL;
1860192895Sjamie				else
1861192895Sjamie					redo_ip6 = 1;
1862192895Sjamie			}
1863192895Sjamie		}
1864192895Sjamie		mtx_unlock(&pr->pr_mtx);
1865192895Sjamie	}
1866192895Sjamie#endif
1867192895Sjamie
1868191673Sjamie	/* Let the modules do their work. */
1869191673Sjamie	sx_downgrade(&allprison_lock);
1870298833Sjamie	if (born) {
1871191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1872191673Sjamie		if (error) {
1873298833Sjamie			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1874298833Sjamie			prison_deref(pr, created
1875298833Sjamie			    ? PD_LIST_SLOCKED
1876298833Sjamie			    : PD_DEREF | PD_LIST_SLOCKED);
1877191673Sjamie			goto done_errmsg;
1878191673Sjamie		}
1879191673Sjamie	}
1880191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1881191673Sjamie	if (error) {
1882298833Sjamie		if (born)
1883298833Sjamie			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1884191673Sjamie		prison_deref(pr, created
1885191673Sjamie		    ? PD_LIST_SLOCKED
1886191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1887191673Sjamie		goto done_errmsg;
1888191673Sjamie	}
1889191673Sjamie
1890191673Sjamie	/* Attach this process to the prison if requested. */
1891191673Sjamie	if (flags & JAIL_ATTACH) {
1892191673Sjamie		mtx_lock(&pr->pr_mtx);
1893191673Sjamie		error = do_jail_attach(td, pr);
1894191673Sjamie		if (error) {
1895191673Sjamie			vfs_opterror(opts, "attach failed");
1896191673Sjamie			if (!created)
1897191673Sjamie				prison_deref(pr, PD_DEREF);
1898191673Sjamie			goto done_errmsg;
1899191673Sjamie		}
1900191673Sjamie	}
1901191673Sjamie
1902235803Strasz#ifdef RACCT
1903284665Strasz	if (racct_enable && !created) {
1904271622Strasz		if (!(flags & JAIL_ATTACH))
1905271622Strasz			sx_sunlock(&allprison_lock);
1906235803Strasz		prison_racct_modify(pr);
1907271622Strasz		if (!(flags & JAIL_ATTACH))
1908271622Strasz			sx_slock(&allprison_lock);
1909235803Strasz	}
1910235803Strasz#endif
1911235803Strasz
1912235803Strasz	td->td_retval[0] = pr->pr_id;
1913235803Strasz
1914191673Sjamie	/*
1915191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1916191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1917191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1918191673Sjamie	 * visible).
1919191673Sjamie	 */
1920191673Sjamie	if (!created) {
1921191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1922191673Sjamie		    ? PD_DEREF
1923191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1924191673Sjamie	} else {
1925191673Sjamie		if (pr_flags & PR_PERSIST) {
1926191673Sjamie			mtx_lock(&pr->pr_mtx);
1927191673Sjamie			pr->pr_ref++;
1928191673Sjamie			pr->pr_uref++;
1929191673Sjamie			mtx_unlock(&pr->pr_mtx);
1930191673Sjamie		}
1931191673Sjamie		if (!(flags & JAIL_ATTACH))
1932191673Sjamie			sx_sunlock(&allprison_lock);
1933191673Sjamie	}
1934232598Strasz
1935298833Sjamie	goto done_free;
1936191673Sjamie
1937192895Sjamie done_deref_locked:
1938192895Sjamie	prison_deref(pr, created
1939192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1940192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1941192895Sjamie	goto done_releroot;
1942191673Sjamie done_unlock_list:
1943191673Sjamie	sx_xunlock(&allprison_lock);
1944191673Sjamie done_releroot:
1945241896Skib	if (root != NULL)
1946191673Sjamie		vrele(root);
1947191673Sjamie done_errmsg:
1948191673Sjamie	if (error) {
1949301908Sjamie		if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1950301908Sjamie		    &errmsg_len) == 0 && errmsg_len > 0) {
1951191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1952301908Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
1953301908Sjamie				bcopy(errmsg,
1954301908Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
1955301908Sjamie				    errmsg_len);
1956301908Sjamie			else
1957301908Sjamie				copyout(errmsg,
1958301908Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
1959301908Sjamie				    errmsg_len);
1960191673Sjamie		}
1961191673Sjamie	}
1962191673Sjamie done_free:
1963191673Sjamie#ifdef INET
1964191673Sjamie	free(ip4, M_PRISON);
1965191673Sjamie#endif
1966191673Sjamie#ifdef INET6
1967191673Sjamie	free(ip6, M_PRISON);
1968191673Sjamie#endif
1969230407Smm	if (g_path != NULL)
1970230407Smm		free(g_path, M_TEMP);
1971191673Sjamie	vfs_freeopts(opts);
1972191673Sjamie	return (error);
1973191673Sjamie}
1974191673Sjamie
1975191673Sjamie
197682710Sdillon/*
1977191673Sjamie * struct jail_get_args {
1978191673Sjamie *	struct iovec *iovp;
1979191673Sjamie *	unsigned int iovcnt;
1980191673Sjamie *	int flags;
1981114168Smike * };
198282710Sdillon */
198346155Sphkint
1984225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap)
198546155Sphk{
1986191673Sjamie	struct uio *auio;
1987185435Sbz	int error;
1988185435Sbz
1989191673Sjamie	/* Check that we have an even number of iovecs. */
1990191673Sjamie	if (uap->iovcnt & 1)
1991191673Sjamie		return (EINVAL);
1992191673Sjamie
1993191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1994185435Sbz	if (error)
1995185435Sbz		return (error);
1996191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1997191673Sjamie	if (error == 0)
1998191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1999191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
2000191673Sjamie	free(auio, M_IOV);
2001191673Sjamie	return (error);
2002191673Sjamie}
2003185435Sbz
2004191673Sjamieint
2005191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
2006191673Sjamie{
2007192895Sjamie	struct prison *pr, *mypr;
2008191673Sjamie	struct vfsopt *opt;
2009191673Sjamie	struct vfsoptlist *opts;
2010191673Sjamie	char *errmsg, *name;
2011192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
2012185435Sbz
2013191673Sjamie	if (flags & ~JAIL_GET_MASK)
2014191673Sjamie		return (EINVAL);
2015185435Sbz
2016191673Sjamie	/* Get the parameter list. */
2017191673Sjamie	error = vfs_buildopts(optuio, &opts);
2018191673Sjamie	if (error)
2019191673Sjamie		return (error);
2020191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2021192895Sjamie	mypr = td->td_ucred->cr_prison;
2022185435Sbz
2023191673Sjamie	/*
2024191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
2025191673Sjamie	 */
2026191673Sjamie	sx_slock(&allprison_lock);
2027191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2028191673Sjamie	if (error == 0) {
2029191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
2030192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
2031191673Sjamie				mtx_lock(&pr->pr_mtx);
2032191673Sjamie				if (pr->pr_ref > 0 &&
2033191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
2034191673Sjamie					break;
2035191673Sjamie				mtx_unlock(&pr->pr_mtx);
2036191673Sjamie			}
2037191673Sjamie		}
2038191673Sjamie		if (pr != NULL)
2039191673Sjamie			goto found_prison;
2040191673Sjamie		error = ENOENT;
2041191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
2042191673Sjamie		goto done_unlock_list;
2043191673Sjamie	} else if (error != ENOENT)
2044191673Sjamie		goto done_unlock_list;
2045185435Sbz
2046191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2047191673Sjamie	if (error == 0) {
2048191673Sjamie		if (jid != 0) {
2049192895Sjamie			pr = prison_find_child(mypr, jid);
2050191673Sjamie			if (pr != NULL) {
2051191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2052191673Sjamie					mtx_unlock(&pr->pr_mtx);
2053191673Sjamie					error = ENOENT;
2054191673Sjamie					vfs_opterror(opts, "jail %d is dying",
2055191673Sjamie					    jid);
2056191673Sjamie					goto done_unlock_list;
2057191673Sjamie				}
2058191673Sjamie				goto found_prison;
2059191673Sjamie			}
2060191673Sjamie			error = ENOENT;
2061191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
2062191673Sjamie			goto done_unlock_list;
2063191673Sjamie		}
2064191673Sjamie	} else if (error != ENOENT)
2065191673Sjamie		goto done_unlock_list;
206646155Sphk
2067191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
2068191673Sjamie	if (error == 0) {
2069191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
2070191673Sjamie			error = EINVAL;
2071191673Sjamie			goto done_unlock_list;
2072191673Sjamie		}
2073192895Sjamie		pr = prison_find_name(mypr, name);
2074191673Sjamie		if (pr != NULL) {
2075191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2076191673Sjamie				mtx_unlock(&pr->pr_mtx);
2077191673Sjamie				error = ENOENT;
2078191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
2079191673Sjamie				    name);
2080191673Sjamie				goto done_unlock_list;
2081191673Sjamie			}
2082191673Sjamie			goto found_prison;
2083191673Sjamie		}
2084191673Sjamie		error = ENOENT;
2085191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
2086191673Sjamie		goto done_unlock_list;
2087191673Sjamie	} else if (error != ENOENT)
2088191673Sjamie		goto done_unlock_list;
2089185435Sbz
2090191673Sjamie	vfs_opterror(opts, "no jail specified");
2091191673Sjamie	error = ENOENT;
2092191673Sjamie	goto done_unlock_list;
2093191673Sjamie
2094191673Sjamie found_prison:
2095191673Sjamie	/* Get the parameters of the prison. */
2096191673Sjamie	pr->pr_ref++;
2097191673Sjamie	locked = PD_LOCKED;
2098191673Sjamie	td->td_retval[0] = pr->pr_id;
2099191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2100191673Sjamie	if (error != 0 && error != ENOENT)
2101191673Sjamie		goto done_deref;
2102192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2103192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2104191673Sjamie	if (error != 0 && error != ENOENT)
2105191673Sjamie		goto done_deref;
2106192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2107192895Sjamie	if (error != 0 && error != ENOENT)
2108192895Sjamie		goto done_deref;
2109192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2110191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
2111191673Sjamie	if (error != 0 && error != ENOENT)
2112191673Sjamie		goto done_deref;
2113192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2114191673Sjamie	if (error != 0 && error != ENOENT)
2115191673Sjamie		goto done_deref;
2116191673Sjamie#ifdef INET
2117191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2118191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2119191673Sjamie	if (error != 0 && error != ENOENT)
2120191673Sjamie		goto done_deref;
2121191673Sjamie#endif
2122191673Sjamie#ifdef INET6
2123191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2124191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2125191673Sjamie	if (error != 0 && error != ENOENT)
2126191673Sjamie		goto done_deref;
2127191673Sjamie#endif
2128191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2129191673Sjamie	    sizeof(pr->pr_securelevel));
2130191673Sjamie	if (error != 0 && error != ENOENT)
2131191673Sjamie		goto done_deref;
2132194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2133194762Sjamie	    sizeof(pr->pr_childcount));
2134194762Sjamie	if (error != 0 && error != ENOENT)
2135194762Sjamie		goto done_deref;
2136194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2137194762Sjamie	    sizeof(pr->pr_childmax));
2138194762Sjamie	if (error != 0 && error != ENOENT)
2139194762Sjamie		goto done_deref;
2140194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2141191673Sjamie	if (error != 0 && error != ENOENT)
2142191673Sjamie		goto done_deref;
2143194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2144193066Sjamie	if (error != 0 && error != ENOENT)
2145193066Sjamie		goto done_deref;
2146194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2147193066Sjamie	if (error != 0 && error != ENOENT)
2148193066Sjamie		goto done_deref;
2149205014Snwhitehorn#ifdef COMPAT_FREEBSD32
2150217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2151193066Sjamie		uint32_t hid32 = pr->pr_hostid;
2152193066Sjamie
2153193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2154193066Sjamie	} else
2155193066Sjamie#endif
2156193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2157193066Sjamie	    sizeof(pr->pr_hostid));
2158193066Sjamie	if (error != 0 && error != ENOENT)
2159193066Sjamie		goto done_deref;
2160192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2161192895Sjamie	    sizeof(pr->pr_enforce_statfs));
2162191673Sjamie	if (error != 0 && error != ENOENT)
2163191673Sjamie		goto done_deref;
2164231267Smm	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2165231267Smm	    sizeof(pr->pr_devfs_rsnum));
2166231267Smm	if (error != 0 && error != ENOENT)
2167231267Smm		goto done_deref;
2168192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2169192895Sjamie	    fi++) {
2170192895Sjamie		if (pr_flag_names[fi] == NULL)
2171192895Sjamie			continue;
2172192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2173192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2174192895Sjamie		if (error != 0 && error != ENOENT)
2175192895Sjamie			goto done_deref;
2176192895Sjamie		i = !i;
2177192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2178192895Sjamie		if (error != 0 && error != ENOENT)
2179192895Sjamie			goto done_deref;
2180192895Sjamie	}
2181195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2182195870Sjamie	    fi++) {
2183195870Sjamie		i = pr->pr_flags &
2184195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2185195870Sjamie		i = pr_flag_jailsys[fi].disable &&
2186195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2187195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2188195870Sjamie		    : JAIL_SYS_INHERIT;
2189195870Sjamie		error =
2190195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2191195870Sjamie		if (error != 0 && error != ENOENT)
2192195870Sjamie			goto done_deref;
2193195870Sjamie	}
2194192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2195192895Sjamie	    fi++) {
2196192895Sjamie		if (pr_allow_names[fi] == NULL)
2197192895Sjamie			continue;
2198192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2199192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2200192895Sjamie		if (error != 0 && error != ENOENT)
2201192895Sjamie			goto done_deref;
2202192895Sjamie		i = !i;
2203192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2204192895Sjamie		if (error != 0 && error != ENOENT)
2205192895Sjamie			goto done_deref;
2206192895Sjamie	}
2207191673Sjamie	i = (pr->pr_uref == 0);
2208191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2209191673Sjamie	if (error != 0 && error != ENOENT)
2210191673Sjamie		goto done_deref;
2211191673Sjamie	i = !i;
2212191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2213191673Sjamie	if (error != 0 && error != ENOENT)
2214191673Sjamie		goto done_deref;
2215280632Sian	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2216280632Sian	    sizeof(pr->pr_osreldate));
2217280632Sian	if (error != 0 && error != ENOENT)
2218280632Sian		goto done_deref;
2219280632Sian	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2220280632Sian	if (error != 0 && error != ENOENT)
2221280632Sian		goto done_deref;
2222191673Sjamie
2223191673Sjamie	/* Get the module parameters. */
2224191673Sjamie	mtx_unlock(&pr->pr_mtx);
2225191673Sjamie	locked = 0;
2226191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
222746155Sphk	if (error)
2228191673Sjamie		goto done_deref;
2229191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
223084828Sjhb
2231191673Sjamie	/* By now, all parameters should have been noted. */
2232191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2233191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2234191673Sjamie			error = EINVAL;
2235191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2236191673Sjamie			goto done_errmsg;
2237191673Sjamie		}
2238185435Sbz	}
2239191673Sjamie
2240191673Sjamie	/* Write the fetched parameters back to userspace. */
2241191673Sjamie	error = 0;
2242191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2243191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2244191673Sjamie			pos = 2 * opt->pos + 1;
2245191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2246191673Sjamie			if (opt->value != NULL) {
2247191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2248191673Sjamie					bcopy(opt->value,
2249191673Sjamie					    optuio->uio_iov[pos].iov_base,
2250191673Sjamie					    opt->len);
2251191673Sjamie				} else {
2252191673Sjamie					error = copyout(opt->value,
2253191673Sjamie					    optuio->uio_iov[pos].iov_base,
2254191673Sjamie					    opt->len);
2255191673Sjamie					if (error)
2256191673Sjamie						break;
2257191673Sjamie				}
2258191673Sjamie			}
2259191673Sjamie		}
2260185435Sbz	}
2261191673Sjamie	goto done_errmsg;
2262191673Sjamie
2263191673Sjamie done_deref:
2264191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2265191673Sjamie	goto done_errmsg;
2266191673Sjamie
2267191673Sjamie done_unlock_list:
2268191673Sjamie	sx_sunlock(&allprison_lock);
2269191673Sjamie done_errmsg:
2270191673Sjamie	if (error && errmsg_pos >= 0) {
2271191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2272191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2273191673Sjamie		if (errmsg_len > 0) {
2274191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2275191673Sjamie				bcopy(errmsg,
2276191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2277191673Sjamie				    errmsg_len);
2278191673Sjamie			else
2279191673Sjamie				copyout(errmsg,
2280191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2281191673Sjamie				    errmsg_len);
2282191673Sjamie		}
2283185435Sbz	}
2284191673Sjamie	vfs_freeopts(opts);
2285191673Sjamie	return (error);
2286191673Sjamie}
2287113275Smike
2288192895Sjamie
2289191673Sjamie/*
2290191673Sjamie * struct jail_remove_args {
2291191673Sjamie *	int jid;
2292191673Sjamie * };
2293191673Sjamie */
2294191673Sjamieint
2295225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2296191673Sjamie{
2297192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2298192895Sjamie	int descend, error;
2299185435Sbz
2300191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2301185435Sbz	if (error)
2302191673Sjamie		return (error);
2303185435Sbz
2304185435Sbz	sx_xlock(&allprison_lock);
2305192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2306191673Sjamie	if (pr == NULL) {
2307185435Sbz		sx_xunlock(&allprison_lock);
2308191673Sjamie		return (EINVAL);
2309185435Sbz	}
2310185435Sbz
2311192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2312192895Sjamie	pr->pr_ref++;
2313192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2314192895Sjamie		mtx_unlock(&pr->pr_mtx);
2315192895Sjamie		lpr = NULL;
2316192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2317192895Sjamie			mtx_lock(&cpr->pr_mtx);
2318192895Sjamie			if (cpr->pr_ref > 0) {
2319192895Sjamie				tpr = cpr;
2320192895Sjamie				cpr->pr_ref++;
2321192895Sjamie			} else {
2322192895Sjamie				/* Already removed - do not do it again. */
2323192895Sjamie				tpr = NULL;
2324192895Sjamie			}
2325192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2326192895Sjamie			if (lpr != NULL) {
2327192895Sjamie				mtx_lock(&lpr->pr_mtx);
2328192895Sjamie				prison_remove_one(lpr);
2329192895Sjamie				sx_xlock(&allprison_lock);
2330192895Sjamie			}
2331192895Sjamie			lpr = tpr;
2332192895Sjamie		}
2333192895Sjamie		if (lpr != NULL) {
2334192895Sjamie			mtx_lock(&lpr->pr_mtx);
2335192895Sjamie			prison_remove_one(lpr);
2336192895Sjamie			sx_xlock(&allprison_lock);
2337192895Sjamie		}
2338192895Sjamie		mtx_lock(&pr->pr_mtx);
2339192895Sjamie	}
2340192895Sjamie	prison_remove_one(pr);
2341192895Sjamie	return (0);
2342192895Sjamie}
2343192895Sjamie
2344192895Sjamiestatic void
2345192895Sjamieprison_remove_one(struct prison *pr)
2346192895Sjamie{
2347192895Sjamie	struct proc *p;
2348192895Sjamie	int deuref;
2349192895Sjamie
2350191673Sjamie	/* If the prison was persistent, it is not anymore. */
2351191673Sjamie	deuref = 0;
2352191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2353191673Sjamie		pr->pr_ref--;
2354191673Sjamie		deuref = PD_DEUREF;
2355191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2356179881Sdelphij	}
2357113275Smike
2358192895Sjamie	/*
2359192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2360192895Sjamie	 * the prison now.
2361192895Sjamie	 */
2362192895Sjamie	KASSERT(pr->pr_ref > 0,
2363192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2364192895Sjamie	if (pr->pr_ref == 1) {
2365191673Sjamie		prison_deref(pr,
2366191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2367192895Sjamie		return;
2368191673Sjamie	}
2369191673Sjamie
2370113275Smike	mtx_unlock(&pr->pr_mtx);
2371191673Sjamie	sx_xunlock(&allprison_lock);
2372191673Sjamie	/*
2373191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2374191673Sjamie	 */
2375191673Sjamie	sx_slock(&allproc_lock);
2376191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2377191673Sjamie		PROC_LOCK(p);
2378191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2379191673Sjamie		    p->p_ucred->cr_prison == pr)
2380225617Skmacy			kern_psignal(p, SIGKILL);
2381191673Sjamie		PROC_UNLOCK(p);
2382191673Sjamie	}
2383191673Sjamie	sx_sunlock(&allproc_lock);
2384192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2385191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2386113275Smike}
2387113275Smike
2388190466Sjamie
2389113275Smike/*
2390114168Smike * struct jail_attach_args {
2391114168Smike *	int jid;
2392114168Smike * };
2393113275Smike */
2394113275Smikeint
2395225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2396113275Smike{
2397113275Smike	struct prison *pr;
2398191673Sjamie	int error;
2399167309Spjd
2400164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2401126023Snectar	if (error)
2402126023Snectar		return (error);
2403126023Snectar
2404301907Sjamie	/*
2405301907Sjamie	 * Start with exclusive hold on allprison_lock to ensure that a possible
2406301907Sjamie	 * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove.
2407301907Sjamie	 * But then immediately downgrade it since we don't need to stop
2408301907Sjamie	 * readers.
2409301907Sjamie	 */
2410301907Sjamie	sx_xlock(&allprison_lock);
2411301907Sjamie	sx_downgrade(&allprison_lock);
2412192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2413113275Smike	if (pr == NULL) {
2414168401Spjd		sx_sunlock(&allprison_lock);
2415113275Smike		return (EINVAL);
2416113275Smike	}
2417185435Sbz
2418185435Sbz	/*
2419185435Sbz	 * Do not allow a process to attach to a prison that is not
2420191673Sjamie	 * considered to be "alive".
2421185435Sbz	 */
2422191673Sjamie	if (pr->pr_uref == 0) {
2423185435Sbz		mtx_unlock(&pr->pr_mtx);
2424185435Sbz		sx_sunlock(&allprison_lock);
2425185435Sbz		return (EINVAL);
2426185435Sbz	}
2427191673Sjamie
2428191673Sjamie	return (do_jail_attach(td, pr));
2429191673Sjamie}
2430191673Sjamie
2431191673Sjamiestatic int
2432191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2433191673Sjamie{
2434191673Sjamie	struct proc *p;
2435191673Sjamie	struct ucred *newcred, *oldcred;
2436241896Skib	int error;
2437191673Sjamie
2438191673Sjamie	/*
2439191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2440191673Sjamie	 * in the same privileged process attempt to attach to two
2441191673Sjamie	 * different jails at the same time.  It is important for
2442191673Sjamie	 * user processes not to do this, or they might end up with
2443191673Sjamie	 * a process root from one prison, but attached to the jail
2444191673Sjamie	 * of another.
2445191673Sjamie	 */
2446113275Smike	pr->pr_ref++;
2447191673Sjamie	pr->pr_uref++;
2448113275Smike	mtx_unlock(&pr->pr_mtx);
2449191673Sjamie
2450191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2451191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2452191673Sjamie	if (error) {
2453191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2454191673Sjamie		return (error);
2455191673Sjamie	}
2456168401Spjd	sx_sunlock(&allprison_lock);
2457113275Smike
2458185435Sbz	/*
2459185435Sbz	 * Reparent the newly attached process to this jail.
2460185435Sbz	 */
2461191673Sjamie	p = td->td_proc;
2462185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2463185435Sbz	if (error)
2464191673Sjamie		goto e_revert_osd;
2465185435Sbz
2466175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2467113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2468113275Smike		goto e_unlock;
2469113275Smike#ifdef MAC
2470172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2471113275Smike		goto e_unlock;
2472113275Smike#endif
2473175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2474191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2475241896Skib		goto e_revert_osd;
2476113275Smike
247784828Sjhb	newcred = crget();
247884828Sjhb	PROC_LOCK(p);
2479298833Sjamie	oldcred = crcopysafe(p, newcred);
2480113630Sjhb	newcred->cr_prison = pr;
2481302229Sbdrewery	proc_set_cred(p, newcred);
2482298833Sjamie	setsugid(p);
248384828Sjhb	PROC_UNLOCK(p);
2484220137Strasz#ifdef RACCT
2485220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
2486220137Strasz#endif
2487298833Sjamie	prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
248884828Sjhb	crfree(oldcred);
248946155Sphk	return (0);
2490298833Sjamie
2491191673Sjamie e_unlock:
2492175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2493191673Sjamie e_revert_osd:
2494191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2495298833Sjamie	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2496191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
249746155Sphk	return (error);
249846155Sphk}
249946155Sphk
2500192895Sjamie
2501113275Smike/*
2502113275Smike * Returns a locked prison instance, or NULL on failure.
2503113275Smike */
2504168399Spjdstruct prison *
2505113275Smikeprison_find(int prid)
2506113275Smike{
2507113275Smike	struct prison *pr;
2508113275Smike
2509168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2510191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2511113275Smike		if (pr->pr_id == prid) {
2512113275Smike			mtx_lock(&pr->pr_mtx);
2513191673Sjamie			if (pr->pr_ref > 0)
2514191673Sjamie				return (pr);
2515191673Sjamie			mtx_unlock(&pr->pr_mtx);
2516113275Smike		}
2517113275Smike	}
2518113275Smike	return (NULL);
2519113275Smike}
2520113275Smike
2521191673Sjamie/*
2522192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2523191673Sjamie */
2524191673Sjamiestruct prison *
2525192895Sjamieprison_find_child(struct prison *mypr, int prid)
2526191673Sjamie{
2527192895Sjamie	struct prison *pr;
2528192895Sjamie	int descend;
2529192895Sjamie
2530192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2531192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2532192895Sjamie		if (pr->pr_id == prid) {
2533192895Sjamie			mtx_lock(&pr->pr_mtx);
2534192895Sjamie			if (pr->pr_ref > 0)
2535192895Sjamie				return (pr);
2536192895Sjamie			mtx_unlock(&pr->pr_mtx);
2537192895Sjamie		}
2538192895Sjamie	}
2539192895Sjamie	return (NULL);
2540192895Sjamie}
2541192895Sjamie
2542192895Sjamie/*
2543192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2544192895Sjamie */
2545192895Sjamiestruct prison *
2546192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2547192895Sjamie{
2548191673Sjamie	struct prison *pr, *deadpr;
2549192895Sjamie	size_t mylen;
2550192895Sjamie	int descend;
2551191673Sjamie
2552191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2553192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2554191673Sjamie again:
2555191673Sjamie	deadpr = NULL;
2556192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2557192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2558191673Sjamie			mtx_lock(&pr->pr_mtx);
2559191673Sjamie			if (pr->pr_ref > 0) {
2560191673Sjamie				if (pr->pr_uref > 0)
2561191673Sjamie					return (pr);
2562191673Sjamie				deadpr = pr;
2563191673Sjamie			}
2564191673Sjamie			mtx_unlock(&pr->pr_mtx);
2565191673Sjamie		}
2566191673Sjamie	}
2567192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2568191673Sjamie	if (deadpr != NULL) {
2569191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2570191673Sjamie		if (deadpr->pr_ref == 0) {
2571191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2572191673Sjamie			goto again;
2573191673Sjamie		}
2574191673Sjamie	}
2575191673Sjamie	return (deadpr);
2576191673Sjamie}
2577191673Sjamie
2578191673Sjamie/*
2579192895Sjamie * See if a prison has the specific flag set.
2580192895Sjamie */
2581192895Sjamieint
2582192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2583192895Sjamie{
2584192895Sjamie
2585192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2586192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2587192895Sjamie}
2588192895Sjamie
2589192895Sjamieint
2590192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2591192895Sjamie{
2592192895Sjamie
2593192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2594192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2595192895Sjamie}
2596192895Sjamie
2597192895Sjamie/*
2598191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2599191673Sjamie * prison itself - but not in this context in case there are locks held.
2600191673Sjamie */
260172786Srwatsonvoid
2602185029Spjdprison_free_locked(struct prison *pr)
260372786Srwatson{
2604298833Sjamie	int ref;
260572786Srwatson
2606185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2607298833Sjamie	ref = --pr->pr_ref;
2608298833Sjamie	mtx_unlock(&pr->pr_mtx);
2609298833Sjamie	if (ref == 0)
2610144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
261172786Srwatson}
261272786Srwatson
2613185029Spjdvoid
2614185029Spjdprison_free(struct prison *pr)
2615185029Spjd{
2616185029Spjd
2617185029Spjd	mtx_lock(&pr->pr_mtx);
2618185029Spjd	prison_free_locked(pr);
2619185029Spjd}
2620185029Spjd
2621298833Sjamie/*
2622298833Sjamie * Complete a call to either prison_free or prison_proc_free.
2623298833Sjamie */
2624124882Srwatsonstatic void
2625124882Srwatsonprison_complete(void *context, int pending)
2626124882Srwatson{
2627298833Sjamie	struct prison *pr = context;
2628191673Sjamie
2629301907Sjamie	sx_xlock(&allprison_lock);
2630298833Sjamie	mtx_lock(&pr->pr_mtx);
2631298833Sjamie	prison_deref(pr, pr->pr_uref
2632301907Sjamie	    ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED
2633301907Sjamie	    : PD_LOCKED | PD_LIST_XLOCKED);
2634191673Sjamie}
2635191673Sjamie
2636191673Sjamie/*
2637191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2638191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2639191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2640191673Sjamie * and the allprison lock are both unlocked.
2641191673Sjamie */
2642191673Sjamiestatic void
2643191673Sjamieprison_deref(struct prison *pr, int flags)
2644191673Sjamie{
2645192895Sjamie	struct prison *ppr, *tpr;
2646298833Sjamie	int ref, lasturef;
2647124882Srwatson
2648191673Sjamie	if (!(flags & PD_LOCKED))
2649191673Sjamie		mtx_lock(&pr->pr_mtx);
2650225191Sjamie	for (;;) {
2651225191Sjamie		if (flags & PD_DEUREF) {
2652298833Sjamie			KASSERT(pr->pr_uref > 0,
2653298833Sjamie			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2654298833Sjamie			     pr->pr_id));
2655225191Sjamie			pr->pr_uref--;
2656298833Sjamie			lasturef = pr->pr_uref == 0;
2657298833Sjamie			if (lasturef)
2658298833Sjamie				pr->pr_ref++;
2659225191Sjamie			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2660298833Sjamie		} else
2661298833Sjamie			lasturef = 0;
2662298833Sjamie		if (flags & PD_DEREF) {
2663298833Sjamie			KASSERT(pr->pr_ref > 0,
2664298833Sjamie			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2665298833Sjamie			     pr->pr_id));
2666298833Sjamie			pr->pr_ref--;
2667192895Sjamie		}
2668298833Sjamie		ref = pr->pr_ref;
2669298833Sjamie		mtx_unlock(&pr->pr_mtx);
2670298833Sjamie
2671298833Sjamie		/*
2672298833Sjamie		 * Tell the modules if the last user reference was removed
2673298833Sjamie		 * (even it sticks around in dying state).
2674298833Sjamie		 */
2675298833Sjamie		if (lasturef) {
2676298833Sjamie			if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
2677301907Sjamie				sx_xlock(&allprison_lock);
2678301907Sjamie				flags |= PD_LIST_XLOCKED;
2679298833Sjamie			}
2680298833Sjamie			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2681298833Sjamie			mtx_lock(&pr->pr_mtx);
2682298833Sjamie			ref = --pr->pr_ref;
2683298833Sjamie			mtx_unlock(&pr->pr_mtx);
2684298833Sjamie		}
2685298833Sjamie
2686192895Sjamie		/* If the prison still has references, nothing else to do. */
2687298833Sjamie		if (ref > 0) {
2688192895Sjamie			if (flags & PD_LIST_SLOCKED)
2689192895Sjamie				sx_sunlock(&allprison_lock);
2690192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2691192895Sjamie				sx_xunlock(&allprison_lock);
2692192895Sjamie			return;
2693191673Sjamie		}
2694191673Sjamie
2695192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2696192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2697192895Sjamie				sx_sunlock(&allprison_lock);
2698192895Sjamie				sx_xlock(&allprison_lock);
2699192895Sjamie			}
2700192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2701192895Sjamie			sx_xlock(&allprison_lock);
2702168489Spjd
2703192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2704192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2705192895Sjamie		ppr = pr->pr_parent;
2706192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2707194762Sjamie			tpr->pr_childcount--;
2708196592Sjamie		sx_xunlock(&allprison_lock);
2709192895Sjamie
2710194251Sjamie#ifdef VIMAGE
2711196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2712194251Sjamie			vnet_destroy(pr->pr_vnet);
2713194251Sjamie#endif
2714241896Skib		if (pr->pr_root != NULL)
2715192895Sjamie			vrele(pr->pr_root);
2716192895Sjamie		mtx_destroy(&pr->pr_mtx);
2717191673Sjamie#ifdef INET
2718192895Sjamie		free(pr->pr_ip4, M_PRISON);
2719191673Sjamie#endif
2720185435Sbz#ifdef INET6
2721192895Sjamie		free(pr->pr_ip6, M_PRISON);
2722185435Sbz#endif
2723192895Sjamie		if (pr->pr_cpuset != NULL)
2724192895Sjamie			cpuset_rel(pr->pr_cpuset);
2725192895Sjamie		osd_jail_exit(pr);
2726221362Strasz#ifdef RACCT
2727284665Strasz		if (racct_enable)
2728284665Strasz			prison_racct_detach(pr);
2729220163Strasz#endif
2730192895Sjamie		free(pr, M_PRISON);
2731192895Sjamie
2732192895Sjamie		/* Removing a prison frees a reference on its parent. */
2733192895Sjamie		pr = ppr;
2734192895Sjamie		mtx_lock(&pr->pr_mtx);
2735225191Sjamie		flags = PD_DEREF | PD_DEUREF;
2736192895Sjamie	}
2737124882Srwatson}
2738124882Srwatson
273972786Srwatsonvoid
2740185029Spjdprison_hold_locked(struct prison *pr)
274172786Srwatson{
274272786Srwatson
2743185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2744168489Spjd	KASSERT(pr->pr_ref > 0,
2745191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
274672786Srwatson	pr->pr_ref++;
2747185029Spjd}
2748185029Spjd
2749185029Spjdvoid
2750185029Spjdprison_hold(struct prison *pr)
2751185029Spjd{
2752185029Spjd
2753185029Spjd	mtx_lock(&pr->pr_mtx);
2754185029Spjd	prison_hold_locked(pr);
275587275Srwatson	mtx_unlock(&pr->pr_mtx);
275672786Srwatson}
275772786Srwatson
2758185435Sbzvoid
2759185435Sbzprison_proc_hold(struct prison *pr)
276087275Srwatson{
276187275Srwatson
2762185435Sbz	mtx_lock(&pr->pr_mtx);
2763191673Sjamie	KASSERT(pr->pr_uref > 0,
2764191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2765191673Sjamie	pr->pr_uref++;
2766185435Sbz	mtx_unlock(&pr->pr_mtx);
276787275Srwatson}
276887275Srwatson
2769185435Sbzvoid
2770185435Sbzprison_proc_free(struct prison *pr)
2771185435Sbz{
2772185435Sbz
2773185435Sbz	mtx_lock(&pr->pr_mtx);
2774191673Sjamie	KASSERT(pr->pr_uref > 0,
2775191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2776298833Sjamie	if (pr->pr_uref > 1)
2777298833Sjamie		pr->pr_uref--;
2778298833Sjamie	else {
2779298833Sjamie		/*
2780298833Sjamie		 * Don't remove the last user reference in this context, which
2781298833Sjamie		 * is expected to be a process that is not only locked, but
2782298833Sjamie		 * also half dead.
2783298833Sjamie		 */
2784298833Sjamie		pr->pr_ref++;
2785298833Sjamie		mtx_unlock(&pr->pr_mtx);
2786298833Sjamie		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2787298833Sjamie		return;
2788298833Sjamie	}
2789298833Sjamie	mtx_unlock(&pr->pr_mtx);
2790185435Sbz}
2791185435Sbz
2792185435Sbz
2793185435Sbz#ifdef INET
2794185435Sbz/*
2795192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2796192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2797192895Sjamie */
2798192895Sjamiestatic int
2799192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2800192895Sjamie{
2801192895Sjamie	int ii, ij, used;
2802192895Sjamie	struct prison *ppr;
2803192895Sjamie
2804192895Sjamie	ppr = pr->pr_parent;
2805192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2806192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2807192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2808192895Sjamie			/*
2809192895Sjamie			 * There's no room for the parent's list.  Use the
2810192895Sjamie			 * new list buffer, which is assumed to be big enough
2811192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2812192895Sjamie			 * allocate one.
2813192895Sjamie			 */
2814192895Sjamie			used = 1;
2815192895Sjamie			if (newip4 == NULL) {
2816192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2817192895Sjamie				    M_PRISON, M_NOWAIT);
2818192895Sjamie				if (newip4 != NULL)
2819192895Sjamie					used = 0;
2820192895Sjamie			}
2821192895Sjamie			if (newip4 != NULL) {
2822192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2823192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2824192895Sjamie				free(pr->pr_ip4, M_PRISON);
2825192895Sjamie				pr->pr_ip4 = newip4;
2826192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2827192895Sjamie			}
2828192895Sjamie			return (used);
2829192895Sjamie		}
2830192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2831192895Sjamie		if (pr->pr_ip4s > 0)
2832192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2833192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2834192895Sjamie		else if (pr->pr_ip4 != NULL) {
2835192895Sjamie			free(pr->pr_ip4, M_PRISON);
2836192895Sjamie			pr->pr_ip4 = NULL;
2837192895Sjamie		}
2838195974Sjamie	} else if (pr->pr_ip4s > 0) {
2839192895Sjamie		/* Remove addresses that aren't in the parent. */
2840192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2841192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2842192895Sjamie				break;
2843192895Sjamie		if (ij < ppr->pr_ip4s)
2844192895Sjamie			ii = 1;
2845192895Sjamie		else {
2846192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2847192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2848192895Sjamie			ii = 0;
2849192895Sjamie		}
2850192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2851192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2852192895Sjamie				ii++;
2853192895Sjamie				continue;
2854192895Sjamie			}
2855192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2856192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2857192895Sjamie			case -1:
2858192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2859192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2860192895Sjamie				break;
2861192895Sjamie			case 0:
2862192895Sjamie				ii++;
2863192895Sjamie				ij++;
2864192895Sjamie				break;
2865192895Sjamie			case 1:
2866192895Sjamie				ij++;
2867192895Sjamie				break;
2868192895Sjamie			}
2869192895Sjamie		}
2870192895Sjamie		if (pr->pr_ip4s == 0) {
2871195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2872192895Sjamie			free(pr->pr_ip4, M_PRISON);
2873192895Sjamie			pr->pr_ip4 = NULL;
2874192895Sjamie		}
2875192895Sjamie	}
2876192895Sjamie	return (0);
2877192895Sjamie}
2878192895Sjamie
2879192895Sjamie/*
2880185435Sbz * Pass back primary IPv4 address of this jail.
2881185435Sbz *
2882192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2883192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2884185435Sbz *
2885188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2886188144Sjamie * Address returned in NBO.
2887185435Sbz */
288846155Sphkint
2889187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
289046155Sphk{
2891191673Sjamie	struct prison *pr;
289246155Sphk
2893185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2894185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2895185435Sbz
2896192895Sjamie	pr = cred->cr_prison;
2897192895Sjamie	if (!(pr->pr_flags & PR_IP4))
289846155Sphk		return (0);
2899191673Sjamie	mtx_lock(&pr->pr_mtx);
2900192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2901192895Sjamie		mtx_unlock(&pr->pr_mtx);
2902192895Sjamie		return (0);
2903192895Sjamie	}
2904191673Sjamie	if (pr->pr_ip4 == NULL) {
2905191673Sjamie		mtx_unlock(&pr->pr_mtx);
2906188144Sjamie		return (EAFNOSUPPORT);
2907191673Sjamie	}
2908185435Sbz
2909191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2910191673Sjamie	mtx_unlock(&pr->pr_mtx);
2911185435Sbz	return (0);
2912185435Sbz}
2913185435Sbz
2914185435Sbz/*
2915202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2916202468Sbz * We will return 0 if we should bypass source address selection in favour
2917202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2918202468Sbz * returned in NBO.
2919202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2920202468Sbz */
2921202468Sbzint
2922202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2923202468Sbz{
2924202468Sbz	struct prison *pr;
2925202468Sbz	struct in_addr lia;
2926202468Sbz	int error;
2927202468Sbz
2928202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2929202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2930202468Sbz
2931202468Sbz	if (!jailed(cred))
2932202468Sbz		return (1);
2933202468Sbz
2934202468Sbz	pr = cred->cr_prison;
2935202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2936202468Sbz		return (1);
2937202468Sbz
2938202468Sbz	lia.s_addr = INADDR_ANY;
2939202468Sbz	error = prison_get_ip4(cred, &lia);
2940202468Sbz	if (error)
2941202468Sbz		return (error);
2942202468Sbz	if (lia.s_addr == INADDR_ANY)
2943202468Sbz		return (1);
2944202468Sbz
2945202468Sbz	ia->s_addr = lia.s_addr;
2946202468Sbz	return (0);
2947202468Sbz}
2948202468Sbz
2949202468Sbz/*
2950192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2951192895Sjamie */
2952192895Sjamieint
2953192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2954192895Sjamie{
2955192895Sjamie
2956192895Sjamie	if (pr1 == pr2)
2957192895Sjamie		return (1);
2958192895Sjamie
2959192895Sjamie	/*
2960195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2961195974Sjamie	 * existing prisons.
2962192895Sjamie	 */
2963195945Sjamie	while (pr1 != &prison0 &&
2964195945Sjamie#ifdef VIMAGE
2965195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2966195945Sjamie#endif
2967195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2968192895Sjamie		pr1 = pr1->pr_parent;
2969195945Sjamie	while (pr2 != &prison0 &&
2970195945Sjamie#ifdef VIMAGE
2971195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2972195945Sjamie#endif
2973195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2974192895Sjamie		pr2 = pr2->pr_parent;
2975192895Sjamie	return (pr1 == pr2);
2976192895Sjamie}
2977192895Sjamie
2978192895Sjamie/*
2979185435Sbz * Make sure our (source) address is set to something meaningful to this
2980185435Sbz * jail.
2981185435Sbz *
2982192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2983192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2984192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2985185435Sbz */
2986185435Sbzint
2987185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2988185435Sbz{
2989191673Sjamie	struct prison *pr;
2990185435Sbz	struct in_addr ia0;
2991191673Sjamie	int error;
2992185435Sbz
2993185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2994185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2995185435Sbz
2996192895Sjamie	pr = cred->cr_prison;
2997192895Sjamie	if (!(pr->pr_flags & PR_IP4))
299846155Sphk		return (0);
2999191673Sjamie	mtx_lock(&pr->pr_mtx);
3000192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
3001192895Sjamie		mtx_unlock(&pr->pr_mtx);
3002192895Sjamie		return (0);
3003192895Sjamie	}
3004191673Sjamie	if (pr->pr_ip4 == NULL) {
3005191673Sjamie		mtx_unlock(&pr->pr_mtx);
3006188144Sjamie		return (EAFNOSUPPORT);
3007191673Sjamie	}
3008185435Sbz
3009185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
3010185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
3011191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
3012191673Sjamie		mtx_unlock(&pr->pr_mtx);
3013185435Sbz		return (0);
301446155Sphk	}
3015185435Sbz
3016188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
3017188144Sjamie		/*
3018188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
3019188144Sjamie		 */
3020191673Sjamie		if (pr->pr_ip4s == 1)
3021191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
3022191673Sjamie		mtx_unlock(&pr->pr_mtx);
3023185435Sbz		return (0);
3024185435Sbz	}
3025185435Sbz
3026191673Sjamie	error = _prison_check_ip4(pr, ia);
3027191673Sjamie	mtx_unlock(&pr->pr_mtx);
3028191673Sjamie	return (error);
3029185435Sbz}
3030185435Sbz
3031185435Sbz/*
3032185435Sbz * Rewrite destination address in case we will connect to loopback address.
3033185435Sbz *
3034188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
3035188144Sjamie * Address passed in in NBO and returned in NBO.
3036185435Sbz */
3037185435Sbzint
3038185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
3039185435Sbz{
3040191673Sjamie	struct prison *pr;
3041185435Sbz
3042185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3043185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
3044185435Sbz
3045192895Sjamie	pr = cred->cr_prison;
3046192895Sjamie	if (!(pr->pr_flags & PR_IP4))
3047185435Sbz		return (0);
3048191673Sjamie	mtx_lock(&pr->pr_mtx);
3049192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
3050192895Sjamie		mtx_unlock(&pr->pr_mtx);
3051192895Sjamie		return (0);
3052192895Sjamie	}
3053191673Sjamie	if (pr->pr_ip4 == NULL) {
3054191673Sjamie		mtx_unlock(&pr->pr_mtx);
3055188144Sjamie		return (EAFNOSUPPORT);
3056191673Sjamie	}
3057188144Sjamie
3058185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
3059191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
3060191673Sjamie		mtx_unlock(&pr->pr_mtx);
3061185435Sbz		return (0);
3062185435Sbz	}
3063185435Sbz
3064185435Sbz	/*
3065185435Sbz	 * Return success because nothing had to be changed.
3066185435Sbz	 */
3067191673Sjamie	mtx_unlock(&pr->pr_mtx);
3068185435Sbz	return (0);
3069185435Sbz}
3070185435Sbz
3071185435Sbz/*
3072188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3073185435Sbz *
3074192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
3075192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3076192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
3077185435Sbz */
3078185435Sbzstatic int
3079185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
3080185435Sbz{
3081185435Sbz	int i, a, z, d;
3082185435Sbz
3083185435Sbz	/*
3084185435Sbz	 * Check the primary IP.
3085185435Sbz	 */
3086185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
3087188144Sjamie		return (0);
3088185435Sbz
3089185435Sbz	/*
3090185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3091185435Sbz	 */
3092185435Sbz	a = 0;
3093185435Sbz	z = pr->pr_ip4s - 2;
3094185435Sbz	while (a <= z) {
3095185435Sbz		i = (a + z) / 2;
3096185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
3097185435Sbz		if (d > 0)
3098185435Sbz			z = i - 1;
3099185435Sbz		else if (d < 0)
3100185435Sbz			a = i + 1;
310181114Srwatson		else
3102188144Sjamie			return (0);
3103185435Sbz	}
3104188144Sjamie
3105188144Sjamie	return (EADDRNOTAVAIL);
3106185435Sbz}
3107185435Sbz
3108185435Sbzint
3109185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
3110185435Sbz{
3111191673Sjamie	struct prison *pr;
3112191673Sjamie	int error;
3113185435Sbz
3114185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3115185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
3116185435Sbz
3117192895Sjamie	pr = cred->cr_prison;
3118192895Sjamie	if (!(pr->pr_flags & PR_IP4))
3119188144Sjamie		return (0);
3120191673Sjamie	mtx_lock(&pr->pr_mtx);
3121192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
3122192895Sjamie		mtx_unlock(&pr->pr_mtx);
3123192895Sjamie		return (0);
3124192895Sjamie	}
3125191673Sjamie	if (pr->pr_ip4 == NULL) {
3126191673Sjamie		mtx_unlock(&pr->pr_mtx);
3127188144Sjamie		return (EAFNOSUPPORT);
3128191673Sjamie	}
3129185435Sbz
3130191673Sjamie	error = _prison_check_ip4(pr, ia);
3131191673Sjamie	mtx_unlock(&pr->pr_mtx);
3132191673Sjamie	return (error);
3133185435Sbz}
3134185435Sbz#endif
3135185435Sbz
3136185435Sbz#ifdef INET6
3137192895Sjamiestatic int
3138192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
3139192895Sjamie{
3140192895Sjamie	int ii, ij, used;
3141192895Sjamie	struct prison *ppr;
3142192895Sjamie
3143192895Sjamie	ppr = pr->pr_parent;
3144192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
3145192895Sjamie		/* This has no user settings, so just copy the parent's list. */
3146192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
3147192895Sjamie			/*
3148192895Sjamie			 * There's no room for the parent's list.  Use the
3149192895Sjamie			 * new list buffer, which is assumed to be big enough
3150192895Sjamie			 * (if it was passed).  If there's no buffer, try to
3151192895Sjamie			 * allocate one.
3152192895Sjamie			 */
3153192895Sjamie			used = 1;
3154192895Sjamie			if (newip6 == NULL) {
3155192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
3156192895Sjamie				    M_PRISON, M_NOWAIT);
3157192895Sjamie				if (newip6 != NULL)
3158192895Sjamie					used = 0;
3159192895Sjamie			}
3160192895Sjamie			if (newip6 != NULL) {
3161192895Sjamie				bcopy(ppr->pr_ip6, newip6,
3162192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
3163192895Sjamie				free(pr->pr_ip6, M_PRISON);
3164192895Sjamie				pr->pr_ip6 = newip6;
3165192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
3166192895Sjamie			}
3167192895Sjamie			return (used);
3168192895Sjamie		}
3169192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
3170192895Sjamie		if (pr->pr_ip6s > 0)
3171192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
3172192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
3173192895Sjamie		else if (pr->pr_ip6 != NULL) {
3174192895Sjamie			free(pr->pr_ip6, M_PRISON);
3175192895Sjamie			pr->pr_ip6 = NULL;
3176192895Sjamie		}
3177195974Sjamie	} else if (pr->pr_ip6s > 0) {
3178192895Sjamie		/* Remove addresses that aren't in the parent. */
3179192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
3180192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
3181192895Sjamie			    &ppr->pr_ip6[ij]))
3182192895Sjamie				break;
3183192895Sjamie		if (ij < ppr->pr_ip6s)
3184192895Sjamie			ii = 1;
3185192895Sjamie		else {
3186192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
3187192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
3188192895Sjamie			ii = 0;
3189192895Sjamie		}
3190192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
3191192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
3192192895Sjamie			    &ppr->pr_ip6[0])) {
3193192895Sjamie				ii++;
3194192895Sjamie				continue;
3195192895Sjamie			}
3196259847Sae			switch (ij >= ppr->pr_ip6s ? -1 :
3197192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3198192895Sjamie			case -1:
3199192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3200192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3201192895Sjamie				break;
3202192895Sjamie			case 0:
3203192895Sjamie				ii++;
3204192895Sjamie				ij++;
3205192895Sjamie				break;
3206192895Sjamie			case 1:
3207192895Sjamie				ij++;
3208192895Sjamie				break;
3209192895Sjamie			}
3210192895Sjamie		}
3211192895Sjamie		if (pr->pr_ip6s == 0) {
3212195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
3213192895Sjamie			free(pr->pr_ip6, M_PRISON);
3214192895Sjamie			pr->pr_ip6 = NULL;
3215192895Sjamie		}
3216192895Sjamie	}
3217192895Sjamie	return 0;
3218192895Sjamie}
3219192895Sjamie
3220185435Sbz/*
3221185435Sbz * Pass back primary IPv6 address for this jail.
3222185435Sbz *
3223192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3224192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3225185435Sbz *
3226188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3227185435Sbz */
3228185435Sbzint
3229187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3230185435Sbz{
3231191673Sjamie	struct prison *pr;
3232185435Sbz
3233185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3234185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3235185435Sbz
3236192895Sjamie	pr = cred->cr_prison;
3237192895Sjamie	if (!(pr->pr_flags & PR_IP6))
323881114Srwatson		return (0);
3239191673Sjamie	mtx_lock(&pr->pr_mtx);
3240192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3241192895Sjamie		mtx_unlock(&pr->pr_mtx);
3242192895Sjamie		return (0);
3243192895Sjamie	}
3244191673Sjamie	if (pr->pr_ip6 == NULL) {
3245191673Sjamie		mtx_unlock(&pr->pr_mtx);
3246188144Sjamie		return (EAFNOSUPPORT);
3247191673Sjamie	}
3248188144Sjamie
3249191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3250191673Sjamie	mtx_unlock(&pr->pr_mtx);
3251185435Sbz	return (0);
3252185435Sbz}
3253185435Sbz
3254185435Sbz/*
3255202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3256202468Sbz * We will return 0 if we should bypass source address selection in favour
3257202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3258202468Sbz * returned in NBO.
3259202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3260202468Sbz */
3261202468Sbzint
3262202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3263202468Sbz{
3264202468Sbz	struct prison *pr;
3265202468Sbz	struct in6_addr lia6;
3266202468Sbz	int error;
3267202468Sbz
3268202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3269202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3270202468Sbz
3271202468Sbz	if (!jailed(cred))
3272202468Sbz		return (1);
3273202468Sbz
3274202468Sbz	pr = cred->cr_prison;
3275202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3276202468Sbz		return (1);
3277202468Sbz
3278202468Sbz	lia6 = in6addr_any;
3279202468Sbz	error = prison_get_ip6(cred, &lia6);
3280202468Sbz	if (error)
3281202468Sbz		return (error);
3282202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3283202468Sbz		return (1);
3284202468Sbz
3285202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3286202468Sbz	return (0);
3287202468Sbz}
3288202468Sbz
3289202468Sbz/*
3290192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3291192895Sjamie */
3292192895Sjamieint
3293192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3294192895Sjamie{
3295192895Sjamie
3296192895Sjamie	if (pr1 == pr2)
3297192895Sjamie		return (1);
3298192895Sjamie
3299195945Sjamie	while (pr1 != &prison0 &&
3300195945Sjamie#ifdef VIMAGE
3301195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3302195945Sjamie#endif
3303195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3304192895Sjamie		pr1 = pr1->pr_parent;
3305195945Sjamie	while (pr2 != &prison0 &&
3306195945Sjamie#ifdef VIMAGE
3307195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3308195945Sjamie#endif
3309195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3310192895Sjamie		pr2 = pr2->pr_parent;
3311192895Sjamie	return (pr1 == pr2);
3312192895Sjamie}
3313192895Sjamie
3314192895Sjamie/*
3315185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3316185435Sbz *
3317185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3318185435Sbz * when needed while binding.
3319185435Sbz *
3320192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3321192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3322192895Sjamie * doesn't allow IPv6.
3323185435Sbz */
3324185435Sbzint
3325185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3326185435Sbz{
3327191673Sjamie	struct prison *pr;
3328191673Sjamie	int error;
3329185435Sbz
3330185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3331185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3332185435Sbz
3333192895Sjamie	pr = cred->cr_prison;
3334192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3335185435Sbz		return (0);
3336191673Sjamie	mtx_lock(&pr->pr_mtx);
3337192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3338192895Sjamie		mtx_unlock(&pr->pr_mtx);
3339192895Sjamie		return (0);
3340192895Sjamie	}
3341191673Sjamie	if (pr->pr_ip6 == NULL) {
3342191673Sjamie		mtx_unlock(&pr->pr_mtx);
3343188144Sjamie		return (EAFNOSUPPORT);
3344191673Sjamie	}
3345188144Sjamie
3346185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3347191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3348191673Sjamie		mtx_unlock(&pr->pr_mtx);
3349185435Sbz		return (0);
335081114Srwatson	}
3351185435Sbz
3352188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3353188144Sjamie		/*
3354188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3355188144Sjamie		 * then bind directly.
3356188144Sjamie		 */
3357191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3358191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3359191673Sjamie		mtx_unlock(&pr->pr_mtx);
3360185435Sbz		return (0);
3361185435Sbz	}
3362188144Sjamie
3363191673Sjamie	error = _prison_check_ip6(pr, ia6);
3364191673Sjamie	mtx_unlock(&pr->pr_mtx);
3365191673Sjamie	return (error);
3366185435Sbz}
3367185435Sbz
3368185435Sbz/*
3369185435Sbz * Rewrite destination address in case we will connect to loopback address.
3370185435Sbz *
3371188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3372185435Sbz */
3373185435Sbzint
3374185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3375185435Sbz{
3376191673Sjamie	struct prison *pr;
3377185435Sbz
3378185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3379185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3380185435Sbz
3381192895Sjamie	pr = cred->cr_prison;
3382192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3383185435Sbz		return (0);
3384191673Sjamie	mtx_lock(&pr->pr_mtx);
3385192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3386192895Sjamie		mtx_unlock(&pr->pr_mtx);
3387192895Sjamie		return (0);
3388192895Sjamie	}
3389191673Sjamie	if (pr->pr_ip6 == NULL) {
3390191673Sjamie		mtx_unlock(&pr->pr_mtx);
3391188144Sjamie		return (EAFNOSUPPORT);
3392191673Sjamie	}
3393188144Sjamie
3394185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3395191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3396191673Sjamie		mtx_unlock(&pr->pr_mtx);
3397185435Sbz		return (0);
3398185435Sbz	}
3399185435Sbz
3400185435Sbz	/*
3401185435Sbz	 * Return success because nothing had to be changed.
3402185435Sbz	 */
3403191673Sjamie	mtx_unlock(&pr->pr_mtx);
340446155Sphk	return (0);
340546155Sphk}
340646155Sphk
3407185435Sbz/*
3408188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3409185435Sbz *
3410192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3411192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3412192895Sjamie * doesn't allow IPv6.
3413185435Sbz */
3414185435Sbzstatic int
3415185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
341646155Sphk{
3417185435Sbz	int i, a, z, d;
341846155Sphk
3419185435Sbz	/*
3420185435Sbz	 * Check the primary IP.
3421185435Sbz	 */
3422185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3423188144Sjamie		return (0);
3424185435Sbz
3425185435Sbz	/*
3426185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3427185435Sbz	 */
3428185435Sbz	a = 0;
3429185435Sbz	z = pr->pr_ip6s - 2;
3430185435Sbz	while (a <= z) {
3431185435Sbz		i = (a + z) / 2;
3432185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3433185435Sbz		if (d > 0)
3434185435Sbz			z = i - 1;
3435185435Sbz		else if (d < 0)
3436185435Sbz			a = i + 1;
343746155Sphk		else
3438188144Sjamie			return (0);
343946155Sphk	}
3440188144Sjamie
3441188144Sjamie	return (EADDRNOTAVAIL);
344246155Sphk}
344346155Sphk
344446155Sphkint
3445185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3446185435Sbz{
3447191673Sjamie	struct prison *pr;
3448191673Sjamie	int error;
3449185435Sbz
3450185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3451185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3452185435Sbz
3453192895Sjamie	pr = cred->cr_prison;
3454192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3455188144Sjamie		return (0);
3456191673Sjamie	mtx_lock(&pr->pr_mtx);
3457192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3458192895Sjamie		mtx_unlock(&pr->pr_mtx);
3459192895Sjamie		return (0);
3460192895Sjamie	}
3461191673Sjamie	if (pr->pr_ip6 == NULL) {
3462191673Sjamie		mtx_unlock(&pr->pr_mtx);
3463188144Sjamie		return (EAFNOSUPPORT);
3464191673Sjamie	}
3465185435Sbz
3466191673Sjamie	error = _prison_check_ip6(pr, ia6);
3467191673Sjamie	mtx_unlock(&pr->pr_mtx);
3468191673Sjamie	return (error);
3469185435Sbz}
3470185435Sbz#endif
3471185435Sbz
3472185435Sbz/*
3473188146Sjamie * Check if a jail supports the given address family.
3474188146Sjamie *
3475188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3476188146Sjamie * if not.
3477188146Sjamie */
3478188146Sjamieint
3479188146Sjamieprison_check_af(struct ucred *cred, int af)
3480188146Sjamie{
3481192895Sjamie	struct prison *pr;
3482188146Sjamie	int error;
3483188146Sjamie
3484188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3485188146Sjamie
3486192895Sjamie	pr = cred->cr_prison;
3487194923Sjamie#ifdef VIMAGE
3488194915Sjamie	/* Prisons with their own network stack are not limited. */
3489200473Sbz	if (prison_owns_vnet(cred))
3490194915Sjamie		return (0);
3491194923Sjamie#endif
3492194915Sjamie
3493188146Sjamie	error = 0;
3494188146Sjamie	switch (af)
3495188146Sjamie	{
3496188146Sjamie#ifdef INET
3497188146Sjamie	case AF_INET:
3498192895Sjamie		if (pr->pr_flags & PR_IP4)
3499192895Sjamie		{
3500192895Sjamie			mtx_lock(&pr->pr_mtx);
3501192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3502192895Sjamie				error = EAFNOSUPPORT;
3503192895Sjamie			mtx_unlock(&pr->pr_mtx);
3504192895Sjamie		}
3505188146Sjamie		break;
3506188146Sjamie#endif
3507188146Sjamie#ifdef INET6
3508188146Sjamie	case AF_INET6:
3509192895Sjamie		if (pr->pr_flags & PR_IP6)
3510192895Sjamie		{
3511192895Sjamie			mtx_lock(&pr->pr_mtx);
3512192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3513192895Sjamie				error = EAFNOSUPPORT;
3514192895Sjamie			mtx_unlock(&pr->pr_mtx);
3515192895Sjamie		}
3516188146Sjamie		break;
3517188146Sjamie#endif
3518188146Sjamie	case AF_LOCAL:
3519188146Sjamie	case AF_ROUTE:
3520188146Sjamie		break;
3521188146Sjamie	default:
3522192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3523188146Sjamie			error = EAFNOSUPPORT;
3524188146Sjamie	}
3525188146Sjamie	return (error);
3526188146Sjamie}
3527188146Sjamie
3528188146Sjamie/*
3529185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3530185435Sbz * prison_check_ip[46]).
3531185435Sbz *
3532192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3533192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3534192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3535185435Sbz */
3536185435Sbzint
353772786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
353846155Sphk{
3539185435Sbz#ifdef INET
3540114168Smike	struct sockaddr_in *sai;
3541185435Sbz#endif
3542185435Sbz#ifdef INET6
3543185435Sbz	struct sockaddr_in6 *sai6;
3544185435Sbz#endif
3545188144Sjamie	int error;
354646155Sphk
3547185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3548185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3549185435Sbz
3550200473Sbz#ifdef VIMAGE
3551200473Sbz	if (prison_owns_vnet(cred))
3552200473Sbz		return (0);
3553200473Sbz#endif
3554200473Sbz
3555188144Sjamie	error = 0;
3556188144Sjamie	switch (sa->sa_family)
3557185435Sbz	{
3558185435Sbz#ifdef INET
3559185435Sbz	case AF_INET:
3560185435Sbz		sai = (struct sockaddr_in *)sa;
3561188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3562185435Sbz		break;
3563185435Sbz#endif
3564185435Sbz#ifdef INET6
3565185435Sbz	case AF_INET6:
3566185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3567188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3568185435Sbz		break;
3569185435Sbz#endif
3570185435Sbz	default:
3571192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3572188144Sjamie			error = EAFNOSUPPORT;
3573185435Sbz	}
3574188144Sjamie	return (error);
357546155Sphk}
357672786Srwatson
357772786Srwatson/*
357872786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
357972786Srwatson */
358072786Srwatsonint
3581114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
358272786Srwatson{
358372786Srwatson
3584192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3585192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3586192895Sjamie}
358772786Srwatson
3588192895Sjamie/*
3589192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3590192895Sjamie */
3591192895Sjamieint
3592192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3593192895Sjamie{
3594192895Sjamie
3595192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3596192895Sjamie		if (pr1 == pr2)
3597192895Sjamie			return (1);
359872786Srwatson	return (0);
359972786Srwatson}
360072786Srwatson
360172786Srwatson/*
360272786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
360372786Srwatson */
360472786Srwatsonint
3605114168Smikejailed(struct ucred *cred)
360672786Srwatson{
360772786Srwatson
3608192895Sjamie	return (cred->cr_prison != &prison0);
360972786Srwatson}
361091384Srobert
361191384Srobert/*
3612200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3613200473Sbz * have its own virtual network stack, otherwise 0.
3614200473Sbz */
3615200473Sbzint
3616200473Sbzjailed_without_vnet(struct ucred *cred)
3617200473Sbz{
3618200473Sbz
3619200473Sbz	if (!jailed(cred))
3620200473Sbz		return (0);
3621200473Sbz#ifdef VIMAGE
3622200473Sbz	if (prison_owns_vnet(cred))
3623200473Sbz		return (0);
3624200473Sbz#endif
3625200473Sbz
3626200473Sbz	return (1);
3627200473Sbz}
3628200473Sbz
3629200473Sbz/*
3630194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
363191384Srobert */
363291391Srobertvoid
3633114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
363491384Srobert{
3635193066Sjamie	struct prison *pr;
363691384Srobert
3637194090Sjamie	/*
3638194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3639194090Sjamie	 * system's hostname.
3640194090Sjamie	 */
3641193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3642193066Sjamie	mtx_lock(&pr->pr_mtx);
3643194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3644193066Sjamie	mtx_unlock(&pr->pr_mtx);
364591384Srobert}
3646113275Smike
3647194090Sjamievoid
3648194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3649194090Sjamie{
3650194090Sjamie
3651194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3652194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3653194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3654194090Sjamie}
3655194090Sjamie
3656194090Sjamievoid
3657194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3658194090Sjamie{
3659194090Sjamie
3660194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3661194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3662194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3663194090Sjamie}
3664194090Sjamie
3665194090Sjamievoid
3666194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3667194090Sjamie{
3668194090Sjamie
3669194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3670194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3671194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3672194090Sjamie}
3673194090Sjamie
3674196176Sbz#ifdef VIMAGE
3675125804Srwatson/*
3676196176Sbz * Determine whether the prison represented by cred owns
3677196176Sbz * its vnet rather than having it inherited.
3678196176Sbz *
3679196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3680196176Sbz */
3681196176Sbzint
3682196176Sbzprison_owns_vnet(struct ucred *cred)
3683196176Sbz{
3684196176Sbz
3685196176Sbz	/*
3686196176Sbz	 * vnets cannot be added/removed after jail creation,
3687196176Sbz	 * so no need to lock here.
3688196176Sbz	 */
3689196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3690196176Sbz}
3691196176Sbz#endif
3692196176Sbz
3693196176Sbz/*
3694147185Spjd * Determine whether the subject represented by cred can "see"
3695147185Spjd * status of a mount point.
3696147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3697147185Spjd * XXX: This function should be called cr_canseemount() and should be
3698147185Spjd *      placed in kern_prot.c.
3699125804Srwatson */
3700125804Srwatsonint
3701147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3702125804Srwatson{
3703147185Spjd	struct prison *pr;
3704147185Spjd	struct statfs *sp;
3705147185Spjd	size_t len;
3706125804Srwatson
3707192895Sjamie	pr = cred->cr_prison;
3708192895Sjamie	if (pr->pr_enforce_statfs == 0)
3709147185Spjd		return (0);
3710147185Spjd	if (pr->pr_root->v_mount == mp)
3711147185Spjd		return (0);
3712192895Sjamie	if (pr->pr_enforce_statfs == 2)
3713147185Spjd		return (ENOENT);
3714147185Spjd	/*
3715147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3716147185Spjd	 * all mount-points from inside a jail.
3717147185Spjd	 * This is ugly check, but this is the only situation when jail's
3718147185Spjd	 * directory ends with '/'.
3719147185Spjd	 */
3720147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3721147185Spjd		return (0);
3722147185Spjd	len = strlen(pr->pr_path);
3723147185Spjd	sp = &mp->mnt_stat;
3724147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3725147185Spjd		return (ENOENT);
3726147185Spjd	/*
3727147185Spjd	 * Be sure that we don't have situation where jail's root directory
3728147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3729147185Spjd	 */
3730147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3731147185Spjd		return (ENOENT);
3732147185Spjd	return (0);
3733147185Spjd}
3734147185Spjd
3735147185Spjdvoid
3736147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3737147185Spjd{
3738147185Spjd	char jpath[MAXPATHLEN];
3739147185Spjd	struct prison *pr;
3740147185Spjd	size_t len;
3741147185Spjd
3742192895Sjamie	pr = cred->cr_prison;
3743192895Sjamie	if (pr->pr_enforce_statfs == 0)
3744147185Spjd		return;
3745147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3746147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3747147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3748147185Spjd		    sizeof(sp->f_mntonname));
3749147185Spjd		return;
3750125804Srwatson	}
3751147185Spjd	if (pr->pr_root->v_mount == mp) {
3752147185Spjd		/*
3753147185Spjd		 * Clear current buffer data, so we are sure nothing from
3754147185Spjd		 * the valid path left there.
3755147185Spjd		 */
3756147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3757147185Spjd		*sp->f_mntonname = '/';
3758147185Spjd		return;
3759147185Spjd	}
3760147185Spjd	/*
3761147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3762147185Spjd	 * all mount-points from inside a jail.
3763147185Spjd	 */
3764147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3765147185Spjd		return;
3766147185Spjd	len = strlen(pr->pr_path);
3767147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3768147185Spjd	/*
3769147185Spjd	 * Clear current buffer data, so we are sure nothing from
3770147185Spjd	 * the valid path left there.
3771147185Spjd	 */
3772147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3773147185Spjd	if (*jpath == '\0') {
3774147185Spjd		/* Should never happen. */
3775147185Spjd		*sp->f_mntonname = '/';
3776147185Spjd	} else {
3777147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3778147185Spjd	}
3779125804Srwatson}
3780125804Srwatson
3781164032Srwatson/*
3782164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3783164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3784164032Srwatson */
3785164032Srwatsonint
3786164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3787164032Srwatson{
3788164032Srwatson
3789164032Srwatson	if (!jailed(cred))
3790164032Srwatson		return (0);
3791164032Srwatson
3792194915Sjamie#ifdef VIMAGE
3793194915Sjamie	/*
3794194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3795194915Sjamie	 * There might be a duplicate entry here in case the privilege
3796194915Sjamie	 * is only granted conditionally in the legacy jail case.
3797194915Sjamie	 */
3798164032Srwatson	switch (priv) {
3799194915Sjamie#ifdef notyet
3800194915Sjamie		/*
3801194915Sjamie		 * NFS-specific privileges.
3802194915Sjamie		 */
3803194915Sjamie	case PRIV_NFS_DAEMON:
3804194915Sjamie	case PRIV_NFS_LOCKD:
3805194915Sjamie#endif
3806194915Sjamie		/*
3807194915Sjamie		 * Network stack privileges.
3808194915Sjamie		 */
3809194915Sjamie	case PRIV_NET_BRIDGE:
3810194915Sjamie	case PRIV_NET_GRE:
3811194915Sjamie	case PRIV_NET_BPF:
3812194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3813194915Sjamie	case PRIV_NET_ROUTE:
3814194915Sjamie	case PRIV_NET_TAP:
3815194915Sjamie	case PRIV_NET_SETIFMTU:
3816194915Sjamie	case PRIV_NET_SETIFFLAGS:
3817194915Sjamie	case PRIV_NET_SETIFCAP:
3818203052Sdelphij	case PRIV_NET_SETIFDESCR:
3819194915Sjamie	case PRIV_NET_SETIFNAME	:
3820194915Sjamie	case PRIV_NET_SETIFMETRIC:
3821194915Sjamie	case PRIV_NET_SETIFPHYS:
3822194915Sjamie	case PRIV_NET_SETIFMAC:
3823194915Sjamie	case PRIV_NET_ADDMULTI:
3824194915Sjamie	case PRIV_NET_DELMULTI:
3825194915Sjamie	case PRIV_NET_HWIOCTL:
3826194915Sjamie	case PRIV_NET_SETLLADDR:
3827194915Sjamie	case PRIV_NET_ADDIFGROUP:
3828194915Sjamie	case PRIV_NET_DELIFGROUP:
3829194915Sjamie	case PRIV_NET_IFCREATE:
3830194915Sjamie	case PRIV_NET_IFDESTROY:
3831194915Sjamie	case PRIV_NET_ADDIFADDR:
3832194915Sjamie	case PRIV_NET_DELIFADDR:
3833194915Sjamie	case PRIV_NET_LAGG:
3834194915Sjamie	case PRIV_NET_GIF:
3835194915Sjamie	case PRIV_NET_SETIFVNET:
3836223735Sbz	case PRIV_NET_SETIFFIB:
3837164032Srwatson
3838164032Srwatson		/*
3839194915Sjamie		 * 802.11-related privileges.
3840194915Sjamie		 */
3841194915Sjamie	case PRIV_NET80211_GETKEY:
3842194915Sjamie#ifdef notyet
3843194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3844194915Sjamie#endif
3845194915Sjamie
3846194915Sjamie#ifdef notyet
3847194915Sjamie		/*
3848194915Sjamie		 * AppleTalk privileges.
3849194915Sjamie		 */
3850194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3851194915Sjamie
3852194915Sjamie		/*
3853194915Sjamie		 * ATM privileges.
3854194915Sjamie		 */
3855194915Sjamie	case PRIV_NETATM_CFG:
3856194915Sjamie	case PRIV_NETATM_ADD:
3857194915Sjamie	case PRIV_NETATM_DEL:
3858194915Sjamie	case PRIV_NETATM_SET:
3859194915Sjamie
3860194915Sjamie		/*
3861194915Sjamie		 * Bluetooth privileges.
3862194915Sjamie		 */
3863194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3864194915Sjamie#endif
3865194915Sjamie
3866194915Sjamie		/*
3867194915Sjamie		 * Netgraph and netgraph module privileges.
3868194915Sjamie		 */
3869194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3870194915Sjamie#ifdef notyet
3871194915Sjamie	case PRIV_NETGRAPH_TTY:
3872194915Sjamie#endif
3873194915Sjamie
3874194915Sjamie		/*
3875194915Sjamie		 * IPv4 and IPv6 privileges.
3876194915Sjamie		 */
3877194915Sjamie	case PRIV_NETINET_IPFW:
3878194915Sjamie	case PRIV_NETINET_DIVERT:
3879194915Sjamie	case PRIV_NETINET_PF:
3880194915Sjamie	case PRIV_NETINET_DUMMYNET:
3881194915Sjamie	case PRIV_NETINET_CARP:
3882194915Sjamie	case PRIV_NETINET_MROUTE:
3883194915Sjamie	case PRIV_NETINET_RAW:
3884194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3885194915Sjamie	case PRIV_NETINET_ND6:
3886194915Sjamie	case PRIV_NETINET_SCOPE6:
3887194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3888194915Sjamie	case PRIV_NETINET_IPSEC:
3889194915Sjamie	case PRIV_NETINET_BINDANY:
3890194915Sjamie
3891194915Sjamie#ifdef notyet
3892194915Sjamie		/*
3893194915Sjamie		 * IPX/SPX privileges.
3894194915Sjamie		 */
3895194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3896194915Sjamie	case PRIV_NETIPX_RAW:
3897194915Sjamie
3898194915Sjamie		/*
3899194915Sjamie		 * NCP privileges.
3900194915Sjamie		 */
3901194915Sjamie	case PRIV_NETNCP:
3902194915Sjamie
3903194915Sjamie		/*
3904194915Sjamie		 * SMB privileges.
3905194915Sjamie		 */
3906194915Sjamie	case PRIV_NETSMB:
3907194915Sjamie#endif
3908194915Sjamie
3909194915Sjamie	/*
3910194915Sjamie	 * No default: or deny here.
3911194915Sjamie	 * In case of no permit fall through to next switch().
3912194915Sjamie	 */
3913194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3914194915Sjamie			return (0);
3915194915Sjamie	}
3916194915Sjamie#endif /* VIMAGE */
3917194915Sjamie
3918194915Sjamie	switch (priv) {
3919194915Sjamie
3920194915Sjamie		/*
3921164032Srwatson		 * Allow ktrace privileges for root in jail.
3922164032Srwatson		 */
3923164032Srwatson	case PRIV_KTRACE:
3924164032Srwatson
3925166827Srwatson#if 0
3926164032Srwatson		/*
3927164032Srwatson		 * Allow jailed processes to configure audit identity and
3928164032Srwatson		 * submit audit records (login, etc).  In the future we may
3929164032Srwatson		 * want to further refine the relationship between audit and
3930164032Srwatson		 * jail.
3931164032Srwatson		 */
3932164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3933164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3934164032Srwatson	case PRIV_AUDIT_SUBMIT:
3935166827Srwatson#endif
3936164032Srwatson
3937164032Srwatson		/*
3938164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3939164032Srwatson		 * credentials in any way they see fit.
3940164032Srwatson		 */
3941164032Srwatson	case PRIV_CRED_SETUID:
3942164032Srwatson	case PRIV_CRED_SETEUID:
3943164032Srwatson	case PRIV_CRED_SETGID:
3944164032Srwatson	case PRIV_CRED_SETEGID:
3945164032Srwatson	case PRIV_CRED_SETGROUPS:
3946164032Srwatson	case PRIV_CRED_SETREUID:
3947164032Srwatson	case PRIV_CRED_SETREGID:
3948164032Srwatson	case PRIV_CRED_SETRESUID:
3949164032Srwatson	case PRIV_CRED_SETRESGID:
3950164032Srwatson
3951164032Srwatson		/*
3952164032Srwatson		 * Jail implements visibility constraints already, so allow
3953164032Srwatson		 * jailed root to override uid/gid-based constraints.
3954164032Srwatson		 */
3955164032Srwatson	case PRIV_SEEOTHERGIDS:
3956164032Srwatson	case PRIV_SEEOTHERUIDS:
3957164032Srwatson
3958164032Srwatson		/*
3959164032Srwatson		 * Jail implements inter-process debugging limits already, so
3960164032Srwatson		 * allow jailed root various debugging privileges.
3961164032Srwatson		 */
3962164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3963164032Srwatson	case PRIV_DEBUG_SUGID:
3964164032Srwatson	case PRIV_DEBUG_UNPRIV:
3965164032Srwatson
3966164032Srwatson		/*
3967164032Srwatson		 * Allow jail to set various resource limits and login
3968164032Srwatson		 * properties, and for now, exceed process resource limits.
3969164032Srwatson		 */
3970164032Srwatson	case PRIV_PROC_LIMIT:
3971164032Srwatson	case PRIV_PROC_SETLOGIN:
3972164032Srwatson	case PRIV_PROC_SETRLIMIT:
3973164032Srwatson
3974164032Srwatson		/*
3975164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3976164032Srwatson		 */
3977164032Srwatson	case PRIV_IPC_READ:
3978164032Srwatson	case PRIV_IPC_WRITE:
3979164032Srwatson	case PRIV_IPC_ADMIN:
3980164032Srwatson	case PRIV_IPC_MSGSIZE:
3981164032Srwatson	case PRIV_MQ_ADMIN:
3982164032Srwatson
3983164032Srwatson		/*
3984192895Sjamie		 * Jail operations within a jail work on child jails.
3985192895Sjamie		 */
3986192895Sjamie	case PRIV_JAIL_ATTACH:
3987192895Sjamie	case PRIV_JAIL_SET:
3988192895Sjamie	case PRIV_JAIL_REMOVE:
3989192895Sjamie
3990192895Sjamie		/*
3991164032Srwatson		 * Jail implements its own inter-process limits, so allow
3992164032Srwatson		 * root processes in jail to change scheduling on other
3993164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3994164032Srwatson		 */
3995164032Srwatson	case PRIV_SCHED_DIFFCRED:
3996185435Sbz	case PRIV_SCHED_CPUSET:
3997164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3998164032Srwatson	case PRIV_SIGNAL_SUGID:
3999164032Srwatson
4000164032Srwatson		/*
4001164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
4002164032Srwatson		 * writable.
4003164032Srwatson		 */
4004164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
4005164032Srwatson
4006164032Srwatson		/*
4007164032Srwatson		 * Allow root in jail to manage a variety of quota
4008166831Srwatson		 * properties.  These should likely be conditional on a
4009166831Srwatson		 * configuration option.
4010164032Srwatson		 */
4011166832Srwatson	case PRIV_VFS_GETQUOTA:
4012166832Srwatson	case PRIV_VFS_SETQUOTA:
4013164032Srwatson
4014164032Srwatson		/*
4015164032Srwatson		 * Since Jail relies on chroot() to implement file system
4016164032Srwatson		 * protections, grant many VFS privileges to root in jail.
4017164032Srwatson		 * Be careful to exclude mount-related and NFS-related
4018164032Srwatson		 * privileges.
4019164032Srwatson		 */
4020164032Srwatson	case PRIV_VFS_READ:
4021164032Srwatson	case PRIV_VFS_WRITE:
4022164032Srwatson	case PRIV_VFS_ADMIN:
4023164032Srwatson	case PRIV_VFS_EXEC:
4024164032Srwatson	case PRIV_VFS_LOOKUP:
4025164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
4026164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
4027164032Srwatson	case PRIV_VFS_CHOWN:
4028164032Srwatson	case PRIV_VFS_CHROOT:
4029167152Spjd	case PRIV_VFS_RETAINSUGID:
4030164032Srwatson	case PRIV_VFS_FCHROOT:
4031164032Srwatson	case PRIV_VFS_LINK:
4032164032Srwatson	case PRIV_VFS_SETGID:
4033172860Srwatson	case PRIV_VFS_STAT:
4034164032Srwatson	case PRIV_VFS_STICKYFILE:
4035255316Sjamie
4036255316Sjamie		/*
4037255316Sjamie		 * As in the non-jail case, non-root users are expected to be
4038255316Sjamie		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
4039255316Sjamie		 * exists in the jail and they have permission to access it).
4040255316Sjamie		 */
4041255316Sjamie	case PRIV_KMEM_READ:
4042164032Srwatson		return (0);
4043164032Srwatson
4044164032Srwatson		/*
4045164032Srwatson		 * Depending on the global setting, allow privilege of
4046164032Srwatson		 * setting system flags.
4047164032Srwatson		 */
4048164032Srwatson	case PRIV_VFS_SYSFLAGS:
4049192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
4050164032Srwatson			return (0);
4051164032Srwatson		else
4052164032Srwatson			return (EPERM);
4053164032Srwatson
4054164032Srwatson		/*
4055168396Spjd		 * Depending on the global setting, allow privilege of
4056168396Spjd		 * mounting/unmounting file systems.
4057168396Spjd		 */
4058168396Spjd	case PRIV_VFS_MOUNT:
4059168396Spjd	case PRIV_VFS_UNMOUNT:
4060168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
4061168699Spjd	case PRIV_VFS_MOUNT_OWNER:
4062224615Smm		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
4063224615Smm		    cred->cr_prison->pr_enforce_statfs < 2)
4064168396Spjd			return (0);
4065168396Spjd		else
4066168396Spjd			return (EPERM);
4067168396Spjd
4068168396Spjd		/*
4069168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
4070168591Srwatson		 * ports.
4071164032Srwatson		 */
4072164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
4073168591Srwatson	case PRIV_NETINET_REUSEPORT:
4074164032Srwatson		return (0);
4075164032Srwatson
4076164032Srwatson		/*
4077302234Sbdrewery		 * Allow jailed root to set certain IPv4/6 (option) headers.
4078175630Sbz		 */
4079175630Sbz	case PRIV_NETINET_SETHDROPTS:
4080175630Sbz		return (0);
4081175630Sbz
4082175630Sbz		/*
4083164032Srwatson		 * Conditionally allow creating raw sockets in jail.
4084164032Srwatson		 */
4085164032Srwatson	case PRIV_NETINET_RAW:
4086192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4087164032Srwatson			return (0);
4088164032Srwatson		else
4089164032Srwatson			return (EPERM);
4090164032Srwatson
4091164032Srwatson		/*
4092164032Srwatson		 * Since jail implements its own visibility limits on netstat
4093164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
4094164032Srwatson		 * jail.
4095164032Srwatson		 */
4096164032Srwatson	case PRIV_NETINET_GETCRED:
4097164032Srwatson		return (0);
4098164032Srwatson
4099219304Strasz		/*
4100219304Strasz		 * Allow jailed root to set loginclass.
4101219304Strasz		 */
4102219304Strasz	case PRIV_PROC_SETLOGINCLASS:
4103219304Strasz		return (0);
4104219304Strasz
4105164032Srwatson	default:
4106164032Srwatson		/*
4107164032Srwatson		 * In all remaining cases, deny the privilege request.  This
4108164032Srwatson		 * includes almost all network privileges, many system
4109164032Srwatson		 * configuration privileges.
4110164032Srwatson		 */
4111164032Srwatson		return (EPERM);
4112164032Srwatson	}
4113164032Srwatson}
4114164032Srwatson
4115192895Sjamie/*
4116192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
4117192895Sjamie * if it does not directly follow.
4118192895Sjamie */
4119192895Sjamie
4120192895Sjamiechar *
4121192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
4122192895Sjamie{
4123192895Sjamie	char *name;
4124192895Sjamie
4125192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
4126192895Sjamie	if (pr1 == pr2)
4127192895Sjamie		return "0";
4128192895Sjamie	name = pr2->pr_name;
4129192895Sjamie	if (prison_ischild(pr1, pr2)) {
4130192895Sjamie		/*
4131192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
4132192895Sjamie		 * so its length can't be counted on.  But the number of dots
4133192895Sjamie		 * can be counted on - and counted.
4134192895Sjamie		 */
4135192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
4136192895Sjamie			name = strchr(name, '.') + 1;
4137192895Sjamie	}
4138192895Sjamie	return (name);
4139192895Sjamie}
4140192895Sjamie
4141192895Sjamie/*
4142192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
4143192895Sjamie * if it does not directly follow.
4144192895Sjamie */
4145192895Sjamiestatic char *
4146192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
4147192895Sjamie{
4148192895Sjamie	char *path1, *path2;
4149192895Sjamie	int len1;
4150192895Sjamie
4151192895Sjamie	path1 = pr1->pr_path;
4152192895Sjamie	path2 = pr2->pr_path;
4153192895Sjamie	if (!strcmp(path1, "/"))
4154192895Sjamie		return (path2);
4155192895Sjamie	len1 = strlen(path1);
4156192895Sjamie	if (strncmp(path1, path2, len1))
4157192895Sjamie		return (path2);
4158192895Sjamie	if (path2[len1] == '\0')
4159192895Sjamie		return "/";
4160192895Sjamie	if (path2[len1] == '/')
4161192895Sjamie		return (path2 + len1);
4162192895Sjamie	return (path2);
4163192895Sjamie}
4164192895Sjamie
4165192895Sjamie
4166192895Sjamie/*
4167192895Sjamie * Jail-related sysctls.
4168192895Sjamie */
4169227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4170192895Sjamie    "Jails");
4171192895Sjamie
4172113275Smikestatic int
4173113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
4174113275Smike{
4175191673Sjamie	struct xprison *xp;
4176192895Sjamie	struct prison *pr, *cpr;
4177191673Sjamie#ifdef INET
4178191673Sjamie	struct in_addr *ip4 = NULL;
4179191673Sjamie	int ip4s = 0;
4180191673Sjamie#endif
4181191673Sjamie#ifdef INET6
4182208803Scperciva	struct in6_addr *ip6 = NULL;
4183191673Sjamie	int ip6s = 0;
4184191673Sjamie#endif
4185192895Sjamie	int descend, error;
4186113275Smike
4187191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4188192895Sjamie	pr = req->td->td_ucred->cr_prison;
4189191673Sjamie	error = 0;
4190168401Spjd	sx_slock(&allprison_lock);
4191192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4192192895Sjamie#if defined(INET) || defined(INET6)
4193191673Sjamie again:
4194192895Sjamie#endif
4195192895Sjamie		mtx_lock(&cpr->pr_mtx);
4196185435Sbz#ifdef INET
4197192895Sjamie		if (cpr->pr_ip4s > 0) {
4198192895Sjamie			if (ip4s < cpr->pr_ip4s) {
4199192895Sjamie				ip4s = cpr->pr_ip4s;
4200192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4201191673Sjamie				ip4 = realloc(ip4, ip4s *
4202191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4203191673Sjamie				goto again;
4204191673Sjamie			}
4205192895Sjamie			bcopy(cpr->pr_ip4, ip4,
4206192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
4207191673Sjamie		}
4208185435Sbz#endif
4209185435Sbz#ifdef INET6
4210192895Sjamie		if (cpr->pr_ip6s > 0) {
4211192895Sjamie			if (ip6s < cpr->pr_ip6s) {
4212192895Sjamie				ip6s = cpr->pr_ip6s;
4213192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4214191673Sjamie				ip6 = realloc(ip6, ip6s *
4215191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4216191673Sjamie				goto again;
4217191673Sjamie			}
4218192895Sjamie			bcopy(cpr->pr_ip6, ip6,
4219192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
4220191673Sjamie		}
4221185435Sbz#endif
4222192895Sjamie		if (cpr->pr_ref == 0) {
4223192895Sjamie			mtx_unlock(&cpr->pr_mtx);
4224191673Sjamie			continue;
4225191673Sjamie		}
4226191673Sjamie		bzero(xp, sizeof(*xp));
4227113275Smike		xp->pr_version = XPRISON_VERSION;
4228192895Sjamie		xp->pr_id = cpr->pr_id;
4229192895Sjamie		xp->pr_state = cpr->pr_uref > 0
4230191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4231192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4232194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4233192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4234185435Sbz#ifdef INET
4235192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
4236185435Sbz#endif
4237185435Sbz#ifdef INET6
4238192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4239185435Sbz#endif
4240192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4241191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4242191673Sjamie		if (error)
4243191673Sjamie			break;
4244185435Sbz#ifdef INET
4245191673Sjamie		if (xp->pr_ip4s > 0) {
4246191673Sjamie			error = SYSCTL_OUT(req, ip4,
4247191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4248191673Sjamie			if (error)
4249191673Sjamie				break;
4250185435Sbz		}
4251185435Sbz#endif
4252185435Sbz#ifdef INET6
4253191673Sjamie		if (xp->pr_ip6s > 0) {
4254191673Sjamie			error = SYSCTL_OUT(req, ip6,
4255191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4256191673Sjamie			if (error)
4257191673Sjamie				break;
4258185435Sbz		}
4259185435Sbz#endif
4260113275Smike	}
4261168401Spjd	sx_sunlock(&allprison_lock);
4262191673Sjamie	free(xp, M_TEMP);
4263191673Sjamie#ifdef INET
4264191673Sjamie	free(ip4, M_TEMP);
4265191673Sjamie#endif
4266191673Sjamie#ifdef INET6
4267191673Sjamie	free(ip6, M_TEMP);
4268191673Sjamie#endif
4269167354Spjd	return (error);
4270113275Smike}
4271113275Smike
4272187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4273187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4274187864Sed    sysctl_jail_list, "S", "List of active jails");
4275126004Spjd
4276126004Spjdstatic int
4277126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4278126004Spjd{
4279126004Spjd	int error, injail;
4280126004Spjd
4281126004Spjd	injail = jailed(req->td->td_ucred);
4282126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4283126004Spjd
4284126004Spjd	return (error);
4285126004Spjd}
4286192895Sjamie
4287187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4288187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4289187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4290185435Sbz
4291250804Sjamiestatic int
4292250804Sjamiesysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4293250804Sjamie{
4294250804Sjamie	int error, havevnet;
4295250804Sjamie#ifdef VIMAGE
4296250804Sjamie	struct ucred *cred = req->td->td_ucred;
4297250804Sjamie
4298250804Sjamie	havevnet = jailed(cred) && prison_owns_vnet(cred);
4299250804Sjamie#else
4300250804Sjamie	havevnet = 0;
4301250804Sjamie#endif
4302250804Sjamie	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4303250804Sjamie
4304250804Sjamie	return (error);
4305250804Sjamie}
4306250804Sjamie
4307250804SjamieSYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4308250804Sjamie    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4309250804Sjamie    sysctl_jail_vnet, "I", "Jail owns VNET?");
4310250804Sjamie
4311192895Sjamie#if defined(INET) || defined(INET6)
4312193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4313192895Sjamie    &jail_max_af_ips, 0,
4314301905Sjamie    "Number of IP addresses a jail may have at most per address family (deprecated)");
4315192895Sjamie#endif
4316192895Sjamie
4317192895Sjamie/*
4318302234Sbdrewery * Default parameters for jail(2) compatibility.  For historical reasons,
4319192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4320192895Sjamie * just see their own parameters, and can't change them.
4321192895Sjamie */
4322192895Sjamiestatic int
4323192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4324192895Sjamie{
4325192895Sjamie	struct prison *pr;
4326192895Sjamie	int allow, error, i;
4327192895Sjamie
4328192895Sjamie	pr = req->td->td_ucred->cr_prison;
4329192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4330192895Sjamie
4331192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4332192895Sjamie	i = (allow & arg2) ? 1 : 0;
4333192895Sjamie	if (arg1 != NULL)
4334192895Sjamie		i = !i;
4335192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4336192895Sjamie	if (error || !req->newptr)
4337192895Sjamie		return (error);
4338192895Sjamie	i = i ? arg2 : 0;
4339192895Sjamie	if (arg1 != NULL)
4340192895Sjamie		i ^= arg2;
4341192895Sjamie	/*
4342192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4343192895Sjamie	 * for writing.
4344192895Sjamie	 */
4345192895Sjamie	mtx_lock(&prison0.pr_mtx);
4346192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4347192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4348192895Sjamie	return (0);
4349192895Sjamie}
4350192895Sjamie
4351192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4352192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4353192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4354301905Sjamie    "Processes in jail can set their hostnames (deprecated)");
4355192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4356192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4357192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4358301905Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4359192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4360192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4361192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4362301905Sjamie    "Processes in jail can use System V IPC primitives (deprecated)");
4363192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4364192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4365192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4366301905Sjamie    "Prison root can create raw sockets (deprecated)");
4367192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4368192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4369192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4370301905Sjamie    "Processes in jail can alter system file flags (deprecated)");
4371192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4372192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4373192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4374301905Sjamie    "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4375232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4376232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4377232059Smm    NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4378301905Sjamie    "Processes in jail can mount the devfs file system (deprecated)");
4379277985SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed,
4380277985Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4381277985Sjamie    NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I",
4382301905Sjamie    "Processes in jail can mount the fdescfs file system (deprecated)");
4383232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4384232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4385232059Smm    NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4386301905Sjamie    "Processes in jail can mount the nullfs file system (deprecated)");
4387232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
4388232278Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4389232278Smm    NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
4390301905Sjamie    "Processes in jail can mount the procfs file system (deprecated)");
4391295951SaraujoSYSCTL_PROC(_security_jail, OID_AUTO, mount_linprocfs_allowed,
4392295951Saraujo    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4393295951Saraujo    NULL, PR_ALLOW_MOUNT_LINPROCFS, sysctl_jail_default_allow, "I",
4394301905Sjamie    "Processes in jail can mount the linprocfs file system (deprecated)");
4395295951SaraujoSYSCTL_PROC(_security_jail, OID_AUTO, mount_linsysfs_allowed,
4396295951Saraujo    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4397295951Saraujo    NULL, PR_ALLOW_MOUNT_LINSYSFS, sysctl_jail_default_allow, "I",
4398301905Sjamie    "Processes in jail can mount the linsysfs file system (deprecated)");
4399254741SdelphijSYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed,
4400254741Sdelphij    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4401254741Sdelphij    NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I",
4402301905Sjamie    "Processes in jail can mount the tmpfs file system (deprecated)");
4403232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4404232186Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4405232186Smm    NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4406301905Sjamie    "Processes in jail can mount the zfs file system (deprecated)");
4407192895Sjamie
4408192895Sjamiestatic int
4409192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4410192895Sjamie{
4411192895Sjamie	struct prison *pr;
4412192895Sjamie	int level, error;
4413192895Sjamie
4414192895Sjamie	pr = req->td->td_ucred->cr_prison;
4415192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4416192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4417192895Sjamie	if (error || !req->newptr)
4418192895Sjamie		return (error);
4419192895Sjamie	*(int *)arg1 = level;
4420192895Sjamie	return (0);
4421192895Sjamie}
4422192895Sjamie
4423192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4424192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4425192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4426192895Sjamie    sysctl_jail_default_level, "I",
4427301905Sjamie    "Processes in jail cannot see all mounted file systems (deprecated)");
4428192895Sjamie
4429231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4430231267Smm    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4431231267Smm    &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4432231267Smm    sysctl_jail_default_level, "I",
4433301905Sjamie    "Ruleset for the devfs filesystem in jail (deprecated)");
4434231267Smm
4435192895Sjamie/*
4436192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4437192895Sjamie * is returned in the string itself, and the other parameters exist merely
4438192895Sjamie * to make themselves and their types known.
4439192895Sjamie */
4440192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4441192895Sjamie    "Jail parameters");
4442192895Sjamie
4443192895Sjamieint
4444192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4445192895Sjamie{
4446192895Sjamie	int i;
4447192895Sjamie	long l;
4448192895Sjamie	size_t s;
4449192895Sjamie	char numbuf[12];
4450192895Sjamie
4451192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4452192895Sjamie	{
4453192895Sjamie	case CTLTYPE_LONG:
4454192895Sjamie	case CTLTYPE_ULONG:
4455192895Sjamie		l = 0;
4456192895Sjamie#ifdef SCTL_MASK32
4457192895Sjamie		if (!(req->flags & SCTL_MASK32))
4458192895Sjamie#endif
4459192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4460192895Sjamie	case CTLTYPE_INT:
4461192895Sjamie	case CTLTYPE_UINT:
4462192895Sjamie		i = 0;
4463192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4464192895Sjamie	case CTLTYPE_STRING:
4465219819Sjeff		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4466192895Sjamie		return
4467192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4468192895Sjamie	case CTLTYPE_STRUCT:
4469192895Sjamie		s = (size_t)arg2;
4470192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4471192895Sjamie	}
4472192895Sjamie	return (0);
4473192895Sjamie}
4474192895Sjamie
4475280632Sian/*
4476280632Sian * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4477280632Sian * jail creation time but cannot be changed in an existing jail.
4478280632Sian */
4479192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4480192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4481192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4482192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4483192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4484192895Sjamie    "I", "Jail secure level");
4485280632SianSYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4486280632Sian    "Jail value for kern.osreldate and uname -K");
4487280632SianSYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4488280632Sian    "Jail value for kern.osrelease and uname -r");
4489192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4490192895Sjamie    "I", "Jail cannot see all mounted file systems");
4491231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4492231267Smm    "I", "Ruleset for in-jail devfs mounts");
4493192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4494192895Sjamie    "B", "Jail persistence");
4495194251Sjamie#ifdef VIMAGE
4496194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4497195870Sjamie    "E,jailsys", "Virtual network stack");
4498194251Sjamie#endif
4499192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4500192895Sjamie    "B", "Jail is in the process of shutting down");
4501192895Sjamie
4502194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4503194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4504194762Sjamie    "I", "Current number of child jails");
4505194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4506194762Sjamie    "I", "Maximum number of child jails");
4507194762Sjamie
4508195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4509192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4510192895Sjamie    "Jail hostname");
4511193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4512193066Sjamie    "Jail NIS domainname");
4513193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4514193066Sjamie    "Jail host UUID");
4515193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4516193066Sjamie    "LU", "Jail host ID");
4517192895Sjamie
4518192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4519192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4520192895Sjamie
4521192895Sjamie#ifdef INET
4522195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4523195974Sjamie    "Jail IPv4 address virtualization");
4524192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4525192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4526202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4527202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4528202468Sbz    "primary jail IPv4 address.");
4529192895Sjamie#endif
4530192895Sjamie#ifdef INET6
4531195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4532195974Sjamie    "Jail IPv6 address virtualization");
4533192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4534192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4535202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4536202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4537202468Sbz    "primary jail IPv6 address.");
4538192895Sjamie#endif
4539192895Sjamie
4540192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4541192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4542192895Sjamie    "B", "Jail may set hostname");
4543192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4544192895Sjamie    "B", "Jail may use SYSV IPC");
4545192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4546192895Sjamie    "B", "Jail may create raw sockets");
4547192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4548192895Sjamie    "B", "Jail may alter system file flags");
4549192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4550192895Sjamie    "B", "Jail may set file quotas");
4551192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4552192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4553192895Sjamie
4554232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4555232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4556232059Smm    "B", "Jail may mount/unmount jail-friendly file systems in general");
4557232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4558232186Smm    "B", "Jail may mount the devfs file system");
4559277985SjamieSYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW,
4560277985Sjamie    "B", "Jail may mount the fdescfs file system");
4561232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4562232186Smm    "B", "Jail may mount the nullfs file system");
4563232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
4564232278Smm    "B", "Jail may mount the procfs file system");
4565295951SaraujoSYSCTL_JAIL_PARAM(_allow_mount, linprocfs, CTLTYPE_INT | CTLFLAG_RW,
4566295951Saraujo    "B", "Jail may mount the linprocfs file system");
4567295951SaraujoSYSCTL_JAIL_PARAM(_allow_mount, linsysfs, CTLTYPE_INT | CTLFLAG_RW,
4568295951Saraujo    "B", "Jail may mount the linsysfs file system");
4569254741SdelphijSYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW,
4570254741Sdelphij    "B", "Jail may mount the tmpfs file system");
4571232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4572232186Smm    "B", "Jail may mount the zfs file system");
4573232059Smm
4574284665Strasz#ifdef RACCT
4575220137Straszvoid
4576220137Straszprison_racct_foreach(void (*callback)(struct racct *racct,
4577220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
4578220137Strasz{
4579221362Strasz	struct prison_racct *prr;
4580192895Sjamie
4581284665Strasz	ASSERT_RACCT_ENABLED();
4582284665Strasz
4583220137Strasz	sx_slock(&allprison_lock);
4584221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next)
4585221362Strasz		(callback)(prr->prr_racct, arg2, arg3);
4586220137Strasz	sx_sunlock(&allprison_lock);
4587220137Strasz}
4588220137Strasz
4589221362Straszstatic struct prison_racct *
4590221362Straszprison_racct_find_locked(const char *name)
4591221362Strasz{
4592221362Strasz	struct prison_racct *prr;
4593221362Strasz
4594284665Strasz	ASSERT_RACCT_ENABLED();
4595221362Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4596221362Strasz
4597221362Strasz	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4598221362Strasz		return (NULL);
4599221362Strasz
4600221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4601221362Strasz		if (strcmp(name, prr->prr_name) != 0)
4602221362Strasz			continue;
4603221362Strasz
4604221362Strasz		/* Found prison_racct with a matching name? */
4605221362Strasz		prison_racct_hold(prr);
4606221362Strasz		return (prr);
4607221362Strasz	}
4608221362Strasz
4609221362Strasz	/* Add new prison_racct. */
4610221362Strasz	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4611221362Strasz	racct_create(&prr->prr_racct);
4612221362Strasz
4613221362Strasz	strcpy(prr->prr_name, name);
4614221362Strasz	refcount_init(&prr->prr_refcount, 1);
4615221362Strasz	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4616221362Strasz
4617221362Strasz	return (prr);
4618221362Strasz}
4619221362Strasz
4620221362Straszstruct prison_racct *
4621221362Straszprison_racct_find(const char *name)
4622221362Strasz{
4623221362Strasz	struct prison_racct *prr;
4624221362Strasz
4625284665Strasz	ASSERT_RACCT_ENABLED();
4626284665Strasz
4627221362Strasz	sx_xlock(&allprison_lock);
4628221362Strasz	prr = prison_racct_find_locked(name);
4629221362Strasz	sx_xunlock(&allprison_lock);
4630221362Strasz	return (prr);
4631221362Strasz}
4632221362Strasz
4633221362Straszvoid
4634221362Straszprison_racct_hold(struct prison_racct *prr)
4635221362Strasz{
4636221362Strasz
4637284665Strasz	ASSERT_RACCT_ENABLED();
4638284665Strasz
4639221362Strasz	refcount_acquire(&prr->prr_refcount);
4640221362Strasz}
4641221362Strasz
4642232598Straszstatic void
4643232598Straszprison_racct_free_locked(struct prison_racct *prr)
4644232598Strasz{
4645232598Strasz
4646284665Strasz	ASSERT_RACCT_ENABLED();
4647232598Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4648232598Strasz
4649232598Strasz	if (refcount_release(&prr->prr_refcount)) {
4650232598Strasz		racct_destroy(&prr->prr_racct);
4651232598Strasz		LIST_REMOVE(prr, prr_next);
4652232598Strasz		free(prr, M_PRISON_RACCT);
4653232598Strasz	}
4654232598Strasz}
4655232598Strasz
4656221362Straszvoid
4657221362Straszprison_racct_free(struct prison_racct *prr)
4658221362Strasz{
4659221362Strasz	int old;
4660221362Strasz
4661284665Strasz	ASSERT_RACCT_ENABLED();
4662232598Strasz	sx_assert(&allprison_lock, SA_UNLOCKED);
4663232598Strasz
4664221362Strasz	old = prr->prr_refcount;
4665221362Strasz	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4666221362Strasz		return;
4667221362Strasz
4668221362Strasz	sx_xlock(&allprison_lock);
4669232598Strasz	prison_racct_free_locked(prr);
4670221362Strasz	sx_xunlock(&allprison_lock);
4671221362Strasz}
4672221362Strasz
4673221362Straszstatic void
4674221362Straszprison_racct_attach(struct prison *pr)
4675221362Strasz{
4676221362Strasz	struct prison_racct *prr;
4677221362Strasz
4678284665Strasz	ASSERT_RACCT_ENABLED();
4679232598Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4680232598Strasz
4681221362Strasz	prr = prison_racct_find_locked(pr->pr_name);
4682221362Strasz	KASSERT(prr != NULL, ("cannot find prison_racct"));
4683221362Strasz
4684221362Strasz	pr->pr_prison_racct = prr;
4685221362Strasz}
4686221362Strasz
4687232598Strasz/*
4688232598Strasz * Handle jail renaming.  From the racct point of view, renaming means
4689232598Strasz * moving from one prison_racct to another.
4690232598Strasz */
4691221362Straszstatic void
4692232598Straszprison_racct_modify(struct prison *pr)
4693232598Strasz{
4694232598Strasz	struct proc *p;
4695232598Strasz	struct ucred *cred;
4696232598Strasz	struct prison_racct *oldprr;
4697232598Strasz
4698284665Strasz	ASSERT_RACCT_ENABLED();
4699284665Strasz
4700232598Strasz	sx_slock(&allproc_lock);
4701232598Strasz	sx_xlock(&allprison_lock);
4702232598Strasz
4703235795Strasz	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4704235795Strasz		sx_xunlock(&allprison_lock);
4705235795Strasz		sx_sunlock(&allproc_lock);
4706232598Strasz		return;
4707235795Strasz	}
4708232598Strasz
4709232598Strasz	oldprr = pr->pr_prison_racct;
4710232598Strasz	pr->pr_prison_racct = NULL;
4711232598Strasz
4712232598Strasz	prison_racct_attach(pr);
4713232598Strasz
4714232598Strasz	/*
4715232598Strasz	 * Move resource utilisation records.
4716232598Strasz	 */
4717232598Strasz	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4718232598Strasz
4719232598Strasz	/*
4720232598Strasz	 * Force rctl to reattach rules to processes.
4721232598Strasz	 */
4722232598Strasz	FOREACH_PROC_IN_SYSTEM(p) {
4723232598Strasz		PROC_LOCK(p);
4724232598Strasz		cred = crhold(p->p_ucred);
4725232598Strasz		PROC_UNLOCK(p);
4726232598Strasz		racct_proc_ucred_changed(p, cred, cred);
4727232598Strasz		crfree(cred);
4728232598Strasz	}
4729232598Strasz
4730232598Strasz	sx_sunlock(&allproc_lock);
4731232598Strasz	prison_racct_free_locked(oldprr);
4732232598Strasz	sx_xunlock(&allprison_lock);
4733232598Strasz}
4734232598Strasz
4735232598Straszstatic void
4736221362Straszprison_racct_detach(struct prison *pr)
4737221362Strasz{
4738232598Strasz
4739284665Strasz	ASSERT_RACCT_ENABLED();
4740232598Strasz	sx_assert(&allprison_lock, SA_UNLOCKED);
4741232598Strasz
4742244404Smjg	if (pr->pr_prison_racct == NULL)
4743244404Smjg		return;
4744221362Strasz	prison_racct_free(pr->pr_prison_racct);
4745221362Strasz	pr->pr_prison_racct = NULL;
4746221362Strasz}
4747221362Strasz#endif /* RACCT */
4748221362Strasz
4749185435Sbz#ifdef DDB
4750191673Sjamie
4751191673Sjamiestatic void
4752191673Sjamiedb_show_prison(struct prison *pr)
4753185435Sbz{
4754192895Sjamie	int fi;
4755191673Sjamie#if defined(INET) || defined(INET6)
4756191673Sjamie	int ii;
4757185435Sbz#endif
4758195870Sjamie	unsigned jsf;
4759185435Sbz#ifdef INET6
4760185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4761185435Sbz#endif
4762185435Sbz
4763191673Sjamie	db_printf("prison %p:\n", pr);
4764191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4765191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4766192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4767191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4768191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4769191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4770191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4771191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4772194251Sjamie#ifdef VIMAGE
4773194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4774194251Sjamie#endif
4775191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4776191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4777231267Smm	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4778202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4779202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4780192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4781192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4782202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4783192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4784192895Sjamie	    fi++)
4785192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4786192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4787195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4788195870Sjamie	    fi++) {
4789195870Sjamie		jsf = pr->pr_flags &
4790195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4791195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4792195870Sjamie		    pr_flag_jailsys[fi].disable &&
4793195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4794195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4795195870Sjamie		    : "inherit");
4796195870Sjamie	}
4797202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4798192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4799192895Sjamie	    fi++)
4800192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4801192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4802191673Sjamie	db_printf("\n");
4803192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4804194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4805194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4806194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4807193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4808185435Sbz#ifdef INET
4809191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4810191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4811191673Sjamie		db_printf(" %s %s\n",
4812202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4813191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4814185435Sbz#endif
4815185435Sbz#ifdef INET6
4816191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4817191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4818191673Sjamie		db_printf(" %s %s\n",
4819202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4820191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4821191673Sjamie#endif
4822191673Sjamie}
4823191673Sjamie
4824191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4825191673Sjamie{
4826191673Sjamie	struct prison *pr;
4827191673Sjamie
4828191673Sjamie	if (!have_addr) {
4829192895Sjamie		/*
4830192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4831192895Sjamie		 * listed.
4832192895Sjamie		 */
4833192895Sjamie		db_show_prison(&prison0);
4834192895Sjamie		if (!db_pager_quit) {
4835192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4836192895Sjamie				db_show_prison(pr);
4837192895Sjamie				if (db_pager_quit)
4838192895Sjamie					break;
4839192895Sjamie			}
4840191673Sjamie		}
4841191673Sjamie		return;
4842191673Sjamie	}
4843191673Sjamie
4844192895Sjamie	if (addr == 0)
4845192895Sjamie		pr = &prison0;
4846192895Sjamie	else {
4847192895Sjamie		/* Look for a prison with the ID and with references. */
4848191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4849192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4850191673Sjamie				break;
4851192895Sjamie		if (pr == NULL)
4852192895Sjamie			/* Look again, without requiring a reference. */
4853192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4854192895Sjamie				if (pr->pr_id == addr)
4855192895Sjamie					break;
4856192895Sjamie		if (pr == NULL)
4857192895Sjamie			/* Assume address points to a valid prison. */
4858192895Sjamie			pr = (struct prison *)addr;
4859192895Sjamie	}
4860191673Sjamie	db_show_prison(pr);
4861185435Sbz}
4862191673Sjamie
4863185435Sbz#endif /* DDB */
4864