2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2008 Bjoern A. Zeeb.
6 * Copyright (c) 2009 James Gritton.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/sysproto.h>
44 #include <sys/malloc.h>
48 #include <sys/taskqueue.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/mutex.h>
54 #include <sys/racct.h>
56 #include <sys/refcount.h>
58 #include <sys/sysent.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
71 #include <netinet/in.h>
77 #include <security/mac/mac_framework.h>
79 #define PRISON0_HOSTUUID_MODULE "hostuuid"
81 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
82 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
84 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
87 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
89 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
93 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
95 #define _PR_IP_SADDRSEL 0
99 /* prison0 describes what is "real" about the system. */
100 struct prison prison0 = {
106 .pr_securelevel = -1,
108 .pr_state = PRISON_STATE_ALIVE,
109 .pr_childmax = JAIL_MAX,
110 .pr_hostuuid = DEFAULT_HOSTUUID,
111 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
113 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
115 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
117 .pr_allow = PR_ALLOW_ALL_STATIC,
119 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
126 struct jailsys_flags {
132 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
133 struct sx allprison_lock;
134 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
135 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
136 LIST_HEAD(, prison_racct) allprison_racct;
139 static int get_next_prid(struct prison **insprp);
140 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
141 static void prison_complete(void *context, int pending);
142 static void prison_deref(struct prison *pr, int flags);
143 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
144 static int prison_lock_xlock(struct prison *pr, int flags);
145 static void prison_free_not_last(struct prison *pr);
146 static void prison_proc_free_not_last(struct prison *pr);
147 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
149 static char *prison_path(struct prison *pr1, struct prison *pr2);
151 static void prison_racct_attach(struct prison *pr);
152 static void prison_racct_modify(struct prison *pr);
153 static void prison_racct_detach(struct prison *pr);
156 /* Flags for prison_deref */
157 #define PD_DEREF 0x01 /* Decrement pr_ref */
158 #define PD_DEUREF 0x02 /* Decrement pr_uref */
159 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
160 #define PD_LOCKED 0x10 /* pr_mtx is held */
161 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
162 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
163 #define PD_OP_FLAGS 0x07 /* Operation flags */
164 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
167 * Parameter names corresponding to PR_* flag values. Size values are for kvm
168 * as we cannot figure out the size of a sparse array, or an array without a
171 static struct bool_flags pr_flag_bool[] = {
172 {"persist", "nopersist", PR_PERSIST},
174 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
177 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
180 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
182 static struct jailsys_flags pr_flag_jailsys[] = {
183 {"host", 0, PR_HOST},
185 {"vnet", 0, PR_VNET},
188 {"ip4", PR_IP4_USER, PR_IP4_USER},
191 {"ip6", PR_IP6_USER, PR_IP6_USER},
194 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
197 * Make this array full-size so dynamic parameters can be added.
198 * It is protected by prison0.mtx, but lockless reading is allowed
199 * with an atomic check of the flag values.
201 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
202 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
203 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
204 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
205 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
206 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
207 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
208 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
209 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
210 {"allow.reserved_ports", "allow.noreserved_ports",
211 PR_ALLOW_RESERVED_PORTS},
212 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
213 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
214 PR_ALLOW_UNPRIV_DEBUG},
215 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
217 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
218 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
220 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
221 PR_ALLOW_RESERVED_PORTS | \
222 PR_ALLOW_UNPRIV_DEBUG | \
224 #define JAIL_DEFAULT_ENFORCE_STATFS 2
225 #define JAIL_DEFAULT_DEVFS_RSNUM 0
226 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
227 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
228 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
229 #if defined(INET) || defined(INET6)
230 static unsigned jail_max_af_ips = 255;
234 * Initialize the parts of prison0 that can't be static-initialized with
235 * constants. This is called from proc0_init() after creating thread0 cpuset.
240 uint8_t *file, *data;
243 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
244 prison0.pr_osreldate = osreldate;
245 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
247 /* If we have a preloaded hostuuid, use it. */
248 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
250 data = preload_fetch_addr(file);
251 size = preload_fetch_size(file);
254 * The preloaded data may include trailing whitespace, almost
255 * certainly a newline; skip over any whitespace or
256 * non-printable characters to be safe.
258 while (size > 0 && data[size - 1] <= 0x20) {
261 if (validate_uuid(data, size, NULL, 0) == 0) {
262 (void)strlcpy(prison0.pr_hostuuid, data,
264 } else if (bootverbose) {
265 printf("hostuuid: preload data malformed: '%.*s'\n",
271 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
280 sys_jail(struct thread *td, struct jail_args *uap)
286 error = copyin(uap->jail, &version, sizeof(uint32_t));
295 /* FreeBSD single IPv4 jails. */
296 bzero(&j, sizeof(struct jail));
297 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
300 j.version = j0.version;
302 j.hostname = j0.hostname;
303 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
309 * Version 1 was used by multi-IPv4 jail implementations
310 * that never made it into the official kernel.
314 case 2: /* JAIL_API_VERSION */
315 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
316 error = copyin(uap->jail, &j, sizeof(struct jail));
322 /* Sci-Fi jails are not supported, sorry. */
325 return (kern_jail(td, &j));
329 kern_jail(struct thread *td, struct jail *j)
331 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
340 char *u_path, *u_hostname, *u_name;
341 struct bool_flags *bf;
344 struct in_addr *u_ip4;
347 struct in6_addr *u_ip6;
350 int error, enforce_statfs;
352 bzero(&optiov, sizeof(optiov));
353 opt.uio_iov = optiov;
357 opt.uio_segflg = UIO_SYSSPACE;
358 opt.uio_rw = UIO_READ;
361 /* Set permissions for top-level jails from sysctls. */
362 if (!jailed(td->td_ucred)) {
363 for (bf = pr_flag_allow;
364 bf < pr_flag_allow + nitems(pr_flag_allow) &&
365 atomic_load_int(&bf->flag) != 0;
367 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
368 (jail_default_allow & bf->flag)
369 ? bf->name : bf->noname);
370 optiov[opt.uio_iovcnt].iov_len =
371 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
374 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
375 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
377 enforce_statfs = jail_default_enforce_statfs;
378 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
379 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
383 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
385 ip4s = (j->version == 0) ? 1 : j->ip4s;
386 if (ip4s > jail_max_af_ips)
388 tmplen += ip4s * sizeof(struct in_addr);
394 if (j->ip6s > jail_max_af_ips)
396 tmplen += j->ip6s * sizeof(struct in6_addr);
401 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
402 u_hostname = u_path + MAXPATHLEN;
403 u_name = u_hostname + MAXHOSTNAMELEN;
405 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
409 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
411 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
414 optiov[opt.uio_iovcnt].iov_base = "path";
415 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
417 optiov[opt.uio_iovcnt].iov_base = u_path;
418 error = copyinstr(j->path, u_path, MAXPATHLEN,
419 &optiov[opt.uio_iovcnt].iov_len);
421 free(u_path, M_TEMP);
425 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
426 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
428 optiov[opt.uio_iovcnt].iov_base = u_hostname;
429 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
430 &optiov[opt.uio_iovcnt].iov_len);
432 free(u_path, M_TEMP);
436 if (j->jailname != NULL) {
437 optiov[opt.uio_iovcnt].iov_base = "name";
438 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
440 optiov[opt.uio_iovcnt].iov_base = u_name;
441 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
442 &optiov[opt.uio_iovcnt].iov_len);
444 free(u_path, M_TEMP);
450 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
451 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
453 optiov[opt.uio_iovcnt].iov_base = u_ip4;
454 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
456 u_ip4->s_addr = j->ip4s;
458 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
460 free(u_path, M_TEMP);
467 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
468 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
470 optiov[opt.uio_iovcnt].iov_base = u_ip6;
471 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
472 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
474 free(u_path, M_TEMP);
479 KASSERT(opt.uio_iovcnt <= nitems(optiov),
480 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
481 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
482 free(u_path, M_TEMP);
487 * struct jail_set_args {
488 * struct iovec *iovp;
489 * unsigned int iovcnt;
494 sys_jail_set(struct thread *td, struct jail_set_args *uap)
499 /* Check that we have an even number of iovecs. */
503 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
506 error = kern_jail_set(td, auio, uap->flags);
512 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
519 struct in6_addr *ip6;
522 struct vfsoptlist *opts;
523 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
525 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
526 char *g_path, *osrelstr;
527 struct bool_flags *bf;
528 struct jailsys_flags *jsf;
529 #if defined(INET) || defined(INET6)
534 size_t namelen, onamelen, pnamelen;
535 int born, created, cuflags, descend, drflags, enforce;
536 int error, errmsg_len, errmsg_pos;
537 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
538 int jid, jsys, len, level;
539 int childmax, osreldt, rsnum, slevel;
540 #if defined(INET) || defined(INET6)
549 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
550 uint64_t pr_allow_diff;
554 error = priv_check(td, PRIV_JAIL_SET);
555 if (!error && (flags & JAIL_ATTACH))
556 error = priv_check(td, PRIV_JAIL_ATTACH);
559 mypr = td->td_ucred->cr_prison;
560 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
562 if (flags & ~JAIL_SET_MASK)
566 * Check all the parameters before committing to anything. Not all
567 * errors can be caught early, but we may as well try. Also, this
568 * takes care of some expensive stuff (path lookup) before getting
569 * the allprison lock.
571 * XXX Jails are not filesystems, and jail parameters are not mount
572 * options. But it makes more sense to re-use the vfsopt code
573 * than duplicate it under a different name.
575 error = vfs_buildopts(optuio, &opts);
586 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
589 vfs_opterror(opts, "no valid operation (create or update)");
593 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
599 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
608 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
616 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
621 else if (enforce < 0 || enforce > 2) {
627 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
635 pr_flags = ch_flags = 0;
636 for (bf = pr_flag_bool;
637 bf < pr_flag_bool + nitems(pr_flag_bool);
639 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
640 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
642 ch_flags |= pr_flags;
643 for (jsf = pr_flag_jailsys;
644 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
646 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
652 case JAIL_SYS_DISABLE:
657 pr_flags |= jsf->disable;
660 pr_flags |= jsf->new;
662 case JAIL_SYS_INHERIT:
668 ch_flags |= jsf->new | jsf->disable;
670 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
671 && !(pr_flags & PR_PERSIST)) {
673 vfs_opterror(opts, "new jail must persist or attach");
677 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
679 vfs_opterror(opts, "vnet cannot be changed after creation");
684 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
686 vfs_opterror(opts, "ip4 cannot be changed after creation");
691 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
693 vfs_opterror(opts, "ip6 cannot be changed after creation");
698 pr_allow = ch_allow = 0;
699 for (bf = pr_flag_allow;
700 bf < pr_flag_allow + nitems(pr_flag_allow) &&
701 atomic_load_int(&bf->flag) != 0;
703 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
704 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
706 ch_allow |= pr_allow;
708 error = vfs_getopt(opts, "name", (void **)&name, &len);
714 if (len == 0 || name[len - 1] != '\0') {
718 if (len > MAXHOSTNAMELEN) {
719 error = ENAMETOOLONG;
724 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
732 if (len == 0 || host[len - 1] != '\0') {
736 if (len > MAXHOSTNAMELEN) {
737 error = ENAMETOOLONG;
742 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
750 if (len == 0 || domain[len - 1] != '\0') {
754 if (len > MAXHOSTNAMELEN) {
755 error = ENAMETOOLONG;
760 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
768 if (len == 0 || uuid[len - 1] != '\0') {
772 if (len > HOSTUUIDLEN) {
773 error = ENAMETOOLONG;
778 #ifdef COMPAT_FREEBSD32
779 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
782 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
786 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
798 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
803 else if (ip4s & (sizeof(*ip4) - 1)) {
807 ch_flags |= PR_IP4_USER;
808 pr_flags |= PR_IP4_USER;
810 ip4s /= sizeof(*ip4);
811 if (ip4s > jail_max_af_ips) {
813 vfs_opterror(opts, "too many IPv4 addresses");
816 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
817 bcopy(op, ip4, ip4s * sizeof(*ip4));
819 * IP addresses are all sorted but ip[0] to preserve
820 * the primary IP address as given from userland.
821 * This special IP is used for unbound outgoing
822 * connections as well for "loopback" traffic in case
823 * source address selection cannot find any more fitting
824 * address to connect from.
827 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
830 * Check for duplicate addresses and do some simple
831 * zero and broadcast checks. If users give other bogus
832 * addresses it is their problem.
834 * We do not have to care about byte order for these
835 * checks so we will do them in NBO.
837 for (ii = 0; ii < ip4s; ii++) {
838 if (ip4[ii].s_addr == INADDR_ANY ||
839 ip4[ii].s_addr == INADDR_BROADCAST) {
844 (ip4[0].s_addr == ip4[ii+1].s_addr ||
845 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
855 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
860 else if (ip6s & (sizeof(*ip6) - 1)) {
864 ch_flags |= PR_IP6_USER;
865 pr_flags |= PR_IP6_USER;
867 ip6s /= sizeof(*ip6);
868 if (ip6s > jail_max_af_ips) {
870 vfs_opterror(opts, "too many IPv6 addresses");
873 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
874 bcopy(op, ip6, ip6s * sizeof(*ip6));
876 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
878 for (ii = 0; ii < ip6s; ii++) {
879 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
884 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
885 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
895 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
896 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
899 "vnet jails cannot have IP address restrictions");
904 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
910 if (flags & JAIL_UPDATE) {
913 "osrelease cannot be changed after creation");
916 if (len == 0 || osrelstr[len - 1] != '\0') {
920 if (len >= OSRELEASELEN) {
921 error = ENAMETOOLONG;
923 "osrelease string must be 1-%d bytes long",
929 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
935 if (flags & JAIL_UPDATE) {
938 "osreldate cannot be changed after creation");
943 vfs_opterror(opts, "osreldate cannot be 0");
949 error = vfs_getopt(opts, "path", (void **)&path, &len);
955 if (flags & JAIL_UPDATE) {
958 "path cannot be changed after creation");
961 if (len == 0 || path[len - 1] != '\0') {
965 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
970 NDFREE(&nd, NDF_ONLY_PNBUF);
971 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
972 strlcpy(g_path, path, MAXPATHLEN);
973 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
977 /* exit on other errors */
980 if (root->v_type != VDIR) {
989 * Find the specified jail, or at least its parent.
990 * This abuses the file error codes ENOENT and EEXIST.
994 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
995 namelc = strrchr(name, '.');
996 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1000 sx_xlock(&allprison_lock);
1001 drflags = PD_LIST_XLOCKED;
1003 if (!prison_isalive(ppr)) {
1004 /* This jail is dying. This process will surely follow. */
1011 vfs_opterror(opts, "negative jid");
1015 * See if a requested jid already exists. Keep track of
1016 * where it can be inserted later.
1018 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1019 if (inspr->pr_id < jid)
1021 if (inspr->pr_id > jid)
1024 mtx_lock(&pr->pr_mtx);
1025 drflags |= PD_LOCKED;
1030 /* Create: jid must not exist. */
1031 if (cuflags == JAIL_CREATE) {
1033 * Even creators that cannot see the jail will
1037 vfs_opterror(opts, "jail %d already exists",
1041 if (!prison_ischild(mypr, pr)) {
1043 * Updaters get ENOENT if they cannot see the
1044 * jail. This is true even for CREATE | UPDATE,
1045 * which normally cannot give this error.
1048 vfs_opterror(opts, "jail %d not found", jid);
1051 ppr = pr->pr_parent;
1052 if (!prison_isalive(ppr)) {
1054 vfs_opterror(opts, "jail %d is dying",
1058 if (!prison_isalive(pr)) {
1059 if (!(flags & JAIL_DYING)) {
1061 vfs_opterror(opts, "jail %d is dying",
1065 if ((flags & JAIL_ATTACH) ||
1066 (pr_flags & PR_PERSIST)) {
1068 * A dying jail might be resurrected
1069 * (via attach or persist), but first
1070 * it must determine if another jail
1071 * has claimed its name. Accomplish
1072 * this by implicitly re-setting the
1076 name = prison_name(mypr, pr);
1080 /* Update: jid must exist. */
1081 if (cuflags == JAIL_UPDATE) {
1083 vfs_opterror(opts, "jail %d not found", jid);
1089 * If the caller provided a name, look for a jail by that name.
1090 * This has different semantics for creates and updates keyed by jid
1091 * (where the name must not already exist in a different jail),
1092 * and updates keyed by the name itself (where the name must exist
1093 * because that is the jail being updated).
1097 namelc = strrchr(name, '.');
1102 * This is a hierarchical name. Split it into the
1103 * parent and child names, and make sure the parent
1104 * exists or matches an already found jail.
1107 if (strncmp(name, ppr->pr_name, namelc - name)
1108 || ppr->pr_name[namelc - name] != '\0') {
1111 "cannot change jail's parent");
1116 ppr = prison_find_name(mypr, name);
1120 "jail \"%s\" not found", name);
1123 mtx_unlock(&ppr->pr_mtx);
1124 if (!prison_isalive(ppr)) {
1127 "jail \"%s\" is dying", name);
1134 if (namelc[0] != '\0') {
1136 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1138 FOREACH_PRISON_CHILD(ppr, tpr) {
1140 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1141 if (prison_isalive(tpr)) {
1143 cuflags != JAIL_CREATE) {
1149 mtx_lock(&pr->pr_mtx);
1150 drflags |= PD_LOCKED;
1154 * Create, or update(jid):
1155 * name must not exist in an
1156 * active sibling jail.
1160 "jail \"%s\" already exists",
1165 cuflags != JAIL_CREATE) {
1170 /* If no active jail is found, use a dying one. */
1171 if (deadpr != NULL && pr == NULL) {
1172 if (flags & JAIL_DYING) {
1174 mtx_lock(&pr->pr_mtx);
1175 drflags |= PD_LOCKED;
1176 } else if (cuflags == JAIL_UPDATE) {
1179 "jail \"%s\" is dying", name);
1183 /* Update: name must exist if no jid. */
1184 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1186 vfs_opterror(opts, "jail \"%s\" not found",
1192 /* Update: must provide a jid or name. */
1193 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1195 vfs_opterror(opts, "update specified no jail");
1199 /* If there's no prison to update, create a new one and link it in. */
1200 created = pr == NULL;
1202 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1203 if (tpr->pr_childcount >= tpr->pr_childmax) {
1205 vfs_opterror(opts, "prison limit exceeded");
1208 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1210 vfs_opterror(opts, "no available jail IDs");
1214 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1215 pr->pr_state = PRISON_STATE_INVALID;
1216 refcount_init(&pr->pr_ref, 1);
1217 refcount_init(&pr->pr_uref, 0);
1218 drflags |= PD_DEREF;
1219 LIST_INIT(&pr->pr_children);
1220 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1221 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1225 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1227 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1229 pr->pr_parent = ppr;
1231 prison_proc_hold(ppr);
1232 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1233 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1234 tpr->pr_childcount++;
1236 /* Set some default values, and inherit some from the parent. */
1241 root = mypr->pr_root;
1244 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1245 pr->pr_flags |= PR_HOST;
1246 #if defined(INET) || defined(INET6)
1248 if (!(pr_flags & PR_VNET))
1252 if (!(ch_flags & PR_IP4_USER))
1253 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1254 else if (!(pr_flags & PR_IP4_USER)) {
1255 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1256 if (ppr->pr_ip4 != NULL) {
1257 pr->pr_ip4s = ppr->pr_ip4s;
1258 pr->pr_ip4 = malloc(pr->pr_ip4s *
1259 sizeof(struct in_addr), M_PRISON,
1261 bcopy(ppr->pr_ip4, pr->pr_ip4,
1262 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1267 if (!(ch_flags & PR_IP6_USER))
1268 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1269 else if (!(pr_flags & PR_IP6_USER)) {
1270 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1271 if (ppr->pr_ip6 != NULL) {
1272 pr->pr_ip6s = ppr->pr_ip6s;
1273 pr->pr_ip6 = malloc(pr->pr_ip6s *
1274 sizeof(struct in6_addr), M_PRISON,
1276 bcopy(ppr->pr_ip6, pr->pr_ip6,
1277 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1283 /* Source address selection is always on by default. */
1284 pr->pr_flags |= _PR_IP_SADDRSEL;
1286 pr->pr_securelevel = ppr->pr_securelevel;
1287 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1288 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1289 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1291 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1292 if (osrelstr == NULL)
1293 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1294 sizeof(pr->pr_osrelease));
1296 strlcpy(pr->pr_osrelease, osrelstr,
1297 sizeof(pr->pr_osrelease));
1300 /* Allocate a new vnet if specified. */
1301 pr->pr_vnet = (pr_flags & PR_VNET)
1302 ? vnet_alloc() : ppr->pr_vnet;
1305 * Allocate a dedicated cpuset for each jail.
1306 * Unlike other initial settings, this may return an error.
1308 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1312 mtx_lock(&pr->pr_mtx);
1313 drflags |= PD_LOCKED;
1316 * Grab a reference for existing prisons, to ensure they
1317 * continue to exist for the duration of the call.
1320 drflags |= PD_DEREF;
1321 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1322 if ((pr->pr_flags & PR_VNET) &&
1323 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1326 "vnet jails cannot have IP address restrictions");
1331 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1334 "ip4 cannot be changed after creation");
1339 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1342 "ip6 cannot be changed after creation");
1348 /* Do final error checking before setting anything. */
1350 if (slevel < ppr->pr_securelevel) {
1356 if (childmax >= ppr->pr_childmax) {
1362 if (enforce < ppr->pr_enforce_statfs) {
1369 * devfs_rsnum is a uint16_t
1371 if (rsnum < 0 || rsnum > 65535) {
1376 * Nested jails always inherit parent's devfs ruleset
1378 if (jailed(td->td_ucred)) {
1379 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1383 rsnum = ppr->pr_devfs_rsnum;
1388 if (ppr->pr_flags & PR_IP4) {
1390 * Make sure the new set of IP addresses is a
1391 * subset of the parent's list. Don't worry
1392 * about the parent being unlocked, as any
1393 * setting is done with allprison_lock held.
1395 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1396 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1398 if (ij == ppr->pr_ip4s) {
1403 for (ii = ij = 1; ii < ip4s; ii++) {
1404 if (ip4[ii].s_addr ==
1405 ppr->pr_ip4[0].s_addr)
1407 for (; ij < ppr->pr_ip4s; ij++)
1408 if (ip4[ii].s_addr ==
1409 ppr->pr_ip4[ij].s_addr)
1411 if (ij == ppr->pr_ip4s)
1414 if (ij == ppr->pr_ip4s) {
1421 * Check for conflicting IP addresses. We permit them
1422 * if there is no more than one IP on each jail. If
1423 * there is a duplicate on a jail with more than one
1424 * IP stop checking and return error.
1427 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1428 if (tppr->pr_flags & PR_VNET)
1433 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1436 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1438 !prison_isalive(tpr)) {
1442 if (!(tpr->pr_flags & PR_IP4_USER))
1445 if (tpr->pr_ip4 == NULL ||
1446 (ip4s == 1 && tpr->pr_ip4s == 1))
1448 for (ii = 0; ii < ip4s; ii++) {
1449 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1453 "IPv4 addresses clash");
1462 if (ppr->pr_flags & PR_IP6) {
1464 * Make sure the new set of IP addresses is a
1465 * subset of the parent's list.
1467 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1468 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1471 if (ij == ppr->pr_ip6s) {
1476 for (ii = ij = 1; ii < ip6s; ii++) {
1477 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1480 for (; ij < ppr->pr_ip6s; ij++)
1481 if (IN6_ARE_ADDR_EQUAL(
1482 &ip6[ii], &ppr->pr_ip6[ij]))
1484 if (ij == ppr->pr_ip6s)
1487 if (ij == ppr->pr_ip6s) {
1493 /* Check for conflicting IP addresses. */
1495 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1496 if (tppr->pr_flags & PR_VNET)
1501 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1504 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1506 !prison_isalive(tpr)) {
1510 if (!(tpr->pr_flags & PR_IP6_USER))
1513 if (tpr->pr_ip6 == NULL ||
1514 (ip6s == 1 && tpr->pr_ip6s == 1))
1516 for (ii = 0; ii < ip6s; ii++) {
1517 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1521 "IPv6 addresses clash");
1528 onamelen = namelen = 0;
1529 if (namelc != NULL) {
1530 /* Give a default name of the jid. Also allow the name to be
1531 * explicitly the jid - but not any other number, and only in
1532 * normal form (no leading zero/etc).
1534 if (namelc[0] == '\0')
1535 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1536 else if ((strtoul(namelc, &p, 10) != jid ||
1537 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1540 "name cannot be numeric (unless it is the jid)");
1544 * Make sure the name isn't too long for the prison or its
1547 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1548 onamelen = strlen(pr->pr_name + pnamelen);
1549 namelen = strlen(namelc);
1550 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1551 error = ENAMETOOLONG;
1554 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1555 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1556 sizeof(pr->pr_name)) {
1557 error = ENAMETOOLONG;
1562 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1563 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1569 * Let modules check their parameters. This requires unlocking and
1570 * then re-locking the prison, but this is still a valid state as long
1571 * as allprison_lock remains xlocked.
1573 mtx_unlock(&pr->pr_mtx);
1574 drflags &= ~PD_LOCKED;
1575 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1578 mtx_lock(&pr->pr_mtx);
1579 drflags |= PD_LOCKED;
1581 /* At this point, all valid parameters should have been noted. */
1582 TAILQ_FOREACH(opt, opts, link) {
1583 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1585 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1590 /* Set the parameters of the prison. */
1593 if (pr_flags & PR_IP4_USER) {
1594 pr->pr_flags |= PR_IP4;
1595 free(pr->pr_ip4, M_PRISON);
1599 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1601 if (tpr->pr_flags & PR_VNET) {
1606 if (prison_restrict_ip4(tpr, NULL)) {
1615 if (pr_flags & PR_IP6_USER) {
1616 pr->pr_flags |= PR_IP6;
1617 free(pr->pr_ip6, M_PRISON);
1621 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1623 if (tpr->pr_flags & PR_VNET) {
1628 if (prison_restrict_ip6(tpr, NULL)) {
1636 pr->pr_securelevel = slevel;
1637 /* Set all child jails to be at least this level. */
1638 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1639 if (tpr->pr_securelevel < slevel)
1640 tpr->pr_securelevel = slevel;
1643 pr->pr_childmax = childmax;
1644 /* Set all child jails to under this limit. */
1645 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1646 if (tpr->pr_childmax > childmax - level)
1647 tpr->pr_childmax = childmax > level
1648 ? childmax - level : 0;
1651 pr->pr_enforce_statfs = enforce;
1652 /* Pass this restriction on to the children. */
1653 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1654 if (tpr->pr_enforce_statfs < enforce)
1655 tpr->pr_enforce_statfs = enforce;
1658 pr->pr_devfs_rsnum = rsnum;
1659 /* Pass this restriction on to the children. */
1660 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1661 tpr->pr_devfs_rsnum = rsnum;
1663 if (namelc != NULL) {
1664 if (ppr == &prison0)
1665 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1667 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1668 ppr->pr_name, namelc);
1669 /* Change this component of child names. */
1670 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1671 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1672 strlen(tpr->pr_name + onamelen) + 1);
1673 bcopy(pr->pr_name, tpr->pr_name, namelen);
1677 /* Try to keep a real-rooted full pathname. */
1678 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1682 if (PR_HOST & ch_flags & ~pr_flags) {
1683 if (pr->pr_flags & PR_HOST) {
1685 * Copy the parent's host info. As with pr_ip4 above,
1686 * the lack of a lock on the parent is not a problem;
1687 * it is always set with allprison_lock at least
1688 * shared, and is held exclusively here.
1690 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1691 sizeof(pr->pr_hostname));
1692 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1693 sizeof(pr->pr_domainname));
1694 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1695 sizeof(pr->pr_hostuuid));
1696 pr->pr_hostid = pr->pr_parent->pr_hostid;
1698 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1699 /* Set this prison, and any descendants without PR_HOST. */
1701 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1703 strlcpy(pr->pr_domainname, domain,
1704 sizeof(pr->pr_domainname));
1706 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1708 pr->pr_hostid = hid;
1709 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1710 if (tpr->pr_flags & PR_HOST)
1714 strlcpy(tpr->pr_hostname,
1716 sizeof(tpr->pr_hostname));
1718 strlcpy(tpr->pr_domainname,
1720 sizeof(tpr->pr_domainname));
1722 strlcpy(tpr->pr_hostuuid,
1724 sizeof(tpr->pr_hostuuid));
1726 tpr->pr_hostid = hid;
1730 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1731 if ((tallow = ch_allow & ~pr_allow))
1732 prison_set_allow_locked(pr, tallow, 0);
1734 * Persistent prisons get an extra reference, and prisons losing their
1735 * persist flag lose that reference.
1737 born = !prison_isalive(pr);
1738 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1739 if (pr_flags & PR_PERSIST) {
1742 * This may make a dead prison alive again, but wait
1743 * to label it as such until after OSD calls have had
1744 * a chance to run (and perhaps to fail).
1746 refcount_acquire(&pr->pr_uref);
1748 drflags |= PD_DEUREF;
1749 prison_free_not_last(pr);
1752 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1753 mtx_unlock(&pr->pr_mtx);
1754 drflags &= ~PD_LOCKED;
1756 * Any errors past this point will need to de-persist newly created
1757 * prisons, as well as call remove methods.
1763 if (racct_enable && created)
1764 prison_racct_attach(pr);
1767 /* Locks may have prevented a complete restriction of child IP
1768 * addresses. If so, allocate some more memory and try again.
1773 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1774 mtx_lock(&pr->pr_mtx);
1776 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1778 if (tpr->pr_flags & PR_VNET) {
1783 if (prison_restrict_ip4(tpr, ip4)) {
1790 mtx_unlock(&pr->pr_mtx);
1796 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1797 mtx_lock(&pr->pr_mtx);
1799 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1801 if (tpr->pr_flags & PR_VNET) {
1806 if (prison_restrict_ip6(tpr, ip6)) {
1813 mtx_unlock(&pr->pr_mtx);
1817 /* Let the modules do their work. */
1819 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1823 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1828 * A new prison is now ready to be seen; either it has gained a user
1829 * reference via persistence, or is about to gain one via attachment.
1832 drflags = prison_lock_xlock(pr, drflags);
1833 pr->pr_state = PRISON_STATE_ALIVE;
1836 /* Attach this process to the prison if requested. */
1837 if (flags & JAIL_ATTACH) {
1838 error = do_jail_attach(td, pr,
1839 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
1840 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1842 vfs_opterror(opts, "attach failed");
1848 if (racct_enable && !created) {
1849 if (drflags & PD_LOCKED) {
1850 mtx_unlock(&pr->pr_mtx);
1851 drflags &= ~PD_LOCKED;
1853 if (drflags & PD_LIST_XLOCKED) {
1854 sx_xunlock(&allprison_lock);
1855 drflags &= ~PD_LIST_XLOCKED;
1857 prison_racct_modify(pr);
1861 drflags &= ~PD_KILL;
1862 td->td_retval[0] = pr->pr_id;
1865 /* Release any temporary prison holds and/or locks. */
1867 prison_deref(pr, drflags);
1868 else if (drflags & PD_LIST_SLOCKED)
1869 sx_sunlock(&allprison_lock);
1870 else if (drflags & PD_LIST_XLOCKED)
1871 sx_xunlock(&allprison_lock);
1876 /* Write the error message back to userspace. */
1877 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1878 &errmsg_len) == 0 && errmsg_len > 0) {
1879 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1880 if (optuio->uio_segflg == UIO_SYSSPACE)
1882 optuio->uio_iov[errmsg_pos].iov_base,
1886 optuio->uio_iov[errmsg_pos].iov_base,
1892 free(ip4, M_PRISON);
1895 free(ip6, M_PRISON);
1898 free(g_path, M_TEMP);
1904 * Find the next available prison ID. Return the ID on success, or zero
1905 * on failure. Also set a pointer to the allprison list entry the prison
1906 * should be inserted before.
1909 get_next_prid(struct prison **insprp)
1911 struct prison *inspr;
1914 jid = lastprid % JAIL_MAX + 1;
1915 if (TAILQ_EMPTY(&allprison) ||
1916 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1918 * A common case is for all jails to be implicitly numbered,
1919 * which means they'll go on the end of the list, at least
1920 * for the first JAIL_MAX times.
1925 * Take two passes through the allprison list: first starting
1926 * with the proposed jid, then ending with it.
1928 for (maxid = JAIL_MAX; maxid != 0; ) {
1929 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1930 if (inspr->pr_id < jid)
1932 if (inspr->pr_id > jid) {
1933 /* Found an opening. */
1937 if (++jid > maxid) {
1938 if (lastprid == maxid || lastprid == 0)
1941 * The entire legal range
1942 * has been traversed
1946 /* Try again from the start. */
1952 if (inspr == NULL) {
1953 /* Found room at the end of the list. */
1964 * struct jail_get_args {
1965 * struct iovec *iovp;
1966 * unsigned int iovcnt;
1971 sys_jail_get(struct thread *td, struct jail_get_args *uap)
1976 /* Check that we have an even number of iovecs. */
1977 if (uap->iovcnt & 1)
1980 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1983 error = kern_jail_get(td, auio, uap->flags);
1985 error = copyout(auio->uio_iov, uap->iovp,
1986 uap->iovcnt * sizeof (struct iovec));
1992 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1994 struct bool_flags *bf;
1995 struct jailsys_flags *jsf;
1996 struct prison *pr, *mypr;
1998 struct vfsoptlist *opts;
1999 char *errmsg, *name;
2000 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2003 if (flags & ~JAIL_GET_MASK)
2006 /* Get the parameter list. */
2007 error = vfs_buildopts(optuio, &opts);
2010 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2011 mypr = td->td_ucred->cr_prison;
2015 * Find the prison specified by one of: lastjid, jid, name.
2017 sx_slock(&allprison_lock);
2018 drflags = PD_LIST_SLOCKED;
2019 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2021 TAILQ_FOREACH(pr, &allprison, pr_list) {
2022 if (pr->pr_id > jid &&
2023 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2024 prison_ischild(mypr, pr)) {
2025 mtx_lock(&pr->pr_mtx);
2026 drflags |= PD_LOCKED;
2031 vfs_opterror(opts, "no jail after %d", jid);
2033 } else if (error != ENOENT)
2036 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2039 pr = prison_find_child(mypr, jid);
2041 drflags |= PD_LOCKED;
2042 if (!(prison_isalive(pr) ||
2043 (flags & JAIL_DYING))) {
2045 vfs_opterror(opts, "jail %d is dying",
2052 vfs_opterror(opts, "jail %d not found", jid);
2055 } else if (error != ENOENT)
2058 error = vfs_getopt(opts, "name", (void **)&name, &len);
2060 if (len == 0 || name[len - 1] != '\0') {
2064 pr = prison_find_name(mypr, name);
2066 drflags |= PD_LOCKED;
2067 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2069 vfs_opterror(opts, "jail \"%s\" is dying",
2076 vfs_opterror(opts, "jail \"%s\" not found", name);
2078 } else if (error != ENOENT)
2081 vfs_opterror(opts, "no jail specified");
2086 /* Get the parameters of the prison. */
2088 drflags |= PD_DEREF;
2089 td->td_retval[0] = pr->pr_id;
2090 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2091 if (error != 0 && error != ENOENT)
2093 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2094 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2095 if (error != 0 && error != ENOENT)
2097 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2098 if (error != 0 && error != ENOENT)
2100 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2101 sizeof(pr->pr_cpuset->cs_id));
2102 if (error != 0 && error != ENOENT)
2104 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2105 if (error != 0 && error != ENOENT)
2108 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2109 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2110 if (error != 0 && error != ENOENT)
2114 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2115 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2116 if (error != 0 && error != ENOENT)
2119 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2120 sizeof(pr->pr_securelevel));
2121 if (error != 0 && error != ENOENT)
2123 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2124 sizeof(pr->pr_childcount));
2125 if (error != 0 && error != ENOENT)
2127 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2128 sizeof(pr->pr_childmax));
2129 if (error != 0 && error != ENOENT)
2131 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2132 if (error != 0 && error != ENOENT)
2134 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2135 if (error != 0 && error != ENOENT)
2137 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2138 if (error != 0 && error != ENOENT)
2140 #ifdef COMPAT_FREEBSD32
2141 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2142 uint32_t hid32 = pr->pr_hostid;
2144 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2147 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2148 sizeof(pr->pr_hostid));
2149 if (error != 0 && error != ENOENT)
2151 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2152 sizeof(pr->pr_enforce_statfs));
2153 if (error != 0 && error != ENOENT)
2155 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2156 sizeof(pr->pr_devfs_rsnum));
2157 if (error != 0 && error != ENOENT)
2159 for (bf = pr_flag_bool;
2160 bf < pr_flag_bool + nitems(pr_flag_bool);
2162 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2163 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2164 if (error != 0 && error != ENOENT)
2167 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2168 if (error != 0 && error != ENOENT)
2171 for (jsf = pr_flag_jailsys;
2172 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2174 f = pr->pr_flags & (jsf->disable | jsf->new);
2175 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2176 : (f == jsf->new) ? JAIL_SYS_NEW
2178 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2179 if (error != 0 && error != ENOENT)
2182 for (bf = pr_flag_allow;
2183 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2184 atomic_load_int(&bf->flag) != 0;
2186 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2187 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2188 if (error != 0 && error != ENOENT)
2191 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2192 if (error != 0 && error != ENOENT)
2195 i = !prison_isalive(pr);
2196 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2197 if (error != 0 && error != ENOENT)
2200 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2201 if (error != 0 && error != ENOENT)
2203 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2204 sizeof(pr->pr_osreldate));
2205 if (error != 0 && error != ENOENT)
2207 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2208 if (error != 0 && error != ENOENT)
2211 /* Get the module parameters. */
2212 mtx_unlock(&pr->pr_mtx);
2213 drflags &= ~PD_LOCKED;
2214 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2217 prison_deref(pr, drflags);
2221 /* By now, all parameters should have been noted. */
2222 TAILQ_FOREACH(opt, opts, link) {
2223 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2225 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2230 /* Write the fetched parameters back to userspace. */
2232 TAILQ_FOREACH(opt, opts, link) {
2233 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2234 pos = 2 * opt->pos + 1;
2235 optuio->uio_iov[pos].iov_len = opt->len;
2236 if (opt->value != NULL) {
2237 if (optuio->uio_segflg == UIO_SYSSPACE) {
2239 optuio->uio_iov[pos].iov_base,
2242 error = copyout(opt->value,
2243 optuio->uio_iov[pos].iov_base,
2253 /* Release any temporary prison holds and/or locks. */
2255 prison_deref(pr, drflags);
2256 else if (drflags & PD_LIST_SLOCKED)
2257 sx_sunlock(&allprison_lock);
2258 if (error && errmsg_pos >= 0) {
2259 /* Write the error message back to userspace. */
2260 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2261 errmsg_pos = 2 * errmsg_pos + 1;
2262 if (errmsg_len > 0) {
2263 if (optuio->uio_segflg == UIO_SYSSPACE)
2265 optuio->uio_iov[errmsg_pos].iov_base,
2269 optuio->uio_iov[errmsg_pos].iov_base,
2278 * struct jail_remove_args {
2283 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2288 error = priv_check(td, PRIV_JAIL_REMOVE);
2292 sx_xlock(&allprison_lock);
2293 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2295 sx_xunlock(&allprison_lock);
2298 if (!prison_isalive(pr)) {
2299 /* Silently ignore already-dying prisons. */
2300 mtx_unlock(&pr->pr_mtx);
2301 sx_xunlock(&allprison_lock);
2304 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2309 * struct jail_attach_args {
2314 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2319 error = priv_check(td, PRIV_JAIL_ATTACH);
2323 sx_slock(&allprison_lock);
2324 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2326 sx_sunlock(&allprison_lock);
2330 /* Do not allow a process to attach to a prison that is not alive. */
2331 if (!prison_isalive(pr)) {
2332 mtx_unlock(&pr->pr_mtx);
2333 sx_sunlock(&allprison_lock);
2337 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2341 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2344 struct ucred *newcred, *oldcred;
2347 mtx_assert(&pr->pr_mtx, MA_OWNED);
2348 sx_assert(&allprison_lock, SX_LOCKED);
2349 drflags &= PD_LOCK_FLAGS;
2351 * XXX: Note that there is a slight race here if two threads
2352 * in the same privileged process attempt to attach to two
2353 * different jails at the same time. It is important for
2354 * user processes not to do this, or they might end up with
2355 * a process root from one prison, but attached to the jail
2359 refcount_acquire(&pr->pr_uref);
2360 drflags |= PD_DEREF | PD_DEUREF;
2361 mtx_unlock(&pr->pr_mtx);
2362 drflags &= ~PD_LOCKED;
2364 /* Let modules do whatever they need to prepare for attaching. */
2365 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2367 prison_deref(pr, drflags);
2370 sx_unlock(&allprison_lock);
2371 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2374 * Reparent the newly attached process to this jail.
2377 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2381 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2382 if ((error = change_dir(pr->pr_root, td)) != 0)
2385 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2388 VOP_UNLOCK(pr->pr_root);
2389 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2394 oldcred = crcopysafe(p, newcred);
2395 newcred->cr_prison = pr;
2396 proc_set_cred(p, newcred);
2399 racct_proc_ucred_changed(p, oldcred, newcred);
2404 rctl_proc_ucred_changed(p, newcred);
2407 prison_deref(oldcred->cr_prison, drflags);
2411 * If the prison was killed while changing credentials, die along
2414 if (!prison_isalive(pr)) {
2416 kern_psignal(p, SIGKILL);
2423 VOP_UNLOCK(pr->pr_root);
2425 /* Tell modules this thread is still in its old jail after all. */
2426 sx_slock(&allprison_lock);
2427 drflags |= PD_LIST_SLOCKED;
2428 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2429 prison_deref(pr, drflags);
2434 * Returns a locked prison instance, or NULL on failure.
2437 prison_find(int prid)
2441 sx_assert(&allprison_lock, SX_LOCKED);
2442 TAILQ_FOREACH(pr, &allprison, pr_list) {
2443 if (pr->pr_id < prid)
2445 if (pr->pr_id > prid)
2447 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2448 mtx_lock(&pr->pr_mtx);
2455 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2458 prison_find_child(struct prison *mypr, int prid)
2463 sx_assert(&allprison_lock, SX_LOCKED);
2464 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2465 if (pr->pr_id == prid) {
2466 KASSERT(prison_isvalid(pr),
2467 ("Found invalid prison %p", pr));
2468 mtx_lock(&pr->pr_mtx);
2476 * Look for the name relative to mypr. Returns a locked prison or NULL.
2479 prison_find_name(struct prison *mypr, const char *name)
2481 struct prison *pr, *deadpr;
2485 sx_assert(&allprison_lock, SX_LOCKED);
2486 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2488 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2489 if (!strcmp(pr->pr_name + mylen, name)) {
2490 KASSERT(prison_isvalid(pr),
2491 ("Found invalid prison %p", pr));
2492 if (prison_isalive(pr)) {
2493 mtx_lock(&pr->pr_mtx);
2499 /* There was no valid prison - perhaps there was a dying one. */
2501 mtx_lock(&deadpr->pr_mtx);
2506 * See if a prison has the specific flag set. The prison should be locked,
2507 * unless checking for flags that are only set at jail creation (such as
2508 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2509 * to any other prison data.
2512 prison_flag(struct ucred *cred, unsigned flag)
2515 return (cred->cr_prison->pr_flags & flag);
2519 prison_allow(struct ucred *cred, unsigned flag)
2522 return ((cred->cr_prison->pr_allow & flag) != 0);
2526 * Hold a prison reference, by incrementing pr_ref. It is generally
2527 * an error to hold a prison that does not already have a reference.
2528 * A prison record will remain valid as long as it has at least one
2529 * reference, and will not be removed as long as either the prison
2530 * mutex or the allprison lock is held (allprison_lock may be shared).
2533 prison_hold_locked(struct prison *pr)
2536 /* Locking is no longer required. */
2541 prison_hold(struct prison *pr)
2544 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2547 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2549 refcount_acquire(&pr->pr_ref);
2554 * Remove a prison reference. If that was the last reference, the
2555 * prison will be removed (at a later time).
2558 prison_free_locked(struct prison *pr)
2561 mtx_assert(&pr->pr_mtx, MA_OWNED);
2563 * Locking is no longer required, but unlock because the caller
2566 mtx_unlock(&pr->pr_mtx);
2571 prison_free(struct prison *pr)
2574 KASSERT(refcount_load(&pr->pr_ref) > 0,
2575 ("Trying to free dead prison %p (jid=%d).",
2577 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2579 * Don't remove the last reference in this context,
2580 * in case there are locks held.
2582 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2587 prison_free_not_last(struct prison *pr)
2592 KASSERT(refcount_load(&pr->pr_ref) > 0,
2593 ("Trying to free dead prison %p (jid=%d).",
2595 lastref = refcount_release(&pr->pr_ref);
2597 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2600 refcount_release(&pr->pr_ref);
2605 * Hold a a prison for user visibility, by incrementing pr_uref.
2606 * It is generally an error to hold a prison that isn't already
2607 * user-visible, except through the the jail system calls. It is also
2608 * an error to hold an invalid prison. A prison record will remain
2609 * alive as long as it has at least one user reference, and will not
2610 * be set to the dying state until the prison mutex and allprison_lock
2614 prison_proc_hold(struct prison *pr)
2617 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2620 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2622 refcount_acquire(&pr->pr_uref);
2627 * Remove a prison user reference. If it was the last reference, the
2628 * prison will be considered "dying", and may be removed once all of
2629 * its references are dropped.
2632 prison_proc_free(struct prison *pr)
2636 * Locking is only required when releasing the last reference.
2637 * This allows assurance that a locked prison will remain alive
2638 * until it is unlocked.
2640 KASSERT(refcount_load(&pr->pr_uref) > 0,
2641 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2642 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2644 * Don't remove the last user reference in this context,
2645 * which is expected to be a process that is not only locked,
2646 * but also half dead. Add a reference so any calls to
2647 * prison_free() won't re-submit the task.
2650 mtx_lock(&pr->pr_mtx);
2651 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2652 ("Redundant last reference in prison_proc_free (jid=%d)",
2654 pr->pr_flags |= PR_COMPLETE_PROC;
2655 mtx_unlock(&pr->pr_mtx);
2656 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2661 prison_proc_free_not_last(struct prison *pr)
2666 KASSERT(refcount_load(&pr->pr_uref) > 0,
2667 ("Trying to free dead prison %p (jid=%d).",
2669 lastref = refcount_release(&pr->pr_uref);
2671 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2674 refcount_release(&pr->pr_uref);
2679 * Complete a call to either prison_free or prison_proc_free.
2682 prison_complete(void *context, int pending)
2684 struct prison *pr = context;
2688 * This could be called to release the last reference, or the last
2689 * user reference (plus the reference held in prison_proc_free).
2691 drflags = prison_lock_xlock(pr, PD_DEREF);
2692 if (pr->pr_flags & PR_COMPLETE_PROC) {
2693 pr->pr_flags &= ~PR_COMPLETE_PROC;
2694 drflags |= PD_DEUREF;
2696 prison_deref(pr, drflags);
2700 * Remove a prison reference and/or user reference (usually).
2701 * This assumes context that allows sleeping (for allprison_lock),
2702 * with no non-sleeping locks held, except perhaps the prison itself.
2703 * If there are no more references, release and delist the prison.
2704 * On completion, the prison lock and the allprison lock are both
2708 prison_deref(struct prison *pr, int flags)
2710 struct prisonlist freeprison;
2711 struct prison *killpr, *rpr, *ppr, *tpr;
2715 TAILQ_INIT(&freeprison);
2717 * Release this prison as requested, which may cause its parent
2718 * to be released, and then maybe its grandparent, etc.
2721 if (flags & PD_KILL) {
2722 /* Kill the prison and its descendents. */
2723 KASSERT(pr != &prison0,
2724 ("prison_deref trying to kill prison0"));
2725 if (!(flags & PD_DEREF)) {
2729 flags = prison_lock_xlock(pr, flags);
2730 prison_deref_kill(pr, &freeprison);
2732 if (flags & PD_DEUREF) {
2733 /* Drop a user reference. */
2734 KASSERT(refcount_load(&pr->pr_uref) > 0,
2735 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2737 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2738 if (!(flags & PD_DEREF)) {
2742 flags = prison_lock_xlock(pr, flags);
2743 if (refcount_release(&pr->pr_uref) &&
2744 pr->pr_state == PRISON_STATE_ALIVE) {
2746 * When the last user references goes,
2747 * this becomes a dying prison.
2750 refcount_load(&prison0.pr_uref) > 0,
2751 ("prison0 pr_uref=0"));
2752 pr->pr_state = PRISON_STATE_DYING;
2753 mtx_unlock(&pr->pr_mtx);
2754 flags &= ~PD_LOCKED;
2755 (void)osd_jail_call(pr,
2756 PR_METHOD_REMOVE, NULL);
2760 if (flags & PD_KILL) {
2762 * Any remaining user references are probably processes
2763 * that need to be killed, either in this prison or its
2766 if (refcount_load(&pr->pr_uref) > 0)
2768 /* Make sure the parent prison doesn't get killed. */
2771 if (flags & PD_DEREF) {
2772 /* Drop a reference. */
2773 KASSERT(refcount_load(&pr->pr_ref) > 0,
2774 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2776 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2777 flags = prison_lock_xlock(pr, flags);
2778 if (refcount_release(&pr->pr_ref)) {
2780 * When the last reference goes,
2781 * unlink the prison and set it aside.
2784 refcount_load(&pr->pr_uref) == 0,
2785 ("prison_deref: last ref, "
2786 "but still has %d urefs (jid=%d)",
2787 pr->pr_uref, pr->pr_id));
2789 refcount_load(&prison0.pr_ref) != 0,
2790 ("prison0 pr_ref=0"));
2791 pr->pr_state = PRISON_STATE_INVALID;
2792 TAILQ_REMOVE(&allprison, pr, pr_list);
2793 LIST_REMOVE(pr, pr_sibling);
2794 TAILQ_INSERT_TAIL(&freeprison, pr,
2796 for (ppr = pr->pr_parent;
2798 ppr = ppr->pr_parent)
2799 ppr->pr_childcount--;
2801 * Removing a prison frees references
2804 mtx_unlock(&pr->pr_mtx);
2805 flags &= ~PD_LOCKED;
2807 flags |= PD_DEREF | PD_DEUREF;
2815 /* Release all the prison locks. */
2816 if (flags & PD_LOCKED)
2817 mtx_unlock(&pr->pr_mtx);
2818 if (flags & PD_LIST_SLOCKED)
2819 sx_sunlock(&allprison_lock);
2820 else if (flags & PD_LIST_XLOCKED)
2821 sx_xunlock(&allprison_lock);
2823 /* Kill any processes attached to a killed prison. */
2824 if (killpr != NULL) {
2825 sx_slock(&allproc_lock);
2826 FOREACH_PROC_IN_SYSTEM(p) {
2828 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
2829 for (ppr = p->p_ucred->cr_prison;
2831 ppr = ppr->pr_parent)
2832 if (ppr == killpr) {
2833 kern_psignal(p, SIGKILL);
2839 sx_sunlock(&allproc_lock);
2843 * Finish removing any unreferenced prisons, which couldn't happen
2844 * while allprison_lock was held (to avoid a LOR on vrele).
2846 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
2848 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
2849 vnet_destroy(rpr->pr_vnet);
2851 if (rpr->pr_root != NULL)
2852 vrele(rpr->pr_root);
2853 mtx_destroy(&rpr->pr_mtx);
2855 free(rpr->pr_ip4, M_PRISON);
2858 free(rpr->pr_ip6, M_PRISON);
2860 if (rpr->pr_cpuset != NULL)
2861 cpuset_rel(rpr->pr_cpuset);
2865 prison_racct_detach(rpr);
2867 TAILQ_REMOVE(&freeprison, rpr, pr_list);
2868 free(rpr, M_PRISON);
2873 * Kill the prison and its descendants. Mark them as dying, clear the
2874 * persist flag, and call module remove methods.
2877 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
2879 struct prison *cpr, *ppr, *rpr;
2883 * Unlike the descendants, the target prison can be killed
2884 * even if it is currently dying. This is useful for failed
2885 * creation in jail_set(2).
2887 KASSERT(refcount_load(&pr->pr_ref) > 0,
2888 ("Trying to kill dead prison %p (jid=%d).",
2890 refcount_acquire(&pr->pr_uref);
2891 pr->pr_state = PRISON_STATE_DYING;
2892 mtx_unlock(&pr->pr_mtx);
2895 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
2897 if (!prison_isalive(cpr)) {
2902 prison_proc_hold(cpr);
2903 mtx_lock(&cpr->pr_mtx);
2904 cpr->pr_state = PRISON_STATE_DYING;
2905 cpr->pr_flags |= PR_REMOVE;
2906 mtx_unlock(&cpr->pr_mtx);
2909 if (!(cpr->pr_flags & PR_REMOVE))
2911 (void)osd_jail_call(cpr, PR_METHOD_REMOVE, NULL);
2912 mtx_lock(&cpr->pr_mtx);
2913 cpr->pr_flags &= ~PR_REMOVE;
2914 if (cpr->pr_flags & PR_PERSIST) {
2915 cpr->pr_flags &= ~PR_PERSIST;
2916 prison_proc_free_not_last(cpr);
2917 prison_free_not_last(cpr);
2919 (void)refcount_release(&cpr->pr_uref);
2920 if (refcount_release(&cpr->pr_ref)) {
2922 * When the last reference goes, unlink the prison
2923 * and set it aside for prison_deref() to handle.
2924 * Delay unlinking the sibling list to keep the loop
2928 LIST_REMOVE(rpr, pr_sibling);
2930 rpr->pr_state = PRISON_STATE_INVALID;
2931 TAILQ_REMOVE(&allprison, rpr, pr_list);
2932 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
2934 * Removing a prison frees references from its parent.
2936 ppr = rpr->pr_parent;
2937 prison_proc_free_not_last(ppr);
2938 prison_free_not_last(ppr);
2939 for (; ppr != NULL; ppr = ppr->pr_parent)
2940 ppr->pr_childcount--;
2942 mtx_unlock(&cpr->pr_mtx);
2945 LIST_REMOVE(rpr, pr_sibling);
2947 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2948 mtx_lock(&pr->pr_mtx);
2949 if (pr->pr_flags & PR_PERSIST) {
2950 pr->pr_flags &= ~PR_PERSIST;
2951 prison_proc_free_not_last(pr);
2952 prison_free_not_last(pr);
2954 (void)refcount_release(&pr->pr_uref);
2958 * Given the current locking state in the flags, make sure allprison_lock
2959 * is held exclusive, and the prison is locked. Return flags indicating
2963 prison_lock_xlock(struct prison *pr, int flags)
2966 if (!(flags & PD_LIST_XLOCKED)) {
2968 * Get allprison_lock, which may be an upgrade,
2969 * and may require unlocking the prison.
2971 if (flags & PD_LOCKED) {
2972 mtx_unlock(&pr->pr_mtx);
2973 flags &= ~PD_LOCKED;
2975 if (flags & PD_LIST_SLOCKED) {
2976 if (!sx_try_upgrade(&allprison_lock)) {
2977 sx_sunlock(&allprison_lock);
2978 sx_xlock(&allprison_lock);
2980 flags &= ~PD_LIST_SLOCKED;
2982 sx_xlock(&allprison_lock);
2983 flags |= PD_LIST_XLOCKED;
2985 if (!(flags & PD_LOCKED)) {
2986 /* Lock the prison mutex. */
2987 mtx_lock(&pr->pr_mtx);
2994 * Set or clear a permission bit in the pr_allow field, passing restrictions
2995 * (cleared permission) down to child jails.
2998 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3002 pr = cred->cr_prison;
3003 sx_slock(&allprison_lock);
3004 mtx_lock(&pr->pr_mtx);
3005 prison_set_allow_locked(pr, flag, enable);
3006 mtx_unlock(&pr->pr_mtx);
3007 sx_sunlock(&allprison_lock);
3011 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3017 pr->pr_allow |= flag;
3019 pr->pr_allow &= ~flag;
3020 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3021 cpr->pr_allow &= ~flag;
3026 * Check if a jail supports the given address family.
3028 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3032 prison_check_af(struct ucred *cred, int af)
3037 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3039 pr = cred->cr_prison;
3041 /* Prisons with their own network stack are not limited. */
3042 if (prison_owns_vnet(cred))
3051 if (pr->pr_flags & PR_IP4)
3053 mtx_lock(&pr->pr_mtx);
3054 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3055 error = EAFNOSUPPORT;
3056 mtx_unlock(&pr->pr_mtx);
3062 if (pr->pr_flags & PR_IP6)
3064 mtx_lock(&pr->pr_mtx);
3065 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3066 error = EAFNOSUPPORT;
3067 mtx_unlock(&pr->pr_mtx);
3075 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3076 error = EAFNOSUPPORT;
3082 * Check if given address belongs to the jail referenced by cred (wrapper to
3083 * prison_check_ip[46]).
3085 * Returns 0 if jail doesn't restrict the address family or if address belongs
3086 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3087 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3090 prison_if(struct ucred *cred, const struct sockaddr *sa)
3093 const struct sockaddr_in *sai;
3096 const struct sockaddr_in6 *sai6;
3100 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3101 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3104 if (prison_owns_vnet(cred))
3109 switch (sa->sa_family)
3113 sai = (const struct sockaddr_in *)sa;
3114 error = prison_check_ip4(cred, &sai->sin_addr);
3119 sai6 = (const struct sockaddr_in6 *)sa;
3120 error = prison_check_ip6(cred, &sai6->sin6_addr);
3124 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3125 error = EAFNOSUPPORT;
3131 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3134 prison_check(struct ucred *cred1, struct ucred *cred2)
3137 return ((cred1->cr_prison == cred2->cr_prison ||
3138 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3142 * Return 1 if p2 is a child of p1, otherwise 0.
3145 prison_ischild(struct prison *pr1, struct prison *pr2)
3148 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3155 * Return true if the prison is currently alive. A prison is alive if it
3156 * holds user references and it isn't being removed.
3159 prison_isalive(struct prison *pr)
3162 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3168 * Return true if the prison is currently valid. A prison is valid if it has
3169 * been fully created, and is not being destroyed. Note that dying prisons
3170 * are still considered valid. Invalid prisons won't be found under normal
3171 * circumstances, as they're only put in that state by functions that have
3172 * an exclusive hold on allprison_lock.
3175 prison_isvalid(struct prison *pr)
3178 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3180 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3186 * Return 1 if the passed credential is in a jail and that jail does not
3187 * have its own virtual network stack, otherwise 0.
3190 jailed_without_vnet(struct ucred *cred)
3196 if (prison_owns_vnet(cred))
3204 * Return the correct hostname (domainname, et al) for the passed credential.
3207 getcredhostname(struct ucred *cred, char *buf, size_t size)
3212 * A NULL credential can be used to shortcut to the physical
3213 * system's hostname.
3215 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3216 mtx_lock(&pr->pr_mtx);
3217 strlcpy(buf, pr->pr_hostname, size);
3218 mtx_unlock(&pr->pr_mtx);
3222 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3225 mtx_lock(&cred->cr_prison->pr_mtx);
3226 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3227 mtx_unlock(&cred->cr_prison->pr_mtx);
3231 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3234 mtx_lock(&cred->cr_prison->pr_mtx);
3235 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3236 mtx_unlock(&cred->cr_prison->pr_mtx);
3240 getcredhostid(struct ucred *cred, unsigned long *hostid)
3243 mtx_lock(&cred->cr_prison->pr_mtx);
3244 *hostid = cred->cr_prison->pr_hostid;
3245 mtx_unlock(&cred->cr_prison->pr_mtx);
3249 getjailname(struct ucred *cred, char *name, size_t len)
3252 mtx_lock(&cred->cr_prison->pr_mtx);
3253 strlcpy(name, cred->cr_prison->pr_name, len);
3254 mtx_unlock(&cred->cr_prison->pr_mtx);
3259 * Determine whether the prison represented by cred owns
3260 * its vnet rather than having it inherited.
3262 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3265 prison_owns_vnet(struct ucred *cred)
3269 * vnets cannot be added/removed after jail creation,
3270 * so no need to lock here.
3272 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3277 * Determine whether the subject represented by cred can "see"
3278 * status of a mount point.
3279 * Returns: 0 for permitted, ENOENT otherwise.
3280 * XXX: This function should be called cr_canseemount() and should be
3281 * placed in kern_prot.c.
3284 prison_canseemount(struct ucred *cred, struct mount *mp)
3290 pr = cred->cr_prison;
3291 if (pr->pr_enforce_statfs == 0)
3293 if (pr->pr_root->v_mount == mp)
3295 if (pr->pr_enforce_statfs == 2)
3298 * If jail's chroot directory is set to "/" we should be able to see
3299 * all mount-points from inside a jail.
3300 * This is ugly check, but this is the only situation when jail's
3301 * directory ends with '/'.
3303 if (strcmp(pr->pr_path, "/") == 0)
3305 len = strlen(pr->pr_path);
3307 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3310 * Be sure that we don't have situation where jail's root directory
3311 * is "/some/path" and mount point is "/some/pathpath".
3313 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3319 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3321 char jpath[MAXPATHLEN];
3325 pr = cred->cr_prison;
3326 if (pr->pr_enforce_statfs == 0)
3328 if (prison_canseemount(cred, mp) != 0) {
3329 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3330 strlcpy(sp->f_mntonname, "[restricted]",
3331 sizeof(sp->f_mntonname));
3334 if (pr->pr_root->v_mount == mp) {
3336 * Clear current buffer data, so we are sure nothing from
3337 * the valid path left there.
3339 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3340 *sp->f_mntonname = '/';
3344 * If jail's chroot directory is set to "/" we should be able to see
3345 * all mount-points from inside a jail.
3347 if (strcmp(pr->pr_path, "/") == 0)
3349 len = strlen(pr->pr_path);
3350 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3352 * Clear current buffer data, so we are sure nothing from
3353 * the valid path left there.
3355 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3356 if (*jpath == '\0') {
3357 /* Should never happen. */
3358 *sp->f_mntonname = '/';
3360 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3365 * Check with permission for a specific privilege is granted within jail. We
3366 * have a specific list of accepted privileges; the rest are denied.
3369 prison_priv_check(struct ucred *cred, int priv)
3375 * Some policies have custom handlers. This routine should not be
3376 * called for them. See priv_check_cred().
3379 case PRIV_VFS_LOOKUP:
3380 case PRIV_VFS_GENERATION:
3381 KASSERT(0, ("prison_priv_check instead of a custom handler "
3382 "called for %d\n", priv));
3390 * Privileges specific to prisons with a virtual network stack.
3391 * There might be a duplicate entry here in case the privilege
3392 * is only granted conditionally in the legacy jail case.
3397 * NFS-specific privileges.
3399 case PRIV_NFS_DAEMON:
3400 case PRIV_NFS_LOCKD:
3403 * Network stack privileges.
3405 case PRIV_NET_BRIDGE:
3408 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3409 case PRIV_NET_ROUTE:
3411 case PRIV_NET_SETIFMTU:
3412 case PRIV_NET_SETIFFLAGS:
3413 case PRIV_NET_SETIFCAP:
3414 case PRIV_NET_SETIFDESCR:
3415 case PRIV_NET_SETIFNAME :
3416 case PRIV_NET_SETIFMETRIC:
3417 case PRIV_NET_SETIFPHYS:
3418 case PRIV_NET_SETIFMAC:
3419 case PRIV_NET_SETLANPCP:
3420 case PRIV_NET_ADDMULTI:
3421 case PRIV_NET_DELMULTI:
3422 case PRIV_NET_HWIOCTL:
3423 case PRIV_NET_SETLLADDR:
3424 case PRIV_NET_ADDIFGROUP:
3425 case PRIV_NET_DELIFGROUP:
3426 case PRIV_NET_IFCREATE:
3427 case PRIV_NET_IFDESTROY:
3428 case PRIV_NET_ADDIFADDR:
3429 case PRIV_NET_DELIFADDR:
3432 case PRIV_NET_SETIFVNET:
3433 case PRIV_NET_SETIFFIB:
3436 * 802.11-related privileges.
3438 case PRIV_NET80211_VAP_GETKEY:
3439 case PRIV_NET80211_VAP_MANAGE:
3445 case PRIV_NETATM_CFG:
3446 case PRIV_NETATM_ADD:
3447 case PRIV_NETATM_DEL:
3448 case PRIV_NETATM_SET:
3451 * Bluetooth privileges.
3453 case PRIV_NETBLUETOOTH_RAW:
3457 * Netgraph and netgraph module privileges.
3459 case PRIV_NETGRAPH_CONTROL:
3461 case PRIV_NETGRAPH_TTY:
3465 * IPv4 and IPv6 privileges.
3467 case PRIV_NETINET_IPFW:
3468 case PRIV_NETINET_DIVERT:
3469 case PRIV_NETINET_PF:
3470 case PRIV_NETINET_DUMMYNET:
3471 case PRIV_NETINET_CARP:
3472 case PRIV_NETINET_MROUTE:
3473 case PRIV_NETINET_RAW:
3474 case PRIV_NETINET_ADDRCTRL6:
3475 case PRIV_NETINET_ND6:
3476 case PRIV_NETINET_SCOPE6:
3477 case PRIV_NETINET_ALIFETIME6:
3478 case PRIV_NETINET_IPSEC:
3479 case PRIV_NETINET_BINDANY:
3494 * No default: or deny here.
3495 * In case of no permit fall through to next switch().
3497 if (cred->cr_prison->pr_flags & PR_VNET)
3504 * Allow ktrace privileges for root in jail.
3510 * Allow jailed processes to configure audit identity and
3511 * submit audit records (login, etc). In the future we may
3512 * want to further refine the relationship between audit and
3515 case PRIV_AUDIT_GETAUDIT:
3516 case PRIV_AUDIT_SETAUDIT:
3517 case PRIV_AUDIT_SUBMIT:
3521 * Allow jailed processes to manipulate process UNIX
3522 * credentials in any way they see fit.
3524 case PRIV_CRED_SETUID:
3525 case PRIV_CRED_SETEUID:
3526 case PRIV_CRED_SETGID:
3527 case PRIV_CRED_SETEGID:
3528 case PRIV_CRED_SETGROUPS:
3529 case PRIV_CRED_SETREUID:
3530 case PRIV_CRED_SETREGID:
3531 case PRIV_CRED_SETRESUID:
3532 case PRIV_CRED_SETRESGID:
3535 * Jail implements visibility constraints already, so allow
3536 * jailed root to override uid/gid-based constraints.
3538 case PRIV_SEEOTHERGIDS:
3539 case PRIV_SEEOTHERUIDS:
3542 * Jail implements inter-process debugging limits already, so
3543 * allow jailed root various debugging privileges.
3545 case PRIV_DEBUG_DIFFCRED:
3546 case PRIV_DEBUG_SUGID:
3547 case PRIV_DEBUG_UNPRIV:
3550 * Allow jail to set various resource limits and login
3551 * properties, and for now, exceed process resource limits.
3553 case PRIV_PROC_LIMIT:
3554 case PRIV_PROC_SETLOGIN:
3555 case PRIV_PROC_SETRLIMIT:
3558 * System V and POSIX IPC privileges are granted in jail.
3561 case PRIV_IPC_WRITE:
3562 case PRIV_IPC_ADMIN:
3563 case PRIV_IPC_MSGSIZE:
3567 * Jail operations within a jail work on child jails.
3569 case PRIV_JAIL_ATTACH:
3571 case PRIV_JAIL_REMOVE:
3574 * Jail implements its own inter-process limits, so allow
3575 * root processes in jail to change scheduling on other
3576 * processes in the same jail. Likewise for signalling.
3578 case PRIV_SCHED_DIFFCRED:
3579 case PRIV_SCHED_CPUSET:
3580 case PRIV_SIGNAL_DIFFCRED:
3581 case PRIV_SIGNAL_SUGID:
3584 * Allow jailed processes to write to sysctls marked as jail
3587 case PRIV_SYSCTL_WRITEJAIL:
3590 * Allow root in jail to manage a variety of quota
3591 * properties. These should likely be conditional on a
3592 * configuration option.
3594 case PRIV_VFS_GETQUOTA:
3595 case PRIV_VFS_SETQUOTA:
3598 * Since Jail relies on chroot() to implement file system
3599 * protections, grant many VFS privileges to root in jail.
3600 * Be careful to exclude mount-related and NFS-related
3604 case PRIV_VFS_WRITE:
3605 case PRIV_VFS_ADMIN:
3607 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3608 case PRIV_VFS_CHFLAGS_DEV:
3609 case PRIV_VFS_CHOWN:
3610 case PRIV_VFS_CHROOT:
3611 case PRIV_VFS_RETAINSUGID:
3612 case PRIV_VFS_FCHROOT:
3614 case PRIV_VFS_SETGID:
3616 case PRIV_VFS_STICKYFILE:
3619 * As in the non-jail case, non-root users are expected to be
3620 * able to read kernel/physical memory (provided /dev/[k]mem
3621 * exists in the jail and they have permission to access it).
3623 case PRIV_KMEM_READ:
3627 * Depending on the global setting, allow privilege of
3628 * setting system flags.
3630 case PRIV_VFS_SYSFLAGS:
3631 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3637 * Depending on the global setting, allow privilege of
3638 * mounting/unmounting file systems.
3640 case PRIV_VFS_MOUNT:
3641 case PRIV_VFS_UNMOUNT:
3642 case PRIV_VFS_MOUNT_NONUSER:
3643 case PRIV_VFS_MOUNT_OWNER:
3644 pr = cred->cr_prison;
3646 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3654 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3655 * policy. priv_check_cred will not specifically allow it, and
3656 * we may want a MAC policy to allow it.
3658 case PRIV_VFS_READ_DIR:
3662 * Conditionnaly allow locking (unlocking) physical pages
3666 case PRIV_VM_MUNLOCK:
3667 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3673 * Conditionally allow jailed root to bind reserved ports.
3675 case PRIV_NETINET_RESERVEDPORT:
3676 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3682 * Allow jailed root to reuse in-use ports.
3684 case PRIV_NETINET_REUSEPORT:
3688 * Allow jailed root to set certain IPv4/6 (option) headers.
3690 case PRIV_NETINET_SETHDROPTS:
3694 * Conditionally allow creating raw sockets in jail.
3696 case PRIV_NETINET_RAW:
3697 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3703 * Since jail implements its own visibility limits on netstat
3704 * sysctls, allow getcred. This allows identd to work in
3707 case PRIV_NETINET_GETCRED:
3711 * Allow jailed root to set loginclass.
3713 case PRIV_PROC_SETLOGINCLASS:
3717 * Do not allow a process inside a jail to read the kernel
3718 * message buffer unless explicitly permitted.
3721 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3727 * In all remaining cases, deny the privilege request. This
3728 * includes almost all network privileges, many system
3729 * configuration privileges.
3736 * Return the part of pr2's name that is relative to pr1, or the whole name
3737 * if it does not directly follow.
3741 prison_name(struct prison *pr1, struct prison *pr2)
3745 /* Jails see themselves as "0" (if they see themselves at all). */
3748 name = pr2->pr_name;
3749 if (prison_ischild(pr1, pr2)) {
3751 * pr1 isn't locked (and allprison_lock may not be either)
3752 * so its length can't be counted on. But the number of dots
3753 * can be counted on - and counted.
3755 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3756 name = strchr(name, '.') + 1;
3762 * Return the part of pr2's path that is relative to pr1, or the whole path
3763 * if it does not directly follow.
3766 prison_path(struct prison *pr1, struct prison *pr2)
3768 char *path1, *path2;
3771 path1 = pr1->pr_path;
3772 path2 = pr2->pr_path;
3773 if (!strcmp(path1, "/"))
3775 len1 = strlen(path1);
3776 if (strncmp(path1, path2, len1))
3778 if (path2[len1] == '\0')
3780 if (path2[len1] == '/')
3781 return (path2 + len1);
3786 * Jail-related sysctls.
3788 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3792 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3795 struct prison *pr, *cpr;
3797 struct in_addr *ip4 = NULL;
3801 struct in6_addr *ip6 = NULL;
3806 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3807 pr = req->td->td_ucred->cr_prison;
3809 sx_slock(&allprison_lock);
3810 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3811 #if defined(INET) || defined(INET6)
3814 mtx_lock(&cpr->pr_mtx);
3816 if (cpr->pr_ip4s > 0) {
3817 if (ip4s < cpr->pr_ip4s) {
3818 ip4s = cpr->pr_ip4s;
3819 mtx_unlock(&cpr->pr_mtx);
3820 ip4 = realloc(ip4, ip4s *
3821 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3824 bcopy(cpr->pr_ip4, ip4,
3825 cpr->pr_ip4s * sizeof(struct in_addr));
3829 if (cpr->pr_ip6s > 0) {
3830 if (ip6s < cpr->pr_ip6s) {
3831 ip6s = cpr->pr_ip6s;
3832 mtx_unlock(&cpr->pr_mtx);
3833 ip6 = realloc(ip6, ip6s *
3834 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3837 bcopy(cpr->pr_ip6, ip6,
3838 cpr->pr_ip6s * sizeof(struct in6_addr));
3841 bzero(xp, sizeof(*xp));
3842 xp->pr_version = XPRISON_VERSION;
3843 xp->pr_id = cpr->pr_id;
3844 xp->pr_state = cpr->pr_state;
3845 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3846 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3847 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3849 xp->pr_ip4s = cpr->pr_ip4s;
3852 xp->pr_ip6s = cpr->pr_ip6s;
3854 mtx_unlock(&cpr->pr_mtx);
3855 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3859 if (xp->pr_ip4s > 0) {
3860 error = SYSCTL_OUT(req, ip4,
3861 xp->pr_ip4s * sizeof(struct in_addr));
3867 if (xp->pr_ip6s > 0) {
3868 error = SYSCTL_OUT(req, ip6,
3869 xp->pr_ip6s * sizeof(struct in6_addr));
3875 sx_sunlock(&allprison_lock);
3886 SYSCTL_OID(_security_jail, OID_AUTO, list,
3887 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3888 sysctl_jail_list, "S", "List of active jails");
3891 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3895 injail = jailed(req->td->td_ucred);
3896 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3901 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3902 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3903 sysctl_jail_jailed, "I", "Process in jail?");
3906 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3908 int error, havevnet;
3910 struct ucred *cred = req->td->td_ucred;
3912 havevnet = jailed(cred) && prison_owns_vnet(cred);
3916 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3921 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3922 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3923 sysctl_jail_vnet, "I", "Jail owns vnet?");
3925 #if defined(INET) || defined(INET6)
3926 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3927 &jail_max_af_ips, 0,
3928 "Number of IP addresses a jail may have at most per address family (deprecated)");
3932 * Default parameters for jail(2) compatibility. For historical reasons,
3933 * the sysctl names have varying similarity to the parameter names. Prisons
3934 * just see their own parameters, and can't change them.
3937 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3941 /* Get the current flag value, and convert it to a boolean. */
3942 if (req->td->td_ucred->cr_prison == &prison0) {
3943 mtx_lock(&prison0.pr_mtx);
3944 i = (jail_default_allow & arg2) != 0;
3945 mtx_unlock(&prison0.pr_mtx);
3947 i = prison_allow(req->td->td_ucred, arg2);
3951 error = sysctl_handle_int(oidp, &i, 0, req);
3952 if (error || !req->newptr)
3958 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3961 mtx_lock(&prison0.pr_mtx);
3962 jail_default_allow = (jail_default_allow & ~arg2) | i;
3963 mtx_unlock(&prison0.pr_mtx);
3967 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3968 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3969 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3970 "Processes in jail can set their hostnames (deprecated)");
3971 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3972 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3973 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3974 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
3975 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3976 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3977 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3978 "Processes in jail can use System V IPC primitives (deprecated)");
3979 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3980 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3981 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3982 "Prison root can create raw sockets (deprecated)");
3983 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3984 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3985 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3986 "Processes in jail can alter system file flags (deprecated)");
3987 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3988 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3989 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3990 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
3993 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3998 pr = req->td->td_ucred->cr_prison;
3999 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4000 error = sysctl_handle_int(oidp, &level, 0, req);
4001 if (error || !req->newptr)
4003 *(int *)arg1 = level;
4007 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4008 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4009 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4010 sysctl_jail_default_level, "I",
4011 "Processes in jail cannot see all mounted file systems (deprecated)");
4013 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4014 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4015 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4016 sysctl_jail_default_level, "I",
4017 "Ruleset for the devfs filesystem in jail (deprecated)");
4020 * Nodes to describe jail parameters. Maximum length of string parameters
4021 * is returned in the string itself, and the other parameters exist merely
4022 * to make themselves and their types known.
4024 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4028 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4035 switch (oidp->oid_kind & CTLTYPE)
4041 if (!(req->flags & SCTL_MASK32))
4043 return (SYSCTL_OUT(req, &l, sizeof(l)));
4047 return (SYSCTL_OUT(req, &i, sizeof(i)));
4048 case CTLTYPE_STRING:
4049 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4051 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4052 case CTLTYPE_STRUCT:
4054 return (SYSCTL_OUT(req, &s, sizeof(s)));
4060 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4061 * jail creation time but cannot be changed in an existing jail.
4063 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4064 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4065 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4066 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4067 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4068 "I", "Jail secure level");
4069 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4070 "Jail value for kern.osreldate and uname -K");
4071 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4072 "Jail value for kern.osrelease and uname -r");
4073 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4074 "I", "Jail cannot see all mounted file systems");
4075 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4076 "I", "Ruleset for in-jail devfs mounts");
4077 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4078 "B", "Jail persistence");
4080 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4081 "E,jailsys", "Virtual network stack");
4083 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4084 "B", "Jail is in the process of shutting down");
4086 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4087 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4088 "I", "Current number of child jails");
4089 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4090 "I", "Maximum number of child jails");
4092 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4093 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4095 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4096 "Jail NIS domainname");
4097 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4099 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4100 "LU", "Jail host ID");
4102 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4103 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4106 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4107 "Jail IPv4 address virtualization");
4108 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4109 "S,in_addr,a", "Jail IPv4 addresses");
4110 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4111 "B", "Do (not) use IPv4 source address selection rather than the "
4112 "primary jail IPv4 address.");
4115 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4116 "Jail IPv6 address virtualization");
4117 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4118 "S,in6_addr,a", "Jail IPv6 addresses");
4119 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4120 "B", "Do (not) use IPv6 source address selection rather than the "
4121 "primary jail IPv6 address.");
4124 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4125 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4126 "B", "Jail may set hostname");
4127 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4128 "B", "Jail may use SYSV IPC");
4129 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4130 "B", "Jail may create raw sockets");
4131 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4132 "B", "Jail may alter system file flags");
4133 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4134 "B", "Jail may set file quotas");
4135 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4136 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4137 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4138 "B", "Jail may lock (unlock) physical pages in memory");
4139 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4140 "B", "Jail may bind sockets to reserved ports");
4141 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4142 "B", "Jail may read the kernel message buffer");
4143 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4144 "B", "Unprivileged processes may use process debugging facilities");
4145 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4146 "B", "Processes in jail with uid 0 have privilege");
4148 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4149 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4150 "B", "Jail may mount/unmount jail-friendly file systems in general");
4153 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4154 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4158 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4161 struct bool_flags *bf;
4162 struct sysctl_oid *parent;
4163 char *allow_name, *allow_noname, *allowed;
4164 #ifndef NO_SYSCTL_DESCR
4165 char *descr_deprecated;
4170 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4172 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4174 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4175 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4176 free(allow_name, M_PRISON);
4181 * See if this parameter has already beed added, i.e. a module was
4182 * previously loaded/unloaded.
4184 mtx_lock(&prison0.pr_mtx);
4185 for (bf = pr_flag_allow;
4186 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4187 atomic_load_int(&bf->flag) != 0;
4189 if (strcmp(bf->name, allow_name) == 0) {
4190 allow_flag = bf->flag;
4196 * Find a free bit in pr_allow_all, failing if there are none
4197 * (which shouldn't happen as long as we keep track of how many
4198 * potential dynamic flags exist).
4200 for (allow_flag = 1;; allow_flag <<= 1) {
4201 if (allow_flag == 0)
4203 if ((pr_allow_all & allow_flag) == 0)
4207 /* Note the parameter in the next open slot in pr_flag_allow. */
4208 for (bf = pr_flag_allow; ; bf++) {
4209 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4210 /* This should never happen, but is not fatal. */
4214 if (atomic_load_int(&bf->flag) == 0)
4217 bf->name = allow_name;
4218 bf->noname = allow_noname;
4219 pr_allow_all |= allow_flag;
4221 * prison0 always has permission for the new parameter.
4222 * Other jails must have it granted to them.
4224 prison0.pr_allow |= allow_flag;
4225 /* The flag indicates a valid entry, so make sure it is set last. */
4226 atomic_store_rel_int(&bf->flag, allow_flag);
4227 mtx_unlock(&prison0.pr_mtx);
4230 * Create sysctls for the paramter, and the back-compat global
4234 ? SYSCTL_ADD_NODE(NULL,
4235 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4236 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4237 : &sysctl___security_jail_param_allow;
4238 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4239 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4240 NULL, 0, sysctl_jail_param, "B", descr);
4242 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4243 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4244 #ifndef NO_SYSCTL_DESCR
4245 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4248 (void)SYSCTL_ADD_PROC(NULL,
4249 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4250 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4251 sysctl_jail_default_allow, "I", descr_deprecated);
4252 #ifndef NO_SYSCTL_DESCR
4253 free(descr_deprecated, M_TEMP);
4255 free(allowed, M_TEMP);
4260 mtx_unlock(&prison0.pr_mtx);
4261 free(allow_name, M_PRISON);
4262 free(allow_noname, M_PRISON);
4267 * The VFS system will register jail-aware filesystems here. They each get
4268 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4269 * attempts to mount.
4272 prison_add_vfs(struct vfsconf *vfsp)
4274 #ifdef NO_SYSCTL_DESCR
4276 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4281 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4283 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4285 free(descr, M_TEMP);
4291 prison_racct_foreach(void (*callback)(struct racct *racct,
4292 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4293 void *arg2, void *arg3)
4295 struct prison_racct *prr;
4297 ASSERT_RACCT_ENABLED();
4299 sx_slock(&allprison_lock);
4302 LIST_FOREACH(prr, &allprison_racct, prr_next)
4303 (callback)(prr->prr_racct, arg2, arg3);
4306 sx_sunlock(&allprison_lock);
4309 static struct prison_racct *
4310 prison_racct_find_locked(const char *name)
4312 struct prison_racct *prr;
4314 ASSERT_RACCT_ENABLED();
4315 sx_assert(&allprison_lock, SA_XLOCKED);
4317 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4320 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4321 if (strcmp(name, prr->prr_name) != 0)
4324 /* Found prison_racct with a matching name? */
4325 prison_racct_hold(prr);
4329 /* Add new prison_racct. */
4330 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4331 racct_create(&prr->prr_racct);
4333 strcpy(prr->prr_name, name);
4334 refcount_init(&prr->prr_refcount, 1);
4335 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4340 struct prison_racct *
4341 prison_racct_find(const char *name)
4343 struct prison_racct *prr;
4345 ASSERT_RACCT_ENABLED();
4347 sx_xlock(&allprison_lock);
4348 prr = prison_racct_find_locked(name);
4349 sx_xunlock(&allprison_lock);
4354 prison_racct_hold(struct prison_racct *prr)
4357 ASSERT_RACCT_ENABLED();
4359 refcount_acquire(&prr->prr_refcount);
4363 prison_racct_free_locked(struct prison_racct *prr)
4366 ASSERT_RACCT_ENABLED();
4367 sx_assert(&allprison_lock, SA_XLOCKED);
4369 if (refcount_release(&prr->prr_refcount)) {
4370 racct_destroy(&prr->prr_racct);
4371 LIST_REMOVE(prr, prr_next);
4372 free(prr, M_PRISON_RACCT);
4377 prison_racct_free(struct prison_racct *prr)
4380 ASSERT_RACCT_ENABLED();
4381 sx_assert(&allprison_lock, SA_UNLOCKED);
4383 if (refcount_release_if_not_last(&prr->prr_refcount))
4386 sx_xlock(&allprison_lock);
4387 prison_racct_free_locked(prr);
4388 sx_xunlock(&allprison_lock);
4392 prison_racct_attach(struct prison *pr)
4394 struct prison_racct *prr;
4396 ASSERT_RACCT_ENABLED();
4397 sx_assert(&allprison_lock, SA_XLOCKED);
4399 prr = prison_racct_find_locked(pr->pr_name);
4400 KASSERT(prr != NULL, ("cannot find prison_racct"));
4402 pr->pr_prison_racct = prr;
4406 * Handle jail renaming. From the racct point of view, renaming means
4407 * moving from one prison_racct to another.
4410 prison_racct_modify(struct prison *pr)
4416 struct prison_racct *oldprr;
4418 ASSERT_RACCT_ENABLED();
4420 sx_slock(&allproc_lock);
4421 sx_xlock(&allprison_lock);
4423 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4424 sx_xunlock(&allprison_lock);
4425 sx_sunlock(&allproc_lock);
4429 oldprr = pr->pr_prison_racct;
4430 pr->pr_prison_racct = NULL;
4432 prison_racct_attach(pr);
4435 * Move resource utilisation records.
4437 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4441 * Force rctl to reattach rules to processes.
4443 FOREACH_PROC_IN_SYSTEM(p) {
4445 cred = crhold(p->p_ucred);
4447 rctl_proc_ucred_changed(p, cred);
4452 sx_sunlock(&allproc_lock);
4453 prison_racct_free_locked(oldprr);
4454 sx_xunlock(&allprison_lock);
4458 prison_racct_detach(struct prison *pr)
4461 ASSERT_RACCT_ENABLED();
4462 sx_assert(&allprison_lock, SA_UNLOCKED);
4464 if (pr->pr_prison_racct == NULL)
4466 prison_racct_free(pr->pr_prison_racct);
4467 pr->pr_prison_racct = NULL;
4474 db_show_prison(struct prison *pr)
4476 struct bool_flags *bf;
4477 struct jailsys_flags *jsf;
4478 #if defined(INET) || defined(INET6)
4483 char ip4buf[INET_ADDRSTRLEN];
4486 char ip6buf[INET6_ADDRSTRLEN];
4489 db_printf("prison %p:\n", pr);
4490 db_printf(" jid = %d\n", pr->pr_id);
4491 db_printf(" name = %s\n", pr->pr_name);
4492 db_printf(" parent = %p\n", pr->pr_parent);
4493 db_printf(" ref = %d\n", pr->pr_ref);
4494 db_printf(" uref = %d\n", pr->pr_uref);
4495 db_printf(" state = %s\n",
4496 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4497 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4499 db_printf(" path = %s\n", pr->pr_path);
4500 db_printf(" cpuset = %d\n", pr->pr_cpuset
4501 ? pr->pr_cpuset->cs_id : -1);
4503 db_printf(" vnet = %p\n", pr->pr_vnet);
4505 db_printf(" root = %p\n", pr->pr_root);
4506 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4507 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4508 db_printf(" children.max = %d\n", pr->pr_childmax);
4509 db_printf(" children.cur = %d\n", pr->pr_childcount);
4510 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4511 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4512 db_printf(" flags = 0x%x", pr->pr_flags);
4513 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4514 if (pr->pr_flags & bf->flag)
4515 db_printf(" %s", bf->name);
4516 for (jsf = pr_flag_jailsys;
4517 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4519 f = pr->pr_flags & (jsf->disable | jsf->new);
4520 db_printf(" %-16s= %s\n", jsf->name,
4521 (f != 0 && f == jsf->disable) ? "disable"
4522 : (f == jsf->new) ? "new"
4525 db_printf(" allow = 0x%x", pr->pr_allow);
4526 for (bf = pr_flag_allow;
4527 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4528 atomic_load_int(&bf->flag) != 0;
4530 if (pr->pr_allow & bf->flag)
4531 db_printf(" %s", bf->name);
4533 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4534 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4535 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4536 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4537 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4539 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4540 for (ii = 0; ii < pr->pr_ip4s; ii++)
4541 db_printf(" %s %s\n",
4542 ii == 0 ? "ip4.addr =" : " ",
4543 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4546 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4547 for (ii = 0; ii < pr->pr_ip6s; ii++)
4548 db_printf(" %s %s\n",
4549 ii == 0 ? "ip6.addr =" : " ",
4550 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4554 DB_SHOW_COMMAND(prison, db_show_prison_command)
4560 * Show all prisons in the list, and prison0 which is not
4563 db_show_prison(&prison0);
4564 if (!db_pager_quit) {
4565 TAILQ_FOREACH(pr, &allprison, pr_list) {
4577 /* Look for a prison with the ID and with references. */
4578 TAILQ_FOREACH(pr, &allprison, pr_list)
4579 if (pr->pr_id == addr && pr->pr_ref > 0)
4582 /* Look again, without requiring a reference. */
4583 TAILQ_FOREACH(pr, &allprison, pr_list)
4584 if (pr->pr_id == addr)
4587 /* Assume address points to a valid prison. */
4588 pr = (struct prison *)addr;