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 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 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 unsigned pr_flags, ch_flags;
544 unsigned pr_allow, ch_allow, tallow;
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 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 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 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 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);
2473 /* Decrement the user references in a separate loop. */
2474 if (flags & PD_DEUREF) {
2475 for (tpr = pr;; tpr = tpr->pr_parent) {
2477 mtx_lock(&tpr->pr_mtx);
2478 if (--tpr->pr_uref > 0)
2480 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2481 mtx_unlock(&tpr->pr_mtx);
2483 /* Done if there were only user references to remove. */
2484 if (!(flags & PD_DEREF)) {
2485 mtx_unlock(&tpr->pr_mtx);
2486 if (flags & PD_LIST_SLOCKED)
2487 sx_sunlock(&allprison_lock);
2488 else if (flags & PD_LIST_XLOCKED)
2489 sx_xunlock(&allprison_lock);
2493 mtx_unlock(&tpr->pr_mtx);
2494 mtx_lock(&pr->pr_mtx);
2499 if (flags & PD_DEREF)
2501 /* If the prison still has references, nothing else to do. */
2502 if (pr->pr_ref > 0) {
2503 mtx_unlock(&pr->pr_mtx);
2504 if (flags & PD_LIST_SLOCKED)
2505 sx_sunlock(&allprison_lock);
2506 else if (flags & PD_LIST_XLOCKED)
2507 sx_xunlock(&allprison_lock);
2511 mtx_unlock(&pr->pr_mtx);
2512 if (flags & PD_LIST_SLOCKED) {
2513 if (!sx_try_upgrade(&allprison_lock)) {
2514 sx_sunlock(&allprison_lock);
2515 sx_xlock(&allprison_lock);
2517 } else if (!(flags & PD_LIST_XLOCKED))
2518 sx_xlock(&allprison_lock);
2520 TAILQ_REMOVE(&allprison, pr, pr_list);
2521 LIST_REMOVE(pr, pr_sibling);
2522 ppr = pr->pr_parent;
2523 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2524 tpr->pr_childcount--;
2525 sx_xunlock(&allprison_lock);
2528 if (pr->pr_vnet != ppr->pr_vnet)
2529 vnet_destroy(pr->pr_vnet);
2531 if (pr->pr_root != NULL) {
2532 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2534 VFS_UNLOCK_GIANT(vfslocked);
2536 mtx_destroy(&pr->pr_mtx);
2538 free(pr->pr_ip4, M_PRISON);
2541 free(pr->pr_ip6, M_PRISON);
2543 if (pr->pr_cpuset != NULL)
2544 cpuset_rel(pr->pr_cpuset);
2547 prison_racct_detach(pr);
2551 /* Removing a prison frees a reference on its parent. */
2553 mtx_lock(&pr->pr_mtx);
2559 prison_hold_locked(struct prison *pr)
2562 mtx_assert(&pr->pr_mtx, MA_OWNED);
2563 KASSERT(pr->pr_ref > 0,
2564 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2569 prison_hold(struct prison *pr)
2572 mtx_lock(&pr->pr_mtx);
2573 prison_hold_locked(pr);
2574 mtx_unlock(&pr->pr_mtx);
2578 prison_proc_hold(struct prison *pr)
2581 mtx_lock(&pr->pr_mtx);
2582 KASSERT(pr->pr_uref > 0,
2583 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2585 mtx_unlock(&pr->pr_mtx);
2589 prison_proc_free(struct prison *pr)
2592 mtx_lock(&pr->pr_mtx);
2593 KASSERT(pr->pr_uref > 0,
2594 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2595 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2601 * Restrict a prison's IP address list with its parent's, possibly replacing
2602 * it. Return true if the replacement buffer was used (or would have been).
2605 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2610 ppr = pr->pr_parent;
2611 if (!(pr->pr_flags & PR_IP4_USER)) {
2612 /* This has no user settings, so just copy the parent's list. */
2613 if (pr->pr_ip4s < ppr->pr_ip4s) {
2615 * There's no room for the parent's list. Use the
2616 * new list buffer, which is assumed to be big enough
2617 * (if it was passed). If there's no buffer, try to
2621 if (newip4 == NULL) {
2622 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2623 M_PRISON, M_NOWAIT);
2627 if (newip4 != NULL) {
2628 bcopy(ppr->pr_ip4, newip4,
2629 ppr->pr_ip4s * sizeof(*newip4));
2630 free(pr->pr_ip4, M_PRISON);
2631 pr->pr_ip4 = newip4;
2632 pr->pr_ip4s = ppr->pr_ip4s;
2636 pr->pr_ip4s = ppr->pr_ip4s;
2637 if (pr->pr_ip4s > 0)
2638 bcopy(ppr->pr_ip4, pr->pr_ip4,
2639 pr->pr_ip4s * sizeof(*newip4));
2640 else if (pr->pr_ip4 != NULL) {
2641 free(pr->pr_ip4, M_PRISON);
2644 } else if (pr->pr_ip4s > 0) {
2645 /* Remove addresses that aren't in the parent. */
2646 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2647 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2649 if (ij < ppr->pr_ip4s)
2652 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2653 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2656 for (ij = 1; ii < pr->pr_ip4s; ) {
2657 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2661 switch (ij >= ppr->pr_ip4s ? -1 :
2662 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2664 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2665 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2676 if (pr->pr_ip4s == 0) {
2677 pr->pr_flags |= PR_IP4_DISABLE;
2678 free(pr->pr_ip4, M_PRISON);
2686 * Pass back primary IPv4 address of this jail.
2688 * If not restricted return success but do not alter the address. Caller has
2689 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2691 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2692 * Address returned in NBO.
2695 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2699 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2700 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2702 pr = cred->cr_prison;
2703 if (!(pr->pr_flags & PR_IP4))
2705 mtx_lock(&pr->pr_mtx);
2706 if (!(pr->pr_flags & PR_IP4)) {
2707 mtx_unlock(&pr->pr_mtx);
2710 if (pr->pr_ip4 == NULL) {
2711 mtx_unlock(&pr->pr_mtx);
2712 return (EAFNOSUPPORT);
2715 ia->s_addr = pr->pr_ip4[0].s_addr;
2716 mtx_unlock(&pr->pr_mtx);
2721 * Return 1 if we should do proper source address selection or are not jailed.
2722 * We will return 0 if we should bypass source address selection in favour
2723 * of the primary jail IPv4 address. Only in this case *ia will be updated and
2725 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2728 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2734 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2735 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2740 pr = cred->cr_prison;
2741 if (pr->pr_flags & PR_IP4_SADDRSEL)
2744 lia.s_addr = INADDR_ANY;
2745 error = prison_get_ip4(cred, &lia);
2748 if (lia.s_addr == INADDR_ANY)
2751 ia->s_addr = lia.s_addr;
2756 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2759 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2766 * No need to lock since the PR_IP4_USER flag can't be altered for
2769 while (pr1 != &prison0 &&
2771 !(pr1->pr_flags & PR_VNET) &&
2773 !(pr1->pr_flags & PR_IP4_USER))
2774 pr1 = pr1->pr_parent;
2775 while (pr2 != &prison0 &&
2777 !(pr2->pr_flags & PR_VNET) &&
2779 !(pr2->pr_flags & PR_IP4_USER))
2780 pr2 = pr2->pr_parent;
2781 return (pr1 == pr2);
2785 * Make sure our (source) address is set to something meaningful to this
2788 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2789 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2790 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2793 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2799 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2800 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2802 pr = cred->cr_prison;
2803 if (!(pr->pr_flags & PR_IP4))
2805 mtx_lock(&pr->pr_mtx);
2806 if (!(pr->pr_flags & PR_IP4)) {
2807 mtx_unlock(&pr->pr_mtx);
2810 if (pr->pr_ip4 == NULL) {
2811 mtx_unlock(&pr->pr_mtx);
2812 return (EAFNOSUPPORT);
2815 ia0.s_addr = ntohl(ia->s_addr);
2816 if (ia0.s_addr == INADDR_LOOPBACK) {
2817 ia->s_addr = pr->pr_ip4[0].s_addr;
2818 mtx_unlock(&pr->pr_mtx);
2822 if (ia0.s_addr == INADDR_ANY) {
2824 * In case there is only 1 IPv4 address, bind directly.
2826 if (pr->pr_ip4s == 1)
2827 ia->s_addr = pr->pr_ip4[0].s_addr;
2828 mtx_unlock(&pr->pr_mtx);
2832 error = _prison_check_ip4(pr, ia);
2833 mtx_unlock(&pr->pr_mtx);
2838 * Rewrite destination address in case we will connect to loopback address.
2840 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2841 * Address passed in in NBO and returned in NBO.
2844 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2848 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2849 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2851 pr = cred->cr_prison;
2852 if (!(pr->pr_flags & PR_IP4))
2854 mtx_lock(&pr->pr_mtx);
2855 if (!(pr->pr_flags & PR_IP4)) {
2856 mtx_unlock(&pr->pr_mtx);
2859 if (pr->pr_ip4 == NULL) {
2860 mtx_unlock(&pr->pr_mtx);
2861 return (EAFNOSUPPORT);
2864 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2865 ia->s_addr = pr->pr_ip4[0].s_addr;
2866 mtx_unlock(&pr->pr_mtx);
2871 * Return success because nothing had to be changed.
2873 mtx_unlock(&pr->pr_mtx);
2878 * Check if given address belongs to the jail referenced by cred/prison.
2880 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2881 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2882 * doesn't allow IPv4. Address passed in in NBO.
2885 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2890 * Check the primary IP.
2892 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2896 * All the other IPs are sorted so we can do a binary search.
2899 z = pr->pr_ip4s - 2;
2902 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2911 return (EADDRNOTAVAIL);
2915 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2920 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2921 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2923 pr = cred->cr_prison;
2924 if (!(pr->pr_flags & PR_IP4))
2926 mtx_lock(&pr->pr_mtx);
2927 if (!(pr->pr_flags & PR_IP4)) {
2928 mtx_unlock(&pr->pr_mtx);
2931 if (pr->pr_ip4 == NULL) {
2932 mtx_unlock(&pr->pr_mtx);
2933 return (EAFNOSUPPORT);
2936 error = _prison_check_ip4(pr, ia);
2937 mtx_unlock(&pr->pr_mtx);
2944 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2949 ppr = pr->pr_parent;
2950 if (!(pr->pr_flags & PR_IP6_USER)) {
2951 /* This has no user settings, so just copy the parent's list. */
2952 if (pr->pr_ip6s < ppr->pr_ip6s) {
2954 * There's no room for the parent's list. Use the
2955 * new list buffer, which is assumed to be big enough
2956 * (if it was passed). If there's no buffer, try to
2960 if (newip6 == NULL) {
2961 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2962 M_PRISON, M_NOWAIT);
2966 if (newip6 != NULL) {
2967 bcopy(ppr->pr_ip6, newip6,
2968 ppr->pr_ip6s * sizeof(*newip6));
2969 free(pr->pr_ip6, M_PRISON);
2970 pr->pr_ip6 = newip6;
2971 pr->pr_ip6s = ppr->pr_ip6s;
2975 pr->pr_ip6s = ppr->pr_ip6s;
2976 if (pr->pr_ip6s > 0)
2977 bcopy(ppr->pr_ip6, pr->pr_ip6,
2978 pr->pr_ip6s * sizeof(*newip6));
2979 else if (pr->pr_ip6 != NULL) {
2980 free(pr->pr_ip6, M_PRISON);
2983 } else if (pr->pr_ip6s > 0) {
2984 /* Remove addresses that aren't in the parent. */
2985 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2986 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2989 if (ij < ppr->pr_ip6s)
2992 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2993 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2996 for (ij = 1; ii < pr->pr_ip6s; ) {
2997 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
3002 switch (ij >= ppr->pr_ip4s ? -1 :
3003 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
3005 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
3006 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3017 if (pr->pr_ip6s == 0) {
3018 pr->pr_flags |= PR_IP6_DISABLE;
3019 free(pr->pr_ip6, M_PRISON);
3027 * Pass back primary IPv6 address for this jail.
3029 * If not restricted return success but do not alter the address. Caller has
3030 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3032 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3035 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3039 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3040 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3042 pr = cred->cr_prison;
3043 if (!(pr->pr_flags & PR_IP6))
3045 mtx_lock(&pr->pr_mtx);
3046 if (!(pr->pr_flags & PR_IP6)) {
3047 mtx_unlock(&pr->pr_mtx);
3050 if (pr->pr_ip6 == NULL) {
3051 mtx_unlock(&pr->pr_mtx);
3052 return (EAFNOSUPPORT);
3055 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3056 mtx_unlock(&pr->pr_mtx);
3061 * Return 1 if we should do proper source address selection or are not jailed.
3062 * We will return 0 if we should bypass source address selection in favour
3063 * of the primary jail IPv6 address. Only in this case *ia will be updated and
3065 * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3068 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3071 struct in6_addr lia6;
3074 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3075 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3080 pr = cred->cr_prison;
3081 if (pr->pr_flags & PR_IP6_SADDRSEL)
3085 error = prison_get_ip6(cred, &lia6);
3088 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3091 bcopy(&lia6, ia6, sizeof(struct in6_addr));
3096 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3099 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
3105 while (pr1 != &prison0 &&
3107 !(pr1->pr_flags & PR_VNET) &&
3109 !(pr1->pr_flags & PR_IP6_USER))
3110 pr1 = pr1->pr_parent;
3111 while (pr2 != &prison0 &&
3113 !(pr2->pr_flags & PR_VNET) &&
3115 !(pr2->pr_flags & PR_IP6_USER))
3116 pr2 = pr2->pr_parent;
3117 return (pr1 == pr2);
3121 * Make sure our (source) address is set to something meaningful to this jail.
3123 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3124 * when needed while binding.
3126 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3127 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3128 * doesn't allow IPv6.
3131 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3136 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3137 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3139 pr = cred->cr_prison;
3140 if (!(pr->pr_flags & PR_IP6))
3142 mtx_lock(&pr->pr_mtx);
3143 if (!(pr->pr_flags & PR_IP6)) {
3144 mtx_unlock(&pr->pr_mtx);
3147 if (pr->pr_ip6 == NULL) {
3148 mtx_unlock(&pr->pr_mtx);
3149 return (EAFNOSUPPORT);
3152 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3153 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3154 mtx_unlock(&pr->pr_mtx);
3158 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3160 * In case there is only 1 IPv6 address, and v6only is true,
3161 * then bind directly.
3163 if (v6only != 0 && pr->pr_ip6s == 1)
3164 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3165 mtx_unlock(&pr->pr_mtx);
3169 error = _prison_check_ip6(pr, ia6);
3170 mtx_unlock(&pr->pr_mtx);
3175 * Rewrite destination address in case we will connect to loopback address.
3177 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3180 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3184 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3185 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3187 pr = cred->cr_prison;
3188 if (!(pr->pr_flags & PR_IP6))
3190 mtx_lock(&pr->pr_mtx);
3191 if (!(pr->pr_flags & PR_IP6)) {
3192 mtx_unlock(&pr->pr_mtx);
3195 if (pr->pr_ip6 == NULL) {
3196 mtx_unlock(&pr->pr_mtx);
3197 return (EAFNOSUPPORT);
3200 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3201 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3202 mtx_unlock(&pr->pr_mtx);
3207 * Return success because nothing had to be changed.
3209 mtx_unlock(&pr->pr_mtx);
3214 * Check if given address belongs to the jail referenced by cred/prison.
3216 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3217 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3218 * doesn't allow IPv6.
3221 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3226 * Check the primary IP.
3228 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3232 * All the other IPs are sorted so we can do a binary search.
3235 z = pr->pr_ip6s - 2;
3238 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3247 return (EADDRNOTAVAIL);
3251 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3256 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3257 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3259 pr = cred->cr_prison;
3260 if (!(pr->pr_flags & PR_IP6))
3262 mtx_lock(&pr->pr_mtx);
3263 if (!(pr->pr_flags & PR_IP6)) {
3264 mtx_unlock(&pr->pr_mtx);
3267 if (pr->pr_ip6 == NULL) {
3268 mtx_unlock(&pr->pr_mtx);
3269 return (EAFNOSUPPORT);
3272 error = _prison_check_ip6(pr, ia6);
3273 mtx_unlock(&pr->pr_mtx);
3279 * Check if a jail supports the given address family.
3281 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3285 prison_check_af(struct ucred *cred, int af)
3290 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3292 pr = cred->cr_prison;
3294 /* Prisons with their own network stack are not limited. */
3295 if (prison_owns_vnet(cred))
3304 if (pr->pr_flags & PR_IP4)
3306 mtx_lock(&pr->pr_mtx);
3307 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3308 error = EAFNOSUPPORT;
3309 mtx_unlock(&pr->pr_mtx);
3315 if (pr->pr_flags & PR_IP6)
3317 mtx_lock(&pr->pr_mtx);
3318 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3319 error = EAFNOSUPPORT;
3320 mtx_unlock(&pr->pr_mtx);
3328 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3329 error = EAFNOSUPPORT;
3335 * Check if given address belongs to the jail referenced by cred (wrapper to
3336 * prison_check_ip[46]).
3338 * Returns 0 if jail doesn't restrict the address family or if address belongs
3339 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3340 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3343 prison_if(struct ucred *cred, struct sockaddr *sa)
3346 struct sockaddr_in *sai;
3349 struct sockaddr_in6 *sai6;
3353 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3354 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3357 if (prison_owns_vnet(cred))
3362 switch (sa->sa_family)
3366 sai = (struct sockaddr_in *)sa;
3367 error = prison_check_ip4(cred, &sai->sin_addr);
3372 sai6 = (struct sockaddr_in6 *)sa;
3373 error = prison_check_ip6(cred, &sai6->sin6_addr);
3377 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3378 error = EAFNOSUPPORT;
3384 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3387 prison_check(struct ucred *cred1, struct ucred *cred2)
3390 return ((cred1->cr_prison == cred2->cr_prison ||
3391 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3395 * Return 1 if p2 is a child of p1, otherwise 0.
3398 prison_ischild(struct prison *pr1, struct prison *pr2)
3401 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3408 * Return 1 if the passed credential is in a jail, otherwise 0.
3411 jailed(struct ucred *cred)
3414 return (cred->cr_prison != &prison0);
3418 * Return 1 if the passed credential is in a jail and that jail does not
3419 * have its own virtual network stack, otherwise 0.
3422 jailed_without_vnet(struct ucred *cred)
3428 if (prison_owns_vnet(cred))
3436 * Return the correct hostname (domainname, et al) for the passed credential.
3439 getcredhostname(struct ucred *cred, char *buf, size_t size)
3444 * A NULL credential can be used to shortcut to the physical
3445 * system's hostname.
3447 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3448 mtx_lock(&pr->pr_mtx);
3449 strlcpy(buf, pr->pr_hostname, size);
3450 mtx_unlock(&pr->pr_mtx);
3454 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3457 mtx_lock(&cred->cr_prison->pr_mtx);
3458 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3459 mtx_unlock(&cred->cr_prison->pr_mtx);
3463 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3466 mtx_lock(&cred->cr_prison->pr_mtx);
3467 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3468 mtx_unlock(&cred->cr_prison->pr_mtx);
3472 getcredhostid(struct ucred *cred, unsigned long *hostid)
3475 mtx_lock(&cred->cr_prison->pr_mtx);
3476 *hostid = cred->cr_prison->pr_hostid;
3477 mtx_unlock(&cred->cr_prison->pr_mtx);
3482 * Determine whether the prison represented by cred owns
3483 * its vnet rather than having it inherited.
3485 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3488 prison_owns_vnet(struct ucred *cred)
3492 * vnets cannot be added/removed after jail creation,
3493 * so no need to lock here.
3495 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3500 * Determine whether the subject represented by cred can "see"
3501 * status of a mount point.
3502 * Returns: 0 for permitted, ENOENT otherwise.
3503 * XXX: This function should be called cr_canseemount() and should be
3504 * placed in kern_prot.c.
3507 prison_canseemount(struct ucred *cred, struct mount *mp)
3513 pr = cred->cr_prison;
3514 if (pr->pr_enforce_statfs == 0)
3516 if (pr->pr_root->v_mount == mp)
3518 if (pr->pr_enforce_statfs == 2)
3521 * If jail's chroot directory is set to "/" we should be able to see
3522 * all mount-points from inside a jail.
3523 * This is ugly check, but this is the only situation when jail's
3524 * directory ends with '/'.
3526 if (strcmp(pr->pr_path, "/") == 0)
3528 len = strlen(pr->pr_path);
3530 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3533 * Be sure that we don't have situation where jail's root directory
3534 * is "/some/path" and mount point is "/some/pathpath".
3536 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3542 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3544 char jpath[MAXPATHLEN];
3548 pr = cred->cr_prison;
3549 if (pr->pr_enforce_statfs == 0)
3551 if (prison_canseemount(cred, mp) != 0) {
3552 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3553 strlcpy(sp->f_mntonname, "[restricted]",
3554 sizeof(sp->f_mntonname));
3557 if (pr->pr_root->v_mount == mp) {
3559 * Clear current buffer data, so we are sure nothing from
3560 * the valid path left there.
3562 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3563 *sp->f_mntonname = '/';
3567 * If jail's chroot directory is set to "/" we should be able to see
3568 * all mount-points from inside a jail.
3570 if (strcmp(pr->pr_path, "/") == 0)
3572 len = strlen(pr->pr_path);
3573 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3575 * Clear current buffer data, so we are sure nothing from
3576 * the valid path left there.
3578 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3579 if (*jpath == '\0') {
3580 /* Should never happen. */
3581 *sp->f_mntonname = '/';
3583 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3588 * Check with permission for a specific privilege is granted within jail. We
3589 * have a specific list of accepted privileges; the rest are denied.
3592 prison_priv_check(struct ucred *cred, int priv)
3600 * Privileges specific to prisons with a virtual network stack.
3601 * There might be a duplicate entry here in case the privilege
3602 * is only granted conditionally in the legacy jail case.
3607 * NFS-specific privileges.
3609 case PRIV_NFS_DAEMON:
3610 case PRIV_NFS_LOCKD:
3613 * Network stack privileges.
3615 case PRIV_NET_BRIDGE:
3618 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3619 case PRIV_NET_ROUTE:
3621 case PRIV_NET_SETIFMTU:
3622 case PRIV_NET_SETIFFLAGS:
3623 case PRIV_NET_SETIFCAP:
3624 case PRIV_NET_SETIFDESCR:
3625 case PRIV_NET_SETIFNAME :
3626 case PRIV_NET_SETIFMETRIC:
3627 case PRIV_NET_SETIFPHYS:
3628 case PRIV_NET_SETIFMAC:
3629 case PRIV_NET_ADDMULTI:
3630 case PRIV_NET_DELMULTI:
3631 case PRIV_NET_HWIOCTL:
3632 case PRIV_NET_SETLLADDR:
3633 case PRIV_NET_ADDIFGROUP:
3634 case PRIV_NET_DELIFGROUP:
3635 case PRIV_NET_IFCREATE:
3636 case PRIV_NET_IFDESTROY:
3637 case PRIV_NET_ADDIFADDR:
3638 case PRIV_NET_DELIFADDR:
3641 case PRIV_NET_SETIFVNET:
3642 case PRIV_NET_SETIFFIB:
3645 * 802.11-related privileges.
3647 case PRIV_NET80211_GETKEY:
3649 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3654 * AppleTalk privileges.
3656 case PRIV_NETATALK_RESERVEDPORT:
3661 case PRIV_NETATM_CFG:
3662 case PRIV_NETATM_ADD:
3663 case PRIV_NETATM_DEL:
3664 case PRIV_NETATM_SET:
3667 * Bluetooth privileges.
3669 case PRIV_NETBLUETOOTH_RAW:
3673 * Netgraph and netgraph module privileges.
3675 case PRIV_NETGRAPH_CONTROL:
3677 case PRIV_NETGRAPH_TTY:
3681 * IPv4 and IPv6 privileges.
3683 case PRIV_NETINET_IPFW:
3684 case PRIV_NETINET_DIVERT:
3685 case PRIV_NETINET_PF:
3686 case PRIV_NETINET_DUMMYNET:
3687 case PRIV_NETINET_CARP:
3688 case PRIV_NETINET_MROUTE:
3689 case PRIV_NETINET_RAW:
3690 case PRIV_NETINET_ADDRCTRL6:
3691 case PRIV_NETINET_ND6:
3692 case PRIV_NETINET_SCOPE6:
3693 case PRIV_NETINET_ALIFETIME6:
3694 case PRIV_NETINET_IPSEC:
3695 case PRIV_NETINET_BINDANY:
3699 * IPX/SPX privileges.
3701 case PRIV_NETIPX_RESERVEDPORT:
3702 case PRIV_NETIPX_RAW:
3716 * No default: or deny here.
3717 * In case of no permit fall through to next switch().
3719 if (cred->cr_prison->pr_flags & PR_VNET)
3727 * Allow ktrace privileges for root in jail.
3733 * Allow jailed processes to configure audit identity and
3734 * submit audit records (login, etc). In the future we may
3735 * want to further refine the relationship between audit and
3738 case PRIV_AUDIT_GETAUDIT:
3739 case PRIV_AUDIT_SETAUDIT:
3740 case PRIV_AUDIT_SUBMIT:
3744 * Allow jailed processes to manipulate process UNIX
3745 * credentials in any way they see fit.
3747 case PRIV_CRED_SETUID:
3748 case PRIV_CRED_SETEUID:
3749 case PRIV_CRED_SETGID:
3750 case PRIV_CRED_SETEGID:
3751 case PRIV_CRED_SETGROUPS:
3752 case PRIV_CRED_SETREUID:
3753 case PRIV_CRED_SETREGID:
3754 case PRIV_CRED_SETRESUID:
3755 case PRIV_CRED_SETRESGID:
3758 * Jail implements visibility constraints already, so allow
3759 * jailed root to override uid/gid-based constraints.
3761 case PRIV_SEEOTHERGIDS:
3762 case PRIV_SEEOTHERUIDS:
3765 * Jail implements inter-process debugging limits already, so
3766 * allow jailed root various debugging privileges.
3768 case PRIV_DEBUG_DIFFCRED:
3769 case PRIV_DEBUG_SUGID:
3770 case PRIV_DEBUG_UNPRIV:
3773 * Allow jail to set various resource limits and login
3774 * properties, and for now, exceed process resource limits.
3776 case PRIV_PROC_LIMIT:
3777 case PRIV_PROC_SETLOGIN:
3778 case PRIV_PROC_SETRLIMIT:
3781 * System V and POSIX IPC privileges are granted in jail.
3784 case PRIV_IPC_WRITE:
3785 case PRIV_IPC_ADMIN:
3786 case PRIV_IPC_MSGSIZE:
3790 * Jail operations within a jail work on child jails.
3792 case PRIV_JAIL_ATTACH:
3794 case PRIV_JAIL_REMOVE:
3797 * Jail implements its own inter-process limits, so allow
3798 * root processes in jail to change scheduling on other
3799 * processes in the same jail. Likewise for signalling.
3801 case PRIV_SCHED_DIFFCRED:
3802 case PRIV_SCHED_CPUSET:
3803 case PRIV_SIGNAL_DIFFCRED:
3804 case PRIV_SIGNAL_SUGID:
3807 * Allow jailed processes to write to sysctls marked as jail
3810 case PRIV_SYSCTL_WRITEJAIL:
3813 * Allow root in jail to manage a variety of quota
3814 * properties. These should likely be conditional on a
3815 * configuration option.
3817 case PRIV_VFS_GETQUOTA:
3818 case PRIV_VFS_SETQUOTA:
3821 * Since Jail relies on chroot() to implement file system
3822 * protections, grant many VFS privileges to root in jail.
3823 * Be careful to exclude mount-related and NFS-related
3827 case PRIV_VFS_WRITE:
3828 case PRIV_VFS_ADMIN:
3830 case PRIV_VFS_LOOKUP:
3831 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3832 case PRIV_VFS_CHFLAGS_DEV:
3833 case PRIV_VFS_CHOWN:
3834 case PRIV_VFS_CHROOT:
3835 case PRIV_VFS_RETAINSUGID:
3836 case PRIV_VFS_FCHROOT:
3838 case PRIV_VFS_SETGID:
3840 case PRIV_VFS_STICKYFILE:
3844 * Depending on the global setting, allow privilege of
3845 * setting system flags.
3847 case PRIV_VFS_SYSFLAGS:
3848 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3854 * Depending on the global setting, allow privilege of
3855 * mounting/unmounting file systems.
3857 case PRIV_VFS_MOUNT:
3858 case PRIV_VFS_UNMOUNT:
3859 case PRIV_VFS_MOUNT_NONUSER:
3860 case PRIV_VFS_MOUNT_OWNER:
3861 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3867 * Allow jailed root to bind reserved ports and reuse in-use
3870 case PRIV_NETINET_RESERVEDPORT:
3871 case PRIV_NETINET_REUSEPORT:
3875 * Allow jailed root to set certian IPv4/6 (option) headers.
3877 case PRIV_NETINET_SETHDROPTS:
3881 * Conditionally allow creating raw sockets in jail.
3883 case PRIV_NETINET_RAW:
3884 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3890 * Since jail implements its own visibility limits on netstat
3891 * sysctls, allow getcred. This allows identd to work in
3894 case PRIV_NETINET_GETCRED:
3898 * Allow jailed root to set loginclass.
3900 case PRIV_PROC_SETLOGINCLASS:
3905 * In all remaining cases, deny the privilege request. This
3906 * includes almost all network privileges, many system
3907 * configuration privileges.
3914 * Return the part of pr2's name that is relative to pr1, or the whole name
3915 * if it does not directly follow.
3919 prison_name(struct prison *pr1, struct prison *pr2)
3923 /* Jails see themselves as "0" (if they see themselves at all). */
3926 name = pr2->pr_name;
3927 if (prison_ischild(pr1, pr2)) {
3929 * pr1 isn't locked (and allprison_lock may not be either)
3930 * so its length can't be counted on. But the number of dots
3931 * can be counted on - and counted.
3933 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3934 name = strchr(name, '.') + 1;
3940 * Return the part of pr2's path that is relative to pr1, or the whole path
3941 * if it does not directly follow.
3944 prison_path(struct prison *pr1, struct prison *pr2)
3946 char *path1, *path2;
3949 path1 = pr1->pr_path;
3950 path2 = pr2->pr_path;
3951 if (!strcmp(path1, "/"))
3953 len1 = strlen(path1);
3954 if (strncmp(path1, path2, len1))
3956 if (path2[len1] == '\0')
3958 if (path2[len1] == '/')
3959 return (path2 + len1);
3965 * Jail-related sysctls.
3967 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3971 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3974 struct prison *pr, *cpr;
3976 struct in_addr *ip4 = NULL;
3980 struct in6_addr *ip6 = NULL;
3985 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3986 pr = req->td->td_ucred->cr_prison;
3988 sx_slock(&allprison_lock);
3989 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3990 #if defined(INET) || defined(INET6)
3993 mtx_lock(&cpr->pr_mtx);
3995 if (cpr->pr_ip4s > 0) {
3996 if (ip4s < cpr->pr_ip4s) {
3997 ip4s = cpr->pr_ip4s;
3998 mtx_unlock(&cpr->pr_mtx);
3999 ip4 = realloc(ip4, ip4s *
4000 sizeof(struct in_addr), M_TEMP, M_WAITOK);
4003 bcopy(cpr->pr_ip4, ip4,
4004 cpr->pr_ip4s * sizeof(struct in_addr));
4008 if (cpr->pr_ip6s > 0) {
4009 if (ip6s < cpr->pr_ip6s) {
4010 ip6s = cpr->pr_ip6s;
4011 mtx_unlock(&cpr->pr_mtx);
4012 ip6 = realloc(ip6, ip6s *
4013 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4016 bcopy(cpr->pr_ip6, ip6,
4017 cpr->pr_ip6s * sizeof(struct in6_addr));
4020 if (cpr->pr_ref == 0) {
4021 mtx_unlock(&cpr->pr_mtx);
4024 bzero(xp, sizeof(*xp));
4025 xp->pr_version = XPRISON_VERSION;
4026 xp->pr_id = cpr->pr_id;
4027 xp->pr_state = cpr->pr_uref > 0
4028 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4029 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4030 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4031 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4033 xp->pr_ip4s = cpr->pr_ip4s;
4036 xp->pr_ip6s = cpr->pr_ip6s;
4038 mtx_unlock(&cpr->pr_mtx);
4039 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4043 if (xp->pr_ip4s > 0) {
4044 error = SYSCTL_OUT(req, ip4,
4045 xp->pr_ip4s * sizeof(struct in_addr));
4051 if (xp->pr_ip6s > 0) {
4052 error = SYSCTL_OUT(req, ip6,
4053 xp->pr_ip6s * sizeof(struct in6_addr));
4059 sx_sunlock(&allprison_lock);
4070 SYSCTL_OID(_security_jail, OID_AUTO, list,
4071 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4072 sysctl_jail_list, "S", "List of active jails");
4075 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4079 injail = jailed(req->td->td_ucred);
4080 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4085 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4086 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4087 sysctl_jail_jailed, "I", "Process in jail?");
4089 #if defined(INET) || defined(INET6)
4090 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4091 &jail_max_af_ips, 0,
4092 "Number of IP addresses a jail may have at most per address family");
4096 * Default parameters for jail(2) compatability. For historical reasons,
4097 * the sysctl names have varying similarity to the parameter names. Prisons
4098 * just see their own parameters, and can't change them.
4101 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4104 int allow, error, i;
4106 pr = req->td->td_ucred->cr_prison;
4107 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4109 /* Get the current flag value, and convert it to a boolean. */
4110 i = (allow & arg2) ? 1 : 0;
4113 error = sysctl_handle_int(oidp, &i, 0, req);
4114 if (error || !req->newptr)
4120 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4123 mtx_lock(&prison0.pr_mtx);
4124 jail_default_allow = (jail_default_allow & ~arg2) | i;
4125 mtx_unlock(&prison0.pr_mtx);
4129 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4130 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4131 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4132 "Processes in jail can set their hostnames");
4133 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4135 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4136 "Processes in jail are limited to creating UNIX/IP/route sockets only");
4137 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4138 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4139 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4140 "Processes in jail can use System V IPC primitives");
4141 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4142 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4143 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4144 "Prison root can create raw sockets");
4145 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4146 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4147 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4148 "Processes in jail can alter system file flags");
4149 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4150 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4151 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4152 "Processes in jail can mount/unmount jail-friendly file systems");
4155 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4160 pr = req->td->td_ucred->cr_prison;
4161 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4162 error = sysctl_handle_int(oidp, &level, 0, req);
4163 if (error || !req->newptr)
4165 *(int *)arg1 = level;
4169 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4170 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4171 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4172 sysctl_jail_default_level, "I",
4173 "Processes in jail cannot see all mounted file systems");
4176 * Nodes to describe jail parameters. Maximum length of string parameters
4177 * is returned in the string itself, and the other parameters exist merely
4178 * to make themselves and their types known.
4180 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4184 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4191 switch (oidp->oid_kind & CTLTYPE)
4197 if (!(req->flags & SCTL_MASK32))
4199 return (SYSCTL_OUT(req, &l, sizeof(l)));
4203 return (SYSCTL_OUT(req, &i, sizeof(i)));
4204 case CTLTYPE_STRING:
4205 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4207 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4208 case CTLTYPE_STRUCT:
4210 return (SYSCTL_OUT(req, &s, sizeof(s)));
4215 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4216 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4217 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4218 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4219 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4220 "I", "Jail secure level");
4221 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4222 "I", "Jail cannot see all mounted file systems");
4223 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4224 "B", "Jail persistence");
4226 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4227 "E,jailsys", "Virtual network stack");
4229 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4230 "B", "Jail is in the process of shutting down");
4232 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4233 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4234 "I", "Current number of child jails");
4235 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4236 "I", "Maximum number of child jails");
4238 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4239 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4241 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4242 "Jail NIS domainname");
4243 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4245 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4246 "LU", "Jail host ID");
4248 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4249 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4252 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4253 "Jail IPv4 address virtualization");
4254 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4255 "S,in_addr,a", "Jail IPv4 addresses");
4256 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4257 "B", "Do (not) use IPv4 source address selection rather than the "
4258 "primary jail IPv4 address.");
4261 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4262 "Jail IPv6 address virtualization");
4263 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4264 "S,in6_addr,a", "Jail IPv6 addresses");
4265 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4266 "B", "Do (not) use IPv6 source address selection rather than the "
4267 "primary jail IPv6 address.");
4270 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4271 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4272 "B", "Jail may set hostname");
4273 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4274 "B", "Jail may use SYSV IPC");
4275 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4276 "B", "Jail may create raw sockets");
4277 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4278 "B", "Jail may alter system file flags");
4279 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4280 "B", "Jail may mount/unmount jail-friendly file systems");
4281 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4282 "B", "Jail may set file quotas");
4283 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4284 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4287 prison_racct_foreach(void (*callback)(struct racct *racct,
4288 void *arg2, void *arg3), void *arg2, void *arg3)
4290 struct prison_racct *prr;
4292 sx_slock(&allprison_lock);
4293 LIST_FOREACH(prr, &allprison_racct, prr_next)
4294 (callback)(prr->prr_racct, arg2, arg3);
4295 sx_sunlock(&allprison_lock);
4298 static struct prison_racct *
4299 prison_racct_find_locked(const char *name)
4301 struct prison_racct *prr;
4303 sx_assert(&allprison_lock, SA_XLOCKED);
4305 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4308 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4309 if (strcmp(name, prr->prr_name) != 0)
4312 /* Found prison_racct with a matching name? */
4313 prison_racct_hold(prr);
4317 /* Add new prison_racct. */
4318 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4319 racct_create(&prr->prr_racct);
4321 strcpy(prr->prr_name, name);
4322 refcount_init(&prr->prr_refcount, 1);
4323 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4328 struct prison_racct *
4329 prison_racct_find(const char *name)
4331 struct prison_racct *prr;
4333 sx_xlock(&allprison_lock);
4334 prr = prison_racct_find_locked(name);
4335 sx_xunlock(&allprison_lock);
4340 prison_racct_hold(struct prison_racct *prr)
4343 refcount_acquire(&prr->prr_refcount);
4347 prison_racct_free(struct prison_racct *prr)
4351 old = prr->prr_refcount;
4352 if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1))
4355 sx_xlock(&allprison_lock);
4356 if (refcount_release(&prr->prr_refcount)) {
4357 racct_destroy(&prr->prr_racct);
4358 LIST_REMOVE(prr, prr_next);
4359 sx_xunlock(&allprison_lock);
4360 free(prr, M_PRISON_RACCT);
4364 sx_xunlock(&allprison_lock);
4369 prison_racct_attach(struct prison *pr)
4371 struct prison_racct *prr;
4373 prr = prison_racct_find_locked(pr->pr_name);
4374 KASSERT(prr != NULL, ("cannot find prison_racct"));
4376 pr->pr_prison_racct = prr;
4380 prison_racct_detach(struct prison *pr)
4382 prison_racct_free(pr->pr_prison_racct);
4383 pr->pr_prison_racct = NULL;
4390 db_show_prison(struct prison *pr)
4393 #if defined(INET) || defined(INET6)
4398 char ip6buf[INET6_ADDRSTRLEN];
4401 db_printf("prison %p:\n", pr);
4402 db_printf(" jid = %d\n", pr->pr_id);
4403 db_printf(" name = %s\n", pr->pr_name);
4404 db_printf(" parent = %p\n", pr->pr_parent);
4405 db_printf(" ref = %d\n", pr->pr_ref);
4406 db_printf(" uref = %d\n", pr->pr_uref);
4407 db_printf(" path = %s\n", pr->pr_path);
4408 db_printf(" cpuset = %d\n", pr->pr_cpuset
4409 ? pr->pr_cpuset->cs_id : -1);
4411 db_printf(" vnet = %p\n", pr->pr_vnet);
4413 db_printf(" root = %p\n", pr->pr_root);
4414 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4415 db_printf(" children.max = %d\n", pr->pr_childmax);
4416 db_printf(" children.cur = %d\n", pr->pr_childcount);
4417 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4418 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4419 db_printf(" flags = 0x%x", pr->pr_flags);
4420 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4422 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4423 db_printf(" %s", pr_flag_names[fi]);
4424 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4426 jsf = pr->pr_flags &
4427 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4428 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4429 pr_flag_jailsys[fi].disable &&
4430 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4431 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4434 db_printf(" allow = 0x%x", pr->pr_allow);
4435 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4437 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4438 db_printf(" %s", pr_allow_names[fi]);
4440 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4441 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4442 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4443 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4444 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4446 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4447 for (ii = 0; ii < pr->pr_ip4s; ii++)
4448 db_printf(" %s %s\n",
4449 ii == 0 ? "ip4.addr =" : " ",
4450 inet_ntoa(pr->pr_ip4[ii]));
4453 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4454 for (ii = 0; ii < pr->pr_ip6s; ii++)
4455 db_printf(" %s %s\n",
4456 ii == 0 ? "ip6.addr =" : " ",
4457 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4461 DB_SHOW_COMMAND(prison, db_show_prison_command)
4467 * Show all prisons in the list, and prison0 which is not
4470 db_show_prison(&prison0);
4471 if (!db_pager_quit) {
4472 TAILQ_FOREACH(pr, &allprison, pr_list) {
4484 /* Look for a prison with the ID and with references. */
4485 TAILQ_FOREACH(pr, &allprison, pr_list)
4486 if (pr->pr_id == addr && pr->pr_ref > 0)
4489 /* Look again, without requiring a reference. */
4490 TAILQ_FOREACH(pr, &allprison, pr_list)
4491 if (pr->pr_id == addr)
4494 /* Assume address points to a valid prison. */
4495 pr = (struct prison *)addr;