2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2008 Bjoern A. Zeeb.
4 * Copyright (c) 2009 James Gritton.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
47 #include <sys/taskqueue.h>
48 #include <sys/fcntl.h>
51 #include <sys/mutex.h>
53 #include <sys/sysent.h>
54 #include <sys/namei.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/vnode.h>
65 #include <netinet/in.h>
70 #include <netinet6/in6_var.h>
74 #include <security/mac/mac_framework.h>
76 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
78 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
80 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
83 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
85 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
89 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
91 #define _PR_IP_SADDRSEL 0
95 /* prison0 describes what is "real" about the system. */
96 struct prison prison0 = {
102 .pr_securelevel = -1,
103 .pr_childmax = JAIL_MAX,
104 .pr_hostuuid = DEFAULT_HOSTUUID,
105 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
107 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
109 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
111 .pr_allow = PR_ALLOW_ALL,
113 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
115 /* allprison and lastprid are protected by allprison_lock. */
116 struct sx allprison_lock;
117 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
118 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
121 static int do_jail_attach(struct thread *td, struct prison *pr);
122 static void prison_complete(void *context, int pending);
123 static void prison_deref(struct prison *pr, int flags);
124 static char *prison_path(struct prison *pr1, struct prison *pr2);
125 static void prison_remove_one(struct prison *pr);
127 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
128 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
131 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
132 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
135 /* Flags for prison_deref */
136 #define PD_DEREF 0x01
137 #define PD_DEUREF 0x02
138 #define PD_LOCKED 0x04
139 #define PD_LIST_SLOCKED 0x08
140 #define PD_LIST_XLOCKED 0x10
143 * Parameter names corresponding to PR_* flag values
145 static char *pr_flag_names[] = {
148 [7] = "ip4.saddrsel",
151 [8] = "ip6.saddrsel",
155 static char *pr_flag_nonames[] = {
158 [7] = "ip4.nosaddrsel",
161 [8] = "ip6.nosaddrsel",
165 struct jailsys_flags {
169 } pr_flag_jailsys[] = {
170 { "host", 0, PR_HOST },
172 { "vnet", 0, PR_VNET },
175 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
178 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
182 static char *pr_allow_names[] = {
183 "allow.set_hostname",
192 static char *pr_allow_nonames[] = {
193 "allow.noset_hostname",
195 "allow.noraw_sockets",
202 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
203 #define JAIL_DEFAULT_ENFORCE_STATFS 2
204 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
205 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
206 #if defined(INET) || defined(INET6)
207 static unsigned jail_max_af_ips = 255;
212 qcmp_v4(const void *ip1, const void *ip2)
217 * We need to compare in HBO here to get the list sorted as expected
218 * by the result of the code. Sorting NBO addresses gives you
219 * interesting results. If you do not understand, do not try.
221 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
222 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
225 * Do not simply return the difference of the two numbers, the int is
239 qcmp_v6(const void *ip1, const void *ip2)
241 const struct in6_addr *ia6a, *ia6b;
244 ia6a = (const struct in6_addr *)ip1;
245 ia6b = (const struct in6_addr *)ip2;
248 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
249 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
251 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
264 jail(struct thread *td, struct jail_args *uap)
270 error = copyin(uap->jail, &version, sizeof(uint32_t));
279 /* FreeBSD single IPv4 jails. */
280 bzero(&j, sizeof(struct jail));
281 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
284 j.version = j0.version;
286 j.hostname = j0.hostname;
287 j.ip4s = j0.ip_number;
293 * Version 1 was used by multi-IPv4 jail implementations
294 * that never made it into the official kernel.
298 case 2: /* JAIL_API_VERSION */
299 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
300 error = copyin(uap->jail, &j, sizeof(struct jail));
306 /* Sci-Fi jails are not supported, sorry. */
309 return (kern_jail(td, &j));
313 kern_jail(struct thread *td, struct jail *j)
315 struct iovec optiov[2 * (4
316 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
325 char *u_path, *u_hostname, *u_name;
328 struct in_addr *u_ip4;
331 struct in6_addr *u_ip6;
334 int error, enforce_statfs, fi;
336 bzero(&optiov, sizeof(optiov));
337 opt.uio_iov = optiov;
341 opt.uio_segflg = UIO_SYSSPACE;
342 opt.uio_rw = UIO_READ;
345 /* Set permissions for top-level jails from sysctls. */
346 if (!jailed(td->td_ucred)) {
347 for (fi = 0; fi < sizeof(pr_allow_names) /
348 sizeof(pr_allow_names[0]); fi++) {
349 optiov[opt.uio_iovcnt].iov_base =
350 (jail_default_allow & (1 << fi))
351 ? pr_allow_names[fi] : pr_allow_nonames[fi];
352 optiov[opt.uio_iovcnt].iov_len =
353 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
356 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
357 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
359 enforce_statfs = jail_default_enforce_statfs;
360 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
361 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
365 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
367 ip4s = (j->version == 0) ? 1 : j->ip4s;
368 if (ip4s > jail_max_af_ips)
370 tmplen += ip4s * sizeof(struct in_addr);
376 if (j->ip6s > jail_max_af_ips)
378 tmplen += j->ip6s * sizeof(struct in6_addr);
383 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
384 u_hostname = u_path + MAXPATHLEN;
385 u_name = u_hostname + MAXHOSTNAMELEN;
387 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
391 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
393 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
396 optiov[opt.uio_iovcnt].iov_base = "path";
397 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
399 optiov[opt.uio_iovcnt].iov_base = u_path;
400 error = copyinstr(j->path, u_path, MAXPATHLEN,
401 &optiov[opt.uio_iovcnt].iov_len);
403 free(u_path, M_TEMP);
407 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
408 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
410 optiov[opt.uio_iovcnt].iov_base = u_hostname;
411 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
412 &optiov[opt.uio_iovcnt].iov_len);
414 free(u_path, M_TEMP);
418 if (j->jailname != NULL) {
419 optiov[opt.uio_iovcnt].iov_base = "name";
420 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
422 optiov[opt.uio_iovcnt].iov_base = u_name;
423 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
424 &optiov[opt.uio_iovcnt].iov_len);
426 free(u_path, M_TEMP);
432 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
433 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
435 optiov[opt.uio_iovcnt].iov_base = u_ip4;
436 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
438 u_ip4->s_addr = j->ip4s;
440 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
442 free(u_path, M_TEMP);
449 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
450 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
452 optiov[opt.uio_iovcnt].iov_base = u_ip6;
453 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
454 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
456 free(u_path, M_TEMP);
461 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
462 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
463 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
464 free(u_path, M_TEMP);
470 * struct jail_set_args {
471 * struct iovec *iovp;
472 * unsigned int iovcnt;
477 jail_set(struct thread *td, struct jail_set_args *uap)
482 /* Check that we have an even number of iovecs. */
486 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
489 error = kern_jail_set(td, auio, uap->flags);
495 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
502 struct in6_addr *ip6;
505 struct vfsoptlist *opts;
506 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
508 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
509 #if defined(INET) || defined(INET6)
514 size_t namelen, onamelen;
515 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
516 int gotchildmax, gotenforce, gothid, gotslevel;
517 int fi, jid, jsys, len, level;
518 int childmax, slevel, vfslocked;
519 #if defined(INET) || defined(INET6)
528 unsigned pr_flags, ch_flags;
529 unsigned pr_allow, ch_allow, tallow;
532 error = priv_check(td, PRIV_JAIL_SET);
533 if (!error && (flags & JAIL_ATTACH))
534 error = priv_check(td, PRIV_JAIL_ATTACH);
537 mypr = ppr = td->td_ucred->cr_prison;
538 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
540 if (flags & ~JAIL_SET_MASK)
544 * Check all the parameters before committing to anything. Not all
545 * errors can be caught early, but we may as well try. Also, this
546 * takes care of some expensive stuff (path lookup) before getting
547 * the allprison lock.
549 * XXX Jails are not filesystems, and jail parameters are not mount
550 * options. But it makes more sense to re-use the vfsopt code
551 * than duplicate it under a different name.
553 error = vfs_buildopts(optuio, &opts);
563 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
569 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
578 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
586 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
587 gotenforce = (error == 0);
589 if (enforce < 0 || enforce > 2)
591 } else if (error != ENOENT)
594 pr_flags = ch_flags = 0;
595 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
597 if (pr_flag_names[fi] == NULL)
599 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
600 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
602 ch_flags |= pr_flags;
603 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
605 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
612 case JAIL_SYS_DISABLE:
613 if (!pr_flag_jailsys[fi].disable) {
617 pr_flags |= pr_flag_jailsys[fi].disable;
620 pr_flags |= pr_flag_jailsys[fi].new;
622 case JAIL_SYS_INHERIT:
629 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
631 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
632 && !(pr_flags & PR_PERSIST)) {
634 vfs_opterror(opts, "new jail must persist or attach");
638 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
640 vfs_opterror(opts, "vnet cannot be changed after creation");
645 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
647 vfs_opterror(opts, "ip4 cannot be changed after creation");
652 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
654 vfs_opterror(opts, "ip6 cannot be changed after creation");
659 pr_allow = ch_allow = 0;
660 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
662 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
663 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
665 ch_allow |= pr_allow;
667 error = vfs_getopt(opts, "name", (void **)&name, &len);
673 if (len == 0 || name[len - 1] != '\0') {
677 if (len > MAXHOSTNAMELEN) {
678 error = ENAMETOOLONG;
683 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
691 if (len == 0 || host[len - 1] != '\0') {
695 if (len > MAXHOSTNAMELEN) {
696 error = ENAMETOOLONG;
701 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
709 if (len == 0 || domain[len - 1] != '\0') {
713 if (len > MAXHOSTNAMELEN) {
714 error = ENAMETOOLONG;
719 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
727 if (len == 0 || uuid[len - 1] != '\0') {
731 if (len > HOSTUUIDLEN) {
732 error = ENAMETOOLONG;
738 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
741 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
745 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
757 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
759 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
762 else if (ip4s & (sizeof(*ip4) - 1)) {
766 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
768 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
770 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
771 ip4s /= sizeof(*ip4);
772 if (ip4s > jail_max_af_ips) {
774 vfs_opterror(opts, "too many IPv4 addresses");
777 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
778 bcopy(op, ip4, ip4s * sizeof(*ip4));
780 * IP addresses are all sorted but ip[0] to preserve
781 * the primary IP address as given from userland.
782 * This special IP is used for unbound outgoing
783 * connections as well for "loopback" traffic in case
784 * source address selection cannot find any more fitting
785 * address to connect from.
788 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
790 * Check for duplicate addresses and do some simple
791 * zero and broadcast checks. If users give other bogus
792 * addresses it is their problem.
794 * We do not have to care about byte order for these
795 * checks so we will do them in NBO.
797 for (ii = 0; ii < ip4s; ii++) {
798 if (ip4[ii].s_addr == INADDR_ANY ||
799 ip4[ii].s_addr == INADDR_BROADCAST) {
804 (ip4[0].s_addr == ip4[ii+1].s_addr ||
805 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
815 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
817 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
820 else if (ip6s & (sizeof(*ip6) - 1)) {
824 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
826 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
828 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
829 ip6s /= sizeof(*ip6);
830 if (ip6s > jail_max_af_ips) {
832 vfs_opterror(opts, "too many IPv6 addresses");
835 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
836 bcopy(op, ip6, ip6s * sizeof(*ip6));
838 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
839 for (ii = 0; ii < ip6s; ii++) {
840 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
845 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
846 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
856 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
857 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
860 "vnet jails cannot have IP address restrictions");
866 error = vfs_getopt(opts, "path", (void **)&path, &len);
872 if (flags & JAIL_UPDATE) {
875 "path cannot be changed after creation");
878 if (len == 0 || path[len - 1] != '\0') {
882 if (len < 2 || (len == 2 && path[0] == '/'))
885 /* Leave room for a real-root full pathname. */
886 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
887 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
888 error = ENAMETOOLONG;
891 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
896 vfslocked = NDHASGIANT(&nd);
898 NDFREE(&nd, NDF_ONLY_PNBUF);
899 if (root->v_type != VDIR) {
902 VFS_UNLOCK_GIANT(vfslocked);
905 VFS_UNLOCK_GIANT(vfslocked);
910 * Grab the allprison lock before letting modules check their
911 * parameters. Once we have it, do not let go so we'll have a
912 * consistent view of the OSD list.
914 sx_xlock(&allprison_lock);
915 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
917 goto done_unlock_list;
919 /* By now, all parameters should have been noted. */
920 TAILQ_FOREACH(opt, opts, link) {
921 if (!opt->seen && strcmp(opt->name, "errmsg")) {
923 vfs_opterror(opts, "unknown parameter: %s", opt->name);
924 goto done_unlock_list;
929 * See if we are creating a new record or updating an existing one.
930 * This abuses the file error codes ENOENT and EEXIST.
932 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
935 vfs_opterror(opts, "no valid operation (create or update)");
936 goto done_unlock_list;
940 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
941 namelc = strrchr(name, '.');
942 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
948 * See if a requested jid already exists. There is an
949 * information leak here if the jid exists but is not within
950 * the caller's jail hierarchy. Jail creators will get EEXIST
951 * even though they cannot see the jail, and CREATE | UPDATE
952 * will return ENOENT which is not normally a valid error.
956 vfs_opterror(opts, "negative jid");
957 goto done_unlock_list;
959 pr = prison_find(jid);
962 /* Create: jid must not exist. */
963 if (cuflags == JAIL_CREATE) {
964 mtx_unlock(&pr->pr_mtx);
966 vfs_opterror(opts, "jail %d already exists",
968 goto done_unlock_list;
970 if (!prison_ischild(mypr, pr)) {
971 mtx_unlock(&pr->pr_mtx);
973 } else if (pr->pr_uref == 0) {
974 if (!(flags & JAIL_DYING)) {
975 mtx_unlock(&pr->pr_mtx);
977 vfs_opterror(opts, "jail %d is dying",
979 goto done_unlock_list;
980 } else if ((flags & JAIL_ATTACH) ||
981 (pr_flags & PR_PERSIST)) {
983 * A dying jail might be resurrected
984 * (via attach or persist), but first
985 * it must determine if another jail
986 * has claimed its name. Accomplish
987 * this by implicitly re-setting the
991 name = prison_name(mypr, pr);
996 /* Update: jid must exist. */
997 if (cuflags == JAIL_UPDATE) {
999 vfs_opterror(opts, "jail %d not found", jid);
1000 goto done_unlock_list;
1005 * If the caller provided a name, look for a jail by that name.
1006 * This has different semantics for creates and updates keyed by jid
1007 * (where the name must not already exist in a different jail),
1008 * and updates keyed by the name itself (where the name must exist
1009 * because that is the jail being updated).
1012 namelc = strrchr(name, '.');
1017 * This is a hierarchical name. Split it into the
1018 * parent and child names, and make sure the parent
1019 * exists or matches an already found jail.
1023 if (strncmp(name, ppr->pr_name, namelc - name)
1024 || ppr->pr_name[namelc - name] != '\0') {
1025 mtx_unlock(&pr->pr_mtx);
1028 "cannot change jail's parent");
1029 goto done_unlock_list;
1032 ppr = prison_find_name(mypr, name);
1036 "jail \"%s\" not found", name);
1037 goto done_unlock_list;
1039 mtx_unlock(&ppr->pr_mtx);
1043 if (name[0] != '\0') {
1045 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1048 FOREACH_PRISON_CHILD(ppr, tpr) {
1049 if (tpr != pr && tpr->pr_ref > 0 &&
1050 !strcmp(tpr->pr_name + namelen, name)) {
1052 cuflags != JAIL_CREATE) {
1053 mtx_lock(&tpr->pr_mtx);
1054 if (tpr->pr_ref > 0) {
1059 if (tpr->pr_uref > 0) {
1065 mtx_unlock(&tpr->pr_mtx);
1066 } else if (tpr->pr_uref > 0) {
1068 * Create, or update(jid):
1069 * name must not exist in an
1070 * active sibling jail.
1074 mtx_unlock(&pr->pr_mtx);
1076 "jail \"%s\" already exists",
1078 goto done_unlock_list;
1082 /* If no active jail is found, use a dying one. */
1083 if (deadpr != NULL && pr == NULL) {
1084 if (flags & JAIL_DYING) {
1085 mtx_lock(&deadpr->pr_mtx);
1086 if (deadpr->pr_ref == 0) {
1087 mtx_unlock(&deadpr->pr_mtx);
1091 } else if (cuflags == JAIL_UPDATE) {
1094 "jail \"%s\" is dying", name);
1095 goto done_unlock_list;
1098 /* Update: name must exist if no jid. */
1099 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1101 vfs_opterror(opts, "jail \"%s\" not found",
1103 goto done_unlock_list;
1107 /* Update: must provide a jid or name. */
1108 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1110 vfs_opterror(opts, "update specified no jail");
1111 goto done_unlock_list;
1114 /* If there's no prison to update, create a new one and link it in. */
1116 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1117 if (tpr->pr_childcount >= tpr->pr_childmax) {
1119 vfs_opterror(opts, "prison limit exceeded");
1120 goto done_unlock_list;
1123 mtx_lock(&ppr->pr_mtx);
1124 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1125 mtx_unlock(&ppr->pr_mtx);
1127 vfs_opterror(opts, "parent jail went away!");
1128 goto done_unlock_list;
1132 mtx_unlock(&ppr->pr_mtx);
1133 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1135 /* Find the next free jid. */
1138 if (jid == JAIL_MAX)
1140 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1141 if (tpr->pr_id < jid)
1143 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1144 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1147 if (jid == lastprid) {
1150 "no available jail IDs");
1152 prison_deref(ppr, PD_DEREF |
1153 PD_DEUREF | PD_LIST_XLOCKED);
1162 * The jail already has a jid (that did not yet exist),
1163 * so just find where to insert it.
1165 TAILQ_FOREACH(tpr, &allprison, pr_list)
1166 if (tpr->pr_id >= jid) {
1167 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1172 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1173 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1174 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1175 tpr->pr_childcount++;
1177 pr->pr_parent = ppr;
1180 /* Set some default values, and inherit some from the parent. */
1185 root = mypr->pr_root;
1188 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1189 pr->pr_flags |= PR_HOST;
1190 #if defined(INET) || defined(INET6)
1192 if (!(pr_flags & PR_VNET))
1196 if (!(ch_flags & PR_IP4_USER))
1198 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1199 else if (!(pr_flags & PR_IP4_USER)) {
1200 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1201 if (ppr->pr_ip4 != NULL) {
1202 pr->pr_ip4s = ppr->pr_ip4s;
1203 pr->pr_ip4 = malloc(pr->pr_ip4s *
1204 sizeof(struct in_addr), M_PRISON,
1206 bcopy(ppr->pr_ip4, pr->pr_ip4,
1207 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1212 if (!(ch_flags & PR_IP6_USER))
1214 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1215 else if (!(pr_flags & PR_IP6_USER)) {
1216 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1217 if (ppr->pr_ip6 != NULL) {
1218 pr->pr_ip6s = ppr->pr_ip6s;
1219 pr->pr_ip6 = malloc(pr->pr_ip6s *
1220 sizeof(struct in6_addr), M_PRISON,
1222 bcopy(ppr->pr_ip6, pr->pr_ip6,
1223 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1229 /* Source address selection is always on by default. */
1230 pr->pr_flags |= _PR_IP_SADDRSEL;
1232 pr->pr_securelevel = ppr->pr_securelevel;
1233 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1234 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1236 LIST_INIT(&pr->pr_children);
1237 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1240 /* Allocate a new vnet if specified. */
1241 pr->pr_vnet = (pr_flags & PR_VNET)
1242 ? vnet_alloc() : ppr->pr_vnet;
1245 * Allocate a dedicated cpuset for each jail.
1246 * Unlike other initial settings, this may return an erorr.
1248 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1250 prison_deref(pr, PD_LIST_XLOCKED);
1254 mtx_lock(&pr->pr_mtx);
1256 * New prisons do not yet have a reference, because we do not
1257 * want other to see the incomplete prison once the
1258 * allprison_lock is downgraded.
1263 * Grab a reference for existing prisons, to ensure they
1264 * continue to exist for the duration of the call.
1267 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1268 if ((pr->pr_flags & PR_VNET) &&
1269 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1272 "vnet jails cannot have IP address restrictions");
1273 goto done_deref_locked;
1277 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1280 "ip4 cannot be changed after creation");
1281 goto done_deref_locked;
1285 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1288 "ip6 cannot be changed after creation");
1289 goto done_deref_locked;
1294 /* Do final error checking before setting anything. */
1296 if (slevel < ppr->pr_securelevel) {
1298 goto done_deref_locked;
1302 if (childmax >= ppr->pr_childmax) {
1304 goto done_deref_locked;
1308 if (enforce < ppr->pr_enforce_statfs) {
1310 goto done_deref_locked;
1315 if (ppr->pr_flags & PR_IP4) {
1317 * Make sure the new set of IP addresses is a
1318 * subset of the parent's list. Don't worry
1319 * about the parent being unlocked, as any
1320 * setting is done with allprison_lock held.
1322 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1323 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1325 if (ij == ppr->pr_ip4s) {
1327 goto done_deref_locked;
1330 for (ii = ij = 1; ii < ip4s; ii++) {
1331 if (ip4[ii].s_addr ==
1332 ppr->pr_ip4[0].s_addr)
1334 for (; ij < ppr->pr_ip4s; ij++)
1335 if (ip4[ii].s_addr ==
1336 ppr->pr_ip4[ij].s_addr)
1338 if (ij == ppr->pr_ip4s)
1341 if (ij == ppr->pr_ip4s) {
1343 goto done_deref_locked;
1348 * Check for conflicting IP addresses. We permit them
1349 * if there is no more than one IP on each jail. If
1350 * there is a duplicate on a jail with more than one
1351 * IP stop checking and return error.
1355 for (; tppr != &prison0; tppr = tppr->pr_parent)
1356 if (tppr->pr_flags & PR_VNET)
1359 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1362 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1364 tpr->pr_uref == 0) {
1368 if (!(tpr->pr_flags & PR_IP4_USER))
1371 if (tpr->pr_ip4 == NULL ||
1372 (ip4s == 1 && tpr->pr_ip4s == 1))
1374 for (ii = 0; ii < ip4s; ii++) {
1375 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1378 "IPv4 addresses clash");
1379 goto done_deref_locked;
1387 if (ppr->pr_flags & PR_IP6) {
1389 * Make sure the new set of IP addresses is a
1390 * subset of the parent's list.
1392 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1393 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1396 if (ij == ppr->pr_ip6s) {
1398 goto done_deref_locked;
1401 for (ii = ij = 1; ii < ip6s; ii++) {
1402 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1405 for (; ij < ppr->pr_ip6s; ij++)
1406 if (IN6_ARE_ADDR_EQUAL(
1407 &ip6[ii], &ppr->pr_ip6[ij]))
1409 if (ij == ppr->pr_ip6s)
1412 if (ij == ppr->pr_ip6s) {
1414 goto done_deref_locked;
1418 /* Check for conflicting IP addresses. */
1421 for (; tppr != &prison0; tppr = tppr->pr_parent)
1422 if (tppr->pr_flags & PR_VNET)
1425 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1428 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1430 tpr->pr_uref == 0) {
1434 if (!(tpr->pr_flags & PR_IP6_USER))
1437 if (tpr->pr_ip6 == NULL ||
1438 (ip6s == 1 && tpr->pr_ip6s == 1))
1440 for (ii = 0; ii < ip6s; ii++) {
1441 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1444 "IPv6 addresses clash");
1445 goto done_deref_locked;
1451 onamelen = namelen = 0;
1453 /* Give a default name of the jid. */
1454 if (name[0] == '\0')
1455 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1456 else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1460 "name cannot be numeric (unless it is the jid)");
1461 goto done_deref_locked;
1464 * Make sure the name isn't too long for the prison or its
1467 onamelen = strlen(pr->pr_name);
1468 namelen = strlen(name);
1469 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1470 error = ENAMETOOLONG;
1471 goto done_deref_locked;
1473 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1474 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1475 sizeof(pr->pr_name)) {
1476 error = ENAMETOOLONG;
1477 goto done_deref_locked;
1481 if (pr_allow & ~ppr->pr_allow) {
1483 goto done_deref_locked;
1486 /* Set the parameters of the prison. */
1489 if (pr_flags & PR_IP4_USER) {
1490 pr->pr_flags |= PR_IP4;
1491 free(pr->pr_ip4, M_PRISON);
1495 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1497 if (tpr->pr_flags & PR_VNET) {
1502 if (prison_restrict_ip4(tpr, NULL)) {
1511 if (pr_flags & PR_IP6_USER) {
1512 pr->pr_flags |= PR_IP6;
1513 free(pr->pr_ip6, M_PRISON);
1517 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1519 if (tpr->pr_flags & PR_VNET) {
1524 if (prison_restrict_ip6(tpr, NULL)) {
1532 pr->pr_securelevel = slevel;
1533 /* Set all child jails to be at least this level. */
1534 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1535 if (tpr->pr_securelevel < slevel)
1536 tpr->pr_securelevel = slevel;
1539 pr->pr_childmax = childmax;
1540 /* Set all child jails to under this limit. */
1541 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1542 if (tpr->pr_childmax > childmax - level)
1543 tpr->pr_childmax = childmax > level
1544 ? childmax - level : 0;
1547 pr->pr_enforce_statfs = enforce;
1548 /* Pass this restriction on to the children. */
1549 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1550 if (tpr->pr_enforce_statfs < enforce)
1551 tpr->pr_enforce_statfs = enforce;
1554 if (ppr == &prison0)
1555 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1557 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1558 ppr->pr_name, name);
1559 /* Change this component of child names. */
1560 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1561 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1562 strlen(tpr->pr_name + onamelen) + 1);
1563 bcopy(pr->pr_name, tpr->pr_name, namelen);
1567 /* Try to keep a real-rooted full pathname. */
1568 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1569 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1570 mypr->pr_path, path);
1572 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1575 if (PR_HOST & ch_flags & ~pr_flags) {
1576 if (pr->pr_flags & PR_HOST) {
1578 * Copy the parent's host info. As with pr_ip4 above,
1579 * the lack of a lock on the parent is not a problem;
1580 * it is always set with allprison_lock at least
1581 * shared, and is held exclusively here.
1583 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1584 sizeof(pr->pr_hostname));
1585 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1586 sizeof(pr->pr_domainname));
1587 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1588 sizeof(pr->pr_hostuuid));
1589 pr->pr_hostid = pr->pr_parent->pr_hostid;
1591 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1592 /* Set this prison, and any descendants without PR_HOST. */
1594 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1596 strlcpy(pr->pr_domainname, domain,
1597 sizeof(pr->pr_domainname));
1599 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1601 pr->pr_hostid = hid;
1602 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1603 if (tpr->pr_flags & PR_HOST)
1607 strlcpy(tpr->pr_hostname,
1609 sizeof(tpr->pr_hostname));
1611 strlcpy(tpr->pr_domainname,
1613 sizeof(tpr->pr_domainname));
1615 strlcpy(tpr->pr_hostuuid,
1617 sizeof(tpr->pr_hostuuid));
1619 tpr->pr_hostid = hid;
1623 if ((tallow = ch_allow & ~pr_allow)) {
1624 /* Clear allow bits in all children. */
1625 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1626 tpr->pr_allow &= ~tallow;
1628 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1630 * Persistent prisons get an extra reference, and prisons losing their
1631 * persist flag lose that reference. Only do this for existing prisons
1632 * for now, so new ones will remain unseen until after the module
1633 * handlers have completed.
1635 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1636 if (pr_flags & PR_PERSIST) {
1644 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1645 mtx_unlock(&pr->pr_mtx);
1647 /* Locks may have prevented a complete restriction of child IP
1648 * addresses. If so, allocate some more memory and try again.
1653 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1654 mtx_lock(&pr->pr_mtx);
1656 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1658 if (tpr->pr_flags & PR_VNET) {
1663 if (prison_restrict_ip4(tpr, ip4)) {
1670 mtx_unlock(&pr->pr_mtx);
1676 ip6 = malloc(ip6s * sizeof(*ip6), 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_ip6(tpr, ip6)) {
1693 mtx_unlock(&pr->pr_mtx);
1697 /* Let the modules do their work. */
1698 sx_downgrade(&allprison_lock);
1700 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1702 prison_deref(pr, PD_LIST_SLOCKED);
1706 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1708 prison_deref(pr, created
1710 : PD_DEREF | PD_LIST_SLOCKED);
1714 /* Attach this process to the prison if requested. */
1715 if (flags & JAIL_ATTACH) {
1716 mtx_lock(&pr->pr_mtx);
1717 error = do_jail_attach(td, pr);
1719 vfs_opterror(opts, "attach failed");
1721 prison_deref(pr, PD_DEREF);
1727 * Now that it is all there, drop the temporary reference from existing
1728 * prisons. Or add a reference to newly created persistent prisons
1729 * (which was not done earlier so that the prison would not be publicly
1733 prison_deref(pr, (flags & JAIL_ATTACH)
1735 : PD_DEREF | PD_LIST_SLOCKED);
1737 if (pr_flags & PR_PERSIST) {
1738 mtx_lock(&pr->pr_mtx);
1741 mtx_unlock(&pr->pr_mtx);
1743 if (!(flags & JAIL_ATTACH))
1744 sx_sunlock(&allprison_lock);
1746 td->td_retval[0] = pr->pr_id;
1750 prison_deref(pr, created
1751 ? PD_LOCKED | PD_LIST_XLOCKED
1752 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1755 sx_xunlock(&allprison_lock);
1758 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1760 VFS_UNLOCK_GIANT(vfslocked);
1764 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1765 if (errmsg_len > 0) {
1766 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1767 if (errmsg_pos > 0) {
1768 if (optuio->uio_segflg == UIO_SYSSPACE)
1770 optuio->uio_iov[errmsg_pos].iov_base,
1774 optuio->uio_iov[errmsg_pos].iov_base,
1781 free(ip4, M_PRISON);
1784 free(ip6, M_PRISON);
1792 * struct jail_get_args {
1793 * struct iovec *iovp;
1794 * unsigned int iovcnt;
1799 jail_get(struct thread *td, struct jail_get_args *uap)
1804 /* Check that we have an even number of iovecs. */
1805 if (uap->iovcnt & 1)
1808 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1811 error = kern_jail_get(td, auio, uap->flags);
1813 error = copyout(auio->uio_iov, uap->iovp,
1814 uap->iovcnt * sizeof (struct iovec));
1820 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1822 struct prison *pr, *mypr;
1824 struct vfsoptlist *opts;
1825 char *errmsg, *name;
1826 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1828 if (flags & ~JAIL_GET_MASK)
1831 /* Get the parameter list. */
1832 error = vfs_buildopts(optuio, &opts);
1835 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1836 mypr = td->td_ucred->cr_prison;
1839 * Find the prison specified by one of: lastjid, jid, name.
1841 sx_slock(&allprison_lock);
1842 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1844 TAILQ_FOREACH(pr, &allprison, pr_list) {
1845 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1846 mtx_lock(&pr->pr_mtx);
1847 if (pr->pr_ref > 0 &&
1848 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1850 mtx_unlock(&pr->pr_mtx);
1856 vfs_opterror(opts, "no jail after %d", jid);
1857 goto done_unlock_list;
1858 } else if (error != ENOENT)
1859 goto done_unlock_list;
1861 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1864 pr = prison_find_child(mypr, jid);
1866 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1867 mtx_unlock(&pr->pr_mtx);
1869 vfs_opterror(opts, "jail %d is dying",
1871 goto done_unlock_list;
1876 vfs_opterror(opts, "jail %d not found", jid);
1877 goto done_unlock_list;
1879 } else if (error != ENOENT)
1880 goto done_unlock_list;
1882 error = vfs_getopt(opts, "name", (void **)&name, &len);
1884 if (len == 0 || name[len - 1] != '\0') {
1886 goto done_unlock_list;
1888 pr = prison_find_name(mypr, name);
1890 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1891 mtx_unlock(&pr->pr_mtx);
1893 vfs_opterror(opts, "jail \"%s\" is dying",
1895 goto done_unlock_list;
1900 vfs_opterror(opts, "jail \"%s\" not found", name);
1901 goto done_unlock_list;
1902 } else if (error != ENOENT)
1903 goto done_unlock_list;
1905 vfs_opterror(opts, "no jail specified");
1907 goto done_unlock_list;
1910 /* Get the parameters of the prison. */
1913 td->td_retval[0] = pr->pr_id;
1914 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1915 if (error != 0 && error != ENOENT)
1917 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1918 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1919 if (error != 0 && error != ENOENT)
1921 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1922 if (error != 0 && error != ENOENT)
1924 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1925 sizeof(pr->pr_cpuset->cs_id));
1926 if (error != 0 && error != ENOENT)
1928 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1929 if (error != 0 && error != ENOENT)
1932 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1933 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1934 if (error != 0 && error != ENOENT)
1938 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1939 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1940 if (error != 0 && error != ENOENT)
1943 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1944 sizeof(pr->pr_securelevel));
1945 if (error != 0 && error != ENOENT)
1947 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1948 sizeof(pr->pr_childcount));
1949 if (error != 0 && error != ENOENT)
1951 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1952 sizeof(pr->pr_childmax));
1953 if (error != 0 && error != ENOENT)
1955 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1956 if (error != 0 && error != ENOENT)
1958 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1959 if (error != 0 && error != ENOENT)
1961 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1962 if (error != 0 && error != ENOENT)
1965 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1966 uint32_t hid32 = pr->pr_hostid;
1968 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1971 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1972 sizeof(pr->pr_hostid));
1973 if (error != 0 && error != ENOENT)
1975 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1976 sizeof(pr->pr_enforce_statfs));
1977 if (error != 0 && error != ENOENT)
1979 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1981 if (pr_flag_names[fi] == NULL)
1983 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1984 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1985 if (error != 0 && error != ENOENT)
1988 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1989 if (error != 0 && error != ENOENT)
1992 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1995 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1996 i = pr_flag_jailsys[fi].disable &&
1997 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1998 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2001 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2002 if (error != 0 && error != ENOENT)
2005 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2007 if (pr_allow_names[fi] == NULL)
2009 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2010 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2011 if (error != 0 && error != ENOENT)
2014 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2015 if (error != 0 && error != ENOENT)
2018 i = (pr->pr_uref == 0);
2019 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2020 if (error != 0 && error != ENOENT)
2023 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2024 if (error != 0 && error != ENOENT)
2027 /* Get the module parameters. */
2028 mtx_unlock(&pr->pr_mtx);
2030 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2033 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2035 /* By now, all parameters should have been noted. */
2036 TAILQ_FOREACH(opt, opts, link) {
2037 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2039 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2044 /* Write the fetched parameters back to userspace. */
2046 TAILQ_FOREACH(opt, opts, link) {
2047 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2048 pos = 2 * opt->pos + 1;
2049 optuio->uio_iov[pos].iov_len = opt->len;
2050 if (opt->value != NULL) {
2051 if (optuio->uio_segflg == UIO_SYSSPACE) {
2053 optuio->uio_iov[pos].iov_base,
2056 error = copyout(opt->value,
2057 optuio->uio_iov[pos].iov_base,
2068 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2072 sx_sunlock(&allprison_lock);
2074 if (error && errmsg_pos >= 0) {
2075 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2076 errmsg_pos = 2 * errmsg_pos + 1;
2077 if (errmsg_len > 0) {
2078 if (optuio->uio_segflg == UIO_SYSSPACE)
2080 optuio->uio_iov[errmsg_pos].iov_base,
2084 optuio->uio_iov[errmsg_pos].iov_base,
2094 * struct jail_remove_args {
2099 jail_remove(struct thread *td, struct jail_remove_args *uap)
2101 struct prison *pr, *cpr, *lpr, *tpr;
2104 error = priv_check(td, PRIV_JAIL_REMOVE);
2108 sx_xlock(&allprison_lock);
2109 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2111 sx_xunlock(&allprison_lock);
2115 /* Remove all descendants of this prison, then remove this prison. */
2117 pr->pr_flags |= PR_REMOVE;
2118 if (!LIST_EMPTY(&pr->pr_children)) {
2119 mtx_unlock(&pr->pr_mtx);
2121 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2122 mtx_lock(&cpr->pr_mtx);
2123 if (cpr->pr_ref > 0) {
2126 cpr->pr_flags |= PR_REMOVE;
2128 /* Already removed - do not do it again. */
2131 mtx_unlock(&cpr->pr_mtx);
2133 mtx_lock(&lpr->pr_mtx);
2134 prison_remove_one(lpr);
2135 sx_xlock(&allprison_lock);
2140 mtx_lock(&lpr->pr_mtx);
2141 prison_remove_one(lpr);
2142 sx_xlock(&allprison_lock);
2144 mtx_lock(&pr->pr_mtx);
2146 prison_remove_one(pr);
2151 prison_remove_one(struct prison *pr)
2156 /* If the prison was persistent, it is not anymore. */
2158 if (pr->pr_flags & PR_PERSIST) {
2161 pr->pr_flags &= ~PR_PERSIST;
2165 * jail_remove added a reference. If that's the only one, remove
2168 KASSERT(pr->pr_ref > 0,
2169 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2170 if (pr->pr_ref == 1) {
2172 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2176 mtx_unlock(&pr->pr_mtx);
2177 sx_xunlock(&allprison_lock);
2179 * Kill all processes unfortunate enough to be attached to this prison.
2181 sx_slock(&allproc_lock);
2182 LIST_FOREACH(p, &allproc, p_list) {
2184 if (p->p_state != PRS_NEW && p->p_ucred &&
2185 p->p_ucred->cr_prison == pr)
2186 psignal(p, SIGKILL);
2189 sx_sunlock(&allproc_lock);
2190 /* Remove the temporary reference added by jail_remove. */
2191 prison_deref(pr, deuref | PD_DEREF);
2196 * struct jail_attach_args {
2201 jail_attach(struct thread *td, struct jail_attach_args *uap)
2206 error = priv_check(td, PRIV_JAIL_ATTACH);
2210 sx_slock(&allprison_lock);
2211 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2213 sx_sunlock(&allprison_lock);
2218 * Do not allow a process to attach to a prison that is not
2219 * considered to be "alive".
2221 if (pr->pr_uref == 0) {
2222 mtx_unlock(&pr->pr_mtx);
2223 sx_sunlock(&allprison_lock);
2227 return (do_jail_attach(td, pr));
2231 do_jail_attach(struct thread *td, struct prison *pr)
2235 struct ucred *newcred, *oldcred;
2236 int vfslocked, error;
2239 * XXX: Note that there is a slight race here if two threads
2240 * in the same privileged process attempt to attach to two
2241 * different jails at the same time. It is important for
2242 * user processes not to do this, or they might end up with
2243 * a process root from one prison, but attached to the jail
2248 mtx_unlock(&pr->pr_mtx);
2250 /* Let modules do whatever they need to prepare for attaching. */
2251 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2253 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2256 sx_sunlock(&allprison_lock);
2259 * Reparent the newly attached process to this jail.
2261 ppr = td->td_ucred->cr_prison;
2263 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2267 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2268 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2269 if ((error = change_dir(pr->pr_root, td)) != 0)
2272 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2275 VOP_UNLOCK(pr->pr_root, 0);
2276 if ((error = change_root(pr->pr_root, td)))
2277 goto e_unlock_giant;
2278 VFS_UNLOCK_GIANT(vfslocked);
2282 oldcred = p->p_ucred;
2284 crcopy(newcred, oldcred);
2285 newcred->cr_prison = pr;
2286 p->p_ucred = newcred;
2289 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2292 VOP_UNLOCK(pr->pr_root, 0);
2294 VFS_UNLOCK_GIANT(vfslocked);
2296 /* Tell modules this thread is still in its old jail after all. */
2297 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2298 prison_deref(pr, PD_DEREF | PD_DEUREF);
2304 * Returns a locked prison instance, or NULL on failure.
2307 prison_find(int prid)
2311 sx_assert(&allprison_lock, SX_LOCKED);
2312 TAILQ_FOREACH(pr, &allprison, pr_list) {
2313 if (pr->pr_id == prid) {
2314 mtx_lock(&pr->pr_mtx);
2317 mtx_unlock(&pr->pr_mtx);
2324 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2327 prison_find_child(struct prison *mypr, int prid)
2332 sx_assert(&allprison_lock, SX_LOCKED);
2333 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2334 if (pr->pr_id == prid) {
2335 mtx_lock(&pr->pr_mtx);
2338 mtx_unlock(&pr->pr_mtx);
2345 * Look for the name relative to mypr. Returns a locked prison or NULL.
2348 prison_find_name(struct prison *mypr, const char *name)
2350 struct prison *pr, *deadpr;
2354 sx_assert(&allprison_lock, SX_LOCKED);
2355 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2358 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2359 if (!strcmp(pr->pr_name + mylen, name)) {
2360 mtx_lock(&pr->pr_mtx);
2361 if (pr->pr_ref > 0) {
2362 if (pr->pr_uref > 0)
2366 mtx_unlock(&pr->pr_mtx);
2369 /* There was no valid prison - perhaps there was a dying one. */
2370 if (deadpr != NULL) {
2371 mtx_lock(&deadpr->pr_mtx);
2372 if (deadpr->pr_ref == 0) {
2373 mtx_unlock(&deadpr->pr_mtx);
2381 * See if a prison has the specific flag set.
2384 prison_flag(struct ucred *cred, unsigned flag)
2387 /* This is an atomic read, so no locking is necessary. */
2388 return (cred->cr_prison->pr_flags & flag);
2392 prison_allow(struct ucred *cred, unsigned flag)
2395 /* This is an atomic read, so no locking is necessary. */
2396 return (cred->cr_prison->pr_allow & flag);
2400 * Remove a prison reference. If that was the last reference, remove the
2401 * prison itself - but not in this context in case there are locks held.
2404 prison_free_locked(struct prison *pr)
2407 mtx_assert(&pr->pr_mtx, MA_OWNED);
2409 if (pr->pr_ref == 0) {
2410 mtx_unlock(&pr->pr_mtx);
2411 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2412 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2415 mtx_unlock(&pr->pr_mtx);
2419 prison_free(struct prison *pr)
2422 mtx_lock(&pr->pr_mtx);
2423 prison_free_locked(pr);
2427 prison_complete(void *context, int pending)
2430 prison_deref((struct prison *)context, 0);
2434 * Remove a prison reference (usually). This internal version assumes no
2435 * mutexes are held, except perhaps the prison itself. If there are no more
2436 * references, release and delist the prison. On completion, the prison lock
2437 * and the allprison lock are both unlocked.
2440 prison_deref(struct prison *pr, int flags)
2442 struct prison *ppr, *tpr;
2445 if (!(flags & PD_LOCKED))
2446 mtx_lock(&pr->pr_mtx);
2447 /* Decrement the user references in a separate loop. */
2448 if (flags & PD_DEUREF) {
2449 for (tpr = pr;; tpr = tpr->pr_parent) {
2451 mtx_lock(&tpr->pr_mtx);
2452 if (--tpr->pr_uref > 0)
2454 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2455 mtx_unlock(&tpr->pr_mtx);
2457 /* Done if there were only user references to remove. */
2458 if (!(flags & PD_DEREF)) {
2459 mtx_unlock(&tpr->pr_mtx);
2460 if (flags & PD_LIST_SLOCKED)
2461 sx_sunlock(&allprison_lock);
2462 else if (flags & PD_LIST_XLOCKED)
2463 sx_xunlock(&allprison_lock);
2467 mtx_unlock(&tpr->pr_mtx);
2468 mtx_lock(&pr->pr_mtx);
2473 if (flags & PD_DEREF)
2475 /* If the prison still has references, nothing else to do. */
2476 if (pr->pr_ref > 0) {
2477 mtx_unlock(&pr->pr_mtx);
2478 if (flags & PD_LIST_SLOCKED)
2479 sx_sunlock(&allprison_lock);
2480 else if (flags & PD_LIST_XLOCKED)
2481 sx_xunlock(&allprison_lock);
2485 mtx_unlock(&pr->pr_mtx);
2486 if (flags & PD_LIST_SLOCKED) {
2487 if (!sx_try_upgrade(&allprison_lock)) {
2488 sx_sunlock(&allprison_lock);
2489 sx_xlock(&allprison_lock);
2491 } else if (!(flags & PD_LIST_XLOCKED))
2492 sx_xlock(&allprison_lock);
2494 TAILQ_REMOVE(&allprison, pr, pr_list);
2495 LIST_REMOVE(pr, pr_sibling);
2496 ppr = pr->pr_parent;
2497 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2498 tpr->pr_childcount--;
2499 sx_xunlock(&allprison_lock);
2502 if (pr->pr_vnet != ppr->pr_vnet)
2503 vnet_destroy(pr->pr_vnet);
2505 if (pr->pr_root != NULL) {
2506 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2508 VFS_UNLOCK_GIANT(vfslocked);
2510 mtx_destroy(&pr->pr_mtx);
2512 free(pr->pr_ip4, M_PRISON);
2515 free(pr->pr_ip6, M_PRISON);
2517 if (pr->pr_cpuset != NULL)
2518 cpuset_rel(pr->pr_cpuset);
2522 /* Removing a prison frees a reference on its parent. */
2524 mtx_lock(&pr->pr_mtx);
2530 prison_hold_locked(struct prison *pr)
2533 mtx_assert(&pr->pr_mtx, MA_OWNED);
2534 KASSERT(pr->pr_ref > 0,
2535 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2540 prison_hold(struct prison *pr)
2543 mtx_lock(&pr->pr_mtx);
2544 prison_hold_locked(pr);
2545 mtx_unlock(&pr->pr_mtx);
2549 prison_proc_hold(struct prison *pr)
2552 mtx_lock(&pr->pr_mtx);
2553 KASSERT(pr->pr_uref > 0,
2554 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2556 mtx_unlock(&pr->pr_mtx);
2560 prison_proc_free(struct prison *pr)
2563 mtx_lock(&pr->pr_mtx);
2564 KASSERT(pr->pr_uref > 0,
2565 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2566 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2572 * Restrict a prison's IP address list with its parent's, possibly replacing
2573 * it. Return true if the replacement buffer was used (or would have been).
2576 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2581 ppr = pr->pr_parent;
2582 if (!(pr->pr_flags & PR_IP4_USER)) {
2583 /* This has no user settings, so just copy the parent's list. */
2584 if (pr->pr_ip4s < ppr->pr_ip4s) {
2586 * There's no room for the parent's list. Use the
2587 * new list buffer, which is assumed to be big enough
2588 * (if it was passed). If there's no buffer, try to
2592 if (newip4 == NULL) {
2593 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2594 M_PRISON, M_NOWAIT);
2598 if (newip4 != NULL) {
2599 bcopy(ppr->pr_ip4, newip4,
2600 ppr->pr_ip4s * sizeof(*newip4));
2601 free(pr->pr_ip4, M_PRISON);
2602 pr->pr_ip4 = newip4;
2603 pr->pr_ip4s = ppr->pr_ip4s;
2607 pr->pr_ip4s = ppr->pr_ip4s;
2608 if (pr->pr_ip4s > 0)
2609 bcopy(ppr->pr_ip4, pr->pr_ip4,
2610 pr->pr_ip4s * sizeof(*newip4));
2611 else if (pr->pr_ip4 != NULL) {
2612 free(pr->pr_ip4, M_PRISON);
2615 } else if (pr->pr_ip4s > 0) {
2616 /* Remove addresses that aren't in the parent. */
2617 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2618 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2620 if (ij < ppr->pr_ip4s)
2623 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2624 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2627 for (ij = 1; ii < pr->pr_ip4s; ) {
2628 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2632 switch (ij >= ppr->pr_ip4s ? -1 :
2633 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2635 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2636 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2647 if (pr->pr_ip4s == 0) {
2648 pr->pr_flags |= PR_IP4_DISABLE;
2649 free(pr->pr_ip4, M_PRISON);
2657 * Pass back primary IPv4 address of this jail.
2659 * If not restricted return success but do not alter the address. Caller has
2660 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2662 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2663 * Address returned in NBO.
2666 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2670 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2671 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2673 pr = cred->cr_prison;
2674 if (!(pr->pr_flags & PR_IP4))
2676 mtx_lock(&pr->pr_mtx);
2677 if (!(pr->pr_flags & PR_IP4)) {
2678 mtx_unlock(&pr->pr_mtx);
2681 if (pr->pr_ip4 == NULL) {
2682 mtx_unlock(&pr->pr_mtx);
2683 return (EAFNOSUPPORT);
2686 ia->s_addr = pr->pr_ip4[0].s_addr;
2687 mtx_unlock(&pr->pr_mtx);
2692 * Return 1 if we should do proper source address selection or are not jailed.
2693 * We will return 0 if we should bypass source address selection in favour
2694 * of the primary jail IPv4 address. Only in this case *ia will be updated and
2696 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2699 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2705 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2706 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2711 pr = cred->cr_prison;
2712 if (pr->pr_flags & PR_IP4_SADDRSEL)
2715 lia.s_addr = INADDR_ANY;
2716 error = prison_get_ip4(cred, &lia);
2719 if (lia.s_addr == INADDR_ANY)
2722 ia->s_addr = lia.s_addr;
2727 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2730 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2737 * No need to lock since the PR_IP4_USER flag can't be altered for
2740 while (pr1 != &prison0 &&
2742 !(pr1->pr_flags & PR_VNET) &&
2744 !(pr1->pr_flags & PR_IP4_USER))
2745 pr1 = pr1->pr_parent;
2746 while (pr2 != &prison0 &&
2748 !(pr2->pr_flags & PR_VNET) &&
2750 !(pr2->pr_flags & PR_IP4_USER))
2751 pr2 = pr2->pr_parent;
2752 return (pr1 == pr2);
2756 * Make sure our (source) address is set to something meaningful to this
2759 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2760 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2761 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2764 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2770 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2771 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2773 pr = cred->cr_prison;
2774 if (!(pr->pr_flags & PR_IP4))
2776 mtx_lock(&pr->pr_mtx);
2777 if (!(pr->pr_flags & PR_IP4)) {
2778 mtx_unlock(&pr->pr_mtx);
2781 if (pr->pr_ip4 == NULL) {
2782 mtx_unlock(&pr->pr_mtx);
2783 return (EAFNOSUPPORT);
2786 ia0.s_addr = ntohl(ia->s_addr);
2787 if (ia0.s_addr == INADDR_LOOPBACK) {
2788 ia->s_addr = pr->pr_ip4[0].s_addr;
2789 mtx_unlock(&pr->pr_mtx);
2793 if (ia0.s_addr == INADDR_ANY) {
2795 * In case there is only 1 IPv4 address, bind directly.
2797 if (pr->pr_ip4s == 1)
2798 ia->s_addr = pr->pr_ip4[0].s_addr;
2799 mtx_unlock(&pr->pr_mtx);
2803 error = _prison_check_ip4(pr, ia);
2804 mtx_unlock(&pr->pr_mtx);
2809 * Rewrite destination address in case we will connect to loopback address.
2811 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2812 * Address passed in in NBO and returned in NBO.
2815 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2819 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2820 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2822 pr = cred->cr_prison;
2823 if (!(pr->pr_flags & PR_IP4))
2825 mtx_lock(&pr->pr_mtx);
2826 if (!(pr->pr_flags & PR_IP4)) {
2827 mtx_unlock(&pr->pr_mtx);
2830 if (pr->pr_ip4 == NULL) {
2831 mtx_unlock(&pr->pr_mtx);
2832 return (EAFNOSUPPORT);
2835 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2836 ia->s_addr = pr->pr_ip4[0].s_addr;
2837 mtx_unlock(&pr->pr_mtx);
2842 * Return success because nothing had to be changed.
2844 mtx_unlock(&pr->pr_mtx);
2849 * Check if given address belongs to the jail referenced by cred/prison.
2851 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2852 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2853 * doesn't allow IPv4. Address passed in in NBO.
2856 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2861 * Check the primary IP.
2863 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2867 * All the other IPs are sorted so we can do a binary search.
2870 z = pr->pr_ip4s - 2;
2873 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2882 return (EADDRNOTAVAIL);
2886 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2891 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2892 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2894 pr = cred->cr_prison;
2895 if (!(pr->pr_flags & PR_IP4))
2897 mtx_lock(&pr->pr_mtx);
2898 if (!(pr->pr_flags & PR_IP4)) {
2899 mtx_unlock(&pr->pr_mtx);
2902 if (pr->pr_ip4 == NULL) {
2903 mtx_unlock(&pr->pr_mtx);
2904 return (EAFNOSUPPORT);
2907 error = _prison_check_ip4(pr, ia);
2908 mtx_unlock(&pr->pr_mtx);
2915 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2920 ppr = pr->pr_parent;
2921 if (!(pr->pr_flags & PR_IP6_USER)) {
2922 /* This has no user settings, so just copy the parent's list. */
2923 if (pr->pr_ip6s < ppr->pr_ip6s) {
2925 * There's no room for the parent's list. Use the
2926 * new list buffer, which is assumed to be big enough
2927 * (if it was passed). If there's no buffer, try to
2931 if (newip6 == NULL) {
2932 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2933 M_PRISON, M_NOWAIT);
2937 if (newip6 != NULL) {
2938 bcopy(ppr->pr_ip6, newip6,
2939 ppr->pr_ip6s * sizeof(*newip6));
2940 free(pr->pr_ip6, M_PRISON);
2941 pr->pr_ip6 = newip6;
2942 pr->pr_ip6s = ppr->pr_ip6s;
2946 pr->pr_ip6s = ppr->pr_ip6s;
2947 if (pr->pr_ip6s > 0)
2948 bcopy(ppr->pr_ip6, pr->pr_ip6,
2949 pr->pr_ip6s * sizeof(*newip6));
2950 else if (pr->pr_ip6 != NULL) {
2951 free(pr->pr_ip6, M_PRISON);
2954 } else if (pr->pr_ip6s > 0) {
2955 /* Remove addresses that aren't in the parent. */
2956 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2957 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2960 if (ij < ppr->pr_ip6s)
2963 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2964 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2967 for (ij = 1; ii < pr->pr_ip6s; ) {
2968 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2973 switch (ij >= ppr->pr_ip4s ? -1 :
2974 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2976 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2977 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2988 if (pr->pr_ip6s == 0) {
2989 pr->pr_flags |= PR_IP6_DISABLE;
2990 free(pr->pr_ip6, M_PRISON);
2998 * Pass back primary IPv6 address for this jail.
3000 * If not restricted return success but do not alter the address. Caller has
3001 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3003 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3006 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3010 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3011 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3013 pr = cred->cr_prison;
3014 if (!(pr->pr_flags & PR_IP6))
3016 mtx_lock(&pr->pr_mtx);
3017 if (!(pr->pr_flags & PR_IP6)) {
3018 mtx_unlock(&pr->pr_mtx);
3021 if (pr->pr_ip6 == NULL) {
3022 mtx_unlock(&pr->pr_mtx);
3023 return (EAFNOSUPPORT);
3026 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3027 mtx_unlock(&pr->pr_mtx);
3032 * Return 1 if we should do proper source address selection or are not jailed.
3033 * We will return 0 if we should bypass source address selection in favour
3034 * of the primary jail IPv6 address. Only in this case *ia will be updated and
3036 * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3039 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3042 struct in6_addr lia6;
3045 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3046 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3051 pr = cred->cr_prison;
3052 if (pr->pr_flags & PR_IP6_SADDRSEL)
3056 error = prison_get_ip6(cred, &lia6);
3059 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3062 bcopy(&lia6, ia6, sizeof(struct in6_addr));
3067 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3070 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
3076 while (pr1 != &prison0 &&
3078 !(pr1->pr_flags & PR_VNET) &&
3080 !(pr1->pr_flags & PR_IP6_USER))
3081 pr1 = pr1->pr_parent;
3082 while (pr2 != &prison0 &&
3084 !(pr2->pr_flags & PR_VNET) &&
3086 !(pr2->pr_flags & PR_IP6_USER))
3087 pr2 = pr2->pr_parent;
3088 return (pr1 == pr2);
3092 * Make sure our (source) address is set to something meaningful to this jail.
3094 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3095 * when needed while binding.
3097 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3098 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3099 * doesn't allow IPv6.
3102 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3107 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3108 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3110 pr = cred->cr_prison;
3111 if (!(pr->pr_flags & PR_IP6))
3113 mtx_lock(&pr->pr_mtx);
3114 if (!(pr->pr_flags & PR_IP6)) {
3115 mtx_unlock(&pr->pr_mtx);
3118 if (pr->pr_ip6 == NULL) {
3119 mtx_unlock(&pr->pr_mtx);
3120 return (EAFNOSUPPORT);
3123 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3124 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3125 mtx_unlock(&pr->pr_mtx);
3129 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3131 * In case there is only 1 IPv6 address, and v6only is true,
3132 * then bind directly.
3134 if (v6only != 0 && pr->pr_ip6s == 1)
3135 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3136 mtx_unlock(&pr->pr_mtx);
3140 error = _prison_check_ip6(pr, ia6);
3141 mtx_unlock(&pr->pr_mtx);
3146 * Rewrite destination address in case we will connect to loopback address.
3148 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3151 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3155 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3156 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3158 pr = cred->cr_prison;
3159 if (!(pr->pr_flags & PR_IP6))
3161 mtx_lock(&pr->pr_mtx);
3162 if (!(pr->pr_flags & PR_IP6)) {
3163 mtx_unlock(&pr->pr_mtx);
3166 if (pr->pr_ip6 == NULL) {
3167 mtx_unlock(&pr->pr_mtx);
3168 return (EAFNOSUPPORT);
3171 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3172 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3173 mtx_unlock(&pr->pr_mtx);
3178 * Return success because nothing had to be changed.
3180 mtx_unlock(&pr->pr_mtx);
3185 * Check if given address belongs to the jail referenced by cred/prison.
3187 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3188 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3189 * doesn't allow IPv6.
3192 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3197 * Check the primary IP.
3199 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3203 * All the other IPs are sorted so we can do a binary search.
3206 z = pr->pr_ip6s - 2;
3209 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3218 return (EADDRNOTAVAIL);
3222 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3227 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3228 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3230 pr = cred->cr_prison;
3231 if (!(pr->pr_flags & PR_IP6))
3233 mtx_lock(&pr->pr_mtx);
3234 if (!(pr->pr_flags & PR_IP6)) {
3235 mtx_unlock(&pr->pr_mtx);
3238 if (pr->pr_ip6 == NULL) {
3239 mtx_unlock(&pr->pr_mtx);
3240 return (EAFNOSUPPORT);
3243 error = _prison_check_ip6(pr, ia6);
3244 mtx_unlock(&pr->pr_mtx);
3250 * Check if a jail supports the given address family.
3252 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3256 prison_check_af(struct ucred *cred, int af)
3261 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3263 pr = cred->cr_prison;
3265 /* Prisons with their own network stack are not limited. */
3266 if (prison_owns_vnet(cred))
3275 if (pr->pr_flags & PR_IP4)
3277 mtx_lock(&pr->pr_mtx);
3278 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3279 error = EAFNOSUPPORT;
3280 mtx_unlock(&pr->pr_mtx);
3286 if (pr->pr_flags & PR_IP6)
3288 mtx_lock(&pr->pr_mtx);
3289 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3290 error = EAFNOSUPPORT;
3291 mtx_unlock(&pr->pr_mtx);
3299 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3300 error = EAFNOSUPPORT;
3306 * Check if given address belongs to the jail referenced by cred (wrapper to
3307 * prison_check_ip[46]).
3309 * Returns 0 if jail doesn't restrict the address family or if address belongs
3310 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3311 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3314 prison_if(struct ucred *cred, struct sockaddr *sa)
3317 struct sockaddr_in *sai;
3320 struct sockaddr_in6 *sai6;
3324 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3325 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3328 if (prison_owns_vnet(cred))
3333 switch (sa->sa_family)
3337 sai = (struct sockaddr_in *)sa;
3338 error = prison_check_ip4(cred, &sai->sin_addr);
3343 sai6 = (struct sockaddr_in6 *)sa;
3344 error = prison_check_ip6(cred, &sai6->sin6_addr);
3348 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3349 error = EAFNOSUPPORT;
3355 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3358 prison_check(struct ucred *cred1, struct ucred *cred2)
3361 return ((cred1->cr_prison == cred2->cr_prison ||
3362 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3366 * Return 1 if p2 is a child of p1, otherwise 0.
3369 prison_ischild(struct prison *pr1, struct prison *pr2)
3372 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3379 * Return 1 if the passed credential is in a jail, otherwise 0.
3382 jailed(struct ucred *cred)
3385 return (cred->cr_prison != &prison0);
3389 * Return 1 if the passed credential is in a jail and that jail does not
3390 * have its own virtual network stack, otherwise 0.
3393 jailed_without_vnet(struct ucred *cred)
3399 if (prison_owns_vnet(cred))
3407 * Return the correct hostname (domainname, et al) for the passed credential.
3410 getcredhostname(struct ucred *cred, char *buf, size_t size)
3415 * A NULL credential can be used to shortcut to the physical
3416 * system's hostname.
3418 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3419 mtx_lock(&pr->pr_mtx);
3420 strlcpy(buf, pr->pr_hostname, size);
3421 mtx_unlock(&pr->pr_mtx);
3425 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3428 mtx_lock(&cred->cr_prison->pr_mtx);
3429 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3430 mtx_unlock(&cred->cr_prison->pr_mtx);
3434 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3437 mtx_lock(&cred->cr_prison->pr_mtx);
3438 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3439 mtx_unlock(&cred->cr_prison->pr_mtx);
3443 getcredhostid(struct ucred *cred, unsigned long *hostid)
3446 mtx_lock(&cred->cr_prison->pr_mtx);
3447 *hostid = cred->cr_prison->pr_hostid;
3448 mtx_unlock(&cred->cr_prison->pr_mtx);
3453 * Determine whether the prison represented by cred owns
3454 * its vnet rather than having it inherited.
3456 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3459 prison_owns_vnet(struct ucred *cred)
3463 * vnets cannot be added/removed after jail creation,
3464 * so no need to lock here.
3466 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3471 * Determine whether the subject represented by cred can "see"
3472 * status of a mount point.
3473 * Returns: 0 for permitted, ENOENT otherwise.
3474 * XXX: This function should be called cr_canseemount() and should be
3475 * placed in kern_prot.c.
3478 prison_canseemount(struct ucred *cred, struct mount *mp)
3484 pr = cred->cr_prison;
3485 if (pr->pr_enforce_statfs == 0)
3487 if (pr->pr_root->v_mount == mp)
3489 if (pr->pr_enforce_statfs == 2)
3492 * If jail's chroot directory is set to "/" we should be able to see
3493 * all mount-points from inside a jail.
3494 * This is ugly check, but this is the only situation when jail's
3495 * directory ends with '/'.
3497 if (strcmp(pr->pr_path, "/") == 0)
3499 len = strlen(pr->pr_path);
3501 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3504 * Be sure that we don't have situation where jail's root directory
3505 * is "/some/path" and mount point is "/some/pathpath".
3507 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3513 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3515 char jpath[MAXPATHLEN];
3519 pr = cred->cr_prison;
3520 if (pr->pr_enforce_statfs == 0)
3522 if (prison_canseemount(cred, mp) != 0) {
3523 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3524 strlcpy(sp->f_mntonname, "[restricted]",
3525 sizeof(sp->f_mntonname));
3528 if (pr->pr_root->v_mount == mp) {
3530 * Clear current buffer data, so we are sure nothing from
3531 * the valid path left there.
3533 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3534 *sp->f_mntonname = '/';
3538 * If jail's chroot directory is set to "/" we should be able to see
3539 * all mount-points from inside a jail.
3541 if (strcmp(pr->pr_path, "/") == 0)
3543 len = strlen(pr->pr_path);
3544 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3546 * Clear current buffer data, so we are sure nothing from
3547 * the valid path left there.
3549 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3550 if (*jpath == '\0') {
3551 /* Should never happen. */
3552 *sp->f_mntonname = '/';
3554 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3559 * Check with permission for a specific privilege is granted within jail. We
3560 * have a specific list of accepted privileges; the rest are denied.
3563 prison_priv_check(struct ucred *cred, int priv)
3571 * Privileges specific to prisons with a virtual network stack.
3572 * There might be a duplicate entry here in case the privilege
3573 * is only granted conditionally in the legacy jail case.
3578 * NFS-specific privileges.
3580 case PRIV_NFS_DAEMON:
3581 case PRIV_NFS_LOCKD:
3584 * Network stack privileges.
3586 case PRIV_NET_BRIDGE:
3589 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3590 case PRIV_NET_ROUTE:
3592 case PRIV_NET_SETIFMTU:
3593 case PRIV_NET_SETIFFLAGS:
3594 case PRIV_NET_SETIFCAP:
3595 case PRIV_NET_SETIFDESCR:
3596 case PRIV_NET_SETIFNAME :
3597 case PRIV_NET_SETIFMETRIC:
3598 case PRIV_NET_SETIFPHYS:
3599 case PRIV_NET_SETIFMAC:
3600 case PRIV_NET_ADDMULTI:
3601 case PRIV_NET_DELMULTI:
3602 case PRIV_NET_HWIOCTL:
3603 case PRIV_NET_SETLLADDR:
3604 case PRIV_NET_ADDIFGROUP:
3605 case PRIV_NET_DELIFGROUP:
3606 case PRIV_NET_IFCREATE:
3607 case PRIV_NET_IFDESTROY:
3608 case PRIV_NET_ADDIFADDR:
3609 case PRIV_NET_DELIFADDR:
3612 case PRIV_NET_SETIFVNET:
3615 * 802.11-related privileges.
3617 case PRIV_NET80211_GETKEY:
3619 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3624 * AppleTalk privileges.
3626 case PRIV_NETATALK_RESERVEDPORT:
3631 case PRIV_NETATM_CFG:
3632 case PRIV_NETATM_ADD:
3633 case PRIV_NETATM_DEL:
3634 case PRIV_NETATM_SET:
3637 * Bluetooth privileges.
3639 case PRIV_NETBLUETOOTH_RAW:
3643 * Netgraph and netgraph module privileges.
3645 case PRIV_NETGRAPH_CONTROL:
3647 case PRIV_NETGRAPH_TTY:
3651 * IPv4 and IPv6 privileges.
3653 case PRIV_NETINET_IPFW:
3654 case PRIV_NETINET_DIVERT:
3655 case PRIV_NETINET_PF:
3656 case PRIV_NETINET_DUMMYNET:
3657 case PRIV_NETINET_CARP:
3658 case PRIV_NETINET_MROUTE:
3659 case PRIV_NETINET_RAW:
3660 case PRIV_NETINET_ADDRCTRL6:
3661 case PRIV_NETINET_ND6:
3662 case PRIV_NETINET_SCOPE6:
3663 case PRIV_NETINET_ALIFETIME6:
3664 case PRIV_NETINET_IPSEC:
3665 case PRIV_NETINET_BINDANY:
3669 * IPX/SPX privileges.
3671 case PRIV_NETIPX_RESERVEDPORT:
3672 case PRIV_NETIPX_RAW:
3686 * No default: or deny here.
3687 * In case of no permit fall through to next switch().
3689 if (cred->cr_prison->pr_flags & PR_VNET)
3697 * Allow ktrace privileges for root in jail.
3703 * Allow jailed processes to configure audit identity and
3704 * submit audit records (login, etc). In the future we may
3705 * want to further refine the relationship between audit and
3708 case PRIV_AUDIT_GETAUDIT:
3709 case PRIV_AUDIT_SETAUDIT:
3710 case PRIV_AUDIT_SUBMIT:
3714 * Allow jailed processes to manipulate process UNIX
3715 * credentials in any way they see fit.
3717 case PRIV_CRED_SETUID:
3718 case PRIV_CRED_SETEUID:
3719 case PRIV_CRED_SETGID:
3720 case PRIV_CRED_SETEGID:
3721 case PRIV_CRED_SETGROUPS:
3722 case PRIV_CRED_SETREUID:
3723 case PRIV_CRED_SETREGID:
3724 case PRIV_CRED_SETRESUID:
3725 case PRIV_CRED_SETRESGID:
3728 * Jail implements visibility constraints already, so allow
3729 * jailed root to override uid/gid-based constraints.
3731 case PRIV_SEEOTHERGIDS:
3732 case PRIV_SEEOTHERUIDS:
3735 * Jail implements inter-process debugging limits already, so
3736 * allow jailed root various debugging privileges.
3738 case PRIV_DEBUG_DIFFCRED:
3739 case PRIV_DEBUG_SUGID:
3740 case PRIV_DEBUG_UNPRIV:
3743 * Allow jail to set various resource limits and login
3744 * properties, and for now, exceed process resource limits.
3746 case PRIV_PROC_LIMIT:
3747 case PRIV_PROC_SETLOGIN:
3748 case PRIV_PROC_SETRLIMIT:
3751 * System V and POSIX IPC privileges are granted in jail.
3754 case PRIV_IPC_WRITE:
3755 case PRIV_IPC_ADMIN:
3756 case PRIV_IPC_MSGSIZE:
3760 * Jail operations within a jail work on child jails.
3762 case PRIV_JAIL_ATTACH:
3764 case PRIV_JAIL_REMOVE:
3767 * Jail implements its own inter-process limits, so allow
3768 * root processes in jail to change scheduling on other
3769 * processes in the same jail. Likewise for signalling.
3771 case PRIV_SCHED_DIFFCRED:
3772 case PRIV_SCHED_CPUSET:
3773 case PRIV_SIGNAL_DIFFCRED:
3774 case PRIV_SIGNAL_SUGID:
3777 * Allow jailed processes to write to sysctls marked as jail
3780 case PRIV_SYSCTL_WRITEJAIL:
3783 * Allow root in jail to manage a variety of quota
3784 * properties. These should likely be conditional on a
3785 * configuration option.
3787 case PRIV_VFS_GETQUOTA:
3788 case PRIV_VFS_SETQUOTA:
3791 * Since Jail relies on chroot() to implement file system
3792 * protections, grant many VFS privileges to root in jail.
3793 * Be careful to exclude mount-related and NFS-related
3797 case PRIV_VFS_WRITE:
3798 case PRIV_VFS_ADMIN:
3800 case PRIV_VFS_LOOKUP:
3801 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3802 case PRIV_VFS_CHFLAGS_DEV:
3803 case PRIV_VFS_CHOWN:
3804 case PRIV_VFS_CHROOT:
3805 case PRIV_VFS_RETAINSUGID:
3806 case PRIV_VFS_FCHROOT:
3808 case PRIV_VFS_SETGID:
3810 case PRIV_VFS_STICKYFILE:
3814 * Depending on the global setting, allow privilege of
3815 * setting system flags.
3817 case PRIV_VFS_SYSFLAGS:
3818 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3824 * Depending on the global setting, allow privilege of
3825 * mounting/unmounting file systems.
3827 case PRIV_VFS_MOUNT:
3828 case PRIV_VFS_UNMOUNT:
3829 case PRIV_VFS_MOUNT_NONUSER:
3830 case PRIV_VFS_MOUNT_OWNER:
3831 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3837 * Allow jailed root to bind reserved ports and reuse in-use
3840 case PRIV_NETINET_RESERVEDPORT:
3841 case PRIV_NETINET_REUSEPORT:
3845 * Allow jailed root to set certian IPv4/6 (option) headers.
3847 case PRIV_NETINET_SETHDROPTS:
3851 * Conditionally allow creating raw sockets in jail.
3853 case PRIV_NETINET_RAW:
3854 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3860 * Since jail implements its own visibility limits on netstat
3861 * sysctls, allow getcred. This allows identd to work in
3864 case PRIV_NETINET_GETCRED:
3869 * In all remaining cases, deny the privilege request. This
3870 * includes almost all network privileges, many system
3871 * configuration privileges.
3878 * Return the part of pr2's name that is relative to pr1, or the whole name
3879 * if it does not directly follow.
3883 prison_name(struct prison *pr1, struct prison *pr2)
3887 /* Jails see themselves as "0" (if they see themselves at all). */
3890 name = pr2->pr_name;
3891 if (prison_ischild(pr1, pr2)) {
3893 * pr1 isn't locked (and allprison_lock may not be either)
3894 * so its length can't be counted on. But the number of dots
3895 * can be counted on - and counted.
3897 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3898 name = strchr(name, '.') + 1;
3904 * Return the part of pr2's path that is relative to pr1, or the whole path
3905 * if it does not directly follow.
3908 prison_path(struct prison *pr1, struct prison *pr2)
3910 char *path1, *path2;
3913 path1 = pr1->pr_path;
3914 path2 = pr2->pr_path;
3915 if (!strcmp(path1, "/"))
3917 len1 = strlen(path1);
3918 if (strncmp(path1, path2, len1))
3920 if (path2[len1] == '\0')
3922 if (path2[len1] == '/')
3923 return (path2 + len1);
3929 * Jail-related sysctls.
3931 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3935 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3938 struct prison *pr, *cpr;
3940 struct in_addr *ip4 = NULL;
3944 struct in_addr *ip6 = NULL;
3949 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3950 pr = req->td->td_ucred->cr_prison;
3952 sx_slock(&allprison_lock);
3953 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3954 #if defined(INET) || defined(INET6)
3957 mtx_lock(&cpr->pr_mtx);
3959 if (cpr->pr_ip4s > 0) {
3960 if (ip4s < cpr->pr_ip4s) {
3961 ip4s = cpr->pr_ip4s;
3962 mtx_unlock(&cpr->pr_mtx);
3963 ip4 = realloc(ip4, ip4s *
3964 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3967 bcopy(cpr->pr_ip4, ip4,
3968 cpr->pr_ip4s * sizeof(struct in_addr));
3972 if (cpr->pr_ip6s > 0) {
3973 if (ip6s < cpr->pr_ip6s) {
3974 ip6s = cpr->pr_ip6s;
3975 mtx_unlock(&cpr->pr_mtx);
3976 ip6 = realloc(ip6, ip6s *
3977 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3980 bcopy(cpr->pr_ip6, ip6,
3981 cpr->pr_ip6s * sizeof(struct in6_addr));
3984 if (cpr->pr_ref == 0) {
3985 mtx_unlock(&cpr->pr_mtx);
3988 bzero(xp, sizeof(*xp));
3989 xp->pr_version = XPRISON_VERSION;
3990 xp->pr_id = cpr->pr_id;
3991 xp->pr_state = cpr->pr_uref > 0
3992 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3993 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3994 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3995 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3997 xp->pr_ip4s = cpr->pr_ip4s;
4000 xp->pr_ip6s = cpr->pr_ip6s;
4002 mtx_unlock(&cpr->pr_mtx);
4003 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4007 if (xp->pr_ip4s > 0) {
4008 error = SYSCTL_OUT(req, ip4,
4009 xp->pr_ip4s * sizeof(struct in_addr));
4015 if (xp->pr_ip6s > 0) {
4016 error = SYSCTL_OUT(req, ip6,
4017 xp->pr_ip6s * sizeof(struct in6_addr));
4023 sx_sunlock(&allprison_lock);
4034 SYSCTL_OID(_security_jail, OID_AUTO, list,
4035 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4036 sysctl_jail_list, "S", "List of active jails");
4039 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4043 injail = jailed(req->td->td_ucred);
4044 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4049 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4050 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4051 sysctl_jail_jailed, "I", "Process in jail?");
4053 #if defined(INET) || defined(INET6)
4054 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4055 &jail_max_af_ips, 0,
4056 "Number of IP addresses a jail may have at most per address family");
4060 * Default parameters for jail(2) compatability. For historical reasons,
4061 * the sysctl names have varying similarity to the parameter names. Prisons
4062 * just see their own parameters, and can't change them.
4065 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4068 int allow, error, i;
4070 pr = req->td->td_ucred->cr_prison;
4071 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4073 /* Get the current flag value, and convert it to a boolean. */
4074 i = (allow & arg2) ? 1 : 0;
4077 error = sysctl_handle_int(oidp, &i, 0, req);
4078 if (error || !req->newptr)
4084 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4087 mtx_lock(&prison0.pr_mtx);
4088 jail_default_allow = (jail_default_allow & ~arg2) | i;
4089 mtx_unlock(&prison0.pr_mtx);
4093 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4094 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4095 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4096 "Processes in jail can set their hostnames");
4097 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4098 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4099 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4100 "Processes in jail are limited to creating UNIX/IP/route sockets only");
4101 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4102 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4103 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4104 "Processes in jail can use System V IPC primitives");
4105 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4106 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4107 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4108 "Prison root can create raw sockets");
4109 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4110 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4111 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4112 "Processes in jail can alter system file flags");
4113 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4114 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4115 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4116 "Processes in jail can mount/unmount jail-friendly file systems");
4119 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4124 pr = req->td->td_ucred->cr_prison;
4125 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4126 error = sysctl_handle_int(oidp, &level, 0, req);
4127 if (error || !req->newptr)
4129 *(int *)arg1 = level;
4133 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4134 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4135 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4136 sysctl_jail_default_level, "I",
4137 "Processes in jail cannot see all mounted file systems");
4140 * Nodes to describe jail parameters. Maximum length of string parameters
4141 * is returned in the string itself, and the other parameters exist merely
4142 * to make themselves and their types known.
4144 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4148 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4155 switch (oidp->oid_kind & CTLTYPE)
4161 if (!(req->flags & SCTL_MASK32))
4163 return (SYSCTL_OUT(req, &l, sizeof(l)));
4167 return (SYSCTL_OUT(req, &i, sizeof(i)));
4168 case CTLTYPE_STRING:
4169 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4171 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4172 case CTLTYPE_STRUCT:
4174 return (SYSCTL_OUT(req, &s, sizeof(s)));
4179 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4180 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4181 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4182 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4183 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4184 "I", "Jail secure level");
4185 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4186 "I", "Jail cannot see all mounted file systems");
4187 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4188 "B", "Jail persistence");
4190 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4191 "E,jailsys", "Virtual network stack");
4193 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4194 "B", "Jail is in the process of shutting down");
4196 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4197 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4198 "I", "Current number of child jails");
4199 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4200 "I", "Maximum number of child jails");
4202 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4203 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4205 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4206 "Jail NIS domainname");
4207 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4209 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4210 "LU", "Jail host ID");
4212 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4213 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4216 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4217 "Jail IPv4 address virtualization");
4218 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4219 "S,in_addr,a", "Jail IPv4 addresses");
4220 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4221 "B", "Do (not) use IPv4 source address selection rather than the "
4222 "primary jail IPv4 address.");
4225 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4226 "Jail IPv6 address virtualization");
4227 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4228 "S,in6_addr,a", "Jail IPv6 addresses");
4229 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4230 "B", "Do (not) use IPv6 source address selection rather than the "
4231 "primary jail IPv6 address.");
4234 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4235 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4236 "B", "Jail may set hostname");
4237 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4238 "B", "Jail may use SYSV IPC");
4239 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4240 "B", "Jail may create raw sockets");
4241 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4242 "B", "Jail may alter system file flags");
4243 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4244 "B", "Jail may mount/unmount jail-friendly file systems");
4245 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4246 "B", "Jail may set file quotas");
4247 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4248 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4254 db_show_prison(struct prison *pr)
4257 #if defined(INET) || defined(INET6)
4262 char ip6buf[INET6_ADDRSTRLEN];
4265 db_printf("prison %p:\n", pr);
4266 db_printf(" jid = %d\n", pr->pr_id);
4267 db_printf(" name = %s\n", pr->pr_name);
4268 db_printf(" parent = %p\n", pr->pr_parent);
4269 db_printf(" ref = %d\n", pr->pr_ref);
4270 db_printf(" uref = %d\n", pr->pr_uref);
4271 db_printf(" path = %s\n", pr->pr_path);
4272 db_printf(" cpuset = %d\n", pr->pr_cpuset
4273 ? pr->pr_cpuset->cs_id : -1);
4275 db_printf(" vnet = %p\n", pr->pr_vnet);
4277 db_printf(" root = %p\n", pr->pr_root);
4278 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4279 db_printf(" children.max = %d\n", pr->pr_childmax);
4280 db_printf(" children.cur = %d\n", pr->pr_childcount);
4281 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4282 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4283 db_printf(" flags = 0x%x", pr->pr_flags);
4284 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4286 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4287 db_printf(" %s", pr_flag_names[fi]);
4288 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4290 jsf = pr->pr_flags &
4291 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4292 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4293 pr_flag_jailsys[fi].disable &&
4294 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4295 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4298 db_printf(" allow = 0x%x", pr->pr_allow);
4299 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4301 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4302 db_printf(" %s", pr_allow_names[fi]);
4304 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4305 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4306 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4307 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4308 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4310 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4311 for (ii = 0; ii < pr->pr_ip4s; ii++)
4312 db_printf(" %s %s\n",
4313 ii == 0 ? "ip4.addr =" : " ",
4314 inet_ntoa(pr->pr_ip4[ii]));
4317 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4318 for (ii = 0; ii < pr->pr_ip6s; ii++)
4319 db_printf(" %s %s\n",
4320 ii == 0 ? "ip6.addr =" : " ",
4321 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4325 DB_SHOW_COMMAND(prison, db_show_prison_command)
4331 * Show all prisons in the list, and prison0 which is not
4334 db_show_prison(&prison0);
4335 if (!db_pager_quit) {
4336 TAILQ_FOREACH(pr, &allprison, pr_list) {
4348 /* Look for a prison with the ID and with references. */
4349 TAILQ_FOREACH(pr, &allprison, pr_list)
4350 if (pr->pr_id == addr && pr->pr_ref > 0)
4353 /* Look again, without requiring a reference. */
4354 TAILQ_FOREACH(pr, &allprison, pr_list)
4355 if (pr->pr_id == addr)
4358 /* Assume address points to a valid prison. */
4359 pr = (struct prison *)addr;