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>
52 #include <sys/racct.h>
53 #include <sys/refcount.h>
55 #include <sys/sysent.h>
56 #include <sys/namei.h>
57 #include <sys/mount.h>
58 #include <sys/queue.h>
59 #include <sys/socket.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysctl.h>
62 #include <sys/vnode.h>
67 #include <netinet/in.h>
72 #include <netinet6/in6_var.h>
76 #include <security/mac/mac_framework.h>
78 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
80 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
81 MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
83 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
86 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
88 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
92 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
94 #define _PR_IP_SADDRSEL 0
98 /* prison0 describes what is "real" about the system. */
99 struct prison prison0 = {
105 .pr_securelevel = -1,
106 .pr_childmax = JAIL_MAX,
107 .pr_hostuuid = DEFAULT_HOSTUUID,
108 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
110 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
112 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
114 .pr_allow = PR_ALLOW_ALL,
116 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
118 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
119 struct sx allprison_lock;
120 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
121 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
122 LIST_HEAD(, prison_racct) allprison_racct;
125 static int do_jail_attach(struct thread *td, struct prison *pr);
126 static void prison_complete(void *context, int pending);
127 static void prison_deref(struct prison *pr, int flags);
128 static char *prison_path(struct prison *pr1, struct prison *pr2);
129 static void prison_remove_one(struct prison *pr);
131 static void prison_racct_attach(struct prison *pr);
132 static void prison_racct_detach(struct prison *pr);
135 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
136 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
139 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
140 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
143 /* Flags for prison_deref */
144 #define PD_DEREF 0x01
145 #define PD_DEUREF 0x02
146 #define PD_LOCKED 0x04
147 #define PD_LIST_SLOCKED 0x08
148 #define PD_LIST_XLOCKED 0x10
151 * Parameter names corresponding to PR_* flag values. Size values are for kvm
152 * as we cannot figure out the size of a sparse array, or an array without a
155 static char *pr_flag_names[] = {
158 [7] = "ip4.saddrsel",
161 [8] = "ip6.saddrsel",
164 const size_t pr_flag_names_size = sizeof(pr_flag_names);
166 static char *pr_flag_nonames[] = {
169 [7] = "ip4.nosaddrsel",
172 [8] = "ip6.nosaddrsel",
175 const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
177 struct jailsys_flags {
181 } pr_flag_jailsys[] = {
182 { "host", 0, PR_HOST },
184 { "vnet", 0, PR_VNET },
187 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
190 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
193 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
195 static char *pr_allow_names[] = {
196 "allow.set_hostname",
204 const size_t pr_allow_names_size = sizeof(pr_allow_names);
206 static char *pr_allow_nonames[] = {
207 "allow.noset_hostname",
209 "allow.noraw_sockets",
215 const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
217 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
218 #define JAIL_DEFAULT_ENFORCE_STATFS 2
219 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
220 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
221 #if defined(INET) || defined(INET6)
222 static unsigned jail_max_af_ips = 255;
227 qcmp_v4(const void *ip1, const void *ip2)
232 * We need to compare in HBO here to get the list sorted as expected
233 * by the result of the code. Sorting NBO addresses gives you
234 * interesting results. If you do not understand, do not try.
236 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
237 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
240 * Do not simply return the difference of the two numbers, the int is
254 qcmp_v6(const void *ip1, const void *ip2)
256 const struct in6_addr *ia6a, *ia6b;
259 ia6a = (const struct in6_addr *)ip1;
260 ia6b = (const struct in6_addr *)ip2;
263 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
264 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
266 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
279 sys_jail(struct thread *td, struct jail_args *uap)
285 error = copyin(uap->jail, &version, sizeof(uint32_t));
294 /* FreeBSD single IPv4 jails. */
295 bzero(&j, sizeof(struct jail));
296 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
299 j.version = j0.version;
301 j.hostname = j0.hostname;
302 j.ip4s = j0.ip_number;
308 * Version 1 was used by multi-IPv4 jail implementations
309 * that never made it into the official kernel.
313 case 2: /* JAIL_API_VERSION */
314 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
315 error = copyin(uap->jail, &j, sizeof(struct jail));
321 /* Sci-Fi jails are not supported, sorry. */
324 return (kern_jail(td, &j));
328 kern_jail(struct thread *td, struct jail *j)
330 struct iovec optiov[2 * (4
331 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
340 char *u_path, *u_hostname, *u_name;
343 struct in_addr *u_ip4;
346 struct in6_addr *u_ip6;
349 int error, enforce_statfs, fi;
351 bzero(&optiov, sizeof(optiov));
352 opt.uio_iov = optiov;
356 opt.uio_segflg = UIO_SYSSPACE;
357 opt.uio_rw = UIO_READ;
360 /* Set permissions for top-level jails from sysctls. */
361 if (!jailed(td->td_ucred)) {
362 for (fi = 0; fi < sizeof(pr_allow_names) /
363 sizeof(pr_allow_names[0]); fi++) {
364 optiov[opt.uio_iovcnt].iov_base =
365 (jail_default_allow & (1 << fi))
366 ? pr_allow_names[fi] : pr_allow_nonames[fi];
367 optiov[opt.uio_iovcnt].iov_len =
368 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
371 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
372 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
374 enforce_statfs = jail_default_enforce_statfs;
375 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
376 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
380 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
382 ip4s = (j->version == 0) ? 1 : j->ip4s;
383 if (ip4s > jail_max_af_ips)
385 tmplen += ip4s * sizeof(struct in_addr);
391 if (j->ip6s > jail_max_af_ips)
393 tmplen += j->ip6s * sizeof(struct in6_addr);
398 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
399 u_hostname = u_path + MAXPATHLEN;
400 u_name = u_hostname + MAXHOSTNAMELEN;
402 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
406 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
408 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
411 optiov[opt.uio_iovcnt].iov_base = "path";
412 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
414 optiov[opt.uio_iovcnt].iov_base = u_path;
415 error = copyinstr(j->path, u_path, MAXPATHLEN,
416 &optiov[opt.uio_iovcnt].iov_len);
418 free(u_path, M_TEMP);
422 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
423 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
425 optiov[opt.uio_iovcnt].iov_base = u_hostname;
426 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
427 &optiov[opt.uio_iovcnt].iov_len);
429 free(u_path, M_TEMP);
433 if (j->jailname != NULL) {
434 optiov[opt.uio_iovcnt].iov_base = "name";
435 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
437 optiov[opt.uio_iovcnt].iov_base = u_name;
438 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
439 &optiov[opt.uio_iovcnt].iov_len);
441 free(u_path, M_TEMP);
447 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
448 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
450 optiov[opt.uio_iovcnt].iov_base = u_ip4;
451 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
453 u_ip4->s_addr = j->ip4s;
455 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
457 free(u_path, M_TEMP);
464 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
465 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
467 optiov[opt.uio_iovcnt].iov_base = u_ip6;
468 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
469 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
471 free(u_path, M_TEMP);
476 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
477 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
478 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
479 free(u_path, M_TEMP);
485 * struct jail_set_args {
486 * struct iovec *iovp;
487 * unsigned int iovcnt;
492 sys_jail_set(struct thread *td, struct jail_set_args *uap)
497 /* Check that we have an even number of iovecs. */
501 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
504 error = kern_jail_set(td, auio, uap->flags);
510 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
517 struct in6_addr *ip6;
520 struct vfsoptlist *opts;
521 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
523 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
524 #if defined(INET) || defined(INET6)
529 size_t namelen, onamelen;
530 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
531 int gotchildmax, gotenforce, gothid, gotslevel;
532 int fi, jid, jsys, len, level;
533 int childmax, slevel, vfslocked;
534 #if defined(INET) || defined(INET6)
543 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
547 error = priv_check(td, PRIV_JAIL_SET);
548 if (!error && (flags & JAIL_ATTACH))
549 error = priv_check(td, PRIV_JAIL_ATTACH);
552 mypr = ppr = td->td_ucred->cr_prison;
553 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
555 if (flags & ~JAIL_SET_MASK)
559 * Check all the parameters before committing to anything. Not all
560 * errors can be caught early, but we may as well try. Also, this
561 * takes care of some expensive stuff (path lookup) before getting
562 * the allprison lock.
564 * XXX Jails are not filesystems, and jail parameters are not mount
565 * options. But it makes more sense to re-use the vfsopt code
566 * than duplicate it under a different name.
568 error = vfs_buildopts(optuio, &opts);
578 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
584 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
593 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
601 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
606 else if (enforce < 0 || enforce > 2) {
612 pr_flags = ch_flags = 0;
613 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
615 if (pr_flag_names[fi] == NULL)
617 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
618 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
620 ch_flags |= pr_flags;
621 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
623 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
630 case JAIL_SYS_DISABLE:
631 if (!pr_flag_jailsys[fi].disable) {
635 pr_flags |= pr_flag_jailsys[fi].disable;
638 pr_flags |= pr_flag_jailsys[fi].new;
640 case JAIL_SYS_INHERIT:
647 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
649 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
650 && !(pr_flags & PR_PERSIST)) {
652 vfs_opterror(opts, "new jail must persist or attach");
656 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
658 vfs_opterror(opts, "vnet cannot be changed after creation");
663 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
665 vfs_opterror(opts, "ip4 cannot be changed after creation");
670 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
672 vfs_opterror(opts, "ip6 cannot be changed after creation");
677 pr_allow = ch_allow = 0;
678 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
680 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
681 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
683 ch_allow |= pr_allow;
685 error = vfs_getopt(opts, "name", (void **)&name, &len);
691 if (len == 0 || name[len - 1] != '\0') {
695 if (len > MAXHOSTNAMELEN) {
696 error = ENAMETOOLONG;
701 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
709 if (len == 0 || host[len - 1] != '\0') {
713 if (len > MAXHOSTNAMELEN) {
714 error = ENAMETOOLONG;
719 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
727 if (len == 0 || domain[len - 1] != '\0') {
731 if (len > MAXHOSTNAMELEN) {
732 error = ENAMETOOLONG;
737 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
745 if (len == 0 || uuid[len - 1] != '\0') {
749 if (len > HOSTUUIDLEN) {
750 error = ENAMETOOLONG;
755 #ifdef COMPAT_FREEBSD32
756 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
759 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
763 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
775 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
777 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
780 else if (ip4s & (sizeof(*ip4) - 1)) {
784 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
786 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
788 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
789 ip4s /= sizeof(*ip4);
790 if (ip4s > jail_max_af_ips) {
792 vfs_opterror(opts, "too many IPv4 addresses");
795 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
796 bcopy(op, ip4, ip4s * sizeof(*ip4));
798 * IP addresses are all sorted but ip[0] to preserve
799 * the primary IP address as given from userland.
800 * This special IP is used for unbound outgoing
801 * connections as well for "loopback" traffic in case
802 * source address selection cannot find any more fitting
803 * address to connect from.
806 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
808 * Check for duplicate addresses and do some simple
809 * zero and broadcast checks. If users give other bogus
810 * addresses it is their problem.
812 * We do not have to care about byte order for these
813 * checks so we will do them in NBO.
815 for (ii = 0; ii < ip4s; ii++) {
816 if (ip4[ii].s_addr == INADDR_ANY ||
817 ip4[ii].s_addr == INADDR_BROADCAST) {
822 (ip4[0].s_addr == ip4[ii+1].s_addr ||
823 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
833 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
835 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
838 else if (ip6s & (sizeof(*ip6) - 1)) {
842 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
844 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
846 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
847 ip6s /= sizeof(*ip6);
848 if (ip6s > jail_max_af_ips) {
850 vfs_opterror(opts, "too many IPv6 addresses");
853 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
854 bcopy(op, ip6, ip6s * sizeof(*ip6));
856 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
857 for (ii = 0; ii < ip6s; ii++) {
858 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
863 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
864 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
874 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
875 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
878 "vnet jails cannot have IP address restrictions");
884 error = vfs_getopt(opts, "path", (void **)&path, &len);
890 if (flags & JAIL_UPDATE) {
893 "path cannot be changed after creation");
896 if (len == 0 || path[len - 1] != '\0') {
900 if (len < 2 || (len == 2 && path[0] == '/'))
903 /* Leave room for a real-root full pathname. */
904 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
905 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
906 error = ENAMETOOLONG;
909 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
914 vfslocked = NDHASGIANT(&nd);
916 NDFREE(&nd, NDF_ONLY_PNBUF);
917 if (root->v_type != VDIR) {
920 VFS_UNLOCK_GIANT(vfslocked);
923 VFS_UNLOCK_GIANT(vfslocked);
928 * Grab the allprison lock before letting modules check their
929 * parameters. Once we have it, do not let go so we'll have a
930 * consistent view of the OSD list.
932 sx_xlock(&allprison_lock);
933 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
935 goto done_unlock_list;
937 /* By now, all parameters should have been noted. */
938 TAILQ_FOREACH(opt, opts, link) {
939 if (!opt->seen && strcmp(opt->name, "errmsg")) {
941 vfs_opterror(opts, "unknown parameter: %s", opt->name);
942 goto done_unlock_list;
947 * See if we are creating a new record or updating an existing one.
948 * This abuses the file error codes ENOENT and EEXIST.
950 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
953 vfs_opterror(opts, "no valid operation (create or update)");
954 goto done_unlock_list;
958 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
959 namelc = strrchr(name, '.');
960 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
966 * See if a requested jid already exists. There is an
967 * information leak here if the jid exists but is not within
968 * the caller's jail hierarchy. Jail creators will get EEXIST
969 * even though they cannot see the jail, and CREATE | UPDATE
970 * will return ENOENT which is not normally a valid error.
974 vfs_opterror(opts, "negative jid");
975 goto done_unlock_list;
977 pr = prison_find(jid);
980 /* Create: jid must not exist. */
981 if (cuflags == JAIL_CREATE) {
982 mtx_unlock(&pr->pr_mtx);
984 vfs_opterror(opts, "jail %d already exists",
986 goto done_unlock_list;
988 if (!prison_ischild(mypr, pr)) {
989 mtx_unlock(&pr->pr_mtx);
991 } else if (pr->pr_uref == 0) {
992 if (!(flags & JAIL_DYING)) {
993 mtx_unlock(&pr->pr_mtx);
995 vfs_opterror(opts, "jail %d is dying",
997 goto done_unlock_list;
998 } else if ((flags & JAIL_ATTACH) ||
999 (pr_flags & PR_PERSIST)) {
1001 * A dying jail might be resurrected
1002 * (via attach or persist), but first
1003 * it must determine if another jail
1004 * has claimed its name. Accomplish
1005 * this by implicitly re-setting the
1009 name = prison_name(mypr, pr);
1014 /* Update: jid must exist. */
1015 if (cuflags == JAIL_UPDATE) {
1017 vfs_opterror(opts, "jail %d not found", jid);
1018 goto done_unlock_list;
1023 * If the caller provided a name, look for a jail by that name.
1024 * This has different semantics for creates and updates keyed by jid
1025 * (where the name must not already exist in a different jail),
1026 * and updates keyed by the name itself (where the name must exist
1027 * because that is the jail being updated).
1030 namelc = strrchr(name, '.');
1035 * This is a hierarchical name. Split it into the
1036 * parent and child names, and make sure the parent
1037 * exists or matches an already found jail.
1041 if (strncmp(name, ppr->pr_name, namelc - name)
1042 || ppr->pr_name[namelc - name] != '\0') {
1043 mtx_unlock(&pr->pr_mtx);
1046 "cannot change jail's parent");
1047 goto done_unlock_list;
1050 ppr = prison_find_name(mypr, name);
1054 "jail \"%s\" not found", name);
1055 goto done_unlock_list;
1057 mtx_unlock(&ppr->pr_mtx);
1061 if (name[0] != '\0') {
1063 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1066 FOREACH_PRISON_CHILD(ppr, tpr) {
1067 if (tpr != pr && tpr->pr_ref > 0 &&
1068 !strcmp(tpr->pr_name + namelen, name)) {
1070 cuflags != JAIL_CREATE) {
1071 mtx_lock(&tpr->pr_mtx);
1072 if (tpr->pr_ref > 0) {
1077 if (tpr->pr_uref > 0) {
1083 mtx_unlock(&tpr->pr_mtx);
1084 } else if (tpr->pr_uref > 0) {
1086 * Create, or update(jid):
1087 * name must not exist in an
1088 * active sibling jail.
1092 mtx_unlock(&pr->pr_mtx);
1094 "jail \"%s\" already exists",
1096 goto done_unlock_list;
1100 /* If no active jail is found, use a dying one. */
1101 if (deadpr != NULL && pr == NULL) {
1102 if (flags & JAIL_DYING) {
1103 mtx_lock(&deadpr->pr_mtx);
1104 if (deadpr->pr_ref == 0) {
1105 mtx_unlock(&deadpr->pr_mtx);
1109 } else if (cuflags == JAIL_UPDATE) {
1112 "jail \"%s\" is dying", name);
1113 goto done_unlock_list;
1116 /* Update: name must exist if no jid. */
1117 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1119 vfs_opterror(opts, "jail \"%s\" not found",
1121 goto done_unlock_list;
1125 /* Update: must provide a jid or name. */
1126 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1128 vfs_opterror(opts, "update specified no jail");
1129 goto done_unlock_list;
1132 /* If there's no prison to update, create a new one and link it in. */
1134 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1135 if (tpr->pr_childcount >= tpr->pr_childmax) {
1137 vfs_opterror(opts, "prison limit exceeded");
1138 goto done_unlock_list;
1141 mtx_lock(&ppr->pr_mtx);
1142 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1143 mtx_unlock(&ppr->pr_mtx);
1145 vfs_opterror(opts, "parent jail went away!");
1146 goto done_unlock_list;
1150 mtx_unlock(&ppr->pr_mtx);
1151 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1153 /* Find the next free jid. */
1156 if (jid == JAIL_MAX)
1158 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1159 if (tpr->pr_id < jid)
1161 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1162 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1165 if (jid == lastprid) {
1168 "no available jail IDs");
1170 prison_deref(ppr, PD_DEREF |
1171 PD_DEUREF | PD_LIST_XLOCKED);
1180 * The jail already has a jid (that did not yet exist),
1181 * so just find where to insert it.
1183 TAILQ_FOREACH(tpr, &allprison, pr_list)
1184 if (tpr->pr_id >= jid) {
1185 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1190 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1191 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1192 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1193 tpr->pr_childcount++;
1195 pr->pr_parent = ppr;
1198 /* Set some default values, and inherit some from the parent. */
1203 root = mypr->pr_root;
1206 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1207 pr->pr_flags |= PR_HOST;
1208 #if defined(INET) || defined(INET6)
1210 if (!(pr_flags & PR_VNET))
1214 if (!(ch_flags & PR_IP4_USER))
1216 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1217 else if (!(pr_flags & PR_IP4_USER)) {
1218 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1219 if (ppr->pr_ip4 != NULL) {
1220 pr->pr_ip4s = ppr->pr_ip4s;
1221 pr->pr_ip4 = malloc(pr->pr_ip4s *
1222 sizeof(struct in_addr), M_PRISON,
1224 bcopy(ppr->pr_ip4, pr->pr_ip4,
1225 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1230 if (!(ch_flags & PR_IP6_USER))
1232 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1233 else if (!(pr_flags & PR_IP6_USER)) {
1234 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1235 if (ppr->pr_ip6 != NULL) {
1236 pr->pr_ip6s = ppr->pr_ip6s;
1237 pr->pr_ip6 = malloc(pr->pr_ip6s *
1238 sizeof(struct in6_addr), M_PRISON,
1240 bcopy(ppr->pr_ip6, pr->pr_ip6,
1241 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1247 /* Source address selection is always on by default. */
1248 pr->pr_flags |= _PR_IP_SADDRSEL;
1250 pr->pr_securelevel = ppr->pr_securelevel;
1251 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1252 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1254 LIST_INIT(&pr->pr_children);
1255 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1258 /* Allocate a new vnet if specified. */
1259 pr->pr_vnet = (pr_flags & PR_VNET)
1260 ? vnet_alloc() : ppr->pr_vnet;
1263 * Allocate a dedicated cpuset for each jail.
1264 * Unlike other initial settings, this may return an erorr.
1266 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1268 prison_deref(pr, PD_LIST_XLOCKED);
1272 mtx_lock(&pr->pr_mtx);
1274 * New prisons do not yet have a reference, because we do not
1275 * want other to see the incomplete prison once the
1276 * allprison_lock is downgraded.
1281 * Grab a reference for existing prisons, to ensure they
1282 * continue to exist for the duration of the call.
1285 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1286 if ((pr->pr_flags & PR_VNET) &&
1287 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1290 "vnet jails cannot have IP address restrictions");
1291 goto done_deref_locked;
1295 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1298 "ip4 cannot be changed after creation");
1299 goto done_deref_locked;
1303 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1306 "ip6 cannot be changed after creation");
1307 goto done_deref_locked;
1312 /* Do final error checking before setting anything. */
1314 if (slevel < ppr->pr_securelevel) {
1316 goto done_deref_locked;
1320 if (childmax >= ppr->pr_childmax) {
1322 goto done_deref_locked;
1326 if (enforce < ppr->pr_enforce_statfs) {
1328 goto done_deref_locked;
1333 if (ppr->pr_flags & PR_IP4) {
1335 * Make sure the new set of IP addresses is a
1336 * subset of the parent's list. Don't worry
1337 * about the parent being unlocked, as any
1338 * setting is done with allprison_lock held.
1340 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1341 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1343 if (ij == ppr->pr_ip4s) {
1345 goto done_deref_locked;
1348 for (ii = ij = 1; ii < ip4s; ii++) {
1349 if (ip4[ii].s_addr ==
1350 ppr->pr_ip4[0].s_addr)
1352 for (; ij < ppr->pr_ip4s; ij++)
1353 if (ip4[ii].s_addr ==
1354 ppr->pr_ip4[ij].s_addr)
1356 if (ij == ppr->pr_ip4s)
1359 if (ij == ppr->pr_ip4s) {
1361 goto done_deref_locked;
1366 * Check for conflicting IP addresses. We permit them
1367 * if there is no more than one IP on each jail. If
1368 * there is a duplicate on a jail with more than one
1369 * IP stop checking and return error.
1373 for (; tppr != &prison0; tppr = tppr->pr_parent)
1374 if (tppr->pr_flags & PR_VNET)
1377 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1380 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1382 tpr->pr_uref == 0) {
1386 if (!(tpr->pr_flags & PR_IP4_USER))
1389 if (tpr->pr_ip4 == NULL ||
1390 (ip4s == 1 && tpr->pr_ip4s == 1))
1392 for (ii = 0; ii < ip4s; ii++) {
1393 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1396 "IPv4 addresses clash");
1397 goto done_deref_locked;
1405 if (ppr->pr_flags & PR_IP6) {
1407 * Make sure the new set of IP addresses is a
1408 * subset of the parent's list.
1410 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1411 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1414 if (ij == ppr->pr_ip6s) {
1416 goto done_deref_locked;
1419 for (ii = ij = 1; ii < ip6s; ii++) {
1420 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1423 for (; ij < ppr->pr_ip6s; ij++)
1424 if (IN6_ARE_ADDR_EQUAL(
1425 &ip6[ii], &ppr->pr_ip6[ij]))
1427 if (ij == ppr->pr_ip6s)
1430 if (ij == ppr->pr_ip6s) {
1432 goto done_deref_locked;
1436 /* Check for conflicting IP addresses. */
1439 for (; tppr != &prison0; tppr = tppr->pr_parent)
1440 if (tppr->pr_flags & PR_VNET)
1443 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1446 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1448 tpr->pr_uref == 0) {
1452 if (!(tpr->pr_flags & PR_IP6_USER))
1455 if (tpr->pr_ip6 == NULL ||
1456 (ip6s == 1 && tpr->pr_ip6s == 1))
1458 for (ii = 0; ii < ip6s; ii++) {
1459 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1462 "IPv6 addresses clash");
1463 goto done_deref_locked;
1469 onamelen = namelen = 0;
1471 /* Give a default name of the jid. */
1472 if (name[0] == '\0')
1473 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1474 else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1478 "name cannot be numeric (unless it is the jid)");
1479 goto done_deref_locked;
1482 * Make sure the name isn't too long for the prison or its
1485 onamelen = strlen(pr->pr_name);
1486 namelen = strlen(name);
1487 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1488 error = ENAMETOOLONG;
1489 goto done_deref_locked;
1491 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1492 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1493 sizeof(pr->pr_name)) {
1494 error = ENAMETOOLONG;
1495 goto done_deref_locked;
1499 if (pr_allow & ~ppr->pr_allow) {
1501 goto done_deref_locked;
1504 /* Set the parameters of the prison. */
1507 if (pr_flags & PR_IP4_USER) {
1508 pr->pr_flags |= PR_IP4;
1509 free(pr->pr_ip4, M_PRISON);
1513 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1515 if (tpr->pr_flags & PR_VNET) {
1520 if (prison_restrict_ip4(tpr, NULL)) {
1529 if (pr_flags & PR_IP6_USER) {
1530 pr->pr_flags |= PR_IP6;
1531 free(pr->pr_ip6, M_PRISON);
1535 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1537 if (tpr->pr_flags & PR_VNET) {
1542 if (prison_restrict_ip6(tpr, NULL)) {
1550 pr->pr_securelevel = slevel;
1551 /* Set all child jails to be at least this level. */
1552 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1553 if (tpr->pr_securelevel < slevel)
1554 tpr->pr_securelevel = slevel;
1557 pr->pr_childmax = childmax;
1558 /* Set all child jails to under this limit. */
1559 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1560 if (tpr->pr_childmax > childmax - level)
1561 tpr->pr_childmax = childmax > level
1562 ? childmax - level : 0;
1565 pr->pr_enforce_statfs = enforce;
1566 /* Pass this restriction on to the children. */
1567 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1568 if (tpr->pr_enforce_statfs < enforce)
1569 tpr->pr_enforce_statfs = enforce;
1572 if (ppr == &prison0)
1573 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1575 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1576 ppr->pr_name, name);
1577 /* Change this component of child names. */
1578 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1579 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1580 strlen(tpr->pr_name + onamelen) + 1);
1581 bcopy(pr->pr_name, tpr->pr_name, namelen);
1585 /* Try to keep a real-rooted full pathname. */
1586 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1587 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1588 mypr->pr_path, path);
1590 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1593 if (PR_HOST & ch_flags & ~pr_flags) {
1594 if (pr->pr_flags & PR_HOST) {
1596 * Copy the parent's host info. As with pr_ip4 above,
1597 * the lack of a lock on the parent is not a problem;
1598 * it is always set with allprison_lock at least
1599 * shared, and is held exclusively here.
1601 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1602 sizeof(pr->pr_hostname));
1603 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1604 sizeof(pr->pr_domainname));
1605 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1606 sizeof(pr->pr_hostuuid));
1607 pr->pr_hostid = pr->pr_parent->pr_hostid;
1609 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1610 /* Set this prison, and any descendants without PR_HOST. */
1612 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1614 strlcpy(pr->pr_domainname, domain,
1615 sizeof(pr->pr_domainname));
1617 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1619 pr->pr_hostid = hid;
1620 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1621 if (tpr->pr_flags & PR_HOST)
1625 strlcpy(tpr->pr_hostname,
1627 sizeof(tpr->pr_hostname));
1629 strlcpy(tpr->pr_domainname,
1631 sizeof(tpr->pr_domainname));
1633 strlcpy(tpr->pr_hostuuid,
1635 sizeof(tpr->pr_hostuuid));
1637 tpr->pr_hostid = hid;
1641 if ((tallow = ch_allow & ~pr_allow)) {
1642 /* Clear allow bits in all children. */
1643 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1644 tpr->pr_allow &= ~tallow;
1646 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1648 * Persistent prisons get an extra reference, and prisons losing their
1649 * persist flag lose that reference. Only do this for existing prisons
1650 * for now, so new ones will remain unseen until after the module
1651 * handlers have completed.
1653 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1654 if (pr_flags & PR_PERSIST) {
1662 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1663 mtx_unlock(&pr->pr_mtx);
1667 prison_racct_attach(pr);
1670 /* Locks may have prevented a complete restriction of child IP
1671 * addresses. If so, allocate some more memory and try again.
1676 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1677 mtx_lock(&pr->pr_mtx);
1679 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1681 if (tpr->pr_flags & PR_VNET) {
1686 if (prison_restrict_ip4(tpr, ip4)) {
1693 mtx_unlock(&pr->pr_mtx);
1699 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1700 mtx_lock(&pr->pr_mtx);
1702 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1704 if (tpr->pr_flags & PR_VNET) {
1709 if (prison_restrict_ip6(tpr, ip6)) {
1716 mtx_unlock(&pr->pr_mtx);
1720 /* Let the modules do their work. */
1721 sx_downgrade(&allprison_lock);
1723 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1725 prison_deref(pr, PD_LIST_SLOCKED);
1729 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1731 prison_deref(pr, created
1733 : PD_DEREF | PD_LIST_SLOCKED);
1737 /* Attach this process to the prison if requested. */
1738 if (flags & JAIL_ATTACH) {
1739 mtx_lock(&pr->pr_mtx);
1740 error = do_jail_attach(td, pr);
1742 vfs_opterror(opts, "attach failed");
1744 prison_deref(pr, PD_DEREF);
1750 * Now that it is all there, drop the temporary reference from existing
1751 * prisons. Or add a reference to newly created persistent prisons
1752 * (which was not done earlier so that the prison would not be publicly
1756 prison_deref(pr, (flags & JAIL_ATTACH)
1758 : PD_DEREF | PD_LIST_SLOCKED);
1760 if (pr_flags & PR_PERSIST) {
1761 mtx_lock(&pr->pr_mtx);
1764 mtx_unlock(&pr->pr_mtx);
1766 if (!(flags & JAIL_ATTACH))
1767 sx_sunlock(&allprison_lock);
1769 td->td_retval[0] = pr->pr_id;
1773 prison_deref(pr, created
1774 ? PD_LOCKED | PD_LIST_XLOCKED
1775 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1778 sx_xunlock(&allprison_lock);
1781 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1783 VFS_UNLOCK_GIANT(vfslocked);
1787 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1788 if (errmsg_len > 0) {
1789 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1790 if (errmsg_pos > 0) {
1791 if (optuio->uio_segflg == UIO_SYSSPACE)
1793 optuio->uio_iov[errmsg_pos].iov_base,
1797 optuio->uio_iov[errmsg_pos].iov_base,
1804 free(ip4, M_PRISON);
1807 free(ip6, M_PRISON);
1815 * struct jail_get_args {
1816 * struct iovec *iovp;
1817 * unsigned int iovcnt;
1822 sys_jail_get(struct thread *td, struct jail_get_args *uap)
1827 /* Check that we have an even number of iovecs. */
1828 if (uap->iovcnt & 1)
1831 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1834 error = kern_jail_get(td, auio, uap->flags);
1836 error = copyout(auio->uio_iov, uap->iovp,
1837 uap->iovcnt * sizeof (struct iovec));
1843 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1845 struct prison *pr, *mypr;
1847 struct vfsoptlist *opts;
1848 char *errmsg, *name;
1849 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1851 if (flags & ~JAIL_GET_MASK)
1854 /* Get the parameter list. */
1855 error = vfs_buildopts(optuio, &opts);
1858 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1859 mypr = td->td_ucred->cr_prison;
1862 * Find the prison specified by one of: lastjid, jid, name.
1864 sx_slock(&allprison_lock);
1865 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1867 TAILQ_FOREACH(pr, &allprison, pr_list) {
1868 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1869 mtx_lock(&pr->pr_mtx);
1870 if (pr->pr_ref > 0 &&
1871 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1873 mtx_unlock(&pr->pr_mtx);
1879 vfs_opterror(opts, "no jail after %d", jid);
1880 goto done_unlock_list;
1881 } else if (error != ENOENT)
1882 goto done_unlock_list;
1884 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1887 pr = prison_find_child(mypr, jid);
1889 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1890 mtx_unlock(&pr->pr_mtx);
1892 vfs_opterror(opts, "jail %d is dying",
1894 goto done_unlock_list;
1899 vfs_opterror(opts, "jail %d not found", jid);
1900 goto done_unlock_list;
1902 } else if (error != ENOENT)
1903 goto done_unlock_list;
1905 error = vfs_getopt(opts, "name", (void **)&name, &len);
1907 if (len == 0 || name[len - 1] != '\0') {
1909 goto done_unlock_list;
1911 pr = prison_find_name(mypr, name);
1913 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1914 mtx_unlock(&pr->pr_mtx);
1916 vfs_opterror(opts, "jail \"%s\" is dying",
1918 goto done_unlock_list;
1923 vfs_opterror(opts, "jail \"%s\" not found", name);
1924 goto done_unlock_list;
1925 } else if (error != ENOENT)
1926 goto done_unlock_list;
1928 vfs_opterror(opts, "no jail specified");
1930 goto done_unlock_list;
1933 /* Get the parameters of the prison. */
1936 td->td_retval[0] = pr->pr_id;
1937 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1938 if (error != 0 && error != ENOENT)
1940 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1941 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1942 if (error != 0 && error != ENOENT)
1944 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1945 if (error != 0 && error != ENOENT)
1947 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1948 sizeof(pr->pr_cpuset->cs_id));
1949 if (error != 0 && error != ENOENT)
1951 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1952 if (error != 0 && error != ENOENT)
1955 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1956 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1957 if (error != 0 && error != ENOENT)
1961 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1962 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1963 if (error != 0 && error != ENOENT)
1966 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1967 sizeof(pr->pr_securelevel));
1968 if (error != 0 && error != ENOENT)
1970 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1971 sizeof(pr->pr_childcount));
1972 if (error != 0 && error != ENOENT)
1974 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1975 sizeof(pr->pr_childmax));
1976 if (error != 0 && error != ENOENT)
1978 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1979 if (error != 0 && error != ENOENT)
1981 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1982 if (error != 0 && error != ENOENT)
1984 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1985 if (error != 0 && error != ENOENT)
1987 #ifdef COMPAT_FREEBSD32
1988 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1989 uint32_t hid32 = pr->pr_hostid;
1991 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1994 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1995 sizeof(pr->pr_hostid));
1996 if (error != 0 && error != ENOENT)
1998 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1999 sizeof(pr->pr_enforce_statfs));
2000 if (error != 0 && error != ENOENT)
2002 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
2004 if (pr_flag_names[fi] == NULL)
2006 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
2007 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
2008 if (error != 0 && error != ENOENT)
2011 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2012 if (error != 0 && error != ENOENT)
2015 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2018 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2019 i = pr_flag_jailsys[fi].disable &&
2020 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2021 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2024 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2025 if (error != 0 && error != ENOENT)
2028 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2030 if (pr_allow_names[fi] == NULL)
2032 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2033 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2034 if (error != 0 && error != ENOENT)
2037 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2038 if (error != 0 && error != ENOENT)
2041 i = (pr->pr_uref == 0);
2042 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2043 if (error != 0 && error != ENOENT)
2046 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2047 if (error != 0 && error != ENOENT)
2050 /* Get the module parameters. */
2051 mtx_unlock(&pr->pr_mtx);
2053 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2056 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2058 /* By now, all parameters should have been noted. */
2059 TAILQ_FOREACH(opt, opts, link) {
2060 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2062 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2067 /* Write the fetched parameters back to userspace. */
2069 TAILQ_FOREACH(opt, opts, link) {
2070 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2071 pos = 2 * opt->pos + 1;
2072 optuio->uio_iov[pos].iov_len = opt->len;
2073 if (opt->value != NULL) {
2074 if (optuio->uio_segflg == UIO_SYSSPACE) {
2076 optuio->uio_iov[pos].iov_base,
2079 error = copyout(opt->value,
2080 optuio->uio_iov[pos].iov_base,
2091 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2095 sx_sunlock(&allprison_lock);
2097 if (error && errmsg_pos >= 0) {
2098 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2099 errmsg_pos = 2 * errmsg_pos + 1;
2100 if (errmsg_len > 0) {
2101 if (optuio->uio_segflg == UIO_SYSSPACE)
2103 optuio->uio_iov[errmsg_pos].iov_base,
2107 optuio->uio_iov[errmsg_pos].iov_base,
2117 * struct jail_remove_args {
2122 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2124 struct prison *pr, *cpr, *lpr, *tpr;
2127 error = priv_check(td, PRIV_JAIL_REMOVE);
2131 sx_xlock(&allprison_lock);
2132 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2134 sx_xunlock(&allprison_lock);
2138 /* Remove all descendants of this prison, then remove this prison. */
2140 pr->pr_flags |= PR_REMOVE;
2141 if (!LIST_EMPTY(&pr->pr_children)) {
2142 mtx_unlock(&pr->pr_mtx);
2144 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2145 mtx_lock(&cpr->pr_mtx);
2146 if (cpr->pr_ref > 0) {
2149 cpr->pr_flags |= PR_REMOVE;
2151 /* Already removed - do not do it again. */
2154 mtx_unlock(&cpr->pr_mtx);
2156 mtx_lock(&lpr->pr_mtx);
2157 prison_remove_one(lpr);
2158 sx_xlock(&allprison_lock);
2163 mtx_lock(&lpr->pr_mtx);
2164 prison_remove_one(lpr);
2165 sx_xlock(&allprison_lock);
2167 mtx_lock(&pr->pr_mtx);
2169 prison_remove_one(pr);
2174 prison_remove_one(struct prison *pr)
2179 /* If the prison was persistent, it is not anymore. */
2181 if (pr->pr_flags & PR_PERSIST) {
2184 pr->pr_flags &= ~PR_PERSIST;
2188 * jail_remove added a reference. If that's the only one, remove
2191 KASSERT(pr->pr_ref > 0,
2192 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2193 if (pr->pr_ref == 1) {
2195 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2199 mtx_unlock(&pr->pr_mtx);
2200 sx_xunlock(&allprison_lock);
2202 * Kill all processes unfortunate enough to be attached to this prison.
2204 sx_slock(&allproc_lock);
2205 LIST_FOREACH(p, &allproc, p_list) {
2207 if (p->p_state != PRS_NEW && p->p_ucred &&
2208 p->p_ucred->cr_prison == pr)
2209 kern_psignal(p, SIGKILL);
2212 sx_sunlock(&allproc_lock);
2213 /* Remove the temporary reference added by jail_remove. */
2214 prison_deref(pr, deuref | PD_DEREF);
2219 * struct jail_attach_args {
2224 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2229 error = priv_check(td, PRIV_JAIL_ATTACH);
2233 sx_slock(&allprison_lock);
2234 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2236 sx_sunlock(&allprison_lock);
2241 * Do not allow a process to attach to a prison that is not
2242 * considered to be "alive".
2244 if (pr->pr_uref == 0) {
2245 mtx_unlock(&pr->pr_mtx);
2246 sx_sunlock(&allprison_lock);
2250 return (do_jail_attach(td, pr));
2254 do_jail_attach(struct thread *td, struct prison *pr)
2258 struct ucred *newcred, *oldcred;
2259 int vfslocked, error;
2262 * XXX: Note that there is a slight race here if two threads
2263 * in the same privileged process attempt to attach to two
2264 * different jails at the same time. It is important for
2265 * user processes not to do this, or they might end up with
2266 * a process root from one prison, but attached to the jail
2271 mtx_unlock(&pr->pr_mtx);
2273 /* Let modules do whatever they need to prepare for attaching. */
2274 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2276 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2279 sx_sunlock(&allprison_lock);
2282 * Reparent the newly attached process to this jail.
2284 ppr = td->td_ucred->cr_prison;
2286 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2290 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2291 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2292 if ((error = change_dir(pr->pr_root, td)) != 0)
2295 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2298 VOP_UNLOCK(pr->pr_root, 0);
2299 if ((error = change_root(pr->pr_root, td)))
2300 goto e_unlock_giant;
2301 VFS_UNLOCK_GIANT(vfslocked);
2305 oldcred = p->p_ucred;
2307 crcopy(newcred, oldcred);
2308 newcred->cr_prison = pr;
2309 p->p_ucred = newcred;
2312 racct_proc_ucred_changed(p, oldcred, newcred);
2315 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2318 VOP_UNLOCK(pr->pr_root, 0);
2320 VFS_UNLOCK_GIANT(vfslocked);
2322 /* Tell modules this thread is still in its old jail after all. */
2323 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2324 prison_deref(pr, PD_DEREF | PD_DEUREF);
2330 * Returns a locked prison instance, or NULL on failure.
2333 prison_find(int prid)
2337 sx_assert(&allprison_lock, SX_LOCKED);
2338 TAILQ_FOREACH(pr, &allprison, pr_list) {
2339 if (pr->pr_id == prid) {
2340 mtx_lock(&pr->pr_mtx);
2343 mtx_unlock(&pr->pr_mtx);
2350 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2353 prison_find_child(struct prison *mypr, int prid)
2358 sx_assert(&allprison_lock, SX_LOCKED);
2359 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2360 if (pr->pr_id == prid) {
2361 mtx_lock(&pr->pr_mtx);
2364 mtx_unlock(&pr->pr_mtx);
2371 * Look for the name relative to mypr. Returns a locked prison or NULL.
2374 prison_find_name(struct prison *mypr, const char *name)
2376 struct prison *pr, *deadpr;
2380 sx_assert(&allprison_lock, SX_LOCKED);
2381 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2384 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2385 if (!strcmp(pr->pr_name + mylen, name)) {
2386 mtx_lock(&pr->pr_mtx);
2387 if (pr->pr_ref > 0) {
2388 if (pr->pr_uref > 0)
2392 mtx_unlock(&pr->pr_mtx);
2395 /* There was no valid prison - perhaps there was a dying one. */
2396 if (deadpr != NULL) {
2397 mtx_lock(&deadpr->pr_mtx);
2398 if (deadpr->pr_ref == 0) {
2399 mtx_unlock(&deadpr->pr_mtx);
2407 * See if a prison has the specific flag set.
2410 prison_flag(struct ucred *cred, unsigned flag)
2413 /* This is an atomic read, so no locking is necessary. */
2414 return (cred->cr_prison->pr_flags & flag);
2418 prison_allow(struct ucred *cred, unsigned flag)
2421 /* This is an atomic read, so no locking is necessary. */
2422 return (cred->cr_prison->pr_allow & flag);
2426 * Remove a prison reference. If that was the last reference, remove the
2427 * prison itself - but not in this context in case there are locks held.
2430 prison_free_locked(struct prison *pr)
2433 mtx_assert(&pr->pr_mtx, MA_OWNED);
2435 if (pr->pr_ref == 0) {
2436 mtx_unlock(&pr->pr_mtx);
2437 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2438 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2441 mtx_unlock(&pr->pr_mtx);
2445 prison_free(struct prison *pr)
2448 mtx_lock(&pr->pr_mtx);
2449 prison_free_locked(pr);
2453 prison_complete(void *context, int pending)
2456 prison_deref((struct prison *)context, 0);
2460 * Remove a prison reference (usually). This internal version assumes no
2461 * mutexes are held, except perhaps the prison itself. If there are no more
2462 * references, release and delist the prison. On completion, the prison lock
2463 * and the allprison lock are both unlocked.
2466 prison_deref(struct prison *pr, int flags)
2468 struct prison *ppr, *tpr;
2471 if (!(flags & PD_LOCKED))
2472 mtx_lock(&pr->pr_mtx);
2474 if (flags & PD_DEUREF) {
2476 KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0"));
2478 if (flags & PD_DEREF)
2480 /* If the prison still has references, nothing else to do. */
2481 if (pr->pr_ref > 0) {
2482 mtx_unlock(&pr->pr_mtx);
2483 if (flags & PD_LIST_SLOCKED)
2484 sx_sunlock(&allprison_lock);
2485 else if (flags & PD_LIST_XLOCKED)
2486 sx_xunlock(&allprison_lock);
2490 mtx_unlock(&pr->pr_mtx);
2491 if (flags & PD_LIST_SLOCKED) {
2492 if (!sx_try_upgrade(&allprison_lock)) {
2493 sx_sunlock(&allprison_lock);
2494 sx_xlock(&allprison_lock);
2496 } else if (!(flags & PD_LIST_XLOCKED))
2497 sx_xlock(&allprison_lock);
2499 TAILQ_REMOVE(&allprison, pr, pr_list);
2500 LIST_REMOVE(pr, pr_sibling);
2501 ppr = pr->pr_parent;
2502 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2503 tpr->pr_childcount--;
2504 sx_xunlock(&allprison_lock);
2507 if (pr->pr_vnet != ppr->pr_vnet)
2508 vnet_destroy(pr->pr_vnet);
2510 if (pr->pr_root != NULL) {
2511 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2513 VFS_UNLOCK_GIANT(vfslocked);
2515 mtx_destroy(&pr->pr_mtx);
2517 free(pr->pr_ip4, M_PRISON);
2520 free(pr->pr_ip6, M_PRISON);
2522 if (pr->pr_cpuset != NULL)
2523 cpuset_rel(pr->pr_cpuset);
2526 prison_racct_detach(pr);
2530 /* Removing a prison frees a reference on its parent. */
2532 mtx_lock(&pr->pr_mtx);
2533 flags = PD_DEREF | PD_DEUREF;
2538 prison_hold_locked(struct prison *pr)
2541 mtx_assert(&pr->pr_mtx, MA_OWNED);
2542 KASSERT(pr->pr_ref > 0,
2543 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2548 prison_hold(struct prison *pr)
2551 mtx_lock(&pr->pr_mtx);
2552 prison_hold_locked(pr);
2553 mtx_unlock(&pr->pr_mtx);
2557 prison_proc_hold(struct prison *pr)
2560 mtx_lock(&pr->pr_mtx);
2561 KASSERT(pr->pr_uref > 0,
2562 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2564 mtx_unlock(&pr->pr_mtx);
2568 prison_proc_free(struct prison *pr)
2571 mtx_lock(&pr->pr_mtx);
2572 KASSERT(pr->pr_uref > 0,
2573 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2574 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2580 * Restrict a prison's IP address list with its parent's, possibly replacing
2581 * it. Return true if the replacement buffer was used (or would have been).
2584 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2589 ppr = pr->pr_parent;
2590 if (!(pr->pr_flags & PR_IP4_USER)) {
2591 /* This has no user settings, so just copy the parent's list. */
2592 if (pr->pr_ip4s < ppr->pr_ip4s) {
2594 * There's no room for the parent's list. Use the
2595 * new list buffer, which is assumed to be big enough
2596 * (if it was passed). If there's no buffer, try to
2600 if (newip4 == NULL) {
2601 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2602 M_PRISON, M_NOWAIT);
2606 if (newip4 != NULL) {
2607 bcopy(ppr->pr_ip4, newip4,
2608 ppr->pr_ip4s * sizeof(*newip4));
2609 free(pr->pr_ip4, M_PRISON);
2610 pr->pr_ip4 = newip4;
2611 pr->pr_ip4s = ppr->pr_ip4s;
2615 pr->pr_ip4s = ppr->pr_ip4s;
2616 if (pr->pr_ip4s > 0)
2617 bcopy(ppr->pr_ip4, pr->pr_ip4,
2618 pr->pr_ip4s * sizeof(*newip4));
2619 else if (pr->pr_ip4 != NULL) {
2620 free(pr->pr_ip4, M_PRISON);
2623 } else if (pr->pr_ip4s > 0) {
2624 /* Remove addresses that aren't in the parent. */
2625 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2626 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2628 if (ij < ppr->pr_ip4s)
2631 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2632 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2635 for (ij = 1; ii < pr->pr_ip4s; ) {
2636 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2640 switch (ij >= ppr->pr_ip4s ? -1 :
2641 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2643 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2644 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2655 if (pr->pr_ip4s == 0) {
2656 pr->pr_flags |= PR_IP4_DISABLE;
2657 free(pr->pr_ip4, M_PRISON);
2665 * Pass back primary IPv4 address of this jail.
2667 * If not restricted return success but do not alter the address. Caller has
2668 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2670 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2671 * Address returned in NBO.
2674 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2678 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2679 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2681 pr = cred->cr_prison;
2682 if (!(pr->pr_flags & PR_IP4))
2684 mtx_lock(&pr->pr_mtx);
2685 if (!(pr->pr_flags & PR_IP4)) {
2686 mtx_unlock(&pr->pr_mtx);
2689 if (pr->pr_ip4 == NULL) {
2690 mtx_unlock(&pr->pr_mtx);
2691 return (EAFNOSUPPORT);
2694 ia->s_addr = pr->pr_ip4[0].s_addr;
2695 mtx_unlock(&pr->pr_mtx);
2700 * Return 1 if we should do proper source address selection or are not jailed.
2701 * We will return 0 if we should bypass source address selection in favour
2702 * of the primary jail IPv4 address. Only in this case *ia will be updated and
2704 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2707 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2713 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2714 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2719 pr = cred->cr_prison;
2720 if (pr->pr_flags & PR_IP4_SADDRSEL)
2723 lia.s_addr = INADDR_ANY;
2724 error = prison_get_ip4(cred, &lia);
2727 if (lia.s_addr == INADDR_ANY)
2730 ia->s_addr = lia.s_addr;
2735 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2738 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2745 * No need to lock since the PR_IP4_USER flag can't be altered for
2748 while (pr1 != &prison0 &&
2750 !(pr1->pr_flags & PR_VNET) &&
2752 !(pr1->pr_flags & PR_IP4_USER))
2753 pr1 = pr1->pr_parent;
2754 while (pr2 != &prison0 &&
2756 !(pr2->pr_flags & PR_VNET) &&
2758 !(pr2->pr_flags & PR_IP4_USER))
2759 pr2 = pr2->pr_parent;
2760 return (pr1 == pr2);
2764 * Make sure our (source) address is set to something meaningful to this
2767 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2768 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2769 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2772 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2778 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2779 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2781 pr = cred->cr_prison;
2782 if (!(pr->pr_flags & PR_IP4))
2784 mtx_lock(&pr->pr_mtx);
2785 if (!(pr->pr_flags & PR_IP4)) {
2786 mtx_unlock(&pr->pr_mtx);
2789 if (pr->pr_ip4 == NULL) {
2790 mtx_unlock(&pr->pr_mtx);
2791 return (EAFNOSUPPORT);
2794 ia0.s_addr = ntohl(ia->s_addr);
2795 if (ia0.s_addr == INADDR_LOOPBACK) {
2796 ia->s_addr = pr->pr_ip4[0].s_addr;
2797 mtx_unlock(&pr->pr_mtx);
2801 if (ia0.s_addr == INADDR_ANY) {
2803 * In case there is only 1 IPv4 address, bind directly.
2805 if (pr->pr_ip4s == 1)
2806 ia->s_addr = pr->pr_ip4[0].s_addr;
2807 mtx_unlock(&pr->pr_mtx);
2811 error = _prison_check_ip4(pr, ia);
2812 mtx_unlock(&pr->pr_mtx);
2817 * Rewrite destination address in case we will connect to loopback address.
2819 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2820 * Address passed in in NBO and returned in NBO.
2823 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2827 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2828 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2830 pr = cred->cr_prison;
2831 if (!(pr->pr_flags & PR_IP4))
2833 mtx_lock(&pr->pr_mtx);
2834 if (!(pr->pr_flags & PR_IP4)) {
2835 mtx_unlock(&pr->pr_mtx);
2838 if (pr->pr_ip4 == NULL) {
2839 mtx_unlock(&pr->pr_mtx);
2840 return (EAFNOSUPPORT);
2843 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2844 ia->s_addr = pr->pr_ip4[0].s_addr;
2845 mtx_unlock(&pr->pr_mtx);
2850 * Return success because nothing had to be changed.
2852 mtx_unlock(&pr->pr_mtx);
2857 * Check if given address belongs to the jail referenced by cred/prison.
2859 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2860 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2861 * doesn't allow IPv4. Address passed in in NBO.
2864 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2869 * Check the primary IP.
2871 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2875 * All the other IPs are sorted so we can do a binary search.
2878 z = pr->pr_ip4s - 2;
2881 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2890 return (EADDRNOTAVAIL);
2894 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2899 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2900 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2902 pr = cred->cr_prison;
2903 if (!(pr->pr_flags & PR_IP4))
2905 mtx_lock(&pr->pr_mtx);
2906 if (!(pr->pr_flags & PR_IP4)) {
2907 mtx_unlock(&pr->pr_mtx);
2910 if (pr->pr_ip4 == NULL) {
2911 mtx_unlock(&pr->pr_mtx);
2912 return (EAFNOSUPPORT);
2915 error = _prison_check_ip4(pr, ia);
2916 mtx_unlock(&pr->pr_mtx);
2923 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2928 ppr = pr->pr_parent;
2929 if (!(pr->pr_flags & PR_IP6_USER)) {
2930 /* This has no user settings, so just copy the parent's list. */
2931 if (pr->pr_ip6s < ppr->pr_ip6s) {
2933 * There's no room for the parent's list. Use the
2934 * new list buffer, which is assumed to be big enough
2935 * (if it was passed). If there's no buffer, try to
2939 if (newip6 == NULL) {
2940 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2941 M_PRISON, M_NOWAIT);
2945 if (newip6 != NULL) {
2946 bcopy(ppr->pr_ip6, newip6,
2947 ppr->pr_ip6s * sizeof(*newip6));
2948 free(pr->pr_ip6, M_PRISON);
2949 pr->pr_ip6 = newip6;
2950 pr->pr_ip6s = ppr->pr_ip6s;
2954 pr->pr_ip6s = ppr->pr_ip6s;
2955 if (pr->pr_ip6s > 0)
2956 bcopy(ppr->pr_ip6, pr->pr_ip6,
2957 pr->pr_ip6s * sizeof(*newip6));
2958 else if (pr->pr_ip6 != NULL) {
2959 free(pr->pr_ip6, M_PRISON);
2962 } else if (pr->pr_ip6s > 0) {
2963 /* Remove addresses that aren't in the parent. */
2964 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2965 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2968 if (ij < ppr->pr_ip6s)
2971 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2972 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2975 for (ij = 1; ii < pr->pr_ip6s; ) {
2976 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2981 switch (ij >= ppr->pr_ip4s ? -1 :
2982 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2984 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2985 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2996 if (pr->pr_ip6s == 0) {
2997 pr->pr_flags |= PR_IP6_DISABLE;
2998 free(pr->pr_ip6, M_PRISON);
3006 * Pass back primary IPv6 address for this jail.
3008 * If not restricted return success but do not alter the address. Caller has
3009 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3011 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3014 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3018 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3019 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3021 pr = cred->cr_prison;
3022 if (!(pr->pr_flags & PR_IP6))
3024 mtx_lock(&pr->pr_mtx);
3025 if (!(pr->pr_flags & PR_IP6)) {
3026 mtx_unlock(&pr->pr_mtx);
3029 if (pr->pr_ip6 == NULL) {
3030 mtx_unlock(&pr->pr_mtx);
3031 return (EAFNOSUPPORT);
3034 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3035 mtx_unlock(&pr->pr_mtx);
3040 * Return 1 if we should do proper source address selection or are not jailed.
3041 * We will return 0 if we should bypass source address selection in favour
3042 * of the primary jail IPv6 address. Only in this case *ia will be updated and
3044 * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3047 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3050 struct in6_addr lia6;
3053 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3054 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3059 pr = cred->cr_prison;
3060 if (pr->pr_flags & PR_IP6_SADDRSEL)
3064 error = prison_get_ip6(cred, &lia6);
3067 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3070 bcopy(&lia6, ia6, sizeof(struct in6_addr));
3075 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3078 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
3084 while (pr1 != &prison0 &&
3086 !(pr1->pr_flags & PR_VNET) &&
3088 !(pr1->pr_flags & PR_IP6_USER))
3089 pr1 = pr1->pr_parent;
3090 while (pr2 != &prison0 &&
3092 !(pr2->pr_flags & PR_VNET) &&
3094 !(pr2->pr_flags & PR_IP6_USER))
3095 pr2 = pr2->pr_parent;
3096 return (pr1 == pr2);
3100 * Make sure our (source) address is set to something meaningful to this jail.
3102 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3103 * when needed while binding.
3105 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3106 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3107 * doesn't allow IPv6.
3110 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3115 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3116 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3118 pr = cred->cr_prison;
3119 if (!(pr->pr_flags & PR_IP6))
3121 mtx_lock(&pr->pr_mtx);
3122 if (!(pr->pr_flags & PR_IP6)) {
3123 mtx_unlock(&pr->pr_mtx);
3126 if (pr->pr_ip6 == NULL) {
3127 mtx_unlock(&pr->pr_mtx);
3128 return (EAFNOSUPPORT);
3131 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3132 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3133 mtx_unlock(&pr->pr_mtx);
3137 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3139 * In case there is only 1 IPv6 address, and v6only is true,
3140 * then bind directly.
3142 if (v6only != 0 && pr->pr_ip6s == 1)
3143 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3144 mtx_unlock(&pr->pr_mtx);
3148 error = _prison_check_ip6(pr, ia6);
3149 mtx_unlock(&pr->pr_mtx);
3154 * Rewrite destination address in case we will connect to loopback address.
3156 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3159 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3163 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3164 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3166 pr = cred->cr_prison;
3167 if (!(pr->pr_flags & PR_IP6))
3169 mtx_lock(&pr->pr_mtx);
3170 if (!(pr->pr_flags & PR_IP6)) {
3171 mtx_unlock(&pr->pr_mtx);
3174 if (pr->pr_ip6 == NULL) {
3175 mtx_unlock(&pr->pr_mtx);
3176 return (EAFNOSUPPORT);
3179 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3180 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3181 mtx_unlock(&pr->pr_mtx);
3186 * Return success because nothing had to be changed.
3188 mtx_unlock(&pr->pr_mtx);
3193 * Check if given address belongs to the jail referenced by cred/prison.
3195 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3196 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3197 * doesn't allow IPv6.
3200 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3205 * Check the primary IP.
3207 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3211 * All the other IPs are sorted so we can do a binary search.
3214 z = pr->pr_ip6s - 2;
3217 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3226 return (EADDRNOTAVAIL);
3230 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3235 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3236 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3238 pr = cred->cr_prison;
3239 if (!(pr->pr_flags & PR_IP6))
3241 mtx_lock(&pr->pr_mtx);
3242 if (!(pr->pr_flags & PR_IP6)) {
3243 mtx_unlock(&pr->pr_mtx);
3246 if (pr->pr_ip6 == NULL) {
3247 mtx_unlock(&pr->pr_mtx);
3248 return (EAFNOSUPPORT);
3251 error = _prison_check_ip6(pr, ia6);
3252 mtx_unlock(&pr->pr_mtx);
3258 * Check if a jail supports the given address family.
3260 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3264 prison_check_af(struct ucred *cred, int af)
3269 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3271 pr = cred->cr_prison;
3273 /* Prisons with their own network stack are not limited. */
3274 if (prison_owns_vnet(cred))
3283 if (pr->pr_flags & PR_IP4)
3285 mtx_lock(&pr->pr_mtx);
3286 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3287 error = EAFNOSUPPORT;
3288 mtx_unlock(&pr->pr_mtx);
3294 if (pr->pr_flags & PR_IP6)
3296 mtx_lock(&pr->pr_mtx);
3297 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3298 error = EAFNOSUPPORT;
3299 mtx_unlock(&pr->pr_mtx);
3307 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3308 error = EAFNOSUPPORT;
3314 * Check if given address belongs to the jail referenced by cred (wrapper to
3315 * prison_check_ip[46]).
3317 * Returns 0 if jail doesn't restrict the address family or if address belongs
3318 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3319 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3322 prison_if(struct ucred *cred, struct sockaddr *sa)
3325 struct sockaddr_in *sai;
3328 struct sockaddr_in6 *sai6;
3332 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3333 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3336 if (prison_owns_vnet(cred))
3341 switch (sa->sa_family)
3345 sai = (struct sockaddr_in *)sa;
3346 error = prison_check_ip4(cred, &sai->sin_addr);
3351 sai6 = (struct sockaddr_in6 *)sa;
3352 error = prison_check_ip6(cred, &sai6->sin6_addr);
3356 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3357 error = EAFNOSUPPORT;
3363 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3366 prison_check(struct ucred *cred1, struct ucred *cred2)
3369 return ((cred1->cr_prison == cred2->cr_prison ||
3370 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3374 * Return 1 if p2 is a child of p1, otherwise 0.
3377 prison_ischild(struct prison *pr1, struct prison *pr2)
3380 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3387 * Return 1 if the passed credential is in a jail, otherwise 0.
3390 jailed(struct ucred *cred)
3393 return (cred->cr_prison != &prison0);
3397 * Return 1 if the passed credential is in a jail and that jail does not
3398 * have its own virtual network stack, otherwise 0.
3401 jailed_without_vnet(struct ucred *cred)
3407 if (prison_owns_vnet(cred))
3415 * Return the correct hostname (domainname, et al) for the passed credential.
3418 getcredhostname(struct ucred *cred, char *buf, size_t size)
3423 * A NULL credential can be used to shortcut to the physical
3424 * system's hostname.
3426 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3427 mtx_lock(&pr->pr_mtx);
3428 strlcpy(buf, pr->pr_hostname, size);
3429 mtx_unlock(&pr->pr_mtx);
3433 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3436 mtx_lock(&cred->cr_prison->pr_mtx);
3437 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3438 mtx_unlock(&cred->cr_prison->pr_mtx);
3442 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3445 mtx_lock(&cred->cr_prison->pr_mtx);
3446 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3447 mtx_unlock(&cred->cr_prison->pr_mtx);
3451 getcredhostid(struct ucred *cred, unsigned long *hostid)
3454 mtx_lock(&cred->cr_prison->pr_mtx);
3455 *hostid = cred->cr_prison->pr_hostid;
3456 mtx_unlock(&cred->cr_prison->pr_mtx);
3461 * Determine whether the prison represented by cred owns
3462 * its vnet rather than having it inherited.
3464 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3467 prison_owns_vnet(struct ucred *cred)
3471 * vnets cannot be added/removed after jail creation,
3472 * so no need to lock here.
3474 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3479 * Determine whether the subject represented by cred can "see"
3480 * status of a mount point.
3481 * Returns: 0 for permitted, ENOENT otherwise.
3482 * XXX: This function should be called cr_canseemount() and should be
3483 * placed in kern_prot.c.
3486 prison_canseemount(struct ucred *cred, struct mount *mp)
3492 pr = cred->cr_prison;
3493 if (pr->pr_enforce_statfs == 0)
3495 if (pr->pr_root->v_mount == mp)
3497 if (pr->pr_enforce_statfs == 2)
3500 * If jail's chroot directory is set to "/" we should be able to see
3501 * all mount-points from inside a jail.
3502 * This is ugly check, but this is the only situation when jail's
3503 * directory ends with '/'.
3505 if (strcmp(pr->pr_path, "/") == 0)
3507 len = strlen(pr->pr_path);
3509 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3512 * Be sure that we don't have situation where jail's root directory
3513 * is "/some/path" and mount point is "/some/pathpath".
3515 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3521 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3523 char jpath[MAXPATHLEN];
3527 pr = cred->cr_prison;
3528 if (pr->pr_enforce_statfs == 0)
3530 if (prison_canseemount(cred, mp) != 0) {
3531 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3532 strlcpy(sp->f_mntonname, "[restricted]",
3533 sizeof(sp->f_mntonname));
3536 if (pr->pr_root->v_mount == mp) {
3538 * Clear current buffer data, so we are sure nothing from
3539 * the valid path left there.
3541 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3542 *sp->f_mntonname = '/';
3546 * If jail's chroot directory is set to "/" we should be able to see
3547 * all mount-points from inside a jail.
3549 if (strcmp(pr->pr_path, "/") == 0)
3551 len = strlen(pr->pr_path);
3552 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3554 * Clear current buffer data, so we are sure nothing from
3555 * the valid path left there.
3557 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3558 if (*jpath == '\0') {
3559 /* Should never happen. */
3560 *sp->f_mntonname = '/';
3562 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3567 * Check with permission for a specific privilege is granted within jail. We
3568 * have a specific list of accepted privileges; the rest are denied.
3571 prison_priv_check(struct ucred *cred, int priv)
3579 * Privileges specific to prisons with a virtual network stack.
3580 * There might be a duplicate entry here in case the privilege
3581 * is only granted conditionally in the legacy jail case.
3586 * NFS-specific privileges.
3588 case PRIV_NFS_DAEMON:
3589 case PRIV_NFS_LOCKD:
3592 * Network stack privileges.
3594 case PRIV_NET_BRIDGE:
3597 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3598 case PRIV_NET_ROUTE:
3600 case PRIV_NET_SETIFMTU:
3601 case PRIV_NET_SETIFFLAGS:
3602 case PRIV_NET_SETIFCAP:
3603 case PRIV_NET_SETIFDESCR:
3604 case PRIV_NET_SETIFNAME :
3605 case PRIV_NET_SETIFMETRIC:
3606 case PRIV_NET_SETIFPHYS:
3607 case PRIV_NET_SETIFMAC:
3608 case PRIV_NET_ADDMULTI:
3609 case PRIV_NET_DELMULTI:
3610 case PRIV_NET_HWIOCTL:
3611 case PRIV_NET_SETLLADDR:
3612 case PRIV_NET_ADDIFGROUP:
3613 case PRIV_NET_DELIFGROUP:
3614 case PRIV_NET_IFCREATE:
3615 case PRIV_NET_IFDESTROY:
3616 case PRIV_NET_ADDIFADDR:
3617 case PRIV_NET_DELIFADDR:
3620 case PRIV_NET_SETIFVNET:
3621 case PRIV_NET_SETIFFIB:
3624 * 802.11-related privileges.
3626 case PRIV_NET80211_GETKEY:
3628 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3633 * AppleTalk privileges.
3635 case PRIV_NETATALK_RESERVEDPORT:
3640 case PRIV_NETATM_CFG:
3641 case PRIV_NETATM_ADD:
3642 case PRIV_NETATM_DEL:
3643 case PRIV_NETATM_SET:
3646 * Bluetooth privileges.
3648 case PRIV_NETBLUETOOTH_RAW:
3652 * Netgraph and netgraph module privileges.
3654 case PRIV_NETGRAPH_CONTROL:
3656 case PRIV_NETGRAPH_TTY:
3660 * IPv4 and IPv6 privileges.
3662 case PRIV_NETINET_IPFW:
3663 case PRIV_NETINET_DIVERT:
3664 case PRIV_NETINET_PF:
3665 case PRIV_NETINET_DUMMYNET:
3666 case PRIV_NETINET_CARP:
3667 case PRIV_NETINET_MROUTE:
3668 case PRIV_NETINET_RAW:
3669 case PRIV_NETINET_ADDRCTRL6:
3670 case PRIV_NETINET_ND6:
3671 case PRIV_NETINET_SCOPE6:
3672 case PRIV_NETINET_ALIFETIME6:
3673 case PRIV_NETINET_IPSEC:
3674 case PRIV_NETINET_BINDANY:
3678 * IPX/SPX privileges.
3680 case PRIV_NETIPX_RESERVEDPORT:
3681 case PRIV_NETIPX_RAW:
3695 * No default: or deny here.
3696 * In case of no permit fall through to next switch().
3698 if (cred->cr_prison->pr_flags & PR_VNET)
3706 * Allow ktrace privileges for root in jail.
3712 * Allow jailed processes to configure audit identity and
3713 * submit audit records (login, etc). In the future we may
3714 * want to further refine the relationship between audit and
3717 case PRIV_AUDIT_GETAUDIT:
3718 case PRIV_AUDIT_SETAUDIT:
3719 case PRIV_AUDIT_SUBMIT:
3723 * Allow jailed processes to manipulate process UNIX
3724 * credentials in any way they see fit.
3726 case PRIV_CRED_SETUID:
3727 case PRIV_CRED_SETEUID:
3728 case PRIV_CRED_SETGID:
3729 case PRIV_CRED_SETEGID:
3730 case PRIV_CRED_SETGROUPS:
3731 case PRIV_CRED_SETREUID:
3732 case PRIV_CRED_SETREGID:
3733 case PRIV_CRED_SETRESUID:
3734 case PRIV_CRED_SETRESGID:
3737 * Jail implements visibility constraints already, so allow
3738 * jailed root to override uid/gid-based constraints.
3740 case PRIV_SEEOTHERGIDS:
3741 case PRIV_SEEOTHERUIDS:
3744 * Jail implements inter-process debugging limits already, so
3745 * allow jailed root various debugging privileges.
3747 case PRIV_DEBUG_DIFFCRED:
3748 case PRIV_DEBUG_SUGID:
3749 case PRIV_DEBUG_UNPRIV:
3752 * Allow jail to set various resource limits and login
3753 * properties, and for now, exceed process resource limits.
3755 case PRIV_PROC_LIMIT:
3756 case PRIV_PROC_SETLOGIN:
3757 case PRIV_PROC_SETRLIMIT:
3760 * System V and POSIX IPC privileges are granted in jail.
3763 case PRIV_IPC_WRITE:
3764 case PRIV_IPC_ADMIN:
3765 case PRIV_IPC_MSGSIZE:
3769 * Jail operations within a jail work on child jails.
3771 case PRIV_JAIL_ATTACH:
3773 case PRIV_JAIL_REMOVE:
3776 * Jail implements its own inter-process limits, so allow
3777 * root processes in jail to change scheduling on other
3778 * processes in the same jail. Likewise for signalling.
3780 case PRIV_SCHED_DIFFCRED:
3781 case PRIV_SCHED_CPUSET:
3782 case PRIV_SIGNAL_DIFFCRED:
3783 case PRIV_SIGNAL_SUGID:
3786 * Allow jailed processes to write to sysctls marked as jail
3789 case PRIV_SYSCTL_WRITEJAIL:
3792 * Allow root in jail to manage a variety of quota
3793 * properties. These should likely be conditional on a
3794 * configuration option.
3796 case PRIV_VFS_GETQUOTA:
3797 case PRIV_VFS_SETQUOTA:
3800 * Since Jail relies on chroot() to implement file system
3801 * protections, grant many VFS privileges to root in jail.
3802 * Be careful to exclude mount-related and NFS-related
3806 case PRIV_VFS_WRITE:
3807 case PRIV_VFS_ADMIN:
3809 case PRIV_VFS_LOOKUP:
3810 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3811 case PRIV_VFS_CHFLAGS_DEV:
3812 case PRIV_VFS_CHOWN:
3813 case PRIV_VFS_CHROOT:
3814 case PRIV_VFS_RETAINSUGID:
3815 case PRIV_VFS_FCHROOT:
3817 case PRIV_VFS_SETGID:
3819 case PRIV_VFS_STICKYFILE:
3823 * Depending on the global setting, allow privilege of
3824 * setting system flags.
3826 case PRIV_VFS_SYSFLAGS:
3827 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3833 * Depending on the global setting, allow privilege of
3834 * mounting/unmounting file systems.
3836 case PRIV_VFS_MOUNT:
3837 case PRIV_VFS_UNMOUNT:
3838 case PRIV_VFS_MOUNT_NONUSER:
3839 case PRIV_VFS_MOUNT_OWNER:
3840 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT &&
3841 cred->cr_prison->pr_enforce_statfs < 2)
3847 * Allow jailed root to bind reserved ports and reuse in-use
3850 case PRIV_NETINET_RESERVEDPORT:
3851 case PRIV_NETINET_REUSEPORT:
3855 * Allow jailed root to set certian IPv4/6 (option) headers.
3857 case PRIV_NETINET_SETHDROPTS:
3861 * Conditionally allow creating raw sockets in jail.
3863 case PRIV_NETINET_RAW:
3864 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3870 * Since jail implements its own visibility limits on netstat
3871 * sysctls, allow getcred. This allows identd to work in
3874 case PRIV_NETINET_GETCRED:
3878 * Allow jailed root to set loginclass.
3880 case PRIV_PROC_SETLOGINCLASS:
3885 * In all remaining cases, deny the privilege request. This
3886 * includes almost all network privileges, many system
3887 * configuration privileges.
3894 * Return the part of pr2's name that is relative to pr1, or the whole name
3895 * if it does not directly follow.
3899 prison_name(struct prison *pr1, struct prison *pr2)
3903 /* Jails see themselves as "0" (if they see themselves at all). */
3906 name = pr2->pr_name;
3907 if (prison_ischild(pr1, pr2)) {
3909 * pr1 isn't locked (and allprison_lock may not be either)
3910 * so its length can't be counted on. But the number of dots
3911 * can be counted on - and counted.
3913 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3914 name = strchr(name, '.') + 1;
3920 * Return the part of pr2's path that is relative to pr1, or the whole path
3921 * if it does not directly follow.
3924 prison_path(struct prison *pr1, struct prison *pr2)
3926 char *path1, *path2;
3929 path1 = pr1->pr_path;
3930 path2 = pr2->pr_path;
3931 if (!strcmp(path1, "/"))
3933 len1 = strlen(path1);
3934 if (strncmp(path1, path2, len1))
3936 if (path2[len1] == '\0')
3938 if (path2[len1] == '/')
3939 return (path2 + len1);
3945 * Jail-related sysctls.
3947 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3951 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3954 struct prison *pr, *cpr;
3956 struct in_addr *ip4 = NULL;
3960 struct in6_addr *ip6 = NULL;
3965 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3966 pr = req->td->td_ucred->cr_prison;
3968 sx_slock(&allprison_lock);
3969 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3970 #if defined(INET) || defined(INET6)
3973 mtx_lock(&cpr->pr_mtx);
3975 if (cpr->pr_ip4s > 0) {
3976 if (ip4s < cpr->pr_ip4s) {
3977 ip4s = cpr->pr_ip4s;
3978 mtx_unlock(&cpr->pr_mtx);
3979 ip4 = realloc(ip4, ip4s *
3980 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3983 bcopy(cpr->pr_ip4, ip4,
3984 cpr->pr_ip4s * sizeof(struct in_addr));
3988 if (cpr->pr_ip6s > 0) {
3989 if (ip6s < cpr->pr_ip6s) {
3990 ip6s = cpr->pr_ip6s;
3991 mtx_unlock(&cpr->pr_mtx);
3992 ip6 = realloc(ip6, ip6s *
3993 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3996 bcopy(cpr->pr_ip6, ip6,
3997 cpr->pr_ip6s * sizeof(struct in6_addr));
4000 if (cpr->pr_ref == 0) {
4001 mtx_unlock(&cpr->pr_mtx);
4004 bzero(xp, sizeof(*xp));
4005 xp->pr_version = XPRISON_VERSION;
4006 xp->pr_id = cpr->pr_id;
4007 xp->pr_state = cpr->pr_uref > 0
4008 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4009 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4010 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4011 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4013 xp->pr_ip4s = cpr->pr_ip4s;
4016 xp->pr_ip6s = cpr->pr_ip6s;
4018 mtx_unlock(&cpr->pr_mtx);
4019 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4023 if (xp->pr_ip4s > 0) {
4024 error = SYSCTL_OUT(req, ip4,
4025 xp->pr_ip4s * sizeof(struct in_addr));
4031 if (xp->pr_ip6s > 0) {
4032 error = SYSCTL_OUT(req, ip6,
4033 xp->pr_ip6s * sizeof(struct in6_addr));
4039 sx_sunlock(&allprison_lock);
4050 SYSCTL_OID(_security_jail, OID_AUTO, list,
4051 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4052 sysctl_jail_list, "S", "List of active jails");
4055 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4059 injail = jailed(req->td->td_ucred);
4060 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4065 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4066 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4067 sysctl_jail_jailed, "I", "Process in jail?");
4069 #if defined(INET) || defined(INET6)
4070 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4071 &jail_max_af_ips, 0,
4072 "Number of IP addresses a jail may have at most per address family");
4076 * Default parameters for jail(2) compatability. For historical reasons,
4077 * the sysctl names have varying similarity to the parameter names. Prisons
4078 * just see their own parameters, and can't change them.
4081 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4084 int allow, error, i;
4086 pr = req->td->td_ucred->cr_prison;
4087 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4089 /* Get the current flag value, and convert it to a boolean. */
4090 i = (allow & arg2) ? 1 : 0;
4093 error = sysctl_handle_int(oidp, &i, 0, req);
4094 if (error || !req->newptr)
4100 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4103 mtx_lock(&prison0.pr_mtx);
4104 jail_default_allow = (jail_default_allow & ~arg2) | i;
4105 mtx_unlock(&prison0.pr_mtx);
4109 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4110 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4111 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4112 "Processes in jail can set their hostnames");
4113 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4114 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4115 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4116 "Processes in jail are limited to creating UNIX/IP/route sockets only");
4117 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4118 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4119 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4120 "Processes in jail can use System V IPC primitives");
4121 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4122 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4123 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4124 "Prison root can create raw sockets");
4125 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4126 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4127 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4128 "Processes in jail can alter system file flags");
4129 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4130 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4131 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4132 "Processes in jail can mount/unmount jail-friendly file systems");
4135 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4140 pr = req->td->td_ucred->cr_prison;
4141 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4142 error = sysctl_handle_int(oidp, &level, 0, req);
4143 if (error || !req->newptr)
4145 *(int *)arg1 = level;
4149 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4150 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4151 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4152 sysctl_jail_default_level, "I",
4153 "Processes in jail cannot see all mounted file systems");
4156 * Nodes to describe jail parameters. Maximum length of string parameters
4157 * is returned in the string itself, and the other parameters exist merely
4158 * to make themselves and their types known.
4160 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4164 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4171 switch (oidp->oid_kind & CTLTYPE)
4177 if (!(req->flags & SCTL_MASK32))
4179 return (SYSCTL_OUT(req, &l, sizeof(l)));
4183 return (SYSCTL_OUT(req, &i, sizeof(i)));
4184 case CTLTYPE_STRING:
4185 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4187 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4188 case CTLTYPE_STRUCT:
4190 return (SYSCTL_OUT(req, &s, sizeof(s)));
4195 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4196 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4197 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4198 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4199 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4200 "I", "Jail secure level");
4201 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4202 "I", "Jail cannot see all mounted file systems");
4203 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4204 "B", "Jail persistence");
4206 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4207 "E,jailsys", "Virtual network stack");
4209 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4210 "B", "Jail is in the process of shutting down");
4212 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4213 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4214 "I", "Current number of child jails");
4215 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4216 "I", "Maximum number of child jails");
4218 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4219 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4221 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4222 "Jail NIS domainname");
4223 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4225 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4226 "LU", "Jail host ID");
4228 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4229 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4232 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4233 "Jail IPv4 address virtualization");
4234 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4235 "S,in_addr,a", "Jail IPv4 addresses");
4236 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4237 "B", "Do (not) use IPv4 source address selection rather than the "
4238 "primary jail IPv4 address.");
4241 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4242 "Jail IPv6 address virtualization");
4243 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4244 "S,in6_addr,a", "Jail IPv6 addresses");
4245 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4246 "B", "Do (not) use IPv6 source address selection rather than the "
4247 "primary jail IPv6 address.");
4250 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4251 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4252 "B", "Jail may set hostname");
4253 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4254 "B", "Jail may use SYSV IPC");
4255 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4256 "B", "Jail may create raw sockets");
4257 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4258 "B", "Jail may alter system file flags");
4259 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4260 "B", "Jail may mount/unmount jail-friendly file systems");
4261 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4262 "B", "Jail may set file quotas");
4263 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4264 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4267 prison_racct_foreach(void (*callback)(struct racct *racct,
4268 void *arg2, void *arg3), void *arg2, void *arg3)
4270 struct prison_racct *prr;
4272 sx_slock(&allprison_lock);
4273 LIST_FOREACH(prr, &allprison_racct, prr_next)
4274 (callback)(prr->prr_racct, arg2, arg3);
4275 sx_sunlock(&allprison_lock);
4278 static struct prison_racct *
4279 prison_racct_find_locked(const char *name)
4281 struct prison_racct *prr;
4283 sx_assert(&allprison_lock, SA_XLOCKED);
4285 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4288 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4289 if (strcmp(name, prr->prr_name) != 0)
4292 /* Found prison_racct with a matching name? */
4293 prison_racct_hold(prr);
4297 /* Add new prison_racct. */
4298 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4299 racct_create(&prr->prr_racct);
4301 strcpy(prr->prr_name, name);
4302 refcount_init(&prr->prr_refcount, 1);
4303 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4308 struct prison_racct *
4309 prison_racct_find(const char *name)
4311 struct prison_racct *prr;
4313 sx_xlock(&allprison_lock);
4314 prr = prison_racct_find_locked(name);
4315 sx_xunlock(&allprison_lock);
4320 prison_racct_hold(struct prison_racct *prr)
4323 refcount_acquire(&prr->prr_refcount);
4327 prison_racct_free(struct prison_racct *prr)
4331 old = prr->prr_refcount;
4332 if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4335 sx_xlock(&allprison_lock);
4336 if (refcount_release(&prr->prr_refcount)) {
4337 racct_destroy(&prr->prr_racct);
4338 LIST_REMOVE(prr, prr_next);
4339 sx_xunlock(&allprison_lock);
4340 free(prr, M_PRISON_RACCT);
4344 sx_xunlock(&allprison_lock);
4349 prison_racct_attach(struct prison *pr)
4351 struct prison_racct *prr;
4353 prr = prison_racct_find_locked(pr->pr_name);
4354 KASSERT(prr != NULL, ("cannot find prison_racct"));
4356 pr->pr_prison_racct = prr;
4360 prison_racct_detach(struct prison *pr)
4362 prison_racct_free(pr->pr_prison_racct);
4363 pr->pr_prison_racct = NULL;
4370 db_show_prison(struct prison *pr)
4373 #if defined(INET) || defined(INET6)
4378 char ip6buf[INET6_ADDRSTRLEN];
4381 db_printf("prison %p:\n", pr);
4382 db_printf(" jid = %d\n", pr->pr_id);
4383 db_printf(" name = %s\n", pr->pr_name);
4384 db_printf(" parent = %p\n", pr->pr_parent);
4385 db_printf(" ref = %d\n", pr->pr_ref);
4386 db_printf(" uref = %d\n", pr->pr_uref);
4387 db_printf(" path = %s\n", pr->pr_path);
4388 db_printf(" cpuset = %d\n", pr->pr_cpuset
4389 ? pr->pr_cpuset->cs_id : -1);
4391 db_printf(" vnet = %p\n", pr->pr_vnet);
4393 db_printf(" root = %p\n", pr->pr_root);
4394 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4395 db_printf(" children.max = %d\n", pr->pr_childmax);
4396 db_printf(" children.cur = %d\n", pr->pr_childcount);
4397 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4398 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4399 db_printf(" flags = 0x%x", pr->pr_flags);
4400 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4402 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4403 db_printf(" %s", pr_flag_names[fi]);
4404 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4406 jsf = pr->pr_flags &
4407 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4408 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4409 pr_flag_jailsys[fi].disable &&
4410 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4411 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4414 db_printf(" allow = 0x%x", pr->pr_allow);
4415 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4417 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4418 db_printf(" %s", pr_allow_names[fi]);
4420 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4421 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4422 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4423 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4424 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4426 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4427 for (ii = 0; ii < pr->pr_ip4s; ii++)
4428 db_printf(" %s %s\n",
4429 ii == 0 ? "ip4.addr =" : " ",
4430 inet_ntoa(pr->pr_ip4[ii]));
4433 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4434 for (ii = 0; ii < pr->pr_ip6s; ii++)
4435 db_printf(" %s %s\n",
4436 ii == 0 ? "ip6.addr =" : " ",
4437 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4441 DB_SHOW_COMMAND(prison, db_show_prison_command)
4447 * Show all prisons in the list, and prison0 which is not
4450 db_show_prison(&prison0);
4451 if (!db_pager_quit) {
4452 TAILQ_FOREACH(pr, &allprison, pr_list) {
4464 /* Look for a prison with the ID and with references. */
4465 TAILQ_FOREACH(pr, &allprison, pr_list)
4466 if (pr->pr_id == addr && pr->pr_ref > 0)
4469 /* Look again, without requiring a reference. */
4470 TAILQ_FOREACH(pr, &allprison, pr_list)
4471 if (pr->pr_id == addr)
4474 /* Assume address points to a valid prison. */
4475 pr = (struct prison *)addr;