kern_jail.c revision 194915
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: head/sys/kern/kern_jail.c 194915 2009-06-24 21:39:50Z 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>
52168401Spjd#include <sys/sx.h>
53193066Sjamie#include <sys/sysent.h>
54113275Smike#include <sys/namei.h>
55147185Spjd#include <sys/mount.h>
56113275Smike#include <sys/queue.h>
5746155Sphk#include <sys/socket.h>
58113275Smike#include <sys/syscallsubr.h>
5957163Srwatson#include <sys/sysctl.h>
60113275Smike#include <sys/vnode.h>
61181803Sbz#include <sys/vimage.h>
6246155Sphk#include <net/if.h>
6346155Sphk#include <netinet/in.h>
64185435Sbz#ifdef DDB
65185435Sbz#include <ddb/ddb.h>
66185435Sbz#ifdef INET6
67185435Sbz#include <netinet6/in6_var.h>
68185435Sbz#endif /* INET6 */
69185435Sbz#endif /* DDB */
7046155Sphk
71163606Srwatson#include <security/mac/mac_framework.h>
72163606Srwatson
7346155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
7446155Sphk
75192895Sjamie/* prison0 describes what is "real" about the system. */
76192895Sjamiestruct prison prison0 = {
77192895Sjamie	.pr_id		= 0,
78192895Sjamie	.pr_name	= "0",
79192895Sjamie	.pr_ref		= 1,
80192895Sjamie	.pr_uref	= 1,
81192895Sjamie	.pr_path	= "/",
82192895Sjamie	.pr_securelevel	= -1,
83194762Sjamie	.pr_childmax	= JAIL_MAX,
84194118Sjamie	.pr_hostuuid	= "00000000-0000-0000-0000-000000000000",
85192895Sjamie	.pr_children	= LIST_HEAD_INITIALIZER(&prison0.pr_children),
86193066Sjamie	.pr_flags	= PR_HOST,
87192895Sjamie	.pr_allow	= PR_ALLOW_ALL,
88192895Sjamie};
89192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
9057163Srwatson
91192895Sjamie/* allprison and lastprid are protected by allprison_lock. */
92168401Spjdstruct	sx allprison_lock;
93191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
94191673Sjamiestruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
95179881Sdelphijint	lastprid = 0;
96113275Smike
97191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr);
98190466Sjamiestatic void prison_complete(void *context, int pending);
99191673Sjamiestatic void prison_deref(struct prison *pr, int flags);
100192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2);
101192895Sjamiestatic void prison_remove_one(struct prison *pr);
102185435Sbz#ifdef INET
103190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
104192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
105185435Sbz#endif
106185435Sbz#ifdef INET6
107190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
108192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
109185435Sbz#endif
110113275Smike
111191673Sjamie/* Flags for prison_deref */
112191673Sjamie#define	PD_DEREF	0x01
113191673Sjamie#define	PD_DEUREF	0x02
114191673Sjamie#define	PD_LOCKED	0x04
115191673Sjamie#define	PD_LIST_SLOCKED	0x08
116191673Sjamie#define	PD_LIST_XLOCKED	0x10
117113275Smike
118192895Sjamie/*
119192895Sjamie * Parameter names corresponding to PR_* flag values
120192895Sjamie */
121192895Sjamiestatic char *pr_flag_names[] = {
122192895Sjamie	[0] = "persist",
123193066Sjamie	"host",
124185435Sbz#ifdef INET
125193066Sjamie	"ip4",
126192895Sjamie#endif
127192895Sjamie#ifdef INET6
128192895Sjamie	[3] = "ip6",
129192895Sjamie#endif
130194251Sjamie#ifdef VIMAGE
131194251Sjamie	[4] = "vnet",
132194251Sjamie#endif
133192895Sjamie};
134192895Sjamie
135192895Sjamiestatic char *pr_flag_nonames[] = {
136192895Sjamie	[0] = "nopersist",
137193066Sjamie	"nohost",
138192895Sjamie#ifdef INET
139193066Sjamie	"noip4",
140192895Sjamie#endif
141192895Sjamie#ifdef INET6
142192895Sjamie	[3] = "noip6",
143192895Sjamie#endif
144194251Sjamie#ifdef VIMAGE
145194251Sjamie	[4] = "novnet",
146194251Sjamie#endif
147192895Sjamie};
148192895Sjamie
149192895Sjamiestatic char *pr_allow_names[] = {
150192895Sjamie	"allow.set_hostname",
151192895Sjamie	"allow.sysvipc",
152192895Sjamie	"allow.raw_sockets",
153192895Sjamie	"allow.chflags",
154192895Sjamie	"allow.mount",
155192895Sjamie	"allow.quotas",
156192895Sjamie	"allow.socket_af",
157192895Sjamie};
158192895Sjamie
159192895Sjamiestatic char *pr_allow_nonames[] = {
160192895Sjamie	"allow.noset_hostname",
161192895Sjamie	"allow.nosysvipc",
162192895Sjamie	"allow.noraw_sockets",
163192895Sjamie	"allow.nochflags",
164192895Sjamie	"allow.nomount",
165192895Sjamie	"allow.noquotas",
166192895Sjamie	"allow.nosocket_af",
167192895Sjamie};
168192895Sjamie
169192895Sjamie#define	JAIL_DEFAULT_ALLOW	PR_ALLOW_SET_HOSTNAME
170192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
171192895Sjamiestatic int jail_default_enforce_statfs = 2;
172192895Sjamie#if defined(INET) || defined(INET6)
173193865Sjamiestatic unsigned jail_max_af_ips = 255;
174192895Sjamie#endif
175192895Sjamie
176192895Sjamie#ifdef INET
177185435Sbzstatic int
178185435Sbzqcmp_v4(const void *ip1, const void *ip2)
179185435Sbz{
180185435Sbz	in_addr_t iaa, iab;
181185435Sbz
182185435Sbz	/*
183185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
184185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
185185435Sbz	 * interesting results.  If you do not understand, do not try.
186185435Sbz	 */
187185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
188185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
189185435Sbz
190185435Sbz	/*
191185435Sbz	 * Do not simply return the difference of the two numbers, the int is
192185435Sbz	 * not wide enough.
193185435Sbz	 */
194185435Sbz	if (iaa > iab)
195185435Sbz		return (1);
196185435Sbz	else if (iaa < iab)
197185435Sbz		return (-1);
198185435Sbz	else
199185435Sbz		return (0);
200185435Sbz}
201185435Sbz#endif
202185435Sbz
203185435Sbz#ifdef INET6
204185435Sbzstatic int
205185435Sbzqcmp_v6(const void *ip1, const void *ip2)
206185435Sbz{
207185435Sbz	const struct in6_addr *ia6a, *ia6b;
208185435Sbz	int i, rc;
209185435Sbz
210185435Sbz	ia6a = (const struct in6_addr *)ip1;
211185435Sbz	ia6b = (const struct in6_addr *)ip2;
212185435Sbz
213185435Sbz	rc = 0;
214190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
215185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
216185435Sbz			rc = 1;
217185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
218185435Sbz			rc = -1;
219185435Sbz	}
220185435Sbz	return (rc);
221185435Sbz}
222185435Sbz#endif
223185435Sbz
224191673Sjamie/*
225191673Sjamie * struct jail_args {
226191673Sjamie *	struct jail *jail;
227191673Sjamie * };
228191673Sjamie */
229191673Sjamieint
230191673Sjamiejail(struct thread *td, struct jail_args *uap)
231185435Sbz{
232191673Sjamie	uint32_t version;
233191673Sjamie	int error;
234192895Sjamie	struct jail j;
235185435Sbz
236191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
237191673Sjamie	if (error)
238191673Sjamie		return (error);
239185435Sbz
240191673Sjamie	switch (version) {
241191673Sjamie	case 0:
242191673Sjamie	{
243191673Sjamie		struct jail_v0 j0;
244185435Sbz
245192895Sjamie		/* FreeBSD single IPv4 jails. */
246192895Sjamie		bzero(&j, sizeof(struct jail));
247191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
248191673Sjamie		if (error)
249191673Sjamie			return (error);
250192895Sjamie		j.version = j0.version;
251192895Sjamie		j.path = j0.path;
252192895Sjamie		j.hostname = j0.hostname;
253192895Sjamie		j.ip4s = j0.ip_number;
254191673Sjamie		break;
255191673Sjamie	}
256191673Sjamie
257191673Sjamie	case 1:
258185435Sbz		/*
259191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
260191673Sjamie		 * that never made it into the official kernel.
261185435Sbz		 */
262191673Sjamie		return (EINVAL);
263185435Sbz
264191673Sjamie	case 2:	/* JAIL_API_VERSION */
265191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
266191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
267191673Sjamie		if (error)
268191673Sjamie			return (error);
269192895Sjamie		break;
270192895Sjamie
271192895Sjamie	default:
272192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
273192895Sjamie		return (EINVAL);
274192895Sjamie	}
275192895Sjamie	return (kern_jail(td, &j));
276192895Sjamie}
277192895Sjamie
278192895Sjamieint
279192895Sjamiekern_jail(struct thread *td, struct jail *j)
280192895Sjamie{
281193865Sjamie	struct iovec optiov[2 * (4
282193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
283193865Sjamie#ifdef INET
284193865Sjamie			    + 1
285193865Sjamie#endif
286193865Sjamie#ifdef INET6
287193865Sjamie			    + 1
288193865Sjamie#endif
289193865Sjamie			    )];
290192895Sjamie	struct uio opt;
291192895Sjamie	char *u_path, *u_hostname, *u_name;
292185435Sbz#ifdef INET
293193865Sjamie	uint32_t ip4s;
294192895Sjamie	struct in_addr *u_ip4;
295192895Sjamie#endif
296192895Sjamie#ifdef INET6
297192895Sjamie	struct in6_addr *u_ip6;
298192895Sjamie#endif
299192895Sjamie	size_t tmplen;
300192895Sjamie	int error, enforce_statfs, fi;
301192895Sjamie
302192895Sjamie	bzero(&optiov, sizeof(optiov));
303192895Sjamie	opt.uio_iov = optiov;
304192895Sjamie	opt.uio_iovcnt = 0;
305192895Sjamie	opt.uio_offset = -1;
306192895Sjamie	opt.uio_resid = -1;
307192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
308192895Sjamie	opt.uio_rw = UIO_READ;
309192895Sjamie	opt.uio_td = td;
310192895Sjamie
311192895Sjamie	/* Set permissions for top-level jails from sysctls. */
312192895Sjamie	if (!jailed(td->td_ucred)) {
313192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
314192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
315192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
316192895Sjamie			    (jail_default_allow & (1 << fi))
317192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
318192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
319192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
320192895Sjamie			opt.uio_iovcnt += 2;
321192895Sjamie		}
322192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
323192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
324192895Sjamie		opt.uio_iovcnt++;
325192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
326192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
327192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
328192895Sjamie		opt.uio_iovcnt++;
329192895Sjamie	}
330192895Sjamie
331192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
332192895Sjamie#ifdef INET
333192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
334192895Sjamie	if (ip4s > jail_max_af_ips)
335192895Sjamie		return (EINVAL);
336192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
337191673Sjamie#else
338192895Sjamie	if (j->ip4s > 0)
339192895Sjamie		return (EINVAL);
340191673Sjamie#endif
341191673Sjamie#ifdef INET6
342192895Sjamie	if (j->ip6s > jail_max_af_ips)
343192895Sjamie		return (EINVAL);
344192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
345191673Sjamie#else
346192895Sjamie	if (j->ip6s > 0)
347192895Sjamie		return (EINVAL);
348191673Sjamie#endif
349192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
350192895Sjamie	u_hostname = u_path + MAXPATHLEN;
351192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
352191673Sjamie#ifdef INET
353192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
354191673Sjamie#endif
355191673Sjamie#ifdef INET6
356191673Sjamie#ifdef INET
357192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
358191673Sjamie#else
359192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
360191673Sjamie#endif
361191673Sjamie#endif
362192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
363192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
364192895Sjamie	opt.uio_iovcnt++;
365192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
366192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
367192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
368192895Sjamie	if (error) {
369192895Sjamie		free(u_path, M_TEMP);
370192895Sjamie		return (error);
371192895Sjamie	}
372192895Sjamie	opt.uio_iovcnt++;
373192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
374192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
375192895Sjamie	opt.uio_iovcnt++;
376192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
377192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
378192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
379192895Sjamie	if (error) {
380192895Sjamie		free(u_path, M_TEMP);
381192895Sjamie		return (error);
382192895Sjamie	}
383192895Sjamie	opt.uio_iovcnt++;
384192895Sjamie	if (j->jailname != NULL) {
385192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
386192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
387192895Sjamie		opt.uio_iovcnt++;
388192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
389192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
390192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
391191673Sjamie		if (error) {
392191673Sjamie			free(u_path, M_TEMP);
393191673Sjamie			return (error);
394191673Sjamie		}
395192895Sjamie		opt.uio_iovcnt++;
396192895Sjamie	}
397191673Sjamie#ifdef INET
398192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
399192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
400192895Sjamie	opt.uio_iovcnt++;
401192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
402192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
403192895Sjamie	if (j->version == 0)
404192895Sjamie		u_ip4->s_addr = j->ip4s;
405192895Sjamie	else {
406192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
407191673Sjamie		if (error) {
408191673Sjamie			free(u_path, M_TEMP);
409191673Sjamie			return (error);
410191673Sjamie		}
411192895Sjamie	}
412192895Sjamie	opt.uio_iovcnt++;
413185435Sbz#endif
414185435Sbz#ifdef INET6
415192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
416192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
417192895Sjamie	opt.uio_iovcnt++;
418192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
419192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
420192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
421192895Sjamie	if (error) {
422192895Sjamie		free(u_path, M_TEMP);
423192895Sjamie		return (error);
424192895Sjamie	}
425192895Sjamie	opt.uio_iovcnt++;
426185435Sbz#endif
427192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
428192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
429191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
430191673Sjamie	free(u_path, M_TEMP);
431191673Sjamie	return (error);
432185435Sbz}
433185435Sbz
434192895Sjamie
435191673Sjamie/*
436191673Sjamie * struct jail_set_args {
437191673Sjamie *	struct iovec *iovp;
438191673Sjamie *	unsigned int iovcnt;
439191673Sjamie *	int flags;
440191673Sjamie * };
441191673Sjamie */
442191673Sjamieint
443191673Sjamiejail_set(struct thread *td, struct jail_set_args *uap)
444185435Sbz{
445191673Sjamie	struct uio *auio;
446191673Sjamie	int error;
447191673Sjamie
448191673Sjamie	/* Check that we have an even number of iovecs. */
449191673Sjamie	if (uap->iovcnt & 1)
450191673Sjamie		return (EINVAL);
451191673Sjamie
452191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
453191673Sjamie	if (error)
454191673Sjamie		return (error);
455191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
456191673Sjamie	free(auio, M_IOV);
457191673Sjamie	return (error);
458191673Sjamie}
459191673Sjamie
460191673Sjamieint
461191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
462191673Sjamie{
463191673Sjamie	struct nameidata nd;
464185435Sbz#ifdef INET
465190466Sjamie	struct in_addr *ip4;
466185435Sbz#endif
467185435Sbz#ifdef INET6
468185435Sbz	struct in6_addr *ip6;
469185435Sbz#endif
470191673Sjamie	struct vfsopt *opt;
471191673Sjamie	struct vfsoptlist *opts;
472192895Sjamie	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
473191673Sjamie	struct vnode *root;
474193066Sjamie	char *domain, *errmsg, *host, *name, *p, *path, *uuid;
475192895Sjamie#if defined(INET) || defined(INET6)
476191673Sjamie	void *op;
477192895Sjamie#endif
478193066Sjamie	unsigned long hid;
479192895Sjamie	size_t namelen, onamelen;
480192895Sjamie	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
481194762Sjamie	int gotchildmax, gotenforce, gothid, gotslevel, fi, jid, len, level;
482194762Sjamie	int childmax, slevel, vfslocked;
483191673Sjamie#if defined(INET) || defined(INET6)
484192895Sjamie	int ii, ij;
485191673Sjamie#endif
486191673Sjamie#ifdef INET
487192895Sjamie	int ip4s, ip4a, redo_ip4;
488191673Sjamie#endif
489191673Sjamie#ifdef INET6
490192895Sjamie	int ip6s, ip6a, redo_ip6;
491191673Sjamie#endif
492191673Sjamie	unsigned pr_flags, ch_flags;
493192895Sjamie	unsigned pr_allow, ch_allow, tallow;
494191673Sjamie	char numbuf[12];
495185435Sbz
496191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
497191673Sjamie	if (!error && (flags & JAIL_ATTACH))
498191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
499191673Sjamie	if (error)
500191673Sjamie		return (error);
501192895Sjamie	mypr = ppr = td->td_ucred->cr_prison;
502194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
503192895Sjamie		return (EPERM);
504191673Sjamie	if (flags & ~JAIL_SET_MASK)
505191673Sjamie		return (EINVAL);
506191673Sjamie
507185435Sbz	/*
508191673Sjamie	 * Check all the parameters before committing to anything.  Not all
509191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
510191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
511191673Sjamie	 * the allprison lock.
512185435Sbz	 *
513191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
514191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
515191673Sjamie	 *     than duplicate it under a different name.
516185435Sbz	 */
517191673Sjamie	error = vfs_buildopts(optuio, &opts);
518191673Sjamie	if (error)
519191673Sjamie		return (error);
520185435Sbz#ifdef INET
521192895Sjamie	ip4a = 0;
522185435Sbz	ip4 = NULL;
523185435Sbz#endif
524185435Sbz#ifdef INET6
525192895Sjamie	ip6a = 0;
526185435Sbz	ip6 = NULL;
527185435Sbz#endif
528191673Sjamie
529192895Sjamie#if defined(INET) || defined(INET6)
530192895Sjamie again:
531192895Sjamie#endif
532191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
533191673Sjamie	if (error == ENOENT)
534191673Sjamie		jid = 0;
535191673Sjamie	else if (error != 0)
536191673Sjamie		goto done_free;
537191673Sjamie
538191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
539191673Sjamie	if (error == ENOENT)
540191673Sjamie		gotslevel = 0;
541191673Sjamie	else if (error != 0)
542191673Sjamie		goto done_free;
543191673Sjamie	else
544191673Sjamie		gotslevel = 1;
545191673Sjamie
546194762Sjamie	error =
547194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
548194762Sjamie	if (error == ENOENT)
549194762Sjamie		gotchildmax = 0;
550194762Sjamie	else if (error != 0)
551194762Sjamie		goto done_free;
552194762Sjamie	else
553194762Sjamie		gotchildmax = 1;
554194762Sjamie
555192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
556192895Sjamie	gotenforce = (error == 0);
557192895Sjamie	if (gotenforce) {
558192895Sjamie		if (enforce < 0 || enforce > 2)
559192895Sjamie			return (EINVAL);
560192895Sjamie	} else if (error != ENOENT)
561192895Sjamie		goto done_free;
562192895Sjamie
563191673Sjamie	pr_flags = ch_flags = 0;
564192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
565192895Sjamie	    fi++) {
566192895Sjamie		if (pr_flag_names[fi] == NULL)
567192895Sjamie			continue;
568192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
569192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
570192895Sjamie	}
571191673Sjamie	ch_flags |= pr_flags;
572191673Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
573191673Sjamie	    && !(pr_flags & PR_PERSIST)) {
574191673Sjamie		error = EINVAL;
575191673Sjamie		vfs_opterror(opts, "new jail must persist or attach");
576191673Sjamie		goto done_errmsg;
577191673Sjamie	}
578194251Sjamie#ifdef VIMAGE
579194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
580194251Sjamie		error = EINVAL;
581194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
582194251Sjamie		goto done_errmsg;
583194251Sjamie	}
584194251Sjamie#endif
585191673Sjamie
586192895Sjamie	pr_allow = ch_allow = 0;
587192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
588192895Sjamie	    fi++) {
589192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
590192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
591192895Sjamie	}
592192895Sjamie	ch_allow |= pr_allow;
593192895Sjamie
594191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
595191673Sjamie	if (error == ENOENT)
596191673Sjamie		name = NULL;
597191673Sjamie	else if (error != 0)
598191673Sjamie		goto done_free;
599191673Sjamie	else {
600191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
601191673Sjamie			error = EINVAL;
602191673Sjamie			goto done_free;
603191673Sjamie		}
604191673Sjamie		if (len > MAXHOSTNAMELEN) {
605191673Sjamie			error = ENAMETOOLONG;
606191673Sjamie			goto done_free;
607191673Sjamie		}
608191673Sjamie	}
609191673Sjamie
610191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
611191673Sjamie	if (error == ENOENT)
612191673Sjamie		host = NULL;
613191673Sjamie	else if (error != 0)
614191673Sjamie		goto done_free;
615191673Sjamie	else {
616193066Sjamie		ch_flags |= PR_HOST;
617193066Sjamie		pr_flags |= PR_HOST;
618191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
619191673Sjamie			error = EINVAL;
620191673Sjamie			goto done_free;
621191673Sjamie		}
622191673Sjamie		if (len > MAXHOSTNAMELEN) {
623191673Sjamie			error = ENAMETOOLONG;
624191673Sjamie			goto done_free;
625191673Sjamie		}
626191673Sjamie	}
627191673Sjamie
628193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
629193066Sjamie	if (error == ENOENT)
630193066Sjamie		domain = NULL;
631193066Sjamie	else if (error != 0)
632193066Sjamie		goto done_free;
633193066Sjamie	else {
634193066Sjamie		ch_flags |= PR_HOST;
635193066Sjamie		pr_flags |= PR_HOST;
636193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
637193066Sjamie			error = EINVAL;
638193066Sjamie			goto done_free;
639193066Sjamie		}
640193066Sjamie		if (len > MAXHOSTNAMELEN) {
641193066Sjamie			error = ENAMETOOLONG;
642193066Sjamie			goto done_free;
643193066Sjamie		}
644193066Sjamie	}
645193066Sjamie
646193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
647193066Sjamie	if (error == ENOENT)
648193066Sjamie		uuid = NULL;
649193066Sjamie	else if (error != 0)
650193066Sjamie		goto done_free;
651193066Sjamie	else {
652193066Sjamie		ch_flags |= PR_HOST;
653193066Sjamie		pr_flags |= PR_HOST;
654193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
655193066Sjamie			error = EINVAL;
656193066Sjamie			goto done_free;
657193066Sjamie		}
658193066Sjamie		if (len > HOSTUUIDLEN) {
659193066Sjamie			error = ENAMETOOLONG;
660193066Sjamie			goto done_free;
661193066Sjamie		}
662193066Sjamie	}
663193066Sjamie
664193066Sjamie#ifdef COMPAT_IA32
665193066Sjamie	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
666193066Sjamie		uint32_t hid32;
667193066Sjamie
668193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
669193066Sjamie		hid = hid32;
670193066Sjamie	} else
671193066Sjamie#endif
672193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
673193066Sjamie	if (error == ENOENT)
674193066Sjamie		gothid = 0;
675193066Sjamie	else if (error != 0)
676193066Sjamie		goto done_free;
677193066Sjamie	else {
678193066Sjamie		gothid = 1;
679193066Sjamie		ch_flags |= PR_HOST;
680193066Sjamie		pr_flags |= PR_HOST;
681193066Sjamie	}
682193066Sjamie
683192895Sjamie	/* This might be the second time around for this option. */
684185435Sbz#ifdef INET
685191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
686191673Sjamie	if (error == ENOENT)
687191673Sjamie		ip4s = -1;
688191673Sjamie	else if (error != 0)
689191673Sjamie		goto done_free;
690191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
691191673Sjamie		error = EINVAL;
692191673Sjamie		goto done_free;
693192895Sjamie	} else {
694192895Sjamie		ch_flags |= PR_IP4_USER;
695192895Sjamie		pr_flags |= PR_IP4_USER;
696192895Sjamie		if (ip4s > 0) {
697192895Sjamie			ip4s /= sizeof(*ip4);
698192895Sjamie			if (ip4s > jail_max_af_ips) {
699185435Sbz				error = EINVAL;
700192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
701192895Sjamie				goto done_errmsg;
702185435Sbz			}
703192895Sjamie			if (ip4a < ip4s) {
704192895Sjamie				ip4a = ip4s;
705192895Sjamie				free(ip4, M_PRISON);
706192895Sjamie				ip4 = NULL;
707185435Sbz			}
708192895Sjamie			if (ip4 == NULL)
709192895Sjamie				ip4 = malloc(ip4a * sizeof(*ip4), M_PRISON,
710192895Sjamie				    M_WAITOK);
711192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
712192895Sjamie			/*
713192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
714192895Sjamie			 * the primary IP address as given from userland.
715192895Sjamie			 * This special IP is used for unbound outgoing
716192895Sjamie			 * connections as well for "loopback" traffic.
717192895Sjamie			 */
718192895Sjamie			if (ip4s > 1)
719192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
720192895Sjamie			/*
721192895Sjamie			 * Check for duplicate addresses and do some simple
722192895Sjamie			 * zero and broadcast checks. If users give other bogus
723192895Sjamie			 * addresses it is their problem.
724192895Sjamie			 *
725192895Sjamie			 * We do not have to care about byte order for these
726192895Sjamie			 * checks so we will do them in NBO.
727192895Sjamie			 */
728192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
729192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
730192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
731192895Sjamie					error = EINVAL;
732192895Sjamie					goto done_free;
733192895Sjamie				}
734192895Sjamie				if ((ii+1) < ip4s &&
735192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
736192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
737192895Sjamie					error = EINVAL;
738192895Sjamie					goto done_free;
739192895Sjamie				}
740192895Sjamie			}
741185435Sbz		}
742191673Sjamie	}
743191673Sjamie#endif
744185435Sbz
745185435Sbz#ifdef INET6
746191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
747191673Sjamie	if (error == ENOENT)
748191673Sjamie		ip6s = -1;
749191673Sjamie	else if (error != 0)
750191673Sjamie		goto done_free;
751191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
752191673Sjamie		error = EINVAL;
753191673Sjamie		goto done_free;
754192895Sjamie	} else {
755192895Sjamie		ch_flags |= PR_IP6_USER;
756192895Sjamie		pr_flags |= PR_IP6_USER;
757192895Sjamie		if (ip6s > 0) {
758192895Sjamie			ip6s /= sizeof(*ip6);
759192895Sjamie			if (ip6s > jail_max_af_ips) {
760185435Sbz				error = EINVAL;
761192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
762192895Sjamie				goto done_errmsg;
763185435Sbz			}
764192895Sjamie			if (ip6a < ip6s) {
765192895Sjamie				ip6a = ip6s;
766192895Sjamie				free(ip6, M_PRISON);
767192895Sjamie				ip6 = NULL;
768185435Sbz			}
769192895Sjamie			if (ip6 == NULL)
770192895Sjamie				ip6 = malloc(ip6a * sizeof(*ip6), M_PRISON,
771192895Sjamie				    M_WAITOK);
772192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
773192895Sjamie			if (ip6s > 1)
774192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
775192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
776192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
777192895Sjamie					error = EINVAL;
778192895Sjamie					goto done_free;
779192895Sjamie				}
780192895Sjamie				if ((ii+1) < ip6s &&
781192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
782192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
783192895Sjamie				{
784192895Sjamie					error = EINVAL;
785192895Sjamie					goto done_free;
786192895Sjamie				}
787192895Sjamie			}
788185435Sbz		}
789191673Sjamie	}
790185435Sbz#endif
791185435Sbz
792191673Sjamie	root = NULL;
793191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
794191673Sjamie	if (error == ENOENT)
795191673Sjamie		path = NULL;
796191673Sjamie	else if (error != 0)
797191673Sjamie		goto done_free;
798191673Sjamie	else {
799191673Sjamie		if (flags & JAIL_UPDATE) {
800191673Sjamie			error = EINVAL;
801191673Sjamie			vfs_opterror(opts,
802191673Sjamie			    "path cannot be changed after creation");
803191673Sjamie			goto done_errmsg;
804191673Sjamie		}
805191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
806191673Sjamie			error = EINVAL;
807191673Sjamie			goto done_free;
808191673Sjamie		}
809191673Sjamie		if (len < 2 || (len == 2 && path[0] == '/'))
810191673Sjamie			path = NULL;
811191673Sjamie		else {
812192895Sjamie			/* Leave room for a real-root full pathname. */
813192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
814192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
815192895Sjamie				error = ENAMETOOLONG;
816192895Sjamie				goto done_free;
817192895Sjamie			}
818191673Sjamie			NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
819191673Sjamie			    path, td);
820191673Sjamie			error = namei(&nd);
821191673Sjamie			if (error)
822191673Sjamie				goto done_free;
823191673Sjamie			vfslocked = NDHASGIANT(&nd);
824191673Sjamie			root = nd.ni_vp;
825191673Sjamie			NDFREE(&nd, NDF_ONLY_PNBUF);
826191673Sjamie			if (root->v_type != VDIR) {
827191673Sjamie				error = ENOTDIR;
828191673Sjamie				vrele(root);
829191673Sjamie				VFS_UNLOCK_GIANT(vfslocked);
830191673Sjamie				goto done_free;
831191673Sjamie			}
832191673Sjamie			VFS_UNLOCK_GIANT(vfslocked);
833191673Sjamie		}
834191673Sjamie	}
835185435Sbz
836191673Sjamie	/*
837191673Sjamie	 * Grab the allprison lock before letting modules check their
838191673Sjamie	 * parameters.  Once we have it, do not let go so we'll have a
839191673Sjamie	 * consistent view of the OSD list.
840191673Sjamie	 */
841191673Sjamie	sx_xlock(&allprison_lock);
842191673Sjamie	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
843191673Sjamie	if (error)
844191673Sjamie		goto done_unlock_list;
845185435Sbz
846191673Sjamie	/* By now, all parameters should have been noted. */
847191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
848191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
849191673Sjamie			error = EINVAL;
850191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
851191673Sjamie			goto done_unlock_list;
852191673Sjamie		}
853191673Sjamie	}
854191673Sjamie
855185435Sbz	/*
856191673Sjamie	 * See if we are creating a new record or updating an existing one.
857191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
858185435Sbz	 */
859191673Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
860191673Sjamie	if (!cuflags) {
861191673Sjamie		error = EINVAL;
862191673Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
863191673Sjamie		goto done_unlock_list;
864191673Sjamie	}
865191673Sjamie	pr = NULL;
866191673Sjamie	if (jid != 0) {
867192895Sjamie		/*
868192895Sjamie		 * See if a requested jid already exists.  There is an
869192895Sjamie		 * information leak here if the jid exists but is not within
870192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
871192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
872192895Sjamie		 * will return ENOENT which is not normally a valid error.
873192895Sjamie		 */
874191673Sjamie		if (jid < 0) {
875191673Sjamie			error = EINVAL;
876191673Sjamie			vfs_opterror(opts, "negative jid");
877191673Sjamie			goto done_unlock_list;
878191673Sjamie		}
879191673Sjamie		pr = prison_find(jid);
880191673Sjamie		if (pr != NULL) {
881192895Sjamie			ppr = pr->pr_parent;
882191673Sjamie			/* Create: jid must not exist. */
883191673Sjamie			if (cuflags == JAIL_CREATE) {
884191673Sjamie				mtx_unlock(&pr->pr_mtx);
885191673Sjamie				error = EEXIST;
886191673Sjamie				vfs_opterror(opts, "jail %d already exists",
887191673Sjamie				    jid);
888191673Sjamie				goto done_unlock_list;
889191673Sjamie			}
890192895Sjamie			if (!prison_ischild(mypr, pr)) {
891192895Sjamie				mtx_unlock(&pr->pr_mtx);
892192895Sjamie				pr = NULL;
893192895Sjamie			} else if (pr->pr_uref == 0) {
894191673Sjamie				if (!(flags & JAIL_DYING)) {
895191673Sjamie					mtx_unlock(&pr->pr_mtx);
896191673Sjamie					error = ENOENT;
897191673Sjamie					vfs_opterror(opts, "jail %d is dying",
898191673Sjamie					    jid);
899191673Sjamie					goto done_unlock_list;
900191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
901191673Sjamie				    (pr_flags & PR_PERSIST)) {
902191673Sjamie					/*
903191673Sjamie					 * A dying jail might be resurrected
904191673Sjamie					 * (via attach or persist), but first
905191673Sjamie					 * it must determine if another jail
906191673Sjamie					 * has claimed its name.  Accomplish
907191673Sjamie					 * this by implicitly re-setting the
908191673Sjamie					 * name.
909191673Sjamie					 */
910191673Sjamie					if (name == NULL)
911192895Sjamie						name = prison_name(mypr, pr);
912191673Sjamie				}
913191673Sjamie			}
914191673Sjamie		}
915191673Sjamie		if (pr == NULL) {
916191673Sjamie			/* Update: jid must exist. */
917191673Sjamie			if (cuflags == JAIL_UPDATE) {
918191673Sjamie				error = ENOENT;
919191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
920191673Sjamie				goto done_unlock_list;
921191673Sjamie			}
922191673Sjamie		}
923191673Sjamie	}
924191673Sjamie	/*
925191673Sjamie	 * If the caller provided a name, look for a jail by that name.
926191673Sjamie	 * This has different semantics for creates and updates keyed by jid
927191673Sjamie	 * (where the name must not already exist in a different jail),
928191673Sjamie	 * and updates keyed by the name itself (where the name must exist
929191673Sjamie	 * because that is the jail being updated).
930191673Sjamie	 */
931191673Sjamie	if (name != NULL) {
932192895Sjamie		p = strrchr(name, '.');
933192895Sjamie		if (p != NULL) {
934192895Sjamie			/*
935192895Sjamie			 * This is a hierarchical name.  Split it into the
936192895Sjamie			 * parent and child names, and make sure the parent
937192895Sjamie			 * exists or matches an already found jail.
938192895Sjamie			 */
939192895Sjamie			*p = '\0';
940192895Sjamie			if (pr != NULL) {
941192895Sjamie				if (strncmp(name, ppr->pr_name, p - name) ||
942192895Sjamie				    ppr->pr_name[p - name] != '\0') {
943192895Sjamie					mtx_unlock(&pr->pr_mtx);
944192895Sjamie					error = EINVAL;
945192895Sjamie					vfs_opterror(opts,
946192895Sjamie					    "cannot change jail's parent");
947192895Sjamie					goto done_unlock_list;
948192895Sjamie				}
949192895Sjamie			} else {
950192895Sjamie				ppr = prison_find_name(mypr, name);
951192895Sjamie				if (ppr == NULL) {
952192895Sjamie					error = ENOENT;
953192895Sjamie					vfs_opterror(opts,
954192895Sjamie					    "jail \"%s\" not found", name);
955192895Sjamie					goto done_unlock_list;
956192895Sjamie				}
957192895Sjamie				mtx_unlock(&ppr->pr_mtx);
958192895Sjamie			}
959192895Sjamie			name = p + 1;
960192895Sjamie		}
961191673Sjamie		if (name[0] != '\0') {
962192895Sjamie			namelen =
963192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
964192895Sjamie name_again:
965191673Sjamie			deadpr = NULL;
966192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
967191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
968192895Sjamie				    !strcmp(tpr->pr_name + namelen, name)) {
969191673Sjamie					if (pr == NULL &&
970191673Sjamie					    cuflags != JAIL_CREATE) {
971191673Sjamie						mtx_lock(&tpr->pr_mtx);
972191673Sjamie						if (tpr->pr_ref > 0) {
973191673Sjamie							/*
974191673Sjamie							 * Use this jail
975191673Sjamie							 * for updates.
976191673Sjamie							 */
977191673Sjamie							if (tpr->pr_uref > 0) {
978191673Sjamie								pr = tpr;
979191673Sjamie								break;
980191673Sjamie							}
981191673Sjamie							deadpr = tpr;
982191673Sjamie						}
983191673Sjamie						mtx_unlock(&tpr->pr_mtx);
984191673Sjamie					} else if (tpr->pr_uref > 0) {
985191673Sjamie						/*
986191673Sjamie						 * Create, or update(jid):
987191673Sjamie						 * name must not exist in an
988192895Sjamie						 * active sibling jail.
989191673Sjamie						 */
990191673Sjamie						error = EEXIST;
991191673Sjamie						if (pr != NULL)
992191673Sjamie							mtx_unlock(&pr->pr_mtx);
993191673Sjamie						vfs_opterror(opts,
994191673Sjamie						   "jail \"%s\" already exists",
995191673Sjamie						   name);
996191673Sjamie						goto done_unlock_list;
997191673Sjamie					}
998191673Sjamie				}
999191673Sjamie			}
1000191673Sjamie			/* If no active jail is found, use a dying one. */
1001191673Sjamie			if (deadpr != NULL && pr == NULL) {
1002191673Sjamie				if (flags & JAIL_DYING) {
1003191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1004191673Sjamie					if (deadpr->pr_ref == 0) {
1005191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1006191673Sjamie						goto name_again;
1007191673Sjamie					}
1008191673Sjamie					pr = deadpr;
1009191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1010191673Sjamie					error = ENOENT;
1011191673Sjamie					vfs_opterror(opts,
1012191673Sjamie					    "jail \"%s\" is dying", name);
1013191673Sjamie					goto done_unlock_list;
1014191673Sjamie				}
1015191673Sjamie			}
1016191673Sjamie			/* Update: name must exist if no jid. */
1017191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1018191673Sjamie				error = ENOENT;
1019191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1020191673Sjamie				    name);
1021191673Sjamie				goto done_unlock_list;
1022191673Sjamie			}
1023191673Sjamie		}
1024191673Sjamie	}
1025191673Sjamie	/* Update: must provide a jid or name. */
1026191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1027191673Sjamie		error = ENOENT;
1028191673Sjamie		vfs_opterror(opts, "update specified no jail");
1029191673Sjamie		goto done_unlock_list;
1030191673Sjamie	}
1031185435Sbz
1032191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1033191673Sjamie	if (pr == NULL) {
1034194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1035194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1036194762Sjamie				error = EPERM;
1037194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1038194762Sjamie				goto done_unlock_list;
1039194762Sjamie			}
1040191673Sjamie		created = 1;
1041192895Sjamie		mtx_lock(&ppr->pr_mtx);
1042192895Sjamie		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1043192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1044192895Sjamie			error = ENOENT;
1045192895Sjamie			vfs_opterror(opts, "parent jail went away!");
1046192895Sjamie			goto done_unlock_list;
1047192895Sjamie		}
1048192895Sjamie		ppr->pr_ref++;
1049192895Sjamie		ppr->pr_uref++;
1050192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1051191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1052191673Sjamie		if (jid == 0) {
1053191673Sjamie			/* Find the next free jid. */
1054191673Sjamie			jid = lastprid + 1;
1055191673Sjamie findnext:
1056191673Sjamie			if (jid == JAIL_MAX)
1057191673Sjamie				jid = 1;
1058191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1059191673Sjamie				if (tpr->pr_id < jid)
1060191673Sjamie					continue;
1061191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1062191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1063191673Sjamie					break;
1064191673Sjamie				}
1065191673Sjamie				if (jid == lastprid) {
1066191673Sjamie					error = EAGAIN;
1067191673Sjamie					vfs_opterror(opts,
1068191673Sjamie					    "no available jail IDs");
1069191673Sjamie					free(pr, M_PRISON);
1070192895Sjamie					prison_deref(ppr, PD_DEREF |
1071192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1072192895Sjamie					goto done_releroot;
1073191673Sjamie				}
1074191673Sjamie				jid++;
1075191673Sjamie				goto findnext;
1076191673Sjamie			}
1077191673Sjamie			lastprid = jid;
1078191673Sjamie		} else {
1079191673Sjamie			/*
1080191673Sjamie			 * The jail already has a jid (that did not yet exist),
1081191673Sjamie			 * so just find where to insert it.
1082191673Sjamie			 */
1083191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1084191673Sjamie				if (tpr->pr_id >= jid) {
1085191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1086191673Sjamie					break;
1087191673Sjamie				}
1088191673Sjamie		}
1089191673Sjamie		if (tpr == NULL)
1090191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1091192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1092192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1093194762Sjamie			tpr->pr_childcount++;
1094185435Sbz
1095192895Sjamie		pr->pr_parent = ppr;
1096191673Sjamie		pr->pr_id = jid;
1097192895Sjamie
1098192895Sjamie		/* Set some default values, and inherit some from the parent. */
1099191673Sjamie		if (name == NULL)
1100191673Sjamie			name = "";
1101193066Sjamie		if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1102193066Sjamie			if (host == NULL)
1103194118Sjamie				host = ppr->pr_hostname;
1104193066Sjamie			if (domain == NULL)
1105194118Sjamie				domain = ppr->pr_domainname;
1106193066Sjamie			if (uuid == NULL)
1107194118Sjamie				uuid = ppr->pr_hostuuid;
1108193066Sjamie			if (!gothid)
1109193066Sjamie				hid = ppr->pr_hostid;
1110193066Sjamie		}
1111191673Sjamie		if (path == NULL) {
1112191673Sjamie			path = "/";
1113192895Sjamie			root = mypr->pr_root;
1114191673Sjamie			vref(root);
1115191673Sjamie		}
1116192895Sjamie#ifdef INET
1117192895Sjamie		pr->pr_flags |= ppr->pr_flags & PR_IP4;
1118192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
1119192895Sjamie		if (ppr->pr_ip4 != NULL) {
1120192895Sjamie			pr->pr_ip4 = malloc(pr->pr_ip4s *
1121192895Sjamie			    sizeof(struct in_addr), M_PRISON, M_WAITOK);
1122192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
1123192895Sjamie			    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1124192895Sjamie		}
1125192895Sjamie#endif
1126192895Sjamie#ifdef INET6
1127192895Sjamie		pr->pr_flags |= ppr->pr_flags & PR_IP6;
1128192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
1129192895Sjamie		if (ppr->pr_ip6 != NULL) {
1130192895Sjamie			pr->pr_ip6 = malloc(pr->pr_ip6s *
1131192895Sjamie			    sizeof(struct in6_addr), M_PRISON, M_WAITOK);
1132192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
1133192895Sjamie			    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1134192895Sjamie		}
1135192895Sjamie#endif
1136192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1137192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1138192895Sjamie		pr->pr_enforce_statfs = ppr->pr_enforce_statfs;
1139191673Sjamie
1140192895Sjamie		LIST_INIT(&pr->pr_children);
1141192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1142191673Sjamie
1143194251Sjamie#ifdef VIMAGE
1144194251Sjamie		/* Allocate a new vnet if specified. */
1145194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1146194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1147194251Sjamie#endif
1148185435Sbz		/*
1149191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1150191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1151185435Sbz		 */
1152192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1153191673Sjamie		if (error) {
1154191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1155191673Sjamie			goto done_releroot;
1156191673Sjamie		}
1157185435Sbz
1158191673Sjamie		mtx_lock(&pr->pr_mtx);
1159185435Sbz		/*
1160191673Sjamie		 * New prisons do not yet have a reference, because we do not
1161191673Sjamie		 * want other to see the incomplete prison once the
1162191673Sjamie		 * allprison_lock is downgraded.
1163185435Sbz		 */
1164191673Sjamie	} else {
1165191673Sjamie		created = 0;
1166191673Sjamie		/*
1167191673Sjamie		 * Grab a reference for existing prisons, to ensure they
1168191673Sjamie		 * continue to exist for the duration of the call.
1169191673Sjamie		 */
1170191673Sjamie		pr->pr_ref++;
1171191673Sjamie	}
1172185435Sbz
1173191673Sjamie	/* Do final error checking before setting anything. */
1174192895Sjamie	if (gotslevel) {
1175192895Sjamie		if (slevel < ppr->pr_securelevel) {
1176192895Sjamie			error = EPERM;
1177192895Sjamie			goto done_deref_locked;
1178192895Sjamie		}
1179192895Sjamie	}
1180194762Sjamie	if (gotchildmax) {
1181194762Sjamie		if (childmax >= ppr->pr_childmax) {
1182194762Sjamie			error = EPERM;
1183194762Sjamie			goto done_deref_locked;
1184194762Sjamie		}
1185194762Sjamie	}
1186192895Sjamie	if (gotenforce) {
1187192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1188192895Sjamie			error = EPERM;
1189192895Sjamie			goto done_deref_locked;
1190192895Sjamie		}
1191192895Sjamie	}
1192185435Sbz#ifdef INET
1193192895Sjamie	if (ch_flags & PR_IP4_USER) {
1194192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1195192895Sjamie			if (!(pr_flags & PR_IP4_USER)) {
1196192895Sjamie				/*
1197192895Sjamie				 * Silently ignore attempts to make the IP
1198192895Sjamie				 * addresses unrestricted when the parent is
1199192895Sjamie				 * restricted; in other words, interpret
1200192895Sjamie				 * "unrestricted" as "as unrestricted as
1201192895Sjamie				 * possible".
1202192895Sjamie				 */
1203192895Sjamie				ip4s = ppr->pr_ip4s;
1204192895Sjamie				if (ip4s == 0) {
1205192895Sjamie					free(ip4, M_PRISON);
1206192895Sjamie					ip4 = NULL;
1207192895Sjamie				} else if (ip4s <= ip4a) {
1208192895Sjamie					/* Inherit the parent's address(es). */
1209192895Sjamie					bcopy(ppr->pr_ip4, ip4,
1210192895Sjamie					    ip4s * sizeof(*ip4));
1211192895Sjamie				} else {
1212192895Sjamie					/*
1213192895Sjamie					 * There's no room for the parent's
1214192895Sjamie					 * address list.  Allocate some more.
1215192895Sjamie					 */
1216192895Sjamie					ip4a = ip4s;
1217192895Sjamie					free(ip4, M_PRISON);
1218192895Sjamie					ip4 = malloc(ip4a * sizeof(*ip4),
1219192895Sjamie					    M_PRISON, M_NOWAIT);
1220192895Sjamie					if (ip4 != NULL)
1221192895Sjamie						bcopy(ppr->pr_ip4, ip4,
1222192895Sjamie						    ip4s * sizeof(*ip4));
1223192895Sjamie					else {
1224192895Sjamie						/* Allocation failed without
1225192895Sjamie						 * sleeping.  Unlocking the
1226192895Sjamie						 * prison now will invalidate
1227192895Sjamie						 * some checks and prematurely
1228192895Sjamie						 * show an unfinished new jail.
1229192895Sjamie						 * So let go of everything and
1230192895Sjamie						 * start over.
1231192895Sjamie						 */
1232192895Sjamie						prison_deref(pr, created
1233192895Sjamie						    ? PD_LOCKED |
1234192895Sjamie						      PD_LIST_XLOCKED
1235192895Sjamie						    : PD_DEREF | PD_LOCKED |
1236192895Sjamie						      PD_LIST_XLOCKED);
1237192895Sjamie						if (root != NULL) {
1238192895Sjamie							vfslocked =
1239192895Sjamie							    VFS_LOCK_GIANT(
1240192895Sjamie							    root->v_mount);
1241192895Sjamie							vrele(root);
1242192895Sjamie							VFS_UNLOCK_GIANT(
1243192895Sjamie							    vfslocked);
1244192895Sjamie						}
1245192895Sjamie						ip4 = malloc(ip4a *
1246192895Sjamie						    sizeof(*ip4), M_PRISON,
1247192895Sjamie						    M_WAITOK);
1248192895Sjamie						goto again;
1249192895Sjamie					}
1250192895Sjamie				}
1251192895Sjamie			} else if (ip4s > 0) {
1252192895Sjamie				/*
1253192895Sjamie				 * Make sure the new set of IP addresses is a
1254192895Sjamie				 * subset of the parent's list.  Don't worry
1255192895Sjamie				 * about the parent being unlocked, as any
1256192895Sjamie				 * setting is done with allprison_lock held.
1257192895Sjamie				 */
1258192895Sjamie				for (ij = 0; ij < ppr->pr_ip4s; ij++)
1259192895Sjamie					if (ip4[0].s_addr ==
1260192895Sjamie					    ppr->pr_ip4[ij].s_addr)
1261192895Sjamie						break;
1262192895Sjamie				if (ij == ppr->pr_ip4s) {
1263192895Sjamie					error = EPERM;
1264192895Sjamie					goto done_deref_locked;
1265192895Sjamie				}
1266192895Sjamie				if (ip4s > 1) {
1267192895Sjamie					for (ii = ij = 1; ii < ip4s; ii++) {
1268192895Sjamie						if (ip4[ii].s_addr ==
1269192895Sjamie						    ppr->pr_ip4[0].s_addr)
1270192895Sjamie							continue;
1271192895Sjamie						for (; ij < ppr->pr_ip4s; ij++)
1272192895Sjamie						    if (ip4[ii].s_addr ==
1273192895Sjamie							ppr->pr_ip4[ij].s_addr)
1274192895Sjamie							    break;
1275192895Sjamie						if (ij == ppr->pr_ip4s)
1276192895Sjamie							break;
1277192895Sjamie					}
1278192895Sjamie					if (ij == ppr->pr_ip4s) {
1279192895Sjamie						error = EPERM;
1280192895Sjamie						goto done_deref_locked;
1281192895Sjamie					}
1282192895Sjamie				}
1283192895Sjamie			}
1284192895Sjamie		}
1285192895Sjamie		if (ip4s > 0) {
1286192895Sjamie			/*
1287192895Sjamie			 * Check for conflicting IP addresses.  We permit them
1288192895Sjamie			 * if there is no more than one IP on each jail.  If
1289192895Sjamie			 * there is a duplicate on a jail with more than one
1290192895Sjamie			 * IP stop checking and return error.
1291192895Sjamie			 */
1292192895Sjamie			FOREACH_PRISON_DESCENDANT(&prison0, tpr, descend) {
1293192895Sjamie				if (tpr == pr || tpr->pr_uref == 0) {
1294192895Sjamie					descend = 0;
1295192895Sjamie					continue;
1296192895Sjamie				}
1297192895Sjamie				if (!(tpr->pr_flags & PR_IP4_USER))
1298192895Sjamie					continue;
1299192895Sjamie				descend = 0;
1300192895Sjamie				if (tpr->pr_ip4 == NULL ||
1301192895Sjamie				    (ip4s == 1 && tpr->pr_ip4s == 1))
1302192895Sjamie					continue;
1303192895Sjamie				for (ii = 0; ii < ip4s; ii++) {
1304191673Sjamie					if (_prison_check_ip4(tpr,
1305191673Sjamie					    &ip4[ii]) == 0) {
1306192895Sjamie						error = EADDRINUSE;
1307191673Sjamie						vfs_opterror(opts,
1308191673Sjamie						    "IPv4 addresses clash");
1309191673Sjamie						goto done_deref_locked;
1310191673Sjamie					}
1311192895Sjamie				}
1312192895Sjamie			}
1313192895Sjamie		}
1314192895Sjamie	}
1315185435Sbz#endif
1316191673Sjamie#ifdef INET6
1317192895Sjamie	if (ch_flags & PR_IP6_USER) {
1318192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1319192895Sjamie			if (!(pr_flags & PR_IP6_USER)) {
1320192895Sjamie				/*
1321192895Sjamie				 * Silently ignore attempts to make the IP
1322192895Sjamie				 * addresses unrestricted when the parent is
1323192895Sjamie				 * restricted.
1324192895Sjamie				 */
1325192895Sjamie				ip6s = ppr->pr_ip6s;
1326192895Sjamie				if (ip6s == 0) {
1327192895Sjamie					free(ip6, M_PRISON);
1328192895Sjamie					ip6 = NULL;
1329192895Sjamie				} else if (ip6s <= ip6a) {
1330192895Sjamie					/* Inherit the parent's address(es). */
1331192895Sjamie					bcopy(ppr->pr_ip6, ip6,
1332192895Sjamie					    ip6s * sizeof(*ip6));
1333192895Sjamie				} else {
1334192895Sjamie					/*
1335192895Sjamie					 * There's no room for the parent's
1336192895Sjamie					 * address list.
1337192895Sjamie					 */
1338192895Sjamie					ip6a = ip6s;
1339192895Sjamie					free(ip6, M_PRISON);
1340192895Sjamie					ip6 = malloc(ip6a * sizeof(*ip6),
1341192895Sjamie					    M_PRISON, M_NOWAIT);
1342192895Sjamie					if (ip6 != NULL)
1343192895Sjamie						bcopy(ppr->pr_ip6, ip6,
1344192895Sjamie						    ip6s * sizeof(*ip6));
1345192895Sjamie					else {
1346192895Sjamie						prison_deref(pr, created
1347192895Sjamie						    ? PD_LOCKED |
1348192895Sjamie						      PD_LIST_XLOCKED
1349192895Sjamie						    : PD_DEREF | PD_LOCKED |
1350192895Sjamie						      PD_LIST_XLOCKED);
1351192895Sjamie						if (root != NULL) {
1352192895Sjamie							vfslocked =
1353192895Sjamie							    VFS_LOCK_GIANT(
1354192895Sjamie							    root->v_mount);
1355192895Sjamie							vrele(root);
1356192895Sjamie							VFS_UNLOCK_GIANT(
1357192895Sjamie							    vfslocked);
1358192895Sjamie						}
1359192895Sjamie						ip6 = malloc(ip6a *
1360192895Sjamie						    sizeof(*ip6), M_PRISON,
1361192895Sjamie						    M_WAITOK);
1362192895Sjamie						goto again;
1363192895Sjamie					}
1364192895Sjamie				}
1365192895Sjamie			} else if (ip6s > 0) {
1366192895Sjamie				/*
1367192895Sjamie				 * Make sure the new set of IP addresses is a
1368192895Sjamie				 * subset of the parent's list.
1369192895Sjamie				 */
1370192895Sjamie				for (ij = 0; ij < ppr->pr_ip6s; ij++)
1371192895Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1372192895Sjamie					    &ppr->pr_ip6[ij]))
1373192895Sjamie						break;
1374192895Sjamie				if (ij == ppr->pr_ip6s) {
1375192895Sjamie					error = EPERM;
1376192895Sjamie					goto done_deref_locked;
1377192895Sjamie				}
1378192895Sjamie				if (ip6s > 1) {
1379192895Sjamie					for (ii = ij = 1; ii < ip6s; ii++) {
1380192895Sjamie						if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1381192895Sjamie						    &ppr->pr_ip6[0]))
1382192895Sjamie							continue;
1383192895Sjamie						for (; ij < ppr->pr_ip6s; ij++)
1384192895Sjamie							if (IN6_ARE_ADDR_EQUAL(
1385192895Sjamie							    &ip6[ii],
1386192895Sjamie							    &ppr->pr_ip6[ij]))
1387192895Sjamie								break;
1388192895Sjamie						if (ij == ppr->pr_ip6s)
1389192895Sjamie							break;
1390192895Sjamie					}
1391192895Sjamie					if (ij == ppr->pr_ip6s) {
1392192895Sjamie						error = EPERM;
1393192895Sjamie						goto done_deref_locked;
1394192895Sjamie					}
1395192895Sjamie				}
1396192895Sjamie			}
1397192895Sjamie		}
1398192895Sjamie		if (ip6s > 0) {
1399192895Sjamie			/* Check for conflicting IP addresses. */
1400192895Sjamie			FOREACH_PRISON_DESCENDANT(&prison0, tpr, descend) {
1401192895Sjamie				if (tpr == pr || tpr->pr_uref == 0) {
1402192895Sjamie					descend = 0;
1403192895Sjamie					continue;
1404192895Sjamie				}
1405192895Sjamie				if (!(tpr->pr_flags & PR_IP6_USER))
1406192895Sjamie					continue;
1407192895Sjamie				descend = 0;
1408192895Sjamie				if (tpr->pr_ip6 == NULL ||
1409192895Sjamie				    (ip6s == 1 && tpr->pr_ip6s == 1))
1410192895Sjamie					continue;
1411192895Sjamie				for (ii = 0; ii < ip6s; ii++) {
1412191673Sjamie					if (_prison_check_ip6(tpr,
1413191673Sjamie					    &ip6[ii]) == 0) {
1414192895Sjamie						error = EADDRINUSE;
1415191673Sjamie						vfs_opterror(opts,
1416191673Sjamie						    "IPv6 addresses clash");
1417191673Sjamie						goto done_deref_locked;
1418191673Sjamie					}
1419192895Sjamie				}
1420192895Sjamie			}
1421191673Sjamie		}
1422192895Sjamie	}
1423191673Sjamie#endif
1424192895Sjamie	onamelen = namelen = 0;
1425192895Sjamie	if (name != NULL) {
1426191673Sjamie		/* Give a default name of the jid. */
1427191673Sjamie		if (name[0] == '\0')
1428191673Sjamie			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1429191673Sjamie		else if (strtoul(name, &p, 10) != jid && *p == '\0') {
1430191673Sjamie			error = EINVAL;
1431191673Sjamie			vfs_opterror(opts, "name cannot be numeric");
1432192895Sjamie			goto done_deref_locked;
1433191673Sjamie		}
1434191673Sjamie		/*
1435192895Sjamie		 * Make sure the name isn't too long for the prison or its
1436192895Sjamie		 * children.
1437191673Sjamie		 */
1438192895Sjamie		onamelen = strlen(pr->pr_name);
1439192895Sjamie		namelen = strlen(name);
1440192895Sjamie		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1441192895Sjamie			error = ENAMETOOLONG;
1442192895Sjamie			goto done_deref_locked;
1443192895Sjamie		}
1444192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1445192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1446192895Sjamie			    sizeof(pr->pr_name)) {
1447192895Sjamie				error = ENAMETOOLONG;
1448192895Sjamie				goto done_deref_locked;
1449192895Sjamie			}
1450192895Sjamie		}
1451191673Sjamie	}
1452192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1453192895Sjamie		error = EPERM;
1454192895Sjamie		goto done_deref_locked;
1455192895Sjamie	}
1456185435Sbz
1457191673Sjamie	/* Set the parameters of the prison. */
1458191673Sjamie#ifdef INET
1459192895Sjamie	redo_ip4 = 0;
1460192895Sjamie	if (ch_flags & PR_IP4_USER) {
1461192895Sjamie		if (pr_flags & PR_IP4_USER) {
1462192895Sjamie			/* Some restriction set. */
1463192895Sjamie			pr->pr_flags |= PR_IP4;
1464192895Sjamie			if (ip4s >= 0) {
1465192895Sjamie				free(pr->pr_ip4, M_PRISON);
1466192895Sjamie				pr->pr_ip4s = ip4s;
1467192895Sjamie				pr->pr_ip4 = ip4;
1468192895Sjamie				ip4 = NULL;
1469192895Sjamie			}
1470192895Sjamie		} else if (ppr->pr_flags & PR_IP4) {
1471192895Sjamie			/* This restriction cleared, but keep inherited. */
1472192895Sjamie			free(pr->pr_ip4, M_PRISON);
1473192895Sjamie			pr->pr_ip4s = ip4s;
1474192895Sjamie			pr->pr_ip4 = ip4;
1475192895Sjamie			ip4 = NULL;
1476192895Sjamie		} else {
1477192895Sjamie			/* Restriction cleared, now unrestricted. */
1478192895Sjamie			pr->pr_flags &= ~PR_IP4;
1479192895Sjamie			free(pr->pr_ip4, M_PRISON);
1480192895Sjamie			pr->pr_ip4s = 0;
1481192895Sjamie		}
1482192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1483192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1484192895Sjamie				redo_ip4 = 1;
1485192895Sjamie				descend = 0;
1486192895Sjamie			}
1487192895Sjamie		}
1488185435Sbz	}
1489191673Sjamie#endif
1490191673Sjamie#ifdef INET6
1491192895Sjamie	redo_ip6 = 0;
1492192895Sjamie	if (ch_flags & PR_IP6_USER) {
1493192895Sjamie		if (pr_flags & PR_IP6_USER) {
1494192895Sjamie			/* Some restriction set. */
1495192895Sjamie			pr->pr_flags |= PR_IP6;
1496192895Sjamie			if (ip6s >= 0) {
1497192895Sjamie				free(pr->pr_ip6, M_PRISON);
1498192895Sjamie				pr->pr_ip6s = ip6s;
1499192895Sjamie				pr->pr_ip6 = ip6;
1500192895Sjamie				ip6 = NULL;
1501192895Sjamie			}
1502192895Sjamie		} else if (ppr->pr_flags & PR_IP6) {
1503192895Sjamie			/* This restriction cleared, but keep inherited. */
1504192895Sjamie			free(pr->pr_ip6, M_PRISON);
1505192895Sjamie			pr->pr_ip6s = ip6s;
1506192895Sjamie			pr->pr_ip6 = ip6;
1507192895Sjamie			ip6 = NULL;
1508192895Sjamie		} else {
1509192895Sjamie			/* Restriction cleared, now unrestricted. */
1510192895Sjamie			pr->pr_flags &= ~PR_IP6;
1511192895Sjamie			free(pr->pr_ip6, M_PRISON);
1512192895Sjamie			pr->pr_ip6s = 0;
1513192895Sjamie		}
1514192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1515192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1516192895Sjamie				redo_ip6 = 1;
1517192895Sjamie				descend = 0;
1518192895Sjamie			}
1519192895Sjamie		}
1520191673Sjamie	}
1521191673Sjamie#endif
1522192895Sjamie	if (gotslevel) {
1523191673Sjamie		pr->pr_securelevel = slevel;
1524192895Sjamie		/* Set all child jails to be at least this level. */
1525192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1526192895Sjamie			if (tpr->pr_securelevel < slevel)
1527192895Sjamie				tpr->pr_securelevel = slevel;
1528192895Sjamie	}
1529194762Sjamie	if (gotchildmax) {
1530194762Sjamie		pr->pr_childmax = childmax;
1531194762Sjamie		/* Set all child jails to under this limit. */
1532194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1533194762Sjamie			if (tpr->pr_childmax > childmax - level)
1534194762Sjamie				tpr->pr_childmax = childmax > level
1535194762Sjamie				    ? childmax - level : 0;
1536194762Sjamie	}
1537192895Sjamie	if (gotenforce) {
1538192895Sjamie		pr->pr_enforce_statfs = enforce;
1539192895Sjamie		/* Pass this restriction on to the children. */
1540192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1541192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1542192895Sjamie				tpr->pr_enforce_statfs = enforce;
1543192895Sjamie	}
1544192895Sjamie	if (name != NULL) {
1545192895Sjamie		if (ppr == &prison0)
1546192895Sjamie			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1547192895Sjamie		else
1548192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1549192895Sjamie			    ppr->pr_name, name);
1550192895Sjamie		/* Change this component of child names. */
1551192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1552192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1553192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1554192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1555192895Sjamie		}
1556192895Sjamie	}
1557191673Sjamie	if (path != NULL) {
1558192895Sjamie		/* Try to keep a real-rooted full pathname. */
1559192895Sjamie		if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1560192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1561192895Sjamie			    mypr->pr_path, path);
1562192895Sjamie		else
1563192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1564191673Sjamie		pr->pr_root = root;
1565191673Sjamie	}
1566193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1567193066Sjamie		if (pr->pr_flags & PR_HOST) {
1568193066Sjamie			/*
1569193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1570193066Sjamie			 * the lack of a lock on the parent is not a problem;
1571193066Sjamie			 * it is always set with allprison_lock at least
1572193066Sjamie			 * shared, and is held exclusively here.
1573193066Sjamie			 */
1574194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1575194118Sjamie			    sizeof(pr->pr_hostname));
1576194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1577194118Sjamie			    sizeof(pr->pr_domainname));
1578194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1579194118Sjamie			    sizeof(pr->pr_hostuuid));
1580193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1581193066Sjamie		}
1582193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1583193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1584193066Sjamie		if (host != NULL)
1585194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1586193066Sjamie		if (domain != NULL)
1587194118Sjamie			strlcpy(pr->pr_domainname, domain,
1588194118Sjamie			    sizeof(pr->pr_domainname));
1589193066Sjamie		if (uuid != NULL)
1590194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1591193066Sjamie		if (gothid)
1592193066Sjamie			pr->pr_hostid = hid;
1593193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1594193066Sjamie			if (tpr->pr_flags & PR_HOST)
1595193066Sjamie				descend = 0;
1596193066Sjamie			else {
1597193066Sjamie				if (host != NULL)
1598194118Sjamie					strlcpy(tpr->pr_hostname,
1599194118Sjamie					    pr->pr_hostname,
1600194118Sjamie					    sizeof(tpr->pr_hostname));
1601193066Sjamie				if (domain != NULL)
1602194118Sjamie					strlcpy(tpr->pr_domainname,
1603194118Sjamie					    pr->pr_domainname,
1604194118Sjamie					    sizeof(tpr->pr_domainname));
1605193066Sjamie				if (uuid != NULL)
1606194118Sjamie					strlcpy(tpr->pr_hostuuid,
1607194118Sjamie					    pr->pr_hostuuid,
1608194118Sjamie					    sizeof(tpr->pr_hostuuid));
1609193066Sjamie				if (gothid)
1610193066Sjamie					tpr->pr_hostid = hid;
1611193066Sjamie			}
1612193066Sjamie		}
1613193066Sjamie	}
1614192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1615192895Sjamie		/* Clear allow bits in all children. */
1616192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1617192895Sjamie			tpr->pr_allow &= ~tallow;
1618192895Sjamie	}
1619192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1620191673Sjamie	/*
1621191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1622191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1623191673Sjamie	 * for now, so new ones will remain unseen until after the module
1624191673Sjamie	 * handlers have completed.
1625191673Sjamie	 */
1626191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1627191673Sjamie		if (pr_flags & PR_PERSIST) {
1628191673Sjamie			pr->pr_ref++;
1629191673Sjamie			pr->pr_uref++;
1630191673Sjamie		} else {
1631191673Sjamie			pr->pr_ref--;
1632191673Sjamie			pr->pr_uref--;
1633191673Sjamie		}
1634191673Sjamie	}
1635191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1636191673Sjamie	mtx_unlock(&pr->pr_mtx);
1637185435Sbz
1638192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1639192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1640192895Sjamie	 */
1641192895Sjamie#ifdef INET
1642192895Sjamie	while (redo_ip4) {
1643192895Sjamie		ip4s = pr->pr_ip4s;
1644192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1645192895Sjamie		mtx_lock(&pr->pr_mtx);
1646192895Sjamie		redo_ip4 = 0;
1647192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1648192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1649192895Sjamie				if (ip4 != NULL)
1650192895Sjamie					ip4 = NULL;
1651192895Sjamie				else
1652192895Sjamie					redo_ip4 = 1;
1653192895Sjamie			}
1654192895Sjamie		}
1655192895Sjamie		mtx_unlock(&pr->pr_mtx);
1656192895Sjamie	}
1657192895Sjamie#endif
1658192895Sjamie#ifdef INET6
1659192895Sjamie	while (redo_ip6) {
1660192895Sjamie		ip6s = pr->pr_ip6s;
1661192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1662192895Sjamie		mtx_lock(&pr->pr_mtx);
1663192895Sjamie		redo_ip6 = 0;
1664192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1665192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1666192895Sjamie				if (ip6 != NULL)
1667192895Sjamie					ip6 = NULL;
1668192895Sjamie				else
1669192895Sjamie					redo_ip6 = 1;
1670192895Sjamie			}
1671192895Sjamie		}
1672192895Sjamie		mtx_unlock(&pr->pr_mtx);
1673192895Sjamie	}
1674192895Sjamie#endif
1675192895Sjamie
1676191673Sjamie	/* Let the modules do their work. */
1677191673Sjamie	sx_downgrade(&allprison_lock);
1678191673Sjamie	if (created) {
1679191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1680191673Sjamie		if (error) {
1681191673Sjamie			prison_deref(pr, PD_LIST_SLOCKED);
1682191673Sjamie			goto done_errmsg;
1683191673Sjamie		}
1684191673Sjamie	}
1685191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1686191673Sjamie	if (error) {
1687191673Sjamie		prison_deref(pr, created
1688191673Sjamie		    ? PD_LIST_SLOCKED
1689191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1690191673Sjamie		goto done_errmsg;
1691191673Sjamie	}
1692191673Sjamie
1693191673Sjamie	/* Attach this process to the prison if requested. */
1694191673Sjamie	if (flags & JAIL_ATTACH) {
1695191673Sjamie		mtx_lock(&pr->pr_mtx);
1696191673Sjamie		error = do_jail_attach(td, pr);
1697191673Sjamie		if (error) {
1698191673Sjamie			vfs_opterror(opts, "attach failed");
1699191673Sjamie			if (!created)
1700191673Sjamie				prison_deref(pr, PD_DEREF);
1701191673Sjamie			goto done_errmsg;
1702191673Sjamie		}
1703191673Sjamie	}
1704191673Sjamie
1705191673Sjamie	/*
1706191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1707191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1708191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1709191673Sjamie	 * visible).
1710191673Sjamie	 */
1711191673Sjamie	if (!created) {
1712191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1713191673Sjamie		    ? PD_DEREF
1714191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1715191673Sjamie	} else {
1716191673Sjamie		if (pr_flags & PR_PERSIST) {
1717191673Sjamie			mtx_lock(&pr->pr_mtx);
1718191673Sjamie			pr->pr_ref++;
1719191673Sjamie			pr->pr_uref++;
1720191673Sjamie			mtx_unlock(&pr->pr_mtx);
1721191673Sjamie		}
1722191673Sjamie		if (!(flags & JAIL_ATTACH))
1723191673Sjamie			sx_sunlock(&allprison_lock);
1724191673Sjamie	}
1725191673Sjamie	td->td_retval[0] = pr->pr_id;
1726191673Sjamie	goto done_errmsg;
1727191673Sjamie
1728192895Sjamie done_deref_locked:
1729192895Sjamie	prison_deref(pr, created
1730192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1731192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1732192895Sjamie	goto done_releroot;
1733191673Sjamie done_unlock_list:
1734191673Sjamie	sx_xunlock(&allprison_lock);
1735191673Sjamie done_releroot:
1736191673Sjamie	if (root != NULL) {
1737191673Sjamie		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1738191673Sjamie		vrele(root);
1739191673Sjamie		VFS_UNLOCK_GIANT(vfslocked);
1740191673Sjamie	}
1741191673Sjamie done_errmsg:
1742191673Sjamie	if (error) {
1743191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1744191673Sjamie		if (errmsg_len > 0) {
1745191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1746191673Sjamie			if (errmsg_pos > 0) {
1747191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1748191673Sjamie					bcopy(errmsg,
1749191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1750191673Sjamie					   errmsg_len);
1751191673Sjamie				else
1752191673Sjamie					copyout(errmsg,
1753191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1754191673Sjamie					   errmsg_len);
1755191673Sjamie			}
1756191673Sjamie		}
1757191673Sjamie	}
1758191673Sjamie done_free:
1759191673Sjamie#ifdef INET
1760191673Sjamie	free(ip4, M_PRISON);
1761191673Sjamie#endif
1762191673Sjamie#ifdef INET6
1763191673Sjamie	free(ip6, M_PRISON);
1764191673Sjamie#endif
1765191673Sjamie	vfs_freeopts(opts);
1766191673Sjamie	return (error);
1767191673Sjamie}
1768191673Sjamie
1769191673Sjamie
177082710Sdillon/*
1771191673Sjamie * struct jail_get_args {
1772191673Sjamie *	struct iovec *iovp;
1773191673Sjamie *	unsigned int iovcnt;
1774191673Sjamie *	int flags;
1775114168Smike * };
177682710Sdillon */
177746155Sphkint
1778191673Sjamiejail_get(struct thread *td, struct jail_get_args *uap)
177946155Sphk{
1780191673Sjamie	struct uio *auio;
1781185435Sbz	int error;
1782185435Sbz
1783191673Sjamie	/* Check that we have an even number of iovecs. */
1784191673Sjamie	if (uap->iovcnt & 1)
1785191673Sjamie		return (EINVAL);
1786191673Sjamie
1787191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1788185435Sbz	if (error)
1789185435Sbz		return (error);
1790191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1791191673Sjamie	if (error == 0)
1792191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1793191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1794191673Sjamie	free(auio, M_IOV);
1795191673Sjamie	return (error);
1796191673Sjamie}
1797185435Sbz
1798191673Sjamieint
1799191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
1800191673Sjamie{
1801192895Sjamie	struct prison *pr, *mypr;
1802191673Sjamie	struct vfsopt *opt;
1803191673Sjamie	struct vfsoptlist *opts;
1804191673Sjamie	char *errmsg, *name;
1805192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1806185435Sbz
1807191673Sjamie	if (flags & ~JAIL_GET_MASK)
1808191673Sjamie		return (EINVAL);
1809185435Sbz
1810191673Sjamie	/* Get the parameter list. */
1811191673Sjamie	error = vfs_buildopts(optuio, &opts);
1812191673Sjamie	if (error)
1813191673Sjamie		return (error);
1814191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1815192895Sjamie	mypr = td->td_ucred->cr_prison;
1816185435Sbz
1817191673Sjamie	/*
1818191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
1819191673Sjamie	 */
1820191673Sjamie	sx_slock(&allprison_lock);
1821191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1822191673Sjamie	if (error == 0) {
1823191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
1824192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1825191673Sjamie				mtx_lock(&pr->pr_mtx);
1826191673Sjamie				if (pr->pr_ref > 0 &&
1827191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1828191673Sjamie					break;
1829191673Sjamie				mtx_unlock(&pr->pr_mtx);
1830191673Sjamie			}
1831191673Sjamie		}
1832191673Sjamie		if (pr != NULL)
1833191673Sjamie			goto found_prison;
1834191673Sjamie		error = ENOENT;
1835191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
1836191673Sjamie		goto done_unlock_list;
1837191673Sjamie	} else if (error != ENOENT)
1838191673Sjamie		goto done_unlock_list;
1839185435Sbz
1840191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1841191673Sjamie	if (error == 0) {
1842191673Sjamie		if (jid != 0) {
1843192895Sjamie			pr = prison_find_child(mypr, jid);
1844191673Sjamie			if (pr != NULL) {
1845191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1846191673Sjamie					mtx_unlock(&pr->pr_mtx);
1847191673Sjamie					error = ENOENT;
1848191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1849191673Sjamie					    jid);
1850191673Sjamie					goto done_unlock_list;
1851191673Sjamie				}
1852191673Sjamie				goto found_prison;
1853191673Sjamie			}
1854191673Sjamie			error = ENOENT;
1855191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
1856191673Sjamie			goto done_unlock_list;
1857191673Sjamie		}
1858191673Sjamie	} else if (error != ENOENT)
1859191673Sjamie		goto done_unlock_list;
186046155Sphk
1861191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
1862191673Sjamie	if (error == 0) {
1863191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
1864191673Sjamie			error = EINVAL;
1865191673Sjamie			goto done_unlock_list;
1866191673Sjamie		}
1867192895Sjamie		pr = prison_find_name(mypr, name);
1868191673Sjamie		if (pr != NULL) {
1869191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1870191673Sjamie				mtx_unlock(&pr->pr_mtx);
1871191673Sjamie				error = ENOENT;
1872191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
1873191673Sjamie				    name);
1874191673Sjamie				goto done_unlock_list;
1875191673Sjamie			}
1876191673Sjamie			goto found_prison;
1877191673Sjamie		}
1878191673Sjamie		error = ENOENT;
1879191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
1880191673Sjamie		goto done_unlock_list;
1881191673Sjamie	} else if (error != ENOENT)
1882191673Sjamie		goto done_unlock_list;
1883185435Sbz
1884191673Sjamie	vfs_opterror(opts, "no jail specified");
1885191673Sjamie	error = ENOENT;
1886191673Sjamie	goto done_unlock_list;
1887191673Sjamie
1888191673Sjamie found_prison:
1889191673Sjamie	/* Get the parameters of the prison. */
1890191673Sjamie	pr->pr_ref++;
1891191673Sjamie	locked = PD_LOCKED;
1892191673Sjamie	td->td_retval[0] = pr->pr_id;
1893191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1894191673Sjamie	if (error != 0 && error != ENOENT)
1895191673Sjamie		goto done_deref;
1896192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1897192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1898191673Sjamie	if (error != 0 && error != ENOENT)
1899191673Sjamie		goto done_deref;
1900192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1901192895Sjamie	if (error != 0 && error != ENOENT)
1902192895Sjamie		goto done_deref;
1903192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1904191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
1905191673Sjamie	if (error != 0 && error != ENOENT)
1906191673Sjamie		goto done_deref;
1907192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1908191673Sjamie	if (error != 0 && error != ENOENT)
1909191673Sjamie		goto done_deref;
1910191673Sjamie#ifdef INET
1911191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1912191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1913191673Sjamie	if (error != 0 && error != ENOENT)
1914191673Sjamie		goto done_deref;
1915191673Sjamie#endif
1916191673Sjamie#ifdef INET6
1917191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1918191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1919191673Sjamie	if (error != 0 && error != ENOENT)
1920191673Sjamie		goto done_deref;
1921191673Sjamie#endif
1922191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1923191673Sjamie	    sizeof(pr->pr_securelevel));
1924191673Sjamie	if (error != 0 && error != ENOENT)
1925191673Sjamie		goto done_deref;
1926194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1927194762Sjamie	    sizeof(pr->pr_childcount));
1928194762Sjamie	if (error != 0 && error != ENOENT)
1929194762Sjamie		goto done_deref;
1930194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1931194762Sjamie	    sizeof(pr->pr_childmax));
1932194762Sjamie	if (error != 0 && error != ENOENT)
1933194762Sjamie		goto done_deref;
1934194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1935191673Sjamie	if (error != 0 && error != ENOENT)
1936191673Sjamie		goto done_deref;
1937194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1938193066Sjamie	if (error != 0 && error != ENOENT)
1939193066Sjamie		goto done_deref;
1940194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1941193066Sjamie	if (error != 0 && error != ENOENT)
1942193066Sjamie		goto done_deref;
1943193066Sjamie#ifdef COMPAT_IA32
1944193066Sjamie	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1945193066Sjamie		uint32_t hid32 = pr->pr_hostid;
1946193066Sjamie
1947193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1948193066Sjamie	} else
1949193066Sjamie#endif
1950193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1951193066Sjamie	    sizeof(pr->pr_hostid));
1952193066Sjamie	if (error != 0 && error != ENOENT)
1953193066Sjamie		goto done_deref;
1954192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1955192895Sjamie	    sizeof(pr->pr_enforce_statfs));
1956191673Sjamie	if (error != 0 && error != ENOENT)
1957191673Sjamie		goto done_deref;
1958192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1959192895Sjamie	    fi++) {
1960192895Sjamie		if (pr_flag_names[fi] == NULL)
1961192895Sjamie			continue;
1962192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1963192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1964192895Sjamie		if (error != 0 && error != ENOENT)
1965192895Sjamie			goto done_deref;
1966192895Sjamie		i = !i;
1967192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1968192895Sjamie		if (error != 0 && error != ENOENT)
1969192895Sjamie			goto done_deref;
1970192895Sjamie	}
1971192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
1972192895Sjamie	    fi++) {
1973192895Sjamie		if (pr_allow_names[fi] == NULL)
1974192895Sjamie			continue;
1975192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
1976192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
1977192895Sjamie		if (error != 0 && error != ENOENT)
1978192895Sjamie			goto done_deref;
1979192895Sjamie		i = !i;
1980192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
1981192895Sjamie		if (error != 0 && error != ENOENT)
1982192895Sjamie			goto done_deref;
1983192895Sjamie	}
1984191673Sjamie	i = (pr->pr_uref == 0);
1985191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
1986191673Sjamie	if (error != 0 && error != ENOENT)
1987191673Sjamie		goto done_deref;
1988191673Sjamie	i = !i;
1989191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1990191673Sjamie	if (error != 0 && error != ENOENT)
1991191673Sjamie		goto done_deref;
1992191673Sjamie
1993191673Sjamie	/* Get the module parameters. */
1994191673Sjamie	mtx_unlock(&pr->pr_mtx);
1995191673Sjamie	locked = 0;
1996191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
199746155Sphk	if (error)
1998191673Sjamie		goto done_deref;
1999191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
200084828Sjhb
2001191673Sjamie	/* By now, all parameters should have been noted. */
2002191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2003191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2004191673Sjamie			error = EINVAL;
2005191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2006191673Sjamie			goto done_errmsg;
2007191673Sjamie		}
2008185435Sbz	}
2009191673Sjamie
2010191673Sjamie	/* Write the fetched parameters back to userspace. */
2011191673Sjamie	error = 0;
2012191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2013191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2014191673Sjamie			pos = 2 * opt->pos + 1;
2015191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2016191673Sjamie			if (opt->value != NULL) {
2017191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2018191673Sjamie					bcopy(opt->value,
2019191673Sjamie					    optuio->uio_iov[pos].iov_base,
2020191673Sjamie					    opt->len);
2021191673Sjamie				} else {
2022191673Sjamie					error = copyout(opt->value,
2023191673Sjamie					    optuio->uio_iov[pos].iov_base,
2024191673Sjamie					    opt->len);
2025191673Sjamie					if (error)
2026191673Sjamie						break;
2027191673Sjamie				}
2028191673Sjamie			}
2029191673Sjamie		}
2030185435Sbz	}
2031191673Sjamie	goto done_errmsg;
2032191673Sjamie
2033191673Sjamie done_deref:
2034191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2035191673Sjamie	goto done_errmsg;
2036191673Sjamie
2037191673Sjamie done_unlock_list:
2038191673Sjamie	sx_sunlock(&allprison_lock);
2039191673Sjamie done_errmsg:
2040191673Sjamie	if (error && errmsg_pos >= 0) {
2041191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2042191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2043191673Sjamie		if (errmsg_len > 0) {
2044191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2045191673Sjamie				bcopy(errmsg,
2046191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2047191673Sjamie				    errmsg_len);
2048191673Sjamie			else
2049191673Sjamie				copyout(errmsg,
2050191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2051191673Sjamie				    errmsg_len);
2052191673Sjamie		}
2053185435Sbz	}
2054191673Sjamie	vfs_freeopts(opts);
2055191673Sjamie	return (error);
2056191673Sjamie}
2057113275Smike
2058192895Sjamie
2059191673Sjamie/*
2060191673Sjamie * struct jail_remove_args {
2061191673Sjamie *	int jid;
2062191673Sjamie * };
2063191673Sjamie */
2064191673Sjamieint
2065191673Sjamiejail_remove(struct thread *td, struct jail_remove_args *uap)
2066191673Sjamie{
2067192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2068192895Sjamie	int descend, error;
2069185435Sbz
2070191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2071185435Sbz	if (error)
2072191673Sjamie		return (error);
2073185435Sbz
2074185435Sbz	sx_xlock(&allprison_lock);
2075192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2076191673Sjamie	if (pr == NULL) {
2077185435Sbz		sx_xunlock(&allprison_lock);
2078191673Sjamie		return (EINVAL);
2079185435Sbz	}
2080185435Sbz
2081192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2082192895Sjamie	pr->pr_ref++;
2083192895Sjamie	pr->pr_flags |= PR_REMOVE;
2084192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2085192895Sjamie		mtx_unlock(&pr->pr_mtx);
2086192895Sjamie		lpr = NULL;
2087192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2088192895Sjamie			mtx_lock(&cpr->pr_mtx);
2089192895Sjamie			if (cpr->pr_ref > 0) {
2090192895Sjamie				tpr = cpr;
2091192895Sjamie				cpr->pr_ref++;
2092192895Sjamie				cpr->pr_flags |= PR_REMOVE;
2093192895Sjamie			} else {
2094192895Sjamie				/* Already removed - do not do it again. */
2095192895Sjamie				tpr = NULL;
2096192895Sjamie			}
2097192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2098192895Sjamie			if (lpr != NULL) {
2099192895Sjamie				mtx_lock(&lpr->pr_mtx);
2100192895Sjamie				prison_remove_one(lpr);
2101192895Sjamie				sx_xlock(&allprison_lock);
2102192895Sjamie			}
2103192895Sjamie			lpr = tpr;
2104192895Sjamie		}
2105192895Sjamie		if (lpr != NULL) {
2106192895Sjamie			mtx_lock(&lpr->pr_mtx);
2107192895Sjamie			prison_remove_one(lpr);
2108192895Sjamie			sx_xlock(&allprison_lock);
2109192895Sjamie		}
2110192895Sjamie		mtx_lock(&pr->pr_mtx);
2111192895Sjamie	}
2112192895Sjamie	prison_remove_one(pr);
2113192895Sjamie	return (0);
2114192895Sjamie}
2115192895Sjamie
2116192895Sjamiestatic void
2117192895Sjamieprison_remove_one(struct prison *pr)
2118192895Sjamie{
2119192895Sjamie	struct proc *p;
2120192895Sjamie	int deuref;
2121192895Sjamie
2122191673Sjamie	/* If the prison was persistent, it is not anymore. */
2123191673Sjamie	deuref = 0;
2124191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2125191673Sjamie		pr->pr_ref--;
2126191673Sjamie		deuref = PD_DEUREF;
2127191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2128179881Sdelphij	}
2129113275Smike
2130192895Sjamie	/*
2131192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2132192895Sjamie	 * the prison now.
2133192895Sjamie	 */
2134192895Sjamie	KASSERT(pr->pr_ref > 0,
2135192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2136192895Sjamie	if (pr->pr_ref == 1) {
2137191673Sjamie		prison_deref(pr,
2138191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2139192895Sjamie		return;
2140191673Sjamie	}
2141191673Sjamie
2142113275Smike	mtx_unlock(&pr->pr_mtx);
2143191673Sjamie	sx_xunlock(&allprison_lock);
2144191673Sjamie	/*
2145191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2146191673Sjamie	 */
2147191673Sjamie	sx_slock(&allproc_lock);
2148191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2149191673Sjamie		PROC_LOCK(p);
2150191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2151191673Sjamie		    p->p_ucred->cr_prison == pr)
2152191673Sjamie			psignal(p, SIGKILL);
2153191673Sjamie		PROC_UNLOCK(p);
2154191673Sjamie	}
2155191673Sjamie	sx_sunlock(&allproc_lock);
2156192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2157191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2158113275Smike}
2159113275Smike
2160190466Sjamie
2161113275Smike/*
2162114168Smike * struct jail_attach_args {
2163114168Smike *	int jid;
2164114168Smike * };
2165113275Smike */
2166113275Smikeint
2167114168Smikejail_attach(struct thread *td, struct jail_attach_args *uap)
2168113275Smike{
2169113275Smike	struct prison *pr;
2170191673Sjamie	int error;
2171167309Spjd
2172164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2173126023Snectar	if (error)
2174126023Snectar		return (error);
2175126023Snectar
2176168401Spjd	sx_slock(&allprison_lock);
2177192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2178113275Smike	if (pr == NULL) {
2179168401Spjd		sx_sunlock(&allprison_lock);
2180113275Smike		return (EINVAL);
2181113275Smike	}
2182185435Sbz
2183185435Sbz	/*
2184185435Sbz	 * Do not allow a process to attach to a prison that is not
2185191673Sjamie	 * considered to be "alive".
2186185435Sbz	 */
2187191673Sjamie	if (pr->pr_uref == 0) {
2188185435Sbz		mtx_unlock(&pr->pr_mtx);
2189185435Sbz		sx_sunlock(&allprison_lock);
2190185435Sbz		return (EINVAL);
2191185435Sbz	}
2192191673Sjamie
2193191673Sjamie	return (do_jail_attach(td, pr));
2194191673Sjamie}
2195191673Sjamie
2196191673Sjamiestatic int
2197191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2198191673Sjamie{
2199192895Sjamie	struct prison *ppr;
2200191673Sjamie	struct proc *p;
2201191673Sjamie	struct ucred *newcred, *oldcred;
2202191673Sjamie	int vfslocked, error;
2203191673Sjamie
2204191673Sjamie	/*
2205191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2206191673Sjamie	 * in the same privileged process attempt to attach to two
2207191673Sjamie	 * different jails at the same time.  It is important for
2208191673Sjamie	 * user processes not to do this, or they might end up with
2209191673Sjamie	 * a process root from one prison, but attached to the jail
2210191673Sjamie	 * of another.
2211191673Sjamie	 */
2212113275Smike	pr->pr_ref++;
2213191673Sjamie	pr->pr_uref++;
2214113275Smike	mtx_unlock(&pr->pr_mtx);
2215191673Sjamie
2216191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2217191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2218191673Sjamie	if (error) {
2219191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2220191673Sjamie		return (error);
2221191673Sjamie	}
2222168401Spjd	sx_sunlock(&allprison_lock);
2223113275Smike
2224185435Sbz	/*
2225185435Sbz	 * Reparent the newly attached process to this jail.
2226185435Sbz	 */
2227192895Sjamie	ppr = td->td_ucred->cr_prison;
2228191673Sjamie	p = td->td_proc;
2229185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2230185435Sbz	if (error)
2231191673Sjamie		goto e_revert_osd;
2232185435Sbz
2233150652Scsjp	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2234175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2235113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2236113275Smike		goto e_unlock;
2237113275Smike#ifdef MAC
2238172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2239113275Smike		goto e_unlock;
2240113275Smike#endif
2241175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2242191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2243191673Sjamie		goto e_unlock_giant;
2244150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2245113275Smike
224684828Sjhb	newcred = crget();
224784828Sjhb	PROC_LOCK(p);
224884828Sjhb	oldcred = p->p_ucred;
2249113275Smike	setsugid(p);
225084828Sjhb	crcopy(newcred, oldcred);
2251113630Sjhb	newcred->cr_prison = pr;
225284828Sjhb	p->p_ucred = newcred;
225384828Sjhb	PROC_UNLOCK(p);
225484828Sjhb	crfree(oldcred);
2255192895Sjamie	prison_deref(ppr, PD_DEREF | PD_DEUREF);
225646155Sphk	return (0);
2257191673Sjamie e_unlock:
2258175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2259191673Sjamie e_unlock_giant:
2260150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2261191673Sjamie e_revert_osd:
2262191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2263192895Sjamie	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2264191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
226546155Sphk	return (error);
226646155Sphk}
226746155Sphk
2268192895Sjamie
2269113275Smike/*
2270113275Smike * Returns a locked prison instance, or NULL on failure.
2271113275Smike */
2272168399Spjdstruct prison *
2273113275Smikeprison_find(int prid)
2274113275Smike{
2275113275Smike	struct prison *pr;
2276113275Smike
2277168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2278191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2279113275Smike		if (pr->pr_id == prid) {
2280113275Smike			mtx_lock(&pr->pr_mtx);
2281191673Sjamie			if (pr->pr_ref > 0)
2282191673Sjamie				return (pr);
2283191673Sjamie			mtx_unlock(&pr->pr_mtx);
2284113275Smike		}
2285113275Smike	}
2286113275Smike	return (NULL);
2287113275Smike}
2288113275Smike
2289191673Sjamie/*
2290192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2291191673Sjamie */
2292191673Sjamiestruct prison *
2293192895Sjamieprison_find_child(struct prison *mypr, int prid)
2294191673Sjamie{
2295192895Sjamie	struct prison *pr;
2296192895Sjamie	int descend;
2297192895Sjamie
2298192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2299192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2300192895Sjamie		if (pr->pr_id == prid) {
2301192895Sjamie			mtx_lock(&pr->pr_mtx);
2302192895Sjamie			if (pr->pr_ref > 0)
2303192895Sjamie				return (pr);
2304192895Sjamie			mtx_unlock(&pr->pr_mtx);
2305192895Sjamie		}
2306192895Sjamie	}
2307192895Sjamie	return (NULL);
2308192895Sjamie}
2309192895Sjamie
2310192895Sjamie/*
2311192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2312192895Sjamie */
2313192895Sjamiestruct prison *
2314192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2315192895Sjamie{
2316191673Sjamie	struct prison *pr, *deadpr;
2317192895Sjamie	size_t mylen;
2318192895Sjamie	int descend;
2319191673Sjamie
2320191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2321192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2322191673Sjamie again:
2323191673Sjamie	deadpr = NULL;
2324192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2325192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2326191673Sjamie			mtx_lock(&pr->pr_mtx);
2327191673Sjamie			if (pr->pr_ref > 0) {
2328191673Sjamie				if (pr->pr_uref > 0)
2329191673Sjamie					return (pr);
2330191673Sjamie				deadpr = pr;
2331191673Sjamie			}
2332191673Sjamie			mtx_unlock(&pr->pr_mtx);
2333191673Sjamie		}
2334191673Sjamie	}
2335192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2336191673Sjamie	if (deadpr != NULL) {
2337191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2338191673Sjamie		if (deadpr->pr_ref == 0) {
2339191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2340191673Sjamie			goto again;
2341191673Sjamie		}
2342191673Sjamie	}
2343191673Sjamie	return (deadpr);
2344191673Sjamie}
2345191673Sjamie
2346191673Sjamie/*
2347192895Sjamie * See if a prison has the specific flag set.
2348192895Sjamie */
2349192895Sjamieint
2350192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2351192895Sjamie{
2352192895Sjamie
2353192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2354192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2355192895Sjamie}
2356192895Sjamie
2357192895Sjamieint
2358192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2359192895Sjamie{
2360192895Sjamie
2361192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2362192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2363192895Sjamie}
2364192895Sjamie
2365192895Sjamie/*
2366191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2367191673Sjamie * prison itself - but not in this context in case there are locks held.
2368191673Sjamie */
236972786Srwatsonvoid
2370185029Spjdprison_free_locked(struct prison *pr)
237172786Srwatson{
237272786Srwatson
2373185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
237472786Srwatson	pr->pr_ref--;
237572786Srwatson	if (pr->pr_ref == 0) {
2376168483Spjd		mtx_unlock(&pr->pr_mtx);
2377124882Srwatson		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2378144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
237987275Srwatson		return;
238072786Srwatson	}
238187275Srwatson	mtx_unlock(&pr->pr_mtx);
238272786Srwatson}
238372786Srwatson
2384185029Spjdvoid
2385185029Spjdprison_free(struct prison *pr)
2386185029Spjd{
2387185029Spjd
2388185029Spjd	mtx_lock(&pr->pr_mtx);
2389185029Spjd	prison_free_locked(pr);
2390185029Spjd}
2391185029Spjd
2392124882Srwatsonstatic void
2393124882Srwatsonprison_complete(void *context, int pending)
2394124882Srwatson{
2395191673Sjamie
2396191673Sjamie	prison_deref((struct prison *)context, 0);
2397191673Sjamie}
2398191673Sjamie
2399191673Sjamie/*
2400191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2401191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2402191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2403191673Sjamie * and the allprison lock are both unlocked.
2404191673Sjamie */
2405191673Sjamiestatic void
2406191673Sjamieprison_deref(struct prison *pr, int flags)
2407191673Sjamie{
2408192895Sjamie	struct prison *ppr, *tpr;
2409150652Scsjp	int vfslocked;
2410124882Srwatson
2411191673Sjamie	if (!(flags & PD_LOCKED))
2412191673Sjamie		mtx_lock(&pr->pr_mtx);
2413192895Sjamie	/* Decrement the user references in a separate loop. */
2414191673Sjamie	if (flags & PD_DEUREF) {
2415192895Sjamie		for (tpr = pr;; tpr = tpr->pr_parent) {
2416192895Sjamie			if (tpr != pr)
2417192895Sjamie				mtx_lock(&tpr->pr_mtx);
2418192895Sjamie			if (--tpr->pr_uref > 0)
2419192895Sjamie				break;
2420192895Sjamie			KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2421192895Sjamie			mtx_unlock(&tpr->pr_mtx);
2422192895Sjamie		}
2423191673Sjamie		/* Done if there were only user references to remove. */
2424191673Sjamie		if (!(flags & PD_DEREF)) {
2425192895Sjamie			mtx_unlock(&tpr->pr_mtx);
2426191673Sjamie			if (flags & PD_LIST_SLOCKED)
2427191673Sjamie				sx_sunlock(&allprison_lock);
2428191673Sjamie			else if (flags & PD_LIST_XLOCKED)
2429191673Sjamie				sx_xunlock(&allprison_lock);
2430191673Sjamie			return;
2431191673Sjamie		}
2432192895Sjamie		if (tpr != pr) {
2433192895Sjamie			mtx_unlock(&tpr->pr_mtx);
2434192895Sjamie			mtx_lock(&pr->pr_mtx);
2435192895Sjamie		}
2436191673Sjamie	}
2437124882Srwatson
2438192895Sjamie	for (;;) {
2439192895Sjamie		if (flags & PD_DEREF)
2440192895Sjamie			pr->pr_ref--;
2441192895Sjamie		/* If the prison still has references, nothing else to do. */
2442192895Sjamie		if (pr->pr_ref > 0) {
2443192895Sjamie			mtx_unlock(&pr->pr_mtx);
2444192895Sjamie			if (flags & PD_LIST_SLOCKED)
2445192895Sjamie				sx_sunlock(&allprison_lock);
2446192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2447192895Sjamie				sx_xunlock(&allprison_lock);
2448192895Sjamie			return;
2449191673Sjamie		}
2450191673Sjamie
2451192895Sjamie		mtx_unlock(&pr->pr_mtx);
2452192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2453192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2454192895Sjamie				sx_sunlock(&allprison_lock);
2455192895Sjamie				sx_xlock(&allprison_lock);
2456192895Sjamie			}
2457192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2458192895Sjamie			sx_xlock(&allprison_lock);
2459168489Spjd
2460192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2461192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2462192895Sjamie		ppr = pr->pr_parent;
2463192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2464194762Sjamie			tpr->pr_childcount--;
2465192895Sjamie		sx_downgrade(&allprison_lock);
2466192895Sjamie
2467194251Sjamie#ifdef VIMAGE
2468194251Sjamie		if (pr->pr_flags & PR_VNET)
2469194251Sjamie			vnet_destroy(pr->pr_vnet);
2470194251Sjamie#endif
2471192895Sjamie		if (pr->pr_root != NULL) {
2472192895Sjamie			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2473192895Sjamie			vrele(pr->pr_root);
2474192895Sjamie			VFS_UNLOCK_GIANT(vfslocked);
2475192895Sjamie		}
2476192895Sjamie		mtx_destroy(&pr->pr_mtx);
2477191673Sjamie#ifdef INET
2478192895Sjamie		free(pr->pr_ip4, M_PRISON);
2479191673Sjamie#endif
2480185435Sbz#ifdef INET6
2481192895Sjamie		free(pr->pr_ip6, M_PRISON);
2482185435Sbz#endif
2483192895Sjamie		if (pr->pr_cpuset != NULL)
2484192895Sjamie			cpuset_rel(pr->pr_cpuset);
2485192895Sjamie		osd_jail_exit(pr);
2486192895Sjamie		free(pr, M_PRISON);
2487192895Sjamie
2488192895Sjamie		/* Removing a prison frees a reference on its parent. */
2489192895Sjamie		pr = ppr;
2490192895Sjamie		mtx_lock(&pr->pr_mtx);
2491192895Sjamie		flags = PD_DEREF | PD_LIST_SLOCKED;
2492192895Sjamie	}
2493124882Srwatson}
2494124882Srwatson
249572786Srwatsonvoid
2496185029Spjdprison_hold_locked(struct prison *pr)
249772786Srwatson{
249872786Srwatson
2499185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2500168489Spjd	KASSERT(pr->pr_ref > 0,
2501191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
250272786Srwatson	pr->pr_ref++;
2503185029Spjd}
2504185029Spjd
2505185029Spjdvoid
2506185029Spjdprison_hold(struct prison *pr)
2507185029Spjd{
2508185029Spjd
2509185029Spjd	mtx_lock(&pr->pr_mtx);
2510185029Spjd	prison_hold_locked(pr);
251187275Srwatson	mtx_unlock(&pr->pr_mtx);
251272786Srwatson}
251372786Srwatson
2514185435Sbzvoid
2515185435Sbzprison_proc_hold(struct prison *pr)
251687275Srwatson{
251787275Srwatson
2518185435Sbz	mtx_lock(&pr->pr_mtx);
2519191673Sjamie	KASSERT(pr->pr_uref > 0,
2520191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2521191673Sjamie	pr->pr_uref++;
2522185435Sbz	mtx_unlock(&pr->pr_mtx);
252387275Srwatson}
252487275Srwatson
2525185435Sbzvoid
2526185435Sbzprison_proc_free(struct prison *pr)
2527185435Sbz{
2528185435Sbz
2529185435Sbz	mtx_lock(&pr->pr_mtx);
2530191673Sjamie	KASSERT(pr->pr_uref > 0,
2531191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2532191673Sjamie	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2533185435Sbz}
2534185435Sbz
2535185435Sbz
2536185435Sbz#ifdef INET
2537185435Sbz/*
2538192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2539192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2540192895Sjamie */
2541192895Sjamiestatic int
2542192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2543192895Sjamie{
2544192895Sjamie	int ii, ij, used;
2545192895Sjamie	struct prison *ppr;
2546192895Sjamie
2547192895Sjamie	ppr = pr->pr_parent;
2548192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2549192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2550192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2551192895Sjamie			/*
2552192895Sjamie			 * There's no room for the parent's list.  Use the
2553192895Sjamie			 * new list buffer, which is assumed to be big enough
2554192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2555192895Sjamie			 * allocate one.
2556192895Sjamie			 */
2557192895Sjamie			used = 1;
2558192895Sjamie			if (newip4 == NULL) {
2559192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2560192895Sjamie				    M_PRISON, M_NOWAIT);
2561192895Sjamie				if (newip4 != NULL)
2562192895Sjamie					used = 0;
2563192895Sjamie			}
2564192895Sjamie			if (newip4 != NULL) {
2565192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2566192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2567192895Sjamie				free(pr->pr_ip4, M_PRISON);
2568192895Sjamie				pr->pr_ip4 = newip4;
2569192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2570192895Sjamie				pr->pr_flags |= PR_IP4;
2571192895Sjamie			}
2572192895Sjamie			return (used);
2573192895Sjamie		}
2574192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2575192895Sjamie		if (pr->pr_ip4s > 0)
2576192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2577192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2578192895Sjamie		else if (pr->pr_ip4 != NULL) {
2579192895Sjamie			free(pr->pr_ip4, M_PRISON);
2580192895Sjamie			pr->pr_ip4 = NULL;
2581192895Sjamie		}
2582192895Sjamie		pr->pr_flags =
2583192895Sjamie			(pr->pr_flags & ~PR_IP4) | (ppr->pr_flags & PR_IP4);
2584192895Sjamie	} else if (pr->pr_ip4s > 0 && (ppr->pr_flags & PR_IP4)) {
2585192895Sjamie		/* Remove addresses that aren't in the parent. */
2586192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2587192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2588192895Sjamie				break;
2589192895Sjamie		if (ij < ppr->pr_ip4s)
2590192895Sjamie			ii = 1;
2591192895Sjamie		else {
2592192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2593192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2594192895Sjamie			ii = 0;
2595192895Sjamie		}
2596192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2597192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2598192895Sjamie				ii++;
2599192895Sjamie				continue;
2600192895Sjamie			}
2601192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2602192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2603192895Sjamie			case -1:
2604192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2605192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2606192895Sjamie				break;
2607192895Sjamie			case 0:
2608192895Sjamie				ii++;
2609192895Sjamie				ij++;
2610192895Sjamie				break;
2611192895Sjamie			case 1:
2612192895Sjamie				ij++;
2613192895Sjamie				break;
2614192895Sjamie			}
2615192895Sjamie		}
2616192895Sjamie		if (pr->pr_ip4s == 0) {
2617192895Sjamie			free(pr->pr_ip4, M_PRISON);
2618192895Sjamie			pr->pr_ip4 = NULL;
2619192895Sjamie		}
2620192895Sjamie	}
2621192895Sjamie	return (0);
2622192895Sjamie}
2623192895Sjamie
2624192895Sjamie/*
2625185435Sbz * Pass back primary IPv4 address of this jail.
2626185435Sbz *
2627192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2628192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2629185435Sbz *
2630188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2631188144Sjamie * Address returned in NBO.
2632185435Sbz */
263346155Sphkint
2634187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
263546155Sphk{
2636191673Sjamie	struct prison *pr;
263746155Sphk
2638185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2639185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2640185435Sbz
2641192895Sjamie	pr = cred->cr_prison;
2642192895Sjamie	if (!(pr->pr_flags & PR_IP4))
264346155Sphk		return (0);
2644191673Sjamie	mtx_lock(&pr->pr_mtx);
2645192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2646192895Sjamie		mtx_unlock(&pr->pr_mtx);
2647192895Sjamie		return (0);
2648192895Sjamie	}
2649191673Sjamie	if (pr->pr_ip4 == NULL) {
2650191673Sjamie		mtx_unlock(&pr->pr_mtx);
2651188144Sjamie		return (EAFNOSUPPORT);
2652191673Sjamie	}
2653185435Sbz
2654191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2655191673Sjamie	mtx_unlock(&pr->pr_mtx);
2656185435Sbz	return (0);
2657185435Sbz}
2658185435Sbz
2659185435Sbz/*
2660192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2661192895Sjamie */
2662192895Sjamieint
2663192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2664192895Sjamie{
2665192895Sjamie
2666192895Sjamie	if (pr1 == pr2)
2667192895Sjamie		return (1);
2668192895Sjamie
2669192895Sjamie	/*
2670192895Sjamie	 * jail_set maintains an exclusive hold on allprison_lock while it
2671192895Sjamie	 * changes the IP addresses, so only a shared hold is needed.  This is
2672192895Sjamie	 * easier than locking the two prisons which would require finding the
2673192895Sjamie	 * proper locking order and end up needing allprison_lock anyway.
2674192895Sjamie	 */
2675192895Sjamie	sx_slock(&allprison_lock);
2676192895Sjamie	while (pr1 != &prison0 && !(pr1->pr_flags & PR_IP4_USER))
2677192895Sjamie		pr1 = pr1->pr_parent;
2678192895Sjamie	while (pr2 != &prison0 && !(pr2->pr_flags & PR_IP4_USER))
2679192895Sjamie		pr2 = pr2->pr_parent;
2680192895Sjamie	sx_sunlock(&allprison_lock);
2681192895Sjamie	return (pr1 == pr2);
2682192895Sjamie}
2683192895Sjamie
2684192895Sjamie/*
2685185435Sbz * Make sure our (source) address is set to something meaningful to this
2686185435Sbz * jail.
2687185435Sbz *
2688192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2689192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2690192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2691185435Sbz */
2692185435Sbzint
2693185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2694185435Sbz{
2695191673Sjamie	struct prison *pr;
2696185435Sbz	struct in_addr ia0;
2697191673Sjamie	int error;
2698185435Sbz
2699185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2700185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2701185435Sbz
2702192895Sjamie	pr = cred->cr_prison;
2703192895Sjamie	if (!(pr->pr_flags & PR_IP4))
270446155Sphk		return (0);
2705191673Sjamie	mtx_lock(&pr->pr_mtx);
2706192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2707192895Sjamie		mtx_unlock(&pr->pr_mtx);
2708192895Sjamie		return (0);
2709192895Sjamie	}
2710191673Sjamie	if (pr->pr_ip4 == NULL) {
2711191673Sjamie		mtx_unlock(&pr->pr_mtx);
2712188144Sjamie		return (EAFNOSUPPORT);
2713191673Sjamie	}
2714185435Sbz
2715185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
2716185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
2717191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2718191673Sjamie		mtx_unlock(&pr->pr_mtx);
2719185435Sbz		return (0);
272046155Sphk	}
2721185435Sbz
2722188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
2723188144Sjamie		/*
2724188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
2725188144Sjamie		 */
2726191673Sjamie		if (pr->pr_ip4s == 1)
2727191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
2728191673Sjamie		mtx_unlock(&pr->pr_mtx);
2729185435Sbz		return (0);
2730185435Sbz	}
2731185435Sbz
2732191673Sjamie	error = _prison_check_ip4(pr, ia);
2733191673Sjamie	mtx_unlock(&pr->pr_mtx);
2734191673Sjamie	return (error);
2735185435Sbz}
2736185435Sbz
2737185435Sbz/*
2738185435Sbz * Rewrite destination address in case we will connect to loopback address.
2739185435Sbz *
2740188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2741188144Sjamie * Address passed in in NBO and returned in NBO.
2742185435Sbz */
2743185435Sbzint
2744185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2745185435Sbz{
2746191673Sjamie	struct prison *pr;
2747185435Sbz
2748185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2749185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2750185435Sbz
2751192895Sjamie	pr = cred->cr_prison;
2752192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2753185435Sbz		return (0);
2754191673Sjamie	mtx_lock(&pr->pr_mtx);
2755192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2756192895Sjamie		mtx_unlock(&pr->pr_mtx);
2757192895Sjamie		return (0);
2758192895Sjamie	}
2759191673Sjamie	if (pr->pr_ip4 == NULL) {
2760191673Sjamie		mtx_unlock(&pr->pr_mtx);
2761188144Sjamie		return (EAFNOSUPPORT);
2762191673Sjamie	}
2763188144Sjamie
2764185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2765191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2766191673Sjamie		mtx_unlock(&pr->pr_mtx);
2767185435Sbz		return (0);
2768185435Sbz	}
2769185435Sbz
2770185435Sbz	/*
2771185435Sbz	 * Return success because nothing had to be changed.
2772185435Sbz	 */
2773191673Sjamie	mtx_unlock(&pr->pr_mtx);
2774185435Sbz	return (0);
2775185435Sbz}
2776185435Sbz
2777185435Sbz/*
2778188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
2779185435Sbz *
2780192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2781192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2782192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
2783185435Sbz */
2784185435Sbzstatic int
2785185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2786185435Sbz{
2787185435Sbz	int i, a, z, d;
2788185435Sbz
2789185435Sbz	/*
2790185435Sbz	 * Check the primary IP.
2791185435Sbz	 */
2792185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2793188144Sjamie		return (0);
2794185435Sbz
2795185435Sbz	/*
2796185435Sbz	 * All the other IPs are sorted so we can do a binary search.
2797185435Sbz	 */
2798185435Sbz	a = 0;
2799185435Sbz	z = pr->pr_ip4s - 2;
2800185435Sbz	while (a <= z) {
2801185435Sbz		i = (a + z) / 2;
2802185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2803185435Sbz		if (d > 0)
2804185435Sbz			z = i - 1;
2805185435Sbz		else if (d < 0)
2806185435Sbz			a = i + 1;
280781114Srwatson		else
2808188144Sjamie			return (0);
2809185435Sbz	}
2810188144Sjamie
2811188144Sjamie	return (EADDRNOTAVAIL);
2812185435Sbz}
2813185435Sbz
2814185435Sbzint
2815185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
2816185435Sbz{
2817191673Sjamie	struct prison *pr;
2818191673Sjamie	int error;
2819185435Sbz
2820185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2821185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2822185435Sbz
2823192895Sjamie	pr = cred->cr_prison;
2824192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2825188144Sjamie		return (0);
2826191673Sjamie	mtx_lock(&pr->pr_mtx);
2827192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2828192895Sjamie		mtx_unlock(&pr->pr_mtx);
2829192895Sjamie		return (0);
2830192895Sjamie	}
2831191673Sjamie	if (pr->pr_ip4 == NULL) {
2832191673Sjamie		mtx_unlock(&pr->pr_mtx);
2833188144Sjamie		return (EAFNOSUPPORT);
2834191673Sjamie	}
2835185435Sbz
2836191673Sjamie	error = _prison_check_ip4(pr, ia);
2837191673Sjamie	mtx_unlock(&pr->pr_mtx);
2838191673Sjamie	return (error);
2839185435Sbz}
2840185435Sbz#endif
2841185435Sbz
2842185435Sbz#ifdef INET6
2843192895Sjamiestatic int
2844192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2845192895Sjamie{
2846192895Sjamie	int ii, ij, used;
2847192895Sjamie	struct prison *ppr;
2848192895Sjamie
2849192895Sjamie	ppr = pr->pr_parent;
2850192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
2851192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2852192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
2853192895Sjamie			/*
2854192895Sjamie			 * There's no room for the parent's list.  Use the
2855192895Sjamie			 * new list buffer, which is assumed to be big enough
2856192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2857192895Sjamie			 * allocate one.
2858192895Sjamie			 */
2859192895Sjamie			used = 1;
2860192895Sjamie			if (newip6 == NULL) {
2861192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2862192895Sjamie				    M_PRISON, M_NOWAIT);
2863192895Sjamie				if (newip6 != NULL)
2864192895Sjamie					used = 0;
2865192895Sjamie			}
2866192895Sjamie			if (newip6 != NULL) {
2867192895Sjamie				bcopy(ppr->pr_ip6, newip6,
2868192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
2869192895Sjamie				free(pr->pr_ip6, M_PRISON);
2870192895Sjamie				pr->pr_ip6 = newip6;
2871192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
2872192895Sjamie				pr->pr_flags |= PR_IP6;
2873192895Sjamie			}
2874192895Sjamie			return (used);
2875192895Sjamie		}
2876192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
2877192895Sjamie		if (pr->pr_ip6s > 0)
2878192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
2879192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
2880192895Sjamie		else if (pr->pr_ip6 != NULL) {
2881192895Sjamie			free(pr->pr_ip6, M_PRISON);
2882192895Sjamie			pr->pr_ip6 = NULL;
2883192895Sjamie		}
2884192895Sjamie		pr->pr_flags =
2885192895Sjamie			(pr->pr_flags & ~PR_IP6) | (ppr->pr_flags & PR_IP6);
2886192895Sjamie	} else if (pr->pr_ip6s > 0 && (ppr->pr_flags & PR_IP6)) {
2887192895Sjamie		/* Remove addresses that aren't in the parent. */
2888192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
2889192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2890192895Sjamie			    &ppr->pr_ip6[ij]))
2891192895Sjamie				break;
2892192895Sjamie		if (ij < ppr->pr_ip6s)
2893192895Sjamie			ii = 1;
2894192895Sjamie		else {
2895192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2896192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2897192895Sjamie			ii = 0;
2898192895Sjamie		}
2899192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
2900192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2901192895Sjamie			    &ppr->pr_ip6[0])) {
2902192895Sjamie				ii++;
2903192895Sjamie				continue;
2904192895Sjamie			}
2905192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2906192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2907192895Sjamie			case -1:
2908192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2909192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2910192895Sjamie				break;
2911192895Sjamie			case 0:
2912192895Sjamie				ii++;
2913192895Sjamie				ij++;
2914192895Sjamie				break;
2915192895Sjamie			case 1:
2916192895Sjamie				ij++;
2917192895Sjamie				break;
2918192895Sjamie			}
2919192895Sjamie		}
2920192895Sjamie		if (pr->pr_ip6s == 0) {
2921192895Sjamie			free(pr->pr_ip6, M_PRISON);
2922192895Sjamie			pr->pr_ip6 = NULL;
2923192895Sjamie		}
2924192895Sjamie	}
2925192895Sjamie	return 0;
2926192895Sjamie}
2927192895Sjamie
2928185435Sbz/*
2929185435Sbz * Pass back primary IPv6 address for this jail.
2930185435Sbz *
2931192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2932192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2933185435Sbz *
2934188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2935185435Sbz */
2936185435Sbzint
2937187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2938185435Sbz{
2939191673Sjamie	struct prison *pr;
2940185435Sbz
2941185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2942185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2943185435Sbz
2944192895Sjamie	pr = cred->cr_prison;
2945192895Sjamie	if (!(pr->pr_flags & PR_IP6))
294681114Srwatson		return (0);
2947191673Sjamie	mtx_lock(&pr->pr_mtx);
2948192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
2949192895Sjamie		mtx_unlock(&pr->pr_mtx);
2950192895Sjamie		return (0);
2951192895Sjamie	}
2952191673Sjamie	if (pr->pr_ip6 == NULL) {
2953191673Sjamie		mtx_unlock(&pr->pr_mtx);
2954188144Sjamie		return (EAFNOSUPPORT);
2955191673Sjamie	}
2956188144Sjamie
2957191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2958191673Sjamie	mtx_unlock(&pr->pr_mtx);
2959185435Sbz	return (0);
2960185435Sbz}
2961185435Sbz
2962185435Sbz/*
2963192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
2964192895Sjamie */
2965192895Sjamieint
2966192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
2967192895Sjamie{
2968192895Sjamie
2969192895Sjamie	if (pr1 == pr2)
2970192895Sjamie		return (1);
2971192895Sjamie
2972192895Sjamie	sx_slock(&allprison_lock);
2973192895Sjamie	while (pr1 != &prison0 && !(pr1->pr_flags & PR_IP6_USER))
2974192895Sjamie		pr1 = pr1->pr_parent;
2975192895Sjamie	while (pr2 != &prison0 && !(pr2->pr_flags & PR_IP6_USER))
2976192895Sjamie		pr2 = pr2->pr_parent;
2977192895Sjamie	sx_sunlock(&allprison_lock);
2978192895Sjamie	return (pr1 == pr2);
2979192895Sjamie}
2980192895Sjamie
2981192895Sjamie/*
2982185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
2983185435Sbz *
2984185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2985185435Sbz * when needed while binding.
2986185435Sbz *
2987192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
2988192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2989192895Sjamie * doesn't allow IPv6.
2990185435Sbz */
2991185435Sbzint
2992185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
2993185435Sbz{
2994191673Sjamie	struct prison *pr;
2995191673Sjamie	int error;
2996185435Sbz
2997185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2998185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2999185435Sbz
3000192895Sjamie	pr = cred->cr_prison;
3001192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3002185435Sbz		return (0);
3003191673Sjamie	mtx_lock(&pr->pr_mtx);
3004192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3005192895Sjamie		mtx_unlock(&pr->pr_mtx);
3006192895Sjamie		return (0);
3007192895Sjamie	}
3008191673Sjamie	if (pr->pr_ip6 == NULL) {
3009191673Sjamie		mtx_unlock(&pr->pr_mtx);
3010188144Sjamie		return (EAFNOSUPPORT);
3011191673Sjamie	}
3012188144Sjamie
3013185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3014191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3015191673Sjamie		mtx_unlock(&pr->pr_mtx);
3016185435Sbz		return (0);
301781114Srwatson	}
3018185435Sbz
3019188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3020188144Sjamie		/*
3021188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3022188144Sjamie		 * then bind directly.
3023188144Sjamie		 */
3024191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3025191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3026191673Sjamie		mtx_unlock(&pr->pr_mtx);
3027185435Sbz		return (0);
3028185435Sbz	}
3029188144Sjamie
3030191673Sjamie	error = _prison_check_ip6(pr, ia6);
3031191673Sjamie	mtx_unlock(&pr->pr_mtx);
3032191673Sjamie	return (error);
3033185435Sbz}
3034185435Sbz
3035185435Sbz/*
3036185435Sbz * Rewrite destination address in case we will connect to loopback address.
3037185435Sbz *
3038188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3039185435Sbz */
3040185435Sbzint
3041185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3042185435Sbz{
3043191673Sjamie	struct prison *pr;
3044185435Sbz
3045185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3046185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3047185435Sbz
3048192895Sjamie	pr = cred->cr_prison;
3049192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3050185435Sbz		return (0);
3051191673Sjamie	mtx_lock(&pr->pr_mtx);
3052192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3053192895Sjamie		mtx_unlock(&pr->pr_mtx);
3054192895Sjamie		return (0);
3055192895Sjamie	}
3056191673Sjamie	if (pr->pr_ip6 == NULL) {
3057191673Sjamie		mtx_unlock(&pr->pr_mtx);
3058188144Sjamie		return (EAFNOSUPPORT);
3059191673Sjamie	}
3060188144Sjamie
3061185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3062191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3063191673Sjamie		mtx_unlock(&pr->pr_mtx);
3064185435Sbz		return (0);
3065185435Sbz	}
3066185435Sbz
3067185435Sbz	/*
3068185435Sbz	 * Return success because nothing had to be changed.
3069185435Sbz	 */
3070191673Sjamie	mtx_unlock(&pr->pr_mtx);
307146155Sphk	return (0);
307246155Sphk}
307346155Sphk
3074185435Sbz/*
3075188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3076185435Sbz *
3077192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3078192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3079192895Sjamie * doesn't allow IPv6.
3080185435Sbz */
3081185435Sbzstatic int
3082185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
308346155Sphk{
3084185435Sbz	int i, a, z, d;
308546155Sphk
3086185435Sbz	/*
3087185435Sbz	 * Check the primary IP.
3088185435Sbz	 */
3089185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3090188144Sjamie		return (0);
3091185435Sbz
3092185435Sbz	/*
3093185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3094185435Sbz	 */
3095185435Sbz	a = 0;
3096185435Sbz	z = pr->pr_ip6s - 2;
3097185435Sbz	while (a <= z) {
3098185435Sbz		i = (a + z) / 2;
3099185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3100185435Sbz		if (d > 0)
3101185435Sbz			z = i - 1;
3102185435Sbz		else if (d < 0)
3103185435Sbz			a = i + 1;
310446155Sphk		else
3105188144Sjamie			return (0);
310646155Sphk	}
3107188144Sjamie
3108188144Sjamie	return (EADDRNOTAVAIL);
310946155Sphk}
311046155Sphk
311146155Sphkint
3112185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3113185435Sbz{
3114191673Sjamie	struct prison *pr;
3115191673Sjamie	int error;
3116185435Sbz
3117185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3118185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3119185435Sbz
3120192895Sjamie	pr = cred->cr_prison;
3121192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3122188144Sjamie		return (0);
3123191673Sjamie	mtx_lock(&pr->pr_mtx);
3124192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3125192895Sjamie		mtx_unlock(&pr->pr_mtx);
3126192895Sjamie		return (0);
3127192895Sjamie	}
3128191673Sjamie	if (pr->pr_ip6 == NULL) {
3129191673Sjamie		mtx_unlock(&pr->pr_mtx);
3130188144Sjamie		return (EAFNOSUPPORT);
3131191673Sjamie	}
3132185435Sbz
3133191673Sjamie	error = _prison_check_ip6(pr, ia6);
3134191673Sjamie	mtx_unlock(&pr->pr_mtx);
3135191673Sjamie	return (error);
3136185435Sbz}
3137185435Sbz#endif
3138185435Sbz
3139185435Sbz/*
3140188146Sjamie * Check if a jail supports the given address family.
3141188146Sjamie *
3142188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3143188146Sjamie * if not.
3144188146Sjamie */
3145188146Sjamieint
3146188146Sjamieprison_check_af(struct ucred *cred, int af)
3147188146Sjamie{
3148192895Sjamie	struct prison *pr;
3149188146Sjamie	int error;
3150188146Sjamie
3151188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3152188146Sjamie
3153192895Sjamie	pr = cred->cr_prison;
3154194915Sjamie	/* Prisons with their own network stack are not limited. */
3155194915Sjamie	if (pr->pr_flags & PR_VNET)
3156194915Sjamie		return (0);
3157194915Sjamie
3158188146Sjamie	error = 0;
3159188146Sjamie	switch (af)
3160188146Sjamie	{
3161188146Sjamie#ifdef INET
3162188146Sjamie	case AF_INET:
3163192895Sjamie		if (pr->pr_flags & PR_IP4)
3164192895Sjamie		{
3165192895Sjamie			mtx_lock(&pr->pr_mtx);
3166192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3167192895Sjamie				error = EAFNOSUPPORT;
3168192895Sjamie			mtx_unlock(&pr->pr_mtx);
3169192895Sjamie		}
3170188146Sjamie		break;
3171188146Sjamie#endif
3172188146Sjamie#ifdef INET6
3173188146Sjamie	case AF_INET6:
3174192895Sjamie		if (pr->pr_flags & PR_IP6)
3175192895Sjamie		{
3176192895Sjamie			mtx_lock(&pr->pr_mtx);
3177192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3178192895Sjamie				error = EAFNOSUPPORT;
3179192895Sjamie			mtx_unlock(&pr->pr_mtx);
3180192895Sjamie		}
3181188146Sjamie		break;
3182188146Sjamie#endif
3183188146Sjamie	case AF_LOCAL:
3184188146Sjamie	case AF_ROUTE:
3185188146Sjamie		break;
3186188146Sjamie	default:
3187192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3188188146Sjamie			error = EAFNOSUPPORT;
3189188146Sjamie	}
3190188146Sjamie	return (error);
3191188146Sjamie}
3192188146Sjamie
3193188146Sjamie/*
3194185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3195185435Sbz * prison_check_ip[46]).
3196185435Sbz *
3197192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3198192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3199192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3200185435Sbz */
3201185435Sbzint
320272786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
320346155Sphk{
3204185435Sbz#ifdef INET
3205114168Smike	struct sockaddr_in *sai;
3206185435Sbz#endif
3207185435Sbz#ifdef INET6
3208185435Sbz	struct sockaddr_in6 *sai6;
3209185435Sbz#endif
3210188144Sjamie	int error;
321146155Sphk
3212185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3213185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3214185435Sbz
3215188144Sjamie	error = 0;
3216188144Sjamie	switch (sa->sa_family)
3217185435Sbz	{
3218185435Sbz#ifdef INET
3219185435Sbz	case AF_INET:
3220185435Sbz		sai = (struct sockaddr_in *)sa;
3221188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3222185435Sbz		break;
3223185435Sbz#endif
3224185435Sbz#ifdef INET6
3225185435Sbz	case AF_INET6:
3226185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3227188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3228185435Sbz		break;
3229185435Sbz#endif
3230185435Sbz	default:
3231192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3232188144Sjamie			error = EAFNOSUPPORT;
3233185435Sbz	}
3234188144Sjamie	return (error);
323546155Sphk}
323672786Srwatson
323772786Srwatson/*
323872786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
323972786Srwatson */
324072786Srwatsonint
3241114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
324272786Srwatson{
324372786Srwatson
3244191915Szec#ifdef VIMAGE
3245191915Szec	if (cred2->cr_vimage->v_procg != cred1->cr_vimage->v_procg)
3246191915Szec		return (ESRCH);
3247191915Szec#endif
3248192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3249192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3250192895Sjamie}
325172786Srwatson
3252192895Sjamie/*
3253192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3254192895Sjamie */
3255192895Sjamieint
3256192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3257192895Sjamie{
3258192895Sjamie
3259192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3260192895Sjamie		if (pr1 == pr2)
3261192895Sjamie			return (1);
326272786Srwatson	return (0);
326372786Srwatson}
326472786Srwatson
326572786Srwatson/*
326672786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
326772786Srwatson */
326872786Srwatsonint
3269114168Smikejailed(struct ucred *cred)
327072786Srwatson{
327172786Srwatson
3272192895Sjamie	return (cred->cr_prison != &prison0);
327372786Srwatson}
327491384Srobert
327591384Srobert/*
3276194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
327791384Srobert */
327891391Srobertvoid
3279114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
328091384Srobert{
3281193066Sjamie	struct prison *pr;
328291384Srobert
3283194090Sjamie	/*
3284194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3285194090Sjamie	 * system's hostname.
3286194090Sjamie	 */
3287193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3288193066Sjamie	mtx_lock(&pr->pr_mtx);
3289194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3290193066Sjamie	mtx_unlock(&pr->pr_mtx);
329191384Srobert}
3292113275Smike
3293194090Sjamievoid
3294194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3295194090Sjamie{
3296194090Sjamie
3297194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3298194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3299194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3300194090Sjamie}
3301194090Sjamie
3302194090Sjamievoid
3303194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3304194090Sjamie{
3305194090Sjamie
3306194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3307194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3308194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3309194090Sjamie}
3310194090Sjamie
3311194090Sjamievoid
3312194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3313194090Sjamie{
3314194090Sjamie
3315194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3316194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3317194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3318194090Sjamie}
3319194090Sjamie
3320125804Srwatson/*
3321147185Spjd * Determine whether the subject represented by cred can "see"
3322147185Spjd * status of a mount point.
3323147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3324147185Spjd * XXX: This function should be called cr_canseemount() and should be
3325147185Spjd *      placed in kern_prot.c.
3326125804Srwatson */
3327125804Srwatsonint
3328147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3329125804Srwatson{
3330147185Spjd	struct prison *pr;
3331147185Spjd	struct statfs *sp;
3332147185Spjd	size_t len;
3333125804Srwatson
3334192895Sjamie	pr = cred->cr_prison;
3335192895Sjamie	if (pr->pr_enforce_statfs == 0)
3336147185Spjd		return (0);
3337147185Spjd	if (pr->pr_root->v_mount == mp)
3338147185Spjd		return (0);
3339192895Sjamie	if (pr->pr_enforce_statfs == 2)
3340147185Spjd		return (ENOENT);
3341147185Spjd	/*
3342147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3343147185Spjd	 * all mount-points from inside a jail.
3344147185Spjd	 * This is ugly check, but this is the only situation when jail's
3345147185Spjd	 * directory ends with '/'.
3346147185Spjd	 */
3347147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3348147185Spjd		return (0);
3349147185Spjd	len = strlen(pr->pr_path);
3350147185Spjd	sp = &mp->mnt_stat;
3351147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3352147185Spjd		return (ENOENT);
3353147185Spjd	/*
3354147185Spjd	 * Be sure that we don't have situation where jail's root directory
3355147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3356147185Spjd	 */
3357147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3358147185Spjd		return (ENOENT);
3359147185Spjd	return (0);
3360147185Spjd}
3361147185Spjd
3362147185Spjdvoid
3363147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3364147185Spjd{
3365147185Spjd	char jpath[MAXPATHLEN];
3366147185Spjd	struct prison *pr;
3367147185Spjd	size_t len;
3368147185Spjd
3369192895Sjamie	pr = cred->cr_prison;
3370192895Sjamie	if (pr->pr_enforce_statfs == 0)
3371147185Spjd		return;
3372147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3373147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3374147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3375147185Spjd		    sizeof(sp->f_mntonname));
3376147185Spjd		return;
3377125804Srwatson	}
3378147185Spjd	if (pr->pr_root->v_mount == mp) {
3379147185Spjd		/*
3380147185Spjd		 * Clear current buffer data, so we are sure nothing from
3381147185Spjd		 * the valid path left there.
3382147185Spjd		 */
3383147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3384147185Spjd		*sp->f_mntonname = '/';
3385147185Spjd		return;
3386147185Spjd	}
3387147185Spjd	/*
3388147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3389147185Spjd	 * all mount-points from inside a jail.
3390147185Spjd	 */
3391147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3392147185Spjd		return;
3393147185Spjd	len = strlen(pr->pr_path);
3394147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3395147185Spjd	/*
3396147185Spjd	 * Clear current buffer data, so we are sure nothing from
3397147185Spjd	 * the valid path left there.
3398147185Spjd	 */
3399147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3400147185Spjd	if (*jpath == '\0') {
3401147185Spjd		/* Should never happen. */
3402147185Spjd		*sp->f_mntonname = '/';
3403147185Spjd	} else {
3404147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3405147185Spjd	}
3406125804Srwatson}
3407125804Srwatson
3408164032Srwatson/*
3409164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3410164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3411164032Srwatson */
3412164032Srwatsonint
3413164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3414164032Srwatson{
3415164032Srwatson
3416164032Srwatson	if (!jailed(cred))
3417164032Srwatson		return (0);
3418164032Srwatson
3419194915Sjamie#ifdef VIMAGE
3420194915Sjamie	/*
3421194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3422194915Sjamie	 * There might be a duplicate entry here in case the privilege
3423194915Sjamie	 * is only granted conditionally in the legacy jail case.
3424194915Sjamie	 */
3425164032Srwatson	switch (priv) {
3426194915Sjamie#ifdef notyet
3427194915Sjamie		/*
3428194915Sjamie		 * NFS-specific privileges.
3429194915Sjamie		 */
3430194915Sjamie	case PRIV_NFS_DAEMON:
3431194915Sjamie	case PRIV_NFS_LOCKD:
3432194915Sjamie#endif
3433194915Sjamie		/*
3434194915Sjamie		 * Network stack privileges.
3435194915Sjamie		 */
3436194915Sjamie	case PRIV_NET_BRIDGE:
3437194915Sjamie	case PRIV_NET_GRE:
3438194915Sjamie	case PRIV_NET_BPF:
3439194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3440194915Sjamie	case PRIV_NET_ROUTE:
3441194915Sjamie	case PRIV_NET_TAP:
3442194915Sjamie	case PRIV_NET_SETIFMTU:
3443194915Sjamie	case PRIV_NET_SETIFFLAGS:
3444194915Sjamie	case PRIV_NET_SETIFCAP:
3445194915Sjamie	case PRIV_NET_SETIFNAME	:
3446194915Sjamie	case PRIV_NET_SETIFMETRIC:
3447194915Sjamie	case PRIV_NET_SETIFPHYS:
3448194915Sjamie	case PRIV_NET_SETIFMAC:
3449194915Sjamie	case PRIV_NET_ADDMULTI:
3450194915Sjamie	case PRIV_NET_DELMULTI:
3451194915Sjamie	case PRIV_NET_HWIOCTL:
3452194915Sjamie	case PRIV_NET_SETLLADDR:
3453194915Sjamie	case PRIV_NET_ADDIFGROUP:
3454194915Sjamie	case PRIV_NET_DELIFGROUP:
3455194915Sjamie	case PRIV_NET_IFCREATE:
3456194915Sjamie	case PRIV_NET_IFDESTROY:
3457194915Sjamie	case PRIV_NET_ADDIFADDR:
3458194915Sjamie	case PRIV_NET_DELIFADDR:
3459194915Sjamie	case PRIV_NET_LAGG:
3460194915Sjamie	case PRIV_NET_GIF:
3461194915Sjamie	case PRIV_NET_SETIFVNET:
3462164032Srwatson
3463164032Srwatson		/*
3464194915Sjamie		 * 802.11-related privileges.
3465194915Sjamie		 */
3466194915Sjamie	case PRIV_NET80211_GETKEY:
3467194915Sjamie#ifdef notyet
3468194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3469194915Sjamie#endif
3470194915Sjamie
3471194915Sjamie#ifdef notyet
3472194915Sjamie		/*
3473194915Sjamie		 * AppleTalk privileges.
3474194915Sjamie		 */
3475194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3476194915Sjamie
3477194915Sjamie		/*
3478194915Sjamie		 * ATM privileges.
3479194915Sjamie		 */
3480194915Sjamie	case PRIV_NETATM_CFG:
3481194915Sjamie	case PRIV_NETATM_ADD:
3482194915Sjamie	case PRIV_NETATM_DEL:
3483194915Sjamie	case PRIV_NETATM_SET:
3484194915Sjamie
3485194915Sjamie		/*
3486194915Sjamie		 * Bluetooth privileges.
3487194915Sjamie		 */
3488194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3489194915Sjamie#endif
3490194915Sjamie
3491194915Sjamie		/*
3492194915Sjamie		 * Netgraph and netgraph module privileges.
3493194915Sjamie		 */
3494194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3495194915Sjamie#ifdef notyet
3496194915Sjamie	case PRIV_NETGRAPH_TTY:
3497194915Sjamie#endif
3498194915Sjamie
3499194915Sjamie		/*
3500194915Sjamie		 * IPv4 and IPv6 privileges.
3501194915Sjamie		 */
3502194915Sjamie	case PRIV_NETINET_IPFW:
3503194915Sjamie	case PRIV_NETINET_DIVERT:
3504194915Sjamie	case PRIV_NETINET_PF:
3505194915Sjamie	case PRIV_NETINET_DUMMYNET:
3506194915Sjamie	case PRIV_NETINET_CARP:
3507194915Sjamie	case PRIV_NETINET_MROUTE:
3508194915Sjamie	case PRIV_NETINET_RAW:
3509194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3510194915Sjamie	case PRIV_NETINET_ND6:
3511194915Sjamie	case PRIV_NETINET_SCOPE6:
3512194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3513194915Sjamie	case PRIV_NETINET_IPSEC:
3514194915Sjamie	case PRIV_NETINET_BINDANY:
3515194915Sjamie
3516194915Sjamie#ifdef notyet
3517194915Sjamie		/*
3518194915Sjamie		 * IPX/SPX privileges.
3519194915Sjamie		 */
3520194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3521194915Sjamie	case PRIV_NETIPX_RAW:
3522194915Sjamie
3523194915Sjamie		/*
3524194915Sjamie		 * NCP privileges.
3525194915Sjamie		 */
3526194915Sjamie	case PRIV_NETNCP:
3527194915Sjamie
3528194915Sjamie		/*
3529194915Sjamie		 * SMB privileges.
3530194915Sjamie		 */
3531194915Sjamie	case PRIV_NETSMB:
3532194915Sjamie#endif
3533194915Sjamie
3534194915Sjamie	/*
3535194915Sjamie	 * No default: or deny here.
3536194915Sjamie	 * In case of no permit fall through to next switch().
3537194915Sjamie	 */
3538194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3539194915Sjamie			return (0);
3540194915Sjamie	}
3541194915Sjamie#endif /* VIMAGE */
3542194915Sjamie
3543194915Sjamie	switch (priv) {
3544194915Sjamie
3545194915Sjamie		/*
3546164032Srwatson		 * Allow ktrace privileges for root in jail.
3547164032Srwatson		 */
3548164032Srwatson	case PRIV_KTRACE:
3549164032Srwatson
3550166827Srwatson#if 0
3551164032Srwatson		/*
3552164032Srwatson		 * Allow jailed processes to configure audit identity and
3553164032Srwatson		 * submit audit records (login, etc).  In the future we may
3554164032Srwatson		 * want to further refine the relationship between audit and
3555164032Srwatson		 * jail.
3556164032Srwatson		 */
3557164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3558164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3559164032Srwatson	case PRIV_AUDIT_SUBMIT:
3560166827Srwatson#endif
3561164032Srwatson
3562164032Srwatson		/*
3563164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3564164032Srwatson		 * credentials in any way they see fit.
3565164032Srwatson		 */
3566164032Srwatson	case PRIV_CRED_SETUID:
3567164032Srwatson	case PRIV_CRED_SETEUID:
3568164032Srwatson	case PRIV_CRED_SETGID:
3569164032Srwatson	case PRIV_CRED_SETEGID:
3570164032Srwatson	case PRIV_CRED_SETGROUPS:
3571164032Srwatson	case PRIV_CRED_SETREUID:
3572164032Srwatson	case PRIV_CRED_SETREGID:
3573164032Srwatson	case PRIV_CRED_SETRESUID:
3574164032Srwatson	case PRIV_CRED_SETRESGID:
3575164032Srwatson
3576164032Srwatson		/*
3577164032Srwatson		 * Jail implements visibility constraints already, so allow
3578164032Srwatson		 * jailed root to override uid/gid-based constraints.
3579164032Srwatson		 */
3580164032Srwatson	case PRIV_SEEOTHERGIDS:
3581164032Srwatson	case PRIV_SEEOTHERUIDS:
3582164032Srwatson
3583164032Srwatson		/*
3584164032Srwatson		 * Jail implements inter-process debugging limits already, so
3585164032Srwatson		 * allow jailed root various debugging privileges.
3586164032Srwatson		 */
3587164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3588164032Srwatson	case PRIV_DEBUG_SUGID:
3589164032Srwatson	case PRIV_DEBUG_UNPRIV:
3590164032Srwatson
3591164032Srwatson		/*
3592164032Srwatson		 * Allow jail to set various resource limits and login
3593164032Srwatson		 * properties, and for now, exceed process resource limits.
3594164032Srwatson		 */
3595164032Srwatson	case PRIV_PROC_LIMIT:
3596164032Srwatson	case PRIV_PROC_SETLOGIN:
3597164032Srwatson	case PRIV_PROC_SETRLIMIT:
3598164032Srwatson
3599164032Srwatson		/*
3600164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3601164032Srwatson		 */
3602164032Srwatson	case PRIV_IPC_READ:
3603164032Srwatson	case PRIV_IPC_WRITE:
3604164032Srwatson	case PRIV_IPC_ADMIN:
3605164032Srwatson	case PRIV_IPC_MSGSIZE:
3606164032Srwatson	case PRIV_MQ_ADMIN:
3607164032Srwatson
3608164032Srwatson		/*
3609192895Sjamie		 * Jail operations within a jail work on child jails.
3610192895Sjamie		 */
3611192895Sjamie	case PRIV_JAIL_ATTACH:
3612192895Sjamie	case PRIV_JAIL_SET:
3613192895Sjamie	case PRIV_JAIL_REMOVE:
3614192895Sjamie
3615192895Sjamie		/*
3616164032Srwatson		 * Jail implements its own inter-process limits, so allow
3617164032Srwatson		 * root processes in jail to change scheduling on other
3618164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3619164032Srwatson		 */
3620164032Srwatson	case PRIV_SCHED_DIFFCRED:
3621185435Sbz	case PRIV_SCHED_CPUSET:
3622164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3623164032Srwatson	case PRIV_SIGNAL_SUGID:
3624164032Srwatson
3625164032Srwatson		/*
3626164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3627164032Srwatson		 * writable.
3628164032Srwatson		 */
3629164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
3630164032Srwatson
3631164032Srwatson		/*
3632164032Srwatson		 * Allow root in jail to manage a variety of quota
3633166831Srwatson		 * properties.  These should likely be conditional on a
3634166831Srwatson		 * configuration option.
3635164032Srwatson		 */
3636166832Srwatson	case PRIV_VFS_GETQUOTA:
3637166832Srwatson	case PRIV_VFS_SETQUOTA:
3638164032Srwatson
3639164032Srwatson		/*
3640164032Srwatson		 * Since Jail relies on chroot() to implement file system
3641164032Srwatson		 * protections, grant many VFS privileges to root in jail.
3642164032Srwatson		 * Be careful to exclude mount-related and NFS-related
3643164032Srwatson		 * privileges.
3644164032Srwatson		 */
3645164032Srwatson	case PRIV_VFS_READ:
3646164032Srwatson	case PRIV_VFS_WRITE:
3647164032Srwatson	case PRIV_VFS_ADMIN:
3648164032Srwatson	case PRIV_VFS_EXEC:
3649164032Srwatson	case PRIV_VFS_LOOKUP:
3650164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3651164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
3652164032Srwatson	case PRIV_VFS_CHOWN:
3653164032Srwatson	case PRIV_VFS_CHROOT:
3654167152Spjd	case PRIV_VFS_RETAINSUGID:
3655164032Srwatson	case PRIV_VFS_FCHROOT:
3656164032Srwatson	case PRIV_VFS_LINK:
3657164032Srwatson	case PRIV_VFS_SETGID:
3658172860Srwatson	case PRIV_VFS_STAT:
3659164032Srwatson	case PRIV_VFS_STICKYFILE:
3660164032Srwatson		return (0);
3661164032Srwatson
3662164032Srwatson		/*
3663164032Srwatson		 * Depending on the global setting, allow privilege of
3664164032Srwatson		 * setting system flags.
3665164032Srwatson		 */
3666164032Srwatson	case PRIV_VFS_SYSFLAGS:
3667192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3668164032Srwatson			return (0);
3669164032Srwatson		else
3670164032Srwatson			return (EPERM);
3671164032Srwatson
3672164032Srwatson		/*
3673168396Spjd		 * Depending on the global setting, allow privilege of
3674168396Spjd		 * mounting/unmounting file systems.
3675168396Spjd		 */
3676168396Spjd	case PRIV_VFS_MOUNT:
3677168396Spjd	case PRIV_VFS_UNMOUNT:
3678168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
3679168699Spjd	case PRIV_VFS_MOUNT_OWNER:
3680192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3681168396Spjd			return (0);
3682168396Spjd		else
3683168396Spjd			return (EPERM);
3684168396Spjd
3685168396Spjd		/*
3686168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
3687168591Srwatson		 * ports.
3688164032Srwatson		 */
3689164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
3690168591Srwatson	case PRIV_NETINET_REUSEPORT:
3691164032Srwatson		return (0);
3692164032Srwatson
3693164032Srwatson		/*
3694175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
3695175630Sbz		 */
3696175630Sbz	case PRIV_NETINET_SETHDROPTS:
3697175630Sbz		return (0);
3698175630Sbz
3699175630Sbz		/*
3700164032Srwatson		 * Conditionally allow creating raw sockets in jail.
3701164032Srwatson		 */
3702164032Srwatson	case PRIV_NETINET_RAW:
3703192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3704164032Srwatson			return (0);
3705164032Srwatson		else
3706164032Srwatson			return (EPERM);
3707164032Srwatson
3708164032Srwatson		/*
3709164032Srwatson		 * Since jail implements its own visibility limits on netstat
3710164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
3711164032Srwatson		 * jail.
3712164032Srwatson		 */
3713164032Srwatson	case PRIV_NETINET_GETCRED:
3714164032Srwatson		return (0);
3715164032Srwatson
3716164032Srwatson	default:
3717164032Srwatson		/*
3718164032Srwatson		 * In all remaining cases, deny the privilege request.  This
3719164032Srwatson		 * includes almost all network privileges, many system
3720164032Srwatson		 * configuration privileges.
3721164032Srwatson		 */
3722164032Srwatson		return (EPERM);
3723164032Srwatson	}
3724164032Srwatson}
3725164032Srwatson
3726192895Sjamie/*
3727192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
3728192895Sjamie * if it does not directly follow.
3729192895Sjamie */
3730192895Sjamie
3731192895Sjamiechar *
3732192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
3733192895Sjamie{
3734192895Sjamie	char *name;
3735192895Sjamie
3736192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
3737192895Sjamie	if (pr1 == pr2)
3738192895Sjamie		return "0";
3739192895Sjamie	name = pr2->pr_name;
3740192895Sjamie	if (prison_ischild(pr1, pr2)) {
3741192895Sjamie		/*
3742192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
3743192895Sjamie		 * so its length can't be counted on.  But the number of dots
3744192895Sjamie		 * can be counted on - and counted.
3745192895Sjamie		 */
3746192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3747192895Sjamie			name = strchr(name, '.') + 1;
3748192895Sjamie	}
3749192895Sjamie	return (name);
3750192895Sjamie}
3751192895Sjamie
3752192895Sjamie/*
3753192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
3754192895Sjamie * if it does not directly follow.
3755192895Sjamie */
3756192895Sjamiestatic char *
3757192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
3758192895Sjamie{
3759192895Sjamie	char *path1, *path2;
3760192895Sjamie	int len1;
3761192895Sjamie
3762192895Sjamie	path1 = pr1->pr_path;
3763192895Sjamie	path2 = pr2->pr_path;
3764192895Sjamie	if (!strcmp(path1, "/"))
3765192895Sjamie		return (path2);
3766192895Sjamie	len1 = strlen(path1);
3767192895Sjamie	if (strncmp(path1, path2, len1))
3768192895Sjamie		return (path2);
3769192895Sjamie	if (path2[len1] == '\0')
3770192895Sjamie		return "/";
3771192895Sjamie	if (path2[len1] == '/')
3772192895Sjamie		return (path2 + len1);
3773192895Sjamie	return (path2);
3774192895Sjamie}
3775192895Sjamie
3776192895Sjamie
3777192895Sjamie/*
3778192895Sjamie * Jail-related sysctls.
3779192895Sjamie */
3780192895SjamieSYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3781192895Sjamie    "Jails");
3782192895Sjamie
3783113275Smikestatic int
3784113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
3785113275Smike{
3786191673Sjamie	struct xprison *xp;
3787192895Sjamie	struct prison *pr, *cpr;
3788191673Sjamie#ifdef INET
3789191673Sjamie	struct in_addr *ip4 = NULL;
3790191673Sjamie	int ip4s = 0;
3791191673Sjamie#endif
3792191673Sjamie#ifdef INET6
3793191673Sjamie	struct in_addr *ip6 = NULL;
3794191673Sjamie	int ip6s = 0;
3795191673Sjamie#endif
3796192895Sjamie	int descend, error;
3797113275Smike
3798191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3799192895Sjamie	pr = req->td->td_ucred->cr_prison;
3800191673Sjamie	error = 0;
3801168401Spjd	sx_slock(&allprison_lock);
3802192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3803192895Sjamie#if defined(INET) || defined(INET6)
3804191673Sjamie again:
3805192895Sjamie#endif
3806192895Sjamie		mtx_lock(&cpr->pr_mtx);
3807185435Sbz#ifdef INET
3808192895Sjamie		if (cpr->pr_ip4s > 0) {
3809192895Sjamie			if (ip4s < cpr->pr_ip4s) {
3810192895Sjamie				ip4s = cpr->pr_ip4s;
3811192895Sjamie				mtx_unlock(&cpr->pr_mtx);
3812191673Sjamie				ip4 = realloc(ip4, ip4s *
3813191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3814191673Sjamie				goto again;
3815191673Sjamie			}
3816192895Sjamie			bcopy(cpr->pr_ip4, ip4,
3817192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
3818191673Sjamie		}
3819185435Sbz#endif
3820185435Sbz#ifdef INET6
3821192895Sjamie		if (cpr->pr_ip6s > 0) {
3822192895Sjamie			if (ip6s < cpr->pr_ip6s) {
3823192895Sjamie				ip6s = cpr->pr_ip6s;
3824192895Sjamie				mtx_unlock(&cpr->pr_mtx);
3825191673Sjamie				ip6 = realloc(ip6, ip6s *
3826191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3827191673Sjamie				goto again;
3828191673Sjamie			}
3829192895Sjamie			bcopy(cpr->pr_ip6, ip6,
3830192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
3831191673Sjamie		}
3832185435Sbz#endif
3833192895Sjamie		if (cpr->pr_ref == 0) {
3834192895Sjamie			mtx_unlock(&cpr->pr_mtx);
3835191673Sjamie			continue;
3836191673Sjamie		}
3837191673Sjamie		bzero(xp, sizeof(*xp));
3838113275Smike		xp->pr_version = XPRISON_VERSION;
3839192895Sjamie		xp->pr_id = cpr->pr_id;
3840192895Sjamie		xp->pr_state = cpr->pr_uref > 0
3841191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3842192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3843194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3844192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3845185435Sbz#ifdef INET
3846192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
3847185435Sbz#endif
3848185435Sbz#ifdef INET6
3849192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
3850185435Sbz#endif
3851192895Sjamie		mtx_unlock(&cpr->pr_mtx);
3852191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3853191673Sjamie		if (error)
3854191673Sjamie			break;
3855185435Sbz#ifdef INET
3856191673Sjamie		if (xp->pr_ip4s > 0) {
3857191673Sjamie			error = SYSCTL_OUT(req, ip4,
3858191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
3859191673Sjamie			if (error)
3860191673Sjamie				break;
3861185435Sbz		}
3862185435Sbz#endif
3863185435Sbz#ifdef INET6
3864191673Sjamie		if (xp->pr_ip6s > 0) {
3865191673Sjamie			error = SYSCTL_OUT(req, ip6,
3866191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
3867191673Sjamie			if (error)
3868191673Sjamie				break;
3869185435Sbz		}
3870185435Sbz#endif
3871113275Smike	}
3872168401Spjd	sx_sunlock(&allprison_lock);
3873191673Sjamie	free(xp, M_TEMP);
3874191673Sjamie#ifdef INET
3875191673Sjamie	free(ip4, M_TEMP);
3876191673Sjamie#endif
3877191673Sjamie#ifdef INET6
3878191673Sjamie	free(ip6, M_TEMP);
3879191673Sjamie#endif
3880167354Spjd	return (error);
3881113275Smike}
3882113275Smike
3883187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
3884187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3885187864Sed    sysctl_jail_list, "S", "List of active jails");
3886126004Spjd
3887126004Spjdstatic int
3888126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3889126004Spjd{
3890126004Spjd	int error, injail;
3891126004Spjd
3892126004Spjd	injail = jailed(req->td->td_ucred);
3893126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3894126004Spjd
3895126004Spjd	return (error);
3896126004Spjd}
3897192895Sjamie
3898187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3899187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3900187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
3901185435Sbz
3902192895Sjamie#if defined(INET) || defined(INET6)
3903193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3904192895Sjamie    &jail_max_af_ips, 0,
3905192895Sjamie    "Number of IP addresses a jail may have at most per address family");
3906192895Sjamie#endif
3907192895Sjamie
3908192895Sjamie/*
3909192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
3910192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
3911192895Sjamie * just see their own parameters, and can't change them.
3912192895Sjamie */
3913192895Sjamiestatic int
3914192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3915192895Sjamie{
3916192895Sjamie	struct prison *pr;
3917192895Sjamie	int allow, error, i;
3918192895Sjamie
3919192895Sjamie	pr = req->td->td_ucred->cr_prison;
3920192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
3921192895Sjamie
3922192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
3923192895Sjamie	i = (allow & arg2) ? 1 : 0;
3924192895Sjamie	if (arg1 != NULL)
3925192895Sjamie		i = !i;
3926192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
3927192895Sjamie	if (error || !req->newptr)
3928192895Sjamie		return (error);
3929192895Sjamie	i = i ? arg2 : 0;
3930192895Sjamie	if (arg1 != NULL)
3931192895Sjamie		i ^= arg2;
3932192895Sjamie	/*
3933192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3934192895Sjamie	 * for writing.
3935192895Sjamie	 */
3936192895Sjamie	mtx_lock(&prison0.pr_mtx);
3937192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
3938192895Sjamie	mtx_unlock(&prison0.pr_mtx);
3939192895Sjamie	return (0);
3940192895Sjamie}
3941192895Sjamie
3942192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3943192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3944192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3945192895Sjamie    "Processes in jail can set their hostnames");
3946192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3947192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3948192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3949192895Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only");
3950192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3951192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3952192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3953192895Sjamie    "Processes in jail can use System V IPC primitives");
3954192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3955192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3956192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3957192895Sjamie    "Prison root can create raw sockets");
3958192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3959192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3960192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3961192895Sjamie    "Processes in jail can alter system file flags");
3962192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3963192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3964192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3965192895Sjamie    "Processes in jail can mount/unmount jail-friendly file systems");
3966192895Sjamie
3967192895Sjamiestatic int
3968192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3969192895Sjamie{
3970192895Sjamie	struct prison *pr;
3971192895Sjamie	int level, error;
3972192895Sjamie
3973192895Sjamie	pr = req->td->td_ucred->cr_prison;
3974192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
3975192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
3976192895Sjamie	if (error || !req->newptr)
3977192895Sjamie		return (error);
3978192895Sjamie	*(int *)arg1 = level;
3979192895Sjamie	return (0);
3980192895Sjamie}
3981192895Sjamie
3982192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3983192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3984192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
3985192895Sjamie    sysctl_jail_default_level, "I",
3986192895Sjamie    "Processes in jail cannot see all mounted file systems");
3987192895Sjamie
3988192895Sjamie/*
3989192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
3990192895Sjamie * is returned in the string itself, and the other parameters exist merely
3991192895Sjamie * to make themselves and their types known.
3992192895Sjamie */
3993192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
3994192895Sjamie    "Jail parameters");
3995192895Sjamie
3996192895Sjamieint
3997192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
3998192895Sjamie{
3999192895Sjamie	int i;
4000192895Sjamie	long l;
4001192895Sjamie	size_t s;
4002192895Sjamie	char numbuf[12];
4003192895Sjamie
4004192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4005192895Sjamie	{
4006192895Sjamie	case CTLTYPE_LONG:
4007192895Sjamie	case CTLTYPE_ULONG:
4008192895Sjamie		l = 0;
4009192895Sjamie#ifdef SCTL_MASK32
4010192895Sjamie		if (!(req->flags & SCTL_MASK32))
4011192895Sjamie#endif
4012192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4013192895Sjamie	case CTLTYPE_INT:
4014192895Sjamie	case CTLTYPE_UINT:
4015192895Sjamie		i = 0;
4016192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4017192895Sjamie	case CTLTYPE_STRING:
4018192895Sjamie		snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4019192895Sjamie		return
4020192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4021192895Sjamie	case CTLTYPE_STRUCT:
4022192895Sjamie		s = (size_t)arg2;
4023192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4024192895Sjamie	}
4025192895Sjamie	return (0);
4026192895Sjamie}
4027192895Sjamie
4028192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4029192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4030192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4031192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4032192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4033192895Sjamie    "I", "Jail secure level");
4034192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4035192895Sjamie    "I", "Jail cannot see all mounted file systems");
4036192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4037192895Sjamie    "B", "Jail persistence");
4038194251Sjamie#ifdef VIMAGE
4039194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4040194251Sjamie    "B", "Virtual network stack");
4041194251Sjamie#endif
4042192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4043192895Sjamie    "B", "Jail is in the process of shutting down");
4044192895Sjamie
4045194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4046194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4047194762Sjamie    "I", "Current number of child jails");
4048194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4049194762Sjamie    "I", "Maximum number of child jails");
4050194762Sjamie
4051192895SjamieSYSCTL_JAIL_PARAM_NODE(host, "Jail host info");
4052193066SjamieSYSCTL_JAIL_PARAM(, nohost, CTLTYPE_INT | CTLFLAG_RW,
4053193066Sjamie    "BN", "Jail w/ no host info");
4054192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4055192895Sjamie    "Jail hostname");
4056193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4057193066Sjamie    "Jail NIS domainname");
4058193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4059193066Sjamie    "Jail host UUID");
4060193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4061193066Sjamie    "LU", "Jail host ID");
4062192895Sjamie
4063192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4064192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4065192895Sjamie
4066192895Sjamie#ifdef INET
4067192895SjamieSYSCTL_JAIL_PARAM_NODE(ip4, "Jail IPv4 address virtualization");
4068192895SjamieSYSCTL_JAIL_PARAM(, noip4, CTLTYPE_INT | CTLFLAG_RW,
4069192895Sjamie    "BN", "Jail w/ no IP address virtualization");
4070192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4071192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4072192895Sjamie#endif
4073192895Sjamie#ifdef INET6
4074192895SjamieSYSCTL_JAIL_PARAM_NODE(ip6, "Jail IPv6 address virtualization");
4075192895SjamieSYSCTL_JAIL_PARAM(, noip6, CTLTYPE_INT | CTLFLAG_RW,
4076192895Sjamie    "BN", "Jail w/ no IP address virtualization");
4077192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4078192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4079192895Sjamie#endif
4080192895Sjamie
4081192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4082192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4083192895Sjamie    "B", "Jail may set hostname");
4084192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4085192895Sjamie    "B", "Jail may use SYSV IPC");
4086192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4087192895Sjamie    "B", "Jail may create raw sockets");
4088192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4089192895Sjamie    "B", "Jail may alter system file flags");
4090192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4091192895Sjamie    "B", "Jail may mount/unmount jail-friendly file systems");
4092192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4093192895Sjamie    "B", "Jail may set file quotas");
4094192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4095192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4096192895Sjamie
4097192895Sjamie
4098185435Sbz#ifdef DDB
4099191673Sjamie
4100191673Sjamiestatic void
4101191673Sjamiedb_show_prison(struct prison *pr)
4102185435Sbz{
4103192895Sjamie	int fi;
4104191673Sjamie#if defined(INET) || defined(INET6)
4105191673Sjamie	int ii;
4106185435Sbz#endif
4107185435Sbz#ifdef INET6
4108185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4109185435Sbz#endif
4110185435Sbz
4111191673Sjamie	db_printf("prison %p:\n", pr);
4112191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4113191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4114192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4115191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4116191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4117191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4118191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4119191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4120194251Sjamie#ifdef VIMAGE
4121194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4122194251Sjamie#endif
4123191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4124191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4125194762Sjamie	db_printf(" childcount      = %d\n", pr->pr_childcount);
4126192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4127192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4128191673Sjamie	db_printf(" flags           = %x", pr->pr_flags);
4129192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4130192895Sjamie	    fi++)
4131192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4132192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4133192895Sjamie	db_printf(" allow           = %x", pr->pr_allow);
4134192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4135192895Sjamie	    fi++)
4136192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4137192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4138191673Sjamie	db_printf("\n");
4139192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4140194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4141194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4142194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4143193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4144185435Sbz#ifdef INET
4145191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4146191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4147191673Sjamie		db_printf(" %s %s\n",
4148191673Sjamie		    ii == 0 ? "ip4             =" : "                 ",
4149191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4150185435Sbz#endif
4151185435Sbz#ifdef INET6
4152191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4153191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4154191673Sjamie		db_printf(" %s %s\n",
4155191673Sjamie		    ii == 0 ? "ip6             =" : "                 ",
4156191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4157191673Sjamie#endif
4158191673Sjamie}
4159191673Sjamie
4160191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4161191673Sjamie{
4162191673Sjamie	struct prison *pr;
4163191673Sjamie
4164191673Sjamie	if (!have_addr) {
4165192895Sjamie		/*
4166192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4167192895Sjamie		 * listed.
4168192895Sjamie		 */
4169192895Sjamie		db_show_prison(&prison0);
4170192895Sjamie		if (!db_pager_quit) {
4171192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4172192895Sjamie				db_show_prison(pr);
4173192895Sjamie				if (db_pager_quit)
4174192895Sjamie					break;
4175192895Sjamie			}
4176191673Sjamie		}
4177191673Sjamie		return;
4178191673Sjamie	}
4179191673Sjamie
4180192895Sjamie	if (addr == 0)
4181192895Sjamie		pr = &prison0;
4182192895Sjamie	else {
4183192895Sjamie		/* Look for a prison with the ID and with references. */
4184191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4185192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4186191673Sjamie				break;
4187192895Sjamie		if (pr == NULL)
4188192895Sjamie			/* Look again, without requiring a reference. */
4189192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4190192895Sjamie				if (pr->pr_id == addr)
4191192895Sjamie					break;
4192192895Sjamie		if (pr == NULL)
4193192895Sjamie			/* Assume address points to a valid prison. */
4194192895Sjamie			pr = (struct prison *)addr;
4195192895Sjamie	}
4196191673Sjamie	db_show_prison(pr);
4197185435Sbz}
4198191673Sjamie
4199185435Sbz#endif /* DDB */
4200