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>
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");
82 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
85 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
87 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
91 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
93 #define _PR_IP_SADDRSEL 0
97 /* prison0 describes what is "real" about the system. */
98 struct prison prison0 = {
104 .pr_securelevel = -1,
105 .pr_childmax = JAIL_MAX,
106 .pr_hostuuid = DEFAULT_HOSTUUID,
107 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
109 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
111 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
113 .pr_allow = PR_ALLOW_ALL,
115 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
117 /* allprison and lastprid are protected by allprison_lock. */
118 struct sx allprison_lock;
119 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
120 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
123 static int do_jail_attach(struct thread *td, struct prison *pr);
124 static void prison_complete(void *context, int pending);
125 static void prison_deref(struct prison *pr, int flags);
126 static char *prison_path(struct prison *pr1, struct prison *pr2);
127 static void prison_remove_one(struct prison *pr);
129 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
130 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
133 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
134 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
137 /* Flags for prison_deref */
138 #define PD_DEREF 0x01
139 #define PD_DEUREF 0x02
140 #define PD_LOCKED 0x04
141 #define PD_LIST_SLOCKED 0x08
142 #define PD_LIST_XLOCKED 0x10
145 * Parameter names corresponding to PR_* flag values. Size values are for kvm
146 * as we cannot figure out the size of a sparse array, or an array without a
149 static char *pr_flag_names[] = {
152 [7] = "ip4.saddrsel",
155 [8] = "ip6.saddrsel",
158 const size_t pr_flag_names_size = sizeof(pr_flag_names);
160 static char *pr_flag_nonames[] = {
163 [7] = "ip4.nosaddrsel",
166 [8] = "ip6.nosaddrsel",
169 const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames);
171 struct jailsys_flags {
175 } pr_flag_jailsys[] = {
176 { "host", 0, PR_HOST },
178 { "vnet", 0, PR_VNET },
181 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
184 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
187 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
189 static char *pr_allow_names[] = {
190 "allow.set_hostname",
198 const size_t pr_allow_names_size = sizeof(pr_allow_names);
200 static char *pr_allow_nonames[] = {
201 "allow.noset_hostname",
203 "allow.noraw_sockets",
209 const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames);
211 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
212 #define JAIL_DEFAULT_ENFORCE_STATFS 2
213 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
214 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
215 #if defined(INET) || defined(INET6)
216 static unsigned jail_max_af_ips = 255;
221 qcmp_v4(const void *ip1, const void *ip2)
226 * We need to compare in HBO here to get the list sorted as expected
227 * by the result of the code. Sorting NBO addresses gives you
228 * interesting results. If you do not understand, do not try.
230 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
231 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
234 * Do not simply return the difference of the two numbers, the int is
248 qcmp_v6(const void *ip1, const void *ip2)
250 const struct in6_addr *ia6a, *ia6b;
253 ia6a = (const struct in6_addr *)ip1;
254 ia6b = (const struct in6_addr *)ip2;
257 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
258 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
260 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
273 jail(struct thread *td, struct jail_args *uap)
279 error = copyin(uap->jail, &version, sizeof(uint32_t));
288 /* FreeBSD single IPv4 jails. */
289 bzero(&j, sizeof(struct jail));
290 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
293 j.version = j0.version;
295 j.hostname = j0.hostname;
296 j.ip4s = j0.ip_number;
302 * Version 1 was used by multi-IPv4 jail implementations
303 * that never made it into the official kernel.
307 case 2: /* JAIL_API_VERSION */
308 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
309 error = copyin(uap->jail, &j, sizeof(struct jail));
315 /* Sci-Fi jails are not supported, sorry. */
318 return (kern_jail(td, &j));
322 kern_jail(struct thread *td, struct jail *j)
324 struct iovec optiov[2 * (4
325 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
334 char *u_path, *u_hostname, *u_name;
337 struct in_addr *u_ip4;
340 struct in6_addr *u_ip6;
343 int error, enforce_statfs, fi;
345 bzero(&optiov, sizeof(optiov));
346 opt.uio_iov = optiov;
350 opt.uio_segflg = UIO_SYSSPACE;
351 opt.uio_rw = UIO_READ;
354 /* Set permissions for top-level jails from sysctls. */
355 if (!jailed(td->td_ucred)) {
356 for (fi = 0; fi < sizeof(pr_allow_names) /
357 sizeof(pr_allow_names[0]); fi++) {
358 optiov[opt.uio_iovcnt].iov_base =
359 (jail_default_allow & (1 << fi))
360 ? pr_allow_names[fi] : pr_allow_nonames[fi];
361 optiov[opt.uio_iovcnt].iov_len =
362 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
365 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
366 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
368 enforce_statfs = jail_default_enforce_statfs;
369 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
370 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
374 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
376 ip4s = (j->version == 0) ? 1 : j->ip4s;
377 if (ip4s > jail_max_af_ips)
379 tmplen += ip4s * sizeof(struct in_addr);
385 if (j->ip6s > jail_max_af_ips)
387 tmplen += j->ip6s * sizeof(struct in6_addr);
392 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
393 u_hostname = u_path + MAXPATHLEN;
394 u_name = u_hostname + MAXHOSTNAMELEN;
396 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
400 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
402 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
405 optiov[opt.uio_iovcnt].iov_base = "path";
406 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
408 optiov[opt.uio_iovcnt].iov_base = u_path;
409 error = copyinstr(j->path, u_path, MAXPATHLEN,
410 &optiov[opt.uio_iovcnt].iov_len);
412 free(u_path, M_TEMP);
416 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
417 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
419 optiov[opt.uio_iovcnt].iov_base = u_hostname;
420 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
421 &optiov[opt.uio_iovcnt].iov_len);
423 free(u_path, M_TEMP);
427 if (j->jailname != NULL) {
428 optiov[opt.uio_iovcnt].iov_base = "name";
429 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
431 optiov[opt.uio_iovcnt].iov_base = u_name;
432 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
433 &optiov[opt.uio_iovcnt].iov_len);
435 free(u_path, M_TEMP);
441 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
442 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
444 optiov[opt.uio_iovcnt].iov_base = u_ip4;
445 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
447 u_ip4->s_addr = j->ip4s;
449 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
451 free(u_path, M_TEMP);
458 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
459 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
461 optiov[opt.uio_iovcnt].iov_base = u_ip6;
462 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
463 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
465 free(u_path, M_TEMP);
470 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
471 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
472 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
473 free(u_path, M_TEMP);
479 * struct jail_set_args {
480 * struct iovec *iovp;
481 * unsigned int iovcnt;
486 jail_set(struct thread *td, struct jail_set_args *uap)
491 /* Check that we have an even number of iovecs. */
495 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
498 error = kern_jail_set(td, auio, uap->flags);
504 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
511 struct in6_addr *ip6;
514 struct vfsoptlist *opts;
515 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
517 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
518 #if defined(INET) || defined(INET6)
523 size_t namelen, onamelen;
524 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
525 int gotchildmax, gotenforce, gothid, gotslevel;
526 int fi, jid, jsys, len, level;
527 int childmax, slevel, vfslocked;
528 #if defined(INET) || defined(INET6)
537 unsigned pr_flags, ch_flags;
538 unsigned pr_allow, ch_allow, tallow;
541 error = priv_check(td, PRIV_JAIL_SET);
542 if (!error && (flags & JAIL_ATTACH))
543 error = priv_check(td, PRIV_JAIL_ATTACH);
546 mypr = ppr = td->td_ucred->cr_prison;
547 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
549 if (flags & ~JAIL_SET_MASK)
553 * Check all the parameters before committing to anything. Not all
554 * errors can be caught early, but we may as well try. Also, this
555 * takes care of some expensive stuff (path lookup) before getting
556 * the allprison lock.
558 * XXX Jails are not filesystems, and jail parameters are not mount
559 * options. But it makes more sense to re-use the vfsopt code
560 * than duplicate it under a different name.
562 error = vfs_buildopts(optuio, &opts);
572 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
578 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
587 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
595 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
600 else if (enforce < 0 || enforce > 2) {
606 pr_flags = ch_flags = 0;
607 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
609 if (pr_flag_names[fi] == NULL)
611 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
612 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
614 ch_flags |= pr_flags;
615 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
617 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
624 case JAIL_SYS_DISABLE:
625 if (!pr_flag_jailsys[fi].disable) {
629 pr_flags |= pr_flag_jailsys[fi].disable;
632 pr_flags |= pr_flag_jailsys[fi].new;
634 case JAIL_SYS_INHERIT:
641 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
643 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
644 && !(pr_flags & PR_PERSIST)) {
646 vfs_opterror(opts, "new jail must persist or attach");
650 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
652 vfs_opterror(opts, "vnet cannot be changed after creation");
657 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
659 vfs_opterror(opts, "ip4 cannot be changed after creation");
664 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
666 vfs_opterror(opts, "ip6 cannot be changed after creation");
671 pr_allow = ch_allow = 0;
672 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
674 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
675 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
677 ch_allow |= pr_allow;
679 error = vfs_getopt(opts, "name", (void **)&name, &len);
685 if (len == 0 || name[len - 1] != '\0') {
689 if (len > MAXHOSTNAMELEN) {
690 error = ENAMETOOLONG;
695 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
703 if (len == 0 || host[len - 1] != '\0') {
707 if (len > MAXHOSTNAMELEN) {
708 error = ENAMETOOLONG;
713 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
721 if (len == 0 || domain[len - 1] != '\0') {
725 if (len > MAXHOSTNAMELEN) {
726 error = ENAMETOOLONG;
731 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
739 if (len == 0 || uuid[len - 1] != '\0') {
743 if (len > HOSTUUIDLEN) {
744 error = ENAMETOOLONG;
749 #ifdef COMPAT_FREEBSD32
750 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
753 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
757 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
769 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
771 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
774 else if (ip4s & (sizeof(*ip4) - 1)) {
778 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
780 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
782 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
783 ip4s /= sizeof(*ip4);
784 if (ip4s > jail_max_af_ips) {
786 vfs_opterror(opts, "too many IPv4 addresses");
789 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
790 bcopy(op, ip4, ip4s * sizeof(*ip4));
792 * IP addresses are all sorted but ip[0] to preserve
793 * the primary IP address as given from userland.
794 * This special IP is used for unbound outgoing
795 * connections as well for "loopback" traffic in case
796 * source address selection cannot find any more fitting
797 * address to connect from.
800 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
802 * Check for duplicate addresses and do some simple
803 * zero and broadcast checks. If users give other bogus
804 * addresses it is their problem.
806 * We do not have to care about byte order for these
807 * checks so we will do them in NBO.
809 for (ii = 0; ii < ip4s; ii++) {
810 if (ip4[ii].s_addr == INADDR_ANY ||
811 ip4[ii].s_addr == INADDR_BROADCAST) {
816 (ip4[0].s_addr == ip4[ii+1].s_addr ||
817 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
827 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
829 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
832 else if (ip6s & (sizeof(*ip6) - 1)) {
836 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
838 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
840 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
841 ip6s /= sizeof(*ip6);
842 if (ip6s > jail_max_af_ips) {
844 vfs_opterror(opts, "too many IPv6 addresses");
847 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
848 bcopy(op, ip6, ip6s * sizeof(*ip6));
850 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
851 for (ii = 0; ii < ip6s; ii++) {
852 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
857 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
858 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
868 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
869 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
872 "vnet jails cannot have IP address restrictions");
878 error = vfs_getopt(opts, "path", (void **)&path, &len);
884 if (flags & JAIL_UPDATE) {
887 "path cannot be changed after creation");
890 if (len == 0 || path[len - 1] != '\0') {
894 if (len < 2 || (len == 2 && path[0] == '/'))
897 /* Leave room for a real-root full pathname. */
898 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
899 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
900 error = ENAMETOOLONG;
903 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
908 vfslocked = NDHASGIANT(&nd);
910 NDFREE(&nd, NDF_ONLY_PNBUF);
911 if (root->v_type != VDIR) {
914 VFS_UNLOCK_GIANT(vfslocked);
917 VFS_UNLOCK_GIANT(vfslocked);
922 * Grab the allprison lock before letting modules check their
923 * parameters. Once we have it, do not let go so we'll have a
924 * consistent view of the OSD list.
926 sx_xlock(&allprison_lock);
927 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
929 goto done_unlock_list;
931 /* By now, all parameters should have been noted. */
932 TAILQ_FOREACH(opt, opts, link) {
933 if (!opt->seen && strcmp(opt->name, "errmsg")) {
935 vfs_opterror(opts, "unknown parameter: %s", opt->name);
936 goto done_unlock_list;
941 * See if we are creating a new record or updating an existing one.
942 * This abuses the file error codes ENOENT and EEXIST.
944 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
947 vfs_opterror(opts, "no valid operation (create or update)");
948 goto done_unlock_list;
952 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
953 namelc = strrchr(name, '.');
954 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
960 * See if a requested jid already exists. There is an
961 * information leak here if the jid exists but is not within
962 * the caller's jail hierarchy. Jail creators will get EEXIST
963 * even though they cannot see the jail, and CREATE | UPDATE
964 * will return ENOENT which is not normally a valid error.
968 vfs_opterror(opts, "negative jid");
969 goto done_unlock_list;
971 pr = prison_find(jid);
974 /* Create: jid must not exist. */
975 if (cuflags == JAIL_CREATE) {
976 mtx_unlock(&pr->pr_mtx);
978 vfs_opterror(opts, "jail %d already exists",
980 goto done_unlock_list;
982 if (!prison_ischild(mypr, pr)) {
983 mtx_unlock(&pr->pr_mtx);
985 } else if (pr->pr_uref == 0) {
986 if (!(flags & JAIL_DYING)) {
987 mtx_unlock(&pr->pr_mtx);
989 vfs_opterror(opts, "jail %d is dying",
991 goto done_unlock_list;
992 } else if ((flags & JAIL_ATTACH) ||
993 (pr_flags & PR_PERSIST)) {
995 * A dying jail might be resurrected
996 * (via attach or persist), but first
997 * it must determine if another jail
998 * has claimed its name. Accomplish
999 * this by implicitly re-setting the
1003 name = prison_name(mypr, pr);
1008 /* Update: jid must exist. */
1009 if (cuflags == JAIL_UPDATE) {
1011 vfs_opterror(opts, "jail %d not found", jid);
1012 goto done_unlock_list;
1017 * If the caller provided a name, look for a jail by that name.
1018 * This has different semantics for creates and updates keyed by jid
1019 * (where the name must not already exist in a different jail),
1020 * and updates keyed by the name itself (where the name must exist
1021 * because that is the jail being updated).
1024 namelc = strrchr(name, '.');
1029 * This is a hierarchical name. Split it into the
1030 * parent and child names, and make sure the parent
1031 * exists or matches an already found jail.
1035 if (strncmp(name, ppr->pr_name, namelc - name)
1036 || ppr->pr_name[namelc - name] != '\0') {
1037 mtx_unlock(&pr->pr_mtx);
1040 "cannot change jail's parent");
1041 goto done_unlock_list;
1044 ppr = prison_find_name(mypr, name);
1048 "jail \"%s\" not found", name);
1049 goto done_unlock_list;
1051 mtx_unlock(&ppr->pr_mtx);
1055 if (name[0] != '\0') {
1057 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1060 FOREACH_PRISON_CHILD(ppr, tpr) {
1061 if (tpr != pr && tpr->pr_ref > 0 &&
1062 !strcmp(tpr->pr_name + namelen, name)) {
1064 cuflags != JAIL_CREATE) {
1065 mtx_lock(&tpr->pr_mtx);
1066 if (tpr->pr_ref > 0) {
1071 if (tpr->pr_uref > 0) {
1077 mtx_unlock(&tpr->pr_mtx);
1078 } else if (tpr->pr_uref > 0) {
1080 * Create, or update(jid):
1081 * name must not exist in an
1082 * active sibling jail.
1086 mtx_unlock(&pr->pr_mtx);
1088 "jail \"%s\" already exists",
1090 goto done_unlock_list;
1094 /* If no active jail is found, use a dying one. */
1095 if (deadpr != NULL && pr == NULL) {
1096 if (flags & JAIL_DYING) {
1097 mtx_lock(&deadpr->pr_mtx);
1098 if (deadpr->pr_ref == 0) {
1099 mtx_unlock(&deadpr->pr_mtx);
1103 } else if (cuflags == JAIL_UPDATE) {
1106 "jail \"%s\" is dying", name);
1107 goto done_unlock_list;
1110 /* Update: name must exist if no jid. */
1111 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1113 vfs_opterror(opts, "jail \"%s\" not found",
1115 goto done_unlock_list;
1119 /* Update: must provide a jid or name. */
1120 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1122 vfs_opterror(opts, "update specified no jail");
1123 goto done_unlock_list;
1126 /* If there's no prison to update, create a new one and link it in. */
1128 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1129 if (tpr->pr_childcount >= tpr->pr_childmax) {
1131 vfs_opterror(opts, "prison limit exceeded");
1132 goto done_unlock_list;
1135 mtx_lock(&ppr->pr_mtx);
1136 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1137 mtx_unlock(&ppr->pr_mtx);
1139 vfs_opterror(opts, "parent jail went away!");
1140 goto done_unlock_list;
1144 mtx_unlock(&ppr->pr_mtx);
1145 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1147 /* Find the next free jid. */
1150 if (jid == JAIL_MAX)
1152 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1153 if (tpr->pr_id < jid)
1155 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1156 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1159 if (jid == lastprid) {
1162 "no available jail IDs");
1164 prison_deref(ppr, PD_DEREF |
1165 PD_DEUREF | PD_LIST_XLOCKED);
1174 * The jail already has a jid (that did not yet exist),
1175 * so just find where to insert it.
1177 TAILQ_FOREACH(tpr, &allprison, pr_list)
1178 if (tpr->pr_id >= jid) {
1179 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1184 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1185 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1186 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1187 tpr->pr_childcount++;
1189 pr->pr_parent = ppr;
1192 /* Set some default values, and inherit some from the parent. */
1197 root = mypr->pr_root;
1200 racct_create(&pr->pr_racct);
1201 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1202 pr->pr_flags |= PR_HOST;
1203 #if defined(INET) || defined(INET6)
1205 if (!(pr_flags & PR_VNET))
1209 if (!(ch_flags & PR_IP4_USER))
1211 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1212 else if (!(pr_flags & PR_IP4_USER)) {
1213 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1214 if (ppr->pr_ip4 != NULL) {
1215 pr->pr_ip4s = ppr->pr_ip4s;
1216 pr->pr_ip4 = malloc(pr->pr_ip4s *
1217 sizeof(struct in_addr), M_PRISON,
1219 bcopy(ppr->pr_ip4, pr->pr_ip4,
1220 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1225 if (!(ch_flags & PR_IP6_USER))
1227 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1228 else if (!(pr_flags & PR_IP6_USER)) {
1229 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1230 if (ppr->pr_ip6 != NULL) {
1231 pr->pr_ip6s = ppr->pr_ip6s;
1232 pr->pr_ip6 = malloc(pr->pr_ip6s *
1233 sizeof(struct in6_addr), M_PRISON,
1235 bcopy(ppr->pr_ip6, pr->pr_ip6,
1236 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1242 /* Source address selection is always on by default. */
1243 pr->pr_flags |= _PR_IP_SADDRSEL;
1245 pr->pr_securelevel = ppr->pr_securelevel;
1246 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1247 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1249 LIST_INIT(&pr->pr_children);
1250 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1253 /* Allocate a new vnet if specified. */
1254 pr->pr_vnet = (pr_flags & PR_VNET)
1255 ? vnet_alloc() : ppr->pr_vnet;
1258 * Allocate a dedicated cpuset for each jail.
1259 * Unlike other initial settings, this may return an erorr.
1261 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1263 prison_deref(pr, PD_LIST_XLOCKED);
1267 mtx_lock(&pr->pr_mtx);
1269 * New prisons do not yet have a reference, because we do not
1270 * want other to see the incomplete prison once the
1271 * allprison_lock is downgraded.
1276 * Grab a reference for existing prisons, to ensure they
1277 * continue to exist for the duration of the call.
1280 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1281 if ((pr->pr_flags & PR_VNET) &&
1282 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1285 "vnet jails cannot have IP address restrictions");
1286 goto done_deref_locked;
1290 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1293 "ip4 cannot be changed after creation");
1294 goto done_deref_locked;
1298 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1301 "ip6 cannot be changed after creation");
1302 goto done_deref_locked;
1307 /* Do final error checking before setting anything. */
1309 if (slevel < ppr->pr_securelevel) {
1311 goto done_deref_locked;
1315 if (childmax >= ppr->pr_childmax) {
1317 goto done_deref_locked;
1321 if (enforce < ppr->pr_enforce_statfs) {
1323 goto done_deref_locked;
1328 if (ppr->pr_flags & PR_IP4) {
1330 * Make sure the new set of IP addresses is a
1331 * subset of the parent's list. Don't worry
1332 * about the parent being unlocked, as any
1333 * setting is done with allprison_lock held.
1335 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1336 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1338 if (ij == ppr->pr_ip4s) {
1340 goto done_deref_locked;
1343 for (ii = ij = 1; ii < ip4s; ii++) {
1344 if (ip4[ii].s_addr ==
1345 ppr->pr_ip4[0].s_addr)
1347 for (; ij < ppr->pr_ip4s; ij++)
1348 if (ip4[ii].s_addr ==
1349 ppr->pr_ip4[ij].s_addr)
1351 if (ij == ppr->pr_ip4s)
1354 if (ij == ppr->pr_ip4s) {
1356 goto done_deref_locked;
1361 * Check for conflicting IP addresses. We permit them
1362 * if there is no more than one IP on each jail. If
1363 * there is a duplicate on a jail with more than one
1364 * IP stop checking and return error.
1368 for (; tppr != &prison0; tppr = tppr->pr_parent)
1369 if (tppr->pr_flags & PR_VNET)
1372 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1375 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1377 tpr->pr_uref == 0) {
1381 if (!(tpr->pr_flags & PR_IP4_USER))
1384 if (tpr->pr_ip4 == NULL ||
1385 (ip4s == 1 && tpr->pr_ip4s == 1))
1387 for (ii = 0; ii < ip4s; ii++) {
1388 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1391 "IPv4 addresses clash");
1392 goto done_deref_locked;
1400 if (ppr->pr_flags & PR_IP6) {
1402 * Make sure the new set of IP addresses is a
1403 * subset of the parent's list.
1405 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1406 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1409 if (ij == ppr->pr_ip6s) {
1411 goto done_deref_locked;
1414 for (ii = ij = 1; ii < ip6s; ii++) {
1415 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1418 for (; ij < ppr->pr_ip6s; ij++)
1419 if (IN6_ARE_ADDR_EQUAL(
1420 &ip6[ii], &ppr->pr_ip6[ij]))
1422 if (ij == ppr->pr_ip6s)
1425 if (ij == ppr->pr_ip6s) {
1427 goto done_deref_locked;
1431 /* Check for conflicting IP addresses. */
1434 for (; tppr != &prison0; tppr = tppr->pr_parent)
1435 if (tppr->pr_flags & PR_VNET)
1438 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1441 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1443 tpr->pr_uref == 0) {
1447 if (!(tpr->pr_flags & PR_IP6_USER))
1450 if (tpr->pr_ip6 == NULL ||
1451 (ip6s == 1 && tpr->pr_ip6s == 1))
1453 for (ii = 0; ii < ip6s; ii++) {
1454 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1457 "IPv6 addresses clash");
1458 goto done_deref_locked;
1464 onamelen = namelen = 0;
1466 /* Give a default name of the jid. */
1467 if (name[0] == '\0')
1468 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1469 else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1473 "name cannot be numeric (unless it is the jid)");
1474 goto done_deref_locked;
1477 * Make sure the name isn't too long for the prison or its
1480 onamelen = strlen(pr->pr_name);
1481 namelen = strlen(name);
1482 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1483 error = ENAMETOOLONG;
1484 goto done_deref_locked;
1486 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1487 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1488 sizeof(pr->pr_name)) {
1489 error = ENAMETOOLONG;
1490 goto done_deref_locked;
1494 if (pr_allow & ~ppr->pr_allow) {
1496 goto done_deref_locked;
1499 /* Set the parameters of the prison. */
1502 if (pr_flags & PR_IP4_USER) {
1503 pr->pr_flags |= PR_IP4;
1504 free(pr->pr_ip4, M_PRISON);
1508 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1510 if (tpr->pr_flags & PR_VNET) {
1515 if (prison_restrict_ip4(tpr, NULL)) {
1524 if (pr_flags & PR_IP6_USER) {
1525 pr->pr_flags |= PR_IP6;
1526 free(pr->pr_ip6, M_PRISON);
1530 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1532 if (tpr->pr_flags & PR_VNET) {
1537 if (prison_restrict_ip6(tpr, NULL)) {
1545 pr->pr_securelevel = slevel;
1546 /* Set all child jails to be at least this level. */
1547 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1548 if (tpr->pr_securelevel < slevel)
1549 tpr->pr_securelevel = slevel;
1552 pr->pr_childmax = childmax;
1553 /* Set all child jails to under this limit. */
1554 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1555 if (tpr->pr_childmax > childmax - level)
1556 tpr->pr_childmax = childmax > level
1557 ? childmax - level : 0;
1560 pr->pr_enforce_statfs = enforce;
1561 /* Pass this restriction on to the children. */
1562 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1563 if (tpr->pr_enforce_statfs < enforce)
1564 tpr->pr_enforce_statfs = enforce;
1567 if (ppr == &prison0)
1568 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1570 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1571 ppr->pr_name, name);
1572 /* Change this component of child names. */
1573 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1574 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1575 strlen(tpr->pr_name + onamelen) + 1);
1576 bcopy(pr->pr_name, tpr->pr_name, namelen);
1580 /* Try to keep a real-rooted full pathname. */
1581 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1582 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1583 mypr->pr_path, path);
1585 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1588 if (PR_HOST & ch_flags & ~pr_flags) {
1589 if (pr->pr_flags & PR_HOST) {
1591 * Copy the parent's host info. As with pr_ip4 above,
1592 * the lack of a lock on the parent is not a problem;
1593 * it is always set with allprison_lock at least
1594 * shared, and is held exclusively here.
1596 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1597 sizeof(pr->pr_hostname));
1598 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1599 sizeof(pr->pr_domainname));
1600 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1601 sizeof(pr->pr_hostuuid));
1602 pr->pr_hostid = pr->pr_parent->pr_hostid;
1604 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1605 /* Set this prison, and any descendants without PR_HOST. */
1607 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1609 strlcpy(pr->pr_domainname, domain,
1610 sizeof(pr->pr_domainname));
1612 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1614 pr->pr_hostid = hid;
1615 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1616 if (tpr->pr_flags & PR_HOST)
1620 strlcpy(tpr->pr_hostname,
1622 sizeof(tpr->pr_hostname));
1624 strlcpy(tpr->pr_domainname,
1626 sizeof(tpr->pr_domainname));
1628 strlcpy(tpr->pr_hostuuid,
1630 sizeof(tpr->pr_hostuuid));
1632 tpr->pr_hostid = hid;
1636 if ((tallow = ch_allow & ~pr_allow)) {
1637 /* Clear allow bits in all children. */
1638 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1639 tpr->pr_allow &= ~tallow;
1641 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1643 * Persistent prisons get an extra reference, and prisons losing their
1644 * persist flag lose that reference. Only do this for existing prisons
1645 * for now, so new ones will remain unseen until after the module
1646 * handlers have completed.
1648 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1649 if (pr_flags & PR_PERSIST) {
1657 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1658 mtx_unlock(&pr->pr_mtx);
1660 /* Locks may have prevented a complete restriction of child IP
1661 * addresses. If so, allocate some more memory and try again.
1666 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1667 mtx_lock(&pr->pr_mtx);
1669 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1671 if (tpr->pr_flags & PR_VNET) {
1676 if (prison_restrict_ip4(tpr, ip4)) {
1683 mtx_unlock(&pr->pr_mtx);
1689 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1690 mtx_lock(&pr->pr_mtx);
1692 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1694 if (tpr->pr_flags & PR_VNET) {
1699 if (prison_restrict_ip6(tpr, ip6)) {
1706 mtx_unlock(&pr->pr_mtx);
1710 /* Let the modules do their work. */
1711 sx_downgrade(&allprison_lock);
1713 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1715 prison_deref(pr, PD_LIST_SLOCKED);
1719 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1721 prison_deref(pr, created
1723 : PD_DEREF | PD_LIST_SLOCKED);
1727 /* Attach this process to the prison if requested. */
1728 if (flags & JAIL_ATTACH) {
1729 mtx_lock(&pr->pr_mtx);
1730 error = do_jail_attach(td, pr);
1732 vfs_opterror(opts, "attach failed");
1734 prison_deref(pr, PD_DEREF);
1740 * Now that it is all there, drop the temporary reference from existing
1741 * prisons. Or add a reference to newly created persistent prisons
1742 * (which was not done earlier so that the prison would not be publicly
1746 prison_deref(pr, (flags & JAIL_ATTACH)
1748 : PD_DEREF | PD_LIST_SLOCKED);
1750 if (pr_flags & PR_PERSIST) {
1751 mtx_lock(&pr->pr_mtx);
1754 mtx_unlock(&pr->pr_mtx);
1756 if (!(flags & JAIL_ATTACH))
1757 sx_sunlock(&allprison_lock);
1759 td->td_retval[0] = pr->pr_id;
1763 prison_deref(pr, created
1764 ? PD_LOCKED | PD_LIST_XLOCKED
1765 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1768 sx_xunlock(&allprison_lock);
1771 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1773 VFS_UNLOCK_GIANT(vfslocked);
1777 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1778 if (errmsg_len > 0) {
1779 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1780 if (errmsg_pos > 0) {
1781 if (optuio->uio_segflg == UIO_SYSSPACE)
1783 optuio->uio_iov[errmsg_pos].iov_base,
1787 optuio->uio_iov[errmsg_pos].iov_base,
1794 free(ip4, M_PRISON);
1797 free(ip6, M_PRISON);
1805 * struct jail_get_args {
1806 * struct iovec *iovp;
1807 * unsigned int iovcnt;
1812 jail_get(struct thread *td, struct jail_get_args *uap)
1817 /* Check that we have an even number of iovecs. */
1818 if (uap->iovcnt & 1)
1821 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1824 error = kern_jail_get(td, auio, uap->flags);
1826 error = copyout(auio->uio_iov, uap->iovp,
1827 uap->iovcnt * sizeof (struct iovec));
1833 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1835 struct prison *pr, *mypr;
1837 struct vfsoptlist *opts;
1838 char *errmsg, *name;
1839 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1841 if (flags & ~JAIL_GET_MASK)
1844 /* Get the parameter list. */
1845 error = vfs_buildopts(optuio, &opts);
1848 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1849 mypr = td->td_ucred->cr_prison;
1852 * Find the prison specified by one of: lastjid, jid, name.
1854 sx_slock(&allprison_lock);
1855 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1857 TAILQ_FOREACH(pr, &allprison, pr_list) {
1858 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1859 mtx_lock(&pr->pr_mtx);
1860 if (pr->pr_ref > 0 &&
1861 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1863 mtx_unlock(&pr->pr_mtx);
1869 vfs_opterror(opts, "no jail after %d", jid);
1870 goto done_unlock_list;
1871 } else if (error != ENOENT)
1872 goto done_unlock_list;
1874 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1877 pr = prison_find_child(mypr, jid);
1879 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1880 mtx_unlock(&pr->pr_mtx);
1882 vfs_opterror(opts, "jail %d is dying",
1884 goto done_unlock_list;
1889 vfs_opterror(opts, "jail %d not found", jid);
1890 goto done_unlock_list;
1892 } else if (error != ENOENT)
1893 goto done_unlock_list;
1895 error = vfs_getopt(opts, "name", (void **)&name, &len);
1897 if (len == 0 || name[len - 1] != '\0') {
1899 goto done_unlock_list;
1901 pr = prison_find_name(mypr, name);
1903 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1904 mtx_unlock(&pr->pr_mtx);
1906 vfs_opterror(opts, "jail \"%s\" is dying",
1908 goto done_unlock_list;
1913 vfs_opterror(opts, "jail \"%s\" not found", name);
1914 goto done_unlock_list;
1915 } else if (error != ENOENT)
1916 goto done_unlock_list;
1918 vfs_opterror(opts, "no jail specified");
1920 goto done_unlock_list;
1923 /* Get the parameters of the prison. */
1926 td->td_retval[0] = pr->pr_id;
1927 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1928 if (error != 0 && error != ENOENT)
1930 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1931 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1932 if (error != 0 && error != ENOENT)
1934 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1935 if (error != 0 && error != ENOENT)
1937 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1938 sizeof(pr->pr_cpuset->cs_id));
1939 if (error != 0 && error != ENOENT)
1941 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1942 if (error != 0 && error != ENOENT)
1945 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1946 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1947 if (error != 0 && error != ENOENT)
1951 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1952 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1953 if (error != 0 && error != ENOENT)
1956 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1957 sizeof(pr->pr_securelevel));
1958 if (error != 0 && error != ENOENT)
1960 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1961 sizeof(pr->pr_childcount));
1962 if (error != 0 && error != ENOENT)
1964 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1965 sizeof(pr->pr_childmax));
1966 if (error != 0 && error != ENOENT)
1968 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1969 if (error != 0 && error != ENOENT)
1971 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1972 if (error != 0 && error != ENOENT)
1974 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1975 if (error != 0 && error != ENOENT)
1977 #ifdef COMPAT_FREEBSD32
1978 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1979 uint32_t hid32 = pr->pr_hostid;
1981 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1984 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1985 sizeof(pr->pr_hostid));
1986 if (error != 0 && error != ENOENT)
1988 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1989 sizeof(pr->pr_enforce_statfs));
1990 if (error != 0 && error != ENOENT)
1992 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1994 if (pr_flag_names[fi] == NULL)
1996 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1997 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1998 if (error != 0 && error != ENOENT)
2001 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
2002 if (error != 0 && error != ENOENT)
2005 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
2008 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
2009 i = pr_flag_jailsys[fi].disable &&
2010 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2011 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2014 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2015 if (error != 0 && error != ENOENT)
2018 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2020 if (pr_allow_names[fi] == NULL)
2022 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2023 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2024 if (error != 0 && error != ENOENT)
2027 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2028 if (error != 0 && error != ENOENT)
2031 i = (pr->pr_uref == 0);
2032 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2033 if (error != 0 && error != ENOENT)
2036 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2037 if (error != 0 && error != ENOENT)
2040 /* Get the module parameters. */
2041 mtx_unlock(&pr->pr_mtx);
2043 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2046 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2048 /* By now, all parameters should have been noted. */
2049 TAILQ_FOREACH(opt, opts, link) {
2050 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2052 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2057 /* Write the fetched parameters back to userspace. */
2059 TAILQ_FOREACH(opt, opts, link) {
2060 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2061 pos = 2 * opt->pos + 1;
2062 optuio->uio_iov[pos].iov_len = opt->len;
2063 if (opt->value != NULL) {
2064 if (optuio->uio_segflg == UIO_SYSSPACE) {
2066 optuio->uio_iov[pos].iov_base,
2069 error = copyout(opt->value,
2070 optuio->uio_iov[pos].iov_base,
2081 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2085 sx_sunlock(&allprison_lock);
2087 if (error && errmsg_pos >= 0) {
2088 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2089 errmsg_pos = 2 * errmsg_pos + 1;
2090 if (errmsg_len > 0) {
2091 if (optuio->uio_segflg == UIO_SYSSPACE)
2093 optuio->uio_iov[errmsg_pos].iov_base,
2097 optuio->uio_iov[errmsg_pos].iov_base,
2107 * struct jail_remove_args {
2112 jail_remove(struct thread *td, struct jail_remove_args *uap)
2114 struct prison *pr, *cpr, *lpr, *tpr;
2117 error = priv_check(td, PRIV_JAIL_REMOVE);
2121 sx_xlock(&allprison_lock);
2122 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2124 sx_xunlock(&allprison_lock);
2128 /* Remove all descendants of this prison, then remove this prison. */
2130 pr->pr_flags |= PR_REMOVE;
2131 if (!LIST_EMPTY(&pr->pr_children)) {
2132 mtx_unlock(&pr->pr_mtx);
2134 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2135 mtx_lock(&cpr->pr_mtx);
2136 if (cpr->pr_ref > 0) {
2139 cpr->pr_flags |= PR_REMOVE;
2141 /* Already removed - do not do it again. */
2144 mtx_unlock(&cpr->pr_mtx);
2146 mtx_lock(&lpr->pr_mtx);
2147 prison_remove_one(lpr);
2148 sx_xlock(&allprison_lock);
2153 mtx_lock(&lpr->pr_mtx);
2154 prison_remove_one(lpr);
2155 sx_xlock(&allprison_lock);
2157 mtx_lock(&pr->pr_mtx);
2159 prison_remove_one(pr);
2164 prison_remove_one(struct prison *pr)
2169 /* If the prison was persistent, it is not anymore. */
2171 if (pr->pr_flags & PR_PERSIST) {
2174 pr->pr_flags &= ~PR_PERSIST;
2178 * jail_remove added a reference. If that's the only one, remove
2181 KASSERT(pr->pr_ref > 0,
2182 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2183 if (pr->pr_ref == 1) {
2185 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2189 mtx_unlock(&pr->pr_mtx);
2190 sx_xunlock(&allprison_lock);
2192 * Kill all processes unfortunate enough to be attached to this prison.
2194 sx_slock(&allproc_lock);
2195 LIST_FOREACH(p, &allproc, p_list) {
2197 if (p->p_state != PRS_NEW && p->p_ucred &&
2198 p->p_ucred->cr_prison == pr)
2199 psignal(p, SIGKILL);
2202 sx_sunlock(&allproc_lock);
2203 /* Remove the temporary reference added by jail_remove. */
2204 prison_deref(pr, deuref | PD_DEREF);
2209 * struct jail_attach_args {
2214 jail_attach(struct thread *td, struct jail_attach_args *uap)
2219 error = priv_check(td, PRIV_JAIL_ATTACH);
2223 sx_slock(&allprison_lock);
2224 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2226 sx_sunlock(&allprison_lock);
2231 * Do not allow a process to attach to a prison that is not
2232 * considered to be "alive".
2234 if (pr->pr_uref == 0) {
2235 mtx_unlock(&pr->pr_mtx);
2236 sx_sunlock(&allprison_lock);
2240 return (do_jail_attach(td, pr));
2244 do_jail_attach(struct thread *td, struct prison *pr)
2248 struct ucred *newcred, *oldcred;
2249 int vfslocked, error;
2252 * XXX: Note that there is a slight race here if two threads
2253 * in the same privileged process attempt to attach to two
2254 * different jails at the same time. It is important for
2255 * user processes not to do this, or they might end up with
2256 * a process root from one prison, but attached to the jail
2261 mtx_unlock(&pr->pr_mtx);
2263 /* Let modules do whatever they need to prepare for attaching. */
2264 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2266 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2269 sx_sunlock(&allprison_lock);
2272 * Reparent the newly attached process to this jail.
2274 ppr = td->td_ucred->cr_prison;
2276 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2280 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2281 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2282 if ((error = change_dir(pr->pr_root, td)) != 0)
2285 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2288 VOP_UNLOCK(pr->pr_root, 0);
2289 if ((error = change_root(pr->pr_root, td)))
2290 goto e_unlock_giant;
2291 VFS_UNLOCK_GIANT(vfslocked);
2295 oldcred = p->p_ucred;
2297 crcopy(newcred, oldcred);
2298 newcred->cr_prison = pr;
2299 p->p_ucred = newcred;
2302 racct_proc_ucred_changed(p, oldcred, newcred);
2305 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2308 VOP_UNLOCK(pr->pr_root, 0);
2310 VFS_UNLOCK_GIANT(vfslocked);
2312 /* Tell modules this thread is still in its old jail after all. */
2313 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2314 prison_deref(pr, PD_DEREF | PD_DEUREF);
2320 * Returns a locked prison instance, or NULL on failure.
2323 prison_find(int prid)
2327 sx_assert(&allprison_lock, SX_LOCKED);
2328 TAILQ_FOREACH(pr, &allprison, pr_list) {
2329 if (pr->pr_id == prid) {
2330 mtx_lock(&pr->pr_mtx);
2333 mtx_unlock(&pr->pr_mtx);
2340 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2343 prison_find_child(struct prison *mypr, int prid)
2348 sx_assert(&allprison_lock, SX_LOCKED);
2349 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2350 if (pr->pr_id == prid) {
2351 mtx_lock(&pr->pr_mtx);
2354 mtx_unlock(&pr->pr_mtx);
2361 * Look for the name relative to mypr. Returns a locked prison or NULL.
2364 prison_find_name(struct prison *mypr, const char *name)
2366 struct prison *pr, *deadpr;
2370 sx_assert(&allprison_lock, SX_LOCKED);
2371 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2374 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2375 if (!strcmp(pr->pr_name + mylen, name)) {
2376 mtx_lock(&pr->pr_mtx);
2377 if (pr->pr_ref > 0) {
2378 if (pr->pr_uref > 0)
2382 mtx_unlock(&pr->pr_mtx);
2385 /* There was no valid prison - perhaps there was a dying one. */
2386 if (deadpr != NULL) {
2387 mtx_lock(&deadpr->pr_mtx);
2388 if (deadpr->pr_ref == 0) {
2389 mtx_unlock(&deadpr->pr_mtx);
2397 * See if a prison has the specific flag set.
2400 prison_flag(struct ucred *cred, unsigned flag)
2403 /* This is an atomic read, so no locking is necessary. */
2404 return (cred->cr_prison->pr_flags & flag);
2408 prison_allow(struct ucred *cred, unsigned flag)
2411 /* This is an atomic read, so no locking is necessary. */
2412 return (cred->cr_prison->pr_allow & flag);
2416 * Remove a prison reference. If that was the last reference, remove the
2417 * prison itself - but not in this context in case there are locks held.
2420 prison_free_locked(struct prison *pr)
2423 mtx_assert(&pr->pr_mtx, MA_OWNED);
2425 if (pr->pr_ref == 0) {
2426 mtx_unlock(&pr->pr_mtx);
2427 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2428 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2431 mtx_unlock(&pr->pr_mtx);
2435 prison_free(struct prison *pr)
2438 mtx_lock(&pr->pr_mtx);
2439 prison_free_locked(pr);
2443 prison_complete(void *context, int pending)
2446 prison_deref((struct prison *)context, 0);
2450 * Remove a prison reference (usually). This internal version assumes no
2451 * mutexes are held, except perhaps the prison itself. If there are no more
2452 * references, release and delist the prison. On completion, the prison lock
2453 * and the allprison lock are both unlocked.
2456 prison_deref(struct prison *pr, int flags)
2458 struct prison *ppr, *tpr;
2461 if (!(flags & PD_LOCKED))
2462 mtx_lock(&pr->pr_mtx);
2463 /* Decrement the user references in a separate loop. */
2464 if (flags & PD_DEUREF) {
2465 for (tpr = pr;; tpr = tpr->pr_parent) {
2467 mtx_lock(&tpr->pr_mtx);
2468 if (--tpr->pr_uref > 0)
2470 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2471 mtx_unlock(&tpr->pr_mtx);
2473 /* Done if there were only user references to remove. */
2474 if (!(flags & PD_DEREF)) {
2475 mtx_unlock(&tpr->pr_mtx);
2476 if (flags & PD_LIST_SLOCKED)
2477 sx_sunlock(&allprison_lock);
2478 else if (flags & PD_LIST_XLOCKED)
2479 sx_xunlock(&allprison_lock);
2483 mtx_unlock(&tpr->pr_mtx);
2484 mtx_lock(&pr->pr_mtx);
2489 if (flags & PD_DEREF)
2491 /* If the prison still has references, nothing else to do. */
2492 if (pr->pr_ref > 0) {
2493 mtx_unlock(&pr->pr_mtx);
2494 if (flags & PD_LIST_SLOCKED)
2495 sx_sunlock(&allprison_lock);
2496 else if (flags & PD_LIST_XLOCKED)
2497 sx_xunlock(&allprison_lock);
2501 mtx_unlock(&pr->pr_mtx);
2502 if (flags & PD_LIST_SLOCKED) {
2503 if (!sx_try_upgrade(&allprison_lock)) {
2504 sx_sunlock(&allprison_lock);
2505 sx_xlock(&allprison_lock);
2507 } else if (!(flags & PD_LIST_XLOCKED))
2508 sx_xlock(&allprison_lock);
2510 TAILQ_REMOVE(&allprison, pr, pr_list);
2511 LIST_REMOVE(pr, pr_sibling);
2512 ppr = pr->pr_parent;
2513 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2514 tpr->pr_childcount--;
2515 sx_xunlock(&allprison_lock);
2518 if (pr->pr_vnet != ppr->pr_vnet)
2519 vnet_destroy(pr->pr_vnet);
2521 if (pr->pr_root != NULL) {
2522 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2524 VFS_UNLOCK_GIANT(vfslocked);
2526 mtx_destroy(&pr->pr_mtx);
2528 free(pr->pr_ip4, M_PRISON);
2531 free(pr->pr_ip6, M_PRISON);
2533 if (pr->pr_cpuset != NULL)
2534 cpuset_rel(pr->pr_cpuset);
2537 rctl_racct_release(pr->pr_racct);
2539 racct_destroy(&pr->pr_racct);
2542 /* Removing a prison frees a reference on its parent. */
2544 mtx_lock(&pr->pr_mtx);
2550 prison_hold_locked(struct prison *pr)
2553 mtx_assert(&pr->pr_mtx, MA_OWNED);
2554 KASSERT(pr->pr_ref > 0,
2555 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2560 prison_hold(struct prison *pr)
2563 mtx_lock(&pr->pr_mtx);
2564 prison_hold_locked(pr);
2565 mtx_unlock(&pr->pr_mtx);
2569 prison_proc_hold(struct prison *pr)
2572 mtx_lock(&pr->pr_mtx);
2573 KASSERT(pr->pr_uref > 0,
2574 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2576 mtx_unlock(&pr->pr_mtx);
2580 prison_proc_free(struct prison *pr)
2583 mtx_lock(&pr->pr_mtx);
2584 KASSERT(pr->pr_uref > 0,
2585 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2586 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2592 * Restrict a prison's IP address list with its parent's, possibly replacing
2593 * it. Return true if the replacement buffer was used (or would have been).
2596 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2601 ppr = pr->pr_parent;
2602 if (!(pr->pr_flags & PR_IP4_USER)) {
2603 /* This has no user settings, so just copy the parent's list. */
2604 if (pr->pr_ip4s < ppr->pr_ip4s) {
2606 * There's no room for the parent's list. Use the
2607 * new list buffer, which is assumed to be big enough
2608 * (if it was passed). If there's no buffer, try to
2612 if (newip4 == NULL) {
2613 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2614 M_PRISON, M_NOWAIT);
2618 if (newip4 != NULL) {
2619 bcopy(ppr->pr_ip4, newip4,
2620 ppr->pr_ip4s * sizeof(*newip4));
2621 free(pr->pr_ip4, M_PRISON);
2622 pr->pr_ip4 = newip4;
2623 pr->pr_ip4s = ppr->pr_ip4s;
2627 pr->pr_ip4s = ppr->pr_ip4s;
2628 if (pr->pr_ip4s > 0)
2629 bcopy(ppr->pr_ip4, pr->pr_ip4,
2630 pr->pr_ip4s * sizeof(*newip4));
2631 else if (pr->pr_ip4 != NULL) {
2632 free(pr->pr_ip4, M_PRISON);
2635 } else if (pr->pr_ip4s > 0) {
2636 /* Remove addresses that aren't in the parent. */
2637 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2638 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2640 if (ij < ppr->pr_ip4s)
2643 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2644 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2647 for (ij = 1; ii < pr->pr_ip4s; ) {
2648 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2652 switch (ij >= ppr->pr_ip4s ? -1 :
2653 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2655 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2656 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2667 if (pr->pr_ip4s == 0) {
2668 pr->pr_flags |= PR_IP4_DISABLE;
2669 free(pr->pr_ip4, M_PRISON);
2677 * Pass back primary IPv4 address of this jail.
2679 * If not restricted return success but do not alter the address. Caller has
2680 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2682 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2683 * Address returned in NBO.
2686 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2690 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2691 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2693 pr = cred->cr_prison;
2694 if (!(pr->pr_flags & PR_IP4))
2696 mtx_lock(&pr->pr_mtx);
2697 if (!(pr->pr_flags & PR_IP4)) {
2698 mtx_unlock(&pr->pr_mtx);
2701 if (pr->pr_ip4 == NULL) {
2702 mtx_unlock(&pr->pr_mtx);
2703 return (EAFNOSUPPORT);
2706 ia->s_addr = pr->pr_ip4[0].s_addr;
2707 mtx_unlock(&pr->pr_mtx);
2712 * Return 1 if we should do proper source address selection or are not jailed.
2713 * We will return 0 if we should bypass source address selection in favour
2714 * of the primary jail IPv4 address. Only in this case *ia will be updated and
2716 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2719 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2725 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2726 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2731 pr = cred->cr_prison;
2732 if (pr->pr_flags & PR_IP4_SADDRSEL)
2735 lia.s_addr = INADDR_ANY;
2736 error = prison_get_ip4(cred, &lia);
2739 if (lia.s_addr == INADDR_ANY)
2742 ia->s_addr = lia.s_addr;
2747 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2750 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2757 * No need to lock since the PR_IP4_USER flag can't be altered for
2760 while (pr1 != &prison0 &&
2762 !(pr1->pr_flags & PR_VNET) &&
2764 !(pr1->pr_flags & PR_IP4_USER))
2765 pr1 = pr1->pr_parent;
2766 while (pr2 != &prison0 &&
2768 !(pr2->pr_flags & PR_VNET) &&
2770 !(pr2->pr_flags & PR_IP4_USER))
2771 pr2 = pr2->pr_parent;
2772 return (pr1 == pr2);
2776 * Make sure our (source) address is set to something meaningful to this
2779 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2780 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2781 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2784 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2790 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2791 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2793 pr = cred->cr_prison;
2794 if (!(pr->pr_flags & PR_IP4))
2796 mtx_lock(&pr->pr_mtx);
2797 if (!(pr->pr_flags & PR_IP4)) {
2798 mtx_unlock(&pr->pr_mtx);
2801 if (pr->pr_ip4 == NULL) {
2802 mtx_unlock(&pr->pr_mtx);
2803 return (EAFNOSUPPORT);
2806 ia0.s_addr = ntohl(ia->s_addr);
2807 if (ia0.s_addr == INADDR_LOOPBACK) {
2808 ia->s_addr = pr->pr_ip4[0].s_addr;
2809 mtx_unlock(&pr->pr_mtx);
2813 if (ia0.s_addr == INADDR_ANY) {
2815 * In case there is only 1 IPv4 address, bind directly.
2817 if (pr->pr_ip4s == 1)
2818 ia->s_addr = pr->pr_ip4[0].s_addr;
2819 mtx_unlock(&pr->pr_mtx);
2823 error = _prison_check_ip4(pr, ia);
2824 mtx_unlock(&pr->pr_mtx);
2829 * Rewrite destination address in case we will connect to loopback address.
2831 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2832 * Address passed in in NBO and returned in NBO.
2835 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2839 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2840 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2842 pr = cred->cr_prison;
2843 if (!(pr->pr_flags & PR_IP4))
2845 mtx_lock(&pr->pr_mtx);
2846 if (!(pr->pr_flags & PR_IP4)) {
2847 mtx_unlock(&pr->pr_mtx);
2850 if (pr->pr_ip4 == NULL) {
2851 mtx_unlock(&pr->pr_mtx);
2852 return (EAFNOSUPPORT);
2855 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2856 ia->s_addr = pr->pr_ip4[0].s_addr;
2857 mtx_unlock(&pr->pr_mtx);
2862 * Return success because nothing had to be changed.
2864 mtx_unlock(&pr->pr_mtx);
2869 * Check if given address belongs to the jail referenced by cred/prison.
2871 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2872 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2873 * doesn't allow IPv4. Address passed in in NBO.
2876 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2881 * Check the primary IP.
2883 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2887 * All the other IPs are sorted so we can do a binary search.
2890 z = pr->pr_ip4s - 2;
2893 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2902 return (EADDRNOTAVAIL);
2906 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2911 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2912 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2914 pr = cred->cr_prison;
2915 if (!(pr->pr_flags & PR_IP4))
2917 mtx_lock(&pr->pr_mtx);
2918 if (!(pr->pr_flags & PR_IP4)) {
2919 mtx_unlock(&pr->pr_mtx);
2922 if (pr->pr_ip4 == NULL) {
2923 mtx_unlock(&pr->pr_mtx);
2924 return (EAFNOSUPPORT);
2927 error = _prison_check_ip4(pr, ia);
2928 mtx_unlock(&pr->pr_mtx);
2935 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2940 ppr = pr->pr_parent;
2941 if (!(pr->pr_flags & PR_IP6_USER)) {
2942 /* This has no user settings, so just copy the parent's list. */
2943 if (pr->pr_ip6s < ppr->pr_ip6s) {
2945 * There's no room for the parent's list. Use the
2946 * new list buffer, which is assumed to be big enough
2947 * (if it was passed). If there's no buffer, try to
2951 if (newip6 == NULL) {
2952 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2953 M_PRISON, M_NOWAIT);
2957 if (newip6 != NULL) {
2958 bcopy(ppr->pr_ip6, newip6,
2959 ppr->pr_ip6s * sizeof(*newip6));
2960 free(pr->pr_ip6, M_PRISON);
2961 pr->pr_ip6 = newip6;
2962 pr->pr_ip6s = ppr->pr_ip6s;
2966 pr->pr_ip6s = ppr->pr_ip6s;
2967 if (pr->pr_ip6s > 0)
2968 bcopy(ppr->pr_ip6, pr->pr_ip6,
2969 pr->pr_ip6s * sizeof(*newip6));
2970 else if (pr->pr_ip6 != NULL) {
2971 free(pr->pr_ip6, M_PRISON);
2974 } else if (pr->pr_ip6s > 0) {
2975 /* Remove addresses that aren't in the parent. */
2976 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2977 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2980 if (ij < ppr->pr_ip6s)
2983 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2984 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2987 for (ij = 1; ii < pr->pr_ip6s; ) {
2988 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2993 switch (ij >= ppr->pr_ip4s ? -1 :
2994 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2996 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2997 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
3008 if (pr->pr_ip6s == 0) {
3009 pr->pr_flags |= PR_IP6_DISABLE;
3010 free(pr->pr_ip6, M_PRISON);
3018 * Pass back primary IPv6 address for this jail.
3020 * If not restricted return success but do not alter the address. Caller has
3021 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3023 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3026 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3030 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3031 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3033 pr = cred->cr_prison;
3034 if (!(pr->pr_flags & PR_IP6))
3036 mtx_lock(&pr->pr_mtx);
3037 if (!(pr->pr_flags & PR_IP6)) {
3038 mtx_unlock(&pr->pr_mtx);
3041 if (pr->pr_ip6 == NULL) {
3042 mtx_unlock(&pr->pr_mtx);
3043 return (EAFNOSUPPORT);
3046 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3047 mtx_unlock(&pr->pr_mtx);
3052 * Return 1 if we should do proper source address selection or are not jailed.
3053 * We will return 0 if we should bypass source address selection in favour
3054 * of the primary jail IPv6 address. Only in this case *ia will be updated and
3056 * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3059 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3062 struct in6_addr lia6;
3065 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3066 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3071 pr = cred->cr_prison;
3072 if (pr->pr_flags & PR_IP6_SADDRSEL)
3076 error = prison_get_ip6(cred, &lia6);
3079 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3082 bcopy(&lia6, ia6, sizeof(struct in6_addr));
3087 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3090 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
3096 while (pr1 != &prison0 &&
3098 !(pr1->pr_flags & PR_VNET) &&
3100 !(pr1->pr_flags & PR_IP6_USER))
3101 pr1 = pr1->pr_parent;
3102 while (pr2 != &prison0 &&
3104 !(pr2->pr_flags & PR_VNET) &&
3106 !(pr2->pr_flags & PR_IP6_USER))
3107 pr2 = pr2->pr_parent;
3108 return (pr1 == pr2);
3112 * Make sure our (source) address is set to something meaningful to this jail.
3114 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3115 * when needed while binding.
3117 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3118 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3119 * doesn't allow IPv6.
3122 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3127 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3128 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3130 pr = cred->cr_prison;
3131 if (!(pr->pr_flags & PR_IP6))
3133 mtx_lock(&pr->pr_mtx);
3134 if (!(pr->pr_flags & PR_IP6)) {
3135 mtx_unlock(&pr->pr_mtx);
3138 if (pr->pr_ip6 == NULL) {
3139 mtx_unlock(&pr->pr_mtx);
3140 return (EAFNOSUPPORT);
3143 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3144 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3145 mtx_unlock(&pr->pr_mtx);
3149 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3151 * In case there is only 1 IPv6 address, and v6only is true,
3152 * then bind directly.
3154 if (v6only != 0 && pr->pr_ip6s == 1)
3155 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3156 mtx_unlock(&pr->pr_mtx);
3160 error = _prison_check_ip6(pr, ia6);
3161 mtx_unlock(&pr->pr_mtx);
3166 * Rewrite destination address in case we will connect to loopback address.
3168 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3171 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3175 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3176 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3178 pr = cred->cr_prison;
3179 if (!(pr->pr_flags & PR_IP6))
3181 mtx_lock(&pr->pr_mtx);
3182 if (!(pr->pr_flags & PR_IP6)) {
3183 mtx_unlock(&pr->pr_mtx);
3186 if (pr->pr_ip6 == NULL) {
3187 mtx_unlock(&pr->pr_mtx);
3188 return (EAFNOSUPPORT);
3191 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3192 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3193 mtx_unlock(&pr->pr_mtx);
3198 * Return success because nothing had to be changed.
3200 mtx_unlock(&pr->pr_mtx);
3205 * Check if given address belongs to the jail referenced by cred/prison.
3207 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3208 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3209 * doesn't allow IPv6.
3212 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3217 * Check the primary IP.
3219 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3223 * All the other IPs are sorted so we can do a binary search.
3226 z = pr->pr_ip6s - 2;
3229 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3238 return (EADDRNOTAVAIL);
3242 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3247 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3248 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3250 pr = cred->cr_prison;
3251 if (!(pr->pr_flags & PR_IP6))
3253 mtx_lock(&pr->pr_mtx);
3254 if (!(pr->pr_flags & PR_IP6)) {
3255 mtx_unlock(&pr->pr_mtx);
3258 if (pr->pr_ip6 == NULL) {
3259 mtx_unlock(&pr->pr_mtx);
3260 return (EAFNOSUPPORT);
3263 error = _prison_check_ip6(pr, ia6);
3264 mtx_unlock(&pr->pr_mtx);
3270 * Check if a jail supports the given address family.
3272 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3276 prison_check_af(struct ucred *cred, int af)
3281 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3283 pr = cred->cr_prison;
3285 /* Prisons with their own network stack are not limited. */
3286 if (prison_owns_vnet(cred))
3295 if (pr->pr_flags & PR_IP4)
3297 mtx_lock(&pr->pr_mtx);
3298 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3299 error = EAFNOSUPPORT;
3300 mtx_unlock(&pr->pr_mtx);
3306 if (pr->pr_flags & PR_IP6)
3308 mtx_lock(&pr->pr_mtx);
3309 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3310 error = EAFNOSUPPORT;
3311 mtx_unlock(&pr->pr_mtx);
3319 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3320 error = EAFNOSUPPORT;
3326 * Check if given address belongs to the jail referenced by cred (wrapper to
3327 * prison_check_ip[46]).
3329 * Returns 0 if jail doesn't restrict the address family or if address belongs
3330 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3331 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3334 prison_if(struct ucred *cred, struct sockaddr *sa)
3337 struct sockaddr_in *sai;
3340 struct sockaddr_in6 *sai6;
3344 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3345 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3348 if (prison_owns_vnet(cred))
3353 switch (sa->sa_family)
3357 sai = (struct sockaddr_in *)sa;
3358 error = prison_check_ip4(cred, &sai->sin_addr);
3363 sai6 = (struct sockaddr_in6 *)sa;
3364 error = prison_check_ip6(cred, &sai6->sin6_addr);
3368 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3369 error = EAFNOSUPPORT;
3375 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3378 prison_check(struct ucred *cred1, struct ucred *cred2)
3381 return ((cred1->cr_prison == cred2->cr_prison ||
3382 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3386 * Return 1 if p2 is a child of p1, otherwise 0.
3389 prison_ischild(struct prison *pr1, struct prison *pr2)
3392 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3399 * Return 1 if the passed credential is in a jail, otherwise 0.
3402 jailed(struct ucred *cred)
3405 return (cred->cr_prison != &prison0);
3409 * Return 1 if the passed credential is in a jail and that jail does not
3410 * have its own virtual network stack, otherwise 0.
3413 jailed_without_vnet(struct ucred *cred)
3419 if (prison_owns_vnet(cred))
3427 * Return the correct hostname (domainname, et al) for the passed credential.
3430 getcredhostname(struct ucred *cred, char *buf, size_t size)
3435 * A NULL credential can be used to shortcut to the physical
3436 * system's hostname.
3438 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3439 mtx_lock(&pr->pr_mtx);
3440 strlcpy(buf, pr->pr_hostname, size);
3441 mtx_unlock(&pr->pr_mtx);
3445 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3448 mtx_lock(&cred->cr_prison->pr_mtx);
3449 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3450 mtx_unlock(&cred->cr_prison->pr_mtx);
3454 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3457 mtx_lock(&cred->cr_prison->pr_mtx);
3458 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3459 mtx_unlock(&cred->cr_prison->pr_mtx);
3463 getcredhostid(struct ucred *cred, unsigned long *hostid)
3466 mtx_lock(&cred->cr_prison->pr_mtx);
3467 *hostid = cred->cr_prison->pr_hostid;
3468 mtx_unlock(&cred->cr_prison->pr_mtx);
3473 * Determine whether the prison represented by cred owns
3474 * its vnet rather than having it inherited.
3476 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3479 prison_owns_vnet(struct ucred *cred)
3483 * vnets cannot be added/removed after jail creation,
3484 * so no need to lock here.
3486 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3491 * Determine whether the subject represented by cred can "see"
3492 * status of a mount point.
3493 * Returns: 0 for permitted, ENOENT otherwise.
3494 * XXX: This function should be called cr_canseemount() and should be
3495 * placed in kern_prot.c.
3498 prison_canseemount(struct ucred *cred, struct mount *mp)
3504 pr = cred->cr_prison;
3505 if (pr->pr_enforce_statfs == 0)
3507 if (pr->pr_root->v_mount == mp)
3509 if (pr->pr_enforce_statfs == 2)
3512 * If jail's chroot directory is set to "/" we should be able to see
3513 * all mount-points from inside a jail.
3514 * This is ugly check, but this is the only situation when jail's
3515 * directory ends with '/'.
3517 if (strcmp(pr->pr_path, "/") == 0)
3519 len = strlen(pr->pr_path);
3521 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3524 * Be sure that we don't have situation where jail's root directory
3525 * is "/some/path" and mount point is "/some/pathpath".
3527 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3533 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3535 char jpath[MAXPATHLEN];
3539 pr = cred->cr_prison;
3540 if (pr->pr_enforce_statfs == 0)
3542 if (prison_canseemount(cred, mp) != 0) {
3543 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3544 strlcpy(sp->f_mntonname, "[restricted]",
3545 sizeof(sp->f_mntonname));
3548 if (pr->pr_root->v_mount == mp) {
3550 * Clear current buffer data, so we are sure nothing from
3551 * the valid path left there.
3553 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3554 *sp->f_mntonname = '/';
3558 * If jail's chroot directory is set to "/" we should be able to see
3559 * all mount-points from inside a jail.
3561 if (strcmp(pr->pr_path, "/") == 0)
3563 len = strlen(pr->pr_path);
3564 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3566 * Clear current buffer data, so we are sure nothing from
3567 * the valid path left there.
3569 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3570 if (*jpath == '\0') {
3571 /* Should never happen. */
3572 *sp->f_mntonname = '/';
3574 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3579 * Check with permission for a specific privilege is granted within jail. We
3580 * have a specific list of accepted privileges; the rest are denied.
3583 prison_priv_check(struct ucred *cred, int priv)
3591 * Privileges specific to prisons with a virtual network stack.
3592 * There might be a duplicate entry here in case the privilege
3593 * is only granted conditionally in the legacy jail case.
3598 * NFS-specific privileges.
3600 case PRIV_NFS_DAEMON:
3601 case PRIV_NFS_LOCKD:
3604 * Network stack privileges.
3606 case PRIV_NET_BRIDGE:
3609 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3610 case PRIV_NET_ROUTE:
3612 case PRIV_NET_SETIFMTU:
3613 case PRIV_NET_SETIFFLAGS:
3614 case PRIV_NET_SETIFCAP:
3615 case PRIV_NET_SETIFDESCR:
3616 case PRIV_NET_SETIFNAME :
3617 case PRIV_NET_SETIFMETRIC:
3618 case PRIV_NET_SETIFPHYS:
3619 case PRIV_NET_SETIFMAC:
3620 case PRIV_NET_ADDMULTI:
3621 case PRIV_NET_DELMULTI:
3622 case PRIV_NET_HWIOCTL:
3623 case PRIV_NET_SETLLADDR:
3624 case PRIV_NET_ADDIFGROUP:
3625 case PRIV_NET_DELIFGROUP:
3626 case PRIV_NET_IFCREATE:
3627 case PRIV_NET_IFDESTROY:
3628 case PRIV_NET_ADDIFADDR:
3629 case PRIV_NET_DELIFADDR:
3632 case PRIV_NET_SETIFVNET:
3635 * 802.11-related privileges.
3637 case PRIV_NET80211_GETKEY:
3639 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3644 * AppleTalk privileges.
3646 case PRIV_NETATALK_RESERVEDPORT:
3651 case PRIV_NETATM_CFG:
3652 case PRIV_NETATM_ADD:
3653 case PRIV_NETATM_DEL:
3654 case PRIV_NETATM_SET:
3657 * Bluetooth privileges.
3659 case PRIV_NETBLUETOOTH_RAW:
3663 * Netgraph and netgraph module privileges.
3665 case PRIV_NETGRAPH_CONTROL:
3667 case PRIV_NETGRAPH_TTY:
3671 * IPv4 and IPv6 privileges.
3673 case PRIV_NETINET_IPFW:
3674 case PRIV_NETINET_DIVERT:
3675 case PRIV_NETINET_PF:
3676 case PRIV_NETINET_DUMMYNET:
3677 case PRIV_NETINET_CARP:
3678 case PRIV_NETINET_MROUTE:
3679 case PRIV_NETINET_RAW:
3680 case PRIV_NETINET_ADDRCTRL6:
3681 case PRIV_NETINET_ND6:
3682 case PRIV_NETINET_SCOPE6:
3683 case PRIV_NETINET_ALIFETIME6:
3684 case PRIV_NETINET_IPSEC:
3685 case PRIV_NETINET_BINDANY:
3689 * IPX/SPX privileges.
3691 case PRIV_NETIPX_RESERVEDPORT:
3692 case PRIV_NETIPX_RAW:
3706 * No default: or deny here.
3707 * In case of no permit fall through to next switch().
3709 if (cred->cr_prison->pr_flags & PR_VNET)
3717 * Allow ktrace privileges for root in jail.
3723 * Allow jailed processes to configure audit identity and
3724 * submit audit records (login, etc). In the future we may
3725 * want to further refine the relationship between audit and
3728 case PRIV_AUDIT_GETAUDIT:
3729 case PRIV_AUDIT_SETAUDIT:
3730 case PRIV_AUDIT_SUBMIT:
3734 * Allow jailed processes to manipulate process UNIX
3735 * credentials in any way they see fit.
3737 case PRIV_CRED_SETUID:
3738 case PRIV_CRED_SETEUID:
3739 case PRIV_CRED_SETGID:
3740 case PRIV_CRED_SETEGID:
3741 case PRIV_CRED_SETGROUPS:
3742 case PRIV_CRED_SETREUID:
3743 case PRIV_CRED_SETREGID:
3744 case PRIV_CRED_SETRESUID:
3745 case PRIV_CRED_SETRESGID:
3748 * Jail implements visibility constraints already, so allow
3749 * jailed root to override uid/gid-based constraints.
3751 case PRIV_SEEOTHERGIDS:
3752 case PRIV_SEEOTHERUIDS:
3755 * Jail implements inter-process debugging limits already, so
3756 * allow jailed root various debugging privileges.
3758 case PRIV_DEBUG_DIFFCRED:
3759 case PRIV_DEBUG_SUGID:
3760 case PRIV_DEBUG_UNPRIV:
3763 * Allow jail to set various resource limits and login
3764 * properties, and for now, exceed process resource limits.
3766 case PRIV_PROC_LIMIT:
3767 case PRIV_PROC_SETLOGIN:
3768 case PRIV_PROC_SETRLIMIT:
3771 * System V and POSIX IPC privileges are granted in jail.
3774 case PRIV_IPC_WRITE:
3775 case PRIV_IPC_ADMIN:
3776 case PRIV_IPC_MSGSIZE:
3780 * Jail operations within a jail work on child jails.
3782 case PRIV_JAIL_ATTACH:
3784 case PRIV_JAIL_REMOVE:
3787 * Jail implements its own inter-process limits, so allow
3788 * root processes in jail to change scheduling on other
3789 * processes in the same jail. Likewise for signalling.
3791 case PRIV_SCHED_DIFFCRED:
3792 case PRIV_SCHED_CPUSET:
3793 case PRIV_SIGNAL_DIFFCRED:
3794 case PRIV_SIGNAL_SUGID:
3797 * Allow jailed processes to write to sysctls marked as jail
3800 case PRIV_SYSCTL_WRITEJAIL:
3803 * Allow root in jail to manage a variety of quota
3804 * properties. These should likely be conditional on a
3805 * configuration option.
3807 case PRIV_VFS_GETQUOTA:
3808 case PRIV_VFS_SETQUOTA:
3811 * Since Jail relies on chroot() to implement file system
3812 * protections, grant many VFS privileges to root in jail.
3813 * Be careful to exclude mount-related and NFS-related
3817 case PRIV_VFS_WRITE:
3818 case PRIV_VFS_ADMIN:
3820 case PRIV_VFS_LOOKUP:
3821 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3822 case PRIV_VFS_CHFLAGS_DEV:
3823 case PRIV_VFS_CHOWN:
3824 case PRIV_VFS_CHROOT:
3825 case PRIV_VFS_RETAINSUGID:
3826 case PRIV_VFS_FCHROOT:
3828 case PRIV_VFS_SETGID:
3830 case PRIV_VFS_STICKYFILE:
3834 * Depending on the global setting, allow privilege of
3835 * setting system flags.
3837 case PRIV_VFS_SYSFLAGS:
3838 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3844 * Depending on the global setting, allow privilege of
3845 * mounting/unmounting file systems.
3847 case PRIV_VFS_MOUNT:
3848 case PRIV_VFS_UNMOUNT:
3849 case PRIV_VFS_MOUNT_NONUSER:
3850 case PRIV_VFS_MOUNT_OWNER:
3851 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3857 * Allow jailed root to bind reserved ports and reuse in-use
3860 case PRIV_NETINET_RESERVEDPORT:
3861 case PRIV_NETINET_REUSEPORT:
3865 * Allow jailed root to set certian IPv4/6 (option) headers.
3867 case PRIV_NETINET_SETHDROPTS:
3871 * Conditionally allow creating raw sockets in jail.
3873 case PRIV_NETINET_RAW:
3874 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3880 * Since jail implements its own visibility limits on netstat
3881 * sysctls, allow getcred. This allows identd to work in
3884 case PRIV_NETINET_GETCRED:
3888 * Allow jailed root to set loginclass.
3890 case PRIV_PROC_SETLOGINCLASS:
3895 * In all remaining cases, deny the privilege request. This
3896 * includes almost all network privileges, many system
3897 * configuration privileges.
3904 * Return the part of pr2's name that is relative to pr1, or the whole name
3905 * if it does not directly follow.
3909 prison_name(struct prison *pr1, struct prison *pr2)
3913 /* Jails see themselves as "0" (if they see themselves at all). */
3916 name = pr2->pr_name;
3917 if (prison_ischild(pr1, pr2)) {
3919 * pr1 isn't locked (and allprison_lock may not be either)
3920 * so its length can't be counted on. But the number of dots
3921 * can be counted on - and counted.
3923 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3924 name = strchr(name, '.') + 1;
3930 * Return the part of pr2's path that is relative to pr1, or the whole path
3931 * if it does not directly follow.
3934 prison_path(struct prison *pr1, struct prison *pr2)
3936 char *path1, *path2;
3939 path1 = pr1->pr_path;
3940 path2 = pr2->pr_path;
3941 if (!strcmp(path1, "/"))
3943 len1 = strlen(path1);
3944 if (strncmp(path1, path2, len1))
3946 if (path2[len1] == '\0')
3948 if (path2[len1] == '/')
3949 return (path2 + len1);
3955 * Jail-related sysctls.
3957 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3961 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3964 struct prison *pr, *cpr;
3966 struct in_addr *ip4 = NULL;
3970 struct in6_addr *ip6 = NULL;
3975 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3976 pr = req->td->td_ucred->cr_prison;
3978 sx_slock(&allprison_lock);
3979 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3980 #if defined(INET) || defined(INET6)
3983 mtx_lock(&cpr->pr_mtx);
3985 if (cpr->pr_ip4s > 0) {
3986 if (ip4s < cpr->pr_ip4s) {
3987 ip4s = cpr->pr_ip4s;
3988 mtx_unlock(&cpr->pr_mtx);
3989 ip4 = realloc(ip4, ip4s *
3990 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3993 bcopy(cpr->pr_ip4, ip4,
3994 cpr->pr_ip4s * sizeof(struct in_addr));
3998 if (cpr->pr_ip6s > 0) {
3999 if (ip6s < cpr->pr_ip6s) {
4000 ip6s = cpr->pr_ip6s;
4001 mtx_unlock(&cpr->pr_mtx);
4002 ip6 = realloc(ip6, ip6s *
4003 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
4006 bcopy(cpr->pr_ip6, ip6,
4007 cpr->pr_ip6s * sizeof(struct in6_addr));
4010 if (cpr->pr_ref == 0) {
4011 mtx_unlock(&cpr->pr_mtx);
4014 bzero(xp, sizeof(*xp));
4015 xp->pr_version = XPRISON_VERSION;
4016 xp->pr_id = cpr->pr_id;
4017 xp->pr_state = cpr->pr_uref > 0
4018 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
4019 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4020 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4021 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4023 xp->pr_ip4s = cpr->pr_ip4s;
4026 xp->pr_ip6s = cpr->pr_ip6s;
4028 mtx_unlock(&cpr->pr_mtx);
4029 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4033 if (xp->pr_ip4s > 0) {
4034 error = SYSCTL_OUT(req, ip4,
4035 xp->pr_ip4s * sizeof(struct in_addr));
4041 if (xp->pr_ip6s > 0) {
4042 error = SYSCTL_OUT(req, ip6,
4043 xp->pr_ip6s * sizeof(struct in6_addr));
4049 sx_sunlock(&allprison_lock);
4060 SYSCTL_OID(_security_jail, OID_AUTO, list,
4061 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4062 sysctl_jail_list, "S", "List of active jails");
4065 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4069 injail = jailed(req->td->td_ucred);
4070 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4075 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4076 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4077 sysctl_jail_jailed, "I", "Process in jail?");
4079 #if defined(INET) || defined(INET6)
4080 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4081 &jail_max_af_ips, 0,
4082 "Number of IP addresses a jail may have at most per address family");
4086 * Default parameters for jail(2) compatability. For historical reasons,
4087 * the sysctl names have varying similarity to the parameter names. Prisons
4088 * just see their own parameters, and can't change them.
4091 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4094 int allow, error, i;
4096 pr = req->td->td_ucred->cr_prison;
4097 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4099 /* Get the current flag value, and convert it to a boolean. */
4100 i = (allow & arg2) ? 1 : 0;
4103 error = sysctl_handle_int(oidp, &i, 0, req);
4104 if (error || !req->newptr)
4110 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4113 mtx_lock(&prison0.pr_mtx);
4114 jail_default_allow = (jail_default_allow & ~arg2) | i;
4115 mtx_unlock(&prison0.pr_mtx);
4119 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4120 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4121 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4122 "Processes in jail can set their hostnames");
4123 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4124 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4125 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4126 "Processes in jail are limited to creating UNIX/IP/route sockets only");
4127 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4128 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4129 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4130 "Processes in jail can use System V IPC primitives");
4131 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4132 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4133 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4134 "Prison root can create raw sockets");
4135 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4136 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4137 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4138 "Processes in jail can alter system file flags");
4139 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4140 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4141 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4142 "Processes in jail can mount/unmount jail-friendly file systems");
4145 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4150 pr = req->td->td_ucred->cr_prison;
4151 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4152 error = sysctl_handle_int(oidp, &level, 0, req);
4153 if (error || !req->newptr)
4155 *(int *)arg1 = level;
4159 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4160 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4161 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4162 sysctl_jail_default_level, "I",
4163 "Processes in jail cannot see all mounted file systems");
4166 * Nodes to describe jail parameters. Maximum length of string parameters
4167 * is returned in the string itself, and the other parameters exist merely
4168 * to make themselves and their types known.
4170 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4174 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4181 switch (oidp->oid_kind & CTLTYPE)
4187 if (!(req->flags & SCTL_MASK32))
4189 return (SYSCTL_OUT(req, &l, sizeof(l)));
4193 return (SYSCTL_OUT(req, &i, sizeof(i)));
4194 case CTLTYPE_STRING:
4195 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4197 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4198 case CTLTYPE_STRUCT:
4200 return (SYSCTL_OUT(req, &s, sizeof(s)));
4205 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4206 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4207 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4208 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4209 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4210 "I", "Jail secure level");
4211 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4212 "I", "Jail cannot see all mounted file systems");
4213 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4214 "B", "Jail persistence");
4216 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4217 "E,jailsys", "Virtual network stack");
4219 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4220 "B", "Jail is in the process of shutting down");
4222 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4223 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4224 "I", "Current number of child jails");
4225 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4226 "I", "Maximum number of child jails");
4228 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4229 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4231 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4232 "Jail NIS domainname");
4233 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4235 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4236 "LU", "Jail host ID");
4238 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4239 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4242 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4243 "Jail IPv4 address virtualization");
4244 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4245 "S,in_addr,a", "Jail IPv4 addresses");
4246 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4247 "B", "Do (not) use IPv4 source address selection rather than the "
4248 "primary jail IPv4 address.");
4251 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4252 "Jail IPv6 address virtualization");
4253 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4254 "S,in6_addr,a", "Jail IPv6 addresses");
4255 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4256 "B", "Do (not) use IPv6 source address selection rather than the "
4257 "primary jail IPv6 address.");
4260 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4261 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4262 "B", "Jail may set hostname");
4263 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4264 "B", "Jail may use SYSV IPC");
4265 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4266 "B", "Jail may create raw sockets");
4267 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4268 "B", "Jail may alter system file flags");
4269 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4270 "B", "Jail may mount/unmount jail-friendly file systems");
4271 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4272 "B", "Jail may set file quotas");
4273 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4274 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4277 prison_racct_foreach(void (*callback)(struct racct *racct,
4278 void *arg2, void *arg3), void *arg2, void *arg3)
4282 sx_slock(&allprison_lock);
4283 TAILQ_FOREACH(pr, &allprison, pr_list)
4284 (callback)(pr->pr_racct, arg2, arg3);
4285 sx_sunlock(&allprison_lock);
4291 db_show_prison(struct prison *pr)
4294 #if defined(INET) || defined(INET6)
4299 char ip6buf[INET6_ADDRSTRLEN];
4302 db_printf("prison %p:\n", pr);
4303 db_printf(" jid = %d\n", pr->pr_id);
4304 db_printf(" name = %s\n", pr->pr_name);
4305 db_printf(" parent = %p\n", pr->pr_parent);
4306 db_printf(" ref = %d\n", pr->pr_ref);
4307 db_printf(" uref = %d\n", pr->pr_uref);
4308 db_printf(" path = %s\n", pr->pr_path);
4309 db_printf(" cpuset = %d\n", pr->pr_cpuset
4310 ? pr->pr_cpuset->cs_id : -1);
4312 db_printf(" vnet = %p\n", pr->pr_vnet);
4314 db_printf(" root = %p\n", pr->pr_root);
4315 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4316 db_printf(" children.max = %d\n", pr->pr_childmax);
4317 db_printf(" children.cur = %d\n", pr->pr_childcount);
4318 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4319 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4320 db_printf(" flags = 0x%x", pr->pr_flags);
4321 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4323 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4324 db_printf(" %s", pr_flag_names[fi]);
4325 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4327 jsf = pr->pr_flags &
4328 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4329 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4330 pr_flag_jailsys[fi].disable &&
4331 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4332 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4335 db_printf(" allow = 0x%x", pr->pr_allow);
4336 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4338 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4339 db_printf(" %s", pr_allow_names[fi]);
4341 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4342 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4343 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4344 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4345 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4347 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4348 for (ii = 0; ii < pr->pr_ip4s; ii++)
4349 db_printf(" %s %s\n",
4350 ii == 0 ? "ip4.addr =" : " ",
4351 inet_ntoa(pr->pr_ip4[ii]));
4354 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4355 for (ii = 0; ii < pr->pr_ip6s; ii++)
4356 db_printf(" %s %s\n",
4357 ii == 0 ? "ip6.addr =" : " ",
4358 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4362 DB_SHOW_COMMAND(prison, db_show_prison_command)
4368 * Show all prisons in the list, and prison0 which is not
4371 db_show_prison(&prison0);
4372 if (!db_pager_quit) {
4373 TAILQ_FOREACH(pr, &allprison, pr_list) {
4385 /* Look for a prison with the ID and with references. */
4386 TAILQ_FOREACH(pr, &allprison, pr_list)
4387 if (pr->pr_id == addr && pr->pr_ref > 0)
4390 /* Look again, without requiring a reference. */
4391 TAILQ_FOREACH(pr, &allprison, pr_list)
4392 if (pr->pr_id == addr)
4395 /* Assume address points to a valid prison. */
4396 pr = (struct prison *)addr;