2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2008 Bjoern A. Zeeb.
6 * Copyright (c) 2009 James Gritton.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/sysproto.h>
44 #include <sys/malloc.h>
48 #include <sys/taskqueue.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/mutex.h>
54 #include <sys/racct.h>
56 #include <sys/refcount.h>
58 #include <sys/sysent.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
71 #include <netinet/in.h>
77 #include <security/mac/mac_framework.h>
79 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
80 #define PRISON0_HOSTUUID_MODULE "hostuuid"
82 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
83 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
85 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
88 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
90 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
94 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
96 #define _PR_IP_SADDRSEL 0
100 /* prison0 describes what is "real" about the system. */
101 struct prison prison0 = {
107 .pr_securelevel = -1,
109 .pr_childmax = JAIL_MAX,
110 .pr_hostuuid = DEFAULT_HOSTUUID,
111 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
113 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
115 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
117 .pr_allow = PR_ALLOW_ALL_STATIC,
119 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
126 struct jailsys_flags {
132 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
133 struct sx allprison_lock;
134 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
135 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
136 LIST_HEAD(, prison_racct) allprison_racct;
139 static int get_next_prid(struct prison **insprp);
140 static int do_jail_attach(struct thread *td, struct prison *pr);
141 static void prison_complete(void *context, int pending);
142 static void prison_deref(struct prison *pr, int flags);
143 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
145 static char *prison_path(struct prison *pr1, struct prison *pr2);
146 static void prison_remove_one(struct prison *pr);
148 static void prison_racct_attach(struct prison *pr);
149 static void prison_racct_modify(struct prison *pr);
150 static void prison_racct_detach(struct prison *pr);
153 /* Flags for prison_deref */
154 #define PD_DEREF 0x01 /* Decrement pr_ref */
155 #define PD_DEUREF 0x02 /* Decrement pr_uref */
156 #define PD_LOCKED 0x04 /* pr_mtx is held */
157 #define PD_LIST_SLOCKED 0x08 /* allprison_lock is held shared */
158 #define PD_LIST_XLOCKED 0x10 /* allprison_lock is held exclusive */
161 * Parameter names corresponding to PR_* flag values. Size values are for kvm
162 * as we cannot figure out the size of a sparse array, or an array without a
165 static struct bool_flags pr_flag_bool[] = {
166 {"persist", "nopersist", PR_PERSIST},
168 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
171 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
174 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
176 static struct jailsys_flags pr_flag_jailsys[] = {
177 {"host", 0, PR_HOST},
179 {"vnet", 0, PR_VNET},
182 {"ip4", PR_IP4_USER, PR_IP4_USER},
185 {"ip6", PR_IP6_USER, PR_IP6_USER},
188 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
191 * Make this array full-size so dynamic parameters can be added.
192 * It is protected by prison0.mtx, but lockless reading is allowed
193 * with an atomic check of the flag values.
195 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
196 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
197 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
198 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
199 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
200 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
201 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
202 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
203 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
204 {"allow.reserved_ports", "allow.noreserved_ports",
205 PR_ALLOW_RESERVED_PORTS},
206 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
207 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
208 PR_ALLOW_UNPRIV_DEBUG},
209 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
211 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
212 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
214 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
215 PR_ALLOW_RESERVED_PORTS | \
216 PR_ALLOW_UNPRIV_DEBUG | \
218 #define JAIL_DEFAULT_ENFORCE_STATFS 2
219 #define JAIL_DEFAULT_DEVFS_RSNUM 0
220 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
221 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
222 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
223 #if defined(INET) || defined(INET6)
224 static unsigned jail_max_af_ips = 255;
228 * Initialize the parts of prison0 that can't be static-initialized with
229 * constants. This is called from proc0_init() after creating thread0 cpuset.
234 uint8_t *file, *data;
237 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
238 prison0.pr_osreldate = osreldate;
239 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
241 /* If we have a preloaded hostuuid, use it. */
242 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
244 data = preload_fetch_addr(file);
245 size = preload_fetch_size(file);
248 * The preloaded data may include trailing whitespace, almost
249 * certainly a newline; skip over any whitespace or
250 * non-printable characters to be safe.
252 while (size > 0 && data[size - 1] <= 0x20) {
255 if (validate_uuid(data, size, NULL, 0) == 0) {
256 (void)strlcpy(prison0.pr_hostuuid, data,
258 } else if (bootverbose) {
259 printf("hostuuid: preload data malformed: '%s'",
265 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
274 sys_jail(struct thread *td, struct jail_args *uap)
280 error = copyin(uap->jail, &version, sizeof(uint32_t));
289 /* FreeBSD single IPv4 jails. */
290 bzero(&j, sizeof(struct jail));
291 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
294 j.version = j0.version;
296 j.hostname = j0.hostname;
297 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
303 * Version 1 was used by multi-IPv4 jail implementations
304 * that never made it into the official kernel.
308 case 2: /* JAIL_API_VERSION */
309 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
310 error = copyin(uap->jail, &j, sizeof(struct jail));
316 /* Sci-Fi jails are not supported, sorry. */
319 return (kern_jail(td, &j));
323 kern_jail(struct thread *td, struct jail *j)
325 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
334 char *u_path, *u_hostname, *u_name;
335 struct bool_flags *bf;
338 struct in_addr *u_ip4;
341 struct in6_addr *u_ip6;
344 int error, enforce_statfs;
346 bzero(&optiov, sizeof(optiov));
347 opt.uio_iov = optiov;
351 opt.uio_segflg = UIO_SYSSPACE;
352 opt.uio_rw = UIO_READ;
355 /* Set permissions for top-level jails from sysctls. */
356 if (!jailed(td->td_ucred)) {
357 for (bf = pr_flag_allow;
358 bf < pr_flag_allow + nitems(pr_flag_allow) &&
359 atomic_load_int(&bf->flag) != 0;
361 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
362 (jail_default_allow & bf->flag)
363 ? bf->name : bf->noname);
364 optiov[opt.uio_iovcnt].iov_len =
365 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
368 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
369 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
371 enforce_statfs = jail_default_enforce_statfs;
372 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
373 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
377 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
379 ip4s = (j->version == 0) ? 1 : j->ip4s;
380 if (ip4s > jail_max_af_ips)
382 tmplen += ip4s * sizeof(struct in_addr);
388 if (j->ip6s > jail_max_af_ips)
390 tmplen += j->ip6s * sizeof(struct in6_addr);
395 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
396 u_hostname = u_path + MAXPATHLEN;
397 u_name = u_hostname + MAXHOSTNAMELEN;
399 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
403 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
405 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
408 optiov[opt.uio_iovcnt].iov_base = "path";
409 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
411 optiov[opt.uio_iovcnt].iov_base = u_path;
412 error = copyinstr(j->path, u_path, MAXPATHLEN,
413 &optiov[opt.uio_iovcnt].iov_len);
415 free(u_path, M_TEMP);
419 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
420 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
422 optiov[opt.uio_iovcnt].iov_base = u_hostname;
423 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
424 &optiov[opt.uio_iovcnt].iov_len);
426 free(u_path, M_TEMP);
430 if (j->jailname != NULL) {
431 optiov[opt.uio_iovcnt].iov_base = "name";
432 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
434 optiov[opt.uio_iovcnt].iov_base = u_name;
435 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
436 &optiov[opt.uio_iovcnt].iov_len);
438 free(u_path, M_TEMP);
444 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
445 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
447 optiov[opt.uio_iovcnt].iov_base = u_ip4;
448 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
450 u_ip4->s_addr = j->ip4s;
452 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
454 free(u_path, M_TEMP);
461 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
462 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
464 optiov[opt.uio_iovcnt].iov_base = u_ip6;
465 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
466 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
468 free(u_path, M_TEMP);
473 KASSERT(opt.uio_iovcnt <= nitems(optiov),
474 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
475 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
476 free(u_path, M_TEMP);
481 * struct jail_set_args {
482 * struct iovec *iovp;
483 * unsigned int iovcnt;
488 sys_jail_set(struct thread *td, struct jail_set_args *uap)
493 /* Check that we have an even number of iovecs. */
497 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
500 error = kern_jail_set(td, auio, uap->flags);
506 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
513 struct in6_addr *ip6;
516 struct vfsoptlist *opts;
517 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
519 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
520 char *g_path, *osrelstr;
521 struct bool_flags *bf;
522 struct jailsys_flags *jsf;
523 #if defined(INET) || defined(INET6)
528 size_t namelen, onamelen, pnamelen;
529 int born, created, cuflags, descend, drflags, enforce;
530 int error, errmsg_len, errmsg_pos;
531 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
532 int jid, jsys, len, level;
533 int childmax, osreldt, rsnum, slevel;
534 #if defined(INET) || defined(INET6)
543 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
544 uint64_t pr_allow_diff;
548 error = priv_check(td, PRIV_JAIL_SET);
549 if (!error && (flags & JAIL_ATTACH))
550 error = priv_check(td, PRIV_JAIL_ATTACH);
553 mypr = td->td_ucred->cr_prison;
554 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
556 if (flags & ~JAIL_SET_MASK)
560 * Check all the parameters before committing to anything. Not all
561 * errors can be caught early, but we may as well try. Also, this
562 * takes care of some expensive stuff (path lookup) before getting
563 * the allprison lock.
565 * XXX Jails are not filesystems, and jail parameters are not mount
566 * options. But it makes more sense to re-use the vfsopt code
567 * than duplicate it under a different name.
569 error = vfs_buildopts(optuio, &opts);
580 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
583 vfs_opterror(opts, "no valid operation (create or update)");
587 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
593 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
602 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
610 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
615 else if (enforce < 0 || enforce > 2) {
621 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
629 pr_flags = ch_flags = 0;
630 for (bf = pr_flag_bool;
631 bf < pr_flag_bool + nitems(pr_flag_bool);
633 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
634 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
636 ch_flags |= pr_flags;
637 for (jsf = pr_flag_jailsys;
638 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
640 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
646 case JAIL_SYS_DISABLE:
651 pr_flags |= jsf->disable;
654 pr_flags |= jsf->new;
656 case JAIL_SYS_INHERIT:
662 ch_flags |= jsf->new | jsf->disable;
664 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
665 && !(pr_flags & PR_PERSIST)) {
667 vfs_opterror(opts, "new jail must persist or attach");
671 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
673 vfs_opterror(opts, "vnet cannot be changed after creation");
678 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
680 vfs_opterror(opts, "ip4 cannot be changed after creation");
685 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
687 vfs_opterror(opts, "ip6 cannot be changed after creation");
692 pr_allow = ch_allow = 0;
693 for (bf = pr_flag_allow;
694 bf < pr_flag_allow + nitems(pr_flag_allow) &&
695 atomic_load_int(&bf->flag) != 0;
697 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
698 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
700 ch_allow |= pr_allow;
702 error = vfs_getopt(opts, "name", (void **)&name, &len);
708 if (len == 0 || name[len - 1] != '\0') {
712 if (len > MAXHOSTNAMELEN) {
713 error = ENAMETOOLONG;
718 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
726 if (len == 0 || host[len - 1] != '\0') {
730 if (len > MAXHOSTNAMELEN) {
731 error = ENAMETOOLONG;
736 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
744 if (len == 0 || domain[len - 1] != '\0') {
748 if (len > MAXHOSTNAMELEN) {
749 error = ENAMETOOLONG;
754 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
762 if (len == 0 || uuid[len - 1] != '\0') {
766 if (len > HOSTUUIDLEN) {
767 error = ENAMETOOLONG;
772 #ifdef COMPAT_FREEBSD32
773 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
776 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
780 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
792 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
797 else if (ip4s & (sizeof(*ip4) - 1)) {
801 ch_flags |= PR_IP4_USER;
802 pr_flags |= PR_IP4_USER;
804 ip4s /= sizeof(*ip4);
805 if (ip4s > jail_max_af_ips) {
807 vfs_opterror(opts, "too many IPv4 addresses");
810 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
811 bcopy(op, ip4, ip4s * sizeof(*ip4));
813 * IP addresses are all sorted but ip[0] to preserve
814 * the primary IP address as given from userland.
815 * This special IP is used for unbound outgoing
816 * connections as well for "loopback" traffic in case
817 * source address selection cannot find any more fitting
818 * address to connect from.
821 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
824 * Check for duplicate addresses and do some simple
825 * zero and broadcast checks. If users give other bogus
826 * addresses it is their problem.
828 * We do not have to care about byte order for these
829 * checks so we will do them in NBO.
831 for (ii = 0; ii < ip4s; ii++) {
832 if (ip4[ii].s_addr == INADDR_ANY ||
833 ip4[ii].s_addr == INADDR_BROADCAST) {
838 (ip4[0].s_addr == ip4[ii+1].s_addr ||
839 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
849 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
854 else if (ip6s & (sizeof(*ip6) - 1)) {
858 ch_flags |= PR_IP6_USER;
859 pr_flags |= PR_IP6_USER;
861 ip6s /= sizeof(*ip6);
862 if (ip6s > jail_max_af_ips) {
864 vfs_opterror(opts, "too many IPv6 addresses");
867 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
868 bcopy(op, ip6, ip6s * sizeof(*ip6));
870 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
872 for (ii = 0; ii < ip6s; ii++) {
873 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
878 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
879 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
889 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
890 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
893 "vnet jails cannot have IP address restrictions");
898 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
904 if (flags & JAIL_UPDATE) {
907 "osrelease cannot be changed after creation");
910 if (len == 0 || osrelstr[len - 1] != '\0') {
914 if (len >= OSRELEASELEN) {
915 error = ENAMETOOLONG;
917 "osrelease string must be 1-%d bytes long",
923 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
929 if (flags & JAIL_UPDATE) {
932 "osreldate cannot be changed after creation");
937 vfs_opterror(opts, "osreldate cannot be 0");
943 error = vfs_getopt(opts, "path", (void **)&path, &len);
949 if (flags & JAIL_UPDATE) {
952 "path cannot be changed after creation");
955 if (len == 0 || path[len - 1] != '\0') {
959 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
965 NDFREE(&nd, NDF_ONLY_PNBUF);
966 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
967 strlcpy(g_path, path, MAXPATHLEN);
968 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
972 /* exit on other errors */
975 if (root->v_type != VDIR) {
984 * Find the specified jail, or at least its parent.
985 * This abuses the file error codes ENOENT and EEXIST.
990 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
991 namelc = strrchr(name, '.');
992 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
996 sx_xlock(&allprison_lock);
997 drflags = PD_LIST_XLOCKED;
1001 vfs_opterror(opts, "negative jid");
1005 * See if a requested jid already exists. Keep track of
1006 * where it can be inserted later.
1008 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1009 if (inspr->pr_id == jid) {
1010 mtx_lock(&inspr->pr_mtx);
1011 if (prison_isvalid(inspr)) {
1013 drflags |= PD_LOCKED;
1016 mtx_unlock(&inspr->pr_mtx);
1019 if (inspr->pr_id > jid)
1023 ppr = pr->pr_parent;
1024 /* Create: jid must not exist. */
1025 if (cuflags == JAIL_CREATE) {
1027 * Even creators that cannot see the jail will
1031 vfs_opterror(opts, "jail %d already exists",
1035 if (!prison_ischild(mypr, pr)) {
1037 * Updaters get ENOENT if they cannot see the
1038 * jail. This is true even for CREATE | UPDATE,
1039 * which normally cannot give this error.
1042 vfs_opterror(opts, "jail %d not found", jid);
1044 } else if (!prison_isalive(pr)) {
1045 if (!(flags & JAIL_DYING)) {
1047 vfs_opterror(opts, "jail %d is dying",
1050 } else if ((flags & JAIL_ATTACH) ||
1051 (pr_flags & PR_PERSIST)) {
1053 * A dying jail might be resurrected
1054 * (via attach or persist), but first
1055 * it must determine if another jail
1056 * has claimed its name. Accomplish
1057 * this by implicitly re-setting the
1061 name = prison_name(mypr, pr);
1065 /* Update: jid must exist. */
1066 if (cuflags == JAIL_UPDATE) {
1068 vfs_opterror(opts, "jail %d not found", jid);
1074 * If the caller provided a name, look for a jail by that name.
1075 * This has different semantics for creates and updates keyed by jid
1076 * (where the name must not already exist in a different jail),
1077 * and updates keyed by the name itself (where the name must exist
1078 * because that is the jail being updated).
1082 namelc = strrchr(name, '.');
1087 * This is a hierarchical name. Split it into the
1088 * parent and child names, and make sure the parent
1089 * exists or matches an already found jail.
1092 if (strncmp(name, ppr->pr_name, namelc - name)
1093 || ppr->pr_name[namelc - name] != '\0') {
1096 "cannot change jail's parent");
1101 ppr = prison_find_name(mypr, name);
1105 "jail \"%s\" not found", name);
1108 if (!(flags & JAIL_DYING) &&
1109 !prison_isalive(ppr)) {
1110 mtx_unlock(&ppr->pr_mtx);
1113 "jail \"%s\" is dying", name);
1116 mtx_unlock(&ppr->pr_mtx);
1121 if (namelc[0] != '\0') {
1123 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1126 FOREACH_PRISON_CHILD(ppr, tpr) {
1128 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1129 mtx_lock(&tpr->pr_mtx);
1130 if (prison_isalive(tpr)) {
1132 cuflags != JAIL_CREATE) {
1138 drflags |= PD_LOCKED;
1142 * Create, or update(jid):
1143 * name must not exist in an
1144 * active sibling jail.
1147 mtx_unlock(&tpr->pr_mtx);
1149 "jail \"%s\" already exists",
1154 cuflags != JAIL_CREATE &&
1155 prison_isvalid(tpr))
1157 mtx_unlock(&tpr->pr_mtx);
1160 /* If no active jail is found, use a dying one. */
1161 if (deadpr != NULL && pr == NULL) {
1162 if (flags & JAIL_DYING) {
1163 mtx_lock(&deadpr->pr_mtx);
1164 if (!prison_isvalid(deadpr)) {
1165 mtx_unlock(&deadpr->pr_mtx);
1169 drflags |= PD_LOCKED;
1170 } else if (cuflags == JAIL_UPDATE) {
1173 "jail \"%s\" is dying", name);
1177 /* Update: name must exist if no jid. */
1178 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1180 vfs_opterror(opts, "jail \"%s\" not found",
1186 /* Update: must provide a jid or name. */
1187 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1189 vfs_opterror(opts, "update specified no jail");
1193 /* If there's no prison to update, create a new one and link it in. */
1194 created = pr == NULL;
1196 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1197 if (tpr->pr_childcount >= tpr->pr_childmax) {
1199 vfs_opterror(opts, "prison limit exceeded");
1202 mtx_lock(&ppr->pr_mtx);
1203 if (!prison_isvalid(ppr)) {
1204 mtx_unlock(&ppr->pr_mtx);
1206 vfs_opterror(opts, "jail \"%s\" not found",
1207 prison_name(mypr, ppr));
1211 if (refcount_acquire(&ppr->pr_uref))
1212 mtx_unlock(&ppr->pr_mtx);
1214 /* This brings the parent back to life. */
1215 mtx_unlock(&ppr->pr_mtx);
1216 error = osd_jail_call(ppr, PR_METHOD_CREATE, opts);
1219 drflags |= PD_DEREF | PD_DEUREF;
1224 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1226 vfs_opterror(opts, "no available jail IDs");
1228 drflags |= PD_DEREF | PD_DEUREF;
1232 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1233 LIST_INIT(&pr->pr_children);
1234 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1235 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1239 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1241 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1243 pr->pr_parent = ppr;
1244 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1245 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1246 tpr->pr_childcount++;
1248 /* Set some default values, and inherit some from the parent. */
1253 root = mypr->pr_root;
1256 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1257 pr->pr_flags |= PR_HOST;
1258 #if defined(INET) || defined(INET6)
1260 if (!(pr_flags & PR_VNET))
1264 if (!(ch_flags & PR_IP4_USER))
1265 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1266 else if (!(pr_flags & PR_IP4_USER)) {
1267 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1268 if (ppr->pr_ip4 != NULL) {
1269 pr->pr_ip4s = ppr->pr_ip4s;
1270 pr->pr_ip4 = malloc(pr->pr_ip4s *
1271 sizeof(struct in_addr), M_PRISON,
1273 bcopy(ppr->pr_ip4, pr->pr_ip4,
1274 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1279 if (!(ch_flags & PR_IP6_USER))
1280 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1281 else if (!(pr_flags & PR_IP6_USER)) {
1282 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1283 if (ppr->pr_ip6 != NULL) {
1284 pr->pr_ip6s = ppr->pr_ip6s;
1285 pr->pr_ip6 = malloc(pr->pr_ip6s *
1286 sizeof(struct in6_addr), M_PRISON,
1288 bcopy(ppr->pr_ip6, pr->pr_ip6,
1289 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1295 /* Source address selection is always on by default. */
1296 pr->pr_flags |= _PR_IP_SADDRSEL;
1298 pr->pr_securelevel = ppr->pr_securelevel;
1299 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1300 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1301 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1303 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1304 if (osrelstr == NULL)
1305 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1306 sizeof(pr->pr_osrelease));
1308 strlcpy(pr->pr_osrelease, osrelstr,
1309 sizeof(pr->pr_osrelease));
1312 /* Allocate a new vnet if specified. */
1313 pr->pr_vnet = (pr_flags & PR_VNET)
1314 ? vnet_alloc() : ppr->pr_vnet;
1317 * Allocate a dedicated cpuset for each jail.
1318 * Unlike other initial settings, this may return an erorr.
1320 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1324 mtx_lock(&pr->pr_mtx);
1325 drflags |= PD_LOCKED;
1327 * New prisons do not yet have a reference, because we do not
1328 * want others to see the incomplete prison once the
1329 * allprison_lock is downgraded.
1333 * Grab a reference for existing prisons, to ensure they
1334 * continue to exist for the duration of the call.
1337 drflags |= PD_DEREF;
1338 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1339 if ((pr->pr_flags & PR_VNET) &&
1340 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1343 "vnet jails cannot have IP address restrictions");
1348 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1351 "ip4 cannot be changed after creation");
1356 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1359 "ip6 cannot be changed after creation");
1365 /* Do final error checking before setting anything. */
1367 if (slevel < ppr->pr_securelevel) {
1373 if (childmax >= ppr->pr_childmax) {
1379 if (enforce < ppr->pr_enforce_statfs) {
1386 * devfs_rsnum is a uint16_t
1388 if (rsnum < 0 || rsnum > 65535) {
1393 * Nested jails always inherit parent's devfs ruleset
1395 if (jailed(td->td_ucred)) {
1396 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1400 rsnum = ppr->pr_devfs_rsnum;
1405 if (ppr->pr_flags & PR_IP4) {
1407 * Make sure the new set of IP addresses is a
1408 * subset of the parent's list. Don't worry
1409 * about the parent being unlocked, as any
1410 * setting is done with allprison_lock held.
1412 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1413 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1415 if (ij == ppr->pr_ip4s) {
1420 for (ii = ij = 1; ii < ip4s; ii++) {
1421 if (ip4[ii].s_addr ==
1422 ppr->pr_ip4[0].s_addr)
1424 for (; ij < ppr->pr_ip4s; ij++)
1425 if (ip4[ii].s_addr ==
1426 ppr->pr_ip4[ij].s_addr)
1428 if (ij == ppr->pr_ip4s)
1431 if (ij == ppr->pr_ip4s) {
1438 * Check for conflicting IP addresses. We permit them
1439 * if there is no more than one IP on each jail. If
1440 * there is a duplicate on a jail with more than one
1441 * IP stop checking and return error.
1444 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1445 if (tppr->pr_flags & PR_VNET)
1450 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1453 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1455 refcount_load(&tpr->pr_uref) == 0) {
1459 if (!(tpr->pr_flags & PR_IP4_USER))
1462 if (tpr->pr_ip4 == NULL ||
1463 (ip4s == 1 && tpr->pr_ip4s == 1))
1465 for (ii = 0; ii < ip4s; ii++) {
1466 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1470 "IPv4 addresses clash");
1479 if (ppr->pr_flags & PR_IP6) {
1481 * Make sure the new set of IP addresses is a
1482 * subset of the parent's list.
1484 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1485 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1488 if (ij == ppr->pr_ip6s) {
1493 for (ii = ij = 1; ii < ip6s; ii++) {
1494 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1497 for (; ij < ppr->pr_ip6s; ij++)
1498 if (IN6_ARE_ADDR_EQUAL(
1499 &ip6[ii], &ppr->pr_ip6[ij]))
1501 if (ij == ppr->pr_ip6s)
1504 if (ij == ppr->pr_ip6s) {
1510 /* Check for conflicting IP addresses. */
1512 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1513 if (tppr->pr_flags & PR_VNET)
1518 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1521 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1523 refcount_load(&tpr->pr_uref) == 0) {
1527 if (!(tpr->pr_flags & PR_IP6_USER))
1530 if (tpr->pr_ip6 == NULL ||
1531 (ip6s == 1 && tpr->pr_ip6s == 1))
1533 for (ii = 0; ii < ip6s; ii++) {
1534 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1538 "IPv6 addresses clash");
1545 onamelen = namelen = 0;
1546 if (namelc != NULL) {
1547 /* Give a default name of the jid. Also allow the name to be
1548 * explicitly the jid - but not any other number, and only in
1549 * normal form (no leading zero/etc).
1551 if (namelc[0] == '\0')
1552 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1553 else if ((strtoul(namelc, &p, 10) != jid ||
1554 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1557 "name cannot be numeric (unless it is the jid)");
1561 * Make sure the name isn't too long for the prison or its
1564 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1565 onamelen = strlen(pr->pr_name + pnamelen);
1566 namelen = strlen(namelc);
1567 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1568 error = ENAMETOOLONG;
1571 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1572 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1573 sizeof(pr->pr_name)) {
1574 error = ENAMETOOLONG;
1579 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1580 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1586 * Let modules check their parameters. This requires unlocking and
1587 * then re-locking the prison, but this is still a valid state as long
1588 * as allprison_lock remains xlocked.
1590 mtx_unlock(&pr->pr_mtx);
1591 drflags &= ~PD_LOCKED;
1592 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1595 mtx_lock(&pr->pr_mtx);
1596 drflags |= PD_LOCKED;
1598 /* At this point, all valid parameters should have been noted. */
1599 TAILQ_FOREACH(opt, opts, link) {
1600 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1602 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1607 /* Set the parameters of the prison. */
1610 if (pr_flags & PR_IP4_USER) {
1611 pr->pr_flags |= PR_IP4;
1612 free(pr->pr_ip4, M_PRISON);
1616 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1618 if (tpr->pr_flags & PR_VNET) {
1623 if (prison_restrict_ip4(tpr, NULL)) {
1632 if (pr_flags & PR_IP6_USER) {
1633 pr->pr_flags |= PR_IP6;
1634 free(pr->pr_ip6, M_PRISON);
1638 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1640 if (tpr->pr_flags & PR_VNET) {
1645 if (prison_restrict_ip6(tpr, NULL)) {
1653 pr->pr_securelevel = slevel;
1654 /* Set all child jails to be at least this level. */
1655 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1656 if (tpr->pr_securelevel < slevel)
1657 tpr->pr_securelevel = slevel;
1660 pr->pr_childmax = childmax;
1661 /* Set all child jails to under this limit. */
1662 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1663 if (tpr->pr_childmax > childmax - level)
1664 tpr->pr_childmax = childmax > level
1665 ? childmax - level : 0;
1668 pr->pr_enforce_statfs = enforce;
1669 /* Pass this restriction on to the children. */
1670 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1671 if (tpr->pr_enforce_statfs < enforce)
1672 tpr->pr_enforce_statfs = enforce;
1675 pr->pr_devfs_rsnum = rsnum;
1676 /* Pass this restriction on to the children. */
1677 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1678 tpr->pr_devfs_rsnum = rsnum;
1680 if (namelc != NULL) {
1681 if (ppr == &prison0)
1682 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1684 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1685 ppr->pr_name, namelc);
1686 /* Change this component of child names. */
1687 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1688 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1689 strlen(tpr->pr_name + onamelen) + 1);
1690 bcopy(pr->pr_name, tpr->pr_name, namelen);
1694 /* Try to keep a real-rooted full pathname. */
1695 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1699 if (PR_HOST & ch_flags & ~pr_flags) {
1700 if (pr->pr_flags & PR_HOST) {
1702 * Copy the parent's host info. As with pr_ip4 above,
1703 * the lack of a lock on the parent is not a problem;
1704 * it is always set with allprison_lock at least
1705 * shared, and is held exclusively here.
1707 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1708 sizeof(pr->pr_hostname));
1709 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1710 sizeof(pr->pr_domainname));
1711 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1712 sizeof(pr->pr_hostuuid));
1713 pr->pr_hostid = pr->pr_parent->pr_hostid;
1715 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1716 /* Set this prison, and any descendants without PR_HOST. */
1718 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1720 strlcpy(pr->pr_domainname, domain,
1721 sizeof(pr->pr_domainname));
1723 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1725 pr->pr_hostid = hid;
1726 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1727 if (tpr->pr_flags & PR_HOST)
1731 strlcpy(tpr->pr_hostname,
1733 sizeof(tpr->pr_hostname));
1735 strlcpy(tpr->pr_domainname,
1737 sizeof(tpr->pr_domainname));
1739 strlcpy(tpr->pr_hostuuid,
1741 sizeof(tpr->pr_hostuuid));
1743 tpr->pr_hostid = hid;
1747 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1748 if ((tallow = ch_allow & ~pr_allow))
1749 prison_set_allow_locked(pr, tallow, 0);
1751 * Persistent prisons get an extra reference, and prisons losing their
1752 * persist flag lose that reference. Only do this for existing prisons
1753 * for now, so new ones will remain unseen until after the module
1754 * handlers have completed.
1756 born = !prison_isalive(pr);
1757 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1758 if (pr_flags & PR_PERSIST) {
1760 refcount_acquire(&pr->pr_uref);
1762 refcount_release(&pr->pr_ref);
1763 drflags |= PD_DEUREF;
1766 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1767 mtx_unlock(&pr->pr_mtx);
1768 drflags &= ~PD_LOCKED;
1771 if (racct_enable && created)
1772 prison_racct_attach(pr);
1775 /* Locks may have prevented a complete restriction of child IP
1776 * addresses. If so, allocate some more memory and try again.
1781 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1782 mtx_lock(&pr->pr_mtx);
1784 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1786 if (tpr->pr_flags & PR_VNET) {
1791 if (prison_restrict_ip4(tpr, ip4)) {
1798 mtx_unlock(&pr->pr_mtx);
1804 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1805 mtx_lock(&pr->pr_mtx);
1807 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1809 if (tpr->pr_flags & PR_VNET) {
1814 if (prison_restrict_ip6(tpr, ip6)) {
1821 mtx_unlock(&pr->pr_mtx);
1825 /* Let the modules do their work. */
1826 sx_downgrade(&allprison_lock);
1827 drflags = (drflags & ~PD_LIST_XLOCKED) | PD_LIST_SLOCKED;
1829 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1831 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1835 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1838 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1842 /* Attach this process to the prison if requested. */
1843 if (flags & JAIL_ATTACH) {
1844 mtx_lock(&pr->pr_mtx);
1845 error = do_jail_attach(td, pr);
1846 drflags &= ~PD_LIST_SLOCKED;
1849 /* do_jail_attach has removed the prison. */
1852 vfs_opterror(opts, "attach failed");
1858 if (racct_enable && !created) {
1859 if (drflags & PD_LIST_SLOCKED) {
1860 sx_sunlock(&allprison_lock);
1861 drflags &= ~PD_LIST_SLOCKED;
1863 prison_racct_modify(pr);
1867 td->td_retval[0] = pr->pr_id;
1871 * Add a reference to newly created persistent prisons
1872 * (which was not done earlier so that the prison would
1873 * not be publicly visible).
1875 if (pr_flags & PR_PERSIST) {
1876 mtx_lock(&pr->pr_mtx);
1877 drflags |= PD_LOCKED;
1878 refcount_acquire(&pr->pr_ref);
1879 refcount_acquire(&pr->pr_uref);
1881 /* Non-persistent jails need no further changes. */
1887 /* Release any temporary prison holds and/or locks. */
1889 prison_deref(pr, drflags);
1890 else if (drflags & PD_LIST_SLOCKED)
1891 sx_sunlock(&allprison_lock);
1892 else if (drflags & PD_LIST_XLOCKED)
1893 sx_xunlock(&allprison_lock);
1898 /* Write the error message back to userspace. */
1899 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1900 &errmsg_len) == 0 && errmsg_len > 0) {
1901 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1902 if (optuio->uio_segflg == UIO_SYSSPACE)
1904 optuio->uio_iov[errmsg_pos].iov_base,
1908 optuio->uio_iov[errmsg_pos].iov_base,
1914 free(ip4, M_PRISON);
1917 free(ip6, M_PRISON);
1920 free(g_path, M_TEMP);
1926 * Find the next available prison ID. Return the ID on success, or zero
1927 * on failure. Also set a pointer to the allprison list entry the prison
1928 * should be inserted before.
1931 get_next_prid(struct prison **insprp)
1933 struct prison *inspr;
1936 jid = lastprid % JAIL_MAX + 1;
1937 if (TAILQ_EMPTY(&allprison) ||
1938 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1940 * A common case is for all jails to be implicitly numbered,
1941 * which means they'll go on the end of the list, at least
1942 * for the first JAIL_MAX times.
1947 * Take two passes through the allprison list: first starting
1948 * with the proposed jid, then ending with it.
1950 for (maxid = JAIL_MAX; maxid != 0; ) {
1951 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1952 if (inspr->pr_id < jid)
1954 if (inspr->pr_id > jid ||
1955 refcount_load(&inspr->pr_ref) == 0) {
1957 * Found an opening. This may be a gap
1958 * in the list, or a dead jail with the
1964 if (++jid > maxid) {
1965 if (lastprid == maxid || lastprid == 0)
1968 * The entire legal range
1969 * has been traversed
1973 /* Try again from the start. */
1979 if (inspr == NULL) {
1980 /* Found room at the end of the list. */
1991 * struct jail_get_args {
1992 * struct iovec *iovp;
1993 * unsigned int iovcnt;
1998 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2003 /* Check that we have an even number of iovecs. */
2004 if (uap->iovcnt & 1)
2007 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2010 error = kern_jail_get(td, auio, uap->flags);
2012 error = copyout(auio->uio_iov, uap->iovp,
2013 uap->iovcnt * sizeof (struct iovec));
2019 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2021 struct bool_flags *bf;
2022 struct jailsys_flags *jsf;
2023 struct prison *pr, *mypr;
2025 struct vfsoptlist *opts;
2026 char *errmsg, *name;
2027 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2030 if (flags & ~JAIL_GET_MASK)
2033 /* Get the parameter list. */
2034 error = vfs_buildopts(optuio, &opts);
2037 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2038 mypr = td->td_ucred->cr_prison;
2042 * Find the prison specified by one of: lastjid, jid, name.
2044 sx_slock(&allprison_lock);
2045 drflags = PD_LIST_SLOCKED;
2046 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2048 TAILQ_FOREACH(pr, &allprison, pr_list) {
2049 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
2050 mtx_lock(&pr->pr_mtx);
2051 if ((flags & JAIL_DYING)
2052 ? prison_isvalid(pr) : prison_isalive(pr))
2054 mtx_unlock(&pr->pr_mtx);
2058 drflags |= PD_LOCKED;
2062 vfs_opterror(opts, "no jail after %d", jid);
2064 } else if (error != ENOENT)
2067 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2070 pr = prison_find_child(mypr, jid);
2072 drflags |= PD_LOCKED;
2073 if (!(prison_isalive(pr) ||
2074 (flags & JAIL_DYING))) {
2076 vfs_opterror(opts, "jail %d is dying",
2083 vfs_opterror(opts, "jail %d not found", jid);
2086 } else if (error != ENOENT)
2089 error = vfs_getopt(opts, "name", (void **)&name, &len);
2091 if (len == 0 || name[len - 1] != '\0') {
2095 pr = prison_find_name(mypr, name);
2097 drflags |= PD_LOCKED;
2098 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2100 vfs_opterror(opts, "jail \"%s\" is dying",
2107 vfs_opterror(opts, "jail \"%s\" not found", name);
2109 } else if (error != ENOENT)
2112 vfs_opterror(opts, "no jail specified");
2117 /* Get the parameters of the prison. */
2119 drflags |= PD_DEREF;
2120 td->td_retval[0] = pr->pr_id;
2121 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2122 if (error != 0 && error != ENOENT)
2124 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2125 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2126 if (error != 0 && error != ENOENT)
2128 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2129 if (error != 0 && error != ENOENT)
2131 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2132 sizeof(pr->pr_cpuset->cs_id));
2133 if (error != 0 && error != ENOENT)
2135 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2136 if (error != 0 && error != ENOENT)
2139 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2140 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2141 if (error != 0 && error != ENOENT)
2145 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2146 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2147 if (error != 0 && error != ENOENT)
2150 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2151 sizeof(pr->pr_securelevel));
2152 if (error != 0 && error != ENOENT)
2154 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2155 sizeof(pr->pr_childcount));
2156 if (error != 0 && error != ENOENT)
2158 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2159 sizeof(pr->pr_childmax));
2160 if (error != 0 && error != ENOENT)
2162 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2163 if (error != 0 && error != ENOENT)
2165 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2166 if (error != 0 && error != ENOENT)
2168 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2169 if (error != 0 && error != ENOENT)
2171 #ifdef COMPAT_FREEBSD32
2172 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2173 uint32_t hid32 = pr->pr_hostid;
2175 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2178 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2179 sizeof(pr->pr_hostid));
2180 if (error != 0 && error != ENOENT)
2182 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2183 sizeof(pr->pr_enforce_statfs));
2184 if (error != 0 && error != ENOENT)
2186 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2187 sizeof(pr->pr_devfs_rsnum));
2188 if (error != 0 && error != ENOENT)
2190 for (bf = pr_flag_bool;
2191 bf < pr_flag_bool + nitems(pr_flag_bool);
2193 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2194 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2195 if (error != 0 && error != ENOENT)
2198 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2199 if (error != 0 && error != ENOENT)
2202 for (jsf = pr_flag_jailsys;
2203 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2205 f = pr->pr_flags & (jsf->disable | jsf->new);
2206 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2207 : (f == jsf->new) ? JAIL_SYS_NEW
2209 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2210 if (error != 0 && error != ENOENT)
2213 for (bf = pr_flag_allow;
2214 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2215 atomic_load_int(&bf->flag) != 0;
2217 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2218 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2219 if (error != 0 && error != ENOENT)
2222 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2223 if (error != 0 && error != ENOENT)
2226 i = !prison_isalive(pr);
2227 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2228 if (error != 0 && error != ENOENT)
2231 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2232 if (error != 0 && error != ENOENT)
2234 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2235 sizeof(pr->pr_osreldate));
2236 if (error != 0 && error != ENOENT)
2238 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2239 if (error != 0 && error != ENOENT)
2242 /* Get the module parameters. */
2243 mtx_unlock(&pr->pr_mtx);
2244 drflags &= ~PD_LOCKED;
2245 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2248 prison_deref(pr, drflags);
2252 /* By now, all parameters should have been noted. */
2253 TAILQ_FOREACH(opt, opts, link) {
2254 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2256 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2261 /* Write the fetched parameters back to userspace. */
2263 TAILQ_FOREACH(opt, opts, link) {
2264 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2265 pos = 2 * opt->pos + 1;
2266 optuio->uio_iov[pos].iov_len = opt->len;
2267 if (opt->value != NULL) {
2268 if (optuio->uio_segflg == UIO_SYSSPACE) {
2270 optuio->uio_iov[pos].iov_base,
2273 error = copyout(opt->value,
2274 optuio->uio_iov[pos].iov_base,
2284 /* Release any temporary prison holds and/or locks. */
2286 prison_deref(pr, drflags);
2287 else if (drflags & PD_LIST_SLOCKED)
2288 sx_sunlock(&allprison_lock);
2289 if (error && errmsg_pos >= 0) {
2290 /* Write the error message back to userspace. */
2291 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2292 errmsg_pos = 2 * errmsg_pos + 1;
2293 if (errmsg_len > 0) {
2294 if (optuio->uio_segflg == UIO_SYSSPACE)
2296 optuio->uio_iov[errmsg_pos].iov_base,
2300 optuio->uio_iov[errmsg_pos].iov_base,
2309 * struct jail_remove_args {
2314 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2316 struct prison *pr, *cpr, *lpr, *tpr;
2319 error = priv_check(td, PRIV_JAIL_REMOVE);
2323 sx_xlock(&allprison_lock);
2324 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2326 sx_xunlock(&allprison_lock);
2330 /* Remove all descendants of this prison, then remove this prison. */
2332 if (!LIST_EMPTY(&pr->pr_children)) {
2333 mtx_unlock(&pr->pr_mtx);
2335 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2336 mtx_lock(&cpr->pr_mtx);
2337 if (prison_isvalid(cpr)) {
2341 /* Already removed - do not do it again. */
2344 mtx_unlock(&cpr->pr_mtx);
2346 mtx_lock(&lpr->pr_mtx);
2347 prison_remove_one(lpr);
2348 sx_xlock(&allprison_lock);
2353 mtx_lock(&lpr->pr_mtx);
2354 prison_remove_one(lpr);
2355 sx_xlock(&allprison_lock);
2357 mtx_lock(&pr->pr_mtx);
2359 prison_remove_one(pr);
2364 prison_remove_one(struct prison *pr)
2369 drflags = PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED;
2371 /* If the prison was persistent, it is not anymore. */
2372 if (pr->pr_flags & PR_PERSIST) {
2373 refcount_release(&pr->pr_ref);
2374 drflags |= PD_DEUREF;
2375 pr->pr_flags &= ~PR_PERSIST;
2379 * jail_remove added a reference. If that's the only one, remove
2380 * the prison now. refcount(9) doesn't guarantee the cache coherence
2381 * of non-zero counters, so force it here.
2383 KASSERT(refcount_load(&pr->pr_ref) > 0,
2384 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2385 if (atomic_load_acq_int(&pr->pr_ref) == 1) {
2386 prison_deref(pr, drflags);
2390 mtx_unlock(&pr->pr_mtx);
2391 sx_xunlock(&allprison_lock);
2392 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2394 * Kill all processes unfortunate enough to be attached to this prison.
2396 sx_slock(&allproc_lock);
2397 FOREACH_PROC_IN_SYSTEM(p) {
2399 if (p->p_state != PRS_NEW && p->p_ucred &&
2400 p->p_ucred->cr_prison == pr)
2401 kern_psignal(p, SIGKILL);
2404 sx_sunlock(&allproc_lock);
2405 /* Remove the temporary reference added by jail_remove. */
2406 prison_deref(pr, drflags);
2410 * struct jail_attach_args {
2415 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2420 error = priv_check(td, PRIV_JAIL_ATTACH);
2425 * Start with exclusive hold on allprison_lock to ensure that a possible
2426 * PR_METHOD_REMOVE call isn't concurrent with jail_set or jail_remove.
2427 * But then immediately downgrade it since we don't need to stop
2430 sx_xlock(&allprison_lock);
2431 sx_downgrade(&allprison_lock);
2432 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2434 sx_sunlock(&allprison_lock);
2438 /* Do not allow a process to attach to a prison that is not alive. */
2439 if (!prison_isalive(pr)) {
2440 mtx_unlock(&pr->pr_mtx);
2441 sx_sunlock(&allprison_lock);
2445 return (do_jail_attach(td, pr));
2449 do_jail_attach(struct thread *td, struct prison *pr)
2452 struct ucred *newcred, *oldcred;
2456 * XXX: Note that there is a slight race here if two threads
2457 * in the same privileged process attempt to attach to two
2458 * different jails at the same time. It is important for
2459 * user processes not to do this, or they might end up with
2460 * a process root from one prison, but attached to the jail
2463 refcount_acquire(&pr->pr_ref);
2464 refcount_acquire(&pr->pr_uref);
2465 mtx_unlock(&pr->pr_mtx);
2467 /* Let modules do whatever they need to prepare for attaching. */
2468 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2470 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2473 sx_sunlock(&allprison_lock);
2476 * Reparent the newly attached process to this jail.
2479 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2483 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2484 if ((error = change_dir(pr->pr_root, td)) != 0)
2487 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2490 VOP_UNLOCK(pr->pr_root);
2491 if ((error = pwd_chroot(td, pr->pr_root)))
2496 oldcred = crcopysafe(p, newcred);
2497 newcred->cr_prison = pr;
2498 proc_set_cred(p, newcred);
2501 racct_proc_ucred_changed(p, oldcred, newcred);
2506 rctl_proc_ucred_changed(p, newcred);
2509 prison_deref(oldcred->cr_prison, PD_DEREF | PD_DEUREF);
2514 VOP_UNLOCK(pr->pr_root);
2516 /* Tell modules this thread is still in its old jail after all. */
2517 sx_slock(&allprison_lock);
2518 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2519 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2524 * Returns a locked prison instance, or NULL on failure.
2527 prison_find(int prid)
2531 sx_assert(&allprison_lock, SX_LOCKED);
2532 TAILQ_FOREACH(pr, &allprison, pr_list) {
2533 if (pr->pr_id == prid) {
2534 mtx_lock(&pr->pr_mtx);
2535 if (prison_isvalid(pr))
2538 * Any active prison with the same ID would have
2539 * been inserted before a dead one.
2541 mtx_unlock(&pr->pr_mtx);
2544 if (pr->pr_id > prid)
2551 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2554 prison_find_child(struct prison *mypr, int prid)
2559 sx_assert(&allprison_lock, SX_LOCKED);
2560 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2561 if (pr->pr_id == prid) {
2562 mtx_lock(&pr->pr_mtx);
2563 if (prison_isvalid(pr))
2565 mtx_unlock(&pr->pr_mtx);
2572 * Look for the name relative to mypr. Returns a locked prison or NULL.
2575 prison_find_name(struct prison *mypr, const char *name)
2577 struct prison *pr, *deadpr;
2581 sx_assert(&allprison_lock, SX_LOCKED);
2582 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2585 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2586 if (!strcmp(pr->pr_name + mylen, name)) {
2587 mtx_lock(&pr->pr_mtx);
2588 if (prison_isalive(pr))
2590 if (prison_isvalid(pr))
2592 mtx_unlock(&pr->pr_mtx);
2595 /* There was no valid prison - perhaps there was a dying one. */
2596 if (deadpr != NULL) {
2597 mtx_lock(&deadpr->pr_mtx);
2598 if (!prison_isvalid(deadpr)) {
2599 mtx_unlock(&deadpr->pr_mtx);
2607 * See if a prison has the specific flag set. The prison should be locked,
2608 * unless checking for flags that are only set at jail creation (such as
2609 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2610 * to any other prison data.
2613 prison_flag(struct ucred *cred, unsigned flag)
2616 return (cred->cr_prison->pr_flags & flag);
2620 prison_allow(struct ucred *cred, unsigned flag)
2623 return ((cred->cr_prison->pr_allow & flag) != 0);
2627 * Hold a prison reference, by incrementing pr_ref. It is generally
2628 * an error to hold a prison that does not already have a reference.
2629 * A prison record will remain valid as long as it has at least one
2630 * reference, and will not be removed as long as either the prison
2631 * mutex or the allprison lock is held (allprison_lock may be shared).
2634 prison_hold_locked(struct prison *pr)
2637 /* Locking is no longer required. */
2642 prison_hold(struct prison *pr)
2645 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2648 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2650 refcount_acquire(&pr->pr_ref);
2655 * Remove a prison reference. If that was the last reference, the
2656 * prison will be removed (at a later time). Return with the prison
2660 prison_free_locked(struct prison *pr)
2664 mtx_assert(&pr->pr_mtx, MA_OWNED);
2665 KASSERT(refcount_load(&pr->pr_ref) > 0,
2666 ("Trying to free dead prison %p (jid=%d).",
2668 lastref = refcount_release(&pr->pr_ref);
2669 mtx_unlock(&pr->pr_mtx);
2672 * Don't remove the prison itself in this context,
2673 * in case there are locks held.
2675 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2680 prison_free(struct prison *pr)
2684 * Locking is only required when releasing the last reference.
2685 * This allows assurance that a locked prison will remain valid
2686 * until it is unlocked.
2688 KASSERT(refcount_load(&pr->pr_ref) > 0,
2689 ("Trying to free dead prison %p (jid=%d).",
2691 if (refcount_release_if_not_last(&pr->pr_ref))
2693 mtx_lock(&pr->pr_mtx);
2694 prison_free_locked(pr);
2698 * Hold a a prison for user visibility, by incrementing pr_uref.
2699 * It is generally an error to hold a prison that isn't already
2700 * user-visible, except through the the jail system calls. It is also
2701 * an error to hold an invalid prison. A prison record will remain
2702 * alive as long as it has at least one user reference, and will not
2703 * be set to the dying state was long as the prison mutex is held.
2706 prison_proc_hold(struct prison *pr)
2709 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2712 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2714 refcount_acquire(&pr->pr_uref);
2719 * Remove a prison user reference. If it was the last reference, the
2720 * prison will be considered "dying", and may be removed once all of
2721 * its references are dropped.
2724 prison_proc_free(struct prison *pr)
2728 * Locking is only required when releasing the last reference.
2729 * This allows assurance that a locked prison will remain alive
2730 * until it is unlocked.
2732 KASSERT(refcount_load(&pr->pr_uref) > 0,
2733 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2734 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2736 * Don't remove the last user reference in this context,
2737 * which is expected to be a process that is not only locked,
2738 * but also half dead. Add a reference so any calls to
2739 * prison_free() won't re-submit the task.
2741 refcount_acquire(&pr->pr_ref);
2742 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2747 * Complete a call to either prison_free or prison_proc_free.
2750 prison_complete(void *context, int pending)
2752 struct prison *pr = context;
2754 sx_xlock(&allprison_lock);
2755 mtx_lock(&pr->pr_mtx);
2757 * If this is completing a call to prison_proc_free, there will still
2758 * be a user reference held; clear that as well as the reference that
2759 * was added. No references are expected if this is completing a call
2760 * to prison_free, but prison_deref is still called for the cleanup.
2762 prison_deref(pr, refcount_load(&pr->pr_uref) > 0
2763 ? PD_DEREF | PD_DEUREF | PD_LOCKED | PD_LIST_XLOCKED
2764 : PD_LOCKED | PD_LIST_XLOCKED);
2768 * Remove a prison reference and/or user reference (usually).
2769 * This assumes context that allows sleeping (for allprison_lock),
2770 * with no non-sleeping locks held, except perhaps the prison itself.
2771 * If there are no more references, release and delist the prison.
2772 * On completion, the prison lock and the allprison lock are both
2776 prison_deref(struct prison *pr, int flags)
2778 struct prison *ppr, *tpr;
2779 int lastref, lasturef;
2781 if (!(flags & PD_LOCKED))
2782 mtx_lock(&pr->pr_mtx);
2784 if (flags & PD_DEUREF) {
2785 KASSERT(refcount_load(&pr->pr_uref) > 0,
2786 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2788 lasturef = refcount_release(&pr->pr_uref);
2790 refcount_acquire(&pr->pr_ref);
2791 KASSERT(refcount_load(&prison0.pr_uref) > 0,
2792 ("prison0 pr_uref=0"));
2795 if (flags & PD_DEREF) {
2796 KASSERT(refcount_load(&pr->pr_ref) > 0,
2797 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2799 lastref = refcount_release(&pr->pr_ref);
2802 lastref = refcount_load(&pr->pr_ref) == 0;
2803 mtx_unlock(&pr->pr_mtx);
2806 * Tell the modules if the last user reference was removed
2807 * (even it sticks around in dying state).
2810 if (!(flags & (PD_LIST_SLOCKED | PD_LIST_XLOCKED))) {
2811 if (atomic_load_acq_int(&pr->pr_ref) > 1) {
2812 sx_slock(&allprison_lock);
2813 flags |= PD_LIST_SLOCKED;
2815 sx_xlock(&allprison_lock);
2816 flags |= PD_LIST_XLOCKED;
2819 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2820 mtx_lock(&pr->pr_mtx);
2821 lastref = refcount_release(&pr->pr_ref);
2822 mtx_unlock(&pr->pr_mtx);
2825 /* If the prison still has references, nothing else to do. */
2827 if (flags & PD_LIST_SLOCKED)
2828 sx_sunlock(&allprison_lock);
2829 else if (flags & PD_LIST_XLOCKED)
2830 sx_xunlock(&allprison_lock);
2834 if (flags & PD_LIST_SLOCKED) {
2835 if (!sx_try_upgrade(&allprison_lock)) {
2836 sx_sunlock(&allprison_lock);
2837 sx_xlock(&allprison_lock);
2839 } else if (!(flags & PD_LIST_XLOCKED))
2840 sx_xlock(&allprison_lock);
2842 TAILQ_REMOVE(&allprison, pr, pr_list);
2843 LIST_REMOVE(pr, pr_sibling);
2844 ppr = pr->pr_parent;
2845 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2846 tpr->pr_childcount--;
2847 sx_xunlock(&allprison_lock);
2850 if (pr->pr_vnet != ppr->pr_vnet)
2851 vnet_destroy(pr->pr_vnet);
2853 if (pr->pr_root != NULL)
2855 mtx_destroy(&pr->pr_mtx);
2857 free(pr->pr_ip4, M_PRISON);
2860 free(pr->pr_ip6, M_PRISON);
2862 if (pr->pr_cpuset != NULL)
2863 cpuset_rel(pr->pr_cpuset);
2867 prison_racct_detach(pr);
2871 /* Removing a prison frees a reference on its parent. */
2873 mtx_lock(&pr->pr_mtx);
2874 flags = PD_DEREF | PD_DEUREF;
2879 * Set or clear a permission bit in the pr_allow field, passing restrictions
2880 * (cleared permission) down to child jails.
2883 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
2887 pr = cred->cr_prison;
2888 sx_slock(&allprison_lock);
2889 mtx_lock(&pr->pr_mtx);
2890 prison_set_allow_locked(pr, flag, enable);
2891 mtx_unlock(&pr->pr_mtx);
2892 sx_sunlock(&allprison_lock);
2896 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
2902 pr->pr_allow |= flag;
2904 pr->pr_allow &= ~flag;
2905 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
2906 cpr->pr_allow &= ~flag;
2911 * Check if a jail supports the given address family.
2913 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
2917 prison_check_af(struct ucred *cred, int af)
2922 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2924 pr = cred->cr_prison;
2926 /* Prisons with their own network stack are not limited. */
2927 if (prison_owns_vnet(cred))
2936 if (pr->pr_flags & PR_IP4)
2938 mtx_lock(&pr->pr_mtx);
2939 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
2940 error = EAFNOSUPPORT;
2941 mtx_unlock(&pr->pr_mtx);
2947 if (pr->pr_flags & PR_IP6)
2949 mtx_lock(&pr->pr_mtx);
2950 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
2951 error = EAFNOSUPPORT;
2952 mtx_unlock(&pr->pr_mtx);
2960 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
2961 error = EAFNOSUPPORT;
2967 * Check if given address belongs to the jail referenced by cred (wrapper to
2968 * prison_check_ip[46]).
2970 * Returns 0 if jail doesn't restrict the address family or if address belongs
2971 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
2972 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
2975 prison_if(struct ucred *cred, const struct sockaddr *sa)
2978 const struct sockaddr_in *sai;
2981 const struct sockaddr_in6 *sai6;
2985 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2986 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
2989 if (prison_owns_vnet(cred))
2994 switch (sa->sa_family)
2998 sai = (const struct sockaddr_in *)sa;
2999 error = prison_check_ip4(cred, &sai->sin_addr);
3004 sai6 = (const struct sockaddr_in6 *)sa;
3005 error = prison_check_ip6(cred, &sai6->sin6_addr);
3009 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3010 error = EAFNOSUPPORT;
3016 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3019 prison_check(struct ucred *cred1, struct ucred *cred2)
3022 return ((cred1->cr_prison == cred2->cr_prison ||
3023 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3027 * Return 1 if p2 is a child of p1, otherwise 0.
3030 prison_ischild(struct prison *pr1, struct prison *pr2)
3033 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3040 * Return true if the prison is currently alive. A prison is alive if it is
3041 * valid and it holds user references.
3044 prison_isalive(struct prison *pr)
3047 mtx_assert(&pr->pr_mtx, MA_OWNED);
3048 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3050 if (__predict_false(refcount_load(&pr->pr_uref) == 0))
3056 * Return true if the prison is currently valid. A prison is valid if it has
3057 * been fully created, and is not being destroyed. Note that dying prisons
3058 * are still considered valid.
3061 prison_isvalid(struct prison *pr)
3064 mtx_assert(&pr->pr_mtx, MA_OWNED);
3065 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3071 * Return 1 if the passed credential is in a jail and that jail does not
3072 * have its own virtual network stack, otherwise 0.
3075 jailed_without_vnet(struct ucred *cred)
3081 if (prison_owns_vnet(cred))
3089 * Return the correct hostname (domainname, et al) for the passed credential.
3092 getcredhostname(struct ucred *cred, char *buf, size_t size)
3097 * A NULL credential can be used to shortcut to the physical
3098 * system's hostname.
3100 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3101 mtx_lock(&pr->pr_mtx);
3102 strlcpy(buf, pr->pr_hostname, size);
3103 mtx_unlock(&pr->pr_mtx);
3107 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3110 mtx_lock(&cred->cr_prison->pr_mtx);
3111 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3112 mtx_unlock(&cred->cr_prison->pr_mtx);
3116 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3119 mtx_lock(&cred->cr_prison->pr_mtx);
3120 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3121 mtx_unlock(&cred->cr_prison->pr_mtx);
3125 getcredhostid(struct ucred *cred, unsigned long *hostid)
3128 mtx_lock(&cred->cr_prison->pr_mtx);
3129 *hostid = cred->cr_prison->pr_hostid;
3130 mtx_unlock(&cred->cr_prison->pr_mtx);
3134 getjailname(struct ucred *cred, char *name, size_t len)
3137 mtx_lock(&cred->cr_prison->pr_mtx);
3138 strlcpy(name, cred->cr_prison->pr_name, len);
3139 mtx_unlock(&cred->cr_prison->pr_mtx);
3144 * Determine whether the prison represented by cred owns
3145 * its vnet rather than having it inherited.
3147 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3150 prison_owns_vnet(struct ucred *cred)
3154 * vnets cannot be added/removed after jail creation,
3155 * so no need to lock here.
3157 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3162 * Determine whether the subject represented by cred can "see"
3163 * status of a mount point.
3164 * Returns: 0 for permitted, ENOENT otherwise.
3165 * XXX: This function should be called cr_canseemount() and should be
3166 * placed in kern_prot.c.
3169 prison_canseemount(struct ucred *cred, struct mount *mp)
3175 pr = cred->cr_prison;
3176 if (pr->pr_enforce_statfs == 0)
3178 if (pr->pr_root->v_mount == mp)
3180 if (pr->pr_enforce_statfs == 2)
3183 * If jail's chroot directory is set to "/" we should be able to see
3184 * all mount-points from inside a jail.
3185 * This is ugly check, but this is the only situation when jail's
3186 * directory ends with '/'.
3188 if (strcmp(pr->pr_path, "/") == 0)
3190 len = strlen(pr->pr_path);
3192 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3195 * Be sure that we don't have situation where jail's root directory
3196 * is "/some/path" and mount point is "/some/pathpath".
3198 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3204 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3206 char jpath[MAXPATHLEN];
3210 pr = cred->cr_prison;
3211 if (pr->pr_enforce_statfs == 0)
3213 if (prison_canseemount(cred, mp) != 0) {
3214 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3215 strlcpy(sp->f_mntonname, "[restricted]",
3216 sizeof(sp->f_mntonname));
3219 if (pr->pr_root->v_mount == mp) {
3221 * Clear current buffer data, so we are sure nothing from
3222 * the valid path left there.
3224 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3225 *sp->f_mntonname = '/';
3229 * If jail's chroot directory is set to "/" we should be able to see
3230 * all mount-points from inside a jail.
3232 if (strcmp(pr->pr_path, "/") == 0)
3234 len = strlen(pr->pr_path);
3235 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3237 * Clear current buffer data, so we are sure nothing from
3238 * the valid path left there.
3240 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3241 if (*jpath == '\0') {
3242 /* Should never happen. */
3243 *sp->f_mntonname = '/';
3245 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3250 * Check with permission for a specific privilege is granted within jail. We
3251 * have a specific list of accepted privileges; the rest are denied.
3254 prison_priv_check(struct ucred *cred, int priv)
3260 * Some policies have custom handlers. This routine should not be
3261 * called for them. See priv_check_cred().
3264 case PRIV_VFS_LOOKUP:
3265 case PRIV_VFS_GENERATION:
3266 KASSERT(0, ("prison_priv_check instead of a custom handler "
3267 "called for %d\n", priv));
3275 * Privileges specific to prisons with a virtual network stack.
3276 * There might be a duplicate entry here in case the privilege
3277 * is only granted conditionally in the legacy jail case.
3282 * NFS-specific privileges.
3284 case PRIV_NFS_DAEMON:
3285 case PRIV_NFS_LOCKD:
3288 * Network stack privileges.
3290 case PRIV_NET_BRIDGE:
3293 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3294 case PRIV_NET_ROUTE:
3296 case PRIV_NET_SETIFMTU:
3297 case PRIV_NET_SETIFFLAGS:
3298 case PRIV_NET_SETIFCAP:
3299 case PRIV_NET_SETIFDESCR:
3300 case PRIV_NET_SETIFNAME :
3301 case PRIV_NET_SETIFMETRIC:
3302 case PRIV_NET_SETIFPHYS:
3303 case PRIV_NET_SETIFMAC:
3304 case PRIV_NET_SETLANPCP:
3305 case PRIV_NET_ADDMULTI:
3306 case PRIV_NET_DELMULTI:
3307 case PRIV_NET_HWIOCTL:
3308 case PRIV_NET_SETLLADDR:
3309 case PRIV_NET_ADDIFGROUP:
3310 case PRIV_NET_DELIFGROUP:
3311 case PRIV_NET_IFCREATE:
3312 case PRIV_NET_IFDESTROY:
3313 case PRIV_NET_ADDIFADDR:
3314 case PRIV_NET_DELIFADDR:
3317 case PRIV_NET_SETIFVNET:
3318 case PRIV_NET_SETIFFIB:
3321 * 802.11-related privileges.
3323 case PRIV_NET80211_VAP_GETKEY:
3324 case PRIV_NET80211_VAP_MANAGE:
3330 case PRIV_NETATM_CFG:
3331 case PRIV_NETATM_ADD:
3332 case PRIV_NETATM_DEL:
3333 case PRIV_NETATM_SET:
3336 * Bluetooth privileges.
3338 case PRIV_NETBLUETOOTH_RAW:
3342 * Netgraph and netgraph module privileges.
3344 case PRIV_NETGRAPH_CONTROL:
3346 case PRIV_NETGRAPH_TTY:
3350 * IPv4 and IPv6 privileges.
3352 case PRIV_NETINET_IPFW:
3353 case PRIV_NETINET_DIVERT:
3354 case PRIV_NETINET_PF:
3355 case PRIV_NETINET_DUMMYNET:
3356 case PRIV_NETINET_CARP:
3357 case PRIV_NETINET_MROUTE:
3358 case PRIV_NETINET_RAW:
3359 case PRIV_NETINET_ADDRCTRL6:
3360 case PRIV_NETINET_ND6:
3361 case PRIV_NETINET_SCOPE6:
3362 case PRIV_NETINET_ALIFETIME6:
3363 case PRIV_NETINET_IPSEC:
3364 case PRIV_NETINET_BINDANY:
3379 * No default: or deny here.
3380 * In case of no permit fall through to next switch().
3382 if (cred->cr_prison->pr_flags & PR_VNET)
3389 * Allow ktrace privileges for root in jail.
3395 * Allow jailed processes to configure audit identity and
3396 * submit audit records (login, etc). In the future we may
3397 * want to further refine the relationship between audit and
3400 case PRIV_AUDIT_GETAUDIT:
3401 case PRIV_AUDIT_SETAUDIT:
3402 case PRIV_AUDIT_SUBMIT:
3406 * Allow jailed processes to manipulate process UNIX
3407 * credentials in any way they see fit.
3409 case PRIV_CRED_SETUID:
3410 case PRIV_CRED_SETEUID:
3411 case PRIV_CRED_SETGID:
3412 case PRIV_CRED_SETEGID:
3413 case PRIV_CRED_SETGROUPS:
3414 case PRIV_CRED_SETREUID:
3415 case PRIV_CRED_SETREGID:
3416 case PRIV_CRED_SETRESUID:
3417 case PRIV_CRED_SETRESGID:
3420 * Jail implements visibility constraints already, so allow
3421 * jailed root to override uid/gid-based constraints.
3423 case PRIV_SEEOTHERGIDS:
3424 case PRIV_SEEOTHERUIDS:
3427 * Jail implements inter-process debugging limits already, so
3428 * allow jailed root various debugging privileges.
3430 case PRIV_DEBUG_DIFFCRED:
3431 case PRIV_DEBUG_SUGID:
3432 case PRIV_DEBUG_UNPRIV:
3435 * Allow jail to set various resource limits and login
3436 * properties, and for now, exceed process resource limits.
3438 case PRIV_PROC_LIMIT:
3439 case PRIV_PROC_SETLOGIN:
3440 case PRIV_PROC_SETRLIMIT:
3443 * System V and POSIX IPC privileges are granted in jail.
3446 case PRIV_IPC_WRITE:
3447 case PRIV_IPC_ADMIN:
3448 case PRIV_IPC_MSGSIZE:
3452 * Jail operations within a jail work on child jails.
3454 case PRIV_JAIL_ATTACH:
3456 case PRIV_JAIL_REMOVE:
3459 * Jail implements its own inter-process limits, so allow
3460 * root processes in jail to change scheduling on other
3461 * processes in the same jail. Likewise for signalling.
3463 case PRIV_SCHED_DIFFCRED:
3464 case PRIV_SCHED_CPUSET:
3465 case PRIV_SIGNAL_DIFFCRED:
3466 case PRIV_SIGNAL_SUGID:
3469 * Allow jailed processes to write to sysctls marked as jail
3472 case PRIV_SYSCTL_WRITEJAIL:
3475 * Allow root in jail to manage a variety of quota
3476 * properties. These should likely be conditional on a
3477 * configuration option.
3479 case PRIV_VFS_GETQUOTA:
3480 case PRIV_VFS_SETQUOTA:
3483 * Since Jail relies on chroot() to implement file system
3484 * protections, grant many VFS privileges to root in jail.
3485 * Be careful to exclude mount-related and NFS-related
3489 case PRIV_VFS_WRITE:
3490 case PRIV_VFS_ADMIN:
3492 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3493 case PRIV_VFS_CHFLAGS_DEV:
3494 case PRIV_VFS_CHOWN:
3495 case PRIV_VFS_CHROOT:
3496 case PRIV_VFS_RETAINSUGID:
3497 case PRIV_VFS_FCHROOT:
3499 case PRIV_VFS_SETGID:
3501 case PRIV_VFS_STICKYFILE:
3504 * As in the non-jail case, non-root users are expected to be
3505 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3506 * exists in the jail and they have permission to access it).
3508 case PRIV_KMEM_READ:
3512 * Depending on the global setting, allow privilege of
3513 * setting system flags.
3515 case PRIV_VFS_SYSFLAGS:
3516 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3522 * Depending on the global setting, allow privilege of
3523 * mounting/unmounting file systems.
3525 case PRIV_VFS_MOUNT:
3526 case PRIV_VFS_UNMOUNT:
3527 case PRIV_VFS_MOUNT_NONUSER:
3528 case PRIV_VFS_MOUNT_OWNER:
3529 pr = cred->cr_prison;
3531 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3539 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3540 * policy. priv_check_cred will not specifically allow it, and
3541 * we may want a MAC policy to allow it.
3543 case PRIV_VFS_READ_DIR:
3547 * Conditionnaly allow locking (unlocking) physical pages
3551 case PRIV_VM_MUNLOCK:
3552 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3558 * Conditionally allow jailed root to bind reserved ports.
3560 case PRIV_NETINET_RESERVEDPORT:
3561 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3567 * Allow jailed root to reuse in-use ports.
3569 case PRIV_NETINET_REUSEPORT:
3573 * Allow jailed root to set certain IPv4/6 (option) headers.
3575 case PRIV_NETINET_SETHDROPTS:
3579 * Conditionally allow creating raw sockets in jail.
3581 case PRIV_NETINET_RAW:
3582 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3588 * Since jail implements its own visibility limits on netstat
3589 * sysctls, allow getcred. This allows identd to work in
3592 case PRIV_NETINET_GETCRED:
3596 * Allow jailed root to set loginclass.
3598 case PRIV_PROC_SETLOGINCLASS:
3602 * Do not allow a process inside a jail to read the kernel
3603 * message buffer unless explicitly permitted.
3606 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3612 * In all remaining cases, deny the privilege request. This
3613 * includes almost all network privileges, many system
3614 * configuration privileges.
3621 * Return the part of pr2's name that is relative to pr1, or the whole name
3622 * if it does not directly follow.
3626 prison_name(struct prison *pr1, struct prison *pr2)
3630 /* Jails see themselves as "0" (if they see themselves at all). */
3633 name = pr2->pr_name;
3634 if (prison_ischild(pr1, pr2)) {
3636 * pr1 isn't locked (and allprison_lock may not be either)
3637 * so its length can't be counted on. But the number of dots
3638 * can be counted on - and counted.
3640 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3641 name = strchr(name, '.') + 1;
3647 * Return the part of pr2's path that is relative to pr1, or the whole path
3648 * if it does not directly follow.
3651 prison_path(struct prison *pr1, struct prison *pr2)
3653 char *path1, *path2;
3656 path1 = pr1->pr_path;
3657 path2 = pr2->pr_path;
3658 if (!strcmp(path1, "/"))
3660 len1 = strlen(path1);
3661 if (strncmp(path1, path2, len1))
3663 if (path2[len1] == '\0')
3665 if (path2[len1] == '/')
3666 return (path2 + len1);
3671 * Jail-related sysctls.
3673 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3677 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3680 struct prison *pr, *cpr;
3682 struct in_addr *ip4 = NULL;
3686 struct in6_addr *ip6 = NULL;
3691 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3692 pr = req->td->td_ucred->cr_prison;
3694 sx_slock(&allprison_lock);
3695 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3696 #if defined(INET) || defined(INET6)
3699 mtx_lock(&cpr->pr_mtx);
3701 if (cpr->pr_ip4s > 0) {
3702 if (ip4s < cpr->pr_ip4s) {
3703 ip4s = cpr->pr_ip4s;
3704 mtx_unlock(&cpr->pr_mtx);
3705 ip4 = realloc(ip4, ip4s *
3706 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3709 bcopy(cpr->pr_ip4, ip4,
3710 cpr->pr_ip4s * sizeof(struct in_addr));
3714 if (cpr->pr_ip6s > 0) {
3715 if (ip6s < cpr->pr_ip6s) {
3716 ip6s = cpr->pr_ip6s;
3717 mtx_unlock(&cpr->pr_mtx);
3718 ip6 = realloc(ip6, ip6s *
3719 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3722 bcopy(cpr->pr_ip6, ip6,
3723 cpr->pr_ip6s * sizeof(struct in6_addr));
3726 if (!prison_isvalid(cpr)) {
3727 mtx_unlock(&cpr->pr_mtx);
3730 bzero(xp, sizeof(*xp));
3731 xp->pr_version = XPRISON_VERSION;
3732 xp->pr_id = cpr->pr_id;
3733 xp->pr_state = prison_isalive(cpr)
3734 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3735 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3736 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3737 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3739 xp->pr_ip4s = cpr->pr_ip4s;
3742 xp->pr_ip6s = cpr->pr_ip6s;
3744 mtx_unlock(&cpr->pr_mtx);
3745 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3749 if (xp->pr_ip4s > 0) {
3750 error = SYSCTL_OUT(req, ip4,
3751 xp->pr_ip4s * sizeof(struct in_addr));
3757 if (xp->pr_ip6s > 0) {
3758 error = SYSCTL_OUT(req, ip6,
3759 xp->pr_ip6s * sizeof(struct in6_addr));
3765 sx_sunlock(&allprison_lock);
3776 SYSCTL_OID(_security_jail, OID_AUTO, list,
3777 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3778 sysctl_jail_list, "S", "List of active jails");
3781 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3785 injail = jailed(req->td->td_ucred);
3786 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3791 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3792 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3793 sysctl_jail_jailed, "I", "Process in jail?");
3796 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3798 int error, havevnet;
3800 struct ucred *cred = req->td->td_ucred;
3802 havevnet = jailed(cred) && prison_owns_vnet(cred);
3806 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3811 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3812 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3813 sysctl_jail_vnet, "I", "Jail owns vnet?");
3815 #if defined(INET) || defined(INET6)
3816 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3817 &jail_max_af_ips, 0,
3818 "Number of IP addresses a jail may have at most per address family (deprecated)");
3822 * Default parameters for jail(2) compatibility. For historical reasons,
3823 * the sysctl names have varying similarity to the parameter names. Prisons
3824 * just see their own parameters, and can't change them.
3827 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3831 /* Get the current flag value, and convert it to a boolean. */
3832 if (req->td->td_ucred->cr_prison == &prison0) {
3833 mtx_lock(&prison0.pr_mtx);
3834 i = (jail_default_allow & arg2) != 0;
3835 mtx_unlock(&prison0.pr_mtx);
3837 i = prison_allow(req->td->td_ucred, arg2);
3841 error = sysctl_handle_int(oidp, &i, 0, req);
3842 if (error || !req->newptr)
3848 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3851 mtx_lock(&prison0.pr_mtx);
3852 jail_default_allow = (jail_default_allow & ~arg2) | i;
3853 mtx_unlock(&prison0.pr_mtx);
3857 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3858 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3859 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3860 "Processes in jail can set their hostnames (deprecated)");
3861 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3862 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3863 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3864 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
3865 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3866 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3867 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3868 "Processes in jail can use System V IPC primitives (deprecated)");
3869 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3870 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3871 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3872 "Prison root can create raw sockets (deprecated)");
3873 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3874 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3875 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3876 "Processes in jail can alter system file flags (deprecated)");
3877 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3878 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3879 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3880 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
3883 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3888 pr = req->td->td_ucred->cr_prison;
3889 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
3890 error = sysctl_handle_int(oidp, &level, 0, req);
3891 if (error || !req->newptr)
3893 *(int *)arg1 = level;
3897 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3898 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3899 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
3900 sysctl_jail_default_level, "I",
3901 "Processes in jail cannot see all mounted file systems (deprecated)");
3903 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
3904 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
3905 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
3906 sysctl_jail_default_level, "I",
3907 "Ruleset for the devfs filesystem in jail (deprecated)");
3910 * Nodes to describe jail parameters. Maximum length of string parameters
3911 * is returned in the string itself, and the other parameters exist merely
3912 * to make themselves and their types known.
3914 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3918 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
3925 switch (oidp->oid_kind & CTLTYPE)
3931 if (!(req->flags & SCTL_MASK32))
3933 return (SYSCTL_OUT(req, &l, sizeof(l)));
3937 return (SYSCTL_OUT(req, &i, sizeof(i)));
3938 case CTLTYPE_STRING:
3939 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
3941 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
3942 case CTLTYPE_STRUCT:
3944 return (SYSCTL_OUT(req, &s, sizeof(s)));
3950 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
3951 * jail creation time but cannot be changed in an existing jail.
3953 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
3954 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
3955 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
3956 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
3957 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
3958 "I", "Jail secure level");
3959 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
3960 "Jail value for kern.osreldate and uname -K");
3961 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
3962 "Jail value for kern.osrelease and uname -r");
3963 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
3964 "I", "Jail cannot see all mounted file systems");
3965 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
3966 "I", "Ruleset for in-jail devfs mounts");
3967 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
3968 "B", "Jail persistence");
3970 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
3971 "E,jailsys", "Virtual network stack");
3973 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
3974 "B", "Jail is in the process of shutting down");
3976 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
3977 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
3978 "I", "Current number of child jails");
3979 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
3980 "I", "Maximum number of child jails");
3982 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
3983 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
3985 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
3986 "Jail NIS domainname");
3987 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
3989 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
3990 "LU", "Jail host ID");
3992 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
3993 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
3996 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
3997 "Jail IPv4 address virtualization");
3998 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
3999 "S,in_addr,a", "Jail IPv4 addresses");
4000 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4001 "B", "Do (not) use IPv4 source address selection rather than the "
4002 "primary jail IPv4 address.");
4005 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4006 "Jail IPv6 address virtualization");
4007 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4008 "S,in6_addr,a", "Jail IPv6 addresses");
4009 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4010 "B", "Do (not) use IPv6 source address selection rather than the "
4011 "primary jail IPv6 address.");
4014 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4015 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4016 "B", "Jail may set hostname");
4017 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4018 "B", "Jail may use SYSV IPC");
4019 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4020 "B", "Jail may create raw sockets");
4021 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4022 "B", "Jail may alter system file flags");
4023 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4024 "B", "Jail may set file quotas");
4025 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4026 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4027 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4028 "B", "Jail may lock (unlock) physical pages in memory");
4029 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4030 "B", "Jail may bind sockets to reserved ports");
4031 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4032 "B", "Jail may read the kernel message buffer");
4033 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4034 "B", "Unprivileged processes may use process debugging facilities");
4035 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4036 "B", "Processes in jail with uid 0 have privilege");
4038 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4039 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4040 "B", "Jail may mount/unmount jail-friendly file systems in general");
4043 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4044 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4048 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4051 struct bool_flags *bf;
4052 struct sysctl_oid *parent;
4053 char *allow_name, *allow_noname, *allowed;
4054 #ifndef NO_SYSCTL_DESCR
4055 char *descr_deprecated;
4060 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4062 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4064 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4065 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4066 free(allow_name, M_PRISON);
4071 * See if this parameter has already beed added, i.e. a module was
4072 * previously loaded/unloaded.
4074 mtx_lock(&prison0.pr_mtx);
4075 for (bf = pr_flag_allow;
4076 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4077 atomic_load_int(&bf->flag) != 0;
4079 if (strcmp(bf->name, allow_name) == 0) {
4080 allow_flag = bf->flag;
4086 * Find a free bit in pr_allow_all, failing if there are none
4087 * (which shouldn't happen as long as we keep track of how many
4088 * potential dynamic flags exist).
4090 for (allow_flag = 1;; allow_flag <<= 1) {
4091 if (allow_flag == 0)
4093 if ((pr_allow_all & allow_flag) == 0)
4097 /* Note the parameter in the next open slot in pr_flag_allow. */
4098 for (bf = pr_flag_allow; ; bf++) {
4099 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4100 /* This should never happen, but is not fatal. */
4104 if (atomic_load_int(&bf->flag) == 0)
4107 bf->name = allow_name;
4108 bf->noname = allow_noname;
4109 pr_allow_all |= allow_flag;
4111 * prison0 always has permission for the new parameter.
4112 * Other jails must have it granted to them.
4114 prison0.pr_allow |= allow_flag;
4115 /* The flag indicates a valid entry, so make sure it is set last. */
4116 atomic_store_rel_int(&bf->flag, allow_flag);
4117 mtx_unlock(&prison0.pr_mtx);
4120 * Create sysctls for the paramter, and the back-compat global
4124 ? SYSCTL_ADD_NODE(NULL,
4125 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4126 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4127 : &sysctl___security_jail_param_allow;
4128 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4129 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4130 NULL, 0, sysctl_jail_param, "B", descr);
4132 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4133 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4134 #ifndef NO_SYSCTL_DESCR
4135 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4138 (void)SYSCTL_ADD_PROC(NULL,
4139 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4140 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4141 sysctl_jail_default_allow, "I", descr_deprecated);
4142 #ifndef NO_SYSCTL_DESCR
4143 free(descr_deprecated, M_TEMP);
4145 free(allowed, M_TEMP);
4150 mtx_unlock(&prison0.pr_mtx);
4151 free(allow_name, M_PRISON);
4152 free(allow_noname, M_PRISON);
4157 * The VFS system will register jail-aware filesystems here. They each get
4158 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4159 * attempts to mount.
4162 prison_add_vfs(struct vfsconf *vfsp)
4164 #ifdef NO_SYSCTL_DESCR
4166 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4171 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4173 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4175 free(descr, M_TEMP);
4181 prison_racct_foreach(void (*callback)(struct racct *racct,
4182 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4183 void *arg2, void *arg3)
4185 struct prison_racct *prr;
4187 ASSERT_RACCT_ENABLED();
4189 sx_slock(&allprison_lock);
4192 LIST_FOREACH(prr, &allprison_racct, prr_next)
4193 (callback)(prr->prr_racct, arg2, arg3);
4196 sx_sunlock(&allprison_lock);
4199 static struct prison_racct *
4200 prison_racct_find_locked(const char *name)
4202 struct prison_racct *prr;
4204 ASSERT_RACCT_ENABLED();
4205 sx_assert(&allprison_lock, SA_XLOCKED);
4207 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4210 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4211 if (strcmp(name, prr->prr_name) != 0)
4214 /* Found prison_racct with a matching name? */
4215 prison_racct_hold(prr);
4219 /* Add new prison_racct. */
4220 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4221 racct_create(&prr->prr_racct);
4223 strcpy(prr->prr_name, name);
4224 refcount_init(&prr->prr_refcount, 1);
4225 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4230 struct prison_racct *
4231 prison_racct_find(const char *name)
4233 struct prison_racct *prr;
4235 ASSERT_RACCT_ENABLED();
4237 sx_xlock(&allprison_lock);
4238 prr = prison_racct_find_locked(name);
4239 sx_xunlock(&allprison_lock);
4244 prison_racct_hold(struct prison_racct *prr)
4247 ASSERT_RACCT_ENABLED();
4249 refcount_acquire(&prr->prr_refcount);
4253 prison_racct_free_locked(struct prison_racct *prr)
4256 ASSERT_RACCT_ENABLED();
4257 sx_assert(&allprison_lock, SA_XLOCKED);
4259 if (refcount_release(&prr->prr_refcount)) {
4260 racct_destroy(&prr->prr_racct);
4261 LIST_REMOVE(prr, prr_next);
4262 free(prr, M_PRISON_RACCT);
4267 prison_racct_free(struct prison_racct *prr)
4270 ASSERT_RACCT_ENABLED();
4271 sx_assert(&allprison_lock, SA_UNLOCKED);
4273 if (refcount_release_if_not_last(&prr->prr_refcount))
4276 sx_xlock(&allprison_lock);
4277 prison_racct_free_locked(prr);
4278 sx_xunlock(&allprison_lock);
4282 prison_racct_attach(struct prison *pr)
4284 struct prison_racct *prr;
4286 ASSERT_RACCT_ENABLED();
4287 sx_assert(&allprison_lock, SA_XLOCKED);
4289 prr = prison_racct_find_locked(pr->pr_name);
4290 KASSERT(prr != NULL, ("cannot find prison_racct"));
4292 pr->pr_prison_racct = prr;
4296 * Handle jail renaming. From the racct point of view, renaming means
4297 * moving from one prison_racct to another.
4300 prison_racct_modify(struct prison *pr)
4306 struct prison_racct *oldprr;
4308 ASSERT_RACCT_ENABLED();
4310 sx_slock(&allproc_lock);
4311 sx_xlock(&allprison_lock);
4313 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4314 sx_xunlock(&allprison_lock);
4315 sx_sunlock(&allproc_lock);
4319 oldprr = pr->pr_prison_racct;
4320 pr->pr_prison_racct = NULL;
4322 prison_racct_attach(pr);
4325 * Move resource utilisation records.
4327 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4331 * Force rctl to reattach rules to processes.
4333 FOREACH_PROC_IN_SYSTEM(p) {
4335 cred = crhold(p->p_ucred);
4337 rctl_proc_ucred_changed(p, cred);
4342 sx_sunlock(&allproc_lock);
4343 prison_racct_free_locked(oldprr);
4344 sx_xunlock(&allprison_lock);
4348 prison_racct_detach(struct prison *pr)
4351 ASSERT_RACCT_ENABLED();
4352 sx_assert(&allprison_lock, SA_UNLOCKED);
4354 if (pr->pr_prison_racct == NULL)
4356 prison_racct_free(pr->pr_prison_racct);
4357 pr->pr_prison_racct = NULL;
4364 db_show_prison(struct prison *pr)
4366 struct bool_flags *bf;
4367 struct jailsys_flags *jsf;
4368 #if defined(INET) || defined(INET6)
4373 char ip4buf[INET_ADDRSTRLEN];
4376 char ip6buf[INET6_ADDRSTRLEN];
4379 db_printf("prison %p:\n", pr);
4380 db_printf(" jid = %d\n", pr->pr_id);
4381 db_printf(" name = %s\n", pr->pr_name);
4382 db_printf(" parent = %p\n", pr->pr_parent);
4383 db_printf(" ref = %d\n", pr->pr_ref);
4384 db_printf(" uref = %d\n", pr->pr_uref);
4385 db_printf(" path = %s\n", pr->pr_path);
4386 db_printf(" cpuset = %d\n", pr->pr_cpuset
4387 ? pr->pr_cpuset->cs_id : -1);
4389 db_printf(" vnet = %p\n", pr->pr_vnet);
4391 db_printf(" root = %p\n", pr->pr_root);
4392 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4393 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4394 db_printf(" children.max = %d\n", pr->pr_childmax);
4395 db_printf(" children.cur = %d\n", pr->pr_childcount);
4396 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4397 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4398 db_printf(" flags = 0x%x", pr->pr_flags);
4399 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4400 if (pr->pr_flags & bf->flag)
4401 db_printf(" %s", bf->name);
4402 for (jsf = pr_flag_jailsys;
4403 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4405 f = pr->pr_flags & (jsf->disable | jsf->new);
4406 db_printf(" %-16s= %s\n", jsf->name,
4407 (f != 0 && f == jsf->disable) ? "disable"
4408 : (f == jsf->new) ? "new"
4411 db_printf(" allow = 0x%x", pr->pr_allow);
4412 for (bf = pr_flag_allow;
4413 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4414 atomic_load_int(&bf->flag) != 0;
4416 if (pr->pr_allow & bf->flag)
4417 db_printf(" %s", bf->name);
4419 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4420 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4421 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4422 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4423 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4425 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4426 for (ii = 0; ii < pr->pr_ip4s; ii++)
4427 db_printf(" %s %s\n",
4428 ii == 0 ? "ip4.addr =" : " ",
4429 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4432 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4433 for (ii = 0; ii < pr->pr_ip6s; ii++)
4434 db_printf(" %s %s\n",
4435 ii == 0 ? "ip6.addr =" : " ",
4436 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4440 DB_SHOW_COMMAND(prison, db_show_prison_command)
4446 * Show all prisons in the list, and prison0 which is not
4449 db_show_prison(&prison0);
4450 if (!db_pager_quit) {
4451 TAILQ_FOREACH(pr, &allprison, pr_list) {
4463 /* Look for a prison with the ID and with references. */
4464 TAILQ_FOREACH(pr, &allprison, pr_list)
4465 if (pr->pr_id == addr && pr->pr_ref > 0)
4468 /* Look again, without requiring a reference. */
4469 TAILQ_FOREACH(pr, &allprison, pr_list)
4470 if (pr->pr_id == addr)
4473 /* Assume address points to a valid prison. */
4474 pr = (struct prison *)addr;