xref: /freebsd/sys/kern/kern_jail.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
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 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
12 
13 #include "opt_mac.h"
14 
15 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/kernel.h>
18 #include <sys/systm.h>
19 #include <sys/errno.h>
20 #include <sys/sysproto.h>
21 #include <sys/mac.h>
22 #include <sys/malloc.h>
23 #include <sys/proc.h>
24 #include <sys/taskqueue.h>
25 #include <sys/jail.h>
26 #include <sys/lock.h>
27 #include <sys/mutex.h>
28 #include <sys/namei.h>
29 #include <sys/queue.h>
30 #include <sys/socket.h>
31 #include <sys/syscallsubr.h>
32 #include <sys/sysctl.h>
33 #include <sys/vnode.h>
34 #include <net/if.h>
35 #include <netinet/in.h>
36 
37 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
38 
39 SYSCTL_DECL(_security);
40 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
41     "Jail rules");
42 
43 mp_fixme("these variables need a lock")
44 
45 int	jail_set_hostname_allowed = 1;
46 SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
47     &jail_set_hostname_allowed, 0,
48     "Processes in jail can set their hostnames");
49 
50 int	jail_socket_unixiproute_only = 1;
51 SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
52     &jail_socket_unixiproute_only, 0,
53     "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
54 
55 int	jail_sysvipc_allowed = 0;
56 SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
57     &jail_sysvipc_allowed, 0,
58     "Processes in jail can use System V IPC primitives");
59 
60 int	jail_getfsstatroot_only = 1;
61 SYSCTL_INT(_security_jail, OID_AUTO, getfsstatroot_only, CTLFLAG_RW,
62     &jail_getfsstatroot_only, 0,
63     "Processes see only their root file system in getfsstat()");
64 
65 int	jail_allow_raw_sockets = 0;
66 SYSCTL_INT(_security_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
67     &jail_allow_raw_sockets, 0,
68     "Prison root can create raw sockets");
69 
70 /* allprison, lastprid, and prisoncount are protected by allprison_mtx. */
71 struct	prisonlist allprison;
72 struct	mtx allprison_mtx;
73 int	lastprid = 0;
74 int	prisoncount = 0;
75 
76 static void		 init_prison(void *);
77 static void		 prison_complete(void *context, int pending);
78 static struct prison	*prison_find(int);
79 static int		 sysctl_jail_list(SYSCTL_HANDLER_ARGS);
80 
81 static void
82 init_prison(void *data __unused)
83 {
84 
85 	mtx_init(&allprison_mtx, "allprison", NULL, MTX_DEF);
86 	LIST_INIT(&allprison);
87 }
88 
89 SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
90 
91 /*
92  * MPSAFE
93  *
94  * struct jail_args {
95  *	struct jail *jail;
96  * };
97  */
98 int
99 jail(struct thread *td, struct jail_args *uap)
100 {
101 	struct nameidata nd;
102 	struct prison *pr, *tpr;
103 	struct jail j;
104 	struct jail_attach_args jaa;
105 	int error, tryprid;
106 
107 	error = copyin(uap->jail, &j, sizeof(j));
108 	if (error)
109 		return (error);
110 	if (j.version != 0)
111 		return (EINVAL);
112 
113 	MALLOC(pr, struct prison *, sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
114 	mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
115 	pr->pr_ref = 1;
116 	error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0);
117 	if (error)
118 		goto e_killmtx;
119 	mtx_lock(&Giant);
120 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, pr->pr_path, td);
121 	error = namei(&nd);
122 	if (error) {
123 		mtx_unlock(&Giant);
124 		goto e_killmtx;
125 	}
126 	pr->pr_root = nd.ni_vp;
127 	VOP_UNLOCK(nd.ni_vp, 0, td);
128 	NDFREE(&nd, NDF_ONLY_PNBUF);
129 	mtx_unlock(&Giant);
130 	error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
131 	if (error)
132 		goto e_dropvnref;
133 	pr->pr_ip = j.ip_number;
134 	pr->pr_linux = NULL;
135 	pr->pr_securelevel = securelevel;
136 
137 	/* Determine next pr_id and add prison to allprison list. */
138 	mtx_lock(&allprison_mtx);
139 	tryprid = lastprid + 1;
140 	if (tryprid == JAIL_MAX)
141 		tryprid = 1;
142 next:
143 	LIST_FOREACH(tpr, &allprison, pr_list) {
144 		if (tpr->pr_id == tryprid) {
145 			tryprid++;
146 			if (tryprid == JAIL_MAX) {
147 				mtx_unlock(&allprison_mtx);
148 				error = EAGAIN;
149 				goto e_dropvnref;
150 			}
151 			goto next;
152 		}
153 	}
154 	pr->pr_id = jaa.jid = lastprid = tryprid;
155 	LIST_INSERT_HEAD(&allprison, pr, pr_list);
156 	prisoncount++;
157 	mtx_unlock(&allprison_mtx);
158 
159 	error = jail_attach(td, &jaa);
160 	if (error)
161 		goto e_dropprref;
162 	mtx_lock(&pr->pr_mtx);
163 	pr->pr_ref--;
164 	mtx_unlock(&pr->pr_mtx);
165 	td->td_retval[0] = jaa.jid;
166 	return (0);
167 e_dropprref:
168 	mtx_lock(&allprison_mtx);
169 	LIST_REMOVE(pr, pr_list);
170 	prisoncount--;
171 	mtx_unlock(&allprison_mtx);
172 e_dropvnref:
173 	mtx_lock(&Giant);
174 	vrele(pr->pr_root);
175 	mtx_unlock(&Giant);
176 e_killmtx:
177 	mtx_destroy(&pr->pr_mtx);
178 	FREE(pr, M_PRISON);
179 	return (error);
180 }
181 
182 /*
183  * MPSAFE
184  *
185  * struct jail_attach_args {
186  *	int jid;
187  * };
188  */
189 int
190 jail_attach(struct thread *td, struct jail_attach_args *uap)
191 {
192 	struct proc *p;
193 	struct ucred *newcred, *oldcred;
194 	struct prison *pr;
195 	int error;
196 
197 	/*
198 	 * XXX: Note that there is a slight race here if two threads
199 	 * in the same privileged process attempt to attach to two
200 	 * different jails at the same time.  It is important for
201 	 * user processes not to do this, or they might end up with
202 	 * a process root from one prison, but attached to the jail
203 	 * of another.
204 	 */
205 	error = suser(td);
206 	if (error)
207 		return (error);
208 
209 	p = td->td_proc;
210 	mtx_lock(&allprison_mtx);
211 	pr = prison_find(uap->jid);
212 	if (pr == NULL) {
213 		mtx_unlock(&allprison_mtx);
214 		return (EINVAL);
215 	}
216 	pr->pr_ref++;
217 	mtx_unlock(&pr->pr_mtx);
218 	mtx_unlock(&allprison_mtx);
219 
220 	mtx_lock(&Giant);
221 	vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td);
222 	if ((error = change_dir(pr->pr_root, td)) != 0)
223 		goto e_unlock;
224 #ifdef MAC
225 	if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root)))
226 		goto e_unlock;
227 #endif
228 	VOP_UNLOCK(pr->pr_root, 0, td);
229 	change_root(pr->pr_root, td);
230 	mtx_unlock(&Giant);
231 
232 	newcred = crget();
233 	PROC_LOCK(p);
234 	oldcred = p->p_ucred;
235 	setsugid(p);
236 	crcopy(newcred, oldcred);
237 	newcred->cr_prison = pr;
238 	p->p_ucred = newcred;
239 	PROC_UNLOCK(p);
240 	crfree(oldcred);
241 	return (0);
242 e_unlock:
243 	VOP_UNLOCK(pr->pr_root, 0, td);
244 	mtx_unlock(&Giant);
245 	mtx_lock(&pr->pr_mtx);
246 	pr->pr_ref--;
247 	mtx_unlock(&pr->pr_mtx);
248 	return (error);
249 }
250 
251 /*
252  * Returns a locked prison instance, or NULL on failure.
253  */
254 static struct prison *
255 prison_find(int prid)
256 {
257 	struct prison *pr;
258 
259 	mtx_assert(&allprison_mtx, MA_OWNED);
260 	LIST_FOREACH(pr, &allprison, pr_list) {
261 		if (pr->pr_id == prid) {
262 			mtx_lock(&pr->pr_mtx);
263 			return (pr);
264 		}
265 	}
266 	return (NULL);
267 }
268 
269 void
270 prison_free(struct prison *pr)
271 {
272 
273 	mtx_lock(&allprison_mtx);
274 	mtx_lock(&pr->pr_mtx);
275 	pr->pr_ref--;
276 	if (pr->pr_ref == 0) {
277 		LIST_REMOVE(pr, pr_list);
278 		mtx_unlock(&pr->pr_mtx);
279 		prisoncount--;
280 		mtx_unlock(&allprison_mtx);
281 
282 		TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
283 		taskqueue_enqueue(taskqueue_swi, &pr->pr_task);
284 		return;
285 	}
286 	mtx_unlock(&pr->pr_mtx);
287 	mtx_unlock(&allprison_mtx);
288 }
289 
290 static void
291 prison_complete(void *context, int pending)
292 {
293 	struct prison *pr;
294 
295 	pr = (struct prison *)context;
296 
297 	mtx_lock(&Giant);
298 	vrele(pr->pr_root);
299 	mtx_unlock(&Giant);
300 
301 	mtx_destroy(&pr->pr_mtx);
302 	if (pr->pr_linux != NULL)
303 		FREE(pr->pr_linux, M_PRISON);
304 	FREE(pr, M_PRISON);
305 }
306 
307 void
308 prison_hold(struct prison *pr)
309 {
310 
311 	mtx_lock(&pr->pr_mtx);
312 	pr->pr_ref++;
313 	mtx_unlock(&pr->pr_mtx);
314 }
315 
316 u_int32_t
317 prison_getip(struct ucred *cred)
318 {
319 
320 	return (cred->cr_prison->pr_ip);
321 }
322 
323 int
324 prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
325 {
326 	u_int32_t tmp;
327 
328 	if (!jailed(cred))
329 		return (0);
330 	if (flag)
331 		tmp = *ip;
332 	else
333 		tmp = ntohl(*ip);
334 	if (tmp == INADDR_ANY) {
335 		if (flag)
336 			*ip = cred->cr_prison->pr_ip;
337 		else
338 			*ip = htonl(cred->cr_prison->pr_ip);
339 		return (0);
340 	}
341 	if (tmp == INADDR_LOOPBACK) {
342 		if (flag)
343 			*ip = cred->cr_prison->pr_ip;
344 		else
345 			*ip = htonl(cred->cr_prison->pr_ip);
346 		return (0);
347 	}
348 	if (cred->cr_prison->pr_ip != tmp)
349 		return (1);
350 	return (0);
351 }
352 
353 void
354 prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
355 {
356 	u_int32_t tmp;
357 
358 	if (!jailed(cred))
359 		return;
360 	if (flag)
361 		tmp = *ip;
362 	else
363 		tmp = ntohl(*ip);
364 	if (tmp == INADDR_LOOPBACK) {
365 		if (flag)
366 			*ip = cred->cr_prison->pr_ip;
367 		else
368 			*ip = htonl(cred->cr_prison->pr_ip);
369 		return;
370 	}
371 	return;
372 }
373 
374 int
375 prison_if(struct ucred *cred, struct sockaddr *sa)
376 {
377 	struct sockaddr_in *sai;
378 	int ok;
379 
380 	sai = (struct sockaddr_in *)sa;
381 	if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
382 		ok = 1;
383 	else if (sai->sin_family != AF_INET)
384 		ok = 0;
385 	else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
386 		ok = 1;
387 	else
388 		ok = 0;
389 	return (ok);
390 }
391 
392 /*
393  * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
394  */
395 int
396 prison_check(struct ucred *cred1, struct ucred *cred2)
397 {
398 
399 	if (jailed(cred1)) {
400 		if (!jailed(cred2))
401 			return (ESRCH);
402 		if (cred2->cr_prison != cred1->cr_prison)
403 			return (ESRCH);
404 	}
405 
406 	return (0);
407 }
408 
409 /*
410  * Return 1 if the passed credential is in a jail, otherwise 0.
411  */
412 int
413 jailed(struct ucred *cred)
414 {
415 
416 	return (cred->cr_prison != NULL);
417 }
418 
419 /*
420  * Return the correct hostname for the passed credential.
421  */
422 void
423 getcredhostname(struct ucred *cred, char *buf, size_t size)
424 {
425 
426 	if (jailed(cred)) {
427 		mtx_lock(&cred->cr_prison->pr_mtx);
428 		strlcpy(buf, cred->cr_prison->pr_host, size);
429 		mtx_unlock(&cred->cr_prison->pr_mtx);
430 	} else
431 		strlcpy(buf, hostname, size);
432 }
433 
434 /*
435  * Return 1 if the passed credential can "see" the passed mountpoint
436  * when performing a getfsstat(); otherwise, 0.
437  */
438 int
439 prison_check_mount(struct ucred *cred, struct mount *mp)
440 {
441 
442 	if (jail_getfsstatroot_only && cred->cr_prison != NULL) {
443 		if (cred->cr_prison->pr_root->v_mount != mp)
444 			return (0);
445 	}
446 	return (1);
447 }
448 
449 static int
450 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
451 {
452 	struct xprison *xp, *sxp;
453 	struct prison *pr;
454 	int count, error;
455 
456 	mtx_assert(&Giant, MA_OWNED);
457 	if (jailed(req->td->td_ucred))
458 		return (0);
459 retry:
460 	mtx_lock(&allprison_mtx);
461 	count = prisoncount;
462 	mtx_unlock(&allprison_mtx);
463 
464 	if (count == 0)
465 		return (0);
466 
467 	sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
468 	mtx_lock(&allprison_mtx);
469 	if (count != prisoncount) {
470 		mtx_unlock(&allprison_mtx);
471 		free(sxp, M_TEMP);
472 		goto retry;
473 	}
474 
475 	LIST_FOREACH(pr, &allprison, pr_list) {
476 		mtx_lock(&pr->pr_mtx);
477 		xp->pr_version = XPRISON_VERSION;
478 		xp->pr_id = pr->pr_id;
479 		strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
480 		strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
481 		xp->pr_ip = pr->pr_ip;
482 		mtx_unlock(&pr->pr_mtx);
483 		xp++;
484 	}
485 	mtx_unlock(&allprison_mtx);
486 
487 	error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
488 	free(sxp, M_TEMP);
489 	if (error)
490 		return (error);
491 	return (0);
492 }
493 
494 SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
495     NULL, 0, sysctl_jail_list, "S", "List of active jails");
496 
497 static int
498 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
499 {
500 	int error, injail;
501 
502 	injail = jailed(req->td->td_ucred);
503 	error = SYSCTL_OUT(req, &injail, sizeof(injail));
504 
505 	return (error);
506 }
507 SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD,
508     NULL, 0, sysctl_jail_jailed, "I", "Process in jail?");
509