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));
591 else if (enforce < 0 || enforce > 2) {
597 pr_flags = ch_flags = 0;
598 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
600 if (pr_flag_names[fi] == NULL)
602 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
603 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
605 ch_flags |= pr_flags;
606 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
608 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
615 case JAIL_SYS_DISABLE:
616 if (!pr_flag_jailsys[fi].disable) {
620 pr_flags |= pr_flag_jailsys[fi].disable;
623 pr_flags |= pr_flag_jailsys[fi].new;
625 case JAIL_SYS_INHERIT:
632 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
634 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
635 && !(pr_flags & PR_PERSIST)) {
637 vfs_opterror(opts, "new jail must persist or attach");
641 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
643 vfs_opterror(opts, "vnet cannot be changed after creation");
648 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
650 vfs_opterror(opts, "ip4 cannot be changed after creation");
655 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
657 vfs_opterror(opts, "ip6 cannot be changed after creation");
662 pr_allow = ch_allow = 0;
663 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
665 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
666 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
668 ch_allow |= pr_allow;
670 error = vfs_getopt(opts, "name", (void **)&name, &len);
676 if (len == 0 || name[len - 1] != '\0') {
680 if (len > MAXHOSTNAMELEN) {
681 error = ENAMETOOLONG;
686 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
694 if (len == 0 || host[len - 1] != '\0') {
698 if (len > MAXHOSTNAMELEN) {
699 error = ENAMETOOLONG;
704 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
712 if (len == 0 || domain[len - 1] != '\0') {
716 if (len > MAXHOSTNAMELEN) {
717 error = ENAMETOOLONG;
722 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
730 if (len == 0 || uuid[len - 1] != '\0') {
734 if (len > HOSTUUIDLEN) {
735 error = ENAMETOOLONG;
740 #ifdef COMPAT_FREEBSD32
741 if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
744 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
748 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
760 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
762 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
765 else if (ip4s & (sizeof(*ip4) - 1)) {
769 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
771 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
773 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
774 ip4s /= sizeof(*ip4);
775 if (ip4s > jail_max_af_ips) {
777 vfs_opterror(opts, "too many IPv4 addresses");
780 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
781 bcopy(op, ip4, ip4s * sizeof(*ip4));
783 * IP addresses are all sorted but ip[0] to preserve
784 * the primary IP address as given from userland.
785 * This special IP is used for unbound outgoing
786 * connections as well for "loopback" traffic in case
787 * source address selection cannot find any more fitting
788 * address to connect from.
791 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
793 * Check for duplicate addresses and do some simple
794 * zero and broadcast checks. If users give other bogus
795 * addresses it is their problem.
797 * We do not have to care about byte order for these
798 * checks so we will do them in NBO.
800 for (ii = 0; ii < ip4s; ii++) {
801 if (ip4[ii].s_addr == INADDR_ANY ||
802 ip4[ii].s_addr == INADDR_BROADCAST) {
807 (ip4[0].s_addr == ip4[ii+1].s_addr ||
808 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
818 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
820 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
823 else if (ip6s & (sizeof(*ip6) - 1)) {
827 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
829 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
831 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
832 ip6s /= sizeof(*ip6);
833 if (ip6s > jail_max_af_ips) {
835 vfs_opterror(opts, "too many IPv6 addresses");
838 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
839 bcopy(op, ip6, ip6s * sizeof(*ip6));
841 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
842 for (ii = 0; ii < ip6s; ii++) {
843 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
848 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
849 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
859 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
860 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
863 "vnet jails cannot have IP address restrictions");
869 error = vfs_getopt(opts, "path", (void **)&path, &len);
875 if (flags & JAIL_UPDATE) {
878 "path cannot be changed after creation");
881 if (len == 0 || path[len - 1] != '\0') {
885 if (len < 2 || (len == 2 && path[0] == '/'))
888 /* Leave room for a real-root full pathname. */
889 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
890 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
891 error = ENAMETOOLONG;
894 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
899 vfslocked = NDHASGIANT(&nd);
901 NDFREE(&nd, NDF_ONLY_PNBUF);
902 if (root->v_type != VDIR) {
905 VFS_UNLOCK_GIANT(vfslocked);
908 VFS_UNLOCK_GIANT(vfslocked);
913 * Grab the allprison lock before letting modules check their
914 * parameters. Once we have it, do not let go so we'll have a
915 * consistent view of the OSD list.
917 sx_xlock(&allprison_lock);
918 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
920 goto done_unlock_list;
922 /* By now, all parameters should have been noted. */
923 TAILQ_FOREACH(opt, opts, link) {
924 if (!opt->seen && strcmp(opt->name, "errmsg")) {
926 vfs_opterror(opts, "unknown parameter: %s", opt->name);
927 goto done_unlock_list;
932 * See if we are creating a new record or updating an existing one.
933 * This abuses the file error codes ENOENT and EEXIST.
935 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
938 vfs_opterror(opts, "no valid operation (create or update)");
939 goto done_unlock_list;
943 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
944 namelc = strrchr(name, '.');
945 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
951 * See if a requested jid already exists. There is an
952 * information leak here if the jid exists but is not within
953 * the caller's jail hierarchy. Jail creators will get EEXIST
954 * even though they cannot see the jail, and CREATE | UPDATE
955 * will return ENOENT which is not normally a valid error.
959 vfs_opterror(opts, "negative jid");
960 goto done_unlock_list;
962 pr = prison_find(jid);
965 /* Create: jid must not exist. */
966 if (cuflags == JAIL_CREATE) {
967 mtx_unlock(&pr->pr_mtx);
969 vfs_opterror(opts, "jail %d already exists",
971 goto done_unlock_list;
973 if (!prison_ischild(mypr, pr)) {
974 mtx_unlock(&pr->pr_mtx);
976 } else if (pr->pr_uref == 0) {
977 if (!(flags & JAIL_DYING)) {
978 mtx_unlock(&pr->pr_mtx);
980 vfs_opterror(opts, "jail %d is dying",
982 goto done_unlock_list;
983 } else if ((flags & JAIL_ATTACH) ||
984 (pr_flags & PR_PERSIST)) {
986 * A dying jail might be resurrected
987 * (via attach or persist), but first
988 * it must determine if another jail
989 * has claimed its name. Accomplish
990 * this by implicitly re-setting the
994 name = prison_name(mypr, pr);
999 /* Update: jid must exist. */
1000 if (cuflags == JAIL_UPDATE) {
1002 vfs_opterror(opts, "jail %d not found", jid);
1003 goto done_unlock_list;
1008 * If the caller provided a name, look for a jail by that name.
1009 * This has different semantics for creates and updates keyed by jid
1010 * (where the name must not already exist in a different jail),
1011 * and updates keyed by the name itself (where the name must exist
1012 * because that is the jail being updated).
1015 namelc = strrchr(name, '.');
1020 * This is a hierarchical name. Split it into the
1021 * parent and child names, and make sure the parent
1022 * exists or matches an already found jail.
1026 if (strncmp(name, ppr->pr_name, namelc - name)
1027 || ppr->pr_name[namelc - name] != '\0') {
1028 mtx_unlock(&pr->pr_mtx);
1031 "cannot change jail's parent");
1032 goto done_unlock_list;
1035 ppr = prison_find_name(mypr, name);
1039 "jail \"%s\" not found", name);
1040 goto done_unlock_list;
1042 mtx_unlock(&ppr->pr_mtx);
1046 if (name[0] != '\0') {
1048 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1051 FOREACH_PRISON_CHILD(ppr, tpr) {
1052 if (tpr != pr && tpr->pr_ref > 0 &&
1053 !strcmp(tpr->pr_name + namelen, name)) {
1055 cuflags != JAIL_CREATE) {
1056 mtx_lock(&tpr->pr_mtx);
1057 if (tpr->pr_ref > 0) {
1062 if (tpr->pr_uref > 0) {
1068 mtx_unlock(&tpr->pr_mtx);
1069 } else if (tpr->pr_uref > 0) {
1071 * Create, or update(jid):
1072 * name must not exist in an
1073 * active sibling jail.
1077 mtx_unlock(&pr->pr_mtx);
1079 "jail \"%s\" already exists",
1081 goto done_unlock_list;
1085 /* If no active jail is found, use a dying one. */
1086 if (deadpr != NULL && pr == NULL) {
1087 if (flags & JAIL_DYING) {
1088 mtx_lock(&deadpr->pr_mtx);
1089 if (deadpr->pr_ref == 0) {
1090 mtx_unlock(&deadpr->pr_mtx);
1094 } else if (cuflags == JAIL_UPDATE) {
1097 "jail \"%s\" is dying", name);
1098 goto done_unlock_list;
1101 /* Update: name must exist if no jid. */
1102 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1104 vfs_opterror(opts, "jail \"%s\" not found",
1106 goto done_unlock_list;
1110 /* Update: must provide a jid or name. */
1111 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1113 vfs_opterror(opts, "update specified no jail");
1114 goto done_unlock_list;
1117 /* If there's no prison to update, create a new one and link it in. */
1119 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1120 if (tpr->pr_childcount >= tpr->pr_childmax) {
1122 vfs_opterror(opts, "prison limit exceeded");
1123 goto done_unlock_list;
1126 mtx_lock(&ppr->pr_mtx);
1127 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1128 mtx_unlock(&ppr->pr_mtx);
1130 vfs_opterror(opts, "parent jail went away!");
1131 goto done_unlock_list;
1135 mtx_unlock(&ppr->pr_mtx);
1136 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1138 /* Find the next free jid. */
1141 if (jid == JAIL_MAX)
1143 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1144 if (tpr->pr_id < jid)
1146 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1147 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1150 if (jid == lastprid) {
1153 "no available jail IDs");
1155 prison_deref(ppr, PD_DEREF |
1156 PD_DEUREF | PD_LIST_XLOCKED);
1165 * The jail already has a jid (that did not yet exist),
1166 * so just find where to insert it.
1168 TAILQ_FOREACH(tpr, &allprison, pr_list)
1169 if (tpr->pr_id >= jid) {
1170 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1175 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1176 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1177 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1178 tpr->pr_childcount++;
1180 pr->pr_parent = ppr;
1183 /* Set some default values, and inherit some from the parent. */
1188 root = mypr->pr_root;
1191 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1192 pr->pr_flags |= PR_HOST;
1193 #if defined(INET) || defined(INET6)
1195 if (!(pr_flags & PR_VNET))
1199 if (!(ch_flags & PR_IP4_USER))
1201 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1202 else if (!(pr_flags & PR_IP4_USER)) {
1203 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1204 if (ppr->pr_ip4 != NULL) {
1205 pr->pr_ip4s = ppr->pr_ip4s;
1206 pr->pr_ip4 = malloc(pr->pr_ip4s *
1207 sizeof(struct in_addr), M_PRISON,
1209 bcopy(ppr->pr_ip4, pr->pr_ip4,
1210 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1215 if (!(ch_flags & PR_IP6_USER))
1217 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1218 else if (!(pr_flags & PR_IP6_USER)) {
1219 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1220 if (ppr->pr_ip6 != NULL) {
1221 pr->pr_ip6s = ppr->pr_ip6s;
1222 pr->pr_ip6 = malloc(pr->pr_ip6s *
1223 sizeof(struct in6_addr), M_PRISON,
1225 bcopy(ppr->pr_ip6, pr->pr_ip6,
1226 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1232 /* Source address selection is always on by default. */
1233 pr->pr_flags |= _PR_IP_SADDRSEL;
1235 pr->pr_securelevel = ppr->pr_securelevel;
1236 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1237 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1239 LIST_INIT(&pr->pr_children);
1240 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1243 /* Allocate a new vnet if specified. */
1244 pr->pr_vnet = (pr_flags & PR_VNET)
1245 ? vnet_alloc() : ppr->pr_vnet;
1248 * Allocate a dedicated cpuset for each jail.
1249 * Unlike other initial settings, this may return an erorr.
1251 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1253 prison_deref(pr, PD_LIST_XLOCKED);
1257 mtx_lock(&pr->pr_mtx);
1259 * New prisons do not yet have a reference, because we do not
1260 * want other to see the incomplete prison once the
1261 * allprison_lock is downgraded.
1266 * Grab a reference for existing prisons, to ensure they
1267 * continue to exist for the duration of the call.
1270 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1271 if ((pr->pr_flags & PR_VNET) &&
1272 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1275 "vnet jails cannot have IP address restrictions");
1276 goto done_deref_locked;
1280 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1283 "ip4 cannot be changed after creation");
1284 goto done_deref_locked;
1288 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1291 "ip6 cannot be changed after creation");
1292 goto done_deref_locked;
1297 /* Do final error checking before setting anything. */
1299 if (slevel < ppr->pr_securelevel) {
1301 goto done_deref_locked;
1305 if (childmax >= ppr->pr_childmax) {
1307 goto done_deref_locked;
1311 if (enforce < ppr->pr_enforce_statfs) {
1313 goto done_deref_locked;
1318 if (ppr->pr_flags & PR_IP4) {
1320 * Make sure the new set of IP addresses is a
1321 * subset of the parent's list. Don't worry
1322 * about the parent being unlocked, as any
1323 * setting is done with allprison_lock held.
1325 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1326 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1328 if (ij == ppr->pr_ip4s) {
1330 goto done_deref_locked;
1333 for (ii = ij = 1; ii < ip4s; ii++) {
1334 if (ip4[ii].s_addr ==
1335 ppr->pr_ip4[0].s_addr)
1337 for (; ij < ppr->pr_ip4s; ij++)
1338 if (ip4[ii].s_addr ==
1339 ppr->pr_ip4[ij].s_addr)
1341 if (ij == ppr->pr_ip4s)
1344 if (ij == ppr->pr_ip4s) {
1346 goto done_deref_locked;
1351 * Check for conflicting IP addresses. We permit them
1352 * if there is no more than one IP on each jail. If
1353 * there is a duplicate on a jail with more than one
1354 * IP stop checking and return error.
1358 for (; tppr != &prison0; tppr = tppr->pr_parent)
1359 if (tppr->pr_flags & PR_VNET)
1362 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1365 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1367 tpr->pr_uref == 0) {
1371 if (!(tpr->pr_flags & PR_IP4_USER))
1374 if (tpr->pr_ip4 == NULL ||
1375 (ip4s == 1 && tpr->pr_ip4s == 1))
1377 for (ii = 0; ii < ip4s; ii++) {
1378 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1381 "IPv4 addresses clash");
1382 goto done_deref_locked;
1390 if (ppr->pr_flags & PR_IP6) {
1392 * Make sure the new set of IP addresses is a
1393 * subset of the parent's list.
1395 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1396 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1399 if (ij == ppr->pr_ip6s) {
1401 goto done_deref_locked;
1404 for (ii = ij = 1; ii < ip6s; ii++) {
1405 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1408 for (; ij < ppr->pr_ip6s; ij++)
1409 if (IN6_ARE_ADDR_EQUAL(
1410 &ip6[ii], &ppr->pr_ip6[ij]))
1412 if (ij == ppr->pr_ip6s)
1415 if (ij == ppr->pr_ip6s) {
1417 goto done_deref_locked;
1421 /* Check for conflicting IP addresses. */
1424 for (; tppr != &prison0; tppr = tppr->pr_parent)
1425 if (tppr->pr_flags & PR_VNET)
1428 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1431 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1433 tpr->pr_uref == 0) {
1437 if (!(tpr->pr_flags & PR_IP6_USER))
1440 if (tpr->pr_ip6 == NULL ||
1441 (ip6s == 1 && tpr->pr_ip6s == 1))
1443 for (ii = 0; ii < ip6s; ii++) {
1444 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1447 "IPv6 addresses clash");
1448 goto done_deref_locked;
1454 onamelen = namelen = 0;
1456 /* Give a default name of the jid. */
1457 if (name[0] == '\0')
1458 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1459 else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1463 "name cannot be numeric (unless it is the jid)");
1464 goto done_deref_locked;
1467 * Make sure the name isn't too long for the prison or its
1470 onamelen = strlen(pr->pr_name);
1471 namelen = strlen(name);
1472 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1473 error = ENAMETOOLONG;
1474 goto done_deref_locked;
1476 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1477 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1478 sizeof(pr->pr_name)) {
1479 error = ENAMETOOLONG;
1480 goto done_deref_locked;
1484 if (pr_allow & ~ppr->pr_allow) {
1486 goto done_deref_locked;
1489 /* Set the parameters of the prison. */
1492 if (pr_flags & PR_IP4_USER) {
1493 pr->pr_flags |= PR_IP4;
1494 free(pr->pr_ip4, M_PRISON);
1498 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1500 if (tpr->pr_flags & PR_VNET) {
1505 if (prison_restrict_ip4(tpr, NULL)) {
1514 if (pr_flags & PR_IP6_USER) {
1515 pr->pr_flags |= PR_IP6;
1516 free(pr->pr_ip6, M_PRISON);
1520 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1522 if (tpr->pr_flags & PR_VNET) {
1527 if (prison_restrict_ip6(tpr, NULL)) {
1535 pr->pr_securelevel = slevel;
1536 /* Set all child jails to be at least this level. */
1537 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1538 if (tpr->pr_securelevel < slevel)
1539 tpr->pr_securelevel = slevel;
1542 pr->pr_childmax = childmax;
1543 /* Set all child jails to under this limit. */
1544 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1545 if (tpr->pr_childmax > childmax - level)
1546 tpr->pr_childmax = childmax > level
1547 ? childmax - level : 0;
1550 pr->pr_enforce_statfs = enforce;
1551 /* Pass this restriction on to the children. */
1552 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1553 if (tpr->pr_enforce_statfs < enforce)
1554 tpr->pr_enforce_statfs = enforce;
1557 if (ppr == &prison0)
1558 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1560 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1561 ppr->pr_name, name);
1562 /* Change this component of child names. */
1563 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1564 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1565 strlen(tpr->pr_name + onamelen) + 1);
1566 bcopy(pr->pr_name, tpr->pr_name, namelen);
1570 /* Try to keep a real-rooted full pathname. */
1571 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1572 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1573 mypr->pr_path, path);
1575 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1578 if (PR_HOST & ch_flags & ~pr_flags) {
1579 if (pr->pr_flags & PR_HOST) {
1581 * Copy the parent's host info. As with pr_ip4 above,
1582 * the lack of a lock on the parent is not a problem;
1583 * it is always set with allprison_lock at least
1584 * shared, and is held exclusively here.
1586 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1587 sizeof(pr->pr_hostname));
1588 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1589 sizeof(pr->pr_domainname));
1590 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1591 sizeof(pr->pr_hostuuid));
1592 pr->pr_hostid = pr->pr_parent->pr_hostid;
1594 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1595 /* Set this prison, and any descendants without PR_HOST. */
1597 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1599 strlcpy(pr->pr_domainname, domain,
1600 sizeof(pr->pr_domainname));
1602 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1604 pr->pr_hostid = hid;
1605 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1606 if (tpr->pr_flags & PR_HOST)
1610 strlcpy(tpr->pr_hostname,
1612 sizeof(tpr->pr_hostname));
1614 strlcpy(tpr->pr_domainname,
1616 sizeof(tpr->pr_domainname));
1618 strlcpy(tpr->pr_hostuuid,
1620 sizeof(tpr->pr_hostuuid));
1622 tpr->pr_hostid = hid;
1626 if ((tallow = ch_allow & ~pr_allow)) {
1627 /* Clear allow bits in all children. */
1628 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1629 tpr->pr_allow &= ~tallow;
1631 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1633 * Persistent prisons get an extra reference, and prisons losing their
1634 * persist flag lose that reference. Only do this for existing prisons
1635 * for now, so new ones will remain unseen until after the module
1636 * handlers have completed.
1638 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1639 if (pr_flags & PR_PERSIST) {
1647 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1648 mtx_unlock(&pr->pr_mtx);
1650 /* Locks may have prevented a complete restriction of child IP
1651 * addresses. If so, allocate some more memory and try again.
1656 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1657 mtx_lock(&pr->pr_mtx);
1659 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1661 if (tpr->pr_flags & PR_VNET) {
1666 if (prison_restrict_ip4(tpr, ip4)) {
1673 mtx_unlock(&pr->pr_mtx);
1679 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1680 mtx_lock(&pr->pr_mtx);
1682 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1684 if (tpr->pr_flags & PR_VNET) {
1689 if (prison_restrict_ip6(tpr, ip6)) {
1696 mtx_unlock(&pr->pr_mtx);
1700 /* Let the modules do their work. */
1701 sx_downgrade(&allprison_lock);
1703 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1705 prison_deref(pr, PD_LIST_SLOCKED);
1709 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1711 prison_deref(pr, created
1713 : PD_DEREF | PD_LIST_SLOCKED);
1717 /* Attach this process to the prison if requested. */
1718 if (flags & JAIL_ATTACH) {
1719 mtx_lock(&pr->pr_mtx);
1720 error = do_jail_attach(td, pr);
1722 vfs_opterror(opts, "attach failed");
1724 prison_deref(pr, PD_DEREF);
1730 * Now that it is all there, drop the temporary reference from existing
1731 * prisons. Or add a reference to newly created persistent prisons
1732 * (which was not done earlier so that the prison would not be publicly
1736 prison_deref(pr, (flags & JAIL_ATTACH)
1738 : PD_DEREF | PD_LIST_SLOCKED);
1740 if (pr_flags & PR_PERSIST) {
1741 mtx_lock(&pr->pr_mtx);
1744 mtx_unlock(&pr->pr_mtx);
1746 if (!(flags & JAIL_ATTACH))
1747 sx_sunlock(&allprison_lock);
1749 td->td_retval[0] = pr->pr_id;
1753 prison_deref(pr, created
1754 ? PD_LOCKED | PD_LIST_XLOCKED
1755 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1758 sx_xunlock(&allprison_lock);
1761 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1763 VFS_UNLOCK_GIANT(vfslocked);
1767 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1768 if (errmsg_len > 0) {
1769 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1770 if (errmsg_pos > 0) {
1771 if (optuio->uio_segflg == UIO_SYSSPACE)
1773 optuio->uio_iov[errmsg_pos].iov_base,
1777 optuio->uio_iov[errmsg_pos].iov_base,
1784 free(ip4, M_PRISON);
1787 free(ip6, M_PRISON);
1795 * struct jail_get_args {
1796 * struct iovec *iovp;
1797 * unsigned int iovcnt;
1802 jail_get(struct thread *td, struct jail_get_args *uap)
1807 /* Check that we have an even number of iovecs. */
1808 if (uap->iovcnt & 1)
1811 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1814 error = kern_jail_get(td, auio, uap->flags);
1816 error = copyout(auio->uio_iov, uap->iovp,
1817 uap->iovcnt * sizeof (struct iovec));
1823 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1825 struct prison *pr, *mypr;
1827 struct vfsoptlist *opts;
1828 char *errmsg, *name;
1829 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1831 if (flags & ~JAIL_GET_MASK)
1834 /* Get the parameter list. */
1835 error = vfs_buildopts(optuio, &opts);
1838 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1839 mypr = td->td_ucred->cr_prison;
1842 * Find the prison specified by one of: lastjid, jid, name.
1844 sx_slock(&allprison_lock);
1845 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1847 TAILQ_FOREACH(pr, &allprison, pr_list) {
1848 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1849 mtx_lock(&pr->pr_mtx);
1850 if (pr->pr_ref > 0 &&
1851 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1853 mtx_unlock(&pr->pr_mtx);
1859 vfs_opterror(opts, "no jail after %d", jid);
1860 goto done_unlock_list;
1861 } else if (error != ENOENT)
1862 goto done_unlock_list;
1864 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1867 pr = prison_find_child(mypr, jid);
1869 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1870 mtx_unlock(&pr->pr_mtx);
1872 vfs_opterror(opts, "jail %d is dying",
1874 goto done_unlock_list;
1879 vfs_opterror(opts, "jail %d not found", jid);
1880 goto done_unlock_list;
1882 } else if (error != ENOENT)
1883 goto done_unlock_list;
1885 error = vfs_getopt(opts, "name", (void **)&name, &len);
1887 if (len == 0 || name[len - 1] != '\0') {
1889 goto done_unlock_list;
1891 pr = prison_find_name(mypr, name);
1893 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1894 mtx_unlock(&pr->pr_mtx);
1896 vfs_opterror(opts, "jail \"%s\" is dying",
1898 goto done_unlock_list;
1903 vfs_opterror(opts, "jail \"%s\" not found", name);
1904 goto done_unlock_list;
1905 } else if (error != ENOENT)
1906 goto done_unlock_list;
1908 vfs_opterror(opts, "no jail specified");
1910 goto done_unlock_list;
1913 /* Get the parameters of the prison. */
1916 td->td_retval[0] = pr->pr_id;
1917 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1918 if (error != 0 && error != ENOENT)
1920 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1921 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1922 if (error != 0 && error != ENOENT)
1924 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1925 if (error != 0 && error != ENOENT)
1927 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1928 sizeof(pr->pr_cpuset->cs_id));
1929 if (error != 0 && error != ENOENT)
1931 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1932 if (error != 0 && error != ENOENT)
1935 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1936 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1937 if (error != 0 && error != ENOENT)
1941 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1942 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1943 if (error != 0 && error != ENOENT)
1946 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1947 sizeof(pr->pr_securelevel));
1948 if (error != 0 && error != ENOENT)
1950 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1951 sizeof(pr->pr_childcount));
1952 if (error != 0 && error != ENOENT)
1954 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1955 sizeof(pr->pr_childmax));
1956 if (error != 0 && error != ENOENT)
1958 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1959 if (error != 0 && error != ENOENT)
1961 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1962 if (error != 0 && error != ENOENT)
1964 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1965 if (error != 0 && error != ENOENT)
1967 #ifdef COMPAT_FREEBSD32
1968 if (td->td_proc->p_sysent->sv_flags & SV_ILP32) {
1969 uint32_t hid32 = pr->pr_hostid;
1971 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1974 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1975 sizeof(pr->pr_hostid));
1976 if (error != 0 && error != ENOENT)
1978 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1979 sizeof(pr->pr_enforce_statfs));
1980 if (error != 0 && error != ENOENT)
1982 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1984 if (pr_flag_names[fi] == NULL)
1986 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1987 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1988 if (error != 0 && error != ENOENT)
1991 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1992 if (error != 0 && error != ENOENT)
1995 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1998 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1999 i = pr_flag_jailsys[fi].disable &&
2000 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
2001 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
2004 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
2005 if (error != 0 && error != ENOENT)
2008 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
2010 if (pr_allow_names[fi] == NULL)
2012 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
2013 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
2014 if (error != 0 && error != ENOENT)
2017 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
2018 if (error != 0 && error != ENOENT)
2021 i = (pr->pr_uref == 0);
2022 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2023 if (error != 0 && error != ENOENT)
2026 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2027 if (error != 0 && error != ENOENT)
2030 /* Get the module parameters. */
2031 mtx_unlock(&pr->pr_mtx);
2033 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2036 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2038 /* By now, all parameters should have been noted. */
2039 TAILQ_FOREACH(opt, opts, link) {
2040 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2042 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2047 /* Write the fetched parameters back to userspace. */
2049 TAILQ_FOREACH(opt, opts, link) {
2050 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2051 pos = 2 * opt->pos + 1;
2052 optuio->uio_iov[pos].iov_len = opt->len;
2053 if (opt->value != NULL) {
2054 if (optuio->uio_segflg == UIO_SYSSPACE) {
2056 optuio->uio_iov[pos].iov_base,
2059 error = copyout(opt->value,
2060 optuio->uio_iov[pos].iov_base,
2071 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2075 sx_sunlock(&allprison_lock);
2077 if (error && errmsg_pos >= 0) {
2078 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2079 errmsg_pos = 2 * errmsg_pos + 1;
2080 if (errmsg_len > 0) {
2081 if (optuio->uio_segflg == UIO_SYSSPACE)
2083 optuio->uio_iov[errmsg_pos].iov_base,
2087 optuio->uio_iov[errmsg_pos].iov_base,
2097 * struct jail_remove_args {
2102 jail_remove(struct thread *td, struct jail_remove_args *uap)
2104 struct prison *pr, *cpr, *lpr, *tpr;
2107 error = priv_check(td, PRIV_JAIL_REMOVE);
2111 sx_xlock(&allprison_lock);
2112 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2114 sx_xunlock(&allprison_lock);
2118 /* Remove all descendants of this prison, then remove this prison. */
2120 pr->pr_flags |= PR_REMOVE;
2121 if (!LIST_EMPTY(&pr->pr_children)) {
2122 mtx_unlock(&pr->pr_mtx);
2124 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2125 mtx_lock(&cpr->pr_mtx);
2126 if (cpr->pr_ref > 0) {
2129 cpr->pr_flags |= PR_REMOVE;
2131 /* Already removed - do not do it again. */
2134 mtx_unlock(&cpr->pr_mtx);
2136 mtx_lock(&lpr->pr_mtx);
2137 prison_remove_one(lpr);
2138 sx_xlock(&allprison_lock);
2143 mtx_lock(&lpr->pr_mtx);
2144 prison_remove_one(lpr);
2145 sx_xlock(&allprison_lock);
2147 mtx_lock(&pr->pr_mtx);
2149 prison_remove_one(pr);
2154 prison_remove_one(struct prison *pr)
2159 /* If the prison was persistent, it is not anymore. */
2161 if (pr->pr_flags & PR_PERSIST) {
2164 pr->pr_flags &= ~PR_PERSIST;
2168 * jail_remove added a reference. If that's the only one, remove
2171 KASSERT(pr->pr_ref > 0,
2172 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2173 if (pr->pr_ref == 1) {
2175 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2179 mtx_unlock(&pr->pr_mtx);
2180 sx_xunlock(&allprison_lock);
2182 * Kill all processes unfortunate enough to be attached to this prison.
2184 sx_slock(&allproc_lock);
2185 LIST_FOREACH(p, &allproc, p_list) {
2187 if (p->p_state != PRS_NEW && p->p_ucred &&
2188 p->p_ucred->cr_prison == pr)
2189 psignal(p, SIGKILL);
2192 sx_sunlock(&allproc_lock);
2193 /* Remove the temporary reference added by jail_remove. */
2194 prison_deref(pr, deuref | PD_DEREF);
2199 * struct jail_attach_args {
2204 jail_attach(struct thread *td, struct jail_attach_args *uap)
2209 error = priv_check(td, PRIV_JAIL_ATTACH);
2213 sx_slock(&allprison_lock);
2214 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2216 sx_sunlock(&allprison_lock);
2221 * Do not allow a process to attach to a prison that is not
2222 * considered to be "alive".
2224 if (pr->pr_uref == 0) {
2225 mtx_unlock(&pr->pr_mtx);
2226 sx_sunlock(&allprison_lock);
2230 return (do_jail_attach(td, pr));
2234 do_jail_attach(struct thread *td, struct prison *pr)
2238 struct ucred *newcred, *oldcred;
2239 int vfslocked, error;
2242 * XXX: Note that there is a slight race here if two threads
2243 * in the same privileged process attempt to attach to two
2244 * different jails at the same time. It is important for
2245 * user processes not to do this, or they might end up with
2246 * a process root from one prison, but attached to the jail
2251 mtx_unlock(&pr->pr_mtx);
2253 /* Let modules do whatever they need to prepare for attaching. */
2254 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2256 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2259 sx_sunlock(&allprison_lock);
2262 * Reparent the newly attached process to this jail.
2264 ppr = td->td_ucred->cr_prison;
2266 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2270 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2271 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2272 if ((error = change_dir(pr->pr_root, td)) != 0)
2275 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2278 VOP_UNLOCK(pr->pr_root, 0);
2279 if ((error = change_root(pr->pr_root, td)))
2280 goto e_unlock_giant;
2281 VFS_UNLOCK_GIANT(vfslocked);
2285 oldcred = p->p_ucred;
2287 crcopy(newcred, oldcred);
2288 newcred->cr_prison = pr;
2289 p->p_ucred = newcred;
2292 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2295 VOP_UNLOCK(pr->pr_root, 0);
2297 VFS_UNLOCK_GIANT(vfslocked);
2299 /* Tell modules this thread is still in its old jail after all. */
2300 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2301 prison_deref(pr, PD_DEREF | PD_DEUREF);
2307 * Returns a locked prison instance, or NULL on failure.
2310 prison_find(int prid)
2314 sx_assert(&allprison_lock, SX_LOCKED);
2315 TAILQ_FOREACH(pr, &allprison, pr_list) {
2316 if (pr->pr_id == prid) {
2317 mtx_lock(&pr->pr_mtx);
2320 mtx_unlock(&pr->pr_mtx);
2327 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2330 prison_find_child(struct prison *mypr, int prid)
2335 sx_assert(&allprison_lock, SX_LOCKED);
2336 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2337 if (pr->pr_id == prid) {
2338 mtx_lock(&pr->pr_mtx);
2341 mtx_unlock(&pr->pr_mtx);
2348 * Look for the name relative to mypr. Returns a locked prison or NULL.
2351 prison_find_name(struct prison *mypr, const char *name)
2353 struct prison *pr, *deadpr;
2357 sx_assert(&allprison_lock, SX_LOCKED);
2358 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2361 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2362 if (!strcmp(pr->pr_name + mylen, name)) {
2363 mtx_lock(&pr->pr_mtx);
2364 if (pr->pr_ref > 0) {
2365 if (pr->pr_uref > 0)
2369 mtx_unlock(&pr->pr_mtx);
2372 /* There was no valid prison - perhaps there was a dying one. */
2373 if (deadpr != NULL) {
2374 mtx_lock(&deadpr->pr_mtx);
2375 if (deadpr->pr_ref == 0) {
2376 mtx_unlock(&deadpr->pr_mtx);
2384 * See if a prison has the specific flag set.
2387 prison_flag(struct ucred *cred, unsigned flag)
2390 /* This is an atomic read, so no locking is necessary. */
2391 return (cred->cr_prison->pr_flags & flag);
2395 prison_allow(struct ucred *cred, unsigned flag)
2398 /* This is an atomic read, so no locking is necessary. */
2399 return (cred->cr_prison->pr_allow & flag);
2403 * Remove a prison reference. If that was the last reference, remove the
2404 * prison itself - but not in this context in case there are locks held.
2407 prison_free_locked(struct prison *pr)
2410 mtx_assert(&pr->pr_mtx, MA_OWNED);
2412 if (pr->pr_ref == 0) {
2413 mtx_unlock(&pr->pr_mtx);
2414 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2415 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2418 mtx_unlock(&pr->pr_mtx);
2422 prison_free(struct prison *pr)
2425 mtx_lock(&pr->pr_mtx);
2426 prison_free_locked(pr);
2430 prison_complete(void *context, int pending)
2433 prison_deref((struct prison *)context, 0);
2437 * Remove a prison reference (usually). This internal version assumes no
2438 * mutexes are held, except perhaps the prison itself. If there are no more
2439 * references, release and delist the prison. On completion, the prison lock
2440 * and the allprison lock are both unlocked.
2443 prison_deref(struct prison *pr, int flags)
2445 struct prison *ppr, *tpr;
2448 if (!(flags & PD_LOCKED))
2449 mtx_lock(&pr->pr_mtx);
2450 /* Decrement the user references in a separate loop. */
2451 if (flags & PD_DEUREF) {
2452 for (tpr = pr;; tpr = tpr->pr_parent) {
2454 mtx_lock(&tpr->pr_mtx);
2455 if (--tpr->pr_uref > 0)
2457 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2458 mtx_unlock(&tpr->pr_mtx);
2460 /* Done if there were only user references to remove. */
2461 if (!(flags & PD_DEREF)) {
2462 mtx_unlock(&tpr->pr_mtx);
2463 if (flags & PD_LIST_SLOCKED)
2464 sx_sunlock(&allprison_lock);
2465 else if (flags & PD_LIST_XLOCKED)
2466 sx_xunlock(&allprison_lock);
2470 mtx_unlock(&tpr->pr_mtx);
2471 mtx_lock(&pr->pr_mtx);
2476 if (flags & PD_DEREF)
2478 /* If the prison still has references, nothing else to do. */
2479 if (pr->pr_ref > 0) {
2480 mtx_unlock(&pr->pr_mtx);
2481 if (flags & PD_LIST_SLOCKED)
2482 sx_sunlock(&allprison_lock);
2483 else if (flags & PD_LIST_XLOCKED)
2484 sx_xunlock(&allprison_lock);
2488 mtx_unlock(&pr->pr_mtx);
2489 if (flags & PD_LIST_SLOCKED) {
2490 if (!sx_try_upgrade(&allprison_lock)) {
2491 sx_sunlock(&allprison_lock);
2492 sx_xlock(&allprison_lock);
2494 } else if (!(flags & PD_LIST_XLOCKED))
2495 sx_xlock(&allprison_lock);
2497 TAILQ_REMOVE(&allprison, pr, pr_list);
2498 LIST_REMOVE(pr, pr_sibling);
2499 ppr = pr->pr_parent;
2500 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2501 tpr->pr_childcount--;
2502 sx_xunlock(&allprison_lock);
2505 if (pr->pr_vnet != ppr->pr_vnet)
2506 vnet_destroy(pr->pr_vnet);
2508 if (pr->pr_root != NULL) {
2509 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2511 VFS_UNLOCK_GIANT(vfslocked);
2513 mtx_destroy(&pr->pr_mtx);
2515 free(pr->pr_ip4, M_PRISON);
2518 free(pr->pr_ip6, M_PRISON);
2520 if (pr->pr_cpuset != NULL)
2521 cpuset_rel(pr->pr_cpuset);
2525 /* Removing a prison frees a reference on its parent. */
2527 mtx_lock(&pr->pr_mtx);
2533 prison_hold_locked(struct prison *pr)
2536 mtx_assert(&pr->pr_mtx, MA_OWNED);
2537 KASSERT(pr->pr_ref > 0,
2538 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2543 prison_hold(struct prison *pr)
2546 mtx_lock(&pr->pr_mtx);
2547 prison_hold_locked(pr);
2548 mtx_unlock(&pr->pr_mtx);
2552 prison_proc_hold(struct prison *pr)
2555 mtx_lock(&pr->pr_mtx);
2556 KASSERT(pr->pr_uref > 0,
2557 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2559 mtx_unlock(&pr->pr_mtx);
2563 prison_proc_free(struct prison *pr)
2566 mtx_lock(&pr->pr_mtx);
2567 KASSERT(pr->pr_uref > 0,
2568 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2569 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2575 * Restrict a prison's IP address list with its parent's, possibly replacing
2576 * it. Return true if the replacement buffer was used (or would have been).
2579 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2584 ppr = pr->pr_parent;
2585 if (!(pr->pr_flags & PR_IP4_USER)) {
2586 /* This has no user settings, so just copy the parent's list. */
2587 if (pr->pr_ip4s < ppr->pr_ip4s) {
2589 * There's no room for the parent's list. Use the
2590 * new list buffer, which is assumed to be big enough
2591 * (if it was passed). If there's no buffer, try to
2595 if (newip4 == NULL) {
2596 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2597 M_PRISON, M_NOWAIT);
2601 if (newip4 != NULL) {
2602 bcopy(ppr->pr_ip4, newip4,
2603 ppr->pr_ip4s * sizeof(*newip4));
2604 free(pr->pr_ip4, M_PRISON);
2605 pr->pr_ip4 = newip4;
2606 pr->pr_ip4s = ppr->pr_ip4s;
2610 pr->pr_ip4s = ppr->pr_ip4s;
2611 if (pr->pr_ip4s > 0)
2612 bcopy(ppr->pr_ip4, pr->pr_ip4,
2613 pr->pr_ip4s * sizeof(*newip4));
2614 else if (pr->pr_ip4 != NULL) {
2615 free(pr->pr_ip4, M_PRISON);
2618 } else if (pr->pr_ip4s > 0) {
2619 /* Remove addresses that aren't in the parent. */
2620 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2621 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2623 if (ij < ppr->pr_ip4s)
2626 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2627 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2630 for (ij = 1; ii < pr->pr_ip4s; ) {
2631 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2635 switch (ij >= ppr->pr_ip4s ? -1 :
2636 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2638 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2639 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2650 if (pr->pr_ip4s == 0) {
2651 pr->pr_flags |= PR_IP4_DISABLE;
2652 free(pr->pr_ip4, M_PRISON);
2660 * Pass back primary IPv4 address of this jail.
2662 * If not restricted return success but do not alter the address. Caller has
2663 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2665 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2666 * Address returned in NBO.
2669 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2673 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2674 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2676 pr = cred->cr_prison;
2677 if (!(pr->pr_flags & PR_IP4))
2679 mtx_lock(&pr->pr_mtx);
2680 if (!(pr->pr_flags & PR_IP4)) {
2681 mtx_unlock(&pr->pr_mtx);
2684 if (pr->pr_ip4 == NULL) {
2685 mtx_unlock(&pr->pr_mtx);
2686 return (EAFNOSUPPORT);
2689 ia->s_addr = pr->pr_ip4[0].s_addr;
2690 mtx_unlock(&pr->pr_mtx);
2695 * Return 1 if we should do proper source address selection or are not jailed.
2696 * We will return 0 if we should bypass source address selection in favour
2697 * of the primary jail IPv4 address. Only in this case *ia will be updated and
2699 * Return EAFNOSUPPORT, in case this jail does not allow IPv4.
2702 prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia)
2708 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2709 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2714 pr = cred->cr_prison;
2715 if (pr->pr_flags & PR_IP4_SADDRSEL)
2718 lia.s_addr = INADDR_ANY;
2719 error = prison_get_ip4(cred, &lia);
2722 if (lia.s_addr == INADDR_ANY)
2725 ia->s_addr = lia.s_addr;
2730 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2733 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2740 * No need to lock since the PR_IP4_USER flag can't be altered for
2743 while (pr1 != &prison0 &&
2745 !(pr1->pr_flags & PR_VNET) &&
2747 !(pr1->pr_flags & PR_IP4_USER))
2748 pr1 = pr1->pr_parent;
2749 while (pr2 != &prison0 &&
2751 !(pr2->pr_flags & PR_VNET) &&
2753 !(pr2->pr_flags & PR_IP4_USER))
2754 pr2 = pr2->pr_parent;
2755 return (pr1 == pr2);
2759 * Make sure our (source) address is set to something meaningful to this
2762 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2763 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2764 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2767 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2773 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2774 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2776 pr = cred->cr_prison;
2777 if (!(pr->pr_flags & PR_IP4))
2779 mtx_lock(&pr->pr_mtx);
2780 if (!(pr->pr_flags & PR_IP4)) {
2781 mtx_unlock(&pr->pr_mtx);
2784 if (pr->pr_ip4 == NULL) {
2785 mtx_unlock(&pr->pr_mtx);
2786 return (EAFNOSUPPORT);
2789 ia0.s_addr = ntohl(ia->s_addr);
2790 if (ia0.s_addr == INADDR_LOOPBACK) {
2791 ia->s_addr = pr->pr_ip4[0].s_addr;
2792 mtx_unlock(&pr->pr_mtx);
2796 if (ia0.s_addr == INADDR_ANY) {
2798 * In case there is only 1 IPv4 address, bind directly.
2800 if (pr->pr_ip4s == 1)
2801 ia->s_addr = pr->pr_ip4[0].s_addr;
2802 mtx_unlock(&pr->pr_mtx);
2806 error = _prison_check_ip4(pr, ia);
2807 mtx_unlock(&pr->pr_mtx);
2812 * Rewrite destination address in case we will connect to loopback address.
2814 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2815 * Address passed in in NBO and returned in NBO.
2818 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2822 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2823 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2825 pr = cred->cr_prison;
2826 if (!(pr->pr_flags & PR_IP4))
2828 mtx_lock(&pr->pr_mtx);
2829 if (!(pr->pr_flags & PR_IP4)) {
2830 mtx_unlock(&pr->pr_mtx);
2833 if (pr->pr_ip4 == NULL) {
2834 mtx_unlock(&pr->pr_mtx);
2835 return (EAFNOSUPPORT);
2838 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2839 ia->s_addr = pr->pr_ip4[0].s_addr;
2840 mtx_unlock(&pr->pr_mtx);
2845 * Return success because nothing had to be changed.
2847 mtx_unlock(&pr->pr_mtx);
2852 * Check if given address belongs to the jail referenced by cred/prison.
2854 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2855 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2856 * doesn't allow IPv4. Address passed in in NBO.
2859 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2864 * Check the primary IP.
2866 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2870 * All the other IPs are sorted so we can do a binary search.
2873 z = pr->pr_ip4s - 2;
2876 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2885 return (EADDRNOTAVAIL);
2889 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2894 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2895 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2897 pr = cred->cr_prison;
2898 if (!(pr->pr_flags & PR_IP4))
2900 mtx_lock(&pr->pr_mtx);
2901 if (!(pr->pr_flags & PR_IP4)) {
2902 mtx_unlock(&pr->pr_mtx);
2905 if (pr->pr_ip4 == NULL) {
2906 mtx_unlock(&pr->pr_mtx);
2907 return (EAFNOSUPPORT);
2910 error = _prison_check_ip4(pr, ia);
2911 mtx_unlock(&pr->pr_mtx);
2918 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2923 ppr = pr->pr_parent;
2924 if (!(pr->pr_flags & PR_IP6_USER)) {
2925 /* This has no user settings, so just copy the parent's list. */
2926 if (pr->pr_ip6s < ppr->pr_ip6s) {
2928 * There's no room for the parent's list. Use the
2929 * new list buffer, which is assumed to be big enough
2930 * (if it was passed). If there's no buffer, try to
2934 if (newip6 == NULL) {
2935 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2936 M_PRISON, M_NOWAIT);
2940 if (newip6 != NULL) {
2941 bcopy(ppr->pr_ip6, newip6,
2942 ppr->pr_ip6s * sizeof(*newip6));
2943 free(pr->pr_ip6, M_PRISON);
2944 pr->pr_ip6 = newip6;
2945 pr->pr_ip6s = ppr->pr_ip6s;
2949 pr->pr_ip6s = ppr->pr_ip6s;
2950 if (pr->pr_ip6s > 0)
2951 bcopy(ppr->pr_ip6, pr->pr_ip6,
2952 pr->pr_ip6s * sizeof(*newip6));
2953 else if (pr->pr_ip6 != NULL) {
2954 free(pr->pr_ip6, M_PRISON);
2957 } else if (pr->pr_ip6s > 0) {
2958 /* Remove addresses that aren't in the parent. */
2959 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2960 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2963 if (ij < ppr->pr_ip6s)
2966 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2967 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2970 for (ij = 1; ii < pr->pr_ip6s; ) {
2971 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2976 switch (ij >= ppr->pr_ip4s ? -1 :
2977 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2979 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2980 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2991 if (pr->pr_ip6s == 0) {
2992 pr->pr_flags |= PR_IP6_DISABLE;
2993 free(pr->pr_ip6, M_PRISON);
3001 * Pass back primary IPv6 address for this jail.
3003 * If not restricted return success but do not alter the address. Caller has
3004 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
3006 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3009 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
3013 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3014 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3016 pr = cred->cr_prison;
3017 if (!(pr->pr_flags & PR_IP6))
3019 mtx_lock(&pr->pr_mtx);
3020 if (!(pr->pr_flags & PR_IP6)) {
3021 mtx_unlock(&pr->pr_mtx);
3024 if (pr->pr_ip6 == NULL) {
3025 mtx_unlock(&pr->pr_mtx);
3026 return (EAFNOSUPPORT);
3029 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3030 mtx_unlock(&pr->pr_mtx);
3035 * Return 1 if we should do proper source address selection or are not jailed.
3036 * We will return 0 if we should bypass source address selection in favour
3037 * of the primary jail IPv6 address. Only in this case *ia will be updated and
3039 * Return EAFNOSUPPORT, in case this jail does not allow IPv6.
3042 prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6)
3045 struct in6_addr lia6;
3048 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3049 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3054 pr = cred->cr_prison;
3055 if (pr->pr_flags & PR_IP6_SADDRSEL)
3059 error = prison_get_ip6(cred, &lia6);
3062 if (IN6_IS_ADDR_UNSPECIFIED(&lia6))
3065 bcopy(&lia6, ia6, sizeof(struct in6_addr));
3070 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
3073 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
3079 while (pr1 != &prison0 &&
3081 !(pr1->pr_flags & PR_VNET) &&
3083 !(pr1->pr_flags & PR_IP6_USER))
3084 pr1 = pr1->pr_parent;
3085 while (pr2 != &prison0 &&
3087 !(pr2->pr_flags & PR_VNET) &&
3089 !(pr2->pr_flags & PR_IP6_USER))
3090 pr2 = pr2->pr_parent;
3091 return (pr1 == pr2);
3095 * Make sure our (source) address is set to something meaningful to this jail.
3097 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
3098 * when needed while binding.
3100 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3101 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3102 * doesn't allow IPv6.
3105 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3110 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3111 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3113 pr = cred->cr_prison;
3114 if (!(pr->pr_flags & PR_IP6))
3116 mtx_lock(&pr->pr_mtx);
3117 if (!(pr->pr_flags & PR_IP6)) {
3118 mtx_unlock(&pr->pr_mtx);
3121 if (pr->pr_ip6 == NULL) {
3122 mtx_unlock(&pr->pr_mtx);
3123 return (EAFNOSUPPORT);
3126 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3127 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3128 mtx_unlock(&pr->pr_mtx);
3132 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3134 * In case there is only 1 IPv6 address, and v6only is true,
3135 * then bind directly.
3137 if (v6only != 0 && pr->pr_ip6s == 1)
3138 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3139 mtx_unlock(&pr->pr_mtx);
3143 error = _prison_check_ip6(pr, ia6);
3144 mtx_unlock(&pr->pr_mtx);
3149 * Rewrite destination address in case we will connect to loopback address.
3151 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3154 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3158 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3159 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3161 pr = cred->cr_prison;
3162 if (!(pr->pr_flags & PR_IP6))
3164 mtx_lock(&pr->pr_mtx);
3165 if (!(pr->pr_flags & PR_IP6)) {
3166 mtx_unlock(&pr->pr_mtx);
3169 if (pr->pr_ip6 == NULL) {
3170 mtx_unlock(&pr->pr_mtx);
3171 return (EAFNOSUPPORT);
3174 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3175 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3176 mtx_unlock(&pr->pr_mtx);
3181 * Return success because nothing had to be changed.
3183 mtx_unlock(&pr->pr_mtx);
3188 * Check if given address belongs to the jail referenced by cred/prison.
3190 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3191 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3192 * doesn't allow IPv6.
3195 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3200 * Check the primary IP.
3202 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3206 * All the other IPs are sorted so we can do a binary search.
3209 z = pr->pr_ip6s - 2;
3212 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3221 return (EADDRNOTAVAIL);
3225 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3230 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3231 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3233 pr = cred->cr_prison;
3234 if (!(pr->pr_flags & PR_IP6))
3236 mtx_lock(&pr->pr_mtx);
3237 if (!(pr->pr_flags & PR_IP6)) {
3238 mtx_unlock(&pr->pr_mtx);
3241 if (pr->pr_ip6 == NULL) {
3242 mtx_unlock(&pr->pr_mtx);
3243 return (EAFNOSUPPORT);
3246 error = _prison_check_ip6(pr, ia6);
3247 mtx_unlock(&pr->pr_mtx);
3253 * Check if a jail supports the given address family.
3255 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3259 prison_check_af(struct ucred *cred, int af)
3264 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3266 pr = cred->cr_prison;
3268 /* Prisons with their own network stack are not limited. */
3269 if (prison_owns_vnet(cred))
3278 if (pr->pr_flags & PR_IP4)
3280 mtx_lock(&pr->pr_mtx);
3281 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3282 error = EAFNOSUPPORT;
3283 mtx_unlock(&pr->pr_mtx);
3289 if (pr->pr_flags & PR_IP6)
3291 mtx_lock(&pr->pr_mtx);
3292 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3293 error = EAFNOSUPPORT;
3294 mtx_unlock(&pr->pr_mtx);
3302 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3303 error = EAFNOSUPPORT;
3309 * Check if given address belongs to the jail referenced by cred (wrapper to
3310 * prison_check_ip[46]).
3312 * Returns 0 if jail doesn't restrict the address family or if address belongs
3313 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3314 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3317 prison_if(struct ucred *cred, struct sockaddr *sa)
3320 struct sockaddr_in *sai;
3323 struct sockaddr_in6 *sai6;
3327 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3328 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3331 if (prison_owns_vnet(cred))
3336 switch (sa->sa_family)
3340 sai = (struct sockaddr_in *)sa;
3341 error = prison_check_ip4(cred, &sai->sin_addr);
3346 sai6 = (struct sockaddr_in6 *)sa;
3347 error = prison_check_ip6(cred, &sai6->sin6_addr);
3351 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3352 error = EAFNOSUPPORT;
3358 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3361 prison_check(struct ucred *cred1, struct ucred *cred2)
3364 return ((cred1->cr_prison == cred2->cr_prison ||
3365 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3369 * Return 1 if p2 is a child of p1, otherwise 0.
3372 prison_ischild(struct prison *pr1, struct prison *pr2)
3375 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3382 * Return 1 if the passed credential is in a jail, otherwise 0.
3385 jailed(struct ucred *cred)
3388 return (cred->cr_prison != &prison0);
3392 * Return 1 if the passed credential is in a jail and that jail does not
3393 * have its own virtual network stack, otherwise 0.
3396 jailed_without_vnet(struct ucred *cred)
3402 if (prison_owns_vnet(cred))
3410 * Return the correct hostname (domainname, et al) for the passed credential.
3413 getcredhostname(struct ucred *cred, char *buf, size_t size)
3418 * A NULL credential can be used to shortcut to the physical
3419 * system's hostname.
3421 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3422 mtx_lock(&pr->pr_mtx);
3423 strlcpy(buf, pr->pr_hostname, size);
3424 mtx_unlock(&pr->pr_mtx);
3428 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3431 mtx_lock(&cred->cr_prison->pr_mtx);
3432 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3433 mtx_unlock(&cred->cr_prison->pr_mtx);
3437 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3440 mtx_lock(&cred->cr_prison->pr_mtx);
3441 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3442 mtx_unlock(&cred->cr_prison->pr_mtx);
3446 getcredhostid(struct ucred *cred, unsigned long *hostid)
3449 mtx_lock(&cred->cr_prison->pr_mtx);
3450 *hostid = cred->cr_prison->pr_hostid;
3451 mtx_unlock(&cred->cr_prison->pr_mtx);
3456 * Determine whether the prison represented by cred owns
3457 * its vnet rather than having it inherited.
3459 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3462 prison_owns_vnet(struct ucred *cred)
3466 * vnets cannot be added/removed after jail creation,
3467 * so no need to lock here.
3469 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3474 * Determine whether the subject represented by cred can "see"
3475 * status of a mount point.
3476 * Returns: 0 for permitted, ENOENT otherwise.
3477 * XXX: This function should be called cr_canseemount() and should be
3478 * placed in kern_prot.c.
3481 prison_canseemount(struct ucred *cred, struct mount *mp)
3487 pr = cred->cr_prison;
3488 if (pr->pr_enforce_statfs == 0)
3490 if (pr->pr_root->v_mount == mp)
3492 if (pr->pr_enforce_statfs == 2)
3495 * If jail's chroot directory is set to "/" we should be able to see
3496 * all mount-points from inside a jail.
3497 * This is ugly check, but this is the only situation when jail's
3498 * directory ends with '/'.
3500 if (strcmp(pr->pr_path, "/") == 0)
3502 len = strlen(pr->pr_path);
3504 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3507 * Be sure that we don't have situation where jail's root directory
3508 * is "/some/path" and mount point is "/some/pathpath".
3510 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3516 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3518 char jpath[MAXPATHLEN];
3522 pr = cred->cr_prison;
3523 if (pr->pr_enforce_statfs == 0)
3525 if (prison_canseemount(cred, mp) != 0) {
3526 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3527 strlcpy(sp->f_mntonname, "[restricted]",
3528 sizeof(sp->f_mntonname));
3531 if (pr->pr_root->v_mount == mp) {
3533 * Clear current buffer data, so we are sure nothing from
3534 * the valid path left there.
3536 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3537 *sp->f_mntonname = '/';
3541 * If jail's chroot directory is set to "/" we should be able to see
3542 * all mount-points from inside a jail.
3544 if (strcmp(pr->pr_path, "/") == 0)
3546 len = strlen(pr->pr_path);
3547 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3549 * Clear current buffer data, so we are sure nothing from
3550 * the valid path left there.
3552 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3553 if (*jpath == '\0') {
3554 /* Should never happen. */
3555 *sp->f_mntonname = '/';
3557 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3562 * Check with permission for a specific privilege is granted within jail. We
3563 * have a specific list of accepted privileges; the rest are denied.
3566 prison_priv_check(struct ucred *cred, int priv)
3574 * Privileges specific to prisons with a virtual network stack.
3575 * There might be a duplicate entry here in case the privilege
3576 * is only granted conditionally in the legacy jail case.
3581 * NFS-specific privileges.
3583 case PRIV_NFS_DAEMON:
3584 case PRIV_NFS_LOCKD:
3587 * Network stack privileges.
3589 case PRIV_NET_BRIDGE:
3592 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3593 case PRIV_NET_ROUTE:
3595 case PRIV_NET_SETIFMTU:
3596 case PRIV_NET_SETIFFLAGS:
3597 case PRIV_NET_SETIFCAP:
3598 case PRIV_NET_SETIFDESCR:
3599 case PRIV_NET_SETIFNAME :
3600 case PRIV_NET_SETIFMETRIC:
3601 case PRIV_NET_SETIFPHYS:
3602 case PRIV_NET_SETIFMAC:
3603 case PRIV_NET_ADDMULTI:
3604 case PRIV_NET_DELMULTI:
3605 case PRIV_NET_HWIOCTL:
3606 case PRIV_NET_SETLLADDR:
3607 case PRIV_NET_ADDIFGROUP:
3608 case PRIV_NET_DELIFGROUP:
3609 case PRIV_NET_IFCREATE:
3610 case PRIV_NET_IFDESTROY:
3611 case PRIV_NET_ADDIFADDR:
3612 case PRIV_NET_DELIFADDR:
3615 case PRIV_NET_SETIFVNET:
3618 * 802.11-related privileges.
3620 case PRIV_NET80211_GETKEY:
3622 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3627 * AppleTalk privileges.
3629 case PRIV_NETATALK_RESERVEDPORT:
3634 case PRIV_NETATM_CFG:
3635 case PRIV_NETATM_ADD:
3636 case PRIV_NETATM_DEL:
3637 case PRIV_NETATM_SET:
3640 * Bluetooth privileges.
3642 case PRIV_NETBLUETOOTH_RAW:
3646 * Netgraph and netgraph module privileges.
3648 case PRIV_NETGRAPH_CONTROL:
3650 case PRIV_NETGRAPH_TTY:
3654 * IPv4 and IPv6 privileges.
3656 case PRIV_NETINET_IPFW:
3657 case PRIV_NETINET_DIVERT:
3658 case PRIV_NETINET_PF:
3659 case PRIV_NETINET_DUMMYNET:
3660 case PRIV_NETINET_CARP:
3661 case PRIV_NETINET_MROUTE:
3662 case PRIV_NETINET_RAW:
3663 case PRIV_NETINET_ADDRCTRL6:
3664 case PRIV_NETINET_ND6:
3665 case PRIV_NETINET_SCOPE6:
3666 case PRIV_NETINET_ALIFETIME6:
3667 case PRIV_NETINET_IPSEC:
3668 case PRIV_NETINET_BINDANY:
3672 * IPX/SPX privileges.
3674 case PRIV_NETIPX_RESERVEDPORT:
3675 case PRIV_NETIPX_RAW:
3689 * No default: or deny here.
3690 * In case of no permit fall through to next switch().
3692 if (cred->cr_prison->pr_flags & PR_VNET)
3700 * Allow ktrace privileges for root in jail.
3706 * Allow jailed processes to configure audit identity and
3707 * submit audit records (login, etc). In the future we may
3708 * want to further refine the relationship between audit and
3711 case PRIV_AUDIT_GETAUDIT:
3712 case PRIV_AUDIT_SETAUDIT:
3713 case PRIV_AUDIT_SUBMIT:
3717 * Allow jailed processes to manipulate process UNIX
3718 * credentials in any way they see fit.
3720 case PRIV_CRED_SETUID:
3721 case PRIV_CRED_SETEUID:
3722 case PRIV_CRED_SETGID:
3723 case PRIV_CRED_SETEGID:
3724 case PRIV_CRED_SETGROUPS:
3725 case PRIV_CRED_SETREUID:
3726 case PRIV_CRED_SETREGID:
3727 case PRIV_CRED_SETRESUID:
3728 case PRIV_CRED_SETRESGID:
3731 * Jail implements visibility constraints already, so allow
3732 * jailed root to override uid/gid-based constraints.
3734 case PRIV_SEEOTHERGIDS:
3735 case PRIV_SEEOTHERUIDS:
3738 * Jail implements inter-process debugging limits already, so
3739 * allow jailed root various debugging privileges.
3741 case PRIV_DEBUG_DIFFCRED:
3742 case PRIV_DEBUG_SUGID:
3743 case PRIV_DEBUG_UNPRIV:
3746 * Allow jail to set various resource limits and login
3747 * properties, and for now, exceed process resource limits.
3749 case PRIV_PROC_LIMIT:
3750 case PRIV_PROC_SETLOGIN:
3751 case PRIV_PROC_SETRLIMIT:
3754 * System V and POSIX IPC privileges are granted in jail.
3757 case PRIV_IPC_WRITE:
3758 case PRIV_IPC_ADMIN:
3759 case PRIV_IPC_MSGSIZE:
3763 * Jail operations within a jail work on child jails.
3765 case PRIV_JAIL_ATTACH:
3767 case PRIV_JAIL_REMOVE:
3770 * Jail implements its own inter-process limits, so allow
3771 * root processes in jail to change scheduling on other
3772 * processes in the same jail. Likewise for signalling.
3774 case PRIV_SCHED_DIFFCRED:
3775 case PRIV_SCHED_CPUSET:
3776 case PRIV_SIGNAL_DIFFCRED:
3777 case PRIV_SIGNAL_SUGID:
3780 * Allow jailed processes to write to sysctls marked as jail
3783 case PRIV_SYSCTL_WRITEJAIL:
3786 * Allow root in jail to manage a variety of quota
3787 * properties. These should likely be conditional on a
3788 * configuration option.
3790 case PRIV_VFS_GETQUOTA:
3791 case PRIV_VFS_SETQUOTA:
3794 * Since Jail relies on chroot() to implement file system
3795 * protections, grant many VFS privileges to root in jail.
3796 * Be careful to exclude mount-related and NFS-related
3800 case PRIV_VFS_WRITE:
3801 case PRIV_VFS_ADMIN:
3803 case PRIV_VFS_LOOKUP:
3804 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3805 case PRIV_VFS_CHFLAGS_DEV:
3806 case PRIV_VFS_CHOWN:
3807 case PRIV_VFS_CHROOT:
3808 case PRIV_VFS_RETAINSUGID:
3809 case PRIV_VFS_FCHROOT:
3811 case PRIV_VFS_SETGID:
3813 case PRIV_VFS_STICKYFILE:
3817 * Depending on the global setting, allow privilege of
3818 * setting system flags.
3820 case PRIV_VFS_SYSFLAGS:
3821 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3827 * Depending on the global setting, allow privilege of
3828 * mounting/unmounting file systems.
3830 case PRIV_VFS_MOUNT:
3831 case PRIV_VFS_UNMOUNT:
3832 case PRIV_VFS_MOUNT_NONUSER:
3833 case PRIV_VFS_MOUNT_OWNER:
3834 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3840 * Allow jailed root to bind reserved ports and reuse in-use
3843 case PRIV_NETINET_RESERVEDPORT:
3844 case PRIV_NETINET_REUSEPORT:
3848 * Allow jailed root to set certian IPv4/6 (option) headers.
3850 case PRIV_NETINET_SETHDROPTS:
3854 * Conditionally allow creating raw sockets in jail.
3856 case PRIV_NETINET_RAW:
3857 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3863 * Since jail implements its own visibility limits on netstat
3864 * sysctls, allow getcred. This allows identd to work in
3867 case PRIV_NETINET_GETCRED:
3872 * In all remaining cases, deny the privilege request. This
3873 * includes almost all network privileges, many system
3874 * configuration privileges.
3881 * Return the part of pr2's name that is relative to pr1, or the whole name
3882 * if it does not directly follow.
3886 prison_name(struct prison *pr1, struct prison *pr2)
3890 /* Jails see themselves as "0" (if they see themselves at all). */
3893 name = pr2->pr_name;
3894 if (prison_ischild(pr1, pr2)) {
3896 * pr1 isn't locked (and allprison_lock may not be either)
3897 * so its length can't be counted on. But the number of dots
3898 * can be counted on - and counted.
3900 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3901 name = strchr(name, '.') + 1;
3907 * Return the part of pr2's path that is relative to pr1, or the whole path
3908 * if it does not directly follow.
3911 prison_path(struct prison *pr1, struct prison *pr2)
3913 char *path1, *path2;
3916 path1 = pr1->pr_path;
3917 path2 = pr2->pr_path;
3918 if (!strcmp(path1, "/"))
3920 len1 = strlen(path1);
3921 if (strncmp(path1, path2, len1))
3923 if (path2[len1] == '\0')
3925 if (path2[len1] == '/')
3926 return (path2 + len1);
3932 * Jail-related sysctls.
3934 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3938 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3941 struct prison *pr, *cpr;
3943 struct in_addr *ip4 = NULL;
3947 struct in6_addr *ip6 = NULL;
3952 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3953 pr = req->td->td_ucred->cr_prison;
3955 sx_slock(&allprison_lock);
3956 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3957 #if defined(INET) || defined(INET6)
3960 mtx_lock(&cpr->pr_mtx);
3962 if (cpr->pr_ip4s > 0) {
3963 if (ip4s < cpr->pr_ip4s) {
3964 ip4s = cpr->pr_ip4s;
3965 mtx_unlock(&cpr->pr_mtx);
3966 ip4 = realloc(ip4, ip4s *
3967 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3970 bcopy(cpr->pr_ip4, ip4,
3971 cpr->pr_ip4s * sizeof(struct in_addr));
3975 if (cpr->pr_ip6s > 0) {
3976 if (ip6s < cpr->pr_ip6s) {
3977 ip6s = cpr->pr_ip6s;
3978 mtx_unlock(&cpr->pr_mtx);
3979 ip6 = realloc(ip6, ip6s *
3980 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3983 bcopy(cpr->pr_ip6, ip6,
3984 cpr->pr_ip6s * sizeof(struct in6_addr));
3987 if (cpr->pr_ref == 0) {
3988 mtx_unlock(&cpr->pr_mtx);
3991 bzero(xp, sizeof(*xp));
3992 xp->pr_version = XPRISON_VERSION;
3993 xp->pr_id = cpr->pr_id;
3994 xp->pr_state = cpr->pr_uref > 0
3995 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3996 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3997 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3998 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4000 xp->pr_ip4s = cpr->pr_ip4s;
4003 xp->pr_ip6s = cpr->pr_ip6s;
4005 mtx_unlock(&cpr->pr_mtx);
4006 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4010 if (xp->pr_ip4s > 0) {
4011 error = SYSCTL_OUT(req, ip4,
4012 xp->pr_ip4s * sizeof(struct in_addr));
4018 if (xp->pr_ip6s > 0) {
4019 error = SYSCTL_OUT(req, ip6,
4020 xp->pr_ip6s * sizeof(struct in6_addr));
4026 sx_sunlock(&allprison_lock);
4037 SYSCTL_OID(_security_jail, OID_AUTO, list,
4038 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4039 sysctl_jail_list, "S", "List of active jails");
4042 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4046 injail = jailed(req->td->td_ucred);
4047 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4052 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4053 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4054 sysctl_jail_jailed, "I", "Process in jail?");
4056 #if defined(INET) || defined(INET6)
4057 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4058 &jail_max_af_ips, 0,
4059 "Number of IP addresses a jail may have at most per address family");
4063 * Default parameters for jail(2) compatability. For historical reasons,
4064 * the sysctl names have varying similarity to the parameter names. Prisons
4065 * just see their own parameters, and can't change them.
4068 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4071 int allow, error, i;
4073 pr = req->td->td_ucred->cr_prison;
4074 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
4076 /* Get the current flag value, and convert it to a boolean. */
4077 i = (allow & arg2) ? 1 : 0;
4080 error = sysctl_handle_int(oidp, &i, 0, req);
4081 if (error || !req->newptr)
4087 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4090 mtx_lock(&prison0.pr_mtx);
4091 jail_default_allow = (jail_default_allow & ~arg2) | i;
4092 mtx_unlock(&prison0.pr_mtx);
4096 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4097 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4098 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4099 "Processes in jail can set their hostnames");
4100 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4101 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4102 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4103 "Processes in jail are limited to creating UNIX/IP/route sockets only");
4104 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4105 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4106 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4107 "Processes in jail can use System V IPC primitives");
4108 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4109 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4110 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4111 "Prison root can create raw sockets");
4112 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4113 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4114 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4115 "Processes in jail can alter system file flags");
4116 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4117 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4118 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4119 "Processes in jail can mount/unmount jail-friendly file systems");
4122 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4127 pr = req->td->td_ucred->cr_prison;
4128 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4129 error = sysctl_handle_int(oidp, &level, 0, req);
4130 if (error || !req->newptr)
4132 *(int *)arg1 = level;
4136 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4137 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4138 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4139 sysctl_jail_default_level, "I",
4140 "Processes in jail cannot see all mounted file systems");
4143 * Nodes to describe jail parameters. Maximum length of string parameters
4144 * is returned in the string itself, and the other parameters exist merely
4145 * to make themselves and their types known.
4147 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4151 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4158 switch (oidp->oid_kind & CTLTYPE)
4164 if (!(req->flags & SCTL_MASK32))
4166 return (SYSCTL_OUT(req, &l, sizeof(l)));
4170 return (SYSCTL_OUT(req, &i, sizeof(i)));
4171 case CTLTYPE_STRING:
4172 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4174 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4175 case CTLTYPE_STRUCT:
4177 return (SYSCTL_OUT(req, &s, sizeof(s)));
4182 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4183 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4184 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4185 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4186 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4187 "I", "Jail secure level");
4188 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4189 "I", "Jail cannot see all mounted file systems");
4190 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4191 "B", "Jail persistence");
4193 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4194 "E,jailsys", "Virtual network stack");
4196 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4197 "B", "Jail is in the process of shutting down");
4199 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4200 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4201 "I", "Current number of child jails");
4202 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4203 "I", "Maximum number of child jails");
4205 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4206 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4208 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4209 "Jail NIS domainname");
4210 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4212 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4213 "LU", "Jail host ID");
4215 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4216 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4219 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4220 "Jail IPv4 address virtualization");
4221 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4222 "S,in_addr,a", "Jail IPv4 addresses");
4223 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4224 "B", "Do (not) use IPv4 source address selection rather than the "
4225 "primary jail IPv4 address.");
4228 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4229 "Jail IPv6 address virtualization");
4230 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4231 "S,in6_addr,a", "Jail IPv6 addresses");
4232 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4233 "B", "Do (not) use IPv6 source address selection rather than the "
4234 "primary jail IPv6 address.");
4237 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4238 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4239 "B", "Jail may set hostname");
4240 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4241 "B", "Jail may use SYSV IPC");
4242 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4243 "B", "Jail may create raw sockets");
4244 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4245 "B", "Jail may alter system file flags");
4246 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4247 "B", "Jail may mount/unmount jail-friendly file systems");
4248 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4249 "B", "Jail may set file quotas");
4250 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4251 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4257 db_show_prison(struct prison *pr)
4260 #if defined(INET) || defined(INET6)
4265 char ip6buf[INET6_ADDRSTRLEN];
4268 db_printf("prison %p:\n", pr);
4269 db_printf(" jid = %d\n", pr->pr_id);
4270 db_printf(" name = %s\n", pr->pr_name);
4271 db_printf(" parent = %p\n", pr->pr_parent);
4272 db_printf(" ref = %d\n", pr->pr_ref);
4273 db_printf(" uref = %d\n", pr->pr_uref);
4274 db_printf(" path = %s\n", pr->pr_path);
4275 db_printf(" cpuset = %d\n", pr->pr_cpuset
4276 ? pr->pr_cpuset->cs_id : -1);
4278 db_printf(" vnet = %p\n", pr->pr_vnet);
4280 db_printf(" root = %p\n", pr->pr_root);
4281 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4282 db_printf(" children.max = %d\n", pr->pr_childmax);
4283 db_printf(" children.cur = %d\n", pr->pr_childcount);
4284 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4285 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4286 db_printf(" flags = 0x%x", pr->pr_flags);
4287 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4289 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4290 db_printf(" %s", pr_flag_names[fi]);
4291 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4293 jsf = pr->pr_flags &
4294 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4295 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4296 pr_flag_jailsys[fi].disable &&
4297 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4298 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4301 db_printf(" allow = 0x%x", pr->pr_allow);
4302 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4304 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4305 db_printf(" %s", pr_allow_names[fi]);
4307 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4308 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4309 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4310 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4311 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4313 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4314 for (ii = 0; ii < pr->pr_ip4s; ii++)
4315 db_printf(" %s %s\n",
4316 ii == 0 ? "ip4.addr =" : " ",
4317 inet_ntoa(pr->pr_ip4[ii]));
4320 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4321 for (ii = 0; ii < pr->pr_ip6s; ii++)
4322 db_printf(" %s %s\n",
4323 ii == 0 ? "ip6.addr =" : " ",
4324 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4328 DB_SHOW_COMMAND(prison, db_show_prison_command)
4334 * Show all prisons in the list, and prison0 which is not
4337 db_show_prison(&prison0);
4338 if (!db_pager_quit) {
4339 TAILQ_FOREACH(pr, &allprison, pr_list) {
4351 /* Look for a prison with the ID and with references. */
4352 TAILQ_FOREACH(pr, &allprison, pr_list)
4353 if (pr->pr_id == addr && pr->pr_ref > 0)
4356 /* Look again, without requiring a reference. */
4357 TAILQ_FOREACH(pr, &allprison, pr_list)
4358 if (pr->pr_id == addr)
4361 /* Assume address points to a valid prison. */
4362 pr = (struct prison *)addr;