kern_jail.c revision 202116
190075Sobrien/*-
290075Sobrien * Copyright (c) 1999 Poul-Henning Kamp.
390075Sobrien * Copyright (c) 2008 Bjoern A. Zeeb.
490075Sobrien * Copyright (c) 2009 James Gritton.
590075Sobrien * All rights reserved.
690075Sobrien *
790075Sobrien * Redistribution and use in source and binary forms, with or without
890075Sobrien * modification, are permitted provided that the following conditions
990075Sobrien * are met:
1090075Sobrien * 1. Redistributions of source code must retain the above copyright
1190075Sobrien *    notice, this list of conditions and the following disclaimer.
1290075Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1390075Sobrien *    notice, this list of conditions and the following disclaimer in the
1490075Sobrien *    documentation and/or other materials provided with the distribution.
1590075Sobrien *
1690075Sobrien * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1790075Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1890075Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1990075Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2090075Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2190075Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2290075Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2390075Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2490075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2590075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2690075Sobrien * SUCH DAMAGE.
2790075Sobrien */
2890075Sobrien
2990075Sobrien#include <sys/cdefs.h>
3090075Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_jail.c 202116 2010-01-11 21:21:30Z bz $");
3190075Sobrien
3290075Sobrien#include "opt_compat.h"
3390075Sobrien#include "opt_ddb.h"
3490075Sobrien#include "opt_inet.h"
3590075Sobrien#include "opt_inet6.h"
3690075Sobrien
3790075Sobrien#include <sys/param.h>
3890075Sobrien#include <sys/types.h>
3990075Sobrien#include <sys/kernel.h>
4090075Sobrien#include <sys/systm.h>
4190075Sobrien#include <sys/errno.h>
4290075Sobrien#include <sys/sysproto.h>
4390075Sobrien#include <sys/malloc.h>
4490075Sobrien#include <sys/osd.h>
4590075Sobrien#include <sys/priv.h>
4690075Sobrien#include <sys/proc.h>
4790075Sobrien#include <sys/taskqueue.h>
4890075Sobrien#include <sys/fcntl.h>
4990075Sobrien#include <sys/jail.h>
5090075Sobrien#include <sys/lock.h>
5190075Sobrien#include <sys/mutex.h>
5290075Sobrien#include <sys/sx.h>
5390075Sobrien#include <sys/sysent.h>
5490075Sobrien#include <sys/namei.h>
5590075Sobrien#include <sys/mount.h>
5690075Sobrien#include <sys/queue.h>
5790075Sobrien#include <sys/socket.h>
5890075Sobrien#include <sys/syscallsubr.h>
5990075Sobrien#include <sys/sysctl.h>
6090075Sobrien#include <sys/vnode.h>
6190075Sobrien
6290075Sobrien#include <net/if.h>
6390075Sobrien#include <net/vnet.h>
6490075Sobrien
6590075Sobrien#include <netinet/in.h>
6690075Sobrien
6790075Sobrien#ifdef DDB
6890075Sobrien#include <ddb/ddb.h>
6990075Sobrien#ifdef INET6
7090075Sobrien#include <netinet6/in6_var.h>
7190075Sobrien#endif /* INET6 */
7290075Sobrien#endif /* DDB */
7390075Sobrien
7490075Sobrien#include <security/mac/mac_framework.h>
7590075Sobrien
7690075Sobrien#define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
7790075Sobrien
7890075SobrienMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
7990075Sobrien
8090075Sobrien/* prison0 describes what is "real" about the system. */
8190075Sobrienstruct prison prison0 = {
8290075Sobrien	.pr_id		= 0,
8390075Sobrien	.pr_name	= "0",
8490075Sobrien	.pr_ref		= 1,
8590075Sobrien	.pr_uref	= 1,
8690075Sobrien	.pr_path	= "/",
8790075Sobrien	.pr_securelevel	= -1,
8890075Sobrien	.pr_childmax	= JAIL_MAX,
8990075Sobrien	.pr_hostuuid	= DEFAULT_HOSTUUID,
9090075Sobrien	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
9190075Sobrien#ifdef VIMAGE
9290075Sobrien	.pr_flags	= PR_HOST|PR_VNET,
9390075Sobrien#else
9490075Sobrien	.pr_flags	= PR_HOST,
9590075Sobrien#endif
9690075Sobrien	.pr_allow	= PR_ALLOW_ALL,
9790075Sobrien};
9890075SobrienMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
9990075Sobrien
10090075Sobrien/* allprison and lastprid are protected by allprison_lock. */
10190075Sobrienstruct	sx allprison_lock;
10290075SobrienSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
10390075Sobrienstruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
10490075Sobrienint	lastprid = 0;
10590075Sobrien
10690075Sobrienstatic int do_jail_attach(struct thread *td, struct prison *pr);
10790075Sobrienstatic void prison_complete(void *context, int pending);
10890075Sobrienstatic void prison_deref(struct prison *pr, int flags);
10990075Sobrienstatic char *prison_path(struct prison *pr1, struct prison *pr2);
11090075Sobrienstatic void prison_remove_one(struct prison *pr);
11190075Sobrien#ifdef INET
11290075Sobrienstatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
11390075Sobrienstatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
11490075Sobrien#endif
11590075Sobrien#ifdef INET6
11690075Sobrienstatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
11790075Sobrienstatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
11890075Sobrien#endif
11990075Sobrien
12090075Sobrien/* Flags for prison_deref */
12190075Sobrien#define	PD_DEREF	0x01
12290075Sobrien#define	PD_DEUREF	0x02
12390075Sobrien#define	PD_LOCKED	0x04
12490075Sobrien#define	PD_LIST_SLOCKED	0x08
12590075Sobrien#define	PD_LIST_XLOCKED	0x10
12690075Sobrien
12790075Sobrien/*
12890075Sobrien * Parameter names corresponding to PR_* flag values
12990075Sobrien */
13090075Sobrienstatic char *pr_flag_names[] = {
13190075Sobrien	[0] = "persist",
13290075Sobrien};
13390075Sobrien
13490075Sobrienstatic char *pr_flag_nonames[] = {
13590075Sobrien	[0] = "nopersist",
13690075Sobrien};
13790075Sobrien
13890075Sobrienstruct jailsys_flags {
13990075Sobrien	const char	*name;
14090075Sobrien	unsigned	 disable;
14190075Sobrien	unsigned	 new;
14290075Sobrien} pr_flag_jailsys[] = {
14390075Sobrien	{ "host", 0, PR_HOST },
14490075Sobrien#ifdef VIMAGE
14590075Sobrien	{ "vnet", 0, PR_VNET },
14690075Sobrien#endif
14790075Sobrien#ifdef INET
14890075Sobrien	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
14990075Sobrien#endif
15090075Sobrien#ifdef INET6
15190075Sobrien	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
15290075Sobrien#endif
15390075Sobrien};
15490075Sobrien
15590075Sobrienstatic char *pr_allow_names[] = {
15690075Sobrien	"allow.set_hostname",
15790075Sobrien	"allow.sysvipc",
15890075Sobrien	"allow.raw_sockets",
15990075Sobrien	"allow.chflags",
16090075Sobrien	"allow.mount",
16190075Sobrien	"allow.quotas",
16290075Sobrien	"allow.socket_af",
16390075Sobrien};
16490075Sobrien
16590075Sobrienstatic char *pr_allow_nonames[] = {
16690075Sobrien	"allow.noset_hostname",
16790075Sobrien	"allow.nosysvipc",
16890075Sobrien	"allow.noraw_sockets",
16990075Sobrien	"allow.nochflags",
17090075Sobrien	"allow.nomount",
17190075Sobrien	"allow.noquotas",
17290075Sobrien	"allow.nosocket_af",
17390075Sobrien};
17490075Sobrien
17590075Sobrien#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
17690075Sobrien#define	JAIL_DEFAULT_ENFORCE_STATFS	2
17790075Sobrienstatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
17890075Sobrienstatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
17990075Sobrien#if defined(INET) || defined(INET6)
18090075Sobrienstatic unsigned jail_max_af_ips = 255;
18190075Sobrien#endif
18290075Sobrien
18390075Sobrien#ifdef INET
18490075Sobrienstatic int
18590075Sobrienqcmp_v4(const void *ip1, const void *ip2)
18690075Sobrien{
18790075Sobrien	in_addr_t iaa, iab;
18890075Sobrien
18990075Sobrien	/*
19090075Sobrien	 * We need to compare in HBO here to get the list sorted as expected
19190075Sobrien	 * by the result of the code.  Sorting NBO addresses gives you
19290075Sobrien	 * interesting results.  If you do not understand, do not try.
19390075Sobrien	 */
19490075Sobrien	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
19590075Sobrien	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
19690075Sobrien
19790075Sobrien	/*
19890075Sobrien	 * Do not simply return the difference of the two numbers, the int is
19990075Sobrien	 * not wide enough.
20090075Sobrien	 */
20190075Sobrien	if (iaa > iab)
20290075Sobrien		return (1);
20390075Sobrien	else if (iaa < iab)
20490075Sobrien		return (-1);
20590075Sobrien	else
20690075Sobrien		return (0);
20790075Sobrien}
20890075Sobrien#endif
20990075Sobrien
21090075Sobrien#ifdef INET6
21190075Sobrienstatic int
21290075Sobrienqcmp_v6(const void *ip1, const void *ip2)
21390075Sobrien{
21490075Sobrien	const struct in6_addr *ia6a, *ia6b;
21590075Sobrien	int i, rc;
21690075Sobrien
21790075Sobrien	ia6a = (const struct in6_addr *)ip1;
21890075Sobrien	ia6b = (const struct in6_addr *)ip2;
21990075Sobrien
22090075Sobrien	rc = 0;
22190075Sobrien	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
22290075Sobrien		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
22390075Sobrien			rc = 1;
22490075Sobrien		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
22590075Sobrien			rc = -1;
22690075Sobrien	}
22790075Sobrien	return (rc);
22890075Sobrien}
22990075Sobrien#endif
23090075Sobrien
23190075Sobrien/*
23290075Sobrien * struct jail_args {
23390075Sobrien *	struct jail *jail;
23490075Sobrien * };
23590075Sobrien */
23690075Sobrienint
23790075Sobrienjail(struct thread *td, struct jail_args *uap)
23890075Sobrien{
23990075Sobrien	uint32_t version;
24090075Sobrien	int error;
24190075Sobrien	struct jail j;
24290075Sobrien
24390075Sobrien	error = copyin(uap->jail, &version, sizeof(uint32_t));
24490075Sobrien	if (error)
24590075Sobrien		return (error);
24690075Sobrien
24790075Sobrien	switch (version) {
24890075Sobrien	case 0:
24990075Sobrien	{
25090075Sobrien		struct jail_v0 j0;
25190075Sobrien
25290075Sobrien		/* FreeBSD single IPv4 jails. */
25390075Sobrien		bzero(&j, sizeof(struct jail));
25490075Sobrien		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
25590075Sobrien		if (error)
25690075Sobrien			return (error);
25790075Sobrien		j.version = j0.version;
25890075Sobrien		j.path = j0.path;
25990075Sobrien		j.hostname = j0.hostname;
26090075Sobrien		j.ip4s = j0.ip_number;
26190075Sobrien		break;
26290075Sobrien	}
26390075Sobrien
26490075Sobrien	case 1:
26590075Sobrien		/*
26690075Sobrien		 * Version 1 was used by multi-IPv4 jail implementations
26790075Sobrien		 * that never made it into the official kernel.
26890075Sobrien		 */
26990075Sobrien		return (EINVAL);
27090075Sobrien
27190075Sobrien	case 2:	/* JAIL_API_VERSION */
27290075Sobrien		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
27390075Sobrien		error = copyin(uap->jail, &j, sizeof(struct jail));
27490075Sobrien		if (error)
27590075Sobrien			return (error);
27690075Sobrien		break;
27790075Sobrien
27890075Sobrien	default:
27990075Sobrien		/* Sci-Fi jails are not supported, sorry. */
28090075Sobrien		return (EINVAL);
28190075Sobrien	}
28290075Sobrien	return (kern_jail(td, &j));
28390075Sobrien}
28490075Sobrien
28590075Sobrienint
28690075Sobrienkern_jail(struct thread *td, struct jail *j)
28790075Sobrien{
28890075Sobrien	struct iovec optiov[2 * (4
28990075Sobrien			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
29090075Sobrien#ifdef INET
29190075Sobrien			    + 1
29290075Sobrien#endif
29390075Sobrien#ifdef INET6
29490075Sobrien			    + 1
29590075Sobrien#endif
29690075Sobrien			    )];
29790075Sobrien	struct uio opt;
29890075Sobrien	char *u_path, *u_hostname, *u_name;
29990075Sobrien#ifdef INET
30090075Sobrien	uint32_t ip4s;
30190075Sobrien	struct in_addr *u_ip4;
30290075Sobrien#endif
30390075Sobrien#ifdef INET6
30490075Sobrien	struct in6_addr *u_ip6;
30590075Sobrien#endif
30690075Sobrien	size_t tmplen;
30790075Sobrien	int error, enforce_statfs, fi;
30890075Sobrien
30990075Sobrien	bzero(&optiov, sizeof(optiov));
31090075Sobrien	opt.uio_iov = optiov;
31190075Sobrien	opt.uio_iovcnt = 0;
31290075Sobrien	opt.uio_offset = -1;
31390075Sobrien	opt.uio_resid = -1;
31490075Sobrien	opt.uio_segflg = UIO_SYSSPACE;
31590075Sobrien	opt.uio_rw = UIO_READ;
31690075Sobrien	opt.uio_td = td;
31790075Sobrien
31890075Sobrien	/* Set permissions for top-level jails from sysctls. */
31990075Sobrien	if (!jailed(td->td_ucred)) {
32090075Sobrien		for (fi = 0; fi < sizeof(pr_allow_names) /
32190075Sobrien		     sizeof(pr_allow_names[0]); fi++) {
32290075Sobrien			optiov[opt.uio_iovcnt].iov_base =
32390075Sobrien			    (jail_default_allow & (1 << fi))
32490075Sobrien			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
32590075Sobrien			optiov[opt.uio_iovcnt].iov_len =
32690075Sobrien			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
32790075Sobrien			opt.uio_iovcnt += 2;
32890075Sobrien		}
32990075Sobrien		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
33090075Sobrien		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
33190075Sobrien		opt.uio_iovcnt++;
33290075Sobrien		enforce_statfs = jail_default_enforce_statfs;
33390075Sobrien		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
33490075Sobrien		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
33590075Sobrien		opt.uio_iovcnt++;
33690075Sobrien	}
33790075Sobrien
33890075Sobrien	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
33990075Sobrien#ifdef INET
34090075Sobrien	ip4s = (j->version == 0) ? 1 : j->ip4s;
34190075Sobrien	if (ip4s > jail_max_af_ips)
34290075Sobrien		return (EINVAL);
34390075Sobrien	tmplen += ip4s * sizeof(struct in_addr);
34490075Sobrien#else
34590075Sobrien	if (j->ip4s > 0)
34690075Sobrien		return (EINVAL);
34790075Sobrien#endif
34890075Sobrien#ifdef INET6
34990075Sobrien	if (j->ip6s > jail_max_af_ips)
35090075Sobrien		return (EINVAL);
35190075Sobrien	tmplen += j->ip6s * sizeof(struct in6_addr);
35290075Sobrien#else
35390075Sobrien	if (j->ip6s > 0)
35490075Sobrien		return (EINVAL);
35590075Sobrien#endif
35690075Sobrien	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
35790075Sobrien	u_hostname = u_path + MAXPATHLEN;
35890075Sobrien	u_name = u_hostname + MAXHOSTNAMELEN;
35990075Sobrien#ifdef INET
36090075Sobrien	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
36190075Sobrien#endif
36290075Sobrien#ifdef INET6
36390075Sobrien#ifdef INET
36490075Sobrien	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
36590075Sobrien#else
36690075Sobrien	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
36790075Sobrien#endif
36890075Sobrien#endif
36990075Sobrien	optiov[opt.uio_iovcnt].iov_base = "path";
37090075Sobrien	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
37190075Sobrien	opt.uio_iovcnt++;
37290075Sobrien	optiov[opt.uio_iovcnt].iov_base = u_path;
37390075Sobrien	error = copyinstr(j->path, u_path, MAXPATHLEN,
37490075Sobrien	    &optiov[opt.uio_iovcnt].iov_len);
37590075Sobrien	if (error) {
37690075Sobrien		free(u_path, M_TEMP);
37790075Sobrien		return (error);
37890075Sobrien	}
37990075Sobrien	opt.uio_iovcnt++;
38090075Sobrien	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
38190075Sobrien	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
38290075Sobrien	opt.uio_iovcnt++;
38390075Sobrien	optiov[opt.uio_iovcnt].iov_base = u_hostname;
38490075Sobrien	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
38590075Sobrien	    &optiov[opt.uio_iovcnt].iov_len);
38690075Sobrien	if (error) {
38790075Sobrien		free(u_path, M_TEMP);
38890075Sobrien		return (error);
38990075Sobrien	}
39090075Sobrien	opt.uio_iovcnt++;
39190075Sobrien	if (j->jailname != NULL) {
39290075Sobrien		optiov[opt.uio_iovcnt].iov_base = "name";
39390075Sobrien		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
39490075Sobrien		opt.uio_iovcnt++;
39590075Sobrien		optiov[opt.uio_iovcnt].iov_base = u_name;
39690075Sobrien		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
39790075Sobrien		    &optiov[opt.uio_iovcnt].iov_len);
39890075Sobrien		if (error) {
39990075Sobrien			free(u_path, M_TEMP);
40090075Sobrien			return (error);
40190075Sobrien		}
40290075Sobrien		opt.uio_iovcnt++;
40390075Sobrien	}
40490075Sobrien#ifdef INET
40590075Sobrien	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
40690075Sobrien	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
40790075Sobrien	opt.uio_iovcnt++;
40890075Sobrien	optiov[opt.uio_iovcnt].iov_base = u_ip4;
40990075Sobrien	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
41090075Sobrien	if (j->version == 0)
41190075Sobrien		u_ip4->s_addr = j->ip4s;
41290075Sobrien	else {
41390075Sobrien		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
41490075Sobrien		if (error) {
41590075Sobrien			free(u_path, M_TEMP);
41690075Sobrien			return (error);
41790075Sobrien		}
41890075Sobrien	}
41990075Sobrien	opt.uio_iovcnt++;
42090075Sobrien#endif
42190075Sobrien#ifdef INET6
42290075Sobrien	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
42390075Sobrien	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
42490075Sobrien	opt.uio_iovcnt++;
42590075Sobrien	optiov[opt.uio_iovcnt].iov_base = u_ip6;
42690075Sobrien	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
42790075Sobrien	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
42890075Sobrien	if (error) {
42990075Sobrien		free(u_path, M_TEMP);
43090075Sobrien		return (error);
43190075Sobrien	}
43290075Sobrien	opt.uio_iovcnt++;
43390075Sobrien#endif
43490075Sobrien	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
43590075Sobrien	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
43690075Sobrien	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
43790075Sobrien	free(u_path, M_TEMP);
43890075Sobrien	return (error);
43990075Sobrien}
44090075Sobrien
44190075Sobrien
44290075Sobrien/*
44390075Sobrien * struct jail_set_args {
44490075Sobrien *	struct iovec *iovp;
44590075Sobrien *	unsigned int iovcnt;
44690075Sobrien *	int flags;
44790075Sobrien * };
44890075Sobrien */
44990075Sobrienint
45090075Sobrienjail_set(struct thread *td, struct jail_set_args *uap)
45190075Sobrien{
45290075Sobrien	struct uio *auio;
45390075Sobrien	int error;
45490075Sobrien
45590075Sobrien	/* Check that we have an even number of iovecs. */
45690075Sobrien	if (uap->iovcnt & 1)
45790075Sobrien		return (EINVAL);
45890075Sobrien
45990075Sobrien	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
46090075Sobrien	if (error)
46190075Sobrien		return (error);
46290075Sobrien	error = kern_jail_set(td, auio, uap->flags);
46390075Sobrien	free(auio, M_IOV);
46490075Sobrien	return (error);
46590075Sobrien}
46690075Sobrien
46790075Sobrienint
46890075Sobrienkern_jail_set(struct thread *td, struct uio *optuio, int flags)
46990075Sobrien{
47090075Sobrien	struct nameidata nd;
47190075Sobrien#ifdef INET
47290075Sobrien	struct in_addr *ip4;
47390075Sobrien#endif
47490075Sobrien#ifdef INET6
47590075Sobrien	struct in6_addr *ip6;
47690075Sobrien#endif
47790075Sobrien	struct vfsopt *opt;
47890075Sobrien	struct vfsoptlist *opts;
47990075Sobrien	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
48090075Sobrien	struct vnode *root;
48190075Sobrien	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
48290075Sobrien#if defined(INET) || defined(INET6)
48390075Sobrien	struct prison *tppr;
48490075Sobrien	void *op;
48590075Sobrien#endif
48690075Sobrien	unsigned long hid;
48790075Sobrien	size_t namelen, onamelen;
48890075Sobrien	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
48990075Sobrien	int gotchildmax, gotenforce, gothid, gotslevel;
49090075Sobrien	int fi, jid, jsys, len, level;
49190075Sobrien	int childmax, slevel, vfslocked;
49290075Sobrien#if defined(INET) || defined(INET6)
49390075Sobrien	int ii, ij;
49490075Sobrien#endif
49590075Sobrien#ifdef INET
49690075Sobrien	int ip4s, redo_ip4;
49790075Sobrien#endif
49890075Sobrien#ifdef INET6
49990075Sobrien	int ip6s, redo_ip6;
50090075Sobrien#endif
50190075Sobrien	unsigned pr_flags, ch_flags;
50290075Sobrien	unsigned pr_allow, ch_allow, tallow;
50390075Sobrien	char numbuf[12];
50490075Sobrien
50590075Sobrien	error = priv_check(td, PRIV_JAIL_SET);
50690075Sobrien	if (!error && (flags & JAIL_ATTACH))
50790075Sobrien		error = priv_check(td, PRIV_JAIL_ATTACH);
50890075Sobrien	if (error)
50990075Sobrien		return (error);
51090075Sobrien	mypr = ppr = td->td_ucred->cr_prison;
51190075Sobrien	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
51290075Sobrien		return (EPERM);
51390075Sobrien	if (flags & ~JAIL_SET_MASK)
51490075Sobrien		return (EINVAL);
51590075Sobrien
51690075Sobrien	/*
51790075Sobrien	 * Check all the parameters before committing to anything.  Not all
51890075Sobrien	 * errors can be caught early, but we may as well try.  Also, this
51990075Sobrien	 * takes care of some expensive stuff (path lookup) before getting
52090075Sobrien	 * the allprison lock.
52190075Sobrien	 *
52290075Sobrien	 * XXX Jails are not filesystems, and jail parameters are not mount
52390075Sobrien	 *     options.  But it makes more sense to re-use the vfsopt code
52490075Sobrien	 *     than duplicate it under a different name.
52590075Sobrien	 */
52690075Sobrien	error = vfs_buildopts(optuio, &opts);
52790075Sobrien	if (error)
52890075Sobrien		return (error);
52990075Sobrien#ifdef INET
53090075Sobrien	ip4 = NULL;
53190075Sobrien#endif
53290075Sobrien#ifdef INET6
53390075Sobrien	ip6 = NULL;
53490075Sobrien#endif
53590075Sobrien
53690075Sobrien	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
53790075Sobrien	if (error == ENOENT)
53890075Sobrien		jid = 0;
53990075Sobrien	else if (error != 0)
54090075Sobrien		goto done_free;
54190075Sobrien
54290075Sobrien	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
54390075Sobrien	if (error == ENOENT)
54490075Sobrien		gotslevel = 0;
54590075Sobrien	else if (error != 0)
54690075Sobrien		goto done_free;
54790075Sobrien	else
54890075Sobrien		gotslevel = 1;
54990075Sobrien
55090075Sobrien	error =
55190075Sobrien	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
55290075Sobrien	if (error == ENOENT)
55390075Sobrien		gotchildmax = 0;
55490075Sobrien	else if (error != 0)
55590075Sobrien		goto done_free;
55690075Sobrien	else
55790075Sobrien		gotchildmax = 1;
55890075Sobrien
55990075Sobrien	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
56090075Sobrien	gotenforce = (error == 0);
56190075Sobrien	if (gotenforce) {
56290075Sobrien		if (enforce < 0 || enforce > 2)
56390075Sobrien			return (EINVAL);
56490075Sobrien	} else if (error != ENOENT)
56590075Sobrien		goto done_free;
56690075Sobrien
56790075Sobrien	pr_flags = ch_flags = 0;
56890075Sobrien	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
56990075Sobrien	    fi++) {
57090075Sobrien		if (pr_flag_names[fi] == NULL)
57190075Sobrien			continue;
57290075Sobrien		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
57390075Sobrien		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
57490075Sobrien	}
57590075Sobrien	ch_flags |= pr_flags;
57690075Sobrien	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
57790075Sobrien	    fi++) {
57890075Sobrien		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
57990075Sobrien		    sizeof(jsys));
58090075Sobrien		if (error == ENOENT)
58190075Sobrien			continue;
58290075Sobrien		if (error != 0)
58390075Sobrien			goto done_free;
58490075Sobrien		switch (jsys) {
58590075Sobrien		case JAIL_SYS_DISABLE:
58690075Sobrien			if (!pr_flag_jailsys[fi].disable) {
58790075Sobrien				error = EINVAL;
58890075Sobrien				goto done_free;
58990075Sobrien			}
59090075Sobrien			pr_flags |= pr_flag_jailsys[fi].disable;
59190075Sobrien			break;
59290075Sobrien		case JAIL_SYS_NEW:
59390075Sobrien			pr_flags |= pr_flag_jailsys[fi].new;
59490075Sobrien			break;
59590075Sobrien		case JAIL_SYS_INHERIT:
59690075Sobrien			break;
59790075Sobrien		default:
59890075Sobrien			error = EINVAL;
59990075Sobrien			goto done_free;
60090075Sobrien		}
60190075Sobrien		ch_flags |=
60290075Sobrien		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
60390075Sobrien	}
60490075Sobrien	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
60590075Sobrien	    && !(pr_flags & PR_PERSIST)) {
60690075Sobrien		error = EINVAL;
60790075Sobrien		vfs_opterror(opts, "new jail must persist or attach");
60890075Sobrien		goto done_errmsg;
60990075Sobrien	}
61090075Sobrien#ifdef VIMAGE
61190075Sobrien	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
61290075Sobrien		error = EINVAL;
61390075Sobrien		vfs_opterror(opts, "vnet cannot be changed after creation");
61490075Sobrien		goto done_errmsg;
61590075Sobrien	}
61690075Sobrien#endif
61790075Sobrien#ifdef INET
61890075Sobrien	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
61990075Sobrien		error = EINVAL;
62090075Sobrien		vfs_opterror(opts, "ip4 cannot be changed after creation");
62190075Sobrien		goto done_errmsg;
62290075Sobrien	}
62390075Sobrien#endif
62490075Sobrien#ifdef INET6
62590075Sobrien	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
62690075Sobrien		error = EINVAL;
62790075Sobrien		vfs_opterror(opts, "ip6 cannot be changed after creation");
62890075Sobrien		goto done_errmsg;
62990075Sobrien	}
63090075Sobrien#endif
63190075Sobrien
63290075Sobrien	pr_allow = ch_allow = 0;
63390075Sobrien	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
63490075Sobrien	    fi++) {
63590075Sobrien		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
63690075Sobrien		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
63790075Sobrien	}
63890075Sobrien	ch_allow |= pr_allow;
63990075Sobrien
64090075Sobrien	error = vfs_getopt(opts, "name", (void **)&name, &len);
64190075Sobrien	if (error == ENOENT)
64290075Sobrien		name = NULL;
64390075Sobrien	else if (error != 0)
64490075Sobrien		goto done_free;
64590075Sobrien	else {
64690075Sobrien		if (len == 0 || name[len - 1] != '\0') {
64790075Sobrien			error = EINVAL;
64890075Sobrien			goto done_free;
64990075Sobrien		}
65090075Sobrien		if (len > MAXHOSTNAMELEN) {
65190075Sobrien			error = ENAMETOOLONG;
65290075Sobrien			goto done_free;
65390075Sobrien		}
65490075Sobrien	}
65590075Sobrien
65690075Sobrien	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
65790075Sobrien	if (error == ENOENT)
65890075Sobrien		host = NULL;
65990075Sobrien	else if (error != 0)
66090075Sobrien		goto done_free;
66190075Sobrien	else {
66290075Sobrien		ch_flags |= PR_HOST;
66390075Sobrien		pr_flags |= PR_HOST;
66490075Sobrien		if (len == 0 || host[len - 1] != '\0') {
66590075Sobrien			error = EINVAL;
66690075Sobrien			goto done_free;
66790075Sobrien		}
66890075Sobrien		if (len > MAXHOSTNAMELEN) {
66990075Sobrien			error = ENAMETOOLONG;
67090075Sobrien			goto done_free;
67190075Sobrien		}
67290075Sobrien	}
67390075Sobrien
67490075Sobrien	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
67590075Sobrien	if (error == ENOENT)
67690075Sobrien		domain = NULL;
67790075Sobrien	else if (error != 0)
67890075Sobrien		goto done_free;
67990075Sobrien	else {
68090075Sobrien		ch_flags |= PR_HOST;
68190075Sobrien		pr_flags |= PR_HOST;
68290075Sobrien		if (len == 0 || domain[len - 1] != '\0') {
68390075Sobrien			error = EINVAL;
68490075Sobrien			goto done_free;
68590075Sobrien		}
68690075Sobrien		if (len > MAXHOSTNAMELEN) {
68790075Sobrien			error = ENAMETOOLONG;
68890075Sobrien			goto done_free;
68990075Sobrien		}
69090075Sobrien	}
69190075Sobrien
69290075Sobrien	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
69390075Sobrien	if (error == ENOENT)
69490075Sobrien		uuid = NULL;
69590075Sobrien	else if (error != 0)
69690075Sobrien		goto done_free;
69790075Sobrien	else {
69890075Sobrien		ch_flags |= PR_HOST;
69990075Sobrien		pr_flags |= PR_HOST;
70090075Sobrien		if (len == 0 || uuid[len - 1] != '\0') {
70190075Sobrien			error = EINVAL;
70290075Sobrien			goto done_free;
70390075Sobrien		}
70490075Sobrien		if (len > HOSTUUIDLEN) {
70590075Sobrien			error = ENAMETOOLONG;
70690075Sobrien			goto done_free;
70790075Sobrien		}
70890075Sobrien	}
70990075Sobrien
71090075Sobrien#ifdef COMPAT_IA32
71190075Sobrien	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
71290075Sobrien		uint32_t hid32;
71390075Sobrien
71490075Sobrien		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
71590075Sobrien		hid = hid32;
71690075Sobrien	} else
71790075Sobrien#endif
71890075Sobrien		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
71990075Sobrien	if (error == ENOENT)
72090075Sobrien		gothid = 0;
72190075Sobrien	else if (error != 0)
72290075Sobrien		goto done_free;
72390075Sobrien	else {
72490075Sobrien		gothid = 1;
72590075Sobrien		ch_flags |= PR_HOST;
72690075Sobrien		pr_flags |= PR_HOST;
72790075Sobrien	}
72890075Sobrien
72990075Sobrien#ifdef INET
73090075Sobrien	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
73190075Sobrien	if (error == ENOENT)
73290075Sobrien		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
73390075Sobrien	else if (error != 0)
73490075Sobrien		goto done_free;
73590075Sobrien	else if (ip4s & (sizeof(*ip4) - 1)) {
73690075Sobrien		error = EINVAL;
73790075Sobrien		goto done_free;
73890075Sobrien	} else {
73990075Sobrien		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
74090075Sobrien		if (ip4s == 0)
74190075Sobrien			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
74290075Sobrien		else {
74390075Sobrien			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
74490075Sobrien			ip4s /= sizeof(*ip4);
74590075Sobrien			if (ip4s > jail_max_af_ips) {
74690075Sobrien				error = EINVAL;
74790075Sobrien				vfs_opterror(opts, "too many IPv4 addresses");
74890075Sobrien				goto done_errmsg;
74990075Sobrien			}
75090075Sobrien			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
75190075Sobrien			bcopy(op, ip4, ip4s * sizeof(*ip4));
75290075Sobrien			/*
75390075Sobrien			 * IP addresses are all sorted but ip[0] to preserve
75490075Sobrien			 * the primary IP address as given from userland.
75590075Sobrien			 * This special IP is used for unbound outgoing
75690075Sobrien			 * connections as well for "loopback" traffic in case
75790075Sobrien			 * source address selection cannot find any more fitting
75890075Sobrien			 * address to connect from.
75990075Sobrien			 */
76090075Sobrien			if (ip4s > 1)
76190075Sobrien				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
76290075Sobrien			/*
76390075Sobrien			 * Check for duplicate addresses and do some simple
76490075Sobrien			 * zero and broadcast checks. If users give other bogus
76590075Sobrien			 * addresses it is their problem.
76690075Sobrien			 *
76790075Sobrien			 * We do not have to care about byte order for these
76890075Sobrien			 * checks so we will do them in NBO.
76990075Sobrien			 */
77090075Sobrien			for (ii = 0; ii < ip4s; ii++) {
77190075Sobrien				if (ip4[ii].s_addr == INADDR_ANY ||
77290075Sobrien				    ip4[ii].s_addr == INADDR_BROADCAST) {
77390075Sobrien					error = EINVAL;
77490075Sobrien					goto done_free;
77590075Sobrien				}
77690075Sobrien				if ((ii+1) < ip4s &&
77790075Sobrien				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
77890075Sobrien				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
77990075Sobrien					error = EINVAL;
78090075Sobrien					goto done_free;
78190075Sobrien				}
78290075Sobrien			}
78390075Sobrien		}
78490075Sobrien	}
78590075Sobrien#endif
78690075Sobrien
78790075Sobrien#ifdef INET6
78890075Sobrien	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
78990075Sobrien	if (error == ENOENT)
79090075Sobrien		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
79190075Sobrien	else if (error != 0)
79290075Sobrien		goto done_free;
79390075Sobrien	else if (ip6s & (sizeof(*ip6) - 1)) {
79490075Sobrien		error = EINVAL;
79590075Sobrien		goto done_free;
79690075Sobrien	} else {
79790075Sobrien		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
79890075Sobrien		if (ip6s == 0)
79990075Sobrien			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
80090075Sobrien		else {
80190075Sobrien			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
80290075Sobrien			ip6s /= sizeof(*ip6);
80390075Sobrien			if (ip6s > jail_max_af_ips) {
80490075Sobrien				error = EINVAL;
80590075Sobrien				vfs_opterror(opts, "too many IPv6 addresses");
80690075Sobrien				goto done_errmsg;
80790075Sobrien			}
80890075Sobrien			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
80990075Sobrien			bcopy(op, ip6, ip6s * sizeof(*ip6));
81090075Sobrien			if (ip6s > 1)
81190075Sobrien				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
81290075Sobrien			for (ii = 0; ii < ip6s; ii++) {
81390075Sobrien				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
81490075Sobrien					error = EINVAL;
81590075Sobrien					goto done_free;
81690075Sobrien				}
81790075Sobrien				if ((ii+1) < ip6s &&
81890075Sobrien				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
81990075Sobrien				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
82090075Sobrien				{
82190075Sobrien					error = EINVAL;
82290075Sobrien					goto done_free;
82390075Sobrien				}
82490075Sobrien			}
82590075Sobrien		}
82690075Sobrien	}
82790075Sobrien#endif
82890075Sobrien
82990075Sobrien#if defined(VIMAGE) && (defined(INET) || defined(INET6))
83090075Sobrien	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
83190075Sobrien		error = EINVAL;
83290075Sobrien		vfs_opterror(opts,
83390075Sobrien		    "vnet jails cannot have IP address restrictions");
83490075Sobrien		goto done_errmsg;
83590075Sobrien	}
83690075Sobrien#endif
83790075Sobrien
83890075Sobrien	root = NULL;
83990075Sobrien	error = vfs_getopt(opts, "path", (void **)&path, &len);
84090075Sobrien	if (error == ENOENT)
84190075Sobrien		path = NULL;
84290075Sobrien	else if (error != 0)
84390075Sobrien		goto done_free;
84490075Sobrien	else {
84590075Sobrien		if (flags & JAIL_UPDATE) {
84690075Sobrien			error = EINVAL;
84790075Sobrien			vfs_opterror(opts,
84890075Sobrien			    "path cannot be changed after creation");
84990075Sobrien			goto done_errmsg;
85090075Sobrien		}
85190075Sobrien		if (len == 0 || path[len - 1] != '\0') {
85290075Sobrien			error = EINVAL;
85390075Sobrien			goto done_free;
85490075Sobrien		}
85590075Sobrien		if (len < 2 || (len == 2 && path[0] == '/'))
85690075Sobrien			path = NULL;
85790075Sobrien		else {
85890075Sobrien			/* Leave room for a real-root full pathname. */
85990075Sobrien			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
86090075Sobrien			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
86190075Sobrien				error = ENAMETOOLONG;
86290075Sobrien				goto done_free;
86390075Sobrien			}
86490075Sobrien			NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
86590075Sobrien			    path, td);
86690075Sobrien			error = namei(&nd);
86790075Sobrien			if (error)
86890075Sobrien				goto done_free;
86990075Sobrien			vfslocked = NDHASGIANT(&nd);
87090075Sobrien			root = nd.ni_vp;
87190075Sobrien			NDFREE(&nd, NDF_ONLY_PNBUF);
87290075Sobrien			if (root->v_type != VDIR) {
87390075Sobrien				error = ENOTDIR;
87490075Sobrien				vrele(root);
87590075Sobrien				VFS_UNLOCK_GIANT(vfslocked);
87690075Sobrien				goto done_free;
87790075Sobrien			}
87890075Sobrien			VFS_UNLOCK_GIANT(vfslocked);
87990075Sobrien		}
88090075Sobrien	}
88190075Sobrien
88290075Sobrien	/*
88390075Sobrien	 * Grab the allprison lock before letting modules check their
88490075Sobrien	 * parameters.  Once we have it, do not let go so we'll have a
88590075Sobrien	 * consistent view of the OSD list.
88690075Sobrien	 */
88790075Sobrien	sx_xlock(&allprison_lock);
88890075Sobrien	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
88990075Sobrien	if (error)
89090075Sobrien		goto done_unlock_list;
89190075Sobrien
89290075Sobrien	/* By now, all parameters should have been noted. */
89390075Sobrien	TAILQ_FOREACH(opt, opts, link) {
89490075Sobrien		if (!opt->seen && strcmp(opt->name, "errmsg")) {
89590075Sobrien			error = EINVAL;
89690075Sobrien			vfs_opterror(opts, "unknown parameter: %s", opt->name);
89790075Sobrien			goto done_unlock_list;
89890075Sobrien		}
89990075Sobrien	}
90090075Sobrien
90190075Sobrien	/*
90290075Sobrien	 * See if we are creating a new record or updating an existing one.
90390075Sobrien	 * This abuses the file error codes ENOENT and EEXIST.
90490075Sobrien	 */
90590075Sobrien	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
90690075Sobrien	if (!cuflags) {
90790075Sobrien		error = EINVAL;
90890075Sobrien		vfs_opterror(opts, "no valid operation (create or update)");
90990075Sobrien		goto done_unlock_list;
91090075Sobrien	}
91190075Sobrien	pr = NULL;
91290075Sobrien	namelc = NULL;
91390075Sobrien	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
91490075Sobrien		namelc = strrchr(name, '.');
91590075Sobrien		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
91690075Sobrien		if (*p != '\0')
91790075Sobrien			jid = 0;
91890075Sobrien	}
91990075Sobrien	if (jid != 0) {
92090075Sobrien		/*
92190075Sobrien		 * See if a requested jid already exists.  There is an
92290075Sobrien		 * information leak here if the jid exists but is not within
92390075Sobrien		 * the caller's jail hierarchy.  Jail creators will get EEXIST
92490075Sobrien		 * even though they cannot see the jail, and CREATE | UPDATE
92590075Sobrien		 * will return ENOENT which is not normally a valid error.
92690075Sobrien		 */
92790075Sobrien		if (jid < 0) {
92890075Sobrien			error = EINVAL;
92990075Sobrien			vfs_opterror(opts, "negative jid");
93090075Sobrien			goto done_unlock_list;
93190075Sobrien		}
93290075Sobrien		pr = prison_find(jid);
93390075Sobrien		if (pr != NULL) {
93490075Sobrien			ppr = pr->pr_parent;
93590075Sobrien			/* Create: jid must not exist. */
93690075Sobrien			if (cuflags == JAIL_CREATE) {
93790075Sobrien				mtx_unlock(&pr->pr_mtx);
93890075Sobrien				error = EEXIST;
93990075Sobrien				vfs_opterror(opts, "jail %d already exists",
94090075Sobrien				    jid);
94190075Sobrien				goto done_unlock_list;
94290075Sobrien			}
94390075Sobrien			if (!prison_ischild(mypr, pr)) {
94490075Sobrien				mtx_unlock(&pr->pr_mtx);
94590075Sobrien				pr = NULL;
94690075Sobrien			} else if (pr->pr_uref == 0) {
94790075Sobrien				if (!(flags & JAIL_DYING)) {
94890075Sobrien					mtx_unlock(&pr->pr_mtx);
94990075Sobrien					error = ENOENT;
95090075Sobrien					vfs_opterror(opts, "jail %d is dying",
95190075Sobrien					    jid);
95290075Sobrien					goto done_unlock_list;
95390075Sobrien				} else if ((flags & JAIL_ATTACH) ||
95490075Sobrien				    (pr_flags & PR_PERSIST)) {
95590075Sobrien					/*
95690075Sobrien					 * A dying jail might be resurrected
95790075Sobrien					 * (via attach or persist), but first
95890075Sobrien					 * it must determine if another jail
95990075Sobrien					 * has claimed its name.  Accomplish
96090075Sobrien					 * this by implicitly re-setting the
96190075Sobrien					 * name.
96290075Sobrien					 */
96390075Sobrien					if (name == NULL)
96490075Sobrien						name = prison_name(mypr, pr);
96590075Sobrien				}
96690075Sobrien			}
96790075Sobrien		}
96890075Sobrien		if (pr == NULL) {
96990075Sobrien			/* Update: jid must exist. */
97090075Sobrien			if (cuflags == JAIL_UPDATE) {
97190075Sobrien				error = ENOENT;
97290075Sobrien				vfs_opterror(opts, "jail %d not found", jid);
97390075Sobrien				goto done_unlock_list;
97490075Sobrien			}
97590075Sobrien		}
97690075Sobrien	}
97790075Sobrien	/*
97890075Sobrien	 * If the caller provided a name, look for a jail by that name.
97990075Sobrien	 * This has different semantics for creates and updates keyed by jid
98090075Sobrien	 * (where the name must not already exist in a different jail),
98190075Sobrien	 * and updates keyed by the name itself (where the name must exist
98290075Sobrien	 * because that is the jail being updated).
98390075Sobrien	 */
98490075Sobrien	if (name != NULL) {
98590075Sobrien		namelc = strrchr(name, '.');
98690075Sobrien		if (namelc == NULL)
98790075Sobrien			namelc = name;
98890075Sobrien		else {
98990075Sobrien			/*
99090075Sobrien			 * This is a hierarchical name.  Split it into the
99190075Sobrien			 * parent and child names, and make sure the parent
99290075Sobrien			 * exists or matches an already found jail.
99390075Sobrien			 */
99490075Sobrien			*namelc = '\0';
99590075Sobrien			if (pr != NULL) {
99690075Sobrien				if (strncmp(name, ppr->pr_name, namelc - name)
99790075Sobrien				    || ppr->pr_name[namelc - name] != '\0') {
99890075Sobrien					mtx_unlock(&pr->pr_mtx);
99990075Sobrien					error = EINVAL;
100090075Sobrien					vfs_opterror(opts,
100190075Sobrien					    "cannot change jail's parent");
100290075Sobrien					goto done_unlock_list;
100390075Sobrien				}
100490075Sobrien			} else {
100590075Sobrien				ppr = prison_find_name(mypr, name);
100690075Sobrien				if (ppr == NULL) {
100790075Sobrien					error = ENOENT;
100890075Sobrien					vfs_opterror(opts,
100990075Sobrien					    "jail \"%s\" not found", name);
101090075Sobrien					goto done_unlock_list;
101190075Sobrien				}
101290075Sobrien				mtx_unlock(&ppr->pr_mtx);
101390075Sobrien			}
101490075Sobrien			name = ++namelc;
101590075Sobrien		}
101690075Sobrien		if (name[0] != '\0') {
101790075Sobrien			namelen =
101890075Sobrien			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
101990075Sobrien name_again:
102090075Sobrien			deadpr = NULL;
102190075Sobrien			FOREACH_PRISON_CHILD(ppr, tpr) {
102290075Sobrien				if (tpr != pr && tpr->pr_ref > 0 &&
102390075Sobrien				    !strcmp(tpr->pr_name + namelen, name)) {
102490075Sobrien					if (pr == NULL &&
102590075Sobrien					    cuflags != JAIL_CREATE) {
102690075Sobrien						mtx_lock(&tpr->pr_mtx);
102790075Sobrien						if (tpr->pr_ref > 0) {
102890075Sobrien							/*
102990075Sobrien							 * Use this jail
103090075Sobrien							 * for updates.
103190075Sobrien							 */
103290075Sobrien							if (tpr->pr_uref > 0) {
103390075Sobrien								pr = tpr;
103490075Sobrien								break;
103590075Sobrien							}
103690075Sobrien							deadpr = tpr;
103790075Sobrien						}
103890075Sobrien						mtx_unlock(&tpr->pr_mtx);
103990075Sobrien					} else if (tpr->pr_uref > 0) {
104090075Sobrien						/*
104190075Sobrien						 * Create, or update(jid):
104290075Sobrien						 * name must not exist in an
104390075Sobrien						 * active sibling jail.
104490075Sobrien						 */
104590075Sobrien						error = EEXIST;
104690075Sobrien						if (pr != NULL)
104790075Sobrien							mtx_unlock(&pr->pr_mtx);
104890075Sobrien						vfs_opterror(opts,
104990075Sobrien						   "jail \"%s\" already exists",
105090075Sobrien						   name);
105190075Sobrien						goto done_unlock_list;
105290075Sobrien					}
105390075Sobrien				}
105490075Sobrien			}
105590075Sobrien			/* If no active jail is found, use a dying one. */
105690075Sobrien			if (deadpr != NULL && pr == NULL) {
105790075Sobrien				if (flags & JAIL_DYING) {
105890075Sobrien					mtx_lock(&deadpr->pr_mtx);
105990075Sobrien					if (deadpr->pr_ref == 0) {
106090075Sobrien						mtx_unlock(&deadpr->pr_mtx);
106190075Sobrien						goto name_again;
106290075Sobrien					}
106390075Sobrien					pr = deadpr;
106490075Sobrien				} else if (cuflags == JAIL_UPDATE) {
106590075Sobrien					error = ENOENT;
106690075Sobrien					vfs_opterror(opts,
106790075Sobrien					    "jail \"%s\" is dying", name);
106890075Sobrien					goto done_unlock_list;
106990075Sobrien				}
107090075Sobrien			}
107190075Sobrien			/* Update: name must exist if no jid. */
107290075Sobrien			else if (cuflags == JAIL_UPDATE && pr == NULL) {
107390075Sobrien				error = ENOENT;
107490075Sobrien				vfs_opterror(opts, "jail \"%s\" not found",
107590075Sobrien				    name);
107690075Sobrien				goto done_unlock_list;
107790075Sobrien			}
107890075Sobrien		}
107990075Sobrien	}
108090075Sobrien	/* Update: must provide a jid or name. */
108190075Sobrien	else if (cuflags == JAIL_UPDATE && pr == NULL) {
108290075Sobrien		error = ENOENT;
108390075Sobrien		vfs_opterror(opts, "update specified no jail");
108490075Sobrien		goto done_unlock_list;
108590075Sobrien	}
108690075Sobrien
108790075Sobrien	/* If there's no prison to update, create a new one and link it in. */
108890075Sobrien	if (pr == NULL) {
108990075Sobrien		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
109090075Sobrien			if (tpr->pr_childcount >= tpr->pr_childmax) {
109190075Sobrien				error = EPERM;
109290075Sobrien				vfs_opterror(opts, "prison limit exceeded");
109390075Sobrien				goto done_unlock_list;
109490075Sobrien			}
109590075Sobrien		created = 1;
109690075Sobrien		mtx_lock(&ppr->pr_mtx);
109790075Sobrien		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
109890075Sobrien			mtx_unlock(&ppr->pr_mtx);
109990075Sobrien			error = ENOENT;
110090075Sobrien			vfs_opterror(opts, "parent jail went away!");
110190075Sobrien			goto done_unlock_list;
110290075Sobrien		}
110390075Sobrien		ppr->pr_ref++;
110490075Sobrien		ppr->pr_uref++;
110590075Sobrien		mtx_unlock(&ppr->pr_mtx);
110690075Sobrien		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
110790075Sobrien		if (jid == 0) {
110890075Sobrien			/* Find the next free jid. */
110990075Sobrien			jid = lastprid + 1;
111090075Sobrien findnext:
111190075Sobrien			if (jid == JAIL_MAX)
111290075Sobrien				jid = 1;
111390075Sobrien			TAILQ_FOREACH(tpr, &allprison, pr_list) {
111490075Sobrien				if (tpr->pr_id < jid)
111590075Sobrien					continue;
111690075Sobrien				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
111790075Sobrien					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
111890075Sobrien					break;
111990075Sobrien				}
112090075Sobrien				if (jid == lastprid) {
112190075Sobrien					error = EAGAIN;
112290075Sobrien					vfs_opterror(opts,
112390075Sobrien					    "no available jail IDs");
112490075Sobrien					free(pr, M_PRISON);
112590075Sobrien					prison_deref(ppr, PD_DEREF |
112690075Sobrien					    PD_DEUREF | PD_LIST_XLOCKED);
112790075Sobrien					goto done_releroot;
112890075Sobrien				}
112990075Sobrien				jid++;
113090075Sobrien				goto findnext;
113190075Sobrien			}
113290075Sobrien			lastprid = jid;
113390075Sobrien		} else {
113490075Sobrien			/*
113590075Sobrien			 * The jail already has a jid (that did not yet exist),
113690075Sobrien			 * so just find where to insert it.
113790075Sobrien			 */
113890075Sobrien			TAILQ_FOREACH(tpr, &allprison, pr_list)
113990075Sobrien				if (tpr->pr_id >= jid) {
114090075Sobrien					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
114190075Sobrien					break;
114290075Sobrien				}
114390075Sobrien		}
114490075Sobrien		if (tpr == NULL)
114590075Sobrien			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
114690075Sobrien		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
114790075Sobrien		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
114890075Sobrien			tpr->pr_childcount++;
114990075Sobrien
115090075Sobrien		pr->pr_parent = ppr;
115190075Sobrien		pr->pr_id = jid;
115290075Sobrien
115390075Sobrien		/* Set some default values, and inherit some from the parent. */
115490075Sobrien		if (name == NULL)
115590075Sobrien			name = "";
115690075Sobrien		if (path == NULL) {
115790075Sobrien			path = "/";
115890075Sobrien			root = mypr->pr_root;
115990075Sobrien			vref(root);
116090075Sobrien		}
116190075Sobrien		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
116290075Sobrien		pr->pr_flags |= PR_HOST;
116390075Sobrien#if defined(INET) || defined(INET6)
116490075Sobrien#ifdef VIMAGE
116590075Sobrien		if (!(pr_flags & PR_VNET))
116690075Sobrien#endif
116790075Sobrien		{
116890075Sobrien#ifdef INET
116990075Sobrien			if (!(ch_flags & PR_IP4_USER))
117090075Sobrien				pr->pr_flags |=
117190075Sobrien				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
117290075Sobrien			else if (!(pr_flags & PR_IP4_USER)) {
117390075Sobrien				pr->pr_flags |= ppr->pr_flags & PR_IP4;
117490075Sobrien				if (ppr->pr_ip4 != NULL) {
117590075Sobrien					pr->pr_ip4s = ppr->pr_ip4s;
117690075Sobrien					pr->pr_ip4 = malloc(pr->pr_ip4s *
117790075Sobrien					    sizeof(struct in_addr), M_PRISON,
117890075Sobrien					    M_WAITOK);
117990075Sobrien					bcopy(ppr->pr_ip4, pr->pr_ip4,
118090075Sobrien					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
118190075Sobrien				}
118290075Sobrien			}
118390075Sobrien#endif
118490075Sobrien#ifdef INET6
118590075Sobrien			if (!(ch_flags & PR_IP6_USER))
118690075Sobrien				pr->pr_flags |=
118790075Sobrien				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
118890075Sobrien			else if (!(pr_flags & PR_IP6_USER)) {
118990075Sobrien				pr->pr_flags |= ppr->pr_flags & PR_IP6;
119090075Sobrien				if (ppr->pr_ip6 != NULL) {
119190075Sobrien					pr->pr_ip6s = ppr->pr_ip6s;
119290075Sobrien					pr->pr_ip6 = malloc(pr->pr_ip6s *
119390075Sobrien					    sizeof(struct in6_addr), M_PRISON,
119490075Sobrien					    M_WAITOK);
119590075Sobrien					bcopy(ppr->pr_ip6, pr->pr_ip6,
119690075Sobrien					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
119790075Sobrien				}
119890075Sobrien			}
119990075Sobrien#endif
120090075Sobrien		}
120190075Sobrien#endif
120290075Sobrien		pr->pr_securelevel = ppr->pr_securelevel;
120390075Sobrien		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
120490075Sobrien		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
120590075Sobrien
120690075Sobrien		LIST_INIT(&pr->pr_children);
120790075Sobrien		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
120890075Sobrien
120990075Sobrien#ifdef VIMAGE
121090075Sobrien		/* Allocate a new vnet if specified. */
121190075Sobrien		pr->pr_vnet = (pr_flags & PR_VNET)
121290075Sobrien		    ? vnet_alloc() : ppr->pr_vnet;
121390075Sobrien#endif
121490075Sobrien		/*
121590075Sobrien		 * Allocate a dedicated cpuset for each jail.
121690075Sobrien		 * Unlike other initial settings, this may return an erorr.
121790075Sobrien		 */
121890075Sobrien		error = cpuset_create_root(ppr, &pr->pr_cpuset);
121990075Sobrien		if (error) {
122090075Sobrien			prison_deref(pr, PD_LIST_XLOCKED);
122190075Sobrien			goto done_releroot;
122290075Sobrien		}
122390075Sobrien
122490075Sobrien		mtx_lock(&pr->pr_mtx);
122590075Sobrien		/*
122690075Sobrien		 * New prisons do not yet have a reference, because we do not
122790075Sobrien		 * want other to see the incomplete prison once the
122890075Sobrien		 * allprison_lock is downgraded.
122990075Sobrien		 */
123090075Sobrien	} else {
123190075Sobrien		created = 0;
123290075Sobrien		/*
123390075Sobrien		 * Grab a reference for existing prisons, to ensure they
123490075Sobrien		 * continue to exist for the duration of the call.
123590075Sobrien		 */
123690075Sobrien		pr->pr_ref++;
123790075Sobrien#if defined(VIMAGE) && (defined(INET) || defined(INET6))
123890075Sobrien		if ((pr->pr_flags & PR_VNET) &&
123990075Sobrien		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
124090075Sobrien			error = EINVAL;
124190075Sobrien			vfs_opterror(opts,
124290075Sobrien			    "vnet jails cannot have IP address restrictions");
124390075Sobrien			goto done_deref_locked;
124490075Sobrien		}
124590075Sobrien#endif
124690075Sobrien#ifdef INET
124790075Sobrien		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
124890075Sobrien			error = EINVAL;
124990075Sobrien			vfs_opterror(opts,
125090075Sobrien			    "ip4 cannot be changed after creation");
125190075Sobrien			goto done_deref_locked;
125290075Sobrien		}
125390075Sobrien#endif
125490075Sobrien#ifdef INET6
125590075Sobrien		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
125690075Sobrien			error = EINVAL;
125790075Sobrien			vfs_opterror(opts,
125890075Sobrien			    "ip6 cannot be changed after creation");
125990075Sobrien			goto done_deref_locked;
126090075Sobrien		}
126190075Sobrien#endif
126290075Sobrien	}
126390075Sobrien
126490075Sobrien	/* Do final error checking before setting anything. */
126590075Sobrien	if (gotslevel) {
126690075Sobrien		if (slevel < ppr->pr_securelevel) {
126790075Sobrien			error = EPERM;
126890075Sobrien			goto done_deref_locked;
126990075Sobrien		}
127090075Sobrien	}
127190075Sobrien	if (gotchildmax) {
127290075Sobrien		if (childmax >= ppr->pr_childmax) {
127390075Sobrien			error = EPERM;
127490075Sobrien			goto done_deref_locked;
127590075Sobrien		}
127690075Sobrien	}
127790075Sobrien	if (gotenforce) {
127890075Sobrien		if (enforce < ppr->pr_enforce_statfs) {
127990075Sobrien			error = EPERM;
128090075Sobrien			goto done_deref_locked;
128190075Sobrien		}
128290075Sobrien	}
128390075Sobrien#ifdef INET
128490075Sobrien	if (ip4s > 0) {
128590075Sobrien		if (ppr->pr_flags & PR_IP4) {
128690075Sobrien			/*
128790075Sobrien			 * Make sure the new set of IP addresses is a
128890075Sobrien			 * subset of the parent's list.  Don't worry
128990075Sobrien			 * about the parent being unlocked, as any
129090075Sobrien			 * setting is done with allprison_lock held.
129190075Sobrien			 */
129290075Sobrien			for (ij = 0; ij < ppr->pr_ip4s; ij++)
129390075Sobrien				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
129490075Sobrien					break;
129590075Sobrien			if (ij == ppr->pr_ip4s) {
129690075Sobrien				error = EPERM;
129790075Sobrien				goto done_deref_locked;
129890075Sobrien			}
129990075Sobrien			if (ip4s > 1) {
130090075Sobrien				for (ii = ij = 1; ii < ip4s; ii++) {
130190075Sobrien					if (ip4[ii].s_addr ==
130290075Sobrien					    ppr->pr_ip4[0].s_addr)
130390075Sobrien						continue;
130490075Sobrien					for (; ij < ppr->pr_ip4s; ij++)
130590075Sobrien						if (ip4[ii].s_addr ==
130690075Sobrien						    ppr->pr_ip4[ij].s_addr)
130790075Sobrien							break;
130890075Sobrien					if (ij == ppr->pr_ip4s)
130990075Sobrien						break;
131090075Sobrien				}
131190075Sobrien				if (ij == ppr->pr_ip4s) {
131290075Sobrien					error = EPERM;
131390075Sobrien					goto done_deref_locked;
131490075Sobrien				}
131590075Sobrien			}
131690075Sobrien		}
131790075Sobrien		/*
131890075Sobrien		 * Check for conflicting IP addresses.  We permit them
131990075Sobrien		 * if there is no more than one IP on each jail.  If
132090075Sobrien		 * there is a duplicate on a jail with more than one
132190075Sobrien		 * IP stop checking and return error.
132290075Sobrien		 */
132390075Sobrien		tppr = ppr;
132490075Sobrien#ifdef VIMAGE
132590075Sobrien		for (; tppr != &prison0; tppr = tppr->pr_parent)
132690075Sobrien			if (tppr->pr_flags & PR_VNET)
132790075Sobrien				break;
132890075Sobrien#endif
132990075Sobrien		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
133090075Sobrien			if (tpr == pr ||
133190075Sobrien#ifdef VIMAGE
133290075Sobrien			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
133390075Sobrien#endif
133490075Sobrien			    tpr->pr_uref == 0) {
133590075Sobrien				descend = 0;
133690075Sobrien				continue;
133790075Sobrien			}
133890075Sobrien			if (!(tpr->pr_flags & PR_IP4_USER))
133990075Sobrien				continue;
134090075Sobrien			descend = 0;
134190075Sobrien			if (tpr->pr_ip4 == NULL ||
134290075Sobrien			    (ip4s == 1 && tpr->pr_ip4s == 1))
134390075Sobrien				continue;
134490075Sobrien			for (ii = 0; ii < ip4s; ii++) {
134590075Sobrien				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
134690075Sobrien					error = EADDRINUSE;
134790075Sobrien					vfs_opterror(opts,
134890075Sobrien					    "IPv4 addresses clash");
134990075Sobrien					goto done_deref_locked;
135090075Sobrien				}
135190075Sobrien			}
135290075Sobrien		}
135390075Sobrien	}
135490075Sobrien#endif
135590075Sobrien#ifdef INET6
135690075Sobrien	if (ip6s > 0) {
135790075Sobrien		if (ppr->pr_flags & PR_IP6) {
135890075Sobrien			/*
135990075Sobrien			 * Make sure the new set of IP addresses is a
136090075Sobrien			 * subset of the parent's list.
136190075Sobrien			 */
136290075Sobrien			for (ij = 0; ij < ppr->pr_ip6s; ij++)
136390075Sobrien				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
136490075Sobrien				    &ppr->pr_ip6[ij]))
136590075Sobrien					break;
136690075Sobrien			if (ij == ppr->pr_ip6s) {
136790075Sobrien				error = EPERM;
136890075Sobrien				goto done_deref_locked;
136990075Sobrien			}
137090075Sobrien			if (ip6s > 1) {
137190075Sobrien				for (ii = ij = 1; ii < ip6s; ii++) {
137290075Sobrien					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
137390075Sobrien					     &ppr->pr_ip6[0]))
137490075Sobrien						continue;
137590075Sobrien					for (; ij < ppr->pr_ip6s; ij++)
137690075Sobrien						if (IN6_ARE_ADDR_EQUAL(
137790075Sobrien						    &ip6[ii], &ppr->pr_ip6[ij]))
137890075Sobrien							break;
137990075Sobrien					if (ij == ppr->pr_ip6s)
138090075Sobrien						break;
138190075Sobrien				}
138290075Sobrien				if (ij == ppr->pr_ip6s) {
138390075Sobrien					error = EPERM;
138490075Sobrien					goto done_deref_locked;
138590075Sobrien				}
138690075Sobrien			}
138790075Sobrien		}
138890075Sobrien		/* Check for conflicting IP addresses. */
138990075Sobrien		tppr = ppr;
139090075Sobrien#ifdef VIMAGE
139190075Sobrien		for (; tppr != &prison0; tppr = tppr->pr_parent)
139290075Sobrien			if (tppr->pr_flags & PR_VNET)
139390075Sobrien				break;
139490075Sobrien#endif
139590075Sobrien		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
139690075Sobrien			if (tpr == pr ||
139790075Sobrien#ifdef VIMAGE
139890075Sobrien			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
139990075Sobrien#endif
140090075Sobrien			    tpr->pr_uref == 0) {
140190075Sobrien				descend = 0;
140290075Sobrien				continue;
140390075Sobrien			}
140490075Sobrien			if (!(tpr->pr_flags & PR_IP6_USER))
140590075Sobrien				continue;
140690075Sobrien			descend = 0;
140790075Sobrien			if (tpr->pr_ip6 == NULL ||
140890075Sobrien			    (ip6s == 1 && tpr->pr_ip6s == 1))
140990075Sobrien				continue;
141090075Sobrien			for (ii = 0; ii < ip6s; ii++) {
141190075Sobrien				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
141290075Sobrien					error = EADDRINUSE;
141390075Sobrien					vfs_opterror(opts,
141490075Sobrien					    "IPv6 addresses clash");
141590075Sobrien					goto done_deref_locked;
141690075Sobrien				}
141790075Sobrien			}
141890075Sobrien		}
141990075Sobrien	}
142090075Sobrien#endif
142190075Sobrien	onamelen = namelen = 0;
142290075Sobrien	if (name != NULL) {
142390075Sobrien		/* Give a default name of the jid. */
142490075Sobrien		if (name[0] == '\0')
142590075Sobrien			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
142690075Sobrien		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
142790075Sobrien		    *p == '\0')) {
142890075Sobrien			error = EINVAL;
142990075Sobrien			vfs_opterror(opts,
143090075Sobrien			    "name cannot be numeric (unless it is the jid)");
143190075Sobrien			goto done_deref_locked;
143290075Sobrien		}
143390075Sobrien		/*
143490075Sobrien		 * Make sure the name isn't too long for the prison or its
143590075Sobrien		 * children.
143690075Sobrien		 */
143790075Sobrien		onamelen = strlen(pr->pr_name);
143890075Sobrien		namelen = strlen(name);
143990075Sobrien		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
144090075Sobrien			error = ENAMETOOLONG;
144190075Sobrien			goto done_deref_locked;
144290075Sobrien		}
144390075Sobrien		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
144490075Sobrien			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
144590075Sobrien			    sizeof(pr->pr_name)) {
144690075Sobrien				error = ENAMETOOLONG;
144790075Sobrien				goto done_deref_locked;
144890075Sobrien			}
144990075Sobrien		}
145090075Sobrien	}
145190075Sobrien	if (pr_allow & ~ppr->pr_allow) {
145290075Sobrien		error = EPERM;
145390075Sobrien		goto done_deref_locked;
145490075Sobrien	}
145590075Sobrien
145690075Sobrien	/* Set the parameters of the prison. */
145790075Sobrien#ifdef INET
145890075Sobrien	redo_ip4 = 0;
145990075Sobrien	if (pr_flags & PR_IP4_USER) {
146090075Sobrien		pr->pr_flags |= PR_IP4;
146190075Sobrien		free(pr->pr_ip4, M_PRISON);
146290075Sobrien		pr->pr_ip4s = ip4s;
146390075Sobrien		pr->pr_ip4 = ip4;
146490075Sobrien		ip4 = NULL;
146590075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
146690075Sobrien#ifdef VIMAGE
146790075Sobrien			if (tpr->pr_flags & PR_VNET) {
146890075Sobrien				descend = 0;
146990075Sobrien				continue;
147090075Sobrien			}
147190075Sobrien#endif
147290075Sobrien			if (prison_restrict_ip4(tpr, NULL)) {
147390075Sobrien				redo_ip4 = 1;
147490075Sobrien				descend = 0;
147590075Sobrien			}
147690075Sobrien		}
147790075Sobrien	}
147890075Sobrien#endif
147990075Sobrien#ifdef INET6
148090075Sobrien	redo_ip6 = 0;
148190075Sobrien	if (pr_flags & PR_IP6_USER) {
148290075Sobrien		pr->pr_flags |= PR_IP6;
148390075Sobrien		free(pr->pr_ip6, M_PRISON);
148490075Sobrien		pr->pr_ip6s = ip6s;
148590075Sobrien		pr->pr_ip6 = ip6;
148690075Sobrien		ip6 = NULL;
148790075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
148890075Sobrien#ifdef VIMAGE
148990075Sobrien			if (tpr->pr_flags & PR_VNET) {
149090075Sobrien				descend = 0;
149190075Sobrien				continue;
149290075Sobrien			}
149390075Sobrien#endif
149490075Sobrien			if (prison_restrict_ip6(tpr, NULL)) {
149590075Sobrien				redo_ip6 = 1;
149690075Sobrien				descend = 0;
149790075Sobrien			}
149890075Sobrien		}
149990075Sobrien	}
150090075Sobrien#endif
150190075Sobrien	if (gotslevel) {
150290075Sobrien		pr->pr_securelevel = slevel;
150390075Sobrien		/* Set all child jails to be at least this level. */
150490075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
150590075Sobrien			if (tpr->pr_securelevel < slevel)
150690075Sobrien				tpr->pr_securelevel = slevel;
150790075Sobrien	}
150890075Sobrien	if (gotchildmax) {
150990075Sobrien		pr->pr_childmax = childmax;
151090075Sobrien		/* Set all child jails to under this limit. */
151190075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
151290075Sobrien			if (tpr->pr_childmax > childmax - level)
151390075Sobrien				tpr->pr_childmax = childmax > level
151490075Sobrien				    ? childmax - level : 0;
151590075Sobrien	}
151690075Sobrien	if (gotenforce) {
151790075Sobrien		pr->pr_enforce_statfs = enforce;
151890075Sobrien		/* Pass this restriction on to the children. */
151990075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
152090075Sobrien			if (tpr->pr_enforce_statfs < enforce)
152190075Sobrien				tpr->pr_enforce_statfs = enforce;
152290075Sobrien	}
152390075Sobrien	if (name != NULL) {
152490075Sobrien		if (ppr == &prison0)
152590075Sobrien			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
152690075Sobrien		else
152790075Sobrien			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
152890075Sobrien			    ppr->pr_name, name);
152990075Sobrien		/* Change this component of child names. */
153090075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
153190075Sobrien			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
153290075Sobrien			    strlen(tpr->pr_name + onamelen) + 1);
153390075Sobrien			bcopy(pr->pr_name, tpr->pr_name, namelen);
153490075Sobrien		}
153590075Sobrien	}
153690075Sobrien	if (path != NULL) {
153790075Sobrien		/* Try to keep a real-rooted full pathname. */
153890075Sobrien		if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
153990075Sobrien			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
154090075Sobrien			    mypr->pr_path, path);
154190075Sobrien		else
154290075Sobrien			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
154390075Sobrien		pr->pr_root = root;
154490075Sobrien	}
154590075Sobrien	if (PR_HOST & ch_flags & ~pr_flags) {
154690075Sobrien		if (pr->pr_flags & PR_HOST) {
154790075Sobrien			/*
154890075Sobrien			 * Copy the parent's host info.  As with pr_ip4 above,
154990075Sobrien			 * the lack of a lock on the parent is not a problem;
155090075Sobrien			 * it is always set with allprison_lock at least
155190075Sobrien			 * shared, and is held exclusively here.
155290075Sobrien			 */
155390075Sobrien			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
155490075Sobrien			    sizeof(pr->pr_hostname));
155590075Sobrien			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
155690075Sobrien			    sizeof(pr->pr_domainname));
155790075Sobrien			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
155890075Sobrien			    sizeof(pr->pr_hostuuid));
155990075Sobrien			pr->pr_hostid = pr->pr_parent->pr_hostid;
156090075Sobrien		}
156190075Sobrien	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
156290075Sobrien		/* Set this prison, and any descendants without PR_HOST. */
156390075Sobrien		if (host != NULL)
156490075Sobrien			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
156590075Sobrien		if (domain != NULL)
156690075Sobrien			strlcpy(pr->pr_domainname, domain,
156790075Sobrien			    sizeof(pr->pr_domainname));
156890075Sobrien		if (uuid != NULL)
156990075Sobrien			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
157090075Sobrien		if (gothid)
157190075Sobrien			pr->pr_hostid = hid;
157290075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
157390075Sobrien			if (tpr->pr_flags & PR_HOST)
157490075Sobrien				descend = 0;
157590075Sobrien			else {
157690075Sobrien				if (host != NULL)
157790075Sobrien					strlcpy(tpr->pr_hostname,
157890075Sobrien					    pr->pr_hostname,
157990075Sobrien					    sizeof(tpr->pr_hostname));
158090075Sobrien				if (domain != NULL)
158190075Sobrien					strlcpy(tpr->pr_domainname,
158290075Sobrien					    pr->pr_domainname,
158390075Sobrien					    sizeof(tpr->pr_domainname));
158490075Sobrien				if (uuid != NULL)
158590075Sobrien					strlcpy(tpr->pr_hostuuid,
158690075Sobrien					    pr->pr_hostuuid,
158790075Sobrien					    sizeof(tpr->pr_hostuuid));
158890075Sobrien				if (gothid)
158990075Sobrien					tpr->pr_hostid = hid;
159090075Sobrien			}
159190075Sobrien		}
159290075Sobrien	}
159390075Sobrien	if ((tallow = ch_allow & ~pr_allow)) {
159490075Sobrien		/* Clear allow bits in all children. */
159590075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
159690075Sobrien			tpr->pr_allow &= ~tallow;
159790075Sobrien	}
159890075Sobrien	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
159990075Sobrien	/*
160090075Sobrien	 * Persistent prisons get an extra reference, and prisons losing their
160190075Sobrien	 * persist flag lose that reference.  Only do this for existing prisons
160290075Sobrien	 * for now, so new ones will remain unseen until after the module
160390075Sobrien	 * handlers have completed.
160490075Sobrien	 */
160590075Sobrien	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
160690075Sobrien		if (pr_flags & PR_PERSIST) {
160790075Sobrien			pr->pr_ref++;
160890075Sobrien			pr->pr_uref++;
160990075Sobrien		} else {
161090075Sobrien			pr->pr_ref--;
161190075Sobrien			pr->pr_uref--;
161290075Sobrien		}
161390075Sobrien	}
161490075Sobrien	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
161590075Sobrien	mtx_unlock(&pr->pr_mtx);
161690075Sobrien
161790075Sobrien	/* Locks may have prevented a complete restriction of child IP
161890075Sobrien	 * addresses.  If so, allocate some more memory and try again.
161990075Sobrien	 */
162090075Sobrien#ifdef INET
162190075Sobrien	while (redo_ip4) {
162290075Sobrien		ip4s = pr->pr_ip4s;
162390075Sobrien		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
162490075Sobrien		mtx_lock(&pr->pr_mtx);
162590075Sobrien		redo_ip4 = 0;
162690075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
162790075Sobrien#ifdef VIMAGE
162890075Sobrien			if (tpr->pr_flags & PR_VNET) {
162990075Sobrien				descend = 0;
163090075Sobrien				continue;
163190075Sobrien			}
163290075Sobrien#endif
163390075Sobrien			if (prison_restrict_ip4(tpr, ip4)) {
163490075Sobrien				if (ip4 != NULL)
163590075Sobrien					ip4 = NULL;
163690075Sobrien				else
163790075Sobrien					redo_ip4 = 1;
163890075Sobrien			}
163990075Sobrien		}
164090075Sobrien		mtx_unlock(&pr->pr_mtx);
164190075Sobrien	}
164290075Sobrien#endif
164390075Sobrien#ifdef INET6
164490075Sobrien	while (redo_ip6) {
164590075Sobrien		ip6s = pr->pr_ip6s;
164690075Sobrien		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
164790075Sobrien		mtx_lock(&pr->pr_mtx);
164890075Sobrien		redo_ip6 = 0;
164990075Sobrien		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
165090075Sobrien#ifdef VIMAGE
165190075Sobrien			if (tpr->pr_flags & PR_VNET) {
165290075Sobrien				descend = 0;
165390075Sobrien				continue;
165490075Sobrien			}
165590075Sobrien#endif
165690075Sobrien			if (prison_restrict_ip6(tpr, ip6)) {
165790075Sobrien				if (ip6 != NULL)
165890075Sobrien					ip6 = NULL;
165990075Sobrien				else
166090075Sobrien					redo_ip6 = 1;
166190075Sobrien			}
166290075Sobrien		}
166390075Sobrien		mtx_unlock(&pr->pr_mtx);
166490075Sobrien	}
166590075Sobrien#endif
166690075Sobrien
166790075Sobrien	/* Let the modules do their work. */
166890075Sobrien	sx_downgrade(&allprison_lock);
166990075Sobrien	if (created) {
167090075Sobrien		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
167190075Sobrien		if (error) {
167290075Sobrien			prison_deref(pr, PD_LIST_SLOCKED);
167390075Sobrien			goto done_errmsg;
167490075Sobrien		}
167590075Sobrien	}
167690075Sobrien	error = osd_jail_call(pr, PR_METHOD_SET, opts);
167790075Sobrien	if (error) {
167890075Sobrien		prison_deref(pr, created
167990075Sobrien		    ? PD_LIST_SLOCKED
168090075Sobrien		    : PD_DEREF | PD_LIST_SLOCKED);
168190075Sobrien		goto done_errmsg;
168290075Sobrien	}
168390075Sobrien
168490075Sobrien	/* Attach this process to the prison if requested. */
168590075Sobrien	if (flags & JAIL_ATTACH) {
168690075Sobrien		mtx_lock(&pr->pr_mtx);
168790075Sobrien		error = do_jail_attach(td, pr);
168890075Sobrien		if (error) {
168990075Sobrien			vfs_opterror(opts, "attach failed");
169090075Sobrien			if (!created)
169190075Sobrien				prison_deref(pr, PD_DEREF);
169290075Sobrien			goto done_errmsg;
169390075Sobrien		}
169490075Sobrien	}
169590075Sobrien
169690075Sobrien	/*
169790075Sobrien	 * Now that it is all there, drop the temporary reference from existing
169890075Sobrien	 * prisons.  Or add a reference to newly created persistent prisons
169990075Sobrien	 * (which was not done earlier so that the prison would not be publicly
170090075Sobrien	 * visible).
170190075Sobrien	 */
170290075Sobrien	if (!created) {
170390075Sobrien		prison_deref(pr, (flags & JAIL_ATTACH)
170490075Sobrien		    ? PD_DEREF
170590075Sobrien		    : PD_DEREF | PD_LIST_SLOCKED);
170690075Sobrien	} else {
170790075Sobrien		if (pr_flags & PR_PERSIST) {
170890075Sobrien			mtx_lock(&pr->pr_mtx);
170990075Sobrien			pr->pr_ref++;
171090075Sobrien			pr->pr_uref++;
171190075Sobrien			mtx_unlock(&pr->pr_mtx);
171290075Sobrien		}
171390075Sobrien		if (!(flags & JAIL_ATTACH))
171490075Sobrien			sx_sunlock(&allprison_lock);
171590075Sobrien	}
171690075Sobrien	td->td_retval[0] = pr->pr_id;
171790075Sobrien	goto done_errmsg;
171890075Sobrien
171990075Sobrien done_deref_locked:
172090075Sobrien	prison_deref(pr, created
172190075Sobrien	    ? PD_LOCKED | PD_LIST_XLOCKED
172290075Sobrien	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
172390075Sobrien	goto done_releroot;
172490075Sobrien done_unlock_list:
172590075Sobrien	sx_xunlock(&allprison_lock);
172690075Sobrien done_releroot:
172790075Sobrien	if (root != NULL) {
172890075Sobrien		vfslocked = VFS_LOCK_GIANT(root->v_mount);
172990075Sobrien		vrele(root);
173090075Sobrien		VFS_UNLOCK_GIANT(vfslocked);
173190075Sobrien	}
173290075Sobrien done_errmsg:
173390075Sobrien	if (error) {
173490075Sobrien		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
173590075Sobrien		if (errmsg_len > 0) {
173690075Sobrien			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
173790075Sobrien			if (errmsg_pos > 0) {
173890075Sobrien				if (optuio->uio_segflg == UIO_SYSSPACE)
173990075Sobrien					bcopy(errmsg,
174090075Sobrien					   optuio->uio_iov[errmsg_pos].iov_base,
174190075Sobrien					   errmsg_len);
174290075Sobrien				else
174390075Sobrien					copyout(errmsg,
174490075Sobrien					   optuio->uio_iov[errmsg_pos].iov_base,
174590075Sobrien					   errmsg_len);
174690075Sobrien			}
174790075Sobrien		}
174890075Sobrien	}
174990075Sobrien done_free:
175090075Sobrien#ifdef INET
175190075Sobrien	free(ip4, M_PRISON);
175290075Sobrien#endif
175390075Sobrien#ifdef INET6
175490075Sobrien	free(ip6, M_PRISON);
175590075Sobrien#endif
175690075Sobrien	vfs_freeopts(opts);
175790075Sobrien	return (error);
175890075Sobrien}
175990075Sobrien
176090075Sobrien
176190075Sobrien/*
176290075Sobrien * struct jail_get_args {
176390075Sobrien *	struct iovec *iovp;
176490075Sobrien *	unsigned int iovcnt;
176590075Sobrien *	int flags;
176690075Sobrien * };
176790075Sobrien */
176890075Sobrienint
176990075Sobrienjail_get(struct thread *td, struct jail_get_args *uap)
177090075Sobrien{
177190075Sobrien	struct uio *auio;
177290075Sobrien	int error;
177390075Sobrien
177490075Sobrien	/* Check that we have an even number of iovecs. */
177590075Sobrien	if (uap->iovcnt & 1)
177690075Sobrien		return (EINVAL);
177790075Sobrien
177890075Sobrien	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
177990075Sobrien	if (error)
178090075Sobrien		return (error);
178190075Sobrien	error = kern_jail_get(td, auio, uap->flags);
178290075Sobrien	if (error == 0)
178390075Sobrien		error = copyout(auio->uio_iov, uap->iovp,
178490075Sobrien		    uap->iovcnt * sizeof (struct iovec));
178590075Sobrien	free(auio, M_IOV);
178690075Sobrien	return (error);
178790075Sobrien}
178890075Sobrien
178990075Sobrienint
179090075Sobrienkern_jail_get(struct thread *td, struct uio *optuio, int flags)
179190075Sobrien{
179290075Sobrien	struct prison *pr, *mypr;
179390075Sobrien	struct vfsopt *opt;
179490075Sobrien	struct vfsoptlist *opts;
179590075Sobrien	char *errmsg, *name;
179690075Sobrien	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
179790075Sobrien
179890075Sobrien	if (flags & ~JAIL_GET_MASK)
179990075Sobrien		return (EINVAL);
180090075Sobrien
180190075Sobrien	/* Get the parameter list. */
180290075Sobrien	error = vfs_buildopts(optuio, &opts);
180390075Sobrien	if (error)
180490075Sobrien		return (error);
180590075Sobrien	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
180690075Sobrien	mypr = td->td_ucred->cr_prison;
180790075Sobrien
180890075Sobrien	/*
180990075Sobrien	 * Find the prison specified by one of: lastjid, jid, name.
181090075Sobrien	 */
181190075Sobrien	sx_slock(&allprison_lock);
181290075Sobrien	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
181390075Sobrien	if (error == 0) {
181490075Sobrien		TAILQ_FOREACH(pr, &allprison, pr_list) {
181590075Sobrien			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
181690075Sobrien				mtx_lock(&pr->pr_mtx);
181790075Sobrien				if (pr->pr_ref > 0 &&
181890075Sobrien				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
181990075Sobrien					break;
182090075Sobrien				mtx_unlock(&pr->pr_mtx);
182190075Sobrien			}
182290075Sobrien		}
182390075Sobrien		if (pr != NULL)
182490075Sobrien			goto found_prison;
182590075Sobrien		error = ENOENT;
182690075Sobrien		vfs_opterror(opts, "no jail after %d", jid);
182790075Sobrien		goto done_unlock_list;
182890075Sobrien	} else if (error != ENOENT)
182990075Sobrien		goto done_unlock_list;
183090075Sobrien
183190075Sobrien	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
183290075Sobrien	if (error == 0) {
183390075Sobrien		if (jid != 0) {
183490075Sobrien			pr = prison_find_child(mypr, jid);
183590075Sobrien			if (pr != NULL) {
183690075Sobrien				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
183790075Sobrien					mtx_unlock(&pr->pr_mtx);
183890075Sobrien					error = ENOENT;
183990075Sobrien					vfs_opterror(opts, "jail %d is dying",
184090075Sobrien					    jid);
184190075Sobrien					goto done_unlock_list;
184290075Sobrien				}
184390075Sobrien				goto found_prison;
184490075Sobrien			}
184590075Sobrien			error = ENOENT;
184690075Sobrien			vfs_opterror(opts, "jail %d not found", jid);
184790075Sobrien			goto done_unlock_list;
184890075Sobrien		}
184990075Sobrien	} else if (error != ENOENT)
185090075Sobrien		goto done_unlock_list;
185190075Sobrien
185290075Sobrien	error = vfs_getopt(opts, "name", (void **)&name, &len);
1853	if (error == 0) {
1854		if (len == 0 || name[len - 1] != '\0') {
1855			error = EINVAL;
1856			goto done_unlock_list;
1857		}
1858		pr = prison_find_name(mypr, name);
1859		if (pr != NULL) {
1860			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1861				mtx_unlock(&pr->pr_mtx);
1862				error = ENOENT;
1863				vfs_opterror(opts, "jail \"%s\" is dying",
1864				    name);
1865				goto done_unlock_list;
1866			}
1867			goto found_prison;
1868		}
1869		error = ENOENT;
1870		vfs_opterror(opts, "jail \"%s\" not found", name);
1871		goto done_unlock_list;
1872	} else if (error != ENOENT)
1873		goto done_unlock_list;
1874
1875	vfs_opterror(opts, "no jail specified");
1876	error = ENOENT;
1877	goto done_unlock_list;
1878
1879 found_prison:
1880	/* Get the parameters of the prison. */
1881	pr->pr_ref++;
1882	locked = PD_LOCKED;
1883	td->td_retval[0] = pr->pr_id;
1884	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1885	if (error != 0 && error != ENOENT)
1886		goto done_deref;
1887	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1888	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1889	if (error != 0 && error != ENOENT)
1890		goto done_deref;
1891	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1892	if (error != 0 && error != ENOENT)
1893		goto done_deref;
1894	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1895	    sizeof(pr->pr_cpuset->cs_id));
1896	if (error != 0 && error != ENOENT)
1897		goto done_deref;
1898	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1899	if (error != 0 && error != ENOENT)
1900		goto done_deref;
1901#ifdef INET
1902	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1903	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1904	if (error != 0 && error != ENOENT)
1905		goto done_deref;
1906#endif
1907#ifdef INET6
1908	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1909	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1910	if (error != 0 && error != ENOENT)
1911		goto done_deref;
1912#endif
1913	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1914	    sizeof(pr->pr_securelevel));
1915	if (error != 0 && error != ENOENT)
1916		goto done_deref;
1917	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1918	    sizeof(pr->pr_childcount));
1919	if (error != 0 && error != ENOENT)
1920		goto done_deref;
1921	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1922	    sizeof(pr->pr_childmax));
1923	if (error != 0 && error != ENOENT)
1924		goto done_deref;
1925	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1926	if (error != 0 && error != ENOENT)
1927		goto done_deref;
1928	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1929	if (error != 0 && error != ENOENT)
1930		goto done_deref;
1931	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1932	if (error != 0 && error != ENOENT)
1933		goto done_deref;
1934#ifdef COMPAT_IA32
1935	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1936		uint32_t hid32 = pr->pr_hostid;
1937
1938		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1939	} else
1940#endif
1941	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1942	    sizeof(pr->pr_hostid));
1943	if (error != 0 && error != ENOENT)
1944		goto done_deref;
1945	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1946	    sizeof(pr->pr_enforce_statfs));
1947	if (error != 0 && error != ENOENT)
1948		goto done_deref;
1949	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1950	    fi++) {
1951		if (pr_flag_names[fi] == NULL)
1952			continue;
1953		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1954		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1955		if (error != 0 && error != ENOENT)
1956			goto done_deref;
1957		i = !i;
1958		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1959		if (error != 0 && error != ENOENT)
1960			goto done_deref;
1961	}
1962	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1963	    fi++) {
1964		i = pr->pr_flags &
1965		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1966		i = pr_flag_jailsys[fi].disable &&
1967		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1968		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
1969		    : JAIL_SYS_INHERIT;
1970		error =
1971		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
1972		if (error != 0 && error != ENOENT)
1973			goto done_deref;
1974	}
1975	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
1976	    fi++) {
1977		if (pr_allow_names[fi] == NULL)
1978			continue;
1979		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
1980		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
1981		if (error != 0 && error != ENOENT)
1982			goto done_deref;
1983		i = !i;
1984		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
1985		if (error != 0 && error != ENOENT)
1986			goto done_deref;
1987	}
1988	i = (pr->pr_uref == 0);
1989	error = vfs_setopt(opts, "dying", &i, sizeof(i));
1990	if (error != 0 && error != ENOENT)
1991		goto done_deref;
1992	i = !i;
1993	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1994	if (error != 0 && error != ENOENT)
1995		goto done_deref;
1996
1997	/* Get the module parameters. */
1998	mtx_unlock(&pr->pr_mtx);
1999	locked = 0;
2000	error = osd_jail_call(pr, PR_METHOD_GET, opts);
2001	if (error)
2002		goto done_deref;
2003	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2004
2005	/* By now, all parameters should have been noted. */
2006	TAILQ_FOREACH(opt, opts, link) {
2007		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2008			error = EINVAL;
2009			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2010			goto done_errmsg;
2011		}
2012	}
2013
2014	/* Write the fetched parameters back to userspace. */
2015	error = 0;
2016	TAILQ_FOREACH(opt, opts, link) {
2017		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2018			pos = 2 * opt->pos + 1;
2019			optuio->uio_iov[pos].iov_len = opt->len;
2020			if (opt->value != NULL) {
2021				if (optuio->uio_segflg == UIO_SYSSPACE) {
2022					bcopy(opt->value,
2023					    optuio->uio_iov[pos].iov_base,
2024					    opt->len);
2025				} else {
2026					error = copyout(opt->value,
2027					    optuio->uio_iov[pos].iov_base,
2028					    opt->len);
2029					if (error)
2030						break;
2031				}
2032			}
2033		}
2034	}
2035	goto done_errmsg;
2036
2037 done_deref:
2038	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2039	goto done_errmsg;
2040
2041 done_unlock_list:
2042	sx_sunlock(&allprison_lock);
2043 done_errmsg:
2044	if (error && errmsg_pos >= 0) {
2045		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2046		errmsg_pos = 2 * errmsg_pos + 1;
2047		if (errmsg_len > 0) {
2048			if (optuio->uio_segflg == UIO_SYSSPACE)
2049				bcopy(errmsg,
2050				    optuio->uio_iov[errmsg_pos].iov_base,
2051				    errmsg_len);
2052			else
2053				copyout(errmsg,
2054				    optuio->uio_iov[errmsg_pos].iov_base,
2055				    errmsg_len);
2056		}
2057	}
2058	vfs_freeopts(opts);
2059	return (error);
2060}
2061
2062
2063/*
2064 * struct jail_remove_args {
2065 *	int jid;
2066 * };
2067 */
2068int
2069jail_remove(struct thread *td, struct jail_remove_args *uap)
2070{
2071	struct prison *pr, *cpr, *lpr, *tpr;
2072	int descend, error;
2073
2074	error = priv_check(td, PRIV_JAIL_REMOVE);
2075	if (error)
2076		return (error);
2077
2078	sx_xlock(&allprison_lock);
2079	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2080	if (pr == NULL) {
2081		sx_xunlock(&allprison_lock);
2082		return (EINVAL);
2083	}
2084
2085	/* Remove all descendants of this prison, then remove this prison. */
2086	pr->pr_ref++;
2087	pr->pr_flags |= PR_REMOVE;
2088	if (!LIST_EMPTY(&pr->pr_children)) {
2089		mtx_unlock(&pr->pr_mtx);
2090		lpr = NULL;
2091		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2092			mtx_lock(&cpr->pr_mtx);
2093			if (cpr->pr_ref > 0) {
2094				tpr = cpr;
2095				cpr->pr_ref++;
2096				cpr->pr_flags |= PR_REMOVE;
2097			} else {
2098				/* Already removed - do not do it again. */
2099				tpr = NULL;
2100			}
2101			mtx_unlock(&cpr->pr_mtx);
2102			if (lpr != NULL) {
2103				mtx_lock(&lpr->pr_mtx);
2104				prison_remove_one(lpr);
2105				sx_xlock(&allprison_lock);
2106			}
2107			lpr = tpr;
2108		}
2109		if (lpr != NULL) {
2110			mtx_lock(&lpr->pr_mtx);
2111			prison_remove_one(lpr);
2112			sx_xlock(&allprison_lock);
2113		}
2114		mtx_lock(&pr->pr_mtx);
2115	}
2116	prison_remove_one(pr);
2117	return (0);
2118}
2119
2120static void
2121prison_remove_one(struct prison *pr)
2122{
2123	struct proc *p;
2124	int deuref;
2125
2126	/* If the prison was persistent, it is not anymore. */
2127	deuref = 0;
2128	if (pr->pr_flags & PR_PERSIST) {
2129		pr->pr_ref--;
2130		deuref = PD_DEUREF;
2131		pr->pr_flags &= ~PR_PERSIST;
2132	}
2133
2134	/*
2135	 * jail_remove added a reference.  If that's the only one, remove
2136	 * the prison now.
2137	 */
2138	KASSERT(pr->pr_ref > 0,
2139	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2140	if (pr->pr_ref == 1) {
2141		prison_deref(pr,
2142		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2143		return;
2144	}
2145
2146	mtx_unlock(&pr->pr_mtx);
2147	sx_xunlock(&allprison_lock);
2148	/*
2149	 * Kill all processes unfortunate enough to be attached to this prison.
2150	 */
2151	sx_slock(&allproc_lock);
2152	LIST_FOREACH(p, &allproc, p_list) {
2153		PROC_LOCK(p);
2154		if (p->p_state != PRS_NEW && p->p_ucred &&
2155		    p->p_ucred->cr_prison == pr)
2156			psignal(p, SIGKILL);
2157		PROC_UNLOCK(p);
2158	}
2159	sx_sunlock(&allproc_lock);
2160	/* Remove the temporary reference added by jail_remove. */
2161	prison_deref(pr, deuref | PD_DEREF);
2162}
2163
2164
2165/*
2166 * struct jail_attach_args {
2167 *	int jid;
2168 * };
2169 */
2170int
2171jail_attach(struct thread *td, struct jail_attach_args *uap)
2172{
2173	struct prison *pr;
2174	int error;
2175
2176	error = priv_check(td, PRIV_JAIL_ATTACH);
2177	if (error)
2178		return (error);
2179
2180	sx_slock(&allprison_lock);
2181	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2182	if (pr == NULL) {
2183		sx_sunlock(&allprison_lock);
2184		return (EINVAL);
2185	}
2186
2187	/*
2188	 * Do not allow a process to attach to a prison that is not
2189	 * considered to be "alive".
2190	 */
2191	if (pr->pr_uref == 0) {
2192		mtx_unlock(&pr->pr_mtx);
2193		sx_sunlock(&allprison_lock);
2194		return (EINVAL);
2195	}
2196
2197	return (do_jail_attach(td, pr));
2198}
2199
2200static int
2201do_jail_attach(struct thread *td, struct prison *pr)
2202{
2203	struct prison *ppr;
2204	struct proc *p;
2205	struct ucred *newcred, *oldcred;
2206	int vfslocked, error;
2207
2208	/*
2209	 * XXX: Note that there is a slight race here if two threads
2210	 * in the same privileged process attempt to attach to two
2211	 * different jails at the same time.  It is important for
2212	 * user processes not to do this, or they might end up with
2213	 * a process root from one prison, but attached to the jail
2214	 * of another.
2215	 */
2216	pr->pr_ref++;
2217	pr->pr_uref++;
2218	mtx_unlock(&pr->pr_mtx);
2219
2220	/* Let modules do whatever they need to prepare for attaching. */
2221	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2222	if (error) {
2223		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2224		return (error);
2225	}
2226	sx_sunlock(&allprison_lock);
2227
2228	/*
2229	 * Reparent the newly attached process to this jail.
2230	 */
2231	ppr = td->td_ucred->cr_prison;
2232	p = td->td_proc;
2233	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2234	if (error)
2235		goto e_revert_osd;
2236
2237	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2238	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2239	if ((error = change_dir(pr->pr_root, td)) != 0)
2240		goto e_unlock;
2241#ifdef MAC
2242	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2243		goto e_unlock;
2244#endif
2245	VOP_UNLOCK(pr->pr_root, 0);
2246	if ((error = change_root(pr->pr_root, td)))
2247		goto e_unlock_giant;
2248	VFS_UNLOCK_GIANT(vfslocked);
2249
2250	newcred = crget();
2251	PROC_LOCK(p);
2252	oldcred = p->p_ucred;
2253	setsugid(p);
2254	crcopy(newcred, oldcred);
2255	newcred->cr_prison = pr;
2256	p->p_ucred = newcred;
2257	PROC_UNLOCK(p);
2258	crfree(oldcred);
2259	prison_deref(ppr, PD_DEREF | PD_DEUREF);
2260	return (0);
2261 e_unlock:
2262	VOP_UNLOCK(pr->pr_root, 0);
2263 e_unlock_giant:
2264	VFS_UNLOCK_GIANT(vfslocked);
2265 e_revert_osd:
2266	/* Tell modules this thread is still in its old jail after all. */
2267	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2268	prison_deref(pr, PD_DEREF | PD_DEUREF);
2269	return (error);
2270}
2271
2272
2273/*
2274 * Returns a locked prison instance, or NULL on failure.
2275 */
2276struct prison *
2277prison_find(int prid)
2278{
2279	struct prison *pr;
2280
2281	sx_assert(&allprison_lock, SX_LOCKED);
2282	TAILQ_FOREACH(pr, &allprison, pr_list) {
2283		if (pr->pr_id == prid) {
2284			mtx_lock(&pr->pr_mtx);
2285			if (pr->pr_ref > 0)
2286				return (pr);
2287			mtx_unlock(&pr->pr_mtx);
2288		}
2289	}
2290	return (NULL);
2291}
2292
2293/*
2294 * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2295 */
2296struct prison *
2297prison_find_child(struct prison *mypr, int prid)
2298{
2299	struct prison *pr;
2300	int descend;
2301
2302	sx_assert(&allprison_lock, SX_LOCKED);
2303	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2304		if (pr->pr_id == prid) {
2305			mtx_lock(&pr->pr_mtx);
2306			if (pr->pr_ref > 0)
2307				return (pr);
2308			mtx_unlock(&pr->pr_mtx);
2309		}
2310	}
2311	return (NULL);
2312}
2313
2314/*
2315 * Look for the name relative to mypr.  Returns a locked prison or NULL.
2316 */
2317struct prison *
2318prison_find_name(struct prison *mypr, const char *name)
2319{
2320	struct prison *pr, *deadpr;
2321	size_t mylen;
2322	int descend;
2323
2324	sx_assert(&allprison_lock, SX_LOCKED);
2325	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2326 again:
2327	deadpr = NULL;
2328	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2329		if (!strcmp(pr->pr_name + mylen, name)) {
2330			mtx_lock(&pr->pr_mtx);
2331			if (pr->pr_ref > 0) {
2332				if (pr->pr_uref > 0)
2333					return (pr);
2334				deadpr = pr;
2335			}
2336			mtx_unlock(&pr->pr_mtx);
2337		}
2338	}
2339	/* There was no valid prison - perhaps there was a dying one. */
2340	if (deadpr != NULL) {
2341		mtx_lock(&deadpr->pr_mtx);
2342		if (deadpr->pr_ref == 0) {
2343			mtx_unlock(&deadpr->pr_mtx);
2344			goto again;
2345		}
2346	}
2347	return (deadpr);
2348}
2349
2350/*
2351 * See if a prison has the specific flag set.
2352 */
2353int
2354prison_flag(struct ucred *cred, unsigned flag)
2355{
2356
2357	/* This is an atomic read, so no locking is necessary. */
2358	return (cred->cr_prison->pr_flags & flag);
2359}
2360
2361int
2362prison_allow(struct ucred *cred, unsigned flag)
2363{
2364
2365	/* This is an atomic read, so no locking is necessary. */
2366	return (cred->cr_prison->pr_allow & flag);
2367}
2368
2369/*
2370 * Remove a prison reference.  If that was the last reference, remove the
2371 * prison itself - but not in this context in case there are locks held.
2372 */
2373void
2374prison_free_locked(struct prison *pr)
2375{
2376
2377	mtx_assert(&pr->pr_mtx, MA_OWNED);
2378	pr->pr_ref--;
2379	if (pr->pr_ref == 0) {
2380		mtx_unlock(&pr->pr_mtx);
2381		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2382		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2383		return;
2384	}
2385	mtx_unlock(&pr->pr_mtx);
2386}
2387
2388void
2389prison_free(struct prison *pr)
2390{
2391
2392	mtx_lock(&pr->pr_mtx);
2393	prison_free_locked(pr);
2394}
2395
2396static void
2397prison_complete(void *context, int pending)
2398{
2399
2400	prison_deref((struct prison *)context, 0);
2401}
2402
2403/*
2404 * Remove a prison reference (usually).  This internal version assumes no
2405 * mutexes are held, except perhaps the prison itself.  If there are no more
2406 * references, release and delist the prison.  On completion, the prison lock
2407 * and the allprison lock are both unlocked.
2408 */
2409static void
2410prison_deref(struct prison *pr, int flags)
2411{
2412	struct prison *ppr, *tpr;
2413	int vfslocked;
2414
2415	if (!(flags & PD_LOCKED))
2416		mtx_lock(&pr->pr_mtx);
2417	/* Decrement the user references in a separate loop. */
2418	if (flags & PD_DEUREF) {
2419		for (tpr = pr;; tpr = tpr->pr_parent) {
2420			if (tpr != pr)
2421				mtx_lock(&tpr->pr_mtx);
2422			if (--tpr->pr_uref > 0)
2423				break;
2424			KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2425			mtx_unlock(&tpr->pr_mtx);
2426		}
2427		/* Done if there were only user references to remove. */
2428		if (!(flags & PD_DEREF)) {
2429			mtx_unlock(&tpr->pr_mtx);
2430			if (flags & PD_LIST_SLOCKED)
2431				sx_sunlock(&allprison_lock);
2432			else if (flags & PD_LIST_XLOCKED)
2433				sx_xunlock(&allprison_lock);
2434			return;
2435		}
2436		if (tpr != pr) {
2437			mtx_unlock(&tpr->pr_mtx);
2438			mtx_lock(&pr->pr_mtx);
2439		}
2440	}
2441
2442	for (;;) {
2443		if (flags & PD_DEREF)
2444			pr->pr_ref--;
2445		/* If the prison still has references, nothing else to do. */
2446		if (pr->pr_ref > 0) {
2447			mtx_unlock(&pr->pr_mtx);
2448			if (flags & PD_LIST_SLOCKED)
2449				sx_sunlock(&allprison_lock);
2450			else if (flags & PD_LIST_XLOCKED)
2451				sx_xunlock(&allprison_lock);
2452			return;
2453		}
2454
2455		mtx_unlock(&pr->pr_mtx);
2456		if (flags & PD_LIST_SLOCKED) {
2457			if (!sx_try_upgrade(&allprison_lock)) {
2458				sx_sunlock(&allprison_lock);
2459				sx_xlock(&allprison_lock);
2460			}
2461		} else if (!(flags & PD_LIST_XLOCKED))
2462			sx_xlock(&allprison_lock);
2463
2464		TAILQ_REMOVE(&allprison, pr, pr_list);
2465		LIST_REMOVE(pr, pr_sibling);
2466		ppr = pr->pr_parent;
2467		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2468			tpr->pr_childcount--;
2469		sx_xunlock(&allprison_lock);
2470
2471#ifdef VIMAGE
2472		if (pr->pr_vnet != ppr->pr_vnet)
2473			vnet_destroy(pr->pr_vnet);
2474#endif
2475		if (pr->pr_root != NULL) {
2476			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2477			vrele(pr->pr_root);
2478			VFS_UNLOCK_GIANT(vfslocked);
2479		}
2480		mtx_destroy(&pr->pr_mtx);
2481#ifdef INET
2482		free(pr->pr_ip4, M_PRISON);
2483#endif
2484#ifdef INET6
2485		free(pr->pr_ip6, M_PRISON);
2486#endif
2487		if (pr->pr_cpuset != NULL)
2488			cpuset_rel(pr->pr_cpuset);
2489		osd_jail_exit(pr);
2490		free(pr, M_PRISON);
2491
2492		/* Removing a prison frees a reference on its parent. */
2493		pr = ppr;
2494		mtx_lock(&pr->pr_mtx);
2495		flags = PD_DEREF;
2496	}
2497}
2498
2499void
2500prison_hold_locked(struct prison *pr)
2501{
2502
2503	mtx_assert(&pr->pr_mtx, MA_OWNED);
2504	KASSERT(pr->pr_ref > 0,
2505	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2506	pr->pr_ref++;
2507}
2508
2509void
2510prison_hold(struct prison *pr)
2511{
2512
2513	mtx_lock(&pr->pr_mtx);
2514	prison_hold_locked(pr);
2515	mtx_unlock(&pr->pr_mtx);
2516}
2517
2518void
2519prison_proc_hold(struct prison *pr)
2520{
2521
2522	mtx_lock(&pr->pr_mtx);
2523	KASSERT(pr->pr_uref > 0,
2524	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2525	pr->pr_uref++;
2526	mtx_unlock(&pr->pr_mtx);
2527}
2528
2529void
2530prison_proc_free(struct prison *pr)
2531{
2532
2533	mtx_lock(&pr->pr_mtx);
2534	KASSERT(pr->pr_uref > 0,
2535	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2536	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2537}
2538
2539
2540#ifdef INET
2541/*
2542 * Restrict a prison's IP address list with its parent's, possibly replacing
2543 * it.  Return true if the replacement buffer was used (or would have been).
2544 */
2545static int
2546prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2547{
2548	int ii, ij, used;
2549	struct prison *ppr;
2550
2551	ppr = pr->pr_parent;
2552	if (!(pr->pr_flags & PR_IP4_USER)) {
2553		/* This has no user settings, so just copy the parent's list. */
2554		if (pr->pr_ip4s < ppr->pr_ip4s) {
2555			/*
2556			 * There's no room for the parent's list.  Use the
2557			 * new list buffer, which is assumed to be big enough
2558			 * (if it was passed).  If there's no buffer, try to
2559			 * allocate one.
2560			 */
2561			used = 1;
2562			if (newip4 == NULL) {
2563				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2564				    M_PRISON, M_NOWAIT);
2565				if (newip4 != NULL)
2566					used = 0;
2567			}
2568			if (newip4 != NULL) {
2569				bcopy(ppr->pr_ip4, newip4,
2570				    ppr->pr_ip4s * sizeof(*newip4));
2571				free(pr->pr_ip4, M_PRISON);
2572				pr->pr_ip4 = newip4;
2573				pr->pr_ip4s = ppr->pr_ip4s;
2574			}
2575			return (used);
2576		}
2577		pr->pr_ip4s = ppr->pr_ip4s;
2578		if (pr->pr_ip4s > 0)
2579			bcopy(ppr->pr_ip4, pr->pr_ip4,
2580			    pr->pr_ip4s * sizeof(*newip4));
2581		else if (pr->pr_ip4 != NULL) {
2582			free(pr->pr_ip4, M_PRISON);
2583			pr->pr_ip4 = NULL;
2584		}
2585	} else if (pr->pr_ip4s > 0) {
2586		/* Remove addresses that aren't in the parent. */
2587		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2588			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2589				break;
2590		if (ij < ppr->pr_ip4s)
2591			ii = 1;
2592		else {
2593			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2594			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2595			ii = 0;
2596		}
2597		for (ij = 1; ii < pr->pr_ip4s; ) {
2598			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2599				ii++;
2600				continue;
2601			}
2602			switch (ij >= ppr->pr_ip4s ? -1 :
2603				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2604			case -1:
2605				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2606				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2607				break;
2608			case 0:
2609				ii++;
2610				ij++;
2611				break;
2612			case 1:
2613				ij++;
2614				break;
2615			}
2616		}
2617		if (pr->pr_ip4s == 0) {
2618			pr->pr_flags |= PR_IP4_DISABLE;
2619			free(pr->pr_ip4, M_PRISON);
2620			pr->pr_ip4 = NULL;
2621		}
2622	}
2623	return (0);
2624}
2625
2626/*
2627 * Pass back primary IPv4 address of this jail.
2628 *
2629 * If not restricted return success but do not alter the address.  Caller has
2630 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2631 *
2632 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2633 * Address returned in NBO.
2634 */
2635int
2636prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2637{
2638	struct prison *pr;
2639
2640	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2641	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2642
2643	pr = cred->cr_prison;
2644	if (!(pr->pr_flags & PR_IP4))
2645		return (0);
2646	mtx_lock(&pr->pr_mtx);
2647	if (!(pr->pr_flags & PR_IP4)) {
2648		mtx_unlock(&pr->pr_mtx);
2649		return (0);
2650	}
2651	if (pr->pr_ip4 == NULL) {
2652		mtx_unlock(&pr->pr_mtx);
2653		return (EAFNOSUPPORT);
2654	}
2655
2656	ia->s_addr = pr->pr_ip4[0].s_addr;
2657	mtx_unlock(&pr->pr_mtx);
2658	return (0);
2659}
2660
2661/*
2662 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2663 */
2664int
2665prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2666{
2667
2668	if (pr1 == pr2)
2669		return (1);
2670
2671	/*
2672	 * No need to lock since the PR_IP4_USER flag can't be altered for
2673	 * existing prisons.
2674	 */
2675	while (pr1 != &prison0 &&
2676#ifdef VIMAGE
2677	       !(pr1->pr_flags & PR_VNET) &&
2678#endif
2679	       !(pr1->pr_flags & PR_IP4_USER))
2680		pr1 = pr1->pr_parent;
2681	while (pr2 != &prison0 &&
2682#ifdef VIMAGE
2683	       !(pr2->pr_flags & PR_VNET) &&
2684#endif
2685	       !(pr2->pr_flags & PR_IP4_USER))
2686		pr2 = pr2->pr_parent;
2687	return (pr1 == pr2);
2688}
2689
2690/*
2691 * Make sure our (source) address is set to something meaningful to this
2692 * jail.
2693 *
2694 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2695 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2696 * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2697 */
2698int
2699prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2700{
2701	struct prison *pr;
2702	struct in_addr ia0;
2703	int error;
2704
2705	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2706	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2707
2708	pr = cred->cr_prison;
2709	if (!(pr->pr_flags & PR_IP4))
2710		return (0);
2711	mtx_lock(&pr->pr_mtx);
2712	if (!(pr->pr_flags & PR_IP4)) {
2713		mtx_unlock(&pr->pr_mtx);
2714		return (0);
2715	}
2716	if (pr->pr_ip4 == NULL) {
2717		mtx_unlock(&pr->pr_mtx);
2718		return (EAFNOSUPPORT);
2719	}
2720
2721	ia0.s_addr = ntohl(ia->s_addr);
2722	if (ia0.s_addr == INADDR_LOOPBACK) {
2723		ia->s_addr = pr->pr_ip4[0].s_addr;
2724		mtx_unlock(&pr->pr_mtx);
2725		return (0);
2726	}
2727
2728	if (ia0.s_addr == INADDR_ANY) {
2729		/*
2730		 * In case there is only 1 IPv4 address, bind directly.
2731		 */
2732		if (pr->pr_ip4s == 1)
2733			ia->s_addr = pr->pr_ip4[0].s_addr;
2734		mtx_unlock(&pr->pr_mtx);
2735		return (0);
2736	}
2737
2738	error = _prison_check_ip4(pr, ia);
2739	mtx_unlock(&pr->pr_mtx);
2740	return (error);
2741}
2742
2743/*
2744 * Rewrite destination address in case we will connect to loopback address.
2745 *
2746 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2747 * Address passed in in NBO and returned in NBO.
2748 */
2749int
2750prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2751{
2752	struct prison *pr;
2753
2754	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2755	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2756
2757	pr = cred->cr_prison;
2758	if (!(pr->pr_flags & PR_IP4))
2759		return (0);
2760	mtx_lock(&pr->pr_mtx);
2761	if (!(pr->pr_flags & PR_IP4)) {
2762		mtx_unlock(&pr->pr_mtx);
2763		return (0);
2764	}
2765	if (pr->pr_ip4 == NULL) {
2766		mtx_unlock(&pr->pr_mtx);
2767		return (EAFNOSUPPORT);
2768	}
2769
2770	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2771		ia->s_addr = pr->pr_ip4[0].s_addr;
2772		mtx_unlock(&pr->pr_mtx);
2773		return (0);
2774	}
2775
2776	/*
2777	 * Return success because nothing had to be changed.
2778	 */
2779	mtx_unlock(&pr->pr_mtx);
2780	return (0);
2781}
2782
2783/*
2784 * Check if given address belongs to the jail referenced by cred/prison.
2785 *
2786 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2787 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2788 * doesn't allow IPv4.  Address passed in in NBO.
2789 */
2790static int
2791_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2792{
2793	int i, a, z, d;
2794
2795	/*
2796	 * Check the primary IP.
2797	 */
2798	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2799		return (0);
2800
2801	/*
2802	 * All the other IPs are sorted so we can do a binary search.
2803	 */
2804	a = 0;
2805	z = pr->pr_ip4s - 2;
2806	while (a <= z) {
2807		i = (a + z) / 2;
2808		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2809		if (d > 0)
2810			z = i - 1;
2811		else if (d < 0)
2812			a = i + 1;
2813		else
2814			return (0);
2815	}
2816
2817	return (EADDRNOTAVAIL);
2818}
2819
2820int
2821prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2822{
2823	struct prison *pr;
2824	int error;
2825
2826	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2827	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2828
2829	pr = cred->cr_prison;
2830	if (!(pr->pr_flags & PR_IP4))
2831		return (0);
2832	mtx_lock(&pr->pr_mtx);
2833	if (!(pr->pr_flags & PR_IP4)) {
2834		mtx_unlock(&pr->pr_mtx);
2835		return (0);
2836	}
2837	if (pr->pr_ip4 == NULL) {
2838		mtx_unlock(&pr->pr_mtx);
2839		return (EAFNOSUPPORT);
2840	}
2841
2842	error = _prison_check_ip4(pr, ia);
2843	mtx_unlock(&pr->pr_mtx);
2844	return (error);
2845}
2846#endif
2847
2848#ifdef INET6
2849static int
2850prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2851{
2852	int ii, ij, used;
2853	struct prison *ppr;
2854
2855	ppr = pr->pr_parent;
2856	if (!(pr->pr_flags & PR_IP6_USER)) {
2857		/* This has no user settings, so just copy the parent's list. */
2858		if (pr->pr_ip6s < ppr->pr_ip6s) {
2859			/*
2860			 * There's no room for the parent's list.  Use the
2861			 * new list buffer, which is assumed to be big enough
2862			 * (if it was passed).  If there's no buffer, try to
2863			 * allocate one.
2864			 */
2865			used = 1;
2866			if (newip6 == NULL) {
2867				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2868				    M_PRISON, M_NOWAIT);
2869				if (newip6 != NULL)
2870					used = 0;
2871			}
2872			if (newip6 != NULL) {
2873				bcopy(ppr->pr_ip6, newip6,
2874				    ppr->pr_ip6s * sizeof(*newip6));
2875				free(pr->pr_ip6, M_PRISON);
2876				pr->pr_ip6 = newip6;
2877				pr->pr_ip6s = ppr->pr_ip6s;
2878			}
2879			return (used);
2880		}
2881		pr->pr_ip6s = ppr->pr_ip6s;
2882		if (pr->pr_ip6s > 0)
2883			bcopy(ppr->pr_ip6, pr->pr_ip6,
2884			    pr->pr_ip6s * sizeof(*newip6));
2885		else if (pr->pr_ip6 != NULL) {
2886			free(pr->pr_ip6, M_PRISON);
2887			pr->pr_ip6 = NULL;
2888		}
2889	} else if (pr->pr_ip6s > 0) {
2890		/* Remove addresses that aren't in the parent. */
2891		for (ij = 0; ij < ppr->pr_ip6s; ij++)
2892			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2893			    &ppr->pr_ip6[ij]))
2894				break;
2895		if (ij < ppr->pr_ip6s)
2896			ii = 1;
2897		else {
2898			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2899			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2900			ii = 0;
2901		}
2902		for (ij = 1; ii < pr->pr_ip6s; ) {
2903			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2904			    &ppr->pr_ip6[0])) {
2905				ii++;
2906				continue;
2907			}
2908			switch (ij >= ppr->pr_ip4s ? -1 :
2909				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2910			case -1:
2911				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2912				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2913				break;
2914			case 0:
2915				ii++;
2916				ij++;
2917				break;
2918			case 1:
2919				ij++;
2920				break;
2921			}
2922		}
2923		if (pr->pr_ip6s == 0) {
2924			pr->pr_flags |= PR_IP6_DISABLE;
2925			free(pr->pr_ip6, M_PRISON);
2926			pr->pr_ip6 = NULL;
2927		}
2928	}
2929	return 0;
2930}
2931
2932/*
2933 * Pass back primary IPv6 address for this jail.
2934 *
2935 * If not restricted return success but do not alter the address.  Caller has
2936 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2937 *
2938 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2939 */
2940int
2941prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2942{
2943	struct prison *pr;
2944
2945	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2946	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2947
2948	pr = cred->cr_prison;
2949	if (!(pr->pr_flags & PR_IP6))
2950		return (0);
2951	mtx_lock(&pr->pr_mtx);
2952	if (!(pr->pr_flags & PR_IP6)) {
2953		mtx_unlock(&pr->pr_mtx);
2954		return (0);
2955	}
2956	if (pr->pr_ip6 == NULL) {
2957		mtx_unlock(&pr->pr_mtx);
2958		return (EAFNOSUPPORT);
2959	}
2960
2961	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2962	mtx_unlock(&pr->pr_mtx);
2963	return (0);
2964}
2965
2966/*
2967 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
2968 */
2969int
2970prison_equal_ip6(struct prison *pr1, struct prison *pr2)
2971{
2972
2973	if (pr1 == pr2)
2974		return (1);
2975
2976	while (pr1 != &prison0 &&
2977#ifdef VIMAGE
2978	       !(pr1->pr_flags & PR_VNET) &&
2979#endif
2980	       !(pr1->pr_flags & PR_IP6_USER))
2981		pr1 = pr1->pr_parent;
2982	while (pr2 != &prison0 &&
2983#ifdef VIMAGE
2984	       !(pr2->pr_flags & PR_VNET) &&
2985#endif
2986	       !(pr2->pr_flags & PR_IP6_USER))
2987		pr2 = pr2->pr_parent;
2988	return (pr1 == pr2);
2989}
2990
2991/*
2992 * Make sure our (source) address is set to something meaningful to this jail.
2993 *
2994 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2995 * when needed while binding.
2996 *
2997 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
2998 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2999 * doesn't allow IPv6.
3000 */
3001int
3002prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3003{
3004	struct prison *pr;
3005	int error;
3006
3007	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3008	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3009
3010	pr = cred->cr_prison;
3011	if (!(pr->pr_flags & PR_IP6))
3012		return (0);
3013	mtx_lock(&pr->pr_mtx);
3014	if (!(pr->pr_flags & PR_IP6)) {
3015		mtx_unlock(&pr->pr_mtx);
3016		return (0);
3017	}
3018	if (pr->pr_ip6 == NULL) {
3019		mtx_unlock(&pr->pr_mtx);
3020		return (EAFNOSUPPORT);
3021	}
3022
3023	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3024		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3025		mtx_unlock(&pr->pr_mtx);
3026		return (0);
3027	}
3028
3029	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3030		/*
3031		 * In case there is only 1 IPv6 address, and v6only is true,
3032		 * then bind directly.
3033		 */
3034		if (v6only != 0 && pr->pr_ip6s == 1)
3035			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3036		mtx_unlock(&pr->pr_mtx);
3037		return (0);
3038	}
3039
3040	error = _prison_check_ip6(pr, ia6);
3041	mtx_unlock(&pr->pr_mtx);
3042	return (error);
3043}
3044
3045/*
3046 * Rewrite destination address in case we will connect to loopback address.
3047 *
3048 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3049 */
3050int
3051prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3052{
3053	struct prison *pr;
3054
3055	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3056	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3057
3058	pr = cred->cr_prison;
3059	if (!(pr->pr_flags & PR_IP6))
3060		return (0);
3061	mtx_lock(&pr->pr_mtx);
3062	if (!(pr->pr_flags & PR_IP6)) {
3063		mtx_unlock(&pr->pr_mtx);
3064		return (0);
3065	}
3066	if (pr->pr_ip6 == NULL) {
3067		mtx_unlock(&pr->pr_mtx);
3068		return (EAFNOSUPPORT);
3069	}
3070
3071	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3072		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3073		mtx_unlock(&pr->pr_mtx);
3074		return (0);
3075	}
3076
3077	/*
3078	 * Return success because nothing had to be changed.
3079	 */
3080	mtx_unlock(&pr->pr_mtx);
3081	return (0);
3082}
3083
3084/*
3085 * Check if given address belongs to the jail referenced by cred/prison.
3086 *
3087 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3088 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3089 * doesn't allow IPv6.
3090 */
3091static int
3092_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3093{
3094	int i, a, z, d;
3095
3096	/*
3097	 * Check the primary IP.
3098	 */
3099	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3100		return (0);
3101
3102	/*
3103	 * All the other IPs are sorted so we can do a binary search.
3104	 */
3105	a = 0;
3106	z = pr->pr_ip6s - 2;
3107	while (a <= z) {
3108		i = (a + z) / 2;
3109		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3110		if (d > 0)
3111			z = i - 1;
3112		else if (d < 0)
3113			a = i + 1;
3114		else
3115			return (0);
3116	}
3117
3118	return (EADDRNOTAVAIL);
3119}
3120
3121int
3122prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3123{
3124	struct prison *pr;
3125	int error;
3126
3127	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3128	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3129
3130	pr = cred->cr_prison;
3131	if (!(pr->pr_flags & PR_IP6))
3132		return (0);
3133	mtx_lock(&pr->pr_mtx);
3134	if (!(pr->pr_flags & PR_IP6)) {
3135		mtx_unlock(&pr->pr_mtx);
3136		return (0);
3137	}
3138	if (pr->pr_ip6 == NULL) {
3139		mtx_unlock(&pr->pr_mtx);
3140		return (EAFNOSUPPORT);
3141	}
3142
3143	error = _prison_check_ip6(pr, ia6);
3144	mtx_unlock(&pr->pr_mtx);
3145	return (error);
3146}
3147#endif
3148
3149/*
3150 * Check if a jail supports the given address family.
3151 *
3152 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3153 * if not.
3154 */
3155int
3156prison_check_af(struct ucred *cred, int af)
3157{
3158	struct prison *pr;
3159	int error;
3160
3161	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3162
3163	pr = cred->cr_prison;
3164#ifdef VIMAGE
3165	/* Prisons with their own network stack are not limited. */
3166	if (prison_owns_vnet(cred))
3167		return (0);
3168#endif
3169
3170	error = 0;
3171	switch (af)
3172	{
3173#ifdef INET
3174	case AF_INET:
3175		if (pr->pr_flags & PR_IP4)
3176		{
3177			mtx_lock(&pr->pr_mtx);
3178			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3179				error = EAFNOSUPPORT;
3180			mtx_unlock(&pr->pr_mtx);
3181		}
3182		break;
3183#endif
3184#ifdef INET6
3185	case AF_INET6:
3186		if (pr->pr_flags & PR_IP6)
3187		{
3188			mtx_lock(&pr->pr_mtx);
3189			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3190				error = EAFNOSUPPORT;
3191			mtx_unlock(&pr->pr_mtx);
3192		}
3193		break;
3194#endif
3195	case AF_LOCAL:
3196	case AF_ROUTE:
3197		break;
3198	default:
3199		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3200			error = EAFNOSUPPORT;
3201	}
3202	return (error);
3203}
3204
3205/*
3206 * Check if given address belongs to the jail referenced by cred (wrapper to
3207 * prison_check_ip[46]).
3208 *
3209 * Returns 0 if jail doesn't restrict the address family or if address belongs
3210 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3211 * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3212 */
3213int
3214prison_if(struct ucred *cred, struct sockaddr *sa)
3215{
3216#ifdef INET
3217	struct sockaddr_in *sai;
3218#endif
3219#ifdef INET6
3220	struct sockaddr_in6 *sai6;
3221#endif
3222	int error;
3223
3224	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3225	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3226
3227#ifdef VIMAGE
3228	if (prison_owns_vnet(cred))
3229		return (0);
3230#endif
3231
3232	error = 0;
3233	switch (sa->sa_family)
3234	{
3235#ifdef INET
3236	case AF_INET:
3237		sai = (struct sockaddr_in *)sa;
3238		error = prison_check_ip4(cred, &sai->sin_addr);
3239		break;
3240#endif
3241#ifdef INET6
3242	case AF_INET6:
3243		sai6 = (struct sockaddr_in6 *)sa;
3244		error = prison_check_ip6(cred, &sai6->sin6_addr);
3245		break;
3246#endif
3247	default:
3248		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3249			error = EAFNOSUPPORT;
3250	}
3251	return (error);
3252}
3253
3254/*
3255 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3256 */
3257int
3258prison_check(struct ucred *cred1, struct ucred *cred2)
3259{
3260
3261	return ((cred1->cr_prison == cred2->cr_prison ||
3262	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3263}
3264
3265/*
3266 * Return 1 if p2 is a child of p1, otherwise 0.
3267 */
3268int
3269prison_ischild(struct prison *pr1, struct prison *pr2)
3270{
3271
3272	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3273		if (pr1 == pr2)
3274			return (1);
3275	return (0);
3276}
3277
3278/*
3279 * Return 1 if the passed credential is in a jail, otherwise 0.
3280 */
3281int
3282jailed(struct ucred *cred)
3283{
3284
3285	return (cred->cr_prison != &prison0);
3286}
3287
3288/*
3289 * Return 1 if the passed credential is in a jail and that jail does not
3290 * have its own virtual network stack, otherwise 0.
3291 */
3292int
3293jailed_without_vnet(struct ucred *cred)
3294{
3295
3296	if (!jailed(cred))
3297		return (0);
3298#ifdef VIMAGE
3299	if (prison_owns_vnet(cred))
3300		return (0);
3301#endif
3302
3303	return (1);
3304}
3305
3306/*
3307 * Return the correct hostname (domainname, et al) for the passed credential.
3308 */
3309void
3310getcredhostname(struct ucred *cred, char *buf, size_t size)
3311{
3312	struct prison *pr;
3313
3314	/*
3315	 * A NULL credential can be used to shortcut to the physical
3316	 * system's hostname.
3317	 */
3318	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3319	mtx_lock(&pr->pr_mtx);
3320	strlcpy(buf, pr->pr_hostname, size);
3321	mtx_unlock(&pr->pr_mtx);
3322}
3323
3324void
3325getcreddomainname(struct ucred *cred, char *buf, size_t size)
3326{
3327
3328	mtx_lock(&cred->cr_prison->pr_mtx);
3329	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3330	mtx_unlock(&cred->cr_prison->pr_mtx);
3331}
3332
3333void
3334getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3335{
3336
3337	mtx_lock(&cred->cr_prison->pr_mtx);
3338	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3339	mtx_unlock(&cred->cr_prison->pr_mtx);
3340}
3341
3342void
3343getcredhostid(struct ucred *cred, unsigned long *hostid)
3344{
3345
3346	mtx_lock(&cred->cr_prison->pr_mtx);
3347	*hostid = cred->cr_prison->pr_hostid;
3348	mtx_unlock(&cred->cr_prison->pr_mtx);
3349}
3350
3351#ifdef VIMAGE
3352/*
3353 * Determine whether the prison represented by cred owns
3354 * its vnet rather than having it inherited.
3355 *
3356 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3357 */
3358int
3359prison_owns_vnet(struct ucred *cred)
3360{
3361
3362	/*
3363	 * vnets cannot be added/removed after jail creation,
3364	 * so no need to lock here.
3365	 */
3366	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3367}
3368#endif
3369
3370/*
3371 * Determine whether the subject represented by cred can "see"
3372 * status of a mount point.
3373 * Returns: 0 for permitted, ENOENT otherwise.
3374 * XXX: This function should be called cr_canseemount() and should be
3375 *      placed in kern_prot.c.
3376 */
3377int
3378prison_canseemount(struct ucred *cred, struct mount *mp)
3379{
3380	struct prison *pr;
3381	struct statfs *sp;
3382	size_t len;
3383
3384	pr = cred->cr_prison;
3385	if (pr->pr_enforce_statfs == 0)
3386		return (0);
3387	if (pr->pr_root->v_mount == mp)
3388		return (0);
3389	if (pr->pr_enforce_statfs == 2)
3390		return (ENOENT);
3391	/*
3392	 * If jail's chroot directory is set to "/" we should be able to see
3393	 * all mount-points from inside a jail.
3394	 * This is ugly check, but this is the only situation when jail's
3395	 * directory ends with '/'.
3396	 */
3397	if (strcmp(pr->pr_path, "/") == 0)
3398		return (0);
3399	len = strlen(pr->pr_path);
3400	sp = &mp->mnt_stat;
3401	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3402		return (ENOENT);
3403	/*
3404	 * Be sure that we don't have situation where jail's root directory
3405	 * is "/some/path" and mount point is "/some/pathpath".
3406	 */
3407	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3408		return (ENOENT);
3409	return (0);
3410}
3411
3412void
3413prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3414{
3415	char jpath[MAXPATHLEN];
3416	struct prison *pr;
3417	size_t len;
3418
3419	pr = cred->cr_prison;
3420	if (pr->pr_enforce_statfs == 0)
3421		return;
3422	if (prison_canseemount(cred, mp) != 0) {
3423		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3424		strlcpy(sp->f_mntonname, "[restricted]",
3425		    sizeof(sp->f_mntonname));
3426		return;
3427	}
3428	if (pr->pr_root->v_mount == mp) {
3429		/*
3430		 * Clear current buffer data, so we are sure nothing from
3431		 * the valid path left there.
3432		 */
3433		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3434		*sp->f_mntonname = '/';
3435		return;
3436	}
3437	/*
3438	 * If jail's chroot directory is set to "/" we should be able to see
3439	 * all mount-points from inside a jail.
3440	 */
3441	if (strcmp(pr->pr_path, "/") == 0)
3442		return;
3443	len = strlen(pr->pr_path);
3444	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3445	/*
3446	 * Clear current buffer data, so we are sure nothing from
3447	 * the valid path left there.
3448	 */
3449	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3450	if (*jpath == '\0') {
3451		/* Should never happen. */
3452		*sp->f_mntonname = '/';
3453	} else {
3454		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3455	}
3456}
3457
3458/*
3459 * Check with permission for a specific privilege is granted within jail.  We
3460 * have a specific list of accepted privileges; the rest are denied.
3461 */
3462int
3463prison_priv_check(struct ucred *cred, int priv)
3464{
3465
3466	if (!jailed(cred))
3467		return (0);
3468
3469#ifdef VIMAGE
3470	/*
3471	 * Privileges specific to prisons with a virtual network stack.
3472	 * There might be a duplicate entry here in case the privilege
3473	 * is only granted conditionally in the legacy jail case.
3474	 */
3475	switch (priv) {
3476#ifdef notyet
3477		/*
3478		 * NFS-specific privileges.
3479		 */
3480	case PRIV_NFS_DAEMON:
3481	case PRIV_NFS_LOCKD:
3482#endif
3483		/*
3484		 * Network stack privileges.
3485		 */
3486	case PRIV_NET_BRIDGE:
3487	case PRIV_NET_GRE:
3488	case PRIV_NET_BPF:
3489	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3490	case PRIV_NET_ROUTE:
3491	case PRIV_NET_TAP:
3492	case PRIV_NET_SETIFMTU:
3493	case PRIV_NET_SETIFFLAGS:
3494	case PRIV_NET_SETIFCAP:
3495	case PRIV_NET_SETIFNAME	:
3496	case PRIV_NET_SETIFMETRIC:
3497	case PRIV_NET_SETIFPHYS:
3498	case PRIV_NET_SETIFMAC:
3499	case PRIV_NET_ADDMULTI:
3500	case PRIV_NET_DELMULTI:
3501	case PRIV_NET_HWIOCTL:
3502	case PRIV_NET_SETLLADDR:
3503	case PRIV_NET_ADDIFGROUP:
3504	case PRIV_NET_DELIFGROUP:
3505	case PRIV_NET_IFCREATE:
3506	case PRIV_NET_IFDESTROY:
3507	case PRIV_NET_ADDIFADDR:
3508	case PRIV_NET_DELIFADDR:
3509	case PRIV_NET_LAGG:
3510	case PRIV_NET_GIF:
3511	case PRIV_NET_SETIFVNET:
3512
3513		/*
3514		 * 802.11-related privileges.
3515		 */
3516	case PRIV_NET80211_GETKEY:
3517#ifdef notyet
3518	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3519#endif
3520
3521#ifdef notyet
3522		/*
3523		 * AppleTalk privileges.
3524		 */
3525	case PRIV_NETATALK_RESERVEDPORT:
3526
3527		/*
3528		 * ATM privileges.
3529		 */
3530	case PRIV_NETATM_CFG:
3531	case PRIV_NETATM_ADD:
3532	case PRIV_NETATM_DEL:
3533	case PRIV_NETATM_SET:
3534
3535		/*
3536		 * Bluetooth privileges.
3537		 */
3538	case PRIV_NETBLUETOOTH_RAW:
3539#endif
3540
3541		/*
3542		 * Netgraph and netgraph module privileges.
3543		 */
3544	case PRIV_NETGRAPH_CONTROL:
3545#ifdef notyet
3546	case PRIV_NETGRAPH_TTY:
3547#endif
3548
3549		/*
3550		 * IPv4 and IPv6 privileges.
3551		 */
3552	case PRIV_NETINET_IPFW:
3553	case PRIV_NETINET_DIVERT:
3554	case PRIV_NETINET_PF:
3555	case PRIV_NETINET_DUMMYNET:
3556	case PRIV_NETINET_CARP:
3557	case PRIV_NETINET_MROUTE:
3558	case PRIV_NETINET_RAW:
3559	case PRIV_NETINET_ADDRCTRL6:
3560	case PRIV_NETINET_ND6:
3561	case PRIV_NETINET_SCOPE6:
3562	case PRIV_NETINET_ALIFETIME6:
3563	case PRIV_NETINET_IPSEC:
3564	case PRIV_NETINET_BINDANY:
3565
3566#ifdef notyet
3567		/*
3568		 * IPX/SPX privileges.
3569		 */
3570	case PRIV_NETIPX_RESERVEDPORT:
3571	case PRIV_NETIPX_RAW:
3572
3573		/*
3574		 * NCP privileges.
3575		 */
3576	case PRIV_NETNCP:
3577
3578		/*
3579		 * SMB privileges.
3580		 */
3581	case PRIV_NETSMB:
3582#endif
3583
3584	/*
3585	 * No default: or deny here.
3586	 * In case of no permit fall through to next switch().
3587	 */
3588		if (cred->cr_prison->pr_flags & PR_VNET)
3589			return (0);
3590	}
3591#endif /* VIMAGE */
3592
3593	switch (priv) {
3594
3595		/*
3596		 * Allow ktrace privileges for root in jail.
3597		 */
3598	case PRIV_KTRACE:
3599
3600#if 0
3601		/*
3602		 * Allow jailed processes to configure audit identity and
3603		 * submit audit records (login, etc).  In the future we may
3604		 * want to further refine the relationship between audit and
3605		 * jail.
3606		 */
3607	case PRIV_AUDIT_GETAUDIT:
3608	case PRIV_AUDIT_SETAUDIT:
3609	case PRIV_AUDIT_SUBMIT:
3610#endif
3611
3612		/*
3613		 * Allow jailed processes to manipulate process UNIX
3614		 * credentials in any way they see fit.
3615		 */
3616	case PRIV_CRED_SETUID:
3617	case PRIV_CRED_SETEUID:
3618	case PRIV_CRED_SETGID:
3619	case PRIV_CRED_SETEGID:
3620	case PRIV_CRED_SETGROUPS:
3621	case PRIV_CRED_SETREUID:
3622	case PRIV_CRED_SETREGID:
3623	case PRIV_CRED_SETRESUID:
3624	case PRIV_CRED_SETRESGID:
3625
3626		/*
3627		 * Jail implements visibility constraints already, so allow
3628		 * jailed root to override uid/gid-based constraints.
3629		 */
3630	case PRIV_SEEOTHERGIDS:
3631	case PRIV_SEEOTHERUIDS:
3632
3633		/*
3634		 * Jail implements inter-process debugging limits already, so
3635		 * allow jailed root various debugging privileges.
3636		 */
3637	case PRIV_DEBUG_DIFFCRED:
3638	case PRIV_DEBUG_SUGID:
3639	case PRIV_DEBUG_UNPRIV:
3640
3641		/*
3642		 * Allow jail to set various resource limits and login
3643		 * properties, and for now, exceed process resource limits.
3644		 */
3645	case PRIV_PROC_LIMIT:
3646	case PRIV_PROC_SETLOGIN:
3647	case PRIV_PROC_SETRLIMIT:
3648
3649		/*
3650		 * System V and POSIX IPC privileges are granted in jail.
3651		 */
3652	case PRIV_IPC_READ:
3653	case PRIV_IPC_WRITE:
3654	case PRIV_IPC_ADMIN:
3655	case PRIV_IPC_MSGSIZE:
3656	case PRIV_MQ_ADMIN:
3657
3658		/*
3659		 * Jail operations within a jail work on child jails.
3660		 */
3661	case PRIV_JAIL_ATTACH:
3662	case PRIV_JAIL_SET:
3663	case PRIV_JAIL_REMOVE:
3664
3665		/*
3666		 * Jail implements its own inter-process limits, so allow
3667		 * root processes in jail to change scheduling on other
3668		 * processes in the same jail.  Likewise for signalling.
3669		 */
3670	case PRIV_SCHED_DIFFCRED:
3671	case PRIV_SCHED_CPUSET:
3672	case PRIV_SIGNAL_DIFFCRED:
3673	case PRIV_SIGNAL_SUGID:
3674
3675		/*
3676		 * Allow jailed processes to write to sysctls marked as jail
3677		 * writable.
3678		 */
3679	case PRIV_SYSCTL_WRITEJAIL:
3680
3681		/*
3682		 * Allow root in jail to manage a variety of quota
3683		 * properties.  These should likely be conditional on a
3684		 * configuration option.
3685		 */
3686	case PRIV_VFS_GETQUOTA:
3687	case PRIV_VFS_SETQUOTA:
3688
3689		/*
3690		 * Since Jail relies on chroot() to implement file system
3691		 * protections, grant many VFS privileges to root in jail.
3692		 * Be careful to exclude mount-related and NFS-related
3693		 * privileges.
3694		 */
3695	case PRIV_VFS_READ:
3696	case PRIV_VFS_WRITE:
3697	case PRIV_VFS_ADMIN:
3698	case PRIV_VFS_EXEC:
3699	case PRIV_VFS_LOOKUP:
3700	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3701	case PRIV_VFS_CHFLAGS_DEV:
3702	case PRIV_VFS_CHOWN:
3703	case PRIV_VFS_CHROOT:
3704	case PRIV_VFS_RETAINSUGID:
3705	case PRIV_VFS_FCHROOT:
3706	case PRIV_VFS_LINK:
3707	case PRIV_VFS_SETGID:
3708	case PRIV_VFS_STAT:
3709	case PRIV_VFS_STICKYFILE:
3710		return (0);
3711
3712		/*
3713		 * Depending on the global setting, allow privilege of
3714		 * setting system flags.
3715		 */
3716	case PRIV_VFS_SYSFLAGS:
3717		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3718			return (0);
3719		else
3720			return (EPERM);
3721
3722		/*
3723		 * Depending on the global setting, allow privilege of
3724		 * mounting/unmounting file systems.
3725		 */
3726	case PRIV_VFS_MOUNT:
3727	case PRIV_VFS_UNMOUNT:
3728	case PRIV_VFS_MOUNT_NONUSER:
3729	case PRIV_VFS_MOUNT_OWNER:
3730		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3731			return (0);
3732		else
3733			return (EPERM);
3734
3735		/*
3736		 * Allow jailed root to bind reserved ports and reuse in-use
3737		 * ports.
3738		 */
3739	case PRIV_NETINET_RESERVEDPORT:
3740	case PRIV_NETINET_REUSEPORT:
3741		return (0);
3742
3743		/*
3744		 * Allow jailed root to set certian IPv4/6 (option) headers.
3745		 */
3746	case PRIV_NETINET_SETHDROPTS:
3747		return (0);
3748
3749		/*
3750		 * Conditionally allow creating raw sockets in jail.
3751		 */
3752	case PRIV_NETINET_RAW:
3753		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3754			return (0);
3755		else
3756			return (EPERM);
3757
3758		/*
3759		 * Since jail implements its own visibility limits on netstat
3760		 * sysctls, allow getcred.  This allows identd to work in
3761		 * jail.
3762		 */
3763	case PRIV_NETINET_GETCRED:
3764		return (0);
3765
3766	default:
3767		/*
3768		 * In all remaining cases, deny the privilege request.  This
3769		 * includes almost all network privileges, many system
3770		 * configuration privileges.
3771		 */
3772		return (EPERM);
3773	}
3774}
3775
3776/*
3777 * Return the part of pr2's name that is relative to pr1, or the whole name
3778 * if it does not directly follow.
3779 */
3780
3781char *
3782prison_name(struct prison *pr1, struct prison *pr2)
3783{
3784	char *name;
3785
3786	/* Jails see themselves as "0" (if they see themselves at all). */
3787	if (pr1 == pr2)
3788		return "0";
3789	name = pr2->pr_name;
3790	if (prison_ischild(pr1, pr2)) {
3791		/*
3792		 * pr1 isn't locked (and allprison_lock may not be either)
3793		 * so its length can't be counted on.  But the number of dots
3794		 * can be counted on - and counted.
3795		 */
3796		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3797			name = strchr(name, '.') + 1;
3798	}
3799	return (name);
3800}
3801
3802/*
3803 * Return the part of pr2's path that is relative to pr1, or the whole path
3804 * if it does not directly follow.
3805 */
3806static char *
3807prison_path(struct prison *pr1, struct prison *pr2)
3808{
3809	char *path1, *path2;
3810	int len1;
3811
3812	path1 = pr1->pr_path;
3813	path2 = pr2->pr_path;
3814	if (!strcmp(path1, "/"))
3815		return (path2);
3816	len1 = strlen(path1);
3817	if (strncmp(path1, path2, len1))
3818		return (path2);
3819	if (path2[len1] == '\0')
3820		return "/";
3821	if (path2[len1] == '/')
3822		return (path2 + len1);
3823	return (path2);
3824}
3825
3826
3827/*
3828 * Jail-related sysctls.
3829 */
3830SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3831    "Jails");
3832
3833static int
3834sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3835{
3836	struct xprison *xp;
3837	struct prison *pr, *cpr;
3838#ifdef INET
3839	struct in_addr *ip4 = NULL;
3840	int ip4s = 0;
3841#endif
3842#ifdef INET6
3843	struct in_addr *ip6 = NULL;
3844	int ip6s = 0;
3845#endif
3846	int descend, error;
3847
3848	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3849	pr = req->td->td_ucred->cr_prison;
3850	error = 0;
3851	sx_slock(&allprison_lock);
3852	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3853#if defined(INET) || defined(INET6)
3854 again:
3855#endif
3856		mtx_lock(&cpr->pr_mtx);
3857#ifdef INET
3858		if (cpr->pr_ip4s > 0) {
3859			if (ip4s < cpr->pr_ip4s) {
3860				ip4s = cpr->pr_ip4s;
3861				mtx_unlock(&cpr->pr_mtx);
3862				ip4 = realloc(ip4, ip4s *
3863				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3864				goto again;
3865			}
3866			bcopy(cpr->pr_ip4, ip4,
3867			    cpr->pr_ip4s * sizeof(struct in_addr));
3868		}
3869#endif
3870#ifdef INET6
3871		if (cpr->pr_ip6s > 0) {
3872			if (ip6s < cpr->pr_ip6s) {
3873				ip6s = cpr->pr_ip6s;
3874				mtx_unlock(&cpr->pr_mtx);
3875				ip6 = realloc(ip6, ip6s *
3876				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3877				goto again;
3878			}
3879			bcopy(cpr->pr_ip6, ip6,
3880			    cpr->pr_ip6s * sizeof(struct in6_addr));
3881		}
3882#endif
3883		if (cpr->pr_ref == 0) {
3884			mtx_unlock(&cpr->pr_mtx);
3885			continue;
3886		}
3887		bzero(xp, sizeof(*xp));
3888		xp->pr_version = XPRISON_VERSION;
3889		xp->pr_id = cpr->pr_id;
3890		xp->pr_state = cpr->pr_uref > 0
3891		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3892		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3893		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3894		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3895#ifdef INET
3896		xp->pr_ip4s = cpr->pr_ip4s;
3897#endif
3898#ifdef INET6
3899		xp->pr_ip6s = cpr->pr_ip6s;
3900#endif
3901		mtx_unlock(&cpr->pr_mtx);
3902		error = SYSCTL_OUT(req, xp, sizeof(*xp));
3903		if (error)
3904			break;
3905#ifdef INET
3906		if (xp->pr_ip4s > 0) {
3907			error = SYSCTL_OUT(req, ip4,
3908			    xp->pr_ip4s * sizeof(struct in_addr));
3909			if (error)
3910				break;
3911		}
3912#endif
3913#ifdef INET6
3914		if (xp->pr_ip6s > 0) {
3915			error = SYSCTL_OUT(req, ip6,
3916			    xp->pr_ip6s * sizeof(struct in6_addr));
3917			if (error)
3918				break;
3919		}
3920#endif
3921	}
3922	sx_sunlock(&allprison_lock);
3923	free(xp, M_TEMP);
3924#ifdef INET
3925	free(ip4, M_TEMP);
3926#endif
3927#ifdef INET6
3928	free(ip6, M_TEMP);
3929#endif
3930	return (error);
3931}
3932
3933SYSCTL_OID(_security_jail, OID_AUTO, list,
3934    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3935    sysctl_jail_list, "S", "List of active jails");
3936
3937static int
3938sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3939{
3940	int error, injail;
3941
3942	injail = jailed(req->td->td_ucred);
3943	error = SYSCTL_OUT(req, &injail, sizeof(injail));
3944
3945	return (error);
3946}
3947
3948SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3949    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3950    sysctl_jail_jailed, "I", "Process in jail?");
3951
3952#if defined(INET) || defined(INET6)
3953SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3954    &jail_max_af_ips, 0,
3955    "Number of IP addresses a jail may have at most per address family");
3956#endif
3957
3958/*
3959 * Default parameters for jail(2) compatability.  For historical reasons,
3960 * the sysctl names have varying similarity to the parameter names.  Prisons
3961 * just see their own parameters, and can't change them.
3962 */
3963static int
3964sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3965{
3966	struct prison *pr;
3967	int allow, error, i;
3968
3969	pr = req->td->td_ucred->cr_prison;
3970	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
3971
3972	/* Get the current flag value, and convert it to a boolean. */
3973	i = (allow & arg2) ? 1 : 0;
3974	if (arg1 != NULL)
3975		i = !i;
3976	error = sysctl_handle_int(oidp, &i, 0, req);
3977	if (error || !req->newptr)
3978		return (error);
3979	i = i ? arg2 : 0;
3980	if (arg1 != NULL)
3981		i ^= arg2;
3982	/*
3983	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3984	 * for writing.
3985	 */
3986	mtx_lock(&prison0.pr_mtx);
3987	jail_default_allow = (jail_default_allow & ~arg2) | i;
3988	mtx_unlock(&prison0.pr_mtx);
3989	return (0);
3990}
3991
3992SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3993    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3994    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3995    "Processes in jail can set their hostnames");
3996SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3997    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3998    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3999    "Processes in jail are limited to creating UNIX/IP/route sockets only");
4000SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4001    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4002    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4003    "Processes in jail can use System V IPC primitives");
4004SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4005    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4006    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4007    "Prison root can create raw sockets");
4008SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4009    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4010    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4011    "Processes in jail can alter system file flags");
4012SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4013    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4014    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4015    "Processes in jail can mount/unmount jail-friendly file systems");
4016
4017static int
4018sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4019{
4020	struct prison *pr;
4021	int level, error;
4022
4023	pr = req->td->td_ucred->cr_prison;
4024	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4025	error = sysctl_handle_int(oidp, &level, 0, req);
4026	if (error || !req->newptr)
4027		return (error);
4028	*(int *)arg1 = level;
4029	return (0);
4030}
4031
4032SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4033    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4034    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4035    sysctl_jail_default_level, "I",
4036    "Processes in jail cannot see all mounted file systems");
4037
4038/*
4039 * Nodes to describe jail parameters.  Maximum length of string parameters
4040 * is returned in the string itself, and the other parameters exist merely
4041 * to make themselves and their types known.
4042 */
4043SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4044    "Jail parameters");
4045
4046int
4047sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4048{
4049	int i;
4050	long l;
4051	size_t s;
4052	char numbuf[12];
4053
4054	switch (oidp->oid_kind & CTLTYPE)
4055	{
4056	case CTLTYPE_LONG:
4057	case CTLTYPE_ULONG:
4058		l = 0;
4059#ifdef SCTL_MASK32
4060		if (!(req->flags & SCTL_MASK32))
4061#endif
4062			return (SYSCTL_OUT(req, &l, sizeof(l)));
4063	case CTLTYPE_INT:
4064	case CTLTYPE_UINT:
4065		i = 0;
4066		return (SYSCTL_OUT(req, &i, sizeof(i)));
4067	case CTLTYPE_STRING:
4068		snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4069		return
4070		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4071	case CTLTYPE_STRUCT:
4072		s = (size_t)arg2;
4073		return (SYSCTL_OUT(req, &s, sizeof(s)));
4074	}
4075	return (0);
4076}
4077
4078SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4079SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4080SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4081SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4082SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4083    "I", "Jail secure level");
4084SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4085    "I", "Jail cannot see all mounted file systems");
4086SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4087    "B", "Jail persistence");
4088#ifdef VIMAGE
4089SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4090    "E,jailsys", "Virtual network stack");
4091#endif
4092SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4093    "B", "Jail is in the process of shutting down");
4094
4095SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4096SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4097    "I", "Current number of child jails");
4098SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4099    "I", "Maximum number of child jails");
4100
4101SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4102SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4103    "Jail hostname");
4104SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4105    "Jail NIS domainname");
4106SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4107    "Jail host UUID");
4108SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4109    "LU", "Jail host ID");
4110
4111SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4112SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4113
4114#ifdef INET
4115SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4116    "Jail IPv4 address virtualization");
4117SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4118    "S,in_addr,a", "Jail IPv4 addresses");
4119#endif
4120#ifdef INET6
4121SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4122    "Jail IPv6 address virtualization");
4123SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4124    "S,in6_addr,a", "Jail IPv6 addresses");
4125#endif
4126
4127SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4128SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4129    "B", "Jail may set hostname");
4130SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4131    "B", "Jail may use SYSV IPC");
4132SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4133    "B", "Jail may create raw sockets");
4134SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4135    "B", "Jail may alter system file flags");
4136SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4137    "B", "Jail may mount/unmount jail-friendly file systems");
4138SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4139    "B", "Jail may set file quotas");
4140SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4141    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4142
4143
4144#ifdef DDB
4145
4146static void
4147db_show_prison(struct prison *pr)
4148{
4149	int fi;
4150#if defined(INET) || defined(INET6)
4151	int ii;
4152#endif
4153	unsigned jsf;
4154#ifdef INET6
4155	char ip6buf[INET6_ADDRSTRLEN];
4156#endif
4157
4158	db_printf("prison %p:\n", pr);
4159	db_printf(" jid             = %d\n", pr->pr_id);
4160	db_printf(" name            = %s\n", pr->pr_name);
4161	db_printf(" parent          = %p\n", pr->pr_parent);
4162	db_printf(" ref             = %d\n", pr->pr_ref);
4163	db_printf(" uref            = %d\n", pr->pr_uref);
4164	db_printf(" path            = %s\n", pr->pr_path);
4165	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4166	    ? pr->pr_cpuset->cs_id : -1);
4167#ifdef VIMAGE
4168	db_printf(" vnet            = %p\n", pr->pr_vnet);
4169#endif
4170	db_printf(" root            = %p\n", pr->pr_root);
4171	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4172	db_printf(" childcount      = %d\n", pr->pr_childcount);
4173	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4174	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4175	db_printf(" flags           = %x", pr->pr_flags);
4176	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4177	    fi++)
4178		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4179			db_printf(" %s", pr_flag_names[fi]);
4180	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4181	    fi++) {
4182		jsf = pr->pr_flags &
4183		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4184		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4185		    pr_flag_jailsys[fi].disable &&
4186		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4187		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4188		    : "inherit");
4189	}
4190	db_printf(" allow           = %x", pr->pr_allow);
4191	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4192	    fi++)
4193		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4194			db_printf(" %s", pr_allow_names[fi]);
4195	db_printf("\n");
4196	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4197	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4198	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4199	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4200	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4201#ifdef INET
4202	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4203	for (ii = 0; ii < pr->pr_ip4s; ii++)
4204		db_printf(" %s %s\n",
4205		    ii == 0 ? "ip4             =" : "                 ",
4206		    inet_ntoa(pr->pr_ip4[ii]));
4207#endif
4208#ifdef INET6
4209	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4210	for (ii = 0; ii < pr->pr_ip6s; ii++)
4211		db_printf(" %s %s\n",
4212		    ii == 0 ? "ip6             =" : "                 ",
4213		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4214#endif
4215}
4216
4217DB_SHOW_COMMAND(prison, db_show_prison_command)
4218{
4219	struct prison *pr;
4220
4221	if (!have_addr) {
4222		/*
4223		 * Show all prisons in the list, and prison0 which is not
4224		 * listed.
4225		 */
4226		db_show_prison(&prison0);
4227		if (!db_pager_quit) {
4228			TAILQ_FOREACH(pr, &allprison, pr_list) {
4229				db_show_prison(pr);
4230				if (db_pager_quit)
4231					break;
4232			}
4233		}
4234		return;
4235	}
4236
4237	if (addr == 0)
4238		pr = &prison0;
4239	else {
4240		/* Look for a prison with the ID and with references. */
4241		TAILQ_FOREACH(pr, &allprison, pr_list)
4242			if (pr->pr_id == addr && pr->pr_ref > 0)
4243				break;
4244		if (pr == NULL)
4245			/* Look again, without requiring a reference. */
4246			TAILQ_FOREACH(pr, &allprison, pr_list)
4247				if (pr->pr_id == addr)
4248					break;
4249		if (pr == NULL)
4250			/* Assume address points to a valid prison. */
4251			pr = (struct prison *)addr;
4252	}
4253	db_show_prison(pr);
4254}
4255
4256#endif /* DDB */
4257