kern_jail.c revision 202468
1139804Simp/*-
2185435Sbz * Copyright (c) 1999 Poul-Henning Kamp.
3185435Sbz * Copyright (c) 2008 Bjoern A. Zeeb.
4191673Sjamie * Copyright (c) 2009 James Gritton.
5185435Sbz * All rights reserved.
6190466Sjamie *
7185404Sbz * Redistribution and use in source and binary forms, with or without
8185404Sbz * modification, are permitted provided that the following conditions
9185404Sbz * are met:
10185404Sbz * 1. Redistributions of source code must retain the above copyright
11185404Sbz *    notice, this list of conditions and the following disclaimer.
12185404Sbz * 2. Redistributions in binary form must reproduce the above copyright
13185404Sbz *    notice, this list of conditions and the following disclaimer in the
14185404Sbz *    documentation and/or other materials provided with the distribution.
15185404Sbz *
16185404Sbz * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17185404Sbz * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18185404Sbz * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19185404Sbz * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20185404Sbz * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21185404Sbz * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22185404Sbz * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23185404Sbz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24185404Sbz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25185404Sbz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26185404Sbz * SUCH DAMAGE.
2746197Sphk */
2846155Sphk
29116182Sobrien#include <sys/cdefs.h>
30116182Sobrien__FBSDID("$FreeBSD: head/sys/kern/kern_jail.c 202468 2010-01-17 12:57:11Z bz $");
31116182Sobrien
32193066Sjamie#include "opt_compat.h"
33185435Sbz#include "opt_ddb.h"
34185435Sbz#include "opt_inet.h"
35185435Sbz#include "opt_inet6.h"
36131177Spjd
3746155Sphk#include <sys/param.h>
3846155Sphk#include <sys/types.h>
3946155Sphk#include <sys/kernel.h>
4046155Sphk#include <sys/systm.h>
4146155Sphk#include <sys/errno.h>
4246155Sphk#include <sys/sysproto.h>
4346155Sphk#include <sys/malloc.h>
44192895Sjamie#include <sys/osd.h>
45164032Srwatson#include <sys/priv.h>
4646155Sphk#include <sys/proc.h>
47124882Srwatson#include <sys/taskqueue.h>
48177785Skib#include <sys/fcntl.h>
4946155Sphk#include <sys/jail.h>
5087275Srwatson#include <sys/lock.h>
5187275Srwatson#include <sys/mutex.h>
52168401Spjd#include <sys/sx.h>
53193066Sjamie#include <sys/sysent.h>
54113275Smike#include <sys/namei.h>
55147185Spjd#include <sys/mount.h>
56113275Smike#include <sys/queue.h>
5746155Sphk#include <sys/socket.h>
58113275Smike#include <sys/syscallsubr.h>
5957163Srwatson#include <sys/sysctl.h>
60113275Smike#include <sys/vnode.h>
61196019Srwatson
6246155Sphk#include <net/if.h>
63196019Srwatson#include <net/vnet.h>
64196019Srwatson
6546155Sphk#include <netinet/in.h>
66196019Srwatson
67185435Sbz#ifdef DDB
68185435Sbz#include <ddb/ddb.h>
69185435Sbz#ifdef INET6
70185435Sbz#include <netinet6/in6_var.h>
71185435Sbz#endif /* INET6 */
72185435Sbz#endif /* DDB */
7346155Sphk
74163606Srwatson#include <security/mac/mac_framework.h>
75163606Srwatson
76195944Sjamie#define	DEFAULT_HOSTUUID	"00000000-0000-0000-0000-000000000000"
77195944Sjamie
7846155SphkMALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
7946155Sphk
80202468Sbz/* Keep struct prison prison0 and some code in kern_jail_set() readable. */
81202468Sbz#ifdef INET
82202468Sbz#ifdef INET6
83202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
84202468Sbz#else
85202468Sbz#define	_PR_IP_SADDRSEL	PR_IP4_SADDRSEL
86202468Sbz#endif
87202468Sbz#else /* !INET */
88202468Sbz#ifdef INET6
89202468Sbz#define	_PR_IP_SADDRSEL	PR_IP6_SADDRSEL
90202468Sbz#else
91202468Sbz#define	_PR_IP_SADDRSEL	0
92202468Sbz#endif
93202468Sbz#endif
94202468Sbz
95192895Sjamie/* prison0 describes what is "real" about the system. */
96192895Sjamiestruct prison prison0 = {
97192895Sjamie	.pr_id		= 0,
98192895Sjamie	.pr_name	= "0",
99192895Sjamie	.pr_ref		= 1,
100192895Sjamie	.pr_uref	= 1,
101192895Sjamie	.pr_path	= "/",
102192895Sjamie	.pr_securelevel	= -1,
103194762Sjamie	.pr_childmax	= JAIL_MAX,
104195944Sjamie	.pr_hostuuid	= DEFAULT_HOSTUUID,
105201145Santoine	.pr_children	= LIST_HEAD_INITIALIZER(prison0.pr_children),
106196176Sbz#ifdef VIMAGE
107202468Sbz	.pr_flags	= PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
108196176Sbz#else
109202468Sbz	.pr_flags	= PR_HOST|_PR_IP_SADDRSEL,
110196176Sbz#endif
111192895Sjamie	.pr_allow	= PR_ALLOW_ALL,
112192895Sjamie};
113192895SjamieMTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
11457163Srwatson
115192895Sjamie/* allprison and lastprid are protected by allprison_lock. */
116168401Spjdstruct	sx allprison_lock;
117191673SjamieSX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
118191673Sjamiestruct	prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
119179881Sdelphijint	lastprid = 0;
120113275Smike
121191673Sjamiestatic int do_jail_attach(struct thread *td, struct prison *pr);
122190466Sjamiestatic void prison_complete(void *context, int pending);
123191673Sjamiestatic void prison_deref(struct prison *pr, int flags);
124192895Sjamiestatic char *prison_path(struct prison *pr1, struct prison *pr2);
125192895Sjamiestatic void prison_remove_one(struct prison *pr);
126185435Sbz#ifdef INET
127190466Sjamiestatic int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
128192895Sjamiestatic int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
129185435Sbz#endif
130185435Sbz#ifdef INET6
131190466Sjamiestatic int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
132192895Sjamiestatic int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
133185435Sbz#endif
134113275Smike
135191673Sjamie/* Flags for prison_deref */
136191673Sjamie#define	PD_DEREF	0x01
137191673Sjamie#define	PD_DEUREF	0x02
138191673Sjamie#define	PD_LOCKED	0x04
139191673Sjamie#define	PD_LIST_SLOCKED	0x08
140191673Sjamie#define	PD_LIST_XLOCKED	0x10
141113275Smike
142192895Sjamie/*
143192895Sjamie * Parameter names corresponding to PR_* flag values
144192895Sjamie */
145192895Sjamiestatic char *pr_flag_names[] = {
146192895Sjamie	[0] = "persist",
147202468Sbz#ifdef INET
148202468Sbz	[7] = "ip4.saddrsel",
149202468Sbz#endif
150202468Sbz#ifdef INET6
151202468Sbz	[8] = "ip6.saddrsel",
152202468Sbz#endif
153192895Sjamie};
154192895Sjamie
155192895Sjamiestatic char *pr_flag_nonames[] = {
156192895Sjamie	[0] = "nopersist",
157202468Sbz#ifdef INET
158202468Sbz	[7] = "ip4.nosaddrsel",
159202468Sbz#endif
160202468Sbz#ifdef INET6
161202468Sbz	[8] = "ip6.nosaddrsel",
162202468Sbz#endif
163195870Sjamie};
164195870Sjamie
165195870Sjamiestruct jailsys_flags {
166195870Sjamie	const char	*name;
167195870Sjamie	unsigned	 disable;
168195870Sjamie	unsigned	 new;
169195870Sjamie} pr_flag_jailsys[] = {
170195870Sjamie	{ "host", 0, PR_HOST },
171195870Sjamie#ifdef VIMAGE
172195870Sjamie	{ "vnet", 0, PR_VNET },
173195870Sjamie#endif
174192895Sjamie#ifdef INET
175195870Sjamie	{ "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
176192895Sjamie#endif
177192895Sjamie#ifdef INET6
178195870Sjamie	{ "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
179192895Sjamie#endif
180192895Sjamie};
181192895Sjamie
182192895Sjamiestatic char *pr_allow_names[] = {
183192895Sjamie	"allow.set_hostname",
184192895Sjamie	"allow.sysvipc",
185192895Sjamie	"allow.raw_sockets",
186192895Sjamie	"allow.chflags",
187192895Sjamie	"allow.mount",
188192895Sjamie	"allow.quotas",
189192895Sjamie	"allow.socket_af",
190192895Sjamie};
191192895Sjamie
192192895Sjamiestatic char *pr_allow_nonames[] = {
193192895Sjamie	"allow.noset_hostname",
194192895Sjamie	"allow.nosysvipc",
195192895Sjamie	"allow.noraw_sockets",
196192895Sjamie	"allow.nochflags",
197192895Sjamie	"allow.nomount",
198192895Sjamie	"allow.noquotas",
199192895Sjamie	"allow.nosocket_af",
200192895Sjamie};
201192895Sjamie
202196002Sjamie#define	JAIL_DEFAULT_ALLOW		PR_ALLOW_SET_HOSTNAME
203196002Sjamie#define	JAIL_DEFAULT_ENFORCE_STATFS	2
204192895Sjamiestatic unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
205196002Sjamiestatic int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
206192895Sjamie#if defined(INET) || defined(INET6)
207193865Sjamiestatic unsigned jail_max_af_ips = 255;
208192895Sjamie#endif
209192895Sjamie
210192895Sjamie#ifdef INET
211185435Sbzstatic int
212185435Sbzqcmp_v4(const void *ip1, const void *ip2)
213185435Sbz{
214185435Sbz	in_addr_t iaa, iab;
215185435Sbz
216185435Sbz	/*
217185435Sbz	 * We need to compare in HBO here to get the list sorted as expected
218185435Sbz	 * by the result of the code.  Sorting NBO addresses gives you
219185435Sbz	 * interesting results.  If you do not understand, do not try.
220185435Sbz	 */
221185435Sbz	iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
222185435Sbz	iab = ntohl(((const struct in_addr *)ip2)->s_addr);
223185435Sbz
224185435Sbz	/*
225185435Sbz	 * Do not simply return the difference of the two numbers, the int is
226185435Sbz	 * not wide enough.
227185435Sbz	 */
228185435Sbz	if (iaa > iab)
229185435Sbz		return (1);
230185435Sbz	else if (iaa < iab)
231185435Sbz		return (-1);
232185435Sbz	else
233185435Sbz		return (0);
234185435Sbz}
235185435Sbz#endif
236185435Sbz
237185435Sbz#ifdef INET6
238185435Sbzstatic int
239185435Sbzqcmp_v6(const void *ip1, const void *ip2)
240185435Sbz{
241185435Sbz	const struct in6_addr *ia6a, *ia6b;
242185435Sbz	int i, rc;
243185435Sbz
244185435Sbz	ia6a = (const struct in6_addr *)ip1;
245185435Sbz	ia6b = (const struct in6_addr *)ip2;
246185435Sbz
247185435Sbz	rc = 0;
248190466Sjamie	for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
249185435Sbz		if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
250185435Sbz			rc = 1;
251185435Sbz		else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
252185435Sbz			rc = -1;
253185435Sbz	}
254185435Sbz	return (rc);
255185435Sbz}
256185435Sbz#endif
257185435Sbz
258191673Sjamie/*
259191673Sjamie * struct jail_args {
260191673Sjamie *	struct jail *jail;
261191673Sjamie * };
262191673Sjamie */
263191673Sjamieint
264191673Sjamiejail(struct thread *td, struct jail_args *uap)
265185435Sbz{
266191673Sjamie	uint32_t version;
267191673Sjamie	int error;
268192895Sjamie	struct jail j;
269185435Sbz
270191673Sjamie	error = copyin(uap->jail, &version, sizeof(uint32_t));
271191673Sjamie	if (error)
272191673Sjamie		return (error);
273185435Sbz
274191673Sjamie	switch (version) {
275191673Sjamie	case 0:
276191673Sjamie	{
277191673Sjamie		struct jail_v0 j0;
278185435Sbz
279192895Sjamie		/* FreeBSD single IPv4 jails. */
280192895Sjamie		bzero(&j, sizeof(struct jail));
281191673Sjamie		error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
282191673Sjamie		if (error)
283191673Sjamie			return (error);
284192895Sjamie		j.version = j0.version;
285192895Sjamie		j.path = j0.path;
286192895Sjamie		j.hostname = j0.hostname;
287192895Sjamie		j.ip4s = j0.ip_number;
288191673Sjamie		break;
289191673Sjamie	}
290191673Sjamie
291191673Sjamie	case 1:
292185435Sbz		/*
293191673Sjamie		 * Version 1 was used by multi-IPv4 jail implementations
294191673Sjamie		 * that never made it into the official kernel.
295185435Sbz		 */
296191673Sjamie		return (EINVAL);
297185435Sbz
298191673Sjamie	case 2:	/* JAIL_API_VERSION */
299191673Sjamie		/* FreeBSD multi-IPv4/IPv6,noIP jails. */
300191673Sjamie		error = copyin(uap->jail, &j, sizeof(struct jail));
301191673Sjamie		if (error)
302191673Sjamie			return (error);
303192895Sjamie		break;
304192895Sjamie
305192895Sjamie	default:
306192895Sjamie		/* Sci-Fi jails are not supported, sorry. */
307192895Sjamie		return (EINVAL);
308192895Sjamie	}
309192895Sjamie	return (kern_jail(td, &j));
310192895Sjamie}
311192895Sjamie
312192895Sjamieint
313192895Sjamiekern_jail(struct thread *td, struct jail *j)
314192895Sjamie{
315193865Sjamie	struct iovec optiov[2 * (4
316193865Sjamie			    + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
317193865Sjamie#ifdef INET
318193865Sjamie			    + 1
319193865Sjamie#endif
320193865Sjamie#ifdef INET6
321193865Sjamie			    + 1
322193865Sjamie#endif
323193865Sjamie			    )];
324192895Sjamie	struct uio opt;
325192895Sjamie	char *u_path, *u_hostname, *u_name;
326185435Sbz#ifdef INET
327193865Sjamie	uint32_t ip4s;
328192895Sjamie	struct in_addr *u_ip4;
329192895Sjamie#endif
330192895Sjamie#ifdef INET6
331192895Sjamie	struct in6_addr *u_ip6;
332192895Sjamie#endif
333192895Sjamie	size_t tmplen;
334192895Sjamie	int error, enforce_statfs, fi;
335192895Sjamie
336192895Sjamie	bzero(&optiov, sizeof(optiov));
337192895Sjamie	opt.uio_iov = optiov;
338192895Sjamie	opt.uio_iovcnt = 0;
339192895Sjamie	opt.uio_offset = -1;
340192895Sjamie	opt.uio_resid = -1;
341192895Sjamie	opt.uio_segflg = UIO_SYSSPACE;
342192895Sjamie	opt.uio_rw = UIO_READ;
343192895Sjamie	opt.uio_td = td;
344192895Sjamie
345192895Sjamie	/* Set permissions for top-level jails from sysctls. */
346192895Sjamie	if (!jailed(td->td_ucred)) {
347192895Sjamie		for (fi = 0; fi < sizeof(pr_allow_names) /
348192895Sjamie		     sizeof(pr_allow_names[0]); fi++) {
349192895Sjamie			optiov[opt.uio_iovcnt].iov_base =
350192895Sjamie			    (jail_default_allow & (1 << fi))
351192895Sjamie			    ? pr_allow_names[fi] : pr_allow_nonames[fi];
352192895Sjamie			optiov[opt.uio_iovcnt].iov_len =
353192895Sjamie			    strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
354192895Sjamie			opt.uio_iovcnt += 2;
355192895Sjamie		}
356192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
357192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
358192895Sjamie		opt.uio_iovcnt++;
359192895Sjamie		enforce_statfs = jail_default_enforce_statfs;
360192895Sjamie		optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
361192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
362192895Sjamie		opt.uio_iovcnt++;
363192895Sjamie	}
364192895Sjamie
365192895Sjamie	tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
366192895Sjamie#ifdef INET
367192895Sjamie	ip4s = (j->version == 0) ? 1 : j->ip4s;
368192895Sjamie	if (ip4s > jail_max_af_ips)
369192895Sjamie		return (EINVAL);
370192895Sjamie	tmplen += ip4s * sizeof(struct in_addr);
371191673Sjamie#else
372192895Sjamie	if (j->ip4s > 0)
373192895Sjamie		return (EINVAL);
374191673Sjamie#endif
375191673Sjamie#ifdef INET6
376192895Sjamie	if (j->ip6s > jail_max_af_ips)
377192895Sjamie		return (EINVAL);
378192895Sjamie	tmplen += j->ip6s * sizeof(struct in6_addr);
379191673Sjamie#else
380192895Sjamie	if (j->ip6s > 0)
381192895Sjamie		return (EINVAL);
382191673Sjamie#endif
383192895Sjamie	u_path = malloc(tmplen, M_TEMP, M_WAITOK);
384192895Sjamie	u_hostname = u_path + MAXPATHLEN;
385192895Sjamie	u_name = u_hostname + MAXHOSTNAMELEN;
386191673Sjamie#ifdef INET
387192895Sjamie	u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
388191673Sjamie#endif
389191673Sjamie#ifdef INET6
390191673Sjamie#ifdef INET
391192895Sjamie	u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
392191673Sjamie#else
393192895Sjamie	u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
394191673Sjamie#endif
395191673Sjamie#endif
396192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "path";
397192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("path");
398192895Sjamie	opt.uio_iovcnt++;
399192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_path;
400192895Sjamie	error = copyinstr(j->path, u_path, MAXPATHLEN,
401192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
402192895Sjamie	if (error) {
403192895Sjamie		free(u_path, M_TEMP);
404192895Sjamie		return (error);
405192895Sjamie	}
406192895Sjamie	opt.uio_iovcnt++;
407192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "host.hostname";
408192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
409192895Sjamie	opt.uio_iovcnt++;
410192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_hostname;
411192895Sjamie	error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
412192895Sjamie	    &optiov[opt.uio_iovcnt].iov_len);
413192895Sjamie	if (error) {
414192895Sjamie		free(u_path, M_TEMP);
415192895Sjamie		return (error);
416192895Sjamie	}
417192895Sjamie	opt.uio_iovcnt++;
418192895Sjamie	if (j->jailname != NULL) {
419192895Sjamie		optiov[opt.uio_iovcnt].iov_base = "name";
420192895Sjamie		optiov[opt.uio_iovcnt].iov_len = sizeof("name");
421192895Sjamie		opt.uio_iovcnt++;
422192895Sjamie		optiov[opt.uio_iovcnt].iov_base = u_name;
423192895Sjamie		error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
424192895Sjamie		    &optiov[opt.uio_iovcnt].iov_len);
425191673Sjamie		if (error) {
426191673Sjamie			free(u_path, M_TEMP);
427191673Sjamie			return (error);
428191673Sjamie		}
429192895Sjamie		opt.uio_iovcnt++;
430192895Sjamie	}
431191673Sjamie#ifdef INET
432192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
433192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
434192895Sjamie	opt.uio_iovcnt++;
435192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip4;
436192895Sjamie	optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
437192895Sjamie	if (j->version == 0)
438192895Sjamie		u_ip4->s_addr = j->ip4s;
439192895Sjamie	else {
440192895Sjamie		error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
441191673Sjamie		if (error) {
442191673Sjamie			free(u_path, M_TEMP);
443191673Sjamie			return (error);
444191673Sjamie		}
445192895Sjamie	}
446192895Sjamie	opt.uio_iovcnt++;
447185435Sbz#endif
448185435Sbz#ifdef INET6
449192895Sjamie	optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
450192895Sjamie	optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
451192895Sjamie	opt.uio_iovcnt++;
452192895Sjamie	optiov[opt.uio_iovcnt].iov_base = u_ip6;
453192895Sjamie	optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
454192895Sjamie	error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
455192895Sjamie	if (error) {
456192895Sjamie		free(u_path, M_TEMP);
457192895Sjamie		return (error);
458192895Sjamie	}
459192895Sjamie	opt.uio_iovcnt++;
460185435Sbz#endif
461192895Sjamie	KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
462192895Sjamie	    ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
463191673Sjamie	error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
464191673Sjamie	free(u_path, M_TEMP);
465191673Sjamie	return (error);
466185435Sbz}
467185435Sbz
468192895Sjamie
469191673Sjamie/*
470191673Sjamie * struct jail_set_args {
471191673Sjamie *	struct iovec *iovp;
472191673Sjamie *	unsigned int iovcnt;
473191673Sjamie *	int flags;
474191673Sjamie * };
475191673Sjamie */
476191673Sjamieint
477191673Sjamiejail_set(struct thread *td, struct jail_set_args *uap)
478185435Sbz{
479191673Sjamie	struct uio *auio;
480191673Sjamie	int error;
481191673Sjamie
482191673Sjamie	/* Check that we have an even number of iovecs. */
483191673Sjamie	if (uap->iovcnt & 1)
484191673Sjamie		return (EINVAL);
485191673Sjamie
486191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
487191673Sjamie	if (error)
488191673Sjamie		return (error);
489191673Sjamie	error = kern_jail_set(td, auio, uap->flags);
490191673Sjamie	free(auio, M_IOV);
491191673Sjamie	return (error);
492191673Sjamie}
493191673Sjamie
494191673Sjamieint
495191673Sjamiekern_jail_set(struct thread *td, struct uio *optuio, int flags)
496191673Sjamie{
497191673Sjamie	struct nameidata nd;
498185435Sbz#ifdef INET
499190466Sjamie	struct in_addr *ip4;
500185435Sbz#endif
501185435Sbz#ifdef INET6
502185435Sbz	struct in6_addr *ip6;
503185435Sbz#endif
504191673Sjamie	struct vfsopt *opt;
505191673Sjamie	struct vfsoptlist *opts;
506196135Sbz	struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
507191673Sjamie	struct vnode *root;
508196835Sjamie	char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
509192895Sjamie#if defined(INET) || defined(INET6)
510196135Sbz	struct prison *tppr;
511191673Sjamie	void *op;
512192895Sjamie#endif
513193066Sjamie	unsigned long hid;
514192895Sjamie	size_t namelen, onamelen;
515192895Sjamie	int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
516195870Sjamie	int gotchildmax, gotenforce, gothid, gotslevel;
517195870Sjamie	int fi, jid, jsys, len, level;
518194762Sjamie	int childmax, slevel, vfslocked;
519191673Sjamie#if defined(INET) || defined(INET6)
520192895Sjamie	int ii, ij;
521191673Sjamie#endif
522191673Sjamie#ifdef INET
523195974Sjamie	int ip4s, redo_ip4;
524191673Sjamie#endif
525191673Sjamie#ifdef INET6
526195974Sjamie	int ip6s, redo_ip6;
527191673Sjamie#endif
528191673Sjamie	unsigned pr_flags, ch_flags;
529192895Sjamie	unsigned pr_allow, ch_allow, tallow;
530191673Sjamie	char numbuf[12];
531185435Sbz
532191673Sjamie	error = priv_check(td, PRIV_JAIL_SET);
533191673Sjamie	if (!error && (flags & JAIL_ATTACH))
534191673Sjamie		error = priv_check(td, PRIV_JAIL_ATTACH);
535191673Sjamie	if (error)
536191673Sjamie		return (error);
537192895Sjamie	mypr = ppr = td->td_ucred->cr_prison;
538194762Sjamie	if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
539192895Sjamie		return (EPERM);
540191673Sjamie	if (flags & ~JAIL_SET_MASK)
541191673Sjamie		return (EINVAL);
542191673Sjamie
543185435Sbz	/*
544191673Sjamie	 * Check all the parameters before committing to anything.  Not all
545191673Sjamie	 * errors can be caught early, but we may as well try.  Also, this
546191673Sjamie	 * takes care of some expensive stuff (path lookup) before getting
547191673Sjamie	 * the allprison lock.
548185435Sbz	 *
549191673Sjamie	 * XXX Jails are not filesystems, and jail parameters are not mount
550191673Sjamie	 *     options.  But it makes more sense to re-use the vfsopt code
551191673Sjamie	 *     than duplicate it under a different name.
552185435Sbz	 */
553191673Sjamie	error = vfs_buildopts(optuio, &opts);
554191673Sjamie	if (error)
555191673Sjamie		return (error);
556185435Sbz#ifdef INET
557185435Sbz	ip4 = NULL;
558185435Sbz#endif
559185435Sbz#ifdef INET6
560185435Sbz	ip6 = NULL;
561185435Sbz#endif
562191673Sjamie
563191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
564191673Sjamie	if (error == ENOENT)
565191673Sjamie		jid = 0;
566191673Sjamie	else if (error != 0)
567191673Sjamie		goto done_free;
568191673Sjamie
569191673Sjamie	error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
570191673Sjamie	if (error == ENOENT)
571191673Sjamie		gotslevel = 0;
572191673Sjamie	else if (error != 0)
573191673Sjamie		goto done_free;
574191673Sjamie	else
575191673Sjamie		gotslevel = 1;
576191673Sjamie
577194762Sjamie	error =
578194762Sjamie	    vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
579194762Sjamie	if (error == ENOENT)
580194762Sjamie		gotchildmax = 0;
581194762Sjamie	else if (error != 0)
582194762Sjamie		goto done_free;
583194762Sjamie	else
584194762Sjamie		gotchildmax = 1;
585194762Sjamie
586192895Sjamie	error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
587192895Sjamie	gotenforce = (error == 0);
588192895Sjamie	if (gotenforce) {
589192895Sjamie		if (enforce < 0 || enforce > 2)
590192895Sjamie			return (EINVAL);
591192895Sjamie	} else if (error != ENOENT)
592192895Sjamie		goto done_free;
593192895Sjamie
594191673Sjamie	pr_flags = ch_flags = 0;
595192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
596192895Sjamie	    fi++) {
597192895Sjamie		if (pr_flag_names[fi] == NULL)
598192895Sjamie			continue;
599192895Sjamie		vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
600192895Sjamie		vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
601192895Sjamie	}
602191673Sjamie	ch_flags |= pr_flags;
603195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
604195870Sjamie	    fi++) {
605195870Sjamie		error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
606195870Sjamie		    sizeof(jsys));
607195870Sjamie		if (error == ENOENT)
608195870Sjamie			continue;
609195870Sjamie		if (error != 0)
610195870Sjamie			goto done_free;
611195870Sjamie		switch (jsys) {
612195870Sjamie		case JAIL_SYS_DISABLE:
613195870Sjamie			if (!pr_flag_jailsys[fi].disable) {
614195870Sjamie				error = EINVAL;
615195870Sjamie				goto done_free;
616195870Sjamie			}
617195870Sjamie			pr_flags |= pr_flag_jailsys[fi].disable;
618195870Sjamie			break;
619195870Sjamie		case JAIL_SYS_NEW:
620195870Sjamie			pr_flags |= pr_flag_jailsys[fi].new;
621195870Sjamie			break;
622195870Sjamie		case JAIL_SYS_INHERIT:
623195870Sjamie			break;
624195870Sjamie		default:
625195870Sjamie			error = EINVAL;
626195870Sjamie			goto done_free;
627195870Sjamie		}
628195870Sjamie		ch_flags |=
629195870Sjamie		    pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
630195870Sjamie	}
631191673Sjamie	if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
632191673Sjamie	    && !(pr_flags & PR_PERSIST)) {
633191673Sjamie		error = EINVAL;
634191673Sjamie		vfs_opterror(opts, "new jail must persist or attach");
635191673Sjamie		goto done_errmsg;
636191673Sjamie	}
637194251Sjamie#ifdef VIMAGE
638194251Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
639194251Sjamie		error = EINVAL;
640194251Sjamie		vfs_opterror(opts, "vnet cannot be changed after creation");
641194251Sjamie		goto done_errmsg;
642194251Sjamie	}
643194251Sjamie#endif
644195974Sjamie#ifdef INET
645195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
646195974Sjamie		error = EINVAL;
647195974Sjamie		vfs_opterror(opts, "ip4 cannot be changed after creation");
648195974Sjamie		goto done_errmsg;
649195974Sjamie	}
650195974Sjamie#endif
651195974Sjamie#ifdef INET6
652195974Sjamie	if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
653195974Sjamie		error = EINVAL;
654195974Sjamie		vfs_opterror(opts, "ip6 cannot be changed after creation");
655195974Sjamie		goto done_errmsg;
656195974Sjamie	}
657195974Sjamie#endif
658191673Sjamie
659192895Sjamie	pr_allow = ch_allow = 0;
660192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
661192895Sjamie	    fi++) {
662192895Sjamie		vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
663192895Sjamie		vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
664192895Sjamie	}
665192895Sjamie	ch_allow |= pr_allow;
666192895Sjamie
667191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
668191673Sjamie	if (error == ENOENT)
669191673Sjamie		name = NULL;
670191673Sjamie	else if (error != 0)
671191673Sjamie		goto done_free;
672191673Sjamie	else {
673191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
674191673Sjamie			error = EINVAL;
675191673Sjamie			goto done_free;
676191673Sjamie		}
677191673Sjamie		if (len > MAXHOSTNAMELEN) {
678191673Sjamie			error = ENAMETOOLONG;
679191673Sjamie			goto done_free;
680191673Sjamie		}
681191673Sjamie	}
682191673Sjamie
683191673Sjamie	error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
684191673Sjamie	if (error == ENOENT)
685191673Sjamie		host = NULL;
686191673Sjamie	else if (error != 0)
687191673Sjamie		goto done_free;
688191673Sjamie	else {
689193066Sjamie		ch_flags |= PR_HOST;
690193066Sjamie		pr_flags |= PR_HOST;
691191673Sjamie		if (len == 0 || host[len - 1] != '\0') {
692191673Sjamie			error = EINVAL;
693191673Sjamie			goto done_free;
694191673Sjamie		}
695191673Sjamie		if (len > MAXHOSTNAMELEN) {
696191673Sjamie			error = ENAMETOOLONG;
697191673Sjamie			goto done_free;
698191673Sjamie		}
699191673Sjamie	}
700191673Sjamie
701193066Sjamie	error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
702193066Sjamie	if (error == ENOENT)
703193066Sjamie		domain = NULL;
704193066Sjamie	else if (error != 0)
705193066Sjamie		goto done_free;
706193066Sjamie	else {
707193066Sjamie		ch_flags |= PR_HOST;
708193066Sjamie		pr_flags |= PR_HOST;
709193066Sjamie		if (len == 0 || domain[len - 1] != '\0') {
710193066Sjamie			error = EINVAL;
711193066Sjamie			goto done_free;
712193066Sjamie		}
713193066Sjamie		if (len > MAXHOSTNAMELEN) {
714193066Sjamie			error = ENAMETOOLONG;
715193066Sjamie			goto done_free;
716193066Sjamie		}
717193066Sjamie	}
718193066Sjamie
719193066Sjamie	error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
720193066Sjamie	if (error == ENOENT)
721193066Sjamie		uuid = NULL;
722193066Sjamie	else if (error != 0)
723193066Sjamie		goto done_free;
724193066Sjamie	else {
725193066Sjamie		ch_flags |= PR_HOST;
726193066Sjamie		pr_flags |= PR_HOST;
727193066Sjamie		if (len == 0 || uuid[len - 1] != '\0') {
728193066Sjamie			error = EINVAL;
729193066Sjamie			goto done_free;
730193066Sjamie		}
731193066Sjamie		if (len > HOSTUUIDLEN) {
732193066Sjamie			error = ENAMETOOLONG;
733193066Sjamie			goto done_free;
734193066Sjamie		}
735193066Sjamie	}
736193066Sjamie
737193066Sjamie#ifdef COMPAT_IA32
738193066Sjamie	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
739193066Sjamie		uint32_t hid32;
740193066Sjamie
741193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
742193066Sjamie		hid = hid32;
743193066Sjamie	} else
744193066Sjamie#endif
745193066Sjamie		error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
746193066Sjamie	if (error == ENOENT)
747193066Sjamie		gothid = 0;
748193066Sjamie	else if (error != 0)
749193066Sjamie		goto done_free;
750193066Sjamie	else {
751193066Sjamie		gothid = 1;
752193066Sjamie		ch_flags |= PR_HOST;
753193066Sjamie		pr_flags |= PR_HOST;
754193066Sjamie	}
755193066Sjamie
756185435Sbz#ifdef INET
757191673Sjamie	error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
758191673Sjamie	if (error == ENOENT)
759195870Sjamie		ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
760191673Sjamie	else if (error != 0)
761191673Sjamie		goto done_free;
762191673Sjamie	else if (ip4s & (sizeof(*ip4) - 1)) {
763191673Sjamie		error = EINVAL;
764191673Sjamie		goto done_free;
765192895Sjamie	} else {
766195870Sjamie		ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
767195870Sjamie		if (ip4s == 0)
768195870Sjamie			pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
769195870Sjamie		else {
770195870Sjamie			pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
771192895Sjamie			ip4s /= sizeof(*ip4);
772192895Sjamie			if (ip4s > jail_max_af_ips) {
773185435Sbz				error = EINVAL;
774192895Sjamie				vfs_opterror(opts, "too many IPv4 addresses");
775192895Sjamie				goto done_errmsg;
776185435Sbz			}
777195974Sjamie			ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
778192895Sjamie			bcopy(op, ip4, ip4s * sizeof(*ip4));
779192895Sjamie			/*
780192895Sjamie			 * IP addresses are all sorted but ip[0] to preserve
781192895Sjamie			 * the primary IP address as given from userland.
782192895Sjamie			 * This special IP is used for unbound outgoing
783202116Sbz			 * connections as well for "loopback" traffic in case
784202116Sbz			 * source address selection cannot find any more fitting
785202116Sbz			 * address to connect from.
786192895Sjamie			 */
787192895Sjamie			if (ip4s > 1)
788192895Sjamie				qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
789192895Sjamie			/*
790192895Sjamie			 * Check for duplicate addresses and do some simple
791192895Sjamie			 * zero and broadcast checks. If users give other bogus
792192895Sjamie			 * addresses it is their problem.
793192895Sjamie			 *
794192895Sjamie			 * We do not have to care about byte order for these
795192895Sjamie			 * checks so we will do them in NBO.
796192895Sjamie			 */
797192895Sjamie			for (ii = 0; ii < ip4s; ii++) {
798192895Sjamie				if (ip4[ii].s_addr == INADDR_ANY ||
799192895Sjamie				    ip4[ii].s_addr == INADDR_BROADCAST) {
800192895Sjamie					error = EINVAL;
801192895Sjamie					goto done_free;
802192895Sjamie				}
803192895Sjamie				if ((ii+1) < ip4s &&
804192895Sjamie				    (ip4[0].s_addr == ip4[ii+1].s_addr ||
805192895Sjamie				     ip4[ii].s_addr == ip4[ii+1].s_addr)) {
806192895Sjamie					error = EINVAL;
807192895Sjamie					goto done_free;
808192895Sjamie				}
809192895Sjamie			}
810185435Sbz		}
811191673Sjamie	}
812191673Sjamie#endif
813185435Sbz
814185435Sbz#ifdef INET6
815191673Sjamie	error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
816191673Sjamie	if (error == ENOENT)
817195870Sjamie		ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
818191673Sjamie	else if (error != 0)
819191673Sjamie		goto done_free;
820191673Sjamie	else if (ip6s & (sizeof(*ip6) - 1)) {
821191673Sjamie		error = EINVAL;
822191673Sjamie		goto done_free;
823192895Sjamie	} else {
824195870Sjamie		ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
825195870Sjamie		if (ip6s == 0)
826195870Sjamie			pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
827195870Sjamie		else {
828195870Sjamie			pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
829192895Sjamie			ip6s /= sizeof(*ip6);
830192895Sjamie			if (ip6s > jail_max_af_ips) {
831185435Sbz				error = EINVAL;
832192895Sjamie				vfs_opterror(opts, "too many IPv6 addresses");
833192895Sjamie				goto done_errmsg;
834185435Sbz			}
835195974Sjamie			ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
836192895Sjamie			bcopy(op, ip6, ip6s * sizeof(*ip6));
837192895Sjamie			if (ip6s > 1)
838192895Sjamie				qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
839192895Sjamie			for (ii = 0; ii < ip6s; ii++) {
840192895Sjamie				if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
841192895Sjamie					error = EINVAL;
842192895Sjamie					goto done_free;
843192895Sjamie				}
844192895Sjamie				if ((ii+1) < ip6s &&
845192895Sjamie				    (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
846192895Sjamie				     IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
847192895Sjamie				{
848192895Sjamie					error = EINVAL;
849192895Sjamie					goto done_free;
850192895Sjamie				}
851192895Sjamie			}
852185435Sbz		}
853191673Sjamie	}
854185435Sbz#endif
855185435Sbz
856195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
857195945Sjamie	if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
858195945Sjamie		error = EINVAL;
859195945Sjamie		vfs_opterror(opts,
860195945Sjamie		    "vnet jails cannot have IP address restrictions");
861195945Sjamie		goto done_errmsg;
862195945Sjamie	}
863195945Sjamie#endif
864195945Sjamie
865191673Sjamie	root = NULL;
866191673Sjamie	error = vfs_getopt(opts, "path", (void **)&path, &len);
867191673Sjamie	if (error == ENOENT)
868191673Sjamie		path = NULL;
869191673Sjamie	else if (error != 0)
870191673Sjamie		goto done_free;
871191673Sjamie	else {
872191673Sjamie		if (flags & JAIL_UPDATE) {
873191673Sjamie			error = EINVAL;
874191673Sjamie			vfs_opterror(opts,
875191673Sjamie			    "path cannot be changed after creation");
876191673Sjamie			goto done_errmsg;
877191673Sjamie		}
878191673Sjamie		if (len == 0 || path[len - 1] != '\0') {
879191673Sjamie			error = EINVAL;
880191673Sjamie			goto done_free;
881191673Sjamie		}
882191673Sjamie		if (len < 2 || (len == 2 && path[0] == '/'))
883191673Sjamie			path = NULL;
884191673Sjamie		else {
885192895Sjamie			/* Leave room for a real-root full pathname. */
886192895Sjamie			if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
887192895Sjamie			    ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
888192895Sjamie				error = ENAMETOOLONG;
889192895Sjamie				goto done_free;
890192895Sjamie			}
891191673Sjamie			NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
892191673Sjamie			    path, td);
893191673Sjamie			error = namei(&nd);
894191673Sjamie			if (error)
895191673Sjamie				goto done_free;
896191673Sjamie			vfslocked = NDHASGIANT(&nd);
897191673Sjamie			root = nd.ni_vp;
898191673Sjamie			NDFREE(&nd, NDF_ONLY_PNBUF);
899191673Sjamie			if (root->v_type != VDIR) {
900191673Sjamie				error = ENOTDIR;
901191673Sjamie				vrele(root);
902191673Sjamie				VFS_UNLOCK_GIANT(vfslocked);
903191673Sjamie				goto done_free;
904191673Sjamie			}
905191673Sjamie			VFS_UNLOCK_GIANT(vfslocked);
906191673Sjamie		}
907191673Sjamie	}
908185435Sbz
909191673Sjamie	/*
910191673Sjamie	 * Grab the allprison lock before letting modules check their
911191673Sjamie	 * parameters.  Once we have it, do not let go so we'll have a
912191673Sjamie	 * consistent view of the OSD list.
913191673Sjamie	 */
914191673Sjamie	sx_xlock(&allprison_lock);
915191673Sjamie	error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
916191673Sjamie	if (error)
917191673Sjamie		goto done_unlock_list;
918185435Sbz
919191673Sjamie	/* By now, all parameters should have been noted. */
920191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
921191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
922191673Sjamie			error = EINVAL;
923191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
924191673Sjamie			goto done_unlock_list;
925191673Sjamie		}
926191673Sjamie	}
927191673Sjamie
928185435Sbz	/*
929191673Sjamie	 * See if we are creating a new record or updating an existing one.
930191673Sjamie	 * This abuses the file error codes ENOENT and EEXIST.
931185435Sbz	 */
932191673Sjamie	cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
933191673Sjamie	if (!cuflags) {
934191673Sjamie		error = EINVAL;
935191673Sjamie		vfs_opterror(opts, "no valid operation (create or update)");
936191673Sjamie		goto done_unlock_list;
937191673Sjamie	}
938191673Sjamie	pr = NULL;
939196835Sjamie	namelc = NULL;
940196835Sjamie	if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
941196835Sjamie		namelc = strrchr(name, '.');
942196835Sjamie		jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
943196835Sjamie		if (*p != '\0')
944196835Sjamie			jid = 0;
945196835Sjamie	}
946191673Sjamie	if (jid != 0) {
947192895Sjamie		/*
948192895Sjamie		 * See if a requested jid already exists.  There is an
949192895Sjamie		 * information leak here if the jid exists but is not within
950192895Sjamie		 * the caller's jail hierarchy.  Jail creators will get EEXIST
951192895Sjamie		 * even though they cannot see the jail, and CREATE | UPDATE
952192895Sjamie		 * will return ENOENT which is not normally a valid error.
953192895Sjamie		 */
954191673Sjamie		if (jid < 0) {
955191673Sjamie			error = EINVAL;
956191673Sjamie			vfs_opterror(opts, "negative jid");
957191673Sjamie			goto done_unlock_list;
958191673Sjamie		}
959191673Sjamie		pr = prison_find(jid);
960191673Sjamie		if (pr != NULL) {
961192895Sjamie			ppr = pr->pr_parent;
962191673Sjamie			/* Create: jid must not exist. */
963191673Sjamie			if (cuflags == JAIL_CREATE) {
964191673Sjamie				mtx_unlock(&pr->pr_mtx);
965191673Sjamie				error = EEXIST;
966191673Sjamie				vfs_opterror(opts, "jail %d already exists",
967191673Sjamie				    jid);
968191673Sjamie				goto done_unlock_list;
969191673Sjamie			}
970192895Sjamie			if (!prison_ischild(mypr, pr)) {
971192895Sjamie				mtx_unlock(&pr->pr_mtx);
972192895Sjamie				pr = NULL;
973192895Sjamie			} else if (pr->pr_uref == 0) {
974191673Sjamie				if (!(flags & JAIL_DYING)) {
975191673Sjamie					mtx_unlock(&pr->pr_mtx);
976191673Sjamie					error = ENOENT;
977191673Sjamie					vfs_opterror(opts, "jail %d is dying",
978191673Sjamie					    jid);
979191673Sjamie					goto done_unlock_list;
980191673Sjamie				} else if ((flags & JAIL_ATTACH) ||
981191673Sjamie				    (pr_flags & PR_PERSIST)) {
982191673Sjamie					/*
983191673Sjamie					 * A dying jail might be resurrected
984191673Sjamie					 * (via attach or persist), but first
985191673Sjamie					 * it must determine if another jail
986191673Sjamie					 * has claimed its name.  Accomplish
987191673Sjamie					 * this by implicitly re-setting the
988191673Sjamie					 * name.
989191673Sjamie					 */
990191673Sjamie					if (name == NULL)
991192895Sjamie						name = prison_name(mypr, pr);
992191673Sjamie				}
993191673Sjamie			}
994191673Sjamie		}
995191673Sjamie		if (pr == NULL) {
996191673Sjamie			/* Update: jid must exist. */
997191673Sjamie			if (cuflags == JAIL_UPDATE) {
998191673Sjamie				error = ENOENT;
999191673Sjamie				vfs_opterror(opts, "jail %d not found", jid);
1000191673Sjamie				goto done_unlock_list;
1001191673Sjamie			}
1002191673Sjamie		}
1003191673Sjamie	}
1004191673Sjamie	/*
1005191673Sjamie	 * If the caller provided a name, look for a jail by that name.
1006191673Sjamie	 * This has different semantics for creates and updates keyed by jid
1007191673Sjamie	 * (where the name must not already exist in a different jail),
1008191673Sjamie	 * and updates keyed by the name itself (where the name must exist
1009191673Sjamie	 * because that is the jail being updated).
1010191673Sjamie	 */
1011191673Sjamie	if (name != NULL) {
1012196835Sjamie		namelc = strrchr(name, '.');
1013196835Sjamie		if (namelc == NULL)
1014196835Sjamie			namelc = name;
1015196835Sjamie		else {
1016192895Sjamie			/*
1017192895Sjamie			 * This is a hierarchical name.  Split it into the
1018192895Sjamie			 * parent and child names, and make sure the parent
1019192895Sjamie			 * exists or matches an already found jail.
1020192895Sjamie			 */
1021196835Sjamie			*namelc = '\0';
1022192895Sjamie			if (pr != NULL) {
1023196835Sjamie				if (strncmp(name, ppr->pr_name, namelc - name)
1024196835Sjamie				    || ppr->pr_name[namelc - name] != '\0') {
1025192895Sjamie					mtx_unlock(&pr->pr_mtx);
1026192895Sjamie					error = EINVAL;
1027192895Sjamie					vfs_opterror(opts,
1028192895Sjamie					    "cannot change jail's parent");
1029192895Sjamie					goto done_unlock_list;
1030192895Sjamie				}
1031192895Sjamie			} else {
1032192895Sjamie				ppr = prison_find_name(mypr, name);
1033192895Sjamie				if (ppr == NULL) {
1034192895Sjamie					error = ENOENT;
1035192895Sjamie					vfs_opterror(opts,
1036192895Sjamie					    "jail \"%s\" not found", name);
1037192895Sjamie					goto done_unlock_list;
1038192895Sjamie				}
1039192895Sjamie				mtx_unlock(&ppr->pr_mtx);
1040192895Sjamie			}
1041196835Sjamie			name = ++namelc;
1042192895Sjamie		}
1043191673Sjamie		if (name[0] != '\0') {
1044192895Sjamie			namelen =
1045192895Sjamie			    (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1046192895Sjamie name_again:
1047191673Sjamie			deadpr = NULL;
1048192895Sjamie			FOREACH_PRISON_CHILD(ppr, tpr) {
1049191673Sjamie				if (tpr != pr && tpr->pr_ref > 0 &&
1050192895Sjamie				    !strcmp(tpr->pr_name + namelen, name)) {
1051191673Sjamie					if (pr == NULL &&
1052191673Sjamie					    cuflags != JAIL_CREATE) {
1053191673Sjamie						mtx_lock(&tpr->pr_mtx);
1054191673Sjamie						if (tpr->pr_ref > 0) {
1055191673Sjamie							/*
1056191673Sjamie							 * Use this jail
1057191673Sjamie							 * for updates.
1058191673Sjamie							 */
1059191673Sjamie							if (tpr->pr_uref > 0) {
1060191673Sjamie								pr = tpr;
1061191673Sjamie								break;
1062191673Sjamie							}
1063191673Sjamie							deadpr = tpr;
1064191673Sjamie						}
1065191673Sjamie						mtx_unlock(&tpr->pr_mtx);
1066191673Sjamie					} else if (tpr->pr_uref > 0) {
1067191673Sjamie						/*
1068191673Sjamie						 * Create, or update(jid):
1069191673Sjamie						 * name must not exist in an
1070192895Sjamie						 * active sibling jail.
1071191673Sjamie						 */
1072191673Sjamie						error = EEXIST;
1073191673Sjamie						if (pr != NULL)
1074191673Sjamie							mtx_unlock(&pr->pr_mtx);
1075191673Sjamie						vfs_opterror(opts,
1076191673Sjamie						   "jail \"%s\" already exists",
1077191673Sjamie						   name);
1078191673Sjamie						goto done_unlock_list;
1079191673Sjamie					}
1080191673Sjamie				}
1081191673Sjamie			}
1082191673Sjamie			/* If no active jail is found, use a dying one. */
1083191673Sjamie			if (deadpr != NULL && pr == NULL) {
1084191673Sjamie				if (flags & JAIL_DYING) {
1085191673Sjamie					mtx_lock(&deadpr->pr_mtx);
1086191673Sjamie					if (deadpr->pr_ref == 0) {
1087191673Sjamie						mtx_unlock(&deadpr->pr_mtx);
1088191673Sjamie						goto name_again;
1089191673Sjamie					}
1090191673Sjamie					pr = deadpr;
1091191673Sjamie				} else if (cuflags == JAIL_UPDATE) {
1092191673Sjamie					error = ENOENT;
1093191673Sjamie					vfs_opterror(opts,
1094191673Sjamie					    "jail \"%s\" is dying", name);
1095191673Sjamie					goto done_unlock_list;
1096191673Sjamie				}
1097191673Sjamie			}
1098191673Sjamie			/* Update: name must exist if no jid. */
1099191673Sjamie			else if (cuflags == JAIL_UPDATE && pr == NULL) {
1100191673Sjamie				error = ENOENT;
1101191673Sjamie				vfs_opterror(opts, "jail \"%s\" not found",
1102191673Sjamie				    name);
1103191673Sjamie				goto done_unlock_list;
1104191673Sjamie			}
1105191673Sjamie		}
1106191673Sjamie	}
1107191673Sjamie	/* Update: must provide a jid or name. */
1108191673Sjamie	else if (cuflags == JAIL_UPDATE && pr == NULL) {
1109191673Sjamie		error = ENOENT;
1110191673Sjamie		vfs_opterror(opts, "update specified no jail");
1111191673Sjamie		goto done_unlock_list;
1112191673Sjamie	}
1113185435Sbz
1114191673Sjamie	/* If there's no prison to update, create a new one and link it in. */
1115191673Sjamie	if (pr == NULL) {
1116194762Sjamie		for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1117194762Sjamie			if (tpr->pr_childcount >= tpr->pr_childmax) {
1118194762Sjamie				error = EPERM;
1119194762Sjamie				vfs_opterror(opts, "prison limit exceeded");
1120194762Sjamie				goto done_unlock_list;
1121194762Sjamie			}
1122191673Sjamie		created = 1;
1123192895Sjamie		mtx_lock(&ppr->pr_mtx);
1124192895Sjamie		if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1125192895Sjamie			mtx_unlock(&ppr->pr_mtx);
1126192895Sjamie			error = ENOENT;
1127192895Sjamie			vfs_opterror(opts, "parent jail went away!");
1128192895Sjamie			goto done_unlock_list;
1129192895Sjamie		}
1130192895Sjamie		ppr->pr_ref++;
1131192895Sjamie		ppr->pr_uref++;
1132192895Sjamie		mtx_unlock(&ppr->pr_mtx);
1133191673Sjamie		pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1134191673Sjamie		if (jid == 0) {
1135191673Sjamie			/* Find the next free jid. */
1136191673Sjamie			jid = lastprid + 1;
1137191673Sjamie findnext:
1138191673Sjamie			if (jid == JAIL_MAX)
1139191673Sjamie				jid = 1;
1140191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list) {
1141191673Sjamie				if (tpr->pr_id < jid)
1142191673Sjamie					continue;
1143191673Sjamie				if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1144191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1145191673Sjamie					break;
1146191673Sjamie				}
1147191673Sjamie				if (jid == lastprid) {
1148191673Sjamie					error = EAGAIN;
1149191673Sjamie					vfs_opterror(opts,
1150191673Sjamie					    "no available jail IDs");
1151191673Sjamie					free(pr, M_PRISON);
1152192895Sjamie					prison_deref(ppr, PD_DEREF |
1153192895Sjamie					    PD_DEUREF | PD_LIST_XLOCKED);
1154192895Sjamie					goto done_releroot;
1155191673Sjamie				}
1156191673Sjamie				jid++;
1157191673Sjamie				goto findnext;
1158191673Sjamie			}
1159191673Sjamie			lastprid = jid;
1160191673Sjamie		} else {
1161191673Sjamie			/*
1162191673Sjamie			 * The jail already has a jid (that did not yet exist),
1163191673Sjamie			 * so just find where to insert it.
1164191673Sjamie			 */
1165191673Sjamie			TAILQ_FOREACH(tpr, &allprison, pr_list)
1166191673Sjamie				if (tpr->pr_id >= jid) {
1167191673Sjamie					TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1168191673Sjamie					break;
1169191673Sjamie				}
1170191673Sjamie		}
1171191673Sjamie		if (tpr == NULL)
1172191673Sjamie			TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1173192895Sjamie		LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1174192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1175194762Sjamie			tpr->pr_childcount++;
1176185435Sbz
1177192895Sjamie		pr->pr_parent = ppr;
1178191673Sjamie		pr->pr_id = jid;
1179192895Sjamie
1180192895Sjamie		/* Set some default values, and inherit some from the parent. */
1181191673Sjamie		if (name == NULL)
1182191673Sjamie			name = "";
1183191673Sjamie		if (path == NULL) {
1184191673Sjamie			path = "/";
1185192895Sjamie			root = mypr->pr_root;
1186191673Sjamie			vref(root);
1187191673Sjamie		}
1188195944Sjamie		strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1189195944Sjamie		pr->pr_flags |= PR_HOST;
1190195945Sjamie#if defined(INET) || defined(INET6)
1191195945Sjamie#ifdef VIMAGE
1192195945Sjamie		if (!(pr_flags & PR_VNET))
1193195945Sjamie#endif
1194195945Sjamie		{
1195192895Sjamie#ifdef INET
1196195974Sjamie			if (!(ch_flags & PR_IP4_USER))
1197195974Sjamie				pr->pr_flags |=
1198195974Sjamie				    PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1199195974Sjamie			else if (!(pr_flags & PR_IP4_USER)) {
1200195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP4;
1201195974Sjamie				if (ppr->pr_ip4 != NULL) {
1202195974Sjamie					pr->pr_ip4s = ppr->pr_ip4s;
1203195974Sjamie					pr->pr_ip4 = malloc(pr->pr_ip4s *
1204195974Sjamie					    sizeof(struct in_addr), M_PRISON,
1205195974Sjamie					    M_WAITOK);
1206195974Sjamie					bcopy(ppr->pr_ip4, pr->pr_ip4,
1207195974Sjamie					    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1208195974Sjamie				}
1209195974Sjamie			}
1210192895Sjamie#endif
1211192895Sjamie#ifdef INET6
1212195974Sjamie			if (!(ch_flags & PR_IP6_USER))
1213195974Sjamie				pr->pr_flags |=
1214195974Sjamie				    PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1215195974Sjamie			else if (!(pr_flags & PR_IP6_USER)) {
1216195974Sjamie				pr->pr_flags |= ppr->pr_flags & PR_IP6;
1217195974Sjamie				if (ppr->pr_ip6 != NULL) {
1218195974Sjamie					pr->pr_ip6s = ppr->pr_ip6s;
1219195974Sjamie					pr->pr_ip6 = malloc(pr->pr_ip6s *
1220195974Sjamie					    sizeof(struct in6_addr), M_PRISON,
1221195974Sjamie					    M_WAITOK);
1222195974Sjamie					bcopy(ppr->pr_ip6, pr->pr_ip6,
1223195974Sjamie					    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1224195974Sjamie				}
1225195974Sjamie			}
1226192895Sjamie#endif
1227195945Sjamie		}
1228195945Sjamie#endif
1229202468Sbz		/* Source address selection is always on by default. */
1230202468Sbz		pr->pr_flags |= _PR_IP_SADDRSEL;
1231202468Sbz
1232192895Sjamie		pr->pr_securelevel = ppr->pr_securelevel;
1233192895Sjamie		pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1234196002Sjamie		pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1235191673Sjamie
1236192895Sjamie		LIST_INIT(&pr->pr_children);
1237192895Sjamie		mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1238191673Sjamie
1239194251Sjamie#ifdef VIMAGE
1240194251Sjamie		/* Allocate a new vnet if specified. */
1241194251Sjamie		pr->pr_vnet = (pr_flags & PR_VNET)
1242194251Sjamie		    ? vnet_alloc() : ppr->pr_vnet;
1243194251Sjamie#endif
1244185435Sbz		/*
1245191673Sjamie		 * Allocate a dedicated cpuset for each jail.
1246191673Sjamie		 * Unlike other initial settings, this may return an erorr.
1247185435Sbz		 */
1248192895Sjamie		error = cpuset_create_root(ppr, &pr->pr_cpuset);
1249191673Sjamie		if (error) {
1250191673Sjamie			prison_deref(pr, PD_LIST_XLOCKED);
1251191673Sjamie			goto done_releroot;
1252191673Sjamie		}
1253185435Sbz
1254191673Sjamie		mtx_lock(&pr->pr_mtx);
1255185435Sbz		/*
1256191673Sjamie		 * New prisons do not yet have a reference, because we do not
1257191673Sjamie		 * want other to see the incomplete prison once the
1258191673Sjamie		 * allprison_lock is downgraded.
1259185435Sbz		 */
1260191673Sjamie	} else {
1261191673Sjamie		created = 0;
1262195974Sjamie		/*
1263195974Sjamie		 * Grab a reference for existing prisons, to ensure they
1264195974Sjamie		 * continue to exist for the duration of the call.
1265195974Sjamie		 */
1266195974Sjamie		pr->pr_ref++;
1267195945Sjamie#if defined(VIMAGE) && (defined(INET) || defined(INET6))
1268195945Sjamie		if ((pr->pr_flags & PR_VNET) &&
1269195945Sjamie		    (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1270195945Sjamie			error = EINVAL;
1271195945Sjamie			vfs_opterror(opts,
1272195945Sjamie			    "vnet jails cannot have IP address restrictions");
1273195945Sjamie			goto done_deref_locked;
1274195945Sjamie		}
1275195945Sjamie#endif
1276195974Sjamie#ifdef INET
1277195974Sjamie		if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1278195974Sjamie			error = EINVAL;
1279195974Sjamie			vfs_opterror(opts,
1280195974Sjamie			    "ip4 cannot be changed after creation");
1281195974Sjamie			goto done_deref_locked;
1282195974Sjamie		}
1283195974Sjamie#endif
1284195974Sjamie#ifdef INET6
1285195974Sjamie		if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1286195974Sjamie			error = EINVAL;
1287195974Sjamie			vfs_opterror(opts,
1288195974Sjamie			    "ip6 cannot be changed after creation");
1289195974Sjamie			goto done_deref_locked;
1290195974Sjamie		}
1291195974Sjamie#endif
1292191673Sjamie	}
1293185435Sbz
1294191673Sjamie	/* Do final error checking before setting anything. */
1295192895Sjamie	if (gotslevel) {
1296192895Sjamie		if (slevel < ppr->pr_securelevel) {
1297192895Sjamie			error = EPERM;
1298192895Sjamie			goto done_deref_locked;
1299192895Sjamie		}
1300192895Sjamie	}
1301194762Sjamie	if (gotchildmax) {
1302194762Sjamie		if (childmax >= ppr->pr_childmax) {
1303194762Sjamie			error = EPERM;
1304194762Sjamie			goto done_deref_locked;
1305194762Sjamie		}
1306194762Sjamie	}
1307192895Sjamie	if (gotenforce) {
1308192895Sjamie		if (enforce < ppr->pr_enforce_statfs) {
1309192895Sjamie			error = EPERM;
1310192895Sjamie			goto done_deref_locked;
1311192895Sjamie		}
1312192895Sjamie	}
1313185435Sbz#ifdef INET
1314195974Sjamie	if (ip4s > 0) {
1315192895Sjamie		if (ppr->pr_flags & PR_IP4) {
1316195974Sjamie			/*
1317195974Sjamie			 * Make sure the new set of IP addresses is a
1318195974Sjamie			 * subset of the parent's list.  Don't worry
1319195974Sjamie			 * about the parent being unlocked, as any
1320195974Sjamie			 * setting is done with allprison_lock held.
1321195974Sjamie			 */
1322195974Sjamie			for (ij = 0; ij < ppr->pr_ip4s; ij++)
1323195974Sjamie				if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1324195974Sjamie					break;
1325195974Sjamie			if (ij == ppr->pr_ip4s) {
1326195974Sjamie				error = EPERM;
1327195974Sjamie				goto done_deref_locked;
1328195974Sjamie			}
1329195974Sjamie			if (ip4s > 1) {
1330195974Sjamie				for (ii = ij = 1; ii < ip4s; ii++) {
1331195974Sjamie					if (ip4[ii].s_addr ==
1332195974Sjamie					    ppr->pr_ip4[0].s_addr)
1333195974Sjamie						continue;
1334195974Sjamie					for (; ij < ppr->pr_ip4s; ij++)
1335195974Sjamie						if (ip4[ii].s_addr ==
1336195974Sjamie						    ppr->pr_ip4[ij].s_addr)
1337195974Sjamie							break;
1338195974Sjamie					if (ij == ppr->pr_ip4s)
1339195974Sjamie						break;
1340192895Sjamie				}
1341192895Sjamie				if (ij == ppr->pr_ip4s) {
1342192895Sjamie					error = EPERM;
1343192895Sjamie					goto done_deref_locked;
1344192895Sjamie				}
1345192895Sjamie			}
1346192895Sjamie		}
1347195974Sjamie		/*
1348195974Sjamie		 * Check for conflicting IP addresses.  We permit them
1349195974Sjamie		 * if there is no more than one IP on each jail.  If
1350195974Sjamie		 * there is a duplicate on a jail with more than one
1351195974Sjamie		 * IP stop checking and return error.
1352195974Sjamie		 */
1353195974Sjamie		tppr = ppr;
1354195945Sjamie#ifdef VIMAGE
1355195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1356195974Sjamie			if (tppr->pr_flags & PR_VNET)
1357195974Sjamie				break;
1358195945Sjamie#endif
1359195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1360195974Sjamie			if (tpr == pr ||
1361195945Sjamie#ifdef VIMAGE
1362195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1363195945Sjamie#endif
1364195974Sjamie			    tpr->pr_uref == 0) {
1365192895Sjamie				descend = 0;
1366195974Sjamie				continue;
1367195974Sjamie			}
1368195974Sjamie			if (!(tpr->pr_flags & PR_IP4_USER))
1369195974Sjamie				continue;
1370195974Sjamie			descend = 0;
1371195974Sjamie			if (tpr->pr_ip4 == NULL ||
1372195974Sjamie			    (ip4s == 1 && tpr->pr_ip4s == 1))
1373195974Sjamie				continue;
1374195974Sjamie			for (ii = 0; ii < ip4s; ii++) {
1375195974Sjamie				if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1376195974Sjamie					error = EADDRINUSE;
1377195974Sjamie					vfs_opterror(opts,
1378195974Sjamie					    "IPv4 addresses clash");
1379195974Sjamie					goto done_deref_locked;
1380192895Sjamie				}
1381192895Sjamie			}
1382192895Sjamie		}
1383192895Sjamie	}
1384185435Sbz#endif
1385191673Sjamie#ifdef INET6
1386195974Sjamie	if (ip6s > 0) {
1387192895Sjamie		if (ppr->pr_flags & PR_IP6) {
1388195974Sjamie			/*
1389195974Sjamie			 * Make sure the new set of IP addresses is a
1390195974Sjamie			 * subset of the parent's list.
1391195974Sjamie			 */
1392195974Sjamie			for (ij = 0; ij < ppr->pr_ip6s; ij++)
1393195974Sjamie				if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1394195974Sjamie				    &ppr->pr_ip6[ij]))
1395195974Sjamie					break;
1396195974Sjamie			if (ij == ppr->pr_ip6s) {
1397195974Sjamie				error = EPERM;
1398195974Sjamie				goto done_deref_locked;
1399195974Sjamie			}
1400195974Sjamie			if (ip6s > 1) {
1401195974Sjamie				for (ii = ij = 1; ii < ip6s; ii++) {
1402195974Sjamie					if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1403195974Sjamie					     &ppr->pr_ip6[0]))
1404195974Sjamie						continue;
1405195974Sjamie					for (; ij < ppr->pr_ip6s; ij++)
1406195974Sjamie						if (IN6_ARE_ADDR_EQUAL(
1407195974Sjamie						    &ip6[ii], &ppr->pr_ip6[ij]))
1408195974Sjamie							break;
1409195974Sjamie					if (ij == ppr->pr_ip6s)
1410195974Sjamie						break;
1411192895Sjamie				}
1412192895Sjamie				if (ij == ppr->pr_ip6s) {
1413192895Sjamie					error = EPERM;
1414192895Sjamie					goto done_deref_locked;
1415192895Sjamie				}
1416192895Sjamie			}
1417192895Sjamie		}
1418195974Sjamie		/* Check for conflicting IP addresses. */
1419195974Sjamie		tppr = ppr;
1420195945Sjamie#ifdef VIMAGE
1421195974Sjamie		for (; tppr != &prison0; tppr = tppr->pr_parent)
1422195974Sjamie			if (tppr->pr_flags & PR_VNET)
1423195974Sjamie				break;
1424195945Sjamie#endif
1425195974Sjamie		FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1426195974Sjamie			if (tpr == pr ||
1427195945Sjamie#ifdef VIMAGE
1428195974Sjamie			    (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1429195945Sjamie#endif
1430195974Sjamie			    tpr->pr_uref == 0) {
1431192895Sjamie				descend = 0;
1432195974Sjamie				continue;
1433195974Sjamie			}
1434195974Sjamie			if (!(tpr->pr_flags & PR_IP6_USER))
1435195974Sjamie				continue;
1436195974Sjamie			descend = 0;
1437195974Sjamie			if (tpr->pr_ip6 == NULL ||
1438195974Sjamie			    (ip6s == 1 && tpr->pr_ip6s == 1))
1439195974Sjamie				continue;
1440195974Sjamie			for (ii = 0; ii < ip6s; ii++) {
1441195974Sjamie				if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1442195974Sjamie					error = EADDRINUSE;
1443195974Sjamie					vfs_opterror(opts,
1444195974Sjamie					    "IPv6 addresses clash");
1445195974Sjamie					goto done_deref_locked;
1446192895Sjamie				}
1447192895Sjamie			}
1448191673Sjamie		}
1449192895Sjamie	}
1450191673Sjamie#endif
1451192895Sjamie	onamelen = namelen = 0;
1452192895Sjamie	if (name != NULL) {
1453191673Sjamie		/* Give a default name of the jid. */
1454191673Sjamie		if (name[0] == '\0')
1455191673Sjamie			snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1456196835Sjamie		else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1457196835Sjamie		    *p == '\0')) {
1458191673Sjamie			error = EINVAL;
1459196835Sjamie			vfs_opterror(opts,
1460196835Sjamie			    "name cannot be numeric (unless it is the jid)");
1461192895Sjamie			goto done_deref_locked;
1462191673Sjamie		}
1463191673Sjamie		/*
1464192895Sjamie		 * Make sure the name isn't too long for the prison or its
1465192895Sjamie		 * children.
1466191673Sjamie		 */
1467192895Sjamie		onamelen = strlen(pr->pr_name);
1468192895Sjamie		namelen = strlen(name);
1469192895Sjamie		if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1470192895Sjamie			error = ENAMETOOLONG;
1471192895Sjamie			goto done_deref_locked;
1472192895Sjamie		}
1473192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1474192895Sjamie			if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1475192895Sjamie			    sizeof(pr->pr_name)) {
1476192895Sjamie				error = ENAMETOOLONG;
1477192895Sjamie				goto done_deref_locked;
1478192895Sjamie			}
1479192895Sjamie		}
1480191673Sjamie	}
1481192895Sjamie	if (pr_allow & ~ppr->pr_allow) {
1482192895Sjamie		error = EPERM;
1483192895Sjamie		goto done_deref_locked;
1484192895Sjamie	}
1485185435Sbz
1486191673Sjamie	/* Set the parameters of the prison. */
1487191673Sjamie#ifdef INET
1488192895Sjamie	redo_ip4 = 0;
1489195974Sjamie	if (pr_flags & PR_IP4_USER) {
1490195974Sjamie		pr->pr_flags |= PR_IP4;
1491195974Sjamie		free(pr->pr_ip4, M_PRISON);
1492195974Sjamie		pr->pr_ip4s = ip4s;
1493195974Sjamie		pr->pr_ip4 = ip4;
1494195974Sjamie		ip4 = NULL;
1495192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1496195945Sjamie#ifdef VIMAGE
1497195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1498195945Sjamie				descend = 0;
1499195945Sjamie				continue;
1500195945Sjamie			}
1501195945Sjamie#endif
1502192895Sjamie			if (prison_restrict_ip4(tpr, NULL)) {
1503192895Sjamie				redo_ip4 = 1;
1504192895Sjamie				descend = 0;
1505192895Sjamie			}
1506192895Sjamie		}
1507185435Sbz	}
1508191673Sjamie#endif
1509191673Sjamie#ifdef INET6
1510192895Sjamie	redo_ip6 = 0;
1511195974Sjamie	if (pr_flags & PR_IP6_USER) {
1512195974Sjamie		pr->pr_flags |= PR_IP6;
1513195974Sjamie		free(pr->pr_ip6, M_PRISON);
1514195974Sjamie		pr->pr_ip6s = ip6s;
1515195974Sjamie		pr->pr_ip6 = ip6;
1516195974Sjamie		ip6 = NULL;
1517192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1518195945Sjamie#ifdef VIMAGE
1519195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1520195945Sjamie				descend = 0;
1521195945Sjamie				continue;
1522195945Sjamie			}
1523195945Sjamie#endif
1524192895Sjamie			if (prison_restrict_ip6(tpr, NULL)) {
1525192895Sjamie				redo_ip6 = 1;
1526192895Sjamie				descend = 0;
1527192895Sjamie			}
1528192895Sjamie		}
1529191673Sjamie	}
1530191673Sjamie#endif
1531192895Sjamie	if (gotslevel) {
1532191673Sjamie		pr->pr_securelevel = slevel;
1533192895Sjamie		/* Set all child jails to be at least this level. */
1534192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1535192895Sjamie			if (tpr->pr_securelevel < slevel)
1536192895Sjamie				tpr->pr_securelevel = slevel;
1537192895Sjamie	}
1538194762Sjamie	if (gotchildmax) {
1539194762Sjamie		pr->pr_childmax = childmax;
1540194762Sjamie		/* Set all child jails to under this limit. */
1541194762Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1542194762Sjamie			if (tpr->pr_childmax > childmax - level)
1543194762Sjamie				tpr->pr_childmax = childmax > level
1544194762Sjamie				    ? childmax - level : 0;
1545194762Sjamie	}
1546192895Sjamie	if (gotenforce) {
1547192895Sjamie		pr->pr_enforce_statfs = enforce;
1548192895Sjamie		/* Pass this restriction on to the children. */
1549192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1550192895Sjamie			if (tpr->pr_enforce_statfs < enforce)
1551192895Sjamie				tpr->pr_enforce_statfs = enforce;
1552192895Sjamie	}
1553192895Sjamie	if (name != NULL) {
1554192895Sjamie		if (ppr == &prison0)
1555192895Sjamie			strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1556192895Sjamie		else
1557192895Sjamie			snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1558192895Sjamie			    ppr->pr_name, name);
1559192895Sjamie		/* Change this component of child names. */
1560192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1561192895Sjamie			bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1562192895Sjamie			    strlen(tpr->pr_name + onamelen) + 1);
1563192895Sjamie			bcopy(pr->pr_name, tpr->pr_name, namelen);
1564192895Sjamie		}
1565192895Sjamie	}
1566191673Sjamie	if (path != NULL) {
1567192895Sjamie		/* Try to keep a real-rooted full pathname. */
1568192895Sjamie		if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1569192895Sjamie			snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1570192895Sjamie			    mypr->pr_path, path);
1571192895Sjamie		else
1572192895Sjamie			strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1573191673Sjamie		pr->pr_root = root;
1574191673Sjamie	}
1575193066Sjamie	if (PR_HOST & ch_flags & ~pr_flags) {
1576193066Sjamie		if (pr->pr_flags & PR_HOST) {
1577193066Sjamie			/*
1578193066Sjamie			 * Copy the parent's host info.  As with pr_ip4 above,
1579193066Sjamie			 * the lack of a lock on the parent is not a problem;
1580193066Sjamie			 * it is always set with allprison_lock at least
1581193066Sjamie			 * shared, and is held exclusively here.
1582193066Sjamie			 */
1583194118Sjamie			strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1584194118Sjamie			    sizeof(pr->pr_hostname));
1585194118Sjamie			strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1586194118Sjamie			    sizeof(pr->pr_domainname));
1587194118Sjamie			strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1588194118Sjamie			    sizeof(pr->pr_hostuuid));
1589193066Sjamie			pr->pr_hostid = pr->pr_parent->pr_hostid;
1590193066Sjamie		}
1591193066Sjamie	} else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1592193066Sjamie		/* Set this prison, and any descendants without PR_HOST. */
1593193066Sjamie		if (host != NULL)
1594194118Sjamie			strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1595193066Sjamie		if (domain != NULL)
1596194118Sjamie			strlcpy(pr->pr_domainname, domain,
1597194118Sjamie			    sizeof(pr->pr_domainname));
1598193066Sjamie		if (uuid != NULL)
1599194118Sjamie			strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1600193066Sjamie		if (gothid)
1601193066Sjamie			pr->pr_hostid = hid;
1602193066Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1603193066Sjamie			if (tpr->pr_flags & PR_HOST)
1604193066Sjamie				descend = 0;
1605193066Sjamie			else {
1606193066Sjamie				if (host != NULL)
1607194118Sjamie					strlcpy(tpr->pr_hostname,
1608194118Sjamie					    pr->pr_hostname,
1609194118Sjamie					    sizeof(tpr->pr_hostname));
1610193066Sjamie				if (domain != NULL)
1611194118Sjamie					strlcpy(tpr->pr_domainname,
1612194118Sjamie					    pr->pr_domainname,
1613194118Sjamie					    sizeof(tpr->pr_domainname));
1614193066Sjamie				if (uuid != NULL)
1615194118Sjamie					strlcpy(tpr->pr_hostuuid,
1616194118Sjamie					    pr->pr_hostuuid,
1617194118Sjamie					    sizeof(tpr->pr_hostuuid));
1618193066Sjamie				if (gothid)
1619193066Sjamie					tpr->pr_hostid = hid;
1620193066Sjamie			}
1621193066Sjamie		}
1622193066Sjamie	}
1623192895Sjamie	if ((tallow = ch_allow & ~pr_allow)) {
1624192895Sjamie		/* Clear allow bits in all children. */
1625192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1626192895Sjamie			tpr->pr_allow &= ~tallow;
1627192895Sjamie	}
1628192895Sjamie	pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1629191673Sjamie	/*
1630191673Sjamie	 * Persistent prisons get an extra reference, and prisons losing their
1631191673Sjamie	 * persist flag lose that reference.  Only do this for existing prisons
1632191673Sjamie	 * for now, so new ones will remain unseen until after the module
1633191673Sjamie	 * handlers have completed.
1634191673Sjamie	 */
1635191673Sjamie	if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1636191673Sjamie		if (pr_flags & PR_PERSIST) {
1637191673Sjamie			pr->pr_ref++;
1638191673Sjamie			pr->pr_uref++;
1639191673Sjamie		} else {
1640191673Sjamie			pr->pr_ref--;
1641191673Sjamie			pr->pr_uref--;
1642191673Sjamie		}
1643191673Sjamie	}
1644191673Sjamie	pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1645191673Sjamie	mtx_unlock(&pr->pr_mtx);
1646185435Sbz
1647192895Sjamie	/* Locks may have prevented a complete restriction of child IP
1648192895Sjamie	 * addresses.  If so, allocate some more memory and try again.
1649192895Sjamie	 */
1650192895Sjamie#ifdef INET
1651192895Sjamie	while (redo_ip4) {
1652192895Sjamie		ip4s = pr->pr_ip4s;
1653192895Sjamie		ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1654192895Sjamie		mtx_lock(&pr->pr_mtx);
1655192895Sjamie		redo_ip4 = 0;
1656192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1657195945Sjamie#ifdef VIMAGE
1658195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1659195945Sjamie				descend = 0;
1660195945Sjamie				continue;
1661195945Sjamie			}
1662195945Sjamie#endif
1663192895Sjamie			if (prison_restrict_ip4(tpr, ip4)) {
1664192895Sjamie				if (ip4 != NULL)
1665192895Sjamie					ip4 = NULL;
1666192895Sjamie				else
1667192895Sjamie					redo_ip4 = 1;
1668192895Sjamie			}
1669192895Sjamie		}
1670192895Sjamie		mtx_unlock(&pr->pr_mtx);
1671192895Sjamie	}
1672192895Sjamie#endif
1673192895Sjamie#ifdef INET6
1674192895Sjamie	while (redo_ip6) {
1675192895Sjamie		ip6s = pr->pr_ip6s;
1676192895Sjamie		ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1677192895Sjamie		mtx_lock(&pr->pr_mtx);
1678192895Sjamie		redo_ip6 = 0;
1679192895Sjamie		FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1680195945Sjamie#ifdef VIMAGE
1681195945Sjamie			if (tpr->pr_flags & PR_VNET) {
1682195945Sjamie				descend = 0;
1683195945Sjamie				continue;
1684195945Sjamie			}
1685195945Sjamie#endif
1686192895Sjamie			if (prison_restrict_ip6(tpr, ip6)) {
1687192895Sjamie				if (ip6 != NULL)
1688192895Sjamie					ip6 = NULL;
1689192895Sjamie				else
1690192895Sjamie					redo_ip6 = 1;
1691192895Sjamie			}
1692192895Sjamie		}
1693192895Sjamie		mtx_unlock(&pr->pr_mtx);
1694192895Sjamie	}
1695192895Sjamie#endif
1696192895Sjamie
1697191673Sjamie	/* Let the modules do their work. */
1698191673Sjamie	sx_downgrade(&allprison_lock);
1699191673Sjamie	if (created) {
1700191673Sjamie		error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1701191673Sjamie		if (error) {
1702191673Sjamie			prison_deref(pr, PD_LIST_SLOCKED);
1703191673Sjamie			goto done_errmsg;
1704191673Sjamie		}
1705191673Sjamie	}
1706191673Sjamie	error = osd_jail_call(pr, PR_METHOD_SET, opts);
1707191673Sjamie	if (error) {
1708191673Sjamie		prison_deref(pr, created
1709191673Sjamie		    ? PD_LIST_SLOCKED
1710191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1711191673Sjamie		goto done_errmsg;
1712191673Sjamie	}
1713191673Sjamie
1714191673Sjamie	/* Attach this process to the prison if requested. */
1715191673Sjamie	if (flags & JAIL_ATTACH) {
1716191673Sjamie		mtx_lock(&pr->pr_mtx);
1717191673Sjamie		error = do_jail_attach(td, pr);
1718191673Sjamie		if (error) {
1719191673Sjamie			vfs_opterror(opts, "attach failed");
1720191673Sjamie			if (!created)
1721191673Sjamie				prison_deref(pr, PD_DEREF);
1722191673Sjamie			goto done_errmsg;
1723191673Sjamie		}
1724191673Sjamie	}
1725191673Sjamie
1726191673Sjamie	/*
1727191673Sjamie	 * Now that it is all there, drop the temporary reference from existing
1728191673Sjamie	 * prisons.  Or add a reference to newly created persistent prisons
1729191673Sjamie	 * (which was not done earlier so that the prison would not be publicly
1730191673Sjamie	 * visible).
1731191673Sjamie	 */
1732191673Sjamie	if (!created) {
1733191673Sjamie		prison_deref(pr, (flags & JAIL_ATTACH)
1734191673Sjamie		    ? PD_DEREF
1735191673Sjamie		    : PD_DEREF | PD_LIST_SLOCKED);
1736191673Sjamie	} else {
1737191673Sjamie		if (pr_flags & PR_PERSIST) {
1738191673Sjamie			mtx_lock(&pr->pr_mtx);
1739191673Sjamie			pr->pr_ref++;
1740191673Sjamie			pr->pr_uref++;
1741191673Sjamie			mtx_unlock(&pr->pr_mtx);
1742191673Sjamie		}
1743191673Sjamie		if (!(flags & JAIL_ATTACH))
1744191673Sjamie			sx_sunlock(&allprison_lock);
1745191673Sjamie	}
1746191673Sjamie	td->td_retval[0] = pr->pr_id;
1747191673Sjamie	goto done_errmsg;
1748191673Sjamie
1749192895Sjamie done_deref_locked:
1750192895Sjamie	prison_deref(pr, created
1751192895Sjamie	    ? PD_LOCKED | PD_LIST_XLOCKED
1752192895Sjamie	    : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1753192895Sjamie	goto done_releroot;
1754191673Sjamie done_unlock_list:
1755191673Sjamie	sx_xunlock(&allprison_lock);
1756191673Sjamie done_releroot:
1757191673Sjamie	if (root != NULL) {
1758191673Sjamie		vfslocked = VFS_LOCK_GIANT(root->v_mount);
1759191673Sjamie		vrele(root);
1760191673Sjamie		VFS_UNLOCK_GIANT(vfslocked);
1761191673Sjamie	}
1762191673Sjamie done_errmsg:
1763191673Sjamie	if (error) {
1764191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1765191673Sjamie		if (errmsg_len > 0) {
1766191673Sjamie			errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1767191673Sjamie			if (errmsg_pos > 0) {
1768191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE)
1769191673Sjamie					bcopy(errmsg,
1770191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1771191673Sjamie					   errmsg_len);
1772191673Sjamie				else
1773191673Sjamie					copyout(errmsg,
1774191673Sjamie					   optuio->uio_iov[errmsg_pos].iov_base,
1775191673Sjamie					   errmsg_len);
1776191673Sjamie			}
1777191673Sjamie		}
1778191673Sjamie	}
1779191673Sjamie done_free:
1780191673Sjamie#ifdef INET
1781191673Sjamie	free(ip4, M_PRISON);
1782191673Sjamie#endif
1783191673Sjamie#ifdef INET6
1784191673Sjamie	free(ip6, M_PRISON);
1785191673Sjamie#endif
1786191673Sjamie	vfs_freeopts(opts);
1787191673Sjamie	return (error);
1788191673Sjamie}
1789191673Sjamie
1790191673Sjamie
179182710Sdillon/*
1792191673Sjamie * struct jail_get_args {
1793191673Sjamie *	struct iovec *iovp;
1794191673Sjamie *	unsigned int iovcnt;
1795191673Sjamie *	int flags;
1796114168Smike * };
179782710Sdillon */
179846155Sphkint
1799191673Sjamiejail_get(struct thread *td, struct jail_get_args *uap)
180046155Sphk{
1801191673Sjamie	struct uio *auio;
1802185435Sbz	int error;
1803185435Sbz
1804191673Sjamie	/* Check that we have an even number of iovecs. */
1805191673Sjamie	if (uap->iovcnt & 1)
1806191673Sjamie		return (EINVAL);
1807191673Sjamie
1808191673Sjamie	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1809185435Sbz	if (error)
1810185435Sbz		return (error);
1811191673Sjamie	error = kern_jail_get(td, auio, uap->flags);
1812191673Sjamie	if (error == 0)
1813191673Sjamie		error = copyout(auio->uio_iov, uap->iovp,
1814191673Sjamie		    uap->iovcnt * sizeof (struct iovec));
1815191673Sjamie	free(auio, M_IOV);
1816191673Sjamie	return (error);
1817191673Sjamie}
1818185435Sbz
1819191673Sjamieint
1820191673Sjamiekern_jail_get(struct thread *td, struct uio *optuio, int flags)
1821191673Sjamie{
1822192895Sjamie	struct prison *pr, *mypr;
1823191673Sjamie	struct vfsopt *opt;
1824191673Sjamie	struct vfsoptlist *opts;
1825191673Sjamie	char *errmsg, *name;
1826192895Sjamie	int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1827185435Sbz
1828191673Sjamie	if (flags & ~JAIL_GET_MASK)
1829191673Sjamie		return (EINVAL);
1830185435Sbz
1831191673Sjamie	/* Get the parameter list. */
1832191673Sjamie	error = vfs_buildopts(optuio, &opts);
1833191673Sjamie	if (error)
1834191673Sjamie		return (error);
1835191673Sjamie	errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1836192895Sjamie	mypr = td->td_ucred->cr_prison;
1837185435Sbz
1838191673Sjamie	/*
1839191673Sjamie	 * Find the prison specified by one of: lastjid, jid, name.
1840191673Sjamie	 */
1841191673Sjamie	sx_slock(&allprison_lock);
1842191673Sjamie	error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1843191673Sjamie	if (error == 0) {
1844191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list) {
1845192895Sjamie			if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1846191673Sjamie				mtx_lock(&pr->pr_mtx);
1847191673Sjamie				if (pr->pr_ref > 0 &&
1848191673Sjamie				    (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1849191673Sjamie					break;
1850191673Sjamie				mtx_unlock(&pr->pr_mtx);
1851191673Sjamie			}
1852191673Sjamie		}
1853191673Sjamie		if (pr != NULL)
1854191673Sjamie			goto found_prison;
1855191673Sjamie		error = ENOENT;
1856191673Sjamie		vfs_opterror(opts, "no jail after %d", jid);
1857191673Sjamie		goto done_unlock_list;
1858191673Sjamie	} else if (error != ENOENT)
1859191673Sjamie		goto done_unlock_list;
1860185435Sbz
1861191673Sjamie	error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1862191673Sjamie	if (error == 0) {
1863191673Sjamie		if (jid != 0) {
1864192895Sjamie			pr = prison_find_child(mypr, jid);
1865191673Sjamie			if (pr != NULL) {
1866191673Sjamie				if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1867191673Sjamie					mtx_unlock(&pr->pr_mtx);
1868191673Sjamie					error = ENOENT;
1869191673Sjamie					vfs_opterror(opts, "jail %d is dying",
1870191673Sjamie					    jid);
1871191673Sjamie					goto done_unlock_list;
1872191673Sjamie				}
1873191673Sjamie				goto found_prison;
1874191673Sjamie			}
1875191673Sjamie			error = ENOENT;
1876191673Sjamie			vfs_opterror(opts, "jail %d not found", jid);
1877191673Sjamie			goto done_unlock_list;
1878191673Sjamie		}
1879191673Sjamie	} else if (error != ENOENT)
1880191673Sjamie		goto done_unlock_list;
188146155Sphk
1882191673Sjamie	error = vfs_getopt(opts, "name", (void **)&name, &len);
1883191673Sjamie	if (error == 0) {
1884191673Sjamie		if (len == 0 || name[len - 1] != '\0') {
1885191673Sjamie			error = EINVAL;
1886191673Sjamie			goto done_unlock_list;
1887191673Sjamie		}
1888192895Sjamie		pr = prison_find_name(mypr, name);
1889191673Sjamie		if (pr != NULL) {
1890191673Sjamie			if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1891191673Sjamie				mtx_unlock(&pr->pr_mtx);
1892191673Sjamie				error = ENOENT;
1893191673Sjamie				vfs_opterror(opts, "jail \"%s\" is dying",
1894191673Sjamie				    name);
1895191673Sjamie				goto done_unlock_list;
1896191673Sjamie			}
1897191673Sjamie			goto found_prison;
1898191673Sjamie		}
1899191673Sjamie		error = ENOENT;
1900191673Sjamie		vfs_opterror(opts, "jail \"%s\" not found", name);
1901191673Sjamie		goto done_unlock_list;
1902191673Sjamie	} else if (error != ENOENT)
1903191673Sjamie		goto done_unlock_list;
1904185435Sbz
1905191673Sjamie	vfs_opterror(opts, "no jail specified");
1906191673Sjamie	error = ENOENT;
1907191673Sjamie	goto done_unlock_list;
1908191673Sjamie
1909191673Sjamie found_prison:
1910191673Sjamie	/* Get the parameters of the prison. */
1911191673Sjamie	pr->pr_ref++;
1912191673Sjamie	locked = PD_LOCKED;
1913191673Sjamie	td->td_retval[0] = pr->pr_id;
1914191673Sjamie	error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1915191673Sjamie	if (error != 0 && error != ENOENT)
1916191673Sjamie		goto done_deref;
1917192895Sjamie	i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1918192895Sjamie	error = vfs_setopt(opts, "parent", &i, sizeof(i));
1919191673Sjamie	if (error != 0 && error != ENOENT)
1920191673Sjamie		goto done_deref;
1921192895Sjamie	error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1922192895Sjamie	if (error != 0 && error != ENOENT)
1923192895Sjamie		goto done_deref;
1924192895Sjamie	error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1925191673Sjamie	    sizeof(pr->pr_cpuset->cs_id));
1926191673Sjamie	if (error != 0 && error != ENOENT)
1927191673Sjamie		goto done_deref;
1928192895Sjamie	error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1929191673Sjamie	if (error != 0 && error != ENOENT)
1930191673Sjamie		goto done_deref;
1931191673Sjamie#ifdef INET
1932191673Sjamie	error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1933191673Sjamie	    pr->pr_ip4s * sizeof(*pr->pr_ip4));
1934191673Sjamie	if (error != 0 && error != ENOENT)
1935191673Sjamie		goto done_deref;
1936191673Sjamie#endif
1937191673Sjamie#ifdef INET6
1938191673Sjamie	error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1939191673Sjamie	    pr->pr_ip6s * sizeof(*pr->pr_ip6));
1940191673Sjamie	if (error != 0 && error != ENOENT)
1941191673Sjamie		goto done_deref;
1942191673Sjamie#endif
1943191673Sjamie	error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1944191673Sjamie	    sizeof(pr->pr_securelevel));
1945191673Sjamie	if (error != 0 && error != ENOENT)
1946191673Sjamie		goto done_deref;
1947194762Sjamie	error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1948194762Sjamie	    sizeof(pr->pr_childcount));
1949194762Sjamie	if (error != 0 && error != ENOENT)
1950194762Sjamie		goto done_deref;
1951194762Sjamie	error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1952194762Sjamie	    sizeof(pr->pr_childmax));
1953194762Sjamie	if (error != 0 && error != ENOENT)
1954194762Sjamie		goto done_deref;
1955194118Sjamie	error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1956191673Sjamie	if (error != 0 && error != ENOENT)
1957191673Sjamie		goto done_deref;
1958194118Sjamie	error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1959193066Sjamie	if (error != 0 && error != ENOENT)
1960193066Sjamie		goto done_deref;
1961194118Sjamie	error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1962193066Sjamie	if (error != 0 && error != ENOENT)
1963193066Sjamie		goto done_deref;
1964193066Sjamie#ifdef COMPAT_IA32
1965193066Sjamie	if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1966193066Sjamie		uint32_t hid32 = pr->pr_hostid;
1967193066Sjamie
1968193066Sjamie		error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1969193066Sjamie	} else
1970193066Sjamie#endif
1971193066Sjamie	error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1972193066Sjamie	    sizeof(pr->pr_hostid));
1973193066Sjamie	if (error != 0 && error != ENOENT)
1974193066Sjamie		goto done_deref;
1975192895Sjamie	error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1976192895Sjamie	    sizeof(pr->pr_enforce_statfs));
1977191673Sjamie	if (error != 0 && error != ENOENT)
1978191673Sjamie		goto done_deref;
1979192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1980192895Sjamie	    fi++) {
1981192895Sjamie		if (pr_flag_names[fi] == NULL)
1982192895Sjamie			continue;
1983192895Sjamie		i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1984192895Sjamie		error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1985192895Sjamie		if (error != 0 && error != ENOENT)
1986192895Sjamie			goto done_deref;
1987192895Sjamie		i = !i;
1988192895Sjamie		error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1989192895Sjamie		if (error != 0 && error != ENOENT)
1990192895Sjamie			goto done_deref;
1991192895Sjamie	}
1992195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1993195870Sjamie	    fi++) {
1994195870Sjamie		i = pr->pr_flags &
1995195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1996195870Sjamie		i = pr_flag_jailsys[fi].disable &&
1997195870Sjamie		      (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1998195870Sjamie		    : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
1999195870Sjamie		    : JAIL_SYS_INHERIT;
2000195870Sjamie		error =
2001195870Sjamie		    vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2002195870Sjamie		if (error != 0 && error != ENOENT)
2003195870Sjamie			goto done_deref;
2004195870Sjamie	}
2005192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2006192895Sjamie	    fi++) {
2007192895Sjamie		if (pr_allow_names[fi] == NULL)
2008192895Sjamie			continue;
2009192895Sjamie		i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2010192895Sjamie		error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2011192895Sjamie		if (error != 0 && error != ENOENT)
2012192895Sjamie			goto done_deref;
2013192895Sjamie		i = !i;
2014192895Sjamie		error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2015192895Sjamie		if (error != 0 && error != ENOENT)
2016192895Sjamie			goto done_deref;
2017192895Sjamie	}
2018191673Sjamie	i = (pr->pr_uref == 0);
2019191673Sjamie	error = vfs_setopt(opts, "dying", &i, sizeof(i));
2020191673Sjamie	if (error != 0 && error != ENOENT)
2021191673Sjamie		goto done_deref;
2022191673Sjamie	i = !i;
2023191673Sjamie	error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2024191673Sjamie	if (error != 0 && error != ENOENT)
2025191673Sjamie		goto done_deref;
2026191673Sjamie
2027191673Sjamie	/* Get the module parameters. */
2028191673Sjamie	mtx_unlock(&pr->pr_mtx);
2029191673Sjamie	locked = 0;
2030191673Sjamie	error = osd_jail_call(pr, PR_METHOD_GET, opts);
203146155Sphk	if (error)
2032191673Sjamie		goto done_deref;
2033191673Sjamie	prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
203484828Sjhb
2035191673Sjamie	/* By now, all parameters should have been noted. */
2036191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2037191673Sjamie		if (!opt->seen && strcmp(opt->name, "errmsg")) {
2038191673Sjamie			error = EINVAL;
2039191673Sjamie			vfs_opterror(opts, "unknown parameter: %s", opt->name);
2040191673Sjamie			goto done_errmsg;
2041191673Sjamie		}
2042185435Sbz	}
2043191673Sjamie
2044191673Sjamie	/* Write the fetched parameters back to userspace. */
2045191673Sjamie	error = 0;
2046191673Sjamie	TAILQ_FOREACH(opt, opts, link) {
2047191673Sjamie		if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2048191673Sjamie			pos = 2 * opt->pos + 1;
2049191673Sjamie			optuio->uio_iov[pos].iov_len = opt->len;
2050191673Sjamie			if (opt->value != NULL) {
2051191673Sjamie				if (optuio->uio_segflg == UIO_SYSSPACE) {
2052191673Sjamie					bcopy(opt->value,
2053191673Sjamie					    optuio->uio_iov[pos].iov_base,
2054191673Sjamie					    opt->len);
2055191673Sjamie				} else {
2056191673Sjamie					error = copyout(opt->value,
2057191673Sjamie					    optuio->uio_iov[pos].iov_base,
2058191673Sjamie					    opt->len);
2059191673Sjamie					if (error)
2060191673Sjamie						break;
2061191673Sjamie				}
2062191673Sjamie			}
2063191673Sjamie		}
2064185435Sbz	}
2065191673Sjamie	goto done_errmsg;
2066191673Sjamie
2067191673Sjamie done_deref:
2068191673Sjamie	prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2069191673Sjamie	goto done_errmsg;
2070191673Sjamie
2071191673Sjamie done_unlock_list:
2072191673Sjamie	sx_sunlock(&allprison_lock);
2073191673Sjamie done_errmsg:
2074191673Sjamie	if (error && errmsg_pos >= 0) {
2075191673Sjamie		vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2076191673Sjamie		errmsg_pos = 2 * errmsg_pos + 1;
2077191673Sjamie		if (errmsg_len > 0) {
2078191673Sjamie			if (optuio->uio_segflg == UIO_SYSSPACE)
2079191673Sjamie				bcopy(errmsg,
2080191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2081191673Sjamie				    errmsg_len);
2082191673Sjamie			else
2083191673Sjamie				copyout(errmsg,
2084191673Sjamie				    optuio->uio_iov[errmsg_pos].iov_base,
2085191673Sjamie				    errmsg_len);
2086191673Sjamie		}
2087185435Sbz	}
2088191673Sjamie	vfs_freeopts(opts);
2089191673Sjamie	return (error);
2090191673Sjamie}
2091113275Smike
2092192895Sjamie
2093191673Sjamie/*
2094191673Sjamie * struct jail_remove_args {
2095191673Sjamie *	int jid;
2096191673Sjamie * };
2097191673Sjamie */
2098191673Sjamieint
2099191673Sjamiejail_remove(struct thread *td, struct jail_remove_args *uap)
2100191673Sjamie{
2101192895Sjamie	struct prison *pr, *cpr, *lpr, *tpr;
2102192895Sjamie	int descend, error;
2103185435Sbz
2104191673Sjamie	error = priv_check(td, PRIV_JAIL_REMOVE);
2105185435Sbz	if (error)
2106191673Sjamie		return (error);
2107185435Sbz
2108185435Sbz	sx_xlock(&allprison_lock);
2109192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2110191673Sjamie	if (pr == NULL) {
2111185435Sbz		sx_xunlock(&allprison_lock);
2112191673Sjamie		return (EINVAL);
2113185435Sbz	}
2114185435Sbz
2115192895Sjamie	/* Remove all descendants of this prison, then remove this prison. */
2116192895Sjamie	pr->pr_ref++;
2117192895Sjamie	pr->pr_flags |= PR_REMOVE;
2118192895Sjamie	if (!LIST_EMPTY(&pr->pr_children)) {
2119192895Sjamie		mtx_unlock(&pr->pr_mtx);
2120192895Sjamie		lpr = NULL;
2121192895Sjamie		FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2122192895Sjamie			mtx_lock(&cpr->pr_mtx);
2123192895Sjamie			if (cpr->pr_ref > 0) {
2124192895Sjamie				tpr = cpr;
2125192895Sjamie				cpr->pr_ref++;
2126192895Sjamie				cpr->pr_flags |= PR_REMOVE;
2127192895Sjamie			} else {
2128192895Sjamie				/* Already removed - do not do it again. */
2129192895Sjamie				tpr = NULL;
2130192895Sjamie			}
2131192895Sjamie			mtx_unlock(&cpr->pr_mtx);
2132192895Sjamie			if (lpr != NULL) {
2133192895Sjamie				mtx_lock(&lpr->pr_mtx);
2134192895Sjamie				prison_remove_one(lpr);
2135192895Sjamie				sx_xlock(&allprison_lock);
2136192895Sjamie			}
2137192895Sjamie			lpr = tpr;
2138192895Sjamie		}
2139192895Sjamie		if (lpr != NULL) {
2140192895Sjamie			mtx_lock(&lpr->pr_mtx);
2141192895Sjamie			prison_remove_one(lpr);
2142192895Sjamie			sx_xlock(&allprison_lock);
2143192895Sjamie		}
2144192895Sjamie		mtx_lock(&pr->pr_mtx);
2145192895Sjamie	}
2146192895Sjamie	prison_remove_one(pr);
2147192895Sjamie	return (0);
2148192895Sjamie}
2149192895Sjamie
2150192895Sjamiestatic void
2151192895Sjamieprison_remove_one(struct prison *pr)
2152192895Sjamie{
2153192895Sjamie	struct proc *p;
2154192895Sjamie	int deuref;
2155192895Sjamie
2156191673Sjamie	/* If the prison was persistent, it is not anymore. */
2157191673Sjamie	deuref = 0;
2158191673Sjamie	if (pr->pr_flags & PR_PERSIST) {
2159191673Sjamie		pr->pr_ref--;
2160191673Sjamie		deuref = PD_DEUREF;
2161191673Sjamie		pr->pr_flags &= ~PR_PERSIST;
2162179881Sdelphij	}
2163113275Smike
2164192895Sjamie	/*
2165192895Sjamie	 * jail_remove added a reference.  If that's the only one, remove
2166192895Sjamie	 * the prison now.
2167192895Sjamie	 */
2168192895Sjamie	KASSERT(pr->pr_ref > 0,
2169192895Sjamie	    ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2170192895Sjamie	if (pr->pr_ref == 1) {
2171191673Sjamie		prison_deref(pr,
2172191673Sjamie		    deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2173192895Sjamie		return;
2174191673Sjamie	}
2175191673Sjamie
2176113275Smike	mtx_unlock(&pr->pr_mtx);
2177191673Sjamie	sx_xunlock(&allprison_lock);
2178191673Sjamie	/*
2179191673Sjamie	 * Kill all processes unfortunate enough to be attached to this prison.
2180191673Sjamie	 */
2181191673Sjamie	sx_slock(&allproc_lock);
2182191673Sjamie	LIST_FOREACH(p, &allproc, p_list) {
2183191673Sjamie		PROC_LOCK(p);
2184191673Sjamie		if (p->p_state != PRS_NEW && p->p_ucred &&
2185191673Sjamie		    p->p_ucred->cr_prison == pr)
2186191673Sjamie			psignal(p, SIGKILL);
2187191673Sjamie		PROC_UNLOCK(p);
2188191673Sjamie	}
2189191673Sjamie	sx_sunlock(&allproc_lock);
2190192895Sjamie	/* Remove the temporary reference added by jail_remove. */
2191191673Sjamie	prison_deref(pr, deuref | PD_DEREF);
2192113275Smike}
2193113275Smike
2194190466Sjamie
2195113275Smike/*
2196114168Smike * struct jail_attach_args {
2197114168Smike *	int jid;
2198114168Smike * };
2199113275Smike */
2200113275Smikeint
2201114168Smikejail_attach(struct thread *td, struct jail_attach_args *uap)
2202113275Smike{
2203113275Smike	struct prison *pr;
2204191673Sjamie	int error;
2205167309Spjd
2206164032Srwatson	error = priv_check(td, PRIV_JAIL_ATTACH);
2207126023Snectar	if (error)
2208126023Snectar		return (error);
2209126023Snectar
2210168401Spjd	sx_slock(&allprison_lock);
2211192895Sjamie	pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2212113275Smike	if (pr == NULL) {
2213168401Spjd		sx_sunlock(&allprison_lock);
2214113275Smike		return (EINVAL);
2215113275Smike	}
2216185435Sbz
2217185435Sbz	/*
2218185435Sbz	 * Do not allow a process to attach to a prison that is not
2219191673Sjamie	 * considered to be "alive".
2220185435Sbz	 */
2221191673Sjamie	if (pr->pr_uref == 0) {
2222185435Sbz		mtx_unlock(&pr->pr_mtx);
2223185435Sbz		sx_sunlock(&allprison_lock);
2224185435Sbz		return (EINVAL);
2225185435Sbz	}
2226191673Sjamie
2227191673Sjamie	return (do_jail_attach(td, pr));
2228191673Sjamie}
2229191673Sjamie
2230191673Sjamiestatic int
2231191673Sjamiedo_jail_attach(struct thread *td, struct prison *pr)
2232191673Sjamie{
2233192895Sjamie	struct prison *ppr;
2234191673Sjamie	struct proc *p;
2235191673Sjamie	struct ucred *newcred, *oldcred;
2236191673Sjamie	int vfslocked, error;
2237191673Sjamie
2238191673Sjamie	/*
2239191673Sjamie	 * XXX: Note that there is a slight race here if two threads
2240191673Sjamie	 * in the same privileged process attempt to attach to two
2241191673Sjamie	 * different jails at the same time.  It is important for
2242191673Sjamie	 * user processes not to do this, or they might end up with
2243191673Sjamie	 * a process root from one prison, but attached to the jail
2244191673Sjamie	 * of another.
2245191673Sjamie	 */
2246113275Smike	pr->pr_ref++;
2247191673Sjamie	pr->pr_uref++;
2248113275Smike	mtx_unlock(&pr->pr_mtx);
2249191673Sjamie
2250191673Sjamie	/* Let modules do whatever they need to prepare for attaching. */
2251191673Sjamie	error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2252191673Sjamie	if (error) {
2253191673Sjamie		prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2254191673Sjamie		return (error);
2255191673Sjamie	}
2256168401Spjd	sx_sunlock(&allprison_lock);
2257113275Smike
2258185435Sbz	/*
2259185435Sbz	 * Reparent the newly attached process to this jail.
2260185435Sbz	 */
2261192895Sjamie	ppr = td->td_ucred->cr_prison;
2262191673Sjamie	p = td->td_proc;
2263185435Sbz	error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2264185435Sbz	if (error)
2265191673Sjamie		goto e_revert_osd;
2266185435Sbz
2267150652Scsjp	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2268175202Sattilio	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2269113275Smike	if ((error = change_dir(pr->pr_root, td)) != 0)
2270113275Smike		goto e_unlock;
2271113275Smike#ifdef MAC
2272172930Srwatson	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2273113275Smike		goto e_unlock;
2274113275Smike#endif
2275175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2276191673Sjamie	if ((error = change_root(pr->pr_root, td)))
2277191673Sjamie		goto e_unlock_giant;
2278150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2279113275Smike
228084828Sjhb	newcred = crget();
228184828Sjhb	PROC_LOCK(p);
228284828Sjhb	oldcred = p->p_ucred;
2283113275Smike	setsugid(p);
228484828Sjhb	crcopy(newcred, oldcred);
2285113630Sjhb	newcred->cr_prison = pr;
228684828Sjhb	p->p_ucred = newcred;
228784828Sjhb	PROC_UNLOCK(p);
228884828Sjhb	crfree(oldcred);
2289192895Sjamie	prison_deref(ppr, PD_DEREF | PD_DEUREF);
229046155Sphk	return (0);
2291191673Sjamie e_unlock:
2292175294Sattilio	VOP_UNLOCK(pr->pr_root, 0);
2293191673Sjamie e_unlock_giant:
2294150652Scsjp	VFS_UNLOCK_GIANT(vfslocked);
2295191673Sjamie e_revert_osd:
2296191673Sjamie	/* Tell modules this thread is still in its old jail after all. */
2297192895Sjamie	(void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2298191673Sjamie	prison_deref(pr, PD_DEREF | PD_DEUREF);
229946155Sphk	return (error);
230046155Sphk}
230146155Sphk
2302192895Sjamie
2303113275Smike/*
2304113275Smike * Returns a locked prison instance, or NULL on failure.
2305113275Smike */
2306168399Spjdstruct prison *
2307113275Smikeprison_find(int prid)
2308113275Smike{
2309113275Smike	struct prison *pr;
2310113275Smike
2311168401Spjd	sx_assert(&allprison_lock, SX_LOCKED);
2312191673Sjamie	TAILQ_FOREACH(pr, &allprison, pr_list) {
2313113275Smike		if (pr->pr_id == prid) {
2314113275Smike			mtx_lock(&pr->pr_mtx);
2315191673Sjamie			if (pr->pr_ref > 0)
2316191673Sjamie				return (pr);
2317191673Sjamie			mtx_unlock(&pr->pr_mtx);
2318113275Smike		}
2319113275Smike	}
2320113275Smike	return (NULL);
2321113275Smike}
2322113275Smike
2323191673Sjamie/*
2324192895Sjamie * Find a prison that is a descendant of mypr.  Returns a locked prison or NULL.
2325191673Sjamie */
2326191673Sjamiestruct prison *
2327192895Sjamieprison_find_child(struct prison *mypr, int prid)
2328191673Sjamie{
2329192895Sjamie	struct prison *pr;
2330192895Sjamie	int descend;
2331192895Sjamie
2332192895Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2333192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2334192895Sjamie		if (pr->pr_id == prid) {
2335192895Sjamie			mtx_lock(&pr->pr_mtx);
2336192895Sjamie			if (pr->pr_ref > 0)
2337192895Sjamie				return (pr);
2338192895Sjamie			mtx_unlock(&pr->pr_mtx);
2339192895Sjamie		}
2340192895Sjamie	}
2341192895Sjamie	return (NULL);
2342192895Sjamie}
2343192895Sjamie
2344192895Sjamie/*
2345192895Sjamie * Look for the name relative to mypr.  Returns a locked prison or NULL.
2346192895Sjamie */
2347192895Sjamiestruct prison *
2348192895Sjamieprison_find_name(struct prison *mypr, const char *name)
2349192895Sjamie{
2350191673Sjamie	struct prison *pr, *deadpr;
2351192895Sjamie	size_t mylen;
2352192895Sjamie	int descend;
2353191673Sjamie
2354191673Sjamie	sx_assert(&allprison_lock, SX_LOCKED);
2355192895Sjamie	mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2356191673Sjamie again:
2357191673Sjamie	deadpr = NULL;
2358192895Sjamie	FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2359192895Sjamie		if (!strcmp(pr->pr_name + mylen, name)) {
2360191673Sjamie			mtx_lock(&pr->pr_mtx);
2361191673Sjamie			if (pr->pr_ref > 0) {
2362191673Sjamie				if (pr->pr_uref > 0)
2363191673Sjamie					return (pr);
2364191673Sjamie				deadpr = pr;
2365191673Sjamie			}
2366191673Sjamie			mtx_unlock(&pr->pr_mtx);
2367191673Sjamie		}
2368191673Sjamie	}
2369192895Sjamie	/* There was no valid prison - perhaps there was a dying one. */
2370191673Sjamie	if (deadpr != NULL) {
2371191673Sjamie		mtx_lock(&deadpr->pr_mtx);
2372191673Sjamie		if (deadpr->pr_ref == 0) {
2373191673Sjamie			mtx_unlock(&deadpr->pr_mtx);
2374191673Sjamie			goto again;
2375191673Sjamie		}
2376191673Sjamie	}
2377191673Sjamie	return (deadpr);
2378191673Sjamie}
2379191673Sjamie
2380191673Sjamie/*
2381192895Sjamie * See if a prison has the specific flag set.
2382192895Sjamie */
2383192895Sjamieint
2384192895Sjamieprison_flag(struct ucred *cred, unsigned flag)
2385192895Sjamie{
2386192895Sjamie
2387192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2388192895Sjamie	return (cred->cr_prison->pr_flags & flag);
2389192895Sjamie}
2390192895Sjamie
2391192895Sjamieint
2392192895Sjamieprison_allow(struct ucred *cred, unsigned flag)
2393192895Sjamie{
2394192895Sjamie
2395192895Sjamie	/* This is an atomic read, so no locking is necessary. */
2396192895Sjamie	return (cred->cr_prison->pr_allow & flag);
2397192895Sjamie}
2398192895Sjamie
2399192895Sjamie/*
2400191673Sjamie * Remove a prison reference.  If that was the last reference, remove the
2401191673Sjamie * prison itself - but not in this context in case there are locks held.
2402191673Sjamie */
240372786Srwatsonvoid
2404185029Spjdprison_free_locked(struct prison *pr)
240572786Srwatson{
240672786Srwatson
2407185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
240872786Srwatson	pr->pr_ref--;
240972786Srwatson	if (pr->pr_ref == 0) {
2410168483Spjd		mtx_unlock(&pr->pr_mtx);
2411124882Srwatson		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2412144660Sjeff		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
241387275Srwatson		return;
241472786Srwatson	}
241587275Srwatson	mtx_unlock(&pr->pr_mtx);
241672786Srwatson}
241772786Srwatson
2418185029Spjdvoid
2419185029Spjdprison_free(struct prison *pr)
2420185029Spjd{
2421185029Spjd
2422185029Spjd	mtx_lock(&pr->pr_mtx);
2423185029Spjd	prison_free_locked(pr);
2424185029Spjd}
2425185029Spjd
2426124882Srwatsonstatic void
2427124882Srwatsonprison_complete(void *context, int pending)
2428124882Srwatson{
2429191673Sjamie
2430191673Sjamie	prison_deref((struct prison *)context, 0);
2431191673Sjamie}
2432191673Sjamie
2433191673Sjamie/*
2434191673Sjamie * Remove a prison reference (usually).  This internal version assumes no
2435191673Sjamie * mutexes are held, except perhaps the prison itself.  If there are no more
2436191673Sjamie * references, release and delist the prison.  On completion, the prison lock
2437191673Sjamie * and the allprison lock are both unlocked.
2438191673Sjamie */
2439191673Sjamiestatic void
2440191673Sjamieprison_deref(struct prison *pr, int flags)
2441191673Sjamie{
2442192895Sjamie	struct prison *ppr, *tpr;
2443150652Scsjp	int vfslocked;
2444124882Srwatson
2445191673Sjamie	if (!(flags & PD_LOCKED))
2446191673Sjamie		mtx_lock(&pr->pr_mtx);
2447192895Sjamie	/* Decrement the user references in a separate loop. */
2448191673Sjamie	if (flags & PD_DEUREF) {
2449192895Sjamie		for (tpr = pr;; tpr = tpr->pr_parent) {
2450192895Sjamie			if (tpr != pr)
2451192895Sjamie				mtx_lock(&tpr->pr_mtx);
2452192895Sjamie			if (--tpr->pr_uref > 0)
2453192895Sjamie				break;
2454192895Sjamie			KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2455192895Sjamie			mtx_unlock(&tpr->pr_mtx);
2456192895Sjamie		}
2457191673Sjamie		/* Done if there were only user references to remove. */
2458191673Sjamie		if (!(flags & PD_DEREF)) {
2459192895Sjamie			mtx_unlock(&tpr->pr_mtx);
2460191673Sjamie			if (flags & PD_LIST_SLOCKED)
2461191673Sjamie				sx_sunlock(&allprison_lock);
2462191673Sjamie			else if (flags & PD_LIST_XLOCKED)
2463191673Sjamie				sx_xunlock(&allprison_lock);
2464191673Sjamie			return;
2465191673Sjamie		}
2466192895Sjamie		if (tpr != pr) {
2467192895Sjamie			mtx_unlock(&tpr->pr_mtx);
2468192895Sjamie			mtx_lock(&pr->pr_mtx);
2469192895Sjamie		}
2470191673Sjamie	}
2471124882Srwatson
2472192895Sjamie	for (;;) {
2473192895Sjamie		if (flags & PD_DEREF)
2474192895Sjamie			pr->pr_ref--;
2475192895Sjamie		/* If the prison still has references, nothing else to do. */
2476192895Sjamie		if (pr->pr_ref > 0) {
2477192895Sjamie			mtx_unlock(&pr->pr_mtx);
2478192895Sjamie			if (flags & PD_LIST_SLOCKED)
2479192895Sjamie				sx_sunlock(&allprison_lock);
2480192895Sjamie			else if (flags & PD_LIST_XLOCKED)
2481192895Sjamie				sx_xunlock(&allprison_lock);
2482192895Sjamie			return;
2483191673Sjamie		}
2484191673Sjamie
2485192895Sjamie		mtx_unlock(&pr->pr_mtx);
2486192895Sjamie		if (flags & PD_LIST_SLOCKED) {
2487192895Sjamie			if (!sx_try_upgrade(&allprison_lock)) {
2488192895Sjamie				sx_sunlock(&allprison_lock);
2489192895Sjamie				sx_xlock(&allprison_lock);
2490192895Sjamie			}
2491192895Sjamie		} else if (!(flags & PD_LIST_XLOCKED))
2492192895Sjamie			sx_xlock(&allprison_lock);
2493168489Spjd
2494192895Sjamie		TAILQ_REMOVE(&allprison, pr, pr_list);
2495192895Sjamie		LIST_REMOVE(pr, pr_sibling);
2496192895Sjamie		ppr = pr->pr_parent;
2497192895Sjamie		for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2498194762Sjamie			tpr->pr_childcount--;
2499196592Sjamie		sx_xunlock(&allprison_lock);
2500192895Sjamie
2501194251Sjamie#ifdef VIMAGE
2502196505Szec		if (pr->pr_vnet != ppr->pr_vnet)
2503194251Sjamie			vnet_destroy(pr->pr_vnet);
2504194251Sjamie#endif
2505192895Sjamie		if (pr->pr_root != NULL) {
2506192895Sjamie			vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2507192895Sjamie			vrele(pr->pr_root);
2508192895Sjamie			VFS_UNLOCK_GIANT(vfslocked);
2509192895Sjamie		}
2510192895Sjamie		mtx_destroy(&pr->pr_mtx);
2511191673Sjamie#ifdef INET
2512192895Sjamie		free(pr->pr_ip4, M_PRISON);
2513191673Sjamie#endif
2514185435Sbz#ifdef INET6
2515192895Sjamie		free(pr->pr_ip6, M_PRISON);
2516185435Sbz#endif
2517192895Sjamie		if (pr->pr_cpuset != NULL)
2518192895Sjamie			cpuset_rel(pr->pr_cpuset);
2519192895Sjamie		osd_jail_exit(pr);
2520192895Sjamie		free(pr, M_PRISON);
2521192895Sjamie
2522192895Sjamie		/* Removing a prison frees a reference on its parent. */
2523192895Sjamie		pr = ppr;
2524192895Sjamie		mtx_lock(&pr->pr_mtx);
2525196592Sjamie		flags = PD_DEREF;
2526192895Sjamie	}
2527124882Srwatson}
2528124882Srwatson
252972786Srwatsonvoid
2530185029Spjdprison_hold_locked(struct prison *pr)
253172786Srwatson{
253272786Srwatson
2533185029Spjd	mtx_assert(&pr->pr_mtx, MA_OWNED);
2534168489Spjd	KASSERT(pr->pr_ref > 0,
2535191671Sjamie	    ("Trying to hold dead prison (jid=%d).", pr->pr_id));
253672786Srwatson	pr->pr_ref++;
2537185029Spjd}
2538185029Spjd
2539185029Spjdvoid
2540185029Spjdprison_hold(struct prison *pr)
2541185029Spjd{
2542185029Spjd
2543185029Spjd	mtx_lock(&pr->pr_mtx);
2544185029Spjd	prison_hold_locked(pr);
254587275Srwatson	mtx_unlock(&pr->pr_mtx);
254672786Srwatson}
254772786Srwatson
2548185435Sbzvoid
2549185435Sbzprison_proc_hold(struct prison *pr)
255087275Srwatson{
255187275Srwatson
2552185435Sbz	mtx_lock(&pr->pr_mtx);
2553191673Sjamie	KASSERT(pr->pr_uref > 0,
2554191673Sjamie	    ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2555191673Sjamie	pr->pr_uref++;
2556185435Sbz	mtx_unlock(&pr->pr_mtx);
255787275Srwatson}
255887275Srwatson
2559185435Sbzvoid
2560185435Sbzprison_proc_free(struct prison *pr)
2561185435Sbz{
2562185435Sbz
2563185435Sbz	mtx_lock(&pr->pr_mtx);
2564191673Sjamie	KASSERT(pr->pr_uref > 0,
2565191673Sjamie	    ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2566191673Sjamie	prison_deref(pr, PD_DEUREF | PD_LOCKED);
2567185435Sbz}
2568185435Sbz
2569185435Sbz
2570185435Sbz#ifdef INET
2571185435Sbz/*
2572192895Sjamie * Restrict a prison's IP address list with its parent's, possibly replacing
2573192895Sjamie * it.  Return true if the replacement buffer was used (or would have been).
2574192895Sjamie */
2575192895Sjamiestatic int
2576192895Sjamieprison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2577192895Sjamie{
2578192895Sjamie	int ii, ij, used;
2579192895Sjamie	struct prison *ppr;
2580192895Sjamie
2581192895Sjamie	ppr = pr->pr_parent;
2582192895Sjamie	if (!(pr->pr_flags & PR_IP4_USER)) {
2583192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2584192895Sjamie		if (pr->pr_ip4s < ppr->pr_ip4s) {
2585192895Sjamie			/*
2586192895Sjamie			 * There's no room for the parent's list.  Use the
2587192895Sjamie			 * new list buffer, which is assumed to be big enough
2588192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2589192895Sjamie			 * allocate one.
2590192895Sjamie			 */
2591192895Sjamie			used = 1;
2592192895Sjamie			if (newip4 == NULL) {
2593192895Sjamie				newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2594192895Sjamie				    M_PRISON, M_NOWAIT);
2595192895Sjamie				if (newip4 != NULL)
2596192895Sjamie					used = 0;
2597192895Sjamie			}
2598192895Sjamie			if (newip4 != NULL) {
2599192895Sjamie				bcopy(ppr->pr_ip4, newip4,
2600192895Sjamie				    ppr->pr_ip4s * sizeof(*newip4));
2601192895Sjamie				free(pr->pr_ip4, M_PRISON);
2602192895Sjamie				pr->pr_ip4 = newip4;
2603192895Sjamie				pr->pr_ip4s = ppr->pr_ip4s;
2604192895Sjamie			}
2605192895Sjamie			return (used);
2606192895Sjamie		}
2607192895Sjamie		pr->pr_ip4s = ppr->pr_ip4s;
2608192895Sjamie		if (pr->pr_ip4s > 0)
2609192895Sjamie			bcopy(ppr->pr_ip4, pr->pr_ip4,
2610192895Sjamie			    pr->pr_ip4s * sizeof(*newip4));
2611192895Sjamie		else if (pr->pr_ip4 != NULL) {
2612192895Sjamie			free(pr->pr_ip4, M_PRISON);
2613192895Sjamie			pr->pr_ip4 = NULL;
2614192895Sjamie		}
2615195974Sjamie	} else if (pr->pr_ip4s > 0) {
2616192895Sjamie		/* Remove addresses that aren't in the parent. */
2617192895Sjamie		for (ij = 0; ij < ppr->pr_ip4s; ij++)
2618192895Sjamie			if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2619192895Sjamie				break;
2620192895Sjamie		if (ij < ppr->pr_ip4s)
2621192895Sjamie			ii = 1;
2622192895Sjamie		else {
2623192895Sjamie			bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2624192895Sjamie			    --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2625192895Sjamie			ii = 0;
2626192895Sjamie		}
2627192895Sjamie		for (ij = 1; ii < pr->pr_ip4s; ) {
2628192895Sjamie			if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2629192895Sjamie				ii++;
2630192895Sjamie				continue;
2631192895Sjamie			}
2632192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2633192895Sjamie				qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2634192895Sjamie			case -1:
2635192895Sjamie				bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2636192895Sjamie				    (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2637192895Sjamie				break;
2638192895Sjamie			case 0:
2639192895Sjamie				ii++;
2640192895Sjamie				ij++;
2641192895Sjamie				break;
2642192895Sjamie			case 1:
2643192895Sjamie				ij++;
2644192895Sjamie				break;
2645192895Sjamie			}
2646192895Sjamie		}
2647192895Sjamie		if (pr->pr_ip4s == 0) {
2648195870Sjamie			pr->pr_flags |= PR_IP4_DISABLE;
2649192895Sjamie			free(pr->pr_ip4, M_PRISON);
2650192895Sjamie			pr->pr_ip4 = NULL;
2651192895Sjamie		}
2652192895Sjamie	}
2653192895Sjamie	return (0);
2654192895Sjamie}
2655192895Sjamie
2656192895Sjamie/*
2657185435Sbz * Pass back primary IPv4 address of this jail.
2658185435Sbz *
2659192895Sjamie * If not restricted return success but do not alter the address.  Caller has
2660192895Sjamie * to make sure to initialize it correctly (e.g. INADDR_ANY).
2661185435Sbz *
2662188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2663188144Sjamie * Address returned in NBO.
2664185435Sbz */
266546155Sphkint
2666187684Sbzprison_get_ip4(struct ucred *cred, struct in_addr *ia)
266746155Sphk{
2668191673Sjamie	struct prison *pr;
266946155Sphk
2670185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2671185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2672185435Sbz
2673192895Sjamie	pr = cred->cr_prison;
2674192895Sjamie	if (!(pr->pr_flags & PR_IP4))
267546155Sphk		return (0);
2676191673Sjamie	mtx_lock(&pr->pr_mtx);
2677192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2678192895Sjamie		mtx_unlock(&pr->pr_mtx);
2679192895Sjamie		return (0);
2680192895Sjamie	}
2681191673Sjamie	if (pr->pr_ip4 == NULL) {
2682191673Sjamie		mtx_unlock(&pr->pr_mtx);
2683188144Sjamie		return (EAFNOSUPPORT);
2684191673Sjamie	}
2685185435Sbz
2686191673Sjamie	ia->s_addr = pr->pr_ip4[0].s_addr;
2687191673Sjamie	mtx_unlock(&pr->pr_mtx);
2688185435Sbz	return (0);
2689185435Sbz}
2690185435Sbz
2691185435Sbz/*
2692202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
2693202468Sbz * We will return 0 if we should bypass source address selection in favour
2694202468Sbz * of the primary jail IPv4 address. Only in this case *ia will be updated and
2695202468Sbz * returned in NBO.
2696202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2697202468Sbz */
2698202468Sbzint
2699202468Sbzprison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2700202468Sbz{
2701202468Sbz	struct prison *pr;
2702202468Sbz	struct in_addr lia;
2703202468Sbz	int error;
2704202468Sbz
2705202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2706202468Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2707202468Sbz
2708202468Sbz	if (!jailed(cred))
2709202468Sbz		return (1);
2710202468Sbz
2711202468Sbz	pr = cred->cr_prison;
2712202468Sbz	if (pr->pr_flags & PR_IP4_SADDRSEL)
2713202468Sbz		return (1);
2714202468Sbz
2715202468Sbz	lia.s_addr = INADDR_ANY;
2716202468Sbz	error = prison_get_ip4(cred, &lia);
2717202468Sbz	if (error)
2718202468Sbz		return (error);
2719202468Sbz	if (lia.s_addr == INADDR_ANY)
2720202468Sbz		return (1);
2721202468Sbz
2722202468Sbz	ia->s_addr = lia.s_addr;
2723202468Sbz	return (0);
2724202468Sbz}
2725202468Sbz
2726202468Sbz/*
2727192895Sjamie * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2728192895Sjamie */
2729192895Sjamieint
2730192895Sjamieprison_equal_ip4(struct prison *pr1, struct prison *pr2)
2731192895Sjamie{
2732192895Sjamie
2733192895Sjamie	if (pr1 == pr2)
2734192895Sjamie		return (1);
2735192895Sjamie
2736192895Sjamie	/*
2737195974Sjamie	 * No need to lock since the PR_IP4_USER flag can't be altered for
2738195974Sjamie	 * existing prisons.
2739192895Sjamie	 */
2740195945Sjamie	while (pr1 != &prison0 &&
2741195945Sjamie#ifdef VIMAGE
2742195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
2743195945Sjamie#endif
2744195945Sjamie	       !(pr1->pr_flags & PR_IP4_USER))
2745192895Sjamie		pr1 = pr1->pr_parent;
2746195945Sjamie	while (pr2 != &prison0 &&
2747195945Sjamie#ifdef VIMAGE
2748195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
2749195945Sjamie#endif
2750195945Sjamie	       !(pr2->pr_flags & PR_IP4_USER))
2751192895Sjamie		pr2 = pr2->pr_parent;
2752192895Sjamie	return (pr1 == pr2);
2753192895Sjamie}
2754192895Sjamie
2755192895Sjamie/*
2756185435Sbz * Make sure our (source) address is set to something meaningful to this
2757185435Sbz * jail.
2758185435Sbz *
2759192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2760192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2761192895Sjamie * doesn't allow IPv4.  Address passed in in NBO and returned in NBO.
2762185435Sbz */
2763185435Sbzint
2764185435Sbzprison_local_ip4(struct ucred *cred, struct in_addr *ia)
2765185435Sbz{
2766191673Sjamie	struct prison *pr;
2767185435Sbz	struct in_addr ia0;
2768191673Sjamie	int error;
2769185435Sbz
2770185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2771185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2772185435Sbz
2773192895Sjamie	pr = cred->cr_prison;
2774192895Sjamie	if (!(pr->pr_flags & PR_IP4))
277546155Sphk		return (0);
2776191673Sjamie	mtx_lock(&pr->pr_mtx);
2777192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2778192895Sjamie		mtx_unlock(&pr->pr_mtx);
2779192895Sjamie		return (0);
2780192895Sjamie	}
2781191673Sjamie	if (pr->pr_ip4 == NULL) {
2782191673Sjamie		mtx_unlock(&pr->pr_mtx);
2783188144Sjamie		return (EAFNOSUPPORT);
2784191673Sjamie	}
2785185435Sbz
2786185435Sbz	ia0.s_addr = ntohl(ia->s_addr);
2787185435Sbz	if (ia0.s_addr == INADDR_LOOPBACK) {
2788191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2789191673Sjamie		mtx_unlock(&pr->pr_mtx);
2790185435Sbz		return (0);
279146155Sphk	}
2792185435Sbz
2793188144Sjamie	if (ia0.s_addr == INADDR_ANY) {
2794188144Sjamie		/*
2795188144Sjamie		 * In case there is only 1 IPv4 address, bind directly.
2796188144Sjamie		 */
2797191673Sjamie		if (pr->pr_ip4s == 1)
2798191673Sjamie			ia->s_addr = pr->pr_ip4[0].s_addr;
2799191673Sjamie		mtx_unlock(&pr->pr_mtx);
2800185435Sbz		return (0);
2801185435Sbz	}
2802185435Sbz
2803191673Sjamie	error = _prison_check_ip4(pr, ia);
2804191673Sjamie	mtx_unlock(&pr->pr_mtx);
2805191673Sjamie	return (error);
2806185435Sbz}
2807185435Sbz
2808185435Sbz/*
2809185435Sbz * Rewrite destination address in case we will connect to loopback address.
2810185435Sbz *
2811188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2812188144Sjamie * Address passed in in NBO and returned in NBO.
2813185435Sbz */
2814185435Sbzint
2815185435Sbzprison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2816185435Sbz{
2817191673Sjamie	struct prison *pr;
2818185435Sbz
2819185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2820185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2821185435Sbz
2822192895Sjamie	pr = cred->cr_prison;
2823192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2824185435Sbz		return (0);
2825191673Sjamie	mtx_lock(&pr->pr_mtx);
2826192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2827192895Sjamie		mtx_unlock(&pr->pr_mtx);
2828192895Sjamie		return (0);
2829192895Sjamie	}
2830191673Sjamie	if (pr->pr_ip4 == NULL) {
2831191673Sjamie		mtx_unlock(&pr->pr_mtx);
2832188144Sjamie		return (EAFNOSUPPORT);
2833191673Sjamie	}
2834188144Sjamie
2835185435Sbz	if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2836191673Sjamie		ia->s_addr = pr->pr_ip4[0].s_addr;
2837191673Sjamie		mtx_unlock(&pr->pr_mtx);
2838185435Sbz		return (0);
2839185435Sbz	}
2840185435Sbz
2841185435Sbz	/*
2842185435Sbz	 * Return success because nothing had to be changed.
2843185435Sbz	 */
2844191673Sjamie	mtx_unlock(&pr->pr_mtx);
2845185435Sbz	return (0);
2846185435Sbz}
2847185435Sbz
2848185435Sbz/*
2849188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
2850185435Sbz *
2851192895Sjamie * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2852192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2853192895Sjamie * doesn't allow IPv4.  Address passed in in NBO.
2854185435Sbz */
2855185435Sbzstatic int
2856185435Sbz_prison_check_ip4(struct prison *pr, struct in_addr *ia)
2857185435Sbz{
2858185435Sbz	int i, a, z, d;
2859185435Sbz
2860185435Sbz	/*
2861185435Sbz	 * Check the primary IP.
2862185435Sbz	 */
2863185435Sbz	if (pr->pr_ip4[0].s_addr == ia->s_addr)
2864188144Sjamie		return (0);
2865185435Sbz
2866185435Sbz	/*
2867185435Sbz	 * All the other IPs are sorted so we can do a binary search.
2868185435Sbz	 */
2869185435Sbz	a = 0;
2870185435Sbz	z = pr->pr_ip4s - 2;
2871185435Sbz	while (a <= z) {
2872185435Sbz		i = (a + z) / 2;
2873185435Sbz		d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2874185435Sbz		if (d > 0)
2875185435Sbz			z = i - 1;
2876185435Sbz		else if (d < 0)
2877185435Sbz			a = i + 1;
287881114Srwatson		else
2879188144Sjamie			return (0);
2880185435Sbz	}
2881188144Sjamie
2882188144Sjamie	return (EADDRNOTAVAIL);
2883185435Sbz}
2884185435Sbz
2885185435Sbzint
2886185435Sbzprison_check_ip4(struct ucred *cred, struct in_addr *ia)
2887185435Sbz{
2888191673Sjamie	struct prison *pr;
2889191673Sjamie	int error;
2890185435Sbz
2891185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2892185435Sbz	KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2893185435Sbz
2894192895Sjamie	pr = cred->cr_prison;
2895192895Sjamie	if (!(pr->pr_flags & PR_IP4))
2896188144Sjamie		return (0);
2897191673Sjamie	mtx_lock(&pr->pr_mtx);
2898192895Sjamie	if (!(pr->pr_flags & PR_IP4)) {
2899192895Sjamie		mtx_unlock(&pr->pr_mtx);
2900192895Sjamie		return (0);
2901192895Sjamie	}
2902191673Sjamie	if (pr->pr_ip4 == NULL) {
2903191673Sjamie		mtx_unlock(&pr->pr_mtx);
2904188144Sjamie		return (EAFNOSUPPORT);
2905191673Sjamie	}
2906185435Sbz
2907191673Sjamie	error = _prison_check_ip4(pr, ia);
2908191673Sjamie	mtx_unlock(&pr->pr_mtx);
2909191673Sjamie	return (error);
2910185435Sbz}
2911185435Sbz#endif
2912185435Sbz
2913185435Sbz#ifdef INET6
2914192895Sjamiestatic int
2915192895Sjamieprison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2916192895Sjamie{
2917192895Sjamie	int ii, ij, used;
2918192895Sjamie	struct prison *ppr;
2919192895Sjamie
2920192895Sjamie	ppr = pr->pr_parent;
2921192895Sjamie	if (!(pr->pr_flags & PR_IP6_USER)) {
2922192895Sjamie		/* This has no user settings, so just copy the parent's list. */
2923192895Sjamie		if (pr->pr_ip6s < ppr->pr_ip6s) {
2924192895Sjamie			/*
2925192895Sjamie			 * There's no room for the parent's list.  Use the
2926192895Sjamie			 * new list buffer, which is assumed to be big enough
2927192895Sjamie			 * (if it was passed).  If there's no buffer, try to
2928192895Sjamie			 * allocate one.
2929192895Sjamie			 */
2930192895Sjamie			used = 1;
2931192895Sjamie			if (newip6 == NULL) {
2932192895Sjamie				newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2933192895Sjamie				    M_PRISON, M_NOWAIT);
2934192895Sjamie				if (newip6 != NULL)
2935192895Sjamie					used = 0;
2936192895Sjamie			}
2937192895Sjamie			if (newip6 != NULL) {
2938192895Sjamie				bcopy(ppr->pr_ip6, newip6,
2939192895Sjamie				    ppr->pr_ip6s * sizeof(*newip6));
2940192895Sjamie				free(pr->pr_ip6, M_PRISON);
2941192895Sjamie				pr->pr_ip6 = newip6;
2942192895Sjamie				pr->pr_ip6s = ppr->pr_ip6s;
2943192895Sjamie			}
2944192895Sjamie			return (used);
2945192895Sjamie		}
2946192895Sjamie		pr->pr_ip6s = ppr->pr_ip6s;
2947192895Sjamie		if (pr->pr_ip6s > 0)
2948192895Sjamie			bcopy(ppr->pr_ip6, pr->pr_ip6,
2949192895Sjamie			    pr->pr_ip6s * sizeof(*newip6));
2950192895Sjamie		else if (pr->pr_ip6 != NULL) {
2951192895Sjamie			free(pr->pr_ip6, M_PRISON);
2952192895Sjamie			pr->pr_ip6 = NULL;
2953192895Sjamie		}
2954195974Sjamie	} else if (pr->pr_ip6s > 0) {
2955192895Sjamie		/* Remove addresses that aren't in the parent. */
2956192895Sjamie		for (ij = 0; ij < ppr->pr_ip6s; ij++)
2957192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2958192895Sjamie			    &ppr->pr_ip6[ij]))
2959192895Sjamie				break;
2960192895Sjamie		if (ij < ppr->pr_ip6s)
2961192895Sjamie			ii = 1;
2962192895Sjamie		else {
2963192895Sjamie			bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2964192895Sjamie			    --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2965192895Sjamie			ii = 0;
2966192895Sjamie		}
2967192895Sjamie		for (ij = 1; ii < pr->pr_ip6s; ) {
2968192895Sjamie			if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2969192895Sjamie			    &ppr->pr_ip6[0])) {
2970192895Sjamie				ii++;
2971192895Sjamie				continue;
2972192895Sjamie			}
2973192895Sjamie			switch (ij >= ppr->pr_ip4s ? -1 :
2974192895Sjamie				qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2975192895Sjamie			case -1:
2976192895Sjamie				bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2977192895Sjamie				    (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2978192895Sjamie				break;
2979192895Sjamie			case 0:
2980192895Sjamie				ii++;
2981192895Sjamie				ij++;
2982192895Sjamie				break;
2983192895Sjamie			case 1:
2984192895Sjamie				ij++;
2985192895Sjamie				break;
2986192895Sjamie			}
2987192895Sjamie		}
2988192895Sjamie		if (pr->pr_ip6s == 0) {
2989195870Sjamie			pr->pr_flags |= PR_IP6_DISABLE;
2990192895Sjamie			free(pr->pr_ip6, M_PRISON);
2991192895Sjamie			pr->pr_ip6 = NULL;
2992192895Sjamie		}
2993192895Sjamie	}
2994192895Sjamie	return 0;
2995192895Sjamie}
2996192895Sjamie
2997185435Sbz/*
2998185435Sbz * Pass back primary IPv6 address for this jail.
2999185435Sbz *
3000192895Sjamie * If not restricted return success but do not alter the address.  Caller has
3001192895Sjamie * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3002185435Sbz *
3003188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3004185435Sbz */
3005185435Sbzint
3006187684Sbzprison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3007185435Sbz{
3008191673Sjamie	struct prison *pr;
3009185435Sbz
3010185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3011185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3012185435Sbz
3013192895Sjamie	pr = cred->cr_prison;
3014192895Sjamie	if (!(pr->pr_flags & PR_IP6))
301581114Srwatson		return (0);
3016191673Sjamie	mtx_lock(&pr->pr_mtx);
3017192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3018192895Sjamie		mtx_unlock(&pr->pr_mtx);
3019192895Sjamie		return (0);
3020192895Sjamie	}
3021191673Sjamie	if (pr->pr_ip6 == NULL) {
3022191673Sjamie		mtx_unlock(&pr->pr_mtx);
3023188144Sjamie		return (EAFNOSUPPORT);
3024191673Sjamie	}
3025188144Sjamie
3026191673Sjamie	bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3027191673Sjamie	mtx_unlock(&pr->pr_mtx);
3028185435Sbz	return (0);
3029185435Sbz}
3030185435Sbz
3031185435Sbz/*
3032202468Sbz * Return 1 if we should do proper source address selection or are not jailed.
3033202468Sbz * We will return 0 if we should bypass source address selection in favour
3034202468Sbz * of the primary jail IPv6 address. Only in this case *ia will be updated and
3035202468Sbz * returned in NBO.
3036202468Sbz * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3037202468Sbz */
3038202468Sbzint
3039202468Sbzprison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3040202468Sbz{
3041202468Sbz	struct prison *pr;
3042202468Sbz	struct in6_addr lia6;
3043202468Sbz	int error;
3044202468Sbz
3045202468Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3046202468Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3047202468Sbz
3048202468Sbz	if (!jailed(cred))
3049202468Sbz		return (1);
3050202468Sbz
3051202468Sbz	pr = cred->cr_prison;
3052202468Sbz	if (pr->pr_flags & PR_IP6_SADDRSEL)
3053202468Sbz		return (1);
3054202468Sbz
3055202468Sbz	lia6 = in6addr_any;
3056202468Sbz	error = prison_get_ip6(cred, &lia6);
3057202468Sbz	if (error)
3058202468Sbz		return (error);
3059202468Sbz	if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3060202468Sbz		return (1);
3061202468Sbz
3062202468Sbz	bcopy(&lia6, ia6, sizeof(struct in6_addr));
3063202468Sbz	return (0);
3064202468Sbz}
3065202468Sbz
3066202468Sbz/*
3067192895Sjamie * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3068192895Sjamie */
3069192895Sjamieint
3070192895Sjamieprison_equal_ip6(struct prison *pr1, struct prison *pr2)
3071192895Sjamie{
3072192895Sjamie
3073192895Sjamie	if (pr1 == pr2)
3074192895Sjamie		return (1);
3075192895Sjamie
3076195945Sjamie	while (pr1 != &prison0 &&
3077195945Sjamie#ifdef VIMAGE
3078195945Sjamie	       !(pr1->pr_flags & PR_VNET) &&
3079195945Sjamie#endif
3080195945Sjamie	       !(pr1->pr_flags & PR_IP6_USER))
3081192895Sjamie		pr1 = pr1->pr_parent;
3082195945Sjamie	while (pr2 != &prison0 &&
3083195945Sjamie#ifdef VIMAGE
3084195945Sjamie	       !(pr2->pr_flags & PR_VNET) &&
3085195945Sjamie#endif
3086195945Sjamie	       !(pr2->pr_flags & PR_IP6_USER))
3087192895Sjamie		pr2 = pr2->pr_parent;
3088192895Sjamie	return (pr1 == pr2);
3089192895Sjamie}
3090192895Sjamie
3091192895Sjamie/*
3092185435Sbz * Make sure our (source) address is set to something meaningful to this jail.
3093185435Sbz *
3094185435Sbz * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3095185435Sbz * when needed while binding.
3096185435Sbz *
3097192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3098192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3099192895Sjamie * doesn't allow IPv6.
3100185435Sbz */
3101185435Sbzint
3102185435Sbzprison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3103185435Sbz{
3104191673Sjamie	struct prison *pr;
3105191673Sjamie	int error;
3106185435Sbz
3107185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3108185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3109185435Sbz
3110192895Sjamie	pr = cred->cr_prison;
3111192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3112185435Sbz		return (0);
3113191673Sjamie	mtx_lock(&pr->pr_mtx);
3114192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3115192895Sjamie		mtx_unlock(&pr->pr_mtx);
3116192895Sjamie		return (0);
3117192895Sjamie	}
3118191673Sjamie	if (pr->pr_ip6 == NULL) {
3119191673Sjamie		mtx_unlock(&pr->pr_mtx);
3120188144Sjamie		return (EAFNOSUPPORT);
3121191673Sjamie	}
3122188144Sjamie
3123185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3124191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3125191673Sjamie		mtx_unlock(&pr->pr_mtx);
3126185435Sbz		return (0);
312781114Srwatson	}
3128185435Sbz
3129188144Sjamie	if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3130188144Sjamie		/*
3131188144Sjamie		 * In case there is only 1 IPv6 address, and v6only is true,
3132188144Sjamie		 * then bind directly.
3133188144Sjamie		 */
3134191673Sjamie		if (v6only != 0 && pr->pr_ip6s == 1)
3135191673Sjamie			bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3136191673Sjamie		mtx_unlock(&pr->pr_mtx);
3137185435Sbz		return (0);
3138185435Sbz	}
3139188144Sjamie
3140191673Sjamie	error = _prison_check_ip6(pr, ia6);
3141191673Sjamie	mtx_unlock(&pr->pr_mtx);
3142191673Sjamie	return (error);
3143185435Sbz}
3144185435Sbz
3145185435Sbz/*
3146185435Sbz * Rewrite destination address in case we will connect to loopback address.
3147185435Sbz *
3148188144Sjamie * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3149185435Sbz */
3150185435Sbzint
3151185435Sbzprison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3152185435Sbz{
3153191673Sjamie	struct prison *pr;
3154185435Sbz
3155185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3156185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3157185435Sbz
3158192895Sjamie	pr = cred->cr_prison;
3159192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3160185435Sbz		return (0);
3161191673Sjamie	mtx_lock(&pr->pr_mtx);
3162192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3163192895Sjamie		mtx_unlock(&pr->pr_mtx);
3164192895Sjamie		return (0);
3165192895Sjamie	}
3166191673Sjamie	if (pr->pr_ip6 == NULL) {
3167191673Sjamie		mtx_unlock(&pr->pr_mtx);
3168188144Sjamie		return (EAFNOSUPPORT);
3169191673Sjamie	}
3170188144Sjamie
3171185435Sbz	if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3172191673Sjamie		bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3173191673Sjamie		mtx_unlock(&pr->pr_mtx);
3174185435Sbz		return (0);
3175185435Sbz	}
3176185435Sbz
3177185435Sbz	/*
3178185435Sbz	 * Return success because nothing had to be changed.
3179185435Sbz	 */
3180191673Sjamie	mtx_unlock(&pr->pr_mtx);
318146155Sphk	return (0);
318246155Sphk}
318346155Sphk
3184185435Sbz/*
3185188144Sjamie * Check if given address belongs to the jail referenced by cred/prison.
3186185435Sbz *
3187192895Sjamie * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3188192895Sjamie * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3189192895Sjamie * doesn't allow IPv6.
3190185435Sbz */
3191185435Sbzstatic int
3192185435Sbz_prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
319346155Sphk{
3194185435Sbz	int i, a, z, d;
319546155Sphk
3196185435Sbz	/*
3197185435Sbz	 * Check the primary IP.
3198185435Sbz	 */
3199185435Sbz	if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3200188144Sjamie		return (0);
3201185435Sbz
3202185435Sbz	/*
3203185435Sbz	 * All the other IPs are sorted so we can do a binary search.
3204185435Sbz	 */
3205185435Sbz	a = 0;
3206185435Sbz	z = pr->pr_ip6s - 2;
3207185435Sbz	while (a <= z) {
3208185435Sbz		i = (a + z) / 2;
3209185435Sbz		d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3210185435Sbz		if (d > 0)
3211185435Sbz			z = i - 1;
3212185435Sbz		else if (d < 0)
3213185435Sbz			a = i + 1;
321446155Sphk		else
3215188144Sjamie			return (0);
321646155Sphk	}
3217188144Sjamie
3218188144Sjamie	return (EADDRNOTAVAIL);
321946155Sphk}
322046155Sphk
322146155Sphkint
3222185435Sbzprison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3223185435Sbz{
3224191673Sjamie	struct prison *pr;
3225191673Sjamie	int error;
3226185435Sbz
3227185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3228185435Sbz	KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3229185435Sbz
3230192895Sjamie	pr = cred->cr_prison;
3231192895Sjamie	if (!(pr->pr_flags & PR_IP6))
3232188144Sjamie		return (0);
3233191673Sjamie	mtx_lock(&pr->pr_mtx);
3234192895Sjamie	if (!(pr->pr_flags & PR_IP6)) {
3235192895Sjamie		mtx_unlock(&pr->pr_mtx);
3236192895Sjamie		return (0);
3237192895Sjamie	}
3238191673Sjamie	if (pr->pr_ip6 == NULL) {
3239191673Sjamie		mtx_unlock(&pr->pr_mtx);
3240188144Sjamie		return (EAFNOSUPPORT);
3241191673Sjamie	}
3242185435Sbz
3243191673Sjamie	error = _prison_check_ip6(pr, ia6);
3244191673Sjamie	mtx_unlock(&pr->pr_mtx);
3245191673Sjamie	return (error);
3246185435Sbz}
3247185435Sbz#endif
3248185435Sbz
3249185435Sbz/*
3250188146Sjamie * Check if a jail supports the given address family.
3251188146Sjamie *
3252188146Sjamie * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3253188146Sjamie * if not.
3254188146Sjamie */
3255188146Sjamieint
3256188146Sjamieprison_check_af(struct ucred *cred, int af)
3257188146Sjamie{
3258192895Sjamie	struct prison *pr;
3259188146Sjamie	int error;
3260188146Sjamie
3261188146Sjamie	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3262188146Sjamie
3263192895Sjamie	pr = cred->cr_prison;
3264194923Sjamie#ifdef VIMAGE
3265194915Sjamie	/* Prisons with their own network stack are not limited. */
3266200473Sbz	if (prison_owns_vnet(cred))
3267194915Sjamie		return (0);
3268194923Sjamie#endif
3269194915Sjamie
3270188146Sjamie	error = 0;
3271188146Sjamie	switch (af)
3272188146Sjamie	{
3273188146Sjamie#ifdef INET
3274188146Sjamie	case AF_INET:
3275192895Sjamie		if (pr->pr_flags & PR_IP4)
3276192895Sjamie		{
3277192895Sjamie			mtx_lock(&pr->pr_mtx);
3278192895Sjamie			if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3279192895Sjamie				error = EAFNOSUPPORT;
3280192895Sjamie			mtx_unlock(&pr->pr_mtx);
3281192895Sjamie		}
3282188146Sjamie		break;
3283188146Sjamie#endif
3284188146Sjamie#ifdef INET6
3285188146Sjamie	case AF_INET6:
3286192895Sjamie		if (pr->pr_flags & PR_IP6)
3287192895Sjamie		{
3288192895Sjamie			mtx_lock(&pr->pr_mtx);
3289192895Sjamie			if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3290192895Sjamie				error = EAFNOSUPPORT;
3291192895Sjamie			mtx_unlock(&pr->pr_mtx);
3292192895Sjamie		}
3293188146Sjamie		break;
3294188146Sjamie#endif
3295188146Sjamie	case AF_LOCAL:
3296188146Sjamie	case AF_ROUTE:
3297188146Sjamie		break;
3298188146Sjamie	default:
3299192895Sjamie		if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3300188146Sjamie			error = EAFNOSUPPORT;
3301188146Sjamie	}
3302188146Sjamie	return (error);
3303188146Sjamie}
3304188146Sjamie
3305188146Sjamie/*
3306185435Sbz * Check if given address belongs to the jail referenced by cred (wrapper to
3307185435Sbz * prison_check_ip[46]).
3308185435Sbz *
3309192895Sjamie * Returns 0 if jail doesn't restrict the address family or if address belongs
3310192895Sjamie * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3311192895Sjamie * the jail doesn't allow the address family.  IPv4 Address passed in in NBO.
3312185435Sbz */
3313185435Sbzint
331472786Srwatsonprison_if(struct ucred *cred, struct sockaddr *sa)
331546155Sphk{
3316185435Sbz#ifdef INET
3317114168Smike	struct sockaddr_in *sai;
3318185435Sbz#endif
3319185435Sbz#ifdef INET6
3320185435Sbz	struct sockaddr_in6 *sai6;
3321185435Sbz#endif
3322188144Sjamie	int error;
332346155Sphk
3324185435Sbz	KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3325185435Sbz	KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3326185435Sbz
3327200473Sbz#ifdef VIMAGE
3328200473Sbz	if (prison_owns_vnet(cred))
3329200473Sbz		return (0);
3330200473Sbz#endif
3331200473Sbz
3332188144Sjamie	error = 0;
3333188144Sjamie	switch (sa->sa_family)
3334185435Sbz	{
3335185435Sbz#ifdef INET
3336185435Sbz	case AF_INET:
3337185435Sbz		sai = (struct sockaddr_in *)sa;
3338188144Sjamie		error = prison_check_ip4(cred, &sai->sin_addr);
3339185435Sbz		break;
3340185435Sbz#endif
3341185435Sbz#ifdef INET6
3342185435Sbz	case AF_INET6:
3343185435Sbz		sai6 = (struct sockaddr_in6 *)sa;
3344188144Sjamie		error = prison_check_ip6(cred, &sai6->sin6_addr);
3345185435Sbz		break;
3346185435Sbz#endif
3347185435Sbz	default:
3348192895Sjamie		if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3349188144Sjamie			error = EAFNOSUPPORT;
3350185435Sbz	}
3351188144Sjamie	return (error);
335246155Sphk}
335372786Srwatson
335472786Srwatson/*
335572786Srwatson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
335672786Srwatson */
335772786Srwatsonint
3358114168Smikeprison_check(struct ucred *cred1, struct ucred *cred2)
335972786Srwatson{
336072786Srwatson
3361192895Sjamie	return ((cred1->cr_prison == cred2->cr_prison ||
3362192895Sjamie	    prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3363192895Sjamie}
336472786Srwatson
3365192895Sjamie/*
3366192895Sjamie * Return 1 if p2 is a child of p1, otherwise 0.
3367192895Sjamie */
3368192895Sjamieint
3369192895Sjamieprison_ischild(struct prison *pr1, struct prison *pr2)
3370192895Sjamie{
3371192895Sjamie
3372192895Sjamie	for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3373192895Sjamie		if (pr1 == pr2)
3374192895Sjamie			return (1);
337572786Srwatson	return (0);
337672786Srwatson}
337772786Srwatson
337872786Srwatson/*
337972786Srwatson * Return 1 if the passed credential is in a jail, otherwise 0.
338072786Srwatson */
338172786Srwatsonint
3382114168Smikejailed(struct ucred *cred)
338372786Srwatson{
338472786Srwatson
3385192895Sjamie	return (cred->cr_prison != &prison0);
338672786Srwatson}
338791384Srobert
338891384Srobert/*
3389200473Sbz * Return 1 if the passed credential is in a jail and that jail does not
3390200473Sbz * have its own virtual network stack, otherwise 0.
3391200473Sbz */
3392200473Sbzint
3393200473Sbzjailed_without_vnet(struct ucred *cred)
3394200473Sbz{
3395200473Sbz
3396200473Sbz	if (!jailed(cred))
3397200473Sbz		return (0);
3398200473Sbz#ifdef VIMAGE
3399200473Sbz	if (prison_owns_vnet(cred))
3400200473Sbz		return (0);
3401200473Sbz#endif
3402200473Sbz
3403200473Sbz	return (1);
3404200473Sbz}
3405200473Sbz
3406200473Sbz/*
3407194090Sjamie * Return the correct hostname (domainname, et al) for the passed credential.
340891384Srobert */
340991391Srobertvoid
3410114168Smikegetcredhostname(struct ucred *cred, char *buf, size_t size)
341191384Srobert{
3412193066Sjamie	struct prison *pr;
341391384Srobert
3414194090Sjamie	/*
3415194090Sjamie	 * A NULL credential can be used to shortcut to the physical
3416194090Sjamie	 * system's hostname.
3417194090Sjamie	 */
3418193066Sjamie	pr = (cred != NULL) ? cred->cr_prison : &prison0;
3419193066Sjamie	mtx_lock(&pr->pr_mtx);
3420194118Sjamie	strlcpy(buf, pr->pr_hostname, size);
3421193066Sjamie	mtx_unlock(&pr->pr_mtx);
342291384Srobert}
3423113275Smike
3424194090Sjamievoid
3425194090Sjamiegetcreddomainname(struct ucred *cred, char *buf, size_t size)
3426194090Sjamie{
3427194090Sjamie
3428194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3429194118Sjamie	strlcpy(buf, cred->cr_prison->pr_domainname, size);
3430194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3431194090Sjamie}
3432194090Sjamie
3433194090Sjamievoid
3434194090Sjamiegetcredhostuuid(struct ucred *cred, char *buf, size_t size)
3435194090Sjamie{
3436194090Sjamie
3437194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3438194118Sjamie	strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3439194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3440194090Sjamie}
3441194090Sjamie
3442194090Sjamievoid
3443194090Sjamiegetcredhostid(struct ucred *cred, unsigned long *hostid)
3444194090Sjamie{
3445194090Sjamie
3446194090Sjamie	mtx_lock(&cred->cr_prison->pr_mtx);
3447194090Sjamie	*hostid = cred->cr_prison->pr_hostid;
3448194090Sjamie	mtx_unlock(&cred->cr_prison->pr_mtx);
3449194090Sjamie}
3450194090Sjamie
3451196176Sbz#ifdef VIMAGE
3452125804Srwatson/*
3453196176Sbz * Determine whether the prison represented by cred owns
3454196176Sbz * its vnet rather than having it inherited.
3455196176Sbz *
3456196176Sbz * Returns 1 in case the prison owns the vnet, 0 otherwise.
3457196176Sbz */
3458196176Sbzint
3459196176Sbzprison_owns_vnet(struct ucred *cred)
3460196176Sbz{
3461196176Sbz
3462196176Sbz	/*
3463196176Sbz	 * vnets cannot be added/removed after jail creation,
3464196176Sbz	 * so no need to lock here.
3465196176Sbz	 */
3466196176Sbz	return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3467196176Sbz}
3468196176Sbz#endif
3469196176Sbz
3470196176Sbz/*
3471147185Spjd * Determine whether the subject represented by cred can "see"
3472147185Spjd * status of a mount point.
3473147185Spjd * Returns: 0 for permitted, ENOENT otherwise.
3474147185Spjd * XXX: This function should be called cr_canseemount() and should be
3475147185Spjd *      placed in kern_prot.c.
3476125804Srwatson */
3477125804Srwatsonint
3478147185Spjdprison_canseemount(struct ucred *cred, struct mount *mp)
3479125804Srwatson{
3480147185Spjd	struct prison *pr;
3481147185Spjd	struct statfs *sp;
3482147185Spjd	size_t len;
3483125804Srwatson
3484192895Sjamie	pr = cred->cr_prison;
3485192895Sjamie	if (pr->pr_enforce_statfs == 0)
3486147185Spjd		return (0);
3487147185Spjd	if (pr->pr_root->v_mount == mp)
3488147185Spjd		return (0);
3489192895Sjamie	if (pr->pr_enforce_statfs == 2)
3490147185Spjd		return (ENOENT);
3491147185Spjd	/*
3492147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3493147185Spjd	 * all mount-points from inside a jail.
3494147185Spjd	 * This is ugly check, but this is the only situation when jail's
3495147185Spjd	 * directory ends with '/'.
3496147185Spjd	 */
3497147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3498147185Spjd		return (0);
3499147185Spjd	len = strlen(pr->pr_path);
3500147185Spjd	sp = &mp->mnt_stat;
3501147185Spjd	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3502147185Spjd		return (ENOENT);
3503147185Spjd	/*
3504147185Spjd	 * Be sure that we don't have situation where jail's root directory
3505147185Spjd	 * is "/some/path" and mount point is "/some/pathpath".
3506147185Spjd	 */
3507147185Spjd	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3508147185Spjd		return (ENOENT);
3509147185Spjd	return (0);
3510147185Spjd}
3511147185Spjd
3512147185Spjdvoid
3513147185Spjdprison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3514147185Spjd{
3515147185Spjd	char jpath[MAXPATHLEN];
3516147185Spjd	struct prison *pr;
3517147185Spjd	size_t len;
3518147185Spjd
3519192895Sjamie	pr = cred->cr_prison;
3520192895Sjamie	if (pr->pr_enforce_statfs == 0)
3521147185Spjd		return;
3522147185Spjd	if (prison_canseemount(cred, mp) != 0) {
3523147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3524147185Spjd		strlcpy(sp->f_mntonname, "[restricted]",
3525147185Spjd		    sizeof(sp->f_mntonname));
3526147185Spjd		return;
3527125804Srwatson	}
3528147185Spjd	if (pr->pr_root->v_mount == mp) {
3529147185Spjd		/*
3530147185Spjd		 * Clear current buffer data, so we are sure nothing from
3531147185Spjd		 * the valid path left there.
3532147185Spjd		 */
3533147185Spjd		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3534147185Spjd		*sp->f_mntonname = '/';
3535147185Spjd		return;
3536147185Spjd	}
3537147185Spjd	/*
3538147185Spjd	 * If jail's chroot directory is set to "/" we should be able to see
3539147185Spjd	 * all mount-points from inside a jail.
3540147185Spjd	 */
3541147185Spjd	if (strcmp(pr->pr_path, "/") == 0)
3542147185Spjd		return;
3543147185Spjd	len = strlen(pr->pr_path);
3544147185Spjd	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3545147185Spjd	/*
3546147185Spjd	 * Clear current buffer data, so we are sure nothing from
3547147185Spjd	 * the valid path left there.
3548147185Spjd	 */
3549147185Spjd	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3550147185Spjd	if (*jpath == '\0') {
3551147185Spjd		/* Should never happen. */
3552147185Spjd		*sp->f_mntonname = '/';
3553147185Spjd	} else {
3554147185Spjd		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3555147185Spjd	}
3556125804Srwatson}
3557125804Srwatson
3558164032Srwatson/*
3559164032Srwatson * Check with permission for a specific privilege is granted within jail.  We
3560164032Srwatson * have a specific list of accepted privileges; the rest are denied.
3561164032Srwatson */
3562164032Srwatsonint
3563164032Srwatsonprison_priv_check(struct ucred *cred, int priv)
3564164032Srwatson{
3565164032Srwatson
3566164032Srwatson	if (!jailed(cred))
3567164032Srwatson		return (0);
3568164032Srwatson
3569194915Sjamie#ifdef VIMAGE
3570194915Sjamie	/*
3571194915Sjamie	 * Privileges specific to prisons with a virtual network stack.
3572194915Sjamie	 * There might be a duplicate entry here in case the privilege
3573194915Sjamie	 * is only granted conditionally in the legacy jail case.
3574194915Sjamie	 */
3575164032Srwatson	switch (priv) {
3576194915Sjamie#ifdef notyet
3577194915Sjamie		/*
3578194915Sjamie		 * NFS-specific privileges.
3579194915Sjamie		 */
3580194915Sjamie	case PRIV_NFS_DAEMON:
3581194915Sjamie	case PRIV_NFS_LOCKD:
3582194915Sjamie#endif
3583194915Sjamie		/*
3584194915Sjamie		 * Network stack privileges.
3585194915Sjamie		 */
3586194915Sjamie	case PRIV_NET_BRIDGE:
3587194915Sjamie	case PRIV_NET_GRE:
3588194915Sjamie	case PRIV_NET_BPF:
3589194915Sjamie	case PRIV_NET_RAW:		/* Dup, cond. in legacy jail case. */
3590194915Sjamie	case PRIV_NET_ROUTE:
3591194915Sjamie	case PRIV_NET_TAP:
3592194915Sjamie	case PRIV_NET_SETIFMTU:
3593194915Sjamie	case PRIV_NET_SETIFFLAGS:
3594194915Sjamie	case PRIV_NET_SETIFCAP:
3595194915Sjamie	case PRIV_NET_SETIFNAME	:
3596194915Sjamie	case PRIV_NET_SETIFMETRIC:
3597194915Sjamie	case PRIV_NET_SETIFPHYS:
3598194915Sjamie	case PRIV_NET_SETIFMAC:
3599194915Sjamie	case PRIV_NET_ADDMULTI:
3600194915Sjamie	case PRIV_NET_DELMULTI:
3601194915Sjamie	case PRIV_NET_HWIOCTL:
3602194915Sjamie	case PRIV_NET_SETLLADDR:
3603194915Sjamie	case PRIV_NET_ADDIFGROUP:
3604194915Sjamie	case PRIV_NET_DELIFGROUP:
3605194915Sjamie	case PRIV_NET_IFCREATE:
3606194915Sjamie	case PRIV_NET_IFDESTROY:
3607194915Sjamie	case PRIV_NET_ADDIFADDR:
3608194915Sjamie	case PRIV_NET_DELIFADDR:
3609194915Sjamie	case PRIV_NET_LAGG:
3610194915Sjamie	case PRIV_NET_GIF:
3611194915Sjamie	case PRIV_NET_SETIFVNET:
3612164032Srwatson
3613164032Srwatson		/*
3614194915Sjamie		 * 802.11-related privileges.
3615194915Sjamie		 */
3616194915Sjamie	case PRIV_NET80211_GETKEY:
3617194915Sjamie#ifdef notyet
3618194915Sjamie	case PRIV_NET80211_MANAGE:		/* XXX-BZ discuss with sam@ */
3619194915Sjamie#endif
3620194915Sjamie
3621194915Sjamie#ifdef notyet
3622194915Sjamie		/*
3623194915Sjamie		 * AppleTalk privileges.
3624194915Sjamie		 */
3625194915Sjamie	case PRIV_NETATALK_RESERVEDPORT:
3626194915Sjamie
3627194915Sjamie		/*
3628194915Sjamie		 * ATM privileges.
3629194915Sjamie		 */
3630194915Sjamie	case PRIV_NETATM_CFG:
3631194915Sjamie	case PRIV_NETATM_ADD:
3632194915Sjamie	case PRIV_NETATM_DEL:
3633194915Sjamie	case PRIV_NETATM_SET:
3634194915Sjamie
3635194915Sjamie		/*
3636194915Sjamie		 * Bluetooth privileges.
3637194915Sjamie		 */
3638194915Sjamie	case PRIV_NETBLUETOOTH_RAW:
3639194915Sjamie#endif
3640194915Sjamie
3641194915Sjamie		/*
3642194915Sjamie		 * Netgraph and netgraph module privileges.
3643194915Sjamie		 */
3644194915Sjamie	case PRIV_NETGRAPH_CONTROL:
3645194915Sjamie#ifdef notyet
3646194915Sjamie	case PRIV_NETGRAPH_TTY:
3647194915Sjamie#endif
3648194915Sjamie
3649194915Sjamie		/*
3650194915Sjamie		 * IPv4 and IPv6 privileges.
3651194915Sjamie		 */
3652194915Sjamie	case PRIV_NETINET_IPFW:
3653194915Sjamie	case PRIV_NETINET_DIVERT:
3654194915Sjamie	case PRIV_NETINET_PF:
3655194915Sjamie	case PRIV_NETINET_DUMMYNET:
3656194915Sjamie	case PRIV_NETINET_CARP:
3657194915Sjamie	case PRIV_NETINET_MROUTE:
3658194915Sjamie	case PRIV_NETINET_RAW:
3659194915Sjamie	case PRIV_NETINET_ADDRCTRL6:
3660194915Sjamie	case PRIV_NETINET_ND6:
3661194915Sjamie	case PRIV_NETINET_SCOPE6:
3662194915Sjamie	case PRIV_NETINET_ALIFETIME6:
3663194915Sjamie	case PRIV_NETINET_IPSEC:
3664194915Sjamie	case PRIV_NETINET_BINDANY:
3665194915Sjamie
3666194915Sjamie#ifdef notyet
3667194915Sjamie		/*
3668194915Sjamie		 * IPX/SPX privileges.
3669194915Sjamie		 */
3670194915Sjamie	case PRIV_NETIPX_RESERVEDPORT:
3671194915Sjamie	case PRIV_NETIPX_RAW:
3672194915Sjamie
3673194915Sjamie		/*
3674194915Sjamie		 * NCP privileges.
3675194915Sjamie		 */
3676194915Sjamie	case PRIV_NETNCP:
3677194915Sjamie
3678194915Sjamie		/*
3679194915Sjamie		 * SMB privileges.
3680194915Sjamie		 */
3681194915Sjamie	case PRIV_NETSMB:
3682194915Sjamie#endif
3683194915Sjamie
3684194915Sjamie	/*
3685194915Sjamie	 * No default: or deny here.
3686194915Sjamie	 * In case of no permit fall through to next switch().
3687194915Sjamie	 */
3688194915Sjamie		if (cred->cr_prison->pr_flags & PR_VNET)
3689194915Sjamie			return (0);
3690194915Sjamie	}
3691194915Sjamie#endif /* VIMAGE */
3692194915Sjamie
3693194915Sjamie	switch (priv) {
3694194915Sjamie
3695194915Sjamie		/*
3696164032Srwatson		 * Allow ktrace privileges for root in jail.
3697164032Srwatson		 */
3698164032Srwatson	case PRIV_KTRACE:
3699164032Srwatson
3700166827Srwatson#if 0
3701164032Srwatson		/*
3702164032Srwatson		 * Allow jailed processes to configure audit identity and
3703164032Srwatson		 * submit audit records (login, etc).  In the future we may
3704164032Srwatson		 * want to further refine the relationship between audit and
3705164032Srwatson		 * jail.
3706164032Srwatson		 */
3707164032Srwatson	case PRIV_AUDIT_GETAUDIT:
3708164032Srwatson	case PRIV_AUDIT_SETAUDIT:
3709164032Srwatson	case PRIV_AUDIT_SUBMIT:
3710166827Srwatson#endif
3711164032Srwatson
3712164032Srwatson		/*
3713164032Srwatson		 * Allow jailed processes to manipulate process UNIX
3714164032Srwatson		 * credentials in any way they see fit.
3715164032Srwatson		 */
3716164032Srwatson	case PRIV_CRED_SETUID:
3717164032Srwatson	case PRIV_CRED_SETEUID:
3718164032Srwatson	case PRIV_CRED_SETGID:
3719164032Srwatson	case PRIV_CRED_SETEGID:
3720164032Srwatson	case PRIV_CRED_SETGROUPS:
3721164032Srwatson	case PRIV_CRED_SETREUID:
3722164032Srwatson	case PRIV_CRED_SETREGID:
3723164032Srwatson	case PRIV_CRED_SETRESUID:
3724164032Srwatson	case PRIV_CRED_SETRESGID:
3725164032Srwatson
3726164032Srwatson		/*
3727164032Srwatson		 * Jail implements visibility constraints already, so allow
3728164032Srwatson		 * jailed root to override uid/gid-based constraints.
3729164032Srwatson		 */
3730164032Srwatson	case PRIV_SEEOTHERGIDS:
3731164032Srwatson	case PRIV_SEEOTHERUIDS:
3732164032Srwatson
3733164032Srwatson		/*
3734164032Srwatson		 * Jail implements inter-process debugging limits already, so
3735164032Srwatson		 * allow jailed root various debugging privileges.
3736164032Srwatson		 */
3737164032Srwatson	case PRIV_DEBUG_DIFFCRED:
3738164032Srwatson	case PRIV_DEBUG_SUGID:
3739164032Srwatson	case PRIV_DEBUG_UNPRIV:
3740164032Srwatson
3741164032Srwatson		/*
3742164032Srwatson		 * Allow jail to set various resource limits and login
3743164032Srwatson		 * properties, and for now, exceed process resource limits.
3744164032Srwatson		 */
3745164032Srwatson	case PRIV_PROC_LIMIT:
3746164032Srwatson	case PRIV_PROC_SETLOGIN:
3747164032Srwatson	case PRIV_PROC_SETRLIMIT:
3748164032Srwatson
3749164032Srwatson		/*
3750164032Srwatson		 * System V and POSIX IPC privileges are granted in jail.
3751164032Srwatson		 */
3752164032Srwatson	case PRIV_IPC_READ:
3753164032Srwatson	case PRIV_IPC_WRITE:
3754164032Srwatson	case PRIV_IPC_ADMIN:
3755164032Srwatson	case PRIV_IPC_MSGSIZE:
3756164032Srwatson	case PRIV_MQ_ADMIN:
3757164032Srwatson
3758164032Srwatson		/*
3759192895Sjamie		 * Jail operations within a jail work on child jails.
3760192895Sjamie		 */
3761192895Sjamie	case PRIV_JAIL_ATTACH:
3762192895Sjamie	case PRIV_JAIL_SET:
3763192895Sjamie	case PRIV_JAIL_REMOVE:
3764192895Sjamie
3765192895Sjamie		/*
3766164032Srwatson		 * Jail implements its own inter-process limits, so allow
3767164032Srwatson		 * root processes in jail to change scheduling on other
3768164032Srwatson		 * processes in the same jail.  Likewise for signalling.
3769164032Srwatson		 */
3770164032Srwatson	case PRIV_SCHED_DIFFCRED:
3771185435Sbz	case PRIV_SCHED_CPUSET:
3772164032Srwatson	case PRIV_SIGNAL_DIFFCRED:
3773164032Srwatson	case PRIV_SIGNAL_SUGID:
3774164032Srwatson
3775164032Srwatson		/*
3776164032Srwatson		 * Allow jailed processes to write to sysctls marked as jail
3777164032Srwatson		 * writable.
3778164032Srwatson		 */
3779164032Srwatson	case PRIV_SYSCTL_WRITEJAIL:
3780164032Srwatson
3781164032Srwatson		/*
3782164032Srwatson		 * Allow root in jail to manage a variety of quota
3783166831Srwatson		 * properties.  These should likely be conditional on a
3784166831Srwatson		 * configuration option.
3785164032Srwatson		 */
3786166832Srwatson	case PRIV_VFS_GETQUOTA:
3787166832Srwatson	case PRIV_VFS_SETQUOTA:
3788164032Srwatson
3789164032Srwatson		/*
3790164032Srwatson		 * Since Jail relies on chroot() to implement file system
3791164032Srwatson		 * protections, grant many VFS privileges to root in jail.
3792164032Srwatson		 * Be careful to exclude mount-related and NFS-related
3793164032Srwatson		 * privileges.
3794164032Srwatson		 */
3795164032Srwatson	case PRIV_VFS_READ:
3796164032Srwatson	case PRIV_VFS_WRITE:
3797164032Srwatson	case PRIV_VFS_ADMIN:
3798164032Srwatson	case PRIV_VFS_EXEC:
3799164032Srwatson	case PRIV_VFS_LOOKUP:
3800164032Srwatson	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
3801164032Srwatson	case PRIV_VFS_CHFLAGS_DEV:
3802164032Srwatson	case PRIV_VFS_CHOWN:
3803164032Srwatson	case PRIV_VFS_CHROOT:
3804167152Spjd	case PRIV_VFS_RETAINSUGID:
3805164032Srwatson	case PRIV_VFS_FCHROOT:
3806164032Srwatson	case PRIV_VFS_LINK:
3807164032Srwatson	case PRIV_VFS_SETGID:
3808172860Srwatson	case PRIV_VFS_STAT:
3809164032Srwatson	case PRIV_VFS_STICKYFILE:
3810164032Srwatson		return (0);
3811164032Srwatson
3812164032Srwatson		/*
3813164032Srwatson		 * Depending on the global setting, allow privilege of
3814164032Srwatson		 * setting system flags.
3815164032Srwatson		 */
3816164032Srwatson	case PRIV_VFS_SYSFLAGS:
3817192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3818164032Srwatson			return (0);
3819164032Srwatson		else
3820164032Srwatson			return (EPERM);
3821164032Srwatson
3822164032Srwatson		/*
3823168396Spjd		 * Depending on the global setting, allow privilege of
3824168396Spjd		 * mounting/unmounting file systems.
3825168396Spjd		 */
3826168396Spjd	case PRIV_VFS_MOUNT:
3827168396Spjd	case PRIV_VFS_UNMOUNT:
3828168396Spjd	case PRIV_VFS_MOUNT_NONUSER:
3829168699Spjd	case PRIV_VFS_MOUNT_OWNER:
3830192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3831168396Spjd			return (0);
3832168396Spjd		else
3833168396Spjd			return (EPERM);
3834168396Spjd
3835168396Spjd		/*
3836168591Srwatson		 * Allow jailed root to bind reserved ports and reuse in-use
3837168591Srwatson		 * ports.
3838164032Srwatson		 */
3839164032Srwatson	case PRIV_NETINET_RESERVEDPORT:
3840168591Srwatson	case PRIV_NETINET_REUSEPORT:
3841164032Srwatson		return (0);
3842164032Srwatson
3843164032Srwatson		/*
3844175630Sbz		 * Allow jailed root to set certian IPv4/6 (option) headers.
3845175630Sbz		 */
3846175630Sbz	case PRIV_NETINET_SETHDROPTS:
3847175630Sbz		return (0);
3848175630Sbz
3849175630Sbz		/*
3850164032Srwatson		 * Conditionally allow creating raw sockets in jail.
3851164032Srwatson		 */
3852164032Srwatson	case PRIV_NETINET_RAW:
3853192895Sjamie		if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3854164032Srwatson			return (0);
3855164032Srwatson		else
3856164032Srwatson			return (EPERM);
3857164032Srwatson
3858164032Srwatson		/*
3859164032Srwatson		 * Since jail implements its own visibility limits on netstat
3860164032Srwatson		 * sysctls, allow getcred.  This allows identd to work in
3861164032Srwatson		 * jail.
3862164032Srwatson		 */
3863164032Srwatson	case PRIV_NETINET_GETCRED:
3864164032Srwatson		return (0);
3865164032Srwatson
3866164032Srwatson	default:
3867164032Srwatson		/*
3868164032Srwatson		 * In all remaining cases, deny the privilege request.  This
3869164032Srwatson		 * includes almost all network privileges, many system
3870164032Srwatson		 * configuration privileges.
3871164032Srwatson		 */
3872164032Srwatson		return (EPERM);
3873164032Srwatson	}
3874164032Srwatson}
3875164032Srwatson
3876192895Sjamie/*
3877192895Sjamie * Return the part of pr2's name that is relative to pr1, or the whole name
3878192895Sjamie * if it does not directly follow.
3879192895Sjamie */
3880192895Sjamie
3881192895Sjamiechar *
3882192895Sjamieprison_name(struct prison *pr1, struct prison *pr2)
3883192895Sjamie{
3884192895Sjamie	char *name;
3885192895Sjamie
3886192895Sjamie	/* Jails see themselves as "0" (if they see themselves at all). */
3887192895Sjamie	if (pr1 == pr2)
3888192895Sjamie		return "0";
3889192895Sjamie	name = pr2->pr_name;
3890192895Sjamie	if (prison_ischild(pr1, pr2)) {
3891192895Sjamie		/*
3892192895Sjamie		 * pr1 isn't locked (and allprison_lock may not be either)
3893192895Sjamie		 * so its length can't be counted on.  But the number of dots
3894192895Sjamie		 * can be counted on - and counted.
3895192895Sjamie		 */
3896192895Sjamie		for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3897192895Sjamie			name = strchr(name, '.') + 1;
3898192895Sjamie	}
3899192895Sjamie	return (name);
3900192895Sjamie}
3901192895Sjamie
3902192895Sjamie/*
3903192895Sjamie * Return the part of pr2's path that is relative to pr1, or the whole path
3904192895Sjamie * if it does not directly follow.
3905192895Sjamie */
3906192895Sjamiestatic char *
3907192895Sjamieprison_path(struct prison *pr1, struct prison *pr2)
3908192895Sjamie{
3909192895Sjamie	char *path1, *path2;
3910192895Sjamie	int len1;
3911192895Sjamie
3912192895Sjamie	path1 = pr1->pr_path;
3913192895Sjamie	path2 = pr2->pr_path;
3914192895Sjamie	if (!strcmp(path1, "/"))
3915192895Sjamie		return (path2);
3916192895Sjamie	len1 = strlen(path1);
3917192895Sjamie	if (strncmp(path1, path2, len1))
3918192895Sjamie		return (path2);
3919192895Sjamie	if (path2[len1] == '\0')
3920192895Sjamie		return "/";
3921192895Sjamie	if (path2[len1] == '/')
3922192895Sjamie		return (path2 + len1);
3923192895Sjamie	return (path2);
3924192895Sjamie}
3925192895Sjamie
3926192895Sjamie
3927192895Sjamie/*
3928192895Sjamie * Jail-related sysctls.
3929192895Sjamie */
3930192895SjamieSYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3931192895Sjamie    "Jails");
3932192895Sjamie
3933113275Smikestatic int
3934113275Smikesysctl_jail_list(SYSCTL_HANDLER_ARGS)
3935113275Smike{
3936191673Sjamie	struct xprison *xp;
3937192895Sjamie	struct prison *pr, *cpr;
3938191673Sjamie#ifdef INET
3939191673Sjamie	struct in_addr *ip4 = NULL;
3940191673Sjamie	int ip4s = 0;
3941191673Sjamie#endif
3942191673Sjamie#ifdef INET6
3943191673Sjamie	struct in_addr *ip6 = NULL;
3944191673Sjamie	int ip6s = 0;
3945191673Sjamie#endif
3946192895Sjamie	int descend, error;
3947113275Smike
3948191673Sjamie	xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3949192895Sjamie	pr = req->td->td_ucred->cr_prison;
3950191673Sjamie	error = 0;
3951168401Spjd	sx_slock(&allprison_lock);
3952192895Sjamie	FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3953192895Sjamie#if defined(INET) || defined(INET6)
3954191673Sjamie again:
3955192895Sjamie#endif
3956192895Sjamie		mtx_lock(&cpr->pr_mtx);
3957185435Sbz#ifdef INET
3958192895Sjamie		if (cpr->pr_ip4s > 0) {
3959192895Sjamie			if (ip4s < cpr->pr_ip4s) {
3960192895Sjamie				ip4s = cpr->pr_ip4s;
3961192895Sjamie				mtx_unlock(&cpr->pr_mtx);
3962191673Sjamie				ip4 = realloc(ip4, ip4s *
3963191673Sjamie				    sizeof(struct in_addr), M_TEMP, M_WAITOK);
3964191673Sjamie				goto again;
3965191673Sjamie			}
3966192895Sjamie			bcopy(cpr->pr_ip4, ip4,
3967192895Sjamie			    cpr->pr_ip4s * sizeof(struct in_addr));
3968191673Sjamie		}
3969185435Sbz#endif
3970185435Sbz#ifdef INET6
3971192895Sjamie		if (cpr->pr_ip6s > 0) {
3972192895Sjamie			if (ip6s < cpr->pr_ip6s) {
3973192895Sjamie				ip6s = cpr->pr_ip6s;
3974192895Sjamie				mtx_unlock(&cpr->pr_mtx);
3975191673Sjamie				ip6 = realloc(ip6, ip6s *
3976191673Sjamie				    sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3977191673Sjamie				goto again;
3978191673Sjamie			}
3979192895Sjamie			bcopy(cpr->pr_ip6, ip6,
3980192895Sjamie			    cpr->pr_ip6s * sizeof(struct in6_addr));
3981191673Sjamie		}
3982185435Sbz#endif
3983192895Sjamie		if (cpr->pr_ref == 0) {
3984192895Sjamie			mtx_unlock(&cpr->pr_mtx);
3985191673Sjamie			continue;
3986191673Sjamie		}
3987191673Sjamie		bzero(xp, sizeof(*xp));
3988113275Smike		xp->pr_version = XPRISON_VERSION;
3989192895Sjamie		xp->pr_id = cpr->pr_id;
3990192895Sjamie		xp->pr_state = cpr->pr_uref > 0
3991191673Sjamie		    ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3992192895Sjamie		strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3993194118Sjamie		strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3994192895Sjamie		strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3995185435Sbz#ifdef INET
3996192895Sjamie		xp->pr_ip4s = cpr->pr_ip4s;
3997185435Sbz#endif
3998185435Sbz#ifdef INET6
3999192895Sjamie		xp->pr_ip6s = cpr->pr_ip6s;
4000185435Sbz#endif
4001192895Sjamie		mtx_unlock(&cpr->pr_mtx);
4002191673Sjamie		error = SYSCTL_OUT(req, xp, sizeof(*xp));
4003191673Sjamie		if (error)
4004191673Sjamie			break;
4005185435Sbz#ifdef INET
4006191673Sjamie		if (xp->pr_ip4s > 0) {
4007191673Sjamie			error = SYSCTL_OUT(req, ip4,
4008191673Sjamie			    xp->pr_ip4s * sizeof(struct in_addr));
4009191673Sjamie			if (error)
4010191673Sjamie				break;
4011185435Sbz		}
4012185435Sbz#endif
4013185435Sbz#ifdef INET6
4014191673Sjamie		if (xp->pr_ip6s > 0) {
4015191673Sjamie			error = SYSCTL_OUT(req, ip6,
4016191673Sjamie			    xp->pr_ip6s * sizeof(struct in6_addr));
4017191673Sjamie			if (error)
4018191673Sjamie				break;
4019185435Sbz		}
4020185435Sbz#endif
4021113275Smike	}
4022168401Spjd	sx_sunlock(&allprison_lock);
4023191673Sjamie	free(xp, M_TEMP);
4024191673Sjamie#ifdef INET
4025191673Sjamie	free(ip4, M_TEMP);
4026191673Sjamie#endif
4027191673Sjamie#ifdef INET6
4028191673Sjamie	free(ip6, M_TEMP);
4029191673Sjamie#endif
4030167354Spjd	return (error);
4031113275Smike}
4032113275Smike
4033187864SedSYSCTL_OID(_security_jail, OID_AUTO, list,
4034187864Sed    CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4035187864Sed    sysctl_jail_list, "S", "List of active jails");
4036126004Spjd
4037126004Spjdstatic int
4038126004Spjdsysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4039126004Spjd{
4040126004Spjd	int error, injail;
4041126004Spjd
4042126004Spjd	injail = jailed(req->td->td_ucred);
4043126004Spjd	error = SYSCTL_OUT(req, &injail, sizeof(injail));
4044126004Spjd
4045126004Spjd	return (error);
4046126004Spjd}
4047192895Sjamie
4048187864SedSYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4049187864Sed    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4050187864Sed    sysctl_jail_jailed, "I", "Process in jail?");
4051185435Sbz
4052192895Sjamie#if defined(INET) || defined(INET6)
4053193865SjamieSYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4054192895Sjamie    &jail_max_af_ips, 0,
4055192895Sjamie    "Number of IP addresses a jail may have at most per address family");
4056192895Sjamie#endif
4057192895Sjamie
4058192895Sjamie/*
4059192895Sjamie * Default parameters for jail(2) compatability.  For historical reasons,
4060192895Sjamie * the sysctl names have varying similarity to the parameter names.  Prisons
4061192895Sjamie * just see their own parameters, and can't change them.
4062192895Sjamie */
4063192895Sjamiestatic int
4064192895Sjamiesysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4065192895Sjamie{
4066192895Sjamie	struct prison *pr;
4067192895Sjamie	int allow, error, i;
4068192895Sjamie
4069192895Sjamie	pr = req->td->td_ucred->cr_prison;
4070192895Sjamie	allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4071192895Sjamie
4072192895Sjamie	/* Get the current flag value, and convert it to a boolean. */
4073192895Sjamie	i = (allow & arg2) ? 1 : 0;
4074192895Sjamie	if (arg1 != NULL)
4075192895Sjamie		i = !i;
4076192895Sjamie	error = sysctl_handle_int(oidp, &i, 0, req);
4077192895Sjamie	if (error || !req->newptr)
4078192895Sjamie		return (error);
4079192895Sjamie	i = i ? arg2 : 0;
4080192895Sjamie	if (arg1 != NULL)
4081192895Sjamie		i ^= arg2;
4082192895Sjamie	/*
4083192895Sjamie	 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4084192895Sjamie	 * for writing.
4085192895Sjamie	 */
4086192895Sjamie	mtx_lock(&prison0.pr_mtx);
4087192895Sjamie	jail_default_allow = (jail_default_allow & ~arg2) | i;
4088192895Sjamie	mtx_unlock(&prison0.pr_mtx);
4089192895Sjamie	return (0);
4090192895Sjamie}
4091192895Sjamie
4092192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4093192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4094192895Sjamie    NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4095192895Sjamie    "Processes in jail can set their hostnames");
4096192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4097192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4098192895Sjamie    (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4099192895Sjamie    "Processes in jail are limited to creating UNIX/IP/route sockets only");
4100192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4101192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4102192895Sjamie    NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4103192895Sjamie    "Processes in jail can use System V IPC primitives");
4104192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4105192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4106192895Sjamie    NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4107192895Sjamie    "Prison root can create raw sockets");
4108192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4109192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4110192895Sjamie    NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4111192895Sjamie    "Processes in jail can alter system file flags");
4112192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4113192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4114192895Sjamie    NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4115192895Sjamie    "Processes in jail can mount/unmount jail-friendly file systems");
4116192895Sjamie
4117192895Sjamiestatic int
4118192895Sjamiesysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4119192895Sjamie{
4120192895Sjamie	struct prison *pr;
4121192895Sjamie	int level, error;
4122192895Sjamie
4123192895Sjamie	pr = req->td->td_ucred->cr_prison;
4124192895Sjamie	level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4125192895Sjamie	error = sysctl_handle_int(oidp, &level, 0, req);
4126192895Sjamie	if (error || !req->newptr)
4127192895Sjamie		return (error);
4128192895Sjamie	*(int *)arg1 = level;
4129192895Sjamie	return (0);
4130192895Sjamie}
4131192895Sjamie
4132192895SjamieSYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4133192895Sjamie    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4134192895Sjamie    &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4135192895Sjamie    sysctl_jail_default_level, "I",
4136192895Sjamie    "Processes in jail cannot see all mounted file systems");
4137192895Sjamie
4138192895Sjamie/*
4139192895Sjamie * Nodes to describe jail parameters.  Maximum length of string parameters
4140192895Sjamie * is returned in the string itself, and the other parameters exist merely
4141192895Sjamie * to make themselves and their types known.
4142192895Sjamie */
4143192895SjamieSYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4144192895Sjamie    "Jail parameters");
4145192895Sjamie
4146192895Sjamieint
4147192895Sjamiesysctl_jail_param(SYSCTL_HANDLER_ARGS)
4148192895Sjamie{
4149192895Sjamie	int i;
4150192895Sjamie	long l;
4151192895Sjamie	size_t s;
4152192895Sjamie	char numbuf[12];
4153192895Sjamie
4154192895Sjamie	switch (oidp->oid_kind & CTLTYPE)
4155192895Sjamie	{
4156192895Sjamie	case CTLTYPE_LONG:
4157192895Sjamie	case CTLTYPE_ULONG:
4158192895Sjamie		l = 0;
4159192895Sjamie#ifdef SCTL_MASK32
4160192895Sjamie		if (!(req->flags & SCTL_MASK32))
4161192895Sjamie#endif
4162192895Sjamie			return (SYSCTL_OUT(req, &l, sizeof(l)));
4163192895Sjamie	case CTLTYPE_INT:
4164192895Sjamie	case CTLTYPE_UINT:
4165192895Sjamie		i = 0;
4166192895Sjamie		return (SYSCTL_OUT(req, &i, sizeof(i)));
4167192895Sjamie	case CTLTYPE_STRING:
4168192895Sjamie		snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4169192895Sjamie		return
4170192895Sjamie		    (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4171192895Sjamie	case CTLTYPE_STRUCT:
4172192895Sjamie		s = (size_t)arg2;
4173192895Sjamie		return (SYSCTL_OUT(req, &s, sizeof(s)));
4174192895Sjamie	}
4175192895Sjamie	return (0);
4176192895Sjamie}
4177192895Sjamie
4178192895SjamieSYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4179192895SjamieSYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4180192895SjamieSYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4181192895SjamieSYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4182192895SjamieSYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4183192895Sjamie    "I", "Jail secure level");
4184192895SjamieSYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4185192895Sjamie    "I", "Jail cannot see all mounted file systems");
4186192895SjamieSYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4187192895Sjamie    "B", "Jail persistence");
4188194251Sjamie#ifdef VIMAGE
4189194251SjamieSYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4190195870Sjamie    "E,jailsys", "Virtual network stack");
4191194251Sjamie#endif
4192192895SjamieSYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4193192895Sjamie    "B", "Jail is in the process of shutting down");
4194192895Sjamie
4195194762SjamieSYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4196194762SjamieSYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4197194762Sjamie    "I", "Current number of child jails");
4198194762SjamieSYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4199194762Sjamie    "I", "Maximum number of child jails");
4200194762Sjamie
4201195870SjamieSYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4202192895SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4203192895Sjamie    "Jail hostname");
4204193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4205193066Sjamie    "Jail NIS domainname");
4206193066SjamieSYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4207193066Sjamie    "Jail host UUID");
4208193066SjamieSYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4209193066Sjamie    "LU", "Jail host ID");
4210192895Sjamie
4211192895SjamieSYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4212192895SjamieSYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4213192895Sjamie
4214192895Sjamie#ifdef INET
4215195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4216195974Sjamie    "Jail IPv4 address virtualization");
4217192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4218192895Sjamie    "S,in_addr,a", "Jail IPv4 addresses");
4219202468SbzSYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4220202468Sbz    "B", "Do (not) use IPv4 source address selection rather than the "
4221202468Sbz    "primary jail IPv4 address.");
4222192895Sjamie#endif
4223192895Sjamie#ifdef INET6
4224195974SjamieSYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4225195974Sjamie    "Jail IPv6 address virtualization");
4226192895SjamieSYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4227192895Sjamie    "S,in6_addr,a", "Jail IPv6 addresses");
4228202468SbzSYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4229202468Sbz    "B", "Do (not) use IPv6 source address selection rather than the "
4230202468Sbz    "primary jail IPv6 address.");
4231192895Sjamie#endif
4232192895Sjamie
4233192895SjamieSYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4234192895SjamieSYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4235192895Sjamie    "B", "Jail may set hostname");
4236192895SjamieSYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4237192895Sjamie    "B", "Jail may use SYSV IPC");
4238192895SjamieSYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4239192895Sjamie    "B", "Jail may create raw sockets");
4240192895SjamieSYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4241192895Sjamie    "B", "Jail may alter system file flags");
4242192895SjamieSYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4243192895Sjamie    "B", "Jail may mount/unmount jail-friendly file systems");
4244192895SjamieSYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4245192895Sjamie    "B", "Jail may set file quotas");
4246192895SjamieSYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4247192895Sjamie    "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4248192895Sjamie
4249192895Sjamie
4250185435Sbz#ifdef DDB
4251191673Sjamie
4252191673Sjamiestatic void
4253191673Sjamiedb_show_prison(struct prison *pr)
4254185435Sbz{
4255192895Sjamie	int fi;
4256191673Sjamie#if defined(INET) || defined(INET6)
4257191673Sjamie	int ii;
4258185435Sbz#endif
4259195870Sjamie	unsigned jsf;
4260185435Sbz#ifdef INET6
4261185435Sbz	char ip6buf[INET6_ADDRSTRLEN];
4262185435Sbz#endif
4263185435Sbz
4264191673Sjamie	db_printf("prison %p:\n", pr);
4265191673Sjamie	db_printf(" jid             = %d\n", pr->pr_id);
4266191673Sjamie	db_printf(" name            = %s\n", pr->pr_name);
4267192895Sjamie	db_printf(" parent          = %p\n", pr->pr_parent);
4268191673Sjamie	db_printf(" ref             = %d\n", pr->pr_ref);
4269191673Sjamie	db_printf(" uref            = %d\n", pr->pr_uref);
4270191673Sjamie	db_printf(" path            = %s\n", pr->pr_path);
4271191673Sjamie	db_printf(" cpuset          = %d\n", pr->pr_cpuset
4272191673Sjamie	    ? pr->pr_cpuset->cs_id : -1);
4273194251Sjamie#ifdef VIMAGE
4274194251Sjamie	db_printf(" vnet            = %p\n", pr->pr_vnet);
4275194251Sjamie#endif
4276191673Sjamie	db_printf(" root            = %p\n", pr->pr_root);
4277191673Sjamie	db_printf(" securelevel     = %d\n", pr->pr_securelevel);
4278202123Sbz	db_printf(" children.max    = %d\n", pr->pr_childmax);
4279202123Sbz	db_printf(" children.cur    = %d\n", pr->pr_childcount);
4280192895Sjamie	db_printf(" child           = %p\n", LIST_FIRST(&pr->pr_children));
4281192895Sjamie	db_printf(" sibling         = %p\n", LIST_NEXT(pr, pr_sibling));
4282202123Sbz	db_printf(" flags           = 0x%x", pr->pr_flags);
4283192895Sjamie	for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4284192895Sjamie	    fi++)
4285192895Sjamie		if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4286192895Sjamie			db_printf(" %s", pr_flag_names[fi]);
4287195870Sjamie	for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4288195870Sjamie	    fi++) {
4289195870Sjamie		jsf = pr->pr_flags &
4290195870Sjamie		    (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4291195870Sjamie		db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4292195870Sjamie		    pr_flag_jailsys[fi].disable &&
4293195870Sjamie		      (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4294195870Sjamie		    : (jsf == pr_flag_jailsys[fi].new) ? "new"
4295195870Sjamie		    : "inherit");
4296195870Sjamie	}
4297202123Sbz	db_printf(" allow           = 0x%x", pr->pr_allow);
4298192895Sjamie	for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4299192895Sjamie	    fi++)
4300192895Sjamie		if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4301192895Sjamie			db_printf(" %s", pr_allow_names[fi]);
4302191673Sjamie	db_printf("\n");
4303192895Sjamie	db_printf(" enforce_statfs  = %d\n", pr->pr_enforce_statfs);
4304194118Sjamie	db_printf(" host.hostname   = %s\n", pr->pr_hostname);
4305194118Sjamie	db_printf(" host.domainname = %s\n", pr->pr_domainname);
4306194118Sjamie	db_printf(" host.hostuuid   = %s\n", pr->pr_hostuuid);
4307193066Sjamie	db_printf(" host.hostid     = %lu\n", pr->pr_hostid);
4308185435Sbz#ifdef INET
4309191673Sjamie	db_printf(" ip4s            = %d\n", pr->pr_ip4s);
4310191673Sjamie	for (ii = 0; ii < pr->pr_ip4s; ii++)
4311191673Sjamie		db_printf(" %s %s\n",
4312202123Sbz		    ii == 0 ? "ip4.addr        =" : "                 ",
4313191673Sjamie		    inet_ntoa(pr->pr_ip4[ii]));
4314185435Sbz#endif
4315185435Sbz#ifdef INET6
4316191673Sjamie	db_printf(" ip6s            = %d\n", pr->pr_ip6s);
4317191673Sjamie	for (ii = 0; ii < pr->pr_ip6s; ii++)
4318191673Sjamie		db_printf(" %s %s\n",
4319202123Sbz		    ii == 0 ? "ip6.addr        =" : "                 ",
4320191673Sjamie		    ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4321191673Sjamie#endif
4322191673Sjamie}
4323191673Sjamie
4324191673SjamieDB_SHOW_COMMAND(prison, db_show_prison_command)
4325191673Sjamie{
4326191673Sjamie	struct prison *pr;
4327191673Sjamie
4328191673Sjamie	if (!have_addr) {
4329192895Sjamie		/*
4330192895Sjamie		 * Show all prisons in the list, and prison0 which is not
4331192895Sjamie		 * listed.
4332192895Sjamie		 */
4333192895Sjamie		db_show_prison(&prison0);
4334192895Sjamie		if (!db_pager_quit) {
4335192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list) {
4336192895Sjamie				db_show_prison(pr);
4337192895Sjamie				if (db_pager_quit)
4338192895Sjamie					break;
4339192895Sjamie			}
4340191673Sjamie		}
4341191673Sjamie		return;
4342191673Sjamie	}
4343191673Sjamie
4344192895Sjamie	if (addr == 0)
4345192895Sjamie		pr = &prison0;
4346192895Sjamie	else {
4347192895Sjamie		/* Look for a prison with the ID and with references. */
4348191673Sjamie		TAILQ_FOREACH(pr, &allprison, pr_list)
4349192895Sjamie			if (pr->pr_id == addr && pr->pr_ref > 0)
4350191673Sjamie				break;
4351192895Sjamie		if (pr == NULL)
4352192895Sjamie			/* Look again, without requiring a reference. */
4353192895Sjamie			TAILQ_FOREACH(pr, &allprison, pr_list)
4354192895Sjamie				if (pr->pr_id == addr)
4355192895Sjamie					break;
4356192895Sjamie		if (pr == NULL)
4357192895Sjamie			/* Assume address points to a valid prison. */
4358192895Sjamie			pr = (struct prison *)addr;
4359192895Sjamie	}
4360191673Sjamie	db_show_prison(pr);
4361185435Sbz}
4362191673Sjamie
4363185435Sbz#endif /* DDB */
4364