19454b2d8SWarner Losh /*- 28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 38a36da99SPedro F. Giffuni * 4413628a7SBjoern A. Zeeb * Copyright (c) 1999 Poul-Henning Kamp. 5413628a7SBjoern A. Zeeb * Copyright (c) 2008 Bjoern A. Zeeb. 6b38ff370SJamie Gritton * Copyright (c) 2009 James Gritton. 7413628a7SBjoern A. Zeeb * All rights reserved. 8b9f0b66cSBjoern A. Zeeb * 9b9f0b66cSBjoern A. Zeeb * Redistribution and use in source and binary forms, with or without 10b9f0b66cSBjoern A. Zeeb * modification, are permitted provided that the following conditions 11b9f0b66cSBjoern A. Zeeb * are met: 12b9f0b66cSBjoern A. Zeeb * 1. Redistributions of source code must retain the above copyright 13b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer. 14b9f0b66cSBjoern A. Zeeb * 2. Redistributions in binary form must reproduce the above copyright 15b9f0b66cSBjoern A. Zeeb * notice, this list of conditions and the following disclaimer in the 16b9f0b66cSBjoern A. Zeeb * documentation and/or other materials provided with the distribution. 17b9f0b66cSBjoern A. Zeeb * 18b9f0b66cSBjoern A. Zeeb * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19b9f0b66cSBjoern A. Zeeb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20b9f0b66cSBjoern A. Zeeb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21b9f0b66cSBjoern A. Zeeb * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 22b9f0b66cSBjoern A. Zeeb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23b9f0b66cSBjoern A. Zeeb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24b9f0b66cSBjoern A. Zeeb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25b9f0b66cSBjoern A. Zeeb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26b9f0b66cSBjoern A. Zeeb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27b9f0b66cSBjoern A. Zeeb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28b9f0b66cSBjoern A. Zeeb * SUCH DAMAGE. 2907901f22SPoul-Henning Kamp */ 3075c13541SPoul-Henning Kamp 31677b542eSDavid E. O'Brien #include <sys/cdefs.h> 32677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 33677b542eSDavid E. O'Brien 34413628a7SBjoern A. Zeeb #include "opt_ddb.h" 35413628a7SBjoern A. Zeeb #include "opt_inet.h" 36413628a7SBjoern A. Zeeb #include "opt_inet6.h" 3746e3b1cbSPawel Jakub Dawidek 3875c13541SPoul-Henning Kamp #include <sys/param.h> 3975c13541SPoul-Henning Kamp #include <sys/types.h> 4075c13541SPoul-Henning Kamp #include <sys/kernel.h> 4175c13541SPoul-Henning Kamp #include <sys/systm.h> 4275c13541SPoul-Henning Kamp #include <sys/errno.h> 4375c13541SPoul-Henning Kamp #include <sys/sysproto.h> 4475c13541SPoul-Henning Kamp #include <sys/malloc.h> 450304c731SJamie Gritton #include <sys/osd.h> 46800c9408SRobert Watson #include <sys/priv.h> 4775c13541SPoul-Henning Kamp #include <sys/proc.h> 48*eb8dcdeaSGleb Smirnoff #include <sys/epoch.h> 49b3059e09SRobert Watson #include <sys/taskqueue.h> 5057b4252eSKonstantin Belousov #include <sys/fcntl.h> 5175c13541SPoul-Henning Kamp #include <sys/jail.h> 52c3188289SKyle Evans #include <sys/linker.h> 5301137630SRobert Watson #include <sys/lock.h> 5401137630SRobert Watson #include <sys/mutex.h> 55097055e2SEdward Tomasz Napierala #include <sys/racct.h> 56f87beb93SAndriy Gapon #include <sys/rctl.h> 57a7ad07bfSEdward Tomasz Napierala #include <sys/refcount.h> 58dc68a633SPawel Jakub Dawidek #include <sys/sx.h> 5976ca6f88SJamie Gritton #include <sys/sysent.h> 60fd7a8150SMike Barcroft #include <sys/namei.h> 61820a0de9SPawel Jakub Dawidek #include <sys/mount.h> 62fd7a8150SMike Barcroft #include <sys/queue.h> 6375c13541SPoul-Henning Kamp #include <sys/socket.h> 64fd7a8150SMike Barcroft #include <sys/syscallsubr.h> 6583f1e257SRobert Watson #include <sys/sysctl.h> 66c3188289SKyle Evans #include <sys/uuid.h> 67fd7a8150SMike Barcroft #include <sys/vnode.h> 68530c0060SRobert Watson 6975c13541SPoul-Henning Kamp #include <net/if.h> 70530c0060SRobert Watson #include <net/vnet.h> 71530c0060SRobert Watson 7275c13541SPoul-Henning Kamp #include <netinet/in.h> 73530c0060SRobert Watson 74413628a7SBjoern A. Zeeb #ifdef DDB 75413628a7SBjoern A. Zeeb #include <ddb/ddb.h> 76413628a7SBjoern A. Zeeb #endif /* DDB */ 7775c13541SPoul-Henning Kamp 78aed55708SRobert Watson #include <security/mac/mac_framework.h> 79aed55708SRobert Watson 80c3188289SKyle Evans #define PRISON0_HOSTUUID_MODULE "hostuuid" 818986e3a0SJamie Gritton 8275c13541SPoul-Henning Kamp MALLOC_DEFINE(M_PRISON, "prison", "Prison structures"); 83d745c852SEd Schouten static MALLOC_DEFINE(M_PRISON_RACCT, "prison_racct", "Prison racct structures"); 8475c13541SPoul-Henning Kamp 85592bcae8SBjoern A. Zeeb /* Keep struct prison prison0 and some code in kern_jail_set() readable. */ 86592bcae8SBjoern A. Zeeb #ifdef INET 87592bcae8SBjoern A. Zeeb #ifdef INET6 88592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL|PR_IP6_SADDRSEL 89592bcae8SBjoern A. Zeeb #else 90592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP4_SADDRSEL 91592bcae8SBjoern A. Zeeb #endif 92592bcae8SBjoern A. Zeeb #else /* !INET */ 93592bcae8SBjoern A. Zeeb #ifdef INET6 94592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL PR_IP6_SADDRSEL 95592bcae8SBjoern A. Zeeb #else 96592bcae8SBjoern A. Zeeb #define _PR_IP_SADDRSEL 0 97592bcae8SBjoern A. Zeeb #endif 98592bcae8SBjoern A. Zeeb #endif 99592bcae8SBjoern A. Zeeb 1000304c731SJamie Gritton /* prison0 describes what is "real" about the system. */ 1010304c731SJamie Gritton struct prison prison0 = { 1020304c731SJamie Gritton .pr_id = 0, 1030304c731SJamie Gritton .pr_name = "0", 1040304c731SJamie Gritton .pr_ref = 1, 1050304c731SJamie Gritton .pr_uref = 1, 1060304c731SJamie Gritton .pr_path = "/", 1070304c731SJamie Gritton .pr_securelevel = -1, 1080cc207a6SMartin Matuska .pr_devfs_rsnum = 0, 1091158508aSJamie Gritton .pr_state = PRISON_STATE_ALIVE, 110b97457e2SJamie Gritton .pr_childmax = JAIL_MAX, 1118986e3a0SJamie Gritton .pr_hostuuid = DEFAULT_HOSTUUID, 11213e403fdSAntoine Brodin .pr_children = LIST_HEAD_INITIALIZER(prison0.pr_children), 113eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 114592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|PR_VNET|_PR_IP_SADDRSEL, 115eb79e1c7SBjoern A. Zeeb #else 116592bcae8SBjoern A. Zeeb .pr_flags = PR_HOST|_PR_IP_SADDRSEL, 117eb79e1c7SBjoern A. Zeeb #endif 1180e5c6bd4SJamie Gritton .pr_allow = PR_ALLOW_ALL_STATIC, 1190304c731SJamie Gritton }; 1200304c731SJamie Gritton MTX_SYSINIT(prison0, &prison0.pr_mtx, "jail mutex", MTX_DEF); 12183f1e257SRobert Watson 122672756aaSJamie Gritton struct bool_flags { 123672756aaSJamie Gritton const char *name; 124672756aaSJamie Gritton const char *noname; 1255d58f959SJamie Gritton volatile u_int flag; 126672756aaSJamie Gritton }; 127672756aaSJamie Gritton struct jailsys_flags { 128672756aaSJamie Gritton const char *name; 129672756aaSJamie Gritton unsigned disable; 130672756aaSJamie Gritton unsigned new; 131672756aaSJamie Gritton }; 132672756aaSJamie Gritton 133a7ad07bfSEdward Tomasz Napierala /* allprison, allprison_racct and lastprid are protected by allprison_lock. */ 134dc68a633SPawel Jakub Dawidek struct sx allprison_lock; 135b38ff370SJamie Gritton SX_SYSINIT(allprison_lock, &allprison_lock, "allprison"); 136b38ff370SJamie Gritton struct prisonlist allprison = TAILQ_HEAD_INITIALIZER(allprison); 137a7ad07bfSEdward Tomasz Napierala LIST_HEAD(, prison_racct) allprison_racct; 1382110d913SXin LI int lastprid = 0; 139fd7a8150SMike Barcroft 1407de883c8SJamie Gritton static int get_next_prid(struct prison **insprp); 141f7496dcaSJamie Gritton static int do_jail_attach(struct thread *td, struct prison *pr, int drflags); 142b3059e09SRobert Watson static void prison_complete(void *context, int pending); 143b38ff370SJamie Gritton static void prison_deref(struct prison *pr, int flags); 144c861373bSJamie Gritton static void prison_deref_kill(struct prison *pr, struct prisonlist *freeprison); 145f7496dcaSJamie Gritton static int prison_lock_xlock(struct prison *pr, int flags); 146f7496dcaSJamie Gritton static void prison_free_not_last(struct prison *pr); 147c861373bSJamie Gritton static void prison_proc_free_not_last(struct prison *pr); 1480fe74ae6SJamie Gritton static void prison_set_allow_locked(struct prison *pr, unsigned flag, 1490fe74ae6SJamie Gritton int enable); 1500304c731SJamie Gritton static char *prison_path(struct prison *pr1, struct prison *pr2); 151a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 152a7ad07bfSEdward Tomasz Napierala static void prison_racct_attach(struct prison *pr); 153c34bbd2aSEdward Tomasz Napierala static void prison_racct_modify(struct prison *pr); 154a7ad07bfSEdward Tomasz Napierala static void prison_racct_detach(struct prison *pr); 155a7ad07bfSEdward Tomasz Napierala #endif 156fd7a8150SMike Barcroft 157b38ff370SJamie Gritton /* Flags for prison_deref */ 1582a4b2251SJamie Gritton #define PD_DEREF 0x01 /* Decrement pr_ref */ 1592a4b2251SJamie Gritton #define PD_DEUREF 0x02 /* Decrement pr_uref */ 160c861373bSJamie Gritton #define PD_KILL 0x04 /* Remove jail, kill processes, etc */ 161c861373bSJamie Gritton #define PD_LOCKED 0x10 /* pr_mtx is held */ 162c861373bSJamie Gritton #define PD_LIST_SLOCKED 0x20 /* allprison_lock is held shared */ 163c861373bSJamie Gritton #define PD_LIST_XLOCKED 0x40 /* allprison_lock is held exclusive */ 164589e4c1dSJamie Gritton #define PD_OP_FLAGS 0x07 /* Operation flags */ 165589e4c1dSJamie Gritton #define PD_LOCK_FLAGS 0x70 /* Lock status flags */ 166fd7a8150SMike Barcroft 1670304c731SJamie Gritton /* 1685cc70397SBjoern A. Zeeb * Parameter names corresponding to PR_* flag values. Size values are for kvm 1695cc70397SBjoern A. Zeeb * as we cannot figure out the size of a sparse array, or an array without a 1705cc70397SBjoern A. Zeeb * terminating entry. 1710304c731SJamie Gritton */ 172672756aaSJamie Gritton static struct bool_flags pr_flag_bool[] = { 173672756aaSJamie Gritton {"persist", "nopersist", PR_PERSIST}, 174592bcae8SBjoern A. Zeeb #ifdef INET 175672756aaSJamie Gritton {"ip4.saddrsel", "ip4.nosaddrsel", PR_IP4_SADDRSEL}, 176592bcae8SBjoern A. Zeeb #endif 177592bcae8SBjoern A. Zeeb #ifdef INET6 178672756aaSJamie Gritton {"ip6.saddrsel", "ip6.nosaddrsel", PR_IP6_SADDRSEL}, 179592bcae8SBjoern A. Zeeb #endif 1800304c731SJamie Gritton }; 181672756aaSJamie Gritton const size_t pr_flag_bool_size = sizeof(pr_flag_bool); 1820304c731SJamie Gritton 183672756aaSJamie Gritton static struct jailsys_flags pr_flag_jailsys[] = { 1847cbf7213SJamie Gritton {"host", 0, PR_HOST}, 1857cbf7213SJamie Gritton #ifdef VIMAGE 1867cbf7213SJamie Gritton {"vnet", 0, PR_VNET}, 1877cbf7213SJamie Gritton #endif 1880304c731SJamie Gritton #ifdef INET 1896a3f2779SJamie Gritton {"ip4", PR_IP4_USER, PR_IP4_USER}, 1900304c731SJamie Gritton #endif 1910304c731SJamie Gritton #ifdef INET6 1926a3f2779SJamie Gritton {"ip6", PR_IP6_USER, PR_IP6_USER}, 193679e1390SJamie Gritton #endif 1940304c731SJamie Gritton }; 1955cc70397SBjoern A. Zeeb const size_t pr_flag_jailsys_size = sizeof(pr_flag_jailsys); 1960304c731SJamie Gritton 1975d58f959SJamie Gritton /* 1985d58f959SJamie Gritton * Make this array full-size so dynamic parameters can be added. 1995d58f959SJamie Gritton * It is protected by prison0.mtx, but lockless reading is allowed 2005d58f959SJamie Gritton * with an atomic check of the flag values. 2015d58f959SJamie Gritton */ 2020e5c6bd4SJamie Gritton static struct bool_flags pr_flag_allow[NBBY * NBPW] = { 203672756aaSJamie Gritton {"allow.set_hostname", "allow.noset_hostname", PR_ALLOW_SET_HOSTNAME}, 204672756aaSJamie Gritton {"allow.sysvipc", "allow.nosysvipc", PR_ALLOW_SYSVIPC}, 205672756aaSJamie Gritton {"allow.raw_sockets", "allow.noraw_sockets", PR_ALLOW_RAW_SOCKETS}, 206672756aaSJamie Gritton {"allow.chflags", "allow.nochflags", PR_ALLOW_CHFLAGS}, 207672756aaSJamie Gritton {"allow.mount", "allow.nomount", PR_ALLOW_MOUNT}, 208672756aaSJamie Gritton {"allow.quotas", "allow.noquotas", PR_ALLOW_QUOTAS}, 209672756aaSJamie Gritton {"allow.socket_af", "allow.nosocket_af", PR_ALLOW_SOCKET_AF}, 210ccd6ac9fSAntoine Brodin {"allow.mlock", "allow.nomlock", PR_ALLOW_MLOCK}, 211672756aaSJamie Gritton {"allow.reserved_ports", "allow.noreserved_ports", 212672756aaSJamie Gritton PR_ALLOW_RESERVED_PORTS}, 213b19d66fdSJamie Gritton {"allow.read_msgbuf", "allow.noread_msgbuf", PR_ALLOW_READ_MSGBUF}, 214b3079544SJamie Gritton {"allow.unprivileged_proc_debug", "allow.nounprivileged_proc_debug", 215b3079544SJamie Gritton PR_ALLOW_UNPRIV_DEBUG}, 21605e1e482SMariusz Zaborski {"allow.suser", "allow.nosuser", PR_ALLOW_SUSER}, 2170304c731SJamie Gritton }; 2185d58f959SJamie Gritton static unsigned pr_allow_all = PR_ALLOW_ALL_STATIC; 219672756aaSJamie Gritton const size_t pr_flag_allow_size = sizeof(pr_flag_allow); 2200304c731SJamie Gritton 221b3079544SJamie Gritton #define JAIL_DEFAULT_ALLOW (PR_ALLOW_SET_HOSTNAME | \ 222b3079544SJamie Gritton PR_ALLOW_RESERVED_PORTS | \ 22305e1e482SMariusz Zaborski PR_ALLOW_UNPRIV_DEBUG | \ 22405e1e482SMariusz Zaborski PR_ALLOW_SUSER) 22542bebd82SJamie Gritton #define JAIL_DEFAULT_ENFORCE_STATFS 2 226bf3db8aaSMartin Matuska #define JAIL_DEFAULT_DEVFS_RSNUM 0 2270304c731SJamie Gritton static unsigned jail_default_allow = JAIL_DEFAULT_ALLOW; 22842bebd82SJamie Gritton static int jail_default_enforce_statfs = JAIL_DEFAULT_ENFORCE_STATFS; 2290cc207a6SMartin Matuska static int jail_default_devfs_rsnum = JAIL_DEFAULT_DEVFS_RSNUM; 2300304c731SJamie Gritton #if defined(INET) || defined(INET6) 231e92e0574SJamie Gritton static unsigned jail_max_af_ips = 255; 2320304c731SJamie Gritton #endif 2330304c731SJamie Gritton 234b96bd95bSIan Lepore /* 235b96bd95bSIan Lepore * Initialize the parts of prison0 that can't be static-initialized with 236b96bd95bSIan Lepore * constants. This is called from proc0_init() after creating thread0 cpuset. 237b96bd95bSIan Lepore */ 238b96bd95bSIan Lepore void 239b96bd95bSIan Lepore prison0_init(void) 240b96bd95bSIan Lepore { 241c3188289SKyle Evans uint8_t *file, *data; 242c3188289SKyle Evans size_t size; 243d2ef3774SJessica Clarke char buf[sizeof(prison0.pr_hostuuid)]; 244d2ef3774SJessica Clarke bool valid; 245b96bd95bSIan Lepore 246b96bd95bSIan Lepore prison0.pr_cpuset = cpuset_ref(thread0.td_cpuset); 247b96bd95bSIan Lepore prison0.pr_osreldate = osreldate; 248b96bd95bSIan Lepore strlcpy(prison0.pr_osrelease, osrelease, sizeof(prison0.pr_osrelease)); 249c3188289SKyle Evans 250c3188289SKyle Evans /* If we have a preloaded hostuuid, use it. */ 251c3188289SKyle Evans file = preload_search_by_type(PRISON0_HOSTUUID_MODULE); 252c3188289SKyle Evans if (file != NULL) { 253c3188289SKyle Evans data = preload_fetch_addr(file); 254c3188289SKyle Evans size = preload_fetch_size(file); 255c3188289SKyle Evans if (data != NULL) { 256c3188289SKyle Evans /* 257c3188289SKyle Evans * The preloaded data may include trailing whitespace, almost 258c3188289SKyle Evans * certainly a newline; skip over any whitespace or 259c3188289SKyle Evans * non-printable characters to be safe. 260c3188289SKyle Evans */ 261c3188289SKyle Evans while (size > 0 && data[size - 1] <= 0x20) { 262b6be9566SColin Percival size--; 263c3188289SKyle Evans } 264d2ef3774SJessica Clarke 265d2ef3774SJessica Clarke valid = false; 266d2ef3774SJessica Clarke 267d2ef3774SJessica Clarke /* 268d2ef3774SJessica Clarke * Not NUL-terminated when passed from loader, but 269d2ef3774SJessica Clarke * validate_uuid requires that due to using sscanf (as 270d2ef3774SJessica Clarke * does the subsequent strlcpy, since it still reads 271d2ef3774SJessica Clarke * past the given size to return the true length); 272d2ef3774SJessica Clarke * bounce to a temporary buffer to fix. 273d2ef3774SJessica Clarke */ 274d2ef3774SJessica Clarke if (size >= sizeof(buf)) 275d2ef3774SJessica Clarke goto done; 276d2ef3774SJessica Clarke 277d2ef3774SJessica Clarke memcpy(buf, data, size); 278d2ef3774SJessica Clarke buf[size] = '\0'; 279d2ef3774SJessica Clarke 280d2ef3774SJessica Clarke if (validate_uuid(buf, size, NULL, 0) != 0) 281d2ef3774SJessica Clarke goto done; 282d2ef3774SJessica Clarke 283d2ef3774SJessica Clarke valid = true; 284d2ef3774SJessica Clarke (void)strlcpy(prison0.pr_hostuuid, buf, 285d2ef3774SJessica Clarke sizeof(prison0.pr_hostuuid)); 286d2ef3774SJessica Clarke 287d2ef3774SJessica Clarke done: 288d2ef3774SJessica Clarke if (bootverbose && !valid) { 289330f110bSColin Percival printf("hostuuid: preload data malformed: '%.*s'\n", 290330f110bSColin Percival (int)size, data); 291c3188289SKyle Evans } 292c3188289SKyle Evans } 293c3188289SKyle Evans } 294c3188289SKyle Evans if (bootverbose) 295c3188289SKyle Evans printf("hostuuid: using %s\n", prison0.pr_hostuuid); 296b96bd95bSIan Lepore } 297b96bd95bSIan Lepore 298116734c4SMatthew Dillon /* 2999ddb7954SMike Barcroft * struct jail_args { 3009ddb7954SMike Barcroft * struct jail *jail; 3019ddb7954SMike Barcroft * }; 302116734c4SMatthew Dillon */ 30375c13541SPoul-Henning Kamp int 304c542c43eSJamie Gritton sys_jail(struct thread *td, struct jail_args *uap) 30575c13541SPoul-Henning Kamp { 306413628a7SBjoern A. Zeeb uint32_t version; 307413628a7SBjoern A. Zeeb int error; 3080304c731SJamie Gritton struct jail j; 309413628a7SBjoern A. Zeeb 310413628a7SBjoern A. Zeeb error = copyin(uap->jail, &version, sizeof(uint32_t)); 311413628a7SBjoern A. Zeeb if (error) 312413628a7SBjoern A. Zeeb return (error); 313413628a7SBjoern A. Zeeb 314413628a7SBjoern A. Zeeb switch (version) { 315413628a7SBjoern A. Zeeb case 0: 316413628a7SBjoern A. Zeeb { 317413628a7SBjoern A. Zeeb struct jail_v0 j0; 318413628a7SBjoern A. Zeeb 3190304c731SJamie Gritton /* FreeBSD single IPv4 jails. */ 3200304c731SJamie Gritton bzero(&j, sizeof(struct jail)); 321413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j0, sizeof(struct jail_v0)); 322413628a7SBjoern A. Zeeb if (error) 323413628a7SBjoern A. Zeeb return (error); 3240304c731SJamie Gritton j.version = j0.version; 3250304c731SJamie Gritton j.path = j0.path; 3260304c731SJamie Gritton j.hostname = j0.hostname; 327b5019bc4SPeter Wemm j.ip4s = htonl(j0.ip_number); /* jail_v0 is host order */ 328413628a7SBjoern A. Zeeb break; 329413628a7SBjoern A. Zeeb } 330413628a7SBjoern A. Zeeb 331413628a7SBjoern A. Zeeb case 1: 332413628a7SBjoern A. Zeeb /* 333413628a7SBjoern A. Zeeb * Version 1 was used by multi-IPv4 jail implementations 334413628a7SBjoern A. Zeeb * that never made it into the official kernel. 335413628a7SBjoern A. Zeeb */ 336413628a7SBjoern A. Zeeb return (EINVAL); 337413628a7SBjoern A. Zeeb 338413628a7SBjoern A. Zeeb case 2: /* JAIL_API_VERSION */ 339413628a7SBjoern A. Zeeb /* FreeBSD multi-IPv4/IPv6,noIP jails. */ 340413628a7SBjoern A. Zeeb error = copyin(uap->jail, &j, sizeof(struct jail)); 341413628a7SBjoern A. Zeeb if (error) 342413628a7SBjoern A. Zeeb return (error); 3430304c731SJamie Gritton break; 3440304c731SJamie Gritton 3450304c731SJamie Gritton default: 3460304c731SJamie Gritton /* Sci-Fi jails are not supported, sorry. */ 3470304c731SJamie Gritton return (EINVAL); 3480304c731SJamie Gritton } 349c542c43eSJamie Gritton return (kern_jail(td, &j)); 3500304c731SJamie Gritton } 3510304c731SJamie Gritton 3520304c731SJamie Gritton int 353c542c43eSJamie Gritton kern_jail(struct thread *td, struct jail *j) 3540304c731SJamie Gritton { 355c542c43eSJamie Gritton struct iovec optiov[2 * (4 + nitems(pr_flag_allow) 356e92e0574SJamie Gritton #ifdef INET 357e92e0574SJamie Gritton + 1 358e92e0574SJamie Gritton #endif 359e92e0574SJamie Gritton #ifdef INET6 360e92e0574SJamie Gritton + 1 361e92e0574SJamie Gritton #endif 362e92e0574SJamie Gritton )]; 3630304c731SJamie Gritton struct uio opt; 3640304c731SJamie Gritton char *u_path, *u_hostname, *u_name; 365672756aaSJamie Gritton struct bool_flags *bf; 3660304c731SJamie Gritton #ifdef INET 367e92e0574SJamie Gritton uint32_t ip4s; 3680304c731SJamie Gritton struct in_addr *u_ip4; 3690304c731SJamie Gritton #endif 3700304c731SJamie Gritton #ifdef INET6 3710304c731SJamie Gritton struct in6_addr *u_ip6; 3720304c731SJamie Gritton #endif 3730304c731SJamie Gritton size_t tmplen; 374c542c43eSJamie Gritton int error, enforce_statfs; 3750304c731SJamie Gritton 3760304c731SJamie Gritton bzero(&optiov, sizeof(optiov)); 3770304c731SJamie Gritton opt.uio_iov = optiov; 3780304c731SJamie Gritton opt.uio_iovcnt = 0; 3790304c731SJamie Gritton opt.uio_offset = -1; 3800304c731SJamie Gritton opt.uio_resid = -1; 3810304c731SJamie Gritton opt.uio_segflg = UIO_SYSSPACE; 3820304c731SJamie Gritton opt.uio_rw = UIO_READ; 3830304c731SJamie Gritton opt.uio_td = td; 3840304c731SJamie Gritton 3850304c731SJamie Gritton /* Set permissions for top-level jails from sysctls. */ 3860304c731SJamie Gritton if (!jailed(td->td_ucred)) { 387672756aaSJamie Gritton for (bf = pr_flag_allow; 3880e5c6bd4SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 3895d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 390672756aaSJamie Gritton bf++) { 391672756aaSJamie Gritton optiov[opt.uio_iovcnt].iov_base = __DECONST(char *, 392672756aaSJamie Gritton (jail_default_allow & bf->flag) 393672756aaSJamie Gritton ? bf->name : bf->noname); 3940304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = 3950304c731SJamie Gritton strlen(optiov[opt.uio_iovcnt].iov_base) + 1; 3960304c731SJamie Gritton opt.uio_iovcnt += 2; 3970304c731SJamie Gritton } 3980304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "enforce_statfs"; 3990304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("enforce_statfs"); 4000304c731SJamie Gritton opt.uio_iovcnt++; 4010304c731SJamie Gritton enforce_statfs = jail_default_enforce_statfs; 4020304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = &enforce_statfs; 4030304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof(enforce_statfs); 4040304c731SJamie Gritton opt.uio_iovcnt++; 4050304c731SJamie Gritton } 4060304c731SJamie Gritton 407b38ff370SJamie Gritton tmplen = MAXPATHLEN + MAXHOSTNAMELEN + MAXHOSTNAMELEN; 408b38ff370SJamie Gritton #ifdef INET 4090304c731SJamie Gritton ip4s = (j->version == 0) ? 1 : j->ip4s; 4100304c731SJamie Gritton if (ip4s > jail_max_af_ips) 411b38ff370SJamie Gritton return (EINVAL); 4120304c731SJamie Gritton tmplen += ip4s * sizeof(struct in_addr); 413b38ff370SJamie Gritton #else 4140304c731SJamie Gritton if (j->ip4s > 0) 415b38ff370SJamie Gritton return (EINVAL); 416b38ff370SJamie Gritton #endif 417b38ff370SJamie Gritton #ifdef INET6 4180304c731SJamie Gritton if (j->ip6s > jail_max_af_ips) 419b38ff370SJamie Gritton return (EINVAL); 4200304c731SJamie Gritton tmplen += j->ip6s * sizeof(struct in6_addr); 421b38ff370SJamie Gritton #else 4220304c731SJamie Gritton if (j->ip6s > 0) 423b38ff370SJamie Gritton return (EINVAL); 424b38ff370SJamie Gritton #endif 425b38ff370SJamie Gritton u_path = malloc(tmplen, M_TEMP, M_WAITOK); 426b38ff370SJamie Gritton u_hostname = u_path + MAXPATHLEN; 427b38ff370SJamie Gritton u_name = u_hostname + MAXHOSTNAMELEN; 428b38ff370SJamie Gritton #ifdef INET 429b38ff370SJamie Gritton u_ip4 = (struct in_addr *)(u_name + MAXHOSTNAMELEN); 430b38ff370SJamie Gritton #endif 431b38ff370SJamie Gritton #ifdef INET6 432b38ff370SJamie Gritton #ifdef INET 4330304c731SJamie Gritton u_ip6 = (struct in6_addr *)(u_ip4 + ip4s); 434b38ff370SJamie Gritton #else 435b38ff370SJamie Gritton u_ip6 = (struct in6_addr *)(u_name + MAXHOSTNAMELEN); 436b38ff370SJamie Gritton #endif 437b38ff370SJamie Gritton #endif 4380304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "path"; 4390304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("path"); 4400304c731SJamie Gritton opt.uio_iovcnt++; 4410304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_path; 4420304c731SJamie Gritton error = copyinstr(j->path, u_path, MAXPATHLEN, 4430304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 444b38ff370SJamie Gritton if (error) { 445b38ff370SJamie Gritton free(u_path, M_TEMP); 446b38ff370SJamie Gritton return (error); 447b38ff370SJamie Gritton } 4480304c731SJamie Gritton opt.uio_iovcnt++; 4490304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "host.hostname"; 4500304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("host.hostname"); 4510304c731SJamie Gritton opt.uio_iovcnt++; 4520304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_hostname; 4530304c731SJamie Gritton error = copyinstr(j->hostname, u_hostname, MAXHOSTNAMELEN, 4540304c731SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 455b38ff370SJamie Gritton if (error) { 456b38ff370SJamie Gritton free(u_path, M_TEMP); 457b38ff370SJamie Gritton return (error); 458b38ff370SJamie Gritton } 4590304c731SJamie Gritton opt.uio_iovcnt++; 4600304c731SJamie Gritton if (j->jailname != NULL) { 461b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "name"; 462b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("name"); 463b38ff370SJamie Gritton opt.uio_iovcnt++; 464b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_name; 4650304c731SJamie Gritton error = copyinstr(j->jailname, u_name, MAXHOSTNAMELEN, 466b38ff370SJamie Gritton &optiov[opt.uio_iovcnt].iov_len); 467b38ff370SJamie Gritton if (error) { 468b38ff370SJamie Gritton free(u_path, M_TEMP); 469b38ff370SJamie Gritton return (error); 470b38ff370SJamie Gritton } 471b38ff370SJamie Gritton opt.uio_iovcnt++; 472b38ff370SJamie Gritton } 473b38ff370SJamie Gritton #ifdef INET 474b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip4.addr"; 475b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip4.addr"); 476b38ff370SJamie Gritton opt.uio_iovcnt++; 477b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip4; 4780304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = ip4s * sizeof(struct in_addr); 4790304c731SJamie Gritton if (j->version == 0) 4800304c731SJamie Gritton u_ip4->s_addr = j->ip4s; 4810304c731SJamie Gritton else { 4820304c731SJamie Gritton error = copyin(j->ip4, u_ip4, optiov[opt.uio_iovcnt].iov_len); 483b38ff370SJamie Gritton if (error) { 484b38ff370SJamie Gritton free(u_path, M_TEMP); 485b38ff370SJamie Gritton return (error); 486b38ff370SJamie Gritton } 4870304c731SJamie Gritton } 488b38ff370SJamie Gritton opt.uio_iovcnt++; 489b38ff370SJamie Gritton #endif 490b38ff370SJamie Gritton #ifdef INET6 491b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = "ip6.addr"; 492b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_len = sizeof("ip6.addr"); 493b38ff370SJamie Gritton opt.uio_iovcnt++; 494b38ff370SJamie Gritton optiov[opt.uio_iovcnt].iov_base = u_ip6; 4950304c731SJamie Gritton optiov[opt.uio_iovcnt].iov_len = j->ip6s * sizeof(struct in6_addr); 4960304c731SJamie Gritton error = copyin(j->ip6, u_ip6, optiov[opt.uio_iovcnt].iov_len); 497b38ff370SJamie Gritton if (error) { 498b38ff370SJamie Gritton free(u_path, M_TEMP); 499b38ff370SJamie Gritton return (error); 500b38ff370SJamie Gritton } 501b38ff370SJamie Gritton opt.uio_iovcnt++; 502b38ff370SJamie Gritton #endif 50302abd400SPedro F. Giffuni KASSERT(opt.uio_iovcnt <= nitems(optiov), 5040304c731SJamie Gritton ("kern_jail: too many iovecs (%d)", opt.uio_iovcnt)); 505b38ff370SJamie Gritton error = kern_jail_set(td, &opt, JAIL_CREATE | JAIL_ATTACH); 506b38ff370SJamie Gritton free(u_path, M_TEMP); 507b38ff370SJamie Gritton return (error); 508b38ff370SJamie Gritton } 509b38ff370SJamie Gritton 510b38ff370SJamie Gritton /* 511b38ff370SJamie Gritton * struct jail_set_args { 512b38ff370SJamie Gritton * struct iovec *iovp; 513b38ff370SJamie Gritton * unsigned int iovcnt; 514b38ff370SJamie Gritton * int flags; 515b38ff370SJamie Gritton * }; 516b38ff370SJamie Gritton */ 517b38ff370SJamie Gritton int 5188451d0ddSKip Macy sys_jail_set(struct thread *td, struct jail_set_args *uap) 519b38ff370SJamie Gritton { 520b38ff370SJamie Gritton struct uio *auio; 521b38ff370SJamie Gritton int error; 522b38ff370SJamie Gritton 523b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 524b38ff370SJamie Gritton if (uap->iovcnt & 1) 525b38ff370SJamie Gritton return (EINVAL); 526b38ff370SJamie Gritton 527b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 528b38ff370SJamie Gritton if (error) 529b38ff370SJamie Gritton return (error); 530b38ff370SJamie Gritton error = kern_jail_set(td, auio, uap->flags); 531b38ff370SJamie Gritton free(auio, M_IOV); 532b38ff370SJamie Gritton return (error); 533413628a7SBjoern A. Zeeb } 534413628a7SBjoern A. Zeeb 535*eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 536*eb8dcdeaSGleb Smirnoff typedef int prison_addr_cmp_t(const void *, const void *); 537*eb8dcdeaSGleb Smirnoff typedef bool prison_addr_valid_t(const void *); 538*eb8dcdeaSGleb Smirnoff static const struct pr_family { 539*eb8dcdeaSGleb Smirnoff size_t size; 540*eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *cmp; 541*eb8dcdeaSGleb Smirnoff prison_addr_valid_t *valid; 542*eb8dcdeaSGleb Smirnoff int ip_flag; 543*eb8dcdeaSGleb Smirnoff } pr_families[PR_FAMILY_MAX] = { 544*eb8dcdeaSGleb Smirnoff #ifdef INET 545*eb8dcdeaSGleb Smirnoff [PR_INET] = { 546*eb8dcdeaSGleb Smirnoff .size = sizeof(struct in_addr), 547*eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v4, 548*eb8dcdeaSGleb Smirnoff .valid = prison_valid_v4, 549*eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP4_USER, 550*eb8dcdeaSGleb Smirnoff }, 551*eb8dcdeaSGleb Smirnoff #endif 552*eb8dcdeaSGleb Smirnoff #ifdef INET6 553*eb8dcdeaSGleb Smirnoff [PR_INET6] = { 554*eb8dcdeaSGleb Smirnoff .size = sizeof(struct in6_addr), 555*eb8dcdeaSGleb Smirnoff .cmp = prison_qcmp_v6, 556*eb8dcdeaSGleb Smirnoff .valid = prison_valid_v6, 557*eb8dcdeaSGleb Smirnoff .ip_flag = PR_IP6_USER, 558*eb8dcdeaSGleb Smirnoff }, 559*eb8dcdeaSGleb Smirnoff #endif 560*eb8dcdeaSGleb Smirnoff }; 561*eb8dcdeaSGleb Smirnoff 562*eb8dcdeaSGleb Smirnoff /* 563*eb8dcdeaSGleb Smirnoff * Network address lists (pr_addrs) allocation for jails. The addresses 564*eb8dcdeaSGleb Smirnoff * are accessed locklessly by the network stack, thus need to be protected by 565*eb8dcdeaSGleb Smirnoff * the network epoch. 566*eb8dcdeaSGleb Smirnoff */ 567*eb8dcdeaSGleb Smirnoff struct prison_ip { 568*eb8dcdeaSGleb Smirnoff struct epoch_context ctx; 569*eb8dcdeaSGleb Smirnoff uint32_t ips; 570*eb8dcdeaSGleb Smirnoff #ifdef FUTURE_C 571*eb8dcdeaSGleb Smirnoff union { 572*eb8dcdeaSGleb Smirnoff struct in_addr pr_ip4[]; 573*eb8dcdeaSGleb Smirnoff struct in6_addr pr_ip6[]; 574*eb8dcdeaSGleb Smirnoff }; 575*eb8dcdeaSGleb Smirnoff #else /* No future C :( */ 576*eb8dcdeaSGleb Smirnoff #define PR_IP(pip, i) ((const char *)((pip) + 1) + pr_families[af].size * (i)) 577*eb8dcdeaSGleb Smirnoff #define PR_IPD(pip, i) ((char *)((pip) + 1) + pr_families[af].size * (i)) 578*eb8dcdeaSGleb Smirnoff #endif 579*eb8dcdeaSGleb Smirnoff }; 580*eb8dcdeaSGleb Smirnoff 581*eb8dcdeaSGleb Smirnoff static struct prison_ip * 582*eb8dcdeaSGleb Smirnoff prison_ip_alloc(const pr_family_t af, uint32_t cnt, int flags) 583*eb8dcdeaSGleb Smirnoff { 584*eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 585*eb8dcdeaSGleb Smirnoff 586*eb8dcdeaSGleb Smirnoff pip = malloc(sizeof(struct prison_ip) + cnt * pr_families[af].size, 587*eb8dcdeaSGleb Smirnoff M_PRISON, flags); 588*eb8dcdeaSGleb Smirnoff if (pip != NULL) 589*eb8dcdeaSGleb Smirnoff pip->ips = cnt; 590*eb8dcdeaSGleb Smirnoff return (pip); 591*eb8dcdeaSGleb Smirnoff } 592*eb8dcdeaSGleb Smirnoff 593*eb8dcdeaSGleb Smirnoff /* 594*eb8dcdeaSGleb Smirnoff * Allocate and copyin user supplied address list, sorting and validating. 595*eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 596*eb8dcdeaSGleb Smirnoff */ 597*eb8dcdeaSGleb Smirnoff static struct prison_ip * 598*eb8dcdeaSGleb Smirnoff prison_ip_copyin(const pr_family_t af, void *op, uint32_t cnt) 599*eb8dcdeaSGleb Smirnoff { 600*eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 601*eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 602*eb8dcdeaSGleb Smirnoff struct prison_ip *pip; 603*eb8dcdeaSGleb Smirnoff 604*eb8dcdeaSGleb Smirnoff pip = prison_ip_alloc(af, cnt, M_WAITOK); 605*eb8dcdeaSGleb Smirnoff bcopy(op, pip + 1, cnt * size); 606*eb8dcdeaSGleb Smirnoff /* 607*eb8dcdeaSGleb Smirnoff * IP addresses are all sorted but ip[0] to preserve 608*eb8dcdeaSGleb Smirnoff * the primary IP address as given from userland. 609*eb8dcdeaSGleb Smirnoff * This special IP is used for unbound outgoing 610*eb8dcdeaSGleb Smirnoff * connections as well for "loopback" traffic in case 611*eb8dcdeaSGleb Smirnoff * source address selection cannot find any more fitting 612*eb8dcdeaSGleb Smirnoff * address to connect from. 613*eb8dcdeaSGleb Smirnoff */ 614*eb8dcdeaSGleb Smirnoff if (cnt > 1) 615*eb8dcdeaSGleb Smirnoff qsort((char *)(pip + 1) + size, cnt - 1, size, 616*eb8dcdeaSGleb Smirnoff pr_families[af].cmp); 617*eb8dcdeaSGleb Smirnoff /* 618*eb8dcdeaSGleb Smirnoff * Check for duplicate addresses and do some simple 619*eb8dcdeaSGleb Smirnoff * zero and broadcast checks. If users give other bogus 620*eb8dcdeaSGleb Smirnoff * addresses it is their problem. 621*eb8dcdeaSGleb Smirnoff */ 622*eb8dcdeaSGleb Smirnoff for (int i = 0; i < cnt; i++) { 623*eb8dcdeaSGleb Smirnoff if (!pr_families[af].valid(PR_IP(pip, i))) { 624*eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 625*eb8dcdeaSGleb Smirnoff return (NULL); 626*eb8dcdeaSGleb Smirnoff } 627*eb8dcdeaSGleb Smirnoff if (i + 1 < cnt && 628*eb8dcdeaSGleb Smirnoff (cmp(PR_IP(pip, 0), PR_IP(pip, i + 1)) == 0 || 629*eb8dcdeaSGleb Smirnoff cmp(PR_IP(pip, i), PR_IP(pip, i + 1)) == 0)) { 630*eb8dcdeaSGleb Smirnoff free(pip, M_PRISON); 631*eb8dcdeaSGleb Smirnoff return (NULL); 632*eb8dcdeaSGleb Smirnoff } 633*eb8dcdeaSGleb Smirnoff } 634*eb8dcdeaSGleb Smirnoff 635*eb8dcdeaSGleb Smirnoff return (pip); 636*eb8dcdeaSGleb Smirnoff } 637*eb8dcdeaSGleb Smirnoff 638*eb8dcdeaSGleb Smirnoff /* 639*eb8dcdeaSGleb Smirnoff * Allocate and dup parent prison address list. 640*eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 641*eb8dcdeaSGleb Smirnoff */ 642*eb8dcdeaSGleb Smirnoff static void 643*eb8dcdeaSGleb Smirnoff prison_ip_dup(struct prison *ppr, struct prison *pr, const pr_family_t af) 644*eb8dcdeaSGleb Smirnoff { 645*eb8dcdeaSGleb Smirnoff 646*eb8dcdeaSGleb Smirnoff if (ppr->pr_addrs[af] != NULL) { 647*eb8dcdeaSGleb Smirnoff pr->pr_addrs[af] = prison_ip_alloc(af, 648*eb8dcdeaSGleb Smirnoff ppr->pr_addrs[af]->ips, M_WAITOK); 649*eb8dcdeaSGleb Smirnoff bcopy(ppr->pr_addrs[af], pr->pr_addrs[af], 650*eb8dcdeaSGleb Smirnoff pr->pr_addrs[af]->ips * pr_families[af].size); 651*eb8dcdeaSGleb Smirnoff } 652*eb8dcdeaSGleb Smirnoff } 653*eb8dcdeaSGleb Smirnoff 654*eb8dcdeaSGleb Smirnoff /* 655*eb8dcdeaSGleb Smirnoff * Make sure the new set of IP addresses is a subset of the parent's list. 656*eb8dcdeaSGleb Smirnoff * Don't worry about the parent being unlocked, as any setting is done with 657*eb8dcdeaSGleb Smirnoff * allprison_lock held. 658*eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 659*eb8dcdeaSGleb Smirnoff */ 660*eb8dcdeaSGleb Smirnoff static bool 661*eb8dcdeaSGleb Smirnoff prison_ip_parent_match(const struct prison_ip *ppip, 662*eb8dcdeaSGleb Smirnoff const struct prison_ip *pip, const pr_family_t af) 663*eb8dcdeaSGleb Smirnoff { 664*eb8dcdeaSGleb Smirnoff prison_addr_cmp_t *const cmp = pr_families[af].cmp; 665*eb8dcdeaSGleb Smirnoff int i, j; 666*eb8dcdeaSGleb Smirnoff 667*eb8dcdeaSGleb Smirnoff if (ppip == NULL) 668*eb8dcdeaSGleb Smirnoff return (false); 669*eb8dcdeaSGleb Smirnoff 670*eb8dcdeaSGleb Smirnoff for (i = 0; i < ppip->ips; i++) 671*eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), PR_IP(ppip, i)) == 0) 672*eb8dcdeaSGleb Smirnoff break; 673*eb8dcdeaSGleb Smirnoff 674*eb8dcdeaSGleb Smirnoff if (i == ppip->ips) 675*eb8dcdeaSGleb Smirnoff /* Main address not present in parent. */ 676*eb8dcdeaSGleb Smirnoff return (false); 677*eb8dcdeaSGleb Smirnoff 678*eb8dcdeaSGleb Smirnoff if (pip->ips > 1) { 679*eb8dcdeaSGleb Smirnoff for (i = j = 1; i < pip->ips; i++) { 680*eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) 681*eb8dcdeaSGleb Smirnoff /* Equals to parent primary address. */ 682*eb8dcdeaSGleb Smirnoff continue; 683*eb8dcdeaSGleb Smirnoff for (; j < ppip->ips; j++) 684*eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, j)) == 0) 685*eb8dcdeaSGleb Smirnoff break; 686*eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 687*eb8dcdeaSGleb Smirnoff break; 688*eb8dcdeaSGleb Smirnoff } 689*eb8dcdeaSGleb Smirnoff if (j == ppip->ips) 690*eb8dcdeaSGleb Smirnoff /* Address not present in parent. */ 691*eb8dcdeaSGleb Smirnoff return (false); 692*eb8dcdeaSGleb Smirnoff } 693*eb8dcdeaSGleb Smirnoff return (true); 694*eb8dcdeaSGleb Smirnoff } 695*eb8dcdeaSGleb Smirnoff 696*eb8dcdeaSGleb Smirnoff /* 697*eb8dcdeaSGleb Smirnoff * Check for conflicting IP addresses. We permit them if there is no more 698*eb8dcdeaSGleb Smirnoff * than one IP on each jail. If there is a duplicate on a jail with more 699*eb8dcdeaSGleb Smirnoff * than one IP stop checking and return error. 700*eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 701*eb8dcdeaSGleb Smirnoff */ 702*eb8dcdeaSGleb Smirnoff static bool 703*eb8dcdeaSGleb Smirnoff prison_ip_conflict_check(const struct prison *ppr, const struct prison *pr, 704*eb8dcdeaSGleb Smirnoff const struct prison_ip *pip, pr_family_t af) 705*eb8dcdeaSGleb Smirnoff { 706*eb8dcdeaSGleb Smirnoff const struct prison *tppr, *tpr; 707*eb8dcdeaSGleb Smirnoff int descend; 708*eb8dcdeaSGleb Smirnoff 709*eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 710*eb8dcdeaSGleb Smirnoff for (tppr = ppr; tppr != &prison0; tppr = tppr->pr_parent) 711*eb8dcdeaSGleb Smirnoff if (tppr->pr_flags & PR_VNET) 712*eb8dcdeaSGleb Smirnoff break; 713*eb8dcdeaSGleb Smirnoff #else 714*eb8dcdeaSGleb Smirnoff tppr = &prison0; 715*eb8dcdeaSGleb Smirnoff #endif 716*eb8dcdeaSGleb Smirnoff FOREACH_PRISON_DESCENDANT(tppr, tpr, descend) { 717*eb8dcdeaSGleb Smirnoff if (tpr == pr || 718*eb8dcdeaSGleb Smirnoff #ifdef VIMAGE 719*eb8dcdeaSGleb Smirnoff (tpr != tppr && (tpr->pr_flags & PR_VNET)) || 720*eb8dcdeaSGleb Smirnoff #endif 721*eb8dcdeaSGleb Smirnoff !prison_isalive(tpr)) { 722*eb8dcdeaSGleb Smirnoff descend = 0; 723*eb8dcdeaSGleb Smirnoff continue; 724*eb8dcdeaSGleb Smirnoff } 725*eb8dcdeaSGleb Smirnoff if (!(tpr->pr_flags & pr_families[af].ip_flag)) 726*eb8dcdeaSGleb Smirnoff continue; 727*eb8dcdeaSGleb Smirnoff descend = 0; 728*eb8dcdeaSGleb Smirnoff if (tpr->pr_addrs[af] == NULL || 729*eb8dcdeaSGleb Smirnoff (pip->ips == 1 && tpr->pr_addrs[af]->ips == 1)) 730*eb8dcdeaSGleb Smirnoff continue; 731*eb8dcdeaSGleb Smirnoff for (int i = 0; i < pip->ips; i++) 732*eb8dcdeaSGleb Smirnoff if (prison_ip_check(tpr, af, PR_IP(pip, i)) == 0) 733*eb8dcdeaSGleb Smirnoff return (false); 734*eb8dcdeaSGleb Smirnoff } 735*eb8dcdeaSGleb Smirnoff 736*eb8dcdeaSGleb Smirnoff return (true); 737*eb8dcdeaSGleb Smirnoff } 738*eb8dcdeaSGleb Smirnoff 739*eb8dcdeaSGleb Smirnoff _Static_assert(offsetof(struct prison_ip, ctx) == 0, 740*eb8dcdeaSGleb Smirnoff "prison must start with epoch context"); 741*eb8dcdeaSGleb Smirnoff static void 742*eb8dcdeaSGleb Smirnoff prison_ip_free_deferred(epoch_context_t ctx) 743*eb8dcdeaSGleb Smirnoff { 744*eb8dcdeaSGleb Smirnoff 745*eb8dcdeaSGleb Smirnoff free(ctx, M_PRISON); 746*eb8dcdeaSGleb Smirnoff } 747*eb8dcdeaSGleb Smirnoff 748*eb8dcdeaSGleb Smirnoff static void 749*eb8dcdeaSGleb Smirnoff prison_ip_free(struct prison_ip *pip) 750*eb8dcdeaSGleb Smirnoff { 751*eb8dcdeaSGleb Smirnoff 752*eb8dcdeaSGleb Smirnoff if (pip != NULL) 753*eb8dcdeaSGleb Smirnoff NET_EPOCH_CALL(prison_ip_free_deferred, &pip->ctx); 754*eb8dcdeaSGleb Smirnoff } 755*eb8dcdeaSGleb Smirnoff 756*eb8dcdeaSGleb Smirnoff static void 757*eb8dcdeaSGleb Smirnoff prison_ip_set(struct prison *pr, const pr_family_t af, struct prison_ip *new) 758*eb8dcdeaSGleb Smirnoff { 759*eb8dcdeaSGleb Smirnoff struct prison_ip **mem, *old; 760*eb8dcdeaSGleb Smirnoff 761*eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 762*eb8dcdeaSGleb Smirnoff 763*eb8dcdeaSGleb Smirnoff mem = &pr->pr_addrs[af]; 764*eb8dcdeaSGleb Smirnoff 765*eb8dcdeaSGleb Smirnoff old = *mem; 766*eb8dcdeaSGleb Smirnoff ck_pr_store_ptr(mem, new); 767*eb8dcdeaSGleb Smirnoff prison_ip_free(old); 768*eb8dcdeaSGleb Smirnoff } 769*eb8dcdeaSGleb Smirnoff 770*eb8dcdeaSGleb Smirnoff /* 771*eb8dcdeaSGleb Smirnoff * Restrict a prison's IP address list with its parent's, possibly replacing 772*eb8dcdeaSGleb Smirnoff * it. Return true if the replacement buffer was used (or would have been). 773*eb8dcdeaSGleb Smirnoff * kern_jail_set() helper. 774*eb8dcdeaSGleb Smirnoff */ 775*eb8dcdeaSGleb Smirnoff static bool 776*eb8dcdeaSGleb Smirnoff prison_ip_restrict(struct prison *pr, const pr_family_t af, 777*eb8dcdeaSGleb Smirnoff struct prison_ip *new) 778*eb8dcdeaSGleb Smirnoff { 779*eb8dcdeaSGleb Smirnoff const struct prison_ip *ppip = pr->pr_parent->pr_addrs[af]; 780*eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 781*eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 782*eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 783*eb8dcdeaSGleb Smirnoff uint32_t ips; 784*eb8dcdeaSGleb Smirnoff bool alloced; 785*eb8dcdeaSGleb Smirnoff 786*eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 787*eb8dcdeaSGleb Smirnoff 788*eb8dcdeaSGleb Smirnoff /* 789*eb8dcdeaSGleb Smirnoff * Due to epoch-synchronized access to the IP address lists we always 790*eb8dcdeaSGleb Smirnoff * allocate a new list even if the old one has enough space. We could 791*eb8dcdeaSGleb Smirnoff * atomically update an IPv4 address inside a list, but that would 792*eb8dcdeaSGleb Smirnoff * screw up sorting, and in case of IPv6 we can't even atomically write 793*eb8dcdeaSGleb Smirnoff * one. 794*eb8dcdeaSGleb Smirnoff */ 795*eb8dcdeaSGleb Smirnoff ips = (pr->pr_flags & pr_families[af].ip_flag) ? pip->ips : ppip->ips; 796*eb8dcdeaSGleb Smirnoff if (ips == 0) { 797*eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, NULL); 798*eb8dcdeaSGleb Smirnoff return (false); 799*eb8dcdeaSGleb Smirnoff } 800*eb8dcdeaSGleb Smirnoff if (new == NULL) { 801*eb8dcdeaSGleb Smirnoff new = prison_ip_alloc(af, ips, M_NOWAIT); 802*eb8dcdeaSGleb Smirnoff if (new == NULL) 803*eb8dcdeaSGleb Smirnoff return (true); 804*eb8dcdeaSGleb Smirnoff alloced = true; 805*eb8dcdeaSGleb Smirnoff } else 806*eb8dcdeaSGleb Smirnoff alloced = false; 807*eb8dcdeaSGleb Smirnoff if (!(pr->pr_flags & pr_families[af].ip_flag)) { 808*eb8dcdeaSGleb Smirnoff /* This has no user settings, so just copy the parent's list. */ 809*eb8dcdeaSGleb Smirnoff bcopy(ppip, new, ips * size); 810*eb8dcdeaSGleb Smirnoff } else { 811*eb8dcdeaSGleb Smirnoff /* Remove addresses that aren't in the parent. */ 812*eb8dcdeaSGleb Smirnoff int i; 813*eb8dcdeaSGleb Smirnoff 814*eb8dcdeaSGleb Smirnoff i = 0; /* index in pip */ 815*eb8dcdeaSGleb Smirnoff ips = 0; /* index in new */ 816*eb8dcdeaSGleb Smirnoff 817*eb8dcdeaSGleb Smirnoff for (int pi = 0; pi < ppip->ips; pi++) 818*eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), PR_IP(ppip, pi)) == 0) { 819*eb8dcdeaSGleb Smirnoff /* Found our primary address in parent. */ 820*eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 821*eb8dcdeaSGleb Smirnoff i++; 822*eb8dcdeaSGleb Smirnoff ips++; 823*eb8dcdeaSGleb Smirnoff break; 824*eb8dcdeaSGleb Smirnoff } 825*eb8dcdeaSGleb Smirnoff for (int pi = 1; i < pip->ips; ) { 826*eb8dcdeaSGleb Smirnoff /* Check against primary, which is unsorted. */ 827*eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, i), PR_IP(ppip, 0)) == 0) { 828*eb8dcdeaSGleb Smirnoff /* Matches parent's primary address. */ 829*eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pip, i), PR_IPD(new, ips), size); 830*eb8dcdeaSGleb Smirnoff i++; 831*eb8dcdeaSGleb Smirnoff ips++; 832*eb8dcdeaSGleb Smirnoff continue; 833*eb8dcdeaSGleb Smirnoff } 834*eb8dcdeaSGleb Smirnoff /* The rest are sorted. */ 835*eb8dcdeaSGleb Smirnoff switch (pi >= ppip->ips ? -1 : 836*eb8dcdeaSGleb Smirnoff cmp(PR_IP(pip, i), PR_IP(ppip, pi))) { 837*eb8dcdeaSGleb Smirnoff case -1: 838*eb8dcdeaSGleb Smirnoff i++; 839*eb8dcdeaSGleb Smirnoff break; 840*eb8dcdeaSGleb Smirnoff case 0: 841*eb8dcdeaSGleb Smirnoff bcopy(PR_IP(pr, i), PR_IPD(new, ips), size); 842*eb8dcdeaSGleb Smirnoff i++; 843*eb8dcdeaSGleb Smirnoff pi++; 844*eb8dcdeaSGleb Smirnoff ips++; 845*eb8dcdeaSGleb Smirnoff break; 846*eb8dcdeaSGleb Smirnoff case 1: 847*eb8dcdeaSGleb Smirnoff pi++; 848*eb8dcdeaSGleb Smirnoff break; 849*eb8dcdeaSGleb Smirnoff } 850*eb8dcdeaSGleb Smirnoff } 851*eb8dcdeaSGleb Smirnoff if (ips == 0) { 852*eb8dcdeaSGleb Smirnoff if (alloced) 853*eb8dcdeaSGleb Smirnoff prison_ip_free(new); 854*eb8dcdeaSGleb Smirnoff new = NULL; 855*eb8dcdeaSGleb Smirnoff } 856*eb8dcdeaSGleb Smirnoff } 857*eb8dcdeaSGleb Smirnoff prison_ip_set(pr, af, new); 858*eb8dcdeaSGleb Smirnoff return (new != NULL ? true : false); 859*eb8dcdeaSGleb Smirnoff } 860*eb8dcdeaSGleb Smirnoff 861*eb8dcdeaSGleb Smirnoff /* 862*eb8dcdeaSGleb Smirnoff * Fast-path check if an address belongs to a prison. 863*eb8dcdeaSGleb Smirnoff */ 864*eb8dcdeaSGleb Smirnoff int 865*eb8dcdeaSGleb Smirnoff prison_ip_check(const struct prison *pr, const pr_family_t af, 866*eb8dcdeaSGleb Smirnoff const void *addr) 867*eb8dcdeaSGleb Smirnoff { 868*eb8dcdeaSGleb Smirnoff int (*const cmp)(const void *, const void *) = pr_families[af].cmp; 869*eb8dcdeaSGleb Smirnoff const struct prison_ip *pip; 870*eb8dcdeaSGleb Smirnoff int i, a, z, d; 871*eb8dcdeaSGleb Smirnoff 872*eb8dcdeaSGleb Smirnoff MPASS(mtx_owned(&pr->pr_mtx) || 873*eb8dcdeaSGleb Smirnoff in_epoch(net_epoch_preempt) || 874*eb8dcdeaSGleb Smirnoff sx_xlocked(&allprison_lock)); 875*eb8dcdeaSGleb Smirnoff 876*eb8dcdeaSGleb Smirnoff pip = ck_pr_load_ptr(&pr->pr_addrs[af]); 877*eb8dcdeaSGleb Smirnoff if (__predict_false(pip == NULL)) 878*eb8dcdeaSGleb Smirnoff return (EAFNOSUPPORT); 879*eb8dcdeaSGleb Smirnoff 880*eb8dcdeaSGleb Smirnoff /* Check the primary IP. */ 881*eb8dcdeaSGleb Smirnoff if (cmp(PR_IP(pip, 0), addr) == 0) 882*eb8dcdeaSGleb Smirnoff return (0); 883*eb8dcdeaSGleb Smirnoff 884*eb8dcdeaSGleb Smirnoff /* 885*eb8dcdeaSGleb Smirnoff * All the other IPs are sorted so we can do a binary search. 886*eb8dcdeaSGleb Smirnoff */ 887*eb8dcdeaSGleb Smirnoff a = 0; 888*eb8dcdeaSGleb Smirnoff z = pip->ips - 2; 889*eb8dcdeaSGleb Smirnoff while (a <= z) { 890*eb8dcdeaSGleb Smirnoff i = (a + z) / 2; 891*eb8dcdeaSGleb Smirnoff d = cmp(PR_IP(pip, i + 1), addr); 892*eb8dcdeaSGleb Smirnoff if (d > 0) 893*eb8dcdeaSGleb Smirnoff z = i - 1; 894*eb8dcdeaSGleb Smirnoff else if (d < 0) 895*eb8dcdeaSGleb Smirnoff a = i + 1; 896*eb8dcdeaSGleb Smirnoff else 897*eb8dcdeaSGleb Smirnoff return (0); 898*eb8dcdeaSGleb Smirnoff } 899*eb8dcdeaSGleb Smirnoff 900*eb8dcdeaSGleb Smirnoff return (EADDRNOTAVAIL); 901*eb8dcdeaSGleb Smirnoff } 902*eb8dcdeaSGleb Smirnoff 903*eb8dcdeaSGleb Smirnoff /* 904*eb8dcdeaSGleb Smirnoff * Grab primary IP. Historically required mutex, but nothing prevents 905*eb8dcdeaSGleb Smirnoff * us to support epoch-protected access. Is it used in fast path? 906*eb8dcdeaSGleb Smirnoff * in{6}_jail.c helper 907*eb8dcdeaSGleb Smirnoff */ 908*eb8dcdeaSGleb Smirnoff const void * 909*eb8dcdeaSGleb Smirnoff prison_ip_get0(const struct prison *pr, const pr_family_t af) 910*eb8dcdeaSGleb Smirnoff { 911*eb8dcdeaSGleb Smirnoff const struct prison_ip *pip = pr->pr_addrs[af]; 912*eb8dcdeaSGleb Smirnoff 913*eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 914*eb8dcdeaSGleb Smirnoff MPASS(pip); 915*eb8dcdeaSGleb Smirnoff 916*eb8dcdeaSGleb Smirnoff return (pip + 1); 917*eb8dcdeaSGleb Smirnoff } 918*eb8dcdeaSGleb Smirnoff 919*eb8dcdeaSGleb Smirnoff u_int 920*eb8dcdeaSGleb Smirnoff prison_ip_cnt(const struct prison *pr, const pr_family_t af) 921*eb8dcdeaSGleb Smirnoff { 922*eb8dcdeaSGleb Smirnoff 923*eb8dcdeaSGleb Smirnoff return (pr->pr_addrs[af]->ips); 924*eb8dcdeaSGleb Smirnoff } 925*eb8dcdeaSGleb Smirnoff #endif /* defined(INET) || defined(INET6) */ 926*eb8dcdeaSGleb Smirnoff 927413628a7SBjoern A. Zeeb int 928b38ff370SJamie Gritton kern_jail_set(struct thread *td, struct uio *optuio, int flags) 929413628a7SBjoern A. Zeeb { 930fd7a8150SMike Barcroft struct nameidata nd; 931b38ff370SJamie Gritton #ifdef INET 932*eb8dcdeaSGleb Smirnoff struct prison_ip *ip4; 933413628a7SBjoern A. Zeeb #endif 934b38ff370SJamie Gritton #ifdef INET6 935*eb8dcdeaSGleb Smirnoff struct prison_ip *ip6; 936b38ff370SJamie Gritton #endif 937b38ff370SJamie Gritton struct vfsopt *opt; 938b38ff370SJamie Gritton struct vfsoptlist *opts; 9397de883c8SJamie Gritton struct prison *pr, *deadpr, *inspr, *mypr, *ppr, *tpr; 940b38ff370SJamie Gritton struct vnode *root; 941babbbb9cSJamie Gritton char *domain, *errmsg, *host, *name, *namelc, *p, *path, *uuid; 942b96bd95bSIan Lepore char *g_path, *osrelstr; 943672756aaSJamie Gritton struct bool_flags *bf; 944672756aaSJamie Gritton struct jailsys_flags *jsf; 9450304c731SJamie Gritton #if defined(INET) || defined(INET6) 946b38ff370SJamie Gritton void *op; 9470304c731SJamie Gritton #endif 94876ca6f88SJamie Gritton unsigned long hid; 949b6f47c23SJamie Gritton size_t namelen, onamelen, pnamelen; 9502a4b2251SJamie Gritton int born, created, cuflags, descend, drflags, enforce; 951cc5fd8c7SJamie Gritton int error, errmsg_len, errmsg_pos; 9520cc207a6SMartin Matuska int gotchildmax, gotenforce, gothid, gotrsnum, gotslevel; 953672756aaSJamie Gritton int jid, jsys, len, level; 954b96bd95bSIan Lepore int childmax, osreldt, rsnum, slevel; 955413628a7SBjoern A. Zeeb #ifdef INET 9562b0d6f81SJamie Gritton int ip4s, redo_ip4; 957413628a7SBjoern A. Zeeb #endif 958b38ff370SJamie Gritton #ifdef INET6 9592b0d6f81SJamie Gritton int ip6s, redo_ip6; 960b38ff370SJamie Gritton #endif 9616beb3bb4SKirk McKusick uint64_t pr_allow, ch_allow, pr_flags, ch_flags; 962b3079544SJamie Gritton uint64_t pr_allow_diff; 9636beb3bb4SKirk McKusick unsigned tallow; 964b38ff370SJamie Gritton char numbuf[12]; 965b38ff370SJamie Gritton 966b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_SET); 967b38ff370SJamie Gritton if (!error && (flags & JAIL_ATTACH)) 968b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 969b38ff370SJamie Gritton if (error) 97075c13541SPoul-Henning Kamp return (error); 971b6f47c23SJamie Gritton mypr = td->td_ucred->cr_prison; 972b97457e2SJamie Gritton if ((flags & JAIL_CREATE) && mypr->pr_childmax == 0) 9730304c731SJamie Gritton return (EPERM); 974b38ff370SJamie Gritton if (flags & ~JAIL_SET_MASK) 975b38ff370SJamie Gritton return (EINVAL); 976b38ff370SJamie Gritton 977b38ff370SJamie Gritton /* 978b38ff370SJamie Gritton * Check all the parameters before committing to anything. Not all 979b38ff370SJamie Gritton * errors can be caught early, but we may as well try. Also, this 980b38ff370SJamie Gritton * takes care of some expensive stuff (path lookup) before getting 981b38ff370SJamie Gritton * the allprison lock. 982b38ff370SJamie Gritton * 983b38ff370SJamie Gritton * XXX Jails are not filesystems, and jail parameters are not mount 984b38ff370SJamie Gritton * options. But it makes more sense to re-use the vfsopt code 985b38ff370SJamie Gritton * than duplicate it under a different name. 986b38ff370SJamie Gritton */ 987b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 988b38ff370SJamie Gritton if (error) 989b38ff370SJamie Gritton return (error); 990b38ff370SJamie Gritton #ifdef INET 991b38ff370SJamie Gritton ip4 = NULL; 992b38ff370SJamie Gritton #endif 993b38ff370SJamie Gritton #ifdef INET6 994b38ff370SJamie Gritton ip6 = NULL; 995b38ff370SJamie Gritton #endif 9966dfe0a3dSMartin Matuska g_path = NULL; 997b38ff370SJamie Gritton 998b6f47c23SJamie Gritton cuflags = flags & (JAIL_CREATE | JAIL_UPDATE); 999b6f47c23SJamie Gritton if (!cuflags) { 1000b6f47c23SJamie Gritton error = EINVAL; 1001b6f47c23SJamie Gritton vfs_opterror(opts, "no valid operation (create or update)"); 1002b6f47c23SJamie Gritton goto done_errmsg; 1003b6f47c23SJamie Gritton } 1004b6f47c23SJamie Gritton 1005b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 1006b38ff370SJamie Gritton if (error == ENOENT) 1007b38ff370SJamie Gritton jid = 0; 1008b38ff370SJamie Gritton else if (error != 0) 1009b38ff370SJamie Gritton goto done_free; 1010b38ff370SJamie Gritton 1011b38ff370SJamie Gritton error = vfs_copyopt(opts, "securelevel", &slevel, sizeof(slevel)); 1012b38ff370SJamie Gritton if (error == ENOENT) 1013b38ff370SJamie Gritton gotslevel = 0; 1014b38ff370SJamie Gritton else if (error != 0) 1015b38ff370SJamie Gritton goto done_free; 1016b38ff370SJamie Gritton else 1017b38ff370SJamie Gritton gotslevel = 1; 1018b38ff370SJamie Gritton 1019b97457e2SJamie Gritton error = 1020b97457e2SJamie Gritton vfs_copyopt(opts, "children.max", &childmax, sizeof(childmax)); 1021b97457e2SJamie Gritton if (error == ENOENT) 1022b97457e2SJamie Gritton gotchildmax = 0; 1023b97457e2SJamie Gritton else if (error != 0) 1024b97457e2SJamie Gritton goto done_free; 1025b97457e2SJamie Gritton else 1026b97457e2SJamie Gritton gotchildmax = 1; 1027b97457e2SJamie Gritton 10280304c731SJamie Gritton error = vfs_copyopt(opts, "enforce_statfs", &enforce, sizeof(enforce)); 1029f337198dSJamie Gritton if (error == ENOENT) 1030f337198dSJamie Gritton gotenforce = 0; 1031f337198dSJamie Gritton else if (error != 0) 10320304c731SJamie Gritton goto done_free; 1033f337198dSJamie Gritton else if (enforce < 0 || enforce > 2) { 1034f337198dSJamie Gritton error = EINVAL; 1035f337198dSJamie Gritton goto done_free; 1036f337198dSJamie Gritton } else 1037f337198dSJamie Gritton gotenforce = 1; 10380304c731SJamie Gritton 10390cc207a6SMartin Matuska error = vfs_copyopt(opts, "devfs_ruleset", &rsnum, sizeof(rsnum)); 10400cc207a6SMartin Matuska if (error == ENOENT) 10410cc207a6SMartin Matuska gotrsnum = 0; 10420cc207a6SMartin Matuska else if (error != 0) 10430cc207a6SMartin Matuska goto done_free; 10440cc207a6SMartin Matuska else 10450cc207a6SMartin Matuska gotrsnum = 1; 10460cc207a6SMartin Matuska 1047b38ff370SJamie Gritton pr_flags = ch_flags = 0; 1048672756aaSJamie Gritton for (bf = pr_flag_bool; 1049672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 1050672756aaSJamie Gritton bf++) { 1051672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_flags, bf->flag); 1052672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_flags, bf->flag); 10530304c731SJamie Gritton } 1054b38ff370SJamie Gritton ch_flags |= pr_flags; 1055672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 1056672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 1057672756aaSJamie Gritton jsf++) { 1058672756aaSJamie Gritton error = vfs_copyopt(opts, jsf->name, &jsys, sizeof(jsys)); 10597cbf7213SJamie Gritton if (error == ENOENT) 10607cbf7213SJamie Gritton continue; 10617cbf7213SJamie Gritton if (error != 0) 10627cbf7213SJamie Gritton goto done_free; 10637cbf7213SJamie Gritton switch (jsys) { 10647cbf7213SJamie Gritton case JAIL_SYS_DISABLE: 1065672756aaSJamie Gritton if (!jsf->disable) { 10667cbf7213SJamie Gritton error = EINVAL; 10677cbf7213SJamie Gritton goto done_free; 10687cbf7213SJamie Gritton } 1069672756aaSJamie Gritton pr_flags |= jsf->disable; 10707cbf7213SJamie Gritton break; 10717cbf7213SJamie Gritton case JAIL_SYS_NEW: 1072672756aaSJamie Gritton pr_flags |= jsf->new; 10737cbf7213SJamie Gritton break; 10747cbf7213SJamie Gritton case JAIL_SYS_INHERIT: 10757cbf7213SJamie Gritton break; 10767cbf7213SJamie Gritton default: 10777cbf7213SJamie Gritton error = EINVAL; 10787cbf7213SJamie Gritton goto done_free; 10797cbf7213SJamie Gritton } 1080672756aaSJamie Gritton ch_flags |= jsf->new | jsf->disable; 10817cbf7213SJamie Gritton } 10821158508aSJamie Gritton if ((flags & (JAIL_CREATE | JAIL_ATTACH)) == JAIL_CREATE 10834affa14cSJamie Gritton && !(pr_flags & PR_PERSIST)) { 10844affa14cSJamie Gritton error = EINVAL; 10854affa14cSJamie Gritton vfs_opterror(opts, "new jail must persist or attach"); 10864affa14cSJamie Gritton goto done_errmsg; 10874affa14cSJamie Gritton } 1088679e1390SJamie Gritton #ifdef VIMAGE 1089679e1390SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_VNET)) { 1090679e1390SJamie Gritton error = EINVAL; 1091679e1390SJamie Gritton vfs_opterror(opts, "vnet cannot be changed after creation"); 1092679e1390SJamie Gritton goto done_errmsg; 1093679e1390SJamie Gritton } 1094679e1390SJamie Gritton #endif 10952b0d6f81SJamie Gritton #ifdef INET 10962b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP4_USER)) { 10972b0d6f81SJamie Gritton error = EINVAL; 10982b0d6f81SJamie Gritton vfs_opterror(opts, "ip4 cannot be changed after creation"); 10992b0d6f81SJamie Gritton goto done_errmsg; 11002b0d6f81SJamie Gritton } 11012b0d6f81SJamie Gritton #endif 11022b0d6f81SJamie Gritton #ifdef INET6 11032b0d6f81SJamie Gritton if ((flags & JAIL_UPDATE) && (ch_flags & PR_IP6_USER)) { 11042b0d6f81SJamie Gritton error = EINVAL; 11052b0d6f81SJamie Gritton vfs_opterror(opts, "ip6 cannot be changed after creation"); 11062b0d6f81SJamie Gritton goto done_errmsg; 11072b0d6f81SJamie Gritton } 11082b0d6f81SJamie Gritton #endif 1109b38ff370SJamie Gritton 11100304c731SJamie Gritton pr_allow = ch_allow = 0; 1111672756aaSJamie Gritton for (bf = pr_flag_allow; 11125d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 11135d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 1114672756aaSJamie Gritton bf++) { 1115672756aaSJamie Gritton vfs_flagopt(opts, bf->name, &pr_allow, bf->flag); 1116672756aaSJamie Gritton vfs_flagopt(opts, bf->noname, &ch_allow, bf->flag); 11170304c731SJamie Gritton } 11180304c731SJamie Gritton ch_allow |= pr_allow; 11190304c731SJamie Gritton 1120b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 1121b38ff370SJamie Gritton if (error == ENOENT) 1122b38ff370SJamie Gritton name = NULL; 1123b38ff370SJamie Gritton else if (error != 0) 1124b38ff370SJamie Gritton goto done_free; 1125b38ff370SJamie Gritton else { 1126b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 1127b38ff370SJamie Gritton error = EINVAL; 1128b38ff370SJamie Gritton goto done_free; 1129b38ff370SJamie Gritton } 1130b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1131b38ff370SJamie Gritton error = ENAMETOOLONG; 1132b38ff370SJamie Gritton goto done_free; 1133b38ff370SJamie Gritton } 1134b38ff370SJamie Gritton } 1135b38ff370SJamie Gritton 1136b38ff370SJamie Gritton error = vfs_getopt(opts, "host.hostname", (void **)&host, &len); 1137b38ff370SJamie Gritton if (error == ENOENT) 1138b38ff370SJamie Gritton host = NULL; 1139b38ff370SJamie Gritton else if (error != 0) 1140b38ff370SJamie Gritton goto done_free; 1141b38ff370SJamie Gritton else { 114276ca6f88SJamie Gritton ch_flags |= PR_HOST; 114376ca6f88SJamie Gritton pr_flags |= PR_HOST; 1144b38ff370SJamie Gritton if (len == 0 || host[len - 1] != '\0') { 1145b38ff370SJamie Gritton error = EINVAL; 1146b38ff370SJamie Gritton goto done_free; 1147b38ff370SJamie Gritton } 1148b38ff370SJamie Gritton if (len > MAXHOSTNAMELEN) { 1149b38ff370SJamie Gritton error = ENAMETOOLONG; 1150b38ff370SJamie Gritton goto done_free; 1151b38ff370SJamie Gritton } 1152b38ff370SJamie Gritton } 1153b38ff370SJamie Gritton 115476ca6f88SJamie Gritton error = vfs_getopt(opts, "host.domainname", (void **)&domain, &len); 115576ca6f88SJamie Gritton if (error == ENOENT) 115676ca6f88SJamie Gritton domain = NULL; 115776ca6f88SJamie Gritton else if (error != 0) 115876ca6f88SJamie Gritton goto done_free; 115976ca6f88SJamie Gritton else { 116076ca6f88SJamie Gritton ch_flags |= PR_HOST; 116176ca6f88SJamie Gritton pr_flags |= PR_HOST; 116276ca6f88SJamie Gritton if (len == 0 || domain[len - 1] != '\0') { 116376ca6f88SJamie Gritton error = EINVAL; 116476ca6f88SJamie Gritton goto done_free; 116576ca6f88SJamie Gritton } 116676ca6f88SJamie Gritton if (len > MAXHOSTNAMELEN) { 116776ca6f88SJamie Gritton error = ENAMETOOLONG; 116876ca6f88SJamie Gritton goto done_free; 116976ca6f88SJamie Gritton } 117076ca6f88SJamie Gritton } 117176ca6f88SJamie Gritton 117276ca6f88SJamie Gritton error = vfs_getopt(opts, "host.hostuuid", (void **)&uuid, &len); 117376ca6f88SJamie Gritton if (error == ENOENT) 117476ca6f88SJamie Gritton uuid = NULL; 117576ca6f88SJamie Gritton else if (error != 0) 117676ca6f88SJamie Gritton goto done_free; 117776ca6f88SJamie Gritton else { 117876ca6f88SJamie Gritton ch_flags |= PR_HOST; 117976ca6f88SJamie Gritton pr_flags |= PR_HOST; 118076ca6f88SJamie Gritton if (len == 0 || uuid[len - 1] != '\0') { 118176ca6f88SJamie Gritton error = EINVAL; 118276ca6f88SJamie Gritton goto done_free; 118376ca6f88SJamie Gritton } 118476ca6f88SJamie Gritton if (len > HOSTUUIDLEN) { 118576ca6f88SJamie Gritton error = ENAMETOOLONG; 118676ca6f88SJamie Gritton goto done_free; 118776ca6f88SJamie Gritton } 118876ca6f88SJamie Gritton } 118976ca6f88SJamie Gritton 1190841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 1191a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 119276ca6f88SJamie Gritton uint32_t hid32; 119376ca6f88SJamie Gritton 119476ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid32, sizeof(hid32)); 119576ca6f88SJamie Gritton hid = hid32; 119676ca6f88SJamie Gritton } else 119776ca6f88SJamie Gritton #endif 119876ca6f88SJamie Gritton error = vfs_copyopt(opts, "host.hostid", &hid, sizeof(hid)); 119976ca6f88SJamie Gritton if (error == ENOENT) 120076ca6f88SJamie Gritton gothid = 0; 120176ca6f88SJamie Gritton else if (error != 0) 120276ca6f88SJamie Gritton goto done_free; 120376ca6f88SJamie Gritton else { 120476ca6f88SJamie Gritton gothid = 1; 120576ca6f88SJamie Gritton ch_flags |= PR_HOST; 120676ca6f88SJamie Gritton pr_flags |= PR_HOST; 120776ca6f88SJamie Gritton } 120876ca6f88SJamie Gritton 1209b38ff370SJamie Gritton #ifdef INET 1210b38ff370SJamie Gritton error = vfs_getopt(opts, "ip4.addr", &op, &ip4s); 1211b38ff370SJamie Gritton if (error == ENOENT) 12120e5e396eSJamie Gritton ip4s = 0; 1213b38ff370SJamie Gritton else if (error != 0) 1214b38ff370SJamie Gritton goto done_free; 1215*eb8dcdeaSGleb Smirnoff else if (ip4s & (sizeof(struct in_addr) - 1)) { 1216b38ff370SJamie Gritton error = EINVAL; 1217b38ff370SJamie Gritton goto done_free; 12180304c731SJamie Gritton } else { 12196a3f2779SJamie Gritton ch_flags |= PR_IP4_USER; 12206a3f2779SJamie Gritton pr_flags |= PR_IP4_USER; 12216a3f2779SJamie Gritton if (ip4s > 0) { 1222*eb8dcdeaSGleb Smirnoff ip4s /= sizeof(struct in_addr); 1223b38ff370SJamie Gritton if (ip4s > jail_max_af_ips) { 1224b38ff370SJamie Gritton error = EINVAL; 1225b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv4 addresses"); 1226b38ff370SJamie Gritton goto done_errmsg; 1227b38ff370SJamie Gritton } 1228*eb8dcdeaSGleb Smirnoff ip4 = prison_ip_copyin(PR_INET, op, ip4s); 1229*eb8dcdeaSGleb Smirnoff if (ip4 == NULL) { 1230b38ff370SJamie Gritton error = EINVAL; 1231b38ff370SJamie Gritton goto done_free; 1232b38ff370SJamie Gritton } 1233b38ff370SJamie Gritton } 12340304c731SJamie Gritton } 1235b38ff370SJamie Gritton #endif 1236b38ff370SJamie Gritton 1237b38ff370SJamie Gritton #ifdef INET6 1238b38ff370SJamie Gritton error = vfs_getopt(opts, "ip6.addr", &op, &ip6s); 1239b38ff370SJamie Gritton if (error == ENOENT) 12400e5e396eSJamie Gritton ip6s = 0; 1241b38ff370SJamie Gritton else if (error != 0) 1242b38ff370SJamie Gritton goto done_free; 1243*eb8dcdeaSGleb Smirnoff else if (ip6s & (sizeof(struct in6_addr) - 1)) { 1244b38ff370SJamie Gritton error = EINVAL; 1245b38ff370SJamie Gritton goto done_free; 12460304c731SJamie Gritton } else { 12476a3f2779SJamie Gritton ch_flags |= PR_IP6_USER; 12486a3f2779SJamie Gritton pr_flags |= PR_IP6_USER; 12496a3f2779SJamie Gritton if (ip6s > 0) { 1250*eb8dcdeaSGleb Smirnoff ip6s /= sizeof(struct in6_addr); 1251b38ff370SJamie Gritton if (ip6s > jail_max_af_ips) { 1252b38ff370SJamie Gritton error = EINVAL; 1253b38ff370SJamie Gritton vfs_opterror(opts, "too many IPv6 addresses"); 1254b38ff370SJamie Gritton goto done_errmsg; 1255b38ff370SJamie Gritton } 1256*eb8dcdeaSGleb Smirnoff ip6 = prison_ip_copyin(PR_INET6, op, ip6s); 1257*eb8dcdeaSGleb Smirnoff if (ip6 == NULL) { 1258b38ff370SJamie Gritton error = EINVAL; 1259b38ff370SJamie Gritton goto done_free; 1260b38ff370SJamie Gritton } 1261b38ff370SJamie Gritton } 12620304c731SJamie Gritton } 1263b38ff370SJamie Gritton #endif 1264b38ff370SJamie Gritton 1265bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1266bdfc8cc4SJamie Gritton if ((ch_flags & PR_VNET) && (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1267bdfc8cc4SJamie Gritton error = EINVAL; 1268bdfc8cc4SJamie Gritton vfs_opterror(opts, 1269bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 1270bdfc8cc4SJamie Gritton goto done_errmsg; 1271bdfc8cc4SJamie Gritton } 1272bdfc8cc4SJamie Gritton #endif 1273bdfc8cc4SJamie Gritton 1274cf0313c6SJamie Gritton error = vfs_getopt(opts, "osrelease", (void **)&osrelstr, &len); 1275cf0313c6SJamie Gritton if (error == ENOENT) 1276cf0313c6SJamie Gritton osrelstr = NULL; 1277cf0313c6SJamie Gritton else if (error != 0) 1278cf0313c6SJamie Gritton goto done_free; 1279cf0313c6SJamie Gritton else { 1280cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1281cf0313c6SJamie Gritton error = EINVAL; 1282cf0313c6SJamie Gritton vfs_opterror(opts, 1283cf0313c6SJamie Gritton "osrelease cannot be changed after creation"); 1284cf0313c6SJamie Gritton goto done_errmsg; 1285cf0313c6SJamie Gritton } 12861b786d01SBjoern A. Zeeb if (len == 0 || osrelstr[len - 1] != '\0') { 1287cf0313c6SJamie Gritton error = EINVAL; 12881b786d01SBjoern A. Zeeb goto done_free; 12891b786d01SBjoern A. Zeeb } 12901b786d01SBjoern A. Zeeb if (len >= OSRELEASELEN) { 12911b786d01SBjoern A. Zeeb error = ENAMETOOLONG; 1292cf0313c6SJamie Gritton vfs_opterror(opts, 1293cf0313c6SJamie Gritton "osrelease string must be 1-%d bytes long", 1294cf0313c6SJamie Gritton OSRELEASELEN - 1); 1295cf0313c6SJamie Gritton goto done_errmsg; 1296cf0313c6SJamie Gritton } 1297cf0313c6SJamie Gritton } 1298cf0313c6SJamie Gritton 1299cf0313c6SJamie Gritton error = vfs_copyopt(opts, "osreldate", &osreldt, sizeof(osreldt)); 1300cf0313c6SJamie Gritton if (error == ENOENT) 1301cf0313c6SJamie Gritton osreldt = 0; 1302cf0313c6SJamie Gritton else if (error != 0) 1303cf0313c6SJamie Gritton goto done_free; 1304cf0313c6SJamie Gritton else { 1305cf0313c6SJamie Gritton if (flags & JAIL_UPDATE) { 1306cf0313c6SJamie Gritton error = EINVAL; 1307cf0313c6SJamie Gritton vfs_opterror(opts, 1308cf0313c6SJamie Gritton "osreldate cannot be changed after creation"); 1309cf0313c6SJamie Gritton goto done_errmsg; 1310cf0313c6SJamie Gritton } 1311cf0313c6SJamie Gritton if (osreldt == 0) { 1312cf0313c6SJamie Gritton error = EINVAL; 1313cf0313c6SJamie Gritton vfs_opterror(opts, "osreldate cannot be 0"); 1314cf0313c6SJamie Gritton goto done_errmsg; 1315cf0313c6SJamie Gritton } 1316cf0313c6SJamie Gritton } 1317cf0313c6SJamie Gritton 1318b38ff370SJamie Gritton root = NULL; 1319b38ff370SJamie Gritton error = vfs_getopt(opts, "path", (void **)&path, &len); 1320b38ff370SJamie Gritton if (error == ENOENT) 1321b38ff370SJamie Gritton path = NULL; 1322b38ff370SJamie Gritton else if (error != 0) 1323b38ff370SJamie Gritton goto done_free; 1324b38ff370SJamie Gritton else { 1325b38ff370SJamie Gritton if (flags & JAIL_UPDATE) { 1326b38ff370SJamie Gritton error = EINVAL; 1327b38ff370SJamie Gritton vfs_opterror(opts, 1328b38ff370SJamie Gritton "path cannot be changed after creation"); 1329b38ff370SJamie Gritton goto done_errmsg; 1330b38ff370SJamie Gritton } 1331b38ff370SJamie Gritton if (len == 0 || path[len - 1] != '\0') { 1332b38ff370SJamie Gritton error = EINVAL; 1333b38ff370SJamie Gritton goto done_free; 1334b38ff370SJamie Gritton } 13357e1d3eefSMateusz Guzik NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, path); 1336b38ff370SJamie Gritton error = namei(&nd); 1337b38ff370SJamie Gritton if (error) 1338b38ff370SJamie Gritton goto done_free; 1339b38ff370SJamie Gritton root = nd.ni_vp; 1340b38ff370SJamie Gritton NDFREE(&nd, NDF_ONLY_PNBUF); 13416dfe0a3dSMartin Matuska g_path = malloc(MAXPATHLEN, M_TEMP, M_WAITOK); 13426dfe0a3dSMartin Matuska strlcpy(g_path, path, MAXPATHLEN); 13436dfe0a3dSMartin Matuska error = vn_path_to_global_path(td, root, g_path, MAXPATHLEN); 13443eb6b656SMateusz Guzik if (error == 0) { 13456dfe0a3dSMartin Matuska path = g_path; 13466dfe0a3dSMartin Matuska } else { 1347f6e633a9SMartin Matuska /* exit on other errors */ 1348b38ff370SJamie Gritton goto done_free; 1349b38ff370SJamie Gritton } 1350f6e633a9SMartin Matuska if (root->v_type != VDIR) { 1351f6e633a9SMartin Matuska error = ENOTDIR; 1352f6e633a9SMartin Matuska vput(root); 1353f6e633a9SMartin Matuska goto done_free; 1354f6e633a9SMartin Matuska } 1355b249ce48SMateusz Guzik VOP_UNLOCK(root); 1356b38ff370SJamie Gritton } 1357b38ff370SJamie Gritton 1358b38ff370SJamie Gritton /* 1359b6f47c23SJamie Gritton * Find the specified jail, or at least its parent. 1360b38ff370SJamie Gritton * This abuses the file error codes ENOENT and EEXIST. 1361b38ff370SJamie Gritton */ 1362b38ff370SJamie Gritton pr = NULL; 13637de883c8SJamie Gritton inspr = NULL; 1364babbbb9cSJamie Gritton if (cuflags == JAIL_CREATE && jid == 0 && name != NULL) { 1365babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1366babbbb9cSJamie Gritton jid = strtoul(namelc != NULL ? namelc + 1 : name, &p, 10); 1367babbbb9cSJamie Gritton if (*p != '\0') 1368babbbb9cSJamie Gritton jid = 0; 1369babbbb9cSJamie Gritton } 1370b6f47c23SJamie Gritton sx_xlock(&allprison_lock); 1371108a9384SJamie Gritton drflags = PD_LIST_XLOCKED; 13720a2a96f3SJamie Gritton ppr = mypr; 13730a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 13740a2a96f3SJamie Gritton /* This jail is dying. This process will surely follow. */ 13750a2a96f3SJamie Gritton error = EAGAIN; 13760a2a96f3SJamie Gritton goto done_deref; 13770a2a96f3SJamie Gritton } 1378b38ff370SJamie Gritton if (jid != 0) { 1379b38ff370SJamie Gritton if (jid < 0) { 1380b38ff370SJamie Gritton error = EINVAL; 1381b38ff370SJamie Gritton vfs_opterror(opts, "negative jid"); 13822a4b2251SJamie Gritton goto done_deref; 1383b38ff370SJamie Gritton } 13847de883c8SJamie Gritton /* 13857de883c8SJamie Gritton * See if a requested jid already exists. Keep track of 13867de883c8SJamie Gritton * where it can be inserted later. 13877de883c8SJamie Gritton */ 13887de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 1389f7496dcaSJamie Gritton if (inspr->pr_id < jid) 1390f7496dcaSJamie Gritton continue; 1391f7496dcaSJamie Gritton if (inspr->pr_id > jid) 1392f7496dcaSJamie Gritton break; 13937de883c8SJamie Gritton pr = inspr; 1394f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 13952a4b2251SJamie Gritton drflags |= PD_LOCKED; 13967de883c8SJamie Gritton inspr = NULL; 13977de883c8SJamie Gritton break; 13987de883c8SJamie Gritton } 1399b38ff370SJamie Gritton if (pr != NULL) { 1400b38ff370SJamie Gritton /* Create: jid must not exist. */ 1401b38ff370SJamie Gritton if (cuflags == JAIL_CREATE) { 14027de883c8SJamie Gritton /* 14037de883c8SJamie Gritton * Even creators that cannot see the jail will 14047de883c8SJamie Gritton * get EEXIST. 14057de883c8SJamie Gritton */ 1406b38ff370SJamie Gritton error = EEXIST; 1407b38ff370SJamie Gritton vfs_opterror(opts, "jail %d already exists", 1408b38ff370SJamie Gritton jid); 14092a4b2251SJamie Gritton goto done_deref; 1410b38ff370SJamie Gritton } 14110304c731SJamie Gritton if (!prison_ischild(mypr, pr)) { 14127de883c8SJamie Gritton /* 14137de883c8SJamie Gritton * Updaters get ENOENT if they cannot see the 14147de883c8SJamie Gritton * jail. This is true even for CREATE | UPDATE, 14157de883c8SJamie Gritton * which normally cannot give this error. 14167de883c8SJamie Gritton */ 14172a4b2251SJamie Gritton error = ENOENT; 14182a4b2251SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14192a4b2251SJamie Gritton goto done_deref; 1420f7496dcaSJamie Gritton } 14210a2a96f3SJamie Gritton ppr = pr->pr_parent; 14220a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 14230a2a96f3SJamie Gritton error = ENOENT; 14240a2a96f3SJamie Gritton vfs_opterror(opts, "jail %d is dying", 14250a2a96f3SJamie Gritton ppr->pr_id); 14260a2a96f3SJamie Gritton goto done_deref; 14270a2a96f3SJamie Gritton } 1428f7496dcaSJamie Gritton if (!prison_isalive(pr)) { 1429b38ff370SJamie Gritton if (!(flags & JAIL_DYING)) { 1430b38ff370SJamie Gritton error = ENOENT; 1431b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 1432b38ff370SJamie Gritton jid); 14332a4b2251SJamie Gritton goto done_deref; 1434f7496dcaSJamie Gritton } 1435f7496dcaSJamie Gritton if ((flags & JAIL_ATTACH) || 1436b38ff370SJamie Gritton (pr_flags & PR_PERSIST)) { 1437b38ff370SJamie Gritton /* 1438b38ff370SJamie Gritton * A dying jail might be resurrected 1439b38ff370SJamie Gritton * (via attach or persist), but first 1440b38ff370SJamie Gritton * it must determine if another jail 1441b38ff370SJamie Gritton * has claimed its name. Accomplish 1442b38ff370SJamie Gritton * this by implicitly re-setting the 1443b38ff370SJamie Gritton * name. 1444b38ff370SJamie Gritton */ 1445b38ff370SJamie Gritton if (name == NULL) 14460304c731SJamie Gritton name = prison_name(mypr, pr); 1447b38ff370SJamie Gritton } 1448b38ff370SJamie Gritton } 14492a4b2251SJamie Gritton } else { 1450b38ff370SJamie Gritton /* Update: jid must exist. */ 1451b38ff370SJamie Gritton if (cuflags == JAIL_UPDATE) { 1452b38ff370SJamie Gritton error = ENOENT; 1453b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 14542a4b2251SJamie Gritton goto done_deref; 1455b38ff370SJamie Gritton } 1456b38ff370SJamie Gritton } 1457b38ff370SJamie Gritton } 1458b38ff370SJamie Gritton /* 1459b38ff370SJamie Gritton * If the caller provided a name, look for a jail by that name. 1460b38ff370SJamie Gritton * This has different semantics for creates and updates keyed by jid 1461b38ff370SJamie Gritton * (where the name must not already exist in a different jail), 1462b38ff370SJamie Gritton * and updates keyed by the name itself (where the name must exist 1463b38ff370SJamie Gritton * because that is the jail being updated). 1464b38ff370SJamie Gritton */ 1465b6f47c23SJamie Gritton namelc = NULL; 1466b38ff370SJamie Gritton if (name != NULL) { 1467babbbb9cSJamie Gritton namelc = strrchr(name, '.'); 1468babbbb9cSJamie Gritton if (namelc == NULL) 1469babbbb9cSJamie Gritton namelc = name; 1470babbbb9cSJamie Gritton else { 14710304c731SJamie Gritton /* 14720304c731SJamie Gritton * This is a hierarchical name. Split it into the 14730304c731SJamie Gritton * parent and child names, and make sure the parent 14740304c731SJamie Gritton * exists or matches an already found jail. 14750304c731SJamie Gritton */ 14760304c731SJamie Gritton if (pr != NULL) { 1477babbbb9cSJamie Gritton if (strncmp(name, ppr->pr_name, namelc - name) 1478babbbb9cSJamie Gritton || ppr->pr_name[namelc - name] != '\0') { 14790304c731SJamie Gritton error = EINVAL; 14800304c731SJamie Gritton vfs_opterror(opts, 14810304c731SJamie Gritton "cannot change jail's parent"); 14822a4b2251SJamie Gritton goto done_deref; 14830304c731SJamie Gritton } 14840304c731SJamie Gritton } else { 1485b6f47c23SJamie Gritton *namelc = '\0'; 14860304c731SJamie Gritton ppr = prison_find_name(mypr, name); 14870304c731SJamie Gritton if (ppr == NULL) { 14880304c731SJamie Gritton error = ENOENT; 14890304c731SJamie Gritton vfs_opterror(opts, 14900304c731SJamie Gritton "jail \"%s\" not found", name); 14912a4b2251SJamie Gritton goto done_deref; 14920304c731SJamie Gritton } 1493c050ea80SJamie Gritton mtx_unlock(&ppr->pr_mtx); 14940a2a96f3SJamie Gritton if (!prison_isalive(ppr)) { 1495c050ea80SJamie Gritton error = ENOENT; 1496c050ea80SJamie Gritton vfs_opterror(opts, 1497c050ea80SJamie Gritton "jail \"%s\" is dying", name); 1498c050ea80SJamie Gritton goto done_deref; 1499c050ea80SJamie Gritton } 1500b6f47c23SJamie Gritton *namelc = '.'; 15010304c731SJamie Gritton } 1502b6f47c23SJamie Gritton namelc++; 15030304c731SJamie Gritton } 1504b6f47c23SJamie Gritton if (namelc[0] != '\0') { 1505b6f47c23SJamie Gritton pnamelen = 15060304c731SJamie Gritton (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 15070304c731SJamie Gritton deadpr = NULL; 15080304c731SJamie Gritton FOREACH_PRISON_CHILD(ppr, tpr) { 15096754ae25SJamie Gritton if (tpr != pr && 1510b6f47c23SJamie Gritton !strcmp(tpr->pr_name + pnamelen, namelc)) { 151176ad42abSJamie Gritton if (prison_isalive(tpr)) { 1512b38ff370SJamie Gritton if (pr == NULL && 1513b38ff370SJamie Gritton cuflags != JAIL_CREATE) { 1514b38ff370SJamie Gritton /* 1515b38ff370SJamie Gritton * Use this jail 1516b38ff370SJamie Gritton * for updates. 1517b38ff370SJamie Gritton */ 1518b38ff370SJamie Gritton pr = tpr; 1519f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 152083bc72a0SJamie Gritton drflags |= PD_LOCKED; 1521b38ff370SJamie Gritton break; 1522b38ff370SJamie Gritton } 1523b38ff370SJamie Gritton /* 1524b38ff370SJamie Gritton * Create, or update(jid): 1525b38ff370SJamie Gritton * name must not exist in an 15260304c731SJamie Gritton * active sibling jail. 1527b38ff370SJamie Gritton */ 1528b38ff370SJamie Gritton error = EEXIST; 1529b38ff370SJamie Gritton vfs_opterror(opts, 1530b38ff370SJamie Gritton "jail \"%s\" already exists", 1531b38ff370SJamie Gritton name); 15322a4b2251SJamie Gritton goto done_deref; 1533b38ff370SJamie Gritton } 153476ad42abSJamie Gritton if (pr == NULL && 1535f7496dcaSJamie Gritton cuflags != JAIL_CREATE) { 153676ad42abSJamie Gritton deadpr = tpr; 1537f7496dcaSJamie Gritton } 1538b38ff370SJamie Gritton } 1539b38ff370SJamie Gritton } 1540b38ff370SJamie Gritton /* If no active jail is found, use a dying one. */ 1541b38ff370SJamie Gritton if (deadpr != NULL && pr == NULL) { 1542b38ff370SJamie Gritton if (flags & JAIL_DYING) { 1543b38ff370SJamie Gritton pr = deadpr; 1544f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 15452a4b2251SJamie Gritton drflags |= PD_LOCKED; 1546b38ff370SJamie Gritton } else if (cuflags == JAIL_UPDATE) { 1547b38ff370SJamie Gritton error = ENOENT; 1548b38ff370SJamie Gritton vfs_opterror(opts, 1549b38ff370SJamie Gritton "jail \"%s\" is dying", name); 15502a4b2251SJamie Gritton goto done_deref; 1551b38ff370SJamie Gritton } 1552b38ff370SJamie Gritton } 1553b38ff370SJamie Gritton /* Update: name must exist if no jid. */ 1554b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1555b38ff370SJamie Gritton error = ENOENT; 1556b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", 1557b38ff370SJamie Gritton name); 15582a4b2251SJamie Gritton goto done_deref; 1559b38ff370SJamie Gritton } 1560b38ff370SJamie Gritton } 1561b38ff370SJamie Gritton } 1562b38ff370SJamie Gritton /* Update: must provide a jid or name. */ 1563b38ff370SJamie Gritton else if (cuflags == JAIL_UPDATE && pr == NULL) { 1564b38ff370SJamie Gritton error = ENOENT; 1565b38ff370SJamie Gritton vfs_opterror(opts, "update specified no jail"); 15662a4b2251SJamie Gritton goto done_deref; 1567b38ff370SJamie Gritton } 1568b38ff370SJamie Gritton 1569b38ff370SJamie Gritton /* If there's no prison to update, create a new one and link it in. */ 15702a4b2251SJamie Gritton created = pr == NULL; 15712a4b2251SJamie Gritton if (created) { 1572b97457e2SJamie Gritton for (tpr = mypr; tpr != NULL; tpr = tpr->pr_parent) 1573b97457e2SJamie Gritton if (tpr->pr_childcount >= tpr->pr_childmax) { 1574b97457e2SJamie Gritton error = EPERM; 1575b97457e2SJamie Gritton vfs_opterror(opts, "prison limit exceeded"); 15762a4b2251SJamie Gritton goto done_deref; 1577b97457e2SJamie Gritton } 15787de883c8SJamie Gritton if (jid == 0 && (jid = get_next_prid(&inspr)) == 0) { 1579b38ff370SJamie Gritton error = EAGAIN; 15807de883c8SJamie Gritton vfs_opterror(opts, "no available jail IDs"); 15812a4b2251SJamie Gritton goto done_deref; 1582b38ff370SJamie Gritton } 15832a4b2251SJamie Gritton 15842a4b2251SJamie Gritton pr = malloc(sizeof(*pr), M_PRISON, M_WAITOK | M_ZERO); 15851158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 15861158508aSJamie Gritton refcount_init(&pr->pr_ref, 1); 1587f7496dcaSJamie Gritton refcount_init(&pr->pr_uref, 0); 15881158508aSJamie Gritton drflags |= PD_DEREF; 15892a4b2251SJamie Gritton LIST_INIT(&pr->pr_children); 15902a4b2251SJamie Gritton mtx_init(&pr->pr_mtx, "jail mutex", NULL, MTX_DEF | MTX_DUPOK); 15912a4b2251SJamie Gritton TASK_INIT(&pr->pr_task, 0, prison_complete, pr); 15922a4b2251SJamie Gritton 15937de883c8SJamie Gritton pr->pr_id = jid; 15947de883c8SJamie Gritton if (inspr != NULL) 15957de883c8SJamie Gritton TAILQ_INSERT_BEFORE(inspr, pr, pr_list); 15967de883c8SJamie Gritton else 1597b38ff370SJamie Gritton TAILQ_INSERT_TAIL(&allprison, pr, pr_list); 15987de883c8SJamie Gritton 15997de883c8SJamie Gritton pr->pr_parent = ppr; 16000a2a96f3SJamie Gritton prison_hold(ppr); 16010a2a96f3SJamie Gritton prison_proc_hold(ppr); 16020304c731SJamie Gritton LIST_INSERT_HEAD(&ppr->pr_children, pr, pr_sibling); 16030304c731SJamie Gritton for (tpr = ppr; tpr != NULL; tpr = tpr->pr_parent) 1604b97457e2SJamie Gritton tpr->pr_childcount++; 1605b38ff370SJamie Gritton 16060304c731SJamie Gritton /* Set some default values, and inherit some from the parent. */ 1607b6f47c23SJamie Gritton if (namelc == NULL) 1608b6f47c23SJamie Gritton namelc = ""; 1609b38ff370SJamie Gritton if (path == NULL) { 1610b38ff370SJamie Gritton path = "/"; 16110304c731SJamie Gritton root = mypr->pr_root; 1612b38ff370SJamie Gritton vref(root); 1613b38ff370SJamie Gritton } 16148986e3a0SJamie Gritton strlcpy(pr->pr_hostuuid, DEFAULT_HOSTUUID, HOSTUUIDLEN); 16158986e3a0SJamie Gritton pr->pr_flags |= PR_HOST; 1616bdfc8cc4SJamie Gritton #if defined(INET) || defined(INET6) 1617bdfc8cc4SJamie Gritton #ifdef VIMAGE 1618bdfc8cc4SJamie Gritton if (!(pr_flags & PR_VNET)) 1619bdfc8cc4SJamie Gritton #endif 1620bdfc8cc4SJamie Gritton { 16210304c731SJamie Gritton #ifdef INET 16222b0d6f81SJamie Gritton if (!(ch_flags & PR_IP4_USER)) 16236a3f2779SJamie Gritton pr->pr_flags |= PR_IP4 | PR_IP4_USER; 16242b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP4_USER)) { 16252b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP4; 1626*eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET); 16272b0d6f81SJamie Gritton } 16280304c731SJamie Gritton #endif 16290304c731SJamie Gritton #ifdef INET6 16302b0d6f81SJamie Gritton if (!(ch_flags & PR_IP6_USER)) 16316a3f2779SJamie Gritton pr->pr_flags |= PR_IP6 | PR_IP6_USER; 16322b0d6f81SJamie Gritton else if (!(pr_flags & PR_IP6_USER)) { 16332b0d6f81SJamie Gritton pr->pr_flags |= ppr->pr_flags & PR_IP6; 1634*eb8dcdeaSGleb Smirnoff prison_ip_dup(ppr, pr, PR_INET6); 16352b0d6f81SJamie Gritton } 16360304c731SJamie Gritton #endif 1637bdfc8cc4SJamie Gritton } 1638bdfc8cc4SJamie Gritton #endif 1639592bcae8SBjoern A. Zeeb /* Source address selection is always on by default. */ 1640592bcae8SBjoern A. Zeeb pr->pr_flags |= _PR_IP_SADDRSEL; 1641592bcae8SBjoern A. Zeeb 16420304c731SJamie Gritton pr->pr_securelevel = ppr->pr_securelevel; 16430304c731SJamie Gritton pr->pr_allow = JAIL_DEFAULT_ALLOW & ppr->pr_allow; 1644af10bf05SBjoern A. Zeeb pr->pr_enforce_statfs = jail_default_enforce_statfs; 1645bf3db8aaSMartin Matuska pr->pr_devfs_rsnum = ppr->pr_devfs_rsnum; 1646b38ff370SJamie Gritton 1647b96bd95bSIan Lepore pr->pr_osreldate = osreldt ? osreldt : ppr->pr_osreldate; 1648b96bd95bSIan Lepore if (osrelstr == NULL) 16491b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, ppr->pr_osrelease, 16501b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1651b96bd95bSIan Lepore else 16521b786d01SBjoern A. Zeeb strlcpy(pr->pr_osrelease, osrelstr, 16531b786d01SBjoern A. Zeeb sizeof(pr->pr_osrelease)); 1654b96bd95bSIan Lepore 1655679e1390SJamie Gritton #ifdef VIMAGE 1656679e1390SJamie Gritton /* Allocate a new vnet if specified. */ 1657679e1390SJamie Gritton pr->pr_vnet = (pr_flags & PR_VNET) 1658679e1390SJamie Gritton ? vnet_alloc() : ppr->pr_vnet; 1659679e1390SJamie Gritton #endif 1660b38ff370SJamie Gritton /* 1661b38ff370SJamie Gritton * Allocate a dedicated cpuset for each jail. 16628771ff75SGordon Bergling * Unlike other initial settings, this may return an error. 1663b38ff370SJamie Gritton */ 16640304c731SJamie Gritton error = cpuset_create_root(ppr, &pr->pr_cpuset); 16652a4b2251SJamie Gritton if (error) 16662a4b2251SJamie Gritton goto done_deref; 1667b38ff370SJamie Gritton 1668b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 16692a4b2251SJamie Gritton drflags |= PD_LOCKED; 1670b38ff370SJamie Gritton } else { 16712b0d6f81SJamie Gritton /* 16722b0d6f81SJamie Gritton * Grab a reference for existing prisons, to ensure they 16732b0d6f81SJamie Gritton * continue to exist for the duration of the call. 16742b0d6f81SJamie Gritton */ 16756754ae25SJamie Gritton prison_hold(pr); 16762a4b2251SJamie Gritton drflags |= PD_DEREF; 1677bdfc8cc4SJamie Gritton #if defined(VIMAGE) && (defined(INET) || defined(INET6)) 1678bdfc8cc4SJamie Gritton if ((pr->pr_flags & PR_VNET) && 1679bdfc8cc4SJamie Gritton (ch_flags & (PR_IP4_USER | PR_IP6_USER))) { 1680bdfc8cc4SJamie Gritton error = EINVAL; 1681bdfc8cc4SJamie Gritton vfs_opterror(opts, 1682bdfc8cc4SJamie Gritton "vnet jails cannot have IP address restrictions"); 16832a4b2251SJamie Gritton goto done_deref; 1684bdfc8cc4SJamie Gritton } 1685bdfc8cc4SJamie Gritton #endif 16862b0d6f81SJamie Gritton #ifdef INET 16872b0d6f81SJamie Gritton if (PR_IP4_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 16882b0d6f81SJamie Gritton error = EINVAL; 16892b0d6f81SJamie Gritton vfs_opterror(opts, 16902b0d6f81SJamie Gritton "ip4 cannot be changed after creation"); 16912a4b2251SJamie Gritton goto done_deref; 16922b0d6f81SJamie Gritton } 16932b0d6f81SJamie Gritton #endif 16942b0d6f81SJamie Gritton #ifdef INET6 16952b0d6f81SJamie Gritton if (PR_IP6_USER & ch_flags & (pr_flags ^ pr->pr_flags)) { 16962b0d6f81SJamie Gritton error = EINVAL; 16972b0d6f81SJamie Gritton vfs_opterror(opts, 16982b0d6f81SJamie Gritton "ip6 cannot be changed after creation"); 16992a4b2251SJamie Gritton goto done_deref; 17002b0d6f81SJamie Gritton } 17012b0d6f81SJamie Gritton #endif 1702b38ff370SJamie Gritton } 1703b38ff370SJamie Gritton 1704b38ff370SJamie Gritton /* Do final error checking before setting anything. */ 17050304c731SJamie Gritton if (gotslevel) { 17060304c731SJamie Gritton if (slevel < ppr->pr_securelevel) { 17070304c731SJamie Gritton error = EPERM; 17082a4b2251SJamie Gritton goto done_deref; 17090304c731SJamie Gritton } 17100304c731SJamie Gritton } 1711b97457e2SJamie Gritton if (gotchildmax) { 1712b97457e2SJamie Gritton if (childmax >= ppr->pr_childmax) { 1713b97457e2SJamie Gritton error = EPERM; 17142a4b2251SJamie Gritton goto done_deref; 1715b97457e2SJamie Gritton } 1716b97457e2SJamie Gritton } 17170304c731SJamie Gritton if (gotenforce) { 17180304c731SJamie Gritton if (enforce < ppr->pr_enforce_statfs) { 17190304c731SJamie Gritton error = EPERM; 17202a4b2251SJamie Gritton goto done_deref; 17210304c731SJamie Gritton } 17220304c731SJamie Gritton } 17230cc207a6SMartin Matuska if (gotrsnum) { 17240cc207a6SMartin Matuska /* 17250cc207a6SMartin Matuska * devfs_rsnum is a uint16_t 17260cc207a6SMartin Matuska */ 1727bf3db8aaSMartin Matuska if (rsnum < 0 || rsnum > 65535) { 17280cc207a6SMartin Matuska error = EINVAL; 17292a4b2251SJamie Gritton goto done_deref; 17300cc207a6SMartin Matuska } 17310cc207a6SMartin Matuska /* 1732bf3db8aaSMartin Matuska * Nested jails always inherit parent's devfs ruleset 17330cc207a6SMartin Matuska */ 17340cc207a6SMartin Matuska if (jailed(td->td_ucred)) { 17350cc207a6SMartin Matuska if (rsnum > 0 && rsnum != ppr->pr_devfs_rsnum) { 17360cc207a6SMartin Matuska error = EPERM; 17372a4b2251SJamie Gritton goto done_deref; 1738bf3db8aaSMartin Matuska } else 17390cc207a6SMartin Matuska rsnum = ppr->pr_devfs_rsnum; 17400cc207a6SMartin Matuska } 17410cc207a6SMartin Matuska } 1742b38ff370SJamie Gritton #ifdef INET 17432b0d6f81SJamie Gritton if (ip4s > 0) { 1744*eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP4) && 1745*eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET], ip4, 1746*eb8dcdeaSGleb Smirnoff PR_INET)) { 17470304c731SJamie Gritton error = EPERM; 17482a4b2251SJamie Gritton goto done_deref; 17490304c731SJamie Gritton } 1750*eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip4, PR_INET)) { 17510304c731SJamie Gritton error = EADDRINUSE; 1752*eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv4 addresses clash"); 17532a4b2251SJamie Gritton goto done_deref; 1754b38ff370SJamie Gritton } 17550304c731SJamie Gritton } 1756b38ff370SJamie Gritton #endif 1757b38ff370SJamie Gritton #ifdef INET6 17582b0d6f81SJamie Gritton if (ip6s > 0) { 1759*eb8dcdeaSGleb Smirnoff if ((ppr->pr_flags & PR_IP6) && 1760*eb8dcdeaSGleb Smirnoff !prison_ip_parent_match(ppr->pr_addrs[PR_INET6], ip6, 1761*eb8dcdeaSGleb Smirnoff PR_INET6)) { 17620304c731SJamie Gritton error = EPERM; 17632a4b2251SJamie Gritton goto done_deref; 17640304c731SJamie Gritton } 1765*eb8dcdeaSGleb Smirnoff if (!prison_ip_conflict_check(ppr, pr, ip6, PR_INET6)) { 17660304c731SJamie Gritton error = EADDRINUSE; 1767*eb8dcdeaSGleb Smirnoff vfs_opterror(opts, "IPv6 addresses clash"); 17682a4b2251SJamie Gritton goto done_deref; 1769b38ff370SJamie Gritton } 17700304c731SJamie Gritton } 1771b38ff370SJamie Gritton #endif 17720304c731SJamie Gritton onamelen = namelen = 0; 1773b6f47c23SJamie Gritton if (namelc != NULL) { 17746ab6058eSJamie Gritton /* Give a default name of the jid. Also allow the name to be 17756ab6058eSJamie Gritton * explicitly the jid - but not any other number, and only in 17766ab6058eSJamie Gritton * normal form (no leading zero/etc). 17776ab6058eSJamie Gritton */ 1778b6f47c23SJamie Gritton if (namelc[0] == '\0') 1779b6f47c23SJamie Gritton snprintf(namelc = numbuf, sizeof(numbuf), "%d", jid); 17806ab6058eSJamie Gritton else if ((strtoul(namelc, &p, 10) != jid || 17816ab6058eSJamie Gritton namelc[0] < '1' || namelc[0] > '9') && *p == '\0') { 1782b38ff370SJamie Gritton error = EINVAL; 1783babbbb9cSJamie Gritton vfs_opterror(opts, 1784babbbb9cSJamie Gritton "name cannot be numeric (unless it is the jid)"); 17852a4b2251SJamie Gritton goto done_deref; 1786b38ff370SJamie Gritton } 1787b38ff370SJamie Gritton /* 17880304c731SJamie Gritton * Make sure the name isn't too long for the prison or its 17890304c731SJamie Gritton * children. 1790b38ff370SJamie Gritton */ 1791b6f47c23SJamie Gritton pnamelen = (ppr == &prison0) ? 0 : strlen(ppr->pr_name) + 1; 1792b6f47c23SJamie Gritton onamelen = strlen(pr->pr_name + pnamelen); 1793b6f47c23SJamie Gritton namelen = strlen(namelc); 1794b6f47c23SJamie Gritton if (pnamelen + namelen + 1 > sizeof(pr->pr_name)) { 17950304c731SJamie Gritton error = ENAMETOOLONG; 17962a4b2251SJamie Gritton goto done_deref; 17970304c731SJamie Gritton } 17980304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, tpr, descend) { 17990304c731SJamie Gritton if (strlen(tpr->pr_name) + (namelen - onamelen) >= 18000304c731SJamie Gritton sizeof(pr->pr_name)) { 18010304c731SJamie Gritton error = ENAMETOOLONG; 18022a4b2251SJamie Gritton goto done_deref; 18030304c731SJamie Gritton } 18040304c731SJamie Gritton } 18050304c731SJamie Gritton } 1806b3079544SJamie Gritton pr_allow_diff = pr_allow & ~ppr->pr_allow; 1807b3079544SJamie Gritton if (pr_allow_diff & ~PR_ALLOW_DIFFERENCES) { 18080304c731SJamie Gritton error = EPERM; 18092a4b2251SJamie Gritton goto done_deref; 1810b38ff370SJamie Gritton } 1811b38ff370SJamie Gritton 1812b6f47c23SJamie Gritton /* 1813b6f47c23SJamie Gritton * Let modules check their parameters. This requires unlocking and 1814b6f47c23SJamie Gritton * then re-locking the prison, but this is still a valid state as long 1815b6f47c23SJamie Gritton * as allprison_lock remains xlocked. 1816b6f47c23SJamie Gritton */ 1817b6f47c23SJamie Gritton mtx_unlock(&pr->pr_mtx); 18182a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1819b6f47c23SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CHECK, opts); 18202a4b2251SJamie Gritton if (error != 0) 18212a4b2251SJamie Gritton goto done_deref; 1822b6f47c23SJamie Gritton mtx_lock(&pr->pr_mtx); 18232a4b2251SJamie Gritton drflags |= PD_LOCKED; 1824b6f47c23SJamie Gritton 1825b6f47c23SJamie Gritton /* At this point, all valid parameters should have been noted. */ 1826b6f47c23SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 1827b6f47c23SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 1828b6f47c23SJamie Gritton error = EINVAL; 1829b6f47c23SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 18302a4b2251SJamie Gritton goto done_deref; 1831b6f47c23SJamie Gritton } 1832b6f47c23SJamie Gritton } 1833b6f47c23SJamie Gritton 1834b38ff370SJamie Gritton /* Set the parameters of the prison. */ 1835b38ff370SJamie Gritton #ifdef INET 18360304c731SJamie Gritton redo_ip4 = 0; 18370304c731SJamie Gritton if (pr_flags & PR_IP4_USER) { 18380304c731SJamie Gritton pr->pr_flags |= PR_IP4; 1839*eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET, ip4); 1840b38ff370SJamie Gritton ip4 = NULL; 18410304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1842bdfc8cc4SJamie Gritton #ifdef VIMAGE 1843bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1844bdfc8cc4SJamie Gritton descend = 0; 1845bdfc8cc4SJamie Gritton continue; 1846bdfc8cc4SJamie Gritton } 1847bdfc8cc4SJamie Gritton #endif 1848*eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET, NULL)) { 18490304c731SJamie Gritton redo_ip4 = 1; 18500304c731SJamie Gritton descend = 0; 18510304c731SJamie Gritton } 18520304c731SJamie Gritton } 18530304c731SJamie Gritton } 1854b38ff370SJamie Gritton #endif 1855b38ff370SJamie Gritton #ifdef INET6 18560304c731SJamie Gritton redo_ip6 = 0; 18570304c731SJamie Gritton if (pr_flags & PR_IP6_USER) { 18580304c731SJamie Gritton pr->pr_flags |= PR_IP6; 1859*eb8dcdeaSGleb Smirnoff prison_ip_set(pr, PR_INET6, ip6); 1860b38ff370SJamie Gritton ip6 = NULL; 18610304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 1862bdfc8cc4SJamie Gritton #ifdef VIMAGE 1863bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 1864bdfc8cc4SJamie Gritton descend = 0; 1865bdfc8cc4SJamie Gritton continue; 1866bdfc8cc4SJamie Gritton } 1867bdfc8cc4SJamie Gritton #endif 1868*eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET6, NULL)) { 18690304c731SJamie Gritton redo_ip6 = 1; 18700304c731SJamie Gritton descend = 0; 18710304c731SJamie Gritton } 18720304c731SJamie Gritton } 18730304c731SJamie Gritton } 1874b38ff370SJamie Gritton #endif 18750304c731SJamie Gritton if (gotslevel) { 1876b38ff370SJamie Gritton pr->pr_securelevel = slevel; 18770304c731SJamie Gritton /* Set all child jails to be at least this level. */ 18780304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 18790304c731SJamie Gritton if (tpr->pr_securelevel < slevel) 18800304c731SJamie Gritton tpr->pr_securelevel = slevel; 18810304c731SJamie Gritton } 1882b97457e2SJamie Gritton if (gotchildmax) { 1883b97457e2SJamie Gritton pr->pr_childmax = childmax; 1884b97457e2SJamie Gritton /* Set all child jails to under this limit. */ 1885b97457e2SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED_LEVEL(pr, tpr, descend, level) 1886b97457e2SJamie Gritton if (tpr->pr_childmax > childmax - level) 1887b97457e2SJamie Gritton tpr->pr_childmax = childmax > level 1888b97457e2SJamie Gritton ? childmax - level : 0; 1889b97457e2SJamie Gritton } 18900304c731SJamie Gritton if (gotenforce) { 18910304c731SJamie Gritton pr->pr_enforce_statfs = enforce; 18920304c731SJamie Gritton /* Pass this restriction on to the children. */ 18930304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 18940304c731SJamie Gritton if (tpr->pr_enforce_statfs < enforce) 18950304c731SJamie Gritton tpr->pr_enforce_statfs = enforce; 18960304c731SJamie Gritton } 18970cc207a6SMartin Matuska if (gotrsnum) { 18980cc207a6SMartin Matuska pr->pr_devfs_rsnum = rsnum; 18990cc207a6SMartin Matuska /* Pass this restriction on to the children. */ 19000cc207a6SMartin Matuska FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) 19010cc207a6SMartin Matuska tpr->pr_devfs_rsnum = rsnum; 19020cc207a6SMartin Matuska } 1903b6f47c23SJamie Gritton if (namelc != NULL) { 19040304c731SJamie Gritton if (ppr == &prison0) 1905b6f47c23SJamie Gritton strlcpy(pr->pr_name, namelc, sizeof(pr->pr_name)); 19060304c731SJamie Gritton else 19070304c731SJamie Gritton snprintf(pr->pr_name, sizeof(pr->pr_name), "%s.%s", 1908b6f47c23SJamie Gritton ppr->pr_name, namelc); 19090304c731SJamie Gritton /* Change this component of child names. */ 19100304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 19110304c731SJamie Gritton bcopy(tpr->pr_name + onamelen, tpr->pr_name + namelen, 19120304c731SJamie Gritton strlen(tpr->pr_name + onamelen) + 1); 19130304c731SJamie Gritton bcopy(pr->pr_name, tpr->pr_name, namelen); 19140304c731SJamie Gritton } 19150304c731SJamie Gritton } 1916b38ff370SJamie Gritton if (path != NULL) { 19170304c731SJamie Gritton /* Try to keep a real-rooted full pathname. */ 1918b38ff370SJamie Gritton strlcpy(pr->pr_path, path, sizeof(pr->pr_path)); 1919b38ff370SJamie Gritton pr->pr_root = root; 19202a4b2251SJamie Gritton root = NULL; 1921b38ff370SJamie Gritton } 192276ca6f88SJamie Gritton if (PR_HOST & ch_flags & ~pr_flags) { 192376ca6f88SJamie Gritton if (pr->pr_flags & PR_HOST) { 192476ca6f88SJamie Gritton /* 192576ca6f88SJamie Gritton * Copy the parent's host info. As with pr_ip4 above, 192676ca6f88SJamie Gritton * the lack of a lock on the parent is not a problem; 192776ca6f88SJamie Gritton * it is always set with allprison_lock at least 192876ca6f88SJamie Gritton * shared, and is held exclusively here. 192976ca6f88SJamie Gritton */ 1930c1f19219SJamie Gritton strlcpy(pr->pr_hostname, pr->pr_parent->pr_hostname, 1931c1f19219SJamie Gritton sizeof(pr->pr_hostname)); 1932c1f19219SJamie Gritton strlcpy(pr->pr_domainname, pr->pr_parent->pr_domainname, 1933c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 1934c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, pr->pr_parent->pr_hostuuid, 1935c1f19219SJamie Gritton sizeof(pr->pr_hostuuid)); 193676ca6f88SJamie Gritton pr->pr_hostid = pr->pr_parent->pr_hostid; 193776ca6f88SJamie Gritton } 193876ca6f88SJamie Gritton } else if (host != NULL || domain != NULL || uuid != NULL || gothid) { 193976ca6f88SJamie Gritton /* Set this prison, and any descendants without PR_HOST. */ 1940b38ff370SJamie Gritton if (host != NULL) 1941c1f19219SJamie Gritton strlcpy(pr->pr_hostname, host, sizeof(pr->pr_hostname)); 194276ca6f88SJamie Gritton if (domain != NULL) 1943c1f19219SJamie Gritton strlcpy(pr->pr_domainname, domain, 1944c1f19219SJamie Gritton sizeof(pr->pr_domainname)); 194576ca6f88SJamie Gritton if (uuid != NULL) 1946c1f19219SJamie Gritton strlcpy(pr->pr_hostuuid, uuid, sizeof(pr->pr_hostuuid)); 194776ca6f88SJamie Gritton if (gothid) 194876ca6f88SJamie Gritton pr->pr_hostid = hid; 194976ca6f88SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 195076ca6f88SJamie Gritton if (tpr->pr_flags & PR_HOST) 195176ca6f88SJamie Gritton descend = 0; 195276ca6f88SJamie Gritton else { 195376ca6f88SJamie Gritton if (host != NULL) 1954c1f19219SJamie Gritton strlcpy(tpr->pr_hostname, 1955c1f19219SJamie Gritton pr->pr_hostname, 1956c1f19219SJamie Gritton sizeof(tpr->pr_hostname)); 195776ca6f88SJamie Gritton if (domain != NULL) 1958c1f19219SJamie Gritton strlcpy(tpr->pr_domainname, 1959c1f19219SJamie Gritton pr->pr_domainname, 1960c1f19219SJamie Gritton sizeof(tpr->pr_domainname)); 196176ca6f88SJamie Gritton if (uuid != NULL) 1962c1f19219SJamie Gritton strlcpy(tpr->pr_hostuuid, 1963c1f19219SJamie Gritton pr->pr_hostuuid, 1964c1f19219SJamie Gritton sizeof(tpr->pr_hostuuid)); 196576ca6f88SJamie Gritton if (gothid) 196676ca6f88SJamie Gritton tpr->pr_hostid = hid; 196776ca6f88SJamie Gritton } 196876ca6f88SJamie Gritton } 196976ca6f88SJamie Gritton } 19700304c731SJamie Gritton pr->pr_allow = (pr->pr_allow & ~ch_allow) | pr_allow; 19710fe74ae6SJamie Gritton if ((tallow = ch_allow & ~pr_allow)) 19720fe74ae6SJamie Gritton prison_set_allow_locked(pr, tallow, 0); 1973b38ff370SJamie Gritton /* 1974b38ff370SJamie Gritton * Persistent prisons get an extra reference, and prisons losing their 19751158508aSJamie Gritton * persist flag lose that reference. 1976b38ff370SJamie Gritton */ 197776ad42abSJamie Gritton born = !prison_isalive(pr); 19781158508aSJamie Gritton if (ch_flags & PR_PERSIST & (pr_flags ^ pr->pr_flags)) { 1979b38ff370SJamie Gritton if (pr_flags & PR_PERSIST) { 19806754ae25SJamie Gritton prison_hold(pr); 19811158508aSJamie Gritton /* 19821158508aSJamie Gritton * This may make a dead prison alive again, but wait 19831158508aSJamie Gritton * to label it as such until after OSD calls have had 19841158508aSJamie Gritton * a chance to run (and perhaps to fail). 19851158508aSJamie Gritton */ 19866754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 1987b38ff370SJamie Gritton } else { 198839c8ef90SJamie Gritton drflags |= PD_DEUREF; 1989f7496dcaSJamie Gritton prison_free_not_last(pr); 1990b38ff370SJamie Gritton } 1991b38ff370SJamie Gritton } 1992b38ff370SJamie Gritton pr->pr_flags = (pr->pr_flags & ~ch_flags) | pr_flags; 1993b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 19942a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 1995c861373bSJamie Gritton /* 1996c861373bSJamie Gritton * Any errors past this point will need to de-persist newly created 1997c861373bSJamie Gritton * prisons, as well as call remove methods. 1998c861373bSJamie Gritton */ 1999c861373bSJamie Gritton if (born) 2000c861373bSJamie Gritton drflags |= PD_KILL; 2001b38ff370SJamie Gritton 2002a7ad07bfSEdward Tomasz Napierala #ifdef RACCT 20034b5c9cf6SEdward Tomasz Napierala if (racct_enable && created) 2004a7ad07bfSEdward Tomasz Napierala prison_racct_attach(pr); 2005a7ad07bfSEdward Tomasz Napierala #endif 2006a7ad07bfSEdward Tomasz Napierala 20070304c731SJamie Gritton /* Locks may have prevented a complete restriction of child IP 20080304c731SJamie Gritton * addresses. If so, allocate some more memory and try again. 20090304c731SJamie Gritton */ 20100304c731SJamie Gritton #ifdef INET 20110304c731SJamie Gritton while (redo_ip4) { 2012*eb8dcdeaSGleb Smirnoff ip4s = pr->pr_addrs[PR_INET]->ips; 2013*eb8dcdeaSGleb Smirnoff ip4 = prison_ip_alloc(PR_INET, ip4s, M_WAITOK); 20140304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20150304c731SJamie Gritton redo_ip4 = 0; 20160304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2017bdfc8cc4SJamie Gritton #ifdef VIMAGE 2018bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2019bdfc8cc4SJamie Gritton descend = 0; 2020bdfc8cc4SJamie Gritton continue; 2021bdfc8cc4SJamie Gritton } 2022bdfc8cc4SJamie Gritton #endif 2023*eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET, ip4)) { 20240304c731SJamie Gritton if (ip4 != NULL) 20250304c731SJamie Gritton ip4 = NULL; 20260304c731SJamie Gritton else 20270304c731SJamie Gritton redo_ip4 = 1; 20280304c731SJamie Gritton } 20290304c731SJamie Gritton } 20300304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20310304c731SJamie Gritton } 20320304c731SJamie Gritton #endif 20330304c731SJamie Gritton #ifdef INET6 20340304c731SJamie Gritton while (redo_ip6) { 2035*eb8dcdeaSGleb Smirnoff ip6s = pr->pr_addrs[PR_INET6]->ips; 2036*eb8dcdeaSGleb Smirnoff ip6 = prison_ip_alloc(PR_INET6, ip6s, M_WAITOK); 20370304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 20380304c731SJamie Gritton redo_ip6 = 0; 20390304c731SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, tpr, descend) { 2040bdfc8cc4SJamie Gritton #ifdef VIMAGE 2041bdfc8cc4SJamie Gritton if (tpr->pr_flags & PR_VNET) { 2042bdfc8cc4SJamie Gritton descend = 0; 2043bdfc8cc4SJamie Gritton continue; 2044bdfc8cc4SJamie Gritton } 2045bdfc8cc4SJamie Gritton #endif 2046*eb8dcdeaSGleb Smirnoff if (prison_ip_restrict(tpr, PR_INET6, ip6)) { 20470304c731SJamie Gritton if (ip6 != NULL) 20480304c731SJamie Gritton ip6 = NULL; 20490304c731SJamie Gritton else 20500304c731SJamie Gritton redo_ip6 = 1; 20510304c731SJamie Gritton } 20520304c731SJamie Gritton } 20530304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 20540304c731SJamie Gritton } 20550304c731SJamie Gritton #endif 20560304c731SJamie Gritton 2057b38ff370SJamie Gritton /* Let the modules do their work. */ 2058cc5fd8c7SJamie Gritton if (born) { 2059b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_CREATE, opts); 2060c861373bSJamie Gritton if (error) 20612a4b2251SJamie Gritton goto done_deref; 2062b38ff370SJamie Gritton } 2063b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_SET, opts); 2064c861373bSJamie Gritton if (error) 20652a4b2251SJamie Gritton goto done_deref; 2066b38ff370SJamie Gritton 20671158508aSJamie Gritton /* 20681158508aSJamie Gritton * A new prison is now ready to be seen; either it has gained a user 20691158508aSJamie Gritton * reference via persistence, or is about to gain one via attachment. 20701158508aSJamie Gritton */ 20711158508aSJamie Gritton if (born) { 20721158508aSJamie Gritton drflags = prison_lock_xlock(pr, drflags); 20731158508aSJamie Gritton pr->pr_state = PRISON_STATE_ALIVE; 20741158508aSJamie Gritton } 20751158508aSJamie Gritton 2076b38ff370SJamie Gritton /* Attach this process to the prison if requested. */ 2077b38ff370SJamie Gritton if (flags & JAIL_ATTACH) { 2078c861373bSJamie Gritton error = do_jail_attach(td, pr, 2079589e4c1dSJamie Gritton prison_lock_xlock(pr, drflags & PD_LOCK_FLAGS)); 2080f7496dcaSJamie Gritton drflags &= ~(PD_LOCKED | PD_LIST_XLOCKED); 2081b38ff370SJamie Gritton if (error) { 2082b38ff370SJamie Gritton vfs_opterror(opts, "attach failed"); 20832a4b2251SJamie Gritton goto done_deref; 2084b38ff370SJamie Gritton } 2085b38ff370SJamie Gritton } 2086b38ff370SJamie Gritton 20871fb24974SEdward Tomasz Napierala #ifdef RACCT 20884b5c9cf6SEdward Tomasz Napierala if (racct_enable && !created) { 2089701d6b50SJamie Gritton if (drflags & PD_LOCKED) { 2090701d6b50SJamie Gritton mtx_unlock(&pr->pr_mtx); 2091701d6b50SJamie Gritton drflags &= ~PD_LOCKED; 2092701d6b50SJamie Gritton } 2093f7496dcaSJamie Gritton if (drflags & PD_LIST_XLOCKED) { 2094f7496dcaSJamie Gritton sx_xunlock(&allprison_lock); 2095f7496dcaSJamie Gritton drflags &= ~PD_LIST_XLOCKED; 2096b4e87a63SJamie Gritton } 20971fb24974SEdward Tomasz Napierala prison_racct_modify(pr); 20981fb24974SEdward Tomasz Napierala } 20991fb24974SEdward Tomasz Napierala #endif 21001fb24974SEdward Tomasz Napierala 2101c861373bSJamie Gritton drflags &= ~PD_KILL; 21021fb24974SEdward Tomasz Napierala td->td_retval[0] = pr->pr_id; 21031fb24974SEdward Tomasz Napierala 21042a4b2251SJamie Gritton done_deref: 21052a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 21062a4b2251SJamie Gritton if (pr != NULL) 21072a4b2251SJamie Gritton prison_deref(pr, drflags); 21082a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2109b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 21102a4b2251SJamie Gritton else if (drflags & PD_LIST_XLOCKED) 2111b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 21125050aa86SKonstantin Belousov if (root != NULL) 2113b38ff370SJamie Gritton vrele(root); 2114b38ff370SJamie Gritton done_errmsg: 2115b38ff370SJamie Gritton if (error) { 21162a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2117176ff3a0SJamie Gritton if (vfs_getopt(opts, "errmsg", (void **)&errmsg, 2118176ff3a0SJamie Gritton &errmsg_len) == 0 && errmsg_len > 0) { 2119b38ff370SJamie Gritton errmsg_pos = 2 * vfs_getopt_pos(opts, "errmsg") + 1; 2120b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2121b38ff370SJamie Gritton bcopy(errmsg, 2122b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2123b38ff370SJamie Gritton errmsg_len); 2124b38ff370SJamie Gritton else 2125b38ff370SJamie Gritton copyout(errmsg, 2126b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2127b38ff370SJamie Gritton errmsg_len); 2128b38ff370SJamie Gritton } 2129b38ff370SJamie Gritton } 2130b38ff370SJamie Gritton done_free: 2131b38ff370SJamie Gritton #ifdef INET 2132*eb8dcdeaSGleb Smirnoff prison_ip_free(ip4); 2133b38ff370SJamie Gritton #endif 2134b38ff370SJamie Gritton #ifdef INET6 2135*eb8dcdeaSGleb Smirnoff prison_ip_free(ip6); 2136b38ff370SJamie Gritton #endif 21376dfe0a3dSMartin Matuska if (g_path != NULL) 21386dfe0a3dSMartin Matuska free(g_path, M_TEMP); 2139b38ff370SJamie Gritton vfs_freeopts(opts); 2140b38ff370SJamie Gritton return (error); 2141b38ff370SJamie Gritton } 2142b38ff370SJamie Gritton 2143b38ff370SJamie Gritton /* 21447de883c8SJamie Gritton * Find the next available prison ID. Return the ID on success, or zero 21457de883c8SJamie Gritton * on failure. Also set a pointer to the allprison list entry the prison 21467de883c8SJamie Gritton * should be inserted before. 21477de883c8SJamie Gritton */ 21487de883c8SJamie Gritton static int 21497de883c8SJamie Gritton get_next_prid(struct prison **insprp) 21507de883c8SJamie Gritton { 21517de883c8SJamie Gritton struct prison *inspr; 21527de883c8SJamie Gritton int jid, maxid; 21537de883c8SJamie Gritton 21547de883c8SJamie Gritton jid = lastprid % JAIL_MAX + 1; 21557de883c8SJamie Gritton if (TAILQ_EMPTY(&allprison) || 21567de883c8SJamie Gritton TAILQ_LAST(&allprison, prisonlist)->pr_id < jid) { 21577de883c8SJamie Gritton /* 21587de883c8SJamie Gritton * A common case is for all jails to be implicitly numbered, 21597de883c8SJamie Gritton * which means they'll go on the end of the list, at least 21607de883c8SJamie Gritton * for the first JAIL_MAX times. 21617de883c8SJamie Gritton */ 21627de883c8SJamie Gritton inspr = NULL; 21637de883c8SJamie Gritton } else { 21647de883c8SJamie Gritton /* 21657de883c8SJamie Gritton * Take two passes through the allprison list: first starting 21667de883c8SJamie Gritton * with the proposed jid, then ending with it. 21677de883c8SJamie Gritton */ 21687de883c8SJamie Gritton for (maxid = JAIL_MAX; maxid != 0; ) { 21697de883c8SJamie Gritton TAILQ_FOREACH(inspr, &allprison, pr_list) { 21707de883c8SJamie Gritton if (inspr->pr_id < jid) 21717de883c8SJamie Gritton continue; 2172f7496dcaSJamie Gritton if (inspr->pr_id > jid) { 2173f7496dcaSJamie Gritton /* Found an opening. */ 21747de883c8SJamie Gritton maxid = 0; 21757de883c8SJamie Gritton break; 21767de883c8SJamie Gritton } 21777de883c8SJamie Gritton if (++jid > maxid) { 21787de883c8SJamie Gritton if (lastprid == maxid || lastprid == 0) 21797de883c8SJamie Gritton { 21807de883c8SJamie Gritton /* 21817de883c8SJamie Gritton * The entire legal range 21827de883c8SJamie Gritton * has been traversed 21837de883c8SJamie Gritton */ 21847de883c8SJamie Gritton return 0; 21857de883c8SJamie Gritton } 21867de883c8SJamie Gritton /* Try again from the start. */ 21877de883c8SJamie Gritton jid = 1; 21887de883c8SJamie Gritton maxid = lastprid; 21897de883c8SJamie Gritton break; 21907de883c8SJamie Gritton } 21917de883c8SJamie Gritton } 21927de883c8SJamie Gritton if (inspr == NULL) { 21937de883c8SJamie Gritton /* Found room at the end of the list. */ 21947de883c8SJamie Gritton break; 21957de883c8SJamie Gritton } 21967de883c8SJamie Gritton } 21977de883c8SJamie Gritton } 21987de883c8SJamie Gritton *insprp = inspr; 21997de883c8SJamie Gritton lastprid = jid; 22007de883c8SJamie Gritton return (jid); 22017de883c8SJamie Gritton } 22027de883c8SJamie Gritton 22037de883c8SJamie Gritton /* 2204b38ff370SJamie Gritton * struct jail_get_args { 2205b38ff370SJamie Gritton * struct iovec *iovp; 2206b38ff370SJamie Gritton * unsigned int iovcnt; 2207b38ff370SJamie Gritton * int flags; 2208b38ff370SJamie Gritton * }; 2209b38ff370SJamie Gritton */ 2210b38ff370SJamie Gritton int 22118451d0ddSKip Macy sys_jail_get(struct thread *td, struct jail_get_args *uap) 2212b38ff370SJamie Gritton { 2213b38ff370SJamie Gritton struct uio *auio; 2214b38ff370SJamie Gritton int error; 2215b38ff370SJamie Gritton 2216b38ff370SJamie Gritton /* Check that we have an even number of iovecs. */ 2217b38ff370SJamie Gritton if (uap->iovcnt & 1) 2218b38ff370SJamie Gritton return (EINVAL); 2219b38ff370SJamie Gritton 2220b38ff370SJamie Gritton error = copyinuio(uap->iovp, uap->iovcnt, &auio); 2221b38ff370SJamie Gritton if (error) 2222b38ff370SJamie Gritton return (error); 2223b38ff370SJamie Gritton error = kern_jail_get(td, auio, uap->flags); 2224b38ff370SJamie Gritton if (error == 0) 2225b38ff370SJamie Gritton error = copyout(auio->uio_iov, uap->iovp, 2226b38ff370SJamie Gritton uap->iovcnt * sizeof (struct iovec)); 2227b38ff370SJamie Gritton free(auio, M_IOV); 2228b38ff370SJamie Gritton return (error); 2229b38ff370SJamie Gritton } 2230b38ff370SJamie Gritton 2231b38ff370SJamie Gritton int 2232b38ff370SJamie Gritton kern_jail_get(struct thread *td, struct uio *optuio, int flags) 2233b38ff370SJamie Gritton { 2234672756aaSJamie Gritton struct bool_flags *bf; 2235672756aaSJamie Gritton struct jailsys_flags *jsf; 22360304c731SJamie Gritton struct prison *pr, *mypr; 2237b38ff370SJamie Gritton struct vfsopt *opt; 2238b38ff370SJamie Gritton struct vfsoptlist *opts; 2239b38ff370SJamie Gritton char *errmsg, *name; 22402a4b2251SJamie Gritton int drflags, error, errmsg_len, errmsg_pos, i, jid, len, pos; 2241672756aaSJamie Gritton unsigned f; 2242b38ff370SJamie Gritton 2243b38ff370SJamie Gritton if (flags & ~JAIL_GET_MASK) 2244b38ff370SJamie Gritton return (EINVAL); 2245b38ff370SJamie Gritton 2246b38ff370SJamie Gritton /* Get the parameter list. */ 2247b38ff370SJamie Gritton error = vfs_buildopts(optuio, &opts); 2248b38ff370SJamie Gritton if (error) 2249b38ff370SJamie Gritton return (error); 2250b38ff370SJamie Gritton errmsg_pos = vfs_getopt_pos(opts, "errmsg"); 22510304c731SJamie Gritton mypr = td->td_ucred->cr_prison; 22522a4b2251SJamie Gritton pr = NULL; 22531e2a13e6SJamie Gritton 2254b38ff370SJamie Gritton /* 2255b38ff370SJamie Gritton * Find the prison specified by one of: lastjid, jid, name. 2256b38ff370SJamie Gritton */ 2257b38ff370SJamie Gritton sx_slock(&allprison_lock); 22582a4b2251SJamie Gritton drflags = PD_LIST_SLOCKED; 2259b38ff370SJamie Gritton error = vfs_copyopt(opts, "lastjid", &jid, sizeof(jid)); 2260b38ff370SJamie Gritton if (error == 0) { 2261b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2262f7496dcaSJamie Gritton if (pr->pr_id > jid && 2263f7496dcaSJamie Gritton ((flags & JAIL_DYING) || prison_isalive(pr)) && 2264f7496dcaSJamie Gritton prison_ischild(mypr, pr)) { 2265b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 22662a4b2251SJamie Gritton drflags |= PD_LOCKED; 2267b38ff370SJamie Gritton goto found_prison; 22682a4b2251SJamie Gritton } 2269f7496dcaSJamie Gritton } 2270b38ff370SJamie Gritton error = ENOENT; 2271b38ff370SJamie Gritton vfs_opterror(opts, "no jail after %d", jid); 22722a4b2251SJamie Gritton goto done; 2273b38ff370SJamie Gritton } else if (error != ENOENT) 22742a4b2251SJamie Gritton goto done; 2275b38ff370SJamie Gritton 2276b38ff370SJamie Gritton error = vfs_copyopt(opts, "jid", &jid, sizeof(jid)); 2277b38ff370SJamie Gritton if (error == 0) { 2278b38ff370SJamie Gritton if (jid != 0) { 22790304c731SJamie Gritton pr = prison_find_child(mypr, jid); 2280b38ff370SJamie Gritton if (pr != NULL) { 22812a4b2251SJamie Gritton drflags |= PD_LOCKED; 228276ad42abSJamie Gritton if (!(prison_isalive(pr) || 228376ad42abSJamie Gritton (flags & JAIL_DYING))) { 2284b38ff370SJamie Gritton error = ENOENT; 2285b38ff370SJamie Gritton vfs_opterror(opts, "jail %d is dying", 2286b38ff370SJamie Gritton jid); 22872a4b2251SJamie Gritton goto done; 2288b38ff370SJamie Gritton } 2289b38ff370SJamie Gritton goto found_prison; 2290b38ff370SJamie Gritton } 2291b38ff370SJamie Gritton error = ENOENT; 2292b38ff370SJamie Gritton vfs_opterror(opts, "jail %d not found", jid); 22932a4b2251SJamie Gritton goto done; 2294b38ff370SJamie Gritton } 2295b38ff370SJamie Gritton } else if (error != ENOENT) 22962a4b2251SJamie Gritton goto done; 2297b38ff370SJamie Gritton 2298b38ff370SJamie Gritton error = vfs_getopt(opts, "name", (void **)&name, &len); 2299b38ff370SJamie Gritton if (error == 0) { 2300b38ff370SJamie Gritton if (len == 0 || name[len - 1] != '\0') { 2301b38ff370SJamie Gritton error = EINVAL; 23022a4b2251SJamie Gritton goto done; 2303b38ff370SJamie Gritton } 23040304c731SJamie Gritton pr = prison_find_name(mypr, name); 2305b38ff370SJamie Gritton if (pr != NULL) { 23062a4b2251SJamie Gritton drflags |= PD_LOCKED; 230776ad42abSJamie Gritton if (!(prison_isalive(pr) || (flags & JAIL_DYING))) { 2308b38ff370SJamie Gritton error = ENOENT; 2309b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" is dying", 2310b38ff370SJamie Gritton name); 23112a4b2251SJamie Gritton goto done; 2312b38ff370SJamie Gritton } 2313b38ff370SJamie Gritton goto found_prison; 2314b38ff370SJamie Gritton } 2315b38ff370SJamie Gritton error = ENOENT; 2316b38ff370SJamie Gritton vfs_opterror(opts, "jail \"%s\" not found", name); 23172a4b2251SJamie Gritton goto done; 2318b38ff370SJamie Gritton } else if (error != ENOENT) 23192a4b2251SJamie Gritton goto done; 2320b38ff370SJamie Gritton 2321b38ff370SJamie Gritton vfs_opterror(opts, "no jail specified"); 2322b38ff370SJamie Gritton error = ENOENT; 23232a4b2251SJamie Gritton goto done; 2324b38ff370SJamie Gritton 2325b38ff370SJamie Gritton found_prison: 2326b38ff370SJamie Gritton /* Get the parameters of the prison. */ 23276754ae25SJamie Gritton prison_hold(pr); 23282a4b2251SJamie Gritton drflags |= PD_DEREF; 2329b38ff370SJamie Gritton td->td_retval[0] = pr->pr_id; 2330b38ff370SJamie Gritton error = vfs_setopt(opts, "jid", &pr->pr_id, sizeof(pr->pr_id)); 2331b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23322a4b2251SJamie Gritton goto done; 23330304c731SJamie Gritton i = (pr->pr_parent == mypr) ? 0 : pr->pr_parent->pr_id; 23340304c731SJamie Gritton error = vfs_setopt(opts, "parent", &i, sizeof(i)); 2335b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23362a4b2251SJamie Gritton goto done; 23370304c731SJamie Gritton error = vfs_setopts(opts, "name", prison_name(mypr, pr)); 23380304c731SJamie Gritton if (error != 0 && error != ENOENT) 23392a4b2251SJamie Gritton goto done; 23400304c731SJamie Gritton error = vfs_setopt(opts, "cpuset.id", &pr->pr_cpuset->cs_id, 2341b38ff370SJamie Gritton sizeof(pr->pr_cpuset->cs_id)); 2342b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23432a4b2251SJamie Gritton goto done; 23440304c731SJamie Gritton error = vfs_setopts(opts, "path", prison_path(mypr, pr)); 2345b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23462a4b2251SJamie Gritton goto done; 2347b38ff370SJamie Gritton #ifdef INET 2348*eb8dcdeaSGleb Smirnoff error = vfs_setopt_part(opts, "ip4.addr", pr->pr_addrs[PR_INET] + 1, 2349*eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] ? pr->pr_addrs[PR_INET]->ips * 2350*eb8dcdeaSGleb Smirnoff pr_families[PR_INET].size : 0 ); 2351b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23522a4b2251SJamie Gritton goto done; 2353b38ff370SJamie Gritton #endif 2354b38ff370SJamie Gritton #ifdef INET6 2355*eb8dcdeaSGleb Smirnoff error = vfs_setopt_part(opts, "ip6.addr", pr->pr_addrs[PR_INET6] + 1, 2356*eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] ? pr->pr_addrs[PR_INET6]->ips * 2357*eb8dcdeaSGleb Smirnoff pr_families[PR_INET6].size : 0 ); 2358b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23592a4b2251SJamie Gritton goto done; 2360b38ff370SJamie Gritton #endif 2361b38ff370SJamie Gritton error = vfs_setopt(opts, "securelevel", &pr->pr_securelevel, 2362b38ff370SJamie Gritton sizeof(pr->pr_securelevel)); 2363b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23642a4b2251SJamie Gritton goto done; 2365b97457e2SJamie Gritton error = vfs_setopt(opts, "children.cur", &pr->pr_childcount, 2366b97457e2SJamie Gritton sizeof(pr->pr_childcount)); 2367b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 23682a4b2251SJamie Gritton goto done; 2369b97457e2SJamie Gritton error = vfs_setopt(opts, "children.max", &pr->pr_childmax, 2370b97457e2SJamie Gritton sizeof(pr->pr_childmax)); 2371b97457e2SJamie Gritton if (error != 0 && error != ENOENT) 23722a4b2251SJamie Gritton goto done; 2373c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostname", pr->pr_hostname); 2374b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 23752a4b2251SJamie Gritton goto done; 2376c1f19219SJamie Gritton error = vfs_setopts(opts, "host.domainname", pr->pr_domainname); 237776ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 23782a4b2251SJamie Gritton goto done; 2379c1f19219SJamie Gritton error = vfs_setopts(opts, "host.hostuuid", pr->pr_hostuuid); 238076ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 23812a4b2251SJamie Gritton goto done; 2382841c0c7eSNathan Whitehorn #ifdef COMPAT_FREEBSD32 2383a5c1afadSDmitry Chagin if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) { 238476ca6f88SJamie Gritton uint32_t hid32 = pr->pr_hostid; 238576ca6f88SJamie Gritton 238676ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &hid32, sizeof(hid32)); 238776ca6f88SJamie Gritton } else 238876ca6f88SJamie Gritton #endif 238976ca6f88SJamie Gritton error = vfs_setopt(opts, "host.hostid", &pr->pr_hostid, 239076ca6f88SJamie Gritton sizeof(pr->pr_hostid)); 239176ca6f88SJamie Gritton if (error != 0 && error != ENOENT) 23922a4b2251SJamie Gritton goto done; 23930304c731SJamie Gritton error = vfs_setopt(opts, "enforce_statfs", &pr->pr_enforce_statfs, 23940304c731SJamie Gritton sizeof(pr->pr_enforce_statfs)); 23950304c731SJamie Gritton if (error != 0 && error != ENOENT) 23962a4b2251SJamie Gritton goto done; 23970cc207a6SMartin Matuska error = vfs_setopt(opts, "devfs_ruleset", &pr->pr_devfs_rsnum, 23980cc207a6SMartin Matuska sizeof(pr->pr_devfs_rsnum)); 23990cc207a6SMartin Matuska if (error != 0 && error != ENOENT) 24002a4b2251SJamie Gritton goto done; 2401672756aaSJamie Gritton for (bf = pr_flag_bool; 2402672756aaSJamie Gritton bf < pr_flag_bool + nitems(pr_flag_bool); 2403672756aaSJamie Gritton bf++) { 2404672756aaSJamie Gritton i = (pr->pr_flags & bf->flag) ? 1 : 0; 2405672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 2406b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24072a4b2251SJamie Gritton goto done; 2408b38ff370SJamie Gritton i = !i; 2409672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 2410b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24112a4b2251SJamie Gritton goto done; 24120304c731SJamie Gritton } 2413672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 2414672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 2415672756aaSJamie Gritton jsf++) { 2416672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 2417672756aaSJamie Gritton i = (f != 0 && f == jsf->disable) ? JAIL_SYS_DISABLE 2418672756aaSJamie Gritton : (f == jsf->new) ? JAIL_SYS_NEW 24197cbf7213SJamie Gritton : JAIL_SYS_INHERIT; 2420672756aaSJamie Gritton error = vfs_setopt(opts, jsf->name, &i, sizeof(i)); 24217cbf7213SJamie Gritton if (error != 0 && error != ENOENT) 24222a4b2251SJamie Gritton goto done; 24237cbf7213SJamie Gritton } 2424672756aaSJamie Gritton for (bf = pr_flag_allow; 24255d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 24265d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 2427672756aaSJamie Gritton bf++) { 2428672756aaSJamie Gritton i = (pr->pr_allow & bf->flag) ? 1 : 0; 2429672756aaSJamie Gritton error = vfs_setopt(opts, bf->name, &i, sizeof(i)); 24300304c731SJamie Gritton if (error != 0 && error != ENOENT) 24312a4b2251SJamie Gritton goto done; 24320304c731SJamie Gritton i = !i; 2433672756aaSJamie Gritton error = vfs_setopt(opts, bf->noname, &i, sizeof(i)); 24340304c731SJamie Gritton if (error != 0 && error != ENOENT) 24352a4b2251SJamie Gritton goto done; 24360304c731SJamie Gritton } 243776ad42abSJamie Gritton i = !prison_isalive(pr); 2438b38ff370SJamie Gritton error = vfs_setopt(opts, "dying", &i, sizeof(i)); 2439b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24402a4b2251SJamie Gritton goto done; 2441b38ff370SJamie Gritton i = !i; 2442b38ff370SJamie Gritton error = vfs_setopt(opts, "nodying", &i, sizeof(i)); 2443b38ff370SJamie Gritton if (error != 0 && error != ENOENT) 24442a4b2251SJamie Gritton goto done; 2445bd96bd15SIan Lepore error = vfs_setopt(opts, "osreldate", &pr->pr_osreldate, 2446bd96bd15SIan Lepore sizeof(pr->pr_osreldate)); 2447a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 24482a4b2251SJamie Gritton goto done; 2449a1a4c1b0SIan Lepore error = vfs_setopts(opts, "osrelease", pr->pr_osrelease); 2450a1a4c1b0SIan Lepore if (error != 0 && error != ENOENT) 24512a4b2251SJamie Gritton goto done; 2452b38ff370SJamie Gritton 2453b38ff370SJamie Gritton /* Get the module parameters. */ 2454b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 24552a4b2251SJamie Gritton drflags &= ~PD_LOCKED; 2456b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_GET, opts); 2457b38ff370SJamie Gritton if (error) 24582a4b2251SJamie Gritton goto done; 24592a4b2251SJamie Gritton prison_deref(pr, drflags); 24602a4b2251SJamie Gritton pr = NULL; 24612a4b2251SJamie Gritton drflags = 0; 2462b38ff370SJamie Gritton 2463b38ff370SJamie Gritton /* By now, all parameters should have been noted. */ 2464b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2465b38ff370SJamie Gritton if (!opt->seen && strcmp(opt->name, "errmsg")) { 2466b38ff370SJamie Gritton error = EINVAL; 2467b38ff370SJamie Gritton vfs_opterror(opts, "unknown parameter: %s", opt->name); 24682a4b2251SJamie Gritton goto done; 2469b38ff370SJamie Gritton } 2470b38ff370SJamie Gritton } 2471b38ff370SJamie Gritton 2472b38ff370SJamie Gritton /* Write the fetched parameters back to userspace. */ 2473b38ff370SJamie Gritton error = 0; 2474b38ff370SJamie Gritton TAILQ_FOREACH(opt, opts, link) { 2475b38ff370SJamie Gritton if (opt->pos >= 0 && opt->pos != errmsg_pos) { 2476b38ff370SJamie Gritton pos = 2 * opt->pos + 1; 2477b38ff370SJamie Gritton optuio->uio_iov[pos].iov_len = opt->len; 2478b38ff370SJamie Gritton if (opt->value != NULL) { 2479b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) { 2480b38ff370SJamie Gritton bcopy(opt->value, 2481b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2482b38ff370SJamie Gritton opt->len); 2483b38ff370SJamie Gritton } else { 2484b38ff370SJamie Gritton error = copyout(opt->value, 2485b38ff370SJamie Gritton optuio->uio_iov[pos].iov_base, 2486b38ff370SJamie Gritton opt->len); 2487b38ff370SJamie Gritton if (error) 2488b38ff370SJamie Gritton break; 2489b38ff370SJamie Gritton } 2490b38ff370SJamie Gritton } 2491b38ff370SJamie Gritton } 2492b38ff370SJamie Gritton } 2493b38ff370SJamie Gritton 24942a4b2251SJamie Gritton done: 24952a4b2251SJamie Gritton /* Release any temporary prison holds and/or locks. */ 24962a4b2251SJamie Gritton if (pr != NULL) 24972a4b2251SJamie Gritton prison_deref(pr, drflags); 24982a4b2251SJamie Gritton else if (drflags & PD_LIST_SLOCKED) 2499b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2500b38ff370SJamie Gritton if (error && errmsg_pos >= 0) { 25012a4b2251SJamie Gritton /* Write the error message back to userspace. */ 2502b38ff370SJamie Gritton vfs_getopt(opts, "errmsg", (void **)&errmsg, &errmsg_len); 2503b38ff370SJamie Gritton errmsg_pos = 2 * errmsg_pos + 1; 2504b38ff370SJamie Gritton if (errmsg_len > 0) { 2505b38ff370SJamie Gritton if (optuio->uio_segflg == UIO_SYSSPACE) 2506b38ff370SJamie Gritton bcopy(errmsg, 2507b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2508b38ff370SJamie Gritton errmsg_len); 2509b38ff370SJamie Gritton else 2510b38ff370SJamie Gritton copyout(errmsg, 2511b38ff370SJamie Gritton optuio->uio_iov[errmsg_pos].iov_base, 2512b38ff370SJamie Gritton errmsg_len); 2513b38ff370SJamie Gritton } 2514b38ff370SJamie Gritton } 2515b38ff370SJamie Gritton vfs_freeopts(opts); 2516b38ff370SJamie Gritton return (error); 2517b38ff370SJamie Gritton } 2518b38ff370SJamie Gritton 2519b38ff370SJamie Gritton /* 2520b38ff370SJamie Gritton * struct jail_remove_args { 2521b38ff370SJamie Gritton * int jid; 2522b38ff370SJamie Gritton * }; 2523b38ff370SJamie Gritton */ 2524b38ff370SJamie Gritton int 25258451d0ddSKip Macy sys_jail_remove(struct thread *td, struct jail_remove_args *uap) 2526b38ff370SJamie Gritton { 2527c861373bSJamie Gritton struct prison *pr; 2528c861373bSJamie Gritton int error; 2529b38ff370SJamie Gritton 2530b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_REMOVE); 2531b38ff370SJamie Gritton if (error) 2532b38ff370SJamie Gritton return (error); 2533b38ff370SJamie Gritton 2534b38ff370SJamie Gritton sx_xlock(&allprison_lock); 25350304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2536b38ff370SJamie Gritton if (pr == NULL) { 2537b38ff370SJamie Gritton sx_xunlock(&allprison_lock); 2538b38ff370SJamie Gritton return (EINVAL); 2539b38ff370SJamie Gritton } 2540c861373bSJamie Gritton if (!prison_isalive(pr)) { 2541c861373bSJamie Gritton /* Silently ignore already-dying prisons. */ 25420304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 2543c861373bSJamie Gritton sx_xunlock(&allprison_lock); 25440304c731SJamie Gritton return (0); 25450304c731SJamie Gritton } 2546c861373bSJamie Gritton prison_deref(pr, PD_KILL | PD_LOCKED | PD_LIST_XLOCKED); 2547c861373bSJamie Gritton return (0); 254875c13541SPoul-Henning Kamp } 254975c13541SPoul-Henning Kamp 2550fd7a8150SMike Barcroft /* 25519ddb7954SMike Barcroft * struct jail_attach_args { 25529ddb7954SMike Barcroft * int jid; 25539ddb7954SMike Barcroft * }; 2554fd7a8150SMike Barcroft */ 2555fd7a8150SMike Barcroft int 25568451d0ddSKip Macy sys_jail_attach(struct thread *td, struct jail_attach_args *uap) 2557fd7a8150SMike Barcroft { 2558b38ff370SJamie Gritton struct prison *pr; 2559b38ff370SJamie Gritton int error; 2560b38ff370SJamie Gritton 2561b38ff370SJamie Gritton error = priv_check(td, PRIV_JAIL_ATTACH); 2562b38ff370SJamie Gritton if (error) 2563b38ff370SJamie Gritton return (error); 2564b38ff370SJamie Gritton 2565f7496dcaSJamie Gritton sx_slock(&allprison_lock); 25660304c731SJamie Gritton pr = prison_find_child(td->td_ucred->cr_prison, uap->jid); 2567b38ff370SJamie Gritton if (pr == NULL) { 2568b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2569b38ff370SJamie Gritton return (EINVAL); 2570b38ff370SJamie Gritton } 2571b38ff370SJamie Gritton 257276ad42abSJamie Gritton /* Do not allow a process to attach to a prison that is not alive. */ 257376ad42abSJamie Gritton if (!prison_isalive(pr)) { 2574b38ff370SJamie Gritton mtx_unlock(&pr->pr_mtx); 2575b38ff370SJamie Gritton sx_sunlock(&allprison_lock); 2576b38ff370SJamie Gritton return (EINVAL); 2577b38ff370SJamie Gritton } 2578b38ff370SJamie Gritton 2579f7496dcaSJamie Gritton return (do_jail_attach(td, pr, PD_LOCKED | PD_LIST_SLOCKED)); 2580b38ff370SJamie Gritton } 2581b38ff370SJamie Gritton 2582b38ff370SJamie Gritton static int 2583f7496dcaSJamie Gritton do_jail_attach(struct thread *td, struct prison *pr, int drflags) 2584b38ff370SJamie Gritton { 2585fd7a8150SMike Barcroft struct proc *p; 2586fd7a8150SMike Barcroft struct ucred *newcred, *oldcred; 25875050aa86SKonstantin Belousov int error; 2588fd7a8150SMike Barcroft 2589f7496dcaSJamie Gritton mtx_assert(&pr->pr_mtx, MA_OWNED); 2590f7496dcaSJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 2591589e4c1dSJamie Gritton drflags &= PD_LOCK_FLAGS; 259257f22bd4SJacques Vidrine /* 259357f22bd4SJacques Vidrine * XXX: Note that there is a slight race here if two threads 259457f22bd4SJacques Vidrine * in the same privileged process attempt to attach to two 259557f22bd4SJacques Vidrine * different jails at the same time. It is important for 259657f22bd4SJacques Vidrine * user processes not to do this, or they might end up with 259757f22bd4SJacques Vidrine * a process root from one prison, but attached to the jail 259857f22bd4SJacques Vidrine * of another. 259957f22bd4SJacques Vidrine */ 26001158508aSJamie Gritton prison_hold(pr); 26016754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 2602f7496dcaSJamie Gritton drflags |= PD_DEREF | PD_DEUREF; 2603fd7a8150SMike Barcroft mtx_unlock(&pr->pr_mtx); 2604f7496dcaSJamie Gritton drflags &= ~PD_LOCKED; 2605b38ff370SJamie Gritton 2606b38ff370SJamie Gritton /* Let modules do whatever they need to prepare for attaching. */ 2607b38ff370SJamie Gritton error = osd_jail_call(pr, PR_METHOD_ATTACH, td); 2608b38ff370SJamie Gritton if (error) { 2609f7496dcaSJamie Gritton prison_deref(pr, drflags); 2610b38ff370SJamie Gritton return (error); 2611b38ff370SJamie Gritton } 2612f7496dcaSJamie Gritton sx_unlock(&allprison_lock); 2613f7496dcaSJamie Gritton drflags &= ~(PD_LIST_SLOCKED | PD_LIST_XLOCKED); 2614fd7a8150SMike Barcroft 2615413628a7SBjoern A. Zeeb /* 2616413628a7SBjoern A. Zeeb * Reparent the newly attached process to this jail. 2617413628a7SBjoern A. Zeeb */ 2618b38ff370SJamie Gritton p = td->td_proc; 2619413628a7SBjoern A. Zeeb error = cpuset_setproc_update_set(p, pr->pr_cpuset); 2620413628a7SBjoern A. Zeeb if (error) 2621b38ff370SJamie Gritton goto e_revert_osd; 2622413628a7SBjoern A. Zeeb 2623cb05b60aSAttilio Rao vn_lock(pr->pr_root, LK_EXCLUSIVE | LK_RETRY); 2624fd7a8150SMike Barcroft if ((error = change_dir(pr->pr_root, td)) != 0) 2625fd7a8150SMike Barcroft goto e_unlock; 2626fd7a8150SMike Barcroft #ifdef MAC 262730d239bcSRobert Watson if ((error = mac_vnode_check_chroot(td->td_ucred, pr->pr_root))) 2628fd7a8150SMike Barcroft goto e_unlock; 2629fd7a8150SMike Barcroft #endif 2630b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2631d4380c0cSJamie Gritton if ((error = pwd_chroot_chdir(td, pr->pr_root))) 26325050aa86SKonstantin Belousov goto e_revert_osd; 2633fd7a8150SMike Barcroft 2634fd7a8150SMike Barcroft newcred = crget(); 2635fd7a8150SMike Barcroft PROC_LOCK(p); 26361fb6767dSJamie Gritton oldcred = crcopysafe(p, newcred); 263769c4ee54SJohn Baldwin newcred->cr_prison = pr; 2638daf63fd2SMateusz Guzik proc_set_cred(p, newcred); 26391fb6767dSJamie Gritton setsugid(p); 2640097055e2SEdward Tomasz Napierala #ifdef RACCT 2641097055e2SEdward Tomasz Napierala racct_proc_ucred_changed(p, oldcred, newcred); 2642f87beb93SAndriy Gapon crhold(newcred); 2643f87beb93SAndriy Gapon #endif 2644f87beb93SAndriy Gapon PROC_UNLOCK(p); 2645f87beb93SAndriy Gapon #ifdef RCTL 2646f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, newcred); 2647f87beb93SAndriy Gapon crfree(newcred); 2648097055e2SEdward Tomasz Napierala #endif 2649f7496dcaSJamie Gritton prison_deref(oldcred->cr_prison, drflags); 2650fd7a8150SMike Barcroft crfree(oldcred); 2651cc7b7306SJamie Gritton 2652cc7b7306SJamie Gritton /* 2653cc7b7306SJamie Gritton * If the prison was killed while changing credentials, die along 2654cc7b7306SJamie Gritton * with it. 2655cc7b7306SJamie Gritton */ 2656cc7b7306SJamie Gritton if (!prison_isalive(pr)) { 2657cc7b7306SJamie Gritton PROC_LOCK(p); 2658cc7b7306SJamie Gritton kern_psignal(p, SIGKILL); 2659cc7b7306SJamie Gritton PROC_UNLOCK(p); 2660cc7b7306SJamie Gritton } 2661cc7b7306SJamie Gritton 2662fd7a8150SMike Barcroft return (0); 26631fb6767dSJamie Gritton 2664fd7a8150SMike Barcroft e_unlock: 2665b249ce48SMateusz Guzik VOP_UNLOCK(pr->pr_root); 2666b38ff370SJamie Gritton e_revert_osd: 2667b38ff370SJamie Gritton /* Tell modules this thread is still in its old jail after all. */ 26687f4e7248SJamie Gritton sx_slock(&allprison_lock); 2669f7496dcaSJamie Gritton drflags |= PD_LIST_SLOCKED; 26701fb6767dSJamie Gritton (void)osd_jail_call(td->td_ucred->cr_prison, PR_METHOD_ATTACH, td); 2671f7496dcaSJamie Gritton prison_deref(pr, drflags); 2672fd7a8150SMike Barcroft return (error); 2673fd7a8150SMike Barcroft } 2674fd7a8150SMike Barcroft 2675fd7a8150SMike Barcroft /* 2676fd7a8150SMike Barcroft * Returns a locked prison instance, or NULL on failure. 2677fd7a8150SMike Barcroft */ 267854b369c1SPawel Jakub Dawidek struct prison * 2679fd7a8150SMike Barcroft prison_find(int prid) 2680fd7a8150SMike Barcroft { 2681fd7a8150SMike Barcroft struct prison *pr; 2682fd7a8150SMike Barcroft 2683dc68a633SPawel Jakub Dawidek sx_assert(&allprison_lock, SX_LOCKED); 2684b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 2685f7496dcaSJamie Gritton if (pr->pr_id < prid) 2686f7496dcaSJamie Gritton continue; 26877de883c8SJamie Gritton if (pr->pr_id > prid) 26887de883c8SJamie Gritton break; 2689f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), ("Found invalid prison %p", pr)); 2690f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 2691f7496dcaSJamie Gritton return (pr); 2692fd7a8150SMike Barcroft } 2693fd7a8150SMike Barcroft return (NULL); 2694fd7a8150SMike Barcroft } 2695fd7a8150SMike Barcroft 2696b38ff370SJamie Gritton /* 26970304c731SJamie Gritton * Find a prison that is a descendant of mypr. Returns a locked prison or NULL. 2698b38ff370SJamie Gritton */ 2699b38ff370SJamie Gritton struct prison * 27000304c731SJamie Gritton prison_find_child(struct prison *mypr, int prid) 2701b38ff370SJamie Gritton { 27020304c731SJamie Gritton struct prison *pr; 27030304c731SJamie Gritton int descend; 2704b38ff370SJamie Gritton 2705b38ff370SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27060304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27070304c731SJamie Gritton if (pr->pr_id == prid) { 2708f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2709f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 27100304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 27110304c731SJamie Gritton return (pr); 27120304c731SJamie Gritton } 27130304c731SJamie Gritton } 27140304c731SJamie Gritton return (NULL); 27150304c731SJamie Gritton } 27160304c731SJamie Gritton 27170304c731SJamie Gritton /* 27180304c731SJamie Gritton * Look for the name relative to mypr. Returns a locked prison or NULL. 27190304c731SJamie Gritton */ 27200304c731SJamie Gritton struct prison * 27210304c731SJamie Gritton prison_find_name(struct prison *mypr, const char *name) 27220304c731SJamie Gritton { 27230304c731SJamie Gritton struct prison *pr, *deadpr; 27240304c731SJamie Gritton size_t mylen; 27250304c731SJamie Gritton int descend; 27260304c731SJamie Gritton 27270304c731SJamie Gritton sx_assert(&allprison_lock, SX_LOCKED); 27280304c731SJamie Gritton mylen = (mypr == &prison0) ? 0 : strlen(mypr->pr_name) + 1; 2729b38ff370SJamie Gritton deadpr = NULL; 27300304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(mypr, pr, descend) { 27310304c731SJamie Gritton if (!strcmp(pr->pr_name + mylen, name)) { 2732f7496dcaSJamie Gritton KASSERT(prison_isvalid(pr), 2733f7496dcaSJamie Gritton ("Found invalid prison %p", pr)); 2734f7496dcaSJamie Gritton if (prison_isalive(pr)) { 2735b38ff370SJamie Gritton mtx_lock(&pr->pr_mtx); 2736b38ff370SJamie Gritton return (pr); 2737f7496dcaSJamie Gritton } 2738b38ff370SJamie Gritton deadpr = pr; 2739b38ff370SJamie Gritton } 2740b38ff370SJamie Gritton } 27410304c731SJamie Gritton /* There was no valid prison - perhaps there was a dying one. */ 2742f7496dcaSJamie Gritton if (deadpr != NULL) 2743b38ff370SJamie Gritton mtx_lock(&deadpr->pr_mtx); 2744b38ff370SJamie Gritton return (deadpr); 2745b38ff370SJamie Gritton } 2746b38ff370SJamie Gritton 2747b38ff370SJamie Gritton /* 27480fe74ae6SJamie Gritton * See if a prison has the specific flag set. The prison should be locked, 27490fe74ae6SJamie Gritton * unless checking for flags that are only set at jail creation (such as 27500fe74ae6SJamie Gritton * PR_IP4 and PR_IP6), or only the single bit is examined, without regard 27510fe74ae6SJamie Gritton * to any other prison data. 27520304c731SJamie Gritton */ 27530304c731SJamie Gritton int 27540304c731SJamie Gritton prison_flag(struct ucred *cred, unsigned flag) 27550304c731SJamie Gritton { 27560304c731SJamie Gritton 27570304c731SJamie Gritton return (cred->cr_prison->pr_flags & flag); 27580304c731SJamie Gritton } 27590304c731SJamie Gritton 27600304c731SJamie Gritton int 27610304c731SJamie Gritton prison_allow(struct ucred *cred, unsigned flag) 27620304c731SJamie Gritton { 27630304c731SJamie Gritton 27640fe74ae6SJamie Gritton return ((cred->cr_prison->pr_allow & flag) != 0); 27650304c731SJamie Gritton } 27660304c731SJamie Gritton 27670304c731SJamie Gritton /* 2768effad35eSJamie Gritton * Hold a prison reference, by incrementing pr_ref. It is generally 2769effad35eSJamie Gritton * an error to hold a prison that does not already have a reference. 2770effad35eSJamie Gritton * A prison record will remain valid as long as it has at least one 2771effad35eSJamie Gritton * reference, and will not be removed as long as either the prison 2772effad35eSJamie Gritton * mutex or the allprison lock is held (allprison_lock may be shared). 2773effad35eSJamie Gritton */ 2774effad35eSJamie Gritton void 2775effad35eSJamie Gritton prison_hold_locked(struct prison *pr) 2776effad35eSJamie Gritton { 2777effad35eSJamie Gritton 27786754ae25SJamie Gritton /* Locking is no longer required. */ 27796754ae25SJamie Gritton prison_hold(pr); 2780effad35eSJamie Gritton } 2781effad35eSJamie Gritton 2782effad35eSJamie Gritton void 2783effad35eSJamie Gritton prison_hold(struct prison *pr) 2784effad35eSJamie Gritton { 27856754ae25SJamie Gritton #ifdef INVARIANTS 27866754ae25SJamie Gritton int was_valid = refcount_acquire_if_not_zero(&pr->pr_ref); 2787effad35eSJamie Gritton 27886754ae25SJamie Gritton KASSERT(was_valid, 27896754ae25SJamie Gritton ("Trying to hold dead prison %p (jid=%d).", pr, pr->pr_id)); 27906754ae25SJamie Gritton #else 27916754ae25SJamie Gritton refcount_acquire(&pr->pr_ref); 27926754ae25SJamie Gritton #endif 2793effad35eSJamie Gritton } 2794effad35eSJamie Gritton 2795effad35eSJamie Gritton /* 2796effad35eSJamie Gritton * Remove a prison reference. If that was the last reference, the 2797f7496dcaSJamie Gritton * prison will be removed (at a later time). 2798b38ff370SJamie Gritton */ 279991421ba2SRobert Watson void 28001ba4a712SPawel Jakub Dawidek prison_free_locked(struct prison *pr) 280191421ba2SRobert Watson { 280291421ba2SRobert Watson 28031ba4a712SPawel Jakub Dawidek mtx_assert(&pr->pr_mtx, MA_OWNED); 2804effad35eSJamie Gritton /* 2805f7496dcaSJamie Gritton * Locking is no longer required, but unlock because the caller 2806f7496dcaSJamie Gritton * expects it. 2807effad35eSJamie Gritton */ 2808f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2809f7496dcaSJamie Gritton prison_free(pr); 2810effad35eSJamie Gritton } 2811c2cda609SPawel Jakub Dawidek 28121ba4a712SPawel Jakub Dawidek void 28131ba4a712SPawel Jakub Dawidek prison_free(struct prison *pr) 28141ba4a712SPawel Jakub Dawidek { 28151ba4a712SPawel Jakub Dawidek 28166754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 28176754ae25SJamie Gritton ("Trying to free dead prison %p (jid=%d).", 28186754ae25SJamie Gritton pr, pr->pr_id)); 2819f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 2820f7496dcaSJamie Gritton /* 2821f7496dcaSJamie Gritton * Don't remove the last reference in this context, 2822f7496dcaSJamie Gritton * in case there are locks held. 2823f7496dcaSJamie Gritton */ 2824f7496dcaSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2825f7496dcaSJamie Gritton } 2826f7496dcaSJamie Gritton } 2827f7496dcaSJamie Gritton 2828f7496dcaSJamie Gritton static void 2829f7496dcaSJamie Gritton prison_free_not_last(struct prison *pr) 2830f7496dcaSJamie Gritton { 2831f7496dcaSJamie Gritton #ifdef INVARIANTS 2832f7496dcaSJamie Gritton int lastref; 2833f7496dcaSJamie Gritton 2834f7496dcaSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 2835f7496dcaSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2836f7496dcaSJamie Gritton pr, pr->pr_id)); 2837f7496dcaSJamie Gritton lastref = refcount_release(&pr->pr_ref); 2838f7496dcaSJamie Gritton KASSERT(!lastref, 2839f7496dcaSJamie Gritton ("prison_free_not_last freed last ref on prison %p (jid=%d).", 2840f7496dcaSJamie Gritton pr, pr->pr_id)); 2841f7496dcaSJamie Gritton #else 2842ee9b37aeSMateusz Guzik refcount_release(&pr->pr_ref); 2843f7496dcaSJamie Gritton #endif 28441ba4a712SPawel Jakub Dawidek } 28451ba4a712SPawel Jakub Dawidek 284673d9e52dSJamie Gritton /* 2847effad35eSJamie Gritton * Hold a a prison for user visibility, by incrementing pr_uref. 2848effad35eSJamie Gritton * It is generally an error to hold a prison that isn't already 2849effad35eSJamie Gritton * user-visible, except through the the jail system calls. It is also 2850effad35eSJamie Gritton * an error to hold an invalid prison. A prison record will remain 2851effad35eSJamie Gritton * alive as long as it has at least one user reference, and will not 2852f7496dcaSJamie Gritton * be set to the dying state until the prison mutex and allprison_lock 2853f7496dcaSJamie Gritton * are both freed. 2854effad35eSJamie Gritton */ 2855effad35eSJamie Gritton void 2856effad35eSJamie Gritton prison_proc_hold(struct prison *pr) 2857effad35eSJamie Gritton { 28586754ae25SJamie Gritton #ifdef INVARIANTS 28596754ae25SJamie Gritton int was_alive = refcount_acquire_if_not_zero(&pr->pr_uref); 2860effad35eSJamie Gritton 28616754ae25SJamie Gritton KASSERT(was_alive, 2862effad35eSJamie Gritton ("Cannot add a process to a non-alive prison (jid=%d)", pr->pr_id)); 28636754ae25SJamie Gritton #else 28646754ae25SJamie Gritton refcount_acquire(&pr->pr_uref); 28656754ae25SJamie Gritton #endif 2866effad35eSJamie Gritton } 2867effad35eSJamie Gritton 2868effad35eSJamie Gritton /* 2869effad35eSJamie Gritton * Remove a prison user reference. If it was the last reference, the 2870effad35eSJamie Gritton * prison will be considered "dying", and may be removed once all of 2871effad35eSJamie Gritton * its references are dropped. 2872effad35eSJamie Gritton */ 2873effad35eSJamie Gritton void 2874effad35eSJamie Gritton prison_proc_free(struct prison *pr) 2875effad35eSJamie Gritton { 2876effad35eSJamie Gritton 28776754ae25SJamie Gritton /* 28786754ae25SJamie Gritton * Locking is only required when releasing the last reference. 28796754ae25SJamie Gritton * This allows assurance that a locked prison will remain alive 28806754ae25SJamie Gritton * until it is unlocked. 28816754ae25SJamie Gritton */ 28826754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2883effad35eSJamie Gritton ("Trying to kill a process in a dead prison (jid=%d)", pr->pr_id)); 2884195cd6aeSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2885effad35eSJamie Gritton /* 2886effad35eSJamie Gritton * Don't remove the last user reference in this context, 2887effad35eSJamie Gritton * which is expected to be a process that is not only locked, 2888effad35eSJamie Gritton * but also half dead. Add a reference so any calls to 2889effad35eSJamie Gritton * prison_free() won't re-submit the task. 2890effad35eSJamie Gritton */ 2891f7496dcaSJamie Gritton prison_hold(pr); 28921158508aSJamie Gritton mtx_lock(&pr->pr_mtx); 28931158508aSJamie Gritton KASSERT(!(pr->pr_flags & PR_COMPLETE_PROC), 28941158508aSJamie Gritton ("Redundant last reference in prison_proc_free (jid=%d)", 28951158508aSJamie Gritton pr->pr_id)); 28961158508aSJamie Gritton pr->pr_flags |= PR_COMPLETE_PROC; 28971158508aSJamie Gritton mtx_unlock(&pr->pr_mtx); 2898effad35eSJamie Gritton taskqueue_enqueue(taskqueue_thread, &pr->pr_task); 2899effad35eSJamie Gritton } 2900effad35eSJamie Gritton } 2901effad35eSJamie Gritton 2902c861373bSJamie Gritton static void 2903c861373bSJamie Gritton prison_proc_free_not_last(struct prison *pr) 2904c861373bSJamie Gritton { 2905c861373bSJamie Gritton #ifdef INVARIANTS 2906c861373bSJamie Gritton int lastref; 2907c861373bSJamie Gritton 2908c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 2909c861373bSJamie Gritton ("Trying to free dead prison %p (jid=%d).", 2910c861373bSJamie Gritton pr, pr->pr_id)); 2911c861373bSJamie Gritton lastref = refcount_release(&pr->pr_uref); 2912c861373bSJamie Gritton KASSERT(!lastref, 2913c861373bSJamie Gritton ("prison_proc_free_not_last freed last uref on prison %p (jid=%d).", 2914c861373bSJamie Gritton pr, pr->pr_id)); 2915c861373bSJamie Gritton #else 2916c861373bSJamie Gritton refcount_release(&pr->pr_uref); 2917c861373bSJamie Gritton #endif 2918c861373bSJamie Gritton } 2919c861373bSJamie Gritton 2920effad35eSJamie Gritton /* 292173d9e52dSJamie Gritton * Complete a call to either prison_free or prison_proc_free. 292273d9e52dSJamie Gritton */ 2923c2cda609SPawel Jakub Dawidek static void 2924c2cda609SPawel Jakub Dawidek prison_complete(void *context, int pending) 2925c2cda609SPawel Jakub Dawidek { 292673d9e52dSJamie Gritton struct prison *pr = context; 2927f7496dcaSJamie Gritton int drflags; 2928b38ff370SJamie Gritton 2929effad35eSJamie Gritton /* 29301158508aSJamie Gritton * This could be called to release the last reference, or the last 29311158508aSJamie Gritton * user reference (plus the reference held in prison_proc_free). 2932effad35eSJamie Gritton */ 2933f7496dcaSJamie Gritton drflags = prison_lock_xlock(pr, PD_DEREF); 29341158508aSJamie Gritton if (pr->pr_flags & PR_COMPLETE_PROC) { 29351158508aSJamie Gritton pr->pr_flags &= ~PR_COMPLETE_PROC; 2936f7496dcaSJamie Gritton drflags |= PD_DEUREF; 29371158508aSJamie Gritton } 2938f7496dcaSJamie Gritton prison_deref(pr, drflags); 2939b38ff370SJamie Gritton } 2940b38ff370SJamie Gritton 2941b38ff370SJamie Gritton /* 2942effad35eSJamie Gritton * Remove a prison reference and/or user reference (usually). 2943effad35eSJamie Gritton * This assumes context that allows sleeping (for allprison_lock), 2944effad35eSJamie Gritton * with no non-sleeping locks held, except perhaps the prison itself. 2945effad35eSJamie Gritton * If there are no more references, release and delist the prison. 2946effad35eSJamie Gritton * On completion, the prison lock and the allprison lock are both 2947effad35eSJamie Gritton * unlocked. 2948b38ff370SJamie Gritton */ 2949b38ff370SJamie Gritton static void 2950b38ff370SJamie Gritton prison_deref(struct prison *pr, int flags) 2951b38ff370SJamie Gritton { 29526e1d1bfcSJamie Gritton struct prisonlist freeprison; 2953c861373bSJamie Gritton struct prison *killpr, *rpr, *ppr, *tpr; 2954c861373bSJamie Gritton struct proc *p; 2955c2cda609SPawel Jakub Dawidek 2956c861373bSJamie Gritton killpr = NULL; 29576e1d1bfcSJamie Gritton TAILQ_INIT(&freeprison); 29586e1d1bfcSJamie Gritton /* 29596e1d1bfcSJamie Gritton * Release this prison as requested, which may cause its parent 29606e1d1bfcSJamie Gritton * to be released, and then maybe its grandparent, etc. 29616e1d1bfcSJamie Gritton */ 29620304c731SJamie Gritton for (;;) { 2963c861373bSJamie Gritton if (flags & PD_KILL) { 2964c861373bSJamie Gritton /* Kill the prison and its descendents. */ 2965589e4c1dSJamie Gritton KASSERT(pr != &prison0, 2966589e4c1dSJamie Gritton ("prison_deref trying to kill prison0")); 2967c861373bSJamie Gritton if (!(flags & PD_DEREF)) { 2968c861373bSJamie Gritton prison_hold(pr); 2969c861373bSJamie Gritton flags |= PD_DEREF; 2970c861373bSJamie Gritton } 2971c861373bSJamie Gritton flags = prison_lock_xlock(pr, flags); 2972c861373bSJamie Gritton prison_deref_kill(pr, &freeprison); 2973c861373bSJamie Gritton } 2974e6d5cb63SJamie Gritton if (flags & PD_DEUREF) { 2975f7496dcaSJamie Gritton /* Drop a user reference. */ 29766754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_uref) > 0, 297773d9e52dSJamie Gritton ("prison_deref PD_DEUREF on a dead prison (jid=%d)", 297873d9e52dSJamie Gritton pr->pr_id)); 2979f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_uref)) { 2980f7496dcaSJamie Gritton if (!(flags & PD_DEREF)) { 2981f7496dcaSJamie Gritton prison_hold(pr); 2982f7496dcaSJamie Gritton flags |= PD_DEREF; 2983f7496dcaSJamie Gritton } 2984f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 29851158508aSJamie Gritton if (refcount_release(&pr->pr_uref) && 29861158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE) { 2987f7496dcaSJamie Gritton /* 2988f7496dcaSJamie Gritton * When the last user references goes, 2989f7496dcaSJamie Gritton * this becomes a dying prison. 2990f7496dcaSJamie Gritton */ 2991f7496dcaSJamie Gritton KASSERT( 2992f7496dcaSJamie Gritton refcount_load(&prison0.pr_uref) > 0, 29936754ae25SJamie Gritton ("prison0 pr_uref=0")); 29941158508aSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 2995f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 2996f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 2997f7496dcaSJamie Gritton (void)osd_jail_call(pr, 2998f7496dcaSJamie Gritton PR_METHOD_REMOVE, NULL); 2999f7496dcaSJamie Gritton } 3000f7496dcaSJamie Gritton } 3001f7496dcaSJamie Gritton } 3002c861373bSJamie Gritton if (flags & PD_KILL) { 3003c861373bSJamie Gritton /* 3004c861373bSJamie Gritton * Any remaining user references are probably processes 3005c861373bSJamie Gritton * that need to be killed, either in this prison or its 3006c861373bSJamie Gritton * descendants. 3007c861373bSJamie Gritton */ 3008c861373bSJamie Gritton if (refcount_load(&pr->pr_uref) > 0) 3009c861373bSJamie Gritton killpr = pr; 3010589e4c1dSJamie Gritton /* Make sure the parent prison doesn't get killed. */ 3011589e4c1dSJamie Gritton flags &= ~PD_KILL; 3012c861373bSJamie Gritton } 301373d9e52dSJamie Gritton if (flags & PD_DEREF) { 3014f7496dcaSJamie Gritton /* Drop a reference. */ 30156754ae25SJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 301673d9e52dSJamie Gritton ("prison_deref PD_DEREF on a dead prison (jid=%d)", 301773d9e52dSJamie Gritton pr->pr_id)); 3018f7496dcaSJamie Gritton if (!refcount_release_if_not_last(&pr->pr_ref)) { 3019f7496dcaSJamie Gritton flags = prison_lock_xlock(pr, flags); 3020f7496dcaSJamie Gritton if (refcount_release(&pr->pr_ref)) { 3021cc5fd8c7SJamie Gritton /* 3022f7496dcaSJamie Gritton * When the last reference goes, 3023f7496dcaSJamie Gritton * unlink the prison and set it aside. 3024cc5fd8c7SJamie Gritton */ 3025f7496dcaSJamie Gritton KASSERT( 3026f7496dcaSJamie Gritton refcount_load(&pr->pr_uref) == 0, 3027f7496dcaSJamie Gritton ("prison_deref: last ref, " 3028f7496dcaSJamie Gritton "but still has %d urefs (jid=%d)", 3029f7496dcaSJamie Gritton pr->pr_uref, pr->pr_id)); 3030f7496dcaSJamie Gritton KASSERT( 3031f7496dcaSJamie Gritton refcount_load(&prison0.pr_ref) != 0, 3032f7496dcaSJamie Gritton ("prison0 pr_ref=0")); 30331158508aSJamie Gritton pr->pr_state = PRISON_STATE_INVALID; 3034b38ff370SJamie Gritton TAILQ_REMOVE(&allprison, pr, pr_list); 30350304c731SJamie Gritton LIST_REMOVE(pr, pr_sibling); 3036f7496dcaSJamie Gritton TAILQ_INSERT_TAIL(&freeprison, pr, 3037f7496dcaSJamie Gritton pr_list); 3038f7496dcaSJamie Gritton for (ppr = pr->pr_parent; 3039f7496dcaSJamie Gritton ppr != NULL; 3040f7496dcaSJamie Gritton ppr = ppr->pr_parent) 3041f7496dcaSJamie Gritton ppr->pr_childcount--; 3042f7496dcaSJamie Gritton /* 3043f7496dcaSJamie Gritton * Removing a prison frees references 3044f7496dcaSJamie Gritton * from its parent. 3045f7496dcaSJamie Gritton */ 3046f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3047f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 30486e1d1bfcSJamie Gritton pr = pr->pr_parent; 30496e1d1bfcSJamie Gritton flags |= PD_DEREF | PD_DEUREF; 3050f7496dcaSJamie Gritton continue; 3051f7496dcaSJamie Gritton } 3052f7496dcaSJamie Gritton } 3053f7496dcaSJamie Gritton } 3054f7496dcaSJamie Gritton break; 30556e1d1bfcSJamie Gritton } 30566e1d1bfcSJamie Gritton 30576e1d1bfcSJamie Gritton /* Release all the prison locks. */ 3058f7496dcaSJamie Gritton if (flags & PD_LOCKED) 3059f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 30606e1d1bfcSJamie Gritton if (flags & PD_LIST_SLOCKED) 30616e1d1bfcSJamie Gritton sx_sunlock(&allprison_lock); 30626e1d1bfcSJamie Gritton else if (flags & PD_LIST_XLOCKED) 30636e1d1bfcSJamie Gritton sx_xunlock(&allprison_lock); 30646e1d1bfcSJamie Gritton 3065c861373bSJamie Gritton /* Kill any processes attached to a killed prison. */ 3066c861373bSJamie Gritton if (killpr != NULL) { 3067c861373bSJamie Gritton sx_slock(&allproc_lock); 3068c861373bSJamie Gritton FOREACH_PROC_IN_SYSTEM(p) { 3069c861373bSJamie Gritton PROC_LOCK(p); 3070c861373bSJamie Gritton if (p->p_state != PRS_NEW && p->p_ucred != NULL) { 3071c861373bSJamie Gritton for (ppr = p->p_ucred->cr_prison; 3072c861373bSJamie Gritton ppr != &prison0; 3073c861373bSJamie Gritton ppr = ppr->pr_parent) 3074c861373bSJamie Gritton if (ppr == killpr) { 3075c861373bSJamie Gritton kern_psignal(p, SIGKILL); 3076c861373bSJamie Gritton break; 3077c861373bSJamie Gritton } 3078c861373bSJamie Gritton } 3079c861373bSJamie Gritton PROC_UNLOCK(p); 3080c861373bSJamie Gritton } 3081c861373bSJamie Gritton sx_sunlock(&allproc_lock); 3082c861373bSJamie Gritton } 3083c861373bSJamie Gritton 30846e1d1bfcSJamie Gritton /* 30856e1d1bfcSJamie Gritton * Finish removing any unreferenced prisons, which couldn't happen 30866e1d1bfcSJamie Gritton * while allprison_lock was held (to avoid a LOR on vrele). 30876e1d1bfcSJamie Gritton */ 30886e1d1bfcSJamie Gritton TAILQ_FOREACH_SAFE(rpr, &freeprison, pr_list, tpr) { 30896e1d1bfcSJamie Gritton #ifdef VIMAGE 30906e1d1bfcSJamie Gritton if (rpr->pr_vnet != rpr->pr_parent->pr_vnet) 30916e1d1bfcSJamie Gritton vnet_destroy(rpr->pr_vnet); 30926e1d1bfcSJamie Gritton #endif 30936e1d1bfcSJamie Gritton if (rpr->pr_root != NULL) 30946e1d1bfcSJamie Gritton vrele(rpr->pr_root); 30956e1d1bfcSJamie Gritton mtx_destroy(&rpr->pr_mtx); 30966e1d1bfcSJamie Gritton #ifdef INET 3097*eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET]); 30986e1d1bfcSJamie Gritton #endif 30996e1d1bfcSJamie Gritton #ifdef INET6 3100*eb8dcdeaSGleb Smirnoff prison_ip_free(rpr->pr_addrs[PR_INET6]); 31016e1d1bfcSJamie Gritton #endif 31026e1d1bfcSJamie Gritton if (rpr->pr_cpuset != NULL) 31036e1d1bfcSJamie Gritton cpuset_rel(rpr->pr_cpuset); 31046e1d1bfcSJamie Gritton osd_jail_exit(rpr); 31056e1d1bfcSJamie Gritton #ifdef RACCT 31066e1d1bfcSJamie Gritton if (racct_enable) 31076e1d1bfcSJamie Gritton prison_racct_detach(rpr); 31086e1d1bfcSJamie Gritton #endif 3109f7496dcaSJamie Gritton TAILQ_REMOVE(&freeprison, rpr, pr_list); 31106e1d1bfcSJamie Gritton free(rpr, M_PRISON); 31110304c731SJamie Gritton } 3112b3059e09SRobert Watson } 3113b3059e09SRobert Watson 3114413628a7SBjoern A. Zeeb /* 3115c861373bSJamie Gritton * Kill the prison and its descendants. Mark them as dying, clear the 3116c861373bSJamie Gritton * persist flag, and call module remove methods. 3117c861373bSJamie Gritton */ 3118c861373bSJamie Gritton static void 3119c861373bSJamie Gritton prison_deref_kill(struct prison *pr, struct prisonlist *freeprison) 3120c861373bSJamie Gritton { 3121c861373bSJamie Gritton struct prison *cpr, *ppr, *rpr; 3122c861373bSJamie Gritton bool descend; 3123c861373bSJamie Gritton 3124c861373bSJamie Gritton /* 3125c861373bSJamie Gritton * Unlike the descendants, the target prison can be killed 3126c861373bSJamie Gritton * even if it is currently dying. This is useful for failed 3127c861373bSJamie Gritton * creation in jail_set(2). 3128c861373bSJamie Gritton */ 3129c861373bSJamie Gritton KASSERT(refcount_load(&pr->pr_ref) > 0, 3130c861373bSJamie Gritton ("Trying to kill dead prison %p (jid=%d).", 3131c861373bSJamie Gritton pr, pr->pr_id)); 3132c861373bSJamie Gritton refcount_acquire(&pr->pr_uref); 3133c861373bSJamie Gritton pr->pr_state = PRISON_STATE_DYING; 3134c861373bSJamie Gritton mtx_unlock(&pr->pr_mtx); 3135c861373bSJamie Gritton 3136c861373bSJamie Gritton rpr = NULL; 3137c861373bSJamie Gritton FOREACH_PRISON_DESCENDANT_PRE_POST(pr, cpr, descend) { 3138c861373bSJamie Gritton if (descend) { 3139c861373bSJamie Gritton if (!prison_isalive(cpr)) { 3140c861373bSJamie Gritton descend = false; 3141c861373bSJamie Gritton continue; 3142c861373bSJamie Gritton } 3143c861373bSJamie Gritton prison_hold(cpr); 3144c861373bSJamie Gritton prison_proc_hold(cpr); 3145c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3146c861373bSJamie Gritton cpr->pr_state = PRISON_STATE_DYING; 3147c861373bSJamie Gritton cpr->pr_flags |= PR_REMOVE; 3148c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3149c861373bSJamie Gritton continue; 3150c861373bSJamie Gritton } 3151c861373bSJamie Gritton if (!(cpr->pr_flags & PR_REMOVE)) 3152c861373bSJamie Gritton continue; 3153c861373bSJamie Gritton (void)osd_jail_call(cpr, PR_METHOD_REMOVE, NULL); 3154c861373bSJamie Gritton mtx_lock(&cpr->pr_mtx); 3155c861373bSJamie Gritton cpr->pr_flags &= ~PR_REMOVE; 3156c861373bSJamie Gritton if (cpr->pr_flags & PR_PERSIST) { 3157c861373bSJamie Gritton cpr->pr_flags &= ~PR_PERSIST; 3158c861373bSJamie Gritton prison_proc_free_not_last(cpr); 3159c861373bSJamie Gritton prison_free_not_last(cpr); 3160c861373bSJamie Gritton } 3161c861373bSJamie Gritton (void)refcount_release(&cpr->pr_uref); 3162c861373bSJamie Gritton if (refcount_release(&cpr->pr_ref)) { 3163c861373bSJamie Gritton /* 3164c861373bSJamie Gritton * When the last reference goes, unlink the prison 3165c861373bSJamie Gritton * and set it aside for prison_deref() to handle. 3166c861373bSJamie Gritton * Delay unlinking the sibling list to keep the loop 3167c861373bSJamie Gritton * safe. 3168c861373bSJamie Gritton */ 3169c861373bSJamie Gritton if (rpr != NULL) 3170c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3171c861373bSJamie Gritton rpr = cpr; 3172c861373bSJamie Gritton rpr->pr_state = PRISON_STATE_INVALID; 3173c861373bSJamie Gritton TAILQ_REMOVE(&allprison, rpr, pr_list); 3174c861373bSJamie Gritton TAILQ_INSERT_TAIL(freeprison, rpr, pr_list); 3175c861373bSJamie Gritton /* 3176c861373bSJamie Gritton * Removing a prison frees references from its parent. 3177c861373bSJamie Gritton */ 3178c861373bSJamie Gritton ppr = rpr->pr_parent; 3179c861373bSJamie Gritton prison_proc_free_not_last(ppr); 3180c861373bSJamie Gritton prison_free_not_last(ppr); 3181c861373bSJamie Gritton for (; ppr != NULL; ppr = ppr->pr_parent) 3182c861373bSJamie Gritton ppr->pr_childcount--; 3183c861373bSJamie Gritton } 3184c861373bSJamie Gritton mtx_unlock(&cpr->pr_mtx); 3185c861373bSJamie Gritton } 3186c861373bSJamie Gritton if (rpr != NULL) 3187c861373bSJamie Gritton LIST_REMOVE(rpr, pr_sibling); 3188c861373bSJamie Gritton 3189c861373bSJamie Gritton (void)osd_jail_call(pr, PR_METHOD_REMOVE, NULL); 3190c861373bSJamie Gritton mtx_lock(&pr->pr_mtx); 3191c861373bSJamie Gritton if (pr->pr_flags & PR_PERSIST) { 3192c861373bSJamie Gritton pr->pr_flags &= ~PR_PERSIST; 3193c861373bSJamie Gritton prison_proc_free_not_last(pr); 3194c861373bSJamie Gritton prison_free_not_last(pr); 3195c861373bSJamie Gritton } 3196c861373bSJamie Gritton (void)refcount_release(&pr->pr_uref); 3197c861373bSJamie Gritton } 3198c861373bSJamie Gritton 3199c861373bSJamie Gritton /* 3200f7496dcaSJamie Gritton * Given the current locking state in the flags, make sure allprison_lock 3201f7496dcaSJamie Gritton * is held exclusive, and the prison is locked. Return flags indicating 3202f7496dcaSJamie Gritton * the new state. 3203f7496dcaSJamie Gritton */ 3204f7496dcaSJamie Gritton static int 3205f7496dcaSJamie Gritton prison_lock_xlock(struct prison *pr, int flags) 3206f7496dcaSJamie Gritton { 3207f7496dcaSJamie Gritton 3208f7496dcaSJamie Gritton if (!(flags & PD_LIST_XLOCKED)) { 3209f7496dcaSJamie Gritton /* 3210f7496dcaSJamie Gritton * Get allprison_lock, which may be an upgrade, 3211f7496dcaSJamie Gritton * and may require unlocking the prison. 3212f7496dcaSJamie Gritton */ 3213f7496dcaSJamie Gritton if (flags & PD_LOCKED) { 3214f7496dcaSJamie Gritton mtx_unlock(&pr->pr_mtx); 3215f7496dcaSJamie Gritton flags &= ~PD_LOCKED; 3216f7496dcaSJamie Gritton } 3217f7496dcaSJamie Gritton if (flags & PD_LIST_SLOCKED) { 3218f7496dcaSJamie Gritton if (!sx_try_upgrade(&allprison_lock)) { 3219f7496dcaSJamie Gritton sx_sunlock(&allprison_lock); 3220f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3221f7496dcaSJamie Gritton } 3222f7496dcaSJamie Gritton flags &= ~PD_LIST_SLOCKED; 3223f7496dcaSJamie Gritton } else 3224f7496dcaSJamie Gritton sx_xlock(&allprison_lock); 3225f7496dcaSJamie Gritton flags |= PD_LIST_XLOCKED; 3226f7496dcaSJamie Gritton } 3227f7496dcaSJamie Gritton if (!(flags & PD_LOCKED)) { 3228f7496dcaSJamie Gritton /* Lock the prison mutex. */ 3229f7496dcaSJamie Gritton mtx_lock(&pr->pr_mtx); 3230f7496dcaSJamie Gritton flags |= PD_LOCKED; 3231f7496dcaSJamie Gritton } 3232f7496dcaSJamie Gritton return flags; 3233f7496dcaSJamie Gritton } 3234f7496dcaSJamie Gritton 3235f7496dcaSJamie Gritton /* 32360fe74ae6SJamie Gritton * Set or clear a permission bit in the pr_allow field, passing restrictions 32370fe74ae6SJamie Gritton * (cleared permission) down to child jails. 32380fe74ae6SJamie Gritton */ 32390fe74ae6SJamie Gritton void 32400fe74ae6SJamie Gritton prison_set_allow(struct ucred *cred, unsigned flag, int enable) 32410fe74ae6SJamie Gritton { 32420fe74ae6SJamie Gritton struct prison *pr; 32430fe74ae6SJamie Gritton 32440fe74ae6SJamie Gritton pr = cred->cr_prison; 32450fe74ae6SJamie Gritton sx_slock(&allprison_lock); 32460fe74ae6SJamie Gritton mtx_lock(&pr->pr_mtx); 32470fe74ae6SJamie Gritton prison_set_allow_locked(pr, flag, enable); 32480fe74ae6SJamie Gritton mtx_unlock(&pr->pr_mtx); 32490fe74ae6SJamie Gritton sx_sunlock(&allprison_lock); 32500fe74ae6SJamie Gritton } 32510fe74ae6SJamie Gritton 32520fe74ae6SJamie Gritton static void 32530fe74ae6SJamie Gritton prison_set_allow_locked(struct prison *pr, unsigned flag, int enable) 32540fe74ae6SJamie Gritton { 32550fe74ae6SJamie Gritton struct prison *cpr; 32560fe74ae6SJamie Gritton int descend; 32570fe74ae6SJamie Gritton 32580fe74ae6SJamie Gritton if (enable != 0) 32590fe74ae6SJamie Gritton pr->pr_allow |= flag; 32600fe74ae6SJamie Gritton else { 32610fe74ae6SJamie Gritton pr->pr_allow &= ~flag; 32620fe74ae6SJamie Gritton FOREACH_PRISON_DESCENDANT_LOCKED(pr, cpr, descend) 32630fe74ae6SJamie Gritton cpr->pr_allow &= ~flag; 32640fe74ae6SJamie Gritton } 32650fe74ae6SJamie Gritton } 32660fe74ae6SJamie Gritton 32670fe74ae6SJamie Gritton /* 3268ca04ba64SJamie Gritton * Check if a jail supports the given address family. 3269ca04ba64SJamie Gritton * 3270ca04ba64SJamie Gritton * Returns 0 if not jailed or the address family is supported, EAFNOSUPPORT 3271ca04ba64SJamie Gritton * if not. 3272ca04ba64SJamie Gritton */ 3273ca04ba64SJamie Gritton int 3274ca04ba64SJamie Gritton prison_check_af(struct ucred *cred, int af) 3275ca04ba64SJamie Gritton { 32760304c731SJamie Gritton struct prison *pr; 3277ca04ba64SJamie Gritton int error; 3278ca04ba64SJamie Gritton 3279ca04ba64SJamie Gritton KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3280ca04ba64SJamie Gritton 32810304c731SJamie Gritton pr = cred->cr_prison; 3282499650a0SJamie Gritton #ifdef VIMAGE 32836bb79563SJamie Gritton /* Prisons with their own network stack are not limited. */ 3284de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 32856bb79563SJamie Gritton return (0); 3286499650a0SJamie Gritton #endif 32876bb79563SJamie Gritton 3288ca04ba64SJamie Gritton error = 0; 3289ca04ba64SJamie Gritton switch (af) 3290ca04ba64SJamie Gritton { 3291ca04ba64SJamie Gritton #ifdef INET 3292ca04ba64SJamie Gritton case AF_INET: 32930304c731SJamie Gritton if (pr->pr_flags & PR_IP4) 32940304c731SJamie Gritton { 32950304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3296*eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP4) && 3297*eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET] == NULL) 3298ca04ba64SJamie Gritton error = EAFNOSUPPORT; 32990304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33000304c731SJamie Gritton } 3301ca04ba64SJamie Gritton break; 3302ca04ba64SJamie Gritton #endif 3303ca04ba64SJamie Gritton #ifdef INET6 3304ca04ba64SJamie Gritton case AF_INET6: 33050304c731SJamie Gritton if (pr->pr_flags & PR_IP6) 33060304c731SJamie Gritton { 33070304c731SJamie Gritton mtx_lock(&pr->pr_mtx); 3308*eb8dcdeaSGleb Smirnoff if ((pr->pr_flags & PR_IP6) && 3309*eb8dcdeaSGleb Smirnoff pr->pr_addrs[PR_INET6] == NULL) 3310ca04ba64SJamie Gritton error = EAFNOSUPPORT; 33110304c731SJamie Gritton mtx_unlock(&pr->pr_mtx); 33120304c731SJamie Gritton } 3313ca04ba64SJamie Gritton break; 3314ca04ba64SJamie Gritton #endif 3315ca04ba64SJamie Gritton case AF_LOCAL: 3316ca04ba64SJamie Gritton case AF_ROUTE: 3317ca04ba64SJamie Gritton break; 3318ca04ba64SJamie Gritton default: 33190304c731SJamie Gritton if (!(pr->pr_allow & PR_ALLOW_SOCKET_AF)) 3320ca04ba64SJamie Gritton error = EAFNOSUPPORT; 3321ca04ba64SJamie Gritton } 3322ca04ba64SJamie Gritton return (error); 3323ca04ba64SJamie Gritton } 3324ca04ba64SJamie Gritton 3325ca04ba64SJamie Gritton /* 3326413628a7SBjoern A. Zeeb * Check if given address belongs to the jail referenced by cred (wrapper to 3327413628a7SBjoern A. Zeeb * prison_check_ip[46]). 3328413628a7SBjoern A. Zeeb * 33290304c731SJamie Gritton * Returns 0 if jail doesn't restrict the address family or if address belongs 33300304c731SJamie Gritton * to jail, EADDRNOTAVAIL if the address doesn't belong, or EAFNOSUPPORT if 33310304c731SJamie Gritton * the jail doesn't allow the address family. IPv4 Address passed in in NBO. 3332413628a7SBjoern A. Zeeb */ 3333413628a7SBjoern A. Zeeb int 3334c83dda36SAlexander V. Chernikov prison_if(struct ucred *cred, const struct sockaddr *sa) 333575c13541SPoul-Henning Kamp { 3336413628a7SBjoern A. Zeeb #ifdef INET 3337c83dda36SAlexander V. Chernikov const struct sockaddr_in *sai; 3338413628a7SBjoern A. Zeeb #endif 3339413628a7SBjoern A. Zeeb #ifdef INET6 3340c83dda36SAlexander V. Chernikov const struct sockaddr_in6 *sai6; 3341413628a7SBjoern A. Zeeb #endif 3342b89e82ddSJamie Gritton int error; 334375c13541SPoul-Henning Kamp 3344413628a7SBjoern A. Zeeb KASSERT(cred != NULL, ("%s: cred is NULL", __func__)); 3345413628a7SBjoern A. Zeeb KASSERT(sa != NULL, ("%s: sa is NULL", __func__)); 3346413628a7SBjoern A. Zeeb 3347de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3348de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3349de0bd6f7SBjoern A. Zeeb return (0); 3350de0bd6f7SBjoern A. Zeeb #endif 3351de0bd6f7SBjoern A. Zeeb 3352b89e82ddSJamie Gritton error = 0; 3353413628a7SBjoern A. Zeeb switch (sa->sa_family) 3354413628a7SBjoern A. Zeeb { 3355413628a7SBjoern A. Zeeb #ifdef INET 3356413628a7SBjoern A. Zeeb case AF_INET: 3357c83dda36SAlexander V. Chernikov sai = (const struct sockaddr_in *)sa; 3358b89e82ddSJamie Gritton error = prison_check_ip4(cred, &sai->sin_addr); 3359413628a7SBjoern A. Zeeb break; 3360413628a7SBjoern A. Zeeb #endif 3361413628a7SBjoern A. Zeeb #ifdef INET6 3362413628a7SBjoern A. Zeeb case AF_INET6: 3363c83dda36SAlexander V. Chernikov sai6 = (const struct sockaddr_in6 *)sa; 3364b89e82ddSJamie Gritton error = prison_check_ip6(cred, &sai6->sin6_addr); 3365413628a7SBjoern A. Zeeb break; 3366413628a7SBjoern A. Zeeb #endif 3367413628a7SBjoern A. Zeeb default: 33680304c731SJamie Gritton if (!(cred->cr_prison->pr_allow & PR_ALLOW_SOCKET_AF)) 3369b89e82ddSJamie Gritton error = EAFNOSUPPORT; 3370413628a7SBjoern A. Zeeb } 3371b89e82ddSJamie Gritton return (error); 337275c13541SPoul-Henning Kamp } 337391421ba2SRobert Watson 337491421ba2SRobert Watson /* 337591421ba2SRobert Watson * Return 0 if jails permit p1 to frob p2, otherwise ESRCH. 337691421ba2SRobert Watson */ 337791421ba2SRobert Watson int 33789ddb7954SMike Barcroft prison_check(struct ucred *cred1, struct ucred *cred2) 337991421ba2SRobert Watson { 338091421ba2SRobert Watson 33810304c731SJamie Gritton return ((cred1->cr_prison == cred2->cr_prison || 33820304c731SJamie Gritton prison_ischild(cred1->cr_prison, cred2->cr_prison)) ? 0 : ESRCH); 33830304c731SJamie Gritton } 338491421ba2SRobert Watson 33850304c731SJamie Gritton /* 33860304c731SJamie Gritton * Return 1 if p2 is a child of p1, otherwise 0. 33870304c731SJamie Gritton */ 33880304c731SJamie Gritton int 33890304c731SJamie Gritton prison_ischild(struct prison *pr1, struct prison *pr2) 33900304c731SJamie Gritton { 33910304c731SJamie Gritton 33920304c731SJamie Gritton for (pr2 = pr2->pr_parent; pr2 != NULL; pr2 = pr2->pr_parent) 33930304c731SJamie Gritton if (pr1 == pr2) 33940304c731SJamie Gritton return (1); 339591421ba2SRobert Watson return (0); 339691421ba2SRobert Watson } 339791421ba2SRobert Watson 339891421ba2SRobert Watson /* 3399f7496dcaSJamie Gritton * Return true if the prison is currently alive. A prison is alive if it 3400f7496dcaSJamie Gritton * holds user references and it isn't being removed. 340176ad42abSJamie Gritton */ 340276ad42abSJamie Gritton bool 3403*eb8dcdeaSGleb Smirnoff prison_isalive(const struct prison *pr) 340476ad42abSJamie Gritton { 340576ad42abSJamie Gritton 34061158508aSJamie Gritton if (__predict_false(pr->pr_state != PRISON_STATE_ALIVE)) 3407cc7b7306SJamie Gritton return (false); 340876ad42abSJamie Gritton return (true); 340976ad42abSJamie Gritton } 341076ad42abSJamie Gritton 341176ad42abSJamie Gritton /* 341276ad42abSJamie Gritton * Return true if the prison is currently valid. A prison is valid if it has 341376ad42abSJamie Gritton * been fully created, and is not being destroyed. Note that dying prisons 3414f7496dcaSJamie Gritton * are still considered valid. Invalid prisons won't be found under normal 3415f7496dcaSJamie Gritton * circumstances, as they're only put in that state by functions that have 3416f7496dcaSJamie Gritton * an exclusive hold on allprison_lock. 341776ad42abSJamie Gritton */ 341876ad42abSJamie Gritton bool 341976ad42abSJamie Gritton prison_isvalid(struct prison *pr) 342076ad42abSJamie Gritton { 342176ad42abSJamie Gritton 34221158508aSJamie Gritton if (__predict_false(pr->pr_state == PRISON_STATE_INVALID)) 34231158508aSJamie Gritton return (false); 34246754ae25SJamie Gritton if (__predict_false(refcount_load(&pr->pr_ref) == 0)) 342576ad42abSJamie Gritton return (false); 342676ad42abSJamie Gritton return (true); 342776ad42abSJamie Gritton } 342876ad42abSJamie Gritton 342976ad42abSJamie Gritton /* 3430de0bd6f7SBjoern A. Zeeb * Return 1 if the passed credential is in a jail and that jail does not 3431de0bd6f7SBjoern A. Zeeb * have its own virtual network stack, otherwise 0. 3432de0bd6f7SBjoern A. Zeeb */ 3433de0bd6f7SBjoern A. Zeeb int 3434de0bd6f7SBjoern A. Zeeb jailed_without_vnet(struct ucred *cred) 3435de0bd6f7SBjoern A. Zeeb { 3436de0bd6f7SBjoern A. Zeeb 3437de0bd6f7SBjoern A. Zeeb if (!jailed(cred)) 3438de0bd6f7SBjoern A. Zeeb return (0); 3439de0bd6f7SBjoern A. Zeeb #ifdef VIMAGE 3440de0bd6f7SBjoern A. Zeeb if (prison_owns_vnet(cred)) 3441de0bd6f7SBjoern A. Zeeb return (0); 3442de0bd6f7SBjoern A. Zeeb #endif 3443de0bd6f7SBjoern A. Zeeb 3444de0bd6f7SBjoern A. Zeeb return (1); 3445de0bd6f7SBjoern A. Zeeb } 3446de0bd6f7SBjoern A. Zeeb 3447de0bd6f7SBjoern A. Zeeb /* 34487455b100SJamie Gritton * Return the correct hostname (domainname, et al) for the passed credential. 34499484d0c0SRobert Drehmel */ 3450ad1ff099SRobert Drehmel void 34519ddb7954SMike Barcroft getcredhostname(struct ucred *cred, char *buf, size_t size) 34529484d0c0SRobert Drehmel { 345376ca6f88SJamie Gritton struct prison *pr; 34549484d0c0SRobert Drehmel 34557455b100SJamie Gritton /* 34567455b100SJamie Gritton * A NULL credential can be used to shortcut to the physical 34577455b100SJamie Gritton * system's hostname. 34587455b100SJamie Gritton */ 345976ca6f88SJamie Gritton pr = (cred != NULL) ? cred->cr_prison : &prison0; 346076ca6f88SJamie Gritton mtx_lock(&pr->pr_mtx); 3461c1f19219SJamie Gritton strlcpy(buf, pr->pr_hostname, size); 346276ca6f88SJamie Gritton mtx_unlock(&pr->pr_mtx); 34639484d0c0SRobert Drehmel } 3464fd7a8150SMike Barcroft 34657455b100SJamie Gritton void 34667455b100SJamie Gritton getcreddomainname(struct ucred *cred, char *buf, size_t size) 34677455b100SJamie Gritton { 34687455b100SJamie Gritton 34697455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3470c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_domainname, size); 34717455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34727455b100SJamie Gritton } 34737455b100SJamie Gritton 34747455b100SJamie Gritton void 34757455b100SJamie Gritton getcredhostuuid(struct ucred *cred, char *buf, size_t size) 34767455b100SJamie Gritton { 34777455b100SJamie Gritton 34787455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 3479c1f19219SJamie Gritton strlcpy(buf, cred->cr_prison->pr_hostuuid, size); 34807455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34817455b100SJamie Gritton } 34827455b100SJamie Gritton 34837455b100SJamie Gritton void 34847455b100SJamie Gritton getcredhostid(struct ucred *cred, unsigned long *hostid) 34857455b100SJamie Gritton { 34867455b100SJamie Gritton 34877455b100SJamie Gritton mtx_lock(&cred->cr_prison->pr_mtx); 34887455b100SJamie Gritton *hostid = cred->cr_prison->pr_hostid; 34897455b100SJamie Gritton mtx_unlock(&cred->cr_prison->pr_mtx); 34907455b100SJamie Gritton } 34917455b100SJamie Gritton 34923f8bc99cSKristof Provost void 34933f8bc99cSKristof Provost getjailname(struct ucred *cred, char *name, size_t len) 34943f8bc99cSKristof Provost { 34953f8bc99cSKristof Provost 34963f8bc99cSKristof Provost mtx_lock(&cred->cr_prison->pr_mtx); 34973f8bc99cSKristof Provost strlcpy(name, cred->cr_prison->pr_name, len); 34983f8bc99cSKristof Provost mtx_unlock(&cred->cr_prison->pr_mtx); 34993f8bc99cSKristof Provost } 35003f8bc99cSKristof Provost 3501eb79e1c7SBjoern A. Zeeb #ifdef VIMAGE 3502eb79e1c7SBjoern A. Zeeb /* 3503eb79e1c7SBjoern A. Zeeb * Determine whether the prison represented by cred owns 3504eb79e1c7SBjoern A. Zeeb * its vnet rather than having it inherited. 3505eb79e1c7SBjoern A. Zeeb * 3506eb79e1c7SBjoern A. Zeeb * Returns 1 in case the prison owns the vnet, 0 otherwise. 3507eb79e1c7SBjoern A. Zeeb */ 3508eb79e1c7SBjoern A. Zeeb int 3509eb79e1c7SBjoern A. Zeeb prison_owns_vnet(struct ucred *cred) 3510eb79e1c7SBjoern A. Zeeb { 3511eb79e1c7SBjoern A. Zeeb 3512eb79e1c7SBjoern A. Zeeb /* 3513eb79e1c7SBjoern A. Zeeb * vnets cannot be added/removed after jail creation, 3514eb79e1c7SBjoern A. Zeeb * so no need to lock here. 3515eb79e1c7SBjoern A. Zeeb */ 3516eb79e1c7SBjoern A. Zeeb return (cred->cr_prison->pr_flags & PR_VNET ? 1 : 0); 3517eb79e1c7SBjoern A. Zeeb } 3518eb79e1c7SBjoern A. Zeeb #endif 3519eb79e1c7SBjoern A. Zeeb 3520f08df373SRobert Watson /* 3521820a0de9SPawel Jakub Dawidek * Determine whether the subject represented by cred can "see" 3522820a0de9SPawel Jakub Dawidek * status of a mount point. 3523820a0de9SPawel Jakub Dawidek * Returns: 0 for permitted, ENOENT otherwise. 3524820a0de9SPawel Jakub Dawidek * XXX: This function should be called cr_canseemount() and should be 3525820a0de9SPawel Jakub Dawidek * placed in kern_prot.c. 3526f08df373SRobert Watson */ 3527f08df373SRobert Watson int 3528820a0de9SPawel Jakub Dawidek prison_canseemount(struct ucred *cred, struct mount *mp) 3529f08df373SRobert Watson { 3530820a0de9SPawel Jakub Dawidek struct prison *pr; 3531820a0de9SPawel Jakub Dawidek struct statfs *sp; 3532820a0de9SPawel Jakub Dawidek size_t len; 3533f08df373SRobert Watson 3534820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 35350304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 35360304c731SJamie Gritton return (0); 3537820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) 3538820a0de9SPawel Jakub Dawidek return (0); 35390304c731SJamie Gritton if (pr->pr_enforce_statfs == 2) 3540820a0de9SPawel Jakub Dawidek return (ENOENT); 3541820a0de9SPawel Jakub Dawidek /* 3542820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3543820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3544820a0de9SPawel Jakub Dawidek * This is ugly check, but this is the only situation when jail's 3545820a0de9SPawel Jakub Dawidek * directory ends with '/'. 3546820a0de9SPawel Jakub Dawidek */ 3547820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3548820a0de9SPawel Jakub Dawidek return (0); 3549820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3550820a0de9SPawel Jakub Dawidek sp = &mp->mnt_stat; 3551820a0de9SPawel Jakub Dawidek if (strncmp(pr->pr_path, sp->f_mntonname, len) != 0) 3552820a0de9SPawel Jakub Dawidek return (ENOENT); 3553820a0de9SPawel Jakub Dawidek /* 3554820a0de9SPawel Jakub Dawidek * Be sure that we don't have situation where jail's root directory 3555820a0de9SPawel Jakub Dawidek * is "/some/path" and mount point is "/some/pathpath". 3556820a0de9SPawel Jakub Dawidek */ 3557820a0de9SPawel Jakub Dawidek if (sp->f_mntonname[len] != '\0' && sp->f_mntonname[len] != '/') 3558820a0de9SPawel Jakub Dawidek return (ENOENT); 3559f08df373SRobert Watson return (0); 3560f08df373SRobert Watson } 3561820a0de9SPawel Jakub Dawidek 3562820a0de9SPawel Jakub Dawidek void 3563820a0de9SPawel Jakub Dawidek prison_enforce_statfs(struct ucred *cred, struct mount *mp, struct statfs *sp) 3564820a0de9SPawel Jakub Dawidek { 3565820a0de9SPawel Jakub Dawidek char jpath[MAXPATHLEN]; 3566820a0de9SPawel Jakub Dawidek struct prison *pr; 3567820a0de9SPawel Jakub Dawidek size_t len; 3568820a0de9SPawel Jakub Dawidek 3569820a0de9SPawel Jakub Dawidek pr = cred->cr_prison; 35700304c731SJamie Gritton if (pr->pr_enforce_statfs == 0) 35710304c731SJamie Gritton return; 3572820a0de9SPawel Jakub Dawidek if (prison_canseemount(cred, mp) != 0) { 3573820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3574820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, "[restricted]", 3575820a0de9SPawel Jakub Dawidek sizeof(sp->f_mntonname)); 3576820a0de9SPawel Jakub Dawidek return; 3577820a0de9SPawel Jakub Dawidek } 3578820a0de9SPawel Jakub Dawidek if (pr->pr_root->v_mount == mp) { 3579820a0de9SPawel Jakub Dawidek /* 3580820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3581820a0de9SPawel Jakub Dawidek * the valid path left there. 3582820a0de9SPawel Jakub Dawidek */ 3583820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3584820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3585820a0de9SPawel Jakub Dawidek return; 3586820a0de9SPawel Jakub Dawidek } 3587820a0de9SPawel Jakub Dawidek /* 3588820a0de9SPawel Jakub Dawidek * If jail's chroot directory is set to "/" we should be able to see 3589820a0de9SPawel Jakub Dawidek * all mount-points from inside a jail. 3590820a0de9SPawel Jakub Dawidek */ 3591820a0de9SPawel Jakub Dawidek if (strcmp(pr->pr_path, "/") == 0) 3592820a0de9SPawel Jakub Dawidek return; 3593820a0de9SPawel Jakub Dawidek len = strlen(pr->pr_path); 3594820a0de9SPawel Jakub Dawidek strlcpy(jpath, sp->f_mntonname + len, sizeof(jpath)); 3595820a0de9SPawel Jakub Dawidek /* 3596820a0de9SPawel Jakub Dawidek * Clear current buffer data, so we are sure nothing from 3597820a0de9SPawel Jakub Dawidek * the valid path left there. 3598820a0de9SPawel Jakub Dawidek */ 3599820a0de9SPawel Jakub Dawidek bzero(sp->f_mntonname, sizeof(sp->f_mntonname)); 3600820a0de9SPawel Jakub Dawidek if (*jpath == '\0') { 3601820a0de9SPawel Jakub Dawidek /* Should never happen. */ 3602820a0de9SPawel Jakub Dawidek *sp->f_mntonname = '/'; 3603820a0de9SPawel Jakub Dawidek } else { 3604820a0de9SPawel Jakub Dawidek strlcpy(sp->f_mntonname, jpath, sizeof(sp->f_mntonname)); 3605820a0de9SPawel Jakub Dawidek } 3606f08df373SRobert Watson } 3607f08df373SRobert Watson 3608800c9408SRobert Watson /* 3609800c9408SRobert Watson * Check with permission for a specific privilege is granted within jail. We 3610800c9408SRobert Watson * have a specific list of accepted privileges; the rest are denied. 3611800c9408SRobert Watson */ 3612800c9408SRobert Watson int 3613800c9408SRobert Watson prison_priv_check(struct ucred *cred, int priv) 3614800c9408SRobert Watson { 36150fe74ae6SJamie Gritton struct prison *pr; 36160fe74ae6SJamie Gritton int error; 3617800c9408SRobert Watson 36187b2ff0dcSMateusz Guzik /* 36197b2ff0dcSMateusz Guzik * Some policies have custom handlers. This routine should not be 36207b2ff0dcSMateusz Guzik * called for them. See priv_check_cred(). 36217b2ff0dcSMateusz Guzik */ 36227b2ff0dcSMateusz Guzik switch (priv) { 3623a459a6cfSMateusz Guzik case PRIV_VFS_LOOKUP: 36247b2ff0dcSMateusz Guzik case PRIV_VFS_GENERATION: 36257b2ff0dcSMateusz Guzik KASSERT(0, ("prison_priv_check instead of a custom handler " 36267b2ff0dcSMateusz Guzik "called for %d\n", priv)); 36277b2ff0dcSMateusz Guzik } 36287b2ff0dcSMateusz Guzik 3629800c9408SRobert Watson if (!jailed(cred)) 3630800c9408SRobert Watson return (0); 3631800c9408SRobert Watson 36326bb79563SJamie Gritton #ifdef VIMAGE 36336bb79563SJamie Gritton /* 36346bb79563SJamie Gritton * Privileges specific to prisons with a virtual network stack. 36356bb79563SJamie Gritton * There might be a duplicate entry here in case the privilege 36366bb79563SJamie Gritton * is only granted conditionally in the legacy jail case. 36376bb79563SJamie Gritton */ 36386bb79563SJamie Gritton switch (priv) { 36396bb79563SJamie Gritton #ifdef notyet 36406bb79563SJamie Gritton /* 36416bb79563SJamie Gritton * NFS-specific privileges. 36426bb79563SJamie Gritton */ 36436bb79563SJamie Gritton case PRIV_NFS_DAEMON: 36446bb79563SJamie Gritton case PRIV_NFS_LOCKD: 36456bb79563SJamie Gritton #endif 36466bb79563SJamie Gritton /* 36476bb79563SJamie Gritton * Network stack privileges. 36486bb79563SJamie Gritton */ 36496bb79563SJamie Gritton case PRIV_NET_BRIDGE: 36506bb79563SJamie Gritton case PRIV_NET_GRE: 36516bb79563SJamie Gritton case PRIV_NET_BPF: 36526bb79563SJamie Gritton case PRIV_NET_RAW: /* Dup, cond. in legacy jail case. */ 36536bb79563SJamie Gritton case PRIV_NET_ROUTE: 36546bb79563SJamie Gritton case PRIV_NET_TAP: 36556bb79563SJamie Gritton case PRIV_NET_SETIFMTU: 36566bb79563SJamie Gritton case PRIV_NET_SETIFFLAGS: 36576bb79563SJamie Gritton case PRIV_NET_SETIFCAP: 3658215940b3SXin LI case PRIV_NET_SETIFDESCR: 36596bb79563SJamie Gritton case PRIV_NET_SETIFNAME : 36606bb79563SJamie Gritton case PRIV_NET_SETIFMETRIC: 36616bb79563SJamie Gritton case PRIV_NET_SETIFPHYS: 36626bb79563SJamie Gritton case PRIV_NET_SETIFMAC: 3663389474c1SKonstantin Belousov case PRIV_NET_SETLANPCP: 36646bb79563SJamie Gritton case PRIV_NET_ADDMULTI: 36656bb79563SJamie Gritton case PRIV_NET_DELMULTI: 36666bb79563SJamie Gritton case PRIV_NET_HWIOCTL: 36676bb79563SJamie Gritton case PRIV_NET_SETLLADDR: 36686bb79563SJamie Gritton case PRIV_NET_ADDIFGROUP: 36696bb79563SJamie Gritton case PRIV_NET_DELIFGROUP: 36706bb79563SJamie Gritton case PRIV_NET_IFCREATE: 36716bb79563SJamie Gritton case PRIV_NET_IFDESTROY: 36726bb79563SJamie Gritton case PRIV_NET_ADDIFADDR: 36736bb79563SJamie Gritton case PRIV_NET_DELIFADDR: 36746bb79563SJamie Gritton case PRIV_NET_LAGG: 36756bb79563SJamie Gritton case PRIV_NET_GIF: 36766bb79563SJamie Gritton case PRIV_NET_SETIFVNET: 367735fd7bc0SBjoern A. Zeeb case PRIV_NET_SETIFFIB: 36786bb79563SJamie Gritton 36796bb79563SJamie Gritton /* 36806bb79563SJamie Gritton * 802.11-related privileges. 36816bb79563SJamie Gritton */ 3682f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_GETKEY: 3683f7d38a13SAdrian Chadd case PRIV_NET80211_VAP_MANAGE: 36846bb79563SJamie Gritton 36856bb79563SJamie Gritton #ifdef notyet 36866bb79563SJamie Gritton /* 36876bb79563SJamie Gritton * ATM privileges. 36886bb79563SJamie Gritton */ 36896bb79563SJamie Gritton case PRIV_NETATM_CFG: 36906bb79563SJamie Gritton case PRIV_NETATM_ADD: 36916bb79563SJamie Gritton case PRIV_NETATM_DEL: 36926bb79563SJamie Gritton case PRIV_NETATM_SET: 36936bb79563SJamie Gritton 36946bb79563SJamie Gritton /* 36956bb79563SJamie Gritton * Bluetooth privileges. 36966bb79563SJamie Gritton */ 36976bb79563SJamie Gritton case PRIV_NETBLUETOOTH_RAW: 36986bb79563SJamie Gritton #endif 36996bb79563SJamie Gritton 37006bb79563SJamie Gritton /* 37016bb79563SJamie Gritton * Netgraph and netgraph module privileges. 37026bb79563SJamie Gritton */ 37036bb79563SJamie Gritton case PRIV_NETGRAPH_CONTROL: 37046bb79563SJamie Gritton #ifdef notyet 37056bb79563SJamie Gritton case PRIV_NETGRAPH_TTY: 37066bb79563SJamie Gritton #endif 37076bb79563SJamie Gritton 37086bb79563SJamie Gritton /* 37096bb79563SJamie Gritton * IPv4 and IPv6 privileges. 37106bb79563SJamie Gritton */ 37116bb79563SJamie Gritton case PRIV_NETINET_IPFW: 37126bb79563SJamie Gritton case PRIV_NETINET_DIVERT: 37136bb79563SJamie Gritton case PRIV_NETINET_PF: 37146bb79563SJamie Gritton case PRIV_NETINET_DUMMYNET: 37156bb79563SJamie Gritton case PRIV_NETINET_CARP: 37166bb79563SJamie Gritton case PRIV_NETINET_MROUTE: 37176bb79563SJamie Gritton case PRIV_NETINET_RAW: 37186bb79563SJamie Gritton case PRIV_NETINET_ADDRCTRL6: 37196bb79563SJamie Gritton case PRIV_NETINET_ND6: 37206bb79563SJamie Gritton case PRIV_NETINET_SCOPE6: 37216bb79563SJamie Gritton case PRIV_NETINET_ALIFETIME6: 37226bb79563SJamie Gritton case PRIV_NETINET_IPSEC: 37236bb79563SJamie Gritton case PRIV_NETINET_BINDANY: 37246bb79563SJamie Gritton 37256bb79563SJamie Gritton #ifdef notyet 37266bb79563SJamie Gritton /* 37276bb79563SJamie Gritton * NCP privileges. 37286bb79563SJamie Gritton */ 37296bb79563SJamie Gritton case PRIV_NETNCP: 37306bb79563SJamie Gritton 37316bb79563SJamie Gritton /* 37326bb79563SJamie Gritton * SMB privileges. 37336bb79563SJamie Gritton */ 37346bb79563SJamie Gritton case PRIV_NETSMB: 37356bb79563SJamie Gritton #endif 37366bb79563SJamie Gritton 37376bb79563SJamie Gritton /* 37386bb79563SJamie Gritton * No default: or deny here. 37396bb79563SJamie Gritton * In case of no permit fall through to next switch(). 37406bb79563SJamie Gritton */ 37416bb79563SJamie Gritton if (cred->cr_prison->pr_flags & PR_VNET) 37426bb79563SJamie Gritton return (0); 37436bb79563SJamie Gritton } 37446bb79563SJamie Gritton #endif /* VIMAGE */ 37456bb79563SJamie Gritton 3746800c9408SRobert Watson switch (priv) { 3747800c9408SRobert Watson /* 3748800c9408SRobert Watson * Allow ktrace privileges for root in jail. 3749800c9408SRobert Watson */ 3750800c9408SRobert Watson case PRIV_KTRACE: 3751800c9408SRobert Watson 3752c3c1b5e6SRobert Watson #if 0 3753800c9408SRobert Watson /* 3754800c9408SRobert Watson * Allow jailed processes to configure audit identity and 3755800c9408SRobert Watson * submit audit records (login, etc). In the future we may 3756800c9408SRobert Watson * want to further refine the relationship between audit and 3757800c9408SRobert Watson * jail. 3758800c9408SRobert Watson */ 3759800c9408SRobert Watson case PRIV_AUDIT_GETAUDIT: 3760800c9408SRobert Watson case PRIV_AUDIT_SETAUDIT: 3761800c9408SRobert Watson case PRIV_AUDIT_SUBMIT: 3762c3c1b5e6SRobert Watson #endif 3763800c9408SRobert Watson 3764800c9408SRobert Watson /* 3765800c9408SRobert Watson * Allow jailed processes to manipulate process UNIX 3766800c9408SRobert Watson * credentials in any way they see fit. 3767800c9408SRobert Watson */ 3768800c9408SRobert Watson case PRIV_CRED_SETUID: 3769800c9408SRobert Watson case PRIV_CRED_SETEUID: 3770800c9408SRobert Watson case PRIV_CRED_SETGID: 3771800c9408SRobert Watson case PRIV_CRED_SETEGID: 3772800c9408SRobert Watson case PRIV_CRED_SETGROUPS: 3773800c9408SRobert Watson case PRIV_CRED_SETREUID: 3774800c9408SRobert Watson case PRIV_CRED_SETREGID: 3775800c9408SRobert Watson case PRIV_CRED_SETRESUID: 3776800c9408SRobert Watson case PRIV_CRED_SETRESGID: 3777800c9408SRobert Watson 3778800c9408SRobert Watson /* 3779800c9408SRobert Watson * Jail implements visibility constraints already, so allow 3780800c9408SRobert Watson * jailed root to override uid/gid-based constraints. 3781800c9408SRobert Watson */ 3782800c9408SRobert Watson case PRIV_SEEOTHERGIDS: 3783800c9408SRobert Watson case PRIV_SEEOTHERUIDS: 3784800c9408SRobert Watson 3785800c9408SRobert Watson /* 3786800c9408SRobert Watson * Jail implements inter-process debugging limits already, so 3787800c9408SRobert Watson * allow jailed root various debugging privileges. 3788800c9408SRobert Watson */ 3789800c9408SRobert Watson case PRIV_DEBUG_DIFFCRED: 3790800c9408SRobert Watson case PRIV_DEBUG_SUGID: 3791800c9408SRobert Watson case PRIV_DEBUG_UNPRIV: 3792800c9408SRobert Watson 3793800c9408SRobert Watson /* 3794800c9408SRobert Watson * Allow jail to set various resource limits and login 3795800c9408SRobert Watson * properties, and for now, exceed process resource limits. 3796800c9408SRobert Watson */ 3797800c9408SRobert Watson case PRIV_PROC_LIMIT: 3798800c9408SRobert Watson case PRIV_PROC_SETLOGIN: 3799800c9408SRobert Watson case PRIV_PROC_SETRLIMIT: 3800800c9408SRobert Watson 3801800c9408SRobert Watson /* 3802800c9408SRobert Watson * System V and POSIX IPC privileges are granted in jail. 3803800c9408SRobert Watson */ 3804800c9408SRobert Watson case PRIV_IPC_READ: 3805800c9408SRobert Watson case PRIV_IPC_WRITE: 3806800c9408SRobert Watson case PRIV_IPC_ADMIN: 3807800c9408SRobert Watson case PRIV_IPC_MSGSIZE: 3808800c9408SRobert Watson case PRIV_MQ_ADMIN: 3809800c9408SRobert Watson 3810800c9408SRobert Watson /* 38110304c731SJamie Gritton * Jail operations within a jail work on child jails. 38120304c731SJamie Gritton */ 38130304c731SJamie Gritton case PRIV_JAIL_ATTACH: 38140304c731SJamie Gritton case PRIV_JAIL_SET: 38150304c731SJamie Gritton case PRIV_JAIL_REMOVE: 38160304c731SJamie Gritton 38170304c731SJamie Gritton /* 3818800c9408SRobert Watson * Jail implements its own inter-process limits, so allow 3819800c9408SRobert Watson * root processes in jail to change scheduling on other 3820800c9408SRobert Watson * processes in the same jail. Likewise for signalling. 3821800c9408SRobert Watson */ 3822800c9408SRobert Watson case PRIV_SCHED_DIFFCRED: 3823413628a7SBjoern A. Zeeb case PRIV_SCHED_CPUSET: 3824800c9408SRobert Watson case PRIV_SIGNAL_DIFFCRED: 3825800c9408SRobert Watson case PRIV_SIGNAL_SUGID: 3826800c9408SRobert Watson 3827800c9408SRobert Watson /* 3828800c9408SRobert Watson * Allow jailed processes to write to sysctls marked as jail 3829800c9408SRobert Watson * writable. 3830800c9408SRobert Watson */ 3831800c9408SRobert Watson case PRIV_SYSCTL_WRITEJAIL: 3832800c9408SRobert Watson 3833800c9408SRobert Watson /* 3834800c9408SRobert Watson * Allow root in jail to manage a variety of quota 3835e82d0201SRobert Watson * properties. These should likely be conditional on a 3836e82d0201SRobert Watson * configuration option. 3837800c9408SRobert Watson */ 383895b091d2SRobert Watson case PRIV_VFS_GETQUOTA: 383995b091d2SRobert Watson case PRIV_VFS_SETQUOTA: 3840800c9408SRobert Watson 3841800c9408SRobert Watson /* 3842800c9408SRobert Watson * Since Jail relies on chroot() to implement file system 3843800c9408SRobert Watson * protections, grant many VFS privileges to root in jail. 3844800c9408SRobert Watson * Be careful to exclude mount-related and NFS-related 3845800c9408SRobert Watson * privileges. 3846800c9408SRobert Watson */ 3847800c9408SRobert Watson case PRIV_VFS_READ: 3848800c9408SRobert Watson case PRIV_VFS_WRITE: 3849800c9408SRobert Watson case PRIV_VFS_ADMIN: 3850800c9408SRobert Watson case PRIV_VFS_EXEC: 3851800c9408SRobert Watson case PRIV_VFS_BLOCKRESERVE: /* XXXRW: Slightly surprising. */ 3852800c9408SRobert Watson case PRIV_VFS_CHFLAGS_DEV: 3853800c9408SRobert Watson case PRIV_VFS_CHOWN: 3854800c9408SRobert Watson case PRIV_VFS_CHROOT: 3855bb531912SPawel Jakub Dawidek case PRIV_VFS_RETAINSUGID: 3856800c9408SRobert Watson case PRIV_VFS_FCHROOT: 3857800c9408SRobert Watson case PRIV_VFS_LINK: 3858800c9408SRobert Watson case PRIV_VFS_SETGID: 3859e41966dcSRobert Watson case PRIV_VFS_STAT: 3860800c9408SRobert Watson case PRIV_VFS_STICKYFILE: 3861bb56d716SJamie Gritton 3862bb56d716SJamie Gritton /* 3863bb56d716SJamie Gritton * As in the non-jail case, non-root users are expected to be 386470de1003SGordon Bergling * able to read kernel/physical memory (provided /dev/[k]mem 3865bb56d716SJamie Gritton * exists in the jail and they have permission to access it). 3866bb56d716SJamie Gritton */ 3867bb56d716SJamie Gritton case PRIV_KMEM_READ: 3868800c9408SRobert Watson return (0); 3869800c9408SRobert Watson 3870800c9408SRobert Watson /* 3871800c9408SRobert Watson * Depending on the global setting, allow privilege of 3872800c9408SRobert Watson * setting system flags. 3873800c9408SRobert Watson */ 3874800c9408SRobert Watson case PRIV_VFS_SYSFLAGS: 38750304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_CHFLAGS) 3876800c9408SRobert Watson return (0); 3877800c9408SRobert Watson else 3878800c9408SRobert Watson return (EPERM); 3879800c9408SRobert Watson 3880800c9408SRobert Watson /* 3881f3a8d2f9SPawel Jakub Dawidek * Depending on the global setting, allow privilege of 3882f3a8d2f9SPawel Jakub Dawidek * mounting/unmounting file systems. 3883f3a8d2f9SPawel Jakub Dawidek */ 3884f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT: 3885f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_UNMOUNT: 3886f3a8d2f9SPawel Jakub Dawidek case PRIV_VFS_MOUNT_NONUSER: 388724b0502eSPawel Jakub Dawidek case PRIV_VFS_MOUNT_OWNER: 38880fe74ae6SJamie Gritton pr = cred->cr_prison; 38890fe74ae6SJamie Gritton prison_lock(pr); 38900fe74ae6SJamie Gritton if (pr->pr_allow & PR_ALLOW_MOUNT && pr->pr_enforce_statfs < 2) 38910fe74ae6SJamie Gritton error = 0; 3892f3a8d2f9SPawel Jakub Dawidek else 38930fe74ae6SJamie Gritton error = EPERM; 38940fe74ae6SJamie Gritton prison_unlock(pr); 38950fe74ae6SJamie Gritton return (error); 3896f3a8d2f9SPawel Jakub Dawidek 3897f3a8d2f9SPawel Jakub Dawidek /* 389863619b6dSKyle Evans * Jails should hold no disposition on the PRIV_VFS_READ_DIR 389963619b6dSKyle Evans * policy. priv_check_cred will not specifically allow it, and 390063619b6dSKyle Evans * we may want a MAC policy to allow it. 390163619b6dSKyle Evans */ 390263619b6dSKyle Evans case PRIV_VFS_READ_DIR: 390363619b6dSKyle Evans return (0); 390463619b6dSKyle Evans 390563619b6dSKyle Evans /* 3906ccd6ac9fSAntoine Brodin * Conditionnaly allow locking (unlocking) physical pages 3907ccd6ac9fSAntoine Brodin * in memory. 3908ccd6ac9fSAntoine Brodin */ 3909ccd6ac9fSAntoine Brodin case PRIV_VM_MLOCK: 3910ccd6ac9fSAntoine Brodin case PRIV_VM_MUNLOCK: 3911ccd6ac9fSAntoine Brodin if (cred->cr_prison->pr_allow & PR_ALLOW_MLOCK) 3912ccd6ac9fSAntoine Brodin return (0); 3913ccd6ac9fSAntoine Brodin else 3914ccd6ac9fSAntoine Brodin return (EPERM); 3915ccd6ac9fSAntoine Brodin 3916ccd6ac9fSAntoine Brodin /* 3917e28f9b7dSAllan Jude * Conditionally allow jailed root to bind reserved ports. 3918800c9408SRobert Watson */ 3919800c9408SRobert Watson case PRIV_NETINET_RESERVEDPORT: 3920e28f9b7dSAllan Jude if (cred->cr_prison->pr_allow & PR_ALLOW_RESERVED_PORTS) 3921e28f9b7dSAllan Jude return (0); 3922e28f9b7dSAllan Jude else 3923e28f9b7dSAllan Jude return (EPERM); 3924e28f9b7dSAllan Jude 3925e28f9b7dSAllan Jude /* 3926e28f9b7dSAllan Jude * Allow jailed root to reuse in-use ports. 3927e28f9b7dSAllan Jude */ 39284b084056SRobert Watson case PRIV_NETINET_REUSEPORT: 3929800c9408SRobert Watson return (0); 3930800c9408SRobert Watson 3931800c9408SRobert Watson /* 3932e3043798SPedro F. Giffuni * Allow jailed root to set certain IPv4/6 (option) headers. 393379ba3952SBjoern A. Zeeb */ 393479ba3952SBjoern A. Zeeb case PRIV_NETINET_SETHDROPTS: 393579ba3952SBjoern A. Zeeb return (0); 393679ba3952SBjoern A. Zeeb 393779ba3952SBjoern A. Zeeb /* 3938800c9408SRobert Watson * Conditionally allow creating raw sockets in jail. 3939800c9408SRobert Watson */ 3940800c9408SRobert Watson case PRIV_NETINET_RAW: 39410304c731SJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_RAW_SOCKETS) 3942800c9408SRobert Watson return (0); 3943800c9408SRobert Watson else 3944800c9408SRobert Watson return (EPERM); 3945800c9408SRobert Watson 3946800c9408SRobert Watson /* 3947800c9408SRobert Watson * Since jail implements its own visibility limits on netstat 3948800c9408SRobert Watson * sysctls, allow getcred. This allows identd to work in 3949800c9408SRobert Watson * jail. 3950800c9408SRobert Watson */ 3951800c9408SRobert Watson case PRIV_NETINET_GETCRED: 3952800c9408SRobert Watson return (0); 3953800c9408SRobert Watson 39542bfc50bcSEdward Tomasz Napierala /* 39552bfc50bcSEdward Tomasz Napierala * Allow jailed root to set loginclass. 39562bfc50bcSEdward Tomasz Napierala */ 39572bfc50bcSEdward Tomasz Napierala case PRIV_PROC_SETLOGINCLASS: 39582bfc50bcSEdward Tomasz Napierala return (0); 39592bfc50bcSEdward Tomasz Napierala 3960b19d66fdSJamie Gritton /* 39614520f617SJamie Gritton * Do not allow a process inside a jail to read the kernel 3962b19d66fdSJamie Gritton * message buffer unless explicitly permitted. 3963b19d66fdSJamie Gritton */ 3964b19d66fdSJamie Gritton case PRIV_MSGBUF: 3965b19d66fdSJamie Gritton if (cred->cr_prison->pr_allow & PR_ALLOW_READ_MSGBUF) 3966b19d66fdSJamie Gritton return (0); 3967b19d66fdSJamie Gritton return (EPERM); 3968b19d66fdSJamie Gritton 3969800c9408SRobert Watson default: 3970800c9408SRobert Watson /* 3971800c9408SRobert Watson * In all remaining cases, deny the privilege request. This 3972800c9408SRobert Watson * includes almost all network privileges, many system 3973800c9408SRobert Watson * configuration privileges. 3974800c9408SRobert Watson */ 3975800c9408SRobert Watson return (EPERM); 3976800c9408SRobert Watson } 3977800c9408SRobert Watson } 3978800c9408SRobert Watson 39790304c731SJamie Gritton /* 39800304c731SJamie Gritton * Return the part of pr2's name that is relative to pr1, or the whole name 39810304c731SJamie Gritton * if it does not directly follow. 39820304c731SJamie Gritton */ 39830304c731SJamie Gritton 39840304c731SJamie Gritton char * 39850304c731SJamie Gritton prison_name(struct prison *pr1, struct prison *pr2) 39860304c731SJamie Gritton { 39870304c731SJamie Gritton char *name; 39880304c731SJamie Gritton 39890304c731SJamie Gritton /* Jails see themselves as "0" (if they see themselves at all). */ 39900304c731SJamie Gritton if (pr1 == pr2) 39910304c731SJamie Gritton return "0"; 39920304c731SJamie Gritton name = pr2->pr_name; 39930304c731SJamie Gritton if (prison_ischild(pr1, pr2)) { 39940304c731SJamie Gritton /* 39950304c731SJamie Gritton * pr1 isn't locked (and allprison_lock may not be either) 39960304c731SJamie Gritton * so its length can't be counted on. But the number of dots 39970304c731SJamie Gritton * can be counted on - and counted. 39980304c731SJamie Gritton */ 39990304c731SJamie Gritton for (; pr1 != &prison0; pr1 = pr1->pr_parent) 40000304c731SJamie Gritton name = strchr(name, '.') + 1; 40010304c731SJamie Gritton } 40020304c731SJamie Gritton return (name); 40030304c731SJamie Gritton } 40040304c731SJamie Gritton 40050304c731SJamie Gritton /* 40060304c731SJamie Gritton * Return the part of pr2's path that is relative to pr1, or the whole path 40070304c731SJamie Gritton * if it does not directly follow. 40080304c731SJamie Gritton */ 40090304c731SJamie Gritton static char * 40100304c731SJamie Gritton prison_path(struct prison *pr1, struct prison *pr2) 40110304c731SJamie Gritton { 40120304c731SJamie Gritton char *path1, *path2; 40130304c731SJamie Gritton int len1; 40140304c731SJamie Gritton 40150304c731SJamie Gritton path1 = pr1->pr_path; 40160304c731SJamie Gritton path2 = pr2->pr_path; 40170304c731SJamie Gritton if (!strcmp(path1, "/")) 40180304c731SJamie Gritton return (path2); 40190304c731SJamie Gritton len1 = strlen(path1); 40200304c731SJamie Gritton if (strncmp(path1, path2, len1)) 40210304c731SJamie Gritton return (path2); 40220304c731SJamie Gritton if (path2[len1] == '\0') 40230304c731SJamie Gritton return "/"; 40240304c731SJamie Gritton if (path2[len1] == '/') 40250304c731SJamie Gritton return (path2 + len1); 40260304c731SJamie Gritton return (path2); 40270304c731SJamie Gritton } 40280304c731SJamie Gritton 40290304c731SJamie Gritton /* 40300304c731SJamie Gritton * Jail-related sysctls. 40310304c731SJamie Gritton */ 40327029da5cSPawel Biernacki static SYSCTL_NODE(_security, OID_AUTO, jail, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 40330304c731SJamie Gritton "Jails"); 40340304c731SJamie Gritton 4035*eb8dcdeaSGleb Smirnoff #if defined(INET) || defined(INET6) 4036*eb8dcdeaSGleb Smirnoff /* 4037*eb8dcdeaSGleb Smirnoff * Copy address array to memory that would be then SYSCTL_OUT-ed. 4038*eb8dcdeaSGleb Smirnoff * sysctl_jail_list() helper. 4039*eb8dcdeaSGleb Smirnoff */ 4040*eb8dcdeaSGleb Smirnoff static void 4041*eb8dcdeaSGleb Smirnoff prison_ip_copyout(struct prison *pr, const pr_family_t af, void **out, int *len) 4042*eb8dcdeaSGleb Smirnoff { 4043*eb8dcdeaSGleb Smirnoff const size_t size = pr_families[af].size; 4044*eb8dcdeaSGleb Smirnoff 4045*eb8dcdeaSGleb Smirnoff again: 4046*eb8dcdeaSGleb Smirnoff mtx_assert(&pr->pr_mtx, MA_OWNED); 4047*eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[af] != NULL) { 4048*eb8dcdeaSGleb Smirnoff if (*len < pr->pr_addrs[af]->ips) { 4049*eb8dcdeaSGleb Smirnoff *len = pr->pr_addrs[af]->ips; 4050*eb8dcdeaSGleb Smirnoff mtx_unlock(&pr->pr_mtx); 4051*eb8dcdeaSGleb Smirnoff *out = realloc(*out, *len * size, M_TEMP, M_WAITOK); 4052*eb8dcdeaSGleb Smirnoff mtx_lock(&pr->pr_mtx); 4053*eb8dcdeaSGleb Smirnoff goto again; 4054*eb8dcdeaSGleb Smirnoff } 4055*eb8dcdeaSGleb Smirnoff bcopy(pr->pr_addrs[af] + 1, *out, pr->pr_addrs[af]->ips * size); 4056*eb8dcdeaSGleb Smirnoff } 4057*eb8dcdeaSGleb Smirnoff } 4058*eb8dcdeaSGleb Smirnoff #endif 4059*eb8dcdeaSGleb Smirnoff 4060fd7a8150SMike Barcroft static int 4061fd7a8150SMike Barcroft sysctl_jail_list(SYSCTL_HANDLER_ARGS) 4062fd7a8150SMike Barcroft { 4063b38ff370SJamie Gritton struct xprison *xp; 40640304c731SJamie Gritton struct prison *pr, *cpr; 4065b38ff370SJamie Gritton #ifdef INET 4066b38ff370SJamie Gritton struct in_addr *ip4 = NULL; 4067b38ff370SJamie Gritton int ip4s = 0; 4068b38ff370SJamie Gritton #endif 4069b38ff370SJamie Gritton #ifdef INET6 40703beefaedSColin Percival struct in6_addr *ip6 = NULL; 4071b38ff370SJamie Gritton int ip6s = 0; 4072b38ff370SJamie Gritton #endif 40730304c731SJamie Gritton int descend, error; 4074fd7a8150SMike Barcroft 4075b38ff370SJamie Gritton xp = malloc(sizeof(*xp), M_TEMP, M_WAITOK); 40760304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 4077b38ff370SJamie Gritton error = 0; 4078dc68a633SPawel Jakub Dawidek sx_slock(&allprison_lock); 40790304c731SJamie Gritton FOREACH_PRISON_DESCENDANT(pr, cpr, descend) { 40800304c731SJamie Gritton mtx_lock(&cpr->pr_mtx); 4081413628a7SBjoern A. Zeeb #ifdef INET 4082*eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET, (void **)&ip4, &ip4s); 4083413628a7SBjoern A. Zeeb #endif 4084413628a7SBjoern A. Zeeb #ifdef INET6 4085*eb8dcdeaSGleb Smirnoff prison_ip_copyout(cpr, PR_INET6, (void **)&ip6, &ip6s); 4086b38ff370SJamie Gritton #endif 4087b38ff370SJamie Gritton bzero(xp, sizeof(*xp)); 4088fd7a8150SMike Barcroft xp->pr_version = XPRISON_VERSION; 40890304c731SJamie Gritton xp->pr_id = cpr->pr_id; 40901158508aSJamie Gritton xp->pr_state = cpr->pr_state; 40910304c731SJamie Gritton strlcpy(xp->pr_path, prison_path(pr, cpr), sizeof(xp->pr_path)); 4092c1f19219SJamie Gritton strlcpy(xp->pr_host, cpr->pr_hostname, sizeof(xp->pr_host)); 40930304c731SJamie Gritton strlcpy(xp->pr_name, prison_name(pr, cpr), sizeof(xp->pr_name)); 4094413628a7SBjoern A. Zeeb #ifdef INET 4095*eb8dcdeaSGleb Smirnoff xp->pr_ip4s = ip4s; 4096413628a7SBjoern A. Zeeb #endif 4097413628a7SBjoern A. Zeeb #ifdef INET6 4098*eb8dcdeaSGleb Smirnoff xp->pr_ip6s = ip6s; 4099413628a7SBjoern A. Zeeb #endif 41000304c731SJamie Gritton mtx_unlock(&cpr->pr_mtx); 4101b38ff370SJamie Gritton error = SYSCTL_OUT(req, xp, sizeof(*xp)); 4102b38ff370SJamie Gritton if (error) 4103b38ff370SJamie Gritton break; 4104413628a7SBjoern A. Zeeb #ifdef INET 4105b38ff370SJamie Gritton if (xp->pr_ip4s > 0) { 4106b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip4, 4107b38ff370SJamie Gritton xp->pr_ip4s * sizeof(struct in_addr)); 4108b38ff370SJamie Gritton if (error) 4109b38ff370SJamie Gritton break; 4110413628a7SBjoern A. Zeeb } 4111413628a7SBjoern A. Zeeb #endif 4112413628a7SBjoern A. Zeeb #ifdef INET6 4113b38ff370SJamie Gritton if (xp->pr_ip6s > 0) { 4114b38ff370SJamie Gritton error = SYSCTL_OUT(req, ip6, 4115b38ff370SJamie Gritton xp->pr_ip6s * sizeof(struct in6_addr)); 4116b38ff370SJamie Gritton if (error) 4117b38ff370SJamie Gritton break; 4118413628a7SBjoern A. Zeeb } 4119413628a7SBjoern A. Zeeb #endif 4120fd7a8150SMike Barcroft } 4121dc68a633SPawel Jakub Dawidek sx_sunlock(&allprison_lock); 4122b38ff370SJamie Gritton free(xp, M_TEMP); 4123b38ff370SJamie Gritton #ifdef INET 4124b38ff370SJamie Gritton free(ip4, M_TEMP); 4125b38ff370SJamie Gritton #endif 4126b38ff370SJamie Gritton #ifdef INET6 4127b38ff370SJamie Gritton free(ip6, M_TEMP); 4128b38ff370SJamie Gritton #endif 4129fd7a8150SMike Barcroft return (error); 4130fd7a8150SMike Barcroft } 4131fd7a8150SMike Barcroft 4132f3b86a5fSEd Schouten SYSCTL_OID(_security_jail, OID_AUTO, list, 4133f3b86a5fSEd Schouten CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4134f3b86a5fSEd Schouten sysctl_jail_list, "S", "List of active jails"); 4135461167c2SPawel Jakub Dawidek 4136461167c2SPawel Jakub Dawidek static int 4137461167c2SPawel Jakub Dawidek sysctl_jail_jailed(SYSCTL_HANDLER_ARGS) 4138461167c2SPawel Jakub Dawidek { 4139461167c2SPawel Jakub Dawidek int error, injail; 4140461167c2SPawel Jakub Dawidek 4141461167c2SPawel Jakub Dawidek injail = jailed(req->td->td_ucred); 4142461167c2SPawel Jakub Dawidek error = SYSCTL_OUT(req, &injail, sizeof(injail)); 4143461167c2SPawel Jakub Dawidek 4144461167c2SPawel Jakub Dawidek return (error); 4145461167c2SPawel Jakub Dawidek } 41460304c731SJamie Gritton 4147f3b86a5fSEd Schouten SYSCTL_PROC(_security_jail, OID_AUTO, jailed, 4148f3b86a5fSEd Schouten CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4149f3b86a5fSEd Schouten sysctl_jail_jailed, "I", "Process in jail?"); 4150413628a7SBjoern A. Zeeb 4151761d2bb5SJamie Gritton static int 4152761d2bb5SJamie Gritton sysctl_jail_vnet(SYSCTL_HANDLER_ARGS) 4153761d2bb5SJamie Gritton { 4154761d2bb5SJamie Gritton int error, havevnet; 4155761d2bb5SJamie Gritton #ifdef VIMAGE 4156761d2bb5SJamie Gritton struct ucred *cred = req->td->td_ucred; 4157761d2bb5SJamie Gritton 4158761d2bb5SJamie Gritton havevnet = jailed(cred) && prison_owns_vnet(cred); 4159761d2bb5SJamie Gritton #else 4160761d2bb5SJamie Gritton havevnet = 0; 4161761d2bb5SJamie Gritton #endif 4162761d2bb5SJamie Gritton error = SYSCTL_OUT(req, &havevnet, sizeof(havevnet)); 4163761d2bb5SJamie Gritton 4164761d2bb5SJamie Gritton return (error); 4165761d2bb5SJamie Gritton } 4166761d2bb5SJamie Gritton 4167761d2bb5SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, vnet, 4168761d2bb5SJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0, 4169bb8f1623SBjoern A. Zeeb sysctl_jail_vnet, "I", "Jail owns vnet?"); 4170761d2bb5SJamie Gritton 41710304c731SJamie Gritton #if defined(INET) || defined(INET6) 4172e92e0574SJamie Gritton SYSCTL_UINT(_security_jail, OID_AUTO, jail_max_af_ips, CTLFLAG_RW, 41730304c731SJamie Gritton &jail_max_af_ips, 0, 4174c542c43eSJamie Gritton "Number of IP addresses a jail may have at most per address family (deprecated)"); 41750304c731SJamie Gritton #endif 41760304c731SJamie Gritton 41770304c731SJamie Gritton /* 4178c542c43eSJamie Gritton * Default parameters for jail(2) compatibility. For historical reasons, 4179c542c43eSJamie Gritton * the sysctl names have varying similarity to the parameter names. Prisons 4180c542c43eSJamie Gritton * just see their own parameters, and can't change them. 41810304c731SJamie Gritton */ 41820304c731SJamie Gritton static int 41830304c731SJamie Gritton sysctl_jail_default_allow(SYSCTL_HANDLER_ARGS) 41840304c731SJamie Gritton { 41850fe74ae6SJamie Gritton int error, i; 41860304c731SJamie Gritton 41870304c731SJamie Gritton /* Get the current flag value, and convert it to a boolean. */ 41880fe74ae6SJamie Gritton if (req->td->td_ucred->cr_prison == &prison0) { 41890fe74ae6SJamie Gritton mtx_lock(&prison0.pr_mtx); 41900fe74ae6SJamie Gritton i = (jail_default_allow & arg2) != 0; 41910fe74ae6SJamie Gritton mtx_unlock(&prison0.pr_mtx); 41920fe74ae6SJamie Gritton } else 41930fe74ae6SJamie Gritton i = prison_allow(req->td->td_ucred, arg2); 41940fe74ae6SJamie Gritton 41950304c731SJamie Gritton if (arg1 != NULL) 41960304c731SJamie Gritton i = !i; 41970304c731SJamie Gritton error = sysctl_handle_int(oidp, &i, 0, req); 4198c542c43eSJamie Gritton if (error || !req->newptr) 41990304c731SJamie Gritton return (error); 42000304c731SJamie Gritton i = i ? arg2 : 0; 42010304c731SJamie Gritton if (arg1 != NULL) 42020304c731SJamie Gritton i ^= arg2; 42030304c731SJamie Gritton /* 42040304c731SJamie Gritton * The sysctls don't have CTLFLAGS_PRISON, so assume prison0 42050304c731SJamie Gritton * for writing. 42060304c731SJamie Gritton */ 42070304c731SJamie Gritton mtx_lock(&prison0.pr_mtx); 42080304c731SJamie Gritton jail_default_allow = (jail_default_allow & ~arg2) | i; 42090304c731SJamie Gritton mtx_unlock(&prison0.pr_mtx); 42100304c731SJamie Gritton return (0); 42110304c731SJamie Gritton } 42120304c731SJamie Gritton 42130304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, set_hostname_allowed, 4214c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42150304c731SJamie Gritton NULL, PR_ALLOW_SET_HOSTNAME, sysctl_jail_default_allow, "I", 4216c542c43eSJamie Gritton "Processes in jail can set their hostnames (deprecated)"); 42170304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, socket_unixiproute_only, 4218c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42190304c731SJamie Gritton (void *)1, PR_ALLOW_SOCKET_AF, sysctl_jail_default_allow, "I", 4220c542c43eSJamie Gritton "Processes in jail are limited to creating UNIX/IP/route sockets only (deprecated)"); 42210304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, sysvipc_allowed, 4222c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42230304c731SJamie Gritton NULL, PR_ALLOW_SYSVIPC, sysctl_jail_default_allow, "I", 4224c542c43eSJamie Gritton "Processes in jail can use System V IPC primitives (deprecated)"); 42250304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, allow_raw_sockets, 4226c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42270304c731SJamie Gritton NULL, PR_ALLOW_RAW_SOCKETS, sysctl_jail_default_allow, "I", 4228c542c43eSJamie Gritton "Prison root can create raw sockets (deprecated)"); 42290304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, chflags_allowed, 4230c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42310304c731SJamie Gritton NULL, PR_ALLOW_CHFLAGS, sysctl_jail_default_allow, "I", 4232c542c43eSJamie Gritton "Processes in jail can alter system file flags (deprecated)"); 42330304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, mount_allowed, 4234c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 42350304c731SJamie Gritton NULL, PR_ALLOW_MOUNT, sysctl_jail_default_allow, "I", 4236c542c43eSJamie Gritton "Processes in jail can mount/unmount jail-friendly file systems (deprecated)"); 42370304c731SJamie Gritton 42380304c731SJamie Gritton static int 42390304c731SJamie Gritton sysctl_jail_default_level(SYSCTL_HANDLER_ARGS) 42400304c731SJamie Gritton { 42410304c731SJamie Gritton struct prison *pr; 42420304c731SJamie Gritton int level, error; 42430304c731SJamie Gritton 42440304c731SJamie Gritton pr = req->td->td_ucred->cr_prison; 42450304c731SJamie Gritton level = (pr == &prison0) ? *(int *)arg1 : *(int *)((char *)pr + arg2); 42460304c731SJamie Gritton error = sysctl_handle_int(oidp, &level, 0, req); 4247c542c43eSJamie Gritton if (error || !req->newptr) 42480304c731SJamie Gritton return (error); 42490304c731SJamie Gritton *(int *)arg1 = level; 42500304c731SJamie Gritton return (0); 42510304c731SJamie Gritton } 42520304c731SJamie Gritton 42530304c731SJamie Gritton SYSCTL_PROC(_security_jail, OID_AUTO, enforce_statfs, 4254c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 4255c542c43eSJamie Gritton &jail_default_enforce_statfs, offsetof(struct prison, pr_enforce_statfs), 42560304c731SJamie Gritton sysctl_jail_default_level, "I", 4257c542c43eSJamie Gritton "Processes in jail cannot see all mounted file systems (deprecated)"); 4258c542c43eSJamie Gritton 42590cc207a6SMartin Matuska SYSCTL_PROC(_security_jail, OID_AUTO, devfs_ruleset, 4260c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, 4261c542c43eSJamie Gritton &jail_default_devfs_rsnum, offsetof(struct prison, pr_devfs_rsnum), 42620cc207a6SMartin Matuska sysctl_jail_default_level, "I", 4263c542c43eSJamie Gritton "Ruleset for the devfs filesystem in jail (deprecated)"); 42640cc207a6SMartin Matuska 42650304c731SJamie Gritton /* 42660304c731SJamie Gritton * Nodes to describe jail parameters. Maximum length of string parameters 42670304c731SJamie Gritton * is returned in the string itself, and the other parameters exist merely 42680304c731SJamie Gritton * to make themselves and their types known. 42690304c731SJamie Gritton */ 42707029da5cSPawel Biernacki SYSCTL_NODE(_security_jail, OID_AUTO, param, CTLFLAG_RW | CTLFLAG_MPSAFE, 0, 42710304c731SJamie Gritton "Jail parameters"); 42720304c731SJamie Gritton 42730304c731SJamie Gritton int 42740304c731SJamie Gritton sysctl_jail_param(SYSCTL_HANDLER_ARGS) 42750304c731SJamie Gritton { 42760304c731SJamie Gritton int i; 42770304c731SJamie Gritton long l; 42780304c731SJamie Gritton size_t s; 42790304c731SJamie Gritton char numbuf[12]; 42800304c731SJamie Gritton 42810304c731SJamie Gritton switch (oidp->oid_kind & CTLTYPE) 42820304c731SJamie Gritton { 42830304c731SJamie Gritton case CTLTYPE_LONG: 42840304c731SJamie Gritton case CTLTYPE_ULONG: 42850304c731SJamie Gritton l = 0; 42860304c731SJamie Gritton #ifdef SCTL_MASK32 42870304c731SJamie Gritton if (!(req->flags & SCTL_MASK32)) 42880304c731SJamie Gritton #endif 42890304c731SJamie Gritton return (SYSCTL_OUT(req, &l, sizeof(l))); 42900304c731SJamie Gritton case CTLTYPE_INT: 42910304c731SJamie Gritton case CTLTYPE_UINT: 42920304c731SJamie Gritton i = 0; 42930304c731SJamie Gritton return (SYSCTL_OUT(req, &i, sizeof(i))); 42940304c731SJamie Gritton case CTLTYPE_STRING: 4295e4cd31ddSJeff Roberson snprintf(numbuf, sizeof(numbuf), "%jd", (intmax_t)arg2); 42960304c731SJamie Gritton return 42970304c731SJamie Gritton (sysctl_handle_string(oidp, numbuf, sizeof(numbuf), req)); 42980304c731SJamie Gritton case CTLTYPE_STRUCT: 42990304c731SJamie Gritton s = (size_t)arg2; 43000304c731SJamie Gritton return (SYSCTL_OUT(req, &s, sizeof(s))); 43010304c731SJamie Gritton } 43020304c731SJamie Gritton return (0); 43030304c731SJamie Gritton } 43040304c731SJamie Gritton 4305b96bd95bSIan Lepore /* 4306b96bd95bSIan Lepore * CTLFLAG_RDTUN in the following indicates jail parameters that can be set at 4307b96bd95bSIan Lepore * jail creation time but cannot be changed in an existing jail. 4308b96bd95bSIan Lepore */ 43090304c731SJamie Gritton SYSCTL_JAIL_PARAM(, jid, CTLTYPE_INT | CTLFLAG_RDTUN, "I", "Jail ID"); 43100304c731SJamie Gritton SYSCTL_JAIL_PARAM(, parent, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail parent ID"); 43110304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, name, CTLFLAG_RW, MAXHOSTNAMELEN, "Jail name"); 43120304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(, path, CTLFLAG_RDTUN, MAXPATHLEN, "Jail root path"); 43130304c731SJamie Gritton SYSCTL_JAIL_PARAM(, securelevel, CTLTYPE_INT | CTLFLAG_RW, 43140304c731SJamie Gritton "I", "Jail secure level"); 4315b96bd95bSIan Lepore SYSCTL_JAIL_PARAM(, osreldate, CTLTYPE_INT | CTLFLAG_RDTUN, "I", 4316b96bd95bSIan Lepore "Jail value for kern.osreldate and uname -K"); 4317b96bd95bSIan Lepore SYSCTL_JAIL_PARAM_STRING(, osrelease, CTLFLAG_RDTUN, OSRELEASELEN, 4318b96bd95bSIan Lepore "Jail value for kern.osrelease and uname -r"); 43190304c731SJamie Gritton SYSCTL_JAIL_PARAM(, enforce_statfs, CTLTYPE_INT | CTLFLAG_RW, 43200304c731SJamie Gritton "I", "Jail cannot see all mounted file systems"); 43210cc207a6SMartin Matuska SYSCTL_JAIL_PARAM(, devfs_ruleset, CTLTYPE_INT | CTLFLAG_RW, 43220cc207a6SMartin Matuska "I", "Ruleset for in-jail devfs mounts"); 43230304c731SJamie Gritton SYSCTL_JAIL_PARAM(, persist, CTLTYPE_INT | CTLFLAG_RW, 43240304c731SJamie Gritton "B", "Jail persistence"); 4325679e1390SJamie Gritton #ifdef VIMAGE 4326679e1390SJamie Gritton SYSCTL_JAIL_PARAM(, vnet, CTLTYPE_INT | CTLFLAG_RDTUN, 43277cbf7213SJamie Gritton "E,jailsys", "Virtual network stack"); 4328679e1390SJamie Gritton #endif 43290304c731SJamie Gritton SYSCTL_JAIL_PARAM(, dying, CTLTYPE_INT | CTLFLAG_RD, 43300304c731SJamie Gritton "B", "Jail is in the process of shutting down"); 43310304c731SJamie Gritton 4332b97457e2SJamie Gritton SYSCTL_JAIL_PARAM_NODE(children, "Number of child jails"); 4333b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, cur, CTLTYPE_INT | CTLFLAG_RD, 4334b97457e2SJamie Gritton "I", "Current number of child jails"); 4335b97457e2SJamie Gritton SYSCTL_JAIL_PARAM(_children, max, CTLTYPE_INT | CTLFLAG_RW, 4336b97457e2SJamie Gritton "I", "Maximum number of child jails"); 4337b97457e2SJamie Gritton 43387cbf7213SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(host, CTLFLAG_RW, "Jail host info"); 43390304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostname, CTLFLAG_RW, MAXHOSTNAMELEN, 43400304c731SJamie Gritton "Jail hostname"); 434176ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, domainname, CTLFLAG_RW, MAXHOSTNAMELEN, 434276ca6f88SJamie Gritton "Jail NIS domainname"); 434376ca6f88SJamie Gritton SYSCTL_JAIL_PARAM_STRING(_host, hostuuid, CTLFLAG_RW, HOSTUUIDLEN, 434476ca6f88SJamie Gritton "Jail host UUID"); 434576ca6f88SJamie Gritton SYSCTL_JAIL_PARAM(_host, hostid, CTLTYPE_ULONG | CTLFLAG_RW, 434676ca6f88SJamie Gritton "LU", "Jail host ID"); 43470304c731SJamie Gritton 43480304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(cpuset, "Jail cpuset"); 43490304c731SJamie Gritton SYSCTL_JAIL_PARAM(_cpuset, id, CTLTYPE_INT | CTLFLAG_RD, "I", "Jail cpuset ID"); 43500304c731SJamie Gritton 43510304c731SJamie Gritton #ifdef INET 43522b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip4, CTLFLAG_RDTUN, 43532b0d6f81SJamie Gritton "Jail IPv4 address virtualization"); 43540304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip4, addr, CTLFLAG_RW, sizeof(struct in_addr), 43550304c731SJamie Gritton "S,in_addr,a", "Jail IPv4 addresses"); 4356592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip4, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4357592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv4 source address selection rather than the " 4358592bcae8SBjoern A. Zeeb "primary jail IPv4 address."); 43590304c731SJamie Gritton #endif 43600304c731SJamie Gritton #ifdef INET6 43612b0d6f81SJamie Gritton SYSCTL_JAIL_PARAM_SYS_NODE(ip6, CTLFLAG_RDTUN, 43622b0d6f81SJamie Gritton "Jail IPv6 address virtualization"); 43630304c731SJamie Gritton SYSCTL_JAIL_PARAM_STRUCT(_ip6, addr, CTLFLAG_RW, sizeof(struct in6_addr), 43640304c731SJamie Gritton "S,in6_addr,a", "Jail IPv6 addresses"); 4365592bcae8SBjoern A. Zeeb SYSCTL_JAIL_PARAM(_ip6, saddrsel, CTLTYPE_INT | CTLFLAG_RW, 4366592bcae8SBjoern A. Zeeb "B", "Do (not) use IPv6 source address selection rather than the " 4367592bcae8SBjoern A. Zeeb "primary jail IPv6 address."); 43680304c731SJamie Gritton #endif 43690304c731SJamie Gritton 43700304c731SJamie Gritton SYSCTL_JAIL_PARAM_NODE(allow, "Jail permission flags"); 43710304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, set_hostname, CTLTYPE_INT | CTLFLAG_RW, 43720304c731SJamie Gritton "B", "Jail may set hostname"); 43730304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, sysvipc, CTLTYPE_INT | CTLFLAG_RW, 43740304c731SJamie Gritton "B", "Jail may use SYSV IPC"); 43750304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, raw_sockets, CTLTYPE_INT | CTLFLAG_RW, 43760304c731SJamie Gritton "B", "Jail may create raw sockets"); 43770304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, chflags, CTLTYPE_INT | CTLFLAG_RW, 43780304c731SJamie Gritton "B", "Jail may alter system file flags"); 43790304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, quotas, CTLTYPE_INT | CTLFLAG_RW, 43800304c731SJamie Gritton "B", "Jail may set file quotas"); 43810304c731SJamie Gritton SYSCTL_JAIL_PARAM(_allow, socket_af, CTLTYPE_INT | CTLFLAG_RW, 43820304c731SJamie Gritton "B", "Jail may create sockets other than just UNIX/IPv4/IPv6/route"); 4383ccd6ac9fSAntoine Brodin SYSCTL_JAIL_PARAM(_allow, mlock, CTLTYPE_INT | CTLFLAG_RW, 4384ccd6ac9fSAntoine Brodin "B", "Jail may lock (unlock) physical pages in memory"); 4385e28f9b7dSAllan Jude SYSCTL_JAIL_PARAM(_allow, reserved_ports, CTLTYPE_INT | CTLFLAG_RW, 4386e28f9b7dSAllan Jude "B", "Jail may bind sockets to reserved ports"); 4387b19d66fdSJamie Gritton SYSCTL_JAIL_PARAM(_allow, read_msgbuf, CTLTYPE_INT | CTLFLAG_RW, 4388b19d66fdSJamie Gritton "B", "Jail may read the kernel message buffer"); 4389b3079544SJamie Gritton SYSCTL_JAIL_PARAM(_allow, unprivileged_proc_debug, CTLTYPE_INT | CTLFLAG_RW, 4390b3079544SJamie Gritton "B", "Unprivileged processes may use process debugging facilities"); 439105e1e482SMariusz Zaborski SYSCTL_JAIL_PARAM(_allow, suser, CTLTYPE_INT | CTLFLAG_RW, 439205e1e482SMariusz Zaborski "B", "Processes in jail with uid 0 have privilege"); 43930304c731SJamie Gritton 4394bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM_SUBNODE(allow, mount, "Jail mount/unmount permission flags"); 4395bf3db8aaSMartin Matuska SYSCTL_JAIL_PARAM(_allow_mount, , CTLTYPE_INT | CTLFLAG_RW, 4396bf3db8aaSMartin Matuska "B", "Jail may mount/unmount jail-friendly file systems in general"); 43970e5c6bd4SJamie Gritton 43980e5c6bd4SJamie Gritton /* 43990a172404SJamie Gritton * Add a dynamic parameter allow.<name>, or allow.<prefix>.<name>. Return 44000a172404SJamie Gritton * its associated bit in the pr_allow bitmask, or zero if the parameter was 44010a172404SJamie Gritton * not created. 44020e5c6bd4SJamie Gritton */ 44030a172404SJamie Gritton unsigned 44040a172404SJamie Gritton prison_add_allow(const char *prefix, const char *name, const char *prefix_descr, 44050a172404SJamie Gritton const char *descr) 44060e5c6bd4SJamie Gritton { 44070e5c6bd4SJamie Gritton struct bool_flags *bf; 44080a172404SJamie Gritton struct sysctl_oid *parent; 44090a172404SJamie Gritton char *allow_name, *allow_noname, *allowed; 4410c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4411c542c43eSJamie Gritton char *descr_deprecated; 4412c542c43eSJamie Gritton #endif 44135d58f959SJamie Gritton u_int allow_flag; 44140e5c6bd4SJamie Gritton 44150a172404SJamie Gritton if (prefix 44160a172404SJamie Gritton ? asprintf(&allow_name, M_PRISON, "allow.%s.%s", prefix, name) 44170a172404SJamie Gritton < 0 || 44180a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.%s.no%s", prefix, name) 44190a172404SJamie Gritton < 0 44200a172404SJamie Gritton : asprintf(&allow_name, M_PRISON, "allow.%s", name) < 0 || 44210a172404SJamie Gritton asprintf(&allow_noname, M_PRISON, "allow.no%s", name) < 0) { 44220e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 44230a172404SJamie Gritton return 0; 44240e5c6bd4SJamie Gritton } 44250e5c6bd4SJamie Gritton 44260e5c6bd4SJamie Gritton /* 44270a172404SJamie Gritton * See if this parameter has already beed added, i.e. a module was 44280a172404SJamie Gritton * previously loaded/unloaded. 44290e5c6bd4SJamie Gritton */ 44300e5c6bd4SJamie Gritton mtx_lock(&prison0.pr_mtx); 44310e5c6bd4SJamie Gritton for (bf = pr_flag_allow; 44325d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 44335d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 44340e5c6bd4SJamie Gritton bf++) { 44350e5c6bd4SJamie Gritton if (strcmp(bf->name, allow_name) == 0) { 44360a172404SJamie Gritton allow_flag = bf->flag; 44370e5c6bd4SJamie Gritton goto no_add; 44380e5c6bd4SJamie Gritton } 44390e5c6bd4SJamie Gritton } 44400e5c6bd4SJamie Gritton 44410e5c6bd4SJamie Gritton /* 44425d58f959SJamie Gritton * Find a free bit in pr_allow_all, failing if there are none 44430e5c6bd4SJamie Gritton * (which shouldn't happen as long as we keep track of how many 44440a172404SJamie Gritton * potential dynamic flags exist). 44450e5c6bd4SJamie Gritton */ 44460e5c6bd4SJamie Gritton for (allow_flag = 1;; allow_flag <<= 1) { 44470e5c6bd4SJamie Gritton if (allow_flag == 0) 44480e5c6bd4SJamie Gritton goto no_add; 44495d58f959SJamie Gritton if ((pr_allow_all & allow_flag) == 0) 44500e5c6bd4SJamie Gritton break; 44510e5c6bd4SJamie Gritton } 44520e5c6bd4SJamie Gritton 44535d58f959SJamie Gritton /* Note the parameter in the next open slot in pr_flag_allow. */ 44545d58f959SJamie Gritton for (bf = pr_flag_allow; ; bf++) { 44550e5c6bd4SJamie Gritton if (bf == pr_flag_allow + nitems(pr_flag_allow)) { 44560e5c6bd4SJamie Gritton /* This should never happen, but is not fatal. */ 44570a172404SJamie Gritton allow_flag = 0; 44580e5c6bd4SJamie Gritton goto no_add; 44590e5c6bd4SJamie Gritton } 44605d58f959SJamie Gritton if (atomic_load_int(&bf->flag) == 0) 44615d58f959SJamie Gritton break; 44625d58f959SJamie Gritton } 44630e5c6bd4SJamie Gritton bf->name = allow_name; 44640e5c6bd4SJamie Gritton bf->noname = allow_noname; 44655d58f959SJamie Gritton pr_allow_all |= allow_flag; 44665d58f959SJamie Gritton /* 44675d58f959SJamie Gritton * prison0 always has permission for the new parameter. 44685d58f959SJamie Gritton * Other jails must have it granted to them. 44695d58f959SJamie Gritton */ 44705d58f959SJamie Gritton prison0.pr_allow |= allow_flag; 44715d58f959SJamie Gritton /* The flag indicates a valid entry, so make sure it is set last. */ 44725d58f959SJamie Gritton atomic_store_rel_int(&bf->flag, allow_flag); 44730e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 44740e5c6bd4SJamie Gritton 4475c542c43eSJamie Gritton /* 4476c542c43eSJamie Gritton * Create sysctls for the paramter, and the back-compat global 4477c542c43eSJamie Gritton * permission. 4478c542c43eSJamie Gritton */ 44790a172404SJamie Gritton parent = prefix 44800a172404SJamie Gritton ? SYSCTL_ADD_NODE(NULL, 44810a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail_param_allow), 44827029da5cSPawel Biernacki OID_AUTO, prefix, CTLFLAG_MPSAFE, 0, prefix_descr) 44830a172404SJamie Gritton : &sysctl___security_jail_param_allow; 44840a172404SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, SYSCTL_CHILDREN(parent), OID_AUTO, 44850a172404SJamie Gritton name, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, 44860e5c6bd4SJamie Gritton NULL, 0, sysctl_jail_param, "B", descr); 44870a172404SJamie Gritton if ((prefix 44880a172404SJamie Gritton ? asprintf(&allowed, M_TEMP, "%s_%s_allowed", prefix, name) 44890a172404SJamie Gritton : asprintf(&allowed, M_TEMP, "%s_allowed", name)) >= 0) { 4490c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4491c542c43eSJamie Gritton (void)asprintf(&descr_deprecated, M_TEMP, "%s (deprecated)", 4492c542c43eSJamie Gritton descr); 4493c542c43eSJamie Gritton #endif 44940e5c6bd4SJamie Gritton (void)SYSCTL_ADD_PROC(NULL, 44950a172404SJamie Gritton SYSCTL_CHILDREN(&sysctl___security_jail), OID_AUTO, allowed, 4496c542c43eSJamie Gritton CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, NULL, allow_flag, 4497c542c43eSJamie Gritton sysctl_jail_default_allow, "I", descr_deprecated); 4498c542c43eSJamie Gritton #ifndef NO_SYSCTL_DESCR 4499c542c43eSJamie Gritton free(descr_deprecated, M_TEMP); 4500c542c43eSJamie Gritton #endif 45010a172404SJamie Gritton free(allowed, M_TEMP); 45020e5c6bd4SJamie Gritton } 45030a172404SJamie Gritton return allow_flag; 45040e5c6bd4SJamie Gritton 45050e5c6bd4SJamie Gritton no_add: 45060e5c6bd4SJamie Gritton mtx_unlock(&prison0.pr_mtx); 45070e5c6bd4SJamie Gritton free(allow_name, M_PRISON); 45080e5c6bd4SJamie Gritton free(allow_noname, M_PRISON); 45090a172404SJamie Gritton return allow_flag; 45100a172404SJamie Gritton } 45110a172404SJamie Gritton 45120a172404SJamie Gritton /* 45130a172404SJamie Gritton * The VFS system will register jail-aware filesystems here. They each get 45140a172404SJamie Gritton * a parameter allow.mount.xxxfs and a flag to check when a jailed user 45150a172404SJamie Gritton * attempts to mount. 45160a172404SJamie Gritton */ 45170a172404SJamie Gritton void 45180a172404SJamie Gritton prison_add_vfs(struct vfsconf *vfsp) 45190a172404SJamie Gritton { 45200a172404SJamie Gritton #ifdef NO_SYSCTL_DESCR 45210a172404SJamie Gritton 45220a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 45230a172404SJamie Gritton NULL, NULL); 45240a172404SJamie Gritton #else 45250a172404SJamie Gritton char *descr; 45260a172404SJamie Gritton 45270a172404SJamie Gritton (void)asprintf(&descr, M_TEMP, "Jail may mount the %s file system", 45280a172404SJamie Gritton vfsp->vfc_name); 45290a172404SJamie Gritton vfsp->vfc_prison_flag = prison_add_allow("mount", vfsp->vfc_name, 45300a172404SJamie Gritton NULL, descr); 45310a172404SJamie Gritton free(descr, M_TEMP); 45320a172404SJamie Gritton #endif 45330e5c6bd4SJamie Gritton } 4534bf3db8aaSMartin Matuska 45354b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4536097055e2SEdward Tomasz Napierala void 4537097055e2SEdward Tomasz Napierala prison_racct_foreach(void (*callback)(struct racct *racct, 453815db3c07SEdward Tomasz Napierala void *arg2, void *arg3), void (*pre)(void), void (*post)(void), 453915db3c07SEdward Tomasz Napierala void *arg2, void *arg3) 4540097055e2SEdward Tomasz Napierala { 4541a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4542097055e2SEdward Tomasz Napierala 45434b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 45444b5c9cf6SEdward Tomasz Napierala 4545097055e2SEdward Tomasz Napierala sx_slock(&allprison_lock); 454615db3c07SEdward Tomasz Napierala if (pre != NULL) 454715db3c07SEdward Tomasz Napierala (pre)(); 4548a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) 4549a7ad07bfSEdward Tomasz Napierala (callback)(prr->prr_racct, arg2, arg3); 455015db3c07SEdward Tomasz Napierala if (post != NULL) 455115db3c07SEdward Tomasz Napierala (post)(); 4552097055e2SEdward Tomasz Napierala sx_sunlock(&allprison_lock); 4553097055e2SEdward Tomasz Napierala } 45540304c731SJamie Gritton 4555a7ad07bfSEdward Tomasz Napierala static struct prison_racct * 4556a7ad07bfSEdward Tomasz Napierala prison_racct_find_locked(const char *name) 4557a7ad07bfSEdward Tomasz Napierala { 4558a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4559a7ad07bfSEdward Tomasz Napierala 45604b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4561a7ad07bfSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4562a7ad07bfSEdward Tomasz Napierala 4563a7ad07bfSEdward Tomasz Napierala if (name[0] == '\0' || strlen(name) >= MAXHOSTNAMELEN) 4564a7ad07bfSEdward Tomasz Napierala return (NULL); 4565a7ad07bfSEdward Tomasz Napierala 4566a7ad07bfSEdward Tomasz Napierala LIST_FOREACH(prr, &allprison_racct, prr_next) { 4567a7ad07bfSEdward Tomasz Napierala if (strcmp(name, prr->prr_name) != 0) 4568a7ad07bfSEdward Tomasz Napierala continue; 4569a7ad07bfSEdward Tomasz Napierala 4570a7ad07bfSEdward Tomasz Napierala /* Found prison_racct with a matching name? */ 4571a7ad07bfSEdward Tomasz Napierala prison_racct_hold(prr); 4572a7ad07bfSEdward Tomasz Napierala return (prr); 4573a7ad07bfSEdward Tomasz Napierala } 4574a7ad07bfSEdward Tomasz Napierala 4575a7ad07bfSEdward Tomasz Napierala /* Add new prison_racct. */ 4576a7ad07bfSEdward Tomasz Napierala prr = malloc(sizeof(*prr), M_PRISON_RACCT, M_ZERO | M_WAITOK); 4577a7ad07bfSEdward Tomasz Napierala racct_create(&prr->prr_racct); 4578a7ad07bfSEdward Tomasz Napierala 4579a7ad07bfSEdward Tomasz Napierala strcpy(prr->prr_name, name); 4580a7ad07bfSEdward Tomasz Napierala refcount_init(&prr->prr_refcount, 1); 4581a7ad07bfSEdward Tomasz Napierala LIST_INSERT_HEAD(&allprison_racct, prr, prr_next); 4582a7ad07bfSEdward Tomasz Napierala 4583a7ad07bfSEdward Tomasz Napierala return (prr); 4584a7ad07bfSEdward Tomasz Napierala } 4585a7ad07bfSEdward Tomasz Napierala 4586a7ad07bfSEdward Tomasz Napierala struct prison_racct * 4587a7ad07bfSEdward Tomasz Napierala prison_racct_find(const char *name) 4588a7ad07bfSEdward Tomasz Napierala { 4589a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4590a7ad07bfSEdward Tomasz Napierala 45914b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 45924b5c9cf6SEdward Tomasz Napierala 4593a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4594a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(name); 4595a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4596a7ad07bfSEdward Tomasz Napierala return (prr); 4597a7ad07bfSEdward Tomasz Napierala } 4598a7ad07bfSEdward Tomasz Napierala 4599a7ad07bfSEdward Tomasz Napierala void 4600a7ad07bfSEdward Tomasz Napierala prison_racct_hold(struct prison_racct *prr) 4601a7ad07bfSEdward Tomasz Napierala { 4602a7ad07bfSEdward Tomasz Napierala 46034b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46044b5c9cf6SEdward Tomasz Napierala 4605a7ad07bfSEdward Tomasz Napierala refcount_acquire(&prr->prr_refcount); 4606a7ad07bfSEdward Tomasz Napierala } 4607a7ad07bfSEdward Tomasz Napierala 4608c34bbd2aSEdward Tomasz Napierala static void 4609c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(struct prison_racct *prr) 4610c34bbd2aSEdward Tomasz Napierala { 4611c34bbd2aSEdward Tomasz Napierala 46124b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4613c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4614c34bbd2aSEdward Tomasz Napierala 4615c34bbd2aSEdward Tomasz Napierala if (refcount_release(&prr->prr_refcount)) { 4616c34bbd2aSEdward Tomasz Napierala racct_destroy(&prr->prr_racct); 4617c34bbd2aSEdward Tomasz Napierala LIST_REMOVE(prr, prr_next); 4618c34bbd2aSEdward Tomasz Napierala free(prr, M_PRISON_RACCT); 4619c34bbd2aSEdward Tomasz Napierala } 4620c34bbd2aSEdward Tomasz Napierala } 4621c34bbd2aSEdward Tomasz Napierala 4622a7ad07bfSEdward Tomasz Napierala void 4623a7ad07bfSEdward Tomasz Napierala prison_racct_free(struct prison_racct *prr) 4624a7ad07bfSEdward Tomasz Napierala { 4625a7ad07bfSEdward Tomasz Napierala 46264b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4627c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4628c34bbd2aSEdward Tomasz Napierala 46296ff4688bSMateusz Guzik if (refcount_release_if_not_last(&prr->prr_refcount)) 4630a7ad07bfSEdward Tomasz Napierala return; 4631a7ad07bfSEdward Tomasz Napierala 4632a7ad07bfSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4633c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(prr); 4634a7ad07bfSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4635a7ad07bfSEdward Tomasz Napierala } 4636a7ad07bfSEdward Tomasz Napierala 4637a7ad07bfSEdward Tomasz Napierala static void 4638a7ad07bfSEdward Tomasz Napierala prison_racct_attach(struct prison *pr) 4639a7ad07bfSEdward Tomasz Napierala { 4640a7ad07bfSEdward Tomasz Napierala struct prison_racct *prr; 4641a7ad07bfSEdward Tomasz Napierala 46424b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4643c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_XLOCKED); 4644c34bbd2aSEdward Tomasz Napierala 4645a7ad07bfSEdward Tomasz Napierala prr = prison_racct_find_locked(pr->pr_name); 4646a7ad07bfSEdward Tomasz Napierala KASSERT(prr != NULL, ("cannot find prison_racct")); 4647a7ad07bfSEdward Tomasz Napierala 4648a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = prr; 4649a7ad07bfSEdward Tomasz Napierala } 4650a7ad07bfSEdward Tomasz Napierala 4651c34bbd2aSEdward Tomasz Napierala /* 4652c34bbd2aSEdward Tomasz Napierala * Handle jail renaming. From the racct point of view, renaming means 4653c34bbd2aSEdward Tomasz Napierala * moving from one prison_racct to another. 4654c34bbd2aSEdward Tomasz Napierala */ 4655c34bbd2aSEdward Tomasz Napierala static void 4656c34bbd2aSEdward Tomasz Napierala prison_racct_modify(struct prison *pr) 4657c34bbd2aSEdward Tomasz Napierala { 4658dbadb015SKonstantin Belousov #ifdef RCTL 4659c34bbd2aSEdward Tomasz Napierala struct proc *p; 4660c34bbd2aSEdward Tomasz Napierala struct ucred *cred; 4661dbadb015SKonstantin Belousov #endif 4662c34bbd2aSEdward Tomasz Napierala struct prison_racct *oldprr; 4663c34bbd2aSEdward Tomasz Napierala 46644b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 46654b5c9cf6SEdward Tomasz Napierala 4666c34bbd2aSEdward Tomasz Napierala sx_slock(&allproc_lock); 4667c34bbd2aSEdward Tomasz Napierala sx_xlock(&allprison_lock); 4668c34bbd2aSEdward Tomasz Napierala 4669e30345e7SEdward Tomasz Napierala if (strcmp(pr->pr_name, pr->pr_prison_racct->prr_name) == 0) { 4670e30345e7SEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4671e30345e7SEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4672c34bbd2aSEdward Tomasz Napierala return; 4673e30345e7SEdward Tomasz Napierala } 4674c34bbd2aSEdward Tomasz Napierala 4675c34bbd2aSEdward Tomasz Napierala oldprr = pr->pr_prison_racct; 4676c34bbd2aSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4677c34bbd2aSEdward Tomasz Napierala 4678c34bbd2aSEdward Tomasz Napierala prison_racct_attach(pr); 4679c34bbd2aSEdward Tomasz Napierala 4680c34bbd2aSEdward Tomasz Napierala /* 4681c34bbd2aSEdward Tomasz Napierala * Move resource utilisation records. 4682c34bbd2aSEdward Tomasz Napierala */ 4683c34bbd2aSEdward Tomasz Napierala racct_move(pr->pr_prison_racct->prr_racct, oldprr->prr_racct); 4684c34bbd2aSEdward Tomasz Napierala 4685f87beb93SAndriy Gapon #ifdef RCTL 4686c34bbd2aSEdward Tomasz Napierala /* 4687c34bbd2aSEdward Tomasz Napierala * Force rctl to reattach rules to processes. 4688c34bbd2aSEdward Tomasz Napierala */ 4689c34bbd2aSEdward Tomasz Napierala FOREACH_PROC_IN_SYSTEM(p) { 4690c34bbd2aSEdward Tomasz Napierala PROC_LOCK(p); 4691c34bbd2aSEdward Tomasz Napierala cred = crhold(p->p_ucred); 4692c34bbd2aSEdward Tomasz Napierala PROC_UNLOCK(p); 4693f87beb93SAndriy Gapon rctl_proc_ucred_changed(p, cred); 4694c34bbd2aSEdward Tomasz Napierala crfree(cred); 4695c34bbd2aSEdward Tomasz Napierala } 4696f87beb93SAndriy Gapon #endif 4697c34bbd2aSEdward Tomasz Napierala 4698c34bbd2aSEdward Tomasz Napierala sx_sunlock(&allproc_lock); 4699c34bbd2aSEdward Tomasz Napierala prison_racct_free_locked(oldprr); 4700c34bbd2aSEdward Tomasz Napierala sx_xunlock(&allprison_lock); 4701c34bbd2aSEdward Tomasz Napierala } 4702c34bbd2aSEdward Tomasz Napierala 4703a7ad07bfSEdward Tomasz Napierala static void 4704a7ad07bfSEdward Tomasz Napierala prison_racct_detach(struct prison *pr) 4705a7ad07bfSEdward Tomasz Napierala { 4706c34bbd2aSEdward Tomasz Napierala 47074b5c9cf6SEdward Tomasz Napierala ASSERT_RACCT_ENABLED(); 4708c34bbd2aSEdward Tomasz Napierala sx_assert(&allprison_lock, SA_UNLOCKED); 4709c34bbd2aSEdward Tomasz Napierala 4710af3c786cSMateusz Guzik if (pr->pr_prison_racct == NULL) 4711af3c786cSMateusz Guzik return; 4712a7ad07bfSEdward Tomasz Napierala prison_racct_free(pr->pr_prison_racct); 4713a7ad07bfSEdward Tomasz Napierala pr->pr_prison_racct = NULL; 4714a7ad07bfSEdward Tomasz Napierala } 4715a7ad07bfSEdward Tomasz Napierala #endif /* RACCT */ 4716a7ad07bfSEdward Tomasz Napierala 4717413628a7SBjoern A. Zeeb #ifdef DDB 4718b38ff370SJamie Gritton 4719b38ff370SJamie Gritton static void 4720b38ff370SJamie Gritton db_show_prison(struct prison *pr) 4721413628a7SBjoern A. Zeeb { 4722672756aaSJamie Gritton struct bool_flags *bf; 4723672756aaSJamie Gritton struct jailsys_flags *jsf; 4724b38ff370SJamie Gritton #if defined(INET) || defined(INET6) 4725b38ff370SJamie Gritton int ii; 4726413628a7SBjoern A. Zeeb #endif 4727672756aaSJamie Gritton unsigned f; 47288144690aSEric van Gyzen #ifdef INET 47298144690aSEric van Gyzen char ip4buf[INET_ADDRSTRLEN]; 47308144690aSEric van Gyzen #endif 4731413628a7SBjoern A. Zeeb #ifdef INET6 4732413628a7SBjoern A. Zeeb char ip6buf[INET6_ADDRSTRLEN]; 4733413628a7SBjoern A. Zeeb #endif 4734413628a7SBjoern A. Zeeb 4735b38ff370SJamie Gritton db_printf("prison %p:\n", pr); 4736b38ff370SJamie Gritton db_printf(" jid = %d\n", pr->pr_id); 4737b38ff370SJamie Gritton db_printf(" name = %s\n", pr->pr_name); 47380304c731SJamie Gritton db_printf(" parent = %p\n", pr->pr_parent); 4739b38ff370SJamie Gritton db_printf(" ref = %d\n", pr->pr_ref); 4740b38ff370SJamie Gritton db_printf(" uref = %d\n", pr->pr_uref); 47411158508aSJamie Gritton db_printf(" state = %s\n", 47421158508aSJamie Gritton pr->pr_state == PRISON_STATE_ALIVE ? "alive" : 47431158508aSJamie Gritton pr->pr_state == PRISON_STATE_DYING ? "dying" : 47441158508aSJamie Gritton "invalid"); 4745b38ff370SJamie Gritton db_printf(" path = %s\n", pr->pr_path); 4746b38ff370SJamie Gritton db_printf(" cpuset = %d\n", pr->pr_cpuset 4747b38ff370SJamie Gritton ? pr->pr_cpuset->cs_id : -1); 4748679e1390SJamie Gritton #ifdef VIMAGE 4749679e1390SJamie Gritton db_printf(" vnet = %p\n", pr->pr_vnet); 4750679e1390SJamie Gritton #endif 4751b38ff370SJamie Gritton db_printf(" root = %p\n", pr->pr_root); 4752b38ff370SJamie Gritton db_printf(" securelevel = %d\n", pr->pr_securelevel); 47530cc207a6SMartin Matuska db_printf(" devfs_rsnum = %d\n", pr->pr_devfs_rsnum); 4754fe0518e9SBjoern A. Zeeb db_printf(" children.max = %d\n", pr->pr_childmax); 4755fe0518e9SBjoern A. Zeeb db_printf(" children.cur = %d\n", pr->pr_childcount); 47560304c731SJamie Gritton db_printf(" child = %p\n", LIST_FIRST(&pr->pr_children)); 47570304c731SJamie Gritton db_printf(" sibling = %p\n", LIST_NEXT(pr, pr_sibling)); 4758fe0518e9SBjoern A. Zeeb db_printf(" flags = 0x%x", pr->pr_flags); 4759672756aaSJamie Gritton for (bf = pr_flag_bool; bf < pr_flag_bool + nitems(pr_flag_bool); bf++) 4760672756aaSJamie Gritton if (pr->pr_flags & bf->flag) 4761672756aaSJamie Gritton db_printf(" %s", bf->name); 4762672756aaSJamie Gritton for (jsf = pr_flag_jailsys; 4763672756aaSJamie Gritton jsf < pr_flag_jailsys + nitems(pr_flag_jailsys); 4764672756aaSJamie Gritton jsf++) { 4765672756aaSJamie Gritton f = pr->pr_flags & (jsf->disable | jsf->new); 4766672756aaSJamie Gritton db_printf(" %-16s= %s\n", jsf->name, 4767672756aaSJamie Gritton (f != 0 && f == jsf->disable) ? "disable" 4768672756aaSJamie Gritton : (f == jsf->new) ? "new" 47697cbf7213SJamie Gritton : "inherit"); 47707cbf7213SJamie Gritton } 4771fe0518e9SBjoern A. Zeeb db_printf(" allow = 0x%x", pr->pr_allow); 4772672756aaSJamie Gritton for (bf = pr_flag_allow; 47735d58f959SJamie Gritton bf < pr_flag_allow + nitems(pr_flag_allow) && 47745d58f959SJamie Gritton atomic_load_int(&bf->flag) != 0; 4775672756aaSJamie Gritton bf++) 4776672756aaSJamie Gritton if (pr->pr_allow & bf->flag) 4777672756aaSJamie Gritton db_printf(" %s", bf->name); 4778b38ff370SJamie Gritton db_printf("\n"); 47790304c731SJamie Gritton db_printf(" enforce_statfs = %d\n", pr->pr_enforce_statfs); 4780c1f19219SJamie Gritton db_printf(" host.hostname = %s\n", pr->pr_hostname); 4781c1f19219SJamie Gritton db_printf(" host.domainname = %s\n", pr->pr_domainname); 4782c1f19219SJamie Gritton db_printf(" host.hostuuid = %s\n", pr->pr_hostuuid); 478376ca6f88SJamie Gritton db_printf(" host.hostid = %lu\n", pr->pr_hostid); 4784413628a7SBjoern A. Zeeb #ifdef INET 4785*eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[PR_INET] != NULL) { 4786*eb8dcdeaSGleb Smirnoff pr_family_t af = PR_INET; 4787*eb8dcdeaSGleb Smirnoff 4788*eb8dcdeaSGleb Smirnoff db_printf(" ip4s = %d\n", pr->pr_addrs[af]->ips); 4789*eb8dcdeaSGleb Smirnoff for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++) 4790b38ff370SJamie Gritton db_printf(" %s %s\n", 4791fe0518e9SBjoern A. Zeeb ii == 0 ? "ip4.addr =" : " ", 4792*eb8dcdeaSGleb Smirnoff inet_ntoa_r( 4793*eb8dcdeaSGleb Smirnoff *(const struct in_addr *)PR_IP(pr, ii), 4794*eb8dcdeaSGleb Smirnoff ip4buf)); 4795*eb8dcdeaSGleb Smirnoff } 4796413628a7SBjoern A. Zeeb #endif 4797413628a7SBjoern A. Zeeb #ifdef INET6 4798*eb8dcdeaSGleb Smirnoff if (pr->pr_addrs[PR_INET6] != NULL) { 4799*eb8dcdeaSGleb Smirnoff pr_family_t af = PR_INET6; 4800*eb8dcdeaSGleb Smirnoff 4801*eb8dcdeaSGleb Smirnoff db_printf(" ip6s = %d\n", pr->pr_addrs[af]->ips); 4802*eb8dcdeaSGleb Smirnoff for (ii = 0; ii < pr->pr_addrs[af]->ips; ii++) 4803b38ff370SJamie Gritton db_printf(" %s %s\n", 4804fe0518e9SBjoern A. Zeeb ii == 0 ? "ip6.addr =" : " ", 4805*eb8dcdeaSGleb Smirnoff ip6_sprintf(ip6buf, 4806*eb8dcdeaSGleb Smirnoff (const struct in6_addr *)PR_IP(pr, ii))); 4807*eb8dcdeaSGleb Smirnoff } 4808b38ff370SJamie Gritton #endif 4809b38ff370SJamie Gritton } 4810b38ff370SJamie Gritton 4811b38ff370SJamie Gritton DB_SHOW_COMMAND(prison, db_show_prison_command) 4812b38ff370SJamie Gritton { 4813b38ff370SJamie Gritton struct prison *pr; 4814b38ff370SJamie Gritton 4815b38ff370SJamie Gritton if (!have_addr) { 48160304c731SJamie Gritton /* 48170304c731SJamie Gritton * Show all prisons in the list, and prison0 which is not 48180304c731SJamie Gritton * listed. 48190304c731SJamie Gritton */ 48200304c731SJamie Gritton db_show_prison(&prison0); 48210304c731SJamie Gritton if (!db_pager_quit) { 4822b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) { 4823b38ff370SJamie Gritton db_show_prison(pr); 4824413628a7SBjoern A. Zeeb if (db_pager_quit) 4825413628a7SBjoern A. Zeeb break; 4826413628a7SBjoern A. Zeeb } 48270304c731SJamie Gritton } 4828b38ff370SJamie Gritton return; 4829413628a7SBjoern A. Zeeb } 4830b38ff370SJamie Gritton 48310304c731SJamie Gritton if (addr == 0) 48320304c731SJamie Gritton pr = &prison0; 48330304c731SJamie Gritton else { 4834b38ff370SJamie Gritton /* Look for a prison with the ID and with references. */ 4835b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4836b38ff370SJamie Gritton if (pr->pr_id == addr && pr->pr_ref > 0) 4837b38ff370SJamie Gritton break; 4838b38ff370SJamie Gritton if (pr == NULL) 4839b38ff370SJamie Gritton /* Look again, without requiring a reference. */ 4840b38ff370SJamie Gritton TAILQ_FOREACH(pr, &allprison, pr_list) 4841b38ff370SJamie Gritton if (pr->pr_id == addr) 4842b38ff370SJamie Gritton break; 4843b38ff370SJamie Gritton if (pr == NULL) 4844b38ff370SJamie Gritton /* Assume address points to a valid prison. */ 4845b38ff370SJamie Gritton pr = (struct prison *)addr; 48460304c731SJamie Gritton } 4847b38ff370SJamie Gritton db_show_prison(pr); 4848b38ff370SJamie Gritton } 4849b38ff370SJamie Gritton 4850413628a7SBjoern A. Zeeb #endif /* DDB */ 4851