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>
22 #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 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
40 SYSCTL_DECL(_security);
41 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
44 int jail_set_hostname_allowed = 1;
45 SYSCTL_INT(_security_jail, OID_AUTO, set_hostname_allowed, CTLFLAG_RW,
46 &jail_set_hostname_allowed, 0,
47 "Processes in jail can set their hostnames");
49 int jail_socket_unixiproute_only = 1;
50 SYSCTL_INT(_security_jail, OID_AUTO, socket_unixiproute_only, CTLFLAG_RW,
51 &jail_socket_unixiproute_only, 0,
52 "Processes in jail are limited to creating UNIX/IPv4/route sockets only");
54 int jail_sysvipc_allowed = 0;
55 SYSCTL_INT(_security_jail, OID_AUTO, sysvipc_allowed, CTLFLAG_RW,
56 &jail_sysvipc_allowed, 0,
57 "Processes in jail can use System V IPC primitives");
59 static int jail_enforce_statfs = 2;
60 SYSCTL_INT(_security_jail, OID_AUTO, enforce_statfs, CTLFLAG_RW,
61 &jail_enforce_statfs, 0,
62 "Processes in jail cannot see all mounted file systems");
64 int jail_allow_raw_sockets = 0;
65 SYSCTL_INT(_security_jail, OID_AUTO, allow_raw_sockets, CTLFLAG_RW,
66 &jail_allow_raw_sockets, 0,
67 "Prison root can create raw sockets");
69 int jail_chflags_allowed = 0;
70 SYSCTL_INT(_security_jail, OID_AUTO, chflags_allowed, CTLFLAG_RW,
71 &jail_chflags_allowed, 0,
72 "Processes in jail can alter system file flags");
74 /* allprison, lastprid, and prisoncount are protected by allprison_mtx. */
75 struct prisonlist allprison;
76 struct mtx allprison_mtx;
80 static void init_prison(void *);
81 static void prison_complete(void *context, int pending);
82 static struct prison *prison_find(int);
83 static int sysctl_jail_list(SYSCTL_HANDLER_ARGS);
86 init_prison(void *data __unused)
89 mtx_init(&allprison_mtx, "allprison", NULL, MTX_DEF);
90 LIST_INIT(&allprison);
93 SYSINIT(prison, SI_SUB_INTRINSIC, SI_ORDER_ANY, init_prison, NULL);
103 jail(struct thread *td, struct jail_args *uap)
106 struct prison *pr, *tpr;
108 struct jail_attach_args jaa;
109 int vfslocked, error, tryprid;
111 error = copyin(uap->jail, &j, sizeof(j));
117 MALLOC(pr, struct prison *, sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
118 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF);
120 error = copyinstr(j.path, &pr->pr_path, sizeof(pr->pr_path), 0);
123 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW | LOCKLEAF, UIO_SYSSPACE,
128 vfslocked = NDHASGIANT(&nd);
129 pr->pr_root = nd.ni_vp;
130 VOP_UNLOCK(nd.ni_vp, 0, td);
131 NDFREE(&nd, NDF_ONLY_PNBUF);
132 VFS_UNLOCK_GIANT(vfslocked);
133 error = copyinstr(j.hostname, &pr->pr_host, sizeof(pr->pr_host), 0);
136 pr->pr_ip = j.ip_number;
138 pr->pr_securelevel = securelevel;
140 /* Determine next pr_id and add prison to allprison list. */
141 mtx_lock(&allprison_mtx);
142 tryprid = lastprid + 1;
143 if (tryprid == JAIL_MAX)
146 LIST_FOREACH(tpr, &allprison, pr_list) {
147 if (tpr->pr_id == tryprid) {
149 if (tryprid == JAIL_MAX) {
150 mtx_unlock(&allprison_mtx);
157 pr->pr_id = jaa.jid = lastprid = tryprid;
158 LIST_INSERT_HEAD(&allprison, pr, pr_list);
160 mtx_unlock(&allprison_mtx);
162 error = jail_attach(td, &jaa);
165 mtx_lock(&pr->pr_mtx);
167 mtx_unlock(&pr->pr_mtx);
168 td->td_retval[0] = jaa.jid;
171 mtx_lock(&allprison_mtx);
172 LIST_REMOVE(pr, pr_list);
174 mtx_unlock(&allprison_mtx);
176 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
178 VFS_UNLOCK_GIANT(vfslocked);
180 mtx_destroy(&pr->pr_mtx);
188 * struct jail_attach_args {
193 jail_attach(struct thread *td, struct jail_attach_args *uap)
196 struct ucred *newcred, *oldcred;
198 int vfslocked, error;
201 * XXX: Note that there is a slight race here if two threads
202 * in the same privileged process attempt to attach to two
203 * different jails at the same time. It is important for
204 * user processes not to do this, or they might end up with
205 * a process root from one prison, but attached to the jail
213 mtx_lock(&allprison_mtx);
214 pr = prison_find(uap->jid);
216 mtx_unlock(&allprison_mtx);
220 mtx_unlock(&pr->pr_mtx);
221 mtx_unlock(&allprison_mtx);
223 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
224 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY, td);
225 if ((error = change_dir(pr->pr_root, td)) != 0)
228 if ((error = mac_check_vnode_chroot(td->td_ucred, pr->pr_root)))
231 VOP_UNLOCK(pr->pr_root, 0, td);
232 change_root(pr->pr_root, td);
233 VFS_UNLOCK_GIANT(vfslocked);
237 oldcred = p->p_ucred;
239 crcopy(newcred, oldcred);
240 newcred->cr_prison = pr;
241 p->p_ucred = newcred;
246 VOP_UNLOCK(pr->pr_root, 0, td);
247 VFS_UNLOCK_GIANT(vfslocked);
248 mtx_lock(&pr->pr_mtx);
250 mtx_unlock(&pr->pr_mtx);
255 * Returns a locked prison instance, or NULL on failure.
257 static struct prison *
258 prison_find(int prid)
262 mtx_assert(&allprison_mtx, MA_OWNED);
263 LIST_FOREACH(pr, &allprison, pr_list) {
264 if (pr->pr_id == prid) {
265 mtx_lock(&pr->pr_mtx);
273 prison_free(struct prison *pr)
276 mtx_lock(&allprison_mtx);
277 mtx_lock(&pr->pr_mtx);
279 if (pr->pr_ref == 0) {
280 LIST_REMOVE(pr, pr_list);
281 mtx_unlock(&pr->pr_mtx);
283 mtx_unlock(&allprison_mtx);
285 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
286 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
289 mtx_unlock(&pr->pr_mtx);
290 mtx_unlock(&allprison_mtx);
294 prison_complete(void *context, int pending)
299 pr = (struct prison *)context;
301 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
303 VFS_UNLOCK_GIANT(vfslocked);
305 mtx_destroy(&pr->pr_mtx);
306 if (pr->pr_linux != NULL)
307 FREE(pr->pr_linux, M_PRISON);
312 prison_hold(struct prison *pr)
315 mtx_lock(&pr->pr_mtx);
317 mtx_unlock(&pr->pr_mtx);
321 prison_getip(struct ucred *cred)
324 return (cred->cr_prison->pr_ip);
328 prison_ip(struct ucred *cred, int flag, u_int32_t *ip)
338 if (tmp == INADDR_ANY) {
340 *ip = cred->cr_prison->pr_ip;
342 *ip = htonl(cred->cr_prison->pr_ip);
345 if (tmp == INADDR_LOOPBACK) {
347 *ip = cred->cr_prison->pr_ip;
349 *ip = htonl(cred->cr_prison->pr_ip);
352 if (cred->cr_prison->pr_ip != tmp)
358 prison_remote_ip(struct ucred *cred, int flag, u_int32_t *ip)
368 if (tmp == INADDR_LOOPBACK) {
370 *ip = cred->cr_prison->pr_ip;
372 *ip = htonl(cred->cr_prison->pr_ip);
379 prison_if(struct ucred *cred, struct sockaddr *sa)
381 struct sockaddr_in *sai;
384 sai = (struct sockaddr_in *)sa;
385 if ((sai->sin_family != AF_INET) && jail_socket_unixiproute_only)
387 else if (sai->sin_family != AF_INET)
389 else if (cred->cr_prison->pr_ip != ntohl(sai->sin_addr.s_addr))
397 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
400 prison_check(struct ucred *cred1, struct ucred *cred2)
406 if (cred2->cr_prison != cred1->cr_prison)
414 * Return 1 if the passed credential is in a jail, otherwise 0.
417 jailed(struct ucred *cred)
420 return (cred->cr_prison != NULL);
424 * Return the correct hostname for the passed credential.
427 getcredhostname(struct ucred *cred, char *buf, size_t size)
431 mtx_lock(&cred->cr_prison->pr_mtx);
432 strlcpy(buf, cred->cr_prison->pr_host, size);
433 mtx_unlock(&cred->cr_prison->pr_mtx);
435 strlcpy(buf, hostname, size);
439 * Determine whether the subject represented by cred can "see"
440 * status of a mount point.
441 * Returns: 0 for permitted, ENOENT otherwise.
442 * XXX: This function should be called cr_canseemount() and should be
443 * placed in kern_prot.c.
446 prison_canseemount(struct ucred *cred, struct mount *mp)
452 if (!jailed(cred) || jail_enforce_statfs == 0)
454 pr = cred->cr_prison;
455 if (pr->pr_root->v_mount == mp)
457 if (jail_enforce_statfs == 2)
460 * If jail's chroot directory is set to "/" we should be able to see
461 * all mount-points from inside a jail.
462 * This is ugly check, but this is the only situation when jail's
463 * directory ends with '/'.
465 if (strcmp(pr->pr_path, "/") == 0)
467 len = strlen(pr->pr_path);
469 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
472 * Be sure that we don't have situation where jail's root directory
473 * is "/some/path" and mount point is "/some/pathpath".
475 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
481 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
483 char jpath[MAXPATHLEN];
487 if (!jailed(cred) || jail_enforce_statfs == 0)
489 pr = cred->cr_prison;
490 if (prison_canseemount(cred, mp) != 0) {
491 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
492 strlcpy(sp->f_mntonname, "[restricted]",
493 sizeof(sp->f_mntonname));
496 if (pr->pr_root->v_mount == mp) {
498 * Clear current buffer data, so we are sure nothing from
499 * the valid path left there.
501 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
502 *sp->f_mntonname = '/';
506 * If jail's chroot directory is set to "/" we should be able to see
507 * all mount-points from inside a jail.
509 if (strcmp(pr->pr_path, "/") == 0)
511 len = strlen(pr->pr_path);
512 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
514 * Clear current buffer data, so we are sure nothing from
515 * the valid path left there.
517 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
518 if (*jpath == '\0') {
519 /* Should never happen. */
520 *sp->f_mntonname = '/';
522 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
527 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
529 struct xprison *xp, *sxp;
533 if (jailed(req->td->td_ucred))
536 mtx_lock(&allprison_mtx);
538 mtx_unlock(&allprison_mtx);
543 sxp = xp = malloc(sizeof(*xp) * count, M_TEMP, M_WAITOK | M_ZERO);
544 mtx_lock(&allprison_mtx);
545 if (count != prisoncount) {
546 mtx_unlock(&allprison_mtx);
551 LIST_FOREACH(pr, &allprison, pr_list) {
552 mtx_lock(&pr->pr_mtx);
553 xp->pr_version = XPRISON_VERSION;
554 xp->pr_id = pr->pr_id;
555 strlcpy(xp->pr_path, pr->pr_path, sizeof(xp->pr_path));
556 strlcpy(xp->pr_host, pr->pr_host, sizeof(xp->pr_host));
557 xp->pr_ip = pr->pr_ip;
558 mtx_unlock(&pr->pr_mtx);
561 mtx_unlock(&allprison_mtx);
563 error = SYSCTL_OUT(req, sxp, sizeof(*sxp) * count);
570 SYSCTL_OID(_security_jail, OID_AUTO, list, CTLTYPE_STRUCT | CTLFLAG_RD,
571 NULL, 0, sysctl_jail_list, "S", "List of active jails");
574 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
578 injail = jailed(req->td->td_ucred);
579 error = SYSCTL_OUT(req, &injail, sizeof(injail));
583 SYSCTL_PROC(_security_jail, OID_AUTO, jailed, CTLTYPE_INT | CTLFLAG_RD,
584 NULL, 0, sysctl_jail_jailed, "I", "Process in jail?");