2 * Copyright (c) 1999 Poul-Henning Kamp.
3 * Copyright (c) 2008 Bjoern A. Zeeb.
4 * Copyright (c) 2009 James Gritton.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_compat.h"
35 #include "opt_inet6.h"
37 #include <sys/param.h>
38 #include <sys/types.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/errno.h>
42 #include <sys/sysproto.h>
43 #include <sys/malloc.h>
47 #include <sys/taskqueue.h>
48 #include <sys/fcntl.h>
51 #include <sys/mutex.h>
53 #include <sys/sysent.h>
54 #include <sys/namei.h>
55 #include <sys/mount.h>
56 #include <sys/queue.h>
57 #include <sys/socket.h>
58 #include <sys/syscallsubr.h>
59 #include <sys/sysctl.h>
60 #include <sys/vnode.h>
65 #include <netinet/in.h>
70 #include <netinet6/in6_var.h>
74 #include <security/mac/mac_framework.h>
76 #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000"
78 MALLOC_DEFINE(M_PRISON, "prison", "Prison structures");
80 /* prison0 describes what is "real" about the system. */
81 struct prison prison0 = {
88 .pr_childmax = JAIL_MAX,
89 .pr_hostuuid = DEFAULT_HOSTUUID,
90 .pr_children = LIST_HEAD_INITIALIZER(&prison0.pr_children),
92 .pr_allow = PR_ALLOW_ALL,
94 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
96 /* allprison and lastprid are protected by allprison_lock. */
97 struct sx allprison_lock;
98 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
99 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
102 static int do_jail_attach(struct thread *td, struct prison *pr);
103 static void prison_complete(void *context, int pending);
104 static void prison_deref(struct prison *pr, int flags);
105 static char *prison_path(struct prison *pr1, struct prison *pr2);
106 static void prison_remove_one(struct prison *pr);
108 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
109 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
112 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
113 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
116 /* Flags for prison_deref */
117 #define PD_DEREF 0x01
118 #define PD_DEUREF 0x02
119 #define PD_LOCKED 0x04
120 #define PD_LIST_SLOCKED 0x08
121 #define PD_LIST_XLOCKED 0x10
124 * Parameter names corresponding to PR_* flag values
126 static char *pr_flag_names[] = {
130 static char *pr_flag_nonames[] = {
134 struct jailsys_flags {
138 } pr_flag_jailsys[] = {
139 { "host", 0, PR_HOST },
141 { "vnet", 0, PR_VNET },
144 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
147 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
151 static char *pr_allow_names[] = {
152 "allow.set_hostname",
161 static char *pr_allow_nonames[] = {
162 "allow.noset_hostname",
164 "allow.noraw_sockets",
171 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
172 #define JAIL_DEFAULT_ENFORCE_STATFS 2
173 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
174 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
175 #if defined(INET) || defined(INET6)
176 static unsigned jail_max_af_ips = 255;
181 qcmp_v4(const void *ip1, const void *ip2)
186 * We need to compare in HBO here to get the list sorted as expected
187 * by the result of the code. Sorting NBO addresses gives you
188 * interesting results. If you do not understand, do not try.
190 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
191 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
194 * Do not simply return the difference of the two numbers, the int is
208 qcmp_v6(const void *ip1, const void *ip2)
210 const struct in6_addr *ia6a, *ia6b;
213 ia6a = (const struct in6_addr *)ip1;
214 ia6b = (const struct in6_addr *)ip2;
217 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
218 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
220 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
233 jail(struct thread *td, struct jail_args *uap)
239 error = copyin(uap->jail, &version, sizeof(uint32_t));
248 /* FreeBSD single IPv4 jails. */
249 bzero(&j, sizeof(struct jail));
250 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
253 j.version = j0.version;
255 j.hostname = j0.hostname;
256 j.ip4s = j0.ip_number;
262 * Version 1 was used by multi-IPv4 jail implementations
263 * that never made it into the official kernel.
267 case 2: /* JAIL_API_VERSION */
268 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
269 error = copyin(uap->jail, &j, sizeof(struct jail));
275 /* Sci-Fi jails are not supported, sorry. */
278 return (kern_jail(td, &j));
282 kern_jail(struct thread *td, struct jail *j)
284 struct iovec optiov[2 * (4
285 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
294 char *u_path, *u_hostname, *u_name;
297 struct in_addr *u_ip4;
300 struct in6_addr *u_ip6;
303 int error, enforce_statfs, fi;
305 bzero(&optiov, sizeof(optiov));
306 opt.uio_iov = optiov;
310 opt.uio_segflg = UIO_SYSSPACE;
311 opt.uio_rw = UIO_READ;
314 /* Set permissions for top-level jails from sysctls. */
315 if (!jailed(td->td_ucred)) {
316 for (fi = 0; fi < sizeof(pr_allow_names) /
317 sizeof(pr_allow_names[0]); fi++) {
318 optiov[opt.uio_iovcnt].iov_base =
319 (jail_default_allow & (1 << fi))
320 ? pr_allow_names[fi] : pr_allow_nonames[fi];
321 optiov[opt.uio_iovcnt].iov_len =
322 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
325 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
326 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
328 enforce_statfs = jail_default_enforce_statfs;
329 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
330 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
334 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
336 ip4s = (j->version == 0) ? 1 : j->ip4s;
337 if (ip4s > jail_max_af_ips)
339 tmplen += ip4s * sizeof(struct in_addr);
345 if (j->ip6s > jail_max_af_ips)
347 tmplen += j->ip6s * sizeof(struct in6_addr);
352 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
353 u_hostname = u_path + MAXPATHLEN;
354 u_name = u_hostname + MAXHOSTNAMELEN;
356 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
360 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
362 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
365 optiov[opt.uio_iovcnt].iov_base = "path";
366 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
368 optiov[opt.uio_iovcnt].iov_base = u_path;
369 error = copyinstr(j->path, u_path, MAXPATHLEN,
370 &optiov[opt.uio_iovcnt].iov_len);
372 free(u_path, M_TEMP);
376 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
377 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
379 optiov[opt.uio_iovcnt].iov_base = u_hostname;
380 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
381 &optiov[opt.uio_iovcnt].iov_len);
383 free(u_path, M_TEMP);
387 if (j->jailname != NULL) {
388 optiov[opt.uio_iovcnt].iov_base = "name";
389 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
391 optiov[opt.uio_iovcnt].iov_base = u_name;
392 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
393 &optiov[opt.uio_iovcnt].iov_len);
395 free(u_path, M_TEMP);
401 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
402 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
404 optiov[opt.uio_iovcnt].iov_base = u_ip4;
405 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
407 u_ip4->s_addr = j->ip4s;
409 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
411 free(u_path, M_TEMP);
418 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
419 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
421 optiov[opt.uio_iovcnt].iov_base = u_ip6;
422 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
423 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
425 free(u_path, M_TEMP);
430 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
431 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
432 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
433 free(u_path, M_TEMP);
439 * struct jail_set_args {
440 * struct iovec *iovp;
441 * unsigned int iovcnt;
446 jail_set(struct thread *td, struct jail_set_args *uap)
451 /* Check that we have an even number of iovecs. */
455 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
458 error = kern_jail_set(td, auio, uap->flags);
464 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
471 struct in6_addr *ip6;
474 struct vfsoptlist *opts;
475 struct prison *pr, *deadpr, *mypr, *ppr, *tpr, *tppr;
477 char *domain, *errmsg, *host, *name, *p, *path, *uuid;
478 #if defined(INET) || defined(INET6)
482 size_t namelen, onamelen;
483 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
484 int gotchildmax, gotenforce, gothid, gotslevel;
485 int fi, jid, jsys, len, level;
486 int childmax, slevel, vfslocked;
487 #if defined(INET) || defined(INET6)
496 unsigned pr_flags, ch_flags;
497 unsigned pr_allow, ch_allow, tallow;
500 error = priv_check(td, PRIV_JAIL_SET);
501 if (!error && (flags & JAIL_ATTACH))
502 error = priv_check(td, PRIV_JAIL_ATTACH);
505 mypr = ppr = td->td_ucred->cr_prison;
506 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
508 if (flags & ~JAIL_SET_MASK)
512 * Check all the parameters before committing to anything. Not all
513 * errors can be caught early, but we may as well try. Also, this
514 * takes care of some expensive stuff (path lookup) before getting
515 * the allprison lock.
517 * XXX Jails are not filesystems, and jail parameters are not mount
518 * options. But it makes more sense to re-use the vfsopt code
519 * than duplicate it under a different name.
521 error = vfs_buildopts(optuio, &opts);
531 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
537 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
546 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
554 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
555 gotenforce = (error == 0);
557 if (enforce < 0 || enforce > 2)
559 } else if (error != ENOENT)
562 pr_flags = ch_flags = 0;
563 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
565 if (pr_flag_names[fi] == NULL)
567 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
568 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
570 ch_flags |= pr_flags;
571 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
573 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
580 case JAIL_SYS_DISABLE:
581 if (!pr_flag_jailsys[fi].disable) {
585 pr_flags |= pr_flag_jailsys[fi].disable;
588 pr_flags |= pr_flag_jailsys[fi].new;
590 case JAIL_SYS_INHERIT:
597 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
599 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
600 && !(pr_flags & PR_PERSIST)) {
602 vfs_opterror(opts, "new jail must persist or attach");
606 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
608 vfs_opterror(opts, "vnet cannot be changed after creation");
613 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
615 vfs_opterror(opts, "ip4 cannot be changed after creation");
620 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
622 vfs_opterror(opts, "ip6 cannot be changed after creation");
627 pr_allow = ch_allow = 0;
628 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
630 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
631 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
633 ch_allow |= pr_allow;
635 error = vfs_getopt(opts, "name", (void **)&name, &len);
641 if (len == 0 || name[len - 1] != '\0') {
645 if (len > MAXHOSTNAMELEN) {
646 error = ENAMETOOLONG;
651 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
659 if (len == 0 || host[len - 1] != '\0') {
663 if (len > MAXHOSTNAMELEN) {
664 error = ENAMETOOLONG;
669 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
677 if (len == 0 || domain[len - 1] != '\0') {
681 if (len > MAXHOSTNAMELEN) {
682 error = ENAMETOOLONG;
687 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
695 if (len == 0 || uuid[len - 1] != '\0') {
699 if (len > HOSTUUIDLEN) {
700 error = ENAMETOOLONG;
706 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
709 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
713 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
725 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
727 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
730 else if (ip4s & (sizeof(*ip4) - 1)) {
734 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
736 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
738 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
739 ip4s /= sizeof(*ip4);
740 if (ip4s > jail_max_af_ips) {
742 vfs_opterror(opts, "too many IPv4 addresses");
745 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
746 bcopy(op, ip4, ip4s * sizeof(*ip4));
748 * IP addresses are all sorted but ip[0] to preserve
749 * the primary IP address as given from userland.
750 * This special IP is used for unbound outgoing
751 * connections as well for "loopback" traffic.
754 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
756 * Check for duplicate addresses and do some simple
757 * zero and broadcast checks. If users give other bogus
758 * addresses it is their problem.
760 * We do not have to care about byte order for these
761 * checks so we will do them in NBO.
763 for (ii = 0; ii < ip4s; ii++) {
764 if (ip4[ii].s_addr == INADDR_ANY ||
765 ip4[ii].s_addr == INADDR_BROADCAST) {
770 (ip4[0].s_addr == ip4[ii+1].s_addr ||
771 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
781 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
783 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
786 else if (ip6s & (sizeof(*ip6) - 1)) {
790 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
792 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
794 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
795 ip6s /= sizeof(*ip6);
796 if (ip6s > jail_max_af_ips) {
798 vfs_opterror(opts, "too many IPv6 addresses");
801 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
802 bcopy(op, ip6, ip6s * sizeof(*ip6));
804 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
805 for (ii = 0; ii < ip6s; ii++) {
806 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
811 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
812 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
822 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
823 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
826 "vnet jails cannot have IP address restrictions");
832 error = vfs_getopt(opts, "path", (void **)&path, &len);
838 if (flags & JAIL_UPDATE) {
841 "path cannot be changed after creation");
844 if (len == 0 || path[len - 1] != '\0') {
848 if (len < 2 || (len == 2 && path[0] == '/'))
851 /* Leave room for a real-root full pathname. */
852 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
853 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
854 error = ENAMETOOLONG;
857 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
862 vfslocked = NDHASGIANT(&nd);
864 NDFREE(&nd, NDF_ONLY_PNBUF);
865 if (root->v_type != VDIR) {
868 VFS_UNLOCK_GIANT(vfslocked);
871 VFS_UNLOCK_GIANT(vfslocked);
876 * Grab the allprison lock before letting modules check their
877 * parameters. Once we have it, do not let go so we'll have a
878 * consistent view of the OSD list.
880 sx_xlock(&allprison_lock);
881 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
883 goto done_unlock_list;
885 /* By now, all parameters should have been noted. */
886 TAILQ_FOREACH(opt, opts, link) {
887 if (!opt->seen && strcmp(opt->name, "errmsg")) {
889 vfs_opterror(opts, "unknown parameter: %s", opt->name);
890 goto done_unlock_list;
895 * See if we are creating a new record or updating an existing one.
896 * This abuses the file error codes ENOENT and EEXIST.
898 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
901 vfs_opterror(opts, "no valid operation (create or update)");
902 goto done_unlock_list;
907 * See if a requested jid already exists. There is an
908 * information leak here if the jid exists but is not within
909 * the caller's jail hierarchy. Jail creators will get EEXIST
910 * even though they cannot see the jail, and CREATE | UPDATE
911 * will return ENOENT which is not normally a valid error.
915 vfs_opterror(opts, "negative jid");
916 goto done_unlock_list;
918 pr = prison_find(jid);
921 /* Create: jid must not exist. */
922 if (cuflags == JAIL_CREATE) {
923 mtx_unlock(&pr->pr_mtx);
925 vfs_opterror(opts, "jail %d already exists",
927 goto done_unlock_list;
929 if (!prison_ischild(mypr, pr)) {
930 mtx_unlock(&pr->pr_mtx);
932 } else if (pr->pr_uref == 0) {
933 if (!(flags & JAIL_DYING)) {
934 mtx_unlock(&pr->pr_mtx);
936 vfs_opterror(opts, "jail %d is dying",
938 goto done_unlock_list;
939 } else if ((flags & JAIL_ATTACH) ||
940 (pr_flags & PR_PERSIST)) {
942 * A dying jail might be resurrected
943 * (via attach or persist), but first
944 * it must determine if another jail
945 * has claimed its name. Accomplish
946 * this by implicitly re-setting the
950 name = prison_name(mypr, pr);
955 /* Update: jid must exist. */
956 if (cuflags == JAIL_UPDATE) {
958 vfs_opterror(opts, "jail %d not found", jid);
959 goto done_unlock_list;
964 * If the caller provided a name, look for a jail by that name.
965 * This has different semantics for creates and updates keyed by jid
966 * (where the name must not already exist in a different jail),
967 * and updates keyed by the name itself (where the name must exist
968 * because that is the jail being updated).
971 p = strrchr(name, '.');
974 * This is a hierarchical name. Split it into the
975 * parent and child names, and make sure the parent
976 * exists or matches an already found jail.
980 if (strncmp(name, ppr->pr_name, p - name) ||
981 ppr->pr_name[p - name] != '\0') {
982 mtx_unlock(&pr->pr_mtx);
985 "cannot change jail's parent");
986 goto done_unlock_list;
989 ppr = prison_find_name(mypr, name);
993 "jail \"%s\" not found", name);
994 goto done_unlock_list;
996 mtx_unlock(&ppr->pr_mtx);
1000 if (name[0] != '\0') {
1002 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1005 FOREACH_PRISON_CHILD(ppr, tpr) {
1006 if (tpr != pr && tpr->pr_ref > 0 &&
1007 !strcmp(tpr->pr_name + namelen, name)) {
1009 cuflags != JAIL_CREATE) {
1010 mtx_lock(&tpr->pr_mtx);
1011 if (tpr->pr_ref > 0) {
1016 if (tpr->pr_uref > 0) {
1022 mtx_unlock(&tpr->pr_mtx);
1023 } else if (tpr->pr_uref > 0) {
1025 * Create, or update(jid):
1026 * name must not exist in an
1027 * active sibling jail.
1031 mtx_unlock(&pr->pr_mtx);
1033 "jail \"%s\" already exists",
1035 goto done_unlock_list;
1039 /* If no active jail is found, use a dying one. */
1040 if (deadpr != NULL && pr == NULL) {
1041 if (flags & JAIL_DYING) {
1042 mtx_lock(&deadpr->pr_mtx);
1043 if (deadpr->pr_ref == 0) {
1044 mtx_unlock(&deadpr->pr_mtx);
1048 } else if (cuflags == JAIL_UPDATE) {
1051 "jail \"%s\" is dying", name);
1052 goto done_unlock_list;
1055 /* Update: name must exist if no jid. */
1056 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1058 vfs_opterror(opts, "jail \"%s\" not found",
1060 goto done_unlock_list;
1064 /* Update: must provide a jid or name. */
1065 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1067 vfs_opterror(opts, "update specified no jail");
1068 goto done_unlock_list;
1071 /* If there's no prison to update, create a new one and link it in. */
1073 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1074 if (tpr->pr_childcount >= tpr->pr_childmax) {
1076 vfs_opterror(opts, "prison limit exceeded");
1077 goto done_unlock_list;
1080 mtx_lock(&ppr->pr_mtx);
1081 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1082 mtx_unlock(&ppr->pr_mtx);
1084 vfs_opterror(opts, "parent jail went away!");
1085 goto done_unlock_list;
1089 mtx_unlock(&ppr->pr_mtx);
1090 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1092 /* Find the next free jid. */
1095 if (jid == JAIL_MAX)
1097 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1098 if (tpr->pr_id < jid)
1100 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1101 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1104 if (jid == lastprid) {
1107 "no available jail IDs");
1109 prison_deref(ppr, PD_DEREF |
1110 PD_DEUREF | PD_LIST_XLOCKED);
1119 * The jail already has a jid (that did not yet exist),
1120 * so just find where to insert it.
1122 TAILQ_FOREACH(tpr, &allprison, pr_list)
1123 if (tpr->pr_id >= jid) {
1124 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1129 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1130 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1131 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1132 tpr->pr_childcount++;
1134 pr->pr_parent = ppr;
1137 /* Set some default values, and inherit some from the parent. */
1142 root = mypr->pr_root;
1145 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1146 pr->pr_flags |= PR_HOST;
1147 #if defined(INET) || defined(INET6)
1149 if (!(pr_flags & PR_VNET))
1153 if (!(ch_flags & PR_IP4_USER))
1155 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1156 else if (!(pr_flags & PR_IP4_USER)) {
1157 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1158 if (ppr->pr_ip4 != NULL) {
1159 pr->pr_ip4s = ppr->pr_ip4s;
1160 pr->pr_ip4 = malloc(pr->pr_ip4s *
1161 sizeof(struct in_addr), M_PRISON,
1163 bcopy(ppr->pr_ip4, pr->pr_ip4,
1164 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1169 if (!(ch_flags & PR_IP6_USER))
1171 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1172 else if (!(pr_flags & PR_IP6_USER)) {
1173 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1174 if (ppr->pr_ip6 != NULL) {
1175 pr->pr_ip6s = ppr->pr_ip6s;
1176 pr->pr_ip6 = malloc(pr->pr_ip6s *
1177 sizeof(struct in6_addr), M_PRISON,
1179 bcopy(ppr->pr_ip6, pr->pr_ip6,
1180 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1186 pr->pr_securelevel = ppr->pr_securelevel;
1187 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1188 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1190 LIST_INIT(&pr->pr_children);
1191 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1194 /* Allocate a new vnet if specified. */
1195 pr->pr_vnet = (pr_flags & PR_VNET)
1196 ? vnet_alloc() : ppr->pr_vnet;
1199 * Allocate a dedicated cpuset for each jail.
1200 * Unlike other initial settings, this may return an erorr.
1202 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1204 prison_deref(pr, PD_LIST_XLOCKED);
1208 mtx_lock(&pr->pr_mtx);
1210 * New prisons do not yet have a reference, because we do not
1211 * want other to see the incomplete prison once the
1212 * allprison_lock is downgraded.
1217 * Grab a reference for existing prisons, to ensure they
1218 * continue to exist for the duration of the call.
1221 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1222 if ((pr->pr_flags & PR_VNET) &&
1223 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1226 "vnet jails cannot have IP address restrictions");
1227 goto done_deref_locked;
1231 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1234 "ip4 cannot be changed after creation");
1235 goto done_deref_locked;
1239 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1242 "ip6 cannot be changed after creation");
1243 goto done_deref_locked;
1248 /* Do final error checking before setting anything. */
1250 if (slevel < ppr->pr_securelevel) {
1252 goto done_deref_locked;
1256 if (childmax >= ppr->pr_childmax) {
1258 goto done_deref_locked;
1262 if (enforce < ppr->pr_enforce_statfs) {
1264 goto done_deref_locked;
1269 if (ppr->pr_flags & PR_IP4) {
1271 * Make sure the new set of IP addresses is a
1272 * subset of the parent's list. Don't worry
1273 * about the parent being unlocked, as any
1274 * setting is done with allprison_lock held.
1276 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1277 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1279 if (ij == ppr->pr_ip4s) {
1281 goto done_deref_locked;
1284 for (ii = ij = 1; ii < ip4s; ii++) {
1285 if (ip4[ii].s_addr ==
1286 ppr->pr_ip4[0].s_addr)
1288 for (; ij < ppr->pr_ip4s; ij++)
1289 if (ip4[ii].s_addr ==
1290 ppr->pr_ip4[ij].s_addr)
1292 if (ij == ppr->pr_ip4s)
1295 if (ij == ppr->pr_ip4s) {
1297 goto done_deref_locked;
1302 * Check for conflicting IP addresses. We permit them
1303 * if there is no more than one IP on each jail. If
1304 * there is a duplicate on a jail with more than one
1305 * IP stop checking and return error.
1309 for (; tppr != &prison0; tppr = tppr->pr_parent)
1310 if (tppr->pr_flags & PR_VNET)
1313 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1316 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1318 tpr->pr_uref == 0) {
1322 if (!(tpr->pr_flags & PR_IP4_USER))
1325 if (tpr->pr_ip4 == NULL ||
1326 (ip4s == 1 && tpr->pr_ip4s == 1))
1328 for (ii = 0; ii < ip4s; ii++) {
1329 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1332 "IPv4 addresses clash");
1333 goto done_deref_locked;
1341 if (ppr->pr_flags & PR_IP6) {
1343 * Make sure the new set of IP addresses is a
1344 * subset of the parent's list.
1346 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1347 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1350 if (ij == ppr->pr_ip6s) {
1352 goto done_deref_locked;
1355 for (ii = ij = 1; ii < ip6s; ii++) {
1356 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1359 for (; ij < ppr->pr_ip6s; ij++)
1360 if (IN6_ARE_ADDR_EQUAL(
1361 &ip6[ii], &ppr->pr_ip6[ij]))
1363 if (ij == ppr->pr_ip6s)
1366 if (ij == ppr->pr_ip6s) {
1368 goto done_deref_locked;
1372 /* Check for conflicting IP addresses. */
1375 for (; tppr != &prison0; tppr = tppr->pr_parent)
1376 if (tppr->pr_flags & PR_VNET)
1379 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1382 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1384 tpr->pr_uref == 0) {
1388 if (!(tpr->pr_flags & PR_IP6_USER))
1391 if (tpr->pr_ip6 == NULL ||
1392 (ip6s == 1 && tpr->pr_ip6s == 1))
1394 for (ii = 0; ii < ip6s; ii++) {
1395 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1398 "IPv6 addresses clash");
1399 goto done_deref_locked;
1405 onamelen = namelen = 0;
1407 /* Give a default name of the jid. */
1408 if (name[0] == '\0')
1409 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1410 else if (strtoul(name, &p, 10) != jid && *p == '\0') {
1412 vfs_opterror(opts, "name cannot be numeric");
1413 goto done_deref_locked;
1416 * Make sure the name isn't too long for the prison or its
1419 onamelen = strlen(pr->pr_name);
1420 namelen = strlen(name);
1421 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1422 error = ENAMETOOLONG;
1423 goto done_deref_locked;
1425 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1426 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1427 sizeof(pr->pr_name)) {
1428 error = ENAMETOOLONG;
1429 goto done_deref_locked;
1433 if (pr_allow & ~ppr->pr_allow) {
1435 goto done_deref_locked;
1438 /* Set the parameters of the prison. */
1441 if (pr_flags & PR_IP4_USER) {
1442 pr->pr_flags |= PR_IP4;
1443 free(pr->pr_ip4, M_PRISON);
1447 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1449 if (tpr->pr_flags & PR_VNET) {
1454 if (prison_restrict_ip4(tpr, NULL)) {
1463 if (pr_flags & PR_IP6_USER) {
1464 pr->pr_flags |= PR_IP6;
1465 free(pr->pr_ip6, M_PRISON);
1469 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1471 if (tpr->pr_flags & PR_VNET) {
1476 if (prison_restrict_ip6(tpr, NULL)) {
1484 pr->pr_securelevel = slevel;
1485 /* Set all child jails to be at least this level. */
1486 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1487 if (tpr->pr_securelevel < slevel)
1488 tpr->pr_securelevel = slevel;
1491 pr->pr_childmax = childmax;
1492 /* Set all child jails to under this limit. */
1493 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1494 if (tpr->pr_childmax > childmax - level)
1495 tpr->pr_childmax = childmax > level
1496 ? childmax - level : 0;
1499 pr->pr_enforce_statfs = enforce;
1500 /* Pass this restriction on to the children. */
1501 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1502 if (tpr->pr_enforce_statfs < enforce)
1503 tpr->pr_enforce_statfs = enforce;
1506 if (ppr == &prison0)
1507 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1509 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1510 ppr->pr_name, name);
1511 /* Change this component of child names. */
1512 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1513 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1514 strlen(tpr->pr_name + onamelen) + 1);
1515 bcopy(pr->pr_name, tpr->pr_name, namelen);
1519 /* Try to keep a real-rooted full pathname. */
1520 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1521 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1522 mypr->pr_path, path);
1524 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1527 if (PR_HOST & ch_flags & ~pr_flags) {
1528 if (pr->pr_flags & PR_HOST) {
1530 * Copy the parent's host info. As with pr_ip4 above,
1531 * the lack of a lock on the parent is not a problem;
1532 * it is always set with allprison_lock at least
1533 * shared, and is held exclusively here.
1535 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1536 sizeof(pr->pr_hostname));
1537 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1538 sizeof(pr->pr_domainname));
1539 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1540 sizeof(pr->pr_hostuuid));
1541 pr->pr_hostid = pr->pr_parent->pr_hostid;
1543 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1544 /* Set this prison, and any descendants without PR_HOST. */
1546 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1548 strlcpy(pr->pr_domainname, domain,
1549 sizeof(pr->pr_domainname));
1551 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1553 pr->pr_hostid = hid;
1554 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1555 if (tpr->pr_flags & PR_HOST)
1559 strlcpy(tpr->pr_hostname,
1561 sizeof(tpr->pr_hostname));
1563 strlcpy(tpr->pr_domainname,
1565 sizeof(tpr->pr_domainname));
1567 strlcpy(tpr->pr_hostuuid,
1569 sizeof(tpr->pr_hostuuid));
1571 tpr->pr_hostid = hid;
1575 if ((tallow = ch_allow & ~pr_allow)) {
1576 /* Clear allow bits in all children. */
1577 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1578 tpr->pr_allow &= ~tallow;
1580 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1582 * Persistent prisons get an extra reference, and prisons losing their
1583 * persist flag lose that reference. Only do this for existing prisons
1584 * for now, so new ones will remain unseen until after the module
1585 * handlers have completed.
1587 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1588 if (pr_flags & PR_PERSIST) {
1596 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1597 mtx_unlock(&pr->pr_mtx);
1599 /* Locks may have prevented a complete restriction of child IP
1600 * addresses. If so, allocate some more memory and try again.
1605 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1606 mtx_lock(&pr->pr_mtx);
1608 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1610 if (tpr->pr_flags & PR_VNET) {
1615 if (prison_restrict_ip4(tpr, ip4)) {
1622 mtx_unlock(&pr->pr_mtx);
1628 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1629 mtx_lock(&pr->pr_mtx);
1631 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1633 if (tpr->pr_flags & PR_VNET) {
1638 if (prison_restrict_ip6(tpr, ip6)) {
1645 mtx_unlock(&pr->pr_mtx);
1649 /* Let the modules do their work. */
1650 sx_downgrade(&allprison_lock);
1652 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1654 prison_deref(pr, PD_LIST_SLOCKED);
1658 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1660 prison_deref(pr, created
1662 : PD_DEREF | PD_LIST_SLOCKED);
1666 /* Attach this process to the prison if requested. */
1667 if (flags & JAIL_ATTACH) {
1668 mtx_lock(&pr->pr_mtx);
1669 error = do_jail_attach(td, pr);
1671 vfs_opterror(opts, "attach failed");
1673 prison_deref(pr, PD_DEREF);
1679 * Now that it is all there, drop the temporary reference from existing
1680 * prisons. Or add a reference to newly created persistent prisons
1681 * (which was not done earlier so that the prison would not be publicly
1685 prison_deref(pr, (flags & JAIL_ATTACH)
1687 : PD_DEREF | PD_LIST_SLOCKED);
1689 if (pr_flags & PR_PERSIST) {
1690 mtx_lock(&pr->pr_mtx);
1693 mtx_unlock(&pr->pr_mtx);
1695 if (!(flags & JAIL_ATTACH))
1696 sx_sunlock(&allprison_lock);
1698 td->td_retval[0] = pr->pr_id;
1702 prison_deref(pr, created
1703 ? PD_LOCKED | PD_LIST_XLOCKED
1704 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1707 sx_xunlock(&allprison_lock);
1710 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1712 VFS_UNLOCK_GIANT(vfslocked);
1716 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1717 if (errmsg_len > 0) {
1718 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1719 if (errmsg_pos > 0) {
1720 if (optuio->uio_segflg == UIO_SYSSPACE)
1722 optuio->uio_iov[errmsg_pos].iov_base,
1726 optuio->uio_iov[errmsg_pos].iov_base,
1733 free(ip4, M_PRISON);
1736 free(ip6, M_PRISON);
1744 * struct jail_get_args {
1745 * struct iovec *iovp;
1746 * unsigned int iovcnt;
1751 jail_get(struct thread *td, struct jail_get_args *uap)
1756 /* Check that we have an even number of iovecs. */
1757 if (uap->iovcnt & 1)
1760 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1763 error = kern_jail_get(td, auio, uap->flags);
1765 error = copyout(auio->uio_iov, uap->iovp,
1766 uap->iovcnt * sizeof (struct iovec));
1772 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1774 struct prison *pr, *mypr;
1776 struct vfsoptlist *opts;
1777 char *errmsg, *name;
1778 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1780 if (flags & ~JAIL_GET_MASK)
1783 /* Get the parameter list. */
1784 error = vfs_buildopts(optuio, &opts);
1787 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1788 mypr = td->td_ucred->cr_prison;
1791 * Find the prison specified by one of: lastjid, jid, name.
1793 sx_slock(&allprison_lock);
1794 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1796 TAILQ_FOREACH(pr, &allprison, pr_list) {
1797 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1798 mtx_lock(&pr->pr_mtx);
1799 if (pr->pr_ref > 0 &&
1800 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1802 mtx_unlock(&pr->pr_mtx);
1808 vfs_opterror(opts, "no jail after %d", jid);
1809 goto done_unlock_list;
1810 } else if (error != ENOENT)
1811 goto done_unlock_list;
1813 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1816 pr = prison_find_child(mypr, jid);
1818 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1819 mtx_unlock(&pr->pr_mtx);
1821 vfs_opterror(opts, "jail %d is dying",
1823 goto done_unlock_list;
1828 vfs_opterror(opts, "jail %d not found", jid);
1829 goto done_unlock_list;
1831 } else if (error != ENOENT)
1832 goto done_unlock_list;
1834 error = vfs_getopt(opts, "name", (void **)&name, &len);
1836 if (len == 0 || name[len - 1] != '\0') {
1838 goto done_unlock_list;
1840 pr = prison_find_name(mypr, name);
1842 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1843 mtx_unlock(&pr->pr_mtx);
1845 vfs_opterror(opts, "jail \"%s\" is dying",
1847 goto done_unlock_list;
1852 vfs_opterror(opts, "jail \"%s\" not found", name);
1853 goto done_unlock_list;
1854 } else if (error != ENOENT)
1855 goto done_unlock_list;
1857 vfs_opterror(opts, "no jail specified");
1859 goto done_unlock_list;
1862 /* Get the parameters of the prison. */
1865 td->td_retval[0] = pr->pr_id;
1866 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1867 if (error != 0 && error != ENOENT)
1869 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1870 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1871 if (error != 0 && error != ENOENT)
1873 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1874 if (error != 0 && error != ENOENT)
1876 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1877 sizeof(pr->pr_cpuset->cs_id));
1878 if (error != 0 && error != ENOENT)
1880 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1881 if (error != 0 && error != ENOENT)
1884 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1885 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1886 if (error != 0 && error != ENOENT)
1890 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1891 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1892 if (error != 0 && error != ENOENT)
1895 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1896 sizeof(pr->pr_securelevel));
1897 if (error != 0 && error != ENOENT)
1899 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1900 sizeof(pr->pr_childcount));
1901 if (error != 0 && error != ENOENT)
1903 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1904 sizeof(pr->pr_childmax));
1905 if (error != 0 && error != ENOENT)
1907 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1908 if (error != 0 && error != ENOENT)
1910 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1911 if (error != 0 && error != ENOENT)
1913 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1914 if (error != 0 && error != ENOENT)
1917 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1918 uint32_t hid32 = pr->pr_hostid;
1920 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1923 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1924 sizeof(pr->pr_hostid));
1925 if (error != 0 && error != ENOENT)
1927 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1928 sizeof(pr->pr_enforce_statfs));
1929 if (error != 0 && error != ENOENT)
1931 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1933 if (pr_flag_names[fi] == NULL)
1935 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1936 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1937 if (error != 0 && error != ENOENT)
1940 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1941 if (error != 0 && error != ENOENT)
1944 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1947 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1948 i = pr_flag_jailsys[fi].disable &&
1949 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1950 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
1953 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
1954 if (error != 0 && error != ENOENT)
1957 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
1959 if (pr_allow_names[fi] == NULL)
1961 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
1962 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
1963 if (error != 0 && error != ENOENT)
1966 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
1967 if (error != 0 && error != ENOENT)
1970 i = (pr->pr_uref == 0);
1971 error = vfs_setopt(opts, "dying", &i, sizeof(i));
1972 if (error != 0 && error != ENOENT)
1975 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1976 if (error != 0 && error != ENOENT)
1979 /* Get the module parameters. */
1980 mtx_unlock(&pr->pr_mtx);
1982 error = osd_jail_call(pr, PR_METHOD_GET, opts);
1985 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
1987 /* By now, all parameters should have been noted. */
1988 TAILQ_FOREACH(opt, opts, link) {
1989 if (!opt->seen && strcmp(opt->name, "errmsg")) {
1991 vfs_opterror(opts, "unknown parameter: %s", opt->name);
1996 /* Write the fetched parameters back to userspace. */
1998 TAILQ_FOREACH(opt, opts, link) {
1999 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2000 pos = 2 * opt->pos + 1;
2001 optuio->uio_iov[pos].iov_len = opt->len;
2002 if (opt->value != NULL) {
2003 if (optuio->uio_segflg == UIO_SYSSPACE) {
2005 optuio->uio_iov[pos].iov_base,
2008 error = copyout(opt->value,
2009 optuio->uio_iov[pos].iov_base,
2020 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2024 sx_sunlock(&allprison_lock);
2026 if (error && errmsg_pos >= 0) {
2027 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2028 errmsg_pos = 2 * errmsg_pos + 1;
2029 if (errmsg_len > 0) {
2030 if (optuio->uio_segflg == UIO_SYSSPACE)
2032 optuio->uio_iov[errmsg_pos].iov_base,
2036 optuio->uio_iov[errmsg_pos].iov_base,
2046 * struct jail_remove_args {
2051 jail_remove(struct thread *td, struct jail_remove_args *uap)
2053 struct prison *pr, *cpr, *lpr, *tpr;
2056 error = priv_check(td, PRIV_JAIL_REMOVE);
2060 sx_xlock(&allprison_lock);
2061 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2063 sx_xunlock(&allprison_lock);
2067 /* Remove all descendants of this prison, then remove this prison. */
2069 pr->pr_flags |= PR_REMOVE;
2070 if (!LIST_EMPTY(&pr->pr_children)) {
2071 mtx_unlock(&pr->pr_mtx);
2073 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2074 mtx_lock(&cpr->pr_mtx);
2075 if (cpr->pr_ref > 0) {
2078 cpr->pr_flags |= PR_REMOVE;
2080 /* Already removed - do not do it again. */
2083 mtx_unlock(&cpr->pr_mtx);
2085 mtx_lock(&lpr->pr_mtx);
2086 prison_remove_one(lpr);
2087 sx_xlock(&allprison_lock);
2092 mtx_lock(&lpr->pr_mtx);
2093 prison_remove_one(lpr);
2094 sx_xlock(&allprison_lock);
2096 mtx_lock(&pr->pr_mtx);
2098 prison_remove_one(pr);
2103 prison_remove_one(struct prison *pr)
2108 /* If the prison was persistent, it is not anymore. */
2110 if (pr->pr_flags & PR_PERSIST) {
2113 pr->pr_flags &= ~PR_PERSIST;
2117 * jail_remove added a reference. If that's the only one, remove
2120 KASSERT(pr->pr_ref > 0,
2121 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2122 if (pr->pr_ref == 1) {
2124 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2128 mtx_unlock(&pr->pr_mtx);
2129 sx_xunlock(&allprison_lock);
2131 * Kill all processes unfortunate enough to be attached to this prison.
2133 sx_slock(&allproc_lock);
2134 LIST_FOREACH(p, &allproc, p_list) {
2136 if (p->p_state != PRS_NEW && p->p_ucred &&
2137 p->p_ucred->cr_prison == pr)
2138 psignal(p, SIGKILL);
2141 sx_sunlock(&allproc_lock);
2142 /* Remove the temporary reference added by jail_remove. */
2143 prison_deref(pr, deuref | PD_DEREF);
2148 * struct jail_attach_args {
2153 jail_attach(struct thread *td, struct jail_attach_args *uap)
2158 error = priv_check(td, PRIV_JAIL_ATTACH);
2162 sx_slock(&allprison_lock);
2163 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2165 sx_sunlock(&allprison_lock);
2170 * Do not allow a process to attach to a prison that is not
2171 * considered to be "alive".
2173 if (pr->pr_uref == 0) {
2174 mtx_unlock(&pr->pr_mtx);
2175 sx_sunlock(&allprison_lock);
2179 return (do_jail_attach(td, pr));
2183 do_jail_attach(struct thread *td, struct prison *pr)
2187 struct ucred *newcred, *oldcred;
2188 int vfslocked, error;
2191 * XXX: Note that there is a slight race here if two threads
2192 * in the same privileged process attempt to attach to two
2193 * different jails at the same time. It is important for
2194 * user processes not to do this, or they might end up with
2195 * a process root from one prison, but attached to the jail
2200 mtx_unlock(&pr->pr_mtx);
2202 /* Let modules do whatever they need to prepare for attaching. */
2203 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2205 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2208 sx_sunlock(&allprison_lock);
2211 * Reparent the newly attached process to this jail.
2213 ppr = td->td_ucred->cr_prison;
2215 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2219 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2220 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2221 if ((error = change_dir(pr->pr_root, td)) != 0)
2224 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2227 VOP_UNLOCK(pr->pr_root, 0);
2228 if ((error = change_root(pr->pr_root, td)))
2229 goto e_unlock_giant;
2230 VFS_UNLOCK_GIANT(vfslocked);
2234 oldcred = p->p_ucred;
2236 crcopy(newcred, oldcred);
2237 newcred->cr_prison = pr;
2238 p->p_ucred = newcred;
2241 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2244 VOP_UNLOCK(pr->pr_root, 0);
2246 VFS_UNLOCK_GIANT(vfslocked);
2248 /* Tell modules this thread is still in its old jail after all. */
2249 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2250 prison_deref(pr, PD_DEREF | PD_DEUREF);
2256 * Returns a locked prison instance, or NULL on failure.
2259 prison_find(int prid)
2263 sx_assert(&allprison_lock, SX_LOCKED);
2264 TAILQ_FOREACH(pr, &allprison, pr_list) {
2265 if (pr->pr_id == prid) {
2266 mtx_lock(&pr->pr_mtx);
2269 mtx_unlock(&pr->pr_mtx);
2276 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2279 prison_find_child(struct prison *mypr, int prid)
2284 sx_assert(&allprison_lock, SX_LOCKED);
2285 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2286 if (pr->pr_id == prid) {
2287 mtx_lock(&pr->pr_mtx);
2290 mtx_unlock(&pr->pr_mtx);
2297 * Look for the name relative to mypr. Returns a locked prison or NULL.
2300 prison_find_name(struct prison *mypr, const char *name)
2302 struct prison *pr, *deadpr;
2306 sx_assert(&allprison_lock, SX_LOCKED);
2307 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2310 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2311 if (!strcmp(pr->pr_name + mylen, name)) {
2312 mtx_lock(&pr->pr_mtx);
2313 if (pr->pr_ref > 0) {
2314 if (pr->pr_uref > 0)
2318 mtx_unlock(&pr->pr_mtx);
2321 /* There was no valid prison - perhaps there was a dying one. */
2322 if (deadpr != NULL) {
2323 mtx_lock(&deadpr->pr_mtx);
2324 if (deadpr->pr_ref == 0) {
2325 mtx_unlock(&deadpr->pr_mtx);
2333 * See if a prison has the specific flag set.
2336 prison_flag(struct ucred *cred, unsigned flag)
2339 /* This is an atomic read, so no locking is necessary. */
2340 return (cred->cr_prison->pr_flags & flag);
2344 prison_allow(struct ucred *cred, unsigned flag)
2347 /* This is an atomic read, so no locking is necessary. */
2348 return (cred->cr_prison->pr_allow & flag);
2352 * Remove a prison reference. If that was the last reference, remove the
2353 * prison itself - but not in this context in case there are locks held.
2356 prison_free_locked(struct prison *pr)
2359 mtx_assert(&pr->pr_mtx, MA_OWNED);
2361 if (pr->pr_ref == 0) {
2362 mtx_unlock(&pr->pr_mtx);
2363 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2364 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2367 mtx_unlock(&pr->pr_mtx);
2371 prison_free(struct prison *pr)
2374 mtx_lock(&pr->pr_mtx);
2375 prison_free_locked(pr);
2379 prison_complete(void *context, int pending)
2382 prison_deref((struct prison *)context, 0);
2386 * Remove a prison reference (usually). This internal version assumes no
2387 * mutexes are held, except perhaps the prison itself. If there are no more
2388 * references, release and delist the prison. On completion, the prison lock
2389 * and the allprison lock are both unlocked.
2392 prison_deref(struct prison *pr, int flags)
2394 struct prison *ppr, *tpr;
2397 if (!(flags & PD_LOCKED))
2398 mtx_lock(&pr->pr_mtx);
2399 /* Decrement the user references in a separate loop. */
2400 if (flags & PD_DEUREF) {
2401 for (tpr = pr;; tpr = tpr->pr_parent) {
2403 mtx_lock(&tpr->pr_mtx);
2404 if (--tpr->pr_uref > 0)
2406 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2407 mtx_unlock(&tpr->pr_mtx);
2409 /* Done if there were only user references to remove. */
2410 if (!(flags & PD_DEREF)) {
2411 mtx_unlock(&tpr->pr_mtx);
2412 if (flags & PD_LIST_SLOCKED)
2413 sx_sunlock(&allprison_lock);
2414 else if (flags & PD_LIST_XLOCKED)
2415 sx_xunlock(&allprison_lock);
2419 mtx_unlock(&tpr->pr_mtx);
2420 mtx_lock(&pr->pr_mtx);
2425 if (flags & PD_DEREF)
2427 /* If the prison still has references, nothing else to do. */
2428 if (pr->pr_ref > 0) {
2429 mtx_unlock(&pr->pr_mtx);
2430 if (flags & PD_LIST_SLOCKED)
2431 sx_sunlock(&allprison_lock);
2432 else if (flags & PD_LIST_XLOCKED)
2433 sx_xunlock(&allprison_lock);
2437 mtx_unlock(&pr->pr_mtx);
2438 if (flags & PD_LIST_SLOCKED) {
2439 if (!sx_try_upgrade(&allprison_lock)) {
2440 sx_sunlock(&allprison_lock);
2441 sx_xlock(&allprison_lock);
2443 } else if (!(flags & PD_LIST_XLOCKED))
2444 sx_xlock(&allprison_lock);
2446 TAILQ_REMOVE(&allprison, pr, pr_list);
2447 LIST_REMOVE(pr, pr_sibling);
2448 ppr = pr->pr_parent;
2449 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2450 tpr->pr_childcount--;
2451 sx_downgrade(&allprison_lock);
2454 if (pr->pr_flags & PR_VNET)
2455 vnet_destroy(pr->pr_vnet);
2457 if (pr->pr_root != NULL) {
2458 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2460 VFS_UNLOCK_GIANT(vfslocked);
2462 mtx_destroy(&pr->pr_mtx);
2464 free(pr->pr_ip4, M_PRISON);
2467 free(pr->pr_ip6, M_PRISON);
2469 if (pr->pr_cpuset != NULL)
2470 cpuset_rel(pr->pr_cpuset);
2474 /* Removing a prison frees a reference on its parent. */
2476 mtx_lock(&pr->pr_mtx);
2477 flags = PD_DEREF | PD_LIST_SLOCKED;
2482 prison_hold_locked(struct prison *pr)
2485 mtx_assert(&pr->pr_mtx, MA_OWNED);
2486 KASSERT(pr->pr_ref > 0,
2487 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2492 prison_hold(struct prison *pr)
2495 mtx_lock(&pr->pr_mtx);
2496 prison_hold_locked(pr);
2497 mtx_unlock(&pr->pr_mtx);
2501 prison_proc_hold(struct prison *pr)
2504 mtx_lock(&pr->pr_mtx);
2505 KASSERT(pr->pr_uref > 0,
2506 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2508 mtx_unlock(&pr->pr_mtx);
2512 prison_proc_free(struct prison *pr)
2515 mtx_lock(&pr->pr_mtx);
2516 KASSERT(pr->pr_uref > 0,
2517 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2518 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2524 * Restrict a prison's IP address list with its parent's, possibly replacing
2525 * it. Return true if the replacement buffer was used (or would have been).
2528 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2533 ppr = pr->pr_parent;
2534 if (!(pr->pr_flags & PR_IP4_USER)) {
2535 /* This has no user settings, so just copy the parent's list. */
2536 if (pr->pr_ip4s < ppr->pr_ip4s) {
2538 * There's no room for the parent's list. Use the
2539 * new list buffer, which is assumed to be big enough
2540 * (if it was passed). If there's no buffer, try to
2544 if (newip4 == NULL) {
2545 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2546 M_PRISON, M_NOWAIT);
2550 if (newip4 != NULL) {
2551 bcopy(ppr->pr_ip4, newip4,
2552 ppr->pr_ip4s * sizeof(*newip4));
2553 free(pr->pr_ip4, M_PRISON);
2554 pr->pr_ip4 = newip4;
2555 pr->pr_ip4s = ppr->pr_ip4s;
2559 pr->pr_ip4s = ppr->pr_ip4s;
2560 if (pr->pr_ip4s > 0)
2561 bcopy(ppr->pr_ip4, pr->pr_ip4,
2562 pr->pr_ip4s * sizeof(*newip4));
2563 else if (pr->pr_ip4 != NULL) {
2564 free(pr->pr_ip4, M_PRISON);
2567 } else if (pr->pr_ip4s > 0) {
2568 /* Remove addresses that aren't in the parent. */
2569 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2570 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2572 if (ij < ppr->pr_ip4s)
2575 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2576 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2579 for (ij = 1; ii < pr->pr_ip4s; ) {
2580 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2584 switch (ij >= ppr->pr_ip4s ? -1 :
2585 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2587 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2588 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2599 if (pr->pr_ip4s == 0) {
2600 pr->pr_flags |= PR_IP4_DISABLE;
2601 free(pr->pr_ip4, M_PRISON);
2609 * Pass back primary IPv4 address of this jail.
2611 * If not restricted return success but do not alter the address. Caller has
2612 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2614 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2615 * Address returned in NBO.
2618 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2622 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2623 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2625 pr = cred->cr_prison;
2626 if (!(pr->pr_flags & PR_IP4))
2628 mtx_lock(&pr->pr_mtx);
2629 if (!(pr->pr_flags & PR_IP4)) {
2630 mtx_unlock(&pr->pr_mtx);
2633 if (pr->pr_ip4 == NULL) {
2634 mtx_unlock(&pr->pr_mtx);
2635 return (EAFNOSUPPORT);
2638 ia->s_addr = pr->pr_ip4[0].s_addr;
2639 mtx_unlock(&pr->pr_mtx);
2644 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2647 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2654 * No need to lock since the PR_IP4_USER flag can't be altered for
2657 while (pr1 != &prison0 &&
2659 !(pr1->pr_flags & PR_VNET) &&
2661 !(pr1->pr_flags & PR_IP4_USER))
2662 pr1 = pr1->pr_parent;
2663 while (pr2 != &prison0 &&
2665 !(pr2->pr_flags & PR_VNET) &&
2667 !(pr2->pr_flags & PR_IP4_USER))
2668 pr2 = pr2->pr_parent;
2669 return (pr1 == pr2);
2673 * Make sure our (source) address is set to something meaningful to this
2676 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2677 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2678 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2681 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2687 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2688 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2690 pr = cred->cr_prison;
2691 if (!(pr->pr_flags & PR_IP4))
2693 mtx_lock(&pr->pr_mtx);
2694 if (!(pr->pr_flags & PR_IP4)) {
2695 mtx_unlock(&pr->pr_mtx);
2698 if (pr->pr_ip4 == NULL) {
2699 mtx_unlock(&pr->pr_mtx);
2700 return (EAFNOSUPPORT);
2703 ia0.s_addr = ntohl(ia->s_addr);
2704 if (ia0.s_addr == INADDR_LOOPBACK) {
2705 ia->s_addr = pr->pr_ip4[0].s_addr;
2706 mtx_unlock(&pr->pr_mtx);
2710 if (ia0.s_addr == INADDR_ANY) {
2712 * In case there is only 1 IPv4 address, bind directly.
2714 if (pr->pr_ip4s == 1)
2715 ia->s_addr = pr->pr_ip4[0].s_addr;
2716 mtx_unlock(&pr->pr_mtx);
2720 error = _prison_check_ip4(pr, ia);
2721 mtx_unlock(&pr->pr_mtx);
2726 * Rewrite destination address in case we will connect to loopback address.
2728 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2729 * Address passed in in NBO and returned in NBO.
2732 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2736 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2737 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2739 pr = cred->cr_prison;
2740 if (!(pr->pr_flags & PR_IP4))
2742 mtx_lock(&pr->pr_mtx);
2743 if (!(pr->pr_flags & PR_IP4)) {
2744 mtx_unlock(&pr->pr_mtx);
2747 if (pr->pr_ip4 == NULL) {
2748 mtx_unlock(&pr->pr_mtx);
2749 return (EAFNOSUPPORT);
2752 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2753 ia->s_addr = pr->pr_ip4[0].s_addr;
2754 mtx_unlock(&pr->pr_mtx);
2759 * Return success because nothing had to be changed.
2761 mtx_unlock(&pr->pr_mtx);
2766 * Check if given address belongs to the jail referenced by cred/prison.
2768 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2769 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2770 * doesn't allow IPv4. Address passed in in NBO.
2773 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2778 * Check the primary IP.
2780 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2784 * All the other IPs are sorted so we can do a binary search.
2787 z = pr->pr_ip4s - 2;
2790 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2799 return (EADDRNOTAVAIL);
2803 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2808 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2809 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2811 pr = cred->cr_prison;
2812 if (!(pr->pr_flags & PR_IP4))
2814 mtx_lock(&pr->pr_mtx);
2815 if (!(pr->pr_flags & PR_IP4)) {
2816 mtx_unlock(&pr->pr_mtx);
2819 if (pr->pr_ip4 == NULL) {
2820 mtx_unlock(&pr->pr_mtx);
2821 return (EAFNOSUPPORT);
2824 error = _prison_check_ip4(pr, ia);
2825 mtx_unlock(&pr->pr_mtx);
2832 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2837 ppr = pr->pr_parent;
2838 if (!(pr->pr_flags & PR_IP6_USER)) {
2839 /* This has no user settings, so just copy the parent's list. */
2840 if (pr->pr_ip6s < ppr->pr_ip6s) {
2842 * There's no room for the parent's list. Use the
2843 * new list buffer, which is assumed to be big enough
2844 * (if it was passed). If there's no buffer, try to
2848 if (newip6 == NULL) {
2849 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2850 M_PRISON, M_NOWAIT);
2854 if (newip6 != NULL) {
2855 bcopy(ppr->pr_ip6, newip6,
2856 ppr->pr_ip6s * sizeof(*newip6));
2857 free(pr->pr_ip6, M_PRISON);
2858 pr->pr_ip6 = newip6;
2859 pr->pr_ip6s = ppr->pr_ip6s;
2863 pr->pr_ip6s = ppr->pr_ip6s;
2864 if (pr->pr_ip6s > 0)
2865 bcopy(ppr->pr_ip6, pr->pr_ip6,
2866 pr->pr_ip6s * sizeof(*newip6));
2867 else if (pr->pr_ip6 != NULL) {
2868 free(pr->pr_ip6, M_PRISON);
2871 } else if (pr->pr_ip6s > 0) {
2872 /* Remove addresses that aren't in the parent. */
2873 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2874 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2877 if (ij < ppr->pr_ip6s)
2880 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2881 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2884 for (ij = 1; ii < pr->pr_ip6s; ) {
2885 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2890 switch (ij >= ppr->pr_ip4s ? -1 :
2891 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2893 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2894 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2905 if (pr->pr_ip6s == 0) {
2906 pr->pr_flags |= PR_IP6_DISABLE;
2907 free(pr->pr_ip6, M_PRISON);
2915 * Pass back primary IPv6 address for this jail.
2917 * If not restricted return success but do not alter the address. Caller has
2918 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2920 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2923 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2927 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2928 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2930 pr = cred->cr_prison;
2931 if (!(pr->pr_flags & PR_IP6))
2933 mtx_lock(&pr->pr_mtx);
2934 if (!(pr->pr_flags & PR_IP6)) {
2935 mtx_unlock(&pr->pr_mtx);
2938 if (pr->pr_ip6 == NULL) {
2939 mtx_unlock(&pr->pr_mtx);
2940 return (EAFNOSUPPORT);
2943 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2944 mtx_unlock(&pr->pr_mtx);
2949 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
2952 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
2958 while (pr1 != &prison0 &&
2960 !(pr1->pr_flags & PR_VNET) &&
2962 !(pr1->pr_flags & PR_IP6_USER))
2963 pr1 = pr1->pr_parent;
2964 while (pr2 != &prison0 &&
2966 !(pr2->pr_flags & PR_VNET) &&
2968 !(pr2->pr_flags & PR_IP6_USER))
2969 pr2 = pr2->pr_parent;
2970 return (pr1 == pr2);
2974 * Make sure our (source) address is set to something meaningful to this jail.
2976 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2977 * when needed while binding.
2979 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
2980 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2981 * doesn't allow IPv6.
2984 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
2989 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2990 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2992 pr = cred->cr_prison;
2993 if (!(pr->pr_flags & PR_IP6))
2995 mtx_lock(&pr->pr_mtx);
2996 if (!(pr->pr_flags & PR_IP6)) {
2997 mtx_unlock(&pr->pr_mtx);
3000 if (pr->pr_ip6 == NULL) {
3001 mtx_unlock(&pr->pr_mtx);
3002 return (EAFNOSUPPORT);
3005 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3006 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3007 mtx_unlock(&pr->pr_mtx);
3011 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3013 * In case there is only 1 IPv6 address, and v6only is true,
3014 * then bind directly.
3016 if (v6only != 0 && pr->pr_ip6s == 1)
3017 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3018 mtx_unlock(&pr->pr_mtx);
3022 error = _prison_check_ip6(pr, ia6);
3023 mtx_unlock(&pr->pr_mtx);
3028 * Rewrite destination address in case we will connect to loopback address.
3030 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3033 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3037 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3038 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3040 pr = cred->cr_prison;
3041 if (!(pr->pr_flags & PR_IP6))
3043 mtx_lock(&pr->pr_mtx);
3044 if (!(pr->pr_flags & PR_IP6)) {
3045 mtx_unlock(&pr->pr_mtx);
3048 if (pr->pr_ip6 == NULL) {
3049 mtx_unlock(&pr->pr_mtx);
3050 return (EAFNOSUPPORT);
3053 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3054 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3055 mtx_unlock(&pr->pr_mtx);
3060 * Return success because nothing had to be changed.
3062 mtx_unlock(&pr->pr_mtx);
3067 * Check if given address belongs to the jail referenced by cred/prison.
3069 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3070 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3071 * doesn't allow IPv6.
3074 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3079 * Check the primary IP.
3081 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3085 * All the other IPs are sorted so we can do a binary search.
3088 z = pr->pr_ip6s - 2;
3091 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3100 return (EADDRNOTAVAIL);
3104 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3109 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3110 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3112 pr = cred->cr_prison;
3113 if (!(pr->pr_flags & PR_IP6))
3115 mtx_lock(&pr->pr_mtx);
3116 if (!(pr->pr_flags & PR_IP6)) {
3117 mtx_unlock(&pr->pr_mtx);
3120 if (pr->pr_ip6 == NULL) {
3121 mtx_unlock(&pr->pr_mtx);
3122 return (EAFNOSUPPORT);
3125 error = _prison_check_ip6(pr, ia6);
3126 mtx_unlock(&pr->pr_mtx);
3132 * Check if a jail supports the given address family.
3134 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3138 prison_check_af(struct ucred *cred, int af)
3143 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3145 pr = cred->cr_prison;
3147 /* Prisons with their own network stack are not limited. */
3148 if (pr->pr_flags & PR_VNET)
3157 if (pr->pr_flags & PR_IP4)
3159 mtx_lock(&pr->pr_mtx);
3160 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3161 error = EAFNOSUPPORT;
3162 mtx_unlock(&pr->pr_mtx);
3168 if (pr->pr_flags & PR_IP6)
3170 mtx_lock(&pr->pr_mtx);
3171 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3172 error = EAFNOSUPPORT;
3173 mtx_unlock(&pr->pr_mtx);
3181 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3182 error = EAFNOSUPPORT;
3188 * Check if given address belongs to the jail referenced by cred (wrapper to
3189 * prison_check_ip[46]).
3191 * Returns 0 if jail doesn't restrict the address family or if address belongs
3192 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3193 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3196 prison_if(struct ucred *cred, struct sockaddr *sa)
3199 struct sockaddr_in *sai;
3202 struct sockaddr_in6 *sai6;
3206 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3207 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3210 switch (sa->sa_family)
3214 sai = (struct sockaddr_in *)sa;
3215 error = prison_check_ip4(cred, &sai->sin_addr);
3220 sai6 = (struct sockaddr_in6 *)sa;
3221 error = prison_check_ip6(cred, &sai6->sin6_addr);
3225 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3226 error = EAFNOSUPPORT;
3232 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3235 prison_check(struct ucred *cred1, struct ucred *cred2)
3238 return ((cred1->cr_prison == cred2->cr_prison ||
3239 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3243 * Return 1 if p2 is a child of p1, otherwise 0.
3246 prison_ischild(struct prison *pr1, struct prison *pr2)
3249 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3256 * Return 1 if the passed credential is in a jail, otherwise 0.
3259 jailed(struct ucred *cred)
3262 return (cred->cr_prison != &prison0);
3266 * Return the correct hostname (domainname, et al) for the passed credential.
3269 getcredhostname(struct ucred *cred, char *buf, size_t size)
3274 * A NULL credential can be used to shortcut to the physical
3275 * system's hostname.
3277 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3278 mtx_lock(&pr->pr_mtx);
3279 strlcpy(buf, pr->pr_hostname, size);
3280 mtx_unlock(&pr->pr_mtx);
3284 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3287 mtx_lock(&cred->cr_prison->pr_mtx);
3288 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3289 mtx_unlock(&cred->cr_prison->pr_mtx);
3293 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3296 mtx_lock(&cred->cr_prison->pr_mtx);
3297 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3298 mtx_unlock(&cred->cr_prison->pr_mtx);
3302 getcredhostid(struct ucred *cred, unsigned long *hostid)
3305 mtx_lock(&cred->cr_prison->pr_mtx);
3306 *hostid = cred->cr_prison->pr_hostid;
3307 mtx_unlock(&cred->cr_prison->pr_mtx);
3311 * Determine whether the subject represented by cred can "see"
3312 * status of a mount point.
3313 * Returns: 0 for permitted, ENOENT otherwise.
3314 * XXX: This function should be called cr_canseemount() and should be
3315 * placed in kern_prot.c.
3318 prison_canseemount(struct ucred *cred, struct mount *mp)
3324 pr = cred->cr_prison;
3325 if (pr->pr_enforce_statfs == 0)
3327 if (pr->pr_root->v_mount == mp)
3329 if (pr->pr_enforce_statfs == 2)
3332 * If jail's chroot directory is set to "/" we should be able to see
3333 * all mount-points from inside a jail.
3334 * This is ugly check, but this is the only situation when jail's
3335 * directory ends with '/'.
3337 if (strcmp(pr->pr_path, "/") == 0)
3339 len = strlen(pr->pr_path);
3341 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3344 * Be sure that we don't have situation where jail's root directory
3345 * is "/some/path" and mount point is "/some/pathpath".
3347 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3353 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3355 char jpath[MAXPATHLEN];
3359 pr = cred->cr_prison;
3360 if (pr->pr_enforce_statfs == 0)
3362 if (prison_canseemount(cred, mp) != 0) {
3363 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3364 strlcpy(sp->f_mntonname, "[restricted]",
3365 sizeof(sp->f_mntonname));
3368 if (pr->pr_root->v_mount == mp) {
3370 * Clear current buffer data, so we are sure nothing from
3371 * the valid path left there.
3373 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3374 *sp->f_mntonname = '/';
3378 * If jail's chroot directory is set to "/" we should be able to see
3379 * all mount-points from inside a jail.
3381 if (strcmp(pr->pr_path, "/") == 0)
3383 len = strlen(pr->pr_path);
3384 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3386 * Clear current buffer data, so we are sure nothing from
3387 * the valid path left there.
3389 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3390 if (*jpath == '\0') {
3391 /* Should never happen. */
3392 *sp->f_mntonname = '/';
3394 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3399 * Check with permission for a specific privilege is granted within jail. We
3400 * have a specific list of accepted privileges; the rest are denied.
3403 prison_priv_check(struct ucred *cred, int priv)
3411 * Privileges specific to prisons with a virtual network stack.
3412 * There might be a duplicate entry here in case the privilege
3413 * is only granted conditionally in the legacy jail case.
3418 * NFS-specific privileges.
3420 case PRIV_NFS_DAEMON:
3421 case PRIV_NFS_LOCKD:
3424 * Network stack privileges.
3426 case PRIV_NET_BRIDGE:
3429 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3430 case PRIV_NET_ROUTE:
3432 case PRIV_NET_SETIFMTU:
3433 case PRIV_NET_SETIFFLAGS:
3434 case PRIV_NET_SETIFCAP:
3435 case PRIV_NET_SETIFNAME :
3436 case PRIV_NET_SETIFMETRIC:
3437 case PRIV_NET_SETIFPHYS:
3438 case PRIV_NET_SETIFMAC:
3439 case PRIV_NET_ADDMULTI:
3440 case PRIV_NET_DELMULTI:
3441 case PRIV_NET_HWIOCTL:
3442 case PRIV_NET_SETLLADDR:
3443 case PRIV_NET_ADDIFGROUP:
3444 case PRIV_NET_DELIFGROUP:
3445 case PRIV_NET_IFCREATE:
3446 case PRIV_NET_IFDESTROY:
3447 case PRIV_NET_ADDIFADDR:
3448 case PRIV_NET_DELIFADDR:
3451 case PRIV_NET_SETIFVNET:
3454 * 802.11-related privileges.
3456 case PRIV_NET80211_GETKEY:
3458 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3463 * AppleTalk privileges.
3465 case PRIV_NETATALK_RESERVEDPORT:
3470 case PRIV_NETATM_CFG:
3471 case PRIV_NETATM_ADD:
3472 case PRIV_NETATM_DEL:
3473 case PRIV_NETATM_SET:
3476 * Bluetooth privileges.
3478 case PRIV_NETBLUETOOTH_RAW:
3482 * Netgraph and netgraph module privileges.
3484 case PRIV_NETGRAPH_CONTROL:
3486 case PRIV_NETGRAPH_TTY:
3490 * IPv4 and IPv6 privileges.
3492 case PRIV_NETINET_IPFW:
3493 case PRIV_NETINET_DIVERT:
3494 case PRIV_NETINET_PF:
3495 case PRIV_NETINET_DUMMYNET:
3496 case PRIV_NETINET_CARP:
3497 case PRIV_NETINET_MROUTE:
3498 case PRIV_NETINET_RAW:
3499 case PRIV_NETINET_ADDRCTRL6:
3500 case PRIV_NETINET_ND6:
3501 case PRIV_NETINET_SCOPE6:
3502 case PRIV_NETINET_ALIFETIME6:
3503 case PRIV_NETINET_IPSEC:
3504 case PRIV_NETINET_BINDANY:
3508 * IPX/SPX privileges.
3510 case PRIV_NETIPX_RESERVEDPORT:
3511 case PRIV_NETIPX_RAW:
3525 * No default: or deny here.
3526 * In case of no permit fall through to next switch().
3528 if (cred->cr_prison->pr_flags & PR_VNET)
3536 * Allow ktrace privileges for root in jail.
3542 * Allow jailed processes to configure audit identity and
3543 * submit audit records (login, etc). In the future we may
3544 * want to further refine the relationship between audit and
3547 case PRIV_AUDIT_GETAUDIT:
3548 case PRIV_AUDIT_SETAUDIT:
3549 case PRIV_AUDIT_SUBMIT:
3553 * Allow jailed processes to manipulate process UNIX
3554 * credentials in any way they see fit.
3556 case PRIV_CRED_SETUID:
3557 case PRIV_CRED_SETEUID:
3558 case PRIV_CRED_SETGID:
3559 case PRIV_CRED_SETEGID:
3560 case PRIV_CRED_SETGROUPS:
3561 case PRIV_CRED_SETREUID:
3562 case PRIV_CRED_SETREGID:
3563 case PRIV_CRED_SETRESUID:
3564 case PRIV_CRED_SETRESGID:
3567 * Jail implements visibility constraints already, so allow
3568 * jailed root to override uid/gid-based constraints.
3570 case PRIV_SEEOTHERGIDS:
3571 case PRIV_SEEOTHERUIDS:
3574 * Jail implements inter-process debugging limits already, so
3575 * allow jailed root various debugging privileges.
3577 case PRIV_DEBUG_DIFFCRED:
3578 case PRIV_DEBUG_SUGID:
3579 case PRIV_DEBUG_UNPRIV:
3582 * Allow jail to set various resource limits and login
3583 * properties, and for now, exceed process resource limits.
3585 case PRIV_PROC_LIMIT:
3586 case PRIV_PROC_SETLOGIN:
3587 case PRIV_PROC_SETRLIMIT:
3590 * System V and POSIX IPC privileges are granted in jail.
3593 case PRIV_IPC_WRITE:
3594 case PRIV_IPC_ADMIN:
3595 case PRIV_IPC_MSGSIZE:
3599 * Jail operations within a jail work on child jails.
3601 case PRIV_JAIL_ATTACH:
3603 case PRIV_JAIL_REMOVE:
3606 * Jail implements its own inter-process limits, so allow
3607 * root processes in jail to change scheduling on other
3608 * processes in the same jail. Likewise for signalling.
3610 case PRIV_SCHED_DIFFCRED:
3611 case PRIV_SCHED_CPUSET:
3612 case PRIV_SIGNAL_DIFFCRED:
3613 case PRIV_SIGNAL_SUGID:
3616 * Allow jailed processes to write to sysctls marked as jail
3619 case PRIV_SYSCTL_WRITEJAIL:
3622 * Allow root in jail to manage a variety of quota
3623 * properties. These should likely be conditional on a
3624 * configuration option.
3626 case PRIV_VFS_GETQUOTA:
3627 case PRIV_VFS_SETQUOTA:
3630 * Since Jail relies on chroot() to implement file system
3631 * protections, grant many VFS privileges to root in jail.
3632 * Be careful to exclude mount-related and NFS-related
3636 case PRIV_VFS_WRITE:
3637 case PRIV_VFS_ADMIN:
3639 case PRIV_VFS_LOOKUP:
3640 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3641 case PRIV_VFS_CHFLAGS_DEV:
3642 case PRIV_VFS_CHOWN:
3643 case PRIV_VFS_CHROOT:
3644 case PRIV_VFS_RETAINSUGID:
3645 case PRIV_VFS_FCHROOT:
3647 case PRIV_VFS_SETGID:
3649 case PRIV_VFS_STICKYFILE:
3653 * Depending on the global setting, allow privilege of
3654 * setting system flags.
3656 case PRIV_VFS_SYSFLAGS:
3657 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3663 * Depending on the global setting, allow privilege of
3664 * mounting/unmounting file systems.
3666 case PRIV_VFS_MOUNT:
3667 case PRIV_VFS_UNMOUNT:
3668 case PRIV_VFS_MOUNT_NONUSER:
3669 case PRIV_VFS_MOUNT_OWNER:
3670 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3676 * Allow jailed root to bind reserved ports and reuse in-use
3679 case PRIV_NETINET_RESERVEDPORT:
3680 case PRIV_NETINET_REUSEPORT:
3684 * Allow jailed root to set certian IPv4/6 (option) headers.
3686 case PRIV_NETINET_SETHDROPTS:
3690 * Conditionally allow creating raw sockets in jail.
3692 case PRIV_NETINET_RAW:
3693 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3699 * Since jail implements its own visibility limits on netstat
3700 * sysctls, allow getcred. This allows identd to work in
3703 case PRIV_NETINET_GETCRED:
3708 * In all remaining cases, deny the privilege request. This
3709 * includes almost all network privileges, many system
3710 * configuration privileges.
3717 * Return the part of pr2's name that is relative to pr1, or the whole name
3718 * if it does not directly follow.
3722 prison_name(struct prison *pr1, struct prison *pr2)
3726 /* Jails see themselves as "0" (if they see themselves at all). */
3729 name = pr2->pr_name;
3730 if (prison_ischild(pr1, pr2)) {
3732 * pr1 isn't locked (and allprison_lock may not be either)
3733 * so its length can't be counted on. But the number of dots
3734 * can be counted on - and counted.
3736 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3737 name = strchr(name, '.') + 1;
3743 * Return the part of pr2's path that is relative to pr1, or the whole path
3744 * if it does not directly follow.
3747 prison_path(struct prison *pr1, struct prison *pr2)
3749 char *path1, *path2;
3752 path1 = pr1->pr_path;
3753 path2 = pr2->pr_path;
3754 if (!strcmp(path1, "/"))
3756 len1 = strlen(path1);
3757 if (strncmp(path1, path2, len1))
3759 if (path2[len1] == '\0')
3761 if (path2[len1] == '/')
3762 return (path2 + len1);
3768 * Jail-related sysctls.
3770 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3774 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3777 struct prison *pr, *cpr;
3779 struct in_addr *ip4 = NULL;
3783 struct in_addr *ip6 = NULL;
3788 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3789 pr = req->td->td_ucred->cr_prison;
3791 sx_slock(&allprison_lock);
3792 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3793 #if defined(INET) || defined(INET6)
3796 mtx_lock(&cpr->pr_mtx);
3798 if (cpr->pr_ip4s > 0) {
3799 if (ip4s < cpr->pr_ip4s) {
3800 ip4s = cpr->pr_ip4s;
3801 mtx_unlock(&cpr->pr_mtx);
3802 ip4 = realloc(ip4, ip4s *
3803 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3806 bcopy(cpr->pr_ip4, ip4,
3807 cpr->pr_ip4s * sizeof(struct in_addr));
3811 if (cpr->pr_ip6s > 0) {
3812 if (ip6s < cpr->pr_ip6s) {
3813 ip6s = cpr->pr_ip6s;
3814 mtx_unlock(&cpr->pr_mtx);
3815 ip6 = realloc(ip6, ip6s *
3816 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3819 bcopy(cpr->pr_ip6, ip6,
3820 cpr->pr_ip6s * sizeof(struct in6_addr));
3823 if (cpr->pr_ref == 0) {
3824 mtx_unlock(&cpr->pr_mtx);
3827 bzero(xp, sizeof(*xp));
3828 xp->pr_version = XPRISON_VERSION;
3829 xp->pr_id = cpr->pr_id;
3830 xp->pr_state = cpr->pr_uref > 0
3831 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3832 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3833 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3834 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3836 xp->pr_ip4s = cpr->pr_ip4s;
3839 xp->pr_ip6s = cpr->pr_ip6s;
3841 mtx_unlock(&cpr->pr_mtx);
3842 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3846 if (xp->pr_ip4s > 0) {
3847 error = SYSCTL_OUT(req, ip4,
3848 xp->pr_ip4s * sizeof(struct in_addr));
3854 if (xp->pr_ip6s > 0) {
3855 error = SYSCTL_OUT(req, ip6,
3856 xp->pr_ip6s * sizeof(struct in6_addr));
3862 sx_sunlock(&allprison_lock);
3873 SYSCTL_OID(_security_jail, OID_AUTO, list,
3874 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3875 sysctl_jail_list, "S", "List of active jails");
3878 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3882 injail = jailed(req->td->td_ucred);
3883 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3888 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3889 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3890 sysctl_jail_jailed, "I", "Process in jail?");
3892 #if defined(INET) || defined(INET6)
3893 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3894 &jail_max_af_ips, 0,
3895 "Number of IP addresses a jail may have at most per address family");
3899 * Default parameters for jail(2) compatability. For historical reasons,
3900 * the sysctl names have varying similarity to the parameter names. Prisons
3901 * just see their own parameters, and can't change them.
3904 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3907 int allow, error, i;
3909 pr = req->td->td_ucred->cr_prison;
3910 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
3912 /* Get the current flag value, and convert it to a boolean. */
3913 i = (allow & arg2) ? 1 : 0;
3916 error = sysctl_handle_int(oidp, &i, 0, req);
3917 if (error || !req->newptr)
3923 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3926 mtx_lock(&prison0.pr_mtx);
3927 jail_default_allow = (jail_default_allow & ~arg2) | i;
3928 mtx_unlock(&prison0.pr_mtx);
3932 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3933 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3934 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3935 "Processes in jail can set their hostnames");
3936 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3937 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3938 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3939 "Processes in jail are limited to creating UNIX/IP/route sockets only");
3940 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3941 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3942 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3943 "Processes in jail can use System V IPC primitives");
3944 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3945 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3946 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3947 "Prison root can create raw sockets");
3948 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3949 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3950 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3951 "Processes in jail can alter system file flags");
3952 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3953 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3954 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3955 "Processes in jail can mount/unmount jail-friendly file systems");
3958 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3963 pr = req->td->td_ucred->cr_prison;
3964 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
3965 error = sysctl_handle_int(oidp, &level, 0, req);
3966 if (error || !req->newptr)
3968 *(int *)arg1 = level;
3972 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
3973 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3974 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
3975 sysctl_jail_default_level, "I",
3976 "Processes in jail cannot see all mounted file systems");
3979 * Nodes to describe jail parameters. Maximum length of string parameters
3980 * is returned in the string itself, and the other parameters exist merely
3981 * to make themselves and their types known.
3983 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
3987 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
3994 switch (oidp->oid_kind & CTLTYPE)
4000 if (!(req->flags & SCTL_MASK32))
4002 return (SYSCTL_OUT(req, &l, sizeof(l)));
4006 return (SYSCTL_OUT(req, &i, sizeof(i)));
4007 case CTLTYPE_STRING:
4008 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4010 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4011 case CTLTYPE_STRUCT:
4013 return (SYSCTL_OUT(req, &s, sizeof(s)));
4018 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4019 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4020 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4021 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4022 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4023 "I", "Jail secure level");
4024 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4025 "I", "Jail cannot see all mounted file systems");
4026 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4027 "B", "Jail persistence");
4029 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4030 "E,jailsys", "Virtual network stack");
4032 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4033 "B", "Jail is in the process of shutting down");
4035 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4036 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4037 "I", "Current number of child jails");
4038 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4039 "I", "Maximum number of child jails");
4041 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4042 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4044 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4045 "Jail NIS domainname");
4046 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4048 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4049 "LU", "Jail host ID");
4051 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4052 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4055 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4056 "Jail IPv4 address virtualization");
4057 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4058 "S,in_addr,a", "Jail IPv4 addresses");
4061 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4062 "Jail IPv6 address virtualization");
4063 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4064 "S,in6_addr,a", "Jail IPv6 addresses");
4067 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4068 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4069 "B", "Jail may set hostname");
4070 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4071 "B", "Jail may use SYSV IPC");
4072 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4073 "B", "Jail may create raw sockets");
4074 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4075 "B", "Jail may alter system file flags");
4076 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4077 "B", "Jail may mount/unmount jail-friendly file systems");
4078 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4079 "B", "Jail may set file quotas");
4080 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4081 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4087 db_show_prison(struct prison *pr)
4090 #if defined(INET) || defined(INET6)
4095 char ip6buf[INET6_ADDRSTRLEN];
4098 db_printf("prison %p:\n", pr);
4099 db_printf(" jid = %d\n", pr->pr_id);
4100 db_printf(" name = %s\n", pr->pr_name);
4101 db_printf(" parent = %p\n", pr->pr_parent);
4102 db_printf(" ref = %d\n", pr->pr_ref);
4103 db_printf(" uref = %d\n", pr->pr_uref);
4104 db_printf(" path = %s\n", pr->pr_path);
4105 db_printf(" cpuset = %d\n", pr->pr_cpuset
4106 ? pr->pr_cpuset->cs_id : -1);
4108 db_printf(" vnet = %p\n", pr->pr_vnet);
4110 db_printf(" root = %p\n", pr->pr_root);
4111 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4112 db_printf(" childcount = %d\n", pr->pr_childcount);
4113 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4114 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4115 db_printf(" flags = %x", pr->pr_flags);
4116 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4118 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4119 db_printf(" %s", pr_flag_names[fi]);
4120 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4122 jsf = pr->pr_flags &
4123 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4124 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4125 pr_flag_jailsys[fi].disable &&
4126 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4127 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4130 db_printf(" allow = %x", pr->pr_allow);
4131 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4133 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4134 db_printf(" %s", pr_allow_names[fi]);
4136 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4137 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4138 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4139 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4140 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4142 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4143 for (ii = 0; ii < pr->pr_ip4s; ii++)
4144 db_printf(" %s %s\n",
4145 ii == 0 ? "ip4 =" : " ",
4146 inet_ntoa(pr->pr_ip4[ii]));
4149 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4150 for (ii = 0; ii < pr->pr_ip6s; ii++)
4151 db_printf(" %s %s\n",
4152 ii == 0 ? "ip6 =" : " ",
4153 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4157 DB_SHOW_COMMAND(prison, db_show_prison_command)
4163 * Show all prisons in the list, and prison0 which is not
4166 db_show_prison(&prison0);
4167 if (!db_pager_quit) {
4168 TAILQ_FOREACH(pr, &allprison, pr_list) {
4180 /* Look for a prison with the ID and with references. */
4181 TAILQ_FOREACH(pr, &allprison, pr_list)
4182 if (pr->pr_id == addr && pr->pr_ref > 0)
4185 /* Look again, without requiring a reference. */
4186 TAILQ_FOREACH(pr, &allprison, pr_list)
4187 if (pr->pr_id == addr)
4190 /* Assume address points to a valid prison. */
4191 pr = (struct prison *)addr;