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>
54 #include <sys/mutex.h>
55 #include <sys/racct.h>
57 #include <sys/refcount.h>
59 #include <sys/sysent.h>
60 #include <sys/namei.h>
61 #include <sys/mount.h>
62 #include <sys/queue.h>
63 #include <sys/socket.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
67 #include <sys/vnode.h>
72 #include <netinet/in.h>
78 #include <security/mac/mac_framework.h>
80 #define PRISON0_HOSTUUID_MODULE "hostuuid"
82 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
83 static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures");
85 /* Keep struct prison prison0 and some code in kern_jail_set() readable. */
88 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL
90 #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL
94 #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL
96 #define _PR_IP_SADDRSEL 0
100 /* prison0 describes what is "real" about the system. */
101 struct prison prison0 = {
107 .pr_securelevel = -1,
109 .pr_state = PRISON_STATE_ALIVE,
110 .pr_childmax = JAIL_MAX,
111 .pr_hostuuid = DEFAULT_HOSTUUID,
112 .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children),
114 .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL,
116 .pr_flags = PR_HOST|_PR_IP_SADDRSEL,
118 .pr_allow = PR_ALLOW_ALL_STATIC,
120 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
127 struct jailsys_flags {
133 /* allprison, allprison_racct and lastprid are protected by allprison_lock. */
134 struct sx allprison_lock;
135 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
136 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
137 LIST_HEAD(, prison_racct) allprison_racct;
140 static int get_next_prid(struct prison **insprp);
141 static int do_jail_attach(struct thread *td, struct prison *pr, int drflags);
142 static void prison_complete(void *context, int pending);
143 static void prison_deref(struct prison *pr, int flags);
144 static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison);
145 static int prison_lock_xlock(struct prison *pr, int flags);
146 static void prison_free_not_last(struct prison *pr);
147 static void prison_proc_free_not_last(struct prison *pr);
148 static void prison_set_allow_locked(struct prison *pr, unsigned flag,
150 static char *prison_path(struct prison *pr1, struct prison *pr2);
152 static void prison_racct_attach(struct prison *pr);
153 static void prison_racct_modify(struct prison *pr);
154 static void prison_racct_detach(struct prison *pr);
157 /* Flags for prison_deref */
158 #define PD_DEREF 0x01 /* Decrement pr_ref */
159 #define PD_DEUREF 0x02 /* Decrement pr_uref */
160 #define PD_KILL 0x04 /* Remove jail, kill processes, etc */
161 #define PD_LOCKED 0x10 /* pr_mtx is held */
162 #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */
163 #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */
164 #define PD_OP_FLAGS 0x07 /* Operation flags */
165 #define PD_LOCK_FLAGS 0x70 /* Lock status flags */
168 * Parameter names corresponding to PR_* flag values. Size values are for kvm
169 * as we cannot figure out the size of a sparse array, or an array without a
172 static struct bool_flags pr_flag_bool[] = {
173 {"persist", "nopersist", PR_PERSIST},
175 {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL},
178 {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL},
181 const size_t pr_flag_bool_size = sizeof(pr_flag_bool);
183 static struct jailsys_flags pr_flag_jailsys[] = {
184 {"host", 0, PR_HOST},
186 {"vnet", 0, PR_VNET},
189 {"ip4", PR_IP4_USER, PR_IP4_USER},
192 {"ip6", PR_IP6_USER, PR_IP6_USER},
195 const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys);
198 * Make this array full-size so dynamic parameters can be added.
199 * It is protected by prison0.mtx, but lockless reading is allowed
200 * with an atomic check of the flag values.
202 static struct bool_flags pr_flag_allow[NBBY * NBPW] = {
203 {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME},
204 {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC},
205 {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS},
206 {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS},
207 {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT},
208 {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS},
209 {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF},
210 {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK},
211 {"allow.reserved_ports", "allow.noreserved_ports",
212 PR_ALLOW_RESERVED_PORTS},
213 {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF},
214 {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug",
215 PR_ALLOW_UNPRIV_DEBUG},
216 {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER},
218 static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC;
219 const size_t pr_flag_allow_size = sizeof(pr_flag_allow);
221 #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \
222 PR_ALLOW_RESERVED_PORTS | \
223 PR_ALLOW_UNPRIV_DEBUG | \
225 #define JAIL_DEFAULT_ENFORCE_STATFS 2
226 #define JAIL_DEFAULT_DEVFS_RSNUM 0
227 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
228 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
229 static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM;
230 #if defined(INET) || defined(INET6)
231 static unsigned jail_max_af_ips = 255;
235 * Initialize the parts of prison0 that can't be static-initialized with
236 * constants. This is called from proc0_init() after creating thread0 cpuset.
241 uint8_t *file, *data;
243 char buf[sizeof(prison0.pr_hostuuid)];
246 prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset);
247 prison0.pr_osreldate = osreldate;
248 strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease));
250 /* If we have a preloaded hostuuid, use it. */
251 file = preload_search_by_type(PRISON0_HOSTUUID_MODULE);
253 data = preload_fetch_addr(file);
254 size = preload_fetch_size(file);
257 * The preloaded data may include trailing whitespace, almost
258 * certainly a newline; skip over any whitespace or
259 * non-printable characters to be safe.
261 while (size > 0 && data[size - 1] <= 0x20) {
268 * Not NUL-terminated when passed from loader, but
269 * validate_uuid requires that due to using sscanf (as
270 * does the subsequent strlcpy, since it still reads
271 * past the given size to return the true length);
272 * bounce to a temporary buffer to fix.
274 if (size >= sizeof(buf))
277 memcpy(buf, data, size);
280 if (validate_uuid(buf, size, NULL, 0) != 0)
284 (void)strlcpy(prison0.pr_hostuuid, buf,
285 sizeof(prison0.pr_hostuuid));
288 if (bootverbose && !valid) {
289 printf("hostuuid: preload data malformed: '%.*s'\n",
295 printf("hostuuid: using %s\n", prison0.pr_hostuuid);
304 sys_jail(struct thread *td, struct jail_args *uap)
310 error = copyin(uap->jail, &version, sizeof(uint32_t));
319 /* FreeBSD single IPv4 jails. */
320 bzero(&j, sizeof(struct jail));
321 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
324 j.version = j0.version;
326 j.hostname = j0.hostname;
327 j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */
333 * Version 1 was used by multi-IPv4 jail implementations
334 * that never made it into the official kernel.
338 case 2: /* JAIL_API_VERSION */
339 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
340 error = copyin(uap->jail, &j, sizeof(struct jail));
346 /* Sci-Fi jails are not supported, sorry. */
349 return (kern_jail(td, &j));
353 kern_jail(struct thread *td, struct jail *j)
355 struct iovec optiov[2 * (4 + nitems(pr_flag_allow)
364 char *u_path, *u_hostname, *u_name;
365 struct bool_flags *bf;
368 struct in_addr *u_ip4;
371 struct in6_addr *u_ip6;
374 int error, enforce_statfs;
376 bzero(&optiov, sizeof(optiov));
377 opt.uio_iov = optiov;
381 opt.uio_segflg = UIO_SYSSPACE;
382 opt.uio_rw = UIO_READ;
385 /* Set permissions for top-level jails from sysctls. */
386 if (!jailed(td->td_ucred)) {
387 for (bf = pr_flag_allow;
388 bf < pr_flag_allow + nitems(pr_flag_allow) &&
389 atomic_load_int(&bf->flag) != 0;
391 optiov[opt.uio_iovcnt].iov_base = __DECONST(char *,
392 (jail_default_allow & bf->flag)
393 ? bf->name : bf->noname);
394 optiov[opt.uio_iovcnt].iov_len =
395 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
398 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
399 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
401 enforce_statfs = jail_default_enforce_statfs;
402 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
403 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
407 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
409 ip4s = (j->version == 0) ? 1 : j->ip4s;
410 if (ip4s > jail_max_af_ips)
412 tmplen += ip4s * sizeof(struct in_addr);
418 if (j->ip6s > jail_max_af_ips)
420 tmplen += j->ip6s * sizeof(struct in6_addr);
425 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
426 u_hostname = u_path + MAXPATHLEN;
427 u_name = u_hostname + MAXHOSTNAMELEN;
429 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
433 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
435 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
438 optiov[opt.uio_iovcnt].iov_base = "path";
439 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
441 optiov[opt.uio_iovcnt].iov_base = u_path;
442 error = copyinstr(j->path, u_path, MAXPATHLEN,
443 &optiov[opt.uio_iovcnt].iov_len);
445 free(u_path, M_TEMP);
449 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
450 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
452 optiov[opt.uio_iovcnt].iov_base = u_hostname;
453 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
454 &optiov[opt.uio_iovcnt].iov_len);
456 free(u_path, M_TEMP);
460 if (j->jailname != NULL) {
461 optiov[opt.uio_iovcnt].iov_base = "name";
462 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
464 optiov[opt.uio_iovcnt].iov_base = u_name;
465 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
466 &optiov[opt.uio_iovcnt].iov_len);
468 free(u_path, M_TEMP);
474 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
475 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
477 optiov[opt.uio_iovcnt].iov_base = u_ip4;
478 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
480 u_ip4->s_addr = j->ip4s;
482 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
484 free(u_path, M_TEMP);
491 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
492 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
494 optiov[opt.uio_iovcnt].iov_base = u_ip6;
495 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
496 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
498 free(u_path, M_TEMP);
503 KASSERT(opt.uio_iovcnt <= nitems(optiov),
504 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
505 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
506 free(u_path, M_TEMP);
511 * struct jail_set_args {
512 * struct iovec *iovp;
513 * unsigned int iovcnt;
518 sys_jail_set(struct thread *td, struct jail_set_args *uap)
523 /* Check that we have an even number of iovecs. */
527 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
530 error = kern_jail_set(td, auio, uap->flags);
535 #if defined(INET) || defined(INET6)
536 typedef int prison_addr_cmp_t(const void *, const void *);
537 typedef bool prison_addr_valid_t(const void *);
538 static const struct pr_family {
540 prison_addr_cmp_t *cmp;
541 prison_addr_valid_t *valid;
543 } pr_families[PR_FAMILY_MAX] = {
546 .size = sizeof(struct in_addr),
547 .cmp = prison_qcmp_v4,
548 .valid = prison_valid_v4,
549 .ip_flag = PR_IP4_USER,
554 .size = sizeof(struct in6_addr),
555 .cmp = prison_qcmp_v6,
556 .valid = prison_valid_v6,
557 .ip_flag = PR_IP6_USER,
563 * Network address lists (pr_addrs) allocation for jails. The addresses
564 * are accessed locklessly by the network stack, thus need to be protected by
568 struct epoch_context ctx;
572 struct in_addr pr_ip4[];
573 struct in6_addr pr_ip6[];
575 #else /* No future C :( */
576 #define PR_IP(pip, i) ((const char *)((pip) + 1) + pr_families[af].size * (i))
577 #define PR_IPD(pip, i) ((char *)((pip) + 1) + pr_families[af].size * (i))
581 static struct prison_ip *
582 prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags)
584 struct prison_ip *pip;
586 pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size,
594 * Allocate and copyin user supplied address list, sorting and validating.
595 * kern_jail_set() helper.
597 static struct prison_ip *
598 prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt)
600 prison_addr_cmp_t *const cmp = pr_families[af].cmp;
601 const size_t size = pr_families[af].size;
602 struct prison_ip *pip;
604 pip = prison_ip_alloc(af, cnt, M_WAITOK);
605 bcopy(op, pip + 1, cnt * size);
607 * IP addresses are all sorted but ip[0] to preserve
608 * the primary IP address as given from userland.
609 * This special IP is used for unbound outgoing
610 * connections as well for "loopback" traffic in case
611 * source address selection cannot find any more fitting
612 * address to connect from.
615 qsort((char *)(pip + 1) + size, cnt - 1, size,
616 pr_families[af].cmp);
618 * Check for duplicate addresses and do some simple
619 * zero and broadcast checks. If users give other bogus
620 * addresses it is their problem.
622 for (int i = 0; i < cnt; i++) {
623 if (!pr_families[af].valid(PR_IP(pip, i))) {
628 (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 ||
629 cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) {
639 * Allocate and dup parent prison address list.
640 * kern_jail_set() helper.
643 prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af)
646 if (ppr->pr_addrs[af] != NULL) {
647 pr->pr_addrs[af] = prison_ip_alloc(af,
648 ppr->pr_addrs[af]->ips, M_WAITOK);
649 bcopy(ppr->pr_addrs[af], pr->pr_addrs[af],
650 pr->pr_addrs[af]->ips * pr_families[af].size);
655 * Make sure the new set of IP addresses is a subset of the parent's list.
656 * Don't worry about the parent being unlocked, as any setting is done with
657 * allprison_lock held.
658 * kern_jail_set() helper.
661 prison_ip_parent_match(const struct prison_ip *ppip,
662 const struct prison_ip *pip, const pr_family_t af)
664 prison_addr_cmp_t *const cmp = pr_families[af].cmp;
670 for (i = 0; i < ppip->ips; i++)
671 if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0)
675 /* Main address not present in parent. */
679 for (i = j = 1; i < pip->ips; i++) {
680 if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0)
681 /* Equals to parent primary address. */
683 for (; j < ppip->ips; j++)
684 if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0)
690 /* Address not present in parent. */
697 * Check for conflicting IP addresses. We permit them if there is no more
698 * than one IP on each jail. If there is a duplicate on a jail with more
699 * than one IP stop checking and return error.
700 * kern_jail_set() helper.
703 prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr,
704 const struct prison_ip *pip, pr_family_t af)
706 const struct prison *tppr, *tpr;
710 for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent)
711 if (tppr->pr_flags & PR_VNET)
716 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
719 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
721 !prison_isalive(tpr)) {
725 if (!(tpr->pr_flags & pr_families[af].ip_flag))
728 if (tpr->pr_addrs[af] == NULL ||
729 (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1))
731 for (int i = 0; i < pip->ips; i++)
732 if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0)
739 _Static_assert(offsetof(struct prison_ip, ctx) == 0,
740 "prison must start with epoch context");
742 prison_ip_free_deferred(epoch_context_t ctx)
749 prison_ip_free(struct prison_ip *pip)
753 NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx);
757 prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new)
759 struct prison_ip **mem, *old;
761 mtx_assert(&pr->pr_mtx, MA_OWNED);
763 mem = &pr->pr_addrs[af];
766 ck_pr_store_ptr(mem, new);
771 * Restrict a prison's IP address list with its parent's, possibly replacing
772 * it. Return true if the replacement buffer was used (or would have been).
773 * kern_jail_set() helper.
776 prison_ip_restrict(struct prison *pr, const pr_family_t af,
777 struct prison_ip *new)
779 const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af];
780 const struct prison_ip *pip = pr->pr_addrs[af];
781 int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
782 const size_t size = pr_families[af].size;
786 mtx_assert(&pr->pr_mtx, MA_OWNED);
789 * Due to epoch-synchronized access to the IP address lists we always
790 * allocate a new list even if the old one has enough space. We could
791 * atomically update an IPv4 address inside a list, but that would
792 * screw up sorting, and in case of IPv6 we can't even atomically write
795 ips = (pr->pr_flags & pr_families[af].ip_flag) ? pip->ips : ppip->ips;
797 prison_ip_set(pr, af, NULL);
801 new = prison_ip_alloc(af, ips, M_NOWAIT);
807 if (!(pr->pr_flags & pr_families[af].ip_flag)) {
808 /* This has no user settings, so just copy the parent's list. */
809 bcopy(ppip, new, ips * size);
811 /* Remove addresses that aren't in the parent. */
814 i = 0; /* index in pip */
815 ips = 0; /* index in new */
817 for (int pi = 0; pi < ppip->ips; pi++)
818 if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) {
819 /* Found our primary address in parent. */
820 bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
825 for (int pi = 1; i < pip->ips; ) {
826 /* Check against primary, which is unsorted. */
827 if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) {
828 /* Matches parent's primary address. */
829 bcopy(PR_IP(pip, i), PR_IPD(new, ips), size);
834 /* The rest are sorted. */
835 switch (pi >= ppip->ips ? -1 :
836 cmp(PR_IP(pip, i), PR_IP(ppip, pi))) {
841 bcopy(PR_IP(pr, i), PR_IPD(new, ips), size);
857 prison_ip_set(pr, af, new);
858 return (new != NULL ? true : false);
862 * Fast-path check if an address belongs to a prison.
865 prison_ip_check(const struct prison *pr, const pr_family_t af,
868 int (*const cmp)(const void *, const void *) = pr_families[af].cmp;
869 const struct prison_ip *pip;
872 MPASS(mtx_owned(&pr->pr_mtx) ||
873 in_epoch(net_epoch_preempt) ||
874 sx_xlocked(&allprison_lock));
876 pip = ck_pr_load_ptr(&pr->pr_addrs[af]);
877 if (__predict_false(pip == NULL))
878 return (EAFNOSUPPORT);
880 /* Check the primary IP. */
881 if (cmp(PR_IP(pip, 0), addr) == 0)
885 * All the other IPs are sorted so we can do a binary search.
891 d = cmp(PR_IP(pip, i + 1), addr);
900 return (EADDRNOTAVAIL);
904 * Grab primary IP. Historically required mutex, but nothing prevents
905 * us to support epoch-protected access. Is it used in fast path?
906 * in{6}_jail.c helper
909 prison_ip_get0(const struct prison *pr, const pr_family_t af)
911 const struct prison_ip *pip = pr->pr_addrs[af];
913 mtx_assert(&pr->pr_mtx, MA_OWNED);
920 prison_ip_cnt(const struct prison *pr, const pr_family_t af)
923 return (pr->pr_addrs[af]->ips);
925 #endif /* defined(INET) || defined(INET6) */
928 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
932 struct prison_ip *ip4;
935 struct prison_ip *ip6;
938 struct vfsoptlist *opts;
939 struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr;
941 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
942 char *g_path, *osrelstr;
943 struct bool_flags *bf;
944 struct jailsys_flags *jsf;
945 #if defined(INET) || defined(INET6)
949 size_t namelen, onamelen, pnamelen;
950 int born, created, cuflags, descend, drflags, enforce;
951 int error, errmsg_len, errmsg_pos;
952 int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel;
953 int jid, jsys, len, level;
954 int childmax, osreldt, rsnum, slevel;
961 uint64_t pr_allow, ch_allow, pr_flags, ch_flags;
962 uint64_t pr_allow_diff;
966 error = priv_check(td, PRIV_JAIL_SET);
967 if (!error && (flags & JAIL_ATTACH))
968 error = priv_check(td, PRIV_JAIL_ATTACH);
971 mypr = td->td_ucred->cr_prison;
972 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
974 if (flags & ~JAIL_SET_MASK)
978 * Check all the parameters before committing to anything. Not all
979 * errors can be caught early, but we may as well try. Also, this
980 * takes care of some expensive stuff (path lookup) before getting
981 * the allprison lock.
983 * XXX Jails are not filesystems, and jail parameters are not mount
984 * options. But it makes more sense to re-use the vfsopt code
985 * than duplicate it under a different name.
987 error = vfs_buildopts(optuio, &opts);
998 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
1001 vfs_opterror(opts, "no valid operation (create or update)");
1005 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1006 if (error == ENOENT)
1008 else if (error != 0)
1011 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
1012 if (error == ENOENT)
1014 else if (error != 0)
1020 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
1021 if (error == ENOENT)
1023 else if (error != 0)
1028 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
1029 if (error == ENOENT)
1031 else if (error != 0)
1033 else if (enforce < 0 || enforce > 2) {
1039 error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum));
1040 if (error == ENOENT)
1042 else if (error != 0)
1047 pr_flags = ch_flags = 0;
1048 for (bf = pr_flag_bool;
1049 bf < pr_flag_bool + nitems(pr_flag_bool);
1051 vfs_flagopt(opts, bf->name, &pr_flags, bf->flag);
1052 vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag);
1054 ch_flags |= pr_flags;
1055 for (jsf = pr_flag_jailsys;
1056 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
1058 error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys));
1059 if (error == ENOENT)
1064 case JAIL_SYS_DISABLE:
1065 if (!jsf->disable) {
1069 pr_flags |= jsf->disable;
1072 pr_flags |= jsf->new;
1074 case JAIL_SYS_INHERIT:
1080 ch_flags |= jsf->new | jsf->disable;
1082 if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE
1083 && !(pr_flags & PR_PERSIST)) {
1085 vfs_opterror(opts, "new jail must persist or attach");
1089 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
1091 vfs_opterror(opts, "vnet cannot be changed after creation");
1096 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
1098 vfs_opterror(opts, "ip4 cannot be changed after creation");
1103 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
1105 vfs_opterror(opts, "ip6 cannot be changed after creation");
1110 pr_allow = ch_allow = 0;
1111 for (bf = pr_flag_allow;
1112 bf < pr_flag_allow + nitems(pr_flag_allow) &&
1113 atomic_load_int(&bf->flag) != 0;
1115 vfs_flagopt(opts, bf->name, &pr_allow, bf->flag);
1116 vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag);
1118 ch_allow |= pr_allow;
1120 error = vfs_getopt(opts, "name", (void **)&name, &len);
1121 if (error == ENOENT)
1123 else if (error != 0)
1126 if (len == 0 || name[len - 1] != '\0') {
1130 if (len > MAXHOSTNAMELEN) {
1131 error = ENAMETOOLONG;
1136 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
1137 if (error == ENOENT)
1139 else if (error != 0)
1142 ch_flags |= PR_HOST;
1143 pr_flags |= PR_HOST;
1144 if (len == 0 || host[len - 1] != '\0') {
1148 if (len > MAXHOSTNAMELEN) {
1149 error = ENAMETOOLONG;
1154 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
1155 if (error == ENOENT)
1157 else if (error != 0)
1160 ch_flags |= PR_HOST;
1161 pr_flags |= PR_HOST;
1162 if (len == 0 || domain[len - 1] != '\0') {
1166 if (len > MAXHOSTNAMELEN) {
1167 error = ENAMETOOLONG;
1172 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
1173 if (error == ENOENT)
1175 else if (error != 0)
1178 ch_flags |= PR_HOST;
1179 pr_flags |= PR_HOST;
1180 if (len == 0 || uuid[len - 1] != '\0') {
1184 if (len > HOSTUUIDLEN) {
1185 error = ENAMETOOLONG;
1190 #ifdef COMPAT_FREEBSD32
1191 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
1194 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
1198 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
1199 if (error == ENOENT)
1201 else if (error != 0)
1205 ch_flags |= PR_HOST;
1206 pr_flags |= PR_HOST;
1210 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
1211 if (error == ENOENT)
1213 else if (error != 0)
1215 else if (ip4s & (sizeof(struct in_addr) - 1)) {
1219 ch_flags |= PR_IP4_USER;
1220 pr_flags |= PR_IP4_USER;
1222 ip4s /= sizeof(struct in_addr);
1223 if (ip4s > jail_max_af_ips) {
1225 vfs_opterror(opts, "too many IPv4 addresses");
1228 ip4 = prison_ip_copyin(PR_INET, op, ip4s);
1238 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
1239 if (error == ENOENT)
1241 else if (error != 0)
1243 else if (ip6s & (sizeof(struct in6_addr) - 1)) {
1247 ch_flags |= PR_IP6_USER;
1248 pr_flags |= PR_IP6_USER;
1250 ip6s /= sizeof(struct in6_addr);
1251 if (ip6s > jail_max_af_ips) {
1253 vfs_opterror(opts, "too many IPv6 addresses");
1256 ip6 = prison_ip_copyin(PR_INET6, op, ip6s);
1265 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1266 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1269 "vnet jails cannot have IP address restrictions");
1274 error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len);
1275 if (error == ENOENT)
1277 else if (error != 0)
1280 if (flags & JAIL_UPDATE) {
1283 "osrelease cannot be changed after creation");
1286 if (len == 0 || osrelstr[len - 1] != '\0') {
1290 if (len >= OSRELEASELEN) {
1291 error = ENAMETOOLONG;
1293 "osrelease string must be 1-%d bytes long",
1299 error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt));
1300 if (error == ENOENT)
1302 else if (error != 0)
1305 if (flags & JAIL_UPDATE) {
1308 "osreldate cannot be changed after creation");
1313 vfs_opterror(opts, "osreldate cannot be 0");
1319 error = vfs_getopt(opts, "path", (void **)&path, &len);
1320 if (error == ENOENT)
1322 else if (error != 0)
1325 if (flags & JAIL_UPDATE) {
1328 "path cannot be changed after creation");
1331 if (len == 0 || path[len - 1] != '\0') {
1335 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path);
1340 NDFREE(&nd, NDF_ONLY_PNBUF);
1341 g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
1342 strlcpy(g_path, path, MAXPATHLEN);
1343 error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN);
1347 /* exit on other errors */
1350 if (root->v_type != VDIR) {
1359 * Find the specified jail, or at least its parent.
1360 * This abuses the file error codes ENOENT and EEXIST.
1364 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
1365 namelc = strrchr(name, '.');
1366 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
1370 sx_xlock(&allprison_lock);
1371 drflags = PD_LIST_XLOCKED;
1373 if (!prison_isalive(ppr)) {
1374 /* This jail is dying. This process will surely follow. */
1381 vfs_opterror(opts, "negative jid");
1385 * See if a requested jid already exists. Keep track of
1386 * where it can be inserted later.
1388 TAILQ_FOREACH(inspr, &allprison, pr_list) {
1389 if (inspr->pr_id < jid)
1391 if (inspr->pr_id > jid)
1394 mtx_lock(&pr->pr_mtx);
1395 drflags |= PD_LOCKED;
1400 /* Create: jid must not exist. */
1401 if (cuflags == JAIL_CREATE) {
1403 * Even creators that cannot see the jail will
1407 vfs_opterror(opts, "jail %d already exists",
1411 if (!prison_ischild(mypr, pr)) {
1413 * Updaters get ENOENT if they cannot see the
1414 * jail. This is true even for CREATE | UPDATE,
1415 * which normally cannot give this error.
1418 vfs_opterror(opts, "jail %d not found", jid);
1421 ppr = pr->pr_parent;
1422 if (!prison_isalive(ppr)) {
1424 vfs_opterror(opts, "jail %d is dying",
1428 if (!prison_isalive(pr)) {
1429 if (!(flags & JAIL_DYING)) {
1431 vfs_opterror(opts, "jail %d is dying",
1435 if ((flags & JAIL_ATTACH) ||
1436 (pr_flags & PR_PERSIST)) {
1438 * A dying jail might be resurrected
1439 * (via attach or persist), but first
1440 * it must determine if another jail
1441 * has claimed its name. Accomplish
1442 * this by implicitly re-setting the
1446 name = prison_name(mypr, pr);
1450 /* Update: jid must exist. */
1451 if (cuflags == JAIL_UPDATE) {
1453 vfs_opterror(opts, "jail %d not found", jid);
1459 * If the caller provided a name, look for a jail by that name.
1460 * This has different semantics for creates and updates keyed by jid
1461 * (where the name must not already exist in a different jail),
1462 * and updates keyed by the name itself (where the name must exist
1463 * because that is the jail being updated).
1467 namelc = strrchr(name, '.');
1472 * This is a hierarchical name. Split it into the
1473 * parent and child names, and make sure the parent
1474 * exists or matches an already found jail.
1477 if (strncmp(name, ppr->pr_name, namelc - name)
1478 || ppr->pr_name[namelc - name] != '\0') {
1481 "cannot change jail's parent");
1486 ppr = prison_find_name(mypr, name);
1490 "jail \"%s\" not found", name);
1493 mtx_unlock(&ppr->pr_mtx);
1494 if (!prison_isalive(ppr)) {
1497 "jail \"%s\" is dying", name);
1504 if (namelc[0] != '\0') {
1506 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1508 FOREACH_PRISON_CHILD(ppr, tpr) {
1510 !strcmp(tpr->pr_name + pnamelen, namelc)) {
1511 if (prison_isalive(tpr)) {
1513 cuflags != JAIL_CREATE) {
1519 mtx_lock(&pr->pr_mtx);
1520 drflags |= PD_LOCKED;
1524 * Create, or update(jid):
1525 * name must not exist in an
1526 * active sibling jail.
1530 "jail \"%s\" already exists",
1535 cuflags != JAIL_CREATE) {
1540 /* If no active jail is found, use a dying one. */
1541 if (deadpr != NULL && pr == NULL) {
1542 if (flags & JAIL_DYING) {
1544 mtx_lock(&pr->pr_mtx);
1545 drflags |= PD_LOCKED;
1546 } else if (cuflags == JAIL_UPDATE) {
1549 "jail \"%s\" is dying", name);
1553 /* Update: name must exist if no jid. */
1554 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1556 vfs_opterror(opts, "jail \"%s\" not found",
1562 /* Update: must provide a jid or name. */
1563 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1565 vfs_opterror(opts, "update specified no jail");
1569 /* If there's no prison to update, create a new one and link it in. */
1570 created = pr == NULL;
1572 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1573 if (tpr->pr_childcount >= tpr->pr_childmax) {
1575 vfs_opterror(opts, "prison limit exceeded");
1578 if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) {
1580 vfs_opterror(opts, "no available jail IDs");
1584 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1585 pr->pr_state = PRISON_STATE_INVALID;
1586 refcount_init(&pr->pr_ref, 1);
1587 refcount_init(&pr->pr_uref, 0);
1588 drflags |= PD_DEREF;
1589 LIST_INIT(&pr->pr_children);
1590 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1591 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
1595 TAILQ_INSERT_BEFORE(inspr, pr, pr_list);
1597 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1599 pr->pr_parent = ppr;
1601 prison_proc_hold(ppr);
1602 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1603 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1604 tpr->pr_childcount++;
1606 /* Set some default values, and inherit some from the parent. */
1611 root = mypr->pr_root;
1614 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1615 pr->pr_flags |= PR_HOST;
1616 #if defined(INET) || defined(INET6)
1618 if (!(pr_flags & PR_VNET))
1622 if (!(ch_flags & PR_IP4_USER))
1623 pr->pr_flags |= PR_IP4 | PR_IP4_USER;
1624 else if (!(pr_flags & PR_IP4_USER)) {
1625 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1626 prison_ip_dup(ppr, pr, PR_INET);
1630 if (!(ch_flags & PR_IP6_USER))
1631 pr->pr_flags |= PR_IP6 | PR_IP6_USER;
1632 else if (!(pr_flags & PR_IP6_USER)) {
1633 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1634 prison_ip_dup(ppr, pr, PR_INET6);
1639 /* Source address selection is always on by default. */
1640 pr->pr_flags |= _PR_IP_SADDRSEL;
1642 pr->pr_securelevel = ppr->pr_securelevel;
1643 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1644 pr->pr_enforce_statfs = jail_default_enforce_statfs;
1645 pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum;
1647 pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate;
1648 if (osrelstr == NULL)
1649 strlcpy(pr->pr_osrelease, ppr->pr_osrelease,
1650 sizeof(pr->pr_osrelease));
1652 strlcpy(pr->pr_osrelease, osrelstr,
1653 sizeof(pr->pr_osrelease));
1656 /* Allocate a new vnet if specified. */
1657 pr->pr_vnet = (pr_flags & PR_VNET)
1658 ? vnet_alloc() : ppr->pr_vnet;
1661 * Allocate a dedicated cpuset for each jail.
1662 * Unlike other initial settings, this may return an error.
1664 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1668 mtx_lock(&pr->pr_mtx);
1669 drflags |= PD_LOCKED;
1672 * Grab a reference for existing prisons, to ensure they
1673 * continue to exist for the duration of the call.
1676 drflags |= PD_DEREF;
1677 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1678 if ((pr->pr_flags & PR_VNET) &&
1679 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1682 "vnet jails cannot have IP address restrictions");
1687 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1690 "ip4 cannot be changed after creation");
1695 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1698 "ip6 cannot be changed after creation");
1704 /* Do final error checking before setting anything. */
1706 if (slevel < ppr->pr_securelevel) {
1712 if (childmax >= ppr->pr_childmax) {
1718 if (enforce < ppr->pr_enforce_statfs) {
1725 * devfs_rsnum is a uint16_t
1727 if (rsnum < 0 || rsnum > 65535) {
1732 * Nested jails always inherit parent's devfs ruleset
1734 if (jailed(td->td_ucred)) {
1735 if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) {
1739 rsnum = ppr->pr_devfs_rsnum;
1744 if ((ppr->pr_flags & PR_IP4) &&
1745 !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4,
1750 if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) {
1752 vfs_opterror(opts, "IPv4 addresses clash");
1759 if ((ppr->pr_flags & PR_IP6) &&
1760 !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6,
1765 if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) {
1767 vfs_opterror(opts, "IPv6 addresses clash");
1772 onamelen = namelen = 0;
1773 if (namelc != NULL) {
1774 /* Give a default name of the jid. Also allow the name to be
1775 * explicitly the jid - but not any other number, and only in
1776 * normal form (no leading zero/etc).
1778 if (namelc[0] == '\0')
1779 snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid);
1780 else if ((strtoul(namelc, &p, 10) != jid ||
1781 namelc[0] < '1' || namelc[0] > '9') && *p == '\0') {
1784 "name cannot be numeric (unless it is the jid)");
1788 * Make sure the name isn't too long for the prison or its
1791 pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1792 onamelen = strlen(pr->pr_name + pnamelen);
1793 namelen = strlen(namelc);
1794 if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) {
1795 error = ENAMETOOLONG;
1798 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1799 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1800 sizeof(pr->pr_name)) {
1801 error = ENAMETOOLONG;
1806 pr_allow_diff = pr_allow & ~ppr->pr_allow;
1807 if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) {
1813 * Let modules check their parameters. This requires unlocking and
1814 * then re-locking the prison, but this is still a valid state as long
1815 * as allprison_lock remains xlocked.
1817 mtx_unlock(&pr->pr_mtx);
1818 drflags &= ~PD_LOCKED;
1819 error = osd_jail_call(pr, PR_METHOD_CHECK, opts);
1822 mtx_lock(&pr->pr_mtx);
1823 drflags |= PD_LOCKED;
1825 /* At this point, all valid parameters should have been noted. */
1826 TAILQ_FOREACH(opt, opts, link) {
1827 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1829 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1834 /* Set the parameters of the prison. */
1837 if (pr_flags & PR_IP4_USER) {
1838 pr->pr_flags |= PR_IP4;
1839 prison_ip_set(pr, PR_INET, ip4);
1841 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1843 if (tpr->pr_flags & PR_VNET) {
1848 if (prison_ip_restrict(tpr, PR_INET, NULL)) {
1857 if (pr_flags & PR_IP6_USER) {
1858 pr->pr_flags |= PR_IP6;
1859 prison_ip_set(pr, PR_INET6, ip6);
1861 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1863 if (tpr->pr_flags & PR_VNET) {
1868 if (prison_ip_restrict(tpr, PR_INET6, NULL)) {
1876 pr->pr_securelevel = slevel;
1877 /* Set all child jails to be at least this level. */
1878 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1879 if (tpr->pr_securelevel < slevel)
1880 tpr->pr_securelevel = slevel;
1883 pr->pr_childmax = childmax;
1884 /* Set all child jails to under this limit. */
1885 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1886 if (tpr->pr_childmax > childmax - level)
1887 tpr->pr_childmax = childmax > level
1888 ? childmax - level : 0;
1891 pr->pr_enforce_statfs = enforce;
1892 /* Pass this restriction on to the children. */
1893 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1894 if (tpr->pr_enforce_statfs < enforce)
1895 tpr->pr_enforce_statfs = enforce;
1898 pr->pr_devfs_rsnum = rsnum;
1899 /* Pass this restriction on to the children. */
1900 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1901 tpr->pr_devfs_rsnum = rsnum;
1903 if (namelc != NULL) {
1904 if (ppr == &prison0)
1905 strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name));
1907 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1908 ppr->pr_name, namelc);
1909 /* Change this component of child names. */
1910 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1911 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1912 strlen(tpr->pr_name + onamelen) + 1);
1913 bcopy(pr->pr_name, tpr->pr_name, namelen);
1917 /* Try to keep a real-rooted full pathname. */
1918 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1922 if (PR_HOST & ch_flags & ~pr_flags) {
1923 if (pr->pr_flags & PR_HOST) {
1925 * Copy the parent's host info. As with pr_ip4 above,
1926 * the lack of a lock on the parent is not a problem;
1927 * it is always set with allprison_lock at least
1928 * shared, and is held exclusively here.
1930 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1931 sizeof(pr->pr_hostname));
1932 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1933 sizeof(pr->pr_domainname));
1934 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1935 sizeof(pr->pr_hostuuid));
1936 pr->pr_hostid = pr->pr_parent->pr_hostid;
1938 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1939 /* Set this prison, and any descendants without PR_HOST. */
1941 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1943 strlcpy(pr->pr_domainname, domain,
1944 sizeof(pr->pr_domainname));
1946 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1948 pr->pr_hostid = hid;
1949 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1950 if (tpr->pr_flags & PR_HOST)
1954 strlcpy(tpr->pr_hostname,
1956 sizeof(tpr->pr_hostname));
1958 strlcpy(tpr->pr_domainname,
1960 sizeof(tpr->pr_domainname));
1962 strlcpy(tpr->pr_hostuuid,
1964 sizeof(tpr->pr_hostuuid));
1966 tpr->pr_hostid = hid;
1970 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1971 if ((tallow = ch_allow & ~pr_allow))
1972 prison_set_allow_locked(pr, tallow, 0);
1974 * Persistent prisons get an extra reference, and prisons losing their
1975 * persist flag lose that reference.
1977 born = !prison_isalive(pr);
1978 if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) {
1979 if (pr_flags & PR_PERSIST) {
1982 * This may make a dead prison alive again, but wait
1983 * to label it as such until after OSD calls have had
1984 * a chance to run (and perhaps to fail).
1986 refcount_acquire(&pr->pr_uref);
1988 drflags |= PD_DEUREF;
1989 prison_free_not_last(pr);
1992 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1993 mtx_unlock(&pr->pr_mtx);
1994 drflags &= ~PD_LOCKED;
1996 * Any errors past this point will need to de-persist newly created
1997 * prisons, as well as call remove methods.
2003 if (racct_enable && created)
2004 prison_racct_attach(pr);
2007 /* Locks may have prevented a complete restriction of child IP
2008 * addresses. If so, allocate some more memory and try again.
2012 ip4s = pr->pr_addrs[PR_INET]->ips;
2013 ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK);
2014 mtx_lock(&pr->pr_mtx);
2016 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2018 if (tpr->pr_flags & PR_VNET) {
2023 if (prison_ip_restrict(tpr, PR_INET, ip4)) {
2030 mtx_unlock(&pr->pr_mtx);
2035 ip6s = pr->pr_addrs[PR_INET6]->ips;
2036 ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK);
2037 mtx_lock(&pr->pr_mtx);
2039 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
2041 if (tpr->pr_flags & PR_VNET) {
2046 if (prison_ip_restrict(tpr, PR_INET6, ip6)) {
2053 mtx_unlock(&pr->pr_mtx);
2057 /* Let the modules do their work. */
2059 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
2063 error = osd_jail_call(pr, PR_METHOD_SET, opts);
2068 * A new prison is now ready to be seen; either it has gained a user
2069 * reference via persistence, or is about to gain one via attachment.
2072 drflags = prison_lock_xlock(pr, drflags);
2073 pr->pr_state = PRISON_STATE_ALIVE;
2076 /* Attach this process to the prison if requested. */
2077 if (flags & JAIL_ATTACH) {
2078 error = do_jail_attach(td, pr,
2079 prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS));
2080 drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED);
2082 vfs_opterror(opts, "attach failed");
2088 if (racct_enable && !created) {
2089 if (drflags & PD_LOCKED) {
2090 mtx_unlock(&pr->pr_mtx);
2091 drflags &= ~PD_LOCKED;
2093 if (drflags & PD_LIST_XLOCKED) {
2094 sx_xunlock(&allprison_lock);
2095 drflags &= ~PD_LIST_XLOCKED;
2097 prison_racct_modify(pr);
2101 drflags &= ~PD_KILL;
2102 td->td_retval[0] = pr->pr_id;
2105 /* Release any temporary prison holds and/or locks. */
2107 prison_deref(pr, drflags);
2108 else if (drflags & PD_LIST_SLOCKED)
2109 sx_sunlock(&allprison_lock);
2110 else if (drflags & PD_LIST_XLOCKED)
2111 sx_xunlock(&allprison_lock);
2116 /* Write the error message back to userspace. */
2117 if (vfs_getopt(opts, "errmsg", (void **)&errmsg,
2118 &errmsg_len) == 0 && errmsg_len > 0) {
2119 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
2120 if (optuio->uio_segflg == UIO_SYSSPACE)
2122 optuio->uio_iov[errmsg_pos].iov_base,
2126 optuio->uio_iov[errmsg_pos].iov_base,
2132 prison_ip_free(ip4);
2135 prison_ip_free(ip6);
2138 free(g_path, M_TEMP);
2144 * Find the next available prison ID. Return the ID on success, or zero
2145 * on failure. Also set a pointer to the allprison list entry the prison
2146 * should be inserted before.
2149 get_next_prid(struct prison **insprp)
2151 struct prison *inspr;
2154 jid = lastprid % JAIL_MAX + 1;
2155 if (TAILQ_EMPTY(&allprison) ||
2156 TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) {
2158 * A common case is for all jails to be implicitly numbered,
2159 * which means they'll go on the end of the list, at least
2160 * for the first JAIL_MAX times.
2165 * Take two passes through the allprison list: first starting
2166 * with the proposed jid, then ending with it.
2168 for (maxid = JAIL_MAX; maxid != 0; ) {
2169 TAILQ_FOREACH(inspr, &allprison, pr_list) {
2170 if (inspr->pr_id < jid)
2172 if (inspr->pr_id > jid) {
2173 /* Found an opening. */
2177 if (++jid > maxid) {
2178 if (lastprid == maxid || lastprid == 0)
2181 * The entire legal range
2182 * has been traversed
2186 /* Try again from the start. */
2192 if (inspr == NULL) {
2193 /* Found room at the end of the list. */
2204 * struct jail_get_args {
2205 * struct iovec *iovp;
2206 * unsigned int iovcnt;
2211 sys_jail_get(struct thread *td, struct jail_get_args *uap)
2216 /* Check that we have an even number of iovecs. */
2217 if (uap->iovcnt & 1)
2220 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
2223 error = kern_jail_get(td, auio, uap->flags);
2225 error = copyout(auio->uio_iov, uap->iovp,
2226 uap->iovcnt * sizeof (struct iovec));
2232 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
2234 struct bool_flags *bf;
2235 struct jailsys_flags *jsf;
2236 struct prison *pr, *mypr;
2238 struct vfsoptlist *opts;
2239 char *errmsg, *name;
2240 int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos;
2243 if (flags & ~JAIL_GET_MASK)
2246 /* Get the parameter list. */
2247 error = vfs_buildopts(optuio, &opts);
2250 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
2251 mypr = td->td_ucred->cr_prison;
2255 * Find the prison specified by one of: lastjid, jid, name.
2257 sx_slock(&allprison_lock);
2258 drflags = PD_LIST_SLOCKED;
2259 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
2261 TAILQ_FOREACH(pr, &allprison, pr_list) {
2262 if (pr->pr_id > jid &&
2263 ((flags & JAIL_DYING) || prison_isalive(pr)) &&
2264 prison_ischild(mypr, pr)) {
2265 mtx_lock(&pr->pr_mtx);
2266 drflags |= PD_LOCKED;
2271 vfs_opterror(opts, "no jail after %d", jid);
2273 } else if (error != ENOENT)
2276 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
2279 pr = prison_find_child(mypr, jid);
2281 drflags |= PD_LOCKED;
2282 if (!(prison_isalive(pr) ||
2283 (flags & JAIL_DYING))) {
2285 vfs_opterror(opts, "jail %d is dying",
2292 vfs_opterror(opts, "jail %d not found", jid);
2295 } else if (error != ENOENT)
2298 error = vfs_getopt(opts, "name", (void **)&name, &len);
2300 if (len == 0 || name[len - 1] != '\0') {
2304 pr = prison_find_name(mypr, name);
2306 drflags |= PD_LOCKED;
2307 if (!(prison_isalive(pr) || (flags & JAIL_DYING))) {
2309 vfs_opterror(opts, "jail \"%s\" is dying",
2316 vfs_opterror(opts, "jail \"%s\" not found", name);
2318 } else if (error != ENOENT)
2321 vfs_opterror(opts, "no jail specified");
2326 /* Get the parameters of the prison. */
2328 drflags |= PD_DEREF;
2329 td->td_retval[0] = pr->pr_id;
2330 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
2331 if (error != 0 && error != ENOENT)
2333 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
2334 error = vfs_setopt(opts, "parent", &i, sizeof(i));
2335 if (error != 0 && error != ENOENT)
2337 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
2338 if (error != 0 && error != ENOENT)
2340 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
2341 sizeof(pr->pr_cpuset->cs_id));
2342 if (error != 0 && error != ENOENT)
2344 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
2345 if (error != 0 && error != ENOENT)
2348 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1,
2349 pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips *
2350 pr_families[PR_INET].size : 0 );
2351 if (error != 0 && error != ENOENT)
2355 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1,
2356 pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips *
2357 pr_families[PR_INET6].size : 0 );
2358 if (error != 0 && error != ENOENT)
2361 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
2362 sizeof(pr->pr_securelevel));
2363 if (error != 0 && error != ENOENT)
2365 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
2366 sizeof(pr->pr_childcount));
2367 if (error != 0 && error != ENOENT)
2369 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
2370 sizeof(pr->pr_childmax));
2371 if (error != 0 && error != ENOENT)
2373 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
2374 if (error != 0 && error != ENOENT)
2376 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
2377 if (error != 0 && error != ENOENT)
2379 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
2380 if (error != 0 && error != ENOENT)
2382 #ifdef COMPAT_FREEBSD32
2383 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) {
2384 uint32_t hid32 = pr->pr_hostid;
2386 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
2389 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
2390 sizeof(pr->pr_hostid));
2391 if (error != 0 && error != ENOENT)
2393 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
2394 sizeof(pr->pr_enforce_statfs));
2395 if (error != 0 && error != ENOENT)
2397 error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum,
2398 sizeof(pr->pr_devfs_rsnum));
2399 if (error != 0 && error != ENOENT)
2401 for (bf = pr_flag_bool;
2402 bf < pr_flag_bool + nitems(pr_flag_bool);
2404 i = (pr->pr_flags & bf->flag) ? 1 : 0;
2405 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2406 if (error != 0 && error != ENOENT)
2409 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2410 if (error != 0 && error != ENOENT)
2413 for (jsf = pr_flag_jailsys;
2414 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
2416 f = pr->pr_flags & (jsf->disable | jsf->new);
2417 i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE
2418 : (f == jsf->new) ? JAIL_SYS_NEW
2420 error = vfs_setopt(opts, jsf->name, &i, sizeof(i));
2421 if (error != 0 && error != ENOENT)
2424 for (bf = pr_flag_allow;
2425 bf < pr_flag_allow + nitems(pr_flag_allow) &&
2426 atomic_load_int(&bf->flag) != 0;
2428 i = (pr->pr_allow & bf->flag) ? 1 : 0;
2429 error = vfs_setopt(opts, bf->name, &i, sizeof(i));
2430 if (error != 0 && error != ENOENT)
2433 error = vfs_setopt(opts, bf->noname, &i, sizeof(i));
2434 if (error != 0 && error != ENOENT)
2437 i = !prison_isalive(pr);
2438 error = vfs_setopt(opts, "dying", &i, sizeof(i));
2439 if (error != 0 && error != ENOENT)
2442 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
2443 if (error != 0 && error != ENOENT)
2445 error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate,
2446 sizeof(pr->pr_osreldate));
2447 if (error != 0 && error != ENOENT)
2449 error = vfs_setopts(opts, "osrelease", pr->pr_osrelease);
2450 if (error != 0 && error != ENOENT)
2453 /* Get the module parameters. */
2454 mtx_unlock(&pr->pr_mtx);
2455 drflags &= ~PD_LOCKED;
2456 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2459 prison_deref(pr, drflags);
2463 /* By now, all parameters should have been noted. */
2464 TAILQ_FOREACH(opt, opts, link) {
2465 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2467 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2472 /* Write the fetched parameters back to userspace. */
2474 TAILQ_FOREACH(opt, opts, link) {
2475 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2476 pos = 2 * opt->pos + 1;
2477 optuio->uio_iov[pos].iov_len = opt->len;
2478 if (opt->value != NULL) {
2479 if (optuio->uio_segflg == UIO_SYSSPACE) {
2481 optuio->uio_iov[pos].iov_base,
2484 error = copyout(opt->value,
2485 optuio->uio_iov[pos].iov_base,
2495 /* Release any temporary prison holds and/or locks. */
2497 prison_deref(pr, drflags);
2498 else if (drflags & PD_LIST_SLOCKED)
2499 sx_sunlock(&allprison_lock);
2500 if (error && errmsg_pos >= 0) {
2501 /* Write the error message back to userspace. */
2502 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2503 errmsg_pos = 2 * errmsg_pos + 1;
2504 if (errmsg_len > 0) {
2505 if (optuio->uio_segflg == UIO_SYSSPACE)
2507 optuio->uio_iov[errmsg_pos].iov_base,
2511 optuio->uio_iov[errmsg_pos].iov_base,
2520 * struct jail_remove_args {
2525 sys_jail_remove(struct thread *td, struct jail_remove_args *uap)
2530 error = priv_check(td, PRIV_JAIL_REMOVE);
2534 sx_xlock(&allprison_lock);
2535 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2537 sx_xunlock(&allprison_lock);
2540 if (!prison_isalive(pr)) {
2541 /* Silently ignore already-dying prisons. */
2542 mtx_unlock(&pr->pr_mtx);
2543 sx_xunlock(&allprison_lock);
2546 prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED);
2551 * struct jail_attach_args {
2556 sys_jail_attach(struct thread *td, struct jail_attach_args *uap)
2561 error = priv_check(td, PRIV_JAIL_ATTACH);
2565 sx_slock(&allprison_lock);
2566 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2568 sx_sunlock(&allprison_lock);
2572 /* Do not allow a process to attach to a prison that is not alive. */
2573 if (!prison_isalive(pr)) {
2574 mtx_unlock(&pr->pr_mtx);
2575 sx_sunlock(&allprison_lock);
2579 return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED));
2583 do_jail_attach(struct thread *td, struct prison *pr, int drflags)
2586 struct ucred *newcred, *oldcred;
2589 mtx_assert(&pr->pr_mtx, MA_OWNED);
2590 sx_assert(&allprison_lock, SX_LOCKED);
2591 drflags &= PD_LOCK_FLAGS;
2593 * XXX: Note that there is a slight race here if two threads
2594 * in the same privileged process attempt to attach to two
2595 * different jails at the same time. It is important for
2596 * user processes not to do this, or they might end up with
2597 * a process root from one prison, but attached to the jail
2601 refcount_acquire(&pr->pr_uref);
2602 drflags |= PD_DEREF | PD_DEUREF;
2603 mtx_unlock(&pr->pr_mtx);
2604 drflags &= ~PD_LOCKED;
2606 /* Let modules do whatever they need to prepare for attaching. */
2607 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2609 prison_deref(pr, drflags);
2612 sx_unlock(&allprison_lock);
2613 drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED);
2616 * Reparent the newly attached process to this jail.
2619 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2623 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2624 if ((error = change_dir(pr->pr_root, td)) != 0)
2627 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2630 VOP_UNLOCK(pr->pr_root);
2631 if ((error = pwd_chroot_chdir(td, pr->pr_root)))
2636 oldcred = crcopysafe(p, newcred);
2637 newcred->cr_prison = pr;
2638 proc_set_cred(p, newcred);
2641 racct_proc_ucred_changed(p, oldcred, newcred);
2646 rctl_proc_ucred_changed(p, newcred);
2649 prison_deref(oldcred->cr_prison, drflags);
2653 * If the prison was killed while changing credentials, die along
2656 if (!prison_isalive(pr)) {
2658 kern_psignal(p, SIGKILL);
2665 VOP_UNLOCK(pr->pr_root);
2667 /* Tell modules this thread is still in its old jail after all. */
2668 sx_slock(&allprison_lock);
2669 drflags |= PD_LIST_SLOCKED;
2670 (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td);
2671 prison_deref(pr, drflags);
2676 * Returns a locked prison instance, or NULL on failure.
2679 prison_find(int prid)
2683 sx_assert(&allprison_lock, SX_LOCKED);
2684 TAILQ_FOREACH(pr, &allprison, pr_list) {
2685 if (pr->pr_id < prid)
2687 if (pr->pr_id > prid)
2689 KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr));
2690 mtx_lock(&pr->pr_mtx);
2697 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2700 prison_find_child(struct prison *mypr, int prid)
2705 sx_assert(&allprison_lock, SX_LOCKED);
2706 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2707 if (pr->pr_id == prid) {
2708 KASSERT(prison_isvalid(pr),
2709 ("Found invalid prison %p", pr));
2710 mtx_lock(&pr->pr_mtx);
2718 * Look for the name relative to mypr. Returns a locked prison or NULL.
2721 prison_find_name(struct prison *mypr, const char *name)
2723 struct prison *pr, *deadpr;
2727 sx_assert(&allprison_lock, SX_LOCKED);
2728 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2730 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2731 if (!strcmp(pr->pr_name + mylen, name)) {
2732 KASSERT(prison_isvalid(pr),
2733 ("Found invalid prison %p", pr));
2734 if (prison_isalive(pr)) {
2735 mtx_lock(&pr->pr_mtx);
2741 /* There was no valid prison - perhaps there was a dying one. */
2743 mtx_lock(&deadpr->pr_mtx);
2748 * See if a prison has the specific flag set. The prison should be locked,
2749 * unless checking for flags that are only set at jail creation (such as
2750 * PR_IP4 and PR_IP6), or only the single bit is examined, without regard
2751 * to any other prison data.
2754 prison_flag(struct ucred *cred, unsigned flag)
2757 return (cred->cr_prison->pr_flags & flag);
2761 prison_allow(struct ucred *cred, unsigned flag)
2764 return ((cred->cr_prison->pr_allow & flag) != 0);
2768 * Hold a prison reference, by incrementing pr_ref. It is generally
2769 * an error to hold a prison that does not already have a reference.
2770 * A prison record will remain valid as long as it has at least one
2771 * reference, and will not be removed as long as either the prison
2772 * mutex or the allprison lock is held (allprison_lock may be shared).
2775 prison_hold_locked(struct prison *pr)
2778 /* Locking is no longer required. */
2783 prison_hold(struct prison *pr)
2786 int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref);
2789 ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id));
2791 refcount_acquire(&pr->pr_ref);
2796 * Remove a prison reference. If that was the last reference, the
2797 * prison will be removed (at a later time).
2800 prison_free_locked(struct prison *pr)
2803 mtx_assert(&pr->pr_mtx, MA_OWNED);
2805 * Locking is no longer required, but unlock because the caller
2808 mtx_unlock(&pr->pr_mtx);
2813 prison_free(struct prison *pr)
2816 KASSERT(refcount_load(&pr->pr_ref) > 0,
2817 ("Trying to free dead prison %p (jid=%d).",
2819 if (!refcount_release_if_not_last(&pr->pr_ref)) {
2821 * Don't remove the last reference in this context,
2822 * in case there are locks held.
2824 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2829 prison_free_not_last(struct prison *pr)
2834 KASSERT(refcount_load(&pr->pr_ref) > 0,
2835 ("Trying to free dead prison %p (jid=%d).",
2837 lastref = refcount_release(&pr->pr_ref);
2839 ("prison_free_not_last freed last ref on prison %p (jid=%d).",
2842 refcount_release(&pr->pr_ref);
2847 * Hold a a prison for user visibility, by incrementing pr_uref.
2848 * It is generally an error to hold a prison that isn't already
2849 * user-visible, except through the the jail system calls. It is also
2850 * an error to hold an invalid prison. A prison record will remain
2851 * alive as long as it has at least one user reference, and will not
2852 * be set to the dying state until the prison mutex and allprison_lock
2856 prison_proc_hold(struct prison *pr)
2859 int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref);
2862 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2864 refcount_acquire(&pr->pr_uref);
2869 * Remove a prison user reference. If it was the last reference, the
2870 * prison will be considered "dying", and may be removed once all of
2871 * its references are dropped.
2874 prison_proc_free(struct prison *pr)
2878 * Locking is only required when releasing the last reference.
2879 * This allows assurance that a locked prison will remain alive
2880 * until it is unlocked.
2882 KASSERT(refcount_load(&pr->pr_uref) > 0,
2883 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2884 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2886 * Don't remove the last user reference in this context,
2887 * which is expected to be a process that is not only locked,
2888 * but also half dead. Add a reference so any calls to
2889 * prison_free() won't re-submit the task.
2892 mtx_lock(&pr->pr_mtx);
2893 KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC),
2894 ("Redundant last reference in prison_proc_free (jid=%d)",
2896 pr->pr_flags |= PR_COMPLETE_PROC;
2897 mtx_unlock(&pr->pr_mtx);
2898 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2903 prison_proc_free_not_last(struct prison *pr)
2908 KASSERT(refcount_load(&pr->pr_uref) > 0,
2909 ("Trying to free dead prison %p (jid=%d).",
2911 lastref = refcount_release(&pr->pr_uref);
2913 ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).",
2916 refcount_release(&pr->pr_uref);
2921 * Complete a call to either prison_free or prison_proc_free.
2924 prison_complete(void *context, int pending)
2926 struct prison *pr = context;
2930 * This could be called to release the last reference, or the last
2931 * user reference (plus the reference held in prison_proc_free).
2933 drflags = prison_lock_xlock(pr, PD_DEREF);
2934 if (pr->pr_flags & PR_COMPLETE_PROC) {
2935 pr->pr_flags &= ~PR_COMPLETE_PROC;
2936 drflags |= PD_DEUREF;
2938 prison_deref(pr, drflags);
2942 * Remove a prison reference and/or user reference (usually).
2943 * This assumes context that allows sleeping (for allprison_lock),
2944 * with no non-sleeping locks held, except perhaps the prison itself.
2945 * If there are no more references, release and delist the prison.
2946 * On completion, the prison lock and the allprison lock are both
2950 prison_deref(struct prison *pr, int flags)
2952 struct prisonlist freeprison;
2953 struct prison *killpr, *rpr, *ppr, *tpr;
2957 TAILQ_INIT(&freeprison);
2959 * Release this prison as requested, which may cause its parent
2960 * to be released, and then maybe its grandparent, etc.
2963 if (flags & PD_KILL) {
2964 /* Kill the prison and its descendents. */
2965 KASSERT(pr != &prison0,
2966 ("prison_deref trying to kill prison0"));
2967 if (!(flags & PD_DEREF)) {
2971 flags = prison_lock_xlock(pr, flags);
2972 prison_deref_kill(pr, &freeprison);
2974 if (flags & PD_DEUREF) {
2975 /* Drop a user reference. */
2976 KASSERT(refcount_load(&pr->pr_uref) > 0,
2977 ("prison_deref PD_DEUREF on a dead prison (jid=%d)",
2979 if (!refcount_release_if_not_last(&pr->pr_uref)) {
2980 if (!(flags & PD_DEREF)) {
2984 flags = prison_lock_xlock(pr, flags);
2985 if (refcount_release(&pr->pr_uref) &&
2986 pr->pr_state == PRISON_STATE_ALIVE) {
2988 * When the last user references goes,
2989 * this becomes a dying prison.
2992 refcount_load(&prison0.pr_uref) > 0,
2993 ("prison0 pr_uref=0"));
2994 pr->pr_state = PRISON_STATE_DYING;
2995 mtx_unlock(&pr->pr_mtx);
2996 flags &= ~PD_LOCKED;
2997 (void)osd_jail_call(pr,
2998 PR_METHOD_REMOVE, NULL);
3002 if (flags & PD_KILL) {
3004 * Any remaining user references are probably processes
3005 * that need to be killed, either in this prison or its
3008 if (refcount_load(&pr->pr_uref) > 0)
3010 /* Make sure the parent prison doesn't get killed. */
3013 if (flags & PD_DEREF) {
3014 /* Drop a reference. */
3015 KASSERT(refcount_load(&pr->pr_ref) > 0,
3016 ("prison_deref PD_DEREF on a dead prison (jid=%d)",
3018 if (!refcount_release_if_not_last(&pr->pr_ref)) {
3019 flags = prison_lock_xlock(pr, flags);
3020 if (refcount_release(&pr->pr_ref)) {
3022 * When the last reference goes,
3023 * unlink the prison and set it aside.
3026 refcount_load(&pr->pr_uref) == 0,
3027 ("prison_deref: last ref, "
3028 "but still has %d urefs (jid=%d)",
3029 pr->pr_uref, pr->pr_id));
3031 refcount_load(&prison0.pr_ref) != 0,
3032 ("prison0 pr_ref=0"));
3033 pr->pr_state = PRISON_STATE_INVALID;
3034 TAILQ_REMOVE(&allprison, pr, pr_list);
3035 LIST_REMOVE(pr, pr_sibling);
3036 TAILQ_INSERT_TAIL(&freeprison, pr,
3038 for (ppr = pr->pr_parent;
3040 ppr = ppr->pr_parent)
3041 ppr->pr_childcount--;
3043 * Removing a prison frees references
3046 mtx_unlock(&pr->pr_mtx);
3047 flags &= ~PD_LOCKED;
3049 flags |= PD_DEREF | PD_DEUREF;
3057 /* Release all the prison locks. */
3058 if (flags & PD_LOCKED)
3059 mtx_unlock(&pr->pr_mtx);
3060 if (flags & PD_LIST_SLOCKED)
3061 sx_sunlock(&allprison_lock);
3062 else if (flags & PD_LIST_XLOCKED)
3063 sx_xunlock(&allprison_lock);
3065 /* Kill any processes attached to a killed prison. */
3066 if (killpr != NULL) {
3067 sx_slock(&allproc_lock);
3068 FOREACH_PROC_IN_SYSTEM(p) {
3070 if (p->p_state != PRS_NEW && p->p_ucred != NULL) {
3071 for (ppr = p->p_ucred->cr_prison;
3073 ppr = ppr->pr_parent)
3074 if (ppr == killpr) {
3075 kern_psignal(p, SIGKILL);
3081 sx_sunlock(&allproc_lock);
3085 * Finish removing any unreferenced prisons, which couldn't happen
3086 * while allprison_lock was held (to avoid a LOR on vrele).
3088 TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) {
3090 if (rpr->pr_vnet != rpr->pr_parent->pr_vnet)
3091 vnet_destroy(rpr->pr_vnet);
3093 if (rpr->pr_root != NULL)
3094 vrele(rpr->pr_root);
3095 mtx_destroy(&rpr->pr_mtx);
3097 prison_ip_free(rpr->pr_addrs[PR_INET]);
3100 prison_ip_free(rpr->pr_addrs[PR_INET6]);
3102 if (rpr->pr_cpuset != NULL)
3103 cpuset_rel(rpr->pr_cpuset);
3107 prison_racct_detach(rpr);
3109 TAILQ_REMOVE(&freeprison, rpr, pr_list);
3110 free(rpr, M_PRISON);
3115 * Kill the prison and its descendants. Mark them as dying, clear the
3116 * persist flag, and call module remove methods.
3119 prison_deref_kill(struct prison *pr, struct prisonlist *freeprison)
3121 struct prison *cpr, *ppr, *rpr;
3125 * Unlike the descendants, the target prison can be killed
3126 * even if it is currently dying. This is useful for failed
3127 * creation in jail_set(2).
3129 KASSERT(refcount_load(&pr->pr_ref) > 0,
3130 ("Trying to kill dead prison %p (jid=%d).",
3132 refcount_acquire(&pr->pr_uref);
3133 pr->pr_state = PRISON_STATE_DYING;
3134 mtx_unlock(&pr->pr_mtx);
3137 FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) {
3139 if (!prison_isalive(cpr)) {
3144 prison_proc_hold(cpr);
3145 mtx_lock(&cpr->pr_mtx);
3146 cpr->pr_state = PRISON_STATE_DYING;
3147 cpr->pr_flags |= PR_REMOVE;
3148 mtx_unlock(&cpr->pr_mtx);
3151 if (!(cpr->pr_flags & PR_REMOVE))
3153 (void)osd_jail_call(cpr, PR_METHOD_REMOVE, NULL);
3154 mtx_lock(&cpr->pr_mtx);
3155 cpr->pr_flags &= ~PR_REMOVE;
3156 if (cpr->pr_flags & PR_PERSIST) {
3157 cpr->pr_flags &= ~PR_PERSIST;
3158 prison_proc_free_not_last(cpr);
3159 prison_free_not_last(cpr);
3161 (void)refcount_release(&cpr->pr_uref);
3162 if (refcount_release(&cpr->pr_ref)) {
3164 * When the last reference goes, unlink the prison
3165 * and set it aside for prison_deref() to handle.
3166 * Delay unlinking the sibling list to keep the loop
3170 LIST_REMOVE(rpr, pr_sibling);
3172 rpr->pr_state = PRISON_STATE_INVALID;
3173 TAILQ_REMOVE(&allprison, rpr, pr_list);
3174 TAILQ_INSERT_TAIL(freeprison, rpr, pr_list);
3176 * Removing a prison frees references from its parent.
3178 ppr = rpr->pr_parent;
3179 prison_proc_free_not_last(ppr);
3180 prison_free_not_last(ppr);
3181 for (; ppr != NULL; ppr = ppr->pr_parent)
3182 ppr->pr_childcount--;
3184 mtx_unlock(&cpr->pr_mtx);
3187 LIST_REMOVE(rpr, pr_sibling);
3189 (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL);
3190 mtx_lock(&pr->pr_mtx);
3191 if (pr->pr_flags & PR_PERSIST) {
3192 pr->pr_flags &= ~PR_PERSIST;
3193 prison_proc_free_not_last(pr);
3194 prison_free_not_last(pr);
3196 (void)refcount_release(&pr->pr_uref);
3200 * Given the current locking state in the flags, make sure allprison_lock
3201 * is held exclusive, and the prison is locked. Return flags indicating
3205 prison_lock_xlock(struct prison *pr, int flags)
3208 if (!(flags & PD_LIST_XLOCKED)) {
3210 * Get allprison_lock, which may be an upgrade,
3211 * and may require unlocking the prison.
3213 if (flags & PD_LOCKED) {
3214 mtx_unlock(&pr->pr_mtx);
3215 flags &= ~PD_LOCKED;
3217 if (flags & PD_LIST_SLOCKED) {
3218 if (!sx_try_upgrade(&allprison_lock)) {
3219 sx_sunlock(&allprison_lock);
3220 sx_xlock(&allprison_lock);
3222 flags &= ~PD_LIST_SLOCKED;
3224 sx_xlock(&allprison_lock);
3225 flags |= PD_LIST_XLOCKED;
3227 if (!(flags & PD_LOCKED)) {
3228 /* Lock the prison mutex. */
3229 mtx_lock(&pr->pr_mtx);
3236 * Set or clear a permission bit in the pr_allow field, passing restrictions
3237 * (cleared permission) down to child jails.
3240 prison_set_allow(struct ucred *cred, unsigned flag, int enable)
3244 pr = cred->cr_prison;
3245 sx_slock(&allprison_lock);
3246 mtx_lock(&pr->pr_mtx);
3247 prison_set_allow_locked(pr, flag, enable);
3248 mtx_unlock(&pr->pr_mtx);
3249 sx_sunlock(&allprison_lock);
3253 prison_set_allow_locked(struct prison *pr, unsigned flag, int enable)
3259 pr->pr_allow |= flag;
3261 pr->pr_allow &= ~flag;
3262 FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend)
3263 cpr->pr_allow &= ~flag;
3268 * Check if a jail supports the given address family.
3270 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3274 prison_check_af(struct ucred *cred, int af)
3279 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3281 pr = cred->cr_prison;
3283 /* Prisons with their own network stack are not limited. */
3284 if (prison_owns_vnet(cred))
3293 if (pr->pr_flags & PR_IP4)
3295 mtx_lock(&pr->pr_mtx);
3296 if ((pr->pr_flags & PR_IP4) &&
3297 pr->pr_addrs[PR_INET] == NULL)
3298 error = EAFNOSUPPORT;
3299 mtx_unlock(&pr->pr_mtx);
3305 if (pr->pr_flags & PR_IP6)
3307 mtx_lock(&pr->pr_mtx);
3308 if ((pr->pr_flags & PR_IP6) &&
3309 pr->pr_addrs[PR_INET6] == NULL)
3310 error = EAFNOSUPPORT;
3311 mtx_unlock(&pr->pr_mtx);
3319 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3320 error = EAFNOSUPPORT;
3326 * Check if given address belongs to the jail referenced by cred (wrapper to
3327 * prison_check_ip[46]).
3329 * Returns 0 if jail doesn't restrict the address family or if address belongs
3330 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3331 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3334 prison_if(struct ucred *cred, const struct sockaddr *sa)
3337 const struct sockaddr_in *sai;
3340 const struct sockaddr_in6 *sai6;
3344 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3345 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3348 if (prison_owns_vnet(cred))
3353 switch (sa->sa_family)
3357 sai = (const struct sockaddr_in *)sa;
3358 error = prison_check_ip4(cred, &sai->sin_addr);
3363 sai6 = (const struct sockaddr_in6 *)sa;
3364 error = prison_check_ip6(cred, &sai6->sin6_addr);
3368 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3369 error = EAFNOSUPPORT;
3375 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3378 prison_check(struct ucred *cred1, struct ucred *cred2)
3381 return ((cred1->cr_prison == cred2->cr_prison ||
3382 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3386 * Return 1 if p2 is a child of p1, otherwise 0.
3389 prison_ischild(struct prison *pr1, struct prison *pr2)
3392 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3399 * Return true if the prison is currently alive. A prison is alive if it
3400 * holds user references and it isn't being removed.
3403 prison_isalive(const struct prison *pr)
3406 if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE))
3412 * Return true if the prison is currently valid. A prison is valid if it has
3413 * been fully created, and is not being destroyed. Note that dying prisons
3414 * are still considered valid. Invalid prisons won't be found under normal
3415 * circumstances, as they're only put in that state by functions that have
3416 * an exclusive hold on allprison_lock.
3419 prison_isvalid(struct prison *pr)
3422 if (__predict_false(pr->pr_state == PRISON_STATE_INVALID))
3424 if (__predict_false(refcount_load(&pr->pr_ref) == 0))
3430 * Return 1 if the passed credential is in a jail and that jail does not
3431 * have its own virtual network stack, otherwise 0.
3434 jailed_without_vnet(struct ucred *cred)
3440 if (prison_owns_vnet(cred))
3448 * Return the correct hostname (domainname, et al) for the passed credential.
3451 getcredhostname(struct ucred *cred, char *buf, size_t size)
3456 * A NULL credential can be used to shortcut to the physical
3457 * system's hostname.
3459 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3460 mtx_lock(&pr->pr_mtx);
3461 strlcpy(buf, pr->pr_hostname, size);
3462 mtx_unlock(&pr->pr_mtx);
3466 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3469 mtx_lock(&cred->cr_prison->pr_mtx);
3470 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3471 mtx_unlock(&cred->cr_prison->pr_mtx);
3475 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3478 mtx_lock(&cred->cr_prison->pr_mtx);
3479 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3480 mtx_unlock(&cred->cr_prison->pr_mtx);
3484 getcredhostid(struct ucred *cred, unsigned long *hostid)
3487 mtx_lock(&cred->cr_prison->pr_mtx);
3488 *hostid = cred->cr_prison->pr_hostid;
3489 mtx_unlock(&cred->cr_prison->pr_mtx);
3493 getjailname(struct ucred *cred, char *name, size_t len)
3496 mtx_lock(&cred->cr_prison->pr_mtx);
3497 strlcpy(name, cred->cr_prison->pr_name, len);
3498 mtx_unlock(&cred->cr_prison->pr_mtx);
3503 * Determine whether the prison represented by cred owns
3504 * its vnet rather than having it inherited.
3506 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3509 prison_owns_vnet(struct ucred *cred)
3513 * vnets cannot be added/removed after jail creation,
3514 * so no need to lock here.
3516 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3521 * Determine whether the subject represented by cred can "see"
3522 * status of a mount point.
3523 * Returns: 0 for permitted, ENOENT otherwise.
3524 * XXX: This function should be called cr_canseemount() and should be
3525 * placed in kern_prot.c.
3528 prison_canseemount(struct ucred *cred, struct mount *mp)
3534 pr = cred->cr_prison;
3535 if (pr->pr_enforce_statfs == 0)
3537 if (pr->pr_root->v_mount == mp)
3539 if (pr->pr_enforce_statfs == 2)
3542 * If jail's chroot directory is set to "/" we should be able to see
3543 * all mount-points from inside a jail.
3544 * This is ugly check, but this is the only situation when jail's
3545 * directory ends with '/'.
3547 if (strcmp(pr->pr_path, "/") == 0)
3549 len = strlen(pr->pr_path);
3551 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3554 * Be sure that we don't have situation where jail's root directory
3555 * is "/some/path" and mount point is "/some/pathpath".
3557 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3563 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3565 char jpath[MAXPATHLEN];
3569 pr = cred->cr_prison;
3570 if (pr->pr_enforce_statfs == 0)
3572 if (prison_canseemount(cred, mp) != 0) {
3573 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3574 strlcpy(sp->f_mntonname, "[restricted]",
3575 sizeof(sp->f_mntonname));
3578 if (pr->pr_root->v_mount == mp) {
3580 * Clear current buffer data, so we are sure nothing from
3581 * the valid path left there.
3583 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3584 *sp->f_mntonname = '/';
3588 * If jail's chroot directory is set to "/" we should be able to see
3589 * all mount-points from inside a jail.
3591 if (strcmp(pr->pr_path, "/") == 0)
3593 len = strlen(pr->pr_path);
3594 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3596 * Clear current buffer data, so we are sure nothing from
3597 * the valid path left there.
3599 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3600 if (*jpath == '\0') {
3601 /* Should never happen. */
3602 *sp->f_mntonname = '/';
3604 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3609 * Check with permission for a specific privilege is granted within jail. We
3610 * have a specific list of accepted privileges; the rest are denied.
3613 prison_priv_check(struct ucred *cred, int priv)
3619 * Some policies have custom handlers. This routine should not be
3620 * called for them. See priv_check_cred().
3623 case PRIV_VFS_LOOKUP:
3624 case PRIV_VFS_GENERATION:
3625 KASSERT(0, ("prison_priv_check instead of a custom handler "
3626 "called for %d\n", priv));
3634 * Privileges specific to prisons with a virtual network stack.
3635 * There might be a duplicate entry here in case the privilege
3636 * is only granted conditionally in the legacy jail case.
3641 * NFS-specific privileges.
3643 case PRIV_NFS_DAEMON:
3644 case PRIV_NFS_LOCKD:
3647 * Network stack privileges.
3649 case PRIV_NET_BRIDGE:
3652 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3653 case PRIV_NET_ROUTE:
3655 case PRIV_NET_SETIFMTU:
3656 case PRIV_NET_SETIFFLAGS:
3657 case PRIV_NET_SETIFCAP:
3658 case PRIV_NET_SETIFDESCR:
3659 case PRIV_NET_SETIFNAME :
3660 case PRIV_NET_SETIFMETRIC:
3661 case PRIV_NET_SETIFPHYS:
3662 case PRIV_NET_SETIFMAC:
3663 case PRIV_NET_SETLANPCP:
3664 case PRIV_NET_ADDMULTI:
3665 case PRIV_NET_DELMULTI:
3666 case PRIV_NET_HWIOCTL:
3667 case PRIV_NET_SETLLADDR:
3668 case PRIV_NET_ADDIFGROUP:
3669 case PRIV_NET_DELIFGROUP:
3670 case PRIV_NET_IFCREATE:
3671 case PRIV_NET_IFDESTROY:
3672 case PRIV_NET_ADDIFADDR:
3673 case PRIV_NET_DELIFADDR:
3676 case PRIV_NET_SETIFVNET:
3677 case PRIV_NET_SETIFFIB:
3680 * 802.11-related privileges.
3682 case PRIV_NET80211_VAP_GETKEY:
3683 case PRIV_NET80211_VAP_MANAGE:
3689 case PRIV_NETATM_CFG:
3690 case PRIV_NETATM_ADD:
3691 case PRIV_NETATM_DEL:
3692 case PRIV_NETATM_SET:
3695 * Bluetooth privileges.
3697 case PRIV_NETBLUETOOTH_RAW:
3701 * Netgraph and netgraph module privileges.
3703 case PRIV_NETGRAPH_CONTROL:
3705 case PRIV_NETGRAPH_TTY:
3709 * IPv4 and IPv6 privileges.
3711 case PRIV_NETINET_IPFW:
3712 case PRIV_NETINET_DIVERT:
3713 case PRIV_NETINET_PF:
3714 case PRIV_NETINET_DUMMYNET:
3715 case PRIV_NETINET_CARP:
3716 case PRIV_NETINET_MROUTE:
3717 case PRIV_NETINET_RAW:
3718 case PRIV_NETINET_ADDRCTRL6:
3719 case PRIV_NETINET_ND6:
3720 case PRIV_NETINET_SCOPE6:
3721 case PRIV_NETINET_ALIFETIME6:
3722 case PRIV_NETINET_IPSEC:
3723 case PRIV_NETINET_BINDANY:
3738 * No default: or deny here.
3739 * In case of no permit fall through to next switch().
3741 if (cred->cr_prison->pr_flags & PR_VNET)
3748 * Allow ktrace privileges for root in jail.
3754 * Allow jailed processes to configure audit identity and
3755 * submit audit records (login, etc). In the future we may
3756 * want to further refine the relationship between audit and
3759 case PRIV_AUDIT_GETAUDIT:
3760 case PRIV_AUDIT_SETAUDIT:
3761 case PRIV_AUDIT_SUBMIT:
3765 * Allow jailed processes to manipulate process UNIX
3766 * credentials in any way they see fit.
3768 case PRIV_CRED_SETUID:
3769 case PRIV_CRED_SETEUID:
3770 case PRIV_CRED_SETGID:
3771 case PRIV_CRED_SETEGID:
3772 case PRIV_CRED_SETGROUPS:
3773 case PRIV_CRED_SETREUID:
3774 case PRIV_CRED_SETREGID:
3775 case PRIV_CRED_SETRESUID:
3776 case PRIV_CRED_SETRESGID:
3779 * Jail implements visibility constraints already, so allow
3780 * jailed root to override uid/gid-based constraints.
3782 case PRIV_SEEOTHERGIDS:
3783 case PRIV_SEEOTHERUIDS:
3786 * Jail implements inter-process debugging limits already, so
3787 * allow jailed root various debugging privileges.
3789 case PRIV_DEBUG_DIFFCRED:
3790 case PRIV_DEBUG_SUGID:
3791 case PRIV_DEBUG_UNPRIV:
3794 * Allow jail to set various resource limits and login
3795 * properties, and for now, exceed process resource limits.
3797 case PRIV_PROC_LIMIT:
3798 case PRIV_PROC_SETLOGIN:
3799 case PRIV_PROC_SETRLIMIT:
3802 * System V and POSIX IPC privileges are granted in jail.
3805 case PRIV_IPC_WRITE:
3806 case PRIV_IPC_ADMIN:
3807 case PRIV_IPC_MSGSIZE:
3811 * Jail operations within a jail work on child jails.
3813 case PRIV_JAIL_ATTACH:
3815 case PRIV_JAIL_REMOVE:
3818 * Jail implements its own inter-process limits, so allow
3819 * root processes in jail to change scheduling on other
3820 * processes in the same jail. Likewise for signalling.
3822 case PRIV_SCHED_DIFFCRED:
3823 case PRIV_SCHED_CPUSET:
3824 case PRIV_SIGNAL_DIFFCRED:
3825 case PRIV_SIGNAL_SUGID:
3828 * Allow jailed processes to write to sysctls marked as jail
3831 case PRIV_SYSCTL_WRITEJAIL:
3834 * Allow root in jail to manage a variety of quota
3835 * properties. These should likely be conditional on a
3836 * configuration option.
3838 case PRIV_VFS_GETQUOTA:
3839 case PRIV_VFS_SETQUOTA:
3842 * Since Jail relies on chroot() to implement file system
3843 * protections, grant many VFS privileges to root in jail.
3844 * Be careful to exclude mount-related and NFS-related
3848 case PRIV_VFS_WRITE:
3849 case PRIV_VFS_ADMIN:
3851 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3852 case PRIV_VFS_CHFLAGS_DEV:
3853 case PRIV_VFS_CHOWN:
3854 case PRIV_VFS_CHROOT:
3855 case PRIV_VFS_RETAINSUGID:
3856 case PRIV_VFS_FCHROOT:
3858 case PRIV_VFS_SETGID:
3860 case PRIV_VFS_STICKYFILE:
3863 * As in the non-jail case, non-root users are expected to be
3864 * able to read kernel/physical memory (provided /dev/[k]mem
3865 * exists in the jail and they have permission to access it).
3867 case PRIV_KMEM_READ:
3871 * Depending on the global setting, allow privilege of
3872 * setting system flags.
3874 case PRIV_VFS_SYSFLAGS:
3875 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3881 * Depending on the global setting, allow privilege of
3882 * mounting/unmounting file systems.
3884 case PRIV_VFS_MOUNT:
3885 case PRIV_VFS_UNMOUNT:
3886 case PRIV_VFS_MOUNT_NONUSER:
3887 case PRIV_VFS_MOUNT_OWNER:
3888 pr = cred->cr_prison;
3890 if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2)
3898 * Jails should hold no disposition on the PRIV_VFS_READ_DIR
3899 * policy. priv_check_cred will not specifically allow it, and
3900 * we may want a MAC policy to allow it.
3902 case PRIV_VFS_READ_DIR:
3906 * Conditionnaly allow locking (unlocking) physical pages
3910 case PRIV_VM_MUNLOCK:
3911 if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK)
3917 * Conditionally allow jailed root to bind reserved ports.
3919 case PRIV_NETINET_RESERVEDPORT:
3920 if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS)
3926 * Allow jailed root to reuse in-use ports.
3928 case PRIV_NETINET_REUSEPORT:
3932 * Allow jailed root to set certain IPv4/6 (option) headers.
3934 case PRIV_NETINET_SETHDROPTS:
3938 * Conditionally allow creating raw sockets in jail.
3940 case PRIV_NETINET_RAW:
3941 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3947 * Since jail implements its own visibility limits on netstat
3948 * sysctls, allow getcred. This allows identd to work in
3951 case PRIV_NETINET_GETCRED:
3955 * Allow jailed root to set loginclass.
3957 case PRIV_PROC_SETLOGINCLASS:
3961 * Do not allow a process inside a jail to read the kernel
3962 * message buffer unless explicitly permitted.
3965 if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF)
3971 * In all remaining cases, deny the privilege request. This
3972 * includes almost all network privileges, many system
3973 * configuration privileges.
3980 * Return the part of pr2's name that is relative to pr1, or the whole name
3981 * if it does not directly follow.
3985 prison_name(struct prison *pr1, struct prison *pr2)
3989 /* Jails see themselves as "0" (if they see themselves at all). */
3992 name = pr2->pr_name;
3993 if (prison_ischild(pr1, pr2)) {
3995 * pr1 isn't locked (and allprison_lock may not be either)
3996 * so its length can't be counted on. But the number of dots
3997 * can be counted on - and counted.
3999 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
4000 name = strchr(name, '.') + 1;
4006 * Return the part of pr2's path that is relative to pr1, or the whole path
4007 * if it does not directly follow.
4010 prison_path(struct prison *pr1, struct prison *pr2)
4012 char *path1, *path2;
4015 path1 = pr1->pr_path;
4016 path2 = pr2->pr_path;
4017 if (!strcmp(path1, "/"))
4019 len1 = strlen(path1);
4020 if (strncmp(path1, path2, len1))
4022 if (path2[len1] == '\0')
4024 if (path2[len1] == '/')
4025 return (path2 + len1);
4030 * Jail-related sysctls.
4032 static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4035 #if defined(INET) || defined(INET6)
4037 * Copy address array to memory that would be then SYSCTL_OUT-ed.
4038 * sysctl_jail_list() helper.
4041 prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len)
4043 const size_t size = pr_families[af].size;
4046 mtx_assert(&pr->pr_mtx, MA_OWNED);
4047 if (pr->pr_addrs[af] != NULL) {
4048 if (*len < pr->pr_addrs[af]->ips) {
4049 *len = pr->pr_addrs[af]->ips;
4050 mtx_unlock(&pr->pr_mtx);
4051 *out = realloc(*out, *len * size, M_TEMP, M_WAITOK);
4052 mtx_lock(&pr->pr_mtx);
4055 bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size);
4061 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
4064 struct prison *pr, *cpr;
4066 struct in_addr *ip4 = NULL;
4070 struct in6_addr *ip6 = NULL;
4075 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
4076 pr = req->td->td_ucred->cr_prison;
4078 sx_slock(&allprison_lock);
4079 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
4080 mtx_lock(&cpr->pr_mtx);
4082 prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s);
4085 prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s);
4087 bzero(xp, sizeof(*xp));
4088 xp->pr_version = XPRISON_VERSION;
4089 xp->pr_id = cpr->pr_id;
4090 xp->pr_state = cpr->pr_state;
4091 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
4092 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
4093 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
4100 mtx_unlock(&cpr->pr_mtx);
4101 error = SYSCTL_OUT(req, xp, sizeof(*xp));
4105 if (xp->pr_ip4s > 0) {
4106 error = SYSCTL_OUT(req, ip4,
4107 xp->pr_ip4s * sizeof(struct in_addr));
4113 if (xp->pr_ip6s > 0) {
4114 error = SYSCTL_OUT(req, ip6,
4115 xp->pr_ip6s * sizeof(struct in6_addr));
4121 sx_sunlock(&allprison_lock);
4132 SYSCTL_OID(_security_jail, OID_AUTO, list,
4133 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4134 sysctl_jail_list, "S", "List of active jails");
4137 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
4141 injail = jailed(req->td->td_ucred);
4142 error = SYSCTL_OUT(req, &injail, sizeof(injail));
4147 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
4148 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4149 sysctl_jail_jailed, "I", "Process in jail?");
4152 sysctl_jail_vnet(SYSCTL_HANDLER_ARGS)
4154 int error, havevnet;
4156 struct ucred *cred = req->td->td_ucred;
4158 havevnet = jailed(cred) && prison_owns_vnet(cred);
4162 error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet));
4167 SYSCTL_PROC(_security_jail, OID_AUTO, vnet,
4168 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
4169 sysctl_jail_vnet, "I", "Jail owns vnet?");
4171 #if defined(INET) || defined(INET6)
4172 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
4173 &jail_max_af_ips, 0,
4174 "Number of IP addresses a jail may have at most per address family (deprecated)");
4178 * Default parameters for jail(2) compatibility. For historical reasons,
4179 * the sysctl names have varying similarity to the parameter names. Prisons
4180 * just see their own parameters, and can't change them.
4183 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
4187 /* Get the current flag value, and convert it to a boolean. */
4188 if (req->td->td_ucred->cr_prison == &prison0) {
4189 mtx_lock(&prison0.pr_mtx);
4190 i = (jail_default_allow & arg2) != 0;
4191 mtx_unlock(&prison0.pr_mtx);
4193 i = prison_allow(req->td->td_ucred, arg2);
4197 error = sysctl_handle_int(oidp, &i, 0, req);
4198 if (error || !req->newptr)
4204 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
4207 mtx_lock(&prison0.pr_mtx);
4208 jail_default_allow = (jail_default_allow & ~arg2) | i;
4209 mtx_unlock(&prison0.pr_mtx);
4213 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
4214 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4215 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
4216 "Processes in jail can set their hostnames (deprecated)");
4217 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
4218 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4219 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
4220 "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)");
4221 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
4222 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4223 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
4224 "Processes in jail can use System V IPC primitives (deprecated)");
4225 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
4226 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4227 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
4228 "Prison root can create raw sockets (deprecated)");
4229 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
4230 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4231 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
4232 "Processes in jail can alter system file flags (deprecated)");
4233 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
4234 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4235 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
4236 "Processes in jail can mount/unmount jail-friendly file systems (deprecated)");
4239 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
4244 pr = req->td->td_ucred->cr_prison;
4245 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4246 error = sysctl_handle_int(oidp, &level, 0, req);
4247 if (error || !req->newptr)
4249 *(int *)arg1 = level;
4253 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4254 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4255 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4256 sysctl_jail_default_level, "I",
4257 "Processes in jail cannot see all mounted file systems (deprecated)");
4259 SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset,
4260 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE,
4261 &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum),
4262 sysctl_jail_default_level, "I",
4263 "Ruleset for the devfs filesystem in jail (deprecated)");
4266 * Nodes to describe jail parameters. Maximum length of string parameters
4267 * is returned in the string itself, and the other parameters exist merely
4268 * to make themselves and their types known.
4270 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
4274 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4281 switch (oidp->oid_kind & CTLTYPE)
4287 if (!(req->flags & SCTL_MASK32))
4289 return (SYSCTL_OUT(req, &l, sizeof(l)));
4293 return (SYSCTL_OUT(req, &i, sizeof(i)));
4294 case CTLTYPE_STRING:
4295 snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2);
4297 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4298 case CTLTYPE_STRUCT:
4300 return (SYSCTL_OUT(req, &s, sizeof(s)));
4306 * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at
4307 * jail creation time but cannot be changed in an existing jail.
4309 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4310 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4311 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4312 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4313 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4314 "I", "Jail secure level");
4315 SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I",
4316 "Jail value for kern.osreldate and uname -K");
4317 SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN,
4318 "Jail value for kern.osrelease and uname -r");
4319 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4320 "I", "Jail cannot see all mounted file systems");
4321 SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW,
4322 "I", "Ruleset for in-jail devfs mounts");
4323 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4324 "B", "Jail persistence");
4326 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4327 "E,jailsys", "Virtual network stack");
4329 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4330 "B", "Jail is in the process of shutting down");
4332 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4333 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4334 "I", "Current number of child jails");
4335 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4336 "I", "Maximum number of child jails");
4338 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4339 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4341 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4342 "Jail NIS domainname");
4343 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4345 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4346 "LU", "Jail host ID");
4348 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4349 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4352 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4353 "Jail IPv4 address virtualization");
4354 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4355 "S,in_addr,a", "Jail IPv4 addresses");
4356 SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4357 "B", "Do (not) use IPv4 source address selection rather than the "
4358 "primary jail IPv4 address.");
4361 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4362 "Jail IPv6 address virtualization");
4363 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4364 "S,in6_addr,a", "Jail IPv6 addresses");
4365 SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW,
4366 "B", "Do (not) use IPv6 source address selection rather than the "
4367 "primary jail IPv6 address.");
4370 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4371 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4372 "B", "Jail may set hostname");
4373 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4374 "B", "Jail may use SYSV IPC");
4375 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4376 "B", "Jail may create raw sockets");
4377 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4378 "B", "Jail may alter system file flags");
4379 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4380 "B", "Jail may set file quotas");
4381 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4382 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4383 SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW,
4384 "B", "Jail may lock (unlock) physical pages in memory");
4385 SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW,
4386 "B", "Jail may bind sockets to reserved ports");
4387 SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW,
4388 "B", "Jail may read the kernel message buffer");
4389 SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW,
4390 "B", "Unprivileged processes may use process debugging facilities");
4391 SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW,
4392 "B", "Processes in jail with uid 0 have privilege");
4394 SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags");
4395 SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW,
4396 "B", "Jail may mount/unmount jail-friendly file systems in general");
4399 * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return
4400 * its associated bit in the pr_allow bitmask, or zero if the parameter was
4404 prison_add_allow(const char *prefix, const char *name, const char *prefix_descr,
4407 struct bool_flags *bf;
4408 struct sysctl_oid *parent;
4409 char *allow_name, *allow_noname, *allowed;
4410 #ifndef NO_SYSCTL_DESCR
4411 char *descr_deprecated;
4416 ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name)
4418 asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name)
4420 : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 ||
4421 asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) {
4422 free(allow_name, M_PRISON);
4427 * See if this parameter has already beed added, i.e. a module was
4428 * previously loaded/unloaded.
4430 mtx_lock(&prison0.pr_mtx);
4431 for (bf = pr_flag_allow;
4432 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4433 atomic_load_int(&bf->flag) != 0;
4435 if (strcmp(bf->name, allow_name) == 0) {
4436 allow_flag = bf->flag;
4442 * Find a free bit in pr_allow_all, failing if there are none
4443 * (which shouldn't happen as long as we keep track of how many
4444 * potential dynamic flags exist).
4446 for (allow_flag = 1;; allow_flag <<= 1) {
4447 if (allow_flag == 0)
4449 if ((pr_allow_all & allow_flag) == 0)
4453 /* Note the parameter in the next open slot in pr_flag_allow. */
4454 for (bf = pr_flag_allow; ; bf++) {
4455 if (bf == pr_flag_allow + nitems(pr_flag_allow)) {
4456 /* This should never happen, but is not fatal. */
4460 if (atomic_load_int(&bf->flag) == 0)
4463 bf->name = allow_name;
4464 bf->noname = allow_noname;
4465 pr_allow_all |= allow_flag;
4467 * prison0 always has permission for the new parameter.
4468 * Other jails must have it granted to them.
4470 prison0.pr_allow |= allow_flag;
4471 /* The flag indicates a valid entry, so make sure it is set last. */
4472 atomic_store_rel_int(&bf->flag, allow_flag);
4473 mtx_unlock(&prison0.pr_mtx);
4476 * Create sysctls for the paramter, and the back-compat global
4480 ? SYSCTL_ADD_NODE(NULL,
4481 SYSCTL_CHILDREN(&sysctl___security_jail_param_allow),
4482 OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr)
4483 : &sysctl___security_jail_param_allow;
4484 (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO,
4485 name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4486 NULL, 0, sysctl_jail_param, "B", descr);
4488 ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name)
4489 : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) {
4490 #ifndef NO_SYSCTL_DESCR
4491 (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)",
4494 (void)SYSCTL_ADD_PROC(NULL,
4495 SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed,
4496 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag,
4497 sysctl_jail_default_allow, "I", descr_deprecated);
4498 #ifndef NO_SYSCTL_DESCR
4499 free(descr_deprecated, M_TEMP);
4501 free(allowed, M_TEMP);
4506 mtx_unlock(&prison0.pr_mtx);
4507 free(allow_name, M_PRISON);
4508 free(allow_noname, M_PRISON);
4513 * The VFS system will register jail-aware filesystems here. They each get
4514 * a parameter allow.mount.xxxfs and a flag to check when a jailed user
4515 * attempts to mount.
4518 prison_add_vfs(struct vfsconf *vfsp)
4520 #ifdef NO_SYSCTL_DESCR
4522 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4527 (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system",
4529 vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name,
4531 free(descr, M_TEMP);
4537 prison_racct_foreach(void (*callback)(struct racct *racct,
4538 void *arg2, void *arg3), void (*pre)(void), void (*post)(void),
4539 void *arg2, void *arg3)
4541 struct prison_racct *prr;
4543 ASSERT_RACCT_ENABLED();
4545 sx_slock(&allprison_lock);
4548 LIST_FOREACH(prr, &allprison_racct, prr_next)
4549 (callback)(prr->prr_racct, arg2, arg3);
4552 sx_sunlock(&allprison_lock);
4555 static struct prison_racct *
4556 prison_racct_find_locked(const char *name)
4558 struct prison_racct *prr;
4560 ASSERT_RACCT_ENABLED();
4561 sx_assert(&allprison_lock, SA_XLOCKED);
4563 if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN)
4566 LIST_FOREACH(prr, &allprison_racct, prr_next) {
4567 if (strcmp(name, prr->prr_name) != 0)
4570 /* Found prison_racct with a matching name? */
4571 prison_racct_hold(prr);
4575 /* Add new prison_racct. */
4576 prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK);
4577 racct_create(&prr->prr_racct);
4579 strcpy(prr->prr_name, name);
4580 refcount_init(&prr->prr_refcount, 1);
4581 LIST_INSERT_HEAD(&allprison_racct, prr, prr_next);
4586 struct prison_racct *
4587 prison_racct_find(const char *name)
4589 struct prison_racct *prr;
4591 ASSERT_RACCT_ENABLED();
4593 sx_xlock(&allprison_lock);
4594 prr = prison_racct_find_locked(name);
4595 sx_xunlock(&allprison_lock);
4600 prison_racct_hold(struct prison_racct *prr)
4603 ASSERT_RACCT_ENABLED();
4605 refcount_acquire(&prr->prr_refcount);
4609 prison_racct_free_locked(struct prison_racct *prr)
4612 ASSERT_RACCT_ENABLED();
4613 sx_assert(&allprison_lock, SA_XLOCKED);
4615 if (refcount_release(&prr->prr_refcount)) {
4616 racct_destroy(&prr->prr_racct);
4617 LIST_REMOVE(prr, prr_next);
4618 free(prr, M_PRISON_RACCT);
4623 prison_racct_free(struct prison_racct *prr)
4626 ASSERT_RACCT_ENABLED();
4627 sx_assert(&allprison_lock, SA_UNLOCKED);
4629 if (refcount_release_if_not_last(&prr->prr_refcount))
4632 sx_xlock(&allprison_lock);
4633 prison_racct_free_locked(prr);
4634 sx_xunlock(&allprison_lock);
4638 prison_racct_attach(struct prison *pr)
4640 struct prison_racct *prr;
4642 ASSERT_RACCT_ENABLED();
4643 sx_assert(&allprison_lock, SA_XLOCKED);
4645 prr = prison_racct_find_locked(pr->pr_name);
4646 KASSERT(prr != NULL, ("cannot find prison_racct"));
4648 pr->pr_prison_racct = prr;
4652 * Handle jail renaming. From the racct point of view, renaming means
4653 * moving from one prison_racct to another.
4656 prison_racct_modify(struct prison *pr)
4662 struct prison_racct *oldprr;
4664 ASSERT_RACCT_ENABLED();
4666 sx_slock(&allproc_lock);
4667 sx_xlock(&allprison_lock);
4669 if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) {
4670 sx_xunlock(&allprison_lock);
4671 sx_sunlock(&allproc_lock);
4675 oldprr = pr->pr_prison_racct;
4676 pr->pr_prison_racct = NULL;
4678 prison_racct_attach(pr);
4681 * Move resource utilisation records.
4683 racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct);
4687 * Force rctl to reattach rules to processes.
4689 FOREACH_PROC_IN_SYSTEM(p) {
4691 cred = crhold(p->p_ucred);
4693 rctl_proc_ucred_changed(p, cred);
4698 sx_sunlock(&allproc_lock);
4699 prison_racct_free_locked(oldprr);
4700 sx_xunlock(&allprison_lock);
4704 prison_racct_detach(struct prison *pr)
4707 ASSERT_RACCT_ENABLED();
4708 sx_assert(&allprison_lock, SA_UNLOCKED);
4710 if (pr->pr_prison_racct == NULL)
4712 prison_racct_free(pr->pr_prison_racct);
4713 pr->pr_prison_racct = NULL;
4720 db_show_prison(struct prison *pr)
4722 struct bool_flags *bf;
4723 struct jailsys_flags *jsf;
4724 #if defined(INET) || defined(INET6)
4729 char ip4buf[INET_ADDRSTRLEN];
4732 char ip6buf[INET6_ADDRSTRLEN];
4735 db_printf("prison %p:\n", pr);
4736 db_printf(" jid = %d\n", pr->pr_id);
4737 db_printf(" name = %s\n", pr->pr_name);
4738 db_printf(" parent = %p\n", pr->pr_parent);
4739 db_printf(" ref = %d\n", pr->pr_ref);
4740 db_printf(" uref = %d\n", pr->pr_uref);
4741 db_printf(" state = %s\n",
4742 pr->pr_state == PRISON_STATE_ALIVE ? "alive" :
4743 pr->pr_state == PRISON_STATE_DYING ? "dying" :
4745 db_printf(" path = %s\n", pr->pr_path);
4746 db_printf(" cpuset = %d\n", pr->pr_cpuset
4747 ? pr->pr_cpuset->cs_id : -1);
4749 db_printf(" vnet = %p\n", pr->pr_vnet);
4751 db_printf(" root = %p\n", pr->pr_root);
4752 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4753 db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum);
4754 db_printf(" children.max = %d\n", pr->pr_childmax);
4755 db_printf(" children.cur = %d\n", pr->pr_childcount);
4756 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4757 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4758 db_printf(" flags = 0x%x", pr->pr_flags);
4759 for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++)
4760 if (pr->pr_flags & bf->flag)
4761 db_printf(" %s", bf->name);
4762 for (jsf = pr_flag_jailsys;
4763 jsf < pr_flag_jailsys + nitems(pr_flag_jailsys);
4765 f = pr->pr_flags & (jsf->disable | jsf->new);
4766 db_printf(" %-16s= %s\n", jsf->name,
4767 (f != 0 && f == jsf->disable) ? "disable"
4768 : (f == jsf->new) ? "new"
4771 db_printf(" allow = 0x%x", pr->pr_allow);
4772 for (bf = pr_flag_allow;
4773 bf < pr_flag_allow + nitems(pr_flag_allow) &&
4774 atomic_load_int(&bf->flag) != 0;
4776 if (pr->pr_allow & bf->flag)
4777 db_printf(" %s", bf->name);
4779 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4780 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4781 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4782 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4783 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4785 if (pr->pr_addrs[PR_INET] != NULL) {
4786 pr_family_t af = PR_INET;
4788 db_printf(" ip4s = %d\n", pr->pr_addrs[af]->ips);
4789 for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4790 db_printf(" %s %s\n",
4791 ii == 0 ? "ip4.addr =" : " ",
4793 *(const struct in_addr *)PR_IP(pr, ii),
4798 if (pr->pr_addrs[PR_INET6] != NULL) {
4799 pr_family_t af = PR_INET6;
4801 db_printf(" ip6s = %d\n", pr->pr_addrs[af]->ips);
4802 for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++)
4803 db_printf(" %s %s\n",
4804 ii == 0 ? "ip6.addr =" : " ",
4806 (const struct in6_addr *)PR_IP(pr, ii)));
4811 DB_SHOW_COMMAND(prison, db_show_prison_command)
4817 * Show all prisons in the list, and prison0 which is not
4820 db_show_prison(&prison0);
4821 if (!db_pager_quit) {
4822 TAILQ_FOREACH(pr, &allprison, pr_list) {
4834 /* Look for a prison with the ID and with references. */
4835 TAILQ_FOREACH(pr, &allprison, pr_list)
4836 if (pr->pr_id == addr && pr->pr_ref > 0)
4839 /* Look again, without requiring a reference. */
4840 TAILQ_FOREACH(pr, &allprison, pr_list)
4841 if (pr->pr_id == addr)
4844 /* Assume address points to a valid prison. */
4845 pr = (struct prison *)addr;