xref: /dragonfly/sys/kern/kern_jail.c (revision a8ca8ac6)
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  * $DragonFly: src/sys/kern/kern_jail.c,v 1.19 2008/05/17 18:20:33 dillon Exp $
40  */
41 
42 #include "opt_inet6.h"
43 
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/systm.h>
48 #include <sys/errno.h>
49 #include <sys/sysproto.h>
50 #include <sys/malloc.h>
51 #include <sys/nlookup.h>
52 #include <sys/namecache.h>
53 #include <sys/proc.h>
54 #include <sys/priv.h>
55 #include <sys/jail.h>
56 #include <sys/socket.h>
57 #include <sys/sysctl.h>
58 #include <sys/kern_syscall.h>
59 #include <net/if.h>
60 #include <netinet/in.h>
61 #include <netinet6/in6_var.h>
62 
63 #include <sys/mplock2.h>
64 
65 static struct prison	*prison_find(int);
66 static void		prison_ipcache_init(struct prison *);
67 
68 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
69 
70 SYSCTL_NODE(, OID_AUTO, jail, CTLFLAG_RW, 0,
71     "Jail rules");
72 
73 int	jail_set_hostname_allowed = 1;
74 SYSCTL_INT(_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
75     &jail_set_hostname_allowed, 0,
76     "Processes in jail can set their hostnames");
77 
78 int	jail_socket_unixiproute_only = 1;
79 SYSCTL_INT(_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
80     &jail_socket_unixiproute_only, 0,
81     "Processes in jail are limited to creating UNIX/IPv[46]/route sockets only");
82 
83 int	jail_sysvipc_allowed = 0;
84 SYSCTL_INT(_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
85     &jail_sysvipc_allowed, 0,
86     "Processes in jail can use System V IPC primitives");
87 
88 int    jail_chflags_allowed = 0;
89 SYSCTL_INT(_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
90     &jail_chflags_allowed, 0,
91     "Process in jail can set chflags(1)");
92 
93 int    jail_allow_raw_sockets = 0;
94 SYSCTL_INT(_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
95     &jail_allow_raw_sockets, 0,
96     "Process in jail can create raw sockets");
97 
98 int	lastprid = 0;
99 int	prisoncount = 0;
100 
101 LIST_HEAD(prisonlist, prison);
102 struct	prisonlist allprison = LIST_HEAD_INITIALIZER(&allprison);
103 
104 static int
105 kern_jail_attach(int jid)
106 {
107 	struct proc *p = curthread->td_proc;
108 	struct prison *pr;
109 	int error;
110 
111 	pr = prison_find(jid);
112 	if (pr == NULL)
113 		return(EINVAL);
114 
115 	error = kern_chroot(&pr->pr_root);
116 	if (error)
117 		return(error);
118 
119 	prison_hold(pr);
120 	cratom(&p->p_ucred);
121 	p->p_ucred->cr_prison = pr;
122 	p->p_flag |= P_JAILED;
123 
124 	return(0);
125 }
126 
127 static int
128 assign_prison_id(struct prison *pr)
129 {
130 	int tryprid;
131 	struct prison *tpr;
132 
133 	tryprid = lastprid + 1;
134 	if (tryprid == JAIL_MAX)
135 		tryprid = 1;
136 next:
137 	LIST_FOREACH(tpr, &allprison, pr_list) {
138 		if (tpr->pr_id != tryprid)
139 			continue;
140 		tryprid++;
141 		if (tryprid == JAIL_MAX) {
142 			return (ERANGE);
143 		}
144 		goto next;
145 	}
146 	pr->pr_id = lastprid = tryprid;
147 
148 	return (0);
149 }
150 
151 static int
152 kern_jail(struct prison *pr, struct jail *j)
153 {
154 	int error;
155 	struct nlookupdata nd;
156 
157 	error = nlookup_init(&nd, j->path, UIO_USERSPACE, NLC_FOLLOW);
158 	if (error) {
159 		nlookup_done(&nd);
160 		return (error);
161 	}
162 	error = nlookup(&nd);
163 	if (error) {
164 		nlookup_done(&nd);
165 		return (error);
166 	}
167 	cache_copy(&nd.nl_nch, &pr->pr_root);
168 
169 	varsymset_init(&pr->pr_varsymset, NULL);
170 	prison_ipcache_init(pr);
171 
172 	error = assign_prison_id(pr);
173 	if (error) {
174 		varsymset_clean(&pr->pr_varsymset);
175 		nlookup_done(&nd);
176 		return (error);
177 	}
178 
179 	LIST_INSERT_HEAD(&allprison, pr, pr_list);
180 	++prisoncount;
181 
182 	error = kern_jail_attach(pr->pr_id);
183 	if (error) {
184 		LIST_REMOVE(pr, pr_list);
185 		--prisoncount;
186 		varsymset_clean(&pr->pr_varsymset);
187 	}
188 	nlookup_done(&nd);
189 	return (error);
190 }
191 
192 /*
193  * jail()
194  *
195  * jail_args(syscallarg(struct jail *) jail)
196  *
197  * MPALMOSTSAFE
198  */
199 int
200 sys_jail(struct jail_args *uap)
201 {
202 	struct thread *td = curthread;
203 	struct prison *pr;
204 	struct jail_ip_storage *jip;
205 	struct jail j;
206 	int error;
207 	uint32_t jversion;
208 
209 	uap->sysmsg_result = -1;
210 
211 	error = priv_check(td, PRIV_JAIL_CREATE);
212 	if (error)
213 		return (error);
214 
215 	error = copyin(uap->jail, &jversion, sizeof(jversion));
216 	if (error)
217 		return (error);
218 
219 	pr = kmalloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
220 	SLIST_INIT(&pr->pr_ips);
221 	get_mplock();
222 
223 	switch (jversion) {
224 	case 0:
225 		/* Single IPv4 jails. */
226 		{
227 		struct jail_v0 jv0;
228 		struct sockaddr_in ip4addr;
229 
230 		error = copyin(uap->jail, &jv0, sizeof(jv0));
231 		if (error)
232 			goto out;
233 
234 		j.path = jv0.path;
235 		j.hostname = jv0.hostname;
236 
237 		jip = kmalloc(sizeof(*jip),  M_PRISON, M_WAITOK | M_ZERO);
238 		ip4addr.sin_family = AF_INET;
239 		ip4addr.sin_addr.s_addr = htonl(jv0.ip_number);
240 		memcpy(&jip->ip, &ip4addr, sizeof(ip4addr));
241 		SLIST_INSERT_HEAD(&pr->pr_ips, jip, entries);
242 		break;
243 		}
244 
245 	case 1:
246 		/*
247 		 * DragonFly multi noIP/IPv4/IPv6 jails
248 		 *
249 		 * NOTE: This version is unsupported by FreeBSD
250 		 * (which uses version 2 instead).
251 		 */
252 
253 		error = copyin(uap->jail, &j, sizeof(j));
254 		if (error)
255 			goto out;
256 
257 		for (int i = 0; i < j.n_ips; i++) {
258 			jip = kmalloc(sizeof(*jip), M_PRISON,
259 				      M_WAITOK | M_ZERO);
260 			SLIST_INSERT_HEAD(&pr->pr_ips, jip, entries);
261 			error = copyin(&j.ips[i], &jip->ip,
262 					sizeof(struct sockaddr_storage));
263 			if (error)
264 				goto out;
265 		}
266 		break;
267 	default:
268 		error = EINVAL;
269 		goto out;
270 	}
271 
272 	error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
273 	if (error)
274 		goto out;
275 
276 	error = kern_jail(pr, &j);
277 	if (error)
278 		goto out;
279 
280 	uap->sysmsg_result = pr->pr_id;
281 	rel_mplock();
282 	return (0);
283 
284 out:
285 	/* Delete all ips */
286 	while (!SLIST_EMPTY(&pr->pr_ips)) {
287 		jip = SLIST_FIRST(&pr->pr_ips);
288 		SLIST_REMOVE_HEAD(&pr->pr_ips, entries);
289 		kfree(jip, M_PRISON);
290 	}
291 	rel_mplock();
292 	kfree(pr, M_PRISON);
293 	return (error);
294 }
295 
296 /*
297  * int jail_attach(int jid);
298  *
299  * MPALMOSTSAFE
300  */
301 int
302 sys_jail_attach(struct jail_attach_args *uap)
303 {
304 	struct thread *td = curthread;
305 	int error;
306 
307 	error = priv_check(td, PRIV_JAIL_ATTACH);
308 	if (error)
309 		return(error);
310 	get_mplock();
311 	error = kern_jail_attach(uap->jid);
312 	rel_mplock();
313 	return (error);
314 }
315 
316 static void
317 prison_ipcache_init(struct prison *pr)
318 {
319 	struct jail_ip_storage *jis;
320 	struct sockaddr_in *ip4;
321 	struct sockaddr_in6 *ip6;
322 
323 	SLIST_FOREACH(jis, &pr->pr_ips, entries) {
324 		switch (jis->ip.ss_family) {
325 		case AF_INET:
326 			ip4 = (struct sockaddr_in *)&jis->ip;
327 			if ((ntohl(ip4->sin_addr.s_addr) >> IN_CLASSA_NSHIFT) ==
328 			    IN_LOOPBACKNET) {
329 				/* loopback address */
330 				if (pr->local_ip4 == NULL)
331 					pr->local_ip4 = ip4;
332 			} else {
333 				/* public address */
334 				if (pr->nonlocal_ip4 == NULL)
335 					pr->nonlocal_ip4 = ip4;
336 			}
337 			break;
338 
339 		case AF_INET6:
340 			ip6 = (struct sockaddr_in6 *)&jis->ip;
341 			if (IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr)) {
342 				/* loopback address */
343 				if (pr->local_ip6 == NULL)
344 					pr->local_ip6 = ip6;
345 			} else {
346 				/* public address */
347 				if (pr->nonlocal_ip6 == NULL)
348 					pr->nonlocal_ip6 = ip6;
349 			}
350 			break;
351 		}
352 	}
353 }
354 
355 /*
356  * Changes INADDR_LOOPBACK for a valid jail address.
357  * ip is in network byte order.
358  * Returns 1 if the ip is among jail valid ips.
359  * Returns 0 if is not among jail valid ips or
360  * if couldn't replace INADDR_LOOPBACK for a valid
361  * IP.
362  */
363 int
364 prison_replace_wildcards(struct thread *td, struct sockaddr *ip)
365 {
366 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
367 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
368 	struct prison *pr;
369 
370 	if (td->td_proc == NULL || td->td_ucred == NULL)
371 		return (1);
372 	if ((pr = td->td_ucred->cr_prison) == NULL)
373 		return (1);
374 
375 	if ((ip->sa_family == AF_INET &&
376 	    ip4->sin_addr.s_addr == htonl(INADDR_ANY)) ||
377 	    (ip->sa_family == AF_INET6 &&
378 	    IN6_IS_ADDR_UNSPECIFIED(&ip6->sin6_addr)))
379 		return (1);
380 	if ((ip->sa_family == AF_INET &&
381 	    ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) ||
382 	    (ip->sa_family == AF_INET6 &&
383 	    IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr))) {
384 		if (!prison_get_local(pr, ip->sa_family, ip) &&
385 		    !prison_get_nonlocal(pr, ip->sa_family, ip))
386 			return(0);
387 		else
388 			return(1);
389 	}
390 	if (jailed_ip(pr, ip))
391 		return(1);
392 	return(0);
393 }
394 
395 int
396 prison_remote_ip(struct thread *td, struct sockaddr *ip)
397 {
398 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
399 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
400 	struct prison *pr;
401 
402 	if (td == NULL || td->td_proc == NULL || td->td_ucred == NULL)
403 		return(1);
404 	if ((pr = td->td_ucred->cr_prison) == NULL)
405 		return(1);
406 	if ((ip->sa_family == AF_INET &&
407 	    ip4->sin_addr.s_addr == htonl(INADDR_LOOPBACK)) ||
408 	    (ip->sa_family == AF_INET6 &&
409 	    IN6_IS_ADDR_LOOPBACK(&ip6->sin6_addr))) {
410 		if (!prison_get_local(pr, ip->sa_family, ip) &&
411 		    !prison_get_nonlocal(pr, ip->sa_family, ip))
412 			return(0);
413 		else
414 			return(1);
415 	}
416 	return(1);
417 }
418 
419 /*
420  * Prison get non loopback ip:
421  * - af is the address family of the ip we want (AF_INET|AF_INET6).
422  * - If ip != NULL, put the first IP address that is not a loopback address
423  *   into *ip.
424  *
425  * ip is in network by order and we don't touch it unless we find a valid ip.
426  * No matter if ip == NULL or not, we return either a valid struct sockaddr *,
427  * or NULL.  This struct may not be modified.
428  */
429 struct sockaddr *
430 prison_get_nonlocal(struct prison *pr, sa_family_t af, struct sockaddr *ip)
431 {
432 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
433 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
434 
435 	/* Check if it is cached */
436 	switch(af) {
437 	case AF_INET:
438 		if (ip4 != NULL && pr->nonlocal_ip4 != NULL)
439 			ip4->sin_addr.s_addr = pr->nonlocal_ip4->sin_addr.s_addr;
440 		return (struct sockaddr *)pr->nonlocal_ip4;
441 
442 	case AF_INET6:
443 		if (ip6 != NULL && pr->nonlocal_ip6 != NULL)
444 			ip6->sin6_addr = pr->nonlocal_ip6->sin6_addr;
445 		return (struct sockaddr *)pr->nonlocal_ip6;
446 	}
447 
448 	/* NOTREACHED */
449 	return NULL;
450 }
451 
452 /*
453  * Prison get loopback ip.
454  * - af is the address family of the ip we want (AF_INET|AF_INET6).
455  * - If ip != NULL, put the first IP address that is not a loopback address
456  *   into *ip.
457  *
458  * ip is in network by order and we don't touch it unless we find a valid ip.
459  * No matter if ip == NULL or not, we return either a valid struct sockaddr *,
460  * or NULL.  This struct may not be modified.
461  */
462 struct sockaddr *
463 prison_get_local(struct prison *pr, sa_family_t af, struct sockaddr *ip)
464 {
465 	struct sockaddr_in *ip4 = (struct sockaddr_in *)ip;
466 	struct sockaddr_in6 *ip6 = (struct sockaddr_in6 *)ip;
467 
468 	/* Check if it is cached */
469 	switch(af) {
470 	case AF_INET:
471 		if (ip4 != NULL && pr->local_ip4 != NULL)
472 			ip4->sin_addr.s_addr = pr->local_ip4->sin_addr.s_addr;
473 		return (struct sockaddr *)pr->local_ip4;
474 
475 	case AF_INET6:
476 		if (ip6 != NULL && pr->local_ip6 != NULL)
477 			ip6->sin6_addr = pr->local_ip6->sin6_addr;
478 		return (struct sockaddr *)pr->local_ip6;
479 	}
480 
481 	/* NOTREACHED */
482 	return NULL;
483 }
484 
485 /* Check if the IP is among ours, if it is return 1, else 0 */
486 int
487 jailed_ip(struct prison *pr, struct sockaddr *ip)
488 {
489 	struct jail_ip_storage *jis;
490 	struct sockaddr_in *jip4, *ip4;
491 	struct sockaddr_in6 *jip6, *ip6;
492 
493 	if (pr == NULL)
494 		return(0);
495 	ip4 = (struct sockaddr_in *)ip;
496 	ip6 = (struct sockaddr_in6 *)ip;
497 	SLIST_FOREACH(jis, &pr->pr_ips, entries) {
498 		switch (ip->sa_family) {
499 		case AF_INET:
500 			jip4 = (struct sockaddr_in *) &jis->ip;
501 			if (jip4->sin_family == AF_INET &&
502 			    ip4->sin_addr.s_addr == jip4->sin_addr.s_addr)
503 				return(1);
504 			break;
505 		case AF_INET6:
506 			jip6 = (struct sockaddr_in6 *) &jis->ip;
507 			if (jip6->sin6_family == AF_INET6 &&
508 			    IN6_ARE_ADDR_EQUAL(&ip6->sin6_addr,
509 					       &jip6->sin6_addr))
510 				return(1);
511 			break;
512 		}
513 	}
514 	/* Ip not in list */
515 	return(0);
516 }
517 
518 int
519 prison_if(struct ucred *cred, struct sockaddr *sa)
520 {
521 	struct prison *pr;
522 	struct sockaddr_in *sai = (struct sockaddr_in*) sa;
523 
524 	pr = cred->cr_prison;
525 
526 	if (((sai->sin_family != AF_INET) && (sai->sin_family != AF_INET6))
527 	    && jail_socket_unixiproute_only)
528 		return(1);
529 	else if ((sai->sin_family != AF_INET) && (sai->sin_family != AF_INET6))
530 		return(0);
531 	else if (jailed_ip(pr, sa))
532 		return(0);
533 	return(1);
534 }
535 
536 /*
537  * Returns a prison instance, or NULL on failure.
538  */
539 static struct prison *
540 prison_find(int prid)
541 {
542 	struct prison *pr;
543 
544 	LIST_FOREACH(pr, &allprison, pr_list) {
545 		if (pr->pr_id == prid)
546 			break;
547 	}
548 	return(pr);
549 }
550 
551 static int
552 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
553 {
554 	struct thread *td = curthread;
555 	struct jail_ip_storage *jip;
556 #ifdef INET6
557 	struct sockaddr_in6 *jsin6;
558 #endif
559 	struct sockaddr_in *jsin;
560 	struct lwp *lp;
561 	struct prison *pr;
562 	unsigned int jlssize, jlsused;
563 	int count, error;
564 	char *jls; /* Jail list */
565 	char *oip; /* Output ip */
566 	char *fullpath, *freepath;
567 
568 	jlsused = 0;
569 
570 	if (jailed(td->td_ucred))
571 		return (0);
572 	lp = td->td_lwp;
573 retry:
574 	count = prisoncount;
575 
576 	if (count == 0)
577 		return(0);
578 
579 	jlssize = (count * 1024);
580 	jls = kmalloc(jlssize + 1, M_TEMP, M_WAITOK | M_ZERO);
581 	if (count < prisoncount) {
582 		kfree(jls, M_TEMP);
583 		goto retry;
584 	}
585 	count = prisoncount;
586 
587 	LIST_FOREACH(pr, &allprison, pr_list) {
588 		error = cache_fullpath(lp->lwp_proc, &pr->pr_root,
589 					&fullpath, &freepath, 0);
590 		if (error)
591 			continue;
592 		if (jlsused && jlsused < jlssize)
593 			jls[jlsused++] = '\n';
594 		count = ksnprintf(jls + jlsused, (jlssize - jlsused),
595 				 "%d %s %s",
596 				 pr->pr_id, pr->pr_host, fullpath);
597 		kfree(freepath, M_TEMP);
598 		if (count < 0)
599 			goto end;
600 		jlsused += count;
601 
602 		/* Copy the IPS */
603 		SLIST_FOREACH(jip, &pr->pr_ips, entries) {
604 			jsin = (struct sockaddr_in *)&jip->ip;
605 
606 			switch(jsin->sin_family) {
607 			case AF_INET:
608 				oip = inet_ntoa(jsin->sin_addr);
609 				break;
610 #ifdef INET6
611 			case AF_INET6:
612 				jsin6 = (struct sockaddr_in6 *)&jip->ip;
613 				oip = ip6_sprintf(&jsin6->sin6_addr);
614 				break;
615 #endif
616 			default:
617 				oip = "?family?";
618 				break;
619 			}
620 
621 			if ((jlssize - jlsused) < (strlen(oip) + 1)) {
622 				error = ERANGE;
623 				goto end;
624 			}
625 			count = ksnprintf(jls + jlsused, (jlssize - jlsused),
626 					  " %s", oip);
627 			if (count < 0)
628 				goto end;
629 			jlsused += count;
630 		}
631 	}
632 
633 	/*
634 	 * The format is:
635 	 * pr_id <SPC> hostname1 <SPC> PATH1 <SPC> IP1 <SPC> IP2\npr_id...
636 	 */
637 	error = SYSCTL_OUT(req, jls, jlsused);
638 end:
639 	kfree(jls, M_TEMP);
640 	return(error);
641 }
642 
643 SYSCTL_OID(_jail, OID_AUTO, list, CTLTYPE_STRING | CTLFLAG_RD, NULL, 0,
644 	   sysctl_jail_list, "A", "List of active jails");
645 
646 /*
647  * MPSAFE
648  */
649 void
650 prison_hold(struct prison *pr)
651 {
652 	atomic_add_int(&pr->pr_ref, 1);
653 }
654 
655 /*
656  * MPALMOSTSAFE
657  */
658 void
659 prison_free(struct prison *pr)
660 {
661 	struct jail_ip_storage *jls;
662 
663 	KKASSERT(pr->pr_ref > 0);
664 	if (atomic_fetchadd_int(&pr->pr_ref, -1) != 1)
665 		return;
666 
667 	/*
668 	 * The MP lock is needed on the last ref to adjust
669 	 * the list.
670 	 */
671 	get_mplock();
672 	if (pr->pr_ref) {
673 		rel_mplock();
674 		return;
675 	}
676 	LIST_REMOVE(pr, pr_list);
677 	--prisoncount;
678 	rel_mplock();
679 
680 	/*
681 	 * Clean up
682 	 */
683 	while (!SLIST_EMPTY(&pr->pr_ips)) {
684 		jls = SLIST_FIRST(&pr->pr_ips);
685 		SLIST_REMOVE_HEAD(&pr->pr_ips, entries);
686 		kfree(jls, M_PRISON);
687 	}
688 
689 	if (pr->pr_linux != NULL)
690 		kfree(pr->pr_linux, M_PRISON);
691 	varsymset_clean(&pr->pr_varsymset);
692 	cache_drop(&pr->pr_root);
693 	kfree(pr, M_PRISON);
694 }
695 
696 /*
697  * Check if permisson for a specific privilege is granted within jail.
698  *
699  * MPSAFE
700  */
701 int
702 prison_priv_check(struct ucred *cred, int priv)
703 {
704 	if (!jailed(cred))
705 		return (0);
706 
707 	switch (priv) {
708 	case PRIV_CRED_SETUID:
709 	case PRIV_CRED_SETEUID:
710 	case PRIV_CRED_SETGID:
711 	case PRIV_CRED_SETEGID:
712 	case PRIV_CRED_SETGROUPS:
713 	case PRIV_CRED_SETREUID:
714 	case PRIV_CRED_SETREGID:
715 	case PRIV_CRED_SETRESUID:
716 	case PRIV_CRED_SETRESGID:
717 
718 	case PRIV_VFS_SYSFLAGS:
719 	case PRIV_VFS_CHOWN:
720 	case PRIV_VFS_CHMOD:
721 	case PRIV_VFS_CHROOT:
722 	case PRIV_VFS_LINK:
723 	case PRIV_VFS_CHFLAGS_DEV:
724 	case PRIV_VFS_REVOKE:
725 	case PRIV_VFS_MKNOD_BAD:
726 	case PRIV_VFS_MKNOD_WHT:
727 	case PRIV_VFS_MKNOD_DIR:
728 	case PRIV_VFS_SETATTR:
729 	case PRIV_VFS_SETGID:
730 
731 	case PRIV_PROC_SETRLIMIT:
732 	case PRIV_PROC_SETLOGIN:
733 
734 	case PRIV_SYSCTL_WRITEJAIL:
735 
736 	case PRIV_VARSYM_SYS:
737 
738 	case PRIV_SETHOSTNAME:
739 
740 	case PRIV_PROC_TRESPASS:
741 
742 		return (0);
743 
744 	case PRIV_UFS_QUOTAON:
745 	case PRIV_UFS_QUOTAOFF:
746 	case PRIV_VFS_SETQUOTA:
747 	case PRIV_UFS_SETUSE:
748 	case PRIV_VFS_GETQUOTA:
749 		return (0);
750 
751 
752 	case PRIV_DEBUG_UNPRIV:
753 		return (0);
754 
755 
756 		/*
757 		 * Allow jailed root to bind reserved ports.
758 		 */
759 	case PRIV_NETINET_RESERVEDPORT:
760 		return (0);
761 
762 
763 		/*
764 		 * Conditionally allow creating raw sockets in jail.
765 		 */
766 	case PRIV_NETINET_RAW:
767 		if (jail_allow_raw_sockets)
768 			return (0);
769 		else
770 			return (EPERM);
771 
772 	case PRIV_HAMMER_IOCTL:
773 		return (0);
774 
775 	default:
776 
777 		return (EPERM);
778 	}
779 }
780