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