2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Poul-Henning Kamp.
5 * Copyright (c) 2008 Bjoern A. Zeeb.
6 * Copyright (c) 2009 James Gritton.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
36 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/kernel.h>
41 #include <sys/systm.h>
42 #include <sys/errno.h>
43 #include <sys/sysproto.h>
44 #include <sys/malloc.h>
48 #include <sys/taskqueue.h>
49 #include <sys/fcntl.h>
51 #include <sys/linker.h>
53 #include <sys/mutex.h>
54 #include <sys/racct.h>
56 #include <sys/refcount.h>
58 #include <sys/sysent.h>
59 #include <sys/namei.h>
60 #include <sys/mount.h>
61 #include <sys/queue.h>
62 #include <sys/socket.h>
63 #include <sys/syscallsubr.h>
64 #include <sys/sysctl.h>
66 #include <sys/vnode.h>
71 #include <netinet/in.h>
77 #include <security/mac/mac_framework.h>
79 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
80 #define PRISON0_HOSTUUID_MODULE "hostuuid"
82 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
83 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
85 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
88 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
90 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
94 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
96 #define _PR_IP_SADDRSEL 0
100 /* prison0 describes what is "real" about the system. */
101 struct prison prison0 = {
107 .pr_securelevel = -1,
109 .pr_state = PRISON_STATE_ALIVE,
110 .pr_childmax = JAIL_MAX,
111 .pr_hostuuid = DEFAULT_HOSTUUID,
112 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
114 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
116 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
118 .pr_allow = PR_ALLOW_ALL_STATIC,
120 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
127 struct jailsys_flags {
133 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
134 struct sx allprison_lock;
135 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
136 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
137 LIST_HEAD(, prison_racct) allprison_racct;
140 static int get_next_prid(struct prison **insprp);
141 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
142 static void prison_complete(void *context, int pending);
143 static void prison_deref(struct prison *pr, int flags);
144 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
145 static int prison_lock_xlock(struct prison *pr, int flags);
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 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
219 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
221 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
222 PR_ALLOW_RESERVED_PORTS | \
223 PR_ALLOW_UNPRIV_DEBUG | \
225 #define JAIL_DEFAULT_ENFORCE_STATFS 2
226 #define JAIL_DEFAULT_DEVFS_RSNUM 0
227 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
228 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
229 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
230 #if defined(INET) || defined(INET6)
231 static unsigned jail_max_af_ips = 255;
235 * Initialize the parts of prison0 that can't be static-initialized with
236 * constants. This is called from proc0_init() after creating thread0 cpuset.
241 uint8_t *file, *data;
244 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
245 prison0.pr_osreldate = osreldate;
246 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
248 /* If we have a preloaded hostuuid, use it. */
249 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
251 data = preload_fetch_addr(file);
252 size = preload_fetch_size(file);
255 * The preloaded data may include trailing whitespace, almost
256 * certainly a newline; skip over any whitespace or
257 * non-printable characters to be safe.
259 while (size > 0 && data[size - 1] <= 0x20) {
262 if (validate_uuid(data, size, NULL, 0) == 0) {
263 (void)strlcpy(prison0.pr_hostuuid, data,
265 } else if (bootverbose) {
266 printf("hostuuid: preload data malformed: '%s'",
272 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
281 sys_jail(struct thread *td, struct jail_args *uap)
287 error = copyin(uap->jail, &version, sizeof(uint32_t));
296 /* FreeBSD single IPv4 jails. */
297 bzero(&j, sizeof(struct jail));
298 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
301 j.version = j0.version;
303 j.hostname = j0.hostname;
304 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
310 * Version 1 was used by multi-IPv4 jail implementations
311 * that never made it into the official kernel.
315 case 2: /* JAIL_API_VERSION */
316 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
317 error = copyin(uap->jail, &j, sizeof(struct jail));
323 /* Sci-Fi jails are not supported, sorry. */
326 return (kern_jail(td, &j));
330 kern_jail(struct thread *td, struct jail *j)
332 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
341 char *u_path, *u_hostname, *u_name;
342 struct bool_flags *bf;
345 struct in_addr *u_ip4;
348 struct in6_addr *u_ip6;
351 int error, enforce_statfs;
353 bzero(&optiov, sizeof(optiov));
354 opt.uio_iov = optiov;
358 opt.uio_segflg = UIO_SYSSPACE;
359 opt.uio_rw = UIO_READ;
362 /* Set permissions for top-level jails from sysctls. */
363 if (!jailed(td->td_ucred)) {
364 for (bf = pr_flag_allow;
365 bf < pr_flag_allow + nitems(pr_flag_allow) &&
366 atomic_load_int(&bf->flag) != 0;
368 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
369 (jail_default_allow & bf->flag)
370 ? bf->name : bf->noname);
371 optiov[opt.uio_iovcnt].iov_len =
372 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
375 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
376 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
378 enforce_statfs = jail_default_enforce_statfs;
379 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
380 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
384 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
386 ip4s = (j->version == 0) ? 1 : j->ip4s;
387 if (ip4s > jail_max_af_ips)
389 tmplen += ip4s * sizeof(struct in_addr);
395 if (j->ip6s > jail_max_af_ips)
397 tmplen += j->ip6s * sizeof(struct in6_addr);
402 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
403 u_hostname = u_path + MAXPATHLEN;
404 u_name = u_hostname + MAXHOSTNAMELEN;
406 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
410 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
412 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
415 optiov[opt.uio_iovcnt].iov_base = "path";
416 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
418 optiov[opt.uio_iovcnt].iov_base = u_path;
419 error = copyinstr(j->path, u_path, MAXPATHLEN,
420 &optiov[opt.uio_iovcnt].iov_len);
422 free(u_path, M_TEMP);
426 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
427 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
429 optiov[opt.uio_iovcnt].iov_base = u_hostname;
430 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
431 &optiov[opt.uio_iovcnt].iov_len);
433 free(u_path, M_TEMP);
437 if (j->jailname != NULL) {
438 optiov[opt.uio_iovcnt].iov_base = "name";
439 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
441 optiov[opt.uio_iovcnt].iov_base = u_name;
442 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
443 &optiov[opt.uio_iovcnt].iov_len);
445 free(u_path, M_TEMP);
451 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
452 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
454 optiov[opt.uio_iovcnt].iov_base = u_ip4;
455 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
457 u_ip4->s_addr = j->ip4s;
459 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
461 free(u_path, M_TEMP);
468 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
469 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
471 optiov[opt.uio_iovcnt].iov_base = u_ip6;
472 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
473 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
475 free(u_path, M_TEMP);
480 KASSERT(opt.uio_iovcnt <= nitems(optiov),
481 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
482 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
483 free(u_path, M_TEMP);
488 * struct jail_set_args {
489 * struct iovec *iovp;
490 * unsigned int iovcnt;
495 sys_jail_set(struct thread *td, struct jail_set_args *uap)
500 /* Check that we have an even number of iovecs. */
504 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
507 error = kern_jail_set(td, auio, uap->flags);
513 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
520 struct in6_addr *ip6;
523 struct vfsoptlist *opts;
524 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
526 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
527 char *g_path, *osrelstr;
528 struct bool_flags *bf;
529 struct jailsys_flags *jsf;
530 #if defined(INET) || defined(INET6)
535 size_t namelen, onamelen, pnamelen;
536 int born, created, cuflags, descend, drflags, enforce;
537 int error, errmsg_len, errmsg_pos;
538 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
539 int jid, jsys, len, level;
540 int childmax, osreldt, rsnum, slevel;
541 #if defined(INET) || defined(INET6)
550 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
551 uint64_t pr_allow_diff;
555 error = priv_check(td, PRIV_JAIL_SET);
556 if (!error && (flags & JAIL_ATTACH))
557 error = priv_check(td, PRIV_JAIL_ATTACH);
560 mypr = td->td_ucred->cr_prison;
561 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
563 if (flags & ~JAIL_SET_MASK)
567 * Check all the parameters before committing to anything. Not all
568 * errors can be caught early, but we may as well try. Also, this
569 * takes care of some expensive stuff (path lookup) before getting
570 * the allprison lock.
572 * XXX Jails are not filesystems, and jail parameters are not mount
573 * options. But it makes more sense to re-use the vfsopt code
574 * than duplicate it under a different name.
576 error = vfs_buildopts(optuio, &opts);
587 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
590 vfs_opterror(opts, "no valid operation (create or update)");
594 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
600 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
609 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
617 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
622 else if (enforce < 0 || enforce > 2) {
628 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
636 pr_flags = ch_flags = 0;
637 for (bf = pr_flag_bool;
638 bf < pr_flag_bool + nitems(pr_flag_bool);
640 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
641 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
643 ch_flags |= pr_flags;
644 for (jsf = pr_flag_jailsys;
645 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
647 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
653 case JAIL_SYS_DISABLE:
658 pr_flags |= jsf->disable;
661 pr_flags |= jsf->new;
663 case JAIL_SYS_INHERIT:
669 ch_flags |= jsf->new | jsf->disable;
671 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
672 && !(pr_flags & PR_PERSIST)) {
674 vfs_opterror(opts, "new jail must persist or attach");
678 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
680 vfs_opterror(opts, "vnet cannot be changed after creation");
685 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
687 vfs_opterror(opts, "ip4 cannot be changed after creation");
692 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
694 vfs_opterror(opts, "ip6 cannot be changed after creation");
699 pr_allow = ch_allow = 0;
700 for (bf = pr_flag_allow;
701 bf < pr_flag_allow + nitems(pr_flag_allow) &&
702 atomic_load_int(&bf->flag) != 0;
704 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
705 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
707 ch_allow |= pr_allow;
709 error = vfs_getopt(opts, "name", (void **)&name, &len);
715 if (len == 0 || name[len - 1] != '\0') {
719 if (len > MAXHOSTNAMELEN) {
720 error = ENAMETOOLONG;
725 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
733 if (len == 0 || host[len - 1] != '\0') {
737 if (len > MAXHOSTNAMELEN) {
738 error = ENAMETOOLONG;
743 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
751 if (len == 0 || domain[len - 1] != '\0') {
755 if (len > MAXHOSTNAMELEN) {
756 error = ENAMETOOLONG;
761 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
769 if (len == 0 || uuid[len - 1] != '\0') {
773 if (len > HOSTUUIDLEN) {
774 error = ENAMETOOLONG;
779 #ifdef COMPAT_FREEBSD32
780 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
783 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
787 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
799 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
804 else if (ip4s & (sizeof(*ip4) - 1)) {
808 ch_flags |= PR_IP4_USER;
809 pr_flags |= PR_IP4_USER;
811 ip4s /= sizeof(*ip4);
812 if (ip4s > jail_max_af_ips) {
814 vfs_opterror(opts, "too many IPv4 addresses");
817 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
818 bcopy(op, ip4, ip4s * sizeof(*ip4));
820 * IP addresses are all sorted but ip[0] to preserve
821 * the primary IP address as given from userland.
822 * This special IP is used for unbound outgoing
823 * connections as well for "loopback" traffic in case
824 * source address selection cannot find any more fitting
825 * address to connect from.
828 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4),
831 * Check for duplicate addresses and do some simple
832 * zero and broadcast checks. If users give other bogus
833 * addresses it is their problem.
835 * We do not have to care about byte order for these
836 * checks so we will do them in NBO.
838 for (ii = 0; ii < ip4s; ii++) {
839 if (ip4[ii].s_addr == INADDR_ANY ||
840 ip4[ii].s_addr == INADDR_BROADCAST) {
845 (ip4[0].s_addr == ip4[ii+1].s_addr ||
846 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
856 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
861 else if (ip6s & (sizeof(*ip6) - 1)) {
865 ch_flags |= PR_IP6_USER;
866 pr_flags |= PR_IP6_USER;
868 ip6s /= sizeof(*ip6);
869 if (ip6s > jail_max_af_ips) {
871 vfs_opterror(opts, "too many IPv6 addresses");
874 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
875 bcopy(op, ip6, ip6s * sizeof(*ip6));
877 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6),
879 for (ii = 0; ii < ip6s; ii++) {
880 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
885 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
886 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
896 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
897 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
900 "vnet jails cannot have IP address restrictions");
905 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
911 if (flags & JAIL_UPDATE) {
914 "osrelease cannot be changed after creation");
917 if (len == 0 || osrelstr[len - 1] != '\0') {
921 if (len >= OSRELEASELEN) {
922 error = ENAMETOOLONG;
924 "osrelease string must be 1-%d bytes long",
930 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
936 if (flags & JAIL_UPDATE) {
939 "osreldate cannot be changed after creation");
944 vfs_opterror(opts, "osreldate cannot be 0");
950 error = vfs_getopt(opts, "path", (void **)&path, &len);
956 if (flags & JAIL_UPDATE) {
959 "path cannot be changed after creation");
962 if (len == 0 || path[len - 1] != '\0') {
966 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
972 NDFREE(&nd, NDF_ONLY_PNBUF);
973 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
974 strlcpy(g_path, path, MAXPATHLEN);
975 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
979 /* exit on other errors */
982 if (root->v_type != VDIR) {
991 * Find the specified jail, or at least its parent.
992 * This abuses the file error codes ENOENT and EEXIST.
996 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
997 namelc = strrchr(name, '.');
998 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1002 sx_xlock(&allprison_lock);
1003 drflags = PD_LIST_XLOCKED;
1005 if (!prison_isalive(ppr)) {
1006 /* This jail is dying. This process will surely follow. */
1013 vfs_opterror(opts, "negative jid");
1017 * See if a requested jid already exists. Keep track of
1018 * where it can be inserted later.
1020 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1021 if (inspr->pr_id < jid)
1023 if (inspr->pr_id > jid)
1026 mtx_lock(&pr->pr_mtx);
1027 drflags |= PD_LOCKED;
1032 /* Create: jid must not exist. */
1033 if (cuflags == JAIL_CREATE) {
1035 * Even creators that cannot see the jail will
1039 vfs_opterror(opts, "jail %d already exists",
1043 if (!prison_ischild(mypr, pr)) {
1045 * Updaters get ENOENT if they cannot see the
1046 * jail. This is true even for CREATE | UPDATE,
1047 * which normally cannot give this error.
1050 vfs_opterror(opts, "jail %d not found", jid);
1053 ppr = pr->pr_parent;
1054 if (!prison_isalive(ppr)) {
1056 vfs_opterror(opts, "jail %d is dying",
1060 if (!prison_isalive(pr)) {
1061 if (!(flags & JAIL_DYING)) {
1063 vfs_opterror(opts, "jail %d is dying",
1067 if ((flags & JAIL_ATTACH) ||
1068 (pr_flags & PR_PERSIST)) {
1070 * A dying jail might be resurrected
1071 * (via attach or persist), but first
1072 * it must determine if another jail
1073 * has claimed its name. Accomplish
1074 * this by implicitly re-setting the
1078 name = prison_name(mypr, pr);
1082 /* Update: jid must exist. */
1083 if (cuflags == JAIL_UPDATE) {
1085 vfs_opterror(opts, "jail %d not found", jid);
1091 * If the caller provided a name, look for a jail by that name.
1092 * This has different semantics for creates and updates keyed by jid
1093 * (where the name must not already exist in a different jail),
1094 * and updates keyed by the name itself (where the name must exist
1095 * because that is the jail being updated).
1099 namelc = strrchr(name, '.');
1104 * This is a hierarchical name. Split it into the
1105 * parent and child names, and make sure the parent
1106 * exists or matches an already found jail.
1109 if (strncmp(name, ppr->pr_name, namelc - name)
1110 || ppr->pr_name[namelc - name] != '\0') {
1113 "cannot change jail's parent");
1118 ppr = prison_find_name(mypr, name);
1122 "jail \"%s\" not found", name);
1125 mtx_unlock(&ppr->pr_mtx);
1126 if (!prison_isalive(ppr)) {
1129 "jail \"%s\" is dying", name);
1136 if (namelc[0] != '\0') {
1138 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1140 FOREACH_PRISON_CHILD(ppr, tpr) {
1142 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1143 if (prison_isalive(tpr)) {
1145 cuflags != JAIL_CREATE) {
1151 mtx_lock(&pr->pr_mtx);
1152 drflags |= PD_LOCKED;
1156 * Create, or update(jid):
1157 * name must not exist in an
1158 * active sibling jail.
1162 "jail \"%s\" already exists",
1167 cuflags != JAIL_CREATE) {
1172 /* If no active jail is found, use a dying one. */
1173 if (deadpr != NULL && pr == NULL) {
1174 if (flags & JAIL_DYING) {
1176 mtx_lock(&pr->pr_mtx);
1177 drflags |= PD_LOCKED;
1178 } else if (cuflags == JAIL_UPDATE) {
1181 "jail \"%s\" is dying", name);
1185 /* Update: name must exist if no jid. */
1186 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1188 vfs_opterror(opts, "jail \"%s\" not found",
1194 /* Update: must provide a jid or name. */
1195 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1197 vfs_opterror(opts, "update specified no jail");
1201 /* If there's no prison to update, create a new one and link it in. */
1202 created = pr == NULL;
1204 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1205 if (tpr->pr_childcount >= tpr->pr_childmax) {
1207 vfs_opterror(opts, "prison limit exceeded");
1210 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1212 vfs_opterror(opts, "no available jail IDs");
1216 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1217 pr->pr_state = PRISON_STATE_INVALID;
1218 refcount_init(&pr->pr_ref, 1);
1219 refcount_init(&pr->pr_uref, 0);
1220 drflags |= PD_DEREF;
1221 LIST_INIT(&pr->pr_children);
1222 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1223 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1227 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1229 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1231 pr->pr_parent = ppr;
1233 prison_proc_hold(ppr);
1234 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1235 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1236 tpr->pr_childcount++;
1238 /* Set some default values, and inherit some from the parent. */
1243 root = mypr->pr_root;
1246 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1247 pr->pr_flags |= PR_HOST;
1248 #if defined(INET) || defined(INET6)
1250 if (!(pr_flags & PR_VNET))
1254 if (!(ch_flags & PR_IP4_USER))
1255 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1256 else if (!(pr_flags & PR_IP4_USER)) {
1257 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1258 if (ppr->pr_ip4 != NULL) {
1259 pr->pr_ip4s = ppr->pr_ip4s;
1260 pr->pr_ip4 = malloc(pr->pr_ip4s *
1261 sizeof(struct in_addr), M_PRISON,
1263 bcopy(ppr->pr_ip4, pr->pr_ip4,
1264 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1269 if (!(ch_flags & PR_IP6_USER))
1270 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1271 else if (!(pr_flags & PR_IP6_USER)) {
1272 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1273 if (ppr->pr_ip6 != NULL) {
1274 pr->pr_ip6s = ppr->pr_ip6s;
1275 pr->pr_ip6 = malloc(pr->pr_ip6s *
1276 sizeof(struct in6_addr), M_PRISON,
1278 bcopy(ppr->pr_ip6, pr->pr_ip6,
1279 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1285 /* Source address selection is always on by default. */
1286 pr->pr_flags |= _PR_IP_SADDRSEL;
1288 pr->pr_securelevel = ppr->pr_securelevel;
1289 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1290 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1291 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1293 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1294 if (osrelstr == NULL)
1295 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1296 sizeof(pr->pr_osrelease));
1298 strlcpy(pr->pr_osrelease, osrelstr,
1299 sizeof(pr->pr_osrelease));
1302 /* Allocate a new vnet if specified. */
1303 pr->pr_vnet = (pr_flags & PR_VNET)
1304 ? vnet_alloc() : ppr->pr_vnet;
1307 * Allocate a dedicated cpuset for each jail.
1308 * Unlike other initial settings, this may return an erorr.
1310 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1314 mtx_lock(&pr->pr_mtx);
1315 drflags |= PD_LOCKED;
1318 * Grab a reference for existing prisons, to ensure they
1319 * continue to exist for the duration of the call.
1322 drflags |= PD_DEREF;
1323 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1324 if ((pr->pr_flags & PR_VNET) &&
1325 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1328 "vnet jails cannot have IP address restrictions");
1333 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1336 "ip4 cannot be changed after creation");
1341 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1344 "ip6 cannot be changed after creation");
1350 /* Do final error checking before setting anything. */
1352 if (slevel < ppr->pr_securelevel) {
1358 if (childmax >= ppr->pr_childmax) {
1364 if (enforce < ppr->pr_enforce_statfs) {
1371 * devfs_rsnum is a uint16_t
1373 if (rsnum < 0 || rsnum > 65535) {
1378 * Nested jails always inherit parent's devfs ruleset
1380 if (jailed(td->td_ucred)) {
1381 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1385 rsnum = ppr->pr_devfs_rsnum;
1390 if (ppr->pr_flags & PR_IP4) {
1392 * Make sure the new set of IP addresses is a
1393 * subset of the parent's list. Don't worry
1394 * about the parent being unlocked, as any
1395 * setting is done with allprison_lock held.
1397 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1398 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1400 if (ij == ppr->pr_ip4s) {
1405 for (ii = ij = 1; ii < ip4s; ii++) {
1406 if (ip4[ii].s_addr ==
1407 ppr->pr_ip4[0].s_addr)
1409 for (; ij < ppr->pr_ip4s; ij++)
1410 if (ip4[ii].s_addr ==
1411 ppr->pr_ip4[ij].s_addr)
1413 if (ij == ppr->pr_ip4s)
1416 if (ij == ppr->pr_ip4s) {
1423 * Check for conflicting IP addresses. We permit them
1424 * if there is no more than one IP on each jail. If
1425 * there is a duplicate on a jail with more than one
1426 * IP stop checking and return error.
1429 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1430 if (tppr->pr_flags & PR_VNET)
1435 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1438 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1440 !prison_isalive(tpr)) {
1444 if (!(tpr->pr_flags & PR_IP4_USER))
1447 if (tpr->pr_ip4 == NULL ||
1448 (ip4s == 1 && tpr->pr_ip4s == 1))
1450 for (ii = 0; ii < ip4s; ii++) {
1451 if (prison_check_ip4_locked(tpr, &ip4[ii]) ==
1455 "IPv4 addresses clash");
1464 if (ppr->pr_flags & PR_IP6) {
1466 * Make sure the new set of IP addresses is a
1467 * subset of the parent's list.
1469 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1470 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1473 if (ij == ppr->pr_ip6s) {
1478 for (ii = ij = 1; ii < ip6s; ii++) {
1479 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1482 for (; ij < ppr->pr_ip6s; ij++)
1483 if (IN6_ARE_ADDR_EQUAL(
1484 &ip6[ii], &ppr->pr_ip6[ij]))
1486 if (ij == ppr->pr_ip6s)
1489 if (ij == ppr->pr_ip6s) {
1495 /* Check for conflicting IP addresses. */
1497 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
1498 if (tppr->pr_flags & PR_VNET)
1503 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1506 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1508 !prison_isalive(tpr)) {
1512 if (!(tpr->pr_flags & PR_IP6_USER))
1515 if (tpr->pr_ip6 == NULL ||
1516 (ip6s == 1 && tpr->pr_ip6s == 1))
1518 for (ii = 0; ii < ip6s; ii++) {
1519 if (prison_check_ip6_locked(tpr, &ip6[ii]) ==
1523 "IPv6 addresses clash");
1530 onamelen = namelen = 0;
1531 if (namelc != NULL) {
1532 /* Give a default name of the jid. Also allow the name to be
1533 * explicitly the jid - but not any other number, and only in
1534 * normal form (no leading zero/etc).
1536 if (namelc[0] == '\0')
1537 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1538 else if ((strtoul(namelc, &p, 10) != jid ||
1539 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1542 "name cannot be numeric (unless it is the jid)");
1546 * Make sure the name isn't too long for the prison or its
1549 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1550 onamelen = strlen(pr->pr_name + pnamelen);
1551 namelen = strlen(namelc);
1552 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1553 error = ENAMETOOLONG;
1556 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1557 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1558 sizeof(pr->pr_name)) {
1559 error = ENAMETOOLONG;
1564 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1565 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1571 * Let modules check their parameters. This requires unlocking and
1572 * then re-locking the prison, but this is still a valid state as long
1573 * as allprison_lock remains xlocked.
1575 mtx_unlock(&pr->pr_mtx);
1576 drflags &= ~PD_LOCKED;
1577 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1580 mtx_lock(&pr->pr_mtx);
1581 drflags |= PD_LOCKED;
1583 /* At this point, all valid parameters should have been noted. */
1584 TAILQ_FOREACH(opt, opts, link) {
1585 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1587 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1592 /* Set the parameters of the prison. */
1595 if (pr_flags & PR_IP4_USER) {
1596 pr->pr_flags |= PR_IP4;
1597 free(pr->pr_ip4, M_PRISON);
1601 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1603 if (tpr->pr_flags & PR_VNET) {
1608 if (prison_restrict_ip4(tpr, NULL)) {
1617 if (pr_flags & PR_IP6_USER) {
1618 pr->pr_flags |= PR_IP6;
1619 free(pr->pr_ip6, M_PRISON);
1623 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1625 if (tpr->pr_flags & PR_VNET) {
1630 if (prison_restrict_ip6(tpr, NULL)) {
1638 pr->pr_securelevel = slevel;
1639 /* Set all child jails to be at least this level. */
1640 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1641 if (tpr->pr_securelevel < slevel)
1642 tpr->pr_securelevel = slevel;
1645 pr->pr_childmax = childmax;
1646 /* Set all child jails to under this limit. */
1647 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1648 if (tpr->pr_childmax > childmax - level)
1649 tpr->pr_childmax = childmax > level
1650 ? childmax - level : 0;
1653 pr->pr_enforce_statfs = enforce;
1654 /* Pass this restriction on to the children. */
1655 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1656 if (tpr->pr_enforce_statfs < enforce)
1657 tpr->pr_enforce_statfs = enforce;
1660 pr->pr_devfs_rsnum = rsnum;
1661 /* Pass this restriction on to the children. */
1662 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1663 tpr->pr_devfs_rsnum = rsnum;
1665 if (namelc != NULL) {
1666 if (ppr == &prison0)
1667 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1669 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1670 ppr->pr_name, namelc);
1671 /* Change this component of child names. */
1672 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1673 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1674 strlen(tpr->pr_name + onamelen) + 1);
1675 bcopy(pr->pr_name, tpr->pr_name, namelen);
1679 /* Try to keep a real-rooted full pathname. */
1680 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1684 if (PR_HOST & ch_flags & ~pr_flags) {
1685 if (pr->pr_flags & PR_HOST) {
1687 * Copy the parent's host info. As with pr_ip4 above,
1688 * the lack of a lock on the parent is not a problem;
1689 * it is always set with allprison_lock at least
1690 * shared, and is held exclusively here.
1692 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1693 sizeof(pr->pr_hostname));
1694 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1695 sizeof(pr->pr_domainname));
1696 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1697 sizeof(pr->pr_hostuuid));
1698 pr->pr_hostid = pr->pr_parent->pr_hostid;
1700 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1701 /* Set this prison, and any descendants without PR_HOST. */
1703 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1705 strlcpy(pr->pr_domainname, domain,
1706 sizeof(pr->pr_domainname));
1708 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1710 pr->pr_hostid = hid;
1711 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1712 if (tpr->pr_flags & PR_HOST)
1716 strlcpy(tpr->pr_hostname,
1718 sizeof(tpr->pr_hostname));
1720 strlcpy(tpr->pr_domainname,
1722 sizeof(tpr->pr_domainname));
1724 strlcpy(tpr->pr_hostuuid,
1726 sizeof(tpr->pr_hostuuid));
1728 tpr->pr_hostid = hid;
1732 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1733 if ((tallow = ch_allow & ~pr_allow))
1734 prison_set_allow_locked(pr, tallow, 0);
1736 * Persistent prisons get an extra reference, and prisons losing their
1737 * persist flag lose that reference.
1739 born = !prison_isalive(pr);
1740 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1741 if (pr_flags & PR_PERSIST) {
1744 * This may make a dead prison alive again, but wait
1745 * to label it as such until after OSD calls have had
1746 * a chance to run (and perhaps to fail).
1748 refcount_acquire(&pr->pr_uref);
1750 drflags |= PD_DEUREF;
1751 prison_free_not_last(pr);
1754 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1755 mtx_unlock(&pr->pr_mtx);
1756 drflags &= ~PD_LOCKED;
1758 * Any errors past this point will need to de-persist newly created
1759 * prisons, as well as call remove methods.
1765 if (racct_enable && created)
1766 prison_racct_attach(pr);
1769 /* Locks may have prevented a complete restriction of child IP
1770 * addresses. If so, allocate some more memory and try again.
1775 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1776 mtx_lock(&pr->pr_mtx);
1778 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1780 if (tpr->pr_flags & PR_VNET) {
1785 if (prison_restrict_ip4(tpr, ip4)) {
1792 mtx_unlock(&pr->pr_mtx);
1798 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1799 mtx_lock(&pr->pr_mtx);
1801 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1803 if (tpr->pr_flags & PR_VNET) {
1808 if (prison_restrict_ip6(tpr, ip6)) {
1815 mtx_unlock(&pr->pr_mtx);
1819 /* Let the modules do their work. */
1821 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1825 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1830 * A new prison is now ready to be seen; either it has gained a user
1831 * reference via persistence, or is about to gain one via attachment.
1834 drflags = prison_lock_xlock(pr, drflags);
1835 pr->pr_state = PRISON_STATE_ALIVE;
1838 /* Attach this process to the prison if requested. */
1839 if (flags & JAIL_ATTACH) {
1840 error = do_jail_attach(td, pr,
1841 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
1842 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
1844 vfs_opterror(opts, "attach failed");
1850 if (racct_enable && !created) {
1851 if (drflags & PD_LOCKED) {
1852 mtx_unlock(&pr->pr_mtx);
1853 drflags &= ~PD_LOCKED;
1855 if (drflags & PD_LIST_XLOCKED) {
1856 sx_xunlock(&allprison_lock);
1857 drflags &= ~PD_LIST_XLOCKED;
1859 prison_racct_modify(pr);
1863 drflags &= ~PD_KILL;
1864 td->td_retval[0] = pr->pr_id;
1867 /* Release any temporary prison holds and/or locks. */
1869 prison_deref(pr, drflags);
1870 else if (drflags & PD_LIST_SLOCKED)
1871 sx_sunlock(&allprison_lock);
1872 else if (drflags & PD_LIST_XLOCKED)
1873 sx_xunlock(&allprison_lock);
1878 /* Write the error message back to userspace. */
1879 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
1880 &errmsg_len) == 0 && errmsg_len > 0) {
1881 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1882 if (optuio->uio_segflg == UIO_SYSSPACE)
1884 optuio->uio_iov[errmsg_pos].iov_base,
1888 optuio->uio_iov[errmsg_pos].iov_base,
1894 free(ip4, M_PRISON);
1897 free(ip6, M_PRISON);
1900 free(g_path, M_TEMP);
1906 * Find the next available prison ID. Return the ID on success, or zero
1907 * on failure. Also set a pointer to the allprison list entry the prison
1908 * should be inserted before.
1911 get_next_prid(struct prison **insprp)
1913 struct prison *inspr;
1916 jid = lastprid % JAIL_MAX + 1;
1917 if (TAILQ_EMPTY(&allprison) ||
1918 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
1920 * A common case is for all jails to be implicitly numbered,
1921 * which means they'll go on the end of the list, at least
1922 * for the first JAIL_MAX times.
1927 * Take two passes through the allprison list: first starting
1928 * with the proposed jid, then ending with it.
1930 for (maxid = JAIL_MAX; maxid != 0; ) {
1931 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1932 if (inspr->pr_id < jid)
1934 if (inspr->pr_id > jid) {
1935 /* Found an opening. */
1939 if (++jid > maxid) {
1940 if (lastprid == maxid || lastprid == 0)
1943 * The entire legal range
1944 * has been traversed
1948 /* Try again from the start. */
1954 if (inspr == NULL) {
1955 /* Found room at the end of the list. */
1966 * struct jail_get_args {
1967 * struct iovec *iovp;
1968 * unsigned int iovcnt;
1973 sys_jail_get(struct thread *td, struct jail_get_args *uap)
1978 /* Check that we have an even number of iovecs. */
1979 if (uap->iovcnt & 1)
1982 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1985 error = kern_jail_get(td, auio, uap->flags);
1987 error = copyout(auio->uio_iov, uap->iovp,
1988 uap->iovcnt * sizeof (struct iovec));
1994 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1996 struct bool_flags *bf;
1997 struct jailsys_flags *jsf;
1998 struct prison *pr, *mypr;
2000 struct vfsoptlist *opts;
2001 char *errmsg, *name;
2002 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2005 if (flags & ~JAIL_GET_MASK)
2008 /* Get the parameter list. */
2009 error = vfs_buildopts(optuio, &opts);
2012 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2013 mypr = td->td_ucred->cr_prison;
2017 * Find the prison specified by one of: lastjid, jid, name.
2019 sx_slock(&allprison_lock);
2020 drflags = PD_LIST_SLOCKED;
2021 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2023 TAILQ_FOREACH(pr, &allprison, pr_list) {
2024 if (pr->pr_id > jid &&
2025 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2026 prison_ischild(mypr, pr)) {
2027 mtx_lock(&pr->pr_mtx);
2028 drflags |= PD_LOCKED;
2033 vfs_opterror(opts, "no jail after %d", jid);
2035 } else if (error != ENOENT)
2038 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2041 pr = prison_find_child(mypr, jid);
2043 drflags |= PD_LOCKED;
2044 if (!(prison_isalive(pr) ||
2045 (flags & JAIL_DYING))) {
2047 vfs_opterror(opts, "jail %d is dying",
2054 vfs_opterror(opts, "jail %d not found", jid);
2057 } else if (error != ENOENT)
2060 error = vfs_getopt(opts, "name", (void **)&name, &len);
2062 if (len == 0 || name[len - 1] != '\0') {
2066 pr = prison_find_name(mypr, name);
2068 drflags |= PD_LOCKED;
2069 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2071 vfs_opterror(opts, "jail \"%s\" is dying",
2078 vfs_opterror(opts, "jail \"%s\" not found", name);
2080 } else if (error != ENOENT)
2083 vfs_opterror(opts, "no jail specified");
2088 /* Get the parameters of the prison. */
2090 drflags |= PD_DEREF;
2091 td->td_retval[0] = pr->pr_id;
2092 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2093 if (error != 0 && error != ENOENT)
2095 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2096 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2097 if (error != 0 && error != ENOENT)
2099 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2100 if (error != 0 && error != ENOENT)
2102 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2103 sizeof(pr->pr_cpuset->cs_id));
2104 if (error != 0 && error != ENOENT)
2106 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2107 if (error != 0 && error != ENOENT)
2110 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
2111 pr->pr_ip4s * sizeof(*pr->pr_ip4));
2112 if (error != 0 && error != ENOENT)
2116 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
2117 pr->pr_ip6s * sizeof(*pr->pr_ip6));
2118 if (error != 0 && error != ENOENT)
2121 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2122 sizeof(pr->pr_securelevel));
2123 if (error != 0 && error != ENOENT)
2125 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2126 sizeof(pr->pr_childcount));
2127 if (error != 0 && error != ENOENT)
2129 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2130 sizeof(pr->pr_childmax));
2131 if (error != 0 && error != ENOENT)
2133 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2134 if (error != 0 && error != ENOENT)
2136 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2137 if (error != 0 && error != ENOENT)
2139 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2140 if (error != 0 && error != ENOENT)
2142 #ifdef COMPAT_FREEBSD32
2143 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2144 uint32_t hid32 = pr->pr_hostid;
2146 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2149 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2150 sizeof(pr->pr_hostid));
2151 if (error != 0 && error != ENOENT)
2153 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2154 sizeof(pr->pr_enforce_statfs));
2155 if (error != 0 && error != ENOENT)
2157 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2158 sizeof(pr->pr_devfs_rsnum));
2159 if (error != 0 && error != ENOENT)
2161 for (bf = pr_flag_bool;
2162 bf < pr_flag_bool + nitems(pr_flag_bool);
2164 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2165 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2166 if (error != 0 && error != ENOENT)
2169 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2170 if (error != 0 && error != ENOENT)
2173 for (jsf = pr_flag_jailsys;
2174 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2176 f = pr->pr_flags & (jsf->disable | jsf->new);
2177 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2178 : (f == jsf->new) ? JAIL_SYS_NEW
2180 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2181 if (error != 0 && error != ENOENT)
2184 for (bf = pr_flag_allow;
2185 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2186 atomic_load_int(&bf->flag) != 0;
2188 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2189 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2190 if (error != 0 && error != ENOENT)
2193 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2194 if (error != 0 && error != ENOENT)
2197 i = !prison_isalive(pr);
2198 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2199 if (error != 0 && error != ENOENT)
2202 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2203 if (error != 0 && error != ENOENT)
2205 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2206 sizeof(pr->pr_osreldate));
2207 if (error != 0 && error != ENOENT)
2209 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2210 if (error != 0 && error != ENOENT)
2213 /* Get the module parameters. */
2214 mtx_unlock(&pr->pr_mtx);
2215 drflags &= ~PD_LOCKED;
2216 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2219 prison_deref(pr, drflags);
2223 /* By now, all parameters should have been noted. */
2224 TAILQ_FOREACH(opt, opts, link) {
2225 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2227 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2232 /* Write the fetched parameters back to userspace. */
2234 TAILQ_FOREACH(opt, opts, link) {
2235 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2236 pos = 2 * opt->pos + 1;
2237 optuio->uio_iov[pos].iov_len = opt->len;
2238 if (opt->value != NULL) {
2239 if (optuio->uio_segflg == UIO_SYSSPACE) {
2241 optuio->uio_iov[pos].iov_base,
2244 error = copyout(opt->value,
2245 optuio->uio_iov[pos].iov_base,
2255 /* Release any temporary prison holds and/or locks. */
2257 prison_deref(pr, drflags);
2258 else if (drflags & PD_LIST_SLOCKED)
2259 sx_sunlock(&allprison_lock);
2260 if (error && errmsg_pos >= 0) {
2261 /* Write the error message back to userspace. */
2262 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2263 errmsg_pos = 2 * errmsg_pos + 1;
2264 if (errmsg_len > 0) {
2265 if (optuio->uio_segflg == UIO_SYSSPACE)
2267 optuio->uio_iov[errmsg_pos].iov_base,
2271 optuio->uio_iov[errmsg_pos].iov_base,
2280 * struct jail_remove_args {
2285 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2290 error = priv_check(td, PRIV_JAIL_REMOVE);
2294 sx_xlock(&allprison_lock);
2295 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2297 sx_xunlock(&allprison_lock);
2300 if (!prison_isalive(pr)) {
2301 /* Silently ignore already-dying prisons. */
2302 mtx_unlock(&pr->pr_mtx);
2303 sx_xunlock(&allprison_lock);
2306 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2311 * struct jail_attach_args {
2316 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2321 error = priv_check(td, PRIV_JAIL_ATTACH);
2325 sx_slock(&allprison_lock);
2326 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2328 sx_sunlock(&allprison_lock);
2332 /* Do not allow a process to attach to a prison that is not alive. */
2333 if (!prison_isalive(pr)) {
2334 mtx_unlock(&pr->pr_mtx);
2335 sx_sunlock(&allprison_lock);
2339 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2343 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2346 struct ucred *newcred, *oldcred;
2349 mtx_assert(&pr->pr_mtx, MA_OWNED);
2350 sx_assert(&allprison_lock, SX_LOCKED);
2351 drflags &= PD_LOCK_FLAGS;
2353 * XXX: Note that there is a slight race here if two threads
2354 * in the same privileged process attempt to attach to two
2355 * different jails at the same time. It is important for
2356 * user processes not to do this, or they might end up with
2357 * a process root from one prison, but attached to the jail
2361 refcount_acquire(&pr->pr_uref);
2362 drflags |= PD_DEREF | PD_DEUREF;
2363 mtx_unlock(&pr->pr_mtx);
2364 drflags &= ~PD_LOCKED;
2366 /* Let modules do whatever they need to prepare for attaching. */
2367 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2369 prison_deref(pr, drflags);
2372 sx_unlock(&allprison_lock);
2373 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2376 * Reparent the newly attached process to this jail.
2379 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2383 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2384 if ((error = change_dir(pr->pr_root, td)) != 0)
2387 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2390 VOP_UNLOCK(pr->pr_root);
2391 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2396 oldcred = crcopysafe(p, newcred);
2397 newcred->cr_prison = pr;
2398 proc_set_cred(p, newcred);
2401 racct_proc_ucred_changed(p, oldcred, newcred);
2406 rctl_proc_ucred_changed(p, newcred);
2409 prison_deref(oldcred->cr_prison, drflags);
2413 * If the prison was killed while changing credentials, die along
2416 if (!prison_isalive(pr)) {
2418 kern_psignal(p, SIGKILL);
2425 VOP_UNLOCK(pr->pr_root);
2427 /* Tell modules this thread is still in its old jail after all. */
2428 sx_slock(&allprison_lock);
2429 drflags |= PD_LIST_SLOCKED;
2430 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2431 prison_deref(pr, drflags);
2436 * Returns a locked prison instance, or NULL on failure.
2439 prison_find(int prid)
2443 sx_assert(&allprison_lock, SX_LOCKED);
2444 TAILQ_FOREACH(pr, &allprison, pr_list) {
2445 if (pr->pr_id < prid)
2447 if (pr->pr_id > prid)
2449 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2450 mtx_lock(&pr->pr_mtx);
2457 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2460 prison_find_child(struct prison *mypr, int prid)
2465 sx_assert(&allprison_lock, SX_LOCKED);
2466 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2467 if (pr->pr_id == prid) {
2468 KASSERT(prison_isvalid(pr),
2469 ("Found invalid prison %p", pr));
2470 mtx_lock(&pr->pr_mtx);
2478 * Look for the name relative to mypr. Returns a locked prison or NULL.
2481 prison_find_name(struct prison *mypr, const char *name)
2483 struct prison *pr, *deadpr;
2487 sx_assert(&allprison_lock, SX_LOCKED);
2488 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2490 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2491 if (!strcmp(pr->pr_name + mylen, name)) {
2492 KASSERT(prison_isvalid(pr),
2493 ("Found invalid prison %p", pr));
2494 if (prison_isalive(pr)) {
2495 mtx_lock(&pr->pr_mtx);
2501 /* There was no valid prison - perhaps there was a dying one. */
2503 mtx_lock(&deadpr->pr_mtx);
2508 * See if a prison has the specific flag set. The prison should be locked,
2509 * unless checking for flags that are only set at jail creation (such as
2510 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2511 * to any other prison data.
2514 prison_flag(struct ucred *cred, unsigned flag)
2517 return (cred->cr_prison->pr_flags & flag);
2521 prison_allow(struct ucred *cred, unsigned flag)
2524 return ((cred->cr_prison->pr_allow & flag) != 0);
2528 * Hold a prison reference, by incrementing pr_ref. It is generally
2529 * an error to hold a prison that does not already have a reference.
2530 * A prison record will remain valid as long as it has at least one
2531 * reference, and will not be removed as long as either the prison
2532 * mutex or the allprison lock is held (allprison_lock may be shared).
2535 prison_hold_locked(struct prison *pr)
2538 /* Locking is no longer required. */
2543 prison_hold(struct prison *pr)
2546 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2549 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2551 refcount_acquire(&pr->pr_ref);
2556 * Remove a prison reference. If that was the last reference, the
2557 * prison will be removed (at a later time).
2560 prison_free_locked(struct prison *pr)
2563 mtx_assert(&pr->pr_mtx, MA_OWNED);
2565 * Locking is no longer required, but unlock because the caller
2568 mtx_unlock(&pr->pr_mtx);
2573 prison_free(struct prison *pr)
2576 KASSERT(refcount_load(&pr->pr_ref) > 0,
2577 ("Trying to free dead prison %p (jid=%d).",
2579 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2581 * Don't remove the last reference in this context,
2582 * in case there are locks held.
2584 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2589 prison_free_not_last(struct prison *pr)
2594 KASSERT(refcount_load(&pr->pr_ref) > 0,
2595 ("Trying to free dead prison %p (jid=%d).",
2597 lastref = refcount_release(&pr->pr_ref);
2599 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2602 refcount_release(&pr->pr_ref);
2607 * Hold a a prison for user visibility, by incrementing pr_uref.
2608 * It is generally an error to hold a prison that isn't already
2609 * user-visible, except through the the jail system calls. It is also
2610 * an error to hold an invalid prison. A prison record will remain
2611 * alive as long as it has at least one user reference, and will not
2612 * be set to the dying state until the prison mutex and allprison_lock
2616 prison_proc_hold(struct prison *pr)
2619 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2622 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2624 refcount_acquire(&pr->pr_uref);
2629 * Remove a prison user reference. If it was the last reference, the
2630 * prison will be considered "dying", and may be removed once all of
2631 * its references are dropped.
2634 prison_proc_free(struct prison *pr)
2638 * Locking is only required when releasing the last reference.
2639 * This allows assurance that a locked prison will remain alive
2640 * until it is unlocked.
2642 KASSERT(refcount_load(&pr->pr_uref) > 0,
2643 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2644 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2646 * Don't remove the last user reference in this context,
2647 * which is expected to be a process that is not only locked,
2648 * but also half dead. Add a reference so any calls to
2649 * prison_free() won't re-submit the task.
2652 mtx_lock(&pr->pr_mtx);
2653 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2654 ("Redundant last reference in prison_proc_free (jid=%d)",
2656 pr->pr_flags |= PR_COMPLETE_PROC;
2657 mtx_unlock(&pr->pr_mtx);
2658 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2663 prison_proc_free_not_last(struct prison *pr)
2668 KASSERT(refcount_load(&pr->pr_uref) > 0,
2669 ("Trying to free dead prison %p (jid=%d).",
2671 lastref = refcount_release(&pr->pr_uref);
2673 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2676 refcount_release(&pr->pr_uref);
2681 * Complete a call to either prison_free or prison_proc_free.
2684 prison_complete(void *context, int pending)
2686 struct prison *pr = context;
2690 * This could be called to release the last reference, or the last
2691 * user reference (plus the reference held in prison_proc_free).
2693 drflags = prison_lock_xlock(pr, PD_DEREF);
2694 if (pr->pr_flags & PR_COMPLETE_PROC) {
2695 pr->pr_flags &= ~PR_COMPLETE_PROC;
2696 drflags |= PD_DEUREF;
2698 prison_deref(pr, drflags);
2702 * Remove a prison reference and/or user reference (usually).
2703 * This assumes context that allows sleeping (for allprison_lock),
2704 * with no non-sleeping locks held, except perhaps the prison itself.
2705 * If there are no more references, release and delist the prison.
2706 * On completion, the prison lock and the allprison lock are both
2710 prison_deref(struct prison *pr, int flags)
2712 struct prisonlist freeprison;
2713 struct prison *killpr, *rpr, *ppr, *tpr;
2717 TAILQ_INIT(&freeprison);
2719 * Release this prison as requested, which may cause its parent
2720 * to be released, and then maybe its grandparent, etc.
2723 if (flags & PD_KILL) {
2724 /* Kill the prison and its descendents. */
2725 KASSERT(pr != &prison0,
2726 ("prison_deref trying to kill prison0"));
2727 if (!(flags & PD_DEREF)) {
2731 flags = prison_lock_xlock(pr, flags);
2732 prison_deref_kill(pr, &freeprison);
2734 if (flags & PD_DEUREF) {
2735 /* Drop a user reference. */
2736 KASSERT(refcount_load(&pr->pr_uref) > 0,
2737 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2739 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2740 if (!(flags & PD_DEREF)) {
2744 flags = prison_lock_xlock(pr, flags);
2745 if (refcount_release(&pr->pr_uref) &&
2746 pr->pr_state == PRISON_STATE_ALIVE) {
2748 * When the last user references goes,
2749 * this becomes a dying prison.
2752 refcount_load(&prison0.pr_uref) > 0,
2753 ("prison0 pr_uref=0"));
2754 pr->pr_state = PRISON_STATE_DYING;
2755 mtx_unlock(&pr->pr_mtx);
2756 flags &= ~PD_LOCKED;
2757 (void)osd_jail_call(pr,
2758 PR_METHOD_REMOVE, NULL);
2762 if (flags & PD_KILL) {
2764 * Any remaining user references are probably processes
2765 * that need to be killed, either in this prison or its
2768 if (refcount_load(&pr->pr_uref) > 0)
2770 /* Make sure the parent prison doesn't get killed. */
2773 if (flags & PD_DEREF) {
2774 /* Drop a reference. */
2775 KASSERT(refcount_load(&pr->pr_ref) > 0,
2776 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
2778 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2779 flags = prison_lock_xlock(pr, flags);
2780 if (refcount_release(&pr->pr_ref)) {
2782 * When the last reference goes,
2783 * unlink the prison and set it aside.
2786 refcount_load(&pr->pr_uref) == 0,
2787 ("prison_deref: last ref, "
2788 "but still has %d urefs (jid=%d)",
2789 pr->pr_uref, pr->pr_id));
2791 refcount_load(&prison0.pr_ref) != 0,
2792 ("prison0 pr_ref=0"));
2793 pr->pr_state = PRISON_STATE_INVALID;
2794 TAILQ_REMOVE(&allprison, pr, pr_list);
2795 LIST_REMOVE(pr, pr_sibling);
2796 TAILQ_INSERT_TAIL(&freeprison, pr,
2798 for (ppr = pr->pr_parent;
2800 ppr = ppr->pr_parent)
2801 ppr->pr_childcount--;
2803 * Removing a prison frees references
2806 mtx_unlock(&pr->pr_mtx);
2807 flags &= ~PD_LOCKED;
2809 flags |= PD_DEREF | PD_DEUREF;
2817 /* Release all the prison locks. */
2818 if (flags & PD_LOCKED)
2819 mtx_unlock(&pr->pr_mtx);
2820 if (flags & PD_LIST_SLOCKED)
2821 sx_sunlock(&allprison_lock);
2822 else if (flags & PD_LIST_XLOCKED)
2823 sx_xunlock(&allprison_lock);
2825 /* Kill any processes attached to a killed prison. */
2826 if (killpr != NULL) {
2827 sx_slock(&allproc_lock);
2828 FOREACH_PROC_IN_SYSTEM(p) {
2830 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
2831 for (ppr = p->p_ucred->cr_prison;
2833 ppr = ppr->pr_parent)
2834 if (ppr == killpr) {
2835 kern_psignal(p, SIGKILL);
2841 sx_sunlock(&allproc_lock);
2845 * Finish removing any unreferenced prisons, which couldn't happen
2846 * while allprison_lock was held (to avoid a LOR on vrele).
2848 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
2850 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
2851 vnet_destroy(rpr->pr_vnet);
2853 if (rpr->pr_root != NULL)
2854 vrele(rpr->pr_root);
2855 mtx_destroy(&rpr->pr_mtx);
2857 free(rpr->pr_ip4, M_PRISON);
2860 free(rpr->pr_ip6, M_PRISON);
2862 if (rpr->pr_cpuset != NULL)
2863 cpuset_rel(rpr->pr_cpuset);
2867 prison_racct_detach(rpr);
2869 TAILQ_REMOVE(&freeprison, rpr, pr_list);
2870 free(rpr, M_PRISON);
2875 * Kill the prison and its descendants. Mark them as dying, clear the
2876 * persist flag, and call module remove methods.
2879 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
2881 struct prison *cpr, *ppr, *rpr;
2885 * Unlike the descendants, the target prison can be killed
2886 * even if it is currently dying. This is useful for failed
2887 * creation in jail_set(2).
2889 KASSERT(refcount_load(&pr->pr_ref) > 0,
2890 ("Trying to kill dead prison %p (jid=%d).",
2892 refcount_acquire(&pr->pr_uref);
2893 pr->pr_state = PRISON_STATE_DYING;
2894 mtx_unlock(&pr->pr_mtx);
2897 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
2899 if (!prison_isalive(cpr)) {
2904 prison_proc_hold(cpr);
2905 mtx_lock(&cpr->pr_mtx);
2906 cpr->pr_state = PRISON_STATE_DYING;
2907 cpr->pr_flags |= PR_REMOVE;
2908 mtx_unlock(&cpr->pr_mtx);
2911 if (!(cpr->pr_flags & PR_REMOVE))
2913 (void)osd_jail_call(cpr, PR_METHOD_REMOVE, NULL);
2914 mtx_lock(&cpr->pr_mtx);
2915 cpr->pr_flags &= ~PR_REMOVE;
2916 if (cpr->pr_flags & PR_PERSIST) {
2917 cpr->pr_flags &= ~PR_PERSIST;
2918 prison_proc_free_not_last(cpr);
2919 prison_free_not_last(cpr);
2921 (void)refcount_release(&cpr->pr_uref);
2922 if (refcount_release(&cpr->pr_ref)) {
2924 * When the last reference goes, unlink the prison
2925 * and set it aside for prison_deref() to handle.
2926 * Delay unlinking the sibling list to keep the loop
2930 LIST_REMOVE(rpr, pr_sibling);
2932 rpr->pr_state = PRISON_STATE_INVALID;
2933 TAILQ_REMOVE(&allprison, rpr, pr_list);
2934 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
2936 * Removing a prison frees references from its parent.
2938 ppr = rpr->pr_parent;
2939 prison_proc_free_not_last(ppr);
2940 prison_free_not_last(ppr);
2941 for (; ppr != NULL; ppr = ppr->pr_parent)
2942 ppr->pr_childcount--;
2944 mtx_unlock(&cpr->pr_mtx);
2947 LIST_REMOVE(rpr, pr_sibling);
2949 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
2950 mtx_lock(&pr->pr_mtx);
2951 if (pr->pr_flags & PR_PERSIST) {
2952 pr->pr_flags &= ~PR_PERSIST;
2953 prison_proc_free_not_last(pr);
2954 prison_free_not_last(pr);
2956 (void)refcount_release(&pr->pr_uref);
2960 * Given the current locking state in the flags, make sure allprison_lock
2961 * is held exclusive, and the prison is locked. Return flags indicating
2965 prison_lock_xlock(struct prison *pr, int flags)
2968 if (!(flags & PD_LIST_XLOCKED)) {
2970 * Get allprison_lock, which may be an upgrade,
2971 * and may require unlocking the prison.
2973 if (flags & PD_LOCKED) {
2974 mtx_unlock(&pr->pr_mtx);
2975 flags &= ~PD_LOCKED;
2977 if (flags & PD_LIST_SLOCKED) {
2978 if (!sx_try_upgrade(&allprison_lock)) {
2979 sx_sunlock(&allprison_lock);
2980 sx_xlock(&allprison_lock);
2982 flags &= ~PD_LIST_SLOCKED;
2984 sx_xlock(&allprison_lock);
2985 flags |= PD_LIST_XLOCKED;
2987 if (!(flags & PD_LOCKED)) {
2988 /* Lock the prison mutex. */
2989 mtx_lock(&pr->pr_mtx);
2996 * Set or clear a permission bit in the pr_allow field, passing restrictions
2997 * (cleared permission) down to child jails.
3000 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3004 pr = cred->cr_prison;
3005 sx_slock(&allprison_lock);
3006 mtx_lock(&pr->pr_mtx);
3007 prison_set_allow_locked(pr, flag, enable);
3008 mtx_unlock(&pr->pr_mtx);
3009 sx_sunlock(&allprison_lock);
3013 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3019 pr->pr_allow |= flag;
3021 pr->pr_allow &= ~flag;
3022 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3023 cpr->pr_allow &= ~flag;
3028 * Check if a jail supports the given address family.
3030 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3034 prison_check_af(struct ucred *cred, int af)
3039 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3041 pr = cred->cr_prison;
3043 /* Prisons with their own network stack are not limited. */
3044 if (prison_owns_vnet(cred))
3053 if (pr->pr_flags & PR_IP4)
3055 mtx_lock(&pr->pr_mtx);
3056 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3057 error = EAFNOSUPPORT;
3058 mtx_unlock(&pr->pr_mtx);
3064 if (pr->pr_flags & PR_IP6)
3066 mtx_lock(&pr->pr_mtx);
3067 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3068 error = EAFNOSUPPORT;
3069 mtx_unlock(&pr->pr_mtx);
3077 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3078 error = EAFNOSUPPORT;
3084 * Check if given address belongs to the jail referenced by cred (wrapper to
3085 * prison_check_ip[46]).
3087 * Returns 0 if jail doesn't restrict the address family or if address belongs
3088 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3089 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3092 prison_if(struct ucred *cred, const struct sockaddr *sa)
3095 const struct sockaddr_in *sai;
3098 const struct sockaddr_in6 *sai6;
3102 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3103 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3106 if (prison_owns_vnet(cred))
3111 switch (sa->sa_family)
3115 sai = (const struct sockaddr_in *)sa;
3116 error = prison_check_ip4(cred, &sai->sin_addr);
3121 sai6 = (const struct sockaddr_in6 *)sa;
3122 error = prison_check_ip6(cred, &sai6->sin6_addr);
3126 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3127 error = EAFNOSUPPORT;
3133 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3136 prison_check(struct ucred *cred1, struct ucred *cred2)
3139 return ((cred1->cr_prison == cred2->cr_prison ||
3140 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3144 * Return 1 if p2 is a child of p1, otherwise 0.
3147 prison_ischild(struct prison *pr1, struct prison *pr2)
3150 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3157 * Return true if the prison is currently alive. A prison is alive if it
3158 * holds user references and it isn't being removed.
3161 prison_isalive(struct prison *pr)
3164 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3170 * Return true if the prison is currently valid. A prison is valid if it has
3171 * been fully created, and is not being destroyed. Note that dying prisons
3172 * are still considered valid. Invalid prisons won't be found under normal
3173 * circumstances, as they're only put in that state by functions that have
3174 * an exclusive hold on allprison_lock.
3177 prison_isvalid(struct prison *pr)
3180 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3182 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3188 * Return 1 if the passed credential is in a jail and that jail does not
3189 * have its own virtual network stack, otherwise 0.
3192 jailed_without_vnet(struct ucred *cred)
3198 if (prison_owns_vnet(cred))
3206 * Return the correct hostname (domainname, et al) for the passed credential.
3209 getcredhostname(struct ucred *cred, char *buf, size_t size)
3214 * A NULL credential can be used to shortcut to the physical
3215 * system's hostname.
3217 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3218 mtx_lock(&pr->pr_mtx);
3219 strlcpy(buf, pr->pr_hostname, size);
3220 mtx_unlock(&pr->pr_mtx);
3224 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3227 mtx_lock(&cred->cr_prison->pr_mtx);
3228 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3229 mtx_unlock(&cred->cr_prison->pr_mtx);
3233 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3236 mtx_lock(&cred->cr_prison->pr_mtx);
3237 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3238 mtx_unlock(&cred->cr_prison->pr_mtx);
3242 getcredhostid(struct ucred *cred, unsigned long *hostid)
3245 mtx_lock(&cred->cr_prison->pr_mtx);
3246 *hostid = cred->cr_prison->pr_hostid;
3247 mtx_unlock(&cred->cr_prison->pr_mtx);
3251 getjailname(struct ucred *cred, char *name, size_t len)
3254 mtx_lock(&cred->cr_prison->pr_mtx);
3255 strlcpy(name, cred->cr_prison->pr_name, len);
3256 mtx_unlock(&cred->cr_prison->pr_mtx);
3261 * Determine whether the prison represented by cred owns
3262 * its vnet rather than having it inherited.
3264 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3267 prison_owns_vnet(struct ucred *cred)
3271 * vnets cannot be added/removed after jail creation,
3272 * so no need to lock here.
3274 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3279 * Determine whether the subject represented by cred can "see"
3280 * status of a mount point.
3281 * Returns: 0 for permitted, ENOENT otherwise.
3282 * XXX: This function should be called cr_canseemount() and should be
3283 * placed in kern_prot.c.
3286 prison_canseemount(struct ucred *cred, struct mount *mp)
3292 pr = cred->cr_prison;
3293 if (pr->pr_enforce_statfs == 0)
3295 if (pr->pr_root->v_mount == mp)
3297 if (pr->pr_enforce_statfs == 2)
3300 * If jail's chroot directory is set to "/" we should be able to see
3301 * all mount-points from inside a jail.
3302 * This is ugly check, but this is the only situation when jail's
3303 * directory ends with '/'.
3305 if (strcmp(pr->pr_path, "/") == 0)
3307 len = strlen(pr->pr_path);
3309 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3312 * Be sure that we don't have situation where jail's root directory
3313 * is "/some/path" and mount point is "/some/pathpath".
3315 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3321 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3323 char jpath[MAXPATHLEN];
3327 pr = cred->cr_prison;
3328 if (pr->pr_enforce_statfs == 0)
3330 if (prison_canseemount(cred, mp) != 0) {
3331 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3332 strlcpy(sp->f_mntonname, "[restricted]",
3333 sizeof(sp->f_mntonname));
3336 if (pr->pr_root->v_mount == mp) {
3338 * Clear current buffer data, so we are sure nothing from
3339 * the valid path left there.
3341 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3342 *sp->f_mntonname = '/';
3346 * If jail's chroot directory is set to "/" we should be able to see
3347 * all mount-points from inside a jail.
3349 if (strcmp(pr->pr_path, "/") == 0)
3351 len = strlen(pr->pr_path);
3352 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3354 * Clear current buffer data, so we are sure nothing from
3355 * the valid path left there.
3357 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3358 if (*jpath == '\0') {
3359 /* Should never happen. */
3360 *sp->f_mntonname = '/';
3362 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3367 * Check with permission for a specific privilege is granted within jail. We
3368 * have a specific list of accepted privileges; the rest are denied.
3371 prison_priv_check(struct ucred *cred, int priv)
3377 * Some policies have custom handlers. This routine should not be
3378 * called for them. See priv_check_cred().
3381 case PRIV_VFS_LOOKUP:
3382 case PRIV_VFS_GENERATION:
3383 KASSERT(0, ("prison_priv_check instead of a custom handler "
3384 "called for %d\n", priv));
3392 * Privileges specific to prisons with a virtual network stack.
3393 * There might be a duplicate entry here in case the privilege
3394 * is only granted conditionally in the legacy jail case.
3399 * NFS-specific privileges.
3401 case PRIV_NFS_DAEMON:
3402 case PRIV_NFS_LOCKD:
3405 * Network stack privileges.
3407 case PRIV_NET_BRIDGE:
3410 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3411 case PRIV_NET_ROUTE:
3413 case PRIV_NET_SETIFMTU:
3414 case PRIV_NET_SETIFFLAGS:
3415 case PRIV_NET_SETIFCAP:
3416 case PRIV_NET_SETIFDESCR:
3417 case PRIV_NET_SETIFNAME :
3418 case PRIV_NET_SETIFMETRIC:
3419 case PRIV_NET_SETIFPHYS:
3420 case PRIV_NET_SETIFMAC:
3421 case PRIV_NET_SETLANPCP:
3422 case PRIV_NET_ADDMULTI:
3423 case PRIV_NET_DELMULTI:
3424 case PRIV_NET_HWIOCTL:
3425 case PRIV_NET_SETLLADDR:
3426 case PRIV_NET_ADDIFGROUP:
3427 case PRIV_NET_DELIFGROUP:
3428 case PRIV_NET_IFCREATE:
3429 case PRIV_NET_IFDESTROY:
3430 case PRIV_NET_ADDIFADDR:
3431 case PRIV_NET_DELIFADDR:
3434 case PRIV_NET_SETIFVNET:
3435 case PRIV_NET_SETIFFIB:
3438 * 802.11-related privileges.
3440 case PRIV_NET80211_VAP_GETKEY:
3441 case PRIV_NET80211_VAP_MANAGE:
3447 case PRIV_NETATM_CFG:
3448 case PRIV_NETATM_ADD:
3449 case PRIV_NETATM_DEL:
3450 case PRIV_NETATM_SET:
3453 * Bluetooth privileges.
3455 case PRIV_NETBLUETOOTH_RAW:
3459 * Netgraph and netgraph module privileges.
3461 case PRIV_NETGRAPH_CONTROL:
3463 case PRIV_NETGRAPH_TTY:
3467 * IPv4 and IPv6 privileges.
3469 case PRIV_NETINET_IPFW:
3470 case PRIV_NETINET_DIVERT:
3471 case PRIV_NETINET_PF:
3472 case PRIV_NETINET_DUMMYNET:
3473 case PRIV_NETINET_CARP:
3474 case PRIV_NETINET_MROUTE:
3475 case PRIV_NETINET_RAW:
3476 case PRIV_NETINET_ADDRCTRL6:
3477 case PRIV_NETINET_ND6:
3478 case PRIV_NETINET_SCOPE6:
3479 case PRIV_NETINET_ALIFETIME6:
3480 case PRIV_NETINET_IPSEC:
3481 case PRIV_NETINET_BINDANY:
3496 * No default: or deny here.
3497 * In case of no permit fall through to next switch().
3499 if (cred->cr_prison->pr_flags & PR_VNET)
3506 * Allow ktrace privileges for root in jail.
3512 * Allow jailed processes to configure audit identity and
3513 * submit audit records (login, etc). In the future we may
3514 * want to further refine the relationship between audit and
3517 case PRIV_AUDIT_GETAUDIT:
3518 case PRIV_AUDIT_SETAUDIT:
3519 case PRIV_AUDIT_SUBMIT:
3523 * Allow jailed processes to manipulate process UNIX
3524 * credentials in any way they see fit.
3526 case PRIV_CRED_SETUID:
3527 case PRIV_CRED_SETEUID:
3528 case PRIV_CRED_SETGID:
3529 case PRIV_CRED_SETEGID:
3530 case PRIV_CRED_SETGROUPS:
3531 case PRIV_CRED_SETREUID:
3532 case PRIV_CRED_SETREGID:
3533 case PRIV_CRED_SETRESUID:
3534 case PRIV_CRED_SETRESGID:
3537 * Jail implements visibility constraints already, so allow
3538 * jailed root to override uid/gid-based constraints.
3540 case PRIV_SEEOTHERGIDS:
3541 case PRIV_SEEOTHERUIDS:
3544 * Jail implements inter-process debugging limits already, so
3545 * allow jailed root various debugging privileges.
3547 case PRIV_DEBUG_DIFFCRED:
3548 case PRIV_DEBUG_SUGID:
3549 case PRIV_DEBUG_UNPRIV:
3552 * Allow jail to set various resource limits and login
3553 * properties, and for now, exceed process resource limits.
3555 case PRIV_PROC_LIMIT:
3556 case PRIV_PROC_SETLOGIN:
3557 case PRIV_PROC_SETRLIMIT:
3560 * System V and POSIX IPC privileges are granted in jail.
3563 case PRIV_IPC_WRITE:
3564 case PRIV_IPC_ADMIN:
3565 case PRIV_IPC_MSGSIZE:
3569 * Jail operations within a jail work on child jails.
3571 case PRIV_JAIL_ATTACH:
3573 case PRIV_JAIL_REMOVE:
3576 * Jail implements its own inter-process limits, so allow
3577 * root processes in jail to change scheduling on other
3578 * processes in the same jail. Likewise for signalling.
3580 case PRIV_SCHED_DIFFCRED:
3581 case PRIV_SCHED_CPUSET:
3582 case PRIV_SIGNAL_DIFFCRED:
3583 case PRIV_SIGNAL_SUGID:
3586 * Allow jailed processes to write to sysctls marked as jail
3589 case PRIV_SYSCTL_WRITEJAIL:
3592 * Allow root in jail to manage a variety of quota
3593 * properties. These should likely be conditional on a
3594 * configuration option.
3596 case PRIV_VFS_GETQUOTA:
3597 case PRIV_VFS_SETQUOTA:
3600 * Since Jail relies on chroot() to implement file system
3601 * protections, grant many VFS privileges to root in jail.
3602 * Be careful to exclude mount-related and NFS-related
3606 case PRIV_VFS_WRITE:
3607 case PRIV_VFS_ADMIN:
3609 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3610 case PRIV_VFS_CHFLAGS_DEV:
3611 case PRIV_VFS_CHOWN:
3612 case PRIV_VFS_CHROOT:
3613 case PRIV_VFS_RETAINSUGID:
3614 case PRIV_VFS_FCHROOT:
3616 case PRIV_VFS_SETGID:
3618 case PRIV_VFS_STICKYFILE:
3621 * As in the non-jail case, non-root users are expected to be
3622 * able to read kernel/phyiscal memory (provided /dev/[k]mem
3623 * exists in the jail and they have permission to access it).
3625 case PRIV_KMEM_READ:
3629 * Depending on the global setting, allow privilege of
3630 * setting system flags.
3632 case PRIV_VFS_SYSFLAGS:
3633 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3639 * Depending on the global setting, allow privilege of
3640 * mounting/unmounting file systems.
3642 case PRIV_VFS_MOUNT:
3643 case PRIV_VFS_UNMOUNT:
3644 case PRIV_VFS_MOUNT_NONUSER:
3645 case PRIV_VFS_MOUNT_OWNER:
3646 pr = cred->cr_prison;
3648 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3656 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3657 * policy. priv_check_cred will not specifically allow it, and
3658 * we may want a MAC policy to allow it.
3660 case PRIV_VFS_READ_DIR:
3664 * Conditionnaly allow locking (unlocking) physical pages
3668 case PRIV_VM_MUNLOCK:
3669 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3675 * Conditionally allow jailed root to bind reserved ports.
3677 case PRIV_NETINET_RESERVEDPORT:
3678 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3684 * Allow jailed root to reuse in-use ports.
3686 case PRIV_NETINET_REUSEPORT:
3690 * Allow jailed root to set certain IPv4/6 (option) headers.
3692 case PRIV_NETINET_SETHDROPTS:
3696 * Conditionally allow creating raw sockets in jail.
3698 case PRIV_NETINET_RAW:
3699 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3705 * Since jail implements its own visibility limits on netstat
3706 * sysctls, allow getcred. This allows identd to work in
3709 case PRIV_NETINET_GETCRED:
3713 * Allow jailed root to set loginclass.
3715 case PRIV_PROC_SETLOGINCLASS:
3719 * Do not allow a process inside a jail to read the kernel
3720 * message buffer unless explicitly permitted.
3723 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3729 * In all remaining cases, deny the privilege request. This
3730 * includes almost all network privileges, many system
3731 * configuration privileges.
3738 * Return the part of pr2's name that is relative to pr1, or the whole name
3739 * if it does not directly follow.
3743 prison_name(struct prison *pr1, struct prison *pr2)
3747 /* Jails see themselves as "0" (if they see themselves at all). */
3750 name = pr2->pr_name;
3751 if (prison_ischild(pr1, pr2)) {
3753 * pr1 isn't locked (and allprison_lock may not be either)
3754 * so its length can't be counted on. But the number of dots
3755 * can be counted on - and counted.
3757 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3758 name = strchr(name, '.') + 1;
3764 * Return the part of pr2's path that is relative to pr1, or the whole path
3765 * if it does not directly follow.
3768 prison_path(struct prison *pr1, struct prison *pr2)
3770 char *path1, *path2;
3773 path1 = pr1->pr_path;
3774 path2 = pr2->pr_path;
3775 if (!strcmp(path1, "/"))
3777 len1 = strlen(path1);
3778 if (strncmp(path1, path2, len1))
3780 if (path2[len1] == '\0')
3782 if (path2[len1] == '/')
3783 return (path2 + len1);
3788 * Jail-related sysctls.
3790 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
3794 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3797 struct prison *pr, *cpr;
3799 struct in_addr *ip4 = NULL;
3803 struct in6_addr *ip6 = NULL;
3808 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3809 pr = req->td->td_ucred->cr_prison;
3811 sx_slock(&allprison_lock);
3812 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3813 #if defined(INET) || defined(INET6)
3816 mtx_lock(&cpr->pr_mtx);
3818 if (cpr->pr_ip4s > 0) {
3819 if (ip4s < cpr->pr_ip4s) {
3820 ip4s = cpr->pr_ip4s;
3821 mtx_unlock(&cpr->pr_mtx);
3822 ip4 = realloc(ip4, ip4s *
3823 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3826 bcopy(cpr->pr_ip4, ip4,
3827 cpr->pr_ip4s * sizeof(struct in_addr));
3831 if (cpr->pr_ip6s > 0) {
3832 if (ip6s < cpr->pr_ip6s) {
3833 ip6s = cpr->pr_ip6s;
3834 mtx_unlock(&cpr->pr_mtx);
3835 ip6 = realloc(ip6, ip6s *
3836 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3839 bcopy(cpr->pr_ip6, ip6,
3840 cpr->pr_ip6s * sizeof(struct in6_addr));
3843 bzero(xp, sizeof(*xp));
3844 xp->pr_version = XPRISON_VERSION;
3845 xp->pr_id = cpr->pr_id;
3846 xp->pr_state = cpr->pr_state;
3847 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3848 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3849 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3851 xp->pr_ip4s = cpr->pr_ip4s;
3854 xp->pr_ip6s = cpr->pr_ip6s;
3856 mtx_unlock(&cpr->pr_mtx);
3857 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3861 if (xp->pr_ip4s > 0) {
3862 error = SYSCTL_OUT(req, ip4,
3863 xp->pr_ip4s * sizeof(struct in_addr));
3869 if (xp->pr_ip6s > 0) {
3870 error = SYSCTL_OUT(req, ip6,
3871 xp->pr_ip6s * sizeof(struct in6_addr));
3877 sx_sunlock(&allprison_lock);
3888 SYSCTL_OID(_security_jail, OID_AUTO, list,
3889 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3890 sysctl_jail_list, "S", "List of active jails");
3893 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3897 injail = jailed(req->td->td_ucred);
3898 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3903 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3904 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3905 sysctl_jail_jailed, "I", "Process in jail?");
3908 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
3910 int error, havevnet;
3912 struct ucred *cred = req->td->td_ucred;
3914 havevnet = jailed(cred) && prison_owns_vnet(cred);
3918 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
3923 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
3924 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3925 sysctl_jail_vnet, "I", "Jail owns vnet?");
3927 #if defined(INET) || defined(INET6)
3928 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3929 &jail_max_af_ips, 0,
3930 "Number of IP addresses a jail may have at most per address family (deprecated)");
3934 * Default parameters for jail(2) compatibility. For historical reasons,
3935 * the sysctl names have varying similarity to the parameter names. Prisons
3936 * just see their own parameters, and can't change them.
3939 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3943 /* Get the current flag value, and convert it to a boolean. */
3944 if (req->td->td_ucred->cr_prison == &prison0) {
3945 mtx_lock(&prison0.pr_mtx);
3946 i = (jail_default_allow & arg2) != 0;
3947 mtx_unlock(&prison0.pr_mtx);
3949 i = prison_allow(req->td->td_ucred, arg2);
3953 error = sysctl_handle_int(oidp, &i, 0, req);
3954 if (error || !req->newptr)
3960 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3963 mtx_lock(&prison0.pr_mtx);
3964 jail_default_allow = (jail_default_allow & ~arg2) | i;
3965 mtx_unlock(&prison0.pr_mtx);
3969 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3970 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3971 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3972 "Processes in jail can set their hostnames (deprecated)");
3973 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3974 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3975 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3976 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
3977 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3978 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3979 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3980 "Processes in jail can use System V IPC primitives (deprecated)");
3981 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3982 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3983 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3984 "Prison root can create raw sockets (deprecated)");
3985 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3986 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3987 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3988 "Processes in jail can alter system file flags (deprecated)");
3989 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3990 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3991 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3992 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
3995 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4000 pr = req->td->td_ucred->cr_prison;
4001 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4002 error = sysctl_handle_int(oidp, &level, 0, req);
4003 if (error || !req->newptr)
4005 *(int *)arg1 = level;
4009 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4010 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4011 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4012 sysctl_jail_default_level, "I",
4013 "Processes in jail cannot see all mounted file systems (deprecated)");
4015 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4016 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4017 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4018 sysctl_jail_default_level, "I",
4019 "Ruleset for the devfs filesystem in jail (deprecated)");
4022 * Nodes to describe jail parameters. Maximum length of string parameters
4023 * is returned in the string itself, and the other parameters exist merely
4024 * to make themselves and their types known.
4026 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4030 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4037 switch (oidp->oid_kind & CTLTYPE)
4043 if (!(req->flags & SCTL_MASK32))
4045 return (SYSCTL_OUT(req, &l, sizeof(l)));
4049 return (SYSCTL_OUT(req, &i, sizeof(i)));
4050 case CTLTYPE_STRING:
4051 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4053 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4054 case CTLTYPE_STRUCT:
4056 return (SYSCTL_OUT(req, &s, sizeof(s)));
4062 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4063 * jail creation time but cannot be changed in an existing jail.
4065 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4066 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4067 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4068 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4069 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4070 "I", "Jail secure level");
4071 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4072 "Jail value for kern.osreldate and uname -K");
4073 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4074 "Jail value for kern.osrelease and uname -r");
4075 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4076 "I", "Jail cannot see all mounted file systems");
4077 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4078 "I", "Ruleset for in-jail devfs mounts");
4079 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4080 "B", "Jail persistence");
4082 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4083 "E,jailsys", "Virtual network stack");
4085 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4086 "B", "Jail is in the process of shutting down");
4088 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4089 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4090 "I", "Current number of child jails");
4091 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4092 "I", "Maximum number of child jails");
4094 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4095 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4097 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4098 "Jail NIS domainname");
4099 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4101 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4102 "LU", "Jail host ID");
4104 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4105 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4108 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4109 "Jail IPv4 address virtualization");
4110 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4111 "S,in_addr,a", "Jail IPv4 addresses");
4112 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4113 "B", "Do (not) use IPv4 source address selection rather than the "
4114 "primary jail IPv4 address.");
4117 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4118 "Jail IPv6 address virtualization");
4119 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4120 "S,in6_addr,a", "Jail IPv6 addresses");
4121 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4122 "B", "Do (not) use IPv6 source address selection rather than the "
4123 "primary jail IPv6 address.");
4126 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4127 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4128 "B", "Jail may set hostname");
4129 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4130 "B", "Jail may use SYSV IPC");
4131 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4132 "B", "Jail may create raw sockets");
4133 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4134 "B", "Jail may alter system file flags");
4135 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4136 "B", "Jail may set file quotas");
4137 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4138 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4139 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4140 "B", "Jail may lock (unlock) physical pages in memory");
4141 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4142 "B", "Jail may bind sockets to reserved ports");
4143 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4144 "B", "Jail may read the kernel message buffer");
4145 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4146 "B", "Unprivileged processes may use process debugging facilities");
4147 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4148 "B", "Processes in jail with uid 0 have privilege");
4150 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4151 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4152 "B", "Jail may mount/unmount jail-friendly file systems in general");
4155 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4156 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4160 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4163 struct bool_flags *bf;
4164 struct sysctl_oid *parent;
4165 char *allow_name, *allow_noname, *allowed;
4166 #ifndef NO_SYSCTL_DESCR
4167 char *descr_deprecated;
4172 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4174 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4176 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4177 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4178 free(allow_name, M_PRISON);
4183 * See if this parameter has already beed added, i.e. a module was
4184 * previously loaded/unloaded.
4186 mtx_lock(&prison0.pr_mtx);
4187 for (bf = pr_flag_allow;
4188 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4189 atomic_load_int(&bf->flag) != 0;
4191 if (strcmp(bf->name, allow_name) == 0) {
4192 allow_flag = bf->flag;
4198 * Find a free bit in pr_allow_all, failing if there are none
4199 * (which shouldn't happen as long as we keep track of how many
4200 * potential dynamic flags exist).
4202 for (allow_flag = 1;; allow_flag <<= 1) {
4203 if (allow_flag == 0)
4205 if ((pr_allow_all & allow_flag) == 0)
4209 /* Note the parameter in the next open slot in pr_flag_allow. */
4210 for (bf = pr_flag_allow; ; bf++) {
4211 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4212 /* This should never happen, but is not fatal. */
4216 if (atomic_load_int(&bf->flag) == 0)
4219 bf->name = allow_name;
4220 bf->noname = allow_noname;
4221 pr_allow_all |= allow_flag;
4223 * prison0 always has permission for the new parameter.
4224 * Other jails must have it granted to them.
4226 prison0.pr_allow |= allow_flag;
4227 /* The flag indicates a valid entry, so make sure it is set last. */
4228 atomic_store_rel_int(&bf->flag, allow_flag);
4229 mtx_unlock(&prison0.pr_mtx);
4232 * Create sysctls for the paramter, and the back-compat global
4236 ? SYSCTL_ADD_NODE(NULL,
4237 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4238 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4239 : &sysctl___security_jail_param_allow;
4240 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4241 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4242 NULL, 0, sysctl_jail_param, "B", descr);
4244 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4245 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4246 #ifndef NO_SYSCTL_DESCR
4247 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4250 (void)SYSCTL_ADD_PROC(NULL,
4251 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4252 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4253 sysctl_jail_default_allow, "I", descr_deprecated);
4254 #ifndef NO_SYSCTL_DESCR
4255 free(descr_deprecated, M_TEMP);
4257 free(allowed, M_TEMP);
4262 mtx_unlock(&prison0.pr_mtx);
4263 free(allow_name, M_PRISON);
4264 free(allow_noname, M_PRISON);
4269 * The VFS system will register jail-aware filesystems here. They each get
4270 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4271 * attempts to mount.
4274 prison_add_vfs(struct vfsconf *vfsp)
4276 #ifdef NO_SYSCTL_DESCR
4278 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4283 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4285 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4287 free(descr, M_TEMP);
4293 prison_racct_foreach(void (*callback)(struct racct *racct,
4294 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4295 void *arg2, void *arg3)
4297 struct prison_racct *prr;
4299 ASSERT_RACCT_ENABLED();
4301 sx_slock(&allprison_lock);
4304 LIST_FOREACH(prr, &allprison_racct, prr_next)
4305 (callback)(prr->prr_racct, arg2, arg3);
4308 sx_sunlock(&allprison_lock);
4311 static struct prison_racct *
4312 prison_racct_find_locked(const char *name)
4314 struct prison_racct *prr;
4316 ASSERT_RACCT_ENABLED();
4317 sx_assert(&allprison_lock, SA_XLOCKED);
4319 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4322 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4323 if (strcmp(name, prr->prr_name) != 0)
4326 /* Found prison_racct with a matching name? */
4327 prison_racct_hold(prr);
4331 /* Add new prison_racct. */
4332 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4333 racct_create(&prr->prr_racct);
4335 strcpy(prr->prr_name, name);
4336 refcount_init(&prr->prr_refcount, 1);
4337 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4342 struct prison_racct *
4343 prison_racct_find(const char *name)
4345 struct prison_racct *prr;
4347 ASSERT_RACCT_ENABLED();
4349 sx_xlock(&allprison_lock);
4350 prr = prison_racct_find_locked(name);
4351 sx_xunlock(&allprison_lock);
4356 prison_racct_hold(struct prison_racct *prr)
4359 ASSERT_RACCT_ENABLED();
4361 refcount_acquire(&prr->prr_refcount);
4365 prison_racct_free_locked(struct prison_racct *prr)
4368 ASSERT_RACCT_ENABLED();
4369 sx_assert(&allprison_lock, SA_XLOCKED);
4371 if (refcount_release(&prr->prr_refcount)) {
4372 racct_destroy(&prr->prr_racct);
4373 LIST_REMOVE(prr, prr_next);
4374 free(prr, M_PRISON_RACCT);
4379 prison_racct_free(struct prison_racct *prr)
4382 ASSERT_RACCT_ENABLED();
4383 sx_assert(&allprison_lock, SA_UNLOCKED);
4385 if (refcount_release_if_not_last(&prr->prr_refcount))
4388 sx_xlock(&allprison_lock);
4389 prison_racct_free_locked(prr);
4390 sx_xunlock(&allprison_lock);
4394 prison_racct_attach(struct prison *pr)
4396 struct prison_racct *prr;
4398 ASSERT_RACCT_ENABLED();
4399 sx_assert(&allprison_lock, SA_XLOCKED);
4401 prr = prison_racct_find_locked(pr->pr_name);
4402 KASSERT(prr != NULL, ("cannot find prison_racct"));
4404 pr->pr_prison_racct = prr;
4408 * Handle jail renaming. From the racct point of view, renaming means
4409 * moving from one prison_racct to another.
4412 prison_racct_modify(struct prison *pr)
4418 struct prison_racct *oldprr;
4420 ASSERT_RACCT_ENABLED();
4422 sx_slock(&allproc_lock);
4423 sx_xlock(&allprison_lock);
4425 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4426 sx_xunlock(&allprison_lock);
4427 sx_sunlock(&allproc_lock);
4431 oldprr = pr->pr_prison_racct;
4432 pr->pr_prison_racct = NULL;
4434 prison_racct_attach(pr);
4437 * Move resource utilisation records.
4439 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4443 * Force rctl to reattach rules to processes.
4445 FOREACH_PROC_IN_SYSTEM(p) {
4447 cred = crhold(p->p_ucred);
4449 rctl_proc_ucred_changed(p, cred);
4454 sx_sunlock(&allproc_lock);
4455 prison_racct_free_locked(oldprr);
4456 sx_xunlock(&allprison_lock);
4460 prison_racct_detach(struct prison *pr)
4463 ASSERT_RACCT_ENABLED();
4464 sx_assert(&allprison_lock, SA_UNLOCKED);
4466 if (pr->pr_prison_racct == NULL)
4468 prison_racct_free(pr->pr_prison_racct);
4469 pr->pr_prison_racct = NULL;
4476 db_show_prison(struct prison *pr)
4478 struct bool_flags *bf;
4479 struct jailsys_flags *jsf;
4480 #if defined(INET) || defined(INET6)
4485 char ip4buf[INET_ADDRSTRLEN];
4488 char ip6buf[INET6_ADDRSTRLEN];
4491 db_printf("prison %p:\n", pr);
4492 db_printf(" jid = %d\n", pr->pr_id);
4493 db_printf(" name = %s\n", pr->pr_name);
4494 db_printf(" parent = %p\n", pr->pr_parent);
4495 db_printf(" ref = %d\n", pr->pr_ref);
4496 db_printf(" uref = %d\n", pr->pr_uref);
4497 db_printf(" state = %s\n",
4498 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4499 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4501 db_printf(" path = %s\n", pr->pr_path);
4502 db_printf(" cpuset = %d\n", pr->pr_cpuset
4503 ? pr->pr_cpuset->cs_id : -1);
4505 db_printf(" vnet = %p\n", pr->pr_vnet);
4507 db_printf(" root = %p\n", pr->pr_root);
4508 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4509 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4510 db_printf(" children.max = %d\n", pr->pr_childmax);
4511 db_printf(" children.cur = %d\n", pr->pr_childcount);
4512 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4513 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4514 db_printf(" flags = 0x%x", pr->pr_flags);
4515 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4516 if (pr->pr_flags & bf->flag)
4517 db_printf(" %s", bf->name);
4518 for (jsf = pr_flag_jailsys;
4519 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4521 f = pr->pr_flags & (jsf->disable | jsf->new);
4522 db_printf(" %-16s= %s\n", jsf->name,
4523 (f != 0 && f == jsf->disable) ? "disable"
4524 : (f == jsf->new) ? "new"
4527 db_printf(" allow = 0x%x", pr->pr_allow);
4528 for (bf = pr_flag_allow;
4529 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4530 atomic_load_int(&bf->flag) != 0;
4532 if (pr->pr_allow & bf->flag)
4533 db_printf(" %s", bf->name);
4535 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4536 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4537 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4538 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4539 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4541 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4542 for (ii = 0; ii < pr->pr_ip4s; ii++)
4543 db_printf(" %s %s\n",
4544 ii == 0 ? "ip4.addr =" : " ",
4545 inet_ntoa_r(pr->pr_ip4[ii], ip4buf));
4548 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4549 for (ii = 0; ii < pr->pr_ip6s; ii++)
4550 db_printf(" %s %s\n",
4551 ii == 0 ? "ip6.addr =" : " ",
4552 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4556 DB_SHOW_COMMAND(prison, db_show_prison_command)
4562 * Show all prisons in the list, and prison0 which is not
4565 db_show_prison(&prison0);
4566 if (!db_pager_quit) {
4567 TAILQ_FOREACH(pr, &allprison, pr_list) {
4579 /* Look for a prison with the ID and with references. */
4580 TAILQ_FOREACH(pr, &allprison, pr_list)
4581 if (pr->pr_id == addr && pr->pr_ref > 0)
4584 /* Look again, without requiring a reference. */
4585 TAILQ_FOREACH(pr, &allprison, pr_list)
4586 if (pr->pr_id == addr)
4589 /* Assume address points to a valid prison. */
4590 pr = (struct prison *)addr;