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 #ifdef INET6 72413628a7SBjoern A. Zeeb #include <netinet6/in6_var.h> 73413628a7SBjoern A. Zeeb #endif /* INET6 */ 74413628a7SBjoern A. Zeeb #endif /* DDB */ 7575c13541SPoul-Henning Kamp 76aed55708SRobert Watson #include <security/mac/mac_framework.h> 77aed55708SRobert Watson 788986e3a0SJamie Gritton #define DEFAULT_HOSTUUID "00000000-0000-0000-0000-000000000000" 798986e3a0SJamie Gritton 8075c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 81a7ad07bfSEdward Tomasz Napierala MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8275c13541SPoul-Henning Kamp 83592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 84592bcae8SBjoern A. Zeeb #ifdef INET 85592bcae8SBjoern A. Zeeb #ifdef INET6 86592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 87592bcae8SBjoern A. Zeeb #else 88592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 89592bcae8SBjoern A. Zeeb #endif 90592bcae8SBjoern A. Zeeb #else /* !INET */ 91592bcae8SBjoern A. Zeeb #ifdef INET6 92592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 93592bcae8SBjoern A. Zeeb #else 94592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 95592bcae8SBjoern A. Zeeb #endif 96592bcae8SBjoern A. Zeeb #endif 97592bcae8SBjoern A. Zeeb 980304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 990304c731SJamie Gritton struct prison prison0 = { 1000304c731SJamie Gritton .pr_id = 0, 1010304c731SJamie Gritton .pr_name = "0", 1020304c731SJamie Gritton .pr_ref = 1, 1030304c731SJamie Gritton .pr_uref = 1, 1040304c731SJamie Gritton .pr_path = "/", 1050304c731SJamie Gritton .pr_securelevel = -1, 106b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1078986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 10813e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 109eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 110592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 111eb79e1c7SBjoern A. Zeeb #else 112592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 113eb79e1c7SBjoern A. Zeeb #endif 1140304c731SJamie Gritton .pr_allow = PR_ALLOW_ALL, 1150304c731SJamie Gritton }; 1160304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 11783f1e257SRobert Watson 118a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 119dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 120b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 121b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 122a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1232110d913SXin LI int lastprid = 0; 124fd7a8150SMike Barcroft 125b38ff370SJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr); 126b3059e09SRobert Watson static void prison_complete(void *context, int pending); 127b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 1280304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 1290304c731SJamie Gritton static void prison_remove_one(struct prison *pr); 130a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 131a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 132a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 133a7ad07bfSEdward Tomasz Napierala #endif 134413628a7SBjoern A. Zeeb #ifdef INET 1358571af59SJamie Gritton static int _prison_check_ip4(struct prison *pr, struct in_addr *ia); 1360304c731SJamie Gritton static int prison_restrict_ip4(struct prison *pr, struct in_addr *newip4); 137413628a7SBjoern A. Zeeb #endif 138413628a7SBjoern A. Zeeb #ifdef INET6 1398571af59SJamie Gritton static int _prison_check_ip6(struct prison *pr, struct in6_addr *ia6); 1400304c731SJamie Gritton static int prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6); 141413628a7SBjoern A. Zeeb #endif 142fd7a8150SMike Barcroft 143b38ff370SJamie Gritton /* Flags for prison_deref */ 144b38ff370SJamie Gritton #define PD_DEREF 0x01 145b38ff370SJamie Gritton #define PD_DEUREF 0x02 146b38ff370SJamie Gritton #define PD_LOCKED 0x04 147b38ff370SJamie Gritton #define PD_LIST_SLOCKED 0x08 148b38ff370SJamie Gritton #define PD_LIST_XLOCKED 0x10 149fd7a8150SMike Barcroft 1500304c731SJamie Gritton /* 1515cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1525cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1535cc70397SBjoern A. Zeeb * terminating entry. 1540304c731SJamie Gritton */ 1550304c731SJamie Gritton static char *pr_flag_names[] = { 1560304c731SJamie Gritton [0] = "persist", 157592bcae8SBjoern A. Zeeb #ifdef INET 158592bcae8SBjoern A. Zeeb [7] = "ip4.saddrsel", 159592bcae8SBjoern A. Zeeb #endif 160592bcae8SBjoern A. Zeeb #ifdef INET6 161592bcae8SBjoern A. Zeeb [8] = "ip6.saddrsel", 162592bcae8SBjoern A. Zeeb #endif 1630304c731SJamie Gritton }; 1645cc70397SBjoern A. Zeeb const size_t pr_flag_names_size = sizeof(pr_flag_names); 1650304c731SJamie Gritton 1660304c731SJamie Gritton static char *pr_flag_nonames[] = { 1670304c731SJamie Gritton [0] = "nopersist", 168592bcae8SBjoern A. Zeeb #ifdef INET 169592bcae8SBjoern A. Zeeb [7] = "ip4.nosaddrsel", 170592bcae8SBjoern A. Zeeb #endif 171592bcae8SBjoern A. Zeeb #ifdef INET6 172592bcae8SBjoern A. Zeeb [8] = "ip6.nosaddrsel", 173592bcae8SBjoern A. Zeeb #endif 1747cbf7213SJamie Gritton }; 1755cc70397SBjoern A. Zeeb const size_t pr_flag_nonames_size = sizeof(pr_flag_nonames); 1767cbf7213SJamie Gritton 1777cbf7213SJamie Gritton struct jailsys_flags { 1787cbf7213SJamie Gritton const char *name; 1797cbf7213SJamie Gritton unsigned disable; 1807cbf7213SJamie Gritton unsigned new; 1817cbf7213SJamie Gritton } pr_flag_jailsys[] = { 1827cbf7213SJamie Gritton { "host", 0, PR_HOST }, 1837cbf7213SJamie Gritton #ifdef VIMAGE 1847cbf7213SJamie Gritton { "vnet", 0, PR_VNET }, 1857cbf7213SJamie Gritton #endif 1860304c731SJamie Gritton #ifdef INET 1877cbf7213SJamie Gritton { "ip4", PR_IP4_USER | PR_IP4_DISABLE, PR_IP4_USER }, 1880304c731SJamie Gritton #endif 1890304c731SJamie Gritton #ifdef INET6 1907cbf7213SJamie Gritton { "ip6", PR_IP6_USER | PR_IP6_DISABLE, PR_IP6_USER }, 191679e1390SJamie Gritton #endif 1920304c731SJamie Gritton }; 1935cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1940304c731SJamie Gritton 1950304c731SJamie Gritton static char *pr_allow_names[] = { 1960304c731SJamie Gritton "allow.set_hostname", 1970304c731SJamie Gritton "allow.sysvipc", 1980304c731SJamie Gritton "allow.raw_sockets", 1990304c731SJamie Gritton "allow.chflags", 2000304c731SJamie Gritton "allow.mount", 2010304c731SJamie Gritton "allow.quotas", 2020304c731SJamie Gritton "allow.socket_af", 2030304c731SJamie Gritton }; 2045cc70397SBjoern A. Zeeb const size_t pr_allow_names_size = sizeof(pr_allow_names); 2050304c731SJamie Gritton 2060304c731SJamie Gritton static char *pr_allow_nonames[] = { 2070304c731SJamie Gritton "allow.noset_hostname", 2080304c731SJamie Gritton "allow.nosysvipc", 2090304c731SJamie Gritton "allow.noraw_sockets", 2100304c731SJamie Gritton "allow.nochflags", 2110304c731SJamie Gritton "allow.nomount", 2120304c731SJamie Gritton "allow.noquotas", 2130304c731SJamie Gritton "allow.nosocket_af", 2140304c731SJamie Gritton }; 2155cc70397SBjoern A. Zeeb const size_t pr_allow_nonames_size = sizeof(pr_allow_nonames); 2160304c731SJamie Gritton 2170304c731SJamie Gritton #define JAIL_DEFAULT_ALLOW PR_ALLOW_SET_HOSTNAME 21842bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 2190304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 22042bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2210304c731SJamie Gritton #if defined(INET) || defined(INET6) 222e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2230304c731SJamie Gritton #endif 2240304c731SJamie Gritton 225413628a7SBjoern A. Zeeb #ifdef INET 226413628a7SBjoern A. Zeeb static int 227413628a7SBjoern A. Zeeb qcmp_v4(const void *ip1, const void *ip2) 228413628a7SBjoern A. Zeeb { 229413628a7SBjoern A. Zeeb in_addr_t iaa, iab; 230413628a7SBjoern A. Zeeb 231413628a7SBjoern A. Zeeb /* 232413628a7SBjoern A. Zeeb * We need to compare in HBO here to get the list sorted as expected 233413628a7SBjoern A. Zeeb * by the result of the code. Sorting NBO addresses gives you 234413628a7SBjoern A. Zeeb * interesting results. If you do not understand, do not try. 235413628a7SBjoern A. Zeeb */ 236413628a7SBjoern A. Zeeb iaa = ntohl(((const struct in_addr *)ip1)->s_addr); 237413628a7SBjoern A. Zeeb iab = ntohl(((const struct in_addr *)ip2)->s_addr); 238413628a7SBjoern A. Zeeb 239413628a7SBjoern A. Zeeb /* 240413628a7SBjoern A. Zeeb * Do not simply return the difference of the two numbers, the int is 241413628a7SBjoern A. Zeeb * not wide enough. 242413628a7SBjoern A. Zeeb */ 243413628a7SBjoern A. Zeeb if (iaa > iab) 244413628a7SBjoern A. Zeeb return (1); 245413628a7SBjoern A. Zeeb else if (iaa < iab) 246413628a7SBjoern A. Zeeb return (-1); 247413628a7SBjoern A. Zeeb else 248413628a7SBjoern A. Zeeb return (0); 249413628a7SBjoern A. Zeeb } 250413628a7SBjoern A. Zeeb #endif 251413628a7SBjoern A. Zeeb 252413628a7SBjoern A. Zeeb #ifdef INET6 253413628a7SBjoern A. Zeeb static int 254413628a7SBjoern A. Zeeb qcmp_v6(const void *ip1, const void *ip2) 255413628a7SBjoern A. Zeeb { 256413628a7SBjoern A. Zeeb const struct in6_addr *ia6a, *ia6b; 257413628a7SBjoern A. Zeeb int i, rc; 258413628a7SBjoern A. Zeeb 259413628a7SBjoern A. Zeeb ia6a = (const struct in6_addr *)ip1; 260413628a7SBjoern A. Zeeb ia6b = (const struct in6_addr *)ip2; 261413628a7SBjoern A. Zeeb 262413628a7SBjoern A. Zeeb rc = 0; 263413628a7SBjoern A. Zeeb for (i = 0; rc == 0 && i < sizeof(struct in6_addr); i++) { 264413628a7SBjoern A. Zeeb if (ia6a->s6_addr[i] > ia6b->s6_addr[i]) 265413628a7SBjoern A. Zeeb rc = 1; 266413628a7SBjoern A. Zeeb else if (ia6a->s6_addr[i] < ia6b->s6_addr[i]) 267413628a7SBjoern A. Zeeb rc = -1; 268413628a7SBjoern A. Zeeb } 269413628a7SBjoern A. Zeeb return (rc); 270413628a7SBjoern A. Zeeb } 271413628a7SBjoern A. Zeeb #endif 272413628a7SBjoern A. Zeeb 273116734c4SMatthew Dillon /* 2749ddb7954SMike Barcroft * struct jail_args { 2759ddb7954SMike Barcroft * struct jail *jail; 2769ddb7954SMike Barcroft * }; 277116734c4SMatthew Dillon */ 27875c13541SPoul-Henning Kamp int 2799ddb7954SMike Barcroft jail(struct thread *td, struct jail_args *uap) 28075c13541SPoul-Henning Kamp { 281413628a7SBjoern A. Zeeb uint32_t version; 282413628a7SBjoern A. Zeeb int error; 2830304c731SJamie Gritton struct jail j; 284413628a7SBjoern A. Zeeb 285413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 286413628a7SBjoern A. Zeeb if (error) 287413628a7SBjoern A. Zeeb return (error); 288413628a7SBjoern A. Zeeb 289413628a7SBjoern A. Zeeb switch (version) { 290413628a7SBjoern A. Zeeb case 0: 291413628a7SBjoern A. Zeeb { 292413628a7SBjoern A. Zeeb struct jail_v0 j0; 293413628a7SBjoern A. Zeeb 2940304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 2950304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 296413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 297413628a7SBjoern A. Zeeb if (error) 298413628a7SBjoern A. Zeeb return (error); 2990304c731SJamie Gritton j.version = j0.version; 3000304c731SJamie Gritton j.path = j0.path; 3010304c731SJamie Gritton j.hostname = j0.hostname; 3020304c731SJamie Gritton j.ip4s = j0.ip_number; 303413628a7SBjoern A. Zeeb break; 304413628a7SBjoern A. Zeeb } 305413628a7SBjoern A. Zeeb 306413628a7SBjoern A. Zeeb case 1: 307413628a7SBjoern A. Zeeb /* 308413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 309413628a7SBjoern A. Zeeb * that never made it into the official kernel. 310413628a7SBjoern A. Zeeb */ 311413628a7SBjoern A. Zeeb return (EINVAL); 312413628a7SBjoern A. Zeeb 313413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 314413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 315413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 316413628a7SBjoern A. Zeeb if (error) 317413628a7SBjoern A. Zeeb return (error); 3180304c731SJamie Gritton break; 3190304c731SJamie Gritton 3200304c731SJamie Gritton default: 3210304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3220304c731SJamie Gritton return (EINVAL); 3230304c731SJamie Gritton } 3240304c731SJamie Gritton return (kern_jail(td, &j)); 3250304c731SJamie Gritton } 3260304c731SJamie Gritton 3270304c731SJamie Gritton int 3280304c731SJamie Gritton kern_jail(struct thread *td, struct jail *j) 3290304c731SJamie Gritton { 330e92e0574SJamie Gritton struct iovec optiov[2 * (4 331e92e0574SJamie Gritton + sizeof(pr_allow_names) / sizeof(pr_allow_names[0]) 332e92e0574SJamie Gritton #ifdef INET 333e92e0574SJamie Gritton + 1 334e92e0574SJamie Gritton #endif 335e92e0574SJamie Gritton #ifdef INET6 336e92e0574SJamie Gritton + 1 337e92e0574SJamie Gritton #endif 338e92e0574SJamie Gritton )]; 3390304c731SJamie Gritton struct uio opt; 3400304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 3410304c731SJamie Gritton #ifdef INET 342e92e0574SJamie Gritton uint32_t ip4s; 3430304c731SJamie Gritton struct in_addr *u_ip4; 3440304c731SJamie Gritton #endif 3450304c731SJamie Gritton #ifdef INET6 3460304c731SJamie Gritton struct in6_addr *u_ip6; 3470304c731SJamie Gritton #endif 3480304c731SJamie Gritton size_t tmplen; 3490304c731SJamie Gritton int error, enforce_statfs, fi; 3500304c731SJamie Gritton 3510304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3520304c731SJamie Gritton opt.uio_iov = optiov; 3530304c731SJamie Gritton opt.uio_iovcnt = 0; 3540304c731SJamie Gritton opt.uio_offset = -1; 3550304c731SJamie Gritton opt.uio_resid = -1; 3560304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3570304c731SJamie Gritton opt.uio_rw = UIO_READ; 3580304c731SJamie Gritton opt.uio_td = td; 3590304c731SJamie Gritton 3600304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3610304c731SJamie Gritton if (!jailed(td->td_ucred)) { 3620304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / 3630304c731SJamie Gritton sizeof(pr_allow_names[0]); fi++) { 3640304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = 3650304c731SJamie Gritton (jail_default_allow & (1 << fi)) 3660304c731SJamie Gritton ? pr_allow_names[fi] : pr_allow_nonames[fi]; 3670304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3680304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3690304c731SJamie Gritton opt.uio_iovcnt += 2; 3700304c731SJamie Gritton } 3710304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3720304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 3730304c731SJamie Gritton opt.uio_iovcnt++; 3740304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 3750304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 3760304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 3770304c731SJamie Gritton opt.uio_iovcnt++; 3780304c731SJamie Gritton } 3790304c731SJamie Gritton 380b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 381b38ff370SJamie Gritton #ifdef INET 3820304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 3830304c731SJamie Gritton if (ip4s > jail_max_af_ips) 384b38ff370SJamie Gritton return (EINVAL); 3850304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 386b38ff370SJamie Gritton #else 3870304c731SJamie Gritton if (j->ip4s > 0) 388b38ff370SJamie Gritton return (EINVAL); 389b38ff370SJamie Gritton #endif 390b38ff370SJamie Gritton #ifdef INET6 3910304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 392b38ff370SJamie Gritton return (EINVAL); 3930304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 394b38ff370SJamie Gritton #else 3950304c731SJamie Gritton if (j->ip6s > 0) 396b38ff370SJamie Gritton return (EINVAL); 397b38ff370SJamie Gritton #endif 398b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 399b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 400b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 401b38ff370SJamie Gritton #ifdef INET 402b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 403b38ff370SJamie Gritton #endif 404b38ff370SJamie Gritton #ifdef INET6 405b38ff370SJamie Gritton #ifdef INET 4060304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 407b38ff370SJamie Gritton #else 408b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 409b38ff370SJamie Gritton #endif 410b38ff370SJamie Gritton #endif 4110304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4120304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4130304c731SJamie Gritton opt.uio_iovcnt++; 4140304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4150304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4160304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 417b38ff370SJamie Gritton if (error) { 418b38ff370SJamie Gritton free(u_path, M_TEMP); 419b38ff370SJamie Gritton return (error); 420b38ff370SJamie Gritton } 4210304c731SJamie Gritton opt.uio_iovcnt++; 4220304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4230304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4240304c731SJamie Gritton opt.uio_iovcnt++; 4250304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4260304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4270304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 428b38ff370SJamie Gritton if (error) { 429b38ff370SJamie Gritton free(u_path, M_TEMP); 430b38ff370SJamie Gritton return (error); 431b38ff370SJamie Gritton } 4320304c731SJamie Gritton opt.uio_iovcnt++; 4330304c731SJamie Gritton if (j->jailname != NULL) { 434b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 435b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 436b38ff370SJamie Gritton opt.uio_iovcnt++; 437b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4380304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 439b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 440b38ff370SJamie Gritton if (error) { 441b38ff370SJamie Gritton free(u_path, M_TEMP); 442b38ff370SJamie Gritton return (error); 443b38ff370SJamie Gritton } 444b38ff370SJamie Gritton opt.uio_iovcnt++; 445b38ff370SJamie Gritton } 446b38ff370SJamie Gritton #ifdef INET 447b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 448b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 449b38ff370SJamie Gritton opt.uio_iovcnt++; 450b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4510304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4520304c731SJamie Gritton if (j->version == 0) 4530304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4540304c731SJamie Gritton else { 4550304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 456b38ff370SJamie Gritton if (error) { 457b38ff370SJamie Gritton free(u_path, M_TEMP); 458b38ff370SJamie Gritton return (error); 459b38ff370SJamie Gritton } 4600304c731SJamie Gritton } 461b38ff370SJamie Gritton opt.uio_iovcnt++; 462b38ff370SJamie Gritton #endif 463b38ff370SJamie Gritton #ifdef INET6 464b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 465b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 466b38ff370SJamie Gritton opt.uio_iovcnt++; 467b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4680304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4690304c731SJamie Gritton error = copyin(j->ip6, u_ip6, 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 #endif 4760304c731SJamie Gritton KASSERT(opt.uio_iovcnt <= sizeof(optiov) / sizeof(optiov[0]), 4770304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 478b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 479b38ff370SJamie Gritton free(u_path, M_TEMP); 480b38ff370SJamie Gritton return (error); 481b38ff370SJamie Gritton } 482b38ff370SJamie Gritton 4830304c731SJamie Gritton 484b38ff370SJamie Gritton /* 485b38ff370SJamie Gritton * struct jail_set_args { 486b38ff370SJamie Gritton * struct iovec *iovp; 487b38ff370SJamie Gritton * unsigned int iovcnt; 488b38ff370SJamie Gritton * int flags; 489b38ff370SJamie Gritton * }; 490b38ff370SJamie Gritton */ 491b38ff370SJamie Gritton int 492b38ff370SJamie Gritton jail_set(struct thread *td, struct jail_set_args *uap) 493b38ff370SJamie Gritton { 494b38ff370SJamie Gritton struct uio *auio; 495b38ff370SJamie Gritton int error; 496b38ff370SJamie Gritton 497b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 498b38ff370SJamie Gritton if (uap->iovcnt & 1) 499b38ff370SJamie Gritton return (EINVAL); 500b38ff370SJamie Gritton 501b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 502b38ff370SJamie Gritton if (error) 503b38ff370SJamie Gritton return (error); 504b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 505b38ff370SJamie Gritton free(auio, M_IOV); 506b38ff370SJamie Gritton return (error); 507413628a7SBjoern A. Zeeb } 508413628a7SBjoern A. Zeeb 509413628a7SBjoern A. Zeeb int 510b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 511413628a7SBjoern A. Zeeb { 512fd7a8150SMike Barcroft struct nameidata nd; 513b38ff370SJamie Gritton #ifdef INET 514b38ff370SJamie Gritton struct in_addr *ip4; 515413628a7SBjoern A. Zeeb #endif 516b38ff370SJamie Gritton #ifdef INET6 517b38ff370SJamie Gritton struct in6_addr *ip6; 518b38ff370SJamie Gritton #endif 519b38ff370SJamie Gritton struct vfsopt *opt; 520b38ff370SJamie Gritton struct vfsoptlist *opts; 52157aea6dfSBjoern A. Zeeb struct prison *pr, *deadpr, *mypr, *ppr, *tpr; 522b38ff370SJamie Gritton struct vnode *root; 523babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 5240304c731SJamie Gritton #if defined(INET) || defined(INET6) 52557aea6dfSBjoern A. Zeeb struct prison *tppr; 526b38ff370SJamie Gritton void *op; 5270304c731SJamie Gritton #endif 52876ca6f88SJamie Gritton unsigned long hid; 5290304c731SJamie Gritton size_t namelen, onamelen; 5300304c731SJamie Gritton int created, cuflags, descend, enforce, error, errmsg_len, errmsg_pos; 5317cbf7213SJamie Gritton int gotchildmax, gotenforce, gothid, gotslevel; 5327cbf7213SJamie Gritton int fi, jid, jsys, len, level; 533b97457e2SJamie Gritton int childmax, slevel, vfslocked; 534d465a41dSBjoern A. Zeeb #if defined(INET) || defined(INET6) 5350304c731SJamie Gritton int ii, ij; 536413628a7SBjoern A. Zeeb #endif 537413628a7SBjoern A. Zeeb #ifdef INET 5382b0d6f81SJamie Gritton int ip4s, redo_ip4; 539413628a7SBjoern A. Zeeb #endif 540b38ff370SJamie Gritton #ifdef INET6 5412b0d6f81SJamie Gritton int ip6s, redo_ip6; 542b38ff370SJamie Gritton #endif 543*6beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 544*6beb3bb4SKirk McKusick unsigned tallow; 545b38ff370SJamie Gritton char numbuf[12]; 546b38ff370SJamie Gritton 547b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 548b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 549b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 550b38ff370SJamie Gritton if (error) 55175c13541SPoul-Henning Kamp return (error); 5520304c731SJamie Gritton mypr = ppr = td->td_ucred->cr_prison; 553b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 5540304c731SJamie Gritton return (EPERM); 555b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 556b38ff370SJamie Gritton return (EINVAL); 557b38ff370SJamie Gritton 558b38ff370SJamie Gritton /* 559b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 560b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 561b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 562b38ff370SJamie Gritton * the allprison lock. 563b38ff370SJamie Gritton * 564b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 565b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 566b38ff370SJamie Gritton * than duplicate it under a different name. 567b38ff370SJamie Gritton */ 568b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 569b38ff370SJamie Gritton if (error) 570b38ff370SJamie Gritton return (error); 571b38ff370SJamie Gritton #ifdef INET 572b38ff370SJamie Gritton ip4 = NULL; 573b38ff370SJamie Gritton #endif 574b38ff370SJamie Gritton #ifdef INET6 575b38ff370SJamie Gritton ip6 = NULL; 576b38ff370SJamie Gritton #endif 577b38ff370SJamie Gritton 578b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 579b38ff370SJamie Gritton if (error == ENOENT) 580b38ff370SJamie Gritton jid = 0; 581b38ff370SJamie Gritton else if (error != 0) 582b38ff370SJamie Gritton goto done_free; 583b38ff370SJamie Gritton 584b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 585b38ff370SJamie Gritton if (error == ENOENT) 586b38ff370SJamie Gritton gotslevel = 0; 587b38ff370SJamie Gritton else if (error != 0) 588b38ff370SJamie Gritton goto done_free; 589b38ff370SJamie Gritton else 590b38ff370SJamie Gritton gotslevel = 1; 591b38ff370SJamie Gritton 592b97457e2SJamie Gritton error = 593b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 594b97457e2SJamie Gritton if (error == ENOENT) 595b97457e2SJamie Gritton gotchildmax = 0; 596b97457e2SJamie Gritton else if (error != 0) 597b97457e2SJamie Gritton goto done_free; 598b97457e2SJamie Gritton else 599b97457e2SJamie Gritton gotchildmax = 1; 600b97457e2SJamie Gritton 6010304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 602f337198dSJamie Gritton if (error == ENOENT) 603f337198dSJamie Gritton gotenforce = 0; 604f337198dSJamie Gritton else if (error != 0) 6050304c731SJamie Gritton goto done_free; 606f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 607f337198dSJamie Gritton error = EINVAL; 608f337198dSJamie Gritton goto done_free; 609f337198dSJamie Gritton } else 610f337198dSJamie Gritton gotenforce = 1; 6110304c731SJamie Gritton 612b38ff370SJamie Gritton pr_flags = ch_flags = 0; 6130304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 6140304c731SJamie Gritton fi++) { 6150304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 6160304c731SJamie Gritton continue; 6170304c731SJamie Gritton vfs_flagopt(opts, pr_flag_names[fi], &pr_flags, 1 << fi); 6180304c731SJamie Gritton vfs_flagopt(opts, pr_flag_nonames[fi], &ch_flags, 1 << fi); 6190304c731SJamie Gritton } 620b38ff370SJamie Gritton ch_flags |= pr_flags; 6217cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 6227cbf7213SJamie Gritton fi++) { 6237cbf7213SJamie Gritton error = vfs_copyopt(opts, pr_flag_jailsys[fi].name, &jsys, 6247cbf7213SJamie Gritton sizeof(jsys)); 6257cbf7213SJamie Gritton if (error == ENOENT) 6267cbf7213SJamie Gritton continue; 6277cbf7213SJamie Gritton if (error != 0) 6287cbf7213SJamie Gritton goto done_free; 6297cbf7213SJamie Gritton switch (jsys) { 6307cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 6317cbf7213SJamie Gritton if (!pr_flag_jailsys[fi].disable) { 6327cbf7213SJamie Gritton error = EINVAL; 6337cbf7213SJamie Gritton goto done_free; 6347cbf7213SJamie Gritton } 6357cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].disable; 6367cbf7213SJamie Gritton break; 6377cbf7213SJamie Gritton case JAIL_SYS_NEW: 6387cbf7213SJamie Gritton pr_flags |= pr_flag_jailsys[fi].new; 6397cbf7213SJamie Gritton break; 6407cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 6417cbf7213SJamie Gritton break; 6427cbf7213SJamie Gritton default: 6437cbf7213SJamie Gritton error = EINVAL; 6447cbf7213SJamie Gritton goto done_free; 6457cbf7213SJamie Gritton } 6467cbf7213SJamie Gritton ch_flags |= 6477cbf7213SJamie Gritton pr_flag_jailsys[fi].new | pr_flag_jailsys[fi].disable; 6487cbf7213SJamie Gritton } 6494affa14cSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_UPDATE | JAIL_ATTACH)) == JAIL_CREATE 6504affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 6514affa14cSJamie Gritton error = EINVAL; 6524affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 6534affa14cSJamie Gritton goto done_errmsg; 6544affa14cSJamie Gritton } 655679e1390SJamie Gritton #ifdef VIMAGE 656679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 657679e1390SJamie Gritton error = EINVAL; 658679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 659679e1390SJamie Gritton goto done_errmsg; 660679e1390SJamie Gritton } 661679e1390SJamie Gritton #endif 6622b0d6f81SJamie Gritton #ifdef INET 6632b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 6642b0d6f81SJamie Gritton error = EINVAL; 6652b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 6662b0d6f81SJamie Gritton goto done_errmsg; 6672b0d6f81SJamie Gritton } 6682b0d6f81SJamie Gritton #endif 6692b0d6f81SJamie Gritton #ifdef INET6 6702b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 6712b0d6f81SJamie Gritton error = EINVAL; 6722b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 6732b0d6f81SJamie Gritton goto done_errmsg; 6742b0d6f81SJamie Gritton } 6752b0d6f81SJamie Gritton #endif 676b38ff370SJamie Gritton 6770304c731SJamie Gritton pr_allow = ch_allow = 0; 6780304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 6790304c731SJamie Gritton fi++) { 6800304c731SJamie Gritton vfs_flagopt(opts, pr_allow_names[fi], &pr_allow, 1 << fi); 6810304c731SJamie Gritton vfs_flagopt(opts, pr_allow_nonames[fi], &ch_allow, 1 << fi); 6820304c731SJamie Gritton } 6830304c731SJamie Gritton ch_allow |= pr_allow; 6840304c731SJamie Gritton 685b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 686b38ff370SJamie Gritton if (error == ENOENT) 687b38ff370SJamie Gritton name = NULL; 688b38ff370SJamie Gritton else if (error != 0) 689b38ff370SJamie Gritton goto done_free; 690b38ff370SJamie Gritton else { 691b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 692b38ff370SJamie Gritton error = EINVAL; 693b38ff370SJamie Gritton goto done_free; 694b38ff370SJamie Gritton } 695b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 696b38ff370SJamie Gritton error = ENAMETOOLONG; 697b38ff370SJamie Gritton goto done_free; 698b38ff370SJamie Gritton } 699b38ff370SJamie Gritton } 700b38ff370SJamie Gritton 701b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 702b38ff370SJamie Gritton if (error == ENOENT) 703b38ff370SJamie Gritton host = NULL; 704b38ff370SJamie Gritton else if (error != 0) 705b38ff370SJamie Gritton goto done_free; 706b38ff370SJamie Gritton else { 70776ca6f88SJamie Gritton ch_flags |= PR_HOST; 70876ca6f88SJamie Gritton pr_flags |= PR_HOST; 709b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 710b38ff370SJamie Gritton error = EINVAL; 711b38ff370SJamie Gritton goto done_free; 712b38ff370SJamie Gritton } 713b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 714b38ff370SJamie Gritton error = ENAMETOOLONG; 715b38ff370SJamie Gritton goto done_free; 716b38ff370SJamie Gritton } 717b38ff370SJamie Gritton } 718b38ff370SJamie Gritton 71976ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 72076ca6f88SJamie Gritton if (error == ENOENT) 72176ca6f88SJamie Gritton domain = NULL; 72276ca6f88SJamie Gritton else if (error != 0) 72376ca6f88SJamie Gritton goto done_free; 72476ca6f88SJamie Gritton else { 72576ca6f88SJamie Gritton ch_flags |= PR_HOST; 72676ca6f88SJamie Gritton pr_flags |= PR_HOST; 72776ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 72876ca6f88SJamie Gritton error = EINVAL; 72976ca6f88SJamie Gritton goto done_free; 73076ca6f88SJamie Gritton } 73176ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 73276ca6f88SJamie Gritton error = ENAMETOOLONG; 73376ca6f88SJamie Gritton goto done_free; 73476ca6f88SJamie Gritton } 73576ca6f88SJamie Gritton } 73676ca6f88SJamie Gritton 73776ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 73876ca6f88SJamie Gritton if (error == ENOENT) 73976ca6f88SJamie Gritton uuid = NULL; 74076ca6f88SJamie Gritton else if (error != 0) 74176ca6f88SJamie Gritton goto done_free; 74276ca6f88SJamie Gritton else { 74376ca6f88SJamie Gritton ch_flags |= PR_HOST; 74476ca6f88SJamie Gritton pr_flags |= PR_HOST; 74576ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 74676ca6f88SJamie Gritton error = EINVAL; 74776ca6f88SJamie Gritton goto done_free; 74876ca6f88SJamie Gritton } 74976ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 75076ca6f88SJamie Gritton error = ENAMETOOLONG; 75176ca6f88SJamie Gritton goto done_free; 75276ca6f88SJamie Gritton } 75376ca6f88SJamie Gritton } 75476ca6f88SJamie Gritton 755841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 756a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 75776ca6f88SJamie Gritton uint32_t hid32; 75876ca6f88SJamie Gritton 75976ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 76076ca6f88SJamie Gritton hid = hid32; 76176ca6f88SJamie Gritton } else 76276ca6f88SJamie Gritton #endif 76376ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 76476ca6f88SJamie Gritton if (error == ENOENT) 76576ca6f88SJamie Gritton gothid = 0; 76676ca6f88SJamie Gritton else if (error != 0) 76776ca6f88SJamie Gritton goto done_free; 76876ca6f88SJamie Gritton else { 76976ca6f88SJamie Gritton gothid = 1; 77076ca6f88SJamie Gritton ch_flags |= PR_HOST; 77176ca6f88SJamie Gritton pr_flags |= PR_HOST; 77276ca6f88SJamie Gritton } 77376ca6f88SJamie Gritton 774b38ff370SJamie Gritton #ifdef INET 775b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 776b38ff370SJamie Gritton if (error == ENOENT) 7777cbf7213SJamie Gritton ip4s = (pr_flags & PR_IP4_DISABLE) ? 0 : -1; 778b38ff370SJamie Gritton else if (error != 0) 779b38ff370SJamie Gritton goto done_free; 780b38ff370SJamie Gritton else if (ip4s & (sizeof(*ip4) - 1)) { 781b38ff370SJamie Gritton error = EINVAL; 782b38ff370SJamie Gritton goto done_free; 7830304c731SJamie Gritton } else { 7847cbf7213SJamie Gritton ch_flags |= PR_IP4_USER | PR_IP4_DISABLE; 7857cbf7213SJamie Gritton if (ip4s == 0) 7867cbf7213SJamie Gritton pr_flags |= PR_IP4_USER | PR_IP4_DISABLE; 7877cbf7213SJamie Gritton else { 7887cbf7213SJamie Gritton pr_flags = (pr_flags & ~PR_IP4_DISABLE) | PR_IP4_USER; 789b38ff370SJamie Gritton ip4s /= sizeof(*ip4); 790b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 791b38ff370SJamie Gritton error = EINVAL; 792b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 793b38ff370SJamie Gritton goto done_errmsg; 794b38ff370SJamie Gritton } 7952b0d6f81SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 796b38ff370SJamie Gritton bcopy(op, ip4, ip4s * sizeof(*ip4)); 797b38ff370SJamie Gritton /* 7980304c731SJamie Gritton * IP addresses are all sorted but ip[0] to preserve 7990304c731SJamie Gritton * the primary IP address as given from userland. 8000304c731SJamie Gritton * This special IP is used for unbound outgoing 801bef916f9SBjoern A. Zeeb * connections as well for "loopback" traffic in case 802bef916f9SBjoern A. Zeeb * source address selection cannot find any more fitting 803bef916f9SBjoern A. Zeeb * address to connect from. 804b38ff370SJamie Gritton */ 805b38ff370SJamie Gritton if (ip4s > 1) 806b38ff370SJamie Gritton qsort(ip4 + 1, ip4s - 1, sizeof(*ip4), qcmp_v4); 807b38ff370SJamie Gritton /* 8080304c731SJamie Gritton * Check for duplicate addresses and do some simple 8090304c731SJamie Gritton * zero and broadcast checks. If users give other bogus 8100304c731SJamie Gritton * addresses it is their problem. 811b38ff370SJamie Gritton * 8120304c731SJamie Gritton * We do not have to care about byte order for these 8130304c731SJamie Gritton * checks so we will do them in NBO. 814b38ff370SJamie Gritton */ 815b38ff370SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 816b38ff370SJamie Gritton if (ip4[ii].s_addr == INADDR_ANY || 817b38ff370SJamie Gritton ip4[ii].s_addr == INADDR_BROADCAST) { 818b38ff370SJamie Gritton error = EINVAL; 819b38ff370SJamie Gritton goto done_free; 820b38ff370SJamie Gritton } 821b38ff370SJamie Gritton if ((ii+1) < ip4s && 822b38ff370SJamie Gritton (ip4[0].s_addr == ip4[ii+1].s_addr || 823b38ff370SJamie Gritton ip4[ii].s_addr == ip4[ii+1].s_addr)) { 824b38ff370SJamie Gritton error = EINVAL; 825b38ff370SJamie Gritton goto done_free; 826b38ff370SJamie Gritton } 827b38ff370SJamie Gritton } 828b38ff370SJamie Gritton } 8290304c731SJamie Gritton } 830b38ff370SJamie Gritton #endif 831b38ff370SJamie Gritton 832b38ff370SJamie Gritton #ifdef INET6 833b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 834b38ff370SJamie Gritton if (error == ENOENT) 8357cbf7213SJamie Gritton ip6s = (pr_flags & PR_IP6_DISABLE) ? 0 : -1; 836b38ff370SJamie Gritton else if (error != 0) 837b38ff370SJamie Gritton goto done_free; 838b38ff370SJamie Gritton else if (ip6s & (sizeof(*ip6) - 1)) { 839b38ff370SJamie Gritton error = EINVAL; 840b38ff370SJamie Gritton goto done_free; 8410304c731SJamie Gritton } else { 8427cbf7213SJamie Gritton ch_flags |= PR_IP6_USER | PR_IP6_DISABLE; 8437cbf7213SJamie Gritton if (ip6s == 0) 8447cbf7213SJamie Gritton pr_flags |= PR_IP6_USER | PR_IP6_DISABLE; 8457cbf7213SJamie Gritton else { 8467cbf7213SJamie Gritton pr_flags = (pr_flags & ~PR_IP6_DISABLE) | PR_IP6_USER; 847b38ff370SJamie Gritton ip6s /= sizeof(*ip6); 848b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 849b38ff370SJamie Gritton error = EINVAL; 850b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 851b38ff370SJamie Gritton goto done_errmsg; 852b38ff370SJamie Gritton } 8532b0d6f81SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 854b38ff370SJamie Gritton bcopy(op, ip6, ip6s * sizeof(*ip6)); 855b38ff370SJamie Gritton if (ip6s > 1) 856b38ff370SJamie Gritton qsort(ip6 + 1, ip6s - 1, sizeof(*ip6), qcmp_v6); 857b38ff370SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 8580304c731SJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(&ip6[ii])) { 859b38ff370SJamie Gritton error = EINVAL; 860b38ff370SJamie Gritton goto done_free; 861b38ff370SJamie Gritton } 862b38ff370SJamie Gritton if ((ii+1) < ip6s && 863b38ff370SJamie Gritton (IN6_ARE_ADDR_EQUAL(&ip6[0], &ip6[ii+1]) || 864b38ff370SJamie Gritton IN6_ARE_ADDR_EQUAL(&ip6[ii], &ip6[ii+1]))) 865b38ff370SJamie Gritton { 866b38ff370SJamie Gritton error = EINVAL; 867b38ff370SJamie Gritton goto done_free; 868b38ff370SJamie Gritton } 869b38ff370SJamie Gritton } 870b38ff370SJamie Gritton } 8710304c731SJamie Gritton } 872b38ff370SJamie Gritton #endif 873b38ff370SJamie Gritton 874bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 875bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 876bdfc8cc4SJamie Gritton error = EINVAL; 877bdfc8cc4SJamie Gritton vfs_opterror(opts, 878bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 879bdfc8cc4SJamie Gritton goto done_errmsg; 880bdfc8cc4SJamie Gritton } 881bdfc8cc4SJamie Gritton #endif 882bdfc8cc4SJamie Gritton 883b38ff370SJamie Gritton root = NULL; 884b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 885b38ff370SJamie Gritton if (error == ENOENT) 886b38ff370SJamie Gritton path = NULL; 887b38ff370SJamie Gritton else if (error != 0) 888b38ff370SJamie Gritton goto done_free; 889b38ff370SJamie Gritton else { 890b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 891b38ff370SJamie Gritton error = EINVAL; 892b38ff370SJamie Gritton vfs_opterror(opts, 893b38ff370SJamie Gritton "path cannot be changed after creation"); 894b38ff370SJamie Gritton goto done_errmsg; 895b38ff370SJamie Gritton } 896b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 897b38ff370SJamie Gritton error = EINVAL; 898b38ff370SJamie Gritton goto done_free; 899b38ff370SJamie Gritton } 900b38ff370SJamie Gritton if (len < 2 || (len == 2 && path[0] == '/')) 901b38ff370SJamie Gritton path = NULL; 902b38ff370SJamie Gritton else { 9030304c731SJamie Gritton /* Leave room for a real-root full pathname. */ 9040304c731SJamie Gritton if (len + (path[0] == '/' && strcmp(mypr->pr_path, "/") 9050304c731SJamie Gritton ? strlen(mypr->pr_path) : 0) > MAXPATHLEN) { 9060304c731SJamie Gritton error = ENAMETOOLONG; 9070304c731SJamie Gritton goto done_free; 9080304c731SJamie Gritton } 909b38ff370SJamie Gritton NDINIT(&nd, LOOKUP, MPSAFE | FOLLOW, UIO_SYSSPACE, 910b38ff370SJamie Gritton path, td); 911b38ff370SJamie Gritton error = namei(&nd); 912b38ff370SJamie Gritton if (error) 913b38ff370SJamie Gritton goto done_free; 914b38ff370SJamie Gritton vfslocked = NDHASGIANT(&nd); 915b38ff370SJamie Gritton root = nd.ni_vp; 916b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 917b38ff370SJamie Gritton if (root->v_type != VDIR) { 918b38ff370SJamie Gritton error = ENOTDIR; 919b38ff370SJamie Gritton vrele(root); 920b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 921b38ff370SJamie Gritton goto done_free; 922b38ff370SJamie Gritton } 923b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 924b38ff370SJamie Gritton } 925b38ff370SJamie Gritton } 926b38ff370SJamie Gritton 927b38ff370SJamie Gritton /* 928b38ff370SJamie Gritton * Grab the allprison lock before letting modules check their 929b38ff370SJamie Gritton * parameters. Once we have it, do not let go so we'll have a 930b38ff370SJamie Gritton * consistent view of the OSD list. 931b38ff370SJamie Gritton */ 932b38ff370SJamie Gritton sx_xlock(&allprison_lock); 933b38ff370SJamie Gritton error = osd_jail_call(NULL, PR_METHOD_CHECK, opts); 934b38ff370SJamie Gritton if (error) 935b38ff370SJamie Gritton goto done_unlock_list; 936b38ff370SJamie Gritton 937b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 938b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 939b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 940b38ff370SJamie Gritton error = EINVAL; 941b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 942b38ff370SJamie Gritton goto done_unlock_list; 943b38ff370SJamie Gritton } 944b38ff370SJamie Gritton } 945b38ff370SJamie Gritton 946b38ff370SJamie Gritton /* 947b38ff370SJamie Gritton * See if we are creating a new record or updating an existing one. 948b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 949b38ff370SJamie Gritton */ 950b38ff370SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 951b38ff370SJamie Gritton if (!cuflags) { 952b38ff370SJamie Gritton error = EINVAL; 953b38ff370SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 954b38ff370SJamie Gritton goto done_unlock_list; 955b38ff370SJamie Gritton } 956b38ff370SJamie Gritton pr = NULL; 957babbbb9cSJamie Gritton namelc = NULL; 958babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 959babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 960babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 961babbbb9cSJamie Gritton if (*p != '\0') 962babbbb9cSJamie Gritton jid = 0; 963babbbb9cSJamie Gritton } 964b38ff370SJamie Gritton if (jid != 0) { 9650304c731SJamie Gritton /* 9660304c731SJamie Gritton * See if a requested jid already exists. There is an 9670304c731SJamie Gritton * information leak here if the jid exists but is not within 9680304c731SJamie Gritton * the caller's jail hierarchy. Jail creators will get EEXIST 9690304c731SJamie Gritton * even though they cannot see the jail, and CREATE | UPDATE 9700304c731SJamie Gritton * will return ENOENT which is not normally a valid error. 9710304c731SJamie Gritton */ 972b38ff370SJamie Gritton if (jid < 0) { 973b38ff370SJamie Gritton error = EINVAL; 974b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 975b38ff370SJamie Gritton goto done_unlock_list; 976b38ff370SJamie Gritton } 977b38ff370SJamie Gritton pr = prison_find(jid); 978b38ff370SJamie Gritton if (pr != NULL) { 9790304c731SJamie Gritton ppr = pr->pr_parent; 980b38ff370SJamie Gritton /* Create: jid must not exist. */ 981b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 982b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 983b38ff370SJamie Gritton error = EEXIST; 984b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 985b38ff370SJamie Gritton jid); 986b38ff370SJamie Gritton goto done_unlock_list; 987b38ff370SJamie Gritton } 9880304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 9890304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 9900304c731SJamie Gritton pr = NULL; 9910304c731SJamie Gritton } else if (pr->pr_uref == 0) { 992b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 993b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 994b38ff370SJamie Gritton error = ENOENT; 995b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 996b38ff370SJamie Gritton jid); 997b38ff370SJamie Gritton goto done_unlock_list; 998b38ff370SJamie Gritton } else if ((flags & JAIL_ATTACH) || 999b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1000b38ff370SJamie Gritton /* 1001b38ff370SJamie Gritton * A dying jail might be resurrected 1002b38ff370SJamie Gritton * (via attach or persist), but first 1003b38ff370SJamie Gritton * it must determine if another jail 1004b38ff370SJamie Gritton * has claimed its name. Accomplish 1005b38ff370SJamie Gritton * this by implicitly re-setting the 1006b38ff370SJamie Gritton * name. 1007b38ff370SJamie Gritton */ 1008b38ff370SJamie Gritton if (name == NULL) 10090304c731SJamie Gritton name = prison_name(mypr, pr); 1010b38ff370SJamie Gritton } 1011b38ff370SJamie Gritton } 1012b38ff370SJamie Gritton } 1013b38ff370SJamie Gritton if (pr == NULL) { 1014b38ff370SJamie Gritton /* Update: jid must exist. */ 1015b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1016b38ff370SJamie Gritton error = ENOENT; 1017b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1018b38ff370SJamie Gritton goto done_unlock_list; 1019b38ff370SJamie Gritton } 1020b38ff370SJamie Gritton } 1021b38ff370SJamie Gritton } 1022b38ff370SJamie Gritton /* 1023b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1024b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1025b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1026b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1027b38ff370SJamie Gritton * because that is the jail being updated). 1028b38ff370SJamie Gritton */ 1029b38ff370SJamie Gritton if (name != NULL) { 1030babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1031babbbb9cSJamie Gritton if (namelc == NULL) 1032babbbb9cSJamie Gritton namelc = name; 1033babbbb9cSJamie Gritton else { 10340304c731SJamie Gritton /* 10350304c731SJamie Gritton * This is a hierarchical name. Split it into the 10360304c731SJamie Gritton * parent and child names, and make sure the parent 10370304c731SJamie Gritton * exists or matches an already found jail. 10380304c731SJamie Gritton */ 1039babbbb9cSJamie Gritton *namelc = '\0'; 10400304c731SJamie Gritton if (pr != NULL) { 1041babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1042babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 10430304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 10440304c731SJamie Gritton error = EINVAL; 10450304c731SJamie Gritton vfs_opterror(opts, 10460304c731SJamie Gritton "cannot change jail's parent"); 10470304c731SJamie Gritton goto done_unlock_list; 10480304c731SJamie Gritton } 10490304c731SJamie Gritton } else { 10500304c731SJamie Gritton ppr = prison_find_name(mypr, name); 10510304c731SJamie Gritton if (ppr == NULL) { 10520304c731SJamie Gritton error = ENOENT; 10530304c731SJamie Gritton vfs_opterror(opts, 10540304c731SJamie Gritton "jail \"%s\" not found", name); 10550304c731SJamie Gritton goto done_unlock_list; 10560304c731SJamie Gritton } 10570304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 10580304c731SJamie Gritton } 1059babbbb9cSJamie Gritton name = ++namelc; 10600304c731SJamie Gritton } 1061b38ff370SJamie Gritton if (name[0] != '\0') { 10620304c731SJamie Gritton namelen = 10630304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1064b38ff370SJamie Gritton name_again: 10650304c731SJamie Gritton deadpr = NULL; 10660304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 1067b38ff370SJamie Gritton if (tpr != pr && tpr->pr_ref > 0 && 10680304c731SJamie Gritton !strcmp(tpr->pr_name + namelen, name)) { 1069b38ff370SJamie Gritton if (pr == NULL && 1070b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1071b38ff370SJamie Gritton mtx_lock(&tpr->pr_mtx); 1072b38ff370SJamie Gritton if (tpr->pr_ref > 0) { 1073b38ff370SJamie Gritton /* 1074b38ff370SJamie Gritton * Use this jail 1075b38ff370SJamie Gritton * for updates. 1076b38ff370SJamie Gritton */ 1077b38ff370SJamie Gritton if (tpr->pr_uref > 0) { 1078b38ff370SJamie Gritton pr = tpr; 1079b38ff370SJamie Gritton break; 1080b38ff370SJamie Gritton } 1081b38ff370SJamie Gritton deadpr = tpr; 1082b38ff370SJamie Gritton } 1083b38ff370SJamie Gritton mtx_unlock(&tpr->pr_mtx); 1084b38ff370SJamie Gritton } else if (tpr->pr_uref > 0) { 1085b38ff370SJamie Gritton /* 1086b38ff370SJamie Gritton * Create, or update(jid): 1087b38ff370SJamie Gritton * name must not exist in an 10880304c731SJamie Gritton * active sibling jail. 1089b38ff370SJamie Gritton */ 1090b38ff370SJamie Gritton error = EEXIST; 1091b38ff370SJamie Gritton if (pr != NULL) 1092b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1093b38ff370SJamie Gritton vfs_opterror(opts, 1094b38ff370SJamie Gritton "jail \"%s\" already exists", 1095b38ff370SJamie Gritton name); 1096b38ff370SJamie Gritton goto done_unlock_list; 1097b38ff370SJamie Gritton } 1098b38ff370SJamie Gritton } 1099b38ff370SJamie Gritton } 1100b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1101b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1102b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1103b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 1104b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 1105b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 1106b38ff370SJamie Gritton goto name_again; 1107b38ff370SJamie Gritton } 1108b38ff370SJamie Gritton pr = deadpr; 1109b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1110b38ff370SJamie Gritton error = ENOENT; 1111b38ff370SJamie Gritton vfs_opterror(opts, 1112b38ff370SJamie Gritton "jail \"%s\" is dying", name); 1113b38ff370SJamie Gritton goto done_unlock_list; 1114b38ff370SJamie Gritton } 1115b38ff370SJamie Gritton } 1116b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1117b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1118b38ff370SJamie Gritton error = ENOENT; 1119b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1120b38ff370SJamie Gritton name); 1121b38ff370SJamie Gritton goto done_unlock_list; 1122b38ff370SJamie Gritton } 1123b38ff370SJamie Gritton } 1124b38ff370SJamie Gritton } 1125b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1126b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1127b38ff370SJamie Gritton error = ENOENT; 1128b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 1129b38ff370SJamie Gritton goto done_unlock_list; 1130b38ff370SJamie Gritton } 1131b38ff370SJamie Gritton 1132b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 1133b38ff370SJamie Gritton if (pr == NULL) { 1134b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1135b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1136b97457e2SJamie Gritton error = EPERM; 1137b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 1138b97457e2SJamie Gritton goto done_unlock_list; 1139b97457e2SJamie Gritton } 1140b38ff370SJamie Gritton created = 1; 11410304c731SJamie Gritton mtx_lock(&ppr->pr_mtx); 11420304c731SJamie Gritton if (ppr->pr_ref == 0 || (ppr->pr_flags & PR_REMOVE)) { 11430304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 11440304c731SJamie Gritton error = ENOENT; 11450304c731SJamie Gritton vfs_opterror(opts, "parent jail went away!"); 11460304c731SJamie Gritton goto done_unlock_list; 11470304c731SJamie Gritton } 11480304c731SJamie Gritton ppr->pr_ref++; 11490304c731SJamie Gritton ppr->pr_uref++; 11500304c731SJamie Gritton mtx_unlock(&ppr->pr_mtx); 1151b38ff370SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 1152b38ff370SJamie Gritton if (jid == 0) { 1153b38ff370SJamie Gritton /* Find the next free jid. */ 1154b38ff370SJamie Gritton jid = lastprid + 1; 1155b38ff370SJamie Gritton findnext: 1156b38ff370SJamie Gritton if (jid == JAIL_MAX) 1157b38ff370SJamie Gritton jid = 1; 1158b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) { 1159b38ff370SJamie Gritton if (tpr->pr_id < jid) 1160b38ff370SJamie Gritton continue; 1161b38ff370SJamie Gritton if (tpr->pr_id > jid || tpr->pr_ref == 0) { 1162b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1163b38ff370SJamie Gritton break; 1164b38ff370SJamie Gritton } 1165b38ff370SJamie Gritton if (jid == lastprid) { 1166b38ff370SJamie Gritton error = EAGAIN; 1167b38ff370SJamie Gritton vfs_opterror(opts, 1168b38ff370SJamie Gritton "no available jail IDs"); 1169b38ff370SJamie Gritton free(pr, M_PRISON); 11700304c731SJamie Gritton prison_deref(ppr, PD_DEREF | 11710304c731SJamie Gritton PD_DEUREF | PD_LIST_XLOCKED); 11720304c731SJamie Gritton goto done_releroot; 1173b38ff370SJamie Gritton } 1174b38ff370SJamie Gritton jid++; 1175b38ff370SJamie Gritton goto findnext; 1176b38ff370SJamie Gritton } 1177b38ff370SJamie Gritton lastprid = jid; 1178b38ff370SJamie Gritton } else { 1179b38ff370SJamie Gritton /* 1180b38ff370SJamie Gritton * The jail already has a jid (that did not yet exist), 1181b38ff370SJamie Gritton * so just find where to insert it. 1182b38ff370SJamie Gritton */ 1183b38ff370SJamie Gritton TAILQ_FOREACH(tpr, &allprison, pr_list) 1184b38ff370SJamie Gritton if (tpr->pr_id >= jid) { 1185b38ff370SJamie Gritton TAILQ_INSERT_BEFORE(tpr, pr, pr_list); 1186b38ff370SJamie Gritton break; 1187b38ff370SJamie Gritton } 1188b38ff370SJamie Gritton } 1189b38ff370SJamie Gritton if (tpr == NULL) 1190b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 11910304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 11920304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1193b97457e2SJamie Gritton tpr->pr_childcount++; 1194b38ff370SJamie Gritton 11950304c731SJamie Gritton pr->pr_parent = ppr; 1196b38ff370SJamie Gritton pr->pr_id = jid; 11970304c731SJamie Gritton 11980304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1199b38ff370SJamie Gritton if (name == NULL) 1200b38ff370SJamie Gritton name = ""; 1201b38ff370SJamie Gritton if (path == NULL) { 1202b38ff370SJamie Gritton path = "/"; 12030304c731SJamie Gritton root = mypr->pr_root; 1204b38ff370SJamie Gritton vref(root); 1205b38ff370SJamie Gritton } 12068986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 12078986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1208bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1209bdfc8cc4SJamie Gritton #ifdef VIMAGE 1210bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1211bdfc8cc4SJamie Gritton #endif 1212bdfc8cc4SJamie Gritton { 12130304c731SJamie Gritton #ifdef INET 12142b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 12152b0d6f81SJamie Gritton pr->pr_flags |= 12162b0d6f81SJamie Gritton PR_IP4 | PR_IP4_USER | PR_IP4_DISABLE; 12172b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 12182b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 12192b0d6f81SJamie Gritton if (ppr->pr_ip4 != NULL) { 12202b0d6f81SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 12212b0d6f81SJamie Gritton pr->pr_ip4 = malloc(pr->pr_ip4s * 12222b0d6f81SJamie Gritton sizeof(struct in_addr), M_PRISON, 12232b0d6f81SJamie Gritton M_WAITOK); 12242b0d6f81SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 12252b0d6f81SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 12262b0d6f81SJamie Gritton } 12272b0d6f81SJamie Gritton } 12280304c731SJamie Gritton #endif 12290304c731SJamie Gritton #ifdef INET6 12302b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 12312b0d6f81SJamie Gritton pr->pr_flags |= 12322b0d6f81SJamie Gritton PR_IP6 | PR_IP6_USER | PR_IP6_DISABLE; 12332b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 12342b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 12352b0d6f81SJamie Gritton if (ppr->pr_ip6 != NULL) { 12362b0d6f81SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 12372b0d6f81SJamie Gritton pr->pr_ip6 = malloc(pr->pr_ip6s * 12382b0d6f81SJamie Gritton sizeof(struct in6_addr), M_PRISON, 12392b0d6f81SJamie Gritton M_WAITOK); 12402b0d6f81SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 12412b0d6f81SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 12422b0d6f81SJamie Gritton } 12432b0d6f81SJamie Gritton } 12440304c731SJamie Gritton #endif 1245bdfc8cc4SJamie Gritton } 1246bdfc8cc4SJamie Gritton #endif 1247592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1248592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1249592bcae8SBjoern A. Zeeb 12500304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 12510304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 125242bebd82SJamie Gritton pr->pr_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 1253b38ff370SJamie Gritton 12540304c731SJamie Gritton LIST_INIT(&pr->pr_children); 12550304c731SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 1256b38ff370SJamie Gritton 1257679e1390SJamie Gritton #ifdef VIMAGE 1258679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1259679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1260679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1261679e1390SJamie Gritton #endif 1262b38ff370SJamie Gritton /* 1263b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 1264b38ff370SJamie Gritton * Unlike other initial settings, this may return an erorr. 1265b38ff370SJamie Gritton */ 12660304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 1267b38ff370SJamie Gritton if (error) { 1268b38ff370SJamie Gritton prison_deref(pr, PD_LIST_XLOCKED); 1269b38ff370SJamie Gritton goto done_releroot; 1270b38ff370SJamie Gritton } 1271b38ff370SJamie Gritton 1272b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1273b38ff370SJamie Gritton /* 1274b38ff370SJamie Gritton * New prisons do not yet have a reference, because we do not 1275b38ff370SJamie Gritton * want other to see the incomplete prison once the 1276b38ff370SJamie Gritton * allprison_lock is downgraded. 1277b38ff370SJamie Gritton */ 1278b38ff370SJamie Gritton } else { 1279b38ff370SJamie Gritton created = 0; 12802b0d6f81SJamie Gritton /* 12812b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 12822b0d6f81SJamie Gritton * continue to exist for the duration of the call. 12832b0d6f81SJamie Gritton */ 12842b0d6f81SJamie Gritton pr->pr_ref++; 1285bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1286bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1287bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1288bdfc8cc4SJamie Gritton error = EINVAL; 1289bdfc8cc4SJamie Gritton vfs_opterror(opts, 1290bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1291bdfc8cc4SJamie Gritton goto done_deref_locked; 1292bdfc8cc4SJamie Gritton } 1293bdfc8cc4SJamie Gritton #endif 12942b0d6f81SJamie Gritton #ifdef INET 12952b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 12962b0d6f81SJamie Gritton error = EINVAL; 12972b0d6f81SJamie Gritton vfs_opterror(opts, 12982b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 12992b0d6f81SJamie Gritton goto done_deref_locked; 13002b0d6f81SJamie Gritton } 13012b0d6f81SJamie Gritton #endif 13022b0d6f81SJamie Gritton #ifdef INET6 13032b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 13042b0d6f81SJamie Gritton error = EINVAL; 13052b0d6f81SJamie Gritton vfs_opterror(opts, 13062b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 13072b0d6f81SJamie Gritton goto done_deref_locked; 13082b0d6f81SJamie Gritton } 13092b0d6f81SJamie Gritton #endif 1310b38ff370SJamie Gritton } 1311b38ff370SJamie Gritton 1312b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 13130304c731SJamie Gritton if (gotslevel) { 13140304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 13150304c731SJamie Gritton error = EPERM; 13160304c731SJamie Gritton goto done_deref_locked; 13170304c731SJamie Gritton } 13180304c731SJamie Gritton } 1319b97457e2SJamie Gritton if (gotchildmax) { 1320b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1321b97457e2SJamie Gritton error = EPERM; 1322b97457e2SJamie Gritton goto done_deref_locked; 1323b97457e2SJamie Gritton } 1324b97457e2SJamie Gritton } 13250304c731SJamie Gritton if (gotenforce) { 13260304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 13270304c731SJamie Gritton error = EPERM; 13280304c731SJamie Gritton goto done_deref_locked; 13290304c731SJamie Gritton } 13300304c731SJamie Gritton } 1331b38ff370SJamie Gritton #ifdef INET 13322b0d6f81SJamie Gritton if (ip4s > 0) { 13330304c731SJamie Gritton if (ppr->pr_flags & PR_IP4) { 13340304c731SJamie Gritton /* 13350304c731SJamie Gritton * Make sure the new set of IP addresses is a 13360304c731SJamie Gritton * subset of the parent's list. Don't worry 13370304c731SJamie Gritton * about the parent being unlocked, as any 13380304c731SJamie Gritton * setting is done with allprison_lock held. 13390304c731SJamie Gritton */ 13400304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 13412b0d6f81SJamie Gritton if (ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 13420304c731SJamie Gritton break; 13430304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13440304c731SJamie Gritton error = EPERM; 13450304c731SJamie Gritton goto done_deref_locked; 13460304c731SJamie Gritton } 13470304c731SJamie Gritton if (ip4s > 1) { 13480304c731SJamie Gritton for (ii = ij = 1; ii < ip4s; ii++) { 13490304c731SJamie Gritton if (ip4[ii].s_addr == 13500304c731SJamie Gritton ppr->pr_ip4[0].s_addr) 1351b38ff370SJamie Gritton continue; 13520304c731SJamie Gritton for (; ij < ppr->pr_ip4s; ij++) 13530304c731SJamie Gritton if (ip4[ii].s_addr == 13540304c731SJamie Gritton ppr->pr_ip4[ij].s_addr) 13550304c731SJamie Gritton break; 13560304c731SJamie Gritton if (ij == ppr->pr_ip4s) 13570304c731SJamie Gritton break; 13580304c731SJamie Gritton } 13590304c731SJamie Gritton if (ij == ppr->pr_ip4s) { 13600304c731SJamie Gritton error = EPERM; 13610304c731SJamie Gritton goto done_deref_locked; 13620304c731SJamie Gritton } 13630304c731SJamie Gritton } 13640304c731SJamie Gritton } 13650304c731SJamie Gritton /* 13660304c731SJamie Gritton * Check for conflicting IP addresses. We permit them 13670304c731SJamie Gritton * if there is no more than one IP on each jail. If 13680304c731SJamie Gritton * there is a duplicate on a jail with more than one 13690304c731SJamie Gritton * IP stop checking and return error. 13700304c731SJamie Gritton */ 1371bdfc8cc4SJamie Gritton tppr = ppr; 1372bdfc8cc4SJamie Gritton #ifdef VIMAGE 1373bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1374bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1375bdfc8cc4SJamie Gritton break; 1376bdfc8cc4SJamie Gritton #endif 1377bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1378bdfc8cc4SJamie Gritton if (tpr == pr || 1379bdfc8cc4SJamie Gritton #ifdef VIMAGE 13802b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1381bdfc8cc4SJamie Gritton #endif 1382bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 13830304c731SJamie Gritton descend = 0; 13840304c731SJamie Gritton continue; 13850304c731SJamie Gritton } 13860304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP4_USER)) 13870304c731SJamie Gritton continue; 13880304c731SJamie Gritton descend = 0; 13890304c731SJamie Gritton if (tpr->pr_ip4 == NULL || 13900304c731SJamie Gritton (ip4s == 1 && tpr->pr_ip4s == 1)) 13910304c731SJamie Gritton continue; 13920304c731SJamie Gritton for (ii = 0; ii < ip4s; ii++) { 13932b0d6f81SJamie Gritton if (_prison_check_ip4(tpr, &ip4[ii]) == 0) { 13940304c731SJamie Gritton error = EADDRINUSE; 1395b38ff370SJamie Gritton vfs_opterror(opts, 1396b38ff370SJamie Gritton "IPv4 addresses clash"); 1397b38ff370SJamie Gritton goto done_deref_locked; 1398b38ff370SJamie Gritton } 13990304c731SJamie Gritton } 14000304c731SJamie Gritton } 14010304c731SJamie Gritton } 1402b38ff370SJamie Gritton #endif 1403b38ff370SJamie Gritton #ifdef INET6 14042b0d6f81SJamie Gritton if (ip6s > 0) { 14050304c731SJamie Gritton if (ppr->pr_flags & PR_IP6) { 14060304c731SJamie Gritton /* 14070304c731SJamie Gritton * Make sure the new set of IP addresses is a 14080304c731SJamie Gritton * subset of the parent's list. 14090304c731SJamie Gritton */ 14100304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 14110304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[0], 14120304c731SJamie Gritton &ppr->pr_ip6[ij])) 14130304c731SJamie Gritton break; 14140304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14150304c731SJamie Gritton error = EPERM; 14160304c731SJamie Gritton goto done_deref_locked; 14170304c731SJamie Gritton } 14180304c731SJamie Gritton if (ip6s > 1) { 14190304c731SJamie Gritton for (ii = ij = 1; ii < ip6s; ii++) { 14200304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&ip6[ii], 14210304c731SJamie Gritton &ppr->pr_ip6[0])) 14220304c731SJamie Gritton continue; 14230304c731SJamie Gritton for (; ij < ppr->pr_ip6s; ij++) 14240304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL( 14252b0d6f81SJamie Gritton &ip6[ii], &ppr->pr_ip6[ij])) 14260304c731SJamie Gritton break; 14270304c731SJamie Gritton if (ij == ppr->pr_ip6s) 14280304c731SJamie Gritton break; 14290304c731SJamie Gritton } 14300304c731SJamie Gritton if (ij == ppr->pr_ip6s) { 14310304c731SJamie Gritton error = EPERM; 14320304c731SJamie Gritton goto done_deref_locked; 14330304c731SJamie Gritton } 14340304c731SJamie Gritton } 14350304c731SJamie Gritton } 14360304c731SJamie Gritton /* Check for conflicting IP addresses. */ 1437bdfc8cc4SJamie Gritton tppr = ppr; 1438bdfc8cc4SJamie Gritton #ifdef VIMAGE 1439bdfc8cc4SJamie Gritton for (; tppr != &prison0; tppr = tppr->pr_parent) 1440bdfc8cc4SJamie Gritton if (tppr->pr_flags & PR_VNET) 1441bdfc8cc4SJamie Gritton break; 1442bdfc8cc4SJamie Gritton #endif 1443bdfc8cc4SJamie Gritton FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 1444bdfc8cc4SJamie Gritton if (tpr == pr || 1445bdfc8cc4SJamie Gritton #ifdef VIMAGE 14462b0d6f81SJamie Gritton (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 1447bdfc8cc4SJamie Gritton #endif 1448bdfc8cc4SJamie Gritton tpr->pr_uref == 0) { 14490304c731SJamie Gritton descend = 0; 14500304c731SJamie Gritton continue; 14510304c731SJamie Gritton } 14520304c731SJamie Gritton if (!(tpr->pr_flags & PR_IP6_USER)) 14530304c731SJamie Gritton continue; 14540304c731SJamie Gritton descend = 0; 14550304c731SJamie Gritton if (tpr->pr_ip6 == NULL || 14560304c731SJamie Gritton (ip6s == 1 && tpr->pr_ip6s == 1)) 14570304c731SJamie Gritton continue; 14580304c731SJamie Gritton for (ii = 0; ii < ip6s; ii++) { 14592b0d6f81SJamie Gritton if (_prison_check_ip6(tpr, &ip6[ii]) == 0) { 14600304c731SJamie Gritton error = EADDRINUSE; 1461b38ff370SJamie Gritton vfs_opterror(opts, 1462b38ff370SJamie Gritton "IPv6 addresses clash"); 1463b38ff370SJamie Gritton goto done_deref_locked; 1464b38ff370SJamie Gritton } 14650304c731SJamie Gritton } 14660304c731SJamie Gritton } 14670304c731SJamie Gritton } 1468b38ff370SJamie Gritton #endif 14690304c731SJamie Gritton onamelen = namelen = 0; 14700304c731SJamie Gritton if (name != NULL) { 1471b38ff370SJamie Gritton /* Give a default name of the jid. */ 1472b38ff370SJamie Gritton if (name[0] == '\0') 1473b38ff370SJamie Gritton snprintf(name = numbuf, sizeof(numbuf), "%d", jid); 1474babbbb9cSJamie Gritton else if (*namelc == '0' || (strtoul(namelc, &p, 10) != jid && 1475babbbb9cSJamie Gritton *p == '\0')) { 1476b38ff370SJamie Gritton error = EINVAL; 1477babbbb9cSJamie Gritton vfs_opterror(opts, 1478babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 14790304c731SJamie Gritton goto done_deref_locked; 1480b38ff370SJamie Gritton } 1481b38ff370SJamie Gritton /* 14820304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 14830304c731SJamie Gritton * children. 1484b38ff370SJamie Gritton */ 14850304c731SJamie Gritton onamelen = strlen(pr->pr_name); 14860304c731SJamie Gritton namelen = strlen(name); 14870304c731SJamie Gritton if (strlen(ppr->pr_name) + namelen + 2 > sizeof(pr->pr_name)) { 14880304c731SJamie Gritton error = ENAMETOOLONG; 14890304c731SJamie Gritton goto done_deref_locked; 14900304c731SJamie Gritton } 14910304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 14920304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 14930304c731SJamie Gritton sizeof(pr->pr_name)) { 14940304c731SJamie Gritton error = ENAMETOOLONG; 14950304c731SJamie Gritton goto done_deref_locked; 14960304c731SJamie Gritton } 14970304c731SJamie Gritton } 14980304c731SJamie Gritton } 14990304c731SJamie Gritton if (pr_allow & ~ppr->pr_allow) { 15000304c731SJamie Gritton error = EPERM; 15010304c731SJamie Gritton goto done_deref_locked; 1502b38ff370SJamie Gritton } 1503b38ff370SJamie Gritton 1504b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1505b38ff370SJamie Gritton #ifdef INET 15060304c731SJamie Gritton redo_ip4 = 0; 15070304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 15080304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1509b38ff370SJamie Gritton free(pr->pr_ip4, M_PRISON); 15100304c731SJamie Gritton pr->pr_ip4s = ip4s; 1511b38ff370SJamie Gritton pr->pr_ip4 = ip4; 1512b38ff370SJamie Gritton ip4 = NULL; 15130304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1514bdfc8cc4SJamie Gritton #ifdef VIMAGE 1515bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1516bdfc8cc4SJamie Gritton descend = 0; 1517bdfc8cc4SJamie Gritton continue; 1518bdfc8cc4SJamie Gritton } 1519bdfc8cc4SJamie Gritton #endif 15200304c731SJamie Gritton if (prison_restrict_ip4(tpr, NULL)) { 15210304c731SJamie Gritton redo_ip4 = 1; 15220304c731SJamie Gritton descend = 0; 15230304c731SJamie Gritton } 15240304c731SJamie Gritton } 15250304c731SJamie Gritton } 1526b38ff370SJamie Gritton #endif 1527b38ff370SJamie Gritton #ifdef INET6 15280304c731SJamie Gritton redo_ip6 = 0; 15290304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 15300304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1531b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 15320304c731SJamie Gritton pr->pr_ip6s = ip6s; 1533b38ff370SJamie Gritton pr->pr_ip6 = ip6; 1534b38ff370SJamie Gritton ip6 = NULL; 15350304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1536bdfc8cc4SJamie Gritton #ifdef VIMAGE 1537bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1538bdfc8cc4SJamie Gritton descend = 0; 1539bdfc8cc4SJamie Gritton continue; 1540bdfc8cc4SJamie Gritton } 1541bdfc8cc4SJamie Gritton #endif 15420304c731SJamie Gritton if (prison_restrict_ip6(tpr, NULL)) { 15430304c731SJamie Gritton redo_ip6 = 1; 15440304c731SJamie Gritton descend = 0; 15450304c731SJamie Gritton } 15460304c731SJamie Gritton } 15470304c731SJamie Gritton } 1548b38ff370SJamie Gritton #endif 15490304c731SJamie Gritton if (gotslevel) { 1550b38ff370SJamie Gritton pr->pr_securelevel = slevel; 15510304c731SJamie Gritton /* Set all child jails to be at least this level. */ 15520304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 15530304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 15540304c731SJamie Gritton tpr->pr_securelevel = slevel; 15550304c731SJamie Gritton } 1556b97457e2SJamie Gritton if (gotchildmax) { 1557b97457e2SJamie Gritton pr->pr_childmax = childmax; 1558b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1559b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1560b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1561b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1562b97457e2SJamie Gritton ? childmax - level : 0; 1563b97457e2SJamie Gritton } 15640304c731SJamie Gritton if (gotenforce) { 15650304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 15660304c731SJamie Gritton /* Pass this restriction on to the children. */ 15670304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 15680304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 15690304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 15700304c731SJamie Gritton } 15710304c731SJamie Gritton if (name != NULL) { 15720304c731SJamie Gritton if (ppr == &prison0) 1573b38ff370SJamie Gritton strlcpy(pr->pr_name, name, sizeof(pr->pr_name)); 15740304c731SJamie Gritton else 15750304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 15760304c731SJamie Gritton ppr->pr_name, name); 15770304c731SJamie Gritton /* Change this component of child names. */ 15780304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 15790304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 15800304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 15810304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 15820304c731SJamie Gritton } 15830304c731SJamie Gritton } 1584b38ff370SJamie Gritton if (path != NULL) { 15850304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 15860304c731SJamie Gritton if (path[0] == '/' && strcmp(mypr->pr_path, "/")) 15870304c731SJamie Gritton snprintf(pr->pr_path, sizeof(pr->pr_path), "%s%s", 15880304c731SJamie Gritton mypr->pr_path, path); 15890304c731SJamie Gritton else 1590b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1591b38ff370SJamie Gritton pr->pr_root = root; 1592b38ff370SJamie Gritton } 159376ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 159476ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 159576ca6f88SJamie Gritton /* 159676ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 159776ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 159876ca6f88SJamie Gritton * it is always set with allprison_lock at least 159976ca6f88SJamie Gritton * shared, and is held exclusively here. 160076ca6f88SJamie Gritton */ 1601c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1602c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1603c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1604c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1605c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1606c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 160776ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 160876ca6f88SJamie Gritton } 160976ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 161076ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1611b38ff370SJamie Gritton if (host != NULL) 1612c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 161376ca6f88SJamie Gritton if (domain != NULL) 1614c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1615c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 161676ca6f88SJamie Gritton if (uuid != NULL) 1617c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 161876ca6f88SJamie Gritton if (gothid) 161976ca6f88SJamie Gritton pr->pr_hostid = hid; 162076ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 162176ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 162276ca6f88SJamie Gritton descend = 0; 162376ca6f88SJamie Gritton else { 162476ca6f88SJamie Gritton if (host != NULL) 1625c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1626c1f19219SJamie Gritton pr->pr_hostname, 1627c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 162876ca6f88SJamie Gritton if (domain != NULL) 1629c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1630c1f19219SJamie Gritton pr->pr_domainname, 1631c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 163276ca6f88SJamie Gritton if (uuid != NULL) 1633c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1634c1f19219SJamie Gritton pr->pr_hostuuid, 1635c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 163676ca6f88SJamie Gritton if (gothid) 163776ca6f88SJamie Gritton tpr->pr_hostid = hid; 163876ca6f88SJamie Gritton } 163976ca6f88SJamie Gritton } 164076ca6f88SJamie Gritton } 16410304c731SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) { 16420304c731SJamie Gritton /* Clear allow bits in all children. */ 16430304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 16440304c731SJamie Gritton tpr->pr_allow &= ~tallow; 16450304c731SJamie Gritton } 16460304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 1647b38ff370SJamie Gritton /* 1648b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 1649b38ff370SJamie Gritton * persist flag lose that reference. Only do this for existing prisons 1650b38ff370SJamie Gritton * for now, so new ones will remain unseen until after the module 1651b38ff370SJamie Gritton * handlers have completed. 1652b38ff370SJamie Gritton */ 1653b38ff370SJamie Gritton if (!created && (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags))) { 1654b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1655b38ff370SJamie Gritton pr->pr_ref++; 1656b38ff370SJamie Gritton pr->pr_uref++; 1657b38ff370SJamie Gritton } else { 1658b38ff370SJamie Gritton pr->pr_ref--; 1659b38ff370SJamie Gritton pr->pr_uref--; 1660b38ff370SJamie Gritton } 1661b38ff370SJamie Gritton } 1662b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1663b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1664b38ff370SJamie Gritton 1665a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 1666a7ad07bfSEdward Tomasz Napierala if (created) 1667a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 1668a7ad07bfSEdward Tomasz Napierala #endif 1669a7ad07bfSEdward Tomasz Napierala 16700304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 16710304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 16720304c731SJamie Gritton */ 16730304c731SJamie Gritton #ifdef INET 16740304c731SJamie Gritton while (redo_ip4) { 16750304c731SJamie Gritton ip4s = pr->pr_ip4s; 16760304c731SJamie Gritton ip4 = malloc(ip4s * sizeof(*ip4), M_PRISON, M_WAITOK); 16770304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 16780304c731SJamie Gritton redo_ip4 = 0; 16790304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1680bdfc8cc4SJamie Gritton #ifdef VIMAGE 1681bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1682bdfc8cc4SJamie Gritton descend = 0; 1683bdfc8cc4SJamie Gritton continue; 1684bdfc8cc4SJamie Gritton } 1685bdfc8cc4SJamie Gritton #endif 16860304c731SJamie Gritton if (prison_restrict_ip4(tpr, ip4)) { 16870304c731SJamie Gritton if (ip4 != NULL) 16880304c731SJamie Gritton ip4 = NULL; 16890304c731SJamie Gritton else 16900304c731SJamie Gritton redo_ip4 = 1; 16910304c731SJamie Gritton } 16920304c731SJamie Gritton } 16930304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 16940304c731SJamie Gritton } 16950304c731SJamie Gritton #endif 16960304c731SJamie Gritton #ifdef INET6 16970304c731SJamie Gritton while (redo_ip6) { 16980304c731SJamie Gritton ip6s = pr->pr_ip6s; 16990304c731SJamie Gritton ip6 = malloc(ip6s * sizeof(*ip6), M_PRISON, M_WAITOK); 17000304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 17010304c731SJamie Gritton redo_ip6 = 0; 17020304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1703bdfc8cc4SJamie Gritton #ifdef VIMAGE 1704bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1705bdfc8cc4SJamie Gritton descend = 0; 1706bdfc8cc4SJamie Gritton continue; 1707bdfc8cc4SJamie Gritton } 1708bdfc8cc4SJamie Gritton #endif 17090304c731SJamie Gritton if (prison_restrict_ip6(tpr, ip6)) { 17100304c731SJamie Gritton if (ip6 != NULL) 17110304c731SJamie Gritton ip6 = NULL; 17120304c731SJamie Gritton else 17130304c731SJamie Gritton redo_ip6 = 1; 17140304c731SJamie Gritton } 17150304c731SJamie Gritton } 17160304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 17170304c731SJamie Gritton } 17180304c731SJamie Gritton #endif 17190304c731SJamie Gritton 1720b38ff370SJamie Gritton /* Let the modules do their work. */ 1721b38ff370SJamie Gritton sx_downgrade(&allprison_lock); 1722b38ff370SJamie Gritton if (created) { 1723b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 1724b38ff370SJamie Gritton if (error) { 1725b38ff370SJamie Gritton prison_deref(pr, PD_LIST_SLOCKED); 1726b38ff370SJamie Gritton goto done_errmsg; 1727b38ff370SJamie Gritton } 1728b38ff370SJamie Gritton } 1729b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 1730b38ff370SJamie Gritton if (error) { 1731b38ff370SJamie Gritton prison_deref(pr, created 1732b38ff370SJamie Gritton ? PD_LIST_SLOCKED 1733b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1734b38ff370SJamie Gritton goto done_errmsg; 1735b38ff370SJamie Gritton } 1736b38ff370SJamie Gritton 1737b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 1738b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 1739b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1740b38ff370SJamie Gritton error = do_jail_attach(td, pr); 1741b38ff370SJamie Gritton if (error) { 1742b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 1743b38ff370SJamie Gritton if (!created) 1744b38ff370SJamie Gritton prison_deref(pr, PD_DEREF); 1745b38ff370SJamie Gritton goto done_errmsg; 1746b38ff370SJamie Gritton } 1747b38ff370SJamie Gritton } 1748b38ff370SJamie Gritton 1749b38ff370SJamie Gritton /* 1750b38ff370SJamie Gritton * Now that it is all there, drop the temporary reference from existing 1751b38ff370SJamie Gritton * prisons. Or add a reference to newly created persistent prisons 1752b38ff370SJamie Gritton * (which was not done earlier so that the prison would not be publicly 1753b38ff370SJamie Gritton * visible). 1754b38ff370SJamie Gritton */ 1755b38ff370SJamie Gritton if (!created) { 1756b38ff370SJamie Gritton prison_deref(pr, (flags & JAIL_ATTACH) 1757b38ff370SJamie Gritton ? PD_DEREF 1758b38ff370SJamie Gritton : PD_DEREF | PD_LIST_SLOCKED); 1759b38ff370SJamie Gritton } else { 1760b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 1761b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1762b38ff370SJamie Gritton pr->pr_ref++; 1763b38ff370SJamie Gritton pr->pr_uref++; 1764b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1765b38ff370SJamie Gritton } 1766b38ff370SJamie Gritton if (!(flags & JAIL_ATTACH)) 1767b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 1768b38ff370SJamie Gritton } 1769b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 1770b38ff370SJamie Gritton goto done_errmsg; 1771b38ff370SJamie Gritton 17720304c731SJamie Gritton done_deref_locked: 17730304c731SJamie Gritton prison_deref(pr, created 17740304c731SJamie Gritton ? PD_LOCKED | PD_LIST_XLOCKED 17750304c731SJamie Gritton : PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 17760304c731SJamie Gritton goto done_releroot; 1777b38ff370SJamie Gritton done_unlock_list: 1778b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 1779b38ff370SJamie Gritton done_releroot: 1780b38ff370SJamie Gritton if (root != NULL) { 1781b38ff370SJamie Gritton vfslocked = VFS_LOCK_GIANT(root->v_mount); 1782b38ff370SJamie Gritton vrele(root); 1783b38ff370SJamie Gritton VFS_UNLOCK_GIANT(vfslocked); 1784b38ff370SJamie Gritton } 1785b38ff370SJamie Gritton done_errmsg: 1786b38ff370SJamie Gritton if (error) { 1787b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 1788b38ff370SJamie Gritton if (errmsg_len > 0) { 1789b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 1790b38ff370SJamie Gritton if (errmsg_pos > 0) { 1791b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 1792b38ff370SJamie Gritton bcopy(errmsg, 1793b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1794b38ff370SJamie Gritton errmsg_len); 1795b38ff370SJamie Gritton else 1796b38ff370SJamie Gritton copyout(errmsg, 1797b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 1798b38ff370SJamie Gritton errmsg_len); 1799b38ff370SJamie Gritton } 1800b38ff370SJamie Gritton } 1801b38ff370SJamie Gritton } 1802b38ff370SJamie Gritton done_free: 1803b38ff370SJamie Gritton #ifdef INET 1804b38ff370SJamie Gritton free(ip4, M_PRISON); 1805b38ff370SJamie Gritton #endif 1806b38ff370SJamie Gritton #ifdef INET6 1807b38ff370SJamie Gritton free(ip6, M_PRISON); 1808b38ff370SJamie Gritton #endif 1809b38ff370SJamie Gritton vfs_freeopts(opts); 1810b38ff370SJamie Gritton return (error); 1811b38ff370SJamie Gritton } 1812b38ff370SJamie Gritton 1813b38ff370SJamie Gritton 1814b38ff370SJamie Gritton /* 1815b38ff370SJamie Gritton * struct jail_get_args { 1816b38ff370SJamie Gritton * struct iovec *iovp; 1817b38ff370SJamie Gritton * unsigned int iovcnt; 1818b38ff370SJamie Gritton * int flags; 1819b38ff370SJamie Gritton * }; 1820b38ff370SJamie Gritton */ 1821b38ff370SJamie Gritton int 1822b38ff370SJamie Gritton jail_get(struct thread *td, struct jail_get_args *uap) 1823b38ff370SJamie Gritton { 1824b38ff370SJamie Gritton struct uio *auio; 1825b38ff370SJamie Gritton int error; 1826b38ff370SJamie Gritton 1827b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 1828b38ff370SJamie Gritton if (uap->iovcnt & 1) 1829b38ff370SJamie Gritton return (EINVAL); 1830b38ff370SJamie Gritton 1831b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 1832b38ff370SJamie Gritton if (error) 1833b38ff370SJamie Gritton return (error); 1834b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 1835b38ff370SJamie Gritton if (error == 0) 1836b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 1837b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 1838b38ff370SJamie Gritton free(auio, M_IOV); 1839b38ff370SJamie Gritton return (error); 1840b38ff370SJamie Gritton } 1841b38ff370SJamie Gritton 1842b38ff370SJamie Gritton int 1843b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 1844b38ff370SJamie Gritton { 18450304c731SJamie Gritton struct prison *pr, *mypr; 1846b38ff370SJamie Gritton struct vfsopt *opt; 1847b38ff370SJamie Gritton struct vfsoptlist *opts; 1848b38ff370SJamie Gritton char *errmsg, *name; 18490304c731SJamie Gritton int error, errmsg_len, errmsg_pos, fi, i, jid, len, locked, pos; 1850b38ff370SJamie Gritton 1851b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 1852b38ff370SJamie Gritton return (EINVAL); 1853b38ff370SJamie Gritton 1854b38ff370SJamie Gritton /* Get the parameter list. */ 1855b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 1856b38ff370SJamie Gritton if (error) 1857b38ff370SJamie Gritton return (error); 1858b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 18590304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 18601e2a13e6SJamie Gritton 1861b38ff370SJamie Gritton /* 1862b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 1863b38ff370SJamie Gritton */ 1864b38ff370SJamie Gritton sx_slock(&allprison_lock); 1865b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 1866b38ff370SJamie Gritton if (error == 0) { 1867b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 18680304c731SJamie Gritton if (pr->pr_id > jid && prison_ischild(mypr, pr)) { 1869b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 1870b38ff370SJamie Gritton if (pr->pr_ref > 0 && 1871b38ff370SJamie Gritton (pr->pr_uref > 0 || (flags & JAIL_DYING))) 1872b38ff370SJamie Gritton break; 1873b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1874b38ff370SJamie Gritton } 1875b38ff370SJamie Gritton } 1876b38ff370SJamie Gritton if (pr != NULL) 1877b38ff370SJamie Gritton goto found_prison; 1878b38ff370SJamie Gritton error = ENOENT; 1879b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 1880b38ff370SJamie Gritton goto done_unlock_list; 1881b38ff370SJamie Gritton } else if (error != ENOENT) 1882b38ff370SJamie Gritton goto done_unlock_list; 1883b38ff370SJamie Gritton 1884b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1885b38ff370SJamie Gritton if (error == 0) { 1886b38ff370SJamie Gritton if (jid != 0) { 18870304c731SJamie Gritton pr = prison_find_child(mypr, jid); 1888b38ff370SJamie Gritton if (pr != NULL) { 1889b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1890b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1891b38ff370SJamie Gritton error = ENOENT; 1892b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1893b38ff370SJamie Gritton jid); 1894b38ff370SJamie Gritton goto done_unlock_list; 1895b38ff370SJamie Gritton } 1896b38ff370SJamie Gritton goto found_prison; 1897b38ff370SJamie Gritton } 1898b38ff370SJamie Gritton error = ENOENT; 1899b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 1900b38ff370SJamie Gritton goto done_unlock_list; 1901b38ff370SJamie Gritton } 1902b38ff370SJamie Gritton } else if (error != ENOENT) 1903b38ff370SJamie Gritton goto done_unlock_list; 1904b38ff370SJamie Gritton 1905b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1906b38ff370SJamie Gritton if (error == 0) { 1907b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1908b38ff370SJamie Gritton error = EINVAL; 1909b38ff370SJamie Gritton goto done_unlock_list; 1910b38ff370SJamie Gritton } 19110304c731SJamie Gritton pr = prison_find_name(mypr, name); 1912b38ff370SJamie Gritton if (pr != NULL) { 1913b38ff370SJamie Gritton if (pr->pr_uref == 0 && !(flags & JAIL_DYING)) { 1914b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 1915b38ff370SJamie Gritton error = ENOENT; 1916b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 1917b38ff370SJamie Gritton name); 1918b38ff370SJamie Gritton goto done_unlock_list; 1919b38ff370SJamie Gritton } 1920b38ff370SJamie Gritton goto found_prison; 1921b38ff370SJamie Gritton } 1922b38ff370SJamie Gritton error = ENOENT; 1923b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 1924b38ff370SJamie Gritton goto done_unlock_list; 1925b38ff370SJamie Gritton } else if (error != ENOENT) 1926b38ff370SJamie Gritton goto done_unlock_list; 1927b38ff370SJamie Gritton 1928b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 1929b38ff370SJamie Gritton error = ENOENT; 1930b38ff370SJamie Gritton goto done_unlock_list; 1931b38ff370SJamie Gritton 1932b38ff370SJamie Gritton found_prison: 1933b38ff370SJamie Gritton /* Get the parameters of the prison. */ 1934b38ff370SJamie Gritton pr->pr_ref++; 1935b38ff370SJamie Gritton locked = PD_LOCKED; 1936b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 1937b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 1938b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1939b38ff370SJamie Gritton goto done_deref; 19400304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 19410304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 1942b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1943b38ff370SJamie Gritton goto done_deref; 19440304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 19450304c731SJamie Gritton if (error != 0 && error != ENOENT) 19460304c731SJamie Gritton goto done_deref; 19470304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 1948b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 1949b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1950b38ff370SJamie Gritton goto done_deref; 19510304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 1952b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1953b38ff370SJamie Gritton goto done_deref; 1954b38ff370SJamie Gritton #ifdef INET 1955b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip4.addr", pr->pr_ip4, 1956b38ff370SJamie Gritton pr->pr_ip4s * sizeof(*pr->pr_ip4)); 1957b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1958b38ff370SJamie Gritton goto done_deref; 1959b38ff370SJamie Gritton #endif 1960b38ff370SJamie Gritton #ifdef INET6 1961b38ff370SJamie Gritton error = vfs_setopt_part(opts, "ip6.addr", pr->pr_ip6, 1962b38ff370SJamie Gritton pr->pr_ip6s * sizeof(*pr->pr_ip6)); 1963b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1964b38ff370SJamie Gritton goto done_deref; 1965b38ff370SJamie Gritton #endif 1966b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 1967b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 1968b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1969b38ff370SJamie Gritton goto done_deref; 1970b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 1971b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 1972b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 1973b97457e2SJamie Gritton goto done_deref; 1974b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 1975b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 1976b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 1977b97457e2SJamie Gritton goto done_deref; 1978c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 1979b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 1980b38ff370SJamie Gritton goto done_deref; 1981c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 198276ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 198376ca6f88SJamie Gritton goto done_deref; 1984c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 198576ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 198676ca6f88SJamie Gritton goto done_deref; 1987841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 1988a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 198976ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 199076ca6f88SJamie Gritton 199176ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 199276ca6f88SJamie Gritton } else 199376ca6f88SJamie Gritton #endif 199476ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 199576ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 199676ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 199776ca6f88SJamie Gritton goto done_deref; 19980304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 19990304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 20000304c731SJamie Gritton if (error != 0 && error != ENOENT) 20010304c731SJamie Gritton goto done_deref; 20020304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 20030304c731SJamie Gritton fi++) { 20040304c731SJamie Gritton if (pr_flag_names[fi] == NULL) 20050304c731SJamie Gritton continue; 20060304c731SJamie Gritton i = (pr->pr_flags & (1 << fi)) ? 1 : 0; 20070304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_names[fi], &i, sizeof(i)); 2008b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2009b38ff370SJamie Gritton goto done_deref; 2010b38ff370SJamie Gritton i = !i; 20110304c731SJamie Gritton error = vfs_setopt(opts, pr_flag_nonames[fi], &i, sizeof(i)); 2012b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2013b38ff370SJamie Gritton goto done_deref; 20140304c731SJamie Gritton } 20157cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 20167cbf7213SJamie Gritton fi++) { 20177cbf7213SJamie Gritton i = pr->pr_flags & 20187cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 20197cbf7213SJamie Gritton i = pr_flag_jailsys[fi].disable && 20207cbf7213SJamie Gritton (i == pr_flag_jailsys[fi].disable) ? JAIL_SYS_DISABLE 20217cbf7213SJamie Gritton : (i == pr_flag_jailsys[fi].new) ? JAIL_SYS_NEW 20227cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 20237cbf7213SJamie Gritton error = 20247cbf7213SJamie Gritton vfs_setopt(opts, pr_flag_jailsys[fi].name, &i, sizeof(i)); 20257cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 20267cbf7213SJamie Gritton goto done_deref; 20277cbf7213SJamie Gritton } 20280304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 20290304c731SJamie Gritton fi++) { 20300304c731SJamie Gritton if (pr_allow_names[fi] == NULL) 20310304c731SJamie Gritton continue; 20320304c731SJamie Gritton i = (pr->pr_allow & (1 << fi)) ? 1 : 0; 20330304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_names[fi], &i, sizeof(i)); 20340304c731SJamie Gritton if (error != 0 && error != ENOENT) 20350304c731SJamie Gritton goto done_deref; 20360304c731SJamie Gritton i = !i; 20370304c731SJamie Gritton error = vfs_setopt(opts, pr_allow_nonames[fi], &i, sizeof(i)); 20380304c731SJamie Gritton if (error != 0 && error != ENOENT) 20390304c731SJamie Gritton goto done_deref; 20400304c731SJamie Gritton } 2041b38ff370SJamie Gritton i = (pr->pr_uref == 0); 2042b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2043b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2044b38ff370SJamie Gritton goto done_deref; 2045b38ff370SJamie Gritton i = !i; 2046b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2047b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 2048b38ff370SJamie Gritton goto done_deref; 2049b38ff370SJamie Gritton 2050b38ff370SJamie Gritton /* Get the module parameters. */ 2051b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2052b38ff370SJamie Gritton locked = 0; 2053b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2054b38ff370SJamie Gritton if (error) 2055b38ff370SJamie Gritton goto done_deref; 2056b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_LIST_SLOCKED); 2057b38ff370SJamie Gritton 2058b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2059b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2060b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2061b38ff370SJamie Gritton error = EINVAL; 2062b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 2063b38ff370SJamie Gritton goto done_errmsg; 2064b38ff370SJamie Gritton } 2065b38ff370SJamie Gritton } 2066b38ff370SJamie Gritton 2067b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2068b38ff370SJamie Gritton error = 0; 2069b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2070b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2071b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2072b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2073b38ff370SJamie Gritton if (opt->value != NULL) { 2074b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2075b38ff370SJamie Gritton bcopy(opt->value, 2076b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2077b38ff370SJamie Gritton opt->len); 2078b38ff370SJamie Gritton } else { 2079b38ff370SJamie Gritton error = copyout(opt->value, 2080b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2081b38ff370SJamie Gritton opt->len); 2082b38ff370SJamie Gritton if (error) 2083b38ff370SJamie Gritton break; 2084b38ff370SJamie Gritton } 2085b38ff370SJamie Gritton } 2086b38ff370SJamie Gritton } 2087b38ff370SJamie Gritton } 2088b38ff370SJamie Gritton goto done_errmsg; 2089b38ff370SJamie Gritton 2090b38ff370SJamie Gritton done_deref: 2091b38ff370SJamie Gritton prison_deref(pr, locked | PD_DEREF | PD_LIST_SLOCKED); 2092b38ff370SJamie Gritton goto done_errmsg; 2093b38ff370SJamie Gritton 2094b38ff370SJamie Gritton done_unlock_list: 2095b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2096b38ff370SJamie Gritton done_errmsg: 2097b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 2098b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2099b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2100b38ff370SJamie Gritton if (errmsg_len > 0) { 2101b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2102b38ff370SJamie Gritton bcopy(errmsg, 2103b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2104b38ff370SJamie Gritton errmsg_len); 2105b38ff370SJamie Gritton else 2106b38ff370SJamie Gritton copyout(errmsg, 2107b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2108b38ff370SJamie Gritton errmsg_len); 2109b38ff370SJamie Gritton } 2110b38ff370SJamie Gritton } 2111b38ff370SJamie Gritton vfs_freeopts(opts); 2112b38ff370SJamie Gritton return (error); 2113b38ff370SJamie Gritton } 2114b38ff370SJamie Gritton 21150304c731SJamie Gritton 2116b38ff370SJamie Gritton /* 2117b38ff370SJamie Gritton * struct jail_remove_args { 2118b38ff370SJamie Gritton * int jid; 2119b38ff370SJamie Gritton * }; 2120b38ff370SJamie Gritton */ 2121b38ff370SJamie Gritton int 2122b38ff370SJamie Gritton jail_remove(struct thread *td, struct jail_remove_args *uap) 2123b38ff370SJamie Gritton { 21240304c731SJamie Gritton struct prison *pr, *cpr, *lpr, *tpr; 21250304c731SJamie Gritton int descend, error; 2126b38ff370SJamie Gritton 2127b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2128b38ff370SJamie Gritton if (error) 2129b38ff370SJamie Gritton return (error); 2130b38ff370SJamie Gritton 2131b38ff370SJamie Gritton sx_xlock(&allprison_lock); 21320304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2133b38ff370SJamie Gritton if (pr == NULL) { 2134b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2135b38ff370SJamie Gritton return (EINVAL); 2136b38ff370SJamie Gritton } 2137b38ff370SJamie Gritton 21380304c731SJamie Gritton /* Remove all descendants of this prison, then remove this prison. */ 21390304c731SJamie Gritton pr->pr_ref++; 21400304c731SJamie Gritton pr->pr_flags |= PR_REMOVE; 21410304c731SJamie Gritton if (!LIST_EMPTY(&pr->pr_children)) { 21420304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 21430304c731SJamie Gritton lpr = NULL; 21440304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 21450304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 21460304c731SJamie Gritton if (cpr->pr_ref > 0) { 21470304c731SJamie Gritton tpr = cpr; 21480304c731SJamie Gritton cpr->pr_ref++; 21490304c731SJamie Gritton cpr->pr_flags |= PR_REMOVE; 21500304c731SJamie Gritton } else { 21510304c731SJamie Gritton /* Already removed - do not do it again. */ 21520304c731SJamie Gritton tpr = NULL; 21530304c731SJamie Gritton } 21540304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 21550304c731SJamie Gritton if (lpr != NULL) { 21560304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 21570304c731SJamie Gritton prison_remove_one(lpr); 21580304c731SJamie Gritton sx_xlock(&allprison_lock); 21590304c731SJamie Gritton } 21600304c731SJamie Gritton lpr = tpr; 21610304c731SJamie Gritton } 21620304c731SJamie Gritton if (lpr != NULL) { 21630304c731SJamie Gritton mtx_lock(&lpr->pr_mtx); 21640304c731SJamie Gritton prison_remove_one(lpr); 21650304c731SJamie Gritton sx_xlock(&allprison_lock); 21660304c731SJamie Gritton } 21670304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 21680304c731SJamie Gritton } 21690304c731SJamie Gritton prison_remove_one(pr); 21700304c731SJamie Gritton return (0); 21710304c731SJamie Gritton } 21720304c731SJamie Gritton 21730304c731SJamie Gritton static void 21740304c731SJamie Gritton prison_remove_one(struct prison *pr) 21750304c731SJamie Gritton { 21760304c731SJamie Gritton struct proc *p; 21770304c731SJamie Gritton int deuref; 21780304c731SJamie Gritton 2179b38ff370SJamie Gritton /* If the prison was persistent, it is not anymore. */ 2180b38ff370SJamie Gritton deuref = 0; 2181b38ff370SJamie Gritton if (pr->pr_flags & PR_PERSIST) { 2182b38ff370SJamie Gritton pr->pr_ref--; 2183b38ff370SJamie Gritton deuref = PD_DEUREF; 2184b38ff370SJamie Gritton pr->pr_flags &= ~PR_PERSIST; 2185b38ff370SJamie Gritton } 2186b38ff370SJamie Gritton 21870304c731SJamie Gritton /* 21880304c731SJamie Gritton * jail_remove added a reference. If that's the only one, remove 21890304c731SJamie Gritton * the prison now. 21900304c731SJamie Gritton */ 21910304c731SJamie Gritton KASSERT(pr->pr_ref > 0, 21920304c731SJamie Gritton ("prison_remove_one removing a dead prison (jid=%d)", pr->pr_id)); 21930304c731SJamie Gritton if (pr->pr_ref == 1) { 2194b38ff370SJamie Gritton prison_deref(pr, 2195b38ff370SJamie Gritton deuref | PD_DEREF | PD_LOCKED | PD_LIST_XLOCKED); 21960304c731SJamie Gritton return; 2197b38ff370SJamie Gritton } 2198b38ff370SJamie Gritton 2199b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2200b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2201b38ff370SJamie Gritton /* 2202b38ff370SJamie Gritton * Kill all processes unfortunate enough to be attached to this prison. 2203b38ff370SJamie Gritton */ 2204b38ff370SJamie Gritton sx_slock(&allproc_lock); 2205b38ff370SJamie Gritton LIST_FOREACH(p, &allproc, p_list) { 2206b38ff370SJamie Gritton PROC_LOCK(p); 2207b38ff370SJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred && 2208b38ff370SJamie Gritton p->p_ucred->cr_prison == pr) 2209b38ff370SJamie Gritton psignal(p, SIGKILL); 2210b38ff370SJamie Gritton PROC_UNLOCK(p); 2211b38ff370SJamie Gritton } 2212b38ff370SJamie Gritton sx_sunlock(&allproc_lock); 22130304c731SJamie Gritton /* Remove the temporary reference added by jail_remove. */ 2214b38ff370SJamie Gritton prison_deref(pr, deuref | PD_DEREF); 221575c13541SPoul-Henning Kamp } 221675c13541SPoul-Henning Kamp 22178571af59SJamie Gritton 2218fd7a8150SMike Barcroft /* 22199ddb7954SMike Barcroft * struct jail_attach_args { 22209ddb7954SMike Barcroft * int jid; 22219ddb7954SMike Barcroft * }; 2222fd7a8150SMike Barcroft */ 2223fd7a8150SMike Barcroft int 22249ddb7954SMike Barcroft jail_attach(struct thread *td, struct jail_attach_args *uap) 2225fd7a8150SMike Barcroft { 2226b38ff370SJamie Gritton struct prison *pr; 2227b38ff370SJamie Gritton int error; 2228b38ff370SJamie Gritton 2229b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2230b38ff370SJamie Gritton if (error) 2231b38ff370SJamie Gritton return (error); 2232b38ff370SJamie Gritton 2233b38ff370SJamie Gritton sx_slock(&allprison_lock); 22340304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2235b38ff370SJamie Gritton if (pr == NULL) { 2236b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2237b38ff370SJamie Gritton return (EINVAL); 2238b38ff370SJamie Gritton } 2239b38ff370SJamie Gritton 2240b38ff370SJamie Gritton /* 2241b38ff370SJamie Gritton * Do not allow a process to attach to a prison that is not 2242b38ff370SJamie Gritton * considered to be "alive". 2243b38ff370SJamie Gritton */ 2244b38ff370SJamie Gritton if (pr->pr_uref == 0) { 2245b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2246b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2247b38ff370SJamie Gritton return (EINVAL); 2248b38ff370SJamie Gritton } 2249b38ff370SJamie Gritton 2250b38ff370SJamie Gritton return (do_jail_attach(td, pr)); 2251b38ff370SJamie Gritton } 2252b38ff370SJamie Gritton 2253b38ff370SJamie Gritton static int 2254b38ff370SJamie Gritton do_jail_attach(struct thread *td, struct prison *pr) 2255b38ff370SJamie Gritton { 22560304c731SJamie Gritton struct prison *ppr; 2257fd7a8150SMike Barcroft struct proc *p; 2258fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 2259453f7d53SChristian S.J. Peron int vfslocked, error; 2260fd7a8150SMike Barcroft 226157f22bd4SJacques Vidrine /* 226257f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 226357f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 226457f22bd4SJacques Vidrine * different jails at the same time. It is important for 226557f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 226657f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 226757f22bd4SJacques Vidrine * of another. 226857f22bd4SJacques Vidrine */ 2269fd7a8150SMike Barcroft pr->pr_ref++; 2270b38ff370SJamie Gritton pr->pr_uref++; 2271fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2272b38ff370SJamie Gritton 2273b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2274b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2275b38ff370SJamie Gritton if (error) { 2276b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF | PD_LIST_SLOCKED); 2277b38ff370SJamie Gritton return (error); 2278b38ff370SJamie Gritton } 2279dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 2280fd7a8150SMike Barcroft 2281413628a7SBjoern A. Zeeb /* 2282413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2283413628a7SBjoern A. Zeeb */ 22840304c731SJamie Gritton ppr = td->td_ucred->cr_prison; 2285b38ff370SJamie Gritton p = td->td_proc; 2286413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2287413628a7SBjoern A. Zeeb if (error) 2288b38ff370SJamie Gritton goto e_revert_osd; 2289413628a7SBjoern A. Zeeb 2290453f7d53SChristian S.J. Peron vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2291cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2292fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2293fd7a8150SMike Barcroft goto e_unlock; 2294fd7a8150SMike Barcroft #ifdef MAC 229530d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2296fd7a8150SMike Barcroft goto e_unlock; 2297fd7a8150SMike Barcroft #endif 229822db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2299b38ff370SJamie Gritton if ((error = change_root(pr->pr_root, td))) 2300b38ff370SJamie Gritton goto e_unlock_giant; 2301453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2302fd7a8150SMike Barcroft 2303fd7a8150SMike Barcroft newcred = crget(); 2304fd7a8150SMike Barcroft PROC_LOCK(p); 2305fd7a8150SMike Barcroft oldcred = p->p_ucred; 2306fd7a8150SMike Barcroft setsugid(p); 2307fd7a8150SMike Barcroft crcopy(newcred, oldcred); 230869c4ee54SJohn Baldwin newcred->cr_prison = pr; 2309fd7a8150SMike Barcroft p->p_ucred = newcred; 2310fd7a8150SMike Barcroft PROC_UNLOCK(p); 2311097055e2SEdward Tomasz Napierala #ifdef RACCT 2312097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2313097055e2SEdward Tomasz Napierala #endif 2314fd7a8150SMike Barcroft crfree(oldcred); 23150304c731SJamie Gritton prison_deref(ppr, PD_DEREF | PD_DEUREF); 2316fd7a8150SMike Barcroft return (0); 2317fd7a8150SMike Barcroft e_unlock: 231822db15c0SAttilio Rao VOP_UNLOCK(pr->pr_root, 0); 2319b38ff370SJamie Gritton e_unlock_giant: 2320453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2321b38ff370SJamie Gritton e_revert_osd: 2322b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 23230304c731SJamie Gritton (void)osd_jail_call(ppr, PR_METHOD_ATTACH, td); 2324b38ff370SJamie Gritton prison_deref(pr, PD_DEREF | PD_DEUREF); 2325fd7a8150SMike Barcroft return (error); 2326fd7a8150SMike Barcroft } 2327fd7a8150SMike Barcroft 23280304c731SJamie Gritton 2329fd7a8150SMike Barcroft /* 2330fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2331fd7a8150SMike Barcroft */ 233254b369c1SPawel Jakub Dawidek struct prison * 2333fd7a8150SMike Barcroft prison_find(int prid) 2334fd7a8150SMike Barcroft { 2335fd7a8150SMike Barcroft struct prison *pr; 2336fd7a8150SMike Barcroft 2337dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2338b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2339fd7a8150SMike Barcroft if (pr->pr_id == prid) { 2340fd7a8150SMike Barcroft mtx_lock(&pr->pr_mtx); 2341b38ff370SJamie Gritton if (pr->pr_ref > 0) 2342fd7a8150SMike Barcroft return (pr); 2343b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2344fd7a8150SMike Barcroft } 2345fd7a8150SMike Barcroft } 2346fd7a8150SMike Barcroft return (NULL); 2347fd7a8150SMike Barcroft } 2348fd7a8150SMike Barcroft 2349b38ff370SJamie Gritton /* 23500304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2351b38ff370SJamie Gritton */ 2352b38ff370SJamie Gritton struct prison * 23530304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2354b38ff370SJamie Gritton { 23550304c731SJamie Gritton struct prison *pr; 23560304c731SJamie Gritton int descend; 2357b38ff370SJamie Gritton 2358b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 23590304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 23600304c731SJamie Gritton if (pr->pr_id == prid) { 23610304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 23620304c731SJamie Gritton if (pr->pr_ref > 0) 23630304c731SJamie Gritton return (pr); 23640304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 23650304c731SJamie Gritton } 23660304c731SJamie Gritton } 23670304c731SJamie Gritton return (NULL); 23680304c731SJamie Gritton } 23690304c731SJamie Gritton 23700304c731SJamie Gritton /* 23710304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 23720304c731SJamie Gritton */ 23730304c731SJamie Gritton struct prison * 23740304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 23750304c731SJamie Gritton { 23760304c731SJamie Gritton struct prison *pr, *deadpr; 23770304c731SJamie Gritton size_t mylen; 23780304c731SJamie Gritton int descend; 23790304c731SJamie Gritton 23800304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 23810304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2382b38ff370SJamie Gritton again: 2383b38ff370SJamie Gritton deadpr = NULL; 23840304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 23850304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2386b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2387b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2388b38ff370SJamie Gritton if (pr->pr_uref > 0) 2389b38ff370SJamie Gritton return (pr); 2390b38ff370SJamie Gritton deadpr = pr; 2391b38ff370SJamie Gritton } 2392b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2393b38ff370SJamie Gritton } 2394b38ff370SJamie Gritton } 23950304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2396b38ff370SJamie Gritton if (deadpr != NULL) { 2397b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2398b38ff370SJamie Gritton if (deadpr->pr_ref == 0) { 2399b38ff370SJamie Gritton mtx_unlock(&deadpr->pr_mtx); 2400b38ff370SJamie Gritton goto again; 2401b38ff370SJamie Gritton } 2402b38ff370SJamie Gritton } 2403b38ff370SJamie Gritton return (deadpr); 2404b38ff370SJamie Gritton } 2405b38ff370SJamie Gritton 2406b38ff370SJamie Gritton /* 24070304c731SJamie Gritton * See if a prison has the specific flag set. 24080304c731SJamie Gritton */ 24090304c731SJamie Gritton int 24100304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 24110304c731SJamie Gritton { 24120304c731SJamie Gritton 24130304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 24140304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 24150304c731SJamie Gritton } 24160304c731SJamie Gritton 24170304c731SJamie Gritton int 24180304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 24190304c731SJamie Gritton { 24200304c731SJamie Gritton 24210304c731SJamie Gritton /* This is an atomic read, so no locking is necessary. */ 24220304c731SJamie Gritton return (cred->cr_prison->pr_allow & flag); 24230304c731SJamie Gritton } 24240304c731SJamie Gritton 24250304c731SJamie Gritton /* 2426b38ff370SJamie Gritton * Remove a prison reference. If that was the last reference, remove the 2427b38ff370SJamie Gritton * prison itself - but not in this context in case there are locks held. 2428b38ff370SJamie Gritton */ 242991421ba2SRobert Watson void 24301ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 243191421ba2SRobert Watson { 243291421ba2SRobert Watson 24331ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 243491421ba2SRobert Watson pr->pr_ref--; 243591421ba2SRobert Watson if (pr->pr_ref == 0) { 243601137630SRobert Watson mtx_unlock(&pr->pr_mtx); 2437c2cda609SPawel Jakub Dawidek TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 2438c2cda609SPawel Jakub Dawidek taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2439c2cda609SPawel Jakub Dawidek return; 2440c2cda609SPawel Jakub Dawidek } 2441c2cda609SPawel Jakub Dawidek mtx_unlock(&pr->pr_mtx); 2442c2cda609SPawel Jakub Dawidek } 2443c2cda609SPawel Jakub Dawidek 24441ba4a712SPawel Jakub Dawidek void 24451ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 24461ba4a712SPawel Jakub Dawidek { 24471ba4a712SPawel Jakub Dawidek 24481ba4a712SPawel Jakub Dawidek mtx_lock(&pr->pr_mtx); 24491ba4a712SPawel Jakub Dawidek prison_free_locked(pr); 24501ba4a712SPawel Jakub Dawidek } 24511ba4a712SPawel Jakub Dawidek 2452c2cda609SPawel Jakub Dawidek static void 2453c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2454c2cda609SPawel Jakub Dawidek { 2455b38ff370SJamie Gritton 2456b38ff370SJamie Gritton prison_deref((struct prison *)context, 0); 2457b38ff370SJamie Gritton } 2458b38ff370SJamie Gritton 2459b38ff370SJamie Gritton /* 2460b38ff370SJamie Gritton * Remove a prison reference (usually). This internal version assumes no 2461b38ff370SJamie Gritton * mutexes are held, except perhaps the prison itself. If there are no more 2462b38ff370SJamie Gritton * references, release and delist the prison. On completion, the prison lock 2463b38ff370SJamie Gritton * and the allprison lock are both unlocked. 2464b38ff370SJamie Gritton */ 2465b38ff370SJamie Gritton static void 2466b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2467b38ff370SJamie Gritton { 24680304c731SJamie Gritton struct prison *ppr, *tpr; 2469c2cda609SPawel Jakub Dawidek int vfslocked; 2470c2cda609SPawel Jakub Dawidek 2471b38ff370SJamie Gritton if (!(flags & PD_LOCKED)) 2472b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 24730304c731SJamie Gritton /* Decrement the user references in a separate loop. */ 2474b38ff370SJamie Gritton if (flags & PD_DEUREF) { 24750304c731SJamie Gritton for (tpr = pr;; tpr = tpr->pr_parent) { 24760304c731SJamie Gritton if (tpr != pr) 24770304c731SJamie Gritton mtx_lock(&tpr->pr_mtx); 24780304c731SJamie Gritton if (--tpr->pr_uref > 0) 24790304c731SJamie Gritton break; 24800304c731SJamie Gritton KASSERT(tpr != &prison0, ("prison0 pr_uref=0")); 24810304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 24820304c731SJamie Gritton } 2483b38ff370SJamie Gritton /* Done if there were only user references to remove. */ 2484b38ff370SJamie Gritton if (!(flags & PD_DEREF)) { 24850304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 2486b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2487b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2488b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2489b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2490b38ff370SJamie Gritton return; 2491b38ff370SJamie Gritton } 24920304c731SJamie Gritton if (tpr != pr) { 24930304c731SJamie Gritton mtx_unlock(&tpr->pr_mtx); 24940304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2495b38ff370SJamie Gritton } 24960304c731SJamie Gritton } 24970304c731SJamie Gritton 24980304c731SJamie Gritton for (;;) { 2499b38ff370SJamie Gritton if (flags & PD_DEREF) 2500b38ff370SJamie Gritton pr->pr_ref--; 2501b38ff370SJamie Gritton /* If the prison still has references, nothing else to do. */ 2502b38ff370SJamie Gritton if (pr->pr_ref > 0) { 2503b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2504b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) 2505b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2506b38ff370SJamie Gritton else if (flags & PD_LIST_XLOCKED) 2507b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2508b38ff370SJamie Gritton return; 2509b38ff370SJamie Gritton } 2510c2cda609SPawel Jakub Dawidek 2511b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2512b38ff370SJamie Gritton if (flags & PD_LIST_SLOCKED) { 2513b38ff370SJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 2514b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2515c2cda609SPawel Jakub Dawidek sx_xlock(&allprison_lock); 2516b38ff370SJamie Gritton } 2517b38ff370SJamie Gritton } else if (!(flags & PD_LIST_XLOCKED)) 2518b38ff370SJamie Gritton sx_xlock(&allprison_lock); 2519b38ff370SJamie Gritton 2520b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 25210304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 25220304c731SJamie Gritton ppr = pr->pr_parent; 25230304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 2524b97457e2SJamie Gritton tpr->pr_childcount--; 2525c4884ffaSJamie Gritton sx_xunlock(&allprison_lock); 25261ba4a712SPawel Jakub Dawidek 2527679e1390SJamie Gritton #ifdef VIMAGE 25280cb8b6a9SMarko Zec if (pr->pr_vnet != ppr->pr_vnet) 2529679e1390SJamie Gritton vnet_destroy(pr->pr_vnet); 2530679e1390SJamie Gritton #endif 2531b38ff370SJamie Gritton if (pr->pr_root != NULL) { 2532453f7d53SChristian S.J. Peron vfslocked = VFS_LOCK_GIANT(pr->pr_root->v_mount); 2533b3059e09SRobert Watson vrele(pr->pr_root); 2534453f7d53SChristian S.J. Peron VFS_UNLOCK_GIANT(vfslocked); 2535b38ff370SJamie Gritton } 2536b3059e09SRobert Watson mtx_destroy(&pr->pr_mtx); 2537413628a7SBjoern A. Zeeb #ifdef INET 2538413628a7SBjoern A. Zeeb free(pr->pr_ip4, M_PRISON); 2539413628a7SBjoern A. Zeeb #endif 2540b38ff370SJamie Gritton #ifdef INET6 2541b38ff370SJamie Gritton free(pr->pr_ip6, M_PRISON); 2542b38ff370SJamie Gritton #endif 2543b38ff370SJamie Gritton if (pr->pr_cpuset != NULL) 2544b38ff370SJamie Gritton cpuset_rel(pr->pr_cpuset); 2545b38ff370SJamie Gritton osd_jail_exit(pr); 2546a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 2547a7ad07bfSEdward Tomasz Napierala prison_racct_detach(pr); 2548ec125fbbSEdward Tomasz Napierala #endif 25491ede983cSDag-Erling Smørgrav free(pr, M_PRISON); 25500304c731SJamie Gritton 25510304c731SJamie Gritton /* Removing a prison frees a reference on its parent. */ 25520304c731SJamie Gritton pr = ppr; 25530304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 2554c4884ffaSJamie Gritton flags = PD_DEREF; 25550304c731SJamie Gritton } 2556b3059e09SRobert Watson } 2557b3059e09SRobert Watson 255891421ba2SRobert Watson void 25591ba4a712SPawel Jakub Dawidek prison_hold_locked(struct prison *pr) 25601ba4a712SPawel Jakub Dawidek { 25611ba4a712SPawel Jakub Dawidek 25621ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 25631ba4a712SPawel Jakub Dawidek KASSERT(pr->pr_ref > 0, 2564af7bd9a4SJamie Gritton ("Trying to hold dead prison (jid=%d).", pr->pr_id)); 25651ba4a712SPawel Jakub Dawidek pr->pr_ref++; 25661ba4a712SPawel Jakub Dawidek } 25671ba4a712SPawel Jakub Dawidek 25681ba4a712SPawel Jakub Dawidek void 256991421ba2SRobert Watson prison_hold(struct prison *pr) 257091421ba2SRobert Watson { 257191421ba2SRobert Watson 257201137630SRobert Watson mtx_lock(&pr->pr_mtx); 25731ba4a712SPawel Jakub Dawidek prison_hold_locked(pr); 257401137630SRobert Watson mtx_unlock(&pr->pr_mtx); 257501137630SRobert Watson } 257601137630SRobert Watson 2577413628a7SBjoern A. Zeeb void 2578413628a7SBjoern A. Zeeb prison_proc_hold(struct prison *pr) 257901137630SRobert Watson { 258001137630SRobert Watson 2581413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2582b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2583b38ff370SJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 2584b38ff370SJamie Gritton pr->pr_uref++; 2585413628a7SBjoern A. Zeeb mtx_unlock(&pr->pr_mtx); 258675c13541SPoul-Henning Kamp } 258775c13541SPoul-Henning Kamp 258875c13541SPoul-Henning Kamp void 2589413628a7SBjoern A. Zeeb prison_proc_free(struct prison *pr) 259075c13541SPoul-Henning Kamp { 2591413628a7SBjoern A. Zeeb 2592413628a7SBjoern A. Zeeb mtx_lock(&pr->pr_mtx); 2593b38ff370SJamie Gritton KASSERT(pr->pr_uref > 0, 2594b38ff370SJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2595b38ff370SJamie Gritton prison_deref(pr, PD_DEUREF | PD_LOCKED); 2596413628a7SBjoern A. Zeeb } 2597413628a7SBjoern A. Zeeb 2598413628a7SBjoern A. Zeeb 2599413628a7SBjoern A. Zeeb #ifdef INET 2600413628a7SBjoern A. Zeeb /* 26010304c731SJamie Gritton * Restrict a prison's IP address list with its parent's, possibly replacing 26020304c731SJamie Gritton * it. Return true if the replacement buffer was used (or would have been). 26030304c731SJamie Gritton */ 26040304c731SJamie Gritton static int 26050304c731SJamie Gritton prison_restrict_ip4(struct prison *pr, struct in_addr *newip4) 26060304c731SJamie Gritton { 26070304c731SJamie Gritton int ii, ij, used; 26080304c731SJamie Gritton struct prison *ppr; 26090304c731SJamie Gritton 26100304c731SJamie Gritton ppr = pr->pr_parent; 26110304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4_USER)) { 26120304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 26130304c731SJamie Gritton if (pr->pr_ip4s < ppr->pr_ip4s) { 26140304c731SJamie Gritton /* 26150304c731SJamie Gritton * There's no room for the parent's list. Use the 26160304c731SJamie Gritton * new list buffer, which is assumed to be big enough 26170304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 26180304c731SJamie Gritton * allocate one. 26190304c731SJamie Gritton */ 26200304c731SJamie Gritton used = 1; 26210304c731SJamie Gritton if (newip4 == NULL) { 26220304c731SJamie Gritton newip4 = malloc(ppr->pr_ip4s * sizeof(*newip4), 26230304c731SJamie Gritton M_PRISON, M_NOWAIT); 26240304c731SJamie Gritton if (newip4 != NULL) 26250304c731SJamie Gritton used = 0; 26260304c731SJamie Gritton } 26270304c731SJamie Gritton if (newip4 != NULL) { 26280304c731SJamie Gritton bcopy(ppr->pr_ip4, newip4, 26290304c731SJamie Gritton ppr->pr_ip4s * sizeof(*newip4)); 26300304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 26310304c731SJamie Gritton pr->pr_ip4 = newip4; 26320304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 26330304c731SJamie Gritton } 26340304c731SJamie Gritton return (used); 26350304c731SJamie Gritton } 26360304c731SJamie Gritton pr->pr_ip4s = ppr->pr_ip4s; 26370304c731SJamie Gritton if (pr->pr_ip4s > 0) 26380304c731SJamie Gritton bcopy(ppr->pr_ip4, pr->pr_ip4, 26390304c731SJamie Gritton pr->pr_ip4s * sizeof(*newip4)); 26400304c731SJamie Gritton else if (pr->pr_ip4 != NULL) { 26410304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 26420304c731SJamie Gritton pr->pr_ip4 = NULL; 26430304c731SJamie Gritton } 26442b0d6f81SJamie Gritton } else if (pr->pr_ip4s > 0) { 26450304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 26460304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip4s; ij++) 26470304c731SJamie Gritton if (pr->pr_ip4[0].s_addr == ppr->pr_ip4[ij].s_addr) 26480304c731SJamie Gritton break; 26490304c731SJamie Gritton if (ij < ppr->pr_ip4s) 26500304c731SJamie Gritton ii = 1; 26510304c731SJamie Gritton else { 26520304c731SJamie Gritton bcopy(pr->pr_ip4 + 1, pr->pr_ip4, 26530304c731SJamie Gritton --pr->pr_ip4s * sizeof(*pr->pr_ip4)); 26540304c731SJamie Gritton ii = 0; 26550304c731SJamie Gritton } 26560304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip4s; ) { 26570304c731SJamie Gritton if (pr->pr_ip4[ii].s_addr == ppr->pr_ip4[0].s_addr) { 26580304c731SJamie Gritton ii++; 26590304c731SJamie Gritton continue; 26600304c731SJamie Gritton } 26610304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 26620304c731SJamie Gritton qcmp_v4(&pr->pr_ip4[ii], &ppr->pr_ip4[ij])) { 26630304c731SJamie Gritton case -1: 26640304c731SJamie Gritton bcopy(pr->pr_ip4 + ii + 1, pr->pr_ip4 + ii, 26650304c731SJamie Gritton (--pr->pr_ip4s - ii) * sizeof(*pr->pr_ip4)); 26660304c731SJamie Gritton break; 26670304c731SJamie Gritton case 0: 26680304c731SJamie Gritton ii++; 26690304c731SJamie Gritton ij++; 26700304c731SJamie Gritton break; 26710304c731SJamie Gritton case 1: 26720304c731SJamie Gritton ij++; 26730304c731SJamie Gritton break; 26740304c731SJamie Gritton } 26750304c731SJamie Gritton } 26760304c731SJamie Gritton if (pr->pr_ip4s == 0) { 26777cbf7213SJamie Gritton pr->pr_flags |= PR_IP4_DISABLE; 26780304c731SJamie Gritton free(pr->pr_ip4, M_PRISON); 26790304c731SJamie Gritton pr->pr_ip4 = NULL; 26800304c731SJamie Gritton } 26810304c731SJamie Gritton } 26820304c731SJamie Gritton return (0); 26830304c731SJamie Gritton } 26840304c731SJamie Gritton 26850304c731SJamie Gritton /* 2686413628a7SBjoern A. Zeeb * Pass back primary IPv4 address of this jail. 2687413628a7SBjoern A. Zeeb * 26880304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 26890304c731SJamie Gritton * to make sure to initialize it correctly (e.g. INADDR_ANY). 2690413628a7SBjoern A. Zeeb * 2691b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2692b89e82ddSJamie Gritton * Address returned in NBO. 2693413628a7SBjoern A. Zeeb */ 2694413628a7SBjoern A. Zeeb int 26951cecba0fSBjoern A. Zeeb prison_get_ip4(struct ucred *cred, struct in_addr *ia) 2696413628a7SBjoern A. Zeeb { 2697b38ff370SJamie Gritton struct prison *pr; 2698413628a7SBjoern A. Zeeb 2699413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2700413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 270175c13541SPoul-Henning Kamp 2702b38ff370SJamie Gritton pr = cred->cr_prison; 27030304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 27040304c731SJamie Gritton return (0); 2705b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 27060304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 27070304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 27080304c731SJamie Gritton return (0); 27090304c731SJamie Gritton } 2710b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2711b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2712b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2713b38ff370SJamie Gritton } 2714413628a7SBjoern A. Zeeb 2715b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2716b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2717413628a7SBjoern A. Zeeb return (0); 271875c13541SPoul-Henning Kamp } 2719413628a7SBjoern A. Zeeb 2720413628a7SBjoern A. Zeeb /* 2721592bcae8SBjoern A. Zeeb * Return 1 if we should do proper source address selection or are not jailed. 2722592bcae8SBjoern A. Zeeb * We will return 0 if we should bypass source address selection in favour 2723592bcae8SBjoern A. Zeeb * of the primary jail IPv4 address. Only in this case *ia will be updated and 2724592bcae8SBjoern A. Zeeb * returned in NBO. 2725592bcae8SBjoern A. Zeeb * Return EAFNOSUPPORT, in case this jail does not allow IPv4. 2726592bcae8SBjoern A. Zeeb */ 2727592bcae8SBjoern A. Zeeb int 2728592bcae8SBjoern A. Zeeb prison_saddrsel_ip4(struct ucred *cred, struct in_addr *ia) 2729592bcae8SBjoern A. Zeeb { 2730592bcae8SBjoern A. Zeeb struct prison *pr; 2731592bcae8SBjoern A. Zeeb struct in_addr lia; 2732592bcae8SBjoern A. Zeeb int error; 2733592bcae8SBjoern A. Zeeb 2734592bcae8SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2735592bcae8SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2736592bcae8SBjoern A. Zeeb 2737592bcae8SBjoern A. Zeeb if (!jailed(cred)) 2738592bcae8SBjoern A. Zeeb return (1); 2739592bcae8SBjoern A. Zeeb 2740592bcae8SBjoern A. Zeeb pr = cred->cr_prison; 2741592bcae8SBjoern A. Zeeb if (pr->pr_flags & PR_IP4_SADDRSEL) 2742592bcae8SBjoern A. Zeeb return (1); 2743592bcae8SBjoern A. Zeeb 2744592bcae8SBjoern A. Zeeb lia.s_addr = INADDR_ANY; 2745592bcae8SBjoern A. Zeeb error = prison_get_ip4(cred, &lia); 2746592bcae8SBjoern A. Zeeb if (error) 2747592bcae8SBjoern A. Zeeb return (error); 2748592bcae8SBjoern A. Zeeb if (lia.s_addr == INADDR_ANY) 2749592bcae8SBjoern A. Zeeb return (1); 2750592bcae8SBjoern A. Zeeb 2751592bcae8SBjoern A. Zeeb ia->s_addr = lia.s_addr; 2752592bcae8SBjoern A. Zeeb return (0); 2753592bcae8SBjoern A. Zeeb } 2754592bcae8SBjoern A. Zeeb 2755592bcae8SBjoern A. Zeeb /* 27560304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv4 address restrictions. 27570304c731SJamie Gritton */ 27580304c731SJamie Gritton int 27590304c731SJamie Gritton prison_equal_ip4(struct prison *pr1, struct prison *pr2) 27600304c731SJamie Gritton { 27610304c731SJamie Gritton 27620304c731SJamie Gritton if (pr1 == pr2) 27630304c731SJamie Gritton return (1); 27640304c731SJamie Gritton 27650304c731SJamie Gritton /* 27662b0d6f81SJamie Gritton * No need to lock since the PR_IP4_USER flag can't be altered for 27672b0d6f81SJamie Gritton * existing prisons. 27680304c731SJamie Gritton */ 2769bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 2770bdfc8cc4SJamie Gritton #ifdef VIMAGE 2771bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 2772bdfc8cc4SJamie Gritton #endif 2773bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP4_USER)) 27740304c731SJamie Gritton pr1 = pr1->pr_parent; 2775bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 2776bdfc8cc4SJamie Gritton #ifdef VIMAGE 2777bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 2778bdfc8cc4SJamie Gritton #endif 2779bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP4_USER)) 27800304c731SJamie Gritton pr2 = pr2->pr_parent; 27810304c731SJamie Gritton return (pr1 == pr2); 27820304c731SJamie Gritton } 27830304c731SJamie Gritton 27840304c731SJamie Gritton /* 2785413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this 2786413628a7SBjoern A. Zeeb * jail. 2787413628a7SBjoern A. Zeeb * 27880304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 27890304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 27900304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO and returned in NBO. 2791413628a7SBjoern A. Zeeb */ 2792413628a7SBjoern A. Zeeb int 2793413628a7SBjoern A. Zeeb prison_local_ip4(struct ucred *cred, struct in_addr *ia) 2794413628a7SBjoern A. Zeeb { 2795b38ff370SJamie Gritton struct prison *pr; 2796413628a7SBjoern A. Zeeb struct in_addr ia0; 2797b38ff370SJamie Gritton int error; 2798413628a7SBjoern A. Zeeb 2799413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2800413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2801413628a7SBjoern A. Zeeb 2802b38ff370SJamie Gritton pr = cred->cr_prison; 28030304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 28040304c731SJamie Gritton return (0); 2805b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 28060304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 28070304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28080304c731SJamie Gritton return (0); 28090304c731SJamie Gritton } 2810b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2811b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2812b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2813b38ff370SJamie Gritton } 2814413628a7SBjoern A. Zeeb 2815413628a7SBjoern A. Zeeb ia0.s_addr = ntohl(ia->s_addr); 2816413628a7SBjoern A. Zeeb if (ia0.s_addr == INADDR_LOOPBACK) { 2817b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2818b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2819413628a7SBjoern A. Zeeb return (0); 2820413628a7SBjoern A. Zeeb } 2821413628a7SBjoern A. Zeeb 2822b89e82ddSJamie Gritton if (ia0.s_addr == INADDR_ANY) { 2823413628a7SBjoern A. Zeeb /* 2824413628a7SBjoern A. Zeeb * In case there is only 1 IPv4 address, bind directly. 2825413628a7SBjoern A. Zeeb */ 2826b38ff370SJamie Gritton if (pr->pr_ip4s == 1) 2827b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2828b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2829413628a7SBjoern A. Zeeb return (0); 2830413628a7SBjoern A. Zeeb } 2831413628a7SBjoern A. Zeeb 2832b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2833b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2834b38ff370SJamie Gritton return (error); 2835413628a7SBjoern A. Zeeb } 2836413628a7SBjoern A. Zeeb 2837413628a7SBjoern A. Zeeb /* 2838413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 2839413628a7SBjoern A. Zeeb * 2840b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv4. 2841b89e82ddSJamie Gritton * Address passed in in NBO and returned in NBO. 2842413628a7SBjoern A. Zeeb */ 2843413628a7SBjoern A. Zeeb int 2844413628a7SBjoern A. Zeeb prison_remote_ip4(struct ucred *cred, struct in_addr *ia) 2845413628a7SBjoern A. Zeeb { 2846b38ff370SJamie Gritton struct prison *pr; 2847413628a7SBjoern A. Zeeb 2848413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2849413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2850413628a7SBjoern A. Zeeb 2851b38ff370SJamie Gritton pr = cred->cr_prison; 28520304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 28530304c731SJamie Gritton return (0); 2854b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 28550304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 28560304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 28570304c731SJamie Gritton return (0); 28580304c731SJamie Gritton } 2859b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2860b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2861b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2862b38ff370SJamie Gritton } 2863b89e82ddSJamie Gritton 2864413628a7SBjoern A. Zeeb if (ntohl(ia->s_addr) == INADDR_LOOPBACK) { 2865b38ff370SJamie Gritton ia->s_addr = pr->pr_ip4[0].s_addr; 2866b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2867413628a7SBjoern A. Zeeb return (0); 2868413628a7SBjoern A. Zeeb } 2869413628a7SBjoern A. Zeeb 2870413628a7SBjoern A. Zeeb /* 2871413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 2872413628a7SBjoern A. Zeeb */ 2873b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2874413628a7SBjoern A. Zeeb return (0); 2875413628a7SBjoern A. Zeeb } 2876413628a7SBjoern A. Zeeb 2877413628a7SBjoern A. Zeeb /* 2878b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 2879413628a7SBjoern A. Zeeb * 28800304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv4 or if address belongs to jail, 28810304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 28820304c731SJamie Gritton * doesn't allow IPv4. Address passed in in NBO. 2883413628a7SBjoern A. Zeeb */ 2884413628a7SBjoern A. Zeeb static int 2885413628a7SBjoern A. Zeeb _prison_check_ip4(struct prison *pr, struct in_addr *ia) 2886413628a7SBjoern A. Zeeb { 2887413628a7SBjoern A. Zeeb int i, a, z, d; 2888413628a7SBjoern A. Zeeb 2889413628a7SBjoern A. Zeeb /* 2890413628a7SBjoern A. Zeeb * Check the primary IP. 2891413628a7SBjoern A. Zeeb */ 2892413628a7SBjoern A. Zeeb if (pr->pr_ip4[0].s_addr == ia->s_addr) 2893b89e82ddSJamie Gritton return (0); 2894413628a7SBjoern A. Zeeb 2895413628a7SBjoern A. Zeeb /* 2896413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 2897413628a7SBjoern A. Zeeb */ 2898413628a7SBjoern A. Zeeb a = 0; 2899413628a7SBjoern A. Zeeb z = pr->pr_ip4s - 2; 2900413628a7SBjoern A. Zeeb while (a <= z) { 2901413628a7SBjoern A. Zeeb i = (a + z) / 2; 2902413628a7SBjoern A. Zeeb d = qcmp_v4(&pr->pr_ip4[i+1], ia); 2903413628a7SBjoern A. Zeeb if (d > 0) 2904413628a7SBjoern A. Zeeb z = i - 1; 2905413628a7SBjoern A. Zeeb else if (d < 0) 2906413628a7SBjoern A. Zeeb a = i + 1; 2907413628a7SBjoern A. Zeeb else 2908413628a7SBjoern A. Zeeb return (0); 290975c13541SPoul-Henning Kamp } 291075c13541SPoul-Henning Kamp 2911b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 2912b89e82ddSJamie Gritton } 2913b89e82ddSJamie Gritton 291475c13541SPoul-Henning Kamp int 2915413628a7SBjoern A. Zeeb prison_check_ip4(struct ucred *cred, struct in_addr *ia) 2916413628a7SBjoern A. Zeeb { 2917b38ff370SJamie Gritton struct prison *pr; 2918b38ff370SJamie Gritton int error; 2919413628a7SBjoern A. Zeeb 2920413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 2921413628a7SBjoern A. Zeeb KASSERT(ia != NULL, ("%s: ia is NULL", __func__)); 2922413628a7SBjoern A. Zeeb 2923b38ff370SJamie Gritton pr = cred->cr_prison; 29240304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) 29250304c731SJamie Gritton return (0); 2926b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 29270304c731SJamie Gritton if (!(pr->pr_flags & PR_IP4)) { 29280304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 29290304c731SJamie Gritton return (0); 29300304c731SJamie Gritton } 2931b38ff370SJamie Gritton if (pr->pr_ip4 == NULL) { 2932b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2933b89e82ddSJamie Gritton return (EAFNOSUPPORT); 2934b38ff370SJamie Gritton } 2935413628a7SBjoern A. Zeeb 2936b38ff370SJamie Gritton error = _prison_check_ip4(pr, ia); 2937b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2938b38ff370SJamie Gritton return (error); 2939413628a7SBjoern A. Zeeb } 2940413628a7SBjoern A. Zeeb #endif 2941413628a7SBjoern A. Zeeb 2942413628a7SBjoern A. Zeeb #ifdef INET6 29430304c731SJamie Gritton static int 29440304c731SJamie Gritton prison_restrict_ip6(struct prison *pr, struct in6_addr *newip6) 29450304c731SJamie Gritton { 29460304c731SJamie Gritton int ii, ij, used; 29470304c731SJamie Gritton struct prison *ppr; 29480304c731SJamie Gritton 29490304c731SJamie Gritton ppr = pr->pr_parent; 29500304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6_USER)) { 29510304c731SJamie Gritton /* This has no user settings, so just copy the parent's list. */ 29520304c731SJamie Gritton if (pr->pr_ip6s < ppr->pr_ip6s) { 29530304c731SJamie Gritton /* 29540304c731SJamie Gritton * There's no room for the parent's list. Use the 29550304c731SJamie Gritton * new list buffer, which is assumed to be big enough 29560304c731SJamie Gritton * (if it was passed). If there's no buffer, try to 29570304c731SJamie Gritton * allocate one. 29580304c731SJamie Gritton */ 29590304c731SJamie Gritton used = 1; 29600304c731SJamie Gritton if (newip6 == NULL) { 29610304c731SJamie Gritton newip6 = malloc(ppr->pr_ip6s * sizeof(*newip6), 29620304c731SJamie Gritton M_PRISON, M_NOWAIT); 29630304c731SJamie Gritton if (newip6 != NULL) 29640304c731SJamie Gritton used = 0; 29650304c731SJamie Gritton } 29660304c731SJamie Gritton if (newip6 != NULL) { 29670304c731SJamie Gritton bcopy(ppr->pr_ip6, newip6, 29680304c731SJamie Gritton ppr->pr_ip6s * sizeof(*newip6)); 29690304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 29700304c731SJamie Gritton pr->pr_ip6 = newip6; 29710304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 29720304c731SJamie Gritton } 29730304c731SJamie Gritton return (used); 29740304c731SJamie Gritton } 29750304c731SJamie Gritton pr->pr_ip6s = ppr->pr_ip6s; 29760304c731SJamie Gritton if (pr->pr_ip6s > 0) 29770304c731SJamie Gritton bcopy(ppr->pr_ip6, pr->pr_ip6, 29780304c731SJamie Gritton pr->pr_ip6s * sizeof(*newip6)); 29790304c731SJamie Gritton else if (pr->pr_ip6 != NULL) { 29800304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 29810304c731SJamie Gritton pr->pr_ip6 = NULL; 29820304c731SJamie Gritton } 29832b0d6f81SJamie Gritton } else if (pr->pr_ip6s > 0) { 29840304c731SJamie Gritton /* Remove addresses that aren't in the parent. */ 29850304c731SJamie Gritton for (ij = 0; ij < ppr->pr_ip6s; ij++) 29860304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], 29870304c731SJamie Gritton &ppr->pr_ip6[ij])) 29880304c731SJamie Gritton break; 29890304c731SJamie Gritton if (ij < ppr->pr_ip6s) 29900304c731SJamie Gritton ii = 1; 29910304c731SJamie Gritton else { 29920304c731SJamie Gritton bcopy(pr->pr_ip6 + 1, pr->pr_ip6, 29930304c731SJamie Gritton --pr->pr_ip6s * sizeof(*pr->pr_ip6)); 29940304c731SJamie Gritton ii = 0; 29950304c731SJamie Gritton } 29960304c731SJamie Gritton for (ij = 1; ii < pr->pr_ip6s; ) { 29970304c731SJamie Gritton if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[ii], 29980304c731SJamie Gritton &ppr->pr_ip6[0])) { 29990304c731SJamie Gritton ii++; 30000304c731SJamie Gritton continue; 30010304c731SJamie Gritton } 30020304c731SJamie Gritton switch (ij >= ppr->pr_ip4s ? -1 : 30030304c731SJamie Gritton qcmp_v6(&pr->pr_ip6[ii], &ppr->pr_ip6[ij])) { 30040304c731SJamie Gritton case -1: 30050304c731SJamie Gritton bcopy(pr->pr_ip6 + ii + 1, pr->pr_ip6 + ii, 30060304c731SJamie Gritton (--pr->pr_ip6s - ii) * sizeof(*pr->pr_ip6)); 30070304c731SJamie Gritton break; 30080304c731SJamie Gritton case 0: 30090304c731SJamie Gritton ii++; 30100304c731SJamie Gritton ij++; 30110304c731SJamie Gritton break; 30120304c731SJamie Gritton case 1: 30130304c731SJamie Gritton ij++; 30140304c731SJamie Gritton break; 30150304c731SJamie Gritton } 30160304c731SJamie Gritton } 30170304c731SJamie Gritton if (pr->pr_ip6s == 0) { 30187cbf7213SJamie Gritton pr->pr_flags |= PR_IP6_DISABLE; 30190304c731SJamie Gritton free(pr->pr_ip6, M_PRISON); 30200304c731SJamie Gritton pr->pr_ip6 = NULL; 30210304c731SJamie Gritton } 30220304c731SJamie Gritton } 30230304c731SJamie Gritton return 0; 30240304c731SJamie Gritton } 30250304c731SJamie Gritton 3026413628a7SBjoern A. Zeeb /* 3027413628a7SBjoern A. Zeeb * Pass back primary IPv6 address for this jail. 3028413628a7SBjoern A. Zeeb * 30290304c731SJamie Gritton * If not restricted return success but do not alter the address. Caller has 30300304c731SJamie Gritton * to make sure to initialize it correctly (e.g. IN6ADDR_ANY_INIT). 3031413628a7SBjoern A. Zeeb * 3032b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3033413628a7SBjoern A. Zeeb */ 3034413628a7SBjoern A. Zeeb int 30351cecba0fSBjoern A. Zeeb prison_get_ip6(struct ucred *cred, struct in6_addr *ia6) 3036413628a7SBjoern A. Zeeb { 3037b38ff370SJamie Gritton struct prison *pr; 3038413628a7SBjoern A. Zeeb 3039413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3040413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3041413628a7SBjoern A. Zeeb 3042b38ff370SJamie Gritton pr = cred->cr_prison; 30430304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 30440304c731SJamie Gritton return (0); 3045b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 30460304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 30470304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 30480304c731SJamie Gritton return (0); 30490304c731SJamie Gritton } 3050b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3051b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3052b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3053b38ff370SJamie Gritton } 3054b89e82ddSJamie Gritton 3055b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3056b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3057413628a7SBjoern A. Zeeb return (0); 3058413628a7SBjoern A. Zeeb } 3059413628a7SBjoern A. Zeeb 3060413628a7SBjoern A. Zeeb /* 3061592bcae8SBjoern A. Zeeb * Return 1 if we should do proper source address selection or are not jailed. 3062592bcae8SBjoern A. Zeeb * We will return 0 if we should bypass source address selection in favour 3063592bcae8SBjoern A. Zeeb * of the primary jail IPv6 address. Only in this case *ia will be updated and 3064592bcae8SBjoern A. Zeeb * returned in NBO. 3065592bcae8SBjoern A. Zeeb * Return EAFNOSUPPORT, in case this jail does not allow IPv6. 3066592bcae8SBjoern A. Zeeb */ 3067592bcae8SBjoern A. Zeeb int 3068592bcae8SBjoern A. Zeeb prison_saddrsel_ip6(struct ucred *cred, struct in6_addr *ia6) 3069592bcae8SBjoern A. Zeeb { 3070592bcae8SBjoern A. Zeeb struct prison *pr; 3071592bcae8SBjoern A. Zeeb struct in6_addr lia6; 3072592bcae8SBjoern A. Zeeb int error; 3073592bcae8SBjoern A. Zeeb 3074592bcae8SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3075592bcae8SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3076592bcae8SBjoern A. Zeeb 3077592bcae8SBjoern A. Zeeb if (!jailed(cred)) 3078592bcae8SBjoern A. Zeeb return (1); 3079592bcae8SBjoern A. Zeeb 3080592bcae8SBjoern A. Zeeb pr = cred->cr_prison; 3081592bcae8SBjoern A. Zeeb if (pr->pr_flags & PR_IP6_SADDRSEL) 3082592bcae8SBjoern A. Zeeb return (1); 3083592bcae8SBjoern A. Zeeb 3084592bcae8SBjoern A. Zeeb lia6 = in6addr_any; 3085592bcae8SBjoern A. Zeeb error = prison_get_ip6(cred, &lia6); 3086592bcae8SBjoern A. Zeeb if (error) 3087592bcae8SBjoern A. Zeeb return (error); 3088592bcae8SBjoern A. Zeeb if (IN6_IS_ADDR_UNSPECIFIED(&lia6)) 3089592bcae8SBjoern A. Zeeb return (1); 3090592bcae8SBjoern A. Zeeb 3091592bcae8SBjoern A. Zeeb bcopy(&lia6, ia6, sizeof(struct in6_addr)); 3092592bcae8SBjoern A. Zeeb return (0); 3093592bcae8SBjoern A. Zeeb } 3094592bcae8SBjoern A. Zeeb 3095592bcae8SBjoern A. Zeeb /* 30960304c731SJamie Gritton * Return true if pr1 and pr2 have the same IPv6 address restrictions. 30970304c731SJamie Gritton */ 30980304c731SJamie Gritton int 30990304c731SJamie Gritton prison_equal_ip6(struct prison *pr1, struct prison *pr2) 31000304c731SJamie Gritton { 31010304c731SJamie Gritton 31020304c731SJamie Gritton if (pr1 == pr2) 31030304c731SJamie Gritton return (1); 31040304c731SJamie Gritton 3105bdfc8cc4SJamie Gritton while (pr1 != &prison0 && 3106bdfc8cc4SJamie Gritton #ifdef VIMAGE 3107bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_VNET) && 3108bdfc8cc4SJamie Gritton #endif 3109bdfc8cc4SJamie Gritton !(pr1->pr_flags & PR_IP6_USER)) 31100304c731SJamie Gritton pr1 = pr1->pr_parent; 3111bdfc8cc4SJamie Gritton while (pr2 != &prison0 && 3112bdfc8cc4SJamie Gritton #ifdef VIMAGE 3113bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_VNET) && 3114bdfc8cc4SJamie Gritton #endif 3115bdfc8cc4SJamie Gritton !(pr2->pr_flags & PR_IP6_USER)) 31160304c731SJamie Gritton pr2 = pr2->pr_parent; 31170304c731SJamie Gritton return (pr1 == pr2); 31180304c731SJamie Gritton } 31190304c731SJamie Gritton 31200304c731SJamie Gritton /* 3121413628a7SBjoern A. Zeeb * Make sure our (source) address is set to something meaningful to this jail. 3122413628a7SBjoern A. Zeeb * 3123413628a7SBjoern A. Zeeb * v6only should be set based on (inp->inp_flags & IN6P_IPV6_V6ONLY != 0) 3124413628a7SBjoern A. Zeeb * when needed while binding. 3125413628a7SBjoern A. Zeeb * 31260304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 31270304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 31280304c731SJamie Gritton * doesn't allow IPv6. 3129413628a7SBjoern A. Zeeb */ 3130413628a7SBjoern A. Zeeb int 3131413628a7SBjoern A. Zeeb prison_local_ip6(struct ucred *cred, struct in6_addr *ia6, int v6only) 3132413628a7SBjoern A. Zeeb { 3133b38ff370SJamie Gritton struct prison *pr; 3134b38ff370SJamie Gritton int error; 3135413628a7SBjoern A. Zeeb 3136413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3137413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3138413628a7SBjoern A. Zeeb 3139b38ff370SJamie Gritton pr = cred->cr_prison; 31400304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 31410304c731SJamie Gritton return (0); 3142b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 31430304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 31440304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31450304c731SJamie Gritton return (0); 31460304c731SJamie Gritton } 3147b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3148b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3149b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3150b38ff370SJamie Gritton } 3151b89e82ddSJamie Gritton 3152413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3153b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3154b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3155413628a7SBjoern A. Zeeb return (0); 3156413628a7SBjoern A. Zeeb } 3157413628a7SBjoern A. Zeeb 3158b89e82ddSJamie Gritton if (IN6_IS_ADDR_UNSPECIFIED(ia6)) { 3159413628a7SBjoern A. Zeeb /* 3160b89e82ddSJamie Gritton * In case there is only 1 IPv6 address, and v6only is true, 3161b89e82ddSJamie Gritton * then bind directly. 3162413628a7SBjoern A. Zeeb */ 3163b38ff370SJamie Gritton if (v6only != 0 && pr->pr_ip6s == 1) 3164b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3165b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3166413628a7SBjoern A. Zeeb return (0); 3167413628a7SBjoern A. Zeeb } 3168b89e82ddSJamie Gritton 3169b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3170b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3171b38ff370SJamie Gritton return (error); 3172413628a7SBjoern A. Zeeb } 3173413628a7SBjoern A. Zeeb 3174413628a7SBjoern A. Zeeb /* 3175413628a7SBjoern A. Zeeb * Rewrite destination address in case we will connect to loopback address. 3176413628a7SBjoern A. Zeeb * 3177b89e82ddSJamie Gritton * Returns 0 on success, EAFNOSUPPORT if the jail doesn't allow IPv6. 3178413628a7SBjoern A. Zeeb */ 3179413628a7SBjoern A. Zeeb int 3180413628a7SBjoern A. Zeeb prison_remote_ip6(struct ucred *cred, struct in6_addr *ia6) 3181413628a7SBjoern A. Zeeb { 3182b38ff370SJamie Gritton struct prison *pr; 3183413628a7SBjoern A. Zeeb 3184413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3185413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3186413628a7SBjoern A. Zeeb 3187b38ff370SJamie Gritton pr = cred->cr_prison; 31880304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 31890304c731SJamie Gritton return (0); 3190b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 31910304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 31920304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 31930304c731SJamie Gritton return (0); 31940304c731SJamie Gritton } 3195b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3196b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3197b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3198b38ff370SJamie Gritton } 3199b89e82ddSJamie Gritton 3200413628a7SBjoern A. Zeeb if (IN6_IS_ADDR_LOOPBACK(ia6)) { 3201b38ff370SJamie Gritton bcopy(&pr->pr_ip6[0], ia6, sizeof(struct in6_addr)); 3202b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3203413628a7SBjoern A. Zeeb return (0); 3204413628a7SBjoern A. Zeeb } 3205413628a7SBjoern A. Zeeb 3206413628a7SBjoern A. Zeeb /* 3207413628a7SBjoern A. Zeeb * Return success because nothing had to be changed. 3208413628a7SBjoern A. Zeeb */ 3209b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3210413628a7SBjoern A. Zeeb return (0); 3211413628a7SBjoern A. Zeeb } 3212413628a7SBjoern A. Zeeb 3213413628a7SBjoern A. Zeeb /* 3214b89e82ddSJamie Gritton * Check if given address belongs to the jail referenced by cred/prison. 3215413628a7SBjoern A. Zeeb * 32160304c731SJamie Gritton * Returns 0 if jail doesn't restrict IPv6 or if address belongs to jail, 32170304c731SJamie Gritton * EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if the jail 32180304c731SJamie Gritton * doesn't allow IPv6. 3219413628a7SBjoern A. Zeeb */ 3220413628a7SBjoern A. Zeeb static int 3221413628a7SBjoern A. Zeeb _prison_check_ip6(struct prison *pr, struct in6_addr *ia6) 3222413628a7SBjoern A. Zeeb { 3223413628a7SBjoern A. Zeeb int i, a, z, d; 3224413628a7SBjoern A. Zeeb 3225413628a7SBjoern A. Zeeb /* 3226413628a7SBjoern A. Zeeb * Check the primary IP. 3227413628a7SBjoern A. Zeeb */ 3228413628a7SBjoern A. Zeeb if (IN6_ARE_ADDR_EQUAL(&pr->pr_ip6[0], ia6)) 3229b89e82ddSJamie Gritton return (0); 3230413628a7SBjoern A. Zeeb 3231413628a7SBjoern A. Zeeb /* 3232413628a7SBjoern A. Zeeb * All the other IPs are sorted so we can do a binary search. 3233413628a7SBjoern A. Zeeb */ 3234413628a7SBjoern A. Zeeb a = 0; 3235413628a7SBjoern A. Zeeb z = pr->pr_ip6s - 2; 3236413628a7SBjoern A. Zeeb while (a <= z) { 3237413628a7SBjoern A. Zeeb i = (a + z) / 2; 3238413628a7SBjoern A. Zeeb d = qcmp_v6(&pr->pr_ip6[i+1], ia6); 3239413628a7SBjoern A. Zeeb if (d > 0) 3240413628a7SBjoern A. Zeeb z = i - 1; 3241413628a7SBjoern A. Zeeb else if (d < 0) 3242413628a7SBjoern A. Zeeb a = i + 1; 3243413628a7SBjoern A. Zeeb else 3244413628a7SBjoern A. Zeeb return (0); 3245413628a7SBjoern A. Zeeb } 3246413628a7SBjoern A. Zeeb 3247b89e82ddSJamie Gritton return (EADDRNOTAVAIL); 3248b89e82ddSJamie Gritton } 3249b89e82ddSJamie Gritton 3250413628a7SBjoern A. Zeeb int 3251413628a7SBjoern A. Zeeb prison_check_ip6(struct ucred *cred, struct in6_addr *ia6) 3252413628a7SBjoern A. Zeeb { 3253b38ff370SJamie Gritton struct prison *pr; 3254b38ff370SJamie Gritton int error; 3255413628a7SBjoern A. Zeeb 3256413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3257413628a7SBjoern A. Zeeb KASSERT(ia6 != NULL, ("%s: ia6 is NULL", __func__)); 3258413628a7SBjoern A. Zeeb 3259b38ff370SJamie Gritton pr = cred->cr_prison; 32600304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) 32610304c731SJamie Gritton return (0); 3262b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 32630304c731SJamie Gritton if (!(pr->pr_flags & PR_IP6)) { 32640304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 32650304c731SJamie Gritton return (0); 32660304c731SJamie Gritton } 3267b38ff370SJamie Gritton if (pr->pr_ip6 == NULL) { 3268b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3269b89e82ddSJamie Gritton return (EAFNOSUPPORT); 3270b38ff370SJamie Gritton } 3271413628a7SBjoern A. Zeeb 3272b38ff370SJamie Gritton error = _prison_check_ip6(pr, ia6); 3273b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 3274b38ff370SJamie Gritton return (error); 3275413628a7SBjoern A. Zeeb } 3276413628a7SBjoern A. Zeeb #endif 3277413628a7SBjoern A. Zeeb 3278413628a7SBjoern A. Zeeb /* 3279ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3280ca04ba64SJamie Gritton * 3281ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3282ca04ba64SJamie Gritton * if not. 3283ca04ba64SJamie Gritton */ 3284ca04ba64SJamie Gritton int 3285ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3286ca04ba64SJamie Gritton { 32870304c731SJamie Gritton struct prison *pr; 3288ca04ba64SJamie Gritton int error; 3289ca04ba64SJamie Gritton 3290ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3291ca04ba64SJamie Gritton 32920304c731SJamie Gritton pr = cred->cr_prison; 3293499650a0SJamie Gritton #ifdef VIMAGE 32946bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3295de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 32966bb79563SJamie Gritton return (0); 3297499650a0SJamie Gritton #endif 32986bb79563SJamie Gritton 3299ca04ba64SJamie Gritton error = 0; 3300ca04ba64SJamie Gritton switch (af) 3301ca04ba64SJamie Gritton { 3302ca04ba64SJamie Gritton #ifdef INET 3303ca04ba64SJamie Gritton case AF_INET: 33040304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 33050304c731SJamie Gritton { 33060304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 33070304c731SJamie Gritton if ((pr->pr_flags & PR_IP4) && pr->pr_ip4 == NULL) 3308ca04ba64SJamie Gritton error = EAFNOSUPPORT; 33090304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33100304c731SJamie Gritton } 3311ca04ba64SJamie Gritton break; 3312ca04ba64SJamie Gritton #endif 3313ca04ba64SJamie Gritton #ifdef INET6 3314ca04ba64SJamie Gritton case AF_INET6: 33150304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 33160304c731SJamie Gritton { 33170304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 33180304c731SJamie Gritton if ((pr->pr_flags & PR_IP6) && pr->pr_ip6 == NULL) 3319ca04ba64SJamie Gritton error = EAFNOSUPPORT; 33200304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33210304c731SJamie Gritton } 3322ca04ba64SJamie Gritton break; 3323ca04ba64SJamie Gritton #endif 3324ca04ba64SJamie Gritton case AF_LOCAL: 3325ca04ba64SJamie Gritton case AF_ROUTE: 3326ca04ba64SJamie Gritton break; 3327ca04ba64SJamie Gritton default: 33280304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3329ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3330ca04ba64SJamie Gritton } 3331ca04ba64SJamie Gritton return (error); 3332ca04ba64SJamie Gritton } 3333ca04ba64SJamie Gritton 3334ca04ba64SJamie Gritton /* 3335413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3336413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3337413628a7SBjoern A. Zeeb * 33380304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 33390304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 33400304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3341413628a7SBjoern A. Zeeb */ 3342413628a7SBjoern A. Zeeb int 334391421ba2SRobert Watson prison_if(struct ucred *cred, struct sockaddr *sa) 334475c13541SPoul-Henning Kamp { 3345413628a7SBjoern A. Zeeb #ifdef INET 33469ddb7954SMike Barcroft struct sockaddr_in *sai; 3347413628a7SBjoern A. Zeeb #endif 3348413628a7SBjoern A. Zeeb #ifdef INET6 3349413628a7SBjoern A. Zeeb struct sockaddr_in6 *sai6; 3350413628a7SBjoern A. Zeeb #endif 3351b89e82ddSJamie Gritton int error; 335275c13541SPoul-Henning Kamp 3353413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3354413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3355413628a7SBjoern A. Zeeb 3356de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3357de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3358de0bd6f7SBjoern A. Zeeb return (0); 3359de0bd6f7SBjoern A. Zeeb #endif 3360de0bd6f7SBjoern A. Zeeb 3361b89e82ddSJamie Gritton error = 0; 3362413628a7SBjoern A. Zeeb switch (sa->sa_family) 3363413628a7SBjoern A. Zeeb { 3364413628a7SBjoern A. Zeeb #ifdef INET 3365413628a7SBjoern A. Zeeb case AF_INET: 33669ddb7954SMike Barcroft sai = (struct sockaddr_in *)sa; 3367b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3368413628a7SBjoern A. Zeeb break; 3369413628a7SBjoern A. Zeeb #endif 3370413628a7SBjoern A. Zeeb #ifdef INET6 3371413628a7SBjoern A. Zeeb case AF_INET6: 3372413628a7SBjoern A. Zeeb sai6 = (struct sockaddr_in6 *)sa; 3373b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3374413628a7SBjoern A. Zeeb break; 3375413628a7SBjoern A. Zeeb #endif 3376413628a7SBjoern A. Zeeb default: 33770304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3378b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3379413628a7SBjoern A. Zeeb } 3380b89e82ddSJamie Gritton return (error); 338175c13541SPoul-Henning Kamp } 338291421ba2SRobert Watson 338391421ba2SRobert Watson /* 338491421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 338591421ba2SRobert Watson */ 338691421ba2SRobert Watson int 33879ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 338891421ba2SRobert Watson { 338991421ba2SRobert Watson 33900304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 33910304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 33920304c731SJamie Gritton } 339391421ba2SRobert Watson 33940304c731SJamie Gritton /* 33950304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 33960304c731SJamie Gritton */ 33970304c731SJamie Gritton int 33980304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 33990304c731SJamie Gritton { 34000304c731SJamie Gritton 34010304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 34020304c731SJamie Gritton if (pr1 == pr2) 34030304c731SJamie Gritton return (1); 340491421ba2SRobert Watson return (0); 340591421ba2SRobert Watson } 340691421ba2SRobert Watson 340791421ba2SRobert Watson /* 340891421ba2SRobert Watson * Return 1 if the passed credential is in a jail, otherwise 0. 340991421ba2SRobert Watson */ 341091421ba2SRobert Watson int 34119ddb7954SMike Barcroft jailed(struct ucred *cred) 341291421ba2SRobert Watson { 341391421ba2SRobert Watson 34140304c731SJamie Gritton return (cred->cr_prison != &prison0); 341591421ba2SRobert Watson } 34169484d0c0SRobert Drehmel 34179484d0c0SRobert Drehmel /* 3418de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 3419de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 3420de0bd6f7SBjoern A. Zeeb */ 3421de0bd6f7SBjoern A. Zeeb int 3422de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3423de0bd6f7SBjoern A. Zeeb { 3424de0bd6f7SBjoern A. Zeeb 3425de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 3426de0bd6f7SBjoern A. Zeeb return (0); 3427de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3428de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3429de0bd6f7SBjoern A. Zeeb return (0); 3430de0bd6f7SBjoern A. Zeeb #endif 3431de0bd6f7SBjoern A. Zeeb 3432de0bd6f7SBjoern A. Zeeb return (1); 3433de0bd6f7SBjoern A. Zeeb } 3434de0bd6f7SBjoern A. Zeeb 3435de0bd6f7SBjoern A. Zeeb /* 34367455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 34379484d0c0SRobert Drehmel */ 3438ad1ff099SRobert Drehmel void 34399ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 34409484d0c0SRobert Drehmel { 344176ca6f88SJamie Gritton struct prison *pr; 34429484d0c0SRobert Drehmel 34437455b100SJamie Gritton /* 34447455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 34457455b100SJamie Gritton * system's hostname. 34467455b100SJamie Gritton */ 344776ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 344876ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3449c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 345076ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 34519484d0c0SRobert Drehmel } 3452fd7a8150SMike Barcroft 34537455b100SJamie Gritton void 34547455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 34557455b100SJamie Gritton { 34567455b100SJamie Gritton 34577455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3458c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 34597455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34607455b100SJamie Gritton } 34617455b100SJamie Gritton 34627455b100SJamie Gritton void 34637455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 34647455b100SJamie Gritton { 34657455b100SJamie Gritton 34667455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3467c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 34687455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34697455b100SJamie Gritton } 34707455b100SJamie Gritton 34717455b100SJamie Gritton void 34727455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 34737455b100SJamie Gritton { 34747455b100SJamie Gritton 34757455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 34767455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 34777455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34787455b100SJamie Gritton } 34797455b100SJamie Gritton 3480eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3481eb79e1c7SBjoern A. Zeeb /* 3482eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3483eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3484eb79e1c7SBjoern A. Zeeb * 3485eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3486eb79e1c7SBjoern A. Zeeb */ 3487eb79e1c7SBjoern A. Zeeb int 3488eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3489eb79e1c7SBjoern A. Zeeb { 3490eb79e1c7SBjoern A. Zeeb 3491eb79e1c7SBjoern A. Zeeb /* 3492eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3493eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3494eb79e1c7SBjoern A. Zeeb */ 3495eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3496eb79e1c7SBjoern A. Zeeb } 3497eb79e1c7SBjoern A. Zeeb #endif 3498eb79e1c7SBjoern A. Zeeb 3499f08df373SRobert Watson /* 3500820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3501820a0de9SPawel Jakub Dawidek * status of a mount point. 3502820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3503820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3504820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3505f08df373SRobert Watson */ 3506f08df373SRobert Watson int 3507820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3508f08df373SRobert Watson { 3509820a0de9SPawel Jakub Dawidek struct prison *pr; 3510820a0de9SPawel Jakub Dawidek struct statfs *sp; 3511820a0de9SPawel Jakub Dawidek size_t len; 3512f08df373SRobert Watson 3513820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 35140304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 35150304c731SJamie Gritton return (0); 3516820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3517820a0de9SPawel Jakub Dawidek return (0); 35180304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3519820a0de9SPawel Jakub Dawidek return (ENOENT); 3520820a0de9SPawel Jakub Dawidek /* 3521820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3522820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3523820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3524820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3525820a0de9SPawel Jakub Dawidek */ 3526820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3527820a0de9SPawel Jakub Dawidek return (0); 3528820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3529820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3530820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3531820a0de9SPawel Jakub Dawidek return (ENOENT); 3532820a0de9SPawel Jakub Dawidek /* 3533820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3534820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3535820a0de9SPawel Jakub Dawidek */ 3536820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3537820a0de9SPawel Jakub Dawidek return (ENOENT); 3538f08df373SRobert Watson return (0); 3539f08df373SRobert Watson } 3540820a0de9SPawel Jakub Dawidek 3541820a0de9SPawel Jakub Dawidek void 3542820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3543820a0de9SPawel Jakub Dawidek { 3544820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3545820a0de9SPawel Jakub Dawidek struct prison *pr; 3546820a0de9SPawel Jakub Dawidek size_t len; 3547820a0de9SPawel Jakub Dawidek 3548820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 35490304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 35500304c731SJamie Gritton return; 3551820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3552820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3553820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3554820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3555820a0de9SPawel Jakub Dawidek return; 3556820a0de9SPawel Jakub Dawidek } 3557820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3558820a0de9SPawel Jakub Dawidek /* 3559820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3560820a0de9SPawel Jakub Dawidek * the valid path left there. 3561820a0de9SPawel Jakub Dawidek */ 3562820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3563820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3564820a0de9SPawel Jakub Dawidek return; 3565820a0de9SPawel Jakub Dawidek } 3566820a0de9SPawel Jakub Dawidek /* 3567820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3568820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3569820a0de9SPawel Jakub Dawidek */ 3570820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3571820a0de9SPawel Jakub Dawidek return; 3572820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3573820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3574820a0de9SPawel Jakub Dawidek /* 3575820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3576820a0de9SPawel Jakub Dawidek * the valid path left there. 3577820a0de9SPawel Jakub Dawidek */ 3578820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3579820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3580820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3581820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3582820a0de9SPawel Jakub Dawidek } else { 3583820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3584820a0de9SPawel Jakub Dawidek } 3585f08df373SRobert Watson } 3586f08df373SRobert Watson 3587800c9408SRobert Watson /* 3588800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3589800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3590800c9408SRobert Watson */ 3591800c9408SRobert Watson int 3592800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3593800c9408SRobert Watson { 3594800c9408SRobert Watson 3595800c9408SRobert Watson if (!jailed(cred)) 3596800c9408SRobert Watson return (0); 3597800c9408SRobert Watson 35986bb79563SJamie Gritton #ifdef VIMAGE 35996bb79563SJamie Gritton /* 36006bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 36016bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 36026bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 36036bb79563SJamie Gritton */ 36046bb79563SJamie Gritton switch (priv) { 36056bb79563SJamie Gritton #ifdef notyet 36066bb79563SJamie Gritton /* 36076bb79563SJamie Gritton * NFS-specific privileges. 36086bb79563SJamie Gritton */ 36096bb79563SJamie Gritton case PRIV_NFS_DAEMON: 36106bb79563SJamie Gritton case PRIV_NFS_LOCKD: 36116bb79563SJamie Gritton #endif 36126bb79563SJamie Gritton /* 36136bb79563SJamie Gritton * Network stack privileges. 36146bb79563SJamie Gritton */ 36156bb79563SJamie Gritton case PRIV_NET_BRIDGE: 36166bb79563SJamie Gritton case PRIV_NET_GRE: 36176bb79563SJamie Gritton case PRIV_NET_BPF: 36186bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 36196bb79563SJamie Gritton case PRIV_NET_ROUTE: 36206bb79563SJamie Gritton case PRIV_NET_TAP: 36216bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 36226bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 36236bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3624215940b3SXin LI case PRIV_NET_SETIFDESCR: 36256bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 36266bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 36276bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 36286bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 36296bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 36306bb79563SJamie Gritton case PRIV_NET_DELMULTI: 36316bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 36326bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 36336bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 36346bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 36356bb79563SJamie Gritton case PRIV_NET_IFCREATE: 36366bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 36376bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 36386bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 36396bb79563SJamie Gritton case PRIV_NET_LAGG: 36406bb79563SJamie Gritton case PRIV_NET_GIF: 36416bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 364235fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 36436bb79563SJamie Gritton 36446bb79563SJamie Gritton /* 36456bb79563SJamie Gritton * 802.11-related privileges. 36466bb79563SJamie Gritton */ 36476bb79563SJamie Gritton case PRIV_NET80211_GETKEY: 36486bb79563SJamie Gritton #ifdef notyet 36496bb79563SJamie Gritton case PRIV_NET80211_MANAGE: /* XXX-BZ discuss with sam@ */ 36506bb79563SJamie Gritton #endif 36516bb79563SJamie Gritton 36526bb79563SJamie Gritton #ifdef notyet 36536bb79563SJamie Gritton /* 36546bb79563SJamie Gritton * AppleTalk privileges. 36556bb79563SJamie Gritton */ 36566bb79563SJamie Gritton case PRIV_NETATALK_RESERVEDPORT: 36576bb79563SJamie Gritton 36586bb79563SJamie Gritton /* 36596bb79563SJamie Gritton * ATM privileges. 36606bb79563SJamie Gritton */ 36616bb79563SJamie Gritton case PRIV_NETATM_CFG: 36626bb79563SJamie Gritton case PRIV_NETATM_ADD: 36636bb79563SJamie Gritton case PRIV_NETATM_DEL: 36646bb79563SJamie Gritton case PRIV_NETATM_SET: 36656bb79563SJamie Gritton 36666bb79563SJamie Gritton /* 36676bb79563SJamie Gritton * Bluetooth privileges. 36686bb79563SJamie Gritton */ 36696bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 36706bb79563SJamie Gritton #endif 36716bb79563SJamie Gritton 36726bb79563SJamie Gritton /* 36736bb79563SJamie Gritton * Netgraph and netgraph module privileges. 36746bb79563SJamie Gritton */ 36756bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 36766bb79563SJamie Gritton #ifdef notyet 36776bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 36786bb79563SJamie Gritton #endif 36796bb79563SJamie Gritton 36806bb79563SJamie Gritton /* 36816bb79563SJamie Gritton * IPv4 and IPv6 privileges. 36826bb79563SJamie Gritton */ 36836bb79563SJamie Gritton case PRIV_NETINET_IPFW: 36846bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 36856bb79563SJamie Gritton case PRIV_NETINET_PF: 36866bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 36876bb79563SJamie Gritton case PRIV_NETINET_CARP: 36886bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 36896bb79563SJamie Gritton case PRIV_NETINET_RAW: 36906bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 36916bb79563SJamie Gritton case PRIV_NETINET_ND6: 36926bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 36936bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 36946bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 36956bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 36966bb79563SJamie Gritton 36976bb79563SJamie Gritton #ifdef notyet 36986bb79563SJamie Gritton /* 36996bb79563SJamie Gritton * IPX/SPX privileges. 37006bb79563SJamie Gritton */ 37016bb79563SJamie Gritton case PRIV_NETIPX_RESERVEDPORT: 37026bb79563SJamie Gritton case PRIV_NETIPX_RAW: 37036bb79563SJamie Gritton 37046bb79563SJamie Gritton /* 37056bb79563SJamie Gritton * NCP privileges. 37066bb79563SJamie Gritton */ 37076bb79563SJamie Gritton case PRIV_NETNCP: 37086bb79563SJamie Gritton 37096bb79563SJamie Gritton /* 37106bb79563SJamie Gritton * SMB privileges. 37116bb79563SJamie Gritton */ 37126bb79563SJamie Gritton case PRIV_NETSMB: 37136bb79563SJamie Gritton #endif 37146bb79563SJamie Gritton 37156bb79563SJamie Gritton /* 37166bb79563SJamie Gritton * No default: or deny here. 37176bb79563SJamie Gritton * In case of no permit fall through to next switch(). 37186bb79563SJamie Gritton */ 37196bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 37206bb79563SJamie Gritton return (0); 37216bb79563SJamie Gritton } 37226bb79563SJamie Gritton #endif /* VIMAGE */ 37236bb79563SJamie Gritton 3724800c9408SRobert Watson switch (priv) { 3725800c9408SRobert Watson 3726800c9408SRobert Watson /* 3727800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3728800c9408SRobert Watson */ 3729800c9408SRobert Watson case PRIV_KTRACE: 3730800c9408SRobert Watson 3731c3c1b5e6SRobert Watson #if 0 3732800c9408SRobert Watson /* 3733800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3734800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3735800c9408SRobert Watson * want to further refine the relationship between audit and 3736800c9408SRobert Watson * jail. 3737800c9408SRobert Watson */ 3738800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3739800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3740800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3741c3c1b5e6SRobert Watson #endif 3742800c9408SRobert Watson 3743800c9408SRobert Watson /* 3744800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3745800c9408SRobert Watson * credentials in any way they see fit. 3746800c9408SRobert Watson */ 3747800c9408SRobert Watson case PRIV_CRED_SETUID: 3748800c9408SRobert Watson case PRIV_CRED_SETEUID: 3749800c9408SRobert Watson case PRIV_CRED_SETGID: 3750800c9408SRobert Watson case PRIV_CRED_SETEGID: 3751800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3752800c9408SRobert Watson case PRIV_CRED_SETREUID: 3753800c9408SRobert Watson case PRIV_CRED_SETREGID: 3754800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3755800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3756800c9408SRobert Watson 3757800c9408SRobert Watson /* 3758800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3759800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3760800c9408SRobert Watson */ 3761800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3762800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3763800c9408SRobert Watson 3764800c9408SRobert Watson /* 3765800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3766800c9408SRobert Watson * allow jailed root various debugging privileges. 3767800c9408SRobert Watson */ 3768800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3769800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3770800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3771800c9408SRobert Watson 3772800c9408SRobert Watson /* 3773800c9408SRobert Watson * Allow jail to set various resource limits and login 3774800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3775800c9408SRobert Watson */ 3776800c9408SRobert Watson case PRIV_PROC_LIMIT: 3777800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3778800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3779800c9408SRobert Watson 3780800c9408SRobert Watson /* 3781800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3782800c9408SRobert Watson */ 3783800c9408SRobert Watson case PRIV_IPC_READ: 3784800c9408SRobert Watson case PRIV_IPC_WRITE: 3785800c9408SRobert Watson case PRIV_IPC_ADMIN: 3786800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3787800c9408SRobert Watson case PRIV_MQ_ADMIN: 3788800c9408SRobert Watson 3789800c9408SRobert Watson /* 37900304c731SJamie Gritton * Jail operations within a jail work on child jails. 37910304c731SJamie Gritton */ 37920304c731SJamie Gritton case PRIV_JAIL_ATTACH: 37930304c731SJamie Gritton case PRIV_JAIL_SET: 37940304c731SJamie Gritton case PRIV_JAIL_REMOVE: 37950304c731SJamie Gritton 37960304c731SJamie Gritton /* 3797800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3798800c9408SRobert Watson * root processes in jail to change scheduling on other 3799800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3800800c9408SRobert Watson */ 3801800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3802413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3803800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3804800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3805800c9408SRobert Watson 3806800c9408SRobert Watson /* 3807800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3808800c9408SRobert Watson * writable. 3809800c9408SRobert Watson */ 3810800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3811800c9408SRobert Watson 3812800c9408SRobert Watson /* 3813800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3814e82d0201SRobert Watson * properties. These should likely be conditional on a 3815e82d0201SRobert Watson * configuration option. 3816800c9408SRobert Watson */ 381795b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 381895b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3819800c9408SRobert Watson 3820800c9408SRobert Watson /* 3821800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3822800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3823800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3824800c9408SRobert Watson * privileges. 3825800c9408SRobert Watson */ 3826800c9408SRobert Watson case PRIV_VFS_READ: 3827800c9408SRobert Watson case PRIV_VFS_WRITE: 3828800c9408SRobert Watson case PRIV_VFS_ADMIN: 3829800c9408SRobert Watson case PRIV_VFS_EXEC: 3830800c9408SRobert Watson case PRIV_VFS_LOOKUP: 3831800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3832800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3833800c9408SRobert Watson case PRIV_VFS_CHOWN: 3834800c9408SRobert Watson case PRIV_VFS_CHROOT: 3835bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3836800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3837800c9408SRobert Watson case PRIV_VFS_LINK: 3838800c9408SRobert Watson case PRIV_VFS_SETGID: 3839e41966dcSRobert Watson case PRIV_VFS_STAT: 3840800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3841800c9408SRobert Watson return (0); 3842800c9408SRobert Watson 3843800c9408SRobert Watson /* 3844800c9408SRobert Watson * Depending on the global setting, allow privilege of 3845800c9408SRobert Watson * setting system flags. 3846800c9408SRobert Watson */ 3847800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 38480304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3849800c9408SRobert Watson return (0); 3850800c9408SRobert Watson else 3851800c9408SRobert Watson return (EPERM); 3852800c9408SRobert Watson 3853800c9408SRobert Watson /* 3854f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3855f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3856f3a8d2f9SPawel Jakub Dawidek */ 3857f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3858f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3859f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 386024b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 38610304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_MOUNT) 3862f3a8d2f9SPawel Jakub Dawidek return (0); 3863f3a8d2f9SPawel Jakub Dawidek else 3864f3a8d2f9SPawel Jakub Dawidek return (EPERM); 3865f3a8d2f9SPawel Jakub Dawidek 3866f3a8d2f9SPawel Jakub Dawidek /* 38674b084056SRobert Watson * Allow jailed root to bind reserved ports and reuse in-use 38684b084056SRobert Watson * ports. 3869800c9408SRobert Watson */ 3870800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 38714b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3872800c9408SRobert Watson return (0); 3873800c9408SRobert Watson 3874800c9408SRobert Watson /* 387579ba3952SBjoern A. Zeeb * Allow jailed root to set certian IPv4/6 (option) headers. 387679ba3952SBjoern A. Zeeb */ 387779ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 387879ba3952SBjoern A. Zeeb return (0); 387979ba3952SBjoern A. Zeeb 388079ba3952SBjoern A. Zeeb /* 3881800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3882800c9408SRobert Watson */ 3883800c9408SRobert Watson case PRIV_NETINET_RAW: 38840304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3885800c9408SRobert Watson return (0); 3886800c9408SRobert Watson else 3887800c9408SRobert Watson return (EPERM); 3888800c9408SRobert Watson 3889800c9408SRobert Watson /* 3890800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3891800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3892800c9408SRobert Watson * jail. 3893800c9408SRobert Watson */ 3894800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3895800c9408SRobert Watson return (0); 3896800c9408SRobert Watson 38972bfc50bcSEdward Tomasz Napierala /* 38982bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 38992bfc50bcSEdward Tomasz Napierala */ 39002bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 39012bfc50bcSEdward Tomasz Napierala return (0); 39022bfc50bcSEdward Tomasz Napierala 3903800c9408SRobert Watson default: 3904800c9408SRobert Watson /* 3905800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3906800c9408SRobert Watson * includes almost all network privileges, many system 3907800c9408SRobert Watson * configuration privileges. 3908800c9408SRobert Watson */ 3909800c9408SRobert Watson return (EPERM); 3910800c9408SRobert Watson } 3911800c9408SRobert Watson } 3912800c9408SRobert Watson 39130304c731SJamie Gritton /* 39140304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 39150304c731SJamie Gritton * if it does not directly follow. 39160304c731SJamie Gritton */ 39170304c731SJamie Gritton 39180304c731SJamie Gritton char * 39190304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 39200304c731SJamie Gritton { 39210304c731SJamie Gritton char *name; 39220304c731SJamie Gritton 39230304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 39240304c731SJamie Gritton if (pr1 == pr2) 39250304c731SJamie Gritton return "0"; 39260304c731SJamie Gritton name = pr2->pr_name; 39270304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 39280304c731SJamie Gritton /* 39290304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 39300304c731SJamie Gritton * so its length can't be counted on. But the number of dots 39310304c731SJamie Gritton * can be counted on - and counted. 39320304c731SJamie Gritton */ 39330304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 39340304c731SJamie Gritton name = strchr(name, '.') + 1; 39350304c731SJamie Gritton } 39360304c731SJamie Gritton return (name); 39370304c731SJamie Gritton } 39380304c731SJamie Gritton 39390304c731SJamie Gritton /* 39400304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 39410304c731SJamie Gritton * if it does not directly follow. 39420304c731SJamie Gritton */ 39430304c731SJamie Gritton static char * 39440304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 39450304c731SJamie Gritton { 39460304c731SJamie Gritton char *path1, *path2; 39470304c731SJamie Gritton int len1; 39480304c731SJamie Gritton 39490304c731SJamie Gritton path1 = pr1->pr_path; 39500304c731SJamie Gritton path2 = pr2->pr_path; 39510304c731SJamie Gritton if (!strcmp(path1, "/")) 39520304c731SJamie Gritton return (path2); 39530304c731SJamie Gritton len1 = strlen(path1); 39540304c731SJamie Gritton if (strncmp(path1, path2, len1)) 39550304c731SJamie Gritton return (path2); 39560304c731SJamie Gritton if (path2[len1] == '\0') 39570304c731SJamie Gritton return "/"; 39580304c731SJamie Gritton if (path2[len1] == '/') 39590304c731SJamie Gritton return (path2 + len1); 39600304c731SJamie Gritton return (path2); 39610304c731SJamie Gritton } 39620304c731SJamie Gritton 39630304c731SJamie Gritton 39640304c731SJamie Gritton /* 39650304c731SJamie Gritton * Jail-related sysctls. 39660304c731SJamie Gritton */ 39670304c731SJamie Gritton SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW, 0, 39680304c731SJamie Gritton "Jails"); 39690304c731SJamie Gritton 3970fd7a8150SMike Barcroft static int 3971fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 3972fd7a8150SMike Barcroft { 3973b38ff370SJamie Gritton struct xprison *xp; 39740304c731SJamie Gritton struct prison *pr, *cpr; 3975b38ff370SJamie Gritton #ifdef INET 3976b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 3977b38ff370SJamie Gritton int ip4s = 0; 3978b38ff370SJamie Gritton #endif 3979b38ff370SJamie Gritton #ifdef INET6 39803beefaedSColin Percival struct in6_addr *ip6 = NULL; 3981b38ff370SJamie Gritton int ip6s = 0; 3982b38ff370SJamie Gritton #endif 39830304c731SJamie Gritton int descend, error; 3984fd7a8150SMike Barcroft 3985b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 39860304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 3987b38ff370SJamie Gritton error = 0; 3988dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 39890304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 39900304c731SJamie Gritton #if defined(INET) || defined(INET6) 3991b38ff370SJamie Gritton again: 39920304c731SJamie Gritton #endif 39930304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 3994413628a7SBjoern A. Zeeb #ifdef INET 39950304c731SJamie Gritton if (cpr->pr_ip4s > 0) { 39960304c731SJamie Gritton if (ip4s < cpr->pr_ip4s) { 39970304c731SJamie Gritton ip4s = cpr->pr_ip4s; 39980304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 3999b38ff370SJamie Gritton ip4 = realloc(ip4, ip4s * 4000b38ff370SJamie Gritton sizeof(struct in_addr), M_TEMP, M_WAITOK); 4001b38ff370SJamie Gritton goto again; 4002b38ff370SJamie Gritton } 40030304c731SJamie Gritton bcopy(cpr->pr_ip4, ip4, 40040304c731SJamie Gritton cpr->pr_ip4s * sizeof(struct in_addr)); 4005b38ff370SJamie Gritton } 4006413628a7SBjoern A. Zeeb #endif 4007413628a7SBjoern A. Zeeb #ifdef INET6 40080304c731SJamie Gritton if (cpr->pr_ip6s > 0) { 40090304c731SJamie Gritton if (ip6s < cpr->pr_ip6s) { 40100304c731SJamie Gritton ip6s = cpr->pr_ip6s; 40110304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4012b38ff370SJamie Gritton ip6 = realloc(ip6, ip6s * 4013b38ff370SJamie Gritton sizeof(struct in6_addr), M_TEMP, M_WAITOK); 4014b38ff370SJamie Gritton goto again; 4015413628a7SBjoern A. Zeeb } 40160304c731SJamie Gritton bcopy(cpr->pr_ip6, ip6, 40170304c731SJamie Gritton cpr->pr_ip6s * sizeof(struct in6_addr)); 4018b38ff370SJamie Gritton } 4019b38ff370SJamie Gritton #endif 40200304c731SJamie Gritton if (cpr->pr_ref == 0) { 40210304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4022b38ff370SJamie Gritton continue; 4023b38ff370SJamie Gritton } 4024b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4025fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 40260304c731SJamie Gritton xp->pr_id = cpr->pr_id; 40270304c731SJamie Gritton xp->pr_state = cpr->pr_uref > 0 4028b38ff370SJamie Gritton ? PRISON_STATE_ALIVE : PRISON_STATE_DYING; 40290304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4030c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 40310304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4032413628a7SBjoern A. Zeeb #ifdef INET 40330304c731SJamie Gritton xp->pr_ip4s = cpr->pr_ip4s; 4034413628a7SBjoern A. Zeeb #endif 4035413628a7SBjoern A. Zeeb #ifdef INET6 40360304c731SJamie Gritton xp->pr_ip6s = cpr->pr_ip6s; 4037413628a7SBjoern A. Zeeb #endif 40380304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4039b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4040b38ff370SJamie Gritton if (error) 4041b38ff370SJamie Gritton break; 4042413628a7SBjoern A. Zeeb #ifdef INET 4043b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4044b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4045b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4046b38ff370SJamie Gritton if (error) 4047b38ff370SJamie Gritton break; 4048413628a7SBjoern A. Zeeb } 4049413628a7SBjoern A. Zeeb #endif 4050413628a7SBjoern A. Zeeb #ifdef INET6 4051b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4052b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4053b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4054b38ff370SJamie Gritton if (error) 4055b38ff370SJamie Gritton break; 4056413628a7SBjoern A. Zeeb } 4057413628a7SBjoern A. Zeeb #endif 4058fd7a8150SMike Barcroft } 4059dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4060b38ff370SJamie Gritton free(xp, M_TEMP); 4061b38ff370SJamie Gritton #ifdef INET 4062b38ff370SJamie Gritton free(ip4, M_TEMP); 4063b38ff370SJamie Gritton #endif 4064b38ff370SJamie Gritton #ifdef INET6 4065b38ff370SJamie Gritton free(ip6, M_TEMP); 4066b38ff370SJamie Gritton #endif 4067fd7a8150SMike Barcroft return (error); 4068fd7a8150SMike Barcroft } 4069fd7a8150SMike Barcroft 4070f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4071f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4072f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4073461167c2SPawel Jakub Dawidek 4074461167c2SPawel Jakub Dawidek static int 4075461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4076461167c2SPawel Jakub Dawidek { 4077461167c2SPawel Jakub Dawidek int error, injail; 4078461167c2SPawel Jakub Dawidek 4079461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4080461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4081461167c2SPawel Jakub Dawidek 4082461167c2SPawel Jakub Dawidek return (error); 4083461167c2SPawel Jakub Dawidek } 40840304c731SJamie Gritton 4085f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4086f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4087f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4088413628a7SBjoern A. Zeeb 40890304c731SJamie Gritton #if defined(INET) || defined(INET6) 4090e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 40910304c731SJamie Gritton &jail_max_af_ips, 0, 40920304c731SJamie Gritton "Number of IP addresses a jail may have at most per address family"); 40930304c731SJamie Gritton #endif 40940304c731SJamie Gritton 40950304c731SJamie Gritton /* 40960304c731SJamie Gritton * Default parameters for jail(2) compatability. For historical reasons, 40970304c731SJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 40980304c731SJamie Gritton * just see their own parameters, and can't change them. 40990304c731SJamie Gritton */ 41000304c731SJamie Gritton static int 41010304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 41020304c731SJamie Gritton { 41030304c731SJamie Gritton struct prison *pr; 41040304c731SJamie Gritton int allow, error, i; 41050304c731SJamie Gritton 41060304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 41070304c731SJamie Gritton allow = (pr == &prison0) ? jail_default_allow : pr->pr_allow; 41080304c731SJamie Gritton 41090304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 41100304c731SJamie Gritton i = (allow & arg2) ? 1 : 0; 41110304c731SJamie Gritton if (arg1 != NULL) 41120304c731SJamie Gritton i = !i; 41130304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 41140304c731SJamie Gritton if (error || !req->newptr) 41150304c731SJamie Gritton return (error); 41160304c731SJamie Gritton i = i ? arg2 : 0; 41170304c731SJamie Gritton if (arg1 != NULL) 41180304c731SJamie Gritton i ^= arg2; 41190304c731SJamie Gritton /* 41200304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 41210304c731SJamie Gritton * for writing. 41220304c731SJamie Gritton */ 41230304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 41240304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 41250304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 41260304c731SJamie Gritton return (0); 41270304c731SJamie Gritton } 41280304c731SJamie Gritton 41290304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 41300304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41310304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 41320304c731SJamie Gritton "Processes in jail can set their hostnames"); 41330304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 41340304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41350304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 41360304c731SJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only"); 41370304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 41380304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41390304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 41400304c731SJamie Gritton "Processes in jail can use System V IPC primitives"); 41410304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 41420304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41430304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 41440304c731SJamie Gritton "Prison root can create raw sockets"); 41450304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 41460304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41470304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 41480304c731SJamie Gritton "Processes in jail can alter system file flags"); 41490304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 41500304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41510304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 41520304c731SJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems"); 41530304c731SJamie Gritton 41540304c731SJamie Gritton static int 41550304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 41560304c731SJamie Gritton { 41570304c731SJamie Gritton struct prison *pr; 41580304c731SJamie Gritton int level, error; 41590304c731SJamie Gritton 41600304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 41610304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 41620304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 41630304c731SJamie Gritton if (error || !req->newptr) 41640304c731SJamie Gritton return (error); 41650304c731SJamie Gritton *(int *)arg1 = level; 41660304c731SJamie Gritton return (0); 41670304c731SJamie Gritton } 41680304c731SJamie Gritton 41690304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 41700304c731SJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 41710304c731SJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 41720304c731SJamie Gritton sysctl_jail_default_level, "I", 41730304c731SJamie Gritton "Processes in jail cannot see all mounted file systems"); 41740304c731SJamie Gritton 41750304c731SJamie Gritton /* 41760304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 41770304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 41780304c731SJamie Gritton * to make themselves and their types known. 41790304c731SJamie Gritton */ 41800304c731SJamie Gritton SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW, 0, 41810304c731SJamie Gritton "Jail parameters"); 41820304c731SJamie Gritton 41830304c731SJamie Gritton int 41840304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 41850304c731SJamie Gritton { 41860304c731SJamie Gritton int i; 41870304c731SJamie Gritton long l; 41880304c731SJamie Gritton size_t s; 41890304c731SJamie Gritton char numbuf[12]; 41900304c731SJamie Gritton 41910304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 41920304c731SJamie Gritton { 41930304c731SJamie Gritton case CTLTYPE_LONG: 41940304c731SJamie Gritton case CTLTYPE_ULONG: 41950304c731SJamie Gritton l = 0; 41960304c731SJamie Gritton #ifdef SCTL_MASK32 41970304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 41980304c731SJamie Gritton #endif 41990304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 42000304c731SJamie Gritton case CTLTYPE_INT: 42010304c731SJamie Gritton case CTLTYPE_UINT: 42020304c731SJamie Gritton i = 0; 42030304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 42040304c731SJamie Gritton case CTLTYPE_STRING: 4205e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 42060304c731SJamie Gritton return 42070304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 42080304c731SJamie Gritton case CTLTYPE_STRUCT: 42090304c731SJamie Gritton s = (size_t)arg2; 42100304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 42110304c731SJamie Gritton } 42120304c731SJamie Gritton return (0); 42130304c731SJamie Gritton } 42140304c731SJamie Gritton 42150304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 42160304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 42170304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 42180304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 42190304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 42200304c731SJamie Gritton "I", "Jail secure level"); 42210304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 42220304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 42230304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 42240304c731SJamie Gritton "B", "Jail persistence"); 4225679e1390SJamie Gritton #ifdef VIMAGE 4226679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 42277cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4228679e1390SJamie Gritton #endif 42290304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 42300304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 42310304c731SJamie Gritton 4232b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4233b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4234b97457e2SJamie Gritton "I", "Current number of child jails"); 4235b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4236b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4237b97457e2SJamie Gritton 42387cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 42390304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 42400304c731SJamie Gritton "Jail hostname"); 424176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 424276ca6f88SJamie Gritton "Jail NIS domainname"); 424376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 424476ca6f88SJamie Gritton "Jail host UUID"); 424576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 424676ca6f88SJamie Gritton "LU", "Jail host ID"); 42470304c731SJamie Gritton 42480304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 42490304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 42500304c731SJamie Gritton 42510304c731SJamie Gritton #ifdef INET 42522b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 42532b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 42540304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 42550304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4256592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4257592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4258592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 42590304c731SJamie Gritton #endif 42600304c731SJamie Gritton #ifdef INET6 42612b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 42622b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 42630304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 42640304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4265592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4266592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4267592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 42680304c731SJamie Gritton #endif 42690304c731SJamie Gritton 42700304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 42710304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 42720304c731SJamie Gritton "B", "Jail may set hostname"); 42730304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 42740304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 42750304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 42760304c731SJamie Gritton "B", "Jail may create raw sockets"); 42770304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 42780304c731SJamie Gritton "B", "Jail may alter system file flags"); 42790304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, mount, CTLTYPE_INT | CTLFLAG_RW, 42800304c731SJamie Gritton "B", "Jail may mount/unmount jail-friendly file systems"); 42810304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 42820304c731SJamie Gritton "B", "Jail may set file quotas"); 42830304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 42840304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 42850304c731SJamie Gritton 4286097055e2SEdward Tomasz Napierala void 4287097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 4288097055e2SEdward Tomasz Napierala void *arg2, void *arg3), void *arg2, void *arg3) 4289097055e2SEdward Tomasz Napierala { 4290a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4291097055e2SEdward Tomasz Napierala 4292097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 4293a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4294a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 4295097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4296097055e2SEdward Tomasz Napierala } 42970304c731SJamie Gritton 4298a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4299a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4300a7ad07bfSEdward Tomasz Napierala { 4301a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4302a7ad07bfSEdward Tomasz Napierala 4303a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4304a7ad07bfSEdward Tomasz Napierala 4305a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4306a7ad07bfSEdward Tomasz Napierala return (NULL); 4307a7ad07bfSEdward Tomasz Napierala 4308a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4309a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4310a7ad07bfSEdward Tomasz Napierala continue; 4311a7ad07bfSEdward Tomasz Napierala 4312a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4313a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4314a7ad07bfSEdward Tomasz Napierala return (prr); 4315a7ad07bfSEdward Tomasz Napierala } 4316a7ad07bfSEdward Tomasz Napierala 4317a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4318a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4319a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4320a7ad07bfSEdward Tomasz Napierala 4321a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4322a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4323a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4324a7ad07bfSEdward Tomasz Napierala 4325a7ad07bfSEdward Tomasz Napierala return (prr); 4326a7ad07bfSEdward Tomasz Napierala } 4327a7ad07bfSEdward Tomasz Napierala 4328a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4329a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4330a7ad07bfSEdward Tomasz Napierala { 4331a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4332a7ad07bfSEdward Tomasz Napierala 4333a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4334a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4335a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4336a7ad07bfSEdward Tomasz Napierala return (prr); 4337a7ad07bfSEdward Tomasz Napierala } 4338a7ad07bfSEdward Tomasz Napierala 4339a7ad07bfSEdward Tomasz Napierala void 4340a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4341a7ad07bfSEdward Tomasz Napierala { 4342a7ad07bfSEdward Tomasz Napierala 4343a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4344a7ad07bfSEdward Tomasz Napierala } 4345a7ad07bfSEdward Tomasz Napierala 4346a7ad07bfSEdward Tomasz Napierala void 4347a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4348a7ad07bfSEdward Tomasz Napierala { 4349a7ad07bfSEdward Tomasz Napierala int old; 4350a7ad07bfSEdward Tomasz Napierala 4351a7ad07bfSEdward Tomasz Napierala old = prr->prr_refcount; 4352a7ad07bfSEdward Tomasz Napierala if (old > 1 && atomic_cmpset_int(&prr->prr_refcount, old, old - 1)) 4353a7ad07bfSEdward Tomasz Napierala return; 4354a7ad07bfSEdward Tomasz Napierala 4355a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4356a7ad07bfSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4357a7ad07bfSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4358a7ad07bfSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4359a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4360a7ad07bfSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4361a7ad07bfSEdward Tomasz Napierala 4362a7ad07bfSEdward Tomasz Napierala return; 4363a7ad07bfSEdward Tomasz Napierala } 4364a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4365a7ad07bfSEdward Tomasz Napierala } 4366a7ad07bfSEdward Tomasz Napierala 4367a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 4368a7ad07bfSEdward Tomasz Napierala static void 4369a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4370a7ad07bfSEdward Tomasz Napierala { 4371a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4372a7ad07bfSEdward Tomasz Napierala 4373a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4374a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4375a7ad07bfSEdward Tomasz Napierala 4376a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4377a7ad07bfSEdward Tomasz Napierala } 4378a7ad07bfSEdward Tomasz Napierala 4379a7ad07bfSEdward Tomasz Napierala static void 4380a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4381a7ad07bfSEdward Tomasz Napierala { 4382a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4383a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4384a7ad07bfSEdward Tomasz Napierala } 4385a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4386a7ad07bfSEdward Tomasz Napierala 4387413628a7SBjoern A. Zeeb #ifdef DDB 4388b38ff370SJamie Gritton 4389b38ff370SJamie Gritton static void 4390b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4391413628a7SBjoern A. Zeeb { 43920304c731SJamie Gritton int fi; 4393b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4394b38ff370SJamie Gritton int ii; 4395413628a7SBjoern A. Zeeb #endif 43967cbf7213SJamie Gritton unsigned jsf; 4397413628a7SBjoern A. Zeeb #ifdef INET6 4398413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4399413628a7SBjoern A. Zeeb #endif 4400413628a7SBjoern A. Zeeb 4401b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4402b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4403b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 44040304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4405b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4406b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 4407b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4408b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4409b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4410679e1390SJamie Gritton #ifdef VIMAGE 4411679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4412679e1390SJamie Gritton #endif 4413b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4414b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 4415fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4416fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 44170304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 44180304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4419fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 44200304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_names) / sizeof(pr_flag_names[0]); 44210304c731SJamie Gritton fi++) 44220304c731SJamie Gritton if (pr_flag_names[fi] != NULL && (pr->pr_flags & (1 << fi))) 44230304c731SJamie Gritton db_printf(" %s", pr_flag_names[fi]); 44247cbf7213SJamie Gritton for (fi = 0; fi < sizeof(pr_flag_jailsys) / sizeof(pr_flag_jailsys[0]); 44257cbf7213SJamie Gritton fi++) { 44267cbf7213SJamie Gritton jsf = pr->pr_flags & 44277cbf7213SJamie Gritton (pr_flag_jailsys[fi].disable | pr_flag_jailsys[fi].new); 44287cbf7213SJamie Gritton db_printf(" %-16s= %s\n", pr_flag_jailsys[fi].name, 44297cbf7213SJamie Gritton pr_flag_jailsys[fi].disable && 44307cbf7213SJamie Gritton (jsf == pr_flag_jailsys[fi].disable) ? "disable" 44317cbf7213SJamie Gritton : (jsf == pr_flag_jailsys[fi].new) ? "new" 44327cbf7213SJamie Gritton : "inherit"); 44337cbf7213SJamie Gritton } 4434fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 44350304c731SJamie Gritton for (fi = 0; fi < sizeof(pr_allow_names) / sizeof(pr_allow_names[0]); 44360304c731SJamie Gritton fi++) 44370304c731SJamie Gritton if (pr_allow_names[fi] != NULL && (pr->pr_allow & (1 << fi))) 44380304c731SJamie Gritton db_printf(" %s", pr_allow_names[fi]); 4439b38ff370SJamie Gritton db_printf("\n"); 44400304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4441c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4442c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4443c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 444476ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4445413628a7SBjoern A. Zeeb #ifdef INET 4446b38ff370SJamie Gritton db_printf(" ip4s = %d\n", pr->pr_ip4s); 4447b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip4s; ii++) 4448b38ff370SJamie Gritton db_printf(" %s %s\n", 4449fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 4450b38ff370SJamie Gritton inet_ntoa(pr->pr_ip4[ii])); 4451413628a7SBjoern A. Zeeb #endif 4452413628a7SBjoern A. Zeeb #ifdef INET6 4453b38ff370SJamie Gritton db_printf(" ip6s = %d\n", pr->pr_ip6s); 4454b38ff370SJamie Gritton for (ii = 0; ii < pr->pr_ip6s; ii++) 4455b38ff370SJamie Gritton db_printf(" %s %s\n", 4456fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4457b38ff370SJamie Gritton ip6_sprintf(ip6buf, &pr->pr_ip6[ii])); 4458b38ff370SJamie Gritton #endif 4459b38ff370SJamie Gritton } 4460b38ff370SJamie Gritton 4461b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4462b38ff370SJamie Gritton { 4463b38ff370SJamie Gritton struct prison *pr; 4464b38ff370SJamie Gritton 4465b38ff370SJamie Gritton if (!have_addr) { 44660304c731SJamie Gritton /* 44670304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 44680304c731SJamie Gritton * listed. 44690304c731SJamie Gritton */ 44700304c731SJamie Gritton db_show_prison(&prison0); 44710304c731SJamie Gritton if (!db_pager_quit) { 4472b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4473b38ff370SJamie Gritton db_show_prison(pr); 4474413628a7SBjoern A. Zeeb if (db_pager_quit) 4475413628a7SBjoern A. Zeeb break; 4476413628a7SBjoern A. Zeeb } 44770304c731SJamie Gritton } 4478b38ff370SJamie Gritton return; 4479413628a7SBjoern A. Zeeb } 4480b38ff370SJamie Gritton 44810304c731SJamie Gritton if (addr == 0) 44820304c731SJamie Gritton pr = &prison0; 44830304c731SJamie Gritton else { 4484b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4485b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4486b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4487b38ff370SJamie Gritton break; 4488b38ff370SJamie Gritton if (pr == NULL) 4489b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4490b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4491b38ff370SJamie Gritton if (pr->pr_id == addr) 4492b38ff370SJamie Gritton break; 4493b38ff370SJamie Gritton if (pr == NULL) 4494b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4495b38ff370SJamie Gritton pr = (struct prison *)addr; 44960304c731SJamie Gritton } 4497b38ff370SJamie Gritton db_show_prison(pr); 4498b38ff370SJamie Gritton } 4499b38ff370SJamie Gritton 4500413628a7SBjoern A. Zeeb #endif /* DDB */ 4501