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 * ----------------------------------------------------------------------------
10 #include <sys/cdefs.h>
11 __FBSDID("$FreeBSD$");
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/malloc.h>
24 #include <sys/taskqueue.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>
36 #include <netinet/in.h>
38 #include <security/mac/mac_framework.h>
40 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
42 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
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");
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");
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");
60 static int jail_enforce_statfs = 2;
61 SYSCTL_INT(_security_jail, OID_AUTO, enforce_statfs, CTLFLAG_RW,
62 &jail_enforce_statfs, 0,
63 "Processes in jail cannot see all mounted file systems");
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");
70 int jail_chflags_allowed = 0;
71 SYSCTL_INT(_security_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
72 &jail_chflags_allowed, 0,
73 "Processes in jail can alter system file flags");
75 /* allprison, lastprid, and prisoncount are protected by allprison_mtx. */
76 struct prisonlist allprison;
77 struct mtx allprison_mtx;
81 static void init_prison(void *);
82 static void prison_complete(void *context, int pending);
83 static struct prison *prison_find(int);
84 static int sysctl_jail_list(SYSCTL_HANDLER_ARGS);
87 init_prison(void *data __unused)
90 mtx_init(&allprison_mtx, "allprison", NULL, MTX_DEF);
91 LIST_INIT(&allprison);
94 SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
104 jail(struct thread *td, struct jail_args *uap)
107 struct prison *pr, *tpr;
109 struct jail_attach_args jaa;
110 int vfslocked, error, tryprid;
112 error = copyin(uap->jail, &j, sizeof(j));
118 MALLOC(pr, struct prison *, sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
119 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
121 error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0);
124 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE,
129 vfslocked = NDHASGIANT(&nd);
130 pr->pr_root = nd.ni_vp;
131 VOP_UNLOCK(nd.ni_vp, 0, td);
132 NDFREE(&nd, NDF_ONLY_PNBUF);
133 VFS_UNLOCK_GIANT(vfslocked);
134 error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
137 pr->pr_ip = j.ip_number;
139 pr->pr_securelevel = securelevel;
141 /* Determine next pr_id and add prison to allprison list. */
142 mtx_lock(&allprison_mtx);
143 tryprid = lastprid + 1;
144 if (tryprid == JAIL_MAX)
147 LIST_FOREACH(tpr, &allprison, pr_list) {
148 if (tpr->pr_id == tryprid) {
150 if (tryprid == JAIL_MAX) {
151 mtx_unlock(&allprison_mtx);
158 pr->pr_id = jaa.jid = lastprid = tryprid;
159 LIST_INSERT_HEAD(&allprison, pr, pr_list);
161 mtx_unlock(&allprison_mtx);
163 error = jail_attach(td, &jaa);
166 mtx_lock(&pr->pr_mtx);
168 mtx_unlock(&pr->pr_mtx);
169 td->td_retval[0] = jaa.jid;
172 mtx_lock(&allprison_mtx);
173 LIST_REMOVE(pr, pr_list);
175 mtx_unlock(&allprison_mtx);
177 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
179 VFS_UNLOCK_GIANT(vfslocked);
181 mtx_destroy(&pr->pr_mtx);
189 * struct jail_attach_args {
194 jail_attach(struct thread *td, struct jail_attach_args *uap)
197 struct ucred *newcred, *oldcred;
199 int vfslocked, error;
202 * XXX: Note that there is a slight race here if two threads
203 * in the same privileged process attempt to attach to two
204 * different jails at the same time. It is important for
205 * user processes not to do this, or they might end up with
206 * a process root from one prison, but attached to the jail
209 error = priv_check(td, PRIV_JAIL_ATTACH);
214 mtx_lock(&allprison_mtx);
215 pr = prison_find(uap->jid);
217 mtx_unlock(&allprison_mtx);
221 mtx_unlock(&pr->pr_mtx);
222 mtx_unlock(&allprison_mtx);
224 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
225 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td);
226 if ((error = change_dir(pr->pr_root, td)) != 0)
229 if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root)))
232 VOP_UNLOCK(pr->pr_root, 0, td);
233 change_root(pr->pr_root, td);
234 VFS_UNLOCK_GIANT(vfslocked);
238 oldcred = p->p_ucred;
240 crcopy(newcred, oldcred);
241 newcred->cr_prison = pr;
242 p->p_ucred = newcred;
247 VOP_UNLOCK(pr->pr_root, 0, td);
248 VFS_UNLOCK_GIANT(vfslocked);
249 mtx_lock(&pr->pr_mtx);
251 mtx_unlock(&pr->pr_mtx);
256 * Returns a locked prison instance, or NULL on failure.
258 static struct prison *
259 prison_find(int prid)
263 mtx_assert(&allprison_mtx, MA_OWNED);
264 LIST_FOREACH(pr, &allprison, pr_list) {
265 if (pr->pr_id == prid) {
266 mtx_lock(&pr->pr_mtx);
274 prison_free(struct prison *pr)
277 mtx_lock(&allprison_mtx);
278 mtx_lock(&pr->pr_mtx);
280 if (pr->pr_ref == 0) {
281 LIST_REMOVE(pr, pr_list);
282 mtx_unlock(&pr->pr_mtx);
284 mtx_unlock(&allprison_mtx);
286 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
287 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
290 mtx_unlock(&pr->pr_mtx);
291 mtx_unlock(&allprison_mtx);
295 prison_complete(void *context, int pending)
300 pr = (struct prison *)context;
302 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
304 VFS_UNLOCK_GIANT(vfslocked);
306 mtx_destroy(&pr->pr_mtx);
307 if (pr->pr_linux != NULL)
308 FREE(pr->pr_linux, M_PRISON);
313 prison_hold(struct prison *pr)
316 mtx_lock(&pr->pr_mtx);
318 mtx_unlock(&pr->pr_mtx);
322 prison_getip(struct ucred *cred)
325 return (cred->cr_prison->pr_ip);
329 prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
339 if (tmp == INADDR_ANY) {
341 *ip = cred->cr_prison->pr_ip;
343 *ip = htonl(cred->cr_prison->pr_ip);
346 if (tmp == INADDR_LOOPBACK) {
348 *ip = cred->cr_prison->pr_ip;
350 *ip = htonl(cred->cr_prison->pr_ip);
353 if (cred->cr_prison->pr_ip != tmp)
359 prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
369 if (tmp == INADDR_LOOPBACK) {
371 *ip = cred->cr_prison->pr_ip;
373 *ip = htonl(cred->cr_prison->pr_ip);
380 prison_if(struct ucred *cred, struct sockaddr *sa)
382 struct sockaddr_in *sai;
385 sai = (struct sockaddr_in *)sa;
386 if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
388 else if (sai->sin_family != AF_INET)
390 else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
398 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
401 prison_check(struct ucred *cred1, struct ucred *cred2)
407 if (cred2->cr_prison != cred1->cr_prison)
415 * Return 1 if the passed credential is in a jail, otherwise 0.
418 jailed(struct ucred *cred)
421 return (cred->cr_prison != NULL);
425 * Return the correct hostname for the passed credential.
428 getcredhostname(struct ucred *cred, char *buf, size_t size)
432 mtx_lock(&cred->cr_prison->pr_mtx);
433 strlcpy(buf, cred->cr_prison->pr_host, size);
434 mtx_unlock(&cred->cr_prison->pr_mtx);
436 strlcpy(buf, hostname, size);
440 * Determine whether the subject represented by cred can "see"
441 * status of a mount point.
442 * Returns: 0 for permitted, ENOENT otherwise.
443 * XXX: This function should be called cr_canseemount() and should be
444 * placed in kern_prot.c.
447 prison_canseemount(struct ucred *cred, struct mount *mp)
453 if (!jailed(cred) || jail_enforce_statfs == 0)
455 pr = cred->cr_prison;
456 if (pr->pr_root->v_mount == mp)
458 if (jail_enforce_statfs == 2)
461 * If jail's chroot directory is set to "/" we should be able to see
462 * all mount-points from inside a jail.
463 * This is ugly check, but this is the only situation when jail's
464 * directory ends with '/'.
466 if (strcmp(pr->pr_path, "/") == 0)
468 len = strlen(pr->pr_path);
470 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
473 * Be sure that we don't have situation where jail's root directory
474 * is "/some/path" and mount point is "/some/pathpath".
476 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
482 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
484 char jpath[MAXPATHLEN];
488 if (!jailed(cred) || jail_enforce_statfs == 0)
490 pr = cred->cr_prison;
491 if (prison_canseemount(cred, mp) != 0) {
492 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
493 strlcpy(sp->f_mntonname, "[restricted]",
494 sizeof(sp->f_mntonname));
497 if (pr->pr_root->v_mount == mp) {
499 * Clear current buffer data, so we are sure nothing from
500 * the valid path left there.
502 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
503 *sp->f_mntonname = '/';
507 * If jail's chroot directory is set to "/" we should be able to see
508 * all mount-points from inside a jail.
510 if (strcmp(pr->pr_path, "/") == 0)
512 len = strlen(pr->pr_path);
513 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
515 * Clear current buffer data, so we are sure nothing from
516 * the valid path left there.
518 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
519 if (*jpath == '\0') {
520 /* Should never happen. */
521 *sp->f_mntonname = '/';
523 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
528 * Check with permission for a specific privilege is granted within jail. We
529 * have a specific list of accepted privileges; the rest are denied.
532 prison_priv_check(struct ucred *cred, int priv)
541 * Allow ktrace privileges for root in jail.
546 * Allow jailed processes to configure audit identity and
547 * submit audit records (login, etc). In the future we may
548 * want to further refine the relationship between audit and
551 case PRIV_AUDIT_GETAUDIT:
552 case PRIV_AUDIT_SETAUDIT:
553 case PRIV_AUDIT_SUBMIT:
556 * Allow jailed processes to manipulate process UNIX
557 * credentials in any way they see fit.
559 case PRIV_CRED_SETUID:
560 case PRIV_CRED_SETEUID:
561 case PRIV_CRED_SETGID:
562 case PRIV_CRED_SETEGID:
563 case PRIV_CRED_SETGROUPS:
564 case PRIV_CRED_SETREUID:
565 case PRIV_CRED_SETREGID:
566 case PRIV_CRED_SETRESUID:
567 case PRIV_CRED_SETRESGID:
570 * Jail implements visibility constraints already, so allow
571 * jailed root to override uid/gid-based constraints.
573 case PRIV_SEEOTHERGIDS:
574 case PRIV_SEEOTHERUIDS:
577 * Jail implements inter-process debugging limits already, so
578 * allow jailed root various debugging privileges.
580 case PRIV_DEBUG_DIFFCRED:
581 case PRIV_DEBUG_SUGID:
582 case PRIV_DEBUG_UNPRIV:
585 * Allow jail to set various resource limits and login
586 * properties, and for now, exceed process resource limits.
588 case PRIV_PROC_LIMIT:
589 case PRIV_PROC_SETLOGIN:
590 case PRIV_PROC_SETRLIMIT:
593 * System V and POSIX IPC privileges are granted in jail.
599 case PRIV_IPC_MSGSIZE:
603 * Jail implements its own inter-process limits, so allow
604 * root processes in jail to change scheduling on other
605 * processes in the same jail. Likewise for signalling.
607 case PRIV_SCHED_DIFFCRED:
608 case PRIV_SIGNAL_DIFFCRED:
609 case PRIV_SIGNAL_SUGID:
612 * Allow jailed processes to write to sysctls marked as jail
615 case PRIV_SYSCTL_WRITEJAIL:
618 * Allow root in jail to manage a variety of quota
619 * properties. Some are a bit surprising and should be
622 case PRIV_UFS_GETQUOTA:
623 case PRIV_UFS_QUOTAOFF: /* XXXRW: Slightly surprising. */
624 case PRIV_UFS_QUOTAON: /* XXXRW: Slightly surprising. */
625 case PRIV_UFS_SETQUOTA:
626 case PRIV_UFS_SETUSE: /* XXXRW: Slightly surprising. */
629 * Since Jail relies on chroot() to implement file system
630 * protections, grant many VFS privileges to root in jail.
631 * Be careful to exclude mount-related and NFS-related
638 case PRIV_VFS_LOOKUP:
639 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
640 case PRIV_VFS_CHFLAGS_DEV:
642 case PRIV_VFS_CHROOT:
643 case PRIV_VFS_CLEARSUGID:
644 case PRIV_VFS_FCHROOT:
646 case PRIV_VFS_SETGID:
647 case PRIV_VFS_STICKYFILE:
651 * Depending on the global setting, allow privilege of
652 * setting system flags.
654 case PRIV_VFS_SYSFLAGS:
655 if (jail_chflags_allowed)
661 * Allow jailed root to bind reserved ports.
663 case PRIV_NETINET_RESERVEDPORT:
667 * Conditionally allow creating raw sockets in jail.
669 case PRIV_NETINET_RAW:
670 if (jail_allow_raw_sockets)
676 * Since jail implements its own visibility limits on netstat
677 * sysctls, allow getcred. This allows identd to work in
680 case PRIV_NETINET_GETCRED:
685 * In all remaining cases, deny the privilege request. This
686 * includes almost all network privileges, many system
687 * configuration privileges.
694 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
696 struct xprison *xp, *sxp;
700 if (jailed(req->td->td_ucred))
703 mtx_lock(&allprison_mtx);
705 mtx_unlock(&allprison_mtx);
710 sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
711 mtx_lock(&allprison_mtx);
712 if (count != prisoncount) {
713 mtx_unlock(&allprison_mtx);
718 LIST_FOREACH(pr, &allprison, pr_list) {
719 mtx_lock(&pr->pr_mtx);
720 xp->pr_version = XPRISON_VERSION;
721 xp->pr_id = pr->pr_id;
722 strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
723 strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
724 xp->pr_ip = pr->pr_ip;
725 mtx_unlock(&pr->pr_mtx);
728 mtx_unlock(&allprison_mtx);
730 error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
737 SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
738 NULL, 0, sysctl_jail_list, "S", "List of active jails");
741 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
745 injail = jailed(req->td->td_ucred);
746 error = SYSCTL_OUT(req, &injail, sizeof(injail));
750 SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD,
751 NULL, 0, sysctl_jail_jailed, "I", "Process in jail?");