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"
39 #include <sys/param.h>
40 #include <sys/types.h>
41 #include <sys/kernel.h>
42 #include <sys/systm.h>
43 #include <sys/errno.h>
44 #include <sys/sysproto.h>
45 #include <sys/malloc.h>
49 #include <sys/taskqueue.h>
50 #include <sys/fcntl.h>
52 #include <sys/linker.h>
55 #include <sys/mutex.h>
56 #include <sys/racct.h>
58 #include <sys/refcount.h>
60 #include <sys/sysent.h>
61 #include <sys/namei.h>
62 #include <sys/mount.h>
63 #include <sys/queue.h>
64 #include <sys/socket.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysctl.h>
68 #include <sys/vnode.h>
73 #include <netinet/in.h>
79 #include <security/mac/mac_framework.h>
81 #define PRISON0_HOSTUUID_MODULE "hostuuid"
83 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
84 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
86 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
89 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
91 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
95 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
97 #define _PR_IP_SADDRSEL 0
101 /* prison0 describes what is "real" about the system. */
102 struct prison prison0 = {
108 .pr_securelevel = -1,
110 .pr_state = PRISON_STATE_ALIVE,
111 .pr_childmax = JAIL_MAX,
112 .pr_hostuuid = DEFAULT_HOSTUUID,
113 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
115 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
117 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
119 .pr_allow = PR_ALLOW_ALL_STATIC,
121 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
128 struct jailsys_flags {
134 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
135 struct sx allprison_lock;
136 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
137 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
138 LIST_HEAD(, prison_racct) allprison_racct;
141 static int get_next_prid(struct prison **insprp);
142 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
143 static void prison_complete(void *context, int pending);
144 static void prison_deref(struct prison *pr, int flags);
145 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
146 static int prison_lock_xlock(struct prison *pr, int flags);
147 static void prison_cleanup(struct prison *pr);
148 static void prison_free_not_last(struct prison *pr);
149 static void prison_proc_free_not_last(struct prison *pr);
150 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
152 static char *prison_path(struct prison *pr1, struct prison *pr2);
154 static void prison_racct_attach(struct prison *pr);
155 static void prison_racct_modify(struct prison *pr);
156 static void prison_racct_detach(struct prison *pr);
159 /* Flags for prison_deref */
160 #define PD_DEREF 0x01 /* Decrement pr_ref */
161 #define PD_DEUREF 0x02 /* Decrement pr_uref */
162 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
163 #define PD_LOCKED 0x10 /* pr_mtx is held */
164 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
165 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
166 #define PD_OP_FLAGS 0x07 /* Operation flags */
167 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
170 * Parameter names corresponding to PR_* flag values. Size values are for kvm
171 * as we cannot figure out the size of a sparse array, or an array without a
174 static struct bool_flags pr_flag_bool[] = {
175 {"persist", "nopersist", PR_PERSIST},
177 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
180 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
183 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
185 static struct jailsys_flags pr_flag_jailsys[] = {
186 {"host", 0, PR_HOST},
188 {"vnet", 0, PR_VNET},
191 {"ip4", PR_IP4_USER, PR_IP4_USER},
194 {"ip6", PR_IP6_USER, PR_IP6_USER},
197 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
200 * Make this array full-size so dynamic parameters can be added.
201 * It is protected by prison0.mtx, but lockless reading is allowed
202 * with an atomic check of the flag values.
204 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
205 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
206 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
207 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
208 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
209 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
210 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
211 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
212 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
213 {"allow.reserved_ports", "allow.noreserved_ports",
214 PR_ALLOW_RESERVED_PORTS},
215 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
216 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
217 PR_ALLOW_UNPRIV_DEBUG},
218 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
220 {"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD},
223 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
224 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
226 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
227 PR_ALLOW_RESERVED_PORTS | \
228 PR_ALLOW_UNPRIV_DEBUG | \
230 #define JAIL_DEFAULT_ENFORCE_STATFS 2
231 #define JAIL_DEFAULT_DEVFS_RSNUM 0
232 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
233 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
234 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
235 #if defined(INET) || defined(INET6)
236 static unsigned jail_max_af_ips = 255;
240 * Initialize the parts of prison0 that can't be static-initialized with
241 * constants. This is called from proc0_init() after creating thread0 cpuset.
246 uint8_t *file, *data;
248 char buf[sizeof(prison0.pr_hostuuid)];
251 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
252 prison0.pr_osreldate = osreldate;
253 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
255 /* If we have a preloaded hostuuid, use it. */
256 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
258 data = preload_fetch_addr(file);
259 size = preload_fetch_size(file);
262 * The preloaded data may include trailing whitespace, almost
263 * certainly a newline; skip over any whitespace or
264 * non-printable characters to be safe.
266 while (size > 0 && data[size - 1] <= 0x20) {
273 * Not NUL-terminated when passed from loader, but
274 * validate_uuid requires that due to using sscanf (as
275 * does the subsequent strlcpy, since it still reads
276 * past the given size to return the true length);
277 * bounce to a temporary buffer to fix.
279 if (size >= sizeof(buf))
282 memcpy(buf, data, size);
285 if (validate_uuid(buf, size, NULL, 0) != 0)
289 (void)strlcpy(prison0.pr_hostuuid, buf,
290 sizeof(prison0.pr_hostuuid));
293 if (bootverbose && !valid) {
294 printf("hostuuid: preload data malformed: '%.*s'\n",
300 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
309 sys_jail(struct thread *td, struct jail_args *uap)
315 error = copyin(uap->jail, &version, sizeof(uint32_t));
324 /* FreeBSD single IPv4 jails. */
325 bzero(&j, sizeof(struct jail));
326 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
329 j.version = j0.version;
331 j.hostname = j0.hostname;
332 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
338 * Version 1 was used by multi-IPv4 jail implementations
339 * that never made it into the official kernel.
343 case 2: /* JAIL_API_VERSION */
344 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
345 error = copyin(uap->jail, &j, sizeof(struct jail));
351 /* Sci-Fi jails are not supported, sorry. */
354 return (kern_jail(td, &j));
358 kern_jail(struct thread *td, struct jail *j)
360 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
369 char *u_path, *u_hostname, *u_name;
370 struct bool_flags *bf;
373 struct in_addr *u_ip4;
376 struct in6_addr *u_ip6;
379 int error, enforce_statfs;
381 bzero(&optiov, sizeof(optiov));
382 opt.uio_iov = optiov;
386 opt.uio_segflg = UIO_SYSSPACE;
387 opt.uio_rw = UIO_READ;
390 /* Set permissions for top-level jails from sysctls. */
391 if (!jailed(td->td_ucred)) {
392 for (bf = pr_flag_allow;
393 bf < pr_flag_allow + nitems(pr_flag_allow) &&
394 atomic_load_int(&bf->flag) != 0;
396 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
397 (jail_default_allow & bf->flag)
398 ? bf->name : bf->noname);
399 optiov[opt.uio_iovcnt].iov_len =
400 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
403 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
404 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
406 enforce_statfs = jail_default_enforce_statfs;
407 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
408 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
412 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
414 ip4s = (j->version == 0) ? 1 : j->ip4s;
415 if (ip4s > jail_max_af_ips)
417 tmplen += ip4s * sizeof(struct in_addr);
423 if (j->ip6s > jail_max_af_ips)
425 tmplen += j->ip6s * sizeof(struct in6_addr);
430 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
431 u_hostname = u_path + MAXPATHLEN;
432 u_name = u_hostname + MAXHOSTNAMELEN;
434 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
438 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
440 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
443 optiov[opt.uio_iovcnt].iov_base = "path";
444 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
446 optiov[opt.uio_iovcnt].iov_base = u_path;
447 error = copyinstr(j->path, u_path, MAXPATHLEN,
448 &optiov[opt.uio_iovcnt].iov_len);
450 free(u_path, M_TEMP);
454 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
455 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
457 optiov[opt.uio_iovcnt].iov_base = u_hostname;
458 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
459 &optiov[opt.uio_iovcnt].iov_len);
461 free(u_path, M_TEMP);
465 if (j->jailname != NULL) {
466 optiov[opt.uio_iovcnt].iov_base = "name";
467 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
469 optiov[opt.uio_iovcnt].iov_base = u_name;
470 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
471 &optiov[opt.uio_iovcnt].iov_len);
473 free(u_path, M_TEMP);
479 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
480 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
482 optiov[opt.uio_iovcnt].iov_base = u_ip4;
483 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
485 u_ip4->s_addr = j->ip4s;
487 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
489 free(u_path, M_TEMP);
496 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
497 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
499 optiov[opt.uio_iovcnt].iov_base = u_ip6;
500 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
501 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
503 free(u_path, M_TEMP);
508 KASSERT(opt.uio_iovcnt <= nitems(optiov),
509 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
510 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
511 free(u_path, M_TEMP);
516 * struct jail_set_args {
517 * struct iovec *iovp;
518 * unsigned int iovcnt;
523 sys_jail_set(struct thread *td, struct jail_set_args *uap)
528 /* Check that we have an even number of iovecs. */
532 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
535 error = kern_jail_set(td, auio, uap->flags);
541 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
548 struct in6_addr *ip6;
551 struct vfsoptlist *opts;
552 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
554 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
555 char *g_path, *osrelstr;
556 struct bool_flags *bf;
557 struct jailsys_flags *jsf;
558 #if defined(INET) || defined(INET6)
563 size_t namelen, onamelen, pnamelen;
564 int born, created, cuflags, descend, drflags, enforce;
565 int error, errmsg_len, errmsg_pos;
566 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
567 int jid, jsys, len, level;
568 int childmax, osreldt, rsnum, slevel;
569 #if defined(INET) || defined(INET6)
578 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
579 uint64_t pr_allow_diff;
583 error = priv_check(td, PRIV_JAIL_SET);
584 if (!error && (flags & JAIL_ATTACH))
585 error = priv_check(td, PRIV_JAIL_ATTACH);
588 mypr = td->td_ucred->cr_prison;
589 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
591 if (flags & ~JAIL_SET_MASK)
595 * Check all the parameters before committing to anything. Not all
596 * errors can be caught early, but we may as well try. Also, this
597 * takes care of some expensive stuff (path lookup) before getting
598 * the allprison lock.
600 * XXX Jails are not filesystems, and jail parameters are not mount
601 * options. But it makes more sense to re-use the vfsopt code
602 * than duplicate it under a different name.
604 error = vfs_buildopts(optuio, &opts);
615 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
618 vfs_opterror(opts, "no valid operation (create or update)");
622 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
628 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
637 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
645 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
650 else if (enforce < 0 || enforce > 2) {
656 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
664 pr_flags = ch_flags = 0;
665 for (bf = pr_flag_bool;
666 bf < pr_flag_bool + nitems(pr_flag_bool);
668 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
669 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
671 ch_flags |= pr_flags;
672 for (jsf = pr_flag_jailsys;
673 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
675 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
681 case JAIL_SYS_DISABLE:
686 pr_flags |= jsf->disable;
689 pr_flags |= jsf->new;
691 case JAIL_SYS_INHERIT:
697 ch_flags |= jsf->new | jsf->disable;
699 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
700 && !(pr_flags & PR_PERSIST)) {
702 vfs_opterror(opts, "new jail must persist or attach");
706 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
708 vfs_opterror(opts, "vnet cannot be changed after creation");
713 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
715 vfs_opterror(opts, "ip4 cannot be changed after creation");
720 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
722 vfs_opterror(opts, "ip6 cannot be changed after creation");
727 pr_allow = ch_allow = 0;
728 for (bf = pr_flag_allow;
729 bf < pr_flag_allow + nitems(pr_flag_allow) &&
730 atomic_load_int(&bf->flag) != 0;
732 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
733 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
735 ch_allow |= pr_allow;
737 error = vfs_getopt(opts, "name", (void **)&name, &len);
743 if (len == 0 || name[len - 1] != '\0') {
747 if (len > MAXHOSTNAMELEN) {
748 error = ENAMETOOLONG;
753 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
761 if (len == 0 || host[len - 1] != '\0') {
765 if (len > MAXHOSTNAMELEN) {
766 error = ENAMETOOLONG;
771 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
779 if (len == 0 || domain[len - 1] != '\0') {
783 if (len > MAXHOSTNAMELEN) {
784 error = ENAMETOOLONG;
789 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
797 if (len == 0 || uuid[len - 1] != '\0') {
801 if (len > HOSTUUIDLEN) {
802 error = ENAMETOOLONG;
807 #ifdef COMPAT_FREEBSD32
808 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
811 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
815 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
827 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
832 else if (ip4s & (sizeof(*ip4) - 1)) {
836 ch_flags |= PR_IP4_USER;
837 pr_flags |= PR_IP4_USER;
839 ip4s /= sizeof(*ip4);
840 if (ip4s > jail_max_af_ips) {
842 vfs_opterror(opts, "too many IPv4 addresses");
845 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
846 bcopy(op, ip4, ip4s * sizeof(*ip4));
848 * IP addresses are all sorted but ip[0] to preserve
849 * the primary IP address as given from userland.
850 * This special IP is used for unbound outgoing
851 * connections as well for "loopback" traffic in case
852 * source address selection cannot find any more fitting
853 * address to connect from.
856 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
859 * Check for duplicate addresses and do some simple
860 * zero and broadcast checks. If users give other bogus
861 * addresses it is their problem.
863 * We do not have to care about byte order for these
864 * checks so we will do them in NBO.
866 for (ii = 0; ii < ip4s; ii++) {
867 if (ip4[ii].s_addr == INADDR_ANY ||
868 ip4[ii].s_addr == INADDR_BROADCAST) {
873 (ip4[0].s_addr == ip4[ii+1].s_addr ||
874 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
884 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
889 else if (ip6s & (sizeof(*ip6) - 1)) {
893 ch_flags |= PR_IP6_USER;
894 pr_flags |= PR_IP6_USER;
896 ip6s /= sizeof(*ip6);
897 if (ip6s > jail_max_af_ips) {
899 vfs_opterror(opts, "too many IPv6 addresses");
902 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
903 bcopy(op, ip6, ip6s * sizeof(*ip6));
905 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
907 for (ii = 0; ii < ip6s; ii++) {
908 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
913 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
914 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
924 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
925 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
928 "vnet jails cannot have IP address restrictions");
933 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
939 if (flags & JAIL_UPDATE) {
942 "osrelease cannot be changed after creation");
945 if (len == 0 || osrelstr[len - 1] != '\0') {
949 if (len >= OSRELEASELEN) {
950 error = ENAMETOOLONG;
952 "osrelease string must be 1-%d bytes long",
958 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
964 if (flags & JAIL_UPDATE) {
967 "osreldate cannot be changed after creation");
972 vfs_opterror(opts, "osreldate cannot be 0");
978 error = vfs_getopt(opts, "path", (void **)&path, &len);
984 if (flags & JAIL_UPDATE) {
987 "path cannot be changed after creation");
990 if (len == 0 || path[len - 1] != '\0') {
994 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
1000 NDFREE(&nd, NDF_ONLY_PNBUF);
1001 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1002 strlcpy(g_path, path, MAXPATHLEN);
1003 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1007 /* exit on other errors */
1010 if (root->v_type != VDIR) {
1019 * Find the specified jail, or at least its parent.
1020 * This abuses the file error codes ENOENT and EEXIST.
1024 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1025 namelc = strrchr(name, '.');
1026 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1030 sx_xlock(&allprison_lock);
1031 drflags = PD_LIST_XLOCKED;
1033 if (!prison_isalive(ppr)) {
1034 /* This jail is dying. This process will surely follow. */
1041 vfs_opterror(opts, "negative jid");
1045 * See if a requested jid already exists. Keep track of
1046 * where it can be inserted later.
1048 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1049 if (inspr->pr_id < jid)
1051 if (inspr->pr_id > jid)
1054 mtx_lock(&pr->pr_mtx);
1055 drflags |= PD_LOCKED;
1060 /* Create: jid must not exist. */
1061 if (cuflags == JAIL_CREATE) {
1063 * Even creators that cannot see the jail will
1067 vfs_opterror(opts, "jail %d already exists",
1071 if (!prison_ischild(mypr, pr)) {
1073 * Updaters get ENOENT if they cannot see the
1074 * jail. This is true even for CREATE | UPDATE,
1075 * which normally cannot give this error.
1078 vfs_opterror(opts, "jail %d not found", jid);
1081 ppr = pr->pr_parent;
1082 if (!prison_isalive(ppr)) {
1084 vfs_opterror(opts, "jail %d is dying",
1088 if (!prison_isalive(pr)) {
1089 if (!(flags & JAIL_DYING)) {
1091 vfs_opterror(opts, "jail %d is dying",
1095 if ((flags & JAIL_ATTACH) ||
1096 (pr_flags & PR_PERSIST)) {
1098 * A dying jail might be resurrected
1099 * (via attach or persist), but first
1100 * it must determine if another jail
1101 * has claimed its name. Accomplish
1102 * this by implicitly re-setting the
1106 name = prison_name(mypr, pr);
1110 /* Update: jid must exist. */
1111 if (cuflags == JAIL_UPDATE) {
1113 vfs_opterror(opts, "jail %d not found", jid);
1119 * If the caller provided a name, look for a jail by that name.
1120 * This has different semantics for creates and updates keyed by jid
1121 * (where the name must not already exist in a different jail),
1122 * and updates keyed by the name itself (where the name must exist
1123 * because that is the jail being updated).
1127 namelc = strrchr(name, '.');
1132 * This is a hierarchical name. Split it into the
1133 * parent and child names, and make sure the parent
1134 * exists or matches an already found jail.
1137 if (strncmp(name, ppr->pr_name, namelc - name)
1138 || ppr->pr_name[namelc - name] != '\0') {
1141 "cannot change jail's parent");
1146 ppr = prison_find_name(mypr, name);
1150 "jail \"%s\" not found", name);
1153 mtx_unlock(&ppr->pr_mtx);
1154 if (!prison_isalive(ppr)) {
1157 "jail \"%s\" is dying", name);
1164 if (namelc[0] != '\0') {
1166 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1168 FOREACH_PRISON_CHILD(ppr, tpr) {
1170 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1171 if (prison_isalive(tpr)) {
1173 cuflags != JAIL_CREATE) {
1179 mtx_lock(&pr->pr_mtx);
1180 drflags |= PD_LOCKED;
1184 * Create, or update(jid):
1185 * name must not exist in an
1186 * active sibling jail.
1190 "jail \"%s\" already exists",
1195 cuflags != JAIL_CREATE) {
1200 /* If no active jail is found, use a dying one. */
1201 if (deadpr != NULL && pr == NULL) {
1202 if (flags & JAIL_DYING) {
1204 mtx_lock(&pr->pr_mtx);
1205 drflags |= PD_LOCKED;
1206 } else if (cuflags == JAIL_UPDATE) {
1209 "jail \"%s\" is dying", name);
1213 /* Update: name must exist if no jid. */
1214 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1216 vfs_opterror(opts, "jail \"%s\" not found",
1222 /* Update: must provide a jid or name. */
1223 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1225 vfs_opterror(opts, "update specified no jail");
1229 /* If there's no prison to update, create a new one and link it in. */
1230 created = pr == NULL;
1232 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1233 if (tpr->pr_childcount >= tpr->pr_childmax) {
1235 vfs_opterror(opts, "prison limit exceeded");
1238 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1240 vfs_opterror(opts, "no available jail IDs");
1244 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1245 pr->pr_state = PRISON_STATE_INVALID;
1246 refcount_init(&pr->pr_ref, 1);
1247 refcount_init(&pr->pr_uref, 0);
1248 drflags |= PD_DEREF;
1249 LIST_INIT(&pr->pr_children);
1250 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1251 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1255 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1257 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1259 pr->pr_parent = ppr;
1261 prison_proc_hold(ppr);
1262 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1263 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1264 tpr->pr_childcount++;
1266 /* Set some default values, and inherit some from the parent. */
1271 root = mypr->pr_root;
1274 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1275 pr->pr_flags |= PR_HOST;
1276 #if defined(INET) || defined(INET6)
1278 if (!(pr_flags & PR_VNET))
1282 if (!(ch_flags & PR_IP4_USER))
1283 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1284 else if (!(pr_flags & PR_IP4_USER)) {
1285 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1286 if (ppr->pr_ip4 != NULL) {
1287 pr->pr_ip4s = ppr->pr_ip4s;
1288 pr->pr_ip4 = malloc(pr->pr_ip4s *
1289 sizeof(struct in_addr), M_PRISON,
1291 bcopy(ppr->pr_ip4, pr->pr_ip4,
1292 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1297 if (!(ch_flags & PR_IP6_USER))
1298 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1299 else if (!(pr_flags & PR_IP6_USER)) {
1300 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1301 if (ppr->pr_ip6 != NULL) {
1302 pr->pr_ip6s = ppr->pr_ip6s;
1303 pr->pr_ip6 = malloc(pr->pr_ip6s *
1304 sizeof(struct in6_addr), M_PRISON,
1306 bcopy(ppr->pr_ip6, pr->pr_ip6,
1307 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1313 /* Source address selection is always on by default. */
1314 pr->pr_flags |= _PR_IP_SADDRSEL;
1316 pr->pr_securelevel = ppr->pr_securelevel;
1317 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1318 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1319 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1321 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1322 if (osrelstr == NULL)
1323 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1324 sizeof(pr->pr_osrelease));
1326 strlcpy(pr->pr_osrelease, osrelstr,
1327 sizeof(pr->pr_osrelease));
1330 /* Allocate a new vnet if specified. */
1331 pr->pr_vnet = (pr_flags & PR_VNET)
1332 ? vnet_alloc() : ppr->pr_vnet;
1335 * Allocate a dedicated cpuset for each jail.
1336 * Unlike other initial settings, this may return an error.
1338 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1342 mtx_lock(&pr->pr_mtx);
1343 drflags |= PD_LOCKED;
1346 * Grab a reference for existing prisons, to ensure they
1347 * continue to exist for the duration of the call.
1350 drflags |= PD_DEREF;
1351 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1352 if ((pr->pr_flags & PR_VNET) &&
1353 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1356 "vnet jails cannot have IP address restrictions");
1361 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1364 "ip4 cannot be changed after creation");
1369 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1372 "ip6 cannot be changed after creation");
1378 /* Do final error checking before setting anything. */
1380 if (slevel < ppr->pr_securelevel) {
1386 if (childmax >= ppr->pr_childmax) {
1392 if (enforce < ppr->pr_enforce_statfs) {
1399 * devfs_rsnum is a uint16_t
1401 if (rsnum < 0 || rsnum > 65535) {
1406 * Nested jails always inherit parent's devfs ruleset
1408 if (jailed(td->td_ucred)) {
1409 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1413 rsnum = ppr->pr_devfs_rsnum;
1418 if (ppr->pr_flags & PR_IP4) {
1420 * Make sure the new set of IP addresses is a
1421 * subset of the parent's list. Don't worry
1422 * about the parent being unlocked, as any
1423 * setting is done with allprison_lock held.
1425 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1426 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1428 if (ij == ppr->pr_ip4s) {
1433 for (ii = ij = 1; ii < ip4s; ii++) {
1434 if (ip4[ii].s_addr ==
1435 ppr->pr_ip4[0].s_addr)
1437 for (; ij < ppr->pr_ip4s; ij++)
1438 if (ip4[ii].s_addr ==
1439 ppr->pr_ip4[ij].s_addr)
1441 if (ij == ppr->pr_ip4s)
1444 if (ij == ppr->pr_ip4s) {
1451 * Check for conflicting IP addresses. We permit them
1452 * if there is no more than one IP on each jail. If
1453 * there is a duplicate on a jail with more than one
1454 * IP stop checking and return error.
1457 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1458 if (tppr->pr_flags & PR_VNET)
1463 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1466 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1468 !prison_isalive(tpr)) {
1472 if (!(tpr->pr_flags & PR_IP4_USER))
1475 if (tpr->pr_ip4 == NULL ||
1476 (ip4s == 1 && tpr->pr_ip4s == 1))
1478 for (ii = 0; ii < ip4s; ii++) {
1479 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1483 "IPv4 addresses clash");
1492 if (ppr->pr_flags & PR_IP6) {
1494 * Make sure the new set of IP addresses is a
1495 * subset of the parent's list.
1497 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1498 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1501 if (ij == ppr->pr_ip6s) {
1506 for (ii = ij = 1; ii < ip6s; ii++) {
1507 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1510 for (; ij < ppr->pr_ip6s; ij++)
1511 if (IN6_ARE_ADDR_EQUAL(
1512 &ip6[ii], &ppr->pr_ip6[ij]))
1514 if (ij == ppr->pr_ip6s)
1517 if (ij == ppr->pr_ip6s) {
1523 /* Check for conflicting IP addresses. */
1525 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1526 if (tppr->pr_flags & PR_VNET)
1531 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1534 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1536 !prison_isalive(tpr)) {
1540 if (!(tpr->pr_flags & PR_IP6_USER))
1543 if (tpr->pr_ip6 == NULL ||
1544 (ip6s == 1 && tpr->pr_ip6s == 1))
1546 for (ii = 0; ii < ip6s; ii++) {
1547 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1551 "IPv6 addresses clash");
1558 onamelen = namelen = 0;
1559 if (namelc != NULL) {
1560 /* Give a default name of the jid. Also allow the name to be
1561 * explicitly the jid - but not any other number, and only in
1562 * normal form (no leading zero/etc).
1564 if (namelc[0] == '\0')
1565 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1566 else if ((strtoul(namelc, &p, 10) != jid ||
1567 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1570 "name cannot be numeric (unless it is the jid)");
1574 * Make sure the name isn't too long for the prison or its
1577 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1578 onamelen = strlen(pr->pr_name + pnamelen);
1579 namelen = strlen(namelc);
1580 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1581 error = ENAMETOOLONG;
1584 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1585 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1586 sizeof(pr->pr_name)) {
1587 error = ENAMETOOLONG;
1592 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1593 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1599 * Let modules check their parameters. This requires unlocking and
1600 * then re-locking the prison, but this is still a valid state as long
1601 * as allprison_lock remains xlocked.
1603 mtx_unlock(&pr->pr_mtx);
1604 drflags &= ~PD_LOCKED;
1605 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1608 mtx_lock(&pr->pr_mtx);
1609 drflags |= PD_LOCKED;
1611 /* At this point, all valid parameters should have been noted. */
1612 TAILQ_FOREACH(opt, opts, link) {
1613 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1615 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1620 /* Set the parameters of the prison. */
1623 if (pr_flags & PR_IP4_USER) {
1624 pr->pr_flags |= PR_IP4;
1625 free(pr->pr_ip4, M_PRISON);
1629 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1631 if (tpr->pr_flags & PR_VNET) {
1636 if (prison_restrict_ip4(tpr, NULL)) {
1645 if (pr_flags & PR_IP6_USER) {
1646 pr->pr_flags |= PR_IP6;
1647 free(pr->pr_ip6, M_PRISON);
1651 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1653 if (tpr->pr_flags & PR_VNET) {
1658 if (prison_restrict_ip6(tpr, NULL)) {
1666 pr->pr_securelevel = slevel;
1667 /* Set all child jails to be at least this level. */
1668 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1669 if (tpr->pr_securelevel < slevel)
1670 tpr->pr_securelevel = slevel;
1673 pr->pr_childmax = childmax;
1674 /* Set all child jails to under this limit. */
1675 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1676 if (tpr->pr_childmax > childmax - level)
1677 tpr->pr_childmax = childmax > level
1678 ? childmax - level : 0;
1681 pr->pr_enforce_statfs = enforce;
1682 /* Pass this restriction on to the children. */
1683 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1684 if (tpr->pr_enforce_statfs < enforce)
1685 tpr->pr_enforce_statfs = enforce;
1688 pr->pr_devfs_rsnum = rsnum;
1689 /* Pass this restriction on to the children. */
1690 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1691 tpr->pr_devfs_rsnum = rsnum;
1693 if (namelc != NULL) {
1694 if (ppr == &prison0)
1695 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1697 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1698 ppr->pr_name, namelc);
1699 /* Change this component of child names. */
1700 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1701 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1702 strlen(tpr->pr_name + onamelen) + 1);
1703 bcopy(pr->pr_name, tpr->pr_name, namelen);
1707 /* Try to keep a real-rooted full pathname. */
1708 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1712 if (PR_HOST & ch_flags & ~pr_flags) {
1713 if (pr->pr_flags & PR_HOST) {
1715 * Copy the parent's host info. As with pr_ip4 above,
1716 * the lack of a lock on the parent is not a problem;
1717 * it is always set with allprison_lock at least
1718 * shared, and is held exclusively here.
1720 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1721 sizeof(pr->pr_hostname));
1722 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1723 sizeof(pr->pr_domainname));
1724 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1725 sizeof(pr->pr_hostuuid));
1726 pr->pr_hostid = pr->pr_parent->pr_hostid;
1728 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1729 /* Set this prison, and any descendants without PR_HOST. */
1731 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1733 strlcpy(pr->pr_domainname, domain,
1734 sizeof(pr->pr_domainname));
1736 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1738 pr->pr_hostid = hid;
1739 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1740 if (tpr->pr_flags & PR_HOST)
1744 strlcpy(tpr->pr_hostname,
1746 sizeof(tpr->pr_hostname));
1748 strlcpy(tpr->pr_domainname,
1750 sizeof(tpr->pr_domainname));
1752 strlcpy(tpr->pr_hostuuid,
1754 sizeof(tpr->pr_hostuuid));
1756 tpr->pr_hostid = hid;
1760 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1761 if ((tallow = ch_allow & ~pr_allow))
1762 prison_set_allow_locked(pr, tallow, 0);
1764 * Persistent prisons get an extra reference, and prisons losing their
1765 * persist flag lose that reference.
1767 born = !prison_isalive(pr);
1768 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1769 if (pr_flags & PR_PERSIST) {
1772 * This may make a dead prison alive again, but wait
1773 * to label it as such until after OSD calls have had
1774 * a chance to run (and perhaps to fail).
1776 refcount_acquire(&pr->pr_uref);
1778 drflags |= PD_DEUREF;
1779 prison_free_not_last(pr);
1782 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1783 mtx_unlock(&pr->pr_mtx);
1784 drflags &= ~PD_LOCKED;
1786 * Any errors past this point will need to de-persist newly created
1787 * prisons, as well as call remove methods.
1793 if (racct_enable && created)
1794 prison_racct_attach(pr);
1797 /* Locks may have prevented a complete restriction of child IP
1798 * addresses. If so, allocate some more memory and try again.
1803 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1804 mtx_lock(&pr->pr_mtx);
1806 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1808 if (tpr->pr_flags & PR_VNET) {
1813 if (prison_restrict_ip4(tpr, ip4)) {
1820 mtx_unlock(&pr->pr_mtx);
1826 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1827 mtx_lock(&pr->pr_mtx);
1829 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1831 if (tpr->pr_flags & PR_VNET) {
1836 if (prison_restrict_ip6(tpr, ip6)) {
1843 mtx_unlock(&pr->pr_mtx);
1847 /* Let the modules do their work. */
1849 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1853 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1858 * A new prison is now ready to be seen; either it has gained a user
1859 * reference via persistence, or is about to gain one via attachment.
1862 drflags = prison_lock_xlock(pr, drflags);
1863 pr->pr_state = PRISON_STATE_ALIVE;
1866 /* Attach this process to the prison if requested. */
1867 if (flags & JAIL_ATTACH) {
1868 error = do_jail_attach(td, pr,
1869 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
1870 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1872 vfs_opterror(opts, "attach failed");
1878 if (racct_enable && !created) {
1879 if (drflags & PD_LOCKED) {
1880 mtx_unlock(&pr->pr_mtx);
1881 drflags &= ~PD_LOCKED;
1883 if (drflags & PD_LIST_XLOCKED) {
1884 sx_xunlock(&allprison_lock);
1885 drflags &= ~PD_LIST_XLOCKED;
1887 prison_racct_modify(pr);
1891 if (born && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 &&
1892 (pr->pr_root->v_vflag & VV_ROOT) == 0)
1893 printf("Warning jail jid=%d: mountd/nfsd requires a separate"
1894 " file system\n", pr->pr_id);
1896 drflags &= ~PD_KILL;
1897 td->td_retval[0] = pr->pr_id;
1900 /* Release any temporary prison holds and/or locks. */
1902 prison_deref(pr, drflags);
1903 else if (drflags & PD_LIST_SLOCKED)
1904 sx_sunlock(&allprison_lock);
1905 else if (drflags & PD_LIST_XLOCKED)
1906 sx_xunlock(&allprison_lock);
1911 /* Write the error message back to userspace. */
1912 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1913 &errmsg_len) == 0 && errmsg_len > 0) {
1914 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1915 if (optuio->uio_segflg == UIO_SYSSPACE)
1917 optuio->uio_iov[errmsg_pos].iov_base,
1921 optuio->uio_iov[errmsg_pos].iov_base,
1927 free(ip4, M_PRISON);
1930 free(ip6, M_PRISON);
1933 free(g_path, M_TEMP);
1939 * Find the next available prison ID. Return the ID on success, or zero
1940 * on failure. Also set a pointer to the allprison list entry the prison
1941 * should be inserted before.
1944 get_next_prid(struct prison **insprp)
1946 struct prison *inspr;
1949 jid = lastprid % JAIL_MAX + 1;
1950 if (TAILQ_EMPTY(&allprison) ||
1951 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1953 * A common case is for all jails to be implicitly numbered,
1954 * which means they'll go on the end of the list, at least
1955 * for the first JAIL_MAX times.
1960 * Take two passes through the allprison list: first starting
1961 * with the proposed jid, then ending with it.
1963 for (maxid = JAIL_MAX; maxid != 0; ) {
1964 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1965 if (inspr->pr_id < jid)
1967 if (inspr->pr_id > jid) {
1968 /* Found an opening. */
1972 if (++jid > maxid) {
1973 if (lastprid == maxid || lastprid == 0)
1976 * The entire legal range
1977 * has been traversed
1981 /* Try again from the start. */
1987 if (inspr == NULL) {
1988 /* Found room at the end of the list. */
1999 * struct jail_get_args {
2000 * struct iovec *iovp;
2001 * unsigned int iovcnt;
2006 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2011 /* Check that we have an even number of iovecs. */
2012 if (uap->iovcnt & 1)
2015 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2018 error = kern_jail_get(td, auio, uap->flags);
2020 error = copyout(auio->uio_iov, uap->iovp,
2021 uap->iovcnt * sizeof (struct iovec));
2027 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2029 struct bool_flags *bf;
2030 struct jailsys_flags *jsf;
2031 struct prison *pr, *mypr;
2033 struct vfsoptlist *opts;
2034 char *errmsg, *name;
2035 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2038 if (flags & ~JAIL_GET_MASK)
2041 /* Get the parameter list. */
2042 error = vfs_buildopts(optuio, &opts);
2045 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2046 mypr = td->td_ucred->cr_prison;
2050 * Find the prison specified by one of: lastjid, jid, name.
2052 sx_slock(&allprison_lock);
2053 drflags = PD_LIST_SLOCKED;
2054 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2056 TAILQ_FOREACH(pr, &allprison, pr_list) {
2057 if (pr->pr_id > jid &&
2058 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2059 prison_ischild(mypr, pr)) {
2060 mtx_lock(&pr->pr_mtx);
2061 drflags |= PD_LOCKED;
2066 vfs_opterror(opts, "no jail after %d", jid);
2068 } else if (error != ENOENT)
2071 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2074 pr = prison_find_child(mypr, jid);
2076 drflags |= PD_LOCKED;
2077 if (!(prison_isalive(pr) ||
2078 (flags & JAIL_DYING))) {
2080 vfs_opterror(opts, "jail %d is dying",
2087 vfs_opterror(opts, "jail %d not found", jid);
2090 } else if (error != ENOENT)
2093 error = vfs_getopt(opts, "name", (void **)&name, &len);
2095 if (len == 0 || name[len - 1] != '\0') {
2099 pr = prison_find_name(mypr, name);
2101 drflags |= PD_LOCKED;
2102 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2104 vfs_opterror(opts, "jail \"%s\" is dying",
2111 vfs_opterror(opts, "jail \"%s\" not found", name);
2113 } else if (error != ENOENT)
2116 vfs_opterror(opts, "no jail specified");
2121 /* Get the parameters of the prison. */
2123 drflags |= PD_DEREF;
2124 td->td_retval[0] = pr->pr_id;
2125 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2126 if (error != 0 && error != ENOENT)
2128 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2129 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2130 if (error != 0 && error != ENOENT)
2132 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2133 if (error != 0 && error != ENOENT)
2135 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2136 sizeof(pr->pr_cpuset->cs_id));
2137 if (error != 0 && error != ENOENT)
2139 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2140 if (error != 0 && error != ENOENT)
2143 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2144 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2145 if (error != 0 && error != ENOENT)
2149 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2150 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2151 if (error != 0 && error != ENOENT)
2154 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2155 sizeof(pr->pr_securelevel));
2156 if (error != 0 && error != ENOENT)
2158 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2159 sizeof(pr->pr_childcount));
2160 if (error != 0 && error != ENOENT)
2162 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2163 sizeof(pr->pr_childmax));
2164 if (error != 0 && error != ENOENT)
2166 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2167 if (error != 0 && error != ENOENT)
2169 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2170 if (error != 0 && error != ENOENT)
2172 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2173 if (error != 0 && error != ENOENT)
2175 #ifdef COMPAT_FREEBSD32
2176 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2177 uint32_t hid32 = pr->pr_hostid;
2179 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2182 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2183 sizeof(pr->pr_hostid));
2184 if (error != 0 && error != ENOENT)
2186 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2187 sizeof(pr->pr_enforce_statfs));
2188 if (error != 0 && error != ENOENT)
2190 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2191 sizeof(pr->pr_devfs_rsnum));
2192 if (error != 0 && error != ENOENT)
2194 for (bf = pr_flag_bool;
2195 bf < pr_flag_bool + nitems(pr_flag_bool);
2197 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2198 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2199 if (error != 0 && error != ENOENT)
2202 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2203 if (error != 0 && error != ENOENT)
2206 for (jsf = pr_flag_jailsys;
2207 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2209 f = pr->pr_flags & (jsf->disable | jsf->new);
2210 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2211 : (f == jsf->new) ? JAIL_SYS_NEW
2213 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2214 if (error != 0 && error != ENOENT)
2217 for (bf = pr_flag_allow;
2218 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2219 atomic_load_int(&bf->flag) != 0;
2221 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2222 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2223 if (error != 0 && error != ENOENT)
2226 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2227 if (error != 0 && error != ENOENT)
2230 i = !prison_isalive(pr);
2231 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2232 if (error != 0 && error != ENOENT)
2235 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2236 if (error != 0 && error != ENOENT)
2238 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2239 sizeof(pr->pr_osreldate));
2240 if (error != 0 && error != ENOENT)
2242 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2243 if (error != 0 && error != ENOENT)
2246 /* Get the module parameters. */
2247 mtx_unlock(&pr->pr_mtx);
2248 drflags &= ~PD_LOCKED;
2249 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2252 prison_deref(pr, drflags);
2256 /* By now, all parameters should have been noted. */
2257 TAILQ_FOREACH(opt, opts, link) {
2258 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2260 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2265 /* Write the fetched parameters back to userspace. */
2267 TAILQ_FOREACH(opt, opts, link) {
2268 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2269 pos = 2 * opt->pos + 1;
2270 optuio->uio_iov[pos].iov_len = opt->len;
2271 if (opt->value != NULL) {
2272 if (optuio->uio_segflg == UIO_SYSSPACE) {
2274 optuio->uio_iov[pos].iov_base,
2277 error = copyout(opt->value,
2278 optuio->uio_iov[pos].iov_base,
2288 /* Release any temporary prison holds and/or locks. */
2290 prison_deref(pr, drflags);
2291 else if (drflags & PD_LIST_SLOCKED)
2292 sx_sunlock(&allprison_lock);
2293 if (error && errmsg_pos >= 0) {
2294 /* Write the error message back to userspace. */
2295 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2296 errmsg_pos = 2 * errmsg_pos + 1;
2297 if (errmsg_len > 0) {
2298 if (optuio->uio_segflg == UIO_SYSSPACE)
2300 optuio->uio_iov[errmsg_pos].iov_base,
2304 optuio->uio_iov[errmsg_pos].iov_base,
2313 * struct jail_remove_args {
2318 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2323 error = priv_check(td, PRIV_JAIL_REMOVE);
2327 sx_xlock(&allprison_lock);
2328 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2330 sx_xunlock(&allprison_lock);
2333 if (!prison_isalive(pr)) {
2334 /* Silently ignore already-dying prisons. */
2335 mtx_unlock(&pr->pr_mtx);
2336 sx_xunlock(&allprison_lock);
2339 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2344 * struct jail_attach_args {
2349 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2354 error = priv_check(td, PRIV_JAIL_ATTACH);
2358 sx_slock(&allprison_lock);
2359 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2361 sx_sunlock(&allprison_lock);
2365 /* Do not allow a process to attach to a prison that is not alive. */
2366 if (!prison_isalive(pr)) {
2367 mtx_unlock(&pr->pr_mtx);
2368 sx_sunlock(&allprison_lock);
2372 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2376 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2379 struct ucred *newcred, *oldcred;
2382 mtx_assert(&pr->pr_mtx, MA_OWNED);
2383 sx_assert(&allprison_lock, SX_LOCKED);
2384 drflags &= PD_LOCK_FLAGS;
2386 * XXX: Note that there is a slight race here if two threads
2387 * in the same privileged process attempt to attach to two
2388 * different jails at the same time. It is important for
2389 * user processes not to do this, or they might end up with
2390 * a process root from one prison, but attached to the jail
2394 refcount_acquire(&pr->pr_uref);
2395 drflags |= PD_DEREF | PD_DEUREF;
2396 mtx_unlock(&pr->pr_mtx);
2397 drflags &= ~PD_LOCKED;
2399 /* Let modules do whatever they need to prepare for attaching. */
2400 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2402 prison_deref(pr, drflags);
2405 sx_unlock(&allprison_lock);
2406 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2409 * Reparent the newly attached process to this jail.
2412 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2416 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2417 if ((error = change_dir(pr->pr_root, td)) != 0)
2420 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2423 VOP_UNLOCK(pr->pr_root);
2424 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2429 oldcred = crcopysafe(p, newcred);
2430 newcred->cr_prison = pr;
2431 proc_set_cred(p, newcred);
2434 racct_proc_ucred_changed(p, oldcred, newcred);
2439 rctl_proc_ucred_changed(p, newcred);
2442 prison_deref(oldcred->cr_prison, drflags);
2446 * If the prison was killed while changing credentials, die along
2449 if (!prison_isalive(pr)) {
2451 kern_psignal(p, SIGKILL);
2458 VOP_UNLOCK(pr->pr_root);
2460 /* Tell modules this thread is still in its old jail after all. */
2461 sx_slock(&allprison_lock);
2462 drflags |= PD_LIST_SLOCKED;
2463 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2464 prison_deref(pr, drflags);
2469 * Returns a locked prison instance, or NULL on failure.
2472 prison_find(int prid)
2476 sx_assert(&allprison_lock, SX_LOCKED);
2477 TAILQ_FOREACH(pr, &allprison, pr_list) {
2478 if (pr->pr_id < prid)
2480 if (pr->pr_id > prid)
2482 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2483 mtx_lock(&pr->pr_mtx);
2490 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2493 prison_find_child(struct prison *mypr, int prid)
2498 sx_assert(&allprison_lock, SX_LOCKED);
2499 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2500 if (pr->pr_id == prid) {
2501 KASSERT(prison_isvalid(pr),
2502 ("Found invalid prison %p", pr));
2503 mtx_lock(&pr->pr_mtx);
2511 * Look for the name relative to mypr. Returns a locked prison or NULL.
2514 prison_find_name(struct prison *mypr, const char *name)
2516 struct prison *pr, *deadpr;
2520 sx_assert(&allprison_lock, SX_LOCKED);
2521 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2523 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2524 if (!strcmp(pr->pr_name + mylen, name)) {
2525 KASSERT(prison_isvalid(pr),
2526 ("Found invalid prison %p", pr));
2527 if (prison_isalive(pr)) {
2528 mtx_lock(&pr->pr_mtx);
2534 /* There was no valid prison - perhaps there was a dying one. */
2536 mtx_lock(&deadpr->pr_mtx);
2541 * See if a prison has the specific flag set. The prison should be locked,
2542 * unless checking for flags that are only set at jail creation (such as
2543 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2544 * to any other prison data.
2547 prison_flag(struct ucred *cred, unsigned flag)
2550 return (cred->cr_prison->pr_flags & flag);
2554 prison_allow(struct ucred *cred, unsigned flag)
2557 return ((cred->cr_prison->pr_allow & flag) != 0);
2561 * Hold a prison reference, by incrementing pr_ref. It is generally
2562 * an error to hold a prison that does not already have a reference.
2563 * A prison record will remain valid as long as it has at least one
2564 * reference, and will not be removed as long as either the prison
2565 * mutex or the allprison lock is held (allprison_lock may be shared).
2568 prison_hold_locked(struct prison *pr)
2571 /* Locking is no longer required. */
2576 prison_hold(struct prison *pr)
2579 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2582 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2584 refcount_acquire(&pr->pr_ref);
2589 * Remove a prison reference. If that was the last reference, the
2590 * prison will be removed (at a later time).
2593 prison_free_locked(struct prison *pr)
2596 mtx_assert(&pr->pr_mtx, MA_OWNED);
2598 * Locking is no longer required, but unlock because the caller
2601 mtx_unlock(&pr->pr_mtx);
2606 prison_free(struct prison *pr)
2609 KASSERT(refcount_load(&pr->pr_ref) > 0,
2610 ("Trying to free dead prison %p (jid=%d).",
2612 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2614 * Don't remove the last reference in this context,
2615 * in case there are locks held.
2617 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2622 prison_free_not_last(struct prison *pr)
2627 KASSERT(refcount_load(&pr->pr_ref) > 0,
2628 ("Trying to free dead prison %p (jid=%d).",
2630 lastref = refcount_release(&pr->pr_ref);
2632 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2635 refcount_release(&pr->pr_ref);
2640 * Hold a prison for user visibility, by incrementing pr_uref.
2641 * It is generally an error to hold a prison that isn't already
2642 * user-visible, except through the the jail system calls. It is also
2643 * an error to hold an invalid prison. A prison record will remain
2644 * alive as long as it has at least one user reference, and will not
2645 * be set to the dying state until the prison mutex and allprison_lock
2649 prison_proc_hold(struct prison *pr)
2652 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2655 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2657 refcount_acquire(&pr->pr_uref);
2662 * Remove a prison user reference. If it was the last reference, the
2663 * prison will be considered "dying", and may be removed once all of
2664 * its references are dropped.
2667 prison_proc_free(struct prison *pr)
2671 * Locking is only required when releasing the last reference.
2672 * This allows assurance that a locked prison will remain alive
2673 * until it is unlocked.
2675 KASSERT(refcount_load(&pr->pr_uref) > 0,
2676 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2677 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2679 * Don't remove the last user reference in this context,
2680 * which is expected to be a process that is not only locked,
2681 * but also half dead. Add a reference so any calls to
2682 * prison_free() won't re-submit the task.
2685 mtx_lock(&pr->pr_mtx);
2686 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2687 ("Redundant last reference in prison_proc_free (jid=%d)",
2689 pr->pr_flags |= PR_COMPLETE_PROC;
2690 mtx_unlock(&pr->pr_mtx);
2691 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2696 prison_proc_free_not_last(struct prison *pr)
2701 KASSERT(refcount_load(&pr->pr_uref) > 0,
2702 ("Trying to free dead prison %p (jid=%d).",
2704 lastref = refcount_release(&pr->pr_uref);
2706 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2709 refcount_release(&pr->pr_uref);
2714 * Complete a call to either prison_free or prison_proc_free.
2717 prison_complete(void *context, int pending)
2719 struct prison *pr = context;
2723 * This could be called to release the last reference, or the last
2724 * user reference (plus the reference held in prison_proc_free).
2726 drflags = prison_lock_xlock(pr, PD_DEREF);
2727 if (pr->pr_flags & PR_COMPLETE_PROC) {
2728 pr->pr_flags &= ~PR_COMPLETE_PROC;
2729 drflags |= PD_DEUREF;
2731 prison_deref(pr, drflags);
2735 * Remove a prison reference and/or user reference (usually).
2736 * This assumes context that allows sleeping (for allprison_lock),
2737 * with no non-sleeping locks held, except perhaps the prison itself.
2738 * If there are no more references, release and delist the prison.
2739 * On completion, the prison lock and the allprison lock are both
2743 prison_deref(struct prison *pr, int flags)
2745 struct prisonlist freeprison;
2746 struct prison *killpr, *rpr, *ppr, *tpr;
2750 TAILQ_INIT(&freeprison);
2752 * Release this prison as requested, which may cause its parent
2753 * to be released, and then maybe its grandparent, etc.
2756 if (flags & PD_KILL) {
2757 /* Kill the prison and its descendents. */
2758 KASSERT(pr != &prison0,
2759 ("prison_deref trying to kill prison0"));
2760 if (!(flags & PD_DEREF)) {
2764 flags = prison_lock_xlock(pr, flags);
2765 prison_deref_kill(pr, &freeprison);
2767 if (flags & PD_DEUREF) {
2768 /* Drop a user reference. */
2769 KASSERT(refcount_load(&pr->pr_uref) > 0,
2770 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2772 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2773 if (!(flags & PD_DEREF)) {
2777 flags = prison_lock_xlock(pr, flags);
2778 if (refcount_release(&pr->pr_uref) &&
2779 pr->pr_state == PRISON_STATE_ALIVE) {
2781 * When the last user references goes,
2782 * this becomes a dying prison.
2785 refcount_load(&prison0.pr_uref) > 0,
2786 ("prison0 pr_uref=0"));
2787 pr->pr_state = PRISON_STATE_DYING;
2788 mtx_unlock(&pr->pr_mtx);
2789 flags &= ~PD_LOCKED;
2794 if (flags & PD_KILL) {
2796 * Any remaining user references are probably processes
2797 * that need to be killed, either in this prison or its
2800 if (refcount_load(&pr->pr_uref) > 0)
2802 /* Make sure the parent prison doesn't get killed. */
2805 if (flags & PD_DEREF) {
2806 /* Drop a reference. */
2807 KASSERT(refcount_load(&pr->pr_ref) > 0,
2808 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2810 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2811 flags = prison_lock_xlock(pr, flags);
2812 if (refcount_release(&pr->pr_ref)) {
2814 * When the last reference goes,
2815 * unlink the prison and set it aside.
2818 refcount_load(&pr->pr_uref) == 0,
2819 ("prison_deref: last ref, "
2820 "but still has %d urefs (jid=%d)",
2821 pr->pr_uref, pr->pr_id));
2823 refcount_load(&prison0.pr_ref) != 0,
2824 ("prison0 pr_ref=0"));
2825 pr->pr_state = PRISON_STATE_INVALID;
2826 TAILQ_REMOVE(&allprison, pr, pr_list);
2827 LIST_REMOVE(pr, pr_sibling);
2828 TAILQ_INSERT_TAIL(&freeprison, pr,
2830 for (ppr = pr->pr_parent;
2832 ppr = ppr->pr_parent)
2833 ppr->pr_childcount--;
2835 * Removing a prison frees references
2838 mtx_unlock(&pr->pr_mtx);
2839 flags &= ~PD_LOCKED;
2841 flags |= PD_DEREF | PD_DEUREF;
2849 /* Release all the prison locks. */
2850 if (flags & PD_LOCKED)
2851 mtx_unlock(&pr->pr_mtx);
2852 if (flags & PD_LIST_SLOCKED)
2853 sx_sunlock(&allprison_lock);
2854 else if (flags & PD_LIST_XLOCKED)
2855 sx_xunlock(&allprison_lock);
2857 /* Kill any processes attached to a killed prison. */
2858 if (killpr != NULL) {
2859 sx_slock(&allproc_lock);
2860 FOREACH_PROC_IN_SYSTEM(p) {
2862 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
2863 for (ppr = p->p_ucred->cr_prison;
2865 ppr = ppr->pr_parent)
2866 if (ppr == killpr) {
2867 kern_psignal(p, SIGKILL);
2873 sx_sunlock(&allproc_lock);
2877 * Finish removing any unreferenced prisons, which couldn't happen
2878 * while allprison_lock was held (to avoid a LOR on vrele).
2880 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
2882 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
2883 vnet_destroy(rpr->pr_vnet);
2885 if (rpr->pr_root != NULL)
2886 vrele(rpr->pr_root);
2887 mtx_destroy(&rpr->pr_mtx);
2889 free(rpr->pr_ip4, M_PRISON);
2892 free(rpr->pr_ip6, M_PRISON);
2894 if (rpr->pr_cpuset != NULL)
2895 cpuset_rel(rpr->pr_cpuset);
2899 prison_racct_detach(rpr);
2901 TAILQ_REMOVE(&freeprison, rpr, pr_list);
2902 free(rpr, M_PRISON);
2907 * Kill the prison and its descendants. Mark them as dying, clear the
2908 * persist flag, and call module remove methods.
2911 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
2913 struct prison *cpr, *ppr, *rpr;
2917 * Unlike the descendants, the target prison can be killed
2918 * even if it is currently dying. This is useful for failed
2919 * creation in jail_set(2).
2921 KASSERT(refcount_load(&pr->pr_ref) > 0,
2922 ("Trying to kill dead prison %p (jid=%d).",
2924 refcount_acquire(&pr->pr_uref);
2925 pr->pr_state = PRISON_STATE_DYING;
2926 mtx_unlock(&pr->pr_mtx);
2929 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
2931 if (!prison_isalive(cpr)) {
2936 prison_proc_hold(cpr);
2937 mtx_lock(&cpr->pr_mtx);
2938 cpr->pr_state = PRISON_STATE_DYING;
2939 cpr->pr_flags |= PR_REMOVE;
2940 mtx_unlock(&cpr->pr_mtx);
2943 if (!(cpr->pr_flags & PR_REMOVE))
2945 prison_cleanup(cpr);
2946 mtx_lock(&cpr->pr_mtx);
2947 cpr->pr_flags &= ~PR_REMOVE;
2948 if (cpr->pr_flags & PR_PERSIST) {
2949 cpr->pr_flags &= ~PR_PERSIST;
2950 prison_proc_free_not_last(cpr);
2951 prison_free_not_last(cpr);
2953 (void)refcount_release(&cpr->pr_uref);
2954 if (refcount_release(&cpr->pr_ref)) {
2956 * When the last reference goes, unlink the prison
2957 * and set it aside for prison_deref() to handle.
2958 * Delay unlinking the sibling list to keep the loop
2962 LIST_REMOVE(rpr, pr_sibling);
2964 rpr->pr_state = PRISON_STATE_INVALID;
2965 TAILQ_REMOVE(&allprison, rpr, pr_list);
2966 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
2968 * Removing a prison frees references from its parent.
2970 ppr = rpr->pr_parent;
2971 prison_proc_free_not_last(ppr);
2972 prison_free_not_last(ppr);
2973 for (; ppr != NULL; ppr = ppr->pr_parent)
2974 ppr->pr_childcount--;
2976 mtx_unlock(&cpr->pr_mtx);
2979 LIST_REMOVE(rpr, pr_sibling);
2982 mtx_lock(&pr->pr_mtx);
2983 if (pr->pr_flags & PR_PERSIST) {
2984 pr->pr_flags &= ~PR_PERSIST;
2985 prison_proc_free_not_last(pr);
2986 prison_free_not_last(pr);
2988 (void)refcount_release(&pr->pr_uref);
2992 * Given the current locking state in the flags, make sure allprison_lock
2993 * is held exclusive, and the prison is locked. Return flags indicating
2997 prison_lock_xlock(struct prison *pr, int flags)
3000 if (!(flags & PD_LIST_XLOCKED)) {
3002 * Get allprison_lock, which may be an upgrade,
3003 * and may require unlocking the prison.
3005 if (flags & PD_LOCKED) {
3006 mtx_unlock(&pr->pr_mtx);
3007 flags &= ~PD_LOCKED;
3009 if (flags & PD_LIST_SLOCKED) {
3010 if (!sx_try_upgrade(&allprison_lock)) {
3011 sx_sunlock(&allprison_lock);
3012 sx_xlock(&allprison_lock);
3014 flags &= ~PD_LIST_SLOCKED;
3016 sx_xlock(&allprison_lock);
3017 flags |= PD_LIST_XLOCKED;
3019 if (!(flags & PD_LOCKED)) {
3020 /* Lock the prison mutex. */
3021 mtx_lock(&pr->pr_mtx);
3028 * Release a prison's resources when it starts dying (when the last user
3029 * reference is dropped, or when it is killed).
3032 prison_cleanup(struct prison *pr)
3034 sx_assert(&allprison_lock, SA_XLOCKED);
3035 mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
3036 vfs_exjail_delete(pr);
3037 shm_remove_prison(pr);
3038 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3042 * Set or clear a permission bit in the pr_allow field, passing restrictions
3043 * (cleared permission) down to child jails.
3046 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3050 pr = cred->cr_prison;
3051 sx_slock(&allprison_lock);
3052 mtx_lock(&pr->pr_mtx);
3053 prison_set_allow_locked(pr, flag, enable);
3054 mtx_unlock(&pr->pr_mtx);
3055 sx_sunlock(&allprison_lock);
3059 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3065 pr->pr_allow |= flag;
3067 pr->pr_allow &= ~flag;
3068 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3069 cpr->pr_allow &= ~flag;
3074 * Check if a jail supports the given address family.
3076 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3080 prison_check_af(struct ucred *cred, int af)
3085 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3087 pr = cred->cr_prison;
3089 /* Prisons with their own network stack are not limited. */
3090 if (prison_owns_vnet(cred))
3099 if (pr->pr_flags & PR_IP4)
3101 mtx_lock(&pr->pr_mtx);
3102 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3103 error = EAFNOSUPPORT;
3104 mtx_unlock(&pr->pr_mtx);
3110 if (pr->pr_flags & PR_IP6)
3112 mtx_lock(&pr->pr_mtx);
3113 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3114 error = EAFNOSUPPORT;
3115 mtx_unlock(&pr->pr_mtx);
3123 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3124 error = EAFNOSUPPORT;
3130 * Check if given address belongs to the jail referenced by cred (wrapper to
3131 * prison_check_ip[46]).
3133 * Returns 0 if jail doesn't restrict the address family or if address belongs
3134 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3135 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3138 prison_if(struct ucred *cred, const struct sockaddr *sa)
3141 const struct sockaddr_in *sai;
3144 const struct sockaddr_in6 *sai6;
3148 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3149 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3152 if (prison_owns_vnet(cred))
3157 switch (sa->sa_family)
3161 sai = (const struct sockaddr_in *)sa;
3162 error = prison_check_ip4(cred, &sai->sin_addr);
3167 sai6 = (const struct sockaddr_in6 *)sa;
3168 error = prison_check_ip6(cred, &sai6->sin6_addr);
3172 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3173 error = EAFNOSUPPORT;
3179 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3182 prison_check(struct ucred *cred1, struct ucred *cred2)
3185 return ((cred1->cr_prison == cred2->cr_prison ||
3186 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3190 * For mountd/nfsd to run within a prison, it must be:
3192 * - PR_ALLOW_NFSD must be set on it.
3193 * - The root directory (pr_root) of the prison must be
3194 * a file system mount point, so the mountd can hang
3195 * export information on it.
3196 * - The prison's enforce_statfs cannot be 0, so that
3197 * mountd(8) can do exports.
3200 prison_check_nfsd(struct ucred *cred)
3203 if (jailed_without_vnet(cred))
3205 if (!prison_allow(cred, PR_ALLOW_NFSD))
3207 if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0)
3209 if (cred->cr_prison->pr_enforce_statfs == 0)
3215 * Return 1 if p2 is a child of p1, otherwise 0.
3218 prison_ischild(struct prison *pr1, struct prison *pr2)
3221 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3228 * Return true if the prison is currently alive. A prison is alive if it
3229 * holds user references and it isn't being removed.
3232 prison_isalive(struct prison *pr)
3235 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3241 * Return true if the prison is currently valid. A prison is valid if it has
3242 * been fully created, and is not being destroyed. Note that dying prisons
3243 * are still considered valid. Invalid prisons won't be found under normal
3244 * circumstances, as they're only put in that state by functions that have
3245 * an exclusive hold on allprison_lock.
3248 prison_isvalid(struct prison *pr)
3251 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3253 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3259 * Return 1 if the passed credential is in a jail and that jail does not
3260 * have its own virtual network stack, otherwise 0.
3263 jailed_without_vnet(struct ucred *cred)
3269 if (prison_owns_vnet(cred))
3277 * Return the correct hostname (domainname, et al) for the passed credential.
3280 getcredhostname(struct ucred *cred, char *buf, size_t size)
3285 * A NULL credential can be used to shortcut to the physical
3286 * system's hostname.
3288 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3289 mtx_lock(&pr->pr_mtx);
3290 strlcpy(buf, pr->pr_hostname, size);
3291 mtx_unlock(&pr->pr_mtx);
3295 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3298 mtx_lock(&cred->cr_prison->pr_mtx);
3299 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3300 mtx_unlock(&cred->cr_prison->pr_mtx);
3304 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3307 mtx_lock(&cred->cr_prison->pr_mtx);
3308 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3309 mtx_unlock(&cred->cr_prison->pr_mtx);
3313 getcredhostid(struct ucred *cred, unsigned long *hostid)
3316 mtx_lock(&cred->cr_prison->pr_mtx);
3317 *hostid = cred->cr_prison->pr_hostid;
3318 mtx_unlock(&cred->cr_prison->pr_mtx);
3322 getjailname(struct ucred *cred, char *name, size_t len)
3325 mtx_lock(&cred->cr_prison->pr_mtx);
3326 strlcpy(name, cred->cr_prison->pr_name, len);
3327 mtx_unlock(&cred->cr_prison->pr_mtx);
3332 * Determine whether the prison represented by cred owns
3333 * its vnet rather than having it inherited.
3335 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3338 prison_owns_vnet(struct ucred *cred)
3342 * vnets cannot be added/removed after jail creation,
3343 * so no need to lock here.
3345 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3350 * Determine whether the subject represented by cred can "see"
3351 * status of a mount point.
3352 * Returns: 0 for permitted, ENOENT otherwise.
3353 * XXX: This function should be called cr_canseemount() and should be
3354 * placed in kern_prot.c.
3357 prison_canseemount(struct ucred *cred, struct mount *mp)
3363 pr = cred->cr_prison;
3364 if (pr->pr_enforce_statfs == 0)
3366 if (pr->pr_root->v_mount == mp)
3368 if (pr->pr_enforce_statfs == 2)
3371 * If jail's chroot directory is set to "/" we should be able to see
3372 * all mount-points from inside a jail.
3373 * This is ugly check, but this is the only situation when jail's
3374 * directory ends with '/'.
3376 if (strcmp(pr->pr_path, "/") == 0)
3378 len = strlen(pr->pr_path);
3380 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3383 * Be sure that we don't have situation where jail's root directory
3384 * is "/some/path" and mount point is "/some/pathpath".
3386 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3392 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3394 char jpath[MAXPATHLEN];
3398 pr = cred->cr_prison;
3399 if (pr->pr_enforce_statfs == 0)
3401 if (prison_canseemount(cred, mp) != 0) {
3402 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3403 strlcpy(sp->f_mntonname, "[restricted]",
3404 sizeof(sp->f_mntonname));
3407 if (pr->pr_root->v_mount == mp) {
3409 * Clear current buffer data, so we are sure nothing from
3410 * the valid path left there.
3412 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3413 *sp->f_mntonname = '/';
3417 * If jail's chroot directory is set to "/" we should be able to see
3418 * all mount-points from inside a jail.
3420 if (strcmp(pr->pr_path, "/") == 0)
3422 len = strlen(pr->pr_path);
3423 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3425 * Clear current buffer data, so we are sure nothing from
3426 * the valid path left there.
3428 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3429 if (*jpath == '\0') {
3430 /* Should never happen. */
3431 *sp->f_mntonname = '/';
3433 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3438 * Check with permission for a specific privilege is granted within jail. We
3439 * have a specific list of accepted privileges; the rest are denied.
3442 prison_priv_check(struct ucred *cred, int priv)
3448 * Some policies have custom handlers. This routine should not be
3449 * called for them. See priv_check_cred().
3452 case PRIV_VFS_LOOKUP:
3453 case PRIV_VFS_GENERATION:
3454 KASSERT(0, ("prison_priv_check instead of a custom handler "
3455 "called for %d\n", priv));
3463 * Privileges specific to prisons with a virtual network stack.
3464 * There might be a duplicate entry here in case the privilege
3465 * is only granted conditionally in the legacy jail case.
3469 * NFS-specific privileges.
3471 case PRIV_NFS_DAEMON:
3472 case PRIV_VFS_GETFH:
3473 case PRIV_VFS_MOUNT_EXPORTED:
3474 if (!prison_check_nfsd(cred))
3477 case PRIV_NFS_LOCKD:
3480 * Network stack privileges.
3482 case PRIV_NET_BRIDGE:
3485 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3486 case PRIV_NET_ROUTE:
3488 case PRIV_NET_SETIFMTU:
3489 case PRIV_NET_SETIFFLAGS:
3490 case PRIV_NET_SETIFCAP:
3491 case PRIV_NET_SETIFDESCR:
3492 case PRIV_NET_SETIFNAME :
3493 case PRIV_NET_SETIFMETRIC:
3494 case PRIV_NET_SETIFPHYS:
3495 case PRIV_NET_SETIFMAC:
3496 case PRIV_NET_SETLANPCP:
3497 case PRIV_NET_ADDMULTI:
3498 case PRIV_NET_DELMULTI:
3499 case PRIV_NET_HWIOCTL:
3500 case PRIV_NET_SETLLADDR:
3501 case PRIV_NET_ADDIFGROUP:
3502 case PRIV_NET_DELIFGROUP:
3503 case PRIV_NET_IFCREATE:
3504 case PRIV_NET_IFDESTROY:
3505 case PRIV_NET_ADDIFADDR:
3506 case PRIV_NET_DELIFADDR:
3509 case PRIV_NET_SETIFVNET:
3510 case PRIV_NET_SETIFFIB:
3515 * 802.11-related privileges.
3517 case PRIV_NET80211_VAP_GETKEY:
3518 case PRIV_NET80211_VAP_MANAGE:
3524 case PRIV_NETATM_CFG:
3525 case PRIV_NETATM_ADD:
3526 case PRIV_NETATM_DEL:
3527 case PRIV_NETATM_SET:
3530 * Bluetooth privileges.
3532 case PRIV_NETBLUETOOTH_RAW:
3536 * Netgraph and netgraph module privileges.
3538 case PRIV_NETGRAPH_CONTROL:
3540 case PRIV_NETGRAPH_TTY:
3544 * IPv4 and IPv6 privileges.
3546 case PRIV_NETINET_IPFW:
3547 case PRIV_NETINET_DIVERT:
3548 case PRIV_NETINET_PF:
3549 case PRIV_NETINET_DUMMYNET:
3550 case PRIV_NETINET_CARP:
3551 case PRIV_NETINET_MROUTE:
3552 case PRIV_NETINET_RAW:
3553 case PRIV_NETINET_ADDRCTRL6:
3554 case PRIV_NETINET_ND6:
3555 case PRIV_NETINET_SCOPE6:
3556 case PRIV_NETINET_ALIFETIME6:
3557 case PRIV_NETINET_IPSEC:
3558 case PRIV_NETINET_BINDANY:
3573 * No default: or deny here.
3574 * In case of no permit fall through to next switch().
3576 if (cred->cr_prison->pr_flags & PR_VNET)
3583 * Allow ktrace privileges for root in jail.
3589 * Allow jailed processes to configure audit identity and
3590 * submit audit records (login, etc). In the future we may
3591 * want to further refine the relationship between audit and
3594 case PRIV_AUDIT_GETAUDIT:
3595 case PRIV_AUDIT_SETAUDIT:
3596 case PRIV_AUDIT_SUBMIT:
3600 * Allow jailed processes to manipulate process UNIX
3601 * credentials in any way they see fit.
3603 case PRIV_CRED_SETUID:
3604 case PRIV_CRED_SETEUID:
3605 case PRIV_CRED_SETGID:
3606 case PRIV_CRED_SETEGID:
3607 case PRIV_CRED_SETGROUPS:
3608 case PRIV_CRED_SETREUID:
3609 case PRIV_CRED_SETREGID:
3610 case PRIV_CRED_SETRESUID:
3611 case PRIV_CRED_SETRESGID:
3614 * Jail implements visibility constraints already, so allow
3615 * jailed root to override uid/gid-based constraints.
3617 case PRIV_SEEOTHERGIDS:
3618 case PRIV_SEEOTHERUIDS:
3621 * Jail implements inter-process debugging limits already, so
3622 * allow jailed root various debugging privileges.
3624 case PRIV_DEBUG_DIFFCRED:
3625 case PRIV_DEBUG_SUGID:
3626 case PRIV_DEBUG_UNPRIV:
3629 * Allow jail to set various resource limits and login
3630 * properties, and for now, exceed process resource limits.
3632 case PRIV_PROC_LIMIT:
3633 case PRIV_PROC_SETLOGIN:
3634 case PRIV_PROC_SETRLIMIT:
3637 * System V and POSIX IPC privileges are granted in jail.
3640 case PRIV_IPC_WRITE:
3641 case PRIV_IPC_ADMIN:
3642 case PRIV_IPC_MSGSIZE:
3646 * Jail operations within a jail work on child jails.
3648 case PRIV_JAIL_ATTACH:
3650 case PRIV_JAIL_REMOVE:
3653 * Jail implements its own inter-process limits, so allow
3654 * root processes in jail to change scheduling on other
3655 * processes in the same jail. Likewise for signalling.
3657 case PRIV_SCHED_DIFFCRED:
3658 case PRIV_SCHED_CPUSET:
3659 case PRIV_SIGNAL_DIFFCRED:
3660 case PRIV_SIGNAL_SUGID:
3663 * Allow jailed processes to write to sysctls marked as jail
3666 case PRIV_SYSCTL_WRITEJAIL:
3669 * Allow root in jail to manage a variety of quota
3670 * properties. These should likely be conditional on a
3671 * configuration option.
3673 case PRIV_VFS_GETQUOTA:
3674 case PRIV_VFS_SETQUOTA:
3677 * Since Jail relies on chroot() to implement file system
3678 * protections, grant many VFS privileges to root in jail.
3679 * Be careful to exclude mount-related and NFS-related
3683 case PRIV_VFS_WRITE:
3684 case PRIV_VFS_ADMIN:
3686 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3687 case PRIV_VFS_CHFLAGS_DEV:
3688 case PRIV_VFS_CHOWN:
3689 case PRIV_VFS_CHROOT:
3690 case PRIV_VFS_RETAINSUGID:
3691 case PRIV_VFS_FCHROOT:
3693 case PRIV_VFS_SETGID:
3695 case PRIV_VFS_STICKYFILE:
3698 * As in the non-jail case, non-root users are expected to be
3699 * able to read kernel/physical memory (provided /dev/[k]mem
3700 * exists in the jail and they have permission to access it).
3702 case PRIV_KMEM_READ:
3706 * Depending on the global setting, allow privilege of
3707 * setting system flags.
3709 case PRIV_VFS_SYSFLAGS:
3710 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3716 * Depending on the global setting, allow privilege of
3717 * mounting/unmounting file systems.
3719 case PRIV_VFS_MOUNT:
3720 case PRIV_VFS_UNMOUNT:
3721 case PRIV_VFS_MOUNT_NONUSER:
3722 case PRIV_VFS_MOUNT_OWNER:
3723 pr = cred->cr_prison;
3725 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3733 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3734 * policy. priv_check_cred will not specifically allow it, and
3735 * we may want a MAC policy to allow it.
3737 case PRIV_VFS_READ_DIR:
3741 * Conditionnaly allow locking (unlocking) physical pages
3745 case PRIV_VM_MUNLOCK:
3746 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3752 * Conditionally allow jailed root to bind reserved ports.
3754 case PRIV_NETINET_RESERVEDPORT:
3755 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3761 * Allow jailed root to reuse in-use ports.
3763 case PRIV_NETINET_REUSEPORT:
3767 * Allow jailed root to set certain IPv4/6 (option) headers.
3769 case PRIV_NETINET_SETHDROPTS:
3773 * Conditionally allow creating raw sockets in jail.
3775 case PRIV_NETINET_RAW:
3776 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3782 * Since jail implements its own visibility limits on netstat
3783 * sysctls, allow getcred. This allows identd to work in
3786 case PRIV_NETINET_GETCRED:
3790 * Allow jailed root to set loginclass.
3792 case PRIV_PROC_SETLOGINCLASS:
3796 * Do not allow a process inside a jail to read the kernel
3797 * message buffer unless explicitly permitted.
3800 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3806 * In all remaining cases, deny the privilege request. This
3807 * includes almost all network privileges, many system
3808 * configuration privileges.
3815 * Return the part of pr2's name that is relative to pr1, or the whole name
3816 * if it does not directly follow.
3820 prison_name(struct prison *pr1, struct prison *pr2)
3824 /* Jails see themselves as "0" (if they see themselves at all). */
3827 name = pr2->pr_name;
3828 if (prison_ischild(pr1, pr2)) {
3830 * pr1 isn't locked (and allprison_lock may not be either)
3831 * so its length can't be counted on. But the number of dots
3832 * can be counted on - and counted.
3834 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3835 name = strchr(name, '.') + 1;
3841 * Return the part of pr2's path that is relative to pr1, or the whole path
3842 * if it does not directly follow.
3845 prison_path(struct prison *pr1, struct prison *pr2)
3847 char *path1, *path2;
3850 path1 = pr1->pr_path;
3851 path2 = pr2->pr_path;
3852 if (!strcmp(path1, "/"))
3854 len1 = strlen(path1);
3855 if (strncmp(path1, path2, len1))
3857 if (path2[len1] == '\0')
3859 if (path2[len1] == '/')
3860 return (path2 + len1);
3865 * Jail-related sysctls.
3867 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3871 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3874 struct prison *pr, *cpr;
3876 struct in_addr *ip4 = NULL;
3880 struct in6_addr *ip6 = NULL;
3885 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3886 pr = req->td->td_ucred->cr_prison;
3888 sx_slock(&allprison_lock);
3889 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3890 #if defined(INET) || defined(INET6)
3893 mtx_lock(&cpr->pr_mtx);
3895 if (cpr->pr_ip4s > 0) {
3896 if (ip4s < cpr->pr_ip4s) {
3897 ip4s = cpr->pr_ip4s;
3898 mtx_unlock(&cpr->pr_mtx);
3899 ip4 = realloc(ip4, ip4s *
3900 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3903 bcopy(cpr->pr_ip4, ip4,
3904 cpr->pr_ip4s * sizeof(struct in_addr));
3908 if (cpr->pr_ip6s > 0) {
3909 if (ip6s < cpr->pr_ip6s) {
3910 ip6s = cpr->pr_ip6s;
3911 mtx_unlock(&cpr->pr_mtx);
3912 ip6 = realloc(ip6, ip6s *
3913 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3916 bcopy(cpr->pr_ip6, ip6,
3917 cpr->pr_ip6s * sizeof(struct in6_addr));
3920 bzero(xp, sizeof(*xp));
3921 xp->pr_version = XPRISON_VERSION;
3922 xp->pr_id = cpr->pr_id;
3923 xp->pr_state = cpr->pr_state;
3924 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3925 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3926 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3928 xp->pr_ip4s = cpr->pr_ip4s;
3931 xp->pr_ip6s = cpr->pr_ip6s;
3933 mtx_unlock(&cpr->pr_mtx);
3934 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3938 if (xp->pr_ip4s > 0) {
3939 error = SYSCTL_OUT(req, ip4,
3940 xp->pr_ip4s * sizeof(struct in_addr));
3946 if (xp->pr_ip6s > 0) {
3947 error = SYSCTL_OUT(req, ip6,
3948 xp->pr_ip6s * sizeof(struct in6_addr));
3954 sx_sunlock(&allprison_lock);
3965 SYSCTL_OID(_security_jail, OID_AUTO, list,
3966 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3967 sysctl_jail_list, "S", "List of active jails");
3970 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3974 injail = jailed(req->td->td_ucred);
3975 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3980 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3981 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3982 sysctl_jail_jailed, "I", "Process in jail?");
3985 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3987 int error, havevnet;
3989 struct ucred *cred = req->td->td_ucred;
3991 havevnet = jailed(cred) && prison_owns_vnet(cred);
3995 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4000 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4001 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4002 sysctl_jail_vnet, "I", "Jail owns vnet?");
4004 #if defined(INET) || defined(INET6)
4005 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4006 &jail_max_af_ips, 0,
4007 "Number of IP addresses a jail may have at most per address family (deprecated)");
4011 * Default parameters for jail(2) compatibility. For historical reasons,
4012 * the sysctl names have varying similarity to the parameter names. Prisons
4013 * just see their own parameters, and can't change them.
4016 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4020 /* Get the current flag value, and convert it to a boolean. */
4021 if (req->td->td_ucred->cr_prison == &prison0) {
4022 mtx_lock(&prison0.pr_mtx);
4023 i = (jail_default_allow & arg2) != 0;
4024 mtx_unlock(&prison0.pr_mtx);
4026 i = prison_allow(req->td->td_ucred, arg2);
4030 error = sysctl_handle_int(oidp, &i, 0, req);
4031 if (error || !req->newptr)
4037 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4040 mtx_lock(&prison0.pr_mtx);
4041 jail_default_allow = (jail_default_allow & ~arg2) | i;
4042 mtx_unlock(&prison0.pr_mtx);
4046 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4047 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4048 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4049 "Processes in jail can set their hostnames (deprecated)");
4050 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4051 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4052 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4053 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4054 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4055 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4056 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4057 "Processes in jail can use System V IPC primitives (deprecated)");
4058 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4059 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4060 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4061 "Prison root can create raw sockets (deprecated)");
4062 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4063 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4064 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4065 "Processes in jail can alter system file flags (deprecated)");
4066 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4067 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4068 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4069 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4072 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4077 pr = req->td->td_ucred->cr_prison;
4078 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4079 error = sysctl_handle_int(oidp, &level, 0, req);
4080 if (error || !req->newptr)
4082 *(int *)arg1 = level;
4086 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4087 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4088 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4089 sysctl_jail_default_level, "I",
4090 "Processes in jail cannot see all mounted file systems (deprecated)");
4092 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4093 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4094 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4095 sysctl_jail_default_level, "I",
4096 "Ruleset for the devfs filesystem in jail (deprecated)");
4099 * Nodes to describe jail parameters. Maximum length of string parameters
4100 * is returned in the string itself, and the other parameters exist merely
4101 * to make themselves and their types known.
4103 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4107 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4114 switch (oidp->oid_kind & CTLTYPE)
4120 if (!(req->flags & SCTL_MASK32))
4122 return (SYSCTL_OUT(req, &l, sizeof(l)));
4126 return (SYSCTL_OUT(req, &i, sizeof(i)));
4127 case CTLTYPE_STRING:
4128 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4130 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4131 case CTLTYPE_STRUCT:
4133 return (SYSCTL_OUT(req, &s, sizeof(s)));
4139 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4140 * jail creation time but cannot be changed in an existing jail.
4142 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4143 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4144 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4145 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4146 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4147 "I", "Jail secure level");
4148 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4149 "Jail value for kern.osreldate and uname -K");
4150 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4151 "Jail value for kern.osrelease and uname -r");
4152 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4153 "I", "Jail cannot see all mounted file systems");
4154 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4155 "I", "Ruleset for in-jail devfs mounts");
4156 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4157 "B", "Jail persistence");
4159 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4160 "E,jailsys", "Virtual network stack");
4162 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4163 "B", "Jail is in the process of shutting down");
4165 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4166 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4167 "I", "Current number of child jails");
4168 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4169 "I", "Maximum number of child jails");
4171 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4172 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4174 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4175 "Jail NIS domainname");
4176 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4178 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4179 "LU", "Jail host ID");
4181 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4182 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4185 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4186 "Jail IPv4 address virtualization");
4187 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4188 "S,in_addr,a", "Jail IPv4 addresses");
4189 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4190 "B", "Do (not) use IPv4 source address selection rather than the "
4191 "primary jail IPv4 address.");
4194 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4195 "Jail IPv6 address virtualization");
4196 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4197 "S,in6_addr,a", "Jail IPv6 addresses");
4198 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4199 "B", "Do (not) use IPv6 source address selection rather than the "
4200 "primary jail IPv6 address.");
4203 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4204 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4205 "B", "Jail may set hostname");
4206 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4207 "B", "Jail may use SYSV IPC");
4208 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4209 "B", "Jail may create raw sockets");
4210 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4211 "B", "Jail may alter system file flags");
4212 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4213 "B", "Jail may set file quotas");
4214 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4215 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4216 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4217 "B", "Jail may lock (unlock) physical pages in memory");
4218 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4219 "B", "Jail may bind sockets to reserved ports");
4220 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4221 "B", "Jail may read the kernel message buffer");
4222 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4223 "B", "Unprivileged processes may use process debugging facilities");
4224 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4225 "B", "Processes in jail with uid 0 have privilege");
4227 SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW,
4228 "B", "Mountd/nfsd may run in the jail");
4231 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4232 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4233 "B", "Jail may mount/unmount jail-friendly file systems in general");
4236 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4237 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4241 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4244 struct bool_flags *bf;
4245 struct sysctl_oid *parent;
4246 char *allow_name, *allow_noname, *allowed;
4247 #ifndef NO_SYSCTL_DESCR
4248 char *descr_deprecated;
4253 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4255 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4257 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4258 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4259 free(allow_name, M_PRISON);
4264 * See if this parameter has already beed added, i.e. a module was
4265 * previously loaded/unloaded.
4267 mtx_lock(&prison0.pr_mtx);
4268 for (bf = pr_flag_allow;
4269 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4270 atomic_load_int(&bf->flag) != 0;
4272 if (strcmp(bf->name, allow_name) == 0) {
4273 allow_flag = bf->flag;
4279 * Find a free bit in pr_allow_all, failing if there are none
4280 * (which shouldn't happen as long as we keep track of how many
4281 * potential dynamic flags exist).
4283 for (allow_flag = 1;; allow_flag <<= 1) {
4284 if (allow_flag == 0)
4286 if ((pr_allow_all & allow_flag) == 0)
4290 /* Note the parameter in the next open slot in pr_flag_allow. */
4291 for (bf = pr_flag_allow; ; bf++) {
4292 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4293 /* This should never happen, but is not fatal. */
4297 if (atomic_load_int(&bf->flag) == 0)
4300 bf->name = allow_name;
4301 bf->noname = allow_noname;
4302 pr_allow_all |= allow_flag;
4304 * prison0 always has permission for the new parameter.
4305 * Other jails must have it granted to them.
4307 prison0.pr_allow |= allow_flag;
4308 /* The flag indicates a valid entry, so make sure it is set last. */
4309 atomic_store_rel_int(&bf->flag, allow_flag);
4310 mtx_unlock(&prison0.pr_mtx);
4313 * Create sysctls for the parameter, and the back-compat global
4317 ? SYSCTL_ADD_NODE(NULL,
4318 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4319 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4320 : &sysctl___security_jail_param_allow;
4321 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4322 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4323 NULL, 0, sysctl_jail_param, "B", descr);
4325 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4326 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4327 #ifndef NO_SYSCTL_DESCR
4328 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4331 (void)SYSCTL_ADD_PROC(NULL,
4332 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4333 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4334 sysctl_jail_default_allow, "I", descr_deprecated);
4335 #ifndef NO_SYSCTL_DESCR
4336 free(descr_deprecated, M_TEMP);
4338 free(allowed, M_TEMP);
4343 mtx_unlock(&prison0.pr_mtx);
4344 free(allow_name, M_PRISON);
4345 free(allow_noname, M_PRISON);
4350 * The VFS system will register jail-aware filesystems here. They each get
4351 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4352 * attempts to mount.
4355 prison_add_vfs(struct vfsconf *vfsp)
4357 #ifdef NO_SYSCTL_DESCR
4359 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4364 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4366 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4368 free(descr, M_TEMP);
4374 prison_racct_foreach(void (*callback)(struct racct *racct,
4375 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4376 void *arg2, void *arg3)
4378 struct prison_racct *prr;
4380 ASSERT_RACCT_ENABLED();
4382 sx_slock(&allprison_lock);
4385 LIST_FOREACH(prr, &allprison_racct, prr_next)
4386 (callback)(prr->prr_racct, arg2, arg3);
4389 sx_sunlock(&allprison_lock);
4392 static struct prison_racct *
4393 prison_racct_find_locked(const char *name)
4395 struct prison_racct *prr;
4397 ASSERT_RACCT_ENABLED();
4398 sx_assert(&allprison_lock, SA_XLOCKED);
4400 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4403 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4404 if (strcmp(name, prr->prr_name) != 0)
4407 /* Found prison_racct with a matching name? */
4408 prison_racct_hold(prr);
4412 /* Add new prison_racct. */
4413 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4414 racct_create(&prr->prr_racct);
4416 strcpy(prr->prr_name, name);
4417 refcount_init(&prr->prr_refcount, 1);
4418 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4423 struct prison_racct *
4424 prison_racct_find(const char *name)
4426 struct prison_racct *prr;
4428 ASSERT_RACCT_ENABLED();
4430 sx_xlock(&allprison_lock);
4431 prr = prison_racct_find_locked(name);
4432 sx_xunlock(&allprison_lock);
4437 prison_racct_hold(struct prison_racct *prr)
4440 ASSERT_RACCT_ENABLED();
4442 refcount_acquire(&prr->prr_refcount);
4446 prison_racct_free_locked(struct prison_racct *prr)
4449 ASSERT_RACCT_ENABLED();
4450 sx_assert(&allprison_lock, SA_XLOCKED);
4452 if (refcount_release(&prr->prr_refcount)) {
4453 racct_destroy(&prr->prr_racct);
4454 LIST_REMOVE(prr, prr_next);
4455 free(prr, M_PRISON_RACCT);
4460 prison_racct_free(struct prison_racct *prr)
4463 ASSERT_RACCT_ENABLED();
4464 sx_assert(&allprison_lock, SA_UNLOCKED);
4466 if (refcount_release_if_not_last(&prr->prr_refcount))
4469 sx_xlock(&allprison_lock);
4470 prison_racct_free_locked(prr);
4471 sx_xunlock(&allprison_lock);
4475 prison_racct_attach(struct prison *pr)
4477 struct prison_racct *prr;
4479 ASSERT_RACCT_ENABLED();
4480 sx_assert(&allprison_lock, SA_XLOCKED);
4482 prr = prison_racct_find_locked(pr->pr_name);
4483 KASSERT(prr != NULL, ("cannot find prison_racct"));
4485 pr->pr_prison_racct = prr;
4489 * Handle jail renaming. From the racct point of view, renaming means
4490 * moving from one prison_racct to another.
4493 prison_racct_modify(struct prison *pr)
4499 struct prison_racct *oldprr;
4501 ASSERT_RACCT_ENABLED();
4503 sx_slock(&allproc_lock);
4504 sx_xlock(&allprison_lock);
4506 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4507 sx_xunlock(&allprison_lock);
4508 sx_sunlock(&allproc_lock);
4512 oldprr = pr->pr_prison_racct;
4513 pr->pr_prison_racct = NULL;
4515 prison_racct_attach(pr);
4518 * Move resource utilisation records.
4520 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4524 * Force rctl to reattach rules to processes.
4526 FOREACH_PROC_IN_SYSTEM(p) {
4528 cred = crhold(p->p_ucred);
4530 rctl_proc_ucred_changed(p, cred);
4535 sx_sunlock(&allproc_lock);
4536 prison_racct_free_locked(oldprr);
4537 sx_xunlock(&allprison_lock);
4541 prison_racct_detach(struct prison *pr)
4544 ASSERT_RACCT_ENABLED();
4545 sx_assert(&allprison_lock, SA_UNLOCKED);
4547 if (pr->pr_prison_racct == NULL)
4549 prison_racct_free(pr->pr_prison_racct);
4550 pr->pr_prison_racct = NULL;
4557 db_show_prison(struct prison *pr)
4559 struct bool_flags *bf;
4560 struct jailsys_flags *jsf;
4561 #if defined(INET) || defined(INET6)
4566 char ip4buf[INET_ADDRSTRLEN];
4569 char ip6buf[INET6_ADDRSTRLEN];
4572 db_printf("prison %p:\n", pr);
4573 db_printf(" jid = %d\n", pr->pr_id);
4574 db_printf(" name = %s\n", pr->pr_name);
4575 db_printf(" parent = %p\n", pr->pr_parent);
4576 db_printf(" ref = %d\n", pr->pr_ref);
4577 db_printf(" uref = %d\n", pr->pr_uref);
4578 db_printf(" state = %s\n",
4579 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4580 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4582 db_printf(" path = %s\n", pr->pr_path);
4583 db_printf(" cpuset = %d\n", pr->pr_cpuset
4584 ? pr->pr_cpuset->cs_id : -1);
4586 db_printf(" vnet = %p\n", pr->pr_vnet);
4588 db_printf(" root = %p\n", pr->pr_root);
4589 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4590 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4591 db_printf(" children.max = %d\n", pr->pr_childmax);
4592 db_printf(" children.cur = %d\n", pr->pr_childcount);
4593 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4594 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4595 db_printf(" flags = 0x%x", pr->pr_flags);
4596 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4597 if (pr->pr_flags & bf->flag)
4598 db_printf(" %s", bf->name);
4599 for (jsf = pr_flag_jailsys;
4600 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4602 f = pr->pr_flags & (jsf->disable | jsf->new);
4603 db_printf(" %-16s= %s\n", jsf->name,
4604 (f != 0 && f == jsf->disable) ? "disable"
4605 : (f == jsf->new) ? "new"
4608 db_printf(" allow = 0x%x", pr->pr_allow);
4609 for (bf = pr_flag_allow;
4610 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4611 atomic_load_int(&bf->flag) != 0;
4613 if (pr->pr_allow & bf->flag)
4614 db_printf(" %s", bf->name);
4616 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4617 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4618 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4619 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4620 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4622 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4623 for (ii = 0; ii < pr->pr_ip4s; ii++)
4624 db_printf(" %s %s\n",
4625 ii == 0 ? "ip4.addr =" : " ",
4626 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4629 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4630 for (ii = 0; ii < pr->pr_ip6s; ii++)
4631 db_printf(" %s %s\n",
4632 ii == 0 ? "ip6.addr =" : " ",
4633 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4637 DB_SHOW_COMMAND(prison, db_show_prison_command)
4643 * Show all prisons in the list, and prison0 which is not
4646 db_show_prison(&prison0);
4647 if (!db_pager_quit) {
4648 TAILQ_FOREACH(pr, &allprison, pr_list) {
4660 /* Look for a prison with the ID and with references. */
4661 TAILQ_FOREACH(pr, &allprison, pr_list)
4662 if (pr->pr_id == addr && pr->pr_ref > 0)
4665 /* Look again, without requiring a reference. */
4666 TAILQ_FOREACH(pr, &allprison, pr_list)
4667 if (pr->pr_id == addr)
4670 /* Assume address points to a valid prison. */
4671 pr = (struct prison *)addr;