kern_jail.c revision 301905
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 301905 2016-06-15 01:49:01Z 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
935230143Smm	fullpath_disabled = 0;
936191673Sjamie	root = NULL;
937191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
938191673Sjamie	if (error == ENOENT)
939191673Sjamie		path = NULL;
940191673Sjamie	else if (error != 0)
941191673Sjamie		goto done_free;
942191673Sjamie	else {
943191673Sjamie		if (flags & JAIL_UPDATE) {
944191673Sjamie			error = EINVAL;
945191673Sjamie			vfs_opterror(opts,
946191673Sjamie			    "path cannot be changed after creation");
947191673Sjamie			goto done_errmsg;
948191673Sjamie		}
949191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
950191673Sjamie			error = EINVAL;
951191673Sjamie			goto done_free;
952191673Sjamie		}
953241896Skib		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
954230129Smm		    path, td);
955230129Smm		error = namei(&nd);
956230129Smm		if (error)
957230129Smm			goto done_free;
958230129Smm		root = nd.ni_vp;
959230129Smm		NDFREE(&nd, NDF_ONLY_PNBUF);
960230407Smm		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
961230407Smm		strlcpy(g_path, path, MAXPATHLEN);
962230407Smm		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
963230407Smm		if (error == 0)
964230407Smm			path = g_path;
965230407Smm		else if (error == ENODEV) {
966230129Smm			/* proceed if sysctl debug.disablefullpath == 1 */
967230129Smm			fullpath_disabled = 1;
968230129Smm			if (len < 2 || (len == 2 && path[0] == '/'))
969230129Smm				path = NULL;
970230407Smm		} else {
971230129Smm			/* exit on other errors */
972230129Smm			goto done_free;
973230129Smm		}
974230129Smm		if (root->v_type != VDIR) {
975230129Smm			error = ENOTDIR;
976230129Smm			vput(root);
977230129Smm			goto done_free;
978230129Smm		}
979230129Smm		VOP_UNLOCK(root, 0);
980230129Smm		if (fullpath_disabled) {
981192895Sjamie			/* Leave room for a real-root full pathname. */
982192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
983192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
984192895Sjamie				error = ENAMETOOLONG;
985192895Sjamie				goto done_free;
986192895Sjamie			}
987191673Sjamie		}
988191673Sjamie	}
989185435Sbz
990280632Sian	error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
991280632Sian	if (error == ENOENT)
992280632Sian		osrelstr = NULL;
993280632Sian	else if (error != 0)
994280632Sian		goto done_free;
995280632Sian	else {
996280632Sian		if (flags & JAIL_UPDATE) {
997280632Sian			error = EINVAL;
998280632Sian			vfs_opterror(opts,
999280632Sian			    "osrelease cannot be changed after creation");
1000280632Sian			goto done_errmsg;
1001280632Sian		}
1002280632Sian		if (len == 0 || len >= OSRELEASELEN) {
1003280632Sian			error = EINVAL;
1004280632Sian			vfs_opterror(opts,
1005280632Sian			    "osrelease string must be 1-%d bytes long",
1006280632Sian			    OSRELEASELEN - 1);
1007280632Sian			goto done_errmsg;
1008280632Sian		}
1009280632Sian	}
1010280632Sian
1011280632Sian	error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1012280632Sian	if (error == ENOENT)
1013280632Sian		osreldt = 0;
1014280632Sian	else if (error != 0)
1015280632Sian		goto done_free;
1016280632Sian	else {
1017280632Sian		if (flags & JAIL_UPDATE) {
1018280632Sian			error = EINVAL;
1019280632Sian			vfs_opterror(opts,
1020280632Sian			    "osreldate cannot be changed after creation");
1021280632Sian			goto done_errmsg;
1022280632Sian		}
1023280632Sian		if (osreldt == 0) {
1024280632Sian			error = EINVAL;
1025280632Sian			vfs_opterror(opts, "osreldate cannot be 0");
1026280632Sian			goto done_errmsg;
1027280632Sian		}
1028280632Sian	}
1029280632Sian
1030191673Sjamie	/*
1031298833Sjamie	 * Find the specified jail, or at least its parent.
1032191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
1033185435Sbz	 */
1034191673Sjamie	pr = NULL;
1035298833Sjamie	ppr = mypr;
1036196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1037196835Sjamie		namelc = strrchr(name, '.');
1038196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1039196835Sjamie		if (*p != '\0')
1040196835Sjamie			jid = 0;
1041196835Sjamie	}
1042298833Sjamie	sx_xlock(&allprison_lock);
1043191673Sjamie	if (jid != 0) {
1044192895Sjamie		/*
1045192895Sjamie		 * See if a requested jid already exists.  There is an
1046192895Sjamie		 * information leak here if the jid exists but is not within
1047192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
1048192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
1049192895Sjamie		 * will return ENOENT which is not normally a valid error.
1050192895Sjamie		 */
1051191673Sjamie		if (jid < 0) {
1052191673Sjamie			error = EINVAL;
1053191673Sjamie			vfs_opterror(opts, "negative jid");
1054191673Sjamie			goto done_unlock_list;
1055191673Sjamie		}
1056191673Sjamie		pr = prison_find(jid);
1057191673Sjamie		if (pr != NULL) {
1058192895Sjamie			ppr = pr->pr_parent;
1059191673Sjamie			/* Create: jid must not exist. */
1060191673Sjamie			if (cuflags == JAIL_CREATE) {
1061191673Sjamie				mtx_unlock(&pr->pr_mtx);
1062191673Sjamie				error = EEXIST;
1063191673Sjamie				vfs_opterror(opts, "jail %d already exists",
1064191673Sjamie				    jid);
1065191673Sjamie				goto done_unlock_list;
1066191673Sjamie			}
1067192895Sjamie			if (!prison_ischild(mypr, pr)) {
1068192895Sjamie				mtx_unlock(&pr->pr_mtx);
1069192895Sjamie				pr = NULL;
1070192895Sjamie			} else if (pr->pr_uref == 0) {
1071191673Sjamie				if (!(flags & JAIL_DYING)) {
1072191673Sjamie					mtx_unlock(&pr->pr_mtx);
1073191673Sjamie					error = ENOENT;
1074191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1075191673Sjamie					    jid);
1076191673Sjamie					goto done_unlock_list;
1077191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
1078191673Sjamie				    (pr_flags & PR_PERSIST)) {
1079191673Sjamie					/*
1080191673Sjamie					 * A dying jail might be resurrected
1081191673Sjamie					 * (via attach or persist), but first
1082191673Sjamie					 * it must determine if another jail
1083191673Sjamie					 * has claimed its name.  Accomplish
1084191673Sjamie					 * this by implicitly re-setting the
1085191673Sjamie					 * name.
1086191673Sjamie					 */
1087191673Sjamie					if (name == NULL)
1088192895Sjamie						name = prison_name(mypr, pr);
1089191673Sjamie				}
1090191673Sjamie			}
1091191673Sjamie		}
1092191673Sjamie		if (pr == NULL) {
1093191673Sjamie			/* Update: jid must exist. */
1094191673Sjamie			if (cuflags == JAIL_UPDATE) {
1095191673Sjamie				error = ENOENT;
1096191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1097191673Sjamie				goto done_unlock_list;
1098191673Sjamie			}
1099191673Sjamie		}
1100191673Sjamie	}
1101191673Sjamie	/*
1102191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1103191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1104191673Sjamie	 * (where the name must not already exist in a different jail),
1105191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1106191673Sjamie	 * because that is the jail being updated).
1107191673Sjamie	 */
1108298833Sjamie	namelc = NULL;
1109191673Sjamie	if (name != NULL) {
1110196835Sjamie		namelc = strrchr(name, '.');
1111196835Sjamie		if (namelc == NULL)
1112196835Sjamie			namelc = name;
1113196835Sjamie		else {
1114192895Sjamie			/*
1115192895Sjamie			 * This is a hierarchical name.  Split it into the
1116192895Sjamie			 * parent and child names, and make sure the parent
1117192895Sjamie			 * exists or matches an already found jail.
1118192895Sjamie			 */
1119192895Sjamie			if (pr != NULL) {
1120196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1121196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1122192895Sjamie					mtx_unlock(&pr->pr_mtx);
1123192895Sjamie					error = EINVAL;
1124192895Sjamie					vfs_opterror(opts,
1125192895Sjamie					    "cannot change jail's parent");
1126192895Sjamie					goto done_unlock_list;
1127192895Sjamie				}
1128192895Sjamie			} else {
1129298833Sjamie				*namelc = '\0';
1130192895Sjamie				ppr = prison_find_name(mypr, name);
1131192895Sjamie				if (ppr == NULL) {
1132192895Sjamie					error = ENOENT;
1133192895Sjamie					vfs_opterror(opts,
1134192895Sjamie					    "jail \"%s\" not found", name);
1135192895Sjamie					goto done_unlock_list;
1136192895Sjamie				}
1137192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1138298833Sjamie				*namelc = '.';
1139192895Sjamie			}
1140298833Sjamie			namelc++;
1141192895Sjamie		}
1142298833Sjamie		if (namelc[0] != '\0') {
1143298833Sjamie			pnamelen =
1144192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1145192895Sjamie name_again:
1146191673Sjamie			deadpr = NULL;
1147192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1148191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1149298833Sjamie				    !strcmp(tpr->pr_name + pnamelen, namelc)) {
1150191673Sjamie					if (pr == NULL &&
1151191673Sjamie					    cuflags != JAIL_CREATE) {
1152191673Sjamie						mtx_lock(&tpr->pr_mtx);
1153191673Sjamie						if (tpr->pr_ref > 0) {
1154191673Sjamie							/*
1155191673Sjamie							 * Use this jail
1156191673Sjamie							 * for updates.
1157191673Sjamie							 */
1158191673Sjamie							if (tpr->pr_uref > 0) {
1159191673Sjamie								pr = tpr;
1160191673Sjamie								break;
1161191673Sjamie							}
1162191673Sjamie							deadpr = tpr;
1163191673Sjamie						}
1164191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1165191673Sjamie					} else if (tpr->pr_uref > 0) {
1166191673Sjamie						/*
1167191673Sjamie						 * Create, or update(jid):
1168191673Sjamie						 * name must not exist in an
1169192895Sjamie						 * active sibling jail.
1170191673Sjamie						 */
1171191673Sjamie						error = EEXIST;
1172191673Sjamie						if (pr != NULL)
1173191673Sjamie							mtx_unlock(&pr->pr_mtx);
1174191673Sjamie						vfs_opterror(opts,
1175191673Sjamie						   "jail \"%s\" already exists",
1176191673Sjamie						   name);
1177191673Sjamie						goto done_unlock_list;
1178191673Sjamie					}
1179191673Sjamie				}
1180191673Sjamie			}
1181191673Sjamie			/* If no active jail is found, use a dying one. */
1182191673Sjamie			if (deadpr != NULL && pr == NULL) {
1183191673Sjamie				if (flags & JAIL_DYING) {
1184191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1185191673Sjamie					if (deadpr->pr_ref == 0) {
1186191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1187191673Sjamie						goto name_again;
1188191673Sjamie					}
1189191673Sjamie					pr = deadpr;
1190191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1191191673Sjamie					error = ENOENT;
1192191673Sjamie					vfs_opterror(opts,
1193191673Sjamie					    "jail \"%s\" is dying", name);
1194191673Sjamie					goto done_unlock_list;
1195191673Sjamie				}
1196191673Sjamie			}
1197191673Sjamie			/* Update: name must exist if no jid. */
1198191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1199191673Sjamie				error = ENOENT;
1200191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1201191673Sjamie				    name);
1202191673Sjamie				goto done_unlock_list;
1203191673Sjamie			}
1204191673Sjamie		}
1205191673Sjamie	}
1206191673Sjamie	/* Update: must provide a jid or name. */
1207191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1208191673Sjamie		error = ENOENT;
1209191673Sjamie		vfs_opterror(opts, "update specified no jail");
1210191673Sjamie		goto done_unlock_list;
1211191673Sjamie	}
1212185435Sbz
1213191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1214191673Sjamie	if (pr == NULL) {
1215194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1216194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1217194762Sjamie				error = EPERM;
1218194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1219194762Sjamie				goto done_unlock_list;
1220194762Sjamie			}
1221191673Sjamie		created = 1;
1222192895Sjamie		mtx_lock(&ppr->pr_mtx);
1223298832Sjamie		if (ppr->pr_ref == 0) {
1224192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1225192895Sjamie			error = ENOENT;
1226298833Sjamie			vfs_opterror(opts, "jail \"%s\" not found",
1227298833Sjamie			    prison_name(mypr, ppr));
1228192895Sjamie			goto done_unlock_list;
1229192895Sjamie		}
1230192895Sjamie		ppr->pr_ref++;
1231192895Sjamie		ppr->pr_uref++;
1232192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1233191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1234191673Sjamie		if (jid == 0) {
1235191673Sjamie			/* Find the next free jid. */
1236191673Sjamie			jid = lastprid + 1;
1237191673Sjamie findnext:
1238191673Sjamie			if (jid == JAIL_MAX)
1239191673Sjamie				jid = 1;
1240191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1241191673Sjamie				if (tpr->pr_id < jid)
1242191673Sjamie					continue;
1243191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1244191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1245191673Sjamie					break;
1246191673Sjamie				}
1247191673Sjamie				if (jid == lastprid) {
1248191673Sjamie					error = EAGAIN;
1249191673Sjamie					vfs_opterror(opts,
1250191673Sjamie					    "no available jail IDs");
1251191673Sjamie					free(pr, M_PRISON);
1252192895Sjamie					prison_deref(ppr, PD_DEREF |
1253192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1254192895Sjamie					goto done_releroot;
1255191673Sjamie				}
1256191673Sjamie				jid++;
1257191673Sjamie				goto findnext;
1258191673Sjamie			}
1259191673Sjamie			lastprid = jid;
1260191673Sjamie		} else {
1261191673Sjamie			/*
1262191673Sjamie			 * The jail already has a jid (that did not yet exist),
1263191673Sjamie			 * so just find where to insert it.
1264191673Sjamie			 */
1265191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1266191673Sjamie				if (tpr->pr_id >= jid) {
1267191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1268191673Sjamie					break;
1269191673Sjamie				}
1270191673Sjamie		}
1271191673Sjamie		if (tpr == NULL)
1272191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1273192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1274192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1275194762Sjamie			tpr->pr_childcount++;
1276185435Sbz
1277192895Sjamie		pr->pr_parent = ppr;
1278191673Sjamie		pr->pr_id = jid;
1279192895Sjamie
1280192895Sjamie		/* Set some default values, and inherit some from the parent. */
1281298833Sjamie		if (namelc == NULL)
1282298833Sjamie			namelc = "";
1283191673Sjamie		if (path == NULL) {
1284191673Sjamie			path = "/";
1285192895Sjamie			root = mypr->pr_root;
1286191673Sjamie			vref(root);
1287191673Sjamie		}
1288195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1289195944Sjamie		pr->pr_flags |= PR_HOST;
1290195945Sjamie#if defined(INET) || defined(INET6)
1291195945Sjamie#ifdef VIMAGE
1292195945Sjamie		if (!(pr_flags & PR_VNET))
1293195945Sjamie#endif
1294195945Sjamie		{
1295192895Sjamie#ifdef INET
1296195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1297195974Sjamie				pr->pr_flags |=
1298195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1299195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1300195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1301195974Sjamie				if (ppr->pr_ip4 != NULL) {
1302195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1303195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1304195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1305195974Sjamie					    M_WAITOK);
1306195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1307195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1308195974Sjamie				}
1309195974Sjamie			}
1310192895Sjamie#endif
1311192895Sjamie#ifdef INET6
1312195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1313195974Sjamie				pr->pr_flags |=
1314195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1315195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1316195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1317195974Sjamie				if (ppr->pr_ip6 != NULL) {
1318195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1319195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1320195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1321195974Sjamie					    M_WAITOK);
1322195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1323195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1324195974Sjamie				}
1325195974Sjamie			}
1326192895Sjamie#endif
1327195945Sjamie		}
1328195945Sjamie#endif
1329202468Sbz		/* Source address selection is always on by default. */
1330202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1331202468Sbz
1332192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1333192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1334196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1335232059Smm		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1336191673Sjamie
1337280632Sian		pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1338280632Sian		if (osrelstr == NULL)
1339280632Sian		    strcpy(pr->pr_osrelease, ppr->pr_osrelease);
1340280632Sian		else
1341280632Sian		    strcpy(pr->pr_osrelease, osrelstr);
1342280632Sian
1343192895Sjamie		LIST_INIT(&pr->pr_children);
1344192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1345298833Sjamie		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1346191673Sjamie
1347194251Sjamie#ifdef VIMAGE
1348194251Sjamie		/* Allocate a new vnet if specified. */
1349194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1350194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1351194251Sjamie#endif
1352185435Sbz		/*
1353191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1354191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1355185435Sbz		 */
1356192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1357191673Sjamie		if (error) {
1358191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1359191673Sjamie			goto done_releroot;
1360191673Sjamie		}
1361185435Sbz
1362191673Sjamie		mtx_lock(&pr->pr_mtx);
1363185435Sbz		/*
1364191673Sjamie		 * New prisons do not yet have a reference, because we do not
1365298833Sjamie		 * want others to see the incomplete prison once the
1366191673Sjamie		 * allprison_lock is downgraded.
1367185435Sbz		 */
1368191673Sjamie	} else {
1369191673Sjamie		created = 0;
1370195974Sjamie		/*
1371195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1372195974Sjamie		 * continue to exist for the duration of the call.
1373195974Sjamie		 */
1374195974Sjamie		pr->pr_ref++;
1375195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1376195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1377195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1378195945Sjamie			error = EINVAL;
1379195945Sjamie			vfs_opterror(opts,
1380195945Sjamie			    "vnet jails cannot have IP address restrictions");
1381195945Sjamie			goto done_deref_locked;
1382195945Sjamie		}
1383195945Sjamie#endif
1384195974Sjamie#ifdef INET
1385195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1386195974Sjamie			error = EINVAL;
1387195974Sjamie			vfs_opterror(opts,
1388195974Sjamie			    "ip4 cannot be changed after creation");
1389195974Sjamie			goto done_deref_locked;
1390195974Sjamie		}
1391195974Sjamie#endif
1392195974Sjamie#ifdef INET6
1393195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1394195974Sjamie			error = EINVAL;
1395195974Sjamie			vfs_opterror(opts,
1396195974Sjamie			    "ip6 cannot be changed after creation");
1397195974Sjamie			goto done_deref_locked;
1398195974Sjamie		}
1399195974Sjamie#endif
1400191673Sjamie	}
1401185435Sbz
1402191673Sjamie	/* Do final error checking before setting anything. */
1403192895Sjamie	if (gotslevel) {
1404192895Sjamie		if (slevel < ppr->pr_securelevel) {
1405192895Sjamie			error = EPERM;
1406192895Sjamie			goto done_deref_locked;
1407192895Sjamie		}
1408192895Sjamie	}
1409194762Sjamie	if (gotchildmax) {
1410194762Sjamie		if (childmax >= ppr->pr_childmax) {
1411194762Sjamie			error = EPERM;
1412194762Sjamie			goto done_deref_locked;
1413194762Sjamie		}
1414194762Sjamie	}
1415192895Sjamie	if (gotenforce) {
1416192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1417192895Sjamie			error = EPERM;
1418192895Sjamie			goto done_deref_locked;
1419192895Sjamie		}
1420192895Sjamie	}
1421231267Smm	if (gotrsnum) {
1422231267Smm		/*
1423231267Smm		 * devfs_rsnum is a uint16_t
1424231267Smm		 */
1425232059Smm		if (rsnum < 0 || rsnum > 65535) {
1426231267Smm			error = EINVAL;
1427231267Smm			goto done_deref_locked;
1428231267Smm		}
1429231267Smm		/*
1430232059Smm		 * Nested jails always inherit parent's devfs ruleset
1431231267Smm		 */
1432231267Smm		if (jailed(td->td_ucred)) {
1433231267Smm			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1434231267Smm				error = EPERM;
1435231267Smm				goto done_deref_locked;
1436232059Smm			} else
1437231267Smm				rsnum = ppr->pr_devfs_rsnum;
1438231267Smm		}
1439231267Smm	}
1440185435Sbz#ifdef INET
1441195974Sjamie	if (ip4s > 0) {
1442192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1443195974Sjamie			/*
1444195974Sjamie			 * Make sure the new set of IP addresses is a
1445195974Sjamie			 * subset of the parent's list.  Don't worry
1446195974Sjamie			 * about the parent being unlocked, as any
1447195974Sjamie			 * setting is done with allprison_lock held.
1448195974Sjamie			 */
1449195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1450195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1451195974Sjamie					break;
1452195974Sjamie			if (ij == ppr->pr_ip4s) {
1453195974Sjamie				error = EPERM;
1454195974Sjamie				goto done_deref_locked;
1455195974Sjamie			}
1456195974Sjamie			if (ip4s > 1) {
1457195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1458195974Sjamie					if (ip4[ii].s_addr ==
1459195974Sjamie					    ppr->pr_ip4[0].s_addr)
1460195974Sjamie						continue;
1461195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1462195974Sjamie						if (ip4[ii].s_addr ==
1463195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1464195974Sjamie							break;
1465195974Sjamie					if (ij == ppr->pr_ip4s)
1466195974Sjamie						break;
1467192895Sjamie				}
1468192895Sjamie				if (ij == ppr->pr_ip4s) {
1469192895Sjamie					error = EPERM;
1470192895Sjamie					goto done_deref_locked;
1471192895Sjamie				}
1472192895Sjamie			}
1473192895Sjamie		}
1474195974Sjamie		/*
1475195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1476195974Sjamie		 * if there is no more than one IP on each jail.  If
1477195974Sjamie		 * there is a duplicate on a jail with more than one
1478195974Sjamie		 * IP stop checking and return error.
1479195974Sjamie		 */
1480195974Sjamie		tppr = ppr;
1481195945Sjamie#ifdef VIMAGE
1482195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1483195974Sjamie			if (tppr->pr_flags & PR_VNET)
1484195974Sjamie				break;
1485195945Sjamie#endif
1486195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1487195974Sjamie			if (tpr == pr ||
1488195945Sjamie#ifdef VIMAGE
1489195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1490195945Sjamie#endif
1491195974Sjamie			    tpr->pr_uref == 0) {
1492192895Sjamie				descend = 0;
1493195974Sjamie				continue;
1494195974Sjamie			}
1495195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1496195974Sjamie				continue;
1497195974Sjamie			descend = 0;
1498195974Sjamie			if (tpr->pr_ip4 == NULL ||
1499195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1500195974Sjamie				continue;
1501195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1502195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1503195974Sjamie					error = EADDRINUSE;
1504195974Sjamie					vfs_opterror(opts,
1505195974Sjamie					    "IPv4 addresses clash");
1506195974Sjamie					goto done_deref_locked;
1507192895Sjamie				}
1508192895Sjamie			}
1509192895Sjamie		}
1510192895Sjamie	}
1511185435Sbz#endif
1512191673Sjamie#ifdef INET6
1513195974Sjamie	if (ip6s > 0) {
1514192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1515195974Sjamie			/*
1516195974Sjamie			 * Make sure the new set of IP addresses is a
1517195974Sjamie			 * subset of the parent's list.
1518195974Sjamie			 */
1519195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1520195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1521195974Sjamie				    &ppr->pr_ip6[ij]))
1522195974Sjamie					break;
1523195974Sjamie			if (ij == ppr->pr_ip6s) {
1524195974Sjamie				error = EPERM;
1525195974Sjamie				goto done_deref_locked;
1526195974Sjamie			}
1527195974Sjamie			if (ip6s > 1) {
1528195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1529195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1530195974Sjamie					     &ppr->pr_ip6[0]))
1531195974Sjamie						continue;
1532195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1533195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1534195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1535195974Sjamie							break;
1536195974Sjamie					if (ij == ppr->pr_ip6s)
1537195974Sjamie						break;
1538192895Sjamie				}
1539192895Sjamie				if (ij == ppr->pr_ip6s) {
1540192895Sjamie					error = EPERM;
1541192895Sjamie					goto done_deref_locked;
1542192895Sjamie				}
1543192895Sjamie			}
1544192895Sjamie		}
1545195974Sjamie		/* Check for conflicting IP addresses. */
1546195974Sjamie		tppr = ppr;
1547195945Sjamie#ifdef VIMAGE
1548195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1549195974Sjamie			if (tppr->pr_flags & PR_VNET)
1550195974Sjamie				break;
1551195945Sjamie#endif
1552195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1553195974Sjamie			if (tpr == pr ||
1554195945Sjamie#ifdef VIMAGE
1555195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1556195945Sjamie#endif
1557195974Sjamie			    tpr->pr_uref == 0) {
1558192895Sjamie				descend = 0;
1559195974Sjamie				continue;
1560195974Sjamie			}
1561195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1562195974Sjamie				continue;
1563195974Sjamie			descend = 0;
1564195974Sjamie			if (tpr->pr_ip6 == NULL ||
1565195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1566195974Sjamie				continue;
1567195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1568195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1569195974Sjamie					error = EADDRINUSE;
1570195974Sjamie					vfs_opterror(opts,
1571195974Sjamie					    "IPv6 addresses clash");
1572195974Sjamie					goto done_deref_locked;
1573192895Sjamie				}
1574192895Sjamie			}
1575191673Sjamie		}
1576192895Sjamie	}
1577191673Sjamie#endif
1578192895Sjamie	onamelen = namelen = 0;
1579298833Sjamie	if (namelc != NULL) {
1580292416Sjamie		/* Give a default name of the jid.  Also allow the name to be
1581292416Sjamie		 * explicitly the jid - but not any other number, and only in
1582292416Sjamie		 * normal form (no leading zero/etc).
1583292416Sjamie		 */
1584298833Sjamie		if (namelc[0] == '\0')
1585298833Sjamie			snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1586292416Sjamie		else if ((strtoul(namelc, &p, 10) != jid ||
1587292416Sjamie			  namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1588191673Sjamie			error = EINVAL;
1589196835Sjamie			vfs_opterror(opts,
1590196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1591192895Sjamie			goto done_deref_locked;
1592191673Sjamie		}
1593191673Sjamie		/*
1594192895Sjamie		 * Make sure the name isn't too long for the prison or its
1595192895Sjamie		 * children.
1596191673Sjamie		 */
1597298833Sjamie		pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1598298833Sjamie		onamelen = strlen(pr->pr_name + pnamelen);
1599298833Sjamie		namelen = strlen(namelc);
1600298833Sjamie		if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1601192895Sjamie			error = ENAMETOOLONG;
1602192895Sjamie			goto done_deref_locked;
1603192895Sjamie		}
1604192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1605192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1606192895Sjamie			    sizeof(pr->pr_name)) {
1607192895Sjamie				error = ENAMETOOLONG;
1608192895Sjamie				goto done_deref_locked;
1609192895Sjamie			}
1610192895Sjamie		}
1611191673Sjamie	}
1612192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1613192895Sjamie		error = EPERM;
1614192895Sjamie		goto done_deref_locked;
1615192895Sjamie	}
1616185435Sbz
1617298833Sjamie	/*
1618298833Sjamie	 * Let modules check their parameters.  This requires unlocking and
1619298833Sjamie	 * then re-locking the prison, but this is still a valid state as long
1620298833Sjamie	 * as allprison_lock remains xlocked.
1621298833Sjamie	 */
1622298833Sjamie	mtx_unlock(&pr->pr_mtx);
1623298833Sjamie	error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1624298833Sjamie	if (error != 0) {
1625298833Sjamie		prison_deref(pr, created
1626298833Sjamie		    ? PD_LIST_XLOCKED
1627298833Sjamie		    : PD_DEREF | PD_LIST_XLOCKED);
1628298833Sjamie		goto done_releroot;
1629298833Sjamie	}
1630298833Sjamie	mtx_lock(&pr->pr_mtx);
1631298833Sjamie
1632298833Sjamie	/* At this point, all valid parameters should have been noted. */
1633298833Sjamie	TAILQ_FOREACH(opt, opts, link) {
1634298833Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
1635298833Sjamie			error = EINVAL;
1636298833Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
1637298833Sjamie			goto done_deref_locked;
1638298833Sjamie		}
1639298833Sjamie	}
1640298833Sjamie
1641191673Sjamie	/* Set the parameters of the prison. */
1642191673Sjamie#ifdef INET
1643192895Sjamie	redo_ip4 = 0;
1644195974Sjamie	if (pr_flags & PR_IP4_USER) {
1645195974Sjamie		pr->pr_flags |= PR_IP4;
1646195974Sjamie		free(pr->pr_ip4, M_PRISON);
1647195974Sjamie		pr->pr_ip4s = ip4s;
1648195974Sjamie		pr->pr_ip4 = ip4;
1649195974Sjamie		ip4 = NULL;
1650192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1651195945Sjamie#ifdef VIMAGE
1652195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1653195945Sjamie				descend = 0;
1654195945Sjamie				continue;
1655195945Sjamie			}
1656195945Sjamie#endif
1657192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1658192895Sjamie				redo_ip4 = 1;
1659192895Sjamie				descend = 0;
1660192895Sjamie			}
1661192895Sjamie		}
1662185435Sbz	}
1663191673Sjamie#endif
1664191673Sjamie#ifdef INET6
1665192895Sjamie	redo_ip6 = 0;
1666195974Sjamie	if (pr_flags & PR_IP6_USER) {
1667195974Sjamie		pr->pr_flags |= PR_IP6;
1668195974Sjamie		free(pr->pr_ip6, M_PRISON);
1669195974Sjamie		pr->pr_ip6s = ip6s;
1670195974Sjamie		pr->pr_ip6 = ip6;
1671195974Sjamie		ip6 = NULL;
1672192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1673195945Sjamie#ifdef VIMAGE
1674195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1675195945Sjamie				descend = 0;
1676195945Sjamie				continue;
1677195945Sjamie			}
1678195945Sjamie#endif
1679192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1680192895Sjamie				redo_ip6 = 1;
1681192895Sjamie				descend = 0;
1682192895Sjamie			}
1683192895Sjamie		}
1684191673Sjamie	}
1685191673Sjamie#endif
1686192895Sjamie	if (gotslevel) {
1687191673Sjamie		pr->pr_securelevel = slevel;
1688192895Sjamie		/* Set all child jails to be at least this level. */
1689192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1690192895Sjamie			if (tpr->pr_securelevel < slevel)
1691192895Sjamie				tpr->pr_securelevel = slevel;
1692192895Sjamie	}
1693194762Sjamie	if (gotchildmax) {
1694194762Sjamie		pr->pr_childmax = childmax;
1695194762Sjamie		/* Set all child jails to under this limit. */
1696194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1697194762Sjamie			if (tpr->pr_childmax > childmax - level)
1698194762Sjamie				tpr->pr_childmax = childmax > level
1699194762Sjamie				    ? childmax - level : 0;
1700194762Sjamie	}
1701192895Sjamie	if (gotenforce) {
1702192895Sjamie		pr->pr_enforce_statfs = enforce;
1703192895Sjamie		/* Pass this restriction on to the children. */
1704192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1705192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1706192895Sjamie				tpr->pr_enforce_statfs = enforce;
1707192895Sjamie	}
1708231267Smm	if (gotrsnum) {
1709231267Smm		pr->pr_devfs_rsnum = rsnum;
1710231267Smm		/* Pass this restriction on to the children. */
1711231267Smm		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1712232059Smm			tpr->pr_devfs_rsnum = rsnum;
1713231267Smm	}
1714298833Sjamie	if (namelc != NULL) {
1715192895Sjamie		if (ppr == &prison0)
1716298833Sjamie			strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1717192895Sjamie		else
1718192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1719298833Sjamie			    ppr->pr_name, namelc);
1720192895Sjamie		/* Change this component of child names. */
1721192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1722192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1723192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1724192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1725192895Sjamie		}
1726192895Sjamie	}
1727191673Sjamie	if (path != NULL) {
1728192895Sjamie		/* Try to keep a real-rooted full pathname. */
1729230129Smm		if (fullpath_disabled && path[0] == '/' &&
1730230129Smm		    strcmp(mypr->pr_path, "/"))
1731192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1732192895Sjamie			    mypr->pr_path, path);
1733192895Sjamie		else
1734192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1735191673Sjamie		pr->pr_root = root;
1736191673Sjamie	}
1737193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1738193066Sjamie		if (pr->pr_flags & PR_HOST) {
1739193066Sjamie			/*
1740193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1741193066Sjamie			 * the lack of a lock on the parent is not a problem;
1742193066Sjamie			 * it is always set with allprison_lock at least
1743193066Sjamie			 * shared, and is held exclusively here.
1744193066Sjamie			 */
1745194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1746194118Sjamie			    sizeof(pr->pr_hostname));
1747194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1748194118Sjamie			    sizeof(pr->pr_domainname));
1749194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1750194118Sjamie			    sizeof(pr->pr_hostuuid));
1751193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1752193066Sjamie		}
1753193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1754193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1755193066Sjamie		if (host != NULL)
1756194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1757193066Sjamie		if (domain != NULL)
1758194118Sjamie			strlcpy(pr->pr_domainname, domain,
1759194118Sjamie			    sizeof(pr->pr_domainname));
1760193066Sjamie		if (uuid != NULL)
1761194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1762193066Sjamie		if (gothid)
1763193066Sjamie			pr->pr_hostid = hid;
1764193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1765193066Sjamie			if (tpr->pr_flags & PR_HOST)
1766193066Sjamie				descend = 0;
1767193066Sjamie			else {
1768193066Sjamie				if (host != NULL)
1769194118Sjamie					strlcpy(tpr->pr_hostname,
1770194118Sjamie					    pr->pr_hostname,
1771194118Sjamie					    sizeof(tpr->pr_hostname));
1772193066Sjamie				if (domain != NULL)
1773194118Sjamie					strlcpy(tpr->pr_domainname,
1774194118Sjamie					    pr->pr_domainname,
1775194118Sjamie					    sizeof(tpr->pr_domainname));
1776193066Sjamie				if (uuid != NULL)
1777194118Sjamie					strlcpy(tpr->pr_hostuuid,
1778194118Sjamie					    pr->pr_hostuuid,
1779194118Sjamie					    sizeof(tpr->pr_hostuuid));
1780193066Sjamie				if (gothid)
1781193066Sjamie					tpr->pr_hostid = hid;
1782193066Sjamie			}
1783193066Sjamie		}
1784193066Sjamie	}
1785192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1786192895Sjamie		/* Clear allow bits in all children. */
1787192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1788192895Sjamie			tpr->pr_allow &= ~tallow;
1789192895Sjamie	}
1790192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1791191673Sjamie	/*
1792191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1793191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1794191673Sjamie	 * for now, so new ones will remain unseen until after the module
1795191673Sjamie	 * handlers have completed.
1796191673Sjamie	 */
1797298833Sjamie	born = pr->pr_uref == 0;
1798191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1799191673Sjamie		if (pr_flags & PR_PERSIST) {
1800191673Sjamie			pr->pr_ref++;
1801191673Sjamie			pr->pr_uref++;
1802191673Sjamie		} else {
1803191673Sjamie			pr->pr_ref--;
1804191673Sjamie			pr->pr_uref--;
1805191673Sjamie		}
1806191673Sjamie	}
1807191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1808191673Sjamie	mtx_unlock(&pr->pr_mtx);
1809185435Sbz
1810221362Strasz#ifdef RACCT
1811284665Strasz	if (racct_enable && created)
1812221362Strasz		prison_racct_attach(pr);
1813221362Strasz#endif
1814221362Strasz
1815192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1816192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1817192895Sjamie	 */
1818192895Sjamie#ifdef INET
1819192895Sjamie	while (redo_ip4) {
1820192895Sjamie		ip4s = pr->pr_ip4s;
1821192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1822192895Sjamie		mtx_lock(&pr->pr_mtx);
1823192895Sjamie		redo_ip4 = 0;
1824192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1825195945Sjamie#ifdef VIMAGE
1826195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1827195945Sjamie				descend = 0;
1828195945Sjamie				continue;
1829195945Sjamie			}
1830195945Sjamie#endif
1831192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1832192895Sjamie				if (ip4 != NULL)
1833192895Sjamie					ip4 = NULL;
1834192895Sjamie				else
1835192895Sjamie					redo_ip4 = 1;
1836192895Sjamie			}
1837192895Sjamie		}
1838192895Sjamie		mtx_unlock(&pr->pr_mtx);
1839192895Sjamie	}
1840192895Sjamie#endif
1841192895Sjamie#ifdef INET6
1842192895Sjamie	while (redo_ip6) {
1843192895Sjamie		ip6s = pr->pr_ip6s;
1844192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1845192895Sjamie		mtx_lock(&pr->pr_mtx);
1846192895Sjamie		redo_ip6 = 0;
1847192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1848195945Sjamie#ifdef VIMAGE
1849195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1850195945Sjamie				descend = 0;
1851195945Sjamie				continue;
1852195945Sjamie			}
1853195945Sjamie#endif
1854192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1855192895Sjamie				if (ip6 != NULL)
1856192895Sjamie					ip6 = NULL;
1857192895Sjamie				else
1858192895Sjamie					redo_ip6 = 1;
1859192895Sjamie			}
1860192895Sjamie		}
1861192895Sjamie		mtx_unlock(&pr->pr_mtx);
1862192895Sjamie	}
1863192895Sjamie#endif
1864192895Sjamie
1865191673Sjamie	/* Let the modules do their work. */
1866191673Sjamie	sx_downgrade(&allprison_lock);
1867298833Sjamie	if (born) {
1868191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1869191673Sjamie		if (error) {
1870298833Sjamie			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1871298833Sjamie			prison_deref(pr, created
1872298833Sjamie			    ? PD_LIST_SLOCKED
1873298833Sjamie			    : PD_DEREF | PD_LIST_SLOCKED);
1874191673Sjamie			goto done_errmsg;
1875191673Sjamie		}
1876191673Sjamie	}
1877191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1878191673Sjamie	if (error) {
1879298833Sjamie		if (born)
1880298833Sjamie			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1881191673Sjamie		prison_deref(pr, created
1882191673Sjamie		    ? PD_LIST_SLOCKED
1883191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1884191673Sjamie		goto done_errmsg;
1885191673Sjamie	}
1886191673Sjamie
1887191673Sjamie	/* Attach this process to the prison if requested. */
1888191673Sjamie	if (flags & JAIL_ATTACH) {
1889191673Sjamie		mtx_lock(&pr->pr_mtx);
1890191673Sjamie		error = do_jail_attach(td, pr);
1891191673Sjamie		if (error) {
1892191673Sjamie			vfs_opterror(opts, "attach failed");
1893191673Sjamie			if (!created)
1894191673Sjamie				prison_deref(pr, PD_DEREF);
1895191673Sjamie			goto done_errmsg;
1896191673Sjamie		}
1897191673Sjamie	}
1898191673Sjamie
1899235803Strasz#ifdef RACCT
1900284665Strasz	if (racct_enable && !created) {
1901271622Strasz		if (!(flags & JAIL_ATTACH))
1902271622Strasz			sx_sunlock(&allprison_lock);
1903235803Strasz		prison_racct_modify(pr);
1904271622Strasz		if (!(flags & JAIL_ATTACH))
1905271622Strasz			sx_slock(&allprison_lock);
1906235803Strasz	}
1907235803Strasz#endif
1908235803Strasz
1909235803Strasz	td->td_retval[0] = pr->pr_id;
1910235803Strasz
1911191673Sjamie	/*
1912191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1913191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1914191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1915191673Sjamie	 * visible).
1916191673Sjamie	 */
1917191673Sjamie	if (!created) {
1918191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1919191673Sjamie		    ? PD_DEREF
1920191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1921191673Sjamie	} else {
1922191673Sjamie		if (pr_flags & PR_PERSIST) {
1923191673Sjamie			mtx_lock(&pr->pr_mtx);
1924191673Sjamie			pr->pr_ref++;
1925191673Sjamie			pr->pr_uref++;
1926191673Sjamie			mtx_unlock(&pr->pr_mtx);
1927191673Sjamie		}
1928191673Sjamie		if (!(flags & JAIL_ATTACH))
1929191673Sjamie			sx_sunlock(&allprison_lock);
1930191673Sjamie	}
1931232598Strasz
1932298833Sjamie	goto done_free;
1933191673Sjamie
1934192895Sjamie done_deref_locked:
1935192895Sjamie	prison_deref(pr, created
1936192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1937192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1938192895Sjamie	goto done_releroot;
1939191673Sjamie done_unlock_list:
1940191673Sjamie	sx_xunlock(&allprison_lock);
1941191673Sjamie done_releroot:
1942241896Skib	if (root != NULL)
1943191673Sjamie		vrele(root);
1944191673Sjamie done_errmsg:
1945191673Sjamie	if (error) {
1946191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1947191673Sjamie		if (errmsg_len > 0) {
1948191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1949191673Sjamie			if (errmsg_pos > 0) {
1950191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1951191673Sjamie					bcopy(errmsg,
1952191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1953191673Sjamie					   errmsg_len);
1954191673Sjamie				else
1955191673Sjamie					copyout(errmsg,
1956191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1957191673Sjamie					   errmsg_len);
1958191673Sjamie			}
1959191673Sjamie		}
1960191673Sjamie	}
1961191673Sjamie done_free:
1962191673Sjamie#ifdef INET
1963191673Sjamie	free(ip4, M_PRISON);
1964191673Sjamie#endif
1965191673Sjamie#ifdef INET6
1966191673Sjamie	free(ip6, M_PRISON);
1967191673Sjamie#endif
1968230407Smm	if (g_path != NULL)
1969230407Smm		free(g_path, M_TEMP);
1970191673Sjamie	vfs_freeopts(opts);
1971191673Sjamie	return (error);
1972191673Sjamie}
1973191673Sjamie
1974191673Sjamie
197582710Sdillon/*
1976191673Sjamie * struct jail_get_args {
1977191673Sjamie *	struct iovec *iovp;
1978191673Sjamie *	unsigned int iovcnt;
1979191673Sjamie *	int flags;
1980114168Smike * };
198182710Sdillon */
198246155Sphkint
1983225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap)
198446155Sphk{
1985191673Sjamie	struct uio *auio;
1986185435Sbz	int error;
1987185435Sbz
1988191673Sjamie	/* Check that we have an even number of iovecs. */
1989191673Sjamie	if (uap->iovcnt & 1)
1990191673Sjamie		return (EINVAL);
1991191673Sjamie
1992191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1993185435Sbz	if (error)
1994185435Sbz		return (error);
1995191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1996191673Sjamie	if (error == 0)
1997191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1998191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1999191673Sjamie	free(auio, M_IOV);
2000191673Sjamie	return (error);
2001191673Sjamie}
2002185435Sbz
2003191673Sjamieint
2004191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
2005191673Sjamie{
2006192895Sjamie	struct prison *pr, *mypr;
2007191673Sjamie	struct vfsopt *opt;
2008191673Sjamie	struct vfsoptlist *opts;
2009191673Sjamie	char *errmsg, *name;
2010192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
2011185435Sbz
2012191673Sjamie	if (flags & ~JAIL_GET_MASK)
2013191673Sjamie		return (EINVAL);
2014185435Sbz
2015191673Sjamie	/* Get the parameter list. */
2016191673Sjamie	error = vfs_buildopts(optuio, &opts);
2017191673Sjamie	if (error)
2018191673Sjamie		return (error);
2019191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2020192895Sjamie	mypr = td->td_ucred->cr_prison;
2021185435Sbz
2022191673Sjamie	/*
2023191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
2024191673Sjamie	 */
2025191673Sjamie	sx_slock(&allprison_lock);
2026191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2027191673Sjamie	if (error == 0) {
2028191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
2029192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
2030191673Sjamie				mtx_lock(&pr->pr_mtx);
2031191673Sjamie				if (pr->pr_ref > 0 &&
2032191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
2033191673Sjamie					break;
2034191673Sjamie				mtx_unlock(&pr->pr_mtx);
2035191673Sjamie			}
2036191673Sjamie		}
2037191673Sjamie		if (pr != NULL)
2038191673Sjamie			goto found_prison;
2039191673Sjamie		error = ENOENT;
2040191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
2041191673Sjamie		goto done_unlock_list;
2042191673Sjamie	} else if (error != ENOENT)
2043191673Sjamie		goto done_unlock_list;
2044185435Sbz
2045191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2046191673Sjamie	if (error == 0) {
2047191673Sjamie		if (jid != 0) {
2048192895Sjamie			pr = prison_find_child(mypr, jid);
2049191673Sjamie			if (pr != NULL) {
2050191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2051191673Sjamie					mtx_unlock(&pr->pr_mtx);
2052191673Sjamie					error = ENOENT;
2053191673Sjamie					vfs_opterror(opts, "jail %d is dying",
2054191673Sjamie					    jid);
2055191673Sjamie					goto done_unlock_list;
2056191673Sjamie				}
2057191673Sjamie				goto found_prison;
2058191673Sjamie			}
2059191673Sjamie			error = ENOENT;
2060191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
2061191673Sjamie			goto done_unlock_list;
2062191673Sjamie		}
2063191673Sjamie	} else if (error != ENOENT)
2064191673Sjamie		goto done_unlock_list;
206546155Sphk
2066191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
2067191673Sjamie	if (error == 0) {
2068191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
2069191673Sjamie			error = EINVAL;
2070191673Sjamie			goto done_unlock_list;
2071191673Sjamie		}
2072192895Sjamie		pr = prison_find_name(mypr, name);
2073191673Sjamie		if (pr != NULL) {
2074191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
2075191673Sjamie				mtx_unlock(&pr->pr_mtx);
2076191673Sjamie				error = ENOENT;
2077191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
2078191673Sjamie				    name);
2079191673Sjamie				goto done_unlock_list;
2080191673Sjamie			}
2081191673Sjamie			goto found_prison;
2082191673Sjamie		}
2083191673Sjamie		error = ENOENT;
2084191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
2085191673Sjamie		goto done_unlock_list;
2086191673Sjamie	} else if (error != ENOENT)
2087191673Sjamie		goto done_unlock_list;
2088185435Sbz
2089191673Sjamie	vfs_opterror(opts, "no jail specified");
2090191673Sjamie	error = ENOENT;
2091191673Sjamie	goto done_unlock_list;
2092191673Sjamie
2093191673Sjamie found_prison:
2094191673Sjamie	/* Get the parameters of the prison. */
2095191673Sjamie	pr->pr_ref++;
2096191673Sjamie	locked = PD_LOCKED;
2097191673Sjamie	td->td_retval[0] = pr->pr_id;
2098191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2099191673Sjamie	if (error != 0 && error != ENOENT)
2100191673Sjamie		goto done_deref;
2101192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2102192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2103191673Sjamie	if (error != 0 && error != ENOENT)
2104191673Sjamie		goto done_deref;
2105192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2106192895Sjamie	if (error != 0 && error != ENOENT)
2107192895Sjamie		goto done_deref;
2108192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2109191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
2110191673Sjamie	if (error != 0 && error != ENOENT)
2111191673Sjamie		goto done_deref;
2112192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2113191673Sjamie	if (error != 0 && error != ENOENT)
2114191673Sjamie		goto done_deref;
2115191673Sjamie#ifdef INET
2116191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2117191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2118191673Sjamie	if (error != 0 && error != ENOENT)
2119191673Sjamie		goto done_deref;
2120191673Sjamie#endif
2121191673Sjamie#ifdef INET6
2122191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2123191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2124191673Sjamie	if (error != 0 && error != ENOENT)
2125191673Sjamie		goto done_deref;
2126191673Sjamie#endif
2127191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2128191673Sjamie	    sizeof(pr->pr_securelevel));
2129191673Sjamie	if (error != 0 && error != ENOENT)
2130191673Sjamie		goto done_deref;
2131194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2132194762Sjamie	    sizeof(pr->pr_childcount));
2133194762Sjamie	if (error != 0 && error != ENOENT)
2134194762Sjamie		goto done_deref;
2135194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2136194762Sjamie	    sizeof(pr->pr_childmax));
2137194762Sjamie	if (error != 0 && error != ENOENT)
2138194762Sjamie		goto done_deref;
2139194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2140191673Sjamie	if (error != 0 && error != ENOENT)
2141191673Sjamie		goto done_deref;
2142194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2143193066Sjamie	if (error != 0 && error != ENOENT)
2144193066Sjamie		goto done_deref;
2145194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2146193066Sjamie	if (error != 0 && error != ENOENT)
2147193066Sjamie		goto done_deref;
2148205014Snwhitehorn#ifdef COMPAT_FREEBSD32
2149217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2150193066Sjamie		uint32_t hid32 = pr->pr_hostid;
2151193066Sjamie
2152193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2153193066Sjamie	} else
2154193066Sjamie#endif
2155193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2156193066Sjamie	    sizeof(pr->pr_hostid));
2157193066Sjamie	if (error != 0 && error != ENOENT)
2158193066Sjamie		goto done_deref;
2159192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2160192895Sjamie	    sizeof(pr->pr_enforce_statfs));
2161191673Sjamie	if (error != 0 && error != ENOENT)
2162191673Sjamie		goto done_deref;
2163231267Smm	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2164231267Smm	    sizeof(pr->pr_devfs_rsnum));
2165231267Smm	if (error != 0 && error != ENOENT)
2166231267Smm		goto done_deref;
2167192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2168192895Sjamie	    fi++) {
2169192895Sjamie		if (pr_flag_names[fi] == NULL)
2170192895Sjamie			continue;
2171192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2172192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2173192895Sjamie		if (error != 0 && error != ENOENT)
2174192895Sjamie			goto done_deref;
2175192895Sjamie		i = !i;
2176192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2177192895Sjamie		if (error != 0 && error != ENOENT)
2178192895Sjamie			goto done_deref;
2179192895Sjamie	}
2180195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2181195870Sjamie	    fi++) {
2182195870Sjamie		i = pr->pr_flags &
2183195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2184195870Sjamie		i = pr_flag_jailsys[fi].disable &&
2185195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2186195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2187195870Sjamie		    : JAIL_SYS_INHERIT;
2188195870Sjamie		error =
2189195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2190195870Sjamie		if (error != 0 && error != ENOENT)
2191195870Sjamie			goto done_deref;
2192195870Sjamie	}
2193192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2194192895Sjamie	    fi++) {
2195192895Sjamie		if (pr_allow_names[fi] == NULL)
2196192895Sjamie			continue;
2197192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2198192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2199192895Sjamie		if (error != 0 && error != ENOENT)
2200192895Sjamie			goto done_deref;
2201192895Sjamie		i = !i;
2202192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2203192895Sjamie		if (error != 0 && error != ENOENT)
2204192895Sjamie			goto done_deref;
2205192895Sjamie	}
2206191673Sjamie	i = (pr->pr_uref == 0);
2207191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2208191673Sjamie	if (error != 0 && error != ENOENT)
2209191673Sjamie		goto done_deref;
2210191673Sjamie	i = !i;
2211191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2212191673Sjamie	if (error != 0 && error != ENOENT)
2213191673Sjamie		goto done_deref;
2214280632Sian	error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2215280632Sian	    sizeof(pr->pr_osreldate));
2216280632Sian	if (error != 0 && error != ENOENT)
2217280632Sian		goto done_deref;
2218280632Sian	error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2219280632Sian	if (error != 0 && error != ENOENT)
2220280632Sian		goto done_deref;
2221191673Sjamie
2222191673Sjamie	/* Get the module parameters. */
2223191673Sjamie	mtx_unlock(&pr->pr_mtx);
2224191673Sjamie	locked = 0;
2225191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
222646155Sphk	if (error)
2227191673Sjamie		goto done_deref;
2228191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
222984828Sjhb
2230191673Sjamie	/* By now, all parameters should have been noted. */
2231191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2232191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2233191673Sjamie			error = EINVAL;
2234191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2235191673Sjamie			goto done_errmsg;
2236191673Sjamie		}
2237185435Sbz	}
2238191673Sjamie
2239191673Sjamie	/* Write the fetched parameters back to userspace. */
2240191673Sjamie	error = 0;
2241191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2242191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2243191673Sjamie			pos = 2 * opt->pos + 1;
2244191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2245191673Sjamie			if (opt->value != NULL) {
2246191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2247191673Sjamie					bcopy(opt->value,
2248191673Sjamie					    optuio->uio_iov[pos].iov_base,
2249191673Sjamie					    opt->len);
2250191673Sjamie				} else {
2251191673Sjamie					error = copyout(opt->value,
2252191673Sjamie					    optuio->uio_iov[pos].iov_base,
2253191673Sjamie					    opt->len);
2254191673Sjamie					if (error)
2255191673Sjamie						break;
2256191673Sjamie				}
2257191673Sjamie			}
2258191673Sjamie		}
2259185435Sbz	}
2260191673Sjamie	goto done_errmsg;
2261191673Sjamie
2262191673Sjamie done_deref:
2263191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2264191673Sjamie	goto done_errmsg;
2265191673Sjamie
2266191673Sjamie done_unlock_list:
2267191673Sjamie	sx_sunlock(&allprison_lock);
2268191673Sjamie done_errmsg:
2269191673Sjamie	if (error && errmsg_pos >= 0) {
2270191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2271191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2272191673Sjamie		if (errmsg_len > 0) {
2273191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2274191673Sjamie				bcopy(errmsg,
2275191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2276191673Sjamie				    errmsg_len);
2277191673Sjamie			else
2278191673Sjamie				copyout(errmsg,
2279191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2280191673Sjamie				    errmsg_len);
2281191673Sjamie		}
2282185435Sbz	}
2283191673Sjamie	vfs_freeopts(opts);
2284191673Sjamie	return (error);
2285191673Sjamie}
2286113275Smike
2287192895Sjamie
2288191673Sjamie/*
2289191673Sjamie * struct jail_remove_args {
2290191673Sjamie *	int jid;
2291191673Sjamie * };
2292191673Sjamie */
2293191673Sjamieint
2294225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2295191673Sjamie{
2296192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2297192895Sjamie	int descend, error;
2298185435Sbz
2299191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2300185435Sbz	if (error)
2301191673Sjamie		return (error);
2302185435Sbz
2303185435Sbz	sx_xlock(&allprison_lock);
2304192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2305191673Sjamie	if (pr == NULL) {
2306185435Sbz		sx_xunlock(&allprison_lock);
2307191673Sjamie		return (EINVAL);
2308185435Sbz	}
2309185435Sbz
2310192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2311192895Sjamie	pr->pr_ref++;
2312192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2313192895Sjamie		mtx_unlock(&pr->pr_mtx);
2314192895Sjamie		lpr = NULL;
2315192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2316192895Sjamie			mtx_lock(&cpr->pr_mtx);
2317192895Sjamie			if (cpr->pr_ref > 0) {
2318192895Sjamie				tpr = cpr;
2319192895Sjamie				cpr->pr_ref++;
2320192895Sjamie			} else {
2321192895Sjamie				/* Already removed - do not do it again. */
2322192895Sjamie				tpr = NULL;
2323192895Sjamie			}
2324192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2325192895Sjamie			if (lpr != NULL) {
2326192895Sjamie				mtx_lock(&lpr->pr_mtx);
2327192895Sjamie				prison_remove_one(lpr);
2328192895Sjamie				sx_xlock(&allprison_lock);
2329192895Sjamie			}
2330192895Sjamie			lpr = tpr;
2331192895Sjamie		}
2332192895Sjamie		if (lpr != NULL) {
2333192895Sjamie			mtx_lock(&lpr->pr_mtx);
2334192895Sjamie			prison_remove_one(lpr);
2335192895Sjamie			sx_xlock(&allprison_lock);
2336192895Sjamie		}
2337192895Sjamie		mtx_lock(&pr->pr_mtx);
2338192895Sjamie	}
2339192895Sjamie	prison_remove_one(pr);
2340192895Sjamie	return (0);
2341192895Sjamie}
2342192895Sjamie
2343192895Sjamiestatic void
2344192895Sjamieprison_remove_one(struct prison *pr)
2345192895Sjamie{
2346192895Sjamie	struct proc *p;
2347192895Sjamie	int deuref;
2348192895Sjamie
2349191673Sjamie	/* If the prison was persistent, it is not anymore. */
2350191673Sjamie	deuref = 0;
2351191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2352191673Sjamie		pr->pr_ref--;
2353191673Sjamie		deuref = PD_DEUREF;
2354191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2355179881Sdelphij	}
2356113275Smike
2357192895Sjamie	/*
2358192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2359192895Sjamie	 * the prison now.
2360192895Sjamie	 */
2361192895Sjamie	KASSERT(pr->pr_ref > 0,
2362192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2363192895Sjamie	if (pr->pr_ref == 1) {
2364191673Sjamie		prison_deref(pr,
2365191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2366192895Sjamie		return;
2367191673Sjamie	}
2368191673Sjamie
2369113275Smike	mtx_unlock(&pr->pr_mtx);
2370191673Sjamie	sx_xunlock(&allprison_lock);
2371191673Sjamie	/*
2372191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2373191673Sjamie	 */
2374191673Sjamie	sx_slock(&allproc_lock);
2375191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2376191673Sjamie		PROC_LOCK(p);
2377191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2378191673Sjamie		    p->p_ucred->cr_prison == pr)
2379225617Skmacy			kern_psignal(p, SIGKILL);
2380191673Sjamie		PROC_UNLOCK(p);
2381191673Sjamie	}
2382191673Sjamie	sx_sunlock(&allproc_lock);
2383192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2384191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2385113275Smike}
2386113275Smike
2387190466Sjamie
2388113275Smike/*
2389114168Smike * struct jail_attach_args {
2390114168Smike *	int jid;
2391114168Smike * };
2392113275Smike */
2393113275Smikeint
2394225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2395113275Smike{
2396113275Smike	struct prison *pr;
2397191673Sjamie	int error;
2398167309Spjd
2399164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2400126023Snectar	if (error)
2401126023Snectar		return (error);
2402126023Snectar
2403168401Spjd	sx_slock(&allprison_lock);
2404192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2405113275Smike	if (pr == NULL) {
2406168401Spjd		sx_sunlock(&allprison_lock);
2407113275Smike		return (EINVAL);
2408113275Smike	}
2409185435Sbz
2410185435Sbz	/*
2411185435Sbz	 * Do not allow a process to attach to a prison that is not
2412191673Sjamie	 * considered to be "alive".
2413185435Sbz	 */
2414191673Sjamie	if (pr->pr_uref == 0) {
2415185435Sbz		mtx_unlock(&pr->pr_mtx);
2416185435Sbz		sx_sunlock(&allprison_lock);
2417185435Sbz		return (EINVAL);
2418185435Sbz	}
2419191673Sjamie
2420191673Sjamie	return (do_jail_attach(td, pr));
2421191673Sjamie}
2422191673Sjamie
2423191673Sjamiestatic int
2424191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2425191673Sjamie{
2426191673Sjamie	struct proc *p;
2427191673Sjamie	struct ucred *newcred, *oldcred;
2428241896Skib	int error;
2429191673Sjamie
2430191673Sjamie	/*
2431191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2432191673Sjamie	 * in the same privileged process attempt to attach to two
2433191673Sjamie	 * different jails at the same time.  It is important for
2434191673Sjamie	 * user processes not to do this, or they might end up with
2435191673Sjamie	 * a process root from one prison, but attached to the jail
2436191673Sjamie	 * of another.
2437191673Sjamie	 */
2438113275Smike	pr->pr_ref++;
2439191673Sjamie	pr->pr_uref++;
2440113275Smike	mtx_unlock(&pr->pr_mtx);
2441191673Sjamie
2442191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2443191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2444191673Sjamie	if (error) {
2445191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2446191673Sjamie		return (error);
2447191673Sjamie	}
2448168401Spjd	sx_sunlock(&allprison_lock);
2449113275Smike
2450185435Sbz	/*
2451185435Sbz	 * Reparent the newly attached process to this jail.
2452185435Sbz	 */
2453191673Sjamie	p = td->td_proc;
2454185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2455185435Sbz	if (error)
2456191673Sjamie		goto e_revert_osd;
2457185435Sbz
2458175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2459113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2460113275Smike		goto e_unlock;
2461113275Smike#ifdef MAC
2462172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2463113275Smike		goto e_unlock;
2464113275Smike#endif
2465175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2466191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2467241896Skib		goto e_revert_osd;
2468113275Smike
246984828Sjhb	newcred = crget();
247084828Sjhb	PROC_LOCK(p);
2471298833Sjamie	oldcred = crcopysafe(p, newcred);
2472113630Sjhb	newcred->cr_prison = pr;
247384828Sjhb	p->p_ucred = newcred;
2474298833Sjamie	setsugid(p);
247584828Sjhb	PROC_UNLOCK(p);
2476220137Strasz#ifdef RACCT
2477220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
2478220137Strasz#endif
2479298833Sjamie	prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
248084828Sjhb	crfree(oldcred);
248146155Sphk	return (0);
2482298833Sjamie
2483191673Sjamie e_unlock:
2484175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2485191673Sjamie e_revert_osd:
2486191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2487298833Sjamie	(void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2488191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
248946155Sphk	return (error);
249046155Sphk}
249146155Sphk
2492192895Sjamie
2493113275Smike/*
2494113275Smike * Returns a locked prison instance, or NULL on failure.
2495113275Smike */
2496168399Spjdstruct prison *
2497113275Smikeprison_find(int prid)
2498113275Smike{
2499113275Smike	struct prison *pr;
2500113275Smike
2501168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2502191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2503113275Smike		if (pr->pr_id == prid) {
2504113275Smike			mtx_lock(&pr->pr_mtx);
2505191673Sjamie			if (pr->pr_ref > 0)
2506191673Sjamie				return (pr);
2507191673Sjamie			mtx_unlock(&pr->pr_mtx);
2508113275Smike		}
2509113275Smike	}
2510113275Smike	return (NULL);
2511113275Smike}
2512113275Smike
2513191673Sjamie/*
2514192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2515191673Sjamie */
2516191673Sjamiestruct prison *
2517192895Sjamieprison_find_child(struct prison *mypr, int prid)
2518191673Sjamie{
2519192895Sjamie	struct prison *pr;
2520192895Sjamie	int descend;
2521192895Sjamie
2522192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2523192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2524192895Sjamie		if (pr->pr_id == prid) {
2525192895Sjamie			mtx_lock(&pr->pr_mtx);
2526192895Sjamie			if (pr->pr_ref > 0)
2527192895Sjamie				return (pr);
2528192895Sjamie			mtx_unlock(&pr->pr_mtx);
2529192895Sjamie		}
2530192895Sjamie	}
2531192895Sjamie	return (NULL);
2532192895Sjamie}
2533192895Sjamie
2534192895Sjamie/*
2535192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2536192895Sjamie */
2537192895Sjamiestruct prison *
2538192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2539192895Sjamie{
2540191673Sjamie	struct prison *pr, *deadpr;
2541192895Sjamie	size_t mylen;
2542192895Sjamie	int descend;
2543191673Sjamie
2544191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2545192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2546191673Sjamie again:
2547191673Sjamie	deadpr = NULL;
2548192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2549192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2550191673Sjamie			mtx_lock(&pr->pr_mtx);
2551191673Sjamie			if (pr->pr_ref > 0) {
2552191673Sjamie				if (pr->pr_uref > 0)
2553191673Sjamie					return (pr);
2554191673Sjamie				deadpr = pr;
2555191673Sjamie			}
2556191673Sjamie			mtx_unlock(&pr->pr_mtx);
2557191673Sjamie		}
2558191673Sjamie	}
2559192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2560191673Sjamie	if (deadpr != NULL) {
2561191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2562191673Sjamie		if (deadpr->pr_ref == 0) {
2563191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2564191673Sjamie			goto again;
2565191673Sjamie		}
2566191673Sjamie	}
2567191673Sjamie	return (deadpr);
2568191673Sjamie}
2569191673Sjamie
2570191673Sjamie/*
2571192895Sjamie * See if a prison has the specific flag set.
2572192895Sjamie */
2573192895Sjamieint
2574192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2575192895Sjamie{
2576192895Sjamie
2577192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2578192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2579192895Sjamie}
2580192895Sjamie
2581192895Sjamieint
2582192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2583192895Sjamie{
2584192895Sjamie
2585192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2586192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2587192895Sjamie}
2588192895Sjamie
2589192895Sjamie/*
2590191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2591191673Sjamie * prison itself - but not in this context in case there are locks held.
2592191673Sjamie */
259372786Srwatsonvoid
2594185029Spjdprison_free_locked(struct prison *pr)
259572786Srwatson{
2596298833Sjamie	int ref;
259772786Srwatson
2598185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2599298833Sjamie	ref = --pr->pr_ref;
2600298833Sjamie	mtx_unlock(&pr->pr_mtx);
2601298833Sjamie	if (ref == 0)
2602144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
260372786Srwatson}
260472786Srwatson
2605185029Spjdvoid
2606185029Spjdprison_free(struct prison *pr)
2607185029Spjd{
2608185029Spjd
2609185029Spjd	mtx_lock(&pr->pr_mtx);
2610185029Spjd	prison_free_locked(pr);
2611185029Spjd}
2612185029Spjd
2613298833Sjamie/*
2614298833Sjamie * Complete a call to either prison_free or prison_proc_free.
2615298833Sjamie */
2616124882Srwatsonstatic void
2617124882Srwatsonprison_complete(void *context, int pending)
2618124882Srwatson{
2619298833Sjamie	struct prison *pr = context;
2620191673Sjamie
2621298833Sjamie	mtx_lock(&pr->pr_mtx);
2622298833Sjamie	prison_deref(pr, pr->pr_uref
2623298833Sjamie	    ? PD_DEREF | PD_DEUREF | PD_LOCKED : PD_LOCKED);
2624191673Sjamie}
2625191673Sjamie
2626191673Sjamie/*
2627191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2628191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2629191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2630191673Sjamie * and the allprison lock are both unlocked.
2631191673Sjamie */
2632191673Sjamiestatic void
2633191673Sjamieprison_deref(struct prison *pr, int flags)
2634191673Sjamie{
2635192895Sjamie	struct prison *ppr, *tpr;
2636298833Sjamie	int ref, lasturef;
2637124882Srwatson
2638191673Sjamie	if (!(flags & PD_LOCKED))
2639191673Sjamie		mtx_lock(&pr->pr_mtx);
2640225191Sjamie	for (;;) {
2641225191Sjamie		if (flags & PD_DEUREF) {
2642298833Sjamie			KASSERT(pr->pr_uref > 0,
2643298833Sjamie			    ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2644298833Sjamie			     pr->pr_id));
2645225191Sjamie			pr->pr_uref--;
2646298833Sjamie			lasturef = pr->pr_uref == 0;
2647298833Sjamie			if (lasturef)
2648298833Sjamie				pr->pr_ref++;
2649225191Sjamie			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2650298833Sjamie		} else
2651298833Sjamie			lasturef = 0;
2652298833Sjamie		if (flags & PD_DEREF) {
2653298833Sjamie			KASSERT(pr->pr_ref > 0,
2654298833Sjamie			    ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2655298833Sjamie			     pr->pr_id));
2656298833Sjamie			pr->pr_ref--;
2657192895Sjamie		}
2658298833Sjamie		ref = pr->pr_ref;
2659298833Sjamie		mtx_unlock(&pr->pr_mtx);
2660298833Sjamie
2661298833Sjamie		/*
2662298833Sjamie		 * Tell the modules if the last user reference was removed
2663298833Sjamie		 * (even it sticks around in dying state).
2664298833Sjamie		 */
2665298833Sjamie		if (lasturef) {
2666298833Sjamie			if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
2667298833Sjamie				if (ref > 1) {
2668298833Sjamie					sx_slock(&allprison_lock);
2669298833Sjamie					flags |= PD_LIST_SLOCKED;
2670298833Sjamie				} else {
2671298833Sjamie					sx_xlock(&allprison_lock);
2672298833Sjamie					flags |= PD_LIST_XLOCKED;
2673298833Sjamie				}
2674298833Sjamie			}
2675298833Sjamie			(void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2676298833Sjamie			mtx_lock(&pr->pr_mtx);
2677298833Sjamie			ref = --pr->pr_ref;
2678298833Sjamie			mtx_unlock(&pr->pr_mtx);
2679298833Sjamie		}
2680298833Sjamie
2681192895Sjamie		/* If the prison still has references, nothing else to do. */
2682298833Sjamie		if (ref > 0) {
2683192895Sjamie			if (flags & PD_LIST_SLOCKED)
2684192895Sjamie				sx_sunlock(&allprison_lock);
2685192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2686192895Sjamie				sx_xunlock(&allprison_lock);
2687192895Sjamie			return;
2688191673Sjamie		}
2689191673Sjamie
2690192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2691192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2692192895Sjamie				sx_sunlock(&allprison_lock);
2693192895Sjamie				sx_xlock(&allprison_lock);
2694192895Sjamie			}
2695192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2696192895Sjamie			sx_xlock(&allprison_lock);
2697168489Spjd
2698192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2699192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2700192895Sjamie		ppr = pr->pr_parent;
2701192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2702194762Sjamie			tpr->pr_childcount--;
2703196592Sjamie		sx_xunlock(&allprison_lock);
2704192895Sjamie
2705194251Sjamie#ifdef VIMAGE
2706196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2707194251Sjamie			vnet_destroy(pr->pr_vnet);
2708194251Sjamie#endif
2709241896Skib		if (pr->pr_root != NULL)
2710192895Sjamie			vrele(pr->pr_root);
2711192895Sjamie		mtx_destroy(&pr->pr_mtx);
2712191673Sjamie#ifdef INET
2713192895Sjamie		free(pr->pr_ip4, M_PRISON);
2714191673Sjamie#endif
2715185435Sbz#ifdef INET6
2716192895Sjamie		free(pr->pr_ip6, M_PRISON);
2717185435Sbz#endif
2718192895Sjamie		if (pr->pr_cpuset != NULL)
2719192895Sjamie			cpuset_rel(pr->pr_cpuset);
2720192895Sjamie		osd_jail_exit(pr);
2721221362Strasz#ifdef RACCT
2722284665Strasz		if (racct_enable)
2723284665Strasz			prison_racct_detach(pr);
2724220163Strasz#endif
2725192895Sjamie		free(pr, M_PRISON);
2726192895Sjamie
2727192895Sjamie		/* Removing a prison frees a reference on its parent. */
2728192895Sjamie		pr = ppr;
2729192895Sjamie		mtx_lock(&pr->pr_mtx);
2730225191Sjamie		flags = PD_DEREF | PD_DEUREF;
2731192895Sjamie	}
2732124882Srwatson}
2733124882Srwatson
273472786Srwatsonvoid
2735185029Spjdprison_hold_locked(struct prison *pr)
273672786Srwatson{
273772786Srwatson
2738185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2739168489Spjd	KASSERT(pr->pr_ref > 0,
2740191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
274172786Srwatson	pr->pr_ref++;
2742185029Spjd}
2743185029Spjd
2744185029Spjdvoid
2745185029Spjdprison_hold(struct prison *pr)
2746185029Spjd{
2747185029Spjd
2748185029Spjd	mtx_lock(&pr->pr_mtx);
2749185029Spjd	prison_hold_locked(pr);
275087275Srwatson	mtx_unlock(&pr->pr_mtx);
275172786Srwatson}
275272786Srwatson
2753185435Sbzvoid
2754185435Sbzprison_proc_hold(struct prison *pr)
275587275Srwatson{
275687275Srwatson
2757185435Sbz	mtx_lock(&pr->pr_mtx);
2758191673Sjamie	KASSERT(pr->pr_uref > 0,
2759191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2760191673Sjamie	pr->pr_uref++;
2761185435Sbz	mtx_unlock(&pr->pr_mtx);
276287275Srwatson}
276387275Srwatson
2764185435Sbzvoid
2765185435Sbzprison_proc_free(struct prison *pr)
2766185435Sbz{
2767185435Sbz
2768185435Sbz	mtx_lock(&pr->pr_mtx);
2769191673Sjamie	KASSERT(pr->pr_uref > 0,
2770191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2771298833Sjamie	if (pr->pr_uref > 1)
2772298833Sjamie		pr->pr_uref--;
2773298833Sjamie	else {
2774298833Sjamie		/*
2775298833Sjamie		 * Don't remove the last user reference in this context, which
2776298833Sjamie		 * is expected to be a process that is not only locked, but
2777298833Sjamie		 * also half dead.
2778298833Sjamie		 */
2779298833Sjamie		pr->pr_ref++;
2780298833Sjamie		mtx_unlock(&pr->pr_mtx);
2781298833Sjamie		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2782298833Sjamie		return;
2783298833Sjamie	}
2784298833Sjamie	mtx_unlock(&pr->pr_mtx);
2785185435Sbz}
2786185435Sbz
2787185435Sbz
2788185435Sbz#ifdef INET
2789185435Sbz/*
2790192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2791192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2792192895Sjamie */
2793192895Sjamiestatic int
2794192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2795192895Sjamie{
2796192895Sjamie	int ii, ij, used;
2797192895Sjamie	struct prison *ppr;
2798192895Sjamie
2799192895Sjamie	ppr = pr->pr_parent;
2800192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2801192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2802192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2803192895Sjamie			/*
2804192895Sjamie			 * There's no room for the parent's list.  Use the
2805192895Sjamie			 * new list buffer, which is assumed to be big enough
2806192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2807192895Sjamie			 * allocate one.
2808192895Sjamie			 */
2809192895Sjamie			used = 1;
2810192895Sjamie			if (newip4 == NULL) {
2811192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2812192895Sjamie				    M_PRISON, M_NOWAIT);
2813192895Sjamie				if (newip4 != NULL)
2814192895Sjamie					used = 0;
2815192895Sjamie			}
2816192895Sjamie			if (newip4 != NULL) {
2817192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2818192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2819192895Sjamie				free(pr->pr_ip4, M_PRISON);
2820192895Sjamie				pr->pr_ip4 = newip4;
2821192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2822192895Sjamie			}
2823192895Sjamie			return (used);
2824192895Sjamie		}
2825192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2826192895Sjamie		if (pr->pr_ip4s > 0)
2827192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2828192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2829192895Sjamie		else if (pr->pr_ip4 != NULL) {
2830192895Sjamie			free(pr->pr_ip4, M_PRISON);
2831192895Sjamie			pr->pr_ip4 = NULL;
2832192895Sjamie		}
2833195974Sjamie	} else if (pr->pr_ip4s > 0) {
2834192895Sjamie		/* Remove addresses that aren't in the parent. */
2835192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2836192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2837192895Sjamie				break;
2838192895Sjamie		if (ij < ppr->pr_ip4s)
2839192895Sjamie			ii = 1;
2840192895Sjamie		else {
2841192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2842192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2843192895Sjamie			ii = 0;
2844192895Sjamie		}
2845192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2846192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2847192895Sjamie				ii++;
2848192895Sjamie				continue;
2849192895Sjamie			}
2850192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2851192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2852192895Sjamie			case -1:
2853192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2854192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2855192895Sjamie				break;
2856192895Sjamie			case 0:
2857192895Sjamie				ii++;
2858192895Sjamie				ij++;
2859192895Sjamie				break;
2860192895Sjamie			case 1:
2861192895Sjamie				ij++;
2862192895Sjamie				break;
2863192895Sjamie			}
2864192895Sjamie		}
2865192895Sjamie		if (pr->pr_ip4s == 0) {
2866195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2867192895Sjamie			free(pr->pr_ip4, M_PRISON);
2868192895Sjamie			pr->pr_ip4 = NULL;
2869192895Sjamie		}
2870192895Sjamie	}
2871192895Sjamie	return (0);
2872192895Sjamie}
2873192895Sjamie
2874192895Sjamie/*
2875185435Sbz * Pass back primary IPv4 address of this jail.
2876185435Sbz *
2877192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2878192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2879185435Sbz *
2880188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2881188144Sjamie * Address returned in NBO.
2882185435Sbz */
288346155Sphkint
2884187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
288546155Sphk{
2886191673Sjamie	struct prison *pr;
288746155Sphk
2888185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2889185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2890185435Sbz
2891192895Sjamie	pr = cred->cr_prison;
2892192895Sjamie	if (!(pr->pr_flags & PR_IP4))
289346155Sphk		return (0);
2894191673Sjamie	mtx_lock(&pr->pr_mtx);
2895192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2896192895Sjamie		mtx_unlock(&pr->pr_mtx);
2897192895Sjamie		return (0);
2898192895Sjamie	}
2899191673Sjamie	if (pr->pr_ip4 == NULL) {
2900191673Sjamie		mtx_unlock(&pr->pr_mtx);
2901188144Sjamie		return (EAFNOSUPPORT);
2902191673Sjamie	}
2903185435Sbz
2904191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2905191673Sjamie	mtx_unlock(&pr->pr_mtx);
2906185435Sbz	return (0);
2907185435Sbz}
2908185435Sbz
2909185435Sbz/*
2910202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2911202468Sbz * We will return 0 if we should bypass source address selection in favour
2912202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2913202468Sbz * returned in NBO.
2914202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2915202468Sbz */
2916202468Sbzint
2917202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2918202468Sbz{
2919202468Sbz	struct prison *pr;
2920202468Sbz	struct in_addr lia;
2921202468Sbz	int error;
2922202468Sbz
2923202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2924202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2925202468Sbz
2926202468Sbz	if (!jailed(cred))
2927202468Sbz		return (1);
2928202468Sbz
2929202468Sbz	pr = cred->cr_prison;
2930202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2931202468Sbz		return (1);
2932202468Sbz
2933202468Sbz	lia.s_addr = INADDR_ANY;
2934202468Sbz	error = prison_get_ip4(cred, &lia);
2935202468Sbz	if (error)
2936202468Sbz		return (error);
2937202468Sbz	if (lia.s_addr == INADDR_ANY)
2938202468Sbz		return (1);
2939202468Sbz
2940202468Sbz	ia->s_addr = lia.s_addr;
2941202468Sbz	return (0);
2942202468Sbz}
2943202468Sbz
2944202468Sbz/*
2945192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2946192895Sjamie */
2947192895Sjamieint
2948192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2949192895Sjamie{
2950192895Sjamie
2951192895Sjamie	if (pr1 == pr2)
2952192895Sjamie		return (1);
2953192895Sjamie
2954192895Sjamie	/*
2955195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2956195974Sjamie	 * existing prisons.
2957192895Sjamie	 */
2958195945Sjamie	while (pr1 != &prison0 &&
2959195945Sjamie#ifdef VIMAGE
2960195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2961195945Sjamie#endif
2962195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2963192895Sjamie		pr1 = pr1->pr_parent;
2964195945Sjamie	while (pr2 != &prison0 &&
2965195945Sjamie#ifdef VIMAGE
2966195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2967195945Sjamie#endif
2968195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2969192895Sjamie		pr2 = pr2->pr_parent;
2970192895Sjamie	return (pr1 == pr2);
2971192895Sjamie}
2972192895Sjamie
2973192895Sjamie/*
2974185435Sbz * Make sure our (source) address is set to something meaningful to this
2975185435Sbz * jail.
2976185435Sbz *
2977192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2978192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2979192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2980185435Sbz */
2981185435Sbzint
2982185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2983185435Sbz{
2984191673Sjamie	struct prison *pr;
2985185435Sbz	struct in_addr ia0;
2986191673Sjamie	int error;
2987185435Sbz
2988185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2989185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2990185435Sbz
2991192895Sjamie	pr = cred->cr_prison;
2992192895Sjamie	if (!(pr->pr_flags & PR_IP4))
299346155Sphk		return (0);
2994191673Sjamie	mtx_lock(&pr->pr_mtx);
2995192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2996192895Sjamie		mtx_unlock(&pr->pr_mtx);
2997192895Sjamie		return (0);
2998192895Sjamie	}
2999191673Sjamie	if (pr->pr_ip4 == NULL) {
3000191673Sjamie		mtx_unlock(&pr->pr_mtx);
3001188144Sjamie		return (EAFNOSUPPORT);
3002191673Sjamie	}
3003185435Sbz
3004185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
3005185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
3006191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
3007191673Sjamie		mtx_unlock(&pr->pr_mtx);
3008185435Sbz		return (0);
300946155Sphk	}
3010185435Sbz
3011188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
3012188144Sjamie		/*
3013188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
3014188144Sjamie		 */
3015191673Sjamie		if (pr->pr_ip4s == 1)
3016191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
3017191673Sjamie		mtx_unlock(&pr->pr_mtx);
3018185435Sbz		return (0);
3019185435Sbz	}
3020185435Sbz
3021191673Sjamie	error = _prison_check_ip4(pr, ia);
3022191673Sjamie	mtx_unlock(&pr->pr_mtx);
3023191673Sjamie	return (error);
3024185435Sbz}
3025185435Sbz
3026185435Sbz/*
3027185435Sbz * Rewrite destination address in case we will connect to loopback address.
3028185435Sbz *
3029188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
3030188144Sjamie * Address passed in in NBO and returned in NBO.
3031185435Sbz */
3032185435Sbzint
3033185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
3034185435Sbz{
3035191673Sjamie	struct prison *pr;
3036185435Sbz
3037185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3038185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
3039185435Sbz
3040192895Sjamie	pr = cred->cr_prison;
3041192895Sjamie	if (!(pr->pr_flags & PR_IP4))
3042185435Sbz		return (0);
3043191673Sjamie	mtx_lock(&pr->pr_mtx);
3044192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
3045192895Sjamie		mtx_unlock(&pr->pr_mtx);
3046192895Sjamie		return (0);
3047192895Sjamie	}
3048191673Sjamie	if (pr->pr_ip4 == NULL) {
3049191673Sjamie		mtx_unlock(&pr->pr_mtx);
3050188144Sjamie		return (EAFNOSUPPORT);
3051191673Sjamie	}
3052188144Sjamie
3053185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
3054191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
3055191673Sjamie		mtx_unlock(&pr->pr_mtx);
3056185435Sbz		return (0);
3057185435Sbz	}
3058185435Sbz
3059185435Sbz	/*
3060185435Sbz	 * Return success because nothing had to be changed.
3061185435Sbz	 */
3062191673Sjamie	mtx_unlock(&pr->pr_mtx);
3063185435Sbz	return (0);
3064185435Sbz}
3065185435Sbz
3066185435Sbz/*
3067188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3068185435Sbz *
3069192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
3070192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3071192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
3072185435Sbz */
3073185435Sbzstatic int
3074185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
3075185435Sbz{
3076185435Sbz	int i, a, z, d;
3077185435Sbz
3078185435Sbz	/*
3079185435Sbz	 * Check the primary IP.
3080185435Sbz	 */
3081185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
3082188144Sjamie		return (0);
3083185435Sbz
3084185435Sbz	/*
3085185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3086185435Sbz	 */
3087185435Sbz	a = 0;
3088185435Sbz	z = pr->pr_ip4s - 2;
3089185435Sbz	while (a <= z) {
3090185435Sbz		i = (a + z) / 2;
3091185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
3092185435Sbz		if (d > 0)
3093185435Sbz			z = i - 1;
3094185435Sbz		else if (d < 0)
3095185435Sbz			a = i + 1;
309681114Srwatson		else
3097188144Sjamie			return (0);
3098185435Sbz	}
3099188144Sjamie
3100188144Sjamie	return (EADDRNOTAVAIL);
3101185435Sbz}
3102185435Sbz
3103185435Sbzint
3104185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
3105185435Sbz{
3106191673Sjamie	struct prison *pr;
3107191673Sjamie	int error;
3108185435Sbz
3109185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3110185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
3111185435Sbz
3112192895Sjamie	pr = cred->cr_prison;
3113192895Sjamie	if (!(pr->pr_flags & PR_IP4))
3114188144Sjamie		return (0);
3115191673Sjamie	mtx_lock(&pr->pr_mtx);
3116192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
3117192895Sjamie		mtx_unlock(&pr->pr_mtx);
3118192895Sjamie		return (0);
3119192895Sjamie	}
3120191673Sjamie	if (pr->pr_ip4 == NULL) {
3121191673Sjamie		mtx_unlock(&pr->pr_mtx);
3122188144Sjamie		return (EAFNOSUPPORT);
3123191673Sjamie	}
3124185435Sbz
3125191673Sjamie	error = _prison_check_ip4(pr, ia);
3126191673Sjamie	mtx_unlock(&pr->pr_mtx);
3127191673Sjamie	return (error);
3128185435Sbz}
3129185435Sbz#endif
3130185435Sbz
3131185435Sbz#ifdef INET6
3132192895Sjamiestatic int
3133192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
3134192895Sjamie{
3135192895Sjamie	int ii, ij, used;
3136192895Sjamie	struct prison *ppr;
3137192895Sjamie
3138192895Sjamie	ppr = pr->pr_parent;
3139192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
3140192895Sjamie		/* This has no user settings, so just copy the parent's list. */
3141192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
3142192895Sjamie			/*
3143192895Sjamie			 * There's no room for the parent's list.  Use the
3144192895Sjamie			 * new list buffer, which is assumed to be big enough
3145192895Sjamie			 * (if it was passed).  If there's no buffer, try to
3146192895Sjamie			 * allocate one.
3147192895Sjamie			 */
3148192895Sjamie			used = 1;
3149192895Sjamie			if (newip6 == NULL) {
3150192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
3151192895Sjamie				    M_PRISON, M_NOWAIT);
3152192895Sjamie				if (newip6 != NULL)
3153192895Sjamie					used = 0;
3154192895Sjamie			}
3155192895Sjamie			if (newip6 != NULL) {
3156192895Sjamie				bcopy(ppr->pr_ip6, newip6,
3157192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
3158192895Sjamie				free(pr->pr_ip6, M_PRISON);
3159192895Sjamie				pr->pr_ip6 = newip6;
3160192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
3161192895Sjamie			}
3162192895Sjamie			return (used);
3163192895Sjamie		}
3164192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
3165192895Sjamie		if (pr->pr_ip6s > 0)
3166192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
3167192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
3168192895Sjamie		else if (pr->pr_ip6 != NULL) {
3169192895Sjamie			free(pr->pr_ip6, M_PRISON);
3170192895Sjamie			pr->pr_ip6 = NULL;
3171192895Sjamie		}
3172195974Sjamie	} else if (pr->pr_ip6s > 0) {
3173192895Sjamie		/* Remove addresses that aren't in the parent. */
3174192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
3175192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
3176192895Sjamie			    &ppr->pr_ip6[ij]))
3177192895Sjamie				break;
3178192895Sjamie		if (ij < ppr->pr_ip6s)
3179192895Sjamie			ii = 1;
3180192895Sjamie		else {
3181192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
3182192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
3183192895Sjamie			ii = 0;
3184192895Sjamie		}
3185192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
3186192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
3187192895Sjamie			    &ppr->pr_ip6[0])) {
3188192895Sjamie				ii++;
3189192895Sjamie				continue;
3190192895Sjamie			}
3191259847Sae			switch (ij >= ppr->pr_ip6s ? -1 :
3192192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3193192895Sjamie			case -1:
3194192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3195192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3196192895Sjamie				break;
3197192895Sjamie			case 0:
3198192895Sjamie				ii++;
3199192895Sjamie				ij++;
3200192895Sjamie				break;
3201192895Sjamie			case 1:
3202192895Sjamie				ij++;
3203192895Sjamie				break;
3204192895Sjamie			}
3205192895Sjamie		}
3206192895Sjamie		if (pr->pr_ip6s == 0) {
3207195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
3208192895Sjamie			free(pr->pr_ip6, M_PRISON);
3209192895Sjamie			pr->pr_ip6 = NULL;
3210192895Sjamie		}
3211192895Sjamie	}
3212192895Sjamie	return 0;
3213192895Sjamie}
3214192895Sjamie
3215185435Sbz/*
3216185435Sbz * Pass back primary IPv6 address for this jail.
3217185435Sbz *
3218192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3219192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3220185435Sbz *
3221188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3222185435Sbz */
3223185435Sbzint
3224187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3225185435Sbz{
3226191673Sjamie	struct prison *pr;
3227185435Sbz
3228185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3229185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3230185435Sbz
3231192895Sjamie	pr = cred->cr_prison;
3232192895Sjamie	if (!(pr->pr_flags & PR_IP6))
323381114Srwatson		return (0);
3234191673Sjamie	mtx_lock(&pr->pr_mtx);
3235192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3236192895Sjamie		mtx_unlock(&pr->pr_mtx);
3237192895Sjamie		return (0);
3238192895Sjamie	}
3239191673Sjamie	if (pr->pr_ip6 == NULL) {
3240191673Sjamie		mtx_unlock(&pr->pr_mtx);
3241188144Sjamie		return (EAFNOSUPPORT);
3242191673Sjamie	}
3243188144Sjamie
3244191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3245191673Sjamie	mtx_unlock(&pr->pr_mtx);
3246185435Sbz	return (0);
3247185435Sbz}
3248185435Sbz
3249185435Sbz/*
3250202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3251202468Sbz * We will return 0 if we should bypass source address selection in favour
3252202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3253202468Sbz * returned in NBO.
3254202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3255202468Sbz */
3256202468Sbzint
3257202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3258202468Sbz{
3259202468Sbz	struct prison *pr;
3260202468Sbz	struct in6_addr lia6;
3261202468Sbz	int error;
3262202468Sbz
3263202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3264202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3265202468Sbz
3266202468Sbz	if (!jailed(cred))
3267202468Sbz		return (1);
3268202468Sbz
3269202468Sbz	pr = cred->cr_prison;
3270202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3271202468Sbz		return (1);
3272202468Sbz
3273202468Sbz	lia6 = in6addr_any;
3274202468Sbz	error = prison_get_ip6(cred, &lia6);
3275202468Sbz	if (error)
3276202468Sbz		return (error);
3277202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3278202468Sbz		return (1);
3279202468Sbz
3280202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3281202468Sbz	return (0);
3282202468Sbz}
3283202468Sbz
3284202468Sbz/*
3285192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3286192895Sjamie */
3287192895Sjamieint
3288192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3289192895Sjamie{
3290192895Sjamie
3291192895Sjamie	if (pr1 == pr2)
3292192895Sjamie		return (1);
3293192895Sjamie
3294195945Sjamie	while (pr1 != &prison0 &&
3295195945Sjamie#ifdef VIMAGE
3296195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3297195945Sjamie#endif
3298195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3299192895Sjamie		pr1 = pr1->pr_parent;
3300195945Sjamie	while (pr2 != &prison0 &&
3301195945Sjamie#ifdef VIMAGE
3302195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3303195945Sjamie#endif
3304195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3305192895Sjamie		pr2 = pr2->pr_parent;
3306192895Sjamie	return (pr1 == pr2);
3307192895Sjamie}
3308192895Sjamie
3309192895Sjamie/*
3310185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3311185435Sbz *
3312185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3313185435Sbz * when needed while binding.
3314185435Sbz *
3315192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3316192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3317192895Sjamie * doesn't allow IPv6.
3318185435Sbz */
3319185435Sbzint
3320185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3321185435Sbz{
3322191673Sjamie	struct prison *pr;
3323191673Sjamie	int error;
3324185435Sbz
3325185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3326185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3327185435Sbz
3328192895Sjamie	pr = cred->cr_prison;
3329192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3330185435Sbz		return (0);
3331191673Sjamie	mtx_lock(&pr->pr_mtx);
3332192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3333192895Sjamie		mtx_unlock(&pr->pr_mtx);
3334192895Sjamie		return (0);
3335192895Sjamie	}
3336191673Sjamie	if (pr->pr_ip6 == NULL) {
3337191673Sjamie		mtx_unlock(&pr->pr_mtx);
3338188144Sjamie		return (EAFNOSUPPORT);
3339191673Sjamie	}
3340188144Sjamie
3341185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3342191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3343191673Sjamie		mtx_unlock(&pr->pr_mtx);
3344185435Sbz		return (0);
334581114Srwatson	}
3346185435Sbz
3347188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3348188144Sjamie		/*
3349188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3350188144Sjamie		 * then bind directly.
3351188144Sjamie		 */
3352191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3353191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3354191673Sjamie		mtx_unlock(&pr->pr_mtx);
3355185435Sbz		return (0);
3356185435Sbz	}
3357188144Sjamie
3358191673Sjamie	error = _prison_check_ip6(pr, ia6);
3359191673Sjamie	mtx_unlock(&pr->pr_mtx);
3360191673Sjamie	return (error);
3361185435Sbz}
3362185435Sbz
3363185435Sbz/*
3364185435Sbz * Rewrite destination address in case we will connect to loopback address.
3365185435Sbz *
3366188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3367185435Sbz */
3368185435Sbzint
3369185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3370185435Sbz{
3371191673Sjamie	struct prison *pr;
3372185435Sbz
3373185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3374185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3375185435Sbz
3376192895Sjamie	pr = cred->cr_prison;
3377192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3378185435Sbz		return (0);
3379191673Sjamie	mtx_lock(&pr->pr_mtx);
3380192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3381192895Sjamie		mtx_unlock(&pr->pr_mtx);
3382192895Sjamie		return (0);
3383192895Sjamie	}
3384191673Sjamie	if (pr->pr_ip6 == NULL) {
3385191673Sjamie		mtx_unlock(&pr->pr_mtx);
3386188144Sjamie		return (EAFNOSUPPORT);
3387191673Sjamie	}
3388188144Sjamie
3389185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3390191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3391191673Sjamie		mtx_unlock(&pr->pr_mtx);
3392185435Sbz		return (0);
3393185435Sbz	}
3394185435Sbz
3395185435Sbz	/*
3396185435Sbz	 * Return success because nothing had to be changed.
3397185435Sbz	 */
3398191673Sjamie	mtx_unlock(&pr->pr_mtx);
339946155Sphk	return (0);
340046155Sphk}
340146155Sphk
3402185435Sbz/*
3403188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3404185435Sbz *
3405192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3406192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3407192895Sjamie * doesn't allow IPv6.
3408185435Sbz */
3409185435Sbzstatic int
3410185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
341146155Sphk{
3412185435Sbz	int i, a, z, d;
341346155Sphk
3414185435Sbz	/*
3415185435Sbz	 * Check the primary IP.
3416185435Sbz	 */
3417185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3418188144Sjamie		return (0);
3419185435Sbz
3420185435Sbz	/*
3421185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3422185435Sbz	 */
3423185435Sbz	a = 0;
3424185435Sbz	z = pr->pr_ip6s - 2;
3425185435Sbz	while (a <= z) {
3426185435Sbz		i = (a + z) / 2;
3427185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3428185435Sbz		if (d > 0)
3429185435Sbz			z = i - 1;
3430185435Sbz		else if (d < 0)
3431185435Sbz			a = i + 1;
343246155Sphk		else
3433188144Sjamie			return (0);
343446155Sphk	}
3435188144Sjamie
3436188144Sjamie	return (EADDRNOTAVAIL);
343746155Sphk}
343846155Sphk
343946155Sphkint
3440185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3441185435Sbz{
3442191673Sjamie	struct prison *pr;
3443191673Sjamie	int error;
3444185435Sbz
3445185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3446185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3447185435Sbz
3448192895Sjamie	pr = cred->cr_prison;
3449192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3450188144Sjamie		return (0);
3451191673Sjamie	mtx_lock(&pr->pr_mtx);
3452192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3453192895Sjamie		mtx_unlock(&pr->pr_mtx);
3454192895Sjamie		return (0);
3455192895Sjamie	}
3456191673Sjamie	if (pr->pr_ip6 == NULL) {
3457191673Sjamie		mtx_unlock(&pr->pr_mtx);
3458188144Sjamie		return (EAFNOSUPPORT);
3459191673Sjamie	}
3460185435Sbz
3461191673Sjamie	error = _prison_check_ip6(pr, ia6);
3462191673Sjamie	mtx_unlock(&pr->pr_mtx);
3463191673Sjamie	return (error);
3464185435Sbz}
3465185435Sbz#endif
3466185435Sbz
3467185435Sbz/*
3468188146Sjamie * Check if a jail supports the given address family.
3469188146Sjamie *
3470188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3471188146Sjamie * if not.
3472188146Sjamie */
3473188146Sjamieint
3474188146Sjamieprison_check_af(struct ucred *cred, int af)
3475188146Sjamie{
3476192895Sjamie	struct prison *pr;
3477188146Sjamie	int error;
3478188146Sjamie
3479188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3480188146Sjamie
3481192895Sjamie	pr = cred->cr_prison;
3482194923Sjamie#ifdef VIMAGE
3483194915Sjamie	/* Prisons with their own network stack are not limited. */
3484200473Sbz	if (prison_owns_vnet(cred))
3485194915Sjamie		return (0);
3486194923Sjamie#endif
3487194915Sjamie
3488188146Sjamie	error = 0;
3489188146Sjamie	switch (af)
3490188146Sjamie	{
3491188146Sjamie#ifdef INET
3492188146Sjamie	case AF_INET:
3493192895Sjamie		if (pr->pr_flags & PR_IP4)
3494192895Sjamie		{
3495192895Sjamie			mtx_lock(&pr->pr_mtx);
3496192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3497192895Sjamie				error = EAFNOSUPPORT;
3498192895Sjamie			mtx_unlock(&pr->pr_mtx);
3499192895Sjamie		}
3500188146Sjamie		break;
3501188146Sjamie#endif
3502188146Sjamie#ifdef INET6
3503188146Sjamie	case AF_INET6:
3504192895Sjamie		if (pr->pr_flags & PR_IP6)
3505192895Sjamie		{
3506192895Sjamie			mtx_lock(&pr->pr_mtx);
3507192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3508192895Sjamie				error = EAFNOSUPPORT;
3509192895Sjamie			mtx_unlock(&pr->pr_mtx);
3510192895Sjamie		}
3511188146Sjamie		break;
3512188146Sjamie#endif
3513188146Sjamie	case AF_LOCAL:
3514188146Sjamie	case AF_ROUTE:
3515188146Sjamie		break;
3516188146Sjamie	default:
3517192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3518188146Sjamie			error = EAFNOSUPPORT;
3519188146Sjamie	}
3520188146Sjamie	return (error);
3521188146Sjamie}
3522188146Sjamie
3523188146Sjamie/*
3524185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3525185435Sbz * prison_check_ip[46]).
3526185435Sbz *
3527192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3528192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3529192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3530185435Sbz */
3531185435Sbzint
353272786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
353346155Sphk{
3534185435Sbz#ifdef INET
3535114168Smike	struct sockaddr_in *sai;
3536185435Sbz#endif
3537185435Sbz#ifdef INET6
3538185435Sbz	struct sockaddr_in6 *sai6;
3539185435Sbz#endif
3540188144Sjamie	int error;
354146155Sphk
3542185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3543185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3544185435Sbz
3545200473Sbz#ifdef VIMAGE
3546200473Sbz	if (prison_owns_vnet(cred))
3547200473Sbz		return (0);
3548200473Sbz#endif
3549200473Sbz
3550188144Sjamie	error = 0;
3551188144Sjamie	switch (sa->sa_family)
3552185435Sbz	{
3553185435Sbz#ifdef INET
3554185435Sbz	case AF_INET:
3555185435Sbz		sai = (struct sockaddr_in *)sa;
3556188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3557185435Sbz		break;
3558185435Sbz#endif
3559185435Sbz#ifdef INET6
3560185435Sbz	case AF_INET6:
3561185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3562188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3563185435Sbz		break;
3564185435Sbz#endif
3565185435Sbz	default:
3566192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3567188144Sjamie			error = EAFNOSUPPORT;
3568185435Sbz	}
3569188144Sjamie	return (error);
357046155Sphk}
357172786Srwatson
357272786Srwatson/*
357372786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
357472786Srwatson */
357572786Srwatsonint
3576114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
357772786Srwatson{
357872786Srwatson
3579192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3580192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3581192895Sjamie}
358272786Srwatson
3583192895Sjamie/*
3584192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3585192895Sjamie */
3586192895Sjamieint
3587192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3588192895Sjamie{
3589192895Sjamie
3590192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3591192895Sjamie		if (pr1 == pr2)
3592192895Sjamie			return (1);
359372786Srwatson	return (0);
359472786Srwatson}
359572786Srwatson
359672786Srwatson/*
359772786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
359872786Srwatson */
359972786Srwatsonint
3600114168Smikejailed(struct ucred *cred)
360172786Srwatson{
360272786Srwatson
3603192895Sjamie	return (cred->cr_prison != &prison0);
360472786Srwatson}
360591384Srobert
360691384Srobert/*
3607200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3608200473Sbz * have its own virtual network stack, otherwise 0.
3609200473Sbz */
3610200473Sbzint
3611200473Sbzjailed_without_vnet(struct ucred *cred)
3612200473Sbz{
3613200473Sbz
3614200473Sbz	if (!jailed(cred))
3615200473Sbz		return (0);
3616200473Sbz#ifdef VIMAGE
3617200473Sbz	if (prison_owns_vnet(cred))
3618200473Sbz		return (0);
3619200473Sbz#endif
3620200473Sbz
3621200473Sbz	return (1);
3622200473Sbz}
3623200473Sbz
3624200473Sbz/*
3625194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
362691384Srobert */
362791391Srobertvoid
3628114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
362991384Srobert{
3630193066Sjamie	struct prison *pr;
363191384Srobert
3632194090Sjamie	/*
3633194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3634194090Sjamie	 * system's hostname.
3635194090Sjamie	 */
3636193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3637193066Sjamie	mtx_lock(&pr->pr_mtx);
3638194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3639193066Sjamie	mtx_unlock(&pr->pr_mtx);
364091384Srobert}
3641113275Smike
3642194090Sjamievoid
3643194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3644194090Sjamie{
3645194090Sjamie
3646194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3647194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3648194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3649194090Sjamie}
3650194090Sjamie
3651194090Sjamievoid
3652194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3653194090Sjamie{
3654194090Sjamie
3655194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3656194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3657194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3658194090Sjamie}
3659194090Sjamie
3660194090Sjamievoid
3661194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3662194090Sjamie{
3663194090Sjamie
3664194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3665194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3666194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3667194090Sjamie}
3668194090Sjamie
3669196176Sbz#ifdef VIMAGE
3670125804Srwatson/*
3671196176Sbz * Determine whether the prison represented by cred owns
3672196176Sbz * its vnet rather than having it inherited.
3673196176Sbz *
3674196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3675196176Sbz */
3676196176Sbzint
3677196176Sbzprison_owns_vnet(struct ucred *cred)
3678196176Sbz{
3679196176Sbz
3680196176Sbz	/*
3681196176Sbz	 * vnets cannot be added/removed after jail creation,
3682196176Sbz	 * so no need to lock here.
3683196176Sbz	 */
3684196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3685196176Sbz}
3686196176Sbz#endif
3687196176Sbz
3688196176Sbz/*
3689147185Spjd * Determine whether the subject represented by cred can "see"
3690147185Spjd * status of a mount point.
3691147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3692147185Spjd * XXX: This function should be called cr_canseemount() and should be
3693147185Spjd *      placed in kern_prot.c.
3694125804Srwatson */
3695125804Srwatsonint
3696147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3697125804Srwatson{
3698147185Spjd	struct prison *pr;
3699147185Spjd	struct statfs *sp;
3700147185Spjd	size_t len;
3701125804Srwatson
3702192895Sjamie	pr = cred->cr_prison;
3703192895Sjamie	if (pr->pr_enforce_statfs == 0)
3704147185Spjd		return (0);
3705147185Spjd	if (pr->pr_root->v_mount == mp)
3706147185Spjd		return (0);
3707192895Sjamie	if (pr->pr_enforce_statfs == 2)
3708147185Spjd		return (ENOENT);
3709147185Spjd	/*
3710147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3711147185Spjd	 * all mount-points from inside a jail.
3712147185Spjd	 * This is ugly check, but this is the only situation when jail's
3713147185Spjd	 * directory ends with '/'.
3714147185Spjd	 */
3715147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3716147185Spjd		return (0);
3717147185Spjd	len = strlen(pr->pr_path);
3718147185Spjd	sp = &mp->mnt_stat;
3719147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3720147185Spjd		return (ENOENT);
3721147185Spjd	/*
3722147185Spjd	 * Be sure that we don't have situation where jail's root directory
3723147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3724147185Spjd	 */
3725147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3726147185Spjd		return (ENOENT);
3727147185Spjd	return (0);
3728147185Spjd}
3729147185Spjd
3730147185Spjdvoid
3731147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3732147185Spjd{
3733147185Spjd	char jpath[MAXPATHLEN];
3734147185Spjd	struct prison *pr;
3735147185Spjd	size_t len;
3736147185Spjd
3737192895Sjamie	pr = cred->cr_prison;
3738192895Sjamie	if (pr->pr_enforce_statfs == 0)
3739147185Spjd		return;
3740147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3741147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3742147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3743147185Spjd		    sizeof(sp->f_mntonname));
3744147185Spjd		return;
3745125804Srwatson	}
3746147185Spjd	if (pr->pr_root->v_mount == mp) {
3747147185Spjd		/*
3748147185Spjd		 * Clear current buffer data, so we are sure nothing from
3749147185Spjd		 * the valid path left there.
3750147185Spjd		 */
3751147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3752147185Spjd		*sp->f_mntonname = '/';
3753147185Spjd		return;
3754147185Spjd	}
3755147185Spjd	/*
3756147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3757147185Spjd	 * all mount-points from inside a jail.
3758147185Spjd	 */
3759147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3760147185Spjd		return;
3761147185Spjd	len = strlen(pr->pr_path);
3762147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3763147185Spjd	/*
3764147185Spjd	 * Clear current buffer data, so we are sure nothing from
3765147185Spjd	 * the valid path left there.
3766147185Spjd	 */
3767147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3768147185Spjd	if (*jpath == '\0') {
3769147185Spjd		/* Should never happen. */
3770147185Spjd		*sp->f_mntonname = '/';
3771147185Spjd	} else {
3772147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3773147185Spjd	}
3774125804Srwatson}
3775125804Srwatson
3776164032Srwatson/*
3777164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3778164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3779164032Srwatson */
3780164032Srwatsonint
3781164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3782164032Srwatson{
3783164032Srwatson
3784164032Srwatson	if (!jailed(cred))
3785164032Srwatson		return (0);
3786164032Srwatson
3787194915Sjamie#ifdef VIMAGE
3788194915Sjamie	/*
3789194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3790194915Sjamie	 * There might be a duplicate entry here in case the privilege
3791194915Sjamie	 * is only granted conditionally in the legacy jail case.
3792194915Sjamie	 */
3793164032Srwatson	switch (priv) {
3794194915Sjamie#ifdef notyet
3795194915Sjamie		/*
3796194915Sjamie		 * NFS-specific privileges.
3797194915Sjamie		 */
3798194915Sjamie	case PRIV_NFS_DAEMON:
3799194915Sjamie	case PRIV_NFS_LOCKD:
3800194915Sjamie#endif
3801194915Sjamie		/*
3802194915Sjamie		 * Network stack privileges.
3803194915Sjamie		 */
3804194915Sjamie	case PRIV_NET_BRIDGE:
3805194915Sjamie	case PRIV_NET_GRE:
3806194915Sjamie	case PRIV_NET_BPF:
3807194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3808194915Sjamie	case PRIV_NET_ROUTE:
3809194915Sjamie	case PRIV_NET_TAP:
3810194915Sjamie	case PRIV_NET_SETIFMTU:
3811194915Sjamie	case PRIV_NET_SETIFFLAGS:
3812194915Sjamie	case PRIV_NET_SETIFCAP:
3813203052Sdelphij	case PRIV_NET_SETIFDESCR:
3814194915Sjamie	case PRIV_NET_SETIFNAME	:
3815194915Sjamie	case PRIV_NET_SETIFMETRIC:
3816194915Sjamie	case PRIV_NET_SETIFPHYS:
3817194915Sjamie	case PRIV_NET_SETIFMAC:
3818194915Sjamie	case PRIV_NET_ADDMULTI:
3819194915Sjamie	case PRIV_NET_DELMULTI:
3820194915Sjamie	case PRIV_NET_HWIOCTL:
3821194915Sjamie	case PRIV_NET_SETLLADDR:
3822194915Sjamie	case PRIV_NET_ADDIFGROUP:
3823194915Sjamie	case PRIV_NET_DELIFGROUP:
3824194915Sjamie	case PRIV_NET_IFCREATE:
3825194915Sjamie	case PRIV_NET_IFDESTROY:
3826194915Sjamie	case PRIV_NET_ADDIFADDR:
3827194915Sjamie	case PRIV_NET_DELIFADDR:
3828194915Sjamie	case PRIV_NET_LAGG:
3829194915Sjamie	case PRIV_NET_GIF:
3830194915Sjamie	case PRIV_NET_SETIFVNET:
3831223735Sbz	case PRIV_NET_SETIFFIB:
3832164032Srwatson
3833164032Srwatson		/*
3834194915Sjamie		 * 802.11-related privileges.
3835194915Sjamie		 */
3836194915Sjamie	case PRIV_NET80211_GETKEY:
3837194915Sjamie#ifdef notyet
3838194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3839194915Sjamie#endif
3840194915Sjamie
3841194915Sjamie#ifdef notyet
3842194915Sjamie		/*
3843194915Sjamie		 * AppleTalk privileges.
3844194915Sjamie		 */
3845194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3846194915Sjamie
3847194915Sjamie		/*
3848194915Sjamie		 * ATM privileges.
3849194915Sjamie		 */
3850194915Sjamie	case PRIV_NETATM_CFG:
3851194915Sjamie	case PRIV_NETATM_ADD:
3852194915Sjamie	case PRIV_NETATM_DEL:
3853194915Sjamie	case PRIV_NETATM_SET:
3854194915Sjamie
3855194915Sjamie		/*
3856194915Sjamie		 * Bluetooth privileges.
3857194915Sjamie		 */
3858194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3859194915Sjamie#endif
3860194915Sjamie
3861194915Sjamie		/*
3862194915Sjamie		 * Netgraph and netgraph module privileges.
3863194915Sjamie		 */
3864194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3865194915Sjamie#ifdef notyet
3866194915Sjamie	case PRIV_NETGRAPH_TTY:
3867194915Sjamie#endif
3868194915Sjamie
3869194915Sjamie		/*
3870194915Sjamie		 * IPv4 and IPv6 privileges.
3871194915Sjamie		 */
3872194915Sjamie	case PRIV_NETINET_IPFW:
3873194915Sjamie	case PRIV_NETINET_DIVERT:
3874194915Sjamie	case PRIV_NETINET_PF:
3875194915Sjamie	case PRIV_NETINET_DUMMYNET:
3876194915Sjamie	case PRIV_NETINET_CARP:
3877194915Sjamie	case PRIV_NETINET_MROUTE:
3878194915Sjamie	case PRIV_NETINET_RAW:
3879194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3880194915Sjamie	case PRIV_NETINET_ND6:
3881194915Sjamie	case PRIV_NETINET_SCOPE6:
3882194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3883194915Sjamie	case PRIV_NETINET_IPSEC:
3884194915Sjamie	case PRIV_NETINET_BINDANY:
3885194915Sjamie
3886194915Sjamie#ifdef notyet
3887194915Sjamie		/*
3888194915Sjamie		 * IPX/SPX privileges.
3889194915Sjamie		 */
3890194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3891194915Sjamie	case PRIV_NETIPX_RAW:
3892194915Sjamie
3893194915Sjamie		/*
3894194915Sjamie		 * NCP privileges.
3895194915Sjamie		 */
3896194915Sjamie	case PRIV_NETNCP:
3897194915Sjamie
3898194915Sjamie		/*
3899194915Sjamie		 * SMB privileges.
3900194915Sjamie		 */
3901194915Sjamie	case PRIV_NETSMB:
3902194915Sjamie#endif
3903194915Sjamie
3904194915Sjamie	/*
3905194915Sjamie	 * No default: or deny here.
3906194915Sjamie	 * In case of no permit fall through to next switch().
3907194915Sjamie	 */
3908194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3909194915Sjamie			return (0);
3910194915Sjamie	}
3911194915Sjamie#endif /* VIMAGE */
3912194915Sjamie
3913194915Sjamie	switch (priv) {
3914194915Sjamie
3915194915Sjamie		/*
3916164032Srwatson		 * Allow ktrace privileges for root in jail.
3917164032Srwatson		 */
3918164032Srwatson	case PRIV_KTRACE:
3919164032Srwatson
3920166827Srwatson#if 0
3921164032Srwatson		/*
3922164032Srwatson		 * Allow jailed processes to configure audit identity and
3923164032Srwatson		 * submit audit records (login, etc).  In the future we may
3924164032Srwatson		 * want to further refine the relationship between audit and
3925164032Srwatson		 * jail.
3926164032Srwatson		 */
3927164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3928164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3929164032Srwatson	case PRIV_AUDIT_SUBMIT:
3930166827Srwatson#endif
3931164032Srwatson
3932164032Srwatson		/*
3933164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3934164032Srwatson		 * credentials in any way they see fit.
3935164032Srwatson		 */
3936164032Srwatson	case PRIV_CRED_SETUID:
3937164032Srwatson	case PRIV_CRED_SETEUID:
3938164032Srwatson	case PRIV_CRED_SETGID:
3939164032Srwatson	case PRIV_CRED_SETEGID:
3940164032Srwatson	case PRIV_CRED_SETGROUPS:
3941164032Srwatson	case PRIV_CRED_SETREUID:
3942164032Srwatson	case PRIV_CRED_SETREGID:
3943164032Srwatson	case PRIV_CRED_SETRESUID:
3944164032Srwatson	case PRIV_CRED_SETRESGID:
3945164032Srwatson
3946164032Srwatson		/*
3947164032Srwatson		 * Jail implements visibility constraints already, so allow
3948164032Srwatson		 * jailed root to override uid/gid-based constraints.
3949164032Srwatson		 */
3950164032Srwatson	case PRIV_SEEOTHERGIDS:
3951164032Srwatson	case PRIV_SEEOTHERUIDS:
3952164032Srwatson
3953164032Srwatson		/*
3954164032Srwatson		 * Jail implements inter-process debugging limits already, so
3955164032Srwatson		 * allow jailed root various debugging privileges.
3956164032Srwatson		 */
3957164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3958164032Srwatson	case PRIV_DEBUG_SUGID:
3959164032Srwatson	case PRIV_DEBUG_UNPRIV:
3960164032Srwatson
3961164032Srwatson		/*
3962164032Srwatson		 * Allow jail to set various resource limits and login
3963164032Srwatson		 * properties, and for now, exceed process resource limits.
3964164032Srwatson		 */
3965164032Srwatson	case PRIV_PROC_LIMIT:
3966164032Srwatson	case PRIV_PROC_SETLOGIN:
3967164032Srwatson	case PRIV_PROC_SETRLIMIT:
3968164032Srwatson
3969164032Srwatson		/*
3970164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3971164032Srwatson		 */
3972164032Srwatson	case PRIV_IPC_READ:
3973164032Srwatson	case PRIV_IPC_WRITE:
3974164032Srwatson	case PRIV_IPC_ADMIN:
3975164032Srwatson	case PRIV_IPC_MSGSIZE:
3976164032Srwatson	case PRIV_MQ_ADMIN:
3977164032Srwatson
3978164032Srwatson		/*
3979192895Sjamie		 * Jail operations within a jail work on child jails.
3980192895Sjamie		 */
3981192895Sjamie	case PRIV_JAIL_ATTACH:
3982192895Sjamie	case PRIV_JAIL_SET:
3983192895Sjamie	case PRIV_JAIL_REMOVE:
3984192895Sjamie
3985192895Sjamie		/*
3986164032Srwatson		 * Jail implements its own inter-process limits, so allow
3987164032Srwatson		 * root processes in jail to change scheduling on other
3988164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3989164032Srwatson		 */
3990164032Srwatson	case PRIV_SCHED_DIFFCRED:
3991185435Sbz	case PRIV_SCHED_CPUSET:
3992164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3993164032Srwatson	case PRIV_SIGNAL_SUGID:
3994164032Srwatson
3995164032Srwatson		/*
3996164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3997164032Srwatson		 * writable.
3998164032Srwatson		 */
3999164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
4000164032Srwatson
4001164032Srwatson		/*
4002164032Srwatson		 * Allow root in jail to manage a variety of quota
4003166831Srwatson		 * properties.  These should likely be conditional on a
4004166831Srwatson		 * configuration option.
4005164032Srwatson		 */
4006166832Srwatson	case PRIV_VFS_GETQUOTA:
4007166832Srwatson	case PRIV_VFS_SETQUOTA:
4008164032Srwatson
4009164032Srwatson		/*
4010164032Srwatson		 * Since Jail relies on chroot() to implement file system
4011164032Srwatson		 * protections, grant many VFS privileges to root in jail.
4012164032Srwatson		 * Be careful to exclude mount-related and NFS-related
4013164032Srwatson		 * privileges.
4014164032Srwatson		 */
4015164032Srwatson	case PRIV_VFS_READ:
4016164032Srwatson	case PRIV_VFS_WRITE:
4017164032Srwatson	case PRIV_VFS_ADMIN:
4018164032Srwatson	case PRIV_VFS_EXEC:
4019164032Srwatson	case PRIV_VFS_LOOKUP:
4020164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
4021164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
4022164032Srwatson	case PRIV_VFS_CHOWN:
4023164032Srwatson	case PRIV_VFS_CHROOT:
4024167152Spjd	case PRIV_VFS_RETAINSUGID:
4025164032Srwatson	case PRIV_VFS_FCHROOT:
4026164032Srwatson	case PRIV_VFS_LINK:
4027164032Srwatson	case PRIV_VFS_SETGID:
4028172860Srwatson	case PRIV_VFS_STAT:
4029164032Srwatson	case PRIV_VFS_STICKYFILE:
4030255316Sjamie
4031255316Sjamie		/*
4032255316Sjamie		 * As in the non-jail case, non-root users are expected to be
4033255316Sjamie		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
4034255316Sjamie		 * exists in the jail and they have permission to access it).
4035255316Sjamie		 */
4036255316Sjamie	case PRIV_KMEM_READ:
4037164032Srwatson		return (0);
4038164032Srwatson
4039164032Srwatson		/*
4040164032Srwatson		 * Depending on the global setting, allow privilege of
4041164032Srwatson		 * setting system flags.
4042164032Srwatson		 */
4043164032Srwatson	case PRIV_VFS_SYSFLAGS:
4044192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
4045164032Srwatson			return (0);
4046164032Srwatson		else
4047164032Srwatson			return (EPERM);
4048164032Srwatson
4049164032Srwatson		/*
4050168396Spjd		 * Depending on the global setting, allow privilege of
4051168396Spjd		 * mounting/unmounting file systems.
4052168396Spjd		 */
4053168396Spjd	case PRIV_VFS_MOUNT:
4054168396Spjd	case PRIV_VFS_UNMOUNT:
4055168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
4056168699Spjd	case PRIV_VFS_MOUNT_OWNER:
4057224615Smm		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
4058224615Smm		    cred->cr_prison->pr_enforce_statfs < 2)
4059168396Spjd			return (0);
4060168396Spjd		else
4061168396Spjd			return (EPERM);
4062168396Spjd
4063168396Spjd		/*
4064168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
4065168591Srwatson		 * ports.
4066164032Srwatson		 */
4067164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
4068168591Srwatson	case PRIV_NETINET_REUSEPORT:
4069164032Srwatson		return (0);
4070164032Srwatson
4071164032Srwatson		/*
4072175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
4073175630Sbz		 */
4074175630Sbz	case PRIV_NETINET_SETHDROPTS:
4075175630Sbz		return (0);
4076175630Sbz
4077175630Sbz		/*
4078164032Srwatson		 * Conditionally allow creating raw sockets in jail.
4079164032Srwatson		 */
4080164032Srwatson	case PRIV_NETINET_RAW:
4081192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
4082164032Srwatson			return (0);
4083164032Srwatson		else
4084164032Srwatson			return (EPERM);
4085164032Srwatson
4086164032Srwatson		/*
4087164032Srwatson		 * Since jail implements its own visibility limits on netstat
4088164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
4089164032Srwatson		 * jail.
4090164032Srwatson		 */
4091164032Srwatson	case PRIV_NETINET_GETCRED:
4092164032Srwatson		return (0);
4093164032Srwatson
4094219304Strasz		/*
4095219304Strasz		 * Allow jailed root to set loginclass.
4096219304Strasz		 */
4097219304Strasz	case PRIV_PROC_SETLOGINCLASS:
4098219304Strasz		return (0);
4099219304Strasz
4100164032Srwatson	default:
4101164032Srwatson		/*
4102164032Srwatson		 * In all remaining cases, deny the privilege request.  This
4103164032Srwatson		 * includes almost all network privileges, many system
4104164032Srwatson		 * configuration privileges.
4105164032Srwatson		 */
4106164032Srwatson		return (EPERM);
4107164032Srwatson	}
4108164032Srwatson}
4109164032Srwatson
4110192895Sjamie/*
4111192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
4112192895Sjamie * if it does not directly follow.
4113192895Sjamie */
4114192895Sjamie
4115192895Sjamiechar *
4116192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
4117192895Sjamie{
4118192895Sjamie	char *name;
4119192895Sjamie
4120192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
4121192895Sjamie	if (pr1 == pr2)
4122192895Sjamie		return "0";
4123192895Sjamie	name = pr2->pr_name;
4124192895Sjamie	if (prison_ischild(pr1, pr2)) {
4125192895Sjamie		/*
4126192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
4127192895Sjamie		 * so its length can't be counted on.  But the number of dots
4128192895Sjamie		 * can be counted on - and counted.
4129192895Sjamie		 */
4130192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
4131192895Sjamie			name = strchr(name, '.') + 1;
4132192895Sjamie	}
4133192895Sjamie	return (name);
4134192895Sjamie}
4135192895Sjamie
4136192895Sjamie/*
4137192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
4138192895Sjamie * if it does not directly follow.
4139192895Sjamie */
4140192895Sjamiestatic char *
4141192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
4142192895Sjamie{
4143192895Sjamie	char *path1, *path2;
4144192895Sjamie	int len1;
4145192895Sjamie
4146192895Sjamie	path1 = pr1->pr_path;
4147192895Sjamie	path2 = pr2->pr_path;
4148192895Sjamie	if (!strcmp(path1, "/"))
4149192895Sjamie		return (path2);
4150192895Sjamie	len1 = strlen(path1);
4151192895Sjamie	if (strncmp(path1, path2, len1))
4152192895Sjamie		return (path2);
4153192895Sjamie	if (path2[len1] == '\0')
4154192895Sjamie		return "/";
4155192895Sjamie	if (path2[len1] == '/')
4156192895Sjamie		return (path2 + len1);
4157192895Sjamie	return (path2);
4158192895Sjamie}
4159192895Sjamie
4160192895Sjamie
4161192895Sjamie/*
4162192895Sjamie * Jail-related sysctls.
4163192895Sjamie */
4164227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4165192895Sjamie    "Jails");
4166192895Sjamie
4167113275Smikestatic int
4168113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
4169113275Smike{
4170191673Sjamie	struct xprison *xp;
4171192895Sjamie	struct prison *pr, *cpr;
4172191673Sjamie#ifdef INET
4173191673Sjamie	struct in_addr *ip4 = NULL;
4174191673Sjamie	int ip4s = 0;
4175191673Sjamie#endif
4176191673Sjamie#ifdef INET6
4177208803Scperciva	struct in6_addr *ip6 = NULL;
4178191673Sjamie	int ip6s = 0;
4179191673Sjamie#endif
4180192895Sjamie	int descend, error;
4181113275Smike
4182191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4183192895Sjamie	pr = req->td->td_ucred->cr_prison;
4184191673Sjamie	error = 0;
4185168401Spjd	sx_slock(&allprison_lock);
4186192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4187192895Sjamie#if defined(INET) || defined(INET6)
4188191673Sjamie again:
4189192895Sjamie#endif
4190192895Sjamie		mtx_lock(&cpr->pr_mtx);
4191185435Sbz#ifdef INET
4192192895Sjamie		if (cpr->pr_ip4s > 0) {
4193192895Sjamie			if (ip4s < cpr->pr_ip4s) {
4194192895Sjamie				ip4s = cpr->pr_ip4s;
4195192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4196191673Sjamie				ip4 = realloc(ip4, ip4s *
4197191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4198191673Sjamie				goto again;
4199191673Sjamie			}
4200192895Sjamie			bcopy(cpr->pr_ip4, ip4,
4201192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
4202191673Sjamie		}
4203185435Sbz#endif
4204185435Sbz#ifdef INET6
4205192895Sjamie		if (cpr->pr_ip6s > 0) {
4206192895Sjamie			if (ip6s < cpr->pr_ip6s) {
4207192895Sjamie				ip6s = cpr->pr_ip6s;
4208192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4209191673Sjamie				ip6 = realloc(ip6, ip6s *
4210191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4211191673Sjamie				goto again;
4212191673Sjamie			}
4213192895Sjamie			bcopy(cpr->pr_ip6, ip6,
4214192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
4215191673Sjamie		}
4216185435Sbz#endif
4217192895Sjamie		if (cpr->pr_ref == 0) {
4218192895Sjamie			mtx_unlock(&cpr->pr_mtx);
4219191673Sjamie			continue;
4220191673Sjamie		}
4221191673Sjamie		bzero(xp, sizeof(*xp));
4222113275Smike		xp->pr_version = XPRISON_VERSION;
4223192895Sjamie		xp->pr_id = cpr->pr_id;
4224192895Sjamie		xp->pr_state = cpr->pr_uref > 0
4225191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4226192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4227194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4228192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4229185435Sbz#ifdef INET
4230192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
4231185435Sbz#endif
4232185435Sbz#ifdef INET6
4233192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4234185435Sbz#endif
4235192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4236191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4237191673Sjamie		if (error)
4238191673Sjamie			break;
4239185435Sbz#ifdef INET
4240191673Sjamie		if (xp->pr_ip4s > 0) {
4241191673Sjamie			error = SYSCTL_OUT(req, ip4,
4242191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4243191673Sjamie			if (error)
4244191673Sjamie				break;
4245185435Sbz		}
4246185435Sbz#endif
4247185435Sbz#ifdef INET6
4248191673Sjamie		if (xp->pr_ip6s > 0) {
4249191673Sjamie			error = SYSCTL_OUT(req, ip6,
4250191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4251191673Sjamie			if (error)
4252191673Sjamie				break;
4253185435Sbz		}
4254185435Sbz#endif
4255113275Smike	}
4256168401Spjd	sx_sunlock(&allprison_lock);
4257191673Sjamie	free(xp, M_TEMP);
4258191673Sjamie#ifdef INET
4259191673Sjamie	free(ip4, M_TEMP);
4260191673Sjamie#endif
4261191673Sjamie#ifdef INET6
4262191673Sjamie	free(ip6, M_TEMP);
4263191673Sjamie#endif
4264167354Spjd	return (error);
4265113275Smike}
4266113275Smike
4267187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4268187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4269187864Sed    sysctl_jail_list, "S", "List of active jails");
4270126004Spjd
4271126004Spjdstatic int
4272126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4273126004Spjd{
4274126004Spjd	int error, injail;
4275126004Spjd
4276126004Spjd	injail = jailed(req->td->td_ucred);
4277126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4278126004Spjd
4279126004Spjd	return (error);
4280126004Spjd}
4281192895Sjamie
4282187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4283187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4284187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4285185435Sbz
4286250804Sjamiestatic int
4287250804Sjamiesysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4288250804Sjamie{
4289250804Sjamie	int error, havevnet;
4290250804Sjamie#ifdef VIMAGE
4291250804Sjamie	struct ucred *cred = req->td->td_ucred;
4292250804Sjamie
4293250804Sjamie	havevnet = jailed(cred) && prison_owns_vnet(cred);
4294250804Sjamie#else
4295250804Sjamie	havevnet = 0;
4296250804Sjamie#endif
4297250804Sjamie	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4298250804Sjamie
4299250804Sjamie	return (error);
4300250804Sjamie}
4301250804Sjamie
4302250804SjamieSYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4303250804Sjamie    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4304250804Sjamie    sysctl_jail_vnet, "I", "Jail owns VNET?");
4305250804Sjamie
4306192895Sjamie#if defined(INET) || defined(INET6)
4307193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4308192895Sjamie    &jail_max_af_ips, 0,
4309301905Sjamie    "Number of IP addresses a jail may have at most per address family (deprecated)");
4310192895Sjamie#endif
4311192895Sjamie
4312192895Sjamie/*
4313192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
4314192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4315192895Sjamie * just see their own parameters, and can't change them.
4316192895Sjamie */
4317192895Sjamiestatic int
4318192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4319192895Sjamie{
4320192895Sjamie	struct prison *pr;
4321192895Sjamie	int allow, error, i;
4322192895Sjamie
4323192895Sjamie	pr = req->td->td_ucred->cr_prison;
4324192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4325192895Sjamie
4326192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4327192895Sjamie	i = (allow & arg2) ? 1 : 0;
4328192895Sjamie	if (arg1 != NULL)
4329192895Sjamie		i = !i;
4330192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4331192895Sjamie	if (error || !req->newptr)
4332192895Sjamie		return (error);
4333192895Sjamie	i = i ? arg2 : 0;
4334192895Sjamie	if (arg1 != NULL)
4335192895Sjamie		i ^= arg2;
4336192895Sjamie	/*
4337192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4338192895Sjamie	 * for writing.
4339192895Sjamie	 */
4340192895Sjamie	mtx_lock(&prison0.pr_mtx);
4341192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4342192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4343192895Sjamie	return (0);
4344192895Sjamie}
4345192895Sjamie
4346192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4347192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4348192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4349301905Sjamie    "Processes in jail can set their hostnames (deprecated)");
4350192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4351192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4352192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4353301905Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4354192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4355192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4356192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4357301905Sjamie    "Processes in jail can use System V IPC primitives (deprecated)");
4358192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4359192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4360192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4361301905Sjamie    "Prison root can create raw sockets (deprecated)");
4362192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4363192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4364192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4365301905Sjamie    "Processes in jail can alter system file flags (deprecated)");
4366192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4367192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4368192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4369301905Sjamie    "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4370232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4371232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4372232059Smm    NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4373301905Sjamie    "Processes in jail can mount the devfs file system (deprecated)");
4374277985SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed,
4375277985Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4376277985Sjamie    NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I",
4377301905Sjamie    "Processes in jail can mount the fdescfs file system (deprecated)");
4378232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4379232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4380232059Smm    NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4381301905Sjamie    "Processes in jail can mount the nullfs file system (deprecated)");
4382232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
4383232278Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4384232278Smm    NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
4385301905Sjamie    "Processes in jail can mount the procfs file system (deprecated)");
4386295951SaraujoSYSCTL_PROC(_security_jail, OID_AUTO, mount_linprocfs_allowed,
4387295951Saraujo    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4388295951Saraujo    NULL, PR_ALLOW_MOUNT_LINPROCFS, sysctl_jail_default_allow, "I",
4389301905Sjamie    "Processes in jail can mount the linprocfs file system (deprecated)");
4390295951SaraujoSYSCTL_PROC(_security_jail, OID_AUTO, mount_linsysfs_allowed,
4391295951Saraujo    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4392295951Saraujo    NULL, PR_ALLOW_MOUNT_LINSYSFS, sysctl_jail_default_allow, "I",
4393301905Sjamie    "Processes in jail can mount the linsysfs file system (deprecated)");
4394254741SdelphijSYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed,
4395254741Sdelphij    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4396254741Sdelphij    NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I",
4397301905Sjamie    "Processes in jail can mount the tmpfs file system (deprecated)");
4398232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4399232186Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4400232186Smm    NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4401301905Sjamie    "Processes in jail can mount the zfs file system (deprecated)");
4402192895Sjamie
4403192895Sjamiestatic int
4404192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4405192895Sjamie{
4406192895Sjamie	struct prison *pr;
4407192895Sjamie	int level, error;
4408192895Sjamie
4409192895Sjamie	pr = req->td->td_ucred->cr_prison;
4410192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4411192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4412192895Sjamie	if (error || !req->newptr)
4413192895Sjamie		return (error);
4414192895Sjamie	*(int *)arg1 = level;
4415192895Sjamie	return (0);
4416192895Sjamie}
4417192895Sjamie
4418192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4419192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4420192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4421192895Sjamie    sysctl_jail_default_level, "I",
4422301905Sjamie    "Processes in jail cannot see all mounted file systems (deprecated)");
4423192895Sjamie
4424231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4425231267Smm    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4426231267Smm    &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4427231267Smm    sysctl_jail_default_level, "I",
4428301905Sjamie    "Ruleset for the devfs filesystem in jail (deprecated)");
4429231267Smm
4430192895Sjamie/*
4431192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4432192895Sjamie * is returned in the string itself, and the other parameters exist merely
4433192895Sjamie * to make themselves and their types known.
4434192895Sjamie */
4435192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4436192895Sjamie    "Jail parameters");
4437192895Sjamie
4438192895Sjamieint
4439192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4440192895Sjamie{
4441192895Sjamie	int i;
4442192895Sjamie	long l;
4443192895Sjamie	size_t s;
4444192895Sjamie	char numbuf[12];
4445192895Sjamie
4446192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4447192895Sjamie	{
4448192895Sjamie	case CTLTYPE_LONG:
4449192895Sjamie	case CTLTYPE_ULONG:
4450192895Sjamie		l = 0;
4451192895Sjamie#ifdef SCTL_MASK32
4452192895Sjamie		if (!(req->flags & SCTL_MASK32))
4453192895Sjamie#endif
4454192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4455192895Sjamie	case CTLTYPE_INT:
4456192895Sjamie	case CTLTYPE_UINT:
4457192895Sjamie		i = 0;
4458192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4459192895Sjamie	case CTLTYPE_STRING:
4460219819Sjeff		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4461192895Sjamie		return
4462192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4463192895Sjamie	case CTLTYPE_STRUCT:
4464192895Sjamie		s = (size_t)arg2;
4465192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4466192895Sjamie	}
4467192895Sjamie	return (0);
4468192895Sjamie}
4469192895Sjamie
4470280632Sian/*
4471280632Sian * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4472280632Sian * jail creation time but cannot be changed in an existing jail.
4473280632Sian */
4474192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4475192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4476192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4477192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4478192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4479192895Sjamie    "I", "Jail secure level");
4480280632SianSYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4481280632Sian    "Jail value for kern.osreldate and uname -K");
4482280632SianSYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4483280632Sian    "Jail value for kern.osrelease and uname -r");
4484192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4485192895Sjamie    "I", "Jail cannot see all mounted file systems");
4486231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4487231267Smm    "I", "Ruleset for in-jail devfs mounts");
4488192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4489192895Sjamie    "B", "Jail persistence");
4490194251Sjamie#ifdef VIMAGE
4491194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4492195870Sjamie    "E,jailsys", "Virtual network stack");
4493194251Sjamie#endif
4494192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4495192895Sjamie    "B", "Jail is in the process of shutting down");
4496192895Sjamie
4497194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4498194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4499194762Sjamie    "I", "Current number of child jails");
4500194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4501194762Sjamie    "I", "Maximum number of child jails");
4502194762Sjamie
4503195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4504192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4505192895Sjamie    "Jail hostname");
4506193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4507193066Sjamie    "Jail NIS domainname");
4508193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4509193066Sjamie    "Jail host UUID");
4510193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4511193066Sjamie    "LU", "Jail host ID");
4512192895Sjamie
4513192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4514192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4515192895Sjamie
4516192895Sjamie#ifdef INET
4517195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4518195974Sjamie    "Jail IPv4 address virtualization");
4519192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4520192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4521202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4522202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4523202468Sbz    "primary jail IPv4 address.");
4524192895Sjamie#endif
4525192895Sjamie#ifdef INET6
4526195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4527195974Sjamie    "Jail IPv6 address virtualization");
4528192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4529192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4530202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4531202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4532202468Sbz    "primary jail IPv6 address.");
4533192895Sjamie#endif
4534192895Sjamie
4535192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4536192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4537192895Sjamie    "B", "Jail may set hostname");
4538192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4539192895Sjamie    "B", "Jail may use SYSV IPC");
4540192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4541192895Sjamie    "B", "Jail may create raw sockets");
4542192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4543192895Sjamie    "B", "Jail may alter system file flags");
4544192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4545192895Sjamie    "B", "Jail may set file quotas");
4546192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4547192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4548192895Sjamie
4549232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4550232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4551232059Smm    "B", "Jail may mount/unmount jail-friendly file systems in general");
4552232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4553232186Smm    "B", "Jail may mount the devfs file system");
4554277985SjamieSYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW,
4555277985Sjamie    "B", "Jail may mount the fdescfs file system");
4556232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4557232186Smm    "B", "Jail may mount the nullfs file system");
4558232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
4559232278Smm    "B", "Jail may mount the procfs file system");
4560295951SaraujoSYSCTL_JAIL_PARAM(_allow_mount, linprocfs, CTLTYPE_INT | CTLFLAG_RW,
4561295951Saraujo    "B", "Jail may mount the linprocfs file system");
4562295951SaraujoSYSCTL_JAIL_PARAM(_allow_mount, linsysfs, CTLTYPE_INT | CTLFLAG_RW,
4563295951Saraujo    "B", "Jail may mount the linsysfs file system");
4564254741SdelphijSYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW,
4565254741Sdelphij    "B", "Jail may mount the tmpfs file system");
4566232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4567232186Smm    "B", "Jail may mount the zfs file system");
4568232059Smm
4569284665Strasz#ifdef RACCT
4570220137Straszvoid
4571220137Straszprison_racct_foreach(void (*callback)(struct racct *racct,
4572220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
4573220137Strasz{
4574221362Strasz	struct prison_racct *prr;
4575192895Sjamie
4576284665Strasz	ASSERT_RACCT_ENABLED();
4577284665Strasz
4578220137Strasz	sx_slock(&allprison_lock);
4579221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next)
4580221362Strasz		(callback)(prr->prr_racct, arg2, arg3);
4581220137Strasz	sx_sunlock(&allprison_lock);
4582220137Strasz}
4583220137Strasz
4584221362Straszstatic struct prison_racct *
4585221362Straszprison_racct_find_locked(const char *name)
4586221362Strasz{
4587221362Strasz	struct prison_racct *prr;
4588221362Strasz
4589284665Strasz	ASSERT_RACCT_ENABLED();
4590221362Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4591221362Strasz
4592221362Strasz	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4593221362Strasz		return (NULL);
4594221362Strasz
4595221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4596221362Strasz		if (strcmp(name, prr->prr_name) != 0)
4597221362Strasz			continue;
4598221362Strasz
4599221362Strasz		/* Found prison_racct with a matching name? */
4600221362Strasz		prison_racct_hold(prr);
4601221362Strasz		return (prr);
4602221362Strasz	}
4603221362Strasz
4604221362Strasz	/* Add new prison_racct. */
4605221362Strasz	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4606221362Strasz	racct_create(&prr->prr_racct);
4607221362Strasz
4608221362Strasz	strcpy(prr->prr_name, name);
4609221362Strasz	refcount_init(&prr->prr_refcount, 1);
4610221362Strasz	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4611221362Strasz
4612221362Strasz	return (prr);
4613221362Strasz}
4614221362Strasz
4615221362Straszstruct prison_racct *
4616221362Straszprison_racct_find(const char *name)
4617221362Strasz{
4618221362Strasz	struct prison_racct *prr;
4619221362Strasz
4620284665Strasz	ASSERT_RACCT_ENABLED();
4621284665Strasz
4622221362Strasz	sx_xlock(&allprison_lock);
4623221362Strasz	prr = prison_racct_find_locked(name);
4624221362Strasz	sx_xunlock(&allprison_lock);
4625221362Strasz	return (prr);
4626221362Strasz}
4627221362Strasz
4628221362Straszvoid
4629221362Straszprison_racct_hold(struct prison_racct *prr)
4630221362Strasz{
4631221362Strasz
4632284665Strasz	ASSERT_RACCT_ENABLED();
4633284665Strasz
4634221362Strasz	refcount_acquire(&prr->prr_refcount);
4635221362Strasz}
4636221362Strasz
4637232598Straszstatic void
4638232598Straszprison_racct_free_locked(struct prison_racct *prr)
4639232598Strasz{
4640232598Strasz
4641284665Strasz	ASSERT_RACCT_ENABLED();
4642232598Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4643232598Strasz
4644232598Strasz	if (refcount_release(&prr->prr_refcount)) {
4645232598Strasz		racct_destroy(&prr->prr_racct);
4646232598Strasz		LIST_REMOVE(prr, prr_next);
4647232598Strasz		free(prr, M_PRISON_RACCT);
4648232598Strasz	}
4649232598Strasz}
4650232598Strasz
4651221362Straszvoid
4652221362Straszprison_racct_free(struct prison_racct *prr)
4653221362Strasz{
4654221362Strasz	int old;
4655221362Strasz
4656284665Strasz	ASSERT_RACCT_ENABLED();
4657232598Strasz	sx_assert(&allprison_lock, SA_UNLOCKED);
4658232598Strasz
4659221362Strasz	old = prr->prr_refcount;
4660221362Strasz	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4661221362Strasz		return;
4662221362Strasz
4663221362Strasz	sx_xlock(&allprison_lock);
4664232598Strasz	prison_racct_free_locked(prr);
4665221362Strasz	sx_xunlock(&allprison_lock);
4666221362Strasz}
4667221362Strasz
4668221362Straszstatic void
4669221362Straszprison_racct_attach(struct prison *pr)
4670221362Strasz{
4671221362Strasz	struct prison_racct *prr;
4672221362Strasz
4673284665Strasz	ASSERT_RACCT_ENABLED();
4674232598Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4675232598Strasz
4676221362Strasz	prr = prison_racct_find_locked(pr->pr_name);
4677221362Strasz	KASSERT(prr != NULL, ("cannot find prison_racct"));
4678221362Strasz
4679221362Strasz	pr->pr_prison_racct = prr;
4680221362Strasz}
4681221362Strasz
4682232598Strasz/*
4683232598Strasz * Handle jail renaming.  From the racct point of view, renaming means
4684232598Strasz * moving from one prison_racct to another.
4685232598Strasz */
4686221362Straszstatic void
4687232598Straszprison_racct_modify(struct prison *pr)
4688232598Strasz{
4689232598Strasz	struct proc *p;
4690232598Strasz	struct ucred *cred;
4691232598Strasz	struct prison_racct *oldprr;
4692232598Strasz
4693284665Strasz	ASSERT_RACCT_ENABLED();
4694284665Strasz
4695232598Strasz	sx_slock(&allproc_lock);
4696232598Strasz	sx_xlock(&allprison_lock);
4697232598Strasz
4698235795Strasz	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4699235795Strasz		sx_xunlock(&allprison_lock);
4700235795Strasz		sx_sunlock(&allproc_lock);
4701232598Strasz		return;
4702235795Strasz	}
4703232598Strasz
4704232598Strasz	oldprr = pr->pr_prison_racct;
4705232598Strasz	pr->pr_prison_racct = NULL;
4706232598Strasz
4707232598Strasz	prison_racct_attach(pr);
4708232598Strasz
4709232598Strasz	/*
4710232598Strasz	 * Move resource utilisation records.
4711232598Strasz	 */
4712232598Strasz	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4713232598Strasz
4714232598Strasz	/*
4715232598Strasz	 * Force rctl to reattach rules to processes.
4716232598Strasz	 */
4717232598Strasz	FOREACH_PROC_IN_SYSTEM(p) {
4718232598Strasz		PROC_LOCK(p);
4719232598Strasz		cred = crhold(p->p_ucred);
4720232598Strasz		PROC_UNLOCK(p);
4721232598Strasz		racct_proc_ucred_changed(p, cred, cred);
4722232598Strasz		crfree(cred);
4723232598Strasz	}
4724232598Strasz
4725232598Strasz	sx_sunlock(&allproc_lock);
4726232598Strasz	prison_racct_free_locked(oldprr);
4727232598Strasz	sx_xunlock(&allprison_lock);
4728232598Strasz}
4729232598Strasz
4730232598Straszstatic void
4731221362Straszprison_racct_detach(struct prison *pr)
4732221362Strasz{
4733232598Strasz
4734284665Strasz	ASSERT_RACCT_ENABLED();
4735232598Strasz	sx_assert(&allprison_lock, SA_UNLOCKED);
4736232598Strasz
4737244404Smjg	if (pr->pr_prison_racct == NULL)
4738244404Smjg		return;
4739221362Strasz	prison_racct_free(pr->pr_prison_racct);
4740221362Strasz	pr->pr_prison_racct = NULL;
4741221362Strasz}
4742221362Strasz#endif /* RACCT */
4743221362Strasz
4744185435Sbz#ifdef DDB
4745191673Sjamie
4746191673Sjamiestatic void
4747191673Sjamiedb_show_prison(struct prison *pr)
4748185435Sbz{
4749192895Sjamie	int fi;
4750191673Sjamie#if defined(INET) || defined(INET6)
4751191673Sjamie	int ii;
4752185435Sbz#endif
4753195870Sjamie	unsigned jsf;
4754185435Sbz#ifdef INET6
4755185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4756185435Sbz#endif
4757185435Sbz
4758191673Sjamie	db_printf("prison %p:\n", pr);
4759191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4760191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4761192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4762191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4763191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4764191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4765191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4766191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4767194251Sjamie#ifdef VIMAGE
4768194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4769194251Sjamie#endif
4770191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4771191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4772231267Smm	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4773202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4774202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4775192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4776192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4777202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4778192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4779192895Sjamie	    fi++)
4780192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4781192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4782195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4783195870Sjamie	    fi++) {
4784195870Sjamie		jsf = pr->pr_flags &
4785195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4786195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4787195870Sjamie		    pr_flag_jailsys[fi].disable &&
4788195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4789195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4790195870Sjamie		    : "inherit");
4791195870Sjamie	}
4792202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4793192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4794192895Sjamie	    fi++)
4795192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4796192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4797191673Sjamie	db_printf("\n");
4798192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4799194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4800194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4801194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4802193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4803185435Sbz#ifdef INET
4804191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4805191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4806191673Sjamie		db_printf(" %s %s\n",
4807202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4808191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4809185435Sbz#endif
4810185435Sbz#ifdef INET6
4811191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4812191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4813191673Sjamie		db_printf(" %s %s\n",
4814202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4815191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4816191673Sjamie#endif
4817191673Sjamie}
4818191673Sjamie
4819191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4820191673Sjamie{
4821191673Sjamie	struct prison *pr;
4822191673Sjamie
4823191673Sjamie	if (!have_addr) {
4824192895Sjamie		/*
4825192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4826192895Sjamie		 * listed.
4827192895Sjamie		 */
4828192895Sjamie		db_show_prison(&prison0);
4829192895Sjamie		if (!db_pager_quit) {
4830192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4831192895Sjamie				db_show_prison(pr);
4832192895Sjamie				if (db_pager_quit)
4833192895Sjamie					break;
4834192895Sjamie			}
4835191673Sjamie		}
4836191673Sjamie		return;
4837191673Sjamie	}
4838191673Sjamie
4839192895Sjamie	if (addr == 0)
4840192895Sjamie		pr = &prison0;
4841192895Sjamie	else {
4842192895Sjamie		/* Look for a prison with the ID and with references. */
4843191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4844192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4845191673Sjamie				break;
4846192895Sjamie		if (pr == NULL)
4847192895Sjamie			/* Look again, without requiring a reference. */
4848192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4849192895Sjamie				if (pr->pr_id == addr)
4850192895Sjamie					break;
4851192895Sjamie		if (pr == NULL)
4852192895Sjamie			/* Assume address points to a valid prison. */
4853192895Sjamie			pr = (struct prison *)addr;
4854192895Sjamie	}
4855191673Sjamie	db_show_prison(pr);
4856185435Sbz}
4857191673Sjamie
4858185435Sbz#endif /* DDB */
4859