kern_jail.c revision 230407
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 230407 2012-01-21 00:06:21Z mm $");
31116182Sobrien
32193066Sjamie#include "opt_compat.h"
33185435Sbz#include "opt_ddb.h"
34185435Sbz#include "opt_inet.h"
35185435Sbz#include "opt_inet6.h"
36131177Spjd
3746155Sphk#include <sys/param.h>
3846155Sphk#include <sys/types.h>
3946155Sphk#include <sys/kernel.h>
4046155Sphk#include <sys/systm.h>
4146155Sphk#include <sys/errno.h>
4246155Sphk#include <sys/sysproto.h>
4346155Sphk#include <sys/malloc.h>
44192895Sjamie#include <sys/osd.h>
45164032Srwatson#include <sys/priv.h>
4646155Sphk#include <sys/proc.h>
47124882Srwatson#include <sys/taskqueue.h>
48177785Skib#include <sys/fcntl.h>
4946155Sphk#include <sys/jail.h>
5087275Srwatson#include <sys/lock.h>
5187275Srwatson#include <sys/mutex.h>
52220137Strasz#include <sys/racct.h>
53221362Strasz#include <sys/refcount.h>
54168401Spjd#include <sys/sx.h>
55193066Sjamie#include <sys/sysent.h>
56113275Smike#include <sys/namei.h>
57147185Spjd#include <sys/mount.h>
58113275Smike#include <sys/queue.h>
5946155Sphk#include <sys/socket.h>
60113275Smike#include <sys/syscallsubr.h>
6157163Srwatson#include <sys/sysctl.h>
62113275Smike#include <sys/vnode.h>
63196019Srwatson
6446155Sphk#include <net/if.h>
65196019Srwatson#include <net/vnet.h>
66196019Srwatson
6746155Sphk#include <netinet/in.h>
68196019Srwatson
69185435Sbz#ifdef DDB
70185435Sbz#include <ddb/ddb.h>
71185435Sbz#ifdef INET6
72185435Sbz#include <netinet6/in6_var.h>
73185435Sbz#endif /* INET6 */
74185435Sbz#endif /* DDB */
7546155Sphk
76163606Srwatson#include <security/mac/mac_framework.h>
77163606Srwatson
78195944Sjamie#define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
79195944Sjamie
8046155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
81227293Sedstatic MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8246155Sphk
83202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */
84202468Sbz#ifdef INET
85202468Sbz#ifdef INET6
86202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
87202468Sbz#else
88202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
89202468Sbz#endif
90202468Sbz#else /* !INET */
91202468Sbz#ifdef INET6
92202468Sbz#define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
93202468Sbz#else
94202468Sbz#define	_PR_IP_SADDRSEL	0
95202468Sbz#endif
96202468Sbz#endif
97202468Sbz
98192895Sjamie/* prison0 describes what is "real" about the system. */
99192895Sjamiestruct prison prison0 = {
100192895Sjamie	.pr_id		= 0,
101192895Sjamie	.pr_name	= "0",
102192895Sjamie	.pr_ref		= 1,
103192895Sjamie	.pr_uref	= 1,
104192895Sjamie	.pr_path	= "/",
105192895Sjamie	.pr_securelevel	= -1,
106194762Sjamie	.pr_childmax	= JAIL_MAX,
107195944Sjamie	.pr_hostuuid	= DEFAULT_HOSTUUID,
108201145Santoine	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
109196176Sbz#ifdef VIMAGE
110202468Sbz	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
111196176Sbz#else
112202468Sbz	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
113196176Sbz#endif
114192895Sjamie	.pr_allow	= PR_ALLOW_ALL,
115192895Sjamie};
116192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11757163Srwatson
118221362Strasz/* allprison, allprison_racct and lastprid are protected by allprison_lock. */
119168401Spjdstruct	sx allprison_lock;
120191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
121191673Sjamiestruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
122221362StraszLIST_HEAD(, prison_racct) allprison_racct;
123179881Sdelphijint	lastprid = 0;
124113275Smike
125191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr);
126190466Sjamiestatic void prison_complete(void *context, int pending);
127191673Sjamiestatic void prison_deref(struct prison *pr, int flags);
128192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2);
129192895Sjamiestatic void prison_remove_one(struct prison *pr);
130221362Strasz#ifdef RACCT
131221362Straszstatic void prison_racct_attach(struct prison *pr);
132221362Straszstatic void prison_racct_detach(struct prison *pr);
133221362Strasz#endif
134185435Sbz#ifdef INET
135190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
136192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
137185435Sbz#endif
138185435Sbz#ifdef INET6
139190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
140192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
141185435Sbz#endif
142113275Smike
143191673Sjamie/* Flags for prison_deref */
144191673Sjamie#define	PD_DEREF	0x01
145191673Sjamie#define	PD_DEUREF	0x02
146191673Sjamie#define	PD_LOCKED	0x04
147191673Sjamie#define	PD_LIST_SLOCKED	0x08
148191673Sjamie#define	PD_LIST_XLOCKED	0x10
149113275Smike
150192895Sjamie/*
151216861Sbz * Parameter names corresponding to PR_* flag values.  Size values are for kvm
152216861Sbz * as we cannot figure out the size of a sparse array, or an array without a
153216861Sbz * terminating entry.
154192895Sjamie */
155192895Sjamiestatic char *pr_flag_names[] = {
156192895Sjamie	[0] = "persist",
157202468Sbz#ifdef INET
158202468Sbz	[7] = "ip4.saddrsel",
159202468Sbz#endif
160202468Sbz#ifdef INET6
161202468Sbz	[8] = "ip6.saddrsel",
162202468Sbz#endif
163192895Sjamie};
164216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names);
165192895Sjamie
166192895Sjamiestatic char *pr_flag_nonames[] = {
167192895Sjamie	[0] = "nopersist",
168202468Sbz#ifdef INET
169202468Sbz	[7] = "ip4.nosaddrsel",
170202468Sbz#endif
171202468Sbz#ifdef INET6
172202468Sbz	[8] = "ip6.nosaddrsel",
173202468Sbz#endif
174195870Sjamie};
175216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
176195870Sjamie
177195870Sjamiestruct jailsys_flags {
178195870Sjamie	const char	*name;
179195870Sjamie	unsigned	 disable;
180195870Sjamie	unsigned	 new;
181195870Sjamie} pr_flag_jailsys[] = {
182195870Sjamie	{ "host", 0, PR_HOST },
183195870Sjamie#ifdef VIMAGE
184195870Sjamie	{ "vnet", 0, PR_VNET },
185195870Sjamie#endif
186192895Sjamie#ifdef INET
187195870Sjamie	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
188192895Sjamie#endif
189192895Sjamie#ifdef INET6
190195870Sjamie	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
191192895Sjamie#endif
192192895Sjamie};
193216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
194192895Sjamie
195192895Sjamiestatic char *pr_allow_names[] = {
196192895Sjamie	"allow.set_hostname",
197192895Sjamie	"allow.sysvipc",
198192895Sjamie	"allow.raw_sockets",
199192895Sjamie	"allow.chflags",
200192895Sjamie	"allow.mount",
201192895Sjamie	"allow.quotas",
202192895Sjamie	"allow.socket_af",
203192895Sjamie};
204216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names);
205192895Sjamie
206192895Sjamiestatic char *pr_allow_nonames[] = {
207192895Sjamie	"allow.noset_hostname",
208192895Sjamie	"allow.nosysvipc",
209192895Sjamie	"allow.noraw_sockets",
210192895Sjamie	"allow.nochflags",
211192895Sjamie	"allow.nomount",
212192895Sjamie	"allow.noquotas",
213192895Sjamie	"allow.nosocket_af",
214192895Sjamie};
215216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
216192895Sjamie
217196002Sjamie#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
218196002Sjamie#define	JAIL_DEFAULT_ENFORCE_STATFS	2
219192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
220196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
221192895Sjamie#if defined(INET) || defined(INET6)
222193865Sjamiestatic unsigned jail_max_af_ips = 255;
223192895Sjamie#endif
224192895Sjamie
225192895Sjamie#ifdef INET
226185435Sbzstatic int
227185435Sbzqcmp_v4(const void *ip1, const void *ip2)
228185435Sbz{
229185435Sbz	in_addr_t iaa, iab;
230185435Sbz
231185435Sbz	/*
232185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
233185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
234185435Sbz	 * interesting results.  If you do not understand, do not try.
235185435Sbz	 */
236185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
237185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
238185435Sbz
239185435Sbz	/*
240185435Sbz	 * Do not simply return the difference of the two numbers, the int is
241185435Sbz	 * not wide enough.
242185435Sbz	 */
243185435Sbz	if (iaa > iab)
244185435Sbz		return (1);
245185435Sbz	else if (iaa < iab)
246185435Sbz		return (-1);
247185435Sbz	else
248185435Sbz		return (0);
249185435Sbz}
250185435Sbz#endif
251185435Sbz
252185435Sbz#ifdef INET6
253185435Sbzstatic int
254185435Sbzqcmp_v6(const void *ip1, const void *ip2)
255185435Sbz{
256185435Sbz	const struct in6_addr *ia6a, *ia6b;
257185435Sbz	int i, rc;
258185435Sbz
259185435Sbz	ia6a = (const struct in6_addr *)ip1;
260185435Sbz	ia6b = (const struct in6_addr *)ip2;
261185435Sbz
262185435Sbz	rc = 0;
263190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
264185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
265185435Sbz			rc = 1;
266185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
267185435Sbz			rc = -1;
268185435Sbz	}
269185435Sbz	return (rc);
270185435Sbz}
271185435Sbz#endif
272185435Sbz
273191673Sjamie/*
274191673Sjamie * struct jail_args {
275191673Sjamie *	struct jail *jail;
276191673Sjamie * };
277191673Sjamie */
278191673Sjamieint
279225617Skmacysys_jail(struct thread *td, struct jail_args *uap)
280185435Sbz{
281191673Sjamie	uint32_t version;
282191673Sjamie	int error;
283192895Sjamie	struct jail j;
284185435Sbz
285191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
286191673Sjamie	if (error)
287191673Sjamie		return (error);
288185435Sbz
289191673Sjamie	switch (version) {
290191673Sjamie	case 0:
291191673Sjamie	{
292191673Sjamie		struct jail_v0 j0;
293185435Sbz
294192895Sjamie		/* FreeBSD single IPv4 jails. */
295192895Sjamie		bzero(&j, sizeof(struct jail));
296191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
297191673Sjamie		if (error)
298191673Sjamie			return (error);
299192895Sjamie		j.version = j0.version;
300192895Sjamie		j.path = j0.path;
301192895Sjamie		j.hostname = j0.hostname;
302192895Sjamie		j.ip4s = j0.ip_number;
303191673Sjamie		break;
304191673Sjamie	}
305191673Sjamie
306191673Sjamie	case 1:
307185435Sbz		/*
308191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
309191673Sjamie		 * that never made it into the official kernel.
310185435Sbz		 */
311191673Sjamie		return (EINVAL);
312185435Sbz
313191673Sjamie	case 2:	/* JAIL_API_VERSION */
314191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
315191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
316191673Sjamie		if (error)
317191673Sjamie			return (error);
318192895Sjamie		break;
319192895Sjamie
320192895Sjamie	default:
321192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
322192895Sjamie		return (EINVAL);
323192895Sjamie	}
324192895Sjamie	return (kern_jail(td, &j));
325192895Sjamie}
326192895Sjamie
327192895Sjamieint
328192895Sjamiekern_jail(struct thread *td, struct jail *j)
329192895Sjamie{
330193865Sjamie	struct iovec optiov[2 * (4
331193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
332193865Sjamie#ifdef INET
333193865Sjamie			    + 1
334193865Sjamie#endif
335193865Sjamie#ifdef INET6
336193865Sjamie			    + 1
337193865Sjamie#endif
338193865Sjamie			    )];
339192895Sjamie	struct uio opt;
340192895Sjamie	char *u_path, *u_hostname, *u_name;
341185435Sbz#ifdef INET
342193865Sjamie	uint32_t ip4s;
343192895Sjamie	struct in_addr *u_ip4;
344192895Sjamie#endif
345192895Sjamie#ifdef INET6
346192895Sjamie	struct in6_addr *u_ip6;
347192895Sjamie#endif
348192895Sjamie	size_t tmplen;
349192895Sjamie	int error, enforce_statfs, fi;
350192895Sjamie
351192895Sjamie	bzero(&optiov, sizeof(optiov));
352192895Sjamie	opt.uio_iov = optiov;
353192895Sjamie	opt.uio_iovcnt = 0;
354192895Sjamie	opt.uio_offset = -1;
355192895Sjamie	opt.uio_resid = -1;
356192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
357192895Sjamie	opt.uio_rw = UIO_READ;
358192895Sjamie	opt.uio_td = td;
359192895Sjamie
360192895Sjamie	/* Set permissions for top-level jails from sysctls. */
361192895Sjamie	if (!jailed(td->td_ucred)) {
362192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
363192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
364192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
365192895Sjamie			    (jail_default_allow & (1 << fi))
366192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
367192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
368192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
369192895Sjamie			opt.uio_iovcnt += 2;
370192895Sjamie		}
371192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
372192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
373192895Sjamie		opt.uio_iovcnt++;
374192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
375192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
376192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
377192895Sjamie		opt.uio_iovcnt++;
378192895Sjamie	}
379192895Sjamie
380192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
381192895Sjamie#ifdef INET
382192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
383192895Sjamie	if (ip4s > jail_max_af_ips)
384192895Sjamie		return (EINVAL);
385192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
386191673Sjamie#else
387192895Sjamie	if (j->ip4s > 0)
388192895Sjamie		return (EINVAL);
389191673Sjamie#endif
390191673Sjamie#ifdef INET6
391192895Sjamie	if (j->ip6s > jail_max_af_ips)
392192895Sjamie		return (EINVAL);
393192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
394191673Sjamie#else
395192895Sjamie	if (j->ip6s > 0)
396192895Sjamie		return (EINVAL);
397191673Sjamie#endif
398192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
399192895Sjamie	u_hostname = u_path + MAXPATHLEN;
400192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
401191673Sjamie#ifdef INET
402192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
403191673Sjamie#endif
404191673Sjamie#ifdef INET6
405191673Sjamie#ifdef INET
406192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
407191673Sjamie#else
408192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
409191673Sjamie#endif
410191673Sjamie#endif
411192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
412192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
413192895Sjamie	opt.uio_iovcnt++;
414192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
415192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
416192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
417192895Sjamie	if (error) {
418192895Sjamie		free(u_path, M_TEMP);
419192895Sjamie		return (error);
420192895Sjamie	}
421192895Sjamie	opt.uio_iovcnt++;
422192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
423192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
424192895Sjamie	opt.uio_iovcnt++;
425192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
426192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
427192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
428192895Sjamie	if (error) {
429192895Sjamie		free(u_path, M_TEMP);
430192895Sjamie		return (error);
431192895Sjamie	}
432192895Sjamie	opt.uio_iovcnt++;
433192895Sjamie	if (j->jailname != NULL) {
434192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
435192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
436192895Sjamie		opt.uio_iovcnt++;
437192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
438192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
439192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
440191673Sjamie		if (error) {
441191673Sjamie			free(u_path, M_TEMP);
442191673Sjamie			return (error);
443191673Sjamie		}
444192895Sjamie		opt.uio_iovcnt++;
445192895Sjamie	}
446191673Sjamie#ifdef INET
447192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
448192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
449192895Sjamie	opt.uio_iovcnt++;
450192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
451192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
452192895Sjamie	if (j->version == 0)
453192895Sjamie		u_ip4->s_addr = j->ip4s;
454192895Sjamie	else {
455192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
456191673Sjamie		if (error) {
457191673Sjamie			free(u_path, M_TEMP);
458191673Sjamie			return (error);
459191673Sjamie		}
460192895Sjamie	}
461192895Sjamie	opt.uio_iovcnt++;
462185435Sbz#endif
463185435Sbz#ifdef INET6
464192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
465192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
466192895Sjamie	opt.uio_iovcnt++;
467192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
468192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
469192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
470192895Sjamie	if (error) {
471192895Sjamie		free(u_path, M_TEMP);
472192895Sjamie		return (error);
473192895Sjamie	}
474192895Sjamie	opt.uio_iovcnt++;
475185435Sbz#endif
476192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
477192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
478191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
479191673Sjamie	free(u_path, M_TEMP);
480191673Sjamie	return (error);
481185435Sbz}
482185435Sbz
483192895Sjamie
484191673Sjamie/*
485191673Sjamie * struct jail_set_args {
486191673Sjamie *	struct iovec *iovp;
487191673Sjamie *	unsigned int iovcnt;
488191673Sjamie *	int flags;
489191673Sjamie * };
490191673Sjamie */
491191673Sjamieint
492225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap)
493185435Sbz{
494191673Sjamie	struct uio *auio;
495191673Sjamie	int error;
496191673Sjamie
497191673Sjamie	/* Check that we have an even number of iovecs. */
498191673Sjamie	if (uap->iovcnt & 1)
499191673Sjamie		return (EINVAL);
500191673Sjamie
501191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
502191673Sjamie	if (error)
503191673Sjamie		return (error);
504191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
505191673Sjamie	free(auio, M_IOV);
506191673Sjamie	return (error);
507191673Sjamie}
508191673Sjamie
509191673Sjamieint
510191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
511191673Sjamie{
512191673Sjamie	struct nameidata nd;
513185435Sbz#ifdef INET
514190466Sjamie	struct in_addr *ip4;
515185435Sbz#endif
516185435Sbz#ifdef INET6
517185435Sbz	struct in6_addr *ip6;
518185435Sbz#endif
519191673Sjamie	struct vfsopt *opt;
520191673Sjamie	struct vfsoptlist *opts;
521196135Sbz	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
522191673Sjamie	struct vnode *root;
523196835Sjamie	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
524230407Smm	char *g_path;
525192895Sjamie#if defined(INET) || defined(INET6)
526196135Sbz	struct prison *tppr;
527191673Sjamie	void *op;
528192895Sjamie#endif
529193066Sjamie	unsigned long hid;
530192895Sjamie	size_t namelen, onamelen;
531192895Sjamie	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
532195870Sjamie	int gotchildmax, gotenforce, gothid, gotslevel;
533195870Sjamie	int fi, jid, jsys, len, level;
534194762Sjamie	int childmax, slevel, vfslocked;
535230129Smm	int fullpath_disabled;
536191673Sjamie#if defined(INET) || defined(INET6)
537192895Sjamie	int ii, ij;
538191673Sjamie#endif
539191673Sjamie#ifdef INET
540195974Sjamie	int ip4s, redo_ip4;
541191673Sjamie#endif
542191673Sjamie#ifdef INET6
543195974Sjamie	int ip6s, redo_ip6;
544191673Sjamie#endif
545224290Smckusick	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
546224290Smckusick	unsigned tallow;
547191673Sjamie	char numbuf[12];
548185435Sbz
549191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
550191673Sjamie	if (!error && (flags & JAIL_ATTACH))
551191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
552191673Sjamie	if (error)
553191673Sjamie		return (error);
554192895Sjamie	mypr = ppr = td->td_ucred->cr_prison;
555194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
556192895Sjamie		return (EPERM);
557191673Sjamie	if (flags & ~JAIL_SET_MASK)
558191673Sjamie		return (EINVAL);
559191673Sjamie
560185435Sbz	/*
561191673Sjamie	 * Check all the parameters before committing to anything.  Not all
562191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
563191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
564191673Sjamie	 * the allprison lock.
565185435Sbz	 *
566191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
567191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
568191673Sjamie	 *     than duplicate it under a different name.
569185435Sbz	 */
570191673Sjamie	error = vfs_buildopts(optuio, &opts);
571191673Sjamie	if (error)
572191673Sjamie		return (error);
573185435Sbz#ifdef INET
574185435Sbz	ip4 = NULL;
575185435Sbz#endif
576185435Sbz#ifdef INET6
577185435Sbz	ip6 = NULL;
578185435Sbz#endif
579230407Smm	g_path = NULL;
580191673Sjamie
581191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
582191673Sjamie	if (error == ENOENT)
583191673Sjamie		jid = 0;
584191673Sjamie	else if (error != 0)
585191673Sjamie		goto done_free;
586191673Sjamie
587191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
588191673Sjamie	if (error == ENOENT)
589191673Sjamie		gotslevel = 0;
590191673Sjamie	else if (error != 0)
591191673Sjamie		goto done_free;
592191673Sjamie	else
593191673Sjamie		gotslevel = 1;
594191673Sjamie
595194762Sjamie	error =
596194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
597194762Sjamie	if (error == ENOENT)
598194762Sjamie		gotchildmax = 0;
599194762Sjamie	else if (error != 0)
600194762Sjamie		goto done_free;
601194762Sjamie	else
602194762Sjamie		gotchildmax = 1;
603194762Sjamie
604192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
605212436Sjamie	if (error == ENOENT)
606212436Sjamie		gotenforce = 0;
607212436Sjamie	else if (error != 0)
608192895Sjamie		goto done_free;
609212436Sjamie	else if (enforce < 0 || enforce > 2) {
610212436Sjamie		error = EINVAL;
611212436Sjamie		goto done_free;
612212436Sjamie	} else
613212436Sjamie		gotenforce = 1;
614192895Sjamie
615191673Sjamie	pr_flags = ch_flags = 0;
616192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
617192895Sjamie	    fi++) {
618192895Sjamie		if (pr_flag_names[fi] == NULL)
619192895Sjamie			continue;
620192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
621192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
622192895Sjamie	}
623191673Sjamie	ch_flags |= pr_flags;
624195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
625195870Sjamie	    fi++) {
626195870Sjamie		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
627195870Sjamie		    sizeof(jsys));
628195870Sjamie		if (error == ENOENT)
629195870Sjamie			continue;
630195870Sjamie		if (error != 0)
631195870Sjamie			goto done_free;
632195870Sjamie		switch (jsys) {
633195870Sjamie		case JAIL_SYS_DISABLE:
634195870Sjamie			if (!pr_flag_jailsys[fi].disable) {
635195870Sjamie				error = EINVAL;
636195870Sjamie				goto done_free;
637195870Sjamie			}
638195870Sjamie			pr_flags |= pr_flag_jailsys[fi].disable;
639195870Sjamie			break;
640195870Sjamie		case JAIL_SYS_NEW:
641195870Sjamie			pr_flags |= pr_flag_jailsys[fi].new;
642195870Sjamie			break;
643195870Sjamie		case JAIL_SYS_INHERIT:
644195870Sjamie			break;
645195870Sjamie		default:
646195870Sjamie			error = EINVAL;
647195870Sjamie			goto done_free;
648195870Sjamie		}
649195870Sjamie		ch_flags |=
650195870Sjamie		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
651195870Sjamie	}
652211085Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
653211085Sjamie	    && !(pr_flags & PR_PERSIST)) {
654211085Sjamie		error = EINVAL;
655211085Sjamie		vfs_opterror(opts, "new jail must persist or attach");
656211085Sjamie		goto done_errmsg;
657211085Sjamie	}
658194251Sjamie#ifdef VIMAGE
659194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
660194251Sjamie		error = EINVAL;
661194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
662194251Sjamie		goto done_errmsg;
663194251Sjamie	}
664194251Sjamie#endif
665195974Sjamie#ifdef INET
666195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
667195974Sjamie		error = EINVAL;
668195974Sjamie		vfs_opterror(opts, "ip4 cannot be changed after creation");
669195974Sjamie		goto done_errmsg;
670195974Sjamie	}
671195974Sjamie#endif
672195974Sjamie#ifdef INET6
673195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
674195974Sjamie		error = EINVAL;
675195974Sjamie		vfs_opterror(opts, "ip6 cannot be changed after creation");
676195974Sjamie		goto done_errmsg;
677195974Sjamie	}
678195974Sjamie#endif
679191673Sjamie
680192895Sjamie	pr_allow = ch_allow = 0;
681192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
682192895Sjamie	    fi++) {
683192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
684192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
685192895Sjamie	}
686192895Sjamie	ch_allow |= pr_allow;
687192895Sjamie
688191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
689191673Sjamie	if (error == ENOENT)
690191673Sjamie		name = NULL;
691191673Sjamie	else if (error != 0)
692191673Sjamie		goto done_free;
693191673Sjamie	else {
694191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
695191673Sjamie			error = EINVAL;
696191673Sjamie			goto done_free;
697191673Sjamie		}
698191673Sjamie		if (len > MAXHOSTNAMELEN) {
699191673Sjamie			error = ENAMETOOLONG;
700191673Sjamie			goto done_free;
701191673Sjamie		}
702191673Sjamie	}
703191673Sjamie
704191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
705191673Sjamie	if (error == ENOENT)
706191673Sjamie		host = NULL;
707191673Sjamie	else if (error != 0)
708191673Sjamie		goto done_free;
709191673Sjamie	else {
710193066Sjamie		ch_flags |= PR_HOST;
711193066Sjamie		pr_flags |= PR_HOST;
712191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
713191673Sjamie			error = EINVAL;
714191673Sjamie			goto done_free;
715191673Sjamie		}
716191673Sjamie		if (len > MAXHOSTNAMELEN) {
717191673Sjamie			error = ENAMETOOLONG;
718191673Sjamie			goto done_free;
719191673Sjamie		}
720191673Sjamie	}
721191673Sjamie
722193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
723193066Sjamie	if (error == ENOENT)
724193066Sjamie		domain = NULL;
725193066Sjamie	else if (error != 0)
726193066Sjamie		goto done_free;
727193066Sjamie	else {
728193066Sjamie		ch_flags |= PR_HOST;
729193066Sjamie		pr_flags |= PR_HOST;
730193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
731193066Sjamie			error = EINVAL;
732193066Sjamie			goto done_free;
733193066Sjamie		}
734193066Sjamie		if (len > MAXHOSTNAMELEN) {
735193066Sjamie			error = ENAMETOOLONG;
736193066Sjamie			goto done_free;
737193066Sjamie		}
738193066Sjamie	}
739193066Sjamie
740193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
741193066Sjamie	if (error == ENOENT)
742193066Sjamie		uuid = NULL;
743193066Sjamie	else if (error != 0)
744193066Sjamie		goto done_free;
745193066Sjamie	else {
746193066Sjamie		ch_flags |= PR_HOST;
747193066Sjamie		pr_flags |= PR_HOST;
748193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
749193066Sjamie			error = EINVAL;
750193066Sjamie			goto done_free;
751193066Sjamie		}
752193066Sjamie		if (len > HOSTUUIDLEN) {
753193066Sjamie			error = ENAMETOOLONG;
754193066Sjamie			goto done_free;
755193066Sjamie		}
756193066Sjamie	}
757193066Sjamie
758205014Snwhitehorn#ifdef COMPAT_FREEBSD32
759217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
760193066Sjamie		uint32_t hid32;
761193066Sjamie
762193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
763193066Sjamie		hid = hid32;
764193066Sjamie	} else
765193066Sjamie#endif
766193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
767193066Sjamie	if (error == ENOENT)
768193066Sjamie		gothid = 0;
769193066Sjamie	else if (error != 0)
770193066Sjamie		goto done_free;
771193066Sjamie	else {
772193066Sjamie		gothid = 1;
773193066Sjamie		ch_flags |= PR_HOST;
774193066Sjamie		pr_flags |= PR_HOST;
775193066Sjamie	}
776193066Sjamie
777185435Sbz#ifdef INET
778191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
779191673Sjamie	if (error == ENOENT)
780195870Sjamie		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
781191673Sjamie	else if (error != 0)
782191673Sjamie		goto done_free;
783191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
784191673Sjamie		error = EINVAL;
785191673Sjamie		goto done_free;
786192895Sjamie	} else {
787195870Sjamie		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
788195870Sjamie		if (ip4s == 0)
789195870Sjamie			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
790195870Sjamie		else {
791195870Sjamie			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
792192895Sjamie			ip4s /= sizeof(*ip4);
793192895Sjamie			if (ip4s > jail_max_af_ips) {
794185435Sbz				error = EINVAL;
795192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
796192895Sjamie				goto done_errmsg;
797185435Sbz			}
798195974Sjamie			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
799192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
800192895Sjamie			/*
801192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
802192895Sjamie			 * the primary IP address as given from userland.
803192895Sjamie			 * This special IP is used for unbound outgoing
804202116Sbz			 * connections as well for "loopback" traffic in case
805202116Sbz			 * source address selection cannot find any more fitting
806202116Sbz			 * address to connect from.
807192895Sjamie			 */
808192895Sjamie			if (ip4s > 1)
809192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
810192895Sjamie			/*
811192895Sjamie			 * Check for duplicate addresses and do some simple
812192895Sjamie			 * zero and broadcast checks. If users give other bogus
813192895Sjamie			 * addresses it is their problem.
814192895Sjamie			 *
815192895Sjamie			 * We do not have to care about byte order for these
816192895Sjamie			 * checks so we will do them in NBO.
817192895Sjamie			 */
818192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
819192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
820192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
821192895Sjamie					error = EINVAL;
822192895Sjamie					goto done_free;
823192895Sjamie				}
824192895Sjamie				if ((ii+1) < ip4s &&
825192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
826192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
827192895Sjamie					error = EINVAL;
828192895Sjamie					goto done_free;
829192895Sjamie				}
830192895Sjamie			}
831185435Sbz		}
832191673Sjamie	}
833191673Sjamie#endif
834185435Sbz
835185435Sbz#ifdef INET6
836191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
837191673Sjamie	if (error == ENOENT)
838195870Sjamie		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
839191673Sjamie	else if (error != 0)
840191673Sjamie		goto done_free;
841191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
842191673Sjamie		error = EINVAL;
843191673Sjamie		goto done_free;
844192895Sjamie	} else {
845195870Sjamie		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
846195870Sjamie		if (ip6s == 0)
847195870Sjamie			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
848195870Sjamie		else {
849195870Sjamie			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
850192895Sjamie			ip6s /= sizeof(*ip6);
851192895Sjamie			if (ip6s > jail_max_af_ips) {
852185435Sbz				error = EINVAL;
853192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
854192895Sjamie				goto done_errmsg;
855185435Sbz			}
856195974Sjamie			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
857192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
858192895Sjamie			if (ip6s > 1)
859192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
860192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
861192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
862192895Sjamie					error = EINVAL;
863192895Sjamie					goto done_free;
864192895Sjamie				}
865192895Sjamie				if ((ii+1) < ip6s &&
866192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
867192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
868192895Sjamie				{
869192895Sjamie					error = EINVAL;
870192895Sjamie					goto done_free;
871192895Sjamie				}
872192895Sjamie			}
873185435Sbz		}
874191673Sjamie	}
875185435Sbz#endif
876185435Sbz
877195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
878195945Sjamie	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
879195945Sjamie		error = EINVAL;
880195945Sjamie		vfs_opterror(opts,
881195945Sjamie		    "vnet jails cannot have IP address restrictions");
882195945Sjamie		goto done_errmsg;
883195945Sjamie	}
884195945Sjamie#endif
885195945Sjamie
886230143Smm	fullpath_disabled = 0;
887191673Sjamie	root = NULL;
888191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
889191673Sjamie	if (error == ENOENT)
890191673Sjamie		path = NULL;
891191673Sjamie	else if (error != 0)
892191673Sjamie		goto done_free;
893191673Sjamie	else {
894191673Sjamie		if (flags & JAIL_UPDATE) {
895191673Sjamie			error = EINVAL;
896191673Sjamie			vfs_opterror(opts,
897191673Sjamie			    "path cannot be changed after creation");
898191673Sjamie			goto done_errmsg;
899191673Sjamie		}
900191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
901191673Sjamie			error = EINVAL;
902191673Sjamie			goto done_free;
903191673Sjamie		}
904230129Smm		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | MPSAFE, UIO_SYSSPACE,
905230129Smm		    path, td);
906230129Smm		error = namei(&nd);
907230129Smm		if (error)
908230129Smm			goto done_free;
909230129Smm		vfslocked = NDHASGIANT(&nd);
910230129Smm		root = nd.ni_vp;
911230129Smm		NDFREE(&nd, NDF_ONLY_PNBUF);
912230407Smm		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
913230407Smm		strlcpy(g_path, path, MAXPATHLEN);
914230407Smm		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
915230407Smm		if (error == 0)
916230407Smm			path = g_path;
917230407Smm		else if (error == ENODEV) {
918230129Smm			/* proceed if sysctl debug.disablefullpath == 1 */
919230129Smm			fullpath_disabled = 1;
920230129Smm			if (len < 2 || (len == 2 && path[0] == '/'))
921230129Smm				path = NULL;
922230407Smm		} else {
923230129Smm			/* exit on other errors */
924230129Smm			VFS_UNLOCK_GIANT(vfslocked);
925230129Smm			goto done_free;
926230129Smm		}
927230129Smm		if (root->v_type != VDIR) {
928230129Smm			error = ENOTDIR;
929230129Smm			vput(root);
930230129Smm			VFS_UNLOCK_GIANT(vfslocked);
931230129Smm			goto done_free;
932230129Smm		}
933230129Smm		VOP_UNLOCK(root, 0);
934230129Smm		VFS_UNLOCK_GIANT(vfslocked);
935230129Smm		if (fullpath_disabled) {
936192895Sjamie			/* Leave room for a real-root full pathname. */
937192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
938192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
939192895Sjamie				error = ENAMETOOLONG;
940192895Sjamie				goto done_free;
941192895Sjamie			}
942191673Sjamie		}
943191673Sjamie	}
944185435Sbz
945191673Sjamie	/*
946191673Sjamie	 * Grab the allprison lock before letting modules check their
947191673Sjamie	 * parameters.  Once we have it, do not let go so we'll have a
948191673Sjamie	 * consistent view of the OSD list.
949191673Sjamie	 */
950191673Sjamie	sx_xlock(&allprison_lock);
951191673Sjamie	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
952191673Sjamie	if (error)
953191673Sjamie		goto done_unlock_list;
954185435Sbz
955191673Sjamie	/* By now, all parameters should have been noted. */
956191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
957191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
958191673Sjamie			error = EINVAL;
959191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
960191673Sjamie			goto done_unlock_list;
961191673Sjamie		}
962191673Sjamie	}
963191673Sjamie
964185435Sbz	/*
965191673Sjamie	 * See if we are creating a new record or updating an existing one.
966191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
967185435Sbz	 */
968191673Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
969191673Sjamie	if (!cuflags) {
970191673Sjamie		error = EINVAL;
971191673Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
972191673Sjamie		goto done_unlock_list;
973191673Sjamie	}
974191673Sjamie	pr = NULL;
975196835Sjamie	namelc = NULL;
976196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
977196835Sjamie		namelc = strrchr(name, '.');
978196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
979196835Sjamie		if (*p != '\0')
980196835Sjamie			jid = 0;
981196835Sjamie	}
982191673Sjamie	if (jid != 0) {
983192895Sjamie		/*
984192895Sjamie		 * See if a requested jid already exists.  There is an
985192895Sjamie		 * information leak here if the jid exists but is not within
986192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
987192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
988192895Sjamie		 * will return ENOENT which is not normally a valid error.
989192895Sjamie		 */
990191673Sjamie		if (jid < 0) {
991191673Sjamie			error = EINVAL;
992191673Sjamie			vfs_opterror(opts, "negative jid");
993191673Sjamie			goto done_unlock_list;
994191673Sjamie		}
995191673Sjamie		pr = prison_find(jid);
996191673Sjamie		if (pr != NULL) {
997192895Sjamie			ppr = pr->pr_parent;
998191673Sjamie			/* Create: jid must not exist. */
999191673Sjamie			if (cuflags == JAIL_CREATE) {
1000191673Sjamie				mtx_unlock(&pr->pr_mtx);
1001191673Sjamie				error = EEXIST;
1002191673Sjamie				vfs_opterror(opts, "jail %d already exists",
1003191673Sjamie				    jid);
1004191673Sjamie				goto done_unlock_list;
1005191673Sjamie			}
1006192895Sjamie			if (!prison_ischild(mypr, pr)) {
1007192895Sjamie				mtx_unlock(&pr->pr_mtx);
1008192895Sjamie				pr = NULL;
1009192895Sjamie			} else if (pr->pr_uref == 0) {
1010191673Sjamie				if (!(flags & JAIL_DYING)) {
1011191673Sjamie					mtx_unlock(&pr->pr_mtx);
1012191673Sjamie					error = ENOENT;
1013191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1014191673Sjamie					    jid);
1015191673Sjamie					goto done_unlock_list;
1016191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
1017191673Sjamie				    (pr_flags & PR_PERSIST)) {
1018191673Sjamie					/*
1019191673Sjamie					 * A dying jail might be resurrected
1020191673Sjamie					 * (via attach or persist), but first
1021191673Sjamie					 * it must determine if another jail
1022191673Sjamie					 * has claimed its name.  Accomplish
1023191673Sjamie					 * this by implicitly re-setting the
1024191673Sjamie					 * name.
1025191673Sjamie					 */
1026191673Sjamie					if (name == NULL)
1027192895Sjamie						name = prison_name(mypr, pr);
1028191673Sjamie				}
1029191673Sjamie			}
1030191673Sjamie		}
1031191673Sjamie		if (pr == NULL) {
1032191673Sjamie			/* Update: jid must exist. */
1033191673Sjamie			if (cuflags == JAIL_UPDATE) {
1034191673Sjamie				error = ENOENT;
1035191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1036191673Sjamie				goto done_unlock_list;
1037191673Sjamie			}
1038191673Sjamie		}
1039191673Sjamie	}
1040191673Sjamie	/*
1041191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1042191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1043191673Sjamie	 * (where the name must not already exist in a different jail),
1044191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1045191673Sjamie	 * because that is the jail being updated).
1046191673Sjamie	 */
1047191673Sjamie	if (name != NULL) {
1048196835Sjamie		namelc = strrchr(name, '.');
1049196835Sjamie		if (namelc == NULL)
1050196835Sjamie			namelc = name;
1051196835Sjamie		else {
1052192895Sjamie			/*
1053192895Sjamie			 * This is a hierarchical name.  Split it into the
1054192895Sjamie			 * parent and child names, and make sure the parent
1055192895Sjamie			 * exists or matches an already found jail.
1056192895Sjamie			 */
1057196835Sjamie			*namelc = '\0';
1058192895Sjamie			if (pr != NULL) {
1059196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1060196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1061192895Sjamie					mtx_unlock(&pr->pr_mtx);
1062192895Sjamie					error = EINVAL;
1063192895Sjamie					vfs_opterror(opts,
1064192895Sjamie					    "cannot change jail's parent");
1065192895Sjamie					goto done_unlock_list;
1066192895Sjamie				}
1067192895Sjamie			} else {
1068192895Sjamie				ppr = prison_find_name(mypr, name);
1069192895Sjamie				if (ppr == NULL) {
1070192895Sjamie					error = ENOENT;
1071192895Sjamie					vfs_opterror(opts,
1072192895Sjamie					    "jail \"%s\" not found", name);
1073192895Sjamie					goto done_unlock_list;
1074192895Sjamie				}
1075192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1076192895Sjamie			}
1077196835Sjamie			name = ++namelc;
1078192895Sjamie		}
1079191673Sjamie		if (name[0] != '\0') {
1080192895Sjamie			namelen =
1081192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1082192895Sjamie name_again:
1083191673Sjamie			deadpr = NULL;
1084192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1085191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1086192895Sjamie				    !strcmp(tpr->pr_name + namelen, name)) {
1087191673Sjamie					if (pr == NULL &&
1088191673Sjamie					    cuflags != JAIL_CREATE) {
1089191673Sjamie						mtx_lock(&tpr->pr_mtx);
1090191673Sjamie						if (tpr->pr_ref > 0) {
1091191673Sjamie							/*
1092191673Sjamie							 * Use this jail
1093191673Sjamie							 * for updates.
1094191673Sjamie							 */
1095191673Sjamie							if (tpr->pr_uref > 0) {
1096191673Sjamie								pr = tpr;
1097191673Sjamie								break;
1098191673Sjamie							}
1099191673Sjamie							deadpr = tpr;
1100191673Sjamie						}
1101191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1102191673Sjamie					} else if (tpr->pr_uref > 0) {
1103191673Sjamie						/*
1104191673Sjamie						 * Create, or update(jid):
1105191673Sjamie						 * name must not exist in an
1106192895Sjamie						 * active sibling jail.
1107191673Sjamie						 */
1108191673Sjamie						error = EEXIST;
1109191673Sjamie						if (pr != NULL)
1110191673Sjamie							mtx_unlock(&pr->pr_mtx);
1111191673Sjamie						vfs_opterror(opts,
1112191673Sjamie						   "jail \"%s\" already exists",
1113191673Sjamie						   name);
1114191673Sjamie						goto done_unlock_list;
1115191673Sjamie					}
1116191673Sjamie				}
1117191673Sjamie			}
1118191673Sjamie			/* If no active jail is found, use a dying one. */
1119191673Sjamie			if (deadpr != NULL && pr == NULL) {
1120191673Sjamie				if (flags & JAIL_DYING) {
1121191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1122191673Sjamie					if (deadpr->pr_ref == 0) {
1123191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1124191673Sjamie						goto name_again;
1125191673Sjamie					}
1126191673Sjamie					pr = deadpr;
1127191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1128191673Sjamie					error = ENOENT;
1129191673Sjamie					vfs_opterror(opts,
1130191673Sjamie					    "jail \"%s\" is dying", name);
1131191673Sjamie					goto done_unlock_list;
1132191673Sjamie				}
1133191673Sjamie			}
1134191673Sjamie			/* Update: name must exist if no jid. */
1135191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1136191673Sjamie				error = ENOENT;
1137191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1138191673Sjamie				    name);
1139191673Sjamie				goto done_unlock_list;
1140191673Sjamie			}
1141191673Sjamie		}
1142191673Sjamie	}
1143191673Sjamie	/* Update: must provide a jid or name. */
1144191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1145191673Sjamie		error = ENOENT;
1146191673Sjamie		vfs_opterror(opts, "update specified no jail");
1147191673Sjamie		goto done_unlock_list;
1148191673Sjamie	}
1149185435Sbz
1150191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1151191673Sjamie	if (pr == NULL) {
1152194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1153194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1154194762Sjamie				error = EPERM;
1155194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1156194762Sjamie				goto done_unlock_list;
1157194762Sjamie			}
1158191673Sjamie		created = 1;
1159192895Sjamie		mtx_lock(&ppr->pr_mtx);
1160192895Sjamie		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1161192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1162192895Sjamie			error = ENOENT;
1163192895Sjamie			vfs_opterror(opts, "parent jail went away!");
1164192895Sjamie			goto done_unlock_list;
1165192895Sjamie		}
1166192895Sjamie		ppr->pr_ref++;
1167192895Sjamie		ppr->pr_uref++;
1168192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1169191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1170191673Sjamie		if (jid == 0) {
1171191673Sjamie			/* Find the next free jid. */
1172191673Sjamie			jid = lastprid + 1;
1173191673Sjamie findnext:
1174191673Sjamie			if (jid == JAIL_MAX)
1175191673Sjamie				jid = 1;
1176191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1177191673Sjamie				if (tpr->pr_id < jid)
1178191673Sjamie					continue;
1179191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1180191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1181191673Sjamie					break;
1182191673Sjamie				}
1183191673Sjamie				if (jid == lastprid) {
1184191673Sjamie					error = EAGAIN;
1185191673Sjamie					vfs_opterror(opts,
1186191673Sjamie					    "no available jail IDs");
1187191673Sjamie					free(pr, M_PRISON);
1188192895Sjamie					prison_deref(ppr, PD_DEREF |
1189192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1190192895Sjamie					goto done_releroot;
1191191673Sjamie				}
1192191673Sjamie				jid++;
1193191673Sjamie				goto findnext;
1194191673Sjamie			}
1195191673Sjamie			lastprid = jid;
1196191673Sjamie		} else {
1197191673Sjamie			/*
1198191673Sjamie			 * The jail already has a jid (that did not yet exist),
1199191673Sjamie			 * so just find where to insert it.
1200191673Sjamie			 */
1201191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1202191673Sjamie				if (tpr->pr_id >= jid) {
1203191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1204191673Sjamie					break;
1205191673Sjamie				}
1206191673Sjamie		}
1207191673Sjamie		if (tpr == NULL)
1208191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1209192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1210192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1211194762Sjamie			tpr->pr_childcount++;
1212185435Sbz
1213192895Sjamie		pr->pr_parent = ppr;
1214191673Sjamie		pr->pr_id = jid;
1215192895Sjamie
1216192895Sjamie		/* Set some default values, and inherit some from the parent. */
1217191673Sjamie		if (name == NULL)
1218191673Sjamie			name = "";
1219191673Sjamie		if (path == NULL) {
1220191673Sjamie			path = "/";
1221192895Sjamie			root = mypr->pr_root;
1222191673Sjamie			vref(root);
1223191673Sjamie		}
1224195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1225195944Sjamie		pr->pr_flags |= PR_HOST;
1226195945Sjamie#if defined(INET) || defined(INET6)
1227195945Sjamie#ifdef VIMAGE
1228195945Sjamie		if (!(pr_flags & PR_VNET))
1229195945Sjamie#endif
1230195945Sjamie		{
1231192895Sjamie#ifdef INET
1232195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1233195974Sjamie				pr->pr_flags |=
1234195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1235195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1236195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1237195974Sjamie				if (ppr->pr_ip4 != NULL) {
1238195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1239195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1240195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1241195974Sjamie					    M_WAITOK);
1242195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1243195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1244195974Sjamie				}
1245195974Sjamie			}
1246192895Sjamie#endif
1247192895Sjamie#ifdef INET6
1248195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1249195974Sjamie				pr->pr_flags |=
1250195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1251195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1252195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1253195974Sjamie				if (ppr->pr_ip6 != NULL) {
1254195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1255195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1256195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1257195974Sjamie					    M_WAITOK);
1258195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1259195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1260195974Sjamie				}
1261195974Sjamie			}
1262192895Sjamie#endif
1263195945Sjamie		}
1264195945Sjamie#endif
1265202468Sbz		/* Source address selection is always on by default. */
1266202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1267202468Sbz
1268192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1269192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1270196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1271191673Sjamie
1272192895Sjamie		LIST_INIT(&pr->pr_children);
1273192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1274191673Sjamie
1275194251Sjamie#ifdef VIMAGE
1276194251Sjamie		/* Allocate a new vnet if specified. */
1277194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1278194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1279194251Sjamie#endif
1280185435Sbz		/*
1281191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1282191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1283185435Sbz		 */
1284192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1285191673Sjamie		if (error) {
1286191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1287191673Sjamie			goto done_releroot;
1288191673Sjamie		}
1289185435Sbz
1290191673Sjamie		mtx_lock(&pr->pr_mtx);
1291185435Sbz		/*
1292191673Sjamie		 * New prisons do not yet have a reference, because we do not
1293191673Sjamie		 * want other to see the incomplete prison once the
1294191673Sjamie		 * allprison_lock is downgraded.
1295185435Sbz		 */
1296191673Sjamie	} else {
1297191673Sjamie		created = 0;
1298195974Sjamie		/*
1299195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1300195974Sjamie		 * continue to exist for the duration of the call.
1301195974Sjamie		 */
1302195974Sjamie		pr->pr_ref++;
1303195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1304195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1305195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1306195945Sjamie			error = EINVAL;
1307195945Sjamie			vfs_opterror(opts,
1308195945Sjamie			    "vnet jails cannot have IP address restrictions");
1309195945Sjamie			goto done_deref_locked;
1310195945Sjamie		}
1311195945Sjamie#endif
1312195974Sjamie#ifdef INET
1313195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1314195974Sjamie			error = EINVAL;
1315195974Sjamie			vfs_opterror(opts,
1316195974Sjamie			    "ip4 cannot be changed after creation");
1317195974Sjamie			goto done_deref_locked;
1318195974Sjamie		}
1319195974Sjamie#endif
1320195974Sjamie#ifdef INET6
1321195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1322195974Sjamie			error = EINVAL;
1323195974Sjamie			vfs_opterror(opts,
1324195974Sjamie			    "ip6 cannot be changed after creation");
1325195974Sjamie			goto done_deref_locked;
1326195974Sjamie		}
1327195974Sjamie#endif
1328191673Sjamie	}
1329185435Sbz
1330191673Sjamie	/* Do final error checking before setting anything. */
1331192895Sjamie	if (gotslevel) {
1332192895Sjamie		if (slevel < ppr->pr_securelevel) {
1333192895Sjamie			error = EPERM;
1334192895Sjamie			goto done_deref_locked;
1335192895Sjamie		}
1336192895Sjamie	}
1337194762Sjamie	if (gotchildmax) {
1338194762Sjamie		if (childmax >= ppr->pr_childmax) {
1339194762Sjamie			error = EPERM;
1340194762Sjamie			goto done_deref_locked;
1341194762Sjamie		}
1342194762Sjamie	}
1343192895Sjamie	if (gotenforce) {
1344192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1345192895Sjamie			error = EPERM;
1346192895Sjamie			goto done_deref_locked;
1347192895Sjamie		}
1348192895Sjamie	}
1349185435Sbz#ifdef INET
1350195974Sjamie	if (ip4s > 0) {
1351192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1352195974Sjamie			/*
1353195974Sjamie			 * Make sure the new set of IP addresses is a
1354195974Sjamie			 * subset of the parent's list.  Don't worry
1355195974Sjamie			 * about the parent being unlocked, as any
1356195974Sjamie			 * setting is done with allprison_lock held.
1357195974Sjamie			 */
1358195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1359195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1360195974Sjamie					break;
1361195974Sjamie			if (ij == ppr->pr_ip4s) {
1362195974Sjamie				error = EPERM;
1363195974Sjamie				goto done_deref_locked;
1364195974Sjamie			}
1365195974Sjamie			if (ip4s > 1) {
1366195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1367195974Sjamie					if (ip4[ii].s_addr ==
1368195974Sjamie					    ppr->pr_ip4[0].s_addr)
1369195974Sjamie						continue;
1370195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1371195974Sjamie						if (ip4[ii].s_addr ==
1372195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1373195974Sjamie							break;
1374195974Sjamie					if (ij == ppr->pr_ip4s)
1375195974Sjamie						break;
1376192895Sjamie				}
1377192895Sjamie				if (ij == ppr->pr_ip4s) {
1378192895Sjamie					error = EPERM;
1379192895Sjamie					goto done_deref_locked;
1380192895Sjamie				}
1381192895Sjamie			}
1382192895Sjamie		}
1383195974Sjamie		/*
1384195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1385195974Sjamie		 * if there is no more than one IP on each jail.  If
1386195974Sjamie		 * there is a duplicate on a jail with more than one
1387195974Sjamie		 * IP stop checking and return error.
1388195974Sjamie		 */
1389195974Sjamie		tppr = ppr;
1390195945Sjamie#ifdef VIMAGE
1391195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1392195974Sjamie			if (tppr->pr_flags & PR_VNET)
1393195974Sjamie				break;
1394195945Sjamie#endif
1395195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1396195974Sjamie			if (tpr == pr ||
1397195945Sjamie#ifdef VIMAGE
1398195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1399195945Sjamie#endif
1400195974Sjamie			    tpr->pr_uref == 0) {
1401192895Sjamie				descend = 0;
1402195974Sjamie				continue;
1403195974Sjamie			}
1404195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1405195974Sjamie				continue;
1406195974Sjamie			descend = 0;
1407195974Sjamie			if (tpr->pr_ip4 == NULL ||
1408195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1409195974Sjamie				continue;
1410195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1411195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1412195974Sjamie					error = EADDRINUSE;
1413195974Sjamie					vfs_opterror(opts,
1414195974Sjamie					    "IPv4 addresses clash");
1415195974Sjamie					goto done_deref_locked;
1416192895Sjamie				}
1417192895Sjamie			}
1418192895Sjamie		}
1419192895Sjamie	}
1420185435Sbz#endif
1421191673Sjamie#ifdef INET6
1422195974Sjamie	if (ip6s > 0) {
1423192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1424195974Sjamie			/*
1425195974Sjamie			 * Make sure the new set of IP addresses is a
1426195974Sjamie			 * subset of the parent's list.
1427195974Sjamie			 */
1428195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1429195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1430195974Sjamie				    &ppr->pr_ip6[ij]))
1431195974Sjamie					break;
1432195974Sjamie			if (ij == ppr->pr_ip6s) {
1433195974Sjamie				error = EPERM;
1434195974Sjamie				goto done_deref_locked;
1435195974Sjamie			}
1436195974Sjamie			if (ip6s > 1) {
1437195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1438195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1439195974Sjamie					     &ppr->pr_ip6[0]))
1440195974Sjamie						continue;
1441195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1442195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1443195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1444195974Sjamie							break;
1445195974Sjamie					if (ij == ppr->pr_ip6s)
1446195974Sjamie						break;
1447192895Sjamie				}
1448192895Sjamie				if (ij == ppr->pr_ip6s) {
1449192895Sjamie					error = EPERM;
1450192895Sjamie					goto done_deref_locked;
1451192895Sjamie				}
1452192895Sjamie			}
1453192895Sjamie		}
1454195974Sjamie		/* Check for conflicting IP addresses. */
1455195974Sjamie		tppr = ppr;
1456195945Sjamie#ifdef VIMAGE
1457195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1458195974Sjamie			if (tppr->pr_flags & PR_VNET)
1459195974Sjamie				break;
1460195945Sjamie#endif
1461195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1462195974Sjamie			if (tpr == pr ||
1463195945Sjamie#ifdef VIMAGE
1464195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1465195945Sjamie#endif
1466195974Sjamie			    tpr->pr_uref == 0) {
1467192895Sjamie				descend = 0;
1468195974Sjamie				continue;
1469195974Sjamie			}
1470195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1471195974Sjamie				continue;
1472195974Sjamie			descend = 0;
1473195974Sjamie			if (tpr->pr_ip6 == NULL ||
1474195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1475195974Sjamie				continue;
1476195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1477195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1478195974Sjamie					error = EADDRINUSE;
1479195974Sjamie					vfs_opterror(opts,
1480195974Sjamie					    "IPv6 addresses clash");
1481195974Sjamie					goto done_deref_locked;
1482192895Sjamie				}
1483192895Sjamie			}
1484191673Sjamie		}
1485192895Sjamie	}
1486191673Sjamie#endif
1487192895Sjamie	onamelen = namelen = 0;
1488192895Sjamie	if (name != NULL) {
1489191673Sjamie		/* Give a default name of the jid. */
1490191673Sjamie		if (name[0] == '\0')
1491191673Sjamie			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1492196835Sjamie		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1493196835Sjamie		    *p == '\0')) {
1494191673Sjamie			error = EINVAL;
1495196835Sjamie			vfs_opterror(opts,
1496196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1497192895Sjamie			goto done_deref_locked;
1498191673Sjamie		}
1499191673Sjamie		/*
1500192895Sjamie		 * Make sure the name isn't too long for the prison or its
1501192895Sjamie		 * children.
1502191673Sjamie		 */
1503192895Sjamie		onamelen = strlen(pr->pr_name);
1504192895Sjamie		namelen = strlen(name);
1505192895Sjamie		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1506192895Sjamie			error = ENAMETOOLONG;
1507192895Sjamie			goto done_deref_locked;
1508192895Sjamie		}
1509192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1510192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1511192895Sjamie			    sizeof(pr->pr_name)) {
1512192895Sjamie				error = ENAMETOOLONG;
1513192895Sjamie				goto done_deref_locked;
1514192895Sjamie			}
1515192895Sjamie		}
1516191673Sjamie	}
1517192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1518192895Sjamie		error = EPERM;
1519192895Sjamie		goto done_deref_locked;
1520192895Sjamie	}
1521185435Sbz
1522191673Sjamie	/* Set the parameters of the prison. */
1523191673Sjamie#ifdef INET
1524192895Sjamie	redo_ip4 = 0;
1525195974Sjamie	if (pr_flags & PR_IP4_USER) {
1526195974Sjamie		pr->pr_flags |= PR_IP4;
1527195974Sjamie		free(pr->pr_ip4, M_PRISON);
1528195974Sjamie		pr->pr_ip4s = ip4s;
1529195974Sjamie		pr->pr_ip4 = ip4;
1530195974Sjamie		ip4 = NULL;
1531192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1532195945Sjamie#ifdef VIMAGE
1533195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1534195945Sjamie				descend = 0;
1535195945Sjamie				continue;
1536195945Sjamie			}
1537195945Sjamie#endif
1538192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1539192895Sjamie				redo_ip4 = 1;
1540192895Sjamie				descend = 0;
1541192895Sjamie			}
1542192895Sjamie		}
1543185435Sbz	}
1544191673Sjamie#endif
1545191673Sjamie#ifdef INET6
1546192895Sjamie	redo_ip6 = 0;
1547195974Sjamie	if (pr_flags & PR_IP6_USER) {
1548195974Sjamie		pr->pr_flags |= PR_IP6;
1549195974Sjamie		free(pr->pr_ip6, M_PRISON);
1550195974Sjamie		pr->pr_ip6s = ip6s;
1551195974Sjamie		pr->pr_ip6 = ip6;
1552195974Sjamie		ip6 = NULL;
1553192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1554195945Sjamie#ifdef VIMAGE
1555195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1556195945Sjamie				descend = 0;
1557195945Sjamie				continue;
1558195945Sjamie			}
1559195945Sjamie#endif
1560192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1561192895Sjamie				redo_ip6 = 1;
1562192895Sjamie				descend = 0;
1563192895Sjamie			}
1564192895Sjamie		}
1565191673Sjamie	}
1566191673Sjamie#endif
1567192895Sjamie	if (gotslevel) {
1568191673Sjamie		pr->pr_securelevel = slevel;
1569192895Sjamie		/* Set all child jails to be at least this level. */
1570192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1571192895Sjamie			if (tpr->pr_securelevel < slevel)
1572192895Sjamie				tpr->pr_securelevel = slevel;
1573192895Sjamie	}
1574194762Sjamie	if (gotchildmax) {
1575194762Sjamie		pr->pr_childmax = childmax;
1576194762Sjamie		/* Set all child jails to under this limit. */
1577194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1578194762Sjamie			if (tpr->pr_childmax > childmax - level)
1579194762Sjamie				tpr->pr_childmax = childmax > level
1580194762Sjamie				    ? childmax - level : 0;
1581194762Sjamie	}
1582192895Sjamie	if (gotenforce) {
1583192895Sjamie		pr->pr_enforce_statfs = enforce;
1584192895Sjamie		/* Pass this restriction on to the children. */
1585192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1586192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1587192895Sjamie				tpr->pr_enforce_statfs = enforce;
1588192895Sjamie	}
1589192895Sjamie	if (name != NULL) {
1590192895Sjamie		if (ppr == &prison0)
1591192895Sjamie			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1592192895Sjamie		else
1593192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1594192895Sjamie			    ppr->pr_name, name);
1595192895Sjamie		/* Change this component of child names. */
1596192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1597192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1598192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1599192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1600192895Sjamie		}
1601192895Sjamie	}
1602191673Sjamie	if (path != NULL) {
1603192895Sjamie		/* Try to keep a real-rooted full pathname. */
1604230129Smm		if (fullpath_disabled && path[0] == '/' &&
1605230129Smm		    strcmp(mypr->pr_path, "/"))
1606192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1607192895Sjamie			    mypr->pr_path, path);
1608192895Sjamie		else
1609192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1610191673Sjamie		pr->pr_root = root;
1611191673Sjamie	}
1612193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1613193066Sjamie		if (pr->pr_flags & PR_HOST) {
1614193066Sjamie			/*
1615193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1616193066Sjamie			 * the lack of a lock on the parent is not a problem;
1617193066Sjamie			 * it is always set with allprison_lock at least
1618193066Sjamie			 * shared, and is held exclusively here.
1619193066Sjamie			 */
1620194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1621194118Sjamie			    sizeof(pr->pr_hostname));
1622194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1623194118Sjamie			    sizeof(pr->pr_domainname));
1624194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1625194118Sjamie			    sizeof(pr->pr_hostuuid));
1626193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1627193066Sjamie		}
1628193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1629193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1630193066Sjamie		if (host != NULL)
1631194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1632193066Sjamie		if (domain != NULL)
1633194118Sjamie			strlcpy(pr->pr_domainname, domain,
1634194118Sjamie			    sizeof(pr->pr_domainname));
1635193066Sjamie		if (uuid != NULL)
1636194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1637193066Sjamie		if (gothid)
1638193066Sjamie			pr->pr_hostid = hid;
1639193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1640193066Sjamie			if (tpr->pr_flags & PR_HOST)
1641193066Sjamie				descend = 0;
1642193066Sjamie			else {
1643193066Sjamie				if (host != NULL)
1644194118Sjamie					strlcpy(tpr->pr_hostname,
1645194118Sjamie					    pr->pr_hostname,
1646194118Sjamie					    sizeof(tpr->pr_hostname));
1647193066Sjamie				if (domain != NULL)
1648194118Sjamie					strlcpy(tpr->pr_domainname,
1649194118Sjamie					    pr->pr_domainname,
1650194118Sjamie					    sizeof(tpr->pr_domainname));
1651193066Sjamie				if (uuid != NULL)
1652194118Sjamie					strlcpy(tpr->pr_hostuuid,
1653194118Sjamie					    pr->pr_hostuuid,
1654194118Sjamie					    sizeof(tpr->pr_hostuuid));
1655193066Sjamie				if (gothid)
1656193066Sjamie					tpr->pr_hostid = hid;
1657193066Sjamie			}
1658193066Sjamie		}
1659193066Sjamie	}
1660192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1661192895Sjamie		/* Clear allow bits in all children. */
1662192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1663192895Sjamie			tpr->pr_allow &= ~tallow;
1664192895Sjamie	}
1665192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1666191673Sjamie	/*
1667191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1668191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1669191673Sjamie	 * for now, so new ones will remain unseen until after the module
1670191673Sjamie	 * handlers have completed.
1671191673Sjamie	 */
1672191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1673191673Sjamie		if (pr_flags & PR_PERSIST) {
1674191673Sjamie			pr->pr_ref++;
1675191673Sjamie			pr->pr_uref++;
1676191673Sjamie		} else {
1677191673Sjamie			pr->pr_ref--;
1678191673Sjamie			pr->pr_uref--;
1679191673Sjamie		}
1680191673Sjamie	}
1681191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1682191673Sjamie	mtx_unlock(&pr->pr_mtx);
1683185435Sbz
1684221362Strasz#ifdef RACCT
1685221362Strasz	if (created)
1686221362Strasz		prison_racct_attach(pr);
1687221362Strasz#endif
1688221362Strasz
1689192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1690192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1691192895Sjamie	 */
1692192895Sjamie#ifdef INET
1693192895Sjamie	while (redo_ip4) {
1694192895Sjamie		ip4s = pr->pr_ip4s;
1695192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1696192895Sjamie		mtx_lock(&pr->pr_mtx);
1697192895Sjamie		redo_ip4 = 0;
1698192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1699195945Sjamie#ifdef VIMAGE
1700195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1701195945Sjamie				descend = 0;
1702195945Sjamie				continue;
1703195945Sjamie			}
1704195945Sjamie#endif
1705192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1706192895Sjamie				if (ip4 != NULL)
1707192895Sjamie					ip4 = NULL;
1708192895Sjamie				else
1709192895Sjamie					redo_ip4 = 1;
1710192895Sjamie			}
1711192895Sjamie		}
1712192895Sjamie		mtx_unlock(&pr->pr_mtx);
1713192895Sjamie	}
1714192895Sjamie#endif
1715192895Sjamie#ifdef INET6
1716192895Sjamie	while (redo_ip6) {
1717192895Sjamie		ip6s = pr->pr_ip6s;
1718192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1719192895Sjamie		mtx_lock(&pr->pr_mtx);
1720192895Sjamie		redo_ip6 = 0;
1721192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1722195945Sjamie#ifdef VIMAGE
1723195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1724195945Sjamie				descend = 0;
1725195945Sjamie				continue;
1726195945Sjamie			}
1727195945Sjamie#endif
1728192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1729192895Sjamie				if (ip6 != NULL)
1730192895Sjamie					ip6 = NULL;
1731192895Sjamie				else
1732192895Sjamie					redo_ip6 = 1;
1733192895Sjamie			}
1734192895Sjamie		}
1735192895Sjamie		mtx_unlock(&pr->pr_mtx);
1736192895Sjamie	}
1737192895Sjamie#endif
1738192895Sjamie
1739191673Sjamie	/* Let the modules do their work. */
1740191673Sjamie	sx_downgrade(&allprison_lock);
1741191673Sjamie	if (created) {
1742191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1743191673Sjamie		if (error) {
1744191673Sjamie			prison_deref(pr, PD_LIST_SLOCKED);
1745191673Sjamie			goto done_errmsg;
1746191673Sjamie		}
1747191673Sjamie	}
1748191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1749191673Sjamie	if (error) {
1750191673Sjamie		prison_deref(pr, created
1751191673Sjamie		    ? PD_LIST_SLOCKED
1752191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1753191673Sjamie		goto done_errmsg;
1754191673Sjamie	}
1755191673Sjamie
1756191673Sjamie	/* Attach this process to the prison if requested. */
1757191673Sjamie	if (flags & JAIL_ATTACH) {
1758191673Sjamie		mtx_lock(&pr->pr_mtx);
1759191673Sjamie		error = do_jail_attach(td, pr);
1760191673Sjamie		if (error) {
1761191673Sjamie			vfs_opterror(opts, "attach failed");
1762191673Sjamie			if (!created)
1763191673Sjamie				prison_deref(pr, PD_DEREF);
1764191673Sjamie			goto done_errmsg;
1765191673Sjamie		}
1766191673Sjamie	}
1767191673Sjamie
1768191673Sjamie	/*
1769191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1770191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1771191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1772191673Sjamie	 * visible).
1773191673Sjamie	 */
1774191673Sjamie	if (!created) {
1775191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1776191673Sjamie		    ? PD_DEREF
1777191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1778191673Sjamie	} else {
1779191673Sjamie		if (pr_flags & PR_PERSIST) {
1780191673Sjamie			mtx_lock(&pr->pr_mtx);
1781191673Sjamie			pr->pr_ref++;
1782191673Sjamie			pr->pr_uref++;
1783191673Sjamie			mtx_unlock(&pr->pr_mtx);
1784191673Sjamie		}
1785191673Sjamie		if (!(flags & JAIL_ATTACH))
1786191673Sjamie			sx_sunlock(&allprison_lock);
1787191673Sjamie	}
1788191673Sjamie	td->td_retval[0] = pr->pr_id;
1789191673Sjamie	goto done_errmsg;
1790191673Sjamie
1791192895Sjamie done_deref_locked:
1792192895Sjamie	prison_deref(pr, created
1793192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1794192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1795192895Sjamie	goto done_releroot;
1796191673Sjamie done_unlock_list:
1797191673Sjamie	sx_xunlock(&allprison_lock);
1798191673Sjamie done_releroot:
1799191673Sjamie	if (root != NULL) {
1800191673Sjamie		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1801191673Sjamie		vrele(root);
1802191673Sjamie		VFS_UNLOCK_GIANT(vfslocked);
1803191673Sjamie	}
1804191673Sjamie done_errmsg:
1805191673Sjamie	if (error) {
1806191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1807191673Sjamie		if (errmsg_len > 0) {
1808191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1809191673Sjamie			if (errmsg_pos > 0) {
1810191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1811191673Sjamie					bcopy(errmsg,
1812191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1813191673Sjamie					   errmsg_len);
1814191673Sjamie				else
1815191673Sjamie					copyout(errmsg,
1816191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1817191673Sjamie					   errmsg_len);
1818191673Sjamie			}
1819191673Sjamie		}
1820191673Sjamie	}
1821191673Sjamie done_free:
1822191673Sjamie#ifdef INET
1823191673Sjamie	free(ip4, M_PRISON);
1824191673Sjamie#endif
1825191673Sjamie#ifdef INET6
1826191673Sjamie	free(ip6, M_PRISON);
1827191673Sjamie#endif
1828230407Smm	if (g_path != NULL)
1829230407Smm		free(g_path, M_TEMP);
1830191673Sjamie	vfs_freeopts(opts);
1831191673Sjamie	return (error);
1832191673Sjamie}
1833191673Sjamie
1834191673Sjamie
183582710Sdillon/*
1836191673Sjamie * struct jail_get_args {
1837191673Sjamie *	struct iovec *iovp;
1838191673Sjamie *	unsigned int iovcnt;
1839191673Sjamie *	int flags;
1840114168Smike * };
184182710Sdillon */
184246155Sphkint
1843225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap)
184446155Sphk{
1845191673Sjamie	struct uio *auio;
1846185435Sbz	int error;
1847185435Sbz
1848191673Sjamie	/* Check that we have an even number of iovecs. */
1849191673Sjamie	if (uap->iovcnt & 1)
1850191673Sjamie		return (EINVAL);
1851191673Sjamie
1852191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1853185435Sbz	if (error)
1854185435Sbz		return (error);
1855191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1856191673Sjamie	if (error == 0)
1857191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1858191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1859191673Sjamie	free(auio, M_IOV);
1860191673Sjamie	return (error);
1861191673Sjamie}
1862185435Sbz
1863191673Sjamieint
1864191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
1865191673Sjamie{
1866192895Sjamie	struct prison *pr, *mypr;
1867191673Sjamie	struct vfsopt *opt;
1868191673Sjamie	struct vfsoptlist *opts;
1869191673Sjamie	char *errmsg, *name;
1870192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1871185435Sbz
1872191673Sjamie	if (flags & ~JAIL_GET_MASK)
1873191673Sjamie		return (EINVAL);
1874185435Sbz
1875191673Sjamie	/* Get the parameter list. */
1876191673Sjamie	error = vfs_buildopts(optuio, &opts);
1877191673Sjamie	if (error)
1878191673Sjamie		return (error);
1879191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1880192895Sjamie	mypr = td->td_ucred->cr_prison;
1881185435Sbz
1882191673Sjamie	/*
1883191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
1884191673Sjamie	 */
1885191673Sjamie	sx_slock(&allprison_lock);
1886191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1887191673Sjamie	if (error == 0) {
1888191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
1889192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1890191673Sjamie				mtx_lock(&pr->pr_mtx);
1891191673Sjamie				if (pr->pr_ref > 0 &&
1892191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1893191673Sjamie					break;
1894191673Sjamie				mtx_unlock(&pr->pr_mtx);
1895191673Sjamie			}
1896191673Sjamie		}
1897191673Sjamie		if (pr != NULL)
1898191673Sjamie			goto found_prison;
1899191673Sjamie		error = ENOENT;
1900191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
1901191673Sjamie		goto done_unlock_list;
1902191673Sjamie	} else if (error != ENOENT)
1903191673Sjamie		goto done_unlock_list;
1904185435Sbz
1905191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1906191673Sjamie	if (error == 0) {
1907191673Sjamie		if (jid != 0) {
1908192895Sjamie			pr = prison_find_child(mypr, jid);
1909191673Sjamie			if (pr != NULL) {
1910191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1911191673Sjamie					mtx_unlock(&pr->pr_mtx);
1912191673Sjamie					error = ENOENT;
1913191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1914191673Sjamie					    jid);
1915191673Sjamie					goto done_unlock_list;
1916191673Sjamie				}
1917191673Sjamie				goto found_prison;
1918191673Sjamie			}
1919191673Sjamie			error = ENOENT;
1920191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
1921191673Sjamie			goto done_unlock_list;
1922191673Sjamie		}
1923191673Sjamie	} else if (error != ENOENT)
1924191673Sjamie		goto done_unlock_list;
192546155Sphk
1926191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
1927191673Sjamie	if (error == 0) {
1928191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
1929191673Sjamie			error = EINVAL;
1930191673Sjamie			goto done_unlock_list;
1931191673Sjamie		}
1932192895Sjamie		pr = prison_find_name(mypr, name);
1933191673Sjamie		if (pr != NULL) {
1934191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1935191673Sjamie				mtx_unlock(&pr->pr_mtx);
1936191673Sjamie				error = ENOENT;
1937191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
1938191673Sjamie				    name);
1939191673Sjamie				goto done_unlock_list;
1940191673Sjamie			}
1941191673Sjamie			goto found_prison;
1942191673Sjamie		}
1943191673Sjamie		error = ENOENT;
1944191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
1945191673Sjamie		goto done_unlock_list;
1946191673Sjamie	} else if (error != ENOENT)
1947191673Sjamie		goto done_unlock_list;
1948185435Sbz
1949191673Sjamie	vfs_opterror(opts, "no jail specified");
1950191673Sjamie	error = ENOENT;
1951191673Sjamie	goto done_unlock_list;
1952191673Sjamie
1953191673Sjamie found_prison:
1954191673Sjamie	/* Get the parameters of the prison. */
1955191673Sjamie	pr->pr_ref++;
1956191673Sjamie	locked = PD_LOCKED;
1957191673Sjamie	td->td_retval[0] = pr->pr_id;
1958191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1959191673Sjamie	if (error != 0 && error != ENOENT)
1960191673Sjamie		goto done_deref;
1961192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1962192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1963191673Sjamie	if (error != 0 && error != ENOENT)
1964191673Sjamie		goto done_deref;
1965192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1966192895Sjamie	if (error != 0 && error != ENOENT)
1967192895Sjamie		goto done_deref;
1968192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1969191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
1970191673Sjamie	if (error != 0 && error != ENOENT)
1971191673Sjamie		goto done_deref;
1972192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1973191673Sjamie	if (error != 0 && error != ENOENT)
1974191673Sjamie		goto done_deref;
1975191673Sjamie#ifdef INET
1976191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1977191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1978191673Sjamie	if (error != 0 && error != ENOENT)
1979191673Sjamie		goto done_deref;
1980191673Sjamie#endif
1981191673Sjamie#ifdef INET6
1982191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1983191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1984191673Sjamie	if (error != 0 && error != ENOENT)
1985191673Sjamie		goto done_deref;
1986191673Sjamie#endif
1987191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1988191673Sjamie	    sizeof(pr->pr_securelevel));
1989191673Sjamie	if (error != 0 && error != ENOENT)
1990191673Sjamie		goto done_deref;
1991194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1992194762Sjamie	    sizeof(pr->pr_childcount));
1993194762Sjamie	if (error != 0 && error != ENOENT)
1994194762Sjamie		goto done_deref;
1995194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1996194762Sjamie	    sizeof(pr->pr_childmax));
1997194762Sjamie	if (error != 0 && error != ENOENT)
1998194762Sjamie		goto done_deref;
1999194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2000191673Sjamie	if (error != 0 && error != ENOENT)
2001191673Sjamie		goto done_deref;
2002194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2003193066Sjamie	if (error != 0 && error != ENOENT)
2004193066Sjamie		goto done_deref;
2005194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2006193066Sjamie	if (error != 0 && error != ENOENT)
2007193066Sjamie		goto done_deref;
2008205014Snwhitehorn#ifdef COMPAT_FREEBSD32
2009217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2010193066Sjamie		uint32_t hid32 = pr->pr_hostid;
2011193066Sjamie
2012193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2013193066Sjamie	} else
2014193066Sjamie#endif
2015193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2016193066Sjamie	    sizeof(pr->pr_hostid));
2017193066Sjamie	if (error != 0 && error != ENOENT)
2018193066Sjamie		goto done_deref;
2019192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2020192895Sjamie	    sizeof(pr->pr_enforce_statfs));
2021191673Sjamie	if (error != 0 && error != ENOENT)
2022191673Sjamie		goto done_deref;
2023192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2024192895Sjamie	    fi++) {
2025192895Sjamie		if (pr_flag_names[fi] == NULL)
2026192895Sjamie			continue;
2027192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2028192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2029192895Sjamie		if (error != 0 && error != ENOENT)
2030192895Sjamie			goto done_deref;
2031192895Sjamie		i = !i;
2032192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2033192895Sjamie		if (error != 0 && error != ENOENT)
2034192895Sjamie			goto done_deref;
2035192895Sjamie	}
2036195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2037195870Sjamie	    fi++) {
2038195870Sjamie		i = pr->pr_flags &
2039195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2040195870Sjamie		i = pr_flag_jailsys[fi].disable &&
2041195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2042195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2043195870Sjamie		    : JAIL_SYS_INHERIT;
2044195870Sjamie		error =
2045195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2046195870Sjamie		if (error != 0 && error != ENOENT)
2047195870Sjamie			goto done_deref;
2048195870Sjamie	}
2049192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2050192895Sjamie	    fi++) {
2051192895Sjamie		if (pr_allow_names[fi] == NULL)
2052192895Sjamie			continue;
2053192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2054192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2055192895Sjamie		if (error != 0 && error != ENOENT)
2056192895Sjamie			goto done_deref;
2057192895Sjamie		i = !i;
2058192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2059192895Sjamie		if (error != 0 && error != ENOENT)
2060192895Sjamie			goto done_deref;
2061192895Sjamie	}
2062191673Sjamie	i = (pr->pr_uref == 0);
2063191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2064191673Sjamie	if (error != 0 && error != ENOENT)
2065191673Sjamie		goto done_deref;
2066191673Sjamie	i = !i;
2067191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2068191673Sjamie	if (error != 0 && error != ENOENT)
2069191673Sjamie		goto done_deref;
2070191673Sjamie
2071191673Sjamie	/* Get the module parameters. */
2072191673Sjamie	mtx_unlock(&pr->pr_mtx);
2073191673Sjamie	locked = 0;
2074191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
207546155Sphk	if (error)
2076191673Sjamie		goto done_deref;
2077191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
207884828Sjhb
2079191673Sjamie	/* By now, all parameters should have been noted. */
2080191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2081191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2082191673Sjamie			error = EINVAL;
2083191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2084191673Sjamie			goto done_errmsg;
2085191673Sjamie		}
2086185435Sbz	}
2087191673Sjamie
2088191673Sjamie	/* Write the fetched parameters back to userspace. */
2089191673Sjamie	error = 0;
2090191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2091191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2092191673Sjamie			pos = 2 * opt->pos + 1;
2093191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2094191673Sjamie			if (opt->value != NULL) {
2095191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2096191673Sjamie					bcopy(opt->value,
2097191673Sjamie					    optuio->uio_iov[pos].iov_base,
2098191673Sjamie					    opt->len);
2099191673Sjamie				} else {
2100191673Sjamie					error = copyout(opt->value,
2101191673Sjamie					    optuio->uio_iov[pos].iov_base,
2102191673Sjamie					    opt->len);
2103191673Sjamie					if (error)
2104191673Sjamie						break;
2105191673Sjamie				}
2106191673Sjamie			}
2107191673Sjamie		}
2108185435Sbz	}
2109191673Sjamie	goto done_errmsg;
2110191673Sjamie
2111191673Sjamie done_deref:
2112191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2113191673Sjamie	goto done_errmsg;
2114191673Sjamie
2115191673Sjamie done_unlock_list:
2116191673Sjamie	sx_sunlock(&allprison_lock);
2117191673Sjamie done_errmsg:
2118191673Sjamie	if (error && errmsg_pos >= 0) {
2119191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2120191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2121191673Sjamie		if (errmsg_len > 0) {
2122191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2123191673Sjamie				bcopy(errmsg,
2124191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2125191673Sjamie				    errmsg_len);
2126191673Sjamie			else
2127191673Sjamie				copyout(errmsg,
2128191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2129191673Sjamie				    errmsg_len);
2130191673Sjamie		}
2131185435Sbz	}
2132191673Sjamie	vfs_freeopts(opts);
2133191673Sjamie	return (error);
2134191673Sjamie}
2135113275Smike
2136192895Sjamie
2137191673Sjamie/*
2138191673Sjamie * struct jail_remove_args {
2139191673Sjamie *	int jid;
2140191673Sjamie * };
2141191673Sjamie */
2142191673Sjamieint
2143225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2144191673Sjamie{
2145192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2146192895Sjamie	int descend, error;
2147185435Sbz
2148191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2149185435Sbz	if (error)
2150191673Sjamie		return (error);
2151185435Sbz
2152185435Sbz	sx_xlock(&allprison_lock);
2153192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2154191673Sjamie	if (pr == NULL) {
2155185435Sbz		sx_xunlock(&allprison_lock);
2156191673Sjamie		return (EINVAL);
2157185435Sbz	}
2158185435Sbz
2159192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2160192895Sjamie	pr->pr_ref++;
2161192895Sjamie	pr->pr_flags |= PR_REMOVE;
2162192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2163192895Sjamie		mtx_unlock(&pr->pr_mtx);
2164192895Sjamie		lpr = NULL;
2165192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2166192895Sjamie			mtx_lock(&cpr->pr_mtx);
2167192895Sjamie			if (cpr->pr_ref > 0) {
2168192895Sjamie				tpr = cpr;
2169192895Sjamie				cpr->pr_ref++;
2170192895Sjamie				cpr->pr_flags |= PR_REMOVE;
2171192895Sjamie			} else {
2172192895Sjamie				/* Already removed - do not do it again. */
2173192895Sjamie				tpr = NULL;
2174192895Sjamie			}
2175192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2176192895Sjamie			if (lpr != NULL) {
2177192895Sjamie				mtx_lock(&lpr->pr_mtx);
2178192895Sjamie				prison_remove_one(lpr);
2179192895Sjamie				sx_xlock(&allprison_lock);
2180192895Sjamie			}
2181192895Sjamie			lpr = tpr;
2182192895Sjamie		}
2183192895Sjamie		if (lpr != NULL) {
2184192895Sjamie			mtx_lock(&lpr->pr_mtx);
2185192895Sjamie			prison_remove_one(lpr);
2186192895Sjamie			sx_xlock(&allprison_lock);
2187192895Sjamie		}
2188192895Sjamie		mtx_lock(&pr->pr_mtx);
2189192895Sjamie	}
2190192895Sjamie	prison_remove_one(pr);
2191192895Sjamie	return (0);
2192192895Sjamie}
2193192895Sjamie
2194192895Sjamiestatic void
2195192895Sjamieprison_remove_one(struct prison *pr)
2196192895Sjamie{
2197192895Sjamie	struct proc *p;
2198192895Sjamie	int deuref;
2199192895Sjamie
2200191673Sjamie	/* If the prison was persistent, it is not anymore. */
2201191673Sjamie	deuref = 0;
2202191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2203191673Sjamie		pr->pr_ref--;
2204191673Sjamie		deuref = PD_DEUREF;
2205191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2206179881Sdelphij	}
2207113275Smike
2208192895Sjamie	/*
2209192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2210192895Sjamie	 * the prison now.
2211192895Sjamie	 */
2212192895Sjamie	KASSERT(pr->pr_ref > 0,
2213192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2214192895Sjamie	if (pr->pr_ref == 1) {
2215191673Sjamie		prison_deref(pr,
2216191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2217192895Sjamie		return;
2218191673Sjamie	}
2219191673Sjamie
2220113275Smike	mtx_unlock(&pr->pr_mtx);
2221191673Sjamie	sx_xunlock(&allprison_lock);
2222191673Sjamie	/*
2223191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2224191673Sjamie	 */
2225191673Sjamie	sx_slock(&allproc_lock);
2226191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2227191673Sjamie		PROC_LOCK(p);
2228191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2229191673Sjamie		    p->p_ucred->cr_prison == pr)
2230225617Skmacy			kern_psignal(p, SIGKILL);
2231191673Sjamie		PROC_UNLOCK(p);
2232191673Sjamie	}
2233191673Sjamie	sx_sunlock(&allproc_lock);
2234192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2235191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2236113275Smike}
2237113275Smike
2238190466Sjamie
2239113275Smike/*
2240114168Smike * struct jail_attach_args {
2241114168Smike *	int jid;
2242114168Smike * };
2243113275Smike */
2244113275Smikeint
2245225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2246113275Smike{
2247113275Smike	struct prison *pr;
2248191673Sjamie	int error;
2249167309Spjd
2250164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2251126023Snectar	if (error)
2252126023Snectar		return (error);
2253126023Snectar
2254168401Spjd	sx_slock(&allprison_lock);
2255192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2256113275Smike	if (pr == NULL) {
2257168401Spjd		sx_sunlock(&allprison_lock);
2258113275Smike		return (EINVAL);
2259113275Smike	}
2260185435Sbz
2261185435Sbz	/*
2262185435Sbz	 * Do not allow a process to attach to a prison that is not
2263191673Sjamie	 * considered to be "alive".
2264185435Sbz	 */
2265191673Sjamie	if (pr->pr_uref == 0) {
2266185435Sbz		mtx_unlock(&pr->pr_mtx);
2267185435Sbz		sx_sunlock(&allprison_lock);
2268185435Sbz		return (EINVAL);
2269185435Sbz	}
2270191673Sjamie
2271191673Sjamie	return (do_jail_attach(td, pr));
2272191673Sjamie}
2273191673Sjamie
2274191673Sjamiestatic int
2275191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2276191673Sjamie{
2277192895Sjamie	struct prison *ppr;
2278191673Sjamie	struct proc *p;
2279191673Sjamie	struct ucred *newcred, *oldcred;
2280191673Sjamie	int vfslocked, error;
2281191673Sjamie
2282191673Sjamie	/*
2283191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2284191673Sjamie	 * in the same privileged process attempt to attach to two
2285191673Sjamie	 * different jails at the same time.  It is important for
2286191673Sjamie	 * user processes not to do this, or they might end up with
2287191673Sjamie	 * a process root from one prison, but attached to the jail
2288191673Sjamie	 * of another.
2289191673Sjamie	 */
2290113275Smike	pr->pr_ref++;
2291191673Sjamie	pr->pr_uref++;
2292113275Smike	mtx_unlock(&pr->pr_mtx);
2293191673Sjamie
2294191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2295191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2296191673Sjamie	if (error) {
2297191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2298191673Sjamie		return (error);
2299191673Sjamie	}
2300168401Spjd	sx_sunlock(&allprison_lock);
2301113275Smike
2302185435Sbz	/*
2303185435Sbz	 * Reparent the newly attached process to this jail.
2304185435Sbz	 */
2305192895Sjamie	ppr = td->td_ucred->cr_prison;
2306191673Sjamie	p = td->td_proc;
2307185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2308185435Sbz	if (error)
2309191673Sjamie		goto e_revert_osd;
2310185435Sbz
2311150652Scsjp	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2312175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2313113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2314113275Smike		goto e_unlock;
2315113275Smike#ifdef MAC
2316172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2317113275Smike		goto e_unlock;
2318113275Smike#endif
2319175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2320191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2321191673Sjamie		goto e_unlock_giant;
2322150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2323113275Smike
232484828Sjhb	newcred = crget();
232584828Sjhb	PROC_LOCK(p);
232684828Sjhb	oldcred = p->p_ucred;
2327113275Smike	setsugid(p);
232884828Sjhb	crcopy(newcred, oldcred);
2329113630Sjhb	newcred->cr_prison = pr;
233084828Sjhb	p->p_ucred = newcred;
233184828Sjhb	PROC_UNLOCK(p);
2332220137Strasz#ifdef RACCT
2333220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
2334220137Strasz#endif
233584828Sjhb	crfree(oldcred);
2336192895Sjamie	prison_deref(ppr, PD_DEREF | PD_DEUREF);
233746155Sphk	return (0);
2338191673Sjamie e_unlock:
2339175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2340191673Sjamie e_unlock_giant:
2341150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2342191673Sjamie e_revert_osd:
2343191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2344192895Sjamie	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2345191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
234646155Sphk	return (error);
234746155Sphk}
234846155Sphk
2349192895Sjamie
2350113275Smike/*
2351113275Smike * Returns a locked prison instance, or NULL on failure.
2352113275Smike */
2353168399Spjdstruct prison *
2354113275Smikeprison_find(int prid)
2355113275Smike{
2356113275Smike	struct prison *pr;
2357113275Smike
2358168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2359191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2360113275Smike		if (pr->pr_id == prid) {
2361113275Smike			mtx_lock(&pr->pr_mtx);
2362191673Sjamie			if (pr->pr_ref > 0)
2363191673Sjamie				return (pr);
2364191673Sjamie			mtx_unlock(&pr->pr_mtx);
2365113275Smike		}
2366113275Smike	}
2367113275Smike	return (NULL);
2368113275Smike}
2369113275Smike
2370191673Sjamie/*
2371192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2372191673Sjamie */
2373191673Sjamiestruct prison *
2374192895Sjamieprison_find_child(struct prison *mypr, int prid)
2375191673Sjamie{
2376192895Sjamie	struct prison *pr;
2377192895Sjamie	int descend;
2378192895Sjamie
2379192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2380192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2381192895Sjamie		if (pr->pr_id == prid) {
2382192895Sjamie			mtx_lock(&pr->pr_mtx);
2383192895Sjamie			if (pr->pr_ref > 0)
2384192895Sjamie				return (pr);
2385192895Sjamie			mtx_unlock(&pr->pr_mtx);
2386192895Sjamie		}
2387192895Sjamie	}
2388192895Sjamie	return (NULL);
2389192895Sjamie}
2390192895Sjamie
2391192895Sjamie/*
2392192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2393192895Sjamie */
2394192895Sjamiestruct prison *
2395192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2396192895Sjamie{
2397191673Sjamie	struct prison *pr, *deadpr;
2398192895Sjamie	size_t mylen;
2399192895Sjamie	int descend;
2400191673Sjamie
2401191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2402192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2403191673Sjamie again:
2404191673Sjamie	deadpr = NULL;
2405192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2406192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2407191673Sjamie			mtx_lock(&pr->pr_mtx);
2408191673Sjamie			if (pr->pr_ref > 0) {
2409191673Sjamie				if (pr->pr_uref > 0)
2410191673Sjamie					return (pr);
2411191673Sjamie				deadpr = pr;
2412191673Sjamie			}
2413191673Sjamie			mtx_unlock(&pr->pr_mtx);
2414191673Sjamie		}
2415191673Sjamie	}
2416192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2417191673Sjamie	if (deadpr != NULL) {
2418191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2419191673Sjamie		if (deadpr->pr_ref == 0) {
2420191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2421191673Sjamie			goto again;
2422191673Sjamie		}
2423191673Sjamie	}
2424191673Sjamie	return (deadpr);
2425191673Sjamie}
2426191673Sjamie
2427191673Sjamie/*
2428192895Sjamie * See if a prison has the specific flag set.
2429192895Sjamie */
2430192895Sjamieint
2431192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2432192895Sjamie{
2433192895Sjamie
2434192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2435192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2436192895Sjamie}
2437192895Sjamie
2438192895Sjamieint
2439192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2440192895Sjamie{
2441192895Sjamie
2442192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2443192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2444192895Sjamie}
2445192895Sjamie
2446192895Sjamie/*
2447191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2448191673Sjamie * prison itself - but not in this context in case there are locks held.
2449191673Sjamie */
245072786Srwatsonvoid
2451185029Spjdprison_free_locked(struct prison *pr)
245272786Srwatson{
245372786Srwatson
2454185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
245572786Srwatson	pr->pr_ref--;
245672786Srwatson	if (pr->pr_ref == 0) {
2457168483Spjd		mtx_unlock(&pr->pr_mtx);
2458124882Srwatson		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2459144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
246087275Srwatson		return;
246172786Srwatson	}
246287275Srwatson	mtx_unlock(&pr->pr_mtx);
246372786Srwatson}
246472786Srwatson
2465185029Spjdvoid
2466185029Spjdprison_free(struct prison *pr)
2467185029Spjd{
2468185029Spjd
2469185029Spjd	mtx_lock(&pr->pr_mtx);
2470185029Spjd	prison_free_locked(pr);
2471185029Spjd}
2472185029Spjd
2473124882Srwatsonstatic void
2474124882Srwatsonprison_complete(void *context, int pending)
2475124882Srwatson{
2476191673Sjamie
2477191673Sjamie	prison_deref((struct prison *)context, 0);
2478191673Sjamie}
2479191673Sjamie
2480191673Sjamie/*
2481191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2482191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2483191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2484191673Sjamie * and the allprison lock are both unlocked.
2485191673Sjamie */
2486191673Sjamiestatic void
2487191673Sjamieprison_deref(struct prison *pr, int flags)
2488191673Sjamie{
2489192895Sjamie	struct prison *ppr, *tpr;
2490150652Scsjp	int vfslocked;
2491124882Srwatson
2492191673Sjamie	if (!(flags & PD_LOCKED))
2493191673Sjamie		mtx_lock(&pr->pr_mtx);
2494225191Sjamie	for (;;) {
2495225191Sjamie		if (flags & PD_DEUREF) {
2496225191Sjamie			pr->pr_uref--;
2497225191Sjamie			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2498192895Sjamie		}
2499192895Sjamie		if (flags & PD_DEREF)
2500192895Sjamie			pr->pr_ref--;
2501192895Sjamie		/* If the prison still has references, nothing else to do. */
2502192895Sjamie		if (pr->pr_ref > 0) {
2503192895Sjamie			mtx_unlock(&pr->pr_mtx);
2504192895Sjamie			if (flags & PD_LIST_SLOCKED)
2505192895Sjamie				sx_sunlock(&allprison_lock);
2506192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2507192895Sjamie				sx_xunlock(&allprison_lock);
2508192895Sjamie			return;
2509191673Sjamie		}
2510191673Sjamie
2511192895Sjamie		mtx_unlock(&pr->pr_mtx);
2512192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2513192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2514192895Sjamie				sx_sunlock(&allprison_lock);
2515192895Sjamie				sx_xlock(&allprison_lock);
2516192895Sjamie			}
2517192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2518192895Sjamie			sx_xlock(&allprison_lock);
2519168489Spjd
2520192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2521192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2522192895Sjamie		ppr = pr->pr_parent;
2523192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2524194762Sjamie			tpr->pr_childcount--;
2525196592Sjamie		sx_xunlock(&allprison_lock);
2526192895Sjamie
2527194251Sjamie#ifdef VIMAGE
2528196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2529194251Sjamie			vnet_destroy(pr->pr_vnet);
2530194251Sjamie#endif
2531192895Sjamie		if (pr->pr_root != NULL) {
2532192895Sjamie			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2533192895Sjamie			vrele(pr->pr_root);
2534192895Sjamie			VFS_UNLOCK_GIANT(vfslocked);
2535192895Sjamie		}
2536192895Sjamie		mtx_destroy(&pr->pr_mtx);
2537191673Sjamie#ifdef INET
2538192895Sjamie		free(pr->pr_ip4, M_PRISON);
2539191673Sjamie#endif
2540185435Sbz#ifdef INET6
2541192895Sjamie		free(pr->pr_ip6, M_PRISON);
2542185435Sbz#endif
2543192895Sjamie		if (pr->pr_cpuset != NULL)
2544192895Sjamie			cpuset_rel(pr->pr_cpuset);
2545192895Sjamie		osd_jail_exit(pr);
2546221362Strasz#ifdef RACCT
2547221362Strasz		prison_racct_detach(pr);
2548220163Strasz#endif
2549192895Sjamie		free(pr, M_PRISON);
2550192895Sjamie
2551192895Sjamie		/* Removing a prison frees a reference on its parent. */
2552192895Sjamie		pr = ppr;
2553192895Sjamie		mtx_lock(&pr->pr_mtx);
2554225191Sjamie		flags = PD_DEREF | PD_DEUREF;
2555192895Sjamie	}
2556124882Srwatson}
2557124882Srwatson
255872786Srwatsonvoid
2559185029Spjdprison_hold_locked(struct prison *pr)
256072786Srwatson{
256172786Srwatson
2562185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2563168489Spjd	KASSERT(pr->pr_ref > 0,
2564191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
256572786Srwatson	pr->pr_ref++;
2566185029Spjd}
2567185029Spjd
2568185029Spjdvoid
2569185029Spjdprison_hold(struct prison *pr)
2570185029Spjd{
2571185029Spjd
2572185029Spjd	mtx_lock(&pr->pr_mtx);
2573185029Spjd	prison_hold_locked(pr);
257487275Srwatson	mtx_unlock(&pr->pr_mtx);
257572786Srwatson}
257672786Srwatson
2577185435Sbzvoid
2578185435Sbzprison_proc_hold(struct prison *pr)
257987275Srwatson{
258087275Srwatson
2581185435Sbz	mtx_lock(&pr->pr_mtx);
2582191673Sjamie	KASSERT(pr->pr_uref > 0,
2583191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2584191673Sjamie	pr->pr_uref++;
2585185435Sbz	mtx_unlock(&pr->pr_mtx);
258687275Srwatson}
258787275Srwatson
2588185435Sbzvoid
2589185435Sbzprison_proc_free(struct prison *pr)
2590185435Sbz{
2591185435Sbz
2592185435Sbz	mtx_lock(&pr->pr_mtx);
2593191673Sjamie	KASSERT(pr->pr_uref > 0,
2594191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2595191673Sjamie	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2596185435Sbz}
2597185435Sbz
2598185435Sbz
2599185435Sbz#ifdef INET
2600185435Sbz/*
2601192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2602192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2603192895Sjamie */
2604192895Sjamiestatic int
2605192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2606192895Sjamie{
2607192895Sjamie	int ii, ij, used;
2608192895Sjamie	struct prison *ppr;
2609192895Sjamie
2610192895Sjamie	ppr = pr->pr_parent;
2611192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2612192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2613192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2614192895Sjamie			/*
2615192895Sjamie			 * There's no room for the parent's list.  Use the
2616192895Sjamie			 * new list buffer, which is assumed to be big enough
2617192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2618192895Sjamie			 * allocate one.
2619192895Sjamie			 */
2620192895Sjamie			used = 1;
2621192895Sjamie			if (newip4 == NULL) {
2622192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2623192895Sjamie				    M_PRISON, M_NOWAIT);
2624192895Sjamie				if (newip4 != NULL)
2625192895Sjamie					used = 0;
2626192895Sjamie			}
2627192895Sjamie			if (newip4 != NULL) {
2628192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2629192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2630192895Sjamie				free(pr->pr_ip4, M_PRISON);
2631192895Sjamie				pr->pr_ip4 = newip4;
2632192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2633192895Sjamie			}
2634192895Sjamie			return (used);
2635192895Sjamie		}
2636192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2637192895Sjamie		if (pr->pr_ip4s > 0)
2638192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2639192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2640192895Sjamie		else if (pr->pr_ip4 != NULL) {
2641192895Sjamie			free(pr->pr_ip4, M_PRISON);
2642192895Sjamie			pr->pr_ip4 = NULL;
2643192895Sjamie		}
2644195974Sjamie	} else if (pr->pr_ip4s > 0) {
2645192895Sjamie		/* Remove addresses that aren't in the parent. */
2646192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2647192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2648192895Sjamie				break;
2649192895Sjamie		if (ij < ppr->pr_ip4s)
2650192895Sjamie			ii = 1;
2651192895Sjamie		else {
2652192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2653192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2654192895Sjamie			ii = 0;
2655192895Sjamie		}
2656192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2657192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2658192895Sjamie				ii++;
2659192895Sjamie				continue;
2660192895Sjamie			}
2661192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2662192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2663192895Sjamie			case -1:
2664192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2665192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2666192895Sjamie				break;
2667192895Sjamie			case 0:
2668192895Sjamie				ii++;
2669192895Sjamie				ij++;
2670192895Sjamie				break;
2671192895Sjamie			case 1:
2672192895Sjamie				ij++;
2673192895Sjamie				break;
2674192895Sjamie			}
2675192895Sjamie		}
2676192895Sjamie		if (pr->pr_ip4s == 0) {
2677195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2678192895Sjamie			free(pr->pr_ip4, M_PRISON);
2679192895Sjamie			pr->pr_ip4 = NULL;
2680192895Sjamie		}
2681192895Sjamie	}
2682192895Sjamie	return (0);
2683192895Sjamie}
2684192895Sjamie
2685192895Sjamie/*
2686185435Sbz * Pass back primary IPv4 address of this jail.
2687185435Sbz *
2688192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2689192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2690185435Sbz *
2691188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2692188144Sjamie * Address returned in NBO.
2693185435Sbz */
269446155Sphkint
2695187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
269646155Sphk{
2697191673Sjamie	struct prison *pr;
269846155Sphk
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
2715191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2716191673Sjamie	mtx_unlock(&pr->pr_mtx);
2717185435Sbz	return (0);
2718185435Sbz}
2719185435Sbz
2720185435Sbz/*
2721202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2722202468Sbz * We will return 0 if we should bypass source address selection in favour
2723202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2724202468Sbz * returned in NBO.
2725202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2726202468Sbz */
2727202468Sbzint
2728202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2729202468Sbz{
2730202468Sbz	struct prison *pr;
2731202468Sbz	struct in_addr lia;
2732202468Sbz	int error;
2733202468Sbz
2734202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2735202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2736202468Sbz
2737202468Sbz	if (!jailed(cred))
2738202468Sbz		return (1);
2739202468Sbz
2740202468Sbz	pr = cred->cr_prison;
2741202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2742202468Sbz		return (1);
2743202468Sbz
2744202468Sbz	lia.s_addr = INADDR_ANY;
2745202468Sbz	error = prison_get_ip4(cred, &lia);
2746202468Sbz	if (error)
2747202468Sbz		return (error);
2748202468Sbz	if (lia.s_addr == INADDR_ANY)
2749202468Sbz		return (1);
2750202468Sbz
2751202468Sbz	ia->s_addr = lia.s_addr;
2752202468Sbz	return (0);
2753202468Sbz}
2754202468Sbz
2755202468Sbz/*
2756192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2757192895Sjamie */
2758192895Sjamieint
2759192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2760192895Sjamie{
2761192895Sjamie
2762192895Sjamie	if (pr1 == pr2)
2763192895Sjamie		return (1);
2764192895Sjamie
2765192895Sjamie	/*
2766195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2767195974Sjamie	 * existing prisons.
2768192895Sjamie	 */
2769195945Sjamie	while (pr1 != &prison0 &&
2770195945Sjamie#ifdef VIMAGE
2771195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2772195945Sjamie#endif
2773195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2774192895Sjamie		pr1 = pr1->pr_parent;
2775195945Sjamie	while (pr2 != &prison0 &&
2776195945Sjamie#ifdef VIMAGE
2777195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2778195945Sjamie#endif
2779195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2780192895Sjamie		pr2 = pr2->pr_parent;
2781192895Sjamie	return (pr1 == pr2);
2782192895Sjamie}
2783192895Sjamie
2784192895Sjamie/*
2785185435Sbz * Make sure our (source) address is set to something meaningful to this
2786185435Sbz * jail.
2787185435Sbz *
2788192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2789192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2790192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2791185435Sbz */
2792185435Sbzint
2793185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2794185435Sbz{
2795191673Sjamie	struct prison *pr;
2796185435Sbz	struct in_addr ia0;
2797191673Sjamie	int error;
2798185435Sbz
2799185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2800185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2801185435Sbz
2802192895Sjamie	pr = cred->cr_prison;
2803192895Sjamie	if (!(pr->pr_flags & PR_IP4))
280446155Sphk		return (0);
2805191673Sjamie	mtx_lock(&pr->pr_mtx);
2806192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2807192895Sjamie		mtx_unlock(&pr->pr_mtx);
2808192895Sjamie		return (0);
2809192895Sjamie	}
2810191673Sjamie	if (pr->pr_ip4 == NULL) {
2811191673Sjamie		mtx_unlock(&pr->pr_mtx);
2812188144Sjamie		return (EAFNOSUPPORT);
2813191673Sjamie	}
2814185435Sbz
2815185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
2816185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
2817191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2818191673Sjamie		mtx_unlock(&pr->pr_mtx);
2819185435Sbz		return (0);
282046155Sphk	}
2821185435Sbz
2822188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
2823188144Sjamie		/*
2824188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
2825188144Sjamie		 */
2826191673Sjamie		if (pr->pr_ip4s == 1)
2827191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
2828191673Sjamie		mtx_unlock(&pr->pr_mtx);
2829185435Sbz		return (0);
2830185435Sbz	}
2831185435Sbz
2832191673Sjamie	error = _prison_check_ip4(pr, ia);
2833191673Sjamie	mtx_unlock(&pr->pr_mtx);
2834191673Sjamie	return (error);
2835185435Sbz}
2836185435Sbz
2837185435Sbz/*
2838185435Sbz * Rewrite destination address in case we will connect to loopback address.
2839185435Sbz *
2840188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2841188144Sjamie * Address passed in in NBO and returned in NBO.
2842185435Sbz */
2843185435Sbzint
2844185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2845185435Sbz{
2846191673Sjamie	struct prison *pr;
2847185435Sbz
2848185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2849185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2850185435Sbz
2851192895Sjamie	pr = cred->cr_prison;
2852192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2853185435Sbz		return (0);
2854191673Sjamie	mtx_lock(&pr->pr_mtx);
2855192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2856192895Sjamie		mtx_unlock(&pr->pr_mtx);
2857192895Sjamie		return (0);
2858192895Sjamie	}
2859191673Sjamie	if (pr->pr_ip4 == NULL) {
2860191673Sjamie		mtx_unlock(&pr->pr_mtx);
2861188144Sjamie		return (EAFNOSUPPORT);
2862191673Sjamie	}
2863188144Sjamie
2864185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2865191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2866191673Sjamie		mtx_unlock(&pr->pr_mtx);
2867185435Sbz		return (0);
2868185435Sbz	}
2869185435Sbz
2870185435Sbz	/*
2871185435Sbz	 * Return success because nothing had to be changed.
2872185435Sbz	 */
2873191673Sjamie	mtx_unlock(&pr->pr_mtx);
2874185435Sbz	return (0);
2875185435Sbz}
2876185435Sbz
2877185435Sbz/*
2878188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
2879185435Sbz *
2880192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2881192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2882192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
2883185435Sbz */
2884185435Sbzstatic int
2885185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2886185435Sbz{
2887185435Sbz	int i, a, z, d;
2888185435Sbz
2889185435Sbz	/*
2890185435Sbz	 * Check the primary IP.
2891185435Sbz	 */
2892185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2893188144Sjamie		return (0);
2894185435Sbz
2895185435Sbz	/*
2896185435Sbz	 * All the other IPs are sorted so we can do a binary search.
2897185435Sbz	 */
2898185435Sbz	a = 0;
2899185435Sbz	z = pr->pr_ip4s - 2;
2900185435Sbz	while (a <= z) {
2901185435Sbz		i = (a + z) / 2;
2902185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2903185435Sbz		if (d > 0)
2904185435Sbz			z = i - 1;
2905185435Sbz		else if (d < 0)
2906185435Sbz			a = i + 1;
290781114Srwatson		else
2908188144Sjamie			return (0);
2909185435Sbz	}
2910188144Sjamie
2911188144Sjamie	return (EADDRNOTAVAIL);
2912185435Sbz}
2913185435Sbz
2914185435Sbzint
2915185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
2916185435Sbz{
2917191673Sjamie	struct prison *pr;
2918191673Sjamie	int error;
2919185435Sbz
2920185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2921185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2922185435Sbz
2923192895Sjamie	pr = cred->cr_prison;
2924192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2925188144Sjamie		return (0);
2926191673Sjamie	mtx_lock(&pr->pr_mtx);
2927192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2928192895Sjamie		mtx_unlock(&pr->pr_mtx);
2929192895Sjamie		return (0);
2930192895Sjamie	}
2931191673Sjamie	if (pr->pr_ip4 == NULL) {
2932191673Sjamie		mtx_unlock(&pr->pr_mtx);
2933188144Sjamie		return (EAFNOSUPPORT);
2934191673Sjamie	}
2935185435Sbz
2936191673Sjamie	error = _prison_check_ip4(pr, ia);
2937191673Sjamie	mtx_unlock(&pr->pr_mtx);
2938191673Sjamie	return (error);
2939185435Sbz}
2940185435Sbz#endif
2941185435Sbz
2942185435Sbz#ifdef INET6
2943192895Sjamiestatic int
2944192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2945192895Sjamie{
2946192895Sjamie	int ii, ij, used;
2947192895Sjamie	struct prison *ppr;
2948192895Sjamie
2949192895Sjamie	ppr = pr->pr_parent;
2950192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
2951192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2952192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
2953192895Sjamie			/*
2954192895Sjamie			 * There's no room for the parent's list.  Use the
2955192895Sjamie			 * new list buffer, which is assumed to be big enough
2956192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2957192895Sjamie			 * allocate one.
2958192895Sjamie			 */
2959192895Sjamie			used = 1;
2960192895Sjamie			if (newip6 == NULL) {
2961192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2962192895Sjamie				    M_PRISON, M_NOWAIT);
2963192895Sjamie				if (newip6 != NULL)
2964192895Sjamie					used = 0;
2965192895Sjamie			}
2966192895Sjamie			if (newip6 != NULL) {
2967192895Sjamie				bcopy(ppr->pr_ip6, newip6,
2968192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
2969192895Sjamie				free(pr->pr_ip6, M_PRISON);
2970192895Sjamie				pr->pr_ip6 = newip6;
2971192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
2972192895Sjamie			}
2973192895Sjamie			return (used);
2974192895Sjamie		}
2975192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
2976192895Sjamie		if (pr->pr_ip6s > 0)
2977192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
2978192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
2979192895Sjamie		else if (pr->pr_ip6 != NULL) {
2980192895Sjamie			free(pr->pr_ip6, M_PRISON);
2981192895Sjamie			pr->pr_ip6 = NULL;
2982192895Sjamie		}
2983195974Sjamie	} else if (pr->pr_ip6s > 0) {
2984192895Sjamie		/* Remove addresses that aren't in the parent. */
2985192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
2986192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2987192895Sjamie			    &ppr->pr_ip6[ij]))
2988192895Sjamie				break;
2989192895Sjamie		if (ij < ppr->pr_ip6s)
2990192895Sjamie			ii = 1;
2991192895Sjamie		else {
2992192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2993192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2994192895Sjamie			ii = 0;
2995192895Sjamie		}
2996192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
2997192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2998192895Sjamie			    &ppr->pr_ip6[0])) {
2999192895Sjamie				ii++;
3000192895Sjamie				continue;
3001192895Sjamie			}
3002192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
3003192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3004192895Sjamie			case -1:
3005192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3006192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3007192895Sjamie				break;
3008192895Sjamie			case 0:
3009192895Sjamie				ii++;
3010192895Sjamie				ij++;
3011192895Sjamie				break;
3012192895Sjamie			case 1:
3013192895Sjamie				ij++;
3014192895Sjamie				break;
3015192895Sjamie			}
3016192895Sjamie		}
3017192895Sjamie		if (pr->pr_ip6s == 0) {
3018195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
3019192895Sjamie			free(pr->pr_ip6, M_PRISON);
3020192895Sjamie			pr->pr_ip6 = NULL;
3021192895Sjamie		}
3022192895Sjamie	}
3023192895Sjamie	return 0;
3024192895Sjamie}
3025192895Sjamie
3026185435Sbz/*
3027185435Sbz * Pass back primary IPv6 address for this jail.
3028185435Sbz *
3029192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3030192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3031185435Sbz *
3032188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3033185435Sbz */
3034185435Sbzint
3035187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3036185435Sbz{
3037191673Sjamie	struct prison *pr;
3038185435Sbz
3039185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3040185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3041185435Sbz
3042192895Sjamie	pr = cred->cr_prison;
3043192895Sjamie	if (!(pr->pr_flags & PR_IP6))
304481114Srwatson		return (0);
3045191673Sjamie	mtx_lock(&pr->pr_mtx);
3046192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3047192895Sjamie		mtx_unlock(&pr->pr_mtx);
3048192895Sjamie		return (0);
3049192895Sjamie	}
3050191673Sjamie	if (pr->pr_ip6 == NULL) {
3051191673Sjamie		mtx_unlock(&pr->pr_mtx);
3052188144Sjamie		return (EAFNOSUPPORT);
3053191673Sjamie	}
3054188144Sjamie
3055191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3056191673Sjamie	mtx_unlock(&pr->pr_mtx);
3057185435Sbz	return (0);
3058185435Sbz}
3059185435Sbz
3060185435Sbz/*
3061202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3062202468Sbz * We will return 0 if we should bypass source address selection in favour
3063202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3064202468Sbz * returned in NBO.
3065202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3066202468Sbz */
3067202468Sbzint
3068202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3069202468Sbz{
3070202468Sbz	struct prison *pr;
3071202468Sbz	struct in6_addr lia6;
3072202468Sbz	int error;
3073202468Sbz
3074202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3075202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3076202468Sbz
3077202468Sbz	if (!jailed(cred))
3078202468Sbz		return (1);
3079202468Sbz
3080202468Sbz	pr = cred->cr_prison;
3081202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3082202468Sbz		return (1);
3083202468Sbz
3084202468Sbz	lia6 = in6addr_any;
3085202468Sbz	error = prison_get_ip6(cred, &lia6);
3086202468Sbz	if (error)
3087202468Sbz		return (error);
3088202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3089202468Sbz		return (1);
3090202468Sbz
3091202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3092202468Sbz	return (0);
3093202468Sbz}
3094202468Sbz
3095202468Sbz/*
3096192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3097192895Sjamie */
3098192895Sjamieint
3099192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3100192895Sjamie{
3101192895Sjamie
3102192895Sjamie	if (pr1 == pr2)
3103192895Sjamie		return (1);
3104192895Sjamie
3105195945Sjamie	while (pr1 != &prison0 &&
3106195945Sjamie#ifdef VIMAGE
3107195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3108195945Sjamie#endif
3109195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3110192895Sjamie		pr1 = pr1->pr_parent;
3111195945Sjamie	while (pr2 != &prison0 &&
3112195945Sjamie#ifdef VIMAGE
3113195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3114195945Sjamie#endif
3115195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3116192895Sjamie		pr2 = pr2->pr_parent;
3117192895Sjamie	return (pr1 == pr2);
3118192895Sjamie}
3119192895Sjamie
3120192895Sjamie/*
3121185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3122185435Sbz *
3123185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3124185435Sbz * when needed while binding.
3125185435Sbz *
3126192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3127192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3128192895Sjamie * doesn't allow IPv6.
3129185435Sbz */
3130185435Sbzint
3131185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3132185435Sbz{
3133191673Sjamie	struct prison *pr;
3134191673Sjamie	int error;
3135185435Sbz
3136185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3137185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3138185435Sbz
3139192895Sjamie	pr = cred->cr_prison;
3140192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3141185435Sbz		return (0);
3142191673Sjamie	mtx_lock(&pr->pr_mtx);
3143192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3144192895Sjamie		mtx_unlock(&pr->pr_mtx);
3145192895Sjamie		return (0);
3146192895Sjamie	}
3147191673Sjamie	if (pr->pr_ip6 == NULL) {
3148191673Sjamie		mtx_unlock(&pr->pr_mtx);
3149188144Sjamie		return (EAFNOSUPPORT);
3150191673Sjamie	}
3151188144Sjamie
3152185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3153191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3154191673Sjamie		mtx_unlock(&pr->pr_mtx);
3155185435Sbz		return (0);
315681114Srwatson	}
3157185435Sbz
3158188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3159188144Sjamie		/*
3160188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3161188144Sjamie		 * then bind directly.
3162188144Sjamie		 */
3163191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3164191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3165191673Sjamie		mtx_unlock(&pr->pr_mtx);
3166185435Sbz		return (0);
3167185435Sbz	}
3168188144Sjamie
3169191673Sjamie	error = _prison_check_ip6(pr, ia6);
3170191673Sjamie	mtx_unlock(&pr->pr_mtx);
3171191673Sjamie	return (error);
3172185435Sbz}
3173185435Sbz
3174185435Sbz/*
3175185435Sbz * Rewrite destination address in case we will connect to loopback address.
3176185435Sbz *
3177188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3178185435Sbz */
3179185435Sbzint
3180185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3181185435Sbz{
3182191673Sjamie	struct prison *pr;
3183185435Sbz
3184185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3185185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3186185435Sbz
3187192895Sjamie	pr = cred->cr_prison;
3188192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3189185435Sbz		return (0);
3190191673Sjamie	mtx_lock(&pr->pr_mtx);
3191192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3192192895Sjamie		mtx_unlock(&pr->pr_mtx);
3193192895Sjamie		return (0);
3194192895Sjamie	}
3195191673Sjamie	if (pr->pr_ip6 == NULL) {
3196191673Sjamie		mtx_unlock(&pr->pr_mtx);
3197188144Sjamie		return (EAFNOSUPPORT);
3198191673Sjamie	}
3199188144Sjamie
3200185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3201191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3202191673Sjamie		mtx_unlock(&pr->pr_mtx);
3203185435Sbz		return (0);
3204185435Sbz	}
3205185435Sbz
3206185435Sbz	/*
3207185435Sbz	 * Return success because nothing had to be changed.
3208185435Sbz	 */
3209191673Sjamie	mtx_unlock(&pr->pr_mtx);
321046155Sphk	return (0);
321146155Sphk}
321246155Sphk
3213185435Sbz/*
3214188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3215185435Sbz *
3216192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3217192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3218192895Sjamie * doesn't allow IPv6.
3219185435Sbz */
3220185435Sbzstatic int
3221185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
322246155Sphk{
3223185435Sbz	int i, a, z, d;
322446155Sphk
3225185435Sbz	/*
3226185435Sbz	 * Check the primary IP.
3227185435Sbz	 */
3228185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3229188144Sjamie		return (0);
3230185435Sbz
3231185435Sbz	/*
3232185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3233185435Sbz	 */
3234185435Sbz	a = 0;
3235185435Sbz	z = pr->pr_ip6s - 2;
3236185435Sbz	while (a <= z) {
3237185435Sbz		i = (a + z) / 2;
3238185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3239185435Sbz		if (d > 0)
3240185435Sbz			z = i - 1;
3241185435Sbz		else if (d < 0)
3242185435Sbz			a = i + 1;
324346155Sphk		else
3244188144Sjamie			return (0);
324546155Sphk	}
3246188144Sjamie
3247188144Sjamie	return (EADDRNOTAVAIL);
324846155Sphk}
324946155Sphk
325046155Sphkint
3251185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3252185435Sbz{
3253191673Sjamie	struct prison *pr;
3254191673Sjamie	int error;
3255185435Sbz
3256185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3257185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3258185435Sbz
3259192895Sjamie	pr = cred->cr_prison;
3260192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3261188144Sjamie		return (0);
3262191673Sjamie	mtx_lock(&pr->pr_mtx);
3263192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3264192895Sjamie		mtx_unlock(&pr->pr_mtx);
3265192895Sjamie		return (0);
3266192895Sjamie	}
3267191673Sjamie	if (pr->pr_ip6 == NULL) {
3268191673Sjamie		mtx_unlock(&pr->pr_mtx);
3269188144Sjamie		return (EAFNOSUPPORT);
3270191673Sjamie	}
3271185435Sbz
3272191673Sjamie	error = _prison_check_ip6(pr, ia6);
3273191673Sjamie	mtx_unlock(&pr->pr_mtx);
3274191673Sjamie	return (error);
3275185435Sbz}
3276185435Sbz#endif
3277185435Sbz
3278185435Sbz/*
3279188146Sjamie * Check if a jail supports the given address family.
3280188146Sjamie *
3281188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3282188146Sjamie * if not.
3283188146Sjamie */
3284188146Sjamieint
3285188146Sjamieprison_check_af(struct ucred *cred, int af)
3286188146Sjamie{
3287192895Sjamie	struct prison *pr;
3288188146Sjamie	int error;
3289188146Sjamie
3290188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3291188146Sjamie
3292192895Sjamie	pr = cred->cr_prison;
3293194923Sjamie#ifdef VIMAGE
3294194915Sjamie	/* Prisons with their own network stack are not limited. */
3295200473Sbz	if (prison_owns_vnet(cred))
3296194915Sjamie		return (0);
3297194923Sjamie#endif
3298194915Sjamie
3299188146Sjamie	error = 0;
3300188146Sjamie	switch (af)
3301188146Sjamie	{
3302188146Sjamie#ifdef INET
3303188146Sjamie	case AF_INET:
3304192895Sjamie		if (pr->pr_flags & PR_IP4)
3305192895Sjamie		{
3306192895Sjamie			mtx_lock(&pr->pr_mtx);
3307192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3308192895Sjamie				error = EAFNOSUPPORT;
3309192895Sjamie			mtx_unlock(&pr->pr_mtx);
3310192895Sjamie		}
3311188146Sjamie		break;
3312188146Sjamie#endif
3313188146Sjamie#ifdef INET6
3314188146Sjamie	case AF_INET6:
3315192895Sjamie		if (pr->pr_flags & PR_IP6)
3316192895Sjamie		{
3317192895Sjamie			mtx_lock(&pr->pr_mtx);
3318192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3319192895Sjamie				error = EAFNOSUPPORT;
3320192895Sjamie			mtx_unlock(&pr->pr_mtx);
3321192895Sjamie		}
3322188146Sjamie		break;
3323188146Sjamie#endif
3324188146Sjamie	case AF_LOCAL:
3325188146Sjamie	case AF_ROUTE:
3326188146Sjamie		break;
3327188146Sjamie	default:
3328192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3329188146Sjamie			error = EAFNOSUPPORT;
3330188146Sjamie	}
3331188146Sjamie	return (error);
3332188146Sjamie}
3333188146Sjamie
3334188146Sjamie/*
3335185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3336185435Sbz * prison_check_ip[46]).
3337185435Sbz *
3338192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3339192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3340192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3341185435Sbz */
3342185435Sbzint
334372786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
334446155Sphk{
3345185435Sbz#ifdef INET
3346114168Smike	struct sockaddr_in *sai;
3347185435Sbz#endif
3348185435Sbz#ifdef INET6
3349185435Sbz	struct sockaddr_in6 *sai6;
3350185435Sbz#endif
3351188144Sjamie	int error;
335246155Sphk
3353185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3354185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3355185435Sbz
3356200473Sbz#ifdef VIMAGE
3357200473Sbz	if (prison_owns_vnet(cred))
3358200473Sbz		return (0);
3359200473Sbz#endif
3360200473Sbz
3361188144Sjamie	error = 0;
3362188144Sjamie	switch (sa->sa_family)
3363185435Sbz	{
3364185435Sbz#ifdef INET
3365185435Sbz	case AF_INET:
3366185435Sbz		sai = (struct sockaddr_in *)sa;
3367188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3368185435Sbz		break;
3369185435Sbz#endif
3370185435Sbz#ifdef INET6
3371185435Sbz	case AF_INET6:
3372185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3373188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3374185435Sbz		break;
3375185435Sbz#endif
3376185435Sbz	default:
3377192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3378188144Sjamie			error = EAFNOSUPPORT;
3379185435Sbz	}
3380188144Sjamie	return (error);
338146155Sphk}
338272786Srwatson
338372786Srwatson/*
338472786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
338572786Srwatson */
338672786Srwatsonint
3387114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
338872786Srwatson{
338972786Srwatson
3390192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3391192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3392192895Sjamie}
339372786Srwatson
3394192895Sjamie/*
3395192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3396192895Sjamie */
3397192895Sjamieint
3398192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3399192895Sjamie{
3400192895Sjamie
3401192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3402192895Sjamie		if (pr1 == pr2)
3403192895Sjamie			return (1);
340472786Srwatson	return (0);
340572786Srwatson}
340672786Srwatson
340772786Srwatson/*
340872786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
340972786Srwatson */
341072786Srwatsonint
3411114168Smikejailed(struct ucred *cred)
341272786Srwatson{
341372786Srwatson
3414192895Sjamie	return (cred->cr_prison != &prison0);
341572786Srwatson}
341691384Srobert
341791384Srobert/*
3418200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3419200473Sbz * have its own virtual network stack, otherwise 0.
3420200473Sbz */
3421200473Sbzint
3422200473Sbzjailed_without_vnet(struct ucred *cred)
3423200473Sbz{
3424200473Sbz
3425200473Sbz	if (!jailed(cred))
3426200473Sbz		return (0);
3427200473Sbz#ifdef VIMAGE
3428200473Sbz	if (prison_owns_vnet(cred))
3429200473Sbz		return (0);
3430200473Sbz#endif
3431200473Sbz
3432200473Sbz	return (1);
3433200473Sbz}
3434200473Sbz
3435200473Sbz/*
3436194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
343791384Srobert */
343891391Srobertvoid
3439114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
344091384Srobert{
3441193066Sjamie	struct prison *pr;
344291384Srobert
3443194090Sjamie	/*
3444194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3445194090Sjamie	 * system's hostname.
3446194090Sjamie	 */
3447193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3448193066Sjamie	mtx_lock(&pr->pr_mtx);
3449194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3450193066Sjamie	mtx_unlock(&pr->pr_mtx);
345191384Srobert}
3452113275Smike
3453194090Sjamievoid
3454194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3455194090Sjamie{
3456194090Sjamie
3457194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3458194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3459194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3460194090Sjamie}
3461194090Sjamie
3462194090Sjamievoid
3463194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3464194090Sjamie{
3465194090Sjamie
3466194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3467194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3468194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3469194090Sjamie}
3470194090Sjamie
3471194090Sjamievoid
3472194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3473194090Sjamie{
3474194090Sjamie
3475194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3476194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3477194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3478194090Sjamie}
3479194090Sjamie
3480196176Sbz#ifdef VIMAGE
3481125804Srwatson/*
3482196176Sbz * Determine whether the prison represented by cred owns
3483196176Sbz * its vnet rather than having it inherited.
3484196176Sbz *
3485196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3486196176Sbz */
3487196176Sbzint
3488196176Sbzprison_owns_vnet(struct ucred *cred)
3489196176Sbz{
3490196176Sbz
3491196176Sbz	/*
3492196176Sbz	 * vnets cannot be added/removed after jail creation,
3493196176Sbz	 * so no need to lock here.
3494196176Sbz	 */
3495196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3496196176Sbz}
3497196176Sbz#endif
3498196176Sbz
3499196176Sbz/*
3500147185Spjd * Determine whether the subject represented by cred can "see"
3501147185Spjd * status of a mount point.
3502147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3503147185Spjd * XXX: This function should be called cr_canseemount() and should be
3504147185Spjd *      placed in kern_prot.c.
3505125804Srwatson */
3506125804Srwatsonint
3507147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3508125804Srwatson{
3509147185Spjd	struct prison *pr;
3510147185Spjd	struct statfs *sp;
3511147185Spjd	size_t len;
3512125804Srwatson
3513192895Sjamie	pr = cred->cr_prison;
3514192895Sjamie	if (pr->pr_enforce_statfs == 0)
3515147185Spjd		return (0);
3516147185Spjd	if (pr->pr_root->v_mount == mp)
3517147185Spjd		return (0);
3518192895Sjamie	if (pr->pr_enforce_statfs == 2)
3519147185Spjd		return (ENOENT);
3520147185Spjd	/*
3521147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3522147185Spjd	 * all mount-points from inside a jail.
3523147185Spjd	 * This is ugly check, but this is the only situation when jail's
3524147185Spjd	 * directory ends with '/'.
3525147185Spjd	 */
3526147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3527147185Spjd		return (0);
3528147185Spjd	len = strlen(pr->pr_path);
3529147185Spjd	sp = &mp->mnt_stat;
3530147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3531147185Spjd		return (ENOENT);
3532147185Spjd	/*
3533147185Spjd	 * Be sure that we don't have situation where jail's root directory
3534147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3535147185Spjd	 */
3536147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3537147185Spjd		return (ENOENT);
3538147185Spjd	return (0);
3539147185Spjd}
3540147185Spjd
3541147185Spjdvoid
3542147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3543147185Spjd{
3544147185Spjd	char jpath[MAXPATHLEN];
3545147185Spjd	struct prison *pr;
3546147185Spjd	size_t len;
3547147185Spjd
3548192895Sjamie	pr = cred->cr_prison;
3549192895Sjamie	if (pr->pr_enforce_statfs == 0)
3550147185Spjd		return;
3551147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3552147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3553147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3554147185Spjd		    sizeof(sp->f_mntonname));
3555147185Spjd		return;
3556125804Srwatson	}
3557147185Spjd	if (pr->pr_root->v_mount == mp) {
3558147185Spjd		/*
3559147185Spjd		 * Clear current buffer data, so we are sure nothing from
3560147185Spjd		 * the valid path left there.
3561147185Spjd		 */
3562147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3563147185Spjd		*sp->f_mntonname = '/';
3564147185Spjd		return;
3565147185Spjd	}
3566147185Spjd	/*
3567147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3568147185Spjd	 * all mount-points from inside a jail.
3569147185Spjd	 */
3570147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3571147185Spjd		return;
3572147185Spjd	len = strlen(pr->pr_path);
3573147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3574147185Spjd	/*
3575147185Spjd	 * Clear current buffer data, so we are sure nothing from
3576147185Spjd	 * the valid path left there.
3577147185Spjd	 */
3578147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3579147185Spjd	if (*jpath == '\0') {
3580147185Spjd		/* Should never happen. */
3581147185Spjd		*sp->f_mntonname = '/';
3582147185Spjd	} else {
3583147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3584147185Spjd	}
3585125804Srwatson}
3586125804Srwatson
3587164032Srwatson/*
3588164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3589164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3590164032Srwatson */
3591164032Srwatsonint
3592164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3593164032Srwatson{
3594164032Srwatson
3595164032Srwatson	if (!jailed(cred))
3596164032Srwatson		return (0);
3597164032Srwatson
3598194915Sjamie#ifdef VIMAGE
3599194915Sjamie	/*
3600194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3601194915Sjamie	 * There might be a duplicate entry here in case the privilege
3602194915Sjamie	 * is only granted conditionally in the legacy jail case.
3603194915Sjamie	 */
3604164032Srwatson	switch (priv) {
3605194915Sjamie#ifdef notyet
3606194915Sjamie		/*
3607194915Sjamie		 * NFS-specific privileges.
3608194915Sjamie		 */
3609194915Sjamie	case PRIV_NFS_DAEMON:
3610194915Sjamie	case PRIV_NFS_LOCKD:
3611194915Sjamie#endif
3612194915Sjamie		/*
3613194915Sjamie		 * Network stack privileges.
3614194915Sjamie		 */
3615194915Sjamie	case PRIV_NET_BRIDGE:
3616194915Sjamie	case PRIV_NET_GRE:
3617194915Sjamie	case PRIV_NET_BPF:
3618194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3619194915Sjamie	case PRIV_NET_ROUTE:
3620194915Sjamie	case PRIV_NET_TAP:
3621194915Sjamie	case PRIV_NET_SETIFMTU:
3622194915Sjamie	case PRIV_NET_SETIFFLAGS:
3623194915Sjamie	case PRIV_NET_SETIFCAP:
3624203052Sdelphij	case PRIV_NET_SETIFDESCR:
3625194915Sjamie	case PRIV_NET_SETIFNAME	:
3626194915Sjamie	case PRIV_NET_SETIFMETRIC:
3627194915Sjamie	case PRIV_NET_SETIFPHYS:
3628194915Sjamie	case PRIV_NET_SETIFMAC:
3629194915Sjamie	case PRIV_NET_ADDMULTI:
3630194915Sjamie	case PRIV_NET_DELMULTI:
3631194915Sjamie	case PRIV_NET_HWIOCTL:
3632194915Sjamie	case PRIV_NET_SETLLADDR:
3633194915Sjamie	case PRIV_NET_ADDIFGROUP:
3634194915Sjamie	case PRIV_NET_DELIFGROUP:
3635194915Sjamie	case PRIV_NET_IFCREATE:
3636194915Sjamie	case PRIV_NET_IFDESTROY:
3637194915Sjamie	case PRIV_NET_ADDIFADDR:
3638194915Sjamie	case PRIV_NET_DELIFADDR:
3639194915Sjamie	case PRIV_NET_LAGG:
3640194915Sjamie	case PRIV_NET_GIF:
3641194915Sjamie	case PRIV_NET_SETIFVNET:
3642223735Sbz	case PRIV_NET_SETIFFIB:
3643164032Srwatson
3644164032Srwatson		/*
3645194915Sjamie		 * 802.11-related privileges.
3646194915Sjamie		 */
3647194915Sjamie	case PRIV_NET80211_GETKEY:
3648194915Sjamie#ifdef notyet
3649194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3650194915Sjamie#endif
3651194915Sjamie
3652194915Sjamie#ifdef notyet
3653194915Sjamie		/*
3654194915Sjamie		 * AppleTalk privileges.
3655194915Sjamie		 */
3656194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3657194915Sjamie
3658194915Sjamie		/*
3659194915Sjamie		 * ATM privileges.
3660194915Sjamie		 */
3661194915Sjamie	case PRIV_NETATM_CFG:
3662194915Sjamie	case PRIV_NETATM_ADD:
3663194915Sjamie	case PRIV_NETATM_DEL:
3664194915Sjamie	case PRIV_NETATM_SET:
3665194915Sjamie
3666194915Sjamie		/*
3667194915Sjamie		 * Bluetooth privileges.
3668194915Sjamie		 */
3669194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3670194915Sjamie#endif
3671194915Sjamie
3672194915Sjamie		/*
3673194915Sjamie		 * Netgraph and netgraph module privileges.
3674194915Sjamie		 */
3675194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3676194915Sjamie#ifdef notyet
3677194915Sjamie	case PRIV_NETGRAPH_TTY:
3678194915Sjamie#endif
3679194915Sjamie
3680194915Sjamie		/*
3681194915Sjamie		 * IPv4 and IPv6 privileges.
3682194915Sjamie		 */
3683194915Sjamie	case PRIV_NETINET_IPFW:
3684194915Sjamie	case PRIV_NETINET_DIVERT:
3685194915Sjamie	case PRIV_NETINET_PF:
3686194915Sjamie	case PRIV_NETINET_DUMMYNET:
3687194915Sjamie	case PRIV_NETINET_CARP:
3688194915Sjamie	case PRIV_NETINET_MROUTE:
3689194915Sjamie	case PRIV_NETINET_RAW:
3690194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3691194915Sjamie	case PRIV_NETINET_ND6:
3692194915Sjamie	case PRIV_NETINET_SCOPE6:
3693194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3694194915Sjamie	case PRIV_NETINET_IPSEC:
3695194915Sjamie	case PRIV_NETINET_BINDANY:
3696194915Sjamie
3697194915Sjamie#ifdef notyet
3698194915Sjamie		/*
3699194915Sjamie		 * IPX/SPX privileges.
3700194915Sjamie		 */
3701194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3702194915Sjamie	case PRIV_NETIPX_RAW:
3703194915Sjamie
3704194915Sjamie		/*
3705194915Sjamie		 * NCP privileges.
3706194915Sjamie		 */
3707194915Sjamie	case PRIV_NETNCP:
3708194915Sjamie
3709194915Sjamie		/*
3710194915Sjamie		 * SMB privileges.
3711194915Sjamie		 */
3712194915Sjamie	case PRIV_NETSMB:
3713194915Sjamie#endif
3714194915Sjamie
3715194915Sjamie	/*
3716194915Sjamie	 * No default: or deny here.
3717194915Sjamie	 * In case of no permit fall through to next switch().
3718194915Sjamie	 */
3719194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3720194915Sjamie			return (0);
3721194915Sjamie	}
3722194915Sjamie#endif /* VIMAGE */
3723194915Sjamie
3724194915Sjamie	switch (priv) {
3725194915Sjamie
3726194915Sjamie		/*
3727164032Srwatson		 * Allow ktrace privileges for root in jail.
3728164032Srwatson		 */
3729164032Srwatson	case PRIV_KTRACE:
3730164032Srwatson
3731166827Srwatson#if 0
3732164032Srwatson		/*
3733164032Srwatson		 * Allow jailed processes to configure audit identity and
3734164032Srwatson		 * submit audit records (login, etc).  In the future we may
3735164032Srwatson		 * want to further refine the relationship between audit and
3736164032Srwatson		 * jail.
3737164032Srwatson		 */
3738164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3739164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3740164032Srwatson	case PRIV_AUDIT_SUBMIT:
3741166827Srwatson#endif
3742164032Srwatson
3743164032Srwatson		/*
3744164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3745164032Srwatson		 * credentials in any way they see fit.
3746164032Srwatson		 */
3747164032Srwatson	case PRIV_CRED_SETUID:
3748164032Srwatson	case PRIV_CRED_SETEUID:
3749164032Srwatson	case PRIV_CRED_SETGID:
3750164032Srwatson	case PRIV_CRED_SETEGID:
3751164032Srwatson	case PRIV_CRED_SETGROUPS:
3752164032Srwatson	case PRIV_CRED_SETREUID:
3753164032Srwatson	case PRIV_CRED_SETREGID:
3754164032Srwatson	case PRIV_CRED_SETRESUID:
3755164032Srwatson	case PRIV_CRED_SETRESGID:
3756164032Srwatson
3757164032Srwatson		/*
3758164032Srwatson		 * Jail implements visibility constraints already, so allow
3759164032Srwatson		 * jailed root to override uid/gid-based constraints.
3760164032Srwatson		 */
3761164032Srwatson	case PRIV_SEEOTHERGIDS:
3762164032Srwatson	case PRIV_SEEOTHERUIDS:
3763164032Srwatson
3764164032Srwatson		/*
3765164032Srwatson		 * Jail implements inter-process debugging limits already, so
3766164032Srwatson		 * allow jailed root various debugging privileges.
3767164032Srwatson		 */
3768164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3769164032Srwatson	case PRIV_DEBUG_SUGID:
3770164032Srwatson	case PRIV_DEBUG_UNPRIV:
3771164032Srwatson
3772164032Srwatson		/*
3773164032Srwatson		 * Allow jail to set various resource limits and login
3774164032Srwatson		 * properties, and for now, exceed process resource limits.
3775164032Srwatson		 */
3776164032Srwatson	case PRIV_PROC_LIMIT:
3777164032Srwatson	case PRIV_PROC_SETLOGIN:
3778164032Srwatson	case PRIV_PROC_SETRLIMIT:
3779164032Srwatson
3780164032Srwatson		/*
3781164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3782164032Srwatson		 */
3783164032Srwatson	case PRIV_IPC_READ:
3784164032Srwatson	case PRIV_IPC_WRITE:
3785164032Srwatson	case PRIV_IPC_ADMIN:
3786164032Srwatson	case PRIV_IPC_MSGSIZE:
3787164032Srwatson	case PRIV_MQ_ADMIN:
3788164032Srwatson
3789164032Srwatson		/*
3790192895Sjamie		 * Jail operations within a jail work on child jails.
3791192895Sjamie		 */
3792192895Sjamie	case PRIV_JAIL_ATTACH:
3793192895Sjamie	case PRIV_JAIL_SET:
3794192895Sjamie	case PRIV_JAIL_REMOVE:
3795192895Sjamie
3796192895Sjamie		/*
3797164032Srwatson		 * Jail implements its own inter-process limits, so allow
3798164032Srwatson		 * root processes in jail to change scheduling on other
3799164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3800164032Srwatson		 */
3801164032Srwatson	case PRIV_SCHED_DIFFCRED:
3802185435Sbz	case PRIV_SCHED_CPUSET:
3803164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3804164032Srwatson	case PRIV_SIGNAL_SUGID:
3805164032Srwatson
3806164032Srwatson		/*
3807164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3808164032Srwatson		 * writable.
3809164032Srwatson		 */
3810164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
3811164032Srwatson
3812164032Srwatson		/*
3813164032Srwatson		 * Allow root in jail to manage a variety of quota
3814166831Srwatson		 * properties.  These should likely be conditional on a
3815166831Srwatson		 * configuration option.
3816164032Srwatson		 */
3817166832Srwatson	case PRIV_VFS_GETQUOTA:
3818166832Srwatson	case PRIV_VFS_SETQUOTA:
3819164032Srwatson
3820164032Srwatson		/*
3821164032Srwatson		 * Since Jail relies on chroot() to implement file system
3822164032Srwatson		 * protections, grant many VFS privileges to root in jail.
3823164032Srwatson		 * Be careful to exclude mount-related and NFS-related
3824164032Srwatson		 * privileges.
3825164032Srwatson		 */
3826164032Srwatson	case PRIV_VFS_READ:
3827164032Srwatson	case PRIV_VFS_WRITE:
3828164032Srwatson	case PRIV_VFS_ADMIN:
3829164032Srwatson	case PRIV_VFS_EXEC:
3830164032Srwatson	case PRIV_VFS_LOOKUP:
3831164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3832164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
3833164032Srwatson	case PRIV_VFS_CHOWN:
3834164032Srwatson	case PRIV_VFS_CHROOT:
3835167152Spjd	case PRIV_VFS_RETAINSUGID:
3836164032Srwatson	case PRIV_VFS_FCHROOT:
3837164032Srwatson	case PRIV_VFS_LINK:
3838164032Srwatson	case PRIV_VFS_SETGID:
3839172860Srwatson	case PRIV_VFS_STAT:
3840164032Srwatson	case PRIV_VFS_STICKYFILE:
3841164032Srwatson		return (0);
3842164032Srwatson
3843164032Srwatson		/*
3844164032Srwatson		 * Depending on the global setting, allow privilege of
3845164032Srwatson		 * setting system flags.
3846164032Srwatson		 */
3847164032Srwatson	case PRIV_VFS_SYSFLAGS:
3848192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3849164032Srwatson			return (0);
3850164032Srwatson		else
3851164032Srwatson			return (EPERM);
3852164032Srwatson
3853164032Srwatson		/*
3854168396Spjd		 * Depending on the global setting, allow privilege of
3855168396Spjd		 * mounting/unmounting file systems.
3856168396Spjd		 */
3857168396Spjd	case PRIV_VFS_MOUNT:
3858168396Spjd	case PRIV_VFS_UNMOUNT:
3859168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
3860168699Spjd	case PRIV_VFS_MOUNT_OWNER:
3861224615Smm		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3862224615Smm		    cred->cr_prison->pr_enforce_statfs < 2)
3863168396Spjd			return (0);
3864168396Spjd		else
3865168396Spjd			return (EPERM);
3866168396Spjd
3867168396Spjd		/*
3868168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
3869168591Srwatson		 * ports.
3870164032Srwatson		 */
3871164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
3872168591Srwatson	case PRIV_NETINET_REUSEPORT:
3873164032Srwatson		return (0);
3874164032Srwatson
3875164032Srwatson		/*
3876175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
3877175630Sbz		 */
3878175630Sbz	case PRIV_NETINET_SETHDROPTS:
3879175630Sbz		return (0);
3880175630Sbz
3881175630Sbz		/*
3882164032Srwatson		 * Conditionally allow creating raw sockets in jail.
3883164032Srwatson		 */
3884164032Srwatson	case PRIV_NETINET_RAW:
3885192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3886164032Srwatson			return (0);
3887164032Srwatson		else
3888164032Srwatson			return (EPERM);
3889164032Srwatson
3890164032Srwatson		/*
3891164032Srwatson		 * Since jail implements its own visibility limits on netstat
3892164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
3893164032Srwatson		 * jail.
3894164032Srwatson		 */
3895164032Srwatson	case PRIV_NETINET_GETCRED:
3896164032Srwatson		return (0);
3897164032Srwatson
3898219304Strasz		/*
3899219304Strasz		 * Allow jailed root to set loginclass.
3900219304Strasz		 */
3901219304Strasz	case PRIV_PROC_SETLOGINCLASS:
3902219304Strasz		return (0);
3903219304Strasz
3904164032Srwatson	default:
3905164032Srwatson		/*
3906164032Srwatson		 * In all remaining cases, deny the privilege request.  This
3907164032Srwatson		 * includes almost all network privileges, many system
3908164032Srwatson		 * configuration privileges.
3909164032Srwatson		 */
3910164032Srwatson		return (EPERM);
3911164032Srwatson	}
3912164032Srwatson}
3913164032Srwatson
3914192895Sjamie/*
3915192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
3916192895Sjamie * if it does not directly follow.
3917192895Sjamie */
3918192895Sjamie
3919192895Sjamiechar *
3920192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
3921192895Sjamie{
3922192895Sjamie	char *name;
3923192895Sjamie
3924192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
3925192895Sjamie	if (pr1 == pr2)
3926192895Sjamie		return "0";
3927192895Sjamie	name = pr2->pr_name;
3928192895Sjamie	if (prison_ischild(pr1, pr2)) {
3929192895Sjamie		/*
3930192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
3931192895Sjamie		 * so its length can't be counted on.  But the number of dots
3932192895Sjamie		 * can be counted on - and counted.
3933192895Sjamie		 */
3934192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3935192895Sjamie			name = strchr(name, '.') + 1;
3936192895Sjamie	}
3937192895Sjamie	return (name);
3938192895Sjamie}
3939192895Sjamie
3940192895Sjamie/*
3941192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
3942192895Sjamie * if it does not directly follow.
3943192895Sjamie */
3944192895Sjamiestatic char *
3945192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
3946192895Sjamie{
3947192895Sjamie	char *path1, *path2;
3948192895Sjamie	int len1;
3949192895Sjamie
3950192895Sjamie	path1 = pr1->pr_path;
3951192895Sjamie	path2 = pr2->pr_path;
3952192895Sjamie	if (!strcmp(path1, "/"))
3953192895Sjamie		return (path2);
3954192895Sjamie	len1 = strlen(path1);
3955192895Sjamie	if (strncmp(path1, path2, len1))
3956192895Sjamie		return (path2);
3957192895Sjamie	if (path2[len1] == '\0')
3958192895Sjamie		return "/";
3959192895Sjamie	if (path2[len1] == '/')
3960192895Sjamie		return (path2 + len1);
3961192895Sjamie	return (path2);
3962192895Sjamie}
3963192895Sjamie
3964192895Sjamie
3965192895Sjamie/*
3966192895Sjamie * Jail-related sysctls.
3967192895Sjamie */
3968227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3969192895Sjamie    "Jails");
3970192895Sjamie
3971113275Smikestatic int
3972113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
3973113275Smike{
3974191673Sjamie	struct xprison *xp;
3975192895Sjamie	struct prison *pr, *cpr;
3976191673Sjamie#ifdef INET
3977191673Sjamie	struct in_addr *ip4 = NULL;
3978191673Sjamie	int ip4s = 0;
3979191673Sjamie#endif
3980191673Sjamie#ifdef INET6
3981208803Scperciva	struct in6_addr *ip6 = NULL;
3982191673Sjamie	int ip6s = 0;
3983191673Sjamie#endif
3984192895Sjamie	int descend, error;
3985113275Smike
3986191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3987192895Sjamie	pr = req->td->td_ucred->cr_prison;
3988191673Sjamie	error = 0;
3989168401Spjd	sx_slock(&allprison_lock);
3990192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3991192895Sjamie#if defined(INET) || defined(INET6)
3992191673Sjamie again:
3993192895Sjamie#endif
3994192895Sjamie		mtx_lock(&cpr->pr_mtx);
3995185435Sbz#ifdef INET
3996192895Sjamie		if (cpr->pr_ip4s > 0) {
3997192895Sjamie			if (ip4s < cpr->pr_ip4s) {
3998192895Sjamie				ip4s = cpr->pr_ip4s;
3999192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4000191673Sjamie				ip4 = realloc(ip4, ip4s *
4001191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4002191673Sjamie				goto again;
4003191673Sjamie			}
4004192895Sjamie			bcopy(cpr->pr_ip4, ip4,
4005192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
4006191673Sjamie		}
4007185435Sbz#endif
4008185435Sbz#ifdef INET6
4009192895Sjamie		if (cpr->pr_ip6s > 0) {
4010192895Sjamie			if (ip6s < cpr->pr_ip6s) {
4011192895Sjamie				ip6s = cpr->pr_ip6s;
4012192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4013191673Sjamie				ip6 = realloc(ip6, ip6s *
4014191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4015191673Sjamie				goto again;
4016191673Sjamie			}
4017192895Sjamie			bcopy(cpr->pr_ip6, ip6,
4018192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
4019191673Sjamie		}
4020185435Sbz#endif
4021192895Sjamie		if (cpr->pr_ref == 0) {
4022192895Sjamie			mtx_unlock(&cpr->pr_mtx);
4023191673Sjamie			continue;
4024191673Sjamie		}
4025191673Sjamie		bzero(xp, sizeof(*xp));
4026113275Smike		xp->pr_version = XPRISON_VERSION;
4027192895Sjamie		xp->pr_id = cpr->pr_id;
4028192895Sjamie		xp->pr_state = cpr->pr_uref > 0
4029191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4030192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4031194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4032192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4033185435Sbz#ifdef INET
4034192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
4035185435Sbz#endif
4036185435Sbz#ifdef INET6
4037192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4038185435Sbz#endif
4039192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4040191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4041191673Sjamie		if (error)
4042191673Sjamie			break;
4043185435Sbz#ifdef INET
4044191673Sjamie		if (xp->pr_ip4s > 0) {
4045191673Sjamie			error = SYSCTL_OUT(req, ip4,
4046191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4047191673Sjamie			if (error)
4048191673Sjamie				break;
4049185435Sbz		}
4050185435Sbz#endif
4051185435Sbz#ifdef INET6
4052191673Sjamie		if (xp->pr_ip6s > 0) {
4053191673Sjamie			error = SYSCTL_OUT(req, ip6,
4054191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4055191673Sjamie			if (error)
4056191673Sjamie				break;
4057185435Sbz		}
4058185435Sbz#endif
4059113275Smike	}
4060168401Spjd	sx_sunlock(&allprison_lock);
4061191673Sjamie	free(xp, M_TEMP);
4062191673Sjamie#ifdef INET
4063191673Sjamie	free(ip4, M_TEMP);
4064191673Sjamie#endif
4065191673Sjamie#ifdef INET6
4066191673Sjamie	free(ip6, M_TEMP);
4067191673Sjamie#endif
4068167354Spjd	return (error);
4069113275Smike}
4070113275Smike
4071187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4072187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4073187864Sed    sysctl_jail_list, "S", "List of active jails");
4074126004Spjd
4075126004Spjdstatic int
4076126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4077126004Spjd{
4078126004Spjd	int error, injail;
4079126004Spjd
4080126004Spjd	injail = jailed(req->td->td_ucred);
4081126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4082126004Spjd
4083126004Spjd	return (error);
4084126004Spjd}
4085192895Sjamie
4086187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4087187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4088187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4089185435Sbz
4090192895Sjamie#if defined(INET) || defined(INET6)
4091193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4092192895Sjamie    &jail_max_af_ips, 0,
4093192895Sjamie    "Number of IP addresses a jail may have at most per address family");
4094192895Sjamie#endif
4095192895Sjamie
4096192895Sjamie/*
4097192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
4098192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4099192895Sjamie * just see their own parameters, and can't change them.
4100192895Sjamie */
4101192895Sjamiestatic int
4102192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4103192895Sjamie{
4104192895Sjamie	struct prison *pr;
4105192895Sjamie	int allow, error, i;
4106192895Sjamie
4107192895Sjamie	pr = req->td->td_ucred->cr_prison;
4108192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4109192895Sjamie
4110192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4111192895Sjamie	i = (allow & arg2) ? 1 : 0;
4112192895Sjamie	if (arg1 != NULL)
4113192895Sjamie		i = !i;
4114192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4115192895Sjamie	if (error || !req->newptr)
4116192895Sjamie		return (error);
4117192895Sjamie	i = i ? arg2 : 0;
4118192895Sjamie	if (arg1 != NULL)
4119192895Sjamie		i ^= arg2;
4120192895Sjamie	/*
4121192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4122192895Sjamie	 * for writing.
4123192895Sjamie	 */
4124192895Sjamie	mtx_lock(&prison0.pr_mtx);
4125192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4126192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4127192895Sjamie	return (0);
4128192895Sjamie}
4129192895Sjamie
4130192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4131192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4132192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4133192895Sjamie    "Processes in jail can set their hostnames");
4134192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4135192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4136192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4137192895Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only");
4138192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4139192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4140192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4141192895Sjamie    "Processes in jail can use System V IPC primitives");
4142192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4143192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4144192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4145192895Sjamie    "Prison root can create raw sockets");
4146192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4147192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4148192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4149192895Sjamie    "Processes in jail can alter system file flags");
4150192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4151192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4152192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4153192895Sjamie    "Processes in jail can mount/unmount jail-friendly file systems");
4154192895Sjamie
4155192895Sjamiestatic int
4156192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4157192895Sjamie{
4158192895Sjamie	struct prison *pr;
4159192895Sjamie	int level, error;
4160192895Sjamie
4161192895Sjamie	pr = req->td->td_ucred->cr_prison;
4162192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4163192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4164192895Sjamie	if (error || !req->newptr)
4165192895Sjamie		return (error);
4166192895Sjamie	*(int *)arg1 = level;
4167192895Sjamie	return (0);
4168192895Sjamie}
4169192895Sjamie
4170192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4171192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4172192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4173192895Sjamie    sysctl_jail_default_level, "I",
4174192895Sjamie    "Processes in jail cannot see all mounted file systems");
4175192895Sjamie
4176192895Sjamie/*
4177192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4178192895Sjamie * is returned in the string itself, and the other parameters exist merely
4179192895Sjamie * to make themselves and their types known.
4180192895Sjamie */
4181192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4182192895Sjamie    "Jail parameters");
4183192895Sjamie
4184192895Sjamieint
4185192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4186192895Sjamie{
4187192895Sjamie	int i;
4188192895Sjamie	long l;
4189192895Sjamie	size_t s;
4190192895Sjamie	char numbuf[12];
4191192895Sjamie
4192192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4193192895Sjamie	{
4194192895Sjamie	case CTLTYPE_LONG:
4195192895Sjamie	case CTLTYPE_ULONG:
4196192895Sjamie		l = 0;
4197192895Sjamie#ifdef SCTL_MASK32
4198192895Sjamie		if (!(req->flags & SCTL_MASK32))
4199192895Sjamie#endif
4200192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4201192895Sjamie	case CTLTYPE_INT:
4202192895Sjamie	case CTLTYPE_UINT:
4203192895Sjamie		i = 0;
4204192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4205192895Sjamie	case CTLTYPE_STRING:
4206219819Sjeff		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4207192895Sjamie		return
4208192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4209192895Sjamie	case CTLTYPE_STRUCT:
4210192895Sjamie		s = (size_t)arg2;
4211192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4212192895Sjamie	}
4213192895Sjamie	return (0);
4214192895Sjamie}
4215192895Sjamie
4216192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4217192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4218192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4219192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4220192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4221192895Sjamie    "I", "Jail secure level");
4222192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4223192895Sjamie    "I", "Jail cannot see all mounted file systems");
4224192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4225192895Sjamie    "B", "Jail persistence");
4226194251Sjamie#ifdef VIMAGE
4227194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4228195870Sjamie    "E,jailsys", "Virtual network stack");
4229194251Sjamie#endif
4230192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4231192895Sjamie    "B", "Jail is in the process of shutting down");
4232192895Sjamie
4233194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4234194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4235194762Sjamie    "I", "Current number of child jails");
4236194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4237194762Sjamie    "I", "Maximum number of child jails");
4238194762Sjamie
4239195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4240192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4241192895Sjamie    "Jail hostname");
4242193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4243193066Sjamie    "Jail NIS domainname");
4244193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4245193066Sjamie    "Jail host UUID");
4246193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4247193066Sjamie    "LU", "Jail host ID");
4248192895Sjamie
4249192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4250192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4251192895Sjamie
4252192895Sjamie#ifdef INET
4253195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4254195974Sjamie    "Jail IPv4 address virtualization");
4255192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4256192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4257202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4258202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4259202468Sbz    "primary jail IPv4 address.");
4260192895Sjamie#endif
4261192895Sjamie#ifdef INET6
4262195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4263195974Sjamie    "Jail IPv6 address virtualization");
4264192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4265192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4266202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4267202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4268202468Sbz    "primary jail IPv6 address.");
4269192895Sjamie#endif
4270192895Sjamie
4271192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4272192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4273192895Sjamie    "B", "Jail may set hostname");
4274192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4275192895Sjamie    "B", "Jail may use SYSV IPC");
4276192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4277192895Sjamie    "B", "Jail may create raw sockets");
4278192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4279192895Sjamie    "B", "Jail may alter system file flags");
4280192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4281192895Sjamie    "B", "Jail may mount/unmount jail-friendly file systems");
4282192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4283192895Sjamie    "B", "Jail may set file quotas");
4284192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4285192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4286192895Sjamie
4287220137Straszvoid
4288220137Straszprison_racct_foreach(void (*callback)(struct racct *racct,
4289220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
4290220137Strasz{
4291221362Strasz	struct prison_racct *prr;
4292192895Sjamie
4293220137Strasz	sx_slock(&allprison_lock);
4294221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next)
4295221362Strasz		(callback)(prr->prr_racct, arg2, arg3);
4296220137Strasz	sx_sunlock(&allprison_lock);
4297220137Strasz}
4298220137Strasz
4299221362Straszstatic struct prison_racct *
4300221362Straszprison_racct_find_locked(const char *name)
4301221362Strasz{
4302221362Strasz	struct prison_racct *prr;
4303221362Strasz
4304221362Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4305221362Strasz
4306221362Strasz	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4307221362Strasz		return (NULL);
4308221362Strasz
4309221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4310221362Strasz		if (strcmp(name, prr->prr_name) != 0)
4311221362Strasz			continue;
4312221362Strasz
4313221362Strasz		/* Found prison_racct with a matching name? */
4314221362Strasz		prison_racct_hold(prr);
4315221362Strasz		return (prr);
4316221362Strasz	}
4317221362Strasz
4318221362Strasz	/* Add new prison_racct. */
4319221362Strasz	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4320221362Strasz	racct_create(&prr->prr_racct);
4321221362Strasz
4322221362Strasz	strcpy(prr->prr_name, name);
4323221362Strasz	refcount_init(&prr->prr_refcount, 1);
4324221362Strasz	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4325221362Strasz
4326221362Strasz	return (prr);
4327221362Strasz}
4328221362Strasz
4329221362Straszstruct prison_racct *
4330221362Straszprison_racct_find(const char *name)
4331221362Strasz{
4332221362Strasz	struct prison_racct *prr;
4333221362Strasz
4334221362Strasz	sx_xlock(&allprison_lock);
4335221362Strasz	prr = prison_racct_find_locked(name);
4336221362Strasz	sx_xunlock(&allprison_lock);
4337221362Strasz	return (prr);
4338221362Strasz}
4339221362Strasz
4340221362Straszvoid
4341221362Straszprison_racct_hold(struct prison_racct *prr)
4342221362Strasz{
4343221362Strasz
4344221362Strasz	refcount_acquire(&prr->prr_refcount);
4345221362Strasz}
4346221362Strasz
4347221362Straszvoid
4348221362Straszprison_racct_free(struct prison_racct *prr)
4349221362Strasz{
4350221362Strasz	int old;
4351221362Strasz
4352221362Strasz	old = prr->prr_refcount;
4353221362Strasz	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4354221362Strasz		return;
4355221362Strasz
4356221362Strasz	sx_xlock(&allprison_lock);
4357221362Strasz	if (refcount_release(&prr->prr_refcount)) {
4358221362Strasz		racct_destroy(&prr->prr_racct);
4359221362Strasz		LIST_REMOVE(prr, prr_next);
4360221362Strasz		sx_xunlock(&allprison_lock);
4361221362Strasz		free(prr, M_PRISON_RACCT);
4362221362Strasz
4363221362Strasz		return;
4364221362Strasz	}
4365221362Strasz	sx_xunlock(&allprison_lock);
4366221362Strasz}
4367221362Strasz
4368221362Strasz#ifdef RACCT
4369221362Straszstatic void
4370221362Straszprison_racct_attach(struct prison *pr)
4371221362Strasz{
4372221362Strasz	struct prison_racct *prr;
4373221362Strasz
4374221362Strasz	prr = prison_racct_find_locked(pr->pr_name);
4375221362Strasz	KASSERT(prr != NULL, ("cannot find prison_racct"));
4376221362Strasz
4377221362Strasz	pr->pr_prison_racct = prr;
4378221362Strasz}
4379221362Strasz
4380221362Straszstatic void
4381221362Straszprison_racct_detach(struct prison *pr)
4382221362Strasz{
4383221362Strasz	prison_racct_free(pr->pr_prison_racct);
4384221362Strasz	pr->pr_prison_racct = NULL;
4385221362Strasz}
4386221362Strasz#endif /* RACCT */
4387221362Strasz
4388185435Sbz#ifdef DDB
4389191673Sjamie
4390191673Sjamiestatic void
4391191673Sjamiedb_show_prison(struct prison *pr)
4392185435Sbz{
4393192895Sjamie	int fi;
4394191673Sjamie#if defined(INET) || defined(INET6)
4395191673Sjamie	int ii;
4396185435Sbz#endif
4397195870Sjamie	unsigned jsf;
4398185435Sbz#ifdef INET6
4399185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4400185435Sbz#endif
4401185435Sbz
4402191673Sjamie	db_printf("prison %p:\n", pr);
4403191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4404191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4405192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4406191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4407191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4408191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4409191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4410191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4411194251Sjamie#ifdef VIMAGE
4412194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4413194251Sjamie#endif
4414191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4415191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4416202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4417202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4418192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4419192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4420202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4421192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4422192895Sjamie	    fi++)
4423192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4424192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4425195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4426195870Sjamie	    fi++) {
4427195870Sjamie		jsf = pr->pr_flags &
4428195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4429195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4430195870Sjamie		    pr_flag_jailsys[fi].disable &&
4431195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4432195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4433195870Sjamie		    : "inherit");
4434195870Sjamie	}
4435202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4436192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4437192895Sjamie	    fi++)
4438192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4439192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4440191673Sjamie	db_printf("\n");
4441192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4442194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4443194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4444194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4445193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4446185435Sbz#ifdef INET
4447191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4448191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4449191673Sjamie		db_printf(" %s %s\n",
4450202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4451191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4452185435Sbz#endif
4453185435Sbz#ifdef INET6
4454191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4455191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4456191673Sjamie		db_printf(" %s %s\n",
4457202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4458191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4459191673Sjamie#endif
4460191673Sjamie}
4461191673Sjamie
4462191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4463191673Sjamie{
4464191673Sjamie	struct prison *pr;
4465191673Sjamie
4466191673Sjamie	if (!have_addr) {
4467192895Sjamie		/*
4468192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4469192895Sjamie		 * listed.
4470192895Sjamie		 */
4471192895Sjamie		db_show_prison(&prison0);
4472192895Sjamie		if (!db_pager_quit) {
4473192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4474192895Sjamie				db_show_prison(pr);
4475192895Sjamie				if (db_pager_quit)
4476192895Sjamie					break;
4477192895Sjamie			}
4478191673Sjamie		}
4479191673Sjamie		return;
4480191673Sjamie	}
4481191673Sjamie
4482192895Sjamie	if (addr == 0)
4483192895Sjamie		pr = &prison0;
4484192895Sjamie	else {
4485192895Sjamie		/* Look for a prison with the ID and with references. */
4486191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4487192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4488191673Sjamie				break;
4489192895Sjamie		if (pr == NULL)
4490192895Sjamie			/* Look again, without requiring a reference. */
4491192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4492192895Sjamie				if (pr->pr_id == addr)
4493192895Sjamie					break;
4494192895Sjamie		if (pr == NULL)
4495192895Sjamie			/* Assume address points to a valid prison. */
4496192895Sjamie			pr = (struct prison *)addr;
4497192895Sjamie	}
4498191673Sjamie	db_show_prison(pr);
4499185435Sbz}
4500191673Sjamie
4501185435Sbz#endif /* DDB */
4502