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 PRISON0_HOSTUUID_MODULE "hostuuid"
81 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
82 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
84 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
87 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
89 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
93 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
95 #define _PR_IP_SADDRSEL 0
99 /* prison0 describes what is "real" about the system. */
100 struct prison prison0 = {
106 .pr_securelevel = -1,
108 .pr_state = PRISON_STATE_ALIVE,
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, int drflags);
141 static void prison_complete(void *context, int pending);
142 static void prison_deref(struct prison *pr, int flags);
143 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
144 static int prison_lock_xlock(struct prison *pr, int flags);
145 static void prison_free_not_last(struct prison *pr);
146 static void prison_proc_free_not_last(struct prison *pr);
147 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
149 static char *prison_path(struct prison *pr1, struct prison *pr2);
151 static void prison_racct_attach(struct prison *pr);
152 static void prison_racct_modify(struct prison *pr);
153 static void prison_racct_detach(struct prison *pr);
156 /* Flags for prison_deref */
157 #define PD_DEREF 0x01 /* Decrement pr_ref */
158 #define PD_DEUREF 0x02 /* Decrement pr_uref */
159 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
160 #define PD_LOCKED 0x10 /* pr_mtx is held */
161 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
162 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
163 #define PD_OP_FLAGS 0x07 /* Operation flags */
164 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
167 * Parameter names corresponding to PR_* flag values. Size values are for kvm
168 * as we cannot figure out the size of a sparse array, or an array without a
171 static struct bool_flags pr_flag_bool[] = {
172 {"persist", "nopersist", PR_PERSIST},
174 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
177 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
180 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
182 static struct jailsys_flags pr_flag_jailsys[] = {
183 {"host", 0, PR_HOST},
185 {"vnet", 0, PR_VNET},
188 {"ip4", PR_IP4_USER, PR_IP4_USER},
191 {"ip6", PR_IP6_USER, PR_IP6_USER},
194 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
197 * Make this array full-size so dynamic parameters can be added.
198 * It is protected by prison0.mtx, but lockless reading is allowed
199 * with an atomic check of the flag values.
201 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
202 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
203 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
204 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
205 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
206 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
207 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
208 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
209 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
210 {"allow.reserved_ports", "allow.noreserved_ports",
211 PR_ALLOW_RESERVED_PORTS},
212 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
213 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
214 PR_ALLOW_UNPRIV_DEBUG},
215 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
217 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
218 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
220 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
221 PR_ALLOW_RESERVED_PORTS | \
222 PR_ALLOW_UNPRIV_DEBUG | \
224 #define JAIL_DEFAULT_ENFORCE_STATFS 2
225 #define JAIL_DEFAULT_DEVFS_RSNUM 0
226 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
227 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
228 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
229 #if defined(INET) || defined(INET6)
230 static unsigned jail_max_af_ips = 255;
234 * Initialize the parts of prison0 that can't be static-initialized with
235 * constants. This is called from proc0_init() after creating thread0 cpuset.
240 uint8_t *file, *data;
242 char buf[sizeof(prison0.pr_hostuuid)];
245 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
246 prison0.pr_osreldate = osreldate;
247 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
249 /* If we have a preloaded hostuuid, use it. */
250 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
252 data = preload_fetch_addr(file);
253 size = preload_fetch_size(file);
256 * The preloaded data may include trailing whitespace, almost
257 * certainly a newline; skip over any whitespace or
258 * non-printable characters to be safe.
260 while (size > 0 && data[size - 1] <= 0x20) {
267 * Not NUL-terminated when passed from loader, but
268 * validate_uuid requires that due to using sscanf (as
269 * does the subsequent strlcpy, since it still reads
270 * past the given size to return the true length);
271 * bounce to a temporary buffer to fix.
273 if (size >= sizeof(buf))
276 memcpy(buf, data, size);
279 if (validate_uuid(buf, size, NULL, 0) != 0)
283 (void)strlcpy(prison0.pr_hostuuid, buf,
284 sizeof(prison0.pr_hostuuid));
287 if (bootverbose && !valid) {
288 printf("hostuuid: preload data malformed: '%.*s'\n",
294 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
303 sys_jail(struct thread *td, struct jail_args *uap)
309 error = copyin(uap->jail, &version, sizeof(uint32_t));
318 /* FreeBSD single IPv4 jails. */
319 bzero(&j, sizeof(struct jail));
320 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
323 j.version = j0.version;
325 j.hostname = j0.hostname;
326 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
332 * Version 1 was used by multi-IPv4 jail implementations
333 * that never made it into the official kernel.
337 case 2: /* JAIL_API_VERSION */
338 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
339 error = copyin(uap->jail, &j, sizeof(struct jail));
345 /* Sci-Fi jails are not supported, sorry. */
348 return (kern_jail(td, &j));
352 kern_jail(struct thread *td, struct jail *j)
354 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
363 char *u_path, *u_hostname, *u_name;
364 struct bool_flags *bf;
367 struct in_addr *u_ip4;
370 struct in6_addr *u_ip6;
373 int error, enforce_statfs;
375 bzero(&optiov, sizeof(optiov));
376 opt.uio_iov = optiov;
380 opt.uio_segflg = UIO_SYSSPACE;
381 opt.uio_rw = UIO_READ;
384 /* Set permissions for top-level jails from sysctls. */
385 if (!jailed(td->td_ucred)) {
386 for (bf = pr_flag_allow;
387 bf < pr_flag_allow + nitems(pr_flag_allow) &&
388 atomic_load_int(&bf->flag) != 0;
390 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
391 (jail_default_allow & bf->flag)
392 ? bf->name : bf->noname);
393 optiov[opt.uio_iovcnt].iov_len =
394 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
397 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
398 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
400 enforce_statfs = jail_default_enforce_statfs;
401 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
402 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
406 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
408 ip4s = (j->version == 0) ? 1 : j->ip4s;
409 if (ip4s > jail_max_af_ips)
411 tmplen += ip4s * sizeof(struct in_addr);
417 if (j->ip6s > jail_max_af_ips)
419 tmplen += j->ip6s * sizeof(struct in6_addr);
424 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
425 u_hostname = u_path + MAXPATHLEN;
426 u_name = u_hostname + MAXHOSTNAMELEN;
428 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
432 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
434 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
437 optiov[opt.uio_iovcnt].iov_base = "path";
438 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
440 optiov[opt.uio_iovcnt].iov_base = u_path;
441 error = copyinstr(j->path, u_path, MAXPATHLEN,
442 &optiov[opt.uio_iovcnt].iov_len);
444 free(u_path, M_TEMP);
448 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
449 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
451 optiov[opt.uio_iovcnt].iov_base = u_hostname;
452 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
453 &optiov[opt.uio_iovcnt].iov_len);
455 free(u_path, M_TEMP);
459 if (j->jailname != NULL) {
460 optiov[opt.uio_iovcnt].iov_base = "name";
461 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
463 optiov[opt.uio_iovcnt].iov_base = u_name;
464 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
465 &optiov[opt.uio_iovcnt].iov_len);
467 free(u_path, M_TEMP);
473 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
474 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
476 optiov[opt.uio_iovcnt].iov_base = u_ip4;
477 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
479 u_ip4->s_addr = j->ip4s;
481 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
483 free(u_path, M_TEMP);
490 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
491 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
493 optiov[opt.uio_iovcnt].iov_base = u_ip6;
494 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
495 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
497 free(u_path, M_TEMP);
502 KASSERT(opt.uio_iovcnt <= nitems(optiov),
503 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
504 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
505 free(u_path, M_TEMP);
510 * struct jail_set_args {
511 * struct iovec *iovp;
512 * unsigned int iovcnt;
517 sys_jail_set(struct thread *td, struct jail_set_args *uap)
522 /* Check that we have an even number of iovecs. */
526 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
529 error = kern_jail_set(td, auio, uap->flags);
535 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
542 struct in6_addr *ip6;
545 struct vfsoptlist *opts;
546 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
548 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
549 char *g_path, *osrelstr;
550 struct bool_flags *bf;
551 struct jailsys_flags *jsf;
552 #if defined(INET) || defined(INET6)
557 size_t namelen, onamelen, pnamelen;
558 int born, created, cuflags, descend, drflags, enforce;
559 int error, errmsg_len, errmsg_pos;
560 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
561 int jid, jsys, len, level;
562 int childmax, osreldt, rsnum, slevel;
563 #if defined(INET) || defined(INET6)
572 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
573 uint64_t pr_allow_diff;
577 error = priv_check(td, PRIV_JAIL_SET);
578 if (!error && (flags & JAIL_ATTACH))
579 error = priv_check(td, PRIV_JAIL_ATTACH);
582 mypr = td->td_ucred->cr_prison;
583 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
585 if (flags & ~JAIL_SET_MASK)
589 * Check all the parameters before committing to anything. Not all
590 * errors can be caught early, but we may as well try. Also, this
591 * takes care of some expensive stuff (path lookup) before getting
592 * the allprison lock.
594 * XXX Jails are not filesystems, and jail parameters are not mount
595 * options. But it makes more sense to re-use the vfsopt code
596 * than duplicate it under a different name.
598 error = vfs_buildopts(optuio, &opts);
609 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
612 vfs_opterror(opts, "no valid operation (create or update)");
616 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
622 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
631 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
639 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
644 else if (enforce < 0 || enforce > 2) {
650 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
658 pr_flags = ch_flags = 0;
659 for (bf = pr_flag_bool;
660 bf < pr_flag_bool + nitems(pr_flag_bool);
662 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
663 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
665 ch_flags |= pr_flags;
666 for (jsf = pr_flag_jailsys;
667 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
669 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
675 case JAIL_SYS_DISABLE:
680 pr_flags |= jsf->disable;
683 pr_flags |= jsf->new;
685 case JAIL_SYS_INHERIT:
691 ch_flags |= jsf->new | jsf->disable;
693 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
694 && !(pr_flags & PR_PERSIST)) {
696 vfs_opterror(opts, "new jail must persist or attach");
700 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
702 vfs_opterror(opts, "vnet cannot be changed after creation");
707 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
709 vfs_opterror(opts, "ip4 cannot be changed after creation");
714 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
716 vfs_opterror(opts, "ip6 cannot be changed after creation");
721 pr_allow = ch_allow = 0;
722 for (bf = pr_flag_allow;
723 bf < pr_flag_allow + nitems(pr_flag_allow) &&
724 atomic_load_int(&bf->flag) != 0;
726 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
727 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
729 ch_allow |= pr_allow;
731 error = vfs_getopt(opts, "name", (void **)&name, &len);
737 if (len == 0 || name[len - 1] != '\0') {
741 if (len > MAXHOSTNAMELEN) {
742 error = ENAMETOOLONG;
747 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
755 if (len == 0 || host[len - 1] != '\0') {
759 if (len > MAXHOSTNAMELEN) {
760 error = ENAMETOOLONG;
765 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
773 if (len == 0 || domain[len - 1] != '\0') {
777 if (len > MAXHOSTNAMELEN) {
778 error = ENAMETOOLONG;
783 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
791 if (len == 0 || uuid[len - 1] != '\0') {
795 if (len > HOSTUUIDLEN) {
796 error = ENAMETOOLONG;
801 #ifdef COMPAT_FREEBSD32
802 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
805 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
809 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
821 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
826 else if (ip4s & (sizeof(*ip4) - 1)) {
830 ch_flags |= PR_IP4_USER;
831 pr_flags |= PR_IP4_USER;
833 ip4s /= sizeof(*ip4);
834 if (ip4s > jail_max_af_ips) {
836 vfs_opterror(opts, "too many IPv4 addresses");
839 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
840 bcopy(op, ip4, ip4s * sizeof(*ip4));
842 * IP addresses are all sorted but ip[0] to preserve
843 * the primary IP address as given from userland.
844 * This special IP is used for unbound outgoing
845 * connections as well for "loopback" traffic in case
846 * source address selection cannot find any more fitting
847 * address to connect from.
850 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
853 * Check for duplicate addresses and do some simple
854 * zero and broadcast checks. If users give other bogus
855 * addresses it is their problem.
857 * We do not have to care about byte order for these
858 * checks so we will do them in NBO.
860 for (ii = 0; ii < ip4s; ii++) {
861 if (ip4[ii].s_addr == INADDR_ANY ||
862 ip4[ii].s_addr == INADDR_BROADCAST) {
867 (ip4[0].s_addr == ip4[ii+1].s_addr ||
868 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
878 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
883 else if (ip6s & (sizeof(*ip6) - 1)) {
887 ch_flags |= PR_IP6_USER;
888 pr_flags |= PR_IP6_USER;
890 ip6s /= sizeof(*ip6);
891 if (ip6s > jail_max_af_ips) {
893 vfs_opterror(opts, "too many IPv6 addresses");
896 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
897 bcopy(op, ip6, ip6s * sizeof(*ip6));
899 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
901 for (ii = 0; ii < ip6s; ii++) {
902 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
907 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
908 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
918 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
919 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
922 "vnet jails cannot have IP address restrictions");
927 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
933 if (flags & JAIL_UPDATE) {
936 "osrelease cannot be changed after creation");
939 if (len == 0 || osrelstr[len - 1] != '\0') {
943 if (len >= OSRELEASELEN) {
944 error = ENAMETOOLONG;
946 "osrelease string must be 1-%d bytes long",
952 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
958 if (flags & JAIL_UPDATE) {
961 "osreldate cannot be changed after creation");
966 vfs_opterror(opts, "osreldate cannot be 0");
972 error = vfs_getopt(opts, "path", (void **)&path, &len);
978 if (flags & JAIL_UPDATE) {
981 "path cannot be changed after creation");
984 if (len == 0 || path[len - 1] != '\0') {
988 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
994 NDFREE(&nd, NDF_ONLY_PNBUF);
995 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
996 strlcpy(g_path, path, MAXPATHLEN);
997 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1001 /* exit on other errors */
1004 if (root->v_type != VDIR) {
1013 * Find the specified jail, or at least its parent.
1014 * This abuses the file error codes ENOENT and EEXIST.
1018 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1019 namelc = strrchr(name, '.');
1020 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1024 sx_xlock(&allprison_lock);
1025 drflags = PD_LIST_XLOCKED;
1027 if (!prison_isalive(ppr)) {
1028 /* This jail is dying. This process will surely follow. */
1035 vfs_opterror(opts, "negative jid");
1039 * See if a requested jid already exists. Keep track of
1040 * where it can be inserted later.
1042 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1043 if (inspr->pr_id < jid)
1045 if (inspr->pr_id > jid)
1048 mtx_lock(&pr->pr_mtx);
1049 drflags |= PD_LOCKED;
1054 /* Create: jid must not exist. */
1055 if (cuflags == JAIL_CREATE) {
1057 * Even creators that cannot see the jail will
1061 vfs_opterror(opts, "jail %d already exists",
1065 if (!prison_ischild(mypr, pr)) {
1067 * Updaters get ENOENT if they cannot see the
1068 * jail. This is true even for CREATE | UPDATE,
1069 * which normally cannot give this error.
1072 vfs_opterror(opts, "jail %d not found", jid);
1075 ppr = pr->pr_parent;
1076 if (!prison_isalive(ppr)) {
1078 vfs_opterror(opts, "jail %d is dying",
1082 if (!prison_isalive(pr)) {
1083 if (!(flags & JAIL_DYING)) {
1085 vfs_opterror(opts, "jail %d is dying",
1089 if ((flags & JAIL_ATTACH) ||
1090 (pr_flags & PR_PERSIST)) {
1092 * A dying jail might be resurrected
1093 * (via attach or persist), but first
1094 * it must determine if another jail
1095 * has claimed its name. Accomplish
1096 * this by implicitly re-setting the
1100 name = prison_name(mypr, pr);
1104 /* Update: jid must exist. */
1105 if (cuflags == JAIL_UPDATE) {
1107 vfs_opterror(opts, "jail %d not found", jid);
1113 * If the caller provided a name, look for a jail by that name.
1114 * This has different semantics for creates and updates keyed by jid
1115 * (where the name must not already exist in a different jail),
1116 * and updates keyed by the name itself (where the name must exist
1117 * because that is the jail being updated).
1121 namelc = strrchr(name, '.');
1126 * This is a hierarchical name. Split it into the
1127 * parent and child names, and make sure the parent
1128 * exists or matches an already found jail.
1131 if (strncmp(name, ppr->pr_name, namelc - name)
1132 || ppr->pr_name[namelc - name] != '\0') {
1135 "cannot change jail's parent");
1140 ppr = prison_find_name(mypr, name);
1144 "jail \"%s\" not found", name);
1147 mtx_unlock(&ppr->pr_mtx);
1148 if (!prison_isalive(ppr)) {
1151 "jail \"%s\" is dying", name);
1158 if (namelc[0] != '\0') {
1160 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1162 FOREACH_PRISON_CHILD(ppr, tpr) {
1164 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1165 if (prison_isalive(tpr)) {
1167 cuflags != JAIL_CREATE) {
1173 mtx_lock(&pr->pr_mtx);
1174 drflags |= PD_LOCKED;
1178 * Create, or update(jid):
1179 * name must not exist in an
1180 * active sibling jail.
1184 "jail \"%s\" already exists",
1189 cuflags != JAIL_CREATE) {
1194 /* If no active jail is found, use a dying one. */
1195 if (deadpr != NULL && pr == NULL) {
1196 if (flags & JAIL_DYING) {
1198 mtx_lock(&pr->pr_mtx);
1199 drflags |= PD_LOCKED;
1200 } else if (cuflags == JAIL_UPDATE) {
1203 "jail \"%s\" is dying", name);
1207 /* Update: name must exist if no jid. */
1208 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1210 vfs_opterror(opts, "jail \"%s\" not found",
1216 /* Update: must provide a jid or name. */
1217 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1219 vfs_opterror(opts, "update specified no jail");
1223 /* If there's no prison to update, create a new one and link it in. */
1224 created = pr == NULL;
1226 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1227 if (tpr->pr_childcount >= tpr->pr_childmax) {
1229 vfs_opterror(opts, "prison limit exceeded");
1232 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1234 vfs_opterror(opts, "no available jail IDs");
1238 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1239 pr->pr_state = PRISON_STATE_INVALID;
1240 refcount_init(&pr->pr_ref, 1);
1241 refcount_init(&pr->pr_uref, 0);
1242 drflags |= PD_DEREF;
1243 LIST_INIT(&pr->pr_children);
1244 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1245 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1249 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1251 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1253 pr->pr_parent = ppr;
1255 prison_proc_hold(ppr);
1256 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1257 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1258 tpr->pr_childcount++;
1260 /* Set some default values, and inherit some from the parent. */
1265 root = mypr->pr_root;
1268 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1269 pr->pr_flags |= PR_HOST;
1270 #if defined(INET) || defined(INET6)
1272 if (!(pr_flags & PR_VNET))
1276 if (!(ch_flags & PR_IP4_USER))
1277 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1278 else if (!(pr_flags & PR_IP4_USER)) {
1279 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1280 if (ppr->pr_ip4 != NULL) {
1281 pr->pr_ip4s = ppr->pr_ip4s;
1282 pr->pr_ip4 = malloc(pr->pr_ip4s *
1283 sizeof(struct in_addr), M_PRISON,
1285 bcopy(ppr->pr_ip4, pr->pr_ip4,
1286 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1291 if (!(ch_flags & PR_IP6_USER))
1292 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1293 else if (!(pr_flags & PR_IP6_USER)) {
1294 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1295 if (ppr->pr_ip6 != NULL) {
1296 pr->pr_ip6s = ppr->pr_ip6s;
1297 pr->pr_ip6 = malloc(pr->pr_ip6s *
1298 sizeof(struct in6_addr), M_PRISON,
1300 bcopy(ppr->pr_ip6, pr->pr_ip6,
1301 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1307 /* Source address selection is always on by default. */
1308 pr->pr_flags |= _PR_IP_SADDRSEL;
1310 pr->pr_securelevel = ppr->pr_securelevel;
1311 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1312 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1313 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1315 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1316 if (osrelstr == NULL)
1317 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1318 sizeof(pr->pr_osrelease));
1320 strlcpy(pr->pr_osrelease, osrelstr,
1321 sizeof(pr->pr_osrelease));
1324 /* Allocate a new vnet if specified. */
1325 pr->pr_vnet = (pr_flags & PR_VNET)
1326 ? vnet_alloc() : ppr->pr_vnet;
1329 * Allocate a dedicated cpuset for each jail.
1330 * Unlike other initial settings, this may return an error.
1332 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1336 mtx_lock(&pr->pr_mtx);
1337 drflags |= PD_LOCKED;
1340 * Grab a reference for existing prisons, to ensure they
1341 * continue to exist for the duration of the call.
1344 drflags |= PD_DEREF;
1345 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1346 if ((pr->pr_flags & PR_VNET) &&
1347 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1350 "vnet jails cannot have IP address restrictions");
1355 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1358 "ip4 cannot be changed after creation");
1363 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1366 "ip6 cannot be changed after creation");
1372 /* Do final error checking before setting anything. */
1374 if (slevel < ppr->pr_securelevel) {
1380 if (childmax >= ppr->pr_childmax) {
1386 if (enforce < ppr->pr_enforce_statfs) {
1393 * devfs_rsnum is a uint16_t
1395 if (rsnum < 0 || rsnum > 65535) {
1400 * Nested jails always inherit parent's devfs ruleset
1402 if (jailed(td->td_ucred)) {
1403 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1407 rsnum = ppr->pr_devfs_rsnum;
1412 if (ppr->pr_flags & PR_IP4) {
1414 * Make sure the new set of IP addresses is a
1415 * subset of the parent's list. Don't worry
1416 * about the parent being unlocked, as any
1417 * setting is done with allprison_lock held.
1419 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1420 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1422 if (ij == ppr->pr_ip4s) {
1427 for (ii = ij = 1; ii < ip4s; ii++) {
1428 if (ip4[ii].s_addr ==
1429 ppr->pr_ip4[0].s_addr)
1431 for (; ij < ppr->pr_ip4s; ij++)
1432 if (ip4[ii].s_addr ==
1433 ppr->pr_ip4[ij].s_addr)
1435 if (ij == ppr->pr_ip4s)
1438 if (ij == ppr->pr_ip4s) {
1445 * Check for conflicting IP addresses. We permit them
1446 * if there is no more than one IP on each jail. If
1447 * there is a duplicate on a jail with more than one
1448 * IP stop checking and return error.
1451 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1452 if (tppr->pr_flags & PR_VNET)
1457 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1460 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1462 !prison_isalive(tpr)) {
1466 if (!(tpr->pr_flags & PR_IP4_USER))
1469 if (tpr->pr_ip4 == NULL ||
1470 (ip4s == 1 && tpr->pr_ip4s == 1))
1472 for (ii = 0; ii < ip4s; ii++) {
1473 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1477 "IPv4 addresses clash");
1486 if (ppr->pr_flags & PR_IP6) {
1488 * Make sure the new set of IP addresses is a
1489 * subset of the parent's list.
1491 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1492 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1495 if (ij == ppr->pr_ip6s) {
1500 for (ii = ij = 1; ii < ip6s; ii++) {
1501 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1504 for (; ij < ppr->pr_ip6s; ij++)
1505 if (IN6_ARE_ADDR_EQUAL(
1506 &ip6[ii], &ppr->pr_ip6[ij]))
1508 if (ij == ppr->pr_ip6s)
1511 if (ij == ppr->pr_ip6s) {
1517 /* Check for conflicting IP addresses. */
1519 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1520 if (tppr->pr_flags & PR_VNET)
1525 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1528 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1530 !prison_isalive(tpr)) {
1534 if (!(tpr->pr_flags & PR_IP6_USER))
1537 if (tpr->pr_ip6 == NULL ||
1538 (ip6s == 1 && tpr->pr_ip6s == 1))
1540 for (ii = 0; ii < ip6s; ii++) {
1541 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1545 "IPv6 addresses clash");
1552 onamelen = namelen = 0;
1553 if (namelc != NULL) {
1554 /* Give a default name of the jid. Also allow the name to be
1555 * explicitly the jid - but not any other number, and only in
1556 * normal form (no leading zero/etc).
1558 if (namelc[0] == '\0')
1559 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1560 else if ((strtoul(namelc, &p, 10) != jid ||
1561 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1564 "name cannot be numeric (unless it is the jid)");
1568 * Make sure the name isn't too long for the prison or its
1571 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1572 onamelen = strlen(pr->pr_name + pnamelen);
1573 namelen = strlen(namelc);
1574 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1575 error = ENAMETOOLONG;
1578 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1579 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1580 sizeof(pr->pr_name)) {
1581 error = ENAMETOOLONG;
1586 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1587 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1593 * Let modules check their parameters. This requires unlocking and
1594 * then re-locking the prison, but this is still a valid state as long
1595 * as allprison_lock remains xlocked.
1597 mtx_unlock(&pr->pr_mtx);
1598 drflags &= ~PD_LOCKED;
1599 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1602 mtx_lock(&pr->pr_mtx);
1603 drflags |= PD_LOCKED;
1605 /* At this point, all valid parameters should have been noted. */
1606 TAILQ_FOREACH(opt, opts, link) {
1607 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1609 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1614 /* Set the parameters of the prison. */
1617 if (pr_flags & PR_IP4_USER) {
1618 pr->pr_flags |= PR_IP4;
1619 free(pr->pr_ip4, M_PRISON);
1623 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1625 if (tpr->pr_flags & PR_VNET) {
1630 if (prison_restrict_ip4(tpr, NULL)) {
1639 if (pr_flags & PR_IP6_USER) {
1640 pr->pr_flags |= PR_IP6;
1641 free(pr->pr_ip6, M_PRISON);
1645 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1647 if (tpr->pr_flags & PR_VNET) {
1652 if (prison_restrict_ip6(tpr, NULL)) {
1660 pr->pr_securelevel = slevel;
1661 /* Set all child jails to be at least this level. */
1662 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1663 if (tpr->pr_securelevel < slevel)
1664 tpr->pr_securelevel = slevel;
1667 pr->pr_childmax = childmax;
1668 /* Set all child jails to under this limit. */
1669 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1670 if (tpr->pr_childmax > childmax - level)
1671 tpr->pr_childmax = childmax > level
1672 ? childmax - level : 0;
1675 pr->pr_enforce_statfs = enforce;
1676 /* Pass this restriction on to the children. */
1677 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1678 if (tpr->pr_enforce_statfs < enforce)
1679 tpr->pr_enforce_statfs = enforce;
1682 pr->pr_devfs_rsnum = rsnum;
1683 /* Pass this restriction on to the children. */
1684 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1685 tpr->pr_devfs_rsnum = rsnum;
1687 if (namelc != NULL) {
1688 if (ppr == &prison0)
1689 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1691 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1692 ppr->pr_name, namelc);
1693 /* Change this component of child names. */
1694 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1695 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1696 strlen(tpr->pr_name + onamelen) + 1);
1697 bcopy(pr->pr_name, tpr->pr_name, namelen);
1701 /* Try to keep a real-rooted full pathname. */
1702 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1706 if (PR_HOST & ch_flags & ~pr_flags) {
1707 if (pr->pr_flags & PR_HOST) {
1709 * Copy the parent's host info. As with pr_ip4 above,
1710 * the lack of a lock on the parent is not a problem;
1711 * it is always set with allprison_lock at least
1712 * shared, and is held exclusively here.
1714 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1715 sizeof(pr->pr_hostname));
1716 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1717 sizeof(pr->pr_domainname));
1718 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1719 sizeof(pr->pr_hostuuid));
1720 pr->pr_hostid = pr->pr_parent->pr_hostid;
1722 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1723 /* Set this prison, and any descendants without PR_HOST. */
1725 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1727 strlcpy(pr->pr_domainname, domain,
1728 sizeof(pr->pr_domainname));
1730 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1732 pr->pr_hostid = hid;
1733 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1734 if (tpr->pr_flags & PR_HOST)
1738 strlcpy(tpr->pr_hostname,
1740 sizeof(tpr->pr_hostname));
1742 strlcpy(tpr->pr_domainname,
1744 sizeof(tpr->pr_domainname));
1746 strlcpy(tpr->pr_hostuuid,
1748 sizeof(tpr->pr_hostuuid));
1750 tpr->pr_hostid = hid;
1754 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1755 if ((tallow = ch_allow & ~pr_allow))
1756 prison_set_allow_locked(pr, tallow, 0);
1758 * Persistent prisons get an extra reference, and prisons losing their
1759 * persist flag lose that reference.
1761 born = !prison_isalive(pr);
1762 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1763 if (pr_flags & PR_PERSIST) {
1766 * This may make a dead prison alive again, but wait
1767 * to label it as such until after OSD calls have had
1768 * a chance to run (and perhaps to fail).
1770 refcount_acquire(&pr->pr_uref);
1772 drflags |= PD_DEUREF;
1773 prison_free_not_last(pr);
1776 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1777 mtx_unlock(&pr->pr_mtx);
1778 drflags &= ~PD_LOCKED;
1780 * Any errors past this point will need to de-persist newly created
1781 * prisons, as well as call remove methods.
1787 if (racct_enable && created)
1788 prison_racct_attach(pr);
1791 /* Locks may have prevented a complete restriction of child IP
1792 * addresses. If so, allocate some more memory and try again.
1797 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1798 mtx_lock(&pr->pr_mtx);
1800 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1802 if (tpr->pr_flags & PR_VNET) {
1807 if (prison_restrict_ip4(tpr, ip4)) {
1814 mtx_unlock(&pr->pr_mtx);
1820 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1821 mtx_lock(&pr->pr_mtx);
1823 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1825 if (tpr->pr_flags & PR_VNET) {
1830 if (prison_restrict_ip6(tpr, ip6)) {
1837 mtx_unlock(&pr->pr_mtx);
1841 /* Let the modules do their work. */
1843 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1847 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1852 * A new prison is now ready to be seen; either it has gained a user
1853 * reference via persistence, or is about to gain one via attachment.
1856 drflags = prison_lock_xlock(pr, drflags);
1857 pr->pr_state = PRISON_STATE_ALIVE;
1860 /* Attach this process to the prison if requested. */
1861 if (flags & JAIL_ATTACH) {
1862 error = do_jail_attach(td, pr,
1863 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
1864 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1866 vfs_opterror(opts, "attach failed");
1872 if (racct_enable && !created) {
1873 if (drflags & PD_LOCKED) {
1874 mtx_unlock(&pr->pr_mtx);
1875 drflags &= ~PD_LOCKED;
1877 if (drflags & PD_LIST_XLOCKED) {
1878 sx_xunlock(&allprison_lock);
1879 drflags &= ~PD_LIST_XLOCKED;
1881 prison_racct_modify(pr);
1885 drflags &= ~PD_KILL;
1886 td->td_retval[0] = pr->pr_id;
1889 /* Release any temporary prison holds and/or locks. */
1891 prison_deref(pr, drflags);
1892 else if (drflags & PD_LIST_SLOCKED)
1893 sx_sunlock(&allprison_lock);
1894 else if (drflags & PD_LIST_XLOCKED)
1895 sx_xunlock(&allprison_lock);
1900 /* Write the error message back to userspace. */
1901 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1902 &errmsg_len) == 0 && errmsg_len > 0) {
1903 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1904 if (optuio->uio_segflg == UIO_SYSSPACE)
1906 optuio->uio_iov[errmsg_pos].iov_base,
1910 optuio->uio_iov[errmsg_pos].iov_base,
1916 free(ip4, M_PRISON);
1919 free(ip6, M_PRISON);
1922 free(g_path, M_TEMP);
1928 * Find the next available prison ID. Return the ID on success, or zero
1929 * on failure. Also set a pointer to the allprison list entry the prison
1930 * should be inserted before.
1933 get_next_prid(struct prison **insprp)
1935 struct prison *inspr;
1938 jid = lastprid % JAIL_MAX + 1;
1939 if (TAILQ_EMPTY(&allprison) ||
1940 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1942 * A common case is for all jails to be implicitly numbered,
1943 * which means they'll go on the end of the list, at least
1944 * for the first JAIL_MAX times.
1949 * Take two passes through the allprison list: first starting
1950 * with the proposed jid, then ending with it.
1952 for (maxid = JAIL_MAX; maxid != 0; ) {
1953 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1954 if (inspr->pr_id < jid)
1956 if (inspr->pr_id > jid) {
1957 /* Found an opening. */
1961 if (++jid > maxid) {
1962 if (lastprid == maxid || lastprid == 0)
1965 * The entire legal range
1966 * has been traversed
1970 /* Try again from the start. */
1976 if (inspr == NULL) {
1977 /* Found room at the end of the list. */
1988 * struct jail_get_args {
1989 * struct iovec *iovp;
1990 * unsigned int iovcnt;
1995 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2000 /* Check that we have an even number of iovecs. */
2001 if (uap->iovcnt & 1)
2004 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2007 error = kern_jail_get(td, auio, uap->flags);
2009 error = copyout(auio->uio_iov, uap->iovp,
2010 uap->iovcnt * sizeof (struct iovec));
2016 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2018 struct bool_flags *bf;
2019 struct jailsys_flags *jsf;
2020 struct prison *pr, *mypr;
2022 struct vfsoptlist *opts;
2023 char *errmsg, *name;
2024 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2027 if (flags & ~JAIL_GET_MASK)
2030 /* Get the parameter list. */
2031 error = vfs_buildopts(optuio, &opts);
2034 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2035 mypr = td->td_ucred->cr_prison;
2039 * Find the prison specified by one of: lastjid, jid, name.
2041 sx_slock(&allprison_lock);
2042 drflags = PD_LIST_SLOCKED;
2043 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2045 TAILQ_FOREACH(pr, &allprison, pr_list) {
2046 if (pr->pr_id > jid &&
2047 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2048 prison_ischild(mypr, pr)) {
2049 mtx_lock(&pr->pr_mtx);
2050 drflags |= PD_LOCKED;
2055 vfs_opterror(opts, "no jail after %d", jid);
2057 } else if (error != ENOENT)
2060 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2063 pr = prison_find_child(mypr, jid);
2065 drflags |= PD_LOCKED;
2066 if (!(prison_isalive(pr) ||
2067 (flags & JAIL_DYING))) {
2069 vfs_opterror(opts, "jail %d is dying",
2076 vfs_opterror(opts, "jail %d not found", jid);
2079 } else if (error != ENOENT)
2082 error = vfs_getopt(opts, "name", (void **)&name, &len);
2084 if (len == 0 || name[len - 1] != '\0') {
2088 pr = prison_find_name(mypr, name);
2090 drflags |= PD_LOCKED;
2091 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2093 vfs_opterror(opts, "jail \"%s\" is dying",
2100 vfs_opterror(opts, "jail \"%s\" not found", name);
2102 } else if (error != ENOENT)
2105 vfs_opterror(opts, "no jail specified");
2110 /* Get the parameters of the prison. */
2112 drflags |= PD_DEREF;
2113 td->td_retval[0] = pr->pr_id;
2114 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2115 if (error != 0 && error != ENOENT)
2117 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2118 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2119 if (error != 0 && error != ENOENT)
2121 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2122 if (error != 0 && error != ENOENT)
2124 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2125 sizeof(pr->pr_cpuset->cs_id));
2126 if (error != 0 && error != ENOENT)
2128 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2129 if (error != 0 && error != ENOENT)
2132 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2133 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2134 if (error != 0 && error != ENOENT)
2138 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2139 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2140 if (error != 0 && error != ENOENT)
2143 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2144 sizeof(pr->pr_securelevel));
2145 if (error != 0 && error != ENOENT)
2147 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2148 sizeof(pr->pr_childcount));
2149 if (error != 0 && error != ENOENT)
2151 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2152 sizeof(pr->pr_childmax));
2153 if (error != 0 && error != ENOENT)
2155 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2156 if (error != 0 && error != ENOENT)
2158 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2159 if (error != 0 && error != ENOENT)
2161 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2162 if (error != 0 && error != ENOENT)
2164 #ifdef COMPAT_FREEBSD32
2165 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2166 uint32_t hid32 = pr->pr_hostid;
2168 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2171 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2172 sizeof(pr->pr_hostid));
2173 if (error != 0 && error != ENOENT)
2175 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2176 sizeof(pr->pr_enforce_statfs));
2177 if (error != 0 && error != ENOENT)
2179 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2180 sizeof(pr->pr_devfs_rsnum));
2181 if (error != 0 && error != ENOENT)
2183 for (bf = pr_flag_bool;
2184 bf < pr_flag_bool + nitems(pr_flag_bool);
2186 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2187 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2188 if (error != 0 && error != ENOENT)
2191 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2192 if (error != 0 && error != ENOENT)
2195 for (jsf = pr_flag_jailsys;
2196 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2198 f = pr->pr_flags & (jsf->disable | jsf->new);
2199 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2200 : (f == jsf->new) ? JAIL_SYS_NEW
2202 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2203 if (error != 0 && error != ENOENT)
2206 for (bf = pr_flag_allow;
2207 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2208 atomic_load_int(&bf->flag) != 0;
2210 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2211 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2212 if (error != 0 && error != ENOENT)
2215 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2216 if (error != 0 && error != ENOENT)
2219 i = !prison_isalive(pr);
2220 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2221 if (error != 0 && error != ENOENT)
2224 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2225 if (error != 0 && error != ENOENT)
2227 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2228 sizeof(pr->pr_osreldate));
2229 if (error != 0 && error != ENOENT)
2231 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2232 if (error != 0 && error != ENOENT)
2235 /* Get the module parameters. */
2236 mtx_unlock(&pr->pr_mtx);
2237 drflags &= ~PD_LOCKED;
2238 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2241 prison_deref(pr, drflags);
2245 /* By now, all parameters should have been noted. */
2246 TAILQ_FOREACH(opt, opts, link) {
2247 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2249 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2254 /* Write the fetched parameters back to userspace. */
2256 TAILQ_FOREACH(opt, opts, link) {
2257 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2258 pos = 2 * opt->pos + 1;
2259 optuio->uio_iov[pos].iov_len = opt->len;
2260 if (opt->value != NULL) {
2261 if (optuio->uio_segflg == UIO_SYSSPACE) {
2263 optuio->uio_iov[pos].iov_base,
2266 error = copyout(opt->value,
2267 optuio->uio_iov[pos].iov_base,
2277 /* Release any temporary prison holds and/or locks. */
2279 prison_deref(pr, drflags);
2280 else if (drflags & PD_LIST_SLOCKED)
2281 sx_sunlock(&allprison_lock);
2282 if (error && errmsg_pos >= 0) {
2283 /* Write the error message back to userspace. */
2284 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2285 errmsg_pos = 2 * errmsg_pos + 1;
2286 if (errmsg_len > 0) {
2287 if (optuio->uio_segflg == UIO_SYSSPACE)
2289 optuio->uio_iov[errmsg_pos].iov_base,
2293 optuio->uio_iov[errmsg_pos].iov_base,
2302 * struct jail_remove_args {
2307 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2312 error = priv_check(td, PRIV_JAIL_REMOVE);
2316 sx_xlock(&allprison_lock);
2317 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2319 sx_xunlock(&allprison_lock);
2322 if (!prison_isalive(pr)) {
2323 /* Silently ignore already-dying prisons. */
2324 mtx_unlock(&pr->pr_mtx);
2325 sx_xunlock(&allprison_lock);
2328 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2333 * struct jail_attach_args {
2338 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2343 error = priv_check(td, PRIV_JAIL_ATTACH);
2347 sx_slock(&allprison_lock);
2348 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2350 sx_sunlock(&allprison_lock);
2354 /* Do not allow a process to attach to a prison that is not alive. */
2355 if (!prison_isalive(pr)) {
2356 mtx_unlock(&pr->pr_mtx);
2357 sx_sunlock(&allprison_lock);
2361 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2365 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2368 struct ucred *newcred, *oldcred;
2371 mtx_assert(&pr->pr_mtx, MA_OWNED);
2372 sx_assert(&allprison_lock, SX_LOCKED);
2373 drflags &= PD_LOCK_FLAGS;
2375 * XXX: Note that there is a slight race here if two threads
2376 * in the same privileged process attempt to attach to two
2377 * different jails at the same time. It is important for
2378 * user processes not to do this, or they might end up with
2379 * a process root from one prison, but attached to the jail
2383 refcount_acquire(&pr->pr_uref);
2384 drflags |= PD_DEREF | PD_DEUREF;
2385 mtx_unlock(&pr->pr_mtx);
2386 drflags &= ~PD_LOCKED;
2388 /* Let modules do whatever they need to prepare for attaching. */
2389 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2391 prison_deref(pr, drflags);
2394 sx_unlock(&allprison_lock);
2395 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2398 * Reparent the newly attached process to this jail.
2401 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2405 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2406 if ((error = change_dir(pr->pr_root, td)) != 0)
2409 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2412 VOP_UNLOCK(pr->pr_root);
2413 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2418 oldcred = crcopysafe(p, newcred);
2419 newcred->cr_prison = pr;
2420 proc_set_cred(p, newcred);
2423 racct_proc_ucred_changed(p, oldcred, newcred);
2428 rctl_proc_ucred_changed(p, newcred);
2431 prison_deref(oldcred->cr_prison, drflags);
2435 * If the prison was killed while changing credentials, die along
2438 if (!prison_isalive(pr)) {
2440 kern_psignal(p, SIGKILL);
2447 VOP_UNLOCK(pr->pr_root);
2449 /* Tell modules this thread is still in its old jail after all. */
2450 sx_slock(&allprison_lock);
2451 drflags |= PD_LIST_SLOCKED;
2452 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2453 prison_deref(pr, drflags);
2458 * Returns a locked prison instance, or NULL on failure.
2461 prison_find(int prid)
2465 sx_assert(&allprison_lock, SX_LOCKED);
2466 TAILQ_FOREACH(pr, &allprison, pr_list) {
2467 if (pr->pr_id < prid)
2469 if (pr->pr_id > prid)
2471 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2472 mtx_lock(&pr->pr_mtx);
2479 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2482 prison_find_child(struct prison *mypr, int prid)
2487 sx_assert(&allprison_lock, SX_LOCKED);
2488 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2489 if (pr->pr_id == prid) {
2490 KASSERT(prison_isvalid(pr),
2491 ("Found invalid prison %p", pr));
2492 mtx_lock(&pr->pr_mtx);
2500 * Look for the name relative to mypr. Returns a locked prison or NULL.
2503 prison_find_name(struct prison *mypr, const char *name)
2505 struct prison *pr, *deadpr;
2509 sx_assert(&allprison_lock, SX_LOCKED);
2510 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2512 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2513 if (!strcmp(pr->pr_name + mylen, name)) {
2514 KASSERT(prison_isvalid(pr),
2515 ("Found invalid prison %p", pr));
2516 if (prison_isalive(pr)) {
2517 mtx_lock(&pr->pr_mtx);
2523 /* There was no valid prison - perhaps there was a dying one. */
2525 mtx_lock(&deadpr->pr_mtx);
2530 * See if a prison has the specific flag set. The prison should be locked,
2531 * unless checking for flags that are only set at jail creation (such as
2532 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2533 * to any other prison data.
2536 prison_flag(struct ucred *cred, unsigned flag)
2539 return (cred->cr_prison->pr_flags & flag);
2543 prison_allow(struct ucred *cred, unsigned flag)
2546 return ((cred->cr_prison->pr_allow & flag) != 0);
2550 * Hold a prison reference, by incrementing pr_ref. It is generally
2551 * an error to hold a prison that does not already have a reference.
2552 * A prison record will remain valid as long as it has at least one
2553 * reference, and will not be removed as long as either the prison
2554 * mutex or the allprison lock is held (allprison_lock may be shared).
2557 prison_hold_locked(struct prison *pr)
2560 /* Locking is no longer required. */
2565 prison_hold(struct prison *pr)
2568 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2571 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2573 refcount_acquire(&pr->pr_ref);
2578 * Remove a prison reference. If that was the last reference, the
2579 * prison will be removed (at a later time).
2582 prison_free_locked(struct prison *pr)
2585 mtx_assert(&pr->pr_mtx, MA_OWNED);
2587 * Locking is no longer required, but unlock because the caller
2590 mtx_unlock(&pr->pr_mtx);
2595 prison_free(struct prison *pr)
2598 KASSERT(refcount_load(&pr->pr_ref) > 0,
2599 ("Trying to free dead prison %p (jid=%d).",
2601 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2603 * Don't remove the last reference in this context,
2604 * in case there are locks held.
2606 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2611 prison_free_not_last(struct prison *pr)
2616 KASSERT(refcount_load(&pr->pr_ref) > 0,
2617 ("Trying to free dead prison %p (jid=%d).",
2619 lastref = refcount_release(&pr->pr_ref);
2621 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2624 refcount_release(&pr->pr_ref);
2629 * Hold a a prison for user visibility, by incrementing pr_uref.
2630 * It is generally an error to hold a prison that isn't already
2631 * user-visible, except through the the jail system calls. It is also
2632 * an error to hold an invalid prison. A prison record will remain
2633 * alive as long as it has at least one user reference, and will not
2634 * be set to the dying state until the prison mutex and allprison_lock
2638 prison_proc_hold(struct prison *pr)
2641 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2644 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2646 refcount_acquire(&pr->pr_uref);
2651 * Remove a prison user reference. If it was the last reference, the
2652 * prison will be considered "dying", and may be removed once all of
2653 * its references are dropped.
2656 prison_proc_free(struct prison *pr)
2660 * Locking is only required when releasing the last reference.
2661 * This allows assurance that a locked prison will remain alive
2662 * until it is unlocked.
2664 KASSERT(refcount_load(&pr->pr_uref) > 0,
2665 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2666 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2668 * Don't remove the last user reference in this context,
2669 * which is expected to be a process that is not only locked,
2670 * but also half dead. Add a reference so any calls to
2671 * prison_free() won't re-submit the task.
2674 mtx_lock(&pr->pr_mtx);
2675 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2676 ("Redundant last reference in prison_proc_free (jid=%d)",
2678 pr->pr_flags |= PR_COMPLETE_PROC;
2679 mtx_unlock(&pr->pr_mtx);
2680 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2685 prison_proc_free_not_last(struct prison *pr)
2690 KASSERT(refcount_load(&pr->pr_uref) > 0,
2691 ("Trying to free dead prison %p (jid=%d).",
2693 lastref = refcount_release(&pr->pr_uref);
2695 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2698 refcount_release(&pr->pr_uref);
2703 * Complete a call to either prison_free or prison_proc_free.
2706 prison_complete(void *context, int pending)
2708 struct prison *pr = context;
2712 * This could be called to release the last reference, or the last
2713 * user reference (plus the reference held in prison_proc_free).
2715 drflags = prison_lock_xlock(pr, PD_DEREF);
2716 if (pr->pr_flags & PR_COMPLETE_PROC) {
2717 pr->pr_flags &= ~PR_COMPLETE_PROC;
2718 drflags |= PD_DEUREF;
2720 prison_deref(pr, drflags);
2724 * Remove a prison reference and/or user reference (usually).
2725 * This assumes context that allows sleeping (for allprison_lock),
2726 * with no non-sleeping locks held, except perhaps the prison itself.
2727 * If there are no more references, release and delist the prison.
2728 * On completion, the prison lock and the allprison lock are both
2732 prison_deref(struct prison *pr, int flags)
2734 struct prisonlist freeprison;
2735 struct prison *killpr, *rpr, *ppr, *tpr;
2739 TAILQ_INIT(&freeprison);
2741 * Release this prison as requested, which may cause its parent
2742 * to be released, and then maybe its grandparent, etc.
2745 if (flags & PD_KILL) {
2746 /* Kill the prison and its descendents. */
2747 KASSERT(pr != &prison0,
2748 ("prison_deref trying to kill prison0"));
2749 if (!(flags & PD_DEREF)) {
2753 flags = prison_lock_xlock(pr, flags);
2754 prison_deref_kill(pr, &freeprison);
2756 if (flags & PD_DEUREF) {
2757 /* Drop a user reference. */
2758 KASSERT(refcount_load(&pr->pr_uref) > 0,
2759 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2761 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2762 if (!(flags & PD_DEREF)) {
2766 flags = prison_lock_xlock(pr, flags);
2767 if (refcount_release(&pr->pr_uref) &&
2768 pr->pr_state == PRISON_STATE_ALIVE) {
2770 * When the last user references goes,
2771 * this becomes a dying prison.
2774 refcount_load(&prison0.pr_uref) > 0,
2775 ("prison0 pr_uref=0"));
2776 pr->pr_state = PRISON_STATE_DYING;
2777 mtx_unlock(&pr->pr_mtx);
2778 flags &= ~PD_LOCKED;
2779 (void)osd_jail_call(pr,
2780 PR_METHOD_REMOVE, NULL);
2784 if (flags & PD_KILL) {
2786 * Any remaining user references are probably processes
2787 * that need to be killed, either in this prison or its
2790 if (refcount_load(&pr->pr_uref) > 0)
2792 /* Make sure the parent prison doesn't get killed. */
2795 if (flags & PD_DEREF) {
2796 /* Drop a reference. */
2797 KASSERT(refcount_load(&pr->pr_ref) > 0,
2798 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2800 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2801 flags = prison_lock_xlock(pr, flags);
2802 if (refcount_release(&pr->pr_ref)) {
2804 * When the last reference goes,
2805 * unlink the prison and set it aside.
2808 refcount_load(&pr->pr_uref) == 0,
2809 ("prison_deref: last ref, "
2810 "but still has %d urefs (jid=%d)",
2811 pr->pr_uref, pr->pr_id));
2813 refcount_load(&prison0.pr_ref) != 0,
2814 ("prison0 pr_ref=0"));
2815 pr->pr_state = PRISON_STATE_INVALID;
2816 TAILQ_REMOVE(&allprison, pr, pr_list);
2817 LIST_REMOVE(pr, pr_sibling);
2818 TAILQ_INSERT_TAIL(&freeprison, pr,
2820 for (ppr = pr->pr_parent;
2822 ppr = ppr->pr_parent)
2823 ppr->pr_childcount--;
2825 * Removing a prison frees references
2828 mtx_unlock(&pr->pr_mtx);
2829 flags &= ~PD_LOCKED;
2831 flags |= PD_DEREF | PD_DEUREF;
2839 /* Release all the prison locks. */
2840 if (flags & PD_LOCKED)
2841 mtx_unlock(&pr->pr_mtx);
2842 if (flags & PD_LIST_SLOCKED)
2843 sx_sunlock(&allprison_lock);
2844 else if (flags & PD_LIST_XLOCKED)
2845 sx_xunlock(&allprison_lock);
2847 /* Kill any processes attached to a killed prison. */
2848 if (killpr != NULL) {
2849 sx_slock(&allproc_lock);
2850 FOREACH_PROC_IN_SYSTEM(p) {
2852 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
2853 for (ppr = p->p_ucred->cr_prison;
2855 ppr = ppr->pr_parent)
2856 if (ppr == killpr) {
2857 kern_psignal(p, SIGKILL);
2863 sx_sunlock(&allproc_lock);
2867 * Finish removing any unreferenced prisons, which couldn't happen
2868 * while allprison_lock was held (to avoid a LOR on vrele).
2870 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
2872 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
2873 vnet_destroy(rpr->pr_vnet);
2875 if (rpr->pr_root != NULL)
2876 vrele(rpr->pr_root);
2877 mtx_destroy(&rpr->pr_mtx);
2879 free(rpr->pr_ip4, M_PRISON);
2882 free(rpr->pr_ip6, M_PRISON);
2884 if (rpr->pr_cpuset != NULL)
2885 cpuset_rel(rpr->pr_cpuset);
2889 prison_racct_detach(rpr);
2891 TAILQ_REMOVE(&freeprison, rpr, pr_list);
2892 free(rpr, M_PRISON);
2897 * Kill the prison and its descendants. Mark them as dying, clear the
2898 * persist flag, and call module remove methods.
2901 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
2903 struct prison *cpr, *ppr, *rpr;
2907 * Unlike the descendants, the target prison can be killed
2908 * even if it is currently dying. This is useful for failed
2909 * creation in jail_set(2).
2911 KASSERT(refcount_load(&pr->pr_ref) > 0,
2912 ("Trying to kill dead prison %p (jid=%d).",
2914 refcount_acquire(&pr->pr_uref);
2915 pr->pr_state = PRISON_STATE_DYING;
2916 mtx_unlock(&pr->pr_mtx);
2919 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
2921 if (!prison_isalive(cpr)) {
2926 prison_proc_hold(cpr);
2927 mtx_lock(&cpr->pr_mtx);
2928 cpr->pr_state = PRISON_STATE_DYING;
2929 cpr->pr_flags |= PR_REMOVE;
2930 mtx_unlock(&cpr->pr_mtx);
2933 if (!(cpr->pr_flags & PR_REMOVE))
2935 (void)osd_jail_call(cpr, PR_METHOD_REMOVE, NULL);
2936 mtx_lock(&cpr->pr_mtx);
2937 cpr->pr_flags &= ~PR_REMOVE;
2938 if (cpr->pr_flags & PR_PERSIST) {
2939 cpr->pr_flags &= ~PR_PERSIST;
2940 prison_proc_free_not_last(cpr);
2941 prison_free_not_last(cpr);
2943 (void)refcount_release(&cpr->pr_uref);
2944 if (refcount_release(&cpr->pr_ref)) {
2946 * When the last reference goes, unlink the prison
2947 * and set it aside for prison_deref() to handle.
2948 * Delay unlinking the sibling list to keep the loop
2952 LIST_REMOVE(rpr, pr_sibling);
2954 rpr->pr_state = PRISON_STATE_INVALID;
2955 TAILQ_REMOVE(&allprison, rpr, pr_list);
2956 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
2958 * Removing a prison frees references from its parent.
2960 ppr = rpr->pr_parent;
2961 prison_proc_free_not_last(ppr);
2962 prison_free_not_last(ppr);
2963 for (; ppr != NULL; ppr = ppr->pr_parent)
2964 ppr->pr_childcount--;
2966 mtx_unlock(&cpr->pr_mtx);
2969 LIST_REMOVE(rpr, pr_sibling);
2971 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2972 mtx_lock(&pr->pr_mtx);
2973 if (pr->pr_flags & PR_PERSIST) {
2974 pr->pr_flags &= ~PR_PERSIST;
2975 prison_proc_free_not_last(pr);
2976 prison_free_not_last(pr);
2978 (void)refcount_release(&pr->pr_uref);
2982 * Given the current locking state in the flags, make sure allprison_lock
2983 * is held exclusive, and the prison is locked. Return flags indicating
2987 prison_lock_xlock(struct prison *pr, int flags)
2990 if (!(flags & PD_LIST_XLOCKED)) {
2992 * Get allprison_lock, which may be an upgrade,
2993 * and may require unlocking the prison.
2995 if (flags & PD_LOCKED) {
2996 mtx_unlock(&pr->pr_mtx);
2997 flags &= ~PD_LOCKED;
2999 if (flags & PD_LIST_SLOCKED) {
3000 if (!sx_try_upgrade(&allprison_lock)) {
3001 sx_sunlock(&allprison_lock);
3002 sx_xlock(&allprison_lock);
3004 flags &= ~PD_LIST_SLOCKED;
3006 sx_xlock(&allprison_lock);
3007 flags |= PD_LIST_XLOCKED;
3009 if (!(flags & PD_LOCKED)) {
3010 /* Lock the prison mutex. */
3011 mtx_lock(&pr->pr_mtx);
3018 * Set or clear a permission bit in the pr_allow field, passing restrictions
3019 * (cleared permission) down to child jails.
3022 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3026 pr = cred->cr_prison;
3027 sx_slock(&allprison_lock);
3028 mtx_lock(&pr->pr_mtx);
3029 prison_set_allow_locked(pr, flag, enable);
3030 mtx_unlock(&pr->pr_mtx);
3031 sx_sunlock(&allprison_lock);
3035 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3041 pr->pr_allow |= flag;
3043 pr->pr_allow &= ~flag;
3044 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3045 cpr->pr_allow &= ~flag;
3050 * Check if a jail supports the given address family.
3052 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3056 prison_check_af(struct ucred *cred, int af)
3061 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3063 pr = cred->cr_prison;
3065 /* Prisons with their own network stack are not limited. */
3066 if (prison_owns_vnet(cred))
3075 if (pr->pr_flags & PR_IP4)
3077 mtx_lock(&pr->pr_mtx);
3078 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3079 error = EAFNOSUPPORT;
3080 mtx_unlock(&pr->pr_mtx);
3086 if (pr->pr_flags & PR_IP6)
3088 mtx_lock(&pr->pr_mtx);
3089 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3090 error = EAFNOSUPPORT;
3091 mtx_unlock(&pr->pr_mtx);
3099 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3100 error = EAFNOSUPPORT;
3106 * Check if given address belongs to the jail referenced by cred (wrapper to
3107 * prison_check_ip[46]).
3109 * Returns 0 if jail doesn't restrict the address family or if address belongs
3110 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3111 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3114 prison_if(struct ucred *cred, const struct sockaddr *sa)
3117 const struct sockaddr_in *sai;
3120 const struct sockaddr_in6 *sai6;
3124 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3125 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3128 if (prison_owns_vnet(cred))
3133 switch (sa->sa_family)
3137 sai = (const struct sockaddr_in *)sa;
3138 error = prison_check_ip4(cred, &sai->sin_addr);
3143 sai6 = (const struct sockaddr_in6 *)sa;
3144 error = prison_check_ip6(cred, &sai6->sin6_addr);
3148 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3149 error = EAFNOSUPPORT;
3155 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3158 prison_check(struct ucred *cred1, struct ucred *cred2)
3161 return ((cred1->cr_prison == cred2->cr_prison ||
3162 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3166 * Return 1 if p2 is a child of p1, otherwise 0.
3169 prison_ischild(struct prison *pr1, struct prison *pr2)
3172 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3179 * Return true if the prison is currently alive. A prison is alive if it
3180 * holds user references and it isn't being removed.
3183 prison_isalive(struct prison *pr)
3186 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3192 * Return true if the prison is currently valid. A prison is valid if it has
3193 * been fully created, and is not being destroyed. Note that dying prisons
3194 * are still considered valid. Invalid prisons won't be found under normal
3195 * circumstances, as they're only put in that state by functions that have
3196 * an exclusive hold on allprison_lock.
3199 prison_isvalid(struct prison *pr)
3202 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3204 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3210 * Return 1 if the passed credential is in a jail and that jail does not
3211 * have its own virtual network stack, otherwise 0.
3214 jailed_without_vnet(struct ucred *cred)
3220 if (prison_owns_vnet(cred))
3228 * Return the correct hostname (domainname, et al) for the passed credential.
3231 getcredhostname(struct ucred *cred, char *buf, size_t size)
3236 * A NULL credential can be used to shortcut to the physical
3237 * system's hostname.
3239 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3240 mtx_lock(&pr->pr_mtx);
3241 strlcpy(buf, pr->pr_hostname, size);
3242 mtx_unlock(&pr->pr_mtx);
3246 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3249 mtx_lock(&cred->cr_prison->pr_mtx);
3250 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3251 mtx_unlock(&cred->cr_prison->pr_mtx);
3255 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3258 mtx_lock(&cred->cr_prison->pr_mtx);
3259 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3260 mtx_unlock(&cred->cr_prison->pr_mtx);
3264 getcredhostid(struct ucred *cred, unsigned long *hostid)
3267 mtx_lock(&cred->cr_prison->pr_mtx);
3268 *hostid = cred->cr_prison->pr_hostid;
3269 mtx_unlock(&cred->cr_prison->pr_mtx);
3273 getjailname(struct ucred *cred, char *name, size_t len)
3276 mtx_lock(&cred->cr_prison->pr_mtx);
3277 strlcpy(name, cred->cr_prison->pr_name, len);
3278 mtx_unlock(&cred->cr_prison->pr_mtx);
3283 * Determine whether the prison represented by cred owns
3284 * its vnet rather than having it inherited.
3286 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3289 prison_owns_vnet(struct ucred *cred)
3293 * vnets cannot be added/removed after jail creation,
3294 * so no need to lock here.
3296 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3301 * Determine whether the subject represented by cred can "see"
3302 * status of a mount point.
3303 * Returns: 0 for permitted, ENOENT otherwise.
3304 * XXX: This function should be called cr_canseemount() and should be
3305 * placed in kern_prot.c.
3308 prison_canseemount(struct ucred *cred, struct mount *mp)
3314 pr = cred->cr_prison;
3315 if (pr->pr_enforce_statfs == 0)
3317 if (pr->pr_root->v_mount == mp)
3319 if (pr->pr_enforce_statfs == 2)
3322 * If jail's chroot directory is set to "/" we should be able to see
3323 * all mount-points from inside a jail.
3324 * This is ugly check, but this is the only situation when jail's
3325 * directory ends with '/'.
3327 if (strcmp(pr->pr_path, "/") == 0)
3329 len = strlen(pr->pr_path);
3331 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3334 * Be sure that we don't have situation where jail's root directory
3335 * is "/some/path" and mount point is "/some/pathpath".
3337 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3343 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3345 char jpath[MAXPATHLEN];
3349 pr = cred->cr_prison;
3350 if (pr->pr_enforce_statfs == 0)
3352 if (prison_canseemount(cred, mp) != 0) {
3353 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3354 strlcpy(sp->f_mntonname, "[restricted]",
3355 sizeof(sp->f_mntonname));
3358 if (pr->pr_root->v_mount == mp) {
3360 * Clear current buffer data, so we are sure nothing from
3361 * the valid path left there.
3363 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3364 *sp->f_mntonname = '/';
3368 * If jail's chroot directory is set to "/" we should be able to see
3369 * all mount-points from inside a jail.
3371 if (strcmp(pr->pr_path, "/") == 0)
3373 len = strlen(pr->pr_path);
3374 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3376 * Clear current buffer data, so we are sure nothing from
3377 * the valid path left there.
3379 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3380 if (*jpath == '\0') {
3381 /* Should never happen. */
3382 *sp->f_mntonname = '/';
3384 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3389 * Check with permission for a specific privilege is granted within jail. We
3390 * have a specific list of accepted privileges; the rest are denied.
3393 prison_priv_check(struct ucred *cred, int priv)
3399 * Some policies have custom handlers. This routine should not be
3400 * called for them. See priv_check_cred().
3403 case PRIV_VFS_LOOKUP:
3404 case PRIV_VFS_GENERATION:
3405 KASSERT(0, ("prison_priv_check instead of a custom handler "
3406 "called for %d\n", priv));
3414 * Privileges specific to prisons with a virtual network stack.
3415 * There might be a duplicate entry here in case the privilege
3416 * is only granted conditionally in the legacy jail case.
3421 * NFS-specific privileges.
3423 case PRIV_NFS_DAEMON:
3424 case PRIV_NFS_LOCKD:
3427 * Network stack privileges.
3429 case PRIV_NET_BRIDGE:
3432 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3433 case PRIV_NET_ROUTE:
3435 case PRIV_NET_SETIFMTU:
3436 case PRIV_NET_SETIFFLAGS:
3437 case PRIV_NET_SETIFCAP:
3438 case PRIV_NET_SETIFDESCR:
3439 case PRIV_NET_SETIFNAME :
3440 case PRIV_NET_SETIFMETRIC:
3441 case PRIV_NET_SETIFPHYS:
3442 case PRIV_NET_SETIFMAC:
3443 case PRIV_NET_SETLANPCP:
3444 case PRIV_NET_ADDMULTI:
3445 case PRIV_NET_DELMULTI:
3446 case PRIV_NET_HWIOCTL:
3447 case PRIV_NET_SETLLADDR:
3448 case PRIV_NET_ADDIFGROUP:
3449 case PRIV_NET_DELIFGROUP:
3450 case PRIV_NET_IFCREATE:
3451 case PRIV_NET_IFDESTROY:
3452 case PRIV_NET_ADDIFADDR:
3453 case PRIV_NET_DELIFADDR:
3456 case PRIV_NET_SETIFVNET:
3457 case PRIV_NET_SETIFFIB:
3460 * 802.11-related privileges.
3462 case PRIV_NET80211_VAP_GETKEY:
3463 case PRIV_NET80211_VAP_MANAGE:
3469 case PRIV_NETATM_CFG:
3470 case PRIV_NETATM_ADD:
3471 case PRIV_NETATM_DEL:
3472 case PRIV_NETATM_SET:
3475 * Bluetooth privileges.
3477 case PRIV_NETBLUETOOTH_RAW:
3481 * Netgraph and netgraph module privileges.
3483 case PRIV_NETGRAPH_CONTROL:
3485 case PRIV_NETGRAPH_TTY:
3489 * IPv4 and IPv6 privileges.
3491 case PRIV_NETINET_IPFW:
3492 case PRIV_NETINET_DIVERT:
3493 case PRIV_NETINET_PF:
3494 case PRIV_NETINET_DUMMYNET:
3495 case PRIV_NETINET_CARP:
3496 case PRIV_NETINET_MROUTE:
3497 case PRIV_NETINET_RAW:
3498 case PRIV_NETINET_ADDRCTRL6:
3499 case PRIV_NETINET_ND6:
3500 case PRIV_NETINET_SCOPE6:
3501 case PRIV_NETINET_ALIFETIME6:
3502 case PRIV_NETINET_IPSEC:
3503 case PRIV_NETINET_BINDANY:
3518 * No default: or deny here.
3519 * In case of no permit fall through to next switch().
3521 if (cred->cr_prison->pr_flags & PR_VNET)
3528 * Allow ktrace privileges for root in jail.
3534 * Allow jailed processes to configure audit identity and
3535 * submit audit records (login, etc). In the future we may
3536 * want to further refine the relationship between audit and
3539 case PRIV_AUDIT_GETAUDIT:
3540 case PRIV_AUDIT_SETAUDIT:
3541 case PRIV_AUDIT_SUBMIT:
3545 * Allow jailed processes to manipulate process UNIX
3546 * credentials in any way they see fit.
3548 case PRIV_CRED_SETUID:
3549 case PRIV_CRED_SETEUID:
3550 case PRIV_CRED_SETGID:
3551 case PRIV_CRED_SETEGID:
3552 case PRIV_CRED_SETGROUPS:
3553 case PRIV_CRED_SETREUID:
3554 case PRIV_CRED_SETREGID:
3555 case PRIV_CRED_SETRESUID:
3556 case PRIV_CRED_SETRESGID:
3559 * Jail implements visibility constraints already, so allow
3560 * jailed root to override uid/gid-based constraints.
3562 case PRIV_SEEOTHERGIDS:
3563 case PRIV_SEEOTHERUIDS:
3566 * Jail implements inter-process debugging limits already, so
3567 * allow jailed root various debugging privileges.
3569 case PRIV_DEBUG_DIFFCRED:
3570 case PRIV_DEBUG_SUGID:
3571 case PRIV_DEBUG_UNPRIV:
3574 * Allow jail to set various resource limits and login
3575 * properties, and for now, exceed process resource limits.
3577 case PRIV_PROC_LIMIT:
3578 case PRIV_PROC_SETLOGIN:
3579 case PRIV_PROC_SETRLIMIT:
3582 * System V and POSIX IPC privileges are granted in jail.
3585 case PRIV_IPC_WRITE:
3586 case PRIV_IPC_ADMIN:
3587 case PRIV_IPC_MSGSIZE:
3591 * Jail operations within a jail work on child jails.
3593 case PRIV_JAIL_ATTACH:
3595 case PRIV_JAIL_REMOVE:
3598 * Jail implements its own inter-process limits, so allow
3599 * root processes in jail to change scheduling on other
3600 * processes in the same jail. Likewise for signalling.
3602 case PRIV_SCHED_DIFFCRED:
3603 case PRIV_SCHED_CPUSET:
3604 case PRIV_SIGNAL_DIFFCRED:
3605 case PRIV_SIGNAL_SUGID:
3608 * Allow jailed processes to write to sysctls marked as jail
3611 case PRIV_SYSCTL_WRITEJAIL:
3614 * Allow root in jail to manage a variety of quota
3615 * properties. These should likely be conditional on a
3616 * configuration option.
3618 case PRIV_VFS_GETQUOTA:
3619 case PRIV_VFS_SETQUOTA:
3622 * Since Jail relies on chroot() to implement file system
3623 * protections, grant many VFS privileges to root in jail.
3624 * Be careful to exclude mount-related and NFS-related
3628 case PRIV_VFS_WRITE:
3629 case PRIV_VFS_ADMIN:
3631 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3632 case PRIV_VFS_CHFLAGS_DEV:
3633 case PRIV_VFS_CHOWN:
3634 case PRIV_VFS_CHROOT:
3635 case PRIV_VFS_RETAINSUGID:
3636 case PRIV_VFS_FCHROOT:
3638 case PRIV_VFS_SETGID:
3640 case PRIV_VFS_STICKYFILE:
3643 * As in the non-jail case, non-root users are expected to be
3644 * able to read kernel/physical memory (provided /dev/[k]mem
3645 * exists in the jail and they have permission to access it).
3647 case PRIV_KMEM_READ:
3651 * Depending on the global setting, allow privilege of
3652 * setting system flags.
3654 case PRIV_VFS_SYSFLAGS:
3655 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3661 * Depending on the global setting, allow privilege of
3662 * mounting/unmounting file systems.
3664 case PRIV_VFS_MOUNT:
3665 case PRIV_VFS_UNMOUNT:
3666 case PRIV_VFS_MOUNT_NONUSER:
3667 case PRIV_VFS_MOUNT_OWNER:
3668 pr = cred->cr_prison;
3670 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3678 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3679 * policy. priv_check_cred will not specifically allow it, and
3680 * we may want a MAC policy to allow it.
3682 case PRIV_VFS_READ_DIR:
3686 * Conditionnaly allow locking (unlocking) physical pages
3690 case PRIV_VM_MUNLOCK:
3691 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3697 * Conditionally allow jailed root to bind reserved ports.
3699 case PRIV_NETINET_RESERVEDPORT:
3700 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3706 * Allow jailed root to reuse in-use ports.
3708 case PRIV_NETINET_REUSEPORT:
3712 * Allow jailed root to set certain IPv4/6 (option) headers.
3714 case PRIV_NETINET_SETHDROPTS:
3718 * Conditionally allow creating raw sockets in jail.
3720 case PRIV_NETINET_RAW:
3721 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3727 * Since jail implements its own visibility limits on netstat
3728 * sysctls, allow getcred. This allows identd to work in
3731 case PRIV_NETINET_GETCRED:
3735 * Allow jailed root to set loginclass.
3737 case PRIV_PROC_SETLOGINCLASS:
3741 * Do not allow a process inside a jail to read the kernel
3742 * message buffer unless explicitly permitted.
3745 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3751 * In all remaining cases, deny the privilege request. This
3752 * includes almost all network privileges, many system
3753 * configuration privileges.
3760 * Return the part of pr2's name that is relative to pr1, or the whole name
3761 * if it does not directly follow.
3765 prison_name(struct prison *pr1, struct prison *pr2)
3769 /* Jails see themselves as "0" (if they see themselves at all). */
3772 name = pr2->pr_name;
3773 if (prison_ischild(pr1, pr2)) {
3775 * pr1 isn't locked (and allprison_lock may not be either)
3776 * so its length can't be counted on. But the number of dots
3777 * can be counted on - and counted.
3779 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3780 name = strchr(name, '.') + 1;
3786 * Return the part of pr2's path that is relative to pr1, or the whole path
3787 * if it does not directly follow.
3790 prison_path(struct prison *pr1, struct prison *pr2)
3792 char *path1, *path2;
3795 path1 = pr1->pr_path;
3796 path2 = pr2->pr_path;
3797 if (!strcmp(path1, "/"))
3799 len1 = strlen(path1);
3800 if (strncmp(path1, path2, len1))
3802 if (path2[len1] == '\0')
3804 if (path2[len1] == '/')
3805 return (path2 + len1);
3810 * Jail-related sysctls.
3812 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3816 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3819 struct prison *pr, *cpr;
3821 struct in_addr *ip4 = NULL;
3825 struct in6_addr *ip6 = NULL;
3830 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3831 pr = req->td->td_ucred->cr_prison;
3833 sx_slock(&allprison_lock);
3834 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3835 #if defined(INET) || defined(INET6)
3838 mtx_lock(&cpr->pr_mtx);
3840 if (cpr->pr_ip4s > 0) {
3841 if (ip4s < cpr->pr_ip4s) {
3842 ip4s = cpr->pr_ip4s;
3843 mtx_unlock(&cpr->pr_mtx);
3844 ip4 = realloc(ip4, ip4s *
3845 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3848 bcopy(cpr->pr_ip4, ip4,
3849 cpr->pr_ip4s * sizeof(struct in_addr));
3853 if (cpr->pr_ip6s > 0) {
3854 if (ip6s < cpr->pr_ip6s) {
3855 ip6s = cpr->pr_ip6s;
3856 mtx_unlock(&cpr->pr_mtx);
3857 ip6 = realloc(ip6, ip6s *
3858 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3861 bcopy(cpr->pr_ip6, ip6,
3862 cpr->pr_ip6s * sizeof(struct in6_addr));
3865 bzero(xp, sizeof(*xp));
3866 xp->pr_version = XPRISON_VERSION;
3867 xp->pr_id = cpr->pr_id;
3868 xp->pr_state = cpr->pr_state;
3869 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3870 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3871 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3873 xp->pr_ip4s = cpr->pr_ip4s;
3876 xp->pr_ip6s = cpr->pr_ip6s;
3878 mtx_unlock(&cpr->pr_mtx);
3879 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3883 if (xp->pr_ip4s > 0) {
3884 error = SYSCTL_OUT(req, ip4,
3885 xp->pr_ip4s * sizeof(struct in_addr));
3891 if (xp->pr_ip6s > 0) {
3892 error = SYSCTL_OUT(req, ip6,
3893 xp->pr_ip6s * sizeof(struct in6_addr));
3899 sx_sunlock(&allprison_lock);
3910 SYSCTL_OID(_security_jail, OID_AUTO, list,
3911 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3912 sysctl_jail_list, "S", "List of active jails");
3915 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3919 injail = jailed(req->td->td_ucred);
3920 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3925 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3926 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3927 sysctl_jail_jailed, "I", "Process in jail?");
3930 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3932 int error, havevnet;
3934 struct ucred *cred = req->td->td_ucred;
3936 havevnet = jailed(cred) && prison_owns_vnet(cred);
3940 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3945 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3946 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3947 sysctl_jail_vnet, "I", "Jail owns vnet?");
3949 #if defined(INET) || defined(INET6)
3950 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3951 &jail_max_af_ips, 0,
3952 "Number of IP addresses a jail may have at most per address family (deprecated)");
3956 * Default parameters for jail(2) compatibility. For historical reasons,
3957 * the sysctl names have varying similarity to the parameter names. Prisons
3958 * just see their own parameters, and can't change them.
3961 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3965 /* Get the current flag value, and convert it to a boolean. */
3966 if (req->td->td_ucred->cr_prison == &prison0) {
3967 mtx_lock(&prison0.pr_mtx);
3968 i = (jail_default_allow & arg2) != 0;
3969 mtx_unlock(&prison0.pr_mtx);
3971 i = prison_allow(req->td->td_ucred, arg2);
3975 error = sysctl_handle_int(oidp, &i, 0, req);
3976 if (error || !req->newptr)
3982 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3985 mtx_lock(&prison0.pr_mtx);
3986 jail_default_allow = (jail_default_allow & ~arg2) | i;
3987 mtx_unlock(&prison0.pr_mtx);
3991 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3992 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3993 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3994 "Processes in jail can set their hostnames (deprecated)");
3995 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3996 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3997 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3998 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
3999 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4000 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4001 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4002 "Processes in jail can use System V IPC primitives (deprecated)");
4003 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4004 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4005 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4006 "Prison root can create raw sockets (deprecated)");
4007 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4008 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4009 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4010 "Processes in jail can alter system file flags (deprecated)");
4011 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4012 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4013 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4014 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4017 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4022 pr = req->td->td_ucred->cr_prison;
4023 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4024 error = sysctl_handle_int(oidp, &level, 0, req);
4025 if (error || !req->newptr)
4027 *(int *)arg1 = level;
4031 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4032 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4033 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4034 sysctl_jail_default_level, "I",
4035 "Processes in jail cannot see all mounted file systems (deprecated)");
4037 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4038 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4039 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4040 sysctl_jail_default_level, "I",
4041 "Ruleset for the devfs filesystem in jail (deprecated)");
4044 * Nodes to describe jail parameters. Maximum length of string parameters
4045 * is returned in the string itself, and the other parameters exist merely
4046 * to make themselves and their types known.
4048 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4052 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4059 switch (oidp->oid_kind & CTLTYPE)
4065 if (!(req->flags & SCTL_MASK32))
4067 return (SYSCTL_OUT(req, &l, sizeof(l)));
4071 return (SYSCTL_OUT(req, &i, sizeof(i)));
4072 case CTLTYPE_STRING:
4073 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4075 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4076 case CTLTYPE_STRUCT:
4078 return (SYSCTL_OUT(req, &s, sizeof(s)));
4084 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4085 * jail creation time but cannot be changed in an existing jail.
4087 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4088 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4089 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4090 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4091 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4092 "I", "Jail secure level");
4093 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4094 "Jail value for kern.osreldate and uname -K");
4095 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4096 "Jail value for kern.osrelease and uname -r");
4097 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4098 "I", "Jail cannot see all mounted file systems");
4099 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4100 "I", "Ruleset for in-jail devfs mounts");
4101 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4102 "B", "Jail persistence");
4104 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4105 "E,jailsys", "Virtual network stack");
4107 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4108 "B", "Jail is in the process of shutting down");
4110 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4111 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4112 "I", "Current number of child jails");
4113 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4114 "I", "Maximum number of child jails");
4116 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4117 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4119 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4120 "Jail NIS domainname");
4121 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4123 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4124 "LU", "Jail host ID");
4126 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4127 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4130 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4131 "Jail IPv4 address virtualization");
4132 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4133 "S,in_addr,a", "Jail IPv4 addresses");
4134 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4135 "B", "Do (not) use IPv4 source address selection rather than the "
4136 "primary jail IPv4 address.");
4139 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4140 "Jail IPv6 address virtualization");
4141 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4142 "S,in6_addr,a", "Jail IPv6 addresses");
4143 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4144 "B", "Do (not) use IPv6 source address selection rather than the "
4145 "primary jail IPv6 address.");
4148 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4149 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4150 "B", "Jail may set hostname");
4151 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4152 "B", "Jail may use SYSV IPC");
4153 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4154 "B", "Jail may create raw sockets");
4155 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4156 "B", "Jail may alter system file flags");
4157 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4158 "B", "Jail may set file quotas");
4159 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4160 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4161 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4162 "B", "Jail may lock (unlock) physical pages in memory");
4163 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4164 "B", "Jail may bind sockets to reserved ports");
4165 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4166 "B", "Jail may read the kernel message buffer");
4167 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4168 "B", "Unprivileged processes may use process debugging facilities");
4169 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4170 "B", "Processes in jail with uid 0 have privilege");
4172 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4173 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4174 "B", "Jail may mount/unmount jail-friendly file systems in general");
4177 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4178 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4182 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4185 struct bool_flags *bf;
4186 struct sysctl_oid *parent;
4187 char *allow_name, *allow_noname, *allowed;
4188 #ifndef NO_SYSCTL_DESCR
4189 char *descr_deprecated;
4194 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4196 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4198 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4199 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4200 free(allow_name, M_PRISON);
4205 * See if this parameter has already beed added, i.e. a module was
4206 * previously loaded/unloaded.
4208 mtx_lock(&prison0.pr_mtx);
4209 for (bf = pr_flag_allow;
4210 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4211 atomic_load_int(&bf->flag) != 0;
4213 if (strcmp(bf->name, allow_name) == 0) {
4214 allow_flag = bf->flag;
4220 * Find a free bit in pr_allow_all, failing if there are none
4221 * (which shouldn't happen as long as we keep track of how many
4222 * potential dynamic flags exist).
4224 for (allow_flag = 1;; allow_flag <<= 1) {
4225 if (allow_flag == 0)
4227 if ((pr_allow_all & allow_flag) == 0)
4231 /* Note the parameter in the next open slot in pr_flag_allow. */
4232 for (bf = pr_flag_allow; ; bf++) {
4233 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4234 /* This should never happen, but is not fatal. */
4238 if (atomic_load_int(&bf->flag) == 0)
4241 bf->name = allow_name;
4242 bf->noname = allow_noname;
4243 pr_allow_all |= allow_flag;
4245 * prison0 always has permission for the new parameter.
4246 * Other jails must have it granted to them.
4248 prison0.pr_allow |= allow_flag;
4249 /* The flag indicates a valid entry, so make sure it is set last. */
4250 atomic_store_rel_int(&bf->flag, allow_flag);
4251 mtx_unlock(&prison0.pr_mtx);
4254 * Create sysctls for the paramter, and the back-compat global
4258 ? SYSCTL_ADD_NODE(NULL,
4259 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4260 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4261 : &sysctl___security_jail_param_allow;
4262 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4263 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4264 NULL, 0, sysctl_jail_param, "B", descr);
4266 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4267 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4268 #ifndef NO_SYSCTL_DESCR
4269 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4272 (void)SYSCTL_ADD_PROC(NULL,
4273 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4274 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4275 sysctl_jail_default_allow, "I", descr_deprecated);
4276 #ifndef NO_SYSCTL_DESCR
4277 free(descr_deprecated, M_TEMP);
4279 free(allowed, M_TEMP);
4284 mtx_unlock(&prison0.pr_mtx);
4285 free(allow_name, M_PRISON);
4286 free(allow_noname, M_PRISON);
4291 * The VFS system will register jail-aware filesystems here. They each get
4292 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4293 * attempts to mount.
4296 prison_add_vfs(struct vfsconf *vfsp)
4298 #ifdef NO_SYSCTL_DESCR
4300 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4305 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4307 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4309 free(descr, M_TEMP);
4315 prison_racct_foreach(void (*callback)(struct racct *racct,
4316 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4317 void *arg2, void *arg3)
4319 struct prison_racct *prr;
4321 ASSERT_RACCT_ENABLED();
4323 sx_slock(&allprison_lock);
4326 LIST_FOREACH(prr, &allprison_racct, prr_next)
4327 (callback)(prr->prr_racct, arg2, arg3);
4330 sx_sunlock(&allprison_lock);
4333 static struct prison_racct *
4334 prison_racct_find_locked(const char *name)
4336 struct prison_racct *prr;
4338 ASSERT_RACCT_ENABLED();
4339 sx_assert(&allprison_lock, SA_XLOCKED);
4341 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4344 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4345 if (strcmp(name, prr->prr_name) != 0)
4348 /* Found prison_racct with a matching name? */
4349 prison_racct_hold(prr);
4353 /* Add new prison_racct. */
4354 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4355 racct_create(&prr->prr_racct);
4357 strcpy(prr->prr_name, name);
4358 refcount_init(&prr->prr_refcount, 1);
4359 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4364 struct prison_racct *
4365 prison_racct_find(const char *name)
4367 struct prison_racct *prr;
4369 ASSERT_RACCT_ENABLED();
4371 sx_xlock(&allprison_lock);
4372 prr = prison_racct_find_locked(name);
4373 sx_xunlock(&allprison_lock);
4378 prison_racct_hold(struct prison_racct *prr)
4381 ASSERT_RACCT_ENABLED();
4383 refcount_acquire(&prr->prr_refcount);
4387 prison_racct_free_locked(struct prison_racct *prr)
4390 ASSERT_RACCT_ENABLED();
4391 sx_assert(&allprison_lock, SA_XLOCKED);
4393 if (refcount_release(&prr->prr_refcount)) {
4394 racct_destroy(&prr->prr_racct);
4395 LIST_REMOVE(prr, prr_next);
4396 free(prr, M_PRISON_RACCT);
4401 prison_racct_free(struct prison_racct *prr)
4404 ASSERT_RACCT_ENABLED();
4405 sx_assert(&allprison_lock, SA_UNLOCKED);
4407 if (refcount_release_if_not_last(&prr->prr_refcount))
4410 sx_xlock(&allprison_lock);
4411 prison_racct_free_locked(prr);
4412 sx_xunlock(&allprison_lock);
4416 prison_racct_attach(struct prison *pr)
4418 struct prison_racct *prr;
4420 ASSERT_RACCT_ENABLED();
4421 sx_assert(&allprison_lock, SA_XLOCKED);
4423 prr = prison_racct_find_locked(pr->pr_name);
4424 KASSERT(prr != NULL, ("cannot find prison_racct"));
4426 pr->pr_prison_racct = prr;
4430 * Handle jail renaming. From the racct point of view, renaming means
4431 * moving from one prison_racct to another.
4434 prison_racct_modify(struct prison *pr)
4440 struct prison_racct *oldprr;
4442 ASSERT_RACCT_ENABLED();
4444 sx_slock(&allproc_lock);
4445 sx_xlock(&allprison_lock);
4447 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4448 sx_xunlock(&allprison_lock);
4449 sx_sunlock(&allproc_lock);
4453 oldprr = pr->pr_prison_racct;
4454 pr->pr_prison_racct = NULL;
4456 prison_racct_attach(pr);
4459 * Move resource utilisation records.
4461 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4465 * Force rctl to reattach rules to processes.
4467 FOREACH_PROC_IN_SYSTEM(p) {
4469 cred = crhold(p->p_ucred);
4471 rctl_proc_ucred_changed(p, cred);
4476 sx_sunlock(&allproc_lock);
4477 prison_racct_free_locked(oldprr);
4478 sx_xunlock(&allprison_lock);
4482 prison_racct_detach(struct prison *pr)
4485 ASSERT_RACCT_ENABLED();
4486 sx_assert(&allprison_lock, SA_UNLOCKED);
4488 if (pr->pr_prison_racct == NULL)
4490 prison_racct_free(pr->pr_prison_racct);
4491 pr->pr_prison_racct = NULL;
4498 db_show_prison(struct prison *pr)
4500 struct bool_flags *bf;
4501 struct jailsys_flags *jsf;
4502 #if defined(INET) || defined(INET6)
4507 char ip4buf[INET_ADDRSTRLEN];
4510 char ip6buf[INET6_ADDRSTRLEN];
4513 db_printf("prison %p:\n", pr);
4514 db_printf(" jid = %d\n", pr->pr_id);
4515 db_printf(" name = %s\n", pr->pr_name);
4516 db_printf(" parent = %p\n", pr->pr_parent);
4517 db_printf(" ref = %d\n", pr->pr_ref);
4518 db_printf(" uref = %d\n", pr->pr_uref);
4519 db_printf(" state = %s\n",
4520 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4521 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4523 db_printf(" path = %s\n", pr->pr_path);
4524 db_printf(" cpuset = %d\n", pr->pr_cpuset
4525 ? pr->pr_cpuset->cs_id : -1);
4527 db_printf(" vnet = %p\n", pr->pr_vnet);
4529 db_printf(" root = %p\n", pr->pr_root);
4530 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4531 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4532 db_printf(" children.max = %d\n", pr->pr_childmax);
4533 db_printf(" children.cur = %d\n", pr->pr_childcount);
4534 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4535 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4536 db_printf(" flags = 0x%x", pr->pr_flags);
4537 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4538 if (pr->pr_flags & bf->flag)
4539 db_printf(" %s", bf->name);
4540 for (jsf = pr_flag_jailsys;
4541 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4543 f = pr->pr_flags & (jsf->disable | jsf->new);
4544 db_printf(" %-16s= %s\n", jsf->name,
4545 (f != 0 && f == jsf->disable) ? "disable"
4546 : (f == jsf->new) ? "new"
4549 db_printf(" allow = 0x%x", pr->pr_allow);
4550 for (bf = pr_flag_allow;
4551 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4552 atomic_load_int(&bf->flag) != 0;
4554 if (pr->pr_allow & bf->flag)
4555 db_printf(" %s", bf->name);
4557 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4558 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4559 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4560 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4561 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4563 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4564 for (ii = 0; ii < pr->pr_ip4s; ii++)
4565 db_printf(" %s %s\n",
4566 ii == 0 ? "ip4.addr =" : " ",
4567 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4570 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4571 for (ii = 0; ii < pr->pr_ip6s; ii++)
4572 db_printf(" %s %s\n",
4573 ii == 0 ? "ip6.addr =" : " ",
4574 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4578 DB_SHOW_COMMAND(prison, db_show_prison_command)
4584 * Show all prisons in the list, and prison0 which is not
4587 db_show_prison(&prison0);
4588 if (!db_pager_quit) {
4589 TAILQ_FOREACH(pr, &allprison, pr_list) {
4601 /* Look for a prison with the ID and with references. */
4602 TAILQ_FOREACH(pr, &allprison, pr_list)
4603 if (pr->pr_id == addr && pr->pr_ref > 0)
4606 /* Look again, without requiring a reference. */
4607 TAILQ_FOREACH(pr, &allprison, pr_list)
4608 if (pr->pr_id == addr)
4611 /* Assume address points to a valid prison. */
4612 pr = (struct prison *)addr;