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_flags = PR_HOST|PR_VNET,
96 .pr_allow = PR_ALLOW_ALL,
98 MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF);
100 /* allprison and lastprid are protected by allprison_lock. */
101 struct sx allprison_lock;
102 SX_SYSINIT(allprison_lock, &allprison_lock, "allprison");
103 struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison);
106 static int do_jail_attach(struct thread *td, struct prison *pr);
107 static void prison_complete(void *context, int pending);
108 static void prison_deref(struct prison *pr, int flags);
109 static char *prison_path(struct prison *pr1, struct prison *pr2);
110 static void prison_remove_one(struct prison *pr);
112 static int _prison_check_ip4(struct prison *pr, struct in_addr *ia);
113 static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4);
116 static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6);
117 static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6);
120 /* Flags for prison_deref */
121 #define PD_DEREF 0x01
122 #define PD_DEUREF 0x02
123 #define PD_LOCKED 0x04
124 #define PD_LIST_SLOCKED 0x08
125 #define PD_LIST_XLOCKED 0x10
128 * Parameter names corresponding to PR_* flag values
130 static char *pr_flag_names[] = {
134 static char *pr_flag_nonames[] = {
138 struct jailsys_flags {
142 } pr_flag_jailsys[] = {
143 { "host", 0, PR_HOST },
145 { "vnet", 0, PR_VNET },
148 { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER },
151 { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER },
155 static char *pr_allow_names[] = {
156 "allow.set_hostname",
165 static char *pr_allow_nonames[] = {
166 "allow.noset_hostname",
168 "allow.noraw_sockets",
175 #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME
176 #define JAIL_DEFAULT_ENFORCE_STATFS 2
177 static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW;
178 static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
179 #if defined(INET) || defined(INET6)
180 static unsigned jail_max_af_ips = 255;
185 qcmp_v4(const void *ip1, const void *ip2)
190 * We need to compare in HBO here to get the list sorted as expected
191 * by the result of the code. Sorting NBO addresses gives you
192 * interesting results. If you do not understand, do not try.
194 iaa = ntohl(((const struct in_addr *)ip1)->s_addr);
195 iab = ntohl(((const struct in_addr *)ip2)->s_addr);
198 * Do not simply return the difference of the two numbers, the int is
212 qcmp_v6(const void *ip1, const void *ip2)
214 const struct in6_addr *ia6a, *ia6b;
217 ia6a = (const struct in6_addr *)ip1;
218 ia6b = (const struct in6_addr *)ip2;
221 for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) {
222 if (ia6a->s6_addr[i] > ia6b->s6_addr[i])
224 else if (ia6a->s6_addr[i] < ia6b->s6_addr[i])
237 jail(struct thread *td, struct jail_args *uap)
243 error = copyin(uap->jail, &version, sizeof(uint32_t));
252 /* FreeBSD single IPv4 jails. */
253 bzero(&j, sizeof(struct jail));
254 error = copyin(uap->jail, &j0, sizeof(struct jail_v0));
257 j.version = j0.version;
259 j.hostname = j0.hostname;
260 j.ip4s = j0.ip_number;
266 * Version 1 was used by multi-IPv4 jail implementations
267 * that never made it into the official kernel.
271 case 2: /* JAIL_API_VERSION */
272 /* FreeBSD multi-IPv4/IPv6,noIP jails. */
273 error = copyin(uap->jail, &j, sizeof(struct jail));
279 /* Sci-Fi jails are not supported, sorry. */
282 return (kern_jail(td, &j));
286 kern_jail(struct thread *td, struct jail *j)
288 struct iovec optiov[2 * (4
289 + sizeof(pr_allow_names) / sizeof(pr_allow_names[0])
298 char *u_path, *u_hostname, *u_name;
301 struct in_addr *u_ip4;
304 struct in6_addr *u_ip6;
307 int error, enforce_statfs, fi;
309 bzero(&optiov, sizeof(optiov));
310 opt.uio_iov = optiov;
314 opt.uio_segflg = UIO_SYSSPACE;
315 opt.uio_rw = UIO_READ;
318 /* Set permissions for top-level jails from sysctls. */
319 if (!jailed(td->td_ucred)) {
320 for (fi = 0; fi < sizeof(pr_allow_names) /
321 sizeof(pr_allow_names[0]); fi++) {
322 optiov[opt.uio_iovcnt].iov_base =
323 (jail_default_allow & (1 << fi))
324 ? pr_allow_names[fi] : pr_allow_nonames[fi];
325 optiov[opt.uio_iovcnt].iov_len =
326 strlen(optiov[opt.uio_iovcnt].iov_base) + 1;
329 optiov[opt.uio_iovcnt].iov_base = "enforce_statfs";
330 optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs");
332 enforce_statfs = jail_default_enforce_statfs;
333 optiov[opt.uio_iovcnt].iov_base = &enforce_statfs;
334 optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs);
338 tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN;
340 ip4s = (j->version == 0) ? 1 : j->ip4s;
341 if (ip4s > jail_max_af_ips)
343 tmplen += ip4s * sizeof(struct in_addr);
349 if (j->ip6s > jail_max_af_ips)
351 tmplen += j->ip6s * sizeof(struct in6_addr);
356 u_path = malloc(tmplen, M_TEMP, M_WAITOK);
357 u_hostname = u_path + MAXPATHLEN;
358 u_name = u_hostname + MAXHOSTNAMELEN;
360 u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN);
364 u_ip6 = (struct in6_addr *)(u_ip4 + ip4s);
366 u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN);
369 optiov[opt.uio_iovcnt].iov_base = "path";
370 optiov[opt.uio_iovcnt].iov_len = sizeof("path");
372 optiov[opt.uio_iovcnt].iov_base = u_path;
373 error = copyinstr(j->path, u_path, MAXPATHLEN,
374 &optiov[opt.uio_iovcnt].iov_len);
376 free(u_path, M_TEMP);
380 optiov[opt.uio_iovcnt].iov_base = "host.hostname";
381 optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname");
383 optiov[opt.uio_iovcnt].iov_base = u_hostname;
384 error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN,
385 &optiov[opt.uio_iovcnt].iov_len);
387 free(u_path, M_TEMP);
391 if (j->jailname != NULL) {
392 optiov[opt.uio_iovcnt].iov_base = "name";
393 optiov[opt.uio_iovcnt].iov_len = sizeof("name");
395 optiov[opt.uio_iovcnt].iov_base = u_name;
396 error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN,
397 &optiov[opt.uio_iovcnt].iov_len);
399 free(u_path, M_TEMP);
405 optiov[opt.uio_iovcnt].iov_base = "ip4.addr";
406 optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr");
408 optiov[opt.uio_iovcnt].iov_base = u_ip4;
409 optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr);
411 u_ip4->s_addr = j->ip4s;
413 error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len);
415 free(u_path, M_TEMP);
422 optiov[opt.uio_iovcnt].iov_base = "ip6.addr";
423 optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr");
425 optiov[opt.uio_iovcnt].iov_base = u_ip6;
426 optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr);
427 error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len);
429 free(u_path, M_TEMP);
434 KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]),
435 ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt));
436 error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH);
437 free(u_path, M_TEMP);
443 * struct jail_set_args {
444 * struct iovec *iovp;
445 * unsigned int iovcnt;
450 jail_set(struct thread *td, struct jail_set_args *uap)
455 /* Check that we have an even number of iovecs. */
459 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
462 error = kern_jail_set(td, auio, uap->flags);
468 kern_jail_set(struct thread *td, struct uio *optuio, int flags)
475 struct in6_addr *ip6;
478 struct vfsoptlist *opts;
479 struct prison *pr, *deadpr, *mypr, *ppr, *tpr;
481 char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid;
482 #if defined(INET) || defined(INET6)
487 size_t namelen, onamelen;
488 int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos;
489 int gotchildmax, gotenforce, gothid, gotslevel;
490 int fi, jid, jsys, len, level;
491 int childmax, slevel, vfslocked;
492 #if defined(INET) || defined(INET6)
501 unsigned pr_flags, ch_flags;
502 unsigned pr_allow, ch_allow, tallow;
505 error = priv_check(td, PRIV_JAIL_SET);
506 if (!error && (flags & JAIL_ATTACH))
507 error = priv_check(td, PRIV_JAIL_ATTACH);
510 mypr = ppr = td->td_ucred->cr_prison;
511 if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0)
513 if (flags & ~JAIL_SET_MASK)
517 * Check all the parameters before committing to anything. Not all
518 * errors can be caught early, but we may as well try. Also, this
519 * takes care of some expensive stuff (path lookup) before getting
520 * the allprison lock.
522 * XXX Jails are not filesystems, and jail parameters are not mount
523 * options. But it makes more sense to re-use the vfsopt code
524 * than duplicate it under a different name.
526 error = vfs_buildopts(optuio, &opts);
536 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
542 error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel));
551 vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax));
559 error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce));
560 gotenforce = (error == 0);
562 if (enforce < 0 || enforce > 2)
564 } else if (error != ENOENT)
567 pr_flags = ch_flags = 0;
568 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
570 if (pr_flag_names[fi] == NULL)
572 vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi);
573 vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi);
575 ch_flags |= pr_flags;
576 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
578 error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys,
585 case JAIL_SYS_DISABLE:
586 if (!pr_flag_jailsys[fi].disable) {
590 pr_flags |= pr_flag_jailsys[fi].disable;
593 pr_flags |= pr_flag_jailsys[fi].new;
595 case JAIL_SYS_INHERIT:
602 pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable;
604 if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE
605 && !(pr_flags & PR_PERSIST)) {
607 vfs_opterror(opts, "new jail must persist or attach");
611 if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) {
613 vfs_opterror(opts, "vnet cannot be changed after creation");
618 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) {
620 vfs_opterror(opts, "ip4 cannot be changed after creation");
625 if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) {
627 vfs_opterror(opts, "ip6 cannot be changed after creation");
632 pr_allow = ch_allow = 0;
633 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
635 vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi);
636 vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi);
638 ch_allow |= pr_allow;
640 error = vfs_getopt(opts, "name", (void **)&name, &len);
646 if (len == 0 || name[len - 1] != '\0') {
650 if (len > MAXHOSTNAMELEN) {
651 error = ENAMETOOLONG;
656 error = vfs_getopt(opts, "host.hostname", (void **)&host, &len);
664 if (len == 0 || host[len - 1] != '\0') {
668 if (len > MAXHOSTNAMELEN) {
669 error = ENAMETOOLONG;
674 error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len);
682 if (len == 0 || domain[len - 1] != '\0') {
686 if (len > MAXHOSTNAMELEN) {
687 error = ENAMETOOLONG;
692 error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len);
700 if (len == 0 || uuid[len - 1] != '\0') {
704 if (len > HOSTUUIDLEN) {
705 error = ENAMETOOLONG;
711 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
714 error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32));
718 error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid));
730 error = vfs_getopt(opts, "ip4.addr", &op, &ip4s);
732 ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1;
735 else if (ip4s & (sizeof(*ip4) - 1)) {
739 ch_flags |= PR_IP4_USER | PR_IP4_DISABLE;
741 pr_flags |= PR_IP4_USER | PR_IP4_DISABLE;
743 pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER;
744 ip4s /= sizeof(*ip4);
745 if (ip4s > jail_max_af_ips) {
747 vfs_opterror(opts, "too many IPv4 addresses");
750 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
751 bcopy(op, ip4, ip4s * sizeof(*ip4));
753 * IP addresses are all sorted but ip[0] to preserve
754 * the primary IP address as given from userland.
755 * This special IP is used for unbound outgoing
756 * connections as well for "loopback" traffic.
759 qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4);
761 * Check for duplicate addresses and do some simple
762 * zero and broadcast checks. If users give other bogus
763 * addresses it is their problem.
765 * We do not have to care about byte order for these
766 * checks so we will do them in NBO.
768 for (ii = 0; ii < ip4s; ii++) {
769 if (ip4[ii].s_addr == INADDR_ANY ||
770 ip4[ii].s_addr == INADDR_BROADCAST) {
775 (ip4[0].s_addr == ip4[ii+1].s_addr ||
776 ip4[ii].s_addr == ip4[ii+1].s_addr)) {
786 error = vfs_getopt(opts, "ip6.addr", &op, &ip6s);
788 ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1;
791 else if (ip6s & (sizeof(*ip6) - 1)) {
795 ch_flags |= PR_IP6_USER | PR_IP6_DISABLE;
797 pr_flags |= PR_IP6_USER | PR_IP6_DISABLE;
799 pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER;
800 ip6s /= sizeof(*ip6);
801 if (ip6s > jail_max_af_ips) {
803 vfs_opterror(opts, "too many IPv6 addresses");
806 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
807 bcopy(op, ip6, ip6s * sizeof(*ip6));
809 qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6);
810 for (ii = 0; ii < ip6s; ii++) {
811 if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) {
816 (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) ||
817 IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1])))
827 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
828 if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
831 "vnet jails cannot have IP address restrictions");
837 error = vfs_getopt(opts, "path", (void **)&path, &len);
843 if (flags & JAIL_UPDATE) {
846 "path cannot be changed after creation");
849 if (len == 0 || path[len - 1] != '\0') {
853 if (len < 2 || (len == 2 && path[0] == '/'))
856 /* Leave room for a real-root full pathname. */
857 if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/")
858 ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) {
859 error = ENAMETOOLONG;
862 NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE,
867 vfslocked = NDHASGIANT(&nd);
869 NDFREE(&nd, NDF_ONLY_PNBUF);
870 if (root->v_type != VDIR) {
873 VFS_UNLOCK_GIANT(vfslocked);
876 VFS_UNLOCK_GIANT(vfslocked);
881 * Grab the allprison lock before letting modules check their
882 * parameters. Once we have it, do not let go so we'll have a
883 * consistent view of the OSD list.
885 sx_xlock(&allprison_lock);
886 error = osd_jail_call(NULL, PR_METHOD_CHECK, opts);
888 goto done_unlock_list;
890 /* By now, all parameters should have been noted. */
891 TAILQ_FOREACH(opt, opts, link) {
892 if (!opt->seen && strcmp(opt->name, "errmsg")) {
894 vfs_opterror(opts, "unknown parameter: %s", opt->name);
895 goto done_unlock_list;
900 * See if we are creating a new record or updating an existing one.
901 * This abuses the file error codes ENOENT and EEXIST.
903 cuflags = flags & (JAIL_CREATE | JAIL_UPDATE);
906 vfs_opterror(opts, "no valid operation (create or update)");
907 goto done_unlock_list;
911 if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) {
912 namelc = strrchr(name, '.');
913 jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10);
919 * See if a requested jid already exists. There is an
920 * information leak here if the jid exists but is not within
921 * the caller's jail hierarchy. Jail creators will get EEXIST
922 * even though they cannot see the jail, and CREATE | UPDATE
923 * will return ENOENT which is not normally a valid error.
927 vfs_opterror(opts, "negative jid");
928 goto done_unlock_list;
930 pr = prison_find(jid);
933 /* Create: jid must not exist. */
934 if (cuflags == JAIL_CREATE) {
935 mtx_unlock(&pr->pr_mtx);
937 vfs_opterror(opts, "jail %d already exists",
939 goto done_unlock_list;
941 if (!prison_ischild(mypr, pr)) {
942 mtx_unlock(&pr->pr_mtx);
944 } else if (pr->pr_uref == 0) {
945 if (!(flags & JAIL_DYING)) {
946 mtx_unlock(&pr->pr_mtx);
948 vfs_opterror(opts, "jail %d is dying",
950 goto done_unlock_list;
951 } else if ((flags & JAIL_ATTACH) ||
952 (pr_flags & PR_PERSIST)) {
954 * A dying jail might be resurrected
955 * (via attach or persist), but first
956 * it must determine if another jail
957 * has claimed its name. Accomplish
958 * this by implicitly re-setting the
962 name = prison_name(mypr, pr);
967 /* Update: jid must exist. */
968 if (cuflags == JAIL_UPDATE) {
970 vfs_opterror(opts, "jail %d not found", jid);
971 goto done_unlock_list;
976 * If the caller provided a name, look for a jail by that name.
977 * This has different semantics for creates and updates keyed by jid
978 * (where the name must not already exist in a different jail),
979 * and updates keyed by the name itself (where the name must exist
980 * because that is the jail being updated).
983 namelc = strrchr(name, '.');
988 * This is a hierarchical name. Split it into the
989 * parent and child names, and make sure the parent
990 * exists or matches an already found jail.
994 if (strncmp(name, ppr->pr_name, namelc - name)
995 || ppr->pr_name[namelc - name] != '\0') {
996 mtx_unlock(&pr->pr_mtx);
999 "cannot change jail's parent");
1000 goto done_unlock_list;
1003 ppr = prison_find_name(mypr, name);
1007 "jail \"%s\" not found", name);
1008 goto done_unlock_list;
1010 mtx_unlock(&ppr->pr_mtx);
1014 if (name[0] != '\0') {
1016 (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1;
1019 FOREACH_PRISON_CHILD(ppr, tpr) {
1020 if (tpr != pr && tpr->pr_ref > 0 &&
1021 !strcmp(tpr->pr_name + namelen, name)) {
1023 cuflags != JAIL_CREATE) {
1024 mtx_lock(&tpr->pr_mtx);
1025 if (tpr->pr_ref > 0) {
1030 if (tpr->pr_uref > 0) {
1036 mtx_unlock(&tpr->pr_mtx);
1037 } else if (tpr->pr_uref > 0) {
1039 * Create, or update(jid):
1040 * name must not exist in an
1041 * active sibling jail.
1045 mtx_unlock(&pr->pr_mtx);
1047 "jail \"%s\" already exists",
1049 goto done_unlock_list;
1053 /* If no active jail is found, use a dying one. */
1054 if (deadpr != NULL && pr == NULL) {
1055 if (flags & JAIL_DYING) {
1056 mtx_lock(&deadpr->pr_mtx);
1057 if (deadpr->pr_ref == 0) {
1058 mtx_unlock(&deadpr->pr_mtx);
1062 } else if (cuflags == JAIL_UPDATE) {
1065 "jail \"%s\" is dying", name);
1066 goto done_unlock_list;
1069 /* Update: name must exist if no jid. */
1070 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1072 vfs_opterror(opts, "jail \"%s\" not found",
1074 goto done_unlock_list;
1078 /* Update: must provide a jid or name. */
1079 else if (cuflags == JAIL_UPDATE && pr == NULL) {
1081 vfs_opterror(opts, "update specified no jail");
1082 goto done_unlock_list;
1085 /* If there's no prison to update, create a new one and link it in. */
1087 for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent)
1088 if (tpr->pr_childcount >= tpr->pr_childmax) {
1090 vfs_opterror(opts, "prison limit exceeded");
1091 goto done_unlock_list;
1094 mtx_lock(&ppr->pr_mtx);
1095 if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) {
1096 mtx_unlock(&ppr->pr_mtx);
1098 vfs_opterror(opts, "parent jail went away!");
1099 goto done_unlock_list;
1103 mtx_unlock(&ppr->pr_mtx);
1104 pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO);
1106 /* Find the next free jid. */
1109 if (jid == JAIL_MAX)
1111 TAILQ_FOREACH(tpr, &allprison, pr_list) {
1112 if (tpr->pr_id < jid)
1114 if (tpr->pr_id > jid || tpr->pr_ref == 0) {
1115 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1118 if (jid == lastprid) {
1121 "no available jail IDs");
1123 prison_deref(ppr, PD_DEREF |
1124 PD_DEUREF | PD_LIST_XLOCKED);
1133 * The jail already has a jid (that did not yet exist),
1134 * so just find where to insert it.
1136 TAILQ_FOREACH(tpr, &allprison, pr_list)
1137 if (tpr->pr_id >= jid) {
1138 TAILQ_INSERT_BEFORE(tpr, pr, pr_list);
1143 TAILQ_INSERT_TAIL(&allprison, pr, pr_list);
1144 LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling);
1145 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
1146 tpr->pr_childcount++;
1148 pr->pr_parent = ppr;
1151 /* Set some default values, and inherit some from the parent. */
1156 root = mypr->pr_root;
1159 strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN);
1160 pr->pr_flags |= PR_HOST;
1161 #if defined(INET) || defined(INET6)
1163 if (!(pr_flags & PR_VNET))
1167 if (!(ch_flags & PR_IP4_USER))
1169 PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE;
1170 else if (!(pr_flags & PR_IP4_USER)) {
1171 pr->pr_flags |= ppr->pr_flags & PR_IP4;
1172 if (ppr->pr_ip4 != NULL) {
1173 pr->pr_ip4s = ppr->pr_ip4s;
1174 pr->pr_ip4 = malloc(pr->pr_ip4s *
1175 sizeof(struct in_addr), M_PRISON,
1177 bcopy(ppr->pr_ip4, pr->pr_ip4,
1178 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1183 if (!(ch_flags & PR_IP6_USER))
1185 PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE;
1186 else if (!(pr_flags & PR_IP6_USER)) {
1187 pr->pr_flags |= ppr->pr_flags & PR_IP6;
1188 if (ppr->pr_ip6 != NULL) {
1189 pr->pr_ip6s = ppr->pr_ip6s;
1190 pr->pr_ip6 = malloc(pr->pr_ip6s *
1191 sizeof(struct in6_addr), M_PRISON,
1193 bcopy(ppr->pr_ip6, pr->pr_ip6,
1194 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1200 pr->pr_securelevel = ppr->pr_securelevel;
1201 pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow;
1202 pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS;
1204 LIST_INIT(&pr->pr_children);
1205 mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK);
1208 /* Allocate a new vnet if specified. */
1209 pr->pr_vnet = (pr_flags & PR_VNET)
1210 ? vnet_alloc() : ppr->pr_vnet;
1213 * Allocate a dedicated cpuset for each jail.
1214 * Unlike other initial settings, this may return an erorr.
1216 error = cpuset_create_root(ppr, &pr->pr_cpuset);
1218 prison_deref(pr, PD_LIST_XLOCKED);
1222 mtx_lock(&pr->pr_mtx);
1224 * New prisons do not yet have a reference, because we do not
1225 * want other to see the incomplete prison once the
1226 * allprison_lock is downgraded.
1231 * Grab a reference for existing prisons, to ensure they
1232 * continue to exist for the duration of the call.
1235 #if defined(VIMAGE) && (defined(INET) || defined(INET6))
1236 if ((pr->pr_flags & PR_VNET) &&
1237 (ch_flags & (PR_IP4_USER | PR_IP6_USER))) {
1240 "vnet jails cannot have IP address restrictions");
1241 goto done_deref_locked;
1245 if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1248 "ip4 cannot be changed after creation");
1249 goto done_deref_locked;
1253 if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) {
1256 "ip6 cannot be changed after creation");
1257 goto done_deref_locked;
1262 /* Do final error checking before setting anything. */
1264 if (slevel < ppr->pr_securelevel) {
1266 goto done_deref_locked;
1270 if (childmax >= ppr->pr_childmax) {
1272 goto done_deref_locked;
1276 if (enforce < ppr->pr_enforce_statfs) {
1278 goto done_deref_locked;
1283 if (ppr->pr_flags & PR_IP4) {
1285 * Make sure the new set of IP addresses is a
1286 * subset of the parent's list. Don't worry
1287 * about the parent being unlocked, as any
1288 * setting is done with allprison_lock held.
1290 for (ij = 0; ij < ppr->pr_ip4s; ij++)
1291 if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
1293 if (ij == ppr->pr_ip4s) {
1295 goto done_deref_locked;
1298 for (ii = ij = 1; ii < ip4s; ii++) {
1299 if (ip4[ii].s_addr ==
1300 ppr->pr_ip4[0].s_addr)
1302 for (; ij < ppr->pr_ip4s; ij++)
1303 if (ip4[ii].s_addr ==
1304 ppr->pr_ip4[ij].s_addr)
1306 if (ij == ppr->pr_ip4s)
1309 if (ij == ppr->pr_ip4s) {
1311 goto done_deref_locked;
1316 * Check for conflicting IP addresses. We permit them
1317 * if there is no more than one IP on each jail. If
1318 * there is a duplicate on a jail with more than one
1319 * IP stop checking and return error.
1323 for (; tppr != &prison0; tppr = tppr->pr_parent)
1324 if (tppr->pr_flags & PR_VNET)
1327 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1330 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1332 tpr->pr_uref == 0) {
1336 if (!(tpr->pr_flags & PR_IP4_USER))
1339 if (tpr->pr_ip4 == NULL ||
1340 (ip4s == 1 && tpr->pr_ip4s == 1))
1342 for (ii = 0; ii < ip4s; ii++) {
1343 if (_prison_check_ip4(tpr, &ip4[ii]) == 0) {
1346 "IPv4 addresses clash");
1347 goto done_deref_locked;
1355 if (ppr->pr_flags & PR_IP6) {
1357 * Make sure the new set of IP addresses is a
1358 * subset of the parent's list.
1360 for (ij = 0; ij < ppr->pr_ip6s; ij++)
1361 if (IN6_ARE_ADDR_EQUAL(&ip6[0],
1364 if (ij == ppr->pr_ip6s) {
1366 goto done_deref_locked;
1369 for (ii = ij = 1; ii < ip6s; ii++) {
1370 if (IN6_ARE_ADDR_EQUAL(&ip6[ii],
1373 for (; ij < ppr->pr_ip6s; ij++)
1374 if (IN6_ARE_ADDR_EQUAL(
1375 &ip6[ii], &ppr->pr_ip6[ij]))
1377 if (ij == ppr->pr_ip6s)
1380 if (ij == ppr->pr_ip6s) {
1382 goto done_deref_locked;
1386 /* Check for conflicting IP addresses. */
1389 for (; tppr != &prison0; tppr = tppr->pr_parent)
1390 if (tppr->pr_flags & PR_VNET)
1393 FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) {
1396 (tpr != tppr && (tpr->pr_flags & PR_VNET)) ||
1398 tpr->pr_uref == 0) {
1402 if (!(tpr->pr_flags & PR_IP6_USER))
1405 if (tpr->pr_ip6 == NULL ||
1406 (ip6s == 1 && tpr->pr_ip6s == 1))
1408 for (ii = 0; ii < ip6s; ii++) {
1409 if (_prison_check_ip6(tpr, &ip6[ii]) == 0) {
1412 "IPv6 addresses clash");
1413 goto done_deref_locked;
1419 onamelen = namelen = 0;
1421 /* Give a default name of the jid. */
1422 if (name[0] == '\0')
1423 snprintf(name = numbuf, sizeof(numbuf), "%d", jid);
1424 else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid &&
1428 "name cannot be numeric (unless it is the jid)");
1429 goto done_deref_locked;
1432 * Make sure the name isn't too long for the prison or its
1435 onamelen = strlen(pr->pr_name);
1436 namelen = strlen(name);
1437 if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) {
1438 error = ENAMETOOLONG;
1439 goto done_deref_locked;
1441 FOREACH_PRISON_DESCENDANT(pr, tpr, descend) {
1442 if (strlen(tpr->pr_name) + (namelen - onamelen) >=
1443 sizeof(pr->pr_name)) {
1444 error = ENAMETOOLONG;
1445 goto done_deref_locked;
1449 if (pr_allow & ~ppr->pr_allow) {
1451 goto done_deref_locked;
1454 /* Set the parameters of the prison. */
1457 if (pr_flags & PR_IP4_USER) {
1458 pr->pr_flags |= PR_IP4;
1459 free(pr->pr_ip4, M_PRISON);
1463 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1465 if (tpr->pr_flags & PR_VNET) {
1470 if (prison_restrict_ip4(tpr, NULL)) {
1479 if (pr_flags & PR_IP6_USER) {
1480 pr->pr_flags |= PR_IP6;
1481 free(pr->pr_ip6, M_PRISON);
1485 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1487 if (tpr->pr_flags & PR_VNET) {
1492 if (prison_restrict_ip6(tpr, NULL)) {
1500 pr->pr_securelevel = slevel;
1501 /* Set all child jails to be at least this level. */
1502 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1503 if (tpr->pr_securelevel < slevel)
1504 tpr->pr_securelevel = slevel;
1507 pr->pr_childmax = childmax;
1508 /* Set all child jails to under this limit. */
1509 FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level)
1510 if (tpr->pr_childmax > childmax - level)
1511 tpr->pr_childmax = childmax > level
1512 ? childmax - level : 0;
1515 pr->pr_enforce_statfs = enforce;
1516 /* Pass this restriction on to the children. */
1517 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1518 if (tpr->pr_enforce_statfs < enforce)
1519 tpr->pr_enforce_statfs = enforce;
1522 if (ppr == &prison0)
1523 strlcpy(pr->pr_name, name, sizeof(pr->pr_name));
1525 snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s",
1526 ppr->pr_name, name);
1527 /* Change this component of child names. */
1528 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1529 bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen,
1530 strlen(tpr->pr_name + onamelen) + 1);
1531 bcopy(pr->pr_name, tpr->pr_name, namelen);
1535 /* Try to keep a real-rooted full pathname. */
1536 if (path[0] == '/' && strcmp(mypr->pr_path, "/"))
1537 snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s",
1538 mypr->pr_path, path);
1540 strlcpy(pr->pr_path, path, sizeof(pr->pr_path));
1543 if (PR_HOST & ch_flags & ~pr_flags) {
1544 if (pr->pr_flags & PR_HOST) {
1546 * Copy the parent's host info. As with pr_ip4 above,
1547 * the lack of a lock on the parent is not a problem;
1548 * it is always set with allprison_lock at least
1549 * shared, and is held exclusively here.
1551 strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname,
1552 sizeof(pr->pr_hostname));
1553 strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname,
1554 sizeof(pr->pr_domainname));
1555 strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid,
1556 sizeof(pr->pr_hostuuid));
1557 pr->pr_hostid = pr->pr_parent->pr_hostid;
1559 } else if (host != NULL || domain != NULL || uuid != NULL || gothid) {
1560 /* Set this prison, and any descendants without PR_HOST. */
1562 strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname));
1564 strlcpy(pr->pr_domainname, domain,
1565 sizeof(pr->pr_domainname));
1567 strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid));
1569 pr->pr_hostid = hid;
1570 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1571 if (tpr->pr_flags & PR_HOST)
1575 strlcpy(tpr->pr_hostname,
1577 sizeof(tpr->pr_hostname));
1579 strlcpy(tpr->pr_domainname,
1581 sizeof(tpr->pr_domainname));
1583 strlcpy(tpr->pr_hostuuid,
1585 sizeof(tpr->pr_hostuuid));
1587 tpr->pr_hostid = hid;
1591 if ((tallow = ch_allow & ~pr_allow)) {
1592 /* Clear allow bits in all children. */
1593 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend)
1594 tpr->pr_allow &= ~tallow;
1596 pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow;
1598 * Persistent prisons get an extra reference, and prisons losing their
1599 * persist flag lose that reference. Only do this for existing prisons
1600 * for now, so new ones will remain unseen until after the module
1601 * handlers have completed.
1603 if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) {
1604 if (pr_flags & PR_PERSIST) {
1612 pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags;
1613 mtx_unlock(&pr->pr_mtx);
1615 /* Locks may have prevented a complete restriction of child IP
1616 * addresses. If so, allocate some more memory and try again.
1621 ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK);
1622 mtx_lock(&pr->pr_mtx);
1624 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1626 if (tpr->pr_flags & PR_VNET) {
1631 if (prison_restrict_ip4(tpr, ip4)) {
1638 mtx_unlock(&pr->pr_mtx);
1644 ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK);
1645 mtx_lock(&pr->pr_mtx);
1647 FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) {
1649 if (tpr->pr_flags & PR_VNET) {
1654 if (prison_restrict_ip6(tpr, ip6)) {
1661 mtx_unlock(&pr->pr_mtx);
1665 /* Let the modules do their work. */
1666 sx_downgrade(&allprison_lock);
1668 error = osd_jail_call(pr, PR_METHOD_CREATE, opts);
1670 prison_deref(pr, PD_LIST_SLOCKED);
1674 error = osd_jail_call(pr, PR_METHOD_SET, opts);
1676 prison_deref(pr, created
1678 : PD_DEREF | PD_LIST_SLOCKED);
1682 /* Attach this process to the prison if requested. */
1683 if (flags & JAIL_ATTACH) {
1684 mtx_lock(&pr->pr_mtx);
1685 error = do_jail_attach(td, pr);
1687 vfs_opterror(opts, "attach failed");
1689 prison_deref(pr, PD_DEREF);
1695 * Now that it is all there, drop the temporary reference from existing
1696 * prisons. Or add a reference to newly created persistent prisons
1697 * (which was not done earlier so that the prison would not be publicly
1701 prison_deref(pr, (flags & JAIL_ATTACH)
1703 : PD_DEREF | PD_LIST_SLOCKED);
1705 if (pr_flags & PR_PERSIST) {
1706 mtx_lock(&pr->pr_mtx);
1709 mtx_unlock(&pr->pr_mtx);
1711 if (!(flags & JAIL_ATTACH))
1712 sx_sunlock(&allprison_lock);
1714 td->td_retval[0] = pr->pr_id;
1718 prison_deref(pr, created
1719 ? PD_LOCKED | PD_LIST_XLOCKED
1720 : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
1723 sx_xunlock(&allprison_lock);
1726 vfslocked = VFS_LOCK_GIANT(root->v_mount);
1728 VFS_UNLOCK_GIANT(vfslocked);
1732 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
1733 if (errmsg_len > 0) {
1734 errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1;
1735 if (errmsg_pos > 0) {
1736 if (optuio->uio_segflg == UIO_SYSSPACE)
1738 optuio->uio_iov[errmsg_pos].iov_base,
1742 optuio->uio_iov[errmsg_pos].iov_base,
1749 free(ip4, M_PRISON);
1752 free(ip6, M_PRISON);
1760 * struct jail_get_args {
1761 * struct iovec *iovp;
1762 * unsigned int iovcnt;
1767 jail_get(struct thread *td, struct jail_get_args *uap)
1772 /* Check that we have an even number of iovecs. */
1773 if (uap->iovcnt & 1)
1776 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
1779 error = kern_jail_get(td, auio, uap->flags);
1781 error = copyout(auio->uio_iov, uap->iovp,
1782 uap->iovcnt * sizeof (struct iovec));
1788 kern_jail_get(struct thread *td, struct uio *optuio, int flags)
1790 struct prison *pr, *mypr;
1792 struct vfsoptlist *opts;
1793 char *errmsg, *name;
1794 int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos;
1796 if (flags & ~JAIL_GET_MASK)
1799 /* Get the parameter list. */
1800 error = vfs_buildopts(optuio, &opts);
1803 errmsg_pos = vfs_getopt_pos(opts, "errmsg");
1804 mypr = td->td_ucred->cr_prison;
1807 * Find the prison specified by one of: lastjid, jid, name.
1809 sx_slock(&allprison_lock);
1810 error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid));
1812 TAILQ_FOREACH(pr, &allprison, pr_list) {
1813 if (pr->pr_id > jid && prison_ischild(mypr, pr)) {
1814 mtx_lock(&pr->pr_mtx);
1815 if (pr->pr_ref > 0 &&
1816 (pr->pr_uref > 0 || (flags & JAIL_DYING)))
1818 mtx_unlock(&pr->pr_mtx);
1824 vfs_opterror(opts, "no jail after %d", jid);
1825 goto done_unlock_list;
1826 } else if (error != ENOENT)
1827 goto done_unlock_list;
1829 error = vfs_copyopt(opts, "jid", &jid, sizeof(jid));
1832 pr = prison_find_child(mypr, jid);
1834 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1835 mtx_unlock(&pr->pr_mtx);
1837 vfs_opterror(opts, "jail %d is dying",
1839 goto done_unlock_list;
1844 vfs_opterror(opts, "jail %d not found", jid);
1845 goto done_unlock_list;
1847 } else if (error != ENOENT)
1848 goto done_unlock_list;
1850 error = vfs_getopt(opts, "name", (void **)&name, &len);
1852 if (len == 0 || name[len - 1] != '\0') {
1854 goto done_unlock_list;
1856 pr = prison_find_name(mypr, name);
1858 if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) {
1859 mtx_unlock(&pr->pr_mtx);
1861 vfs_opterror(opts, "jail \"%s\" is dying",
1863 goto done_unlock_list;
1868 vfs_opterror(opts, "jail \"%s\" not found", name);
1869 goto done_unlock_list;
1870 } else if (error != ENOENT)
1871 goto done_unlock_list;
1873 vfs_opterror(opts, "no jail specified");
1875 goto done_unlock_list;
1878 /* Get the parameters of the prison. */
1881 td->td_retval[0] = pr->pr_id;
1882 error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id));
1883 if (error != 0 && error != ENOENT)
1885 i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id;
1886 error = vfs_setopt(opts, "parent", &i, sizeof(i));
1887 if (error != 0 && error != ENOENT)
1889 error = vfs_setopts(opts, "name", prison_name(mypr, pr));
1890 if (error != 0 && error != ENOENT)
1892 error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id,
1893 sizeof(pr->pr_cpuset->cs_id));
1894 if (error != 0 && error != ENOENT)
1896 error = vfs_setopts(opts, "path", prison_path(mypr, pr));
1897 if (error != 0 && error != ENOENT)
1900 error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4,
1901 pr->pr_ip4s * sizeof(*pr->pr_ip4));
1902 if (error != 0 && error != ENOENT)
1906 error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6,
1907 pr->pr_ip6s * sizeof(*pr->pr_ip6));
1908 if (error != 0 && error != ENOENT)
1911 error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel,
1912 sizeof(pr->pr_securelevel));
1913 if (error != 0 && error != ENOENT)
1915 error = vfs_setopt(opts, "children.cur", &pr->pr_childcount,
1916 sizeof(pr->pr_childcount));
1917 if (error != 0 && error != ENOENT)
1919 error = vfs_setopt(opts, "children.max", &pr->pr_childmax,
1920 sizeof(pr->pr_childmax));
1921 if (error != 0 && error != ENOENT)
1923 error = vfs_setopts(opts, "host.hostname", pr->pr_hostname);
1924 if (error != 0 && error != ENOENT)
1926 error = vfs_setopts(opts, "host.domainname", pr->pr_domainname);
1927 if (error != 0 && error != ENOENT)
1929 error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid);
1930 if (error != 0 && error != ENOENT)
1933 if (td->td_proc->p_sysent->sv_flags & SV_IA32) {
1934 uint32_t hid32 = pr->pr_hostid;
1936 error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32));
1939 error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid,
1940 sizeof(pr->pr_hostid));
1941 if (error != 0 && error != ENOENT)
1943 error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs,
1944 sizeof(pr->pr_enforce_statfs));
1945 if (error != 0 && error != ENOENT)
1947 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
1949 if (pr_flag_names[fi] == NULL)
1951 i = (pr->pr_flags & (1 << fi)) ? 1 : 0;
1952 error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i));
1953 if (error != 0 && error != ENOENT)
1956 error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i));
1957 if (error != 0 && error != ENOENT)
1960 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
1963 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
1964 i = pr_flag_jailsys[fi].disable &&
1965 (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE
1966 : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW
1969 vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i));
1970 if (error != 0 && error != ENOENT)
1973 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
1975 if (pr_allow_names[fi] == NULL)
1977 i = (pr->pr_allow & (1 << fi)) ? 1 : 0;
1978 error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i));
1979 if (error != 0 && error != ENOENT)
1982 error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i));
1983 if (error != 0 && error != ENOENT)
1986 i = (pr->pr_uref == 0);
1987 error = vfs_setopt(opts, "dying", &i, sizeof(i));
1988 if (error != 0 && error != ENOENT)
1991 error = vfs_setopt(opts, "nodying", &i, sizeof(i));
1992 if (error != 0 && error != ENOENT)
1995 /* Get the module parameters. */
1996 mtx_unlock(&pr->pr_mtx);
1998 error = osd_jail_call(pr, PR_METHOD_GET, opts);
2001 prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED);
2003 /* By now, all parameters should have been noted. */
2004 TAILQ_FOREACH(opt, opts, link) {
2005 if (!opt->seen && strcmp(opt->name, "errmsg")) {
2007 vfs_opterror(opts, "unknown parameter: %s", opt->name);
2012 /* Write the fetched parameters back to userspace. */
2014 TAILQ_FOREACH(opt, opts, link) {
2015 if (opt->pos >= 0 && opt->pos != errmsg_pos) {
2016 pos = 2 * opt->pos + 1;
2017 optuio->uio_iov[pos].iov_len = opt->len;
2018 if (opt->value != NULL) {
2019 if (optuio->uio_segflg == UIO_SYSSPACE) {
2021 optuio->uio_iov[pos].iov_base,
2024 error = copyout(opt->value,
2025 optuio->uio_iov[pos].iov_base,
2036 prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED);
2040 sx_sunlock(&allprison_lock);
2042 if (error && errmsg_pos >= 0) {
2043 vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len);
2044 errmsg_pos = 2 * errmsg_pos + 1;
2045 if (errmsg_len > 0) {
2046 if (optuio->uio_segflg == UIO_SYSSPACE)
2048 optuio->uio_iov[errmsg_pos].iov_base,
2052 optuio->uio_iov[errmsg_pos].iov_base,
2062 * struct jail_remove_args {
2067 jail_remove(struct thread *td, struct jail_remove_args *uap)
2069 struct prison *pr, *cpr, *lpr, *tpr;
2072 error = priv_check(td, PRIV_JAIL_REMOVE);
2076 sx_xlock(&allprison_lock);
2077 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2079 sx_xunlock(&allprison_lock);
2083 /* Remove all descendants of this prison, then remove this prison. */
2085 pr->pr_flags |= PR_REMOVE;
2086 if (!LIST_EMPTY(&pr->pr_children)) {
2087 mtx_unlock(&pr->pr_mtx);
2089 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
2090 mtx_lock(&cpr->pr_mtx);
2091 if (cpr->pr_ref > 0) {
2094 cpr->pr_flags |= PR_REMOVE;
2096 /* Already removed - do not do it again. */
2099 mtx_unlock(&cpr->pr_mtx);
2101 mtx_lock(&lpr->pr_mtx);
2102 prison_remove_one(lpr);
2103 sx_xlock(&allprison_lock);
2108 mtx_lock(&lpr->pr_mtx);
2109 prison_remove_one(lpr);
2110 sx_xlock(&allprison_lock);
2112 mtx_lock(&pr->pr_mtx);
2114 prison_remove_one(pr);
2119 prison_remove_one(struct prison *pr)
2124 /* If the prison was persistent, it is not anymore. */
2126 if (pr->pr_flags & PR_PERSIST) {
2129 pr->pr_flags &= ~PR_PERSIST;
2133 * jail_remove added a reference. If that's the only one, remove
2136 KASSERT(pr->pr_ref > 0,
2137 ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id));
2138 if (pr->pr_ref == 1) {
2140 deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED);
2144 mtx_unlock(&pr->pr_mtx);
2145 sx_xunlock(&allprison_lock);
2147 * Kill all processes unfortunate enough to be attached to this prison.
2149 sx_slock(&allproc_lock);
2150 LIST_FOREACH(p, &allproc, p_list) {
2152 if (p->p_state != PRS_NEW && p->p_ucred &&
2153 p->p_ucred->cr_prison == pr)
2154 psignal(p, SIGKILL);
2157 sx_sunlock(&allproc_lock);
2158 /* Remove the temporary reference added by jail_remove. */
2159 prison_deref(pr, deuref | PD_DEREF);
2164 * struct jail_attach_args {
2169 jail_attach(struct thread *td, struct jail_attach_args *uap)
2174 error = priv_check(td, PRIV_JAIL_ATTACH);
2178 sx_slock(&allprison_lock);
2179 pr = prison_find_child(td->td_ucred->cr_prison, uap->jid);
2181 sx_sunlock(&allprison_lock);
2186 * Do not allow a process to attach to a prison that is not
2187 * considered to be "alive".
2189 if (pr->pr_uref == 0) {
2190 mtx_unlock(&pr->pr_mtx);
2191 sx_sunlock(&allprison_lock);
2195 return (do_jail_attach(td, pr));
2199 do_jail_attach(struct thread *td, struct prison *pr)
2203 struct ucred *newcred, *oldcred;
2204 int vfslocked, error;
2207 * XXX: Note that there is a slight race here if two threads
2208 * in the same privileged process attempt to attach to two
2209 * different jails at the same time. It is important for
2210 * user processes not to do this, or they might end up with
2211 * a process root from one prison, but attached to the jail
2216 mtx_unlock(&pr->pr_mtx);
2218 /* Let modules do whatever they need to prepare for attaching. */
2219 error = osd_jail_call(pr, PR_METHOD_ATTACH, td);
2221 prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED);
2224 sx_sunlock(&allprison_lock);
2227 * Reparent the newly attached process to this jail.
2229 ppr = td->td_ucred->cr_prison;
2231 error = cpuset_setproc_update_set(p, pr->pr_cpuset);
2235 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2236 vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY);
2237 if ((error = change_dir(pr->pr_root, td)) != 0)
2240 if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root)))
2243 VOP_UNLOCK(pr->pr_root, 0);
2244 if ((error = change_root(pr->pr_root, td)))
2245 goto e_unlock_giant;
2246 VFS_UNLOCK_GIANT(vfslocked);
2250 oldcred = p->p_ucred;
2252 crcopy(newcred, oldcred);
2253 newcred->cr_prison = pr;
2254 p->p_ucred = newcred;
2257 prison_deref(ppr, PD_DEREF | PD_DEUREF);
2260 VOP_UNLOCK(pr->pr_root, 0);
2262 VFS_UNLOCK_GIANT(vfslocked);
2264 /* Tell modules this thread is still in its old jail after all. */
2265 (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td);
2266 prison_deref(pr, PD_DEREF | PD_DEUREF);
2272 * Returns a locked prison instance, or NULL on failure.
2275 prison_find(int prid)
2279 sx_assert(&allprison_lock, SX_LOCKED);
2280 TAILQ_FOREACH(pr, &allprison, pr_list) {
2281 if (pr->pr_id == prid) {
2282 mtx_lock(&pr->pr_mtx);
2285 mtx_unlock(&pr->pr_mtx);
2292 * Find a prison that is a descendant of mypr. Returns a locked prison or NULL.
2295 prison_find_child(struct prison *mypr, int prid)
2300 sx_assert(&allprison_lock, SX_LOCKED);
2301 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2302 if (pr->pr_id == prid) {
2303 mtx_lock(&pr->pr_mtx);
2306 mtx_unlock(&pr->pr_mtx);
2313 * Look for the name relative to mypr. Returns a locked prison or NULL.
2316 prison_find_name(struct prison *mypr, const char *name)
2318 struct prison *pr, *deadpr;
2322 sx_assert(&allprison_lock, SX_LOCKED);
2323 mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1;
2326 FOREACH_PRISON_DESCENDANT(mypr, pr, descend) {
2327 if (!strcmp(pr->pr_name + mylen, name)) {
2328 mtx_lock(&pr->pr_mtx);
2329 if (pr->pr_ref > 0) {
2330 if (pr->pr_uref > 0)
2334 mtx_unlock(&pr->pr_mtx);
2337 /* There was no valid prison - perhaps there was a dying one. */
2338 if (deadpr != NULL) {
2339 mtx_lock(&deadpr->pr_mtx);
2340 if (deadpr->pr_ref == 0) {
2341 mtx_unlock(&deadpr->pr_mtx);
2349 * See if a prison has the specific flag set.
2352 prison_flag(struct ucred *cred, unsigned flag)
2355 /* This is an atomic read, so no locking is necessary. */
2356 return (cred->cr_prison->pr_flags & flag);
2360 prison_allow(struct ucred *cred, unsigned flag)
2363 /* This is an atomic read, so no locking is necessary. */
2364 return (cred->cr_prison->pr_allow & flag);
2368 * Remove a prison reference. If that was the last reference, remove the
2369 * prison itself - but not in this context in case there are locks held.
2372 prison_free_locked(struct prison *pr)
2375 mtx_assert(&pr->pr_mtx, MA_OWNED);
2377 if (pr->pr_ref == 0) {
2378 mtx_unlock(&pr->pr_mtx);
2379 TASK_INIT(&pr->pr_task, 0, prison_complete, pr);
2380 taskqueue_enqueue(taskqueue_thread, &pr->pr_task);
2383 mtx_unlock(&pr->pr_mtx);
2387 prison_free(struct prison *pr)
2390 mtx_lock(&pr->pr_mtx);
2391 prison_free_locked(pr);
2395 prison_complete(void *context, int pending)
2398 prison_deref((struct prison *)context, 0);
2402 * Remove a prison reference (usually). This internal version assumes no
2403 * mutexes are held, except perhaps the prison itself. If there are no more
2404 * references, release and delist the prison. On completion, the prison lock
2405 * and the allprison lock are both unlocked.
2408 prison_deref(struct prison *pr, int flags)
2410 struct prison *ppr, *tpr;
2413 if (!(flags & PD_LOCKED))
2414 mtx_lock(&pr->pr_mtx);
2415 /* Decrement the user references in a separate loop. */
2416 if (flags & PD_DEUREF) {
2417 for (tpr = pr;; tpr = tpr->pr_parent) {
2419 mtx_lock(&tpr->pr_mtx);
2420 if (--tpr->pr_uref > 0)
2422 KASSERT(tpr != &prison0, ("prison0 pr_uref=0"));
2423 mtx_unlock(&tpr->pr_mtx);
2425 /* Done if there were only user references to remove. */
2426 if (!(flags & PD_DEREF)) {
2427 mtx_unlock(&tpr->pr_mtx);
2428 if (flags & PD_LIST_SLOCKED)
2429 sx_sunlock(&allprison_lock);
2430 else if (flags & PD_LIST_XLOCKED)
2431 sx_xunlock(&allprison_lock);
2435 mtx_unlock(&tpr->pr_mtx);
2436 mtx_lock(&pr->pr_mtx);
2441 if (flags & PD_DEREF)
2443 /* If the prison still has references, nothing else to do. */
2444 if (pr->pr_ref > 0) {
2445 mtx_unlock(&pr->pr_mtx);
2446 if (flags & PD_LIST_SLOCKED)
2447 sx_sunlock(&allprison_lock);
2448 else if (flags & PD_LIST_XLOCKED)
2449 sx_xunlock(&allprison_lock);
2453 mtx_unlock(&pr->pr_mtx);
2454 if (flags & PD_LIST_SLOCKED) {
2455 if (!sx_try_upgrade(&allprison_lock)) {
2456 sx_sunlock(&allprison_lock);
2457 sx_xlock(&allprison_lock);
2459 } else if (!(flags & PD_LIST_XLOCKED))
2460 sx_xlock(&allprison_lock);
2462 TAILQ_REMOVE(&allprison, pr, pr_list);
2463 LIST_REMOVE(pr, pr_sibling);
2464 ppr = pr->pr_parent;
2465 for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent)
2466 tpr->pr_childcount--;
2467 sx_xunlock(&allprison_lock);
2470 if (pr->pr_vnet != ppr->pr_vnet)
2471 vnet_destroy(pr->pr_vnet);
2473 if (pr->pr_root != NULL) {
2474 vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount);
2476 VFS_UNLOCK_GIANT(vfslocked);
2478 mtx_destroy(&pr->pr_mtx);
2480 free(pr->pr_ip4, M_PRISON);
2483 free(pr->pr_ip6, M_PRISON);
2485 if (pr->pr_cpuset != NULL)
2486 cpuset_rel(pr->pr_cpuset);
2490 /* Removing a prison frees a reference on its parent. */
2492 mtx_lock(&pr->pr_mtx);
2498 prison_hold_locked(struct prison *pr)
2501 mtx_assert(&pr->pr_mtx, MA_OWNED);
2502 KASSERT(pr->pr_ref > 0,
2503 ("Trying to hold dead prison (jid=%d).", pr->pr_id));
2508 prison_hold(struct prison *pr)
2511 mtx_lock(&pr->pr_mtx);
2512 prison_hold_locked(pr);
2513 mtx_unlock(&pr->pr_mtx);
2517 prison_proc_hold(struct prison *pr)
2520 mtx_lock(&pr->pr_mtx);
2521 KASSERT(pr->pr_uref > 0,
2522 ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id));
2524 mtx_unlock(&pr->pr_mtx);
2528 prison_proc_free(struct prison *pr)
2531 mtx_lock(&pr->pr_mtx);
2532 KASSERT(pr->pr_uref > 0,
2533 ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id));
2534 prison_deref(pr, PD_DEUREF | PD_LOCKED);
2540 * Restrict a prison's IP address list with its parent's, possibly replacing
2541 * it. Return true if the replacement buffer was used (or would have been).
2544 prison_restrict_ip4(struct prison *pr, struct in_addr *newip4)
2549 ppr = pr->pr_parent;
2550 if (!(pr->pr_flags & PR_IP4_USER)) {
2551 /* This has no user settings, so just copy the parent's list. */
2552 if (pr->pr_ip4s < ppr->pr_ip4s) {
2554 * There's no room for the parent's list. Use the
2555 * new list buffer, which is assumed to be big enough
2556 * (if it was passed). If there's no buffer, try to
2560 if (newip4 == NULL) {
2561 newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4),
2562 M_PRISON, M_NOWAIT);
2566 if (newip4 != NULL) {
2567 bcopy(ppr->pr_ip4, newip4,
2568 ppr->pr_ip4s * sizeof(*newip4));
2569 free(pr->pr_ip4, M_PRISON);
2570 pr->pr_ip4 = newip4;
2571 pr->pr_ip4s = ppr->pr_ip4s;
2575 pr->pr_ip4s = ppr->pr_ip4s;
2576 if (pr->pr_ip4s > 0)
2577 bcopy(ppr->pr_ip4, pr->pr_ip4,
2578 pr->pr_ip4s * sizeof(*newip4));
2579 else if (pr->pr_ip4 != NULL) {
2580 free(pr->pr_ip4, M_PRISON);
2583 } else if (pr->pr_ip4s > 0) {
2584 /* Remove addresses that aren't in the parent. */
2585 for (ij = 0; ij < ppr->pr_ip4s; ij++)
2586 if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr)
2588 if (ij < ppr->pr_ip4s)
2591 bcopy(pr->pr_ip4 + 1, pr->pr_ip4,
2592 --pr->pr_ip4s * sizeof(*pr->pr_ip4));
2595 for (ij = 1; ii < pr->pr_ip4s; ) {
2596 if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) {
2600 switch (ij >= ppr->pr_ip4s ? -1 :
2601 qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) {
2603 bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii,
2604 (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4));
2615 if (pr->pr_ip4s == 0) {
2616 pr->pr_flags |= PR_IP4_DISABLE;
2617 free(pr->pr_ip4, M_PRISON);
2625 * Pass back primary IPv4 address of this jail.
2627 * If not restricted return success but do not alter the address. Caller has
2628 * to make sure to initialize it correctly (e.g. INADDR_ANY).
2630 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2631 * Address returned in NBO.
2634 prison_get_ip4(struct ucred *cred, struct in_addr *ia)
2638 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2639 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2641 pr = cred->cr_prison;
2642 if (!(pr->pr_flags & PR_IP4))
2644 mtx_lock(&pr->pr_mtx);
2645 if (!(pr->pr_flags & PR_IP4)) {
2646 mtx_unlock(&pr->pr_mtx);
2649 if (pr->pr_ip4 == NULL) {
2650 mtx_unlock(&pr->pr_mtx);
2651 return (EAFNOSUPPORT);
2654 ia->s_addr = pr->pr_ip4[0].s_addr;
2655 mtx_unlock(&pr->pr_mtx);
2660 * Return true if pr1 and pr2 have the same IPv4 address restrictions.
2663 prison_equal_ip4(struct prison *pr1, struct prison *pr2)
2670 * No need to lock since the PR_IP4_USER flag can't be altered for
2673 while (pr1 != &prison0 &&
2675 !(pr1->pr_flags & PR_VNET) &&
2677 !(pr1->pr_flags & PR_IP4_USER))
2678 pr1 = pr1->pr_parent;
2679 while (pr2 != &prison0 &&
2681 !(pr2->pr_flags & PR_VNET) &&
2683 !(pr2->pr_flags & PR_IP4_USER))
2684 pr2 = pr2->pr_parent;
2685 return (pr1 == pr2);
2689 * Make sure our (source) address is set to something meaningful to this
2692 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2693 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2694 * doesn't allow IPv4. Address passed in in NBO and returned in NBO.
2697 prison_local_ip4(struct ucred *cred, struct in_addr *ia)
2703 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2704 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2706 pr = cred->cr_prison;
2707 if (!(pr->pr_flags & PR_IP4))
2709 mtx_lock(&pr->pr_mtx);
2710 if (!(pr->pr_flags & PR_IP4)) {
2711 mtx_unlock(&pr->pr_mtx);
2714 if (pr->pr_ip4 == NULL) {
2715 mtx_unlock(&pr->pr_mtx);
2716 return (EAFNOSUPPORT);
2719 ia0.s_addr = ntohl(ia->s_addr);
2720 if (ia0.s_addr == INADDR_LOOPBACK) {
2721 ia->s_addr = pr->pr_ip4[0].s_addr;
2722 mtx_unlock(&pr->pr_mtx);
2726 if (ia0.s_addr == INADDR_ANY) {
2728 * In case there is only 1 IPv4 address, bind directly.
2730 if (pr->pr_ip4s == 1)
2731 ia->s_addr = pr->pr_ip4[0].s_addr;
2732 mtx_unlock(&pr->pr_mtx);
2736 error = _prison_check_ip4(pr, ia);
2737 mtx_unlock(&pr->pr_mtx);
2742 * Rewrite destination address in case we will connect to loopback address.
2744 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4.
2745 * Address passed in in NBO and returned in NBO.
2748 prison_remote_ip4(struct ucred *cred, struct in_addr *ia)
2752 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2753 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2755 pr = cred->cr_prison;
2756 if (!(pr->pr_flags & PR_IP4))
2758 mtx_lock(&pr->pr_mtx);
2759 if (!(pr->pr_flags & PR_IP4)) {
2760 mtx_unlock(&pr->pr_mtx);
2763 if (pr->pr_ip4 == NULL) {
2764 mtx_unlock(&pr->pr_mtx);
2765 return (EAFNOSUPPORT);
2768 if (ntohl(ia->s_addr) == INADDR_LOOPBACK) {
2769 ia->s_addr = pr->pr_ip4[0].s_addr;
2770 mtx_unlock(&pr->pr_mtx);
2775 * Return success because nothing had to be changed.
2777 mtx_unlock(&pr->pr_mtx);
2782 * Check if given address belongs to the jail referenced by cred/prison.
2784 * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail,
2785 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2786 * doesn't allow IPv4. Address passed in in NBO.
2789 _prison_check_ip4(struct prison *pr, struct in_addr *ia)
2794 * Check the primary IP.
2796 if (pr->pr_ip4[0].s_addr == ia->s_addr)
2800 * All the other IPs are sorted so we can do a binary search.
2803 z = pr->pr_ip4s - 2;
2806 d = qcmp_v4(&pr->pr_ip4[i+1], ia);
2815 return (EADDRNOTAVAIL);
2819 prison_check_ip4(struct ucred *cred, struct in_addr *ia)
2824 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2825 KASSERT(ia != NULL, ("%s: ia is NULL", __func__));
2827 pr = cred->cr_prison;
2828 if (!(pr->pr_flags & PR_IP4))
2830 mtx_lock(&pr->pr_mtx);
2831 if (!(pr->pr_flags & PR_IP4)) {
2832 mtx_unlock(&pr->pr_mtx);
2835 if (pr->pr_ip4 == NULL) {
2836 mtx_unlock(&pr->pr_mtx);
2837 return (EAFNOSUPPORT);
2840 error = _prison_check_ip4(pr, ia);
2841 mtx_unlock(&pr->pr_mtx);
2848 prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6)
2853 ppr = pr->pr_parent;
2854 if (!(pr->pr_flags & PR_IP6_USER)) {
2855 /* This has no user settings, so just copy the parent's list. */
2856 if (pr->pr_ip6s < ppr->pr_ip6s) {
2858 * There's no room for the parent's list. Use the
2859 * new list buffer, which is assumed to be big enough
2860 * (if it was passed). If there's no buffer, try to
2864 if (newip6 == NULL) {
2865 newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6),
2866 M_PRISON, M_NOWAIT);
2870 if (newip6 != NULL) {
2871 bcopy(ppr->pr_ip6, newip6,
2872 ppr->pr_ip6s * sizeof(*newip6));
2873 free(pr->pr_ip6, M_PRISON);
2874 pr->pr_ip6 = newip6;
2875 pr->pr_ip6s = ppr->pr_ip6s;
2879 pr->pr_ip6s = ppr->pr_ip6s;
2880 if (pr->pr_ip6s > 0)
2881 bcopy(ppr->pr_ip6, pr->pr_ip6,
2882 pr->pr_ip6s * sizeof(*newip6));
2883 else if (pr->pr_ip6 != NULL) {
2884 free(pr->pr_ip6, M_PRISON);
2887 } else if (pr->pr_ip6s > 0) {
2888 /* Remove addresses that aren't in the parent. */
2889 for (ij = 0; ij < ppr->pr_ip6s; ij++)
2890 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0],
2893 if (ij < ppr->pr_ip6s)
2896 bcopy(pr->pr_ip6 + 1, pr->pr_ip6,
2897 --pr->pr_ip6s * sizeof(*pr->pr_ip6));
2900 for (ij = 1; ii < pr->pr_ip6s; ) {
2901 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii],
2906 switch (ij >= ppr->pr_ip4s ? -1 :
2907 qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) {
2909 bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii,
2910 (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6));
2921 if (pr->pr_ip6s == 0) {
2922 pr->pr_flags |= PR_IP6_DISABLE;
2923 free(pr->pr_ip6, M_PRISON);
2931 * Pass back primary IPv6 address for this jail.
2933 * If not restricted return success but do not alter the address. Caller has
2934 * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT).
2936 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
2939 prison_get_ip6(struct ucred *cred, struct in6_addr *ia6)
2943 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
2944 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
2946 pr = cred->cr_prison;
2947 if (!(pr->pr_flags & PR_IP6))
2949 mtx_lock(&pr->pr_mtx);
2950 if (!(pr->pr_flags & PR_IP6)) {
2951 mtx_unlock(&pr->pr_mtx);
2954 if (pr->pr_ip6 == NULL) {
2955 mtx_unlock(&pr->pr_mtx);
2956 return (EAFNOSUPPORT);
2959 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
2960 mtx_unlock(&pr->pr_mtx);
2965 * Return true if pr1 and pr2 have the same IPv6 address restrictions.
2968 prison_equal_ip6(struct prison *pr1, struct prison *pr2)
2974 while (pr1 != &prison0 &&
2976 !(pr1->pr_flags & PR_VNET) &&
2978 !(pr1->pr_flags & PR_IP6_USER))
2979 pr1 = pr1->pr_parent;
2980 while (pr2 != &prison0 &&
2982 !(pr2->pr_flags & PR_VNET) &&
2984 !(pr2->pr_flags & PR_IP6_USER))
2985 pr2 = pr2->pr_parent;
2986 return (pr1 == pr2);
2990 * Make sure our (source) address is set to something meaningful to this jail.
2992 * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0)
2993 * when needed while binding.
2995 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
2996 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
2997 * doesn't allow IPv6.
3000 prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only)
3005 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3006 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3008 pr = cred->cr_prison;
3009 if (!(pr->pr_flags & PR_IP6))
3011 mtx_lock(&pr->pr_mtx);
3012 if (!(pr->pr_flags & PR_IP6)) {
3013 mtx_unlock(&pr->pr_mtx);
3016 if (pr->pr_ip6 == NULL) {
3017 mtx_unlock(&pr->pr_mtx);
3018 return (EAFNOSUPPORT);
3021 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3022 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3023 mtx_unlock(&pr->pr_mtx);
3027 if (IN6_IS_ADDR_UNSPECIFIED(ia6)) {
3029 * In case there is only 1 IPv6 address, and v6only is true,
3030 * then bind directly.
3032 if (v6only != 0 && pr->pr_ip6s == 1)
3033 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3034 mtx_unlock(&pr->pr_mtx);
3038 error = _prison_check_ip6(pr, ia6);
3039 mtx_unlock(&pr->pr_mtx);
3044 * Rewrite destination address in case we will connect to loopback address.
3046 * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6.
3049 prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6)
3053 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3054 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3056 pr = cred->cr_prison;
3057 if (!(pr->pr_flags & PR_IP6))
3059 mtx_lock(&pr->pr_mtx);
3060 if (!(pr->pr_flags & PR_IP6)) {
3061 mtx_unlock(&pr->pr_mtx);
3064 if (pr->pr_ip6 == NULL) {
3065 mtx_unlock(&pr->pr_mtx);
3066 return (EAFNOSUPPORT);
3069 if (IN6_IS_ADDR_LOOPBACK(ia6)) {
3070 bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr));
3071 mtx_unlock(&pr->pr_mtx);
3076 * Return success because nothing had to be changed.
3078 mtx_unlock(&pr->pr_mtx);
3083 * Check if given address belongs to the jail referenced by cred/prison.
3085 * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail,
3086 * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail
3087 * doesn't allow IPv6.
3090 _prison_check_ip6(struct prison *pr, struct in6_addr *ia6)
3095 * Check the primary IP.
3097 if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6))
3101 * All the other IPs are sorted so we can do a binary search.
3104 z = pr->pr_ip6s - 2;
3107 d = qcmp_v6(&pr->pr_ip6[i+1], ia6);
3116 return (EADDRNOTAVAIL);
3120 prison_check_ip6(struct ucred *cred, struct in6_addr *ia6)
3125 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3126 KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__));
3128 pr = cred->cr_prison;
3129 if (!(pr->pr_flags & PR_IP6))
3131 mtx_lock(&pr->pr_mtx);
3132 if (!(pr->pr_flags & PR_IP6)) {
3133 mtx_unlock(&pr->pr_mtx);
3136 if (pr->pr_ip6 == NULL) {
3137 mtx_unlock(&pr->pr_mtx);
3138 return (EAFNOSUPPORT);
3141 error = _prison_check_ip6(pr, ia6);
3142 mtx_unlock(&pr->pr_mtx);
3148 * Check if a jail supports the given address family.
3150 * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT
3154 prison_check_af(struct ucred *cred, int af)
3159 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3161 pr = cred->cr_prison;
3163 /* Prisons with their own network stack are not limited. */
3164 if (pr->pr_flags & PR_VNET)
3173 if (pr->pr_flags & PR_IP4)
3175 mtx_lock(&pr->pr_mtx);
3176 if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL)
3177 error = EAFNOSUPPORT;
3178 mtx_unlock(&pr->pr_mtx);
3184 if (pr->pr_flags & PR_IP6)
3186 mtx_lock(&pr->pr_mtx);
3187 if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL)
3188 error = EAFNOSUPPORT;
3189 mtx_unlock(&pr->pr_mtx);
3197 if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF))
3198 error = EAFNOSUPPORT;
3204 * Check if given address belongs to the jail referenced by cred (wrapper to
3205 * prison_check_ip[46]).
3207 * Returns 0 if jail doesn't restrict the address family or if address belongs
3208 * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if
3209 * the jail doesn't allow the address family. IPv4 Address passed in in NBO.
3212 prison_if(struct ucred *cred, struct sockaddr *sa)
3215 struct sockaddr_in *sai;
3218 struct sockaddr_in6 *sai6;
3222 KASSERT(cred != NULL, ("%s: cred is NULL", __func__));
3223 KASSERT(sa != NULL, ("%s: sa is NULL", __func__));
3226 switch (sa->sa_family)
3230 sai = (struct sockaddr_in *)sa;
3231 error = prison_check_ip4(cred, &sai->sin_addr);
3236 sai6 = (struct sockaddr_in6 *)sa;
3237 error = prison_check_ip6(cred, &sai6->sin6_addr);
3241 if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF))
3242 error = EAFNOSUPPORT;
3248 * Return 0 if jails permit p1 to frob p2, otherwise ESRCH.
3251 prison_check(struct ucred *cred1, struct ucred *cred2)
3254 return ((cred1->cr_prison == cred2->cr_prison ||
3255 prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH);
3259 * Return 1 if p2 is a child of p1, otherwise 0.
3262 prison_ischild(struct prison *pr1, struct prison *pr2)
3265 for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent)
3272 * Return 1 if the passed credential is in a jail, otherwise 0.
3275 jailed(struct ucred *cred)
3278 return (cred->cr_prison != &prison0);
3282 * Return the correct hostname (domainname, et al) for the passed credential.
3285 getcredhostname(struct ucred *cred, char *buf, size_t size)
3290 * A NULL credential can be used to shortcut to the physical
3291 * system's hostname.
3293 pr = (cred != NULL) ? cred->cr_prison : &prison0;
3294 mtx_lock(&pr->pr_mtx);
3295 strlcpy(buf, pr->pr_hostname, size);
3296 mtx_unlock(&pr->pr_mtx);
3300 getcreddomainname(struct ucred *cred, char *buf, size_t size)
3303 mtx_lock(&cred->cr_prison->pr_mtx);
3304 strlcpy(buf, cred->cr_prison->pr_domainname, size);
3305 mtx_unlock(&cred->cr_prison->pr_mtx);
3309 getcredhostuuid(struct ucred *cred, char *buf, size_t size)
3312 mtx_lock(&cred->cr_prison->pr_mtx);
3313 strlcpy(buf, cred->cr_prison->pr_hostuuid, size);
3314 mtx_unlock(&cred->cr_prison->pr_mtx);
3318 getcredhostid(struct ucred *cred, unsigned long *hostid)
3321 mtx_lock(&cred->cr_prison->pr_mtx);
3322 *hostid = cred->cr_prison->pr_hostid;
3323 mtx_unlock(&cred->cr_prison->pr_mtx);
3328 * Determine whether the prison represented by cred owns
3329 * its vnet rather than having it inherited.
3331 * Returns 1 in case the prison owns the vnet, 0 otherwise.
3334 prison_owns_vnet(struct ucred *cred)
3338 * vnets cannot be added/removed after jail creation,
3339 * so no need to lock here.
3341 return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0);
3346 * Determine whether the subject represented by cred can "see"
3347 * status of a mount point.
3348 * Returns: 0 for permitted, ENOENT otherwise.
3349 * XXX: This function should be called cr_canseemount() and should be
3350 * placed in kern_prot.c.
3353 prison_canseemount(struct ucred *cred, struct mount *mp)
3359 pr = cred->cr_prison;
3360 if (pr->pr_enforce_statfs == 0)
3362 if (pr->pr_root->v_mount == mp)
3364 if (pr->pr_enforce_statfs == 2)
3367 * If jail's chroot directory is set to "/" we should be able to see
3368 * all mount-points from inside a jail.
3369 * This is ugly check, but this is the only situation when jail's
3370 * directory ends with '/'.
3372 if (strcmp(pr->pr_path, "/") == 0)
3374 len = strlen(pr->pr_path);
3376 if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0)
3379 * Be sure that we don't have situation where jail's root directory
3380 * is "/some/path" and mount point is "/some/pathpath".
3382 if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/')
3388 prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp)
3390 char jpath[MAXPATHLEN];
3394 pr = cred->cr_prison;
3395 if (pr->pr_enforce_statfs == 0)
3397 if (prison_canseemount(cred, mp) != 0) {
3398 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3399 strlcpy(sp->f_mntonname, "[restricted]",
3400 sizeof(sp->f_mntonname));
3403 if (pr->pr_root->v_mount == mp) {
3405 * Clear current buffer data, so we are sure nothing from
3406 * the valid path left there.
3408 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3409 *sp->f_mntonname = '/';
3413 * If jail's chroot directory is set to "/" we should be able to see
3414 * all mount-points from inside a jail.
3416 if (strcmp(pr->pr_path, "/") == 0)
3418 len = strlen(pr->pr_path);
3419 strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath));
3421 * Clear current buffer data, so we are sure nothing from
3422 * the valid path left there.
3424 bzero(sp->f_mntonname, sizeof(sp->f_mntonname));
3425 if (*jpath == '\0') {
3426 /* Should never happen. */
3427 *sp->f_mntonname = '/';
3429 strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname));
3434 * Check with permission for a specific privilege is granted within jail. We
3435 * have a specific list of accepted privileges; the rest are denied.
3438 prison_priv_check(struct ucred *cred, int priv)
3446 * Privileges specific to prisons with a virtual network stack.
3447 * There might be a duplicate entry here in case the privilege
3448 * is only granted conditionally in the legacy jail case.
3453 * NFS-specific privileges.
3455 case PRIV_NFS_DAEMON:
3456 case PRIV_NFS_LOCKD:
3459 * Network stack privileges.
3461 case PRIV_NET_BRIDGE:
3464 case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */
3465 case PRIV_NET_ROUTE:
3467 case PRIV_NET_SETIFMTU:
3468 case PRIV_NET_SETIFFLAGS:
3469 case PRIV_NET_SETIFCAP:
3470 case PRIV_NET_SETIFNAME :
3471 case PRIV_NET_SETIFMETRIC:
3472 case PRIV_NET_SETIFPHYS:
3473 case PRIV_NET_SETIFMAC:
3474 case PRIV_NET_ADDMULTI:
3475 case PRIV_NET_DELMULTI:
3476 case PRIV_NET_HWIOCTL:
3477 case PRIV_NET_SETLLADDR:
3478 case PRIV_NET_ADDIFGROUP:
3479 case PRIV_NET_DELIFGROUP:
3480 case PRIV_NET_IFCREATE:
3481 case PRIV_NET_IFDESTROY:
3482 case PRIV_NET_ADDIFADDR:
3483 case PRIV_NET_DELIFADDR:
3486 case PRIV_NET_SETIFVNET:
3489 * 802.11-related privileges.
3491 case PRIV_NET80211_GETKEY:
3493 case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */
3498 * AppleTalk privileges.
3500 case PRIV_NETATALK_RESERVEDPORT:
3505 case PRIV_NETATM_CFG:
3506 case PRIV_NETATM_ADD:
3507 case PRIV_NETATM_DEL:
3508 case PRIV_NETATM_SET:
3511 * Bluetooth privileges.
3513 case PRIV_NETBLUETOOTH_RAW:
3517 * Netgraph and netgraph module privileges.
3519 case PRIV_NETGRAPH_CONTROL:
3521 case PRIV_NETGRAPH_TTY:
3525 * IPv4 and IPv6 privileges.
3527 case PRIV_NETINET_IPFW:
3528 case PRIV_NETINET_DIVERT:
3529 case PRIV_NETINET_PF:
3530 case PRIV_NETINET_DUMMYNET:
3531 case PRIV_NETINET_CARP:
3532 case PRIV_NETINET_MROUTE:
3533 case PRIV_NETINET_RAW:
3534 case PRIV_NETINET_ADDRCTRL6:
3535 case PRIV_NETINET_ND6:
3536 case PRIV_NETINET_SCOPE6:
3537 case PRIV_NETINET_ALIFETIME6:
3538 case PRIV_NETINET_IPSEC:
3539 case PRIV_NETINET_BINDANY:
3543 * IPX/SPX privileges.
3545 case PRIV_NETIPX_RESERVEDPORT:
3546 case PRIV_NETIPX_RAW:
3560 * No default: or deny here.
3561 * In case of no permit fall through to next switch().
3563 if (cred->cr_prison->pr_flags & PR_VNET)
3571 * Allow ktrace privileges for root in jail.
3577 * Allow jailed processes to configure audit identity and
3578 * submit audit records (login, etc). In the future we may
3579 * want to further refine the relationship between audit and
3582 case PRIV_AUDIT_GETAUDIT:
3583 case PRIV_AUDIT_SETAUDIT:
3584 case PRIV_AUDIT_SUBMIT:
3588 * Allow jailed processes to manipulate process UNIX
3589 * credentials in any way they see fit.
3591 case PRIV_CRED_SETUID:
3592 case PRIV_CRED_SETEUID:
3593 case PRIV_CRED_SETGID:
3594 case PRIV_CRED_SETEGID:
3595 case PRIV_CRED_SETGROUPS:
3596 case PRIV_CRED_SETREUID:
3597 case PRIV_CRED_SETREGID:
3598 case PRIV_CRED_SETRESUID:
3599 case PRIV_CRED_SETRESGID:
3602 * Jail implements visibility constraints already, so allow
3603 * jailed root to override uid/gid-based constraints.
3605 case PRIV_SEEOTHERGIDS:
3606 case PRIV_SEEOTHERUIDS:
3609 * Jail implements inter-process debugging limits already, so
3610 * allow jailed root various debugging privileges.
3612 case PRIV_DEBUG_DIFFCRED:
3613 case PRIV_DEBUG_SUGID:
3614 case PRIV_DEBUG_UNPRIV:
3617 * Allow jail to set various resource limits and login
3618 * properties, and for now, exceed process resource limits.
3620 case PRIV_PROC_LIMIT:
3621 case PRIV_PROC_SETLOGIN:
3622 case PRIV_PROC_SETRLIMIT:
3625 * System V and POSIX IPC privileges are granted in jail.
3628 case PRIV_IPC_WRITE:
3629 case PRIV_IPC_ADMIN:
3630 case PRIV_IPC_MSGSIZE:
3634 * Jail operations within a jail work on child jails.
3636 case PRIV_JAIL_ATTACH:
3638 case PRIV_JAIL_REMOVE:
3641 * Jail implements its own inter-process limits, so allow
3642 * root processes in jail to change scheduling on other
3643 * processes in the same jail. Likewise for signalling.
3645 case PRIV_SCHED_DIFFCRED:
3646 case PRIV_SCHED_CPUSET:
3647 case PRIV_SIGNAL_DIFFCRED:
3648 case PRIV_SIGNAL_SUGID:
3651 * Allow jailed processes to write to sysctls marked as jail
3654 case PRIV_SYSCTL_WRITEJAIL:
3657 * Allow root in jail to manage a variety of quota
3658 * properties. These should likely be conditional on a
3659 * configuration option.
3661 case PRIV_VFS_GETQUOTA:
3662 case PRIV_VFS_SETQUOTA:
3665 * Since Jail relies on chroot() to implement file system
3666 * protections, grant many VFS privileges to root in jail.
3667 * Be careful to exclude mount-related and NFS-related
3671 case PRIV_VFS_WRITE:
3672 case PRIV_VFS_ADMIN:
3674 case PRIV_VFS_LOOKUP:
3675 case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */
3676 case PRIV_VFS_CHFLAGS_DEV:
3677 case PRIV_VFS_CHOWN:
3678 case PRIV_VFS_CHROOT:
3679 case PRIV_VFS_RETAINSUGID:
3680 case PRIV_VFS_FCHROOT:
3682 case PRIV_VFS_SETGID:
3684 case PRIV_VFS_STICKYFILE:
3688 * Depending on the global setting, allow privilege of
3689 * setting system flags.
3691 case PRIV_VFS_SYSFLAGS:
3692 if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS)
3698 * Depending on the global setting, allow privilege of
3699 * mounting/unmounting file systems.
3701 case PRIV_VFS_MOUNT:
3702 case PRIV_VFS_UNMOUNT:
3703 case PRIV_VFS_MOUNT_NONUSER:
3704 case PRIV_VFS_MOUNT_OWNER:
3705 if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT)
3711 * Allow jailed root to bind reserved ports and reuse in-use
3714 case PRIV_NETINET_RESERVEDPORT:
3715 case PRIV_NETINET_REUSEPORT:
3719 * Allow jailed root to set certian IPv4/6 (option) headers.
3721 case PRIV_NETINET_SETHDROPTS:
3725 * Conditionally allow creating raw sockets in jail.
3727 case PRIV_NETINET_RAW:
3728 if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS)
3734 * Since jail implements its own visibility limits on netstat
3735 * sysctls, allow getcred. This allows identd to work in
3738 case PRIV_NETINET_GETCRED:
3743 * In all remaining cases, deny the privilege request. This
3744 * includes almost all network privileges, many system
3745 * configuration privileges.
3752 * Return the part of pr2's name that is relative to pr1, or the whole name
3753 * if it does not directly follow.
3757 prison_name(struct prison *pr1, struct prison *pr2)
3761 /* Jails see themselves as "0" (if they see themselves at all). */
3764 name = pr2->pr_name;
3765 if (prison_ischild(pr1, pr2)) {
3767 * pr1 isn't locked (and allprison_lock may not be either)
3768 * so its length can't be counted on. But the number of dots
3769 * can be counted on - and counted.
3771 for (; pr1 != &prison0; pr1 = pr1->pr_parent)
3772 name = strchr(name, '.') + 1;
3778 * Return the part of pr2's path that is relative to pr1, or the whole path
3779 * if it does not directly follow.
3782 prison_path(struct prison *pr1, struct prison *pr2)
3784 char *path1, *path2;
3787 path1 = pr1->pr_path;
3788 path2 = pr2->pr_path;
3789 if (!strcmp(path1, "/"))
3791 len1 = strlen(path1);
3792 if (strncmp(path1, path2, len1))
3794 if (path2[len1] == '\0')
3796 if (path2[len1] == '/')
3797 return (path2 + len1);
3803 * Jail-related sysctls.
3805 SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0,
3809 sysctl_jail_list(SYSCTL_HANDLER_ARGS)
3812 struct prison *pr, *cpr;
3814 struct in_addr *ip4 = NULL;
3818 struct in_addr *ip6 = NULL;
3823 xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK);
3824 pr = req->td->td_ucred->cr_prison;
3826 sx_slock(&allprison_lock);
3827 FOREACH_PRISON_DESCENDANT(pr, cpr, descend) {
3828 #if defined(INET) || defined(INET6)
3831 mtx_lock(&cpr->pr_mtx);
3833 if (cpr->pr_ip4s > 0) {
3834 if (ip4s < cpr->pr_ip4s) {
3835 ip4s = cpr->pr_ip4s;
3836 mtx_unlock(&cpr->pr_mtx);
3837 ip4 = realloc(ip4, ip4s *
3838 sizeof(struct in_addr), M_TEMP, M_WAITOK);
3841 bcopy(cpr->pr_ip4, ip4,
3842 cpr->pr_ip4s * sizeof(struct in_addr));
3846 if (cpr->pr_ip6s > 0) {
3847 if (ip6s < cpr->pr_ip6s) {
3848 ip6s = cpr->pr_ip6s;
3849 mtx_unlock(&cpr->pr_mtx);
3850 ip6 = realloc(ip6, ip6s *
3851 sizeof(struct in6_addr), M_TEMP, M_WAITOK);
3854 bcopy(cpr->pr_ip6, ip6,
3855 cpr->pr_ip6s * sizeof(struct in6_addr));
3858 if (cpr->pr_ref == 0) {
3859 mtx_unlock(&cpr->pr_mtx);
3862 bzero(xp, sizeof(*xp));
3863 xp->pr_version = XPRISON_VERSION;
3864 xp->pr_id = cpr->pr_id;
3865 xp->pr_state = cpr->pr_uref > 0
3866 ? PRISON_STATE_ALIVE : PRISON_STATE_DYING;
3867 strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path));
3868 strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host));
3869 strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name));
3871 xp->pr_ip4s = cpr->pr_ip4s;
3874 xp->pr_ip6s = cpr->pr_ip6s;
3876 mtx_unlock(&cpr->pr_mtx);
3877 error = SYSCTL_OUT(req, xp, sizeof(*xp));
3881 if (xp->pr_ip4s > 0) {
3882 error = SYSCTL_OUT(req, ip4,
3883 xp->pr_ip4s * sizeof(struct in_addr));
3889 if (xp->pr_ip6s > 0) {
3890 error = SYSCTL_OUT(req, ip6,
3891 xp->pr_ip6s * sizeof(struct in6_addr));
3897 sx_sunlock(&allprison_lock);
3908 SYSCTL_OID(_security_jail, OID_AUTO, list,
3909 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3910 sysctl_jail_list, "S", "List of active jails");
3913 sysctl_jail_jailed(SYSCTL_HANDLER_ARGS)
3917 injail = jailed(req->td->td_ucred);
3918 error = SYSCTL_OUT(req, &injail, sizeof(injail));
3923 SYSCTL_PROC(_security_jail, OID_AUTO, jailed,
3924 CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
3925 sysctl_jail_jailed, "I", "Process in jail?");
3927 #if defined(INET) || defined(INET6)
3928 SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW,
3929 &jail_max_af_ips, 0,
3930 "Number of IP addresses a jail may have at most per address family");
3934 * Default parameters for jail(2) compatability. For historical reasons,
3935 * the sysctl names have varying similarity to the parameter names. Prisons
3936 * just see their own parameters, and can't change them.
3939 sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS)
3942 int allow, error, i;
3944 pr = req->td->td_ucred->cr_prison;
3945 allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow;
3947 /* Get the current flag value, and convert it to a boolean. */
3948 i = (allow & arg2) ? 1 : 0;
3951 error = sysctl_handle_int(oidp, &i, 0, req);
3952 if (error || !req->newptr)
3958 * The sysctls don't have CTLFLAGS_PRISON, so assume prison0
3961 mtx_lock(&prison0.pr_mtx);
3962 jail_default_allow = (jail_default_allow & ~arg2) | i;
3963 mtx_unlock(&prison0.pr_mtx);
3967 SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed,
3968 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3969 NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I",
3970 "Processes in jail can set their hostnames");
3971 SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only,
3972 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3973 (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I",
3974 "Processes in jail are limited to creating UNIX/IP/route sockets only");
3975 SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed,
3976 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3977 NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I",
3978 "Processes in jail can use System V IPC primitives");
3979 SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets,
3980 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3981 NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I",
3982 "Prison root can create raw sockets");
3983 SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed,
3984 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3985 NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I",
3986 "Processes in jail can alter system file flags");
3987 SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed,
3988 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
3989 NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I",
3990 "Processes in jail can mount/unmount jail-friendly file systems");
3993 sysctl_jail_default_level(SYSCTL_HANDLER_ARGS)
3998 pr = req->td->td_ucred->cr_prison;
3999 level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2);
4000 error = sysctl_handle_int(oidp, &level, 0, req);
4001 if (error || !req->newptr)
4003 *(int *)arg1 = level;
4007 SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs,
4008 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE,
4009 &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs),
4010 sysctl_jail_default_level, "I",
4011 "Processes in jail cannot see all mounted file systems");
4014 * Nodes to describe jail parameters. Maximum length of string parameters
4015 * is returned in the string itself, and the other parameters exist merely
4016 * to make themselves and their types known.
4018 SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0,
4022 sysctl_jail_param(SYSCTL_HANDLER_ARGS)
4029 switch (oidp->oid_kind & CTLTYPE)
4035 if (!(req->flags & SCTL_MASK32))
4037 return (SYSCTL_OUT(req, &l, sizeof(l)));
4041 return (SYSCTL_OUT(req, &i, sizeof(i)));
4042 case CTLTYPE_STRING:
4043 snprintf(numbuf, sizeof(numbuf), "%d", arg2);
4045 (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req));
4046 case CTLTYPE_STRUCT:
4048 return (SYSCTL_OUT(req, &s, sizeof(s)));
4053 SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID");
4054 SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID");
4055 SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name");
4056 SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path");
4057 SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW,
4058 "I", "Jail secure level");
4059 SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW,
4060 "I", "Jail cannot see all mounted file systems");
4061 SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW,
4062 "B", "Jail persistence");
4064 SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN,
4065 "E,jailsys", "Virtual network stack");
4067 SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD,
4068 "B", "Jail is in the process of shutting down");
4070 SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails");
4071 SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD,
4072 "I", "Current number of child jails");
4073 SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW,
4074 "I", "Maximum number of child jails");
4076 SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info");
4077 SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN,
4079 SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN,
4080 "Jail NIS domainname");
4081 SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN,
4083 SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW,
4084 "LU", "Jail host ID");
4086 SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset");
4087 SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID");
4090 SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN,
4091 "Jail IPv4 address virtualization");
4092 SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr),
4093 "S,in_addr,a", "Jail IPv4 addresses");
4096 SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN,
4097 "Jail IPv6 address virtualization");
4098 SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr),
4099 "S,in6_addr,a", "Jail IPv6 addresses");
4102 SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags");
4103 SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW,
4104 "B", "Jail may set hostname");
4105 SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW,
4106 "B", "Jail may use SYSV IPC");
4107 SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW,
4108 "B", "Jail may create raw sockets");
4109 SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW,
4110 "B", "Jail may alter system file flags");
4111 SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW,
4112 "B", "Jail may mount/unmount jail-friendly file systems");
4113 SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW,
4114 "B", "Jail may set file quotas");
4115 SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW,
4116 "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route");
4122 db_show_prison(struct prison *pr)
4125 #if defined(INET) || defined(INET6)
4130 char ip6buf[INET6_ADDRSTRLEN];
4133 db_printf("prison %p:\n", pr);
4134 db_printf(" jid = %d\n", pr->pr_id);
4135 db_printf(" name = %s\n", pr->pr_name);
4136 db_printf(" parent = %p\n", pr->pr_parent);
4137 db_printf(" ref = %d\n", pr->pr_ref);
4138 db_printf(" uref = %d\n", pr->pr_uref);
4139 db_printf(" path = %s\n", pr->pr_path);
4140 db_printf(" cpuset = %d\n", pr->pr_cpuset
4141 ? pr->pr_cpuset->cs_id : -1);
4143 db_printf(" vnet = %p\n", pr->pr_vnet);
4145 db_printf(" root = %p\n", pr->pr_root);
4146 db_printf(" securelevel = %d\n", pr->pr_securelevel);
4147 db_printf(" childcount = %d\n", pr->pr_childcount);
4148 db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children));
4149 db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling));
4150 db_printf(" flags = %x", pr->pr_flags);
4151 for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]);
4153 if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi)))
4154 db_printf(" %s", pr_flag_names[fi]);
4155 for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]);
4157 jsf = pr->pr_flags &
4158 (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new);
4159 db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name,
4160 pr_flag_jailsys[fi].disable &&
4161 (jsf == pr_flag_jailsys[fi].disable) ? "disable"
4162 : (jsf == pr_flag_jailsys[fi].new) ? "new"
4165 db_printf(" allow = %x", pr->pr_allow);
4166 for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]);
4168 if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi)))
4169 db_printf(" %s", pr_allow_names[fi]);
4171 db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs);
4172 db_printf(" host.hostname = %s\n", pr->pr_hostname);
4173 db_printf(" host.domainname = %s\n", pr->pr_domainname);
4174 db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid);
4175 db_printf(" host.hostid = %lu\n", pr->pr_hostid);
4177 db_printf(" ip4s = %d\n", pr->pr_ip4s);
4178 for (ii = 0; ii < pr->pr_ip4s; ii++)
4179 db_printf(" %s %s\n",
4180 ii == 0 ? "ip4 =" : " ",
4181 inet_ntoa(pr->pr_ip4[ii]));
4184 db_printf(" ip6s = %d\n", pr->pr_ip6s);
4185 for (ii = 0; ii < pr->pr_ip6s; ii++)
4186 db_printf(" %s %s\n",
4187 ii == 0 ? "ip6 =" : " ",
4188 ip6_sprintf(ip6buf, &pr->pr_ip6[ii]));
4192 DB_SHOW_COMMAND(prison, db_show_prison_command)
4198 * Show all prisons in the list, and prison0 which is not
4201 db_show_prison(&prison0);
4202 if (!db_pager_quit) {
4203 TAILQ_FOREACH(pr, &allprison, pr_list) {
4215 /* Look for a prison with the ID and with references. */
4216 TAILQ_FOREACH(pr, &allprison, pr_list)
4217 if (pr->pr_id == addr && pr->pr_ref > 0)
4220 /* Look again, without requiring a reference. */
4221 TAILQ_FOREACH(pr, &allprison, pr_list)
4222 if (pr->pr_id == addr)
4225 /* Assume address points to a valid prison. */
4226 pr = (struct prison *)addr;