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/epoch.h>
49 #include <sys/taskqueue.h>
50 #include <sys/fcntl.h>
52 #include <sys/linker.h>
55 #include <sys/mutex.h>
56 #include <sys/racct.h>
58 #include <sys/refcount.h>
60 #include <sys/sysent.h>
61 #include <sys/namei.h>
62 #include <sys/mount.h>
63 #include <sys/queue.h>
64 #include <sys/socket.h>
65 #include <sys/syscallsubr.h>
66 #include <sys/sysctl.h>
68 #include <sys/vnode.h>
73 #include <netinet/in.h>
79 #include <security/mac/mac_framework.h>
81 #define PRISON0_HOSTUUID_MODULE "hostuuid"
83 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
84 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
86 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
89 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
91 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
95 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
97 #define _PR_IP_SADDRSEL 0
101 /* prison0 describes what is "real" about the system. */
102 struct prison prison0 = {
108 .pr_securelevel = -1,
110 .pr_state = PRISON_STATE_ALIVE,
111 .pr_childmax = JAIL_MAX,
112 .pr_hostuuid = DEFAULT_HOSTUUID,
113 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
115 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
117 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
119 .pr_allow = PR_ALLOW_ALL_STATIC,
121 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
128 struct jailsys_flags {
134 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
135 struct sx allprison_lock;
136 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
137 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
138 LIST_HEAD(, prison_racct) allprison_racct;
141 static int get_next_prid(struct prison **insprp);
142 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
143 static void prison_complete(void *context, int pending);
144 static void prison_deref(struct prison *pr, int flags);
145 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
146 static int prison_lock_xlock(struct prison *pr, int flags);
147 static void prison_cleanup(struct prison *pr);
148 static void prison_free_not_last(struct prison *pr);
149 static void prison_proc_free_not_last(struct prison *pr);
150 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
152 static char *prison_path(struct prison *pr1, struct prison *pr2);
154 static void prison_racct_attach(struct prison *pr);
155 static void prison_racct_modify(struct prison *pr);
156 static void prison_racct_detach(struct prison *pr);
159 /* Flags for prison_deref */
160 #define PD_DEREF 0x01 /* Decrement pr_ref */
161 #define PD_DEUREF 0x02 /* Decrement pr_uref */
162 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
163 #define PD_LOCKED 0x10 /* pr_mtx is held */
164 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
165 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
166 #define PD_OP_FLAGS 0x07 /* Operation flags */
167 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
170 * Parameter names corresponding to PR_* flag values. Size values are for kvm
171 * as we cannot figure out the size of a sparse array, or an array without a
174 static struct bool_flags pr_flag_bool[] = {
175 {"persist", "nopersist", PR_PERSIST},
177 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
180 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
183 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
185 static struct jailsys_flags pr_flag_jailsys[] = {
186 {"host", 0, PR_HOST},
188 {"vnet", 0, PR_VNET},
191 {"ip4", PR_IP4_USER, PR_IP4_USER},
194 {"ip6", PR_IP6_USER, PR_IP6_USER},
197 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
200 * Make this array full-size so dynamic parameters can be added.
201 * It is protected by prison0.mtx, but lockless reading is allowed
202 * with an atomic check of the flag values.
204 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
205 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
206 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
207 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
208 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
209 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
210 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
211 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
212 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
213 {"allow.reserved_ports", "allow.noreserved_ports",
214 PR_ALLOW_RESERVED_PORTS},
215 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
216 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
217 PR_ALLOW_UNPRIV_DEBUG},
218 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
220 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
221 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
223 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
224 PR_ALLOW_RESERVED_PORTS | \
225 PR_ALLOW_UNPRIV_DEBUG | \
227 #define JAIL_DEFAULT_ENFORCE_STATFS 2
228 #define JAIL_DEFAULT_DEVFS_RSNUM 0
229 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
230 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
231 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
232 #if defined(INET) || defined(INET6)
233 static unsigned jail_max_af_ips = 255;
237 * Initialize the parts of prison0 that can't be static-initialized with
238 * constants. This is called from proc0_init() after creating thread0 cpuset.
243 uint8_t *file, *data;
245 char buf[sizeof(prison0.pr_hostuuid)];
248 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
249 prison0.pr_osreldate = osreldate;
250 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
252 /* If we have a preloaded hostuuid, use it. */
253 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
255 data = preload_fetch_addr(file);
256 size = preload_fetch_size(file);
259 * The preloaded data may include trailing whitespace, almost
260 * certainly a newline; skip over any whitespace or
261 * non-printable characters to be safe.
263 while (size > 0 && data[size - 1] <= 0x20) {
270 * Not NUL-terminated when passed from loader, but
271 * validate_uuid requires that due to using sscanf (as
272 * does the subsequent strlcpy, since it still reads
273 * past the given size to return the true length);
274 * bounce to a temporary buffer to fix.
276 if (size >= sizeof(buf))
279 memcpy(buf, data, size);
282 if (validate_uuid(buf, size, NULL, 0) != 0)
286 (void)strlcpy(prison0.pr_hostuuid, buf,
287 sizeof(prison0.pr_hostuuid));
290 if (bootverbose && !valid) {
291 printf("hostuuid: preload data malformed: '%.*s'\n",
297 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
306 sys_jail(struct thread *td, struct jail_args *uap)
312 error = copyin(uap->jail, &version, sizeof(uint32_t));
321 /* FreeBSD single IPv4 jails. */
322 bzero(&j, sizeof(struct jail));
323 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
326 j.version = j0.version;
328 j.hostname = j0.hostname;
329 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
335 * Version 1 was used by multi-IPv4 jail implementations
336 * that never made it into the official kernel.
340 case 2: /* JAIL_API_VERSION */
341 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
342 error = copyin(uap->jail, &j, sizeof(struct jail));
348 /* Sci-Fi jails are not supported, sorry. */
351 return (kern_jail(td, &j));
355 kern_jail(struct thread *td, struct jail *j)
357 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
366 char *u_path, *u_hostname, *u_name;
367 struct bool_flags *bf;
370 struct in_addr *u_ip4;
373 struct in6_addr *u_ip6;
376 int error, enforce_statfs;
378 bzero(&optiov, sizeof(optiov));
379 opt.uio_iov = optiov;
383 opt.uio_segflg = UIO_SYSSPACE;
384 opt.uio_rw = UIO_READ;
387 /* Set permissions for top-level jails from sysctls. */
388 if (!jailed(td->td_ucred)) {
389 for (bf = pr_flag_allow;
390 bf < pr_flag_allow + nitems(pr_flag_allow) &&
391 atomic_load_int(&bf->flag) != 0;
393 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
394 (jail_default_allow & bf->flag)
395 ? bf->name : bf->noname);
396 optiov[opt.uio_iovcnt].iov_len =
397 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
400 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
401 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
403 enforce_statfs = jail_default_enforce_statfs;
404 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
405 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
409 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
411 ip4s = (j->version == 0) ? 1 : j->ip4s;
412 if (ip4s > jail_max_af_ips)
414 tmplen += ip4s * sizeof(struct in_addr);
420 if (j->ip6s > jail_max_af_ips)
422 tmplen += j->ip6s * sizeof(struct in6_addr);
427 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
428 u_hostname = u_path + MAXPATHLEN;
429 u_name = u_hostname + MAXHOSTNAMELEN;
431 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
435 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
437 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
440 optiov[opt.uio_iovcnt].iov_base = "path";
441 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
443 optiov[opt.uio_iovcnt].iov_base = u_path;
444 error = copyinstr(j->path, u_path, MAXPATHLEN,
445 &optiov[opt.uio_iovcnt].iov_len);
447 free(u_path, M_TEMP);
451 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
452 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
454 optiov[opt.uio_iovcnt].iov_base = u_hostname;
455 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
456 &optiov[opt.uio_iovcnt].iov_len);
458 free(u_path, M_TEMP);
462 if (j->jailname != NULL) {
463 optiov[opt.uio_iovcnt].iov_base = "name";
464 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
466 optiov[opt.uio_iovcnt].iov_base = u_name;
467 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
468 &optiov[opt.uio_iovcnt].iov_len);
470 free(u_path, M_TEMP);
476 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
477 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
479 optiov[opt.uio_iovcnt].iov_base = u_ip4;
480 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
482 u_ip4->s_addr = j->ip4s;
484 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
486 free(u_path, M_TEMP);
493 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
494 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
496 optiov[opt.uio_iovcnt].iov_base = u_ip6;
497 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
498 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
500 free(u_path, M_TEMP);
505 KASSERT(opt.uio_iovcnt <= nitems(optiov),
506 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
507 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
508 free(u_path, M_TEMP);
513 * struct jail_set_args {
514 * struct iovec *iovp;
515 * unsigned int iovcnt;
520 sys_jail_set(struct thread *td, struct jail_set_args *uap)
525 /* Check that we have an even number of iovecs. */
529 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
532 error = kern_jail_set(td, auio, uap->flags);
537 #if defined(INET) || defined(INET6)
538 typedef int prison_addr_cmp_t(const void *, const void *);
539 typedef bool prison_addr_valid_t(const void *);
540 static const struct pr_family {
542 prison_addr_cmp_t *cmp;
543 prison_addr_valid_t *valid;
545 } pr_families[PR_FAMILY_MAX] = {
548 .size = sizeof(struct in_addr),
549 .cmp = prison_qcmp_v4,
550 .valid = prison_valid_v4,
551 .ip_flag = PR_IP4_USER,
556 .size = sizeof(struct in6_addr),
557 .cmp = prison_qcmp_v6,
558 .valid = prison_valid_v6,
559 .ip_flag = PR_IP6_USER,
565 * Network address lists (pr_addrs) allocation for jails. The addresses
566 * are accessed locklessly by the network stack, thus need to be protected by
570 struct epoch_context ctx;
574 struct in_addr pr_ip4[];
575 struct in6_addr pr_ip6[];
577 #else /* No future C :( */
578 #define PR_IP(pip, i) ((const char *)((pip) + 1) + pr_families[af].size * (i))
579 #define PR_IPD(pip, i) ((char *)((pip) + 1) + pr_families[af].size * (i))
583 static struct prison_ip *
584 prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
586 struct prison_ip *pip;
588 pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
596 * Allocate and copyin user supplied address list, sorting and validating.
597 * kern_jail_set() helper.
599 static struct prison_ip *
600 prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
602 prison_addr_cmp_t *const cmp = pr_families[af].cmp;
603 const size_t size = pr_families[af].size;
604 struct prison_ip *pip;
606 pip = prison_ip_alloc(af, cnt, M_WAITOK);
607 bcopy(op, pip + 1, cnt * size);
609 * IP addresses are all sorted but ip[0] to preserve
610 * the primary IP address as given from userland.
611 * This special IP is used for unbound outgoing
612 * connections as well for "loopback" traffic in case
613 * source address selection cannot find any more fitting
614 * address to connect from.
617 qsort((char *)(pip + 1) + size, cnt - 1, size,
618 pr_families[af].cmp);
620 * Check for duplicate addresses and do some simple
621 * zero and broadcast checks. If users give other bogus
622 * addresses it is their problem.
624 for (int i = 0; i < cnt; i++) {
625 if (!pr_families[af].valid(PR_IP(pip, i))) {
630 (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 ||
631 cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) {
641 * Allocate and dup parent prison address list.
642 * kern_jail_set() helper.
645 prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
648 if (ppr->pr_addrs[af] != NULL) {
649 pr->pr_addrs[af] = prison_ip_alloc(af,
650 ppr->pr_addrs[af]->ips, M_WAITOK);
651 bcopy(ppr->pr_addrs[af], pr->pr_addrs[af],
652 pr->pr_addrs[af]->ips * pr_families[af].size);
657 * Make sure the new set of IP addresses is a subset of the parent's list.
658 * Don't worry about the parent being unlocked, as any setting is done with
659 * allprison_lock held.
660 * kern_jail_set() helper.
663 prison_ip_parent_match(const struct prison_ip *ppip,
664 const struct prison_ip *pip, const pr_family_t af)
666 prison_addr_cmp_t *const cmp = pr_families[af].cmp;
672 for (i = 0; i < ppip->ips; i++)
673 if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0)
677 /* Main address not present in parent. */
681 for (i = j = 1; i < pip->ips; i++) {
682 if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0)
683 /* Equals to parent primary address. */
685 for (; j < ppip->ips; j++)
686 if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0)
692 /* Address not present in parent. */
699 * Check for conflicting IP addresses. We permit them if there is no more
700 * than one IP on each jail. If there is a duplicate on a jail with more
701 * than one IP stop checking and return error.
702 * kern_jail_set() helper.
705 prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
706 const struct prison_ip *pip, pr_family_t af)
708 const struct prison *tppr, *tpr;
712 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
713 if (tppr->pr_flags & PR_VNET)
718 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
721 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
723 !prison_isalive(tpr)) {
727 if (!(tpr->pr_flags & pr_families[af].ip_flag))
730 if (tpr->pr_addrs[af] == NULL ||
731 (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
733 for (int i = 0; i < pip->ips; i++)
734 if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0)
741 _Static_assert(offsetof(struct prison_ip, ctx) == 0,
742 "prison must start with epoch context");
744 prison_ip_free_deferred(epoch_context_t ctx)
751 prison_ip_free(struct prison_ip *pip)
755 NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
759 prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
761 struct prison_ip **mem, *old;
763 mtx_assert(&pr->pr_mtx, MA_OWNED);
765 mem = &pr->pr_addrs[af];
768 ck_pr_store_ptr(mem, new);
773 * Restrict a prison's IP address list with its parent's, possibly replacing
774 * it. Return true if the replacement buffer was used (or would have been).
775 * kern_jail_set() helper.
778 prison_ip_restrict(struct prison *pr, const pr_family_t af,
779 struct prison_ip *new)
781 const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
782 const struct prison_ip *pip = pr->pr_addrs[af];
783 int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
784 const size_t size = pr_families[af].size;
788 mtx_assert(&pr->pr_mtx, MA_OWNED);
791 * Due to epoch-synchronized access to the IP address lists we always
792 * allocate a new list even if the old one has enough space. We could
793 * atomically update an IPv4 address inside a list, but that would
794 * screw up sorting, and in case of IPv6 we can't even atomically write
797 ips = (pr->pr_flags & pr_families[af].ip_flag) ? pip->ips : ppip->ips;
799 prison_ip_set(pr, af, NULL);
803 new = prison_ip_alloc(af, ips, M_NOWAIT);
809 if (!(pr->pr_flags & pr_families[af].ip_flag)) {
810 /* This has no user settings, so just copy the parent's list. */
811 bcopy(ppip, new, ips * size);
813 /* Remove addresses that aren't in the parent. */
816 i = 0; /* index in pip */
817 ips = 0; /* index in new */
819 for (int pi = 0; pi < ppip->ips; pi++)
820 if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) {
821 /* Found our primary address in parent. */
822 bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
827 for (int pi = 1; i < pip->ips; ) {
828 /* Check against primary, which is unsorted. */
829 if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) {
830 /* Matches parent's primary address. */
831 bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
836 /* The rest are sorted. */
837 switch (pi >= ppip->ips ? -1 :
838 cmp(PR_IP(pip, i), PR_IP(ppip, pi))) {
843 bcopy(PR_IP(pr, i), PR_IPD(new, ips), size);
859 prison_ip_set(pr, af, new);
860 return (new != NULL ? true : false);
864 * Fast-path check if an address belongs to a prison.
867 prison_ip_check(const struct prison *pr, const pr_family_t af,
870 int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
871 const struct prison_ip *pip;
874 MPASS(mtx_owned(&pr->pr_mtx) ||
875 in_epoch(net_epoch_preempt) ||
876 sx_xlocked(&allprison_lock));
878 pip = ck_pr_load_ptr(&pr->pr_addrs[af]);
879 if (__predict_false(pip == NULL))
880 return (EAFNOSUPPORT);
882 /* Check the primary IP. */
883 if (cmp(PR_IP(pip, 0), addr) == 0)
887 * All the other IPs are sorted so we can do a binary search.
893 d = cmp(PR_IP(pip, i + 1), addr);
902 return (EADDRNOTAVAIL);
906 * Grab primary IP. Historically required mutex, but nothing prevents
907 * us to support epoch-protected access. Is it used in fast path?
908 * in{6}_jail.c helper
911 prison_ip_get0(const struct prison *pr, const pr_family_t af)
913 const struct prison_ip *pip = pr->pr_addrs[af];
915 mtx_assert(&pr->pr_mtx, MA_OWNED);
922 prison_ip_cnt(const struct prison *pr, const pr_family_t af)
925 return (pr->pr_addrs[af]->ips);
927 #endif /* defined(INET) || defined(INET6) */
930 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
934 struct prison_ip *ip4;
937 struct prison_ip *ip6;
940 struct vfsoptlist *opts;
941 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
943 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
944 char *g_path, *osrelstr;
945 struct bool_flags *bf;
946 struct jailsys_flags *jsf;
947 #if defined(INET) || defined(INET6)
951 size_t namelen, onamelen, pnamelen;
952 int born, created, cuflags, descend, drflags, enforce;
953 int error, errmsg_len, errmsg_pos;
954 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
955 int jid, jsys, len, level;
956 int childmax, osreldt, rsnum, slevel;
963 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
964 uint64_t pr_allow_diff;
968 error = priv_check(td, PRIV_JAIL_SET);
969 if (!error && (flags & JAIL_ATTACH))
970 error = priv_check(td, PRIV_JAIL_ATTACH);
973 mypr = td->td_ucred->cr_prison;
974 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
976 if (flags & ~JAIL_SET_MASK)
980 * Check all the parameters before committing to anything. Not all
981 * errors can be caught early, but we may as well try. Also, this
982 * takes care of some expensive stuff (path lookup) before getting
983 * the allprison lock.
985 * XXX Jails are not filesystems, and jail parameters are not mount
986 * options. But it makes more sense to re-use the vfsopt code
987 * than duplicate it under a different name.
989 error = vfs_buildopts(optuio, &opts);
1000 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1003 vfs_opterror(opts, "no valid operation (create or update)");
1007 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1008 if (error == ENOENT)
1010 else if (error != 0)
1013 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1014 if (error == ENOENT)
1016 else if (error != 0)
1022 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1023 if (error == ENOENT)
1025 else if (error != 0)
1030 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1031 if (error == ENOENT)
1033 else if (error != 0)
1035 else if (enforce < 0 || enforce > 2) {
1041 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
1042 if (error == ENOENT)
1044 else if (error != 0)
1049 pr_flags = ch_flags = 0;
1050 for (bf = pr_flag_bool;
1051 bf < pr_flag_bool + nitems(pr_flag_bool);
1053 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1054 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
1056 ch_flags |= pr_flags;
1057 for (jsf = pr_flag_jailsys;
1058 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1060 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
1061 if (error == ENOENT)
1066 case JAIL_SYS_DISABLE:
1067 if (!jsf->disable) {
1071 pr_flags |= jsf->disable;
1074 pr_flags |= jsf->new;
1076 case JAIL_SYS_INHERIT:
1082 ch_flags |= jsf->new | jsf->disable;
1084 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
1085 && !(pr_flags & PR_PERSIST)) {
1087 vfs_opterror(opts, "new jail must persist or attach");
1091 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1093 vfs_opterror(opts, "vnet cannot be changed after creation");
1098 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
1100 vfs_opterror(opts, "ip4 cannot be changed after creation");
1105 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
1107 vfs_opterror(opts, "ip6 cannot be changed after creation");
1112 pr_allow = ch_allow = 0;
1113 for (bf = pr_flag_allow;
1114 bf < pr_flag_allow + nitems(pr_flag_allow) &&
1115 atomic_load_int(&bf->flag) != 0;
1117 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1118 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
1120 ch_allow |= pr_allow;
1122 error = vfs_getopt(opts, "name", (void **)&name, &len);
1123 if (error == ENOENT)
1125 else if (error != 0)
1128 if (len == 0 || name[len - 1] != '\0') {
1132 if (len > MAXHOSTNAMELEN) {
1133 error = ENAMETOOLONG;
1138 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1139 if (error == ENOENT)
1141 else if (error != 0)
1144 ch_flags |= PR_HOST;
1145 pr_flags |= PR_HOST;
1146 if (len == 0 || host[len - 1] != '\0') {
1150 if (len > MAXHOSTNAMELEN) {
1151 error = ENAMETOOLONG;
1156 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
1157 if (error == ENOENT)
1159 else if (error != 0)
1162 ch_flags |= PR_HOST;
1163 pr_flags |= PR_HOST;
1164 if (len == 0 || domain[len - 1] != '\0') {
1168 if (len > MAXHOSTNAMELEN) {
1169 error = ENAMETOOLONG;
1174 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
1175 if (error == ENOENT)
1177 else if (error != 0)
1180 ch_flags |= PR_HOST;
1181 pr_flags |= PR_HOST;
1182 if (len == 0 || uuid[len - 1] != '\0') {
1186 if (len > HOSTUUIDLEN) {
1187 error = ENAMETOOLONG;
1192 #ifdef COMPAT_FREEBSD32
1193 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1196 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
1200 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
1201 if (error == ENOENT)
1203 else if (error != 0)
1207 ch_flags |= PR_HOST;
1208 pr_flags |= PR_HOST;
1212 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1213 if (error == ENOENT)
1215 else if (error != 0)
1217 else if (ip4s & (sizeof(struct in_addr) - 1)) {
1221 ch_flags |= PR_IP4_USER;
1222 pr_flags |= PR_IP4_USER;
1224 ip4s /= sizeof(struct in_addr);
1225 if (ip4s > jail_max_af_ips) {
1227 vfs_opterror(opts, "too many IPv4 addresses");
1230 ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1240 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1241 if (error == ENOENT)
1243 else if (error != 0)
1245 else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1249 ch_flags |= PR_IP6_USER;
1250 pr_flags |= PR_IP6_USER;
1252 ip6s /= sizeof(struct in6_addr);
1253 if (ip6s > jail_max_af_ips) {
1255 vfs_opterror(opts, "too many IPv6 addresses");
1258 ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1267 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1268 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1271 "vnet jails cannot have IP address restrictions");
1276 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1277 if (error == ENOENT)
1279 else if (error != 0)
1282 if (flags & JAIL_UPDATE) {
1285 "osrelease cannot be changed after creation");
1288 if (len == 0 || osrelstr[len - 1] != '\0') {
1292 if (len >= OSRELEASELEN) {
1293 error = ENAMETOOLONG;
1295 "osrelease string must be 1-%d bytes long",
1301 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1302 if (error == ENOENT)
1304 else if (error != 0)
1307 if (flags & JAIL_UPDATE) {
1310 "osreldate cannot be changed after creation");
1315 vfs_opterror(opts, "osreldate cannot be 0");
1321 error = vfs_getopt(opts, "path", (void **)&path, &len);
1322 if (error == ENOENT)
1324 else if (error != 0)
1327 if (flags & JAIL_UPDATE) {
1330 "path cannot be changed after creation");
1333 if (len == 0 || path[len - 1] != '\0') {
1337 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1343 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1344 strlcpy(g_path, path, MAXPATHLEN);
1345 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1349 /* exit on other errors */
1352 if (root->v_type != VDIR) {
1361 * Find the specified jail, or at least its parent.
1362 * This abuses the file error codes ENOENT and EEXIST.
1366 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1367 namelc = strrchr(name, '.');
1368 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1372 sx_xlock(&allprison_lock);
1373 drflags = PD_LIST_XLOCKED;
1375 if (!prison_isalive(ppr)) {
1376 /* This jail is dying. This process will surely follow. */
1383 vfs_opterror(opts, "negative jid");
1387 * See if a requested jid already exists. Keep track of
1388 * where it can be inserted later.
1390 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1391 if (inspr->pr_id < jid)
1393 if (inspr->pr_id > jid)
1396 mtx_lock(&pr->pr_mtx);
1397 drflags |= PD_LOCKED;
1402 /* Create: jid must not exist. */
1403 if (cuflags == JAIL_CREATE) {
1405 * Even creators that cannot see the jail will
1409 vfs_opterror(opts, "jail %d already exists",
1413 if (!prison_ischild(mypr, pr)) {
1415 * Updaters get ENOENT if they cannot see the
1416 * jail. This is true even for CREATE | UPDATE,
1417 * which normally cannot give this error.
1420 vfs_opterror(opts, "jail %d not found", jid);
1423 ppr = pr->pr_parent;
1424 if (!prison_isalive(ppr)) {
1426 vfs_opterror(opts, "jail %d is dying",
1430 if (!prison_isalive(pr)) {
1431 if (!(flags & JAIL_DYING)) {
1433 vfs_opterror(opts, "jail %d is dying",
1437 if ((flags & JAIL_ATTACH) ||
1438 (pr_flags & PR_PERSIST)) {
1440 * A dying jail might be resurrected
1441 * (via attach or persist), but first
1442 * it must determine if another jail
1443 * has claimed its name. Accomplish
1444 * this by implicitly re-setting the
1448 name = prison_name(mypr, pr);
1452 /* Update: jid must exist. */
1453 if (cuflags == JAIL_UPDATE) {
1455 vfs_opterror(opts, "jail %d not found", jid);
1461 * If the caller provided a name, look for a jail by that name.
1462 * This has different semantics for creates and updates keyed by jid
1463 * (where the name must not already exist in a different jail),
1464 * and updates keyed by the name itself (where the name must exist
1465 * because that is the jail being updated).
1469 namelc = strrchr(name, '.');
1474 * This is a hierarchical name. Split it into the
1475 * parent and child names, and make sure the parent
1476 * exists or matches an already found jail.
1479 if (strncmp(name, ppr->pr_name, namelc - name)
1480 || ppr->pr_name[namelc - name] != '\0') {
1483 "cannot change jail's parent");
1488 ppr = prison_find_name(mypr, name);
1492 "jail \"%s\" not found", name);
1495 mtx_unlock(&ppr->pr_mtx);
1496 if (!prison_isalive(ppr)) {
1499 "jail \"%s\" is dying", name);
1506 if (namelc[0] != '\0') {
1508 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1510 FOREACH_PRISON_CHILD(ppr, tpr) {
1512 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1513 if (prison_isalive(tpr)) {
1515 cuflags != JAIL_CREATE) {
1521 mtx_lock(&pr->pr_mtx);
1522 drflags |= PD_LOCKED;
1526 * Create, or update(jid):
1527 * name must not exist in an
1528 * active sibling jail.
1532 "jail \"%s\" already exists",
1537 cuflags != JAIL_CREATE) {
1542 /* If no active jail is found, use a dying one. */
1543 if (deadpr != NULL && pr == NULL) {
1544 if (flags & JAIL_DYING) {
1546 mtx_lock(&pr->pr_mtx);
1547 drflags |= PD_LOCKED;
1548 } else if (cuflags == JAIL_UPDATE) {
1551 "jail \"%s\" is dying", name);
1555 /* Update: name must exist if no jid. */
1556 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1558 vfs_opterror(opts, "jail \"%s\" not found",
1564 /* Update: must provide a jid or name. */
1565 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1567 vfs_opterror(opts, "update specified no jail");
1571 /* If there's no prison to update, create a new one and link it in. */
1572 created = pr == NULL;
1574 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1575 if (tpr->pr_childcount >= tpr->pr_childmax) {
1577 vfs_opterror(opts, "prison limit exceeded");
1580 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1582 vfs_opterror(opts, "no available jail IDs");
1586 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1587 pr->pr_state = PRISON_STATE_INVALID;
1588 refcount_init(&pr->pr_ref, 1);
1589 refcount_init(&pr->pr_uref, 0);
1590 drflags |= PD_DEREF;
1591 LIST_INIT(&pr->pr_children);
1592 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1593 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1597 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1599 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1601 pr->pr_parent = ppr;
1603 prison_proc_hold(ppr);
1604 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1605 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1606 tpr->pr_childcount++;
1608 /* Set some default values, and inherit some from the parent. */
1613 root = mypr->pr_root;
1616 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1617 pr->pr_flags |= PR_HOST;
1618 #if defined(INET) || defined(INET6)
1620 if (!(pr_flags & PR_VNET))
1624 if (!(ch_flags & PR_IP4_USER))
1625 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1626 else if (!(pr_flags & PR_IP4_USER)) {
1627 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1628 prison_ip_dup(ppr, pr, PR_INET);
1632 if (!(ch_flags & PR_IP6_USER))
1633 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1634 else if (!(pr_flags & PR_IP6_USER)) {
1635 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1636 prison_ip_dup(ppr, pr, PR_INET6);
1641 /* Source address selection is always on by default. */
1642 pr->pr_flags |= _PR_IP_SADDRSEL;
1644 pr->pr_securelevel = ppr->pr_securelevel;
1645 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1646 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1647 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1649 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1650 if (osrelstr == NULL)
1651 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1652 sizeof(pr->pr_osrelease));
1654 strlcpy(pr->pr_osrelease, osrelstr,
1655 sizeof(pr->pr_osrelease));
1658 /* Allocate a new vnet if specified. */
1659 pr->pr_vnet = (pr_flags & PR_VNET)
1660 ? vnet_alloc() : ppr->pr_vnet;
1663 * Allocate a dedicated cpuset for each jail.
1664 * Unlike other initial settings, this may return an error.
1666 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1670 mtx_lock(&pr->pr_mtx);
1671 drflags |= PD_LOCKED;
1674 * Grab a reference for existing prisons, to ensure they
1675 * continue to exist for the duration of the call.
1678 drflags |= PD_DEREF;
1679 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1680 if ((pr->pr_flags & PR_VNET) &&
1681 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1684 "vnet jails cannot have IP address restrictions");
1689 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1692 "ip4 cannot be changed after creation");
1697 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1700 "ip6 cannot be changed after creation");
1706 /* Do final error checking before setting anything. */
1708 if (slevel < ppr->pr_securelevel) {
1714 if (childmax >= ppr->pr_childmax) {
1720 if (enforce < ppr->pr_enforce_statfs) {
1727 * devfs_rsnum is a uint16_t
1729 if (rsnum < 0 || rsnum > 65535) {
1734 * Nested jails always inherit parent's devfs ruleset
1736 if (jailed(td->td_ucred)) {
1737 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1741 rsnum = ppr->pr_devfs_rsnum;
1746 if ((ppr->pr_flags & PR_IP4) &&
1747 !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1752 if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
1754 vfs_opterror(opts, "IPv4 addresses clash");
1761 if ((ppr->pr_flags & PR_IP6) &&
1762 !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1767 if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
1769 vfs_opterror(opts, "IPv6 addresses clash");
1774 onamelen = namelen = 0;
1775 if (namelc != NULL) {
1776 /* Give a default name of the jid. Also allow the name to be
1777 * explicitly the jid - but not any other number, and only in
1778 * normal form (no leading zero/etc).
1780 if (namelc[0] == '\0')
1781 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1782 else if ((strtoul(namelc, &p, 10) != jid ||
1783 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1786 "name cannot be numeric (unless it is the jid)");
1790 * Make sure the name isn't too long for the prison or its
1793 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1794 onamelen = strlen(pr->pr_name + pnamelen);
1795 namelen = strlen(namelc);
1796 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1797 error = ENAMETOOLONG;
1800 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1801 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1802 sizeof(pr->pr_name)) {
1803 error = ENAMETOOLONG;
1808 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1809 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1815 * Let modules check their parameters. This requires unlocking and
1816 * then re-locking the prison, but this is still a valid state as long
1817 * as allprison_lock remains xlocked.
1819 mtx_unlock(&pr->pr_mtx);
1820 drflags &= ~PD_LOCKED;
1821 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1824 mtx_lock(&pr->pr_mtx);
1825 drflags |= PD_LOCKED;
1827 /* At this point, all valid parameters should have been noted. */
1828 TAILQ_FOREACH(opt, opts, link) {
1829 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1831 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1836 /* Set the parameters of the prison. */
1839 if (pr_flags & PR_IP4_USER) {
1840 pr->pr_flags |= PR_IP4;
1841 prison_ip_set(pr, PR_INET, ip4);
1843 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1845 if (tpr->pr_flags & PR_VNET) {
1850 if (prison_ip_restrict(tpr, PR_INET, NULL)) {
1859 if (pr_flags & PR_IP6_USER) {
1860 pr->pr_flags |= PR_IP6;
1861 prison_ip_set(pr, PR_INET6, ip6);
1863 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1865 if (tpr->pr_flags & PR_VNET) {
1870 if (prison_ip_restrict(tpr, PR_INET6, NULL)) {
1878 pr->pr_securelevel = slevel;
1879 /* Set all child jails to be at least this level. */
1880 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1881 if (tpr->pr_securelevel < slevel)
1882 tpr->pr_securelevel = slevel;
1885 pr->pr_childmax = childmax;
1886 /* Set all child jails to under this limit. */
1887 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1888 if (tpr->pr_childmax > childmax - level)
1889 tpr->pr_childmax = childmax > level
1890 ? childmax - level : 0;
1893 pr->pr_enforce_statfs = enforce;
1894 /* Pass this restriction on to the children. */
1895 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1896 if (tpr->pr_enforce_statfs < enforce)
1897 tpr->pr_enforce_statfs = enforce;
1900 pr->pr_devfs_rsnum = rsnum;
1901 /* Pass this restriction on to the children. */
1902 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1903 tpr->pr_devfs_rsnum = rsnum;
1905 if (namelc != NULL) {
1906 if (ppr == &prison0)
1907 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1909 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1910 ppr->pr_name, namelc);
1911 /* Change this component of child names. */
1912 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1913 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1914 strlen(tpr->pr_name + onamelen) + 1);
1915 bcopy(pr->pr_name, tpr->pr_name, namelen);
1919 /* Try to keep a real-rooted full pathname. */
1920 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1924 if (PR_HOST & ch_flags & ~pr_flags) {
1925 if (pr->pr_flags & PR_HOST) {
1927 * Copy the parent's host info. As with pr_ip4 above,
1928 * the lack of a lock on the parent is not a problem;
1929 * it is always set with allprison_lock at least
1930 * shared, and is held exclusively here.
1932 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1933 sizeof(pr->pr_hostname));
1934 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1935 sizeof(pr->pr_domainname));
1936 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1937 sizeof(pr->pr_hostuuid));
1938 pr->pr_hostid = pr->pr_parent->pr_hostid;
1940 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1941 /* Set this prison, and any descendants without PR_HOST. */
1943 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1945 strlcpy(pr->pr_domainname, domain,
1946 sizeof(pr->pr_domainname));
1948 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1950 pr->pr_hostid = hid;
1951 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1952 if (tpr->pr_flags & PR_HOST)
1956 strlcpy(tpr->pr_hostname,
1958 sizeof(tpr->pr_hostname));
1960 strlcpy(tpr->pr_domainname,
1962 sizeof(tpr->pr_domainname));
1964 strlcpy(tpr->pr_hostuuid,
1966 sizeof(tpr->pr_hostuuid));
1968 tpr->pr_hostid = hid;
1972 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1973 if ((tallow = ch_allow & ~pr_allow))
1974 prison_set_allow_locked(pr, tallow, 0);
1976 * Persistent prisons get an extra reference, and prisons losing their
1977 * persist flag lose that reference.
1979 born = !prison_isalive(pr);
1980 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1981 if (pr_flags & PR_PERSIST) {
1984 * This may make a dead prison alive again, but wait
1985 * to label it as such until after OSD calls have had
1986 * a chance to run (and perhaps to fail).
1988 refcount_acquire(&pr->pr_uref);
1990 drflags |= PD_DEUREF;
1991 prison_free_not_last(pr);
1994 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1995 mtx_unlock(&pr->pr_mtx);
1996 drflags &= ~PD_LOCKED;
1998 * Any errors past this point will need to de-persist newly created
1999 * prisons, as well as call remove methods.
2005 if (racct_enable && created)
2006 prison_racct_attach(pr);
2009 /* Locks may have prevented a complete restriction of child IP
2010 * addresses. If so, allocate some more memory and try again.
2014 ip4s = pr->pr_addrs[PR_INET]->ips;
2015 ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
2016 mtx_lock(&pr->pr_mtx);
2018 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2020 if (tpr->pr_flags & PR_VNET) {
2025 if (prison_ip_restrict(tpr, PR_INET, ip4)) {
2032 mtx_unlock(&pr->pr_mtx);
2037 ip6s = pr->pr_addrs[PR_INET6]->ips;
2038 ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
2039 mtx_lock(&pr->pr_mtx);
2041 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2043 if (tpr->pr_flags & PR_VNET) {
2048 if (prison_ip_restrict(tpr, PR_INET6, ip6)) {
2055 mtx_unlock(&pr->pr_mtx);
2059 /* Let the modules do their work. */
2061 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2065 error = osd_jail_call(pr, PR_METHOD_SET, opts);
2070 * A new prison is now ready to be seen; either it has gained a user
2071 * reference via persistence, or is about to gain one via attachment.
2074 drflags = prison_lock_xlock(pr, drflags);
2075 pr->pr_state = PRISON_STATE_ALIVE;
2078 /* Attach this process to the prison if requested. */
2079 if (flags & JAIL_ATTACH) {
2080 error = do_jail_attach(td, pr,
2081 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
2082 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2084 vfs_opterror(opts, "attach failed");
2090 if (racct_enable && !created) {
2091 if (drflags & PD_LOCKED) {
2092 mtx_unlock(&pr->pr_mtx);
2093 drflags &= ~PD_LOCKED;
2095 if (drflags & PD_LIST_XLOCKED) {
2096 sx_xunlock(&allprison_lock);
2097 drflags &= ~PD_LIST_XLOCKED;
2099 prison_racct_modify(pr);
2103 drflags &= ~PD_KILL;
2104 td->td_retval[0] = pr->pr_id;
2107 /* Release any temporary prison holds and/or locks. */
2109 prison_deref(pr, drflags);
2110 else if (drflags & PD_LIST_SLOCKED)
2111 sx_sunlock(&allprison_lock);
2112 else if (drflags & PD_LIST_XLOCKED)
2113 sx_xunlock(&allprison_lock);
2118 /* Write the error message back to userspace. */
2119 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2120 &errmsg_len) == 0 && errmsg_len > 0) {
2121 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2122 if (optuio->uio_segflg == UIO_SYSSPACE)
2124 optuio->uio_iov[errmsg_pos].iov_base,
2128 optuio->uio_iov[errmsg_pos].iov_base,
2134 prison_ip_free(ip4);
2137 prison_ip_free(ip6);
2140 free(g_path, M_TEMP);
2146 * Find the next available prison ID. Return the ID on success, or zero
2147 * on failure. Also set a pointer to the allprison list entry the prison
2148 * should be inserted before.
2151 get_next_prid(struct prison **insprp)
2153 struct prison *inspr;
2156 jid = lastprid % JAIL_MAX + 1;
2157 if (TAILQ_EMPTY(&allprison) ||
2158 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
2160 * A common case is for all jails to be implicitly numbered,
2161 * which means they'll go on the end of the list, at least
2162 * for the first JAIL_MAX times.
2167 * Take two passes through the allprison list: first starting
2168 * with the proposed jid, then ending with it.
2170 for (maxid = JAIL_MAX; maxid != 0; ) {
2171 TAILQ_FOREACH(inspr, &allprison, pr_list) {
2172 if (inspr->pr_id < jid)
2174 if (inspr->pr_id > jid) {
2175 /* Found an opening. */
2179 if (++jid > maxid) {
2180 if (lastprid == maxid || lastprid == 0)
2183 * The entire legal range
2184 * has been traversed
2188 /* Try again from the start. */
2194 if (inspr == NULL) {
2195 /* Found room at the end of the list. */
2206 * struct jail_get_args {
2207 * struct iovec *iovp;
2208 * unsigned int iovcnt;
2213 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2218 /* Check that we have an even number of iovecs. */
2219 if (uap->iovcnt & 1)
2222 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2225 error = kern_jail_get(td, auio, uap->flags);
2227 error = copyout(auio->uio_iov, uap->iovp,
2228 uap->iovcnt * sizeof (struct iovec));
2234 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2236 struct bool_flags *bf;
2237 struct jailsys_flags *jsf;
2238 struct prison *pr, *mypr;
2240 struct vfsoptlist *opts;
2241 char *errmsg, *name;
2242 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2245 if (flags & ~JAIL_GET_MASK)
2248 /* Get the parameter list. */
2249 error = vfs_buildopts(optuio, &opts);
2252 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2253 mypr = td->td_ucred->cr_prison;
2257 * Find the prison specified by one of: lastjid, jid, name.
2259 sx_slock(&allprison_lock);
2260 drflags = PD_LIST_SLOCKED;
2261 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2263 TAILQ_FOREACH(pr, &allprison, pr_list) {
2264 if (pr->pr_id > jid &&
2265 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2266 prison_ischild(mypr, pr)) {
2267 mtx_lock(&pr->pr_mtx);
2268 drflags |= PD_LOCKED;
2273 vfs_opterror(opts, "no jail after %d", jid);
2275 } else if (error != ENOENT)
2278 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2281 pr = prison_find_child(mypr, jid);
2283 drflags |= PD_LOCKED;
2284 if (!(prison_isalive(pr) ||
2285 (flags & JAIL_DYING))) {
2287 vfs_opterror(opts, "jail %d is dying",
2294 vfs_opterror(opts, "jail %d not found", jid);
2297 } else if (error != ENOENT)
2300 error = vfs_getopt(opts, "name", (void **)&name, &len);
2302 if (len == 0 || name[len - 1] != '\0') {
2306 pr = prison_find_name(mypr, name);
2308 drflags |= PD_LOCKED;
2309 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2311 vfs_opterror(opts, "jail \"%s\" is dying",
2318 vfs_opterror(opts, "jail \"%s\" not found", name);
2320 } else if (error != ENOENT)
2323 vfs_opterror(opts, "no jail specified");
2328 /* Get the parameters of the prison. */
2330 drflags |= PD_DEREF;
2331 td->td_retval[0] = pr->pr_id;
2332 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2333 if (error != 0 && error != ENOENT)
2335 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2336 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2337 if (error != 0 && error != ENOENT)
2339 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2340 if (error != 0 && error != ENOENT)
2342 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2343 sizeof(pr->pr_cpuset->cs_id));
2344 if (error != 0 && error != ENOENT)
2346 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2347 if (error != 0 && error != ENOENT)
2350 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1,
2351 pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2352 pr_families[PR_INET].size : 0 );
2353 if (error != 0 && error != ENOENT)
2357 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1,
2358 pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2359 pr_families[PR_INET6].size : 0 );
2360 if (error != 0 && error != ENOENT)
2363 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2364 sizeof(pr->pr_securelevel));
2365 if (error != 0 && error != ENOENT)
2367 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2368 sizeof(pr->pr_childcount));
2369 if (error != 0 && error != ENOENT)
2371 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2372 sizeof(pr->pr_childmax));
2373 if (error != 0 && error != ENOENT)
2375 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2376 if (error != 0 && error != ENOENT)
2378 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2379 if (error != 0 && error != ENOENT)
2381 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2382 if (error != 0 && error != ENOENT)
2384 #ifdef COMPAT_FREEBSD32
2385 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2386 uint32_t hid32 = pr->pr_hostid;
2388 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2391 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2392 sizeof(pr->pr_hostid));
2393 if (error != 0 && error != ENOENT)
2395 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2396 sizeof(pr->pr_enforce_statfs));
2397 if (error != 0 && error != ENOENT)
2399 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2400 sizeof(pr->pr_devfs_rsnum));
2401 if (error != 0 && error != ENOENT)
2403 for (bf = pr_flag_bool;
2404 bf < pr_flag_bool + nitems(pr_flag_bool);
2406 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2407 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2408 if (error != 0 && error != ENOENT)
2411 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2412 if (error != 0 && error != ENOENT)
2415 for (jsf = pr_flag_jailsys;
2416 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2418 f = pr->pr_flags & (jsf->disable | jsf->new);
2419 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2420 : (f == jsf->new) ? JAIL_SYS_NEW
2422 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2423 if (error != 0 && error != ENOENT)
2426 for (bf = pr_flag_allow;
2427 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2428 atomic_load_int(&bf->flag) != 0;
2430 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2431 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2432 if (error != 0 && error != ENOENT)
2435 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2436 if (error != 0 && error != ENOENT)
2439 i = !prison_isalive(pr);
2440 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2441 if (error != 0 && error != ENOENT)
2444 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2445 if (error != 0 && error != ENOENT)
2447 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2448 sizeof(pr->pr_osreldate));
2449 if (error != 0 && error != ENOENT)
2451 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2452 if (error != 0 && error != ENOENT)
2455 /* Get the module parameters. */
2456 mtx_unlock(&pr->pr_mtx);
2457 drflags &= ~PD_LOCKED;
2458 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2461 prison_deref(pr, drflags);
2465 /* By now, all parameters should have been noted. */
2466 TAILQ_FOREACH(opt, opts, link) {
2467 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2469 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2474 /* Write the fetched parameters back to userspace. */
2476 TAILQ_FOREACH(opt, opts, link) {
2477 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2478 pos = 2 * opt->pos + 1;
2479 optuio->uio_iov[pos].iov_len = opt->len;
2480 if (opt->value != NULL) {
2481 if (optuio->uio_segflg == UIO_SYSSPACE) {
2483 optuio->uio_iov[pos].iov_base,
2486 error = copyout(opt->value,
2487 optuio->uio_iov[pos].iov_base,
2497 /* Release any temporary prison holds and/or locks. */
2499 prison_deref(pr, drflags);
2500 else if (drflags & PD_LIST_SLOCKED)
2501 sx_sunlock(&allprison_lock);
2502 if (error && errmsg_pos >= 0) {
2503 /* Write the error message back to userspace. */
2504 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2505 errmsg_pos = 2 * errmsg_pos + 1;
2506 if (errmsg_len > 0) {
2507 if (optuio->uio_segflg == UIO_SYSSPACE)
2509 optuio->uio_iov[errmsg_pos].iov_base,
2513 optuio->uio_iov[errmsg_pos].iov_base,
2522 * struct jail_remove_args {
2527 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2532 error = priv_check(td, PRIV_JAIL_REMOVE);
2536 sx_xlock(&allprison_lock);
2537 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2539 sx_xunlock(&allprison_lock);
2542 if (!prison_isalive(pr)) {
2543 /* Silently ignore already-dying prisons. */
2544 mtx_unlock(&pr->pr_mtx);
2545 sx_xunlock(&allprison_lock);
2548 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2553 * struct jail_attach_args {
2558 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2563 error = priv_check(td, PRIV_JAIL_ATTACH);
2567 sx_slock(&allprison_lock);
2568 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2570 sx_sunlock(&allprison_lock);
2574 /* Do not allow a process to attach to a prison that is not alive. */
2575 if (!prison_isalive(pr)) {
2576 mtx_unlock(&pr->pr_mtx);
2577 sx_sunlock(&allprison_lock);
2581 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2585 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2588 struct ucred *newcred, *oldcred;
2591 mtx_assert(&pr->pr_mtx, MA_OWNED);
2592 sx_assert(&allprison_lock, SX_LOCKED);
2593 drflags &= PD_LOCK_FLAGS;
2595 * XXX: Note that there is a slight race here if two threads
2596 * in the same privileged process attempt to attach to two
2597 * different jails at the same time. It is important for
2598 * user processes not to do this, or they might end up with
2599 * a process root from one prison, but attached to the jail
2603 refcount_acquire(&pr->pr_uref);
2604 drflags |= PD_DEREF | PD_DEUREF;
2605 mtx_unlock(&pr->pr_mtx);
2606 drflags &= ~PD_LOCKED;
2608 /* Let modules do whatever they need to prepare for attaching. */
2609 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2611 prison_deref(pr, drflags);
2614 sx_unlock(&allprison_lock);
2615 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2618 * Reparent the newly attached process to this jail.
2621 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2625 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2626 if ((error = change_dir(pr->pr_root, td)) != 0)
2629 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2632 VOP_UNLOCK(pr->pr_root);
2633 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2638 oldcred = crcopysafe(p, newcred);
2639 newcred->cr_prison = pr;
2640 proc_set_cred(p, newcred);
2643 racct_proc_ucred_changed(p, oldcred, newcred);
2648 rctl_proc_ucred_changed(p, newcred);
2651 prison_deref(oldcred->cr_prison, drflags);
2655 * If the prison was killed while changing credentials, die along
2658 if (!prison_isalive(pr)) {
2660 kern_psignal(p, SIGKILL);
2667 VOP_UNLOCK(pr->pr_root);
2669 /* Tell modules this thread is still in its old jail after all. */
2670 sx_slock(&allprison_lock);
2671 drflags |= PD_LIST_SLOCKED;
2672 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2673 prison_deref(pr, drflags);
2678 * Returns a locked prison instance, or NULL on failure.
2681 prison_find(int prid)
2685 sx_assert(&allprison_lock, SX_LOCKED);
2686 TAILQ_FOREACH(pr, &allprison, pr_list) {
2687 if (pr->pr_id < prid)
2689 if (pr->pr_id > prid)
2691 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2692 mtx_lock(&pr->pr_mtx);
2699 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2702 prison_find_child(struct prison *mypr, int prid)
2707 sx_assert(&allprison_lock, SX_LOCKED);
2708 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2709 if (pr->pr_id == prid) {
2710 KASSERT(prison_isvalid(pr),
2711 ("Found invalid prison %p", pr));
2712 mtx_lock(&pr->pr_mtx);
2720 * Look for the name relative to mypr. Returns a locked prison or NULL.
2723 prison_find_name(struct prison *mypr, const char *name)
2725 struct prison *pr, *deadpr;
2729 sx_assert(&allprison_lock, SX_LOCKED);
2730 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2732 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2733 if (!strcmp(pr->pr_name + mylen, name)) {
2734 KASSERT(prison_isvalid(pr),
2735 ("Found invalid prison %p", pr));
2736 if (prison_isalive(pr)) {
2737 mtx_lock(&pr->pr_mtx);
2743 /* There was no valid prison - perhaps there was a dying one. */
2745 mtx_lock(&deadpr->pr_mtx);
2750 * See if a prison has the specific flag set. The prison should be locked,
2751 * unless checking for flags that are only set at jail creation (such as
2752 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2753 * to any other prison data.
2756 prison_flag(struct ucred *cred, unsigned flag)
2759 return (cred->cr_prison->pr_flags & flag);
2763 prison_allow(struct ucred *cred, unsigned flag)
2766 return ((cred->cr_prison->pr_allow & flag) != 0);
2770 * Hold a prison reference, by incrementing pr_ref. It is generally
2771 * an error to hold a prison that does not already have a reference.
2772 * A prison record will remain valid as long as it has at least one
2773 * reference, and will not be removed as long as either the prison
2774 * mutex or the allprison lock is held (allprison_lock may be shared).
2777 prison_hold_locked(struct prison *pr)
2780 /* Locking is no longer required. */
2785 prison_hold(struct prison *pr)
2788 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2791 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2793 refcount_acquire(&pr->pr_ref);
2798 * Remove a prison reference. If that was the last reference, the
2799 * prison will be removed (at a later time).
2802 prison_free_locked(struct prison *pr)
2805 mtx_assert(&pr->pr_mtx, MA_OWNED);
2807 * Locking is no longer required, but unlock because the caller
2810 mtx_unlock(&pr->pr_mtx);
2815 prison_free(struct prison *pr)
2818 KASSERT(refcount_load(&pr->pr_ref) > 0,
2819 ("Trying to free dead prison %p (jid=%d).",
2821 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2823 * Don't remove the last reference in this context,
2824 * in case there are locks held.
2826 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2831 prison_free_not_last(struct prison *pr)
2836 KASSERT(refcount_load(&pr->pr_ref) > 0,
2837 ("Trying to free dead prison %p (jid=%d).",
2839 lastref = refcount_release(&pr->pr_ref);
2841 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2844 refcount_release(&pr->pr_ref);
2849 * Hold a prison for user visibility, by incrementing pr_uref.
2850 * It is generally an error to hold a prison that isn't already
2851 * user-visible, except through the the jail system calls. It is also
2852 * an error to hold an invalid prison. A prison record will remain
2853 * alive as long as it has at least one user reference, and will not
2854 * be set to the dying state until the prison mutex and allprison_lock
2858 prison_proc_hold(struct prison *pr)
2861 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2864 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2866 refcount_acquire(&pr->pr_uref);
2871 * Remove a prison user reference. If it was the last reference, the
2872 * prison will be considered "dying", and may be removed once all of
2873 * its references are dropped.
2876 prison_proc_free(struct prison *pr)
2880 * Locking is only required when releasing the last reference.
2881 * This allows assurance that a locked prison will remain alive
2882 * until it is unlocked.
2884 KASSERT(refcount_load(&pr->pr_uref) > 0,
2885 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2886 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2888 * Don't remove the last user reference in this context,
2889 * which is expected to be a process that is not only locked,
2890 * but also half dead. Add a reference so any calls to
2891 * prison_free() won't re-submit the task.
2894 mtx_lock(&pr->pr_mtx);
2895 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2896 ("Redundant last reference in prison_proc_free (jid=%d)",
2898 pr->pr_flags |= PR_COMPLETE_PROC;
2899 mtx_unlock(&pr->pr_mtx);
2900 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2905 prison_proc_free_not_last(struct prison *pr)
2910 KASSERT(refcount_load(&pr->pr_uref) > 0,
2911 ("Trying to free dead prison %p (jid=%d).",
2913 lastref = refcount_release(&pr->pr_uref);
2915 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2918 refcount_release(&pr->pr_uref);
2923 * Complete a call to either prison_free or prison_proc_free.
2926 prison_complete(void *context, int pending)
2928 struct prison *pr = context;
2932 * This could be called to release the last reference, or the last
2933 * user reference (plus the reference held in prison_proc_free).
2935 drflags = prison_lock_xlock(pr, PD_DEREF);
2936 if (pr->pr_flags & PR_COMPLETE_PROC) {
2937 pr->pr_flags &= ~PR_COMPLETE_PROC;
2938 drflags |= PD_DEUREF;
2940 prison_deref(pr, drflags);
2944 * Remove a prison reference and/or user reference (usually).
2945 * This assumes context that allows sleeping (for allprison_lock),
2946 * with no non-sleeping locks held, except perhaps the prison itself.
2947 * If there are no more references, release and delist the prison.
2948 * On completion, the prison lock and the allprison lock are both
2952 prison_deref(struct prison *pr, int flags)
2954 struct prisonlist freeprison;
2955 struct prison *killpr, *rpr, *ppr, *tpr;
2959 TAILQ_INIT(&freeprison);
2961 * Release this prison as requested, which may cause its parent
2962 * to be released, and then maybe its grandparent, etc.
2965 if (flags & PD_KILL) {
2966 /* Kill the prison and its descendents. */
2967 KASSERT(pr != &prison0,
2968 ("prison_deref trying to kill prison0"));
2969 if (!(flags & PD_DEREF)) {
2973 flags = prison_lock_xlock(pr, flags);
2974 prison_deref_kill(pr, &freeprison);
2976 if (flags & PD_DEUREF) {
2977 /* Drop a user reference. */
2978 KASSERT(refcount_load(&pr->pr_uref) > 0,
2979 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2981 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2982 if (!(flags & PD_DEREF)) {
2986 flags = prison_lock_xlock(pr, flags);
2987 if (refcount_release(&pr->pr_uref) &&
2988 pr->pr_state == PRISON_STATE_ALIVE) {
2990 * When the last user references goes,
2991 * this becomes a dying prison.
2994 refcount_load(&prison0.pr_uref) > 0,
2995 ("prison0 pr_uref=0"));
2996 pr->pr_state = PRISON_STATE_DYING;
2997 mtx_unlock(&pr->pr_mtx);
2998 flags &= ~PD_LOCKED;
3003 if (flags & PD_KILL) {
3005 * Any remaining user references are probably processes
3006 * that need to be killed, either in this prison or its
3009 if (refcount_load(&pr->pr_uref) > 0)
3011 /* Make sure the parent prison doesn't get killed. */
3014 if (flags & PD_DEREF) {
3015 /* Drop a reference. */
3016 KASSERT(refcount_load(&pr->pr_ref) > 0,
3017 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
3019 if (!refcount_release_if_not_last(&pr->pr_ref)) {
3020 flags = prison_lock_xlock(pr, flags);
3021 if (refcount_release(&pr->pr_ref)) {
3023 * When the last reference goes,
3024 * unlink the prison and set it aside.
3027 refcount_load(&pr->pr_uref) == 0,
3028 ("prison_deref: last ref, "
3029 "but still has %d urefs (jid=%d)",
3030 pr->pr_uref, pr->pr_id));
3032 refcount_load(&prison0.pr_ref) != 0,
3033 ("prison0 pr_ref=0"));
3034 pr->pr_state = PRISON_STATE_INVALID;
3035 TAILQ_REMOVE(&allprison, pr, pr_list);
3036 LIST_REMOVE(pr, pr_sibling);
3037 TAILQ_INSERT_TAIL(&freeprison, pr,
3039 for (ppr = pr->pr_parent;
3041 ppr = ppr->pr_parent)
3042 ppr->pr_childcount--;
3044 * Removing a prison frees references
3047 mtx_unlock(&pr->pr_mtx);
3048 flags &= ~PD_LOCKED;
3050 flags |= PD_DEREF | PD_DEUREF;
3058 /* Release all the prison locks. */
3059 if (flags & PD_LOCKED)
3060 mtx_unlock(&pr->pr_mtx);
3061 if (flags & PD_LIST_SLOCKED)
3062 sx_sunlock(&allprison_lock);
3063 else if (flags & PD_LIST_XLOCKED)
3064 sx_xunlock(&allprison_lock);
3066 /* Kill any processes attached to a killed prison. */
3067 if (killpr != NULL) {
3068 sx_slock(&allproc_lock);
3069 FOREACH_PROC_IN_SYSTEM(p) {
3071 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
3072 for (ppr = p->p_ucred->cr_prison;
3074 ppr = ppr->pr_parent)
3075 if (ppr == killpr) {
3076 kern_psignal(p, SIGKILL);
3082 sx_sunlock(&allproc_lock);
3086 * Finish removing any unreferenced prisons, which couldn't happen
3087 * while allprison_lock was held (to avoid a LOR on vrele).
3089 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
3091 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
3092 vnet_destroy(rpr->pr_vnet);
3094 if (rpr->pr_root != NULL)
3095 vrele(rpr->pr_root);
3096 mtx_destroy(&rpr->pr_mtx);
3098 prison_ip_free(rpr->pr_addrs[PR_INET]);
3101 prison_ip_free(rpr->pr_addrs[PR_INET6]);
3103 if (rpr->pr_cpuset != NULL)
3104 cpuset_rel(rpr->pr_cpuset);
3108 prison_racct_detach(rpr);
3110 TAILQ_REMOVE(&freeprison, rpr, pr_list);
3111 free(rpr, M_PRISON);
3116 * Kill the prison and its descendants. Mark them as dying, clear the
3117 * persist flag, and call module remove methods.
3120 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3122 struct prison *cpr, *ppr, *rpr;
3126 * Unlike the descendants, the target prison can be killed
3127 * even if it is currently dying. This is useful for failed
3128 * creation in jail_set(2).
3130 KASSERT(refcount_load(&pr->pr_ref) > 0,
3131 ("Trying to kill dead prison %p (jid=%d).",
3133 refcount_acquire(&pr->pr_uref);
3134 pr->pr_state = PRISON_STATE_DYING;
3135 mtx_unlock(&pr->pr_mtx);
3138 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3140 if (!prison_isalive(cpr)) {
3145 prison_proc_hold(cpr);
3146 mtx_lock(&cpr->pr_mtx);
3147 cpr->pr_state = PRISON_STATE_DYING;
3148 cpr->pr_flags |= PR_REMOVE;
3149 mtx_unlock(&cpr->pr_mtx);
3152 if (!(cpr->pr_flags & PR_REMOVE))
3154 prison_cleanup(cpr);
3155 mtx_lock(&cpr->pr_mtx);
3156 cpr->pr_flags &= ~PR_REMOVE;
3157 if (cpr->pr_flags & PR_PERSIST) {
3158 cpr->pr_flags &= ~PR_PERSIST;
3159 prison_proc_free_not_last(cpr);
3160 prison_free_not_last(cpr);
3162 (void)refcount_release(&cpr->pr_uref);
3163 if (refcount_release(&cpr->pr_ref)) {
3165 * When the last reference goes, unlink the prison
3166 * and set it aside for prison_deref() to handle.
3167 * Delay unlinking the sibling list to keep the loop
3171 LIST_REMOVE(rpr, pr_sibling);
3173 rpr->pr_state = PRISON_STATE_INVALID;
3174 TAILQ_REMOVE(&allprison, rpr, pr_list);
3175 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3177 * Removing a prison frees references from its parent.
3179 ppr = rpr->pr_parent;
3180 prison_proc_free_not_last(ppr);
3181 prison_free_not_last(ppr);
3182 for (; ppr != NULL; ppr = ppr->pr_parent)
3183 ppr->pr_childcount--;
3185 mtx_unlock(&cpr->pr_mtx);
3188 LIST_REMOVE(rpr, pr_sibling);
3191 mtx_lock(&pr->pr_mtx);
3192 if (pr->pr_flags & PR_PERSIST) {
3193 pr->pr_flags &= ~PR_PERSIST;
3194 prison_proc_free_not_last(pr);
3195 prison_free_not_last(pr);
3197 (void)refcount_release(&pr->pr_uref);
3201 * Given the current locking state in the flags, make sure allprison_lock
3202 * is held exclusive, and the prison is locked. Return flags indicating
3206 prison_lock_xlock(struct prison *pr, int flags)
3209 if (!(flags & PD_LIST_XLOCKED)) {
3211 * Get allprison_lock, which may be an upgrade,
3212 * and may require unlocking the prison.
3214 if (flags & PD_LOCKED) {
3215 mtx_unlock(&pr->pr_mtx);
3216 flags &= ~PD_LOCKED;
3218 if (flags & PD_LIST_SLOCKED) {
3219 if (!sx_try_upgrade(&allprison_lock)) {
3220 sx_sunlock(&allprison_lock);
3221 sx_xlock(&allprison_lock);
3223 flags &= ~PD_LIST_SLOCKED;
3225 sx_xlock(&allprison_lock);
3226 flags |= PD_LIST_XLOCKED;
3228 if (!(flags & PD_LOCKED)) {
3229 /* Lock the prison mutex. */
3230 mtx_lock(&pr->pr_mtx);
3237 * Release a prison's resources when it starts dying (when the last user
3238 * reference is dropped, or when it is killed).
3241 prison_cleanup(struct prison *pr)
3243 sx_assert(&allprison_lock, SA_XLOCKED);
3244 mtx_assert(&pr->pr_mtx, MA_NOTOWNED);
3245 shm_remove_prison(pr);
3246 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3250 * Set or clear a permission bit in the pr_allow field, passing restrictions
3251 * (cleared permission) down to child jails.
3254 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3258 pr = cred->cr_prison;
3259 sx_slock(&allprison_lock);
3260 mtx_lock(&pr->pr_mtx);
3261 prison_set_allow_locked(pr, flag, enable);
3262 mtx_unlock(&pr->pr_mtx);
3263 sx_sunlock(&allprison_lock);
3267 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3273 pr->pr_allow |= flag;
3275 pr->pr_allow &= ~flag;
3276 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3277 cpr->pr_allow &= ~flag;
3282 * Check if a jail supports the given address family.
3284 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3288 prison_check_af(struct ucred *cred, int af)
3293 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3295 pr = cred->cr_prison;
3297 /* Prisons with their own network stack are not limited. */
3298 if (prison_owns_vnet(cred))
3307 if (pr->pr_flags & PR_IP4)
3309 mtx_lock(&pr->pr_mtx);
3310 if ((pr->pr_flags & PR_IP4) &&
3311 pr->pr_addrs[PR_INET] == NULL)
3312 error = EAFNOSUPPORT;
3313 mtx_unlock(&pr->pr_mtx);
3319 if (pr->pr_flags & PR_IP6)
3321 mtx_lock(&pr->pr_mtx);
3322 if ((pr->pr_flags & PR_IP6) &&
3323 pr->pr_addrs[PR_INET6] == NULL)
3324 error = EAFNOSUPPORT;
3325 mtx_unlock(&pr->pr_mtx);
3333 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3334 error = EAFNOSUPPORT;
3340 * Check if given address belongs to the jail referenced by cred (wrapper to
3341 * prison_check_ip[46]).
3343 * Returns 0 if jail doesn't restrict the address family or if address belongs
3344 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3345 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3348 prison_if(struct ucred *cred, const struct sockaddr *sa)
3351 const struct sockaddr_in *sai;
3354 const struct sockaddr_in6 *sai6;
3358 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3359 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3362 if (prison_owns_vnet(cred))
3367 switch (sa->sa_family)
3371 sai = (const struct sockaddr_in *)sa;
3372 error = prison_check_ip4(cred, &sai->sin_addr);
3377 sai6 = (const struct sockaddr_in6 *)sa;
3378 error = prison_check_ip6(cred, &sai6->sin6_addr);
3382 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3383 error = EAFNOSUPPORT;
3389 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3392 prison_check(struct ucred *cred1, struct ucred *cred2)
3395 return ((cred1->cr_prison == cred2->cr_prison ||
3396 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3400 * Return 1 if p2 is a child of p1, otherwise 0.
3403 prison_ischild(struct prison *pr1, struct prison *pr2)
3406 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3413 * Return true if the prison is currently alive. A prison is alive if it
3414 * holds user references and it isn't being removed.
3417 prison_isalive(const struct prison *pr)
3420 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3426 * Return true if the prison is currently valid. A prison is valid if it has
3427 * been fully created, and is not being destroyed. Note that dying prisons
3428 * are still considered valid. Invalid prisons won't be found under normal
3429 * circumstances, as they're only put in that state by functions that have
3430 * an exclusive hold on allprison_lock.
3433 prison_isvalid(struct prison *pr)
3436 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3438 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3444 * Return 1 if the passed credential is in a jail and that jail does not
3445 * have its own virtual network stack, otherwise 0.
3448 jailed_without_vnet(struct ucred *cred)
3454 if (prison_owns_vnet(cred))
3462 * Return the correct hostname (domainname, et al) for the passed credential.
3465 getcredhostname(struct ucred *cred, char *buf, size_t size)
3470 * A NULL credential can be used to shortcut to the physical
3471 * system's hostname.
3473 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3474 mtx_lock(&pr->pr_mtx);
3475 strlcpy(buf, pr->pr_hostname, size);
3476 mtx_unlock(&pr->pr_mtx);
3480 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3483 mtx_lock(&cred->cr_prison->pr_mtx);
3484 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3485 mtx_unlock(&cred->cr_prison->pr_mtx);
3489 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3492 mtx_lock(&cred->cr_prison->pr_mtx);
3493 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3494 mtx_unlock(&cred->cr_prison->pr_mtx);
3498 getcredhostid(struct ucred *cred, unsigned long *hostid)
3501 mtx_lock(&cred->cr_prison->pr_mtx);
3502 *hostid = cred->cr_prison->pr_hostid;
3503 mtx_unlock(&cred->cr_prison->pr_mtx);
3507 getjailname(struct ucred *cred, char *name, size_t len)
3510 mtx_lock(&cred->cr_prison->pr_mtx);
3511 strlcpy(name, cred->cr_prison->pr_name, len);
3512 mtx_unlock(&cred->cr_prison->pr_mtx);
3517 * Determine whether the prison represented by cred owns
3518 * its vnet rather than having it inherited.
3520 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3523 prison_owns_vnet(struct ucred *cred)
3527 * vnets cannot be added/removed after jail creation,
3528 * so no need to lock here.
3530 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3535 * Determine whether the subject represented by cred can "see"
3536 * status of a mount point.
3537 * Returns: 0 for permitted, ENOENT otherwise.
3538 * XXX: This function should be called cr_canseemount() and should be
3539 * placed in kern_prot.c.
3542 prison_canseemount(struct ucred *cred, struct mount *mp)
3548 pr = cred->cr_prison;
3549 if (pr->pr_enforce_statfs == 0)
3551 if (pr->pr_root->v_mount == mp)
3553 if (pr->pr_enforce_statfs == 2)
3556 * If jail's chroot directory is set to "/" we should be able to see
3557 * all mount-points from inside a jail.
3558 * This is ugly check, but this is the only situation when jail's
3559 * directory ends with '/'.
3561 if (strcmp(pr->pr_path, "/") == 0)
3563 len = strlen(pr->pr_path);
3565 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3568 * Be sure that we don't have situation where jail's root directory
3569 * is "/some/path" and mount point is "/some/pathpath".
3571 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3577 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3579 char jpath[MAXPATHLEN];
3583 pr = cred->cr_prison;
3584 if (pr->pr_enforce_statfs == 0)
3586 if (prison_canseemount(cred, mp) != 0) {
3587 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3588 strlcpy(sp->f_mntonname, "[restricted]",
3589 sizeof(sp->f_mntonname));
3592 if (pr->pr_root->v_mount == mp) {
3594 * Clear current buffer data, so we are sure nothing from
3595 * the valid path left there.
3597 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3598 *sp->f_mntonname = '/';
3602 * If jail's chroot directory is set to "/" we should be able to see
3603 * all mount-points from inside a jail.
3605 if (strcmp(pr->pr_path, "/") == 0)
3607 len = strlen(pr->pr_path);
3608 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3610 * Clear current buffer data, so we are sure nothing from
3611 * the valid path left there.
3613 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3614 if (*jpath == '\0') {
3615 /* Should never happen. */
3616 *sp->f_mntonname = '/';
3618 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3623 * Check with permission for a specific privilege is granted within jail. We
3624 * have a specific list of accepted privileges; the rest are denied.
3627 prison_priv_check(struct ucred *cred, int priv)
3633 * Some policies have custom handlers. This routine should not be
3634 * called for them. See priv_check_cred().
3637 case PRIV_VFS_LOOKUP:
3638 case PRIV_VFS_GENERATION:
3639 KASSERT(0, ("prison_priv_check instead of a custom handler "
3640 "called for %d\n", priv));
3648 * Privileges specific to prisons with a virtual network stack.
3649 * There might be a duplicate entry here in case the privilege
3650 * is only granted conditionally in the legacy jail case.
3655 * NFS-specific privileges.
3657 case PRIV_NFS_DAEMON:
3658 case PRIV_NFS_LOCKD:
3661 * Network stack privileges.
3663 case PRIV_NET_BRIDGE:
3666 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3667 case PRIV_NET_ROUTE:
3669 case PRIV_NET_SETIFMTU:
3670 case PRIV_NET_SETIFFLAGS:
3671 case PRIV_NET_SETIFCAP:
3672 case PRIV_NET_SETIFDESCR:
3673 case PRIV_NET_SETIFNAME :
3674 case PRIV_NET_SETIFMETRIC:
3675 case PRIV_NET_SETIFPHYS:
3676 case PRIV_NET_SETIFMAC:
3677 case PRIV_NET_SETLANPCP:
3678 case PRIV_NET_ADDMULTI:
3679 case PRIV_NET_DELMULTI:
3680 case PRIV_NET_HWIOCTL:
3681 case PRIV_NET_SETLLADDR:
3682 case PRIV_NET_ADDIFGROUP:
3683 case PRIV_NET_DELIFGROUP:
3684 case PRIV_NET_IFCREATE:
3685 case PRIV_NET_IFDESTROY:
3686 case PRIV_NET_ADDIFADDR:
3687 case PRIV_NET_DELIFADDR:
3690 case PRIV_NET_SETIFVNET:
3691 case PRIV_NET_SETIFFIB:
3695 * 802.11-related privileges.
3697 case PRIV_NET80211_VAP_GETKEY:
3698 case PRIV_NET80211_VAP_MANAGE:
3704 case PRIV_NETATM_CFG:
3705 case PRIV_NETATM_ADD:
3706 case PRIV_NETATM_DEL:
3707 case PRIV_NETATM_SET:
3710 * Bluetooth privileges.
3712 case PRIV_NETBLUETOOTH_RAW:
3716 * Netgraph and netgraph module privileges.
3718 case PRIV_NETGRAPH_CONTROL:
3720 case PRIV_NETGRAPH_TTY:
3724 * IPv4 and IPv6 privileges.
3726 case PRIV_NETINET_IPFW:
3727 case PRIV_NETINET_DIVERT:
3728 case PRIV_NETINET_PF:
3729 case PRIV_NETINET_DUMMYNET:
3730 case PRIV_NETINET_CARP:
3731 case PRIV_NETINET_MROUTE:
3732 case PRIV_NETINET_RAW:
3733 case PRIV_NETINET_ADDRCTRL6:
3734 case PRIV_NETINET_ND6:
3735 case PRIV_NETINET_SCOPE6:
3736 case PRIV_NETINET_ALIFETIME6:
3737 case PRIV_NETINET_IPSEC:
3738 case PRIV_NETINET_BINDANY:
3753 * No default: or deny here.
3754 * In case of no permit fall through to next switch().
3756 if (cred->cr_prison->pr_flags & PR_VNET)
3763 * Allow ktrace privileges for root in jail.
3769 * Allow jailed processes to configure audit identity and
3770 * submit audit records (login, etc). In the future we may
3771 * want to further refine the relationship between audit and
3774 case PRIV_AUDIT_GETAUDIT:
3775 case PRIV_AUDIT_SETAUDIT:
3776 case PRIV_AUDIT_SUBMIT:
3780 * Allow jailed processes to manipulate process UNIX
3781 * credentials in any way they see fit.
3783 case PRIV_CRED_SETUID:
3784 case PRIV_CRED_SETEUID:
3785 case PRIV_CRED_SETGID:
3786 case PRIV_CRED_SETEGID:
3787 case PRIV_CRED_SETGROUPS:
3788 case PRIV_CRED_SETREUID:
3789 case PRIV_CRED_SETREGID:
3790 case PRIV_CRED_SETRESUID:
3791 case PRIV_CRED_SETRESGID:
3794 * Jail implements visibility constraints already, so allow
3795 * jailed root to override uid/gid-based constraints.
3797 case PRIV_SEEOTHERGIDS:
3798 case PRIV_SEEOTHERUIDS:
3801 * Jail implements inter-process debugging limits already, so
3802 * allow jailed root various debugging privileges.
3804 case PRIV_DEBUG_DIFFCRED:
3805 case PRIV_DEBUG_SUGID:
3806 case PRIV_DEBUG_UNPRIV:
3809 * Allow jail to set various resource limits and login
3810 * properties, and for now, exceed process resource limits.
3812 case PRIV_PROC_LIMIT:
3813 case PRIV_PROC_SETLOGIN:
3814 case PRIV_PROC_SETRLIMIT:
3817 * System V and POSIX IPC privileges are granted in jail.
3820 case PRIV_IPC_WRITE:
3821 case PRIV_IPC_ADMIN:
3822 case PRIV_IPC_MSGSIZE:
3826 * Jail operations within a jail work on child jails.
3828 case PRIV_JAIL_ATTACH:
3830 case PRIV_JAIL_REMOVE:
3833 * Jail implements its own inter-process limits, so allow
3834 * root processes in jail to change scheduling on other
3835 * processes in the same jail. Likewise for signalling.
3837 case PRIV_SCHED_DIFFCRED:
3838 case PRIV_SCHED_CPUSET:
3839 case PRIV_SIGNAL_DIFFCRED:
3840 case PRIV_SIGNAL_SUGID:
3843 * Allow jailed processes to write to sysctls marked as jail
3846 case PRIV_SYSCTL_WRITEJAIL:
3849 * Allow root in jail to manage a variety of quota
3850 * properties. These should likely be conditional on a
3851 * configuration option.
3853 case PRIV_VFS_GETQUOTA:
3854 case PRIV_VFS_SETQUOTA:
3857 * Since Jail relies on chroot() to implement file system
3858 * protections, grant many VFS privileges to root in jail.
3859 * Be careful to exclude mount-related and NFS-related
3863 case PRIV_VFS_WRITE:
3864 case PRIV_VFS_ADMIN:
3866 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3867 case PRIV_VFS_CHFLAGS_DEV:
3868 case PRIV_VFS_CHOWN:
3869 case PRIV_VFS_CHROOT:
3870 case PRIV_VFS_RETAINSUGID:
3871 case PRIV_VFS_FCHROOT:
3873 case PRIV_VFS_SETGID:
3875 case PRIV_VFS_STICKYFILE:
3878 * As in the non-jail case, non-root users are expected to be
3879 * able to read kernel/physical memory (provided /dev/[k]mem
3880 * exists in the jail and they have permission to access it).
3882 case PRIV_KMEM_READ:
3886 * Depending on the global setting, allow privilege of
3887 * setting system flags.
3889 case PRIV_VFS_SYSFLAGS:
3890 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3896 * Depending on the global setting, allow privilege of
3897 * mounting/unmounting file systems.
3899 case PRIV_VFS_MOUNT:
3900 case PRIV_VFS_UNMOUNT:
3901 case PRIV_VFS_MOUNT_NONUSER:
3902 case PRIV_VFS_MOUNT_OWNER:
3903 pr = cred->cr_prison;
3905 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3913 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3914 * policy. priv_check_cred will not specifically allow it, and
3915 * we may want a MAC policy to allow it.
3917 case PRIV_VFS_READ_DIR:
3921 * Conditionnaly allow locking (unlocking) physical pages
3925 case PRIV_VM_MUNLOCK:
3926 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3932 * Conditionally allow jailed root to bind reserved ports.
3934 case PRIV_NETINET_RESERVEDPORT:
3935 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3941 * Allow jailed root to reuse in-use ports.
3943 case PRIV_NETINET_REUSEPORT:
3947 * Allow jailed root to set certain IPv4/6 (option) headers.
3949 case PRIV_NETINET_SETHDROPTS:
3953 * Conditionally allow creating raw sockets in jail.
3955 case PRIV_NETINET_RAW:
3956 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3962 * Since jail implements its own visibility limits on netstat
3963 * sysctls, allow getcred. This allows identd to work in
3966 case PRIV_NETINET_GETCRED:
3970 * Allow jailed root to set loginclass.
3972 case PRIV_PROC_SETLOGINCLASS:
3976 * Do not allow a process inside a jail to read the kernel
3977 * message buffer unless explicitly permitted.
3980 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3986 * In all remaining cases, deny the privilege request. This
3987 * includes almost all network privileges, many system
3988 * configuration privileges.
3995 * Return the part of pr2's name that is relative to pr1, or the whole name
3996 * if it does not directly follow.
4000 prison_name(struct prison *pr1, struct prison *pr2)
4004 /* Jails see themselves as "0" (if they see themselves at all). */
4007 name = pr2->pr_name;
4008 if (prison_ischild(pr1, pr2)) {
4010 * pr1 isn't locked (and allprison_lock may not be either)
4011 * so its length can't be counted on. But the number of dots
4012 * can be counted on - and counted.
4014 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
4015 name = strchr(name, '.') + 1;
4021 * Return the part of pr2's path that is relative to pr1, or the whole path
4022 * if it does not directly follow.
4025 prison_path(struct prison *pr1, struct prison *pr2)
4027 char *path1, *path2;
4030 path1 = pr1->pr_path;
4031 path2 = pr2->pr_path;
4032 if (!strcmp(path1, "/"))
4034 len1 = strlen(path1);
4035 if (strncmp(path1, path2, len1))
4037 if (path2[len1] == '\0')
4039 if (path2[len1] == '/')
4040 return (path2 + len1);
4045 * Jail-related sysctls.
4047 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4050 #if defined(INET) || defined(INET6)
4052 * Copy address array to memory that would be then SYSCTL_OUT-ed.
4053 * sysctl_jail_list() helper.
4056 prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4058 const size_t size = pr_families[af].size;
4061 mtx_assert(&pr->pr_mtx, MA_OWNED);
4062 if (pr->pr_addrs[af] != NULL) {
4063 if (*len < pr->pr_addrs[af]->ips) {
4064 *len = pr->pr_addrs[af]->ips;
4065 mtx_unlock(&pr->pr_mtx);
4066 *out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4067 mtx_lock(&pr->pr_mtx);
4070 bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size);
4076 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4079 struct prison *pr, *cpr;
4081 struct in_addr *ip4 = NULL;
4085 struct in6_addr *ip6 = NULL;
4090 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4091 pr = req->td->td_ucred->cr_prison;
4093 sx_slock(&allprison_lock);
4094 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4095 mtx_lock(&cpr->pr_mtx);
4097 prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4100 prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4102 bzero(xp, sizeof(*xp));
4103 xp->pr_version = XPRISON_VERSION;
4104 xp->pr_id = cpr->pr_id;
4105 xp->pr_state = cpr->pr_state;
4106 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4107 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4108 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4115 mtx_unlock(&cpr->pr_mtx);
4116 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4120 if (xp->pr_ip4s > 0) {
4121 error = SYSCTL_OUT(req, ip4,
4122 xp->pr_ip4s * sizeof(struct in_addr));
4128 if (xp->pr_ip6s > 0) {
4129 error = SYSCTL_OUT(req, ip6,
4130 xp->pr_ip6s * sizeof(struct in6_addr));
4136 sx_sunlock(&allprison_lock);
4147 SYSCTL_OID(_security_jail, OID_AUTO, list,
4148 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4149 sysctl_jail_list, "S", "List of active jails");
4152 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4156 injail = jailed(req->td->td_ucred);
4157 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4162 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4163 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4164 sysctl_jail_jailed, "I", "Process in jail?");
4167 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4169 int error, havevnet;
4171 struct ucred *cred = req->td->td_ucred;
4173 havevnet = jailed(cred) && prison_owns_vnet(cred);
4177 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4182 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4183 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4184 sysctl_jail_vnet, "I", "Jail owns vnet?");
4186 #if defined(INET) || defined(INET6)
4187 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4188 &jail_max_af_ips, 0,
4189 "Number of IP addresses a jail may have at most per address family (deprecated)");
4193 * Default parameters for jail(2) compatibility. For historical reasons,
4194 * the sysctl names have varying similarity to the parameter names. Prisons
4195 * just see their own parameters, and can't change them.
4198 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4202 /* Get the current flag value, and convert it to a boolean. */
4203 if (req->td->td_ucred->cr_prison == &prison0) {
4204 mtx_lock(&prison0.pr_mtx);
4205 i = (jail_default_allow & arg2) != 0;
4206 mtx_unlock(&prison0.pr_mtx);
4208 i = prison_allow(req->td->td_ucred, arg2);
4212 error = sysctl_handle_int(oidp, &i, 0, req);
4213 if (error || !req->newptr)
4219 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4222 mtx_lock(&prison0.pr_mtx);
4223 jail_default_allow = (jail_default_allow & ~arg2) | i;
4224 mtx_unlock(&prison0.pr_mtx);
4228 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4229 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4230 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4231 "Processes in jail can set their hostnames (deprecated)");
4232 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4233 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4234 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4235 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4236 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4237 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4238 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4239 "Processes in jail can use System V IPC primitives (deprecated)");
4240 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4241 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4242 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4243 "Prison root can create raw sockets (deprecated)");
4244 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4245 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4246 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4247 "Processes in jail can alter system file flags (deprecated)");
4248 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4249 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4250 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4251 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4254 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4259 pr = req->td->td_ucred->cr_prison;
4260 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4261 error = sysctl_handle_int(oidp, &level, 0, req);
4262 if (error || !req->newptr)
4264 *(int *)arg1 = level;
4268 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4269 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4270 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4271 sysctl_jail_default_level, "I",
4272 "Processes in jail cannot see all mounted file systems (deprecated)");
4274 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4275 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4276 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4277 sysctl_jail_default_level, "I",
4278 "Ruleset for the devfs filesystem in jail (deprecated)");
4281 * Nodes to describe jail parameters. Maximum length of string parameters
4282 * is returned in the string itself, and the other parameters exist merely
4283 * to make themselves and their types known.
4285 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4289 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4296 switch (oidp->oid_kind & CTLTYPE)
4302 if (!(req->flags & SCTL_MASK32))
4304 return (SYSCTL_OUT(req, &l, sizeof(l)));
4308 return (SYSCTL_OUT(req, &i, sizeof(i)));
4309 case CTLTYPE_STRING:
4310 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4312 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4313 case CTLTYPE_STRUCT:
4315 return (SYSCTL_OUT(req, &s, sizeof(s)));
4321 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4322 * jail creation time but cannot be changed in an existing jail.
4324 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4325 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4326 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4327 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4328 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4329 "I", "Jail secure level");
4330 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4331 "Jail value for kern.osreldate and uname -K");
4332 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4333 "Jail value for kern.osrelease and uname -r");
4334 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4335 "I", "Jail cannot see all mounted file systems");
4336 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4337 "I", "Ruleset for in-jail devfs mounts");
4338 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4339 "B", "Jail persistence");
4341 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4342 "E,jailsys", "Virtual network stack");
4344 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4345 "B", "Jail is in the process of shutting down");
4347 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4348 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4349 "I", "Current number of child jails");
4350 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4351 "I", "Maximum number of child jails");
4353 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4354 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4356 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4357 "Jail NIS domainname");
4358 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4360 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4361 "LU", "Jail host ID");
4363 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4364 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4367 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4368 "Jail IPv4 address virtualization");
4369 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4370 "S,in_addr,a", "Jail IPv4 addresses");
4371 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4372 "B", "Do (not) use IPv4 source address selection rather than the "
4373 "primary jail IPv4 address.");
4376 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4377 "Jail IPv6 address virtualization");
4378 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4379 "S,in6_addr,a", "Jail IPv6 addresses");
4380 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4381 "B", "Do (not) use IPv6 source address selection rather than the "
4382 "primary jail IPv6 address.");
4385 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4386 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4387 "B", "Jail may set hostname");
4388 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4389 "B", "Jail may use SYSV IPC");
4390 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4391 "B", "Jail may create raw sockets");
4392 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4393 "B", "Jail may alter system file flags");
4394 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4395 "B", "Jail may set file quotas");
4396 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4397 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4398 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4399 "B", "Jail may lock (unlock) physical pages in memory");
4400 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4401 "B", "Jail may bind sockets to reserved ports");
4402 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4403 "B", "Jail may read the kernel message buffer");
4404 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4405 "B", "Unprivileged processes may use process debugging facilities");
4406 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4407 "B", "Processes in jail with uid 0 have privilege");
4409 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4410 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4411 "B", "Jail may mount/unmount jail-friendly file systems in general");
4414 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4415 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4419 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4422 struct bool_flags *bf;
4423 struct sysctl_oid *parent;
4424 char *allow_name, *allow_noname, *allowed;
4425 #ifndef NO_SYSCTL_DESCR
4426 char *descr_deprecated;
4431 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4433 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4435 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4436 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4437 free(allow_name, M_PRISON);
4442 * See if this parameter has already beed added, i.e. a module was
4443 * previously loaded/unloaded.
4445 mtx_lock(&prison0.pr_mtx);
4446 for (bf = pr_flag_allow;
4447 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4448 atomic_load_int(&bf->flag) != 0;
4450 if (strcmp(bf->name, allow_name) == 0) {
4451 allow_flag = bf->flag;
4457 * Find a free bit in pr_allow_all, failing if there are none
4458 * (which shouldn't happen as long as we keep track of how many
4459 * potential dynamic flags exist).
4461 for (allow_flag = 1;; allow_flag <<= 1) {
4462 if (allow_flag == 0)
4464 if ((pr_allow_all & allow_flag) == 0)
4468 /* Note the parameter in the next open slot in pr_flag_allow. */
4469 for (bf = pr_flag_allow; ; bf++) {
4470 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4471 /* This should never happen, but is not fatal. */
4475 if (atomic_load_int(&bf->flag) == 0)
4478 bf->name = allow_name;
4479 bf->noname = allow_noname;
4480 pr_allow_all |= allow_flag;
4482 * prison0 always has permission for the new parameter.
4483 * Other jails must have it granted to them.
4485 prison0.pr_allow |= allow_flag;
4486 /* The flag indicates a valid entry, so make sure it is set last. */
4487 atomic_store_rel_int(&bf->flag, allow_flag);
4488 mtx_unlock(&prison0.pr_mtx);
4491 * Create sysctls for the paramter, and the back-compat global
4495 ? SYSCTL_ADD_NODE(NULL,
4496 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4497 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4498 : &sysctl___security_jail_param_allow;
4499 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4500 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4501 NULL, 0, sysctl_jail_param, "B", descr);
4503 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4504 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4505 #ifndef NO_SYSCTL_DESCR
4506 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4509 (void)SYSCTL_ADD_PROC(NULL,
4510 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4511 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4512 sysctl_jail_default_allow, "I", descr_deprecated);
4513 #ifndef NO_SYSCTL_DESCR
4514 free(descr_deprecated, M_TEMP);
4516 free(allowed, M_TEMP);
4521 mtx_unlock(&prison0.pr_mtx);
4522 free(allow_name, M_PRISON);
4523 free(allow_noname, M_PRISON);
4528 * The VFS system will register jail-aware filesystems here. They each get
4529 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4530 * attempts to mount.
4533 prison_add_vfs(struct vfsconf *vfsp)
4535 #ifdef NO_SYSCTL_DESCR
4537 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4542 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4544 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4546 free(descr, M_TEMP);
4552 prison_racct_foreach(void (*callback)(struct racct *racct,
4553 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4554 void *arg2, void *arg3)
4556 struct prison_racct *prr;
4558 ASSERT_RACCT_ENABLED();
4560 sx_slock(&allprison_lock);
4563 LIST_FOREACH(prr, &allprison_racct, prr_next)
4564 (callback)(prr->prr_racct, arg2, arg3);
4567 sx_sunlock(&allprison_lock);
4570 static struct prison_racct *
4571 prison_racct_find_locked(const char *name)
4573 struct prison_racct *prr;
4575 ASSERT_RACCT_ENABLED();
4576 sx_assert(&allprison_lock, SA_XLOCKED);
4578 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4581 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4582 if (strcmp(name, prr->prr_name) != 0)
4585 /* Found prison_racct with a matching name? */
4586 prison_racct_hold(prr);
4590 /* Add new prison_racct. */
4591 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4592 racct_create(&prr->prr_racct);
4594 strcpy(prr->prr_name, name);
4595 refcount_init(&prr->prr_refcount, 1);
4596 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4601 struct prison_racct *
4602 prison_racct_find(const char *name)
4604 struct prison_racct *prr;
4606 ASSERT_RACCT_ENABLED();
4608 sx_xlock(&allprison_lock);
4609 prr = prison_racct_find_locked(name);
4610 sx_xunlock(&allprison_lock);
4615 prison_racct_hold(struct prison_racct *prr)
4618 ASSERT_RACCT_ENABLED();
4620 refcount_acquire(&prr->prr_refcount);
4624 prison_racct_free_locked(struct prison_racct *prr)
4627 ASSERT_RACCT_ENABLED();
4628 sx_assert(&allprison_lock, SA_XLOCKED);
4630 if (refcount_release(&prr->prr_refcount)) {
4631 racct_destroy(&prr->prr_racct);
4632 LIST_REMOVE(prr, prr_next);
4633 free(prr, M_PRISON_RACCT);
4638 prison_racct_free(struct prison_racct *prr)
4641 ASSERT_RACCT_ENABLED();
4642 sx_assert(&allprison_lock, SA_UNLOCKED);
4644 if (refcount_release_if_not_last(&prr->prr_refcount))
4647 sx_xlock(&allprison_lock);
4648 prison_racct_free_locked(prr);
4649 sx_xunlock(&allprison_lock);
4653 prison_racct_attach(struct prison *pr)
4655 struct prison_racct *prr;
4657 ASSERT_RACCT_ENABLED();
4658 sx_assert(&allprison_lock, SA_XLOCKED);
4660 prr = prison_racct_find_locked(pr->pr_name);
4661 KASSERT(prr != NULL, ("cannot find prison_racct"));
4663 pr->pr_prison_racct = prr;
4667 * Handle jail renaming. From the racct point of view, renaming means
4668 * moving from one prison_racct to another.
4671 prison_racct_modify(struct prison *pr)
4677 struct prison_racct *oldprr;
4679 ASSERT_RACCT_ENABLED();
4681 sx_slock(&allproc_lock);
4682 sx_xlock(&allprison_lock);
4684 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4685 sx_xunlock(&allprison_lock);
4686 sx_sunlock(&allproc_lock);
4690 oldprr = pr->pr_prison_racct;
4691 pr->pr_prison_racct = NULL;
4693 prison_racct_attach(pr);
4696 * Move resource utilisation records.
4698 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4702 * Force rctl to reattach rules to processes.
4704 FOREACH_PROC_IN_SYSTEM(p) {
4706 cred = crhold(p->p_ucred);
4708 rctl_proc_ucred_changed(p, cred);
4713 sx_sunlock(&allproc_lock);
4714 prison_racct_free_locked(oldprr);
4715 sx_xunlock(&allprison_lock);
4719 prison_racct_detach(struct prison *pr)
4722 ASSERT_RACCT_ENABLED();
4723 sx_assert(&allprison_lock, SA_UNLOCKED);
4725 if (pr->pr_prison_racct == NULL)
4727 prison_racct_free(pr->pr_prison_racct);
4728 pr->pr_prison_racct = NULL;
4735 db_show_prison(struct prison *pr)
4737 struct bool_flags *bf;
4738 struct jailsys_flags *jsf;
4739 #if defined(INET) || defined(INET6)
4744 char ip4buf[INET_ADDRSTRLEN];
4747 char ip6buf[INET6_ADDRSTRLEN];
4750 db_printf("prison %p:\n", pr);
4751 db_printf(" jid = %d\n", pr->pr_id);
4752 db_printf(" name = %s\n", pr->pr_name);
4753 db_printf(" parent = %p\n", pr->pr_parent);
4754 db_printf(" ref = %d\n", pr->pr_ref);
4755 db_printf(" uref = %d\n", pr->pr_uref);
4756 db_printf(" state = %s\n",
4757 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4758 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4760 db_printf(" path = %s\n", pr->pr_path);
4761 db_printf(" cpuset = %d\n", pr->pr_cpuset
4762 ? pr->pr_cpuset->cs_id : -1);
4764 db_printf(" vnet = %p\n", pr->pr_vnet);
4766 db_printf(" root = %p\n", pr->pr_root);
4767 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4768 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4769 db_printf(" children.max = %d\n", pr->pr_childmax);
4770 db_printf(" children.cur = %d\n", pr->pr_childcount);
4771 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4772 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4773 db_printf(" flags = 0x%x", pr->pr_flags);
4774 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4775 if (pr->pr_flags & bf->flag)
4776 db_printf(" %s", bf->name);
4777 for (jsf = pr_flag_jailsys;
4778 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4780 f = pr->pr_flags & (jsf->disable | jsf->new);
4781 db_printf(" %-16s= %s\n", jsf->name,
4782 (f != 0 && f == jsf->disable) ? "disable"
4783 : (f == jsf->new) ? "new"
4786 db_printf(" allow = 0x%x", pr->pr_allow);
4787 for (bf = pr_flag_allow;
4788 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4789 atomic_load_int(&bf->flag) != 0;
4791 if (pr->pr_allow & bf->flag)
4792 db_printf(" %s", bf->name);
4794 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4795 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4796 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4797 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4798 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4800 if (pr->pr_addrs[PR_INET] != NULL) {
4801 pr_family_t af = PR_INET;
4803 db_printf(" ip4s = %d\n", pr->pr_addrs[af]->ips);
4804 for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4805 db_printf(" %s %s\n",
4806 ii == 0 ? "ip4.addr =" : " ",
4808 *(const struct in_addr *)PR_IP(pr, ii),
4813 if (pr->pr_addrs[PR_INET6] != NULL) {
4814 pr_family_t af = PR_INET6;
4816 db_printf(" ip6s = %d\n", pr->pr_addrs[af]->ips);
4817 for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4818 db_printf(" %s %s\n",
4819 ii == 0 ? "ip6.addr =" : " ",
4821 (const struct in6_addr *)PR_IP(pr, ii)));
4826 DB_SHOW_COMMAND(prison, db_show_prison_command)
4832 * Show all prisons in the list, and prison0 which is not
4835 db_show_prison(&prison0);
4836 if (!db_pager_quit) {
4837 TAILQ_FOREACH(pr, &allprison, pr_list) {
4849 /* Look for a prison with the ID and with references. */
4850 TAILQ_FOREACH(pr, &allprison, pr_list)
4851 if (pr->pr_id == addr && pr->pr_ref > 0)
4854 /* Look again, without requiring a reference. */
4855 TAILQ_FOREACH(pr, &allprison, pr_list)
4856 if (pr->pr_id == addr)
4859 /* Assume address points to a valid prison. */
4860 pr = (struct prison *)addr;