19454b2d8SWarner Losh /*- 2413628a7SBjoern A. Zeeb * Copyright (c) 1999 Poul-Henning Kamp. 3413628a7SBjoern A. Zeeb * Copyright (c) 2008 Bjoern A. Zeeb. 4b38ff370SJamie Gritton * Copyright (c) 2009 James Gritton. 5413628a7SBjoern A. Zeeb * All rights reserved. 6b9f0b66cSBjoern A. Zeeb * 7b9f0b66cSBjoern A. Zeeb * Redistribution and use in source and binary forms, with or without 8b9f0b66cSBjoern A. Zeeb * modification, are permitted provided that the following conditions 9b9f0b66cSBjoern A. Zeeb * are met: 10b9f0b66cSBjoern A. Zeeb * 1. Redistributions of source code must retain the above copyright 11b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer. 12b9f0b66cSBjoern A. Zeeb * 2. Redistributions in binary form must reproduce the above copyright 13b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer in the 14b9f0b66cSBjoern A. Zeeb * documentation and/or other materials provided with the distribution. 15b9f0b66cSBjoern A. Zeeb * 16b9f0b66cSBjoern A. Zeeb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17b9f0b66cSBjoern A. Zeeb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18b9f0b66cSBjoern A. Zeeb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19b9f0b66cSBjoern A. Zeeb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20b9f0b66cSBjoern A. Zeeb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21b9f0b66cSBjoern A. Zeeb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22b9f0b66cSBjoern A. Zeeb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23b9f0b66cSBjoern A. Zeeb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24b9f0b66cSBjoern A. Zeeb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25b9f0b66cSBjoern A. Zeeb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26b9f0b66cSBjoern A. Zeeb * SUCH DAMAGE. 2707901f22SPoul-Henning Kamp */ 2875c13541SPoul-Henning Kamp 29677b542eSDavid E. O'Brien #include <sys/cdefs.h> 30677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 31677b542eSDavid E. O'Brien 3276ca6f88SJamie Gritton #include "opt_compat.h" 33413628a7SBjoern A. Zeeb #include "opt_ddb.h" 34413628a7SBjoern A. Zeeb #include "opt_inet.h" 35413628a7SBjoern A. Zeeb #include "opt_inet6.h" 3646e3b1cbSPawel Jakub Dawidek 3775c13541SPoul-Henning Kamp #include <sys/param.h> 3875c13541SPoul-Henning Kamp #include <sys/types.h> 3975c13541SPoul-Henning Kamp #include <sys/kernel.h> 4075c13541SPoul-Henning Kamp #include <sys/systm.h> 4175c13541SPoul-Henning Kamp #include <sys/errno.h> 4275c13541SPoul-Henning Kamp #include <sys/sysproto.h> 4375c13541SPoul-Henning Kamp #include <sys/malloc.h> 440304c731SJamie Gritton #include <sys/osd.h> 45800c9408SRobert Watson #include <sys/priv.h> 4675c13541SPoul-Henning Kamp #include <sys/proc.h> 47b3059e09SRobert Watson #include <sys/taskqueue.h> 4857b4252eSKonstantin Belousov #include <sys/fcntl.h> 4975c13541SPoul-Henning Kamp #include <sys/jail.h> 5001137630SRobert Watson #include <sys/lock.h> 5101137630SRobert Watson #include <sys/mutex.h> 52dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5376ca6f88SJamie Gritton #include <sys/sysent.h> 54fd7a8150SMike Barcroft #include <sys/namei.h> 55820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 56fd7a8150SMike Barcroft #include <sys/queue.h> 5775c13541SPoul-Henning Kamp #include <sys/socket.h> 58fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 5983f1e257SRobert Watson #include <sys/sysctl.h> 60fd7a8150SMike Barcroft #include <sys/vnode.h> 61530c0060SRobert Watson 6275c13541SPoul-Henning Kamp #include <net/if.h> 63530c0060SRobert Watson #include <net/vnet.h> 64530c0060SRobert Watson 6575c13541SPoul-Henning Kamp #include <netinet/in.h> 66530c0060SRobert Watson 67413628a7SBjoern A. Zeeb #ifdef DDB 68413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 69413628a7SBjoern A. Zeeb #ifdef INET6 70413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h> 71413628a7SBjoern A. Zeeb #endif /* INET6 */ 72413628a7SBjoern A. Zeeb #endif /* DDB */ 7375c13541SPoul-Henning Kamp 74aed55708SRobert Watson #include <security/mac/mac_framework.h> 75aed55708SRobert Watson 768986e3a0SJamie Gritton #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 778986e3a0SJamie Gritton 7875c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 7975c13541SPoul-Henning Kamp 800304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 810304c731SJamie Gritton struct prison prison0 = { 820304c731SJamie Gritton .pr_id = 0, 830304c731SJamie Gritton .pr_name = "0", 840304c731SJamie Gritton .pr_ref = 1, 850304c731SJamie Gritton .pr_uref = 1, 860304c731SJamie Gritton .pr_path = "/", 870304c731SJamie Gritton .pr_securelevel = -1, 88b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 898986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 900304c731SJamie Gritton .pr_children = LIST_HEAD_INITIALIZER(&prison0.pr_children), 91eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 92eb79e1c7SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET, 93eb79e1c7SBjoern A. Zeeb #else 9476ca6f88SJamie Gritton .pr_flags = PR_HOST, 95eb79e1c7SBjoern A. Zeeb #endif 960304c731SJamie Gritton .pr_allow = PR_ALLOW_ALL, 970304c731SJamie Gritton }; 980304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 9983f1e257SRobert Watson 1000304c731SJamie Gritton /* allprison and lastprid are protected by allprison_lock. */ 101dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 102b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 103b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 1042110d913SXin LI int lastprid = 0; 105fd7a8150SMike Barcroft 106b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 107b3059e09SRobert Watson static void prison_complete(void *context, int pending); 108b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1090304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1100304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 111413628a7SBjoern A. Zeeb #ifdef INET 1128571af59SJamie Gritton static int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 1130304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 114413628a7SBjoern A. Zeeb #endif 115413628a7SBjoern A. Zeeb #ifdef INET6 1168571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 1170304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 118413628a7SBjoern A. Zeeb #endif 119fd7a8150SMike Barcroft 120b38ff370SJamie Gritton /* Flags for prison_deref */ 121b38ff370SJamie Gritton #define PD_DEREF 0x01 122b38ff370SJamie Gritton #define PD_DEUREF 0x02 123b38ff370SJamie Gritton #define PD_LOCKED 0x04 124b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 125b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 126fd7a8150SMike Barcroft 1270304c731SJamie Gritton /* 1280304c731SJamie Gritton * Parameter names corresponding to PR_* flag values 1290304c731SJamie Gritton */ 1300304c731SJamie Gritton static char *pr_flag_names[] = { 1310304c731SJamie Gritton [0] = "persist", 1320304c731SJamie Gritton }; 1330304c731SJamie Gritton 1340304c731SJamie Gritton static char *pr_flag_nonames[] = { 1350304c731SJamie Gritton [0] = "nopersist", 1367cbf7213SJamie Gritton }; 1377cbf7213SJamie Gritton 1387cbf7213SJamie Gritton struct jailsys_flags { 1397cbf7213SJamie Gritton const char *name; 1407cbf7213SJamie Gritton unsigned disable; 1417cbf7213SJamie Gritton unsigned new; 1427cbf7213SJamie Gritton } pr_flag_jailsys[] = { 1437cbf7213SJamie Gritton { "host", 0, PR_HOST }, 1447cbf7213SJamie Gritton #ifdef VIMAGE 1457cbf7213SJamie Gritton { "vnet", 0, PR_VNET }, 1467cbf7213SJamie Gritton #endif 1470304c731SJamie Gritton #ifdef INET 1487cbf7213SJamie Gritton { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 1490304c731SJamie Gritton #endif 1500304c731SJamie Gritton #ifdef INET6 1517cbf7213SJamie Gritton { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 152679e1390SJamie Gritton #endif 1530304c731SJamie Gritton }; 1540304c731SJamie Gritton 1550304c731SJamie Gritton static char *pr_allow_names[] = { 1560304c731SJamie Gritton "allow.set_hostname", 1570304c731SJamie Gritton "allow.sysvipc", 1580304c731SJamie Gritton "allow.raw_sockets", 1590304c731SJamie Gritton "allow.chflags", 1600304c731SJamie Gritton "allow.mount", 1610304c731SJamie Gritton "allow.quotas", 1620304c731SJamie Gritton "allow.socket_af", 1630304c731SJamie Gritton }; 1640304c731SJamie Gritton 1650304c731SJamie Gritton static char *pr_allow_nonames[] = { 1660304c731SJamie Gritton "allow.noset_hostname", 1670304c731SJamie Gritton "allow.nosysvipc", 1680304c731SJamie Gritton "allow.noraw_sockets", 1690304c731SJamie Gritton "allow.nochflags", 1700304c731SJamie Gritton "allow.nomount", 1710304c731SJamie Gritton "allow.noquotas", 1720304c731SJamie Gritton "allow.nosocket_af", 1730304c731SJamie Gritton }; 1740304c731SJamie Gritton 1750304c731SJamie Gritton #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 17642bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 1770304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 17842bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1790304c731SJamie Gritton #if defined(INET) || defined(INET6) 180e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 1810304c731SJamie Gritton #endif 1820304c731SJamie Gritton 183413628a7SBjoern A. Zeeb #ifdef INET 184413628a7SBjoern A. Zeeb static int 185413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2) 186413628a7SBjoern A. Zeeb { 187413628a7SBjoern A. Zeeb in_addr_t iaa, iab; 188413628a7SBjoern A. Zeeb 189413628a7SBjoern A. Zeeb /* 190413628a7SBjoern A. Zeeb * We need to compare in HBO here to get the list sorted as expected 191413628a7SBjoern A. Zeeb * by the result of the code. Sorting NBO addresses gives you 192413628a7SBjoern A. Zeeb * interesting results. If you do not understand, do not try. 193413628a7SBjoern A. Zeeb */ 194413628a7SBjoern A. Zeeb iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 195413628a7SBjoern A. Zeeb iab = ntohl(((const struct in_addr *)ip2)->s_addr); 196413628a7SBjoern A. Zeeb 197413628a7SBjoern A. Zeeb /* 198413628a7SBjoern A. Zeeb * Do not simply return the difference of the two numbers, the int is 199413628a7SBjoern A. Zeeb * not wide enough. 200413628a7SBjoern A. Zeeb */ 201413628a7SBjoern A. Zeeb if (iaa > iab) 202413628a7SBjoern A. Zeeb return (1); 203413628a7SBjoern A. Zeeb else if (iaa < iab) 204413628a7SBjoern A. Zeeb return (-1); 205413628a7SBjoern A. Zeeb else 206413628a7SBjoern A. Zeeb return (0); 207413628a7SBjoern A. Zeeb } 208413628a7SBjoern A. Zeeb #endif 209413628a7SBjoern A. Zeeb 210413628a7SBjoern A. Zeeb #ifdef INET6 211413628a7SBjoern A. Zeeb static int 212413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2) 213413628a7SBjoern A. Zeeb { 214413628a7SBjoern A. Zeeb const struct in6_addr *ia6a, *ia6b; 215413628a7SBjoern A. Zeeb int i, rc; 216413628a7SBjoern A. Zeeb 217413628a7SBjoern A. Zeeb ia6a = (const struct in6_addr *)ip1; 218413628a7SBjoern A. Zeeb ia6b = (const struct in6_addr *)ip2; 219413628a7SBjoern A. Zeeb 220413628a7SBjoern A. Zeeb rc = 0; 221413628a7SBjoern A. Zeeb for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 222413628a7SBjoern A. Zeeb if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 223413628a7SBjoern A. Zeeb rc = 1; 224413628a7SBjoern A. Zeeb else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 225413628a7SBjoern A. Zeeb rc = -1; 226413628a7SBjoern A. Zeeb } 227413628a7SBjoern A. Zeeb return (rc); 228413628a7SBjoern A. Zeeb } 229413628a7SBjoern A. Zeeb #endif 230413628a7SBjoern A. Zeeb 231116734c4SMatthew Dillon /* 2329ddb7954SMike Barcroft * struct jail_args { 2339ddb7954SMike Barcroft * struct jail *jail; 2349ddb7954SMike Barcroft * }; 235116734c4SMatthew Dillon */ 23675c13541SPoul-Henning Kamp int 2379ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap) 23875c13541SPoul-Henning Kamp { 239413628a7SBjoern A. Zeeb uint32_t version; 240413628a7SBjoern A. Zeeb int error; 2410304c731SJamie Gritton struct jail j; 242413628a7SBjoern A. Zeeb 243413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 244413628a7SBjoern A. Zeeb if (error) 245413628a7SBjoern A. Zeeb return (error); 246413628a7SBjoern A. Zeeb 247413628a7SBjoern A. Zeeb switch (version) { 248413628a7SBjoern A. Zeeb case 0: 249413628a7SBjoern A. Zeeb { 250413628a7SBjoern A. Zeeb struct jail_v0 j0; 251413628a7SBjoern A. Zeeb 2520304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2530304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 254413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 255413628a7SBjoern A. Zeeb if (error) 256413628a7SBjoern A. Zeeb return (error); 2570304c731SJamie Gritton j.version = j0.version; 2580304c731SJamie Gritton j.path = j0.path; 2590304c731SJamie Gritton j.hostname = j0.hostname; 2600304c731SJamie Gritton j.ip4s = j0.ip_number; 261413628a7SBjoern A. Zeeb break; 262413628a7SBjoern A. Zeeb } 263413628a7SBjoern A. Zeeb 264413628a7SBjoern A. Zeeb case 1: 265413628a7SBjoern A. Zeeb /* 266413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 267413628a7SBjoern A. Zeeb * that never made it into the official kernel. 268413628a7SBjoern A. Zeeb */ 269413628a7SBjoern A. Zeeb return (EINVAL); 270413628a7SBjoern A. Zeeb 271413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 272413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 273413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 274413628a7SBjoern A. Zeeb if (error) 275413628a7SBjoern A. Zeeb return (error); 2760304c731SJamie Gritton break; 2770304c731SJamie Gritton 2780304c731SJamie Gritton default: 2790304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 2800304c731SJamie Gritton return (EINVAL); 2810304c731SJamie Gritton } 2820304c731SJamie Gritton return (kern_jail(td, &j)); 2830304c731SJamie Gritton } 2840304c731SJamie Gritton 2850304c731SJamie Gritton int 2860304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j) 2870304c731SJamie Gritton { 288e92e0574SJamie Gritton struct iovec optiov[2 * (4 289e92e0574SJamie Gritton + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 290e92e0574SJamie Gritton #ifdef INET 291e92e0574SJamie Gritton + 1 292e92e0574SJamie Gritton #endif 293e92e0574SJamie Gritton #ifdef INET6 294e92e0574SJamie Gritton + 1 295e92e0574SJamie Gritton #endif 296e92e0574SJamie Gritton )]; 2970304c731SJamie Gritton struct uio opt; 2980304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 2990304c731SJamie Gritton #ifdef INET 300e92e0574SJamie Gritton uint32_t ip4s; 3010304c731SJamie Gritton struct in_addr *u_ip4; 3020304c731SJamie Gritton #endif 3030304c731SJamie Gritton #ifdef INET6 3040304c731SJamie Gritton struct in6_addr *u_ip6; 3050304c731SJamie Gritton #endif 3060304c731SJamie Gritton size_t tmplen; 3070304c731SJamie Gritton int error, enforce_statfs, fi; 3080304c731SJamie Gritton 3090304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3100304c731SJamie Gritton opt.uio_iov = optiov; 3110304c731SJamie Gritton opt.uio_iovcnt = 0; 3120304c731SJamie Gritton opt.uio_offset = -1; 3130304c731SJamie Gritton opt.uio_resid = -1; 3140304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3150304c731SJamie Gritton opt.uio_rw = UIO_READ; 3160304c731SJamie Gritton opt.uio_td = td; 3170304c731SJamie Gritton 3180304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3190304c731SJamie Gritton if (!jailed(td->td_ucred)) { 3200304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / 3210304c731SJamie Gritton sizeof(pr_allow_names[0]); fi++) { 3220304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = 3230304c731SJamie Gritton (jail_default_allow & (1 << fi)) 3240304c731SJamie Gritton ? pr_allow_names[fi] : pr_allow_nonames[fi]; 3250304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3260304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3270304c731SJamie Gritton opt.uio_iovcnt += 2; 3280304c731SJamie Gritton } 3290304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3300304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3310304c731SJamie Gritton opt.uio_iovcnt++; 3320304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3330304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3340304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3350304c731SJamie Gritton opt.uio_iovcnt++; 3360304c731SJamie Gritton } 3370304c731SJamie Gritton 338b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 339b38ff370SJamie Gritton #ifdef INET 3400304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3410304c731SJamie Gritton if (ip4s > jail_max_af_ips) 342b38ff370SJamie Gritton return (EINVAL); 3430304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 344b38ff370SJamie Gritton #else 3450304c731SJamie Gritton if (j->ip4s > 0) 346b38ff370SJamie Gritton return (EINVAL); 347b38ff370SJamie Gritton #endif 348b38ff370SJamie Gritton #ifdef INET6 3490304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 350b38ff370SJamie Gritton return (EINVAL); 3510304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 352b38ff370SJamie Gritton #else 3530304c731SJamie Gritton if (j->ip6s > 0) 354b38ff370SJamie Gritton return (EINVAL); 355b38ff370SJamie Gritton #endif 356b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 357b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 358b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 359b38ff370SJamie Gritton #ifdef INET 360b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 361b38ff370SJamie Gritton #endif 362b38ff370SJamie Gritton #ifdef INET6 363b38ff370SJamie Gritton #ifdef INET 3640304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 365b38ff370SJamie Gritton #else 366b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 367b38ff370SJamie Gritton #endif 368b38ff370SJamie Gritton #endif 3690304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 3700304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 3710304c731SJamie Gritton opt.uio_iovcnt++; 3720304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 3730304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 3740304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 375b38ff370SJamie Gritton if (error) { 376b38ff370SJamie Gritton free(u_path, M_TEMP); 377b38ff370SJamie Gritton return (error); 378b38ff370SJamie Gritton } 3790304c731SJamie Gritton opt.uio_iovcnt++; 3800304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 3810304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 3820304c731SJamie Gritton opt.uio_iovcnt++; 3830304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 3840304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 3850304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 386b38ff370SJamie Gritton if (error) { 387b38ff370SJamie Gritton free(u_path, M_TEMP); 388b38ff370SJamie Gritton return (error); 389b38ff370SJamie Gritton } 3900304c731SJamie Gritton opt.uio_iovcnt++; 3910304c731SJamie Gritton if (j->jailname != NULL) { 392b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 393b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 394b38ff370SJamie Gritton opt.uio_iovcnt++; 395b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 3960304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 397b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 398b38ff370SJamie Gritton if (error) { 399b38ff370SJamie Gritton free(u_path, M_TEMP); 400b38ff370SJamie Gritton return (error); 401b38ff370SJamie Gritton } 402b38ff370SJamie Gritton opt.uio_iovcnt++; 403b38ff370SJamie Gritton } 404b38ff370SJamie Gritton #ifdef INET 405b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 406b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 407b38ff370SJamie Gritton opt.uio_iovcnt++; 408b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4090304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4100304c731SJamie Gritton if (j->version == 0) 4110304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4120304c731SJamie Gritton else { 4130304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 414b38ff370SJamie Gritton if (error) { 415b38ff370SJamie Gritton free(u_path, M_TEMP); 416b38ff370SJamie Gritton return (error); 417b38ff370SJamie Gritton } 4180304c731SJamie Gritton } 419b38ff370SJamie Gritton opt.uio_iovcnt++; 420b38ff370SJamie Gritton #endif 421b38ff370SJamie Gritton #ifdef INET6 422b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 423b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 424b38ff370SJamie Gritton opt.uio_iovcnt++; 425b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4260304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4270304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 428b38ff370SJamie Gritton if (error) { 429b38ff370SJamie Gritton free(u_path, M_TEMP); 430b38ff370SJamie Gritton return (error); 431b38ff370SJamie Gritton } 432b38ff370SJamie Gritton opt.uio_iovcnt++; 433b38ff370SJamie Gritton #endif 4340304c731SJamie Gritton KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 4350304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 436b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 437b38ff370SJamie Gritton free(u_path, M_TEMP); 438b38ff370SJamie Gritton return (error); 439b38ff370SJamie Gritton } 440b38ff370SJamie Gritton 4410304c731SJamie Gritton 442b38ff370SJamie Gritton /* 443b38ff370SJamie Gritton * struct jail_set_args { 444b38ff370SJamie Gritton * struct iovec *iovp; 445b38ff370SJamie Gritton * unsigned int iovcnt; 446b38ff370SJamie Gritton * int flags; 447b38ff370SJamie Gritton * }; 448b38ff370SJamie Gritton */ 449b38ff370SJamie Gritton int 450b38ff370SJamie Gritton jail_set(struct thread *td, struct jail_set_args *uap) 451b38ff370SJamie Gritton { 452b38ff370SJamie Gritton struct uio *auio; 453b38ff370SJamie Gritton int error; 454b38ff370SJamie Gritton 455b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 456b38ff370SJamie Gritton if (uap->iovcnt & 1) 457b38ff370SJamie Gritton return (EINVAL); 458b38ff370SJamie Gritton 459b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 460b38ff370SJamie Gritton if (error) 461b38ff370SJamie Gritton return (error); 462b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 463b38ff370SJamie Gritton free(auio, M_IOV); 464b38ff370SJamie Gritton return (error); 465413628a7SBjoern A. Zeeb } 466413628a7SBjoern A. Zeeb 467413628a7SBjoern A. Zeeb int 468b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 469413628a7SBjoern A. Zeeb { 470fd7a8150SMike Barcroft struct nameidata nd; 471b38ff370SJamie Gritton #ifdef INET 472b38ff370SJamie Gritton struct in_addr *ip4; 473413628a7SBjoern A. Zeeb #endif 474b38ff370SJamie Gritton #ifdef INET6 475b38ff370SJamie Gritton struct in6_addr *ip6; 476b38ff370SJamie Gritton #endif 477b38ff370SJamie Gritton struct vfsopt *opt; 478b38ff370SJamie Gritton struct vfsoptlist *opts; 47957aea6dfSBjoern A. Zeeb struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 480b38ff370SJamie Gritton struct vnode *root; 481babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 4820304c731SJamie Gritton #if defined(INET) || defined(INET6) 48357aea6dfSBjoern A. Zeeb struct prison *tppr; 484b38ff370SJamie Gritton void *op; 4850304c731SJamie Gritton #endif 48676ca6f88SJamie Gritton unsigned long hid; 4870304c731SJamie Gritton size_t namelen, onamelen; 4880304c731SJamie Gritton int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 4897cbf7213SJamie Gritton int gotchildmax, gotenforce, gothid, gotslevel; 4907cbf7213SJamie Gritton int fi, jid, jsys, len, level; 491b97457e2SJamie Gritton int childmax, slevel, vfslocked; 492d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 4930304c731SJamie Gritton int ii, ij; 494413628a7SBjoern A. Zeeb #endif 495413628a7SBjoern A. Zeeb #ifdef INET 4962b0d6f81SJamie Gritton int ip4s, redo_ip4; 497413628a7SBjoern A. Zeeb #endif 498b38ff370SJamie Gritton #ifdef INET6 4992b0d6f81SJamie Gritton int ip6s, redo_ip6; 500b38ff370SJamie Gritton #endif 501b38ff370SJamie Gritton unsigned pr_flags, ch_flags; 5020304c731SJamie Gritton unsigned pr_allow, ch_allow, tallow; 503b38ff370SJamie Gritton char numbuf[12]; 504b38ff370SJamie Gritton 505b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 506b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 507b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 508b38ff370SJamie Gritton if (error) 50975c13541SPoul-Henning Kamp return (error); 5100304c731SJamie Gritton mypr = ppr = td->td_ucred->cr_prison; 511b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5120304c731SJamie Gritton return (EPERM); 513b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 514b38ff370SJamie Gritton return (EINVAL); 515b38ff370SJamie Gritton 516b38ff370SJamie Gritton /* 517b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 518b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 519b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 520b38ff370SJamie Gritton * the allprison lock. 521b38ff370SJamie Gritton * 522b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 523b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 524b38ff370SJamie Gritton * than duplicate it under a different name. 525b38ff370SJamie Gritton */ 526b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 527b38ff370SJamie Gritton if (error) 528b38ff370SJamie Gritton return (error); 529b38ff370SJamie Gritton #ifdef INET 530b38ff370SJamie Gritton ip4 = NULL; 531b38ff370SJamie Gritton #endif 532b38ff370SJamie Gritton #ifdef INET6 533b38ff370SJamie Gritton ip6 = NULL; 534b38ff370SJamie Gritton #endif 535b38ff370SJamie Gritton 536b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 537b38ff370SJamie Gritton if (error == ENOENT) 538b38ff370SJamie Gritton jid = 0; 539b38ff370SJamie Gritton else if (error != 0) 540b38ff370SJamie Gritton goto done_free; 541b38ff370SJamie Gritton 542b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 543b38ff370SJamie Gritton if (error == ENOENT) 544b38ff370SJamie Gritton gotslevel = 0; 545b38ff370SJamie Gritton else if (error != 0) 546b38ff370SJamie Gritton goto done_free; 547b38ff370SJamie Gritton else 548b38ff370SJamie Gritton gotslevel = 1; 549b38ff370SJamie Gritton 550b97457e2SJamie Gritton error = 551b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 552b97457e2SJamie Gritton if (error == ENOENT) 553b97457e2SJamie Gritton gotchildmax = 0; 554b97457e2SJamie Gritton else if (error != 0) 555b97457e2SJamie Gritton goto done_free; 556b97457e2SJamie Gritton else 557b97457e2SJamie Gritton gotchildmax = 1; 558b97457e2SJamie Gritton 5590304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 5600304c731SJamie Gritton gotenforce = (error == 0); 5610304c731SJamie Gritton if (gotenforce) { 5620304c731SJamie Gritton if (enforce < 0 || enforce > 2) 5630304c731SJamie Gritton return (EINVAL); 5640304c731SJamie Gritton } else if (error != ENOENT) 5650304c731SJamie Gritton goto done_free; 5660304c731SJamie Gritton 567b38ff370SJamie Gritton pr_flags = ch_flags = 0; 5680304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 5690304c731SJamie Gritton fi++) { 5700304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 5710304c731SJamie Gritton continue; 5720304c731SJamie Gritton vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 5730304c731SJamie Gritton vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 5740304c731SJamie Gritton } 575b38ff370SJamie Gritton ch_flags |= pr_flags; 5767cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 5777cbf7213SJamie Gritton fi++) { 5787cbf7213SJamie Gritton error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 5797cbf7213SJamie Gritton sizeof(jsys)); 5807cbf7213SJamie Gritton if (error == ENOENT) 5817cbf7213SJamie Gritton continue; 5827cbf7213SJamie Gritton if (error != 0) 5837cbf7213SJamie Gritton goto done_free; 5847cbf7213SJamie Gritton switch (jsys) { 5857cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 5867cbf7213SJamie Gritton if (!pr_flag_jailsys[fi].disable) { 5877cbf7213SJamie Gritton error = EINVAL; 5887cbf7213SJamie Gritton goto done_free; 5897cbf7213SJamie Gritton } 5907cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].disable; 5917cbf7213SJamie Gritton break; 5927cbf7213SJamie Gritton case JAIL_SYS_NEW: 5937cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].new; 5947cbf7213SJamie Gritton break; 5957cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 5967cbf7213SJamie Gritton break; 5977cbf7213SJamie Gritton default: 5987cbf7213SJamie Gritton error = EINVAL; 5997cbf7213SJamie Gritton goto done_free; 6007cbf7213SJamie Gritton } 6017cbf7213SJamie Gritton ch_flags |= 6027cbf7213SJamie Gritton pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 6037cbf7213SJamie Gritton } 604b38ff370SJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 605b38ff370SJamie Gritton && !(pr_flags & PR_PERSIST)) { 606b38ff370SJamie Gritton error = EINVAL; 607b38ff370SJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 608b38ff370SJamie Gritton goto done_errmsg; 609b38ff370SJamie Gritton } 610679e1390SJamie Gritton #ifdef VIMAGE 611679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 612679e1390SJamie Gritton error = EINVAL; 613679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 614679e1390SJamie Gritton goto done_errmsg; 615679e1390SJamie Gritton } 616679e1390SJamie Gritton #endif 6172b0d6f81SJamie Gritton #ifdef INET 6182b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 6192b0d6f81SJamie Gritton error = EINVAL; 6202b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 6212b0d6f81SJamie Gritton goto done_errmsg; 6222b0d6f81SJamie Gritton } 6232b0d6f81SJamie Gritton #endif 6242b0d6f81SJamie Gritton #ifdef INET6 6252b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 6262b0d6f81SJamie Gritton error = EINVAL; 6272b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 6282b0d6f81SJamie Gritton goto done_errmsg; 6292b0d6f81SJamie Gritton } 6302b0d6f81SJamie Gritton #endif 631b38ff370SJamie Gritton 6320304c731SJamie Gritton pr_allow = ch_allow = 0; 6330304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 6340304c731SJamie Gritton fi++) { 6350304c731SJamie Gritton vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 6360304c731SJamie Gritton vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 6370304c731SJamie Gritton } 6380304c731SJamie Gritton ch_allow |= pr_allow; 6390304c731SJamie Gritton 640b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 641b38ff370SJamie Gritton if (error == ENOENT) 642b38ff370SJamie Gritton name = NULL; 643b38ff370SJamie Gritton else if (error != 0) 644b38ff370SJamie Gritton goto done_free; 645b38ff370SJamie Gritton else { 646b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 647b38ff370SJamie Gritton error = EINVAL; 648b38ff370SJamie Gritton goto done_free; 649b38ff370SJamie Gritton } 650b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 651b38ff370SJamie Gritton error = ENAMETOOLONG; 652b38ff370SJamie Gritton goto done_free; 653b38ff370SJamie Gritton } 654b38ff370SJamie Gritton } 655b38ff370SJamie Gritton 656b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 657b38ff370SJamie Gritton if (error == ENOENT) 658b38ff370SJamie Gritton host = NULL; 659b38ff370SJamie Gritton else if (error != 0) 660b38ff370SJamie Gritton goto done_free; 661b38ff370SJamie Gritton else { 66276ca6f88SJamie Gritton ch_flags |= PR_HOST; 66376ca6f88SJamie Gritton pr_flags |= PR_HOST; 664b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 665b38ff370SJamie Gritton error = EINVAL; 666b38ff370SJamie Gritton goto done_free; 667b38ff370SJamie Gritton } 668b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 669b38ff370SJamie Gritton error = ENAMETOOLONG; 670b38ff370SJamie Gritton goto done_free; 671b38ff370SJamie Gritton } 672b38ff370SJamie Gritton } 673b38ff370SJamie Gritton 67476ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 67576ca6f88SJamie Gritton if (error == ENOENT) 67676ca6f88SJamie Gritton domain = NULL; 67776ca6f88SJamie Gritton else if (error != 0) 67876ca6f88SJamie Gritton goto done_free; 67976ca6f88SJamie Gritton else { 68076ca6f88SJamie Gritton ch_flags |= PR_HOST; 68176ca6f88SJamie Gritton pr_flags |= PR_HOST; 68276ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 68376ca6f88SJamie Gritton error = EINVAL; 68476ca6f88SJamie Gritton goto done_free; 68576ca6f88SJamie Gritton } 68676ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 68776ca6f88SJamie Gritton error = ENAMETOOLONG; 68876ca6f88SJamie Gritton goto done_free; 68976ca6f88SJamie Gritton } 69076ca6f88SJamie Gritton } 69176ca6f88SJamie Gritton 69276ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 69376ca6f88SJamie Gritton if (error == ENOENT) 69476ca6f88SJamie Gritton uuid = NULL; 69576ca6f88SJamie Gritton else if (error != 0) 69676ca6f88SJamie Gritton goto done_free; 69776ca6f88SJamie Gritton else { 69876ca6f88SJamie Gritton ch_flags |= PR_HOST; 69976ca6f88SJamie Gritton pr_flags |= PR_HOST; 70076ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 70176ca6f88SJamie Gritton error = EINVAL; 70276ca6f88SJamie Gritton goto done_free; 70376ca6f88SJamie Gritton } 70476ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 70576ca6f88SJamie Gritton error = ENAMETOOLONG; 70676ca6f88SJamie Gritton goto done_free; 70776ca6f88SJamie Gritton } 70876ca6f88SJamie Gritton } 70976ca6f88SJamie Gritton 71076ca6f88SJamie Gritton #ifdef COMPAT_IA32 71176ca6f88SJamie Gritton if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 71276ca6f88SJamie Gritton uint32_t hid32; 71376ca6f88SJamie Gritton 71476ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 71576ca6f88SJamie Gritton hid = hid32; 71676ca6f88SJamie Gritton } else 71776ca6f88SJamie Gritton #endif 71876ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 71976ca6f88SJamie Gritton if (error == ENOENT) 72076ca6f88SJamie Gritton gothid = 0; 72176ca6f88SJamie Gritton else if (error != 0) 72276ca6f88SJamie Gritton goto done_free; 72376ca6f88SJamie Gritton else { 72476ca6f88SJamie Gritton gothid = 1; 72576ca6f88SJamie Gritton ch_flags |= PR_HOST; 72676ca6f88SJamie Gritton pr_flags |= PR_HOST; 72776ca6f88SJamie Gritton } 72876ca6f88SJamie Gritton 729b38ff370SJamie Gritton #ifdef INET 730b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 731b38ff370SJamie Gritton if (error == ENOENT) 7327cbf7213SJamie Gritton ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 733b38ff370SJamie Gritton else if (error != 0) 734b38ff370SJamie Gritton goto done_free; 735b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 736b38ff370SJamie Gritton error = EINVAL; 737b38ff370SJamie Gritton goto done_free; 7380304c731SJamie Gritton } else { 7397cbf7213SJamie Gritton ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 7407cbf7213SJamie Gritton if (ip4s == 0) 7417cbf7213SJamie Gritton pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 7427cbf7213SJamie Gritton else { 7437cbf7213SJamie Gritton pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 744b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 745b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 746b38ff370SJamie Gritton error = EINVAL; 747b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 748b38ff370SJamie Gritton goto done_errmsg; 749b38ff370SJamie Gritton } 7502b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 751b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 752b38ff370SJamie Gritton /* 7530304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 7540304c731SJamie Gritton * the primary IP address as given from userland. 7550304c731SJamie Gritton * This special IP is used for unbound outgoing 7560304c731SJamie Gritton * connections as well for "loopback" traffic. 757b38ff370SJamie Gritton */ 758b38ff370SJamie Gritton if (ip4s > 1) 759b38ff370SJamie Gritton qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 760b38ff370SJamie Gritton /* 7610304c731SJamie Gritton * Check for duplicate addresses and do some simple 7620304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 7630304c731SJamie Gritton * addresses it is their problem. 764b38ff370SJamie Gritton * 7650304c731SJamie Gritton * We do not have to care about byte order for these 7660304c731SJamie Gritton * checks so we will do them in NBO. 767b38ff370SJamie Gritton */ 768b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 769b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 770b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 771b38ff370SJamie Gritton error = EINVAL; 772b38ff370SJamie Gritton goto done_free; 773b38ff370SJamie Gritton } 774b38ff370SJamie Gritton if ((ii+1) < ip4s && 775b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 776b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 777b38ff370SJamie Gritton error = EINVAL; 778b38ff370SJamie Gritton goto done_free; 779b38ff370SJamie Gritton } 780b38ff370SJamie Gritton } 781b38ff370SJamie Gritton } 7820304c731SJamie Gritton } 783b38ff370SJamie Gritton #endif 784b38ff370SJamie Gritton 785b38ff370SJamie Gritton #ifdef INET6 786b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 787b38ff370SJamie Gritton if (error == ENOENT) 7887cbf7213SJamie Gritton ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 789b38ff370SJamie Gritton else if (error != 0) 790b38ff370SJamie Gritton goto done_free; 791b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 792b38ff370SJamie Gritton error = EINVAL; 793b38ff370SJamie Gritton goto done_free; 7940304c731SJamie Gritton } else { 7957cbf7213SJamie Gritton ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 7967cbf7213SJamie Gritton if (ip6s == 0) 7977cbf7213SJamie Gritton pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 7987cbf7213SJamie Gritton else { 7997cbf7213SJamie Gritton pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 800b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 801b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 802b38ff370SJamie Gritton error = EINVAL; 803b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 804b38ff370SJamie Gritton goto done_errmsg; 805b38ff370SJamie Gritton } 8062b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 807b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 808b38ff370SJamie Gritton if (ip6s > 1) 809b38ff370SJamie Gritton qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 810b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8110304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 812b38ff370SJamie Gritton error = EINVAL; 813b38ff370SJamie Gritton goto done_free; 814b38ff370SJamie Gritton } 815b38ff370SJamie Gritton if ((ii+1) < ip6s && 816b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 817b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 818b38ff370SJamie Gritton { 819b38ff370SJamie Gritton error = EINVAL; 820b38ff370SJamie Gritton goto done_free; 821b38ff370SJamie Gritton } 822b38ff370SJamie Gritton } 823b38ff370SJamie Gritton } 8240304c731SJamie Gritton } 825b38ff370SJamie Gritton #endif 826b38ff370SJamie Gritton 827bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 828bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 829bdfc8cc4SJamie Gritton error = EINVAL; 830bdfc8cc4SJamie Gritton vfs_opterror(opts, 831bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 832bdfc8cc4SJamie Gritton goto done_errmsg; 833bdfc8cc4SJamie Gritton } 834bdfc8cc4SJamie Gritton #endif 835bdfc8cc4SJamie Gritton 836b38ff370SJamie Gritton root = NULL; 837b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 838b38ff370SJamie Gritton if (error == ENOENT) 839b38ff370SJamie Gritton path = NULL; 840b38ff370SJamie Gritton else if (error != 0) 841b38ff370SJamie Gritton goto done_free; 842b38ff370SJamie Gritton else { 843b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 844b38ff370SJamie Gritton error = EINVAL; 845b38ff370SJamie Gritton vfs_opterror(opts, 846b38ff370SJamie Gritton "path cannot be changed after creation"); 847b38ff370SJamie Gritton goto done_errmsg; 848b38ff370SJamie Gritton } 849b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 850b38ff370SJamie Gritton error = EINVAL; 851b38ff370SJamie Gritton goto done_free; 852b38ff370SJamie Gritton } 853b38ff370SJamie Gritton if (len < 2 || (len == 2 && path[0] == '/')) 854b38ff370SJamie Gritton path = NULL; 855b38ff370SJamie Gritton else { 8560304c731SJamie Gritton /* Leave room for a real-root full pathname. */ 8570304c731SJamie Gritton if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 8580304c731SJamie Gritton ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 8590304c731SJamie Gritton error = ENAMETOOLONG; 8600304c731SJamie Gritton goto done_free; 8610304c731SJamie Gritton } 862b38ff370SJamie Gritton NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 863b38ff370SJamie Gritton path, td); 864b38ff370SJamie Gritton error = namei(&nd); 865b38ff370SJamie Gritton if (error) 866b38ff370SJamie Gritton goto done_free; 867b38ff370SJamie Gritton vfslocked = NDHASGIANT(&nd); 868b38ff370SJamie Gritton root = nd.ni_vp; 869b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 870b38ff370SJamie Gritton if (root->v_type != VDIR) { 871b38ff370SJamie Gritton error = ENOTDIR; 872b38ff370SJamie Gritton vrele(root); 873b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 874b38ff370SJamie Gritton goto done_free; 875b38ff370SJamie Gritton } 876b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 877b38ff370SJamie Gritton } 878b38ff370SJamie Gritton } 879b38ff370SJamie Gritton 880b38ff370SJamie Gritton /* 881b38ff370SJamie Gritton * Grab the allprison lock before letting modules check their 882b38ff370SJamie Gritton * parameters. Once we have it, do not let go so we'll have a 883b38ff370SJamie Gritton * consistent view of the OSD list. 884b38ff370SJamie Gritton */ 885b38ff370SJamie Gritton sx_xlock(&allprison_lock); 886b38ff370SJamie Gritton error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 887b38ff370SJamie Gritton if (error) 888b38ff370SJamie Gritton goto done_unlock_list; 889b38ff370SJamie Gritton 890b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 891b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 892b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 893b38ff370SJamie Gritton error = EINVAL; 894b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 895b38ff370SJamie Gritton goto done_unlock_list; 896b38ff370SJamie Gritton } 897b38ff370SJamie Gritton } 898b38ff370SJamie Gritton 899b38ff370SJamie Gritton /* 900b38ff370SJamie Gritton * See if we are creating a new record or updating an existing one. 901b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 902b38ff370SJamie Gritton */ 903b38ff370SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 904b38ff370SJamie Gritton if (!cuflags) { 905b38ff370SJamie Gritton error = EINVAL; 906b38ff370SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 907b38ff370SJamie Gritton goto done_unlock_list; 908b38ff370SJamie Gritton } 909b38ff370SJamie Gritton pr = NULL; 910babbbb9cSJamie Gritton namelc = NULL; 911babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 912babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 913babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 914babbbb9cSJamie Gritton if (*p != '\0') 915babbbb9cSJamie Gritton jid = 0; 916babbbb9cSJamie Gritton } 917b38ff370SJamie Gritton if (jid != 0) { 9180304c731SJamie Gritton /* 9190304c731SJamie Gritton * See if a requested jid already exists. There is an 9200304c731SJamie Gritton * information leak here if the jid exists but is not within 9210304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 9220304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 9230304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 9240304c731SJamie Gritton */ 925b38ff370SJamie Gritton if (jid < 0) { 926b38ff370SJamie Gritton error = EINVAL; 927b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 928b38ff370SJamie Gritton goto done_unlock_list; 929b38ff370SJamie Gritton } 930b38ff370SJamie Gritton pr = prison_find(jid); 931b38ff370SJamie Gritton if (pr != NULL) { 9320304c731SJamie Gritton ppr = pr->pr_parent; 933b38ff370SJamie Gritton /* Create: jid must not exist. */ 934b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 935b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 936b38ff370SJamie Gritton error = EEXIST; 937b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 938b38ff370SJamie Gritton jid); 939b38ff370SJamie Gritton goto done_unlock_list; 940b38ff370SJamie Gritton } 9410304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 9420304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9430304c731SJamie Gritton pr = NULL; 9440304c731SJamie Gritton } else if (pr->pr_uref == 0) { 945b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 946b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 947b38ff370SJamie Gritton error = ENOENT; 948b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 949b38ff370SJamie Gritton jid); 950b38ff370SJamie Gritton goto done_unlock_list; 951b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 952b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 953b38ff370SJamie Gritton /* 954b38ff370SJamie Gritton * A dying jail might be resurrected 955b38ff370SJamie Gritton * (via attach or persist), but first 956b38ff370SJamie Gritton * it must determine if another jail 957b38ff370SJamie Gritton * has claimed its name. Accomplish 958b38ff370SJamie Gritton * this by implicitly re-setting the 959b38ff370SJamie Gritton * name. 960b38ff370SJamie Gritton */ 961b38ff370SJamie Gritton if (name == NULL) 9620304c731SJamie Gritton name = prison_name(mypr, pr); 963b38ff370SJamie Gritton } 964b38ff370SJamie Gritton } 965b38ff370SJamie Gritton } 966b38ff370SJamie Gritton if (pr == NULL) { 967b38ff370SJamie Gritton /* Update: jid must exist. */ 968b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 969b38ff370SJamie Gritton error = ENOENT; 970b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 971b38ff370SJamie Gritton goto done_unlock_list; 972b38ff370SJamie Gritton } 973b38ff370SJamie Gritton } 974b38ff370SJamie Gritton } 975b38ff370SJamie Gritton /* 976b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 977b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 978b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 979b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 980b38ff370SJamie Gritton * because that is the jail being updated). 981b38ff370SJamie Gritton */ 982b38ff370SJamie Gritton if (name != NULL) { 983babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 984babbbb9cSJamie Gritton if (namelc == NULL) 985babbbb9cSJamie Gritton namelc = name; 986babbbb9cSJamie Gritton else { 9870304c731SJamie Gritton /* 9880304c731SJamie Gritton * This is a hierarchical name. Split it into the 9890304c731SJamie Gritton * parent and child names, and make sure the parent 9900304c731SJamie Gritton * exists or matches an already found jail. 9910304c731SJamie Gritton */ 992babbbb9cSJamie Gritton *namelc = '\0'; 9930304c731SJamie Gritton if (pr != NULL) { 994babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 995babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 9960304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9970304c731SJamie Gritton error = EINVAL; 9980304c731SJamie Gritton vfs_opterror(opts, 9990304c731SJamie Gritton "cannot change jail's parent"); 10000304c731SJamie Gritton goto done_unlock_list; 10010304c731SJamie Gritton } 10020304c731SJamie Gritton } else { 10030304c731SJamie Gritton ppr = prison_find_name(mypr, name); 10040304c731SJamie Gritton if (ppr == NULL) { 10050304c731SJamie Gritton error = ENOENT; 10060304c731SJamie Gritton vfs_opterror(opts, 10070304c731SJamie Gritton "jail \"%s\" not found", name); 10080304c731SJamie Gritton goto done_unlock_list; 10090304c731SJamie Gritton } 10100304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 10110304c731SJamie Gritton } 1012babbbb9cSJamie Gritton name = ++namelc; 10130304c731SJamie Gritton } 1014b38ff370SJamie Gritton if (name[0] != '\0') { 10150304c731SJamie Gritton namelen = 10160304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1017b38ff370SJamie Gritton name_again: 10180304c731SJamie Gritton deadpr = NULL; 10190304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1020b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 10210304c731SJamie Gritton !strcmp(tpr->pr_name + namelen, name)) { 1022b38ff370SJamie Gritton if (pr == NULL && 1023b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1024b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1025b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1026b38ff370SJamie Gritton /* 1027b38ff370SJamie Gritton * Use this jail 1028b38ff370SJamie Gritton * for updates. 1029b38ff370SJamie Gritton */ 1030b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1031b38ff370SJamie Gritton pr = tpr; 1032b38ff370SJamie Gritton break; 1033b38ff370SJamie Gritton } 1034b38ff370SJamie Gritton deadpr = tpr; 1035b38ff370SJamie Gritton } 1036b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1037b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1038b38ff370SJamie Gritton /* 1039b38ff370SJamie Gritton * Create, or update(jid): 1040b38ff370SJamie Gritton * name must not exist in an 10410304c731SJamie Gritton * active sibling jail. 1042b38ff370SJamie Gritton */ 1043b38ff370SJamie Gritton error = EEXIST; 1044b38ff370SJamie Gritton if (pr != NULL) 1045b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1046b38ff370SJamie Gritton vfs_opterror(opts, 1047b38ff370SJamie Gritton "jail \"%s\" already exists", 1048b38ff370SJamie Gritton name); 1049b38ff370SJamie Gritton goto done_unlock_list; 1050b38ff370SJamie Gritton } 1051b38ff370SJamie Gritton } 1052b38ff370SJamie Gritton } 1053b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1054b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1055b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1056b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1057b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1058b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1059b38ff370SJamie Gritton goto name_again; 1060b38ff370SJamie Gritton } 1061b38ff370SJamie Gritton pr = deadpr; 1062b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1063b38ff370SJamie Gritton error = ENOENT; 1064b38ff370SJamie Gritton vfs_opterror(opts, 1065b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1066b38ff370SJamie Gritton goto done_unlock_list; 1067b38ff370SJamie Gritton } 1068b38ff370SJamie Gritton } 1069b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1070b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1071b38ff370SJamie Gritton error = ENOENT; 1072b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1073b38ff370SJamie Gritton name); 1074b38ff370SJamie Gritton goto done_unlock_list; 1075b38ff370SJamie Gritton } 1076b38ff370SJamie Gritton } 1077b38ff370SJamie Gritton } 1078b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1079b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1080b38ff370SJamie Gritton error = ENOENT; 1081b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1082b38ff370SJamie Gritton goto done_unlock_list; 1083b38ff370SJamie Gritton } 1084b38ff370SJamie Gritton 1085b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1086b38ff370SJamie Gritton if (pr == NULL) { 1087b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1088b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1089b97457e2SJamie Gritton error = EPERM; 1090b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1091b97457e2SJamie Gritton goto done_unlock_list; 1092b97457e2SJamie Gritton } 1093b38ff370SJamie Gritton created = 1; 10940304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 10950304c731SJamie Gritton if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 10960304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 10970304c731SJamie Gritton error = ENOENT; 10980304c731SJamie Gritton vfs_opterror(opts, "parent jail went away!"); 10990304c731SJamie Gritton goto done_unlock_list; 11000304c731SJamie Gritton } 11010304c731SJamie Gritton ppr->pr_ref++; 11020304c731SJamie Gritton ppr->pr_uref++; 11030304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1104b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1105b38ff370SJamie Gritton if (jid == 0) { 1106b38ff370SJamie Gritton /* Find the next free jid. */ 1107b38ff370SJamie Gritton jid = lastprid + 1; 1108b38ff370SJamie Gritton findnext: 1109b38ff370SJamie Gritton if (jid == JAIL_MAX) 1110b38ff370SJamie Gritton jid = 1; 1111b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1112b38ff370SJamie Gritton if (tpr->pr_id < jid) 1113b38ff370SJamie Gritton continue; 1114b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1115b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1116b38ff370SJamie Gritton break; 1117b38ff370SJamie Gritton } 1118b38ff370SJamie Gritton if (jid == lastprid) { 1119b38ff370SJamie Gritton error = EAGAIN; 1120b38ff370SJamie Gritton vfs_opterror(opts, 1121b38ff370SJamie Gritton "no available jail IDs"); 1122b38ff370SJamie Gritton free(pr, M_PRISON); 11230304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 11240304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 11250304c731SJamie Gritton goto done_releroot; 1126b38ff370SJamie Gritton } 1127b38ff370SJamie Gritton jid++; 1128b38ff370SJamie Gritton goto findnext; 1129b38ff370SJamie Gritton } 1130b38ff370SJamie Gritton lastprid = jid; 1131b38ff370SJamie Gritton } else { 1132b38ff370SJamie Gritton /* 1133b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1134b38ff370SJamie Gritton * so just find where to insert it. 1135b38ff370SJamie Gritton */ 1136b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1137b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1138b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1139b38ff370SJamie Gritton break; 1140b38ff370SJamie Gritton } 1141b38ff370SJamie Gritton } 1142b38ff370SJamie Gritton if (tpr == NULL) 1143b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 11440304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 11450304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1146b97457e2SJamie Gritton tpr->pr_childcount++; 1147b38ff370SJamie Gritton 11480304c731SJamie Gritton pr->pr_parent = ppr; 1149b38ff370SJamie Gritton pr->pr_id = jid; 11500304c731SJamie Gritton 11510304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1152b38ff370SJamie Gritton if (name == NULL) 1153b38ff370SJamie Gritton name = ""; 1154b38ff370SJamie Gritton if (path == NULL) { 1155b38ff370SJamie Gritton path = "/"; 11560304c731SJamie Gritton root = mypr->pr_root; 1157b38ff370SJamie Gritton vref(root); 1158b38ff370SJamie Gritton } 11598986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 11608986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1161bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1162bdfc8cc4SJamie Gritton #ifdef VIMAGE 1163bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1164bdfc8cc4SJamie Gritton #endif 1165bdfc8cc4SJamie Gritton { 11660304c731SJamie Gritton #ifdef INET 11672b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 11682b0d6f81SJamie Gritton pr->pr_flags |= 11692b0d6f81SJamie Gritton PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 11702b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 11712b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 11722b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 11732b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 11742b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 11752b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 11762b0d6f81SJamie Gritton M_WAITOK); 11772b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 11782b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 11792b0d6f81SJamie Gritton } 11802b0d6f81SJamie Gritton } 11810304c731SJamie Gritton #endif 11820304c731SJamie Gritton #ifdef INET6 11832b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 11842b0d6f81SJamie Gritton pr->pr_flags |= 11852b0d6f81SJamie Gritton PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 11862b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 11872b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 11882b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 11892b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 11902b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 11912b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 11922b0d6f81SJamie Gritton M_WAITOK); 11932b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 11942b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 11952b0d6f81SJamie Gritton } 11962b0d6f81SJamie Gritton } 11970304c731SJamie Gritton #endif 1198bdfc8cc4SJamie Gritton } 1199bdfc8cc4SJamie Gritton #endif 12000304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 12010304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 120242bebd82SJamie Gritton pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1203b38ff370SJamie Gritton 12040304c731SJamie Gritton LIST_INIT(&pr->pr_children); 12050304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1206b38ff370SJamie Gritton 1207679e1390SJamie Gritton #ifdef VIMAGE 1208679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1209679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1210679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1211679e1390SJamie Gritton #endif 1212b38ff370SJamie Gritton /* 1213b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1214b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1215b38ff370SJamie Gritton */ 12160304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1217b38ff370SJamie Gritton if (error) { 1218b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1219b38ff370SJamie Gritton goto done_releroot; 1220b38ff370SJamie Gritton } 1221b38ff370SJamie Gritton 1222b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1223b38ff370SJamie Gritton /* 1224b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1225b38ff370SJamie Gritton * want other to see the incomplete prison once the 1226b38ff370SJamie Gritton * allprison_lock is downgraded. 1227b38ff370SJamie Gritton */ 1228b38ff370SJamie Gritton } else { 1229b38ff370SJamie Gritton created = 0; 12302b0d6f81SJamie Gritton /* 12312b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 12322b0d6f81SJamie Gritton * continue to exist for the duration of the call. 12332b0d6f81SJamie Gritton */ 12342b0d6f81SJamie Gritton pr->pr_ref++; 1235bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1236bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1237bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1238bdfc8cc4SJamie Gritton error = EINVAL; 1239bdfc8cc4SJamie Gritton vfs_opterror(opts, 1240bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1241bdfc8cc4SJamie Gritton goto done_deref_locked; 1242bdfc8cc4SJamie Gritton } 1243bdfc8cc4SJamie Gritton #endif 12442b0d6f81SJamie Gritton #ifdef INET 12452b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 12462b0d6f81SJamie Gritton error = EINVAL; 12472b0d6f81SJamie Gritton vfs_opterror(opts, 12482b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 12492b0d6f81SJamie Gritton goto done_deref_locked; 12502b0d6f81SJamie Gritton } 12512b0d6f81SJamie Gritton #endif 12522b0d6f81SJamie Gritton #ifdef INET6 12532b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 12542b0d6f81SJamie Gritton error = EINVAL; 12552b0d6f81SJamie Gritton vfs_opterror(opts, 12562b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 12572b0d6f81SJamie Gritton goto done_deref_locked; 12582b0d6f81SJamie Gritton } 12592b0d6f81SJamie Gritton #endif 1260b38ff370SJamie Gritton } 1261b38ff370SJamie Gritton 1262b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 12630304c731SJamie Gritton if (gotslevel) { 12640304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 12650304c731SJamie Gritton error = EPERM; 12660304c731SJamie Gritton goto done_deref_locked; 12670304c731SJamie Gritton } 12680304c731SJamie Gritton } 1269b97457e2SJamie Gritton if (gotchildmax) { 1270b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1271b97457e2SJamie Gritton error = EPERM; 1272b97457e2SJamie Gritton goto done_deref_locked; 1273b97457e2SJamie Gritton } 1274b97457e2SJamie Gritton } 12750304c731SJamie Gritton if (gotenforce) { 12760304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 12770304c731SJamie Gritton error = EPERM; 12780304c731SJamie Gritton goto done_deref_locked; 12790304c731SJamie Gritton } 12800304c731SJamie Gritton } 1281b38ff370SJamie Gritton #ifdef INET 12822b0d6f81SJamie Gritton if (ip4s > 0) { 12830304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 12840304c731SJamie Gritton /* 12850304c731SJamie Gritton * Make sure the new set of IP addresses is a 12860304c731SJamie Gritton * subset of the parent's list. Don't worry 12870304c731SJamie Gritton * about the parent being unlocked, as any 12880304c731SJamie Gritton * setting is done with allprison_lock held. 12890304c731SJamie Gritton */ 12900304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 12912b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 12920304c731SJamie Gritton break; 12930304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 12940304c731SJamie Gritton error = EPERM; 12950304c731SJamie Gritton goto done_deref_locked; 12960304c731SJamie Gritton } 12970304c731SJamie Gritton if (ip4s > 1) { 12980304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 12990304c731SJamie Gritton if (ip4[ii].s_addr == 13000304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1301b38ff370SJamie Gritton continue; 13020304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 13030304c731SJamie Gritton if (ip4[ii].s_addr == 13040304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 13050304c731SJamie Gritton break; 13060304c731SJamie Gritton if (ij == ppr->pr_ip4s) 13070304c731SJamie Gritton break; 13080304c731SJamie Gritton } 13090304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13100304c731SJamie Gritton error = EPERM; 13110304c731SJamie Gritton goto done_deref_locked; 13120304c731SJamie Gritton } 13130304c731SJamie Gritton } 13140304c731SJamie Gritton } 13150304c731SJamie Gritton /* 13160304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 13170304c731SJamie Gritton * if there is no more than one IP on each jail. If 13180304c731SJamie Gritton * there is a duplicate on a jail with more than one 13190304c731SJamie Gritton * IP stop checking and return error. 13200304c731SJamie Gritton */ 1321bdfc8cc4SJamie Gritton tppr = ppr; 1322bdfc8cc4SJamie Gritton #ifdef VIMAGE 1323bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1324bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1325bdfc8cc4SJamie Gritton break; 1326bdfc8cc4SJamie Gritton #endif 1327bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1328bdfc8cc4SJamie Gritton if (tpr == pr || 1329bdfc8cc4SJamie Gritton #ifdef VIMAGE 13302b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1331bdfc8cc4SJamie Gritton #endif 1332bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 13330304c731SJamie Gritton descend = 0; 13340304c731SJamie Gritton continue; 13350304c731SJamie Gritton } 13360304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 13370304c731SJamie Gritton continue; 13380304c731SJamie Gritton descend = 0; 13390304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 13400304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 13410304c731SJamie Gritton continue; 13420304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 13432b0d6f81SJamie Gritton if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 13440304c731SJamie Gritton error = EADDRINUSE; 1345b38ff370SJamie Gritton vfs_opterror(opts, 1346b38ff370SJamie Gritton "IPv4 addresses clash"); 1347b38ff370SJamie Gritton goto done_deref_locked; 1348b38ff370SJamie Gritton } 13490304c731SJamie Gritton } 13500304c731SJamie Gritton } 13510304c731SJamie Gritton } 1352b38ff370SJamie Gritton #endif 1353b38ff370SJamie Gritton #ifdef INET6 13542b0d6f81SJamie Gritton if (ip6s > 0) { 13550304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 13560304c731SJamie Gritton /* 13570304c731SJamie Gritton * Make sure the new set of IP addresses is a 13580304c731SJamie Gritton * subset of the parent's list. 13590304c731SJamie Gritton */ 13600304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 13610304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 13620304c731SJamie Gritton &ppr->pr_ip6[ij])) 13630304c731SJamie Gritton break; 13640304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 13650304c731SJamie Gritton error = EPERM; 13660304c731SJamie Gritton goto done_deref_locked; 13670304c731SJamie Gritton } 13680304c731SJamie Gritton if (ip6s > 1) { 13690304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 13700304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 13710304c731SJamie Gritton &ppr->pr_ip6[0])) 13720304c731SJamie Gritton continue; 13730304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 13740304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 13752b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 13760304c731SJamie Gritton break; 13770304c731SJamie Gritton if (ij == ppr->pr_ip6s) 13780304c731SJamie Gritton break; 13790304c731SJamie Gritton } 13800304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 13810304c731SJamie Gritton error = EPERM; 13820304c731SJamie Gritton goto done_deref_locked; 13830304c731SJamie Gritton } 13840304c731SJamie Gritton } 13850304c731SJamie Gritton } 13860304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1387bdfc8cc4SJamie Gritton tppr = ppr; 1388bdfc8cc4SJamie Gritton #ifdef VIMAGE 1389bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1390bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1391bdfc8cc4SJamie Gritton break; 1392bdfc8cc4SJamie Gritton #endif 1393bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1394bdfc8cc4SJamie Gritton if (tpr == pr || 1395bdfc8cc4SJamie Gritton #ifdef VIMAGE 13962b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1397bdfc8cc4SJamie Gritton #endif 1398bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 13990304c731SJamie Gritton descend = 0; 14000304c731SJamie Gritton continue; 14010304c731SJamie Gritton } 14020304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 14030304c731SJamie Gritton continue; 14040304c731SJamie Gritton descend = 0; 14050304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 14060304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 14070304c731SJamie Gritton continue; 14080304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 14092b0d6f81SJamie Gritton if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 14100304c731SJamie Gritton error = EADDRINUSE; 1411b38ff370SJamie Gritton vfs_opterror(opts, 1412b38ff370SJamie Gritton "IPv6 addresses clash"); 1413b38ff370SJamie Gritton goto done_deref_locked; 1414b38ff370SJamie Gritton } 14150304c731SJamie Gritton } 14160304c731SJamie Gritton } 14170304c731SJamie Gritton } 1418b38ff370SJamie Gritton #endif 14190304c731SJamie Gritton onamelen = namelen = 0; 14200304c731SJamie Gritton if (name != NULL) { 1421b38ff370SJamie Gritton /* Give a default name of the jid. */ 1422b38ff370SJamie Gritton if (name[0] == '\0') 1423b38ff370SJamie Gritton snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1424babbbb9cSJamie Gritton else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1425babbbb9cSJamie Gritton *p == '\0')) { 1426b38ff370SJamie Gritton error = EINVAL; 1427babbbb9cSJamie Gritton vfs_opterror(opts, 1428babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 14290304c731SJamie Gritton goto done_deref_locked; 1430b38ff370SJamie Gritton } 1431b38ff370SJamie Gritton /* 14320304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 14330304c731SJamie Gritton * children. 1434b38ff370SJamie Gritton */ 14350304c731SJamie Gritton onamelen = strlen(pr->pr_name); 14360304c731SJamie Gritton namelen = strlen(name); 14370304c731SJamie Gritton if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 14380304c731SJamie Gritton error = ENAMETOOLONG; 14390304c731SJamie Gritton goto done_deref_locked; 14400304c731SJamie Gritton } 14410304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 14420304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 14430304c731SJamie Gritton sizeof(pr->pr_name)) { 14440304c731SJamie Gritton error = ENAMETOOLONG; 14450304c731SJamie Gritton goto done_deref_locked; 14460304c731SJamie Gritton } 14470304c731SJamie Gritton } 14480304c731SJamie Gritton } 14490304c731SJamie Gritton if (pr_allow & ~ppr->pr_allow) { 14500304c731SJamie Gritton error = EPERM; 14510304c731SJamie Gritton goto done_deref_locked; 1452b38ff370SJamie Gritton } 1453b38ff370SJamie Gritton 1454b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1455b38ff370SJamie Gritton #ifdef INET 14560304c731SJamie Gritton redo_ip4 = 0; 14570304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 14580304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1459b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 14600304c731SJamie Gritton pr->pr_ip4s = ip4s; 1461b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1462b38ff370SJamie Gritton ip4 = NULL; 14630304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1464bdfc8cc4SJamie Gritton #ifdef VIMAGE 1465bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1466bdfc8cc4SJamie Gritton descend = 0; 1467bdfc8cc4SJamie Gritton continue; 1468bdfc8cc4SJamie Gritton } 1469bdfc8cc4SJamie Gritton #endif 14700304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 14710304c731SJamie Gritton redo_ip4 = 1; 14720304c731SJamie Gritton descend = 0; 14730304c731SJamie Gritton } 14740304c731SJamie Gritton } 14750304c731SJamie Gritton } 1476b38ff370SJamie Gritton #endif 1477b38ff370SJamie Gritton #ifdef INET6 14780304c731SJamie Gritton redo_ip6 = 0; 14790304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 14800304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1481b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 14820304c731SJamie Gritton pr->pr_ip6s = ip6s; 1483b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1484b38ff370SJamie Gritton ip6 = NULL; 14850304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1486bdfc8cc4SJamie Gritton #ifdef VIMAGE 1487bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1488bdfc8cc4SJamie Gritton descend = 0; 1489bdfc8cc4SJamie Gritton continue; 1490bdfc8cc4SJamie Gritton } 1491bdfc8cc4SJamie Gritton #endif 14920304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 14930304c731SJamie Gritton redo_ip6 = 1; 14940304c731SJamie Gritton descend = 0; 14950304c731SJamie Gritton } 14960304c731SJamie Gritton } 14970304c731SJamie Gritton } 1498b38ff370SJamie Gritton #endif 14990304c731SJamie Gritton if (gotslevel) { 1500b38ff370SJamie Gritton pr->pr_securelevel = slevel; 15010304c731SJamie Gritton /* Set all child jails to be at least this level. */ 15020304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 15030304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 15040304c731SJamie Gritton tpr->pr_securelevel = slevel; 15050304c731SJamie Gritton } 1506b97457e2SJamie Gritton if (gotchildmax) { 1507b97457e2SJamie Gritton pr->pr_childmax = childmax; 1508b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1509b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1510b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1511b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1512b97457e2SJamie Gritton ? childmax - level : 0; 1513b97457e2SJamie Gritton } 15140304c731SJamie Gritton if (gotenforce) { 15150304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 15160304c731SJamie Gritton /* Pass this restriction on to the children. */ 15170304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 15180304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 15190304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 15200304c731SJamie Gritton } 15210304c731SJamie Gritton if (name != NULL) { 15220304c731SJamie Gritton if (ppr == &prison0) 1523b38ff370SJamie Gritton strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 15240304c731SJamie Gritton else 15250304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 15260304c731SJamie Gritton ppr->pr_name, name); 15270304c731SJamie Gritton /* Change this component of child names. */ 15280304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 15290304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 15300304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 15310304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 15320304c731SJamie Gritton } 15330304c731SJamie Gritton } 1534b38ff370SJamie Gritton if (path != NULL) { 15350304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 15360304c731SJamie Gritton if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 15370304c731SJamie Gritton snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 15380304c731SJamie Gritton mypr->pr_path, path); 15390304c731SJamie Gritton else 1540b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1541b38ff370SJamie Gritton pr->pr_root = root; 1542b38ff370SJamie Gritton } 154376ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 154476ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 154576ca6f88SJamie Gritton /* 154676ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 154776ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 154876ca6f88SJamie Gritton * it is always set with allprison_lock at least 154976ca6f88SJamie Gritton * shared, and is held exclusively here. 155076ca6f88SJamie Gritton */ 1551c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1552c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1553c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1554c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1555c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1556c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 155776ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 155876ca6f88SJamie Gritton } 155976ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 156076ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1561b38ff370SJamie Gritton if (host != NULL) 1562c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 156376ca6f88SJamie Gritton if (domain != NULL) 1564c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1565c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 156676ca6f88SJamie Gritton if (uuid != NULL) 1567c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 156876ca6f88SJamie Gritton if (gothid) 156976ca6f88SJamie Gritton pr->pr_hostid = hid; 157076ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 157176ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 157276ca6f88SJamie Gritton descend = 0; 157376ca6f88SJamie Gritton else { 157476ca6f88SJamie Gritton if (host != NULL) 1575c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1576c1f19219SJamie Gritton pr->pr_hostname, 1577c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 157876ca6f88SJamie Gritton if (domain != NULL) 1579c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1580c1f19219SJamie Gritton pr->pr_domainname, 1581c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 158276ca6f88SJamie Gritton if (uuid != NULL) 1583c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1584c1f19219SJamie Gritton pr->pr_hostuuid, 1585c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 158676ca6f88SJamie Gritton if (gothid) 158776ca6f88SJamie Gritton tpr->pr_hostid = hid; 158876ca6f88SJamie Gritton } 158976ca6f88SJamie Gritton } 159076ca6f88SJamie Gritton } 15910304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 15920304c731SJamie Gritton /* Clear allow bits in all children. */ 15930304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 15940304c731SJamie Gritton tpr->pr_allow &= ~tallow; 15950304c731SJamie Gritton } 15960304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1597b38ff370SJamie Gritton /* 1598b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1599b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1600b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1601b38ff370SJamie Gritton * handlers have completed. 1602b38ff370SJamie Gritton */ 1603b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1604b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1605b38ff370SJamie Gritton pr->pr_ref++; 1606b38ff370SJamie Gritton pr->pr_uref++; 1607b38ff370SJamie Gritton } else { 1608b38ff370SJamie Gritton pr->pr_ref--; 1609b38ff370SJamie Gritton pr->pr_uref--; 1610b38ff370SJamie Gritton } 1611b38ff370SJamie Gritton } 1612b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1613b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1614b38ff370SJamie Gritton 16150304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 16160304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 16170304c731SJamie Gritton */ 16180304c731SJamie Gritton #ifdef INET 16190304c731SJamie Gritton while (redo_ip4) { 16200304c731SJamie Gritton ip4s = pr->pr_ip4s; 16210304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 16220304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 16230304c731SJamie Gritton redo_ip4 = 0; 16240304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1625bdfc8cc4SJamie Gritton #ifdef VIMAGE 1626bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1627bdfc8cc4SJamie Gritton descend = 0; 1628bdfc8cc4SJamie Gritton continue; 1629bdfc8cc4SJamie Gritton } 1630bdfc8cc4SJamie Gritton #endif 16310304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 16320304c731SJamie Gritton if (ip4 != NULL) 16330304c731SJamie Gritton ip4 = NULL; 16340304c731SJamie Gritton else 16350304c731SJamie Gritton redo_ip4 = 1; 16360304c731SJamie Gritton } 16370304c731SJamie Gritton } 16380304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 16390304c731SJamie Gritton } 16400304c731SJamie Gritton #endif 16410304c731SJamie Gritton #ifdef INET6 16420304c731SJamie Gritton while (redo_ip6) { 16430304c731SJamie Gritton ip6s = pr->pr_ip6s; 16440304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 16450304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 16460304c731SJamie Gritton redo_ip6 = 0; 16470304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1648bdfc8cc4SJamie Gritton #ifdef VIMAGE 1649bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1650bdfc8cc4SJamie Gritton descend = 0; 1651bdfc8cc4SJamie Gritton continue; 1652bdfc8cc4SJamie Gritton } 1653bdfc8cc4SJamie Gritton #endif 16540304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 16550304c731SJamie Gritton if (ip6 != NULL) 16560304c731SJamie Gritton ip6 = NULL; 16570304c731SJamie Gritton else 16580304c731SJamie Gritton redo_ip6 = 1; 16590304c731SJamie Gritton } 16600304c731SJamie Gritton } 16610304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 16620304c731SJamie Gritton } 16630304c731SJamie Gritton #endif 16640304c731SJamie Gritton 1665b38ff370SJamie Gritton /* Let the modules do their work. */ 1666b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1667b38ff370SJamie Gritton if (created) { 1668b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1669b38ff370SJamie Gritton if (error) { 1670b38ff370SJamie Gritton prison_deref(pr, PD_LIST_SLOCKED); 1671b38ff370SJamie Gritton goto done_errmsg; 1672b38ff370SJamie Gritton } 1673b38ff370SJamie Gritton } 1674b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1675b38ff370SJamie Gritton if (error) { 1676b38ff370SJamie Gritton prison_deref(pr, created 1677b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1678b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1679b38ff370SJamie Gritton goto done_errmsg; 1680b38ff370SJamie Gritton } 1681b38ff370SJamie Gritton 1682b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1683b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1684b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1685b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1686b38ff370SJamie Gritton if (error) { 1687b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1688b38ff370SJamie Gritton if (!created) 1689b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1690b38ff370SJamie Gritton goto done_errmsg; 1691b38ff370SJamie Gritton } 1692b38ff370SJamie Gritton } 1693b38ff370SJamie Gritton 1694b38ff370SJamie Gritton /* 1695b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1696b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1697b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1698b38ff370SJamie Gritton * visible). 1699b38ff370SJamie Gritton */ 1700b38ff370SJamie Gritton if (!created) { 1701b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1702b38ff370SJamie Gritton ? PD_DEREF 1703b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1704b38ff370SJamie Gritton } else { 1705b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1706b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1707b38ff370SJamie Gritton pr->pr_ref++; 1708b38ff370SJamie Gritton pr->pr_uref++; 1709b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1710b38ff370SJamie Gritton } 1711b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1712b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1713b38ff370SJamie Gritton } 1714b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 1715b38ff370SJamie Gritton goto done_errmsg; 1716b38ff370SJamie Gritton 17170304c731SJamie Gritton done_deref_locked: 17180304c731SJamie Gritton prison_deref(pr, created 17190304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 17200304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 17210304c731SJamie Gritton goto done_releroot; 1722b38ff370SJamie Gritton done_unlock_list: 1723b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1724b38ff370SJamie Gritton done_releroot: 1725b38ff370SJamie Gritton if (root != NULL) { 1726b38ff370SJamie Gritton vfslocked = VFS_LOCK_GIANT(root->v_mount); 1727b38ff370SJamie Gritton vrele(root); 1728b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 1729b38ff370SJamie Gritton } 1730b38ff370SJamie Gritton done_errmsg: 1731b38ff370SJamie Gritton if (error) { 1732b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1733b38ff370SJamie Gritton if (errmsg_len > 0) { 1734b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1735b38ff370SJamie Gritton if (errmsg_pos > 0) { 1736b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1737b38ff370SJamie Gritton bcopy(errmsg, 1738b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1739b38ff370SJamie Gritton errmsg_len); 1740b38ff370SJamie Gritton else 1741b38ff370SJamie Gritton copyout(errmsg, 1742b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1743b38ff370SJamie Gritton errmsg_len); 1744b38ff370SJamie Gritton } 1745b38ff370SJamie Gritton } 1746b38ff370SJamie Gritton } 1747b38ff370SJamie Gritton done_free: 1748b38ff370SJamie Gritton #ifdef INET 1749b38ff370SJamie Gritton free(ip4, M_PRISON); 1750b38ff370SJamie Gritton #endif 1751b38ff370SJamie Gritton #ifdef INET6 1752b38ff370SJamie Gritton free(ip6, M_PRISON); 1753b38ff370SJamie Gritton #endif 1754b38ff370SJamie Gritton vfs_freeopts(opts); 1755b38ff370SJamie Gritton return (error); 1756b38ff370SJamie Gritton } 1757b38ff370SJamie Gritton 1758b38ff370SJamie Gritton 1759b38ff370SJamie Gritton /* 1760b38ff370SJamie Gritton * struct jail_get_args { 1761b38ff370SJamie Gritton * struct iovec *iovp; 1762b38ff370SJamie Gritton * unsigned int iovcnt; 1763b38ff370SJamie Gritton * int flags; 1764b38ff370SJamie Gritton * }; 1765b38ff370SJamie Gritton */ 1766b38ff370SJamie Gritton int 1767b38ff370SJamie Gritton jail_get(struct thread *td, struct jail_get_args *uap) 1768b38ff370SJamie Gritton { 1769b38ff370SJamie Gritton struct uio *auio; 1770b38ff370SJamie Gritton int error; 1771b38ff370SJamie Gritton 1772b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1773b38ff370SJamie Gritton if (uap->iovcnt & 1) 1774b38ff370SJamie Gritton return (EINVAL); 1775b38ff370SJamie Gritton 1776b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1777b38ff370SJamie Gritton if (error) 1778b38ff370SJamie Gritton return (error); 1779b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1780b38ff370SJamie Gritton if (error == 0) 1781b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1782b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1783b38ff370SJamie Gritton free(auio, M_IOV); 1784b38ff370SJamie Gritton return (error); 1785b38ff370SJamie Gritton } 1786b38ff370SJamie Gritton 1787b38ff370SJamie Gritton int 1788b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1789b38ff370SJamie Gritton { 17900304c731SJamie Gritton struct prison *pr, *mypr; 1791b38ff370SJamie Gritton struct vfsopt *opt; 1792b38ff370SJamie Gritton struct vfsoptlist *opts; 1793b38ff370SJamie Gritton char *errmsg, *name; 17940304c731SJamie Gritton int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1795b38ff370SJamie Gritton 1796b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1797b38ff370SJamie Gritton return (EINVAL); 1798b38ff370SJamie Gritton 1799b38ff370SJamie Gritton /* Get the parameter list. */ 1800b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1801b38ff370SJamie Gritton if (error) 1802b38ff370SJamie Gritton return (error); 1803b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 18040304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 18051e2a13e6SJamie Gritton 1806b38ff370SJamie Gritton /* 1807b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1808b38ff370SJamie Gritton */ 1809b38ff370SJamie Gritton sx_slock(&allprison_lock); 1810b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1811b38ff370SJamie Gritton if (error == 0) { 1812b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 18130304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1814b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1815b38ff370SJamie Gritton if (pr->pr_ref > 0 && 1816b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1817b38ff370SJamie Gritton break; 1818b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1819b38ff370SJamie Gritton } 1820b38ff370SJamie Gritton } 1821b38ff370SJamie Gritton if (pr != NULL) 1822b38ff370SJamie Gritton goto found_prison; 1823b38ff370SJamie Gritton error = ENOENT; 1824b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 1825b38ff370SJamie Gritton goto done_unlock_list; 1826b38ff370SJamie Gritton } else if (error != ENOENT) 1827b38ff370SJamie Gritton goto done_unlock_list; 1828b38ff370SJamie Gritton 1829b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1830b38ff370SJamie Gritton if (error == 0) { 1831b38ff370SJamie Gritton if (jid != 0) { 18320304c731SJamie Gritton pr = prison_find_child(mypr, jid); 1833b38ff370SJamie Gritton if (pr != NULL) { 1834b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1835b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1836b38ff370SJamie Gritton error = ENOENT; 1837b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1838b38ff370SJamie Gritton jid); 1839b38ff370SJamie Gritton goto done_unlock_list; 1840b38ff370SJamie Gritton } 1841b38ff370SJamie Gritton goto found_prison; 1842b38ff370SJamie Gritton } 1843b38ff370SJamie Gritton error = ENOENT; 1844b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1845b38ff370SJamie Gritton goto done_unlock_list; 1846b38ff370SJamie Gritton } 1847b38ff370SJamie Gritton } else if (error != ENOENT) 1848b38ff370SJamie Gritton goto done_unlock_list; 1849b38ff370SJamie Gritton 1850b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1851b38ff370SJamie Gritton if (error == 0) { 1852b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1853b38ff370SJamie Gritton error = EINVAL; 1854b38ff370SJamie Gritton goto done_unlock_list; 1855b38ff370SJamie Gritton } 18560304c731SJamie Gritton pr = prison_find_name(mypr, name); 1857b38ff370SJamie Gritton if (pr != NULL) { 1858b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1859b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1860b38ff370SJamie Gritton error = ENOENT; 1861b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 1862b38ff370SJamie Gritton name); 1863b38ff370SJamie Gritton goto done_unlock_list; 1864b38ff370SJamie Gritton } 1865b38ff370SJamie Gritton goto found_prison; 1866b38ff370SJamie Gritton } 1867b38ff370SJamie Gritton error = ENOENT; 1868b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 1869b38ff370SJamie Gritton goto done_unlock_list; 1870b38ff370SJamie Gritton } else if (error != ENOENT) 1871b38ff370SJamie Gritton goto done_unlock_list; 1872b38ff370SJamie Gritton 1873b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 1874b38ff370SJamie Gritton error = ENOENT; 1875b38ff370SJamie Gritton goto done_unlock_list; 1876b38ff370SJamie Gritton 1877b38ff370SJamie Gritton found_prison: 1878b38ff370SJamie Gritton /* Get the parameters of the prison. */ 1879b38ff370SJamie Gritton pr->pr_ref++; 1880b38ff370SJamie Gritton locked = PD_LOCKED; 1881b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 1882b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1883b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1884b38ff370SJamie Gritton goto done_deref; 18850304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 18860304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1887b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1888b38ff370SJamie Gritton goto done_deref; 18890304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 18900304c731SJamie Gritton if (error != 0 && error != ENOENT) 18910304c731SJamie Gritton goto done_deref; 18920304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1893b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 1894b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1895b38ff370SJamie Gritton goto done_deref; 18960304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1897b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1898b38ff370SJamie Gritton goto done_deref; 1899b38ff370SJamie Gritton #ifdef INET 1900b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1901b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1902b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1903b38ff370SJamie Gritton goto done_deref; 1904b38ff370SJamie Gritton #endif 1905b38ff370SJamie Gritton #ifdef INET6 1906b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1907b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1908b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1909b38ff370SJamie Gritton goto done_deref; 1910b38ff370SJamie Gritton #endif 1911b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1912b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 1913b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1914b38ff370SJamie Gritton goto done_deref; 1915b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1916b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 1917b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 1918b97457e2SJamie Gritton goto done_deref; 1919b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1920b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 1921b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 1922b97457e2SJamie Gritton goto done_deref; 1923c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1924b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1925b38ff370SJamie Gritton goto done_deref; 1926c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 192776ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 192876ca6f88SJamie Gritton goto done_deref; 1929c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 193076ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 193176ca6f88SJamie Gritton goto done_deref; 193276ca6f88SJamie Gritton #ifdef COMPAT_IA32 193376ca6f88SJamie Gritton if (td->td_proc->p_sysent->sv_flags & SV_IA32) { 193476ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 193576ca6f88SJamie Gritton 193676ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 193776ca6f88SJamie Gritton } else 193876ca6f88SJamie Gritton #endif 193976ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 194076ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 194176ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 194276ca6f88SJamie Gritton goto done_deref; 19430304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 19440304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 19450304c731SJamie Gritton if (error != 0 && error != ENOENT) 19460304c731SJamie Gritton goto done_deref; 19470304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 19480304c731SJamie Gritton fi++) { 19490304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 19500304c731SJamie Gritton continue; 19510304c731SJamie Gritton i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 19520304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 1953b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1954b38ff370SJamie Gritton goto done_deref; 1955b38ff370SJamie Gritton i = !i; 19560304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 1957b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1958b38ff370SJamie Gritton goto done_deref; 19590304c731SJamie Gritton } 19607cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 19617cbf7213SJamie Gritton fi++) { 19627cbf7213SJamie Gritton i = pr->pr_flags & 19637cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 19647cbf7213SJamie Gritton i = pr_flag_jailsys[fi].disable && 19657cbf7213SJamie Gritton (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 19667cbf7213SJamie Gritton : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 19677cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 19687cbf7213SJamie Gritton error = 19697cbf7213SJamie Gritton vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 19707cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 19717cbf7213SJamie Gritton goto done_deref; 19727cbf7213SJamie Gritton } 19730304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 19740304c731SJamie Gritton fi++) { 19750304c731SJamie Gritton if (pr_allow_names[fi] == NULL) 19760304c731SJamie Gritton continue; 19770304c731SJamie Gritton i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 19780304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 19790304c731SJamie Gritton if (error != 0 && error != ENOENT) 19800304c731SJamie Gritton goto done_deref; 19810304c731SJamie Gritton i = !i; 19820304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 19830304c731SJamie Gritton if (error != 0 && error != ENOENT) 19840304c731SJamie Gritton goto done_deref; 19850304c731SJamie Gritton } 1986b38ff370SJamie Gritton i = (pr->pr_uref == 0); 1987b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 1988b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1989b38ff370SJamie Gritton goto done_deref; 1990b38ff370SJamie Gritton i = !i; 1991b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 1992b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1993b38ff370SJamie Gritton goto done_deref; 1994b38ff370SJamie Gritton 1995b38ff370SJamie Gritton /* Get the module parameters. */ 1996b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1997b38ff370SJamie Gritton locked = 0; 1998b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 1999b38ff370SJamie Gritton if (error) 2000b38ff370SJamie Gritton goto done_deref; 2001b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2002b38ff370SJamie Gritton 2003b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2004b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2005b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2006b38ff370SJamie Gritton error = EINVAL; 2007b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2008b38ff370SJamie Gritton goto done_errmsg; 2009b38ff370SJamie Gritton } 2010b38ff370SJamie Gritton } 2011b38ff370SJamie Gritton 2012b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2013b38ff370SJamie Gritton error = 0; 2014b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2015b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2016b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2017b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2018b38ff370SJamie Gritton if (opt->value != NULL) { 2019b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2020b38ff370SJamie Gritton bcopy(opt->value, 2021b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2022b38ff370SJamie Gritton opt->len); 2023b38ff370SJamie Gritton } else { 2024b38ff370SJamie Gritton error = copyout(opt->value, 2025b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2026b38ff370SJamie Gritton opt->len); 2027b38ff370SJamie Gritton if (error) 2028b38ff370SJamie Gritton break; 2029b38ff370SJamie Gritton } 2030b38ff370SJamie Gritton } 2031b38ff370SJamie Gritton } 2032b38ff370SJamie Gritton } 2033b38ff370SJamie Gritton goto done_errmsg; 2034b38ff370SJamie Gritton 2035b38ff370SJamie Gritton done_deref: 2036b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2037b38ff370SJamie Gritton goto done_errmsg; 2038b38ff370SJamie Gritton 2039b38ff370SJamie Gritton done_unlock_list: 2040b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2041b38ff370SJamie Gritton done_errmsg: 2042b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2043b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2044b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2045b38ff370SJamie Gritton if (errmsg_len > 0) { 2046b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2047b38ff370SJamie Gritton bcopy(errmsg, 2048b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2049b38ff370SJamie Gritton errmsg_len); 2050b38ff370SJamie Gritton else 2051b38ff370SJamie Gritton copyout(errmsg, 2052b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2053b38ff370SJamie Gritton errmsg_len); 2054b38ff370SJamie Gritton } 2055b38ff370SJamie Gritton } 2056b38ff370SJamie Gritton vfs_freeopts(opts); 2057b38ff370SJamie Gritton return (error); 2058b38ff370SJamie Gritton } 2059b38ff370SJamie Gritton 20600304c731SJamie Gritton 2061b38ff370SJamie Gritton /* 2062b38ff370SJamie Gritton * struct jail_remove_args { 2063b38ff370SJamie Gritton * int jid; 2064b38ff370SJamie Gritton * }; 2065b38ff370SJamie Gritton */ 2066b38ff370SJamie Gritton int 2067b38ff370SJamie Gritton jail_remove(struct thread *td, struct jail_remove_args *uap) 2068b38ff370SJamie Gritton { 20690304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 20700304c731SJamie Gritton int descend, error; 2071b38ff370SJamie Gritton 2072b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2073b38ff370SJamie Gritton if (error) 2074b38ff370SJamie Gritton return (error); 2075b38ff370SJamie Gritton 2076b38ff370SJamie Gritton sx_xlock(&allprison_lock); 20770304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2078b38ff370SJamie Gritton if (pr == NULL) { 2079b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2080b38ff370SJamie Gritton return (EINVAL); 2081b38ff370SJamie Gritton } 2082b38ff370SJamie Gritton 20830304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 20840304c731SJamie Gritton pr->pr_ref++; 20850304c731SJamie Gritton pr->pr_flags |= PR_REMOVE; 20860304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 20870304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20880304c731SJamie Gritton lpr = NULL; 20890304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 20900304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 20910304c731SJamie Gritton if (cpr->pr_ref > 0) { 20920304c731SJamie Gritton tpr = cpr; 20930304c731SJamie Gritton cpr->pr_ref++; 20940304c731SJamie Gritton cpr->pr_flags |= PR_REMOVE; 20950304c731SJamie Gritton } else { 20960304c731SJamie Gritton /* Already removed - do not do it again. */ 20970304c731SJamie Gritton tpr = NULL; 20980304c731SJamie Gritton } 20990304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 21000304c731SJamie Gritton if (lpr != NULL) { 21010304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 21020304c731SJamie Gritton prison_remove_one(lpr); 21030304c731SJamie Gritton sx_xlock(&allprison_lock); 21040304c731SJamie Gritton } 21050304c731SJamie Gritton lpr = tpr; 21060304c731SJamie Gritton } 21070304c731SJamie Gritton if (lpr != NULL) { 21080304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 21090304c731SJamie Gritton prison_remove_one(lpr); 21100304c731SJamie Gritton sx_xlock(&allprison_lock); 21110304c731SJamie Gritton } 21120304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 21130304c731SJamie Gritton } 21140304c731SJamie Gritton prison_remove_one(pr); 21150304c731SJamie Gritton return (0); 21160304c731SJamie Gritton } 21170304c731SJamie Gritton 21180304c731SJamie Gritton static void 21190304c731SJamie Gritton prison_remove_one(struct prison *pr) 21200304c731SJamie Gritton { 21210304c731SJamie Gritton struct proc *p; 21220304c731SJamie Gritton int deuref; 21230304c731SJamie Gritton 2124b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2125b38ff370SJamie Gritton deuref = 0; 2126b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2127b38ff370SJamie Gritton pr->pr_ref--; 2128b38ff370SJamie Gritton deuref = PD_DEUREF; 2129b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2130b38ff370SJamie Gritton } 2131b38ff370SJamie Gritton 21320304c731SJamie Gritton /* 21330304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 21340304c731SJamie Gritton * the prison now. 21350304c731SJamie Gritton */ 21360304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 21370304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 21380304c731SJamie Gritton if (pr->pr_ref == 1) { 2139b38ff370SJamie Gritton prison_deref(pr, 2140b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 21410304c731SJamie Gritton return; 2142b38ff370SJamie Gritton } 2143b38ff370SJamie Gritton 2144b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2145b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2146b38ff370SJamie Gritton /* 2147b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2148b38ff370SJamie Gritton */ 2149b38ff370SJamie Gritton sx_slock(&allproc_lock); 2150b38ff370SJamie Gritton LIST_FOREACH(p, &allproc, p_list) { 2151b38ff370SJamie Gritton PROC_LOCK(p); 2152b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2153b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 2154b38ff370SJamie Gritton psignal(p, SIGKILL); 2155b38ff370SJamie Gritton PROC_UNLOCK(p); 2156b38ff370SJamie Gritton } 2157b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 21580304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2159b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 216075c13541SPoul-Henning Kamp } 216175c13541SPoul-Henning Kamp 21628571af59SJamie Gritton 2163fd7a8150SMike Barcroft /* 21649ddb7954SMike Barcroft * struct jail_attach_args { 21659ddb7954SMike Barcroft * int jid; 21669ddb7954SMike Barcroft * }; 2167fd7a8150SMike Barcroft */ 2168fd7a8150SMike Barcroft int 21699ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap) 2170fd7a8150SMike Barcroft { 2171b38ff370SJamie Gritton struct prison *pr; 2172b38ff370SJamie Gritton int error; 2173b38ff370SJamie Gritton 2174b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2175b38ff370SJamie Gritton if (error) 2176b38ff370SJamie Gritton return (error); 2177b38ff370SJamie Gritton 2178b38ff370SJamie Gritton sx_slock(&allprison_lock); 21790304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2180b38ff370SJamie Gritton if (pr == NULL) { 2181b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2182b38ff370SJamie Gritton return (EINVAL); 2183b38ff370SJamie Gritton } 2184b38ff370SJamie Gritton 2185b38ff370SJamie Gritton /* 2186b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2187b38ff370SJamie Gritton * considered to be "alive". 2188b38ff370SJamie Gritton */ 2189b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2190b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2191b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2192b38ff370SJamie Gritton return (EINVAL); 2193b38ff370SJamie Gritton } 2194b38ff370SJamie Gritton 2195b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2196b38ff370SJamie Gritton } 2197b38ff370SJamie Gritton 2198b38ff370SJamie Gritton static int 2199b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2200b38ff370SJamie Gritton { 22010304c731SJamie Gritton struct prison *ppr; 2202fd7a8150SMike Barcroft struct proc *p; 2203fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 2204453f7d53SChristian S.J. Peron int vfslocked, error; 2205fd7a8150SMike Barcroft 220657f22bd4SJacques Vidrine /* 220757f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 220857f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 220957f22bd4SJacques Vidrine * different jails at the same time. It is important for 221057f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 221157f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 221257f22bd4SJacques Vidrine * of another. 221357f22bd4SJacques Vidrine */ 2214fd7a8150SMike Barcroft pr->pr_ref++; 2215b38ff370SJamie Gritton pr->pr_uref++; 2216fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2217b38ff370SJamie Gritton 2218b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2219b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2220b38ff370SJamie Gritton if (error) { 2221b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2222b38ff370SJamie Gritton return (error); 2223b38ff370SJamie Gritton } 2224dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2225fd7a8150SMike Barcroft 2226413628a7SBjoern A. Zeeb /* 2227413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2228413628a7SBjoern A. Zeeb */ 22290304c731SJamie Gritton ppr = td->td_ucred->cr_prison; 2230b38ff370SJamie Gritton p = td->td_proc; 2231413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2232413628a7SBjoern A. Zeeb if (error) 2233b38ff370SJamie Gritton goto e_revert_osd; 2234413628a7SBjoern A. Zeeb 2235453f7d53SChristian S.J. Peron vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2236cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2237fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2238fd7a8150SMike Barcroft goto e_unlock; 2239fd7a8150SMike Barcroft #ifdef MAC 224030d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2241fd7a8150SMike Barcroft goto e_unlock; 2242fd7a8150SMike Barcroft #endif 224322db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2244b38ff370SJamie Gritton if ((error = change_root(pr->pr_root, td))) 2245b38ff370SJamie Gritton goto e_unlock_giant; 2246453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2247fd7a8150SMike Barcroft 2248fd7a8150SMike Barcroft newcred = crget(); 2249fd7a8150SMike Barcroft PROC_LOCK(p); 2250fd7a8150SMike Barcroft oldcred = p->p_ucred; 2251fd7a8150SMike Barcroft setsugid(p); 2252fd7a8150SMike Barcroft crcopy(newcred, oldcred); 225369c4ee54SJohn Baldwin newcred->cr_prison = pr; 2254fd7a8150SMike Barcroft p->p_ucred = newcred; 2255fd7a8150SMike Barcroft PROC_UNLOCK(p); 2256fd7a8150SMike Barcroft crfree(oldcred); 22570304c731SJamie Gritton prison_deref(ppr, PD_DEREF | PD_DEUREF); 2258fd7a8150SMike Barcroft return (0); 2259fd7a8150SMike Barcroft e_unlock: 226022db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2261b38ff370SJamie Gritton e_unlock_giant: 2262453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2263b38ff370SJamie Gritton e_revert_osd: 2264b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 22650304c731SJamie Gritton (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2266b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2267fd7a8150SMike Barcroft return (error); 2268fd7a8150SMike Barcroft } 2269fd7a8150SMike Barcroft 22700304c731SJamie Gritton 2271fd7a8150SMike Barcroft /* 2272fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2273fd7a8150SMike Barcroft */ 227454b369c1SPawel Jakub Dawidek struct prison * 2275fd7a8150SMike Barcroft prison_find(int prid) 2276fd7a8150SMike Barcroft { 2277fd7a8150SMike Barcroft struct prison *pr; 2278fd7a8150SMike Barcroft 2279dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2280b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2281fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2282fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2283b38ff370SJamie Gritton if (pr->pr_ref > 0) 2284fd7a8150SMike Barcroft return (pr); 2285b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2286fd7a8150SMike Barcroft } 2287fd7a8150SMike Barcroft } 2288fd7a8150SMike Barcroft return (NULL); 2289fd7a8150SMike Barcroft } 2290fd7a8150SMike Barcroft 2291b38ff370SJamie Gritton /* 22920304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2293b38ff370SJamie Gritton */ 2294b38ff370SJamie Gritton struct prison * 22950304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2296b38ff370SJamie Gritton { 22970304c731SJamie Gritton struct prison *pr; 22980304c731SJamie Gritton int descend; 2299b38ff370SJamie Gritton 2300b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 23010304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 23020304c731SJamie Gritton if (pr->pr_id == prid) { 23030304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 23040304c731SJamie Gritton if (pr->pr_ref > 0) 23050304c731SJamie Gritton return (pr); 23060304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 23070304c731SJamie Gritton } 23080304c731SJamie Gritton } 23090304c731SJamie Gritton return (NULL); 23100304c731SJamie Gritton } 23110304c731SJamie Gritton 23120304c731SJamie Gritton /* 23130304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 23140304c731SJamie Gritton */ 23150304c731SJamie Gritton struct prison * 23160304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 23170304c731SJamie Gritton { 23180304c731SJamie Gritton struct prison *pr, *deadpr; 23190304c731SJamie Gritton size_t mylen; 23200304c731SJamie Gritton int descend; 23210304c731SJamie Gritton 23220304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 23230304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2324b38ff370SJamie Gritton again: 2325b38ff370SJamie Gritton deadpr = NULL; 23260304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 23270304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2328b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2329b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2330b38ff370SJamie Gritton if (pr->pr_uref > 0) 2331b38ff370SJamie Gritton return (pr); 2332b38ff370SJamie Gritton deadpr = pr; 2333b38ff370SJamie Gritton } 2334b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2335b38ff370SJamie Gritton } 2336b38ff370SJamie Gritton } 23370304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2338b38ff370SJamie Gritton if (deadpr != NULL) { 2339b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2340b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2341b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2342b38ff370SJamie Gritton goto again; 2343b38ff370SJamie Gritton } 2344b38ff370SJamie Gritton } 2345b38ff370SJamie Gritton return (deadpr); 2346b38ff370SJamie Gritton } 2347b38ff370SJamie Gritton 2348b38ff370SJamie Gritton /* 23490304c731SJamie Gritton * See if a prison has the specific flag set. 23500304c731SJamie Gritton */ 23510304c731SJamie Gritton int 23520304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 23530304c731SJamie Gritton { 23540304c731SJamie Gritton 23550304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 23560304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 23570304c731SJamie Gritton } 23580304c731SJamie Gritton 23590304c731SJamie Gritton int 23600304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 23610304c731SJamie Gritton { 23620304c731SJamie Gritton 23630304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 23640304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 23650304c731SJamie Gritton } 23660304c731SJamie Gritton 23670304c731SJamie Gritton /* 2368b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2369b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2370b38ff370SJamie Gritton */ 237191421ba2SRobert Watson void 23721ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 237391421ba2SRobert Watson { 237491421ba2SRobert Watson 23751ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 237691421ba2SRobert Watson pr->pr_ref--; 237791421ba2SRobert Watson if (pr->pr_ref == 0) { 237801137630SRobert Watson mtx_unlock(&pr->pr_mtx); 2379c2cda609SPawel Jakub Dawidek TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2380c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2381c2cda609SPawel Jakub Dawidek return; 2382c2cda609SPawel Jakub Dawidek } 2383c2cda609SPawel Jakub Dawidek mtx_unlock(&pr->pr_mtx); 2384c2cda609SPawel Jakub Dawidek } 2385c2cda609SPawel Jakub Dawidek 23861ba4a712SPawel Jakub Dawidek void 23871ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 23881ba4a712SPawel Jakub Dawidek { 23891ba4a712SPawel Jakub Dawidek 23901ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 23911ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 23921ba4a712SPawel Jakub Dawidek } 23931ba4a712SPawel Jakub Dawidek 2394c2cda609SPawel Jakub Dawidek static void 2395c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2396c2cda609SPawel Jakub Dawidek { 2397b38ff370SJamie Gritton 2398b38ff370SJamie Gritton prison_deref((struct prison *)context, 0); 2399b38ff370SJamie Gritton } 2400b38ff370SJamie Gritton 2401b38ff370SJamie Gritton /* 2402b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2403b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2404b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2405b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2406b38ff370SJamie Gritton */ 2407b38ff370SJamie Gritton static void 2408b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2409b38ff370SJamie Gritton { 24100304c731SJamie Gritton struct prison *ppr, *tpr; 2411c2cda609SPawel Jakub Dawidek int vfslocked; 2412c2cda609SPawel Jakub Dawidek 2413b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2414b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 24150304c731SJamie Gritton /* Decrement the user references in a separate loop. */ 2416b38ff370SJamie Gritton if (flags & PD_DEUREF) { 24170304c731SJamie Gritton for (tpr = pr;; tpr = tpr->pr_parent) { 24180304c731SJamie Gritton if (tpr != pr) 24190304c731SJamie Gritton mtx_lock(&tpr->pr_mtx); 24200304c731SJamie Gritton if (--tpr->pr_uref > 0) 24210304c731SJamie Gritton break; 24220304c731SJamie Gritton KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 24230304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 24240304c731SJamie Gritton } 2425b38ff370SJamie Gritton /* Done if there were only user references to remove. */ 2426b38ff370SJamie Gritton if (!(flags & PD_DEREF)) { 24270304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 2428b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2429b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2430b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2431b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2432b38ff370SJamie Gritton return; 2433b38ff370SJamie Gritton } 24340304c731SJamie Gritton if (tpr != pr) { 24350304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 24360304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2437b38ff370SJamie Gritton } 24380304c731SJamie Gritton } 24390304c731SJamie Gritton 24400304c731SJamie Gritton for (;;) { 2441b38ff370SJamie Gritton if (flags & PD_DEREF) 2442b38ff370SJamie Gritton pr->pr_ref--; 2443b38ff370SJamie Gritton /* If the prison still has references, nothing else to do. */ 2444b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2445b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2446b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2447b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2448b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2449b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2450b38ff370SJamie Gritton return; 2451b38ff370SJamie Gritton } 2452c2cda609SPawel Jakub Dawidek 2453b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2454b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2455b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2456b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2457c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2458b38ff370SJamie Gritton } 2459b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2460b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2461b38ff370SJamie Gritton 2462b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 24630304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 24640304c731SJamie Gritton ppr = pr->pr_parent; 24650304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2466b97457e2SJamie Gritton tpr->pr_childcount--; 2467c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 24681ba4a712SPawel Jakub Dawidek 2469679e1390SJamie Gritton #ifdef VIMAGE 24700cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2471679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2472679e1390SJamie Gritton #endif 2473b38ff370SJamie Gritton if (pr->pr_root != NULL) { 2474453f7d53SChristian S.J. Peron vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2475b3059e09SRobert Watson vrele(pr->pr_root); 2476453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2477b38ff370SJamie Gritton } 2478b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2479413628a7SBjoern A. Zeeb #ifdef INET 2480413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2481413628a7SBjoern A. Zeeb #endif 2482b38ff370SJamie Gritton #ifdef INET6 2483b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2484b38ff370SJamie Gritton #endif 2485b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2486b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2487b38ff370SJamie Gritton osd_jail_exit(pr); 24881ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 24890304c731SJamie Gritton 24900304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 24910304c731SJamie Gritton pr = ppr; 24920304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2493c4884ffaSJamie Gritton flags = PD_DEREF; 24940304c731SJamie Gritton } 2495b3059e09SRobert Watson } 2496b3059e09SRobert Watson 249791421ba2SRobert Watson void 24981ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 24991ba4a712SPawel Jakub Dawidek { 25001ba4a712SPawel Jakub Dawidek 25011ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 25021ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 2503af7bd9a4SJamie Gritton ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 25041ba4a712SPawel Jakub Dawidek pr->pr_ref++; 25051ba4a712SPawel Jakub Dawidek } 25061ba4a712SPawel Jakub Dawidek 25071ba4a712SPawel Jakub Dawidek void 250891421ba2SRobert Watson prison_hold(struct prison *pr) 250991421ba2SRobert Watson { 251091421ba2SRobert Watson 251101137630SRobert Watson mtx_lock(&pr->pr_mtx); 25121ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 251301137630SRobert Watson mtx_unlock(&pr->pr_mtx); 251401137630SRobert Watson } 251501137630SRobert Watson 2516413628a7SBjoern A. Zeeb void 2517413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 251801137630SRobert Watson { 251901137630SRobert Watson 2520413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2521b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2522b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2523b38ff370SJamie Gritton pr->pr_uref++; 2524413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 252575c13541SPoul-Henning Kamp } 252675c13541SPoul-Henning Kamp 252775c13541SPoul-Henning Kamp void 2528413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 252975c13541SPoul-Henning Kamp { 2530413628a7SBjoern A. Zeeb 2531413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2532b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2533b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2534b38ff370SJamie Gritton prison_deref(pr, PD_DEUREF | PD_LOCKED); 2535413628a7SBjoern A. Zeeb } 2536413628a7SBjoern A. Zeeb 2537413628a7SBjoern A. Zeeb 2538413628a7SBjoern A. Zeeb #ifdef INET 2539413628a7SBjoern A. Zeeb /* 25400304c731SJamie Gritton * Restrict a prison's IP address list with its parent's, possibly replacing 25410304c731SJamie Gritton * it. Return true if the replacement buffer was used (or would have been). 25420304c731SJamie Gritton */ 25430304c731SJamie Gritton static int 25440304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 25450304c731SJamie Gritton { 25460304c731SJamie Gritton int ii, ij, used; 25470304c731SJamie Gritton struct prison *ppr; 25480304c731SJamie Gritton 25490304c731SJamie Gritton ppr = pr->pr_parent; 25500304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4_USER)) { 25510304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 25520304c731SJamie Gritton if (pr->pr_ip4s < ppr->pr_ip4s) { 25530304c731SJamie Gritton /* 25540304c731SJamie Gritton * There's no room for the parent's list. Use the 25550304c731SJamie Gritton * new list buffer, which is assumed to be big enough 25560304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 25570304c731SJamie Gritton * allocate one. 25580304c731SJamie Gritton */ 25590304c731SJamie Gritton used = 1; 25600304c731SJamie Gritton if (newip4 == NULL) { 25610304c731SJamie Gritton newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 25620304c731SJamie Gritton M_PRISON, M_NOWAIT); 25630304c731SJamie Gritton if (newip4 != NULL) 25640304c731SJamie Gritton used = 0; 25650304c731SJamie Gritton } 25660304c731SJamie Gritton if (newip4 != NULL) { 25670304c731SJamie Gritton bcopy(ppr->pr_ip4, newip4, 25680304c731SJamie Gritton ppr->pr_ip4s * sizeof(*newip4)); 25690304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 25700304c731SJamie Gritton pr->pr_ip4 = newip4; 25710304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 25720304c731SJamie Gritton } 25730304c731SJamie Gritton return (used); 25740304c731SJamie Gritton } 25750304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 25760304c731SJamie Gritton if (pr->pr_ip4s > 0) 25770304c731SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 25780304c731SJamie Gritton pr->pr_ip4s * sizeof(*newip4)); 25790304c731SJamie Gritton else if (pr->pr_ip4 != NULL) { 25800304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 25810304c731SJamie Gritton pr->pr_ip4 = NULL; 25820304c731SJamie Gritton } 25832b0d6f81SJamie Gritton } else if (pr->pr_ip4s > 0) { 25840304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 25850304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 25860304c731SJamie Gritton if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 25870304c731SJamie Gritton break; 25880304c731SJamie Gritton if (ij < ppr->pr_ip4s) 25890304c731SJamie Gritton ii = 1; 25900304c731SJamie Gritton else { 25910304c731SJamie Gritton bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 25920304c731SJamie Gritton --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 25930304c731SJamie Gritton ii = 0; 25940304c731SJamie Gritton } 25950304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip4s; ) { 25960304c731SJamie Gritton if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 25970304c731SJamie Gritton ii++; 25980304c731SJamie Gritton continue; 25990304c731SJamie Gritton } 26000304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 26010304c731SJamie Gritton qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 26020304c731SJamie Gritton case -1: 26030304c731SJamie Gritton bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 26040304c731SJamie Gritton (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 26050304c731SJamie Gritton break; 26060304c731SJamie Gritton case 0: 26070304c731SJamie Gritton ii++; 26080304c731SJamie Gritton ij++; 26090304c731SJamie Gritton break; 26100304c731SJamie Gritton case 1: 26110304c731SJamie Gritton ij++; 26120304c731SJamie Gritton break; 26130304c731SJamie Gritton } 26140304c731SJamie Gritton } 26150304c731SJamie Gritton if (pr->pr_ip4s == 0) { 26167cbf7213SJamie Gritton pr->pr_flags |= PR_IP4_DISABLE; 26170304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 26180304c731SJamie Gritton pr->pr_ip4 = NULL; 26190304c731SJamie Gritton } 26200304c731SJamie Gritton } 26210304c731SJamie Gritton return (0); 26220304c731SJamie Gritton } 26230304c731SJamie Gritton 26240304c731SJamie Gritton /* 2625413628a7SBjoern A. Zeeb * Pass back primary IPv4 address of this jail. 2626413628a7SBjoern A. Zeeb * 26270304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 26280304c731SJamie Gritton * to make sure to initialize it correctly (e.g. INADDR_ANY). 2629413628a7SBjoern A. Zeeb * 2630b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2631b89e82ddSJamie Gritton * Address returned in NBO. 2632413628a7SBjoern A. Zeeb */ 2633413628a7SBjoern A. Zeeb int 26341cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia) 2635413628a7SBjoern A. Zeeb { 2636b38ff370SJamie Gritton struct prison *pr; 2637413628a7SBjoern A. Zeeb 2638413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2639413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 264075c13541SPoul-Henning Kamp 2641b38ff370SJamie Gritton pr = cred->cr_prison; 26420304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 26430304c731SJamie Gritton return (0); 2644b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 26450304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 26460304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 26470304c731SJamie Gritton return (0); 26480304c731SJamie Gritton } 2649b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2650b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2651b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2652b38ff370SJamie Gritton } 2653413628a7SBjoern A. Zeeb 2654b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2655b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2656413628a7SBjoern A. Zeeb return (0); 265775c13541SPoul-Henning Kamp } 2658413628a7SBjoern A. Zeeb 2659413628a7SBjoern A. Zeeb /* 26600304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv4 address restrictions. 26610304c731SJamie Gritton */ 26620304c731SJamie Gritton int 26630304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2) 26640304c731SJamie Gritton { 26650304c731SJamie Gritton 26660304c731SJamie Gritton if (pr1 == pr2) 26670304c731SJamie Gritton return (1); 26680304c731SJamie Gritton 26690304c731SJamie Gritton /* 26702b0d6f81SJamie Gritton * No need to lock since the PR_IP4_USER flag can't be altered for 26712b0d6f81SJamie Gritton * existing prisons. 26720304c731SJamie Gritton */ 2673bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 2674bdfc8cc4SJamie Gritton #ifdef VIMAGE 2675bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 2676bdfc8cc4SJamie Gritton #endif 2677bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP4_USER)) 26780304c731SJamie Gritton pr1 = pr1->pr_parent; 2679bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 2680bdfc8cc4SJamie Gritton #ifdef VIMAGE 2681bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 2682bdfc8cc4SJamie Gritton #endif 2683bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP4_USER)) 26840304c731SJamie Gritton pr2 = pr2->pr_parent; 26850304c731SJamie Gritton return (pr1 == pr2); 26860304c731SJamie Gritton } 26870304c731SJamie Gritton 26880304c731SJamie Gritton /* 2689413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this 2690413628a7SBjoern A. Zeeb * jail. 2691413628a7SBjoern A. Zeeb * 26920304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 26930304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 26940304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2695413628a7SBjoern A. Zeeb */ 2696413628a7SBjoern A. Zeeb int 2697413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia) 2698413628a7SBjoern A. Zeeb { 2699b38ff370SJamie Gritton struct prison *pr; 2700413628a7SBjoern A. Zeeb struct in_addr ia0; 2701b38ff370SJamie Gritton int error; 2702413628a7SBjoern A. Zeeb 2703413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2704413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2705413628a7SBjoern A. Zeeb 2706b38ff370SJamie Gritton pr = cred->cr_prison; 27070304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 27080304c731SJamie Gritton return (0); 2709b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 27100304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 27110304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27120304c731SJamie Gritton return (0); 27130304c731SJamie Gritton } 2714b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2715b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2716b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2717b38ff370SJamie Gritton } 2718413628a7SBjoern A. Zeeb 2719413628a7SBjoern A. Zeeb ia0.s_addr = ntohl(ia->s_addr); 2720413628a7SBjoern A. Zeeb if (ia0.s_addr == INADDR_LOOPBACK) { 2721b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2722b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2723413628a7SBjoern A. Zeeb return (0); 2724413628a7SBjoern A. Zeeb } 2725413628a7SBjoern A. Zeeb 2726b89e82ddSJamie Gritton if (ia0.s_addr == INADDR_ANY) { 2727413628a7SBjoern A. Zeeb /* 2728413628a7SBjoern A. Zeeb * In case there is only 1 IPv4 address, bind directly. 2729413628a7SBjoern A. Zeeb */ 2730b38ff370SJamie Gritton if (pr->pr_ip4s == 1) 2731b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2732b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2733413628a7SBjoern A. Zeeb return (0); 2734413628a7SBjoern A. Zeeb } 2735413628a7SBjoern A. Zeeb 2736b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2737b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2738b38ff370SJamie Gritton return (error); 2739413628a7SBjoern A. Zeeb } 2740413628a7SBjoern A. Zeeb 2741413628a7SBjoern A. Zeeb /* 2742413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 2743413628a7SBjoern A. Zeeb * 2744b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2745b89e82ddSJamie Gritton * Address passed in in NBO and returned in NBO. 2746413628a7SBjoern A. Zeeb */ 2747413628a7SBjoern A. Zeeb int 2748413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2749413628a7SBjoern A. Zeeb { 2750b38ff370SJamie Gritton struct prison *pr; 2751413628a7SBjoern A. Zeeb 2752413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2753413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2754413628a7SBjoern A. Zeeb 2755b38ff370SJamie Gritton pr = cred->cr_prison; 27560304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 27570304c731SJamie Gritton return (0); 2758b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 27590304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 27600304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27610304c731SJamie Gritton return (0); 27620304c731SJamie Gritton } 2763b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2764b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2765b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2766b38ff370SJamie Gritton } 2767b89e82ddSJamie Gritton 2768413628a7SBjoern A. Zeeb if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2769b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2770b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2771413628a7SBjoern A. Zeeb return (0); 2772413628a7SBjoern A. Zeeb } 2773413628a7SBjoern A. Zeeb 2774413628a7SBjoern A. Zeeb /* 2775413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 2776413628a7SBjoern A. Zeeb */ 2777b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2778413628a7SBjoern A. Zeeb return (0); 2779413628a7SBjoern A. Zeeb } 2780413628a7SBjoern A. Zeeb 2781413628a7SBjoern A. Zeeb /* 2782b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 2783413628a7SBjoern A. Zeeb * 27840304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 27850304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 27860304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO. 2787413628a7SBjoern A. Zeeb */ 2788413628a7SBjoern A. Zeeb static int 2789413628a7SBjoern A. Zeeb _prison_check_ip4(struct prison *pr, struct in_addr *ia) 2790413628a7SBjoern A. Zeeb { 2791413628a7SBjoern A. Zeeb int i, a, z, d; 2792413628a7SBjoern A. Zeeb 2793413628a7SBjoern A. Zeeb /* 2794413628a7SBjoern A. Zeeb * Check the primary IP. 2795413628a7SBjoern A. Zeeb */ 2796413628a7SBjoern A. Zeeb if (pr->pr_ip4[0].s_addr == ia->s_addr) 2797b89e82ddSJamie Gritton return (0); 2798413628a7SBjoern A. Zeeb 2799413628a7SBjoern A. Zeeb /* 2800413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 2801413628a7SBjoern A. Zeeb */ 2802413628a7SBjoern A. Zeeb a = 0; 2803413628a7SBjoern A. Zeeb z = pr->pr_ip4s - 2; 2804413628a7SBjoern A. Zeeb while (a <= z) { 2805413628a7SBjoern A. Zeeb i = (a + z) / 2; 2806413628a7SBjoern A. Zeeb d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2807413628a7SBjoern A. Zeeb if (d > 0) 2808413628a7SBjoern A. Zeeb z = i - 1; 2809413628a7SBjoern A. Zeeb else if (d < 0) 2810413628a7SBjoern A. Zeeb a = i + 1; 2811413628a7SBjoern A. Zeeb else 2812413628a7SBjoern A. Zeeb return (0); 281375c13541SPoul-Henning Kamp } 281475c13541SPoul-Henning Kamp 2815b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 2816b89e82ddSJamie Gritton } 2817b89e82ddSJamie Gritton 281875c13541SPoul-Henning Kamp int 2819413628a7SBjoern A. Zeeb prison_check_ip4(struct ucred *cred, struct in_addr *ia) 2820413628a7SBjoern A. Zeeb { 2821b38ff370SJamie Gritton struct prison *pr; 2822b38ff370SJamie Gritton int error; 2823413628a7SBjoern A. Zeeb 2824413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2825413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2826413628a7SBjoern A. Zeeb 2827b38ff370SJamie Gritton pr = cred->cr_prison; 28280304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 28290304c731SJamie Gritton return (0); 2830b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 28310304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 28320304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28330304c731SJamie Gritton return (0); 28340304c731SJamie Gritton } 2835b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2836b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2837b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2838b38ff370SJamie Gritton } 2839413628a7SBjoern A. Zeeb 2840b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2841b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2842b38ff370SJamie Gritton return (error); 2843413628a7SBjoern A. Zeeb } 2844413628a7SBjoern A. Zeeb #endif 2845413628a7SBjoern A. Zeeb 2846413628a7SBjoern A. Zeeb #ifdef INET6 28470304c731SJamie Gritton static int 28480304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 28490304c731SJamie Gritton { 28500304c731SJamie Gritton int ii, ij, used; 28510304c731SJamie Gritton struct prison *ppr; 28520304c731SJamie Gritton 28530304c731SJamie Gritton ppr = pr->pr_parent; 28540304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6_USER)) { 28550304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 28560304c731SJamie Gritton if (pr->pr_ip6s < ppr->pr_ip6s) { 28570304c731SJamie Gritton /* 28580304c731SJamie Gritton * There's no room for the parent's list. Use the 28590304c731SJamie Gritton * new list buffer, which is assumed to be big enough 28600304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 28610304c731SJamie Gritton * allocate one. 28620304c731SJamie Gritton */ 28630304c731SJamie Gritton used = 1; 28640304c731SJamie Gritton if (newip6 == NULL) { 28650304c731SJamie Gritton newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 28660304c731SJamie Gritton M_PRISON, M_NOWAIT); 28670304c731SJamie Gritton if (newip6 != NULL) 28680304c731SJamie Gritton used = 0; 28690304c731SJamie Gritton } 28700304c731SJamie Gritton if (newip6 != NULL) { 28710304c731SJamie Gritton bcopy(ppr->pr_ip6, newip6, 28720304c731SJamie Gritton ppr->pr_ip6s * sizeof(*newip6)); 28730304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 28740304c731SJamie Gritton pr->pr_ip6 = newip6; 28750304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 28760304c731SJamie Gritton } 28770304c731SJamie Gritton return (used); 28780304c731SJamie Gritton } 28790304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 28800304c731SJamie Gritton if (pr->pr_ip6s > 0) 28810304c731SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 28820304c731SJamie Gritton pr->pr_ip6s * sizeof(*newip6)); 28830304c731SJamie Gritton else if (pr->pr_ip6 != NULL) { 28840304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 28850304c731SJamie Gritton pr->pr_ip6 = NULL; 28860304c731SJamie Gritton } 28872b0d6f81SJamie Gritton } else if (pr->pr_ip6s > 0) { 28880304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 28890304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 28900304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 28910304c731SJamie Gritton &ppr->pr_ip6[ij])) 28920304c731SJamie Gritton break; 28930304c731SJamie Gritton if (ij < ppr->pr_ip6s) 28940304c731SJamie Gritton ii = 1; 28950304c731SJamie Gritton else { 28960304c731SJamie Gritton bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 28970304c731SJamie Gritton --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 28980304c731SJamie Gritton ii = 0; 28990304c731SJamie Gritton } 29000304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip6s; ) { 29010304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 29020304c731SJamie Gritton &ppr->pr_ip6[0])) { 29030304c731SJamie Gritton ii++; 29040304c731SJamie Gritton continue; 29050304c731SJamie Gritton } 29060304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 29070304c731SJamie Gritton qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 29080304c731SJamie Gritton case -1: 29090304c731SJamie Gritton bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 29100304c731SJamie Gritton (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 29110304c731SJamie Gritton break; 29120304c731SJamie Gritton case 0: 29130304c731SJamie Gritton ii++; 29140304c731SJamie Gritton ij++; 29150304c731SJamie Gritton break; 29160304c731SJamie Gritton case 1: 29170304c731SJamie Gritton ij++; 29180304c731SJamie Gritton break; 29190304c731SJamie Gritton } 29200304c731SJamie Gritton } 29210304c731SJamie Gritton if (pr->pr_ip6s == 0) { 29227cbf7213SJamie Gritton pr->pr_flags |= PR_IP6_DISABLE; 29230304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 29240304c731SJamie Gritton pr->pr_ip6 = NULL; 29250304c731SJamie Gritton } 29260304c731SJamie Gritton } 29270304c731SJamie Gritton return 0; 29280304c731SJamie Gritton } 29290304c731SJamie Gritton 2930413628a7SBjoern A. Zeeb /* 2931413628a7SBjoern A. Zeeb * Pass back primary IPv6 address for this jail. 2932413628a7SBjoern A. Zeeb * 29330304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 29340304c731SJamie Gritton * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 2935413628a7SBjoern A. Zeeb * 2936b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 2937413628a7SBjoern A. Zeeb */ 2938413628a7SBjoern A. Zeeb int 29391cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 2940413628a7SBjoern A. Zeeb { 2941b38ff370SJamie Gritton struct prison *pr; 2942413628a7SBjoern A. Zeeb 2943413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2944413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 2945413628a7SBjoern A. Zeeb 2946b38ff370SJamie Gritton pr = cred->cr_prison; 29470304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 29480304c731SJamie Gritton return (0); 2949b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 29500304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 29510304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 29520304c731SJamie Gritton return (0); 29530304c731SJamie Gritton } 2954b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 2955b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2956b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2957b38ff370SJamie Gritton } 2958b89e82ddSJamie Gritton 2959b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 2960b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2961413628a7SBjoern A. Zeeb return (0); 2962413628a7SBjoern A. Zeeb } 2963413628a7SBjoern A. Zeeb 2964413628a7SBjoern A. Zeeb /* 29650304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv6 address restrictions. 29660304c731SJamie Gritton */ 29670304c731SJamie Gritton int 29680304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2) 29690304c731SJamie Gritton { 29700304c731SJamie Gritton 29710304c731SJamie Gritton if (pr1 == pr2) 29720304c731SJamie Gritton return (1); 29730304c731SJamie Gritton 2974bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 2975bdfc8cc4SJamie Gritton #ifdef VIMAGE 2976bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 2977bdfc8cc4SJamie Gritton #endif 2978bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP6_USER)) 29790304c731SJamie Gritton pr1 = pr1->pr_parent; 2980bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 2981bdfc8cc4SJamie Gritton #ifdef VIMAGE 2982bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 2983bdfc8cc4SJamie Gritton #endif 2984bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP6_USER)) 29850304c731SJamie Gritton pr2 = pr2->pr_parent; 29860304c731SJamie Gritton return (pr1 == pr2); 29870304c731SJamie Gritton } 29880304c731SJamie Gritton 29890304c731SJamie Gritton /* 2990413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this jail. 2991413628a7SBjoern A. Zeeb * 2992413628a7SBjoern A. Zeeb * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 2993413628a7SBjoern A. Zeeb * when needed while binding. 2994413628a7SBjoern A. Zeeb * 29950304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 29960304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 29970304c731SJamie Gritton * doesn't allow IPv6. 2998413628a7SBjoern A. Zeeb */ 2999413628a7SBjoern A. Zeeb int 3000413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3001413628a7SBjoern A. Zeeb { 3002b38ff370SJamie Gritton struct prison *pr; 3003b38ff370SJamie Gritton int error; 3004413628a7SBjoern A. Zeeb 3005413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3006413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3007413628a7SBjoern A. Zeeb 3008b38ff370SJamie Gritton pr = cred->cr_prison; 30090304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 30100304c731SJamie Gritton return (0); 3011b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 30120304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 30130304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 30140304c731SJamie Gritton return (0); 30150304c731SJamie Gritton } 3016b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3017b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3018b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3019b38ff370SJamie Gritton } 3020b89e82ddSJamie Gritton 3021413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3022b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3023b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3024413628a7SBjoern A. Zeeb return (0); 3025413628a7SBjoern A. Zeeb } 3026413628a7SBjoern A. Zeeb 3027b89e82ddSJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3028413628a7SBjoern A. Zeeb /* 3029b89e82ddSJamie Gritton * In case there is only 1 IPv6 address, and v6only is true, 3030b89e82ddSJamie Gritton * then bind directly. 3031413628a7SBjoern A. Zeeb */ 3032b38ff370SJamie Gritton if (v6only != 0 && pr->pr_ip6s == 1) 3033b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3034b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3035413628a7SBjoern A. Zeeb return (0); 3036413628a7SBjoern A. Zeeb } 3037b89e82ddSJamie Gritton 3038b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3039b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3040b38ff370SJamie Gritton return (error); 3041413628a7SBjoern A. Zeeb } 3042413628a7SBjoern A. Zeeb 3043413628a7SBjoern A. Zeeb /* 3044413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 3045413628a7SBjoern A. Zeeb * 3046b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3047413628a7SBjoern A. Zeeb */ 3048413628a7SBjoern A. Zeeb int 3049413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3050413628a7SBjoern A. Zeeb { 3051b38ff370SJamie Gritton struct prison *pr; 3052413628a7SBjoern A. Zeeb 3053413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3054413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3055413628a7SBjoern A. Zeeb 3056b38ff370SJamie Gritton pr = cred->cr_prison; 30570304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 30580304c731SJamie Gritton return (0); 3059b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 30600304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 30610304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 30620304c731SJamie Gritton return (0); 30630304c731SJamie Gritton } 3064b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3065b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3066b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3067b38ff370SJamie Gritton } 3068b89e82ddSJamie Gritton 3069413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3070b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3071b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3072413628a7SBjoern A. Zeeb return (0); 3073413628a7SBjoern A. Zeeb } 3074413628a7SBjoern A. Zeeb 3075413628a7SBjoern A. Zeeb /* 3076413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 3077413628a7SBjoern A. Zeeb */ 3078b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3079413628a7SBjoern A. Zeeb return (0); 3080413628a7SBjoern A. Zeeb } 3081413628a7SBjoern A. Zeeb 3082413628a7SBjoern A. Zeeb /* 3083b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 3084413628a7SBjoern A. Zeeb * 30850304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 30860304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 30870304c731SJamie Gritton * doesn't allow IPv6. 3088413628a7SBjoern A. Zeeb */ 3089413628a7SBjoern A. Zeeb static int 3090413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 3091413628a7SBjoern A. Zeeb { 3092413628a7SBjoern A. Zeeb int i, a, z, d; 3093413628a7SBjoern A. Zeeb 3094413628a7SBjoern A. Zeeb /* 3095413628a7SBjoern A. Zeeb * Check the primary IP. 3096413628a7SBjoern A. Zeeb */ 3097413628a7SBjoern A. Zeeb if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3098b89e82ddSJamie Gritton return (0); 3099413628a7SBjoern A. Zeeb 3100413628a7SBjoern A. Zeeb /* 3101413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 3102413628a7SBjoern A. Zeeb */ 3103413628a7SBjoern A. Zeeb a = 0; 3104413628a7SBjoern A. Zeeb z = pr->pr_ip6s - 2; 3105413628a7SBjoern A. Zeeb while (a <= z) { 3106413628a7SBjoern A. Zeeb i = (a + z) / 2; 3107413628a7SBjoern A. Zeeb d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3108413628a7SBjoern A. Zeeb if (d > 0) 3109413628a7SBjoern A. Zeeb z = i - 1; 3110413628a7SBjoern A. Zeeb else if (d < 0) 3111413628a7SBjoern A. Zeeb a = i + 1; 3112413628a7SBjoern A. Zeeb else 3113413628a7SBjoern A. Zeeb return (0); 3114413628a7SBjoern A. Zeeb } 3115413628a7SBjoern A. Zeeb 3116b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 3117b89e82ddSJamie Gritton } 3118b89e82ddSJamie Gritton 3119413628a7SBjoern A. Zeeb int 3120413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3121413628a7SBjoern A. Zeeb { 3122b38ff370SJamie Gritton struct prison *pr; 3123b38ff370SJamie Gritton int error; 3124413628a7SBjoern A. Zeeb 3125413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3126413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3127413628a7SBjoern A. Zeeb 3128b38ff370SJamie Gritton pr = cred->cr_prison; 31290304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 31300304c731SJamie Gritton return (0); 3131b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 31320304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 31330304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31340304c731SJamie Gritton return (0); 31350304c731SJamie Gritton } 3136b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3137b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3138b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3139b38ff370SJamie Gritton } 3140413628a7SBjoern A. Zeeb 3141b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3142b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3143b38ff370SJamie Gritton return (error); 3144413628a7SBjoern A. Zeeb } 3145413628a7SBjoern A. Zeeb #endif 3146413628a7SBjoern A. Zeeb 3147413628a7SBjoern A. Zeeb /* 3148ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3149ca04ba64SJamie Gritton * 3150ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3151ca04ba64SJamie Gritton * if not. 3152ca04ba64SJamie Gritton */ 3153ca04ba64SJamie Gritton int 3154ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3155ca04ba64SJamie Gritton { 31560304c731SJamie Gritton struct prison *pr; 3157ca04ba64SJamie Gritton int error; 3158ca04ba64SJamie Gritton 3159ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3160ca04ba64SJamie Gritton 31610304c731SJamie Gritton pr = cred->cr_prison; 3162499650a0SJamie Gritton #ifdef VIMAGE 31636bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 31646bb79563SJamie Gritton if (pr->pr_flags & PR_VNET) 31656bb79563SJamie Gritton return (0); 3166499650a0SJamie Gritton #endif 31676bb79563SJamie Gritton 3168ca04ba64SJamie Gritton error = 0; 3169ca04ba64SJamie Gritton switch (af) 3170ca04ba64SJamie Gritton { 3171ca04ba64SJamie Gritton #ifdef INET 3172ca04ba64SJamie Gritton case AF_INET: 31730304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 31740304c731SJamie Gritton { 31750304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 31760304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3177ca04ba64SJamie Gritton error = EAFNOSUPPORT; 31780304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31790304c731SJamie Gritton } 3180ca04ba64SJamie Gritton break; 3181ca04ba64SJamie Gritton #endif 3182ca04ba64SJamie Gritton #ifdef INET6 3183ca04ba64SJamie Gritton case AF_INET6: 31840304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 31850304c731SJamie Gritton { 31860304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 31870304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3188ca04ba64SJamie Gritton error = EAFNOSUPPORT; 31890304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31900304c731SJamie Gritton } 3191ca04ba64SJamie Gritton break; 3192ca04ba64SJamie Gritton #endif 3193ca04ba64SJamie Gritton case AF_LOCAL: 3194ca04ba64SJamie Gritton case AF_ROUTE: 3195ca04ba64SJamie Gritton break; 3196ca04ba64SJamie Gritton default: 31970304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3198ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3199ca04ba64SJamie Gritton } 3200ca04ba64SJamie Gritton return (error); 3201ca04ba64SJamie Gritton } 3202ca04ba64SJamie Gritton 3203ca04ba64SJamie Gritton /* 3204413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3205413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3206413628a7SBjoern A. Zeeb * 32070304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 32080304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 32090304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3210413628a7SBjoern A. Zeeb */ 3211413628a7SBjoern A. Zeeb int 321291421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa) 321375c13541SPoul-Henning Kamp { 3214413628a7SBjoern A. Zeeb #ifdef INET 32159ddb7954SMike Barcroft struct sockaddr_in *sai; 3216413628a7SBjoern A. Zeeb #endif 3217413628a7SBjoern A. Zeeb #ifdef INET6 3218413628a7SBjoern A. Zeeb struct sockaddr_in6 *sai6; 3219413628a7SBjoern A. Zeeb #endif 3220b89e82ddSJamie Gritton int error; 322175c13541SPoul-Henning Kamp 3222413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3223413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3224413628a7SBjoern A. Zeeb 3225b89e82ddSJamie Gritton error = 0; 3226413628a7SBjoern A. Zeeb switch (sa->sa_family) 3227413628a7SBjoern A. Zeeb { 3228413628a7SBjoern A. Zeeb #ifdef INET 3229413628a7SBjoern A. Zeeb case AF_INET: 32309ddb7954SMike Barcroft sai = (struct sockaddr_in *)sa; 3231b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3232413628a7SBjoern A. Zeeb break; 3233413628a7SBjoern A. Zeeb #endif 3234413628a7SBjoern A. Zeeb #ifdef INET6 3235413628a7SBjoern A. Zeeb case AF_INET6: 3236413628a7SBjoern A. Zeeb sai6 = (struct sockaddr_in6 *)sa; 3237b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3238413628a7SBjoern A. Zeeb break; 3239413628a7SBjoern A. Zeeb #endif 3240413628a7SBjoern A. Zeeb default: 32410304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3242b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3243413628a7SBjoern A. Zeeb } 3244b89e82ddSJamie Gritton return (error); 324575c13541SPoul-Henning Kamp } 324691421ba2SRobert Watson 324791421ba2SRobert Watson /* 324891421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 324991421ba2SRobert Watson */ 325091421ba2SRobert Watson int 32519ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 325291421ba2SRobert Watson { 325391421ba2SRobert Watson 32540304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 32550304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 32560304c731SJamie Gritton } 325791421ba2SRobert Watson 32580304c731SJamie Gritton /* 32590304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 32600304c731SJamie Gritton */ 32610304c731SJamie Gritton int 32620304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 32630304c731SJamie Gritton { 32640304c731SJamie Gritton 32650304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 32660304c731SJamie Gritton if (pr1 == pr2) 32670304c731SJamie Gritton return (1); 326891421ba2SRobert Watson return (0); 326991421ba2SRobert Watson } 327091421ba2SRobert Watson 327191421ba2SRobert Watson /* 327291421ba2SRobert Watson * Return 1 if the passed credential is in a jail, otherwise 0. 327391421ba2SRobert Watson */ 327491421ba2SRobert Watson int 32759ddb7954SMike Barcroft jailed(struct ucred *cred) 327691421ba2SRobert Watson { 327791421ba2SRobert Watson 32780304c731SJamie Gritton return (cred->cr_prison != &prison0); 327991421ba2SRobert Watson } 32809484d0c0SRobert Drehmel 32819484d0c0SRobert Drehmel /* 32827455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 32839484d0c0SRobert Drehmel */ 3284ad1ff099SRobert Drehmel void 32859ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 32869484d0c0SRobert Drehmel { 328776ca6f88SJamie Gritton struct prison *pr; 32889484d0c0SRobert Drehmel 32897455b100SJamie Gritton /* 32907455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 32917455b100SJamie Gritton * system's hostname. 32927455b100SJamie Gritton */ 329376ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 329476ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3295c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 329676ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 32979484d0c0SRobert Drehmel } 3298fd7a8150SMike Barcroft 32997455b100SJamie Gritton void 33007455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 33017455b100SJamie Gritton { 33027455b100SJamie Gritton 33037455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3304c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 33057455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 33067455b100SJamie Gritton } 33077455b100SJamie Gritton 33087455b100SJamie Gritton void 33097455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 33107455b100SJamie Gritton { 33117455b100SJamie Gritton 33127455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3313c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 33147455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 33157455b100SJamie Gritton } 33167455b100SJamie Gritton 33177455b100SJamie Gritton void 33187455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 33197455b100SJamie Gritton { 33207455b100SJamie Gritton 33217455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 33227455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 33237455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 33247455b100SJamie Gritton } 33257455b100SJamie Gritton 3326eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3327eb79e1c7SBjoern A. Zeeb /* 3328eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3329eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3330eb79e1c7SBjoern A. Zeeb * 3331eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3332eb79e1c7SBjoern A. Zeeb */ 3333eb79e1c7SBjoern A. Zeeb int 3334eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3335eb79e1c7SBjoern A. Zeeb { 3336eb79e1c7SBjoern A. Zeeb 3337eb79e1c7SBjoern A. Zeeb /* 3338eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3339eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3340eb79e1c7SBjoern A. Zeeb */ 3341eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3342eb79e1c7SBjoern A. Zeeb } 3343eb79e1c7SBjoern A. Zeeb #endif 3344eb79e1c7SBjoern A. Zeeb 3345f08df373SRobert Watson /* 3346820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3347820a0de9SPawel Jakub Dawidek * status of a mount point. 3348820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3349820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3350820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3351f08df373SRobert Watson */ 3352f08df373SRobert Watson int 3353820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3354f08df373SRobert Watson { 3355820a0de9SPawel Jakub Dawidek struct prison *pr; 3356820a0de9SPawel Jakub Dawidek struct statfs *sp; 3357820a0de9SPawel Jakub Dawidek size_t len; 3358f08df373SRobert Watson 3359820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 33600304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 33610304c731SJamie Gritton return (0); 3362820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3363820a0de9SPawel Jakub Dawidek return (0); 33640304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3365820a0de9SPawel Jakub Dawidek return (ENOENT); 3366820a0de9SPawel Jakub Dawidek /* 3367820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3368820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3369820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3370820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3371820a0de9SPawel Jakub Dawidek */ 3372820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3373820a0de9SPawel Jakub Dawidek return (0); 3374820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3375820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3376820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3377820a0de9SPawel Jakub Dawidek return (ENOENT); 3378820a0de9SPawel Jakub Dawidek /* 3379820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3380820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3381820a0de9SPawel Jakub Dawidek */ 3382820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3383820a0de9SPawel Jakub Dawidek return (ENOENT); 3384f08df373SRobert Watson return (0); 3385f08df373SRobert Watson } 3386820a0de9SPawel Jakub Dawidek 3387820a0de9SPawel Jakub Dawidek void 3388820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3389820a0de9SPawel Jakub Dawidek { 3390820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3391820a0de9SPawel Jakub Dawidek struct prison *pr; 3392820a0de9SPawel Jakub Dawidek size_t len; 3393820a0de9SPawel Jakub Dawidek 3394820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 33950304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 33960304c731SJamie Gritton return; 3397820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3398820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3399820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3400820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3401820a0de9SPawel Jakub Dawidek return; 3402820a0de9SPawel Jakub Dawidek } 3403820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3404820a0de9SPawel Jakub Dawidek /* 3405820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3406820a0de9SPawel Jakub Dawidek * the valid path left there. 3407820a0de9SPawel Jakub Dawidek */ 3408820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3409820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3410820a0de9SPawel Jakub Dawidek return; 3411820a0de9SPawel Jakub Dawidek } 3412820a0de9SPawel Jakub Dawidek /* 3413820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3414820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3415820a0de9SPawel Jakub Dawidek */ 3416820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3417820a0de9SPawel Jakub Dawidek return; 3418820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3419820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3420820a0de9SPawel Jakub Dawidek /* 3421820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3422820a0de9SPawel Jakub Dawidek * the valid path left there. 3423820a0de9SPawel Jakub Dawidek */ 3424820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3425820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3426820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3427820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3428820a0de9SPawel Jakub Dawidek } else { 3429820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3430820a0de9SPawel Jakub Dawidek } 3431f08df373SRobert Watson } 3432f08df373SRobert Watson 3433800c9408SRobert Watson /* 3434800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3435800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3436800c9408SRobert Watson */ 3437800c9408SRobert Watson int 3438800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3439800c9408SRobert Watson { 3440800c9408SRobert Watson 3441800c9408SRobert Watson if (!jailed(cred)) 3442800c9408SRobert Watson return (0); 3443800c9408SRobert Watson 34446bb79563SJamie Gritton #ifdef VIMAGE 34456bb79563SJamie Gritton /* 34466bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 34476bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 34486bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 34496bb79563SJamie Gritton */ 34506bb79563SJamie Gritton switch (priv) { 34516bb79563SJamie Gritton #ifdef notyet 34526bb79563SJamie Gritton /* 34536bb79563SJamie Gritton * NFS-specific privileges. 34546bb79563SJamie Gritton */ 34556bb79563SJamie Gritton case PRIV_NFS_DAEMON: 34566bb79563SJamie Gritton case PRIV_NFS_LOCKD: 34576bb79563SJamie Gritton #endif 34586bb79563SJamie Gritton /* 34596bb79563SJamie Gritton * Network stack privileges. 34606bb79563SJamie Gritton */ 34616bb79563SJamie Gritton case PRIV_NET_BRIDGE: 34626bb79563SJamie Gritton case PRIV_NET_GRE: 34636bb79563SJamie Gritton case PRIV_NET_BPF: 34646bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 34656bb79563SJamie Gritton case PRIV_NET_ROUTE: 34666bb79563SJamie Gritton case PRIV_NET_TAP: 34676bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 34686bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 34696bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 34706bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 34716bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 34726bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 34736bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 34746bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 34756bb79563SJamie Gritton case PRIV_NET_DELMULTI: 34766bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 34776bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 34786bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 34796bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 34806bb79563SJamie Gritton case PRIV_NET_IFCREATE: 34816bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 34826bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 34836bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 34846bb79563SJamie Gritton case PRIV_NET_LAGG: 34856bb79563SJamie Gritton case PRIV_NET_GIF: 34866bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 34876bb79563SJamie Gritton 34886bb79563SJamie Gritton /* 34896bb79563SJamie Gritton * 802.11-related privileges. 34906bb79563SJamie Gritton */ 34916bb79563SJamie Gritton case PRIV_NET80211_GETKEY: 34926bb79563SJamie Gritton #ifdef notyet 34936bb79563SJamie Gritton case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 34946bb79563SJamie Gritton #endif 34956bb79563SJamie Gritton 34966bb79563SJamie Gritton #ifdef notyet 34976bb79563SJamie Gritton /* 34986bb79563SJamie Gritton * AppleTalk privileges. 34996bb79563SJamie Gritton */ 35006bb79563SJamie Gritton case PRIV_NETATALK_RESERVEDPORT: 35016bb79563SJamie Gritton 35026bb79563SJamie Gritton /* 35036bb79563SJamie Gritton * ATM privileges. 35046bb79563SJamie Gritton */ 35056bb79563SJamie Gritton case PRIV_NETATM_CFG: 35066bb79563SJamie Gritton case PRIV_NETATM_ADD: 35076bb79563SJamie Gritton case PRIV_NETATM_DEL: 35086bb79563SJamie Gritton case PRIV_NETATM_SET: 35096bb79563SJamie Gritton 35106bb79563SJamie Gritton /* 35116bb79563SJamie Gritton * Bluetooth privileges. 35126bb79563SJamie Gritton */ 35136bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 35146bb79563SJamie Gritton #endif 35156bb79563SJamie Gritton 35166bb79563SJamie Gritton /* 35176bb79563SJamie Gritton * Netgraph and netgraph module privileges. 35186bb79563SJamie Gritton */ 35196bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 35206bb79563SJamie Gritton #ifdef notyet 35216bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 35226bb79563SJamie Gritton #endif 35236bb79563SJamie Gritton 35246bb79563SJamie Gritton /* 35256bb79563SJamie Gritton * IPv4 and IPv6 privileges. 35266bb79563SJamie Gritton */ 35276bb79563SJamie Gritton case PRIV_NETINET_IPFW: 35286bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 35296bb79563SJamie Gritton case PRIV_NETINET_PF: 35306bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 35316bb79563SJamie Gritton case PRIV_NETINET_CARP: 35326bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 35336bb79563SJamie Gritton case PRIV_NETINET_RAW: 35346bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 35356bb79563SJamie Gritton case PRIV_NETINET_ND6: 35366bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 35376bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 35386bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 35396bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 35406bb79563SJamie Gritton 35416bb79563SJamie Gritton #ifdef notyet 35426bb79563SJamie Gritton /* 35436bb79563SJamie Gritton * IPX/SPX privileges. 35446bb79563SJamie Gritton */ 35456bb79563SJamie Gritton case PRIV_NETIPX_RESERVEDPORT: 35466bb79563SJamie Gritton case PRIV_NETIPX_RAW: 35476bb79563SJamie Gritton 35486bb79563SJamie Gritton /* 35496bb79563SJamie Gritton * NCP privileges. 35506bb79563SJamie Gritton */ 35516bb79563SJamie Gritton case PRIV_NETNCP: 35526bb79563SJamie Gritton 35536bb79563SJamie Gritton /* 35546bb79563SJamie Gritton * SMB privileges. 35556bb79563SJamie Gritton */ 35566bb79563SJamie Gritton case PRIV_NETSMB: 35576bb79563SJamie Gritton #endif 35586bb79563SJamie Gritton 35596bb79563SJamie Gritton /* 35606bb79563SJamie Gritton * No default: or deny here. 35616bb79563SJamie Gritton * In case of no permit fall through to next switch(). 35626bb79563SJamie Gritton */ 35636bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 35646bb79563SJamie Gritton return (0); 35656bb79563SJamie Gritton } 35666bb79563SJamie Gritton #endif /* VIMAGE */ 35676bb79563SJamie Gritton 3568800c9408SRobert Watson switch (priv) { 3569800c9408SRobert Watson 3570800c9408SRobert Watson /* 3571800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3572800c9408SRobert Watson */ 3573800c9408SRobert Watson case PRIV_KTRACE: 3574800c9408SRobert Watson 3575c3c1b5e6SRobert Watson #if 0 3576800c9408SRobert Watson /* 3577800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3578800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3579800c9408SRobert Watson * want to further refine the relationship between audit and 3580800c9408SRobert Watson * jail. 3581800c9408SRobert Watson */ 3582800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3583800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3584800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3585c3c1b5e6SRobert Watson #endif 3586800c9408SRobert Watson 3587800c9408SRobert Watson /* 3588800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3589800c9408SRobert Watson * credentials in any way they see fit. 3590800c9408SRobert Watson */ 3591800c9408SRobert Watson case PRIV_CRED_SETUID: 3592800c9408SRobert Watson case PRIV_CRED_SETEUID: 3593800c9408SRobert Watson case PRIV_CRED_SETGID: 3594800c9408SRobert Watson case PRIV_CRED_SETEGID: 3595800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3596800c9408SRobert Watson case PRIV_CRED_SETREUID: 3597800c9408SRobert Watson case PRIV_CRED_SETREGID: 3598800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3599800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3600800c9408SRobert Watson 3601800c9408SRobert Watson /* 3602800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3603800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3604800c9408SRobert Watson */ 3605800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3606800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3607800c9408SRobert Watson 3608800c9408SRobert Watson /* 3609800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3610800c9408SRobert Watson * allow jailed root various debugging privileges. 3611800c9408SRobert Watson */ 3612800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3613800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3614800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3615800c9408SRobert Watson 3616800c9408SRobert Watson /* 3617800c9408SRobert Watson * Allow jail to set various resource limits and login 3618800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3619800c9408SRobert Watson */ 3620800c9408SRobert Watson case PRIV_PROC_LIMIT: 3621800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3622800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3623800c9408SRobert Watson 3624800c9408SRobert Watson /* 3625800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3626800c9408SRobert Watson */ 3627800c9408SRobert Watson case PRIV_IPC_READ: 3628800c9408SRobert Watson case PRIV_IPC_WRITE: 3629800c9408SRobert Watson case PRIV_IPC_ADMIN: 3630800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3631800c9408SRobert Watson case PRIV_MQ_ADMIN: 3632800c9408SRobert Watson 3633800c9408SRobert Watson /* 36340304c731SJamie Gritton * Jail operations within a jail work on child jails. 36350304c731SJamie Gritton */ 36360304c731SJamie Gritton case PRIV_JAIL_ATTACH: 36370304c731SJamie Gritton case PRIV_JAIL_SET: 36380304c731SJamie Gritton case PRIV_JAIL_REMOVE: 36390304c731SJamie Gritton 36400304c731SJamie Gritton /* 3641800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3642800c9408SRobert Watson * root processes in jail to change scheduling on other 3643800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3644800c9408SRobert Watson */ 3645800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3646413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3647800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3648800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3649800c9408SRobert Watson 3650800c9408SRobert Watson /* 3651800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3652800c9408SRobert Watson * writable. 3653800c9408SRobert Watson */ 3654800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3655800c9408SRobert Watson 3656800c9408SRobert Watson /* 3657800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3658e82d0201SRobert Watson * properties. These should likely be conditional on a 3659e82d0201SRobert Watson * configuration option. 3660800c9408SRobert Watson */ 366195b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 366295b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3663800c9408SRobert Watson 3664800c9408SRobert Watson /* 3665800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3666800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3667800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3668800c9408SRobert Watson * privileges. 3669800c9408SRobert Watson */ 3670800c9408SRobert Watson case PRIV_VFS_READ: 3671800c9408SRobert Watson case PRIV_VFS_WRITE: 3672800c9408SRobert Watson case PRIV_VFS_ADMIN: 3673800c9408SRobert Watson case PRIV_VFS_EXEC: 3674800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3675800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3676800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3677800c9408SRobert Watson case PRIV_VFS_CHOWN: 3678800c9408SRobert Watson case PRIV_VFS_CHROOT: 3679bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3680800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3681800c9408SRobert Watson case PRIV_VFS_LINK: 3682800c9408SRobert Watson case PRIV_VFS_SETGID: 3683e41966dcSRobert Watson case PRIV_VFS_STAT: 3684800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3685800c9408SRobert Watson return (0); 3686800c9408SRobert Watson 3687800c9408SRobert Watson /* 3688800c9408SRobert Watson * Depending on the global setting, allow privilege of 3689800c9408SRobert Watson * setting system flags. 3690800c9408SRobert Watson */ 3691800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 36920304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3693800c9408SRobert Watson return (0); 3694800c9408SRobert Watson else 3695800c9408SRobert Watson return (EPERM); 3696800c9408SRobert Watson 3697800c9408SRobert Watson /* 3698f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3699f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3700f3a8d2f9SPawel Jakub Dawidek */ 3701f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3702f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3703f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 370424b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 37050304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3706f3a8d2f9SPawel Jakub Dawidek return (0); 3707f3a8d2f9SPawel Jakub Dawidek else 3708f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3709f3a8d2f9SPawel Jakub Dawidek 3710f3a8d2f9SPawel Jakub Dawidek /* 37114b084056SRobert Watson * Allow jailed root to bind reserved ports and reuse in-use 37124b084056SRobert Watson * ports. 3713800c9408SRobert Watson */ 3714800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 37154b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3716800c9408SRobert Watson return (0); 3717800c9408SRobert Watson 3718800c9408SRobert Watson /* 371979ba3952SBjoern A. Zeeb * Allow jailed root to set certian IPv4/6 (option) headers. 372079ba3952SBjoern A. Zeeb */ 372179ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 372279ba3952SBjoern A. Zeeb return (0); 372379ba3952SBjoern A. Zeeb 372479ba3952SBjoern A. Zeeb /* 3725800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3726800c9408SRobert Watson */ 3727800c9408SRobert Watson case PRIV_NETINET_RAW: 37280304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3729800c9408SRobert Watson return (0); 3730800c9408SRobert Watson else 3731800c9408SRobert Watson return (EPERM); 3732800c9408SRobert Watson 3733800c9408SRobert Watson /* 3734800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3735800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3736800c9408SRobert Watson * jail. 3737800c9408SRobert Watson */ 3738800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3739800c9408SRobert Watson return (0); 3740800c9408SRobert Watson 3741800c9408SRobert Watson default: 3742800c9408SRobert Watson /* 3743800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3744800c9408SRobert Watson * includes almost all network privileges, many system 3745800c9408SRobert Watson * configuration privileges. 3746800c9408SRobert Watson */ 3747800c9408SRobert Watson return (EPERM); 3748800c9408SRobert Watson } 3749800c9408SRobert Watson } 3750800c9408SRobert Watson 37510304c731SJamie Gritton /* 37520304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 37530304c731SJamie Gritton * if it does not directly follow. 37540304c731SJamie Gritton */ 37550304c731SJamie Gritton 37560304c731SJamie Gritton char * 37570304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 37580304c731SJamie Gritton { 37590304c731SJamie Gritton char *name; 37600304c731SJamie Gritton 37610304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 37620304c731SJamie Gritton if (pr1 == pr2) 37630304c731SJamie Gritton return "0"; 37640304c731SJamie Gritton name = pr2->pr_name; 37650304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 37660304c731SJamie Gritton /* 37670304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 37680304c731SJamie Gritton * so its length can't be counted on. But the number of dots 37690304c731SJamie Gritton * can be counted on - and counted. 37700304c731SJamie Gritton */ 37710304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 37720304c731SJamie Gritton name = strchr(name, '.') + 1; 37730304c731SJamie Gritton } 37740304c731SJamie Gritton return (name); 37750304c731SJamie Gritton } 37760304c731SJamie Gritton 37770304c731SJamie Gritton /* 37780304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 37790304c731SJamie Gritton * if it does not directly follow. 37800304c731SJamie Gritton */ 37810304c731SJamie Gritton static char * 37820304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 37830304c731SJamie Gritton { 37840304c731SJamie Gritton char *path1, *path2; 37850304c731SJamie Gritton int len1; 37860304c731SJamie Gritton 37870304c731SJamie Gritton path1 = pr1->pr_path; 37880304c731SJamie Gritton path2 = pr2->pr_path; 37890304c731SJamie Gritton if (!strcmp(path1, "/")) 37900304c731SJamie Gritton return (path2); 37910304c731SJamie Gritton len1 = strlen(path1); 37920304c731SJamie Gritton if (strncmp(path1, path2, len1)) 37930304c731SJamie Gritton return (path2); 37940304c731SJamie Gritton if (path2[len1] == '\0') 37950304c731SJamie Gritton return "/"; 37960304c731SJamie Gritton if (path2[len1] == '/') 37970304c731SJamie Gritton return (path2 + len1); 37980304c731SJamie Gritton return (path2); 37990304c731SJamie Gritton } 38000304c731SJamie Gritton 38010304c731SJamie Gritton 38020304c731SJamie Gritton /* 38030304c731SJamie Gritton * Jail-related sysctls. 38040304c731SJamie Gritton */ 38050304c731SJamie Gritton SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 38060304c731SJamie Gritton "Jails"); 38070304c731SJamie Gritton 3808fd7a8150SMike Barcroft static int 3809fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3810fd7a8150SMike Barcroft { 3811b38ff370SJamie Gritton struct xprison *xp; 38120304c731SJamie Gritton struct prison *pr, *cpr; 3813b38ff370SJamie Gritton #ifdef INET 3814b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3815b38ff370SJamie Gritton int ip4s = 0; 3816b38ff370SJamie Gritton #endif 3817b38ff370SJamie Gritton #ifdef INET6 3818b38ff370SJamie Gritton struct in_addr *ip6 = NULL; 3819b38ff370SJamie Gritton int ip6s = 0; 3820b38ff370SJamie Gritton #endif 38210304c731SJamie Gritton int descend, error; 3822fd7a8150SMike Barcroft 3823b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 38240304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3825b38ff370SJamie Gritton error = 0; 3826dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 38270304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 38280304c731SJamie Gritton #if defined(INET) || defined(INET6) 3829b38ff370SJamie Gritton again: 38300304c731SJamie Gritton #endif 38310304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3832413628a7SBjoern A. Zeeb #ifdef INET 38330304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 38340304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 38350304c731SJamie Gritton ip4s = cpr->pr_ip4s; 38360304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3837b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 3838b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 3839b38ff370SJamie Gritton goto again; 3840b38ff370SJamie Gritton } 38410304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 38420304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 3843b38ff370SJamie Gritton } 3844413628a7SBjoern A. Zeeb #endif 3845413628a7SBjoern A. Zeeb #ifdef INET6 38460304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 38470304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 38480304c731SJamie Gritton ip6s = cpr->pr_ip6s; 38490304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3850b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 3851b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 3852b38ff370SJamie Gritton goto again; 3853413628a7SBjoern A. Zeeb } 38540304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 38550304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 3856b38ff370SJamie Gritton } 3857b38ff370SJamie Gritton #endif 38580304c731SJamie Gritton if (cpr->pr_ref == 0) { 38590304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3860b38ff370SJamie Gritton continue; 3861b38ff370SJamie Gritton } 3862b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 3863fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 38640304c731SJamie Gritton xp->pr_id = cpr->pr_id; 38650304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 3866b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 38670304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 3868c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 38690304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 3870413628a7SBjoern A. Zeeb #ifdef INET 38710304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 3872413628a7SBjoern A. Zeeb #endif 3873413628a7SBjoern A. Zeeb #ifdef INET6 38740304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 3875413628a7SBjoern A. Zeeb #endif 38760304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3877b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 3878b38ff370SJamie Gritton if (error) 3879b38ff370SJamie Gritton break; 3880413628a7SBjoern A. Zeeb #ifdef INET 3881b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 3882b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 3883b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 3884b38ff370SJamie Gritton if (error) 3885b38ff370SJamie Gritton break; 3886413628a7SBjoern A. Zeeb } 3887413628a7SBjoern A. Zeeb #endif 3888413628a7SBjoern A. Zeeb #ifdef INET6 3889b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 3890b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 3891b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 3892b38ff370SJamie Gritton if (error) 3893b38ff370SJamie Gritton break; 3894413628a7SBjoern A. Zeeb } 3895413628a7SBjoern A. Zeeb #endif 3896fd7a8150SMike Barcroft } 3897dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 3898b38ff370SJamie Gritton free(xp, M_TEMP); 3899b38ff370SJamie Gritton #ifdef INET 3900b38ff370SJamie Gritton free(ip4, M_TEMP); 3901b38ff370SJamie Gritton #endif 3902b38ff370SJamie Gritton #ifdef INET6 3903b38ff370SJamie Gritton free(ip6, M_TEMP); 3904b38ff370SJamie Gritton #endif 3905fd7a8150SMike Barcroft return (error); 3906fd7a8150SMike Barcroft } 3907fd7a8150SMike Barcroft 3908f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 3909f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3910f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 3911461167c2SPawel Jakub Dawidek 3912461167c2SPawel Jakub Dawidek static int 3913461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 3914461167c2SPawel Jakub Dawidek { 3915461167c2SPawel Jakub Dawidek int error, injail; 3916461167c2SPawel Jakub Dawidek 3917461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 3918461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 3919461167c2SPawel Jakub Dawidek 3920461167c2SPawel Jakub Dawidek return (error); 3921461167c2SPawel Jakub Dawidek } 39220304c731SJamie Gritton 3923f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 3924f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 3925f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 3926413628a7SBjoern A. Zeeb 39270304c731SJamie Gritton #if defined(INET) || defined(INET6) 3928e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 39290304c731SJamie Gritton &jail_max_af_ips, 0, 39300304c731SJamie Gritton "Number of IP addresses a jail may have at most per address family"); 39310304c731SJamie Gritton #endif 39320304c731SJamie Gritton 39330304c731SJamie Gritton /* 39340304c731SJamie Gritton * Default parameters for jail(2) compatability. For historical reasons, 39350304c731SJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 39360304c731SJamie Gritton * just see their own parameters, and can't change them. 39370304c731SJamie Gritton */ 39380304c731SJamie Gritton static int 39390304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 39400304c731SJamie Gritton { 39410304c731SJamie Gritton struct prison *pr; 39420304c731SJamie Gritton int allow, error, i; 39430304c731SJamie Gritton 39440304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 39450304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 39460304c731SJamie Gritton 39470304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 39480304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 39490304c731SJamie Gritton if (arg1 != NULL) 39500304c731SJamie Gritton i = !i; 39510304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 39520304c731SJamie Gritton if (error || !req->newptr) 39530304c731SJamie Gritton return (error); 39540304c731SJamie Gritton i = i ? arg2 : 0; 39550304c731SJamie Gritton if (arg1 != NULL) 39560304c731SJamie Gritton i ^= arg2; 39570304c731SJamie Gritton /* 39580304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 39590304c731SJamie Gritton * for writing. 39600304c731SJamie Gritton */ 39610304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 39620304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 39630304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 39640304c731SJamie Gritton return (0); 39650304c731SJamie Gritton } 39660304c731SJamie Gritton 39670304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 39680304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39690304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 39700304c731SJamie Gritton "Processes in jail can set their hostnames"); 39710304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 39720304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39730304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 39740304c731SJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only"); 39750304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 39760304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39770304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 39780304c731SJamie Gritton "Processes in jail can use System V IPC primitives"); 39790304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 39800304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39810304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 39820304c731SJamie Gritton "Prison root can create raw sockets"); 39830304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 39840304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39850304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 39860304c731SJamie Gritton "Processes in jail can alter system file flags"); 39870304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 39880304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 39890304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 39900304c731SJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems"); 39910304c731SJamie Gritton 39920304c731SJamie Gritton static int 39930304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 39940304c731SJamie Gritton { 39950304c731SJamie Gritton struct prison *pr; 39960304c731SJamie Gritton int level, error; 39970304c731SJamie Gritton 39980304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 39990304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 40000304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 40010304c731SJamie Gritton if (error || !req->newptr) 40020304c731SJamie Gritton return (error); 40030304c731SJamie Gritton *(int *)arg1 = level; 40040304c731SJamie Gritton return (0); 40050304c731SJamie Gritton } 40060304c731SJamie Gritton 40070304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 40080304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 40090304c731SJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 40100304c731SJamie Gritton sysctl_jail_default_level, "I", 40110304c731SJamie Gritton "Processes in jail cannot see all mounted file systems"); 40120304c731SJamie Gritton 40130304c731SJamie Gritton /* 40140304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 40150304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 40160304c731SJamie Gritton * to make themselves and their types known. 40170304c731SJamie Gritton */ 40180304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 40190304c731SJamie Gritton "Jail parameters"); 40200304c731SJamie Gritton 40210304c731SJamie Gritton int 40220304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 40230304c731SJamie Gritton { 40240304c731SJamie Gritton int i; 40250304c731SJamie Gritton long l; 40260304c731SJamie Gritton size_t s; 40270304c731SJamie Gritton char numbuf[12]; 40280304c731SJamie Gritton 40290304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 40300304c731SJamie Gritton { 40310304c731SJamie Gritton case CTLTYPE_LONG: 40320304c731SJamie Gritton case CTLTYPE_ULONG: 40330304c731SJamie Gritton l = 0; 40340304c731SJamie Gritton #ifdef SCTL_MASK32 40350304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 40360304c731SJamie Gritton #endif 40370304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 40380304c731SJamie Gritton case CTLTYPE_INT: 40390304c731SJamie Gritton case CTLTYPE_UINT: 40400304c731SJamie Gritton i = 0; 40410304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 40420304c731SJamie Gritton case CTLTYPE_STRING: 40430304c731SJamie Gritton snprintf(numbuf, sizeof(numbuf), "%d", arg2); 40440304c731SJamie Gritton return 40450304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 40460304c731SJamie Gritton case CTLTYPE_STRUCT: 40470304c731SJamie Gritton s = (size_t)arg2; 40480304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 40490304c731SJamie Gritton } 40500304c731SJamie Gritton return (0); 40510304c731SJamie Gritton } 40520304c731SJamie Gritton 40530304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 40540304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 40550304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 40560304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 40570304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 40580304c731SJamie Gritton "I", "Jail secure level"); 40590304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 40600304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 40610304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 40620304c731SJamie Gritton "B", "Jail persistence"); 4063679e1390SJamie Gritton #ifdef VIMAGE 4064679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 40657cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4066679e1390SJamie Gritton #endif 40670304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 40680304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 40690304c731SJamie Gritton 4070b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4071b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4072b97457e2SJamie Gritton "I", "Current number of child jails"); 4073b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4074b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4075b97457e2SJamie Gritton 40767cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 40770304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 40780304c731SJamie Gritton "Jail hostname"); 407976ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 408076ca6f88SJamie Gritton "Jail NIS domainname"); 408176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 408276ca6f88SJamie Gritton "Jail host UUID"); 408376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 408476ca6f88SJamie Gritton "LU", "Jail host ID"); 40850304c731SJamie Gritton 40860304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 40870304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 40880304c731SJamie Gritton 40890304c731SJamie Gritton #ifdef INET 40902b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 40912b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 40920304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 40930304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 40940304c731SJamie Gritton #endif 40950304c731SJamie Gritton #ifdef INET6 40962b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 40972b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 40980304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 40990304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 41000304c731SJamie Gritton #endif 41010304c731SJamie Gritton 41020304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 41030304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 41040304c731SJamie Gritton "B", "Jail may set hostname"); 41050304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 41060304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 41070304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 41080304c731SJamie Gritton "B", "Jail may create raw sockets"); 41090304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 41100304c731SJamie Gritton "B", "Jail may alter system file flags"); 41110304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 41120304c731SJamie Gritton "B", "Jail may mount/unmount jail-friendly file systems"); 41130304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 41140304c731SJamie Gritton "B", "Jail may set file quotas"); 41150304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 41160304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 41170304c731SJamie Gritton 41180304c731SJamie Gritton 4119413628a7SBjoern A. Zeeb #ifdef DDB 4120b38ff370SJamie Gritton 4121b38ff370SJamie Gritton static void 4122b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4123413628a7SBjoern A. Zeeb { 41240304c731SJamie Gritton int fi; 4125b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4126b38ff370SJamie Gritton int ii; 4127413628a7SBjoern A. Zeeb #endif 41287cbf7213SJamie Gritton unsigned jsf; 4129413628a7SBjoern A. Zeeb #ifdef INET6 4130413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4131413628a7SBjoern A. Zeeb #endif 4132413628a7SBjoern A. Zeeb 4133b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4134b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4135b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 41360304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4137b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4138b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4139b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4140b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4141b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4142679e1390SJamie Gritton #ifdef VIMAGE 4143679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4144679e1390SJamie Gritton #endif 4145b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4146b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 4147b97457e2SJamie Gritton db_printf(" childcount = %d\n", pr->pr_childcount); 41480304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 41490304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4150b38ff370SJamie Gritton db_printf(" flags = %x", pr->pr_flags); 41510304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 41520304c731SJamie Gritton fi++) 41530304c731SJamie Gritton if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 41540304c731SJamie Gritton db_printf(" %s", pr_flag_names[fi]); 41557cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 41567cbf7213SJamie Gritton fi++) { 41577cbf7213SJamie Gritton jsf = pr->pr_flags & 41587cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 41597cbf7213SJamie Gritton db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 41607cbf7213SJamie Gritton pr_flag_jailsys[fi].disable && 41617cbf7213SJamie Gritton (jsf == pr_flag_jailsys[fi].disable) ? "disable" 41627cbf7213SJamie Gritton : (jsf == pr_flag_jailsys[fi].new) ? "new" 41637cbf7213SJamie Gritton : "inherit"); 41647cbf7213SJamie Gritton } 41650304c731SJamie Gritton db_printf(" allow = %x", pr->pr_allow); 41660304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 41670304c731SJamie Gritton fi++) 41680304c731SJamie Gritton if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 41690304c731SJamie Gritton db_printf(" %s", pr_allow_names[fi]); 4170b38ff370SJamie Gritton db_printf("\n"); 41710304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4172c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4173c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4174c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 417576ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4176413628a7SBjoern A. Zeeb #ifdef INET 4177b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4178b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4179b38ff370SJamie Gritton db_printf(" %s %s\n", 4180b38ff370SJamie Gritton ii == 0 ? "ip4 =" : " ", 4181b38ff370SJamie Gritton inet_ntoa(pr->pr_ip4[ii])); 4182413628a7SBjoern A. Zeeb #endif 4183413628a7SBjoern A. Zeeb #ifdef INET6 4184b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4185b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4186b38ff370SJamie Gritton db_printf(" %s %s\n", 4187b38ff370SJamie Gritton ii == 0 ? "ip6 =" : " ", 4188b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4189b38ff370SJamie Gritton #endif 4190b38ff370SJamie Gritton } 4191b38ff370SJamie Gritton 4192b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4193b38ff370SJamie Gritton { 4194b38ff370SJamie Gritton struct prison *pr; 4195b38ff370SJamie Gritton 4196b38ff370SJamie Gritton if (!have_addr) { 41970304c731SJamie Gritton /* 41980304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 41990304c731SJamie Gritton * listed. 42000304c731SJamie Gritton */ 42010304c731SJamie Gritton db_show_prison(&prison0); 42020304c731SJamie Gritton if (!db_pager_quit) { 4203b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4204b38ff370SJamie Gritton db_show_prison(pr); 4205413628a7SBjoern A. Zeeb if (db_pager_quit) 4206413628a7SBjoern A. Zeeb break; 4207413628a7SBjoern A. Zeeb } 42080304c731SJamie Gritton } 4209b38ff370SJamie Gritton return; 4210413628a7SBjoern A. Zeeb } 4211b38ff370SJamie Gritton 42120304c731SJamie Gritton if (addr == 0) 42130304c731SJamie Gritton pr = &prison0; 42140304c731SJamie Gritton else { 4215b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4216b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4217b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4218b38ff370SJamie Gritton break; 4219b38ff370SJamie Gritton if (pr == NULL) 4220b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4221b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4222b38ff370SJamie Gritton if (pr->pr_id == addr) 4223b38ff370SJamie Gritton break; 4224b38ff370SJamie Gritton if (pr == NULL) 4225b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4226b38ff370SJamie Gritton pr = (struct prison *)addr; 42270304c731SJamie Gritton } 4228b38ff370SJamie Gritton db_show_prison(pr); 4229b38ff370SJamie Gritton } 4230b38ff370SJamie Gritton 4231413628a7SBjoern A. Zeeb #endif /* DDB */ 4232