xref: /dragonfly/sys/kern/kern_jail.c (revision 0212bfce)
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7  * ----------------------------------------------------------------------------
8  *
9  */
10 /*-
11  * Copyright (c) 2006 Victor Balada Diaz <victor@bsdes.net>
12  * All rights reserved.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 
37 /*
38  * $FreeBSD: src/sys/kern/kern_jail.c,v 1.6.2.3 2001/08/17 01:00:26 rwatson Exp $
39  */
40 
41 #include "opt_inet6.h"
42 
43 #include <sys/param.h>
44 #include <sys/types.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/errno.h>
48 #include <sys/sysproto.h>
49 #include <sys/malloc.h>
50 #include <sys/nlookup.h>
51 #include <sys/namecache.h>
52 #include <sys/proc.h>
53 #include <sys/priv.h>
54 #include <sys/jail.h>
55 #include <sys/socket.h>
56 #include <sys/sysctl.h>
57 #include <sys/kern_syscall.h>
58 #include <net/if.h>
59 #include <netinet/in.h>
60 #include <netinet6/in6_var.h>
61 
62 static struct prison	*prison_find(int);
63 static void		prison_ipcache_init(struct prison *);
64 
65 static prison_cap_t	prison_default_caps;
66 
67 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
68 
69 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
70     "All jails settings");
71 
72 SYSCTL_NODE(_jail, OID_AUTO, defaults, CTLFLAG_RW, 0,
73     "Default options for jails");
74 
75 SYSCTL_BIT64(_jail_defaults, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
76     &prison_default_caps, 1, PRISON_CAP_SYS_SET_HOSTNAME,
77     "Processes in jail can set their hostnames");
78 
79 SYSCTL_BIT64(_jail_defaults, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
80     &prison_default_caps, 0, PRISON_CAP_NET_UNIXIPROUTE,
81     "Processes in jail are limited to creating UNIX/IPv[46]/route sockets only");
82 
83 SYSCTL_BIT64(_jail_defaults, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
84     &prison_default_caps, 0, PRISON_CAP_SYS_SYSVIPC,
85     "Processes in jail can use System V IPC primitives");
86 
87 SYSCTL_BIT64(_jail_defaults, OID_AUTO, chflags_allowed, CTLFLAG_RW,
88     &prison_default_caps, 0, PRISON_CAP_VFS_CHFLAGS,
89     "Processes in jail can alter system file flags");
90 
91 SYSCTL_BIT64(_jail_defaults, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
92     &prison_default_caps, 0, PRISON_CAP_NET_RAW_SOCKETS,
93     "Process in jail can create raw sockets");
94 
95 SYSCTL_BIT64(_jail_defaults, OID_AUTO, vfs_mount_nullfs, CTLFLAG_RW,
96     &prison_default_caps, 0, PRISON_CAP_VFS_MOUNT_NULLFS,
97     "Process in jail can mount nullfs(5) filesystems");
98 
99 SYSCTL_BIT64(_jail_defaults, OID_AUTO, vfs_mount_tmpfs, CTLFLAG_RW,
100     &prison_default_caps, 0, PRISON_CAP_VFS_MOUNT_TMPFS,
101     "Process in jail can mount tmpfs(5) filesystems");
102 
103 int	lastprid = 0;
104 int	prisoncount = 0;
105 
106 static struct lock jail_lock =
107        LOCK_INITIALIZER("jail", 0, LK_CANRECURSE);
108 
109 LIST_HEAD(prisonlist, prison);
110 struct	prisonlist allprison = LIST_HEAD_INITIALIZER(&allprison);
111 
112 static int
113 kern_jail_attach(int jid)
114 {
115 	struct proc *p = curthread->td_proc;
116 	struct prison *pr;
117 	struct ucred *cr;
118 	int error;
119 
120 	pr = prison_find(jid);
121 	if (pr == NULL)
122 		return(EINVAL);
123 
124 	error = kern_chroot(&pr->pr_root);
125 	if (error)
126 		return(error);
127 
128 	prison_hold(pr);
129 	lwkt_gettoken(&p->p_token);
130 	cr = cratom_proc(p);
131 	cr->cr_prison = pr;
132 	p->p_flags |= P_JAILED;
133 	lwkt_reltoken(&p->p_token);
134 
135 	return(0);
136 }
137 
138 static int
139 assign_prison_id(struct prison *pr)
140 {
141 	int tryprid;
142 	struct prison *tpr;
143 
144 	tryprid = lastprid + 1;
145 	if (tryprid == JAIL_MAX)
146 		tryprid = 1;
147 
148 	lockmgr(&jail_lock, LK_EXCLUSIVE);
149 next:
150 	LIST_FOREACH(tpr, &allprison, pr_list) {
151 		if (tpr->pr_id != tryprid)
152 			continue;
153 		tryprid++;
154 		if (tryprid == JAIL_MAX) {
155 			lockmgr(&jail_lock, LK_RELEASE);
156 			return (ERANGE);
157 		}
158 		goto next;
159 	}
160 	pr->pr_id = lastprid = tryprid;
161 	lockmgr(&jail_lock, LK_RELEASE);
162 
163 	return (0);
164 }
165 
166 static int
167 kern_jail(struct prison *pr, struct jail *j)
168 {
169 	int error;
170 	struct nlookupdata nd;
171 
172 	error = nlookup_init(&nd, j->path, UIO_USERSPACE, NLC_FOLLOW);
173 	if (error) {
174 		nlookup_done(&nd);
175 		return (error);
176 	}
177 	error = nlookup(&nd);
178 	if (error) {
179 		nlookup_done(&nd);
180 		return (error);
181 	}
182 	cache_copy(&nd.nl_nch, &pr->pr_root);
183 
184 	varsymset_init(&pr->pr_varsymset, NULL);
185 	prison_ipcache_init(pr);
186 
187 	error = assign_prison_id(pr);
188 	if (error) {
189 		varsymset_clean(&pr->pr_varsymset);
190 		nlookup_done(&nd);
191 		return (error);
192 	}
193 
194 	lockmgr(&jail_lock, LK_EXCLUSIVE);
195 	LIST_INSERT_HEAD(&allprison, pr, pr_list);
196 	++prisoncount;
197 	lockmgr(&jail_lock, LK_RELEASE);
198 
199 	error = prison_sysctl_create(pr);
200 	if (error)
201 		goto out;
202 
203 	error = kern_jail_attach(pr->pr_id);
204 	if (error)
205 		goto out2;
206 
207 	nlookup_done(&nd);
208 	return 0;
209 
210 out2:
211 	prison_sysctl_done(pr);
212 
213 out:
214 	lockmgr(&jail_lock, LK_EXCLUSIVE);
215 	LIST_REMOVE(pr, pr_list);
216 	--prisoncount;
217 	lockmgr(&jail_lock, LK_RELEASE);
218 	varsymset_clean(&pr->pr_varsymset);
219 	nlookup_done(&nd);
220 	return (error);
221 }
222 
223 /*
224  * jail()
225  *
226  * jail_args(syscallarg(struct jail *) jail)
227  *
228  * MPALMOSTSAFE
229  */
230 int
231 sys_jail(struct jail_args *uap)
232 {
233 	struct thread *td = curthread;
234 	struct prison *pr;
235 	struct jail_ip_storage *jip;
236 	struct jail j;
237 	int error;
238 	uint32_t jversion;
239 
240 	uap->sysmsg_result = -1;
241 
242 	error = priv_check(td, PRIV_JAIL_CREATE);
243 	if (error)
244 		return (error);
245 
246 	error = copyin(uap->jail, &jversion, sizeof(jversion));
247 	if (error)
248 		return (error);
249 
250 	pr = kmalloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
251 	SLIST_INIT(&pr->pr_ips);
252 	lockmgr(&jail_lock, LK_EXCLUSIVE);
253 
254 	switch (jversion) {
255 	case 0:
256 		/* Single IPv4 jails. */
257 		{
258 		struct jail_v0 jv0;
259 		struct sockaddr_in ip4addr;
260 
261 		error = copyin(uap->jail, &jv0, sizeof(jv0));
262 		if (error)
263 			goto out;
264 
265 		j.path = jv0.path;
266 		j.hostname = jv0.hostname;
267 
268 		jip = kmalloc(sizeof(*jip),  M_PRISON, M_WAITOK | M_ZERO);
269 		ip4addr.sin_family = AF_INET;
270 		ip4addr.sin_addr.s_addr = htonl(jv0.ip_number);
271 		memcpy(&jip->ip, &ip4addr, sizeof(ip4addr));
272 		SLIST_INSERT_HEAD(&pr->pr_ips, jip, entries);
273 		break;
274 		}
275 
276 	case 1:
277 		/*
278 		 * DragonFly multi noIP/IPv4/IPv6 jails
279 		 *
280 		 * NOTE: This version is unsupported by FreeBSD
281 		 * (which uses version 2 instead).
282 		 */
283 
284 		error = copyin(uap->jail, &j, sizeof(j));
285 		if (error)
286 			goto out;
287 
288 		for (int i = 0; i < j.n_ips; i++) {
289 			jip = kmalloc(sizeof(*jip), M_PRISON,
290 				      M_WAITOK | M_ZERO);
291 			SLIST_INSERT_HEAD(&pr->pr_ips, jip, entries);
292 			error = copyin(&j.ips[i], &jip->ip,
293 					sizeof(struct sockaddr_storage));
294 			if (error)
295 				goto out;
296 		}
297 		break;
298 	default:
299 		error = EINVAL;
300 		goto out;
301 	}
302 
303 	error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
304 	if (error)
305 		goto out;
306 
307 	/* Use default capabilities as a template */
308 	pr->pr_caps = prison_default_caps;
309 
310 	error = kern_jail(pr, &j);
311 	if (error)
312 		goto out;
313 
314 	uap->sysmsg_result = pr->pr_id;
315 	lockmgr(&jail_lock, LK_RELEASE);
316 
317 	return (0);
318 
319 out:
320 	/* Delete all ips */
321 	while (!SLIST_EMPTY(&pr->pr_ips)) {
322 		jip = SLIST_FIRST(&pr->pr_ips);
323 		SLIST_REMOVE_HEAD(&pr->pr_ips, entries);
324 		kfree(jip, M_PRISON);
325 	}
326 	lockmgr(&jail_lock, LK_RELEASE);
327 	kfree(pr, M_PRISON);
328 
329 	return (error);
330 }
331 
332 /*
333  * int jail_attach(int jid);
334  *
335  * MPALMOSTSAFE
336  */
337 int
338 sys_jail_attach(struct jail_attach_args *uap)
339 {
340 	struct thread *td = curthread;
341 	int error;
342 
343 	error = priv_check(td, PRIV_JAIL_ATTACH);
344 	if (error)
345 		return(error);
346 	lockmgr(&jail_lock, LK_EXCLUSIVE);
347 	error = kern_jail_attach(uap->jid);
348 	lockmgr(&jail_lock, LK_RELEASE);
349 	return (error);
350 }
351 
352 static void
353 prison_ipcache_init(struct prison *pr)
354 {
355 	struct jail_ip_storage *jis;
356 	struct sockaddr_in *ip4;
357 	struct sockaddr_in6 *ip6;
358 
359 	lockmgr(&jail_lock, LK_EXCLUSIVE);
360 	SLIST_FOREACH(jis, &pr->pr_ips, entries) {
361 		switch (jis->ip.ss_family) {
362 		case AF_INET:
363 			ip4 = (struct sockaddr_in *)&jis->ip;
364 			if ((ntohl(ip4->sin_addr.s_addr) >> IN_CLASSA_NSHIFT) ==
365 			    IN_LOOPBACKNET) {
366 				/* loopback address */
367 				if (pr->local_ip4 == NULL)
368 					pr->local_ip4 = ip4;
369 			} else {
370 				/* public address */
371 				if (pr->nonlocal_ip4 == NULL)
372 					pr->nonlocal_ip4 = ip4;
373 			}
374 			break;
375 
376 		case AF_INET6:
377 			ip6 = (struct sockaddr_in6 *)&jis->ip;
378 			if (IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr)) {
379 				/* loopback address */
380 				if (pr->local_ip6 == NULL)
381 					pr->local_ip6 = ip6;
382 			} else {
383 				/* public address */
384 				if (pr->nonlocal_ip6 == NULL)
385 					pr->nonlocal_ip6 = ip6;
386 			}
387 			break;
388 		}
389 	}
390 	lockmgr(&jail_lock, LK_RELEASE);
391 }
392 
393 /*
394  * Changes INADDR_LOOPBACK for a valid jail address.
395  * ip is in network byte order.
396  * Returns 1 if the ip is among jail valid ips.
397  * Returns 0 if is not among jail valid ips or
398  * if couldn't replace INADDR_LOOPBACK for a valid
399  * IP.
400  */
401 int
402 prison_replace_wildcards(struct thread *td, struct sockaddr *ip)
403 {
404 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
405 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
406 	struct prison *pr;
407 
408 	if (td->td_proc == NULL || td->td_ucred == NULL)
409 		return (1);
410 	if ((pr = td->td_ucred->cr_prison) == NULL)
411 		return (1);
412 
413 	if ((ip->sa_family == AF_INET &&
414 	    ip4->sin_addr.s_addr == htonl(INADDR_ANY)) ||
415 	    (ip->sa_family == AF_INET6 &&
416 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->sin6_addr)))
417 		return (1);
418 	if ((ip->sa_family == AF_INET &&
419 	    ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) ||
420 	    (ip->sa_family == AF_INET6 &&
421 	    IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr))) {
422 		if (!prison_get_local(pr, ip->sa_family, ip) &&
423 		    !prison_get_nonlocal(pr, ip->sa_family, ip))
424 			return(0);
425 		else
426 			return(1);
427 	}
428 	if (jailed_ip(pr, ip))
429 		return(1);
430 	return(0);
431 }
432 
433 int
434 prison_remote_ip(struct thread *td, struct sockaddr *ip)
435 {
436 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
437 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
438 	struct prison *pr;
439 
440 	if (td == NULL || td->td_proc == NULL || td->td_ucred == NULL)
441 		return(1);
442 	if ((pr = td->td_ucred->cr_prison) == NULL)
443 		return(1);
444 	if ((ip->sa_family == AF_INET &&
445 	    ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) ||
446 	    (ip->sa_family == AF_INET6 &&
447 	    IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr))) {
448 		if (!prison_get_local(pr, ip->sa_family, ip) &&
449 		    !prison_get_nonlocal(pr, ip->sa_family, ip))
450 			return(0);
451 		else
452 			return(1);
453 	}
454 	return(1);
455 }
456 
457 /*
458  * Prison get non loopback ip:
459  * - af is the address family of the ip we want (AF_INET|AF_INET6).
460  * - If ip != NULL, put the first IP address that is not a loopback address
461  *   into *ip.
462  *
463  * ip is in network by order and we don't touch it unless we find a valid ip.
464  * No matter if ip == NULL or not, we return either a valid struct sockaddr *,
465  * or NULL.  This struct may not be modified.
466  */
467 struct sockaddr *
468 prison_get_nonlocal(struct prison *pr, sa_family_t af, struct sockaddr *ip)
469 {
470 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
471 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
472 
473 	/* Check if it is cached */
474 	switch(af) {
475 	case AF_INET:
476 		if (ip4 != NULL && pr->nonlocal_ip4 != NULL)
477 			ip4->sin_addr.s_addr = pr->nonlocal_ip4->sin_addr.s_addr;
478 		return (struct sockaddr *)pr->nonlocal_ip4;
479 
480 	case AF_INET6:
481 		if (ip6 != NULL && pr->nonlocal_ip6 != NULL)
482 			ip6->sin6_addr = pr->nonlocal_ip6->sin6_addr;
483 		return (struct sockaddr *)pr->nonlocal_ip6;
484 	}
485 
486 	/* NOTREACHED */
487 	return NULL;
488 }
489 
490 /*
491  * Prison get loopback ip.
492  * - af is the address family of the ip we want (AF_INET|AF_INET6).
493  * - If ip != NULL, put the first IP address that is not a loopback address
494  *   into *ip.
495  *
496  * ip is in network by order and we don't touch it unless we find a valid ip.
497  * No matter if ip == NULL or not, we return either a valid struct sockaddr *,
498  * or NULL.  This struct may not be modified.
499  */
500 struct sockaddr *
501 prison_get_local(struct prison *pr, sa_family_t af, struct sockaddr *ip)
502 {
503 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
504 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
505 
506 	/* Check if it is cached */
507 	switch(af) {
508 	case AF_INET:
509 		if (ip4 != NULL && pr->local_ip4 != NULL)
510 			ip4->sin_addr.s_addr = pr->local_ip4->sin_addr.s_addr;
511 		return (struct sockaddr *)pr->local_ip4;
512 
513 	case AF_INET6:
514 		if (ip6 != NULL && pr->local_ip6 != NULL)
515 			ip6->sin6_addr = pr->local_ip6->sin6_addr;
516 		return (struct sockaddr *)pr->local_ip6;
517 	}
518 
519 	/* NOTREACHED */
520 	return NULL;
521 }
522 
523 /* Check if the IP is among ours, if it is return 1, else 0 */
524 int
525 jailed_ip(struct prison *pr, struct sockaddr *ip)
526 {
527 	struct jail_ip_storage *jis;
528 	struct sockaddr_in *jip4, *ip4;
529 	struct sockaddr_in6 *jip6, *ip6;
530 
531 	if (pr == NULL)
532 		return(0);
533 	ip4 = (struct sockaddr_in *)ip;
534 	ip6 = (struct sockaddr_in6 *)ip;
535 
536 	lockmgr(&jail_lock, LK_EXCLUSIVE);
537 	SLIST_FOREACH(jis, &pr->pr_ips, entries) {
538 		switch (ip->sa_family) {
539 		case AF_INET:
540 			jip4 = (struct sockaddr_in *) &jis->ip;
541 			if (jip4->sin_family == AF_INET &&
542 			    ip4->sin_addr.s_addr == jip4->sin_addr.s_addr) {
543 				lockmgr(&jail_lock, LK_RELEASE);
544 				return(1);
545 			}
546 			break;
547 		case AF_INET6:
548 			jip6 = (struct sockaddr_in6 *) &jis->ip;
549 			if (jip6->sin6_family == AF_INET6 &&
550 			    IN6_ARE_ADDR_EQUAL(&ip6->sin6_addr,
551 				&jip6->sin6_addr)) {
552 				lockmgr(&jail_lock, LK_RELEASE);
553 				return(1);
554 			}
555 			break;
556 		}
557 	}
558 	lockmgr(&jail_lock, LK_RELEASE);
559 	/* Ip not in list */
560 	return(0);
561 }
562 
563 int
564 prison_if(struct ucred *cred, struct sockaddr *sa)
565 {
566 	struct prison *pr;
567 	struct sockaddr_in *sai = (struct sockaddr_in*) sa;
568 
569 	pr = cred->cr_prison;
570 
571 	if (((sai->sin_family != AF_INET) && (sai->sin_family != AF_INET6))
572 	    && PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_NET_UNIXIPROUTE))
573 		return(1);
574 	else if ((sai->sin_family != AF_INET) && (sai->sin_family != AF_INET6))
575 		return(0);
576 	else if (jailed_ip(pr, sa))
577 		return(0);
578 	return(1);
579 }
580 
581 /*
582  * Returns a prison instance, or NULL on failure.
583  */
584 static struct prison *
585 prison_find(int prid)
586 {
587 	struct prison *pr;
588 
589 	lockmgr(&jail_lock, LK_EXCLUSIVE);
590 	LIST_FOREACH(pr, &allprison, pr_list) {
591 		if (pr->pr_id == prid)
592 			break;
593 	}
594 	lockmgr(&jail_lock, LK_RELEASE);
595 
596 	return(pr);
597 }
598 
599 static int
600 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
601 {
602 	struct thread *td = curthread;
603 	struct jail_ip_storage *jip;
604 #ifdef INET6
605 	struct sockaddr_in6 *jsin6;
606 #endif
607 	struct sockaddr_in *jsin;
608 	struct lwp *lp;
609 	struct prison *pr;
610 	unsigned int jlssize, jlsused;
611 	int count, error;
612 	char *jls; /* Jail list */
613 	char *oip; /* Output ip */
614 	char *fullpath, *freepath;
615 
616 	jlsused = 0;
617 
618 	if (jailed(td->td_ucred))
619 		return (0);
620 	lp = td->td_lwp;
621 retry:
622 	count = prisoncount;
623 
624 	if (count == 0)
625 		return(0);
626 
627 	jlssize = (count * 1024);
628 	jls = kmalloc(jlssize + 1, M_TEMP, M_WAITOK | M_ZERO);
629 	if (count < prisoncount) {
630 		kfree(jls, M_TEMP);
631 		goto retry;
632 	}
633 	count = prisoncount;
634 
635 	lockmgr(&jail_lock, LK_EXCLUSIVE);
636 	LIST_FOREACH(pr, &allprison, pr_list) {
637 		error = cache_fullpath(lp->lwp_proc, &pr->pr_root, NULL,
638 					&fullpath, &freepath, 0);
639 		if (error)
640 			continue;
641 		if (jlsused && jlsused < jlssize)
642 			jls[jlsused++] = '\n';
643 		count = ksnprintf(jls + jlsused, (jlssize - jlsused),
644 				 "%d %s %s",
645 				 pr->pr_id, pr->pr_host, fullpath);
646 		kfree(freepath, M_TEMP);
647 		if (count < 0)
648 			goto end;
649 		jlsused += count;
650 
651 		/* Copy the IPS */
652 		SLIST_FOREACH(jip, &pr->pr_ips, entries) {
653 			char buf[INET_ADDRSTRLEN];
654 
655 			jsin = (struct sockaddr_in *)&jip->ip;
656 
657 			switch(jsin->sin_family) {
658 			case AF_INET:
659 				oip = kinet_ntoa(jsin->sin_addr, buf);
660 				break;
661 #ifdef INET6
662 			case AF_INET6:
663 				jsin6 = (struct sockaddr_in6 *)&jip->ip;
664 				oip = ip6_sprintf(&jsin6->sin6_addr);
665 				break;
666 #endif
667 			default:
668 				oip = "?family?";
669 				break;
670 			}
671 
672 			if ((jlssize - jlsused) < (strlen(oip) + 1)) {
673 				error = ERANGE;
674 				goto end;
675 			}
676 			count = ksnprintf(jls + jlsused, (jlssize - jlsused),
677 					  " %s", oip);
678 			if (count < 0)
679 				goto end;
680 			jlsused += count;
681 		}
682 	}
683 
684 	/*
685 	 * The format is:
686 	 * pr_id <SPC> hostname1 <SPC> PATH1 <SPC> IP1 <SPC> IP2\npr_id...
687 	 */
688 	error = SYSCTL_OUT(req, jls, jlsused);
689 end:
690 	lockmgr(&jail_lock, LK_RELEASE);
691 	kfree(jls, M_TEMP);
692 
693 	return(error);
694 }
695 
696 SYSCTL_OID(_jail, OID_AUTO, list, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
697 	   sysctl_jail_list, "A", "List of active jails");
698 
699 static int
700 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
701 {
702 	int error, injail;
703 
704 	injail = jailed(req->td->td_ucred);
705 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
706 
707 	return (error);
708 }
709 
710 SYSCTL_PROC(_jail, OID_AUTO, jailed,
711 	    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_NOLOCK, NULL, 0,
712 	    sysctl_jail_jailed, "I", "Process in jail?");
713 
714 /*
715  * MPSAFE
716  */
717 void
718 prison_hold(struct prison *pr)
719 {
720 	atomic_add_int(&pr->pr_ref, 1);
721 }
722 
723 /*
724  * MPALMOSTSAFE
725  */
726 void
727 prison_free(struct prison *pr)
728 {
729 	struct jail_ip_storage *jls;
730 
731 	KKASSERT(pr->pr_ref > 0);
732 	if (atomic_fetchadd_int(&pr->pr_ref, -1) != 1)
733 		return;
734 
735 	/*
736 	 * The MP lock is needed on the last ref to adjust
737 	 * the list.
738 	 */
739 	lockmgr(&jail_lock, LK_EXCLUSIVE);
740 	if (pr->pr_ref) {
741 		lockmgr(&jail_lock, LK_RELEASE);
742 		return;
743 	}
744 	LIST_REMOVE(pr, pr_list);
745 	--prisoncount;
746 
747 	/*
748 	 * Clean up
749 	 */
750 	while (!SLIST_EMPTY(&pr->pr_ips)) {
751 		jls = SLIST_FIRST(&pr->pr_ips);
752 		SLIST_REMOVE_HEAD(&pr->pr_ips, entries);
753 		kfree(jls, M_PRISON);
754 	}
755 	lockmgr(&jail_lock, LK_RELEASE);
756 
757 	if (pr->pr_linux != NULL)
758 		kfree(pr->pr_linux, M_PRISON);
759 	varsymset_clean(&pr->pr_varsymset);
760 
761 	/* Release the sysctl tree */
762 	prison_sysctl_done(pr);
763 
764 	cache_drop(&pr->pr_root);
765 	kfree(pr, M_PRISON);
766 }
767 
768 /*
769  * Check if permisson for a specific privilege is granted within jail.
770  *
771  * MPSAFE
772  */
773 int
774 prison_priv_check(struct ucred *cred, int priv)
775 {
776 	struct prison *pr = cred->cr_prison;
777 
778 	if (!jailed(cred))
779 		return (0);
780 
781 	switch (priv) {
782 	case PRIV_CRED_SETUID:
783 	case PRIV_CRED_SETEUID:
784 	case PRIV_CRED_SETGID:
785 	case PRIV_CRED_SETEGID:
786 	case PRIV_CRED_SETGROUPS:
787 	case PRIV_CRED_SETREUID:
788 	case PRIV_CRED_SETREGID:
789 	case PRIV_CRED_SETRESUID:
790 	case PRIV_CRED_SETRESGID:
791 
792 	case PRIV_VFS_SYSFLAGS:
793 	case PRIV_VFS_CHOWN:
794 	case PRIV_VFS_CHMOD:
795 	case PRIV_VFS_CHROOT:
796 	case PRIV_VFS_LINK:
797 	case PRIV_VFS_CHFLAGS_DEV:
798 	case PRIV_VFS_REVOKE:
799 	case PRIV_VFS_MKNOD_BAD:
800 	case PRIV_VFS_MKNOD_WHT:
801 	case PRIV_VFS_MKNOD_DIR:
802 		return (0);
803 
804 	case PRIV_VFS_MOUNT_NULLFS:
805 		if (PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_VFS_MOUNT_NULLFS))
806 			return (0);
807 		else
808 			return (EPERM);
809 	case PRIV_VFS_MOUNT_DEVFS:
810 		return (EPERM);
811 	case PRIV_VFS_MOUNT_TMPFS:
812 		if (PRISON_CAP_ISSET(pr->pr_caps, PRISON_CAP_VFS_MOUNT_TMPFS))
813 			return (0);
814 		else
815 			return (EPERM);
816 
817 	case PRIV_VFS_SETATTR:
818 	case PRIV_VFS_SETGID:
819 
820 	case PRIV_PROC_SETRLIMIT:
821 	case PRIV_PROC_SETLOGIN:
822 
823 	case PRIV_SYSCTL_WRITEJAIL:
824 
825 	case PRIV_VARSYM_SYS:
826 
827 	case PRIV_SETHOSTNAME:
828 
829 	case PRIV_PROC_TRESPASS:
830 
831 		return (0);
832 
833 	case PRIV_UFS_QUOTAON:
834 	case PRIV_UFS_QUOTAOFF:
835 	case PRIV_VFS_SETQUOTA:
836 	case PRIV_UFS_SETUSE:
837 	case PRIV_VFS_GETQUOTA:
838 		return (0);
839 
840 
841 	case PRIV_DEBUG_UNPRIV:
842 		return (0);
843 
844 
845 		/*
846 		 * Allow jailed root to bind reserved ports.
847 		 */
848 	case PRIV_NETINET_RESERVEDPORT:
849 		return (0);
850 
851 
852 		/*
853 		 * Conditionally allow creating raw sockets in jail.
854 		 */
855 	case PRIV_NETINET_RAW:
856 		if (PRISON_CAP_ISSET(pr->pr_caps,
857 			PRISON_CAP_NET_RAW_SOCKETS))
858 			return (0);
859 		else
860 			return (EPERM);
861 
862 	case PRIV_HAMMER_IOCTL:
863 		return (0);
864 
865 	default:
866 
867 		return (EPERM);
868 	}
869 }
870 
871 
872 /*
873  * Create a per-jail sysctl tree to control the prison
874  */
875 int
876 prison_sysctl_create(struct prison *pr)
877 {
878 	char id_str[7];
879 
880 	ksnprintf(id_str, 6, "%d", pr->pr_id);
881 
882 	pr->pr_sysctl_ctx = (struct sysctl_ctx_list *) kmalloc(
883 		sizeof(struct sysctl_ctx_list), M_PRISON, M_WAITOK | M_ZERO);
884 
885 	sysctl_ctx_init(pr->pr_sysctl_ctx);
886 
887 	/* Main jail node */
888 	pr->pr_sysctl_tree = SYSCTL_ADD_NODE(pr->pr_sysctl_ctx,
889 	    SYSCTL_STATIC_CHILDREN(_jail),
890 	    OID_AUTO, id_str, CTLFLAG_RD, 0,
891 	    "Jail specific settings");
892 
893 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
894 	    OID_AUTO, "sys_set_hostname", CTLFLAG_RW,
895 	    &pr->pr_caps, 0, PRISON_CAP_SYS_SET_HOSTNAME,
896 	    "Processes in jail can set their hostnames");
897 
898 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
899 	    OID_AUTO, "sys_sysvipc", CTLFLAG_RW,
900 	    &pr->pr_caps, 0, PRISON_CAP_SYS_SYSVIPC,
901 	    "Processes in jail can use System V IPC primitives");
902 
903 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
904 	    OID_AUTO, "net_unixiproute", CTLFLAG_RW,
905 	    &pr->pr_caps, 0, PRISON_CAP_NET_UNIXIPROUTE,
906 	    "Processes in jail are limited to creating UNIX/IPv[46]/route sockets only");
907 
908 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
909 	    OID_AUTO, "net_raw_sockets", CTLFLAG_RW,
910 	    &pr->pr_caps, 0, PRISON_CAP_NET_RAW_SOCKETS,
911 	    "Process in jail can create raw sockets");
912 
913 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
914 	    OID_AUTO, "vfs_chflags", CTLFLAG_RW,
915 	    &pr->pr_caps, 0, PRISON_CAP_VFS_CHFLAGS,
916 	    "Processes in jail can alter system file flags");
917 
918 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
919 	    OID_AUTO, "vfs_mount_nullfs", CTLFLAG_RW,
920 	    &pr->pr_caps, 0, PRISON_CAP_VFS_MOUNT_NULLFS,
921 	    "Processes in jail can mount nullfs(5) filesystems");
922 
923 	SYSCTL_ADD_BIT64(pr->pr_sysctl_ctx, SYSCTL_CHILDREN(pr->pr_sysctl_tree),
924 	    OID_AUTO, "vfs_mount_tmpfs", CTLFLAG_RW,
925 	    &pr->pr_caps, 0, PRISON_CAP_VFS_MOUNT_TMPFS,
926 	    "Processes in jail can mount tmpfs(5) filesystems");
927 
928 	return 0;
929 }
930 
931 int
932 prison_sysctl_done(struct prison *pr)
933 {
934 	if (pr->pr_sysctl_tree) {
935 		sysctl_ctx_free(pr->pr_sysctl_ctx);
936 		kfree(pr->pr_sysctl_ctx, M_PRISON);
937 		pr->pr_sysctl_tree = NULL;
938 	}
939 
940 	return 0;
941 }
942