2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2008 Bjoern A. Zeeb.
6 * Copyright (c) 2009 James Gritton.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/sysproto.h>
44 #include <sys/malloc.h>
48 #include <sys/taskqueue.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/mutex.h>
54 #include <sys/racct.h>
56 #include <sys/refcount.h>
58 #include <sys/sysent.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
71 #include <netinet/in.h>
77 #include <security/mac/mac_framework.h>
79 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
80 #define PRISON0_HOSTUUID_MODULE "hostuuid"
82 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
83 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
85 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
88 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
90 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
94 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
96 #define _PR_IP_SADDRSEL 0
100 /* prison0 describes what is "real" about the system. */
101 struct prison prison0 = {
107 .pr_securelevel = -1,
109 .pr_state = PRISON_STATE_ALIVE,
110 .pr_childmax = JAIL_MAX,
111 .pr_hostuuid = DEFAULT_HOSTUUID,
112 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
114 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
116 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
118 .pr_allow = PR_ALLOW_ALL_STATIC,
120 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
127 struct jailsys_flags {
133 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
134 struct sx allprison_lock;
135 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
136 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
137 LIST_HEAD(, prison_racct) allprison_racct;
140 static int get_next_prid(struct prison **insprp);
141 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
142 static void prison_complete(void *context, int pending);
143 static void prison_deref(struct prison *pr, int flags);
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_set_allow_locked(struct prison *pr, unsigned flag,
148 static char *prison_path(struct prison *pr1, struct prison *pr2);
149 static void prison_remove_one(struct prison *pr);
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_LOCKED 0x04 /* pr_mtx is held */
160 #define PD_LIST_SLOCKED 0x08 /* allprison_lock is held shared */
161 #define PD_LIST_XLOCKED 0x10 /* allprison_lock is held exclusive */
164 * Parameter names corresponding to PR_* flag values. Size values are for kvm
165 * as we cannot figure out the size of a sparse array, or an array without a
168 static struct bool_flags pr_flag_bool[] = {
169 {"persist", "nopersist", PR_PERSIST},
171 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
174 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
177 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
179 static struct jailsys_flags pr_flag_jailsys[] = {
180 {"host", 0, PR_HOST},
182 {"vnet", 0, PR_VNET},
185 {"ip4", PR_IP4_USER, PR_IP4_USER},
188 {"ip6", PR_IP6_USER, PR_IP6_USER},
191 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
194 * Make this array full-size so dynamic parameters can be added.
195 * It is protected by prison0.mtx, but lockless reading is allowed
196 * with an atomic check of the flag values.
198 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
199 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
200 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
201 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
202 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
203 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
204 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
205 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
206 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
207 {"allow.reserved_ports", "allow.noreserved_ports",
208 PR_ALLOW_RESERVED_PORTS},
209 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
210 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
211 PR_ALLOW_UNPRIV_DEBUG},
212 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
214 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
215 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
217 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
218 PR_ALLOW_RESERVED_PORTS | \
219 PR_ALLOW_UNPRIV_DEBUG | \
221 #define JAIL_DEFAULT_ENFORCE_STATFS 2
222 #define JAIL_DEFAULT_DEVFS_RSNUM 0
223 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
224 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
225 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
226 #if defined(INET) || defined(INET6)
227 static unsigned jail_max_af_ips = 255;
231 * Initialize the parts of prison0 that can't be static-initialized with
232 * constants. This is called from proc0_init() after creating thread0 cpuset.
237 uint8_t *file, *data;
240 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
241 prison0.pr_osreldate = osreldate;
242 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
244 /* If we have a preloaded hostuuid, use it. */
245 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
247 data = preload_fetch_addr(file);
248 size = preload_fetch_size(file);
251 * The preloaded data may include trailing whitespace, almost
252 * certainly a newline; skip over any whitespace or
253 * non-printable characters to be safe.
255 while (size > 0 && data[size - 1] <= 0x20) {
258 if (validate_uuid(data, size, NULL, 0) == 0) {
259 (void)strlcpy(prison0.pr_hostuuid, data,
261 } else if (bootverbose) {
262 printf("hostuuid: preload data malformed: '%s'",
268 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
277 sys_jail(struct thread *td, struct jail_args *uap)
283 error = copyin(uap->jail, &version, sizeof(uint32_t));
292 /* FreeBSD single IPv4 jails. */
293 bzero(&j, sizeof(struct jail));
294 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
297 j.version = j0.version;
299 j.hostname = j0.hostname;
300 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
306 * Version 1 was used by multi-IPv4 jail implementations
307 * that never made it into the official kernel.
311 case 2: /* JAIL_API_VERSION */
312 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
313 error = copyin(uap->jail, &j, sizeof(struct jail));
319 /* Sci-Fi jails are not supported, sorry. */
322 return (kern_jail(td, &j));
326 kern_jail(struct thread *td, struct jail *j)
328 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
337 char *u_path, *u_hostname, *u_name;
338 struct bool_flags *bf;
341 struct in_addr *u_ip4;
344 struct in6_addr *u_ip6;
347 int error, enforce_statfs;
349 bzero(&optiov, sizeof(optiov));
350 opt.uio_iov = optiov;
354 opt.uio_segflg = UIO_SYSSPACE;
355 opt.uio_rw = UIO_READ;
358 /* Set permissions for top-level jails from sysctls. */
359 if (!jailed(td->td_ucred)) {
360 for (bf = pr_flag_allow;
361 bf < pr_flag_allow + nitems(pr_flag_allow) &&
362 atomic_load_int(&bf->flag) != 0;
364 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
365 (jail_default_allow & bf->flag)
366 ? bf->name : bf->noname);
367 optiov[opt.uio_iovcnt].iov_len =
368 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
371 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
372 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
374 enforce_statfs = jail_default_enforce_statfs;
375 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
376 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
380 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
382 ip4s = (j->version == 0) ? 1 : j->ip4s;
383 if (ip4s > jail_max_af_ips)
385 tmplen += ip4s * sizeof(struct in_addr);
391 if (j->ip6s > jail_max_af_ips)
393 tmplen += j->ip6s * sizeof(struct in6_addr);
398 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
399 u_hostname = u_path + MAXPATHLEN;
400 u_name = u_hostname + MAXHOSTNAMELEN;
402 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
406 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
408 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
411 optiov[opt.uio_iovcnt].iov_base = "path";
412 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
414 optiov[opt.uio_iovcnt].iov_base = u_path;
415 error = copyinstr(j->path, u_path, MAXPATHLEN,
416 &optiov[opt.uio_iovcnt].iov_len);
418 free(u_path, M_TEMP);
422 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
423 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
425 optiov[opt.uio_iovcnt].iov_base = u_hostname;
426 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
427 &optiov[opt.uio_iovcnt].iov_len);
429 free(u_path, M_TEMP);
433 if (j->jailname != NULL) {
434 optiov[opt.uio_iovcnt].iov_base = "name";
435 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
437 optiov[opt.uio_iovcnt].iov_base = u_name;
438 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
439 &optiov[opt.uio_iovcnt].iov_len);
441 free(u_path, M_TEMP);
447 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
448 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
450 optiov[opt.uio_iovcnt].iov_base = u_ip4;
451 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
453 u_ip4->s_addr = j->ip4s;
455 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
457 free(u_path, M_TEMP);
464 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
465 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
467 optiov[opt.uio_iovcnt].iov_base = u_ip6;
468 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
469 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
471 free(u_path, M_TEMP);
476 KASSERT(opt.uio_iovcnt <= nitems(optiov),
477 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
478 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
479 free(u_path, M_TEMP);
484 * struct jail_set_args {
485 * struct iovec *iovp;
486 * unsigned int iovcnt;
491 sys_jail_set(struct thread *td, struct jail_set_args *uap)
496 /* Check that we have an even number of iovecs. */
500 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
503 error = kern_jail_set(td, auio, uap->flags);
509 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
516 struct in6_addr *ip6;
519 struct vfsoptlist *opts;
520 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
522 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
523 char *g_path, *osrelstr;
524 struct bool_flags *bf;
525 struct jailsys_flags *jsf;
526 #if defined(INET) || defined(INET6)
531 size_t namelen, onamelen, pnamelen;
532 int born, created, cuflags, descend, drflags, enforce;
533 int error, errmsg_len, errmsg_pos;
534 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
535 int jid, jsys, len, level;
536 int childmax, osreldt, rsnum, slevel;
537 #if defined(INET) || defined(INET6)
546 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
547 uint64_t pr_allow_diff;
551 error = priv_check(td, PRIV_JAIL_SET);
552 if (!error && (flags & JAIL_ATTACH))
553 error = priv_check(td, PRIV_JAIL_ATTACH);
556 mypr = td->td_ucred->cr_prison;
557 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
559 if (flags & ~JAIL_SET_MASK)
563 * Check all the parameters before committing to anything. Not all
564 * errors can be caught early, but we may as well try. Also, this
565 * takes care of some expensive stuff (path lookup) before getting
566 * the allprison lock.
568 * XXX Jails are not filesystems, and jail parameters are not mount
569 * options. But it makes more sense to re-use the vfsopt code
570 * than duplicate it under a different name.
572 error = vfs_buildopts(optuio, &opts);
583 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
586 vfs_opterror(opts, "no valid operation (create or update)");
590 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
596 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
605 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
613 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
618 else if (enforce < 0 || enforce > 2) {
624 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
632 pr_flags = ch_flags = 0;
633 for (bf = pr_flag_bool;
634 bf < pr_flag_bool + nitems(pr_flag_bool);
636 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
637 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
639 ch_flags |= pr_flags;
640 for (jsf = pr_flag_jailsys;
641 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
643 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
649 case JAIL_SYS_DISABLE:
654 pr_flags |= jsf->disable;
657 pr_flags |= jsf->new;
659 case JAIL_SYS_INHERIT:
665 ch_flags |= jsf->new | jsf->disable;
667 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
668 && !(pr_flags & PR_PERSIST)) {
670 vfs_opterror(opts, "new jail must persist or attach");
674 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
676 vfs_opterror(opts, "vnet cannot be changed after creation");
681 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
683 vfs_opterror(opts, "ip4 cannot be changed after creation");
688 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
690 vfs_opterror(opts, "ip6 cannot be changed after creation");
695 pr_allow = ch_allow = 0;
696 for (bf = pr_flag_allow;
697 bf < pr_flag_allow + nitems(pr_flag_allow) &&
698 atomic_load_int(&bf->flag) != 0;
700 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
701 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
703 ch_allow |= pr_allow;
705 error = vfs_getopt(opts, "name", (void **)&name, &len);
711 if (len == 0 || name[len - 1] != '\0') {
715 if (len > MAXHOSTNAMELEN) {
716 error = ENAMETOOLONG;
721 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
729 if (len == 0 || host[len - 1] != '\0') {
733 if (len > MAXHOSTNAMELEN) {
734 error = ENAMETOOLONG;
739 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
747 if (len == 0 || domain[len - 1] != '\0') {
751 if (len > MAXHOSTNAMELEN) {
752 error = ENAMETOOLONG;
757 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
765 if (len == 0 || uuid[len - 1] != '\0') {
769 if (len > HOSTUUIDLEN) {
770 error = ENAMETOOLONG;
775 #ifdef COMPAT_FREEBSD32
776 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
779 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
783 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
795 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
800 else if (ip4s & (sizeof(*ip4) - 1)) {
804 ch_flags |= PR_IP4_USER;
805 pr_flags |= PR_IP4_USER;
807 ip4s /= sizeof(*ip4);
808 if (ip4s > jail_max_af_ips) {
810 vfs_opterror(opts, "too many IPv4 addresses");
813 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
814 bcopy(op, ip4, ip4s * sizeof(*ip4));
816 * IP addresses are all sorted but ip[0] to preserve
817 * the primary IP address as given from userland.
818 * This special IP is used for unbound outgoing
819 * connections as well for "loopback" traffic in case
820 * source address selection cannot find any more fitting
821 * address to connect from.
824 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
827 * Check for duplicate addresses and do some simple
828 * zero and broadcast checks. If users give other bogus
829 * addresses it is their problem.
831 * We do not have to care about byte order for these
832 * checks so we will do them in NBO.
834 for (ii = 0; ii < ip4s; ii++) {
835 if (ip4[ii].s_addr == INADDR_ANY ||
836 ip4[ii].s_addr == INADDR_BROADCAST) {
841 (ip4[0].s_addr == ip4[ii+1].s_addr ||
842 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
852 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
857 else if (ip6s & (sizeof(*ip6) - 1)) {
861 ch_flags |= PR_IP6_USER;
862 pr_flags |= PR_IP6_USER;
864 ip6s /= sizeof(*ip6);
865 if (ip6s > jail_max_af_ips) {
867 vfs_opterror(opts, "too many IPv6 addresses");
870 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
871 bcopy(op, ip6, ip6s * sizeof(*ip6));
873 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
875 for (ii = 0; ii < ip6s; ii++) {
876 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
881 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
882 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
892 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
893 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
896 "vnet jails cannot have IP address restrictions");
901 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
907 if (flags & JAIL_UPDATE) {
910 "osrelease cannot be changed after creation");
913 if (len == 0 || osrelstr[len - 1] != '\0') {
917 if (len >= OSRELEASELEN) {
918 error = ENAMETOOLONG;
920 "osrelease string must be 1-%d bytes long",
926 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
932 if (flags & JAIL_UPDATE) {
935 "osreldate cannot be changed after creation");
940 vfs_opterror(opts, "osreldate cannot be 0");
946 error = vfs_getopt(opts, "path", (void **)&path, &len);
952 if (flags & JAIL_UPDATE) {
955 "path cannot be changed after creation");
958 if (len == 0 || path[len - 1] != '\0') {
962 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
968 NDFREE(&nd, NDF_ONLY_PNBUF);
969 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
970 strlcpy(g_path, path, MAXPATHLEN);
971 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
975 /* exit on other errors */
978 if (root->v_type != VDIR) {
987 * Find the specified jail, or at least its parent.
988 * This abuses the file error codes ENOENT and EEXIST.
993 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
994 namelc = strrchr(name, '.');
995 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
999 sx_xlock(&allprison_lock);
1000 drflags = PD_LIST_XLOCKED;
1004 vfs_opterror(opts, "negative jid");
1008 * See if a requested jid already exists. Keep track of
1009 * where it can be inserted later.
1011 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1012 if (inspr->pr_id < jid)
1014 if (inspr->pr_id > jid)
1017 mtx_lock(&pr->pr_mtx);
1018 drflags |= PD_LOCKED;
1023 ppr = pr->pr_parent;
1024 /* Create: jid must not exist. */
1025 if (cuflags == JAIL_CREATE) {
1027 * Even creators that cannot see the jail will
1031 vfs_opterror(opts, "jail %d already exists",
1035 if (!prison_ischild(mypr, pr)) {
1037 * Updaters get ENOENT if they cannot see the
1038 * jail. This is true even for CREATE | UPDATE,
1039 * which normally cannot give this error.
1042 vfs_opterror(opts, "jail %d not found", jid);
1045 if (!prison_isalive(pr)) {
1046 if (!(flags & JAIL_DYING)) {
1048 vfs_opterror(opts, "jail %d is dying",
1052 if ((flags & JAIL_ATTACH) ||
1053 (pr_flags & PR_PERSIST)) {
1055 * A dying jail might be resurrected
1056 * (via attach or persist), but first
1057 * it must determine if another jail
1058 * has claimed its name. Accomplish
1059 * this by implicitly re-setting the
1063 name = prison_name(mypr, pr);
1067 /* Update: jid must exist. */
1068 if (cuflags == JAIL_UPDATE) {
1070 vfs_opterror(opts, "jail %d not found", jid);
1076 * If the caller provided a name, look for a jail by that name.
1077 * This has different semantics for creates and updates keyed by jid
1078 * (where the name must not already exist in a different jail),
1079 * and updates keyed by the name itself (where the name must exist
1080 * because that is the jail being updated).
1084 namelc = strrchr(name, '.');
1089 * This is a hierarchical name. Split it into the
1090 * parent and child names, and make sure the parent
1091 * exists or matches an already found jail.
1094 if (strncmp(name, ppr->pr_name, namelc - name)
1095 || ppr->pr_name[namelc - name] != '\0') {
1098 "cannot change jail's parent");
1103 ppr = prison_find_name(mypr, name);
1107 "jail \"%s\" not found", name);
1110 if (!(flags & JAIL_DYING) &&
1111 !prison_isalive(ppr)) {
1112 mtx_unlock(&ppr->pr_mtx);
1115 "jail \"%s\" is dying", name);
1118 mtx_unlock(&ppr->pr_mtx);
1123 if (namelc[0] != '\0') {
1125 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1127 FOREACH_PRISON_CHILD(ppr, tpr) {
1129 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1130 if (prison_isalive(tpr)) {
1132 cuflags != JAIL_CREATE) {
1138 mtx_lock(&pr->pr_mtx);
1139 drflags |= PD_LOCKED;
1143 * Create, or update(jid):
1144 * name must not exist in an
1145 * active sibling jail.
1149 "jail \"%s\" already exists",
1154 cuflags != JAIL_CREATE) {
1159 /* If no active jail is found, use a dying one. */
1160 if (deadpr != NULL && pr == NULL) {
1161 if (flags & JAIL_DYING) {
1163 mtx_lock(&pr->pr_mtx);
1164 drflags |= PD_LOCKED;
1165 } else if (cuflags == JAIL_UPDATE) {
1168 "jail \"%s\" is dying", name);
1172 /* Update: name must exist if no jid. */
1173 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1175 vfs_opterror(opts, "jail \"%s\" not found",
1181 /* Update: must provide a jid or name. */
1182 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1184 vfs_opterror(opts, "update specified no jail");
1188 /* If there's no prison to update, create a new one and link it in. */
1189 created = pr == NULL;
1191 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1192 if (tpr->pr_childcount >= tpr->pr_childmax) {
1194 vfs_opterror(opts, "prison limit exceeded");
1198 if (!refcount_acquire_if_not_zero(&ppr->pr_uref)) {
1199 /* This brings the parent back to life. */
1200 mtx_lock(&ppr->pr_mtx);
1201 refcount_acquire(&ppr->pr_uref);
1202 ppr->pr_state = PRISON_STATE_ALIVE;
1203 mtx_unlock(&ppr->pr_mtx);
1204 error = osd_jail_call(ppr, PR_METHOD_CREATE, opts);
1207 drflags |= PD_DEREF | PD_DEUREF;
1212 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1214 vfs_opterror(opts, "no available jail IDs");
1216 drflags |= PD_DEREF | PD_DEUREF;
1220 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1221 pr->pr_state = PRISON_STATE_INVALID;
1222 refcount_init(&pr->pr_ref, 1);
1223 refcount_init(&pr->pr_uref, 0);
1224 drflags |= PD_DEREF;
1225 LIST_INIT(&pr->pr_children);
1226 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1227 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1231 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1233 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1235 pr->pr_parent = ppr;
1236 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1237 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1238 tpr->pr_childcount++;
1240 /* Set some default values, and inherit some from the parent. */
1245 root = mypr->pr_root;
1248 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1249 pr->pr_flags |= PR_HOST;
1250 #if defined(INET) || defined(INET6)
1252 if (!(pr_flags & PR_VNET))
1256 if (!(ch_flags & PR_IP4_USER))
1257 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1258 else if (!(pr_flags & PR_IP4_USER)) {
1259 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1260 if (ppr->pr_ip4 != NULL) {
1261 pr->pr_ip4s = ppr->pr_ip4s;
1262 pr->pr_ip4 = malloc(pr->pr_ip4s *
1263 sizeof(struct in_addr), M_PRISON,
1265 bcopy(ppr->pr_ip4, pr->pr_ip4,
1266 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1271 if (!(ch_flags & PR_IP6_USER))
1272 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1273 else if (!(pr_flags & PR_IP6_USER)) {
1274 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1275 if (ppr->pr_ip6 != NULL) {
1276 pr->pr_ip6s = ppr->pr_ip6s;
1277 pr->pr_ip6 = malloc(pr->pr_ip6s *
1278 sizeof(struct in6_addr), M_PRISON,
1280 bcopy(ppr->pr_ip6, pr->pr_ip6,
1281 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1287 /* Source address selection is always on by default. */
1288 pr->pr_flags |= _PR_IP_SADDRSEL;
1290 pr->pr_securelevel = ppr->pr_securelevel;
1291 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1292 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1293 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1295 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1296 if (osrelstr == NULL)
1297 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1298 sizeof(pr->pr_osrelease));
1300 strlcpy(pr->pr_osrelease, osrelstr,
1301 sizeof(pr->pr_osrelease));
1304 /* Allocate a new vnet if specified. */
1305 pr->pr_vnet = (pr_flags & PR_VNET)
1306 ? vnet_alloc() : ppr->pr_vnet;
1309 * Allocate a dedicated cpuset for each jail.
1310 * Unlike other initial settings, this may return an erorr.
1312 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1316 mtx_lock(&pr->pr_mtx);
1317 drflags |= PD_LOCKED;
1320 * Grab a reference for existing prisons, to ensure they
1321 * continue to exist for the duration of the call.
1324 drflags |= PD_DEREF;
1325 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1326 if ((pr->pr_flags & PR_VNET) &&
1327 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1330 "vnet jails cannot have IP address restrictions");
1335 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1338 "ip4 cannot be changed after creation");
1343 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1346 "ip6 cannot be changed after creation");
1352 /* Do final error checking before setting anything. */
1354 if (slevel < ppr->pr_securelevel) {
1360 if (childmax >= ppr->pr_childmax) {
1366 if (enforce < ppr->pr_enforce_statfs) {
1373 * devfs_rsnum is a uint16_t
1375 if (rsnum < 0 || rsnum > 65535) {
1380 * Nested jails always inherit parent's devfs ruleset
1382 if (jailed(td->td_ucred)) {
1383 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1387 rsnum = ppr->pr_devfs_rsnum;
1392 if (ppr->pr_flags & PR_IP4) {
1394 * Make sure the new set of IP addresses is a
1395 * subset of the parent's list. Don't worry
1396 * about the parent being unlocked, as any
1397 * setting is done with allprison_lock held.
1399 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1400 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1402 if (ij == ppr->pr_ip4s) {
1407 for (ii = ij = 1; ii < ip4s; ii++) {
1408 if (ip4[ii].s_addr ==
1409 ppr->pr_ip4[0].s_addr)
1411 for (; ij < ppr->pr_ip4s; ij++)
1412 if (ip4[ii].s_addr ==
1413 ppr->pr_ip4[ij].s_addr)
1415 if (ij == ppr->pr_ip4s)
1418 if (ij == ppr->pr_ip4s) {
1425 * Check for conflicting IP addresses. We permit them
1426 * if there is no more than one IP on each jail. If
1427 * there is a duplicate on a jail with more than one
1428 * IP stop checking and return error.
1431 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1432 if (tppr->pr_flags & PR_VNET)
1437 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1440 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1442 !prison_isalive(tpr)) {
1446 if (!(tpr->pr_flags & PR_IP4_USER))
1449 if (tpr->pr_ip4 == NULL ||
1450 (ip4s == 1 && tpr->pr_ip4s == 1))
1452 for (ii = 0; ii < ip4s; ii++) {
1453 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1457 "IPv4 addresses clash");
1466 if (ppr->pr_flags & PR_IP6) {
1468 * Make sure the new set of IP addresses is a
1469 * subset of the parent's list.
1471 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1472 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1475 if (ij == ppr->pr_ip6s) {
1480 for (ii = ij = 1; ii < ip6s; ii++) {
1481 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1484 for (; ij < ppr->pr_ip6s; ij++)
1485 if (IN6_ARE_ADDR_EQUAL(
1486 &ip6[ii], &ppr->pr_ip6[ij]))
1488 if (ij == ppr->pr_ip6s)
1491 if (ij == ppr->pr_ip6s) {
1497 /* Check for conflicting IP addresses. */
1499 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1500 if (tppr->pr_flags & PR_VNET)
1505 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1508 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1510 !prison_isalive(tpr)) {
1514 if (!(tpr->pr_flags & PR_IP6_USER))
1517 if (tpr->pr_ip6 == NULL ||
1518 (ip6s == 1 && tpr->pr_ip6s == 1))
1520 for (ii = 0; ii < ip6s; ii++) {
1521 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1525 "IPv6 addresses clash");
1532 onamelen = namelen = 0;
1533 if (namelc != NULL) {
1534 /* Give a default name of the jid. Also allow the name to be
1535 * explicitly the jid - but not any other number, and only in
1536 * normal form (no leading zero/etc).
1538 if (namelc[0] == '\0')
1539 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1540 else if ((strtoul(namelc, &p, 10) != jid ||
1541 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1544 "name cannot be numeric (unless it is the jid)");
1548 * Make sure the name isn't too long for the prison or its
1551 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1552 onamelen = strlen(pr->pr_name + pnamelen);
1553 namelen = strlen(namelc);
1554 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1555 error = ENAMETOOLONG;
1558 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1559 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1560 sizeof(pr->pr_name)) {
1561 error = ENAMETOOLONG;
1566 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1567 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1573 * Let modules check their parameters. This requires unlocking and
1574 * then re-locking the prison, but this is still a valid state as long
1575 * as allprison_lock remains xlocked.
1577 mtx_unlock(&pr->pr_mtx);
1578 drflags &= ~PD_LOCKED;
1579 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1582 mtx_lock(&pr->pr_mtx);
1583 drflags |= PD_LOCKED;
1585 /* At this point, all valid parameters should have been noted. */
1586 TAILQ_FOREACH(opt, opts, link) {
1587 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1589 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1594 /* Set the parameters of the prison. */
1597 if (pr_flags & PR_IP4_USER) {
1598 pr->pr_flags |= PR_IP4;
1599 free(pr->pr_ip4, M_PRISON);
1603 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1605 if (tpr->pr_flags & PR_VNET) {
1610 if (prison_restrict_ip4(tpr, NULL)) {
1619 if (pr_flags & PR_IP6_USER) {
1620 pr->pr_flags |= PR_IP6;
1621 free(pr->pr_ip6, M_PRISON);
1625 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1627 if (tpr->pr_flags & PR_VNET) {
1632 if (prison_restrict_ip6(tpr, NULL)) {
1640 pr->pr_securelevel = slevel;
1641 /* Set all child jails to be at least this level. */
1642 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1643 if (tpr->pr_securelevel < slevel)
1644 tpr->pr_securelevel = slevel;
1647 pr->pr_childmax = childmax;
1648 /* Set all child jails to under this limit. */
1649 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1650 if (tpr->pr_childmax > childmax - level)
1651 tpr->pr_childmax = childmax > level
1652 ? childmax - level : 0;
1655 pr->pr_enforce_statfs = enforce;
1656 /* Pass this restriction on to the children. */
1657 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1658 if (tpr->pr_enforce_statfs < enforce)
1659 tpr->pr_enforce_statfs = enforce;
1662 pr->pr_devfs_rsnum = rsnum;
1663 /* Pass this restriction on to the children. */
1664 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1665 tpr->pr_devfs_rsnum = rsnum;
1667 if (namelc != NULL) {
1668 if (ppr == &prison0)
1669 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1671 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1672 ppr->pr_name, namelc);
1673 /* Change this component of child names. */
1674 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1675 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1676 strlen(tpr->pr_name + onamelen) + 1);
1677 bcopy(pr->pr_name, tpr->pr_name, namelen);
1681 /* Try to keep a real-rooted full pathname. */
1682 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1686 if (PR_HOST & ch_flags & ~pr_flags) {
1687 if (pr->pr_flags & PR_HOST) {
1689 * Copy the parent's host info. As with pr_ip4 above,
1690 * the lack of a lock on the parent is not a problem;
1691 * it is always set with allprison_lock at least
1692 * shared, and is held exclusively here.
1694 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1695 sizeof(pr->pr_hostname));
1696 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1697 sizeof(pr->pr_domainname));
1698 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1699 sizeof(pr->pr_hostuuid));
1700 pr->pr_hostid = pr->pr_parent->pr_hostid;
1702 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1703 /* Set this prison, and any descendants without PR_HOST. */
1705 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1707 strlcpy(pr->pr_domainname, domain,
1708 sizeof(pr->pr_domainname));
1710 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1712 pr->pr_hostid = hid;
1713 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1714 if (tpr->pr_flags & PR_HOST)
1718 strlcpy(tpr->pr_hostname,
1720 sizeof(tpr->pr_hostname));
1722 strlcpy(tpr->pr_domainname,
1724 sizeof(tpr->pr_domainname));
1726 strlcpy(tpr->pr_hostuuid,
1728 sizeof(tpr->pr_hostuuid));
1730 tpr->pr_hostid = hid;
1734 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1735 if ((tallow = ch_allow & ~pr_allow))
1736 prison_set_allow_locked(pr, tallow, 0);
1738 * Persistent prisons get an extra reference, and prisons losing their
1739 * persist flag lose that reference.
1741 born = !prison_isalive(pr);
1742 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1743 if (pr_flags & PR_PERSIST) {
1746 * This may make a dead prison alive again, but wait
1747 * to label it as such until after OSD calls have had
1748 * a chance to run (and perhaps to fail).
1750 refcount_acquire(&pr->pr_uref);
1752 drflags |= PD_DEUREF;
1753 prison_free_not_last(pr);
1756 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1757 mtx_unlock(&pr->pr_mtx);
1758 drflags &= ~PD_LOCKED;
1761 if (racct_enable && created)
1762 prison_racct_attach(pr);
1765 /* Locks may have prevented a complete restriction of child IP
1766 * addresses. If so, allocate some more memory and try again.
1771 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1772 mtx_lock(&pr->pr_mtx);
1774 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1776 if (tpr->pr_flags & PR_VNET) {
1781 if (prison_restrict_ip4(tpr, ip4)) {
1788 mtx_unlock(&pr->pr_mtx);
1794 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1795 mtx_lock(&pr->pr_mtx);
1797 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1799 if (tpr->pr_flags & PR_VNET) {
1804 if (prison_restrict_ip6(tpr, ip6)) {
1811 mtx_unlock(&pr->pr_mtx);
1815 /* Let the modules do their work. */
1817 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1819 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1823 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1826 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
1831 * A new prison is now ready to be seen; either it has gained a user
1832 * reference via persistence, or is about to gain one via attachment.
1835 drflags = prison_lock_xlock(pr, drflags);
1836 pr->pr_state = PRISON_STATE_ALIVE;
1839 /* Attach this process to the prison if requested. */
1840 if (flags & JAIL_ATTACH) {
1841 error = do_jail_attach(td, pr, prison_lock_xlock(pr, drflags));
1842 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1844 vfs_opterror(opts, "attach failed");
1850 if (racct_enable && !created) {
1851 if (drflags & PD_LOCKED) {
1852 mtx_unlock(&pr->pr_mtx);
1853 drflags &= ~PD_LOCKED;
1855 if (drflags & PD_LIST_XLOCKED) {
1856 sx_xunlock(&allprison_lock);
1857 drflags &= ~PD_LIST_XLOCKED;
1859 prison_racct_modify(pr);
1863 td->td_retval[0] = pr->pr_id;
1866 /* Release any temporary prison holds and/or locks. */
1868 prison_deref(pr, drflags);
1869 else if (drflags & PD_LIST_SLOCKED)
1870 sx_sunlock(&allprison_lock);
1871 else if (drflags & PD_LIST_XLOCKED)
1872 sx_xunlock(&allprison_lock);
1877 /* Write the error message back to userspace. */
1878 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1879 &errmsg_len) == 0 && errmsg_len > 0) {
1880 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1881 if (optuio->uio_segflg == UIO_SYSSPACE)
1883 optuio->uio_iov[errmsg_pos].iov_base,
1887 optuio->uio_iov[errmsg_pos].iov_base,
1893 free(ip4, M_PRISON);
1896 free(ip6, M_PRISON);
1899 free(g_path, M_TEMP);
1905 * Find the next available prison ID. Return the ID on success, or zero
1906 * on failure. Also set a pointer to the allprison list entry the prison
1907 * should be inserted before.
1910 get_next_prid(struct prison **insprp)
1912 struct prison *inspr;
1915 jid = lastprid % JAIL_MAX + 1;
1916 if (TAILQ_EMPTY(&allprison) ||
1917 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1919 * A common case is for all jails to be implicitly numbered,
1920 * which means they'll go on the end of the list, at least
1921 * for the first JAIL_MAX times.
1926 * Take two passes through the allprison list: first starting
1927 * with the proposed jid, then ending with it.
1929 for (maxid = JAIL_MAX; maxid != 0; ) {
1930 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1931 if (inspr->pr_id < jid)
1933 if (inspr->pr_id > jid) {
1934 /* Found an opening. */
1938 if (++jid > maxid) {
1939 if (lastprid == maxid || lastprid == 0)
1942 * The entire legal range
1943 * has been traversed
1947 /* Try again from the start. */
1953 if (inspr == NULL) {
1954 /* Found room at the end of the list. */
1965 * struct jail_get_args {
1966 * struct iovec *iovp;
1967 * unsigned int iovcnt;
1972 sys_jail_get(struct thread *td, struct jail_get_args *uap)
1977 /* Check that we have an even number of iovecs. */
1978 if (uap->iovcnt & 1)
1981 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1984 error = kern_jail_get(td, auio, uap->flags);
1986 error = copyout(auio->uio_iov, uap->iovp,
1987 uap->iovcnt * sizeof (struct iovec));
1993 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1995 struct bool_flags *bf;
1996 struct jailsys_flags *jsf;
1997 struct prison *pr, *mypr;
1999 struct vfsoptlist *opts;
2000 char *errmsg, *name;
2001 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2004 if (flags & ~JAIL_GET_MASK)
2007 /* Get the parameter list. */
2008 error = vfs_buildopts(optuio, &opts);
2011 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2012 mypr = td->td_ucred->cr_prison;
2016 * Find the prison specified by one of: lastjid, jid, name.
2018 sx_slock(&allprison_lock);
2019 drflags = PD_LIST_SLOCKED;
2020 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2022 TAILQ_FOREACH(pr, &allprison, pr_list) {
2023 if (pr->pr_id > jid &&
2024 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2025 prison_ischild(mypr, pr)) {
2026 mtx_lock(&pr->pr_mtx);
2027 drflags |= PD_LOCKED;
2032 vfs_opterror(opts, "no jail after %d", jid);
2034 } else if (error != ENOENT)
2037 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2040 pr = prison_find_child(mypr, jid);
2042 drflags |= PD_LOCKED;
2043 if (!(prison_isalive(pr) ||
2044 (flags & JAIL_DYING))) {
2046 vfs_opterror(opts, "jail %d is dying",
2053 vfs_opterror(opts, "jail %d not found", jid);
2056 } else if (error != ENOENT)
2059 error = vfs_getopt(opts, "name", (void **)&name, &len);
2061 if (len == 0 || name[len - 1] != '\0') {
2065 pr = prison_find_name(mypr, name);
2067 drflags |= PD_LOCKED;
2068 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2070 vfs_opterror(opts, "jail \"%s\" is dying",
2077 vfs_opterror(opts, "jail \"%s\" not found", name);
2079 } else if (error != ENOENT)
2082 vfs_opterror(opts, "no jail specified");
2087 /* Get the parameters of the prison. */
2089 drflags |= PD_DEREF;
2090 td->td_retval[0] = pr->pr_id;
2091 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2092 if (error != 0 && error != ENOENT)
2094 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2095 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2096 if (error != 0 && error != ENOENT)
2098 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2099 if (error != 0 && error != ENOENT)
2101 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2102 sizeof(pr->pr_cpuset->cs_id));
2103 if (error != 0 && error != ENOENT)
2105 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2106 if (error != 0 && error != ENOENT)
2109 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2110 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2111 if (error != 0 && error != ENOENT)
2115 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2116 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2117 if (error != 0 && error != ENOENT)
2120 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2121 sizeof(pr->pr_securelevel));
2122 if (error != 0 && error != ENOENT)
2124 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2125 sizeof(pr->pr_childcount));
2126 if (error != 0 && error != ENOENT)
2128 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2129 sizeof(pr->pr_childmax));
2130 if (error != 0 && error != ENOENT)
2132 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2133 if (error != 0 && error != ENOENT)
2135 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2136 if (error != 0 && error != ENOENT)
2138 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2139 if (error != 0 && error != ENOENT)
2141 #ifdef COMPAT_FREEBSD32
2142 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2143 uint32_t hid32 = pr->pr_hostid;
2145 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2148 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2149 sizeof(pr->pr_hostid));
2150 if (error != 0 && error != ENOENT)
2152 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2153 sizeof(pr->pr_enforce_statfs));
2154 if (error != 0 && error != ENOENT)
2156 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2157 sizeof(pr->pr_devfs_rsnum));
2158 if (error != 0 && error != ENOENT)
2160 for (bf = pr_flag_bool;
2161 bf < pr_flag_bool + nitems(pr_flag_bool);
2163 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2164 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2165 if (error != 0 && error != ENOENT)
2168 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2169 if (error != 0 && error != ENOENT)
2172 for (jsf = pr_flag_jailsys;
2173 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2175 f = pr->pr_flags & (jsf->disable | jsf->new);
2176 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2177 : (f == jsf->new) ? JAIL_SYS_NEW
2179 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2180 if (error != 0 && error != ENOENT)
2183 for (bf = pr_flag_allow;
2184 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2185 atomic_load_int(&bf->flag) != 0;
2187 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2188 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2189 if (error != 0 && error != ENOENT)
2192 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2193 if (error != 0 && error != ENOENT)
2196 i = !prison_isalive(pr);
2197 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2198 if (error != 0 && error != ENOENT)
2201 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2202 if (error != 0 && error != ENOENT)
2204 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2205 sizeof(pr->pr_osreldate));
2206 if (error != 0 && error != ENOENT)
2208 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2209 if (error != 0 && error != ENOENT)
2212 /* Get the module parameters. */
2213 mtx_unlock(&pr->pr_mtx);
2214 drflags &= ~PD_LOCKED;
2215 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2218 prison_deref(pr, drflags);
2222 /* By now, all parameters should have been noted. */
2223 TAILQ_FOREACH(opt, opts, link) {
2224 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2226 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2231 /* Write the fetched parameters back to userspace. */
2233 TAILQ_FOREACH(opt, opts, link) {
2234 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2235 pos = 2 * opt->pos + 1;
2236 optuio->uio_iov[pos].iov_len = opt->len;
2237 if (opt->value != NULL) {
2238 if (optuio->uio_segflg == UIO_SYSSPACE) {
2240 optuio->uio_iov[pos].iov_base,
2243 error = copyout(opt->value,
2244 optuio->uio_iov[pos].iov_base,
2254 /* Release any temporary prison holds and/or locks. */
2256 prison_deref(pr, drflags);
2257 else if (drflags & PD_LIST_SLOCKED)
2258 sx_sunlock(&allprison_lock);
2259 if (error && errmsg_pos >= 0) {
2260 /* Write the error message back to userspace. */
2261 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2262 errmsg_pos = 2 * errmsg_pos + 1;
2263 if (errmsg_len > 0) {
2264 if (optuio->uio_segflg == UIO_SYSSPACE)
2266 optuio->uio_iov[errmsg_pos].iov_base,
2270 optuio->uio_iov[errmsg_pos].iov_base,
2279 * struct jail_remove_args {
2284 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2286 struct prison *pr, *cpr, *lpr;
2289 error = priv_check(td, PRIV_JAIL_REMOVE);
2293 sx_xlock(&allprison_lock);
2294 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2296 sx_xunlock(&allprison_lock);
2300 /* Remove all descendants of this prison, then remove this prison. */
2302 if (!LIST_EMPTY(&pr->pr_children)) {
2303 mtx_unlock(&pr->pr_mtx);
2305 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2308 mtx_lock(&lpr->pr_mtx);
2309 prison_remove_one(lpr);
2310 sx_xlock(&allprison_lock);
2315 mtx_lock(&lpr->pr_mtx);
2316 prison_remove_one(lpr);
2317 sx_xlock(&allprison_lock);
2319 mtx_lock(&pr->pr_mtx);
2321 prison_remove_one(pr);
2326 prison_remove_one(struct prison *pr)
2329 int was_alive, drflags;
2331 drflags = PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED;
2334 * Mark the prison as doomed, so it doesn't accidentally come back
2335 * to life. It may still be explicitly brought back by jail_set(2).
2337 was_alive = pr->pr_state == PRISON_STATE_ALIVE;
2338 pr->pr_state = PRISON_STATE_DYING;
2340 /* If the prison was persistent, it is not anymore. */
2341 if (pr->pr_flags & PR_PERSIST) {
2342 drflags |= PD_DEUREF;
2343 prison_free_not_last(pr);
2344 pr->pr_flags &= ~PR_PERSIST;
2348 * jail_remove added a reference. If that's the only one, remove
2349 * the prison now. refcount(9) doesn't guarantee the cache coherence
2350 * of non-zero counters, so force it here.
2352 KASSERT(refcount_load(&pr->pr_ref) > 0,
2353 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2354 if (atomic_load_acq_int(&pr->pr_ref) == 1) {
2355 prison_deref(pr, drflags);
2359 /* Tell modules this prison has died. */
2360 mtx_unlock(&pr->pr_mtx);
2361 drflags &= ~PD_LOCKED;
2363 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2365 sx_xunlock(&allprison_lock);
2366 drflags &= ~PD_LIST_XLOCKED;
2368 * Kill all processes unfortunate enough to be attached to this prison.
2370 sx_slock(&allproc_lock);
2371 FOREACH_PROC_IN_SYSTEM(p) {
2373 if (p->p_state != PRS_NEW && p->p_ucred &&
2374 p->p_ucred->cr_prison == pr)
2375 kern_psignal(p, SIGKILL);
2378 sx_sunlock(&allproc_lock);
2379 /* Remove the temporary reference added by jail_remove. */
2380 prison_deref(pr, drflags);
2384 * struct jail_attach_args {
2389 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2394 error = priv_check(td, PRIV_JAIL_ATTACH);
2398 sx_slock(&allprison_lock);
2399 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2401 sx_sunlock(&allprison_lock);
2405 /* Do not allow a process to attach to a prison that is not alive. */
2406 if (!prison_isalive(pr)) {
2407 mtx_unlock(&pr->pr_mtx);
2408 sx_sunlock(&allprison_lock);
2412 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2416 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2419 struct ucred *newcred, *oldcred;
2422 mtx_assert(&pr->pr_mtx, MA_OWNED);
2423 sx_assert(&allprison_lock, SX_LOCKED);
2425 * XXX: Note that there is a slight race here if two threads
2426 * in the same privileged process attempt to attach to two
2427 * different jails at the same time. It is important for
2428 * user processes not to do this, or they might end up with
2429 * a process root from one prison, but attached to the jail
2433 refcount_acquire(&pr->pr_uref);
2434 drflags |= PD_DEREF | PD_DEUREF;
2435 mtx_unlock(&pr->pr_mtx);
2436 drflags &= ~PD_LOCKED;
2438 /* Let modules do whatever they need to prepare for attaching. */
2439 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2441 prison_deref(pr, drflags);
2444 sx_unlock(&allprison_lock);
2445 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2448 * Reparent the newly attached process to this jail.
2451 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2455 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2456 if ((error = change_dir(pr->pr_root, td)) != 0)
2459 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2462 VOP_UNLOCK(pr->pr_root);
2463 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2468 oldcred = crcopysafe(p, newcred);
2469 newcred->cr_prison = pr;
2470 proc_set_cred(p, newcred);
2473 racct_proc_ucred_changed(p, oldcred, newcred);
2478 rctl_proc_ucred_changed(p, newcred);
2481 prison_deref(oldcred->cr_prison, drflags);
2485 * If the prison was killed while changing credentials, die along
2488 if (!prison_isalive(pr)) {
2490 kern_psignal(p, SIGKILL);
2497 VOP_UNLOCK(pr->pr_root);
2499 /* Tell modules this thread is still in its old jail after all. */
2500 sx_slock(&allprison_lock);
2501 drflags |= PD_LIST_SLOCKED;
2502 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2503 prison_deref(pr, drflags);
2508 * Returns a locked prison instance, or NULL on failure.
2511 prison_find(int prid)
2515 sx_assert(&allprison_lock, SX_LOCKED);
2516 TAILQ_FOREACH(pr, &allprison, pr_list) {
2517 if (pr->pr_id < prid)
2519 if (pr->pr_id > prid)
2521 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2522 mtx_lock(&pr->pr_mtx);
2529 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2532 prison_find_child(struct prison *mypr, int prid)
2537 sx_assert(&allprison_lock, SX_LOCKED);
2538 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2539 if (pr->pr_id == prid) {
2540 KASSERT(prison_isvalid(pr),
2541 ("Found invalid prison %p", pr));
2542 mtx_lock(&pr->pr_mtx);
2550 * Look for the name relative to mypr. Returns a locked prison or NULL.
2553 prison_find_name(struct prison *mypr, const char *name)
2555 struct prison *pr, *deadpr;
2559 sx_assert(&allprison_lock, SX_LOCKED);
2560 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2562 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2563 if (!strcmp(pr->pr_name + mylen, name)) {
2564 KASSERT(prison_isvalid(pr),
2565 ("Found invalid prison %p", pr));
2566 if (prison_isalive(pr)) {
2567 mtx_lock(&pr->pr_mtx);
2573 /* There was no valid prison - perhaps there was a dying one. */
2575 mtx_lock(&deadpr->pr_mtx);
2580 * See if a prison has the specific flag set. The prison should be locked,
2581 * unless checking for flags that are only set at jail creation (such as
2582 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2583 * to any other prison data.
2586 prison_flag(struct ucred *cred, unsigned flag)
2589 return (cred->cr_prison->pr_flags & flag);
2593 prison_allow(struct ucred *cred, unsigned flag)
2596 return ((cred->cr_prison->pr_allow & flag) != 0);
2600 * Hold a prison reference, by incrementing pr_ref. It is generally
2601 * an error to hold a prison that does not already have a reference.
2602 * A prison record will remain valid as long as it has at least one
2603 * reference, and will not be removed as long as either the prison
2604 * mutex or the allprison lock is held (allprison_lock may be shared).
2607 prison_hold_locked(struct prison *pr)
2610 /* Locking is no longer required. */
2615 prison_hold(struct prison *pr)
2618 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2621 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2623 refcount_acquire(&pr->pr_ref);
2628 * Remove a prison reference. If that was the last reference, the
2629 * prison will be removed (at a later time).
2632 prison_free_locked(struct prison *pr)
2635 mtx_assert(&pr->pr_mtx, MA_OWNED);
2637 * Locking is no longer required, but unlock because the caller
2640 mtx_unlock(&pr->pr_mtx);
2645 prison_free(struct prison *pr)
2648 KASSERT(refcount_load(&pr->pr_ref) > 0,
2649 ("Trying to free dead prison %p (jid=%d).",
2651 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2653 * Don't remove the last reference in this context,
2654 * in case there are locks held.
2656 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2661 prison_free_not_last(struct prison *pr)
2666 KASSERT(refcount_load(&pr->pr_ref) > 0,
2667 ("Trying to free dead prison %p (jid=%d).",
2669 lastref = refcount_release(&pr->pr_ref);
2671 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2674 refcount_release(&pr->pr_ref);
2679 * Hold a a prison for user visibility, by incrementing pr_uref.
2680 * It is generally an error to hold a prison that isn't already
2681 * user-visible, except through the the jail system calls. It is also
2682 * an error to hold an invalid prison. A prison record will remain
2683 * alive as long as it has at least one user reference, and will not
2684 * be set to the dying state until the prison mutex and allprison_lock
2688 prison_proc_hold(struct prison *pr)
2691 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2694 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2696 refcount_acquire(&pr->pr_uref);
2701 * Remove a prison user reference. If it was the last reference, the
2702 * prison will be considered "dying", and may be removed once all of
2703 * its references are dropped.
2706 prison_proc_free(struct prison *pr)
2710 * Locking is only required when releasing the last reference.
2711 * This allows assurance that a locked prison will remain alive
2712 * until it is unlocked.
2714 KASSERT(refcount_load(&pr->pr_uref) > 0,
2715 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2716 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2718 * Don't remove the last user reference in this context,
2719 * which is expected to be a process that is not only locked,
2720 * but also half dead. Add a reference so any calls to
2721 * prison_free() won't re-submit the task.
2724 mtx_lock(&pr->pr_mtx);
2725 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2726 ("Redundant last reference in prison_proc_free (jid=%d)",
2728 pr->pr_flags |= PR_COMPLETE_PROC;
2729 mtx_unlock(&pr->pr_mtx);
2730 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2735 * Complete a call to either prison_free or prison_proc_free.
2738 prison_complete(void *context, int pending)
2740 struct prison *pr = context;
2744 * This could be called to release the last reference, or the last
2745 * user reference (plus the reference held in prison_proc_free).
2747 drflags = prison_lock_xlock(pr, PD_DEREF);
2748 if (pr->pr_flags & PR_COMPLETE_PROC) {
2749 pr->pr_flags &= ~PR_COMPLETE_PROC;
2750 drflags |= PD_DEUREF;
2752 prison_deref(pr, drflags);
2756 * Remove a prison reference and/or user reference (usually).
2757 * This assumes context that allows sleeping (for allprison_lock),
2758 * with no non-sleeping locks held, except perhaps the prison itself.
2759 * If there are no more references, release and delist the prison.
2760 * On completion, the prison lock and the allprison lock are both
2764 prison_deref(struct prison *pr, int flags)
2766 struct prisonlist freeprison;
2767 struct prison *rpr, *ppr, *tpr;
2769 TAILQ_INIT(&freeprison);
2771 * Release this prison as requested, which may cause its parent
2772 * to be released, and then maybe its grandparent, etc.
2775 if (flags & PD_DEUREF) {
2776 /* Drop a user reference. */
2777 KASSERT(refcount_load(&pr->pr_uref) > 0,
2778 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2780 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2781 if (!(flags & PD_DEREF)) {
2785 flags = prison_lock_xlock(pr, flags);
2786 if (refcount_release(&pr->pr_uref) &&
2787 pr->pr_state == PRISON_STATE_ALIVE) {
2789 * When the last user references goes,
2790 * this becomes a dying prison.
2793 refcount_load(&prison0.pr_uref) > 0,
2794 ("prison0 pr_uref=0"));
2795 pr->pr_state = PRISON_STATE_DYING;
2796 mtx_unlock(&pr->pr_mtx);
2797 flags &= ~PD_LOCKED;
2798 (void)osd_jail_call(pr,
2799 PR_METHOD_REMOVE, NULL);
2803 if (flags & PD_DEREF) {
2804 /* Drop a reference. */
2805 KASSERT(refcount_load(&pr->pr_ref) > 0,
2806 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2808 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2809 flags = prison_lock_xlock(pr, flags);
2810 if (refcount_release(&pr->pr_ref)) {
2812 * When the last reference goes,
2813 * unlink the prison and set it aside.
2816 refcount_load(&pr->pr_uref) == 0,
2817 ("prison_deref: last ref, "
2818 "but still has %d urefs (jid=%d)",
2819 pr->pr_uref, pr->pr_id));
2821 refcount_load(&prison0.pr_ref) != 0,
2822 ("prison0 pr_ref=0"));
2823 pr->pr_state = PRISON_STATE_INVALID;
2824 TAILQ_REMOVE(&allprison, pr, pr_list);
2825 LIST_REMOVE(pr, pr_sibling);
2826 TAILQ_INSERT_TAIL(&freeprison, pr,
2828 for (ppr = pr->pr_parent;
2830 ppr = ppr->pr_parent)
2831 ppr->pr_childcount--;
2833 * Removing a prison frees references
2836 mtx_unlock(&pr->pr_mtx);
2837 flags &= ~PD_LOCKED;
2839 flags |= PD_DEREF | PD_DEUREF;
2847 /* Release all the prison locks. */
2848 if (flags & PD_LOCKED)
2849 mtx_unlock(&pr->pr_mtx);
2850 if (flags & PD_LIST_SLOCKED)
2851 sx_sunlock(&allprison_lock);
2852 else if (flags & PD_LIST_XLOCKED)
2853 sx_xunlock(&allprison_lock);
2856 * Finish removing any unreferenced prisons, which couldn't happen
2857 * while allprison_lock was held (to avoid a LOR on vrele).
2859 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
2861 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
2862 vnet_destroy(rpr->pr_vnet);
2864 if (rpr->pr_root != NULL)
2865 vrele(rpr->pr_root);
2866 mtx_destroy(&rpr->pr_mtx);
2868 free(rpr->pr_ip4, M_PRISON);
2871 free(rpr->pr_ip6, M_PRISON);
2873 if (rpr->pr_cpuset != NULL)
2874 cpuset_rel(rpr->pr_cpuset);
2878 prison_racct_detach(rpr);
2880 TAILQ_REMOVE(&freeprison, rpr, pr_list);
2881 free(rpr, M_PRISON);
2886 * Given the current locking state in the flags, make sure allprison_lock
2887 * is held exclusive, and the prison is locked. Return flags indicating
2891 prison_lock_xlock(struct prison *pr, int flags)
2894 if (!(flags & PD_LIST_XLOCKED)) {
2896 * Get allprison_lock, which may be an upgrade,
2897 * and may require unlocking the prison.
2899 if (flags & PD_LOCKED) {
2900 mtx_unlock(&pr->pr_mtx);
2901 flags &= ~PD_LOCKED;
2903 if (flags & PD_LIST_SLOCKED) {
2904 if (!sx_try_upgrade(&allprison_lock)) {
2905 sx_sunlock(&allprison_lock);
2906 sx_xlock(&allprison_lock);
2908 flags &= ~PD_LIST_SLOCKED;
2910 sx_xlock(&allprison_lock);
2911 flags |= PD_LIST_XLOCKED;
2913 if (!(flags & PD_LOCKED)) {
2914 /* Lock the prison mutex. */
2915 mtx_lock(&pr->pr_mtx);
2922 * Set or clear a permission bit in the pr_allow field, passing restrictions
2923 * (cleared permission) down to child jails.
2926 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
2930 pr = cred->cr_prison;
2931 sx_slock(&allprison_lock);
2932 mtx_lock(&pr->pr_mtx);
2933 prison_set_allow_locked(pr, flag, enable);
2934 mtx_unlock(&pr->pr_mtx);
2935 sx_sunlock(&allprison_lock);
2939 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
2945 pr->pr_allow |= flag;
2947 pr->pr_allow &= ~flag;
2948 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
2949 cpr->pr_allow &= ~flag;
2954 * Check if a jail supports the given address family.
2956 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
2960 prison_check_af(struct ucred *cred, int af)
2965 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2967 pr = cred->cr_prison;
2969 /* Prisons with their own network stack are not limited. */
2970 if (prison_owns_vnet(cred))
2979 if (pr->pr_flags & PR_IP4)
2981 mtx_lock(&pr->pr_mtx);
2982 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
2983 error = EAFNOSUPPORT;
2984 mtx_unlock(&pr->pr_mtx);
2990 if (pr->pr_flags & PR_IP6)
2992 mtx_lock(&pr->pr_mtx);
2993 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
2994 error = EAFNOSUPPORT;
2995 mtx_unlock(&pr->pr_mtx);
3003 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3004 error = EAFNOSUPPORT;
3010 * Check if given address belongs to the jail referenced by cred (wrapper to
3011 * prison_check_ip[46]).
3013 * Returns 0 if jail doesn't restrict the address family or if address belongs
3014 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3015 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3018 prison_if(struct ucred *cred, const struct sockaddr *sa)
3021 const struct sockaddr_in *sai;
3024 const struct sockaddr_in6 *sai6;
3028 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3029 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3032 if (prison_owns_vnet(cred))
3037 switch (sa->sa_family)
3041 sai = (const struct sockaddr_in *)sa;
3042 error = prison_check_ip4(cred, &sai->sin_addr);
3047 sai6 = (const struct sockaddr_in6 *)sa;
3048 error = prison_check_ip6(cred, &sai6->sin6_addr);
3052 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3053 error = EAFNOSUPPORT;
3059 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3062 prison_check(struct ucred *cred1, struct ucred *cred2)
3065 return ((cred1->cr_prison == cred2->cr_prison ||
3066 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3070 * Return 1 if p2 is a child of p1, otherwise 0.
3073 prison_ischild(struct prison *pr1, struct prison *pr2)
3076 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3083 * Return true if the prison is currently alive. A prison is alive if it
3084 * holds user references and it isn't being removed.
3087 prison_isalive(struct prison *pr)
3090 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3096 * Return true if the prison is currently valid. A prison is valid if it has
3097 * been fully created, and is not being destroyed. Note that dying prisons
3098 * are still considered valid. Invalid prisons won't be found under normal
3099 * circumstances, as they're only put in that state by functions that have
3100 * an exclusive hold on allprison_lock.
3103 prison_isvalid(struct prison *pr)
3106 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3108 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3114 * Return 1 if the passed credential is in a jail and that jail does not
3115 * have its own virtual network stack, otherwise 0.
3118 jailed_without_vnet(struct ucred *cred)
3124 if (prison_owns_vnet(cred))
3132 * Return the correct hostname (domainname, et al) for the passed credential.
3135 getcredhostname(struct ucred *cred, char *buf, size_t size)
3140 * A NULL credential can be used to shortcut to the physical
3141 * system's hostname.
3143 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3144 mtx_lock(&pr->pr_mtx);
3145 strlcpy(buf, pr->pr_hostname, size);
3146 mtx_unlock(&pr->pr_mtx);
3150 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3153 mtx_lock(&cred->cr_prison->pr_mtx);
3154 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3155 mtx_unlock(&cred->cr_prison->pr_mtx);
3159 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3162 mtx_lock(&cred->cr_prison->pr_mtx);
3163 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3164 mtx_unlock(&cred->cr_prison->pr_mtx);
3168 getcredhostid(struct ucred *cred, unsigned long *hostid)
3171 mtx_lock(&cred->cr_prison->pr_mtx);
3172 *hostid = cred->cr_prison->pr_hostid;
3173 mtx_unlock(&cred->cr_prison->pr_mtx);
3177 getjailname(struct ucred *cred, char *name, size_t len)
3180 mtx_lock(&cred->cr_prison->pr_mtx);
3181 strlcpy(name, cred->cr_prison->pr_name, len);
3182 mtx_unlock(&cred->cr_prison->pr_mtx);
3187 * Determine whether the prison represented by cred owns
3188 * its vnet rather than having it inherited.
3190 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3193 prison_owns_vnet(struct ucred *cred)
3197 * vnets cannot be added/removed after jail creation,
3198 * so no need to lock here.
3200 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3205 * Determine whether the subject represented by cred can "see"
3206 * status of a mount point.
3207 * Returns: 0 for permitted, ENOENT otherwise.
3208 * XXX: This function should be called cr_canseemount() and should be
3209 * placed in kern_prot.c.
3212 prison_canseemount(struct ucred *cred, struct mount *mp)
3218 pr = cred->cr_prison;
3219 if (pr->pr_enforce_statfs == 0)
3221 if (pr->pr_root->v_mount == mp)
3223 if (pr->pr_enforce_statfs == 2)
3226 * If jail's chroot directory is set to "/" we should be able to see
3227 * all mount-points from inside a jail.
3228 * This is ugly check, but this is the only situation when jail's
3229 * directory ends with '/'.
3231 if (strcmp(pr->pr_path, "/") == 0)
3233 len = strlen(pr->pr_path);
3235 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3238 * Be sure that we don't have situation where jail's root directory
3239 * is "/some/path" and mount point is "/some/pathpath".
3241 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3247 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3249 char jpath[MAXPATHLEN];
3253 pr = cred->cr_prison;
3254 if (pr->pr_enforce_statfs == 0)
3256 if (prison_canseemount(cred, mp) != 0) {
3257 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3258 strlcpy(sp->f_mntonname, "[restricted]",
3259 sizeof(sp->f_mntonname));
3262 if (pr->pr_root->v_mount == mp) {
3264 * Clear current buffer data, so we are sure nothing from
3265 * the valid path left there.
3267 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3268 *sp->f_mntonname = '/';
3272 * If jail's chroot directory is set to "/" we should be able to see
3273 * all mount-points from inside a jail.
3275 if (strcmp(pr->pr_path, "/") == 0)
3277 len = strlen(pr->pr_path);
3278 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3280 * Clear current buffer data, so we are sure nothing from
3281 * the valid path left there.
3283 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3284 if (*jpath == '\0') {
3285 /* Should never happen. */
3286 *sp->f_mntonname = '/';
3288 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3293 * Check with permission for a specific privilege is granted within jail. We
3294 * have a specific list of accepted privileges; the rest are denied.
3297 prison_priv_check(struct ucred *cred, int priv)
3303 * Some policies have custom handlers. This routine should not be
3304 * called for them. See priv_check_cred().
3307 case PRIV_VFS_LOOKUP:
3308 case PRIV_VFS_GENERATION:
3309 KASSERT(0, ("prison_priv_check instead of a custom handler "
3310 "called for %d\n", priv));
3318 * Privileges specific to prisons with a virtual network stack.
3319 * There might be a duplicate entry here in case the privilege
3320 * is only granted conditionally in the legacy jail case.
3325 * NFS-specific privileges.
3327 case PRIV_NFS_DAEMON:
3328 case PRIV_NFS_LOCKD:
3331 * Network stack privileges.
3333 case PRIV_NET_BRIDGE:
3336 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3337 case PRIV_NET_ROUTE:
3339 case PRIV_NET_SETIFMTU:
3340 case PRIV_NET_SETIFFLAGS:
3341 case PRIV_NET_SETIFCAP:
3342 case PRIV_NET_SETIFDESCR:
3343 case PRIV_NET_SETIFNAME :
3344 case PRIV_NET_SETIFMETRIC:
3345 case PRIV_NET_SETIFPHYS:
3346 case PRIV_NET_SETIFMAC:
3347 case PRIV_NET_SETLANPCP:
3348 case PRIV_NET_ADDMULTI:
3349 case PRIV_NET_DELMULTI:
3350 case PRIV_NET_HWIOCTL:
3351 case PRIV_NET_SETLLADDR:
3352 case PRIV_NET_ADDIFGROUP:
3353 case PRIV_NET_DELIFGROUP:
3354 case PRIV_NET_IFCREATE:
3355 case PRIV_NET_IFDESTROY:
3356 case PRIV_NET_ADDIFADDR:
3357 case PRIV_NET_DELIFADDR:
3360 case PRIV_NET_SETIFVNET:
3361 case PRIV_NET_SETIFFIB:
3364 * 802.11-related privileges.
3366 case PRIV_NET80211_VAP_GETKEY:
3367 case PRIV_NET80211_VAP_MANAGE:
3373 case PRIV_NETATM_CFG:
3374 case PRIV_NETATM_ADD:
3375 case PRIV_NETATM_DEL:
3376 case PRIV_NETATM_SET:
3379 * Bluetooth privileges.
3381 case PRIV_NETBLUETOOTH_RAW:
3385 * Netgraph and netgraph module privileges.
3387 case PRIV_NETGRAPH_CONTROL:
3389 case PRIV_NETGRAPH_TTY:
3393 * IPv4 and IPv6 privileges.
3395 case PRIV_NETINET_IPFW:
3396 case PRIV_NETINET_DIVERT:
3397 case PRIV_NETINET_PF:
3398 case PRIV_NETINET_DUMMYNET:
3399 case PRIV_NETINET_CARP:
3400 case PRIV_NETINET_MROUTE:
3401 case PRIV_NETINET_RAW:
3402 case PRIV_NETINET_ADDRCTRL6:
3403 case PRIV_NETINET_ND6:
3404 case PRIV_NETINET_SCOPE6:
3405 case PRIV_NETINET_ALIFETIME6:
3406 case PRIV_NETINET_IPSEC:
3407 case PRIV_NETINET_BINDANY:
3422 * No default: or deny here.
3423 * In case of no permit fall through to next switch().
3425 if (cred->cr_prison->pr_flags & PR_VNET)
3432 * Allow ktrace privileges for root in jail.
3438 * Allow jailed processes to configure audit identity and
3439 * submit audit records (login, etc). In the future we may
3440 * want to further refine the relationship between audit and
3443 case PRIV_AUDIT_GETAUDIT:
3444 case PRIV_AUDIT_SETAUDIT:
3445 case PRIV_AUDIT_SUBMIT:
3449 * Allow jailed processes to manipulate process UNIX
3450 * credentials in any way they see fit.
3452 case PRIV_CRED_SETUID:
3453 case PRIV_CRED_SETEUID:
3454 case PRIV_CRED_SETGID:
3455 case PRIV_CRED_SETEGID:
3456 case PRIV_CRED_SETGROUPS:
3457 case PRIV_CRED_SETREUID:
3458 case PRIV_CRED_SETREGID:
3459 case PRIV_CRED_SETRESUID:
3460 case PRIV_CRED_SETRESGID:
3463 * Jail implements visibility constraints already, so allow
3464 * jailed root to override uid/gid-based constraints.
3466 case PRIV_SEEOTHERGIDS:
3467 case PRIV_SEEOTHERUIDS:
3470 * Jail implements inter-process debugging limits already, so
3471 * allow jailed root various debugging privileges.
3473 case PRIV_DEBUG_DIFFCRED:
3474 case PRIV_DEBUG_SUGID:
3475 case PRIV_DEBUG_UNPRIV:
3478 * Allow jail to set various resource limits and login
3479 * properties, and for now, exceed process resource limits.
3481 case PRIV_PROC_LIMIT:
3482 case PRIV_PROC_SETLOGIN:
3483 case PRIV_PROC_SETRLIMIT:
3486 * System V and POSIX IPC privileges are granted in jail.
3489 case PRIV_IPC_WRITE:
3490 case PRIV_IPC_ADMIN:
3491 case PRIV_IPC_MSGSIZE:
3495 * Jail operations within a jail work on child jails.
3497 case PRIV_JAIL_ATTACH:
3499 case PRIV_JAIL_REMOVE:
3502 * Jail implements its own inter-process limits, so allow
3503 * root processes in jail to change scheduling on other
3504 * processes in the same jail. Likewise for signalling.
3506 case PRIV_SCHED_DIFFCRED:
3507 case PRIV_SCHED_CPUSET:
3508 case PRIV_SIGNAL_DIFFCRED:
3509 case PRIV_SIGNAL_SUGID:
3512 * Allow jailed processes to write to sysctls marked as jail
3515 case PRIV_SYSCTL_WRITEJAIL:
3518 * Allow root in jail to manage a variety of quota
3519 * properties. These should likely be conditional on a
3520 * configuration option.
3522 case PRIV_VFS_GETQUOTA:
3523 case PRIV_VFS_SETQUOTA:
3526 * Since Jail relies on chroot() to implement file system
3527 * protections, grant many VFS privileges to root in jail.
3528 * Be careful to exclude mount-related and NFS-related
3532 case PRIV_VFS_WRITE:
3533 case PRIV_VFS_ADMIN:
3535 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3536 case PRIV_VFS_CHFLAGS_DEV:
3537 case PRIV_VFS_CHOWN:
3538 case PRIV_VFS_CHROOT:
3539 case PRIV_VFS_RETAINSUGID:
3540 case PRIV_VFS_FCHROOT:
3542 case PRIV_VFS_SETGID:
3544 case PRIV_VFS_STICKYFILE:
3547 * As in the non-jail case, non-root users are expected to be
3548 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3549 * exists in the jail and they have permission to access it).
3551 case PRIV_KMEM_READ:
3555 * Depending on the global setting, allow privilege of
3556 * setting system flags.
3558 case PRIV_VFS_SYSFLAGS:
3559 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3565 * Depending on the global setting, allow privilege of
3566 * mounting/unmounting file systems.
3568 case PRIV_VFS_MOUNT:
3569 case PRIV_VFS_UNMOUNT:
3570 case PRIV_VFS_MOUNT_NONUSER:
3571 case PRIV_VFS_MOUNT_OWNER:
3572 pr = cred->cr_prison;
3574 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3582 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3583 * policy. priv_check_cred will not specifically allow it, and
3584 * we may want a MAC policy to allow it.
3586 case PRIV_VFS_READ_DIR:
3590 * Conditionnaly allow locking (unlocking) physical pages
3594 case PRIV_VM_MUNLOCK:
3595 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3601 * Conditionally allow jailed root to bind reserved ports.
3603 case PRIV_NETINET_RESERVEDPORT:
3604 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3610 * Allow jailed root to reuse in-use ports.
3612 case PRIV_NETINET_REUSEPORT:
3616 * Allow jailed root to set certain IPv4/6 (option) headers.
3618 case PRIV_NETINET_SETHDROPTS:
3622 * Conditionally allow creating raw sockets in jail.
3624 case PRIV_NETINET_RAW:
3625 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3631 * Since jail implements its own visibility limits on netstat
3632 * sysctls, allow getcred. This allows identd to work in
3635 case PRIV_NETINET_GETCRED:
3639 * Allow jailed root to set loginclass.
3641 case PRIV_PROC_SETLOGINCLASS:
3645 * Do not allow a process inside a jail to read the kernel
3646 * message buffer unless explicitly permitted.
3649 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3655 * In all remaining cases, deny the privilege request. This
3656 * includes almost all network privileges, many system
3657 * configuration privileges.
3664 * Return the part of pr2's name that is relative to pr1, or the whole name
3665 * if it does not directly follow.
3669 prison_name(struct prison *pr1, struct prison *pr2)
3673 /* Jails see themselves as "0" (if they see themselves at all). */
3676 name = pr2->pr_name;
3677 if (prison_ischild(pr1, pr2)) {
3679 * pr1 isn't locked (and allprison_lock may not be either)
3680 * so its length can't be counted on. But the number of dots
3681 * can be counted on - and counted.
3683 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3684 name = strchr(name, '.') + 1;
3690 * Return the part of pr2's path that is relative to pr1, or the whole path
3691 * if it does not directly follow.
3694 prison_path(struct prison *pr1, struct prison *pr2)
3696 char *path1, *path2;
3699 path1 = pr1->pr_path;
3700 path2 = pr2->pr_path;
3701 if (!strcmp(path1, "/"))
3703 len1 = strlen(path1);
3704 if (strncmp(path1, path2, len1))
3706 if (path2[len1] == '\0')
3708 if (path2[len1] == '/')
3709 return (path2 + len1);
3714 * Jail-related sysctls.
3716 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3720 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3723 struct prison *pr, *cpr;
3725 struct in_addr *ip4 = NULL;
3729 struct in6_addr *ip6 = NULL;
3734 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3735 pr = req->td->td_ucred->cr_prison;
3737 sx_slock(&allprison_lock);
3738 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3739 #if defined(INET) || defined(INET6)
3742 mtx_lock(&cpr->pr_mtx);
3744 if (cpr->pr_ip4s > 0) {
3745 if (ip4s < cpr->pr_ip4s) {
3746 ip4s = cpr->pr_ip4s;
3747 mtx_unlock(&cpr->pr_mtx);
3748 ip4 = realloc(ip4, ip4s *
3749 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3752 bcopy(cpr->pr_ip4, ip4,
3753 cpr->pr_ip4s * sizeof(struct in_addr));
3757 if (cpr->pr_ip6s > 0) {
3758 if (ip6s < cpr->pr_ip6s) {
3759 ip6s = cpr->pr_ip6s;
3760 mtx_unlock(&cpr->pr_mtx);
3761 ip6 = realloc(ip6, ip6s *
3762 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3765 bcopy(cpr->pr_ip6, ip6,
3766 cpr->pr_ip6s * sizeof(struct in6_addr));
3769 bzero(xp, sizeof(*xp));
3770 xp->pr_version = XPRISON_VERSION;
3771 xp->pr_id = cpr->pr_id;
3772 xp->pr_state = cpr->pr_state;
3773 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3774 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3775 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3777 xp->pr_ip4s = cpr->pr_ip4s;
3780 xp->pr_ip6s = cpr->pr_ip6s;
3782 mtx_unlock(&cpr->pr_mtx);
3783 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3787 if (xp->pr_ip4s > 0) {
3788 error = SYSCTL_OUT(req, ip4,
3789 xp->pr_ip4s * sizeof(struct in_addr));
3795 if (xp->pr_ip6s > 0) {
3796 error = SYSCTL_OUT(req, ip6,
3797 xp->pr_ip6s * sizeof(struct in6_addr));
3803 sx_sunlock(&allprison_lock);
3814 SYSCTL_OID(_security_jail, OID_AUTO, list,
3815 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3816 sysctl_jail_list, "S", "List of active jails");
3819 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3823 injail = jailed(req->td->td_ucred);
3824 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3829 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3830 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3831 sysctl_jail_jailed, "I", "Process in jail?");
3834 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3836 int error, havevnet;
3838 struct ucred *cred = req->td->td_ucred;
3840 havevnet = jailed(cred) && prison_owns_vnet(cred);
3844 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3849 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3850 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3851 sysctl_jail_vnet, "I", "Jail owns vnet?");
3853 #if defined(INET) || defined(INET6)
3854 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3855 &jail_max_af_ips, 0,
3856 "Number of IP addresses a jail may have at most per address family (deprecated)");
3860 * Default parameters for jail(2) compatibility. For historical reasons,
3861 * the sysctl names have varying similarity to the parameter names. Prisons
3862 * just see their own parameters, and can't change them.
3865 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3869 /* Get the current flag value, and convert it to a boolean. */
3870 if (req->td->td_ucred->cr_prison == &prison0) {
3871 mtx_lock(&prison0.pr_mtx);
3872 i = (jail_default_allow & arg2) != 0;
3873 mtx_unlock(&prison0.pr_mtx);
3875 i = prison_allow(req->td->td_ucred, arg2);
3879 error = sysctl_handle_int(oidp, &i, 0, req);
3880 if (error || !req->newptr)
3886 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3889 mtx_lock(&prison0.pr_mtx);
3890 jail_default_allow = (jail_default_allow & ~arg2) | i;
3891 mtx_unlock(&prison0.pr_mtx);
3895 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3896 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3897 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3898 "Processes in jail can set their hostnames (deprecated)");
3899 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3900 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3901 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3902 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
3903 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3904 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3905 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3906 "Processes in jail can use System V IPC primitives (deprecated)");
3907 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3908 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3909 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3910 "Prison root can create raw sockets (deprecated)");
3911 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3912 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3913 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3914 "Processes in jail can alter system file flags (deprecated)");
3915 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3916 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3917 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3918 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
3921 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3926 pr = req->td->td_ucred->cr_prison;
3927 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
3928 error = sysctl_handle_int(oidp, &level, 0, req);
3929 if (error || !req->newptr)
3931 *(int *)arg1 = level;
3935 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3936 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3937 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
3938 sysctl_jail_default_level, "I",
3939 "Processes in jail cannot see all mounted file systems (deprecated)");
3941 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
3942 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
3943 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
3944 sysctl_jail_default_level, "I",
3945 "Ruleset for the devfs filesystem in jail (deprecated)");
3948 * Nodes to describe jail parameters. Maximum length of string parameters
3949 * is returned in the string itself, and the other parameters exist merely
3950 * to make themselves and their types known.
3952 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3956 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
3963 switch (oidp->oid_kind & CTLTYPE)
3969 if (!(req->flags & SCTL_MASK32))
3971 return (SYSCTL_OUT(req, &l, sizeof(l)));
3975 return (SYSCTL_OUT(req, &i, sizeof(i)));
3976 case CTLTYPE_STRING:
3977 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
3979 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
3980 case CTLTYPE_STRUCT:
3982 return (SYSCTL_OUT(req, &s, sizeof(s)));
3988 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
3989 * jail creation time but cannot be changed in an existing jail.
3991 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
3992 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
3993 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
3994 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
3995 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
3996 "I", "Jail secure level");
3997 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
3998 "Jail value for kern.osreldate and uname -K");
3999 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4000 "Jail value for kern.osrelease and uname -r");
4001 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4002 "I", "Jail cannot see all mounted file systems");
4003 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4004 "I", "Ruleset for in-jail devfs mounts");
4005 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4006 "B", "Jail persistence");
4008 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4009 "E,jailsys", "Virtual network stack");
4011 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4012 "B", "Jail is in the process of shutting down");
4014 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4015 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4016 "I", "Current number of child jails");
4017 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4018 "I", "Maximum number of child jails");
4020 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4021 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4023 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4024 "Jail NIS domainname");
4025 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4027 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4028 "LU", "Jail host ID");
4030 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4031 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4034 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4035 "Jail IPv4 address virtualization");
4036 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4037 "S,in_addr,a", "Jail IPv4 addresses");
4038 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4039 "B", "Do (not) use IPv4 source address selection rather than the "
4040 "primary jail IPv4 address.");
4043 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4044 "Jail IPv6 address virtualization");
4045 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4046 "S,in6_addr,a", "Jail IPv6 addresses");
4047 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4048 "B", "Do (not) use IPv6 source address selection rather than the "
4049 "primary jail IPv6 address.");
4052 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4053 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4054 "B", "Jail may set hostname");
4055 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4056 "B", "Jail may use SYSV IPC");
4057 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4058 "B", "Jail may create raw sockets");
4059 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4060 "B", "Jail may alter system file flags");
4061 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4062 "B", "Jail may set file quotas");
4063 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4064 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4065 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4066 "B", "Jail may lock (unlock) physical pages in memory");
4067 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4068 "B", "Jail may bind sockets to reserved ports");
4069 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4070 "B", "Jail may read the kernel message buffer");
4071 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4072 "B", "Unprivileged processes may use process debugging facilities");
4073 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4074 "B", "Processes in jail with uid 0 have privilege");
4076 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4077 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4078 "B", "Jail may mount/unmount jail-friendly file systems in general");
4081 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4082 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4086 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4089 struct bool_flags *bf;
4090 struct sysctl_oid *parent;
4091 char *allow_name, *allow_noname, *allowed;
4092 #ifndef NO_SYSCTL_DESCR
4093 char *descr_deprecated;
4098 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4100 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4102 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4103 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4104 free(allow_name, M_PRISON);
4109 * See if this parameter has already beed added, i.e. a module was
4110 * previously loaded/unloaded.
4112 mtx_lock(&prison0.pr_mtx);
4113 for (bf = pr_flag_allow;
4114 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4115 atomic_load_int(&bf->flag) != 0;
4117 if (strcmp(bf->name, allow_name) == 0) {
4118 allow_flag = bf->flag;
4124 * Find a free bit in pr_allow_all, failing if there are none
4125 * (which shouldn't happen as long as we keep track of how many
4126 * potential dynamic flags exist).
4128 for (allow_flag = 1;; allow_flag <<= 1) {
4129 if (allow_flag == 0)
4131 if ((pr_allow_all & allow_flag) == 0)
4135 /* Note the parameter in the next open slot in pr_flag_allow. */
4136 for (bf = pr_flag_allow; ; bf++) {
4137 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4138 /* This should never happen, but is not fatal. */
4142 if (atomic_load_int(&bf->flag) == 0)
4145 bf->name = allow_name;
4146 bf->noname = allow_noname;
4147 pr_allow_all |= allow_flag;
4149 * prison0 always has permission for the new parameter.
4150 * Other jails must have it granted to them.
4152 prison0.pr_allow |= allow_flag;
4153 /* The flag indicates a valid entry, so make sure it is set last. */
4154 atomic_store_rel_int(&bf->flag, allow_flag);
4155 mtx_unlock(&prison0.pr_mtx);
4158 * Create sysctls for the paramter, and the back-compat global
4162 ? SYSCTL_ADD_NODE(NULL,
4163 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4164 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4165 : &sysctl___security_jail_param_allow;
4166 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4167 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4168 NULL, 0, sysctl_jail_param, "B", descr);
4170 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4171 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4172 #ifndef NO_SYSCTL_DESCR
4173 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4176 (void)SYSCTL_ADD_PROC(NULL,
4177 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4178 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4179 sysctl_jail_default_allow, "I", descr_deprecated);
4180 #ifndef NO_SYSCTL_DESCR
4181 free(descr_deprecated, M_TEMP);
4183 free(allowed, M_TEMP);
4188 mtx_unlock(&prison0.pr_mtx);
4189 free(allow_name, M_PRISON);
4190 free(allow_noname, M_PRISON);
4195 * The VFS system will register jail-aware filesystems here. They each get
4196 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4197 * attempts to mount.
4200 prison_add_vfs(struct vfsconf *vfsp)
4202 #ifdef NO_SYSCTL_DESCR
4204 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4209 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4211 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4213 free(descr, M_TEMP);
4219 prison_racct_foreach(void (*callback)(struct racct *racct,
4220 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4221 void *arg2, void *arg3)
4223 struct prison_racct *prr;
4225 ASSERT_RACCT_ENABLED();
4227 sx_slock(&allprison_lock);
4230 LIST_FOREACH(prr, &allprison_racct, prr_next)
4231 (callback)(prr->prr_racct, arg2, arg3);
4234 sx_sunlock(&allprison_lock);
4237 static struct prison_racct *
4238 prison_racct_find_locked(const char *name)
4240 struct prison_racct *prr;
4242 ASSERT_RACCT_ENABLED();
4243 sx_assert(&allprison_lock, SA_XLOCKED);
4245 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4248 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4249 if (strcmp(name, prr->prr_name) != 0)
4252 /* Found prison_racct with a matching name? */
4253 prison_racct_hold(prr);
4257 /* Add new prison_racct. */
4258 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4259 racct_create(&prr->prr_racct);
4261 strcpy(prr->prr_name, name);
4262 refcount_init(&prr->prr_refcount, 1);
4263 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4268 struct prison_racct *
4269 prison_racct_find(const char *name)
4271 struct prison_racct *prr;
4273 ASSERT_RACCT_ENABLED();
4275 sx_xlock(&allprison_lock);
4276 prr = prison_racct_find_locked(name);
4277 sx_xunlock(&allprison_lock);
4282 prison_racct_hold(struct prison_racct *prr)
4285 ASSERT_RACCT_ENABLED();
4287 refcount_acquire(&prr->prr_refcount);
4291 prison_racct_free_locked(struct prison_racct *prr)
4294 ASSERT_RACCT_ENABLED();
4295 sx_assert(&allprison_lock, SA_XLOCKED);
4297 if (refcount_release(&prr->prr_refcount)) {
4298 racct_destroy(&prr->prr_racct);
4299 LIST_REMOVE(prr, prr_next);
4300 free(prr, M_PRISON_RACCT);
4305 prison_racct_free(struct prison_racct *prr)
4308 ASSERT_RACCT_ENABLED();
4309 sx_assert(&allprison_lock, SA_UNLOCKED);
4311 if (refcount_release_if_not_last(&prr->prr_refcount))
4314 sx_xlock(&allprison_lock);
4315 prison_racct_free_locked(prr);
4316 sx_xunlock(&allprison_lock);
4320 prison_racct_attach(struct prison *pr)
4322 struct prison_racct *prr;
4324 ASSERT_RACCT_ENABLED();
4325 sx_assert(&allprison_lock, SA_XLOCKED);
4327 prr = prison_racct_find_locked(pr->pr_name);
4328 KASSERT(prr != NULL, ("cannot find prison_racct"));
4330 pr->pr_prison_racct = prr;
4334 * Handle jail renaming. From the racct point of view, renaming means
4335 * moving from one prison_racct to another.
4338 prison_racct_modify(struct prison *pr)
4344 struct prison_racct *oldprr;
4346 ASSERT_RACCT_ENABLED();
4348 sx_slock(&allproc_lock);
4349 sx_xlock(&allprison_lock);
4351 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4352 sx_xunlock(&allprison_lock);
4353 sx_sunlock(&allproc_lock);
4357 oldprr = pr->pr_prison_racct;
4358 pr->pr_prison_racct = NULL;
4360 prison_racct_attach(pr);
4363 * Move resource utilisation records.
4365 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4369 * Force rctl to reattach rules to processes.
4371 FOREACH_PROC_IN_SYSTEM(p) {
4373 cred = crhold(p->p_ucred);
4375 rctl_proc_ucred_changed(p, cred);
4380 sx_sunlock(&allproc_lock);
4381 prison_racct_free_locked(oldprr);
4382 sx_xunlock(&allprison_lock);
4386 prison_racct_detach(struct prison *pr)
4389 ASSERT_RACCT_ENABLED();
4390 sx_assert(&allprison_lock, SA_UNLOCKED);
4392 if (pr->pr_prison_racct == NULL)
4394 prison_racct_free(pr->pr_prison_racct);
4395 pr->pr_prison_racct = NULL;
4402 db_show_prison(struct prison *pr)
4404 struct bool_flags *bf;
4405 struct jailsys_flags *jsf;
4406 #if defined(INET) || defined(INET6)
4411 char ip4buf[INET_ADDRSTRLEN];
4414 char ip6buf[INET6_ADDRSTRLEN];
4417 db_printf("prison %p:\n", pr);
4418 db_printf(" jid = %d\n", pr->pr_id);
4419 db_printf(" name = %s\n", pr->pr_name);
4420 db_printf(" parent = %p\n", pr->pr_parent);
4421 db_printf(" ref = %d\n", pr->pr_ref);
4422 db_printf(" uref = %d\n", pr->pr_uref);
4423 db_printf(" state = %s\n",
4424 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4425 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4427 db_printf(" path = %s\n", pr->pr_path);
4428 db_printf(" cpuset = %d\n", pr->pr_cpuset
4429 ? pr->pr_cpuset->cs_id : -1);
4431 db_printf(" vnet = %p\n", pr->pr_vnet);
4433 db_printf(" root = %p\n", pr->pr_root);
4434 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4435 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4436 db_printf(" children.max = %d\n", pr->pr_childmax);
4437 db_printf(" children.cur = %d\n", pr->pr_childcount);
4438 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4439 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4440 db_printf(" flags = 0x%x", pr->pr_flags);
4441 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4442 if (pr->pr_flags & bf->flag)
4443 db_printf(" %s", bf->name);
4444 for (jsf = pr_flag_jailsys;
4445 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4447 f = pr->pr_flags & (jsf->disable | jsf->new);
4448 db_printf(" %-16s= %s\n", jsf->name,
4449 (f != 0 && f == jsf->disable) ? "disable"
4450 : (f == jsf->new) ? "new"
4453 db_printf(" allow = 0x%x", pr->pr_allow);
4454 for (bf = pr_flag_allow;
4455 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4456 atomic_load_int(&bf->flag) != 0;
4458 if (pr->pr_allow & bf->flag)
4459 db_printf(" %s", bf->name);
4461 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4462 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4463 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4464 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4465 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4467 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4468 for (ii = 0; ii < pr->pr_ip4s; ii++)
4469 db_printf(" %s %s\n",
4470 ii == 0 ? "ip4.addr =" : " ",
4471 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4474 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4475 for (ii = 0; ii < pr->pr_ip6s; ii++)
4476 db_printf(" %s %s\n",
4477 ii == 0 ? "ip6.addr =" : " ",
4478 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4482 DB_SHOW_COMMAND(prison, db_show_prison_command)
4488 * Show all prisons in the list, and prison0 which is not
4491 db_show_prison(&prison0);
4492 if (!db_pager_quit) {
4493 TAILQ_FOREACH(pr, &allprison, pr_list) {
4505 /* Look for a prison with the ID and with references. */
4506 TAILQ_FOREACH(pr, &allprison, pr_list)
4507 if (pr->pr_id == addr && pr->pr_ref > 0)
4510 /* Look again, without requiring a reference. */
4511 TAILQ_FOREACH(pr, &allprison, pr_list)
4512 if (pr->pr_id == addr)
4515 /* Assume address points to a valid prison. */
4516 pr = (struct prison *)addr;