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$");
31116182Sobrien
32193066Sjamie#include "opt_compat.h"
33185435Sbz#include "opt_ddb.h"
34185435Sbz#include "opt_inet.h"
35185435Sbz#include "opt_inet6.h"
36131177Spjd
3746155Sphk#include <sys/param.h>
3846155Sphk#include <sys/types.h>
3946155Sphk#include <sys/kernel.h>
4046155Sphk#include <sys/systm.h>
4146155Sphk#include <sys/errno.h>
4246155Sphk#include <sys/sysproto.h>
4346155Sphk#include <sys/malloc.h>
44192895Sjamie#include <sys/osd.h>
45164032Srwatson#include <sys/priv.h>
4646155Sphk#include <sys/proc.h>
47124882Srwatson#include <sys/taskqueue.h>
48177785Skib#include <sys/fcntl.h>
4946155Sphk#include <sys/jail.h>
5087275Srwatson#include <sys/lock.h>
5187275Srwatson#include <sys/mutex.h>
52220137Strasz#include <sys/racct.h>
53221362Strasz#include <sys/refcount.h>
54168401Spjd#include <sys/sx.h>
55193066Sjamie#include <sys/sysent.h>
56113275Smike#include <sys/namei.h>
57147185Spjd#include <sys/mount.h>
58113275Smike#include <sys/queue.h>
5946155Sphk#include <sys/socket.h>
60113275Smike#include <sys/syscallsubr.h>
6157163Srwatson#include <sys/sysctl.h>
62113275Smike#include <sys/vnode.h>
63196019Srwatson
6446155Sphk#include <net/if.h>
65196019Srwatson#include <net/vnet.h>
66196019Srwatson
6746155Sphk#include <netinet/in.h>
68196019Srwatson
69185435Sbz#ifdef DDB
70185435Sbz#include <ddb/ddb.h>
71185435Sbz#ifdef INET6
72185435Sbz#include <netinet6/in6_var.h>
73185435Sbz#endif /* INET6 */
74185435Sbz#endif /* DDB */
7546155Sphk
76163606Srwatson#include <security/mac/mac_framework.h>
77163606Srwatson
78195944Sjamie#define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
79195944Sjamie
8046155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
81227293Sedstatic MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
8246155Sphk
83202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */
84202468Sbz#ifdef INET
85202468Sbz#ifdef INET6
86202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
87202468Sbz#else
88202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
89202468Sbz#endif
90202468Sbz#else /* !INET */
91202468Sbz#ifdef INET6
92202468Sbz#define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
93202468Sbz#else
94202468Sbz#define	_PR_IP_SADDRSEL	0
95202468Sbz#endif
96202468Sbz#endif
97202468Sbz
98192895Sjamie/* prison0 describes what is "real" about the system. */
99192895Sjamiestruct prison prison0 = {
100192895Sjamie	.pr_id		= 0,
101192895Sjamie	.pr_name	= "0",
102192895Sjamie	.pr_ref		= 1,
103192895Sjamie	.pr_uref	= 1,
104192895Sjamie	.pr_path	= "/",
105192895Sjamie	.pr_securelevel	= -1,
106231267Smm	.pr_devfs_rsnum = 0,
107194762Sjamie	.pr_childmax	= JAIL_MAX,
108195944Sjamie	.pr_hostuuid	= DEFAULT_HOSTUUID,
109201145Santoine	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
110196176Sbz#ifdef VIMAGE
111202468Sbz	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
112196176Sbz#else
113202468Sbz	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
114196176Sbz#endif
115192895Sjamie	.pr_allow	= PR_ALLOW_ALL,
116192895Sjamie};
117192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11857163Srwatson
119221362Strasz/* allprison, allprison_racct and lastprid are protected by allprison_lock. */
120168401Spjdstruct	sx allprison_lock;
121191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
122191673Sjamiestruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
123221362StraszLIST_HEAD(, prison_racct) allprison_racct;
124179881Sdelphijint	lastprid = 0;
125113275Smike
126191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr);
127190466Sjamiestatic void prison_complete(void *context, int pending);
128191673Sjamiestatic void prison_deref(struct prison *pr, int flags);
129192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2);
130192895Sjamiestatic void prison_remove_one(struct prison *pr);
131221362Strasz#ifdef RACCT
132221362Straszstatic void prison_racct_attach(struct prison *pr);
133232598Straszstatic void prison_racct_modify(struct prison *pr);
134221362Straszstatic void prison_racct_detach(struct prison *pr);
135221362Strasz#endif
136185435Sbz#ifdef INET
137190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
138192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
139185435Sbz#endif
140185435Sbz#ifdef INET6
141190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
142192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
143185435Sbz#endif
144113275Smike
145191673Sjamie/* Flags for prison_deref */
146191673Sjamie#define	PD_DEREF	0x01
147191673Sjamie#define	PD_DEUREF	0x02
148191673Sjamie#define	PD_LOCKED	0x04
149191673Sjamie#define	PD_LIST_SLOCKED	0x08
150191673Sjamie#define	PD_LIST_XLOCKED	0x10
151113275Smike
152192895Sjamie/*
153216861Sbz * Parameter names corresponding to PR_* flag values.  Size values are for kvm
154216861Sbz * as we cannot figure out the size of a sparse array, or an array without a
155216861Sbz * terminating entry.
156192895Sjamie */
157192895Sjamiestatic char *pr_flag_names[] = {
158192895Sjamie	[0] = "persist",
159202468Sbz#ifdef INET
160202468Sbz	[7] = "ip4.saddrsel",
161202468Sbz#endif
162202468Sbz#ifdef INET6
163202468Sbz	[8] = "ip6.saddrsel",
164202468Sbz#endif
165192895Sjamie};
166216861Sbzconst size_t pr_flag_names_size = sizeof(pr_flag_names);
167192895Sjamie
168192895Sjamiestatic char *pr_flag_nonames[] = {
169192895Sjamie	[0] = "nopersist",
170202468Sbz#ifdef INET
171202468Sbz	[7] = "ip4.nosaddrsel",
172202468Sbz#endif
173202468Sbz#ifdef INET6
174202468Sbz	[8] = "ip6.nosaddrsel",
175202468Sbz#endif
176195870Sjamie};
177216861Sbzconst size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
178195870Sjamie
179195870Sjamiestruct jailsys_flags {
180195870Sjamie	const char	*name;
181195870Sjamie	unsigned	 disable;
182195870Sjamie	unsigned	 new;
183195870Sjamie} pr_flag_jailsys[] = {
184195870Sjamie	{ "host", 0, PR_HOST },
185195870Sjamie#ifdef VIMAGE
186195870Sjamie	{ "vnet", 0, PR_VNET },
187195870Sjamie#endif
188192895Sjamie#ifdef INET
189195870Sjamie	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
190192895Sjamie#endif
191192895Sjamie#ifdef INET6
192195870Sjamie	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
193192895Sjamie#endif
194192895Sjamie};
195216861Sbzconst size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
196192895Sjamie
197192895Sjamiestatic char *pr_allow_names[] = {
198192895Sjamie	"allow.set_hostname",
199192895Sjamie	"allow.sysvipc",
200192895Sjamie	"allow.raw_sockets",
201192895Sjamie	"allow.chflags",
202192895Sjamie	"allow.mount",
203192895Sjamie	"allow.quotas",
204192895Sjamie	"allow.socket_af",
205232059Smm	"allow.mount.devfs",
206232059Smm	"allow.mount.nullfs",
207232186Smm	"allow.mount.zfs",
208232278Smm	"allow.mount.procfs",
209254741Sdelphij	"allow.mount.tmpfs",
210192895Sjamie};
211216861Sbzconst size_t pr_allow_names_size = sizeof(pr_allow_names);
212192895Sjamie
213192895Sjamiestatic char *pr_allow_nonames[] = {
214192895Sjamie	"allow.noset_hostname",
215192895Sjamie	"allow.nosysvipc",
216192895Sjamie	"allow.noraw_sockets",
217192895Sjamie	"allow.nochflags",
218192895Sjamie	"allow.nomount",
219192895Sjamie	"allow.noquotas",
220192895Sjamie	"allow.nosocket_af",
221232059Smm	"allow.mount.nodevfs",
222232059Smm	"allow.mount.nonullfs",
223232186Smm	"allow.mount.nozfs",
224232278Smm	"allow.mount.noprocfs",
225254741Sdelphij	"allow.mount.notmpfs",
226192895Sjamie};
227216861Sbzconst size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
228192895Sjamie
229196002Sjamie#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
230196002Sjamie#define	JAIL_DEFAULT_ENFORCE_STATFS	2
231232059Smm#define	JAIL_DEFAULT_DEVFS_RSNUM	0
232192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
233196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
234231267Smmstatic int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
235192895Sjamie#if defined(INET) || defined(INET6)
236193865Sjamiestatic unsigned jail_max_af_ips = 255;
237192895Sjamie#endif
238192895Sjamie
239192895Sjamie#ifdef INET
240185435Sbzstatic int
241185435Sbzqcmp_v4(const void *ip1, const void *ip2)
242185435Sbz{
243185435Sbz	in_addr_t iaa, iab;
244185435Sbz
245185435Sbz	/*
246185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
247185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
248185435Sbz	 * interesting results.  If you do not understand, do not try.
249185435Sbz	 */
250185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
251185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
252185435Sbz
253185435Sbz	/*
254185435Sbz	 * Do not simply return the difference of the two numbers, the int is
255185435Sbz	 * not wide enough.
256185435Sbz	 */
257185435Sbz	if (iaa > iab)
258185435Sbz		return (1);
259185435Sbz	else if (iaa < iab)
260185435Sbz		return (-1);
261185435Sbz	else
262185435Sbz		return (0);
263185435Sbz}
264185435Sbz#endif
265185435Sbz
266185435Sbz#ifdef INET6
267185435Sbzstatic int
268185435Sbzqcmp_v6(const void *ip1, const void *ip2)
269185435Sbz{
270185435Sbz	const struct in6_addr *ia6a, *ia6b;
271185435Sbz	int i, rc;
272185435Sbz
273185435Sbz	ia6a = (const struct in6_addr *)ip1;
274185435Sbz	ia6b = (const struct in6_addr *)ip2;
275185435Sbz
276185435Sbz	rc = 0;
277190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
278185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
279185435Sbz			rc = 1;
280185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
281185435Sbz			rc = -1;
282185435Sbz	}
283185435Sbz	return (rc);
284185435Sbz}
285185435Sbz#endif
286185435Sbz
287191673Sjamie/*
288191673Sjamie * struct jail_args {
289191673Sjamie *	struct jail *jail;
290191673Sjamie * };
291191673Sjamie */
292191673Sjamieint
293225617Skmacysys_jail(struct thread *td, struct jail_args *uap)
294185435Sbz{
295191673Sjamie	uint32_t version;
296191673Sjamie	int error;
297192895Sjamie	struct jail j;
298185435Sbz
299191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
300191673Sjamie	if (error)
301191673Sjamie		return (error);
302185435Sbz
303191673Sjamie	switch (version) {
304191673Sjamie	case 0:
305191673Sjamie	{
306191673Sjamie		struct jail_v0 j0;
307185435Sbz
308192895Sjamie		/* FreeBSD single IPv4 jails. */
309192895Sjamie		bzero(&j, sizeof(struct jail));
310191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
311191673Sjamie		if (error)
312191673Sjamie			return (error);
313192895Sjamie		j.version = j0.version;
314192895Sjamie		j.path = j0.path;
315192895Sjamie		j.hostname = j0.hostname;
316258929Speter		j.ip4s = htonl(j0.ip_number);	/* jail_v0 is host order */
317191673Sjamie		break;
318191673Sjamie	}
319191673Sjamie
320191673Sjamie	case 1:
321185435Sbz		/*
322191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
323191673Sjamie		 * that never made it into the official kernel.
324185435Sbz		 */
325191673Sjamie		return (EINVAL);
326185435Sbz
327191673Sjamie	case 2:	/* JAIL_API_VERSION */
328191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
329191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
330191673Sjamie		if (error)
331191673Sjamie			return (error);
332192895Sjamie		break;
333192895Sjamie
334192895Sjamie	default:
335192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
336192895Sjamie		return (EINVAL);
337192895Sjamie	}
338192895Sjamie	return (kern_jail(td, &j));
339192895Sjamie}
340192895Sjamie
341192895Sjamieint
342192895Sjamiekern_jail(struct thread *td, struct jail *j)
343192895Sjamie{
344193865Sjamie	struct iovec optiov[2 * (4
345193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
346193865Sjamie#ifdef INET
347193865Sjamie			    + 1
348193865Sjamie#endif
349193865Sjamie#ifdef INET6
350193865Sjamie			    + 1
351193865Sjamie#endif
352193865Sjamie			    )];
353192895Sjamie	struct uio opt;
354192895Sjamie	char *u_path, *u_hostname, *u_name;
355185435Sbz#ifdef INET
356193865Sjamie	uint32_t ip4s;
357192895Sjamie	struct in_addr *u_ip4;
358192895Sjamie#endif
359192895Sjamie#ifdef INET6
360192895Sjamie	struct in6_addr *u_ip6;
361192895Sjamie#endif
362192895Sjamie	size_t tmplen;
363192895Sjamie	int error, enforce_statfs, fi;
364192895Sjamie
365192895Sjamie	bzero(&optiov, sizeof(optiov));
366192895Sjamie	opt.uio_iov = optiov;
367192895Sjamie	opt.uio_iovcnt = 0;
368192895Sjamie	opt.uio_offset = -1;
369192895Sjamie	opt.uio_resid = -1;
370192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
371192895Sjamie	opt.uio_rw = UIO_READ;
372192895Sjamie	opt.uio_td = td;
373192895Sjamie
374192895Sjamie	/* Set permissions for top-level jails from sysctls. */
375192895Sjamie	if (!jailed(td->td_ucred)) {
376192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
377192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
378192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
379192895Sjamie			    (jail_default_allow & (1 << fi))
380192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
381192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
382192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
383192895Sjamie			opt.uio_iovcnt += 2;
384192895Sjamie		}
385192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
386192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
387192895Sjamie		opt.uio_iovcnt++;
388192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
389192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
390192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
391192895Sjamie		opt.uio_iovcnt++;
392192895Sjamie	}
393192895Sjamie
394192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
395192895Sjamie#ifdef INET
396192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
397192895Sjamie	if (ip4s > jail_max_af_ips)
398192895Sjamie		return (EINVAL);
399192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
400191673Sjamie#else
401192895Sjamie	if (j->ip4s > 0)
402192895Sjamie		return (EINVAL);
403191673Sjamie#endif
404191673Sjamie#ifdef INET6
405192895Sjamie	if (j->ip6s > jail_max_af_ips)
406192895Sjamie		return (EINVAL);
407192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
408191673Sjamie#else
409192895Sjamie	if (j->ip6s > 0)
410192895Sjamie		return (EINVAL);
411191673Sjamie#endif
412192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
413192895Sjamie	u_hostname = u_path + MAXPATHLEN;
414192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
415191673Sjamie#ifdef INET
416192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
417191673Sjamie#endif
418191673Sjamie#ifdef INET6
419191673Sjamie#ifdef INET
420192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
421191673Sjamie#else
422192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
423191673Sjamie#endif
424191673Sjamie#endif
425192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
426192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
427192895Sjamie	opt.uio_iovcnt++;
428192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
429192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
430192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
431192895Sjamie	if (error) {
432192895Sjamie		free(u_path, M_TEMP);
433192895Sjamie		return (error);
434192895Sjamie	}
435192895Sjamie	opt.uio_iovcnt++;
436192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
437192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
438192895Sjamie	opt.uio_iovcnt++;
439192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
440192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
441192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
442192895Sjamie	if (error) {
443192895Sjamie		free(u_path, M_TEMP);
444192895Sjamie		return (error);
445192895Sjamie	}
446192895Sjamie	opt.uio_iovcnt++;
447192895Sjamie	if (j->jailname != NULL) {
448192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
449192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
450192895Sjamie		opt.uio_iovcnt++;
451192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
452192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
453192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
454191673Sjamie		if (error) {
455191673Sjamie			free(u_path, M_TEMP);
456191673Sjamie			return (error);
457191673Sjamie		}
458192895Sjamie		opt.uio_iovcnt++;
459192895Sjamie	}
460191673Sjamie#ifdef INET
461192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
462192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
463192895Sjamie	opt.uio_iovcnt++;
464192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
465192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
466192895Sjamie	if (j->version == 0)
467192895Sjamie		u_ip4->s_addr = j->ip4s;
468192895Sjamie	else {
469192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
470191673Sjamie		if (error) {
471191673Sjamie			free(u_path, M_TEMP);
472191673Sjamie			return (error);
473191673Sjamie		}
474192895Sjamie	}
475192895Sjamie	opt.uio_iovcnt++;
476185435Sbz#endif
477185435Sbz#ifdef INET6
478192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
479192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
480192895Sjamie	opt.uio_iovcnt++;
481192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
482192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
483192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
484192895Sjamie	if (error) {
485192895Sjamie		free(u_path, M_TEMP);
486192895Sjamie		return (error);
487192895Sjamie	}
488192895Sjamie	opt.uio_iovcnt++;
489185435Sbz#endif
490192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
491192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
492191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
493191673Sjamie	free(u_path, M_TEMP);
494191673Sjamie	return (error);
495185435Sbz}
496185435Sbz
497192895Sjamie
498191673Sjamie/*
499191673Sjamie * struct jail_set_args {
500191673Sjamie *	struct iovec *iovp;
501191673Sjamie *	unsigned int iovcnt;
502191673Sjamie *	int flags;
503191673Sjamie * };
504191673Sjamie */
505191673Sjamieint
506225617Skmacysys_jail_set(struct thread *td, struct jail_set_args *uap)
507185435Sbz{
508191673Sjamie	struct uio *auio;
509191673Sjamie	int error;
510191673Sjamie
511191673Sjamie	/* Check that we have an even number of iovecs. */
512191673Sjamie	if (uap->iovcnt & 1)
513191673Sjamie		return (EINVAL);
514191673Sjamie
515191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
516191673Sjamie	if (error)
517191673Sjamie		return (error);
518191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
519191673Sjamie	free(auio, M_IOV);
520191673Sjamie	return (error);
521191673Sjamie}
522191673Sjamie
523191673Sjamieint
524191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
525191673Sjamie{
526191673Sjamie	struct nameidata nd;
527185435Sbz#ifdef INET
528190466Sjamie	struct in_addr *ip4;
529185435Sbz#endif
530185435Sbz#ifdef INET6
531185435Sbz	struct in6_addr *ip6;
532185435Sbz#endif
533191673Sjamie	struct vfsopt *opt;
534191673Sjamie	struct vfsoptlist *opts;
535196135Sbz	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
536191673Sjamie	struct vnode *root;
537196835Sjamie	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
538230407Smm	char *g_path;
539192895Sjamie#if defined(INET) || defined(INET6)
540196135Sbz	struct prison *tppr;
541191673Sjamie	void *op;
542192895Sjamie#endif
543193066Sjamie	unsigned long hid;
544192895Sjamie	size_t namelen, onamelen;
545192895Sjamie	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
546231267Smm	int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
547195870Sjamie	int fi, jid, jsys, len, level;
548241896Skib	int childmax, rsnum, slevel;
549230129Smm	int fullpath_disabled;
550191673Sjamie#if defined(INET) || defined(INET6)
551192895Sjamie	int ii, ij;
552191673Sjamie#endif
553191673Sjamie#ifdef INET
554195974Sjamie	int ip4s, redo_ip4;
555191673Sjamie#endif
556191673Sjamie#ifdef INET6
557195974Sjamie	int ip6s, redo_ip6;
558191673Sjamie#endif
559224290Smckusick	uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
560224290Smckusick	unsigned tallow;
561191673Sjamie	char numbuf[12];
562185435Sbz
563191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
564191673Sjamie	if (!error && (flags & JAIL_ATTACH))
565191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
566191673Sjamie	if (error)
567191673Sjamie		return (error);
568192895Sjamie	mypr = ppr = td->td_ucred->cr_prison;
569194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
570192895Sjamie		return (EPERM);
571191673Sjamie	if (flags & ~JAIL_SET_MASK)
572191673Sjamie		return (EINVAL);
573191673Sjamie
574185435Sbz	/*
575191673Sjamie	 * Check all the parameters before committing to anything.  Not all
576191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
577191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
578191673Sjamie	 * the allprison lock.
579185435Sbz	 *
580191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
581191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
582191673Sjamie	 *     than duplicate it under a different name.
583185435Sbz	 */
584191673Sjamie	error = vfs_buildopts(optuio, &opts);
585191673Sjamie	if (error)
586191673Sjamie		return (error);
587185435Sbz#ifdef INET
588185435Sbz	ip4 = NULL;
589185435Sbz#endif
590185435Sbz#ifdef INET6
591185435Sbz	ip6 = NULL;
592185435Sbz#endif
593230407Smm	g_path = NULL;
594191673Sjamie
595191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
596191673Sjamie	if (error == ENOENT)
597191673Sjamie		jid = 0;
598191673Sjamie	else if (error != 0)
599191673Sjamie		goto done_free;
600191673Sjamie
601191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
602191673Sjamie	if (error == ENOENT)
603191673Sjamie		gotslevel = 0;
604191673Sjamie	else if (error != 0)
605191673Sjamie		goto done_free;
606191673Sjamie	else
607191673Sjamie		gotslevel = 1;
608191673Sjamie
609194762Sjamie	error =
610194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
611194762Sjamie	if (error == ENOENT)
612194762Sjamie		gotchildmax = 0;
613194762Sjamie	else if (error != 0)
614194762Sjamie		goto done_free;
615194762Sjamie	else
616194762Sjamie		gotchildmax = 1;
617194762Sjamie
618192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
619212436Sjamie	if (error == ENOENT)
620212436Sjamie		gotenforce = 0;
621212436Sjamie	else if (error != 0)
622192895Sjamie		goto done_free;
623212436Sjamie	else if (enforce < 0 || enforce > 2) {
624212436Sjamie		error = EINVAL;
625212436Sjamie		goto done_free;
626212436Sjamie	} else
627212436Sjamie		gotenforce = 1;
628192895Sjamie
629231267Smm	error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
630231267Smm	if (error == ENOENT)
631231267Smm		gotrsnum = 0;
632231267Smm	else if (error != 0)
633231267Smm		goto done_free;
634231267Smm	else
635231267Smm		gotrsnum = 1;
636231267Smm
637191673Sjamie	pr_flags = ch_flags = 0;
638192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
639192895Sjamie	    fi++) {
640192895Sjamie		if (pr_flag_names[fi] == NULL)
641192895Sjamie			continue;
642192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
643192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
644192895Sjamie	}
645191673Sjamie	ch_flags |= pr_flags;
646195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
647195870Sjamie	    fi++) {
648195870Sjamie		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
649195870Sjamie		    sizeof(jsys));
650195870Sjamie		if (error == ENOENT)
651195870Sjamie			continue;
652195870Sjamie		if (error != 0)
653195870Sjamie			goto done_free;
654195870Sjamie		switch (jsys) {
655195870Sjamie		case JAIL_SYS_DISABLE:
656195870Sjamie			if (!pr_flag_jailsys[fi].disable) {
657195870Sjamie				error = EINVAL;
658195870Sjamie				goto done_free;
659195870Sjamie			}
660195870Sjamie			pr_flags |= pr_flag_jailsys[fi].disable;
661195870Sjamie			break;
662195870Sjamie		case JAIL_SYS_NEW:
663195870Sjamie			pr_flags |= pr_flag_jailsys[fi].new;
664195870Sjamie			break;
665195870Sjamie		case JAIL_SYS_INHERIT:
666195870Sjamie			break;
667195870Sjamie		default:
668195870Sjamie			error = EINVAL;
669195870Sjamie			goto done_free;
670195870Sjamie		}
671195870Sjamie		ch_flags |=
672195870Sjamie		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
673195870Sjamie	}
674211085Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
675211085Sjamie	    && !(pr_flags & PR_PERSIST)) {
676211085Sjamie		error = EINVAL;
677211085Sjamie		vfs_opterror(opts, "new jail must persist or attach");
678211085Sjamie		goto done_errmsg;
679211085Sjamie	}
680194251Sjamie#ifdef VIMAGE
681194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
682194251Sjamie		error = EINVAL;
683194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
684194251Sjamie		goto done_errmsg;
685194251Sjamie	}
686194251Sjamie#endif
687195974Sjamie#ifdef INET
688195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
689195974Sjamie		error = EINVAL;
690195974Sjamie		vfs_opterror(opts, "ip4 cannot be changed after creation");
691195974Sjamie		goto done_errmsg;
692195974Sjamie	}
693195974Sjamie#endif
694195974Sjamie#ifdef INET6
695195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
696195974Sjamie		error = EINVAL;
697195974Sjamie		vfs_opterror(opts, "ip6 cannot be changed after creation");
698195974Sjamie		goto done_errmsg;
699195974Sjamie	}
700195974Sjamie#endif
701191673Sjamie
702192895Sjamie	pr_allow = ch_allow = 0;
703192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
704192895Sjamie	    fi++) {
705192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
706192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
707192895Sjamie	}
708192895Sjamie	ch_allow |= pr_allow;
709192895Sjamie
710191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
711191673Sjamie	if (error == ENOENT)
712191673Sjamie		name = NULL;
713191673Sjamie	else if (error != 0)
714191673Sjamie		goto done_free;
715191673Sjamie	else {
716191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
717191673Sjamie			error = EINVAL;
718191673Sjamie			goto done_free;
719191673Sjamie		}
720191673Sjamie		if (len > MAXHOSTNAMELEN) {
721191673Sjamie			error = ENAMETOOLONG;
722191673Sjamie			goto done_free;
723191673Sjamie		}
724191673Sjamie	}
725191673Sjamie
726191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
727191673Sjamie	if (error == ENOENT)
728191673Sjamie		host = NULL;
729191673Sjamie	else if (error != 0)
730191673Sjamie		goto done_free;
731191673Sjamie	else {
732193066Sjamie		ch_flags |= PR_HOST;
733193066Sjamie		pr_flags |= PR_HOST;
734191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
735191673Sjamie			error = EINVAL;
736191673Sjamie			goto done_free;
737191673Sjamie		}
738191673Sjamie		if (len > MAXHOSTNAMELEN) {
739191673Sjamie			error = ENAMETOOLONG;
740191673Sjamie			goto done_free;
741191673Sjamie		}
742191673Sjamie	}
743191673Sjamie
744193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
745193066Sjamie	if (error == ENOENT)
746193066Sjamie		domain = NULL;
747193066Sjamie	else if (error != 0)
748193066Sjamie		goto done_free;
749193066Sjamie	else {
750193066Sjamie		ch_flags |= PR_HOST;
751193066Sjamie		pr_flags |= PR_HOST;
752193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
753193066Sjamie			error = EINVAL;
754193066Sjamie			goto done_free;
755193066Sjamie		}
756193066Sjamie		if (len > MAXHOSTNAMELEN) {
757193066Sjamie			error = ENAMETOOLONG;
758193066Sjamie			goto done_free;
759193066Sjamie		}
760193066Sjamie	}
761193066Sjamie
762193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
763193066Sjamie	if (error == ENOENT)
764193066Sjamie		uuid = NULL;
765193066Sjamie	else if (error != 0)
766193066Sjamie		goto done_free;
767193066Sjamie	else {
768193066Sjamie		ch_flags |= PR_HOST;
769193066Sjamie		pr_flags |= PR_HOST;
770193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
771193066Sjamie			error = EINVAL;
772193066Sjamie			goto done_free;
773193066Sjamie		}
774193066Sjamie		if (len > HOSTUUIDLEN) {
775193066Sjamie			error = ENAMETOOLONG;
776193066Sjamie			goto done_free;
777193066Sjamie		}
778193066Sjamie	}
779193066Sjamie
780205014Snwhitehorn#ifdef COMPAT_FREEBSD32
781217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
782193066Sjamie		uint32_t hid32;
783193066Sjamie
784193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
785193066Sjamie		hid = hid32;
786193066Sjamie	} else
787193066Sjamie#endif
788193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
789193066Sjamie	if (error == ENOENT)
790193066Sjamie		gothid = 0;
791193066Sjamie	else if (error != 0)
792193066Sjamie		goto done_free;
793193066Sjamie	else {
794193066Sjamie		gothid = 1;
795193066Sjamie		ch_flags |= PR_HOST;
796193066Sjamie		pr_flags |= PR_HOST;
797193066Sjamie	}
798193066Sjamie
799185435Sbz#ifdef INET
800191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
801191673Sjamie	if (error == ENOENT)
802195870Sjamie		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
803191673Sjamie	else if (error != 0)
804191673Sjamie		goto done_free;
805191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
806191673Sjamie		error = EINVAL;
807191673Sjamie		goto done_free;
808192895Sjamie	} else {
809195870Sjamie		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
810195870Sjamie		if (ip4s == 0)
811195870Sjamie			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
812195870Sjamie		else {
813195870Sjamie			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
814192895Sjamie			ip4s /= sizeof(*ip4);
815192895Sjamie			if (ip4s > jail_max_af_ips) {
816185435Sbz				error = EINVAL;
817192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
818192895Sjamie				goto done_errmsg;
819185435Sbz			}
820195974Sjamie			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
821192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
822192895Sjamie			/*
823192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
824192895Sjamie			 * the primary IP address as given from userland.
825192895Sjamie			 * This special IP is used for unbound outgoing
826202116Sbz			 * connections as well for "loopback" traffic in case
827202116Sbz			 * source address selection cannot find any more fitting
828202116Sbz			 * address to connect from.
829192895Sjamie			 */
830192895Sjamie			if (ip4s > 1)
831192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
832192895Sjamie			/*
833192895Sjamie			 * Check for duplicate addresses and do some simple
834192895Sjamie			 * zero and broadcast checks. If users give other bogus
835192895Sjamie			 * addresses it is their problem.
836192895Sjamie			 *
837192895Sjamie			 * We do not have to care about byte order for these
838192895Sjamie			 * checks so we will do them in NBO.
839192895Sjamie			 */
840192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
841192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
842192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
843192895Sjamie					error = EINVAL;
844192895Sjamie					goto done_free;
845192895Sjamie				}
846192895Sjamie				if ((ii+1) < ip4s &&
847192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
848192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
849192895Sjamie					error = EINVAL;
850192895Sjamie					goto done_free;
851192895Sjamie				}
852192895Sjamie			}
853185435Sbz		}
854191673Sjamie	}
855191673Sjamie#endif
856185435Sbz
857185435Sbz#ifdef INET6
858191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
859191673Sjamie	if (error == ENOENT)
860195870Sjamie		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
861191673Sjamie	else if (error != 0)
862191673Sjamie		goto done_free;
863191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
864191673Sjamie		error = EINVAL;
865191673Sjamie		goto done_free;
866192895Sjamie	} else {
867195870Sjamie		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
868195870Sjamie		if (ip6s == 0)
869195870Sjamie			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
870195870Sjamie		else {
871195870Sjamie			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
872192895Sjamie			ip6s /= sizeof(*ip6);
873192895Sjamie			if (ip6s > jail_max_af_ips) {
874185435Sbz				error = EINVAL;
875192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
876192895Sjamie				goto done_errmsg;
877185435Sbz			}
878195974Sjamie			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
879192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
880192895Sjamie			if (ip6s > 1)
881192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
882192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
883192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
884192895Sjamie					error = EINVAL;
885192895Sjamie					goto done_free;
886192895Sjamie				}
887192895Sjamie				if ((ii+1) < ip6s &&
888192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
889192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
890192895Sjamie				{
891192895Sjamie					error = EINVAL;
892192895Sjamie					goto done_free;
893192895Sjamie				}
894192895Sjamie			}
895185435Sbz		}
896191673Sjamie	}
897185435Sbz#endif
898185435Sbz
899195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
900195945Sjamie	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
901195945Sjamie		error = EINVAL;
902195945Sjamie		vfs_opterror(opts,
903195945Sjamie		    "vnet jails cannot have IP address restrictions");
904195945Sjamie		goto done_errmsg;
905195945Sjamie	}
906195945Sjamie#endif
907195945Sjamie
908230143Smm	fullpath_disabled = 0;
909191673Sjamie	root = NULL;
910191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
911191673Sjamie	if (error == ENOENT)
912191673Sjamie		path = NULL;
913191673Sjamie	else if (error != 0)
914191673Sjamie		goto done_free;
915191673Sjamie	else {
916191673Sjamie		if (flags & JAIL_UPDATE) {
917191673Sjamie			error = EINVAL;
918191673Sjamie			vfs_opterror(opts,
919191673Sjamie			    "path cannot be changed after creation");
920191673Sjamie			goto done_errmsg;
921191673Sjamie		}
922191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
923191673Sjamie			error = EINVAL;
924191673Sjamie			goto done_free;
925191673Sjamie		}
926241896Skib		NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
927230129Smm		    path, td);
928230129Smm		error = namei(&nd);
929230129Smm		if (error)
930230129Smm			goto done_free;
931230129Smm		root = nd.ni_vp;
932230129Smm		NDFREE(&nd, NDF_ONLY_PNBUF);
933230407Smm		g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
934230407Smm		strlcpy(g_path, path, MAXPATHLEN);
935230407Smm		error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
936230407Smm		if (error == 0)
937230407Smm			path = g_path;
938230407Smm		else if (error == ENODEV) {
939230129Smm			/* proceed if sysctl debug.disablefullpath == 1 */
940230129Smm			fullpath_disabled = 1;
941230129Smm			if (len < 2 || (len == 2 && path[0] == '/'))
942230129Smm				path = NULL;
943230407Smm		} else {
944230129Smm			/* exit on other errors */
945230129Smm			goto done_free;
946230129Smm		}
947230129Smm		if (root->v_type != VDIR) {
948230129Smm			error = ENOTDIR;
949230129Smm			vput(root);
950230129Smm			goto done_free;
951230129Smm		}
952230129Smm		VOP_UNLOCK(root, 0);
953230129Smm		if (fullpath_disabled) {
954192895Sjamie			/* Leave room for a real-root full pathname. */
955192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
956192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
957192895Sjamie				error = ENAMETOOLONG;
958192895Sjamie				goto done_free;
959192895Sjamie			}
960191673Sjamie		}
961191673Sjamie	}
962185435Sbz
963191673Sjamie	/*
964191673Sjamie	 * Grab the allprison lock before letting modules check their
965191673Sjamie	 * parameters.  Once we have it, do not let go so we'll have a
966191673Sjamie	 * consistent view of the OSD list.
967191673Sjamie	 */
968191673Sjamie	sx_xlock(&allprison_lock);
969191673Sjamie	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
970191673Sjamie	if (error)
971191673Sjamie		goto done_unlock_list;
972185435Sbz
973191673Sjamie	/* By now, all parameters should have been noted. */
974191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
975191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
976191673Sjamie			error = EINVAL;
977191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
978191673Sjamie			goto done_unlock_list;
979191673Sjamie		}
980191673Sjamie	}
981191673Sjamie
982185435Sbz	/*
983191673Sjamie	 * See if we are creating a new record or updating an existing one.
984191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
985185435Sbz	 */
986191673Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
987191673Sjamie	if (!cuflags) {
988191673Sjamie		error = EINVAL;
989191673Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
990191673Sjamie		goto done_unlock_list;
991191673Sjamie	}
992191673Sjamie	pr = NULL;
993196835Sjamie	namelc = NULL;
994196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
995196835Sjamie		namelc = strrchr(name, '.');
996196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
997196835Sjamie		if (*p != '\0')
998196835Sjamie			jid = 0;
999196835Sjamie	}
1000191673Sjamie	if (jid != 0) {
1001192895Sjamie		/*
1002192895Sjamie		 * See if a requested jid already exists.  There is an
1003192895Sjamie		 * information leak here if the jid exists but is not within
1004192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
1005192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
1006192895Sjamie		 * will return ENOENT which is not normally a valid error.
1007192895Sjamie		 */
1008191673Sjamie		if (jid < 0) {
1009191673Sjamie			error = EINVAL;
1010191673Sjamie			vfs_opterror(opts, "negative jid");
1011191673Sjamie			goto done_unlock_list;
1012191673Sjamie		}
1013191673Sjamie		pr = prison_find(jid);
1014191673Sjamie		if (pr != NULL) {
1015192895Sjamie			ppr = pr->pr_parent;
1016191673Sjamie			/* Create: jid must not exist. */
1017191673Sjamie			if (cuflags == JAIL_CREATE) {
1018191673Sjamie				mtx_unlock(&pr->pr_mtx);
1019191673Sjamie				error = EEXIST;
1020191673Sjamie				vfs_opterror(opts, "jail %d already exists",
1021191673Sjamie				    jid);
1022191673Sjamie				goto done_unlock_list;
1023191673Sjamie			}
1024192895Sjamie			if (!prison_ischild(mypr, pr)) {
1025192895Sjamie				mtx_unlock(&pr->pr_mtx);
1026192895Sjamie				pr = NULL;
1027192895Sjamie			} else if (pr->pr_uref == 0) {
1028191673Sjamie				if (!(flags & JAIL_DYING)) {
1029191673Sjamie					mtx_unlock(&pr->pr_mtx);
1030191673Sjamie					error = ENOENT;
1031191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1032191673Sjamie					    jid);
1033191673Sjamie					goto done_unlock_list;
1034191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
1035191673Sjamie				    (pr_flags & PR_PERSIST)) {
1036191673Sjamie					/*
1037191673Sjamie					 * A dying jail might be resurrected
1038191673Sjamie					 * (via attach or persist), but first
1039191673Sjamie					 * it must determine if another jail
1040191673Sjamie					 * has claimed its name.  Accomplish
1041191673Sjamie					 * this by implicitly re-setting the
1042191673Sjamie					 * name.
1043191673Sjamie					 */
1044191673Sjamie					if (name == NULL)
1045192895Sjamie						name = prison_name(mypr, pr);
1046191673Sjamie				}
1047191673Sjamie			}
1048191673Sjamie		}
1049191673Sjamie		if (pr == NULL) {
1050191673Sjamie			/* Update: jid must exist. */
1051191673Sjamie			if (cuflags == JAIL_UPDATE) {
1052191673Sjamie				error = ENOENT;
1053191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1054191673Sjamie				goto done_unlock_list;
1055191673Sjamie			}
1056191673Sjamie		}
1057191673Sjamie	}
1058191673Sjamie	/*
1059191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1060191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1061191673Sjamie	 * (where the name must not already exist in a different jail),
1062191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1063191673Sjamie	 * because that is the jail being updated).
1064191673Sjamie	 */
1065191673Sjamie	if (name != NULL) {
1066196835Sjamie		namelc = strrchr(name, '.');
1067196835Sjamie		if (namelc == NULL)
1068196835Sjamie			namelc = name;
1069196835Sjamie		else {
1070192895Sjamie			/*
1071192895Sjamie			 * This is a hierarchical name.  Split it into the
1072192895Sjamie			 * parent and child names, and make sure the parent
1073192895Sjamie			 * exists or matches an already found jail.
1074192895Sjamie			 */
1075196835Sjamie			*namelc = '\0';
1076192895Sjamie			if (pr != NULL) {
1077196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1078196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1079192895Sjamie					mtx_unlock(&pr->pr_mtx);
1080192895Sjamie					error = EINVAL;
1081192895Sjamie					vfs_opterror(opts,
1082192895Sjamie					    "cannot change jail's parent");
1083192895Sjamie					goto done_unlock_list;
1084192895Sjamie				}
1085192895Sjamie			} else {
1086192895Sjamie				ppr = prison_find_name(mypr, name);
1087192895Sjamie				if (ppr == NULL) {
1088192895Sjamie					error = ENOENT;
1089192895Sjamie					vfs_opterror(opts,
1090192895Sjamie					    "jail \"%s\" not found", name);
1091192895Sjamie					goto done_unlock_list;
1092192895Sjamie				}
1093192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1094192895Sjamie			}
1095196835Sjamie			name = ++namelc;
1096192895Sjamie		}
1097191673Sjamie		if (name[0] != '\0') {
1098192895Sjamie			namelen =
1099192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1100192895Sjamie name_again:
1101191673Sjamie			deadpr = NULL;
1102192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1103191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1104192895Sjamie				    !strcmp(tpr->pr_name + namelen, name)) {
1105191673Sjamie					if (pr == NULL &&
1106191673Sjamie					    cuflags != JAIL_CREATE) {
1107191673Sjamie						mtx_lock(&tpr->pr_mtx);
1108191673Sjamie						if (tpr->pr_ref > 0) {
1109191673Sjamie							/*
1110191673Sjamie							 * Use this jail
1111191673Sjamie							 * for updates.
1112191673Sjamie							 */
1113191673Sjamie							if (tpr->pr_uref > 0) {
1114191673Sjamie								pr = tpr;
1115191673Sjamie								break;
1116191673Sjamie							}
1117191673Sjamie							deadpr = tpr;
1118191673Sjamie						}
1119191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1120191673Sjamie					} else if (tpr->pr_uref > 0) {
1121191673Sjamie						/*
1122191673Sjamie						 * Create, or update(jid):
1123191673Sjamie						 * name must not exist in an
1124192895Sjamie						 * active sibling jail.
1125191673Sjamie						 */
1126191673Sjamie						error = EEXIST;
1127191673Sjamie						if (pr != NULL)
1128191673Sjamie							mtx_unlock(&pr->pr_mtx);
1129191673Sjamie						vfs_opterror(opts,
1130191673Sjamie						   "jail \"%s\" already exists",
1131191673Sjamie						   name);
1132191673Sjamie						goto done_unlock_list;
1133191673Sjamie					}
1134191673Sjamie				}
1135191673Sjamie			}
1136191673Sjamie			/* If no active jail is found, use a dying one. */
1137191673Sjamie			if (deadpr != NULL && pr == NULL) {
1138191673Sjamie				if (flags & JAIL_DYING) {
1139191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1140191673Sjamie					if (deadpr->pr_ref == 0) {
1141191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1142191673Sjamie						goto name_again;
1143191673Sjamie					}
1144191673Sjamie					pr = deadpr;
1145191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1146191673Sjamie					error = ENOENT;
1147191673Sjamie					vfs_opterror(opts,
1148191673Sjamie					    "jail \"%s\" is dying", name);
1149191673Sjamie					goto done_unlock_list;
1150191673Sjamie				}
1151191673Sjamie			}
1152191673Sjamie			/* Update: name must exist if no jid. */
1153191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1154191673Sjamie				error = ENOENT;
1155191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1156191673Sjamie				    name);
1157191673Sjamie				goto done_unlock_list;
1158191673Sjamie			}
1159191673Sjamie		}
1160191673Sjamie	}
1161191673Sjamie	/* Update: must provide a jid or name. */
1162191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1163191673Sjamie		error = ENOENT;
1164191673Sjamie		vfs_opterror(opts, "update specified no jail");
1165191673Sjamie		goto done_unlock_list;
1166191673Sjamie	}
1167185435Sbz
1168191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1169191673Sjamie	if (pr == NULL) {
1170194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1171194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1172194762Sjamie				error = EPERM;
1173194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1174194762Sjamie				goto done_unlock_list;
1175194762Sjamie			}
1176191673Sjamie		created = 1;
1177192895Sjamie		mtx_lock(&ppr->pr_mtx);
1178192895Sjamie		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1179192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1180192895Sjamie			error = ENOENT;
1181192895Sjamie			vfs_opterror(opts, "parent jail went away!");
1182192895Sjamie			goto done_unlock_list;
1183192895Sjamie		}
1184192895Sjamie		ppr->pr_ref++;
1185192895Sjamie		ppr->pr_uref++;
1186192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1187191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1188191673Sjamie		if (jid == 0) {
1189191673Sjamie			/* Find the next free jid. */
1190191673Sjamie			jid = lastprid + 1;
1191191673Sjamie findnext:
1192191673Sjamie			if (jid == JAIL_MAX)
1193191673Sjamie				jid = 1;
1194191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1195191673Sjamie				if (tpr->pr_id < jid)
1196191673Sjamie					continue;
1197191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1198191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1199191673Sjamie					break;
1200191673Sjamie				}
1201191673Sjamie				if (jid == lastprid) {
1202191673Sjamie					error = EAGAIN;
1203191673Sjamie					vfs_opterror(opts,
1204191673Sjamie					    "no available jail IDs");
1205191673Sjamie					free(pr, M_PRISON);
1206192895Sjamie					prison_deref(ppr, PD_DEREF |
1207192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1208192895Sjamie					goto done_releroot;
1209191673Sjamie				}
1210191673Sjamie				jid++;
1211191673Sjamie				goto findnext;
1212191673Sjamie			}
1213191673Sjamie			lastprid = jid;
1214191673Sjamie		} else {
1215191673Sjamie			/*
1216191673Sjamie			 * The jail already has a jid (that did not yet exist),
1217191673Sjamie			 * so just find where to insert it.
1218191673Sjamie			 */
1219191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1220191673Sjamie				if (tpr->pr_id >= jid) {
1221191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1222191673Sjamie					break;
1223191673Sjamie				}
1224191673Sjamie		}
1225191673Sjamie		if (tpr == NULL)
1226191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1227192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1228192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1229194762Sjamie			tpr->pr_childcount++;
1230185435Sbz
1231192895Sjamie		pr->pr_parent = ppr;
1232191673Sjamie		pr->pr_id = jid;
1233192895Sjamie
1234192895Sjamie		/* Set some default values, and inherit some from the parent. */
1235191673Sjamie		if (name == NULL)
1236191673Sjamie			name = "";
1237191673Sjamie		if (path == NULL) {
1238191673Sjamie			path = "/";
1239192895Sjamie			root = mypr->pr_root;
1240191673Sjamie			vref(root);
1241191673Sjamie		}
1242195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1243195944Sjamie		pr->pr_flags |= PR_HOST;
1244195945Sjamie#if defined(INET) || defined(INET6)
1245195945Sjamie#ifdef VIMAGE
1246195945Sjamie		if (!(pr_flags & PR_VNET))
1247195945Sjamie#endif
1248195945Sjamie		{
1249192895Sjamie#ifdef INET
1250195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1251195974Sjamie				pr->pr_flags |=
1252195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1253195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1254195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1255195974Sjamie				if (ppr->pr_ip4 != NULL) {
1256195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1257195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1258195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1259195974Sjamie					    M_WAITOK);
1260195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1261195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1262195974Sjamie				}
1263195974Sjamie			}
1264192895Sjamie#endif
1265192895Sjamie#ifdef INET6
1266195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1267195974Sjamie				pr->pr_flags |=
1268195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1269195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1270195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1271195974Sjamie				if (ppr->pr_ip6 != NULL) {
1272195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1273195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1274195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1275195974Sjamie					    M_WAITOK);
1276195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1277195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1278195974Sjamie				}
1279195974Sjamie			}
1280192895Sjamie#endif
1281195945Sjamie		}
1282195945Sjamie#endif
1283202468Sbz		/* Source address selection is always on by default. */
1284202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1285202468Sbz
1286192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1287192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1288196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1289232059Smm		pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1290191673Sjamie
1291192895Sjamie		LIST_INIT(&pr->pr_children);
1292192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1293191673Sjamie
1294194251Sjamie#ifdef VIMAGE
1295194251Sjamie		/* Allocate a new vnet if specified. */
1296194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1297194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1298194251Sjamie#endif
1299185435Sbz		/*
1300191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1301191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1302185435Sbz		 */
1303192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1304191673Sjamie		if (error) {
1305191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1306191673Sjamie			goto done_releroot;
1307191673Sjamie		}
1308185435Sbz
1309191673Sjamie		mtx_lock(&pr->pr_mtx);
1310185435Sbz		/*
1311191673Sjamie		 * New prisons do not yet have a reference, because we do not
1312191673Sjamie		 * want other to see the incomplete prison once the
1313191673Sjamie		 * allprison_lock is downgraded.
1314185435Sbz		 */
1315191673Sjamie	} else {
1316191673Sjamie		created = 0;
1317195974Sjamie		/*
1318195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1319195974Sjamie		 * continue to exist for the duration of the call.
1320195974Sjamie		 */
1321195974Sjamie		pr->pr_ref++;
1322195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1323195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1324195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1325195945Sjamie			error = EINVAL;
1326195945Sjamie			vfs_opterror(opts,
1327195945Sjamie			    "vnet jails cannot have IP address restrictions");
1328195945Sjamie			goto done_deref_locked;
1329195945Sjamie		}
1330195945Sjamie#endif
1331195974Sjamie#ifdef INET
1332195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1333195974Sjamie			error = EINVAL;
1334195974Sjamie			vfs_opterror(opts,
1335195974Sjamie			    "ip4 cannot be changed after creation");
1336195974Sjamie			goto done_deref_locked;
1337195974Sjamie		}
1338195974Sjamie#endif
1339195974Sjamie#ifdef INET6
1340195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1341195974Sjamie			error = EINVAL;
1342195974Sjamie			vfs_opterror(opts,
1343195974Sjamie			    "ip6 cannot be changed after creation");
1344195974Sjamie			goto done_deref_locked;
1345195974Sjamie		}
1346195974Sjamie#endif
1347191673Sjamie	}
1348185435Sbz
1349191673Sjamie	/* Do final error checking before setting anything. */
1350192895Sjamie	if (gotslevel) {
1351192895Sjamie		if (slevel < ppr->pr_securelevel) {
1352192895Sjamie			error = EPERM;
1353192895Sjamie			goto done_deref_locked;
1354192895Sjamie		}
1355192895Sjamie	}
1356194762Sjamie	if (gotchildmax) {
1357194762Sjamie		if (childmax >= ppr->pr_childmax) {
1358194762Sjamie			error = EPERM;
1359194762Sjamie			goto done_deref_locked;
1360194762Sjamie		}
1361194762Sjamie	}
1362192895Sjamie	if (gotenforce) {
1363192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1364192895Sjamie			error = EPERM;
1365192895Sjamie			goto done_deref_locked;
1366192895Sjamie		}
1367192895Sjamie	}
1368231267Smm	if (gotrsnum) {
1369231267Smm		/*
1370231267Smm		 * devfs_rsnum is a uint16_t
1371231267Smm		 */
1372232059Smm		if (rsnum < 0 || rsnum > 65535) {
1373231267Smm			error = EINVAL;
1374231267Smm			goto done_deref_locked;
1375231267Smm		}
1376231267Smm		/*
1377232059Smm		 * Nested jails always inherit parent's devfs ruleset
1378231267Smm		 */
1379231267Smm		if (jailed(td->td_ucred)) {
1380231267Smm			if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1381231267Smm				error = EPERM;
1382231267Smm				goto done_deref_locked;
1383232059Smm			} else
1384231267Smm				rsnum = ppr->pr_devfs_rsnum;
1385231267Smm		}
1386231267Smm	}
1387185435Sbz#ifdef INET
1388195974Sjamie	if (ip4s > 0) {
1389192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1390195974Sjamie			/*
1391195974Sjamie			 * Make sure the new set of IP addresses is a
1392195974Sjamie			 * subset of the parent's list.  Don't worry
1393195974Sjamie			 * about the parent being unlocked, as any
1394195974Sjamie			 * setting is done with allprison_lock held.
1395195974Sjamie			 */
1396195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1397195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1398195974Sjamie					break;
1399195974Sjamie			if (ij == ppr->pr_ip4s) {
1400195974Sjamie				error = EPERM;
1401195974Sjamie				goto done_deref_locked;
1402195974Sjamie			}
1403195974Sjamie			if (ip4s > 1) {
1404195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1405195974Sjamie					if (ip4[ii].s_addr ==
1406195974Sjamie					    ppr->pr_ip4[0].s_addr)
1407195974Sjamie						continue;
1408195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1409195974Sjamie						if (ip4[ii].s_addr ==
1410195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1411195974Sjamie							break;
1412195974Sjamie					if (ij == ppr->pr_ip4s)
1413195974Sjamie						break;
1414192895Sjamie				}
1415192895Sjamie				if (ij == ppr->pr_ip4s) {
1416192895Sjamie					error = EPERM;
1417192895Sjamie					goto done_deref_locked;
1418192895Sjamie				}
1419192895Sjamie			}
1420192895Sjamie		}
1421195974Sjamie		/*
1422195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1423195974Sjamie		 * if there is no more than one IP on each jail.  If
1424195974Sjamie		 * there is a duplicate on a jail with more than one
1425195974Sjamie		 * IP stop checking and return error.
1426195974Sjamie		 */
1427195974Sjamie		tppr = ppr;
1428195945Sjamie#ifdef VIMAGE
1429195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1430195974Sjamie			if (tppr->pr_flags & PR_VNET)
1431195974Sjamie				break;
1432195945Sjamie#endif
1433195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1434195974Sjamie			if (tpr == pr ||
1435195945Sjamie#ifdef VIMAGE
1436195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1437195945Sjamie#endif
1438195974Sjamie			    tpr->pr_uref == 0) {
1439192895Sjamie				descend = 0;
1440195974Sjamie				continue;
1441195974Sjamie			}
1442195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1443195974Sjamie				continue;
1444195974Sjamie			descend = 0;
1445195974Sjamie			if (tpr->pr_ip4 == NULL ||
1446195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1447195974Sjamie				continue;
1448195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1449195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1450195974Sjamie					error = EADDRINUSE;
1451195974Sjamie					vfs_opterror(opts,
1452195974Sjamie					    "IPv4 addresses clash");
1453195974Sjamie					goto done_deref_locked;
1454192895Sjamie				}
1455192895Sjamie			}
1456192895Sjamie		}
1457192895Sjamie	}
1458185435Sbz#endif
1459191673Sjamie#ifdef INET6
1460195974Sjamie	if (ip6s > 0) {
1461192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1462195974Sjamie			/*
1463195974Sjamie			 * Make sure the new set of IP addresses is a
1464195974Sjamie			 * subset of the parent's list.
1465195974Sjamie			 */
1466195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1467195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1468195974Sjamie				    &ppr->pr_ip6[ij]))
1469195974Sjamie					break;
1470195974Sjamie			if (ij == ppr->pr_ip6s) {
1471195974Sjamie				error = EPERM;
1472195974Sjamie				goto done_deref_locked;
1473195974Sjamie			}
1474195974Sjamie			if (ip6s > 1) {
1475195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1476195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1477195974Sjamie					     &ppr->pr_ip6[0]))
1478195974Sjamie						continue;
1479195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1480195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1481195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1482195974Sjamie							break;
1483195974Sjamie					if (ij == ppr->pr_ip6s)
1484195974Sjamie						break;
1485192895Sjamie				}
1486192895Sjamie				if (ij == ppr->pr_ip6s) {
1487192895Sjamie					error = EPERM;
1488192895Sjamie					goto done_deref_locked;
1489192895Sjamie				}
1490192895Sjamie			}
1491192895Sjamie		}
1492195974Sjamie		/* Check for conflicting IP addresses. */
1493195974Sjamie		tppr = ppr;
1494195945Sjamie#ifdef VIMAGE
1495195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1496195974Sjamie			if (tppr->pr_flags & PR_VNET)
1497195974Sjamie				break;
1498195945Sjamie#endif
1499195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1500195974Sjamie			if (tpr == pr ||
1501195945Sjamie#ifdef VIMAGE
1502195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1503195945Sjamie#endif
1504195974Sjamie			    tpr->pr_uref == 0) {
1505192895Sjamie				descend = 0;
1506195974Sjamie				continue;
1507195974Sjamie			}
1508195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1509195974Sjamie				continue;
1510195974Sjamie			descend = 0;
1511195974Sjamie			if (tpr->pr_ip6 == NULL ||
1512195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1513195974Sjamie				continue;
1514195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1515195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1516195974Sjamie					error = EADDRINUSE;
1517195974Sjamie					vfs_opterror(opts,
1518195974Sjamie					    "IPv6 addresses clash");
1519195974Sjamie					goto done_deref_locked;
1520192895Sjamie				}
1521192895Sjamie			}
1522191673Sjamie		}
1523192895Sjamie	}
1524191673Sjamie#endif
1525192895Sjamie	onamelen = namelen = 0;
1526192895Sjamie	if (name != NULL) {
1527191673Sjamie		/* Give a default name of the jid. */
1528191673Sjamie		if (name[0] == '\0')
1529191673Sjamie			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1530196835Sjamie		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1531196835Sjamie		    *p == '\0')) {
1532191673Sjamie			error = EINVAL;
1533196835Sjamie			vfs_opterror(opts,
1534196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1535192895Sjamie			goto done_deref_locked;
1536191673Sjamie		}
1537191673Sjamie		/*
1538192895Sjamie		 * Make sure the name isn't too long for the prison or its
1539192895Sjamie		 * children.
1540191673Sjamie		 */
1541192895Sjamie		onamelen = strlen(pr->pr_name);
1542192895Sjamie		namelen = strlen(name);
1543192895Sjamie		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1544192895Sjamie			error = ENAMETOOLONG;
1545192895Sjamie			goto done_deref_locked;
1546192895Sjamie		}
1547192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1548192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1549192895Sjamie			    sizeof(pr->pr_name)) {
1550192895Sjamie				error = ENAMETOOLONG;
1551192895Sjamie				goto done_deref_locked;
1552192895Sjamie			}
1553192895Sjamie		}
1554191673Sjamie	}
1555192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1556192895Sjamie		error = EPERM;
1557192895Sjamie		goto done_deref_locked;
1558192895Sjamie	}
1559185435Sbz
1560191673Sjamie	/* Set the parameters of the prison. */
1561191673Sjamie#ifdef INET
1562192895Sjamie	redo_ip4 = 0;
1563195974Sjamie	if (pr_flags & PR_IP4_USER) {
1564195974Sjamie		pr->pr_flags |= PR_IP4;
1565195974Sjamie		free(pr->pr_ip4, M_PRISON);
1566195974Sjamie		pr->pr_ip4s = ip4s;
1567195974Sjamie		pr->pr_ip4 = ip4;
1568195974Sjamie		ip4 = NULL;
1569192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1570195945Sjamie#ifdef VIMAGE
1571195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1572195945Sjamie				descend = 0;
1573195945Sjamie				continue;
1574195945Sjamie			}
1575195945Sjamie#endif
1576192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1577192895Sjamie				redo_ip4 = 1;
1578192895Sjamie				descend = 0;
1579192895Sjamie			}
1580192895Sjamie		}
1581185435Sbz	}
1582191673Sjamie#endif
1583191673Sjamie#ifdef INET6
1584192895Sjamie	redo_ip6 = 0;
1585195974Sjamie	if (pr_flags & PR_IP6_USER) {
1586195974Sjamie		pr->pr_flags |= PR_IP6;
1587195974Sjamie		free(pr->pr_ip6, M_PRISON);
1588195974Sjamie		pr->pr_ip6s = ip6s;
1589195974Sjamie		pr->pr_ip6 = ip6;
1590195974Sjamie		ip6 = NULL;
1591192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1592195945Sjamie#ifdef VIMAGE
1593195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1594195945Sjamie				descend = 0;
1595195945Sjamie				continue;
1596195945Sjamie			}
1597195945Sjamie#endif
1598192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1599192895Sjamie				redo_ip6 = 1;
1600192895Sjamie				descend = 0;
1601192895Sjamie			}
1602192895Sjamie		}
1603191673Sjamie	}
1604191673Sjamie#endif
1605192895Sjamie	if (gotslevel) {
1606191673Sjamie		pr->pr_securelevel = slevel;
1607192895Sjamie		/* Set all child jails to be at least this level. */
1608192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1609192895Sjamie			if (tpr->pr_securelevel < slevel)
1610192895Sjamie				tpr->pr_securelevel = slevel;
1611192895Sjamie	}
1612194762Sjamie	if (gotchildmax) {
1613194762Sjamie		pr->pr_childmax = childmax;
1614194762Sjamie		/* Set all child jails to under this limit. */
1615194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1616194762Sjamie			if (tpr->pr_childmax > childmax - level)
1617194762Sjamie				tpr->pr_childmax = childmax > level
1618194762Sjamie				    ? childmax - level : 0;
1619194762Sjamie	}
1620192895Sjamie	if (gotenforce) {
1621192895Sjamie		pr->pr_enforce_statfs = enforce;
1622192895Sjamie		/* Pass this restriction on to the children. */
1623192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1624192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1625192895Sjamie				tpr->pr_enforce_statfs = enforce;
1626192895Sjamie	}
1627231267Smm	if (gotrsnum) {
1628231267Smm		pr->pr_devfs_rsnum = rsnum;
1629231267Smm		/* Pass this restriction on to the children. */
1630231267Smm		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1631232059Smm			tpr->pr_devfs_rsnum = rsnum;
1632231267Smm	}
1633192895Sjamie	if (name != NULL) {
1634192895Sjamie		if (ppr == &prison0)
1635192895Sjamie			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1636192895Sjamie		else
1637192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1638192895Sjamie			    ppr->pr_name, name);
1639192895Sjamie		/* Change this component of child names. */
1640192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1641192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1642192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1643192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1644192895Sjamie		}
1645192895Sjamie	}
1646191673Sjamie	if (path != NULL) {
1647192895Sjamie		/* Try to keep a real-rooted full pathname. */
1648230129Smm		if (fullpath_disabled && path[0] == '/' &&
1649230129Smm		    strcmp(mypr->pr_path, "/"))
1650192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1651192895Sjamie			    mypr->pr_path, path);
1652192895Sjamie		else
1653192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1654191673Sjamie		pr->pr_root = root;
1655191673Sjamie	}
1656193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1657193066Sjamie		if (pr->pr_flags & PR_HOST) {
1658193066Sjamie			/*
1659193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1660193066Sjamie			 * the lack of a lock on the parent is not a problem;
1661193066Sjamie			 * it is always set with allprison_lock at least
1662193066Sjamie			 * shared, and is held exclusively here.
1663193066Sjamie			 */
1664194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1665194118Sjamie			    sizeof(pr->pr_hostname));
1666194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1667194118Sjamie			    sizeof(pr->pr_domainname));
1668194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1669194118Sjamie			    sizeof(pr->pr_hostuuid));
1670193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1671193066Sjamie		}
1672193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1673193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1674193066Sjamie		if (host != NULL)
1675194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1676193066Sjamie		if (domain != NULL)
1677194118Sjamie			strlcpy(pr->pr_domainname, domain,
1678194118Sjamie			    sizeof(pr->pr_domainname));
1679193066Sjamie		if (uuid != NULL)
1680194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1681193066Sjamie		if (gothid)
1682193066Sjamie			pr->pr_hostid = hid;
1683193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1684193066Sjamie			if (tpr->pr_flags & PR_HOST)
1685193066Sjamie				descend = 0;
1686193066Sjamie			else {
1687193066Sjamie				if (host != NULL)
1688194118Sjamie					strlcpy(tpr->pr_hostname,
1689194118Sjamie					    pr->pr_hostname,
1690194118Sjamie					    sizeof(tpr->pr_hostname));
1691193066Sjamie				if (domain != NULL)
1692194118Sjamie					strlcpy(tpr->pr_domainname,
1693194118Sjamie					    pr->pr_domainname,
1694194118Sjamie					    sizeof(tpr->pr_domainname));
1695193066Sjamie				if (uuid != NULL)
1696194118Sjamie					strlcpy(tpr->pr_hostuuid,
1697194118Sjamie					    pr->pr_hostuuid,
1698194118Sjamie					    sizeof(tpr->pr_hostuuid));
1699193066Sjamie				if (gothid)
1700193066Sjamie					tpr->pr_hostid = hid;
1701193066Sjamie			}
1702193066Sjamie		}
1703193066Sjamie	}
1704192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1705192895Sjamie		/* Clear allow bits in all children. */
1706192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1707192895Sjamie			tpr->pr_allow &= ~tallow;
1708192895Sjamie	}
1709192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1710191673Sjamie	/*
1711191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1712191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1713191673Sjamie	 * for now, so new ones will remain unseen until after the module
1714191673Sjamie	 * handlers have completed.
1715191673Sjamie	 */
1716191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1717191673Sjamie		if (pr_flags & PR_PERSIST) {
1718191673Sjamie			pr->pr_ref++;
1719191673Sjamie			pr->pr_uref++;
1720191673Sjamie		} else {
1721191673Sjamie			pr->pr_ref--;
1722191673Sjamie			pr->pr_uref--;
1723191673Sjamie		}
1724191673Sjamie	}
1725191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1726191673Sjamie	mtx_unlock(&pr->pr_mtx);
1727185435Sbz
1728221362Strasz#ifdef RACCT
1729221362Strasz	if (created)
1730221362Strasz		prison_racct_attach(pr);
1731221362Strasz#endif
1732221362Strasz
1733192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1734192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1735192895Sjamie	 */
1736192895Sjamie#ifdef INET
1737192895Sjamie	while (redo_ip4) {
1738192895Sjamie		ip4s = pr->pr_ip4s;
1739192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1740192895Sjamie		mtx_lock(&pr->pr_mtx);
1741192895Sjamie		redo_ip4 = 0;
1742192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1743195945Sjamie#ifdef VIMAGE
1744195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1745195945Sjamie				descend = 0;
1746195945Sjamie				continue;
1747195945Sjamie			}
1748195945Sjamie#endif
1749192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1750192895Sjamie				if (ip4 != NULL)
1751192895Sjamie					ip4 = NULL;
1752192895Sjamie				else
1753192895Sjamie					redo_ip4 = 1;
1754192895Sjamie			}
1755192895Sjamie		}
1756192895Sjamie		mtx_unlock(&pr->pr_mtx);
1757192895Sjamie	}
1758192895Sjamie#endif
1759192895Sjamie#ifdef INET6
1760192895Sjamie	while (redo_ip6) {
1761192895Sjamie		ip6s = pr->pr_ip6s;
1762192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1763192895Sjamie		mtx_lock(&pr->pr_mtx);
1764192895Sjamie		redo_ip6 = 0;
1765192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1766195945Sjamie#ifdef VIMAGE
1767195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1768195945Sjamie				descend = 0;
1769195945Sjamie				continue;
1770195945Sjamie			}
1771195945Sjamie#endif
1772192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1773192895Sjamie				if (ip6 != NULL)
1774192895Sjamie					ip6 = NULL;
1775192895Sjamie				else
1776192895Sjamie					redo_ip6 = 1;
1777192895Sjamie			}
1778192895Sjamie		}
1779192895Sjamie		mtx_unlock(&pr->pr_mtx);
1780192895Sjamie	}
1781192895Sjamie#endif
1782192895Sjamie
1783191673Sjamie	/* Let the modules do their work. */
1784191673Sjamie	sx_downgrade(&allprison_lock);
1785191673Sjamie	if (created) {
1786191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1787191673Sjamie		if (error) {
1788191673Sjamie			prison_deref(pr, PD_LIST_SLOCKED);
1789191673Sjamie			goto done_errmsg;
1790191673Sjamie		}
1791191673Sjamie	}
1792191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1793191673Sjamie	if (error) {
1794191673Sjamie		prison_deref(pr, created
1795191673Sjamie		    ? PD_LIST_SLOCKED
1796191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1797191673Sjamie		goto done_errmsg;
1798191673Sjamie	}
1799191673Sjamie
1800191673Sjamie	/* Attach this process to the prison if requested. */
1801191673Sjamie	if (flags & JAIL_ATTACH) {
1802191673Sjamie		mtx_lock(&pr->pr_mtx);
1803191673Sjamie		error = do_jail_attach(td, pr);
1804191673Sjamie		if (error) {
1805191673Sjamie			vfs_opterror(opts, "attach failed");
1806191673Sjamie			if (!created)
1807191673Sjamie				prison_deref(pr, PD_DEREF);
1808191673Sjamie			goto done_errmsg;
1809191673Sjamie		}
1810191673Sjamie	}
1811191673Sjamie
1812235803Strasz#ifdef RACCT
1813235803Strasz	if (!created) {
1814235803Strasz		sx_sunlock(&allprison_lock);
1815235803Strasz		prison_racct_modify(pr);
1816235803Strasz		sx_slock(&allprison_lock);
1817235803Strasz	}
1818235803Strasz#endif
1819235803Strasz
1820235803Strasz	td->td_retval[0] = pr->pr_id;
1821235803Strasz
1822191673Sjamie	/*
1823191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1824191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1825191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1826191673Sjamie	 * visible).
1827191673Sjamie	 */
1828191673Sjamie	if (!created) {
1829191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1830191673Sjamie		    ? PD_DEREF
1831191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1832191673Sjamie	} else {
1833191673Sjamie		if (pr_flags & PR_PERSIST) {
1834191673Sjamie			mtx_lock(&pr->pr_mtx);
1835191673Sjamie			pr->pr_ref++;
1836191673Sjamie			pr->pr_uref++;
1837191673Sjamie			mtx_unlock(&pr->pr_mtx);
1838191673Sjamie		}
1839191673Sjamie		if (!(flags & JAIL_ATTACH))
1840191673Sjamie			sx_sunlock(&allprison_lock);
1841191673Sjamie	}
1842232598Strasz
1843191673Sjamie	goto done_errmsg;
1844191673Sjamie
1845192895Sjamie done_deref_locked:
1846192895Sjamie	prison_deref(pr, created
1847192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1848192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1849192895Sjamie	goto done_releroot;
1850191673Sjamie done_unlock_list:
1851191673Sjamie	sx_xunlock(&allprison_lock);
1852191673Sjamie done_releroot:
1853241896Skib	if (root != NULL)
1854191673Sjamie		vrele(root);
1855191673Sjamie done_errmsg:
1856191673Sjamie	if (error) {
1857191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1858191673Sjamie		if (errmsg_len > 0) {
1859191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1860191673Sjamie			if (errmsg_pos > 0) {
1861191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1862191673Sjamie					bcopy(errmsg,
1863191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1864191673Sjamie					   errmsg_len);
1865191673Sjamie				else
1866191673Sjamie					copyout(errmsg,
1867191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1868191673Sjamie					   errmsg_len);
1869191673Sjamie			}
1870191673Sjamie		}
1871191673Sjamie	}
1872191673Sjamie done_free:
1873191673Sjamie#ifdef INET
1874191673Sjamie	free(ip4, M_PRISON);
1875191673Sjamie#endif
1876191673Sjamie#ifdef INET6
1877191673Sjamie	free(ip6, M_PRISON);
1878191673Sjamie#endif
1879230407Smm	if (g_path != NULL)
1880230407Smm		free(g_path, M_TEMP);
1881191673Sjamie	vfs_freeopts(opts);
1882191673Sjamie	return (error);
1883191673Sjamie}
1884191673Sjamie
1885191673Sjamie
188682710Sdillon/*
1887191673Sjamie * struct jail_get_args {
1888191673Sjamie *	struct iovec *iovp;
1889191673Sjamie *	unsigned int iovcnt;
1890191673Sjamie *	int flags;
1891114168Smike * };
189282710Sdillon */
189346155Sphkint
1894225617Skmacysys_jail_get(struct thread *td, struct jail_get_args *uap)
189546155Sphk{
1896191673Sjamie	struct uio *auio;
1897185435Sbz	int error;
1898185435Sbz
1899191673Sjamie	/* Check that we have an even number of iovecs. */
1900191673Sjamie	if (uap->iovcnt & 1)
1901191673Sjamie		return (EINVAL);
1902191673Sjamie
1903191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1904185435Sbz	if (error)
1905185435Sbz		return (error);
1906191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1907191673Sjamie	if (error == 0)
1908191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1909191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1910191673Sjamie	free(auio, M_IOV);
1911191673Sjamie	return (error);
1912191673Sjamie}
1913185435Sbz
1914191673Sjamieint
1915191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
1916191673Sjamie{
1917192895Sjamie	struct prison *pr, *mypr;
1918191673Sjamie	struct vfsopt *opt;
1919191673Sjamie	struct vfsoptlist *opts;
1920191673Sjamie	char *errmsg, *name;
1921192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1922185435Sbz
1923191673Sjamie	if (flags & ~JAIL_GET_MASK)
1924191673Sjamie		return (EINVAL);
1925185435Sbz
1926191673Sjamie	/* Get the parameter list. */
1927191673Sjamie	error = vfs_buildopts(optuio, &opts);
1928191673Sjamie	if (error)
1929191673Sjamie		return (error);
1930191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1931192895Sjamie	mypr = td->td_ucred->cr_prison;
1932185435Sbz
1933191673Sjamie	/*
1934191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
1935191673Sjamie	 */
1936191673Sjamie	sx_slock(&allprison_lock);
1937191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1938191673Sjamie	if (error == 0) {
1939191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
1940192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1941191673Sjamie				mtx_lock(&pr->pr_mtx);
1942191673Sjamie				if (pr->pr_ref > 0 &&
1943191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1944191673Sjamie					break;
1945191673Sjamie				mtx_unlock(&pr->pr_mtx);
1946191673Sjamie			}
1947191673Sjamie		}
1948191673Sjamie		if (pr != NULL)
1949191673Sjamie			goto found_prison;
1950191673Sjamie		error = ENOENT;
1951191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
1952191673Sjamie		goto done_unlock_list;
1953191673Sjamie	} else if (error != ENOENT)
1954191673Sjamie		goto done_unlock_list;
1955185435Sbz
1956191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1957191673Sjamie	if (error == 0) {
1958191673Sjamie		if (jid != 0) {
1959192895Sjamie			pr = prison_find_child(mypr, jid);
1960191673Sjamie			if (pr != NULL) {
1961191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1962191673Sjamie					mtx_unlock(&pr->pr_mtx);
1963191673Sjamie					error = ENOENT;
1964191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1965191673Sjamie					    jid);
1966191673Sjamie					goto done_unlock_list;
1967191673Sjamie				}
1968191673Sjamie				goto found_prison;
1969191673Sjamie			}
1970191673Sjamie			error = ENOENT;
1971191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
1972191673Sjamie			goto done_unlock_list;
1973191673Sjamie		}
1974191673Sjamie	} else if (error != ENOENT)
1975191673Sjamie		goto done_unlock_list;
197646155Sphk
1977191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
1978191673Sjamie	if (error == 0) {
1979191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
1980191673Sjamie			error = EINVAL;
1981191673Sjamie			goto done_unlock_list;
1982191673Sjamie		}
1983192895Sjamie		pr = prison_find_name(mypr, name);
1984191673Sjamie		if (pr != NULL) {
1985191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1986191673Sjamie				mtx_unlock(&pr->pr_mtx);
1987191673Sjamie				error = ENOENT;
1988191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
1989191673Sjamie				    name);
1990191673Sjamie				goto done_unlock_list;
1991191673Sjamie			}
1992191673Sjamie			goto found_prison;
1993191673Sjamie		}
1994191673Sjamie		error = ENOENT;
1995191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
1996191673Sjamie		goto done_unlock_list;
1997191673Sjamie	} else if (error != ENOENT)
1998191673Sjamie		goto done_unlock_list;
1999185435Sbz
2000191673Sjamie	vfs_opterror(opts, "no jail specified");
2001191673Sjamie	error = ENOENT;
2002191673Sjamie	goto done_unlock_list;
2003191673Sjamie
2004191673Sjamie found_prison:
2005191673Sjamie	/* Get the parameters of the prison. */
2006191673Sjamie	pr->pr_ref++;
2007191673Sjamie	locked = PD_LOCKED;
2008191673Sjamie	td->td_retval[0] = pr->pr_id;
2009191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2010191673Sjamie	if (error != 0 && error != ENOENT)
2011191673Sjamie		goto done_deref;
2012192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2013192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
2014191673Sjamie	if (error != 0 && error != ENOENT)
2015191673Sjamie		goto done_deref;
2016192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2017192895Sjamie	if (error != 0 && error != ENOENT)
2018192895Sjamie		goto done_deref;
2019192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2020191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
2021191673Sjamie	if (error != 0 && error != ENOENT)
2022191673Sjamie		goto done_deref;
2023192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2024191673Sjamie	if (error != 0 && error != ENOENT)
2025191673Sjamie		goto done_deref;
2026191673Sjamie#ifdef INET
2027191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2028191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
2029191673Sjamie	if (error != 0 && error != ENOENT)
2030191673Sjamie		goto done_deref;
2031191673Sjamie#endif
2032191673Sjamie#ifdef INET6
2033191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2034191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
2035191673Sjamie	if (error != 0 && error != ENOENT)
2036191673Sjamie		goto done_deref;
2037191673Sjamie#endif
2038191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2039191673Sjamie	    sizeof(pr->pr_securelevel));
2040191673Sjamie	if (error != 0 && error != ENOENT)
2041191673Sjamie		goto done_deref;
2042194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2043194762Sjamie	    sizeof(pr->pr_childcount));
2044194762Sjamie	if (error != 0 && error != ENOENT)
2045194762Sjamie		goto done_deref;
2046194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2047194762Sjamie	    sizeof(pr->pr_childmax));
2048194762Sjamie	if (error != 0 && error != ENOENT)
2049194762Sjamie		goto done_deref;
2050194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2051191673Sjamie	if (error != 0 && error != ENOENT)
2052191673Sjamie		goto done_deref;
2053194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2054193066Sjamie	if (error != 0 && error != ENOENT)
2055193066Sjamie		goto done_deref;
2056194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2057193066Sjamie	if (error != 0 && error != ENOENT)
2058193066Sjamie		goto done_deref;
2059205014Snwhitehorn#ifdef COMPAT_FREEBSD32
2060217896Sdchagin	if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2061193066Sjamie		uint32_t hid32 = pr->pr_hostid;
2062193066Sjamie
2063193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2064193066Sjamie	} else
2065193066Sjamie#endif
2066193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2067193066Sjamie	    sizeof(pr->pr_hostid));
2068193066Sjamie	if (error != 0 && error != ENOENT)
2069193066Sjamie		goto done_deref;
2070192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2071192895Sjamie	    sizeof(pr->pr_enforce_statfs));
2072191673Sjamie	if (error != 0 && error != ENOENT)
2073191673Sjamie		goto done_deref;
2074231267Smm	error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2075231267Smm	    sizeof(pr->pr_devfs_rsnum));
2076231267Smm	if (error != 0 && error != ENOENT)
2077231267Smm		goto done_deref;
2078192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2079192895Sjamie	    fi++) {
2080192895Sjamie		if (pr_flag_names[fi] == NULL)
2081192895Sjamie			continue;
2082192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2083192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2084192895Sjamie		if (error != 0 && error != ENOENT)
2085192895Sjamie			goto done_deref;
2086192895Sjamie		i = !i;
2087192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2088192895Sjamie		if (error != 0 && error != ENOENT)
2089192895Sjamie			goto done_deref;
2090192895Sjamie	}
2091195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2092195870Sjamie	    fi++) {
2093195870Sjamie		i = pr->pr_flags &
2094195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2095195870Sjamie		i = pr_flag_jailsys[fi].disable &&
2096195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2097195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2098195870Sjamie		    : JAIL_SYS_INHERIT;
2099195870Sjamie		error =
2100195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2101195870Sjamie		if (error != 0 && error != ENOENT)
2102195870Sjamie			goto done_deref;
2103195870Sjamie	}
2104192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2105192895Sjamie	    fi++) {
2106192895Sjamie		if (pr_allow_names[fi] == NULL)
2107192895Sjamie			continue;
2108192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2109192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2110192895Sjamie		if (error != 0 && error != ENOENT)
2111192895Sjamie			goto done_deref;
2112192895Sjamie		i = !i;
2113192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2114192895Sjamie		if (error != 0 && error != ENOENT)
2115192895Sjamie			goto done_deref;
2116192895Sjamie	}
2117191673Sjamie	i = (pr->pr_uref == 0);
2118191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2119191673Sjamie	if (error != 0 && error != ENOENT)
2120191673Sjamie		goto done_deref;
2121191673Sjamie	i = !i;
2122191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2123191673Sjamie	if (error != 0 && error != ENOENT)
2124191673Sjamie		goto done_deref;
2125191673Sjamie
2126191673Sjamie	/* Get the module parameters. */
2127191673Sjamie	mtx_unlock(&pr->pr_mtx);
2128191673Sjamie	locked = 0;
2129191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
213046155Sphk	if (error)
2131191673Sjamie		goto done_deref;
2132191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
213384828Sjhb
2134191673Sjamie	/* By now, all parameters should have been noted. */
2135191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2136191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2137191673Sjamie			error = EINVAL;
2138191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2139191673Sjamie			goto done_errmsg;
2140191673Sjamie		}
2141185435Sbz	}
2142191673Sjamie
2143191673Sjamie	/* Write the fetched parameters back to userspace. */
2144191673Sjamie	error = 0;
2145191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2146191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2147191673Sjamie			pos = 2 * opt->pos + 1;
2148191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2149191673Sjamie			if (opt->value != NULL) {
2150191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2151191673Sjamie					bcopy(opt->value,
2152191673Sjamie					    optuio->uio_iov[pos].iov_base,
2153191673Sjamie					    opt->len);
2154191673Sjamie				} else {
2155191673Sjamie					error = copyout(opt->value,
2156191673Sjamie					    optuio->uio_iov[pos].iov_base,
2157191673Sjamie					    opt->len);
2158191673Sjamie					if (error)
2159191673Sjamie						break;
2160191673Sjamie				}
2161191673Sjamie			}
2162191673Sjamie		}
2163185435Sbz	}
2164191673Sjamie	goto done_errmsg;
2165191673Sjamie
2166191673Sjamie done_deref:
2167191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2168191673Sjamie	goto done_errmsg;
2169191673Sjamie
2170191673Sjamie done_unlock_list:
2171191673Sjamie	sx_sunlock(&allprison_lock);
2172191673Sjamie done_errmsg:
2173191673Sjamie	if (error && errmsg_pos >= 0) {
2174191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2175191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2176191673Sjamie		if (errmsg_len > 0) {
2177191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2178191673Sjamie				bcopy(errmsg,
2179191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2180191673Sjamie				    errmsg_len);
2181191673Sjamie			else
2182191673Sjamie				copyout(errmsg,
2183191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2184191673Sjamie				    errmsg_len);
2185191673Sjamie		}
2186185435Sbz	}
2187191673Sjamie	vfs_freeopts(opts);
2188191673Sjamie	return (error);
2189191673Sjamie}
2190113275Smike
2191192895Sjamie
2192191673Sjamie/*
2193191673Sjamie * struct jail_remove_args {
2194191673Sjamie *	int jid;
2195191673Sjamie * };
2196191673Sjamie */
2197191673Sjamieint
2198225617Skmacysys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2199191673Sjamie{
2200192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2201192895Sjamie	int descend, error;
2202185435Sbz
2203191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2204185435Sbz	if (error)
2205191673Sjamie		return (error);
2206185435Sbz
2207185435Sbz	sx_xlock(&allprison_lock);
2208192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2209191673Sjamie	if (pr == NULL) {
2210185435Sbz		sx_xunlock(&allprison_lock);
2211191673Sjamie		return (EINVAL);
2212185435Sbz	}
2213185435Sbz
2214192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2215192895Sjamie	pr->pr_ref++;
2216192895Sjamie	pr->pr_flags |= PR_REMOVE;
2217192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2218192895Sjamie		mtx_unlock(&pr->pr_mtx);
2219192895Sjamie		lpr = NULL;
2220192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2221192895Sjamie			mtx_lock(&cpr->pr_mtx);
2222192895Sjamie			if (cpr->pr_ref > 0) {
2223192895Sjamie				tpr = cpr;
2224192895Sjamie				cpr->pr_ref++;
2225192895Sjamie				cpr->pr_flags |= PR_REMOVE;
2226192895Sjamie			} else {
2227192895Sjamie				/* Already removed - do not do it again. */
2228192895Sjamie				tpr = NULL;
2229192895Sjamie			}
2230192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2231192895Sjamie			if (lpr != NULL) {
2232192895Sjamie				mtx_lock(&lpr->pr_mtx);
2233192895Sjamie				prison_remove_one(lpr);
2234192895Sjamie				sx_xlock(&allprison_lock);
2235192895Sjamie			}
2236192895Sjamie			lpr = tpr;
2237192895Sjamie		}
2238192895Sjamie		if (lpr != NULL) {
2239192895Sjamie			mtx_lock(&lpr->pr_mtx);
2240192895Sjamie			prison_remove_one(lpr);
2241192895Sjamie			sx_xlock(&allprison_lock);
2242192895Sjamie		}
2243192895Sjamie		mtx_lock(&pr->pr_mtx);
2244192895Sjamie	}
2245192895Sjamie	prison_remove_one(pr);
2246192895Sjamie	return (0);
2247192895Sjamie}
2248192895Sjamie
2249192895Sjamiestatic void
2250192895Sjamieprison_remove_one(struct prison *pr)
2251192895Sjamie{
2252192895Sjamie	struct proc *p;
2253192895Sjamie	int deuref;
2254192895Sjamie
2255191673Sjamie	/* If the prison was persistent, it is not anymore. */
2256191673Sjamie	deuref = 0;
2257191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2258191673Sjamie		pr->pr_ref--;
2259191673Sjamie		deuref = PD_DEUREF;
2260191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2261179881Sdelphij	}
2262113275Smike
2263192895Sjamie	/*
2264192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2265192895Sjamie	 * the prison now.
2266192895Sjamie	 */
2267192895Sjamie	KASSERT(pr->pr_ref > 0,
2268192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2269192895Sjamie	if (pr->pr_ref == 1) {
2270191673Sjamie		prison_deref(pr,
2271191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2272192895Sjamie		return;
2273191673Sjamie	}
2274191673Sjamie
2275113275Smike	mtx_unlock(&pr->pr_mtx);
2276191673Sjamie	sx_xunlock(&allprison_lock);
2277191673Sjamie	/*
2278191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2279191673Sjamie	 */
2280191673Sjamie	sx_slock(&allproc_lock);
2281191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2282191673Sjamie		PROC_LOCK(p);
2283191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2284191673Sjamie		    p->p_ucred->cr_prison == pr)
2285225617Skmacy			kern_psignal(p, SIGKILL);
2286191673Sjamie		PROC_UNLOCK(p);
2287191673Sjamie	}
2288191673Sjamie	sx_sunlock(&allproc_lock);
2289192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2290191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2291113275Smike}
2292113275Smike
2293190466Sjamie
2294113275Smike/*
2295114168Smike * struct jail_attach_args {
2296114168Smike *	int jid;
2297114168Smike * };
2298113275Smike */
2299113275Smikeint
2300225617Skmacysys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2301113275Smike{
2302113275Smike	struct prison *pr;
2303191673Sjamie	int error;
2304167309Spjd
2305164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2306126023Snectar	if (error)
2307126023Snectar		return (error);
2308126023Snectar
2309168401Spjd	sx_slock(&allprison_lock);
2310192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2311113275Smike	if (pr == NULL) {
2312168401Spjd		sx_sunlock(&allprison_lock);
2313113275Smike		return (EINVAL);
2314113275Smike	}
2315185435Sbz
2316185435Sbz	/*
2317185435Sbz	 * Do not allow a process to attach to a prison that is not
2318191673Sjamie	 * considered to be "alive".
2319185435Sbz	 */
2320191673Sjamie	if (pr->pr_uref == 0) {
2321185435Sbz		mtx_unlock(&pr->pr_mtx);
2322185435Sbz		sx_sunlock(&allprison_lock);
2323185435Sbz		return (EINVAL);
2324185435Sbz	}
2325191673Sjamie
2326191673Sjamie	return (do_jail_attach(td, pr));
2327191673Sjamie}
2328191673Sjamie
2329191673Sjamiestatic int
2330191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2331191673Sjamie{
2332192895Sjamie	struct prison *ppr;
2333191673Sjamie	struct proc *p;
2334191673Sjamie	struct ucred *newcred, *oldcred;
2335241896Skib	int error;
2336191673Sjamie
2337191673Sjamie	/*
2338191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2339191673Sjamie	 * in the same privileged process attempt to attach to two
2340191673Sjamie	 * different jails at the same time.  It is important for
2341191673Sjamie	 * user processes not to do this, or they might end up with
2342191673Sjamie	 * a process root from one prison, but attached to the jail
2343191673Sjamie	 * of another.
2344191673Sjamie	 */
2345113275Smike	pr->pr_ref++;
2346191673Sjamie	pr->pr_uref++;
2347113275Smike	mtx_unlock(&pr->pr_mtx);
2348191673Sjamie
2349191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2350191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2351191673Sjamie	if (error) {
2352191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2353191673Sjamie		return (error);
2354191673Sjamie	}
2355168401Spjd	sx_sunlock(&allprison_lock);
2356113275Smike
2357185435Sbz	/*
2358185435Sbz	 * Reparent the newly attached process to this jail.
2359185435Sbz	 */
2360192895Sjamie	ppr = td->td_ucred->cr_prison;
2361191673Sjamie	p = td->td_proc;
2362185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2363185435Sbz	if (error)
2364191673Sjamie		goto e_revert_osd;
2365185435Sbz
2366175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2367113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2368113275Smike		goto e_unlock;
2369113275Smike#ifdef MAC
2370172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2371113275Smike		goto e_unlock;
2372113275Smike#endif
2373175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2374191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2375241896Skib		goto e_revert_osd;
2376113275Smike
237784828Sjhb	newcred = crget();
237884828Sjhb	PROC_LOCK(p);
237984828Sjhb	oldcred = p->p_ucred;
2380113275Smike	setsugid(p);
238184828Sjhb	crcopy(newcred, oldcred);
2382113630Sjhb	newcred->cr_prison = pr;
238384828Sjhb	p->p_ucred = newcred;
238484828Sjhb	PROC_UNLOCK(p);
2385220137Strasz#ifdef RACCT
2386220137Strasz	racct_proc_ucred_changed(p, oldcred, newcred);
2387220137Strasz#endif
238884828Sjhb	crfree(oldcred);
2389192895Sjamie	prison_deref(ppr, PD_DEREF | PD_DEUREF);
239046155Sphk	return (0);
2391191673Sjamie e_unlock:
2392175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2393191673Sjamie e_revert_osd:
2394191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2395192895Sjamie	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2396191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
239746155Sphk	return (error);
239846155Sphk}
239946155Sphk
2400192895Sjamie
2401113275Smike/*
2402113275Smike * Returns a locked prison instance, or NULL on failure.
2403113275Smike */
2404168399Spjdstruct prison *
2405113275Smikeprison_find(int prid)
2406113275Smike{
2407113275Smike	struct prison *pr;
2408113275Smike
2409168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2410191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2411113275Smike		if (pr->pr_id == prid) {
2412113275Smike			mtx_lock(&pr->pr_mtx);
2413191673Sjamie			if (pr->pr_ref > 0)
2414191673Sjamie				return (pr);
2415191673Sjamie			mtx_unlock(&pr->pr_mtx);
2416113275Smike		}
2417113275Smike	}
2418113275Smike	return (NULL);
2419113275Smike}
2420113275Smike
2421191673Sjamie/*
2422192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2423191673Sjamie */
2424191673Sjamiestruct prison *
2425192895Sjamieprison_find_child(struct prison *mypr, int prid)
2426191673Sjamie{
2427192895Sjamie	struct prison *pr;
2428192895Sjamie	int descend;
2429192895Sjamie
2430192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2431192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2432192895Sjamie		if (pr->pr_id == prid) {
2433192895Sjamie			mtx_lock(&pr->pr_mtx);
2434192895Sjamie			if (pr->pr_ref > 0)
2435192895Sjamie				return (pr);
2436192895Sjamie			mtx_unlock(&pr->pr_mtx);
2437192895Sjamie		}
2438192895Sjamie	}
2439192895Sjamie	return (NULL);
2440192895Sjamie}
2441192895Sjamie
2442192895Sjamie/*
2443192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2444192895Sjamie */
2445192895Sjamiestruct prison *
2446192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2447192895Sjamie{
2448191673Sjamie	struct prison *pr, *deadpr;
2449192895Sjamie	size_t mylen;
2450192895Sjamie	int descend;
2451191673Sjamie
2452191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2453192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2454191673Sjamie again:
2455191673Sjamie	deadpr = NULL;
2456192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2457192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2458191673Sjamie			mtx_lock(&pr->pr_mtx);
2459191673Sjamie			if (pr->pr_ref > 0) {
2460191673Sjamie				if (pr->pr_uref > 0)
2461191673Sjamie					return (pr);
2462191673Sjamie				deadpr = pr;
2463191673Sjamie			}
2464191673Sjamie			mtx_unlock(&pr->pr_mtx);
2465191673Sjamie		}
2466191673Sjamie	}
2467192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2468191673Sjamie	if (deadpr != NULL) {
2469191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2470191673Sjamie		if (deadpr->pr_ref == 0) {
2471191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2472191673Sjamie			goto again;
2473191673Sjamie		}
2474191673Sjamie	}
2475191673Sjamie	return (deadpr);
2476191673Sjamie}
2477191673Sjamie
2478191673Sjamie/*
2479192895Sjamie * See if a prison has the specific flag set.
2480192895Sjamie */
2481192895Sjamieint
2482192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2483192895Sjamie{
2484192895Sjamie
2485192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2486192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2487192895Sjamie}
2488192895Sjamie
2489192895Sjamieint
2490192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2491192895Sjamie{
2492192895Sjamie
2493192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2494192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2495192895Sjamie}
2496192895Sjamie
2497192895Sjamie/*
2498191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2499191673Sjamie * prison itself - but not in this context in case there are locks held.
2500191673Sjamie */
250172786Srwatsonvoid
2502185029Spjdprison_free_locked(struct prison *pr)
250372786Srwatson{
250472786Srwatson
2505185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
250672786Srwatson	pr->pr_ref--;
250772786Srwatson	if (pr->pr_ref == 0) {
2508168483Spjd		mtx_unlock(&pr->pr_mtx);
2509124882Srwatson		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2510144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
251187275Srwatson		return;
251272786Srwatson	}
251387275Srwatson	mtx_unlock(&pr->pr_mtx);
251472786Srwatson}
251572786Srwatson
2516185029Spjdvoid
2517185029Spjdprison_free(struct prison *pr)
2518185029Spjd{
2519185029Spjd
2520185029Spjd	mtx_lock(&pr->pr_mtx);
2521185029Spjd	prison_free_locked(pr);
2522185029Spjd}
2523185029Spjd
2524124882Srwatsonstatic void
2525124882Srwatsonprison_complete(void *context, int pending)
2526124882Srwatson{
2527191673Sjamie
2528191673Sjamie	prison_deref((struct prison *)context, 0);
2529191673Sjamie}
2530191673Sjamie
2531191673Sjamie/*
2532191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2533191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2534191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2535191673Sjamie * and the allprison lock are both unlocked.
2536191673Sjamie */
2537191673Sjamiestatic void
2538191673Sjamieprison_deref(struct prison *pr, int flags)
2539191673Sjamie{
2540192895Sjamie	struct prison *ppr, *tpr;
2541124882Srwatson
2542191673Sjamie	if (!(flags & PD_LOCKED))
2543191673Sjamie		mtx_lock(&pr->pr_mtx);
2544225191Sjamie	for (;;) {
2545225191Sjamie		if (flags & PD_DEUREF) {
2546225191Sjamie			pr->pr_uref--;
2547225191Sjamie			KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2548192895Sjamie		}
2549192895Sjamie		if (flags & PD_DEREF)
2550192895Sjamie			pr->pr_ref--;
2551192895Sjamie		/* If the prison still has references, nothing else to do. */
2552192895Sjamie		if (pr->pr_ref > 0) {
2553192895Sjamie			mtx_unlock(&pr->pr_mtx);
2554192895Sjamie			if (flags & PD_LIST_SLOCKED)
2555192895Sjamie				sx_sunlock(&allprison_lock);
2556192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2557192895Sjamie				sx_xunlock(&allprison_lock);
2558192895Sjamie			return;
2559191673Sjamie		}
2560191673Sjamie
2561192895Sjamie		mtx_unlock(&pr->pr_mtx);
2562192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2563192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2564192895Sjamie				sx_sunlock(&allprison_lock);
2565192895Sjamie				sx_xlock(&allprison_lock);
2566192895Sjamie			}
2567192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2568192895Sjamie			sx_xlock(&allprison_lock);
2569168489Spjd
2570192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2571192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2572192895Sjamie		ppr = pr->pr_parent;
2573192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2574194762Sjamie			tpr->pr_childcount--;
2575196592Sjamie		sx_xunlock(&allprison_lock);
2576192895Sjamie
2577194251Sjamie#ifdef VIMAGE
2578196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2579194251Sjamie			vnet_destroy(pr->pr_vnet);
2580194251Sjamie#endif
2581241896Skib		if (pr->pr_root != NULL)
2582192895Sjamie			vrele(pr->pr_root);
2583192895Sjamie		mtx_destroy(&pr->pr_mtx);
2584191673Sjamie#ifdef INET
2585192895Sjamie		free(pr->pr_ip4, M_PRISON);
2586191673Sjamie#endif
2587185435Sbz#ifdef INET6
2588192895Sjamie		free(pr->pr_ip6, M_PRISON);
2589185435Sbz#endif
2590192895Sjamie		if (pr->pr_cpuset != NULL)
2591192895Sjamie			cpuset_rel(pr->pr_cpuset);
2592192895Sjamie		osd_jail_exit(pr);
2593221362Strasz#ifdef RACCT
2594221362Strasz		prison_racct_detach(pr);
2595220163Strasz#endif
2596192895Sjamie		free(pr, M_PRISON);
2597192895Sjamie
2598192895Sjamie		/* Removing a prison frees a reference on its parent. */
2599192895Sjamie		pr = ppr;
2600192895Sjamie		mtx_lock(&pr->pr_mtx);
2601225191Sjamie		flags = PD_DEREF | PD_DEUREF;
2602192895Sjamie	}
2603124882Srwatson}
2604124882Srwatson
260572786Srwatsonvoid
2606185029Spjdprison_hold_locked(struct prison *pr)
260772786Srwatson{
260872786Srwatson
2609185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2610168489Spjd	KASSERT(pr->pr_ref > 0,
2611191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
261272786Srwatson	pr->pr_ref++;
2613185029Spjd}
2614185029Spjd
2615185029Spjdvoid
2616185029Spjdprison_hold(struct prison *pr)
2617185029Spjd{
2618185029Spjd
2619185029Spjd	mtx_lock(&pr->pr_mtx);
2620185029Spjd	prison_hold_locked(pr);
262187275Srwatson	mtx_unlock(&pr->pr_mtx);
262272786Srwatson}
262372786Srwatson
2624185435Sbzvoid
2625185435Sbzprison_proc_hold(struct prison *pr)
262687275Srwatson{
262787275Srwatson
2628185435Sbz	mtx_lock(&pr->pr_mtx);
2629191673Sjamie	KASSERT(pr->pr_uref > 0,
2630191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2631191673Sjamie	pr->pr_uref++;
2632185435Sbz	mtx_unlock(&pr->pr_mtx);
263387275Srwatson}
263487275Srwatson
2635185435Sbzvoid
2636185435Sbzprison_proc_free(struct prison *pr)
2637185435Sbz{
2638185435Sbz
2639185435Sbz	mtx_lock(&pr->pr_mtx);
2640191673Sjamie	KASSERT(pr->pr_uref > 0,
2641191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2642191673Sjamie	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2643185435Sbz}
2644185435Sbz
2645185435Sbz
2646185435Sbz#ifdef INET
2647185435Sbz/*
2648192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2649192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2650192895Sjamie */
2651192895Sjamiestatic int
2652192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2653192895Sjamie{
2654192895Sjamie	int ii, ij, used;
2655192895Sjamie	struct prison *ppr;
2656192895Sjamie
2657192895Sjamie	ppr = pr->pr_parent;
2658192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2659192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2660192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2661192895Sjamie			/*
2662192895Sjamie			 * There's no room for the parent's list.  Use the
2663192895Sjamie			 * new list buffer, which is assumed to be big enough
2664192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2665192895Sjamie			 * allocate one.
2666192895Sjamie			 */
2667192895Sjamie			used = 1;
2668192895Sjamie			if (newip4 == NULL) {
2669192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2670192895Sjamie				    M_PRISON, M_NOWAIT);
2671192895Sjamie				if (newip4 != NULL)
2672192895Sjamie					used = 0;
2673192895Sjamie			}
2674192895Sjamie			if (newip4 != NULL) {
2675192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2676192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2677192895Sjamie				free(pr->pr_ip4, M_PRISON);
2678192895Sjamie				pr->pr_ip4 = newip4;
2679192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2680192895Sjamie			}
2681192895Sjamie			return (used);
2682192895Sjamie		}
2683192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2684192895Sjamie		if (pr->pr_ip4s > 0)
2685192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2686192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2687192895Sjamie		else if (pr->pr_ip4 != NULL) {
2688192895Sjamie			free(pr->pr_ip4, M_PRISON);
2689192895Sjamie			pr->pr_ip4 = NULL;
2690192895Sjamie		}
2691195974Sjamie	} else if (pr->pr_ip4s > 0) {
2692192895Sjamie		/* Remove addresses that aren't in the parent. */
2693192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2694192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2695192895Sjamie				break;
2696192895Sjamie		if (ij < ppr->pr_ip4s)
2697192895Sjamie			ii = 1;
2698192895Sjamie		else {
2699192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2700192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2701192895Sjamie			ii = 0;
2702192895Sjamie		}
2703192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2704192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2705192895Sjamie				ii++;
2706192895Sjamie				continue;
2707192895Sjamie			}
2708192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2709192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2710192895Sjamie			case -1:
2711192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2712192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2713192895Sjamie				break;
2714192895Sjamie			case 0:
2715192895Sjamie				ii++;
2716192895Sjamie				ij++;
2717192895Sjamie				break;
2718192895Sjamie			case 1:
2719192895Sjamie				ij++;
2720192895Sjamie				break;
2721192895Sjamie			}
2722192895Sjamie		}
2723192895Sjamie		if (pr->pr_ip4s == 0) {
2724195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2725192895Sjamie			free(pr->pr_ip4, M_PRISON);
2726192895Sjamie			pr->pr_ip4 = NULL;
2727192895Sjamie		}
2728192895Sjamie	}
2729192895Sjamie	return (0);
2730192895Sjamie}
2731192895Sjamie
2732192895Sjamie/*
2733185435Sbz * Pass back primary IPv4 address of this jail.
2734185435Sbz *
2735192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2736192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2737185435Sbz *
2738188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2739188144Sjamie * Address returned in NBO.
2740185435Sbz */
274146155Sphkint
2742187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
274346155Sphk{
2744191673Sjamie	struct prison *pr;
274546155Sphk
2746185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2747185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2748185435Sbz
2749192895Sjamie	pr = cred->cr_prison;
2750192895Sjamie	if (!(pr->pr_flags & PR_IP4))
275146155Sphk		return (0);
2752191673Sjamie	mtx_lock(&pr->pr_mtx);
2753192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2754192895Sjamie		mtx_unlock(&pr->pr_mtx);
2755192895Sjamie		return (0);
2756192895Sjamie	}
2757191673Sjamie	if (pr->pr_ip4 == NULL) {
2758191673Sjamie		mtx_unlock(&pr->pr_mtx);
2759188144Sjamie		return (EAFNOSUPPORT);
2760191673Sjamie	}
2761185435Sbz
2762191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2763191673Sjamie	mtx_unlock(&pr->pr_mtx);
2764185435Sbz	return (0);
2765185435Sbz}
2766185435Sbz
2767185435Sbz/*
2768202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2769202468Sbz * We will return 0 if we should bypass source address selection in favour
2770202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2771202468Sbz * returned in NBO.
2772202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2773202468Sbz */
2774202468Sbzint
2775202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2776202468Sbz{
2777202468Sbz	struct prison *pr;
2778202468Sbz	struct in_addr lia;
2779202468Sbz	int error;
2780202468Sbz
2781202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2782202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2783202468Sbz
2784202468Sbz	if (!jailed(cred))
2785202468Sbz		return (1);
2786202468Sbz
2787202468Sbz	pr = cred->cr_prison;
2788202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2789202468Sbz		return (1);
2790202468Sbz
2791202468Sbz	lia.s_addr = INADDR_ANY;
2792202468Sbz	error = prison_get_ip4(cred, &lia);
2793202468Sbz	if (error)
2794202468Sbz		return (error);
2795202468Sbz	if (lia.s_addr == INADDR_ANY)
2796202468Sbz		return (1);
2797202468Sbz
2798202468Sbz	ia->s_addr = lia.s_addr;
2799202468Sbz	return (0);
2800202468Sbz}
2801202468Sbz
2802202468Sbz/*
2803192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2804192895Sjamie */
2805192895Sjamieint
2806192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2807192895Sjamie{
2808192895Sjamie
2809192895Sjamie	if (pr1 == pr2)
2810192895Sjamie		return (1);
2811192895Sjamie
2812192895Sjamie	/*
2813195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2814195974Sjamie	 * existing prisons.
2815192895Sjamie	 */
2816195945Sjamie	while (pr1 != &prison0 &&
2817195945Sjamie#ifdef VIMAGE
2818195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2819195945Sjamie#endif
2820195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2821192895Sjamie		pr1 = pr1->pr_parent;
2822195945Sjamie	while (pr2 != &prison0 &&
2823195945Sjamie#ifdef VIMAGE
2824195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2825195945Sjamie#endif
2826195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2827192895Sjamie		pr2 = pr2->pr_parent;
2828192895Sjamie	return (pr1 == pr2);
2829192895Sjamie}
2830192895Sjamie
2831192895Sjamie/*
2832185435Sbz * Make sure our (source) address is set to something meaningful to this
2833185435Sbz * jail.
2834185435Sbz *
2835192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2836192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2837192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2838185435Sbz */
2839185435Sbzint
2840185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2841185435Sbz{
2842191673Sjamie	struct prison *pr;
2843185435Sbz	struct in_addr ia0;
2844191673Sjamie	int error;
2845185435Sbz
2846185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2847185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2848185435Sbz
2849192895Sjamie	pr = cred->cr_prison;
2850192895Sjamie	if (!(pr->pr_flags & PR_IP4))
285146155Sphk		return (0);
2852191673Sjamie	mtx_lock(&pr->pr_mtx);
2853192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2854192895Sjamie		mtx_unlock(&pr->pr_mtx);
2855192895Sjamie		return (0);
2856192895Sjamie	}
2857191673Sjamie	if (pr->pr_ip4 == NULL) {
2858191673Sjamie		mtx_unlock(&pr->pr_mtx);
2859188144Sjamie		return (EAFNOSUPPORT);
2860191673Sjamie	}
2861185435Sbz
2862185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
2863185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
2864191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2865191673Sjamie		mtx_unlock(&pr->pr_mtx);
2866185435Sbz		return (0);
286746155Sphk	}
2868185435Sbz
2869188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
2870188144Sjamie		/*
2871188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
2872188144Sjamie		 */
2873191673Sjamie		if (pr->pr_ip4s == 1)
2874191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
2875191673Sjamie		mtx_unlock(&pr->pr_mtx);
2876185435Sbz		return (0);
2877185435Sbz	}
2878185435Sbz
2879191673Sjamie	error = _prison_check_ip4(pr, ia);
2880191673Sjamie	mtx_unlock(&pr->pr_mtx);
2881191673Sjamie	return (error);
2882185435Sbz}
2883185435Sbz
2884185435Sbz/*
2885185435Sbz * Rewrite destination address in case we will connect to loopback address.
2886185435Sbz *
2887188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2888188144Sjamie * Address passed in in NBO and returned in NBO.
2889185435Sbz */
2890185435Sbzint
2891185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2892185435Sbz{
2893191673Sjamie	struct prison *pr;
2894185435Sbz
2895185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2896185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2897185435Sbz
2898192895Sjamie	pr = cred->cr_prison;
2899192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2900185435Sbz		return (0);
2901191673Sjamie	mtx_lock(&pr->pr_mtx);
2902192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2903192895Sjamie		mtx_unlock(&pr->pr_mtx);
2904192895Sjamie		return (0);
2905192895Sjamie	}
2906191673Sjamie	if (pr->pr_ip4 == NULL) {
2907191673Sjamie		mtx_unlock(&pr->pr_mtx);
2908188144Sjamie		return (EAFNOSUPPORT);
2909191673Sjamie	}
2910188144Sjamie
2911185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2912191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2913191673Sjamie		mtx_unlock(&pr->pr_mtx);
2914185435Sbz		return (0);
2915185435Sbz	}
2916185435Sbz
2917185435Sbz	/*
2918185435Sbz	 * Return success because nothing had to be changed.
2919185435Sbz	 */
2920191673Sjamie	mtx_unlock(&pr->pr_mtx);
2921185435Sbz	return (0);
2922185435Sbz}
2923185435Sbz
2924185435Sbz/*
2925188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
2926185435Sbz *
2927192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2928192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2929192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
2930185435Sbz */
2931185435Sbzstatic int
2932185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2933185435Sbz{
2934185435Sbz	int i, a, z, d;
2935185435Sbz
2936185435Sbz	/*
2937185435Sbz	 * Check the primary IP.
2938185435Sbz	 */
2939185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2940188144Sjamie		return (0);
2941185435Sbz
2942185435Sbz	/*
2943185435Sbz	 * All the other IPs are sorted so we can do a binary search.
2944185435Sbz	 */
2945185435Sbz	a = 0;
2946185435Sbz	z = pr->pr_ip4s - 2;
2947185435Sbz	while (a <= z) {
2948185435Sbz		i = (a + z) / 2;
2949185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2950185435Sbz		if (d > 0)
2951185435Sbz			z = i - 1;
2952185435Sbz		else if (d < 0)
2953185435Sbz			a = i + 1;
295481114Srwatson		else
2955188144Sjamie			return (0);
2956185435Sbz	}
2957188144Sjamie
2958188144Sjamie	return (EADDRNOTAVAIL);
2959185435Sbz}
2960185435Sbz
2961185435Sbzint
2962185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
2963185435Sbz{
2964191673Sjamie	struct prison *pr;
2965191673Sjamie	int error;
2966185435Sbz
2967185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2968185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2969185435Sbz
2970192895Sjamie	pr = cred->cr_prison;
2971192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2972188144Sjamie		return (0);
2973191673Sjamie	mtx_lock(&pr->pr_mtx);
2974192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2975192895Sjamie		mtx_unlock(&pr->pr_mtx);
2976192895Sjamie		return (0);
2977192895Sjamie	}
2978191673Sjamie	if (pr->pr_ip4 == NULL) {
2979191673Sjamie		mtx_unlock(&pr->pr_mtx);
2980188144Sjamie		return (EAFNOSUPPORT);
2981191673Sjamie	}
2982185435Sbz
2983191673Sjamie	error = _prison_check_ip4(pr, ia);
2984191673Sjamie	mtx_unlock(&pr->pr_mtx);
2985191673Sjamie	return (error);
2986185435Sbz}
2987185435Sbz#endif
2988185435Sbz
2989185435Sbz#ifdef INET6
2990192895Sjamiestatic int
2991192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2992192895Sjamie{
2993192895Sjamie	int ii, ij, used;
2994192895Sjamie	struct prison *ppr;
2995192895Sjamie
2996192895Sjamie	ppr = pr->pr_parent;
2997192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
2998192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2999192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
3000192895Sjamie			/*
3001192895Sjamie			 * There's no room for the parent's list.  Use the
3002192895Sjamie			 * new list buffer, which is assumed to be big enough
3003192895Sjamie			 * (if it was passed).  If there's no buffer, try to
3004192895Sjamie			 * allocate one.
3005192895Sjamie			 */
3006192895Sjamie			used = 1;
3007192895Sjamie			if (newip6 == NULL) {
3008192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
3009192895Sjamie				    M_PRISON, M_NOWAIT);
3010192895Sjamie				if (newip6 != NULL)
3011192895Sjamie					used = 0;
3012192895Sjamie			}
3013192895Sjamie			if (newip6 != NULL) {
3014192895Sjamie				bcopy(ppr->pr_ip6, newip6,
3015192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
3016192895Sjamie				free(pr->pr_ip6, M_PRISON);
3017192895Sjamie				pr->pr_ip6 = newip6;
3018192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
3019192895Sjamie			}
3020192895Sjamie			return (used);
3021192895Sjamie		}
3022192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
3023192895Sjamie		if (pr->pr_ip6s > 0)
3024192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
3025192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
3026192895Sjamie		else if (pr->pr_ip6 != NULL) {
3027192895Sjamie			free(pr->pr_ip6, M_PRISON);
3028192895Sjamie			pr->pr_ip6 = NULL;
3029192895Sjamie		}
3030195974Sjamie	} else if (pr->pr_ip6s > 0) {
3031192895Sjamie		/* Remove addresses that aren't in the parent. */
3032192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
3033192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
3034192895Sjamie			    &ppr->pr_ip6[ij]))
3035192895Sjamie				break;
3036192895Sjamie		if (ij < ppr->pr_ip6s)
3037192895Sjamie			ii = 1;
3038192895Sjamie		else {
3039192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
3040192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
3041192895Sjamie			ii = 0;
3042192895Sjamie		}
3043192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
3044192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
3045192895Sjamie			    &ppr->pr_ip6[0])) {
3046192895Sjamie				ii++;
3047192895Sjamie				continue;
3048192895Sjamie			}
3049192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
3050192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3051192895Sjamie			case -1:
3052192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3053192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3054192895Sjamie				break;
3055192895Sjamie			case 0:
3056192895Sjamie				ii++;
3057192895Sjamie				ij++;
3058192895Sjamie				break;
3059192895Sjamie			case 1:
3060192895Sjamie				ij++;
3061192895Sjamie				break;
3062192895Sjamie			}
3063192895Sjamie		}
3064192895Sjamie		if (pr->pr_ip6s == 0) {
3065195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
3066192895Sjamie			free(pr->pr_ip6, M_PRISON);
3067192895Sjamie			pr->pr_ip6 = NULL;
3068192895Sjamie		}
3069192895Sjamie	}
3070192895Sjamie	return 0;
3071192895Sjamie}
3072192895Sjamie
3073185435Sbz/*
3074185435Sbz * Pass back primary IPv6 address for this jail.
3075185435Sbz *
3076192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3077192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3078185435Sbz *
3079188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3080185435Sbz */
3081185435Sbzint
3082187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3083185435Sbz{
3084191673Sjamie	struct prison *pr;
3085185435Sbz
3086185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3087185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3088185435Sbz
3089192895Sjamie	pr = cred->cr_prison;
3090192895Sjamie	if (!(pr->pr_flags & PR_IP6))
309181114Srwatson		return (0);
3092191673Sjamie	mtx_lock(&pr->pr_mtx);
3093192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3094192895Sjamie		mtx_unlock(&pr->pr_mtx);
3095192895Sjamie		return (0);
3096192895Sjamie	}
3097191673Sjamie	if (pr->pr_ip6 == NULL) {
3098191673Sjamie		mtx_unlock(&pr->pr_mtx);
3099188144Sjamie		return (EAFNOSUPPORT);
3100191673Sjamie	}
3101188144Sjamie
3102191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3103191673Sjamie	mtx_unlock(&pr->pr_mtx);
3104185435Sbz	return (0);
3105185435Sbz}
3106185435Sbz
3107185435Sbz/*
3108202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3109202468Sbz * We will return 0 if we should bypass source address selection in favour
3110202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3111202468Sbz * returned in NBO.
3112202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3113202468Sbz */
3114202468Sbzint
3115202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3116202468Sbz{
3117202468Sbz	struct prison *pr;
3118202468Sbz	struct in6_addr lia6;
3119202468Sbz	int error;
3120202468Sbz
3121202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3122202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3123202468Sbz
3124202468Sbz	if (!jailed(cred))
3125202468Sbz		return (1);
3126202468Sbz
3127202468Sbz	pr = cred->cr_prison;
3128202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3129202468Sbz		return (1);
3130202468Sbz
3131202468Sbz	lia6 = in6addr_any;
3132202468Sbz	error = prison_get_ip6(cred, &lia6);
3133202468Sbz	if (error)
3134202468Sbz		return (error);
3135202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3136202468Sbz		return (1);
3137202468Sbz
3138202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3139202468Sbz	return (0);
3140202468Sbz}
3141202468Sbz
3142202468Sbz/*
3143192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3144192895Sjamie */
3145192895Sjamieint
3146192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3147192895Sjamie{
3148192895Sjamie
3149192895Sjamie	if (pr1 == pr2)
3150192895Sjamie		return (1);
3151192895Sjamie
3152195945Sjamie	while (pr1 != &prison0 &&
3153195945Sjamie#ifdef VIMAGE
3154195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3155195945Sjamie#endif
3156195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3157192895Sjamie		pr1 = pr1->pr_parent;
3158195945Sjamie	while (pr2 != &prison0 &&
3159195945Sjamie#ifdef VIMAGE
3160195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3161195945Sjamie#endif
3162195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3163192895Sjamie		pr2 = pr2->pr_parent;
3164192895Sjamie	return (pr1 == pr2);
3165192895Sjamie}
3166192895Sjamie
3167192895Sjamie/*
3168185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3169185435Sbz *
3170185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3171185435Sbz * when needed while binding.
3172185435Sbz *
3173192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3174192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3175192895Sjamie * doesn't allow IPv6.
3176185435Sbz */
3177185435Sbzint
3178185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3179185435Sbz{
3180191673Sjamie	struct prison *pr;
3181191673Sjamie	int error;
3182185435Sbz
3183185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3184185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3185185435Sbz
3186192895Sjamie	pr = cred->cr_prison;
3187192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3188185435Sbz		return (0);
3189191673Sjamie	mtx_lock(&pr->pr_mtx);
3190192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3191192895Sjamie		mtx_unlock(&pr->pr_mtx);
3192192895Sjamie		return (0);
3193192895Sjamie	}
3194191673Sjamie	if (pr->pr_ip6 == NULL) {
3195191673Sjamie		mtx_unlock(&pr->pr_mtx);
3196188144Sjamie		return (EAFNOSUPPORT);
3197191673Sjamie	}
3198188144Sjamie
3199185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3200191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3201191673Sjamie		mtx_unlock(&pr->pr_mtx);
3202185435Sbz		return (0);
320381114Srwatson	}
3204185435Sbz
3205188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3206188144Sjamie		/*
3207188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3208188144Sjamie		 * then bind directly.
3209188144Sjamie		 */
3210191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3211191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3212191673Sjamie		mtx_unlock(&pr->pr_mtx);
3213185435Sbz		return (0);
3214185435Sbz	}
3215188144Sjamie
3216191673Sjamie	error = _prison_check_ip6(pr, ia6);
3217191673Sjamie	mtx_unlock(&pr->pr_mtx);
3218191673Sjamie	return (error);
3219185435Sbz}
3220185435Sbz
3221185435Sbz/*
3222185435Sbz * Rewrite destination address in case we will connect to loopback address.
3223185435Sbz *
3224188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3225185435Sbz */
3226185435Sbzint
3227185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3228185435Sbz{
3229191673Sjamie	struct prison *pr;
3230185435Sbz
3231185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3232185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3233185435Sbz
3234192895Sjamie	pr = cred->cr_prison;
3235192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3236185435Sbz		return (0);
3237191673Sjamie	mtx_lock(&pr->pr_mtx);
3238192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3239192895Sjamie		mtx_unlock(&pr->pr_mtx);
3240192895Sjamie		return (0);
3241192895Sjamie	}
3242191673Sjamie	if (pr->pr_ip6 == NULL) {
3243191673Sjamie		mtx_unlock(&pr->pr_mtx);
3244188144Sjamie		return (EAFNOSUPPORT);
3245191673Sjamie	}
3246188144Sjamie
3247185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3248191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3249191673Sjamie		mtx_unlock(&pr->pr_mtx);
3250185435Sbz		return (0);
3251185435Sbz	}
3252185435Sbz
3253185435Sbz	/*
3254185435Sbz	 * Return success because nothing had to be changed.
3255185435Sbz	 */
3256191673Sjamie	mtx_unlock(&pr->pr_mtx);
325746155Sphk	return (0);
325846155Sphk}
325946155Sphk
3260185435Sbz/*
3261188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3262185435Sbz *
3263192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3264192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3265192895Sjamie * doesn't allow IPv6.
3266185435Sbz */
3267185435Sbzstatic int
3268185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
326946155Sphk{
3270185435Sbz	int i, a, z, d;
327146155Sphk
3272185435Sbz	/*
3273185435Sbz	 * Check the primary IP.
3274185435Sbz	 */
3275185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3276188144Sjamie		return (0);
3277185435Sbz
3278185435Sbz	/*
3279185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3280185435Sbz	 */
3281185435Sbz	a = 0;
3282185435Sbz	z = pr->pr_ip6s - 2;
3283185435Sbz	while (a <= z) {
3284185435Sbz		i = (a + z) / 2;
3285185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3286185435Sbz		if (d > 0)
3287185435Sbz			z = i - 1;
3288185435Sbz		else if (d < 0)
3289185435Sbz			a = i + 1;
329046155Sphk		else
3291188144Sjamie			return (0);
329246155Sphk	}
3293188144Sjamie
3294188144Sjamie	return (EADDRNOTAVAIL);
329546155Sphk}
329646155Sphk
329746155Sphkint
3298185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3299185435Sbz{
3300191673Sjamie	struct prison *pr;
3301191673Sjamie	int error;
3302185435Sbz
3303185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3304185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3305185435Sbz
3306192895Sjamie	pr = cred->cr_prison;
3307192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3308188144Sjamie		return (0);
3309191673Sjamie	mtx_lock(&pr->pr_mtx);
3310192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3311192895Sjamie		mtx_unlock(&pr->pr_mtx);
3312192895Sjamie		return (0);
3313192895Sjamie	}
3314191673Sjamie	if (pr->pr_ip6 == NULL) {
3315191673Sjamie		mtx_unlock(&pr->pr_mtx);
3316188144Sjamie		return (EAFNOSUPPORT);
3317191673Sjamie	}
3318185435Sbz
3319191673Sjamie	error = _prison_check_ip6(pr, ia6);
3320191673Sjamie	mtx_unlock(&pr->pr_mtx);
3321191673Sjamie	return (error);
3322185435Sbz}
3323185435Sbz#endif
3324185435Sbz
3325185435Sbz/*
3326188146Sjamie * Check if a jail supports the given address family.
3327188146Sjamie *
3328188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3329188146Sjamie * if not.
3330188146Sjamie */
3331188146Sjamieint
3332188146Sjamieprison_check_af(struct ucred *cred, int af)
3333188146Sjamie{
3334192895Sjamie	struct prison *pr;
3335188146Sjamie	int error;
3336188146Sjamie
3337188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3338188146Sjamie
3339192895Sjamie	pr = cred->cr_prison;
3340194923Sjamie#ifdef VIMAGE
3341194915Sjamie	/* Prisons with their own network stack are not limited. */
3342200473Sbz	if (prison_owns_vnet(cred))
3343194915Sjamie		return (0);
3344194923Sjamie#endif
3345194915Sjamie
3346188146Sjamie	error = 0;
3347188146Sjamie	switch (af)
3348188146Sjamie	{
3349188146Sjamie#ifdef INET
3350188146Sjamie	case AF_INET:
3351192895Sjamie		if (pr->pr_flags & PR_IP4)
3352192895Sjamie		{
3353192895Sjamie			mtx_lock(&pr->pr_mtx);
3354192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3355192895Sjamie				error = EAFNOSUPPORT;
3356192895Sjamie			mtx_unlock(&pr->pr_mtx);
3357192895Sjamie		}
3358188146Sjamie		break;
3359188146Sjamie#endif
3360188146Sjamie#ifdef INET6
3361188146Sjamie	case AF_INET6:
3362192895Sjamie		if (pr->pr_flags & PR_IP6)
3363192895Sjamie		{
3364192895Sjamie			mtx_lock(&pr->pr_mtx);
3365192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3366192895Sjamie				error = EAFNOSUPPORT;
3367192895Sjamie			mtx_unlock(&pr->pr_mtx);
3368192895Sjamie		}
3369188146Sjamie		break;
3370188146Sjamie#endif
3371188146Sjamie	case AF_LOCAL:
3372188146Sjamie	case AF_ROUTE:
3373188146Sjamie		break;
3374188146Sjamie	default:
3375192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3376188146Sjamie			error = EAFNOSUPPORT;
3377188146Sjamie	}
3378188146Sjamie	return (error);
3379188146Sjamie}
3380188146Sjamie
3381188146Sjamie/*
3382185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3383185435Sbz * prison_check_ip[46]).
3384185435Sbz *
3385192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3386192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3387192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3388185435Sbz */
3389185435Sbzint
339072786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
339146155Sphk{
3392185435Sbz#ifdef INET
3393114168Smike	struct sockaddr_in *sai;
3394185435Sbz#endif
3395185435Sbz#ifdef INET6
3396185435Sbz	struct sockaddr_in6 *sai6;
3397185435Sbz#endif
3398188144Sjamie	int error;
339946155Sphk
3400185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3401185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3402185435Sbz
3403200473Sbz#ifdef VIMAGE
3404200473Sbz	if (prison_owns_vnet(cred))
3405200473Sbz		return (0);
3406200473Sbz#endif
3407200473Sbz
3408188144Sjamie	error = 0;
3409188144Sjamie	switch (sa->sa_family)
3410185435Sbz	{
3411185435Sbz#ifdef INET
3412185435Sbz	case AF_INET:
3413185435Sbz		sai = (struct sockaddr_in *)sa;
3414188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3415185435Sbz		break;
3416185435Sbz#endif
3417185435Sbz#ifdef INET6
3418185435Sbz	case AF_INET6:
3419185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3420188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3421185435Sbz		break;
3422185435Sbz#endif
3423185435Sbz	default:
3424192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3425188144Sjamie			error = EAFNOSUPPORT;
3426185435Sbz	}
3427188144Sjamie	return (error);
342846155Sphk}
342972786Srwatson
343072786Srwatson/*
343172786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
343272786Srwatson */
343372786Srwatsonint
3434114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
343572786Srwatson{
343672786Srwatson
3437192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3438192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3439192895Sjamie}
344072786Srwatson
3441192895Sjamie/*
3442192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3443192895Sjamie */
3444192895Sjamieint
3445192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3446192895Sjamie{
3447192895Sjamie
3448192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3449192895Sjamie		if (pr1 == pr2)
3450192895Sjamie			return (1);
345172786Srwatson	return (0);
345272786Srwatson}
345372786Srwatson
345472786Srwatson/*
345572786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
345672786Srwatson */
345772786Srwatsonint
3458114168Smikejailed(struct ucred *cred)
345972786Srwatson{
346072786Srwatson
3461192895Sjamie	return (cred->cr_prison != &prison0);
346272786Srwatson}
346391384Srobert
346491384Srobert/*
3465200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3466200473Sbz * have its own virtual network stack, otherwise 0.
3467200473Sbz */
3468200473Sbzint
3469200473Sbzjailed_without_vnet(struct ucred *cred)
3470200473Sbz{
3471200473Sbz
3472200473Sbz	if (!jailed(cred))
3473200473Sbz		return (0);
3474200473Sbz#ifdef VIMAGE
3475200473Sbz	if (prison_owns_vnet(cred))
3476200473Sbz		return (0);
3477200473Sbz#endif
3478200473Sbz
3479200473Sbz	return (1);
3480200473Sbz}
3481200473Sbz
3482200473Sbz/*
3483194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
348491384Srobert */
348591391Srobertvoid
3486114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
348791384Srobert{
3488193066Sjamie	struct prison *pr;
348991384Srobert
3490194090Sjamie	/*
3491194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3492194090Sjamie	 * system's hostname.
3493194090Sjamie	 */
3494193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3495193066Sjamie	mtx_lock(&pr->pr_mtx);
3496194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3497193066Sjamie	mtx_unlock(&pr->pr_mtx);
349891384Srobert}
3499113275Smike
3500194090Sjamievoid
3501194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3502194090Sjamie{
3503194090Sjamie
3504194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3505194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3506194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3507194090Sjamie}
3508194090Sjamie
3509194090Sjamievoid
3510194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3511194090Sjamie{
3512194090Sjamie
3513194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3514194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3515194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3516194090Sjamie}
3517194090Sjamie
3518194090Sjamievoid
3519194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3520194090Sjamie{
3521194090Sjamie
3522194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3523194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3524194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3525194090Sjamie}
3526194090Sjamie
3527196176Sbz#ifdef VIMAGE
3528125804Srwatson/*
3529196176Sbz * Determine whether the prison represented by cred owns
3530196176Sbz * its vnet rather than having it inherited.
3531196176Sbz *
3532196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3533196176Sbz */
3534196176Sbzint
3535196176Sbzprison_owns_vnet(struct ucred *cred)
3536196176Sbz{
3537196176Sbz
3538196176Sbz	/*
3539196176Sbz	 * vnets cannot be added/removed after jail creation,
3540196176Sbz	 * so no need to lock here.
3541196176Sbz	 */
3542196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3543196176Sbz}
3544196176Sbz#endif
3545196176Sbz
3546196176Sbz/*
3547147185Spjd * Determine whether the subject represented by cred can "see"
3548147185Spjd * status of a mount point.
3549147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3550147185Spjd * XXX: This function should be called cr_canseemount() and should be
3551147185Spjd *      placed in kern_prot.c.
3552125804Srwatson */
3553125804Srwatsonint
3554147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3555125804Srwatson{
3556147185Spjd	struct prison *pr;
3557147185Spjd	struct statfs *sp;
3558147185Spjd	size_t len;
3559125804Srwatson
3560192895Sjamie	pr = cred->cr_prison;
3561192895Sjamie	if (pr->pr_enforce_statfs == 0)
3562147185Spjd		return (0);
3563147185Spjd	if (pr->pr_root->v_mount == mp)
3564147185Spjd		return (0);
3565192895Sjamie	if (pr->pr_enforce_statfs == 2)
3566147185Spjd		return (ENOENT);
3567147185Spjd	/*
3568147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3569147185Spjd	 * all mount-points from inside a jail.
3570147185Spjd	 * This is ugly check, but this is the only situation when jail's
3571147185Spjd	 * directory ends with '/'.
3572147185Spjd	 */
3573147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3574147185Spjd		return (0);
3575147185Spjd	len = strlen(pr->pr_path);
3576147185Spjd	sp = &mp->mnt_stat;
3577147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3578147185Spjd		return (ENOENT);
3579147185Spjd	/*
3580147185Spjd	 * Be sure that we don't have situation where jail's root directory
3581147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3582147185Spjd	 */
3583147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3584147185Spjd		return (ENOENT);
3585147185Spjd	return (0);
3586147185Spjd}
3587147185Spjd
3588147185Spjdvoid
3589147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3590147185Spjd{
3591147185Spjd	char jpath[MAXPATHLEN];
3592147185Spjd	struct prison *pr;
3593147185Spjd	size_t len;
3594147185Spjd
3595192895Sjamie	pr = cred->cr_prison;
3596192895Sjamie	if (pr->pr_enforce_statfs == 0)
3597147185Spjd		return;
3598147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3599147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3600147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3601147185Spjd		    sizeof(sp->f_mntonname));
3602147185Spjd		return;
3603125804Srwatson	}
3604147185Spjd	if (pr->pr_root->v_mount == mp) {
3605147185Spjd		/*
3606147185Spjd		 * Clear current buffer data, so we are sure nothing from
3607147185Spjd		 * the valid path left there.
3608147185Spjd		 */
3609147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3610147185Spjd		*sp->f_mntonname = '/';
3611147185Spjd		return;
3612147185Spjd	}
3613147185Spjd	/*
3614147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3615147185Spjd	 * all mount-points from inside a jail.
3616147185Spjd	 */
3617147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3618147185Spjd		return;
3619147185Spjd	len = strlen(pr->pr_path);
3620147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3621147185Spjd	/*
3622147185Spjd	 * Clear current buffer data, so we are sure nothing from
3623147185Spjd	 * the valid path left there.
3624147185Spjd	 */
3625147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3626147185Spjd	if (*jpath == '\0') {
3627147185Spjd		/* Should never happen. */
3628147185Spjd		*sp->f_mntonname = '/';
3629147185Spjd	} else {
3630147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3631147185Spjd	}
3632125804Srwatson}
3633125804Srwatson
3634164032Srwatson/*
3635164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3636164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3637164032Srwatson */
3638164032Srwatsonint
3639164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3640164032Srwatson{
3641164032Srwatson
3642164032Srwatson	if (!jailed(cred))
3643164032Srwatson		return (0);
3644164032Srwatson
3645194915Sjamie#ifdef VIMAGE
3646194915Sjamie	/*
3647194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3648194915Sjamie	 * There might be a duplicate entry here in case the privilege
3649194915Sjamie	 * is only granted conditionally in the legacy jail case.
3650194915Sjamie	 */
3651164032Srwatson	switch (priv) {
3652194915Sjamie#ifdef notyet
3653194915Sjamie		/*
3654194915Sjamie		 * NFS-specific privileges.
3655194915Sjamie		 */
3656194915Sjamie	case PRIV_NFS_DAEMON:
3657194915Sjamie	case PRIV_NFS_LOCKD:
3658194915Sjamie#endif
3659194915Sjamie		/*
3660194915Sjamie		 * Network stack privileges.
3661194915Sjamie		 */
3662194915Sjamie	case PRIV_NET_BRIDGE:
3663194915Sjamie	case PRIV_NET_GRE:
3664194915Sjamie	case PRIV_NET_BPF:
3665194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3666194915Sjamie	case PRIV_NET_ROUTE:
3667194915Sjamie	case PRIV_NET_TAP:
3668194915Sjamie	case PRIV_NET_SETIFMTU:
3669194915Sjamie	case PRIV_NET_SETIFFLAGS:
3670194915Sjamie	case PRIV_NET_SETIFCAP:
3671203052Sdelphij	case PRIV_NET_SETIFDESCR:
3672194915Sjamie	case PRIV_NET_SETIFNAME	:
3673194915Sjamie	case PRIV_NET_SETIFMETRIC:
3674194915Sjamie	case PRIV_NET_SETIFPHYS:
3675194915Sjamie	case PRIV_NET_SETIFMAC:
3676194915Sjamie	case PRIV_NET_ADDMULTI:
3677194915Sjamie	case PRIV_NET_DELMULTI:
3678194915Sjamie	case PRIV_NET_HWIOCTL:
3679194915Sjamie	case PRIV_NET_SETLLADDR:
3680194915Sjamie	case PRIV_NET_ADDIFGROUP:
3681194915Sjamie	case PRIV_NET_DELIFGROUP:
3682194915Sjamie	case PRIV_NET_IFCREATE:
3683194915Sjamie	case PRIV_NET_IFDESTROY:
3684194915Sjamie	case PRIV_NET_ADDIFADDR:
3685194915Sjamie	case PRIV_NET_DELIFADDR:
3686194915Sjamie	case PRIV_NET_LAGG:
3687194915Sjamie	case PRIV_NET_GIF:
3688194915Sjamie	case PRIV_NET_SETIFVNET:
3689223735Sbz	case PRIV_NET_SETIFFIB:
3690164032Srwatson
3691164032Srwatson		/*
3692194915Sjamie		 * 802.11-related privileges.
3693194915Sjamie		 */
3694194915Sjamie	case PRIV_NET80211_GETKEY:
3695194915Sjamie#ifdef notyet
3696194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3697194915Sjamie#endif
3698194915Sjamie
3699194915Sjamie#ifdef notyet
3700194915Sjamie		/*
3701194915Sjamie		 * AppleTalk privileges.
3702194915Sjamie		 */
3703194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3704194915Sjamie
3705194915Sjamie		/*
3706194915Sjamie		 * ATM privileges.
3707194915Sjamie		 */
3708194915Sjamie	case PRIV_NETATM_CFG:
3709194915Sjamie	case PRIV_NETATM_ADD:
3710194915Sjamie	case PRIV_NETATM_DEL:
3711194915Sjamie	case PRIV_NETATM_SET:
3712194915Sjamie
3713194915Sjamie		/*
3714194915Sjamie		 * Bluetooth privileges.
3715194915Sjamie		 */
3716194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3717194915Sjamie#endif
3718194915Sjamie
3719194915Sjamie		/*
3720194915Sjamie		 * Netgraph and netgraph module privileges.
3721194915Sjamie		 */
3722194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3723194915Sjamie#ifdef notyet
3724194915Sjamie	case PRIV_NETGRAPH_TTY:
3725194915Sjamie#endif
3726194915Sjamie
3727194915Sjamie		/*
3728194915Sjamie		 * IPv4 and IPv6 privileges.
3729194915Sjamie		 */
3730194915Sjamie	case PRIV_NETINET_IPFW:
3731194915Sjamie	case PRIV_NETINET_DIVERT:
3732194915Sjamie	case PRIV_NETINET_PF:
3733194915Sjamie	case PRIV_NETINET_DUMMYNET:
3734194915Sjamie	case PRIV_NETINET_CARP:
3735194915Sjamie	case PRIV_NETINET_MROUTE:
3736194915Sjamie	case PRIV_NETINET_RAW:
3737194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3738194915Sjamie	case PRIV_NETINET_ND6:
3739194915Sjamie	case PRIV_NETINET_SCOPE6:
3740194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3741194915Sjamie	case PRIV_NETINET_IPSEC:
3742194915Sjamie	case PRIV_NETINET_BINDANY:
3743194915Sjamie
3744194915Sjamie#ifdef notyet
3745194915Sjamie		/*
3746194915Sjamie		 * IPX/SPX privileges.
3747194915Sjamie		 */
3748194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3749194915Sjamie	case PRIV_NETIPX_RAW:
3750194915Sjamie
3751194915Sjamie		/*
3752194915Sjamie		 * NCP privileges.
3753194915Sjamie		 */
3754194915Sjamie	case PRIV_NETNCP:
3755194915Sjamie
3756194915Sjamie		/*
3757194915Sjamie		 * SMB privileges.
3758194915Sjamie		 */
3759194915Sjamie	case PRIV_NETSMB:
3760194915Sjamie#endif
3761194915Sjamie
3762194915Sjamie	/*
3763194915Sjamie	 * No default: or deny here.
3764194915Sjamie	 * In case of no permit fall through to next switch().
3765194915Sjamie	 */
3766194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3767194915Sjamie			return (0);
3768194915Sjamie	}
3769194915Sjamie#endif /* VIMAGE */
3770194915Sjamie
3771194915Sjamie	switch (priv) {
3772194915Sjamie
3773194915Sjamie		/*
3774164032Srwatson		 * Allow ktrace privileges for root in jail.
3775164032Srwatson		 */
3776164032Srwatson	case PRIV_KTRACE:
3777164032Srwatson
3778166827Srwatson#if 0
3779164032Srwatson		/*
3780164032Srwatson		 * Allow jailed processes to configure audit identity and
3781164032Srwatson		 * submit audit records (login, etc).  In the future we may
3782164032Srwatson		 * want to further refine the relationship between audit and
3783164032Srwatson		 * jail.
3784164032Srwatson		 */
3785164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3786164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3787164032Srwatson	case PRIV_AUDIT_SUBMIT:
3788166827Srwatson#endif
3789164032Srwatson
3790164032Srwatson		/*
3791164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3792164032Srwatson		 * credentials in any way they see fit.
3793164032Srwatson		 */
3794164032Srwatson	case PRIV_CRED_SETUID:
3795164032Srwatson	case PRIV_CRED_SETEUID:
3796164032Srwatson	case PRIV_CRED_SETGID:
3797164032Srwatson	case PRIV_CRED_SETEGID:
3798164032Srwatson	case PRIV_CRED_SETGROUPS:
3799164032Srwatson	case PRIV_CRED_SETREUID:
3800164032Srwatson	case PRIV_CRED_SETREGID:
3801164032Srwatson	case PRIV_CRED_SETRESUID:
3802164032Srwatson	case PRIV_CRED_SETRESGID:
3803164032Srwatson
3804164032Srwatson		/*
3805164032Srwatson		 * Jail implements visibility constraints already, so allow
3806164032Srwatson		 * jailed root to override uid/gid-based constraints.
3807164032Srwatson		 */
3808164032Srwatson	case PRIV_SEEOTHERGIDS:
3809164032Srwatson	case PRIV_SEEOTHERUIDS:
3810164032Srwatson
3811164032Srwatson		/*
3812164032Srwatson		 * Jail implements inter-process debugging limits already, so
3813164032Srwatson		 * allow jailed root various debugging privileges.
3814164032Srwatson		 */
3815164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3816164032Srwatson	case PRIV_DEBUG_SUGID:
3817164032Srwatson	case PRIV_DEBUG_UNPRIV:
3818164032Srwatson
3819164032Srwatson		/*
3820164032Srwatson		 * Allow jail to set various resource limits and login
3821164032Srwatson		 * properties, and for now, exceed process resource limits.
3822164032Srwatson		 */
3823164032Srwatson	case PRIV_PROC_LIMIT:
3824164032Srwatson	case PRIV_PROC_SETLOGIN:
3825164032Srwatson	case PRIV_PROC_SETRLIMIT:
3826164032Srwatson
3827164032Srwatson		/*
3828164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3829164032Srwatson		 */
3830164032Srwatson	case PRIV_IPC_READ:
3831164032Srwatson	case PRIV_IPC_WRITE:
3832164032Srwatson	case PRIV_IPC_ADMIN:
3833164032Srwatson	case PRIV_IPC_MSGSIZE:
3834164032Srwatson	case PRIV_MQ_ADMIN:
3835164032Srwatson
3836164032Srwatson		/*
3837192895Sjamie		 * Jail operations within a jail work on child jails.
3838192895Sjamie		 */
3839192895Sjamie	case PRIV_JAIL_ATTACH:
3840192895Sjamie	case PRIV_JAIL_SET:
3841192895Sjamie	case PRIV_JAIL_REMOVE:
3842192895Sjamie
3843192895Sjamie		/*
3844164032Srwatson		 * Jail implements its own inter-process limits, so allow
3845164032Srwatson		 * root processes in jail to change scheduling on other
3846164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3847164032Srwatson		 */
3848164032Srwatson	case PRIV_SCHED_DIFFCRED:
3849185435Sbz	case PRIV_SCHED_CPUSET:
3850164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3851164032Srwatson	case PRIV_SIGNAL_SUGID:
3852164032Srwatson
3853164032Srwatson		/*
3854164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3855164032Srwatson		 * writable.
3856164032Srwatson		 */
3857164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
3858164032Srwatson
3859164032Srwatson		/*
3860164032Srwatson		 * Allow root in jail to manage a variety of quota
3861166831Srwatson		 * properties.  These should likely be conditional on a
3862166831Srwatson		 * configuration option.
3863164032Srwatson		 */
3864166832Srwatson	case PRIV_VFS_GETQUOTA:
3865166832Srwatson	case PRIV_VFS_SETQUOTA:
3866164032Srwatson
3867164032Srwatson		/*
3868164032Srwatson		 * Since Jail relies on chroot() to implement file system
3869164032Srwatson		 * protections, grant many VFS privileges to root in jail.
3870164032Srwatson		 * Be careful to exclude mount-related and NFS-related
3871164032Srwatson		 * privileges.
3872164032Srwatson		 */
3873164032Srwatson	case PRIV_VFS_READ:
3874164032Srwatson	case PRIV_VFS_WRITE:
3875164032Srwatson	case PRIV_VFS_ADMIN:
3876164032Srwatson	case PRIV_VFS_EXEC:
3877164032Srwatson	case PRIV_VFS_LOOKUP:
3878164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3879164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
3880164032Srwatson	case PRIV_VFS_CHOWN:
3881164032Srwatson	case PRIV_VFS_CHROOT:
3882167152Spjd	case PRIV_VFS_RETAINSUGID:
3883164032Srwatson	case PRIV_VFS_FCHROOT:
3884164032Srwatson	case PRIV_VFS_LINK:
3885164032Srwatson	case PRIV_VFS_SETGID:
3886172860Srwatson	case PRIV_VFS_STAT:
3887164032Srwatson	case PRIV_VFS_STICKYFILE:
3888255316Sjamie
3889255316Sjamie		/*
3890255316Sjamie		 * As in the non-jail case, non-root users are expected to be
3891255316Sjamie		 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3892255316Sjamie		 * exists in the jail and they have permission to access it).
3893255316Sjamie		 */
3894255316Sjamie	case PRIV_KMEM_READ:
3895164032Srwatson		return (0);
3896164032Srwatson
3897164032Srwatson		/*
3898164032Srwatson		 * Depending on the global setting, allow privilege of
3899164032Srwatson		 * setting system flags.
3900164032Srwatson		 */
3901164032Srwatson	case PRIV_VFS_SYSFLAGS:
3902192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3903164032Srwatson			return (0);
3904164032Srwatson		else
3905164032Srwatson			return (EPERM);
3906164032Srwatson
3907164032Srwatson		/*
3908168396Spjd		 * Depending on the global setting, allow privilege of
3909168396Spjd		 * mounting/unmounting file systems.
3910168396Spjd		 */
3911168396Spjd	case PRIV_VFS_MOUNT:
3912168396Spjd	case PRIV_VFS_UNMOUNT:
3913168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
3914168699Spjd	case PRIV_VFS_MOUNT_OWNER:
3915224615Smm		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3916224615Smm		    cred->cr_prison->pr_enforce_statfs < 2)
3917168396Spjd			return (0);
3918168396Spjd		else
3919168396Spjd			return (EPERM);
3920168396Spjd
3921168396Spjd		/*
3922168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
3923168591Srwatson		 * ports.
3924164032Srwatson		 */
3925164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
3926168591Srwatson	case PRIV_NETINET_REUSEPORT:
3927164032Srwatson		return (0);
3928164032Srwatson
3929164032Srwatson		/*
3930175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
3931175630Sbz		 */
3932175630Sbz	case PRIV_NETINET_SETHDROPTS:
3933175630Sbz		return (0);
3934175630Sbz
3935175630Sbz		/*
3936164032Srwatson		 * Conditionally allow creating raw sockets in jail.
3937164032Srwatson		 */
3938164032Srwatson	case PRIV_NETINET_RAW:
3939192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3940164032Srwatson			return (0);
3941164032Srwatson		else
3942164032Srwatson			return (EPERM);
3943164032Srwatson
3944164032Srwatson		/*
3945164032Srwatson		 * Since jail implements its own visibility limits on netstat
3946164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
3947164032Srwatson		 * jail.
3948164032Srwatson		 */
3949164032Srwatson	case PRIV_NETINET_GETCRED:
3950164032Srwatson		return (0);
3951164032Srwatson
3952219304Strasz		/*
3953219304Strasz		 * Allow jailed root to set loginclass.
3954219304Strasz		 */
3955219304Strasz	case PRIV_PROC_SETLOGINCLASS:
3956219304Strasz		return (0);
3957219304Strasz
3958164032Srwatson	default:
3959164032Srwatson		/*
3960164032Srwatson		 * In all remaining cases, deny the privilege request.  This
3961164032Srwatson		 * includes almost all network privileges, many system
3962164032Srwatson		 * configuration privileges.
3963164032Srwatson		 */
3964164032Srwatson		return (EPERM);
3965164032Srwatson	}
3966164032Srwatson}
3967164032Srwatson
3968192895Sjamie/*
3969192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
3970192895Sjamie * if it does not directly follow.
3971192895Sjamie */
3972192895Sjamie
3973192895Sjamiechar *
3974192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
3975192895Sjamie{
3976192895Sjamie	char *name;
3977192895Sjamie
3978192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
3979192895Sjamie	if (pr1 == pr2)
3980192895Sjamie		return "0";
3981192895Sjamie	name = pr2->pr_name;
3982192895Sjamie	if (prison_ischild(pr1, pr2)) {
3983192895Sjamie		/*
3984192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
3985192895Sjamie		 * so its length can't be counted on.  But the number of dots
3986192895Sjamie		 * can be counted on - and counted.
3987192895Sjamie		 */
3988192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3989192895Sjamie			name = strchr(name, '.') + 1;
3990192895Sjamie	}
3991192895Sjamie	return (name);
3992192895Sjamie}
3993192895Sjamie
3994192895Sjamie/*
3995192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
3996192895Sjamie * if it does not directly follow.
3997192895Sjamie */
3998192895Sjamiestatic char *
3999192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
4000192895Sjamie{
4001192895Sjamie	char *path1, *path2;
4002192895Sjamie	int len1;
4003192895Sjamie
4004192895Sjamie	path1 = pr1->pr_path;
4005192895Sjamie	path2 = pr2->pr_path;
4006192895Sjamie	if (!strcmp(path1, "/"))
4007192895Sjamie		return (path2);
4008192895Sjamie	len1 = strlen(path1);
4009192895Sjamie	if (strncmp(path1, path2, len1))
4010192895Sjamie		return (path2);
4011192895Sjamie	if (path2[len1] == '\0')
4012192895Sjamie		return "/";
4013192895Sjamie	if (path2[len1] == '/')
4014192895Sjamie		return (path2 + len1);
4015192895Sjamie	return (path2);
4016192895Sjamie}
4017192895Sjamie
4018192895Sjamie
4019192895Sjamie/*
4020192895Sjamie * Jail-related sysctls.
4021192895Sjamie */
4022227309Sedstatic SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4023192895Sjamie    "Jails");
4024192895Sjamie
4025113275Smikestatic int
4026113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
4027113275Smike{
4028191673Sjamie	struct xprison *xp;
4029192895Sjamie	struct prison *pr, *cpr;
4030191673Sjamie#ifdef INET
4031191673Sjamie	struct in_addr *ip4 = NULL;
4032191673Sjamie	int ip4s = 0;
4033191673Sjamie#endif
4034191673Sjamie#ifdef INET6
4035208803Scperciva	struct in6_addr *ip6 = NULL;
4036191673Sjamie	int ip6s = 0;
4037191673Sjamie#endif
4038192895Sjamie	int descend, error;
4039113275Smike
4040191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4041192895Sjamie	pr = req->td->td_ucred->cr_prison;
4042191673Sjamie	error = 0;
4043168401Spjd	sx_slock(&allprison_lock);
4044192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4045192895Sjamie#if defined(INET) || defined(INET6)
4046191673Sjamie again:
4047192895Sjamie#endif
4048192895Sjamie		mtx_lock(&cpr->pr_mtx);
4049185435Sbz#ifdef INET
4050192895Sjamie		if (cpr->pr_ip4s > 0) {
4051192895Sjamie			if (ip4s < cpr->pr_ip4s) {
4052192895Sjamie				ip4s = cpr->pr_ip4s;
4053192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4054191673Sjamie				ip4 = realloc(ip4, ip4s *
4055191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
4056191673Sjamie				goto again;
4057191673Sjamie			}
4058192895Sjamie			bcopy(cpr->pr_ip4, ip4,
4059192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
4060191673Sjamie		}
4061185435Sbz#endif
4062185435Sbz#ifdef INET6
4063192895Sjamie		if (cpr->pr_ip6s > 0) {
4064192895Sjamie			if (ip6s < cpr->pr_ip6s) {
4065192895Sjamie				ip6s = cpr->pr_ip6s;
4066192895Sjamie				mtx_unlock(&cpr->pr_mtx);
4067191673Sjamie				ip6 = realloc(ip6, ip6s *
4068191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4069191673Sjamie				goto again;
4070191673Sjamie			}
4071192895Sjamie			bcopy(cpr->pr_ip6, ip6,
4072192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
4073191673Sjamie		}
4074185435Sbz#endif
4075192895Sjamie		if (cpr->pr_ref == 0) {
4076192895Sjamie			mtx_unlock(&cpr->pr_mtx);
4077191673Sjamie			continue;
4078191673Sjamie		}
4079191673Sjamie		bzero(xp, sizeof(*xp));
4080113275Smike		xp->pr_version = XPRISON_VERSION;
4081192895Sjamie		xp->pr_id = cpr->pr_id;
4082192895Sjamie		xp->pr_state = cpr->pr_uref > 0
4083191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4084192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4085194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4086192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4087185435Sbz#ifdef INET
4088192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
4089185435Sbz#endif
4090185435Sbz#ifdef INET6
4091192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4092185435Sbz#endif
4093192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4094191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4095191673Sjamie		if (error)
4096191673Sjamie			break;
4097185435Sbz#ifdef INET
4098191673Sjamie		if (xp->pr_ip4s > 0) {
4099191673Sjamie			error = SYSCTL_OUT(req, ip4,
4100191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4101191673Sjamie			if (error)
4102191673Sjamie				break;
4103185435Sbz		}
4104185435Sbz#endif
4105185435Sbz#ifdef INET6
4106191673Sjamie		if (xp->pr_ip6s > 0) {
4107191673Sjamie			error = SYSCTL_OUT(req, ip6,
4108191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4109191673Sjamie			if (error)
4110191673Sjamie				break;
4111185435Sbz		}
4112185435Sbz#endif
4113113275Smike	}
4114168401Spjd	sx_sunlock(&allprison_lock);
4115191673Sjamie	free(xp, M_TEMP);
4116191673Sjamie#ifdef INET
4117191673Sjamie	free(ip4, M_TEMP);
4118191673Sjamie#endif
4119191673Sjamie#ifdef INET6
4120191673Sjamie	free(ip6, M_TEMP);
4121191673Sjamie#endif
4122167354Spjd	return (error);
4123113275Smike}
4124113275Smike
4125187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4126187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4127187864Sed    sysctl_jail_list, "S", "List of active jails");
4128126004Spjd
4129126004Spjdstatic int
4130126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4131126004Spjd{
4132126004Spjd	int error, injail;
4133126004Spjd
4134126004Spjd	injail = jailed(req->td->td_ucred);
4135126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4136126004Spjd
4137126004Spjd	return (error);
4138126004Spjd}
4139192895Sjamie
4140187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4141187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4142187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4143185435Sbz
4144250804Sjamiestatic int
4145250804Sjamiesysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4146250804Sjamie{
4147250804Sjamie	int error, havevnet;
4148250804Sjamie#ifdef VIMAGE
4149250804Sjamie	struct ucred *cred = req->td->td_ucred;
4150250804Sjamie
4151250804Sjamie	havevnet = jailed(cred) && prison_owns_vnet(cred);
4152250804Sjamie#else
4153250804Sjamie	havevnet = 0;
4154250804Sjamie#endif
4155250804Sjamie	error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4156250804Sjamie
4157250804Sjamie	return (error);
4158250804Sjamie}
4159250804Sjamie
4160250804SjamieSYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4161250804Sjamie    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4162250804Sjamie    sysctl_jail_vnet, "I", "Jail owns VNET?");
4163250804Sjamie
4164192895Sjamie#if defined(INET) || defined(INET6)
4165193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4166192895Sjamie    &jail_max_af_ips, 0,
4167192895Sjamie    "Number of IP addresses a jail may have at most per address family");
4168192895Sjamie#endif
4169192895Sjamie
4170192895Sjamie/*
4171192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
4172192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4173192895Sjamie * just see their own parameters, and can't change them.
4174192895Sjamie */
4175192895Sjamiestatic int
4176192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4177192895Sjamie{
4178192895Sjamie	struct prison *pr;
4179192895Sjamie	int allow, error, i;
4180192895Sjamie
4181192895Sjamie	pr = req->td->td_ucred->cr_prison;
4182192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4183192895Sjamie
4184192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4185192895Sjamie	i = (allow & arg2) ? 1 : 0;
4186192895Sjamie	if (arg1 != NULL)
4187192895Sjamie		i = !i;
4188192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4189192895Sjamie	if (error || !req->newptr)
4190192895Sjamie		return (error);
4191192895Sjamie	i = i ? arg2 : 0;
4192192895Sjamie	if (arg1 != NULL)
4193192895Sjamie		i ^= arg2;
4194192895Sjamie	/*
4195192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4196192895Sjamie	 * for writing.
4197192895Sjamie	 */
4198192895Sjamie	mtx_lock(&prison0.pr_mtx);
4199192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4200192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4201192895Sjamie	return (0);
4202192895Sjamie}
4203192895Sjamie
4204192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4205192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4206192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4207192895Sjamie    "Processes in jail can set their hostnames");
4208192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4209192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4210192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4211192895Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only");
4212192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4213192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4214192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4215192895Sjamie    "Processes in jail can use System V IPC primitives");
4216192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4217192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4218192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4219192895Sjamie    "Prison root can create raw sockets");
4220192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4221192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4222192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4223192895Sjamie    "Processes in jail can alter system file flags");
4224192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4225192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4226192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4227192895Sjamie    "Processes in jail can mount/unmount jail-friendly file systems");
4228232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed,
4229232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4230232059Smm    NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I",
4231232186Smm    "Processes in jail can mount the devfs file system");
4232232059SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed,
4233232059Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4234232059Smm    NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I",
4235232186Smm    "Processes in jail can mount the nullfs file system");
4236232278SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed,
4237232278Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4238232278Smm    NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I",
4239232278Smm    "Processes in jail can mount the procfs file system");
4240254741SdelphijSYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed,
4241254741Sdelphij    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4242254741Sdelphij    NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I",
4243254741Sdelphij    "Processes in jail can mount the tmpfs file system");
4244232186SmmSYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed,
4245232186Smm    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4246232186Smm    NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I",
4247232186Smm    "Processes in jail can mount the zfs file system");
4248192895Sjamie
4249192895Sjamiestatic int
4250192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4251192895Sjamie{
4252192895Sjamie	struct prison *pr;
4253192895Sjamie	int level, error;
4254192895Sjamie
4255192895Sjamie	pr = req->td->td_ucred->cr_prison;
4256192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4257192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4258192895Sjamie	if (error || !req->newptr)
4259192895Sjamie		return (error);
4260192895Sjamie	*(int *)arg1 = level;
4261192895Sjamie	return (0);
4262192895Sjamie}
4263192895Sjamie
4264192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4265192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4266192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4267192895Sjamie    sysctl_jail_default_level, "I",
4268192895Sjamie    "Processes in jail cannot see all mounted file systems");
4269192895Sjamie
4270231267SmmSYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4271231267Smm    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4272231267Smm    &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4273231267Smm    sysctl_jail_default_level, "I",
4274231267Smm    "Ruleset for the devfs filesystem in jail");
4275231267Smm
4276192895Sjamie/*
4277192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4278192895Sjamie * is returned in the string itself, and the other parameters exist merely
4279192895Sjamie * to make themselves and their types known.
4280192895Sjamie */
4281192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4282192895Sjamie    "Jail parameters");
4283192895Sjamie
4284192895Sjamieint
4285192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4286192895Sjamie{
4287192895Sjamie	int i;
4288192895Sjamie	long l;
4289192895Sjamie	size_t s;
4290192895Sjamie	char numbuf[12];
4291192895Sjamie
4292192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4293192895Sjamie	{
4294192895Sjamie	case CTLTYPE_LONG:
4295192895Sjamie	case CTLTYPE_ULONG:
4296192895Sjamie		l = 0;
4297192895Sjamie#ifdef SCTL_MASK32
4298192895Sjamie		if (!(req->flags & SCTL_MASK32))
4299192895Sjamie#endif
4300192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4301192895Sjamie	case CTLTYPE_INT:
4302192895Sjamie	case CTLTYPE_UINT:
4303192895Sjamie		i = 0;
4304192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4305192895Sjamie	case CTLTYPE_STRING:
4306219819Sjeff		snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4307192895Sjamie		return
4308192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4309192895Sjamie	case CTLTYPE_STRUCT:
4310192895Sjamie		s = (size_t)arg2;
4311192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4312192895Sjamie	}
4313192895Sjamie	return (0);
4314192895Sjamie}
4315192895Sjamie
4316192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4317192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4318192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4319192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4320192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4321192895Sjamie    "I", "Jail secure level");
4322192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4323192895Sjamie    "I", "Jail cannot see all mounted file systems");
4324231267SmmSYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4325231267Smm    "I", "Ruleset for in-jail devfs mounts");
4326192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4327192895Sjamie    "B", "Jail persistence");
4328194251Sjamie#ifdef VIMAGE
4329194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4330195870Sjamie    "E,jailsys", "Virtual network stack");
4331194251Sjamie#endif
4332192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4333192895Sjamie    "B", "Jail is in the process of shutting down");
4334192895Sjamie
4335194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4336194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4337194762Sjamie    "I", "Current number of child jails");
4338194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4339194762Sjamie    "I", "Maximum number of child jails");
4340194762Sjamie
4341195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4342192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4343192895Sjamie    "Jail hostname");
4344193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4345193066Sjamie    "Jail NIS domainname");
4346193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4347193066Sjamie    "Jail host UUID");
4348193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4349193066Sjamie    "LU", "Jail host ID");
4350192895Sjamie
4351192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4352192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4353192895Sjamie
4354192895Sjamie#ifdef INET
4355195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4356195974Sjamie    "Jail IPv4 address virtualization");
4357192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4358192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4359202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4360202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4361202468Sbz    "primary jail IPv4 address.");
4362192895Sjamie#endif
4363192895Sjamie#ifdef INET6
4364195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4365195974Sjamie    "Jail IPv6 address virtualization");
4366192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4367192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4368202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4369202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4370202468Sbz    "primary jail IPv6 address.");
4371192895Sjamie#endif
4372192895Sjamie
4373192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4374192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4375192895Sjamie    "B", "Jail may set hostname");
4376192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4377192895Sjamie    "B", "Jail may use SYSV IPC");
4378192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4379192895Sjamie    "B", "Jail may create raw sockets");
4380192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4381192895Sjamie    "B", "Jail may alter system file flags");
4382192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4383192895Sjamie    "B", "Jail may set file quotas");
4384192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4385192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4386192895Sjamie
4387232059SmmSYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4388232059SmmSYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4389232059Smm    "B", "Jail may mount/unmount jail-friendly file systems in general");
4390232059SmmSYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW,
4391232186Smm    "B", "Jail may mount the devfs file system");
4392232059SmmSYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW,
4393232186Smm    "B", "Jail may mount the nullfs file system");
4394232278SmmSYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW,
4395232278Smm    "B", "Jail may mount the procfs file system");
4396254741SdelphijSYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW,
4397254741Sdelphij    "B", "Jail may mount the tmpfs file system");
4398232186SmmSYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW,
4399232186Smm    "B", "Jail may mount the zfs file system");
4400232059Smm
4401220137Straszvoid
4402220137Straszprison_racct_foreach(void (*callback)(struct racct *racct,
4403220137Strasz    void *arg2, void *arg3), void *arg2, void *arg3)
4404220137Strasz{
4405221362Strasz	struct prison_racct *prr;
4406192895Sjamie
4407220137Strasz	sx_slock(&allprison_lock);
4408221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next)
4409221362Strasz		(callback)(prr->prr_racct, arg2, arg3);
4410220137Strasz	sx_sunlock(&allprison_lock);
4411220137Strasz}
4412220137Strasz
4413221362Straszstatic struct prison_racct *
4414221362Straszprison_racct_find_locked(const char *name)
4415221362Strasz{
4416221362Strasz	struct prison_racct *prr;
4417221362Strasz
4418221362Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4419221362Strasz
4420221362Strasz	if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4421221362Strasz		return (NULL);
4422221362Strasz
4423221362Strasz	LIST_FOREACH(prr, &allprison_racct, prr_next) {
4424221362Strasz		if (strcmp(name, prr->prr_name) != 0)
4425221362Strasz			continue;
4426221362Strasz
4427221362Strasz		/* Found prison_racct with a matching name? */
4428221362Strasz		prison_racct_hold(prr);
4429221362Strasz		return (prr);
4430221362Strasz	}
4431221362Strasz
4432221362Strasz	/* Add new prison_racct. */
4433221362Strasz	prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4434221362Strasz	racct_create(&prr->prr_racct);
4435221362Strasz
4436221362Strasz	strcpy(prr->prr_name, name);
4437221362Strasz	refcount_init(&prr->prr_refcount, 1);
4438221362Strasz	LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4439221362Strasz
4440221362Strasz	return (prr);
4441221362Strasz}
4442221362Strasz
4443221362Straszstruct prison_racct *
4444221362Straszprison_racct_find(const char *name)
4445221362Strasz{
4446221362Strasz	struct prison_racct *prr;
4447221362Strasz
4448221362Strasz	sx_xlock(&allprison_lock);
4449221362Strasz	prr = prison_racct_find_locked(name);
4450221362Strasz	sx_xunlock(&allprison_lock);
4451221362Strasz	return (prr);
4452221362Strasz}
4453221362Strasz
4454221362Straszvoid
4455221362Straszprison_racct_hold(struct prison_racct *prr)
4456221362Strasz{
4457221362Strasz
4458221362Strasz	refcount_acquire(&prr->prr_refcount);
4459221362Strasz}
4460221362Strasz
4461232598Straszstatic void
4462232598Straszprison_racct_free_locked(struct prison_racct *prr)
4463232598Strasz{
4464232598Strasz
4465232598Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4466232598Strasz
4467232598Strasz	if (refcount_release(&prr->prr_refcount)) {
4468232598Strasz		racct_destroy(&prr->prr_racct);
4469232598Strasz		LIST_REMOVE(prr, prr_next);
4470232598Strasz		free(prr, M_PRISON_RACCT);
4471232598Strasz	}
4472232598Strasz}
4473232598Strasz
4474221362Straszvoid
4475221362Straszprison_racct_free(struct prison_racct *prr)
4476221362Strasz{
4477221362Strasz	int old;
4478221362Strasz
4479232598Strasz	sx_assert(&allprison_lock, SA_UNLOCKED);
4480232598Strasz
4481221362Strasz	old = prr->prr_refcount;
4482221362Strasz	if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4483221362Strasz		return;
4484221362Strasz
4485221362Strasz	sx_xlock(&allprison_lock);
4486232598Strasz	prison_racct_free_locked(prr);
4487221362Strasz	sx_xunlock(&allprison_lock);
4488221362Strasz}
4489221362Strasz
4490221362Strasz#ifdef RACCT
4491221362Straszstatic void
4492221362Straszprison_racct_attach(struct prison *pr)
4493221362Strasz{
4494221362Strasz	struct prison_racct *prr;
4495221362Strasz
4496232598Strasz	sx_assert(&allprison_lock, SA_XLOCKED);
4497232598Strasz
4498221362Strasz	prr = prison_racct_find_locked(pr->pr_name);
4499221362Strasz	KASSERT(prr != NULL, ("cannot find prison_racct"));
4500221362Strasz
4501221362Strasz	pr->pr_prison_racct = prr;
4502221362Strasz}
4503221362Strasz
4504232598Strasz/*
4505232598Strasz * Handle jail renaming.  From the racct point of view, renaming means
4506232598Strasz * moving from one prison_racct to another.
4507232598Strasz */
4508221362Straszstatic void
4509232598Straszprison_racct_modify(struct prison *pr)
4510232598Strasz{
4511232598Strasz	struct proc *p;
4512232598Strasz	struct ucred *cred;
4513232598Strasz	struct prison_racct *oldprr;
4514232598Strasz
4515232598Strasz	sx_slock(&allproc_lock);
4516232598Strasz	sx_xlock(&allprison_lock);
4517232598Strasz
4518235795Strasz	if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4519235795Strasz		sx_xunlock(&allprison_lock);
4520235795Strasz		sx_sunlock(&allproc_lock);
4521232598Strasz		return;
4522235795Strasz	}
4523232598Strasz
4524232598Strasz	oldprr = pr->pr_prison_racct;
4525232598Strasz	pr->pr_prison_racct = NULL;
4526232598Strasz
4527232598Strasz	prison_racct_attach(pr);
4528232598Strasz
4529232598Strasz	/*
4530232598Strasz	 * Move resource utilisation records.
4531232598Strasz	 */
4532232598Strasz	racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4533232598Strasz
4534232598Strasz	/*
4535232598Strasz	 * Force rctl to reattach rules to processes.
4536232598Strasz	 */
4537232598Strasz	FOREACH_PROC_IN_SYSTEM(p) {
4538232598Strasz		PROC_LOCK(p);
4539232598Strasz		cred = crhold(p->p_ucred);
4540232598Strasz		PROC_UNLOCK(p);
4541232598Strasz		racct_proc_ucred_changed(p, cred, cred);
4542232598Strasz		crfree(cred);
4543232598Strasz	}
4544232598Strasz
4545232598Strasz	sx_sunlock(&allproc_lock);
4546232598Strasz	prison_racct_free_locked(oldprr);
4547232598Strasz	sx_xunlock(&allprison_lock);
4548232598Strasz}
4549232598Strasz
4550232598Straszstatic void
4551221362Straszprison_racct_detach(struct prison *pr)
4552221362Strasz{
4553232598Strasz
4554232598Strasz	sx_assert(&allprison_lock, SA_UNLOCKED);
4555232598Strasz
4556244404Smjg	if (pr->pr_prison_racct == NULL)
4557244404Smjg		return;
4558221362Strasz	prison_racct_free(pr->pr_prison_racct);
4559221362Strasz	pr->pr_prison_racct = NULL;
4560221362Strasz}
4561221362Strasz#endif /* RACCT */
4562221362Strasz
4563185435Sbz#ifdef DDB
4564191673Sjamie
4565191673Sjamiestatic void
4566191673Sjamiedb_show_prison(struct prison *pr)
4567185435Sbz{
4568192895Sjamie	int fi;
4569191673Sjamie#if defined(INET) || defined(INET6)
4570191673Sjamie	int ii;
4571185435Sbz#endif
4572195870Sjamie	unsigned jsf;
4573185435Sbz#ifdef INET6
4574185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4575185435Sbz#endif
4576185435Sbz
4577191673Sjamie	db_printf("prison %p:\n", pr);
4578191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4579191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4580192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4581191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4582191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4583191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4584191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4585191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4586194251Sjamie#ifdef VIMAGE
4587194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4588194251Sjamie#endif
4589191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4590191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4591231267Smm	db_printf(" devfs_rsnum     = %d\n", pr->pr_devfs_rsnum);
4592202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4593202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4594192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4595192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4596202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4597192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4598192895Sjamie	    fi++)
4599192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4600192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4601195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4602195870Sjamie	    fi++) {
4603195870Sjamie		jsf = pr->pr_flags &
4604195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4605195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4606195870Sjamie		    pr_flag_jailsys[fi].disable &&
4607195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4608195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4609195870Sjamie		    : "inherit");
4610195870Sjamie	}
4611202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4612192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4613192895Sjamie	    fi++)
4614192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4615192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4616191673Sjamie	db_printf("\n");
4617192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4618194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4619194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4620194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4621193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4622185435Sbz#ifdef INET
4623191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4624191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4625191673Sjamie		db_printf(" %s %s\n",
4626202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4627191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4628185435Sbz#endif
4629185435Sbz#ifdef INET6
4630191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4631191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4632191673Sjamie		db_printf(" %s %s\n",
4633202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4634191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4635191673Sjamie#endif
4636191673Sjamie}
4637191673Sjamie
4638191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4639191673Sjamie{
4640191673Sjamie	struct prison *pr;
4641191673Sjamie
4642191673Sjamie	if (!have_addr) {
4643192895Sjamie		/*
4644192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4645192895Sjamie		 * listed.
4646192895Sjamie		 */
4647192895Sjamie		db_show_prison(&prison0);
4648192895Sjamie		if (!db_pager_quit) {
4649192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4650192895Sjamie				db_show_prison(pr);
4651192895Sjamie				if (db_pager_quit)
4652192895Sjamie					break;
4653192895Sjamie			}
4654191673Sjamie		}
4655191673Sjamie		return;
4656191673Sjamie	}
4657191673Sjamie
4658192895Sjamie	if (addr == 0)
4659192895Sjamie		pr = &prison0;
4660192895Sjamie	else {
4661192895Sjamie		/* Look for a prison with the ID and with references. */
4662191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4663192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4664191673Sjamie				break;
4665192895Sjamie		if (pr == NULL)
4666192895Sjamie			/* Look again, without requiring a reference. */
4667192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4668192895Sjamie				if (pr->pr_id == addr)
4669192895Sjamie					break;
4670192895Sjamie		if (pr == NULL)
4671192895Sjamie			/* Assume address points to a valid prison. */
4672192895Sjamie			pr = (struct prison *)addr;
4673192895Sjamie	}
4674191673Sjamie	db_show_prison(pr);
4675185435Sbz}
4676191673Sjamie
4677185435Sbz#endif /* DDB */
4678