xref: /freebsd/sys/kern/kern_jail.c (revision 1ba4a712dde6e6c613fc411a96958b4ade67de4c)
19454b2d8SWarner Losh /*-
207901f22SPoul-Henning Kamp  * ----------------------------------------------------------------------------
307901f22SPoul-Henning Kamp  * "THE BEER-WARE LICENSE" (Revision 42):
407901f22SPoul-Henning Kamp  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
507901f22SPoul-Henning Kamp  * can do whatever you want with this stuff. If we meet some day, and you think
607901f22SPoul-Henning Kamp  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
707901f22SPoul-Henning Kamp  * ----------------------------------------------------------------------------
807901f22SPoul-Henning Kamp  */
975c13541SPoul-Henning Kamp 
10677b542eSDavid E. O'Brien #include <sys/cdefs.h>
11677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
12677b542eSDavid E. O'Brien 
1346e3b1cbSPawel Jakub Dawidek #include "opt_mac.h"
1446e3b1cbSPawel Jakub Dawidek 
1575c13541SPoul-Henning Kamp #include <sys/param.h>
1675c13541SPoul-Henning Kamp #include <sys/types.h>
1775c13541SPoul-Henning Kamp #include <sys/kernel.h>
1875c13541SPoul-Henning Kamp #include <sys/systm.h>
1975c13541SPoul-Henning Kamp #include <sys/errno.h>
2075c13541SPoul-Henning Kamp #include <sys/sysproto.h>
2175c13541SPoul-Henning Kamp #include <sys/malloc.h>
22800c9408SRobert Watson #include <sys/priv.h>
2375c13541SPoul-Henning Kamp #include <sys/proc.h>
24b3059e09SRobert Watson #include <sys/taskqueue.h>
2557b4252eSKonstantin Belousov #include <sys/fcntl.h>
2675c13541SPoul-Henning Kamp #include <sys/jail.h>
2701137630SRobert Watson #include <sys/lock.h>
2801137630SRobert Watson #include <sys/mutex.h>
29dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
30fd7a8150SMike Barcroft #include <sys/namei.h>
31820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
32fd7a8150SMike Barcroft #include <sys/queue.h>
3375c13541SPoul-Henning Kamp #include <sys/socket.h>
34fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
3583f1e257SRobert Watson #include <sys/sysctl.h>
36fd7a8150SMike Barcroft #include <sys/vnode.h>
37603724d3SBjoern A. Zeeb #include <sys/vimage.h>
381ba4a712SPawel Jakub Dawidek #include <sys/osd.h>
3975c13541SPoul-Henning Kamp #include <net/if.h>
4075c13541SPoul-Henning Kamp #include <netinet/in.h>
4175c13541SPoul-Henning Kamp 
42aed55708SRobert Watson #include <security/mac/mac_framework.h>
43aed55708SRobert Watson 
4475c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
4575c13541SPoul-Henning Kamp 
46d0615c64SAndrew R. Reiter SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4783f1e257SRobert Watson     "Jail rules");
4883f1e257SRobert Watson 
4983f1e257SRobert Watson int	jail_set_hostname_allowed = 1;
50d0615c64SAndrew R. Reiter SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
5183f1e257SRobert Watson     &jail_set_hostname_allowed, 0,
5283f1e257SRobert Watson     "Processes in jail can set their hostnames");
5383f1e257SRobert Watson 
547cadc266SRobert Watson int	jail_socket_unixiproute_only = 1;
55d0615c64SAndrew R. Reiter SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
567cadc266SRobert Watson     &jail_socket_unixiproute_only, 0,
577cadc266SRobert Watson     "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
587cadc266SRobert Watson 
59cb1f0db9SRobert Watson int	jail_sysvipc_allowed = 0;
60d0615c64SAndrew R. Reiter SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
61cb1f0db9SRobert Watson     &jail_sysvipc_allowed, 0,
62cb1f0db9SRobert Watson     "Processes in jail can use System V IPC primitives");
63cb1f0db9SRobert Watson 
64820a0de9SPawel Jakub Dawidek static int jail_enforce_statfs = 2;
65820a0de9SPawel Jakub Dawidek SYSCTL_INT(_security_jail, OID_AUTO, enforce_statfs, CTLFLAG_RW,
66820a0de9SPawel Jakub Dawidek     &jail_enforce_statfs, 0,
67820a0de9SPawel Jakub Dawidek     "Processes in jail cannot see all mounted file systems");
68f08df373SRobert Watson 
695a59cefcSBosko Milekic int	jail_allow_raw_sockets = 0;
705a59cefcSBosko Milekic SYSCTL_INT(_security_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
715a59cefcSBosko Milekic     &jail_allow_raw_sockets, 0,
725a59cefcSBosko Milekic     "Prison root can create raw sockets");
735a59cefcSBosko Milekic 
7479653046SColin Percival int	jail_chflags_allowed = 0;
7579653046SColin Percival SYSCTL_INT(_security_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
7679653046SColin Percival     &jail_chflags_allowed, 0,
7779653046SColin Percival     "Processes in jail can alter system file flags");
7879653046SColin Percival 
79f3a8d2f9SPawel Jakub Dawidek int	jail_mount_allowed = 0;
80f3a8d2f9SPawel Jakub Dawidek SYSCTL_INT(_security_jail, OID_AUTO, mount_allowed, CTLFLAG_RW,
81f3a8d2f9SPawel Jakub Dawidek     &jail_mount_allowed, 0,
82f3a8d2f9SPawel Jakub Dawidek     "Processes in jail can mount/unmount jail-friendly file systems");
83f3a8d2f9SPawel Jakub Dawidek 
842110d913SXin LI /* allprison, lastprid, and prisoncount are protected by allprison_lock. */
852110d913SXin LI struct	prisonlist allprison;
86dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
872110d913SXin LI int	lastprid = 0;
88fd7a8150SMike Barcroft int	prisoncount = 0;
89fd7a8150SMike Barcroft 
90fd7a8150SMike Barcroft static void		 init_prison(void *);
91b3059e09SRobert Watson static void		 prison_complete(void *context, int pending);
92fd7a8150SMike Barcroft static int		 sysctl_jail_list(SYSCTL_HANDLER_ARGS);
93fd7a8150SMike Barcroft 
94fd7a8150SMike Barcroft static void
95fd7a8150SMike Barcroft init_prison(void *data __unused)
96fd7a8150SMike Barcroft {
97fd7a8150SMike Barcroft 
982110d913SXin LI 	sx_init(&allprison_lock, "allprison");
992110d913SXin LI 	LIST_INIT(&allprison);
100fd7a8150SMike Barcroft }
101fd7a8150SMike Barcroft 
102fd7a8150SMike Barcroft SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
103fd7a8150SMike Barcroft 
104116734c4SMatthew Dillon /*
1059ddb7954SMike Barcroft  * struct jail_args {
1069ddb7954SMike Barcroft  *	struct jail *jail;
1079ddb7954SMike Barcroft  * };
108116734c4SMatthew Dillon  */
10975c13541SPoul-Henning Kamp int
1109ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap)
11175c13541SPoul-Henning Kamp {
112fd7a8150SMike Barcroft 	struct nameidata nd;
1132110d913SXin LI 	struct prison *pr, *tpr;
11475c13541SPoul-Henning Kamp 	struct jail j;
115fd7a8150SMike Barcroft 	struct jail_attach_args jaa;
1162110d913SXin LI 	int vfslocked, error, tryprid;
11775c13541SPoul-Henning Kamp 
1189ddb7954SMike Barcroft 	error = copyin(uap->jail, &j, sizeof(j));
11975c13541SPoul-Henning Kamp 	if (error)
120a2f2b3afSJohn Baldwin 		return (error);
121a2f2b3afSJohn Baldwin 	if (j.version != 0)
122a2f2b3afSJohn Baldwin 		return (EINVAL);
123a2f2b3afSJohn Baldwin 
1241ede983cSDag-Erling Smørgrav 	pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1256008862bSJohn Baldwin 	mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
126fd7a8150SMike Barcroft 	pr->pr_ref = 1;
1279ddb7954SMike Barcroft 	error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0);
128fd7a8150SMike Barcroft 	if (error)
129fd7a8150SMike Barcroft 		goto e_killmtx;
130453f7d53SChristian S.J. Peron 	NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE,
131453f7d53SChristian S.J. Peron 	    pr->pr_path, td);
132fd7a8150SMike Barcroft 	error = namei(&nd);
133453f7d53SChristian S.J. Peron 	if (error)
134fd7a8150SMike Barcroft 		goto e_killmtx;
135453f7d53SChristian S.J. Peron 	vfslocked = NDHASGIANT(&nd);
136fd7a8150SMike Barcroft 	pr->pr_root = nd.ni_vp;
13722db15c0SAttilio Rao 	VOP_UNLOCK(nd.ni_vp, 0);
138fd7a8150SMike Barcroft 	NDFREE(&nd, NDF_ONLY_PNBUF);
139453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
1409ddb7954SMike Barcroft 	error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
14175c13541SPoul-Henning Kamp 	if (error)
142fd7a8150SMike Barcroft 		goto e_dropvnref;
143a2f2b3afSJohn Baldwin 	pr->pr_ip = j.ip_number;
144fd7a8150SMike Barcroft 	pr->pr_linux = NULL;
145fd7a8150SMike Barcroft 	pr->pr_securelevel = securelevel;
1461ba4a712SPawel Jakub Dawidek 	bzero(&pr->pr_osd, sizeof(pr->pr_osd));
147fd7a8150SMike Barcroft 
1482110d913SXin LI 	/* Determine next pr_id and add prison to allprison list. */
149dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
1502110d913SXin LI 	tryprid = lastprid + 1;
1512110d913SXin LI 	if (tryprid == JAIL_MAX)
1522110d913SXin LI 		tryprid = 1;
1532110d913SXin LI next:
1542110d913SXin LI 	LIST_FOREACH(tpr, &allprison, pr_list) {
1552110d913SXin LI 		if (tpr->pr_id == tryprid) {
1562110d913SXin LI 			tryprid++;
1572110d913SXin LI 			if (tryprid == JAIL_MAX) {
1582110d913SXin LI 				sx_xunlock(&allprison_lock);
1592110d913SXin LI 				error = EAGAIN;
1602110d913SXin LI 				goto e_dropvnref;
1612110d913SXin LI 			}
1622110d913SXin LI 			goto next;
1632110d913SXin LI 		}
1642110d913SXin LI 	}
1652110d913SXin LI 	pr->pr_id = jaa.jid = lastprid = tryprid;
166fd7a8150SMike Barcroft 	LIST_INSERT_HEAD(&allprison, pr, pr_list);
167fd7a8150SMike Barcroft 	prisoncount++;
1681ba4a712SPawel Jakub Dawidek 	sx_xunlock(&allprison_lock);
169fd7a8150SMike Barcroft 
170fd7a8150SMike Barcroft 	error = jail_attach(td, &jaa);
171a2f2b3afSJohn Baldwin 	if (error)
172fd7a8150SMike Barcroft 		goto e_dropprref;
173fd7a8150SMike Barcroft 	mtx_lock(&pr->pr_mtx);
174fd7a8150SMike Barcroft 	pr->pr_ref--;
175fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
176fd7a8150SMike Barcroft 	td->td_retval[0] = jaa.jid;
17775c13541SPoul-Henning Kamp 	return (0);
178fd7a8150SMike Barcroft e_dropprref:
179dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
180fd7a8150SMike Barcroft 	LIST_REMOVE(pr, pr_list);
181fd7a8150SMike Barcroft 	prisoncount--;
1821ba4a712SPawel Jakub Dawidek 	sx_xunlock(&allprison_lock);
183fd7a8150SMike Barcroft e_dropvnref:
184453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
185fd7a8150SMike Barcroft 	vrele(pr->pr_root);
186453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
187fd7a8150SMike Barcroft e_killmtx:
188894db7b0SMaxime Henrion 	mtx_destroy(&pr->pr_mtx);
1891ede983cSDag-Erling Smørgrav 	free(pr, M_PRISON);
19075c13541SPoul-Henning Kamp 	return (error);
19175c13541SPoul-Henning Kamp }
19275c13541SPoul-Henning Kamp 
193fd7a8150SMike Barcroft /*
1949ddb7954SMike Barcroft  * struct jail_attach_args {
1959ddb7954SMike Barcroft  *	int jid;
1969ddb7954SMike Barcroft  * };
197fd7a8150SMike Barcroft  */
198fd7a8150SMike Barcroft int
1999ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap)
200fd7a8150SMike Barcroft {
201fd7a8150SMike Barcroft 	struct proc *p;
202fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
203fd7a8150SMike Barcroft 	struct prison *pr;
204453f7d53SChristian S.J. Peron 	int vfslocked, error;
205fd7a8150SMike Barcroft 
20657f22bd4SJacques Vidrine 	/*
20757f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
20857f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
20957f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
21057f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
21157f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
21257f22bd4SJacques Vidrine 	 * of another.
21357f22bd4SJacques Vidrine 	 */
214800c9408SRobert Watson 	error = priv_check(td, PRIV_JAIL_ATTACH);
21557f22bd4SJacques Vidrine 	if (error)
21657f22bd4SJacques Vidrine 		return (error);
217fd7a8150SMike Barcroft 
21857f22bd4SJacques Vidrine 	p = td->td_proc;
219dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
220fd7a8150SMike Barcroft 	pr = prison_find(uap->jid);
221fd7a8150SMike Barcroft 	if (pr == NULL) {
222dc68a633SPawel Jakub Dawidek 		sx_sunlock(&allprison_lock);
223fd7a8150SMike Barcroft 		return (EINVAL);
224fd7a8150SMike Barcroft 	}
225fd7a8150SMike Barcroft 	pr->pr_ref++;
226fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
227dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
228fd7a8150SMike Barcroft 
229453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
230cb05b60aSAttilio Rao 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
231fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
232fd7a8150SMike Barcroft 		goto e_unlock;
233fd7a8150SMike Barcroft #ifdef MAC
23430d239bcSRobert Watson 	if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
235fd7a8150SMike Barcroft 		goto e_unlock;
236fd7a8150SMike Barcroft #endif
23722db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
238fd7a8150SMike Barcroft 	change_root(pr->pr_root, td);
239453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
240fd7a8150SMike Barcroft 
241fd7a8150SMike Barcroft 	newcred = crget();
242fd7a8150SMike Barcroft 	PROC_LOCK(p);
243fd7a8150SMike Barcroft 	oldcred = p->p_ucred;
244fd7a8150SMike Barcroft 	setsugid(p);
245fd7a8150SMike Barcroft 	crcopy(newcred, oldcred);
24669c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
247fd7a8150SMike Barcroft 	p->p_ucred = newcred;
248fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
249fd7a8150SMike Barcroft 	crfree(oldcred);
250fd7a8150SMike Barcroft 	return (0);
251fd7a8150SMike Barcroft e_unlock:
25222db15c0SAttilio Rao 	VOP_UNLOCK(pr->pr_root, 0);
253453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
254fd7a8150SMike Barcroft 	mtx_lock(&pr->pr_mtx);
255fd7a8150SMike Barcroft 	pr->pr_ref--;
256fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
257fd7a8150SMike Barcroft 	return (error);
258fd7a8150SMike Barcroft }
259fd7a8150SMike Barcroft 
260fd7a8150SMike Barcroft /*
261fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
262fd7a8150SMike Barcroft  */
26354b369c1SPawel Jakub Dawidek struct prison *
264fd7a8150SMike Barcroft prison_find(int prid)
265fd7a8150SMike Barcroft {
266fd7a8150SMike Barcroft 	struct prison *pr;
267fd7a8150SMike Barcroft 
268dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
269fd7a8150SMike Barcroft 	LIST_FOREACH(pr, &allprison, pr_list) {
270fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
271fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
272c2cda609SPawel Jakub Dawidek 			if (pr->pr_ref == 0) {
273c2cda609SPawel Jakub Dawidek 				mtx_unlock(&pr->pr_mtx);
274c2cda609SPawel Jakub Dawidek 				break;
275c2cda609SPawel Jakub Dawidek 			}
276fd7a8150SMike Barcroft 			return (pr);
277fd7a8150SMike Barcroft 		}
278fd7a8150SMike Barcroft 	}
279fd7a8150SMike Barcroft 	return (NULL);
280fd7a8150SMike Barcroft }
281fd7a8150SMike Barcroft 
28291421ba2SRobert Watson void
2831ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr)
28491421ba2SRobert Watson {
28591421ba2SRobert Watson 
2861ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
28791421ba2SRobert Watson 	pr->pr_ref--;
28891421ba2SRobert Watson 	if (pr->pr_ref == 0) {
28901137630SRobert Watson 		mtx_unlock(&pr->pr_mtx);
290c2cda609SPawel Jakub Dawidek 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
291c2cda609SPawel Jakub Dawidek 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
292c2cda609SPawel Jakub Dawidek 		return;
293c2cda609SPawel Jakub Dawidek 	}
294c2cda609SPawel Jakub Dawidek 	mtx_unlock(&pr->pr_mtx);
295c2cda609SPawel Jakub Dawidek }
296c2cda609SPawel Jakub Dawidek 
2971ba4a712SPawel Jakub Dawidek void
2981ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr)
2991ba4a712SPawel Jakub Dawidek {
3001ba4a712SPawel Jakub Dawidek 
3011ba4a712SPawel Jakub Dawidek 	mtx_lock(&pr->pr_mtx);
3021ba4a712SPawel Jakub Dawidek 	prison_free_locked(pr);
3031ba4a712SPawel Jakub Dawidek }
3041ba4a712SPawel Jakub Dawidek 
305c2cda609SPawel Jakub Dawidek static void
306c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending)
307c2cda609SPawel Jakub Dawidek {
308c2cda609SPawel Jakub Dawidek 	struct prison *pr;
309c2cda609SPawel Jakub Dawidek 	int vfslocked;
310c2cda609SPawel Jakub Dawidek 
311c2cda609SPawel Jakub Dawidek 	pr = (struct prison *)context;
312c2cda609SPawel Jakub Dawidek 
313c2cda609SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
314264de85eSPawel Jakub Dawidek 	LIST_REMOVE(pr, pr_list);
315fd7a8150SMike Barcroft 	prisoncount--;
3161ba4a712SPawel Jakub Dawidek 	sx_xunlock(&allprison_lock);
3171ba4a712SPawel Jakub Dawidek 
3181ba4a712SPawel Jakub Dawidek 	/* Free all OSD associated to this jail. */
3191ba4a712SPawel Jakub Dawidek 	osd_jail_exit(pr);
320b3059e09SRobert Watson 
321453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
322b3059e09SRobert Watson 	vrele(pr->pr_root);
323453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
324b3059e09SRobert Watson 
325b3059e09SRobert Watson 	mtx_destroy(&pr->pr_mtx);
326b3059e09SRobert Watson 	if (pr->pr_linux != NULL)
3271ede983cSDag-Erling Smørgrav 		free(pr->pr_linux, M_PRISON);
3281ede983cSDag-Erling Smørgrav 	free(pr, M_PRISON);
329b3059e09SRobert Watson }
330b3059e09SRobert Watson 
33191421ba2SRobert Watson void
3321ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr)
3331ba4a712SPawel Jakub Dawidek {
3341ba4a712SPawel Jakub Dawidek 
3351ba4a712SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
3361ba4a712SPawel Jakub Dawidek 	KASSERT(pr->pr_ref > 0,
3371ba4a712SPawel Jakub Dawidek 	    ("Trying to hold dead prison (id=%d).", pr->pr_id));
3381ba4a712SPawel Jakub Dawidek 	pr->pr_ref++;
3391ba4a712SPawel Jakub Dawidek }
3401ba4a712SPawel Jakub Dawidek 
3411ba4a712SPawel Jakub Dawidek void
34291421ba2SRobert Watson prison_hold(struct prison *pr)
34391421ba2SRobert Watson {
34491421ba2SRobert Watson 
34501137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
3461ba4a712SPawel Jakub Dawidek 	prison_hold_locked(pr);
34701137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
34801137630SRobert Watson }
34901137630SRobert Watson 
35001137630SRobert Watson u_int32_t
35101137630SRobert Watson prison_getip(struct ucred *cred)
35201137630SRobert Watson {
35301137630SRobert Watson 
35401137630SRobert Watson 	return (cred->cr_prison->pr_ip);
35591421ba2SRobert Watson }
35691421ba2SRobert Watson 
35775c13541SPoul-Henning Kamp int
35891421ba2SRobert Watson prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
35975c13541SPoul-Henning Kamp {
36075c13541SPoul-Henning Kamp 	u_int32_t tmp;
36175c13541SPoul-Henning Kamp 
36291421ba2SRobert Watson 	if (!jailed(cred))
36375c13541SPoul-Henning Kamp 		return (0);
36475c13541SPoul-Henning Kamp 	if (flag)
36575c13541SPoul-Henning Kamp 		tmp = *ip;
36675c13541SPoul-Henning Kamp 	else
36775c13541SPoul-Henning Kamp 		tmp = ntohl(*ip);
36875c13541SPoul-Henning Kamp 	if (tmp == INADDR_ANY) {
36975c13541SPoul-Henning Kamp 		if (flag)
37091421ba2SRobert Watson 			*ip = cred->cr_prison->pr_ip;
37175c13541SPoul-Henning Kamp 		else
37291421ba2SRobert Watson 			*ip = htonl(cred->cr_prison->pr_ip);
37375c13541SPoul-Henning Kamp 		return (0);
37475c13541SPoul-Henning Kamp 	}
375fd6aaf7fSRobert Watson 	if (tmp == INADDR_LOOPBACK) {
376fd6aaf7fSRobert Watson 		if (flag)
377fd6aaf7fSRobert Watson 			*ip = cred->cr_prison->pr_ip;
378fd6aaf7fSRobert Watson 		else
379fd6aaf7fSRobert Watson 			*ip = htonl(cred->cr_prison->pr_ip);
380fd6aaf7fSRobert Watson 		return (0);
381fd6aaf7fSRobert Watson 	}
38291421ba2SRobert Watson 	if (cred->cr_prison->pr_ip != tmp)
38375c13541SPoul-Henning Kamp 		return (1);
38475c13541SPoul-Henning Kamp 	return (0);
38575c13541SPoul-Henning Kamp }
38675c13541SPoul-Henning Kamp 
38775c13541SPoul-Henning Kamp void
38891421ba2SRobert Watson prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
38975c13541SPoul-Henning Kamp {
39075c13541SPoul-Henning Kamp 	u_int32_t tmp;
39175c13541SPoul-Henning Kamp 
39291421ba2SRobert Watson 	if (!jailed(cred))
39375c13541SPoul-Henning Kamp 		return;
39475c13541SPoul-Henning Kamp 	if (flag)
39575c13541SPoul-Henning Kamp 		tmp = *ip;
39675c13541SPoul-Henning Kamp 	else
39775c13541SPoul-Henning Kamp 		tmp = ntohl(*ip);
398fd6aaf7fSRobert Watson 	if (tmp == INADDR_LOOPBACK) {
39975c13541SPoul-Henning Kamp 		if (flag)
40091421ba2SRobert Watson 			*ip = cred->cr_prison->pr_ip;
40175c13541SPoul-Henning Kamp 		else
40291421ba2SRobert Watson 			*ip = htonl(cred->cr_prison->pr_ip);
40375c13541SPoul-Henning Kamp 		return;
40475c13541SPoul-Henning Kamp 	}
40575c13541SPoul-Henning Kamp 	return;
40675c13541SPoul-Henning Kamp }
40775c13541SPoul-Henning Kamp 
40875c13541SPoul-Henning Kamp int
40991421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
41075c13541SPoul-Henning Kamp {
4119ddb7954SMike Barcroft 	struct sockaddr_in *sai;
41275c13541SPoul-Henning Kamp 	int ok;
41375c13541SPoul-Henning Kamp 
4149ddb7954SMike Barcroft 	sai = (struct sockaddr_in *)sa;
4157cadc266SRobert Watson 	if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
4167cadc266SRobert Watson 		ok = 1;
4177cadc266SRobert Watson 	else if (sai->sin_family != AF_INET)
41875c13541SPoul-Henning Kamp 		ok = 0;
41991421ba2SRobert Watson 	else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
42075c13541SPoul-Henning Kamp 		ok = 1;
42175c13541SPoul-Henning Kamp 	else
42275c13541SPoul-Henning Kamp 		ok = 0;
42375c13541SPoul-Henning Kamp 	return (ok);
42475c13541SPoul-Henning Kamp }
42591421ba2SRobert Watson 
42691421ba2SRobert Watson /*
42791421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
42891421ba2SRobert Watson  */
42991421ba2SRobert Watson int
4309ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
43191421ba2SRobert Watson {
43291421ba2SRobert Watson 
43391421ba2SRobert Watson 	if (jailed(cred1)) {
43491421ba2SRobert Watson 		if (!jailed(cred2))
43591421ba2SRobert Watson 			return (ESRCH);
43691421ba2SRobert Watson 		if (cred2->cr_prison != cred1->cr_prison)
43791421ba2SRobert Watson 			return (ESRCH);
43891421ba2SRobert Watson 	}
43991421ba2SRobert Watson 
44091421ba2SRobert Watson 	return (0);
44191421ba2SRobert Watson }
44291421ba2SRobert Watson 
44391421ba2SRobert Watson /*
44491421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
44591421ba2SRobert Watson  */
44691421ba2SRobert Watson int
4479ddb7954SMike Barcroft jailed(struct ucred *cred)
44891421ba2SRobert Watson {
44991421ba2SRobert Watson 
45091421ba2SRobert Watson 	return (cred->cr_prison != NULL);
45191421ba2SRobert Watson }
4529484d0c0SRobert Drehmel 
4539484d0c0SRobert Drehmel /*
4549484d0c0SRobert Drehmel  * Return the correct hostname for the passed credential.
4559484d0c0SRobert Drehmel  */
456ad1ff099SRobert Drehmel void
4579ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
4589484d0c0SRobert Drehmel {
4598b615593SMarko Zec 	INIT_VPROCG(cred->cr_vimage->v_procg);
4609484d0c0SRobert Drehmel 
461ad1ff099SRobert Drehmel 	if (jailed(cred)) {
462ad1ff099SRobert Drehmel 		mtx_lock(&cred->cr_prison->pr_mtx);
463e80fb434SRobert Drehmel 		strlcpy(buf, cred->cr_prison->pr_host, size);
464ad1ff099SRobert Drehmel 		mtx_unlock(&cred->cr_prison->pr_mtx);
4654f7d1876SRobert Watson 	} else {
4664f7d1876SRobert Watson 		mtx_lock(&hostname_mtx);
467603724d3SBjoern A. Zeeb 		strlcpy(buf, V_hostname, size);
4684f7d1876SRobert Watson 		mtx_unlock(&hostname_mtx);
4694f7d1876SRobert Watson 	}
4709484d0c0SRobert Drehmel }
471fd7a8150SMike Barcroft 
472f08df373SRobert Watson /*
473820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
474820a0de9SPawel Jakub Dawidek  * status of a mount point.
475820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
476820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
477820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
478f08df373SRobert Watson  */
479f08df373SRobert Watson int
480820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
481f08df373SRobert Watson {
482820a0de9SPawel Jakub Dawidek 	struct prison *pr;
483820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
484820a0de9SPawel Jakub Dawidek 	size_t len;
485f08df373SRobert Watson 
486820a0de9SPawel Jakub Dawidek 	if (!jailed(cred) || jail_enforce_statfs == 0)
487820a0de9SPawel Jakub Dawidek 		return (0);
488820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
489820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
490820a0de9SPawel Jakub Dawidek 		return (0);
491820a0de9SPawel Jakub Dawidek 	if (jail_enforce_statfs == 2)
492820a0de9SPawel Jakub Dawidek 		return (ENOENT);
493820a0de9SPawel Jakub Dawidek 	/*
494820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
495820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
496820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
497820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
498820a0de9SPawel Jakub Dawidek 	 */
499820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
500820a0de9SPawel Jakub Dawidek 		return (0);
501820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
502820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
503820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
504820a0de9SPawel Jakub Dawidek 		return (ENOENT);
505820a0de9SPawel Jakub Dawidek 	/*
506820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
507820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
508820a0de9SPawel Jakub Dawidek 	 */
509820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
510820a0de9SPawel Jakub Dawidek 		return (ENOENT);
511f08df373SRobert Watson 	return (0);
512f08df373SRobert Watson }
513820a0de9SPawel Jakub Dawidek 
514820a0de9SPawel Jakub Dawidek void
515820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
516820a0de9SPawel Jakub Dawidek {
517820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
518820a0de9SPawel Jakub Dawidek 	struct prison *pr;
519820a0de9SPawel Jakub Dawidek 	size_t len;
520820a0de9SPawel Jakub Dawidek 
521820a0de9SPawel Jakub Dawidek 	if (!jailed(cred) || jail_enforce_statfs == 0)
522820a0de9SPawel Jakub Dawidek 		return;
523820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
524820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
525820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
526820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
527820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
528820a0de9SPawel Jakub Dawidek 		return;
529820a0de9SPawel Jakub Dawidek 	}
530820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
531820a0de9SPawel Jakub Dawidek 		/*
532820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
533820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
534820a0de9SPawel Jakub Dawidek 		 */
535820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
536820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
537820a0de9SPawel Jakub Dawidek 		return;
538820a0de9SPawel Jakub Dawidek 	}
539820a0de9SPawel Jakub Dawidek 	/*
540820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
541820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
542820a0de9SPawel Jakub Dawidek 	 */
543820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
544820a0de9SPawel Jakub Dawidek 		return;
545820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
546820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
547820a0de9SPawel Jakub Dawidek 	/*
548820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
549820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
550820a0de9SPawel Jakub Dawidek 	 */
551820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
552820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
553820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
554820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
555820a0de9SPawel Jakub Dawidek 	} else {
556820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
557820a0de9SPawel Jakub Dawidek 	}
558f08df373SRobert Watson }
559f08df373SRobert Watson 
560800c9408SRobert Watson /*
561800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
562800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
563800c9408SRobert Watson  */
564800c9408SRobert Watson int
565800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
566800c9408SRobert Watson {
567800c9408SRobert Watson 
568800c9408SRobert Watson 	if (!jailed(cred))
569800c9408SRobert Watson 		return (0);
570800c9408SRobert Watson 
571800c9408SRobert Watson 	switch (priv) {
572800c9408SRobert Watson 
573800c9408SRobert Watson 		/*
574800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
575800c9408SRobert Watson 		 */
576800c9408SRobert Watson 	case PRIV_KTRACE:
577800c9408SRobert Watson 
578c3c1b5e6SRobert Watson #if 0
579800c9408SRobert Watson 		/*
580800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
581800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
582800c9408SRobert Watson 		 * want to further refine the relationship between audit and
583800c9408SRobert Watson 		 * jail.
584800c9408SRobert Watson 		 */
585800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
586800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
587800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
588c3c1b5e6SRobert Watson #endif
589800c9408SRobert Watson 
590800c9408SRobert Watson 		/*
591800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
592800c9408SRobert Watson 		 * credentials in any way they see fit.
593800c9408SRobert Watson 		 */
594800c9408SRobert Watson 	case PRIV_CRED_SETUID:
595800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
596800c9408SRobert Watson 	case PRIV_CRED_SETGID:
597800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
598800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
599800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
600800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
601800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
602800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
603800c9408SRobert Watson 
604800c9408SRobert Watson 		/*
605800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
606800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
607800c9408SRobert Watson 		 */
608800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
609800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
610800c9408SRobert Watson 
611800c9408SRobert Watson 		/*
612800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
613800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
614800c9408SRobert Watson 		 */
615800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
616800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
617800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
618800c9408SRobert Watson 
619800c9408SRobert Watson 		/*
620800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
621800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
622800c9408SRobert Watson 		 */
623800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
624800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
625800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
626800c9408SRobert Watson 
627800c9408SRobert Watson 		/*
628800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
629800c9408SRobert Watson 		 */
630800c9408SRobert Watson 	case PRIV_IPC_READ:
631800c9408SRobert Watson 	case PRIV_IPC_WRITE:
632800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
633800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
634800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
635800c9408SRobert Watson 
636800c9408SRobert Watson 		/*
637800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
638800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
639800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
640800c9408SRobert Watson 		 */
641800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
642800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
643800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
644800c9408SRobert Watson 
645800c9408SRobert Watson 		/*
646800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
647800c9408SRobert Watson 		 * writable.
648800c9408SRobert Watson 		 */
649800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
650800c9408SRobert Watson 
651800c9408SRobert Watson 		/*
652800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
653e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
654e82d0201SRobert Watson 		 * configuration option.
655800c9408SRobert Watson 		 */
65695b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
65795b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
658800c9408SRobert Watson 
659800c9408SRobert Watson 		/*
660800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
661800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
662800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
663800c9408SRobert Watson 		 * privileges.
664800c9408SRobert Watson 		 */
665800c9408SRobert Watson 	case PRIV_VFS_READ:
666800c9408SRobert Watson 	case PRIV_VFS_WRITE:
667800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
668800c9408SRobert Watson 	case PRIV_VFS_EXEC:
669800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
670800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
671800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
672800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
673800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
674bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
675800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
676800c9408SRobert Watson 	case PRIV_VFS_LINK:
677800c9408SRobert Watson 	case PRIV_VFS_SETGID:
678e41966dcSRobert Watson 	case PRIV_VFS_STAT:
679800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
680800c9408SRobert Watson 		return (0);
681800c9408SRobert Watson 
682800c9408SRobert Watson 		/*
683800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
684800c9408SRobert Watson 		 * setting system flags.
685800c9408SRobert Watson 		 */
686800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
687800c9408SRobert Watson 		if (jail_chflags_allowed)
688800c9408SRobert Watson 			return (0);
689800c9408SRobert Watson 		else
690800c9408SRobert Watson 			return (EPERM);
691800c9408SRobert Watson 
692800c9408SRobert Watson 		/*
693f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
694f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
695f3a8d2f9SPawel Jakub Dawidek 		 */
696f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
697f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
698f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
69924b0502eSPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_OWNER:
700f3a8d2f9SPawel Jakub Dawidek 		if (jail_mount_allowed)
701f3a8d2f9SPawel Jakub Dawidek 			return (0);
702f3a8d2f9SPawel Jakub Dawidek 		else
703f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
704f3a8d2f9SPawel Jakub Dawidek 
705f3a8d2f9SPawel Jakub Dawidek 		/*
7064b084056SRobert Watson 		 * Allow jailed root to bind reserved ports and reuse in-use
7074b084056SRobert Watson 		 * ports.
708800c9408SRobert Watson 		 */
709800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
7104b084056SRobert Watson 	case PRIV_NETINET_REUSEPORT:
711800c9408SRobert Watson 		return (0);
712800c9408SRobert Watson 
713800c9408SRobert Watson 		/*
71479ba3952SBjoern A. Zeeb 		 * Allow jailed root to set certian IPv4/6 (option) headers.
71579ba3952SBjoern A. Zeeb 		 */
71679ba3952SBjoern A. Zeeb 	case PRIV_NETINET_SETHDROPTS:
71779ba3952SBjoern A. Zeeb 		return (0);
71879ba3952SBjoern A. Zeeb 
71979ba3952SBjoern A. Zeeb 		/*
720800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
721800c9408SRobert Watson 		 */
722800c9408SRobert Watson 	case PRIV_NETINET_RAW:
723800c9408SRobert Watson 		if (jail_allow_raw_sockets)
724800c9408SRobert Watson 			return (0);
725800c9408SRobert Watson 		else
726800c9408SRobert Watson 			return (EPERM);
727800c9408SRobert Watson 
728800c9408SRobert Watson 		/*
729800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
730800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
731800c9408SRobert Watson 		 * jail.
732800c9408SRobert Watson 		 */
733800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
734800c9408SRobert Watson 		return (0);
735800c9408SRobert Watson 
736800c9408SRobert Watson 	default:
737800c9408SRobert Watson 		/*
738800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
739800c9408SRobert Watson 		 * includes almost all network privileges, many system
740800c9408SRobert Watson 		 * configuration privileges.
741800c9408SRobert Watson 		 */
742800c9408SRobert Watson 		return (EPERM);
743800c9408SRobert Watson 	}
744800c9408SRobert Watson }
745800c9408SRobert Watson 
746fd7a8150SMike Barcroft static int
747fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
748fd7a8150SMike Barcroft {
749fd7a8150SMike Barcroft 	struct xprison *xp, *sxp;
750fd7a8150SMike Barcroft 	struct prison *pr;
751fd7a8150SMike Barcroft 	int count, error;
752fd7a8150SMike Barcroft 
7537f4704c0SPawel Jakub Dawidek 	if (jailed(req->td->td_ucred))
754679a1060SRobert Watson 		return (0);
755fd7a8150SMike Barcroft 
756dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
757dc68a633SPawel Jakub Dawidek 	if ((count = prisoncount) == 0) {
758dc68a633SPawel Jakub Dawidek 		sx_sunlock(&allprison_lock);
759fd7a8150SMike Barcroft 		return (0);
760dc68a633SPawel Jakub Dawidek 	}
761fd7a8150SMike Barcroft 
762fd7a8150SMike Barcroft 	sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
763fd7a8150SMike Barcroft 
764fd7a8150SMike Barcroft 	LIST_FOREACH(pr, &allprison, pr_list) {
765fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
766fd7a8150SMike Barcroft 		xp->pr_id = pr->pr_id;
767fd7a8150SMike Barcroft 		xp->pr_ip = pr->pr_ip;
768b63b0c65SPawel Jakub Dawidek 		strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
769b63b0c65SPawel Jakub Dawidek 		mtx_lock(&pr->pr_mtx);
770b63b0c65SPawel Jakub Dawidek 		strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
771fd7a8150SMike Barcroft 		mtx_unlock(&pr->pr_mtx);
772fd7a8150SMike Barcroft 		xp++;
773fd7a8150SMike Barcroft 	}
774dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
775fd7a8150SMike Barcroft 
776fd7a8150SMike Barcroft 	error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
777fd7a8150SMike Barcroft 	free(sxp, M_TEMP);
778fd7a8150SMike Barcroft 	return (error);
779fd7a8150SMike Barcroft }
780fd7a8150SMike Barcroft 
781fd7a8150SMike Barcroft SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
782fd7a8150SMike Barcroft     NULL, 0, sysctl_jail_list, "S", "List of active jails");
783461167c2SPawel Jakub Dawidek 
784461167c2SPawel Jakub Dawidek static int
785461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
786461167c2SPawel Jakub Dawidek {
787461167c2SPawel Jakub Dawidek 	int error, injail;
788461167c2SPawel Jakub Dawidek 
789461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
790461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
791461167c2SPawel Jakub Dawidek 
792461167c2SPawel Jakub Dawidek 	return (error);
793461167c2SPawel Jakub Dawidek }
794461167c2SPawel Jakub Dawidek SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD,
795461167c2SPawel Jakub Dawidek     NULL, 0, sysctl_jail_jailed, "I", "Process in jail?");
796