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> 52097055e2SEdward Tomasz Napierala #include <sys/racct.h> 53a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 54dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5576ca6f88SJamie Gritton #include <sys/sysent.h> 56fd7a8150SMike Barcroft #include <sys/namei.h> 57820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 58fd7a8150SMike Barcroft #include <sys/queue.h> 5975c13541SPoul-Henning Kamp #include <sys/socket.h> 60fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6183f1e257SRobert Watson #include <sys/sysctl.h> 62fd7a8150SMike Barcroft #include <sys/vnode.h> 63530c0060SRobert Watson 6475c13541SPoul-Henning Kamp #include <net/if.h> 65530c0060SRobert Watson #include <net/vnet.h> 66530c0060SRobert Watson 6775c13541SPoul-Henning Kamp #include <netinet/in.h> 68530c0060SRobert Watson 69413628a7SBjoern A. Zeeb #ifdef DDB 70413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 71413628a7SBjoern A. Zeeb #endif /* DDB */ 7275c13541SPoul-Henning Kamp 73aed55708SRobert Watson #include <security/mac/mac_framework.h> 74aed55708SRobert Watson 758986e3a0SJamie Gritton #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 768986e3a0SJamie Gritton 7775c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 78d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 7975c13541SPoul-Henning Kamp 80592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 81592bcae8SBjoern A. Zeeb #ifdef INET 82592bcae8SBjoern A. Zeeb #ifdef INET6 83592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 84592bcae8SBjoern A. Zeeb #else 85592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 86592bcae8SBjoern A. Zeeb #endif 87592bcae8SBjoern A. Zeeb #else /* !INET */ 88592bcae8SBjoern A. Zeeb #ifdef INET6 89592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 90592bcae8SBjoern A. Zeeb #else 91592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 92592bcae8SBjoern A. Zeeb #endif 93592bcae8SBjoern A. Zeeb #endif 94592bcae8SBjoern A. Zeeb 950304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 960304c731SJamie Gritton struct prison prison0 = { 970304c731SJamie Gritton .pr_id = 0, 980304c731SJamie Gritton .pr_name = "0", 990304c731SJamie Gritton .pr_ref = 1, 1000304c731SJamie Gritton .pr_uref = 1, 1010304c731SJamie Gritton .pr_path = "/", 1020304c731SJamie Gritton .pr_securelevel = -1, 1030cc207a6SMartin Matuska .pr_devfs_rsnum = 0, 104b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1058986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 10613e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 107eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 108592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 109eb79e1c7SBjoern A. Zeeb #else 110592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 111eb79e1c7SBjoern A. Zeeb #endif 1120304c731SJamie Gritton .pr_allow = PR_ALLOW_ALL, 1130304c731SJamie Gritton }; 1140304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11583f1e257SRobert Watson 116a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 117dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 118b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 119b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 120a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1212110d913SXin LI int lastprid = 0; 122fd7a8150SMike Barcroft 123b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 124b3059e09SRobert Watson static void prison_complete(void *context, int pending); 125b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1260304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1270304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 128a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 129a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 130c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 131a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 132a7ad07bfSEdward Tomasz Napierala #endif 133413628a7SBjoern A. Zeeb #ifdef INET 1340d168b8dSGleb Smirnoff static int _prison_check_ip4(const struct prison *, const struct in_addr *); 1350304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 136413628a7SBjoern A. Zeeb #endif 137413628a7SBjoern A. Zeeb #ifdef INET6 1388571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 1390304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 140413628a7SBjoern A. Zeeb #endif 141fd7a8150SMike Barcroft 142b38ff370SJamie Gritton /* Flags for prison_deref */ 143b38ff370SJamie Gritton #define PD_DEREF 0x01 144b38ff370SJamie Gritton #define PD_DEUREF 0x02 145b38ff370SJamie Gritton #define PD_LOCKED 0x04 146b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 147b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 148fd7a8150SMike Barcroft 1490304c731SJamie Gritton /* 1505cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1515cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1525cc70397SBjoern A. Zeeb * terminating entry. 1530304c731SJamie Gritton */ 1540304c731SJamie Gritton static char *pr_flag_names[] = { 1550304c731SJamie Gritton [0] = "persist", 156592bcae8SBjoern A. Zeeb #ifdef INET 157592bcae8SBjoern A. Zeeb [7] = "ip4.saddrsel", 158592bcae8SBjoern A. Zeeb #endif 159592bcae8SBjoern A. Zeeb #ifdef INET6 160592bcae8SBjoern A. Zeeb [8] = "ip6.saddrsel", 161592bcae8SBjoern A. Zeeb #endif 1620304c731SJamie Gritton }; 1635cc70397SBjoern A. Zeeb const size_t pr_flag_names_size = sizeof(pr_flag_names); 1640304c731SJamie Gritton 1650304c731SJamie Gritton static char *pr_flag_nonames[] = { 1660304c731SJamie Gritton [0] = "nopersist", 167592bcae8SBjoern A. Zeeb #ifdef INET 168592bcae8SBjoern A. Zeeb [7] = "ip4.nosaddrsel", 169592bcae8SBjoern A. Zeeb #endif 170592bcae8SBjoern A. Zeeb #ifdef INET6 171592bcae8SBjoern A. Zeeb [8] = "ip6.nosaddrsel", 172592bcae8SBjoern A. Zeeb #endif 1737cbf7213SJamie Gritton }; 1745cc70397SBjoern A. Zeeb const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames); 1757cbf7213SJamie Gritton 1767cbf7213SJamie Gritton struct jailsys_flags { 1777cbf7213SJamie Gritton const char *name; 1787cbf7213SJamie Gritton unsigned disable; 1797cbf7213SJamie Gritton unsigned new; 1807cbf7213SJamie Gritton } pr_flag_jailsys[] = { 1817cbf7213SJamie Gritton { "host", 0, PR_HOST }, 1827cbf7213SJamie Gritton #ifdef VIMAGE 1837cbf7213SJamie Gritton { "vnet", 0, PR_VNET }, 1847cbf7213SJamie Gritton #endif 1850304c731SJamie Gritton #ifdef INET 1866a3f2779SJamie Gritton { "ip4", PR_IP4_USER, PR_IP4_USER }, 1870304c731SJamie Gritton #endif 1880304c731SJamie Gritton #ifdef INET6 1896a3f2779SJamie Gritton { "ip6", PR_IP6_USER, PR_IP6_USER }, 190679e1390SJamie Gritton #endif 1910304c731SJamie Gritton }; 1925cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1930304c731SJamie Gritton 1940304c731SJamie Gritton static char *pr_allow_names[] = { 1950304c731SJamie Gritton "allow.set_hostname", 1960304c731SJamie Gritton "allow.sysvipc", 1970304c731SJamie Gritton "allow.raw_sockets", 1980304c731SJamie Gritton "allow.chflags", 1990304c731SJamie Gritton "allow.mount", 2000304c731SJamie Gritton "allow.quotas", 2010304c731SJamie Gritton "allow.socket_af", 202bf3db8aaSMartin Matuska "allow.mount.devfs", 203bf3db8aaSMartin Matuska "allow.mount.nullfs", 204e7af90abSMartin Matuska "allow.mount.zfs", 20541c0675eSMartin Matuska "allow.mount.procfs", 2062454886eSXin LI "allow.mount.tmpfs", 207464aad14SJamie Gritton "allow.mount.fdescfs", 208f19e47d6SMarcelo Araujo "allow.mount.linprocfs", 209f19e47d6SMarcelo Araujo "allow.mount.linsysfs", 2100304c731SJamie Gritton }; 2115cc70397SBjoern A. Zeeb const size_t pr_allow_names_size = sizeof(pr_allow_names); 2120304c731SJamie Gritton 2130304c731SJamie Gritton static char *pr_allow_nonames[] = { 2140304c731SJamie Gritton "allow.noset_hostname", 2150304c731SJamie Gritton "allow.nosysvipc", 2160304c731SJamie Gritton "allow.noraw_sockets", 2170304c731SJamie Gritton "allow.nochflags", 2180304c731SJamie Gritton "allow.nomount", 2190304c731SJamie Gritton "allow.noquotas", 2200304c731SJamie Gritton "allow.nosocket_af", 221bf3db8aaSMartin Matuska "allow.mount.nodevfs", 222bf3db8aaSMartin Matuska "allow.mount.nonullfs", 223e7af90abSMartin Matuska "allow.mount.nozfs", 22441c0675eSMartin Matuska "allow.mount.noprocfs", 2252454886eSXin LI "allow.mount.notmpfs", 226464aad14SJamie Gritton "allow.mount.nofdescfs", 227f19e47d6SMarcelo Araujo "allow.mount.nolinprocfs", 228f19e47d6SMarcelo Araujo "allow.mount.nolinsysfs", 2290304c731SJamie Gritton }; 2305cc70397SBjoern A. Zeeb const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 2310304c731SJamie Gritton 2320304c731SJamie Gritton #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 23342bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 234bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2350304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 23642bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2370cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2380304c731SJamie Gritton #if defined(INET) || defined(INET6) 239e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2400304c731SJamie Gritton #endif 2410304c731SJamie Gritton 242b96bd95bSIan Lepore /* 243b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 244b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 245b96bd95bSIan Lepore */ 246b96bd95bSIan Lepore void 247b96bd95bSIan Lepore prison0_init(void) 248b96bd95bSIan Lepore { 249b96bd95bSIan Lepore 250b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 251b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 252b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 253b96bd95bSIan Lepore } 254b96bd95bSIan Lepore 255413628a7SBjoern A. Zeeb #ifdef INET 256413628a7SBjoern A. Zeeb static int 257413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2) 258413628a7SBjoern A. Zeeb { 259413628a7SBjoern A. Zeeb in_addr_t iaa, iab; 260413628a7SBjoern A. Zeeb 261413628a7SBjoern A. Zeeb /* 262413628a7SBjoern A. Zeeb * We need to compare in HBO here to get the list sorted as expected 263413628a7SBjoern A. Zeeb * by the result of the code. Sorting NBO addresses gives you 264413628a7SBjoern A. Zeeb * interesting results. If you do not understand, do not try. 265413628a7SBjoern A. Zeeb */ 266413628a7SBjoern A. Zeeb iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 267413628a7SBjoern A. Zeeb iab = ntohl(((const struct in_addr *)ip2)->s_addr); 268413628a7SBjoern A. Zeeb 269413628a7SBjoern A. Zeeb /* 270413628a7SBjoern A. Zeeb * Do not simply return the difference of the two numbers, the int is 271413628a7SBjoern A. Zeeb * not wide enough. 272413628a7SBjoern A. Zeeb */ 273413628a7SBjoern A. Zeeb if (iaa > iab) 274413628a7SBjoern A. Zeeb return (1); 275413628a7SBjoern A. Zeeb else if (iaa < iab) 276413628a7SBjoern A. Zeeb return (-1); 277413628a7SBjoern A. Zeeb else 278413628a7SBjoern A. Zeeb return (0); 279413628a7SBjoern A. Zeeb } 280413628a7SBjoern A. Zeeb #endif 281413628a7SBjoern A. Zeeb 282413628a7SBjoern A. Zeeb #ifdef INET6 283413628a7SBjoern A. Zeeb static int 284413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2) 285413628a7SBjoern A. Zeeb { 286413628a7SBjoern A. Zeeb const struct in6_addr *ia6a, *ia6b; 287413628a7SBjoern A. Zeeb int i, rc; 288413628a7SBjoern A. Zeeb 289413628a7SBjoern A. Zeeb ia6a = (const struct in6_addr *)ip1; 290413628a7SBjoern A. Zeeb ia6b = (const struct in6_addr *)ip2; 291413628a7SBjoern A. Zeeb 292413628a7SBjoern A. Zeeb rc = 0; 293413628a7SBjoern A. Zeeb for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 294413628a7SBjoern A. Zeeb if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 295413628a7SBjoern A. Zeeb rc = 1; 296413628a7SBjoern A. Zeeb else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 297413628a7SBjoern A. Zeeb rc = -1; 298413628a7SBjoern A. Zeeb } 299413628a7SBjoern A. Zeeb return (rc); 300413628a7SBjoern A. Zeeb } 301413628a7SBjoern A. Zeeb #endif 302413628a7SBjoern A. Zeeb 303116734c4SMatthew Dillon /* 3049ddb7954SMike Barcroft * struct jail_args { 3059ddb7954SMike Barcroft * struct jail *jail; 3069ddb7954SMike Barcroft * }; 307116734c4SMatthew Dillon */ 30875c13541SPoul-Henning Kamp int 3098451d0ddSKip Macy sys_jail(struct thread *td, struct jail_args *uap) 31075c13541SPoul-Henning Kamp { 311413628a7SBjoern A. Zeeb uint32_t version; 312413628a7SBjoern A. Zeeb int error; 3130304c731SJamie Gritton struct jail j; 314413628a7SBjoern A. Zeeb 315413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 316413628a7SBjoern A. Zeeb if (error) 317413628a7SBjoern A. Zeeb return (error); 318413628a7SBjoern A. Zeeb 319413628a7SBjoern A. Zeeb switch (version) { 320413628a7SBjoern A. Zeeb case 0: 321413628a7SBjoern A. Zeeb { 322413628a7SBjoern A. Zeeb struct jail_v0 j0; 323413628a7SBjoern A. Zeeb 3240304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 3250304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 326413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 327413628a7SBjoern A. Zeeb if (error) 328413628a7SBjoern A. Zeeb return (error); 3290304c731SJamie Gritton j.version = j0.version; 3300304c731SJamie Gritton j.path = j0.path; 3310304c731SJamie Gritton j.hostname = j0.hostname; 332b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 333413628a7SBjoern A. Zeeb break; 334413628a7SBjoern A. Zeeb } 335413628a7SBjoern A. Zeeb 336413628a7SBjoern A. Zeeb case 1: 337413628a7SBjoern A. Zeeb /* 338413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 339413628a7SBjoern A. Zeeb * that never made it into the official kernel. 340413628a7SBjoern A. Zeeb */ 341413628a7SBjoern A. Zeeb return (EINVAL); 342413628a7SBjoern A. Zeeb 343413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 344413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 345413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 346413628a7SBjoern A. Zeeb if (error) 347413628a7SBjoern A. Zeeb return (error); 3480304c731SJamie Gritton break; 3490304c731SJamie Gritton 3500304c731SJamie Gritton default: 3510304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3520304c731SJamie Gritton return (EINVAL); 3530304c731SJamie Gritton } 3540304c731SJamie Gritton return (kern_jail(td, &j)); 3550304c731SJamie Gritton } 3560304c731SJamie Gritton 3570304c731SJamie Gritton int 3580304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j) 3590304c731SJamie Gritton { 360e92e0574SJamie Gritton struct iovec optiov[2 * (4 361e92e0574SJamie Gritton + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 362e92e0574SJamie Gritton #ifdef INET 363e92e0574SJamie Gritton + 1 364e92e0574SJamie Gritton #endif 365e92e0574SJamie Gritton #ifdef INET6 366e92e0574SJamie Gritton + 1 367e92e0574SJamie Gritton #endif 368e92e0574SJamie Gritton )]; 3690304c731SJamie Gritton struct uio opt; 3700304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 3710304c731SJamie Gritton #ifdef INET 372e92e0574SJamie Gritton uint32_t ip4s; 3730304c731SJamie Gritton struct in_addr *u_ip4; 3740304c731SJamie Gritton #endif 3750304c731SJamie Gritton #ifdef INET6 3760304c731SJamie Gritton struct in6_addr *u_ip6; 3770304c731SJamie Gritton #endif 3780304c731SJamie Gritton size_t tmplen; 3790304c731SJamie Gritton int error, enforce_statfs, fi; 3800304c731SJamie Gritton 3810304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3820304c731SJamie Gritton opt.uio_iov = optiov; 3830304c731SJamie Gritton opt.uio_iovcnt = 0; 3840304c731SJamie Gritton opt.uio_offset = -1; 3850304c731SJamie Gritton opt.uio_resid = -1; 3860304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3870304c731SJamie Gritton opt.uio_rw = UIO_READ; 3880304c731SJamie Gritton opt.uio_td = td; 3890304c731SJamie Gritton 3900304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3910304c731SJamie Gritton if (!jailed(td->td_ucred)) { 3920304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / 3930304c731SJamie Gritton sizeof(pr_allow_names[0]); fi++) { 3940304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = 3950304c731SJamie Gritton (jail_default_allow & (1 << fi)) 3960304c731SJamie Gritton ? pr_allow_names[fi] : pr_allow_nonames[fi]; 3970304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3980304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3990304c731SJamie Gritton opt.uio_iovcnt += 2; 4000304c731SJamie Gritton } 4010304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 4020304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 4030304c731SJamie Gritton opt.uio_iovcnt++; 4040304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 4050304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 4060304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 4070304c731SJamie Gritton opt.uio_iovcnt++; 4080304c731SJamie Gritton } 4090304c731SJamie Gritton 410b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 411b38ff370SJamie Gritton #ifdef INET 4120304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 4130304c731SJamie Gritton if (ip4s > jail_max_af_ips) 414b38ff370SJamie Gritton return (EINVAL); 4150304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 416b38ff370SJamie Gritton #else 4170304c731SJamie Gritton if (j->ip4s > 0) 418b38ff370SJamie Gritton return (EINVAL); 419b38ff370SJamie Gritton #endif 420b38ff370SJamie Gritton #ifdef INET6 4210304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 422b38ff370SJamie Gritton return (EINVAL); 4230304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 424b38ff370SJamie Gritton #else 4250304c731SJamie Gritton if (j->ip6s > 0) 426b38ff370SJamie Gritton return (EINVAL); 427b38ff370SJamie Gritton #endif 428b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 429b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 430b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 431b38ff370SJamie Gritton #ifdef INET 432b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 433b38ff370SJamie Gritton #endif 434b38ff370SJamie Gritton #ifdef INET6 435b38ff370SJamie Gritton #ifdef INET 4360304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 437b38ff370SJamie Gritton #else 438b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 439b38ff370SJamie Gritton #endif 440b38ff370SJamie Gritton #endif 4410304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4420304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4430304c731SJamie Gritton opt.uio_iovcnt++; 4440304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4450304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4460304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 447b38ff370SJamie Gritton if (error) { 448b38ff370SJamie Gritton free(u_path, M_TEMP); 449b38ff370SJamie Gritton return (error); 450b38ff370SJamie Gritton } 4510304c731SJamie Gritton opt.uio_iovcnt++; 4520304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4530304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4540304c731SJamie Gritton opt.uio_iovcnt++; 4550304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4560304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4570304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 458b38ff370SJamie Gritton if (error) { 459b38ff370SJamie Gritton free(u_path, M_TEMP); 460b38ff370SJamie Gritton return (error); 461b38ff370SJamie Gritton } 4620304c731SJamie Gritton opt.uio_iovcnt++; 4630304c731SJamie Gritton if (j->jailname != NULL) { 464b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 465b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 466b38ff370SJamie Gritton opt.uio_iovcnt++; 467b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4680304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 469b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 470b38ff370SJamie Gritton if (error) { 471b38ff370SJamie Gritton free(u_path, M_TEMP); 472b38ff370SJamie Gritton return (error); 473b38ff370SJamie Gritton } 474b38ff370SJamie Gritton opt.uio_iovcnt++; 475b38ff370SJamie Gritton } 476b38ff370SJamie Gritton #ifdef INET 477b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 478b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 479b38ff370SJamie Gritton opt.uio_iovcnt++; 480b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4810304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4820304c731SJamie Gritton if (j->version == 0) 4830304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4840304c731SJamie Gritton else { 4850304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 486b38ff370SJamie Gritton if (error) { 487b38ff370SJamie Gritton free(u_path, M_TEMP); 488b38ff370SJamie Gritton return (error); 489b38ff370SJamie Gritton } 4900304c731SJamie Gritton } 491b38ff370SJamie Gritton opt.uio_iovcnt++; 492b38ff370SJamie Gritton #endif 493b38ff370SJamie Gritton #ifdef INET6 494b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 495b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 496b38ff370SJamie Gritton opt.uio_iovcnt++; 497b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4980304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4990304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 500b38ff370SJamie Gritton if (error) { 501b38ff370SJamie Gritton free(u_path, M_TEMP); 502b38ff370SJamie Gritton return (error); 503b38ff370SJamie Gritton } 504b38ff370SJamie Gritton opt.uio_iovcnt++; 505b38ff370SJamie Gritton #endif 5060304c731SJamie Gritton KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 5070304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 508b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 509b38ff370SJamie Gritton free(u_path, M_TEMP); 510b38ff370SJamie Gritton return (error); 511b38ff370SJamie Gritton } 512b38ff370SJamie Gritton 5130304c731SJamie Gritton 514b38ff370SJamie Gritton /* 515b38ff370SJamie Gritton * struct jail_set_args { 516b38ff370SJamie Gritton * struct iovec *iovp; 517b38ff370SJamie Gritton * unsigned int iovcnt; 518b38ff370SJamie Gritton * int flags; 519b38ff370SJamie Gritton * }; 520b38ff370SJamie Gritton */ 521b38ff370SJamie Gritton int 5228451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 523b38ff370SJamie Gritton { 524b38ff370SJamie Gritton struct uio *auio; 525b38ff370SJamie Gritton int error; 526b38ff370SJamie Gritton 527b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 528b38ff370SJamie Gritton if (uap->iovcnt & 1) 529b38ff370SJamie Gritton return (EINVAL); 530b38ff370SJamie Gritton 531b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 532b38ff370SJamie Gritton if (error) 533b38ff370SJamie Gritton return (error); 534b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 535b38ff370SJamie Gritton free(auio, M_IOV); 536b38ff370SJamie Gritton return (error); 537413628a7SBjoern A. Zeeb } 538413628a7SBjoern A. Zeeb 539413628a7SBjoern A. Zeeb int 540b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 541413628a7SBjoern A. Zeeb { 542fd7a8150SMike Barcroft struct nameidata nd; 543b38ff370SJamie Gritton #ifdef INET 544b38ff370SJamie Gritton struct in_addr *ip4; 545413628a7SBjoern A. Zeeb #endif 546b38ff370SJamie Gritton #ifdef INET6 547b38ff370SJamie Gritton struct in6_addr *ip6; 548b38ff370SJamie Gritton #endif 549b38ff370SJamie Gritton struct vfsopt *opt; 550b38ff370SJamie Gritton struct vfsoptlist *opts; 55157aea6dfSBjoern A. Zeeb struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 552b38ff370SJamie Gritton struct vnode *root; 553babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 554b96bd95bSIan Lepore char *g_path, *osrelstr; 5550304c731SJamie Gritton #if defined(INET) || defined(INET6) 55657aea6dfSBjoern A. Zeeb struct prison *tppr; 557b38ff370SJamie Gritton void *op; 5580304c731SJamie Gritton #endif 55976ca6f88SJamie Gritton unsigned long hid; 5600304c731SJamie Gritton size_t namelen, onamelen; 5610304c731SJamie Gritton int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 5620cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 5637cbf7213SJamie Gritton int fi, jid, jsys, len, level; 564b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 565f6e633a9SMartin Matuska int fullpath_disabled; 566d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 5670304c731SJamie Gritton int ii, ij; 568413628a7SBjoern A. Zeeb #endif 569413628a7SBjoern A. Zeeb #ifdef INET 5702b0d6f81SJamie Gritton int ip4s, redo_ip4; 571413628a7SBjoern A. Zeeb #endif 572b38ff370SJamie Gritton #ifdef INET6 5732b0d6f81SJamie Gritton int ip6s, redo_ip6; 574b38ff370SJamie Gritton #endif 5756beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 5766beb3bb4SKirk McKusick unsigned tallow; 577b38ff370SJamie Gritton char numbuf[12]; 578b38ff370SJamie Gritton 579b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 580b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 581b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 582b38ff370SJamie Gritton if (error) 58375c13541SPoul-Henning Kamp return (error); 5840304c731SJamie Gritton mypr = ppr = td->td_ucred->cr_prison; 585b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5860304c731SJamie Gritton return (EPERM); 587b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 588b38ff370SJamie Gritton return (EINVAL); 589b38ff370SJamie Gritton 590b38ff370SJamie Gritton /* 591b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 592b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 593b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 594b38ff370SJamie Gritton * the allprison lock. 595b38ff370SJamie Gritton * 596b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 597b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 598b38ff370SJamie Gritton * than duplicate it under a different name. 599b38ff370SJamie Gritton */ 600b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 601b38ff370SJamie Gritton if (error) 602b38ff370SJamie Gritton return (error); 603b38ff370SJamie Gritton #ifdef INET 604b38ff370SJamie Gritton ip4 = NULL; 605b38ff370SJamie Gritton #endif 606b38ff370SJamie Gritton #ifdef INET6 607b38ff370SJamie Gritton ip6 = NULL; 608b38ff370SJamie Gritton #endif 6096dfe0a3dSMartin Matuska g_path = NULL; 610b38ff370SJamie Gritton 611b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 612b38ff370SJamie Gritton if (error == ENOENT) 613b38ff370SJamie Gritton jid = 0; 614b38ff370SJamie Gritton else if (error != 0) 615b38ff370SJamie Gritton goto done_free; 616b38ff370SJamie Gritton 617b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 618b38ff370SJamie Gritton if (error == ENOENT) 619b38ff370SJamie Gritton gotslevel = 0; 620b38ff370SJamie Gritton else if (error != 0) 621b38ff370SJamie Gritton goto done_free; 622b38ff370SJamie Gritton else 623b38ff370SJamie Gritton gotslevel = 1; 624b38ff370SJamie Gritton 625b97457e2SJamie Gritton error = 626b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 627b97457e2SJamie Gritton if (error == ENOENT) 628b97457e2SJamie Gritton gotchildmax = 0; 629b97457e2SJamie Gritton else if (error != 0) 630b97457e2SJamie Gritton goto done_free; 631b97457e2SJamie Gritton else 632b97457e2SJamie Gritton gotchildmax = 1; 633b97457e2SJamie Gritton 6340304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 635f337198dSJamie Gritton if (error == ENOENT) 636f337198dSJamie Gritton gotenforce = 0; 637f337198dSJamie Gritton else if (error != 0) 6380304c731SJamie Gritton goto done_free; 639f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 640f337198dSJamie Gritton error = EINVAL; 641f337198dSJamie Gritton goto done_free; 642f337198dSJamie Gritton } else 643f337198dSJamie Gritton gotenforce = 1; 6440304c731SJamie Gritton 6450cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 6460cc207a6SMartin Matuska if (error == ENOENT) 6470cc207a6SMartin Matuska gotrsnum = 0; 6480cc207a6SMartin Matuska else if (error != 0) 6490cc207a6SMartin Matuska goto done_free; 6500cc207a6SMartin Matuska else 6510cc207a6SMartin Matuska gotrsnum = 1; 6520cc207a6SMartin Matuska 653b38ff370SJamie Gritton pr_flags = ch_flags = 0; 6540304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 6550304c731SJamie Gritton fi++) { 6560304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 6570304c731SJamie Gritton continue; 6580304c731SJamie Gritton vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 6590304c731SJamie Gritton vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 6600304c731SJamie Gritton } 661b38ff370SJamie Gritton ch_flags |= pr_flags; 6627cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 6637cbf7213SJamie Gritton fi++) { 6647cbf7213SJamie Gritton error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 6657cbf7213SJamie Gritton sizeof(jsys)); 6667cbf7213SJamie Gritton if (error == ENOENT) 6677cbf7213SJamie Gritton continue; 6687cbf7213SJamie Gritton if (error != 0) 6697cbf7213SJamie Gritton goto done_free; 6707cbf7213SJamie Gritton switch (jsys) { 6717cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 6727cbf7213SJamie Gritton if (!pr_flag_jailsys[fi].disable) { 6737cbf7213SJamie Gritton error = EINVAL; 6747cbf7213SJamie Gritton goto done_free; 6757cbf7213SJamie Gritton } 6767cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].disable; 6777cbf7213SJamie Gritton break; 6787cbf7213SJamie Gritton case JAIL_SYS_NEW: 6797cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].new; 6807cbf7213SJamie Gritton break; 6817cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 6827cbf7213SJamie Gritton break; 6837cbf7213SJamie Gritton default: 6847cbf7213SJamie Gritton error = EINVAL; 6857cbf7213SJamie Gritton goto done_free; 6867cbf7213SJamie Gritton } 6877cbf7213SJamie Gritton ch_flags |= 6887cbf7213SJamie Gritton pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 6897cbf7213SJamie Gritton } 6904affa14cSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 6914affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 6924affa14cSJamie Gritton error = EINVAL; 6934affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 6944affa14cSJamie Gritton goto done_errmsg; 6954affa14cSJamie Gritton } 696679e1390SJamie Gritton #ifdef VIMAGE 697679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 698679e1390SJamie Gritton error = EINVAL; 699679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 700679e1390SJamie Gritton goto done_errmsg; 701679e1390SJamie Gritton } 702679e1390SJamie Gritton #endif 7032b0d6f81SJamie Gritton #ifdef INET 7042b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 7052b0d6f81SJamie Gritton error = EINVAL; 7062b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 7072b0d6f81SJamie Gritton goto done_errmsg; 7082b0d6f81SJamie Gritton } 7092b0d6f81SJamie Gritton #endif 7102b0d6f81SJamie Gritton #ifdef INET6 7112b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 7122b0d6f81SJamie Gritton error = EINVAL; 7132b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 7142b0d6f81SJamie Gritton goto done_errmsg; 7152b0d6f81SJamie Gritton } 7162b0d6f81SJamie Gritton #endif 717b38ff370SJamie Gritton 7180304c731SJamie Gritton pr_allow = ch_allow = 0; 7190304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 7200304c731SJamie Gritton fi++) { 7210304c731SJamie Gritton vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 7220304c731SJamie Gritton vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 7230304c731SJamie Gritton } 7240304c731SJamie Gritton ch_allow |= pr_allow; 7250304c731SJamie Gritton 726b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 727b38ff370SJamie Gritton if (error == ENOENT) 728b38ff370SJamie Gritton name = NULL; 729b38ff370SJamie Gritton else if (error != 0) 730b38ff370SJamie Gritton goto done_free; 731b38ff370SJamie Gritton else { 732b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 733b38ff370SJamie Gritton error = EINVAL; 734b38ff370SJamie Gritton goto done_free; 735b38ff370SJamie Gritton } 736b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 737b38ff370SJamie Gritton error = ENAMETOOLONG; 738b38ff370SJamie Gritton goto done_free; 739b38ff370SJamie Gritton } 740b38ff370SJamie Gritton } 741b38ff370SJamie Gritton 742b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 743b38ff370SJamie Gritton if (error == ENOENT) 744b38ff370SJamie Gritton host = NULL; 745b38ff370SJamie Gritton else if (error != 0) 746b38ff370SJamie Gritton goto done_free; 747b38ff370SJamie Gritton else { 74876ca6f88SJamie Gritton ch_flags |= PR_HOST; 74976ca6f88SJamie Gritton pr_flags |= PR_HOST; 750b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 751b38ff370SJamie Gritton error = EINVAL; 752b38ff370SJamie Gritton goto done_free; 753b38ff370SJamie Gritton } 754b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 755b38ff370SJamie Gritton error = ENAMETOOLONG; 756b38ff370SJamie Gritton goto done_free; 757b38ff370SJamie Gritton } 758b38ff370SJamie Gritton } 759b38ff370SJamie Gritton 76076ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 76176ca6f88SJamie Gritton if (error == ENOENT) 76276ca6f88SJamie Gritton domain = NULL; 76376ca6f88SJamie Gritton else if (error != 0) 76476ca6f88SJamie Gritton goto done_free; 76576ca6f88SJamie Gritton else { 76676ca6f88SJamie Gritton ch_flags |= PR_HOST; 76776ca6f88SJamie Gritton pr_flags |= PR_HOST; 76876ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 76976ca6f88SJamie Gritton error = EINVAL; 77076ca6f88SJamie Gritton goto done_free; 77176ca6f88SJamie Gritton } 77276ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 77376ca6f88SJamie Gritton error = ENAMETOOLONG; 77476ca6f88SJamie Gritton goto done_free; 77576ca6f88SJamie Gritton } 77676ca6f88SJamie Gritton } 77776ca6f88SJamie Gritton 77876ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 77976ca6f88SJamie Gritton if (error == ENOENT) 78076ca6f88SJamie Gritton uuid = NULL; 78176ca6f88SJamie Gritton else if (error != 0) 78276ca6f88SJamie Gritton goto done_free; 78376ca6f88SJamie Gritton else { 78476ca6f88SJamie Gritton ch_flags |= PR_HOST; 78576ca6f88SJamie Gritton pr_flags |= PR_HOST; 78676ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 78776ca6f88SJamie Gritton error = EINVAL; 78876ca6f88SJamie Gritton goto done_free; 78976ca6f88SJamie Gritton } 79076ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 79176ca6f88SJamie Gritton error = ENAMETOOLONG; 79276ca6f88SJamie Gritton goto done_free; 79376ca6f88SJamie Gritton } 79476ca6f88SJamie Gritton } 79576ca6f88SJamie Gritton 796841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 797a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 79876ca6f88SJamie Gritton uint32_t hid32; 79976ca6f88SJamie Gritton 80076ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 80176ca6f88SJamie Gritton hid = hid32; 80276ca6f88SJamie Gritton } else 80376ca6f88SJamie Gritton #endif 80476ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 80576ca6f88SJamie Gritton if (error == ENOENT) 80676ca6f88SJamie Gritton gothid = 0; 80776ca6f88SJamie Gritton else if (error != 0) 80876ca6f88SJamie Gritton goto done_free; 80976ca6f88SJamie Gritton else { 81076ca6f88SJamie Gritton gothid = 1; 81176ca6f88SJamie Gritton ch_flags |= PR_HOST; 81276ca6f88SJamie Gritton pr_flags |= PR_HOST; 81376ca6f88SJamie Gritton } 81476ca6f88SJamie Gritton 815b38ff370SJamie Gritton #ifdef INET 816b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 817b38ff370SJamie Gritton if (error == ENOENT) 8180e5e396eSJamie Gritton ip4s = 0; 819b38ff370SJamie Gritton else if (error != 0) 820b38ff370SJamie Gritton goto done_free; 821b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 822b38ff370SJamie Gritton error = EINVAL; 823b38ff370SJamie Gritton goto done_free; 8240304c731SJamie Gritton } else { 8256a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 8266a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 8276a3f2779SJamie Gritton if (ip4s > 0) { 828b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 829b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 830b38ff370SJamie Gritton error = EINVAL; 831b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 832b38ff370SJamie Gritton goto done_errmsg; 833b38ff370SJamie Gritton } 8342b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 835b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 836b38ff370SJamie Gritton /* 8370304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 8380304c731SJamie Gritton * the primary IP address as given from userland. 8390304c731SJamie Gritton * This special IP is used for unbound outgoing 840bef916f9SBjoern A. Zeeb * connections as well for "loopback" traffic in case 841bef916f9SBjoern A. Zeeb * source address selection cannot find any more fitting 842bef916f9SBjoern A. Zeeb * address to connect from. 843b38ff370SJamie Gritton */ 844b38ff370SJamie Gritton if (ip4s > 1) 845b38ff370SJamie Gritton qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 846b38ff370SJamie Gritton /* 8470304c731SJamie Gritton * Check for duplicate addresses and do some simple 8480304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 8490304c731SJamie Gritton * addresses it is their problem. 850b38ff370SJamie Gritton * 8510304c731SJamie Gritton * We do not have to care about byte order for these 8520304c731SJamie Gritton * checks so we will do them in NBO. 853b38ff370SJamie Gritton */ 854b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 855b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 856b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 857b38ff370SJamie Gritton error = EINVAL; 858b38ff370SJamie Gritton goto done_free; 859b38ff370SJamie Gritton } 860b38ff370SJamie Gritton if ((ii+1) < ip4s && 861b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 862b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 863b38ff370SJamie Gritton error = EINVAL; 864b38ff370SJamie Gritton goto done_free; 865b38ff370SJamie Gritton } 866b38ff370SJamie Gritton } 867b38ff370SJamie Gritton } 8680304c731SJamie Gritton } 869b38ff370SJamie Gritton #endif 870b38ff370SJamie Gritton 871b38ff370SJamie Gritton #ifdef INET6 872b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 873b38ff370SJamie Gritton if (error == ENOENT) 8740e5e396eSJamie Gritton ip6s = 0; 875b38ff370SJamie Gritton else if (error != 0) 876b38ff370SJamie Gritton goto done_free; 877b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 878b38ff370SJamie Gritton error = EINVAL; 879b38ff370SJamie Gritton goto done_free; 8800304c731SJamie Gritton } else { 8816a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 8826a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 8836a3f2779SJamie Gritton if (ip6s > 0) { 884b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 885b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 886b38ff370SJamie Gritton error = EINVAL; 887b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 888b38ff370SJamie Gritton goto done_errmsg; 889b38ff370SJamie Gritton } 8902b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 891b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 892b38ff370SJamie Gritton if (ip6s > 1) 893b38ff370SJamie Gritton qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 894b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8950304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 896b38ff370SJamie Gritton error = EINVAL; 897b38ff370SJamie Gritton goto done_free; 898b38ff370SJamie Gritton } 899b38ff370SJamie Gritton if ((ii+1) < ip6s && 900b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 901b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 902b38ff370SJamie Gritton { 903b38ff370SJamie Gritton error = EINVAL; 904b38ff370SJamie Gritton goto done_free; 905b38ff370SJamie Gritton } 906b38ff370SJamie Gritton } 907b38ff370SJamie Gritton } 9080304c731SJamie Gritton } 909b38ff370SJamie Gritton #endif 910b38ff370SJamie Gritton 911bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 912bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 913bdfc8cc4SJamie Gritton error = EINVAL; 914bdfc8cc4SJamie Gritton vfs_opterror(opts, 915bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 916bdfc8cc4SJamie Gritton goto done_errmsg; 917bdfc8cc4SJamie Gritton } 918bdfc8cc4SJamie Gritton #endif 919bdfc8cc4SJamie Gritton 9209cbe30e1SMartin Matuska fullpath_disabled = 0; 921b38ff370SJamie Gritton root = NULL; 922b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 923b38ff370SJamie Gritton if (error == ENOENT) 924b38ff370SJamie Gritton path = NULL; 925b38ff370SJamie Gritton else if (error != 0) 926b38ff370SJamie Gritton goto done_free; 927b38ff370SJamie Gritton else { 928b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 929b38ff370SJamie Gritton error = EINVAL; 930b38ff370SJamie Gritton vfs_opterror(opts, 931b38ff370SJamie Gritton "path cannot be changed after creation"); 932b38ff370SJamie Gritton goto done_errmsg; 933b38ff370SJamie Gritton } 934b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 935b38ff370SJamie Gritton error = EINVAL; 936b38ff370SJamie Gritton goto done_free; 937b38ff370SJamie Gritton } 9385050aa86SKonstantin Belousov NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 939b38ff370SJamie Gritton path, td); 940b38ff370SJamie Gritton error = namei(&nd); 941b38ff370SJamie Gritton if (error) 942b38ff370SJamie Gritton goto done_free; 943b38ff370SJamie Gritton root = nd.ni_vp; 944b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 9456dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 9466dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 9476dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 9486dfe0a3dSMartin Matuska if (error == 0) 9496dfe0a3dSMartin Matuska path = g_path; 9506dfe0a3dSMartin Matuska else if (error == ENODEV) { 951f6e633a9SMartin Matuska /* proceed if sysctl debug.disablefullpath == 1 */ 952f6e633a9SMartin Matuska fullpath_disabled = 1; 953f6e633a9SMartin Matuska if (len < 2 || (len == 2 && path[0] == '/')) 954f6e633a9SMartin Matuska path = NULL; 9556dfe0a3dSMartin Matuska } else { 956f6e633a9SMartin Matuska /* exit on other errors */ 957b38ff370SJamie Gritton goto done_free; 958b38ff370SJamie Gritton } 959f6e633a9SMartin Matuska if (root->v_type != VDIR) { 960f6e633a9SMartin Matuska error = ENOTDIR; 961f6e633a9SMartin Matuska vput(root); 962f6e633a9SMartin Matuska goto done_free; 963f6e633a9SMartin Matuska } 964f6e633a9SMartin Matuska VOP_UNLOCK(root, 0); 965f6e633a9SMartin Matuska if (fullpath_disabled) { 966f6e633a9SMartin Matuska /* Leave room for a real-root full pathname. */ 967f6e633a9SMartin Matuska if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 968f6e633a9SMartin Matuska ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 969f6e633a9SMartin Matuska error = ENAMETOOLONG; 970f6e633a9SMartin Matuska goto done_free; 971f6e633a9SMartin Matuska } 972b38ff370SJamie Gritton } 973b38ff370SJamie Gritton } 974b38ff370SJamie Gritton 975b96bd95bSIan Lepore error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 976b96bd95bSIan Lepore if (error == ENOENT) 977b96bd95bSIan Lepore osrelstr = NULL; 978b96bd95bSIan Lepore else if (error != 0) 979b96bd95bSIan Lepore goto done_free; 980b96bd95bSIan Lepore else { 981b96bd95bSIan Lepore if (flags & JAIL_UPDATE) { 982b96bd95bSIan Lepore error = EINVAL; 983b96bd95bSIan Lepore vfs_opterror(opts, 984b96bd95bSIan Lepore "osrelease cannot be changed after creation"); 985b96bd95bSIan Lepore goto done_errmsg; 986b96bd95bSIan Lepore } 987b96bd95bSIan Lepore if (len == 0 || len >= OSRELEASELEN) { 988b96bd95bSIan Lepore error = EINVAL; 989b96bd95bSIan Lepore vfs_opterror(opts, 990b96bd95bSIan Lepore "osrelease string must be 1-%d bytes long", 991b96bd95bSIan Lepore OSRELEASELEN - 1); 992b96bd95bSIan Lepore goto done_errmsg; 993b96bd95bSIan Lepore } 994b96bd95bSIan Lepore } 995b96bd95bSIan Lepore 996b96bd95bSIan Lepore error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 997b96bd95bSIan Lepore if (error == ENOENT) 998b96bd95bSIan Lepore osreldt = 0; 999b96bd95bSIan Lepore else if (error != 0) 1000b96bd95bSIan Lepore goto done_free; 1001b96bd95bSIan Lepore else { 1002b96bd95bSIan Lepore if (flags & JAIL_UPDATE) { 1003b96bd95bSIan Lepore error = EINVAL; 1004b96bd95bSIan Lepore vfs_opterror(opts, 1005b96bd95bSIan Lepore "osreldate cannot be changed after creation"); 1006b96bd95bSIan Lepore goto done_errmsg; 1007b96bd95bSIan Lepore } 1008b96bd95bSIan Lepore if (osreldt == 0) { 1009b96bd95bSIan Lepore error = EINVAL; 1010b96bd95bSIan Lepore vfs_opterror(opts, "osreldate cannot be 0"); 1011b96bd95bSIan Lepore goto done_errmsg; 1012b96bd95bSIan Lepore } 1013b96bd95bSIan Lepore } 1014b96bd95bSIan Lepore 1015b38ff370SJamie Gritton /* 1016b38ff370SJamie Gritton * Grab the allprison lock before letting modules check their 1017b38ff370SJamie Gritton * parameters. Once we have it, do not let go so we'll have a 1018b38ff370SJamie Gritton * consistent view of the OSD list. 1019b38ff370SJamie Gritton */ 1020b38ff370SJamie Gritton sx_xlock(&allprison_lock); 1021b38ff370SJamie Gritton error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 1022b38ff370SJamie Gritton if (error) 1023b38ff370SJamie Gritton goto done_unlock_list; 1024b38ff370SJamie Gritton 1025b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 1026b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1027b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1028b38ff370SJamie Gritton error = EINVAL; 1029b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 1030b38ff370SJamie Gritton goto done_unlock_list; 1031b38ff370SJamie Gritton } 1032b38ff370SJamie Gritton } 1033b38ff370SJamie Gritton 1034b38ff370SJamie Gritton /* 1035b38ff370SJamie Gritton * See if we are creating a new record or updating an existing one. 1036b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 1037b38ff370SJamie Gritton */ 1038b38ff370SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 1039b38ff370SJamie Gritton if (!cuflags) { 1040b38ff370SJamie Gritton error = EINVAL; 1041b38ff370SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 1042b38ff370SJamie Gritton goto done_unlock_list; 1043b38ff370SJamie Gritton } 1044b38ff370SJamie Gritton pr = NULL; 1045babbbb9cSJamie Gritton namelc = NULL; 1046babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1047babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1048babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1049babbbb9cSJamie Gritton if (*p != '\0') 1050babbbb9cSJamie Gritton jid = 0; 1051babbbb9cSJamie Gritton } 1052b38ff370SJamie Gritton if (jid != 0) { 10530304c731SJamie Gritton /* 10540304c731SJamie Gritton * See if a requested jid already exists. There is an 10550304c731SJamie Gritton * information leak here if the jid exists but is not within 10560304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 10570304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 10580304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 10590304c731SJamie Gritton */ 1060b38ff370SJamie Gritton if (jid < 0) { 1061b38ff370SJamie Gritton error = EINVAL; 1062b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 1063b38ff370SJamie Gritton goto done_unlock_list; 1064b38ff370SJamie Gritton } 1065b38ff370SJamie Gritton pr = prison_find(jid); 1066b38ff370SJamie Gritton if (pr != NULL) { 10670304c731SJamie Gritton ppr = pr->pr_parent; 1068b38ff370SJamie Gritton /* Create: jid must not exist. */ 1069b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 1070b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1071b38ff370SJamie Gritton error = EEXIST; 1072b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 1073b38ff370SJamie Gritton jid); 1074b38ff370SJamie Gritton goto done_unlock_list; 1075b38ff370SJamie Gritton } 10760304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 10770304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10780304c731SJamie Gritton pr = NULL; 10790304c731SJamie Gritton } else if (pr->pr_uref == 0) { 1080b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 1081b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1082b38ff370SJamie Gritton error = ENOENT; 1083b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1084b38ff370SJamie Gritton jid); 1085b38ff370SJamie Gritton goto done_unlock_list; 1086b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 1087b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1088b38ff370SJamie Gritton /* 1089b38ff370SJamie Gritton * A dying jail might be resurrected 1090b38ff370SJamie Gritton * (via attach or persist), but first 1091b38ff370SJamie Gritton * it must determine if another jail 1092b38ff370SJamie Gritton * has claimed its name. Accomplish 1093b38ff370SJamie Gritton * this by implicitly re-setting the 1094b38ff370SJamie Gritton * name. 1095b38ff370SJamie Gritton */ 1096b38ff370SJamie Gritton if (name == NULL) 10970304c731SJamie Gritton name = prison_name(mypr, pr); 1098b38ff370SJamie Gritton } 1099b38ff370SJamie Gritton } 1100b38ff370SJamie Gritton } 1101b38ff370SJamie Gritton if (pr == NULL) { 1102b38ff370SJamie Gritton /* Update: jid must exist. */ 1103b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1104b38ff370SJamie Gritton error = ENOENT; 1105b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1106b38ff370SJamie Gritton goto done_unlock_list; 1107b38ff370SJamie Gritton } 1108b38ff370SJamie Gritton } 1109b38ff370SJamie Gritton } 1110b38ff370SJamie Gritton /* 1111b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1112b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1113b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1114b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1115b38ff370SJamie Gritton * because that is the jail being updated). 1116b38ff370SJamie Gritton */ 1117b38ff370SJamie Gritton if (name != NULL) { 1118babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1119babbbb9cSJamie Gritton if (namelc == NULL) 1120babbbb9cSJamie Gritton namelc = name; 1121babbbb9cSJamie Gritton else { 11220304c731SJamie Gritton /* 11230304c731SJamie Gritton * This is a hierarchical name. Split it into the 11240304c731SJamie Gritton * parent and child names, and make sure the parent 11250304c731SJamie Gritton * exists or matches an already found jail. 11260304c731SJamie Gritton */ 1127babbbb9cSJamie Gritton *namelc = '\0'; 11280304c731SJamie Gritton if (pr != NULL) { 1129babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1130babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 11310304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 11320304c731SJamie Gritton error = EINVAL; 11330304c731SJamie Gritton vfs_opterror(opts, 11340304c731SJamie Gritton "cannot change jail's parent"); 11350304c731SJamie Gritton goto done_unlock_list; 11360304c731SJamie Gritton } 11370304c731SJamie Gritton } else { 11380304c731SJamie Gritton ppr = prison_find_name(mypr, name); 11390304c731SJamie Gritton if (ppr == NULL) { 11400304c731SJamie Gritton error = ENOENT; 11410304c731SJamie Gritton vfs_opterror(opts, 11420304c731SJamie Gritton "jail \"%s\" not found", name); 11430304c731SJamie Gritton goto done_unlock_list; 11440304c731SJamie Gritton } 11450304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 11460304c731SJamie Gritton } 1147babbbb9cSJamie Gritton name = ++namelc; 11480304c731SJamie Gritton } 1149b38ff370SJamie Gritton if (name[0] != '\0') { 11500304c731SJamie Gritton namelen = 11510304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1152b38ff370SJamie Gritton name_again: 11530304c731SJamie Gritton deadpr = NULL; 11540304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1155b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 11560304c731SJamie Gritton !strcmp(tpr->pr_name + namelen, name)) { 1157b38ff370SJamie Gritton if (pr == NULL && 1158b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1159b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1160b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1161b38ff370SJamie Gritton /* 1162b38ff370SJamie Gritton * Use this jail 1163b38ff370SJamie Gritton * for updates. 1164b38ff370SJamie Gritton */ 1165b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1166b38ff370SJamie Gritton pr = tpr; 1167b38ff370SJamie Gritton break; 1168b38ff370SJamie Gritton } 1169b38ff370SJamie Gritton deadpr = tpr; 1170b38ff370SJamie Gritton } 1171b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1172b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1173b38ff370SJamie Gritton /* 1174b38ff370SJamie Gritton * Create, or update(jid): 1175b38ff370SJamie Gritton * name must not exist in an 11760304c731SJamie Gritton * active sibling jail. 1177b38ff370SJamie Gritton */ 1178b38ff370SJamie Gritton error = EEXIST; 1179b38ff370SJamie Gritton if (pr != NULL) 1180b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1181b38ff370SJamie Gritton vfs_opterror(opts, 1182b38ff370SJamie Gritton "jail \"%s\" already exists", 1183b38ff370SJamie Gritton name); 1184b38ff370SJamie Gritton goto done_unlock_list; 1185b38ff370SJamie Gritton } 1186b38ff370SJamie Gritton } 1187b38ff370SJamie Gritton } 1188b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1189b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1190b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1191b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1192b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1193b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1194b38ff370SJamie Gritton goto name_again; 1195b38ff370SJamie Gritton } 1196b38ff370SJamie Gritton pr = deadpr; 1197b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1198b38ff370SJamie Gritton error = ENOENT; 1199b38ff370SJamie Gritton vfs_opterror(opts, 1200b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1201b38ff370SJamie Gritton goto done_unlock_list; 1202b38ff370SJamie Gritton } 1203b38ff370SJamie Gritton } 1204b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1205b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1206b38ff370SJamie Gritton error = ENOENT; 1207b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1208b38ff370SJamie Gritton name); 1209b38ff370SJamie Gritton goto done_unlock_list; 1210b38ff370SJamie Gritton } 1211b38ff370SJamie Gritton } 1212b38ff370SJamie Gritton } 1213b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1214b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1215b38ff370SJamie Gritton error = ENOENT; 1216b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1217b38ff370SJamie Gritton goto done_unlock_list; 1218b38ff370SJamie Gritton } 1219b38ff370SJamie Gritton 1220b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1221b38ff370SJamie Gritton if (pr == NULL) { 1222b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1223b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1224b97457e2SJamie Gritton error = EPERM; 1225b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1226b97457e2SJamie Gritton goto done_unlock_list; 1227b97457e2SJamie Gritton } 1228b38ff370SJamie Gritton created = 1; 12290304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 12300304c731SJamie Gritton if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 12310304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 12320304c731SJamie Gritton error = ENOENT; 12330304c731SJamie Gritton vfs_opterror(opts, "parent jail went away!"); 12340304c731SJamie Gritton goto done_unlock_list; 12350304c731SJamie Gritton } 12360304c731SJamie Gritton ppr->pr_ref++; 12370304c731SJamie Gritton ppr->pr_uref++; 12380304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1239b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1240b38ff370SJamie Gritton if (jid == 0) { 1241b38ff370SJamie Gritton /* Find the next free jid. */ 1242b38ff370SJamie Gritton jid = lastprid + 1; 1243b38ff370SJamie Gritton findnext: 1244b38ff370SJamie Gritton if (jid == JAIL_MAX) 1245b38ff370SJamie Gritton jid = 1; 1246b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1247b38ff370SJamie Gritton if (tpr->pr_id < jid) 1248b38ff370SJamie Gritton continue; 1249b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1250b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1251b38ff370SJamie Gritton break; 1252b38ff370SJamie Gritton } 1253b38ff370SJamie Gritton if (jid == lastprid) { 1254b38ff370SJamie Gritton error = EAGAIN; 1255b38ff370SJamie Gritton vfs_opterror(opts, 1256b38ff370SJamie Gritton "no available jail IDs"); 1257b38ff370SJamie Gritton free(pr, M_PRISON); 12580304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 12590304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 12600304c731SJamie Gritton goto done_releroot; 1261b38ff370SJamie Gritton } 1262b38ff370SJamie Gritton jid++; 1263b38ff370SJamie Gritton goto findnext; 1264b38ff370SJamie Gritton } 1265b38ff370SJamie Gritton lastprid = jid; 1266b38ff370SJamie Gritton } else { 1267b38ff370SJamie Gritton /* 1268b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1269b38ff370SJamie Gritton * so just find where to insert it. 1270b38ff370SJamie Gritton */ 1271b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1272b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1273b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1274b38ff370SJamie Gritton break; 1275b38ff370SJamie Gritton } 1276b38ff370SJamie Gritton } 1277b38ff370SJamie Gritton if (tpr == NULL) 1278b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 12790304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 12800304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1281b97457e2SJamie Gritton tpr->pr_childcount++; 1282b38ff370SJamie Gritton 12830304c731SJamie Gritton pr->pr_parent = ppr; 1284b38ff370SJamie Gritton pr->pr_id = jid; 12850304c731SJamie Gritton 12860304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1287b38ff370SJamie Gritton if (name == NULL) 1288b38ff370SJamie Gritton name = ""; 1289b38ff370SJamie Gritton if (path == NULL) { 1290b38ff370SJamie Gritton path = "/"; 12910304c731SJamie Gritton root = mypr->pr_root; 1292b38ff370SJamie Gritton vref(root); 1293b38ff370SJamie Gritton } 12948986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 12958986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1296bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1297bdfc8cc4SJamie Gritton #ifdef VIMAGE 1298bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1299bdfc8cc4SJamie Gritton #endif 1300bdfc8cc4SJamie Gritton { 13010304c731SJamie Gritton #ifdef INET 13022b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 13036a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 13042b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 13052b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 13062b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 13072b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 13082b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 13092b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 13102b0d6f81SJamie Gritton M_WAITOK); 13112b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 13122b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 13132b0d6f81SJamie Gritton } 13142b0d6f81SJamie Gritton } 13150304c731SJamie Gritton #endif 13160304c731SJamie Gritton #ifdef INET6 13172b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 13186a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 13192b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 13202b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 13212b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 13222b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 13232b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 13242b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 13252b0d6f81SJamie Gritton M_WAITOK); 13262b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 13272b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 13282b0d6f81SJamie Gritton } 13292b0d6f81SJamie Gritton } 13300304c731SJamie Gritton #endif 1331bdfc8cc4SJamie Gritton } 1332bdfc8cc4SJamie Gritton #endif 1333592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1334592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1335592bcae8SBjoern A. Zeeb 13360304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 13370304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1338af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1339bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1340b38ff370SJamie Gritton 1341b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1342b96bd95bSIan Lepore if (osrelstr == NULL) 1343b96bd95bSIan Lepore strcpy(pr->pr_osrelease, ppr->pr_osrelease); 1344b96bd95bSIan Lepore else 1345b96bd95bSIan Lepore strcpy(pr->pr_osrelease, osrelstr); 1346b96bd95bSIan Lepore 13470304c731SJamie Gritton LIST_INIT(&pr->pr_children); 13480304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1349b38ff370SJamie Gritton 1350679e1390SJamie Gritton #ifdef VIMAGE 1351679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1352679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1353679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1354679e1390SJamie Gritton #endif 1355b38ff370SJamie Gritton /* 1356b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1357b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1358b38ff370SJamie Gritton */ 13590304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1360b38ff370SJamie Gritton if (error) { 1361b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1362b38ff370SJamie Gritton goto done_releroot; 1363b38ff370SJamie Gritton } 1364b38ff370SJamie Gritton 1365b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1366b38ff370SJamie Gritton /* 1367b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1368b38ff370SJamie Gritton * want other to see the incomplete prison once the 1369b38ff370SJamie Gritton * allprison_lock is downgraded. 1370b38ff370SJamie Gritton */ 1371b38ff370SJamie Gritton } else { 1372b38ff370SJamie Gritton created = 0; 13732b0d6f81SJamie Gritton /* 13742b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 13752b0d6f81SJamie Gritton * continue to exist for the duration of the call. 13762b0d6f81SJamie Gritton */ 13772b0d6f81SJamie Gritton pr->pr_ref++; 1378bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1379bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1380bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1381bdfc8cc4SJamie Gritton error = EINVAL; 1382bdfc8cc4SJamie Gritton vfs_opterror(opts, 1383bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1384bdfc8cc4SJamie Gritton goto done_deref_locked; 1385bdfc8cc4SJamie Gritton } 1386bdfc8cc4SJamie Gritton #endif 13872b0d6f81SJamie Gritton #ifdef INET 13882b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13892b0d6f81SJamie Gritton error = EINVAL; 13902b0d6f81SJamie Gritton vfs_opterror(opts, 13912b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 13922b0d6f81SJamie Gritton goto done_deref_locked; 13932b0d6f81SJamie Gritton } 13942b0d6f81SJamie Gritton #endif 13952b0d6f81SJamie Gritton #ifdef INET6 13962b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13972b0d6f81SJamie Gritton error = EINVAL; 13982b0d6f81SJamie Gritton vfs_opterror(opts, 13992b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 14002b0d6f81SJamie Gritton goto done_deref_locked; 14012b0d6f81SJamie Gritton } 14022b0d6f81SJamie Gritton #endif 1403b38ff370SJamie Gritton } 1404b38ff370SJamie Gritton 1405b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 14060304c731SJamie Gritton if (gotslevel) { 14070304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 14080304c731SJamie Gritton error = EPERM; 14090304c731SJamie Gritton goto done_deref_locked; 14100304c731SJamie Gritton } 14110304c731SJamie Gritton } 1412b97457e2SJamie Gritton if (gotchildmax) { 1413b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1414b97457e2SJamie Gritton error = EPERM; 1415b97457e2SJamie Gritton goto done_deref_locked; 1416b97457e2SJamie Gritton } 1417b97457e2SJamie Gritton } 14180304c731SJamie Gritton if (gotenforce) { 14190304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 14200304c731SJamie Gritton error = EPERM; 14210304c731SJamie Gritton goto done_deref_locked; 14220304c731SJamie Gritton } 14230304c731SJamie Gritton } 14240cc207a6SMartin Matuska if (gotrsnum) { 14250cc207a6SMartin Matuska /* 14260cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 14270cc207a6SMartin Matuska */ 1428bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 14290cc207a6SMartin Matuska error = EINVAL; 14300cc207a6SMartin Matuska goto done_deref_locked; 14310cc207a6SMartin Matuska } 14320cc207a6SMartin Matuska /* 1433bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 14340cc207a6SMartin Matuska */ 14350cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 14360cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 14370cc207a6SMartin Matuska error = EPERM; 14380cc207a6SMartin Matuska goto done_deref_locked; 1439bf3db8aaSMartin Matuska } else 14400cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 14410cc207a6SMartin Matuska } 14420cc207a6SMartin Matuska } 1443b38ff370SJamie Gritton #ifdef INET 14442b0d6f81SJamie Gritton if (ip4s > 0) { 14450304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 14460304c731SJamie Gritton /* 14470304c731SJamie Gritton * Make sure the new set of IP addresses is a 14480304c731SJamie Gritton * subset of the parent's list. Don't worry 14490304c731SJamie Gritton * about the parent being unlocked, as any 14500304c731SJamie Gritton * setting is done with allprison_lock held. 14510304c731SJamie Gritton */ 14520304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 14532b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 14540304c731SJamie Gritton break; 14550304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 14560304c731SJamie Gritton error = EPERM; 14570304c731SJamie Gritton goto done_deref_locked; 14580304c731SJamie Gritton } 14590304c731SJamie Gritton if (ip4s > 1) { 14600304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 14610304c731SJamie Gritton if (ip4[ii].s_addr == 14620304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1463b38ff370SJamie Gritton continue; 14640304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 14650304c731SJamie Gritton if (ip4[ii].s_addr == 14660304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 14670304c731SJamie Gritton break; 14680304c731SJamie Gritton if (ij == ppr->pr_ip4s) 14690304c731SJamie Gritton break; 14700304c731SJamie Gritton } 14710304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 14720304c731SJamie Gritton error = EPERM; 14730304c731SJamie Gritton goto done_deref_locked; 14740304c731SJamie Gritton } 14750304c731SJamie Gritton } 14760304c731SJamie Gritton } 14770304c731SJamie Gritton /* 14780304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 14790304c731SJamie Gritton * if there is no more than one IP on each jail. If 14800304c731SJamie Gritton * there is a duplicate on a jail with more than one 14810304c731SJamie Gritton * IP stop checking and return error. 14820304c731SJamie Gritton */ 1483bdfc8cc4SJamie Gritton tppr = ppr; 1484bdfc8cc4SJamie Gritton #ifdef VIMAGE 1485bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1486bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1487bdfc8cc4SJamie Gritton break; 1488bdfc8cc4SJamie Gritton #endif 1489bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1490bdfc8cc4SJamie Gritton if (tpr == pr || 1491bdfc8cc4SJamie Gritton #ifdef VIMAGE 14922b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1493bdfc8cc4SJamie Gritton #endif 1494bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14950304c731SJamie Gritton descend = 0; 14960304c731SJamie Gritton continue; 14970304c731SJamie Gritton } 14980304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 14990304c731SJamie Gritton continue; 15000304c731SJamie Gritton descend = 0; 15010304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 15020304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 15030304c731SJamie Gritton continue; 15040304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 15052b0d6f81SJamie Gritton if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 15060304c731SJamie Gritton error = EADDRINUSE; 1507b38ff370SJamie Gritton vfs_opterror(opts, 1508b38ff370SJamie Gritton "IPv4 addresses clash"); 1509b38ff370SJamie Gritton goto done_deref_locked; 1510b38ff370SJamie Gritton } 15110304c731SJamie Gritton } 15120304c731SJamie Gritton } 15130304c731SJamie Gritton } 1514b38ff370SJamie Gritton #endif 1515b38ff370SJamie Gritton #ifdef INET6 15162b0d6f81SJamie Gritton if (ip6s > 0) { 15170304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 15180304c731SJamie Gritton /* 15190304c731SJamie Gritton * Make sure the new set of IP addresses is a 15200304c731SJamie Gritton * subset of the parent's list. 15210304c731SJamie Gritton */ 15220304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 15230304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 15240304c731SJamie Gritton &ppr->pr_ip6[ij])) 15250304c731SJamie Gritton break; 15260304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 15270304c731SJamie Gritton error = EPERM; 15280304c731SJamie Gritton goto done_deref_locked; 15290304c731SJamie Gritton } 15300304c731SJamie Gritton if (ip6s > 1) { 15310304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 15320304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 15330304c731SJamie Gritton &ppr->pr_ip6[0])) 15340304c731SJamie Gritton continue; 15350304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 15360304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 15372b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 15380304c731SJamie Gritton break; 15390304c731SJamie Gritton if (ij == ppr->pr_ip6s) 15400304c731SJamie Gritton break; 15410304c731SJamie Gritton } 15420304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 15430304c731SJamie Gritton error = EPERM; 15440304c731SJamie Gritton goto done_deref_locked; 15450304c731SJamie Gritton } 15460304c731SJamie Gritton } 15470304c731SJamie Gritton } 15480304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1549bdfc8cc4SJamie Gritton tppr = ppr; 1550bdfc8cc4SJamie Gritton #ifdef VIMAGE 1551bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1552bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1553bdfc8cc4SJamie Gritton break; 1554bdfc8cc4SJamie Gritton #endif 1555bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1556bdfc8cc4SJamie Gritton if (tpr == pr || 1557bdfc8cc4SJamie Gritton #ifdef VIMAGE 15582b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1559bdfc8cc4SJamie Gritton #endif 1560bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 15610304c731SJamie Gritton descend = 0; 15620304c731SJamie Gritton continue; 15630304c731SJamie Gritton } 15640304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 15650304c731SJamie Gritton continue; 15660304c731SJamie Gritton descend = 0; 15670304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 15680304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 15690304c731SJamie Gritton continue; 15700304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 15712b0d6f81SJamie Gritton if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 15720304c731SJamie Gritton error = EADDRINUSE; 1573b38ff370SJamie Gritton vfs_opterror(opts, 1574b38ff370SJamie Gritton "IPv6 addresses clash"); 1575b38ff370SJamie Gritton goto done_deref_locked; 1576b38ff370SJamie Gritton } 15770304c731SJamie Gritton } 15780304c731SJamie Gritton } 15790304c731SJamie Gritton } 1580b38ff370SJamie Gritton #endif 15810304c731SJamie Gritton onamelen = namelen = 0; 15820304c731SJamie Gritton if (name != NULL) { 1583*6ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 1584*6ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 1585*6ab6058eSJamie Gritton * normal form (no leading zero/etc). 1586*6ab6058eSJamie Gritton */ 1587b38ff370SJamie Gritton if (name[0] == '\0') 1588b38ff370SJamie Gritton snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1589*6ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 1590*6ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1591b38ff370SJamie Gritton error = EINVAL; 1592babbbb9cSJamie Gritton vfs_opterror(opts, 1593babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 15940304c731SJamie Gritton goto done_deref_locked; 1595b38ff370SJamie Gritton } 1596b38ff370SJamie Gritton /* 15970304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 15980304c731SJamie Gritton * children. 1599b38ff370SJamie Gritton */ 16000304c731SJamie Gritton onamelen = strlen(pr->pr_name); 16010304c731SJamie Gritton namelen = strlen(name); 16020304c731SJamie Gritton if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 16030304c731SJamie Gritton error = ENAMETOOLONG; 16040304c731SJamie Gritton goto done_deref_locked; 16050304c731SJamie Gritton } 16060304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 16070304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 16080304c731SJamie Gritton sizeof(pr->pr_name)) { 16090304c731SJamie Gritton error = ENAMETOOLONG; 16100304c731SJamie Gritton goto done_deref_locked; 16110304c731SJamie Gritton } 16120304c731SJamie Gritton } 16130304c731SJamie Gritton } 16140304c731SJamie Gritton if (pr_allow & ~ppr->pr_allow) { 16150304c731SJamie Gritton error = EPERM; 16160304c731SJamie Gritton goto done_deref_locked; 1617b38ff370SJamie Gritton } 1618b38ff370SJamie Gritton 1619b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1620b38ff370SJamie Gritton #ifdef INET 16210304c731SJamie Gritton redo_ip4 = 0; 16220304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 16230304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1624b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 16250304c731SJamie Gritton pr->pr_ip4s = ip4s; 1626b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1627b38ff370SJamie Gritton ip4 = NULL; 16280304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1629bdfc8cc4SJamie Gritton #ifdef VIMAGE 1630bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1631bdfc8cc4SJamie Gritton descend = 0; 1632bdfc8cc4SJamie Gritton continue; 1633bdfc8cc4SJamie Gritton } 1634bdfc8cc4SJamie Gritton #endif 16350304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 16360304c731SJamie Gritton redo_ip4 = 1; 16370304c731SJamie Gritton descend = 0; 16380304c731SJamie Gritton } 16390304c731SJamie Gritton } 16400304c731SJamie Gritton } 1641b38ff370SJamie Gritton #endif 1642b38ff370SJamie Gritton #ifdef INET6 16430304c731SJamie Gritton redo_ip6 = 0; 16440304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 16450304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1646b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 16470304c731SJamie Gritton pr->pr_ip6s = ip6s; 1648b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1649b38ff370SJamie Gritton ip6 = NULL; 16500304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1651bdfc8cc4SJamie Gritton #ifdef VIMAGE 1652bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1653bdfc8cc4SJamie Gritton descend = 0; 1654bdfc8cc4SJamie Gritton continue; 1655bdfc8cc4SJamie Gritton } 1656bdfc8cc4SJamie Gritton #endif 16570304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 16580304c731SJamie Gritton redo_ip6 = 1; 16590304c731SJamie Gritton descend = 0; 16600304c731SJamie Gritton } 16610304c731SJamie Gritton } 16620304c731SJamie Gritton } 1663b38ff370SJamie Gritton #endif 16640304c731SJamie Gritton if (gotslevel) { 1665b38ff370SJamie Gritton pr->pr_securelevel = slevel; 16660304c731SJamie Gritton /* Set all child jails to be at least this level. */ 16670304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16680304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 16690304c731SJamie Gritton tpr->pr_securelevel = slevel; 16700304c731SJamie Gritton } 1671b97457e2SJamie Gritton if (gotchildmax) { 1672b97457e2SJamie Gritton pr->pr_childmax = childmax; 1673b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1674b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1675b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1676b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1677b97457e2SJamie Gritton ? childmax - level : 0; 1678b97457e2SJamie Gritton } 16790304c731SJamie Gritton if (gotenforce) { 16800304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 16810304c731SJamie Gritton /* Pass this restriction on to the children. */ 16820304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16830304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 16840304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 16850304c731SJamie Gritton } 16860cc207a6SMartin Matuska if (gotrsnum) { 16870cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 16880cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 16890cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16900cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 16910cc207a6SMartin Matuska } 16920304c731SJamie Gritton if (name != NULL) { 16930304c731SJamie Gritton if (ppr == &prison0) 1694b38ff370SJamie Gritton strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 16950304c731SJamie Gritton else 16960304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 16970304c731SJamie Gritton ppr->pr_name, name); 16980304c731SJamie Gritton /* Change this component of child names. */ 16990304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 17000304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 17010304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 17020304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 17030304c731SJamie Gritton } 17040304c731SJamie Gritton } 1705b38ff370SJamie Gritton if (path != NULL) { 17060304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1707f6e633a9SMartin Matuska if (fullpath_disabled && path[0] == '/' && 1708f6e633a9SMartin Matuska strcmp(mypr->pr_path, "/")) 17090304c731SJamie Gritton snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 17100304c731SJamie Gritton mypr->pr_path, path); 17110304c731SJamie Gritton else 1712b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1713b38ff370SJamie Gritton pr->pr_root = root; 1714b38ff370SJamie Gritton } 171576ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 171676ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 171776ca6f88SJamie Gritton /* 171876ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 171976ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 172076ca6f88SJamie Gritton * it is always set with allprison_lock at least 172176ca6f88SJamie Gritton * shared, and is held exclusively here. 172276ca6f88SJamie Gritton */ 1723c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1724c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1725c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1726c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1727c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1728c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 172976ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 173076ca6f88SJamie Gritton } 173176ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 173276ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1733b38ff370SJamie Gritton if (host != NULL) 1734c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 173576ca6f88SJamie Gritton if (domain != NULL) 1736c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1737c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 173876ca6f88SJamie Gritton if (uuid != NULL) 1739c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 174076ca6f88SJamie Gritton if (gothid) 174176ca6f88SJamie Gritton pr->pr_hostid = hid; 174276ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 174376ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 174476ca6f88SJamie Gritton descend = 0; 174576ca6f88SJamie Gritton else { 174676ca6f88SJamie Gritton if (host != NULL) 1747c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1748c1f19219SJamie Gritton pr->pr_hostname, 1749c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 175076ca6f88SJamie Gritton if (domain != NULL) 1751c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1752c1f19219SJamie Gritton pr->pr_domainname, 1753c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 175476ca6f88SJamie Gritton if (uuid != NULL) 1755c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1756c1f19219SJamie Gritton pr->pr_hostuuid, 1757c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 175876ca6f88SJamie Gritton if (gothid) 175976ca6f88SJamie Gritton tpr->pr_hostid = hid; 176076ca6f88SJamie Gritton } 176176ca6f88SJamie Gritton } 176276ca6f88SJamie Gritton } 17630304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 17640304c731SJamie Gritton /* Clear allow bits in all children. */ 17650304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 17660304c731SJamie Gritton tpr->pr_allow &= ~tallow; 17670304c731SJamie Gritton } 17680304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1769b38ff370SJamie Gritton /* 1770b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1771b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1772b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1773b38ff370SJamie Gritton * handlers have completed. 1774b38ff370SJamie Gritton */ 1775b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1776b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1777b38ff370SJamie Gritton pr->pr_ref++; 1778b38ff370SJamie Gritton pr->pr_uref++; 1779b38ff370SJamie Gritton } else { 1780b38ff370SJamie Gritton pr->pr_ref--; 1781b38ff370SJamie Gritton pr->pr_uref--; 1782b38ff370SJamie Gritton } 1783b38ff370SJamie Gritton } 1784b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1785b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1786b38ff370SJamie Gritton 1787a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 17884b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 1789a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 1790a7ad07bfSEdward Tomasz Napierala #endif 1791a7ad07bfSEdward Tomasz Napierala 17920304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 17930304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 17940304c731SJamie Gritton */ 17950304c731SJamie Gritton #ifdef INET 17960304c731SJamie Gritton while (redo_ip4) { 17970304c731SJamie Gritton ip4s = pr->pr_ip4s; 17980304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 17990304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 18000304c731SJamie Gritton redo_ip4 = 0; 18010304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1802bdfc8cc4SJamie Gritton #ifdef VIMAGE 1803bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1804bdfc8cc4SJamie Gritton descend = 0; 1805bdfc8cc4SJamie Gritton continue; 1806bdfc8cc4SJamie Gritton } 1807bdfc8cc4SJamie Gritton #endif 18080304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 18090304c731SJamie Gritton if (ip4 != NULL) 18100304c731SJamie Gritton ip4 = NULL; 18110304c731SJamie Gritton else 18120304c731SJamie Gritton redo_ip4 = 1; 18130304c731SJamie Gritton } 18140304c731SJamie Gritton } 18150304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 18160304c731SJamie Gritton } 18170304c731SJamie Gritton #endif 18180304c731SJamie Gritton #ifdef INET6 18190304c731SJamie Gritton while (redo_ip6) { 18200304c731SJamie Gritton ip6s = pr->pr_ip6s; 18210304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 18220304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 18230304c731SJamie Gritton redo_ip6 = 0; 18240304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1825bdfc8cc4SJamie Gritton #ifdef VIMAGE 1826bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1827bdfc8cc4SJamie Gritton descend = 0; 1828bdfc8cc4SJamie Gritton continue; 1829bdfc8cc4SJamie Gritton } 1830bdfc8cc4SJamie Gritton #endif 18310304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 18320304c731SJamie Gritton if (ip6 != NULL) 18330304c731SJamie Gritton ip6 = NULL; 18340304c731SJamie Gritton else 18350304c731SJamie Gritton redo_ip6 = 1; 18360304c731SJamie Gritton } 18370304c731SJamie Gritton } 18380304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 18390304c731SJamie Gritton } 18400304c731SJamie Gritton #endif 18410304c731SJamie Gritton 1842b38ff370SJamie Gritton /* Let the modules do their work. */ 1843b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1844b38ff370SJamie Gritton if (created) { 1845b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1846b38ff370SJamie Gritton if (error) { 1847b38ff370SJamie Gritton prison_deref(pr, PD_LIST_SLOCKED); 1848b38ff370SJamie Gritton goto done_errmsg; 1849b38ff370SJamie Gritton } 1850b38ff370SJamie Gritton } 1851b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1852b38ff370SJamie Gritton if (error) { 1853b38ff370SJamie Gritton prison_deref(pr, created 1854b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1855b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1856b38ff370SJamie Gritton goto done_errmsg; 1857b38ff370SJamie Gritton } 1858b38ff370SJamie Gritton 1859b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1860b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1861b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1862b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1863b38ff370SJamie Gritton if (error) { 1864b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1865b38ff370SJamie Gritton if (!created) 1866b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1867b38ff370SJamie Gritton goto done_errmsg; 1868b38ff370SJamie Gritton } 1869b38ff370SJamie Gritton } 1870b38ff370SJamie Gritton 18711fb24974SEdward Tomasz Napierala #ifdef RACCT 18724b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 1873f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18741fb24974SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 18751fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 1876f514b97bSEdward Tomasz Napierala if (!(flags & JAIL_ATTACH)) 18771fb24974SEdward Tomasz Napierala sx_slock(&allprison_lock); 18781fb24974SEdward Tomasz Napierala } 18791fb24974SEdward Tomasz Napierala #endif 18801fb24974SEdward Tomasz Napierala 18811fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 18821fb24974SEdward Tomasz Napierala 1883b38ff370SJamie Gritton /* 1884b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1885b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1886b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1887b38ff370SJamie Gritton * visible). 1888b38ff370SJamie Gritton */ 1889b38ff370SJamie Gritton if (!created) { 1890b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1891b38ff370SJamie Gritton ? PD_DEREF 1892b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1893b38ff370SJamie Gritton } else { 1894b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1895b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1896b38ff370SJamie Gritton pr->pr_ref++; 1897b38ff370SJamie Gritton pr->pr_uref++; 1898b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1899b38ff370SJamie Gritton } 1900b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1901b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1902b38ff370SJamie Gritton } 1903c34bbd2aSEdward Tomasz Napierala 1904b38ff370SJamie Gritton goto done_errmsg; 1905b38ff370SJamie Gritton 19060304c731SJamie Gritton done_deref_locked: 19070304c731SJamie Gritton prison_deref(pr, created 19080304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 19090304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 19100304c731SJamie Gritton goto done_releroot; 1911b38ff370SJamie Gritton done_unlock_list: 1912b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1913b38ff370SJamie Gritton done_releroot: 19145050aa86SKonstantin Belousov if (root != NULL) 1915b38ff370SJamie Gritton vrele(root); 1916b38ff370SJamie Gritton done_errmsg: 1917b38ff370SJamie Gritton if (error) { 1918b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1919b38ff370SJamie Gritton if (errmsg_len > 0) { 1920b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1921b38ff370SJamie Gritton if (errmsg_pos > 0) { 1922b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1923b38ff370SJamie Gritton bcopy(errmsg, 1924b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1925b38ff370SJamie Gritton errmsg_len); 1926b38ff370SJamie Gritton else 1927b38ff370SJamie Gritton copyout(errmsg, 1928b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1929b38ff370SJamie Gritton errmsg_len); 1930b38ff370SJamie Gritton } 1931b38ff370SJamie Gritton } 1932b38ff370SJamie Gritton } 1933b38ff370SJamie Gritton done_free: 1934b38ff370SJamie Gritton #ifdef INET 1935b38ff370SJamie Gritton free(ip4, M_PRISON); 1936b38ff370SJamie Gritton #endif 1937b38ff370SJamie Gritton #ifdef INET6 1938b38ff370SJamie Gritton free(ip6, M_PRISON); 1939b38ff370SJamie Gritton #endif 19406dfe0a3dSMartin Matuska if (g_path != NULL) 19416dfe0a3dSMartin Matuska free(g_path, M_TEMP); 1942b38ff370SJamie Gritton vfs_freeopts(opts); 1943b38ff370SJamie Gritton return (error); 1944b38ff370SJamie Gritton } 1945b38ff370SJamie Gritton 1946b38ff370SJamie Gritton 1947b38ff370SJamie Gritton /* 1948b38ff370SJamie Gritton * struct jail_get_args { 1949b38ff370SJamie Gritton * struct iovec *iovp; 1950b38ff370SJamie Gritton * unsigned int iovcnt; 1951b38ff370SJamie Gritton * int flags; 1952b38ff370SJamie Gritton * }; 1953b38ff370SJamie Gritton */ 1954b38ff370SJamie Gritton int 19558451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 1956b38ff370SJamie Gritton { 1957b38ff370SJamie Gritton struct uio *auio; 1958b38ff370SJamie Gritton int error; 1959b38ff370SJamie Gritton 1960b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1961b38ff370SJamie Gritton if (uap->iovcnt & 1) 1962b38ff370SJamie Gritton return (EINVAL); 1963b38ff370SJamie Gritton 1964b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1965b38ff370SJamie Gritton if (error) 1966b38ff370SJamie Gritton return (error); 1967b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1968b38ff370SJamie Gritton if (error == 0) 1969b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1970b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1971b38ff370SJamie Gritton free(auio, M_IOV); 1972b38ff370SJamie Gritton return (error); 1973b38ff370SJamie Gritton } 1974b38ff370SJamie Gritton 1975b38ff370SJamie Gritton int 1976b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1977b38ff370SJamie Gritton { 19780304c731SJamie Gritton struct prison *pr, *mypr; 1979b38ff370SJamie Gritton struct vfsopt *opt; 1980b38ff370SJamie Gritton struct vfsoptlist *opts; 1981b38ff370SJamie Gritton char *errmsg, *name; 19820304c731SJamie Gritton int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1983b38ff370SJamie Gritton 1984b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1985b38ff370SJamie Gritton return (EINVAL); 1986b38ff370SJamie Gritton 1987b38ff370SJamie Gritton /* Get the parameter list. */ 1988b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1989b38ff370SJamie Gritton if (error) 1990b38ff370SJamie Gritton return (error); 1991b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 19920304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 19931e2a13e6SJamie Gritton 1994b38ff370SJamie Gritton /* 1995b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1996b38ff370SJamie Gritton */ 1997b38ff370SJamie Gritton sx_slock(&allprison_lock); 1998b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1999b38ff370SJamie Gritton if (error == 0) { 2000b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 20010304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 2002b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2003b38ff370SJamie Gritton if (pr->pr_ref > 0 && 2004b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 2005b38ff370SJamie Gritton break; 2006b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2007b38ff370SJamie Gritton } 2008b38ff370SJamie Gritton } 2009b38ff370SJamie Gritton if (pr != NULL) 2010b38ff370SJamie Gritton goto found_prison; 2011b38ff370SJamie Gritton error = ENOENT; 2012b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 2013b38ff370SJamie Gritton goto done_unlock_list; 2014b38ff370SJamie Gritton } else if (error != ENOENT) 2015b38ff370SJamie Gritton goto done_unlock_list; 2016b38ff370SJamie Gritton 2017b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2018b38ff370SJamie Gritton if (error == 0) { 2019b38ff370SJamie Gritton if (jid != 0) { 20200304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2021b38ff370SJamie Gritton if (pr != NULL) { 2022b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2023b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2024b38ff370SJamie Gritton error = ENOENT; 2025b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2026b38ff370SJamie Gritton jid); 2027b38ff370SJamie Gritton goto done_unlock_list; 2028b38ff370SJamie Gritton } 2029b38ff370SJamie Gritton goto found_prison; 2030b38ff370SJamie Gritton } 2031b38ff370SJamie Gritton error = ENOENT; 2032b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 2033b38ff370SJamie Gritton goto done_unlock_list; 2034b38ff370SJamie Gritton } 2035b38ff370SJamie Gritton } else if (error != ENOENT) 2036b38ff370SJamie Gritton goto done_unlock_list; 2037b38ff370SJamie Gritton 2038b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2039b38ff370SJamie Gritton if (error == 0) { 2040b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2041b38ff370SJamie Gritton error = EINVAL; 2042b38ff370SJamie Gritton goto done_unlock_list; 2043b38ff370SJamie Gritton } 20440304c731SJamie Gritton pr = prison_find_name(mypr, name); 2045b38ff370SJamie Gritton if (pr != NULL) { 2046b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 2047b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2048b38ff370SJamie Gritton error = ENOENT; 2049b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2050b38ff370SJamie Gritton name); 2051b38ff370SJamie Gritton goto done_unlock_list; 2052b38ff370SJamie Gritton } 2053b38ff370SJamie Gritton goto found_prison; 2054b38ff370SJamie Gritton } 2055b38ff370SJamie Gritton error = ENOENT; 2056b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 2057b38ff370SJamie Gritton goto done_unlock_list; 2058b38ff370SJamie Gritton } else if (error != ENOENT) 2059b38ff370SJamie Gritton goto done_unlock_list; 2060b38ff370SJamie Gritton 2061b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2062b38ff370SJamie Gritton error = ENOENT; 2063b38ff370SJamie Gritton goto done_unlock_list; 2064b38ff370SJamie Gritton 2065b38ff370SJamie Gritton found_prison: 2066b38ff370SJamie Gritton /* Get the parameters of the prison. */ 2067b38ff370SJamie Gritton pr->pr_ref++; 2068b38ff370SJamie Gritton locked = PD_LOCKED; 2069b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2070b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2071b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2072b38ff370SJamie Gritton goto done_deref; 20730304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 20740304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2075b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2076b38ff370SJamie Gritton goto done_deref; 20770304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 20780304c731SJamie Gritton if (error != 0 && error != ENOENT) 20790304c731SJamie Gritton goto done_deref; 20800304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2081b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2082b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2083b38ff370SJamie Gritton goto done_deref; 20840304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2085b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2086b38ff370SJamie Gritton goto done_deref; 2087b38ff370SJamie Gritton #ifdef INET 2088b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 2089b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 2090b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2091b38ff370SJamie Gritton goto done_deref; 2092b38ff370SJamie Gritton #endif 2093b38ff370SJamie Gritton #ifdef INET6 2094b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 2095b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 2096b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2097b38ff370SJamie Gritton goto done_deref; 2098b38ff370SJamie Gritton #endif 2099b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2100b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2101b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2102b38ff370SJamie Gritton goto done_deref; 2103b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2104b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2105b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2106b97457e2SJamie Gritton goto done_deref; 2107b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2108b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2109b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 2110b97457e2SJamie Gritton goto done_deref; 2111c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2112b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2113b38ff370SJamie Gritton goto done_deref; 2114c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 211576ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 211676ca6f88SJamie Gritton goto done_deref; 2117c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 211876ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 211976ca6f88SJamie Gritton goto done_deref; 2120841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2121a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 212276ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 212376ca6f88SJamie Gritton 212476ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 212576ca6f88SJamie Gritton } else 212676ca6f88SJamie Gritton #endif 212776ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 212876ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 212976ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 213076ca6f88SJamie Gritton goto done_deref; 21310304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 21320304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 21330304c731SJamie Gritton if (error != 0 && error != ENOENT) 21340304c731SJamie Gritton goto done_deref; 21350cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 21360cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 21370cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 21380cc207a6SMartin Matuska goto done_deref; 21390304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 21400304c731SJamie Gritton fi++) { 21410304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 21420304c731SJamie Gritton continue; 21430304c731SJamie Gritton i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 21440304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2145b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2146b38ff370SJamie Gritton goto done_deref; 2147b38ff370SJamie Gritton i = !i; 21480304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2149b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2150b38ff370SJamie Gritton goto done_deref; 21510304c731SJamie Gritton } 21527cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 21537cbf7213SJamie Gritton fi++) { 21547cbf7213SJamie Gritton i = pr->pr_flags & 21557cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 21567cbf7213SJamie Gritton i = pr_flag_jailsys[fi].disable && 21577cbf7213SJamie Gritton (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 21587cbf7213SJamie Gritton : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 21597cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 21607cbf7213SJamie Gritton error = 21617cbf7213SJamie Gritton vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 21627cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 21637cbf7213SJamie Gritton goto done_deref; 21647cbf7213SJamie Gritton } 21650304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 21660304c731SJamie Gritton fi++) { 21670304c731SJamie Gritton if (pr_allow_names[fi] == NULL) 21680304c731SJamie Gritton continue; 21690304c731SJamie Gritton i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 21700304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 21710304c731SJamie Gritton if (error != 0 && error != ENOENT) 21720304c731SJamie Gritton goto done_deref; 21730304c731SJamie Gritton i = !i; 21740304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 21750304c731SJamie Gritton if (error != 0 && error != ENOENT) 21760304c731SJamie Gritton goto done_deref; 21770304c731SJamie Gritton } 2178b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2179b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2180b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2181b38ff370SJamie Gritton goto done_deref; 2182b38ff370SJamie Gritton i = !i; 2183b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2184b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2185b38ff370SJamie Gritton goto done_deref; 2186bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2187bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2188a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2189a1a4c1b0SIan Lepore goto done_deref; 2190a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2191a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 2192a1a4c1b0SIan Lepore goto done_deref; 2193b38ff370SJamie Gritton 2194b38ff370SJamie Gritton /* Get the module parameters. */ 2195b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2196b38ff370SJamie Gritton locked = 0; 2197b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2198b38ff370SJamie Gritton if (error) 2199b38ff370SJamie Gritton goto done_deref; 2200b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2201b38ff370SJamie Gritton 2202b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2203b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2204b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2205b38ff370SJamie Gritton error = EINVAL; 2206b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2207b38ff370SJamie Gritton goto done_errmsg; 2208b38ff370SJamie Gritton } 2209b38ff370SJamie Gritton } 2210b38ff370SJamie Gritton 2211b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2212b38ff370SJamie Gritton error = 0; 2213b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2214b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2215b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2216b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2217b38ff370SJamie Gritton if (opt->value != NULL) { 2218b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2219b38ff370SJamie Gritton bcopy(opt->value, 2220b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2221b38ff370SJamie Gritton opt->len); 2222b38ff370SJamie Gritton } else { 2223b38ff370SJamie Gritton error = copyout(opt->value, 2224b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2225b38ff370SJamie Gritton opt->len); 2226b38ff370SJamie Gritton if (error) 2227b38ff370SJamie Gritton break; 2228b38ff370SJamie Gritton } 2229b38ff370SJamie Gritton } 2230b38ff370SJamie Gritton } 2231b38ff370SJamie Gritton } 2232b38ff370SJamie Gritton goto done_errmsg; 2233b38ff370SJamie Gritton 2234b38ff370SJamie Gritton done_deref: 2235b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2236b38ff370SJamie Gritton goto done_errmsg; 2237b38ff370SJamie Gritton 2238b38ff370SJamie Gritton done_unlock_list: 2239b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2240b38ff370SJamie Gritton done_errmsg: 2241b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2242b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2243b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2244b38ff370SJamie Gritton if (errmsg_len > 0) { 2245b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2246b38ff370SJamie Gritton bcopy(errmsg, 2247b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2248b38ff370SJamie Gritton errmsg_len); 2249b38ff370SJamie Gritton else 2250b38ff370SJamie Gritton copyout(errmsg, 2251b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2252b38ff370SJamie Gritton errmsg_len); 2253b38ff370SJamie Gritton } 2254b38ff370SJamie Gritton } 2255b38ff370SJamie Gritton vfs_freeopts(opts); 2256b38ff370SJamie Gritton return (error); 2257b38ff370SJamie Gritton } 2258b38ff370SJamie Gritton 22590304c731SJamie Gritton 2260b38ff370SJamie Gritton /* 2261b38ff370SJamie Gritton * struct jail_remove_args { 2262b38ff370SJamie Gritton * int jid; 2263b38ff370SJamie Gritton * }; 2264b38ff370SJamie Gritton */ 2265b38ff370SJamie Gritton int 22668451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2267b38ff370SJamie Gritton { 22680304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 22690304c731SJamie Gritton int descend, error; 2270b38ff370SJamie Gritton 2271b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2272b38ff370SJamie Gritton if (error) 2273b38ff370SJamie Gritton return (error); 2274b38ff370SJamie Gritton 2275b38ff370SJamie Gritton sx_xlock(&allprison_lock); 22760304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2277b38ff370SJamie Gritton if (pr == NULL) { 2278b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2279b38ff370SJamie Gritton return (EINVAL); 2280b38ff370SJamie Gritton } 2281b38ff370SJamie Gritton 22820304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 22830304c731SJamie Gritton pr->pr_ref++; 22840304c731SJamie Gritton pr->pr_flags |= PR_REMOVE; 22850304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 22860304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 22870304c731SJamie Gritton lpr = NULL; 22880304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 22890304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 22900304c731SJamie Gritton if (cpr->pr_ref > 0) { 22910304c731SJamie Gritton tpr = cpr; 22920304c731SJamie Gritton cpr->pr_ref++; 22930304c731SJamie Gritton cpr->pr_flags |= PR_REMOVE; 22940304c731SJamie Gritton } else { 22950304c731SJamie Gritton /* Already removed - do not do it again. */ 22960304c731SJamie Gritton tpr = NULL; 22970304c731SJamie Gritton } 22980304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 22990304c731SJamie Gritton if (lpr != NULL) { 23000304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 23010304c731SJamie Gritton prison_remove_one(lpr); 23020304c731SJamie Gritton sx_xlock(&allprison_lock); 23030304c731SJamie Gritton } 23040304c731SJamie Gritton lpr = tpr; 23050304c731SJamie Gritton } 23060304c731SJamie Gritton if (lpr != NULL) { 23070304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 23080304c731SJamie Gritton prison_remove_one(lpr); 23090304c731SJamie Gritton sx_xlock(&allprison_lock); 23100304c731SJamie Gritton } 23110304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 23120304c731SJamie Gritton } 23130304c731SJamie Gritton prison_remove_one(pr); 23140304c731SJamie Gritton return (0); 23150304c731SJamie Gritton } 23160304c731SJamie Gritton 23170304c731SJamie Gritton static void 23180304c731SJamie Gritton prison_remove_one(struct prison *pr) 23190304c731SJamie Gritton { 23200304c731SJamie Gritton struct proc *p; 23210304c731SJamie Gritton int deuref; 23220304c731SJamie Gritton 2323b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2324b38ff370SJamie Gritton deuref = 0; 2325b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2326b38ff370SJamie Gritton pr->pr_ref--; 2327b38ff370SJamie Gritton deuref = PD_DEUREF; 2328b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2329b38ff370SJamie Gritton } 2330b38ff370SJamie Gritton 23310304c731SJamie Gritton /* 23320304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 23330304c731SJamie Gritton * the prison now. 23340304c731SJamie Gritton */ 23350304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 23360304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 23370304c731SJamie Gritton if (pr->pr_ref == 1) { 2338b38ff370SJamie Gritton prison_deref(pr, 2339b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 23400304c731SJamie Gritton return; 2341b38ff370SJamie Gritton } 2342b38ff370SJamie Gritton 2343b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2344b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2345b38ff370SJamie Gritton /* 2346b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2347b38ff370SJamie Gritton */ 2348b38ff370SJamie Gritton sx_slock(&allproc_lock); 2349b38ff370SJamie Gritton LIST_FOREACH(p, &allproc, p_list) { 2350b38ff370SJamie Gritton PROC_LOCK(p); 2351b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2352b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 23538451d0ddSKip Macy kern_psignal(p, SIGKILL); 2354b38ff370SJamie Gritton PROC_UNLOCK(p); 2355b38ff370SJamie Gritton } 2356b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 23570304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2358b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 235975c13541SPoul-Henning Kamp } 236075c13541SPoul-Henning Kamp 23618571af59SJamie Gritton 2362fd7a8150SMike Barcroft /* 23639ddb7954SMike Barcroft * struct jail_attach_args { 23649ddb7954SMike Barcroft * int jid; 23659ddb7954SMike Barcroft * }; 2366fd7a8150SMike Barcroft */ 2367fd7a8150SMike Barcroft int 23688451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2369fd7a8150SMike Barcroft { 2370b38ff370SJamie Gritton struct prison *pr; 2371b38ff370SJamie Gritton int error; 2372b38ff370SJamie Gritton 2373b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2374b38ff370SJamie Gritton if (error) 2375b38ff370SJamie Gritton return (error); 2376b38ff370SJamie Gritton 2377b38ff370SJamie Gritton sx_slock(&allprison_lock); 23780304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2379b38ff370SJamie Gritton if (pr == NULL) { 2380b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2381b38ff370SJamie Gritton return (EINVAL); 2382b38ff370SJamie Gritton } 2383b38ff370SJamie Gritton 2384b38ff370SJamie Gritton /* 2385b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2386b38ff370SJamie Gritton * considered to be "alive". 2387b38ff370SJamie Gritton */ 2388b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2389b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2390b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2391b38ff370SJamie Gritton return (EINVAL); 2392b38ff370SJamie Gritton } 2393b38ff370SJamie Gritton 2394b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2395b38ff370SJamie Gritton } 2396b38ff370SJamie Gritton 2397b38ff370SJamie Gritton static int 2398b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2399b38ff370SJamie Gritton { 24000304c731SJamie Gritton struct prison *ppr; 2401fd7a8150SMike Barcroft struct proc *p; 2402fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 24035050aa86SKonstantin Belousov int error; 2404fd7a8150SMike Barcroft 240557f22bd4SJacques Vidrine /* 240657f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 240757f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 240857f22bd4SJacques Vidrine * different jails at the same time. It is important for 240957f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 241057f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 241157f22bd4SJacques Vidrine * of another. 241257f22bd4SJacques Vidrine */ 2413fd7a8150SMike Barcroft pr->pr_ref++; 2414b38ff370SJamie Gritton pr->pr_uref++; 2415fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2416b38ff370SJamie Gritton 2417b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2418b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2419b38ff370SJamie Gritton if (error) { 2420b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2421b38ff370SJamie Gritton return (error); 2422b38ff370SJamie Gritton } 2423dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2424fd7a8150SMike Barcroft 2425413628a7SBjoern A. Zeeb /* 2426413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2427413628a7SBjoern A. Zeeb */ 24280304c731SJamie Gritton ppr = td->td_ucred->cr_prison; 2429b38ff370SJamie Gritton p = td->td_proc; 2430413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2431413628a7SBjoern A. Zeeb if (error) 2432b38ff370SJamie Gritton goto e_revert_osd; 2433413628a7SBjoern A. Zeeb 2434cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2435fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2436fd7a8150SMike Barcroft goto e_unlock; 2437fd7a8150SMike Barcroft #ifdef MAC 243830d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2439fd7a8150SMike Barcroft goto e_unlock; 2440fd7a8150SMike Barcroft #endif 244122db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2442f0725a8eSMateusz Guzik if ((error = pwd_chroot(td, pr->pr_root))) 24435050aa86SKonstantin Belousov goto e_revert_osd; 2444fd7a8150SMike Barcroft 2445fd7a8150SMike Barcroft newcred = crget(); 2446fd7a8150SMike Barcroft PROC_LOCK(p); 2447fd7a8150SMike Barcroft oldcred = p->p_ucred; 2448fd7a8150SMike Barcroft setsugid(p); 2449fd7a8150SMike Barcroft crcopy(newcred, oldcred); 245069c4ee54SJohn Baldwin newcred->cr_prison = pr; 2451daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 2452fd7a8150SMike Barcroft PROC_UNLOCK(p); 2453097055e2SEdward Tomasz Napierala #ifdef RACCT 2454097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2455097055e2SEdward Tomasz Napierala #endif 2456fd7a8150SMike Barcroft crfree(oldcred); 24570304c731SJamie Gritton prison_deref(ppr, PD_DEREF | PD_DEUREF); 2458fd7a8150SMike Barcroft return (0); 2459fd7a8150SMike Barcroft e_unlock: 246022db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2461b38ff370SJamie Gritton e_revert_osd: 2462b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 24630304c731SJamie Gritton (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2464b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2465fd7a8150SMike Barcroft return (error); 2466fd7a8150SMike Barcroft } 2467fd7a8150SMike Barcroft 24680304c731SJamie Gritton 2469fd7a8150SMike Barcroft /* 2470fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2471fd7a8150SMike Barcroft */ 247254b369c1SPawel Jakub Dawidek struct prison * 2473fd7a8150SMike Barcroft prison_find(int prid) 2474fd7a8150SMike Barcroft { 2475fd7a8150SMike Barcroft struct prison *pr; 2476fd7a8150SMike Barcroft 2477dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2478b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2479fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2480fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2481b38ff370SJamie Gritton if (pr->pr_ref > 0) 2482fd7a8150SMike Barcroft return (pr); 2483b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2484fd7a8150SMike Barcroft } 2485fd7a8150SMike Barcroft } 2486fd7a8150SMike Barcroft return (NULL); 2487fd7a8150SMike Barcroft } 2488fd7a8150SMike Barcroft 2489b38ff370SJamie Gritton /* 24900304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2491b38ff370SJamie Gritton */ 2492b38ff370SJamie Gritton struct prison * 24930304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2494b38ff370SJamie Gritton { 24950304c731SJamie Gritton struct prison *pr; 24960304c731SJamie Gritton int descend; 2497b38ff370SJamie Gritton 2498b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 24990304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 25000304c731SJamie Gritton if (pr->pr_id == prid) { 25010304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 25020304c731SJamie Gritton if (pr->pr_ref > 0) 25030304c731SJamie Gritton return (pr); 25040304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 25050304c731SJamie Gritton } 25060304c731SJamie Gritton } 25070304c731SJamie Gritton return (NULL); 25080304c731SJamie Gritton } 25090304c731SJamie Gritton 25100304c731SJamie Gritton /* 25110304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 25120304c731SJamie Gritton */ 25130304c731SJamie Gritton struct prison * 25140304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 25150304c731SJamie Gritton { 25160304c731SJamie Gritton struct prison *pr, *deadpr; 25170304c731SJamie Gritton size_t mylen; 25180304c731SJamie Gritton int descend; 25190304c731SJamie Gritton 25200304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 25210304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2522b38ff370SJamie Gritton again: 2523b38ff370SJamie Gritton deadpr = NULL; 25240304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 25250304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2526b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2527b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2528b38ff370SJamie Gritton if (pr->pr_uref > 0) 2529b38ff370SJamie Gritton return (pr); 2530b38ff370SJamie Gritton deadpr = pr; 2531b38ff370SJamie Gritton } 2532b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2533b38ff370SJamie Gritton } 2534b38ff370SJamie Gritton } 25350304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2536b38ff370SJamie Gritton if (deadpr != NULL) { 2537b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2538b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2539b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2540b38ff370SJamie Gritton goto again; 2541b38ff370SJamie Gritton } 2542b38ff370SJamie Gritton } 2543b38ff370SJamie Gritton return (deadpr); 2544b38ff370SJamie Gritton } 2545b38ff370SJamie Gritton 2546b38ff370SJamie Gritton /* 25470304c731SJamie Gritton * See if a prison has the specific flag set. 25480304c731SJamie Gritton */ 25490304c731SJamie Gritton int 25500304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 25510304c731SJamie Gritton { 25520304c731SJamie Gritton 25530304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25540304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 25550304c731SJamie Gritton } 25560304c731SJamie Gritton 25570304c731SJamie Gritton int 25580304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 25590304c731SJamie Gritton { 25600304c731SJamie Gritton 25610304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 25620304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 25630304c731SJamie Gritton } 25640304c731SJamie Gritton 25650304c731SJamie Gritton /* 2566b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2567b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2568b38ff370SJamie Gritton */ 256991421ba2SRobert Watson void 25701ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 257191421ba2SRobert Watson { 257291421ba2SRobert Watson 25731ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 257491421ba2SRobert Watson pr->pr_ref--; 257591421ba2SRobert Watson if (pr->pr_ref == 0) { 257601137630SRobert Watson mtx_unlock(&pr->pr_mtx); 2577c2cda609SPawel Jakub Dawidek TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2578c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2579c2cda609SPawel Jakub Dawidek return; 2580c2cda609SPawel Jakub Dawidek } 2581c2cda609SPawel Jakub Dawidek mtx_unlock(&pr->pr_mtx); 2582c2cda609SPawel Jakub Dawidek } 2583c2cda609SPawel Jakub Dawidek 25841ba4a712SPawel Jakub Dawidek void 25851ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 25861ba4a712SPawel Jakub Dawidek { 25871ba4a712SPawel Jakub Dawidek 25881ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 25891ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 25901ba4a712SPawel Jakub Dawidek } 25911ba4a712SPawel Jakub Dawidek 2592c2cda609SPawel Jakub Dawidek static void 2593c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2594c2cda609SPawel Jakub Dawidek { 2595b38ff370SJamie Gritton 2596b38ff370SJamie Gritton prison_deref((struct prison *)context, 0); 2597b38ff370SJamie Gritton } 2598b38ff370SJamie Gritton 2599b38ff370SJamie Gritton /* 2600b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2601b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2602b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2603b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2604b38ff370SJamie Gritton */ 2605b38ff370SJamie Gritton static void 2606b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2607b38ff370SJamie Gritton { 26080304c731SJamie Gritton struct prison *ppr, *tpr; 2609c2cda609SPawel Jakub Dawidek 2610b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2611b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 26120304c731SJamie Gritton for (;;) { 2613e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 2614e6d5cb63SJamie Gritton pr->pr_uref--; 2615e6d5cb63SJamie Gritton KASSERT(prison0.pr_uref != 0, ("prison0 pr_uref=0")); 2616e6d5cb63SJamie Gritton } 2617b38ff370SJamie Gritton if (flags & PD_DEREF) 2618b38ff370SJamie Gritton pr->pr_ref--; 2619b38ff370SJamie Gritton /* If the prison still has references, nothing else to do. */ 2620b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2621b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2622b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2623b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2624b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2625b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2626b38ff370SJamie Gritton return; 2627b38ff370SJamie Gritton } 2628c2cda609SPawel Jakub Dawidek 2629b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2630b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2631b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2632b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2633c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2634b38ff370SJamie Gritton } 2635b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2636b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2637b38ff370SJamie Gritton 2638b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 26390304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 26400304c731SJamie Gritton ppr = pr->pr_parent; 26410304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2642b97457e2SJamie Gritton tpr->pr_childcount--; 2643c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 26441ba4a712SPawel Jakub Dawidek 2645679e1390SJamie Gritton #ifdef VIMAGE 26460cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2647679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2648679e1390SJamie Gritton #endif 26495050aa86SKonstantin Belousov if (pr->pr_root != NULL) 2650b3059e09SRobert Watson vrele(pr->pr_root); 2651b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2652413628a7SBjoern A. Zeeb #ifdef INET 2653413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2654413628a7SBjoern A. Zeeb #endif 2655b38ff370SJamie Gritton #ifdef INET6 2656b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2657b38ff370SJamie Gritton #endif 2658b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2659b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2660b38ff370SJamie Gritton osd_jail_exit(pr); 2661a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 26624b5c9cf6SEdward Tomasz Napierala if (racct_enable) 2663a7ad07bfSEdward Tomasz Napierala prison_racct_detach(pr); 2664ec125fbbSEdward Tomasz Napierala #endif 26651ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 26660304c731SJamie Gritton 26670304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 26680304c731SJamie Gritton pr = ppr; 26690304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2670e6d5cb63SJamie Gritton flags = PD_DEREF | PD_DEUREF; 26710304c731SJamie Gritton } 2672b3059e09SRobert Watson } 2673b3059e09SRobert Watson 267491421ba2SRobert Watson void 26751ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 26761ba4a712SPawel Jakub Dawidek { 26771ba4a712SPawel Jakub Dawidek 26781ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 26791ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 2680af7bd9a4SJamie Gritton ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 26811ba4a712SPawel Jakub Dawidek pr->pr_ref++; 26821ba4a712SPawel Jakub Dawidek } 26831ba4a712SPawel Jakub Dawidek 26841ba4a712SPawel Jakub Dawidek void 268591421ba2SRobert Watson prison_hold(struct prison *pr) 268691421ba2SRobert Watson { 268791421ba2SRobert Watson 268801137630SRobert Watson mtx_lock(&pr->pr_mtx); 26891ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 269001137630SRobert Watson mtx_unlock(&pr->pr_mtx); 269101137630SRobert Watson } 269201137630SRobert Watson 2693413628a7SBjoern A. Zeeb void 2694413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 269501137630SRobert Watson { 269601137630SRobert Watson 2697413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2698b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2699b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2700b38ff370SJamie Gritton pr->pr_uref++; 2701413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 270275c13541SPoul-Henning Kamp } 270375c13541SPoul-Henning Kamp 270475c13541SPoul-Henning Kamp void 2705413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 270675c13541SPoul-Henning Kamp { 2707413628a7SBjoern A. Zeeb 2708413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2709b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2710b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2711b38ff370SJamie Gritton prison_deref(pr, PD_DEUREF | PD_LOCKED); 2712413628a7SBjoern A. Zeeb } 2713413628a7SBjoern A. Zeeb 2714413628a7SBjoern A. Zeeb 2715413628a7SBjoern A. Zeeb #ifdef INET 2716413628a7SBjoern A. Zeeb /* 27170304c731SJamie Gritton * Restrict a prison's IP address list with its parent's, possibly replacing 27180304c731SJamie Gritton * it. Return true if the replacement buffer was used (or would have been). 27190304c731SJamie Gritton */ 27200304c731SJamie Gritton static int 27210304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 27220304c731SJamie Gritton { 27230304c731SJamie Gritton int ii, ij, used; 27240304c731SJamie Gritton struct prison *ppr; 27250304c731SJamie Gritton 27260304c731SJamie Gritton ppr = pr->pr_parent; 27270304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4_USER)) { 27280304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 27290304c731SJamie Gritton if (pr->pr_ip4s < ppr->pr_ip4s) { 27300304c731SJamie Gritton /* 27310304c731SJamie Gritton * There's no room for the parent's list. Use the 27320304c731SJamie Gritton * new list buffer, which is assumed to be big enough 27330304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 27340304c731SJamie Gritton * allocate one. 27350304c731SJamie Gritton */ 27360304c731SJamie Gritton used = 1; 27370304c731SJamie Gritton if (newip4 == NULL) { 27380304c731SJamie Gritton newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 27390304c731SJamie Gritton M_PRISON, M_NOWAIT); 27400304c731SJamie Gritton if (newip4 != NULL) 27410304c731SJamie Gritton used = 0; 27420304c731SJamie Gritton } 27430304c731SJamie Gritton if (newip4 != NULL) { 27440304c731SJamie Gritton bcopy(ppr->pr_ip4, newip4, 27450304c731SJamie Gritton ppr->pr_ip4s * sizeof(*newip4)); 27460304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 27470304c731SJamie Gritton pr->pr_ip4 = newip4; 27480304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 27490304c731SJamie Gritton } 27500304c731SJamie Gritton return (used); 27510304c731SJamie Gritton } 27520304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 27530304c731SJamie Gritton if (pr->pr_ip4s > 0) 27540304c731SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 27550304c731SJamie Gritton pr->pr_ip4s * sizeof(*newip4)); 27560304c731SJamie Gritton else if (pr->pr_ip4 != NULL) { 27570304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 27580304c731SJamie Gritton pr->pr_ip4 = NULL; 27590304c731SJamie Gritton } 27602b0d6f81SJamie Gritton } else if (pr->pr_ip4s > 0) { 27610304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 27620304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 27630304c731SJamie Gritton if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 27640304c731SJamie Gritton break; 27650304c731SJamie Gritton if (ij < ppr->pr_ip4s) 27660304c731SJamie Gritton ii = 1; 27670304c731SJamie Gritton else { 27680304c731SJamie Gritton bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 27690304c731SJamie Gritton --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 27700304c731SJamie Gritton ii = 0; 27710304c731SJamie Gritton } 27720304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip4s; ) { 27730304c731SJamie Gritton if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 27740304c731SJamie Gritton ii++; 27750304c731SJamie Gritton continue; 27760304c731SJamie Gritton } 27770304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 27780304c731SJamie Gritton qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 27790304c731SJamie Gritton case -1: 27800304c731SJamie Gritton bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 27810304c731SJamie Gritton (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 27820304c731SJamie Gritton break; 27830304c731SJamie Gritton case 0: 27840304c731SJamie Gritton ii++; 27850304c731SJamie Gritton ij++; 27860304c731SJamie Gritton break; 27870304c731SJamie Gritton case 1: 27880304c731SJamie Gritton ij++; 27890304c731SJamie Gritton break; 27900304c731SJamie Gritton } 27910304c731SJamie Gritton } 27920304c731SJamie Gritton if (pr->pr_ip4s == 0) { 27930304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 27940304c731SJamie Gritton pr->pr_ip4 = NULL; 27950304c731SJamie Gritton } 27960304c731SJamie Gritton } 27970304c731SJamie Gritton return (0); 27980304c731SJamie Gritton } 27990304c731SJamie Gritton 28000304c731SJamie Gritton /* 2801413628a7SBjoern A. Zeeb * Pass back primary IPv4 address of this jail. 2802413628a7SBjoern A. Zeeb * 28030304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 28040304c731SJamie Gritton * to make sure to initialize it correctly (e.g. INADDR_ANY). 2805413628a7SBjoern A. Zeeb * 2806b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2807b89e82ddSJamie Gritton * Address returned in NBO. 2808413628a7SBjoern A. Zeeb */ 2809413628a7SBjoern A. Zeeb int 28101cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia) 2811413628a7SBjoern A. Zeeb { 2812b38ff370SJamie Gritton struct prison *pr; 2813413628a7SBjoern A. Zeeb 2814413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2815413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 281675c13541SPoul-Henning Kamp 2817b38ff370SJamie Gritton pr = cred->cr_prison; 28180304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 28190304c731SJamie Gritton return (0); 2820b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 28210304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 28220304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28230304c731SJamie Gritton return (0); 28240304c731SJamie Gritton } 2825b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2826b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2827b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2828b38ff370SJamie Gritton } 2829413628a7SBjoern A. Zeeb 2830b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2831b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2832413628a7SBjoern A. Zeeb return (0); 283375c13541SPoul-Henning Kamp } 2834413628a7SBjoern A. Zeeb 2835413628a7SBjoern A. Zeeb /* 2836592bcae8SBjoern A. Zeeb * Return 1 if we should do proper source address selection or are not jailed. 2837592bcae8SBjoern A. Zeeb * We will return 0 if we should bypass source address selection in favour 2838592bcae8SBjoern A. Zeeb * of the primary jail IPv4 address. Only in this case *ia will be updated and 2839592bcae8SBjoern A. Zeeb * returned in NBO. 2840592bcae8SBjoern A. Zeeb * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2841592bcae8SBjoern A. Zeeb */ 2842592bcae8SBjoern A. Zeeb int 2843592bcae8SBjoern A. Zeeb prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2844592bcae8SBjoern A. Zeeb { 2845592bcae8SBjoern A. Zeeb struct prison *pr; 2846592bcae8SBjoern A. Zeeb struct in_addr lia; 2847592bcae8SBjoern A. Zeeb int error; 2848592bcae8SBjoern A. Zeeb 2849592bcae8SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2850592bcae8SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2851592bcae8SBjoern A. Zeeb 2852592bcae8SBjoern A. Zeeb if (!jailed(cred)) 2853592bcae8SBjoern A. Zeeb return (1); 2854592bcae8SBjoern A. Zeeb 2855592bcae8SBjoern A. Zeeb pr = cred->cr_prison; 2856592bcae8SBjoern A. Zeeb if (pr->pr_flags & PR_IP4_SADDRSEL) 2857592bcae8SBjoern A. Zeeb return (1); 2858592bcae8SBjoern A. Zeeb 2859592bcae8SBjoern A. Zeeb lia.s_addr = INADDR_ANY; 2860592bcae8SBjoern A. Zeeb error = prison_get_ip4(cred, &lia); 2861592bcae8SBjoern A. Zeeb if (error) 2862592bcae8SBjoern A. Zeeb return (error); 2863592bcae8SBjoern A. Zeeb if (lia.s_addr == INADDR_ANY) 2864592bcae8SBjoern A. Zeeb return (1); 2865592bcae8SBjoern A. Zeeb 2866592bcae8SBjoern A. Zeeb ia->s_addr = lia.s_addr; 2867592bcae8SBjoern A. Zeeb return (0); 2868592bcae8SBjoern A. Zeeb } 2869592bcae8SBjoern A. Zeeb 2870592bcae8SBjoern A. Zeeb /* 28710304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv4 address restrictions. 28720304c731SJamie Gritton */ 28730304c731SJamie Gritton int 28740304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2) 28750304c731SJamie Gritton { 28760304c731SJamie Gritton 28770304c731SJamie Gritton if (pr1 == pr2) 28780304c731SJamie Gritton return (1); 28790304c731SJamie Gritton 28800304c731SJamie Gritton /* 28812b0d6f81SJamie Gritton * No need to lock since the PR_IP4_USER flag can't be altered for 28822b0d6f81SJamie Gritton * existing prisons. 28830304c731SJamie Gritton */ 2884bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 2885bdfc8cc4SJamie Gritton #ifdef VIMAGE 2886bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 2887bdfc8cc4SJamie Gritton #endif 2888bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP4_USER)) 28890304c731SJamie Gritton pr1 = pr1->pr_parent; 2890bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 2891bdfc8cc4SJamie Gritton #ifdef VIMAGE 2892bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 2893bdfc8cc4SJamie Gritton #endif 2894bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP4_USER)) 28950304c731SJamie Gritton pr2 = pr2->pr_parent; 28960304c731SJamie Gritton return (pr1 == pr2); 28970304c731SJamie Gritton } 28980304c731SJamie Gritton 28990304c731SJamie Gritton /* 2900413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this 2901413628a7SBjoern A. Zeeb * jail. 2902413628a7SBjoern A. Zeeb * 29030304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 29040304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 29050304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2906413628a7SBjoern A. Zeeb */ 2907413628a7SBjoern A. Zeeb int 2908413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia) 2909413628a7SBjoern A. Zeeb { 2910b38ff370SJamie Gritton struct prison *pr; 2911413628a7SBjoern A. Zeeb struct in_addr ia0; 2912b38ff370SJamie Gritton int error; 2913413628a7SBjoern A. Zeeb 2914413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2915413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2916413628a7SBjoern A. Zeeb 2917b38ff370SJamie Gritton pr = cred->cr_prison; 29180304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 29190304c731SJamie Gritton return (0); 2920b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 29210304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 29220304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 29230304c731SJamie Gritton return (0); 29240304c731SJamie Gritton } 2925b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2926b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2927b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2928b38ff370SJamie Gritton } 2929413628a7SBjoern A. Zeeb 2930413628a7SBjoern A. Zeeb ia0.s_addr = ntohl(ia->s_addr); 2931413628a7SBjoern A. Zeeb if (ia0.s_addr == INADDR_LOOPBACK) { 2932b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2933b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2934413628a7SBjoern A. Zeeb return (0); 2935413628a7SBjoern A. Zeeb } 2936413628a7SBjoern A. Zeeb 2937b89e82ddSJamie Gritton if (ia0.s_addr == INADDR_ANY) { 2938413628a7SBjoern A. Zeeb /* 2939413628a7SBjoern A. Zeeb * In case there is only 1 IPv4 address, bind directly. 2940413628a7SBjoern A. Zeeb */ 2941b38ff370SJamie Gritton if (pr->pr_ip4s == 1) 2942b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2943b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2944413628a7SBjoern A. Zeeb return (0); 2945413628a7SBjoern A. Zeeb } 2946413628a7SBjoern A. Zeeb 2947b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2948b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2949b38ff370SJamie Gritton return (error); 2950413628a7SBjoern A. Zeeb } 2951413628a7SBjoern A. Zeeb 2952413628a7SBjoern A. Zeeb /* 2953413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 2954413628a7SBjoern A. Zeeb * 2955b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2956b89e82ddSJamie Gritton * Address passed in in NBO and returned in NBO. 2957413628a7SBjoern A. Zeeb */ 2958413628a7SBjoern A. Zeeb int 2959413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2960413628a7SBjoern A. Zeeb { 2961b38ff370SJamie Gritton struct prison *pr; 2962413628a7SBjoern A. Zeeb 2963413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2964413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2965413628a7SBjoern A. Zeeb 2966b38ff370SJamie Gritton pr = cred->cr_prison; 29670304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 29680304c731SJamie Gritton return (0); 2969b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 29700304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 29710304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 29720304c731SJamie Gritton return (0); 29730304c731SJamie Gritton } 2974b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2975b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2976b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2977b38ff370SJamie Gritton } 2978b89e82ddSJamie Gritton 2979413628a7SBjoern A. Zeeb if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2980b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2981b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2982413628a7SBjoern A. Zeeb return (0); 2983413628a7SBjoern A. Zeeb } 2984413628a7SBjoern A. Zeeb 2985413628a7SBjoern A. Zeeb /* 2986413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 2987413628a7SBjoern A. Zeeb */ 2988b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2989413628a7SBjoern A. Zeeb return (0); 2990413628a7SBjoern A. Zeeb } 2991413628a7SBjoern A. Zeeb 2992413628a7SBjoern A. Zeeb /* 2993b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 2994413628a7SBjoern A. Zeeb * 29950304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 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 IPv4. Address passed in in NBO. 2998413628a7SBjoern A. Zeeb */ 2999413628a7SBjoern A. Zeeb static int 30000d168b8dSGleb Smirnoff _prison_check_ip4(const struct prison *pr, const struct in_addr *ia) 3001413628a7SBjoern A. Zeeb { 3002413628a7SBjoern A. Zeeb int i, a, z, d; 3003413628a7SBjoern A. Zeeb 3004413628a7SBjoern A. Zeeb /* 3005413628a7SBjoern A. Zeeb * Check the primary IP. 3006413628a7SBjoern A. Zeeb */ 3007413628a7SBjoern A. Zeeb if (pr->pr_ip4[0].s_addr == ia->s_addr) 3008b89e82ddSJamie Gritton return (0); 3009413628a7SBjoern A. Zeeb 3010413628a7SBjoern A. Zeeb /* 3011413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 3012413628a7SBjoern A. Zeeb */ 3013413628a7SBjoern A. Zeeb a = 0; 3014413628a7SBjoern A. Zeeb z = pr->pr_ip4s - 2; 3015413628a7SBjoern A. Zeeb while (a <= z) { 3016413628a7SBjoern A. Zeeb i = (a + z) / 2; 3017413628a7SBjoern A. Zeeb d = qcmp_v4(&pr->pr_ip4[i+1], ia); 3018413628a7SBjoern A. Zeeb if (d > 0) 3019413628a7SBjoern A. Zeeb z = i - 1; 3020413628a7SBjoern A. Zeeb else if (d < 0) 3021413628a7SBjoern A. Zeeb a = i + 1; 3022413628a7SBjoern A. Zeeb else 3023413628a7SBjoern A. Zeeb return (0); 302475c13541SPoul-Henning Kamp } 302575c13541SPoul-Henning Kamp 3026b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 3027b89e82ddSJamie Gritton } 3028b89e82ddSJamie Gritton 302975c13541SPoul-Henning Kamp int 30300d168b8dSGleb Smirnoff prison_check_ip4(const struct ucred *cred, const struct in_addr *ia) 3031413628a7SBjoern A. Zeeb { 3032b38ff370SJamie Gritton struct prison *pr; 3033b38ff370SJamie Gritton int error; 3034413628a7SBjoern A. Zeeb 3035413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3036413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 3037413628a7SBjoern A. Zeeb 3038b38ff370SJamie Gritton pr = cred->cr_prison; 30390304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 30400304c731SJamie Gritton return (0); 3041b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 30420304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 30430304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 30440304c731SJamie Gritton return (0); 30450304c731SJamie Gritton } 3046b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 3047b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3048b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3049b38ff370SJamie Gritton } 3050413628a7SBjoern A. Zeeb 3051b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 3052b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3053b38ff370SJamie Gritton return (error); 3054413628a7SBjoern A. Zeeb } 3055413628a7SBjoern A. Zeeb #endif 3056413628a7SBjoern A. Zeeb 3057413628a7SBjoern A. Zeeb #ifdef INET6 30580304c731SJamie Gritton static int 30590304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 30600304c731SJamie Gritton { 30610304c731SJamie Gritton int ii, ij, used; 30620304c731SJamie Gritton struct prison *ppr; 30630304c731SJamie Gritton 30640304c731SJamie Gritton ppr = pr->pr_parent; 30650304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6_USER)) { 30660304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 30670304c731SJamie Gritton if (pr->pr_ip6s < ppr->pr_ip6s) { 30680304c731SJamie Gritton /* 30690304c731SJamie Gritton * There's no room for the parent's list. Use the 30700304c731SJamie Gritton * new list buffer, which is assumed to be big enough 30710304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 30720304c731SJamie Gritton * allocate one. 30730304c731SJamie Gritton */ 30740304c731SJamie Gritton used = 1; 30750304c731SJamie Gritton if (newip6 == NULL) { 30760304c731SJamie Gritton newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 30770304c731SJamie Gritton M_PRISON, M_NOWAIT); 30780304c731SJamie Gritton if (newip6 != NULL) 30790304c731SJamie Gritton used = 0; 30800304c731SJamie Gritton } 30810304c731SJamie Gritton if (newip6 != NULL) { 30820304c731SJamie Gritton bcopy(ppr->pr_ip6, newip6, 30830304c731SJamie Gritton ppr->pr_ip6s * sizeof(*newip6)); 30840304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 30850304c731SJamie Gritton pr->pr_ip6 = newip6; 30860304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 30870304c731SJamie Gritton } 30880304c731SJamie Gritton return (used); 30890304c731SJamie Gritton } 30900304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 30910304c731SJamie Gritton if (pr->pr_ip6s > 0) 30920304c731SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 30930304c731SJamie Gritton pr->pr_ip6s * sizeof(*newip6)); 30940304c731SJamie Gritton else if (pr->pr_ip6 != NULL) { 30950304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 30960304c731SJamie Gritton pr->pr_ip6 = NULL; 30970304c731SJamie Gritton } 30982b0d6f81SJamie Gritton } else if (pr->pr_ip6s > 0) { 30990304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 31000304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 31010304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 31020304c731SJamie Gritton &ppr->pr_ip6[ij])) 31030304c731SJamie Gritton break; 31040304c731SJamie Gritton if (ij < ppr->pr_ip6s) 31050304c731SJamie Gritton ii = 1; 31060304c731SJamie Gritton else { 31070304c731SJamie Gritton bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 31080304c731SJamie Gritton --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 31090304c731SJamie Gritton ii = 0; 31100304c731SJamie Gritton } 31110304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip6s; ) { 31120304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 31130304c731SJamie Gritton &ppr->pr_ip6[0])) { 31140304c731SJamie Gritton ii++; 31150304c731SJamie Gritton continue; 31160304c731SJamie Gritton } 3117da0770bdSAndrey V. Elsukov switch (ij >= ppr->pr_ip6s ? -1 : 31180304c731SJamie Gritton qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 31190304c731SJamie Gritton case -1: 31200304c731SJamie Gritton bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 31210304c731SJamie Gritton (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 31220304c731SJamie Gritton break; 31230304c731SJamie Gritton case 0: 31240304c731SJamie Gritton ii++; 31250304c731SJamie Gritton ij++; 31260304c731SJamie Gritton break; 31270304c731SJamie Gritton case 1: 31280304c731SJamie Gritton ij++; 31290304c731SJamie Gritton break; 31300304c731SJamie Gritton } 31310304c731SJamie Gritton } 31320304c731SJamie Gritton if (pr->pr_ip6s == 0) { 31330304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 31340304c731SJamie Gritton pr->pr_ip6 = NULL; 31350304c731SJamie Gritton } 31360304c731SJamie Gritton } 31370304c731SJamie Gritton return 0; 31380304c731SJamie Gritton } 31390304c731SJamie Gritton 3140413628a7SBjoern A. Zeeb /* 3141413628a7SBjoern A. Zeeb * Pass back primary IPv6 address for this jail. 3142413628a7SBjoern A. Zeeb * 31430304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 31440304c731SJamie Gritton * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3145413628a7SBjoern A. Zeeb * 3146b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3147413628a7SBjoern A. Zeeb */ 3148413628a7SBjoern A. Zeeb int 31491cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3150413628a7SBjoern A. Zeeb { 3151b38ff370SJamie Gritton struct prison *pr; 3152413628a7SBjoern A. Zeeb 3153413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3154413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3155413628a7SBjoern A. Zeeb 3156b38ff370SJamie Gritton pr = cred->cr_prison; 31570304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 31580304c731SJamie Gritton return (0); 3159b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 31600304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 31610304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31620304c731SJamie Gritton return (0); 31630304c731SJamie Gritton } 3164b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3165b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3166b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3167b38ff370SJamie Gritton } 3168b89e82ddSJamie Gritton 3169b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3170b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3171413628a7SBjoern A. Zeeb return (0); 3172413628a7SBjoern A. Zeeb } 3173413628a7SBjoern A. Zeeb 3174413628a7SBjoern A. Zeeb /* 3175592bcae8SBjoern A. Zeeb * Return 1 if we should do proper source address selection or are not jailed. 3176592bcae8SBjoern A. Zeeb * We will return 0 if we should bypass source address selection in favour 3177592bcae8SBjoern A. Zeeb * of the primary jail IPv6 address. Only in this case *ia will be updated and 3178592bcae8SBjoern A. Zeeb * returned in NBO. 3179592bcae8SBjoern A. Zeeb * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3180592bcae8SBjoern A. Zeeb */ 3181592bcae8SBjoern A. Zeeb int 3182592bcae8SBjoern A. Zeeb prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3183592bcae8SBjoern A. Zeeb { 3184592bcae8SBjoern A. Zeeb struct prison *pr; 3185592bcae8SBjoern A. Zeeb struct in6_addr lia6; 3186592bcae8SBjoern A. Zeeb int error; 3187592bcae8SBjoern A. Zeeb 3188592bcae8SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3189592bcae8SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3190592bcae8SBjoern A. Zeeb 3191592bcae8SBjoern A. Zeeb if (!jailed(cred)) 3192592bcae8SBjoern A. Zeeb return (1); 3193592bcae8SBjoern A. Zeeb 3194592bcae8SBjoern A. Zeeb pr = cred->cr_prison; 3195592bcae8SBjoern A. Zeeb if (pr->pr_flags & PR_IP6_SADDRSEL) 3196592bcae8SBjoern A. Zeeb return (1); 3197592bcae8SBjoern A. Zeeb 3198592bcae8SBjoern A. Zeeb lia6 = in6addr_any; 3199592bcae8SBjoern A. Zeeb error = prison_get_ip6(cred, &lia6); 3200592bcae8SBjoern A. Zeeb if (error) 3201592bcae8SBjoern A. Zeeb return (error); 3202592bcae8SBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3203592bcae8SBjoern A. Zeeb return (1); 3204592bcae8SBjoern A. Zeeb 3205592bcae8SBjoern A. Zeeb bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3206592bcae8SBjoern A. Zeeb return (0); 3207592bcae8SBjoern A. Zeeb } 3208592bcae8SBjoern A. Zeeb 3209592bcae8SBjoern A. Zeeb /* 32100304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv6 address restrictions. 32110304c731SJamie Gritton */ 32120304c731SJamie Gritton int 32130304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2) 32140304c731SJamie Gritton { 32150304c731SJamie Gritton 32160304c731SJamie Gritton if (pr1 == pr2) 32170304c731SJamie Gritton return (1); 32180304c731SJamie Gritton 3219bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 3220bdfc8cc4SJamie Gritton #ifdef VIMAGE 3221bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 3222bdfc8cc4SJamie Gritton #endif 3223bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP6_USER)) 32240304c731SJamie Gritton pr1 = pr1->pr_parent; 3225bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 3226bdfc8cc4SJamie Gritton #ifdef VIMAGE 3227bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 3228bdfc8cc4SJamie Gritton #endif 3229bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP6_USER)) 32300304c731SJamie Gritton pr2 = pr2->pr_parent; 32310304c731SJamie Gritton return (pr1 == pr2); 32320304c731SJamie Gritton } 32330304c731SJamie Gritton 32340304c731SJamie Gritton /* 3235413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this jail. 3236413628a7SBjoern A. Zeeb * 3237413628a7SBjoern A. Zeeb * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3238413628a7SBjoern A. Zeeb * when needed while binding. 3239413628a7SBjoern A. Zeeb * 32400304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 32410304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 32420304c731SJamie Gritton * doesn't allow IPv6. 3243413628a7SBjoern A. Zeeb */ 3244413628a7SBjoern A. Zeeb int 3245413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3246413628a7SBjoern A. Zeeb { 3247b38ff370SJamie Gritton struct prison *pr; 3248b38ff370SJamie Gritton int error; 3249413628a7SBjoern A. Zeeb 3250413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3251413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3252413628a7SBjoern A. Zeeb 3253b38ff370SJamie Gritton pr = cred->cr_prison; 32540304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 32550304c731SJamie Gritton return (0); 3256b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 32570304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 32580304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 32590304c731SJamie Gritton return (0); 32600304c731SJamie Gritton } 3261b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3262b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3263b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3264b38ff370SJamie Gritton } 3265b89e82ddSJamie Gritton 3266413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3267b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3268b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3269413628a7SBjoern A. Zeeb return (0); 3270413628a7SBjoern A. Zeeb } 3271413628a7SBjoern A. Zeeb 3272b89e82ddSJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3273413628a7SBjoern A. Zeeb /* 3274b89e82ddSJamie Gritton * In case there is only 1 IPv6 address, and v6only is true, 3275b89e82ddSJamie Gritton * then bind directly. 3276413628a7SBjoern A. Zeeb */ 3277b38ff370SJamie Gritton if (v6only != 0 && pr->pr_ip6s == 1) 3278b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3279b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3280413628a7SBjoern A. Zeeb return (0); 3281413628a7SBjoern A. Zeeb } 3282b89e82ddSJamie Gritton 3283b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3284b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3285b38ff370SJamie Gritton return (error); 3286413628a7SBjoern A. Zeeb } 3287413628a7SBjoern A. Zeeb 3288413628a7SBjoern A. Zeeb /* 3289413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 3290413628a7SBjoern A. Zeeb * 3291b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3292413628a7SBjoern A. Zeeb */ 3293413628a7SBjoern A. Zeeb int 3294413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3295413628a7SBjoern A. Zeeb { 3296b38ff370SJamie Gritton struct prison *pr; 3297413628a7SBjoern A. Zeeb 3298413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3299413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3300413628a7SBjoern A. Zeeb 3301b38ff370SJamie Gritton pr = cred->cr_prison; 33020304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 33030304c731SJamie Gritton return (0); 3304b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 33050304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 33060304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33070304c731SJamie Gritton return (0); 33080304c731SJamie Gritton } 3309b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3310b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3311b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3312b38ff370SJamie Gritton } 3313b89e82ddSJamie Gritton 3314413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3315b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3316b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3317413628a7SBjoern A. Zeeb return (0); 3318413628a7SBjoern A. Zeeb } 3319413628a7SBjoern A. Zeeb 3320413628a7SBjoern A. Zeeb /* 3321413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 3322413628a7SBjoern A. Zeeb */ 3323b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3324413628a7SBjoern A. Zeeb return (0); 3325413628a7SBjoern A. Zeeb } 3326413628a7SBjoern A. Zeeb 3327413628a7SBjoern A. Zeeb /* 3328b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 3329413628a7SBjoern A. Zeeb * 33300304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 33310304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 33320304c731SJamie Gritton * doesn't allow IPv6. 3333413628a7SBjoern A. Zeeb */ 3334413628a7SBjoern A. Zeeb static int 3335413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 3336413628a7SBjoern A. Zeeb { 3337413628a7SBjoern A. Zeeb int i, a, z, d; 3338413628a7SBjoern A. Zeeb 3339413628a7SBjoern A. Zeeb /* 3340413628a7SBjoern A. Zeeb * Check the primary IP. 3341413628a7SBjoern A. Zeeb */ 3342413628a7SBjoern A. Zeeb if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3343b89e82ddSJamie Gritton return (0); 3344413628a7SBjoern A. Zeeb 3345413628a7SBjoern A. Zeeb /* 3346413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 3347413628a7SBjoern A. Zeeb */ 3348413628a7SBjoern A. Zeeb a = 0; 3349413628a7SBjoern A. Zeeb z = pr->pr_ip6s - 2; 3350413628a7SBjoern A. Zeeb while (a <= z) { 3351413628a7SBjoern A. Zeeb i = (a + z) / 2; 3352413628a7SBjoern A. Zeeb d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3353413628a7SBjoern A. Zeeb if (d > 0) 3354413628a7SBjoern A. Zeeb z = i - 1; 3355413628a7SBjoern A. Zeeb else if (d < 0) 3356413628a7SBjoern A. Zeeb a = i + 1; 3357413628a7SBjoern A. Zeeb else 3358413628a7SBjoern A. Zeeb return (0); 3359413628a7SBjoern A. Zeeb } 3360413628a7SBjoern A. Zeeb 3361b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 3362b89e82ddSJamie Gritton } 3363b89e82ddSJamie Gritton 3364413628a7SBjoern A. Zeeb int 3365413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3366413628a7SBjoern A. Zeeb { 3367b38ff370SJamie Gritton struct prison *pr; 3368b38ff370SJamie Gritton int error; 3369413628a7SBjoern A. Zeeb 3370413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3371413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3372413628a7SBjoern A. Zeeb 3373b38ff370SJamie Gritton pr = cred->cr_prison; 33740304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 33750304c731SJamie Gritton return (0); 3376b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 33770304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 33780304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33790304c731SJamie Gritton return (0); 33800304c731SJamie Gritton } 3381b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3382b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3383b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3384b38ff370SJamie Gritton } 3385413628a7SBjoern A. Zeeb 3386b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3387b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3388b38ff370SJamie Gritton return (error); 3389413628a7SBjoern A. Zeeb } 3390413628a7SBjoern A. Zeeb #endif 3391413628a7SBjoern A. Zeeb 3392413628a7SBjoern A. Zeeb /* 3393ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3394ca04ba64SJamie Gritton * 3395ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3396ca04ba64SJamie Gritton * if not. 3397ca04ba64SJamie Gritton */ 3398ca04ba64SJamie Gritton int 3399ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3400ca04ba64SJamie Gritton { 34010304c731SJamie Gritton struct prison *pr; 3402ca04ba64SJamie Gritton int error; 3403ca04ba64SJamie Gritton 3404ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3405ca04ba64SJamie Gritton 34060304c731SJamie Gritton pr = cred->cr_prison; 3407499650a0SJamie Gritton #ifdef VIMAGE 34086bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3409de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 34106bb79563SJamie Gritton return (0); 3411499650a0SJamie Gritton #endif 34126bb79563SJamie Gritton 3413ca04ba64SJamie Gritton error = 0; 3414ca04ba64SJamie Gritton switch (af) 3415ca04ba64SJamie Gritton { 3416ca04ba64SJamie Gritton #ifdef INET 3417ca04ba64SJamie Gritton case AF_INET: 34180304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 34190304c731SJamie Gritton { 34200304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 34210304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3422ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34230304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34240304c731SJamie Gritton } 3425ca04ba64SJamie Gritton break; 3426ca04ba64SJamie Gritton #endif 3427ca04ba64SJamie Gritton #ifdef INET6 3428ca04ba64SJamie Gritton case AF_INET6: 34290304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 34300304c731SJamie Gritton { 34310304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 34320304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3433ca04ba64SJamie Gritton error = EAFNOSUPPORT; 34340304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 34350304c731SJamie Gritton } 3436ca04ba64SJamie Gritton break; 3437ca04ba64SJamie Gritton #endif 3438ca04ba64SJamie Gritton case AF_LOCAL: 3439ca04ba64SJamie Gritton case AF_ROUTE: 3440ca04ba64SJamie Gritton break; 3441ca04ba64SJamie Gritton default: 34420304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3443ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3444ca04ba64SJamie Gritton } 3445ca04ba64SJamie Gritton return (error); 3446ca04ba64SJamie Gritton } 3447ca04ba64SJamie Gritton 3448ca04ba64SJamie Gritton /* 3449413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3450413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3451413628a7SBjoern A. Zeeb * 34520304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 34530304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 34540304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3455413628a7SBjoern A. Zeeb */ 3456413628a7SBjoern A. Zeeb int 345791421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa) 345875c13541SPoul-Henning Kamp { 3459413628a7SBjoern A. Zeeb #ifdef INET 34609ddb7954SMike Barcroft struct sockaddr_in *sai; 3461413628a7SBjoern A. Zeeb #endif 3462413628a7SBjoern A. Zeeb #ifdef INET6 3463413628a7SBjoern A. Zeeb struct sockaddr_in6 *sai6; 3464413628a7SBjoern A. Zeeb #endif 3465b89e82ddSJamie Gritton int error; 346675c13541SPoul-Henning Kamp 3467413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3468413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3469413628a7SBjoern A. Zeeb 3470de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3471de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3472de0bd6f7SBjoern A. Zeeb return (0); 3473de0bd6f7SBjoern A. Zeeb #endif 3474de0bd6f7SBjoern A. Zeeb 3475b89e82ddSJamie Gritton error = 0; 3476413628a7SBjoern A. Zeeb switch (sa->sa_family) 3477413628a7SBjoern A. Zeeb { 3478413628a7SBjoern A. Zeeb #ifdef INET 3479413628a7SBjoern A. Zeeb case AF_INET: 34809ddb7954SMike Barcroft sai = (struct sockaddr_in *)sa; 3481b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3482413628a7SBjoern A. Zeeb break; 3483413628a7SBjoern A. Zeeb #endif 3484413628a7SBjoern A. Zeeb #ifdef INET6 3485413628a7SBjoern A. Zeeb case AF_INET6: 3486413628a7SBjoern A. Zeeb sai6 = (struct sockaddr_in6 *)sa; 3487b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3488413628a7SBjoern A. Zeeb break; 3489413628a7SBjoern A. Zeeb #endif 3490413628a7SBjoern A. Zeeb default: 34910304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3492b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3493413628a7SBjoern A. Zeeb } 3494b89e82ddSJamie Gritton return (error); 349575c13541SPoul-Henning Kamp } 349691421ba2SRobert Watson 349791421ba2SRobert Watson /* 349891421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 349991421ba2SRobert Watson */ 350091421ba2SRobert Watson int 35019ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 350291421ba2SRobert Watson { 350391421ba2SRobert Watson 35040304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 35050304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 35060304c731SJamie Gritton } 350791421ba2SRobert Watson 35080304c731SJamie Gritton /* 35090304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 35100304c731SJamie Gritton */ 35110304c731SJamie Gritton int 35120304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 35130304c731SJamie Gritton { 35140304c731SJamie Gritton 35150304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 35160304c731SJamie Gritton if (pr1 == pr2) 35170304c731SJamie Gritton return (1); 351891421ba2SRobert Watson return (0); 351991421ba2SRobert Watson } 352091421ba2SRobert Watson 352191421ba2SRobert Watson /* 352291421ba2SRobert Watson * Return 1 if the passed credential is in a jail, otherwise 0. 352391421ba2SRobert Watson */ 352491421ba2SRobert Watson int 35259ddb7954SMike Barcroft jailed(struct ucred *cred) 352691421ba2SRobert Watson { 352791421ba2SRobert Watson 35280304c731SJamie Gritton return (cred->cr_prison != &prison0); 352991421ba2SRobert Watson } 35309484d0c0SRobert Drehmel 35319484d0c0SRobert Drehmel /* 3532de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 3533de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 3534de0bd6f7SBjoern A. Zeeb */ 3535de0bd6f7SBjoern A. Zeeb int 3536de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3537de0bd6f7SBjoern A. Zeeb { 3538de0bd6f7SBjoern A. Zeeb 3539de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 3540de0bd6f7SBjoern A. Zeeb return (0); 3541de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3542de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3543de0bd6f7SBjoern A. Zeeb return (0); 3544de0bd6f7SBjoern A. Zeeb #endif 3545de0bd6f7SBjoern A. Zeeb 3546de0bd6f7SBjoern A. Zeeb return (1); 3547de0bd6f7SBjoern A. Zeeb } 3548de0bd6f7SBjoern A. Zeeb 3549de0bd6f7SBjoern A. Zeeb /* 35507455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 35519484d0c0SRobert Drehmel */ 3552ad1ff099SRobert Drehmel void 35539ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 35549484d0c0SRobert Drehmel { 355576ca6f88SJamie Gritton struct prison *pr; 35569484d0c0SRobert Drehmel 35577455b100SJamie Gritton /* 35587455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 35597455b100SJamie Gritton * system's hostname. 35607455b100SJamie Gritton */ 356176ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 356276ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3563c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 356476ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 35659484d0c0SRobert Drehmel } 3566fd7a8150SMike Barcroft 35677455b100SJamie Gritton void 35687455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 35697455b100SJamie Gritton { 35707455b100SJamie Gritton 35717455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3572c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 35737455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 35747455b100SJamie Gritton } 35757455b100SJamie Gritton 35767455b100SJamie Gritton void 35777455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 35787455b100SJamie Gritton { 35797455b100SJamie Gritton 35807455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3581c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 35827455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 35837455b100SJamie Gritton } 35847455b100SJamie Gritton 35857455b100SJamie Gritton void 35867455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 35877455b100SJamie Gritton { 35887455b100SJamie Gritton 35897455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 35907455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 35917455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 35927455b100SJamie Gritton } 35937455b100SJamie Gritton 3594eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3595eb79e1c7SBjoern A. Zeeb /* 3596eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3597eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3598eb79e1c7SBjoern A. Zeeb * 3599eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3600eb79e1c7SBjoern A. Zeeb */ 3601eb79e1c7SBjoern A. Zeeb int 3602eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3603eb79e1c7SBjoern A. Zeeb { 3604eb79e1c7SBjoern A. Zeeb 3605eb79e1c7SBjoern A. Zeeb /* 3606eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3607eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3608eb79e1c7SBjoern A. Zeeb */ 3609eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3610eb79e1c7SBjoern A. Zeeb } 3611eb79e1c7SBjoern A. Zeeb #endif 3612eb79e1c7SBjoern A. Zeeb 3613f08df373SRobert Watson /* 3614820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3615820a0de9SPawel Jakub Dawidek * status of a mount point. 3616820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3617820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3618820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3619f08df373SRobert Watson */ 3620f08df373SRobert Watson int 3621820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3622f08df373SRobert Watson { 3623820a0de9SPawel Jakub Dawidek struct prison *pr; 3624820a0de9SPawel Jakub Dawidek struct statfs *sp; 3625820a0de9SPawel Jakub Dawidek size_t len; 3626f08df373SRobert Watson 3627820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 36280304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 36290304c731SJamie Gritton return (0); 3630820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3631820a0de9SPawel Jakub Dawidek return (0); 36320304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3633820a0de9SPawel Jakub Dawidek return (ENOENT); 3634820a0de9SPawel Jakub Dawidek /* 3635820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3636820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3637820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3638820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3639820a0de9SPawel Jakub Dawidek */ 3640820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3641820a0de9SPawel Jakub Dawidek return (0); 3642820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3643820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3644820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3645820a0de9SPawel Jakub Dawidek return (ENOENT); 3646820a0de9SPawel Jakub Dawidek /* 3647820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3648820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3649820a0de9SPawel Jakub Dawidek */ 3650820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3651820a0de9SPawel Jakub Dawidek return (ENOENT); 3652f08df373SRobert Watson return (0); 3653f08df373SRobert Watson } 3654820a0de9SPawel Jakub Dawidek 3655820a0de9SPawel Jakub Dawidek void 3656820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3657820a0de9SPawel Jakub Dawidek { 3658820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3659820a0de9SPawel Jakub Dawidek struct prison *pr; 3660820a0de9SPawel Jakub Dawidek size_t len; 3661820a0de9SPawel Jakub Dawidek 3662820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 36630304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 36640304c731SJamie Gritton return; 3665820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3666820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3667820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3668820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3669820a0de9SPawel Jakub Dawidek return; 3670820a0de9SPawel Jakub Dawidek } 3671820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3672820a0de9SPawel Jakub Dawidek /* 3673820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3674820a0de9SPawel Jakub Dawidek * the valid path left there. 3675820a0de9SPawel Jakub Dawidek */ 3676820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3677820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3678820a0de9SPawel Jakub Dawidek return; 3679820a0de9SPawel Jakub Dawidek } 3680820a0de9SPawel Jakub Dawidek /* 3681820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3682820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3683820a0de9SPawel Jakub Dawidek */ 3684820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3685820a0de9SPawel Jakub Dawidek return; 3686820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3687820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3688820a0de9SPawel Jakub Dawidek /* 3689820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3690820a0de9SPawel Jakub Dawidek * the valid path left there. 3691820a0de9SPawel Jakub Dawidek */ 3692820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3693820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3694820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3695820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3696820a0de9SPawel Jakub Dawidek } else { 3697820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3698820a0de9SPawel Jakub Dawidek } 3699f08df373SRobert Watson } 3700f08df373SRobert Watson 3701800c9408SRobert Watson /* 3702800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3703800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3704800c9408SRobert Watson */ 3705800c9408SRobert Watson int 3706800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3707800c9408SRobert Watson { 3708800c9408SRobert Watson 3709800c9408SRobert Watson if (!jailed(cred)) 3710800c9408SRobert Watson return (0); 3711800c9408SRobert Watson 37126bb79563SJamie Gritton #ifdef VIMAGE 37136bb79563SJamie Gritton /* 37146bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 37156bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 37166bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 37176bb79563SJamie Gritton */ 37186bb79563SJamie Gritton switch (priv) { 37196bb79563SJamie Gritton #ifdef notyet 37206bb79563SJamie Gritton /* 37216bb79563SJamie Gritton * NFS-specific privileges. 37226bb79563SJamie Gritton */ 37236bb79563SJamie Gritton case PRIV_NFS_DAEMON: 37246bb79563SJamie Gritton case PRIV_NFS_LOCKD: 37256bb79563SJamie Gritton #endif 37266bb79563SJamie Gritton /* 37276bb79563SJamie Gritton * Network stack privileges. 37286bb79563SJamie Gritton */ 37296bb79563SJamie Gritton case PRIV_NET_BRIDGE: 37306bb79563SJamie Gritton case PRIV_NET_GRE: 37316bb79563SJamie Gritton case PRIV_NET_BPF: 37326bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 37336bb79563SJamie Gritton case PRIV_NET_ROUTE: 37346bb79563SJamie Gritton case PRIV_NET_TAP: 37356bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 37366bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 37376bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3738215940b3SXin LI case PRIV_NET_SETIFDESCR: 37396bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 37406bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 37416bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 37426bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 37436bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 37446bb79563SJamie Gritton case PRIV_NET_DELMULTI: 37456bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 37466bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 37476bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 37486bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 37496bb79563SJamie Gritton case PRIV_NET_IFCREATE: 37506bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 37516bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 37526bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 37536bb79563SJamie Gritton case PRIV_NET_LAGG: 37546bb79563SJamie Gritton case PRIV_NET_GIF: 37556bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 375635fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 37576bb79563SJamie Gritton 37586bb79563SJamie Gritton /* 37596bb79563SJamie Gritton * 802.11-related privileges. 37606bb79563SJamie Gritton */ 37616bb79563SJamie Gritton case PRIV_NET80211_GETKEY: 37626bb79563SJamie Gritton #ifdef notyet 37636bb79563SJamie Gritton case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 37646bb79563SJamie Gritton #endif 37656bb79563SJamie Gritton 37666bb79563SJamie Gritton #ifdef notyet 37676bb79563SJamie Gritton /* 37686bb79563SJamie Gritton * ATM privileges. 37696bb79563SJamie Gritton */ 37706bb79563SJamie Gritton case PRIV_NETATM_CFG: 37716bb79563SJamie Gritton case PRIV_NETATM_ADD: 37726bb79563SJamie Gritton case PRIV_NETATM_DEL: 37736bb79563SJamie Gritton case PRIV_NETATM_SET: 37746bb79563SJamie Gritton 37756bb79563SJamie Gritton /* 37766bb79563SJamie Gritton * Bluetooth privileges. 37776bb79563SJamie Gritton */ 37786bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 37796bb79563SJamie Gritton #endif 37806bb79563SJamie Gritton 37816bb79563SJamie Gritton /* 37826bb79563SJamie Gritton * Netgraph and netgraph module privileges. 37836bb79563SJamie Gritton */ 37846bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 37856bb79563SJamie Gritton #ifdef notyet 37866bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 37876bb79563SJamie Gritton #endif 37886bb79563SJamie Gritton 37896bb79563SJamie Gritton /* 37906bb79563SJamie Gritton * IPv4 and IPv6 privileges. 37916bb79563SJamie Gritton */ 37926bb79563SJamie Gritton case PRIV_NETINET_IPFW: 37936bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 37946bb79563SJamie Gritton case PRIV_NETINET_PF: 37956bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 37966bb79563SJamie Gritton case PRIV_NETINET_CARP: 37976bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 37986bb79563SJamie Gritton case PRIV_NETINET_RAW: 37996bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 38006bb79563SJamie Gritton case PRIV_NETINET_ND6: 38016bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 38026bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 38036bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 38046bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 38056bb79563SJamie Gritton 38066bb79563SJamie Gritton #ifdef notyet 38076bb79563SJamie Gritton /* 38086bb79563SJamie Gritton * NCP privileges. 38096bb79563SJamie Gritton */ 38106bb79563SJamie Gritton case PRIV_NETNCP: 38116bb79563SJamie Gritton 38126bb79563SJamie Gritton /* 38136bb79563SJamie Gritton * SMB privileges. 38146bb79563SJamie Gritton */ 38156bb79563SJamie Gritton case PRIV_NETSMB: 38166bb79563SJamie Gritton #endif 38176bb79563SJamie Gritton 38186bb79563SJamie Gritton /* 38196bb79563SJamie Gritton * No default: or deny here. 38206bb79563SJamie Gritton * In case of no permit fall through to next switch(). 38216bb79563SJamie Gritton */ 38226bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 38236bb79563SJamie Gritton return (0); 38246bb79563SJamie Gritton } 38256bb79563SJamie Gritton #endif /* VIMAGE */ 38266bb79563SJamie Gritton 3827800c9408SRobert Watson switch (priv) { 3828800c9408SRobert Watson 3829800c9408SRobert Watson /* 3830800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3831800c9408SRobert Watson */ 3832800c9408SRobert Watson case PRIV_KTRACE: 3833800c9408SRobert Watson 3834c3c1b5e6SRobert Watson #if 0 3835800c9408SRobert Watson /* 3836800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3837800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3838800c9408SRobert Watson * want to further refine the relationship between audit and 3839800c9408SRobert Watson * jail. 3840800c9408SRobert Watson */ 3841800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3842800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3843800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3844c3c1b5e6SRobert Watson #endif 3845800c9408SRobert Watson 3846800c9408SRobert Watson /* 3847800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3848800c9408SRobert Watson * credentials in any way they see fit. 3849800c9408SRobert Watson */ 3850800c9408SRobert Watson case PRIV_CRED_SETUID: 3851800c9408SRobert Watson case PRIV_CRED_SETEUID: 3852800c9408SRobert Watson case PRIV_CRED_SETGID: 3853800c9408SRobert Watson case PRIV_CRED_SETEGID: 3854800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3855800c9408SRobert Watson case PRIV_CRED_SETREUID: 3856800c9408SRobert Watson case PRIV_CRED_SETREGID: 3857800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3858800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3859800c9408SRobert Watson 3860800c9408SRobert Watson /* 3861800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3862800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3863800c9408SRobert Watson */ 3864800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3865800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3866800c9408SRobert Watson 3867800c9408SRobert Watson /* 3868800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3869800c9408SRobert Watson * allow jailed root various debugging privileges. 3870800c9408SRobert Watson */ 3871800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3872800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3873800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3874800c9408SRobert Watson 3875800c9408SRobert Watson /* 3876800c9408SRobert Watson * Allow jail to set various resource limits and login 3877800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3878800c9408SRobert Watson */ 3879800c9408SRobert Watson case PRIV_PROC_LIMIT: 3880800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3881800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3882800c9408SRobert Watson 3883800c9408SRobert Watson /* 3884800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3885800c9408SRobert Watson */ 3886800c9408SRobert Watson case PRIV_IPC_READ: 3887800c9408SRobert Watson case PRIV_IPC_WRITE: 3888800c9408SRobert Watson case PRIV_IPC_ADMIN: 3889800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3890800c9408SRobert Watson case PRIV_MQ_ADMIN: 3891800c9408SRobert Watson 3892800c9408SRobert Watson /* 38930304c731SJamie Gritton * Jail operations within a jail work on child jails. 38940304c731SJamie Gritton */ 38950304c731SJamie Gritton case PRIV_JAIL_ATTACH: 38960304c731SJamie Gritton case PRIV_JAIL_SET: 38970304c731SJamie Gritton case PRIV_JAIL_REMOVE: 38980304c731SJamie Gritton 38990304c731SJamie Gritton /* 3900800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3901800c9408SRobert Watson * root processes in jail to change scheduling on other 3902800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3903800c9408SRobert Watson */ 3904800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3905413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3906800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3907800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3908800c9408SRobert Watson 3909800c9408SRobert Watson /* 3910800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3911800c9408SRobert Watson * writable. 3912800c9408SRobert Watson */ 3913800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3914800c9408SRobert Watson 3915800c9408SRobert Watson /* 3916800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3917e82d0201SRobert Watson * properties. These should likely be conditional on a 3918e82d0201SRobert Watson * configuration option. 3919800c9408SRobert Watson */ 392095b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 392195b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3922800c9408SRobert Watson 3923800c9408SRobert Watson /* 3924800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3925800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3926800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3927800c9408SRobert Watson * privileges. 3928800c9408SRobert Watson */ 3929800c9408SRobert Watson case PRIV_VFS_READ: 3930800c9408SRobert Watson case PRIV_VFS_WRITE: 3931800c9408SRobert Watson case PRIV_VFS_ADMIN: 3932800c9408SRobert Watson case PRIV_VFS_EXEC: 3933800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3934800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3935800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3936800c9408SRobert Watson case PRIV_VFS_CHOWN: 3937800c9408SRobert Watson case PRIV_VFS_CHROOT: 3938bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3939800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3940800c9408SRobert Watson case PRIV_VFS_LINK: 3941800c9408SRobert Watson case PRIV_VFS_SETGID: 3942e41966dcSRobert Watson case PRIV_VFS_STAT: 3943800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3944bb56d716SJamie Gritton 3945bb56d716SJamie Gritton /* 3946bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 3947bb56d716SJamie Gritton * able to read kernel/phyiscal memory (provided /dev/[k]mem 3948bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3949bb56d716SJamie Gritton */ 3950bb56d716SJamie Gritton case PRIV_KMEM_READ: 3951800c9408SRobert Watson return (0); 3952800c9408SRobert Watson 3953800c9408SRobert Watson /* 3954800c9408SRobert Watson * Depending on the global setting, allow privilege of 3955800c9408SRobert Watson * setting system flags. 3956800c9408SRobert Watson */ 3957800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 39580304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3959800c9408SRobert Watson return (0); 3960800c9408SRobert Watson else 3961800c9408SRobert Watson return (EPERM); 3962800c9408SRobert Watson 3963800c9408SRobert Watson /* 3964f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3965f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3966f3a8d2f9SPawel Jakub Dawidek */ 3967f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3968f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3969f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 397024b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 3971435d4667SMartin Matuska if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT && 3972435d4667SMartin Matuska cred->cr_prison->pr_enforce_statfs < 2) 3973f3a8d2f9SPawel Jakub Dawidek return (0); 3974f3a8d2f9SPawel Jakub Dawidek else 3975f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3976f3a8d2f9SPawel Jakub Dawidek 3977f3a8d2f9SPawel Jakub Dawidek /* 39784b084056SRobert Watson * Allow jailed root to bind reserved ports and reuse in-use 39794b084056SRobert Watson * ports. 3980800c9408SRobert Watson */ 3981800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 39824b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3983800c9408SRobert Watson return (0); 3984800c9408SRobert Watson 3985800c9408SRobert Watson /* 398679ba3952SBjoern A. Zeeb * Allow jailed root to set certian IPv4/6 (option) headers. 398779ba3952SBjoern A. Zeeb */ 398879ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 398979ba3952SBjoern A. Zeeb return (0); 399079ba3952SBjoern A. Zeeb 399179ba3952SBjoern A. Zeeb /* 3992800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3993800c9408SRobert Watson */ 3994800c9408SRobert Watson case PRIV_NETINET_RAW: 39950304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3996800c9408SRobert Watson return (0); 3997800c9408SRobert Watson else 3998800c9408SRobert Watson return (EPERM); 3999800c9408SRobert Watson 4000800c9408SRobert Watson /* 4001800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 4002800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 4003800c9408SRobert Watson * jail. 4004800c9408SRobert Watson */ 4005800c9408SRobert Watson case PRIV_NETINET_GETCRED: 4006800c9408SRobert Watson return (0); 4007800c9408SRobert Watson 40082bfc50bcSEdward Tomasz Napierala /* 40092bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 40102bfc50bcSEdward Tomasz Napierala */ 40112bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 40122bfc50bcSEdward Tomasz Napierala return (0); 40132bfc50bcSEdward Tomasz Napierala 4014800c9408SRobert Watson default: 4015800c9408SRobert Watson /* 4016800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 4017800c9408SRobert Watson * includes almost all network privileges, many system 4018800c9408SRobert Watson * configuration privileges. 4019800c9408SRobert Watson */ 4020800c9408SRobert Watson return (EPERM); 4021800c9408SRobert Watson } 4022800c9408SRobert Watson } 4023800c9408SRobert Watson 40240304c731SJamie Gritton /* 40250304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 40260304c731SJamie Gritton * if it does not directly follow. 40270304c731SJamie Gritton */ 40280304c731SJamie Gritton 40290304c731SJamie Gritton char * 40300304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 40310304c731SJamie Gritton { 40320304c731SJamie Gritton char *name; 40330304c731SJamie Gritton 40340304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 40350304c731SJamie Gritton if (pr1 == pr2) 40360304c731SJamie Gritton return "0"; 40370304c731SJamie Gritton name = pr2->pr_name; 40380304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 40390304c731SJamie Gritton /* 40400304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 40410304c731SJamie Gritton * so its length can't be counted on. But the number of dots 40420304c731SJamie Gritton * can be counted on - and counted. 40430304c731SJamie Gritton */ 40440304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 40450304c731SJamie Gritton name = strchr(name, '.') + 1; 40460304c731SJamie Gritton } 40470304c731SJamie Gritton return (name); 40480304c731SJamie Gritton } 40490304c731SJamie Gritton 40500304c731SJamie Gritton /* 40510304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 40520304c731SJamie Gritton * if it does not directly follow. 40530304c731SJamie Gritton */ 40540304c731SJamie Gritton static char * 40550304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 40560304c731SJamie Gritton { 40570304c731SJamie Gritton char *path1, *path2; 40580304c731SJamie Gritton int len1; 40590304c731SJamie Gritton 40600304c731SJamie Gritton path1 = pr1->pr_path; 40610304c731SJamie Gritton path2 = pr2->pr_path; 40620304c731SJamie Gritton if (!strcmp(path1, "/")) 40630304c731SJamie Gritton return (path2); 40640304c731SJamie Gritton len1 = strlen(path1); 40650304c731SJamie Gritton if (strncmp(path1, path2, len1)) 40660304c731SJamie Gritton return (path2); 40670304c731SJamie Gritton if (path2[len1] == '\0') 40680304c731SJamie Gritton return "/"; 40690304c731SJamie Gritton if (path2[len1] == '/') 40700304c731SJamie Gritton return (path2 + len1); 40710304c731SJamie Gritton return (path2); 40720304c731SJamie Gritton } 40730304c731SJamie Gritton 40740304c731SJamie Gritton 40750304c731SJamie Gritton /* 40760304c731SJamie Gritton * Jail-related sysctls. 40770304c731SJamie Gritton */ 40786472ac3dSEd Schouten static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 40790304c731SJamie Gritton "Jails"); 40800304c731SJamie Gritton 4081fd7a8150SMike Barcroft static int 4082fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4083fd7a8150SMike Barcroft { 4084b38ff370SJamie Gritton struct xprison *xp; 40850304c731SJamie Gritton struct prison *pr, *cpr; 4086b38ff370SJamie Gritton #ifdef INET 4087b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4088b38ff370SJamie Gritton int ip4s = 0; 4089b38ff370SJamie Gritton #endif 4090b38ff370SJamie Gritton #ifdef INET6 40913beefaedSColin Percival struct in6_addr *ip6 = NULL; 4092b38ff370SJamie Gritton int ip6s = 0; 4093b38ff370SJamie Gritton #endif 40940304c731SJamie Gritton int descend, error; 4095fd7a8150SMike Barcroft 4096b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 40970304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4098b38ff370SJamie Gritton error = 0; 4099dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 41000304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 41010304c731SJamie Gritton #if defined(INET) || defined(INET6) 4102b38ff370SJamie Gritton again: 41030304c731SJamie Gritton #endif 41040304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4105413628a7SBjoern A. Zeeb #ifdef INET 41060304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 41070304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 41080304c731SJamie Gritton ip4s = cpr->pr_ip4s; 41090304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4110b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 4111b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 4112b38ff370SJamie Gritton goto again; 4113b38ff370SJamie Gritton } 41140304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 41150304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 4116b38ff370SJamie Gritton } 4117413628a7SBjoern A. Zeeb #endif 4118413628a7SBjoern A. Zeeb #ifdef INET6 41190304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 41200304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 41210304c731SJamie Gritton ip6s = cpr->pr_ip6s; 41220304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4123b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 4124b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4125b38ff370SJamie Gritton goto again; 4126413628a7SBjoern A. Zeeb } 41270304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 41280304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 4129b38ff370SJamie Gritton } 4130b38ff370SJamie Gritton #endif 41310304c731SJamie Gritton if (cpr->pr_ref == 0) { 41320304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4133b38ff370SJamie Gritton continue; 4134b38ff370SJamie Gritton } 4135b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4136fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 41370304c731SJamie Gritton xp->pr_id = cpr->pr_id; 41380304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 4139b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 41400304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4141c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 41420304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4143413628a7SBjoern A. Zeeb #ifdef INET 41440304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 4145413628a7SBjoern A. Zeeb #endif 4146413628a7SBjoern A. Zeeb #ifdef INET6 41470304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 4148413628a7SBjoern A. Zeeb #endif 41490304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4150b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4151b38ff370SJamie Gritton if (error) 4152b38ff370SJamie Gritton break; 4153413628a7SBjoern A. Zeeb #ifdef INET 4154b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4155b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4156b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4157b38ff370SJamie Gritton if (error) 4158b38ff370SJamie Gritton break; 4159413628a7SBjoern A. Zeeb } 4160413628a7SBjoern A. Zeeb #endif 4161413628a7SBjoern A. Zeeb #ifdef INET6 4162b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4163b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4164b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4165b38ff370SJamie Gritton if (error) 4166b38ff370SJamie Gritton break; 4167413628a7SBjoern A. Zeeb } 4168413628a7SBjoern A. Zeeb #endif 4169fd7a8150SMike Barcroft } 4170dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4171b38ff370SJamie Gritton free(xp, M_TEMP); 4172b38ff370SJamie Gritton #ifdef INET 4173b38ff370SJamie Gritton free(ip4, M_TEMP); 4174b38ff370SJamie Gritton #endif 4175b38ff370SJamie Gritton #ifdef INET6 4176b38ff370SJamie Gritton free(ip6, M_TEMP); 4177b38ff370SJamie Gritton #endif 4178fd7a8150SMike Barcroft return (error); 4179fd7a8150SMike Barcroft } 4180fd7a8150SMike Barcroft 4181f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4182f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4183f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4184461167c2SPawel Jakub Dawidek 4185461167c2SPawel Jakub Dawidek static int 4186461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4187461167c2SPawel Jakub Dawidek { 4188461167c2SPawel Jakub Dawidek int error, injail; 4189461167c2SPawel Jakub Dawidek 4190461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4191461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4192461167c2SPawel Jakub Dawidek 4193461167c2SPawel Jakub Dawidek return (error); 4194461167c2SPawel Jakub Dawidek } 41950304c731SJamie Gritton 4196f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4197f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4198f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4199413628a7SBjoern A. Zeeb 4200761d2bb5SJamie Gritton static int 4201761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4202761d2bb5SJamie Gritton { 4203761d2bb5SJamie Gritton int error, havevnet; 4204761d2bb5SJamie Gritton #ifdef VIMAGE 4205761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4206761d2bb5SJamie Gritton 4207761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4208761d2bb5SJamie Gritton #else 4209761d2bb5SJamie Gritton havevnet = 0; 4210761d2bb5SJamie Gritton #endif 4211761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4212761d2bb5SJamie Gritton 4213761d2bb5SJamie Gritton return (error); 4214761d2bb5SJamie Gritton } 4215761d2bb5SJamie Gritton 4216761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4217761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4218761d2bb5SJamie Gritton sysctl_jail_vnet, "I", "Jail owns VNET?"); 4219761d2bb5SJamie Gritton 42200304c731SJamie Gritton #if defined(INET) || defined(INET6) 4221e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 42220304c731SJamie Gritton &jail_max_af_ips, 0, 42230304c731SJamie Gritton "Number of IP addresses a jail may have at most per address family"); 42240304c731SJamie Gritton #endif 42250304c731SJamie Gritton 42260304c731SJamie Gritton /* 42270304c731SJamie Gritton * Default parameters for jail(2) compatability. For historical reasons, 42280304c731SJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 42290304c731SJamie Gritton * just see their own parameters, and can't change them. 42300304c731SJamie Gritton */ 42310304c731SJamie Gritton static int 42320304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 42330304c731SJamie Gritton { 42340304c731SJamie Gritton struct prison *pr; 42350304c731SJamie Gritton int allow, error, i; 42360304c731SJamie Gritton 42370304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 42380304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 42390304c731SJamie Gritton 42400304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 42410304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 42420304c731SJamie Gritton if (arg1 != NULL) 42430304c731SJamie Gritton i = !i; 42440304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 42450304c731SJamie Gritton if (error || !req->newptr) 42460304c731SJamie Gritton return (error); 42470304c731SJamie Gritton i = i ? arg2 : 0; 42480304c731SJamie Gritton if (arg1 != NULL) 42490304c731SJamie Gritton i ^= arg2; 42500304c731SJamie Gritton /* 42510304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 42520304c731SJamie Gritton * for writing. 42530304c731SJamie Gritton */ 42540304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 42550304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 42560304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 42570304c731SJamie Gritton return (0); 42580304c731SJamie Gritton } 42590304c731SJamie Gritton 42600304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 42610304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42620304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 42630304c731SJamie Gritton "Processes in jail can set their hostnames"); 42640304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 42650304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42660304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 42670304c731SJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only"); 42680304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 42690304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42700304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 42710304c731SJamie Gritton "Processes in jail can use System V IPC primitives"); 42720304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 42730304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42740304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 42750304c731SJamie Gritton "Prison root can create raw sockets"); 42760304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 42770304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42780304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 42790304c731SJamie Gritton "Processes in jail can alter system file flags"); 42800304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 42810304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42820304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 42830304c731SJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems"); 4284bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_devfs_allowed, 4285bf3db8aaSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4286bf3db8aaSMartin Matuska NULL, PR_ALLOW_MOUNT_DEVFS, sysctl_jail_default_allow, "I", 4287e7af90abSMartin Matuska "Processes in jail can mount the devfs file system"); 4288464aad14SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_fdescfs_allowed, 4289464aad14SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4290464aad14SJamie Gritton NULL, PR_ALLOW_MOUNT_FDESCFS, sysctl_jail_default_allow, "I", 4291464aad14SJamie Gritton "Processes in jail can mount the fdescfs file system"); 4292bf3db8aaSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_nullfs_allowed, 4293bf3db8aaSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4294bf3db8aaSMartin Matuska NULL, PR_ALLOW_MOUNT_NULLFS, sysctl_jail_default_allow, "I", 4295e7af90abSMartin Matuska "Processes in jail can mount the nullfs file system"); 429641c0675eSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_procfs_allowed, 429741c0675eSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 429841c0675eSMartin Matuska NULL, PR_ALLOW_MOUNT_PROCFS, sysctl_jail_default_allow, "I", 429941c0675eSMartin Matuska "Processes in jail can mount the procfs file system"); 4300f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linprocfs_allowed, 4301f19e47d6SMarcelo Araujo CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4302f19e47d6SMarcelo Araujo NULL, PR_ALLOW_MOUNT_LINPROCFS, sysctl_jail_default_allow, "I", 4303f19e47d6SMarcelo Araujo "Processes in jail can mount the linprocfs file system"); 4304f19e47d6SMarcelo Araujo SYSCTL_PROC(_security_jail, OID_AUTO, mount_linsysfs_allowed, 4305f19e47d6SMarcelo Araujo CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4306f19e47d6SMarcelo Araujo NULL, PR_ALLOW_MOUNT_LINSYSFS, sysctl_jail_default_allow, "I", 4307f19e47d6SMarcelo Araujo "Processes in jail can mount the linsysfs file system"); 43082454886eSXin LI SYSCTL_PROC(_security_jail, OID_AUTO, mount_tmpfs_allowed, 43092454886eSXin LI CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43102454886eSXin LI NULL, PR_ALLOW_MOUNT_TMPFS, sysctl_jail_default_allow, "I", 43112454886eSXin LI "Processes in jail can mount the tmpfs file system"); 4312e7af90abSMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, mount_zfs_allowed, 4313e7af90abSMartin Matuska CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4314e7af90abSMartin Matuska NULL, PR_ALLOW_MOUNT_ZFS, sysctl_jail_default_allow, "I", 4315e7af90abSMartin Matuska "Processes in jail can mount the zfs file system"); 43160304c731SJamie Gritton 43170304c731SJamie Gritton static int 43180304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 43190304c731SJamie Gritton { 43200304c731SJamie Gritton struct prison *pr; 43210304c731SJamie Gritton int level, error; 43220304c731SJamie Gritton 43230304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 43240304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 43250304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 43260304c731SJamie Gritton if (error || !req->newptr) 43270304c731SJamie Gritton return (error); 43280304c731SJamie Gritton *(int *)arg1 = level; 43290304c731SJamie Gritton return (0); 43300304c731SJamie Gritton } 43310304c731SJamie Gritton 43320304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 43330304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 43340304c731SJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 43350304c731SJamie Gritton sysctl_jail_default_level, "I", 43360304c731SJamie Gritton "Processes in jail cannot see all mounted file systems"); 43370304c731SJamie Gritton 43380cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 43390cc207a6SMartin Matuska CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 43400cc207a6SMartin Matuska &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 43410cc207a6SMartin Matuska sysctl_jail_default_level, "I", 43420cc207a6SMartin Matuska "Ruleset for the devfs filesystem in jail"); 43430cc207a6SMartin Matuska 43440304c731SJamie Gritton /* 43450304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 43460304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 43470304c731SJamie Gritton * to make themselves and their types known. 43480304c731SJamie Gritton */ 43490304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 43500304c731SJamie Gritton "Jail parameters"); 43510304c731SJamie Gritton 43520304c731SJamie Gritton int 43530304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 43540304c731SJamie Gritton { 43550304c731SJamie Gritton int i; 43560304c731SJamie Gritton long l; 43570304c731SJamie Gritton size_t s; 43580304c731SJamie Gritton char numbuf[12]; 43590304c731SJamie Gritton 43600304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 43610304c731SJamie Gritton { 43620304c731SJamie Gritton case CTLTYPE_LONG: 43630304c731SJamie Gritton case CTLTYPE_ULONG: 43640304c731SJamie Gritton l = 0; 43650304c731SJamie Gritton #ifdef SCTL_MASK32 43660304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 43670304c731SJamie Gritton #endif 43680304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 43690304c731SJamie Gritton case CTLTYPE_INT: 43700304c731SJamie Gritton case CTLTYPE_UINT: 43710304c731SJamie Gritton i = 0; 43720304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 43730304c731SJamie Gritton case CTLTYPE_STRING: 4374e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 43750304c731SJamie Gritton return 43760304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 43770304c731SJamie Gritton case CTLTYPE_STRUCT: 43780304c731SJamie Gritton s = (size_t)arg2; 43790304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 43800304c731SJamie Gritton } 43810304c731SJamie Gritton return (0); 43820304c731SJamie Gritton } 43830304c731SJamie Gritton 4384b96bd95bSIan Lepore /* 4385b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4386b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4387b96bd95bSIan Lepore */ 43880304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 43890304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 43900304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 43910304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 43920304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 43930304c731SJamie Gritton "I", "Jail secure level"); 4394b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4395b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4396b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4397b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 43980304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 43990304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 44000cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 44010cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 44020304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 44030304c731SJamie Gritton "B", "Jail persistence"); 4404679e1390SJamie Gritton #ifdef VIMAGE 4405679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 44067cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4407679e1390SJamie Gritton #endif 44080304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 44090304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 44100304c731SJamie Gritton 4411b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4412b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4413b97457e2SJamie Gritton "I", "Current number of child jails"); 4414b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4415b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4416b97457e2SJamie Gritton 44177cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 44180304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 44190304c731SJamie Gritton "Jail hostname"); 442076ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 442176ca6f88SJamie Gritton "Jail NIS domainname"); 442276ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 442376ca6f88SJamie Gritton "Jail host UUID"); 442476ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 442576ca6f88SJamie Gritton "LU", "Jail host ID"); 44260304c731SJamie Gritton 44270304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 44280304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 44290304c731SJamie Gritton 44300304c731SJamie Gritton #ifdef INET 44312b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 44322b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 44330304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 44340304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4435592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4436592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4437592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 44380304c731SJamie Gritton #endif 44390304c731SJamie Gritton #ifdef INET6 44402b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 44412b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 44420304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 44430304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4444592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4445592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4446592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 44470304c731SJamie Gritton #endif 44480304c731SJamie Gritton 44490304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 44500304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 44510304c731SJamie Gritton "B", "Jail may set hostname"); 44520304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 44530304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 44540304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 44550304c731SJamie Gritton "B", "Jail may create raw sockets"); 44560304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 44570304c731SJamie Gritton "B", "Jail may alter system file flags"); 44580304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 44590304c731SJamie Gritton "B", "Jail may set file quotas"); 44600304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 44610304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 44620304c731SJamie Gritton 4463bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4464bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4465bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 4466bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, devfs, CTLTYPE_INT | CTLFLAG_RW, 4467e7af90abSMartin Matuska "B", "Jail may mount the devfs file system"); 4468464aad14SJamie Gritton SYSCTL_JAIL_PARAM(_allow_mount, fdescfs, CTLTYPE_INT | CTLFLAG_RW, 4469464aad14SJamie Gritton "B", "Jail may mount the fdescfs file system"); 4470bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, nullfs, CTLTYPE_INT | CTLFLAG_RW, 4471e7af90abSMartin Matuska "B", "Jail may mount the nullfs file system"); 447241c0675eSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, procfs, CTLTYPE_INT | CTLFLAG_RW, 447341c0675eSMartin Matuska "B", "Jail may mount the procfs file system"); 4474f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linprocfs, CTLTYPE_INT | CTLFLAG_RW, 4475f19e47d6SMarcelo Araujo "B", "Jail may mount the linprocfs file system"); 4476f19e47d6SMarcelo Araujo SYSCTL_JAIL_PARAM(_allow_mount, linsysfs, CTLTYPE_INT | CTLFLAG_RW, 4477f19e47d6SMarcelo Araujo "B", "Jail may mount the linsysfs file system"); 44782454886eSXin LI SYSCTL_JAIL_PARAM(_allow_mount, tmpfs, CTLTYPE_INT | CTLFLAG_RW, 44792454886eSXin LI "B", "Jail may mount the tmpfs file system"); 4480e7af90abSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, zfs, CTLTYPE_INT | CTLFLAG_RW, 4481e7af90abSMartin Matuska "B", "Jail may mount the zfs file system"); 4482bf3db8aaSMartin Matuska 44834b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4484097055e2SEdward Tomasz Napierala void 4485097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 448615db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 448715db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4488097055e2SEdward Tomasz Napierala { 4489a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4490097055e2SEdward Tomasz Napierala 44914b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 44924b5c9cf6SEdward Tomasz Napierala 4493097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 449415db3c07SEdward Tomasz Napierala if (pre != NULL) 449515db3c07SEdward Tomasz Napierala (pre)(); 4496a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4497a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 449815db3c07SEdward Tomasz Napierala if (post != NULL) 449915db3c07SEdward Tomasz Napierala (post)(); 4500097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4501097055e2SEdward Tomasz Napierala } 45020304c731SJamie Gritton 4503a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4504a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4505a7ad07bfSEdward Tomasz Napierala { 4506a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4507a7ad07bfSEdward Tomasz Napierala 45084b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4509a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4510a7ad07bfSEdward Tomasz Napierala 4511a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4512a7ad07bfSEdward Tomasz Napierala return (NULL); 4513a7ad07bfSEdward Tomasz Napierala 4514a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4515a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4516a7ad07bfSEdward Tomasz Napierala continue; 4517a7ad07bfSEdward Tomasz Napierala 4518a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4519a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4520a7ad07bfSEdward Tomasz Napierala return (prr); 4521a7ad07bfSEdward Tomasz Napierala } 4522a7ad07bfSEdward Tomasz Napierala 4523a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4524a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4525a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4526a7ad07bfSEdward Tomasz Napierala 4527a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4528a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4529a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4530a7ad07bfSEdward Tomasz Napierala 4531a7ad07bfSEdward Tomasz Napierala return (prr); 4532a7ad07bfSEdward Tomasz Napierala } 4533a7ad07bfSEdward Tomasz Napierala 4534a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4535a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4536a7ad07bfSEdward Tomasz Napierala { 4537a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4538a7ad07bfSEdward Tomasz Napierala 45394b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 45404b5c9cf6SEdward Tomasz Napierala 4541a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4542a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4543a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4544a7ad07bfSEdward Tomasz Napierala return (prr); 4545a7ad07bfSEdward Tomasz Napierala } 4546a7ad07bfSEdward Tomasz Napierala 4547a7ad07bfSEdward Tomasz Napierala void 4548a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4549a7ad07bfSEdward Tomasz Napierala { 4550a7ad07bfSEdward Tomasz Napierala 45514b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 45524b5c9cf6SEdward Tomasz Napierala 4553a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4554a7ad07bfSEdward Tomasz Napierala } 4555a7ad07bfSEdward Tomasz Napierala 4556c34bbd2aSEdward Tomasz Napierala static void 4557c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4558c34bbd2aSEdward Tomasz Napierala { 4559c34bbd2aSEdward Tomasz Napierala 45604b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4561c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4562c34bbd2aSEdward Tomasz Napierala 4563c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4564c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4565c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4566c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4567c34bbd2aSEdward Tomasz Napierala } 4568c34bbd2aSEdward Tomasz Napierala } 4569c34bbd2aSEdward Tomasz Napierala 4570a7ad07bfSEdward Tomasz Napierala void 4571a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4572a7ad07bfSEdward Tomasz Napierala { 4573a7ad07bfSEdward Tomasz Napierala int old; 4574a7ad07bfSEdward Tomasz Napierala 45754b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4576c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4577c34bbd2aSEdward Tomasz Napierala 4578a7ad07bfSEdward Tomasz Napierala old = prr->prr_refcount; 4579a7ad07bfSEdward Tomasz Napierala if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 4580a7ad07bfSEdward Tomasz Napierala return; 4581a7ad07bfSEdward Tomasz Napierala 4582a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4583c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4584a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4585a7ad07bfSEdward Tomasz Napierala } 4586a7ad07bfSEdward Tomasz Napierala 4587a7ad07bfSEdward Tomasz Napierala static void 4588a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4589a7ad07bfSEdward Tomasz Napierala { 4590a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4591a7ad07bfSEdward Tomasz Napierala 45924b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4593c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4594c34bbd2aSEdward Tomasz Napierala 4595a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4596a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4597a7ad07bfSEdward Tomasz Napierala 4598a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4599a7ad07bfSEdward Tomasz Napierala } 4600a7ad07bfSEdward Tomasz Napierala 4601c34bbd2aSEdward Tomasz Napierala /* 4602c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4603c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4604c34bbd2aSEdward Tomasz Napierala */ 4605c34bbd2aSEdward Tomasz Napierala static void 4606c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4607c34bbd2aSEdward Tomasz Napierala { 4608c34bbd2aSEdward Tomasz Napierala struct proc *p; 4609c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4610c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4611c34bbd2aSEdward Tomasz Napierala 46124b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46134b5c9cf6SEdward Tomasz Napierala 4614c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4615c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4616c34bbd2aSEdward Tomasz Napierala 4617e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4618e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4619e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4620c34bbd2aSEdward Tomasz Napierala return; 4621e30345e7SEdward Tomasz Napierala } 4622c34bbd2aSEdward Tomasz Napierala 4623c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4624c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4625c34bbd2aSEdward Tomasz Napierala 4626c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4627c34bbd2aSEdward Tomasz Napierala 4628c34bbd2aSEdward Tomasz Napierala /* 4629c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4630c34bbd2aSEdward Tomasz Napierala */ 4631c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4632c34bbd2aSEdward Tomasz Napierala 4633c34bbd2aSEdward Tomasz Napierala /* 4634c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4635c34bbd2aSEdward Tomasz Napierala */ 4636c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4637c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4638c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4639c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4640c34bbd2aSEdward Tomasz Napierala racct_proc_ucred_changed(p, cred, cred); 4641c34bbd2aSEdward Tomasz Napierala crfree(cred); 4642c34bbd2aSEdward Tomasz Napierala } 4643c34bbd2aSEdward Tomasz Napierala 4644c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4645c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4646c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4647c34bbd2aSEdward Tomasz Napierala } 4648c34bbd2aSEdward Tomasz Napierala 4649a7ad07bfSEdward Tomasz Napierala static void 4650a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4651a7ad07bfSEdward Tomasz Napierala { 4652c34bbd2aSEdward Tomasz Napierala 46534b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4654c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4655c34bbd2aSEdward Tomasz Napierala 4656af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4657af3c786cSMateusz Guzik return; 4658a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4659a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4660a7ad07bfSEdward Tomasz Napierala } 4661a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4662a7ad07bfSEdward Tomasz Napierala 4663413628a7SBjoern A. Zeeb #ifdef DDB 4664b38ff370SJamie Gritton 4665b38ff370SJamie Gritton static void 4666b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4667413628a7SBjoern A. Zeeb { 46680304c731SJamie Gritton int fi; 4669b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4670b38ff370SJamie Gritton int ii; 4671413628a7SBjoern A. Zeeb #endif 46727cbf7213SJamie Gritton unsigned jsf; 4673413628a7SBjoern A. Zeeb #ifdef INET6 4674413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4675413628a7SBjoern A. Zeeb #endif 4676413628a7SBjoern A. Zeeb 4677b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4678b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4679b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 46800304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4681b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4682b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4683b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4684b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4685b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4686679e1390SJamie Gritton #ifdef VIMAGE 4687679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4688679e1390SJamie Gritton #endif 4689b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4690b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 46910cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4692fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4693fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 46940304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 46950304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4696fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 46970304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 46980304c731SJamie Gritton fi++) 46990304c731SJamie Gritton if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 47000304c731SJamie Gritton db_printf(" %s", pr_flag_names[fi]); 47017cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 47027cbf7213SJamie Gritton fi++) { 47037cbf7213SJamie Gritton jsf = pr->pr_flags & 47047cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 47057cbf7213SJamie Gritton db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 47067cbf7213SJamie Gritton pr_flag_jailsys[fi].disable && 47077cbf7213SJamie Gritton (jsf == pr_flag_jailsys[fi].disable) ? "disable" 47087cbf7213SJamie Gritton : (jsf == pr_flag_jailsys[fi].new) ? "new" 47097cbf7213SJamie Gritton : "inherit"); 47107cbf7213SJamie Gritton } 4711fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 47120304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 47130304c731SJamie Gritton fi++) 47140304c731SJamie Gritton if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 47150304c731SJamie Gritton db_printf(" %s", pr_allow_names[fi]); 4716b38ff370SJamie Gritton db_printf("\n"); 47170304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4718c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4719c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4720c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 472176ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4722413628a7SBjoern A. Zeeb #ifdef INET 4723b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4724b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4725b38ff370SJamie Gritton db_printf(" %s %s\n", 4726fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 4727b38ff370SJamie Gritton inet_ntoa(pr->pr_ip4[ii])); 4728413628a7SBjoern A. Zeeb #endif 4729413628a7SBjoern A. Zeeb #ifdef INET6 4730b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4731b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4732b38ff370SJamie Gritton db_printf(" %s %s\n", 4733fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4734b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4735b38ff370SJamie Gritton #endif 4736b38ff370SJamie Gritton } 4737b38ff370SJamie Gritton 4738b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4739b38ff370SJamie Gritton { 4740b38ff370SJamie Gritton struct prison *pr; 4741b38ff370SJamie Gritton 4742b38ff370SJamie Gritton if (!have_addr) { 47430304c731SJamie Gritton /* 47440304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 47450304c731SJamie Gritton * listed. 47460304c731SJamie Gritton */ 47470304c731SJamie Gritton db_show_prison(&prison0); 47480304c731SJamie Gritton if (!db_pager_quit) { 4749b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4750b38ff370SJamie Gritton db_show_prison(pr); 4751413628a7SBjoern A. Zeeb if (db_pager_quit) 4752413628a7SBjoern A. Zeeb break; 4753413628a7SBjoern A. Zeeb } 47540304c731SJamie Gritton } 4755b38ff370SJamie Gritton return; 4756413628a7SBjoern A. Zeeb } 4757b38ff370SJamie Gritton 47580304c731SJamie Gritton if (addr == 0) 47590304c731SJamie Gritton pr = &prison0; 47600304c731SJamie Gritton else { 4761b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4762b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4763b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4764b38ff370SJamie Gritton break; 4765b38ff370SJamie Gritton if (pr == NULL) 4766b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4767b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4768b38ff370SJamie Gritton if (pr->pr_id == addr) 4769b38ff370SJamie Gritton break; 4770b38ff370SJamie Gritton if (pr == NULL) 4771b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4772b38ff370SJamie Gritton pr = (struct prison *)addr; 47730304c731SJamie Gritton } 4774b38ff370SJamie Gritton db_show_prison(pr); 4775b38ff370SJamie Gritton } 4776b38ff370SJamie Gritton 4777413628a7SBjoern A. Zeeb #endif /* DDB */ 4778