xref: /freebsd/sys/kern/kern_jail.c (revision dc68a633328c51142a160e723a32cc1427404674)
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>
2575c13541SPoul-Henning Kamp #include <sys/jail.h>
2601137630SRobert Watson #include <sys/lock.h>
2701137630SRobert Watson #include <sys/mutex.h>
28dc68a633SPawel Jakub Dawidek #include <sys/sx.h>
29fd7a8150SMike Barcroft #include <sys/namei.h>
30820a0de9SPawel Jakub Dawidek #include <sys/mount.h>
31fd7a8150SMike Barcroft #include <sys/queue.h>
3275c13541SPoul-Henning Kamp #include <sys/socket.h>
33fd7a8150SMike Barcroft #include <sys/syscallsubr.h>
3483f1e257SRobert Watson #include <sys/sysctl.h>
35fd7a8150SMike Barcroft #include <sys/vnode.h>
3675c13541SPoul-Henning Kamp #include <net/if.h>
3775c13541SPoul-Henning Kamp #include <netinet/in.h>
3875c13541SPoul-Henning Kamp 
39aed55708SRobert Watson #include <security/mac/mac_framework.h>
40aed55708SRobert Watson 
4175c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
4275c13541SPoul-Henning Kamp 
43d0615c64SAndrew R. Reiter SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
4483f1e257SRobert Watson     "Jail rules");
4583f1e257SRobert Watson 
4683f1e257SRobert Watson int	jail_set_hostname_allowed = 1;
47d0615c64SAndrew R. Reiter SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
4883f1e257SRobert Watson     &jail_set_hostname_allowed, 0,
4983f1e257SRobert Watson     "Processes in jail can set their hostnames");
5083f1e257SRobert Watson 
517cadc266SRobert Watson int	jail_socket_unixiproute_only = 1;
52d0615c64SAndrew R. Reiter SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
537cadc266SRobert Watson     &jail_socket_unixiproute_only, 0,
547cadc266SRobert Watson     "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
557cadc266SRobert Watson 
56cb1f0db9SRobert Watson int	jail_sysvipc_allowed = 0;
57d0615c64SAndrew R. Reiter SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
58cb1f0db9SRobert Watson     &jail_sysvipc_allowed, 0,
59cb1f0db9SRobert Watson     "Processes in jail can use System V IPC primitives");
60cb1f0db9SRobert Watson 
61820a0de9SPawel Jakub Dawidek static int jail_enforce_statfs = 2;
62820a0de9SPawel Jakub Dawidek SYSCTL_INT(_security_jail, OID_AUTO, enforce_statfs, CTLFLAG_RW,
63820a0de9SPawel Jakub Dawidek     &jail_enforce_statfs, 0,
64820a0de9SPawel Jakub Dawidek     "Processes in jail cannot see all mounted file systems");
65f08df373SRobert Watson 
665a59cefcSBosko Milekic int	jail_allow_raw_sockets = 0;
675a59cefcSBosko Milekic SYSCTL_INT(_security_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
685a59cefcSBosko Milekic     &jail_allow_raw_sockets, 0,
695a59cefcSBosko Milekic     "Prison root can create raw sockets");
705a59cefcSBosko Milekic 
7179653046SColin Percival int	jail_chflags_allowed = 0;
7279653046SColin Percival SYSCTL_INT(_security_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
7379653046SColin Percival     &jail_chflags_allowed, 0,
7479653046SColin Percival     "Processes in jail can alter system file flags");
7579653046SColin Percival 
76f3a8d2f9SPawel Jakub Dawidek int	jail_mount_allowed = 0;
77f3a8d2f9SPawel Jakub Dawidek SYSCTL_INT(_security_jail, OID_AUTO, mount_allowed, CTLFLAG_RW,
78f3a8d2f9SPawel Jakub Dawidek     &jail_mount_allowed, 0,
79f3a8d2f9SPawel Jakub Dawidek     "Processes in jail can mount/unmount jail-friendly file systems");
80f3a8d2f9SPawel Jakub Dawidek 
81dc68a633SPawel Jakub Dawidek /* allprison, lastprid, and prisoncount are protected by allprison_lock. */
82fd7a8150SMike Barcroft struct	prisonlist allprison;
83dc68a633SPawel Jakub Dawidek struct	sx allprison_lock;
84fd7a8150SMike Barcroft int	lastprid = 0;
85fd7a8150SMike Barcroft int	prisoncount = 0;
86fd7a8150SMike Barcroft 
87dc68a633SPawel Jakub Dawidek /*
88dc68a633SPawel Jakub Dawidek  * List of jail services. Protected by allprison_lock.
89dc68a633SPawel Jakub Dawidek  */
90dc68a633SPawel Jakub Dawidek TAILQ_HEAD(prison_services_head, prison_service);
91dc68a633SPawel Jakub Dawidek static struct prison_services_head prison_services =
92dc68a633SPawel Jakub Dawidek     TAILQ_HEAD_INITIALIZER(prison_services);
93dc68a633SPawel Jakub Dawidek static int prison_service_slots = 0;
94dc68a633SPawel Jakub Dawidek 
95dc68a633SPawel Jakub Dawidek struct prison_service {
96dc68a633SPawel Jakub Dawidek 	prison_create_t ps_create;
97dc68a633SPawel Jakub Dawidek 	prison_destroy_t ps_destroy;
98dc68a633SPawel Jakub Dawidek 	int		ps_slotno;
99dc68a633SPawel Jakub Dawidek 	TAILQ_ENTRY(prison_service) ps_next;
100dc68a633SPawel Jakub Dawidek 	char	ps_name[0];
101dc68a633SPawel Jakub Dawidek };
102dc68a633SPawel Jakub Dawidek 
103fd7a8150SMike Barcroft static void		 init_prison(void *);
104b3059e09SRobert Watson static void		 prison_complete(void *context, int pending);
105fd7a8150SMike Barcroft static int		 sysctl_jail_list(SYSCTL_HANDLER_ARGS);
106fd7a8150SMike Barcroft 
107fd7a8150SMike Barcroft static void
108fd7a8150SMike Barcroft init_prison(void *data __unused)
109fd7a8150SMike Barcroft {
110fd7a8150SMike Barcroft 
111dc68a633SPawel Jakub Dawidek 	sx_init(&allprison_lock, "allprison");
112fd7a8150SMike Barcroft 	LIST_INIT(&allprison);
113fd7a8150SMike Barcroft }
114fd7a8150SMike Barcroft 
115fd7a8150SMike Barcroft SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
116fd7a8150SMike Barcroft 
117116734c4SMatthew Dillon /*
1189ddb7954SMike Barcroft  * struct jail_args {
1199ddb7954SMike Barcroft  *	struct jail *jail;
1209ddb7954SMike Barcroft  * };
121116734c4SMatthew Dillon  */
12275c13541SPoul-Henning Kamp int
1239ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap)
12475c13541SPoul-Henning Kamp {
125fd7a8150SMike Barcroft 	struct nameidata nd;
126fd7a8150SMike Barcroft 	struct prison *pr, *tpr;
127dc68a633SPawel Jakub Dawidek 	struct prison_service *psrv;
12875c13541SPoul-Henning Kamp 	struct jail j;
129fd7a8150SMike Barcroft 	struct jail_attach_args jaa;
130453f7d53SChristian S.J. Peron 	int vfslocked, error, tryprid;
13175c13541SPoul-Henning Kamp 
1329ddb7954SMike Barcroft 	error = copyin(uap->jail, &j, sizeof(j));
13375c13541SPoul-Henning Kamp 	if (error)
134a2f2b3afSJohn Baldwin 		return (error);
135a2f2b3afSJohn Baldwin 	if (j.version != 0)
136a2f2b3afSJohn Baldwin 		return (EINVAL);
137a2f2b3afSJohn Baldwin 
1389ddb7954SMike Barcroft 	MALLOC(pr, struct prison *, sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1396008862bSJohn Baldwin 	mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
140fd7a8150SMike Barcroft 	pr->pr_ref = 1;
1419ddb7954SMike Barcroft 	error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0);
142fd7a8150SMike Barcroft 	if (error)
143fd7a8150SMike Barcroft 		goto e_killmtx;
144453f7d53SChristian S.J. Peron 	NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE,
145453f7d53SChristian S.J. Peron 	    pr->pr_path, td);
146fd7a8150SMike Barcroft 	error = namei(&nd);
147453f7d53SChristian S.J. Peron 	if (error)
148fd7a8150SMike Barcroft 		goto e_killmtx;
149453f7d53SChristian S.J. Peron 	vfslocked = NDHASGIANT(&nd);
150fd7a8150SMike Barcroft 	pr->pr_root = nd.ni_vp;
151fd7a8150SMike Barcroft 	VOP_UNLOCK(nd.ni_vp, 0, td);
152fd7a8150SMike Barcroft 	NDFREE(&nd, NDF_ONLY_PNBUF);
153453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
1549ddb7954SMike Barcroft 	error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
15575c13541SPoul-Henning Kamp 	if (error)
156fd7a8150SMike Barcroft 		goto e_dropvnref;
157a2f2b3afSJohn Baldwin 	pr->pr_ip = j.ip_number;
158fd7a8150SMike Barcroft 	pr->pr_linux = NULL;
159fd7a8150SMike Barcroft 	pr->pr_securelevel = securelevel;
160dc68a633SPawel Jakub Dawidek 	if (prison_service_slots == 0)
161dc68a633SPawel Jakub Dawidek 		pr->pr_slots = NULL;
162dc68a633SPawel Jakub Dawidek 	else {
163dc68a633SPawel Jakub Dawidek 		pr->pr_slots = malloc(sizeof(*pr->pr_slots) * prison_service_slots,
164dc68a633SPawel Jakub Dawidek 		    M_PRISON, M_ZERO | M_WAITOK);
165dc68a633SPawel Jakub Dawidek 	}
166fd7a8150SMike Barcroft 
167fd7a8150SMike Barcroft 	/* Determine next pr_id and add prison to allprison list. */
168dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
169fd7a8150SMike Barcroft 	tryprid = lastprid + 1;
170fd7a8150SMike Barcroft 	if (tryprid == JAIL_MAX)
171fd7a8150SMike Barcroft 		tryprid = 1;
172fd7a8150SMike Barcroft next:
173fd7a8150SMike Barcroft 	LIST_FOREACH(tpr, &allprison, pr_list) {
174fd7a8150SMike Barcroft 		if (tpr->pr_id == tryprid) {
175fd7a8150SMike Barcroft 			tryprid++;
176fd7a8150SMike Barcroft 			if (tryprid == JAIL_MAX) {
177dc68a633SPawel Jakub Dawidek 				sx_xunlock(&allprison_lock);
178fd7a8150SMike Barcroft 				error = EAGAIN;
179fd7a8150SMike Barcroft 				goto e_dropvnref;
180fd7a8150SMike Barcroft 			}
181fd7a8150SMike Barcroft 			goto next;
182fd7a8150SMike Barcroft 		}
183fd7a8150SMike Barcroft 	}
184fd7a8150SMike Barcroft 	pr->pr_id = jaa.jid = lastprid = tryprid;
185fd7a8150SMike Barcroft 	LIST_INSERT_HEAD(&allprison, pr, pr_list);
186fd7a8150SMike Barcroft 	prisoncount++;
187dc68a633SPawel Jakub Dawidek 	sx_downgrade(&allprison_lock);
188dc68a633SPawel Jakub Dawidek 	TAILQ_FOREACH(psrv, &prison_services, ps_next) {
189dc68a633SPawel Jakub Dawidek 		psrv->ps_create(psrv, pr);
190dc68a633SPawel Jakub Dawidek 	}
191dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
192fd7a8150SMike Barcroft 
193fd7a8150SMike Barcroft 	error = jail_attach(td, &jaa);
194a2f2b3afSJohn Baldwin 	if (error)
195fd7a8150SMike Barcroft 		goto e_dropprref;
196fd7a8150SMike Barcroft 	mtx_lock(&pr->pr_mtx);
197fd7a8150SMike Barcroft 	pr->pr_ref--;
198fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
199fd7a8150SMike Barcroft 	td->td_retval[0] = jaa.jid;
20075c13541SPoul-Henning Kamp 	return (0);
201fd7a8150SMike Barcroft e_dropprref:
202dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
203fd7a8150SMike Barcroft 	LIST_REMOVE(pr, pr_list);
204fd7a8150SMike Barcroft 	prisoncount--;
205dc68a633SPawel Jakub Dawidek 	sx_downgrade(&allprison_lock);
206dc68a633SPawel Jakub Dawidek 	TAILQ_FOREACH(psrv, &prison_services, ps_next) {
207dc68a633SPawel Jakub Dawidek 		psrv->ps_destroy(psrv, pr);
208dc68a633SPawel Jakub Dawidek 	}
209dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
210fd7a8150SMike Barcroft e_dropvnref:
211453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
212fd7a8150SMike Barcroft 	vrele(pr->pr_root);
213453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
214fd7a8150SMike Barcroft e_killmtx:
215894db7b0SMaxime Henrion 	mtx_destroy(&pr->pr_mtx);
21675c13541SPoul-Henning Kamp 	FREE(pr, M_PRISON);
21775c13541SPoul-Henning Kamp 	return (error);
21875c13541SPoul-Henning Kamp }
21975c13541SPoul-Henning Kamp 
220fd7a8150SMike Barcroft /*
2219ddb7954SMike Barcroft  * struct jail_attach_args {
2229ddb7954SMike Barcroft  *	int jid;
2239ddb7954SMike Barcroft  * };
224fd7a8150SMike Barcroft  */
225fd7a8150SMike Barcroft int
2269ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap)
227fd7a8150SMike Barcroft {
228fd7a8150SMike Barcroft 	struct proc *p;
229fd7a8150SMike Barcroft 	struct ucred *newcred, *oldcred;
230fd7a8150SMike Barcroft 	struct prison *pr;
231453f7d53SChristian S.J. Peron 	int vfslocked, error;
232fd7a8150SMike Barcroft 
23357f22bd4SJacques Vidrine 	/*
23457f22bd4SJacques Vidrine 	 * XXX: Note that there is a slight race here if two threads
23557f22bd4SJacques Vidrine 	 * in the same privileged process attempt to attach to two
23657f22bd4SJacques Vidrine 	 * different jails at the same time.  It is important for
23757f22bd4SJacques Vidrine 	 * user processes not to do this, or they might end up with
23857f22bd4SJacques Vidrine 	 * a process root from one prison, but attached to the jail
23957f22bd4SJacques Vidrine 	 * of another.
24057f22bd4SJacques Vidrine 	 */
241800c9408SRobert Watson 	error = priv_check(td, PRIV_JAIL_ATTACH);
24257f22bd4SJacques Vidrine 	if (error)
24357f22bd4SJacques Vidrine 		return (error);
244fd7a8150SMike Barcroft 
24557f22bd4SJacques Vidrine 	p = td->td_proc;
246dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
247fd7a8150SMike Barcroft 	pr = prison_find(uap->jid);
248fd7a8150SMike Barcroft 	if (pr == NULL) {
249dc68a633SPawel Jakub Dawidek 		sx_sunlock(&allprison_lock);
250fd7a8150SMike Barcroft 		return (EINVAL);
251fd7a8150SMike Barcroft 	}
252fd7a8150SMike Barcroft 	pr->pr_ref++;
253fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
254dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
255fd7a8150SMike Barcroft 
256453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
257fd7a8150SMike Barcroft 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td);
258fd7a8150SMike Barcroft 	if ((error = change_dir(pr->pr_root, td)) != 0)
259fd7a8150SMike Barcroft 		goto e_unlock;
260fd7a8150SMike Barcroft #ifdef MAC
261fd7a8150SMike Barcroft 	if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root)))
262fd7a8150SMike Barcroft 		goto e_unlock;
263fd7a8150SMike Barcroft #endif
264fd7a8150SMike Barcroft 	VOP_UNLOCK(pr->pr_root, 0, td);
265fd7a8150SMike Barcroft 	change_root(pr->pr_root, td);
266453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
267fd7a8150SMike Barcroft 
268fd7a8150SMike Barcroft 	newcred = crget();
269fd7a8150SMike Barcroft 	PROC_LOCK(p);
270fd7a8150SMike Barcroft 	oldcred = p->p_ucred;
271fd7a8150SMike Barcroft 	setsugid(p);
272fd7a8150SMike Barcroft 	crcopy(newcred, oldcred);
27369c4ee54SJohn Baldwin 	newcred->cr_prison = pr;
274fd7a8150SMike Barcroft 	p->p_ucred = newcred;
275fd7a8150SMike Barcroft 	PROC_UNLOCK(p);
276fd7a8150SMike Barcroft 	crfree(oldcred);
277fd7a8150SMike Barcroft 	return (0);
278fd7a8150SMike Barcroft e_unlock:
279fd7a8150SMike Barcroft 	VOP_UNLOCK(pr->pr_root, 0, td);
280453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
281fd7a8150SMike Barcroft 	mtx_lock(&pr->pr_mtx);
282fd7a8150SMike Barcroft 	pr->pr_ref--;
283fd7a8150SMike Barcroft 	mtx_unlock(&pr->pr_mtx);
284fd7a8150SMike Barcroft 	return (error);
285fd7a8150SMike Barcroft }
286fd7a8150SMike Barcroft 
287fd7a8150SMike Barcroft /*
288fd7a8150SMike Barcroft  * Returns a locked prison instance, or NULL on failure.
289fd7a8150SMike Barcroft  */
29054b369c1SPawel Jakub Dawidek struct prison *
291fd7a8150SMike Barcroft prison_find(int prid)
292fd7a8150SMike Barcroft {
293fd7a8150SMike Barcroft 	struct prison *pr;
294fd7a8150SMike Barcroft 
295dc68a633SPawel Jakub Dawidek 	sx_assert(&allprison_lock, SX_LOCKED);
296fd7a8150SMike Barcroft 	LIST_FOREACH(pr, &allprison, pr_list) {
297fd7a8150SMike Barcroft 		if (pr->pr_id == prid) {
298fd7a8150SMike Barcroft 			mtx_lock(&pr->pr_mtx);
299fd7a8150SMike Barcroft 			return (pr);
300fd7a8150SMike Barcroft 		}
301fd7a8150SMike Barcroft 	}
302fd7a8150SMike Barcroft 	return (NULL);
303fd7a8150SMike Barcroft }
304fd7a8150SMike Barcroft 
30591421ba2SRobert Watson void
30691421ba2SRobert Watson prison_free(struct prison *pr)
30791421ba2SRobert Watson {
308dc68a633SPawel Jakub Dawidek 	struct prison_service *psrv;
30991421ba2SRobert Watson 
310dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
31101137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
31291421ba2SRobert Watson 	pr->pr_ref--;
31391421ba2SRobert Watson 	if (pr->pr_ref == 0) {
314fd7a8150SMike Barcroft 		LIST_REMOVE(pr, pr_list);
31501137630SRobert Watson 		mtx_unlock(&pr->pr_mtx);
316fd7a8150SMike Barcroft 		prisoncount--;
317dc68a633SPawel Jakub Dawidek 		sx_downgrade(&allprison_lock);
318dc68a633SPawel Jakub Dawidek 		TAILQ_FOREACH(psrv, &prison_services, ps_next) {
319dc68a633SPawel Jakub Dawidek 			psrv->ps_destroy(psrv, pr);
320dc68a633SPawel Jakub Dawidek 		}
321dc68a633SPawel Jakub Dawidek 		sx_sunlock(&allprison_lock);
322b3059e09SRobert Watson 
323b3059e09SRobert Watson 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
32422fdc83fSJeff Roberson 		taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
32501137630SRobert Watson 		return;
32691421ba2SRobert Watson 	}
32701137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
328dc68a633SPawel Jakub Dawidek 	sx_xunlock(&allprison_lock);
32991421ba2SRobert Watson }
33091421ba2SRobert Watson 
331b3059e09SRobert Watson static void
332b3059e09SRobert Watson prison_complete(void *context, int pending)
333b3059e09SRobert Watson {
334b3059e09SRobert Watson 	struct prison *pr;
335453f7d53SChristian S.J. Peron 	int vfslocked;
336b3059e09SRobert Watson 
337b3059e09SRobert Watson 	pr = (struct prison *)context;
338b3059e09SRobert Watson 
339453f7d53SChristian S.J. Peron 	vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
340b3059e09SRobert Watson 	vrele(pr->pr_root);
341453f7d53SChristian S.J. Peron 	VFS_UNLOCK_GIANT(vfslocked);
342b3059e09SRobert Watson 
343b3059e09SRobert Watson 	mtx_destroy(&pr->pr_mtx);
344b3059e09SRobert Watson 	if (pr->pr_linux != NULL)
345b3059e09SRobert Watson 		FREE(pr->pr_linux, M_PRISON);
346b3059e09SRobert Watson 	FREE(pr, M_PRISON);
347b3059e09SRobert Watson }
348b3059e09SRobert Watson 
34991421ba2SRobert Watson void
35091421ba2SRobert Watson prison_hold(struct prison *pr)
35191421ba2SRobert Watson {
35291421ba2SRobert Watson 
35301137630SRobert Watson 	mtx_lock(&pr->pr_mtx);
35491421ba2SRobert Watson 	pr->pr_ref++;
35501137630SRobert Watson 	mtx_unlock(&pr->pr_mtx);
35601137630SRobert Watson }
35701137630SRobert Watson 
35801137630SRobert Watson u_int32_t
35901137630SRobert Watson prison_getip(struct ucred *cred)
36001137630SRobert Watson {
36101137630SRobert Watson 
36201137630SRobert Watson 	return (cred->cr_prison->pr_ip);
36391421ba2SRobert Watson }
36491421ba2SRobert Watson 
36575c13541SPoul-Henning Kamp int
36691421ba2SRobert Watson prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
36775c13541SPoul-Henning Kamp {
36875c13541SPoul-Henning Kamp 	u_int32_t tmp;
36975c13541SPoul-Henning Kamp 
37091421ba2SRobert Watson 	if (!jailed(cred))
37175c13541SPoul-Henning Kamp 		return (0);
37275c13541SPoul-Henning Kamp 	if (flag)
37375c13541SPoul-Henning Kamp 		tmp = *ip;
37475c13541SPoul-Henning Kamp 	else
37575c13541SPoul-Henning Kamp 		tmp = ntohl(*ip);
37675c13541SPoul-Henning Kamp 	if (tmp == INADDR_ANY) {
37775c13541SPoul-Henning Kamp 		if (flag)
37891421ba2SRobert Watson 			*ip = cred->cr_prison->pr_ip;
37975c13541SPoul-Henning Kamp 		else
38091421ba2SRobert Watson 			*ip = htonl(cred->cr_prison->pr_ip);
38175c13541SPoul-Henning Kamp 		return (0);
38275c13541SPoul-Henning Kamp 	}
383fd6aaf7fSRobert Watson 	if (tmp == INADDR_LOOPBACK) {
384fd6aaf7fSRobert Watson 		if (flag)
385fd6aaf7fSRobert Watson 			*ip = cred->cr_prison->pr_ip;
386fd6aaf7fSRobert Watson 		else
387fd6aaf7fSRobert Watson 			*ip = htonl(cred->cr_prison->pr_ip);
388fd6aaf7fSRobert Watson 		return (0);
389fd6aaf7fSRobert Watson 	}
39091421ba2SRobert Watson 	if (cred->cr_prison->pr_ip != tmp)
39175c13541SPoul-Henning Kamp 		return (1);
39275c13541SPoul-Henning Kamp 	return (0);
39375c13541SPoul-Henning Kamp }
39475c13541SPoul-Henning Kamp 
39575c13541SPoul-Henning Kamp void
39691421ba2SRobert Watson prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
39775c13541SPoul-Henning Kamp {
39875c13541SPoul-Henning Kamp 	u_int32_t tmp;
39975c13541SPoul-Henning Kamp 
40091421ba2SRobert Watson 	if (!jailed(cred))
40175c13541SPoul-Henning Kamp 		return;
40275c13541SPoul-Henning Kamp 	if (flag)
40375c13541SPoul-Henning Kamp 		tmp = *ip;
40475c13541SPoul-Henning Kamp 	else
40575c13541SPoul-Henning Kamp 		tmp = ntohl(*ip);
406fd6aaf7fSRobert Watson 	if (tmp == INADDR_LOOPBACK) {
40775c13541SPoul-Henning Kamp 		if (flag)
40891421ba2SRobert Watson 			*ip = cred->cr_prison->pr_ip;
40975c13541SPoul-Henning Kamp 		else
41091421ba2SRobert Watson 			*ip = htonl(cred->cr_prison->pr_ip);
41175c13541SPoul-Henning Kamp 		return;
41275c13541SPoul-Henning Kamp 	}
41375c13541SPoul-Henning Kamp 	return;
41475c13541SPoul-Henning Kamp }
41575c13541SPoul-Henning Kamp 
41675c13541SPoul-Henning Kamp int
41791421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa)
41875c13541SPoul-Henning Kamp {
4199ddb7954SMike Barcroft 	struct sockaddr_in *sai;
42075c13541SPoul-Henning Kamp 	int ok;
42175c13541SPoul-Henning Kamp 
4229ddb7954SMike Barcroft 	sai = (struct sockaddr_in *)sa;
4237cadc266SRobert Watson 	if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
4247cadc266SRobert Watson 		ok = 1;
4257cadc266SRobert Watson 	else if (sai->sin_family != AF_INET)
42675c13541SPoul-Henning Kamp 		ok = 0;
42791421ba2SRobert Watson 	else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
42875c13541SPoul-Henning Kamp 		ok = 1;
42975c13541SPoul-Henning Kamp 	else
43075c13541SPoul-Henning Kamp 		ok = 0;
43175c13541SPoul-Henning Kamp 	return (ok);
43275c13541SPoul-Henning Kamp }
43391421ba2SRobert Watson 
43491421ba2SRobert Watson /*
43591421ba2SRobert Watson  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
43691421ba2SRobert Watson  */
43791421ba2SRobert Watson int
4389ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2)
43991421ba2SRobert Watson {
44091421ba2SRobert Watson 
44191421ba2SRobert Watson 	if (jailed(cred1)) {
44291421ba2SRobert Watson 		if (!jailed(cred2))
44391421ba2SRobert Watson 			return (ESRCH);
44491421ba2SRobert Watson 		if (cred2->cr_prison != cred1->cr_prison)
44591421ba2SRobert Watson 			return (ESRCH);
44691421ba2SRobert Watson 	}
44791421ba2SRobert Watson 
44891421ba2SRobert Watson 	return (0);
44991421ba2SRobert Watson }
45091421ba2SRobert Watson 
45191421ba2SRobert Watson /*
45291421ba2SRobert Watson  * Return 1 if the passed credential is in a jail, otherwise 0.
45391421ba2SRobert Watson  */
45491421ba2SRobert Watson int
4559ddb7954SMike Barcroft jailed(struct ucred *cred)
45691421ba2SRobert Watson {
45791421ba2SRobert Watson 
45891421ba2SRobert Watson 	return (cred->cr_prison != NULL);
45991421ba2SRobert Watson }
4609484d0c0SRobert Drehmel 
4619484d0c0SRobert Drehmel /*
4629484d0c0SRobert Drehmel  * Return the correct hostname for the passed credential.
4639484d0c0SRobert Drehmel  */
464ad1ff099SRobert Drehmel void
4659ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size)
4669484d0c0SRobert Drehmel {
4679484d0c0SRobert Drehmel 
468ad1ff099SRobert Drehmel 	if (jailed(cred)) {
469ad1ff099SRobert Drehmel 		mtx_lock(&cred->cr_prison->pr_mtx);
470e80fb434SRobert Drehmel 		strlcpy(buf, cred->cr_prison->pr_host, size);
471ad1ff099SRobert Drehmel 		mtx_unlock(&cred->cr_prison->pr_mtx);
4729ddb7954SMike Barcroft 	} else
473e80fb434SRobert Drehmel 		strlcpy(buf, hostname, size);
4749484d0c0SRobert Drehmel }
475fd7a8150SMike Barcroft 
476f08df373SRobert Watson /*
477820a0de9SPawel Jakub Dawidek  * Determine whether the subject represented by cred can "see"
478820a0de9SPawel Jakub Dawidek  * status of a mount point.
479820a0de9SPawel Jakub Dawidek  * Returns: 0 for permitted, ENOENT otherwise.
480820a0de9SPawel Jakub Dawidek  * XXX: This function should be called cr_canseemount() and should be
481820a0de9SPawel Jakub Dawidek  *      placed in kern_prot.c.
482f08df373SRobert Watson  */
483f08df373SRobert Watson int
484820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp)
485f08df373SRobert Watson {
486820a0de9SPawel Jakub Dawidek 	struct prison *pr;
487820a0de9SPawel Jakub Dawidek 	struct statfs *sp;
488820a0de9SPawel Jakub Dawidek 	size_t len;
489f08df373SRobert Watson 
490820a0de9SPawel Jakub Dawidek 	if (!jailed(cred) || jail_enforce_statfs == 0)
491820a0de9SPawel Jakub Dawidek 		return (0);
492820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
493820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp)
494820a0de9SPawel Jakub Dawidek 		return (0);
495820a0de9SPawel Jakub Dawidek 	if (jail_enforce_statfs == 2)
496820a0de9SPawel Jakub Dawidek 		return (ENOENT);
497820a0de9SPawel Jakub Dawidek 	/*
498820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
499820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
500820a0de9SPawel Jakub Dawidek 	 * This is ugly check, but this is the only situation when jail's
501820a0de9SPawel Jakub Dawidek 	 * directory ends with '/'.
502820a0de9SPawel Jakub Dawidek 	 */
503820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
504820a0de9SPawel Jakub Dawidek 		return (0);
505820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
506820a0de9SPawel Jakub Dawidek 	sp = &mp->mnt_stat;
507820a0de9SPawel Jakub Dawidek 	if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
508820a0de9SPawel Jakub Dawidek 		return (ENOENT);
509820a0de9SPawel Jakub Dawidek 	/*
510820a0de9SPawel Jakub Dawidek 	 * Be sure that we don't have situation where jail's root directory
511820a0de9SPawel Jakub Dawidek 	 * is "/some/path" and mount point is "/some/pathpath".
512820a0de9SPawel Jakub Dawidek 	 */
513820a0de9SPawel Jakub Dawidek 	if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
514820a0de9SPawel Jakub Dawidek 		return (ENOENT);
515f08df373SRobert Watson 	return (0);
516f08df373SRobert Watson }
517820a0de9SPawel Jakub Dawidek 
518820a0de9SPawel Jakub Dawidek void
519820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
520820a0de9SPawel Jakub Dawidek {
521820a0de9SPawel Jakub Dawidek 	char jpath[MAXPATHLEN];
522820a0de9SPawel Jakub Dawidek 	struct prison *pr;
523820a0de9SPawel Jakub Dawidek 	size_t len;
524820a0de9SPawel Jakub Dawidek 
525820a0de9SPawel Jakub Dawidek 	if (!jailed(cred) || jail_enforce_statfs == 0)
526820a0de9SPawel Jakub Dawidek 		return;
527820a0de9SPawel Jakub Dawidek 	pr = cred->cr_prison;
528820a0de9SPawel Jakub Dawidek 	if (prison_canseemount(cred, mp) != 0) {
529820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
530820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, "[restricted]",
531820a0de9SPawel Jakub Dawidek 		    sizeof(sp->f_mntonname));
532820a0de9SPawel Jakub Dawidek 		return;
533820a0de9SPawel Jakub Dawidek 	}
534820a0de9SPawel Jakub Dawidek 	if (pr->pr_root->v_mount == mp) {
535820a0de9SPawel Jakub Dawidek 		/*
536820a0de9SPawel Jakub Dawidek 		 * Clear current buffer data, so we are sure nothing from
537820a0de9SPawel Jakub Dawidek 		 * the valid path left there.
538820a0de9SPawel Jakub Dawidek 		 */
539820a0de9SPawel Jakub Dawidek 		bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
540820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
541820a0de9SPawel Jakub Dawidek 		return;
542820a0de9SPawel Jakub Dawidek 	}
543820a0de9SPawel Jakub Dawidek 	/*
544820a0de9SPawel Jakub Dawidek 	 * If jail's chroot directory is set to "/" we should be able to see
545820a0de9SPawel Jakub Dawidek 	 * all mount-points from inside a jail.
546820a0de9SPawel Jakub Dawidek 	 */
547820a0de9SPawel Jakub Dawidek 	if (strcmp(pr->pr_path, "/") == 0)
548820a0de9SPawel Jakub Dawidek 		return;
549820a0de9SPawel Jakub Dawidek 	len = strlen(pr->pr_path);
550820a0de9SPawel Jakub Dawidek 	strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
551820a0de9SPawel Jakub Dawidek 	/*
552820a0de9SPawel Jakub Dawidek 	 * Clear current buffer data, so we are sure nothing from
553820a0de9SPawel Jakub Dawidek 	 * the valid path left there.
554820a0de9SPawel Jakub Dawidek 	 */
555820a0de9SPawel Jakub Dawidek 	bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
556820a0de9SPawel Jakub Dawidek 	if (*jpath == '\0') {
557820a0de9SPawel Jakub Dawidek 		/* Should never happen. */
558820a0de9SPawel Jakub Dawidek 		*sp->f_mntonname = '/';
559820a0de9SPawel Jakub Dawidek 	} else {
560820a0de9SPawel Jakub Dawidek 		strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
561820a0de9SPawel Jakub Dawidek 	}
562f08df373SRobert Watson }
563f08df373SRobert Watson 
564800c9408SRobert Watson /*
565800c9408SRobert Watson  * Check with permission for a specific privilege is granted within jail.  We
566800c9408SRobert Watson  * have a specific list of accepted privileges; the rest are denied.
567800c9408SRobert Watson  */
568800c9408SRobert Watson int
569800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv)
570800c9408SRobert Watson {
571800c9408SRobert Watson 
572800c9408SRobert Watson 	if (!jailed(cred))
573800c9408SRobert Watson 		return (0);
574800c9408SRobert Watson 
575800c9408SRobert Watson 	switch (priv) {
576800c9408SRobert Watson 
577800c9408SRobert Watson 		/*
578800c9408SRobert Watson 		 * Allow ktrace privileges for root in jail.
579800c9408SRobert Watson 		 */
580800c9408SRobert Watson 	case PRIV_KTRACE:
581800c9408SRobert Watson 
582c3c1b5e6SRobert Watson #if 0
583800c9408SRobert Watson 		/*
584800c9408SRobert Watson 		 * Allow jailed processes to configure audit identity and
585800c9408SRobert Watson 		 * submit audit records (login, etc).  In the future we may
586800c9408SRobert Watson 		 * want to further refine the relationship between audit and
587800c9408SRobert Watson 		 * jail.
588800c9408SRobert Watson 		 */
589800c9408SRobert Watson 	case PRIV_AUDIT_GETAUDIT:
590800c9408SRobert Watson 	case PRIV_AUDIT_SETAUDIT:
591800c9408SRobert Watson 	case PRIV_AUDIT_SUBMIT:
592c3c1b5e6SRobert Watson #endif
593800c9408SRobert Watson 
594800c9408SRobert Watson 		/*
595800c9408SRobert Watson 		 * Allow jailed processes to manipulate process UNIX
596800c9408SRobert Watson 		 * credentials in any way they see fit.
597800c9408SRobert Watson 		 */
598800c9408SRobert Watson 	case PRIV_CRED_SETUID:
599800c9408SRobert Watson 	case PRIV_CRED_SETEUID:
600800c9408SRobert Watson 	case PRIV_CRED_SETGID:
601800c9408SRobert Watson 	case PRIV_CRED_SETEGID:
602800c9408SRobert Watson 	case PRIV_CRED_SETGROUPS:
603800c9408SRobert Watson 	case PRIV_CRED_SETREUID:
604800c9408SRobert Watson 	case PRIV_CRED_SETREGID:
605800c9408SRobert Watson 	case PRIV_CRED_SETRESUID:
606800c9408SRobert Watson 	case PRIV_CRED_SETRESGID:
607800c9408SRobert Watson 
608800c9408SRobert Watson 		/*
609800c9408SRobert Watson 		 * Jail implements visibility constraints already, so allow
610800c9408SRobert Watson 		 * jailed root to override uid/gid-based constraints.
611800c9408SRobert Watson 		 */
612800c9408SRobert Watson 	case PRIV_SEEOTHERGIDS:
613800c9408SRobert Watson 	case PRIV_SEEOTHERUIDS:
614800c9408SRobert Watson 
615800c9408SRobert Watson 		/*
616800c9408SRobert Watson 		 * Jail implements inter-process debugging limits already, so
617800c9408SRobert Watson 		 * allow jailed root various debugging privileges.
618800c9408SRobert Watson 		 */
619800c9408SRobert Watson 	case PRIV_DEBUG_DIFFCRED:
620800c9408SRobert Watson 	case PRIV_DEBUG_SUGID:
621800c9408SRobert Watson 	case PRIV_DEBUG_UNPRIV:
622800c9408SRobert Watson 
623800c9408SRobert Watson 		/*
624800c9408SRobert Watson 		 * Allow jail to set various resource limits and login
625800c9408SRobert Watson 		 * properties, and for now, exceed process resource limits.
626800c9408SRobert Watson 		 */
627800c9408SRobert Watson 	case PRIV_PROC_LIMIT:
628800c9408SRobert Watson 	case PRIV_PROC_SETLOGIN:
629800c9408SRobert Watson 	case PRIV_PROC_SETRLIMIT:
630800c9408SRobert Watson 
631800c9408SRobert Watson 		/*
632800c9408SRobert Watson 		 * System V and POSIX IPC privileges are granted in jail.
633800c9408SRobert Watson 		 */
634800c9408SRobert Watson 	case PRIV_IPC_READ:
635800c9408SRobert Watson 	case PRIV_IPC_WRITE:
636800c9408SRobert Watson 	case PRIV_IPC_ADMIN:
637800c9408SRobert Watson 	case PRIV_IPC_MSGSIZE:
638800c9408SRobert Watson 	case PRIV_MQ_ADMIN:
639800c9408SRobert Watson 
640800c9408SRobert Watson 		/*
641800c9408SRobert Watson 		 * Jail implements its own inter-process limits, so allow
642800c9408SRobert Watson 		 * root processes in jail to change scheduling on other
643800c9408SRobert Watson 		 * processes in the same jail.  Likewise for signalling.
644800c9408SRobert Watson 		 */
645800c9408SRobert Watson 	case PRIV_SCHED_DIFFCRED:
646800c9408SRobert Watson 	case PRIV_SIGNAL_DIFFCRED:
647800c9408SRobert Watson 	case PRIV_SIGNAL_SUGID:
648800c9408SRobert Watson 
649800c9408SRobert Watson 		/*
650800c9408SRobert Watson 		 * Allow jailed processes to write to sysctls marked as jail
651800c9408SRobert Watson 		 * writable.
652800c9408SRobert Watson 		 */
653800c9408SRobert Watson 	case PRIV_SYSCTL_WRITEJAIL:
654800c9408SRobert Watson 
655800c9408SRobert Watson 		/*
656800c9408SRobert Watson 		 * Allow root in jail to manage a variety of quota
657e82d0201SRobert Watson 		 * properties.  These should likely be conditional on a
658e82d0201SRobert Watson 		 * configuration option.
659800c9408SRobert Watson 		 */
66095b091d2SRobert Watson 	case PRIV_VFS_GETQUOTA:
66195b091d2SRobert Watson 	case PRIV_VFS_SETQUOTA:
662800c9408SRobert Watson 
663800c9408SRobert Watson 		/*
664800c9408SRobert Watson 		 * Since Jail relies on chroot() to implement file system
665800c9408SRobert Watson 		 * protections, grant many VFS privileges to root in jail.
666800c9408SRobert Watson 		 * Be careful to exclude mount-related and NFS-related
667800c9408SRobert Watson 		 * privileges.
668800c9408SRobert Watson 		 */
669800c9408SRobert Watson 	case PRIV_VFS_READ:
670800c9408SRobert Watson 	case PRIV_VFS_WRITE:
671800c9408SRobert Watson 	case PRIV_VFS_ADMIN:
672800c9408SRobert Watson 	case PRIV_VFS_EXEC:
673800c9408SRobert Watson 	case PRIV_VFS_LOOKUP:
674800c9408SRobert Watson 	case PRIV_VFS_BLOCKRESERVE:	/* XXXRW: Slightly surprising. */
675800c9408SRobert Watson 	case PRIV_VFS_CHFLAGS_DEV:
676800c9408SRobert Watson 	case PRIV_VFS_CHOWN:
677800c9408SRobert Watson 	case PRIV_VFS_CHROOT:
678bb531912SPawel Jakub Dawidek 	case PRIV_VFS_RETAINSUGID:
679800c9408SRobert Watson 	case PRIV_VFS_FCHROOT:
680800c9408SRobert Watson 	case PRIV_VFS_LINK:
681800c9408SRobert Watson 	case PRIV_VFS_SETGID:
682800c9408SRobert Watson 	case PRIV_VFS_STICKYFILE:
683800c9408SRobert Watson 		return (0);
684800c9408SRobert Watson 
685800c9408SRobert Watson 		/*
686800c9408SRobert Watson 		 * Depending on the global setting, allow privilege of
687800c9408SRobert Watson 		 * setting system flags.
688800c9408SRobert Watson 		 */
689800c9408SRobert Watson 	case PRIV_VFS_SYSFLAGS:
690800c9408SRobert Watson 		if (jail_chflags_allowed)
691800c9408SRobert Watson 			return (0);
692800c9408SRobert Watson 		else
693800c9408SRobert Watson 			return (EPERM);
694800c9408SRobert Watson 
695800c9408SRobert Watson 		/*
696f3a8d2f9SPawel Jakub Dawidek 		 * Depending on the global setting, allow privilege of
697f3a8d2f9SPawel Jakub Dawidek 		 * mounting/unmounting file systems.
698f3a8d2f9SPawel Jakub Dawidek 		 */
699f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT:
700f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_UNMOUNT:
701f3a8d2f9SPawel Jakub Dawidek 	case PRIV_VFS_MOUNT_NONUSER:
702f3a8d2f9SPawel Jakub Dawidek 		if (jail_mount_allowed)
703f3a8d2f9SPawel Jakub Dawidek 			return (0);
704f3a8d2f9SPawel Jakub Dawidek 		else
705f3a8d2f9SPawel Jakub Dawidek 			return (EPERM);
706f3a8d2f9SPawel Jakub Dawidek 
707f3a8d2f9SPawel Jakub Dawidek 		/*
708800c9408SRobert Watson 		 * Allow jailed root to bind reserved ports.
709800c9408SRobert Watson 		 */
710800c9408SRobert Watson 	case PRIV_NETINET_RESERVEDPORT:
711800c9408SRobert Watson 		return (0);
712800c9408SRobert Watson 
713800c9408SRobert Watson 		/*
714800c9408SRobert Watson 		 * Conditionally allow creating raw sockets in jail.
715800c9408SRobert Watson 		 */
716800c9408SRobert Watson 	case PRIV_NETINET_RAW:
717800c9408SRobert Watson 		if (jail_allow_raw_sockets)
718800c9408SRobert Watson 			return (0);
719800c9408SRobert Watson 		else
720800c9408SRobert Watson 			return (EPERM);
721800c9408SRobert Watson 
722800c9408SRobert Watson 		/*
723800c9408SRobert Watson 		 * Since jail implements its own visibility limits on netstat
724800c9408SRobert Watson 		 * sysctls, allow getcred.  This allows identd to work in
725800c9408SRobert Watson 		 * jail.
726800c9408SRobert Watson 		 */
727800c9408SRobert Watson 	case PRIV_NETINET_GETCRED:
728800c9408SRobert Watson 		return (0);
729800c9408SRobert Watson 
730800c9408SRobert Watson 	default:
731800c9408SRobert Watson 		/*
732800c9408SRobert Watson 		 * In all remaining cases, deny the privilege request.  This
733800c9408SRobert Watson 		 * includes almost all network privileges, many system
734800c9408SRobert Watson 		 * configuration privileges.
735800c9408SRobert Watson 		 */
736800c9408SRobert Watson 		return (EPERM);
737800c9408SRobert Watson 	}
738800c9408SRobert Watson }
739800c9408SRobert Watson 
740dc68a633SPawel Jakub Dawidek /*
741dc68a633SPawel Jakub Dawidek  * Register jail service. Provides 'create' and 'destroy' methods.
742dc68a633SPawel Jakub Dawidek  * 'create' method will be called for every existing jail and all
743dc68a633SPawel Jakub Dawidek  * jails in the future as they beeing created.
744dc68a633SPawel Jakub Dawidek  * 'destroy' method will be called for every jail going away and
745dc68a633SPawel Jakub Dawidek  * for all existing jails at the time of service deregistration.
746dc68a633SPawel Jakub Dawidek  */
747dc68a633SPawel Jakub Dawidek struct prison_service *
748dc68a633SPawel Jakub Dawidek prison_service_register(const char *name, prison_create_t create,
749dc68a633SPawel Jakub Dawidek     prison_destroy_t destroy)
750dc68a633SPawel Jakub Dawidek {
751dc68a633SPawel Jakub Dawidek 	struct prison_service *psrv, *psrv2;
752dc68a633SPawel Jakub Dawidek 	struct prison *pr;
753dc68a633SPawel Jakub Dawidek 	int reallocate = 1, slotno = 0;
754dc68a633SPawel Jakub Dawidek 	void **slots, **oldslots;
755dc68a633SPawel Jakub Dawidek 
756dc68a633SPawel Jakub Dawidek 	psrv = malloc(sizeof(*psrv) + strlen(name) + 1, M_PRISON,
757dc68a633SPawel Jakub Dawidek 	    M_WAITOK | M_ZERO);
758dc68a633SPawel Jakub Dawidek 	psrv->ps_create = create;
759dc68a633SPawel Jakub Dawidek 	psrv->ps_destroy = destroy;
760dc68a633SPawel Jakub Dawidek 	strcpy(psrv->ps_name, name);
761dc68a633SPawel Jakub Dawidek 	/*
762dc68a633SPawel Jakub Dawidek 	 * Grab the allprison_lock here, so we won't miss any jail
763dc68a633SPawel Jakub Dawidek 	 * creation/destruction.
764dc68a633SPawel Jakub Dawidek 	 */
765dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
766dc68a633SPawel Jakub Dawidek #ifdef INVARIANTS
767dc68a633SPawel Jakub Dawidek 	/*
768dc68a633SPawel Jakub Dawidek 	 * Verify if service is not already registered.
769dc68a633SPawel Jakub Dawidek 	 */
770dc68a633SPawel Jakub Dawidek 	TAILQ_FOREACH(psrv2, &prison_services, ps_next) {
771dc68a633SPawel Jakub Dawidek 		KASSERT(strcmp(psrv2->ps_name, name) != 0,
772dc68a633SPawel Jakub Dawidek 		    ("jail service %s already registered", name));
773dc68a633SPawel Jakub Dawidek 	}
774dc68a633SPawel Jakub Dawidek #endif
775dc68a633SPawel Jakub Dawidek 	/*
776dc68a633SPawel Jakub Dawidek 	 * Find free slot. When there is no existing free slot available,
777dc68a633SPawel Jakub Dawidek 	 * allocate one at the end.
778dc68a633SPawel Jakub Dawidek 	 */
779dc68a633SPawel Jakub Dawidek 	TAILQ_FOREACH(psrv2, &prison_services, ps_next) {
780dc68a633SPawel Jakub Dawidek 		if (psrv2->ps_slotno != slotno) {
781dc68a633SPawel Jakub Dawidek 			KASSERT(slotno < psrv2->ps_slotno,
782dc68a633SPawel Jakub Dawidek 			    ("Invalid slotno (slotno=%d >= ps_slotno=%d",
783dc68a633SPawel Jakub Dawidek 			    slotno, psrv2->ps_slotno));
784dc68a633SPawel Jakub Dawidek 			/* We found free slot. */
785dc68a633SPawel Jakub Dawidek 			reallocate = 0;
786dc68a633SPawel Jakub Dawidek 			break;
787dc68a633SPawel Jakub Dawidek 		}
788dc68a633SPawel Jakub Dawidek 		slotno++;
789dc68a633SPawel Jakub Dawidek 	}
790dc68a633SPawel Jakub Dawidek 	psrv->ps_slotno = slotno;
791dc68a633SPawel Jakub Dawidek 	/*
792dc68a633SPawel Jakub Dawidek 	 * Keep the list sorted by slot number.
793dc68a633SPawel Jakub Dawidek 	 */
794dc68a633SPawel Jakub Dawidek 	if (psrv2 != NULL) {
795dc68a633SPawel Jakub Dawidek 		KASSERT(reallocate == 0, ("psrv2 != NULL && reallocate != 0"));
796dc68a633SPawel Jakub Dawidek 		TAILQ_INSERT_BEFORE(psrv2, psrv, ps_next);
797dc68a633SPawel Jakub Dawidek 	} else {
798dc68a633SPawel Jakub Dawidek 		KASSERT(reallocate == 1, ("psrv2 == NULL && reallocate == 0"));
799dc68a633SPawel Jakub Dawidek 		TAILQ_INSERT_TAIL(&prison_services, psrv, ps_next);
800dc68a633SPawel Jakub Dawidek 	}
801dc68a633SPawel Jakub Dawidek 	prison_service_slots++;
802dc68a633SPawel Jakub Dawidek 	sx_downgrade(&allprison_lock);
803dc68a633SPawel Jakub Dawidek 	/*
804dc68a633SPawel Jakub Dawidek 	 * Allocate memory for new slot if we didn't found empty one.
805dc68a633SPawel Jakub Dawidek 	 * Do not use realloc(9), because pr_slots is protected with a mutex,
806dc68a633SPawel Jakub Dawidek 	 * so we can't sleep.
807dc68a633SPawel Jakub Dawidek 	 */
808dc68a633SPawel Jakub Dawidek 	LIST_FOREACH(pr, &allprison, pr_list) {
809dc68a633SPawel Jakub Dawidek 		if (reallocate) {
810dc68a633SPawel Jakub Dawidek 			/* First allocate memory with M_WAITOK. */
811dc68a633SPawel Jakub Dawidek 			slots = malloc(sizeof(*slots) * prison_service_slots,
812dc68a633SPawel Jakub Dawidek 			    M_PRISON, M_WAITOK);
813dc68a633SPawel Jakub Dawidek 			/* Now grab the mutex and replace pr_slots. */
814dc68a633SPawel Jakub Dawidek 			mtx_lock(&pr->pr_mtx);
815dc68a633SPawel Jakub Dawidek 			oldslots = pr->pr_slots;
816dc68a633SPawel Jakub Dawidek 			if (psrv->ps_slotno > 0) {
817dc68a633SPawel Jakub Dawidek 				bcopy(oldslots, slots,
818dc68a633SPawel Jakub Dawidek 				    sizeof(*slots) * (prison_service_slots - 1));
819dc68a633SPawel Jakub Dawidek 			}
820dc68a633SPawel Jakub Dawidek 			slots[psrv->ps_slotno] = NULL;
821dc68a633SPawel Jakub Dawidek 			pr->pr_slots = slots;
822dc68a633SPawel Jakub Dawidek 			mtx_unlock(&pr->pr_mtx);
823dc68a633SPawel Jakub Dawidek 			if (oldslots != NULL)
824dc68a633SPawel Jakub Dawidek 				free(oldslots, M_PRISON);
825dc68a633SPawel Jakub Dawidek 		}
826dc68a633SPawel Jakub Dawidek 		/*
827dc68a633SPawel Jakub Dawidek 		 * Call 'create' method for each existing jail.
828dc68a633SPawel Jakub Dawidek 		 */
829dc68a633SPawel Jakub Dawidek 		psrv->ps_create(psrv, pr);
830dc68a633SPawel Jakub Dawidek 	}
831dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
832dc68a633SPawel Jakub Dawidek 
833dc68a633SPawel Jakub Dawidek 	return (psrv);
834dc68a633SPawel Jakub Dawidek }
835dc68a633SPawel Jakub Dawidek 
836dc68a633SPawel Jakub Dawidek void
837dc68a633SPawel Jakub Dawidek prison_service_deregister(struct prison_service *psrv)
838dc68a633SPawel Jakub Dawidek {
839dc68a633SPawel Jakub Dawidek 	struct prison *pr;
840dc68a633SPawel Jakub Dawidek 	void **slots, **oldslots;
841dc68a633SPawel Jakub Dawidek 	int last = 0;
842dc68a633SPawel Jakub Dawidek 
843dc68a633SPawel Jakub Dawidek 	sx_xlock(&allprison_lock);
844dc68a633SPawel Jakub Dawidek 	if (TAILQ_LAST(&prison_services, prison_services_head) == psrv)
845dc68a633SPawel Jakub Dawidek 		last = 1;
846dc68a633SPawel Jakub Dawidek 	TAILQ_REMOVE(&prison_services, psrv, ps_next);
847dc68a633SPawel Jakub Dawidek 	prison_service_slots--;
848dc68a633SPawel Jakub Dawidek 	sx_downgrade(&allprison_lock);
849dc68a633SPawel Jakub Dawidek 	LIST_FOREACH(pr, &allprison, pr_list) {
850dc68a633SPawel Jakub Dawidek 		/*
851dc68a633SPawel Jakub Dawidek 		 * Call 'destroy' method for every currently existing jail.
852dc68a633SPawel Jakub Dawidek 		 */
853dc68a633SPawel Jakub Dawidek 		psrv->ps_destroy(psrv, pr);
854dc68a633SPawel Jakub Dawidek 		/*
855dc68a633SPawel Jakub Dawidek 		 * If this is the last slot, free the memory allocated for it.
856dc68a633SPawel Jakub Dawidek 		 */
857dc68a633SPawel Jakub Dawidek 		if (last) {
858dc68a633SPawel Jakub Dawidek 			if (prison_service_slots == 0)
859dc68a633SPawel Jakub Dawidek 				slots = NULL;
860dc68a633SPawel Jakub Dawidek 			else {
861dc68a633SPawel Jakub Dawidek 				slots = malloc(sizeof(*slots) * prison_service_slots,
862dc68a633SPawel Jakub Dawidek 				    M_PRISON, M_WAITOK);
863dc68a633SPawel Jakub Dawidek 			}
864dc68a633SPawel Jakub Dawidek 			mtx_lock(&pr->pr_mtx);
865dc68a633SPawel Jakub Dawidek 			oldslots = pr->pr_slots;
866dc68a633SPawel Jakub Dawidek 			/*
867dc68a633SPawel Jakub Dawidek 			 * We require setting slot to NULL after freeing it,
868dc68a633SPawel Jakub Dawidek 			 * this way we can check for memory leaks here.
869dc68a633SPawel Jakub Dawidek 			 */
870dc68a633SPawel Jakub Dawidek 			KASSERT(oldslots[psrv->ps_slotno] == NULL,
871dc68a633SPawel Jakub Dawidek 			    ("Slot %d (service %s, jailid=%d) still contains data?",
872dc68a633SPawel Jakub Dawidek 			     psrv->ps_slotno, psrv->ps_name, pr->pr_id));
873dc68a633SPawel Jakub Dawidek 			if (psrv->ps_slotno > 0) {
874dc68a633SPawel Jakub Dawidek 				bcopy(oldslots, slots,
875dc68a633SPawel Jakub Dawidek 				    sizeof(*slots) * prison_service_slots);
876dc68a633SPawel Jakub Dawidek 			}
877dc68a633SPawel Jakub Dawidek 			pr->pr_slots = slots;
878dc68a633SPawel Jakub Dawidek 			mtx_unlock(&pr->pr_mtx);
879dc68a633SPawel Jakub Dawidek 			KASSERT(oldslots != NULL, ("oldslots == NULL"));
880dc68a633SPawel Jakub Dawidek 			free(oldslots, M_PRISON);
881dc68a633SPawel Jakub Dawidek 		}
882dc68a633SPawel Jakub Dawidek 	}
883dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
884dc68a633SPawel Jakub Dawidek 	free(psrv, M_PRISON);
885dc68a633SPawel Jakub Dawidek }
886dc68a633SPawel Jakub Dawidek 
887dc68a633SPawel Jakub Dawidek /*
888dc68a633SPawel Jakub Dawidek  * Function sets data for the given jail in slot assigned for the given
889dc68a633SPawel Jakub Dawidek  * jail service.
890dc68a633SPawel Jakub Dawidek  */
891dc68a633SPawel Jakub Dawidek void
892dc68a633SPawel Jakub Dawidek prison_service_data_set(struct prison_service *psrv, struct prison *pr,
893dc68a633SPawel Jakub Dawidek     void *data)
894dc68a633SPawel Jakub Dawidek {
895dc68a633SPawel Jakub Dawidek 
896dc68a633SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
897dc68a633SPawel Jakub Dawidek 	pr->pr_slots[psrv->ps_slotno] = data;
898dc68a633SPawel Jakub Dawidek }
899dc68a633SPawel Jakub Dawidek 
900dc68a633SPawel Jakub Dawidek /*
901dc68a633SPawel Jakub Dawidek  * Function clears slots assigned for the given jail service in the given
902dc68a633SPawel Jakub Dawidek  * prison structure and returns current slot data.
903dc68a633SPawel Jakub Dawidek  */
904dc68a633SPawel Jakub Dawidek void *
905dc68a633SPawel Jakub Dawidek prison_service_data_del(struct prison_service *psrv, struct prison *pr)
906dc68a633SPawel Jakub Dawidek {
907dc68a633SPawel Jakub Dawidek 	void *data;
908dc68a633SPawel Jakub Dawidek 
909dc68a633SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
910dc68a633SPawel Jakub Dawidek 	data = pr->pr_slots[psrv->ps_slotno];
911dc68a633SPawel Jakub Dawidek 	pr->pr_slots[psrv->ps_slotno] = NULL;
912dc68a633SPawel Jakub Dawidek 	return (data);
913dc68a633SPawel Jakub Dawidek }
914dc68a633SPawel Jakub Dawidek 
915dc68a633SPawel Jakub Dawidek /*
916dc68a633SPawel Jakub Dawidek  * Function returns current data from the slot assigned to the given jail
917dc68a633SPawel Jakub Dawidek  * service for the given jail.
918dc68a633SPawel Jakub Dawidek  */
919dc68a633SPawel Jakub Dawidek void *
920dc68a633SPawel Jakub Dawidek prison_service_data_get(struct prison_service *psrv, struct prison *pr)
921dc68a633SPawel Jakub Dawidek {
922dc68a633SPawel Jakub Dawidek 
923dc68a633SPawel Jakub Dawidek 	mtx_assert(&pr->pr_mtx, MA_OWNED);
924dc68a633SPawel Jakub Dawidek 	return (pr->pr_slots[psrv->ps_slotno]);
925dc68a633SPawel Jakub Dawidek }
926dc68a633SPawel Jakub Dawidek 
927fd7a8150SMike Barcroft static int
928fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS)
929fd7a8150SMike Barcroft {
930fd7a8150SMike Barcroft 	struct xprison *xp, *sxp;
931fd7a8150SMike Barcroft 	struct prison *pr;
932fd7a8150SMike Barcroft 	int count, error;
933fd7a8150SMike Barcroft 
9347f4704c0SPawel Jakub Dawidek 	if (jailed(req->td->td_ucred))
935679a1060SRobert Watson 		return (0);
936fd7a8150SMike Barcroft 
937dc68a633SPawel Jakub Dawidek 	sx_slock(&allprison_lock);
938dc68a633SPawel Jakub Dawidek 	if ((count = prisoncount) == 0) {
939dc68a633SPawel Jakub Dawidek 		sx_sunlock(&allprison_lock);
940fd7a8150SMike Barcroft 		return (0);
941dc68a633SPawel Jakub Dawidek 	}
942fd7a8150SMike Barcroft 
943fd7a8150SMike Barcroft 	sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
944fd7a8150SMike Barcroft 
945fd7a8150SMike Barcroft 	LIST_FOREACH(pr, &allprison, pr_list) {
946fd7a8150SMike Barcroft 		mtx_lock(&pr->pr_mtx);
947fd7a8150SMike Barcroft 		xp->pr_version = XPRISON_VERSION;
948fd7a8150SMike Barcroft 		xp->pr_id = pr->pr_id;
949fd7a8150SMike Barcroft 		strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
950fd7a8150SMike Barcroft 		strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
951fd7a8150SMike Barcroft 		xp->pr_ip = pr->pr_ip;
952fd7a8150SMike Barcroft 		mtx_unlock(&pr->pr_mtx);
953fd7a8150SMike Barcroft 		xp++;
954fd7a8150SMike Barcroft 	}
955dc68a633SPawel Jakub Dawidek 	sx_sunlock(&allprison_lock);
956fd7a8150SMike Barcroft 
957fd7a8150SMike Barcroft 	error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
958fd7a8150SMike Barcroft 	free(sxp, M_TEMP);
959fd7a8150SMike Barcroft 	return (error);
960fd7a8150SMike Barcroft }
961fd7a8150SMike Barcroft 
962fd7a8150SMike Barcroft SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
963fd7a8150SMike Barcroft     NULL, 0, sysctl_jail_list, "S", "List of active jails");
964461167c2SPawel Jakub Dawidek 
965461167c2SPawel Jakub Dawidek static int
966461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
967461167c2SPawel Jakub Dawidek {
968461167c2SPawel Jakub Dawidek 	int error, injail;
969461167c2SPawel Jakub Dawidek 
970461167c2SPawel Jakub Dawidek 	injail = jailed(req->td->td_ucred);
971461167c2SPawel Jakub Dawidek 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
972461167c2SPawel Jakub Dawidek 
973461167c2SPawel Jakub Dawidek 	return (error);
974461167c2SPawel Jakub Dawidek }
975461167c2SPawel Jakub Dawidek SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD,
976461167c2SPawel Jakub Dawidek     NULL, 0, sysctl_jail_jailed, "I", "Process in jail?");
977