2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2008 Bjoern A. Zeeb.
4 * Copyright (c) 2009 James Gritton.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
47 #include <sys/taskqueue.h>
48 #include <sys/fcntl.h>
51 #include <sys/mutex.h>
53 #include <sys/sysent.h>
54 #include <sys/namei.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/vnode.h>
65 #include <netinet/in.h>
70 #include <netinet6/in6_var.h>
74 #include <security/mac/mac_framework.h>
76 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
78 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
80 /* prison0 describes what is "real" about the system. */
81 struct prison prison0 = {
88 .pr_childmax = JAIL_MAX,
89 .pr_hostuuid = DEFAULT_HOSTUUID,
90 .pr_children = LIST_HEAD_INITIALIZER(&prison0.pr_children),
92 .pr_allow = PR_ALLOW_ALL,
94 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
96 /* allprison and lastprid are protected by allprison_lock. */
97 struct sx allprison_lock;
98 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
99 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
102 static int do_jail_attach(struct thread *td, struct prison *pr);
103 static void prison_complete(void *context, int pending);
104 static void prison_deref(struct prison *pr, int flags);
105 static char *prison_path(struct prison *pr1, struct prison *pr2);
106 static void prison_remove_one(struct prison *pr);
108 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
109 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
112 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
113 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
116 /* Flags for prison_deref */
117 #define PD_DEREF 0x01
118 #define PD_DEUREF 0x02
119 #define PD_LOCKED 0x04
120 #define PD_LIST_SLOCKED 0x08
121 #define PD_LIST_XLOCKED 0x10
124 * Parameter names corresponding to PR_* flag values
126 static char *pr_flag_names[] = {
130 static char *pr_flag_nonames[] = {
134 struct jailsys_flags {
138 } pr_flag_jailsys[] = {
139 { "host", 0, PR_HOST },
141 { "vnet", 0, PR_VNET },
144 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
147 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
151 static char *pr_allow_names[] = {
152 "allow.set_hostname",
161 static char *pr_allow_nonames[] = {
162 "allow.noset_hostname",
164 "allow.noraw_sockets",
171 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
172 #define JAIL_DEFAULT_ENFORCE_STATFS 2
173 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
174 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
175 #if defined(INET) || defined(INET6)
176 static unsigned jail_max_af_ips = 255;
181 qcmp_v4(const void *ip1, const void *ip2)
186 * We need to compare in HBO here to get the list sorted as expected
187 * by the result of the code. Sorting NBO addresses gives you
188 * interesting results. If you do not understand, do not try.
190 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
191 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
194 * Do not simply return the difference of the two numbers, the int is
208 qcmp_v6(const void *ip1, const void *ip2)
210 const struct in6_addr *ia6a, *ia6b;
213 ia6a = (const struct in6_addr *)ip1;
214 ia6b = (const struct in6_addr *)ip2;
217 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
218 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
220 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
233 jail(struct thread *td, struct jail_args *uap)
239 error = copyin(uap->jail, &version, sizeof(uint32_t));
248 /* FreeBSD single IPv4 jails. */
249 bzero(&j, sizeof(struct jail));
250 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
253 j.version = j0.version;
255 j.hostname = j0.hostname;
256 j.ip4s = j0.ip_number;
262 * Version 1 was used by multi-IPv4 jail implementations
263 * that never made it into the official kernel.
267 case 2: /* JAIL_API_VERSION */
268 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
269 error = copyin(uap->jail, &j, sizeof(struct jail));
275 /* Sci-Fi jails are not supported, sorry. */
278 return (kern_jail(td, &j));
282 kern_jail(struct thread *td, struct jail *j)
284 struct iovec optiov[2 * (4
285 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
294 char *u_path, *u_hostname, *u_name;
297 struct in_addr *u_ip4;
300 struct in6_addr *u_ip6;
303 int error, enforce_statfs, fi;
305 bzero(&optiov, sizeof(optiov));
306 opt.uio_iov = optiov;
310 opt.uio_segflg = UIO_SYSSPACE;
311 opt.uio_rw = UIO_READ;
314 /* Set permissions for top-level jails from sysctls. */
315 if (!jailed(td->td_ucred)) {
316 for (fi = 0; fi < sizeof(pr_allow_names) /
317 sizeof(pr_allow_names[0]); fi++) {
318 optiov[opt.uio_iovcnt].iov_base =
319 (jail_default_allow & (1 << fi))
320 ? pr_allow_names[fi] : pr_allow_nonames[fi];
321 optiov[opt.uio_iovcnt].iov_len =
322 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
325 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
326 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
328 enforce_statfs = jail_default_enforce_statfs;
329 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
330 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
334 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
336 ip4s = (j->version == 0) ? 1 : j->ip4s;
337 if (ip4s > jail_max_af_ips)
339 tmplen += ip4s * sizeof(struct in_addr);
345 if (j->ip6s > jail_max_af_ips)
347 tmplen += j->ip6s * sizeof(struct in6_addr);
352 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
353 u_hostname = u_path + MAXPATHLEN;
354 u_name = u_hostname + MAXHOSTNAMELEN;
356 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
360 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
362 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
365 optiov[opt.uio_iovcnt].iov_base = "path";
366 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
368 optiov[opt.uio_iovcnt].iov_base = u_path;
369 error = copyinstr(j->path, u_path, MAXPATHLEN,
370 &optiov[opt.uio_iovcnt].iov_len);
372 free(u_path, M_TEMP);
376 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
377 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
379 optiov[opt.uio_iovcnt].iov_base = u_hostname;
380 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
381 &optiov[opt.uio_iovcnt].iov_len);
383 free(u_path, M_TEMP);
387 if (j->jailname != NULL) {
388 optiov[opt.uio_iovcnt].iov_base = "name";
389 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
391 optiov[opt.uio_iovcnt].iov_base = u_name;
392 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
393 &optiov[opt.uio_iovcnt].iov_len);
395 free(u_path, M_TEMP);
401 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
402 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
404 optiov[opt.uio_iovcnt].iov_base = u_ip4;
405 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
407 u_ip4->s_addr = j->ip4s;
409 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
411 free(u_path, M_TEMP);
418 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
419 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
421 optiov[opt.uio_iovcnt].iov_base = u_ip6;
422 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
423 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
425 free(u_path, M_TEMP);
430 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
431 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
432 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
433 free(u_path, M_TEMP);
439 * struct jail_set_args {
440 * struct iovec *iovp;
441 * unsigned int iovcnt;
446 jail_set(struct thread *td, struct jail_set_args *uap)
451 /* Check that we have an even number of iovecs. */
455 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
458 error = kern_jail_set(td, auio, uap->flags);
464 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
471 struct in6_addr *ip6;
474 struct vfsoptlist *opts;
475 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
477 char *domain, *errmsg, *host, *name, *p, *path, *uuid;
478 #if defined(INET) || defined(INET6)
483 size_t namelen, onamelen;
484 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
485 int gotchildmax, gotenforce, gothid, gotslevel;
486 int fi, jid, jsys, len, level;
487 int childmax, slevel, vfslocked;
488 #if defined(INET) || defined(INET6)
497 unsigned pr_flags, ch_flags;
498 unsigned pr_allow, ch_allow, tallow;
501 error = priv_check(td, PRIV_JAIL_SET);
502 if (!error && (flags & JAIL_ATTACH))
503 error = priv_check(td, PRIV_JAIL_ATTACH);
506 mypr = ppr = td->td_ucred->cr_prison;
507 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
509 if (flags & ~JAIL_SET_MASK)
513 * Check all the parameters before committing to anything. Not all
514 * errors can be caught early, but we may as well try. Also, this
515 * takes care of some expensive stuff (path lookup) before getting
516 * the allprison lock.
518 * XXX Jails are not filesystems, and jail parameters are not mount
519 * options. But it makes more sense to re-use the vfsopt code
520 * than duplicate it under a different name.
522 error = vfs_buildopts(optuio, &opts);
532 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
538 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
547 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
555 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
556 gotenforce = (error == 0);
558 if (enforce < 0 || enforce > 2)
560 } else if (error != ENOENT)
563 pr_flags = ch_flags = 0;
564 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
566 if (pr_flag_names[fi] == NULL)
568 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
569 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
571 ch_flags |= pr_flags;
572 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
574 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
581 case JAIL_SYS_DISABLE:
582 if (!pr_flag_jailsys[fi].disable) {
586 pr_flags |= pr_flag_jailsys[fi].disable;
589 pr_flags |= pr_flag_jailsys[fi].new;
591 case JAIL_SYS_INHERIT:
598 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
600 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
601 && !(pr_flags & PR_PERSIST)) {
603 vfs_opterror(opts, "new jail must persist or attach");
607 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
609 vfs_opterror(opts, "vnet cannot be changed after creation");
614 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
616 vfs_opterror(opts, "ip4 cannot be changed after creation");
621 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
623 vfs_opterror(opts, "ip6 cannot be changed after creation");
628 pr_allow = ch_allow = 0;
629 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
631 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
632 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
634 ch_allow |= pr_allow;
636 error = vfs_getopt(opts, "name", (void **)&name, &len);
642 if (len == 0 || name[len - 1] != '\0') {
646 if (len > MAXHOSTNAMELEN) {
647 error = ENAMETOOLONG;
652 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
660 if (len == 0 || host[len - 1] != '\0') {
664 if (len > MAXHOSTNAMELEN) {
665 error = ENAMETOOLONG;
670 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
678 if (len == 0 || domain[len - 1] != '\0') {
682 if (len > MAXHOSTNAMELEN) {
683 error = ENAMETOOLONG;
688 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
696 if (len == 0 || uuid[len - 1] != '\0') {
700 if (len > HOSTUUIDLEN) {
701 error = ENAMETOOLONG;
707 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
710 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
714 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
726 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
728 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
731 else if (ip4s & (sizeof(*ip4) - 1)) {
735 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
737 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
739 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
740 ip4s /= sizeof(*ip4);
741 if (ip4s > jail_max_af_ips) {
743 vfs_opterror(opts, "too many IPv4 addresses");
746 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
747 bcopy(op, ip4, ip4s * sizeof(*ip4));
749 * IP addresses are all sorted but ip[0] to preserve
750 * the primary IP address as given from userland.
751 * This special IP is used for unbound outgoing
752 * connections as well for "loopback" traffic.
755 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
757 * Check for duplicate addresses and do some simple
758 * zero and broadcast checks. If users give other bogus
759 * addresses it is their problem.
761 * We do not have to care about byte order for these
762 * checks so we will do them in NBO.
764 for (ii = 0; ii < ip4s; ii++) {
765 if (ip4[ii].s_addr == INADDR_ANY ||
766 ip4[ii].s_addr == INADDR_BROADCAST) {
771 (ip4[0].s_addr == ip4[ii+1].s_addr ||
772 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
782 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
784 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
787 else if (ip6s & (sizeof(*ip6) - 1)) {
791 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
793 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
795 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
796 ip6s /= sizeof(*ip6);
797 if (ip6s > jail_max_af_ips) {
799 vfs_opterror(opts, "too many IPv6 addresses");
802 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
803 bcopy(op, ip6, ip6s * sizeof(*ip6));
805 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
806 for (ii = 0; ii < ip6s; ii++) {
807 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
812 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
813 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
823 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
824 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
827 "vnet jails cannot have IP address restrictions");
833 error = vfs_getopt(opts, "path", (void **)&path, &len);
839 if (flags & JAIL_UPDATE) {
842 "path cannot be changed after creation");
845 if (len == 0 || path[len - 1] != '\0') {
849 if (len < 2 || (len == 2 && path[0] == '/'))
852 /* Leave room for a real-root full pathname. */
853 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
854 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
855 error = ENAMETOOLONG;
858 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
863 vfslocked = NDHASGIANT(&nd);
865 NDFREE(&nd, NDF_ONLY_PNBUF);
866 if (root->v_type != VDIR) {
869 VFS_UNLOCK_GIANT(vfslocked);
872 VFS_UNLOCK_GIANT(vfslocked);
877 * Grab the allprison lock before letting modules check their
878 * parameters. Once we have it, do not let go so we'll have a
879 * consistent view of the OSD list.
881 sx_xlock(&allprison_lock);
882 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
884 goto done_unlock_list;
886 /* By now, all parameters should have been noted. */
887 TAILQ_FOREACH(opt, opts, link) {
888 if (!opt->seen && strcmp(opt->name, "errmsg")) {
890 vfs_opterror(opts, "unknown parameter: %s", opt->name);
891 goto done_unlock_list;
896 * See if we are creating a new record or updating an existing one.
897 * This abuses the file error codes ENOENT and EEXIST.
899 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
902 vfs_opterror(opts, "no valid operation (create or update)");
903 goto done_unlock_list;
908 * See if a requested jid already exists. There is an
909 * information leak here if the jid exists but is not within
910 * the caller's jail hierarchy. Jail creators will get EEXIST
911 * even though they cannot see the jail, and CREATE | UPDATE
912 * will return ENOENT which is not normally a valid error.
916 vfs_opterror(opts, "negative jid");
917 goto done_unlock_list;
919 pr = prison_find(jid);
922 /* Create: jid must not exist. */
923 if (cuflags == JAIL_CREATE) {
924 mtx_unlock(&pr->pr_mtx);
926 vfs_opterror(opts, "jail %d already exists",
928 goto done_unlock_list;
930 if (!prison_ischild(mypr, pr)) {
931 mtx_unlock(&pr->pr_mtx);
933 } else if (pr->pr_uref == 0) {
934 if (!(flags & JAIL_DYING)) {
935 mtx_unlock(&pr->pr_mtx);
937 vfs_opterror(opts, "jail %d is dying",
939 goto done_unlock_list;
940 } else if ((flags & JAIL_ATTACH) ||
941 (pr_flags & PR_PERSIST)) {
943 * A dying jail might be resurrected
944 * (via attach or persist), but first
945 * it must determine if another jail
946 * has claimed its name. Accomplish
947 * this by implicitly re-setting the
951 name = prison_name(mypr, pr);
956 /* Update: jid must exist. */
957 if (cuflags == JAIL_UPDATE) {
959 vfs_opterror(opts, "jail %d not found", jid);
960 goto done_unlock_list;
965 * If the caller provided a name, look for a jail by that name.
966 * This has different semantics for creates and updates keyed by jid
967 * (where the name must not already exist in a different jail),
968 * and updates keyed by the name itself (where the name must exist
969 * because that is the jail being updated).
972 p = strrchr(name, '.');
975 * This is a hierarchical name. Split it into the
976 * parent and child names, and make sure the parent
977 * exists or matches an already found jail.
981 if (strncmp(name, ppr->pr_name, p - name) ||
982 ppr->pr_name[p - name] != '\0') {
983 mtx_unlock(&pr->pr_mtx);
986 "cannot change jail's parent");
987 goto done_unlock_list;
990 ppr = prison_find_name(mypr, name);
994 "jail \"%s\" not found", name);
995 goto done_unlock_list;
997 mtx_unlock(&ppr->pr_mtx);
1001 if (name[0] != '\0') {
1003 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1006 FOREACH_PRISON_CHILD(ppr, tpr) {
1007 if (tpr != pr && tpr->pr_ref > 0 &&
1008 !strcmp(tpr->pr_name + namelen, name)) {
1010 cuflags != JAIL_CREATE) {
1011 mtx_lock(&tpr->pr_mtx);
1012 if (tpr->pr_ref > 0) {
1017 if (tpr->pr_uref > 0) {
1023 mtx_unlock(&tpr->pr_mtx);
1024 } else if (tpr->pr_uref > 0) {
1026 * Create, or update(jid):
1027 * name must not exist in an
1028 * active sibling jail.
1032 mtx_unlock(&pr->pr_mtx);
1034 "jail \"%s\" already exists",
1036 goto done_unlock_list;
1040 /* If no active jail is found, use a dying one. */
1041 if (deadpr != NULL && pr == NULL) {
1042 if (flags & JAIL_DYING) {
1043 mtx_lock(&deadpr->pr_mtx);
1044 if (deadpr->pr_ref == 0) {
1045 mtx_unlock(&deadpr->pr_mtx);
1049 } else if (cuflags == JAIL_UPDATE) {
1052 "jail \"%s\" is dying", name);
1053 goto done_unlock_list;
1056 /* Update: name must exist if no jid. */
1057 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1059 vfs_opterror(opts, "jail \"%s\" not found",
1061 goto done_unlock_list;
1065 /* Update: must provide a jid or name. */
1066 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1068 vfs_opterror(opts, "update specified no jail");
1069 goto done_unlock_list;
1072 /* If there's no prison to update, create a new one and link it in. */
1074 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1075 if (tpr->pr_childcount >= tpr->pr_childmax) {
1077 vfs_opterror(opts, "prison limit exceeded");
1078 goto done_unlock_list;
1081 mtx_lock(&ppr->pr_mtx);
1082 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1083 mtx_unlock(&ppr->pr_mtx);
1085 vfs_opterror(opts, "parent jail went away!");
1086 goto done_unlock_list;
1090 mtx_unlock(&ppr->pr_mtx);
1091 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1093 /* Find the next free jid. */
1096 if (jid == JAIL_MAX)
1098 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1099 if (tpr->pr_id < jid)
1101 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1102 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1105 if (jid == lastprid) {
1108 "no available jail IDs");
1110 prison_deref(ppr, PD_DEREF |
1111 PD_DEUREF | PD_LIST_XLOCKED);
1120 * The jail already has a jid (that did not yet exist),
1121 * so just find where to insert it.
1123 TAILQ_FOREACH(tpr, &allprison, pr_list)
1124 if (tpr->pr_id >= jid) {
1125 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1130 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1131 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1132 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1133 tpr->pr_childcount++;
1135 pr->pr_parent = ppr;
1138 /* Set some default values, and inherit some from the parent. */
1143 root = mypr->pr_root;
1146 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1147 pr->pr_flags |= PR_HOST;
1148 #if defined(INET) || defined(INET6)
1150 if (!(pr_flags & PR_VNET))
1154 if (!(ch_flags & PR_IP4_USER))
1156 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1157 else if (!(pr_flags & PR_IP4_USER)) {
1158 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1159 if (ppr->pr_ip4 != NULL) {
1160 pr->pr_ip4s = ppr->pr_ip4s;
1161 pr->pr_ip4 = malloc(pr->pr_ip4s *
1162 sizeof(struct in_addr), M_PRISON,
1164 bcopy(ppr->pr_ip4, pr->pr_ip4,
1165 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1170 if (!(ch_flags & PR_IP6_USER))
1172 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1173 else if (!(pr_flags & PR_IP6_USER)) {
1174 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1175 if (ppr->pr_ip6 != NULL) {
1176 pr->pr_ip6s = ppr->pr_ip6s;
1177 pr->pr_ip6 = malloc(pr->pr_ip6s *
1178 sizeof(struct in6_addr), M_PRISON,
1180 bcopy(ppr->pr_ip6, pr->pr_ip6,
1181 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1187 pr->pr_securelevel = ppr->pr_securelevel;
1188 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1189 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1191 LIST_INIT(&pr->pr_children);
1192 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1195 /* Allocate a new vnet if specified. */
1196 pr->pr_vnet = (pr_flags & PR_VNET)
1197 ? vnet_alloc() : ppr->pr_vnet;
1200 * Allocate a dedicated cpuset for each jail.
1201 * Unlike other initial settings, this may return an erorr.
1203 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1205 prison_deref(pr, PD_LIST_XLOCKED);
1209 mtx_lock(&pr->pr_mtx);
1211 * New prisons do not yet have a reference, because we do not
1212 * want other to see the incomplete prison once the
1213 * allprison_lock is downgraded.
1218 * Grab a reference for existing prisons, to ensure they
1219 * continue to exist for the duration of the call.
1222 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1223 if ((pr->pr_flags & PR_VNET) &&
1224 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1227 "vnet jails cannot have IP address restrictions");
1228 goto done_deref_locked;
1232 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1235 "ip4 cannot be changed after creation");
1236 goto done_deref_locked;
1240 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1243 "ip6 cannot be changed after creation");
1244 goto done_deref_locked;
1249 /* Do final error checking before setting anything. */
1251 if (slevel < ppr->pr_securelevel) {
1253 goto done_deref_locked;
1257 if (childmax >= ppr->pr_childmax) {
1259 goto done_deref_locked;
1263 if (enforce < ppr->pr_enforce_statfs) {
1265 goto done_deref_locked;
1270 if (ppr->pr_flags & PR_IP4) {
1272 * Make sure the new set of IP addresses is a
1273 * subset of the parent's list. Don't worry
1274 * about the parent being unlocked, as any
1275 * setting is done with allprison_lock held.
1277 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1278 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1280 if (ij == ppr->pr_ip4s) {
1282 goto done_deref_locked;
1285 for (ii = ij = 1; ii < ip4s; ii++) {
1286 if (ip4[ii].s_addr ==
1287 ppr->pr_ip4[0].s_addr)
1289 for (; ij < ppr->pr_ip4s; ij++)
1290 if (ip4[ii].s_addr ==
1291 ppr->pr_ip4[ij].s_addr)
1293 if (ij == ppr->pr_ip4s)
1296 if (ij == ppr->pr_ip4s) {
1298 goto done_deref_locked;
1303 * Check for conflicting IP addresses. We permit them
1304 * if there is no more than one IP on each jail. If
1305 * there is a duplicate on a jail with more than one
1306 * IP stop checking and return error.
1310 for (; tppr != &prison0; tppr = tppr->pr_parent)
1311 if (tppr->pr_flags & PR_VNET)
1314 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1317 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1319 tpr->pr_uref == 0) {
1323 if (!(tpr->pr_flags & PR_IP4_USER))
1326 if (tpr->pr_ip4 == NULL ||
1327 (ip4s == 1 && tpr->pr_ip4s == 1))
1329 for (ii = 0; ii < ip4s; ii++) {
1330 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1333 "IPv4 addresses clash");
1334 goto done_deref_locked;
1342 if (ppr->pr_flags & PR_IP6) {
1344 * Make sure the new set of IP addresses is a
1345 * subset of the parent's list.
1347 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1348 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1351 if (ij == ppr->pr_ip6s) {
1353 goto done_deref_locked;
1356 for (ii = ij = 1; ii < ip6s; ii++) {
1357 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1360 for (; ij < ppr->pr_ip6s; ij++)
1361 if (IN6_ARE_ADDR_EQUAL(
1362 &ip6[ii], &ppr->pr_ip6[ij]))
1364 if (ij == ppr->pr_ip6s)
1367 if (ij == ppr->pr_ip6s) {
1369 goto done_deref_locked;
1373 /* Check for conflicting IP addresses. */
1376 for (; tppr != &prison0; tppr = tppr->pr_parent)
1377 if (tppr->pr_flags & PR_VNET)
1380 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1383 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1385 tpr->pr_uref == 0) {
1389 if (!(tpr->pr_flags & PR_IP6_USER))
1392 if (tpr->pr_ip6 == NULL ||
1393 (ip6s == 1 && tpr->pr_ip6s == 1))
1395 for (ii = 0; ii < ip6s; ii++) {
1396 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1399 "IPv6 addresses clash");
1400 goto done_deref_locked;
1406 onamelen = namelen = 0;
1408 /* Give a default name of the jid. */
1409 if (name[0] == '\0')
1410 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1411 else if (strtoul(name, &p, 10) != jid && *p == '\0') {
1413 vfs_opterror(opts, "name cannot be numeric");
1414 goto done_deref_locked;
1417 * Make sure the name isn't too long for the prison or its
1420 onamelen = strlen(pr->pr_name);
1421 namelen = strlen(name);
1422 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1423 error = ENAMETOOLONG;
1424 goto done_deref_locked;
1426 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1427 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1428 sizeof(pr->pr_name)) {
1429 error = ENAMETOOLONG;
1430 goto done_deref_locked;
1434 if (pr_allow & ~ppr->pr_allow) {
1436 goto done_deref_locked;
1439 /* Set the parameters of the prison. */
1442 if (pr_flags & PR_IP4_USER) {
1443 pr->pr_flags |= PR_IP4;
1444 free(pr->pr_ip4, M_PRISON);
1448 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1450 if (tpr->pr_flags & PR_VNET) {
1455 if (prison_restrict_ip4(tpr, NULL)) {
1464 if (pr_flags & PR_IP6_USER) {
1465 pr->pr_flags |= PR_IP6;
1466 free(pr->pr_ip6, M_PRISON);
1470 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1472 if (tpr->pr_flags & PR_VNET) {
1477 if (prison_restrict_ip6(tpr, NULL)) {
1485 pr->pr_securelevel = slevel;
1486 /* Set all child jails to be at least this level. */
1487 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1488 if (tpr->pr_securelevel < slevel)
1489 tpr->pr_securelevel = slevel;
1492 pr->pr_childmax = childmax;
1493 /* Set all child jails to under this limit. */
1494 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1495 if (tpr->pr_childmax > childmax - level)
1496 tpr->pr_childmax = childmax > level
1497 ? childmax - level : 0;
1500 pr->pr_enforce_statfs = enforce;
1501 /* Pass this restriction on to the children. */
1502 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1503 if (tpr->pr_enforce_statfs < enforce)
1504 tpr->pr_enforce_statfs = enforce;
1507 if (ppr == &prison0)
1508 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1510 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1511 ppr->pr_name, name);
1512 /* Change this component of child names. */
1513 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1514 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1515 strlen(tpr->pr_name + onamelen) + 1);
1516 bcopy(pr->pr_name, tpr->pr_name, namelen);
1520 /* Try to keep a real-rooted full pathname. */
1521 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1522 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1523 mypr->pr_path, path);
1525 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1528 if (PR_HOST & ch_flags & ~pr_flags) {
1529 if (pr->pr_flags & PR_HOST) {
1531 * Copy the parent's host info. As with pr_ip4 above,
1532 * the lack of a lock on the parent is not a problem;
1533 * it is always set with allprison_lock at least
1534 * shared, and is held exclusively here.
1536 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1537 sizeof(pr->pr_hostname));
1538 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1539 sizeof(pr->pr_domainname));
1540 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1541 sizeof(pr->pr_hostuuid));
1542 pr->pr_hostid = pr->pr_parent->pr_hostid;
1544 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1545 /* Set this prison, and any descendants without PR_HOST. */
1547 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1549 strlcpy(pr->pr_domainname, domain,
1550 sizeof(pr->pr_domainname));
1552 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1554 pr->pr_hostid = hid;
1555 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1556 if (tpr->pr_flags & PR_HOST)
1560 strlcpy(tpr->pr_hostname,
1562 sizeof(tpr->pr_hostname));
1564 strlcpy(tpr->pr_domainname,
1566 sizeof(tpr->pr_domainname));
1568 strlcpy(tpr->pr_hostuuid,
1570 sizeof(tpr->pr_hostuuid));
1572 tpr->pr_hostid = hid;
1576 if ((tallow = ch_allow & ~pr_allow)) {
1577 /* Clear allow bits in all children. */
1578 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1579 tpr->pr_allow &= ~tallow;
1581 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1583 * Persistent prisons get an extra reference, and prisons losing their
1584 * persist flag lose that reference. Only do this for existing prisons
1585 * for now, so new ones will remain unseen until after the module
1586 * handlers have completed.
1588 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1589 if (pr_flags & PR_PERSIST) {
1597 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1598 mtx_unlock(&pr->pr_mtx);
1600 /* Locks may have prevented a complete restriction of child IP
1601 * addresses. If so, allocate some more memory and try again.
1606 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1607 mtx_lock(&pr->pr_mtx);
1609 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1611 if (tpr->pr_flags & PR_VNET) {
1616 if (prison_restrict_ip4(tpr, ip4)) {
1623 mtx_unlock(&pr->pr_mtx);
1629 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1630 mtx_lock(&pr->pr_mtx);
1632 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1634 if (tpr->pr_flags & PR_VNET) {
1639 if (prison_restrict_ip6(tpr, ip6)) {
1646 mtx_unlock(&pr->pr_mtx);
1650 /* Let the modules do their work. */
1651 sx_downgrade(&allprison_lock);
1653 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1655 prison_deref(pr, PD_LIST_SLOCKED);
1659 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1661 prison_deref(pr, created
1663 : PD_DEREF | PD_LIST_SLOCKED);
1667 /* Attach this process to the prison if requested. */
1668 if (flags & JAIL_ATTACH) {
1669 mtx_lock(&pr->pr_mtx);
1670 error = do_jail_attach(td, pr);
1672 vfs_opterror(opts, "attach failed");
1674 prison_deref(pr, PD_DEREF);
1680 * Now that it is all there, drop the temporary reference from existing
1681 * prisons. Or add a reference to newly created persistent prisons
1682 * (which was not done earlier so that the prison would not be publicly
1686 prison_deref(pr, (flags & JAIL_ATTACH)
1688 : PD_DEREF | PD_LIST_SLOCKED);
1690 if (pr_flags & PR_PERSIST) {
1691 mtx_lock(&pr->pr_mtx);
1694 mtx_unlock(&pr->pr_mtx);
1696 if (!(flags & JAIL_ATTACH))
1697 sx_sunlock(&allprison_lock);
1699 td->td_retval[0] = pr->pr_id;
1703 prison_deref(pr, created
1704 ? PD_LOCKED | PD_LIST_XLOCKED
1705 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1708 sx_xunlock(&allprison_lock);
1711 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1713 VFS_UNLOCK_GIANT(vfslocked);
1717 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1718 if (errmsg_len > 0) {
1719 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1720 if (errmsg_pos > 0) {
1721 if (optuio->uio_segflg == UIO_SYSSPACE)
1723 optuio->uio_iov[errmsg_pos].iov_base,
1727 optuio->uio_iov[errmsg_pos].iov_base,
1734 free(ip4, M_PRISON);
1737 free(ip6, M_PRISON);
1745 * struct jail_get_args {
1746 * struct iovec *iovp;
1747 * unsigned int iovcnt;
1752 jail_get(struct thread *td, struct jail_get_args *uap)
1757 /* Check that we have an even number of iovecs. */
1758 if (uap->iovcnt & 1)
1761 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1764 error = kern_jail_get(td, auio, uap->flags);
1766 error = copyout(auio->uio_iov, uap->iovp,
1767 uap->iovcnt * sizeof (struct iovec));
1773 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1775 struct prison *pr, *mypr;
1777 struct vfsoptlist *opts;
1778 char *errmsg, *name;
1779 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1781 if (flags & ~JAIL_GET_MASK)
1784 /* Get the parameter list. */
1785 error = vfs_buildopts(optuio, &opts);
1788 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1789 mypr = td->td_ucred->cr_prison;
1792 * Find the prison specified by one of: lastjid, jid, name.
1794 sx_slock(&allprison_lock);
1795 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1797 TAILQ_FOREACH(pr, &allprison, pr_list) {
1798 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1799 mtx_lock(&pr->pr_mtx);
1800 if (pr->pr_ref > 0 &&
1801 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1803 mtx_unlock(&pr->pr_mtx);
1809 vfs_opterror(opts, "no jail after %d", jid);
1810 goto done_unlock_list;
1811 } else if (error != ENOENT)
1812 goto done_unlock_list;
1814 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1817 pr = prison_find_child(mypr, jid);
1819 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1820 mtx_unlock(&pr->pr_mtx);
1822 vfs_opterror(opts, "jail %d is dying",
1824 goto done_unlock_list;
1829 vfs_opterror(opts, "jail %d not found", jid);
1830 goto done_unlock_list;
1832 } else if (error != ENOENT)
1833 goto done_unlock_list;
1835 error = vfs_getopt(opts, "name", (void **)&name, &len);
1837 if (len == 0 || name[len - 1] != '\0') {
1839 goto done_unlock_list;
1841 pr = prison_find_name(mypr, name);
1843 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1844 mtx_unlock(&pr->pr_mtx);
1846 vfs_opterror(opts, "jail \"%s\" is dying",
1848 goto done_unlock_list;
1853 vfs_opterror(opts, "jail \"%s\" not found", name);
1854 goto done_unlock_list;
1855 } else if (error != ENOENT)
1856 goto done_unlock_list;
1858 vfs_opterror(opts, "no jail specified");
1860 goto done_unlock_list;
1863 /* Get the parameters of the prison. */
1866 td->td_retval[0] = pr->pr_id;
1867 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1868 if (error != 0 && error != ENOENT)
1870 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1871 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1872 if (error != 0 && error != ENOENT)
1874 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1875 if (error != 0 && error != ENOENT)
1877 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1878 sizeof(pr->pr_cpuset->cs_id));
1879 if (error != 0 && error != ENOENT)
1881 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1882 if (error != 0 && error != ENOENT)
1885 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1886 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1887 if (error != 0 && error != ENOENT)
1891 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1892 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1893 if (error != 0 && error != ENOENT)
1896 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1897 sizeof(pr->pr_securelevel));
1898 if (error != 0 && error != ENOENT)
1900 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1901 sizeof(pr->pr_childcount));
1902 if (error != 0 && error != ENOENT)
1904 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1905 sizeof(pr->pr_childmax));
1906 if (error != 0 && error != ENOENT)
1908 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1909 if (error != 0 && error != ENOENT)
1911 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1912 if (error != 0 && error != ENOENT)
1914 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1915 if (error != 0 && error != ENOENT)
1918 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1919 uint32_t hid32 = pr->pr_hostid;
1921 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1924 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1925 sizeof(pr->pr_hostid));
1926 if (error != 0 && error != ENOENT)
1928 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1929 sizeof(pr->pr_enforce_statfs));
1930 if (error != 0 && error != ENOENT)
1932 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1934 if (pr_flag_names[fi] == NULL)
1936 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1937 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1938 if (error != 0 && error != ENOENT)
1941 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1942 if (error != 0 && error != ENOENT)
1945 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1948 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1949 i = pr_flag_jailsys[fi].disable &&
1950 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1951 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
1954 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
1955 if (error != 0 && error != ENOENT)
1958 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
1960 if (pr_allow_names[fi] == NULL)
1962 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
1963 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
1964 if (error != 0 && error != ENOENT)
1967 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
1968 if (error != 0 && error != ENOENT)
1971 i = (pr->pr_uref == 0);
1972 error = vfs_setopt(opts, "dying", &i, sizeof(i));
1973 if (error != 0 && error != ENOENT)
1976 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1977 if (error != 0 && error != ENOENT)
1980 /* Get the module parameters. */
1981 mtx_unlock(&pr->pr_mtx);
1983 error = osd_jail_call(pr, PR_METHOD_GET, opts);
1986 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
1988 /* By now, all parameters should have been noted. */
1989 TAILQ_FOREACH(opt, opts, link) {
1990 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1992 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1997 /* Write the fetched parameters back to userspace. */
1999 TAILQ_FOREACH(opt, opts, link) {
2000 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2001 pos = 2 * opt->pos + 1;
2002 optuio->uio_iov[pos].iov_len = opt->len;
2003 if (opt->value != NULL) {
2004 if (optuio->uio_segflg == UIO_SYSSPACE) {
2006 optuio->uio_iov[pos].iov_base,
2009 error = copyout(opt->value,
2010 optuio->uio_iov[pos].iov_base,
2021 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2025 sx_sunlock(&allprison_lock);
2027 if (error && errmsg_pos >= 0) {
2028 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2029 errmsg_pos = 2 * errmsg_pos + 1;
2030 if (errmsg_len > 0) {
2031 if (optuio->uio_segflg == UIO_SYSSPACE)
2033 optuio->uio_iov[errmsg_pos].iov_base,
2037 optuio->uio_iov[errmsg_pos].iov_base,
2047 * struct jail_remove_args {
2052 jail_remove(struct thread *td, struct jail_remove_args *uap)
2054 struct prison *pr, *cpr, *lpr, *tpr;
2057 error = priv_check(td, PRIV_JAIL_REMOVE);
2061 sx_xlock(&allprison_lock);
2062 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2064 sx_xunlock(&allprison_lock);
2068 /* Remove all descendants of this prison, then remove this prison. */
2070 pr->pr_flags |= PR_REMOVE;
2071 if (!LIST_EMPTY(&pr->pr_children)) {
2072 mtx_unlock(&pr->pr_mtx);
2074 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2075 mtx_lock(&cpr->pr_mtx);
2076 if (cpr->pr_ref > 0) {
2079 cpr->pr_flags |= PR_REMOVE;
2081 /* Already removed - do not do it again. */
2084 mtx_unlock(&cpr->pr_mtx);
2086 mtx_lock(&lpr->pr_mtx);
2087 prison_remove_one(lpr);
2088 sx_xlock(&allprison_lock);
2093 mtx_lock(&lpr->pr_mtx);
2094 prison_remove_one(lpr);
2095 sx_xlock(&allprison_lock);
2097 mtx_lock(&pr->pr_mtx);
2099 prison_remove_one(pr);
2104 prison_remove_one(struct prison *pr)
2109 /* If the prison was persistent, it is not anymore. */
2111 if (pr->pr_flags & PR_PERSIST) {
2114 pr->pr_flags &= ~PR_PERSIST;
2118 * jail_remove added a reference. If that's the only one, remove
2121 KASSERT(pr->pr_ref > 0,
2122 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2123 if (pr->pr_ref == 1) {
2125 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2129 mtx_unlock(&pr->pr_mtx);
2130 sx_xunlock(&allprison_lock);
2132 * Kill all processes unfortunate enough to be attached to this prison.
2134 sx_slock(&allproc_lock);
2135 LIST_FOREACH(p, &allproc, p_list) {
2137 if (p->p_state != PRS_NEW && p->p_ucred &&
2138 p->p_ucred->cr_prison == pr)
2139 psignal(p, SIGKILL);
2142 sx_sunlock(&allproc_lock);
2143 /* Remove the temporary reference added by jail_remove. */
2144 prison_deref(pr, deuref | PD_DEREF);
2149 * struct jail_attach_args {
2154 jail_attach(struct thread *td, struct jail_attach_args *uap)
2159 error = priv_check(td, PRIV_JAIL_ATTACH);
2163 sx_slock(&allprison_lock);
2164 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2166 sx_sunlock(&allprison_lock);
2171 * Do not allow a process to attach to a prison that is not
2172 * considered to be "alive".
2174 if (pr->pr_uref == 0) {
2175 mtx_unlock(&pr->pr_mtx);
2176 sx_sunlock(&allprison_lock);
2180 return (do_jail_attach(td, pr));
2184 do_jail_attach(struct thread *td, struct prison *pr)
2188 struct ucred *newcred, *oldcred;
2189 int vfslocked, error;
2192 * XXX: Note that there is a slight race here if two threads
2193 * in the same privileged process attempt to attach to two
2194 * different jails at the same time. It is important for
2195 * user processes not to do this, or they might end up with
2196 * a process root from one prison, but attached to the jail
2201 mtx_unlock(&pr->pr_mtx);
2203 /* Let modules do whatever they need to prepare for attaching. */
2204 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2206 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2209 sx_sunlock(&allprison_lock);
2212 * Reparent the newly attached process to this jail.
2214 ppr = td->td_ucred->cr_prison;
2216 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2220 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2221 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2222 if ((error = change_dir(pr->pr_root, td)) != 0)
2225 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2228 VOP_UNLOCK(pr->pr_root, 0);
2229 if ((error = change_root(pr->pr_root, td)))
2230 goto e_unlock_giant;
2231 VFS_UNLOCK_GIANT(vfslocked);
2235 oldcred = p->p_ucred;
2237 crcopy(newcred, oldcred);
2238 newcred->cr_prison = pr;
2239 p->p_ucred = newcred;
2242 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2245 VOP_UNLOCK(pr->pr_root, 0);
2247 VFS_UNLOCK_GIANT(vfslocked);
2249 /* Tell modules this thread is still in its old jail after all. */
2250 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2251 prison_deref(pr, PD_DEREF | PD_DEUREF);
2257 * Returns a locked prison instance, or NULL on failure.
2260 prison_find(int prid)
2264 sx_assert(&allprison_lock, SX_LOCKED);
2265 TAILQ_FOREACH(pr, &allprison, pr_list) {
2266 if (pr->pr_id == prid) {
2267 mtx_lock(&pr->pr_mtx);
2270 mtx_unlock(&pr->pr_mtx);
2277 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2280 prison_find_child(struct prison *mypr, int prid)
2285 sx_assert(&allprison_lock, SX_LOCKED);
2286 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2287 if (pr->pr_id == prid) {
2288 mtx_lock(&pr->pr_mtx);
2291 mtx_unlock(&pr->pr_mtx);
2298 * Look for the name relative to mypr. Returns a locked prison or NULL.
2301 prison_find_name(struct prison *mypr, const char *name)
2303 struct prison *pr, *deadpr;
2307 sx_assert(&allprison_lock, SX_LOCKED);
2308 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2311 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2312 if (!strcmp(pr->pr_name + mylen, name)) {
2313 mtx_lock(&pr->pr_mtx);
2314 if (pr->pr_ref > 0) {
2315 if (pr->pr_uref > 0)
2319 mtx_unlock(&pr->pr_mtx);
2322 /* There was no valid prison - perhaps there was a dying one. */
2323 if (deadpr != NULL) {
2324 mtx_lock(&deadpr->pr_mtx);
2325 if (deadpr->pr_ref == 0) {
2326 mtx_unlock(&deadpr->pr_mtx);
2334 * See if a prison has the specific flag set.
2337 prison_flag(struct ucred *cred, unsigned flag)
2340 /* This is an atomic read, so no locking is necessary. */
2341 return (cred->cr_prison->pr_flags & flag);
2345 prison_allow(struct ucred *cred, unsigned flag)
2348 /* This is an atomic read, so no locking is necessary. */
2349 return (cred->cr_prison->pr_allow & flag);
2353 * Remove a prison reference. If that was the last reference, remove the
2354 * prison itself - but not in this context in case there are locks held.
2357 prison_free_locked(struct prison *pr)
2360 mtx_assert(&pr->pr_mtx, MA_OWNED);
2362 if (pr->pr_ref == 0) {
2363 mtx_unlock(&pr->pr_mtx);
2364 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2365 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2368 mtx_unlock(&pr->pr_mtx);
2372 prison_free(struct prison *pr)
2375 mtx_lock(&pr->pr_mtx);
2376 prison_free_locked(pr);
2380 prison_complete(void *context, int pending)
2383 prison_deref((struct prison *)context, 0);
2387 * Remove a prison reference (usually). This internal version assumes no
2388 * mutexes are held, except perhaps the prison itself. If there are no more
2389 * references, release and delist the prison. On completion, the prison lock
2390 * and the allprison lock are both unlocked.
2393 prison_deref(struct prison *pr, int flags)
2395 struct prison *ppr, *tpr;
2398 if (!(flags & PD_LOCKED))
2399 mtx_lock(&pr->pr_mtx);
2400 /* Decrement the user references in a separate loop. */
2401 if (flags & PD_DEUREF) {
2402 for (tpr = pr;; tpr = tpr->pr_parent) {
2404 mtx_lock(&tpr->pr_mtx);
2405 if (--tpr->pr_uref > 0)
2407 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2408 mtx_unlock(&tpr->pr_mtx);
2410 /* Done if there were only user references to remove. */
2411 if (!(flags & PD_DEREF)) {
2412 mtx_unlock(&tpr->pr_mtx);
2413 if (flags & PD_LIST_SLOCKED)
2414 sx_sunlock(&allprison_lock);
2415 else if (flags & PD_LIST_XLOCKED)
2416 sx_xunlock(&allprison_lock);
2420 mtx_unlock(&tpr->pr_mtx);
2421 mtx_lock(&pr->pr_mtx);
2426 if (flags & PD_DEREF)
2428 /* If the prison still has references, nothing else to do. */
2429 if (pr->pr_ref > 0) {
2430 mtx_unlock(&pr->pr_mtx);
2431 if (flags & PD_LIST_SLOCKED)
2432 sx_sunlock(&allprison_lock);
2433 else if (flags & PD_LIST_XLOCKED)
2434 sx_xunlock(&allprison_lock);
2438 mtx_unlock(&pr->pr_mtx);
2439 if (flags & PD_LIST_SLOCKED) {
2440 if (!sx_try_upgrade(&allprison_lock)) {
2441 sx_sunlock(&allprison_lock);
2442 sx_xlock(&allprison_lock);
2444 } else if (!(flags & PD_LIST_XLOCKED))
2445 sx_xlock(&allprison_lock);
2447 TAILQ_REMOVE(&allprison, pr, pr_list);
2448 LIST_REMOVE(pr, pr_sibling);
2449 ppr = pr->pr_parent;
2450 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2451 tpr->pr_childcount--;
2452 sx_downgrade(&allprison_lock);
2455 if (pr->pr_flags & PR_VNET)
2456 vnet_destroy(pr->pr_vnet);
2458 if (pr->pr_root != NULL) {
2459 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2461 VFS_UNLOCK_GIANT(vfslocked);
2463 mtx_destroy(&pr->pr_mtx);
2465 free(pr->pr_ip4, M_PRISON);
2468 free(pr->pr_ip6, M_PRISON);
2470 if (pr->pr_cpuset != NULL)
2471 cpuset_rel(pr->pr_cpuset);
2475 /* Removing a prison frees a reference on its parent. */
2477 mtx_lock(&pr->pr_mtx);
2478 flags = PD_DEREF | PD_LIST_SLOCKED;
2483 prison_hold_locked(struct prison *pr)
2486 mtx_assert(&pr->pr_mtx, MA_OWNED);
2487 KASSERT(pr->pr_ref > 0,
2488 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2493 prison_hold(struct prison *pr)
2496 mtx_lock(&pr->pr_mtx);
2497 prison_hold_locked(pr);
2498 mtx_unlock(&pr->pr_mtx);
2502 prison_proc_hold(struct prison *pr)
2505 mtx_lock(&pr->pr_mtx);
2506 KASSERT(pr->pr_uref > 0,
2507 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2509 mtx_unlock(&pr->pr_mtx);
2513 prison_proc_free(struct prison *pr)
2516 mtx_lock(&pr->pr_mtx);
2517 KASSERT(pr->pr_uref > 0,
2518 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2519 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2525 * Restrict a prison's IP address list with its parent's, possibly replacing
2526 * it. Return true if the replacement buffer was used (or would have been).
2529 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2534 ppr = pr->pr_parent;
2535 if (!(pr->pr_flags & PR_IP4_USER)) {
2536 /* This has no user settings, so just copy the parent's list. */
2537 if (pr->pr_ip4s < ppr->pr_ip4s) {
2539 * There's no room for the parent's list. Use the
2540 * new list buffer, which is assumed to be big enough
2541 * (if it was passed). If there's no buffer, try to
2545 if (newip4 == NULL) {
2546 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2547 M_PRISON, M_NOWAIT);
2551 if (newip4 != NULL) {
2552 bcopy(ppr->pr_ip4, newip4,
2553 ppr->pr_ip4s * sizeof(*newip4));
2554 free(pr->pr_ip4, M_PRISON);
2555 pr->pr_ip4 = newip4;
2556 pr->pr_ip4s = ppr->pr_ip4s;
2560 pr->pr_ip4s = ppr->pr_ip4s;
2561 if (pr->pr_ip4s > 0)
2562 bcopy(ppr->pr_ip4, pr->pr_ip4,
2563 pr->pr_ip4s * sizeof(*newip4));
2564 else if (pr->pr_ip4 != NULL) {
2565 free(pr->pr_ip4, M_PRISON);
2568 } else if (pr->pr_ip4s > 0) {
2569 /* Remove addresses that aren't in the parent. */
2570 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2571 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2573 if (ij < ppr->pr_ip4s)
2576 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2577 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2580 for (ij = 1; ii < pr->pr_ip4s; ) {
2581 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2585 switch (ij >= ppr->pr_ip4s ? -1 :
2586 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2588 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2589 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2600 if (pr->pr_ip4s == 0) {
2601 pr->pr_flags |= PR_IP4_DISABLE;
2602 free(pr->pr_ip4, M_PRISON);
2610 * Pass back primary IPv4 address of this jail.
2612 * If not restricted return success but do not alter the address. Caller has
2613 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2615 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2616 * Address returned in NBO.
2619 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2623 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2624 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2626 pr = cred->cr_prison;
2627 if (!(pr->pr_flags & PR_IP4))
2629 mtx_lock(&pr->pr_mtx);
2630 if (!(pr->pr_flags & PR_IP4)) {
2631 mtx_unlock(&pr->pr_mtx);
2634 if (pr->pr_ip4 == NULL) {
2635 mtx_unlock(&pr->pr_mtx);
2636 return (EAFNOSUPPORT);
2639 ia->s_addr = pr->pr_ip4[0].s_addr;
2640 mtx_unlock(&pr->pr_mtx);
2645 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2648 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2655 * No need to lock since the PR_IP4_USER flag can't be altered for
2658 while (pr1 != &prison0 &&
2660 !(pr1->pr_flags & PR_VNET) &&
2662 !(pr1->pr_flags & PR_IP4_USER))
2663 pr1 = pr1->pr_parent;
2664 while (pr2 != &prison0 &&
2666 !(pr2->pr_flags & PR_VNET) &&
2668 !(pr2->pr_flags & PR_IP4_USER))
2669 pr2 = pr2->pr_parent;
2670 return (pr1 == pr2);
2674 * Make sure our (source) address is set to something meaningful to this
2677 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2678 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2679 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2682 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2688 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2689 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2691 pr = cred->cr_prison;
2692 if (!(pr->pr_flags & PR_IP4))
2694 mtx_lock(&pr->pr_mtx);
2695 if (!(pr->pr_flags & PR_IP4)) {
2696 mtx_unlock(&pr->pr_mtx);
2699 if (pr->pr_ip4 == NULL) {
2700 mtx_unlock(&pr->pr_mtx);
2701 return (EAFNOSUPPORT);
2704 ia0.s_addr = ntohl(ia->s_addr);
2705 if (ia0.s_addr == INADDR_LOOPBACK) {
2706 ia->s_addr = pr->pr_ip4[0].s_addr;
2707 mtx_unlock(&pr->pr_mtx);
2711 if (ia0.s_addr == INADDR_ANY) {
2713 * In case there is only 1 IPv4 address, bind directly.
2715 if (pr->pr_ip4s == 1)
2716 ia->s_addr = pr->pr_ip4[0].s_addr;
2717 mtx_unlock(&pr->pr_mtx);
2721 error = _prison_check_ip4(pr, ia);
2722 mtx_unlock(&pr->pr_mtx);
2727 * Rewrite destination address in case we will connect to loopback address.
2729 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2730 * Address passed in in NBO and returned in NBO.
2733 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2737 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2738 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2740 pr = cred->cr_prison;
2741 if (!(pr->pr_flags & PR_IP4))
2743 mtx_lock(&pr->pr_mtx);
2744 if (!(pr->pr_flags & PR_IP4)) {
2745 mtx_unlock(&pr->pr_mtx);
2748 if (pr->pr_ip4 == NULL) {
2749 mtx_unlock(&pr->pr_mtx);
2750 return (EAFNOSUPPORT);
2753 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2754 ia->s_addr = pr->pr_ip4[0].s_addr;
2755 mtx_unlock(&pr->pr_mtx);
2760 * Return success because nothing had to be changed.
2762 mtx_unlock(&pr->pr_mtx);
2767 * Check if given address belongs to the jail referenced by cred/prison.
2769 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2770 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2771 * doesn't allow IPv4. Address passed in in NBO.
2774 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2779 * Check the primary IP.
2781 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2785 * All the other IPs are sorted so we can do a binary search.
2788 z = pr->pr_ip4s - 2;
2791 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2800 return (EADDRNOTAVAIL);
2804 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2809 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2810 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2812 pr = cred->cr_prison;
2813 if (!(pr->pr_flags & PR_IP4))
2815 mtx_lock(&pr->pr_mtx);
2816 if (!(pr->pr_flags & PR_IP4)) {
2817 mtx_unlock(&pr->pr_mtx);
2820 if (pr->pr_ip4 == NULL) {
2821 mtx_unlock(&pr->pr_mtx);
2822 return (EAFNOSUPPORT);
2825 error = _prison_check_ip4(pr, ia);
2826 mtx_unlock(&pr->pr_mtx);
2833 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2838 ppr = pr->pr_parent;
2839 if (!(pr->pr_flags & PR_IP6_USER)) {
2840 /* This has no user settings, so just copy the parent's list. */
2841 if (pr->pr_ip6s < ppr->pr_ip6s) {
2843 * There's no room for the parent's list. Use the
2844 * new list buffer, which is assumed to be big enough
2845 * (if it was passed). If there's no buffer, try to
2849 if (newip6 == NULL) {
2850 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2851 M_PRISON, M_NOWAIT);
2855 if (newip6 != NULL) {
2856 bcopy(ppr->pr_ip6, newip6,
2857 ppr->pr_ip6s * sizeof(*newip6));
2858 free(pr->pr_ip6, M_PRISON);
2859 pr->pr_ip6 = newip6;
2860 pr->pr_ip6s = ppr->pr_ip6s;
2864 pr->pr_ip6s = ppr->pr_ip6s;
2865 if (pr->pr_ip6s > 0)
2866 bcopy(ppr->pr_ip6, pr->pr_ip6,
2867 pr->pr_ip6s * sizeof(*newip6));
2868 else if (pr->pr_ip6 != NULL) {
2869 free(pr->pr_ip6, M_PRISON);
2872 } else if (pr->pr_ip6s > 0) {
2873 /* Remove addresses that aren't in the parent. */
2874 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2875 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2878 if (ij < ppr->pr_ip6s)
2881 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2882 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2885 for (ij = 1; ii < pr->pr_ip6s; ) {
2886 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2891 switch (ij >= ppr->pr_ip4s ? -1 :
2892 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2894 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2895 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2906 if (pr->pr_ip6s == 0) {
2907 pr->pr_flags |= PR_IP6_DISABLE;
2908 free(pr->pr_ip6, M_PRISON);
2916 * Pass back primary IPv6 address for this jail.
2918 * If not restricted return success but do not alter the address. Caller has
2919 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2921 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2924 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2928 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2929 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2931 pr = cred->cr_prison;
2932 if (!(pr->pr_flags & PR_IP6))
2934 mtx_lock(&pr->pr_mtx);
2935 if (!(pr->pr_flags & PR_IP6)) {
2936 mtx_unlock(&pr->pr_mtx);
2939 if (pr->pr_ip6 == NULL) {
2940 mtx_unlock(&pr->pr_mtx);
2941 return (EAFNOSUPPORT);
2944 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2945 mtx_unlock(&pr->pr_mtx);
2950 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
2953 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
2959 while (pr1 != &prison0 &&
2961 !(pr1->pr_flags & PR_VNET) &&
2963 !(pr1->pr_flags & PR_IP6_USER))
2964 pr1 = pr1->pr_parent;
2965 while (pr2 != &prison0 &&
2967 !(pr2->pr_flags & PR_VNET) &&
2969 !(pr2->pr_flags & PR_IP6_USER))
2970 pr2 = pr2->pr_parent;
2971 return (pr1 == pr2);
2975 * Make sure our (source) address is set to something meaningful to this jail.
2977 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2978 * when needed while binding.
2980 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
2981 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2982 * doesn't allow IPv6.
2985 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
2990 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2991 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2993 pr = cred->cr_prison;
2994 if (!(pr->pr_flags & PR_IP6))
2996 mtx_lock(&pr->pr_mtx);
2997 if (!(pr->pr_flags & PR_IP6)) {
2998 mtx_unlock(&pr->pr_mtx);
3001 if (pr->pr_ip6 == NULL) {
3002 mtx_unlock(&pr->pr_mtx);
3003 return (EAFNOSUPPORT);
3006 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3007 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3008 mtx_unlock(&pr->pr_mtx);
3012 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3014 * In case there is only 1 IPv6 address, and v6only is true,
3015 * then bind directly.
3017 if (v6only != 0 && pr->pr_ip6s == 1)
3018 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3019 mtx_unlock(&pr->pr_mtx);
3023 error = _prison_check_ip6(pr, ia6);
3024 mtx_unlock(&pr->pr_mtx);
3029 * Rewrite destination address in case we will connect to loopback address.
3031 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3034 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3038 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3039 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3041 pr = cred->cr_prison;
3042 if (!(pr->pr_flags & PR_IP6))
3044 mtx_lock(&pr->pr_mtx);
3045 if (!(pr->pr_flags & PR_IP6)) {
3046 mtx_unlock(&pr->pr_mtx);
3049 if (pr->pr_ip6 == NULL) {
3050 mtx_unlock(&pr->pr_mtx);
3051 return (EAFNOSUPPORT);
3054 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3055 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3056 mtx_unlock(&pr->pr_mtx);
3061 * Return success because nothing had to be changed.
3063 mtx_unlock(&pr->pr_mtx);
3068 * Check if given address belongs to the jail referenced by cred/prison.
3070 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3071 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3072 * doesn't allow IPv6.
3075 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3080 * Check the primary IP.
3082 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3086 * All the other IPs are sorted so we can do a binary search.
3089 z = pr->pr_ip6s - 2;
3092 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3101 return (EADDRNOTAVAIL);
3105 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3110 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3111 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3113 pr = cred->cr_prison;
3114 if (!(pr->pr_flags & PR_IP6))
3116 mtx_lock(&pr->pr_mtx);
3117 if (!(pr->pr_flags & PR_IP6)) {
3118 mtx_unlock(&pr->pr_mtx);
3121 if (pr->pr_ip6 == NULL) {
3122 mtx_unlock(&pr->pr_mtx);
3123 return (EAFNOSUPPORT);
3126 error = _prison_check_ip6(pr, ia6);
3127 mtx_unlock(&pr->pr_mtx);
3133 * Check if a jail supports the given address family.
3135 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3139 prison_check_af(struct ucred *cred, int af)
3144 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3146 pr = cred->cr_prison;
3148 /* Prisons with their own network stack are not limited. */
3149 if (pr->pr_flags & PR_VNET)
3158 if (pr->pr_flags & PR_IP4)
3160 mtx_lock(&pr->pr_mtx);
3161 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3162 error = EAFNOSUPPORT;
3163 mtx_unlock(&pr->pr_mtx);
3169 if (pr->pr_flags & PR_IP6)
3171 mtx_lock(&pr->pr_mtx);
3172 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3173 error = EAFNOSUPPORT;
3174 mtx_unlock(&pr->pr_mtx);
3182 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3183 error = EAFNOSUPPORT;
3189 * Check if given address belongs to the jail referenced by cred (wrapper to
3190 * prison_check_ip[46]).
3192 * Returns 0 if jail doesn't restrict the address family or if address belongs
3193 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3194 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3197 prison_if(struct ucred *cred, struct sockaddr *sa)
3200 struct sockaddr_in *sai;
3203 struct sockaddr_in6 *sai6;
3207 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3208 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3211 switch (sa->sa_family)
3215 sai = (struct sockaddr_in *)sa;
3216 error = prison_check_ip4(cred, &sai->sin_addr);
3221 sai6 = (struct sockaddr_in6 *)sa;
3222 error = prison_check_ip6(cred, &sai6->sin6_addr);
3226 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3227 error = EAFNOSUPPORT;
3233 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3236 prison_check(struct ucred *cred1, struct ucred *cred2)
3239 return ((cred1->cr_prison == cred2->cr_prison ||
3240 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3244 * Return 1 if p2 is a child of p1, otherwise 0.
3247 prison_ischild(struct prison *pr1, struct prison *pr2)
3250 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3257 * Return 1 if the passed credential is in a jail, otherwise 0.
3260 jailed(struct ucred *cred)
3263 return (cred->cr_prison != &prison0);
3267 * Return the correct hostname (domainname, et al) for the passed credential.
3270 getcredhostname(struct ucred *cred, char *buf, size_t size)
3275 * A NULL credential can be used to shortcut to the physical
3276 * system's hostname.
3278 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3279 mtx_lock(&pr->pr_mtx);
3280 strlcpy(buf, pr->pr_hostname, size);
3281 mtx_unlock(&pr->pr_mtx);
3285 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3288 mtx_lock(&cred->cr_prison->pr_mtx);
3289 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3290 mtx_unlock(&cred->cr_prison->pr_mtx);
3294 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3297 mtx_lock(&cred->cr_prison->pr_mtx);
3298 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3299 mtx_unlock(&cred->cr_prison->pr_mtx);
3303 getcredhostid(struct ucred *cred, unsigned long *hostid)
3306 mtx_lock(&cred->cr_prison->pr_mtx);
3307 *hostid = cred->cr_prison->pr_hostid;
3308 mtx_unlock(&cred->cr_prison->pr_mtx);
3312 * Determine whether the subject represented by cred can "see"
3313 * status of a mount point.
3314 * Returns: 0 for permitted, ENOENT otherwise.
3315 * XXX: This function should be called cr_canseemount() and should be
3316 * placed in kern_prot.c.
3319 prison_canseemount(struct ucred *cred, struct mount *mp)
3325 pr = cred->cr_prison;
3326 if (pr->pr_enforce_statfs == 0)
3328 if (pr->pr_root->v_mount == mp)
3330 if (pr->pr_enforce_statfs == 2)
3333 * If jail's chroot directory is set to "/" we should be able to see
3334 * all mount-points from inside a jail.
3335 * This is ugly check, but this is the only situation when jail's
3336 * directory ends with '/'.
3338 if (strcmp(pr->pr_path, "/") == 0)
3340 len = strlen(pr->pr_path);
3342 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3345 * Be sure that we don't have situation where jail's root directory
3346 * is "/some/path" and mount point is "/some/pathpath".
3348 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3354 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3356 char jpath[MAXPATHLEN];
3360 pr = cred->cr_prison;
3361 if (pr->pr_enforce_statfs == 0)
3363 if (prison_canseemount(cred, mp) != 0) {
3364 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3365 strlcpy(sp->f_mntonname, "[restricted]",
3366 sizeof(sp->f_mntonname));
3369 if (pr->pr_root->v_mount == mp) {
3371 * Clear current buffer data, so we are sure nothing from
3372 * the valid path left there.
3374 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3375 *sp->f_mntonname = '/';
3379 * If jail's chroot directory is set to "/" we should be able to see
3380 * all mount-points from inside a jail.
3382 if (strcmp(pr->pr_path, "/") == 0)
3384 len = strlen(pr->pr_path);
3385 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3387 * Clear current buffer data, so we are sure nothing from
3388 * the valid path left there.
3390 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3391 if (*jpath == '\0') {
3392 /* Should never happen. */
3393 *sp->f_mntonname = '/';
3395 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3400 * Check with permission for a specific privilege is granted within jail. We
3401 * have a specific list of accepted privileges; the rest are denied.
3404 prison_priv_check(struct ucred *cred, int priv)
3412 * Privileges specific to prisons with a virtual network stack.
3413 * There might be a duplicate entry here in case the privilege
3414 * is only granted conditionally in the legacy jail case.
3419 * NFS-specific privileges.
3421 case PRIV_NFS_DAEMON:
3422 case PRIV_NFS_LOCKD:
3425 * Network stack privileges.
3427 case PRIV_NET_BRIDGE:
3430 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3431 case PRIV_NET_ROUTE:
3433 case PRIV_NET_SETIFMTU:
3434 case PRIV_NET_SETIFFLAGS:
3435 case PRIV_NET_SETIFCAP:
3436 case PRIV_NET_SETIFNAME :
3437 case PRIV_NET_SETIFMETRIC:
3438 case PRIV_NET_SETIFPHYS:
3439 case PRIV_NET_SETIFMAC:
3440 case PRIV_NET_ADDMULTI:
3441 case PRIV_NET_DELMULTI:
3442 case PRIV_NET_HWIOCTL:
3443 case PRIV_NET_SETLLADDR:
3444 case PRIV_NET_ADDIFGROUP:
3445 case PRIV_NET_DELIFGROUP:
3446 case PRIV_NET_IFCREATE:
3447 case PRIV_NET_IFDESTROY:
3448 case PRIV_NET_ADDIFADDR:
3449 case PRIV_NET_DELIFADDR:
3452 case PRIV_NET_SETIFVNET:
3455 * 802.11-related privileges.
3457 case PRIV_NET80211_GETKEY:
3459 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3464 * AppleTalk privileges.
3466 case PRIV_NETATALK_RESERVEDPORT:
3471 case PRIV_NETATM_CFG:
3472 case PRIV_NETATM_ADD:
3473 case PRIV_NETATM_DEL:
3474 case PRIV_NETATM_SET:
3477 * Bluetooth privileges.
3479 case PRIV_NETBLUETOOTH_RAW:
3483 * Netgraph and netgraph module privileges.
3485 case PRIV_NETGRAPH_CONTROL:
3487 case PRIV_NETGRAPH_TTY:
3491 * IPv4 and IPv6 privileges.
3493 case PRIV_NETINET_IPFW:
3494 case PRIV_NETINET_DIVERT:
3495 case PRIV_NETINET_PF:
3496 case PRIV_NETINET_DUMMYNET:
3497 case PRIV_NETINET_CARP:
3498 case PRIV_NETINET_MROUTE:
3499 case PRIV_NETINET_RAW:
3500 case PRIV_NETINET_ADDRCTRL6:
3501 case PRIV_NETINET_ND6:
3502 case PRIV_NETINET_SCOPE6:
3503 case PRIV_NETINET_ALIFETIME6:
3504 case PRIV_NETINET_IPSEC:
3505 case PRIV_NETINET_BINDANY:
3509 * IPX/SPX privileges.
3511 case PRIV_NETIPX_RESERVEDPORT:
3512 case PRIV_NETIPX_RAW:
3526 * No default: or deny here.
3527 * In case of no permit fall through to next switch().
3529 if (cred->cr_prison->pr_flags & PR_VNET)
3537 * Allow ktrace privileges for root in jail.
3543 * Allow jailed processes to configure audit identity and
3544 * submit audit records (login, etc). In the future we may
3545 * want to further refine the relationship between audit and
3548 case PRIV_AUDIT_GETAUDIT:
3549 case PRIV_AUDIT_SETAUDIT:
3550 case PRIV_AUDIT_SUBMIT:
3554 * Allow jailed processes to manipulate process UNIX
3555 * credentials in any way they see fit.
3557 case PRIV_CRED_SETUID:
3558 case PRIV_CRED_SETEUID:
3559 case PRIV_CRED_SETGID:
3560 case PRIV_CRED_SETEGID:
3561 case PRIV_CRED_SETGROUPS:
3562 case PRIV_CRED_SETREUID:
3563 case PRIV_CRED_SETREGID:
3564 case PRIV_CRED_SETRESUID:
3565 case PRIV_CRED_SETRESGID:
3568 * Jail implements visibility constraints already, so allow
3569 * jailed root to override uid/gid-based constraints.
3571 case PRIV_SEEOTHERGIDS:
3572 case PRIV_SEEOTHERUIDS:
3575 * Jail implements inter-process debugging limits already, so
3576 * allow jailed root various debugging privileges.
3578 case PRIV_DEBUG_DIFFCRED:
3579 case PRIV_DEBUG_SUGID:
3580 case PRIV_DEBUG_UNPRIV:
3583 * Allow jail to set various resource limits and login
3584 * properties, and for now, exceed process resource limits.
3586 case PRIV_PROC_LIMIT:
3587 case PRIV_PROC_SETLOGIN:
3588 case PRIV_PROC_SETRLIMIT:
3591 * System V and POSIX IPC privileges are granted in jail.
3594 case PRIV_IPC_WRITE:
3595 case PRIV_IPC_ADMIN:
3596 case PRIV_IPC_MSGSIZE:
3600 * Jail operations within a jail work on child jails.
3602 case PRIV_JAIL_ATTACH:
3604 case PRIV_JAIL_REMOVE:
3607 * Jail implements its own inter-process limits, so allow
3608 * root processes in jail to change scheduling on other
3609 * processes in the same jail. Likewise for signalling.
3611 case PRIV_SCHED_DIFFCRED:
3612 case PRIV_SCHED_CPUSET:
3613 case PRIV_SIGNAL_DIFFCRED:
3614 case PRIV_SIGNAL_SUGID:
3617 * Allow jailed processes to write to sysctls marked as jail
3620 case PRIV_SYSCTL_WRITEJAIL:
3623 * Allow root in jail to manage a variety of quota
3624 * properties. These should likely be conditional on a
3625 * configuration option.
3627 case PRIV_VFS_GETQUOTA:
3628 case PRIV_VFS_SETQUOTA:
3631 * Since Jail relies on chroot() to implement file system
3632 * protections, grant many VFS privileges to root in jail.
3633 * Be careful to exclude mount-related and NFS-related
3637 case PRIV_VFS_WRITE:
3638 case PRIV_VFS_ADMIN:
3640 case PRIV_VFS_LOOKUP:
3641 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3642 case PRIV_VFS_CHFLAGS_DEV:
3643 case PRIV_VFS_CHOWN:
3644 case PRIV_VFS_CHROOT:
3645 case PRIV_VFS_RETAINSUGID:
3646 case PRIV_VFS_FCHROOT:
3648 case PRIV_VFS_SETGID:
3650 case PRIV_VFS_STICKYFILE:
3654 * Depending on the global setting, allow privilege of
3655 * setting system flags.
3657 case PRIV_VFS_SYSFLAGS:
3658 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3664 * Depending on the global setting, allow privilege of
3665 * mounting/unmounting file systems.
3667 case PRIV_VFS_MOUNT:
3668 case PRIV_VFS_UNMOUNT:
3669 case PRIV_VFS_MOUNT_NONUSER:
3670 case PRIV_VFS_MOUNT_OWNER:
3671 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3677 * Allow jailed root to bind reserved ports and reuse in-use
3680 case PRIV_NETINET_RESERVEDPORT:
3681 case PRIV_NETINET_REUSEPORT:
3685 * Allow jailed root to set certian IPv4/6 (option) headers.
3687 case PRIV_NETINET_SETHDROPTS:
3691 * Conditionally allow creating raw sockets in jail.
3693 case PRIV_NETINET_RAW:
3694 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3700 * Since jail implements its own visibility limits on netstat
3701 * sysctls, allow getcred. This allows identd to work in
3704 case PRIV_NETINET_GETCRED:
3709 * In all remaining cases, deny the privilege request. This
3710 * includes almost all network privileges, many system
3711 * configuration privileges.
3718 * Return the part of pr2's name that is relative to pr1, or the whole name
3719 * if it does not directly follow.
3723 prison_name(struct prison *pr1, struct prison *pr2)
3727 /* Jails see themselves as "0" (if they see themselves at all). */
3730 name = pr2->pr_name;
3731 if (prison_ischild(pr1, pr2)) {
3733 * pr1 isn't locked (and allprison_lock may not be either)
3734 * so its length can't be counted on. But the number of dots
3735 * can be counted on - and counted.
3737 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3738 name = strchr(name, '.') + 1;
3744 * Return the part of pr2's path that is relative to pr1, or the whole path
3745 * if it does not directly follow.
3748 prison_path(struct prison *pr1, struct prison *pr2)
3750 char *path1, *path2;
3753 path1 = pr1->pr_path;
3754 path2 = pr2->pr_path;
3755 if (!strcmp(path1, "/"))
3757 len1 = strlen(path1);
3758 if (strncmp(path1, path2, len1))
3760 if (path2[len1] == '\0')
3762 if (path2[len1] == '/')
3763 return (path2 + len1);
3769 * Jail-related sysctls.
3771 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3775 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3778 struct prison *pr, *cpr;
3780 struct in_addr *ip4 = NULL;
3784 struct in_addr *ip6 = NULL;
3789 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3790 pr = req->td->td_ucred->cr_prison;
3792 sx_slock(&allprison_lock);
3793 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3794 #if defined(INET) || defined(INET6)
3797 mtx_lock(&cpr->pr_mtx);
3799 if (cpr->pr_ip4s > 0) {
3800 if (ip4s < cpr->pr_ip4s) {
3801 ip4s = cpr->pr_ip4s;
3802 mtx_unlock(&cpr->pr_mtx);
3803 ip4 = realloc(ip4, ip4s *
3804 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3807 bcopy(cpr->pr_ip4, ip4,
3808 cpr->pr_ip4s * sizeof(struct in_addr));
3812 if (cpr->pr_ip6s > 0) {
3813 if (ip6s < cpr->pr_ip6s) {
3814 ip6s = cpr->pr_ip6s;
3815 mtx_unlock(&cpr->pr_mtx);
3816 ip6 = realloc(ip6, ip6s *
3817 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3820 bcopy(cpr->pr_ip6, ip6,
3821 cpr->pr_ip6s * sizeof(struct in6_addr));
3824 if (cpr->pr_ref == 0) {
3825 mtx_unlock(&cpr->pr_mtx);
3828 bzero(xp, sizeof(*xp));
3829 xp->pr_version = XPRISON_VERSION;
3830 xp->pr_id = cpr->pr_id;
3831 xp->pr_state = cpr->pr_uref > 0
3832 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3833 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3834 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3835 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3837 xp->pr_ip4s = cpr->pr_ip4s;
3840 xp->pr_ip6s = cpr->pr_ip6s;
3842 mtx_unlock(&cpr->pr_mtx);
3843 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3847 if (xp->pr_ip4s > 0) {
3848 error = SYSCTL_OUT(req, ip4,
3849 xp->pr_ip4s * sizeof(struct in_addr));
3855 if (xp->pr_ip6s > 0) {
3856 error = SYSCTL_OUT(req, ip6,
3857 xp->pr_ip6s * sizeof(struct in6_addr));
3863 sx_sunlock(&allprison_lock);
3874 SYSCTL_OID(_security_jail, OID_AUTO, list,
3875 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3876 sysctl_jail_list, "S", "List of active jails");
3879 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3883 injail = jailed(req->td->td_ucred);
3884 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3889 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3890 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3891 sysctl_jail_jailed, "I", "Process in jail?");
3893 #if defined(INET) || defined(INET6)
3894 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3895 &jail_max_af_ips, 0,
3896 "Number of IP addresses a jail may have at most per address family");
3900 * Default parameters for jail(2) compatability. For historical reasons,
3901 * the sysctl names have varying similarity to the parameter names. Prisons
3902 * just see their own parameters, and can't change them.
3905 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3908 int allow, error, i;
3910 pr = req->td->td_ucred->cr_prison;
3911 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
3913 /* Get the current flag value, and convert it to a boolean. */
3914 i = (allow & arg2) ? 1 : 0;
3917 error = sysctl_handle_int(oidp, &i, 0, req);
3918 if (error || !req->newptr)
3924 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3927 mtx_lock(&prison0.pr_mtx);
3928 jail_default_allow = (jail_default_allow & ~arg2) | i;
3929 mtx_unlock(&prison0.pr_mtx);
3933 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3934 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3935 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3936 "Processes in jail can set their hostnames");
3937 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3938 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3939 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3940 "Processes in jail are limited to creating UNIX/IP/route sockets only");
3941 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3942 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3943 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3944 "Processes in jail can use System V IPC primitives");
3945 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3946 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3947 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3948 "Prison root can create raw sockets");
3949 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3950 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3951 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3952 "Processes in jail can alter system file flags");
3953 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3954 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3955 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3956 "Processes in jail can mount/unmount jail-friendly file systems");
3959 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3964 pr = req->td->td_ucred->cr_prison;
3965 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
3966 error = sysctl_handle_int(oidp, &level, 0, req);
3967 if (error || !req->newptr)
3969 *(int *)arg1 = level;
3973 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3974 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3975 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
3976 sysctl_jail_default_level, "I",
3977 "Processes in jail cannot see all mounted file systems");
3980 * Nodes to describe jail parameters. Maximum length of string parameters
3981 * is returned in the string itself, and the other parameters exist merely
3982 * to make themselves and their types known.
3984 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
3988 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
3995 switch (oidp->oid_kind & CTLTYPE)
4001 if (!(req->flags & SCTL_MASK32))
4003 return (SYSCTL_OUT(req, &l, sizeof(l)));
4007 return (SYSCTL_OUT(req, &i, sizeof(i)));
4008 case CTLTYPE_STRING:
4009 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4011 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4012 case CTLTYPE_STRUCT:
4014 return (SYSCTL_OUT(req, &s, sizeof(s)));
4019 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4020 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4021 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4022 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4023 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4024 "I", "Jail secure level");
4025 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4026 "I", "Jail cannot see all mounted file systems");
4027 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4028 "B", "Jail persistence");
4030 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4031 "E,jailsys", "Virtual network stack");
4033 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4034 "B", "Jail is in the process of shutting down");
4036 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4037 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4038 "I", "Current number of child jails");
4039 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4040 "I", "Maximum number of child jails");
4042 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4043 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4045 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4046 "Jail NIS domainname");
4047 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4049 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4050 "LU", "Jail host ID");
4052 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4053 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4056 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4057 "Jail IPv4 address virtualization");
4058 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4059 "S,in_addr,a", "Jail IPv4 addresses");
4062 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4063 "Jail IPv6 address virtualization");
4064 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4065 "S,in6_addr,a", "Jail IPv6 addresses");
4068 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4069 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4070 "B", "Jail may set hostname");
4071 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4072 "B", "Jail may use SYSV IPC");
4073 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4074 "B", "Jail may create raw sockets");
4075 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4076 "B", "Jail may alter system file flags");
4077 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4078 "B", "Jail may mount/unmount jail-friendly file systems");
4079 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4080 "B", "Jail may set file quotas");
4081 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4082 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4088 db_show_prison(struct prison *pr)
4091 #if defined(INET) || defined(INET6)
4096 char ip6buf[INET6_ADDRSTRLEN];
4099 db_printf("prison %p:\n", pr);
4100 db_printf(" jid = %d\n", pr->pr_id);
4101 db_printf(" name = %s\n", pr->pr_name);
4102 db_printf(" parent = %p\n", pr->pr_parent);
4103 db_printf(" ref = %d\n", pr->pr_ref);
4104 db_printf(" uref = %d\n", pr->pr_uref);
4105 db_printf(" path = %s\n", pr->pr_path);
4106 db_printf(" cpuset = %d\n", pr->pr_cpuset
4107 ? pr->pr_cpuset->cs_id : -1);
4109 db_printf(" vnet = %p\n", pr->pr_vnet);
4111 db_printf(" root = %p\n", pr->pr_root);
4112 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4113 db_printf(" childcount = %d\n", pr->pr_childcount);
4114 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4115 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4116 db_printf(" flags = %x", pr->pr_flags);
4117 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4119 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4120 db_printf(" %s", pr_flag_names[fi]);
4121 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4123 jsf = pr->pr_flags &
4124 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4125 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4126 pr_flag_jailsys[fi].disable &&
4127 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4128 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4131 db_printf(" allow = %x", pr->pr_allow);
4132 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4134 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4135 db_printf(" %s", pr_allow_names[fi]);
4137 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4138 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4139 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4140 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4141 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4143 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4144 for (ii = 0; ii < pr->pr_ip4s; ii++)
4145 db_printf(" %s %s\n",
4146 ii == 0 ? "ip4 =" : " ",
4147 inet_ntoa(pr->pr_ip4[ii]));
4150 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4151 for (ii = 0; ii < pr->pr_ip6s; ii++)
4152 db_printf(" %s %s\n",
4153 ii == 0 ? "ip6 =" : " ",
4154 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4158 DB_SHOW_COMMAND(prison, db_show_prison_command)
4164 * Show all prisons in the list, and prison0 which is not
4167 db_show_prison(&prison0);
4168 if (!db_pager_quit) {
4169 TAILQ_FOREACH(pr, &allprison, pr_list) {
4181 /* Look for a prison with the ID and with references. */
4182 TAILQ_FOREACH(pr, &allprison, pr_list)
4183 if (pr->pr_id == addr && pr->pr_ref > 0)
4186 /* Look again, without requiring a reference. */
4187 TAILQ_FOREACH(pr, &allprison, pr_list)
4188 if (pr->pr_id == addr)
4191 /* Assume address points to a valid prison. */
4192 pr = (struct prison *)addr;