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_flags = PR_HOST|PR_VNET,
96 .pr_allow = PR_ALLOW_ALL,
98 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
100 /* allprison and lastprid are protected by allprison_lock. */
101 struct sx allprison_lock;
102 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
103 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
106 static int do_jail_attach(struct thread *td, struct prison *pr);
107 static void prison_complete(void *context, int pending);
108 static void prison_deref(struct prison *pr, int flags);
109 static char *prison_path(struct prison *pr1, struct prison *pr2);
110 static void prison_remove_one(struct prison *pr);
112 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
113 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
116 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
117 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
120 /* Flags for prison_deref */
121 #define PD_DEREF 0x01
122 #define PD_DEUREF 0x02
123 #define PD_LOCKED 0x04
124 #define PD_LIST_SLOCKED 0x08
125 #define PD_LIST_XLOCKED 0x10
128 * Parameter names corresponding to PR_* flag values
130 static char *pr_flag_names[] = {
134 static char *pr_flag_nonames[] = {
138 struct jailsys_flags {
142 } pr_flag_jailsys[] = {
143 { "host", 0, PR_HOST },
145 { "vnet", 0, PR_VNET },
148 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
151 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
155 static char *pr_allow_names[] = {
156 "allow.set_hostname",
165 static char *pr_allow_nonames[] = {
166 "allow.noset_hostname",
168 "allow.noraw_sockets",
175 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
176 #define JAIL_DEFAULT_ENFORCE_STATFS 2
177 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
178 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
179 #if defined(INET) || defined(INET6)
180 static unsigned jail_max_af_ips = 255;
185 qcmp_v4(const void *ip1, const void *ip2)
190 * We need to compare in HBO here to get the list sorted as expected
191 * by the result of the code. Sorting NBO addresses gives you
192 * interesting results. If you do not understand, do not try.
194 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
195 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
198 * Do not simply return the difference of the two numbers, the int is
212 qcmp_v6(const void *ip1, const void *ip2)
214 const struct in6_addr *ia6a, *ia6b;
217 ia6a = (const struct in6_addr *)ip1;
218 ia6b = (const struct in6_addr *)ip2;
221 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
222 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
224 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
237 jail(struct thread *td, struct jail_args *uap)
243 error = copyin(uap->jail, &version, sizeof(uint32_t));
252 /* FreeBSD single IPv4 jails. */
253 bzero(&j, sizeof(struct jail));
254 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
257 j.version = j0.version;
259 j.hostname = j0.hostname;
260 j.ip4s = j0.ip_number;
266 * Version 1 was used by multi-IPv4 jail implementations
267 * that never made it into the official kernel.
271 case 2: /* JAIL_API_VERSION */
272 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
273 error = copyin(uap->jail, &j, sizeof(struct jail));
279 /* Sci-Fi jails are not supported, sorry. */
282 return (kern_jail(td, &j));
286 kern_jail(struct thread *td, struct jail *j)
288 struct iovec optiov[2 * (4
289 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
298 char *u_path, *u_hostname, *u_name;
301 struct in_addr *u_ip4;
304 struct in6_addr *u_ip6;
307 int error, enforce_statfs, fi;
309 bzero(&optiov, sizeof(optiov));
310 opt.uio_iov = optiov;
314 opt.uio_segflg = UIO_SYSSPACE;
315 opt.uio_rw = UIO_READ;
318 /* Set permissions for top-level jails from sysctls. */
319 if (!jailed(td->td_ucred)) {
320 for (fi = 0; fi < sizeof(pr_allow_names) /
321 sizeof(pr_allow_names[0]); fi++) {
322 optiov[opt.uio_iovcnt].iov_base =
323 (jail_default_allow & (1 << fi))
324 ? pr_allow_names[fi] : pr_allow_nonames[fi];
325 optiov[opt.uio_iovcnt].iov_len =
326 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
329 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
330 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
332 enforce_statfs = jail_default_enforce_statfs;
333 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
334 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
338 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
340 ip4s = (j->version == 0) ? 1 : j->ip4s;
341 if (ip4s > jail_max_af_ips)
343 tmplen += ip4s * sizeof(struct in_addr);
349 if (j->ip6s > jail_max_af_ips)
351 tmplen += j->ip6s * sizeof(struct in6_addr);
356 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
357 u_hostname = u_path + MAXPATHLEN;
358 u_name = u_hostname + MAXHOSTNAMELEN;
360 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
364 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
366 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
369 optiov[opt.uio_iovcnt].iov_base = "path";
370 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
372 optiov[opt.uio_iovcnt].iov_base = u_path;
373 error = copyinstr(j->path, u_path, MAXPATHLEN,
374 &optiov[opt.uio_iovcnt].iov_len);
376 free(u_path, M_TEMP);
380 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
381 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
383 optiov[opt.uio_iovcnt].iov_base = u_hostname;
384 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
385 &optiov[opt.uio_iovcnt].iov_len);
387 free(u_path, M_TEMP);
391 if (j->jailname != NULL) {
392 optiov[opt.uio_iovcnt].iov_base = "name";
393 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
395 optiov[opt.uio_iovcnt].iov_base = u_name;
396 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
397 &optiov[opt.uio_iovcnt].iov_len);
399 free(u_path, M_TEMP);
405 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
406 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
408 optiov[opt.uio_iovcnt].iov_base = u_ip4;
409 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
411 u_ip4->s_addr = j->ip4s;
413 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
415 free(u_path, M_TEMP);
422 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
423 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
425 optiov[opt.uio_iovcnt].iov_base = u_ip6;
426 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
427 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
429 free(u_path, M_TEMP);
434 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
435 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
436 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
437 free(u_path, M_TEMP);
443 * struct jail_set_args {
444 * struct iovec *iovp;
445 * unsigned int iovcnt;
450 jail_set(struct thread *td, struct jail_set_args *uap)
455 /* Check that we have an even number of iovecs. */
459 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
462 error = kern_jail_set(td, auio, uap->flags);
468 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
475 struct in6_addr *ip6;
478 struct vfsoptlist *opts;
479 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
481 char *domain, *errmsg, *host, *name, *p, *path, *uuid;
482 #if defined(INET) || defined(INET6)
487 size_t namelen, onamelen;
488 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
489 int gotchildmax, gotenforce, gothid, gotslevel;
490 int fi, jid, jsys, len, level;
491 int childmax, slevel, vfslocked;
492 #if defined(INET) || defined(INET6)
501 unsigned pr_flags, ch_flags;
502 unsigned pr_allow, ch_allow, tallow;
505 error = priv_check(td, PRIV_JAIL_SET);
506 if (!error && (flags & JAIL_ATTACH))
507 error = priv_check(td, PRIV_JAIL_ATTACH);
510 mypr = ppr = td->td_ucred->cr_prison;
511 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
513 if (flags & ~JAIL_SET_MASK)
517 * Check all the parameters before committing to anything. Not all
518 * errors can be caught early, but we may as well try. Also, this
519 * takes care of some expensive stuff (path lookup) before getting
520 * the allprison lock.
522 * XXX Jails are not filesystems, and jail parameters are not mount
523 * options. But it makes more sense to re-use the vfsopt code
524 * than duplicate it under a different name.
526 error = vfs_buildopts(optuio, &opts);
536 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
542 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
551 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
559 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
560 gotenforce = (error == 0);
562 if (enforce < 0 || enforce > 2)
564 } else if (error != ENOENT)
567 pr_flags = ch_flags = 0;
568 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
570 if (pr_flag_names[fi] == NULL)
572 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
573 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
575 ch_flags |= pr_flags;
576 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
578 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
585 case JAIL_SYS_DISABLE:
586 if (!pr_flag_jailsys[fi].disable) {
590 pr_flags |= pr_flag_jailsys[fi].disable;
593 pr_flags |= pr_flag_jailsys[fi].new;
595 case JAIL_SYS_INHERIT:
602 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
604 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
605 && !(pr_flags & PR_PERSIST)) {
607 vfs_opterror(opts, "new jail must persist or attach");
611 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
613 vfs_opterror(opts, "vnet cannot be changed after creation");
618 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
620 vfs_opterror(opts, "ip4 cannot be changed after creation");
625 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
627 vfs_opterror(opts, "ip6 cannot be changed after creation");
632 pr_allow = ch_allow = 0;
633 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
635 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
636 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
638 ch_allow |= pr_allow;
640 error = vfs_getopt(opts, "name", (void **)&name, &len);
646 if (len == 0 || name[len - 1] != '\0') {
650 if (len > MAXHOSTNAMELEN) {
651 error = ENAMETOOLONG;
656 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
664 if (len == 0 || host[len - 1] != '\0') {
668 if (len > MAXHOSTNAMELEN) {
669 error = ENAMETOOLONG;
674 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
682 if (len == 0 || domain[len - 1] != '\0') {
686 if (len > MAXHOSTNAMELEN) {
687 error = ENAMETOOLONG;
692 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
700 if (len == 0 || uuid[len - 1] != '\0') {
704 if (len > HOSTUUIDLEN) {
705 error = ENAMETOOLONG;
711 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
714 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
718 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
730 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
732 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
735 else if (ip4s & (sizeof(*ip4) - 1)) {
739 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
741 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
743 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
744 ip4s /= sizeof(*ip4);
745 if (ip4s > jail_max_af_ips) {
747 vfs_opterror(opts, "too many IPv4 addresses");
750 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
751 bcopy(op, ip4, ip4s * sizeof(*ip4));
753 * IP addresses are all sorted but ip[0] to preserve
754 * the primary IP address as given from userland.
755 * This special IP is used for unbound outgoing
756 * connections as well for "loopback" traffic.
759 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
761 * Check for duplicate addresses and do some simple
762 * zero and broadcast checks. If users give other bogus
763 * addresses it is their problem.
765 * We do not have to care about byte order for these
766 * checks so we will do them in NBO.
768 for (ii = 0; ii < ip4s; ii++) {
769 if (ip4[ii].s_addr == INADDR_ANY ||
770 ip4[ii].s_addr == INADDR_BROADCAST) {
775 (ip4[0].s_addr == ip4[ii+1].s_addr ||
776 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
786 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
788 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
791 else if (ip6s & (sizeof(*ip6) - 1)) {
795 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
797 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
799 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
800 ip6s /= sizeof(*ip6);
801 if (ip6s > jail_max_af_ips) {
803 vfs_opterror(opts, "too many IPv6 addresses");
806 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
807 bcopy(op, ip6, ip6s * sizeof(*ip6));
809 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
810 for (ii = 0; ii < ip6s; ii++) {
811 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
816 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
817 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
827 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
828 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
831 "vnet jails cannot have IP address restrictions");
837 error = vfs_getopt(opts, "path", (void **)&path, &len);
843 if (flags & JAIL_UPDATE) {
846 "path cannot be changed after creation");
849 if (len == 0 || path[len - 1] != '\0') {
853 if (len < 2 || (len == 2 && path[0] == '/'))
856 /* Leave room for a real-root full pathname. */
857 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
858 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
859 error = ENAMETOOLONG;
862 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
867 vfslocked = NDHASGIANT(&nd);
869 NDFREE(&nd, NDF_ONLY_PNBUF);
870 if (root->v_type != VDIR) {
873 VFS_UNLOCK_GIANT(vfslocked);
876 VFS_UNLOCK_GIANT(vfslocked);
881 * Grab the allprison lock before letting modules check their
882 * parameters. Once we have it, do not let go so we'll have a
883 * consistent view of the OSD list.
885 sx_xlock(&allprison_lock);
886 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
888 goto done_unlock_list;
890 /* By now, all parameters should have been noted. */
891 TAILQ_FOREACH(opt, opts, link) {
892 if (!opt->seen && strcmp(opt->name, "errmsg")) {
894 vfs_opterror(opts, "unknown parameter: %s", opt->name);
895 goto done_unlock_list;
900 * See if we are creating a new record or updating an existing one.
901 * This abuses the file error codes ENOENT and EEXIST.
903 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
906 vfs_opterror(opts, "no valid operation (create or update)");
907 goto done_unlock_list;
912 * See if a requested jid already exists. There is an
913 * information leak here if the jid exists but is not within
914 * the caller's jail hierarchy. Jail creators will get EEXIST
915 * even though they cannot see the jail, and CREATE | UPDATE
916 * will return ENOENT which is not normally a valid error.
920 vfs_opterror(opts, "negative jid");
921 goto done_unlock_list;
923 pr = prison_find(jid);
926 /* Create: jid must not exist. */
927 if (cuflags == JAIL_CREATE) {
928 mtx_unlock(&pr->pr_mtx);
930 vfs_opterror(opts, "jail %d already exists",
932 goto done_unlock_list;
934 if (!prison_ischild(mypr, pr)) {
935 mtx_unlock(&pr->pr_mtx);
937 } else if (pr->pr_uref == 0) {
938 if (!(flags & JAIL_DYING)) {
939 mtx_unlock(&pr->pr_mtx);
941 vfs_opterror(opts, "jail %d is dying",
943 goto done_unlock_list;
944 } else if ((flags & JAIL_ATTACH) ||
945 (pr_flags & PR_PERSIST)) {
947 * A dying jail might be resurrected
948 * (via attach or persist), but first
949 * it must determine if another jail
950 * has claimed its name. Accomplish
951 * this by implicitly re-setting the
955 name = prison_name(mypr, pr);
960 /* Update: jid must exist. */
961 if (cuflags == JAIL_UPDATE) {
963 vfs_opterror(opts, "jail %d not found", jid);
964 goto done_unlock_list;
969 * If the caller provided a name, look for a jail by that name.
970 * This has different semantics for creates and updates keyed by jid
971 * (where the name must not already exist in a different jail),
972 * and updates keyed by the name itself (where the name must exist
973 * because that is the jail being updated).
976 p = strrchr(name, '.');
979 * This is a hierarchical name. Split it into the
980 * parent and child names, and make sure the parent
981 * exists or matches an already found jail.
985 if (strncmp(name, ppr->pr_name, p - name) ||
986 ppr->pr_name[p - name] != '\0') {
987 mtx_unlock(&pr->pr_mtx);
990 "cannot change jail's parent");
991 goto done_unlock_list;
994 ppr = prison_find_name(mypr, name);
998 "jail \"%s\" not found", name);
999 goto done_unlock_list;
1001 mtx_unlock(&ppr->pr_mtx);
1005 if (name[0] != '\0') {
1007 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1010 FOREACH_PRISON_CHILD(ppr, tpr) {
1011 if (tpr != pr && tpr->pr_ref > 0 &&
1012 !strcmp(tpr->pr_name + namelen, name)) {
1014 cuflags != JAIL_CREATE) {
1015 mtx_lock(&tpr->pr_mtx);
1016 if (tpr->pr_ref > 0) {
1021 if (tpr->pr_uref > 0) {
1027 mtx_unlock(&tpr->pr_mtx);
1028 } else if (tpr->pr_uref > 0) {
1030 * Create, or update(jid):
1031 * name must not exist in an
1032 * active sibling jail.
1036 mtx_unlock(&pr->pr_mtx);
1038 "jail \"%s\" already exists",
1040 goto done_unlock_list;
1044 /* If no active jail is found, use a dying one. */
1045 if (deadpr != NULL && pr == NULL) {
1046 if (flags & JAIL_DYING) {
1047 mtx_lock(&deadpr->pr_mtx);
1048 if (deadpr->pr_ref == 0) {
1049 mtx_unlock(&deadpr->pr_mtx);
1053 } else if (cuflags == JAIL_UPDATE) {
1056 "jail \"%s\" is dying", name);
1057 goto done_unlock_list;
1060 /* Update: name must exist if no jid. */
1061 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1063 vfs_opterror(opts, "jail \"%s\" not found",
1065 goto done_unlock_list;
1069 /* Update: must provide a jid or name. */
1070 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1072 vfs_opterror(opts, "update specified no jail");
1073 goto done_unlock_list;
1076 /* If there's no prison to update, create a new one and link it in. */
1078 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1079 if (tpr->pr_childcount >= tpr->pr_childmax) {
1081 vfs_opterror(opts, "prison limit exceeded");
1082 goto done_unlock_list;
1085 mtx_lock(&ppr->pr_mtx);
1086 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1087 mtx_unlock(&ppr->pr_mtx);
1089 vfs_opterror(opts, "parent jail went away!");
1090 goto done_unlock_list;
1094 mtx_unlock(&ppr->pr_mtx);
1095 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1097 /* Find the next free jid. */
1100 if (jid == JAIL_MAX)
1102 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1103 if (tpr->pr_id < jid)
1105 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1106 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1109 if (jid == lastprid) {
1112 "no available jail IDs");
1114 prison_deref(ppr, PD_DEREF |
1115 PD_DEUREF | PD_LIST_XLOCKED);
1124 * The jail already has a jid (that did not yet exist),
1125 * so just find where to insert it.
1127 TAILQ_FOREACH(tpr, &allprison, pr_list)
1128 if (tpr->pr_id >= jid) {
1129 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1134 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1135 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1136 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1137 tpr->pr_childcount++;
1139 pr->pr_parent = ppr;
1142 /* Set some default values, and inherit some from the parent. */
1147 root = mypr->pr_root;
1150 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1151 pr->pr_flags |= PR_HOST;
1152 #if defined(INET) || defined(INET6)
1154 if (!(pr_flags & PR_VNET))
1158 if (!(ch_flags & PR_IP4_USER))
1160 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1161 else if (!(pr_flags & PR_IP4_USER)) {
1162 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1163 if (ppr->pr_ip4 != NULL) {
1164 pr->pr_ip4s = ppr->pr_ip4s;
1165 pr->pr_ip4 = malloc(pr->pr_ip4s *
1166 sizeof(struct in_addr), M_PRISON,
1168 bcopy(ppr->pr_ip4, pr->pr_ip4,
1169 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1174 if (!(ch_flags & PR_IP6_USER))
1176 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1177 else if (!(pr_flags & PR_IP6_USER)) {
1178 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1179 if (ppr->pr_ip6 != NULL) {
1180 pr->pr_ip6s = ppr->pr_ip6s;
1181 pr->pr_ip6 = malloc(pr->pr_ip6s *
1182 sizeof(struct in6_addr), M_PRISON,
1184 bcopy(ppr->pr_ip6, pr->pr_ip6,
1185 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1191 pr->pr_securelevel = ppr->pr_securelevel;
1192 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1193 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1195 LIST_INIT(&pr->pr_children);
1196 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1199 /* Allocate a new vnet if specified. */
1200 pr->pr_vnet = (pr_flags & PR_VNET)
1201 ? vnet_alloc() : ppr->pr_vnet;
1204 * Allocate a dedicated cpuset for each jail.
1205 * Unlike other initial settings, this may return an erorr.
1207 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1209 prison_deref(pr, PD_LIST_XLOCKED);
1213 mtx_lock(&pr->pr_mtx);
1215 * New prisons do not yet have a reference, because we do not
1216 * want other to see the incomplete prison once the
1217 * allprison_lock is downgraded.
1222 * Grab a reference for existing prisons, to ensure they
1223 * continue to exist for the duration of the call.
1226 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1227 if ((pr->pr_flags & PR_VNET) &&
1228 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1231 "vnet jails cannot have IP address restrictions");
1232 goto done_deref_locked;
1236 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1239 "ip4 cannot be changed after creation");
1240 goto done_deref_locked;
1244 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1247 "ip6 cannot be changed after creation");
1248 goto done_deref_locked;
1253 /* Do final error checking before setting anything. */
1255 if (slevel < ppr->pr_securelevel) {
1257 goto done_deref_locked;
1261 if (childmax >= ppr->pr_childmax) {
1263 goto done_deref_locked;
1267 if (enforce < ppr->pr_enforce_statfs) {
1269 goto done_deref_locked;
1274 if (ppr->pr_flags & PR_IP4) {
1276 * Make sure the new set of IP addresses is a
1277 * subset of the parent's list. Don't worry
1278 * about the parent being unlocked, as any
1279 * setting is done with allprison_lock held.
1281 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1282 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1284 if (ij == ppr->pr_ip4s) {
1286 goto done_deref_locked;
1289 for (ii = ij = 1; ii < ip4s; ii++) {
1290 if (ip4[ii].s_addr ==
1291 ppr->pr_ip4[0].s_addr)
1293 for (; ij < ppr->pr_ip4s; ij++)
1294 if (ip4[ii].s_addr ==
1295 ppr->pr_ip4[ij].s_addr)
1297 if (ij == ppr->pr_ip4s)
1300 if (ij == ppr->pr_ip4s) {
1302 goto done_deref_locked;
1307 * Check for conflicting IP addresses. We permit them
1308 * if there is no more than one IP on each jail. If
1309 * there is a duplicate on a jail with more than one
1310 * IP stop checking and return error.
1314 for (; tppr != &prison0; tppr = tppr->pr_parent)
1315 if (tppr->pr_flags & PR_VNET)
1318 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1321 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1323 tpr->pr_uref == 0) {
1327 if (!(tpr->pr_flags & PR_IP4_USER))
1330 if (tpr->pr_ip4 == NULL ||
1331 (ip4s == 1 && tpr->pr_ip4s == 1))
1333 for (ii = 0; ii < ip4s; ii++) {
1334 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1337 "IPv4 addresses clash");
1338 goto done_deref_locked;
1346 if (ppr->pr_flags & PR_IP6) {
1348 * Make sure the new set of IP addresses is a
1349 * subset of the parent's list.
1351 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1352 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1355 if (ij == ppr->pr_ip6s) {
1357 goto done_deref_locked;
1360 for (ii = ij = 1; ii < ip6s; ii++) {
1361 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1364 for (; ij < ppr->pr_ip6s; ij++)
1365 if (IN6_ARE_ADDR_EQUAL(
1366 &ip6[ii], &ppr->pr_ip6[ij]))
1368 if (ij == ppr->pr_ip6s)
1371 if (ij == ppr->pr_ip6s) {
1373 goto done_deref_locked;
1377 /* Check for conflicting IP addresses. */
1380 for (; tppr != &prison0; tppr = tppr->pr_parent)
1381 if (tppr->pr_flags & PR_VNET)
1384 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1387 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1389 tpr->pr_uref == 0) {
1393 if (!(tpr->pr_flags & PR_IP6_USER))
1396 if (tpr->pr_ip6 == NULL ||
1397 (ip6s == 1 && tpr->pr_ip6s == 1))
1399 for (ii = 0; ii < ip6s; ii++) {
1400 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1403 "IPv6 addresses clash");
1404 goto done_deref_locked;
1410 onamelen = namelen = 0;
1412 /* Give a default name of the jid. */
1413 if (name[0] == '\0')
1414 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1415 else if (strtoul(name, &p, 10) != jid && *p == '\0') {
1417 vfs_opterror(opts, "name cannot be numeric");
1418 goto done_deref_locked;
1421 * Make sure the name isn't too long for the prison or its
1424 onamelen = strlen(pr->pr_name);
1425 namelen = strlen(name);
1426 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1427 error = ENAMETOOLONG;
1428 goto done_deref_locked;
1430 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1431 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1432 sizeof(pr->pr_name)) {
1433 error = ENAMETOOLONG;
1434 goto done_deref_locked;
1438 if (pr_allow & ~ppr->pr_allow) {
1440 goto done_deref_locked;
1443 /* Set the parameters of the prison. */
1446 if (pr_flags & PR_IP4_USER) {
1447 pr->pr_flags |= PR_IP4;
1448 free(pr->pr_ip4, M_PRISON);
1452 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1454 if (tpr->pr_flags & PR_VNET) {
1459 if (prison_restrict_ip4(tpr, NULL)) {
1468 if (pr_flags & PR_IP6_USER) {
1469 pr->pr_flags |= PR_IP6;
1470 free(pr->pr_ip6, M_PRISON);
1474 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1476 if (tpr->pr_flags & PR_VNET) {
1481 if (prison_restrict_ip6(tpr, NULL)) {
1489 pr->pr_securelevel = slevel;
1490 /* Set all child jails to be at least this level. */
1491 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1492 if (tpr->pr_securelevel < slevel)
1493 tpr->pr_securelevel = slevel;
1496 pr->pr_childmax = childmax;
1497 /* Set all child jails to under this limit. */
1498 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1499 if (tpr->pr_childmax > childmax - level)
1500 tpr->pr_childmax = childmax > level
1501 ? childmax - level : 0;
1504 pr->pr_enforce_statfs = enforce;
1505 /* Pass this restriction on to the children. */
1506 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1507 if (tpr->pr_enforce_statfs < enforce)
1508 tpr->pr_enforce_statfs = enforce;
1511 if (ppr == &prison0)
1512 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1514 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1515 ppr->pr_name, name);
1516 /* Change this component of child names. */
1517 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1518 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1519 strlen(tpr->pr_name + onamelen) + 1);
1520 bcopy(pr->pr_name, tpr->pr_name, namelen);
1524 /* Try to keep a real-rooted full pathname. */
1525 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1526 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1527 mypr->pr_path, path);
1529 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1532 if (PR_HOST & ch_flags & ~pr_flags) {
1533 if (pr->pr_flags & PR_HOST) {
1535 * Copy the parent's host info. As with pr_ip4 above,
1536 * the lack of a lock on the parent is not a problem;
1537 * it is always set with allprison_lock at least
1538 * shared, and is held exclusively here.
1540 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1541 sizeof(pr->pr_hostname));
1542 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1543 sizeof(pr->pr_domainname));
1544 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1545 sizeof(pr->pr_hostuuid));
1546 pr->pr_hostid = pr->pr_parent->pr_hostid;
1548 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1549 /* Set this prison, and any descendants without PR_HOST. */
1551 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1553 strlcpy(pr->pr_domainname, domain,
1554 sizeof(pr->pr_domainname));
1556 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1558 pr->pr_hostid = hid;
1559 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1560 if (tpr->pr_flags & PR_HOST)
1564 strlcpy(tpr->pr_hostname,
1566 sizeof(tpr->pr_hostname));
1568 strlcpy(tpr->pr_domainname,
1570 sizeof(tpr->pr_domainname));
1572 strlcpy(tpr->pr_hostuuid,
1574 sizeof(tpr->pr_hostuuid));
1576 tpr->pr_hostid = hid;
1580 if ((tallow = ch_allow & ~pr_allow)) {
1581 /* Clear allow bits in all children. */
1582 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1583 tpr->pr_allow &= ~tallow;
1585 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1587 * Persistent prisons get an extra reference, and prisons losing their
1588 * persist flag lose that reference. Only do this for existing prisons
1589 * for now, so new ones will remain unseen until after the module
1590 * handlers have completed.
1592 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1593 if (pr_flags & PR_PERSIST) {
1601 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1602 mtx_unlock(&pr->pr_mtx);
1604 /* Locks may have prevented a complete restriction of child IP
1605 * addresses. If so, allocate some more memory and try again.
1610 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1611 mtx_lock(&pr->pr_mtx);
1613 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1615 if (tpr->pr_flags & PR_VNET) {
1620 if (prison_restrict_ip4(tpr, ip4)) {
1627 mtx_unlock(&pr->pr_mtx);
1633 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1634 mtx_lock(&pr->pr_mtx);
1636 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1638 if (tpr->pr_flags & PR_VNET) {
1643 if (prison_restrict_ip6(tpr, ip6)) {
1650 mtx_unlock(&pr->pr_mtx);
1654 /* Let the modules do their work. */
1655 sx_downgrade(&allprison_lock);
1657 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1659 prison_deref(pr, PD_LIST_SLOCKED);
1663 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1665 prison_deref(pr, created
1667 : PD_DEREF | PD_LIST_SLOCKED);
1671 /* Attach this process to the prison if requested. */
1672 if (flags & JAIL_ATTACH) {
1673 mtx_lock(&pr->pr_mtx);
1674 error = do_jail_attach(td, pr);
1676 vfs_opterror(opts, "attach failed");
1678 prison_deref(pr, PD_DEREF);
1684 * Now that it is all there, drop the temporary reference from existing
1685 * prisons. Or add a reference to newly created persistent prisons
1686 * (which was not done earlier so that the prison would not be publicly
1690 prison_deref(pr, (flags & JAIL_ATTACH)
1692 : PD_DEREF | PD_LIST_SLOCKED);
1694 if (pr_flags & PR_PERSIST) {
1695 mtx_lock(&pr->pr_mtx);
1698 mtx_unlock(&pr->pr_mtx);
1700 if (!(flags & JAIL_ATTACH))
1701 sx_sunlock(&allprison_lock);
1703 td->td_retval[0] = pr->pr_id;
1707 prison_deref(pr, created
1708 ? PD_LOCKED | PD_LIST_XLOCKED
1709 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1712 sx_xunlock(&allprison_lock);
1715 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1717 VFS_UNLOCK_GIANT(vfslocked);
1721 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1722 if (errmsg_len > 0) {
1723 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1724 if (errmsg_pos > 0) {
1725 if (optuio->uio_segflg == UIO_SYSSPACE)
1727 optuio->uio_iov[errmsg_pos].iov_base,
1731 optuio->uio_iov[errmsg_pos].iov_base,
1738 free(ip4, M_PRISON);
1741 free(ip6, M_PRISON);
1749 * struct jail_get_args {
1750 * struct iovec *iovp;
1751 * unsigned int iovcnt;
1756 jail_get(struct thread *td, struct jail_get_args *uap)
1761 /* Check that we have an even number of iovecs. */
1762 if (uap->iovcnt & 1)
1765 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1768 error = kern_jail_get(td, auio, uap->flags);
1770 error = copyout(auio->uio_iov, uap->iovp,
1771 uap->iovcnt * sizeof (struct iovec));
1777 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1779 struct prison *pr, *mypr;
1781 struct vfsoptlist *opts;
1782 char *errmsg, *name;
1783 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1785 if (flags & ~JAIL_GET_MASK)
1788 /* Get the parameter list. */
1789 error = vfs_buildopts(optuio, &opts);
1792 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1793 mypr = td->td_ucred->cr_prison;
1796 * Find the prison specified by one of: lastjid, jid, name.
1798 sx_slock(&allprison_lock);
1799 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1801 TAILQ_FOREACH(pr, &allprison, pr_list) {
1802 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1803 mtx_lock(&pr->pr_mtx);
1804 if (pr->pr_ref > 0 &&
1805 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1807 mtx_unlock(&pr->pr_mtx);
1813 vfs_opterror(opts, "no jail after %d", jid);
1814 goto done_unlock_list;
1815 } else if (error != ENOENT)
1816 goto done_unlock_list;
1818 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1821 pr = prison_find_child(mypr, jid);
1823 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1824 mtx_unlock(&pr->pr_mtx);
1826 vfs_opterror(opts, "jail %d is dying",
1828 goto done_unlock_list;
1833 vfs_opterror(opts, "jail %d not found", jid);
1834 goto done_unlock_list;
1836 } else if (error != ENOENT)
1837 goto done_unlock_list;
1839 error = vfs_getopt(opts, "name", (void **)&name, &len);
1841 if (len == 0 || name[len - 1] != '\0') {
1843 goto done_unlock_list;
1845 pr = prison_find_name(mypr, name);
1847 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1848 mtx_unlock(&pr->pr_mtx);
1850 vfs_opterror(opts, "jail \"%s\" is dying",
1852 goto done_unlock_list;
1857 vfs_opterror(opts, "jail \"%s\" not found", name);
1858 goto done_unlock_list;
1859 } else if (error != ENOENT)
1860 goto done_unlock_list;
1862 vfs_opterror(opts, "no jail specified");
1864 goto done_unlock_list;
1867 /* Get the parameters of the prison. */
1870 td->td_retval[0] = pr->pr_id;
1871 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1872 if (error != 0 && error != ENOENT)
1874 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1875 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1876 if (error != 0 && error != ENOENT)
1878 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1879 if (error != 0 && error != ENOENT)
1881 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1882 sizeof(pr->pr_cpuset->cs_id));
1883 if (error != 0 && error != ENOENT)
1885 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1886 if (error != 0 && error != ENOENT)
1889 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1890 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1891 if (error != 0 && error != ENOENT)
1895 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1896 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1897 if (error != 0 && error != ENOENT)
1900 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1901 sizeof(pr->pr_securelevel));
1902 if (error != 0 && error != ENOENT)
1904 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1905 sizeof(pr->pr_childcount));
1906 if (error != 0 && error != ENOENT)
1908 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1909 sizeof(pr->pr_childmax));
1910 if (error != 0 && error != ENOENT)
1912 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1913 if (error != 0 && error != ENOENT)
1915 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1916 if (error != 0 && error != ENOENT)
1918 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1919 if (error != 0 && error != ENOENT)
1922 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1923 uint32_t hid32 = pr->pr_hostid;
1925 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1928 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1929 sizeof(pr->pr_hostid));
1930 if (error != 0 && error != ENOENT)
1932 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1933 sizeof(pr->pr_enforce_statfs));
1934 if (error != 0 && error != ENOENT)
1936 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1938 if (pr_flag_names[fi] == NULL)
1940 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1941 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1942 if (error != 0 && error != ENOENT)
1945 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1946 if (error != 0 && error != ENOENT)
1949 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1952 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1953 i = pr_flag_jailsys[fi].disable &&
1954 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1955 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
1958 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
1959 if (error != 0 && error != ENOENT)
1962 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
1964 if (pr_allow_names[fi] == NULL)
1966 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
1967 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
1968 if (error != 0 && error != ENOENT)
1971 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
1972 if (error != 0 && error != ENOENT)
1975 i = (pr->pr_uref == 0);
1976 error = vfs_setopt(opts, "dying", &i, sizeof(i));
1977 if (error != 0 && error != ENOENT)
1980 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1981 if (error != 0 && error != ENOENT)
1984 /* Get the module parameters. */
1985 mtx_unlock(&pr->pr_mtx);
1987 error = osd_jail_call(pr, PR_METHOD_GET, opts);
1990 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
1992 /* By now, all parameters should have been noted. */
1993 TAILQ_FOREACH(opt, opts, link) {
1994 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1996 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2001 /* Write the fetched parameters back to userspace. */
2003 TAILQ_FOREACH(opt, opts, link) {
2004 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2005 pos = 2 * opt->pos + 1;
2006 optuio->uio_iov[pos].iov_len = opt->len;
2007 if (opt->value != NULL) {
2008 if (optuio->uio_segflg == UIO_SYSSPACE) {
2010 optuio->uio_iov[pos].iov_base,
2013 error = copyout(opt->value,
2014 optuio->uio_iov[pos].iov_base,
2025 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2029 sx_sunlock(&allprison_lock);
2031 if (error && errmsg_pos >= 0) {
2032 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2033 errmsg_pos = 2 * errmsg_pos + 1;
2034 if (errmsg_len > 0) {
2035 if (optuio->uio_segflg == UIO_SYSSPACE)
2037 optuio->uio_iov[errmsg_pos].iov_base,
2041 optuio->uio_iov[errmsg_pos].iov_base,
2051 * struct jail_remove_args {
2056 jail_remove(struct thread *td, struct jail_remove_args *uap)
2058 struct prison *pr, *cpr, *lpr, *tpr;
2061 error = priv_check(td, PRIV_JAIL_REMOVE);
2065 sx_xlock(&allprison_lock);
2066 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2068 sx_xunlock(&allprison_lock);
2072 /* Remove all descendants of this prison, then remove this prison. */
2074 pr->pr_flags |= PR_REMOVE;
2075 if (!LIST_EMPTY(&pr->pr_children)) {
2076 mtx_unlock(&pr->pr_mtx);
2078 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2079 mtx_lock(&cpr->pr_mtx);
2080 if (cpr->pr_ref > 0) {
2083 cpr->pr_flags |= PR_REMOVE;
2085 /* Already removed - do not do it again. */
2088 mtx_unlock(&cpr->pr_mtx);
2090 mtx_lock(&lpr->pr_mtx);
2091 prison_remove_one(lpr);
2092 sx_xlock(&allprison_lock);
2097 mtx_lock(&lpr->pr_mtx);
2098 prison_remove_one(lpr);
2099 sx_xlock(&allprison_lock);
2101 mtx_lock(&pr->pr_mtx);
2103 prison_remove_one(pr);
2108 prison_remove_one(struct prison *pr)
2113 /* If the prison was persistent, it is not anymore. */
2115 if (pr->pr_flags & PR_PERSIST) {
2118 pr->pr_flags &= ~PR_PERSIST;
2122 * jail_remove added a reference. If that's the only one, remove
2125 KASSERT(pr->pr_ref > 0,
2126 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2127 if (pr->pr_ref == 1) {
2129 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2133 mtx_unlock(&pr->pr_mtx);
2134 sx_xunlock(&allprison_lock);
2136 * Kill all processes unfortunate enough to be attached to this prison.
2138 sx_slock(&allproc_lock);
2139 LIST_FOREACH(p, &allproc, p_list) {
2141 if (p->p_state != PRS_NEW && p->p_ucred &&
2142 p->p_ucred->cr_prison == pr)
2143 psignal(p, SIGKILL);
2146 sx_sunlock(&allproc_lock);
2147 /* Remove the temporary reference added by jail_remove. */
2148 prison_deref(pr, deuref | PD_DEREF);
2153 * struct jail_attach_args {
2158 jail_attach(struct thread *td, struct jail_attach_args *uap)
2163 error = priv_check(td, PRIV_JAIL_ATTACH);
2167 sx_slock(&allprison_lock);
2168 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2170 sx_sunlock(&allprison_lock);
2175 * Do not allow a process to attach to a prison that is not
2176 * considered to be "alive".
2178 if (pr->pr_uref == 0) {
2179 mtx_unlock(&pr->pr_mtx);
2180 sx_sunlock(&allprison_lock);
2184 return (do_jail_attach(td, pr));
2188 do_jail_attach(struct thread *td, struct prison *pr)
2192 struct ucred *newcred, *oldcred;
2193 int vfslocked, error;
2196 * XXX: Note that there is a slight race here if two threads
2197 * in the same privileged process attempt to attach to two
2198 * different jails at the same time. It is important for
2199 * user processes not to do this, or they might end up with
2200 * a process root from one prison, but attached to the jail
2205 mtx_unlock(&pr->pr_mtx);
2207 /* Let modules do whatever they need to prepare for attaching. */
2208 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2210 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2213 sx_sunlock(&allprison_lock);
2216 * Reparent the newly attached process to this jail.
2218 ppr = td->td_ucred->cr_prison;
2220 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2224 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2225 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2226 if ((error = change_dir(pr->pr_root, td)) != 0)
2229 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2232 VOP_UNLOCK(pr->pr_root, 0);
2233 if ((error = change_root(pr->pr_root, td)))
2234 goto e_unlock_giant;
2235 VFS_UNLOCK_GIANT(vfslocked);
2239 oldcred = p->p_ucred;
2241 crcopy(newcred, oldcred);
2242 newcred->cr_prison = pr;
2243 p->p_ucred = newcred;
2246 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2249 VOP_UNLOCK(pr->pr_root, 0);
2251 VFS_UNLOCK_GIANT(vfslocked);
2253 /* Tell modules this thread is still in its old jail after all. */
2254 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2255 prison_deref(pr, PD_DEREF | PD_DEUREF);
2261 * Returns a locked prison instance, or NULL on failure.
2264 prison_find(int prid)
2268 sx_assert(&allprison_lock, SX_LOCKED);
2269 TAILQ_FOREACH(pr, &allprison, pr_list) {
2270 if (pr->pr_id == prid) {
2271 mtx_lock(&pr->pr_mtx);
2274 mtx_unlock(&pr->pr_mtx);
2281 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2284 prison_find_child(struct prison *mypr, int prid)
2289 sx_assert(&allprison_lock, SX_LOCKED);
2290 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2291 if (pr->pr_id == prid) {
2292 mtx_lock(&pr->pr_mtx);
2295 mtx_unlock(&pr->pr_mtx);
2302 * Look for the name relative to mypr. Returns a locked prison or NULL.
2305 prison_find_name(struct prison *mypr, const char *name)
2307 struct prison *pr, *deadpr;
2311 sx_assert(&allprison_lock, SX_LOCKED);
2312 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2315 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2316 if (!strcmp(pr->pr_name + mylen, name)) {
2317 mtx_lock(&pr->pr_mtx);
2318 if (pr->pr_ref > 0) {
2319 if (pr->pr_uref > 0)
2323 mtx_unlock(&pr->pr_mtx);
2326 /* There was no valid prison - perhaps there was a dying one. */
2327 if (deadpr != NULL) {
2328 mtx_lock(&deadpr->pr_mtx);
2329 if (deadpr->pr_ref == 0) {
2330 mtx_unlock(&deadpr->pr_mtx);
2338 * See if a prison has the specific flag set.
2341 prison_flag(struct ucred *cred, unsigned flag)
2344 /* This is an atomic read, so no locking is necessary. */
2345 return (cred->cr_prison->pr_flags & flag);
2349 prison_allow(struct ucred *cred, unsigned flag)
2352 /* This is an atomic read, so no locking is necessary. */
2353 return (cred->cr_prison->pr_allow & flag);
2357 * Remove a prison reference. If that was the last reference, remove the
2358 * prison itself - but not in this context in case there are locks held.
2361 prison_free_locked(struct prison *pr)
2364 mtx_assert(&pr->pr_mtx, MA_OWNED);
2366 if (pr->pr_ref == 0) {
2367 mtx_unlock(&pr->pr_mtx);
2368 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2369 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2372 mtx_unlock(&pr->pr_mtx);
2376 prison_free(struct prison *pr)
2379 mtx_lock(&pr->pr_mtx);
2380 prison_free_locked(pr);
2384 prison_complete(void *context, int pending)
2387 prison_deref((struct prison *)context, 0);
2391 * Remove a prison reference (usually). This internal version assumes no
2392 * mutexes are held, except perhaps the prison itself. If there are no more
2393 * references, release and delist the prison. On completion, the prison lock
2394 * and the allprison lock are both unlocked.
2397 prison_deref(struct prison *pr, int flags)
2399 struct prison *ppr, *tpr;
2402 if (!(flags & PD_LOCKED))
2403 mtx_lock(&pr->pr_mtx);
2404 /* Decrement the user references in a separate loop. */
2405 if (flags & PD_DEUREF) {
2406 for (tpr = pr;; tpr = tpr->pr_parent) {
2408 mtx_lock(&tpr->pr_mtx);
2409 if (--tpr->pr_uref > 0)
2411 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2412 mtx_unlock(&tpr->pr_mtx);
2414 /* Done if there were only user references to remove. */
2415 if (!(flags & PD_DEREF)) {
2416 mtx_unlock(&tpr->pr_mtx);
2417 if (flags & PD_LIST_SLOCKED)
2418 sx_sunlock(&allprison_lock);
2419 else if (flags & PD_LIST_XLOCKED)
2420 sx_xunlock(&allprison_lock);
2424 mtx_unlock(&tpr->pr_mtx);
2425 mtx_lock(&pr->pr_mtx);
2430 if (flags & PD_DEREF)
2432 /* If the prison still has references, nothing else to do. */
2433 if (pr->pr_ref > 0) {
2434 mtx_unlock(&pr->pr_mtx);
2435 if (flags & PD_LIST_SLOCKED)
2436 sx_sunlock(&allprison_lock);
2437 else if (flags & PD_LIST_XLOCKED)
2438 sx_xunlock(&allprison_lock);
2442 mtx_unlock(&pr->pr_mtx);
2443 if (flags & PD_LIST_SLOCKED) {
2444 if (!sx_try_upgrade(&allprison_lock)) {
2445 sx_sunlock(&allprison_lock);
2446 sx_xlock(&allprison_lock);
2448 } else if (!(flags & PD_LIST_XLOCKED))
2449 sx_xlock(&allprison_lock);
2451 TAILQ_REMOVE(&allprison, pr, pr_list);
2452 LIST_REMOVE(pr, pr_sibling);
2453 ppr = pr->pr_parent;
2454 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2455 tpr->pr_childcount--;
2456 sx_downgrade(&allprison_lock);
2459 if (pr->pr_vnet != ppr->pr_vnet)
2460 vnet_destroy(pr->pr_vnet);
2462 if (pr->pr_root != NULL) {
2463 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2465 VFS_UNLOCK_GIANT(vfslocked);
2467 mtx_destroy(&pr->pr_mtx);
2469 free(pr->pr_ip4, M_PRISON);
2472 free(pr->pr_ip6, M_PRISON);
2474 if (pr->pr_cpuset != NULL)
2475 cpuset_rel(pr->pr_cpuset);
2479 /* Removing a prison frees a reference on its parent. */
2481 mtx_lock(&pr->pr_mtx);
2482 flags = PD_DEREF | PD_LIST_SLOCKED;
2487 prison_hold_locked(struct prison *pr)
2490 mtx_assert(&pr->pr_mtx, MA_OWNED);
2491 KASSERT(pr->pr_ref > 0,
2492 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2497 prison_hold(struct prison *pr)
2500 mtx_lock(&pr->pr_mtx);
2501 prison_hold_locked(pr);
2502 mtx_unlock(&pr->pr_mtx);
2506 prison_proc_hold(struct prison *pr)
2509 mtx_lock(&pr->pr_mtx);
2510 KASSERT(pr->pr_uref > 0,
2511 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2513 mtx_unlock(&pr->pr_mtx);
2517 prison_proc_free(struct prison *pr)
2520 mtx_lock(&pr->pr_mtx);
2521 KASSERT(pr->pr_uref > 0,
2522 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2523 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2529 * Restrict a prison's IP address list with its parent's, possibly replacing
2530 * it. Return true if the replacement buffer was used (or would have been).
2533 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2538 ppr = pr->pr_parent;
2539 if (!(pr->pr_flags & PR_IP4_USER)) {
2540 /* This has no user settings, so just copy the parent's list. */
2541 if (pr->pr_ip4s < ppr->pr_ip4s) {
2543 * There's no room for the parent's list. Use the
2544 * new list buffer, which is assumed to be big enough
2545 * (if it was passed). If there's no buffer, try to
2549 if (newip4 == NULL) {
2550 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2551 M_PRISON, M_NOWAIT);
2555 if (newip4 != NULL) {
2556 bcopy(ppr->pr_ip4, newip4,
2557 ppr->pr_ip4s * sizeof(*newip4));
2558 free(pr->pr_ip4, M_PRISON);
2559 pr->pr_ip4 = newip4;
2560 pr->pr_ip4s = ppr->pr_ip4s;
2564 pr->pr_ip4s = ppr->pr_ip4s;
2565 if (pr->pr_ip4s > 0)
2566 bcopy(ppr->pr_ip4, pr->pr_ip4,
2567 pr->pr_ip4s * sizeof(*newip4));
2568 else if (pr->pr_ip4 != NULL) {
2569 free(pr->pr_ip4, M_PRISON);
2572 } else if (pr->pr_ip4s > 0) {
2573 /* Remove addresses that aren't in the parent. */
2574 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2575 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2577 if (ij < ppr->pr_ip4s)
2580 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2581 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2584 for (ij = 1; ii < pr->pr_ip4s; ) {
2585 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2589 switch (ij >= ppr->pr_ip4s ? -1 :
2590 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2592 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2593 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2604 if (pr->pr_ip4s == 0) {
2605 pr->pr_flags |= PR_IP4_DISABLE;
2606 free(pr->pr_ip4, M_PRISON);
2614 * Pass back primary IPv4 address of this jail.
2616 * If not restricted return success but do not alter the address. Caller has
2617 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2619 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2620 * Address returned in NBO.
2623 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2627 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2628 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2630 pr = cred->cr_prison;
2631 if (!(pr->pr_flags & PR_IP4))
2633 mtx_lock(&pr->pr_mtx);
2634 if (!(pr->pr_flags & PR_IP4)) {
2635 mtx_unlock(&pr->pr_mtx);
2638 if (pr->pr_ip4 == NULL) {
2639 mtx_unlock(&pr->pr_mtx);
2640 return (EAFNOSUPPORT);
2643 ia->s_addr = pr->pr_ip4[0].s_addr;
2644 mtx_unlock(&pr->pr_mtx);
2649 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2652 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2659 * No need to lock since the PR_IP4_USER flag can't be altered for
2662 while (pr1 != &prison0 &&
2664 !(pr1->pr_flags & PR_VNET) &&
2666 !(pr1->pr_flags & PR_IP4_USER))
2667 pr1 = pr1->pr_parent;
2668 while (pr2 != &prison0 &&
2670 !(pr2->pr_flags & PR_VNET) &&
2672 !(pr2->pr_flags & PR_IP4_USER))
2673 pr2 = pr2->pr_parent;
2674 return (pr1 == pr2);
2678 * Make sure our (source) address is set to something meaningful to this
2681 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2682 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2683 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2686 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2692 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2693 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2695 pr = cred->cr_prison;
2696 if (!(pr->pr_flags & PR_IP4))
2698 mtx_lock(&pr->pr_mtx);
2699 if (!(pr->pr_flags & PR_IP4)) {
2700 mtx_unlock(&pr->pr_mtx);
2703 if (pr->pr_ip4 == NULL) {
2704 mtx_unlock(&pr->pr_mtx);
2705 return (EAFNOSUPPORT);
2708 ia0.s_addr = ntohl(ia->s_addr);
2709 if (ia0.s_addr == INADDR_LOOPBACK) {
2710 ia->s_addr = pr->pr_ip4[0].s_addr;
2711 mtx_unlock(&pr->pr_mtx);
2715 if (ia0.s_addr == INADDR_ANY) {
2717 * In case there is only 1 IPv4 address, bind directly.
2719 if (pr->pr_ip4s == 1)
2720 ia->s_addr = pr->pr_ip4[0].s_addr;
2721 mtx_unlock(&pr->pr_mtx);
2725 error = _prison_check_ip4(pr, ia);
2726 mtx_unlock(&pr->pr_mtx);
2731 * Rewrite destination address in case we will connect to loopback address.
2733 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2734 * Address passed in in NBO and returned in NBO.
2737 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2741 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2742 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2744 pr = cred->cr_prison;
2745 if (!(pr->pr_flags & PR_IP4))
2747 mtx_lock(&pr->pr_mtx);
2748 if (!(pr->pr_flags & PR_IP4)) {
2749 mtx_unlock(&pr->pr_mtx);
2752 if (pr->pr_ip4 == NULL) {
2753 mtx_unlock(&pr->pr_mtx);
2754 return (EAFNOSUPPORT);
2757 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2758 ia->s_addr = pr->pr_ip4[0].s_addr;
2759 mtx_unlock(&pr->pr_mtx);
2764 * Return success because nothing had to be changed.
2766 mtx_unlock(&pr->pr_mtx);
2771 * Check if given address belongs to the jail referenced by cred/prison.
2773 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2774 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2775 * doesn't allow IPv4. Address passed in in NBO.
2778 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2783 * Check the primary IP.
2785 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2789 * All the other IPs are sorted so we can do a binary search.
2792 z = pr->pr_ip4s - 2;
2795 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2804 return (EADDRNOTAVAIL);
2808 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2813 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2814 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2816 pr = cred->cr_prison;
2817 if (!(pr->pr_flags & PR_IP4))
2819 mtx_lock(&pr->pr_mtx);
2820 if (!(pr->pr_flags & PR_IP4)) {
2821 mtx_unlock(&pr->pr_mtx);
2824 if (pr->pr_ip4 == NULL) {
2825 mtx_unlock(&pr->pr_mtx);
2826 return (EAFNOSUPPORT);
2829 error = _prison_check_ip4(pr, ia);
2830 mtx_unlock(&pr->pr_mtx);
2837 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2842 ppr = pr->pr_parent;
2843 if (!(pr->pr_flags & PR_IP6_USER)) {
2844 /* This has no user settings, so just copy the parent's list. */
2845 if (pr->pr_ip6s < ppr->pr_ip6s) {
2847 * There's no room for the parent's list. Use the
2848 * new list buffer, which is assumed to be big enough
2849 * (if it was passed). If there's no buffer, try to
2853 if (newip6 == NULL) {
2854 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2855 M_PRISON, M_NOWAIT);
2859 if (newip6 != NULL) {
2860 bcopy(ppr->pr_ip6, newip6,
2861 ppr->pr_ip6s * sizeof(*newip6));
2862 free(pr->pr_ip6, M_PRISON);
2863 pr->pr_ip6 = newip6;
2864 pr->pr_ip6s = ppr->pr_ip6s;
2868 pr->pr_ip6s = ppr->pr_ip6s;
2869 if (pr->pr_ip6s > 0)
2870 bcopy(ppr->pr_ip6, pr->pr_ip6,
2871 pr->pr_ip6s * sizeof(*newip6));
2872 else if (pr->pr_ip6 != NULL) {
2873 free(pr->pr_ip6, M_PRISON);
2876 } else if (pr->pr_ip6s > 0) {
2877 /* Remove addresses that aren't in the parent. */
2878 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2879 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2882 if (ij < ppr->pr_ip6s)
2885 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2886 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2889 for (ij = 1; ii < pr->pr_ip6s; ) {
2890 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2895 switch (ij >= ppr->pr_ip4s ? -1 :
2896 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2898 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2899 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2910 if (pr->pr_ip6s == 0) {
2911 pr->pr_flags |= PR_IP6_DISABLE;
2912 free(pr->pr_ip6, M_PRISON);
2920 * Pass back primary IPv6 address for this jail.
2922 * If not restricted return success but do not alter the address. Caller has
2923 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2925 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2928 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2932 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2933 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2935 pr = cred->cr_prison;
2936 if (!(pr->pr_flags & PR_IP6))
2938 mtx_lock(&pr->pr_mtx);
2939 if (!(pr->pr_flags & PR_IP6)) {
2940 mtx_unlock(&pr->pr_mtx);
2943 if (pr->pr_ip6 == NULL) {
2944 mtx_unlock(&pr->pr_mtx);
2945 return (EAFNOSUPPORT);
2948 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2949 mtx_unlock(&pr->pr_mtx);
2954 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
2957 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
2963 while (pr1 != &prison0 &&
2965 !(pr1->pr_flags & PR_VNET) &&
2967 !(pr1->pr_flags & PR_IP6_USER))
2968 pr1 = pr1->pr_parent;
2969 while (pr2 != &prison0 &&
2971 !(pr2->pr_flags & PR_VNET) &&
2973 !(pr2->pr_flags & PR_IP6_USER))
2974 pr2 = pr2->pr_parent;
2975 return (pr1 == pr2);
2979 * Make sure our (source) address is set to something meaningful to this jail.
2981 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2982 * when needed while binding.
2984 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
2985 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2986 * doesn't allow IPv6.
2989 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
2994 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2995 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2997 pr = cred->cr_prison;
2998 if (!(pr->pr_flags & PR_IP6))
3000 mtx_lock(&pr->pr_mtx);
3001 if (!(pr->pr_flags & PR_IP6)) {
3002 mtx_unlock(&pr->pr_mtx);
3005 if (pr->pr_ip6 == NULL) {
3006 mtx_unlock(&pr->pr_mtx);
3007 return (EAFNOSUPPORT);
3010 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3011 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3012 mtx_unlock(&pr->pr_mtx);
3016 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3018 * In case there is only 1 IPv6 address, and v6only is true,
3019 * then bind directly.
3021 if (v6only != 0 && pr->pr_ip6s == 1)
3022 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3023 mtx_unlock(&pr->pr_mtx);
3027 error = _prison_check_ip6(pr, ia6);
3028 mtx_unlock(&pr->pr_mtx);
3033 * Rewrite destination address in case we will connect to loopback address.
3035 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3038 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3042 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3043 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3045 pr = cred->cr_prison;
3046 if (!(pr->pr_flags & PR_IP6))
3048 mtx_lock(&pr->pr_mtx);
3049 if (!(pr->pr_flags & PR_IP6)) {
3050 mtx_unlock(&pr->pr_mtx);
3053 if (pr->pr_ip6 == NULL) {
3054 mtx_unlock(&pr->pr_mtx);
3055 return (EAFNOSUPPORT);
3058 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3059 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3060 mtx_unlock(&pr->pr_mtx);
3065 * Return success because nothing had to be changed.
3067 mtx_unlock(&pr->pr_mtx);
3072 * Check if given address belongs to the jail referenced by cred/prison.
3074 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3075 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3076 * doesn't allow IPv6.
3079 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3084 * Check the primary IP.
3086 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3090 * All the other IPs are sorted so we can do a binary search.
3093 z = pr->pr_ip6s - 2;
3096 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3105 return (EADDRNOTAVAIL);
3109 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3114 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3115 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3117 pr = cred->cr_prison;
3118 if (!(pr->pr_flags & PR_IP6))
3120 mtx_lock(&pr->pr_mtx);
3121 if (!(pr->pr_flags & PR_IP6)) {
3122 mtx_unlock(&pr->pr_mtx);
3125 if (pr->pr_ip6 == NULL) {
3126 mtx_unlock(&pr->pr_mtx);
3127 return (EAFNOSUPPORT);
3130 error = _prison_check_ip6(pr, ia6);
3131 mtx_unlock(&pr->pr_mtx);
3137 * Check if a jail supports the given address family.
3139 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3143 prison_check_af(struct ucred *cred, int af)
3148 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3150 pr = cred->cr_prison;
3152 /* Prisons with their own network stack are not limited. */
3153 if (pr->pr_flags & PR_VNET)
3162 if (pr->pr_flags & PR_IP4)
3164 mtx_lock(&pr->pr_mtx);
3165 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3166 error = EAFNOSUPPORT;
3167 mtx_unlock(&pr->pr_mtx);
3173 if (pr->pr_flags & PR_IP6)
3175 mtx_lock(&pr->pr_mtx);
3176 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3177 error = EAFNOSUPPORT;
3178 mtx_unlock(&pr->pr_mtx);
3186 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3187 error = EAFNOSUPPORT;
3193 * Check if given address belongs to the jail referenced by cred (wrapper to
3194 * prison_check_ip[46]).
3196 * Returns 0 if jail doesn't restrict the address family or if address belongs
3197 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3198 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3201 prison_if(struct ucred *cred, struct sockaddr *sa)
3204 struct sockaddr_in *sai;
3207 struct sockaddr_in6 *sai6;
3211 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3212 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3215 switch (sa->sa_family)
3219 sai = (struct sockaddr_in *)sa;
3220 error = prison_check_ip4(cred, &sai->sin_addr);
3225 sai6 = (struct sockaddr_in6 *)sa;
3226 error = prison_check_ip6(cred, &sai6->sin6_addr);
3230 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3231 error = EAFNOSUPPORT;
3237 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3240 prison_check(struct ucred *cred1, struct ucred *cred2)
3243 return ((cred1->cr_prison == cred2->cr_prison ||
3244 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3248 * Return 1 if p2 is a child of p1, otherwise 0.
3251 prison_ischild(struct prison *pr1, struct prison *pr2)
3254 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3261 * Return 1 if the passed credential is in a jail, otherwise 0.
3264 jailed(struct ucred *cred)
3267 return (cred->cr_prison != &prison0);
3271 * Return the correct hostname (domainname, et al) for the passed credential.
3274 getcredhostname(struct ucred *cred, char *buf, size_t size)
3279 * A NULL credential can be used to shortcut to the physical
3280 * system's hostname.
3282 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3283 mtx_lock(&pr->pr_mtx);
3284 strlcpy(buf, pr->pr_hostname, size);
3285 mtx_unlock(&pr->pr_mtx);
3289 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3292 mtx_lock(&cred->cr_prison->pr_mtx);
3293 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3294 mtx_unlock(&cred->cr_prison->pr_mtx);
3298 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3301 mtx_lock(&cred->cr_prison->pr_mtx);
3302 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3303 mtx_unlock(&cred->cr_prison->pr_mtx);
3307 getcredhostid(struct ucred *cred, unsigned long *hostid)
3310 mtx_lock(&cred->cr_prison->pr_mtx);
3311 *hostid = cred->cr_prison->pr_hostid;
3312 mtx_unlock(&cred->cr_prison->pr_mtx);
3317 * Determine whether the prison represented by cred owns
3318 * its vnet rather than having it inherited.
3320 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3323 prison_owns_vnet(struct ucred *cred)
3327 * vnets cannot be added/removed after jail creation,
3328 * so no need to lock here.
3330 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3335 * Determine whether the subject represented by cred can "see"
3336 * status of a mount point.
3337 * Returns: 0 for permitted, ENOENT otherwise.
3338 * XXX: This function should be called cr_canseemount() and should be
3339 * placed in kern_prot.c.
3342 prison_canseemount(struct ucred *cred, struct mount *mp)
3348 pr = cred->cr_prison;
3349 if (pr->pr_enforce_statfs == 0)
3351 if (pr->pr_root->v_mount == mp)
3353 if (pr->pr_enforce_statfs == 2)
3356 * If jail's chroot directory is set to "/" we should be able to see
3357 * all mount-points from inside a jail.
3358 * This is ugly check, but this is the only situation when jail's
3359 * directory ends with '/'.
3361 if (strcmp(pr->pr_path, "/") == 0)
3363 len = strlen(pr->pr_path);
3365 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3368 * Be sure that we don't have situation where jail's root directory
3369 * is "/some/path" and mount point is "/some/pathpath".
3371 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3377 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3379 char jpath[MAXPATHLEN];
3383 pr = cred->cr_prison;
3384 if (pr->pr_enforce_statfs == 0)
3386 if (prison_canseemount(cred, mp) != 0) {
3387 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3388 strlcpy(sp->f_mntonname, "[restricted]",
3389 sizeof(sp->f_mntonname));
3392 if (pr->pr_root->v_mount == mp) {
3394 * Clear current buffer data, so we are sure nothing from
3395 * the valid path left there.
3397 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3398 *sp->f_mntonname = '/';
3402 * If jail's chroot directory is set to "/" we should be able to see
3403 * all mount-points from inside a jail.
3405 if (strcmp(pr->pr_path, "/") == 0)
3407 len = strlen(pr->pr_path);
3408 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3410 * Clear current buffer data, so we are sure nothing from
3411 * the valid path left there.
3413 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3414 if (*jpath == '\0') {
3415 /* Should never happen. */
3416 *sp->f_mntonname = '/';
3418 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3423 * Check with permission for a specific privilege is granted within jail. We
3424 * have a specific list of accepted privileges; the rest are denied.
3427 prison_priv_check(struct ucred *cred, int priv)
3435 * Privileges specific to prisons with a virtual network stack.
3436 * There might be a duplicate entry here in case the privilege
3437 * is only granted conditionally in the legacy jail case.
3442 * NFS-specific privileges.
3444 case PRIV_NFS_DAEMON:
3445 case PRIV_NFS_LOCKD:
3448 * Network stack privileges.
3450 case PRIV_NET_BRIDGE:
3453 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3454 case PRIV_NET_ROUTE:
3456 case PRIV_NET_SETIFMTU:
3457 case PRIV_NET_SETIFFLAGS:
3458 case PRIV_NET_SETIFCAP:
3459 case PRIV_NET_SETIFNAME :
3460 case PRIV_NET_SETIFMETRIC:
3461 case PRIV_NET_SETIFPHYS:
3462 case PRIV_NET_SETIFMAC:
3463 case PRIV_NET_ADDMULTI:
3464 case PRIV_NET_DELMULTI:
3465 case PRIV_NET_HWIOCTL:
3466 case PRIV_NET_SETLLADDR:
3467 case PRIV_NET_ADDIFGROUP:
3468 case PRIV_NET_DELIFGROUP:
3469 case PRIV_NET_IFCREATE:
3470 case PRIV_NET_IFDESTROY:
3471 case PRIV_NET_ADDIFADDR:
3472 case PRIV_NET_DELIFADDR:
3475 case PRIV_NET_SETIFVNET:
3478 * 802.11-related privileges.
3480 case PRIV_NET80211_GETKEY:
3482 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3487 * AppleTalk privileges.
3489 case PRIV_NETATALK_RESERVEDPORT:
3494 case PRIV_NETATM_CFG:
3495 case PRIV_NETATM_ADD:
3496 case PRIV_NETATM_DEL:
3497 case PRIV_NETATM_SET:
3500 * Bluetooth privileges.
3502 case PRIV_NETBLUETOOTH_RAW:
3506 * Netgraph and netgraph module privileges.
3508 case PRIV_NETGRAPH_CONTROL:
3510 case PRIV_NETGRAPH_TTY:
3514 * IPv4 and IPv6 privileges.
3516 case PRIV_NETINET_IPFW:
3517 case PRIV_NETINET_DIVERT:
3518 case PRIV_NETINET_PF:
3519 case PRIV_NETINET_DUMMYNET:
3520 case PRIV_NETINET_CARP:
3521 case PRIV_NETINET_MROUTE:
3522 case PRIV_NETINET_RAW:
3523 case PRIV_NETINET_ADDRCTRL6:
3524 case PRIV_NETINET_ND6:
3525 case PRIV_NETINET_SCOPE6:
3526 case PRIV_NETINET_ALIFETIME6:
3527 case PRIV_NETINET_IPSEC:
3528 case PRIV_NETINET_BINDANY:
3532 * IPX/SPX privileges.
3534 case PRIV_NETIPX_RESERVEDPORT:
3535 case PRIV_NETIPX_RAW:
3549 * No default: or deny here.
3550 * In case of no permit fall through to next switch().
3552 if (cred->cr_prison->pr_flags & PR_VNET)
3560 * Allow ktrace privileges for root in jail.
3566 * Allow jailed processes to configure audit identity and
3567 * submit audit records (login, etc). In the future we may
3568 * want to further refine the relationship between audit and
3571 case PRIV_AUDIT_GETAUDIT:
3572 case PRIV_AUDIT_SETAUDIT:
3573 case PRIV_AUDIT_SUBMIT:
3577 * Allow jailed processes to manipulate process UNIX
3578 * credentials in any way they see fit.
3580 case PRIV_CRED_SETUID:
3581 case PRIV_CRED_SETEUID:
3582 case PRIV_CRED_SETGID:
3583 case PRIV_CRED_SETEGID:
3584 case PRIV_CRED_SETGROUPS:
3585 case PRIV_CRED_SETREUID:
3586 case PRIV_CRED_SETREGID:
3587 case PRIV_CRED_SETRESUID:
3588 case PRIV_CRED_SETRESGID:
3591 * Jail implements visibility constraints already, so allow
3592 * jailed root to override uid/gid-based constraints.
3594 case PRIV_SEEOTHERGIDS:
3595 case PRIV_SEEOTHERUIDS:
3598 * Jail implements inter-process debugging limits already, so
3599 * allow jailed root various debugging privileges.
3601 case PRIV_DEBUG_DIFFCRED:
3602 case PRIV_DEBUG_SUGID:
3603 case PRIV_DEBUG_UNPRIV:
3606 * Allow jail to set various resource limits and login
3607 * properties, and for now, exceed process resource limits.
3609 case PRIV_PROC_LIMIT:
3610 case PRIV_PROC_SETLOGIN:
3611 case PRIV_PROC_SETRLIMIT:
3614 * System V and POSIX IPC privileges are granted in jail.
3617 case PRIV_IPC_WRITE:
3618 case PRIV_IPC_ADMIN:
3619 case PRIV_IPC_MSGSIZE:
3623 * Jail operations within a jail work on child jails.
3625 case PRIV_JAIL_ATTACH:
3627 case PRIV_JAIL_REMOVE:
3630 * Jail implements its own inter-process limits, so allow
3631 * root processes in jail to change scheduling on other
3632 * processes in the same jail. Likewise for signalling.
3634 case PRIV_SCHED_DIFFCRED:
3635 case PRIV_SCHED_CPUSET:
3636 case PRIV_SIGNAL_DIFFCRED:
3637 case PRIV_SIGNAL_SUGID:
3640 * Allow jailed processes to write to sysctls marked as jail
3643 case PRIV_SYSCTL_WRITEJAIL:
3646 * Allow root in jail to manage a variety of quota
3647 * properties. These should likely be conditional on a
3648 * configuration option.
3650 case PRIV_VFS_GETQUOTA:
3651 case PRIV_VFS_SETQUOTA:
3654 * Since Jail relies on chroot() to implement file system
3655 * protections, grant many VFS privileges to root in jail.
3656 * Be careful to exclude mount-related and NFS-related
3660 case PRIV_VFS_WRITE:
3661 case PRIV_VFS_ADMIN:
3663 case PRIV_VFS_LOOKUP:
3664 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3665 case PRIV_VFS_CHFLAGS_DEV:
3666 case PRIV_VFS_CHOWN:
3667 case PRIV_VFS_CHROOT:
3668 case PRIV_VFS_RETAINSUGID:
3669 case PRIV_VFS_FCHROOT:
3671 case PRIV_VFS_SETGID:
3673 case PRIV_VFS_STICKYFILE:
3677 * Depending on the global setting, allow privilege of
3678 * setting system flags.
3680 case PRIV_VFS_SYSFLAGS:
3681 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3687 * Depending on the global setting, allow privilege of
3688 * mounting/unmounting file systems.
3690 case PRIV_VFS_MOUNT:
3691 case PRIV_VFS_UNMOUNT:
3692 case PRIV_VFS_MOUNT_NONUSER:
3693 case PRIV_VFS_MOUNT_OWNER:
3694 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3700 * Allow jailed root to bind reserved ports and reuse in-use
3703 case PRIV_NETINET_RESERVEDPORT:
3704 case PRIV_NETINET_REUSEPORT:
3708 * Allow jailed root to set certian IPv4/6 (option) headers.
3710 case PRIV_NETINET_SETHDROPTS:
3714 * Conditionally allow creating raw sockets in jail.
3716 case PRIV_NETINET_RAW:
3717 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3723 * Since jail implements its own visibility limits on netstat
3724 * sysctls, allow getcred. This allows identd to work in
3727 case PRIV_NETINET_GETCRED:
3732 * In all remaining cases, deny the privilege request. This
3733 * includes almost all network privileges, many system
3734 * configuration privileges.
3741 * Return the part of pr2's name that is relative to pr1, or the whole name
3742 * if it does not directly follow.
3746 prison_name(struct prison *pr1, struct prison *pr2)
3750 /* Jails see themselves as "0" (if they see themselves at all). */
3753 name = pr2->pr_name;
3754 if (prison_ischild(pr1, pr2)) {
3756 * pr1 isn't locked (and allprison_lock may not be either)
3757 * so its length can't be counted on. But the number of dots
3758 * can be counted on - and counted.
3760 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3761 name = strchr(name, '.') + 1;
3767 * Return the part of pr2's path that is relative to pr1, or the whole path
3768 * if it does not directly follow.
3771 prison_path(struct prison *pr1, struct prison *pr2)
3773 char *path1, *path2;
3776 path1 = pr1->pr_path;
3777 path2 = pr2->pr_path;
3778 if (!strcmp(path1, "/"))
3780 len1 = strlen(path1);
3781 if (strncmp(path1, path2, len1))
3783 if (path2[len1] == '\0')
3785 if (path2[len1] == '/')
3786 return (path2 + len1);
3792 * Jail-related sysctls.
3794 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3798 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3801 struct prison *pr, *cpr;
3803 struct in_addr *ip4 = NULL;
3807 struct in_addr *ip6 = NULL;
3812 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3813 pr = req->td->td_ucred->cr_prison;
3815 sx_slock(&allprison_lock);
3816 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3817 #if defined(INET) || defined(INET6)
3820 mtx_lock(&cpr->pr_mtx);
3822 if (cpr->pr_ip4s > 0) {
3823 if (ip4s < cpr->pr_ip4s) {
3824 ip4s = cpr->pr_ip4s;
3825 mtx_unlock(&cpr->pr_mtx);
3826 ip4 = realloc(ip4, ip4s *
3827 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3830 bcopy(cpr->pr_ip4, ip4,
3831 cpr->pr_ip4s * sizeof(struct in_addr));
3835 if (cpr->pr_ip6s > 0) {
3836 if (ip6s < cpr->pr_ip6s) {
3837 ip6s = cpr->pr_ip6s;
3838 mtx_unlock(&cpr->pr_mtx);
3839 ip6 = realloc(ip6, ip6s *
3840 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3843 bcopy(cpr->pr_ip6, ip6,
3844 cpr->pr_ip6s * sizeof(struct in6_addr));
3847 if (cpr->pr_ref == 0) {
3848 mtx_unlock(&cpr->pr_mtx);
3851 bzero(xp, sizeof(*xp));
3852 xp->pr_version = XPRISON_VERSION;
3853 xp->pr_id = cpr->pr_id;
3854 xp->pr_state = cpr->pr_uref > 0
3855 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3856 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3857 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3858 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3860 xp->pr_ip4s = cpr->pr_ip4s;
3863 xp->pr_ip6s = cpr->pr_ip6s;
3865 mtx_unlock(&cpr->pr_mtx);
3866 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3870 if (xp->pr_ip4s > 0) {
3871 error = SYSCTL_OUT(req, ip4,
3872 xp->pr_ip4s * sizeof(struct in_addr));
3878 if (xp->pr_ip6s > 0) {
3879 error = SYSCTL_OUT(req, ip6,
3880 xp->pr_ip6s * sizeof(struct in6_addr));
3886 sx_sunlock(&allprison_lock);
3897 SYSCTL_OID(_security_jail, OID_AUTO, list,
3898 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3899 sysctl_jail_list, "S", "List of active jails");
3902 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3906 injail = jailed(req->td->td_ucred);
3907 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3912 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3913 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3914 sysctl_jail_jailed, "I", "Process in jail?");
3916 #if defined(INET) || defined(INET6)
3917 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3918 &jail_max_af_ips, 0,
3919 "Number of IP addresses a jail may have at most per address family");
3923 * Default parameters for jail(2) compatability. For historical reasons,
3924 * the sysctl names have varying similarity to the parameter names. Prisons
3925 * just see their own parameters, and can't change them.
3928 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3931 int allow, error, i;
3933 pr = req->td->td_ucred->cr_prison;
3934 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
3936 /* Get the current flag value, and convert it to a boolean. */
3937 i = (allow & arg2) ? 1 : 0;
3940 error = sysctl_handle_int(oidp, &i, 0, req);
3941 if (error || !req->newptr)
3947 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3950 mtx_lock(&prison0.pr_mtx);
3951 jail_default_allow = (jail_default_allow & ~arg2) | i;
3952 mtx_unlock(&prison0.pr_mtx);
3956 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3957 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3958 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3959 "Processes in jail can set their hostnames");
3960 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3961 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3962 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3963 "Processes in jail are limited to creating UNIX/IP/route sockets only");
3964 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3965 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3966 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3967 "Processes in jail can use System V IPC primitives");
3968 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3969 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3970 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3971 "Prison root can create raw sockets");
3972 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3973 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3974 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3975 "Processes in jail can alter system file flags");
3976 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3977 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3978 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3979 "Processes in jail can mount/unmount jail-friendly file systems");
3982 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3987 pr = req->td->td_ucred->cr_prison;
3988 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
3989 error = sysctl_handle_int(oidp, &level, 0, req);
3990 if (error || !req->newptr)
3992 *(int *)arg1 = level;
3996 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3997 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3998 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
3999 sysctl_jail_default_level, "I",
4000 "Processes in jail cannot see all mounted file systems");
4003 * Nodes to describe jail parameters. Maximum length of string parameters
4004 * is returned in the string itself, and the other parameters exist merely
4005 * to make themselves and their types known.
4007 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4011 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4018 switch (oidp->oid_kind & CTLTYPE)
4024 if (!(req->flags & SCTL_MASK32))
4026 return (SYSCTL_OUT(req, &l, sizeof(l)));
4030 return (SYSCTL_OUT(req, &i, sizeof(i)));
4031 case CTLTYPE_STRING:
4032 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4034 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4035 case CTLTYPE_STRUCT:
4037 return (SYSCTL_OUT(req, &s, sizeof(s)));
4042 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4043 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4044 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4045 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4046 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4047 "I", "Jail secure level");
4048 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4049 "I", "Jail cannot see all mounted file systems");
4050 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4051 "B", "Jail persistence");
4053 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4054 "E,jailsys", "Virtual network stack");
4056 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4057 "B", "Jail is in the process of shutting down");
4059 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4060 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4061 "I", "Current number of child jails");
4062 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4063 "I", "Maximum number of child jails");
4065 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4066 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4068 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4069 "Jail NIS domainname");
4070 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4072 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4073 "LU", "Jail host ID");
4075 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4076 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4079 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4080 "Jail IPv4 address virtualization");
4081 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4082 "S,in_addr,a", "Jail IPv4 addresses");
4085 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4086 "Jail IPv6 address virtualization");
4087 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4088 "S,in6_addr,a", "Jail IPv6 addresses");
4091 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4092 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4093 "B", "Jail may set hostname");
4094 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4095 "B", "Jail may use SYSV IPC");
4096 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4097 "B", "Jail may create raw sockets");
4098 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4099 "B", "Jail may alter system file flags");
4100 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4101 "B", "Jail may mount/unmount jail-friendly file systems");
4102 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4103 "B", "Jail may set file quotas");
4104 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4105 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4111 db_show_prison(struct prison *pr)
4114 #if defined(INET) || defined(INET6)
4119 char ip6buf[INET6_ADDRSTRLEN];
4122 db_printf("prison %p:\n", pr);
4123 db_printf(" jid = %d\n", pr->pr_id);
4124 db_printf(" name = %s\n", pr->pr_name);
4125 db_printf(" parent = %p\n", pr->pr_parent);
4126 db_printf(" ref = %d\n", pr->pr_ref);
4127 db_printf(" uref = %d\n", pr->pr_uref);
4128 db_printf(" path = %s\n", pr->pr_path);
4129 db_printf(" cpuset = %d\n", pr->pr_cpuset
4130 ? pr->pr_cpuset->cs_id : -1);
4132 db_printf(" vnet = %p\n", pr->pr_vnet);
4134 db_printf(" root = %p\n", pr->pr_root);
4135 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4136 db_printf(" childcount = %d\n", pr->pr_childcount);
4137 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4138 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4139 db_printf(" flags = %x", pr->pr_flags);
4140 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4142 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4143 db_printf(" %s", pr_flag_names[fi]);
4144 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4146 jsf = pr->pr_flags &
4147 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4148 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4149 pr_flag_jailsys[fi].disable &&
4150 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4151 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4154 db_printf(" allow = %x", pr->pr_allow);
4155 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4157 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4158 db_printf(" %s", pr_allow_names[fi]);
4160 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4161 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4162 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4163 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4164 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4166 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4167 for (ii = 0; ii < pr->pr_ip4s; ii++)
4168 db_printf(" %s %s\n",
4169 ii == 0 ? "ip4 =" : " ",
4170 inet_ntoa(pr->pr_ip4[ii]));
4173 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4174 for (ii = 0; ii < pr->pr_ip6s; ii++)
4175 db_printf(" %s %s\n",
4176 ii == 0 ? "ip6 =" : " ",
4177 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4181 DB_SHOW_COMMAND(prison, db_show_prison_command)
4187 * Show all prisons in the list, and prison0 which is not
4190 db_show_prison(&prison0);
4191 if (!db_pager_quit) {
4192 TAILQ_FOREACH(pr, &allprison, pr_list) {
4204 /* Look for a prison with the ID and with references. */
4205 TAILQ_FOREACH(pr, &allprison, pr_list)
4206 if (pr->pr_id == addr && pr->pr_ref > 0)
4209 /* Look again, without requiring a reference. */
4210 TAILQ_FOREACH(pr, &allprison, pr_list)
4211 if (pr->pr_id == addr)
4214 /* Assume address points to a valid prison. */
4215 pr = (struct prison *)addr;