2 * SPDX-License-Identifier: BSD-2-Clause
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>
34 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
47 #include <sys/taskqueue.h>
48 #include <sys/fcntl.h>
50 #include <sys/linker.h>
53 #include <sys/mutex.h>
54 #include <sys/racct.h>
56 #include <sys/refcount.h>
58 #include <sys/sysent.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
71 #include <netinet/in.h>
77 #include <security/mac/mac_framework.h>
79 #define PRISON0_HOSTUUID_MODULE "hostuuid"
81 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
82 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
84 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
87 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
89 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
93 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
95 #define _PR_IP_SADDRSEL 0
99 /* prison0 describes what is "real" about the system. */
100 struct prison prison0 = {
106 .pr_securelevel = -1,
108 .pr_state = PRISON_STATE_ALIVE,
109 .pr_childmax = JAIL_MAX,
110 .pr_hostuuid = DEFAULT_HOSTUUID,
111 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
113 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
115 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
117 .pr_allow = PR_ALLOW_ALL_STATIC,
119 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
126 struct jailsys_flags {
132 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
133 struct sx allprison_lock;
134 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
135 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
136 LIST_HEAD(, prison_racct) allprison_racct;
139 static int get_next_prid(struct prison **insprp);
140 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
141 static void prison_complete(void *context, int pending);
142 static void prison_deref(struct prison *pr, int flags);
143 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
144 static int prison_lock_xlock(struct prison *pr, int flags);
145 static void prison_cleanup(struct prison *pr);
146 static void prison_free_not_last(struct prison *pr);
147 static void prison_proc_free_not_last(struct prison *pr);
148 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
150 static char *prison_path(struct prison *pr1, struct prison *pr2);
152 static void prison_racct_attach(struct prison *pr);
153 static void prison_racct_modify(struct prison *pr);
154 static void prison_racct_detach(struct prison *pr);
157 /* Flags for prison_deref */
158 #define PD_DEREF 0x01 /* Decrement pr_ref */
159 #define PD_DEUREF 0x02 /* Decrement pr_uref */
160 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
161 #define PD_LOCKED 0x10 /* pr_mtx is held */
162 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
163 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
164 #define PD_OP_FLAGS 0x07 /* Operation flags */
165 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
168 * Parameter names corresponding to PR_* flag values. Size values are for kvm
169 * as we cannot figure out the size of a sparse array, or an array without a
172 static struct bool_flags pr_flag_bool[] = {
173 {"persist", "nopersist", PR_PERSIST},
175 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
178 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
181 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
183 static struct jailsys_flags pr_flag_jailsys[] = {
184 {"host", 0, PR_HOST},
186 {"vnet", 0, PR_VNET},
189 {"ip4", PR_IP4_USER, PR_IP4_USER},
192 {"ip6", PR_IP6_USER, PR_IP6_USER},
195 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
198 * Make this array full-size so dynamic parameters can be added.
199 * It is protected by prison0.mtx, but lockless reading is allowed
200 * with an atomic check of the flag values.
202 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
203 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
204 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
205 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
206 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
207 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
208 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
209 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
210 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
211 {"allow.reserved_ports", "allow.noreserved_ports",
212 PR_ALLOW_RESERVED_PORTS},
213 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
214 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
215 PR_ALLOW_UNPRIV_DEBUG},
216 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
218 {"allow.nfsd", "allow.nonfsd", PR_ALLOW_NFSD},
221 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
222 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
224 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
225 PR_ALLOW_RESERVED_PORTS | \
226 PR_ALLOW_UNPRIV_DEBUG | \
228 #define JAIL_DEFAULT_ENFORCE_STATFS 2
229 #define JAIL_DEFAULT_DEVFS_RSNUM 0
230 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
231 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
232 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
233 #if defined(INET) || defined(INET6)
234 static unsigned jail_max_af_ips = 255;
238 * Initialize the parts of prison0 that can't be static-initialized with
239 * constants. This is called from proc0_init() after creating thread0 cpuset.
244 uint8_t *file, *data;
246 char buf[sizeof(prison0.pr_hostuuid)];
249 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
250 prison0.pr_osreldate = osreldate;
251 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
253 /* If we have a preloaded hostuuid, use it. */
254 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
256 data = preload_fetch_addr(file);
257 size = preload_fetch_size(file);
260 * The preloaded data may include trailing whitespace, almost
261 * certainly a newline; skip over any whitespace or
262 * non-printable characters to be safe.
264 while (size > 0 && data[size - 1] <= 0x20) {
271 * Not NUL-terminated when passed from loader, but
272 * validate_uuid requires that due to using sscanf (as
273 * does the subsequent strlcpy, since it still reads
274 * past the given size to return the true length);
275 * bounce to a temporary buffer to fix.
277 if (size >= sizeof(buf))
280 memcpy(buf, data, size);
283 if (validate_uuid(buf, size, NULL, 0) != 0)
287 (void)strlcpy(prison0.pr_hostuuid, buf,
288 sizeof(prison0.pr_hostuuid));
291 if (bootverbose && !valid) {
292 printf("hostuuid: preload data malformed: '%.*s'\n",
298 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
307 sys_jail(struct thread *td, struct jail_args *uap)
313 error = copyin(uap->jail, &version, sizeof(uint32_t));
322 /* FreeBSD single IPv4 jails. */
323 bzero(&j, sizeof(struct jail));
324 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
327 j.version = j0.version;
329 j.hostname = j0.hostname;
330 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
336 * Version 1 was used by multi-IPv4 jail implementations
337 * that never made it into the official kernel.
341 case 2: /* JAIL_API_VERSION */
342 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
343 error = copyin(uap->jail, &j, sizeof(struct jail));
349 /* Sci-Fi jails are not supported, sorry. */
352 return (kern_jail(td, &j));
356 kern_jail(struct thread *td, struct jail *j)
358 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
367 char *u_path, *u_hostname, *u_name;
368 struct bool_flags *bf;
371 struct in_addr *u_ip4;
374 struct in6_addr *u_ip6;
377 int error, enforce_statfs;
379 bzero(&optiov, sizeof(optiov));
380 opt.uio_iov = optiov;
384 opt.uio_segflg = UIO_SYSSPACE;
385 opt.uio_rw = UIO_READ;
388 /* Set permissions for top-level jails from sysctls. */
389 if (!jailed(td->td_ucred)) {
390 for (bf = pr_flag_allow;
391 bf < pr_flag_allow + nitems(pr_flag_allow) &&
392 atomic_load_int(&bf->flag) != 0;
394 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
395 (jail_default_allow & bf->flag)
396 ? bf->name : bf->noname);
397 optiov[opt.uio_iovcnt].iov_len =
398 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
401 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
402 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
404 enforce_statfs = jail_default_enforce_statfs;
405 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
406 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
410 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
412 ip4s = (j->version == 0) ? 1 : j->ip4s;
413 if (ip4s > jail_max_af_ips)
415 tmplen += ip4s * sizeof(struct in_addr);
421 if (j->ip6s > jail_max_af_ips)
423 tmplen += j->ip6s * sizeof(struct in6_addr);
428 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
429 u_hostname = u_path + MAXPATHLEN;
430 u_name = u_hostname + MAXHOSTNAMELEN;
432 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
436 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
438 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
441 optiov[opt.uio_iovcnt].iov_base = "path";
442 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
444 optiov[opt.uio_iovcnt].iov_base = u_path;
445 error = copyinstr(j->path, u_path, MAXPATHLEN,
446 &optiov[opt.uio_iovcnt].iov_len);
448 free(u_path, M_TEMP);
452 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
453 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
455 optiov[opt.uio_iovcnt].iov_base = u_hostname;
456 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
457 &optiov[opt.uio_iovcnt].iov_len);
459 free(u_path, M_TEMP);
463 if (j->jailname != NULL) {
464 optiov[opt.uio_iovcnt].iov_base = "name";
465 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
467 optiov[opt.uio_iovcnt].iov_base = u_name;
468 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
469 &optiov[opt.uio_iovcnt].iov_len);
471 free(u_path, M_TEMP);
477 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
478 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
480 optiov[opt.uio_iovcnt].iov_base = u_ip4;
481 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
483 u_ip4->s_addr = j->ip4s;
485 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
487 free(u_path, M_TEMP);
494 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
495 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
497 optiov[opt.uio_iovcnt].iov_base = u_ip6;
498 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
499 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
501 free(u_path, M_TEMP);
506 KASSERT(opt.uio_iovcnt <= nitems(optiov),
507 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
508 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
509 free(u_path, M_TEMP);
514 * struct jail_set_args {
515 * struct iovec *iovp;
516 * unsigned int iovcnt;
521 sys_jail_set(struct thread *td, struct jail_set_args *uap)
526 /* Check that we have an even number of iovecs. */
530 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
533 error = kern_jail_set(td, auio, uap->flags);
539 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
546 struct in6_addr *ip6;
549 struct vfsoptlist *opts;
550 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
552 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
553 char *g_path, *osrelstr;
554 struct bool_flags *bf;
555 struct jailsys_flags *jsf;
556 #if defined(INET) || defined(INET6)
561 size_t namelen, onamelen, pnamelen;
562 int born, created, cuflags, descend, drflags, enforce;
563 int error, errmsg_len, errmsg_pos;
564 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
565 int jid, jsys, len, level;
566 int childmax, osreldt, rsnum, slevel;
567 #if defined(INET) || defined(INET6)
576 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
577 uint64_t pr_allow_diff;
581 error = priv_check(td, PRIV_JAIL_SET);
582 if (!error && (flags & JAIL_ATTACH))
583 error = priv_check(td, PRIV_JAIL_ATTACH);
586 mypr = td->td_ucred->cr_prison;
587 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
589 if (flags & ~JAIL_SET_MASK)
593 * Check all the parameters before committing to anything. Not all
594 * errors can be caught early, but we may as well try. Also, this
595 * takes care of some expensive stuff (path lookup) before getting
596 * the allprison lock.
598 * XXX Jails are not filesystems, and jail parameters are not mount
599 * options. But it makes more sense to re-use the vfsopt code
600 * than duplicate it under a different name.
602 error = vfs_buildopts(optuio, &opts);
613 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
616 vfs_opterror(opts, "no valid operation (create or update)");
620 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
626 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
635 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
643 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
648 else if (enforce < 0 || enforce > 2) {
654 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
662 pr_flags = ch_flags = 0;
663 for (bf = pr_flag_bool;
664 bf < pr_flag_bool + nitems(pr_flag_bool);
666 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
667 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
669 ch_flags |= pr_flags;
670 for (jsf = pr_flag_jailsys;
671 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
673 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
679 case JAIL_SYS_DISABLE:
684 pr_flags |= jsf->disable;
687 pr_flags |= jsf->new;
689 case JAIL_SYS_INHERIT:
695 ch_flags |= jsf->new | jsf->disable;
697 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
698 && !(pr_flags & PR_PERSIST)) {
700 vfs_opterror(opts, "new jail must persist or attach");
704 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
706 vfs_opterror(opts, "vnet cannot be changed after creation");
711 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
713 vfs_opterror(opts, "ip4 cannot be changed after creation");
718 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
720 vfs_opterror(opts, "ip6 cannot be changed after creation");
725 pr_allow = ch_allow = 0;
726 for (bf = pr_flag_allow;
727 bf < pr_flag_allow + nitems(pr_flag_allow) &&
728 atomic_load_int(&bf->flag) != 0;
730 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
731 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
733 ch_allow |= pr_allow;
735 error = vfs_getopt(opts, "name", (void **)&name, &len);
741 if (len == 0 || name[len - 1] != '\0') {
745 if (len > MAXHOSTNAMELEN) {
746 error = ENAMETOOLONG;
751 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
759 if (len == 0 || host[len - 1] != '\0') {
763 if (len > MAXHOSTNAMELEN) {
764 error = ENAMETOOLONG;
769 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
777 if (len == 0 || domain[len - 1] != '\0') {
781 if (len > MAXHOSTNAMELEN) {
782 error = ENAMETOOLONG;
787 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
795 if (len == 0 || uuid[len - 1] != '\0') {
799 if (len > HOSTUUIDLEN) {
800 error = ENAMETOOLONG;
805 #ifdef COMPAT_FREEBSD32
806 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
809 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
813 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
825 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
830 else if (ip4s & (sizeof(*ip4) - 1)) {
834 ch_flags |= PR_IP4_USER;
835 pr_flags |= PR_IP4_USER;
837 ip4s /= sizeof(*ip4);
838 if (ip4s > jail_max_af_ips) {
840 vfs_opterror(opts, "too many IPv4 addresses");
843 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
844 bcopy(op, ip4, ip4s * sizeof(*ip4));
846 * IP addresses are all sorted but ip[0] to preserve
847 * the primary IP address as given from userland.
848 * This special IP is used for unbound outgoing
849 * connections as well for "loopback" traffic in case
850 * source address selection cannot find any more fitting
851 * address to connect from.
854 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
857 * Check for duplicate addresses and do some simple
858 * zero and broadcast checks. If users give other bogus
859 * addresses it is their problem.
861 * We do not have to care about byte order for these
862 * checks so we will do them in NBO.
864 for (ii = 0; ii < ip4s; ii++) {
865 if (ip4[ii].s_addr == INADDR_ANY ||
866 ip4[ii].s_addr == INADDR_BROADCAST) {
871 (ip4[0].s_addr == ip4[ii+1].s_addr ||
872 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
882 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
887 else if (ip6s & (sizeof(*ip6) - 1)) {
891 ch_flags |= PR_IP6_USER;
892 pr_flags |= PR_IP6_USER;
894 ip6s /= sizeof(*ip6);
895 if (ip6s > jail_max_af_ips) {
897 vfs_opterror(opts, "too many IPv6 addresses");
900 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
901 bcopy(op, ip6, ip6s * sizeof(*ip6));
903 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
905 for (ii = 0; ii < ip6s; ii++) {
906 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
911 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
912 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
922 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
923 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
926 "vnet jails cannot have IP address restrictions");
931 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
937 if (flags & JAIL_UPDATE) {
940 "osrelease cannot be changed after creation");
943 if (len == 0 || osrelstr[len - 1] != '\0') {
947 if (len >= OSRELEASELEN) {
948 error = ENAMETOOLONG;
950 "osrelease string must be 1-%d bytes long",
956 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
962 if (flags & JAIL_UPDATE) {
965 "osreldate cannot be changed after creation");
970 vfs_opterror(opts, "osreldate cannot be 0");
976 error = vfs_getopt(opts, "path", (void **)&path, &len);
982 if (flags & JAIL_UPDATE) {
985 "path cannot be changed after creation");
988 if (len == 0 || path[len - 1] != '\0') {
992 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
998 NDFREE(&nd, NDF_ONLY_PNBUF);
999 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1000 strlcpy(g_path, path, MAXPATHLEN);
1001 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1005 /* exit on other errors */
1008 if (root->v_type != VDIR) {
1017 * Find the specified jail, or at least its parent.
1018 * This abuses the file error codes ENOENT and EEXIST.
1022 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1023 namelc = strrchr(name, '.');
1024 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1028 sx_xlock(&allprison_lock);
1029 drflags = PD_LIST_XLOCKED;
1031 if (!prison_isalive(ppr)) {
1032 /* This jail is dying. This process will surely follow. */
1039 vfs_opterror(opts, "negative jid");
1043 * See if a requested jid already exists. Keep track of
1044 * where it can be inserted later.
1046 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1047 if (inspr->pr_id < jid)
1049 if (inspr->pr_id > jid)
1052 mtx_lock(&pr->pr_mtx);
1053 drflags |= PD_LOCKED;
1058 /* Create: jid must not exist. */
1059 if (cuflags == JAIL_CREATE) {
1061 * Even creators that cannot see the jail will
1065 vfs_opterror(opts, "jail %d already exists",
1069 if (!prison_ischild(mypr, pr)) {
1071 * Updaters get ENOENT if they cannot see the
1072 * jail. This is true even for CREATE | UPDATE,
1073 * which normally cannot give this error.
1076 vfs_opterror(opts, "jail %d not found", jid);
1079 ppr = pr->pr_parent;
1080 if (!prison_isalive(ppr)) {
1082 vfs_opterror(opts, "jail %d is dying",
1086 if (!prison_isalive(pr)) {
1087 if (!(flags & JAIL_DYING)) {
1089 vfs_opterror(opts, "jail %d is dying",
1093 if ((flags & JAIL_ATTACH) ||
1094 (pr_flags & PR_PERSIST)) {
1096 * A dying jail might be resurrected
1097 * (via attach or persist), but first
1098 * it must determine if another jail
1099 * has claimed its name. Accomplish
1100 * this by implicitly re-setting the
1104 name = prison_name(mypr, pr);
1108 /* Update: jid must exist. */
1109 if (cuflags == JAIL_UPDATE) {
1111 vfs_opterror(opts, "jail %d not found", jid);
1117 * If the caller provided a name, look for a jail by that name.
1118 * This has different semantics for creates and updates keyed by jid
1119 * (where the name must not already exist in a different jail),
1120 * and updates keyed by the name itself (where the name must exist
1121 * because that is the jail being updated).
1125 namelc = strrchr(name, '.');
1130 * This is a hierarchical name. Split it into the
1131 * parent and child names, and make sure the parent
1132 * exists or matches an already found jail.
1135 if (strncmp(name, ppr->pr_name, namelc - name)
1136 || ppr->pr_name[namelc - name] != '\0') {
1139 "cannot change jail's parent");
1144 ppr = prison_find_name(mypr, name);
1148 "jail \"%s\" not found", name);
1151 mtx_unlock(&ppr->pr_mtx);
1152 if (!prison_isalive(ppr)) {
1155 "jail \"%s\" is dying", name);
1162 if (namelc[0] != '\0') {
1164 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1166 FOREACH_PRISON_CHILD(ppr, tpr) {
1168 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1169 if (prison_isalive(tpr)) {
1171 cuflags != JAIL_CREATE) {
1177 mtx_lock(&pr->pr_mtx);
1178 drflags |= PD_LOCKED;
1182 * Create, or update(jid):
1183 * name must not exist in an
1184 * active sibling jail.
1188 "jail \"%s\" already exists",
1193 cuflags != JAIL_CREATE) {
1198 /* If no active jail is found, use a dying one. */
1199 if (deadpr != NULL && pr == NULL) {
1200 if (flags & JAIL_DYING) {
1202 mtx_lock(&pr->pr_mtx);
1203 drflags |= PD_LOCKED;
1204 } else if (cuflags == JAIL_UPDATE) {
1207 "jail \"%s\" is dying", name);
1211 /* Update: name must exist if no jid. */
1212 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1214 vfs_opterror(opts, "jail \"%s\" not found",
1220 /* Update: must provide a jid or name. */
1221 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1223 vfs_opterror(opts, "update specified no jail");
1227 /* If there's no prison to update, create a new one and link it in. */
1228 created = pr == NULL;
1230 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1231 if (tpr->pr_childcount >= tpr->pr_childmax) {
1233 vfs_opterror(opts, "prison limit exceeded");
1236 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1238 vfs_opterror(opts, "no available jail IDs");
1242 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1243 pr->pr_state = PRISON_STATE_INVALID;
1244 refcount_init(&pr->pr_ref, 1);
1245 refcount_init(&pr->pr_uref, 0);
1246 drflags |= PD_DEREF;
1247 LIST_INIT(&pr->pr_children);
1248 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1249 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1253 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1255 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1257 pr->pr_parent = ppr;
1259 prison_proc_hold(ppr);
1260 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1261 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1262 tpr->pr_childcount++;
1264 /* Set some default values, and inherit some from the parent. */
1269 root = mypr->pr_root;
1272 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1273 pr->pr_flags |= PR_HOST;
1274 #if defined(INET) || defined(INET6)
1276 if (!(pr_flags & PR_VNET))
1280 if (!(ch_flags & PR_IP4_USER))
1281 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1282 else if (!(pr_flags & PR_IP4_USER)) {
1283 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1284 if (ppr->pr_ip4 != NULL) {
1285 pr->pr_ip4s = ppr->pr_ip4s;
1286 pr->pr_ip4 = malloc(pr->pr_ip4s *
1287 sizeof(struct in_addr), M_PRISON,
1289 bcopy(ppr->pr_ip4, pr->pr_ip4,
1290 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1295 if (!(ch_flags & PR_IP6_USER))
1296 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1297 else if (!(pr_flags & PR_IP6_USER)) {
1298 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1299 if (ppr->pr_ip6 != NULL) {
1300 pr->pr_ip6s = ppr->pr_ip6s;
1301 pr->pr_ip6 = malloc(pr->pr_ip6s *
1302 sizeof(struct in6_addr), M_PRISON,
1304 bcopy(ppr->pr_ip6, pr->pr_ip6,
1305 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1311 /* Source address selection is always on by default. */
1312 pr->pr_flags |= _PR_IP_SADDRSEL;
1314 pr->pr_securelevel = ppr->pr_securelevel;
1315 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1316 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1317 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1319 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1320 if (osrelstr == NULL)
1321 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1322 sizeof(pr->pr_osrelease));
1324 strlcpy(pr->pr_osrelease, osrelstr,
1325 sizeof(pr->pr_osrelease));
1328 /* Allocate a new vnet if specified. */
1329 pr->pr_vnet = (pr_flags & PR_VNET)
1330 ? vnet_alloc() : ppr->pr_vnet;
1333 * Allocate a dedicated cpuset for each jail.
1334 * Unlike other initial settings, this may return an error.
1336 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1340 mtx_lock(&pr->pr_mtx);
1341 drflags |= PD_LOCKED;
1344 * Grab a reference for existing prisons, to ensure they
1345 * continue to exist for the duration of the call.
1348 drflags |= PD_DEREF;
1349 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1350 if ((pr->pr_flags & PR_VNET) &&
1351 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1354 "vnet jails cannot have IP address restrictions");
1359 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1362 "ip4 cannot be changed after creation");
1367 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1370 "ip6 cannot be changed after creation");
1376 /* Do final error checking before setting anything. */
1378 if (slevel < ppr->pr_securelevel) {
1384 if (childmax >= ppr->pr_childmax) {
1390 if (enforce < ppr->pr_enforce_statfs) {
1397 * devfs_rsnum is a uint16_t
1399 if (rsnum < 0 || rsnum > 65535) {
1404 * Nested jails always inherit parent's devfs ruleset
1406 if (jailed(td->td_ucred)) {
1407 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1411 rsnum = ppr->pr_devfs_rsnum;
1416 if (ppr->pr_flags & PR_IP4) {
1418 * Make sure the new set of IP addresses is a
1419 * subset of the parent's list. Don't worry
1420 * about the parent being unlocked, as any
1421 * setting is done with allprison_lock held.
1423 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1424 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1426 if (ij == ppr->pr_ip4s) {
1431 for (ii = ij = 1; ii < ip4s; ii++) {
1432 if (ip4[ii].s_addr ==
1433 ppr->pr_ip4[0].s_addr)
1435 for (; ij < ppr->pr_ip4s; ij++)
1436 if (ip4[ii].s_addr ==
1437 ppr->pr_ip4[ij].s_addr)
1439 if (ij == ppr->pr_ip4s)
1442 if (ij == ppr->pr_ip4s) {
1449 * Check for conflicting IP addresses. We permit them
1450 * if there is no more than one IP on each jail. If
1451 * there is a duplicate on a jail with more than one
1452 * IP stop checking and return error.
1455 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1456 if (tppr->pr_flags & PR_VNET)
1461 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1464 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1466 !prison_isalive(tpr)) {
1470 if (!(tpr->pr_flags & PR_IP4_USER))
1473 if (tpr->pr_ip4 == NULL ||
1474 (ip4s == 1 && tpr->pr_ip4s == 1))
1476 for (ii = 0; ii < ip4s; ii++) {
1477 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1481 "IPv4 addresses clash");
1490 if (ppr->pr_flags & PR_IP6) {
1492 * Make sure the new set of IP addresses is a
1493 * subset of the parent's list.
1495 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1496 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1499 if (ij == ppr->pr_ip6s) {
1504 for (ii = ij = 1; ii < ip6s; ii++) {
1505 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1508 for (; ij < ppr->pr_ip6s; ij++)
1509 if (IN6_ARE_ADDR_EQUAL(
1510 &ip6[ii], &ppr->pr_ip6[ij]))
1512 if (ij == ppr->pr_ip6s)
1515 if (ij == ppr->pr_ip6s) {
1521 /* Check for conflicting IP addresses. */
1523 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1524 if (tppr->pr_flags & PR_VNET)
1529 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1532 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1534 !prison_isalive(tpr)) {
1538 if (!(tpr->pr_flags & PR_IP6_USER))
1541 if (tpr->pr_ip6 == NULL ||
1542 (ip6s == 1 && tpr->pr_ip6s == 1))
1544 for (ii = 0; ii < ip6s; ii++) {
1545 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1549 "IPv6 addresses clash");
1556 onamelen = namelen = 0;
1557 if (namelc != NULL) {
1558 /* Give a default name of the jid. Also allow the name to be
1559 * explicitly the jid - but not any other number, and only in
1560 * normal form (no leading zero/etc).
1562 if (namelc[0] == '\0')
1563 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1564 else if ((strtoul(namelc, &p, 10) != jid ||
1565 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1568 "name cannot be numeric (unless it is the jid)");
1572 * Make sure the name isn't too long for the prison or its
1575 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1576 onamelen = strlen(pr->pr_name + pnamelen);
1577 namelen = strlen(namelc);
1578 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1579 error = ENAMETOOLONG;
1582 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1583 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1584 sizeof(pr->pr_name)) {
1585 error = ENAMETOOLONG;
1590 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1591 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1597 * Let modules check their parameters. This requires unlocking and
1598 * then re-locking the prison, but this is still a valid state as long
1599 * as allprison_lock remains xlocked.
1601 mtx_unlock(&pr->pr_mtx);
1602 drflags &= ~PD_LOCKED;
1603 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1606 mtx_lock(&pr->pr_mtx);
1607 drflags |= PD_LOCKED;
1609 /* At this point, all valid parameters should have been noted. */
1610 TAILQ_FOREACH(opt, opts, link) {
1611 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1613 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1618 /* Set the parameters of the prison. */
1621 if (pr_flags & PR_IP4_USER) {
1622 pr->pr_flags |= PR_IP4;
1623 free(pr->pr_ip4, M_PRISON);
1627 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1629 if (tpr->pr_flags & PR_VNET) {
1634 if (prison_restrict_ip4(tpr, NULL)) {
1643 if (pr_flags & PR_IP6_USER) {
1644 pr->pr_flags |= PR_IP6;
1645 free(pr->pr_ip6, M_PRISON);
1649 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1651 if (tpr->pr_flags & PR_VNET) {
1656 if (prison_restrict_ip6(tpr, NULL)) {
1664 pr->pr_securelevel = slevel;
1665 /* Set all child jails to be at least this level. */
1666 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1667 if (tpr->pr_securelevel < slevel)
1668 tpr->pr_securelevel = slevel;
1671 pr->pr_childmax = childmax;
1672 /* Set all child jails to under this limit. */
1673 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1674 if (tpr->pr_childmax > childmax - level)
1675 tpr->pr_childmax = childmax > level
1676 ? childmax - level : 0;
1679 pr->pr_enforce_statfs = enforce;
1680 /* Pass this restriction on to the children. */
1681 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1682 if (tpr->pr_enforce_statfs < enforce)
1683 tpr->pr_enforce_statfs = enforce;
1686 pr->pr_devfs_rsnum = rsnum;
1687 /* Pass this restriction on to the children. */
1688 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1689 tpr->pr_devfs_rsnum = rsnum;
1691 if (namelc != NULL) {
1692 if (ppr == &prison0)
1693 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1695 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1696 ppr->pr_name, namelc);
1697 /* Change this component of child names. */
1698 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1699 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1700 strlen(tpr->pr_name + onamelen) + 1);
1701 bcopy(pr->pr_name, tpr->pr_name, namelen);
1705 /* Try to keep a real-rooted full pathname. */
1706 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1710 if (PR_HOST & ch_flags & ~pr_flags) {
1711 if (pr->pr_flags & PR_HOST) {
1713 * Copy the parent's host info. As with pr_ip4 above,
1714 * the lack of a lock on the parent is not a problem;
1715 * it is always set with allprison_lock at least
1716 * shared, and is held exclusively here.
1718 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1719 sizeof(pr->pr_hostname));
1720 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1721 sizeof(pr->pr_domainname));
1722 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1723 sizeof(pr->pr_hostuuid));
1724 pr->pr_hostid = pr->pr_parent->pr_hostid;
1726 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1727 /* Set this prison, and any descendants without PR_HOST. */
1729 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1731 strlcpy(pr->pr_domainname, domain,
1732 sizeof(pr->pr_domainname));
1734 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1736 pr->pr_hostid = hid;
1737 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1738 if (tpr->pr_flags & PR_HOST)
1742 strlcpy(tpr->pr_hostname,
1744 sizeof(tpr->pr_hostname));
1746 strlcpy(tpr->pr_domainname,
1748 sizeof(tpr->pr_domainname));
1750 strlcpy(tpr->pr_hostuuid,
1752 sizeof(tpr->pr_hostuuid));
1754 tpr->pr_hostid = hid;
1758 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1759 if ((tallow = ch_allow & ~pr_allow))
1760 prison_set_allow_locked(pr, tallow, 0);
1762 * Persistent prisons get an extra reference, and prisons losing their
1763 * persist flag lose that reference.
1765 born = !prison_isalive(pr);
1766 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1767 if (pr_flags & PR_PERSIST) {
1770 * This may make a dead prison alive again, but wait
1771 * to label it as such until after OSD calls have had
1772 * a chance to run (and perhaps to fail).
1774 refcount_acquire(&pr->pr_uref);
1776 drflags |= PD_DEUREF;
1777 prison_free_not_last(pr);
1780 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1781 mtx_unlock(&pr->pr_mtx);
1782 drflags &= ~PD_LOCKED;
1784 * Any errors past this point will need to de-persist newly created
1785 * prisons, as well as call remove methods.
1791 if (racct_enable && created)
1792 prison_racct_attach(pr);
1795 /* Locks may have prevented a complete restriction of child IP
1796 * addresses. If so, allocate some more memory and try again.
1801 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1802 mtx_lock(&pr->pr_mtx);
1804 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1806 if (tpr->pr_flags & PR_VNET) {
1811 if (prison_restrict_ip4(tpr, ip4)) {
1818 mtx_unlock(&pr->pr_mtx);
1824 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1825 mtx_lock(&pr->pr_mtx);
1827 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1829 if (tpr->pr_flags & PR_VNET) {
1834 if (prison_restrict_ip6(tpr, ip6)) {
1841 mtx_unlock(&pr->pr_mtx);
1845 /* Let the modules do their work. */
1847 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1851 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1856 * A new prison is now ready to be seen; either it has gained a user
1857 * reference via persistence, or is about to gain one via attachment.
1860 drflags = prison_lock_xlock(pr, drflags);
1861 pr->pr_state = PRISON_STATE_ALIVE;
1864 /* Attach this process to the prison if requested. */
1865 if (flags & JAIL_ATTACH) {
1866 error = do_jail_attach(td, pr,
1867 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
1868 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1870 vfs_opterror(opts, "attach failed");
1876 if (racct_enable && !created) {
1877 if (drflags & PD_LOCKED) {
1878 mtx_unlock(&pr->pr_mtx);
1879 drflags &= ~PD_LOCKED;
1881 if (drflags & PD_LIST_XLOCKED) {
1882 sx_xunlock(&allprison_lock);
1883 drflags &= ~PD_LIST_XLOCKED;
1885 prison_racct_modify(pr);
1889 if (born && pr != &prison0 && (pr->pr_allow & PR_ALLOW_NFSD) != 0 &&
1890 (pr->pr_root->v_vflag & VV_ROOT) == 0)
1891 printf("Warning jail jid=%d: mountd/nfsd requires a separate"
1892 " file system\n", pr->pr_id);
1894 drflags &= ~PD_KILL;
1895 td->td_retval[0] = pr->pr_id;
1898 /* Release any temporary prison holds and/or locks. */
1900 prison_deref(pr, drflags);
1901 else if (drflags & PD_LIST_SLOCKED)
1902 sx_sunlock(&allprison_lock);
1903 else if (drflags & PD_LIST_XLOCKED)
1904 sx_xunlock(&allprison_lock);
1909 /* Write the error message back to userspace. */
1910 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1911 &errmsg_len) == 0 && errmsg_len > 0) {
1912 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1913 if (optuio->uio_segflg == UIO_SYSSPACE)
1915 optuio->uio_iov[errmsg_pos].iov_base,
1918 (void)copyout(errmsg,
1919 optuio->uio_iov[errmsg_pos].iov_base,
1925 free(ip4, M_PRISON);
1928 free(ip6, M_PRISON);
1931 free(g_path, M_TEMP);
1937 * Find the next available prison ID. Return the ID on success, or zero
1938 * on failure. Also set a pointer to the allprison list entry the prison
1939 * should be inserted before.
1942 get_next_prid(struct prison **insprp)
1944 struct prison *inspr;
1947 jid = lastprid % JAIL_MAX + 1;
1948 if (TAILQ_EMPTY(&allprison) ||
1949 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1951 * A common case is for all jails to be implicitly numbered,
1952 * which means they'll go on the end of the list, at least
1953 * for the first JAIL_MAX times.
1958 * Take two passes through the allprison list: first starting
1959 * with the proposed jid, then ending with it.
1961 for (maxid = JAIL_MAX; maxid != 0; ) {
1962 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1963 if (inspr->pr_id < jid)
1965 if (inspr->pr_id > jid) {
1966 /* Found an opening. */
1970 if (++jid > maxid) {
1971 if (lastprid == maxid || lastprid == 0)
1974 * The entire legal range
1975 * has been traversed
1979 /* Try again from the start. */
1985 if (inspr == NULL) {
1986 /* Found room at the end of the list. */
1997 * struct jail_get_args {
1998 * struct iovec *iovp;
1999 * unsigned int iovcnt;
2004 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2009 /* Check that we have an even number of iovecs. */
2010 if (uap->iovcnt & 1)
2013 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2016 error = kern_jail_get(td, auio, uap->flags);
2018 error = copyout(auio->uio_iov, uap->iovp,
2019 uap->iovcnt * sizeof (struct iovec));
2025 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2027 struct bool_flags *bf;
2028 struct jailsys_flags *jsf;
2029 struct prison *pr, *mypr;
2031 struct vfsoptlist *opts;
2032 char *errmsg, *name;
2033 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2036 if (flags & ~JAIL_GET_MASK)
2039 /* Get the parameter list. */
2040 error = vfs_buildopts(optuio, &opts);
2043 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2044 mypr = td->td_ucred->cr_prison;
2048 * Find the prison specified by one of: lastjid, jid, name.
2050 sx_slock(&allprison_lock);
2051 drflags = PD_LIST_SLOCKED;
2052 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2054 TAILQ_FOREACH(pr, &allprison, pr_list) {
2055 if (pr->pr_id > jid &&
2056 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2057 prison_ischild(mypr, pr)) {
2058 mtx_lock(&pr->pr_mtx);
2059 drflags |= PD_LOCKED;
2064 vfs_opterror(opts, "no jail after %d", jid);
2066 } else if (error != ENOENT)
2069 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2072 pr = prison_find_child(mypr, jid);
2074 drflags |= PD_LOCKED;
2075 if (!(prison_isalive(pr) ||
2076 (flags & JAIL_DYING))) {
2078 vfs_opterror(opts, "jail %d is dying",
2085 vfs_opterror(opts, "jail %d not found", jid);
2088 } else if (error != ENOENT)
2091 error = vfs_getopt(opts, "name", (void **)&name, &len);
2093 if (len == 0 || name[len - 1] != '\0') {
2097 pr = prison_find_name(mypr, name);
2099 drflags |= PD_LOCKED;
2100 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2102 vfs_opterror(opts, "jail \"%s\" is dying",
2109 vfs_opterror(opts, "jail \"%s\" not found", name);
2111 } else if (error != ENOENT)
2114 vfs_opterror(opts, "no jail specified");
2119 /* Get the parameters of the prison. */
2121 drflags |= PD_DEREF;
2122 td->td_retval[0] = pr->pr_id;
2123 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2124 if (error != 0 && error != ENOENT)
2126 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2127 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2128 if (error != 0 && error != ENOENT)
2130 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2131 if (error != 0 && error != ENOENT)
2133 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2134 sizeof(pr->pr_cpuset->cs_id));
2135 if (error != 0 && error != ENOENT)
2137 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2138 if (error != 0 && error != ENOENT)
2141 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2142 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2143 if (error != 0 && error != ENOENT)
2147 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2148 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2149 if (error != 0 && error != ENOENT)
2152 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2153 sizeof(pr->pr_securelevel));
2154 if (error != 0 && error != ENOENT)
2156 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2157 sizeof(pr->pr_childcount));
2158 if (error != 0 && error != ENOENT)
2160 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2161 sizeof(pr->pr_childmax));
2162 if (error != 0 && error != ENOENT)
2164 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2165 if (error != 0 && error != ENOENT)
2167 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2168 if (error != 0 && error != ENOENT)
2170 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2171 if (error != 0 && error != ENOENT)
2173 #ifdef COMPAT_FREEBSD32
2174 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2175 uint32_t hid32 = pr->pr_hostid;
2177 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2180 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2181 sizeof(pr->pr_hostid));
2182 if (error != 0 && error != ENOENT)
2184 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2185 sizeof(pr->pr_enforce_statfs));
2186 if (error != 0 && error != ENOENT)
2188 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2189 sizeof(pr->pr_devfs_rsnum));
2190 if (error != 0 && error != ENOENT)
2192 for (bf = pr_flag_bool;
2193 bf < pr_flag_bool + nitems(pr_flag_bool);
2195 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2196 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2197 if (error != 0 && error != ENOENT)
2200 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2201 if (error != 0 && error != ENOENT)
2204 for (jsf = pr_flag_jailsys;
2205 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2207 f = pr->pr_flags & (jsf->disable | jsf->new);
2208 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2209 : (f == jsf->new) ? JAIL_SYS_NEW
2211 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2212 if (error != 0 && error != ENOENT)
2215 for (bf = pr_flag_allow;
2216 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2217 atomic_load_int(&bf->flag) != 0;
2219 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2220 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2221 if (error != 0 && error != ENOENT)
2224 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2225 if (error != 0 && error != ENOENT)
2228 i = !prison_isalive(pr);
2229 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2230 if (error != 0 && error != ENOENT)
2233 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2234 if (error != 0 && error != ENOENT)
2236 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2237 sizeof(pr->pr_osreldate));
2238 if (error != 0 && error != ENOENT)
2240 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2241 if (error != 0 && error != ENOENT)
2244 /* Get the module parameters. */
2245 mtx_unlock(&pr->pr_mtx);
2246 drflags &= ~PD_LOCKED;
2247 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2250 prison_deref(pr, drflags);
2254 /* By now, all parameters should have been noted. */
2255 TAILQ_FOREACH(opt, opts, link) {
2256 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2258 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2263 /* Write the fetched parameters back to userspace. */
2265 TAILQ_FOREACH(opt, opts, link) {
2266 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2267 pos = 2 * opt->pos + 1;
2268 optuio->uio_iov[pos].iov_len = opt->len;
2269 if (opt->value != NULL) {
2270 if (optuio->uio_segflg == UIO_SYSSPACE) {
2272 optuio->uio_iov[pos].iov_base,
2275 error = copyout(opt->value,
2276 optuio->uio_iov[pos].iov_base,
2286 /* Release any temporary prison holds and/or locks. */
2288 prison_deref(pr, drflags);
2289 else if (drflags & PD_LIST_SLOCKED)
2290 sx_sunlock(&allprison_lock);
2291 if (error && errmsg_pos >= 0) {
2292 /* Write the error message back to userspace. */
2293 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2294 errmsg_pos = 2 * errmsg_pos + 1;
2295 if (errmsg_len > 0) {
2296 if (optuio->uio_segflg == UIO_SYSSPACE)
2298 optuio->uio_iov[errmsg_pos].iov_base,
2301 (void)copyout(errmsg,
2302 optuio->uio_iov[errmsg_pos].iov_base,
2311 * struct jail_remove_args {
2316 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2321 error = priv_check(td, PRIV_JAIL_REMOVE);
2325 sx_xlock(&allprison_lock);
2326 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2328 sx_xunlock(&allprison_lock);
2331 if (!prison_isalive(pr)) {
2332 /* Silently ignore already-dying prisons. */
2333 mtx_unlock(&pr->pr_mtx);
2334 sx_xunlock(&allprison_lock);
2337 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2342 * struct jail_attach_args {
2347 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2352 error = priv_check(td, PRIV_JAIL_ATTACH);
2356 sx_slock(&allprison_lock);
2357 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2359 sx_sunlock(&allprison_lock);
2363 /* Do not allow a process to attach to a prison that is not alive. */
2364 if (!prison_isalive(pr)) {
2365 mtx_unlock(&pr->pr_mtx);
2366 sx_sunlock(&allprison_lock);
2370 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2374 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2377 struct ucred *newcred, *oldcred;
2380 mtx_assert(&pr->pr_mtx, MA_OWNED);
2381 sx_assert(&allprison_lock, SX_LOCKED);
2382 drflags &= PD_LOCK_FLAGS;
2384 * XXX: Note that there is a slight race here if two threads
2385 * in the same privileged process attempt to attach to two
2386 * different jails at the same time. It is important for
2387 * user processes not to do this, or they might end up with
2388 * a process root from one prison, but attached to the jail
2392 refcount_acquire(&pr->pr_uref);
2393 drflags |= PD_DEREF | PD_DEUREF;
2394 mtx_unlock(&pr->pr_mtx);
2395 drflags &= ~PD_LOCKED;
2397 /* Let modules do whatever they need to prepare for attaching. */
2398 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2400 prison_deref(pr, drflags);
2403 sx_unlock(&allprison_lock);
2404 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2407 * Reparent the newly attached process to this jail.
2410 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2414 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2415 if ((error = change_dir(pr->pr_root, td)) != 0)
2418 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2421 VOP_UNLOCK(pr->pr_root);
2422 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2427 oldcred = crcopysafe(p, newcred);
2428 newcred->cr_prison = pr;
2429 proc_set_cred(p, newcred);
2432 racct_proc_ucred_changed(p, oldcred, newcred);
2437 rctl_proc_ucred_changed(p, newcred);
2440 prison_deref(oldcred->cr_prison, drflags);
2444 * If the prison was killed while changing credentials, die along
2447 if (!prison_isalive(pr)) {
2449 kern_psignal(p, SIGKILL);
2456 VOP_UNLOCK(pr->pr_root);
2458 /* Tell modules this thread is still in its old jail after all. */
2459 sx_slock(&allprison_lock);
2460 drflags |= PD_LIST_SLOCKED;
2461 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2462 prison_deref(pr, drflags);
2467 * Returns a locked prison instance, or NULL on failure.
2470 prison_find(int prid)
2474 sx_assert(&allprison_lock, SX_LOCKED);
2475 TAILQ_FOREACH(pr, &allprison, pr_list) {
2476 if (pr->pr_id < prid)
2478 if (pr->pr_id > prid)
2480 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2481 mtx_lock(&pr->pr_mtx);
2488 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2491 prison_find_child(struct prison *mypr, int prid)
2496 sx_assert(&allprison_lock, SX_LOCKED);
2497 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2498 if (pr->pr_id == prid) {
2499 KASSERT(prison_isvalid(pr),
2500 ("Found invalid prison %p", pr));
2501 mtx_lock(&pr->pr_mtx);
2509 * Look for the name relative to mypr. Returns a locked prison or NULL.
2512 prison_find_name(struct prison *mypr, const char *name)
2514 struct prison *pr, *deadpr;
2518 sx_assert(&allprison_lock, SX_LOCKED);
2519 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2521 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2522 if (!strcmp(pr->pr_name + mylen, name)) {
2523 KASSERT(prison_isvalid(pr),
2524 ("Found invalid prison %p", pr));
2525 if (prison_isalive(pr)) {
2526 mtx_lock(&pr->pr_mtx);
2532 /* There was no valid prison - perhaps there was a dying one. */
2534 mtx_lock(&deadpr->pr_mtx);
2539 * See if a prison has the specific flag set. The prison should be locked,
2540 * unless checking for flags that are only set at jail creation (such as
2541 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2542 * to any other prison data.
2545 prison_flag(struct ucred *cred, unsigned flag)
2548 return (cred->cr_prison->pr_flags & flag);
2552 prison_allow(struct ucred *cred, unsigned flag)
2555 return ((cred->cr_prison->pr_allow & flag) != 0);
2559 * Hold a prison reference, by incrementing pr_ref. It is generally
2560 * an error to hold a prison that does not already have a reference.
2561 * A prison record will remain valid as long as it has at least one
2562 * reference, and will not be removed as long as either the prison
2563 * mutex or the allprison lock is held (allprison_lock may be shared).
2566 prison_hold_locked(struct prison *pr)
2569 /* Locking is no longer required. */
2574 prison_hold(struct prison *pr)
2577 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2580 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2582 refcount_acquire(&pr->pr_ref);
2587 * Remove a prison reference. If that was the last reference, the
2588 * prison will be removed (at a later time).
2591 prison_free_locked(struct prison *pr)
2594 mtx_assert(&pr->pr_mtx, MA_OWNED);
2596 * Locking is no longer required, but unlock because the caller
2599 mtx_unlock(&pr->pr_mtx);
2604 prison_free(struct prison *pr)
2607 KASSERT(refcount_load(&pr->pr_ref) > 0,
2608 ("Trying to free dead prison %p (jid=%d).",
2610 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2612 * Don't remove the last reference in this context,
2613 * in case there are locks held.
2615 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2620 prison_free_not_last(struct prison *pr)
2625 KASSERT(refcount_load(&pr->pr_ref) > 0,
2626 ("Trying to free dead prison %p (jid=%d).",
2628 lastref = refcount_release(&pr->pr_ref);
2630 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2633 refcount_release(&pr->pr_ref);
2638 * Hold a prison for user visibility, by incrementing pr_uref.
2639 * It is generally an error to hold a prison that isn't already
2640 * user-visible, except through the the jail system calls. It is also
2641 * an error to hold an invalid prison. A prison record will remain
2642 * alive as long as it has at least one user reference, and will not
2643 * be set to the dying state until the prison mutex and allprison_lock
2647 prison_proc_hold(struct prison *pr)
2650 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2653 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2655 refcount_acquire(&pr->pr_uref);
2660 * Remove a prison user reference. If it was the last reference, the
2661 * prison will be considered "dying", and may be removed once all of
2662 * its references are dropped.
2665 prison_proc_free(struct prison *pr)
2669 * Locking is only required when releasing the last reference.
2670 * This allows assurance that a locked prison will remain alive
2671 * until it is unlocked.
2673 KASSERT(refcount_load(&pr->pr_uref) > 0,
2674 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2675 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2677 * Don't remove the last user reference in this context,
2678 * which is expected to be a process that is not only locked,
2679 * but also half dead. Add a reference so any calls to
2680 * prison_free() won't re-submit the task.
2683 mtx_lock(&pr->pr_mtx);
2684 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2685 ("Redundant last reference in prison_proc_free (jid=%d)",
2687 pr->pr_flags |= PR_COMPLETE_PROC;
2688 mtx_unlock(&pr->pr_mtx);
2689 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2694 prison_proc_free_not_last(struct prison *pr)
2699 KASSERT(refcount_load(&pr->pr_uref) > 0,
2700 ("Trying to free dead prison %p (jid=%d).",
2702 lastref = refcount_release(&pr->pr_uref);
2704 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2707 refcount_release(&pr->pr_uref);
2712 * Complete a call to either prison_free or prison_proc_free.
2715 prison_complete(void *context, int pending)
2717 struct prison *pr = context;
2721 * This could be called to release the last reference, or the last
2722 * user reference (plus the reference held in prison_proc_free).
2724 drflags = prison_lock_xlock(pr, PD_DEREF);
2725 if (pr->pr_flags & PR_COMPLETE_PROC) {
2726 pr->pr_flags &= ~PR_COMPLETE_PROC;
2727 drflags |= PD_DEUREF;
2729 prison_deref(pr, drflags);
2733 * Remove a prison reference and/or user reference (usually).
2734 * This assumes context that allows sleeping (for allprison_lock),
2735 * with no non-sleeping locks held, except perhaps the prison itself.
2736 * If there are no more references, release and delist the prison.
2737 * On completion, the prison lock and the allprison lock are both
2741 prison_deref(struct prison *pr, int flags)
2743 struct prisonlist freeprison;
2744 struct prison *killpr, *rpr, *ppr, *tpr;
2748 TAILQ_INIT(&freeprison);
2750 * Release this prison as requested, which may cause its parent
2751 * to be released, and then maybe its grandparent, etc.
2754 if (flags & PD_KILL) {
2755 /* Kill the prison and its descendents. */
2756 KASSERT(pr != &prison0,
2757 ("prison_deref trying to kill prison0"));
2758 if (!(flags & PD_DEREF)) {
2762 flags = prison_lock_xlock(pr, flags);
2763 prison_deref_kill(pr, &freeprison);
2765 if (flags & PD_DEUREF) {
2766 /* Drop a user reference. */
2767 KASSERT(refcount_load(&pr->pr_uref) > 0,
2768 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2770 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2771 if (!(flags & PD_DEREF)) {
2775 flags = prison_lock_xlock(pr, flags);
2776 if (refcount_release(&pr->pr_uref) &&
2777 pr->pr_state == PRISON_STATE_ALIVE) {
2779 * When the last user references goes,
2780 * this becomes a dying prison.
2783 refcount_load(&prison0.pr_uref) > 0,
2784 ("prison0 pr_uref=0"));
2785 pr->pr_state = PRISON_STATE_DYING;
2786 mtx_unlock(&pr->pr_mtx);
2787 flags &= ~PD_LOCKED;
2792 if (flags & PD_KILL) {
2794 * Any remaining user references are probably processes
2795 * that need to be killed, either in this prison or its
2798 if (refcount_load(&pr->pr_uref) > 0)
2800 /* Make sure the parent prison doesn't get killed. */
2803 if (flags & PD_DEREF) {
2804 /* Drop a reference. */
2805 KASSERT(refcount_load(&pr->pr_ref) > 0,
2806 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2808 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2809 flags = prison_lock_xlock(pr, flags);
2810 if (refcount_release(&pr->pr_ref)) {
2812 * When the last reference goes,
2813 * unlink the prison and set it aside.
2816 refcount_load(&pr->pr_uref) == 0,
2817 ("prison_deref: last ref, "
2818 "but still has %d urefs (jid=%d)",
2819 pr->pr_uref, pr->pr_id));
2821 refcount_load(&prison0.pr_ref) != 0,
2822 ("prison0 pr_ref=0"));
2823 pr->pr_state = PRISON_STATE_INVALID;
2824 TAILQ_REMOVE(&allprison, pr, pr_list);
2825 LIST_REMOVE(pr, pr_sibling);
2826 TAILQ_INSERT_TAIL(&freeprison, pr,
2828 for (ppr = pr->pr_parent;
2830 ppr = ppr->pr_parent)
2831 ppr->pr_childcount--;
2833 * Removing a prison frees references
2836 mtx_unlock(&pr->pr_mtx);
2837 flags &= ~PD_LOCKED;
2839 flags |= PD_DEREF | PD_DEUREF;
2847 /* Release all the prison locks. */
2848 if (flags & PD_LOCKED)
2849 mtx_unlock(&pr->pr_mtx);
2850 if (flags & PD_LIST_SLOCKED)
2851 sx_sunlock(&allprison_lock);
2852 else if (flags & PD_LIST_XLOCKED)
2853 sx_xunlock(&allprison_lock);
2855 /* Kill any processes attached to a killed prison. */
2856 if (killpr != NULL) {
2857 sx_slock(&allproc_lock);
2858 FOREACH_PROC_IN_SYSTEM(p) {
2860 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
2861 for (ppr = p->p_ucred->cr_prison;
2863 ppr = ppr->pr_parent)
2864 if (ppr == killpr) {
2865 kern_psignal(p, SIGKILL);
2871 sx_sunlock(&allproc_lock);
2875 * Finish removing any unreferenced prisons, which couldn't happen
2876 * while allprison_lock was held (to avoid a LOR on vrele).
2878 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
2880 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
2881 vnet_destroy(rpr->pr_vnet);
2883 if (rpr->pr_root != NULL)
2884 vrele(rpr->pr_root);
2885 mtx_destroy(&rpr->pr_mtx);
2887 free(rpr->pr_ip4, M_PRISON);
2890 free(rpr->pr_ip6, M_PRISON);
2892 if (rpr->pr_cpuset != NULL)
2893 cpuset_rel(rpr->pr_cpuset);
2897 prison_racct_detach(rpr);
2899 TAILQ_REMOVE(&freeprison, rpr, pr_list);
2900 free(rpr, M_PRISON);
2905 * Kill the prison and its descendants. Mark them as dying, clear the
2906 * persist flag, and call module remove methods.
2909 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
2911 struct prison *cpr, *ppr, *rpr;
2915 * Unlike the descendants, the target prison can be killed
2916 * even if it is currently dying. This is useful for failed
2917 * creation in jail_set(2).
2919 KASSERT(refcount_load(&pr->pr_ref) > 0,
2920 ("Trying to kill dead prison %p (jid=%d).",
2922 refcount_acquire(&pr->pr_uref);
2923 pr->pr_state = PRISON_STATE_DYING;
2924 mtx_unlock(&pr->pr_mtx);
2927 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
2929 if (!prison_isalive(cpr)) {
2934 prison_proc_hold(cpr);
2935 mtx_lock(&cpr->pr_mtx);
2936 cpr->pr_state = PRISON_STATE_DYING;
2937 cpr->pr_flags |= PR_REMOVE;
2938 mtx_unlock(&cpr->pr_mtx);
2941 if (!(cpr->pr_flags & PR_REMOVE))
2943 prison_cleanup(cpr);
2944 mtx_lock(&cpr->pr_mtx);
2945 cpr->pr_flags &= ~PR_REMOVE;
2946 if (cpr->pr_flags & PR_PERSIST) {
2947 cpr->pr_flags &= ~PR_PERSIST;
2948 prison_proc_free_not_last(cpr);
2949 prison_free_not_last(cpr);
2951 (void)refcount_release(&cpr->pr_uref);
2952 if (refcount_release(&cpr->pr_ref)) {
2954 * When the last reference goes, unlink the prison
2955 * and set it aside for prison_deref() to handle.
2956 * Delay unlinking the sibling list to keep the loop
2960 LIST_REMOVE(rpr, pr_sibling);
2962 rpr->pr_state = PRISON_STATE_INVALID;
2963 TAILQ_REMOVE(&allprison, rpr, pr_list);
2964 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
2966 * Removing a prison frees references from its parent.
2968 ppr = rpr->pr_parent;
2969 prison_proc_free_not_last(ppr);
2970 prison_free_not_last(ppr);
2971 for (; ppr != NULL; ppr = ppr->pr_parent)
2972 ppr->pr_childcount--;
2974 mtx_unlock(&cpr->pr_mtx);
2977 LIST_REMOVE(rpr, pr_sibling);
2980 mtx_lock(&pr->pr_mtx);
2981 if (pr->pr_flags & PR_PERSIST) {
2982 pr->pr_flags &= ~PR_PERSIST;
2983 prison_proc_free_not_last(pr);
2984 prison_free_not_last(pr);
2986 (void)refcount_release(&pr->pr_uref);
2990 * Given the current locking state in the flags, make sure allprison_lock
2991 * is held exclusive, and the prison is locked. Return flags indicating
2995 prison_lock_xlock(struct prison *pr, int flags)
2998 if (!(flags & PD_LIST_XLOCKED)) {
3000 * Get allprison_lock, which may be an upgrade,
3001 * and may require unlocking the prison.
3003 if (flags & PD_LOCKED) {
3004 mtx_unlock(&pr->pr_mtx);
3005 flags &= ~PD_LOCKED;
3007 if (flags & PD_LIST_SLOCKED) {
3008 if (!sx_try_upgrade(&allprison_lock)) {
3009 sx_sunlock(&allprison_lock);
3010 sx_xlock(&allprison_lock);
3012 flags &= ~PD_LIST_SLOCKED;
3014 sx_xlock(&allprison_lock);
3015 flags |= PD_LIST_XLOCKED;
3017 if (!(flags & PD_LOCKED)) {
3018 /* Lock the prison mutex. */
3019 mtx_lock(&pr->pr_mtx);
3026 * Release a prison's resources when it starts dying (when the last user
3027 * reference is dropped, or when it is killed).
3030 prison_cleanup(struct prison *pr)
3032 sx_assert(&allprison_lock, SA_XLOCKED);
3033 mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
3034 vfs_exjail_delete(pr);
3035 shm_remove_prison(pr);
3036 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3040 * Set or clear a permission bit in the pr_allow field, passing restrictions
3041 * (cleared permission) down to child jails.
3044 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3048 pr = cred->cr_prison;
3049 sx_slock(&allprison_lock);
3050 mtx_lock(&pr->pr_mtx);
3051 prison_set_allow_locked(pr, flag, enable);
3052 mtx_unlock(&pr->pr_mtx);
3053 sx_sunlock(&allprison_lock);
3057 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3063 pr->pr_allow |= flag;
3065 pr->pr_allow &= ~flag;
3066 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3067 cpr->pr_allow &= ~flag;
3072 * Check if a jail supports the given address family.
3074 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3078 prison_check_af(struct ucred *cred, int af)
3083 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3085 pr = cred->cr_prison;
3087 /* Prisons with their own network stack are not limited. */
3088 if (prison_owns_vnet(cred))
3097 if (pr->pr_flags & PR_IP4)
3099 mtx_lock(&pr->pr_mtx);
3100 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3101 error = EAFNOSUPPORT;
3102 mtx_unlock(&pr->pr_mtx);
3108 if (pr->pr_flags & PR_IP6)
3110 mtx_lock(&pr->pr_mtx);
3111 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3112 error = EAFNOSUPPORT;
3113 mtx_unlock(&pr->pr_mtx);
3121 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3122 error = EAFNOSUPPORT;
3128 * Check if given address belongs to the jail referenced by cred (wrapper to
3129 * prison_check_ip[46]).
3131 * Returns 0 if jail doesn't restrict the address family or if address belongs
3132 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3133 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3136 prison_if(struct ucred *cred, const struct sockaddr *sa)
3139 const struct sockaddr_in *sai;
3142 const struct sockaddr_in6 *sai6;
3146 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3147 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3150 if (prison_owns_vnet(cred))
3155 switch (sa->sa_family)
3159 sai = (const struct sockaddr_in *)sa;
3160 error = prison_check_ip4(cred, &sai->sin_addr);
3165 sai6 = (const struct sockaddr_in6 *)sa;
3166 error = prison_check_ip6(cred, &sai6->sin6_addr);
3170 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3171 error = EAFNOSUPPORT;
3177 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3180 prison_check(struct ucred *cred1, struct ucred *cred2)
3183 return ((cred1->cr_prison == cred2->cr_prison ||
3184 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3188 * For mountd/nfsd to run within a prison, it must be:
3190 * - PR_ALLOW_NFSD must be set on it.
3191 * - The root directory (pr_root) of the prison must be
3192 * a file system mount point, so the mountd can hang
3193 * export information on it.
3194 * - The prison's enforce_statfs cannot be 0, so that
3195 * mountd(8) can do exports.
3198 prison_check_nfsd(struct ucred *cred)
3201 if (jailed_without_vnet(cred))
3203 if (!prison_allow(cred, PR_ALLOW_NFSD))
3205 if ((cred->cr_prison->pr_root->v_vflag & VV_ROOT) == 0)
3207 if (cred->cr_prison->pr_enforce_statfs == 0)
3213 * Return 1 if p2 is a child of p1, otherwise 0.
3216 prison_ischild(struct prison *pr1, struct prison *pr2)
3219 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3226 * Return true if the prison is currently alive. A prison is alive if it
3227 * holds user references and it isn't being removed.
3230 prison_isalive(struct prison *pr)
3233 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3239 * Return true if the prison is currently valid. A prison is valid if it has
3240 * been fully created, and is not being destroyed. Note that dying prisons
3241 * are still considered valid. Invalid prisons won't be found under normal
3242 * circumstances, as they're only put in that state by functions that have
3243 * an exclusive hold on allprison_lock.
3246 prison_isvalid(struct prison *pr)
3249 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3251 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3257 * Return 1 if the passed credential is in a jail and that jail does not
3258 * have its own virtual network stack, otherwise 0.
3261 jailed_without_vnet(struct ucred *cred)
3267 if (prison_owns_vnet(cred))
3275 * Return the correct hostname (domainname, et al) for the passed credential.
3278 getcredhostname(struct ucred *cred, char *buf, size_t size)
3283 * A NULL credential can be used to shortcut to the physical
3284 * system's hostname.
3286 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3287 mtx_lock(&pr->pr_mtx);
3288 strlcpy(buf, pr->pr_hostname, size);
3289 mtx_unlock(&pr->pr_mtx);
3293 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3296 mtx_lock(&cred->cr_prison->pr_mtx);
3297 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3298 mtx_unlock(&cred->cr_prison->pr_mtx);
3302 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3305 mtx_lock(&cred->cr_prison->pr_mtx);
3306 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3307 mtx_unlock(&cred->cr_prison->pr_mtx);
3311 getcredhostid(struct ucred *cred, unsigned long *hostid)
3314 mtx_lock(&cred->cr_prison->pr_mtx);
3315 *hostid = cred->cr_prison->pr_hostid;
3316 mtx_unlock(&cred->cr_prison->pr_mtx);
3320 getjailname(struct ucred *cred, char *name, size_t len)
3323 mtx_lock(&cred->cr_prison->pr_mtx);
3324 strlcpy(name, cred->cr_prison->pr_name, len);
3325 mtx_unlock(&cred->cr_prison->pr_mtx);
3330 * Determine whether the prison represented by cred owns
3331 * its vnet rather than having it inherited.
3333 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3336 prison_owns_vnet(struct ucred *cred)
3340 * vnets cannot be added/removed after jail creation,
3341 * so no need to lock here.
3343 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3348 * Determine whether the subject represented by cred can "see"
3349 * status of a mount point.
3350 * Returns: 0 for permitted, ENOENT otherwise.
3351 * XXX: This function should be called cr_canseemount() and should be
3352 * placed in kern_prot.c.
3355 prison_canseemount(struct ucred *cred, struct mount *mp)
3361 pr = cred->cr_prison;
3362 if (pr->pr_enforce_statfs == 0)
3364 if (pr->pr_root->v_mount == mp)
3366 if (pr->pr_enforce_statfs == 2)
3369 * If jail's chroot directory is set to "/" we should be able to see
3370 * all mount-points from inside a jail.
3371 * This is ugly check, but this is the only situation when jail's
3372 * directory ends with '/'.
3374 if (strcmp(pr->pr_path, "/") == 0)
3376 len = strlen(pr->pr_path);
3378 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3381 * Be sure that we don't have situation where jail's root directory
3382 * is "/some/path" and mount point is "/some/pathpath".
3384 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3390 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3392 char jpath[MAXPATHLEN];
3396 pr = cred->cr_prison;
3397 if (pr->pr_enforce_statfs == 0)
3399 if (prison_canseemount(cred, mp) != 0) {
3400 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3401 strlcpy(sp->f_mntonname, "[restricted]",
3402 sizeof(sp->f_mntonname));
3405 if (pr->pr_root->v_mount == mp) {
3407 * Clear current buffer data, so we are sure nothing from
3408 * the valid path left there.
3410 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3411 *sp->f_mntonname = '/';
3415 * If jail's chroot directory is set to "/" we should be able to see
3416 * all mount-points from inside a jail.
3418 if (strcmp(pr->pr_path, "/") == 0)
3420 len = strlen(pr->pr_path);
3421 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3423 * Clear current buffer data, so we are sure nothing from
3424 * the valid path left there.
3426 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3427 if (*jpath == '\0') {
3428 /* Should never happen. */
3429 *sp->f_mntonname = '/';
3431 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3436 * Check with permission for a specific privilege is granted within jail. We
3437 * have a specific list of accepted privileges; the rest are denied.
3440 prison_priv_check(struct ucred *cred, int priv)
3446 * Some policies have custom handlers. This routine should not be
3447 * called for them. See priv_check_cred().
3450 case PRIV_VFS_LOOKUP:
3451 case PRIV_VFS_GENERATION:
3452 KASSERT(0, ("prison_priv_check instead of a custom handler "
3453 "called for %d\n", priv));
3461 * Privileges specific to prisons with a virtual network stack.
3462 * There might be a duplicate entry here in case the privilege
3463 * is only granted conditionally in the legacy jail case.
3467 * NFS-specific privileges.
3469 case PRIV_NFS_DAEMON:
3470 case PRIV_VFS_GETFH:
3471 case PRIV_VFS_MOUNT_EXPORTED:
3472 if (!prison_check_nfsd(cred))
3475 case PRIV_NFS_LOCKD:
3478 * Network stack privileges.
3480 case PRIV_NET_BRIDGE:
3483 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3484 case PRIV_NET_ROUTE:
3486 case PRIV_NET_SETIFMTU:
3487 case PRIV_NET_SETIFFLAGS:
3488 case PRIV_NET_SETIFCAP:
3489 case PRIV_NET_SETIFDESCR:
3490 case PRIV_NET_SETIFNAME :
3491 case PRIV_NET_SETIFMETRIC:
3492 case PRIV_NET_SETIFPHYS:
3493 case PRIV_NET_SETIFMAC:
3494 case PRIV_NET_SETLANPCP:
3495 case PRIV_NET_ADDMULTI:
3496 case PRIV_NET_DELMULTI:
3497 case PRIV_NET_HWIOCTL:
3498 case PRIV_NET_SETLLADDR:
3499 case PRIV_NET_ADDIFGROUP:
3500 case PRIV_NET_DELIFGROUP:
3501 case PRIV_NET_IFCREATE:
3502 case PRIV_NET_IFDESTROY:
3503 case PRIV_NET_ADDIFADDR:
3504 case PRIV_NET_DELIFADDR:
3507 case PRIV_NET_SETIFVNET:
3508 case PRIV_NET_SETIFFIB:
3513 * 802.11-related privileges.
3515 case PRIV_NET80211_VAP_GETKEY:
3516 case PRIV_NET80211_VAP_MANAGE:
3522 case PRIV_NETATM_CFG:
3523 case PRIV_NETATM_ADD:
3524 case PRIV_NETATM_DEL:
3525 case PRIV_NETATM_SET:
3528 * Bluetooth privileges.
3530 case PRIV_NETBLUETOOTH_RAW:
3534 * Netgraph and netgraph module privileges.
3536 case PRIV_NETGRAPH_CONTROL:
3538 case PRIV_NETGRAPH_TTY:
3542 * IPv4 and IPv6 privileges.
3544 case PRIV_NETINET_IPFW:
3545 case PRIV_NETINET_DIVERT:
3546 case PRIV_NETINET_PF:
3547 case PRIV_NETINET_DUMMYNET:
3548 case PRIV_NETINET_CARP:
3549 case PRIV_NETINET_MROUTE:
3550 case PRIV_NETINET_RAW:
3551 case PRIV_NETINET_ADDRCTRL6:
3552 case PRIV_NETINET_ND6:
3553 case PRIV_NETINET_SCOPE6:
3554 case PRIV_NETINET_ALIFETIME6:
3555 case PRIV_NETINET_IPSEC:
3556 case PRIV_NETINET_BINDANY:
3571 * No default: or deny here.
3572 * In case of no permit fall through to next switch().
3574 if (cred->cr_prison->pr_flags & PR_VNET)
3581 * Allow ktrace privileges for root in jail.
3587 * Allow jailed processes to configure audit identity and
3588 * submit audit records (login, etc). In the future we may
3589 * want to further refine the relationship between audit and
3592 case PRIV_AUDIT_GETAUDIT:
3593 case PRIV_AUDIT_SETAUDIT:
3594 case PRIV_AUDIT_SUBMIT:
3598 * Allow jailed processes to manipulate process UNIX
3599 * credentials in any way they see fit.
3601 case PRIV_CRED_SETUID:
3602 case PRIV_CRED_SETEUID:
3603 case PRIV_CRED_SETGID:
3604 case PRIV_CRED_SETEGID:
3605 case PRIV_CRED_SETGROUPS:
3606 case PRIV_CRED_SETREUID:
3607 case PRIV_CRED_SETREGID:
3608 case PRIV_CRED_SETRESUID:
3609 case PRIV_CRED_SETRESGID:
3612 * Jail implements visibility constraints already, so allow
3613 * jailed root to override uid/gid-based constraints.
3615 case PRIV_SEEOTHERGIDS:
3616 case PRIV_SEEOTHERUIDS:
3617 case PRIV_SEEJAILPROC:
3620 * Jail implements inter-process debugging limits already, so
3621 * allow jailed root various debugging privileges.
3623 case PRIV_DEBUG_DIFFCRED:
3624 case PRIV_DEBUG_SUGID:
3625 case PRIV_DEBUG_UNPRIV:
3628 * Allow jail to set various resource limits and login
3629 * properties, and for now, exceed process resource limits.
3631 case PRIV_PROC_LIMIT:
3632 case PRIV_PROC_SETLOGIN:
3633 case PRIV_PROC_SETRLIMIT:
3636 * System V and POSIX IPC privileges are granted in jail.
3639 case PRIV_IPC_WRITE:
3640 case PRIV_IPC_ADMIN:
3641 case PRIV_IPC_MSGSIZE:
3645 * Jail operations within a jail work on child jails.
3647 case PRIV_JAIL_ATTACH:
3649 case PRIV_JAIL_REMOVE:
3652 * Jail implements its own inter-process limits, so allow
3653 * root processes in jail to change scheduling on other
3654 * processes in the same jail. Likewise for signalling.
3656 case PRIV_SCHED_DIFFCRED:
3657 case PRIV_SCHED_CPUSET:
3658 case PRIV_SIGNAL_DIFFCRED:
3659 case PRIV_SIGNAL_SUGID:
3662 * Allow jailed processes to write to sysctls marked as jail
3665 case PRIV_SYSCTL_WRITEJAIL:
3668 * Allow root in jail to manage a variety of quota
3669 * properties. These should likely be conditional on a
3670 * configuration option.
3672 case PRIV_VFS_GETQUOTA:
3673 case PRIV_VFS_SETQUOTA:
3676 * Since Jail relies on chroot() to implement file system
3677 * protections, grant many VFS privileges to root in jail.
3678 * Be careful to exclude mount-related and NFS-related
3682 case PRIV_VFS_WRITE:
3683 case PRIV_VFS_ADMIN:
3685 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3686 case PRIV_VFS_CHFLAGS_DEV:
3687 case PRIV_VFS_CHOWN:
3688 case PRIV_VFS_CHROOT:
3689 case PRIV_VFS_RETAINSUGID:
3690 case PRIV_VFS_FCHROOT:
3692 case PRIV_VFS_SETGID:
3694 case PRIV_VFS_STICKYFILE:
3697 * As in the non-jail case, non-root users are expected to be
3698 * able to read kernel/physical memory (provided /dev/[k]mem
3699 * exists in the jail and they have permission to access it).
3701 case PRIV_KMEM_READ:
3705 * Depending on the global setting, allow privilege of
3706 * setting system flags.
3708 case PRIV_VFS_SYSFLAGS:
3709 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3715 * Depending on the global setting, allow privilege of
3716 * mounting/unmounting file systems.
3718 case PRIV_VFS_MOUNT:
3719 case PRIV_VFS_UNMOUNT:
3720 case PRIV_VFS_MOUNT_NONUSER:
3721 case PRIV_VFS_MOUNT_OWNER:
3722 pr = cred->cr_prison;
3724 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3732 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3733 * policy. priv_check_cred will not specifically allow it, and
3734 * we may want a MAC policy to allow it.
3736 case PRIV_VFS_READ_DIR:
3740 * Conditionnaly allow locking (unlocking) physical pages
3744 case PRIV_VM_MUNLOCK:
3745 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3751 * Conditionally allow jailed root to bind reserved ports.
3753 case PRIV_NETINET_RESERVEDPORT:
3754 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3760 * Allow jailed root to reuse in-use ports.
3762 case PRIV_NETINET_REUSEPORT:
3766 * Allow jailed root to set certain IPv4/6 (option) headers.
3768 case PRIV_NETINET_SETHDROPTS:
3772 * Conditionally allow creating raw sockets in jail.
3774 case PRIV_NETINET_RAW:
3775 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3781 * Since jail implements its own visibility limits on netstat
3782 * sysctls, allow getcred. This allows identd to work in
3785 case PRIV_NETINET_GETCRED:
3789 * Allow jailed root to set loginclass.
3791 case PRIV_PROC_SETLOGINCLASS:
3795 * Do not allow a process inside a jail to read the kernel
3796 * message buffer unless explicitly permitted.
3799 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3805 * In all remaining cases, deny the privilege request. This
3806 * includes almost all network privileges, many system
3807 * configuration privileges.
3814 * Return the part of pr2's name that is relative to pr1, or the whole name
3815 * if it does not directly follow.
3819 prison_name(struct prison *pr1, struct prison *pr2)
3823 /* Jails see themselves as "0" (if they see themselves at all). */
3826 name = pr2->pr_name;
3827 if (prison_ischild(pr1, pr2)) {
3829 * pr1 isn't locked (and allprison_lock may not be either)
3830 * so its length can't be counted on. But the number of dots
3831 * can be counted on - and counted.
3833 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3834 name = strchr(name, '.') + 1;
3840 * Return the part of pr2's path that is relative to pr1, or the whole path
3841 * if it does not directly follow.
3844 prison_path(struct prison *pr1, struct prison *pr2)
3846 char *path1, *path2;
3849 path1 = pr1->pr_path;
3850 path2 = pr2->pr_path;
3851 if (!strcmp(path1, "/"))
3853 len1 = strlen(path1);
3854 if (strncmp(path1, path2, len1))
3856 if (path2[len1] == '\0')
3858 if (path2[len1] == '/')
3859 return (path2 + len1);
3864 * Jail-related sysctls.
3866 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3870 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3873 struct prison *pr, *cpr;
3875 struct in_addr *ip4 = NULL;
3879 struct in6_addr *ip6 = NULL;
3884 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3885 pr = req->td->td_ucred->cr_prison;
3887 sx_slock(&allprison_lock);
3888 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3889 #if defined(INET) || defined(INET6)
3892 mtx_lock(&cpr->pr_mtx);
3894 if (cpr->pr_ip4s > 0) {
3895 if (ip4s < cpr->pr_ip4s) {
3896 ip4s = cpr->pr_ip4s;
3897 mtx_unlock(&cpr->pr_mtx);
3898 ip4 = realloc(ip4, ip4s *
3899 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3902 bcopy(cpr->pr_ip4, ip4,
3903 cpr->pr_ip4s * sizeof(struct in_addr));
3907 if (cpr->pr_ip6s > 0) {
3908 if (ip6s < cpr->pr_ip6s) {
3909 ip6s = cpr->pr_ip6s;
3910 mtx_unlock(&cpr->pr_mtx);
3911 ip6 = realloc(ip6, ip6s *
3912 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3915 bcopy(cpr->pr_ip6, ip6,
3916 cpr->pr_ip6s * sizeof(struct in6_addr));
3919 bzero(xp, sizeof(*xp));
3920 xp->pr_version = XPRISON_VERSION;
3921 xp->pr_id = cpr->pr_id;
3922 xp->pr_state = cpr->pr_state;
3923 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3924 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3925 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3927 xp->pr_ip4s = cpr->pr_ip4s;
3930 xp->pr_ip6s = cpr->pr_ip6s;
3932 mtx_unlock(&cpr->pr_mtx);
3933 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3937 if (xp->pr_ip4s > 0) {
3938 error = SYSCTL_OUT(req, ip4,
3939 xp->pr_ip4s * sizeof(struct in_addr));
3945 if (xp->pr_ip6s > 0) {
3946 error = SYSCTL_OUT(req, ip6,
3947 xp->pr_ip6s * sizeof(struct in6_addr));
3953 sx_sunlock(&allprison_lock);
3964 SYSCTL_OID(_security_jail, OID_AUTO, list,
3965 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3966 sysctl_jail_list, "S", "List of active jails");
3969 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3973 injail = jailed(req->td->td_ucred);
3974 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3979 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3980 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3981 sysctl_jail_jailed, "I", "Process in jail?");
3984 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3986 int error, havevnet;
3988 struct ucred *cred = req->td->td_ucred;
3990 havevnet = jailed(cred) && prison_owns_vnet(cred);
3994 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3999 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4000 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4001 sysctl_jail_vnet, "I", "Jail owns vnet?");
4003 #if defined(INET) || defined(INET6)
4004 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4005 &jail_max_af_ips, 0,
4006 "Number of IP addresses a jail may have at most per address family (deprecated)");
4010 * Default parameters for jail(2) compatibility. For historical reasons,
4011 * the sysctl names have varying similarity to the parameter names. Prisons
4012 * just see their own parameters, and can't change them.
4015 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4019 /* Get the current flag value, and convert it to a boolean. */
4020 if (req->td->td_ucred->cr_prison == &prison0) {
4021 mtx_lock(&prison0.pr_mtx);
4022 i = (jail_default_allow & arg2) != 0;
4023 mtx_unlock(&prison0.pr_mtx);
4025 i = prison_allow(req->td->td_ucred, arg2);
4029 error = sysctl_handle_int(oidp, &i, 0, req);
4030 if (error || !req->newptr)
4036 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4039 mtx_lock(&prison0.pr_mtx);
4040 jail_default_allow = (jail_default_allow & ~arg2) | i;
4041 mtx_unlock(&prison0.pr_mtx);
4045 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4046 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4047 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4048 "Processes in jail can set their hostnames (deprecated)");
4049 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4050 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4051 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4052 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4053 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4054 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4055 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4056 "Processes in jail can use System V IPC primitives (deprecated)");
4057 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4058 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4059 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4060 "Prison root can create raw sockets (deprecated)");
4061 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4062 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4063 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4064 "Processes in jail can alter system file flags (deprecated)");
4065 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4066 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4067 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4068 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4071 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4076 pr = req->td->td_ucred->cr_prison;
4077 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4078 error = sysctl_handle_int(oidp, &level, 0, req);
4079 if (error || !req->newptr)
4081 *(int *)arg1 = level;
4085 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4086 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4087 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4088 sysctl_jail_default_level, "I",
4089 "Processes in jail cannot see all mounted file systems (deprecated)");
4091 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4092 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4093 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4094 sysctl_jail_default_level, "I",
4095 "Ruleset for the devfs filesystem in jail (deprecated)");
4098 * Nodes to describe jail parameters. Maximum length of string parameters
4099 * is returned in the string itself, and the other parameters exist merely
4100 * to make themselves and their types known.
4102 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4106 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4113 switch (oidp->oid_kind & CTLTYPE)
4119 if (!(req->flags & SCTL_MASK32))
4121 return (SYSCTL_OUT(req, &l, sizeof(l)));
4125 return (SYSCTL_OUT(req, &i, sizeof(i)));
4126 case CTLTYPE_STRING:
4127 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4129 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4130 case CTLTYPE_STRUCT:
4132 return (SYSCTL_OUT(req, &s, sizeof(s)));
4138 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4139 * jail creation time but cannot be changed in an existing jail.
4141 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4142 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4143 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4144 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4145 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4146 "I", "Jail secure level");
4147 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4148 "Jail value for kern.osreldate and uname -K");
4149 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4150 "Jail value for kern.osrelease and uname -r");
4151 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4152 "I", "Jail cannot see all mounted file systems");
4153 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4154 "I", "Ruleset for in-jail devfs mounts");
4155 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4156 "B", "Jail persistence");
4158 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4159 "E,jailsys", "Virtual network stack");
4161 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4162 "B", "Jail is in the process of shutting down");
4164 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4165 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4166 "I", "Current number of child jails");
4167 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4168 "I", "Maximum number of child jails");
4170 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4171 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4173 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4174 "Jail NIS domainname");
4175 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4177 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4178 "LU", "Jail host ID");
4180 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4181 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4184 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4185 "Jail IPv4 address virtualization");
4186 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4187 "S,in_addr,a", "Jail IPv4 addresses");
4188 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4189 "B", "Do (not) use IPv4 source address selection rather than the "
4190 "primary jail IPv4 address.");
4193 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4194 "Jail IPv6 address virtualization");
4195 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4196 "S,in6_addr,a", "Jail IPv6 addresses");
4197 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4198 "B", "Do (not) use IPv6 source address selection rather than the "
4199 "primary jail IPv6 address.");
4202 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4203 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4204 "B", "Jail may set hostname");
4205 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4206 "B", "Jail may use SYSV IPC");
4207 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4208 "B", "Jail may create raw sockets");
4209 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4210 "B", "Jail may alter system file flags");
4211 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4212 "B", "Jail may set file quotas");
4213 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4214 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4215 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4216 "B", "Jail may lock (unlock) physical pages in memory");
4217 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4218 "B", "Jail may bind sockets to reserved ports");
4219 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4220 "B", "Jail may read the kernel message buffer");
4221 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4222 "B", "Unprivileged processes may use process debugging facilities");
4223 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4224 "B", "Processes in jail with uid 0 have privilege");
4226 SYSCTL_JAIL_PARAM(_allow, nfsd, CTLTYPE_INT | CTLFLAG_RW,
4227 "B", "Mountd/nfsd may run in the jail");
4230 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4231 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4232 "B", "Jail may mount/unmount jail-friendly file systems in general");
4235 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4236 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4240 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4243 struct bool_flags *bf;
4244 struct sysctl_oid *parent;
4245 char *allow_name, *allow_noname, *allowed;
4246 #ifndef NO_SYSCTL_DESCR
4247 char *descr_deprecated;
4252 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4254 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4256 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4257 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4258 free(allow_name, M_PRISON);
4263 * See if this parameter has already beed added, i.e. a module was
4264 * previously loaded/unloaded.
4266 mtx_lock(&prison0.pr_mtx);
4267 for (bf = pr_flag_allow;
4268 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4269 atomic_load_int(&bf->flag) != 0;
4271 if (strcmp(bf->name, allow_name) == 0) {
4272 allow_flag = bf->flag;
4278 * Find a free bit in pr_allow_all, failing if there are none
4279 * (which shouldn't happen as long as we keep track of how many
4280 * potential dynamic flags exist).
4282 for (allow_flag = 1;; allow_flag <<= 1) {
4283 if (allow_flag == 0)
4285 if ((pr_allow_all & allow_flag) == 0)
4289 /* Note the parameter in the next open slot in pr_flag_allow. */
4290 for (bf = pr_flag_allow; ; bf++) {
4291 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4292 /* This should never happen, but is not fatal. */
4296 if (atomic_load_int(&bf->flag) == 0)
4299 bf->name = allow_name;
4300 bf->noname = allow_noname;
4301 pr_allow_all |= allow_flag;
4303 * prison0 always has permission for the new parameter.
4304 * Other jails must have it granted to them.
4306 prison0.pr_allow |= allow_flag;
4307 /* The flag indicates a valid entry, so make sure it is set last. */
4308 atomic_store_rel_int(&bf->flag, allow_flag);
4309 mtx_unlock(&prison0.pr_mtx);
4312 * Create sysctls for the parameter, and the back-compat global
4316 ? SYSCTL_ADD_NODE(NULL,
4317 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4318 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4319 : &sysctl___security_jail_param_allow;
4320 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4321 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4322 NULL, 0, sysctl_jail_param, "B", descr);
4324 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4325 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4326 #ifndef NO_SYSCTL_DESCR
4327 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4330 (void)SYSCTL_ADD_PROC(NULL,
4331 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4332 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4333 sysctl_jail_default_allow, "I", descr_deprecated);
4334 #ifndef NO_SYSCTL_DESCR
4335 free(descr_deprecated, M_TEMP);
4337 free(allowed, M_TEMP);
4342 mtx_unlock(&prison0.pr_mtx);
4343 free(allow_name, M_PRISON);
4344 free(allow_noname, M_PRISON);
4349 * The VFS system will register jail-aware filesystems here. They each get
4350 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4351 * attempts to mount.
4354 prison_add_vfs(struct vfsconf *vfsp)
4356 #ifdef NO_SYSCTL_DESCR
4358 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4363 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4365 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4367 free(descr, M_TEMP);
4373 prison_racct_foreach(void (*callback)(struct racct *racct,
4374 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4375 void *arg2, void *arg3)
4377 struct prison_racct *prr;
4379 ASSERT_RACCT_ENABLED();
4381 sx_slock(&allprison_lock);
4384 LIST_FOREACH(prr, &allprison_racct, prr_next)
4385 (callback)(prr->prr_racct, arg2, arg3);
4388 sx_sunlock(&allprison_lock);
4391 static struct prison_racct *
4392 prison_racct_find_locked(const char *name)
4394 struct prison_racct *prr;
4396 ASSERT_RACCT_ENABLED();
4397 sx_assert(&allprison_lock, SA_XLOCKED);
4399 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4402 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4403 if (strcmp(name, prr->prr_name) != 0)
4406 /* Found prison_racct with a matching name? */
4407 prison_racct_hold(prr);
4411 /* Add new prison_racct. */
4412 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4413 racct_create(&prr->prr_racct);
4415 strcpy(prr->prr_name, name);
4416 refcount_init(&prr->prr_refcount, 1);
4417 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4422 struct prison_racct *
4423 prison_racct_find(const char *name)
4425 struct prison_racct *prr;
4427 ASSERT_RACCT_ENABLED();
4429 sx_xlock(&allprison_lock);
4430 prr = prison_racct_find_locked(name);
4431 sx_xunlock(&allprison_lock);
4436 prison_racct_hold(struct prison_racct *prr)
4439 ASSERT_RACCT_ENABLED();
4441 refcount_acquire(&prr->prr_refcount);
4445 prison_racct_free_locked(struct prison_racct *prr)
4448 ASSERT_RACCT_ENABLED();
4449 sx_assert(&allprison_lock, SA_XLOCKED);
4451 if (refcount_release(&prr->prr_refcount)) {
4452 racct_destroy(&prr->prr_racct);
4453 LIST_REMOVE(prr, prr_next);
4454 free(prr, M_PRISON_RACCT);
4459 prison_racct_free(struct prison_racct *prr)
4462 ASSERT_RACCT_ENABLED();
4463 sx_assert(&allprison_lock, SA_UNLOCKED);
4465 if (refcount_release_if_not_last(&prr->prr_refcount))
4468 sx_xlock(&allprison_lock);
4469 prison_racct_free_locked(prr);
4470 sx_xunlock(&allprison_lock);
4474 prison_racct_attach(struct prison *pr)
4476 struct prison_racct *prr;
4478 ASSERT_RACCT_ENABLED();
4479 sx_assert(&allprison_lock, SA_XLOCKED);
4481 prr = prison_racct_find_locked(pr->pr_name);
4482 KASSERT(prr != NULL, ("cannot find prison_racct"));
4484 pr->pr_prison_racct = prr;
4488 * Handle jail renaming. From the racct point of view, renaming means
4489 * moving from one prison_racct to another.
4492 prison_racct_modify(struct prison *pr)
4498 struct prison_racct *oldprr;
4500 ASSERT_RACCT_ENABLED();
4502 sx_slock(&allproc_lock);
4503 sx_xlock(&allprison_lock);
4505 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4506 sx_xunlock(&allprison_lock);
4507 sx_sunlock(&allproc_lock);
4511 oldprr = pr->pr_prison_racct;
4512 pr->pr_prison_racct = NULL;
4514 prison_racct_attach(pr);
4517 * Move resource utilisation records.
4519 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4523 * Force rctl to reattach rules to processes.
4525 FOREACH_PROC_IN_SYSTEM(p) {
4527 cred = crhold(p->p_ucred);
4529 rctl_proc_ucred_changed(p, cred);
4534 sx_sunlock(&allproc_lock);
4535 prison_racct_free_locked(oldprr);
4536 sx_xunlock(&allprison_lock);
4540 prison_racct_detach(struct prison *pr)
4543 ASSERT_RACCT_ENABLED();
4544 sx_assert(&allprison_lock, SA_UNLOCKED);
4546 if (pr->pr_prison_racct == NULL)
4548 prison_racct_free(pr->pr_prison_racct);
4549 pr->pr_prison_racct = NULL;
4556 db_show_prison(struct prison *pr)
4558 struct bool_flags *bf;
4559 struct jailsys_flags *jsf;
4560 #if defined(INET) || defined(INET6)
4565 char ip4buf[INET_ADDRSTRLEN];
4568 char ip6buf[INET6_ADDRSTRLEN];
4571 db_printf("prison %p:\n", pr);
4572 db_printf(" jid = %d\n", pr->pr_id);
4573 db_printf(" name = %s\n", pr->pr_name);
4574 db_printf(" parent = %p\n", pr->pr_parent);
4575 db_printf(" ref = %d\n", pr->pr_ref);
4576 db_printf(" uref = %d\n", pr->pr_uref);
4577 db_printf(" state = %s\n",
4578 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4579 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4581 db_printf(" path = %s\n", pr->pr_path);
4582 db_printf(" cpuset = %d\n", pr->pr_cpuset
4583 ? pr->pr_cpuset->cs_id : -1);
4585 db_printf(" vnet = %p\n", pr->pr_vnet);
4587 db_printf(" root = %p\n", pr->pr_root);
4588 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4589 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4590 db_printf(" children.max = %d\n", pr->pr_childmax);
4591 db_printf(" children.cur = %d\n", pr->pr_childcount);
4592 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4593 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4594 db_printf(" flags = 0x%x", pr->pr_flags);
4595 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4596 if (pr->pr_flags & bf->flag)
4597 db_printf(" %s", bf->name);
4598 for (jsf = pr_flag_jailsys;
4599 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4601 f = pr->pr_flags & (jsf->disable | jsf->new);
4602 db_printf(" %-16s= %s\n", jsf->name,
4603 (f != 0 && f == jsf->disable) ? "disable"
4604 : (f == jsf->new) ? "new"
4607 db_printf(" allow = 0x%x", pr->pr_allow);
4608 for (bf = pr_flag_allow;
4609 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4610 atomic_load_int(&bf->flag) != 0;
4612 if (pr->pr_allow & bf->flag)
4613 db_printf(" %s", bf->name);
4615 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4616 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4617 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4618 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4619 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4621 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4622 for (ii = 0; ii < pr->pr_ip4s; ii++)
4623 db_printf(" %s %s\n",
4624 ii == 0 ? "ip4.addr =" : " ",
4625 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4628 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4629 for (ii = 0; ii < pr->pr_ip6s; ii++)
4630 db_printf(" %s %s\n",
4631 ii == 0 ? "ip6.addr =" : " ",
4632 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4636 DB_SHOW_COMMAND(prison, db_show_prison_command)
4642 * Show all prisons in the list, and prison0 which is not
4645 db_show_prison(&prison0);
4646 if (!db_pager_quit) {
4647 TAILQ_FOREACH(pr, &allprison, pr_list) {
4659 /* Look for a prison with the ID and with references. */
4660 TAILQ_FOREACH(pr, &allprison, pr_list)
4661 if (pr->pr_id == addr && pr->pr_ref > 0)
4664 /* Look again, without requiring a reference. */
4665 TAILQ_FOREACH(pr, &allprison, pr_list)
4666 if (pr->pr_id == addr)
4669 /* Assume address points to a valid prison. */
4670 pr = (struct prison *)addr;